protoize.c 138 KB
Newer Older
Richard Kenner committed
1
/* Protoize program - Original version by Ron Guilmette (rfg@segfault.us.com).
Jeff Law committed
2
   Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3
   1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
Richard Stallman committed
4

5
This file is part of GCC.
Richard Stallman committed
6

7 8 9 10
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
Richard Stallman committed
11

12 13 14 15
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
Richard Stallman committed
16 17

You should have received a copy of the GNU General Public License
18 19 20
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */
Richard Stallman committed
21

22
#include "config.h"
23
#include "system.h"
24 25
#include "coretypes.h"
#include "tm.h"
26
#include "intl.h"
27
#include "cppdefault.h"
28

Richard Stallman committed
29
#include <setjmp.h>
30
#include <signal.h>
31 32 33
#if ! defined( SIGCHLD ) && defined( SIGCLD )
#  define SIGCHLD SIGCLD
#endif
34 35 36
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
37
#undef abort
38
#include "version.h"
39

40
/* Include getopt.h for the sake of getopt_long.  */
Richard Stallman committed
41 42
#include "getopt.h"

43 44
/* Macro to see if the path elements match.  */
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
Kaveh R. Ghazi committed
45
#define IS_SAME_PATH_CHAR(a,b) (TOUPPER (a) == TOUPPER (b))
46 47 48 49 50
#else
#define IS_SAME_PATH_CHAR(a,b) ((a) == (b))
#endif

/* Macro to see if the paths match.  */
51
#define IS_SAME_PATH(a,b) (FILENAME_CMP (a, b) == 0)
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66
/* Suffix for aux-info files.  */
#ifdef __MSDOS__
#define AUX_INFO_SUFFIX "X"
#else
#define AUX_INFO_SUFFIX ".X"
#endif

/* Suffix for saved files.  */
#ifdef __MSDOS__
#define SAVE_SUFFIX "sav"
#else
#define SAVE_SUFFIX ".save"
#endif

67 68 69 70 71 72 73
/* Suffix for renamed C++ files.  */
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
#define CPLUS_FILE_SUFFIX "cc"
#else
#define CPLUS_FILE_SUFFIX "C"
#endif

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static void usage (void) ATTRIBUTE_NORETURN;
static void aux_info_corrupted (void) ATTRIBUTE_NORETURN;
static void declare_source_confusing (const char *) ATTRIBUTE_NORETURN;
static const char *shortpath (const char *, const char *);
static void notice (const char *, ...) ATTRIBUTE_PRINTF_1;
static char *savestring (const char *, unsigned int);
static char *dupnstr (const char *, size_t);
static int safe_read (int, void *, int);
static void safe_write (int, void *, int, const char *);
static void save_pointers (void);
static void restore_pointers (void);
static int is_id_char (int);
static int in_system_include_dir (const char *);
static int directory_specified_p (const char *);
static int file_excluded_p (const char *);
static char *unexpand_if_needed (const char *);
static char *abspath (const char *, const char *);
static void check_aux_info (int);
static const char *find_corresponding_lparen (const char *);
static int referenced_file_is_newer (const char *, time_t);
static void save_def_or_dec (const char *, int);
static void munge_compile_params (const char *);
static int gen_aux_info_file (const char *);
static void process_aux_info_file (const char *, int, int);
static int identify_lineno (const char *);
static void check_source (int, const char *);
static const char *seek_to_line (int);
static const char *forward_to_next_token_char (const char *);
static void output_bytes (const char *, size_t);
static void output_string (const char *);
static void output_up_to (const char *);
static int other_variable_style_function (const char *);
static const char *find_rightmost_formals_list (const char *);
static void do_cleaning (char *, const char *);
static const char *careful_find_l_paren (const char *);
static void do_processing (void);
Richard Stallman committed
110 111 112 113 114 115

/* Look for these where the `const' qualifier is intentionally cast aside.  */
#define NONCONST

/* Define a default place to find the SYSCALLS.X file.  */

116 117 118 119 120 121
#ifndef UNPROTOIZE

#ifndef STANDARD_EXEC_PREFIX
#define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
#endif /* !defined STANDARD_EXEC_PREFIX */

122 123 124
static const char * const standard_exec_prefix = STANDARD_EXEC_PREFIX;
static const char * const target_machine = DEFAULT_TARGET_MACHINE;
static const char * const target_version = DEFAULT_TARGET_VERSION;
125 126

#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
127 128 129

/* Suffix of aux_info files.  */

130
static const char * const aux_info_suffix = AUX_INFO_SUFFIX;
Richard Stallman committed
131

132
/* String to attach to filenames for saved versions of original files.  */
Richard Stallman committed
133

134
static const char * const save_suffix = SAVE_SUFFIX;
Richard Stallman committed
135

136 137
#ifndef UNPROTOIZE

138 139 140 141
/* String to attach to C filenames renamed to C++.  */

static const char * const cplus_suffix = CPLUS_FILE_SUFFIX;

Richard Stallman committed
142 143 144 145
/* File name of the file which contains descriptions of standard system
   routines.  Note that we never actually do anything with this file per se,
   but we do read in its corresponding aux_info file.  */

146
static const char syscalls_filename[] = "SYSCALLS.c";
Richard Stallman committed
147 148 149

/* Default place to find the above file.  */

150
static const char * default_syscalls_dir;
Richard Stallman committed
151

152
/* Variable to hold the complete absolutized filename of the SYSCALLS.c.X
Richard Stallman committed
153 154
   file.  */

155
static char * syscalls_absolute_filename;
Richard Stallman committed
156

157
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
158

Mike Stump committed
159
/* Type of the structure that holds information about macro unexpansions.  */
Richard Stallman committed
160 161

struct unexpansion_struct {
162 163
  const char *const expanded;
  const char *const contracted;
Richard Stallman committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
};
typedef struct unexpansion_struct unexpansion;

/* A table of conversions that may need to be made for some (stupid) older
   operating systems where these types are preprocessor macros rather than
   typedefs (as they really ought to be).

   WARNING: The contracted forms must be as small (or smaller) as the
   expanded forms, or else havoc will ensue.  */

static const unexpansion unexpansions[] = {
  { "struct _iobuf", "FILE" },
  { 0, 0 }
};

/* The number of "primary" slots in the hash tables for filenames and for
   function names.  This can be as big or as small as you like, except that
   it must be a power of two.  */

#define HASH_TABLE_SIZE		(1 << 9)

/* Bit mask to use when computing hash values.  */

static const int hash_mask = (HASH_TABLE_SIZE - 1);


/* Datatype for lists of directories or filenames.  */
struct string_list
{
193
  const char *name;
Richard Stallman committed
194 195 196
  struct string_list *next;
};

197 198
static struct string_list *string_list_cons (const char *,
					     struct string_list *);
199

Richard Stallman committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/* List of directories in which files should be converted.  */

struct string_list *directory_list;

/* List of file names which should not be converted.
   A file is excluded if the end of its name, following a /,
   matches one of the names in this list.  */

struct string_list *exclude_list;

/* The name of the other style of variable-number-of-parameters functions
   (i.e. the style that we want to leave unconverted because we don't yet
   know how to convert them to this style.  This string is used in warning
   messages.  */

/* Also define here the string that we can search for in the parameter lists
   taken from the .X files which will unambiguously indicate that we have
   found a varargs style function.  */

#ifdef UNPROTOIZE
static const char * const other_var_style = "stdarg";
221
#else /* !defined (UNPROTOIZE) */
Richard Stallman committed
222
static const char * const other_var_style = "varargs";
223
static const char *varargs_style_indicator = "va_alist";
224
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
225 226 227 228

/* The following two types are used to create hash tables.  In this program,
   there are two hash tables which are used to store and quickly lookup two
   different classes of strings.  The first type of strings stored in the
229
   first hash table are absolute filenames of files which protoize needs to
Richard Stallman committed
230 231 232 233 234 235 236 237 238 239 240 241
   know about.  The second type of strings (stored in the second hash table)
   are function names.  It is this second class of strings which really
   inspired the use of the hash tables, because there may be a lot of them.  */

typedef struct hash_table_entry_struct hash_table_entry;

/* Do some typedefs so that we don't have to write "struct" so often.  */

typedef struct def_dec_info_struct def_dec_info;
typedef struct file_info_struct file_info;
typedef struct f_list_chain_item_struct f_list_chain_item;

242
#ifndef UNPROTOIZE
243 244 245 246 247 248 249 250
static int is_syscalls_file (const file_info *);
static void rename_c_file (const hash_table_entry *);
static const def_dec_info *find_extern_def (const def_dec_info *,
					    const def_dec_info *);
static const def_dec_info *find_static_definition (const def_dec_info *);
static void connect_defs_and_decs (const hash_table_entry *);
static void add_local_decl (const def_dec_info *, const char *);
static void add_global_decls (const file_info *, const char *);
251
#endif /* ! UNPROTOIZE */
252 253 254 255 256 257 258 259 260 261 262 263 264 265
static int needs_to_be_converted (const file_info *);
static void visit_each_hash_node (const hash_table_entry *,
				  void (*)(const hash_table_entry *));
static hash_table_entry *add_symbol (hash_table_entry *, const char *);
static hash_table_entry *lookup (hash_table_entry *, const char *);
static void free_def_dec (def_dec_info *);
static file_info *find_file (const char *, int);
static void reverse_def_dec_list (const hash_table_entry *);
static void edit_fn_declaration (const def_dec_info *, const char *);
static int edit_formals_lists (const char *, unsigned int,
			       const def_dec_info *);
static void edit_fn_definition (const def_dec_info *, const char *);
static void scan_for_missed_items (const file_info *);
static void edit_file (const hash_table_entry *);
266

Richard Stallman committed
267
/* In the struct below, note that the "_info" field has two different uses
268 269
   depending on the type of hash table we are in (i.e. either the filenames
   hash table or the function names hash table).  In the filenames hash table
Richard Stallman committed
270
   the info fields of the entries point to the file_info struct which is
271
   associated with each filename (1 per filename).  In the function names
Richard Stallman committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
   hash table, the info field points to the head of a singly linked list of
   def_dec_info entries which are all defs or decs of the function whose
   name is pointed to by the "symbol" field.  Keeping all of the defs/decs
   for a given function name on a special list specifically for that function
   name makes it quick and easy to find out all of the important information
   about a given (named) function.  */

struct hash_table_entry_struct {
  hash_table_entry *		hash_next;	/* -> to secondary entries */
  const char *			symbol;		/* -> to the hashed string */
  union {
    const def_dec_info *	_ddip;
    file_info *			_fip;
  } _info;
};
#define ddip _info._ddip
#define fip _info._fip

/* Define a type specifically for our two hash tables.  */

typedef hash_table_entry hash_table[HASH_TABLE_SIZE];

/* The following struct holds all of the important information about any
295
   single filename (e.g. file) which we need to know about.  */
Richard Stallman committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

struct file_info_struct {
  const hash_table_entry *	hash_entry; /* -> to associated hash entry */
  const def_dec_info *		defs_decs;  /* -> to chain of defs/decs */
  time_t			mtime;      /* Time of last modification.  */
};

/* Due to the possibility that functions may return pointers to functions,
   (which may themselves have their own parameter lists) and due to the
   fact that returned pointers-to-functions may be of type "pointer-to-
   function-returning-pointer-to-function" (ad nauseum) we have to keep
   an entire chain of ANSI style formal parameter lists for each function.

   Normally, for any given function, there will only be one formals list
   on the chain, but you never know.

   Note that the head of each chain of formals lists is pointed to by the
   `f_list_chain' field of the corresponding def_dec_info record.

   For any given chain, the item at the head of the chain is the *leftmost*
   parameter list seen in the actual C language function declaration.  If
   there are other members of the chain, then these are linked in left-to-right
   order from the head of the chain.  */

struct f_list_chain_item_struct {
  const f_list_chain_item *	chain_next;	/* -> to next item on chain */
  const char *			formals_list;	/* -> to formals list string */
};

/* The following struct holds all of the important information about any
   single function definition or declaration which we need to know about.
   Note that for unprotoize we don't need to know very much because we
   never even create records for stuff that we don't intend to convert
   (like for instance defs and decs which are already in old K&R format
   and "implicit" function declarations).  */

struct def_dec_info_struct {
  const def_dec_info *	next_in_file;	/* -> to rest of chain for file */
  file_info *        	file;		/* -> file_info for containing file */
  int        		line;		/* source line number of def/dec */
  const char *		ansi_decl;	/* -> left end of ansi decl */
  hash_table_entry *	hash_entry;	/* -> hash entry for function name */
  unsigned int        	is_func_def;	/* = 0 means this is a declaration */
  const def_dec_info *	next_for_func;	/* -> to rest of chain for func name */
  unsigned int		f_list_count;	/* count of formals lists we expect */
  char			prototyped;	/* = 0 means already prototyped */
#ifndef UNPROTOIZE
  const f_list_chain_item * f_list_chain;	/* -> chain of formals lists */
  const def_dec_info *	definition;	/* -> def/dec containing related def */
345
  char	        	is_static;	/* = 0 means visibility is "extern"  */
Richard Stallman committed
346 347
  char			is_implicit;	/* != 0 for implicit func decl's */
  char			written;	/* != 0 means written for implicit */
348
#else /* !defined (UNPROTOIZE) */
Richard Stallman committed
349
  const char *		formal_names;	/* -> to list of names of formals */
350
  const char *		formal_decls;	/* -> to string of formal declarations */
351
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
352 353
};

354
/* Pointer to the tail component of the filename by which this program was
Richard Stallman committed
355 356 357 358
   invoked.  Used everywhere in error and warning messages.  */

static const char *pname;

359
/* Error counter.  Will be nonzero if we should give up at the next convenient
Richard Stallman committed
360 361 362 363 364 365 366 367
   stopping point.  */

static int errors = 0;

/* Option flags.  */
/* ??? These comments should say what the flag mean as well as the options
   that set them.  */

368 369
/* File name to use for running gcc.  Allows GCC 2 to be named
   something other than gcc.  */
370
static const char *compiler_file_name = "gcc";
371

372 373 374 375 376 377 378
static int version_flag = 0;		/* Print our version number.  */
static int quiet_flag = 0;		/* Don't print messages normally.  */
static int nochange_flag = 0;		/* Don't convert, just say what files
					   we would have converted.  */
static int nosave_flag = 0;		/* Don't save the old version.  */
static int keep_flag = 0;		/* Don't delete the .X files.  */
static const char ** compile_params = 0;	/* Option string for gcc.  */
Richard Stallman committed
379
#ifdef UNPROTOIZE
380 381
static const char *indent_string = "     ";	/* Indentation for newly
						   inserted parm decls.  */
382
#else /* !defined (UNPROTOIZE) */
383
static int local_flag = 0;		/* Insert new local decls (when?).  */
Richard Stallman committed
384
static int global_flag = 0;		/* set by -g option */
385
static int cplusplus_flag = 0;		/* Rename converted files to *.C.  */
Mike Stump committed
386
static const char *nondefault_syscalls_dir = 0; /* Dir to look for
387
						   SYSCALLS.c.X in.  */
388
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
389

390 391
/* An index into the compile_params array where we should insert the source
   file name when we are ready to exec the C compiler.  A zero value indicates
392
   that we have not yet called munge_compile_params.  */
Richard Stallman committed
393

394 395 396 397 398
static int input_file_name_index = 0;

/* An index into the compile_params array where we should insert the filename
   for the aux info file, when we run the C compiler.  */
static int aux_info_file_name_index = 0;
Richard Stallman committed
399 400 401

/* Count of command line arguments which were "filename" arguments.  */

402
static int n_base_source_files = 0;
Richard Stallman committed
403 404 405 406

/* Points to a malloc'ed list of pointers to all of the filenames of base
   source files which were specified on the command line.  */

407
static const char **base_source_filenames;
Richard Stallman committed
408 409 410 411 412 413 414 415 416

/* Line number of the line within the current aux_info file that we
   are currently processing.  Used for error messages in case the prototypes
   info file is corrupted somehow.  */

static int current_aux_info_lineno;

/* Pointer to the name of the source file currently being converted.  */

417
static const char *convert_filename;
Richard Stallman committed
418 419 420

/* Pointer to relative root string (taken from aux_info file) which indicates
   where directory the user was in when he did the compilation step that
Mike Stump committed
421
   produced the containing aux_info file.  */
Richard Stallman committed
422

423
static const char *invocation_filename;
Richard Stallman committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

/* Pointer to the base of the input buffer that holds the original text for the
   source file currently being converted.  */

static const char *orig_text_base;

/* Pointer to the byte just beyond the end of the input buffer that holds the
   original text for the source file currently being converted.  */

static const char *orig_text_limit;

/* Pointer to the base of the input buffer that holds the cleaned text for the
   source file currently being converted.  */

static const char *clean_text_base;

/* Pointer to the byte just beyond the end of the input buffer that holds the
   cleaned text for the source file currently being converted.  */

static const char *clean_text_limit;

/* Pointer to the last byte in the cleaned text buffer that we have already
   (virtually) copied to the output buffer (or decided to ignore).  */

static const char * clean_read_ptr;

/* Pointer to the base of the output buffer that holds the replacement text
   for the source file currently being converted.  */

static char *repl_text_base;

/* Pointer to the byte just beyond the end of the output buffer that holds the
   replacement text for the source file currently being converted.  */

static char *repl_text_limit;

/* Pointer to the last byte which has been stored into the output buffer.
   The next byte to be stored should be stored just past where this points
   to.  */

static char * repl_write_ptr;

/* Pointer into the cleaned text buffer for the source file we are currently
   converting.  This points to the first character of the line that we last
468
   did a "seek_to_line" to (see below).  */
Richard Stallman committed
469 470 471 472

static const char *last_known_line_start;

/* Number of the line (in the cleaned text buffer) that we last did a
473
   "seek_to_line" to.  Will be one if we just read a new source file
Richard Stallman committed
474 475 476 477
   into the cleaned text buffer.  */

static int last_known_line_number;

478
/* The filenames hash table.  */
Richard Stallman committed
479

480
static hash_table filename_primary;
Richard Stallman committed
481 482 483 484 485 486 487 488 489 490

/* The function names hash table.  */

static hash_table function_name_primary;

/* The place to keep the recovery address which is used only in cases where
   we get hopelessly confused by something in the cleaned original text.  */

static jmp_buf source_confusion_recovery;

491
/* A pointer to the current directory filename (used by abspath).  */
Richard Stallman committed
492 493 494 495 496 497 498 499 500 501 502 503 504

static char *cwd_buffer;

/* A place to save the read pointer until we are sure that an individual
   attempt at editing will succeed.  */

static const char * saved_clean_read_ptr;

/* A place to save the write pointer until we are sure that an individual
   attempt at editing will succeed.  */

static char * saved_repl_write_ptr;

505 506
/* Translate and output an error message.  */
static void
507
notice (const char *msgid, ...)
508
{
509 510 511
  va_list ap;
  
  va_start (ap, msgid);
512
  vfprintf (stderr, _(msgid), ap);
513
  va_end (ap);
514 515 516
}


Richard Stallman committed
517 518 519
/* Make a copy of a string INPUT with size SIZE.  */

static char *
520
savestring (const char *input, unsigned int size)
Richard Stallman committed
521
{
522
  char *output = xmalloc (size + 1);
Richard Stallman committed
523 524 525 526 527 528 529 530 531
  strcpy (output, input);
  return output;
}


/* Make a duplicate of the first N bytes of a given string in a newly
   allocated area.  */

static char *
532
dupnstr (const char *s, size_t n)
Richard Stallman committed
533
{
534
  char *ret_val = xmalloc (n + 1);
Richard Stallman committed
535

536
  strncpy (ret_val, s, n);
Richard Stallman committed
537 538 539 540
  ret_val[n] = '\0';
  return ret_val;
}

541 542 543 544
/* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
   retrying if necessary.  Return the actual number of bytes read.  */

static int
545
safe_read (int desc, void *ptr, int len)
546 547 548
{
  int left = len;
  while (left > 0) {
549
    int nchars = read (desc, ptr, left);
550
    if (nchars < 0)
551 552 553 554 555 556 557
      {
#ifdef EINTR
	if (errno == EINTR)
	  continue;
#endif
	return nchars;
      }
558 559
    if (nchars == 0)
      break;
560
    /* Arithmetic on void pointers is a gcc extension.  */
561
    ptr = (char *) ptr + nchars;
562 563 564 565 566 567 568 569 570
    left -= nchars;
  }
  return len - left;
}

/* Write LEN bytes at PTR to descriptor DESC,
   retrying if necessary, and treating any real error as fatal.  */

static void
571
safe_write (int desc, void *ptr, int len, const char *out_fname)
572 573
{
  while (len > 0) {
574
    int written = write (desc, ptr, len);
575
    if (written < 0)
576
      {
577
	int errno_val = errno;
578
#ifdef EINTR
579
	if (errno_val == EINTR)
580 581
	  continue;
#endif
582
	notice ("%s: error writing file '%s': %s\n",
583
		pname, shortpath (NULL, out_fname), xstrerror (errno_val));
584
	return;
585
      }
586
    /* Arithmetic on void pointers is a gcc extension.  */
587
    ptr = (char *) ptr + written;
588 589 590 591
    len -= written;
  }
}

