* fix-header.c (read_scan_file): Use CPP_FATAL_ERRORS.
[platform/upstream/gcc.git] / gcc / fix-header.c
1 /* fix-header.c - Make C header file suitable for C++.
2    Copyright (C) 1993, 1994, 1995, 1996 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
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++:
20
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.
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 required 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         fix-header FOO.H INFILE.H OUTFILE.H [OPTIONS]
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    * OPTIONS are such as you would pass to cpp.
70
71    Written by Per Bothner <bothner@cygnus.com>, July 1993. */
72
73 #include <stdio.h>
74 #include <ctype.h>
75 #include "hconfig.h"
76 #include "obstack.h"
77 #include "scan.h"
78 #include "cpplib.h"
79 #include "gansidecl.h"
80
81 #ifndef O_RDONLY
82 #define O_RDONLY 0
83 #endif
84
85 extern void cpp_fatal ();
86
87 #if !__STDC__ && !defined(const)
88 #define const /* nothing */
89 #endif
90
91 sstring buf;
92
93 int verbose = 0;
94 int partial_count = 0;
95 int warnings = 0;
96
97 /* We no longer need to add extern "C", because cpp implicitly
98    forces the standard include files to be treated as C.  */
99 /*#define ADD_MISSING_EXTERN_C 1 */
100
101 #if ADD_MISSING_EXTERN_C
102 int missing_extern_C_count = 0;
103 #endif
104
105 #include "xsys-protos.h"
106
107 #ifdef FIXPROTO_IGNORE_LIST
108 /* This is a currently unused feature. */
109
110 /* List of files and directories to ignore.
111    A directory name (ending in '/') means ignore anything in that
112    directory.  (It might be more efficient to do directory pruning
113    earlier in fixproto, but this is simpler and easier to customize.) */
114
115 static char *files_to_ignore[] = {
116   "X11/",
117   FIXPROTO_IGNORE_LIST
118   0
119 };
120 #endif
121
122 char *inf_buffer;
123 char *inf_limit;
124 char *inf_ptr;
125
126 /* Certain standard files get extra treatment */
127
128 enum special_file
129 {
130   no_special,
131   errno_h,
132   stdio_h,
133   stdlib_h,
134   sys_stat_h
135 };
136
137 /* A NAMELIST is a sequence of names, separated by '\0', and terminated
138    by an empty name (i.e. by "\0\0"). */
139
140 typedef const char* namelist;
141
142 /* The following macros provide the bits for symbol_flags. */
143 typedef int symbol_flags;
144
145 /* Used to mark names defined in the ANSI/ISO C standard. */
146 #define ANSI_SYMBOL 1
147
148 /* Used to mark names defined in the Posix.1 or Posix.2 standard. */
149 #define POSIX1_SYMBOL 2
150 #define POSIX2_SYMBOL 4
151
152 /* Used to mark names defined in X/Open Portability Guide. */
153 #define XOPEN_SYMBOL 8
154 /* Used to mark names defined in X/Open UNIX Extensions. */
155 #define XOPEN_EXTENDED_SYMBOL 16
156
157 /* Used to indicate names that are not functions */
158 #define MACRO_SYMBOL 512
159
160 struct symbol_list {
161   symbol_flags flags;
162   namelist names;
163 };
164
165 #define SYMBOL_TABLE_SIZE 10
166 struct symbol_list symbol_table[SYMBOL_TABLE_SIZE];
167 int cur_symbol_table_size;
168
169 void
170 add_symbols (flags, names)
171      symbol_flags flags;
172      namelist names;
173 {
174   symbol_table[cur_symbol_table_size].flags = flags;
175   symbol_table[cur_symbol_table_size].names = names;
176   cur_symbol_table_size++;
177   if (cur_symbol_table_size >= SYMBOL_TABLE_SIZE)
178     fatal ("too many calls to add_symbols");
179   symbol_table[cur_symbol_table_size].names = NULL; /* Termination. */
180 }
181
182 struct std_include_entry {
183   const char *name;
184   symbol_flags flags;
185   namelist names;
186 };
187
188 const char NONE[] = "";  /* The empty namelist. */
189
190 /* Special name to indicate a continuation line in std_include_table. */
191 const char CONTINUED[] = "";
192
193 struct std_include_entry *include_entry;
194
195 struct std_include_entry std_include_table [] = {
196   { "ctype.h", ANSI_SYMBOL,
197       "isalnum\0isalpha\0iscntrl\0isdigit\0isgraph\0islower\0\
198 isprint\0ispunct\0isspace\0isupper\0isxdigit\0tolower\0toupper\0" },
199
200   { "dirent.h", POSIX1_SYMBOL, "closedir\0opendir\0readdir\0rewinddir\0"},
201
202   { "errno.h", ANSI_SYMBOL|MACRO_SYMBOL, "errno\0" },
203
204   /* ANSI_SYMBOL is wrong, but ... */
205   { "curses.h", ANSI_SYMBOL, "box\0delwin\0endwin\0getcurx\0getcury\0initscr\0\
206 mvcur\0mvwprintw\0mvwscanw\0newwin\0overlay\0overwrite\0\
207 scroll\0subwin\0touchwin\0waddstr\0wclear\0wclrtobot\0wclrtoeol\0\
208 waddch\0wdelch\0wdeleteln\0werase\0wgetch\0wgetstr\0winsch\0winsertln\0\
209 wmove\0wprintw\0wrefresh\0wscanw\0wstandend\0wstandout\0" },
210
211   { "fcntl.h", POSIX1_SYMBOL, "creat\0fcntl\0open\0" },
212
213   /* Maybe also "getgrent fgetgrent setgrent endgrent" */
214   { "grp.h", POSIX1_SYMBOL, "getgrgid\0getgrnam\0" },
215
216 /*{ "limit.h", ... provided by gcc }, */
217
218   { "locale.h", ANSI_SYMBOL, "localeconv\0setlocale\0" },
219
220   { "math.h", ANSI_SYMBOL,
221       "acos\0asin\0atan\0atan2\0ceil\0cos\0cosh\0exp\0\
222 fabs\0floor\0fmod\0frexp\0ldexp\0log10\0log\0modf\0pow\0sin\0sinh\0sqrt\0\
223 tan\0tanh\0" },
224
225   { CONTINUED, ANSI_SYMBOL|MACRO_SYMBOL, "HUGE_VAL\0" },
226
227   { "pwd.h", POSIX1_SYMBOL, "getpwnam\0getpwuid\0" },
228
229   /* Left out siglongjmp sigsetjmp - these depend on sigjmp_buf. */
230   { "setjmp.h", ANSI_SYMBOL, "longjmp\0setjmp\0" },
231
232   /* Left out signal() - its prototype is too complex for us!
233      Also left out "sigaction sigaddset sigdelset sigemptyset
234      sigfillset sigismember sigpending sigprocmask sigsuspend"
235      because these need sigset_t or struct sigaction.
236      Most systems that provide them will also declare them. */
237   { "signal.h", ANSI_SYMBOL, "kill\0raise\0" },
238
239   { "stdio.h", ANSI_SYMBOL,
240       "clearerr\0fclose\0feof\0ferror\0fflush\0fgetc\0fgetpos\0\
241 fgets\0fopen\0fprintf\0fputc\0fputs\0fread\0freopen\0fscanf\0fseek\0\
242 fsetpos\0ftell\0fwrite\0getc\0getchar\0gets\00perror\0popen\0\
243 printf\0putc\0putchar\0puts\0remove\0rename\0rewind\0scanf\0setbuf\0\
244 setvbuf\0sprintf\0sscanf\0vprintf\0vsprintf\0vfprintf\0tmpfile\0\
245 tmpnam\0ungetc\0" },
246   { CONTINUED, POSIX1_SYMBOL, "fdopen\0fileno\0" },
247   { CONTINUED, POSIX2_SYMBOL, "pclose\0popen\0" },  /* I think ... */
248 /* Should perhaps also handle NULL, EOF, ... ? */
249
250   /* "div ldiv", - ignored because these depend on div_t, ldiv_t
251      ignore these: "mblen mbstowcs mbstowc wcstombs wctomb"
252      Left out getgroups, because SunOS4 has incompatible BSD and SVR4 versions.
253      Should perhaps also add NULL */
254   { "stdlib.h", ANSI_SYMBOL,
255       "abort\0abs\0atexit\0atof\0atoi\0atol\0bsearch\0calloc\0\
256 exit\0free\0getenv\0labs\0malloc\0putenv\0qsort\0rand\0realloc\0\
257 srand\0strtod\0strtol\0strtoul\0system\0" },
258   { CONTINUED, ANSI_SYMBOL|MACRO_SYMBOL, "EXIT_FAILURE\0EXIT_SUCCESS\0" },
259
260   { "string.h", ANSI_SYMBOL, "memchr\0memcmp\0memcpy\0memmove\0memset\0\
261 strcat\0strchr\0strcmp\0strcoll\0strcpy\0strcspn\0strerror\0\
262 strlen\0strncat\0strncmp\0strncpy\0strpbrk\0strrchr\0strspn\0strstr\0\
263 strtok\0strxfrm\0" },
264 /* Should perhaps also add NULL and size_t */
265
266   { "strings.h", XOPEN_EXTENDED_SYMBOL,
267       "bcmp\0bcopy\0bzero\0ffs\0index\0rindex\0strcasecmp\0strncasecmp\0" },
268
269   { "strops.h", XOPEN_EXTENDED_SYMBOL, "ioctl\0" },
270
271   /* Actually, XPG4 does not seem to have <sys/ioctl.h>, but defines
272      ioctl in <strops.h>.  However, many systems have it is sys/ioctl.h,
273      and many systems do have <sys/ioctl.h> but not <strops.h>. */
274   { "sys/ioctl.h", XOPEN_EXTENDED_SYMBOL, "ioctl\0" },
275
276   { "sys/socket.h", XOPEN_EXTENDED_SYMBOL, "socket\0" },
277
278   { "sys/stat.h", POSIX1_SYMBOL,
279       "chmod\0fstat\0mkdir\0mkfifo\0stat\0lstat\0umask\0" },
280   { CONTINUED, POSIX1_SYMBOL|MACRO_SYMBOL,
281       "S_ISDIR\0S_ISBLK\0S_ISCHR\0S_ISFIFO\0S_ISREG\0S_ISLNK\0S_IFDIR\0\
282 S_IFBLK\0S_IFCHR\0S_IFIFO\0S_IFREG\0S_IFLNK\0" },
283   { CONTINUED, XOPEN_EXTENDED_SYMBOL, "fchmod\0" },
284
285 #if 0
286 /* How do we handle fd_set? */
287   { "sys/time.h", XOPEN_EXTENDED_SYMBOL, "select\0" },
288   { "sys/select.h", XOPEN_EXTENDED_SYMBOL /* fake */, "select\0" },
289 #endif
290
291   { "sys/times.h", POSIX1_SYMBOL, "times\0" },
292   /* "sys/types.h" add types (not in old g++-include) */
293
294   { "sys/utsname.h", POSIX1_SYMBOL, "uname\0" },
295
296   { "sys/wait.h", POSIX1_SYMBOL, "wait\0waitpid\0" },
297   { CONTINUED, POSIX1_SYMBOL|MACRO_SYMBOL,
298       "WEXITSTATUS\0WIFEXITED\0WIFSIGNALED\0WIFSTOPPED\0WSTOPSIG\0\
299 WTERMSIG\0WNOHANG\0WNOTRACED\0" },
300
301   { "tar.h", POSIX1_SYMBOL, NONE },
302
303   { "termios.h", POSIX1_SYMBOL,
304       "cfgetispeed\0cfgetospeed\0cfsetispeed\0cfsetospeed\0tcdrain\0tcflow\0tcflush\0tcgetattr\0tcsendbreak\0tcsetattr\0" },
305
306   { "time.h", ANSI_SYMBOL,
307       "asctime\0clock\0ctime\0difftime\0gmtime\0localtime\0mktime\0strftime\0time\0tzset\0" },
308
309   { "unistd.h", POSIX1_SYMBOL,
310       "_exit\0access\0alarm\0chdir\0chown\0close\0ctermid\0cuserid\0\
311 dup\0dup2\0execl\0execle\0execlp\0execv\0execve\0execvp\0fork\0fpathconf\0\
312 getcwd\0getegid\0geteuid\0getgid\0getlogin\0getpgrp\0getpid\0\
313 getppid\0getuid\0isatty\0link\0lseek\0pathconf\0pause\0pipe\0read\0rmdir\0\
314 setgid\0setpgid\0setsid\0setuid\0sleep\0sysconf\0tcgetpgrp\0tcsetpgrp\0\
315 ttyname\0unlink\0write\0" },
316   { CONTINUED, POSIX2_SYMBOL, "getopt\0" },
317   { CONTINUED, XOPEN_EXTENDED_SYMBOL,
318       "lockf\0gethostid\0gethostname\0readlink\0" },
319
320   { "utime.h", POSIX1_SYMBOL, "utime\0" },
321
322   { NULL, 0, NONE }
323 };
324
325 enum special_file special_file_handling = no_special;
326
327 /* They are set if the corresponding macro has been seen. */
328 /* The following are only used when handling sys/stat.h */
329 int seen_S_IFBLK = 0, seen_S_ISBLK  = 0;
330 int seen_S_IFCHR = 0, seen_S_ISCHR  = 0;
331 int seen_S_IFDIR = 0, seen_S_ISDIR  = 0;
332 int seen_S_IFIFO = 0, seen_S_ISFIFO = 0;
333 int seen_S_IFLNK = 0, seen_S_ISLNK  = 0;
334 int seen_S_IFREG = 0, seen_S_ISREG  = 0;
335 /* The following are only used when handling errno.h */
336 int seen_errno = 0;
337 /* The following are only used when handling stdlib.h */
338 int seen_EXIT_FAILURE = 0, seen_EXIT_SUCCESS = 0;
339 \f
340 /* Wrapper around free, to avoid prototype clashes. */
341
342 void
343 xfree (ptr)
344      char *ptr;
345 {
346   free (ptr);
347 }
348
349 /* Avoid error if config defines abort as fancy_abort.
350    It's not worth "really" implementing this because ordinary
351    compiler users never run fix-header.  */
352
353 void
354 fancy_abort ()
355 {
356   abort ();
357 }
358 \f
359 #define obstack_chunk_alloc xmalloc
360 #define obstack_chunk_free xfree
361 struct obstack scan_file_obstack;
362
363 /* NOTE:  If you edit this, also edit gen-protos.c !! */
364 struct fn_decl *
365 lookup_std_proto (name, name_length)
366      const char *name;
367      int name_length;
368 {
369   int i = hashf (name, name_length, HASH_SIZE);
370   int i0 = i;
371   for (;;)
372     {
373       struct fn_decl *fn;
374       if (hash_tab[i] == 0)
375         return NULL;
376       fn = &std_protos[hash_tab[i]];
377       if (strlen (fn->fname) == name_length
378           && strncmp (fn->fname, name, name_length) == 0)
379         return fn;
380       i = (i+1) % HASH_SIZE;
381       if (i == i0)
382         abort ();
383     }
384 }
385
386 char *inc_filename;
387 int inc_filename_length;
388 char *progname = "fix-header";
389 FILE *outf;
390 sstring line;
391
392 int lbrac_line, rbrac_line;
393
394 int required_unseen_count = 0;
395 int required_other = 0;
396
397 void 
398 write_lbrac ()
399 {
400   
401 #if ADD_MISSING_EXTERN_C
402   if (missing_extern_C_count + required_unseen_count > 0)
403     fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
404 #endif
405
406   if (partial_count)
407     {
408       fprintf (outf, "#ifndef _PARAMS\n");
409       fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
410       fprintf (outf, "#define _PARAMS(ARGS) ARGS\n");
411       fprintf (outf, "#else\n");
412       fprintf (outf, "#define _PARAMS(ARGS) ()\n");
413       fprintf (outf, "#endif\n#endif /* _PARAMS */\n");
414     }
415 }
416
417 struct partial_proto
418 {
419   struct partial_proto *next;
420   char *fname;  /* name of function */
421   char *rtype;  /* return type */
422   struct fn_decl *fn;
423   int line_seen;
424 };
425
426 struct partial_proto *partial_proto_list = NULL;
427
428 struct partial_proto required_dummy_proto, seen_dummy_proto;
429 #define REQUIRED(FN) ((FN)->partial == &required_dummy_proto)
430 #define SET_REQUIRED(FN) ((FN)->partial = &required_dummy_proto)
431 #define SET_SEEN(FN) ((FN)->partial = &seen_dummy_proto)
432 #define SEEN(FN) ((FN)->partial == &seen_dummy_proto)
433
434 void
435 recognized_macro (fname)
436      char *fname;
437 {
438   /* The original include file defines fname as a macro. */
439   struct fn_decl *fn = lookup_std_proto (fname, strlen (fname));
440
441   /* Since fname is a macro, don't require a prototype for it. */
442   if (fn)
443     {
444       if (REQUIRED (fn))
445         required_unseen_count--;
446       SET_SEEN (fn);
447     }
448
449   switch (special_file_handling)
450     {
451     case errno_h:
452       if (strcmp (fname, "errno") == 0 && !seen_errno)
453         seen_errno = 1, required_other--;
454       break;
455     case stdlib_h:
456       if (strcmp (fname, "EXIT_FAILURE") == 0 && !seen_EXIT_FAILURE)
457         seen_EXIT_FAILURE = 1, required_other--;
458       if (strcmp (fname, "EXIT_SUCCESS") == 0 && !seen_EXIT_SUCCESS)
459         seen_EXIT_SUCCESS = 1, required_other--;
460       break;
461     case sys_stat_h:
462       if (fname[0] == 'S' && fname[1] == '_')
463         {
464           if (strcmp (fname, "S_IFBLK") == 0) seen_S_IFBLK++;
465           else if (strcmp (fname, "S_ISBLK") == 0) seen_S_ISBLK++;
466           else if (strcmp (fname, "S_IFCHR") == 0) seen_S_IFCHR++;
467           else if (strcmp (fname, "S_ISCHR") == 0) seen_S_ISCHR++;
468           else if (strcmp (fname, "S_IFDIR") == 0) seen_S_IFDIR++;
469           else if (strcmp (fname, "S_ISDIR") == 0) seen_S_ISDIR++;
470           else if (strcmp (fname, "S_IFIFO") == 0) seen_S_IFIFO++;
471           else if (strcmp (fname, "S_ISFIFO") == 0) seen_S_ISFIFO++;
472           else if (strcmp (fname, "S_IFLNK") == 0) seen_S_IFLNK++;
473           else if (strcmp (fname, "S_ISLNK") == 0) seen_S_ISLNK++;
474           else if (strcmp (fname, "S_IFREG") == 0) seen_S_IFREG++;
475           else if (strcmp (fname, "S_ISREG") == 0) seen_S_ISREG++;
476         }
477     }
478 }
479
480 void
481 recognized_extern (name, name_length, type, type_length)
482      char *name;
483      char *type;
484      int name_length, type_length;
485 {
486   switch (special_file_handling)
487     {
488     case errno_h:
489       if (strcmp (name, "errno") == 0 && !seen_errno)
490         seen_errno = 1, required_other--;
491       break;
492     }
493 }
494
495 /* Called by scan_decls if it saw a function definition for a function
496    named FNAME, with return type RTYPE, and argument list ARGS,
497    in source file FILE_SEEN on line LINE_SEEN.
498    KIND is 'I' for an inline function;
499    'F' if a normal function declaration preceded by 'extern "C"'
500    (or nested inside 'extern "C"' braces); or
501    'f' for other function declarations. */
502
503 void
504 recognized_function (fname, fname_length,
505                      kind, rtype, rtype_length,
506                      have_arg_list, file_seen, line_seen)
507      char *fname;
508      int fname_length;
509      int kind; /* One of 'f' 'F' or 'I' */
510      char *rtype;
511      int rtype_length;
512      int have_arg_list;
513      char *file_seen;
514      int line_seen;
515 {
516   struct partial_proto *partial;
517   int i;
518   struct fn_decl *fn;
519 #if ADD_MISSING_EXTERN_C
520   if (kind == 'f')
521     missing_extern_C_count++;
522 #endif
523
524   fn = lookup_std_proto (fname, fname_length);
525
526   /* Remove the function from the list of required function. */
527   if (fn)
528     {
529       if (REQUIRED (fn))
530         required_unseen_count--;
531       SET_SEEN (fn);
532     }
533
534   /* If we have a full prototype, we're done. */
535   if (have_arg_list)
536     return;
537       
538   if (kind == 'I')  /* don't edit inline function */
539     return;
540
541   /* If the partial prototype was included from some other file,
542      we don't need to patch it up (in this run). */
543   i = strlen (file_seen);
544   if (i < inc_filename_length
545       || strcmp (inc_filename, file_seen + (i - inc_filename_length)) != 0)
546     return;
547
548   if (fn == NULL)
549     return;
550   if (fn->params[0] == '\0' || strcmp (fn->params, "void") == 0)
551     return;
552
553   /* We only have a partial function declaration,
554      so remember that we have to add a complete prototype. */
555   partial_count++;
556   partial = (struct partial_proto*)
557     obstack_alloc (&scan_file_obstack, sizeof (struct partial_proto));
558   partial->fname = obstack_alloc (&scan_file_obstack, fname_length + 1);
559   bcopy (fname, partial->fname, fname_length);
560   partial->fname[fname_length] = 0;
561   partial->rtype = obstack_alloc (&scan_file_obstack, rtype_length + 1);
562   sprintf (partial->rtype, "%.*s", rtype_length, rtype);
563   partial->line_seen = line_seen;
564   partial->fn = fn;
565   fn->partial = partial;
566   partial->next = partial_proto_list;
567   partial_proto_list = partial;
568   if (verbose)
569     {
570       fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
571                inc_filename, partial->fname);
572     }
573 }
574
575 /* For any name in NAMES that is defined as a macro,
576    call recognized_macro on it. */
577
578 void
579 check_macro_names (pfile, names)
580      cpp_reader *pfile;
581      namelist names;
582 {
583   while (*names)
584     {
585       if (cpp_lookup (pfile, names, -1, -1))
586         recognized_macro (names);
587       names += strlen (names) + 1;
588     }
589 }
590
591 void
592 read_scan_file (in_fname, argc, argv)
593      char *in_fname;
594      int argc;
595      char **argv;
596 {
597   cpp_reader scan_in;
598   cpp_options scan_options;
599   struct fn_decl *fn;
600   int i;
601   register struct symbol_list *cur_symbols;
602
603   obstack_init (&scan_file_obstack); 
604
605   cpp_reader_init (&scan_in);
606   scan_in.data = &scan_options;
607   cpp_options_init (&scan_options);
608   i = cpp_handle_options (&scan_in, argc, argv);
609   if (i < argc && ! CPP_FATAL_ERRORS (&scan_in))
610     cpp_fatal (&scan_in, "Invalid option `%s'", argv[i]);
611   if (CPP_FATAL_ERRORS (&scan_in))
612     exit (FATAL_EXIT_CODE);
613
614   if (! cpp_start_read (&scan_in, in_fname))
615     exit (FATAL_EXIT_CODE);
616   CPP_OPTIONS (&scan_in)->no_line_commands = 1;
617
618   scan_decls (&scan_in, argc, argv);
619   for (cur_symbols = &symbol_table[0]; cur_symbols->names; cur_symbols++)
620     check_macro_names (&scan_in, cur_symbols->names);
621
622   if (verbose && (scan_in.errors + warnings) > 0)
623     fprintf (stderr, "(%s: %d errors and %d warnings from cpp)\n",
624              inc_filename, scan_in.errors, warnings);
625   if (scan_in.errors)
626     exit (0);
627
628   /* Traditionally, getc and putc are defined in terms of _filbuf and _flsbuf.
629      If so, those functions are also required. */
630   if (special_file_handling == stdio_h
631       && (fn = lookup_std_proto ("_filbuf", 7)) != NULL)
632     {
633       static char getchar_call[] = "getchar();";
634       cpp_buffer *buf =
635         cpp_push_buffer (&scan_in, getchar_call, sizeof(getchar_call) - 1);
636       int old_written = CPP_WRITTEN (&scan_in);
637       int seen_filbuf = 0;
638
639       /* Scan the macro expansion of "getchar();". */
640       for (;;)
641         {
642           enum cpp_token token = cpp_get_token (&scan_in);
643           int length = CPP_WRITTEN (&scan_in) - old_written;
644           CPP_SET_WRITTEN (&scan_in, old_written);
645           if (token == CPP_EOF) /* Should not happen ... */
646             break;
647           if (token == CPP_POP && CPP_BUFFER (&scan_in) == buf)
648             {
649               cpp_pop_buffer (&scan_in);
650               break;
651             }
652           if (token == CPP_NAME && length == 7
653               && strcmp ("_filbuf", scan_in.token_buffer + old_written) == 0)
654             seen_filbuf++;
655         }
656       if (seen_filbuf)
657         {
658           int need_filbuf = !SEEN (fn) && !REQUIRED (fn);
659           struct fn_decl *flsbuf_fn = lookup_std_proto ("_flsbuf", 7);
660           int need_flsbuf
661             = flsbuf_fn && !SEEN (flsbuf_fn) && !REQUIRED (flsbuf_fn);
662
663           /* Append "_filbuf" and/or "_flsbuf" to the required functions. */
664           if (need_filbuf + need_flsbuf)
665             {
666               char *new_list;
667               if (need_filbuf)
668                 SET_REQUIRED (fn);
669               if (need_flsbuf)
670                 SET_REQUIRED (flsbuf_fn);
671               if (need_flsbuf + need_filbuf == 2)
672                 new_list = "_filbuf\0_flsbuf\0";
673               else if (need_flsbuf)
674                 new_list = "_flsbuf\0";
675               else /* if (need_flsbuf) */
676                 new_list = "_filbuf\0";
677               add_symbols (ANSI_SYMBOL, new_list);
678               required_unseen_count += need_filbuf + need_flsbuf;
679             }
680         }
681     }
682
683   if (required_unseen_count + partial_count + required_other
684 #if ADD_MISSING_EXTERN_C
685       + missing_extern_C_count
686 #endif      
687       == 0)
688     {
689       if (verbose)
690         fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
691       exit (0);
692     }
693   if (!verbose)
694     fprintf (stderr, "%s: fixing %s\n", progname, inc_filename);
695   else
696     {
697       if (required_unseen_count)
698         fprintf (stderr, "%s: %d missing function declarations.\n",
699                  inc_filename, required_unseen_count);
700       if (partial_count)
701         fprintf (stderr, "%s: %d non-prototype function declarations.\n",
702                  inc_filename, partial_count);
703 #if ADD_MISSING_EXTERN_C
704       if (missing_extern_C_count)
705         fprintf (stderr,
706                  "%s: %d declarations not protected by extern \"C\".\n",
707                  inc_filename, missing_extern_C_count);
708 #endif
709     }
710 }
711
712 void
713 write_rbrac ()
714 {
715   struct fn_decl *fn;
716   const char *cptr;
717   register struct symbol_list *cur_symbols;
718
719   if (required_unseen_count)
720     {
721 #ifdef NO_IMPLICIT_EXTERN_C
722       fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
723 #endif
724     }
725
726   /* Now we print out prototypes for those functions that we haven't seen. */
727   for (cur_symbols = &symbol_table[0]; cur_symbols->names; cur_symbols++)
728     {
729       int if_was_emitted = 0;
730       int name_len;
731       cptr = cur_symbols->names;
732       for ( ; (name_len = strlen (cptr)) != 0; cptr+= name_len + 1)
733         {
734           int macro_protect = 0;
735
736           if (cur_symbols->flags & MACRO_SYMBOL)
737             continue;
738
739           fn = lookup_std_proto (cptr, name_len);
740           if (fn == NULL || !REQUIRED (fn))
741             continue;
742
743           if (!if_was_emitted)
744             {
745 /*            what about curses. ??? or _flsbuf/_filbuf ??? */
746               if (cur_symbols->flags & ANSI_SYMBOL)
747                 fprintf (outf,
748          "#if defined(__USE_FIXED_PROTOTYPES__) || defined(__cplusplus) || defined (__STRICT_ANSI__)\n");
749               else if (cur_symbols->flags & (POSIX1_SYMBOL|POSIX2_SYMBOL))
750                 fprintf (outf,
751        "#if defined(__USE_FIXED_PROTOTYPES__) || (defined(__cplusplus) \\\n\
752     ? (!defined(__STRICT_ANSI__) || defined(_POSIX_SOURCE)) \\\n\
753     : (defined(__STRICT_ANSI__) && defined(_POSIX_SOURCE)))\n");
754               else if (cur_symbols->flags & XOPEN_SYMBOL)
755                 {
756                 fprintf (outf,
757        "#if defined(__USE_FIXED_PROTOTYPES__) \\\n\
758    || (defined(__STRICT_ANSI__) && defined(_XOPEN_SOURCE))\n");
759                 }
760               else if (cur_symbols->flags & XOPEN_EXTENDED_SYMBOL)
761                 {
762                 fprintf (outf,
763        "#if defined(__USE_FIXED_PROTOTYPES__) \\\n\
764    || (defined(__STRICT_ANSI__) && defined(_XOPEN_EXTENDED_SOURCE))\n");
765                 }
766               else
767                 {
768                   fatal ("internal error for function %s", fn->fname);
769                 }
770               if_was_emitted = 1;
771             }
772
773           /* In the case of memmove, protect in case the application
774              defines it as a macro before including the header.  */
775           if (!strcmp (fn->fname, "memmove")
776               || !strcmp (fn->fname, "vprintf")
777               || !strcmp (fn->fname, "vfprintf")
778               || !strcmp (fn->fname, "vsprintf")
779               || !strcmp (fn->fname, "rewinddir"))
780             macro_protect = 1;
781
782           if (macro_protect)
783             fprintf (outf, "#ifndef %s\n", fn->fname);
784           fprintf (outf, "extern %s %s (%s);\n",
785                    fn->rtype, fn->fname, fn->params);
786           if (macro_protect)
787             fprintf (outf, "#endif\n");
788         }
789       if (if_was_emitted)
790         fprintf (outf,
791                  "#endif /* defined(__USE_FIXED_PROTOTYPES__) || ... */\n");
792     }
793   if (required_unseen_count)
794     {
795 #ifdef NO_IMPLICIT_EXTERN_C
796       fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
797 #endif
798     }
799
800   switch (special_file_handling)
801     {
802     case errno_h:
803       if (!seen_errno)
804         fprintf (outf, "extern int errno;\n");
805       break;
806     case stdlib_h:
807       if (!seen_EXIT_FAILURE)
808         fprintf (outf, "#define EXIT_FAILURE 1\n");
809       if (!seen_EXIT_SUCCESS)
810         fprintf (outf, "#define EXIT_SUCCESS 0\n");
811       break;
812     case sys_stat_h:
813       if (!seen_S_ISBLK && seen_S_IFBLK)
814         fprintf (outf,
815                  "#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
816       if (!seen_S_ISCHR && seen_S_IFCHR)
817         fprintf (outf,
818                  "#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
819       if (!seen_S_ISDIR && seen_S_IFDIR)
820         fprintf (outf,
821                  "#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
822       if (!seen_S_ISFIFO && seen_S_IFIFO)
823         fprintf (outf,
824                  "#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
825       if (!seen_S_ISLNK && seen_S_IFLNK)
826         fprintf (outf,
827                  "#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
828       if (!seen_S_ISREG && seen_S_IFREG)
829         fprintf (outf,
830                  "#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
831       break;
832     }
833
834
835 #if ADD_MISSING_EXTERN_C
836   if (missing_extern_C_count + required_unseen_count > 0)
837     fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
838 #endif
839 }
840
841 char *
842 xstrdup (str)
843      char *str;
844 {
845   char *copy = (char *) xmalloc (strlen (str) + 1);
846   strcpy (copy, str);
847   return copy;
848 }
849
850 /* Returns 1 iff the file is properly protected from multiple inclusion:
851    #ifndef PROTECT_NAME
852    #define PROTECT_NAME
853    #endif
854
855  */
856
857 #define INF_GET() (inf_ptr < inf_limit ? *(unsigned char*)inf_ptr++ : EOF)
858 #define INF_UNGET(c) ((c)!=EOF && inf_ptr--)
859
860 int
861 inf_skip_spaces (c)
862      int c;
863 {
864   for (;;)
865     {
866       if (c == ' ' || c == '\t')
867         c = INF_GET ();
868       else if (c == '/')
869         {
870           c = INF_GET ();
871           if (c != '*')
872             {
873               INF_UNGET (c);
874               return '/';
875             }
876           c = INF_GET ();
877           for (;;)
878             {
879               if (c == EOF)
880                 return EOF;
881               else if (c != '*')
882                 {
883                   if (c == '\n')
884                     source_lineno++, lineno++;
885                   c = INF_GET ();
886                 }
887               else if ((c = INF_GET ()) == '/')
888                 return INF_GET ();
889             }
890         }
891       else
892         break;
893     }
894   return c;
895 }
896
897 /* Read into STR from inf_buffer upto DELIM. */
898
899 int
900 inf_read_upto (str, delim)
901      sstring *str;
902      int delim;
903 {
904   int ch;
905   for (;;)
906     {
907       ch = INF_GET ();
908       if (ch == EOF || ch == delim)
909         break;
910       SSTRING_PUT (str, ch);
911     }
912   MAKE_SSTRING_SPACE (str, 1);
913   *str->ptr = 0;
914   return ch;
915 }
916
917 int
918 inf_scan_ident (s, c)
919      register sstring *s;
920      int c;
921 {
922   s->ptr = s->base;
923   if (isalpha (c) || c == '_')
924     {
925       for (;;)
926         {
927           SSTRING_PUT (s, c);
928           c = INF_GET ();
929           if (c == EOF || !(isalnum (c) || c == '_'))
930             break;
931         }
932     }
933   MAKE_SSTRING_SPACE (s, 1);
934   *s->ptr = 0;
935   return c;
936 }
937
938 /* Returns 1 if the file is correctly protected against multiple
939    inclusion, setting *ifndef_line to the line number of the initial #ifndef
940    and setting *endif_line to the final #endif.
941    Otherwise return 0. */
942
943 int
944 check_protection (ifndef_line, endif_line)
945      int *ifndef_line, *endif_line;
946 {
947   int c;
948   int if_nesting = 1; /* Level of nesting of #if's */
949   char *protect_name = NULL; /* Identifier following initial #ifndef */
950   int define_seen = 0;
951
952   /* Skip initial white space (including comments). */
953   for (;; lineno++)
954     {
955       c = inf_skip_spaces (' ');
956       if (c == EOF)
957         return 0;
958       if (c != '\n')
959         break;
960     }
961   if (c != '#')
962     return 0;
963   c = inf_scan_ident (&buf, inf_skip_spaces (' '));
964   if (SSTRING_LENGTH (&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
965     return 0;
966
967   /* So far so good: We've seen an initial #ifndef. */
968   *ifndef_line = lineno;
969   c = inf_scan_ident (&buf, inf_skip_spaces (c));
970   if (SSTRING_LENGTH (&buf) == 0 || c == EOF)
971     return 0;
972   protect_name = xstrdup (buf.base);
973
974   INF_UNGET (c);
975   c = inf_read_upto (&buf, '\n');
976   if (c == EOF)
977     return 0;
978   lineno++;
979
980   for (;;)
981     {
982       c = inf_skip_spaces (' ');
983       if (c == EOF)
984         return 0;
985       if (c == '\n')
986         {
987           lineno++;
988           continue;
989         }
990       if (c != '#')
991         goto skip_to_eol;
992       c = inf_scan_ident (&buf, inf_skip_spaces (' '));
993       if (SSTRING_LENGTH (&buf) == 0)
994         ;
995       else if (!strcmp (buf.base, "ifndef")
996           || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
997         {
998           if_nesting++;
999         }
1000       else if (!strcmp (buf.base, "endif"))
1001         {
1002           if_nesting--;
1003           if (if_nesting == 0)
1004             break;
1005         }
1006       else if (!strcmp (buf.base, "else"))
1007         {
1008           if (if_nesting == 1)
1009             return 0;
1010         }
1011       else if (!strcmp (buf.base, "define"))
1012         {
1013           if (if_nesting != 1)
1014             goto skip_to_eol;
1015           c = inf_skip_spaces (c);
1016           c = inf_scan_ident (&buf, c);
1017           if (buf.base[0] > 0 && strcmp (buf.base, protect_name) == 0)
1018             define_seen = 1;
1019         }
1020     skip_to_eol:
1021       for (;;)
1022         {
1023           if (c == '\n' || c == EOF)
1024             break;
1025           c = INF_GET ();
1026         }
1027       if (c == EOF)
1028         return 0;
1029       lineno++;
1030     }
1031
1032   if (!define_seen)
1033      return 0;
1034   *endif_line = lineno;
1035   /* Skip final white space (including comments). */
1036   for (;;)
1037     {
1038       c = inf_skip_spaces (' ');
1039       if (c == EOF)
1040         break;
1041       if (c != '\n')
1042         return 0;
1043     }
1044
1045   return 1;
1046 }
1047
1048 int
1049 main (argc, argv)
1050      int argc;
1051      char **argv;
1052 {
1053   int inf_fd;
1054   struct stat sbuf;
1055   int c;
1056   int i, done;
1057   const char *cptr, **pptr;
1058   int ifndef_line;
1059   int endif_line;
1060   long to_read;
1061   long int inf_size;
1062   register struct symbol_list *cur_symbols;
1063
1064   if (argv[0] && argv[0][0])
1065     {
1066       register char *p;
1067
1068       progname = 0;
1069       for (p = argv[0]; *p; p++)
1070         if (*p == '/')
1071           progname = p;
1072       progname = progname ? progname+1 : argv[0];
1073     }
1074
1075   if (argc < 4)
1076     {
1077       fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h options\n",
1078                progname);
1079       exit (-1);
1080     }
1081
1082   inc_filename = argv[1];
1083   inc_filename_length = strlen (inc_filename);
1084
1085 #ifdef FIXPROTO_IGNORE_LIST
1086   for (i = 0; files_to_ignore[i] != NULL; i++)
1087     {
1088       char *ignore_name = files_to_ignore[i];
1089       int ignore_len = strlen (ignore_name);
1090       if (strncmp (inc_filename, ignore_name, ignore_len) == 0)
1091         {
1092           if (ignore_name[ignore_len-1] == '/'
1093               || inc_filename[ignore_len] == '\0')
1094             {
1095               if (verbose)
1096                 fprintf (stderr, "%s: ignoring %s\n", progname, inc_filename);
1097               exit (0);
1098             }
1099         }
1100           
1101     }
1102 #endif
1103
1104   if (strcmp (inc_filename, "sys/stat.h") == 0)
1105     special_file_handling = sys_stat_h;
1106   else if (strcmp (inc_filename, "errno.h") == 0)
1107     special_file_handling = errno_h, required_other++;
1108   else if (strcmp (inc_filename, "stdlib.h") == 0)
1109     special_file_handling = stdlib_h, required_other+=2;
1110   else if (strcmp (inc_filename, "stdio.h") == 0)
1111     special_file_handling = stdio_h;
1112   include_entry = std_include_table;
1113   while (include_entry->name != NULL
1114          && (include_entry->name == CONTINUED
1115              || strcmp (inc_filename, include_entry->name) != 0))
1116     include_entry++;
1117
1118   if (include_entry->name != NULL)
1119     {
1120       struct std_include_entry *entry;
1121       cur_symbol_table_size = 0;
1122       for (entry = include_entry; ;)
1123         {
1124           add_symbols (entry->flags, entry->names);
1125           entry++;
1126           if (entry->name != CONTINUED)
1127             break;
1128         }
1129     }
1130   else
1131     symbol_table[0].names = NULL;
1132
1133   /* Count and mark the prototypes required for this include file. */ 
1134   for (cur_symbols = &symbol_table[0]; cur_symbols->names; cur_symbols++)
1135     {
1136       int name_len;
1137       if (cur_symbols->flags & MACRO_SYMBOL)
1138         continue;
1139       cptr = cur_symbols->names;
1140       for ( ; (name_len = strlen (cptr)) != 0; cptr+= name_len + 1)
1141         {
1142           struct fn_decl *fn = lookup_std_proto (cptr, name_len);
1143           required_unseen_count++;
1144           if (fn == NULL)
1145             fprintf (stderr, "Internal error:  No prototype for %s\n", cptr);
1146           else
1147             SET_REQUIRED (fn);
1148         }
1149     }
1150
1151   read_scan_file (argv[2], argc - 4, argv + 4);
1152
1153   inf_fd = open (argv[2], O_RDONLY, 0666);
1154   if (inf_fd < 0)
1155     {
1156       fprintf (stderr, "%s: Cannot open '%s' for reading -",
1157                progname, argv[2]);
1158       perror (NULL);
1159       exit (-1);
1160     }
1161   if (fstat (inf_fd, &sbuf) < 0)
1162     {
1163       fprintf (stderr, "%s: Cannot get size of '%s' -", progname, argv[2]);
1164       perror (NULL);
1165       exit (-1);
1166     }
1167   inf_size = sbuf.st_size;
1168   inf_buffer = (char*) xmalloc (inf_size + 2);
1169   inf_buffer[inf_size] = '\n';
1170   inf_buffer[inf_size + 1] = '\0';
1171   inf_limit = inf_buffer + inf_size;
1172   inf_ptr = inf_buffer;
1173
1174   to_read = inf_size;
1175   while (to_read > 0)
1176     {
1177       long i = read (inf_fd, inf_buffer + inf_size - to_read, to_read);
1178       if (i < 0)
1179         {
1180           fprintf (stderr, "%s: Failed to read '%s' -", progname, argv[2]);
1181           perror (NULL);
1182           exit (-1);
1183         }
1184       if (i == 0)
1185         {
1186           inf_size -= to_read;
1187           break;
1188         }
1189       to_read -= i;
1190     }
1191
1192   close (inf_fd);
1193
1194   /* If file doesn't end with '\n', add one. */
1195   if (inf_limit > inf_buffer && inf_limit[-1] != '\n')
1196     inf_limit++;
1197
1198   unlink (argv[3]);
1199   outf = fopen (argv[3], "w");
1200   if (outf == NULL)
1201     {
1202       fprintf (stderr, "%s: Cannot open '%s' for writing -",
1203                progname, argv[3]);
1204       perror (NULL);
1205       exit (-1);
1206     }
1207
1208   lineno = 1;
1209
1210   if (check_protection (&ifndef_line, &endif_line))
1211     {
1212       lbrac_line = ifndef_line+1;
1213       rbrac_line = endif_line;
1214     }
1215   else
1216     {
1217       lbrac_line = 1;
1218       rbrac_line = -1;
1219     }
1220
1221   /* Reset input file. */
1222   inf_ptr = inf_buffer;
1223   lineno = 1;
1224
1225   for (;;)
1226     {
1227       if (lineno == lbrac_line)
1228         write_lbrac ();
1229       if (lineno == rbrac_line)
1230         write_rbrac ();
1231       for (;;)
1232         {
1233           struct fn_decl *fn;
1234           c = INF_GET ();
1235           if (c == EOF)
1236             break;
1237           if (isalpha (c) || c == '_')
1238             {
1239               c = inf_scan_ident (&buf, c);
1240               INF_UNGET (c);
1241               fputs (buf.base, outf);
1242               fn = lookup_std_proto (buf.base, strlen (buf.base));
1243               /* We only want to edit the declaration matching the one
1244                  seen by scan-decls, as there can be multiple
1245                  declarations, selected by #ifdef __STDC__ or whatever. */
1246               if (fn && fn->partial && fn->partial->line_seen == lineno)
1247                 {
1248                   c = inf_skip_spaces (' ');
1249                   if (c == EOF)
1250                     break;
1251                   if (c == '(')
1252                     {
1253                       c = inf_skip_spaces (' ');
1254                       if (c == ')')
1255                         {
1256                           fprintf (outf, " _PARAMS((%s))", fn->params);
1257                         }
1258                       else
1259                         {
1260                           putc ('(', outf);
1261                           INF_UNGET (c);
1262                         }
1263                     }
1264                   else
1265                     fprintf (outf, " %c", c);
1266                 }
1267             }
1268           else
1269             {
1270               putc (c, outf);
1271               if (c == '\n')
1272                 break;
1273             }
1274         }
1275       if (c == EOF)
1276         break;
1277       lineno++;
1278     }
1279   if (rbrac_line < 0)
1280     write_rbrac ();
1281
1282   fclose (outf);
1283
1284   return 0;
1285 }
1286 \f
1287 /* Stub error functions.  These replace cpperror.c,
1288    because we want to suppress error messages. */
1289
1290 void
1291 cpp_file_line_for_message (pfile, filename, line, column)
1292      cpp_reader *pfile;
1293      char *filename;
1294      int line, column;
1295 {
1296   if (!verbose)
1297     return;
1298   if (column > 0)
1299     fprintf (stderr, "%s:%d:%d: ", filename, line, column);
1300   else
1301     fprintf (stderr, "%s:%d: ", filename, line);
1302 }
1303
1304 void
1305 cpp_print_containing_files (pfile)
1306      cpp_reader *pfile;
1307 {
1308 }
1309
1310 /* IS_ERROR is 2 for fatal error, 1 for error, 0 for warning */
1311 void cpp_message (pfile, is_error, msg, arg1, arg2, arg3)
1312      int is_error;
1313      cpp_reader *pfile;
1314      char *msg;
1315      char *arg1, *arg2, *arg3;
1316 {
1317   if (is_error == 1)
1318     pfile->errors++;
1319   else if (is_error > 1)
1320     pfile->errors = CPP_FATAL_LIMIT;
1321   if (!verbose)
1322     return;
1323   if (!is_error)
1324     fprintf (stderr, "warning: ");
1325   fprintf (stderr, msg, arg1, arg2, arg3);
1326   fprintf (stderr, "\n");
1327 }
1328
1329 void
1330 fatal (str, arg)
1331      char *str, *arg;
1332 {
1333   fprintf (stderr, "%s: %s: ", progname, inc_filename);
1334   fprintf (stderr, str, arg);
1335   fprintf (stderr, "\n");
1336   exit (FATAL_EXIT_CODE);
1337 }
1338
1339 void
1340 cpp_fatal (pfile, str, arg)
1341      cpp_reader *pfile;
1342      char *str, *arg;
1343 {
1344   fatal (str, arg);
1345 }
1346
1347 void
1348 cpp_pfatal_with_name (pfile, name)
1349      cpp_reader *pfile;
1350      char *name;
1351 {
1352   cpp_perror_with_name (pfile, name);
1353   exit (FATAL_EXIT_CODE);
1354 }