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