Richard Stallman committed
592 593
/* Get setup to recover in case the edit we are about to do goes awry.  */

594
static void
595
save_pointers (void)
Richard Stallman committed
596 597 598 599 600 601 602 603
{
  saved_clean_read_ptr = clean_read_ptr;
  saved_repl_write_ptr = repl_write_ptr;
}

/* Call this routine to recover our previous state whenever something looks
   too confusing in the source code we are trying to edit.  */

604
static void
605
restore_pointers (void)
Richard Stallman committed
606 607 608 609 610
{
  clean_read_ptr = saved_clean_read_ptr;
  repl_write_ptr = saved_repl_write_ptr;
}

Richard Kenner committed
611
/* Return true if the given character is a valid identifier character.  */
Richard Stallman committed
612

613
static int
614
is_id_char (int ch)
Richard Stallman committed
615
{
616
  return (ISIDNUM (ch) || (ch == '$'));
Richard Stallman committed
617 618 619
}

/* Give a message indicating the proper way to invoke this program and then
620
   exit with nonzero status.  */
Richard Stallman committed
621 622

static void
623
usage (void)
Richard Stallman committed
624 625
{
#ifdef UNPROTOIZE
626 627
  notice ("%s: usage '%s [ -VqfnkN ] [ -i <istring> ] [ filename ... ]'\n",
	  pname, pname);
628
#else /* !defined (UNPROTOIZE) */
629 630
  notice ("%s: usage '%s [ -VqfnkNlgC ] [ -B <dirname> ] [ filename ... ]'\n",
	  pname, pname);
631
#endif /* !defined (UNPROTOIZE) */
632
  exit (FATAL_EXIT_CODE);
Richard Stallman committed
633 634
}

635
/* Return true if the given filename (assumed to be an absolute filename)
Richard Stallman committed
636 637 638 639
   designates a file residing anywhere beneath any one of the "system"
   include directories.  */

static int
640
in_system_include_dir (const char *path)
Richard Stallman committed
641
{
642
  const struct default_include *p;
Richard Stallman committed
643

644
  if (! IS_ABSOLUTE_PATH (path))
645
    abort ();		/* Must be an absolutized filename.  */
Richard Stallman committed
646

647
  for (p = cpp_include_defaults; p->fname; p++)
Richard Stallman committed
648
    if (!strncmp (path, p->fname, strlen (p->fname))
649
	&& IS_DIR_SEPARATOR (path[strlen (p->fname)]))
Richard Stallman committed
650 651 652 653 654
      return 1;
  return 0;
}

#if 0
655
/* Return true if the given filename designates a file that the user has
Richard Stallman committed
656 657 658 659 660 661
   read access to and for which the user has write access to the containing
   directory.  */

static int
file_could_be_converted (const char *path)
{
662
  char *const dir_name = alloca (strlen (path) + 1);
Richard Stallman committed
663

664
  if (access (path, R_OK))
Richard Stallman committed
665 666 667 668 669 670
    return 0;

  {
    char *dir_last_slash;

    strcpy (dir_name, path);
671 672 673 674 675
    dir_last_slash = strrchr (dir_name, DIR_SEPARATOR);
#ifdef DIR_SEPARATOR_2
    {
      char *slash;

676 677
      slash = strrchr (dir_last_slash ? dir_last_slash : dir_name,
		       DIR_SEPARATOR_2);
678 679 680 681
      if (slash)
	dir_last_slash = slash;
    }
#endif
Richard Stallman committed
682 683 684
    if (dir_last_slash)
      *dir_last_slash = '\0';
    else
685
      abort ();  /* Should have been an absolutized filename.  */
Richard Stallman committed
686 687
  }

688
  if (access (path, W_OK))
Richard Stallman committed
689 690 691 692 693
    return 0;

  return 1;
}

694
/* Return true if the given filename designates a file that we are allowed
Richard Stallman committed
695 696 697 698 699 700 701 702 703
   to modify.  Files which we should not attempt to modify are (a) "system"
   include files, and (b) files which the user doesn't have write access to,
   and (c) files which reside in directories which the user doesn't have
   write access to.  Unless requested to be quiet, give warnings about
   files that we will not try to convert for one reason or another.  An
   exception is made for "system" include files, which we never try to
   convert and for which we don't issue the usual warnings.  */

static int
704
file_normally_convertible (const char *path)
Richard Stallman committed
705 706 707 708 709 710 711 712 713 714
{
  char *const dir_name = alloca (strlen (path) + 1);

  if (in_system_include_dir (path))
    return 0;

  {
    char *dir_last_slash;

    strcpy (dir_name, path);
715 716 717 718 719
    dir_last_slash = strrchr (dir_name, DIR_SEPARATOR);
#ifdef DIR_SEPARATOR_2
    {
      char *slash;

720 721
      slash = strrchr (dir_last_slash ? dir_last_slash : dir_name,
		       DIR_SEPARATOR_2);
722 723 724 725
      if (slash)
	dir_last_slash = slash;
    }
#endif
Richard Stallman committed
726 727 728
    if (dir_last_slash)
      *dir_last_slash = '\0';
    else
729
      abort ();  /* Should have been an absolutized filename.  */
Richard Stallman committed
730 731
  }

732
  if (access (path, R_OK))
Richard Stallman committed
733 734
    {
      if (!quiet_flag)
735
	notice ("%s: warning: no read access for file '%s'\n",
736
		pname, shortpath (NULL, path));
Richard Stallman committed
737 738 739
      return 0;
    }

740
  if (access (path, W_OK))
Richard Stallman committed
741 742
    {
      if (!quiet_flag)
743
	notice ("%s: warning: no write access for file '%s'\n",
744
		pname, shortpath (NULL, path));
Richard Stallman committed
745 746 747
      return 0;
    }

748
  if (access (dir_name, W_OK))
Richard Stallman committed
749 750
    {
      if (!quiet_flag)
751
	notice ("%s: warning: no write access for dir containing '%s'\n",
752
		pname, shortpath (NULL, path));
Richard Stallman committed
753 754 755 756 757 758 759 760 761 762 763 764 765
      return 0;
    }

  return 1;
}
#endif /* 0 */

#ifndef UNPROTOIZE

/* Return true if the given file_info struct refers to the special SYSCALLS.c.X
   file.  Return false otherwise.  */

static int
766
is_syscalls_file (const file_info *fi_p)
Richard Stallman committed
767
{
768 769 770
  char const *f = fi_p->hash_entry->symbol;
  size_t fl = strlen (f), sysl = sizeof (syscalls_filename) - 1;
  return sysl <= fl  &&  strcmp (f + fl - sysl, syscalls_filename) == 0;
Richard Stallman committed
771 772
}

773
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
774 775 776 777 778 779 780 781

/* Check to see if this file will need to have anything done to it on this
   run.  If there is nothing in the given file which both needs conversion
   and for which we have the necessary stuff to do the conversion, return
   false.  Otherwise, return true.

   Note that (for protoize) it is only valid to call this function *after*
   the connections between declarations and definitions have all been made
782
   by connect_defs_and_decs.  */
Richard Stallman committed
783 784

static int
785
needs_to_be_converted (const file_info *file_p)
Richard Stallman committed
786 787 788 789 790 791 792 793
{
  const def_dec_info *ddp;

#ifndef UNPROTOIZE

  if (is_syscalls_file (file_p))
    return 0;

794
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
795 796 797 798 799 800 801

  for (ddp = file_p->defs_decs; ddp; ddp = ddp->next_in_file)

    if (

#ifndef UNPROTOIZE

Mike Stump committed
802
      /* ... and if we a protoizing and this function is in old style ...  */
Richard Stallman committed
803
      !ddp->prototyped
Mike Stump committed
804
      /* ... and if this a definition or is a decl with an associated def ...  */
Richard Stallman committed
805 806
      && (ddp->is_func_def || (!ddp->is_func_def && ddp->definition))

807
#else /* defined (UNPROTOIZE) */
Richard Stallman committed
808

Mike Stump committed
809
      /* ... and if we are unprotoizing and this function is in new style ...  */
Richard Stallman committed
810 811
      ddp->prototyped

812
#endif /* defined (UNPROTOIZE) */
Richard Stallman committed
813
      )
814 815
	  /* ... then the containing file needs converting.  */
	  return -1;
Richard Stallman committed
816 817 818 819 820 821 822
  return 0;
}

/* Return 1 if the file name NAME is in a directory
   that should be converted.  */

static int
823
directory_specified_p (const char *name)
Richard Stallman committed
824 825 826 827 828
{
  struct string_list *p;

  for (p = directory_list; p; p = p->next)
    if (!strncmp (name, p->name, strlen (p->name))
829
	&& IS_DIR_SEPARATOR (name[strlen (p->name)]))
830 831 832 833 834
      {
	const char *q = name + strlen (p->name) + 1;

	/* If there are more slashes, it's in a subdir, so
	   this match doesn't count.  */
835 836
	while (*q++)
	  if (IS_DIR_SEPARATOR (*(q-1)))
837 838 839 840 841
	    goto lose;
	return 1;

      lose: ;
      }
Richard Stallman committed
842 843 844 845 846 847 848

  return 0;
}

/* Return 1 if the file named NAME should be excluded from conversion.  */

static int
849
file_excluded_p (const char *name)
Richard Stallman committed
850 851 852 853 854 855
{
  struct string_list *p;
  int len = strlen (name);

  for (p = exclude_list; p; p = p->next)
    if (!strcmp (name + len - strlen (p->name), p->name)
856
	&& IS_DIR_SEPARATOR (name[len - strlen (p->name) - 1]))
Richard Stallman committed
857 858 859 860 861 862 863 864 865
      return 1;

  return 0;
}

/* Construct a new element of a string_list.
   STRING is the new element value, and REST holds the remaining elements.  */

static struct string_list *
866
string_list_cons (const char *string, struct string_list *rest)
Richard Stallman committed
867
{
868
  struct string_list *temp = xmalloc (sizeof (struct string_list));
869

Richard Stallman committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
  temp->next = rest;
  temp->name = string;
  return temp;
}

/* ??? The GNU convention for mentioning function args in its comments
   is to capitalize them.  So change "hash_tab_p" to HASH_TAB_P below.
   Likewise for all the other functions.  */

/* Given a hash table, apply some function to each node in the table. The
   table to traverse is given as the "hash_tab_p" argument, and the
   function to be applied to each node in the table is given as "func"
   argument.  */

static void
885 886
visit_each_hash_node (const hash_table_entry *hash_tab_p,
		      void (*func) (const hash_table_entry *))
Richard Stallman committed
887 888 889 890 891 892
{
  const hash_table_entry *primary;

  for (primary = hash_tab_p; primary < &hash_tab_p[HASH_TABLE_SIZE]; primary++)
    if (primary->symbol)
      {
893
	hash_table_entry *second;
Richard Stallman committed
894

895 896 897
	(*func)(primary);
	for (second = primary->hash_next; second; second = second->hash_next)
	  (*func) (second);
Richard Stallman committed
898 899 900 901 902 903 904 905 906
      }
}

/* Initialize all of the fields of a new hash table entry, pointed
   to by the "p" parameter.  Note that the space to hold the entry
   is assumed to have already been allocated before this routine is
   called.  */

static hash_table_entry *
907
add_symbol (hash_table_entry *p, const char *s)
Richard Stallman committed
908 909
{
  p->hash_next = NULL;
910
  p->symbol = xstrdup (s);
Richard Stallman committed
911 912 913 914 915
  p->ddip = NULL;
  p->fip = NULL;
  return p;
}

916
/* Look for a particular function name or filename in the particular
Richard Stallman committed
917 918 919 920 921
   hash table indicated by "hash_tab_p".  If the name is not in the
   given hash table, add it.  Either way, return a pointer to the
   hash table entry for the given name.  */

static hash_table_entry *
922
lookup (hash_table_entry *hash_tab_p, const char *search_symbol)
Richard Stallman committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
{
  int hash_value = 0;
  const char *search_symbol_char_p = search_symbol;
  hash_table_entry *p;

  while (*search_symbol_char_p)
    hash_value += *search_symbol_char_p++;
  hash_value &= hash_mask;
  p = &hash_tab_p[hash_value];
  if (! p->symbol)
      return add_symbol (p, search_symbol);
  if (!strcmp (p->symbol, search_symbol))
    return p;
  while (p->hash_next)
    {
      p = p->hash_next;
      if (!strcmp (p->symbol, search_symbol))
940
	return p;
Richard Stallman committed
941
    }
942
  p->hash_next = xmalloc (sizeof (hash_table_entry));
Richard Stallman committed
943 944 945 946 947 948 949 950 951
  p = p->hash_next;
  return add_symbol (p, search_symbol);
}

/* Throw a def/dec record on the junk heap.

   Also, since we are not using this record anymore, free up all of the
   stuff it pointed to.  */

952
static void
953
free_def_dec (def_dec_info *p)
Richard Stallman committed
954
{
955
  free ((NONCONST void *) p->ansi_decl);
Richard Stallman committed
956 957 958 959 960 961 962 963

#ifndef UNPROTOIZE
  {
    const f_list_chain_item * curr;
    const f_list_chain_item * next;

    for (curr = p->f_list_chain; curr; curr = next)
      {
964
	next = curr->chain_next;
965
	free ((NONCONST void *) curr);
Richard Stallman committed
966 967
      }
  }
968
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
969

970
  free (p);
Richard Stallman committed
971 972
}

973
/* Unexpand as many macro symbols as we can find.
Richard Stallman committed
974 975 976 977 978

   If the given line must be unexpanded, make a copy of it in the heap and
   return a pointer to the unexpanded copy.  Otherwise return NULL.  */

static char *
979
unexpand_if_needed (const char *aux_info_line)
Richard Stallman committed
980 981 982
{
  static char *line_buf = 0;
  static int line_buf_size = 0;
Mike Stump committed
983
  const unexpansion *unexp_p;
Richard Stallman committed
984 985 986 987 988 989 990
  int got_unexpanded = 0;
  const char *s;
  char *copy_p = line_buf;

  if (line_buf == 0)
    {
      line_buf_size = 1024;
991
      line_buf = xmalloc (line_buf_size);
Richard Stallman committed
992 993 994 995 996 997 998 999 1000
    }

  copy_p = line_buf;

  /* Make a copy of the input string in line_buf, expanding as necessary.  */

  for (s = aux_info_line; *s != '\n'; )
    {
      for (unexp_p = unexpansions; unexp_p->expanded; unexp_p++)
1001 1002 1003
	{
	  const char *in_p = unexp_p->expanded;
	  size_t len = strlen (in_p);
Richard Stallman committed
1004

1005 1006
	  if (*s == *in_p && !strncmp (s, in_p, len) && !is_id_char (s[len]))
	    {
Richard Stallman committed
1007
	      int size = strlen (unexp_p->contracted);
1008
	      got_unexpanded = 1;
Richard Stallman committed
1009 1010 1011 1012 1013
	      if (copy_p + size - line_buf >= line_buf_size)
		{
		  int offset = copy_p - line_buf;
		  line_buf_size *= 2;
		  line_buf_size += size;
1014
		  line_buf = xrealloc (line_buf, line_buf_size);
Richard Stallman committed
1015 1016
		  copy_p = line_buf + offset;
		}
1017 1018
	      strcpy (copy_p, unexp_p->contracted);
	      copy_p += size;
Richard Stallman committed
1019

1020 1021
	      /* Assume that there will not be another replacement required
	         within the text just replaced.  */
Richard Stallman committed
1022

1023 1024 1025 1026
	      s += len;
	      goto continue_outer;
	    }
	}
Richard Stallman committed
1027 1028 1029 1030
      if (copy_p - line_buf == line_buf_size)
	{
	  int offset = copy_p - line_buf;
	  line_buf_size *= 2;
1031
	  line_buf = xrealloc (line_buf, line_buf_size);
Richard Stallman committed
1032 1033 1034 1035 1036 1037 1038 1039 1040
	  copy_p = line_buf + offset;
	}
      *copy_p++ = *s++;
continue_outer: ;
    }
  if (copy_p + 2 - line_buf >= line_buf_size)
    {
      int offset = copy_p - line_buf;
      line_buf_size *= 2;
1041
      line_buf = xrealloc (line_buf, line_buf_size);
Richard Stallman committed
1042 1043 1044
      copy_p = line_buf + offset;
    }
  *copy_p++ = '\n';
1045
  *copy_p = '\0';
Richard Stallman committed
1046

1047
  return (got_unexpanded ? savestring (line_buf, copy_p - line_buf) : 0);
Richard Stallman committed
1048 1049
}

1050 1051
/* Return the absolutized filename for the given relative
   filename.  Note that if that filename is already absolute, it may
Richard Stallman committed
1052 1053
   still be returned in a modified form because this routine also
   eliminates redundant slashes and single dots and eliminates double
1054 1055 1056
   dots to get a shortest possible filename from the given input
   filename.  The absolutization of relative filenames is made by
   assuming that the given filename is to be taken as relative to
Richard Stallman committed
1057 1058 1059 1060
   the first argument (cwd) or to the current directory if cwd is
   NULL.  */

static char *
1061
abspath (const char *cwd, const char *rel_filename)
Richard Stallman committed
1062 1063
{
  /* Setup the current working directory as needed.  */
1064
  const char *const cwd2 = (cwd) ? cwd : cwd_buffer;
1065
  char *const abs_buffer = alloca (strlen (cwd2) + strlen (rel_filename) + 2);
Richard Stallman committed
1066 1067 1068
  char *endp = abs_buffer;
  char *outp, *inp;

1069
  /* Copy the  filename (possibly preceded by the current working
Richard Stallman committed
1070 1071 1072 1073 1074
     directory name) into the absolutization buffer.  */

  {
    const char *src_p;

1075
    if (! IS_ABSOLUTE_PATH (rel_filename))
Richard Stallman committed
1076
      {
1077 1078 1079 1080
	src_p = cwd2;
	while ((*endp++ = *src_p++))
	  continue;
	*(endp-1) = DIR_SEPARATOR;     		/* overwrite null */
Richard Stallman committed
1081
      }
1082 1083 1084
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
    else if (IS_DIR_SEPARATOR (rel_filename[0]))
      {
1085 1086
	/* A path starting with a directory separator is considered absolute
	   for dos based filesystems, but it's really not -- it's just the
1087 1088 1089 1090 1091 1092
	   convention used throughout GCC and it works. However, in this
	   case, we still need to prepend the drive spec from cwd_buffer.  */
	*endp++ = cwd2[0];
	*endp++ = cwd2[1];
      }
#endif
1093
    src_p = rel_filename;
Kaveh R. Ghazi committed
1094
    while ((*endp++ = *src_p++))
Richard Stallman committed
1095 1096 1097 1098
      continue;
  }

  /* Now make a copy of abs_buffer into abs_buffer, shortening the
1099
     filename (by taking out slashes and dots) as we go.  */
Richard Stallman committed
1100 1101 1102

  outp = inp = abs_buffer;
  *outp++ = *inp++;        	/* copy first slash */
1103
#if defined (apollo) || defined (_WIN32) || defined (__INTERIX)
1104
  if (IS_DIR_SEPARATOR (inp[0]))
1105 1106
    *outp++ = *inp++;        	/* copy second slash */
#endif
Richard Stallman committed
1107 1108 1109
  for (;;)
    {
      if (!inp[0])
1110
	break;
1111
      else if (IS_DIR_SEPARATOR (inp[0]) && IS_DIR_SEPARATOR (outp[-1]))
1112 1113 1114 1115
	{
	  inp++;
	  continue;
	}
1116
      else if (inp[0] == '.' && IS_DIR_SEPARATOR (outp[-1]))
1117 1118 1119 1120 1121 1122 1123 1124 1125
	{
	  if (!inp[1])
	    break;
	  else if (IS_DIR_SEPARATOR (inp[1]))
	    {
	      inp += 2;
	      continue;
	    }
	  else if ((inp[1] == '.') && (inp[2] == 0
1126
	                               || IS_DIR_SEPARATOR (inp[2])))
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	    {
	      inp += (IS_DIR_SEPARATOR (inp[2])) ? 3 : 2;
	      outp -= 2;
	      while (outp >= abs_buffer && ! IS_DIR_SEPARATOR (*outp))
	      	outp--;
	      if (outp < abs_buffer)
		{
		  /* Catch cases like /.. where we try to backup to a
		     point above the absolute root of the logical file
		     system.  */

		  notice ("%s: invalid file name: %s\n",
			  pname, rel_filename);
		  exit (FATAL_EXIT_CODE);
		}
	      *++outp = '\0';
	      continue;
	    }
	}
Richard Stallman committed
1146 1147 1148 1149 1150 1151 1152
      *outp++ = *inp++;
    }

  /* On exit, make sure that there is a trailing null, and make sure that
     the last character of the returned string is *not* a slash.  */

  *outp = '\0';
1153
  if (IS_DIR_SEPARATOR (outp[-1]))
Richard Stallman committed
1154 1155 1156 1157 1158
    *--outp  = '\0';

  /* Make a copy (in the heap) of the stuff left in the absolutization
     buffer and return a pointer to the copy.  */

1159
  return savestring (abs_buffer, outp - abs_buffer);
Richard Stallman committed
1160 1161
}

