OSDN Git Service

(fancy_abort): Defined.
[pf3gnuchains/gcc-fork.git] / gcc / fix-header.c
1 /* fix-header.c - Make C header file suitable for C++.
2    Copyright (C) 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /* This program massages a system include file (such as stdio.h),
19    into a form more conformant with ANSI/POSIX, and more suitable for C++:
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 reqquired 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 REQUIRED_FUNCS <SCAN-FILE
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    * SCAN-FILE is the output of the scan-decls program.
70    * REQUIRED_FUNCS is a list of required function (e.g. fclose for stdio.h).
71
72    Written by Per Bothner <bothner@cygnus.com>, July 1993. */
73
74 #include <stdio.h>
75 #include <ctype.h>
76 #include <sys/types.h>
77 #include <sys/stat.h>
78 #ifndef O_RDONLY
79 #define O_RDONLY 0
80 #endif
81 #include "hconfig.h"
82 #include "obstack.h"
83 #include "scan.h"
84
85 int verbose = 0;
86 int partial_count = 0;
87 int missing_extern_C_count = 0;
88 int missing_errno = 0;
89
90 #include "xsys-protos.h"
91
92 char *inf_buffer;
93 char *inf_limit;
94 char *inf_ptr;
95
96 /* Certain standard files get extra treatment */
97
98 enum special_file
99 {
100   no_special,
101   errno_special,
102   sys_stat_special
103 };
104
105 enum special_file special_file_handling = no_special;
106
107 /* The following are only used when handling sys/stat.h */
108 /* They are set if the corresponding macro has been seen. */
109 int seen_S_IFBLK = 0, seen_S_ISBLK  = 0;
110 int seen_S_IFCHR = 0, seen_S_ISCHR  = 0;
111 int seen_S_IFDIR = 0, seen_S_ISDIR  = 0;
112 int seen_S_IFIFO = 0, seen_S_ISFIFO = 0;
113 int seen_S_IFLNK = 0, seen_S_ISLNK  = 0;
114 int seen_S_IFREG = 0, seen_S_ISREG  = 0;
115 \f
116 /* Wrapper around free, to avoid prototype clashes. */
117
118 void
119 xfree (ptr)
120      char *ptr;
121 {
122   free (ptr);
123 }
124
125 /* Avoid error if config defines abort as fancy_abort.
126    It's not worth "really" implementing this because ordinary
127    compiler users never run fix-header.  */
128
129 void
130 fancy_abort ()
131 {
132   abort ();
133 }
134 \f
135 #define obstack_chunk_alloc xmalloc
136 #define obstack_chunk_free xfree
137 struct obstack scan_file_obstack;
138
139 /* NOTE:  If you edit this, also edit gen-protos.c !! */
140 struct fn_decl *
141 lookup_std_proto (name)
142      char *name;
143 {
144   int i = hash (name) % HASH_SIZE;
145   int i0 = i;
146   for (;;)
147     {
148       struct fn_decl *fn;
149       if (hash_tab[i] == 0)
150         return NULL;
151       fn = &std_protos[hash_tab[i]];
152       if (strcmp (fn->fname, name) == 0)
153         return fn;
154       i = (i+1) % HASH_SIZE;
155       if (i == i0)
156         abort ();
157     }
158 }
159
160 char *inc_filename;
161 int inc_filename_length;
162 char *progname = "fix-header";
163 FILE *outf;
164 sstring buf;
165 sstring line;
166
167 int lbrac_line, rbrac_line;
168
169 char **required_functions;
170 int required_unseen_count;
171
172 int 
173 write_lbrac ()
174 {
175   
176   if (missing_extern_C_count + required_unseen_count > 0)
177     fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
178
179   if (partial_count)
180     {
181       fprintf (outf, "#ifndef _PARAMS\n");
182       fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
183       fprintf (outf, "#define _PARAMS(ARGS) ARGS\n");
184       fprintf (outf, "#else\n");
185       fprintf (outf, "#define _PARAMS(ARGS) ()\n");
186       fprintf (outf, "#endif\n#endif /* _PARAMS */\n");
187     }
188 }
189
190 struct partial_proto
191 {
192   struct partial_proto *next;
193   char *fname;  /* name of function */
194   char *rtype;  /* return type */
195   struct fn_decl *fn;
196   int line_seen;
197 };
198
199 struct partial_proto *partial_proto_list = NULL;
200
201 struct partial_proto required_dummy_proto;
202 #define REQUIRED(FN) ((FN)->partial == &required_dummy_proto)
203 #define SET_REQUIRED(FN) ((FN)->partial = &required_dummy_proto)
204 #define CLEAR_REQUIRED(FN) ((FN)->partial = 0)
205
206 void
207 recognized_macro (fname)
208      char *fname;
209 {
210   /* The original include file defines fname as a macro. */
211   struct fn_decl *fn = lookup_std_proto (fname);
212
213   /* Since fname is a macro, don't require a prototype for it. */
214   if (fn && REQUIRED (fn))
215     {
216       CLEAR_REQUIRED (fn);
217       required_unseen_count--;
218     }
219
220   switch (special_file_handling)
221     {
222     case errno_special:
223       if (strcmp (fname, "errno") == 0) missing_errno = 0;
224       break;
225     case sys_stat_special:
226       if (fname[0] == 'S' && fname[1] == '_')
227         {
228           if (strcmp (fname, "S_IFBLK") == 0) seen_S_IFBLK++;
229           else if (strcmp (fname, "S_ISBLK") == 0) seen_S_ISBLK++;
230           else if (strcmp (fname, "S_IFCHR") == 0) seen_S_IFCHR++;
231           else if (strcmp (fname, "S_ISCHR") == 0) seen_S_ISCHR++;
232           else if (strcmp (fname, "S_IFDIR") == 0) seen_S_IFDIR++;
233           else if (strcmp (fname, "S_ISDIR") == 0) seen_S_ISDIR++;
234           else if (strcmp (fname, "S_IFIFO") == 0) seen_S_IFIFO++;
235           else if (strcmp (fname, "S_ISFIFO") == 0) seen_S_ISFIFO++;
236           else if (strcmp (fname, "S_IFLNK") == 0) seen_S_IFLNK++;
237           else if (strcmp (fname, "S_ISLNK") == 0) seen_S_ISLNK++;
238           else if (strcmp (fname, "S_IFREG") == 0) seen_S_IFREG++;
239           else if (strcmp (fname, "S_ISREG") == 0) seen_S_ISREG++;
240         }
241     }
242 }
243
244 void
245 recognized_extern (name, type)
246      char *name;
247      char *type;
248 {
249   switch (special_file_handling)
250     {
251     case errno_special:
252       if (strcmp (name, "errno") == 0) missing_errno = 0;
253       break;
254     }
255 }
256
257 /* Called by scan_decls if it saw a function definition for a function
258    named FNAME, with return type RTYPE, and argument list ARGS,
259    in source file FILE_SEEN on line LINE_SEEN.
260    KIND is 'I' for an inline function;
261    'F' if a normal function declaration preceded by 'extern "C"'
262    (or nested inside 'extern "C"' braces); or
263    'f' for other function declarations. */
264
265 void
266 recognized_function (fname, kind, rtype, args, file_seen, line_seen)
267      char *fname;
268      int kind; /* One of 'f' 'F' or 'I' */
269      char *rtype;
270      char *args;
271      char *file_seen;
272      int line_seen;
273 {
274   struct partial_proto *partial;
275   int i;
276   struct fn_decl *fn;
277   if (kind == 'f')
278     missing_extern_C_count++;
279
280   fn = lookup_std_proto (fname);
281
282   /* Remove the function from the list of required function. */
283   if (fn && REQUIRED (fn))
284     {
285       CLEAR_REQUIRED (fn);
286       required_unseen_count--;
287     }
288
289   /* If we have a full prototype, we're done. */
290   if (args[0] != '\0')
291     return;
292       
293   if (kind == 'I')  /* don't edit inline function */
294     return;
295
296   /* If the partial prototype was included from some other file,
297      we don't need to patch it up (in this run). */
298   i = strlen (file_seen);
299   if (i < inc_filename_length
300       || strcmp (inc_filename, file_seen + (i - inc_filename_length)) != 0)
301     return;
302
303   if (fn == NULL)
304     return;
305   if (fn->params[0] == '\0' || strcmp (fn->params, "void") == 0)
306     return;
307
308   /* We only have a partial function declaration,
309      so remember that we have to add a complete prototype. */
310   partial_count++;
311   partial = (struct partial_proto*)
312     obstack_alloc (&scan_file_obstack, sizeof (struct partial_proto));
313   partial->fname = obstack_alloc (&scan_file_obstack, strlen (fname) + 1);
314   strcpy (partial->fname, fname);
315   partial->rtype = obstack_alloc (&scan_file_obstack, strlen (rtype) + 1);
316   strcpy (partial->rtype, rtype);
317   partial->line_seen = line_seen;
318   partial->fn = fn;
319   fn->partial = partial;
320   partial->next = partial_proto_list;
321   partial_proto_list = partial;
322   if (verbose)
323     {
324       fprintf (stderr, "(%s: %s non-prototype function declaration.)\n",
325                inc_filename, fname);
326     }
327 }
328
329 void
330 read_scan_file (scan_file)
331      FILE *scan_file;
332 {
333   char **rptr;
334   obstack_init (&scan_file_obstack); 
335
336   scan_decls (scan_file);
337
338   if (missing_extern_C_count + required_unseen_count + partial_count
339       + missing_errno == 0)
340     {
341       if (verbose)
342         fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
343       exit (0);
344     }
345   if (!verbose)
346     fprintf (stderr, "%s: fixing %s\n", progname, inc_filename);
347   else
348     {
349       if (required_unseen_count)
350         fprintf (stderr, "%s: %d missing function declarations.\n",
351                  inc_filename, required_unseen_count);
352       if (partial_count)
353         fprintf (stderr, "%s: %d non-prototype function declarations.\n",
354                  inc_filename, partial_count);
355       if (missing_extern_C_count)
356         fprintf (stderr,
357                  "%s: %d declarations not protected by extern \"C\".\n",
358                  inc_filename, missing_extern_C_count);
359     }
360 }
361
362 write_rbrac ()
363 {
364   struct fn_decl *fn;
365   char **rptr;
366   register struct partial_proto *partial;
367
368   if (required_unseen_count)
369     fprintf (outf, "#if defined(__STDC__) || defined(__cplusplus)\n");
370
371   /* Now we print out prototypes for those functions that we haven't seen. */
372   for (rptr = required_functions; *rptr; rptr++)
373     {
374       int macro_protect = 0;
375
376       fn = lookup_std_proto (*rptr);
377       if (fn == NULL || !REQUIRED (fn))
378         continue;
379
380       /* In the case of memmove, protect in case the application
381          defines it as a macro before including the header.  */
382       if (!strcmp (fn->fname, "memmove"))
383         macro_protect = 1;
384
385       if (macro_protect)
386         fprintf (outf, "#ifndef %s\n", fn->fname);
387       fprintf (outf, "extern %s %s (%s);\n",
388                fn->rtype, fn->fname, fn->params);
389       if (macro_protect)
390         fprintf (outf, "#endif\n");
391     }
392   if (required_unseen_count)
393     fprintf (outf,
394              "#endif /* defined(__STDC__) || defined(__cplusplus) */\n");
395
396   switch (special_file_handling)
397     {
398     case errno_special:
399       if (missing_errno)
400         fprintf (outf, "extern int errno;\n");
401       break;
402     case sys_stat_special:
403       if (!seen_S_ISBLK && seen_S_IFBLK)
404         fprintf (outf,
405                  "#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)\n");
406       if (!seen_S_ISCHR && seen_S_IFCHR)
407         fprintf (outf,
408                  "#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)\n");
409       if (!seen_S_ISDIR && seen_S_IFDIR)
410         fprintf (outf,
411                  "#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)\n");
412       if (!seen_S_ISFIFO && seen_S_IFIFO)
413         fprintf (outf,
414                  "#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)\n");
415       if (!seen_S_ISLNK && seen_S_IFLNK)
416         fprintf (outf,
417                  "#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)\n");
418       if (!seen_S_ISREG && seen_S_IFREG)
419         fprintf (outf,
420                  "#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)\n");
421       break;
422     }
423
424
425   if (missing_extern_C_count + required_unseen_count > 0)
426     fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
427 }
428
429 char *
430 strdup (str)
431      char *str;
432 {
433   char *copy = (char *) xmalloc (strlen (str) + 1);
434   strcpy (copy, str);
435   return copy;
436 }
437
438 /* Returns 1 iff the file is properly protected from multiple inclusion:
439    #ifndef PROTECT_NAME
440    #define PROTECT_NAME
441    #endif
442
443  */
444
445 #define INF_GET() (inf_ptr < inf_limit ? *(unsigned char*)inf_ptr++ : EOF)
446 #define INF_UNGET(c) ((c)!=EOF && inf_ptr--)
447
448 int
449 inf_skip_spaces (c)
450      int c;
451 {
452   for (;;)
453     {
454       if (c == ' ' || c == '\t')
455         c = INF_GET ();
456       else if (c == '/')
457         {
458           c = INF_GET ();
459           if (c != '*')
460             {
461               INF_UNGET (c);
462               return '/';
463             }
464           c = INF_GET ();
465           for (;;)
466             {
467               if (c == EOF)
468                 return EOF;
469               else if (c != '*')
470                 {
471                   if (c == '\n')
472                     source_lineno++, lineno++;
473                   c = INF_GET ();
474                 }
475               else if ((c = INF_GET ()) == '/')
476                 return INF_GET ();
477             }
478         }
479       else
480         break;
481     }
482   return c;
483 }
484
485 /* Read into STR from inf_buffer upto DELIM. */
486
487 int
488 inf_read_upto (str, delim)
489      sstring *str;
490      int delim;
491 {
492   int ch;
493   for (;;)
494     {
495       ch = INF_GET ();
496       if (ch == EOF || ch == delim)
497         break;
498       SSTRING_PUT (str, ch);
499     }
500   MAKE_SSTRING_SPACE (str, 1);
501   *str->ptr = 0;
502   return ch;
503 }
504
505 int
506 inf_scan_ident (s, c)
507      register sstring *s;
508      int c;
509 {
510   s->ptr = s->base;
511   if (isalpha (c) || c == '_')
512     {
513       for (;;)
514         {
515           SSTRING_PUT (s, c);
516           c = INF_GET ();
517           if (c == EOF || !(isalnum (c) || c == '_'))
518             break;
519         }
520     }
521   MAKE_SSTRING_SPACE (s, 1);
522   *s->ptr = 0;
523   return c;
524 }
525
526 /* Returns 1 if the file is correctly protected against multiple
527    inclusion, setting *ifndef_line to the line number of the initial #ifndef
528    and setting *endif_line to the final #endif.
529    Otherwise return 0. */
530
531 int
532 check_protection (ifndef_line, endif_line)
533      int *ifndef_line, *endif_line;
534 {
535   int c;
536   int if_nesting = 1; /* Level of nesting of #if's */
537   char *protect_name = NULL; /* Identifier following initial #ifndef */
538   int define_seen = 0;
539
540   /* Skip initial white space (including comments). */
541   for (;; lineno++)
542     {
543       c = inf_skip_spaces (' ');
544       if (c == EOF)
545         return 0;
546       if (c != '\n')
547         break;
548     }
549   if (c != '#')
550     return 0;
551   c = inf_scan_ident (&buf, inf_skip_spaces (' '));
552   if (SSTRING_LENGTH (&buf) == 0 || strcmp (buf.base, "ifndef") != 0)
553     return 0;
554
555   /* So far so good: We've seen an initial #ifndef. */
556   *ifndef_line = lineno;
557   c = inf_scan_ident (&buf, inf_skip_spaces (c));
558   if (SSTRING_LENGTH (&buf) == 0 || c == EOF)
559     return 0;
560   protect_name = strdup (buf.base);
561
562   INF_UNGET (c);
563   c = inf_read_upto (&buf, '\n');
564   if (c == EOF)
565     return 0;
566   lineno++;
567
568   for (;;)
569     {
570       c = inf_skip_spaces (' ');
571       if (c == EOF)
572         return 0;
573       if (c == '\n')
574         {
575           lineno++;
576           continue;
577         }
578       if (c != '#')
579         goto skip_to_eol;
580       c = inf_scan_ident (&buf, inf_skip_spaces (' '));
581       if (SSTRING_LENGTH (&buf) == 0)
582         ;
583       else if (!strcmp (buf.base, "ifndef")
584           || !strcmp (buf.base, "ifdef") || !strcmp (buf.base, "if"))
585         {
586           if_nesting++;
587         }
588       else if (!strcmp (buf.base, "endif"))
589         {
590           if_nesting--;
591           if (if_nesting == 0)
592             break;
593         }
594       else if (!strcmp (buf.base, "else"))
595         {
596           if (if_nesting == 1)
597             return 0;
598         }
599       else if (!strcmp (buf.base, "define"))
600         {
601           if (if_nesting != 1)
602             goto skip_to_eol;
603           c = inf_skip_spaces (c);
604           c = inf_scan_ident (&buf, c);
605           if (buf.base[0] > 0 && strcmp (buf.base, protect_name) == 0)
606             define_seen = 1;
607         }
608     skip_to_eol:
609       for (;;)
610         {
611           if (c == '\n' || c == EOF)
612             break;
613           c = INF_GET ();
614         }
615       if (c == EOF)
616         return 0;
617       lineno++;
618     }
619
620   if (!define_seen)
621      return 0;
622   *endif_line = lineno;
623   /* Skip final white space (including comments). */
624   for (;;)
625     {
626       c = inf_skip_spaces (' ');
627       if (c == EOF)
628         break;
629       if (c != '\n')
630         return 0;
631     }
632
633   return 1;
634 }
635
636 int
637 main (argc, argv)
638      int argc;
639      char **argv;
640 {
641   int inf_fd;
642   struct stat sbuf;
643   int c;
644   int i, done;
645   char *cptr, *cptr0, **pptr;
646   int ifndef_line;
647   int endif_line;
648   long to_read;
649   long int inf_size;
650
651   if (argv[0] && argv[0][0])
652     {
653       register char *p;
654
655       progname = 0;
656       for (p = argv[0]; *p; p++)
657         if (*p == '/')
658           progname = p;
659       progname = progname ? progname+1 : argv[0];
660     }
661
662   if (argc < 4)
663     {
664       fprintf (stderr, "%s: Usage: foo.h infile.h outfile.h req_funcs <scan-file-name\n",
665                progname);
666       exit (-1);
667     }
668
669   inc_filename = argv[1];
670   inc_filename_length = strlen (inc_filename);
671   if (strcmp (inc_filename, "sys/stat.h") == 0)
672     special_file_handling = sys_stat_special;
673   else if (strcmp (inc_filename, "errno.h") == 0)
674     special_file_handling = errno_special, missing_errno = 1;
675
676   /* Calculate an upper bound of the number of function names in argv[4] */
677   for (i = 1, cptr = argv[4]; *cptr; cptr++)
678     if (*cptr == ' ') i++;
679   /* Find the list of prototypes required for this include file. */ 
680   required_functions = (char**)xmalloc ((i+1) * sizeof (char*));
681   for (cptr = argv[4], cptr0 = cptr, pptr = required_functions, done = 0; 
682        !done; cptr++)
683     {
684       done = *cptr == '\0';
685       if (*cptr == ' ' || done)
686         {
687           *cptr = '\0';
688           if (cptr > cptr0)
689             {
690               struct fn_decl *fn = lookup_std_proto (cptr0);
691               *pptr++ = cptr0;
692               if (fn == NULL)
693                 fprintf (stderr, "Internal error:  No prototype for %s\n",
694                          cptr0);
695               else
696                 SET_REQUIRED (fn);
697             }
698           cptr0 = cptr + 1;
699         }
700     }
701   required_unseen_count = pptr - required_functions;
702   *pptr = 0;
703
704   read_scan_file (stdin);
705
706   inf_fd = open (argv[2], O_RDONLY, 0666);
707   if (inf_fd < 0)
708     {
709       fprintf (stderr, "%s: Cannot open '%s' for reading -",
710                progname, argv[2]);
711       perror (NULL);
712       exit (-1);
713     }
714   if (fstat (inf_fd, &sbuf) < 0)
715     {
716       fprintf (stderr, "%s: Cannot get size of '%s' -", progname, argv[2]);
717       perror (NULL);
718       exit (-1);
719     }
720   inf_size = sbuf.st_size;
721   inf_buffer = (char*) xmalloc (inf_size + 2);
722   inf_buffer[inf_size] = '\n';
723   inf_buffer[inf_size + 1] = '\0';
724   inf_limit = inf_buffer + inf_size;
725   inf_ptr = inf_buffer;
726
727   to_read = inf_size;
728   while (to_read > 0)
729     {
730       long i = read (inf_fd, inf_buffer + inf_size - to_read, to_read);
731       if (i < 0)
732         {
733           fprintf (stderr, "%s: Failed to read '%s' -", progname, argv[2]);
734           perror (NULL);
735           exit (-1);
736         }
737       if (i == 0)
738         {
739           inf_size -= to_read;
740           break;
741         }
742       to_read -= i;
743     }
744
745   close (inf_fd);
746
747   /* If file doesn't end with '\n', add one. */
748   if (inf_limit > inf_buffer && inf_limit[-1] != '\n')
749     inf_limit++;
750
751   unlink (argv[3]);
752   outf = fopen (argv[3], "w");
753   if (outf == NULL)
754     {
755       fprintf (stderr, "%s: Cannot open '%s' for writing -",
756                progname, argv[3]);
757       perror (NULL);
758       exit (-1);
759     }
760
761   lineno = 1;
762
763   if (check_protection (&ifndef_line, &endif_line))
764     {
765 #if 0
766       fprintf (stderr, "#ifndef %s on line %d; #endif on line %d\n",
767                protect_name, ifndef_line, endif_line);
768 #endif
769       lbrac_line = ifndef_line+1;
770       rbrac_line = endif_line;
771     }
772   else
773     {
774       lbrac_line = 1;
775       rbrac_line = -1;
776     }
777
778   /* Reset input file. */
779   inf_ptr = inf_buffer;
780   lineno = 1;
781
782   for (;;)
783     {
784       if (lineno == lbrac_line)
785         write_lbrac ();
786       if (lineno == rbrac_line)
787         write_rbrac ();
788       for (;;)
789         {
790           struct fn_decl *fn;
791           c = INF_GET ();
792           if (c == EOF)
793             break;
794           if (isalpha (c) || c == '_')
795             {
796               struct partial_proto *partial;
797               c = inf_scan_ident (&buf, c);
798               INF_UNGET (c);
799               fputs (buf.base, outf);
800               fn = lookup_std_proto (buf.base);
801               /* We only want to edit the declaration matching the one
802                  seen by scan-decls, as there can be multiple
803                  declarations, selected by #ifdef __STDC__ or whatever. */
804               if (fn && fn->partial && fn->partial->line_seen == lineno)
805                 {
806                   c = inf_skip_spaces (' ');
807                   if (c == EOF)
808                     break;
809                   if (c == '(')
810                     {
811                       c = inf_skip_spaces (' ');
812                       if (c == ')')
813                         {
814                           fprintf (outf, " _PARAMS((%s))", fn->params);
815                         }
816                       else
817                         {
818                           putc ('(', outf);
819                           INF_UNGET (c);
820                         }
821                     }
822                   else
823                     fprintf (outf, " %c", c);
824                 }
825             }
826           else
827             {
828               putc (c, outf);
829               if (c == '\n')
830                 break;
831             }
832         }
833       if (c == EOF)
834         break;
835       lineno++;
836     }
837   if (rbrac_line < 0)
838     write_rbrac ();
839
840   fclose (outf);
841
842   return 0;
843 }