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 prevcnt 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. */
79 extern char *strcpy();
82 int partial_count = 0;
83 int missing_extern_C_count = 0;
84 int missing_extra_stuff = 0;
86 #include "xsys-protos.h"
88 /* Certain standard files get extra treatment */
97 enum special_file special_file_handling = no_special;
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;
108 /* The following are only used when handling errno.h */
111 /* Wrapper around free, to avoid prototype clashes. */
119 #define obstack_chunk_alloc xmalloc
120 #define obstack_chunk_free xfree
121 struct obstack scan_file_obstack;
123 /* NOTE: If you edit this, also edit gen-protos.c !! */
125 lookup_std_proto (name)
128 int i = hash(name) % HASH_SIZE;
133 if (hash_tab[i] == 0)
135 fn = &std_protos[hash_tab[i]];
136 if (strcmp (fn->fname, name) == 0)
138 i = (i+1) % HASH_SIZE;
145 int inc_filename_length;
146 char *progname = "patch-header";
151 int lbrac_line, rbrac_line;
153 char **required_functions;
154 int required_unseen_count;
159 fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
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");
174 struct partial_proto *next;
175 char *fname; /* name of function */
176 char *rtype; /* return type */
181 struct partial_proto *partial_proto_list = NULL;
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)
189 read_scan_file (scan_file)
194 obstack_init(&scan_file_obstack);
198 struct partial_proto *partial;
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');
208 for (ptr = fname; *ptr != ';'; ) ptr++;
211 for (ptr = extern_C; *ptr != ';'; ) ptr++;
214 if (*extern_C == 'X')
216 switch (special_file_handling)
219 if (strcmp (fname, "errno") == 0) seen_errno++;
225 if (*extern_C == 'M')
227 /* The original include file defines fname as a macro. */
228 fn = lookup_std_proto (fname);
230 /* Since fname is a macro, don't require a prototype for it. */
231 if (fn && REQUIRED (fn))
234 required_unseen_count--;
237 switch (special_file_handling)
240 if (strcmp (fname, "errno") == 0) seen_errno++;
242 case sys_stat_special:
243 if (fname[0] == 'S' && fname[1] == '_')
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++;
264 for (ptr = rtype; *ptr != ';'; ) ptr++;
267 for (ptr = args; *ptr != ';'; ) ptr++;
270 for (ptr = file_seen; *ptr != ';'; ) ptr++;
273 for (ptr = line_seen; *ptr != ';'; ) ptr++;
276 if (extern_C[0] == 'f')
277 missing_extern_C_count++;
279 fn = lookup_std_proto (fname);
281 /* Remove the function from the list of required function. */
282 if (fn && REQUIRED (fn))
285 required_unseen_count--;
288 /* If we have a full prototype, we're done. */
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)
301 if (fn->fname[0] == '\0' || strcmp(fn->fname, "void") == 0)
304 /* We only have a partial function declaration,
305 so remember that we have to add a complete prototype. */
307 partial = (struct partial_proto*)
308 obstack_alloc (&scan_file_obstack, sizeof(struct partial_proto));
309 partial->fname = obstack_alloc (&scan_file_obstack, strlen(fname) + 1);
310 strcpy (partial->fname, fname);
311 partial->rtype = obstack_alloc (&scan_file_obstack, strlen(rtype) + 1);
312 strcpy (partial->rtype, rtype);
313 partial->line_seen = atoi(line_seen);
315 fn->partial = partial;
316 partial->next = partial_proto_list;
317 partial_proto_list = partial;
320 fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
321 inc_filename, fname);
325 if (missing_extern_C_count + required_unseen_count + partial_count
326 + missing_extra_stuff == 0)
329 fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
332 if (required_unseen_count)
333 fprintf (stderr, "%s: %d missing function declarations.\n",
334 inc_filename, required_unseen_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);
347 register struct partial_proto *partial;
349 if (required_unseen_count)
350 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
352 /* Now we print out prototypes for those functions that we haven't seen. */
353 for (rptr = required_functions; *rptr; rptr++)
355 fn = lookup_std_proto (*rptr);
356 if (fn == NULL || !REQUIRED (fn))
358 fprintf (outf, "extern %s %s (%s);\n",
359 fn->rtype, fn->fname, fn->params);
361 if (required_unseen_count)
363 "#endif /* defined(__STDC__) || defined(__cplusplus) */\n");
365 switch (special_file_handling)
369 fprintf (outf, "extern int errno;\n");
371 case sys_stat_special:
372 if (!seen_S_ISBLK && seen_S_IFBLK)
374 "#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
375 if (!seen_S_ISCHR && seen_S_IFCHR)
377 "#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
378 if (!seen_S_ISDIR && seen_S_IFDIR)
380 "#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
381 if (!seen_S_ISFIFO && seen_S_IFIFO)
383 "#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
384 if (!seen_S_ISLNK && seen_S_IFLNK)
386 "#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
387 if (!seen_S_ISREG && seen_S_IFREG)
389 "#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
394 fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
401 return strcpy((char*)malloc (strlen (str) + 1), str);
404 /* Returns 1 iff the file is properly protected from multiple inclusion:
412 check_protection (inf, ifndef_line, endif_line)
414 int *ifndef_line, *endif_line;
417 int if_nesting = 1; /* Level of nesting of #if's */
418 char *protect_name = NULL; /* Identifier following initial #ifndef */
421 /* Skip initial white space (including comments). */
424 c = skip_spaces (inf, ' ');
432 c = scan_ident (inf, &buf, skip_spaces (inf, ' '));
433 if (SSTRING_LENGTH(&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
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)
441 protect_name = strdup (buf.base);
444 c = read_upto (inf, &buf, '\n');
451 c = skip_spaces(inf, ' ');
461 c = scan_ident (inf, &buf, skip_spaces (inf, ' '));
462 if (SSTRING_LENGTH(&buf) == 0)
464 else if (!strcmp (buf.base, "ifndef")
465 || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
469 else if (!strcmp (buf.base, "endif"))
475 else if (!strcmp (buf.base, "else"))
480 else if (!strcmp (buf.base, "define"))
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)
492 if (c == '\n' || c == EOF)
503 *endif_line = lineno;
504 /* Skip final white space (including comments). */
507 c = skip_spaces (inf, ' ');
525 char *cptr, *cptr0, **pptr;
530 if (argv[0] && argv[0][0])
535 fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h req_funcs <scan-file-name\n",
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++;
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;
555 done = *cptr == '\0';
556 if (*cptr == ' ' || done)
561 struct fn_decl *fn = lookup_std_proto(cptr0);
564 fprintf (stderr, "Internal error: No prototype for %s\n",
572 required_unseen_count = pptr - required_functions;
575 read_scan_file (stdin);
577 inf = fopen (argv[2], "r");
580 fprintf (stderr, "%s: Cannot open '%s' for reading -",
586 outf = fopen (argv[3], "w");
589 fprintf (stderr, "%s: Cannot open '%s' for writing -",
595 if (check_protection (inf, &ifndef_line, &endif_line))
598 fprintf(stderr, "#ifndef %s on line %d; #endif on line %d\n",
599 protect_name, ifndef_line, endif_line);
601 lbrac_line = ifndef_line+1;
602 rbrac_line = endif_line;
615 if (lineno == lbrac_line)
617 if (lineno == rbrac_line)
625 if (isalpha (c) || c == '_')
627 struct partial_proto *partial;
629 if (get_token (inf, &buf) != IDENTIFIER_TOKEN)
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)
638 c = skip_spaces (inf, ' ');
643 c = skip_spaces (inf, ' ');
646 fprintf (outf, " _PARAMS((%s))", fn->params);