1162
/* Given a filename (and possibly a directory name from which the filename
Richard Stallman committed
1163
   is relative) return a string which is the shortest possible
1164
   equivalent for the corresponding full (absolutized) filename.  The
Richard Stallman committed
1165
   shortest possible equivalent may be constructed by converting the
1166 1167 1168
   absolutized filename to be a relative filename (i.e. relative to
   the actual current working directory).  However if a relative filename
   is longer, then the full absolute filename is returned.
Richard Stallman committed
1169 1170 1171

   KNOWN BUG:

1172 1173 1174
   Note that "simple-minded" conversion of any given type of filename (either
   relative or absolute) may not result in a valid equivalent filename if any
   subpart of the original filename is actually a symbolic link.  */
Richard Stallman committed
1175 1176

static const char *
1177
shortpath (const char *cwd, const char *filename)
Richard Stallman committed
1178 1179 1180 1181 1182 1183
{
  char *rel_buffer;
  char *rel_buf_p;
  char *cwd_p = cwd_buffer;
  char *path_p;
  int unmatched_slash_count = 0;
1184
  size_t filename_len = strlen (filename);
Richard Stallman committed
1185

1186
  path_p = abspath (cwd, filename);
1187
  rel_buf_p = rel_buffer = xmalloc (filename_len);
Richard Stallman committed
1188

1189
  while (*cwd_p && IS_SAME_PATH_CHAR (*cwd_p, *path_p))
Richard Stallman committed
1190 1191 1192 1193
    {
      cwd_p++;
      path_p++;
    }
1194
  if (!*cwd_p && (!*path_p || IS_DIR_SEPARATOR (*path_p)))
Richard Stallman committed
1195
    {
1196
      /* whole pwd matched */
Richard Stallman committed
1197
      if (!*path_p)        	/* input *is* the current path! */
1198
	return ".";
Richard Stallman committed
1199
      else
1200
	return ++path_p;
Richard Stallman committed
1201 1202 1203 1204
    }
  else
    {
      if (*path_p)
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
	{
	  --cwd_p;
	  --path_p;
	  while (! IS_DIR_SEPARATOR (*cwd_p))     /* backup to last slash */
	    {
	      --cwd_p;
	      --path_p;
	    }
	  cwd_p++;
	  path_p++;
	  unmatched_slash_count++;
	}
1217 1218

      /* Find out how many directory levels in cwd were *not* matched.  */
1219
      while (*cwd_p++)
1220
	if (IS_DIR_SEPARATOR (*(cwd_p-1)))
1221 1222 1223 1224 1225 1226 1227 1228
	  unmatched_slash_count++;

      /* Now we know how long the "short name" will be.
	 Reject it if longer than the input.  */
      if (unmatched_slash_count * 3 + strlen (path_p) >= filename_len)
	return filename;

      /* For each of them, put a `../' at the beginning of the short name.  */
Richard Stallman committed
1229
      while (unmatched_slash_count--)
1230
	{
1231 1232
	  /* Give up if the result gets to be longer
	     than the absolute path name.  */
1233 1234
	  if (rel_buffer + filename_len <= rel_buf_p + 3)
	    return filename;
1235 1236 1237 1238
	  *rel_buf_p++ = '.';
	  *rel_buf_p++ = '.';
	  *rel_buf_p++ = DIR_SEPARATOR;
	}
1239

1240
      /* Then tack on the unmatched part of the desired file's name.  */
1241 1242 1243 1244 1245
      do
	{
	  if (rel_buffer + filename_len <= rel_buf_p)
	    return filename;
	}
Kaveh R. Ghazi committed
1246
      while ((*rel_buf_p++ = *path_p++));
1247

Richard Stallman committed
1248
      --rel_buf_p;
1249
      if (IS_DIR_SEPARATOR (*(rel_buf_p-1)))
1250
	*--rel_buf_p = '\0';
Richard Stallman committed
1251 1252 1253 1254
      return rel_buffer;
    }
}

1255
/* Lookup the given filename in the hash table for filenames.  If it is a
Richard Stallman committed
1256
   new one, then the hash table info pointer will be null.  In this case,
1257
   we create a new file_info record to go with the filename, and we initialize
Richard Stallman committed
1258 1259
   that record with some reasonable values.  */

1260 1261 1262
/* FILENAME was const, but that causes a warning on AIX when calling stat.
   That is probably a bug in AIX, but might as well avoid the warning.  */

Richard Stallman committed
1263
static file_info *
1264
find_file (const char *filename, int do_not_stat)
Richard Stallman committed
1265 1266 1267
{
  hash_table_entry *hash_entry_p;

1268
  hash_entry_p = lookup (filename_primary, filename);
Richard Stallman committed
1269 1270 1271 1272 1273
  if (hash_entry_p->fip)
    return hash_entry_p->fip;
  else
    {
      struct stat stat_buf;
1274
      file_info *file_p = xmalloc (sizeof (file_info));
Richard Stallman committed
1275 1276

      /* If we cannot get status on any given source file, give a warning
1277
	 and then just set its time of last modification to infinity.  */
Richard Stallman committed
1278 1279

      if (do_not_stat)
1280
	stat_buf.st_mtime = (time_t) 0;
Richard Stallman committed
1281
      else
1282 1283 1284
	{
	  if (stat (filename, &stat_buf) == -1)
	    {
1285
	      int errno_val = errno;
1286
	      notice ("%s: %s: can't get status: %s\n",
1287 1288
		      pname, shortpath (NULL, filename),
		      xstrerror (errno_val));
1289 1290 1291
	      stat_buf.st_mtime = (time_t) -1;
	    }
	}
Richard Stallman committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

      hash_entry_p->fip = file_p;
      file_p->hash_entry = hash_entry_p;
      file_p->defs_decs = NULL;
      file_p->mtime = stat_buf.st_mtime;
      return file_p;
    }
}

/* Generate a fatal error because some part of the aux_info file is
   messed up.  */

static void
1305
aux_info_corrupted (void)
Richard Stallman committed
1306
{
1307 1308
  notice ("\n%s: fatal error: aux info file corrupted at line %d\n",
	  pname, current_aux_info_lineno);
1309
  exit (FATAL_EXIT_CODE);
Richard Stallman committed
1310 1311 1312
}

/* ??? This comment is vague.  Say what the condition is for.  */
1313
/* Check to see that a condition is true.  This is kind of like an assert.  */
Richard Stallman committed
1314

1315
static void
1316
check_aux_info (int cond)
Richard Stallman committed
1317 1318 1319 1320 1321 1322
{
  if (! cond)
    aux_info_corrupted ();
}

/* Given a pointer to the closing right parenthesis for a particular formals
1323
   list (in an aux_info file) find the corresponding left parenthesis and
Richard Stallman committed
1324 1325 1326
   return a pointer to it.  */

static const char *
1327
find_corresponding_lparen (const char *p)
Richard Stallman committed
1328 1329 1330 1331 1332 1333 1334
{
  const char *q;
  int paren_depth;

  for (paren_depth = 1, q = p-1; paren_depth; q--)
    {
      switch (*q)
1335 1336 1337 1338 1339 1340 1341 1342
	{
	case ')':
	  paren_depth++;
	  break;
	case '(':
	  paren_depth--;
	  break;
	}
Richard Stallman committed
1343 1344 1345 1346 1347 1348 1349
    }
  return ++q;
}

/* Given a line from  an aux info file, and a time at which the aux info
   file it came from was created, check to see if the item described in
   the line comes from a file which has been modified since the aux info
1350
   file was created.  If so, return nonzero, else return zero.  */
Richard Stallman committed
1351 1352

static int
1353
referenced_file_is_newer (const char *l, time_t aux_info_mtime)
Richard Stallman committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
{
  const char *p;
  file_info *fi_p;
  char *filename;

  check_aux_info (l[0] == '/');
  check_aux_info (l[1] == '*');
  check_aux_info (l[2] == ' ');

  {
    const char *filename_start = p = l + 3;

1366 1367
    while (*p != ':'
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1368
	   || (*p == ':' && *p && *(p+1) && IS_DIR_SEPARATOR (*(p+1)))
1369
#endif
1370
	   )
Richard Stallman committed
1371
      p++;
1372
    filename = alloca ((size_t) (p - filename_start) + 1);
Richard Stallman committed
1373 1374 1375 1376 1377 1378 1379 1380 1381
    strncpy (filename, filename_start, (size_t) (p - filename_start));
    filename[p-filename_start] = '\0';
  }

  /* Call find_file to find the file_info record associated with the file
     which contained this particular def or dec item.  Note that this call
     may cause a new file_info record to be created if this is the first time
     that we have ever known about this particular file.  */

1382
  fi_p = find_file (abspath (invocation_filename, filename), 0);
Richard Stallman committed
1383 1384 1385 1386 1387 1388 1389 1390 1391

  return (fi_p->mtime > aux_info_mtime);
}

/* Given a line of info from the aux_info file, create a new
   def_dec_info record to remember all of the important information about
   a function definition or declaration.

   Link this record onto the list of such records for the particular file in
1392
   which it occurred in proper (descending) line number order (for now).
Richard Stallman committed
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402

   If there is an identical record already on the list for the file, throw
   this one away.  Doing so takes care of the (useless and troublesome)
   duplicates which are bound to crop up due to multiple inclusions of any
   given individual header file.

   Finally, link the new def_dec record onto the list of such records
   pertaining to this particular function name.  */

static void
1403
save_def_or_dec (const char *l, int is_syscalls)
Richard Stallman committed
1404 1405 1406
{
  const char *p;
  const char *semicolon_p;
1407
  def_dec_info *def_dec_p = xmalloc (sizeof (def_dec_info));
Richard Stallman committed
1408 1409 1410

#ifndef UNPROTOIZE
  def_dec_p->written = 0;
1411
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

  /* Start processing the line by picking off 5 pieces of information from
     the left hand end of the line.  These are filename, line number,
     new/old/implicit flag (new = ANSI prototype format), definition or
     declaration flag, and extern/static flag).  */

  check_aux_info (l[0] == '/');
  check_aux_info (l[1] == '*');
  check_aux_info (l[2] == ' ');

  {
    const char *filename_start = p = l + 3;
    char *filename;

1426 1427
    while (*p != ':'
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1428
	   || (*p == ':' && *p && *(p+1) && IS_DIR_SEPARATOR (*(p+1)))
1429
#endif
1430
	   )
Richard Stallman committed
1431
      p++;
1432
    filename = alloca ((size_t) (p - filename_start) + 1);
Richard Stallman committed
1433 1434 1435 1436 1437 1438 1439
    strncpy (filename, filename_start, (size_t) (p - filename_start));
    filename[p-filename_start] = '\0';

    /* Call find_file to find the file_info record associated with the file
       which contained this particular def or dec item.  Note that this call
       may cause a new file_info record to be created if this is the first time
       that we have ever known about this particular file.
1440

1441
       Note that we started out by forcing all of the base source file names
Richard Stallman committed
1442
       (i.e. the names of the aux_info files with the .X stripped off) into the
1443 1444 1445
       filenames hash table, and we simultaneously setup file_info records for
       all of these base file names (even if they may be useless later).
       The file_info records for all of these "base" file names (properly)
Richard Stallman committed
1446
       act as file_info records for the "original" (i.e. un-included) files
1447
       which were submitted to gcc for compilation (when the -aux-info
Richard Stallman committed
1448
       option was used).  */
1449

1450
    def_dec_p->file = find_file (abspath (invocation_filename, filename), is_syscalls);
Richard Stallman committed
1451 1452 1453 1454 1455 1456
  }

  {
    const char *line_number_start = ++p;
    char line_number[10];

1457 1458
    while (*p != ':'
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1459
	   || (*p == ':' && *p && *(p+1) && IS_DIR_SEPARATOR (*(p+1)))
1460
#endif
1461
	   )
Richard Stallman committed
1462 1463 1464 1465 1466 1467 1468 1469 1470
      p++;
    strncpy (line_number, line_number_start, (size_t) (p - line_number_start));
    line_number[p-line_number_start] = '\0';
    def_dec_p->line = atoi (line_number);
  }

  /* Check that this record describes a new-style, old-style, or implicit
     definition or declaration.  */

Mike Stump committed
1471
  p++;	/* Skip over the `:'.  */
Richard Stallman committed
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
  check_aux_info ((*p == 'N') || (*p == 'O') || (*p == 'I'));

  /* Is this a new style (ANSI prototyped) definition or declaration? */

  def_dec_p->prototyped = (*p == 'N');

#ifndef UNPROTOIZE

  /* Is this an implicit declaration? */

  def_dec_p->is_implicit = (*p == 'I');

1484
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

  p++;

  check_aux_info ((*p == 'C') || (*p == 'F'));

  /* Is this item a function definition (F) or a declaration (C).  Note that
     we treat item taken from the syscalls file as though they were function
     definitions regardless of what the stuff in the file says.  */

  def_dec_p->is_func_def = ((*p++ == 'F') || is_syscalls);

#ifndef UNPROTOIZE
  def_dec_p->definition = 0;	/* Fill this in later if protoizing.  */
1498
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
1499 1500 1501 1502 1503 1504 1505 1506

  check_aux_info (*p++ == ' ');
  check_aux_info (*p++ == '*');
  check_aux_info (*p++ == '/');
  check_aux_info (*p++ == ' ');

#ifdef UNPROTOIZE
  check_aux_info ((!strncmp (p, "static", 6)) || (!strncmp (p, "extern", 6)));
1507
#else /* !defined (UNPROTOIZE) */
Richard Stallman committed
1508 1509 1510 1511 1512 1513
  if (!strncmp (p, "static", 6))
    def_dec_p->is_static = -1;
  else if (!strncmp (p, "extern", 6))
    def_dec_p->is_static = 0;
  else
    check_aux_info (0);	/* Didn't find either `extern' or `static'.  */
1514
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535

  {
    const char *ansi_start = p;

    p += 6;	/* Pass over the "static" or "extern".  */

    /* We are now past the initial stuff.  Search forward from here to find
       the terminating semicolon that should immediately follow the entire
       ANSI format function declaration.  */

    while (*++p != ';')
      continue;

    semicolon_p = p;

    /* Make a copy of the ansi declaration part of the line from the aux_info
       file.  */

    def_dec_p->ansi_decl
      = dupnstr (ansi_start, (size_t) ((semicolon_p+1) - ansi_start));

1536 1537 1538
    /* Backup and point at the final right paren of the final argument list.  */

    p--;
Richard Stallman committed
1539

1540 1541 1542 1543
#ifndef UNPROTOIZE
    def_dec_p->f_list_chain = NULL;
#endif /* !defined (UNPROTOIZE) */

1544
    while (p != ansi_start && (p[-1] == ' ' || p[-1] == '\t')) p--;
1545
    if (*p != ')')
1546 1547 1548 1549 1550
      {
	free_def_dec (def_dec_p);
	return;
      }
  }
Richard Stallman committed
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561

  /* Now isolate a whole set of formal argument lists, one-by-one.  Normally,
     there will only be one list to isolate, but there could be more.  */

  def_dec_p->f_list_count = 0;

  for (;;)
    {
      const char *left_paren_p = find_corresponding_lparen (p);
#ifndef UNPROTOIZE
      {
1562
	f_list_chain_item *cip = xmalloc (sizeof (f_list_chain_item));
Richard Stallman committed
1563

1564
	cip->formals_list
Richard Stallman committed
1565 1566
	  = dupnstr (left_paren_p + 1, (size_t) (p - (left_paren_p+1)));

1567 1568 1569 1570
	/* Add the new chain item at the head of the current list.  */

	cip->chain_next = def_dec_p->f_list_chain;
	def_dec_p->f_list_chain = cip;
Richard Stallman committed
1571
      }
1572
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
1573 1574 1575 1576 1577
      def_dec_p->f_list_count++;

      p = left_paren_p - 2;

      /* p must now point either to another right paren, or to the last
1578 1579 1580 1581
	 character of the name of the function that was declared/defined.
	 If p points to another right paren, then this indicates that we
	 are dealing with multiple formals lists.  In that case, there
	 really should be another right paren preceding this right paren.  */
Richard Stallman committed
1582 1583

      if (*p != ')')
1584
	break;
Richard Stallman committed
1585
      else
1586
	check_aux_info (*--p == ')');
Richard Stallman committed
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
    }


  {
    const char *past_fn = p + 1;

    check_aux_info (*past_fn == ' ');

    /* Scan leftwards over the identifier that names the function.  */

    while (is_id_char (*p))
      p--;
    p++;

    /* p now points to the leftmost character of the function name.  */

    {
1604
      char *fn_string = alloca (past_fn - p + 1);
Richard Stallman committed
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631

      strncpy (fn_string, p, (size_t) (past_fn - p));
      fn_string[past_fn-p] = '\0';
      def_dec_p->hash_entry = lookup (function_name_primary, fn_string);
    }
  }

  /* Look at all of the defs and decs for this function name that we have
     collected so far.  If there is already one which is at the same
     line number in the same file, then we can discard this new def_dec_info
     record.

     As an extra assurance that any such pair of (nominally) identical
     function declarations are in fact identical, we also compare the
     ansi_decl parts of the lines from the aux_info files just to be on
     the safe side.

     This comparison will fail if (for instance) the user was playing
     messy games with the preprocessor which ultimately causes one
     function declaration in one header file to look differently when
     that file is included by two (or more) other files.  */

  {
    const def_dec_info *other;

    for (other = def_dec_p->hash_entry->ddip; other; other = other->next_for_func)
      {
1632 1633 1634 1635
	if (def_dec_p->line == other->line && def_dec_p->file == other->file)
	  {
	    if (strcmp (def_dec_p->ansi_decl, other->ansi_decl))
	      {
1636
	        notice ("%s:%d: declaration of function '%s' takes different forms\n",
1637 1638 1639
			def_dec_p->file->hash_entry->symbol,
			def_dec_p->line,
			def_dec_p->hash_entry->symbol);
1640 1641 1642 1643 1644
	        exit (FATAL_EXIT_CODE);
	      }
	    free_def_dec (def_dec_p);
	    return;
	  }
Richard Stallman committed
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
      }
  }

#ifdef UNPROTOIZE

  /* If we are doing unprotoizing, we must now setup the pointers that will
     point to the K&R name list and to the K&R argument declarations list.

     Note that if this is only a function declaration, then we should not
     expect to find any K&R style formals list following the ANSI-style
     formals list.  This is because GCC knows that such information is
     useless in the case of function declarations (function definitions
     are a different story however).

     Since we are unprotoizing, we don't need any such lists anyway.
     All we plan to do is to delete all characters between ()'s in any
     case.  */

  def_dec_p->formal_names = NULL;
  def_dec_p->formal_decls = NULL;

  if (def_dec_p->is_func_def)
    {
      p = semicolon_p;
      check_aux_info (*++p == ' ');
      check_aux_info (*++p == '/');
      check_aux_info (*++p == '*');
      check_aux_info (*++p == ' ');
      check_aux_info (*++p == '(');

      {
1676
	const char *kr_names_start = ++p;   /* Point just inside '('.  */
Richard Stallman committed
1677

1678 1679 1680
	while (*p++ != ')')
	  continue;
	p--;		/* point to closing right paren */
Richard Stallman committed
1681

1682
	/* Make a copy of the K&R parameter names list.  */
Richard Stallman committed
1683

1684
	def_dec_p->formal_names
Richard Stallman committed
1685 1686 1687 1688 1689 1690 1691
	  = dupnstr (kr_names_start, (size_t) (p - kr_names_start));
      }

      check_aux_info (*++p == ' ');
      p++;

      /* p now points to the first character of the K&R style declarations
1692 1693
	 list (if there is one) or to the star-slash combination that ends
	 the comment in which such lists get embedded.  */
Richard Stallman committed
1694 1695

      /* Make a copy of the K&R formal decls list and set the def_dec record
1696
	 to point to it.  */
Richard Stallman committed
1697 1698

      if (*p == '*')		/* Are there no K&R declarations? */
1699 1700 1701 1702
	{
	  check_aux_info (*++p == '/');
	  def_dec_p->formal_decls = "";
	}
Richard Stallman committed
1703
      else
1704 1705
	{
	  const char *kr_decls_start = p;
Richard Stallman committed
1706

1707 1708 1709
	  while (p[0] != '*' || p[1] != '/')
	    p++;
	  p--;
Richard Stallman committed
1710

1711
	  check_aux_info (*p == ' ');
Richard Stallman committed
1712

1713
	  def_dec_p->formal_decls
Richard Stallman committed
1714
	    = dupnstr (kr_decls_start, (size_t) (p - kr_decls_start));
1715
	}
Richard Stallman committed
1716 1717

      /* Handle a special case.  If we have a function definition marked as
1718 1719 1720 1721
	 being in "old" style, and if its formal names list is empty, then
	 it may actually have the string "void" in its real formals list
	 in the original source code.  Just to make sure, we will get setup
	 to convert such things anyway.
Richard Stallman committed
1722

1723 1724
	 This kludge only needs to be here because of an insurmountable
	 problem with generating .X files.  */
Richard Stallman committed
1725 1726

      if (!def_dec_p->prototyped && !*def_dec_p->formal_names)
1727
	def_dec_p->prototyped = 1;
Richard Stallman committed
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
    }

  /* Since we are unprotoizing, if this item is already in old (K&R) style,
     we can just ignore it.  If that is true, throw away the itme now.  */

  if (!def_dec_p->prototyped)
    {
      free_def_dec (def_dec_p);
      return;
    }

1739
#endif /* defined (UNPROTOIZE) */
Richard Stallman committed
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764

  /* Add this record to the head of the list of records pertaining to this
     particular function name.  */

  def_dec_p->next_for_func = def_dec_p->hash_entry->ddip;
  def_dec_p->hash_entry->ddip = def_dec_p;

  /* Add this new def_dec_info record to the sorted list of def_dec_info
     records for this file.  Note that we don't have to worry about duplicates
     (caused by multiple inclusions of header files) here because we have
     already eliminated duplicates above.  */

  if (!def_dec_p->file->defs_decs)
    {
      def_dec_p->file->defs_decs = def_dec_p;
      def_dec_p->next_in_file = NULL;
    }
  else
    {
      int line = def_dec_p->line;
      const def_dec_info *prev = NULL;
      const def_dec_info *curr = def_dec_p->file->defs_decs;
      const def_dec_info *next = curr->next_in_file;

      while (next && (line < curr->line))
1765 1766 1767 1768 1769
	{
	  prev = curr;
	  curr = next;
	  next = next->next_in_file;
	}
Richard Stallman committed
1770
      if (line >= curr->line)
1771 1772 1773 1774 1775 1776 1777
	{
	  def_dec_p->next_in_file = curr;
	  if (prev)
	    ((NONCONST def_dec_info *) prev)->next_in_file = def_dec_p;
	  else
	    def_dec_p->file->defs_decs = def_dec_p;
	}
Richard Stallman committed
1778
      else	/* assert (next == NULL); */
1779 1780 1781 1782 1783
	{
	  ((NONCONST def_dec_info *) curr)->next_in_file = def_dec_p;
	  /* assert (next == NULL); */
	  def_dec_p->next_in_file = next;
	}
Richard Stallman committed
1784 1785 1786
    }
}

