1 /* fix-header.c - Make C header file suitable for C++.
2 Copyright (C) 1993 Free Software Foundation, Inc.
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
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.
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. */
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++:
21 * extern "C" { ... } braces are added (inside #ifndef __cplusplus),
22 if they seem to be needed. These prevent C++ compilers from name
23 mangling the functions inside the braces.
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.
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.
36 * If all of the non-comment code of the original file is protected
37 against multiple inclusion:
40 <body of include file>
42 then extra matter added to the include file is placed inside the <body>.
44 * If the input file is OK (nothing needs to be done);
45 the output file is not written (nor removed if it exists).
47 There are also some special actions that are done for certain
48 well-known standard include files:
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
55 * If argv[1] is "errno.h", errno is declared if it was missing.
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.
62 fix-header FOO.H INFILE.H OUTFILE.H REQUIRED_FUNCS <SCAN-FILE
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).
72 Written by Per Bothner <bothner@cygnus.com>, July 1993. */
76 #include <sys/types.h>
86 int partial_count = 0;
87 int missing_extern_C_count = 0;
88 int missing_errno = 0;
90 #include "xsys-protos.h"
96 /* Certain standard files get extra treatment */
105 enum special_file special_file_handling = no_special;
107 /* The following are only used when handling sys/stat.h */
108 /* They are set if the corresponding macro has been seen. */
109 int seen_S_IFBLK = 0, seen_S_ISBLK = 0;
110 int seen_S_IFCHR = 0, seen_S_ISCHR = 0;
111 int seen_S_IFDIR = 0, seen_S_ISDIR = 0;
112 int seen_S_IFIFO = 0, seen_S_ISFIFO = 0;
113 int seen_S_IFLNK = 0, seen_S_ISLNK = 0;
114 int seen_S_IFREG = 0, seen_S_ISREG = 0;
116 /* Wrapper around free, to avoid prototype clashes. */
125 /* Avoid error if config defines abort as fancy_abort.
126 It's not worth "really" implementing this because ordinary
127 compiler users never run fix-header. */
135 #define obstack_chunk_alloc xmalloc
136 #define obstack_chunk_free xfree
137 struct obstack scan_file_obstack;
139 /* NOTE: If you edit this, also edit gen-protos.c !! */
141 lookup_std_proto (name)
144 int i = hash (name) % HASH_SIZE;
149 if (hash_tab[i] == 0)
151 fn = &std_protos[hash_tab[i]];
152 if (strcmp (fn->fname, name) == 0)
154 i = (i+1) % HASH_SIZE;
161 int inc_filename_length;
162 char *progname = "fix-header";
167 int lbrac_line, rbrac_line;
169 char **required_functions;
170 int required_unseen_count;
176 if (missing_extern_C_count + required_unseen_count > 0)
177 fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
181 fprintf (outf, "#ifndef _PARAMS\n");
182 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
183 fprintf (outf, "#define _PARAMS(ARGS) ARGS\n");
184 fprintf (outf, "#else\n");
185 fprintf (outf, "#define _PARAMS(ARGS) ()\n");
186 fprintf (outf, "#endif\n#endif /* _PARAMS */\n");
192 struct partial_proto *next;
193 char *fname; /* name of function */
194 char *rtype; /* return type */
199 struct partial_proto *partial_proto_list = NULL;
201 struct partial_proto required_dummy_proto;
202 #define REQUIRED(FN) ((FN)->partial == &required_dummy_proto)
203 #define SET_REQUIRED(FN) ((FN)->partial = &required_dummy_proto)
204 #define CLEAR_REQUIRED(FN) ((FN)->partial = 0)
207 recognized_macro (fname)
210 /* The original include file defines fname as a macro. */
211 struct fn_decl *fn = lookup_std_proto (fname);
213 /* Since fname is a macro, don't require a prototype for it. */
214 if (fn && REQUIRED (fn))
217 required_unseen_count--;
220 switch (special_file_handling)
223 if (strcmp (fname, "errno") == 0) missing_errno = 0;
225 case sys_stat_special:
226 if (fname[0] == 'S' && fname[1] == '_')
228 if (strcmp (fname, "S_IFBLK") == 0) seen_S_IFBLK++;
229 else if (strcmp (fname, "S_ISBLK") == 0) seen_S_ISBLK++;
230 else if (strcmp (fname, "S_IFCHR") == 0) seen_S_IFCHR++;
231 else if (strcmp (fname, "S_ISCHR") == 0) seen_S_ISCHR++;
232 else if (strcmp (fname, "S_IFDIR") == 0) seen_S_IFDIR++;
233 else if (strcmp (fname, "S_ISDIR") == 0) seen_S_ISDIR++;
234 else if (strcmp (fname, "S_IFIFO") == 0) seen_S_IFIFO++;
235 else if (strcmp (fname, "S_ISFIFO") == 0) seen_S_ISFIFO++;
236 else if (strcmp (fname, "S_IFLNK") == 0) seen_S_IFLNK++;
237 else if (strcmp (fname, "S_ISLNK") == 0) seen_S_ISLNK++;
238 else if (strcmp (fname, "S_IFREG") == 0) seen_S_IFREG++;
239 else if (strcmp (fname, "S_ISREG") == 0) seen_S_ISREG++;
245 recognized_extern (name, type)
249 switch (special_file_handling)
252 if (strcmp (name, "errno") == 0) missing_errno = 0;
257 /* Called by scan_decls if it saw a function definition for a function
258 named FNAME, with return type RTYPE, and argument list ARGS,
259 in source file FILE_SEEN on line LINE_SEEN.
260 KIND is 'I' for an inline function;
261 'F' if a normal function declaration preceded by 'extern "C"'
262 (or nested inside 'extern "C"' braces); or
263 'f' for other function declarations. */
266 recognized_function (fname, kind, rtype, args, file_seen, line_seen)
268 int kind; /* One of 'f' 'F' or 'I' */
274 struct partial_proto *partial;
278 missing_extern_C_count++;
280 fn = lookup_std_proto (fname);
282 /* Remove the function from the list of required function. */
283 if (fn && REQUIRED (fn))
286 required_unseen_count--;
289 /* If we have a full prototype, we're done. */
293 if (kind == 'I') /* don't edit inline function */
296 /* If the partial prototype was included from some other file,
297 we don't need to patch it up (in this run). */
298 i = strlen (file_seen);
299 if (i < inc_filename_length
300 || strcmp (inc_filename, file_seen + (i - inc_filename_length)) != 0)
305 if (fn->params[0] == '\0' || strcmp (fn->params, "void") == 0)
308 /* We only have a partial function declaration,
309 so remember that we have to add a complete prototype. */
311 partial = (struct partial_proto*)
312 obstack_alloc (&scan_file_obstack, sizeof (struct partial_proto));
313 partial->fname = obstack_alloc (&scan_file_obstack, strlen (fname) + 1);
314 strcpy (partial->fname, fname);
315 partial->rtype = obstack_alloc (&scan_file_obstack, strlen (rtype) + 1);
316 strcpy (partial->rtype, rtype);
317 partial->line_seen = line_seen;
319 fn->partial = partial;
320 partial->next = partial_proto_list;
321 partial_proto_list = partial;
324 fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
325 inc_filename, fname);
330 read_scan_file (scan_file)
334 obstack_init (&scan_file_obstack);
336 scan_decls (scan_file);
338 if (missing_extern_C_count + required_unseen_count + partial_count
339 + missing_errno == 0)
342 fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
346 fprintf (stderr, "%s: fixing %s\n", progname, inc_filename);
349 if (required_unseen_count)
350 fprintf (stderr, "%s: %d missing function declarations.\n",
351 inc_filename, required_unseen_count);
353 fprintf (stderr, "%s: %d non-prototype function declarations.\n",
354 inc_filename, partial_count);
355 if (missing_extern_C_count)
357 "%s: %d declarations not protected by extern \"C\".\n",
358 inc_filename, missing_extern_C_count);
366 register struct partial_proto *partial;
368 if (required_unseen_count)
369 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
371 /* Now we print out prototypes for those functions that we haven't seen. */
372 for (rptr = required_functions; *rptr; rptr++)
374 int macro_protect = 0;
376 fn = lookup_std_proto (*rptr);
377 if (fn == NULL || !REQUIRED (fn))
380 /* In the case of memmove, protect in case the application
381 defines it as a macro before including the header. */
382 if (!strcmp (fn->fname, "memmove")
383 || !strcmp (fn->fname, "vprintf")
384 || !strcmp (fn->fname, "vfprintf")
385 || !strcmp (fn->fname, "vsprintf")
386 || !strcmp (fn->fname, "rewinddir"))
390 fprintf (outf, "#ifndef %s\n", fn->fname);
391 fprintf (outf, "extern %s %s (%s);\n",
392 fn->rtype, fn->fname, fn->params);
394 fprintf (outf, "#endif\n");
396 if (required_unseen_count)
398 "#endif /* defined(__STDC__) || defined(__cplusplus) */\n");
400 switch (special_file_handling)
404 fprintf (outf, "extern int errno;\n");
406 case sys_stat_special:
407 if (!seen_S_ISBLK && seen_S_IFBLK)
409 "#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
410 if (!seen_S_ISCHR && seen_S_IFCHR)
412 "#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
413 if (!seen_S_ISDIR && seen_S_IFDIR)
415 "#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
416 if (!seen_S_ISFIFO && seen_S_IFIFO)
418 "#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
419 if (!seen_S_ISLNK && seen_S_IFLNK)
421 "#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
422 if (!seen_S_ISREG && seen_S_IFREG)
424 "#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
429 if (missing_extern_C_count + required_unseen_count > 0)
430 fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
437 char *copy = (char *) xmalloc (strlen (str) + 1);
442 /* Returns 1 iff the file is properly protected from multiple inclusion:
449 #define INF_GET() (inf_ptr < inf_limit ? *(unsigned char*)inf_ptr++ : EOF)
450 #define INF_UNGET(c) ((c)!=EOF && inf_ptr--)
458 if (c == ' ' || c == '\t')
476 source_lineno++, lineno++;
479 else if ((c = INF_GET ()) == '/')
489 /* Read into STR from inf_buffer upto DELIM. */
492 inf_read_upto (str, delim)
500 if (ch == EOF || ch == delim)
502 SSTRING_PUT (str, ch);
504 MAKE_SSTRING_SPACE (str, 1);
510 inf_scan_ident (s, c)
515 if (isalpha (c) || c == '_')
521 if (c == EOF || !(isalnum (c) || c == '_'))
525 MAKE_SSTRING_SPACE (s, 1);
530 /* Returns 1 if the file is correctly protected against multiple
531 inclusion, setting *ifndef_line to the line number of the initial #ifndef
532 and setting *endif_line to the final #endif.
533 Otherwise return 0. */
536 check_protection (ifndef_line, endif_line)
537 int *ifndef_line, *endif_line;
540 int if_nesting = 1; /* Level of nesting of #if's */
541 char *protect_name = NULL; /* Identifier following initial #ifndef */
544 /* Skip initial white space (including comments). */
547 c = inf_skip_spaces (' ');
555 c = inf_scan_ident (&buf, inf_skip_spaces (' '));
556 if (SSTRING_LENGTH (&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
559 /* So far so good: We've seen an initial #ifndef. */
560 *ifndef_line = lineno;
561 c = inf_scan_ident (&buf, inf_skip_spaces (c));
562 if (SSTRING_LENGTH (&buf) == 0 || c == EOF)
564 protect_name = strdup (buf.base);
567 c = inf_read_upto (&buf, '\n');
574 c = inf_skip_spaces (' ');
584 c = inf_scan_ident (&buf, inf_skip_spaces (' '));
585 if (SSTRING_LENGTH (&buf) == 0)
587 else if (!strcmp (buf.base, "ifndef")
588 || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
592 else if (!strcmp (buf.base, "endif"))
598 else if (!strcmp (buf.base, "else"))
603 else if (!strcmp (buf.base, "define"))
607 c = inf_skip_spaces (c);
608 c = inf_scan_ident (&buf, c);
609 if (buf.base[0] > 0 && strcmp (buf.base, protect_name) == 0)
615 if (c == '\n' || c == EOF)
626 *endif_line = lineno;
627 /* Skip final white space (including comments). */
630 c = inf_skip_spaces (' ');
649 char *cptr, *cptr0, **pptr;
655 if (argv[0] && argv[0][0])
660 for (p = argv[0]; *p; p++)
663 progname = progname ? progname+1 : argv[0];
668 fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h req_funcs <scan-file-name\n",
673 inc_filename = argv[1];
674 inc_filename_length = strlen (inc_filename);
675 if (strcmp (inc_filename, "sys/stat.h") == 0)
676 special_file_handling = sys_stat_special;
677 else if (strcmp (inc_filename, "errno.h") == 0)
678 special_file_handling = errno_special, missing_errno = 1;
680 /* Calculate an upper bound of the number of function names in argv[4] */
681 for (i = 1, cptr = argv[4]; *cptr; cptr++)
682 if (*cptr == ' ') i++;
683 /* Find the list of prototypes required for this include file. */
684 required_functions = (char**)xmalloc ((i+1) * sizeof (char*));
685 for (cptr = argv[4], cptr0 = cptr, pptr = required_functions, done = 0;
688 done = *cptr == '\0';
689 if (*cptr == ' ' || done)
694 struct fn_decl *fn = lookup_std_proto (cptr0);
697 fprintf (stderr, "Internal error: No prototype for %s\n",
705 required_unseen_count = pptr - required_functions;
708 read_scan_file (stdin);
710 inf_fd = open (argv[2], O_RDONLY, 0666);
713 fprintf (stderr, "%s: Cannot open '%s' for reading -",
718 if (fstat (inf_fd, &sbuf) < 0)
720 fprintf (stderr, "%s: Cannot get size of '%s' -", progname, argv[2]);
724 inf_size = sbuf.st_size;
725 inf_buffer = (char*) xmalloc (inf_size + 2);
726 inf_buffer[inf_size] = '\n';
727 inf_buffer[inf_size + 1] = '\0';
728 inf_limit = inf_buffer + inf_size;
729 inf_ptr = inf_buffer;
734 long i = read (inf_fd, inf_buffer + inf_size - to_read, to_read);
737 fprintf (stderr, "%s: Failed to read '%s' -", progname, argv[2]);
751 /* If file doesn't end with '\n', add one. */
752 if (inf_limit > inf_buffer && inf_limit[-1] != '\n')
756 outf = fopen (argv[3], "w");
759 fprintf (stderr, "%s: Cannot open '%s' for writing -",
767 if (check_protection (&ifndef_line, &endif_line))
770 fprintf (stderr, "#ifndef %s on line %d; #endif on line %d\n",
771 protect_name, ifndef_line, endif_line);
773 lbrac_line = ifndef_line+1;
774 rbrac_line = endif_line;
782 /* Reset input file. */
783 inf_ptr = inf_buffer;
788 if (lineno == lbrac_line)
790 if (lineno == rbrac_line)
798 if (isalpha (c) || c == '_')
800 struct partial_proto *partial;
801 c = inf_scan_ident (&buf, c);
803 fputs (buf.base, outf);
804 fn = lookup_std_proto (buf.base);
805 /* We only want to edit the declaration matching the one
806 seen by scan-decls, as there can be multiple
807 declarations, selected by #ifdef __STDC__ or whatever. */
808 if (fn && fn->partial && fn->partial->line_seen == lineno)
810 c = inf_skip_spaces (' ');
815 c = inf_skip_spaces (' ');
818 fprintf (outf, " _PARAMS((%s))", fn->params);
827 fprintf (outf, " %c", c);