OSDN Git Service

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