1787 1788 1789 1790 1791 1792
/* Set up the vector COMPILE_PARAMS which is the argument list for running GCC.
   Also set input_file_name_index and aux_info_file_name_index
   to the indices of the slots where the file names should go.  */

/* We initialize the vector by  removing -g, -O, -S, -c, and -o options,
   and adding '-aux-info AUXFILE -S  -o /dev/null INFILE' at the end.  */
Richard Stallman committed
1793 1794

static void
1795
munge_compile_params (const char *params_list)
Richard Stallman committed
1796
{
1797 1798
  /* Build up the contents in a temporary vector
     that is so big that to has to be big enough.  */
1799
  const char **temp_params
1800
    = alloca ((strlen (params_list) + 8) * sizeof (char *));
Richard Stallman committed
1801 1802
  int param_count = 0;
  const char *param;
1803
  struct stat st;
Richard Stallman committed
1804

1805
  temp_params[param_count++] = compiler_file_name;
Richard Stallman committed
1806 1807
  for (;;)
    {
Kaveh R. Ghazi committed
1808
      while (ISSPACE ((const unsigned char)*params_list))
1809
	params_list++;
Richard Stallman committed
1810
      if (!*params_list)
1811
	break;
Richard Stallman committed
1812
      param = params_list;
Kaveh R. Ghazi committed
1813
      while (*params_list && !ISSPACE ((const unsigned char)*params_list))
1814
	params_list++;
Richard Stallman committed
1815
      if (param[0] != '-')
1816
	temp_params[param_count++]
Richard Stallman committed
1817 1818
	  = dupnstr (param, (size_t) (params_list - param));
      else
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
	{
	  switch (param[1])
	    {
	    case 'g':
	    case 'O':
	    case 'S':
	    case 'c':
	      break;		/* Don't copy these.  */
	    case 'o':
	      while (ISSPACE ((const unsigned char)*params_list))
		params_list++;
	      while (*params_list
		     && !ISSPACE ((const unsigned char)*params_list))
		params_list++;
	      break;
	    default:
	      temp_params[param_count++]
		= dupnstr (param, (size_t) (params_list - param));
	    }
	}
Richard Stallman committed
1839
      if (!*params_list)
1840
	break;
Richard Stallman committed
1841
    }
1842 1843 1844 1845 1846 1847
  temp_params[param_count++] = "-aux-info";

  /* Leave room for the aux-info file name argument.  */
  aux_info_file_name_index = param_count;
  temp_params[param_count++] = NULL;

Richard Stallman committed
1848 1849
  temp_params[param_count++] = "-S";
  temp_params[param_count++] = "-o";
1850

1851 1852 1853
  if ((stat (HOST_BIT_BUCKET, &st) == 0)
      && (!S_ISDIR (st.st_mode))
      && (access (HOST_BIT_BUCKET, W_OK) == 0))
1854 1855 1856 1857 1858 1859
    temp_params[param_count++] = HOST_BIT_BUCKET;
  else
    /* FIXME: This is hardly likely to be right, if HOST_BIT_BUCKET is not
       writable.  But until this is rejigged to use make_temp_file(), this
       is the best we can do.  */
    temp_params[param_count++] = "/dev/null";
Richard Stallman committed
1860

1861 1862 1863 1864
  /* Leave room for the input file name argument.  */
  input_file_name_index = param_count;
  temp_params[param_count++] = NULL;
  /* Terminate the list.  */
Richard Stallman committed
1865 1866 1867 1868
  temp_params[param_count++] = NULL;

  /* Make a copy of the compile_params in heap space.  */

1869
  compile_params = xmalloc (sizeof (char *) * (param_count+1));
Richard Stallman committed
1870 1871 1872 1873
  memcpy (compile_params, temp_params, sizeof (char *) * param_count);
}

/* Do a recompilation for the express purpose of generating a new aux_info
1874 1875 1876
   file to go with a specific base source file.

   The result is a boolean indicating success.  */
Richard Stallman committed
1877 1878

static int
1879
gen_aux_info_file (const char *base_filename)
Richard Stallman committed
1880
{
1881
  if (!input_file_name_index)
Richard Stallman committed
1882 1883
    munge_compile_params ("");

1884 1885 1886
  /* Store the full source file name in the argument vector.  */
  compile_params[input_file_name_index] = shortpath (NULL, base_filename);
  /* Add .X to source file name to get aux-info file name.  */
1887
  compile_params[aux_info_file_name_index] =
1888
    concat (compile_params[input_file_name_index], aux_info_suffix, NULL);
1889

Richard Stallman committed
1890
  if (!quiet_flag)
1891
    notice ("%s: compiling '%s'\n",
1892
	    pname, compile_params[input_file_name_index]);
Richard Stallman committed
1893

1894 1895 1896
  {
    char *errmsg_fmt, *errmsg_arg;
    int wait_status, pid;
Richard Stallman committed
1897

1898
    pid = pexecute (compile_params[0], (char * const *) compile_params,
1899
		    pname, NULL, &errmsg_fmt, &errmsg_arg,
1900
		    PEXECUTE_FIRST | PEXECUTE_LAST | PEXECUTE_SEARCH);
Richard Stallman committed
1901

1902
    if (pid == -1)
Richard Stallman committed
1903
      {
1904 1905 1906
	int errno_val = errno;
	fprintf (stderr, "%s: ", pname);
	fprintf (stderr, errmsg_fmt, errmsg_arg);
1907
	fprintf (stderr, ": %s\n", xstrerror (errno_val));
1908 1909
	return 0;
      }
Richard Stallman committed
1910

1911 1912 1913
    pid = pwait (pid, &wait_status, 0);
    if (pid == -1)
      {
1914
	notice ("%s: wait: %s\n", pname, xstrerror (errno));
1915 1916 1917 1918
	return 0;
      }
    if (WIFSIGNALED (wait_status))
      {
1919 1920
	notice ("%s: subprocess got fatal signal %d\n",
		pname, WTERMSIG (wait_status));
1921 1922 1923 1924 1925
	return 0;
      }
    if (WIFEXITED (wait_status))
      {
	if (WEXITSTATUS (wait_status) != 0)
1926
	  {
1927 1928
	    notice ("%s: %s exited with status %d\n",
		    pname, compile_params[0], WEXITSTATUS (wait_status));
1929 1930 1931
	    return 0;
	  }
	return 1;
Richard Stallman committed
1932
      }
1933 1934
    abort ();
  }
Richard Stallman committed
1935 1936 1937 1938 1939 1940
}

/* Read in all of the information contained in a single aux_info file.
   Save all of the important stuff for later.  */

static void
1941 1942
process_aux_info_file (const char *base_source_filename, int keep_it,
		       int is_syscalls)
Richard Stallman committed
1943
{
1944
  size_t base_len = strlen (base_source_filename);
1945
  char * aux_info_filename = alloca (base_len + strlen (aux_info_suffix) + 1);
Richard Stallman committed
1946 1947
  char *aux_info_base;
  char *aux_info_limit;
1948
  char *aux_info_relocated_name;
Richard Stallman committed
1949 1950 1951
  const char *aux_info_second_line;
  time_t aux_info_mtime;
  size_t aux_info_size;
1952
  int must_create;
Richard Stallman committed
1953

1954
  /* Construct the aux_info filename from the base source filename.  */
Richard Stallman committed
1955

1956 1957
  strcpy (aux_info_filename, base_source_filename);
  strcat (aux_info_filename, aux_info_suffix);
Richard Stallman committed
1958 1959 1960 1961

  /* Check that the aux_info file exists and is readable.  If it does not
     exist, try to create it (once only).  */

1962 1963 1964 1965
  /* If file doesn't exist, set must_create.
     Likewise if it exists and we can read it but it is obsolete.
     Otherwise, report an error.  */
  must_create = 0;
1966 1967 1968 1969

  /* Come here with must_create set to 1 if file is out of date.  */
start_over: ;

1970
  if (access (aux_info_filename, R_OK) == -1)
1971 1972 1973 1974 1975
    {
      if (errno == ENOENT)
	{
	  if (is_syscalls)
	    {
1976
	      notice ("%s: warning: missing SYSCALLS file '%s'\n",
1977
		      pname, aux_info_filename);
1978 1979 1980 1981 1982 1983
	      return;
	    }
	  must_create = 1;
	}
      else
	{
1984
	  int errno_val = errno;
1985
	  notice ("%s: can't read aux info file '%s': %s\n",
1986 1987
		  pname, shortpath (NULL, aux_info_filename),
		  xstrerror (errno_val));
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
	  errors++;
	  return;
	}
    }
#if 0 /* There is code farther down to take care of this.  */
  else
    {
      struct stat s1, s2;
      stat (aux_info_file_name, &s1);
      stat (base_source_file_name, &s2);
      if (s2.st_mtime > s1.st_mtime)
	must_create = 1;
    }
#endif /* 0 */
Richard Stallman committed
2002

2003 2004 2005 2006 2007 2008 2009 2010
  /* If we need a .X file, create it, and verify we can read it.  */
  if (must_create)
    {
      if (!gen_aux_info_file (base_source_filename))
	{
	  errors++;
	  return;
	}
2011
      if (access (aux_info_filename, R_OK) == -1)
2012
	{
2013
	  int errno_val = errno;
2014
	  notice ("%s: can't read aux info file '%s': %s\n",
2015 2016
		  pname, shortpath (NULL, aux_info_filename),
		  xstrerror (errno_val));
2017 2018 2019 2020
	  errors++;
	  return;
	}
    }
Richard Stallman committed
2021 2022 2023 2024 2025

  {
    struct stat stat_buf;

    /* Get some status information about this aux_info file.  */
2026

2027
    if (stat (aux_info_filename, &stat_buf) == -1)
Richard Stallman committed
2028
      {
2029
	int errno_val = errno;
2030
	notice ("%s: can't get status of aux info file '%s': %s\n",
2031 2032
		pname, shortpath (NULL, aux_info_filename),
		xstrerror (errno_val));
2033 2034
	errors++;
	return;
Richard Stallman committed
2035
      }
2036

Richard Stallman committed
2037 2038
    /* Check on whether or not this aux_info file is zero length.  If it is,
       then just ignore it and return.  */
2039

Richard Stallman committed
2040 2041
    if ((aux_info_size = stat_buf.st_size) == 0)
      return;
2042

Richard Stallman committed
2043 2044 2045
    /* Get the date/time of last modification for this aux_info file and
       remember it.  We will have to check that any source files that it
       contains information about are at least this old or older.  */
2046

Richard Stallman committed
2047
    aux_info_mtime = stat_buf.st_mtime;
2048

2049
    if (!is_syscalls)
2050
      {
2051 2052 2053 2054
	/* Compare mod time with the .c file; update .X file if obsolete.
	   The code later on can fail to check the .c file
	   if it did not directly define any functions.  */

2055
	if (stat (base_source_filename, &stat_buf) == -1)
2056
	  {
2057
	    int errno_val = errno;
2058
	    notice ("%s: can't get status of aux info file '%s': %s\n",
2059 2060
		    pname, shortpath (NULL, base_source_filename),
		    xstrerror (errno_val));
2061 2062 2063 2064 2065 2066 2067 2068
	    errors++;
	    return;
	  }
	if (stat_buf.st_mtime > aux_info_mtime)
	  {
	    must_create = 1;
	    goto start_over;
	  }
2069
      }
Richard Stallman committed
2070 2071 2072 2073
  }

  {
    int aux_info_file;
2074
    int fd_flags;
Richard Stallman committed
2075 2076

    /* Open the aux_info file.  */
2077

2078 2079
    fd_flags = O_RDONLY;
#ifdef O_BINARY
2080
    /* Use binary mode to avoid having to deal with different EOL characters.  */
2081 2082 2083
    fd_flags |= O_BINARY;
#endif
    if ((aux_info_file = open (aux_info_filename, fd_flags, 0444 )) == -1)
Richard Stallman committed
2084
      {
2085
	int errno_val = errno;
2086
	notice ("%s: can't open aux info file '%s' for reading: %s\n",
2087 2088
		pname, shortpath (NULL, aux_info_filename),
		xstrerror (errno_val));
2089
	return;
Richard Stallman committed
2090
      }
2091

Richard Stallman committed
2092
    /* Allocate space to hold the aux_info file in memory.  */
2093

Richard Stallman committed
2094 2095 2096
    aux_info_base = xmalloc (aux_info_size + 1);
    aux_info_limit = aux_info_base + aux_info_size;
    *aux_info_limit = '\0';
2097

Richard Stallman committed
2098
    /* Read the aux_info file into memory.  */
2099

Kaveh R. Ghazi committed
2100 2101
    if (safe_read (aux_info_file, aux_info_base, aux_info_size) !=
	(int) aux_info_size)
Richard Stallman committed
2102
      {
2103
	int errno_val = errno;
2104
	notice ("%s: error reading aux info file '%s': %s\n",
2105 2106
		pname, shortpath (NULL, aux_info_filename),
		xstrerror (errno_val));
2107 2108 2109
	free (aux_info_base);
	close (aux_info_file);
	return;
Richard Stallman committed
2110
      }
2111

Richard Stallman committed
2112
    /* Close the aux info file.  */
2113

Richard Stallman committed
2114 2115
    if (close (aux_info_file))
      {
2116
	int errno_val = errno;
2117
	notice ("%s: error closing aux info file '%s': %s\n",
2118 2119
		pname, shortpath (NULL, aux_info_filename),
		xstrerror (errno_val));
2120 2121 2122
	free (aux_info_base);
	close (aux_info_file);
	return;
Richard Stallman committed
2123 2124 2125 2126 2127 2128
      }
  }

  /* Delete the aux_info file (unless requested not to).  If the deletion
     fails for some reason, don't even worry about it.  */

2129
  if (must_create && !keep_it)
2130
    if (unlink (aux_info_filename) == -1)
2131 2132
      {
	int errno_val = errno;
2133
	notice ("%s: can't delete aux info file '%s': %s\n",
2134 2135
		pname, shortpath (NULL, aux_info_filename),
		xstrerror (errno_val));
2136
      }
Richard Stallman committed
2137 2138

  /* Save a pointer into the first line of the aux_info file which
2139
     contains the filename of the directory from which the compiler
Richard Stallman committed
2140 2141
     was invoked when the associated source file was compiled.
     This information is used later to help create complete
2142
     filenames out of the (potentially) relative filenames in
Richard Stallman committed
2143 2144 2145 2146 2147
     the aux_info file.  */

  {
    char *p = aux_info_base;

2148 2149
    while (*p != ':'
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
2150
	   || (*p == ':' && *p && *(p+1) && IS_DIR_SEPARATOR (*(p+1)))
2151
#endif
2152
	   )
Richard Stallman committed
2153 2154 2155 2156
      p++;
    p++;
    while (*p == ' ')
      p++;
2157
    invocation_filename = p;	/* Save a pointer to first byte of path.  */
Richard Stallman committed
2158 2159
    while (*p != ' ')
      p++;
2160
    *p++ = DIR_SEPARATOR;
Richard Stallman committed
2161 2162 2163 2164
    *p++ = '\0';
    while (*p++ != '\n')
      continue;
    aux_info_second_line = p;
2165
    aux_info_relocated_name = 0;
2166
    if (! IS_ABSOLUTE_PATH (invocation_filename))
2167 2168 2169 2170 2171 2172
      {
	/* INVOCATION_FILENAME is relative;
	   append it to BASE_SOURCE_FILENAME's dir.  */
	char *dir_end;
	aux_info_relocated_name = xmalloc (base_len + (p-invocation_filename));
	strcpy (aux_info_relocated_name, base_source_filename);
2173 2174 2175 2176 2177
	dir_end = strrchr (aux_info_relocated_name, DIR_SEPARATOR);
#ifdef DIR_SEPARATOR_2
	{
	  char *slash;

2178 2179
	  slash = strrchr (dir_end ? dir_end : aux_info_relocated_name,
			   DIR_SEPARATOR_2);
2180 2181 2182 2183
	  if (slash)
	    dir_end = slash;
	}
#endif
2184 2185 2186 2187 2188 2189 2190
	if (dir_end)
	  dir_end++;
	else
	  dir_end = aux_info_relocated_name;
	strcpy (dir_end, invocation_filename);
	invocation_filename = aux_info_relocated_name;
      }
Richard Stallman committed
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
  }


  {
    const char *aux_info_p;

    /* Do a pre-pass on the lines in the aux_info file, making sure that all
       of the source files referenced in there are at least as old as this
       aux_info file itself.  If not, go back and regenerate the aux_info
       file anew.  Don't do any of this for the syscalls file.  */

    if (!is_syscalls)
      {
2204 2205 2206 2207 2208 2209 2210
	current_aux_info_lineno = 2;

	for (aux_info_p = aux_info_second_line; *aux_info_p; )
	  {
	    if (referenced_file_is_newer (aux_info_p, aux_info_mtime))
	      {
		free (aux_info_base);
2211
		free (aux_info_relocated_name);
2212 2213
		if (keep_it && unlink (aux_info_filename) == -1)
		  {
2214
		    int errno_val = errno;
2215
	            notice ("%s: can't delete file '%s': %s\n",
2216 2217
			    pname, shortpath (NULL, aux_info_filename),
			    xstrerror (errno_val));
2218 2219
	            return;
	          }
2220
		must_create = 1;
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
	        goto start_over;
	      }

	    /* Skip over the rest of this line to start of next line.  */

	    while (*aux_info_p != '\n')
	      aux_info_p++;
	    aux_info_p++;
	    current_aux_info_lineno++;
	  }
Richard Stallman committed
2231 2232 2233 2234
      }

    /* Now do the real pass on the aux_info lines.  Save their information in
       the in-core data base.  */
2235

Richard Stallman committed
2236
    current_aux_info_lineno = 2;
2237

Richard Stallman committed
2238 2239
    for (aux_info_p = aux_info_second_line; *aux_info_p;)
      {
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
	char *unexpanded_line = unexpand_if_needed (aux_info_p);

	if (unexpanded_line)
	  {
	    save_def_or_dec (unexpanded_line, is_syscalls);
	    free (unexpanded_line);
	  }
	else
	  save_def_or_dec (aux_info_p, is_syscalls);

	/* Skip over the rest of this line and get to start of next line.  */

	while (*aux_info_p != '\n')
	  aux_info_p++;
	aux_info_p++;
	current_aux_info_lineno++;
Richard Stallman committed
2256 2257 2258 2259
      }
  }

  free (aux_info_base);
2260
  free (aux_info_relocated_name);
Richard Stallman committed
2261 2262 2263 2264 2265 2266 2267 2268 2269
}

#ifndef UNPROTOIZE

/* Check an individual filename for a .c suffix.  If the filename has this
   suffix, rename the file such that its suffix is changed to .C.  This
   function implements the -C option.  */

