]> gcc.gnu.org Git - gcc.git/blob - gcc/fix-header.c
Initial revision
[gcc.git] / gcc / fix-header.c
1 /* patch-header.c - Make C header file suitable for C++.
2 Copyright (C) 1993 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 /* This program massages a system include file (such as stdio.h),
19 into a form more conformant with ANSI/POSIX, and more suitable for C++:
20
21 * extern "C" { ... } braces are added (inside #ifndef __cplusplus),
22 if they seem to be needed. These prevcnt C++ compilers from name
23 mangling the functions inside the braces.
24
25 * If an old-style incomplete function declaration is seen (without
26 an argument list), and it is a "standard" function listed in
27 the file sys-protos.h (and with a non-empty argument list), then
28 the declaration is converted to a complete prototype by replacing
29 the empty parameter list with the argument lust from sys-protos.h.
30
31 * The program can be given a list of (names of) required standard
32 functions (such as fclose for stdio.h). If a reqquired function
33 is not seen in the input, then a prototype for it will be
34 written to the output.
35
36 * If all of the non-comment code of the original file is protected
37 against multiple inclusion:
38 #ifndef FOO
39 #define FOO
40 <body of include file>
41 #endif
42 then extra matter added to the include file is placed inside the <body>.
43
44 * If the input file is OK (nothing needs to be done);
45 the output file is not written (nor removed if it exists).
46
47 There are also some special actions that are done for certain
48 well-known standard include files:
49
50 * If argv[1] is "sys/stat.h", the Posix.1 macros
51 S_ISBLK, S_ISCHR, S_ISDIR, S_ISFIFO, S_ISLNK, S_ISREG are added if
52 they were missing, and the corresponding "traditional" S_IFxxx
53 macros were defined.
54
55 * If argv[1] is "errno.h", errno is declared if it was missing.
56
57 * TODO: The input file should be read complete into memory, because:
58 a) it needs to be scanned twice anyway, and
59 b) it would be nice to allow update in place.
60
61 Usage:
62 patch-header FOO.H INFILE.H OUTFILE.H REQUIRED_FUNCS <SCAN-FILE
63 where:
64 * FOO.H is the relative file name of the include file,
65 as it would be #include'd by a C file. (E.g. stdio.h)
66 * INFILE.H is a full pathname for the input file (e.g. /usr/include/stdio.h)
67 * OUTFILE.H is the full pathname for where to write the output file,
68 if anything needs to be done. (e.g. ./include/stdio.h)
69 * SCAN-FILE is the output of the scan-decls program.
70 * REQUIRED_FUNCS is a list of required function (e.g. fclose for stdio.h).
71
72 Written by Per Bothner <bothner@cygnus.com>, July 1993. */
73
74 #include <stdio.h>
75 #include <ctype.h>
76 #include "obstack.h"
77 #include "scan.h"
78
79 extern char *strcpy();
80 sstring buf;
81 int verbose = 0;
82 int partial_count = 0;
83 int missing_extern_C_count = 0;
84 int missing_extra_stuff = 0;
85
86 #include "xsys-protos.h"
87
88 /* Certain standard files get extra treatment */
89
90 enum special_file
91 {
92 no_special,
93 errno_special,
94 sys_stat_special
95 };
96
97 enum special_file special_file_handling = no_special;
98
99 /* The following are only used when handling sys/stat.h */
100 /* They are set if the corresponding macro has been seen. */
101 int seen_S_IFBLK = 0, seen_S_ISBLK = 0;
102 int seen_S_IFCHR = 0, seen_S_ISCHR = 0;
103 int seen_S_IFDIR = 0, seen_S_ISDIR = 0;
104 int seen_S_IFIFO = 0, seen_S_ISFIFO = 0;
105 int seen_S_IFLNK = 0, seen_S_ISLNK = 0;
106 int seen_S_IFREG = 0, seen_S_ISREG = 0;
107
108 /* The following are only used when handling errno.h */
109 int seen_errno = 0;
110
111 /* Wrapper around free, to avoid prototype clashes. */
112
113 void xfree (ptr)
114 char *ptr;
115 {
116 free(ptr);
117 }
118
119 #define obstack_chunk_alloc xmalloc
120 #define obstack_chunk_free xfree
121 struct obstack scan_file_obstack;
122
123 /* NOTE: If you edit this, also edit gen-protos.c !! */
124 struct fn_decl *
125 lookup_std_proto (name)
126 char *name;
127 {
128 int i = hash(name) % HASH_SIZE;
129 int i0 = i;
130 for (;;)
131 {
132 struct fn_decl *fn;
133 if (hash_tab[i] == 0)
134 return NULL;
135 fn = &std_protos[hash_tab[i]];
136 if (strcmp (fn->fname, name) == 0)
137 return fn;
138 i = (i+1) % HASH_SIZE;
139 if (i == i0)
140 abort();
141 }
142 }
143
144 char *inc_filename;
145 int inc_filename_length;
146 char *progname = "patch-header";
147 FILE *outf;
148 sstring buf;
149 sstring line;
150
151 int lbrac_line, rbrac_line;
152
153 char **required_functions;
154 int required_unseen_count;
155
156 int
157 write_lbrac ()
158 {
159 fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
160
161 if (partial_count)
162 {
163 fprintf (outf, "#ifndef _PARAMS\n");
164 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
165 fprintf (outf, "#define _PARAMS(ARGS) ARGS\n");
166 fprintf (outf, "#else\n");
167 fprintf (outf, "#define _PARAMS(ARGS) ()\n");
168 fprintf (outf, "#endif\n#endif /* _PARAMS */\n");
169 }
170 }
171
172 struct partial_proto
173 {
174 struct partial_proto *next;
175 char *fname; /* name of function */
176 char *rtype; /* return type */
177 struct fn_decl *fn;
178 int line_seen;
179 };
180
181 struct partial_proto *partial_proto_list = NULL;
182
183 struct partial_proto required_dummy_proto;
184 #define REQUIRED(FN) ((FN)->partial == &required_dummy_proto)
185 #define SET_REQUIRED(FN) ((FN)->partial = &required_dummy_proto)
186 #define CLEAR_REQUIRED(FN) ((FN)->partial = 0)
187
188 void
189 read_scan_file (scan_file)
190 FILE *scan_file;
191 {
192 char **rptr;
193 int i;
194 obstack_init(&scan_file_obstack);
195
196 for (;;)
197 {
198 struct partial_proto *partial;
199 struct fn_decl *fn;
200 int ch;
201 char *ptr, *fname, *extern_C, *rtype, *args, *file_seen, *line_seen;
202 line.ptr = line.base;
203 ch = read_upto (scan_file, &line, '\n');
204 if (ch == EOF)
205 break;
206
207 fname = line.base;
208 for (ptr = fname; *ptr != ';'; ) ptr++;
209 *ptr = 0;
210 extern_C = ptr + 1;
211 for (ptr = extern_C; *ptr != ';'; ) ptr++;
212 *ptr = 0;
213
214 if (*extern_C == 'X')
215 {
216 switch (special_file_handling)
217 {
218 case errno_special:
219 if (strcmp (fname, "errno") == 0) seen_errno++;
220 break;
221 }
222 continue;
223 }
224
225 if (*extern_C == 'M')
226 {
227 /* The original include file defines fname as a macro. */
228 fn = lookup_std_proto (fname);
229
230 /* Since fname is a macro, don't require a prototype for it. */
231 if (fn && REQUIRED (fn))
232 {
233 CLEAR_REQUIRED(fn);
234 required_unseen_count--;
235 }
236
237 switch (special_file_handling)
238 {
239 case errno_special:
240 if (strcmp (fname, "errno") == 0) seen_errno++;
241 break;
242 case sys_stat_special:
243 if (fname[0] == 'S' && fname[1] == '_')
244 {
245 if (strcmp (fname, "S_IFBLK") == 0) seen_S_IFBLK++;
246 else if (strcmp (fname, "S_ISBLK") == 0) seen_S_ISBLK++;
247 else if (strcmp (fname, "S_IFCHR") == 0) seen_S_IFCHR++;
248 else if (strcmp (fname, "S_ISCHR") == 0) seen_S_ISCHR++;
249 else if (strcmp (fname, "S_IFDIR") == 0) seen_S_IFDIR++;
250 else if (strcmp (fname, "S_ISDIR") == 0) seen_S_ISDIR++;
251 else if (strcmp (fname, "S_IFIFO") == 0) seen_S_IFIFO++;
252 else if (strcmp (fname, "S_ISFIFO") == 0) seen_S_ISFIFO++;
253 else if (strcmp (fname, "S_IFLNK") == 0) seen_S_IFLNK++;
254 else if (strcmp (fname, "S_ISLNK") == 0) seen_S_ISLNK++;
255 else if (strcmp (fname, "S_IFREG") == 0) seen_S_IFREG++;
256 else if (strcmp (fname, "S_ISREG") == 0) seen_S_ISREG++;
257 }
258 break;
259 }
260 continue;
261 }
262
263 rtype = ptr + 1;
264 for (ptr = rtype; *ptr != ';'; ) ptr++;
265 *ptr = 0;
266 args = ptr + 1;
267 for (ptr = args; *ptr != ';'; ) ptr++;
268 *ptr = 0;
269 file_seen = ptr + 1;
270 for (ptr = file_seen; *ptr != ';'; ) ptr++;
271 *ptr = 0;
272 line_seen = ptr + 1;
273 for (ptr = line_seen; *ptr != ';'; ) ptr++;
274 *ptr = 0;
275
276 if (extern_C[0] == 'f')
277 missing_extern_C_count++;
278
279 fn = lookup_std_proto (fname);
280
281 /* Remove the function from the list of required function. */
282 if (fn && REQUIRED (fn))
283 {
284 CLEAR_REQUIRED(fn);
285 required_unseen_count--;
286 }
287
288 /* If we have a full prototype, we're done. */
289 if (args[0] != '\0')
290 continue;
291
292 /* If the partial prototype was included from some other file,
293 we don't need to patch it up (in this run). */
294 i = strlen (file_seen);
295 if (i < inc_filename_length
296 || strcmp (inc_filename, file_seen + (i - inc_filename_length)) != 0)
297 continue;
298
299 if (fn == NULL)
300 continue;
301 if (fn->fname[0] == '\0' || strcmp(fn->fname, "void") == 0)
302 continue;
303
304 /* We only have a partial function declaration,
305 so remember that we have to add a complete prototype. */
306 partial_count++;
307 partial = (struct partial_proto*)
308 obstack_alloc (&scan_file_obstack, sizeof(struct partial_proto));
309 partial->fname
310 = obstack_copy0 (&scan_file_obstack, fname, strlen (fname));
311 partial->rtype
312 = obstack_copy0 (&scan_file_obstack, rtype, strlen (rtype));
313 partial->line_seen = atoi(line_seen);
314 partial->fn = fn;
315 fn->partial = partial;
316 partial->next = partial_proto_list;
317 partial_proto_list = partial;
318 if (verbose)
319 {
320 fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
321 inc_filename, fname);
322 }
323 }
324
325 if (missing_extern_C_count + required_unseen_count + partial_count
326 + missing_extra_stuff == 0)
327 {
328 if (verbose)
329 fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
330 exit (0);
331 }
332 if (required_unseen_count)
333 fprintf (stderr, "%s: %d missing function declarations.\n",
334 inc_filename, required_unseen_count);
335 if (partial_count)
336 fprintf (stderr, "%s: %d non-prototype function declarations.\n",
337 inc_filename, partial_count);
338 if (missing_extern_C_count)
339 fprintf (stderr, "%s: %d declarations not protected by extern \"C\".\n",
340 inc_filename, missing_extern_C_count);
341 }
342
343 write_rbrac ()
344 {
345 struct fn_decl *fn;
346 char **rptr;
347 register struct partial_proto *partial;
348
349 if (required_unseen_count)
350 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
351
352 /* Now we print out prototypes for those functions that we haven't seen. */
353 for (rptr = required_functions; *rptr; rptr++)
354 {
355 fn = lookup_std_proto (*rptr);
356 if (fn == NULL || !REQUIRED (fn))
357 continue;
358 fprintf (outf, "extern %s %s (%s);\n",
359 fn->rtype, fn->fname, fn->params);
360 }
361 if (required_unseen_count)
362 fprintf (outf,
363 "#endif /* defined(__STDC__) || defined(__cplusplus) */\n");
364
365 switch (special_file_handling)
366 {
367 case errno_special:
368 if (!seen_errno)
369 fprintf (outf, "extern int errno;\n");
370 break;
371 case sys_stat_special:
372 if (!seen_S_ISBLK && seen_S_IFBLK)
373 fprintf (outf,
374 "#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
375 if (!seen_S_ISCHR && seen_S_IFCHR)
376 fprintf (outf,
377 "#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
378 if (!seen_S_ISDIR && seen_S_IFDIR)
379 fprintf (outf,
380 "#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
381 if (!seen_S_ISFIFO && seen_S_IFIFO)
382 fprintf (outf,
383 "#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
384 if (!seen_S_ISLNK && seen_S_IFLNK)
385 fprintf (outf,
386 "#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
387 if (!seen_S_ISREG && seen_S_IFREG)
388 fprintf (outf,
389 "#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
390 break;
391 }
392
393
394 fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
395 }
396
397 char *
398 strdup (str)
399 char *str;
400 {
401 return strcpy((char*)malloc (strlen (str) + 1), str);
402 }
403
404 /* Returns 1 iff the file is properly protected from multiple inclusion:
405 #ifndef PROTECT_NAME
406 #define PROTECT_NAME
407 #endif
408
409 */
410
411 int
412 check_protection (inf, ifndef_line, endif_line)
413 FILE *inf;
414 int *ifndef_line, *endif_line;
415 {
416 int c;
417 int if_nesting = 1; /* Level of nesting of #if's */
418 char *protect_name = NULL; /* Identifier following initial #ifndef */
419 int define_seen = 0;
420
421 /* Skip initial white space (including comments). */
422 for (;; lineno++)
423 {
424 c = skip_spaces (inf, ' ');
425 if (c == EOF)
426 return 0;
427 if (c != '\n')
428 break;
429 }
430 if (c != '#')
431 return 0;
432 c = scan_ident (inf, &buf, skip_spaces (inf, ' '));
433 if (SSTRING_LENGTH(&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
434 return 0;
435
436 /* So far so good: We've seen an initial #ifndef. */
437 *ifndef_line = lineno;
438 c = scan_ident (inf, &buf, skip_spaces (inf, c));
439 if (SSTRING_LENGTH(&buf) == 0 || c == EOF)
440 return 0;
441 protect_name = strdup (buf.base);
442
443 ungetc (c, inf);
444 c = read_upto (inf, &buf, '\n');
445 if (c == EOF)
446 return 0;
447 lineno++;
448
449 for (;;)
450 {
451 c = skip_spaces(inf, ' ');
452 if (c == EOF)
453 return 0;
454 if (c == '\n')
455 {
456 lineno++;
457 continue;
458 }
459 if (c != '#')
460 goto skip_to_eol;
461 c = scan_ident (inf, &buf, skip_spaces (inf, ' '));
462 if (SSTRING_LENGTH(&buf) == 0)
463 ;
464 else if (!strcmp (buf.base, "ifndef")
465 || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
466 {
467 if_nesting++;
468 }
469 else if (!strcmp (buf.base, "endif"))
470 {
471 if_nesting--;
472 if (if_nesting == 0)
473 break;
474 }
475 else if (!strcmp (buf.base, "else"))
476 {
477 if (if_nesting == 1)
478 return 0;
479 }
480 else if (!strcmp (buf.base, "define"))
481 {
482 if (if_nesting != 1)
483 goto skip_to_eol;
484 c = skip_spaces (inf, c);
485 c = scan_ident (inf, &buf, c);
486 if (buf.base[0] > 0 && strcmp(buf.base, protect_name) == 0)
487 define_seen = 1;
488 }
489 skip_to_eol:
490 for (;;)
491 {
492 if (c == '\n' || c == EOF)
493 break;
494 c = getc (inf);
495 }
496 if (c == EOF)
497 return 0;
498 lineno++;
499 }
500
501 if (!define_seen)
502 return 0;
503 *endif_line = lineno;
504 /* Skip final white space (including comments). */
505 for (;;)
506 {
507 c = skip_spaces (inf, ' ');
508 if (c == EOF)
509 break;
510 if (c != '\n')
511 return 0;
512 }
513
514 return 1;
515 }
516
517 int
518 main(argc, argv)
519 int argc;
520 char **argv;
521 {
522 FILE *inf;
523 int c;
524 int i, done;
525 char *cptr, *cptr0, **pptr;
526 int ifndef_line;
527 int endif_line;;
528
529
530 if (argv[0] && argv[0][0])
531 progname = argv[0];
532
533 if (argc < 4)
534 {
535 fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h req_funcs <scan-file-name\n",
536 progname);
537 exit (-1);
538 }
539
540 inc_filename = argv[1];
541 inc_filename_length = strlen (inc_filename);
542 if (strcmp (inc_filename, "sys/stat.h") == 0)
543 special_file_handling = sys_stat_special;
544 else if (strcmp (inc_filename, "errno.h") == 0)
545 special_file_handling = errno_special, missing_extra_stuff++;
546
547 /* Calculate an upper bound of the number of function names in argv[4] */
548 for (i = 1, cptr = argv[4]; *cptr; cptr++)
549 if (*cptr == ' ') i++;
550 /* Find the list of prototypes required for this include file. */
551 required_functions = (char**)xmalloc((i+1) * sizeof(char*));
552 for (cptr = argv[4], cptr0 = cptr, pptr = required_functions, done = 0;
553 !done; cptr++)
554 {
555 done = *cptr == '\0';
556 if (*cptr == ' ' || done)
557 {
558 *cptr = '\0';
559 if (cptr > cptr0)
560 {
561 struct fn_decl *fn = lookup_std_proto(cptr0);
562 *pptr++ = cptr0;
563 if (fn == NULL)
564 fprintf (stderr, "Internal error: No prototype for %s\n",
565 cptr0);
566 else
567 SET_REQUIRED(fn);
568 }
569 cptr0 = cptr + 1;
570 }
571 }
572 required_unseen_count = pptr - required_functions;
573 *pptr = 0;
574
575 read_scan_file (stdin);
576
577 inf = fopen (argv[2], "r");
578 if (inf == NULL)
579 {
580 fprintf (stderr, "%s: Cannot open '%s' for reading -",
581 progname, argv[2]);
582 perror (NULL);
583 exit (-1);
584 }
585
586 outf = fopen (argv[3], "w");
587 if (outf == NULL)
588 {
589 fprintf (stderr, "%s: Cannot open '%s' for writing -",
590 progname, argv[3]);
591 perror (NULL);
592 exit (-1);
593 }
594
595 if (check_protection (inf, &ifndef_line, &endif_line))
596 {
597 #if 0
598 fprintf(stderr, "#ifndef %s on line %d; #endif on line %d\n",
599 protect_name, ifndef_line, endif_line);
600 #endif
601 lbrac_line = ifndef_line+1;
602 rbrac_line = endif_line;
603 }
604 else
605 {
606 lbrac_line = 1;
607 rbrac_line = -1;
608 }
609
610 fseek(inf, 0, 0);
611 lineno = 1;
612
613 for (;;)
614 {
615 if (lineno == lbrac_line)
616 write_lbrac ();
617 if (lineno == rbrac_line)
618 write_rbrac ();
619 for (;;)
620 {
621 struct fn_decl *fn;
622 c = getc (inf);
623 if (c == EOF)
624 break;
625 if (isalpha (c) || c == '_')
626 {
627 struct partial_proto *partial;
628 ungetc (c, inf);
629 if (get_token (inf, &buf) != IDENTIFIER_TOKEN)
630 abort ();
631 fputs (buf.base, outf);
632 fn = lookup_std_proto (buf.base);
633 /* We only want to edit the declaration matching the one
634 seen by scan-decls, as there can be multiple
635 declarations, selected by #ifdef __STDC__ or whatever. */
636 if (fn && fn->partial && fn->partial->line_seen == lineno)
637 {
638 c = skip_spaces (inf, ' ');
639 if (c == EOF)
640 break;
641 if (c == '(')
642 {
643 c = skip_spaces (inf, ' ');
644 if (c == ')')
645 {
646 fprintf (outf, " _PARAMS((%s))", fn->params);
647 }
648 else
649 {
650 putc ('(', outf);
651 ungetc (c, inf);
652 }
653 }
654 else
655 putc (c, outf);
656 }
657 }
658 else
659 putc (c, outf);
660 if (c == '\n')
661 break;
662 }
663 if (c == EOF)
664 break;
665 lineno++;
666 }
667 if (rbrac_line < 0)
668 write_rbrac ();
669
670 fclose (inf);
671 fclose (outf);
672
673 return 0;
674 }
This page took 0.072658 seconds and 5 git commands to generate.