summaryrefslogtreecommitdiff
path: root/setup/update_errors.c
blob: df1f6d4e9840c5a78f868c45c0297642c077557e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 *
 * This file is part of Open Sound System.
 *
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * This this source file is released under GPL v2 license (no other versions).
 * See the COPYING file included in the main directory of this source
 * distribution for the license terms and conditions.
 *
 */

#define OSS_LICENSE ""

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include "/tmp/buildid.h"

#define MAX_ERRORS	1000

typedef struct
{
  int err;
  char *sourcename;
  int linenum;
  char *msg;
  char *desc;
} errordesc_t;

errordesc_t *errors[MAX_ERRORS];
int nerrors = 0;

char *
strmatch (char *s, const char *pattern)
{
  int i, l, pl;

  if (s == NULL || *pattern == 0)
    return NULL;

  l = strlen (s);
  pl = strlen (pattern);

  if (pl > l)
    return NULL;

  while (s && l >= pl)
    {
      int ok = 0;

      for (i = 0; !ok && i < pl; i++)
	if (s[i] != pattern[i])
	  {
	    ok = 1;
	    continue;
	  }

      if (!ok)
	return s;

      s++;
      l--;
    }
  return NULL;
}

char *
check_comment (FILE * f, const char *sourcename, int *linenum)
{
  char line[4096];
  int is_desc = 0;
  static char errorbuf[64 * 1024];
  int done = 0;

  *errorbuf = 0;

  if (fgets (line, sizeof (line) - 1, f) != NULL)
    {
      char *s, *p;
      int l;

      *linenum = *linenum + 1;

      if ((l = strlen (line)) > 4000)
	{
	  fprintf (stderr, "Too long line in %s\n", sourcename);
	  exit (-1);
	}

      /*
       * Strip trailing CR and LF characters.
       */
      s = line + l - 1;
      while (*s == '\n' || *s == '\r')
	*s-- = 0;

      s = line;

      while (*s == ' ' || *s == '\t')
	s++;

      if (s[0] != '/' || s[1] != '*')
	return NULL;

      if ((p = strmatch (s, "Errordesc:")))
	{
	  p += 10;
	  while (*p == ' ')
	    p++;

	  is_desc = 1;
	  if ((s = strmatch (p, "*/")))
	    {
	      *s = 0;
	      return p;
	    }
	  else
	    {
	      strcat (errorbuf, p);
	    }
	}
    }

/* 
 * Handle comment continuation lines.
 */

  while (!done && fgets (line, sizeof (line) - 1, f) != NULL)
    {
      char *s, *p;
      int l;

      *linenum = *linenum + 1;

      if ((l = strlen (line)) > 4000)
	{
	  fprintf (stderr, "Too long line in %s\n", sourcename);
	  exit (-1);
	}

      /*
       * Strip trailing CR and LF characters.
       */
      s = line + l - 1;
      while (*s == '\n' || *s == '\r')
	*s-- = 0;

      s = line;

      while (*s == ' ' || *s == '\t' || *s == '*')
	s++;
      if (*s == '/' && s[-1] == '*')	/* End of comment */
	break;

      if ((p = strmatch (s, "Errordesc:")))
	{
	  p += 10;
	  while (*p == ' ')
	    p++;

	  is_desc = 1;
	  if ((s = strmatch (p, "*/")))
	    {
	      *s = 0;
	      if (*errorbuf != 0)
		strcat (errorbuf, " ");
	      strcat (errorbuf, p);
	      done = 1;
	    }
	  else
	    {
	      if (*errorbuf != 0)
		strcat (errorbuf, " ");
	      strcat (errorbuf, p);
	    }
	  continue;
	}

      p = s;
      if ((s = strmatch (p, "*/")))
	{
	  *s = 0;
	  if (*errorbuf != 0)
	    strcat (errorbuf, " ");
	  strcat (errorbuf, p);
	  done = 1;
	}
      else
	{
	  if (*errorbuf != 0)
	    strcat (errorbuf, " ");
	  strcat (errorbuf, p);
	}
    }

  if (is_desc)
    return errorbuf;
  return NULL;
}

void
store_error (int errnum, const char *sourcename, int linenum, const char *msg,
	     const char *desc)
{
  errordesc_t *err;

  //printf("%s:%d: %05d=%s\n", sourcename, linenum, errnum, msg);

  if (nerrors >= MAX_ERRORS)
    {
      fprintf (stderr, "Too many errors\n");
      exit (-1);
    }

  if ((err = malloc (sizeof (*err))) == NULL)
    {
      fprintf (stderr, "Too many errors defined\n");
      exit (-1);
    }

  err->err = errnum;
  err->sourcename = strdup (sourcename);
  err->linenum = linenum;
  err->msg = strdup (msg);
  if (desc == NULL)
    err->desc = NULL;
  else
    err->desc = strdup (desc);

  errors[nerrors++] = err;
}