static void
2270
rename_c_file (const hash_table_entry *hp)
Richard Stallman committed
2271
{
2272 2273
  const char *filename = hp->symbol;
  int last_char_index = strlen (filename) - 1;
2274 2275
  char *const new_filename = alloca (strlen (filename)
				     + strlen (cplus_suffix) + 1);
Richard Stallman committed
2276 2277 2278 2279 2280

  /* Note that we don't care here if the given file was converted or not.  It
     is possible that the given file was *not* converted, simply because there
     was nothing in it which actually required conversion.  Even in this case,
     we want to do the renaming.  Note that we only rename files with the .c
2281
     suffix (except for the syscalls file, which is left alone).  */
Richard Stallman committed
2282

2283 2284
  if (filename[last_char_index] != 'c' || filename[last_char_index-1] != '.'
      || IS_SAME_PATH (syscalls_absolute_filename, filename))
Richard Stallman committed
2285 2286
    return;

2287
  strcpy (new_filename, filename);
2288
  strcpy (&new_filename[last_char_index], cplus_suffix);
Richard Stallman committed
2289

2290
  if (rename (filename, new_filename) == -1)
Richard Stallman committed
2291
    {
2292
      int errno_val = errno;
2293
      notice ("%s: warning: can't rename file '%s' to '%s': %s\n",
2294 2295
	      pname, shortpath (NULL, filename),
	      shortpath (NULL, new_filename), xstrerror (errno_val));
Richard Stallman committed
2296 2297 2298 2299 2300
      errors++;
      return;
    }
}

2301
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311

/* Take the list of definitions and declarations attached to a particular
   file_info node and reverse the order of the list.  This should get the
   list into an order such that the item with the lowest associated line
   number is nearest the head of the list.  When these lists are originally
   built, they are in the opposite order.  We want to traverse them in
   normal line number order later (i.e. lowest to highest) so reverse the
   order here.  */

static void
2312
reverse_def_dec_list (const hash_table_entry *hp)
Richard Stallman committed
2313 2314
{
  file_info *file_p = hp->fip;
2315
  def_dec_info *prev = NULL;
Kazu Hirata committed
2316
  def_dec_info *current = (def_dec_info *) file_p->defs_decs;
Richard Stallman committed
2317

2318
  if (!current)
Richard Stallman committed
2319 2320 2321
    return;        		/* no list to reverse */

  prev = current;
Kazu Hirata committed
2322
  if (! (current = (def_dec_info *) current->next_in_file))
Richard Stallman committed
2323 2324
    return;        		/* can't reverse a single list element */

2325
  prev->next_in_file = NULL;
Richard Stallman committed
2326 2327 2328

  while (current)
    {
Kazu Hirata committed
2329
      def_dec_info *next = (def_dec_info *) current->next_in_file;
Richard Stallman committed
2330

2331
      current->next_in_file = prev;
Richard Stallman committed
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
      prev = current;
      current = next;
    }

  file_p->defs_decs = prev;
}

#ifndef UNPROTOIZE

/* Find the (only?) extern definition for a particular function name, starting
   from the head of the linked list of entries for the given name.  If we
   cannot find an extern definition for the given function name, issue a
   warning and scrounge around for the next best thing, i.e. an extern
   function declaration with a prototype attached to it.  Note that we only
   allow such substitutions for extern declarations and never for static
   declarations.  That's because the only reason we allow them at all is
   to let un-prototyped function declarations for system-supplied library
   functions get their prototypes from our own extra SYSCALLS.c.X file which
   contains all of the correct prototypes for system functions.  */

static const def_dec_info *
2353
find_extern_def (const def_dec_info *head, const def_dec_info *user)
Richard Stallman committed
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
{
  const def_dec_info *dd_p;
  const def_dec_info *extern_def_p = NULL;
  int conflict_noted = 0;

  /* Don't act too stupid here.  Somebody may try to convert an entire system
     in one swell fwoop (rather than one program at a time, as should be done)
     and in that case, we may find that there are multiple extern definitions
     of a given function name in the entire set of source files that we are
     converting.  If however one of these definitions resides in exactly the
     same source file as the reference we are trying to satisfy then in that
     case it would be stupid for us to fail to realize that this one definition
     *must* be the precise one we are looking for.

     To make sure that we don't miss an opportunity to make this "same file"
     leap of faith, we do a prescan of the list of records relating to the
     given function name, and we look (on this first scan) *only* for a
     definition of the function which is in the same file as the reference
     we are currently trying to satisfy.  */

  for (dd_p = head; dd_p; dd_p = dd_p->next_for_func)
    if (dd_p->is_func_def && !dd_p->is_static && dd_p->file == user->file)
      return dd_p;

  /* Now, since we have not found a definition in the same file as the
     reference, we scan the list again and consider all possibilities from
     all files.  Here we may get conflicts with the things listed in the
     SYSCALLS.c.X file, but if that happens it only means that the source
     code being converted contains its own definition of a function which
     could have been supplied by libc.a.  In such cases, we should avoid
     issuing the normal warning, and defer to the definition given in the
Kazu Hirata committed
2385
     user's own code.  */
Richard Stallman committed
2386 2387 2388 2389

  for (dd_p = head; dd_p; dd_p = dd_p->next_for_func)
    if (dd_p->is_func_def && !dd_p->is_static)
      {
2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
	if (!extern_def_p)	/* Previous definition? */
	  extern_def_p = dd_p;	/* Remember the first definition found.  */
	else
	  {
	    /* Ignore definition just found if it came from SYSCALLS.c.X.  */

	    if (is_syscalls_file (dd_p->file))
	      continue;

	    /* Quietly replace the definition previously found with the one
	       just found if the previous one was from SYSCALLS.c.X.  */

	    if (is_syscalls_file (extern_def_p->file))
	      {
	        extern_def_p = dd_p;
	        continue;
	      }

	    /* If we get here, then there is a conflict between two function
	       declarations for the same function, both of which came from the
	       user's own code.  */

	    if (!conflict_noted)	/* first time we noticed? */
	      {
		conflict_noted = 1;
		notice ("%s: conflicting extern definitions of '%s'\n",
2416
			pname, head->hash_entry->symbol);
2417 2418 2419
		if (!quiet_flag)
		  {
		    notice ("%s: declarations of '%s' will not be converted\n",
2420
			    pname, head->hash_entry->symbol);
2421
		    notice ("%s: conflict list for '%s' follows:\n",
2422
			    pname, head->hash_entry->symbol);
2423
		    fprintf (stderr, "%s:     %s(%d): %s\n",
2424 2425 2426
			     pname,
			     shortpath (NULL, extern_def_p->file->hash_entry->symbol),
			     extern_def_p->line, extern_def_p->ansi_decl);
2427 2428 2429 2430
		  }
	      }
	    if (!quiet_flag)
	      fprintf (stderr, "%s:     %s(%d): %s\n",
2431 2432 2433
		       pname,
		       shortpath (NULL, dd_p->file->hash_entry->symbol),
		       dd_p->line, dd_p->ansi_decl);
2434
	  }
Richard Stallman committed
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446
      }

  /* We want to err on the side of caution, so if we found multiple conflicting
     definitions for the same function, treat this as being that same as if we
     had found no definitions (i.e. return NULL).  */

  if (conflict_noted)
    return NULL;

  if (!extern_def_p)
    {
      /* We have no definitions for this function so do the next best thing.
2447
	 Search for an extern declaration already in prototype form.  */
Richard Stallman committed
2448 2449

      for (dd_p = head; dd_p; dd_p = dd_p->next_for_func)
2450 2451 2452 2453
	if (!dd_p->is_func_def && !dd_p->is_static && dd_p->prototyped)
	  {
	    extern_def_p = dd_p;	/* save a pointer to the definition */
	    if (!quiet_flag)
2454
	      notice ("%s: warning: using formals list from %s(%d) for function '%s'\n",
2455 2456 2457
		      pname,
		      shortpath (NULL, dd_p->file->hash_entry->symbol),
		      dd_p->line, dd_p->hash_entry->symbol);
2458 2459
	    break;
	  }
Richard Stallman committed
2460 2461

      /* Gripe about unprototyped function declarations that we found no
2462 2463
	 corresponding definition (or other source of prototype information)
	 for.
Richard Stallman committed
2464

2465 2466 2467 2468
	 Gripe even if the unprototyped declaration we are worried about
	 exists in a file in one of the "system" include directories.  We
	 can gripe about these because we should have at least found a
	 corresponding (pseudo) definition in the SYSCALLS.c.X file.  If we
Richard Stallman committed
2469
	 didn't, then that means that the SYSCALLS.c.X file is missing some
2470 2471
	 needed prototypes for this particular system.  That is worth telling
	 the user about!  */
Richard Stallman committed
2472 2473

      if (!extern_def_p)
2474 2475
	{
	  const char *file = user->file->hash_entry->symbol;
Richard Stallman committed
2476

2477 2478 2479
	  if (!quiet_flag)
	    if (in_system_include_dir (file))
	      {
Richard Stallman committed
2480 2481
		/* Why copy this string into `needed' at all?
		   Why not just use user->ansi_decl without copying?  */
2482
		char *needed = alloca (strlen (user->ansi_decl) + 1);
2483
	        char *p;
Richard Stallman committed
2484

2485
	        strcpy (needed, user->ansi_decl);
2486
	        p = strstr (needed, user->hash_entry->symbol)
2487
	            + strlen (user->hash_entry->symbol) + 2;
2488 2489 2490 2491
		/* Avoid having ??? in the string.  */
		*p++ = '?';
		*p++ = '?';
		*p++ = '?';
2492
	        strcpy (p, ");");
Richard Stallman committed
2493

2494
	        notice ("%s: %d: '%s' used but missing from SYSCALLS\n",
2495 2496
			shortpath (NULL, file), user->line,
			needed+7);	/* Don't print "extern " */
2497
	      }
2498
#if 0
2499
	    else
2500
	      notice ("%s: %d: warning: no extern definition for '%s'\n",
2501 2502
		      shortpath (NULL, file), user->line,
		      user->hash_entry->symbol);
2503
#endif
2504
	}
Richard Stallman committed
2505 2506 2507 2508 2509 2510
    }
  return extern_def_p;
}

/* Find the (only?) static definition for a particular function name in a
   given file.  Here we get the function-name and the file info indirectly
Mike Stump committed
2511
   from the def_dec_info record pointer which is passed in.  */
Richard Stallman committed
2512 2513

static const def_dec_info *
2514
find_static_definition (const def_dec_info *user)
Richard Stallman committed
2515 2516 2517 2518 2519 2520 2521 2522 2523
{
  const def_dec_info *head = user->hash_entry->ddip;
  const def_dec_info *dd_p;
  int num_static_defs = 0;
  const def_dec_info *static_def_p = NULL;

  for (dd_p = head; dd_p; dd_p = dd_p->next_for_func)
    if (dd_p->is_func_def && dd_p->is_static && (dd_p->file == user->file))
      {
2524 2525
	static_def_p = dd_p;	/* save a pointer to the definition */
	num_static_defs++;
Richard Stallman committed
2526 2527 2528 2529
      }
  if (num_static_defs == 0)
    {
      if (!quiet_flag)
2530
	notice ("%s: warning: no static definition for '%s' in file '%s'\n",
2531 2532
		pname, head->hash_entry->symbol,
		shortpath (NULL, user->file->hash_entry->symbol));
Richard Stallman committed
2533 2534 2535
    }
  else if (num_static_defs > 1)
    {
2536
      notice ("%s: multiple static defs of '%s' in file '%s'\n",
2537 2538
	      pname, head->hash_entry->symbol,
	      shortpath (NULL, user->file->hash_entry->symbol));
Richard Stallman committed
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562
      return NULL;
    }
  return static_def_p;
}

/* Find good prototype style formal argument lists for all of the function
   declarations which didn't have them before now.

   To do this we consider each function name one at a time.  For each function
   name, we look at the items on the linked list of def_dec_info records for
   that particular name.

   Somewhere on this list we should find one (and only one) def_dec_info
   record which represents the actual function definition, and this record
   should have a nice formal argument list already associated with it.

   Thus, all we have to do is to connect up all of the other def_dec_info
   records for this particular function name to the special one which has
   the full-blown formals list.

   Of course it is a little more complicated than just that.  See below for
   more details.  */

static void
2563
connect_defs_and_decs (const hash_table_entry *hp)
Richard Stallman committed
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
{
  const def_dec_info *dd_p;
  const def_dec_info *extern_def_p = NULL;
  int first_extern_reference = 1;

  /* Traverse the list of definitions and declarations for this particular
     function name.  For each item on the list, if it is a function
     definition (either old style or new style) then GCC has already been
     kind enough to produce a prototype for us, and it is associated with
     the item already, so declare the item as its own associated "definition".

     Also, for each item which is only a function declaration, but which
     nonetheless has its own prototype already (obviously supplied by the user)
2577
     declare the item as its own definition.
Richard Stallman committed
2578 2579 2580 2581 2582 2583

     Note that when/if there are multiple user-supplied prototypes already
     present for multiple declarations of any given function, these multiple
     prototypes *should* all match exactly with one another and with the
     prototype for the actual function definition.  We don't check for this
     here however, since we assume that the compiler must have already done
2584
     this consistency checking when it was creating the .X files.  */
Richard Stallman committed
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608

  for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func)
    if (dd_p->prototyped)
      ((NONCONST def_dec_info *) dd_p)->definition = dd_p;

  /* Traverse the list of definitions and declarations for this particular
     function name.  For each item on the list, if it is an extern function
     declaration and if it has no associated definition yet, go try to find
     the matching extern definition for the declaration.

     When looking for the matching function definition, warn the user if we
     fail to find one.

     If we find more that one function definition also issue a warning.

     Do the search for the matching definition only once per unique function
     name (and only when absolutely needed) so that we can avoid putting out
     redundant warning messages, and so that we will only put out warning
     messages when there is actually a reference (i.e. a declaration) for
     which we need to find a matching definition.  */

  for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func)
    if (!dd_p->is_func_def && !dd_p->is_static && !dd_p->definition)
      {
2609 2610 2611 2612 2613 2614
	if (first_extern_reference)
	  {
	    extern_def_p = find_extern_def (hp->ddip, dd_p);
	    first_extern_reference = 0;
	  }
	((NONCONST def_dec_info *) dd_p)->definition = extern_def_p;
Richard Stallman committed
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
      }

  /* Traverse the list of definitions and declarations for this particular
     function name.  For each item on the list, if it is a static function
     declaration and if it has no associated definition yet, go try to find
     the matching static definition for the declaration within the same file.

     When looking for the matching function definition, warn the user if we
     fail to find one in the same file with the declaration, and refuse to
     convert this kind of cross-file static function declaration.  After all,
     this is stupid practice and should be discouraged.

     We don't have to worry about the possibility that there is more than one
     matching function definition in the given file because that would have
     been flagged as an error by the compiler.

     Do the search for the matching definition only once per unique
     function-name/source-file pair (and only when absolutely needed) so that
     we can avoid putting out redundant warning messages, and so that we will
     only put out warning messages when there is actually a reference (i.e. a
     declaration) for which we actually need to find a matching definition.  */

  for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func)
    if (!dd_p->is_func_def && dd_p->is_static && !dd_p->definition)
      {
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
	const def_dec_info *dd_p2;
	const def_dec_info *static_def;

	/* We have now found a single static declaration for which we need to
	   find a matching definition.  We want to minimize the work (and the
	   number of warnings), so we will find an appropriate (matching)
	   static definition for this declaration, and then distribute it
	   (as the definition for) any and all other static declarations
	   for this function name which occur within the same file, and which
	   do not already have definitions.

	   Note that a trick is used here to prevent subsequent attempts to
	   call find_static_definition for a given function-name & file
	   if the first such call returns NULL.  Essentially, we convert
	   these NULL return values to -1, and put the -1 into the definition
	   field for each other static declaration from the same file which
	   does not already have an associated definition.
	   This makes these other static declarations look like they are
	   actually defined already when the outer loop here revisits them
	   later on.  Thus, the outer loop will skip over them.  Later, we
	   turn the -1's back to NULL's.  */

	((NONCONST def_dec_info *) dd_p)->definition =
	  (static_def = find_static_definition (dd_p))
	  ? static_def
	  : (const def_dec_info *) -1;

	for (dd_p2 = dd_p->next_for_func; dd_p2; dd_p2 = dd_p2->next_for_func)
	  if (!dd_p2->is_func_def && dd_p2->is_static
	      && !dd_p2->definition && (dd_p2->file == dd_p->file))
	    ((NONCONST def_dec_info *) dd_p2)->definition = dd_p->definition;
Richard Stallman committed
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
      }

  /* Convert any dummy (-1) definitions we created in the step above back to
     NULL's (as they should be).  */

  for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func)
    if (dd_p->definition == (def_dec_info *) -1)
      ((NONCONST def_dec_info *) dd_p)->definition = NULL;
}

2681
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
2682 2683 2684 2685 2686

/* Give a pointer into the clean text buffer, return a number which is the
   original source line number that the given pointer points into.  */

static int
2687
identify_lineno (const char *clean_p)
Richard Stallman committed
2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700
{
  int line_num = 1;
  const char *scan_p;

  for (scan_p = clean_text_base; scan_p <= clean_p; scan_p++)
    if (*scan_p == '\n')
      line_num++;
  return line_num;
}

/* Issue an error message and give up on doing this particular edit.  */

static void
2701
declare_source_confusing (const char *clean_p)
Richard Stallman committed
2702 2703 2704 2705
{
  if (!quiet_flag)
    {
      if (clean_p == 0)
2706
	notice ("%s: %d: warning: source too confusing\n",
2707
		shortpath (NULL, convert_filename), last_known_line_number);
Richard Stallman committed
2708
      else
2709
	notice ("%s: %d: warning: source too confusing\n",
2710 2711
		shortpath (NULL, convert_filename),
		identify_lineno (clean_p));
Richard Stallman committed
2712 2713 2714 2715 2716 2717 2718 2719
    }
  longjmp (source_confusion_recovery, 1);
}

/* Check that a condition which is expected to be true in the original source
   code is in fact true.  If not, issue an error message and give up on
   converting this particular source file.  */

2720
static void
2721
check_source (int cond, const char *clean_p)
Richard Stallman committed
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
{
  if (!cond)
    declare_source_confusing (clean_p);
}

/* If we think of the in-core cleaned text buffer as a memory mapped
   file (with the variable last_known_line_start acting as sort of a
   file pointer) then we can imagine doing "seeks" on the buffer.  The
   following routine implements a kind of "seek" operation for the in-core
   (cleaned) copy of the source file.  When finished, it returns a pointer to
   the start of a given (numbered) line in the cleaned text buffer.

   Note that protoize only has to "seek" in the forward direction on the
   in-core cleaned text file buffers, and it never needs to back up.

   This routine is made a little bit faster by remembering the line number
   (and pointer value) supplied (and returned) from the previous "seek".
   This prevents us from always having to start all over back at the top
   of the in-core cleaned buffer again.  */

static const char *
2743
seek_to_line (int n)
Richard Stallman committed
2744 2745 2746 2747 2748 2749 2750
{
  if (n < last_known_line_number)
    abort ();

  while (n > last_known_line_number)
    {
      while (*last_known_line_start != '\n')
2751
	check_source (++last_known_line_start < clean_text_limit, 0);
Richard Stallman committed
2752 2753 2754 2755 2756 2757 2758
      last_known_line_start++;
      last_known_line_number++;
    }
  return last_known_line_start;
}

/* Given a pointer to a character in the cleaned text buffer, return a pointer
Richard Kenner committed
2759
   to the next non-whitespace character which follows it.  */
Richard Stallman committed
2760 2761

static const char *
2762
forward_to_next_token_char (const char *ptr)
Richard Stallman committed
2763
{
Kaveh R. Ghazi committed
2764 2765
  for (++ptr; ISSPACE ((const unsigned char)*ptr);
       check_source (++ptr < clean_text_limit, 0))
Richard Stallman committed
2766 2767 2768 2769 2770 2771 2772 2773 2774
    continue;
  return ptr;
}

/* Copy a chunk of text of length `len' and starting at `str' to the current
   output buffer.  Note that all attempts to add stuff to the current output
   buffer ultimately go through here.  */

static void
2775
output_bytes (const char *str, size_t len)
Richard Stallman committed
2776 2777 2778 2779
{
  if ((repl_write_ptr + 1) + len >= repl_text_limit)
    {
      size_t new_size = (repl_text_limit - repl_text_base) << 1;
2780
      char *new_buf = xrealloc (repl_text_base, new_size);
Richard Stallman committed
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793

      repl_write_ptr = new_buf + (repl_write_ptr - repl_text_base);
      repl_text_base = new_buf;
      repl_text_limit = new_buf + new_size;
    }
  memcpy (repl_write_ptr + 1, str, len);
  repl_write_ptr += len;
}

/* Copy all bytes (except the trailing null) of a null terminated string to
   the current output buffer.  */

static void
2794
output_string (const char *str)
Richard Stallman committed
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
{
  output_bytes (str, strlen (str));
}

/* Copy some characters from the original text buffer to the current output
   buffer.

   This routine takes a pointer argument `p' which is assumed to be a pointer
   into the cleaned text buffer.  The bytes which are copied are the `original'
   equivalents for the set of bytes between the last value of `clean_read_ptr'
   and the argument value `p'.

   The set of bytes copied however, comes *not* from the cleaned text buffer,
   but rather from the direct counterparts of these bytes within the original
   text buffer.

   Thus, when this function is called, some bytes from the original text
   buffer (which may include original comments and preprocessing directives)
   will be copied into the  output buffer.

Richard Kenner committed
2815
   Note that the request implied when this routine is called includes the
Richard Stallman committed
2816 2817 2818
   byte pointed to by the argument pointer `p'.  */

