]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/protoize.c
*** empty log message ***
[thirdparty/gcc.git] / gcc / protoize.c
CommitLineData
5f8037c4
RS
1/* Protoize program - Written by Ron Guilmette at the Microelectronics
2 and Computer Technology Corporation (MCC). The author's current
3 E-mail address is <rfg@ncd.com>.
4
34e56753 5 Copyright (C) 1989, 1992 Free Software Foundation, Inc.
5f8037c4
RS
6
7This file is part of GNU CC.
8
9GNU CC is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
14GNU CC is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU CC; see the file COPYING. If not, write to
21the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22
23/* Any reasonable C++ compiler should have all of the same features
24 as __STDC__ plus more, so make sure that __STDC__ is defined if
25 __cplusplus is defined. */
26
27#if defined(__cplusplus) && !defined(__STDC__)
28#define __STDC__ 1
29#endif /* defined(__cplusplus) && !defined(__STDC__) */
30
a2b22788
RS
31#if defined(__GNUC__) || defined (__GNUG__)
32#define VOLATILE volatile
33#else
34#define VOLATILE
35#endif
5f8037c4 36
34e56753
RS
37#ifndef __STDC__
38#define const
39#endif
5f8037c4 40
34e56753 41#include "config.h"
a2b22788 42
34e56753
RS
43#ifdef POSIX /* We should be able to define _POSIX_SOURCE unconditionally,
44 but some systems respond in buggy ways to it,
45 including Sunos 4.1.1. Which we don't classify as POSIX. */
46/* In case this is a POSIX system with an ANSI C compiler,
47 ask for definition of all POSIX facilities. */
48#define _POSIX_SOURCE
a2b22788
RS
49#endif
50
34e56753
RS
51#if 0
52/* Users are not supposed to use _POSIX_SOURCE to say the
53 system is a POSIX system. That is not what _POSIX_SOURCE means! -- rms */
a2b22788
RS
54/* If the user asked for POSIX via _POSIX_SOURCE, turn on POSIX code. */
55#if defined(_POSIX_SOURCE) && !defined(POSIX)
56#define POSIX
57#endif
34e56753 58#endif /* 0 */
5f8037c4
RS
59
60#include <stdio.h>
61#include <ctype.h>
62#include <errno.h>
63#include <sys/types.h>
64#include <sys/stat.h>
65#include <sys/dir.h>
34e56753 66#if ! defined (USG) || defined (SVR4)
5f8037c4 67#include <sys/wait.h>
34e56753 68#endif
5f8037c4
RS
69#include <setjmp.h>
70#include "gvarargs.h"
71#include "getopt.h"
72
5f8037c4
RS
73#ifndef PATH_MAX /* <limits.h> defines this on most POSIX systems. */
74#include <sys/param.h>
5f8037c4
RS
75/* Sometimes <sys/param.h> defines these macros. */
76#undef CHAR_BIT
77#undef CHAR_MAX
78#undef CHAR_MIN
79#undef CLK_TCK
80#undef INT_MAX
81#undef INT_MIN
82#undef LONG_MAX
83#undef LONG_MIN
84#undef SCHAR_MAX
85#undef SCHAR_MIN
86#undef SHRT_MAX
87#undef SHRT_MIN
88#undef UCHAR_MAX
89#undef UINT_MAX
90#undef ULONG_MAX
91#undef USHRT_MAX
92#endif
93
94extern int errno;
a2b22788
RS
95extern char *sys_errlist[];
96extern char *version_string;
5f8037c4 97
5f8037c4
RS
98/* Systems which are compatible only with POSIX 1003.1-1988 (but *not*
99 with POSIX 1003.1-1990), e.g. Ultrix 4.2, might not have
100 const qualifiers in the prototypes in the system include files.
101 Unfortunately, this can lead to GCC issuing lots of warnings for
102 calls to the following functions. To eliminate these warnings we
103 provide the following #defines. */
104
34e56753
RS
105#define my_access(file,flag) access((char *)file, flag)
106#define my_stat(file,pkt) stat((char *)file, pkt)
107#define my_execvp(prog,argv) execvp((char *)prog, (char **)argv)
108#define my_link(file1, file2) link((char *)file1, (char *)file2)
109#define my_unlink(file) unlink((char *)file)
110#define my_open(file, mode, flag) open((char *)file, mode, flag)
111#define my_chmod(file, mode) chmod((char *)file, mode)
112
113#if !(defined (USG) || defined (VMS) || defined (POSIX))
114#define GUESSPATHLEN (MAXPATHLEN + 1)
115#else /* (defined (USG) || defined (VMS) || defined (POSIX)) */
116/* We actually use this as a starting point, not a limit. */
117#define GUESSPATHLEN 200
118#endif /* (defined (USG) || defined (VMS) || defined (POSIX)) */
5f8037c4 119
34e56753
RS
120/* Aliases for pointers to void.
121 These were made to facilitate compilation with other compilers. */
5f8037c4 122
34e56753
RS
123#ifdef __STDC__
124typedef void * pointer_type;
125typedef const void * const_pointer_type;
126#else
127typedef char * pointer_type;
128typedef char * const_pointer_type;
129#endif
130
131#if defined(POSIX)
132
133#include <stdlib.h>
134#include <unistd.h>
135#include <signal.h>
136#include <fcntl.h>
137#include <string.h>
5f8037c4 138
a2b22788 139#else /* !defined(POSIX) */
5f8037c4
RS
140
141#define R_OK 4 /* Test for Read permission */
142#define W_OK 2 /* Test for Write permission */
143#define X_OK 1 /* Test for eXecute permission */
144#define F_OK 0 /* Test for existence of File */
145
146#define O_RDONLY 0
147#define O_WRONLY 1
148
149/* Virtually every UN*X system now in common use (except for pre-4.3-tahoe
150 BSD systems) now provides getcwd as called for by POSIX. Allow for
151 the few exceptions to the general rule here. */
152
153#if !(defined (USG) || defined (VMS))
34e56753 154extern char *getwd ();
5f8037c4
RS
155#define getcwd(buf,len) getwd(buf)
156#else /* (defined (USG) || defined (VMS)) */
34e56753 157extern char *getcwd ();
5f8037c4
RS
158#endif /* (defined (USG) || defined (VMS)) */
159
a2b22788
RS
160/* Declaring stat or __flsbuf with a prototype
161 causes conflicts with system headers on some systems. */
5f8037c4 162
a2b22788 163#ifndef abort
34e56753 164extern VOLATILE void abort ();
a2b22788 165#endif
34e56753
RS
166extern int kill();
167extern int creat ();
a2b22788 168#if 0 /* These conflict with stdio.h on some systems. */
5f8037c4
RS
169extern int fprintf (FILE *, const char *, ...);
170extern int printf (const char *, ...);
a2b22788 171#endif /* 0 */
34e56753
RS
172extern void exit ();
173extern pointer_type malloc ();
174extern pointer_type realloc ();
175extern void free ();
176extern int read ();
177extern int write ();
178extern int close ();
179extern int fflush ();
180extern int atoi ();
181extern int puts ();
182extern int fputs ();
183extern int fputc ();
184#if 0 /* Causes trouble on some systems that define setjmp as a macro. */
185extern int setjmp ();
186extern void longjmp ();
187#endif
188
189extern char * strcat();
190extern int strcmp();
191extern char * strcpy();
a2b22788
RS
192#if 0 /* size_t from sys/types.h may fail to match GCC.
193 If so, we would get a warning from this.
194 So do without the prototype. */
5f8037c4 195extern size_t strlen(const char *);
a2b22788 196#endif
34e56753
RS
197extern int strncmp();
198extern char * strncpy();
199extern char * strrchr();
5f8037c4
RS
200
201#if !(defined (USG) || defined (VMS))
34e56753 202extern int vfork ();
5f8037c4 203#define fork vfork
a2b22788 204#else
34e56753 205extern int fork ();
5f8037c4
RS
206#endif /* !(defined (USG) || defined (VMS)) */
207
a2b22788 208#endif /* !defined(POSIX) */
5f8037c4
RS
209
210/* Look for these where the `const' qualifier is intentionally cast aside. */
211
212#define NONCONST
213
34e56753 214/* Define a STRINGIFY macro that's right for ANSI or traditional C. */
5f8037c4 215
34e56753 216#ifdef __STDC__
5f8037c4 217#define STRINGIFY(STRING) #STRING
34e56753
RS
218#else
219#define STRINGIFY(STRING) "STRING"
220#endif
5f8037c4
RS
221
222/* POSIX systems will not have definitions for WIFEXITED or WEXITSTATUS.
223 Define them correctly and so that they work for all environments. */
224
225#undef WIFEXITED
226#define WIFEXITED(status_word) ((*((int *)&status_word) & 0xff) == 0x00)
227
228#undef WEXITSTATUS
229#define WEXITSTATUS(status_word) ((*((int *)&status_word) & 0xff00) >> 8)
230
231/* Define a default place to find the SYSCALLS.X file. */
232
233#ifndef STD_PROTO_DIR
234#define STD_PROTO_DIR "/usr/local/lib"
235#endif /* !defined(STD_PROTO_DIR) */
236
5f8037c4
RS
237/* Suffix of aux_info files. */
238
239static const char * const aux_info_suffix = ".X";
240
a2b22788 241/* String to attach to filenames for saved versions of original files. */
5f8037c4
RS
242
243static const char * const save_suffix = ".save";
244
245#ifndef UNPROTOIZE
246
247/* File name of the file which contains descriptions of standard system
248 routines. Note that we never actually do anything with this file per se,
249 but we do read in its corresponding aux_info file. */
250
251static const char * const syscalls_filename = "SYSCALLS.c";
252
253/* Default place to find the above file. */
254
255static const char * const default_syscalls_dir = STD_PROTO_DIR;
256
a2b22788 257/* Variable to hold the complete absolutized filename of the SYSCALLS.c.X
5f8037c4
RS
258 file. */
259
a2b22788 260static char * syscalls_absolute_filename;
5f8037c4
RS
261
262#endif /* !defined(UNPROTOIZE) */
263
264/* Type of the structure that holds information about macro unexpansions. */
265
266struct unexpansion_struct {
267 const char *expanded;
268 const char *contracted;
269};
270typedef struct unexpansion_struct unexpansion;
271
272/* A table of conversions that may need to be made for some (stupid) older
273 operating systems where these types are preprocessor macros rather than
274 typedefs (as they really ought to be).
275
276 WARNING: The contracted forms must be as small (or smaller) as the
277 expanded forms, or else havoc will ensue. */
278
279static const unexpansion unexpansions[] = {
280 { "struct _iobuf", "FILE" },
281 { 0, 0 }
282};
283
284/* The number of "primary" slots in the hash tables for filenames and for
285 function names. This can be as big or as small as you like, except that
286 it must be a power of two. */
287
288#define HASH_TABLE_SIZE (1 << 9)
289
290/* Bit mask to use when computing hash values. */
291
292static const int hash_mask = (HASH_TABLE_SIZE - 1);
293
294/* Make a table of default system include directories
295 just as it is done in cccp.c. */
296
297#ifndef STANDARD_INCLUDE_DIR
298#define STANDARD_INCLUDE_DIR "/usr/include"
299#endif
300
301#ifndef LOCAL_INCLUDE_DIR
302#define LOCAL_INCLUDE_DIR "/usr/local/include"
303#endif
304
305struct default_include { char *fname; int cplusplus; } include_defaults[]
306#ifdef INCLUDE_DEFAULTS
307 = INCLUDE_DEFAULTS;
308#else
309 = {
310 /* Pick up GNU C++ specific include files. */
311 { GPLUSPLUS_INCLUDE_DIR, 1},
312 { GCC_INCLUDE_DIR, 0},
313#ifdef CROSS_COMPILE
314 /* For cross-compilation, this dir name is generated
315 automatically in Makefile.in. */
316 { CROSS_INCLUDE_DIR, 0 },
317#else /* not CROSS_COMPILE */
318 { LOCAL_INCLUDE_DIR, 0},
319 /* Some systems have an extra dir of include files. */
320#ifdef SYSTEM_INCLUDE_DIR
321 { SYSTEM_INCLUDE_DIR, 0},
322#endif
5f8037c4 323 { STANDARD_INCLUDE_DIR, 0},
5f8037c4
RS
324#endif /* not CROSS_COMPILE */
325 { 0, 0}
326 };
327#endif /* no INCLUDE_DEFAULTS */
328
329/* Datatype for lists of directories or filenames. */
330struct string_list
331{
332 char *name;
333 struct string_list *next;
334};
335
336/* List of directories in which files should be converted. */
337
338struct string_list *directory_list;
339
340/* List of file names which should not be converted.
341 A file is excluded if the end of its name, following a /,
342 matches one of the names in this list. */
343
344struct string_list *exclude_list;
345
346/* The name of the other style of variable-number-of-parameters functions
347 (i.e. the style that we want to leave unconverted because we don't yet
348 know how to convert them to this style. This string is used in warning
349 messages. */
350
351/* Also define here the string that we can search for in the parameter lists
352 taken from the .X files which will unambiguously indicate that we have
353 found a varargs style function. */
354
355#ifdef UNPROTOIZE
356static const char * const other_var_style = "stdarg";
357#else /* !defined(UNPROTOIZE) */
358static const char * const other_var_style = "varargs";
a2b22788
RS
359/* Note that this is a string containing the expansion of va_alist.
360 But in `main' we discard all but the first token. */
361static const char *varargs_style_indicator = STRINGIFY(va_alist);
5f8037c4
RS
362#endif /* !defined(UNPROTOIZE) */
363
364/* The following two types are used to create hash tables. In this program,
365 there are two hash tables which are used to store and quickly lookup two
366 different classes of strings. The first type of strings stored in the
a2b22788 367 first hash table are absolute filenames of files which protoize needs to
5f8037c4
RS
368 know about. The second type of strings (stored in the second hash table)
369 are function names. It is this second class of strings which really
370 inspired the use of the hash tables, because there may be a lot of them. */
371
372typedef struct hash_table_entry_struct hash_table_entry;
373
374/* Do some typedefs so that we don't have to write "struct" so often. */
375
376typedef struct def_dec_info_struct def_dec_info;
377typedef struct file_info_struct file_info;
378typedef struct f_list_chain_item_struct f_list_chain_item;
379
380/* In the struct below, note that the "_info" field has two different uses
a2b22788
RS
381 depending on the type of hash table we are in (i.e. either the filenames
382 hash table or the function names hash table). In the filenames hash table
5f8037c4 383 the info fields of the entries point to the file_info struct which is
a2b22788 384 associated with each filename (1 per filename). In the function names
5f8037c4
RS
385 hash table, the info field points to the head of a singly linked list of
386 def_dec_info entries which are all defs or decs of the function whose
387 name is pointed to by the "symbol" field. Keeping all of the defs/decs
388 for a given function name on a special list specifically for that function
389 name makes it quick and easy to find out all of the important information
390 about a given (named) function. */
391
392struct hash_table_entry_struct {
393 hash_table_entry * hash_next; /* -> to secondary entries */
394 const char * symbol; /* -> to the hashed string */
395 union {
396 const def_dec_info * _ddip;
397 file_info * _fip;
398 } _info;
399};
400#define ddip _info._ddip
401#define fip _info._fip
402
403/* Define a type specifically for our two hash tables. */
404
405typedef hash_table_entry hash_table[HASH_TABLE_SIZE];
406
407/* The following struct holds all of the important information about any
a2b22788 408 single filename (e.g. file) which we need to know about. */
5f8037c4
RS
409
410struct file_info_struct {
411 const hash_table_entry * hash_entry; /* -> to associated hash entry */
412 const def_dec_info * defs_decs; /* -> to chain of defs/decs */
413 time_t mtime; /* Time of last modification. */
414};
415
416/* Due to the possibility that functions may return pointers to functions,
417 (which may themselves have their own parameter lists) and due to the
418 fact that returned pointers-to-functions may be of type "pointer-to-
419 function-returning-pointer-to-function" (ad nauseum) we have to keep
420 an entire chain of ANSI style formal parameter lists for each function.
421
422 Normally, for any given function, there will only be one formals list
423 on the chain, but you never know.
424
425 Note that the head of each chain of formals lists is pointed to by the
426 `f_list_chain' field of the corresponding def_dec_info record.
427
428 For any given chain, the item at the head of the chain is the *leftmost*
429 parameter list seen in the actual C language function declaration. If
430 there are other members of the chain, then these are linked in left-to-right
431 order from the head of the chain. */
432
433struct f_list_chain_item_struct {
434 const f_list_chain_item * chain_next; /* -> to next item on chain */
435 const char * formals_list; /* -> to formals list string */
436};
437
438/* The following struct holds all of the important information about any
439 single function definition or declaration which we need to know about.
440 Note that for unprotoize we don't need to know very much because we
441 never even create records for stuff that we don't intend to convert
442 (like for instance defs and decs which are already in old K&R format
443 and "implicit" function declarations). */
444
445struct def_dec_info_struct {
446 const def_dec_info * next_in_file; /* -> to rest of chain for file */
447 file_info * file; /* -> file_info for containing file */
448 int line; /* source line number of def/dec */
449 const char * ansi_decl; /* -> left end of ansi decl */
450 hash_table_entry * hash_entry; /* -> hash entry for function name */
451 unsigned int is_func_def; /* = 0 means this is a declaration */
452 const def_dec_info * next_for_func; /* -> to rest of chain for func name */
453 unsigned int f_list_count; /* count of formals lists we expect */
454 char prototyped; /* = 0 means already prototyped */
455#ifndef UNPROTOIZE
456 const f_list_chain_item * f_list_chain; /* -> chain of formals lists */
457 const def_dec_info * definition; /* -> def/dec containing related def */
458 char is_static; /* = 0 means visiblilty is "extern" */
459 char is_implicit; /* != 0 for implicit func decl's */
460 char written; /* != 0 means written for implicit */
461#else /* !defined(UNPROTOIZE) */
462 const char * formal_names; /* -> to list of names of formals */
463 const char * formal_decls; /* -> to string of formal declartions */
464#endif /* !defined(UNPROTOIZE) */
465};
466
a2b22788 467/* Pointer to the tail component of the filename by which this program was
5f8037c4
RS
468 invoked. Used everywhere in error and warning messages. */
469
470static const char *pname;
471
472/* Error counter. Will be non-zero if we should give up at the next convenient
473 stopping point. */
474
475static int errors = 0;
476
477/* Option flags. */
478/* ??? These comments should say what the flag mean as well as the options
479 that set them. */
480
ef91d7e2
RS
481/* allow the user to pass the pathname of the gcc2 compiler as a
482 command line option, instead of relying on the fact that 'gcc' in
483 the path should be gcc2 */
484
485static char *compiler_pathname = "gcc";
486
487
34e56753
RS
488static int version_flag = 0; /* Print our version number. */
489static int quiet_flag = 0; /* Don't print messages normally. */
490static int nochange_flag = 0; /* Don't convert, just say what files
491 we would have converted. */
492static int nosave_flag = 0; /* Don't save the old version. */
493static int keep_flag = 0; /* Don't delete the .X files. */
494static const char ** compile_params = 0; /* Option string for gcc. */
5f8037c4 495#ifdef UNPROTOIZE
34e56753
RS
496static const char *indent_string = " "; /* Indentation for newly
497 inserted parm decls. */
5f8037c4 498#else /* !defined(UNPROTOIZE) */
34e56753 499static int local_flag = 0; /* Insert new local decls (when?). */
5f8037c4 500static int global_flag = 0; /* set by -g option */
34e56753
RS
501static int cplusplus_flag = 0; /* Rename converted files to *.C. */
502static const char* nondefault_syscalls_dir = 0; /* Dir to look for
503 SYSCALLS.c.X in. */
5f8037c4
RS
504#endif /* !defined(UNPROTOIZE) */
505
506/* An index into the compile_params array where we should insert the filename
507 parameter when we are ready to exec the C compiler. A zero value indicates
508 that we have not yet called munge_compile_params(). */
509
510static int filename_index = 0;
511
512/* Count of command line arguments which were "filename" arguments. */
513
a2b22788 514static int n_base_source_files = 0;
5f8037c4
RS
515
516/* Points to a malloc'ed list of pointers to all of the filenames of base
517 source files which were specified on the command line. */
518
a2b22788 519static const char **base_source_filenames;
5f8037c4
RS
520
521/* Line number of the line within the current aux_info file that we
522 are currently processing. Used for error messages in case the prototypes
523 info file is corrupted somehow. */
524
525static int current_aux_info_lineno;
526
527/* Pointer to the name of the source file currently being converted. */
528
a2b22788 529static const char *convert_filename;
5f8037c4
RS
530
531/* Pointer to relative root string (taken from aux_info file) which indicates
532 where directory the user was in when he did the compilation step that
533 produced the containing aux_info file. */
534
a2b22788 535static const char *invocation_filename;
5f8037c4
RS
536
537/* Pointer to the base of the input buffer that holds the original text for the
538 source file currently being converted. */
539
540static const char *orig_text_base;
541
542/* Pointer to the byte just beyond the end of the input buffer that holds the
543 original text for the source file currently being converted. */
544
545static const char *orig_text_limit;
546
547/* Pointer to the base of the input buffer that holds the cleaned text for the
548 source file currently being converted. */
549
550static const char *clean_text_base;
551
552/* Pointer to the byte just beyond the end of the input buffer that holds the
553 cleaned text for the source file currently being converted. */
554
555static const char *clean_text_limit;
556
557/* Pointer to the last byte in the cleaned text buffer that we have already
558 (virtually) copied to the output buffer (or decided to ignore). */
559
560static const char * clean_read_ptr;
561
562/* Pointer to the base of the output buffer that holds the replacement text
563 for the source file currently being converted. */
564
565static char *repl_text_base;
566
567/* Pointer to the byte just beyond the end of the output buffer that holds the
568 replacement text for the source file currently being converted. */
569
570static char *repl_text_limit;
571
572/* Pointer to the last byte which has been stored into the output buffer.
573 The next byte to be stored should be stored just past where this points
574 to. */
575
576static char * repl_write_ptr;
577
578/* Pointer into the cleaned text buffer for the source file we are currently
579 converting. This points to the first character of the line that we last
580 did a "seek_to_line()" to (see below). */
581
582static const char *last_known_line_start;
583
584/* Number of the line (in the cleaned text buffer) that we last did a
585 "seek_to_line()" to. Will be one if we just read a new source file
586 into the cleaned text buffer. */
587
588static int last_known_line_number;
589
a2b22788 590/* The filenames hash table. */
5f8037c4 591
a2b22788 592static hash_table filename_primary;
5f8037c4
RS
593
594/* The function names hash table. */
595
596static hash_table function_name_primary;
597
598/* The place to keep the recovery address which is used only in cases where
599 we get hopelessly confused by something in the cleaned original text. */
600
601static jmp_buf source_confusion_recovery;
602
a2b22788 603/* A pointer to the current directory filename (used by abspath). */
5f8037c4
RS
604
605static char *cwd_buffer;
606
607/* A place to save the read pointer until we are sure that an individual
608 attempt at editing will succeed. */
609
610static const char * saved_clean_read_ptr;
611
612/* A place to save the write pointer until we are sure that an individual
613 attempt at editing will succeed. */
614
615static char * saved_repl_write_ptr;
616
617/* Forward declaration. */
618
34e56753 619static const char *shortpath ();
5f8037c4
RS
620\f
621/* Allocate some space, but check that the allocation was successful. */
ff57c94e 622/* alloca.c uses this, so don't make it static. */
5f8037c4 623
ff57c94e 624pointer_type
34e56753
RS
625xmalloc (byte_count)
626 size_t byte_count;
5f8037c4
RS
627{
628 pointer_type rv;
629
34e56753
RS
630 rv = malloc (byte_count);
631 if (rv == NULL)
5f8037c4
RS
632 {
633 fprintf (stderr, "\n%s: fatal error: can't allocate %u more bytes of memory\n",
a2b22788 634 pname, byte_count);
5f8037c4
RS
635 exit (1);
636 return 0; /* avoid warnings */
637 }
638 else
639 return rv;
640}
641
642/* Reallocate some space, but check that the reallocation was successful. */
643
ff57c94e 644pointer_type
34e56753
RS
645xrealloc (old_space, byte_count)
646 pointer_type old_space;
647 size_t byte_count;
5f8037c4
RS
648{
649 pointer_type rv;
650
34e56753
RS
651 rv = realloc (old_space, byte_count);
652 if (rv == NULL)
5f8037c4
RS
653 {
654 fprintf (stderr, "\n%s: fatal error: can't allocate %u more bytes of memory\n",
a2b22788 655 pname, byte_count);
5f8037c4
RS
656 exit (1);
657 return 0; /* avoid warnings */
658 }
659 else
660 return rv;
661}
662
663/* Deallocate the area pointed to by an arbitrary pointer, but first, strip
664 the `const' qualifier from it and also make sure that the pointer value
665 is non-null. */
666
ff57c94e 667void
34e56753
RS
668xfree (p)
669 const_pointer_type p;
5f8037c4
RS
670{
671 if (p)
672 free ((NONCONST pointer_type) p);
673}
674
675/* Make a copy of a string INPUT with size SIZE. */
676
677static char *
34e56753
RS
678savestring (input, size)
679 const char *input;
680 int size;
5f8037c4
RS
681{
682 char *output = (char *) xmalloc (size + 1);
683 strcpy (output, input);
684 return output;
685}
686
687/* More 'friendly' abort that prints the line and file.
688 config.h can #define abort fancy_abort if you like that sort of thing. */
689
690void
691fancy_abort ()
692{
693 fprintf (stderr, "%s: internal abort\n", pname);
694 exit (1);
695}
696\f
697/* Make a duplicate of a given string in a newly allocated area. */
698
699static char *
34e56753
RS
700dupstr (s)
701 const char *s;
5f8037c4
RS
702{
703 return strcpy ((char *) xmalloc (strlen (s) + 1), s);
704}
705
706/* Make a duplicate of the first N bytes of a given string in a newly
707 allocated area. */
708
709static char *
34e56753
RS
710dupnstr (s, n)
711 const char *s;
712 size_t n;
5f8037c4
RS
713{
714 char *ret_val = strncpy ((char *) xmalloc (n + 1), s, n);
715
716 ret_val[n] = '\0';
717 return ret_val;
718}
719
720/* Return a pointer to the first occurance of s2 within s1 or NULL if s2
721 does not occur within s1. Assume neither s1 nor s2 are null pointers. */
722
723static const char *
34e56753
RS
724substr (s1, s2)
725 const char *s1;
726 const char *const s2;
5f8037c4
RS
727{
728 for (; *s1 ; s1++)
729 {
730 const char *p1;
731 const char *p2;
a2b22788 732 int c;
5f8037c4
RS
733
734 for (p1 = s1, p2 = s2; c = *p2; p1++, p2++)
735 if (*p1 != c)
736 goto outer;
737 return s1;
738outer:
739 ;
740 }
741 return 0;
742}
743\f
744/* Get setup to recover in case the edit we are about to do goes awry. */
745
746void
34e56753 747save_pointers ()
5f8037c4
RS
748{
749 saved_clean_read_ptr = clean_read_ptr;
750 saved_repl_write_ptr = repl_write_ptr;
751}
752
753/* Call this routine to recover our previous state whenever something looks
754 too confusing in the source code we are trying to edit. */
755
756void
34e56753 757restore_pointers ()
5f8037c4
RS
758{
759 clean_read_ptr = saved_clean_read_ptr;
760 repl_write_ptr = saved_repl_write_ptr;
761}
762
763/* Return true if the given character is a legal identifier character. */
764
34e56753
RS
765static int
766is_id_char (ch)
767 char ch;
5f8037c4
RS
768{
769 return (isalnum (ch) || (ch == '_') || (ch == '$'));
770}
771
772/* Give a message indicating the proper way to invoke this program and then
773 exit with non-zero status. */
774
775static void
34e56753 776usage ()
5f8037c4
RS
777{
778#ifdef UNPROTOIZE
a2b22788
RS
779 fprintf (stderr, "%s: usage '%s [ -VqfnkN ] [ -i <istring> ] [ filename ... ]'\n",
780 pname, pname);
5f8037c4 781#else /* !defined(UNPROTOIZE) */
a2b22788
RS
782 fprintf (stderr, "%s: usage '%s [ -VqfnkNlgC ] [ -B <diname> ] [ filename ... ]'\n",
783 pname, pname);
5f8037c4 784#endif /* !defined(UNPROTOIZE) */
5f8037c4
RS
785 exit (1);
786}
787
a2b22788 788/* Return true if the given filename (assumed to be an absolute filename)
5f8037c4
RS
789 designates a file residing anywhere beneath any one of the "system"
790 include directories. */
791
792static int
34e56753
RS
793in_system_include_dir (path)
794 const char *path;
5f8037c4
RS
795{
796 struct default_include *p;
797
798 if (path[0] != '/')
a2b22788 799 abort (); /* Must be an absolutized filename. */
5f8037c4
RS
800
801 for (p = include_defaults; p->fname; p++)
802 if (!strncmp (path, p->fname, strlen (p->fname))
803 && path[strlen (p->fname)] == '/')
804 return 1;
805 return 0;
806}
807\f
808#if 0
a2b22788 809/* Return true if the given filename designates a file that the user has
5f8037c4
RS
810 read access to and for which the user has write access to the containing
811 directory. */
812
813static int
814file_could_be_converted (const char *path)
815{
816 char *const dir_name = (char *) alloca (strlen (path) + 1);
817
34e56753 818 if (my_access (path, R_OK))
5f8037c4
RS
819 return 0;
820
821 {
822 char *dir_last_slash;
823
824 strcpy (dir_name, path);
825 dir_last_slash = strrchr (dir_name, '/');
826 if (dir_last_slash)
827 *dir_last_slash = '\0';
828 else
a2b22788 829 abort (); /* Should have been an absolutized filename. */
5f8037c4
RS
830 }
831
34e56753 832 if (my_access (path, W_OK))
5f8037c4
RS
833 return 0;
834
835 return 1;
836}
837
a2b22788 838/* Return true if the given filename designates a file that we are allowed
5f8037c4
RS
839 to modify. Files which we should not attempt to modify are (a) "system"
840 include files, and (b) files which the user doesn't have write access to,
841 and (c) files which reside in directories which the user doesn't have
842 write access to. Unless requested to be quiet, give warnings about
843 files that we will not try to convert for one reason or another. An
844 exception is made for "system" include files, which we never try to
845 convert and for which we don't issue the usual warnings. */
846
847static int
848file_normally_convertable (const char *path)
849{
850 char *const dir_name = alloca (strlen (path) + 1);
851
852 if (in_system_include_dir (path))
853 return 0;
854
855 {
856 char *dir_last_slash;
857
858 strcpy (dir_name, path);
859 dir_last_slash = strrchr (dir_name, '/');
860 if (dir_last_slash)
861 *dir_last_slash = '\0';
862 else
a2b22788 863 abort (); /* Should have been an absolutized filename. */
5f8037c4
RS
864 }
865
34e56753 866 if (my_access (path, R_OK))
5f8037c4
RS
867 {
868 if (!quiet_flag)
869 fprintf (stderr, "%s: warning: no read access for file `%s'\n",
a2b22788 870 pname, shortpath (NULL, path));
5f8037c4
RS
871 return 0;
872 }
873
34e56753 874 if (my_access (path, W_OK))
5f8037c4
RS
875 {
876 if (!quiet_flag)
877 fprintf (stderr, "%s: warning: no write access for file `%s'\n",
a2b22788 878 pname, shortpath (NULL, path));
5f8037c4
RS
879 return 0;
880 }
881
34e56753 882 if (my_access (dir_name, W_OK))
5f8037c4
RS
883 {
884 if (!quiet_flag)
885 fprintf (stderr, "%s: warning: no write access for dir containing `%s'\n",
a2b22788 886 pname, shortpath (NULL, path));
5f8037c4
RS
887 return 0;
888 }
889
890 return 1;
891}
892#endif /* 0 */
893\f
894#ifndef UNPROTOIZE
895
896/* Return true if the given file_info struct refers to the special SYSCALLS.c.X
897 file. Return false otherwise. */
898
899static int
34e56753
RS
900is_syscalls_file (fi_p)
901 const file_info *fi_p;
5f8037c4
RS
902{
903 return (substr (fi_p->hash_entry->symbol, syscalls_filename) != NULL);
904}
905
906#endif /* !defined(UNPROTOIZE) */
907
908/* Check to see if this file will need to have anything done to it on this
909 run. If there is nothing in the given file which both needs conversion
910 and for which we have the necessary stuff to do the conversion, return
911 false. Otherwise, return true.
912
913 Note that (for protoize) it is only valid to call this function *after*
914 the connections between declarations and definitions have all been made
915 by connect_defs_and_decs(). */
916
917static int
34e56753
RS
918needs_to_be_converted (file_p)
919 const file_info *file_p;
5f8037c4
RS
920{
921 const def_dec_info *ddp;
922
923#ifndef UNPROTOIZE
924
925 if (is_syscalls_file (file_p))
926 return 0;
927
928#endif /* !defined(UNPROTOIZE) */
929
930 for (ddp = file_p->defs_decs; ddp; ddp = ddp->next_in_file)
931
932 if (
933
934#ifndef UNPROTOIZE
935
936 /* ... and if we a protoizing and this function is in old style ... */
937 !ddp->prototyped
938 /* ... and if this a definition or is a decl with an associated def ... */
939 && (ddp->is_func_def || (!ddp->is_func_def && ddp->definition))
940
941#else /* defined(UNPROTOIZE) */
942
943 /* ... and if we are unprotoizing and this function is in new style ... */
944 ddp->prototyped
945
946#endif /* defined(UNPROTOIZE) */
947 )
948 /* ... then the containing file needs converting. */
949 return -1;
950 return 0;
951}
952
953/* Return 1 if the file name NAME is in a directory
954 that should be converted. */
955
956static int
34e56753
RS
957directory_specified_p (name)
958 const char *name;
5f8037c4
RS
959{
960 struct string_list *p;
961
962 for (p = directory_list; p; p = p->next)
963 if (!strncmp (name, p->name, strlen (p->name))
964 && name[strlen (p->name)] == '/')
37114d0d
RS
965 {
966 const char *q = name + strlen (p->name) + 1;
967
968 /* If there are more slashes, it's in a subdir, so
969 this match doesn't count. */
970 while (*q)
971 if (*q++ == '/')
972 goto lose;
973 return 1;
974
975 lose: ;
976 }
5f8037c4
RS
977
978 return 0;
979}
980
981/* Return 1 if the file named NAME should be excluded from conversion. */
982
983static int
34e56753
RS
984file_excluded_p (name)
985 const char *name;
5f8037c4
RS
986{
987 struct string_list *p;
988 int len = strlen (name);
989
990 for (p = exclude_list; p; p = p->next)
991 if (!strcmp (name + len - strlen (p->name), p->name)
992 && name[len - strlen (p->name) - 1] == '/')
993 return 1;
994
995 return 0;
996}
997
998/* Construct a new element of a string_list.
999 STRING is the new element value, and REST holds the remaining elements. */
1000
1001static struct string_list *
34e56753
RS
1002string_list_cons (string, rest)
1003 char *string;
1004 struct string_list *rest;
5f8037c4 1005{
34e56753
RS
1006 struct string_list *temp
1007 = (struct string_list *) xmalloc (sizeof (struct string_list));
1008
5f8037c4
RS
1009 temp->next = rest;
1010 temp->name = string;
1011 return temp;
1012}
1013\f
1014/* ??? The GNU convention for mentioning function args in its comments
1015 is to capitalize them. So change "hash_tab_p" to HASH_TAB_P below.
1016 Likewise for all the other functions. */
1017
1018/* Given a hash table, apply some function to each node in the table. The
1019 table to traverse is given as the "hash_tab_p" argument, and the
1020 function to be applied to each node in the table is given as "func"
1021 argument. */
1022
1023static void
34e56753
RS
1024visit_each_hash_node (hash_tab_p, func)
1025 const hash_table_entry *hash_tab_p;
1026 void (*func)();
5f8037c4
RS
1027{
1028 const hash_table_entry *primary;
1029
1030 for (primary = hash_tab_p; primary < &hash_tab_p[HASH_TABLE_SIZE]; primary++)
1031 if (primary->symbol)
1032 {
1033 hash_table_entry *second;
1034
1035 (*func)(primary);
1036 for (second = primary->hash_next; second; second = second->hash_next)
1037 (*func) (second);
1038 }
1039}
1040
1041/* Initialize all of the fields of a new hash table entry, pointed
1042 to by the "p" parameter. Note that the space to hold the entry
1043 is assumed to have already been allocated before this routine is
1044 called. */
1045
1046static hash_table_entry *
34e56753
RS
1047add_symbol (p, s)
1048 hash_table_entry *p;
1049 const char *s;
5f8037c4
RS
1050{
1051 p->hash_next = NULL;
1052 p->symbol = dupstr (s);
1053 p->ddip = NULL;
1054 p->fip = NULL;
1055 return p;
1056}
1057
a2b22788 1058/* Look for a particular function name or filename in the particular
5f8037c4
RS
1059 hash table indicated by "hash_tab_p". If the name is not in the
1060 given hash table, add it. Either way, return a pointer to the
1061 hash table entry for the given name. */
1062
1063static hash_table_entry *
34e56753
RS
1064lookup (hash_tab_p, search_symbol)
1065 hash_table_entry *hash_tab_p;
1066 const char *search_symbol;
5f8037c4
RS
1067{
1068 int hash_value = 0;
1069 const char *search_symbol_char_p = search_symbol;
1070 hash_table_entry *p;
1071
1072 while (*search_symbol_char_p)
1073 hash_value += *search_symbol_char_p++;
1074 hash_value &= hash_mask;
1075 p = &hash_tab_p[hash_value];
1076 if (! p->symbol)
1077 return add_symbol (p, search_symbol);
1078 if (!strcmp (p->symbol, search_symbol))
1079 return p;
1080 while (p->hash_next)
1081 {
1082 p = p->hash_next;
1083 if (!strcmp (p->symbol, search_symbol))
1084 return p;
1085 }
1086 p->hash_next = (hash_table_entry *) xmalloc (sizeof (hash_table_entry));
1087 p = p->hash_next;
1088 return add_symbol (p, search_symbol);
1089}
1090\f
1091/* Throw a def/dec record on the junk heap.
1092
1093 Also, since we are not using this record anymore, free up all of the
1094 stuff it pointed to. */
1095
34e56753
RS
1096static void
1097free_def_dec (p)
1098 def_dec_info *p;
5f8037c4
RS
1099{
1100 xfree (p->ansi_decl);
1101
1102#ifndef UNPROTOIZE
1103 {
1104 const f_list_chain_item * curr;
1105 const f_list_chain_item * next;
1106
1107 for (curr = p->f_list_chain; curr; curr = next)
1108 {
1109 next = curr->chain_next;
1110 xfree (curr);
1111 }
1112 }
1113#endif /* !defined(UNPROTOIZE) */
1114
1115 xfree (p);
1116}
1117
1118/* Unexpand as many macro symbol as we can find.
1119
1120 If the given line must be unexpanded, make a copy of it in the heap and
1121 return a pointer to the unexpanded copy. Otherwise return NULL. */
1122
1123static char *
34e56753
RS
1124unexpand_if_needed (aux_info_line)
1125 const char *aux_info_line;
5f8037c4
RS
1126{
1127 static char *line_buf = 0;
1128 static int line_buf_size = 0;
1129 const unexpansion* unexp_p;
1130 int got_unexpanded = 0;
1131 const char *s;
1132 char *copy_p = line_buf;
1133
1134 if (line_buf == 0)
1135 {
1136 line_buf_size = 1024;
1137 line_buf = (char *) xmalloc (line_buf_size);
1138 }
1139
1140 copy_p = line_buf;
1141
1142 /* Make a copy of the input string in line_buf, expanding as necessary. */
1143
1144 for (s = aux_info_line; *s != '\n'; )
1145 {
1146 for (unexp_p = unexpansions; unexp_p->expanded; unexp_p++)
1147 {
1148 const char *in_p = unexp_p->expanded;
1149 size_t len = strlen (in_p);
1150
1151 if (*s == *in_p && !strncmp (s, in_p, len) && !is_id_char (s[len]))
1152 {
1153 int size = strlen (unexp_p->contracted);
1154 got_unexpanded = 1;
1155 if (copy_p + size - line_buf >= line_buf_size)
1156 {
1157 int offset = copy_p - line_buf;
1158 line_buf_size *= 2;
1159 line_buf_size += size;
1160 line_buf = (char *) xrealloc (line_buf, line_buf_size);
1161 copy_p = line_buf + offset;
1162 }
1163 strcpy (copy_p, unexp_p->contracted);
1164 copy_p += size;
1165
1166 /* Assume the there will not be another replacement required
1167 within the text just replaced. */
1168
1169 s += len;
1170 goto continue_outer;
1171 }
1172 }
1173 if (copy_p - line_buf == line_buf_size)
1174 {
1175 int offset = copy_p - line_buf;
1176 line_buf_size *= 2;
1177 line_buf = (char *) xrealloc (line_buf, line_buf_size);
1178 copy_p = line_buf + offset;
1179 }
1180 *copy_p++ = *s++;
1181continue_outer: ;
1182 }
1183 if (copy_p + 2 - line_buf >= line_buf_size)
1184 {
1185 int offset = copy_p - line_buf;
1186 line_buf_size *= 2;
1187 line_buf = (char *) xrealloc (line_buf, line_buf_size);
1188 copy_p = line_buf + offset;
1189 }
1190 *copy_p++ = '\n';
1191 *copy_p++ = '\0';
1192
1193 return (got_unexpanded ? dupstr (line_buf) : 0);
1194}
1195\f
a2b22788
RS
1196/* Return the absolutized filename for the given relative
1197 filename. Note that if that filename is already absolute, it may
5f8037c4
RS
1198 still be returned in a modified form because this routine also
1199 eliminates redundant slashes and single dots and eliminates double
a2b22788
RS
1200 dots to get a shortest possible filename from the given input
1201 filename. The absolutization of relative filenames is made by
1202 assuming that the given filename is to be taken as relative to
5f8037c4
RS
1203 the first argument (cwd) or to the current directory if cwd is
1204 NULL. */
1205
1206static char *
34e56753
RS
1207abspath (cwd, rel_filename)
1208 const char *cwd;
1209 const char *rel_filename;
5f8037c4
RS
1210{
1211 /* Setup the current working directory as needed. */
1212 const char *cwd2 = (cwd) ? cwd : cwd_buffer;
1213 char *const abs_buffer
a2b22788 1214 = (char *) alloca (strlen (cwd2) + strlen (rel_filename) + 1);
5f8037c4
RS
1215 char *endp = abs_buffer;
1216 char *outp, *inp;
1217
a2b22788 1218 /* Copy the filename (possibly preceeded by the current working
5f8037c4
RS
1219 directory name) into the absolutization buffer. */
1220
1221 {
1222 const char *src_p;
1223
a2b22788 1224 if (rel_filename[0] != '/')
5f8037c4
RS
1225 {
1226 src_p = cwd2;
1227 while (*endp++ = *src_p++)
1228 continue;
1229 *(endp-1) = '/'; /* overwrite null */
1230 }
a2b22788 1231 src_p = rel_filename;
5f8037c4
RS
1232 while (*endp++ = *src_p++)
1233 continue;
1234 if (endp[-1] == '/')
1235 *endp = '\0';
1236 }
1237
1238 /* Now make a copy of abs_buffer into abs_buffer, shortening the
a2b22788 1239 filename (by taking out slashes and dots) as we go. */
5f8037c4
RS
1240
1241 outp = inp = abs_buffer;
1242 *outp++ = *inp++; /* copy first slash */
1243 for (;;)
1244 {
1245 if (!inp[0])
1246 break;
1247 else if (inp[0] == '/' && outp[-1] == '/')
1248 {
1249 inp++;
1250 continue;
1251 }
1252 else if (inp[0] == '.' && outp[-1] == '/')
1253 {
1254 if (!inp[1])
1255 break;
1256 else if (inp[1] == '/')
1257 {
1258 inp += 2;
1259 continue;
1260 }
1261 else if ((inp[1] == '.') && (inp[2] == 0 || inp[2] == '/'))
1262 {
1263 inp += (inp[2] == '/') ? 3 : 2;
1264 outp -= 2;
1265 while (outp >= abs_buffer && *outp != '/')
1266 outp--;
1267 if (outp < abs_buffer)
1268 {
1269 /* Catch cases like /.. where we try to backup to a
1270 point above the absolute root of the logical file
1271 system. */
1272
a2b22788
RS
1273 fprintf (stderr, "%s: invalid file name: %s\n",
1274 pname, rel_filename);
5f8037c4
RS
1275 exit (1);
1276 }
1277 *++outp = '\0';
1278 continue;
1279 }
1280 }
1281 *outp++ = *inp++;
1282 }
1283
1284 /* On exit, make sure that there is a trailing null, and make sure that
1285 the last character of the returned string is *not* a slash. */
1286
1287 *outp = '\0';
1288 if (outp[-1] == '/')
1289 *--outp = '\0';
1290
1291 /* Make a copy (in the heap) of the stuff left in the absolutization
1292 buffer and return a pointer to the copy. */
1293
1294 return dupstr (abs_buffer);
1295}
1296\f
a2b22788 1297/* Given a filename (and possibly a directory name from which the filename
5f8037c4 1298 is relative) return a string which is the shortest possible
a2b22788 1299 equivalent for the corresponding full (absolutized) filename. The
5f8037c4 1300 shortest possible equivalent may be constructed by converting the
a2b22788
RS
1301 absolutized filename to be a relative filename (i.e. relative to
1302 the actual current working directory). However if a relative filename
1303 is longer, then the full absolute filename is returned.
5f8037c4
RS
1304
1305 KNOWN BUG:
1306
a2b22788
RS
1307 Note that "simple-minded" conversion of any given type of filename (either
1308 relative or absolute) may not result in a valid equivalent filename if any
1309 subpart of the original filename is actually a symbolic link. */
5f8037c4
RS
1310
1311static const char *
34e56753
RS
1312shortpath (cwd, filename)
1313 const char *cwd;
1314 const char *filename;
5f8037c4
RS
1315{
1316 char *rel_buffer;
1317 char *rel_buf_p;
1318 char *cwd_p = cwd_buffer;
1319 char *path_p;
1320 int unmatched_slash_count = 0;
1321
a2b22788 1322 path_p = abspath (cwd, filename);
5f8037c4
RS
1323 rel_buf_p = rel_buffer = (char *) xmalloc (strlen (path_p) + 1);
1324
1325 while (*cwd_p && (*cwd_p == *path_p))
1326 {
1327 cwd_p++;
1328 path_p++;
1329 }
1330 if (!*cwd_p) /* whole pwd matched */
1331 {
1332 if (!*path_p) /* input *is* the current path! */
1333 return ".";
1334 else
1335 return ++path_p;
1336 }
1337 else
1338 {
1339 if (*path_p)
1340 {
1341 --cwd_p;
1342 --path_p;
1343 while (*cwd_p != '/') /* backup to last slash */
1344 {
1345 --cwd_p;
1346 --path_p;
1347 }
1348 cwd_p++;
1349 path_p++;
1350 unmatched_slash_count++;
1351 }
1352 while (*cwd_p)
1353 if (*cwd_p++ == '/')
1354 unmatched_slash_count++;
1355 while (unmatched_slash_count--)
1356 {
1357 *rel_buf_p++ = '.';
1358 *rel_buf_p++ = '.';
1359 *rel_buf_p++ = '/';
1360 }
1361 while (*rel_buf_p++ = *path_p++)
1362 continue;
1363 --rel_buf_p;
1364 if (*(rel_buf_p-1) == '/')
1365 *--rel_buf_p = '\0';
34e56753 1366 if (strlen (rel_buffer) > (unsigned) strlen (filename))
a2b22788 1367 strcpy (rel_buffer, filename);
5f8037c4
RS
1368 return rel_buffer;
1369 }
5f8037c4
RS
1370}
1371\f
a2b22788 1372/* Lookup the given filename in the hash table for filenames. If it is a
5f8037c4 1373 new one, then the hash table info pointer will be null. In this case,
a2b22788 1374 we create a new file_info record to go with the filename, and we initialize
5f8037c4
RS
1375 that record with some reasonable values. */
1376
1377static file_info *
34e56753
RS
1378find_file (filename, do_not_stat)
1379 char *filename;
1380 int do_not_stat;
a2b22788
RS
1381/* FILENAME was const, but that causes a warning on AIX when calling stat.
1382 That is probably a bug in AIX, but might as well avoid the warning. */
5f8037c4
RS
1383{
1384 hash_table_entry *hash_entry_p;
1385
a2b22788 1386 hash_entry_p = lookup (filename_primary, filename);
5f8037c4
RS
1387 if (hash_entry_p->fip)
1388 return hash_entry_p->fip;
1389 else
1390 {
1391 struct stat stat_buf;
1392 file_info *file_p = (file_info *) xmalloc (sizeof (file_info));
1393
1394 /* If we cannot get status on any given source file, give a warning
1395 and then just set its time of last modification to infinity. */
1396
1397 if (do_not_stat)
1398 stat_buf.st_mtime = (time_t) 0;
1399 else
1400 {
34e56753 1401 if (my_stat (filename, &stat_buf) == -1)
5f8037c4
RS
1402 {
1403 fprintf (stderr, "%s: error: can't get status of `%s': %s\n",
a2b22788 1404 pname, shortpath (NULL, filename), sys_errlist[errno]);
5f8037c4
RS
1405 stat_buf.st_mtime = (time_t) -1;
1406 }
1407 }
1408
1409 hash_entry_p->fip = file_p;
1410 file_p->hash_entry = hash_entry_p;
1411 file_p->defs_decs = NULL;
1412 file_p->mtime = stat_buf.st_mtime;
1413 return file_p;
1414 }
1415}
1416
1417/* Generate a fatal error because some part of the aux_info file is
1418 messed up. */
1419
1420static void
34e56753 1421aux_info_corrupted ()
5f8037c4
RS
1422{
1423 fprintf (stderr, "\n%s: fatal error: aux info file corrupted at line %d\n",
a2b22788 1424 pname, current_aux_info_lineno);
5f8037c4
RS
1425 exit (1);
1426}
1427
1428/* ??? This comment is vague. Say what the condition is for. */
1429/* Check to see that a condition is true. This is kind of like an assert(). */
1430
34e56753
RS
1431static void
1432check_aux_info (cond)
1433 int cond;
5f8037c4
RS
1434{
1435 if (! cond)
1436 aux_info_corrupted ();
1437}
1438
1439/* Given a pointer to the closing right parenthesis for a particular formals
1440 list (in a aux_info file) find the corresponding left parenthesis and
1441 return a pointer to it. */
1442
1443static const char *
34e56753
RS
1444find_corresponding_lparen (p)
1445 const char *p;
5f8037c4
RS
1446{
1447 const char *q;
1448 int paren_depth;
1449
1450 for (paren_depth = 1, q = p-1; paren_depth; q--)
1451 {
1452 switch (*q)
1453 {
1454 case ')':
1455 paren_depth++;
1456 break;
1457 case '(':
1458 paren_depth--;
1459 break;
1460 }
1461 }
1462 return ++q;
1463}
1464\f
1465/* Given a line from an aux info file, and a time at which the aux info
1466 file it came from was created, check to see if the item described in
1467 the line comes from a file which has been modified since the aux info
1468 file was created. If so, return non-zero, else return zero. */
1469
1470static int
34e56753
RS
1471referenced_file_is_newer (l, aux_info_mtime)
1472 const char *l;
1473 time_t aux_info_mtime;
5f8037c4
RS
1474{
1475 const char *p;
1476 file_info *fi_p;
1477 char *filename;
1478
1479 check_aux_info (l[0] == '/');
1480 check_aux_info (l[1] == '*');
1481 check_aux_info (l[2] == ' ');
1482
1483 {
1484 const char *filename_start = p = l + 3;
1485
1486 while (*p != ':')
1487 p++;
1488 filename = (char *) alloca ((size_t) (p - filename_start) + 1);
1489 strncpy (filename, filename_start, (size_t) (p - filename_start));
1490 filename[p-filename_start] = '\0';
1491 }
1492
1493 /* Call find_file to find the file_info record associated with the file
1494 which contained this particular def or dec item. Note that this call
1495 may cause a new file_info record to be created if this is the first time
1496 that we have ever known about this particular file. */
1497
a2b22788 1498 fi_p = find_file (abspath (invocation_filename, filename), 0);
5f8037c4
RS
1499
1500 return (fi_p->mtime > aux_info_mtime);
1501}
1502\f
1503/* Given a line of info from the aux_info file, create a new
1504 def_dec_info record to remember all of the important information about
1505 a function definition or declaration.
1506
1507 Link this record onto the list of such records for the particular file in
1508 which it occured in proper (descending) line number order (for now).
1509
1510 If there is an identical record already on the list for the file, throw
1511 this one away. Doing so takes care of the (useless and troublesome)
1512 duplicates which are bound to crop up due to multiple inclusions of any
1513 given individual header file.
1514
1515 Finally, link the new def_dec record onto the list of such records
1516 pertaining to this particular function name. */
1517
1518static void
34e56753
RS
1519save_def_or_dec (l, is_syscalls)
1520 const char *l;
1521 int is_syscalls;
5f8037c4
RS
1522{
1523 const char *p;
1524 const char *semicolon_p;
1525 def_dec_info *def_dec_p = (def_dec_info *) xmalloc (sizeof (def_dec_info));
1526
1527#ifndef UNPROTOIZE
1528 def_dec_p->written = 0;
1529#endif /* !defined(UNPROTOIZE) */
1530
1531 /* Start processing the line by picking off 5 pieces of information from
1532 the left hand end of the line. These are filename, line number,
1533 new/old/implicit flag (new = ANSI prototype format), definition or
1534 declaration flag, and extern/static flag). */
1535
1536 check_aux_info (l[0] == '/');
1537 check_aux_info (l[1] == '*');
1538 check_aux_info (l[2] == ' ');
1539
1540 {
1541 const char *filename_start = p = l + 3;
1542 char *filename;
1543
1544 while (*p != ':')
1545 p++;
1546 filename = (char *) alloca ((size_t) (p - filename_start) + 1);
1547 strncpy (filename, filename_start, (size_t) (p - filename_start));
1548 filename[p-filename_start] = '\0';
1549
1550 /* Call find_file to find the file_info record associated with the file
1551 which contained this particular def or dec item. Note that this call
1552 may cause a new file_info record to be created if this is the first time
1553 that we have ever known about this particular file.
1554
a2b22788 1555 Note that we started out by forcing all of the base source file names
5f8037c4 1556 (i.e. the names of the aux_info files with the .X stripped off) into the
a2b22788
RS
1557 filenames hash table, and we simultaneously setup file_info records for
1558 all of these base file names (even if they may be useless later).
1559 The file_info records for all of these "base" file names (properly)
5f8037c4
RS
1560 act as file_info records for the "original" (i.e. un-included) files
1561 which were submitted to gcc for compilation (when the -fgen-aux-info
1562 option was used). */
1563
a2b22788 1564 def_dec_p->file = find_file (abspath (invocation_filename, filename), is_syscalls);
5f8037c4
RS
1565 }
1566
1567 {
1568 const char *line_number_start = ++p;
1569 char line_number[10];
1570
1571 while (*p != ':')
1572 p++;
1573 strncpy (line_number, line_number_start, (size_t) (p - line_number_start));
1574 line_number[p-line_number_start] = '\0';
1575 def_dec_p->line = atoi (line_number);
1576 }
1577
1578 /* Check that this record describes a new-style, old-style, or implicit
1579 definition or declaration. */
1580
1581 p++; /* Skip over the `:'. */
1582 check_aux_info ((*p == 'N') || (*p == 'O') || (*p == 'I'));
1583
1584 /* Is this a new style (ANSI prototyped) definition or declaration? */
1585
1586 def_dec_p->prototyped = (*p == 'N');
1587
1588#ifndef UNPROTOIZE
1589
1590 /* Is this an implicit declaration? */
1591
1592 def_dec_p->is_implicit = (*p == 'I');
1593
1594#endif /* !defined(UNPROTOIZE) */
1595
1596 p++;
1597
1598 check_aux_info ((*p == 'C') || (*p == 'F'));
1599
1600 /* Is this item a function definition (F) or a declaration (C). Note that
1601 we treat item taken from the syscalls file as though they were function
1602 definitions regardless of what the stuff in the file says. */
1603
1604 def_dec_p->is_func_def = ((*p++ == 'F') || is_syscalls);
1605
1606#ifndef UNPROTOIZE
1607 def_dec_p->definition = 0; /* Fill this in later if protoizing. */
1608#endif /* !defined(UNPROTOIZE) */
1609
1610 check_aux_info (*p++ == ' ');
1611 check_aux_info (*p++ == '*');
1612 check_aux_info (*p++ == '/');
1613 check_aux_info (*p++ == ' ');
1614
1615#ifdef UNPROTOIZE
1616 check_aux_info ((!strncmp (p, "static", 6)) || (!strncmp (p, "extern", 6)));
1617#else /* !defined(UNPROTOIZE) */
1618 if (!strncmp (p, "static", 6))
1619 def_dec_p->is_static = -1;
1620 else if (!strncmp (p, "extern", 6))
1621 def_dec_p->is_static = 0;
1622 else
1623 check_aux_info (0); /* Didn't find either `extern' or `static'. */
1624#endif /* !defined(UNPROTOIZE) */
1625
1626 {
1627 const char *ansi_start = p;
1628
1629 p += 6; /* Pass over the "static" or "extern". */
1630
1631 /* We are now past the initial stuff. Search forward from here to find
1632 the terminating semicolon that should immediately follow the entire
1633 ANSI format function declaration. */
1634
1635 while (*++p != ';')
1636 continue;
1637
1638 semicolon_p = p;
1639
1640 /* Make a copy of the ansi declaration part of the line from the aux_info
1641 file. */
1642
1643 def_dec_p->ansi_decl
1644 = dupnstr (ansi_start, (size_t) ((semicolon_p+1) - ansi_start));
1645 }
1646
1647 /* Backup and point at the final right paren of the final argument list. */
1648
1649 p--;
1650
1651 /* Now isolate a whole set of formal argument lists, one-by-one. Normally,
1652 there will only be one list to isolate, but there could be more. */
1653
1654 def_dec_p->f_list_count = 0;
1655
1656#ifndef UNPROTOIZE
1657 def_dec_p->f_list_chain = NULL;
1658#endif /* !defined(UNPROTOIZE) */
1659
1660 for (;;)
1661 {
1662 const char *left_paren_p = find_corresponding_lparen (p);
1663#ifndef UNPROTOIZE
1664 {
1665 f_list_chain_item *cip =
1666 (f_list_chain_item *) xmalloc (sizeof (f_list_chain_item));
1667
1668 cip->formals_list
1669 = dupnstr (left_paren_p + 1, (size_t) (p - (left_paren_p+1)));
1670
1671 /* Add the new chain item at the head of the current list. */
1672
1673 cip->chain_next = def_dec_p->f_list_chain;
1674 def_dec_p->f_list_chain = cip;
1675 }
1676#endif /* !defined(UNPROTOIZE) */
1677 def_dec_p->f_list_count++;
1678
1679 p = left_paren_p - 2;
1680
1681 /* p must now point either to another right paren, or to the last
1682 character of the name of the function that was declared/defined.
1683 If p points to another right paren, then this indicates that we
1684 are dealing with multiple formals lists. In that case, there
1685 really should be another right paren preceeding this right paren. */
1686
1687 if (*p != ')')
1688 break;
1689 else
1690 check_aux_info (*--p == ')');
1691 }
1692
1693
1694 {
1695 const char *past_fn = p + 1;
1696
1697 check_aux_info (*past_fn == ' ');
1698
1699 /* Scan leftwards over the identifier that names the function. */
1700
1701 while (is_id_char (*p))
1702 p--;
1703 p++;
1704
1705 /* p now points to the leftmost character of the function name. */
1706
1707 {
34e56753 1708 char *fn_string = (char *) alloca (past_fn - p + 1);
5f8037c4
RS
1709
1710 strncpy (fn_string, p, (size_t) (past_fn - p));
1711 fn_string[past_fn-p] = '\0';
1712 def_dec_p->hash_entry = lookup (function_name_primary, fn_string);
1713 }
1714 }
1715
1716 /* Look at all of the defs and decs for this function name that we have
1717 collected so far. If there is already one which is at the same
1718 line number in the same file, then we can discard this new def_dec_info
1719 record.
1720
1721 As an extra assurance that any such pair of (nominally) identical
1722 function declarations are in fact identical, we also compare the
1723 ansi_decl parts of the lines from the aux_info files just to be on
1724 the safe side.
1725
1726 This comparison will fail if (for instance) the user was playing
1727 messy games with the preprocessor which ultimately causes one
1728 function declaration in one header file to look differently when
1729 that file is included by two (or more) other files. */
1730
1731 {
1732 const def_dec_info *other;
1733
1734 for (other = def_dec_p->hash_entry->ddip; other; other = other->next_for_func)
1735 {
1736 if (def_dec_p->line == other->line && def_dec_p->file == other->file)
1737 {
1738 if (strcmp (def_dec_p->ansi_decl, other->ansi_decl))
1739 {
1740 fprintf (stderr, "%s: error: declaration of function `%s' at %s(%d) takes different forms\n",
1741 pname,
1742 def_dec_p->hash_entry->symbol,
1743 def_dec_p->file->hash_entry->symbol,
1744 def_dec_p->line);
1745 exit (1);
1746 }
1747 free_def_dec (def_dec_p);
1748 return;
1749 }
1750 }
1751 }
1752
1753#ifdef UNPROTOIZE
1754
1755 /* If we are doing unprotoizing, we must now setup the pointers that will
1756 point to the K&R name list and to the K&R argument declarations list.
1757
1758 Note that if this is only a function declaration, then we should not
1759 expect to find any K&R style formals list following the ANSI-style
1760 formals list. This is because GCC knows that such information is
1761 useless in the case of function declarations (function definitions
1762 are a different story however).
1763
1764 Since we are unprotoizing, we don't need any such lists anyway.
1765 All we plan to do is to delete all characters between ()'s in any
1766 case. */
1767
1768 def_dec_p->formal_names = NULL;
1769 def_dec_p->formal_decls = NULL;
1770
1771 if (def_dec_p->is_func_def)
1772 {
1773 p = semicolon_p;
1774 check_aux_info (*++p == ' ');
1775 check_aux_info (*++p == '/');
1776 check_aux_info (*++p == '*');
1777 check_aux_info (*++p == ' ');
1778 check_aux_info (*++p == '(');
1779
1780 {
1781 const char *kr_names_start = ++p; /* Point just inside '('. */
1782
1783 while (*p++ != ')')
1784 continue;
1785 p--; /* point to closing right paren */
1786
1787 /* Make a copy of the K&R parameter names list. */
1788
1789 def_dec_p->formal_names
1790 = dupnstr (kr_names_start, (size_t) (p - kr_names_start));
1791 }
1792
1793 check_aux_info (*++p == ' ');
1794 p++;
1795
1796 /* p now points to the first character of the K&R style declarations
1797 list (if there is one) or to the star-slash combination that ends
1798 the comment in which such lists get embedded. */
1799
1800 /* Make a copy of the K&R formal decls list and set the def_dec record
1801 to point to it. */
1802
1803 if (*p == '*') /* Are there no K&R declarations? */
1804 {
1805 check_aux_info (*++p == '/');
1806 def_dec_p->formal_decls = "";
1807 }
1808 else
1809 {
1810 const char *kr_decls_start = p;
1811
1812 while (p[0] != '*' || p[1] != '/')
1813 p++;
1814 p--;
1815
1816 check_aux_info (*p == ' ');
1817
1818 def_dec_p->formal_decls
1819 = dupnstr (kr_decls_start, (size_t) (p - kr_decls_start));
1820 }
1821
1822 /* Handle a special case. If we have a function definition marked as
1823 being in "old" style, and if it's formal names list is empty, then
1824 it may actually have the string "void" in its real formals list
1825 in the original source code. Just to make sure, we will get setup
1826 to convert such things anyway.
1827
1828 This kludge only needs to be here because of an insurmountable
1829 problem with generating .X files. */
1830
1831 if (!def_dec_p->prototyped && !*def_dec_p->formal_names)
1832 def_dec_p->prototyped = 1;
1833 }
1834
1835 /* Since we are unprotoizing, if this item is already in old (K&R) style,
1836 we can just ignore it. If that is true, throw away the itme now. */
1837
1838 if (!def_dec_p->prototyped)
1839 {
1840 free_def_dec (def_dec_p);
1841 return;
1842 }
1843
1844#endif /* defined(UNPROTOIZE) */
1845
1846 /* Add this record to the head of the list of records pertaining to this
1847 particular function name. */
1848
1849 def_dec_p->next_for_func = def_dec_p->hash_entry->ddip;
1850 def_dec_p->hash_entry->ddip = def_dec_p;
1851
1852 /* Add this new def_dec_info record to the sorted list of def_dec_info
1853 records for this file. Note that we don't have to worry about duplicates
1854 (caused by multiple inclusions of header files) here because we have
1855 already eliminated duplicates above. */
1856
1857 if (!def_dec_p->file->defs_decs)
1858 {
1859 def_dec_p->file->defs_decs = def_dec_p;
1860 def_dec_p->next_in_file = NULL;
1861 }
1862 else
1863 {
1864 int line = def_dec_p->line;
1865 const def_dec_info *prev = NULL;
1866 const def_dec_info *curr = def_dec_p->file->defs_decs;
1867 const def_dec_info *next = curr->next_in_file;
1868
1869 while (next && (line < curr->line))
1870 {
1871 prev = curr;
1872 curr = next;
1873 next = next->next_in_file;
1874 }
1875 if (line >= curr->line)
1876 {
1877 def_dec_p->next_in_file = curr;
1878 if (prev)
1879 ((NONCONST def_dec_info *) prev)->next_in_file = def_dec_p;
1880 else
1881 def_dec_p->file->defs_decs = def_dec_p;
1882 }
1883 else /* assert (next == NULL); */
1884 {
1885 ((NONCONST def_dec_info *) curr)->next_in_file = def_dec_p;
1886 /* assert (next == NULL); */
1887 def_dec_p->next_in_file = next;
1888 }
1889 }
1890}
1891\f
1892/* Rewrite the options list used to recompile base source files. All we are
1893 really doing here is removing -g, -O, -S, -c, and -o options, and then
1894 adding a final group of options like '-fgen-aux-info -S -o /dev/null'. */
1895
1896static void
34e56753
RS
1897munge_compile_params (params_list)
1898 const char *params_list;
5f8037c4 1899{
34e56753 1900 char **temp_params = (char **) alloca (strlen (params_list) + 10);
5f8037c4
RS
1901 int param_count = 0;
1902 const char *param;
1903
ef91d7e2 1904 temp_params[param_count++] = compiler_pathname;
5f8037c4
RS
1905 for (;;)
1906 {
1907 while (isspace (*params_list))
1908 params_list++;
1909 if (!*params_list)
1910 break;
1911 param = params_list;
1912 while (*params_list && !isspace (*params_list))
1913 params_list++;
1914 if (param[0] != '-')
1915 temp_params[param_count++]
1916 = dupnstr (param, (size_t) (params_list - param));
1917 else
1918 {
1919 switch (param[1])
1920 {
1921 case 'g':
1922 case 'O':
1923 case 'S':
1924 case 'c':
1925 break; /* Don't copy these. */
1926 case 'o':
1927 while (isspace (*params_list))
1928 params_list++;
1929 while (*params_list && !isspace (*params_list))
1930 params_list++;
1931 break;
1932 default:
1933 temp_params[param_count++]
1934 = dupnstr (param, (size_t) (params_list - param));
1935 }
1936 }
1937 if (!*params_list)
1938 break;
1939 }
1940 temp_params[param_count++] = "-fgen-aux-info";
1941 temp_params[param_count++] = "-S";
1942 temp_params[param_count++] = "-o";
1943 temp_params[param_count++] = "/dev/null";
1944
1945 /* Leave room for the filename argument and a terminating null pointer. */
1946
1947 temp_params[filename_index = param_count++] = NULL;
1948 temp_params[param_count++] = NULL;
1949
1950 /* Make a copy of the compile_params in heap space. */
1951
34e56753 1952 compile_params
ff57c94e 1953 = (const char **) xmalloc (sizeof (char *) * (param_count+1));
5f8037c4
RS
1954 memcpy (compile_params, temp_params, sizeof (char *) * param_count);
1955}
1956
1957/* Do a recompilation for the express purpose of generating a new aux_info
1958 file to go with a specific base source file. */
1959
1960static int
34e56753
RS
1961gen_aux_info_file (base_filename)
1962 const char *base_filename;
5f8037c4
RS
1963{
1964 int child_pid;
1965
1966 if (!filename_index)
1967 munge_compile_params ("");
1968
a2b22788 1969 compile_params[filename_index] = shortpath (NULL, base_filename);
5f8037c4
RS
1970
1971 if (!quiet_flag)
1972 fprintf (stderr, "%s: compiling `%s'\n",
a2b22788 1973 pname, compile_params[filename_index]);
5f8037c4
RS
1974
1975 if (child_pid = fork ())
1976 {
1977 if (child_pid == -1)
1978 {
1979 fprintf (stderr, "%s: error: could not fork process: %s\n",
a2b22788 1980 pname, sys_errlist[errno]);
5f8037c4
RS
1981 return 0;
1982 }
1983
1984#if 0
1985 /* Print out the command line that the other process is now executing. */
1986
1987 if (!quiet_flag)
1988 {
1989 const char **arg;
1990
1991 fputs ("\t", stderr);
1992 for (arg = compile_params; *arg; arg++)
1993 {
1994 fputs (*arg, stderr);
1995 fputc (' ', stderr);
1996 }
1997 fputc ('\n', stderr);
1998 fflush (stderr);
1999 }
2000#endif /* 0 */
2001
2002 {
2003 int wait_status;
2004
2005 if (wait (&wait_status) == -1)
2006 {
2007 fprintf (stderr, "%s: error: wait for process failed: %s\n",
a2b22788 2008 pname, sys_errlist[errno]);
5f8037c4
RS
2009 return 0;
2010 }
2011 if (!WIFEXITED (wait_status))
2012 {
2013 kill (child_pid, 9);
2014 return 0;
2015 }
2016 return (WEXITSTATUS (wait_status) == 0) ? 1 : 0;
2017 }
2018 }
2019 else
2020 {
34e56753 2021 if (my_execvp (compile_params[0], (char *const *) compile_params))
5f8037c4
RS
2022 {
2023 fprintf (stderr, "%s: error: execvp returned: %s\n",
a2b22788 2024 pname, sys_errlist[errno]);
5f8037c4
RS
2025 exit (errno);
2026 }
2027 return 1; /* Never executed. */
2028 }
2029}
2030\f
2031/* Read in all of the information contained in a single aux_info file.
2032 Save all of the important stuff for later. */
2033
2034static void
34e56753
RS
2035process_aux_info_file (base_source_filename, keep_it, is_syscalls)
2036 const char *base_source_filename;
2037 int keep_it;
2038 int is_syscalls;
5f8037c4 2039{
a2b22788
RS
2040 char *const aux_info_filename
2041 = (char *) alloca (strlen (base_source_filename)
5f8037c4
RS
2042 + strlen (aux_info_suffix) + 1);
2043 char *aux_info_base;
2044 char *aux_info_limit;
2045 const char *aux_info_second_line;
2046 time_t aux_info_mtime;
2047 size_t aux_info_size;
2048
a2b22788 2049 /* Construct the aux_info filename from the base source filename. */
5f8037c4 2050
a2b22788
RS
2051 strcpy (aux_info_filename, base_source_filename);
2052 strcat (aux_info_filename, aux_info_suffix);
5f8037c4
RS
2053
2054 /* Check that the aux_info file exists and is readable. If it does not
2055 exist, try to create it (once only). */
2056
2057start_over: ;
2058
2059 {
2060 int retries = 0;
2061
2062retry:
34e56753 2063 if (my_access (aux_info_filename, R_OK) == -1)
5f8037c4
RS
2064 {
2065 if (errno == ENOENT && retries == 0)
2066 {
2067 if (is_syscalls)
2068 {
2069 fprintf (stderr, "%s: warning: missing SYSCALLS file `%s'\n",
a2b22788 2070 pname, aux_info_filename);
5f8037c4
RS
2071 return;
2072 }
a2b22788 2073 if (!gen_aux_info_file (base_source_filename))
5f8037c4
RS
2074 return;
2075 retries++;
2076 goto retry;
2077 }
2078 else
2079 {
2080 fprintf (stderr, "%s: error: can't read aux info file `%s': %s\n",
a2b22788
RS
2081 pname, shortpath (NULL, aux_info_filename),
2082 sys_errlist[errno]);
5f8037c4
RS
2083 errors++;
2084 return;
2085 }
2086 }
2087 }
2088
2089 {
2090 struct stat stat_buf;
2091
2092 /* Get some status information about this aux_info file. */
2093
34e56753 2094 if (my_stat (aux_info_filename, &stat_buf) == -1)
5f8037c4
RS
2095 {
2096 fprintf (stderr, "%s: error: can't get status of aux info file `%s': %s\n",
a2b22788
RS
2097 pname, shortpath (NULL, aux_info_filename),
2098 sys_errlist[errno]);
5f8037c4
RS
2099 errors++;
2100 return;
2101 }
2102
2103 /* Check on whether or not this aux_info file is zero length. If it is,
2104 then just ignore it and return. */
2105
2106 if ((aux_info_size = stat_buf.st_size) == 0)
2107 return;
2108
2109 /* Get the date/time of last modification for this aux_info file and
2110 remember it. We will have to check that any source files that it
2111 contains information about are at least this old or older. */
2112
2113 aux_info_mtime = stat_buf.st_mtime;
2114 }
2115
2116 {
2117 int aux_info_file;
2118
2119 /* Open the aux_info file. */
2120
34e56753 2121 if ((aux_info_file = my_open (aux_info_filename, O_RDONLY, 0444 )) == -1)
5f8037c4
RS
2122 {
2123 fprintf (stderr, "%s: error: can't open aux info file `%s' for reading: %s\n",
a2b22788
RS
2124 pname, shortpath (NULL, aux_info_filename),
2125 sys_errlist[errno]);
5f8037c4
RS
2126 return;
2127 }
2128
2129 /* Allocate space to hold the aux_info file in memory. */
2130
2131 aux_info_base = xmalloc (aux_info_size + 1);
2132 aux_info_limit = aux_info_base + aux_info_size;
2133 *aux_info_limit = '\0';
2134
2135 /* Read the aux_info file into memory. */
2136
2137 if (read (aux_info_file, aux_info_base, aux_info_size) != aux_info_size)
2138 {
2139 fprintf (stderr, "%s: error: while reading aux info file `%s': %s\n",
a2b22788
RS
2140 pname, shortpath (NULL, aux_info_filename),
2141 sys_errlist[errno]);
5f8037c4
RS
2142 free (aux_info_base);
2143 close (aux_info_file);
2144 return;
2145 }
2146
2147 /* Close the aux info file. */
2148
2149 if (close (aux_info_file))
2150 {
2151 fprintf (stderr, "%s: error: while closing aux info file `%s': %s\n",
a2b22788
RS
2152 pname, shortpath (NULL, aux_info_filename),
2153 sys_errlist[errno]);
5f8037c4
RS
2154 free (aux_info_base);
2155 close (aux_info_file);
2156 return;
2157 }
2158 }
2159
2160 /* Delete the aux_info file (unless requested not to). If the deletion
2161 fails for some reason, don't even worry about it. */
2162
2163 if (!keep_it)
34e56753 2164 if (my_unlink (aux_info_filename) == -1)
5f8037c4 2165 fprintf (stderr, "%s: error: can't delete aux info file `%s': %s\n",
a2b22788
RS
2166 pname, shortpath (NULL, aux_info_filename),
2167 sys_errlist[errno]);
5f8037c4
RS
2168
2169 /* Save a pointer into the first line of the aux_info file which
a2b22788 2170 contains the filename of the directory from which the compiler
5f8037c4
RS
2171 was invoked when the associated source file was compiled.
2172 This information is used later to help create complete
a2b22788 2173 filenames out of the (potentially) relative filenames in
5f8037c4
RS
2174 the aux_info file. */
2175
2176 {
2177 char *p = aux_info_base;
2178
2179 while (*p != ':')
2180 p++;
2181 p++;
2182 while (*p == ' ')
2183 p++;
a2b22788 2184 invocation_filename = p; /* Save a pointer to first byte of path. */
5f8037c4
RS
2185 while (*p != ' ')
2186 p++;
2187 *p++ = '/';
2188 *p++ = '\0';
2189 while (*p++ != '\n')
2190 continue;
2191 aux_info_second_line = p;
2192 }
2193
2194
2195 {
2196 const char *aux_info_p;
2197
2198 /* Do a pre-pass on the lines in the aux_info file, making sure that all
2199 of the source files referenced in there are at least as old as this
2200 aux_info file itself. If not, go back and regenerate the aux_info
2201 file anew. Don't do any of this for the syscalls file. */
2202
2203 if (!is_syscalls)
2204 {
2205 current_aux_info_lineno = 2;
2206
2207 for (aux_info_p = aux_info_second_line; *aux_info_p; )
2208 {
2209 if (referenced_file_is_newer (aux_info_p, aux_info_mtime))
2210 {
2211 free (aux_info_base);
34e56753 2212 if (my_unlink (aux_info_filename) == -1)
5f8037c4
RS
2213 {
2214 fprintf (stderr, "%s: error: can't delete file `%s': %s\n",
a2b22788
RS
2215 pname, shortpath (NULL, aux_info_filename),
2216 sys_errlist[errno]);
5f8037c4
RS
2217 return;
2218 }
2219 goto start_over;
2220 }
2221
2222 /* Skip over the rest of this line to start of next line. */
2223
2224 while (*aux_info_p != '\n')
2225 aux_info_p++;
2226 aux_info_p++;
2227 current_aux_info_lineno++;
2228 }
2229 }
2230
2231 /* Now do the real pass on the aux_info lines. Save their information in
2232 the in-core data base. */
2233
2234 current_aux_info_lineno = 2;
2235
2236 for (aux_info_p = aux_info_second_line; *aux_info_p;)
2237 {
2238 char *unexpanded_line = unexpand_if_needed (aux_info_p);
2239
2240 if (unexpanded_line)
2241 {
2242 save_def_or_dec (unexpanded_line, is_syscalls);
2243 free (unexpanded_line);
2244 }
2245 else
2246 save_def_or_dec (aux_info_p, is_syscalls);
2247
2248 /* Skip over the rest of this line and get to start of next line. */
2249
2250 while (*aux_info_p != '\n')
2251 aux_info_p++;
2252 aux_info_p++;
2253 current_aux_info_lineno++;
2254 }
2255 }
2256
2257 free (aux_info_base);
2258}
2259\f
2260#ifndef UNPROTOIZE
2261
2262/* Check an individual filename for a .c suffix. If the filename has this
2263 suffix, rename the file such that its suffix is changed to .C. This
2264 function implements the -C option. */
2265
2266static void
34e56753
RS
2267rename_c_file (hp)
2268 const hash_table_entry *hp;
5f8037c4 2269{
a2b22788
RS
2270 const char *filename = hp->symbol;
2271 int last_char_index = strlen (filename) - 1;
2272 char *const new_filename = (char *) alloca (strlen (filename) + 1);
5f8037c4
RS
2273
2274 /* Note that we don't care here if the given file was converted or not. It
2275 is possible that the given file was *not* converted, simply because there
2276 was nothing in it which actually required conversion. Even in this case,
2277 we want to do the renaming. Note that we only rename files with the .c
2278 suffix. */
2279
a2b22788 2280 if (filename[last_char_index] != 'c' || filename[last_char_index-1] != '.')
5f8037c4
RS
2281 return;
2282
a2b22788
RS
2283 strcpy (new_filename, filename);
2284 new_filename[last_char_index] = 'C';
5f8037c4 2285
34e56753 2286 if (my_link (filename, new_filename) == -1)
5f8037c4
RS
2287 {
2288 fprintf (stderr, "%s: warning: can't link file `%s' to `%s': %s\n",
a2b22788
RS
2289 pname, shortpath (NULL, filename),
2290 shortpath (NULL, new_filename), sys_errlist[errno]);
5f8037c4
RS
2291 errors++;
2292 return;
2293 }
2294
34e56753 2295 if (my_unlink (filename) == -1)
5f8037c4
RS
2296 {
2297 fprintf (stderr, "%s: warning: can't delete file `%s': %s\n",
a2b22788 2298 pname, shortpath (NULL, filename), sys_errlist[errno]);
5f8037c4
RS
2299 errors++;
2300 return;
2301 }
2302}
2303
2304#endif /* !defined(UNPROTOIZE) */
2305\f
2306/* Take the list of definitions and declarations attached to a particular
2307 file_info node and reverse the order of the list. This should get the
2308 list into an order such that the item with the lowest associated line
2309 number is nearest the head of the list. When these lists are originally
2310 built, they are in the opposite order. We want to traverse them in
2311 normal line number order later (i.e. lowest to highest) so reverse the
2312 order here. */
2313
2314static void
34e56753
RS
2315reverse_def_dec_list (hp)
2316 const hash_table_entry *hp;
5f8037c4
RS
2317{
2318 file_info *file_p = hp->fip;
2319 const def_dec_info *prev = NULL;
2320 const def_dec_info *current = file_p->defs_decs;
2321
2322 if (!( current = file_p->defs_decs))
2323 return; /* no list to reverse */
2324
2325 prev = current;
2326 if (! (current = current->next_in_file))
2327 return; /* can't reverse a single list element */
2328
2329 ((NONCONST def_dec_info *) prev)->next_in_file = NULL;
2330
2331 while (current)
2332 {
2333 const def_dec_info *next = current->next_in_file;
2334
2335 ((NONCONST def_dec_info *) current)->next_in_file = prev;
2336 prev = current;
2337 current = next;
2338 }
2339
2340 file_p->defs_decs = prev;
2341}
2342
2343#ifndef UNPROTOIZE
2344
2345/* Find the (only?) extern definition for a particular function name, starting
2346 from the head of the linked list of entries for the given name. If we
2347 cannot find an extern definition for the given function name, issue a
2348 warning and scrounge around for the next best thing, i.e. an extern
2349 function declaration with a prototype attached to it. Note that we only
2350 allow such substitutions for extern declarations and never for static
2351 declarations. That's because the only reason we allow them at all is
2352 to let un-prototyped function declarations for system-supplied library
2353 functions get their prototypes from our own extra SYSCALLS.c.X file which
2354 contains all of the correct prototypes for system functions. */
2355
2356static const def_dec_info *
34e56753
RS
2357find_extern_def (head, user)
2358 const def_dec_info *head;
2359 const def_dec_info *user;
5f8037c4
RS
2360{
2361 const def_dec_info *dd_p;
2362 const def_dec_info *extern_def_p = NULL;
2363 int conflict_noted = 0;
2364
2365 /* Don't act too stupid here. Somebody may try to convert an entire system
2366 in one swell fwoop (rather than one program at a time, as should be done)
2367 and in that case, we may find that there are multiple extern definitions
2368 of a given function name in the entire set of source files that we are
2369 converting. If however one of these definitions resides in exactly the
2370 same source file as the reference we are trying to satisfy then in that
2371 case it would be stupid for us to fail to realize that this one definition
2372 *must* be the precise one we are looking for.
2373
2374 To make sure that we don't miss an opportunity to make this "same file"
2375 leap of faith, we do a prescan of the list of records relating to the
2376 given function name, and we look (on this first scan) *only* for a
2377 definition of the function which is in the same file as the reference
2378 we are currently trying to satisfy. */
2379
2380 for (dd_p = head; dd_p; dd_p = dd_p->next_for_func)
2381 if (dd_p->is_func_def && !dd_p->is_static && dd_p->file == user->file)
2382 return dd_p;
2383
2384 /* Now, since we have not found a definition in the same file as the
2385 reference, we scan the list again and consider all possibilities from
2386 all files. Here we may get conflicts with the things listed in the
2387 SYSCALLS.c.X file, but if that happens it only means that the source
2388 code being converted contains its own definition of a function which
2389 could have been supplied by libc.a. In such cases, we should avoid
2390 issuing the normal warning, and defer to the definition given in the
2391 user's own code. */
2392
2393 for (dd_p = head; dd_p; dd_p = dd_p->next_for_func)
2394 if (dd_p->is_func_def && !dd_p->is_static)
2395 {
2396 if (!extern_def_p) /* Previous definition? */
2397 extern_def_p = dd_p; /* Remember the first definition found. */
2398 else
2399 {
2400 /* Ignore definition just found if it came from SYSCALLS.c.X. */
2401
2402 if (is_syscalls_file (dd_p->file))
2403 continue;
2404
2405 /* Quietly replace the definition previously found with the one
2406 just found if the previous one was from SYSCALLS.c.X. */
2407
2408 if (is_syscalls_file (extern_def_p->file))
2409 {
2410 extern_def_p = dd_p;
2411 continue;
2412 }
2413
2414 /* If we get here, then there is a conflict between two function
2415 declarations for the same function, both of which came from the
2416 user's own code. */
2417
2418 if (!conflict_noted) /* first time we noticed? */
2419 {
2420 conflict_noted = 1;
2421 fprintf (stderr, "%s: error: conflicting extern definitions of '%s'\n",
a2b22788 2422 pname, head->hash_entry->symbol);
5f8037c4
RS
2423 if (!quiet_flag)
2424 {
2425 fprintf (stderr, "%s: declarations of '%s' will not be converted\n",
a2b22788 2426 pname, head->hash_entry->symbol);
5f8037c4 2427 fprintf (stderr, "%s: conflict list for '%s' follows:\n",
a2b22788 2428 pname, head->hash_entry->symbol);
5f8037c4 2429 fprintf (stderr, "%s: %s(%d): %s\n",
a2b22788
RS
2430 pname,
2431 shortpath (NULL, extern_def_p->file->hash_entry->symbol),
2432 extern_def_p->line, extern_def_p->ansi_decl);
5f8037c4
RS
2433 }
2434 }
2435 if (!quiet_flag)
2436 fprintf (stderr, "%s: %s(%d): %s\n",
a2b22788
RS
2437 pname,
2438 shortpath (NULL, dd_p->file->hash_entry->symbol),
2439 dd_p->line, dd_p->ansi_decl);
5f8037c4
RS
2440 }
2441 }
2442
2443 /* We want to err on the side of caution, so if we found multiple conflicting
2444 definitions for the same function, treat this as being that same as if we
2445 had found no definitions (i.e. return NULL). */
2446
2447 if (conflict_noted)
2448 return NULL;
2449
2450 if (!extern_def_p)
2451 {
2452 /* We have no definitions for this function so do the next best thing.
2453 Search for an extern declaration already in prototype form. */
2454
2455 for (dd_p = head; dd_p; dd_p = dd_p->next_for_func)
2456 if (!dd_p->is_func_def && !dd_p->is_static && dd_p->prototyped)
2457 {
2458 extern_def_p = dd_p; /* save a pointer to the definition */
2459 if (!quiet_flag)
2460 fprintf (stderr, "%s: warning: using formals list from %s(%d) for function `%s'\n",
a2b22788
RS
2461 pname,
2462 shortpath (NULL, dd_p->file->hash_entry->symbol),
2463 dd_p->line, dd_p->hash_entry->symbol);
5f8037c4
RS
2464 break;
2465 }
2466
2467 /* Gripe about unprototyped function declarations that we found no
2468 corresponding definition (or other source of prototype information)
2469 for.
2470
2471 Gripe even if the unprototyped declaration we are worried about
2472 exists in a file in one of the "system" include directories. We
2473 can gripe about these because we should have at least found a
2474 corresponding (pseudo) definition in the SYSCALLS.c.X file. If we
2475 didn't, then that means that the SYSCALLS.c.X file is missing some
2476 needed prototypes for this particular system. That is worth telling
2477 the user about! */
2478
2479 if (!extern_def_p)
2480 {
2481 const char *file = user->file->hash_entry->symbol;
2482
2483 if (!quiet_flag)
2484 if (in_system_include_dir (file))
2485 {
2486 /* Why copy this string into `needed' at all?
2487 Why not just use user->ansi_decl without copying? */
34e56753 2488 char *needed = (char *) alloca (strlen (user->ansi_decl) + 1);
5f8037c4
RS
2489 char *p;
2490
2491 strcpy (needed, user->ansi_decl);
2492 p = (NONCONST char *) substr (needed, user->hash_entry->symbol)
2493 + strlen (user->hash_entry->symbol) + 2;
34e56753 2494 strcpy (p, "??\?);");
5f8037c4 2495
a2b22788
RS
2496 fprintf (stderr, "%s: %d: `%s' used but missing from SYSCALLS\n",
2497 shortpath (NULL, file), user->line,
2498 needed+7); /* Don't print "extern " */
5f8037c4
RS
2499 }
2500 else
a2b22788
RS
2501 fprintf (stderr, "%s: %d: warning: no extern definition for `%s'\n",
2502 shortpath (NULL, file), user->line,
2503 user->hash_entry->symbol);
5f8037c4
RS
2504 }
2505 }
2506 return extern_def_p;
2507}
2508\f
2509/* Find the (only?) static definition for a particular function name in a
2510 given file. Here we get the function-name and the file info indirectly
2511 from the def_dec_info record pointer which is passed in. */
2512
2513static const def_dec_info *
34e56753
RS
2514find_static_definition (user)
2515 const def_dec_info *user;
5f8037c4
RS
2516{
2517 const def_dec_info *head = user->hash_entry->ddip;
2518 const def_dec_info *dd_p;
2519 int num_static_defs = 0;
2520 const def_dec_info *static_def_p = NULL;
2521
2522 for (dd_p = head; dd_p; dd_p = dd_p->next_for_func)
2523 if (dd_p->is_func_def && dd_p->is_static && (dd_p->file == user->file))
2524 {
2525 static_def_p = dd_p; /* save a pointer to the definition */
2526 num_static_defs++;
2527 }
2528 if (num_static_defs == 0)
2529 {
2530 if (!quiet_flag)
2531 fprintf (stderr, "%s: warning: no static definition for `%s' in file `%s'\n",
a2b22788
RS
2532 pname, head->hash_entry->symbol,
2533 shortpath (NULL, user->file->hash_entry->symbol));
5f8037c4
RS
2534 }
2535 else if (num_static_defs > 1)
2536 {
2537 fprintf (stderr, "%s: error: multiple static defs of `%s' in file `%s'\n",
a2b22788
RS
2538 pname, head->hash_entry->symbol,
2539 shortpath (NULL, user->file->hash_entry->symbol));
5f8037c4
RS
2540 return NULL;
2541 }
2542 return static_def_p;
2543}
2544
2545/* Find good prototype style formal argument lists for all of the function
2546 declarations which didn't have them before now.
2547
2548 To do this we consider each function name one at a time. For each function
2549 name, we look at the items on the linked list of def_dec_info records for
2550 that particular name.
2551
2552 Somewhere on this list we should find one (and only one) def_dec_info
2553 record which represents the actual function definition, and this record
2554 should have a nice formal argument list already associated with it.
2555
2556 Thus, all we have to do is to connect up all of the other def_dec_info
2557 records for this particular function name to the special one which has
2558 the full-blown formals list.
2559
2560 Of course it is a little more complicated than just that. See below for
2561 more details. */
2562
2563static void
34e56753
RS
2564connect_defs_and_decs (hp)
2565 const hash_table_entry *hp;
5f8037c4
RS
2566{
2567 const def_dec_info *dd_p;
2568 const def_dec_info *extern_def_p = NULL;
2569 int first_extern_reference = 1;
2570
2571 /* Traverse the list of definitions and declarations for this particular
2572 function name. For each item on the list, if it is a function
2573 definition (either old style or new style) then GCC has already been
2574 kind enough to produce a prototype for us, and it is associated with
2575 the item already, so declare the item as its own associated "definition".
2576
2577 Also, for each item which is only a function declaration, but which
2578 nonetheless has its own prototype already (obviously supplied by the user)
2579 declare the item as it's own definition.
2580
2581 Note that when/if there are multiple user-supplied prototypes already
2582 present for multiple declarations of any given function, these multiple
2583 prototypes *should* all match exactly with one another and with the
2584 prototype for the actual function definition. We don't check for this
2585 here however, since we assume that the compiler must have already done
2586 this consistancy checking when it was creating the .X files. */
2587
2588 for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func)
2589 if (dd_p->prototyped)
2590 ((NONCONST def_dec_info *) dd_p)->definition = dd_p;
2591
2592 /* Traverse the list of definitions and declarations for this particular
2593 function name. For each item on the list, if it is an extern function
2594 declaration and if it has no associated definition yet, go try to find
2595 the matching extern definition for the declaration.
2596
2597 When looking for the matching function definition, warn the user if we
2598 fail to find one.
2599
2600 If we find more that one function definition also issue a warning.
2601
2602 Do the search for the matching definition only once per unique function
2603 name (and only when absolutely needed) so that we can avoid putting out
2604 redundant warning messages, and so that we will only put out warning
2605 messages when there is actually a reference (i.e. a declaration) for
2606 which we need to find a matching definition. */
2607
2608 for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func)
2609 if (!dd_p->is_func_def && !dd_p->is_static && !dd_p->definition)
2610 {
2611 if (first_extern_reference)
2612 {
2613 extern_def_p = find_extern_def (hp->ddip, dd_p);
2614 first_extern_reference = 0;
2615 }
2616 ((NONCONST def_dec_info *) dd_p)->definition = extern_def_p;
2617 }
2618
2619 /* Traverse the list of definitions and declarations for this particular
2620 function name. For each item on the list, if it is a static function
2621 declaration and if it has no associated definition yet, go try to find
2622 the matching static definition for the declaration within the same file.
2623
2624 When looking for the matching function definition, warn the user if we
2625 fail to find one in the same file with the declaration, and refuse to
2626 convert this kind of cross-file static function declaration. After all,
2627 this is stupid practice and should be discouraged.
2628
2629 We don't have to worry about the possibility that there is more than one
2630 matching function definition in the given file because that would have
2631 been flagged as an error by the compiler.
2632
2633 Do the search for the matching definition only once per unique
2634 function-name/source-file pair (and only when absolutely needed) so that
2635 we can avoid putting out redundant warning messages, and so that we will
2636 only put out warning messages when there is actually a reference (i.e. a
2637 declaration) for which we actually need to find a matching definition. */
2638
2639 for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func)
2640 if (!dd_p->is_func_def && dd_p->is_static && !dd_p->definition)
2641 {
2642 const def_dec_info *dd_p2;
2643 const def_dec_info *static_def;
2644
2645 /* We have now found a single static declaration for which we need to
2646 find a matching definition. We want to minimize the work (and the
2647 number of warnings), so we will find an appropriate (matching)
2648 static definition for this declaration, and then distribute it
2649 (as the definition for) any and all other static declarations
2650 for this function name which occur within the same file, and which
2651 do not already have definitions.
2652
2653 Note that a trick is used here to prevent subsequent attempts to
2654 call find_static_definition() for a given function-name & file
2655 if the first such call returns NULL. Essentially, we convert
2656 these NULL return values to -1, and put the -1 into the definition
2657 field for each other static declaration from the same file which
2658 does not already have an associated definition.
2659 This makes these other static declarations look like they are
2660 actually defined already when the outer loop here revisits them
2661 later on. Thus, the outer loop will skip over them. Later, we
2662 turn the -1's back to NULL's. */
2663
2664 ((NONCONST def_dec_info *) dd_p)->definition =
2665 (static_def = find_static_definition (dd_p))
2666 ? static_def
2667 : (const def_dec_info *) -1;
2668
2669 for (dd_p2 = dd_p->next_for_func; dd_p2; dd_p2 = dd_p2->next_for_func)
2670 if (!dd_p2->is_func_def && dd_p2->is_static
2671 && !dd_p2->definition && (dd_p2->file == dd_p->file))
2672 ((NONCONST def_dec_info *)dd_p2)->definition = dd_p->definition;
2673 }
2674
2675 /* Convert any dummy (-1) definitions we created in the step above back to
2676 NULL's (as they should be). */
2677
2678 for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func)
2679 if (dd_p->definition == (def_dec_info *) -1)
2680 ((NONCONST def_dec_info *) dd_p)->definition = NULL;
2681}
2682
2683#endif /* !defined(UNPROTOIZE) */
2684
2685/* Give a pointer into the clean text buffer, return a number which is the
2686 original source line number that the given pointer points into. */
2687
2688static int
34e56753
RS
2689identify_lineno (clean_p)
2690 const char *clean_p;
5f8037c4
RS
2691{
2692 int line_num = 1;
2693 const char *scan_p;
2694
2695 for (scan_p = clean_text_base; scan_p <= clean_p; scan_p++)
2696 if (*scan_p == '\n')
2697 line_num++;
2698 return line_num;
2699}
2700
2701/* Issue an error message and give up on doing this particular edit. */
2702
2703static void
34e56753
RS
2704declare_source_confusing (clean_p)
2705 const char *clean_p;
5f8037c4
RS
2706{
2707 if (!quiet_flag)
2708 {
2709 if (clean_p == 0)
a2b22788
RS
2710 fprintf (stderr, "%s: %d: warning: source too confusing\n",
2711 shortpath (NULL, convert_filename), last_known_line_number);
5f8037c4 2712 else
a2b22788
RS
2713 fprintf (stderr, "%s: %d: warning: source too confusing\n",
2714 shortpath (NULL, convert_filename),
2715 identify_lineno (clean_p));
5f8037c4
RS
2716 }
2717 longjmp (source_confusion_recovery, 1);
2718}
2719
2720/* Check that a condition which is expected to be true in the original source
2721 code is in fact true. If not, issue an error message and give up on
2722 converting this particular source file. */
2723
34e56753
RS
2724static void
2725check_source (cond, clean_p)
2726 int cond;
2727 const char *clean_p;
5f8037c4
RS
2728{
2729 if (!cond)
2730 declare_source_confusing (clean_p);
2731}
2732
2733/* If we think of the in-core cleaned text buffer as a memory mapped
2734 file (with the variable last_known_line_start acting as sort of a
2735 file pointer) then we can imagine doing "seeks" on the buffer. The
2736 following routine implements a kind of "seek" operation for the in-core
2737 (cleaned) copy of the source file. When finished, it returns a pointer to
2738 the start of a given (numbered) line in the cleaned text buffer.
2739
2740 Note that protoize only has to "seek" in the forward direction on the
2741 in-core cleaned text file buffers, and it never needs to back up.
2742
2743 This routine is made a little bit faster by remembering the line number
2744 (and pointer value) supplied (and returned) from the previous "seek".
2745 This prevents us from always having to start all over back at the top
2746 of the in-core cleaned buffer again. */
2747
2748static const char *
34e56753
RS
2749seek_to_line (n)
2750 int n;
5f8037c4
RS
2751{
2752 if (n < last_known_line_number)
2753 abort ();
2754
2755 while (n > last_known_line_number)
2756 {
2757 while (*last_known_line_start != '\n')
2758 check_source (++last_known_line_start < clean_text_limit, 0);
2759 last_known_line_start++;
2760 last_known_line_number++;
2761 }
2762 return last_known_line_start;
2763}
2764
2765/* Given a pointer to a character in the cleaned text buffer, return a pointer
2766 to the next non-whitepace character which follows it. */
2767
2768static const char *
34e56753
RS
2769forward_to_next_token_char (ptr)
2770 const char *ptr;
5f8037c4
RS
2771{
2772 for (++ptr; isspace (*ptr); check_source (++ptr < clean_text_limit, 0))
2773 continue;
2774 return ptr;
2775}
2776
2777/* Copy a chunk of text of length `len' and starting at `str' to the current
2778 output buffer. Note that all attempts to add stuff to the current output
2779 buffer ultimately go through here. */
2780
2781static void
34e56753
RS
2782output_bytes (str, len)
2783 const char *str;
2784 size_t len;
5f8037c4
RS
2785{
2786 if ((repl_write_ptr + 1) + len >= repl_text_limit)
2787 {
2788 size_t new_size = (repl_text_limit - repl_text_base) << 1;
2789 char *new_buf = (char *) xrealloc (repl_text_base, new_size);
2790
2791 repl_write_ptr = new_buf + (repl_write_ptr - repl_text_base);
2792 repl_text_base = new_buf;
2793 repl_text_limit = new_buf + new_size;
2794 }
2795 memcpy (repl_write_ptr + 1, str, len);
2796 repl_write_ptr += len;
2797}
2798
2799/* Copy all bytes (except the trailing null) of a null terminated string to
2800 the current output buffer. */
2801
2802static void
34e56753
RS
2803output_string (str)
2804 const char *str;
5f8037c4
RS
2805{
2806 output_bytes (str, strlen (str));
2807}
2808
2809/* Copy some characters from the original text buffer to the current output
2810 buffer.
2811
2812 This routine takes a pointer argument `p' which is assumed to be a pointer
2813 into the cleaned text buffer. The bytes which are copied are the `original'
2814 equivalents for the set of bytes between the last value of `clean_read_ptr'
2815 and the argument value `p'.
2816
2817 The set of bytes copied however, comes *not* from the cleaned text buffer,
2818 but rather from the direct counterparts of these bytes within the original
2819 text buffer.
2820
2821 Thus, when this function is called, some bytes from the original text
2822 buffer (which may include original comments and preprocessing directives)
2823 will be copied into the output buffer.
2824
2825 Note that the request implide when this routine is called includes the
2826 byte pointed to by the argument pointer `p'. */
2827
2828static void
34e56753
RS
2829output_up_to (p)
2830 const char *p;
5f8037c4
RS
2831{
2832 size_t copy_length = (size_t) (p - clean_read_ptr);
2833 const char *copy_start = orig_text_base+(clean_read_ptr-clean_text_base)+1;
2834
2835 if (copy_length == 0)
2836 return;
2837
2838 output_bytes (copy_start, copy_length);
2839 clean_read_ptr = p;
2840}
2841
2842/* Given a pointer to a def_dec_info record which represents some form of
2843 definition of a function (perhaps a real definition, or in lieu of that
2844 perhaps just a declaration with a full prototype) return true if this
2845 function is one which we should avoid converting. Return false
2846 otherwise. */
2847
2848static int
34e56753
RS
2849other_variable_style_function (ansi_header)
2850 const char *ansi_header;
5f8037c4
RS
2851{
2852#ifdef UNPROTOIZE
2853
2854 /* See if we have a stdarg function, or a function which has stdarg style
2855 parameters or a stdarg style return type. */
2856
2857 return (int) substr (ansi_header, "...");
2858
2859#else /* !defined(UNPROTOIZE) */
2860
2861 /* See if we have a varargs function, or a function which has varargs style
2862 parameters or a varargs style return type. */
2863
2864 const char *p;
2865 int len = strlen (varargs_style_indicator);
2866
2867 for (p = ansi_header; p; )
2868 {
2869 const char *candidate;
2870
2871 if ((candidate = substr (p, varargs_style_indicator)) == 0)
2872 return 0;
2873 else
2874 if (!is_id_char (candidate[-1]) && !is_id_char (candidate[len]))
2875 return 1;
2876 else
2877 p = candidate + 1;
2878 }
2879 return 0;
2880#endif /* !defined(UNPROTOIZE) */
2881}
2882
2883/* Do the editing operation specifically for a function "declaration". Note
2884 that editing for function "definitions" are handled in a separate routine
2885 below. */
2886
2887static void
34e56753
RS
2888edit_fn_declaration (def_dec_p, clean_text_p)
2889 const def_dec_info *def_dec_p;
2890 const char *VOLATILE clean_text_p;
5f8037c4
RS
2891{
2892 const char *start_formals;
2893 const char *end_formals;
2894 const char *function_to_edit = def_dec_p->hash_entry->symbol;
2895 size_t func_name_len = strlen (function_to_edit);
2896 const char *end_of_fn_name;
2897
2898#ifndef UNPROTOIZE
2899
2900 const f_list_chain_item *this_f_list_chain_item;
2901 const def_dec_info *definition = def_dec_p->definition;
2902
2903 /* If we are protoizing, and if we found no corresponding definition for
2904 this particular function declaration, then just leave this declaration
2905 exactly as it is. */
2906
2907 if (!definition)
2908 return;
2909
2910 /* If we are protoizing, and if the corresponding definition that we found
2911 for this particular function declaration defined an old style varargs
2912 function, then we want to issue a warning and just leave this function
2913 declaration unconverted. */
2914
2915 if (other_variable_style_function (definition->ansi_decl))
2916 {
2917 if (!quiet_flag)
a2b22788
RS
2918 fprintf (stderr, "%s: %d: warning: varargs function declaration not converted\n",
2919 shortpath (NULL, def_dec_p->file->hash_entry->symbol),
2920 def_dec_p->line);
5f8037c4
RS
2921 return;
2922 }
2923
2924#endif /* !defined(UNPROTOIZE) */
2925
2926 /* Setup here to recover from confusing source code detected during this
2927 particular "edit". */
2928
2929 save_pointers ();
2930 if (setjmp (source_confusion_recovery))
2931 {
2932 restore_pointers ();
2933 fprintf (stderr, "%s: declaration of function `%s' not converted\n",
a2b22788 2934 pname, function_to_edit);
5f8037c4
RS
2935 return;
2936 }
2937
2938 /* We are editing a function declaration. The line number we did a seek to
2939 contains the comma or semicolon which follows the declaration. Our job
2940 now is to scan backwards looking for the function name. This name *must*
2941 be followed by open paren (ignoring whitespace, of course). We need to
2942 replace everything between that open paren and the corresponding closing
2943 paren. If we are protoizing, we need to insert the prototype-style
2944 formals lists. If we are unprotoizing, we need to just delete everything
2945 between the pairs of opening and closing parens. */
2946
2947 /* First move up to the end of the line. */
2948
2949 while (*clean_text_p != '\n')
2950 check_source (++clean_text_p < clean_text_limit, 0);
2951 clean_text_p--; /* Point to just before the newline character. */
2952
2953 /* Now we can scan backwards for the function name. */
2954
2955 do
2956 {
2957 for (;;)
2958 {
2959 /* Scan leftwards until we find some character which can be
2960 part of an identifier. */
2961
2962 while (!is_id_char (*clean_text_p))
2963 check_source (--clean_text_p > clean_read_ptr, 0);
2964
2965 /* Scan backwards until we find a char that cannot be part of an
2966 identifier. */
2967
2968 while (is_id_char (*clean_text_p))
2969 check_source (--clean_text_p > clean_read_ptr, 0);
2970
2971 /* Having found an "id break", see if the following id is the one
2972 that we are looking for. If so, then exit from this loop. */
2973
2974 if (!strncmp (clean_text_p+1, function_to_edit, func_name_len))
2975 {
2976 char ch = *(clean_text_p + 1 + func_name_len);
2977
2978 /* Must also check to see that the name in the source text
2979 ends where it should (in order to prevent bogus matches
2980 on similar but longer identifiers. */
2981
2982 if (! is_id_char (ch))
2983 break; /* exit from loop */
2984 }
2985 }
2986
2987 /* We have now found the first perfect match for the function name in
2988 our backward search. This may or may not be the actual function
2989 name at the start of the actual function declaration (i.e. we could
2990 have easily been mislead). We will try to avoid getting fooled too
2991 often by looking forward for the open paren which should follow the
2992 identifier we just found. We ignore whitespace while hunting. If
2993 the next non-whitespace byte we see is *not* an open left paren,
2994 then we must assume that we have been fooled and we start over
2995 again accordingly. Note that there is no guarrantee, that even if
2996 we do see the open paren, that we are in the right place.
2997 Programmers do the strangest things sometimes! */
2998
2999 end_of_fn_name = clean_text_p + strlen (def_dec_p->hash_entry->symbol);
3000 start_formals = forward_to_next_token_char (end_of_fn_name);
3001 }
3002 while (*start_formals != '(');
3003
3004 /* start_of_formals now points to the opening left paren which immediately
3005 follows the name of the function. */
3006
3007 /* Note that there may be several formals lists which need to be modified
3008 due to the possibility that the return type of this function is a
3009 pointer-to-function type. If there are several formals lists, we
3010 convert them in left-to-right order here. */
3011
3012#ifndef UNPROTOIZE
3013 this_f_list_chain_item = definition->f_list_chain;
3014#endif /* !defined(UNPROTOIZE) */
3015
3016 for (;;)
3017 {
3018 {
3019 int depth;
3020
3021 end_formals = start_formals + 1;
3022 depth = 1;
3023 for (; depth; check_source (++end_formals < clean_text_limit, 0))
3024 {
3025 switch (*end_formals)
3026 {
3027 case '(':
3028 depth++;
3029 break;
3030 case ')':
3031 depth--;
3032 break;
3033 }
3034 }
3035 end_formals--;
3036 }
3037
3038 /* end_formals now points to the closing right paren of the formals
3039 list whose left paren is pointed to by start_formals. */
3040
3041 /* Now, if we are protoizing, we insert the new ANSI-style formals list
3042 attached to the associated definition of this function. If however
3043 we are unprotoizing, then we simply delete any formals list which
3044 may be present. */
3045
3046 output_up_to (start_formals);
3047#ifndef UNPROTOIZE
3048 if (this_f_list_chain_item)
3049 {
3050 output_string (this_f_list_chain_item->formals_list);
3051 this_f_list_chain_item = this_f_list_chain_item->chain_next;
3052 }
3053 else
3054 {
3055 if (!quiet_flag)
3056 fprintf (stderr, "%s: warning: too many parameter lists in declaration of `%s'\n",
a2b22788 3057 pname, def_dec_p->hash_entry->symbol);
5f8037c4
RS
3058 check_source (0, end_formals); /* leave the declaration intact */
3059 }
3060#endif /* !defined(UNPROTOIZE) */
3061 clean_read_ptr = end_formals - 1;
3062
3063 /* Now see if it looks like there may be another formals list associated
3064 with the function declaration that we are converting (following the
3065 formals list that we just converted. */
3066
3067 {
3068 const char *another_r_paren = forward_to_next_token_char (end_formals);
3069
3070 if ((*another_r_paren != ')')
3071 || (*(start_formals = forward_to_next_token_char (another_r_paren)) != '('))
3072 {
3073#ifndef UNPROTOIZE
3074 if (this_f_list_chain_item)
3075 {
3076 if (!quiet_flag)
3077 fprintf (stderr, "\n%s: warning: too few parameter lists in declaration of `%s'\n",
a2b22788 3078 pname, def_dec_p->hash_entry->symbol);
5f8037c4
RS
3079 check_source (0, start_formals); /* leave the decl intact */
3080 }
3081#endif /* !defined(UNPROTOIZE) */
3082 break;
3083
3084 }
3085 }
3086
3087 /* There does appear to be yet another formals list, so loop around
3088 again, and convert it also. */
3089 }
3090}
3091
3092/* Edit a whole group of formals lists, starting with the rightmost one
3093 from some set of formals lists. This routine is called once (from the
3094 outside) for each function declaration which is converted. It is
3095 recursive however, and it calls itself once for each remaining formal
3096 list that lies to the left of the one it was originally called to work
3097 on. Thus, a whole set gets done in right-to-left order.
3098
3099 This routine returns non-zero if it thinks that it should not be trying
3100 to convert this particular function definition (because the name of the
3101 function doesn't match the one expected). */
3102
3103static int
34e56753
RS
3104edit_formals_lists (end_formals, f_list_count, def_dec_p)
3105 const char *end_formals;
3106 unsigned int f_list_count;
3107 const def_dec_info *def_dec_p;
5f8037c4
RS
3108{
3109 const char *start_formals;
3110 int depth;
3111
3112 start_formals = end_formals - 1;
3113 depth = 1;
3114 for (; depth; check_source (--start_formals > clean_read_ptr, 0))
3115 {
3116 switch (*start_formals)
3117 {
3118 case '(':
3119 depth--;
3120 break;
3121 case ')':
3122 depth++;
3123 break;
3124 }
3125 }
3126 start_formals++;
3127
3128 /* start_formals now points to the opening left paren of the formals list. */
3129
3130 f_list_count--;
3131
3132 if (f_list_count)
3133 {
3134 const char *next_end;
3135
3136 /* There should be more formal lists to the left of here. */
3137
3138 next_end = start_formals - 1;
3139 check_source (next_end > clean_read_ptr, 0);
3140 while (isspace (*next_end))
3141 check_source (--next_end > clean_read_ptr, 0);
3142 check_source (*next_end == ')', next_end);
3143 check_source (--next_end > clean_read_ptr, 0);
3144 check_source (*next_end == ')', next_end);
3145 if (edit_formals_lists (next_end, f_list_count, def_dec_p))
3146 return 1;
3147 }
3148
3149 /* Check that the function name in the header we are working on is the same
3150 as the one we would expect to find. If not, issue a warning and return
3151 non-zero. */
3152
3153 if (f_list_count == 0)
3154 {
3155 const char *expected = def_dec_p->hash_entry->symbol;
3156 const char *func_name_start;
3157 const char *func_name_limit;
3158 size_t func_name_len;
3159
3160 for (func_name_limit = start_formals-1; isspace (*func_name_limit); )
3161 check_source (--func_name_limit > clean_read_ptr, 0);
3162
3163 for (func_name_start = func_name_limit++;
3164 is_id_char (*func_name_start);
3165 func_name_start--)
3166 check_source (func_name_start > clean_read_ptr, 0);
3167 func_name_start++;
3168 func_name_len = func_name_limit - func_name_start;
3169 if (func_name_len == 0)
3170 check_source (0, func_name_start);
3171 if (func_name_len != strlen (expected)
a2b22788 3172 || strncmp (func_name_start, expected, func_name_len))
5f8037c4 3173 {
a2b22788
RS
3174 fprintf (stderr, "%s: %d: warning: found `%s' but expected `%s'\n",
3175 shortpath (NULL, def_dec_p->file->hash_entry->symbol),
3176 identify_lineno (func_name_start),
3177 dupnstr (func_name_start, func_name_len),
3178 expected);
5f8037c4
RS
3179 return 1;
3180 }
3181 }
3182
3183 output_up_to (start_formals);
3184
3185#ifdef UNPROTOIZE
3186 if (f_list_count == 0)
3187 output_string (def_dec_p->formal_names);
3188#else /* !defined(UNPROTOIZE) */
3189 {
3190 unsigned f_list_depth;
3191 const f_list_chain_item *flci_p = def_dec_p->f_list_chain;
3192
3193 /* At this point, the current value of f_list count says how many
3194 links we have to follow through the f_list_chain to get to the
3195 particular formals list that we need to output next. */
3196
3197 for (f_list_depth = 0; f_list_depth < f_list_count; f_list_depth++)
3198 flci_p = flci_p->chain_next;
3199 output_string (flci_p->formals_list);
3200 }
3201#endif /* !defined(UNPROTOIZE) */
3202
3203 clean_read_ptr = end_formals - 1;
3204 return 0;
3205}
3206
3207/* Given a pointer to a byte in the clean text buffer which points to the
3208 beginning of a line that contains a "follower" token for a function
3209 definition header, do whatever is necessary to find the right closing
3210 paren for the rightmost formals list of the function definition header.
3211*/
3212
3213static const char *
34e56753
RS
3214find_rightmost_formals_list (clean_text_p)
3215 const char *clean_text_p;
5f8037c4
RS
3216{
3217 const char *end_formals;
3218
3219 /* We are editing a function definition. The line number we did a seek
3220 to contains the first token which immediately follows the entire set of
3221 formals lists which are part of this particular function definition
3222 header.
3223
3224 Our job now is to scan leftwards in the clean text looking for the
3225 right-paren which is at the end of the function header's rightmost
3226 formals list.
3227
3228 If we ignore whitespace, this right paren should be the first one we
3229 see which is (ignoring whitespace) immediately followed either by the
3230 open curly-brace beginning the function body or by an alphabetic
3231 character (in the case where the function definition is in old (K&R)
3232 style and there are some declarations of formal parameters). */
3233
3234 /* It is possible that the right paren we are looking for is on the
3235 current line (together with its following token). Just in case that
3236 might be true, we start out here by skipping down to the right end of
3237 the current line before starting our scan. */
3238
3239 for (end_formals = clean_text_p; *end_formals != '\n'; end_formals++)
3240 continue;
3241 end_formals--;
3242
34e56753
RS
3243#ifdef UNPROTOIZE
3244
5f8037c4
RS
3245 /* Now scan backwards while looking for the right end of the rightmost
3246 formals list associated with this function definition. */
3247
34e56753
RS
3248 {
3249 char ch;
3250 const char *l_brace_p;
3251
3252 /* Look leftward and try to find a right-paren. */
3253
3254 while (*end_formals != ')')
3255 {
3256 if (isspace (*end_formals))
3257 while (isspace (*end_formals))
3258 check_source (--end_formals > clean_read_ptr, 0);
3259 else
3260 check_source (--end_formals > clean_read_ptr, 0);
3261 }
3262
3263 ch = *(l_brace_p = forward_to_next_token_char (end_formals));
3264 /* Since we are unprotoizing an ANSI-style (prototyped) function
3265 definition, there had better not be anything (except whitespace)
3266 between the end of the ANSI formals list and the beginning of the
3267 function body (i.e. the '{'). */
3268
3269 check_source (ch == '{', l_brace_p);
3270 }
3271
3272#else /* !defined(UNPROTOIZE) */
3273
3274 /* Now scan backwards while looking for the right end of the rightmost
3275 formals list associated with this function definition. */
3276
3277 while (1)
5f8037c4
RS
3278 {
3279 char ch;
3280 const char *l_brace_p;
3281
3282 /* Look leftward and try to find a right-paren. */
3283
3284 while (*end_formals != ')')
3285 {
3286 if (isspace (*end_formals))
3287 while (isspace (*end_formals))
3288 check_source (--end_formals > clean_read_ptr, 0);
3289 else
3290 check_source (--end_formals > clean_read_ptr, 0);
3291 }
3292
3293 ch = *(l_brace_p = forward_to_next_token_char (end_formals));
3294
5f8037c4
RS
3295 /* Since it is possible that we found a right paren before the starting
3296 '{' of the body which IS NOT the one at the end of the real K&R
3297 formals list (say for instance, we found one embedded inside one of
3298 the old K&R formal parameter declarations) we have to check to be
3299 sure that this is in fact the right paren that we were looking for.
3300
3301 The one we were looking for *must* be followed by either a '{' or
3302 by an alphabetic character, while others *cannot* legally be followed
3303 by such characters. */
3304
3305 if ((ch == '{') || isalpha (ch))
3306 break;
3307
3308 /* At this point, we have found a right paren, but we know that it is
3309 not the one we were looking for, so backup one character and keep
3310 looking. */
3311
3312 check_source (--end_formals > clean_read_ptr, 0);
34e56753 3313 }
5f8037c4
RS
3314
3315#endif /* !defined(UNPROTOIZE) */
3316
5f8037c4
RS
3317 return end_formals;
3318}
3319
3320#ifndef UNPROTOIZE
3321
3322/* Insert into the output file a totally new declaration for a function
3323 which (up until now) was being called from within the current block
3324 without having been declared at any point such that the declaration
3325 was visible (i.e. in scope) at the point of the call.
3326
3327 We need to add in explicit declarations for all such function calls
3328 in order to get the full benefit of prototype-based function call
3329 parameter type checking. */
3330
3331static void
34e56753
RS
3332add_local_decl (def_dec_p, clean_text_p)
3333 const def_dec_info *def_dec_p;
3334 const char *clean_text_p;
5f8037c4
RS
3335{
3336 const char *start_of_block;
3337 const char *function_to_edit = def_dec_p->hash_entry->symbol;
3338
3339 /* Don't insert new local explicit declarations unless explicitly requested
3340 to do so. */
3341
3342 if (!local_flag)
3343 return;
3344
3345 /* Setup here to recover from confusing source code detected during this
3346 particular "edit". */
3347
3348 save_pointers ();
3349 if (setjmp (source_confusion_recovery))
3350 {
3351 restore_pointers ();
3352 fprintf (stderr, "%s: local declaration for function `%s' not inserted\n",
a2b22788 3353 pname, function_to_edit);
5f8037c4
RS
3354 return;
3355 }
3356
3357 /* We have already done a seek to the start of the line which should
3358 contain *the* open curly brace which begins the block in which we need
3359 to insert an explicit function declaration (to replace the implicit one).
3360
3361 Now we scan that line, starting from the left, until we find the
3362 open curly brace we are looking for. Note that there may actually be
3363 multiple open curly braces on the given line, but we will be happy
3364 with the leftmost one no matter what. */
3365
3366 start_of_block = clean_text_p;
3367 while (*start_of_block != '{' && *start_of_block != '\n')
3368 check_source (++start_of_block < clean_text_limit, 0);
3369
3370 /* Note that the line from the original source could possibly
3371 contain *no* open curly braces! This happens if the line contains
3372 a macro call which expands into a chunk of text which includes a
3373 block (and that block's associated open and close curly braces).
3374 In cases like this, we give up, issue a warning, and do nothing. */
3375
3376 if (*start_of_block != '{')
3377 {
3378 if (!quiet_flag)
3379 fprintf (stderr,
a2b22788
RS
3380 "\n%s: %d: warning: can't add declaration of `%s' into macro call\n",
3381 def_dec_p->file->hash_entry->symbol, def_dec_p->line,
3382 def_dec_p->hash_entry->symbol);
5f8037c4
RS
3383 return;
3384 }
3385
3386 /* Figure out what a nice (pretty) indentation would be for the new
3387 declaration we are adding. In order to do this, we must scan forward
3388 from the '{' until we find the first line which starts with some
3389 non-whitespace characters (i.e. real "token" material). */
3390
3391 {
3392 const char *ep = forward_to_next_token_char (start_of_block) - 1;
3393 const char *sp;
3394
3395 /* Now we have ep pointing at the rightmost byte of some existing indent
3396 stuff. At least that is the hope.
3397
3398 We can now just scan backwards and find the left end of the existing
3399 indentation string, and then copy it to the output buffer. */
3400
3401 for (sp = ep; isspace (*sp) && *sp != '\n'; sp--)
3402 continue;
3403
3404 /* Now write out the open { which began this block, and any following
3405 trash up to and including the last byte of the existing indent that
3406 we just found. */
3407
3408 output_up_to (ep);
3409
3410 /* Now we go ahead and insert the new declaration at this point.
3411
3412 If the definition of the given function is in the same file that we
3413 are currently editing, and if its full ANSI declaration normally
3414 would start with the keyword `extern', suppress the `extern'. */
3415
3416 {
3417 const char *decl = def_dec_p->definition->ansi_decl;
3418
3419 if ((*decl == 'e') && (def_dec_p->file == def_dec_p->definition->file))
3420 decl += 7;
3421 output_string (decl);
3422 }
3423
3424 /* Finally, write out a new indent string, just like the preceeding one
3425 that we found. This will typically include a newline as the first
3426 character of the indent string. */
3427
3428 output_bytes (sp, (size_t) (ep - sp) + 1);
3429 }
3430}
3431
3432/* Given a pointer to a file_info record, and a pointer to the beginning
3433 of a line (in the clean text buffer) which is assumed to contain the
3434 first "follower" token for the first function definition header in the
3435 given file, find a good place to insert some new global function
3436 declarations (which will replace scattered and imprecise implicit ones)
3437 and then insert the new explicit declaration at that point in the file. */
3438
3439static void
34e56753
RS
3440add_global_decls (file_p, clean_text_p)
3441 const file_info *file_p;
3442 const char *clean_text_p;
5f8037c4
RS
3443{
3444 const def_dec_info *dd_p;
3445 const char *scan_p;
3446
3447 /* Setup here to recover from confusing source code detected during this
3448 particular "edit". */
3449
3450 save_pointers ();
3451 if (setjmp (source_confusion_recovery))
3452 {
3453 restore_pointers ();
3454 fprintf (stderr, "%s: global declarations for file `%s' not inserted\n",
a2b22788 3455 pname, shortpath (NULL, file_p->hash_entry->symbol));
5f8037c4
RS
3456 return;
3457 }
3458
3459 /* Start by finding a good location for adding the new explicit function
3460 declarations. To do this, we scan backwards, ignoring whitespace
3461 and comments and other junk until we find either a semicolon, or until
3462 we hit the beginning of the file. */
3463
3464 scan_p = find_rightmost_formals_list (clean_text_p);
3465 for (;; --scan_p)
3466 {
3467 if (scan_p < clean_text_base)
3468 break;
3469 check_source (scan_p > clean_read_ptr, 0);
3470 if (*scan_p == ';')
3471 break;
3472 }
3473
3474 /* scan_p now points either to a semicolon, or to just before the start
3475 of the whole file. */
3476
3477 /* Now scan forward for the first non-whitespace character. In theory,
3478 this should be the first character of the following function definition
3479 header. We will put in the added declarations just prior to that. */
3480
3481 scan_p++;
3482 while (isspace (*scan_p))
3483 scan_p++;
3484 scan_p--;
3485
3486 output_up_to (scan_p);
3487
3488 /* Now write out full prototypes for all of the things that had been
3489 implicitly declared in this file (but only those for which we were
3490 actually able to find unique matching definitions). Avoid duplicates
3491 by marking things that we write out as we go. */
3492
3493 {
3494 int some_decls_added = 0;
3495
3496 for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file)
3497 if (dd_p->is_implicit && dd_p->definition && !dd_p->definition->written)
3498 {
3499 const char *decl = dd_p->definition->ansi_decl;
3500
3501 /* If the function for which we are inserting a declaration is
3502 actually defined later in the same file, then suppress the
3503 leading `extern' keyword (if there is one). */
3504
3505 if (*decl == 'e' && (dd_p->file == dd_p->definition->file))
3506 decl += 7;
3507
3508 output_string ("\n");
3509 output_string (decl);
3510 some_decls_added = 1;
3511 ((NONCONST def_dec_info *) dd_p->definition)->written = 1;
3512 }
3513 if (some_decls_added)
3514 output_string ("\n\n");
3515 }
3516
3517 /* Unmark all of the definitions that we just marked. */
3518
3519 for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file)
3520 if (dd_p->definition)
3521 ((NONCONST def_dec_info *) dd_p->definition)->written = 0;
3522}
3523
3524#endif /* !defined(UNPROTOIZE) */
3525
3526/* Do the editing operation specifically for a function "definition". Note
3527 that editing operations for function "declarations" are handled by a
3528 separate routine above. */
3529
3530static void
34e56753
RS
3531edit_fn_definition (def_dec_p, clean_text_p)
3532 const def_dec_info *def_dec_p;
3533 const char *clean_text_p;
5f8037c4
RS
3534{
3535 const char *end_formals;
3536 const char *function_to_edit = def_dec_p->hash_entry->symbol;
3537
3538 /* Setup here to recover from confusing source code detected during this
3539 particular "edit". */
3540
3541 save_pointers ();
3542 if (setjmp (source_confusion_recovery))
3543 {
3544 restore_pointers ();
3545 fprintf (stderr, "%s: definition of function `%s' not converted\n",
a2b22788 3546 pname, function_to_edit);
5f8037c4
RS
3547 return;
3548 }
3549
3550 end_formals = find_rightmost_formals_list (clean_text_p);
3551
3552 /* end_of_formals now points to the closing right paren of the rightmost
3553 formals list which is actually part of the `header' of the function
3554 definition that we are converting. */
3555
3556 /* If the header of this function definition looks like it declares a
3557 function with a variable number of arguments, and if the way it does
3558 that is different from that way we would like it (i.e. varargs vs.
3559 stdarg) then issue a warning and leave the header unconverted. */
3560
3561 if (other_variable_style_function (def_dec_p->ansi_decl))
3562 {
3563 if (!quiet_flag)
a2b22788
RS
3564 fprintf (stderr, "%s: %d: warning: definition of %s not converted\n",
3565 shortpath (NULL, def_dec_p->file->hash_entry->symbol),
3566 identify_lineno (end_formals),
3567 other_var_style);
5f8037c4
RS
3568 output_up_to (end_formals);
3569 return;
3570 }
3571
3572 if (edit_formals_lists (end_formals, def_dec_p->f_list_count, def_dec_p))
3573 {
3574 restore_pointers ();
3575 fprintf (stderr, "%s: definition of function `%s' not converted\n",
a2b22788 3576 pname, function_to_edit);
5f8037c4
RS
3577 return;
3578 }
3579
3580 /* Have to output the last right paren because this never gets flushed by
3581 edit_formals_list. */
3582
3583 output_up_to (end_formals);
3584
3585#ifdef UNPROTOIZE
3586 {
3587 const char *decl_p;
3588 const char *semicolon_p;
3589 const char *limit_p;
3590 const char *scan_p;
3591 int had_newlines = 0;
3592
3593 /* Now write out the K&R style formal declarations, one per line. */
3594
3595 decl_p = def_dec_p->formal_decls;
3596 limit_p = decl_p + strlen (decl_p);
3597 for (;decl_p < limit_p; decl_p = semicolon_p + 2)
3598 {
3599 for (semicolon_p = decl_p; *semicolon_p != ';'; semicolon_p++)
3600 continue;
3601 output_string ("\n");
3602 output_string (indent_string);
3603 output_bytes (decl_p, (size_t) ((semicolon_p + 1) - decl_p));
3604 }
3605
3606 /* If there are no newlines between the end of the formals list and the
3607 start of the body, we should insert one now. */
3608
3609 for (scan_p = end_formals+1; *scan_p != '{'; )
3610 {
3611 if (*scan_p == '\n')
3612 {
3613 had_newlines = 1;
3614 break;
3615 }
3616 check_source (++scan_p < clean_text_limit, 0);
3617 }
3618 if (!had_newlines)
3619 output_string ("\n");
3620 }
3621#else /* !defined(UNPROTOIZE) */
3622 /* If we are protoizing, there may be some flotsum & jetsum (like comments
3623 and preprocessing directives) after the old formals list but before
3624 the following { and we would like to preserve that stuff while effectively
3625 deleting the existing K&R formal parameter declarations. We do so here
3626 in a rather tricky way. Basically, we white out any stuff *except*
3627 the comments/pp-directives in the original text buffer, then, if there
3628 is anything in this area *other* than whitespace, we output it. */
3629 {
3630 const char *end_formals_orig;
3631 const char *start_body;
3632 const char *start_body_orig;
3633 const char *scan;
3634 const char *scan_orig;
3635 int have_flotsum = 0;
3636 int have_newlines = 0;
3637
3638 for (start_body = end_formals + 1; *start_body != '{';)
3639 check_source (++start_body < clean_text_limit, 0);
3640
3641 end_formals_orig = orig_text_base + (end_formals - clean_text_base);
3642 start_body_orig = orig_text_base + (start_body - clean_text_base);
3643 scan = end_formals + 1;
3644 scan_orig = end_formals_orig + 1;
3645 for (; scan < start_body; scan++, scan_orig++)
3646 {
3647 if (*scan == *scan_orig)
3648 {
3649 have_newlines |= (*scan_orig == '\n');
3650 /* Leave identical whitespace alone. */
3651 if (!isspace (*scan_orig))
3652 *((NONCONST char *)scan_orig) = ' '; /* identical - so whiteout */
3653 }
3654 else
3655 have_flotsum = 1;
3656 }
3657 if (have_flotsum)
3658 output_bytes (end_formals_orig + 1,
3659 (size_t) (start_body_orig - end_formals_orig) - 1);
3660 else
3661 if (have_newlines)
3662 output_string ("\n");
3663 else
3664 output_string (" ");
3665 clean_read_ptr = start_body - 1;
3666 }
3667#endif /* !defined(UNPROTOIZE) */
3668}
3669
3670/* Clean up the clean text buffer. Do this by converting comments and
3671 preprocessor directives into spaces. Also convert line continuations
3672 into whitespace. Also, whiteout string and character literals. */
3673
3674static void
34e56753
RS
3675do_cleaning (new_clean_text_base, new_clean_text_limit)
3676 char *new_clean_text_base;
3677 char *new_clean_text_limit;
5f8037c4
RS
3678{
3679 char *scan_p;
3680 int non_whitespace_since_newline = 0;
3681
3682 for (scan_p = new_clean_text_base; scan_p < new_clean_text_limit; scan_p++)
3683 {
3684 switch (*scan_p)
3685 {
3686 case '/': /* Handle comments. */
3687 if (scan_p[1] != '*')
3688 goto regular;
3689 non_whitespace_since_newline = 1;
3690 scan_p[0] = ' ';
3691 scan_p[1] = ' ';
3692 scan_p += 2;
3693 while (scan_p[1] != '/' || scan_p[0] != '*')
3694 {
3695 if (!isspace (*scan_p))
3696 *scan_p = ' ';
3697 if (++scan_p >= new_clean_text_limit)
3698 abort ();
3699 }
3700 *scan_p++ = ' ';
3701 *scan_p = ' ';
3702 break;
3703
3704 case '#': /* Handle pp directives. */
3705 if (non_whitespace_since_newline)
3706 goto regular;
3707 *scan_p = ' ';
3708 while (scan_p[1] != '\n' || scan_p[0] == '\\')
3709 {
3710 if (!isspace (*scan_p))
3711 *scan_p = ' ';
3712 if (++scan_p >= new_clean_text_limit)
3713 abort ();
3714 }
3715 *scan_p++ = ' ';
3716 break;
3717
3718 case '\'': /* Handle character literals. */
3719 non_whitespace_since_newline = 1;
3720 while (scan_p[1] != '\'' || scan_p[0] == '\\')
3721 {
3722 if (scan_p[0] == '\\' && !isspace (scan_p[1]))
3723 scan_p[1] = ' ';
3724 if (!isspace (*scan_p))
3725 *scan_p = ' ';
3726 if (++scan_p >= new_clean_text_limit)
3727 abort ();
3728 }
3729 *scan_p++ = ' ';
3730 break;
3731
3732 case '"': /* Handle string literals. */
3733 non_whitespace_since_newline = 1;
3734 while (scan_p[1] != '"' || scan_p[0] == '\\')
3735 {
3736 if (scan_p[0] == '\\' && !isspace (scan_p[1]))
3737 scan_p[1] = ' ';
3738 if (!isspace (*scan_p))
3739 *scan_p = ' ';
3740 if (++scan_p >= new_clean_text_limit)
3741 abort ();
3742 }
3743 *scan_p++ = ' ';
3744 break;
3745
3746 case '\\': /* Handle line continuations. */
3747 if (scan_p[1] != '\n')
3748 goto regular;
3749 *scan_p = ' ';
3750 break;
3751
3752 case '\n':
3753 non_whitespace_since_newline = 0; /* Reset. */
3754 break;
3755
3756 case ' ':
3757 case '\v':
3758 case '\t':
3759 case '\r':
3760 case '\f':
3761 case '\b':
3762 break; /* Whitespace characters. */
3763
3764 default:
3765regular:
3766 non_whitespace_since_newline = 1;
3767 break;
3768 }
3769 }
3770}
3771
3772/* Given a pointer to the closing right parenthesis for a particular formals
3773 list (in the clean text buffer) find the corresponding left parenthesis
3774 and return a pointer to it. */
3775
3776static const char *
34e56753
RS
3777careful_find_l_paren (p)
3778 const char *p;
5f8037c4
RS
3779{
3780 const char *q;
3781 int paren_depth;
3782
3783 for (paren_depth = 1, q = p-1; paren_depth; check_source (--q >= clean_text_base, 0))
3784 {
3785 switch (*q)
3786 {
3787 case ')':
3788 paren_depth++;
3789 break;
3790 case '(':
3791 paren_depth--;
3792 break;
3793 }
3794 }
3795 return ++q;
3796}
3797
3798/* Scan the clean text buffer for cases of function definitions that we
3799 don't really know about because they were preprocessed out when the
3800 aux info files were created.
3801
3802 In this version of protoize/unprotoize we just give a warning for each
3803 one found. A later version may be able to at least unprotoize such
3804 missed items.
3805
3806 Note that we may easily find all function definitions simply by
3807 looking for places where there is a left paren which is (ignoring
3808 whitespace) immediately followed by either a left-brace or by an
3809 upper or lower case letter. Whenever we find this combination, we
3810 have also found a function definition header.
3811
3812 Finding function *declarations* using syntactic clues is much harder.
3813 I will probably try to do this in a later version though. */
3814
3815static void
34e56753
RS
3816scan_for_missed_items (file_p)
3817 const file_info *file_p;
5f8037c4
RS
3818{
3819 static const char *scan_p;
3820 const char *limit = clean_text_limit - 3;
3821 static const char *backup_limit;
3822
3823 backup_limit = clean_text_base - 1;
3824
3825 for (scan_p = clean_text_base; scan_p < limit; scan_p++)
3826 {
3827 if (*scan_p == ')')
3828 {
3829 static const char *last_r_paren;
3830 const char *ahead_p;
3831
3832 last_r_paren = scan_p;
3833
3834 for (ahead_p = scan_p + 1; isspace (*ahead_p); )
3835 check_source (++ahead_p < limit, limit);
3836
3837 scan_p = ahead_p - 1;
3838
3839 if (isalpha (*ahead_p) || *ahead_p == '{')
3840 {
3841 const char *last_l_paren;
3842 const int lineno = identify_lineno (ahead_p);
3843
3844 if (setjmp (source_confusion_recovery))
3845 continue;
3846
3847 /* We know we have a function definition header. Now skip
3848 leftwards over all of its associated formals lists. */
3849
3850 do
3851 {
3852 last_l_paren = careful_find_l_paren (last_r_paren);
3853 for (last_r_paren = last_l_paren-1; isspace (*last_r_paren); )
3854 check_source (--last_r_paren >= backup_limit, backup_limit);
3855 }
3856 while (*last_r_paren == ')');
3857
3858 if (is_id_char (*last_r_paren))
3859 {
3860 const char *id_limit = last_r_paren + 1;
3861 const char *id_start;
3862 size_t id_length;
3863 const def_dec_info *dd_p;
3864
3865 for (id_start = id_limit-1; is_id_char (*id_start); )
3866 check_source (--id_start >= backup_limit, backup_limit);
3867 id_start++;
3868 backup_limit = id_start;
3869 if ((id_length = (size_t) (id_limit - id_start)) == 0)
3870 goto not_missed;
3871
3872 {
34e56753 3873 char *func_name = (char *) alloca (id_length + 1);
5f8037c4
RS
3874 static const char * const stmt_keywords[]
3875 = { "if", "while", "for", "switch", "return", 0 };
3876 const char * const *stmt_keyword;
3877
3878 strncpy (func_name, id_start, id_length);
3879 func_name[id_length] = '\0';
3880
3881 /* We must check here to see if we are actually looking at
3882 a statement rather than an actual function call. */
3883
3884 for (stmt_keyword = stmt_keywords; *stmt_keyword; stmt_keyword++)
3885 if (!strcmp (func_name, *stmt_keyword))
3886 goto not_missed;
3887
3888#if 0
3889 fprintf (stderr, "%s: found definition of `%s' at %s(%d)\n",
3890 pname,
3891 func_name,
3892 shortpath (NULL, file_p->hash_entry->symbol),
3893 identify_lineno (id_start));
3894#endif /* 0 */
3895 /* We really should check for a match of the function name
3896 here also, but why bother. */
3897
3898 for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file)
3899 if (dd_p->is_func_def && dd_p->line == lineno)
3900 goto not_missed;
3901
3902 /* If we make it here, then we did not know about this
3903 function definition. */
3904
a2b22788 3905 fprintf (stderr, "%s: %d: warning: `%s' was #if 0\n",
5f8037c4 3906 shortpath (NULL, file_p->hash_entry->symbol),
a2b22788 3907 identify_lineno (id_start), func_name);
5f8037c4
RS
3908 fprintf (stderr, "%s: function definition not converted\n",
3909 pname);
3910 }
3911 not_missed: ;
3912 }
3913 }
3914 }
3915 }
3916}
3917
3918/* Do all editing operations for a single source file (either a "base" file
3919 or an "include" file). To do this we read the file into memory, keep a
3920 virgin copy there, make another cleaned in-core copy of the original file
3921 (i.e. one in which all of the comments and preprocessor directives have
3922 been replaced with whitespace), then use these two in-core copies of the
3923 file to make a new edited in-core copy of the file. Finally, rename the
3924 original file (as a way of saving it), and then write the edited version
3925 of the file from core to a disk file of the same name as the original.
3926
3927 Note that the trick of making a copy of the original sans comments &
3928 preprocessor directives make the editing a whole lot easier. */
3929
3930static void
34e56753
RS
3931edit_file (hp)
3932 const hash_table_entry *hp;
5f8037c4
RS
3933{
3934 struct stat stat_buf;
3935 const file_info *file_p = hp->fip;
3936 char *new_orig_text_base;
3937 char *new_orig_text_limit;
3938 char *new_clean_text_base;
3939 char *new_clean_text_limit;
3940 size_t orig_size;
3941 size_t repl_size;
3942 int first_definition_in_file;
3943
3944 /* If we are not supposed to be converting this file, or if there is
3945 nothing in there which needs converting, just skip this file. */
3946
3947 if (!needs_to_be_converted (file_p))
3948 return;
3949
a2b22788 3950 convert_filename = file_p->hash_entry->symbol;
5f8037c4
RS
3951
3952 /* Convert a file if it is in a directory where we want conversion
3953 and the file is not excluded. */
3954
a2b22788
RS
3955 if (!directory_specified_p (convert_filename)
3956 || file_excluded_p (convert_filename))
5f8037c4
RS
3957 {
3958 if (!quiet_flag
3959#ifdef UNPROTOIZE
3960 /* Don't even mention "system" include files unless we are
3961 protoizing. If we are protoizing, we mention these as a
3962 gentile way of prodding the user to convert his "system"
3963 include files to prototype format. */
a2b22788 3964 && !in_system_include_dir (convert_filename)
5f8037c4
RS
3965#endif /* defined(UNPROTOIZE) */
3966 )
3967 fprintf (stderr, "%s: file `%s' not converted\n",
a2b22788 3968 pname, shortpath (NULL, convert_filename));
5f8037c4
RS
3969 return;
3970 }
3971
3972 /* Let the user know what we are up to. */
3973
3974 if (nochange_flag)
34e56753
RS
3975 fprintf (stderr, "%s: would convert file `%s'\n",
3976 pname, shortpath (NULL, convert_filename));
5f8037c4 3977 else
34e56753
RS
3978 fprintf (stderr, "%s: converting file `%s'\n",
3979 pname, shortpath (NULL, convert_filename));
3980 fflush (stderr);
5f8037c4
RS
3981
3982 /* Find out the size (in bytes) of the original file. */
3983
a2b22788 3984 /* The cast avoids an erroneous warning on AIX. */
34e56753 3985 if (my_stat ((char *)convert_filename, &stat_buf) == -1)
5f8037c4
RS
3986 {
3987 fprintf (stderr, "%s: error: can't get status for file `%s': %s\n",
a2b22788 3988 pname, shortpath (NULL, convert_filename), sys_errlist[errno]);
5f8037c4
RS
3989 return;
3990 }
3991 orig_size = stat_buf.st_size;
3992
3993 /* Allocate a buffer to hold the original text. */
3994
3995 orig_text_base = new_orig_text_base = (char *) xmalloc (orig_size + 2);
3996 orig_text_limit = new_orig_text_limit = new_orig_text_base + orig_size;
3997
3998 /* Allocate a buffer to hold the cleaned-up version of the original text. */
3999
4000 clean_text_base = new_clean_text_base = (char *) xmalloc (orig_size + 2);
4001 clean_text_limit = new_clean_text_limit = new_clean_text_base + orig_size;
4002 clean_read_ptr = clean_text_base - 1;
4003
4004 /* Allocate a buffer that will hopefully be large enough to hold the entire
4005 converted output text. As an initial guess for the maximum size of the
4006 output buffer, use 125% of the size of the original + some extra. This
4007 buffer can be expanded later as needed. */
4008
4009 repl_size = orig_size + (orig_size >> 2) + 4096;
4010 repl_text_base = (char *) xmalloc (repl_size + 2);
4011 repl_text_limit = repl_text_base + repl_size - 1;
4012 repl_write_ptr = repl_text_base - 1;
4013
4014 {
4015 int input_file;
4016
4017 /* Open the file to be converted in READ ONLY mode. */
4018
34e56753 4019 if ((input_file = my_open (convert_filename, O_RDONLY, 0444)) == -1)
5f8037c4
RS
4020 {
4021 fprintf (stderr, "%s: error: can't open file `%s' for reading: %s\n",
a2b22788
RS
4022 pname, shortpath (NULL, convert_filename),
4023 sys_errlist[errno]);
5f8037c4
RS
4024 return;
4025 }
4026
4027 /* Read the entire original source text file into the original text buffer
4028 in one swell fwoop. Then figure out where the end of the text is and
4029 make sure that it ends with a newline followed by a null. */
4030
4031 if (read (input_file, new_orig_text_base, orig_size) != orig_size)
4032 {
4033 close (input_file);
4034 fprintf (stderr, "\n%s: error: while reading input file `%s': %s\n",
a2b22788
RS
4035 pname, shortpath (NULL, convert_filename),
4036 sys_errlist[errno]);
5f8037c4
RS
4037 return;
4038 }
4039
4040 close (input_file);
4041 }
4042
4043 if (orig_size == 0 || orig_text_limit[-1] != '\n')
4044 {
4045 *new_orig_text_limit++ = '\n';
4046 orig_text_limit++;
4047 }
4048
4049 /* Create the cleaned up copy of the original text. */
4050
4051 memcpy (new_clean_text_base, orig_text_base,
4052 (size_t) (orig_text_limit - orig_text_base));
4053 do_cleaning (new_clean_text_base, new_clean_text_limit);
4054
4055#if 0
4056 {
4057 int clean_file;
4058 size_t clean_size = orig_text_limit - orig_text_base;
a2b22788 4059 char *const clean_filename = (char *) alloca (strlen (convert_filename) + 6 + 1);
5f8037c4
RS
4060
4061 /* Open (and create) the clean file. */
4062
a2b22788
RS
4063 strcpy (clean_filename, convert_filename);
4064 strcat (clean_filename, ".clean");
4065 if ((clean_file = creat (clean_filename, 0666)) == -1)
5f8037c4
RS
4066 {
4067 fprintf (stderr, "%s: error: can't create/open clean file `%s': %s\n",
a2b22788
RS
4068 pname, shortpath (NULL, clean_filename),
4069 sys_errlist[errno]);
5f8037c4
RS
4070 return;
4071 }
4072
4073 /* Write the clean file. */
4074
4075 if (write (clean_file, new_clean_text_base, clean_size) != clean_size)
4076 fprintf (stderr, "%s: error: while writing file `%s': %s\n",
a2b22788 4077 pname, shortpath (NULL, clean_filename), sys_errlist[errno]);
5f8037c4
RS
4078
4079 close (clean_file);
4080 }
4081#endif /* 0 */
4082
4083 /* Do a simplified scan of the input looking for things that were not
4084 mentioned in the aux info files because of the fact that they were
4085 in a region of the source which was preprocessed-out (via #if or
4086 via #ifdef). */
4087
4088 scan_for_missed_items (file_p);
4089
4090 /* Setup to do line-oriented forward seeking in the clean text buffer. */
4091
4092 last_known_line_number = 1;
4093 last_known_line_start = clean_text_base;
4094
4095 /* Now get down to business and make all of the necessary edits. */
4096
4097 {
4098 const def_dec_info *def_dec_p;
4099
4100 first_definition_in_file = 1;
4101 def_dec_p = file_p->defs_decs;
4102 for (; def_dec_p; def_dec_p = def_dec_p->next_in_file)
4103 {
4104 const char *clean_text_p = seek_to_line (def_dec_p->line);
4105
4106 /* clean_text_p now points to the first character of the line which
4107 contains the `terminator' for the declaration or definition that
4108 we are about to process. */
4109
4110#ifndef UNPROTOIZE
4111
4112 if (global_flag && def_dec_p->is_func_def && first_definition_in_file)
4113 {
4114 add_global_decls (def_dec_p->file, clean_text_p);
4115 first_definition_in_file = 0;
4116 }
4117
4118 /* Don't edit this item if it is already in prototype format or if it
4119 is a function declaration and we have found no corresponding
4120 definition. */
4121
4122 if (def_dec_p->prototyped
4123 || (!def_dec_p->is_func_def && !def_dec_p->definition))
4124 continue;
4125
4126#endif /* !defined(UNPROTOIZE) */
4127
4128 if (def_dec_p->is_func_def)
4129 edit_fn_definition (def_dec_p, clean_text_p);
4130 else
4131#ifndef UNPROTOIZE
4132 if (def_dec_p->is_implicit)
4133 add_local_decl (def_dec_p, clean_text_p);
4134 else
4135#endif /* !defined(UNPROTOIZE) */
4136 edit_fn_declaration (def_dec_p, clean_text_p);
4137 }
4138 }
4139
4140 /* Finalize things. Output the last trailing part of the original text. */
4141
4142 output_up_to (clean_text_limit - 1);
4143
4144 /* If this is just a test run, stop now and just deallocate the buffers. */
4145
4146 if (nochange_flag)
4147 {
4148 free (new_orig_text_base);
4149 free (new_clean_text_base);
4150 free (repl_text_base);
4151 return;
4152 }
4153
4154 /* Change the name of the original input file. This is just a quick way of
4155 saving the original file. */
4156
4157 if (!nosave_flag)
4158 {
a2b22788
RS
4159 char *new_filename =
4160 (char *) xmalloc (strlen (convert_filename) + strlen (save_suffix) + 2);
5f8037c4 4161
a2b22788
RS
4162 strcpy (new_filename, convert_filename);
4163 strcat (new_filename, save_suffix);
34e56753 4164 if (my_link (convert_filename, new_filename) == -1)
5f8037c4
RS
4165 {
4166 if (errno == EEXIST)
4167 {
4168 if (!quiet_flag)
4169 fprintf (stderr, "%s: warning: file `%s' already saved in `%s'\n",
a2b22788
RS
4170 pname,
4171 shortpath (NULL, convert_filename),
4172 shortpath (NULL, new_filename));
5f8037c4
RS
4173 }
4174 else
4175 {
4176 fprintf (stderr, "%s: error: can't link file `%s' to `%s': %s\n",
a2b22788
RS
4177 pname,
4178 shortpath (NULL, convert_filename),
4179 shortpath (NULL, new_filename),
4180 sys_errlist[errno]);
5f8037c4
RS
4181 return;
4182 }
4183 }
4184 }
4185
34e56753 4186 if (my_unlink (convert_filename) == -1)
5f8037c4
RS
4187 {
4188 fprintf (stderr, "%s: error: can't delete file `%s': %s\n",
a2b22788 4189 pname, shortpath (NULL, convert_filename), sys_errlist[errno]);
5f8037c4
RS
4190 return;
4191 }
4192
4193 {
4194 int output_file;
4195
4196 /* Open (and create) the output file. */
4197
a2b22788 4198 if ((output_file = creat (convert_filename, 0666)) == -1)
5f8037c4
RS
4199 {
4200 fprintf (stderr, "%s: error: can't create/open output file `%s': %s\n",
a2b22788
RS
4201 pname, shortpath (NULL, convert_filename),
4202 sys_errlist[errno]);
5f8037c4
RS
4203 return;
4204 }
4205
4206 /* Write the output file. */
4207
4208 {
4209 unsigned int out_size = (repl_write_ptr + 1) - repl_text_base;
4210
4211 if (write (output_file, repl_text_base, out_size) != out_size)
4212 fprintf (stderr, "%s: error: while writing file `%s': %s\n",
a2b22788
RS
4213 pname, shortpath (NULL, convert_filename),
4214 sys_errlist[errno]);
5f8037c4
RS
4215 }
4216
4217 close (output_file);
4218 }
4219
4220 /* Deallocate the conversion buffers. */
4221
4222 free (new_orig_text_base);
4223 free (new_clean_text_base);
4224 free (repl_text_base);
4225
4226 /* Change the mode of the output file to match the original file. */
4227
a2b22788 4228 /* The cast avoids an erroneous warning on AIX. */
34e56753 4229 if (my_chmod ((char *)convert_filename, stat_buf.st_mode) == -1)
5f8037c4 4230 fprintf (stderr, "%s: error: can't change mode of file `%s': %s\n",
a2b22788 4231 pname, shortpath (NULL, convert_filename), sys_errlist[errno]);
5f8037c4
RS
4232
4233 /* Note: We would try to change the owner and group of the output file
4234 to match those of the input file here, except that may not be a good
4235 thing to do because it might be misleading. Also, it might not even
4236 be possible to do that (on BSD systems with quotas for instance). */
4237}
4238
4239/* Do all of the individual steps needed to do the protoization (or
4240 unprotoization) of the files referenced in the aux_info files given
4241 in the command line. */
4242
4243static void
34e56753 4244do_processing ()
5f8037c4
RS
4245{
4246 const char * const *base_pp;
a2b22788
RS
4247 const char * const * const end_pps
4248 = &base_source_filenames[n_base_source_files];
5f8037c4
RS
4249
4250#ifndef UNPROTOIZE
4251 int syscalls_len;
4252#endif /* !defined(UNPROTOIZE) */
4253
4254 /* One-by-one, check (and create if necessary), open, and read all of the
4255 stuff in each aux_info file. After reading each aux_info file, the
4256 aux_info_file just read will be automatically deleted unless the
4257 keep_flag is set. */
4258
a2b22788 4259 for (base_pp = base_source_filenames; base_pp < end_pps; base_pp++)
5f8037c4
RS
4260 process_aux_info_file (*base_pp, keep_flag, 0);
4261
4262#ifndef UNPROTOIZE
4263
4264 /* Also open and read the special SYSCALLS.c aux_info file which gives us
4265 the prototypes for all of the standard system-supplied functions. */
4266
4267 if (nondefault_syscalls_dir)
4268 {
a2b22788 4269 syscalls_absolute_filename
5f8037c4 4270 = (char *) xmalloc (strlen (nondefault_syscalls_dir)
34e56753 4271 + strlen (syscalls_filename) + 2);
a2b22788 4272 strcpy (syscalls_absolute_filename, nondefault_syscalls_dir);
5f8037c4
RS
4273 }
4274 else
4275 {
a2b22788 4276 syscalls_absolute_filename
5f8037c4 4277 = (char *) xmalloc (strlen (default_syscalls_dir)
34e56753 4278 + strlen (syscalls_filename) + 2);
a2b22788 4279 strcpy (syscalls_absolute_filename, default_syscalls_dir);
5f8037c4
RS
4280 }
4281
34e56753 4282 syscalls_len = strlen (syscalls_absolute_filename);
a2b22788 4283 if (*(syscalls_absolute_filename + syscalls_len - 1) != '/')
5f8037c4 4284 {
a2b22788
RS
4285 *(syscalls_absolute_filename + syscalls_len++) = '/';
4286 *(syscalls_absolute_filename + syscalls_len) = '\0';
5f8037c4 4287 }
a2b22788 4288 strcat (syscalls_absolute_filename, syscalls_filename);
5f8037c4
RS
4289
4290 /* Call process_aux_info_file in such a way that it does not try to
4291 delete the SYSCALLS aux_info file. */
4292
a2b22788 4293 process_aux_info_file (syscalls_absolute_filename, 1, 1);
5f8037c4
RS
4294
4295#endif /* !defined(UNPROTOIZE) */
4296
4297 /* When we first read in all of the information from the aux_info files
4298 we saved in it decending line number order, because that was likely to
4299 be faster. Now however, we want the chains of def & dec records to
4300 appear in ascending line number order as we get further away from the
4301 file_info record that they hang from. The following line causes all of
4302 these lists to be rearranged into ascending line number order. */
4303
a2b22788 4304 visit_each_hash_node (filename_primary, reverse_def_dec_list);
5f8037c4
RS
4305
4306#ifndef UNPROTOIZE
4307
4308 /* Now do the "real" work. The following line causes each declaration record
4309 to be "visited". For each of these nodes, an attempt is made to match
4310 up the function declaration with a corresponding function definition,
4311 which should have a full prototype-format formals list with it. Once
4312 these match-ups are made, the conversion of the function declarations
4313 to prototype format can be made. */
4314
4315 visit_each_hash_node (function_name_primary, connect_defs_and_decs);
4316
4317#endif /* !defined(UNPROTOIZE) */
4318
4319 /* Now convert each file that can be converted (and needs to be). */
4320
a2b22788 4321 visit_each_hash_node (filename_primary, edit_file);
5f8037c4
RS
4322
4323#ifndef UNPROTOIZE
4324
4325 /* If we are working in cplusplus mode, try to rename all .c files to .C
4326 files. Don't panic if some of the renames don't work. */
4327
4328 if (cplusplus_flag && !nochange_flag)
a2b22788 4329 visit_each_hash_node (filename_primary, rename_c_file);
5f8037c4
RS
4330
4331#endif /* !defined(UNPROTOIZE) */
4332}
4333\f
4334static struct option longopts[] =
4335{
4336 {"version", 0, 0, 'V'},
ef91d7e2 4337 {"pathname", 0, 0, 'p'},
5f8037c4
RS
4338 {"quiet", 0, 0, 'q'},
4339 {"silent", 0, 0, 'q'},
4340 {"force", 0, 0, 'f'},
4341 {"keep", 0, 0, 'k'},
4342 {"nosave", 0, 0, 'N'},
4343 {"nochange", 0, 0, 'n'},
4344 {"compiler-options", 1, 0, 'c'},
4345 {"exclude", 1, 0, 'x'},
4346 {"directory", 1, 0, 'd'},
4347#ifdef UNPROTOIZE
4348 {"indent", 1, 0, 'i'},
4349#else
4350 {"local", 0, 0, 'l'},
4351 {"global", 0, 0, 'g'},
4352 {"c++", 0, 0, 'C'},
4353 {"syscalls-dir", 1, 0, 'B'},
4354#endif
4355 {0, 0, 0, 0}
4356};
4357
4358int
34e56753
RS
4359main (argc, argv)
4360 int argc;
4361 char **const argv;
5f8037c4
RS
4362{
4363 int longind;
4364 int c;
4365 int size;
4366
4367 pname = strrchr (argv[0], '/');
4368 pname = pname ? pname+1 : argv[0];
4369
4370 /* Read the working directory, avoiding arbitrary limit. */
34e56753 4371 size = GUESSPATHLEN;
5f8037c4
RS
4372 while (1)
4373 {
a2b22788 4374 char *value;
5f8037c4
RS
4375
4376 cwd_buffer = (char *) xmalloc (size);
4377 value = getcwd (cwd_buffer, size);
a2b22788 4378 if (value != 0 || errno != ERANGE)
5f8037c4
RS
4379 break;
4380 free (cwd_buffer);
4381 size *= 2;
4382 }
4383
4384 /* By default, convert the files in the current directory. */
4385 directory_list = string_list_cons (cwd_buffer, NULL);
4386
4387 while ((c = getopt_long (argc, argv,
4388#ifdef UNPROTOIZE
ef91d7e2 4389 "c:d:i:knNp:qVx:",
5f8037c4 4390#else
ef91d7e2 4391 "B:c:Cd:gklnNp:qVx:",
5f8037c4
RS
4392#endif
4393 longopts, &longind)) != EOF)
4394 {
4395 if (c == 0) /* Long option. */
4396 c = longopts[longind].val;
4397 switch (c)
4398 {
ef91d7e2
RS
4399 case 'p':
4400 compiler_pathname = optarg;
4401 break;
5f8037c4
RS
4402 case 'd':
4403 directory_list
4404 = string_list_cons (abspath (NULL, optarg), directory_list);
4405 break;
4406 case 'x':
4407 exclude_list = string_list_cons (optarg, exclude_list);
4408 break;
4409
4410 case 'V':
4411 version_flag = 1;
4412 break;
4413 case 'q':
4414 quiet_flag = 1;
4415 break;
4416#if 0
4417 case 'f':
4418 force_flag = 1;
4419 break;
4420#endif
4421 case 'n':
4422 nochange_flag = 1;
4423 keep_flag = 1;
4424 break;
4425 case 'N':
4426 nosave_flag = 1;
4427 break;
4428 case 'k':
4429 keep_flag = 1;
4430 break;
4431 case 'c':
4432 munge_compile_params (optarg);
4433 break;
4434#ifdef UNPROTOIZE
4435 case 'i':
4436 indent_string = optarg;
4437 break;
4438#else /* !defined(UNPROTOIZE) */
4439 case 'l':
4440 local_flag = 1;
4441 break;
4442 case 'g':
4443 global_flag = 1;
4444 break;
4445 case 'C':
4446 cplusplus_flag = 1;
4447 break;
4448 case 'B':
4449 nondefault_syscalls_dir = optarg;
4450 break;
4451#endif /* !defined(UNPROTOIZE) */
4452 default:
4453 usage ();
4454 }
4455 }
4456
a2b22788 4457 n_base_source_files = argc - optind;
5f8037c4 4458
a2b22788 4459 /* Now actually make a list of the base source filenames. */
5f8037c4 4460
a2b22788
RS
4461 base_source_filenames =
4462 (const char **) xmalloc ((n_base_source_files + 1) * sizeof (char *));
4463 n_base_source_files = 0;
5f8037c4
RS
4464 for (; optind < argc; optind++)
4465 {
4466 const char *path = abspath (NULL, argv[optind]);
4467 int len = strlen (path);
4468
4469 if (path[len-1] == 'c' && path[len-2] == '.')
a2b22788 4470 base_source_filenames[n_base_source_files++] = path;
5f8037c4
RS
4471 else
4472 {
a2b22788 4473 fprintf (stderr, "%s: input file names must have .c suffixes: %s\n",
5f8037c4
RS
4474 pname, shortpath (NULL, path));
4475 errors++;
4476 }
4477 }
4478
4479#ifndef UNPROTOIZE
4480 /* We are only interested in the very first identifier token in the
4481 definition of `va_list', so if there is more junk after that first
4482 identifier token, delete it from the `varargs_style_indicator'. */
4483 {
4484 const char *cp;
4485
4486 for (cp = varargs_style_indicator; isalnum (*cp) || *cp == '_'; cp++)
4487 continue;
4488 if (*cp != 0)
4489 varargs_style_indicator = savestring (varargs_style_indicator,
4490 cp - varargs_style_indicator);
4491 }
4492#endif /* !defined(UNPROTOIZE) */
4493
4494 if (errors)
4495 usage ();
4496 else
4497 {
4498 if (version_flag)
4499 fprintf (stderr, "%s: %s\n", pname, version_string);
4500 do_processing ();
4501 }
4502 if (errors)
4503 exit (1);
4504 else
4505 exit (0);
4506 return 1;
4507}