void
parse_sourcefile (char *sourcename)
{
  int linenum = 0;
  char line[4096];
  FILE *f;

  if (*sourcename == '.')
    sourcename += 2;		/* Skip the "./" directory prefix */

/*
 * Don't handle myself or oss_config.h.
 */

  if (strcmp (basename (sourcename), "update_errors.c") == 0)
    return;
  if (strcmp (basename (sourcename), "oss_config.h") == 0)
    return;

  if ((f = fopen (sourcename, "r")) == NULL)
    {
      perror (sourcename);
      exit (-1);
    }

  while (fgets (line, sizeof (line) - 1, f) != NULL)
    {
      char *s;
      int l;

      linenum++;

      if ((l = strlen (line)) > 4000)
	{
	  fprintf (stderr, "Too long line in %s\n", sourcename);
	  exit (-1);
	}

      /*
       * Strip trailing CR and LF characters.
       */
      s = line + l - 1;
      while (*s == '\n' || *s == '\r')
	*s-- = 0;

      if ((s = strmatch (line, "OSSERR")))
	{
	  char *p;
	  char tmp[4096];
	  char tmp2[4096];
	  int num, thisline;

	  s += 6;		/* Skip "OSSERR" */
	  while (*s == ' ' || *s == '\t' || *s == '(')
	    s++;

	  /* 
	   * Get the error number
	   */
	  p = s;
	  while (*s >= '0' && *s <= '9')
	    s++;

	  while (*s == ',' || *s == ' ' || *s == '\t' || *s == '"')
	    *s++ = 0;

	  if (sscanf (p, "%d", &num) != 1)
	    {
	      fprintf (stderr, "Bad error number in %s:%d\n",
		       sourcename, linenum);
	    }

	  p = s;

	  if (*p == 0)
	    {
	      /*
	       * No '"' was found. Read the continuation line.
	       */
	      fgets (tmp, sizeof (tmp) - 1, f);
	      p = tmp;

	      while (*p == ' ' || *p == '\t' || *p == '"')
		p++;

	      s = p;
	      while (*s && *s != '"')
		s++;
	      if (*s)
		*s++ = 0;
	    }

	  while (*s && *s != '"')
	    s++;
	  if (*s)
	    *s++ = 0;
	  // printf("%s:%d: %d=%s\n", sourcename, linenum, num, p);
	  while (*s && *s != ';')
	    s++;

	  if (*s != ';')	/* No semicolon. Discard the next line */
	    fgets (tmp2, sizeof (tmp2) - 1, f);
	  *s = 0;

	  /*
	   * Check if the next line starts the descriptive comment
	   */

	  thisline = linenum;
	  if ((s = check_comment (f, sourcename, &linenum)))
	    store_error (num, sourcename, thisline, p, s);
	  else
	    store_error (num, sourcename, thisline, p, NULL);
	}
    }

  fclose (f);
}

int
compare (const void *a_, const void *b_)
{
  const char *const *a__ = a_;
  const char *const *b__ = b_;
  const errordesc_t *a = *a__;
  const errordesc_t *b = *b__;

  if (a->err == b->err)
    return 0;
  if (a->err < b->err)
    return -1;

  return 1;
}

int
main (int argc, char *argv[])
{
  int i, status = 0;

  for (i = 1; i < argc; i++)
    {
      parse_sourcefile (argv[i]);
    }

  qsort (errors, nerrors, sizeof (errordesc_t *), compare);

  for (i = 0; i < nerrors; i++)
    {
      if (errors[i]->err == 0)
	{
	  fprintf (stderr, "%s:%d: Error number is zero\n",
		   errors[i]->sourcename, errors[i]->linenum);
	  status = 1;
	  continue;
	}

      if (i > 0 && errors[i]->err == errors[i - 1]->err)
	{
	  fprintf (stderr, "%s:%d: duplicate error %05d\n",
		   errors[i]->sourcename, errors[i]->linenum, errors[i]->err);
	  status = 1;
	  continue;
	}

      if (i < nerrors - 1 && errors[i]->err == errors[i + 1]->err)
	{
	  fprintf (stderr, "%s:%d: duplicate error %05d\n",
		   errors[i]->sourcename, errors[i]->linenum, errors[i]->err);
	  status = 1;
	  continue;
	}
    }

  printf ("<html>\n");
  printf ("<head><title>OSS run time error list for OSS v%s</title></head>\n",
	  OSS_VERSION_STRING);
  printf ("<body style=\"font-family: Helvetica,Arial,sans-serif;\">\n");
  printf ("<h2>OSS run time error list for OSS v%s</h2>\n",
	  OSS_VERSION_STRING);

  printf ("<table border=\"0\">\n");
  printf ("<tr><td><b>Code</b></td><td><b>Description</b></td>/</tr>\n");
  for (i = 0; i < nerrors; i++)
    {
#if 1
      printf
	("<tr><td style=\"vertical-align: top;\">%05d</td><td><b>%s</b>\n",
	 errors[i]->err, errors[i]->msg);

      if (errors[i]->desc != NULL)
	printf ("<br>%s\n", errors[i]->desc);
      printf ("</td></tr>\n");
#else
      printf ("<p><b>%05d: %s</b></p>\n", errors[i]->err, errors[i]->msg);

      if (errors[i]->desc != NULL)
	printf ("<ul><li>%s</li></ul>\n", errors[i]->desc);
#endif
    }
  printf ("</table>\n");
  printf ("</body>\n");
  printf ("</html>\n");

  exit (status);
}