static void
2819
output_up_to (const char *p)
Richard Stallman committed
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837
{
  size_t copy_length = (size_t) (p - clean_read_ptr);
  const char *copy_start = orig_text_base+(clean_read_ptr-clean_text_base)+1;

  if (copy_length == 0)
    return;

  output_bytes (copy_start, copy_length);
  clean_read_ptr = p;
}

/* Given a pointer to a def_dec_info record which represents some form of
   definition of a function (perhaps a real definition, or in lieu of that
   perhaps just a declaration with a full prototype) return true if this
   function is one which we should avoid converting.  Return false
   otherwise.  */

static int
2838
other_variable_style_function (const char *ansi_header)
Richard Stallman committed
2839 2840 2841 2842 2843 2844
{
#ifdef UNPROTOIZE

  /* See if we have a stdarg function, or a function which has stdarg style
     parameters or a stdarg style return type.  */

2845
  return strstr (ansi_header, "...") != 0;
Richard Stallman committed
2846

2847
#else /* !defined (UNPROTOIZE) */
Richard Stallman committed
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858

  /* See if we have a varargs function, or a function which has varargs style
     parameters or a varargs style return type.  */

  const char *p;
  int len = strlen (varargs_style_indicator);

  for (p = ansi_header; p; )
    {
      const char *candidate;

2859
      if ((candidate = strstr (p, varargs_style_indicator)) == 0)
2860
	return 0;
Richard Stallman committed
2861
      else
2862 2863 2864 2865
	if (!is_id_char (candidate[-1]) && !is_id_char (candidate[len]))
	  return 1;
	else
	  p = candidate + 1;
Richard Stallman committed
2866 2867
    }
  return 0;
2868
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
2869 2870 2871 2872 2873 2874 2875
}

/* Do the editing operation specifically for a function "declaration".  Note
   that editing for function "definitions" are handled in a separate routine
   below.  */

static void
2876 2877
edit_fn_declaration (const def_dec_info *def_dec_p,
		     const char *volatile clean_text_p)
Richard Stallman committed
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
{
  const char *start_formals;
  const char *end_formals;
  const char *function_to_edit = def_dec_p->hash_entry->symbol;
  size_t func_name_len = strlen (function_to_edit);
  const char *end_of_fn_name;

#ifndef UNPROTOIZE

  const f_list_chain_item *this_f_list_chain_item;
  const def_dec_info *definition = def_dec_p->definition;

  /* If we are protoizing, and if we found no corresponding definition for
     this particular function declaration, then just leave this declaration
     exactly as it is.  */

  if (!definition)
    return;

  /* If we are protoizing, and if the corresponding definition that we found
     for this particular function declaration defined an old style varargs
     function, then we want to issue a warning and just leave this function
     declaration unconverted.  */

  if (other_variable_style_function (definition->ansi_decl))
    {
      if (!quiet_flag)
2905
	notice ("%s: %d: warning: varargs function declaration not converted\n",
2906 2907
		shortpath (NULL, def_dec_p->file->hash_entry->symbol),
		def_dec_p->line);
Richard Stallman committed
2908 2909 2910
      return;
    }

2911
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
2912 2913 2914 2915 2916 2917 2918 2919

  /* Setup here to recover from confusing source code detected during this
     particular "edit".  */

  save_pointers ();
  if (setjmp (source_confusion_recovery))
    {
      restore_pointers ();
2920
      notice ("%s: declaration of function '%s' not converted\n",
2921
	      pname, function_to_edit);
Richard Stallman committed
2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
      return;
    }

  /* We are editing a function declaration.  The line number we did a seek to
     contains the comma or semicolon which follows the declaration.  Our job
     now is to scan backwards looking for the function name.  This name *must*
     be followed by open paren (ignoring whitespace, of course).  We need to
     replace everything between that open paren and the corresponding closing
     paren.  If we are protoizing, we need to insert the prototype-style
     formals lists.  If we are unprotoizing, we need to just delete everything
     between the pairs of opening and closing parens.  */

  /* First move up to the end of the line.  */

  while (*clean_text_p != '\n')
    check_source (++clean_text_p < clean_text_limit, 0);
  clean_text_p--;  /* Point to just before the newline character.  */

  /* Now we can scan backwards for the function name.  */

  do
    {
      for (;;)
2945 2946 2947 2948 2949 2950
	{
	  /* Scan leftwards until we find some character which can be
	     part of an identifier.  */

	  while (!is_id_char (*clean_text_p))
	    check_source (--clean_text_p > clean_read_ptr, 0);
Richard Stallman committed
2951

2952 2953
	  /* Scan backwards until we find a char that cannot be part of an
	     identifier.  */
Richard Stallman committed
2954

2955 2956
	  while (is_id_char (*clean_text_p))
	    check_source (--clean_text_p > clean_read_ptr, 0);
Richard Stallman committed
2957

2958 2959
	  /* Having found an "id break", see if the following id is the one
	     that we are looking for.  If so, then exit from this loop.  */
Richard Stallman committed
2960

2961 2962 2963
	  if (!strncmp (clean_text_p+1, function_to_edit, func_name_len))
	    {
	      char ch = *(clean_text_p + 1 + func_name_len);
Richard Stallman committed
2964

2965 2966 2967
	      /* Must also check to see that the name in the source text
	         ends where it should (in order to prevent bogus matches
	         on similar but longer identifiers.  */
Richard Stallman committed
2968

2969 2970 2971 2972
	      if (! is_id_char (ch))
	        break;			/* exit from loop */
	    }
	}
Richard Stallman committed
2973 2974

      /* We have now found the first perfect match for the function name in
2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
	 our backward search.  This may or may not be the actual function
	 name at the start of the actual function declaration (i.e. we could
	 have easily been mislead).  We will try to avoid getting fooled too
	 often by looking forward for the open paren which should follow the
	 identifier we just found.  We ignore whitespace while hunting.  If
	 the next non-whitespace byte we see is *not* an open left paren,
	 then we must assume that we have been fooled and we start over
	 again accordingly.  Note that there is no guarantee, that even if
	 we do see the open paren, that we are in the right place.
	 Programmers do the strangest things sometimes!  */

Richard Stallman committed
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
      end_of_fn_name = clean_text_p + strlen (def_dec_p->hash_entry->symbol);
      start_formals = forward_to_next_token_char (end_of_fn_name);
    }
  while (*start_formals != '(');

  /* start_of_formals now points to the opening left paren which immediately
     follows the name of the function.  */

  /* Note that there may be several formals lists which need to be modified
     due to the possibility that the return type of this function is a
     pointer-to-function type.  If there are several formals lists, we
     convert them in left-to-right order here.  */

#ifndef UNPROTOIZE
  this_f_list_chain_item = definition->f_list_chain;
3001
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
3002 3003 3004 3005

  for (;;)
    {
      {
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
	int depth;

	end_formals = start_formals + 1;
	depth = 1;
	for (; depth; check_source (++end_formals < clean_text_limit, 0))
	  {
	    switch (*end_formals)
	      {
	      case '(':
		depth++;
		break;
	      case ')':
		depth--;
		break;
	      }
	  }
	end_formals--;
Richard Stallman committed
3023 3024 3025
      }

      /* end_formals now points to the closing right paren of the formals
3026 3027
	 list whose left paren is pointed to by start_formals.  */

Richard Stallman committed
3028
      /* Now, if we are protoizing, we insert the new ANSI-style formals list
3029 3030 3031 3032
	 attached to the associated definition of this function.  If however
	 we are unprotoizing, then we simply delete any formals list which
	 may be present.  */

Richard Stallman committed
3033 3034 3035
      output_up_to (start_formals);
#ifndef UNPROTOIZE
      if (this_f_list_chain_item)
3036 3037 3038 3039
	{
	  output_string (this_f_list_chain_item->formals_list);
	  this_f_list_chain_item = this_f_list_chain_item->chain_next;
	}
Richard Stallman committed
3040
      else
3041 3042
	{
	  if (!quiet_flag)
3043
	    notice ("%s: warning: too many parameter lists in declaration of '%s'\n",
3044
		    pname, def_dec_p->hash_entry->symbol);
3045 3046
	  check_source (0, end_formals);  /* leave the declaration intact */
	}
3047
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
3048 3049 3050
      clean_read_ptr = end_formals - 1;

      /* Now see if it looks like there may be another formals list associated
3051 3052
	 with the function declaration that we are converting (following the
	 formals list that we just converted.  */
Richard Stallman committed
3053 3054

      {
3055
	const char *another_r_paren = forward_to_next_token_char (end_formals);
Richard Stallman committed
3056

3057 3058 3059
	if ((*another_r_paren != ')')
	    || (*(start_formals = forward_to_next_token_char (another_r_paren)) != '('))
	  {
Richard Stallman committed
3060
#ifndef UNPROTOIZE
3061 3062 3063
	    if (this_f_list_chain_item)
	      {
		if (!quiet_flag)
3064
		  notice ("\n%s: warning: too few parameter lists in declaration of '%s'\n",
3065
			  pname, def_dec_p->hash_entry->symbol);
3066 3067
		check_source (0, start_formals); /* leave the decl intact */
	      }
3068
#endif /* !defined (UNPROTOIZE) */
3069 3070 3071
	    break;

	  }
Richard Stallman committed
3072 3073 3074
      }

      /* There does appear to be yet another formals list, so loop around
3075
	 again, and convert it also.  */
Richard Stallman committed
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085
    }
}

/* Edit a whole group of formals lists, starting with the rightmost one
   from some set of formals lists.  This routine is called once (from the
   outside) for each function declaration which is converted.  It is
   recursive however, and it calls itself once for each remaining formal
   list that lies to the left of the one it was originally called to work
   on.  Thus, a whole set gets done in right-to-left order.

3086
   This routine returns nonzero if it thinks that it should not be trying
Richard Stallman committed
3087 3088 3089 3090
   to convert this particular function definition (because the name of the
   function doesn't match the one expected).  */

static int
3091 3092
edit_formals_lists (const char *end_formals, unsigned int f_list_count,
		    const def_dec_info *def_dec_p)
Richard Stallman committed
3093 3094 3095 3096 3097 3098 3099 3100 3101
{
  const char *start_formals;
  int depth;

  start_formals = end_formals - 1;
  depth = 1;
  for (; depth; check_source (--start_formals > clean_read_ptr, 0))
    {
      switch (*start_formals)
3102 3103 3104 3105 3106 3107 3108 3109
	{
	case '(':
	  depth--;
	  break;
	case ')':
	  depth++;
	  break;
	}
Richard Stallman committed
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124
    }
  start_formals++;

  /* start_formals now points to the opening left paren of the formals list.  */

  f_list_count--;

  if (f_list_count)
    {
      const char *next_end;

      /* There should be more formal lists to the left of here.  */

      next_end = start_formals - 1;
      check_source (next_end > clean_read_ptr, 0);
Kaveh R. Ghazi committed
3125
      while (ISSPACE ((const unsigned char)*next_end))
3126
	check_source (--next_end > clean_read_ptr, 0);
Richard Stallman committed
3127 3128 3129 3130
      check_source (*next_end == ')', next_end);
      check_source (--next_end > clean_read_ptr, 0);
      check_source (*next_end == ')', next_end);
      if (edit_formals_lists (next_end, f_list_count, def_dec_p))
3131
	return 1;
Richard Stallman committed
3132 3133 3134 3135
    }

  /* Check that the function name in the header we are working on is the same
     as the one we would expect to find.  If not, issue a warning and return
3136
     nonzero.  */
Richard Stallman committed
3137 3138 3139 3140 3141 3142 3143 3144

  if (f_list_count == 0)
    {
      const char *expected = def_dec_p->hash_entry->symbol;
      const char *func_name_start;
      const char *func_name_limit;
      size_t func_name_len;

Kaveh R. Ghazi committed
3145 3146
      for (func_name_limit = start_formals-1;
	   ISSPACE ((const unsigned char)*func_name_limit); )
3147
	check_source (--func_name_limit > clean_read_ptr, 0);
Richard Stallman committed
3148 3149

      for (func_name_start = func_name_limit++;
3150 3151 3152
	   is_id_char (*func_name_start);
	   func_name_start--)
	check_source (func_name_start > clean_read_ptr, 0);
Richard Stallman committed
3153 3154 3155
      func_name_start++;
      func_name_len = func_name_limit - func_name_start;
      if (func_name_len == 0)
3156
	check_source (0, func_name_start);
Richard Stallman committed
3157
      if (func_name_len != strlen (expected)
3158
	  || strncmp (func_name_start, expected, func_name_len))
3159
	{
3160
	  notice ("%s: %d: warning: found '%s' but expected '%s'\n",
3161 3162 3163 3164
		  shortpath (NULL, def_dec_p->file->hash_entry->symbol),
		  identify_lineno (func_name_start),
		  dupnstr (func_name_start, func_name_len),
		  expected);
3165 3166
	  return 1;
	}
Richard Stallman committed
3167 3168 3169 3170 3171 3172 3173
    }

  output_up_to (start_formals);

#ifdef UNPROTOIZE
  if (f_list_count == 0)
    output_string (def_dec_p->formal_names);
3174
#else /* !defined (UNPROTOIZE) */
Richard Stallman committed
3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186
  {
    unsigned f_list_depth;
    const f_list_chain_item *flci_p = def_dec_p->f_list_chain;

    /* At this point, the current value of f_list count says how many
       links we have to follow through the f_list_chain to get to the
       particular formals list that we need to output next.  */

    for (f_list_depth = 0; f_list_depth < f_list_count; f_list_depth++)
      flci_p = flci_p->chain_next;
    output_string (flci_p->formals_list);
  }
3187
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
3188 3189 3190 3191 3192

  clean_read_ptr = end_formals - 1;
  return 0;
}

Mike Stump committed
3193 3194 3195 3196 3197
/* Given a pointer to a byte in the clean text buffer which points to
   the beginning of a line that contains a "follower" token for a
   function definition header, do whatever is necessary to find the
   right closing paren for the rightmost formals list of the function
   definition header.  */
Richard Stallman committed
3198 3199

static const char *
3200
find_rightmost_formals_list (const char *clean_text_p)
Richard Stallman committed
3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
{
  const char *end_formals;

  /* We are editing a function definition.  The line number we did a seek
     to contains the first token which immediately follows the entire set of
     formals lists which are part of this particular function definition
     header.

     Our job now is to scan leftwards in the clean text looking for the
     right-paren which is at the end of the function header's rightmost
     formals list.

     If we ignore whitespace, this right paren should be the first one we
     see which is (ignoring whitespace) immediately followed either by the
     open curly-brace beginning the function body or by an alphabetic
     character (in the case where the function definition is in old (K&R)
     style and there are some declarations of formal parameters).  */

   /* It is possible that the right paren we are looking for is on the
      current line (together with its following token).  Just in case that
      might be true, we start out here by skipping down to the right end of
      the current line before starting our scan.  */

  for (end_formals = clean_text_p; *end_formals != '\n'; end_formals++)
    continue;
  end_formals--;

3228 3229
#ifdef UNPROTOIZE

Richard Stallman committed
3230 3231 3232
  /* Now scan backwards while looking for the right end of the rightmost
     formals list associated with this function definition.  */

3233 3234 3235 3236 3237 3238 3239 3240
  {
    char ch;
    const char *l_brace_p;

    /* Look leftward and try to find a right-paren.  */

    while (*end_formals != ')')
      {
Kaveh R. Ghazi committed
3241 3242
	if (ISSPACE ((unsigned char)*end_formals))
	  while (ISSPACE ((unsigned char)*end_formals))
3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256
	    check_source (--end_formals > clean_read_ptr, 0);
	else
	  check_source (--end_formals > clean_read_ptr, 0);
      }

    ch = *(l_brace_p = forward_to_next_token_char (end_formals));
    /* Since we are unprotoizing an ANSI-style (prototyped) function
       definition, there had better not be anything (except whitespace)
       between the end of the ANSI formals list and the beginning of the
       function body (i.e. the '{').  */

    check_source (ch == '{', l_brace_p);
  }

3257
#else /* !defined (UNPROTOIZE) */
3258 3259 3260 3261 3262

  /* Now scan backwards while looking for the right end of the rightmost
     formals list associated with this function definition.  */

  while (1)
Richard Stallman committed
3263 3264 3265 3266 3267 3268 3269
    {
      char ch;
      const char *l_brace_p;

      /* Look leftward and try to find a right-paren.  */

      while (*end_formals != ')')
3270 3271 3272 3273 3274 3275 3276
	{
	  if (ISSPACE ((const unsigned char)*end_formals))
	    while (ISSPACE ((const unsigned char)*end_formals))
	      check_source (--end_formals > clean_read_ptr, 0);
	  else
	    check_source (--end_formals > clean_read_ptr, 0);
	}
Richard Stallman committed
3277 3278 3279 3280

      ch = *(l_brace_p = forward_to_next_token_char (end_formals));

      /* Since it is possible that we found a right paren before the starting
3281 3282 3283 3284
	 '{' of the body which IS NOT the one at the end of the real K&R
	 formals list (say for instance, we found one embedded inside one of
	 the old K&R formal parameter declarations) we have to check to be
	 sure that this is in fact the right paren that we were looking for.
Richard Stallman committed
3285

3286 3287 3288
	 The one we were looking for *must* be followed by either a '{' or
	 by an alphabetic character, while others *cannot* validly be followed
	 by such characters.  */
Richard Stallman committed
3289

Kazu Hirata committed
3290
      if ((ch == '{') || ISALPHA ((unsigned char) ch))
3291
	break;
Richard Stallman committed
3292 3293

      /* At this point, we have found a right paren, but we know that it is
3294 3295
	 not the one we were looking for, so backup one character and keep
	 looking.  */
Richard Stallman committed
3296 3297

      check_source (--end_formals > clean_read_ptr, 0);
3298
    }
Richard Stallman committed
3299

3300
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316

  return end_formals;
}

#ifndef UNPROTOIZE

/* Insert into the output file a totally new declaration for a function
   which (up until now) was being called from within the current block
   without having been declared at any point such that the declaration
   was visible (i.e. in scope) at the point of the call.

   We need to add in explicit declarations for all such function calls
   in order to get the full benefit of prototype-based function call
   parameter type checking.  */

static void
3317
add_local_decl (const def_dec_info *def_dec_p, const char *clean_text_p)
Richard Stallman committed
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334
{
  const char *start_of_block;
  const char *function_to_edit = def_dec_p->hash_entry->symbol;

  /* Don't insert new local explicit declarations unless explicitly requested
     to do so.  */

  if (!local_flag)
    return;

  /* Setup here to recover from confusing source code detected during this
     particular "edit".  */

  save_pointers ();
  if (setjmp (source_confusion_recovery))
    {
      restore_pointers ();
3335
      notice ("%s: local declaration for function '%s' not inserted\n",
3336
	      pname, function_to_edit);
Richard Stallman committed
3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361
      return;
    }

  /* We have already done a seek to the start of the line which should
     contain *the* open curly brace which begins the block in which we need
     to insert an explicit function declaration (to replace the implicit one).

     Now we scan that line, starting from the left, until we find the
     open curly brace we are looking for.  Note that there may actually be
     multiple open curly braces on the given line, but we will be happy
     with the leftmost one no matter what.  */

  start_of_block = clean_text_p;
  while (*start_of_block != '{' && *start_of_block != '\n')
    check_source (++start_of_block < clean_text_limit, 0);

  /* Note that the line from the original source could possibly
     contain *no* open curly braces!  This happens if the line contains
     a macro call which expands into a chunk of text which includes a
     block (and that block's associated open and close curly braces).
     In cases like this, we give up, issue a warning, and do nothing.  */

  if (*start_of_block != '{')
    {
      if (!quiet_flag)
3362
	notice ("\n%s: %d: warning: can't add declaration of '%s' into macro call\n",
3363 3364
	  def_dec_p->file->hash_entry->symbol, def_dec_p->line,
	  def_dec_p->hash_entry->symbol);
Richard Stallman committed
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382
      return;
    }

  /* Figure out what a nice (pretty) indentation would be for the new
     declaration we are adding.  In order to do this, we must scan forward
     from the '{' until we find the first line which starts with some
     non-whitespace characters (i.e. real "token" material).  */

  {
    const char *ep = forward_to_next_token_char (start_of_block) - 1;
    const char *sp;

    /* Now we have ep pointing at the rightmost byte of some existing indent
       stuff.  At least that is the hope.

       We can now just scan backwards and find the left end of the existing
       indentation string, and then copy it to the output buffer.  */

Kaveh R. Ghazi committed
3383
    for (sp = ep; ISSPACE ((const unsigned char)*sp) && *sp != '\n'; sp--)
Richard Stallman committed
3384 3385 3386 3387 3388 3389 3390
      continue;

    /* Now write out the open { which began this block, and any following
       trash up to and including the last byte of the existing indent that
       we just found.  */

    output_up_to (ep);
3391

Richard Stallman committed
3392 3393 3394 3395 3396
    /* Now we go ahead and insert the new declaration at this point.

       If the definition of the given function is in the same file that we
       are currently editing, and if its full ANSI declaration normally
       would start with the keyword `extern', suppress the `extern'.  */
3397

Richard Stallman committed
3398 3399
    {
      const char *decl = def_dec_p->definition->ansi_decl;
3400

Richard Stallman committed
3401
      if ((*decl == 'e') && (def_dec_p->file == def_dec_p->definition->file))
3402
	decl += 7;
Richard Stallman committed
3403 3404 3405
      output_string (decl);
    }

3406
    /* Finally, write out a new indent string, just like the preceding one
Richard Stallman committed
3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421
       that we found.  This will typically include a newline as the first
       character of the indent string.  */

    output_bytes (sp, (size_t) (ep - sp) + 1);
  }
}

