1 /* fix-header.c - Make C header file suitable for C++.
2 Copyright (C) 1993, 1994, 1995 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* This program massages a system include file (such as stdio.h),
19 into a form more conforming 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 required 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 [OPTIONS]
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 * OPTIONS are such as you would pass to cpp.
71 Written by Per Bothner <bothner@cygnus.com>, July 1993. */
84 #define const /* nothing */
90 int partial_count = 0;
93 /* We no longer need to add extern "C", because cpp implicitly
94 forces the standard include files to be treated as C. */
95 /*#define ADD_MISSING_EXTERN_C 1 */
97 #if ADD_MISSING_EXTERN_C
98 int missing_extern_C_count = 0;
100 int missing_errno = 0;
102 #include "xsys-protos.h"
104 #ifdef FIXPROTO_IGNORE_LIST
105 /* This is a currently unused feature. */
107 /* List of files and directories to ignore.
108 A directory name (ending in '/') means ignore anything in that
109 directory. (It might be more efficient to do directory pruning
110 earlier in fixproto, but this is simpler and easier to customize.) */
112 static char *files_to_ignore[] = {
123 /* Certain standard files get extra treatment */
133 /* A NAMELIST is a sequence of names, separated by '\0', and terminated
134 by an empty name (i.e. by "\0\0"). */
136 typedef const char* namelist;
138 struct std_include_entry {
145 /* End of namelist NAMES. */
151 register namelist ptr;
152 for (ptr = names; ; ptr++)
163 const char NONE[] = "";
165 struct std_include_entry *include_entry;
167 struct std_include_entry std_include_table [] = {
169 "isalnum\0isalpha\0iscntrl\0isdigit\0isgraph\0islower\0\
170 isprint\0ispunct\0isspace\0isupper\0isxdigit\0tolower\0toupper\0", NONE },
172 { "dirent.h", "closedir\0opendir\0readdir\0rewinddir\0", NONE},
174 { "errno.h", NONE, "errno\0" },
176 { "curses.h", "box\0delwin\0endwin\0getcurx\0getcury\0initscr\0\
177 mvcur\0mvwprintw\0mvwscanw\0newwin\0overlay\0overwrite\0\
178 scroll\0subwin\0touchwin\0waddstr\0wclear\0wclrtobot\0wclrtoeol\0\
179 waddch\0wdelch\0wdeleteln\0werase\0wgetch\0wgetstr\0winsch\0winsertln\0\
180 wmove\0wprintw\0wrefresh\0wscanw\0wstandend\0wstandout\0", NONE },
182 { "fcntl.h", "creat\0fcntl\0open\0", NONE },
184 /* Maybe also "getgrent fgetgrent setgrent endgrent" */
185 { "grp.h", "getgrgid\0getgrnam\0", NONE },
187 /*{ "limit.h", ... provided by gcc }, */
189 { "locale.h", "localeconv\0setlocale\0", NONE },
191 { "math.h", "acos\0asin\0atan\0atan2\0ceil\0cos\0cosh\0exp\0\
192 fabs\0floor\0fmod\0frexp\0ldexp\0log10\0log\0modf\0pow\0sin\0sinh\0sqrt\0\
193 tan\0tanh\0", "HUGE_VAL\0" },
195 { "pwd.h", "getpwnam\0getpwuid\0", NONE },
197 /* Left out siglongjmp sigsetjmp - these depend on sigjmp_buf. */
198 { "setjmp.h", "longjmp\0setjmp\0", NONE },
200 /* Left out signal() - its prototype is too complex for us!
201 Also left out "sigaction sigaddset sigdelset sigemptyset
202 sigfillset sigismember sigpending sigprocmask sigsuspend"
203 because these need sigset_t or struct sigaction.
204 Most systems that provide them will also declare them. */
205 { "signal.h", "kill\0raise\0", NONE },
207 { "stdio.h", "clearerr\0fclose\0feof\0ferror\0fflush\0fgetc\0fgetpos\0\
208 fgets\0fopen\0fprintf\0fputc\0fputs\0fread\0freopen\0fscanf\0fseek\0\
209 fsetpos\0ftell\0fwrite\0getc\0getchar\0gets\0pclose\0perror\0popen\0\
210 printf\0putc\0putchar\0puts\0remove\0rename\0rewind\0scanf\0setbuf\0\
211 setvbuf\0sprintf\0sscanf\0vprintf\0vsprintf\0vfprintf\0tmpfile\0\
212 tmpnam\0ungetc\0", NONE },
213 /* Should perhaps also handle NULL, EOF, ... ? */
215 /* "div ldiv", - ignored because these depend on div_t, ldiv_t
216 ignore these: "mblen mbstowcs mbstowc wcstombs wctomb"
217 Left out getgroups, because SunOS4 has incompatible BSD and SVR4 versions.
218 Should perhaps also add NULL */
219 { "stdlib.h", "abort\0abs\0atexit\0atof\0atoi\0atol\0bsearch\0calloc\0\
220 exit\0free\0getenv\0labs\0malloc\0putenv\0qsort\0rand\0realloc\0\
221 srand\0strtod\0strtol\0strtoul\0system\0", NONE },
223 { "string.h", "memchr\0memcmp\0memcpy\0memmove\0memset\0\
224 strcat\0strchr\0strcmp\0strcoll\0strcpy\0strcspn\0strerror\0\
225 strlen\0strncat\0strncmp\0strncpy\0strpbrk\0strrchr\0strspn\0strstr\0\
226 strtok\0strxfrm\0", NONE },
227 /* Should perhaps also add NULL and size_t */
229 { "sys/stat.h", "chmod\0fstat\0mkdir\0mkfifo\0stat\0lstat\0umask\0",
230 "S_ISDIR\0S_ISBLK\0S_ISCHR\0S_ISFIFO\0S_ISREG\0S_ISLNK\0S_IFDIR\0\
231 S_IFBLK\0S_IFCHR\0S_IFIFO\0S_IFREG\0S_IFLNK\0" },
233 { "sys/times.h", "times\0", NONE },
234 /* "sys/types.h" add types (not in old g++-include) */
236 { "sys/utsname.h", "uname\0", NONE },
238 { "sys/wait.h", "wait\0waitpid\0",
239 "WEXITSTATUS\0WIFEXITED\0WIFSIGNALED\0WIFSTOPPED\0WSTOPSIG\0\
240 WTERMSIG\0WNOHANG\0WNOTRACED\0" },
242 { "tar.h", NONE, NONE },
244 { "termios.h", "cfgetispeed\0cfgetospeed\0cfsetispeed\0cfsetospeed\0tcdrain\0tcflow\0tcflush\0tcgetattr\0tcsendbreak\0tcsetattr\0", NONE },
246 { "time.h", "asctime\0clock\0ctime\0difftime\0gmtime\0localtime\0mktime\0strftime\0time\0tzset\0", NONE },
248 { "unistd.h", "_exit\0access\0alarm\0chdir\0chown\0close\0ctermid\0cuserid\0\
249 dup\0dup2\0execl\0execle\0execlp\0execv\0execve\0execvp\0fork\0fpathconf\0\
250 getcwd\0getegid\0geteuid\0getgid\0getlogin\0getopt\0getpgrp\0getpid\0\
251 getppid\0getuid\0isatty\0link\0lseek\0pathconf\0pause\0pipe\0read\0rmdir\0\
252 setgid\0setpgid\0setsid\0setuid\0sleep\0sysconf\0tcgetpgrp\0tcsetpgrp\0\
253 ttyname\0unlink\0write\0", NONE },
258 enum special_file special_file_handling = no_special;
260 /* The following are only used when handling sys/stat.h */
261 /* They are set if the corresponding macro has been seen. */
262 int seen_S_IFBLK = 0, seen_S_ISBLK = 0;
263 int seen_S_IFCHR = 0, seen_S_ISCHR = 0;
264 int seen_S_IFDIR = 0, seen_S_ISDIR = 0;
265 int seen_S_IFIFO = 0, seen_S_ISFIFO = 0;
266 int seen_S_IFLNK = 0, seen_S_ISLNK = 0;
267 int seen_S_IFREG = 0, seen_S_ISREG = 0;
269 /* Wrapper around free, to avoid prototype clashes. */
278 /* Avoid error if config defines abort as fancy_abort.
279 It's not worth "really" implementing this because ordinary
280 compiler users never run fix-header. */
288 #define obstack_chunk_alloc xmalloc
289 #define obstack_chunk_free xfree
290 struct obstack scan_file_obstack;
292 /* NOTE: If you edit this, also edit gen-protos.c !! */
294 lookup_std_proto (name, name_length)
298 int i = hashf (name, name_length, HASH_SIZE);
303 if (hash_tab[i] == 0)
305 fn = &std_protos[hash_tab[i]];
306 if (strlen (fn->fname) == name_length
307 && strncmp (fn->fname, name, name_length) == 0)
309 i = (i+1) % HASH_SIZE;
316 int inc_filename_length;
317 char *progname = "fix-header";
321 int lbrac_line, rbrac_line;
323 namelist required_functions_list;
324 int required_unseen_count = 0;
330 #if ADD_MISSING_EXTERN_C
331 if (missing_extern_C_count + required_unseen_count > 0)
332 fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
337 fprintf (outf, "#ifndef _PARAMS\n");
338 fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
339 fprintf (outf, "#define _PARAMS(ARGS) ARGS\n");
340 fprintf (outf, "#else\n");
341 fprintf (outf, "#define _PARAMS(ARGS) ()\n");
342 fprintf (outf, "#endif\n#endif /* _PARAMS */\n");
348 struct partial_proto *next;
349 char *fname; /* name of function */
350 char *rtype; /* return type */
355 struct partial_proto *partial_proto_list = NULL;
357 struct partial_proto required_dummy_proto, seen_dummy_proto;
358 #define REQUIRED(FN) ((FN)->partial == &required_dummy_proto)
359 #define SET_REQUIRED(FN) ((FN)->partial = &required_dummy_proto)
360 #define SET_SEEN(FN) ((FN)->partial = &seen_dummy_proto)
361 #define SEEN(FN) ((FN)->partial == &seen_dummy_proto)
364 recognized_macro (fname)
367 /* The original include file defines fname as a macro. */
368 struct fn_decl *fn = lookup_std_proto (fname, strlen (fname));
370 /* Since fname is a macro, don't require a prototype for it. */
374 required_unseen_count--;
378 switch (special_file_handling)
381 if (strcmp (fname, "errno") == 0) missing_errno = 0;
384 if (fname[0] == 'S' && fname[1] == '_')
386 if (strcmp (fname, "S_IFBLK") == 0) seen_S_IFBLK++;
387 else if (strcmp (fname, "S_ISBLK") == 0) seen_S_ISBLK++;
388 else if (strcmp (fname, "S_IFCHR") == 0) seen_S_IFCHR++;
389 else if (strcmp (fname, "S_ISCHR") == 0) seen_S_ISCHR++;
390 else if (strcmp (fname, "S_IFDIR") == 0) seen_S_IFDIR++;
391 else if (strcmp (fname, "S_ISDIR") == 0) seen_S_ISDIR++;
392 else if (strcmp (fname, "S_IFIFO") == 0) seen_S_IFIFO++;
393 else if (strcmp (fname, "S_ISFIFO") == 0) seen_S_ISFIFO++;
394 else if (strcmp (fname, "S_IFLNK") == 0) seen_S_IFLNK++;
395 else if (strcmp (fname, "S_ISLNK") == 0) seen_S_ISLNK++;
396 else if (strcmp (fname, "S_IFREG") == 0) seen_S_IFREG++;
397 else if (strcmp (fname, "S_ISREG") == 0) seen_S_ISREG++;
403 recognized_extern (name, name_length, type, type_length)
406 int name_length, type_length;
408 switch (special_file_handling)
411 if (strncmp (name, "errno", name_length) == 0) missing_errno = 0;
416 /* Called by scan_decls if it saw a function definition for a function
417 named FNAME, with return type RTYPE, and argument list ARGS,
418 in source file FILE_SEEN on line LINE_SEEN.
419 KIND is 'I' for an inline function;
420 'F' if a normal function declaration preceded by 'extern "C"'
421 (or nested inside 'extern "C"' braces); or
422 'f' for other function declarations. */
425 recognized_function (fname, fname_length,
426 kind, rtype, rtype_length,
427 have_arg_list, file_seen, line_seen)
430 int kind; /* One of 'f' 'F' or 'I' */
437 struct partial_proto *partial;
440 #if ADD_MISSING_EXTERN_C
442 missing_extern_C_count++;
445 fn = lookup_std_proto (fname, fname_length);
447 /* Remove the function from the list of required function. */
451 required_unseen_count--;
455 /* If we have a full prototype, we're done. */
459 if (kind == 'I') /* don't edit inline function */
462 /* If the partial prototype was included from some other file,
463 we don't need to patch it up (in this run). */
464 i = strlen (file_seen);
465 if (i < inc_filename_length
466 || strcmp (inc_filename, file_seen + (i - inc_filename_length)) != 0)
471 if (fn->params[0] == '\0' || strcmp (fn->params, "void") == 0)
474 /* We only have a partial function declaration,
475 so remember that we have to add a complete prototype. */
477 partial = (struct partial_proto*)
478 obstack_alloc (&scan_file_obstack, sizeof (struct partial_proto));
479 partial->fname = obstack_alloc (&scan_file_obstack, fname_length + 1);
480 bcopy (fname, partial->fname, fname_length);
481 partial->fname[fname_length] = 0;
482 partial->rtype = obstack_alloc (&scan_file_obstack, rtype_length + 1);
483 sprintf (partial->rtype, "%.*s", rtype_length, rtype);
484 partial->line_seen = line_seen;
486 fn->partial = partial;
487 partial->next = partial_proto_list;
488 partial_proto_list = partial;
491 fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
492 inc_filename, partial->fname);
496 /* For any name in NAMES that is defined as a macro,
497 call recognized_macro on it. */
500 check_macro_names (pfile, names)
501 struct parse_file *pfile;
506 if (cpp_lookup (pfile, names, -1, -1))
507 recognized_macro (names);
508 names += strlen (names) + 1;
513 read_scan_file (in_fname, argc, argv)
519 cpp_options scan_options;
523 obstack_init (&scan_file_obstack);
525 init_parse_file (&scan_in);
526 scan_in.data = &scan_options;
527 init_parse_options (&scan_options);
528 i = cpp_handle_options (&scan_in, argc, argv);
530 fatal ("Invalid option `%s'", argv[i]);
531 push_parse_file (&scan_in, in_fname);
532 CPP_OPTIONS (&scan_in)->no_line_commands = 1;
535 /* Some targets may assume special definitions (for example
536 OSF header files assume __LANGUAGE_C__). These macros
537 are normally passed to cpplib by gcc - but we here invoke
538 cpplib directly, without going through gcc.
539 Handle these and other target-dependent initializations here. */
540 FIXPROTO_INIT (&scan_in);
543 /* Actually (pre-)process the header file. */
544 scan_decls (&scan_in, argc, argv);
546 check_macro_names (&scan_in, include_entry->required);
547 check_macro_names (&scan_in, include_entry->extra);
549 if (verbose && (scan_in.errors + warnings) > 0)
550 fprintf (stderr, "(%s: %d errors and %d warnings from cpp)\n",
551 inc_filename, scan_in.errors, warnings);
555 /* Traditionally, getc and putc are defined in terms of _filbuf and _flsbuf.
556 If so, those functions are also required. */
557 if (special_file_handling == stdio_h
558 && (fn = lookup_std_proto ("_filbuf", 7)) != NULL)
560 static char getchar_call[] = "getchar();";
562 cpp_push_buffer (&scan_in, getchar_call, sizeof(getchar_call) - 1);
563 int old_written = CPP_WRITTEN (&scan_in);
566 /* Scan the macro expansion of "getchar();". */
569 enum cpp_token token = cpp_get_token (&scan_in);
570 int length = CPP_WRITTEN (&scan_in) - old_written;
571 CPP_SET_WRITTEN (&scan_in, old_written);
572 if (token == CPP_EOF) /* Should not happen ... */
574 if (token == CPP_POP && CPP_BUFFER (&scan_in) == buf)
576 cpp_pop_buffer (&scan_in);
579 if (token == CPP_NAME && length == 7
580 && strcmp ("_filbuf", scan_in.token_buffer + old_written) == 0)
585 int need_filbuf = !SEEN (fn) && !REQUIRED (fn);
586 struct fn_decl *flsbuf_fn = lookup_std_proto ("_flsbuf", 7);
588 = flsbuf_fn && !SEEN (flsbuf_fn) && !REQUIRED (flsbuf_fn);
590 /* Append "_filbuf" and/or "_flsbuf" to end of
591 required_functions_list. */
592 if (need_filbuf + need_flsbuf)
594 int old_len = namelist_end (required_functions_list)
595 - required_functions_list;
596 char *new_list = (char*) xmalloc (old_len + 20);
597 bcopy (required_functions_list, new_list, old_len);
600 strcpy (new_list + old_len, "_filbuf");
606 strcpy (new_list + old_len, "_flsbuf");
608 SET_REQUIRED (flsbuf_fn);
610 new_list[old_len] = '\0';
611 required_functions_list = (namelist)new_list;
612 required_unseen_count += need_filbuf + need_flsbuf;
617 if (required_unseen_count + partial_count + missing_errno
618 #if ADD_MISSING_EXTERN_C
619 + missing_extern_C_count
624 fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
628 fprintf (stderr, "%s: fixing %s\n", progname, inc_filename);
631 if (required_unseen_count)
632 fprintf (stderr, "%s: %d missing function declarations.\n",
633 inc_filename, required_unseen_count);
635 fprintf (stderr, "%s: %d non-prototype function declarations.\n",
636 inc_filename, partial_count);
637 #if ADD_MISSING_EXTERN_C
638 if (missing_extern_C_count)
640 "%s: %d declarations not protected by extern \"C\".\n",
641 inc_filename, missing_extern_C_count);
652 if (required_unseen_count)
655 "#if defined(__cplusplus) || defined(__USE_FIXED_PROTOTYPES__)\n");
656 #ifdef NO_IMPLICIT_EXTERN_C
657 fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
661 /* Now we print out prototypes for those functions that we haven't seen. */
662 for (cptr = required_functions_list; *cptr!= '\0'; )
664 int macro_protect = 0;
665 int name_len = strlen (cptr);
667 fn = lookup_std_proto (cptr, name_len);
669 if (fn == NULL || !REQUIRED (fn))
672 /* In the case of memmove, protect in case the application
673 defines it as a macro before including the header. */
674 if (!strcmp (fn->fname, "memmove")
675 || !strcmp (fn->fname, "vprintf")
676 || !strcmp (fn->fname, "vfprintf")
677 || !strcmp (fn->fname, "vsprintf")
678 || !strcmp (fn->fname, "rewinddir"))
682 fprintf (outf, "#ifndef %s\n", fn->fname);
683 fprintf (outf, "extern %s %s (%s);\n",
684 fn->rtype, fn->fname, fn->params);
686 fprintf (outf, "#endif\n");
688 if (required_unseen_count)
690 #ifdef NO_IMPLICIT_EXTERN_C
691 fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
694 "#endif /* defined(__cplusplus) || defined(__USE_FIXED_PROTOTYPES__*/\n");
697 switch (special_file_handling)
701 fprintf (outf, "extern int errno;\n");
704 if (!seen_S_ISBLK && seen_S_IFBLK)
706 "#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
707 if (!seen_S_ISCHR && seen_S_IFCHR)
709 "#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
710 if (!seen_S_ISDIR && seen_S_IFDIR)
712 "#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
713 if (!seen_S_ISFIFO && seen_S_IFIFO)
715 "#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
716 if (!seen_S_ISLNK && seen_S_IFLNK)
718 "#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
719 if (!seen_S_ISREG && seen_S_IFREG)
721 "#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
726 #if ADD_MISSING_EXTERN_C
727 if (missing_extern_C_count + required_unseen_count > 0)
728 fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
736 char *copy = (char *) xmalloc (strlen (str) + 1);
741 /* Returns 1 iff the file is properly protected from multiple inclusion:
748 #define INF_GET() (inf_ptr < inf_limit ? *(unsigned char*)inf_ptr++ : EOF)
749 #define INF_UNGET(c) ((c)!=EOF && inf_ptr--)
757 if (c == ' ' || c == '\t')
775 source_lineno++, lineno++;
778 else if ((c = INF_GET ()) == '/')
788 /* Read into STR from inf_buffer upto DELIM. */
791 inf_read_upto (str, delim)
799 if (ch == EOF || ch == delim)
801 SSTRING_PUT (str, ch);
803 MAKE_SSTRING_SPACE (str, 1);
809 inf_scan_ident (s, c)
814 if (isalpha (c) || c == '_')
820 if (c == EOF || !(isalnum (c) || c == '_'))
824 MAKE_SSTRING_SPACE (s, 1);
829 /* Returns 1 if the file is correctly protected against multiple
830 inclusion, setting *ifndef_line to the line number of the initial #ifndef
831 and setting *endif_line to the final #endif.
832 Otherwise return 0. */
835 check_protection (ifndef_line, endif_line)
836 int *ifndef_line, *endif_line;
839 int if_nesting = 1; /* Level of nesting of #if's */
840 char *protect_name = NULL; /* Identifier following initial #ifndef */
843 /* Skip initial white space (including comments). */
846 c = inf_skip_spaces (' ');
854 c = inf_scan_ident (&buf, inf_skip_spaces (' '));
855 if (SSTRING_LENGTH (&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
858 /* So far so good: We've seen an initial #ifndef. */
859 *ifndef_line = lineno;
860 c = inf_scan_ident (&buf, inf_skip_spaces (c));
861 if (SSTRING_LENGTH (&buf) == 0 || c == EOF)
863 protect_name = xstrdup (buf.base);
866 c = inf_read_upto (&buf, '\n');
873 c = inf_skip_spaces (' ');
883 c = inf_scan_ident (&buf, inf_skip_spaces (' '));
884 if (SSTRING_LENGTH (&buf) == 0)
886 else if (!strcmp (buf.base, "ifndef")
887 || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
891 else if (!strcmp (buf.base, "endif"))
897 else if (!strcmp (buf.base, "else"))
902 else if (!strcmp (buf.base, "define"))
906 c = inf_skip_spaces (c);
907 c = inf_scan_ident (&buf, c);
908 if (buf.base[0] > 0 && strcmp (buf.base, protect_name) == 0)
914 if (c == '\n' || c == EOF)
925 *endif_line = lineno;
926 /* Skip final white space (including comments). */
929 c = inf_skip_spaces (' ');
948 const char *cptr, **pptr;
954 if (argv[0] && argv[0][0])
959 for (p = argv[0]; *p; p++)
962 progname = progname ? progname+1 : argv[0];
967 fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h options\n",
972 inc_filename = argv[1];
973 inc_filename_length = strlen (inc_filename);
975 #ifdef FIXPROTO_IGNORE_LIST
976 for (i = 0; files_to_ignore[i] != NULL; i++)
978 char *ignore_name = files_to_ignore[i];
979 int ignore_len = strlen (ignore_name);
980 if (strncmp (inc_filename, ignore_name, ignore_len) == 0)
982 if (ignore_name[ignore_len-1] == '/'
983 || inc_filename[ignore_len] == '\0')
986 fprintf (stderr, "%s: ignoring %s\n", progname, inc_filename);
994 if (strcmp (inc_filename, "sys/stat.h") == 0)
995 special_file_handling = sys_stat_h;
996 else if (strcmp (inc_filename, "errno.h") == 0)
997 special_file_handling = errno_h, missing_errno = 1;
998 else if (strcmp (inc_filename, "stdio.h") == 0)
999 special_file_handling = stdio_h;
1000 include_entry = std_include_table;
1001 while (include_entry->name != NULL
1002 && strcmp (inc_filename, include_entry->name) != 0)
1005 required_functions_list = include_entry->required;
1007 /* Count and mark the prototypes required for this include file. */
1008 for (cptr = required_functions_list; *cptr!= '\0'; )
1010 int name_len = strlen (cptr);
1011 struct fn_decl *fn = lookup_std_proto (cptr, name_len);
1012 required_unseen_count++;
1014 fprintf (stderr, "Internal error: No prototype for %s\n", cptr);
1017 cptr += name_len + 1;
1020 read_scan_file (argv[2], argc - 4, argv + 4);
1022 inf_fd = open (argv[2], O_RDONLY, 0666);
1025 fprintf (stderr, "%s: Cannot open '%s' for reading -",
1030 if (fstat (inf_fd, &sbuf) < 0)
1032 fprintf (stderr, "%s: Cannot get size of '%s' -", progname, argv[2]);
1036 inf_size = sbuf.st_size;
1037 inf_buffer = (char*) xmalloc (inf_size + 2);
1038 inf_buffer[inf_size] = '\n';
1039 inf_buffer[inf_size + 1] = '\0';
1040 inf_limit = inf_buffer + inf_size;
1041 inf_ptr = inf_buffer;
1046 long i = read (inf_fd, inf_buffer + inf_size - to_read, to_read);
1049 fprintf (stderr, "%s: Failed to read '%s' -", progname, argv[2]);
1055 inf_size -= to_read;
1063 /* If file doesn't end with '\n', add one. */
1064 if (inf_limit > inf_buffer && inf_limit[-1] != '\n')
1068 outf = fopen (argv[3], "w");
1071 fprintf (stderr, "%s: Cannot open '%s' for writing -",
1079 if (check_protection (&ifndef_line, &endif_line))
1081 lbrac_line = ifndef_line+1;
1082 rbrac_line = endif_line;
1090 /* Reset input file. */
1091 inf_ptr = inf_buffer;
1096 if (lineno == lbrac_line)
1098 if (lineno == rbrac_line)
1106 if (isalpha (c) || c == '_')
1108 c = inf_scan_ident (&buf, c);
1110 fputs (buf.base, outf);
1111 fn = lookup_std_proto (buf.base, strlen (buf.base));
1112 /* We only want to edit the declaration matching the one
1113 seen by scan-decls, as there can be multiple
1114 declarations, selected by #ifdef __STDC__ or whatever. */
1115 if (fn && fn->partial && fn->partial->line_seen == lineno)
1117 c = inf_skip_spaces (' ');
1122 c = inf_skip_spaces (' ');
1125 fprintf (outf, " _PARAMS((%s))", fn->params);
1134 fprintf (outf, " %c", c);
1156 /* Stub error functions. These replace cpperror.c,
1157 because we want to suppress error messages. */
1160 cpp_file_line_for_message (pfile, filename, line, column)
1168 fprintf (stderr, "%s:%d:%d: ", filename, line, column);
1170 fprintf (stderr, "%s:%d: ", filename, line);
1174 cpp_print_containing_files (pfile)
1179 /* IS_ERROR is 1 for error, 0 for warning */
1180 void cpp_message (pfile, is_error, msg, arg1, arg2, arg3)
1184 char *arg1, *arg2, *arg3;
1191 fprintf (stderr, "warning: ");
1192 fprintf (stderr, msg, arg1, arg2, arg3);
1193 fprintf (stderr, "\n");
1200 fprintf (stderr, "%s: %s: ", progname, inc_filename);
1201 fprintf (stderr, str, arg);
1202 fprintf (stderr, "\n");
1203 exit (FATAL_EXIT_CODE);
1207 cpp_pfatal_with_name (pfile, name)
1211 cpp_perror_with_name (pfile, name);
1212 exit (FATAL_EXIT_CODE);