1 /* patch-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 patch-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>
84 extern char *strcpy();
87 int partial_count = 0;
88 int missing_extern_C_count = 0;
89 int missing_extra_stuff = 0;
91 #include "xsys-protos.h"
98 /* Certain standard files get extra treatment */
107 enum special_file special_file_handling = no_special;
109 /* The following are only used when handling sys/stat.h */
110 /* They are set if the corresponding macro has been seen. */
111 int seen_S_IFBLK = 0, seen_S_ISBLK = 0;
112 int seen_S_IFCHR = 0, seen_S_ISCHR = 0;
113 int seen_S_IFDIR = 0, seen_S_ISDIR = 0;
114 int seen_S_IFIFO = 0, seen_S_ISFIFO = 0;
115 int seen_S_IFLNK = 0, seen_S_ISLNK = 0;
116 int seen_S_IFREG = 0, seen_S_ISREG = 0;
118 /* The following are only used when handling errno.h */
121 /* Wrapper around free, to avoid prototype clashes. */
129 #define obstack_chunk_alloc xmalloc
130 #define obstack_chunk_free xfree
131 struct obstack scan_file_obstack;
133 /* NOTE: If you edit this, also edit gen-protos.c !! */
135 lookup_std_proto (name)
138 int i = hash(name) % HASH_SIZE;
143 if (hash_tab[i] == 0)
145 fn = &std_protos[hash_tab[i]];
146 if (strcmp (fn->fname, name) == 0)
148 i = (i+1) % HASH_SIZE;
155 int inc_filename_length;
156 char *progname = "patch-header";
161 int lbrac_line, rbrac_line;
163 char **required_functions;
164 int required_unseen_count;
169 fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
173 fprintf (outf, "#ifndef _PARAMS\n");
174 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
175 fprintf (outf, "#define _PARAMS(ARGS) ARGS\n");
176 fprintf (outf, "#else\n");
177 fprintf (outf, "#define _PARAMS(ARGS) ()\n");
178 fprintf (outf, "#endif\n#endif /* _PARAMS */\n");
184 struct partial_proto *next;
185 char *fname; /* name of function */
186 char *rtype; /* return type */
191 struct partial_proto *partial_proto_list = NULL;
193 struct partial_proto required_dummy_proto;
194 #define REQUIRED(FN) ((FN)->partial == &required_dummy_proto)
195 #define SET_REQUIRED(FN) ((FN)->partial = &required_dummy_proto)
196 #define CLEAR_REQUIRED(FN) ((FN)->partial = 0)
199 recognized_macro (fname)
202 /* The original include file defines fname as a macro. */
203 struct fn_decl *fn = lookup_std_proto (fname);
205 /* Since fname is a macro, don't require a prototype for it. */
206 if (fn && REQUIRED (fn))
209 required_unseen_count--;
212 switch (special_file_handling)
215 if (strcmp (fname, "errno") == 0) seen_errno++;
217 case sys_stat_special:
218 if (fname[0] == 'S' && fname[1] == '_')
220 if (strcmp (fname, "S_IFBLK") == 0) seen_S_IFBLK++;
221 else if (strcmp (fname, "S_ISBLK") == 0) seen_S_ISBLK++;
222 else if (strcmp (fname, "S_IFCHR") == 0) seen_S_IFCHR++;
223 else if (strcmp (fname, "S_ISCHR") == 0) seen_S_ISCHR++;
224 else if (strcmp (fname, "S_IFDIR") == 0) seen_S_IFDIR++;
225 else if (strcmp (fname, "S_ISDIR") == 0) seen_S_ISDIR++;
226 else if (strcmp (fname, "S_IFIFO") == 0) seen_S_IFIFO++;
227 else if (strcmp (fname, "S_ISFIFO") == 0) seen_S_ISFIFO++;
228 else if (strcmp (fname, "S_IFLNK") == 0) seen_S_IFLNK++;
229 else if (strcmp (fname, "S_ISLNK") == 0) seen_S_ISLNK++;
230 else if (strcmp (fname, "S_IFREG") == 0) seen_S_IFREG++;
231 else if (strcmp (fname, "S_ISREG") == 0) seen_S_ISREG++;
237 recognized_extern (name, type)
241 switch (special_file_handling)
244 if (strcmp (name, "errno") == 0) seen_errno++;
249 /* Called by scan_decls if it saw a function definition for a function
250 named FNAME, with return type RTYPE, and argument list ARGS,
251 in source file FILE_SEEN on line LINE_SEEN.
252 KIND is 'I' for an inline function;
253 'F' if a normal function declaration preceded by 'extern "C"'
254 (or nested inside 'extern "C"' braces); or
255 'f' for other function declarations. */
258 recognized_function (fname, kind, rtype, args, file_seen, line_seen)
260 int kind; /* One of 'f' 'F' or 'I' */
266 struct partial_proto *partial;
270 missing_extern_C_count++;
272 fn = lookup_std_proto (fname);
274 /* Remove the function from the list of required function. */
275 if (fn && REQUIRED (fn))
278 required_unseen_count--;
281 /* If we have a full prototype, we're done. */
285 if (kind == 'I') /* don't edit inline function */
288 /* If the partial prototype was included from some other file,
289 we don't need to patch it up (in this run). */
290 i = strlen (file_seen);
291 if (i < inc_filename_length
292 || strcmp (inc_filename, file_seen + (i - inc_filename_length)) != 0)
297 if (fn->params[0] == '\0' || strcmp(fn->params, "void") == 0)
300 /* We only have a partial function declaration,
301 so remember that we have to add a complete prototype. */
303 partial = (struct partial_proto*)
304 obstack_alloc (&scan_file_obstack, sizeof(struct partial_proto));
305 partial->fname = obstack_alloc (&scan_file_obstack, strlen(fname) + 1);
306 strcpy (partial->fname, fname);
307 partial->rtype = obstack_alloc (&scan_file_obstack, strlen(rtype) + 1);
308 strcpy (partial->rtype, rtype);
309 partial->line_seen = line_seen;
311 fn->partial = partial;
312 partial->next = partial_proto_list;
313 partial_proto_list = partial;
316 fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
317 inc_filename, fname);
322 read_scan_file (scan_file)
326 obstack_init(&scan_file_obstack);
328 scan_decls (scan_file);
330 if (missing_extern_C_count + required_unseen_count + partial_count
331 + missing_extra_stuff == 0)
334 fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
338 fprintf (stderr, "%s: fixing %s\n", progname, inc_filename);
341 if (required_unseen_count)
342 fprintf (stderr, "%s: %d missing function declarations.\n",
343 inc_filename, required_unseen_count);
345 fprintf (stderr, "%s: %d non-prototype function declarations.\n",
346 inc_filename, partial_count);
347 if (missing_extern_C_count)
349 "%s: %d declarations not protected by extern \"C\".\n",
350 inc_filename, missing_extern_C_count);
358 register struct partial_proto *partial;
360 if (required_unseen_count)
361 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
363 /* Now we print out prototypes for those functions that we haven't seen. */
364 for (rptr = required_functions; *rptr; rptr++)
366 fn = lookup_std_proto (*rptr);
367 if (fn == NULL || !REQUIRED (fn))
369 fprintf (outf, "extern %s %s (%s);\n",
370 fn->rtype, fn->fname, fn->params);
372 if (required_unseen_count)
374 "#endif /* defined(__STDC__) || defined(__cplusplus) */\n");
376 switch (special_file_handling)
380 fprintf (outf, "extern int errno;\n");
382 case sys_stat_special:
383 if (!seen_S_ISBLK && seen_S_IFBLK)
385 "#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
386 if (!seen_S_ISCHR && seen_S_IFCHR)
388 "#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
389 if (!seen_S_ISDIR && seen_S_IFDIR)
391 "#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
392 if (!seen_S_ISFIFO && seen_S_IFIFO)
394 "#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
395 if (!seen_S_ISLNK && seen_S_IFLNK)
397 "#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
398 if (!seen_S_ISREG && seen_S_IFREG)
400 "#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
405 fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
412 return strcpy((char*)malloc (strlen (str) + 1), str);
415 /* Returns 1 iff the file is properly protected from multiple inclusion:
422 #define INF_GET() (inf_ptr < inf_limit ? *(unsigned char*)inf_ptr++ : EOF)
423 #define INF_UNGET() inf_ptr--
431 if (c == ' ' || c == '\t')
449 source_lineno++, lineno++;
452 else if ((c = INF_GET()) == '/')
462 /* Read into STR from inf_buffer upto DELIM. */
465 inf_read_upto (str, delim)
473 if (ch == EOF || ch == delim)
475 SSTRING_PUT(str, ch);
477 MAKE_SSTRING_SPACE(str, 1);
483 inf_scan_ident (s, c)
488 if (isalpha(c) || c == '_')
494 if (c == EOF || !(isalnum(c) || c == '_'))
498 MAKE_SSTRING_SPACE(s, 1);
503 /* Returns 1 if the file is correctly protected against multiple
504 inclusion, setting *ifndef_line to the line number of the initial #ifndef
505 and setting *endif_line to the final #endif.
506 Otherwise return 0. */
509 check_protection (ifndef_line, endif_line)
510 int *ifndef_line, *endif_line;
513 int if_nesting = 1; /* Level of nesting of #if's */
514 char *protect_name = NULL; /* Identifier following initial #ifndef */
517 /* Skip initial white space (including comments). */
520 c = inf_skip_spaces (' ');
528 c = inf_scan_ident (&buf, inf_skip_spaces (' '));
529 if (SSTRING_LENGTH(&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
532 /* So far so good: We've seen an initial #ifndef. */
533 *ifndef_line = lineno;
534 c = inf_scan_ident (&buf, inf_skip_spaces (c));
535 if (SSTRING_LENGTH(&buf) == 0 || c == EOF)
537 protect_name = strdup (buf.base);
540 c = inf_read_upto (&buf, '\n');
547 c = inf_skip_spaces(' ');
557 c = inf_scan_ident (&buf, inf_skip_spaces (' '));
558 if (SSTRING_LENGTH(&buf) == 0)
560 else if (!strcmp (buf.base, "ifndef")
561 || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
565 else if (!strcmp (buf.base, "endif"))
571 else if (!strcmp (buf.base, "else"))
576 else if (!strcmp (buf.base, "define"))
580 c = inf_skip_spaces (c);
581 c = inf_scan_ident (&buf, c);
582 if (buf.base[0] > 0 && strcmp(buf.base, protect_name) == 0)
588 if (c == '\n' || c == EOF)
599 *endif_line = lineno;
600 /* Skip final white space (including comments). */
603 c = inf_skip_spaces (' ');
622 char *cptr, *cptr0, **pptr;
628 if (argv[0] && argv[0][0])
633 fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h req_funcs <scan-file-name\n",
638 inc_filename = argv[1];
639 inc_filename_length = strlen (inc_filename);
640 if (strcmp (inc_filename, "sys/stat.h") == 0)
641 special_file_handling = sys_stat_special;
642 else if (strcmp (inc_filename, "errno.h") == 0)
643 special_file_handling = errno_special, missing_extra_stuff++;
645 /* Calculate an upper bound of the number of function names in argv[4] */
646 for (i = 1, cptr = argv[4]; *cptr; cptr++)
647 if (*cptr == ' ') i++;
648 /* Find the list of prototypes required for this include file. */
649 required_functions = (char**)xmalloc((i+1) * sizeof(char*));
650 for (cptr = argv[4], cptr0 = cptr, pptr = required_functions, done = 0;
653 done = *cptr == '\0';
654 if (*cptr == ' ' || done)
659 struct fn_decl *fn = lookup_std_proto(cptr0);
662 fprintf (stderr, "Internal error: No prototype for %s\n",
670 required_unseen_count = pptr - required_functions;
673 read_scan_file (stdin);
675 inf_fd = open (argv[2], O_RDONLY, 0666);
678 fprintf (stderr, "%s: Cannot open '%s' for reading -",
683 if (fstat (inf_fd, &sbuf) < 0)
685 fprintf (stderr, "%s: Cannot get size of '%s' -", progname, argv[2]);
689 inf_size = sbuf.st_size;
690 inf_buffer = (char*) xmalloc (inf_size + 2);
691 inf_buffer[inf_size] = '\n';
692 inf_buffer[inf_size + 1] = '\0';
693 inf_limit = inf_buffer + inf_size;
694 inf_ptr = inf_buffer;
699 long i = read (inf_fd, inf_buffer + inf_size - to_read, to_read);
702 fprintf (stderr, "%s: Failed to read '%s' -", progname, argv[2]);
716 outf = fopen (argv[3], "w");
719 fprintf (stderr, "%s: Cannot open '%s' for writing -",
727 if (check_protection (&ifndef_line, &endif_line))
730 fprintf(stderr, "#ifndef %s on line %d; #endif on line %d\n",
731 protect_name, ifndef_line, endif_line);
733 lbrac_line = ifndef_line+1;
734 rbrac_line = endif_line;
742 /* Reset input file. */
743 inf_ptr = inf_buffer;
748 if (lineno == lbrac_line)
750 if (lineno == rbrac_line)
758 if (isalpha (c) || c == '_')
760 struct partial_proto *partial;
761 c = inf_scan_ident (&buf, c);
763 fputs (buf.base, outf);
764 fn = lookup_std_proto (buf.base);
765 /* We only want to edit the declaration matching the one
766 seen by scan-decls, as there can be multiple
767 declarations, selected by #ifdef __STDC__ or whatever. */
768 if (fn && fn->partial && fn->partial->line_seen == lineno)
770 c = inf_skip_spaces (' ');
775 c = inf_skip_spaces (' ');
778 fprintf (outf, " _PARAMS((%s))", fn->params);
787 fprintf (outf, " %c", c);