/* Given a pointer to a file_info record, and a pointer to the beginning
   of a line (in the clean text buffer) which is assumed to contain the
   first "follower" token for the first function definition header in the
   given file, find a good place to insert some new global function
   declarations (which will replace scattered and imprecise implicit ones)
   and then insert the new explicit declaration at that point in the file.  */

static void
3422
add_global_decls (const file_info *file_p, const char *clean_text_p)
Richard Stallman committed
3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
{
  const def_dec_info *dd_p;
  const char *scan_p;

  /* Setup here to recover from confusing source code detected during this
     particular "edit".  */

  save_pointers ();
  if (setjmp (source_confusion_recovery))
    {
      restore_pointers ();
3434
      notice ("%s: global declarations for file '%s' not inserted\n",
3435
	      pname, shortpath (NULL, file_p->hash_entry->symbol));
Richard Stallman committed
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447
      return;
    }

  /* Start by finding a good location for adding the new explicit function
     declarations.  To do this, we scan backwards, ignoring whitespace
     and comments and other junk until we find either a semicolon, or until
     we hit the beginning of the file.  */

  scan_p = find_rightmost_formals_list (clean_text_p);
  for (;; --scan_p)
    {
      if (scan_p < clean_text_base)
3448
	break;
Richard Stallman committed
3449 3450
      check_source (scan_p > clean_read_ptr, 0);
      if (*scan_p == ';')
3451
	break;
Richard Stallman committed
3452 3453 3454 3455 3456 3457 3458
    }

  /* scan_p now points either to a semicolon, or to just before the start
     of the whole file.  */

  /* Now scan forward for the first non-whitespace character.  In theory,
     this should be the first character of the following function definition
Mike Stump committed
3459
     header.  We will put in the added declarations just prior to that.  */
Richard Stallman committed
3460 3461

  scan_p++;
Kaveh R. Ghazi committed
3462
  while (ISSPACE ((const unsigned char)*scan_p))
Richard Stallman committed
3463 3464 3465 3466 3467 3468 3469 3470
    scan_p++;
  scan_p--;

  output_up_to (scan_p);

  /* Now write out full prototypes for all of the things that had been
     implicitly declared in this file (but only those for which we were
     actually able to find unique matching definitions).  Avoid duplicates
Kazu Hirata committed
3471
     by marking things that we write out as we go.  */
Richard Stallman committed
3472 3473 3474

  {
    int some_decls_added = 0;
3475

Richard Stallman committed
3476 3477
    for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file)
      if (dd_p->is_implicit && dd_p->definition && !dd_p->definition->written)
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492
	{
	  const char *decl = dd_p->definition->ansi_decl;

	  /* If the function for which we are inserting a declaration is
	     actually defined later in the same file, then suppress the
	     leading `extern' keyword (if there is one).  */

	  if (*decl == 'e' && (dd_p->file == dd_p->definition->file))
	    decl += 7;

	  output_string ("\n");
	  output_string (decl);
	  some_decls_added = 1;
	  ((NONCONST def_dec_info *) dd_p->definition)->written = 1;
	}
Richard Stallman committed
3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503
    if (some_decls_added)
      output_string ("\n\n");
  }

  /* Unmark all of the definitions that we just marked.  */

  for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file)
    if (dd_p->definition)
      ((NONCONST def_dec_info *) dd_p->definition)->written = 0;
}

3504
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
3505 3506 3507 3508 3509 3510

/* Do the editing operation specifically for a function "definition".  Note
   that editing operations for function "declarations" are handled by a
   separate routine above.  */

static void
3511
edit_fn_definition (const def_dec_info *def_dec_p, const char *clean_text_p)
Richard Stallman committed
3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
{
  const char *end_formals;
  const char *function_to_edit = def_dec_p->hash_entry->symbol;

  /* Setup here to recover from confusing source code detected during this
     particular "edit".  */

  save_pointers ();
  if (setjmp (source_confusion_recovery))
    {
      restore_pointers ();
3523
      notice ("%s: definition of function '%s' not converted\n",
3524
	      pname, function_to_edit);
Richard Stallman committed
3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537
      return;
    }

  end_formals = find_rightmost_formals_list (clean_text_p);

  /* end_of_formals now points to the closing right paren of the rightmost
     formals list which is actually part of the `header' of the function
     definition that we are converting.  */

  /* If the header of this function definition looks like it declares a
     function with a variable number of arguments, and if the way it does
     that is different from that way we would like it (i.e. varargs vs.
     stdarg) then issue a warning and leave the header unconverted.  */
3538

Richard Stallman committed
3539 3540 3541
  if (other_variable_style_function (def_dec_p->ansi_decl))
    {
      if (!quiet_flag)
3542
	notice ("%s: %d: warning: definition of %s not converted\n",
3543
		shortpath (NULL, def_dec_p->file->hash_entry->symbol),
3544
		identify_lineno (end_formals),
3545
		other_var_style);
Richard Stallman committed
3546 3547 3548 3549 3550 3551 3552
      output_up_to (end_formals);
      return;
    }

  if (edit_formals_lists (end_formals, def_dec_p->f_list_count, def_dec_p))
    {
      restore_pointers ();
3553
      notice ("%s: definition of function '%s' not converted\n",
3554
	      pname, function_to_edit);
Richard Stallman committed
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
      return;
    }

  /* Have to output the last right paren because this never gets flushed by
     edit_formals_list.  */

  output_up_to (end_formals);

#ifdef UNPROTOIZE
  {
    const char *decl_p;
    const char *semicolon_p;
    const char *limit_p;
    const char *scan_p;
    int had_newlines = 0;

    /* Now write out the K&R style formal declarations, one per line.  */

    decl_p = def_dec_p->formal_decls;
    limit_p = decl_p + strlen (decl_p);
    for (;decl_p < limit_p; decl_p = semicolon_p + 2)
      {
3577 3578 3579 3580 3581
	for (semicolon_p = decl_p; *semicolon_p != ';'; semicolon_p++)
	  continue;
	output_string ("\n");
	output_string (indent_string);
	output_bytes (decl_p, (size_t) ((semicolon_p + 1) - decl_p));
Richard Stallman committed
3582 3583 3584 3585 3586 3587 3588
      }

    /* If there are no newlines between the end of the formals list and the
       start of the body, we should insert one now.  */

    for (scan_p = end_formals+1; *scan_p != '{'; )
      {
3589 3590 3591 3592 3593 3594
	if (*scan_p == '\n')
	  {
	    had_newlines = 1;
	    break;
	  }
	check_source (++scan_p < clean_text_limit, 0);
Richard Stallman committed
3595 3596 3597 3598
      }
    if (!had_newlines)
      output_string ("\n");
  }
3599
#else /* !defined (UNPROTOIZE) */
Richard Kenner committed
3600
  /* If we are protoizing, there may be some flotsam & jetsam (like comments
Richard Stallman committed
3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612
     and preprocessing directives) after the old formals list but before
     the following { and we would like to preserve that stuff while effectively
     deleting the existing K&R formal parameter declarations.  We do so here
     in a rather tricky way.  Basically, we white out any stuff *except*
     the comments/pp-directives in the original text buffer, then, if there
     is anything in this area *other* than whitespace, we output it.  */
  {
    const char *end_formals_orig;
    const char *start_body;
    const char *start_body_orig;
    const char *scan;
    const char *scan_orig;
3613
    int have_flotsam = 0;
Richard Stallman committed
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
    int have_newlines = 0;

    for (start_body = end_formals + 1; *start_body != '{';)
      check_source (++start_body < clean_text_limit, 0);

    end_formals_orig = orig_text_base + (end_formals - clean_text_base);
    start_body_orig = orig_text_base + (start_body - clean_text_base);
    scan = end_formals + 1;
    scan_orig = end_formals_orig + 1;
    for (; scan < start_body; scan++, scan_orig++)
      {
3625 3626 3627 3628 3629 3630 3631 3632 3633
	if (*scan == *scan_orig)
	  {
	    have_newlines |= (*scan_orig == '\n');
	    /* Leave identical whitespace alone.  */
	    if (!ISSPACE ((const unsigned char)*scan_orig))
	      *((NONCONST char *) scan_orig) = ' '; /* identical - so whiteout */
	  }
	else
	  have_flotsam = 1;
Richard Stallman committed
3634
      }
3635
    if (have_flotsam)
Richard Stallman committed
3636 3637 3638 3639
      output_bytes (end_formals_orig + 1,
		    (size_t) (start_body_orig - end_formals_orig) - 1);
    else
      if (have_newlines)
3640
	output_string ("\n");
Richard Stallman committed
3641
      else
3642
	output_string (" ");
Richard Stallman committed
3643 3644
    clean_read_ptr = start_body - 1;
  }
3645
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
3646 3647 3648
}

/* Clean up the clean text buffer.  Do this by converting comments and
3649
   preprocessing directives into spaces.   Also convert line continuations
Richard Stallman committed
3650 3651 3652
   into whitespace.  Also, whiteout string and character literals.  */

static void
3653
do_cleaning (char *new_clean_text_base, const char *new_clean_text_limit)
Richard Stallman committed
3654 3655 3656 3657 3658 3659 3660
{
  char *scan_p;
  int non_whitespace_since_newline = 0;

  for (scan_p = new_clean_text_base; scan_p < new_clean_text_limit; scan_p++)
    {
      switch (*scan_p)
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744
	{
	case '/':			/* Handle comments.  */
	  if (scan_p[1] != '*')
	    goto regular;
	  non_whitespace_since_newline = 1;
	  scan_p[0] = ' ';
	  scan_p[1] = ' ';
	  scan_p += 2;
	  while (scan_p[1] != '/' || scan_p[0] != '*')
	    {
	      if (!ISSPACE ((const unsigned char)*scan_p))
		*scan_p = ' ';
	      if (++scan_p >= new_clean_text_limit)
		abort ();
	    }
	  *scan_p++ = ' ';
	  *scan_p = ' ';
	  break;

	case '#':			/* Handle pp directives.  */
	  if (non_whitespace_since_newline)
	    goto regular;
	  *scan_p = ' ';
	  while (scan_p[1] != '\n' || scan_p[0] == '\\')
	    {
	      if (!ISSPACE ((const unsigned char)*scan_p))
		*scan_p = ' ';
	      if (++scan_p >= new_clean_text_limit)
		abort ();
	    }
	  *scan_p++ = ' ';
	  break;

	case '\'':			/* Handle character literals.  */
	  non_whitespace_since_newline = 1;
	  while (scan_p[1] != '\'' || scan_p[0] == '\\')
	    {
	      if (scan_p[0] == '\\'
		  && !ISSPACE ((const unsigned char) scan_p[1]))
		scan_p[1] = ' ';
	      if (!ISSPACE ((const unsigned char)*scan_p))
		*scan_p = ' ';
	      if (++scan_p >= new_clean_text_limit)
		abort ();
	    }
	  *scan_p++ = ' ';
	  break;

	case '"':			/* Handle string literals.  */
	  non_whitespace_since_newline = 1;
	  while (scan_p[1] != '"' || scan_p[0] == '\\')
	    {
	      if (scan_p[0] == '\\'
		  && !ISSPACE ((const unsigned char) scan_p[1]))
		scan_p[1] = ' ';
	      if (!ISSPACE ((const unsigned char)*scan_p))
		*scan_p = ' ';
	      if (++scan_p >= new_clean_text_limit)
		abort ();
	    }
	  if (!ISSPACE ((const unsigned char)*scan_p))
	    *scan_p = ' ';
	  scan_p++;
	  break;

	case '\\':			/* Handle line continuations.  */
	  if (scan_p[1] != '\n')
	    goto regular;
	  *scan_p = ' ';
	  break;

	case '\n':
	  non_whitespace_since_newline = 0;	/* Reset.  */
	  break;

	case ' ':
	case '\v':
	case '\t':
	case '\r':
	case '\f':
	case '\b':
	  break;		/* Whitespace characters.  */

	default:
Richard Stallman committed
3745
regular:
3746 3747 3748
	  non_whitespace_since_newline = 1;
	  break;
	}
Richard Stallman committed
3749 3750 3751 3752 3753 3754 3755 3756
    }
}

/* Given a pointer to the closing right parenthesis for a particular formals
   list (in the clean text buffer) find the corresponding left parenthesis
   and return a pointer to it.  */

static const char *
3757
careful_find_l_paren (const char *p)
Richard Stallman committed
3758 3759 3760 3761 3762 3763 3764
{
  const char *q;
  int paren_depth;

  for (paren_depth = 1, q = p-1; paren_depth; check_source (--q >= clean_text_base, 0))
    {
      switch (*q)
3765 3766 3767 3768 3769 3770 3771 3772
	{
	case ')':
	  paren_depth++;
	  break;
	case '(':
	  paren_depth--;
	  break;
	}
Richard Stallman committed
3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794
    }
  return ++q;
}

/* Scan the clean text buffer for cases of function definitions that we
   don't really know about because they were preprocessed out when the
   aux info files were created.

   In this version of protoize/unprotoize we just give a warning for each
   one found.  A later version may be able to at least unprotoize such
   missed items.

   Note that we may easily find all function definitions simply by
   looking for places where there is a left paren which is (ignoring
   whitespace) immediately followed by either a left-brace or by an
   upper or lower case letter.  Whenever we find this combination, we
   have also found a function definition header.

   Finding function *declarations* using syntactic clues is much harder.
   I will probably try to do this in a later version though.  */

static void
3795
scan_for_missed_items (const file_info *file_p)
Richard Stallman committed
3796 3797 3798 3799 3800 3801 3802 3803 3804 3805
{
  static const char *scan_p;
  const char *limit = clean_text_limit - 3;
  static const char *backup_limit;

  backup_limit = clean_text_base - 1;

  for (scan_p = clean_text_base; scan_p < limit; scan_p++)
    {
      if (*scan_p == ')')
3806 3807 3808
	{
	  static const char *last_r_paren;
	  const char *ahead_p;
Richard Stallman committed
3809

3810
	  last_r_paren = scan_p;
Richard Stallman committed
3811

3812 3813
	  for (ahead_p = scan_p + 1; ISSPACE ((const unsigned char)*ahead_p); )
	    check_source (++ahead_p < limit, limit);
Richard Stallman committed
3814

3815
	  scan_p = ahead_p - 1;
Richard Stallman committed
3816

3817 3818 3819 3820
	  if (ISALPHA ((const unsigned char)*ahead_p) || *ahead_p == '{')
	    {
	      const char *last_l_paren;
	      const int lineno = identify_lineno (ahead_p);
Richard Stallman committed
3821

3822 3823
	      if (setjmp (source_confusion_recovery))
		continue;
Richard Stallman committed
3824

3825 3826
	      /* We know we have a function definition header.  Now skip
	         leftwards over all of its associated formals lists.  */
Richard Stallman committed
3827

3828 3829 3830 3831
	      do
		{
		  last_l_paren = careful_find_l_paren (last_r_paren);
		  for (last_r_paren = last_l_paren-1;
Kaveh R. Ghazi committed
3832
		       ISSPACE ((const unsigned char)*last_r_paren); )
3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849
		    check_source (--last_r_paren >= backup_limit, backup_limit);
		}
	      while (*last_r_paren == ')');

	      if (is_id_char (*last_r_paren))
		{
		  const char *id_limit = last_r_paren + 1;
		  const char *id_start;
		  size_t id_length;
		  const def_dec_info *dd_p;

		  for (id_start = id_limit-1; is_id_char (*id_start); )
		    check_source (--id_start >= backup_limit, backup_limit);
		  id_start++;
		  backup_limit = id_start;
		  if ((id_length = (size_t) (id_limit - id_start)) == 0)
		    goto not_missed;
Richard Stallman committed
3850 3851

		  {
3852
		    char *func_name = alloca (id_length + 1);
Richard Stallman committed
3853
		    static const char * const stmt_keywords[]
3854
		      = { "if", "else", "do", "while", "for", "switch", "case", "return", 0 };
Richard Stallman committed
3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867
		    const char * const *stmt_keyword;

		    strncpy (func_name, id_start, id_length);
		    func_name[id_length] = '\0';

		    /* We must check here to see if we are actually looking at
		       a statement rather than an actual function call.  */

		    for (stmt_keyword = stmt_keywords; *stmt_keyword; stmt_keyword++)
		      if (!strcmp (func_name, *stmt_keyword))
			goto not_missed;

#if 0
3868
		    notice ("%s: found definition of '%s' at %s(%d)\n",
3869 3870 3871 3872
			    pname,
			    func_name,
			    shortpath (NULL, file_p->hash_entry->symbol),
			    identify_lineno (id_start));
Richard Stallman committed
3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
#endif				/* 0 */
		    /* We really should check for a match of the function name
		       here also, but why bother.  */

		    for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file)
		      if (dd_p->is_func_def && dd_p->line == lineno)
			goto not_missed;

		    /* If we make it here, then we did not know about this
		       function definition.  */

3884
		    notice ("%s: %d: warning: '%s' excluded by preprocessing\n",
3885 3886 3887 3888
			    shortpath (NULL, file_p->hash_entry->symbol),
			    identify_lineno (id_start), func_name);
		    notice ("%s: function definition not converted\n",
			    pname);
Richard Stallman committed
3889 3890
		  }
		not_missed: ;
3891 3892 3893
	        }
	    }
	}
Richard Stallman committed
3894 3895 3896 3897 3898 3899
    }
}

/* Do all editing operations for a single source file (either a "base" file
   or an "include" file).  To do this we read the file into memory, keep a
   virgin copy there, make another cleaned in-core copy of the original file
3900
   (i.e. one in which all of the comments and preprocessing directives have
Richard Stallman committed
3901 3902 3903 3904 3905 3906
   been replaced with whitespace), then use these two in-core copies of the
   file to make a new edited in-core copy of the file.  Finally, rename the
   original file (as a way of saving it), and then write the edited version
   of the file from core to a disk file of the same name as the original.

   Note that the trick of making a copy of the original sans comments &
3907
   preprocessing directives make the editing a whole lot easier.  */
3908

Richard Stallman committed
3909
static void
3910
edit_file (const hash_table_entry *hp)
Richard Stallman committed
3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927
{
  struct stat stat_buf;
  const file_info *file_p = hp->fip;
  char *new_orig_text_base;
  char *new_orig_text_limit;
  char *new_clean_text_base;
  char *new_clean_text_limit;
  size_t orig_size;
  size_t repl_size;
  int first_definition_in_file;

  /* If we are not supposed to be converting this file, or if there is
     nothing in there which needs converting, just skip this file.  */

  if (!needs_to_be_converted (file_p))
    return;

3928
  convert_filename = file_p->hash_entry->symbol;
Richard Stallman committed
3929 3930 3931 3932

  /* Convert a file if it is in a directory where we want conversion
     and the file is not excluded.  */

3933 3934
  if (!directory_specified_p (convert_filename)
      || file_excluded_p (convert_filename))
Richard Stallman committed
3935 3936 3937
    {
      if (!quiet_flag
#ifdef UNPROTOIZE
3938 3939 3940 3941 3942
	  /* Don't even mention "system" include files unless we are
	     protoizing.  If we are protoizing, we mention these as a
	     gentle way of prodding the user to convert his "system"
	     include files to prototype format.  */
	  && !in_system_include_dir (convert_filename)
3943
#endif /* defined (UNPROTOIZE) */
3944
	  )
3945
	notice ("%s: '%s' not converted\n",
3946
		pname, shortpath (NULL, convert_filename));
Richard Stallman committed
3947 3948 3949 3950 3951 3952
      return;
    }

  /* Let the user know what we are up to.  */

  if (nochange_flag)
3953
    notice ("%s: would convert file '%s'\n",
3954
	    pname, shortpath (NULL, convert_filename));
Richard Stallman committed
3955
  else
3956
    notice ("%s: converting file '%s'\n",
3957
	    pname, shortpath (NULL, convert_filename));
3958
  fflush (stderr);
Richard Stallman committed
3959 3960 3961

  /* Find out the size (in bytes) of the original file.  */

3962
  /* The cast avoids an erroneous warning on AIX.  */
3963
  if (stat (convert_filename, &stat_buf) == -1)
Richard Stallman committed
3964
    {
3965
      int errno_val = errno;
3966
      notice ("%s: can't get status for file '%s': %s\n",
3967 3968
	      pname, shortpath (NULL, convert_filename),
	      xstrerror (errno_val));
Richard Stallman committed
3969 3970 3971 3972 3973 3974
      return;
    }
  orig_size = stat_buf.st_size;

  /* Allocate a buffer to hold the original text.  */

3975
  orig_text_base = new_orig_text_base = xmalloc (orig_size + 2);
Richard Stallman committed
3976 3977 3978 3979
  orig_text_limit = new_orig_text_limit = new_orig_text_base + orig_size;

  /* Allocate a buffer to hold the cleaned-up version of the original text.  */

3980
  clean_text_base = new_clean_text_base = xmalloc (orig_size + 2);
Richard Stallman committed
3981 3982 3983 3984 3985 3986 3987 3988 3989
  clean_text_limit = new_clean_text_limit = new_clean_text_base + orig_size;
  clean_read_ptr = clean_text_base - 1;

  /* Allocate a buffer that will hopefully be large enough to hold the entire
     converted output text.  As an initial guess for the maximum size of the
     output buffer, use 125% of the size of the original + some extra.  This
     buffer can be expanded later as needed.  */

  repl_size = orig_size + (orig_size >> 2) + 4096;
3990
  repl_text_base = xmalloc (repl_size + 2);
Richard Stallman committed
3991 3992 3993 3994 3995
  repl_text_limit = repl_text_base + repl_size - 1;
  repl_write_ptr = repl_text_base - 1;

  {
    int input_file;
3996
    int fd_flags;
Richard Stallman committed
3997 3998 3999

    /* Open the file to be converted in READ ONLY mode.  */

4000 4001
    fd_flags = O_RDONLY;
#ifdef O_BINARY
4002
    /* Use binary mode to avoid having to deal with different EOL characters.  */
4003 4004 4005
    fd_flags |= O_BINARY;
#endif
    if ((input_file = open (convert_filename, fd_flags, 0444)) == -1)
Richard Stallman committed
4006
      {
4007
	int errno_val = errno;
4008
	notice ("%s: can't open file '%s' for reading: %s\n",
4009 4010
		pname, shortpath (NULL, convert_filename),
		xstrerror (errno_val));
4011
	return;
Richard Stallman committed
4012 4013 4014 4015 4016 4017
      }

    /* Read the entire original source text file into the original text buffer
       in one swell fwoop.  Then figure out where the end of the text is and
       make sure that it ends with a newline followed by a null.  */

Kaveh R. Ghazi committed
4018 4019
    if (safe_read (input_file, new_orig_text_base, orig_size) !=
	(int) orig_size)
Richard Stallman committed
4020
      {
4021
	int errno_val = errno;
4022
	close (input_file);
4023
	notice ("\n%s: error reading input file '%s': %s\n",
4024 4025
		pname, shortpath (NULL, convert_filename),
		xstrerror (errno_val));
4026
	return;
Richard Stallman committed
4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047
      }

    close (input_file);
  }

  if (orig_size == 0 || orig_text_limit[-1] != '\n')
    {
      *new_orig_text_limit++ = '\n';
      orig_text_limit++;
    }

  /* Create the cleaned up copy of the original text.  */

  memcpy (new_clean_text_base, orig_text_base,
	  (size_t) (orig_text_limit - orig_text_base));
  do_cleaning (new_clean_text_base, new_clean_text_limit);

#if 0
  {
    int clean_file;
    size_t clean_size = orig_text_limit - orig_text_base;
4048
    char *const clean_filename = alloca (strlen (convert_filename) + 6 + 1);
Richard Stallman committed
4049 4050

    /* Open (and create) the clean file.  */
4051

4052 4053 4054
    strcpy (clean_filename, convert_filename);
    strcat (clean_filename, ".clean");
    if ((clean_file = creat (clean_filename, 0666)) == -1)
Richard Stallman committed
4055
      {
4056
	int errno_val = errno;
4057
	notice ("%s: can't create/open clean file '%s': %s\n",
4058 4059
		pname, shortpath (NULL, clean_filename),
		xstrerror (errno_val));
4060
	return;
Richard Stallman committed
4061
      }
4062

Richard Stallman committed
4063
    /* Write the clean file.  */
4064

4065
    safe_write (clean_file, new_clean_text_base, clean_size, clean_filename);
4066

Richard Stallman committed
4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091
    close (clean_file);
  }
#endif /* 0 */

  /* Do a simplified scan of the input looking for things that were not
     mentioned in the aux info files because of the fact that they were
     in a region of the source which was preprocessed-out (via #if or
     via #ifdef).  */

  scan_for_missed_items (file_p);

  /* Setup to do line-oriented forward seeking in the clean text buffer.  */

  last_known_line_number = 1;
  last_known_line_start = clean_text_base;

  /* Now get down to business and make all of the necessary edits.  */

  {
    const def_dec_info *def_dec_p;

    first_definition_in_file = 1;
    def_dec_p = file_p->defs_decs;
    for (; def_dec_p; def_dec_p = def_dec_p->next_in_file)
      {
4092 4093 4094 4095 4096 4097
	const char *clean_text_p = seek_to_line (def_dec_p->line);

	/* clean_text_p now points to the first character of the line which
	   contains the `terminator' for the declaration or definition that
	   we are about to process.  */

Richard Stallman committed
4098 4099
#ifndef UNPROTOIZE

4100 4101 4102 4103 4104
	if (global_flag && def_dec_p->is_func_def && first_definition_in_file)
	  {
	    add_global_decls (def_dec_p->file, clean_text_p);
	    first_definition_in_file = 0;
	  }
Richard Stallman committed
4105

4106 4107 4108 4109 4110 4111 4112
	/* Don't edit this item if it is already in prototype format or if it
	   is a function declaration and we have found no corresponding
	   definition.  */

	if (def_dec_p->prototyped
	    || (!def_dec_p->is_func_def && !def_dec_p->definition))
	  continue;
Richard Stallman committed
4113

4114
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
4115

4116 4117 4118
	if (def_dec_p->is_func_def)
	  edit_fn_definition (def_dec_p, clean_text_p);
	else
Richard Stallman committed
4119
#ifndef UNPROTOIZE
4120 4121 4122
	if (def_dec_p->is_implicit)
	  add_local_decl (def_dec_p, clean_text_p);
	else
4123
#endif /* !defined (UNPROTOIZE) */
4124
	  edit_fn_declaration (def_dec_p, clean_text_p);
Richard Stallman committed
4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146
      }
  }

  /* Finalize things.  Output the last trailing part of the original text.  */

  output_up_to (clean_text_limit - 1);

  /* If this is just a test run, stop now and just deallocate the buffers.  */

  if (nochange_flag)
    {
      free (new_orig_text_base);
      free (new_clean_text_base);
      free (repl_text_base);
      return;
    }

  /* Change the name of the original input file.  This is just a quick way of
     saving the original file.  */

  if (!nosave_flag)
    {
4147
      char *new_filename
4148
	= xmalloc (strlen (convert_filename) + strlen (save_suffix) + 2);
4149

4150
      strcpy (new_filename, convert_filename);
4151 4152
#ifdef __MSDOS__
      /* MSDOS filenames are restricted to 8.3 format, so we save `foo.c'
4153
	 as `foo.<save_suffix>'.  */
4154 4155
      new_filename[(strlen (convert_filename) - 1] = '\0';
#endif
4156
      strcat (new_filename, save_suffix);
4157 4158 4159 4160 4161

      /* Don't overwrite existing file.  */
      if (access (new_filename, F_OK) == 0)
	{
	  if (!quiet_flag)
4162
	    notice ("%s: warning: file '%s' already saved in '%s'\n",
4163 4164 4165 4166 4167
		    pname,
		    shortpath (NULL, convert_filename),
		    shortpath (NULL, new_filename));
	}
      else if (rename (convert_filename, new_filename) == -1)
4168
	{
4169
	  int errno_val = errno;
4170
	  notice ("%s: can't link file '%s' to '%s': %s\n",
4171 4172 4173 4174 4175
		  pname,
		  shortpath (NULL, convert_filename),
		  shortpath (NULL, new_filename),
		  xstrerror (errno_val));
	  return;
4176
	}
Richard Stallman committed
4177 4178
    }

4179
  if (unlink (convert_filename) == -1)
Richard Stallman committed
4180
    {
4181
      int errno_val = errno;
4182 4183
      /* The file may have already been renamed.  */
      if (errno_val != ENOENT)
4184
	{
4185
	  notice ("%s: can't delete file '%s': %s\n",
4186 4187 4188 4189
		  pname, shortpath (NULL, convert_filename),
		  xstrerror (errno_val));
	  return;
	}
Richard Stallman committed
4190 4191 4192 4193 4194 4195
    }

  {
    int output_file;

    /* Open (and create) the output file.  */
4196

4197
    if ((output_file = creat (convert_filename, 0666)) == -1)
Richard Stallman committed
4198
      {
4199
	int errno_val = errno;
4200
	notice ("%s: can't create/open output file '%s': %s\n",
4201 4202
		pname, shortpath (NULL, convert_filename),
		xstrerror (errno_val));
4203
	return;
Richard Stallman committed
4204
      }
4205 4206 4207 4208
#ifdef O_BINARY
    /* Use binary mode to avoid changing the existing EOL character.  */
    setmode (output_file, O_BINARY);
#endif
4209

Richard Stallman committed
4210
    /* Write the output file.  */
4211

Richard Stallman committed
4212 4213
    {
      unsigned int out_size = (repl_write_ptr + 1) - repl_text_base;
4214

4215
      safe_write (output_file, repl_text_base, out_size, convert_filename);
Richard Stallman committed
4216
    }
4217

Richard Stallman committed
4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228
    close (output_file);
  }

  /* Deallocate the conversion buffers.  */

  free (new_orig_text_base);
  free (new_clean_text_base);
  free (repl_text_base);

  /* Change the mode of the output file to match the original file.  */

4229
  /* The cast avoids an erroneous warning on AIX.  */
4230
  if (chmod (convert_filename, stat_buf.st_mode) == -1)
4231 4232
    {
      int errno_val = errno;
4233
      notice ("%s: can't change mode of file '%s': %s\n",
4234 4235
	      pname, shortpath (NULL, convert_filename),
	      xstrerror (errno_val));
4236
    }
Richard Stallman committed
4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248

  /* Note:  We would try to change the owner and group of the output file
     to match those of the input file here, except that may not be a good
     thing to do because it might be misleading.  Also, it might not even
     be possible to do that (on BSD systems with quotas for instance).  */
}

/* Do all of the individual steps needed to do the protoization (or
   unprotoization) of the files referenced in the aux_info files given
   in the command line.  */

static void
4249
do_processing (void)
Richard Stallman committed
4250 4251
{
  const char * const *base_pp;
4252 4253
  const char * const * const end_pps
    = &base_source_filenames[n_base_source_files];
Richard Stallman committed
4254 4255 4256

#ifndef UNPROTOIZE
  int syscalls_len;
4257
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
4258 4259 4260 4261 4262 4263

  /* One-by-one, check (and create if necessary), open, and read all of the
     stuff in each aux_info file.  After reading each aux_info file, the
     aux_info_file just read will be automatically deleted unless the
     keep_flag is set.  */

4264
  for (base_pp = base_source_filenames; base_pp < end_pps; base_pp++)
Richard Stallman committed
4265 4266 4267 4268 4269 4270 4271 4272 4273
    process_aux_info_file (*base_pp, keep_flag, 0);

#ifndef UNPROTOIZE

  /* Also open and read the special SYSCALLS.c aux_info file which gives us
     the prototypes for all of the standard system-supplied functions.  */

  if (nondefault_syscalls_dir)
    {
4274
      syscalls_absolute_filename
4275 4276
	= xmalloc (strlen (nondefault_syscalls_dir) + 1
		   + sizeof (syscalls_filename));
4277
      strcpy (syscalls_absolute_filename, nondefault_syscalls_dir);
Richard Stallman committed
4278 4279 4280
    }
  else
    {
4281
      GET_ENVIRONMENT (default_syscalls_dir, "GCC_EXEC_PREFIX");
4282 4283 4284 4285
      if (!default_syscalls_dir)
	{
	  default_syscalls_dir = standard_exec_prefix;
	}
4286
      syscalls_absolute_filename
4287 4288 4289 4290
	= xmalloc (strlen (default_syscalls_dir) + 0
		   + strlen (target_machine) + 1
		   + strlen (target_version) + 1
		   + sizeof (syscalls_filename));
4291
      strcpy (syscalls_absolute_filename, default_syscalls_dir);
4292 4293 4294 4295
      strcat (syscalls_absolute_filename, target_machine);
      strcat (syscalls_absolute_filename, "/");
      strcat (syscalls_absolute_filename, target_version);
      strcat (syscalls_absolute_filename, "/");
Richard Stallman committed
4296 4297
    }

4298
  syscalls_len = strlen (syscalls_absolute_filename);
4299
  if (! IS_DIR_SEPARATOR (*(syscalls_absolute_filename + syscalls_len - 1)))
Richard Stallman committed
4300
    {
4301
      *(syscalls_absolute_filename + syscalls_len++) = DIR_SEPARATOR;
4302
      *(syscalls_absolute_filename + syscalls_len) = '\0';
Richard Stallman committed
4303
    }
4304
  strcat (syscalls_absolute_filename, syscalls_filename);
4305

Richard Stallman committed
4306 4307 4308
  /* Call process_aux_info_file in such a way that it does not try to
     delete the SYSCALLS aux_info file.  */

4309
  process_aux_info_file (syscalls_absolute_filename, 1, 1);
Richard Stallman committed
4310

4311
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
4312 4313

  /* When we first read in all of the information from the aux_info files
4314
     we saved in it descending line number order, because that was likely to
Richard Stallman committed
4315 4316 4317 4318 4319
     be faster.  Now however, we want the chains of def & dec records to
     appear in ascending line number order as we get further away from the
     file_info record that they hang from.  The following line causes all of
     these lists to be rearranged into ascending line number order.  */

4320
  visit_each_hash_node (filename_primary, reverse_def_dec_list);
Richard Stallman committed
4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332

#ifndef UNPROTOIZE

  /* Now do the "real" work.  The following line causes each declaration record
     to be "visited".  For each of these nodes, an attempt is made to match
     up the function declaration with a corresponding function definition,
     which should have a full prototype-format formals list with it.  Once
     these match-ups are made, the conversion of the function declarations
     to prototype format can be made.  */

  visit_each_hash_node (function_name_primary, connect_defs_and_decs);

4333
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
4334 4335 4336

  /* Now convert each file that can be converted (and needs to be).  */

4337
  visit_each_hash_node (filename_primary, edit_file);
Richard Stallman committed
4338 4339 4340 4341 4342 4343 4344

#ifndef UNPROTOIZE

  /* If we are working in cplusplus mode, try to rename all .c files to .C
     files.  Don't panic if some of the renames don't work.  */

  if (cplusplus_flag && !nochange_flag)
4345
    visit_each_hash_node (filename_primary, rename_c_file);
Richard Stallman committed
4346

4347
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
4348 4349
}

4350
static const struct option longopts[] =
Richard Stallman committed
4351 4352
{
  {"version", 0, 0, 'V'},
4353
  {"file_name", 0, 0, 'p'},
Richard Stallman committed
4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
  {"quiet", 0, 0, 'q'},
  {"silent", 0, 0, 'q'},
  {"force", 0, 0, 'f'},
  {"keep", 0, 0, 'k'},
  {"nosave", 0, 0, 'N'},
  {"nochange", 0, 0, 'n'},
  {"compiler-options", 1, 0, 'c'},
  {"exclude", 1, 0, 'x'},
  {"directory", 1, 0, 'd'},
#ifdef UNPROTOIZE
  {"indent", 1, 0, 'i'},
#else
  {"local", 0, 0, 'l'},
  {"global", 0, 0, 'g'},
  {"c++", 0, 0, 'C'},
  {"syscalls-dir", 1, 0, 'B'},
#endif
  {0, 0, 0, 0}
};

4374
extern int main (int, char **const);
4375

Richard Stallman committed
4376
int
4377
main (int argc, char **const argv)
Richard Stallman committed
4378 4379 4380
{
  int longind;
  int c;
4381
  const char *params = "";
Richard Stallman committed
4382

4383 4384 4385 4386 4387 4388 4389 4390 4391 4392
  pname = strrchr (argv[0], DIR_SEPARATOR);
#ifdef DIR_SEPARATOR_2
  {
    char *slash;

    slash = strrchr (pname ? pname : argv[0], DIR_SEPARATOR_2);
    if (slash)
      pname = slash;
  }
#endif
Richard Stallman committed
4393 4394
  pname = pname ? pname+1 : argv[0];

4395 4396 4397 4398 4399 4400
#ifdef SIGCHLD
  /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
     receive the signal.  A different setting is inheritable */
  signal (SIGCHLD, SIG_DFL);
#endif

4401
  gcc_init_libintl ();
4402

4403 4404
  cwd_buffer = getpwd ();
  if (!cwd_buffer)
Richard Stallman committed
4405
    {
4406 4407
      notice ("%s: cannot get working directory: %s\n",
	      pname, xstrerror(errno));
4408
      return (FATAL_EXIT_CODE);
Richard Stallman committed
4409 4410 4411 4412 4413 4414 4415
    }

  /* By default, convert the files in the current directory.  */
  directory_list = string_list_cons (cwd_buffer, NULL);

  while ((c = getopt_long (argc, argv,
#ifdef UNPROTOIZE
4416
			   "c:d:i:knNp:qvVx:",
Richard Stallman committed
4417
#else
4418
			   "B:c:Cd:gklnNp:qvVx:",
Richard Stallman committed
4419 4420 4421
#endif
			   longopts, &longind)) != EOF)
    {
Mike Stump committed
4422
      if (c == 0)		/* Long option.  */
Richard Stallman committed
4423 4424 4425
	c = longopts[longind].val;
      switch (c)
	{
4426
	case 'p':
4427
	  compiler_file_name = optarg;
4428
	  break;
Richard Stallman committed
4429 4430 4431 4432 4433 4434 4435
	case 'd':
	  directory_list
	    = string_list_cons (abspath (NULL, optarg), directory_list);
	  break;
	case 'x':
	  exclude_list = string_list_cons (optarg, exclude_list);
	  break;
4436

4437
	case 'v':
Richard Stallman committed
4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459
	case 'V':
	  version_flag = 1;
	  break;
	case 'q':
	  quiet_flag = 1;
	  break;
#if 0
	case 'f':
	  force_flag = 1;
	  break;
#endif
	case 'n':
	  nochange_flag = 1;
	  keep_flag = 1;
	  break;
	case 'N':
	  nosave_flag = 1;
	  break;
	case 'k':
	  keep_flag = 1;
	  break;
	case 'c':
4460
	  params = optarg;
Richard Stallman committed
4461 4462 4463 4464 4465
	  break;
#ifdef UNPROTOIZE
	case 'i':
	  indent_string = optarg;
	  break;
4466
#else				/* !defined (UNPROTOIZE) */
Richard Stallman committed
4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478
	case 'l':
	  local_flag = 1;
	  break;
	case 'g':
	  global_flag = 1;
	  break;
	case 'C':
	  cplusplus_flag = 1;
	  break;
	case 'B':
	  nondefault_syscalls_dir = optarg;
	  break;
4479
#endif				/* !defined (UNPROTOIZE) */
Richard Stallman committed
4480 4481 4482 4483
	default:
	  usage ();
	}
    }
4484

4485 4486 4487
  /* Set up compile_params based on -p and -c options.  */
  munge_compile_params (params);

4488
  n_base_source_files = argc - optind;
Richard Stallman committed
4489

4490
  /* Now actually make a list of the base source filenames.  */
Richard Stallman committed
4491

4492
  base_source_filenames
4493
    = xmalloc ((n_base_source_files + 1) * sizeof (char *));
4494
  n_base_source_files = 0;
Richard Stallman committed
4495 4496 4497 4498 4499 4500
  for (; optind < argc; optind++)
    {
      const char *path = abspath (NULL, argv[optind]);
      int len = strlen (path);

      if (path[len-1] == 'c' && path[len-2] == '.')
4501
	base_source_filenames[n_base_source_files++] = path;
Richard Stallman committed
4502 4503
      else
	{
4504 4505
	  notice ("%s: input file names must have .c suffixes: %s\n",
		  pname, shortpath (NULL, path));
Richard Stallman committed
4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516
	  errors++;
	}
    }

#ifndef UNPROTOIZE
  /* We are only interested in the very first identifier token in the
     definition of `va_list', so if there is more junk after that first
     identifier token, delete it from the `varargs_style_indicator'.  */
  {
    const char *cp;

4517
    for (cp = varargs_style_indicator; ISIDNUM (*cp); cp++)
Richard Stallman committed
4518 4519 4520 4521 4522
      continue;
    if (*cp != 0)
      varargs_style_indicator = savestring (varargs_style_indicator,
					    cp - varargs_style_indicator);
  }
4523
#endif /* !defined (UNPROTOIZE) */
Richard Stallman committed
4524 4525 4526 4527 4528 4529

  if (errors)
    usage ();
  else
    {
      if (version_flag)
4530
	fprintf (stderr, "%s: %s\n", pname, version_string);
Richard Stallman committed
4531 4532
      do_processing ();
    }
4533

4534
  return (errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
Richard Stallman committed
4535
}