home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / cccp.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  242KB  |  8,989 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2. Copyright (C) 1986, 1987, 1989, 1992 Free Software Foundation, Inc.
  3.                     Written by Paul Rubin, June 1986
  4.             Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  In other words, you are welcome to use, share and improve this program.
  21.  You are forbidden to forbid anyone else to use, share and improve
  22.  what you give them.   Help stamp out software-hoarding!  */
  23.  
  24. typedef unsigned char U_CHAR;
  25.  
  26. #ifdef EMACS
  27. #define NO_SHORTNAMES
  28. #include "../src/config.h"
  29. #ifdef open
  30. #undef open
  31. #undef read
  32. #undef write
  33. #endif /* open */
  34. #endif /* EMACS */
  35.  
  36. /* The macro EMACS is defined when cpp is distributed as part of Emacs,
  37.    for the sake of machines with limited C compilers.  */
  38. #ifndef EMACS
  39. #include "config.h"
  40. #endif /* not EMACS */
  41.  
  42. #ifdef amigados
  43. /* Since cpp uses alloca to store all its read files, this is quite deadly
  44.    on a system with non-automatic stackgrowth like amigados, so we better
  45.    turn it off now.  Normally alloca is #defined to __builtin_alloca, so
  46.    undefining it causes an external alloca to be used.
  47.  
  48.    Note that it's not wise to generally inhibit __builtin_alloca, since
  49.    using the generic emulator entitels a serious (!) speed penalty, and
  50.    it's bad enough that we have to live with it in cccp, don't make cc1
  51.    unbearably slow as well... */
  52.  
  53. #undef alloca
  54.  
  55. static int amigados_abs_filename ();
  56. #endif
  57.  
  58. #ifndef STANDARD_INCLUDE_DIR
  59. #define STANDARD_INCLUDE_DIR "/usr/include"
  60. #endif
  61.  
  62. #ifndef LOCAL_INCLUDE_DIR
  63. #define LOCAL_INCLUDE_DIR "/usr/local/include"
  64. #endif
  65.  
  66. #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
  67. #ifdef __STDC__
  68. #define PTR_INT_TYPE ptrdiff_t
  69. #else
  70. #define PTR_INT_TYPE long
  71. #endif
  72. #endif /* 0 */
  73.  
  74. #include "pcp.h"
  75.  
  76. #ifndef STDC_VALUE
  77. #define STDC_VALUE 1
  78. #endif
  79.  
  80. /* By default, colon separates directories in a path.  */
  81. #ifndef PATH_SEPARATOR
  82. #define PATH_SEPARATOR ':'
  83. #endif
  84.  
  85. /* In case config.h defines these.  */
  86. #undef bcopy
  87. #undef bzero
  88. #undef bcmp
  89.  
  90. #include <sys/types.h>
  91. #include <sys/stat.h>
  92. #include <ctype.h>
  93. #include <stdio.h>
  94.  
  95. #ifndef VMS
  96. #ifndef USG
  97. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  98. #include <sys/resource.h>
  99. #else
  100. #include <time.h>
  101. #include <fcntl.h>
  102. #endif /* USG */
  103. #endif /* not VMS */
  104.  
  105. /* VMS-specific definitions */
  106. #ifdef VMS
  107. #include <time.h>
  108. #include <errno.h>        /* This defines "errno" properly */
  109. #include <perror.h>        /* This defines sys_errlist/sys_nerr properly */
  110. #include <descrip.h>
  111. #define O_RDONLY    0    /* Open arg for Read/Only  */
  112. #define O_WRONLY    1    /* Open arg for Write/Only */
  113. #define read(fd,buf,size)    VMS_read(fd,buf,size)
  114. #define write(fd,buf,size)    VMS_write(fd,buf,size)
  115. #define open(fname,mode,prot)    VMS_open(fname,mode,prot)
  116. #define fopen(fname,mode)    VMS_fopen(fname,mode)
  117. #define freopen(fname,mode,ofile) VMS_freopen(fname,mode,ofile)
  118. #define strncat(dst,src,cnt) VMS_strncat(dst,src,cnt)
  119. static char * VMS_strncat ();
  120. static int VMS_read ();
  121. static int VMS_write ();
  122. static int VMS_open ();
  123. static FILE * VMS_fopen ();
  124. static FILE * VMS_freopen ();
  125. static void hack_vms_include_specification ();
  126. typedef struct { unsigned :16, :16, :16; } vms_ino_t;
  127. #define ino_t vms_ino_t
  128. #define INCLUDE_LEN_FUDGE 10    /* leave room for VMS syntax conversion */
  129. #ifdef __GNUC__
  130. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  131. #endif /* __GNUC__ */
  132. #endif /* VMS */
  133.   
  134. extern char *index ();
  135. extern char *rindex ();
  136.  
  137. #ifndef O_RDONLY
  138. #define O_RDONLY 0
  139. #endif
  140.  
  141. #undef MIN
  142. #undef MAX
  143. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  144. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  145.  
  146. /* Find the largest host integer type and set its size and type.  */
  147.  
  148. #ifndef HOST_BITS_PER_WIDE_INT
  149.  
  150. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  151. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  152. #define HOST_WIDE_INT long
  153. #else
  154. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  155. #define HOST_WIDE_INT int
  156. #endif
  157.  
  158. #endif
  159.  
  160. #ifndef S_ISREG
  161. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  162. #endif
  163.  
  164. /* Define a generic NULL if one hasn't already been defined.  */
  165.  
  166. #ifndef NULL
  167. #define NULL 0
  168. #endif
  169.  
  170. #ifndef GENERIC_PTR
  171. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  172. #define GENERIC_PTR void *
  173. #else
  174. #define GENERIC_PTR char *
  175. #endif
  176. #endif
  177.  
  178. #ifndef NULL_PTR
  179. #define NULL_PTR ((GENERIC_PTR)0)
  180. #endif
  181.  
  182. #ifndef INCLUDE_LEN_FUDGE
  183. #define INCLUDE_LEN_FUDGE 0
  184. #endif
  185.  
  186. /* Forward declarations.  */
  187.  
  188. char *xmalloc ();
  189. void error ();
  190. void warning ();
  191.  
  192. /* External declarations.  */
  193.  
  194. extern char *getenv ();
  195. extern FILE *fdopen ();
  196. extern char *version_string;
  197. extern struct tm *localtime ();
  198. extern int sys_nerr;
  199.  
  200. #ifndef HAVE_STRERROR
  201. extern char *sys_errlist[];
  202. #define strerror(err) sys_errlist[err]
  203. #endif
  204.  
  205. #ifndef errno
  206. extern int errno;
  207. #endif
  208.  
  209. /* Forward declarations.  */
  210.  
  211. struct directive;
  212. struct file_buf;
  213. struct arglist;
  214. struct argdata;
  215.  
  216. #if defined(USG) || defined(VMS)
  217. #ifndef BSTRING
  218. void bcopy ();
  219. void bzero ();
  220. int bcmp ();
  221. #endif
  222. #endif
  223.  
  224. /* These functions are declared to return int instead of void since they
  225.    are going to be placed in a table and some old compilers have trouble with
  226.    pointers to functions returning void.  */
  227.  
  228. static int do_define ();
  229. static int do_line ();
  230. static int do_include ();
  231. static int do_undef ();
  232. static int do_error ();
  233. static int do_pragma ();
  234. static int do_ident ();
  235. static int do_if ();
  236. static int do_xifdef ();
  237. static int do_else ();
  238. static int do_elif ();
  239. static int do_endif ();
  240. static int do_sccs ();
  241. static int do_once ();
  242. static int do_assert ();
  243. static int do_unassert ();
  244. static int do_warning ();
  245.  
  246. static void add_import ();
  247. static void append_include_chain ();
  248. static void deps_output ();
  249. static void make_undef ();
  250. static void make_definition ();
  251. static void make_assertion ();
  252. static void path_include ();
  253. static void initialize_builtins ();
  254. static void initialize_char_syntax ();
  255. static void dump_arg_n ();
  256. static void dump_defn_1 ();
  257. static void delete_macro ();
  258. static void trigraph_pcp ();
  259. static void rescan ();
  260. static void finclude ();
  261. static void validate_else ();
  262. static int comp_def_part ();
  263. static void error_from_errno ();
  264. static void error_with_line ();
  265. void pedwarn ();
  266. static void pedwarn_with_file_and_line ();
  267. static void fatal ();
  268. void fancy_abort ();
  269. static void pfatal_with_name ();
  270. static void perror_with_name ();
  271. static void print_containing_files ();
  272. static int lookup_import ();
  273. static int redundant_include_p ();
  274. static is_system_include ();
  275. static int check_preconditions ();
  276. static void pcfinclude ();
  277. static void pcstring_used ();
  278. static void write_output ();
  279. static int check_macro_name ();
  280. static int compare_defs ();
  281. static int compare_token_lists ();
  282. static int eval_if_expression ();
  283. static int discard_comments ();
  284. static int delete_newlines ();
  285. static int line_for_error ();
  286. static int hashf ();
  287. static int file_size_and_mode ();
  288.  
  289. static struct arglist *read_token_list ();
  290. static void free_token_list ();
  291.  
  292. static struct hashnode *install ();
  293. struct hashnode *lookup ();
  294.  
  295. static struct assertion_hashnode *assertion_install ();
  296. static struct assertion_hashnode *assertion_lookup ();
  297.  
  298. static char *xrealloc ();
  299. static char *xcalloc ();
  300. static char *savestring ();
  301.  
  302. static void delete_assertion ();
  303. static void macroexpand ();
  304. static void dump_all_macros ();
  305. static void conditional_skip ();
  306. static void skip_if_group ();
  307. static void output_line_command ();
  308.  
  309. /* Last arg to output_line_command.  */
  310. enum file_change_code {same_file, enter_file, leave_file};
  311.  
  312. static int grow_outbuf ();
  313. static int handle_directive ();
  314. static void memory_full ();
  315.  
  316. static U_CHAR *macarg1 ();
  317. static char *macarg ();
  318.  
  319. static U_CHAR *skip_to_end_of_comment ();
  320. static U_CHAR *skip_quoted_string ();
  321. static U_CHAR *skip_paren_group ();
  322.  
  323. static char *check_precompiled ();
  324. /* static struct macrodef create_definition ();    [moved below] */
  325. static void dump_single_macro ();
  326.  
  327. #ifndef FAILURE_EXIT_CODE
  328. #define FAILURE_EXIT_CODE 33    /* gnu cc command understands this */
  329. #endif
  330.  
  331. #ifndef SUCCESS_EXIT_CODE
  332. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  333. #endif
  334.  
  335. /* Name under which this program was invoked.  */
  336.  
  337. static char *progname;
  338.  
  339. /* Nonzero means use extra default include directories for C++.  */
  340.  
  341. static int cplusplus;
  342.  
  343. /* Nonzero means handle cplusplus style comments */
  344.  
  345. static int cplusplus_comments;
  346.  
  347. /* Nonzero means handle #import, for objective C.  */
  348.  
  349. static int objc;
  350.  
  351. /* Nonzero means this is an assembly file, and allow
  352.    unknown directives, which could be comments.  */
  353.  
  354. static int lang_asm;
  355.  
  356. /* Current maximum length of directory names in the search path
  357.    for include files.  (Altered as we get more of them.)  */
  358.  
  359. static int max_include_len;
  360.  
  361. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  362.  
  363. static int lint = 0;
  364.  
  365. /* Nonzero means copy comments into the output file.  */
  366.  
  367. static int put_out_comments = 0;
  368.  
  369. /* Nonzero means don't process the ANSI trigraph sequences.  */
  370.  
  371. static int no_trigraphs = 0;
  372.  
  373. /* Nonzero means print the names of included files rather than
  374.    the preprocessed output.  1 means just the #include "...",
  375.    2 means #include <...> as well.  */
  376.  
  377. static int print_deps = 0;
  378.  
  379. /* Nonzero means print names of header files (-H).  */
  380.  
  381. static int print_include_names = 0;
  382.  
  383. /* Nonzero means don't output line number information.  */
  384.  
  385. static int no_line_commands;
  386.  
  387. /* dump_only means inhibit output of the preprocessed text
  388.              and instead output the definitions of all user-defined
  389.              macros in a form suitable for use as input to cccp.
  390.    dump_names means pass #define and the macro name through to output.
  391.    dump_definitions means pass the whole definition (plus #define) through
  392. */
  393.  
  394. static enum {dump_none, dump_only, dump_names, dump_definitions}
  395.      dump_macros = dump_none;
  396.  
  397. /* Nonzero means pass all #define and #undef directives which we actually
  398.    process through to the output stream.  This feature is used primarily
  399.    to allow cc1 to record the #defines and #undefs for the sake of
  400.    debuggers which understand about preprocessor macros, but it may
  401.    also be useful with -E to figure out how symbols are defined, and
  402.    where they are defined.  */
  403. static int debug_output = 0;
  404.  
  405. /* Nonzero indicates special processing used by the pcp program.  The
  406.    special effects of this mode are: 
  407.      
  408.      Inhibit all macro expansion, except those inside #if directives.
  409.  
  410.      Process #define directives normally, and output their contents 
  411.      to the output file.
  412.  
  413.      Output preconditions to pcp_outfile indicating all the relevant
  414.      preconditions for use of this file in a later cpp run.
  415. */
  416. static FILE *pcp_outfile;
  417.  
  418. /* Nonzero means we are inside an IF during a -pcp run.  In this mode
  419.    macro expansion is done, and preconditions are output for all macro
  420.    uses requiring them. */
  421. static int pcp_inside_if;
  422.  
  423. /* Nonzero means never to include precompiled files. */
  424. static int no_precomp;
  425.  
  426. /* Nonzero means give all the error messages the ANSI standard requires.  */
  427.  
  428. int pedantic;
  429.  
  430. /* Nonzero means try to make failure to fit ANSI C an error.  */
  431.  
  432. static int pedantic_errors;
  433.  
  434. /* Nonzero means don't print warning messages.  -w.  */
  435.  
  436. static int inhibit_warnings = 0;
  437.  
  438. /* Nonzero means warn if slash-star appears in a comment.  */
  439.  
  440. static int warn_comments;
  441.  
  442. /* Nonzero means warn if a macro argument is (or would be)
  443.    stringified with -traditional.  */
  444.  
  445. static int warn_stringify;
  446.  
  447. /* Nonzero means warn if there are any trigraphs.  */
  448.  
  449. static int warn_trigraphs;
  450.  
  451. /* Nonzero means warn if #import is used.  */
  452.  
  453. static int warn_import = 1;
  454.  
  455. /* Nonzero means turn warnings into errors.  */
  456.  
  457. static int warnings_are_errors;
  458.  
  459. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  460.  
  461. int traditional;
  462.  
  463. /* Nonzero causes output not to be done,
  464.    but directives such as #define that have side effects
  465.    are still obeyed.  */
  466.  
  467. static int no_output;
  468.  
  469. /* Nonzero means that we have finished processing the command line options.
  470.    This flag is used to decide whether or not to issue certain errors
  471.    and/or warnings.  */
  472.  
  473. static int done_initializing = 0;
  474.  
  475. /* I/O buffer structure.
  476.    The `fname' field is nonzero for source files and #include files
  477.    and for the dummy text used for -D and -U.
  478.    It is zero for rescanning results of macro expansion
  479.    and for expanding macro arguments.  */
  480. #define INPUT_STACK_MAX 200
  481. static struct file_buf {
  482.   char *fname;
  483.   /* Filename specified with #line command.  */
  484.   char *nominal_fname;
  485.   /* Record where in the search path this file was found.
  486.      For #include_next.  */
  487.   struct file_name_list *dir;
  488.   int lineno;
  489.   int length;
  490.   U_CHAR *buf;
  491.   U_CHAR *bufp;
  492.   /* Macro that this level is the expansion of.
  493.      Included so that we can reenable the macro
  494.      at the end of this level.  */
  495.   struct hashnode *macro;
  496.   /* Value of if_stack at start of this file.
  497.      Used to prohibit unmatched #endif (etc) in an include file.  */
  498.   struct if_stack *if_stack;
  499.   /* Object to be freed at end of input at this level.  */
  500.   U_CHAR *free_ptr;
  501.   /* True if this is a header file included using <FILENAME>.  */
  502.   char system_header_p;
  503. } instack[INPUT_STACK_MAX];
  504.  
  505. static int last_error_tick;       /* Incremented each time we print it.  */
  506. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  507.  
  508. /* Current nesting level of input sources.
  509.    `instack[indepth]' is the level currently being read.  */
  510. static int indepth = -1;
  511. #define CHECK_DEPTH(code) \
  512.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  513.     {                                    \
  514.       error_with_line (line_for_error (instack[indepth].lineno),    \
  515.                "macro or `#include' recursion too deep");    \
  516.       code;                                \
  517.     }
  518.  
  519. /* Current depth in #include directives that use <...>.  */
  520. static int system_include_depth = 0;
  521.  
  522. typedef struct file_buf FILE_BUF;
  523.  
  524. /* The output buffer.  Its LENGTH field is the amount of room allocated
  525.    for the buffer, not the number of chars actually present.  To get
  526.    that, subtract outbuf.buf from outbuf.bufp. */
  527.  
  528. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  529. static FILE_BUF outbuf;
  530.  
  531. /* Grow output buffer OBUF points at
  532.    so it can hold at least NEEDED more chars.  */
  533.  
  534. #define check_expand(OBUF, NEEDED)  \
  535.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  536.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  537.  
  538. struct file_name_list
  539.   {
  540.     struct file_name_list *next;
  541.     char *fname;
  542.     /* If the following is nonzero, it is a macro name.
  543.        Don't include the file again if that macro is defined.  */
  544.     U_CHAR *control_macro;
  545.   };
  546.  
  547. /* #include "file" looks in source file dir, then stack. */
  548. /* #include <file> just looks in the stack. */
  549. /* -I directories are added to the end, then the defaults are added. */
  550. static struct default_include { char *fname; int cplusplus; } include_defaults_array[]
  551. #ifdef INCLUDE_DEFAULTS
  552.   = INCLUDE_DEFAULTS;
  553. #else
  554.   = {
  555.     /* Pick up GNU C++ specific include files.  */
  556.     { GPLUSPLUS_INCLUDE_DIR, 1},
  557.     { GCC_INCLUDE_DIR, 0},
  558. #ifdef CROSS_COMPILE
  559.     /* For cross-compilation, this dir name is generated
  560.        automatically in Makefile.in.  */
  561.     { CROSS_INCLUDE_DIR, 0 },
  562. #else /* not CROSS_COMPILE */
  563.     { LOCAL_INCLUDE_DIR, 0},
  564.     /* Some systems have an extra dir of include files.  */
  565. #ifdef SYSTEM_INCLUDE_DIR
  566.     { SYSTEM_INCLUDE_DIR, 0},
  567. #endif
  568.     { STANDARD_INCLUDE_DIR, 0},
  569. #endif /* not CROSS_COMPILE */
  570.     { 0, 0}
  571.     };
  572. #endif /* no INCLUDE_DEFAULTS */
  573.  
  574. /* The code looks at the defaults through this pointer, rather than through
  575.    the constant structure above.  This pointer gets changed if an environment
  576.    variable specifies other defaults.  */
  577. static struct default_include *include_defaults = include_defaults_array;
  578.  
  579. static struct file_name_list *include = 0;    /* First dir to search */
  580.     /* First dir to search for <file> */
  581. /* This is the first element to use for #include <...>.
  582.    If it is 0, use the entire chain for such includes.  */
  583. static struct file_name_list *first_bracket_include = 0;
  584. /* This is the first element in the chain that corresponds to
  585.    a directory of system header files.  */
  586. static struct file_name_list *first_system_include = 0;
  587. static struct file_name_list *last_include = 0;    /* Last in chain */
  588.  
  589. /* Chain of include directories to put at the end of the other chain.  */
  590. static struct file_name_list *after_include = 0;
  591. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  592.  
  593. /* List of included files that contained #pragma once.  */
  594. static struct file_name_list *dont_repeat_files = 0;
  595.  
  596. /* List of other included files.
  597.    If ->control_macro if nonzero, the file had a #ifndef
  598.    around the entire contents, and ->control_macro gives the macro name.  */
  599. static struct file_name_list *all_include_files = 0;
  600.  
  601. /* Directory prefix that should replace `/usr' in the standard
  602.    include file directories.  */
  603. static char *include_prefix;
  604.  
  605. /* Global list of strings read in from precompiled files.  This list
  606.    is kept in the order the strings are read in, with new strings being
  607.    added at the end through stringlist_tailp.  We use this list to output
  608.    the strings at the end of the run. 
  609. */
  610. static STRINGDEF *stringlist;
  611. static STRINGDEF **stringlist_tailp = &stringlist;
  612.  
  613.  
  614. /* Structure returned by create_definition */
  615. typedef struct macrodef MACRODEF;
  616. struct macrodef
  617. {
  618.   struct definition *defn;
  619.   U_CHAR *symnam;
  620.   int symlen;
  621. };
  622.  
  623. static struct macrodef create_definition ();
  624.  
  625.  
  626. /* Structure allocated for every #define.  For a simple replacement
  627.    such as
  628.        #define foo bar ,
  629.    nargs = -1, the `pattern' list is null, and the expansion is just
  630.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  631.    e.g.,
  632.        #define getchar() getc (stdin) .
  633.    When there are args, the expansion is the replacement text with the
  634.    args squashed out, and the reflist is a list describing how to
  635.    build the output from the input: e.g., "3 chars, then the 1st arg,
  636.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  637.    The chars here come from the expansion.  Whatever is left of the
  638.    expansion after the last arg-occurrence is copied after that arg.
  639.    Note that the reflist can be arbitrarily long---
  640.    its length depends on the number of times the arguments appear in
  641.    the replacement text, not how many args there are.  Example:
  642.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  643.    pattern list
  644.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  645.    where (x, y) means (nchars, argno). */
  646.  
  647. typedef struct definition DEFINITION;
  648. struct definition {
  649.   int nargs;
  650.   int length;            /* length of expansion string */
  651.   int predefined;        /* True if the macro was builtin or */
  652.                 /* came from the command line */
  653.   U_CHAR *expansion;
  654.   int line;            /* Line number of definition */
  655.   char *file;            /* File of definition */
  656.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  657.   struct reflist {
  658.     struct reflist *next;
  659.     char stringify;        /* nonzero if this arg was preceded by a
  660.                    # operator. */
  661.     char raw_before;        /* Nonzero if a ## operator before arg. */
  662.     char raw_after;        /* Nonzero if a ## operator after arg. */
  663.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  664.     int nchars;            /* Number of literal chars to copy before
  665.                    this arg occurrence.  */
  666.     int argno;            /* Number of arg to substitute (origin-0) */
  667.   } *pattern;
  668.   union {
  669.     /* Names of macro args, concatenated in reverse order
  670.        with comma-space between them.
  671.        The only use of this is that we warn on redefinition
  672.        if this differs between the old and new definitions.  */
  673.     U_CHAR *argnames;
  674.   } args;
  675. };
  676.  
  677. /* different kinds of things that can appear in the value field
  678.    of a hash node.  Actually, this may be useless now. */
  679. union hashval {
  680.   int ival;
  681.   char *cpval;
  682.   DEFINITION *defn;
  683.   KEYDEF *keydef;
  684. };
  685.  
  686. /*
  687.  * special extension string that can be added to the last macro argument to 
  688.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  689.  *         #define wow(a, b...)        process(b, a, b)
  690.  *        { wow(1, 2, 3); }    ->    { process( 2, 3, 1,  2, 3); }
  691.  *        { wow(one, two); }    ->    { process( two, one,  two); }
  692.  * if this "rest_arg" is used with the concat token '##' and if it is not
  693.  * supplied then the token attached to with ## will not be outputted.  Ex:
  694.  *         #define wow(a, b...)        process(b ## , a, ## b)
  695.  *        { wow(1, 2); }        ->    { process( 2, 1,2); }
  696.  *        { wow(one); }        ->    { process( one); {
  697.  */
  698. static char rest_extension[] = "...";
  699. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  700.  
  701. /* The structure of a node in the hash table.  The hash table
  702.    has entries for all tokens defined by #define commands (type T_MACRO),
  703.    plus some special tokens like __LINE__ (these each have their own
  704.    type, and the appropriate code is run when that type of node is seen.
  705.    It does not contain control words like "#define", which are recognized
  706.    by a separate piece of code. */
  707.  
  708. /* different flavors of hash nodes --- also used in keyword table */
  709. enum node_type {
  710.  T_DEFINE = 1,    /* the `#define' keyword */
  711.  T_INCLUDE,    /* the `#include' keyword */
  712.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  713.  T_IMPORT,      /* the `#import' keyword */
  714.  T_IFDEF,    /* the `#ifdef' keyword */
  715.  T_IFNDEF,    /* the `#ifndef' keyword */
  716.  T_IF,        /* the `#if' keyword */
  717.  T_ELSE,    /* `#else' */
  718.  T_PRAGMA,    /* `#pragma' */
  719.  T_ELIF,    /* `#elif' */
  720.  T_UNDEF,    /* `#undef' */
  721.  T_LINE,    /* `#line' */
  722.  T_ERROR,    /* `#error' */
  723.  T_WARNING,    /* `#warning' */
  724.  T_ENDIF,    /* `#endif' */
  725.  T_SCCS,    /* `#sccs', used on system V.  */
  726.  T_IDENT,    /* `#ident', used on system V.  */
  727.  T_ASSERT,    /* `#assert', taken from system V.  */
  728.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  729.  T_SPECLINE,    /* special symbol `__LINE__' */
  730.  T_DATE,    /* `__DATE__' */
  731.  T_FILE,    /* `__FILE__' */
  732.  T_BASE_FILE,    /* `__BASE_FILE__' */
  733.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  734.  T_VERSION,    /* `__VERSION__' */
  735.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  736.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  737.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  738.  T_TIME,    /* `__TIME__' */
  739.  T_CONST,    /* Constant value, used by `__STDC__' */
  740.  T_MACRO,    /* macro defined by `#define' */
  741.  T_DISABLED,    /* macro temporarily turned off for rescan */
  742.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  743.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  744.  T_UNUSED    /* Used for something not defined.  */
  745.  };
  746.  
  747. struct hashnode {
  748.   struct hashnode *next;    /* double links for easy deletion */
  749.   struct hashnode *prev;
  750.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  751.                    chain is kept, in case the node is the head
  752.                    of the chain and gets deleted. */
  753.   enum node_type type;        /* type of special token */
  754.   int length;            /* length of token, for quick comparison */
  755.   U_CHAR *name;            /* the actual name */
  756.   union hashval value;        /* pointer to expansion, or whatever */
  757. };
  758.  
  759. typedef struct hashnode HASHNODE;
  760.  
  761. /* Some definitions for the hash table.  The hash function MUST be
  762.    computed as shown in hashf () below.  That is because the rescan
  763.    loop computes the hash value `on the fly' for most tokens,
  764.    in order to avoid the overhead of a lot of procedure calls to
  765.    the hashf () function.  Hashf () only exists for the sake of
  766.    politeness, for use when speed isn't so important. */
  767.  
  768. #define HASHSIZE 1403
  769. static HASHNODE *hashtab[HASHSIZE];
  770. #define HASHSTEP(old, c) ((old << 2) + c)
  771. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  772.  
  773. /* Symbols to predefine.  */
  774.  
  775. #ifdef CPP_PREDEFINES
  776. static char *predefs = CPP_PREDEFINES;
  777. #else
  778. static char *predefs = "";
  779. #endif
  780.  
  781. /* We let tm.h override the types used here, to handle trivial differences
  782.    such as the choice of unsigned int or long unsigned int for size_t.
  783.    When machines start needing nontrivial differences in the size type,
  784.    it would be best to do something here to figure out automatically
  785.    from other information what type to use.  */
  786.  
  787. /* The string value for __size_type__.  */
  788.  
  789. #ifndef SIZE_TYPE
  790. #define SIZE_TYPE "long unsigned int"
  791. #endif
  792.  
  793. /* The string value for __ptrdiff_type__.  */
  794.  
  795. #ifndef PTRDIFF_TYPE
  796. #define PTRDIFF_TYPE "long int"
  797. #endif
  798.  
  799. /* The string value for __wchar_type__.  */
  800.  
  801. #ifndef WCHAR_TYPE
  802. #define WCHAR_TYPE "int"
  803. #endif
  804.  
  805. /* In the definition of a #assert name, this structure forms
  806.    a list of the individual values asserted.
  807.    Each value is itself a list of "tokens".
  808.    These are strings that are compared by name.  */
  809.  
  810. struct tokenlist_list {
  811.   struct tokenlist_list *next;
  812.   struct arglist *tokens;
  813. };
  814.  
  815. struct assertion_hashnode {
  816.   struct assertion_hashnode *next;    /* double links for easy deletion */
  817.   struct assertion_hashnode *prev;
  818.   /* also, a back pointer to this node's hash
  819.      chain is kept, in case the node is the head
  820.      of the chain and gets deleted. */
  821.   struct assertion_hashnode **bucket_hdr;
  822.   int length;            /* length of token, for quick comparison */
  823.   U_CHAR *name;            /* the actual name */
  824.   /* List of token-sequences.  */
  825.   struct tokenlist_list *value;
  826. };
  827.  
  828. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  829.  
  830. /* Some definitions for the hash table.  The hash function MUST be
  831.    computed as shown in hashf below.  That is because the rescan
  832.    loop computes the hash value `on the fly' for most tokens,
  833.    in order to avoid the overhead of a lot of procedure calls to
  834.    the hashf function.  hashf only exists for the sake of
  835.    politeness, for use when speed isn't so important. */
  836.  
  837. #define ASSERTION_HASHSIZE 37
  838. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  839.  
  840. /* Nonzero means inhibit macroexpansion of what seem to be
  841.    assertion tests, in rescan.  For #if.  */
  842. static int assertions_flag;
  843.  
  844. /* `struct directive' defines one #-directive, including how to handle it.  */
  845.  
  846. struct directive {
  847.   int length;            /* Length of name */
  848.   int (*func)();        /* Function to handle directive */
  849.   char *name;            /* Name of directive */
  850.   enum node_type type;        /* Code which describes which directive. */
  851.   char angle_brackets;        /* Nonzero => <...> is special.  */
  852.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  853.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  854. };
  855.  
  856. /* Here is the actual list of #-directives, most-often-used first.  */
  857.  
  858. static struct directive directive_table[] = {
  859.   {  6, do_define, "define", T_DEFINE, 0, 1},
  860.   {  2, do_if, "if", T_IF},
  861.   {  5, do_xifdef, "ifdef", T_IFDEF},
  862.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  863.   {  5, do_endif, "endif", T_ENDIF},
  864.   {  4, do_else, "else", T_ELSE},
  865.   {  4, do_elif, "elif", T_ELIF},
  866.   {  4, do_line, "line", T_LINE},
  867.   {  7, do_include, "include", T_INCLUDE, 1},
  868.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  869.   {  6, do_include, "import", T_IMPORT, 1},
  870.   {  5, do_undef, "undef", T_UNDEF},
  871.   {  5, do_error, "error", T_ERROR},
  872.   {  7, do_warning, "warning", T_WARNING},
  873. #ifdef SCCS_DIRECTIVE
  874.   {  4, do_sccs, "sccs", T_SCCS},
  875. #endif
  876.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  877.   {  5, do_ident, "ident", T_IDENT, 0, 0, 1},
  878.   {  6, do_assert, "assert", T_ASSERT},
  879.   {  8, do_unassert, "unassert", T_UNASSERT},
  880.   {  -1, 0, "", T_UNUSED},
  881. };
  882.  
  883. /* When a directive handler is called,
  884.    this points to the # that started the directive.  */
  885. U_CHAR *directive_start;
  886.  
  887. /* table to tell if char can be part of a C identifier. */
  888. U_CHAR is_idchar[256];
  889. /* table to tell if char can be first char of a c identifier. */
  890. U_CHAR is_idstart[256];
  891. /* table to tell if c is horizontal space.  */
  892. U_CHAR is_hor_space[256];
  893. /* table to tell if c is horizontal or vertical space.  */
  894. static U_CHAR is_space[256];
  895.  
  896. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  897. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  898.   
  899. static int errors = 0;            /* Error counter for exit code */
  900.  
  901. /* Zero means dollar signs are punctuation.
  902.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  903.    This must be 0 for correct processing of this ANSI C program:
  904.     #define foo(a) #a
  905.     #define lose(b) foo(b)
  906.     #define test$
  907.     lose(test)    */
  908. static int dollars_in_ident;
  909. #ifndef DOLLARS_IN_IDENTIFIERS
  910. #define DOLLARS_IN_IDENTIFIERS 1
  911. #endif
  912.  
  913. static FILE_BUF expand_to_temp_buffer ();
  914.  
  915. static DEFINITION *collect_expansion ();
  916.  
  917. /* Stack of conditionals currently in progress
  918.    (including both successful and failing conditionals).  */
  919.  
  920. struct if_stack {
  921.   struct if_stack *next;    /* for chaining to the next stack frame */
  922.   char *fname;        /* copied from input when frame is made */
  923.   int lineno;            /* similarly */
  924.   int if_succeeded;        /* true if a leg of this if-group
  925.                     has been passed through rescan */
  926.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  927.                    this is the macro name tested.  */
  928.   enum node_type type;        /* type of last directive seen in this group */
  929. };
  930. typedef struct if_stack IF_STACK_FRAME;
  931. static IF_STACK_FRAME *if_stack = NULL;
  932.  
  933. /* Buffer of -M output.  */
  934. static char *deps_buffer;
  935.  
  936. /* Number of bytes allocated in above.  */
  937. static int deps_allocated_size;
  938.  
  939. /* Number of bytes used.  */
  940. static int deps_size;
  941.  
  942. /* Number of bytes since the last newline.  */
  943. static int deps_column;
  944.  
  945. /* Nonzero means -I- has been seen,
  946.    so don't look for #include "foo" the source-file directory.  */
  947. static int ignore_srcdir;
  948.  
  949. int
  950. main (argc, argv)
  951.      int argc;
  952.      char **argv;
  953. {
  954.   int st_mode;
  955.   long st_size;
  956.   char *in_fname, *out_fname;
  957.   char *p;
  958.   int f, i;
  959.   FILE_BUF *fp;
  960.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  961.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  962.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  963.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  964.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  965.  
  966.   /* Record the option used with each element of pend_assertions.
  967.      This is preparation for supporting more than one option for making
  968.      an assertion.  */
  969.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  970.   int inhibit_predefs = 0;
  971.   int no_standard_includes = 0;
  972.   int no_standard_cplusplus_includes = 0;
  973.   int missing_newline = 0;
  974.  
  975.   /* Non-0 means don't output the preprocessed program.  */
  976.   int inhibit_output = 0;
  977.  
  978.   /* File name which deps are being written to.
  979.      This is 0 if deps are being written to stdout.  */
  980.   char *deps_file = 0;
  981.   /* Stream on which to print the dependency information.  */
  982.   FILE *deps_stream = 0;
  983.   /* Target-name to write with the dependency information.  */
  984.   char *deps_target = 0;
  985.  
  986. #ifdef RLIMIT_STACK
  987.   /* Get rid of any avoidable limit on stack size.  */
  988.   {
  989.     struct rlimit rlim;
  990.  
  991.     /* Set the stack limit huge so that alloca (particularly stringtab
  992.      * in dbxread.c) does not fail. */
  993.     getrlimit (RLIMIT_STACK, &rlim);
  994.     rlim.rlim_cur = rlim.rlim_max;
  995.     setrlimit (RLIMIT_STACK, &rlim);
  996.   }
  997. #endif /* RLIMIT_STACK defined */
  998.  
  999.   progname = argv[0];
  1000. #ifdef VMS
  1001.   {
  1002.     /* Remove directories from PROGNAME.  */
  1003.     char *s;
  1004.  
  1005.     progname = savestring (argv[0]);
  1006.  
  1007.     if (!(s = rindex (progname, ']')))
  1008.       s = rindex (progname, ':');
  1009.     if (s)
  1010.       strcpy (progname, s+1);
  1011.     if (s = rindex (progname, '.'))
  1012.       *s = '\0';
  1013.   }
  1014. #endif
  1015.  
  1016.   in_fname = NULL;
  1017.   out_fname = NULL;
  1018.  
  1019.   /* Initialize is_idchar to allow $.  */
  1020.   dollars_in_ident = 1;
  1021.   initialize_char_syntax ();
  1022.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1023.  
  1024.   no_line_commands = 0;
  1025.   no_trigraphs = 1;
  1026.   dump_macros = dump_none;
  1027.   no_output = 0;
  1028.   cplusplus = 0;
  1029.   cplusplus_comments = 0;
  1030.  
  1031.   bzero (pend_files, argc * sizeof (char *));
  1032.   bzero (pend_defs, argc * sizeof (char *));
  1033.   bzero (pend_undefs, argc * sizeof (char *));
  1034.   bzero (pend_assertions, argc * sizeof (char *));
  1035.   bzero (pend_includes, argc * sizeof (char *));
  1036.  
  1037.   /* Process switches and find input file name.  */
  1038.  
  1039.   for (i = 1; i < argc; i++) {
  1040.     if (argv[i][0] != '-') {
  1041.       if (out_fname != NULL)
  1042.     fatal ("Usage: %s [switches] input output", argv[0]);
  1043.       else if (in_fname != NULL)
  1044.     out_fname = argv[i];
  1045.       else
  1046.     in_fname = argv[i];
  1047.     } else {
  1048.       switch (argv[i][1]) {
  1049.  
  1050.       case 'i':
  1051.     if (!strcmp (argv[i], "-include")) {
  1052.       if (i + 1 == argc)
  1053.         fatal ("Filename missing after -include option");
  1054.       else
  1055.         pend_includes[i] = argv[i+1], i++;
  1056.     }
  1057.     if (!strcmp (argv[i], "-imacros")) {
  1058.       if (i + 1 == argc)
  1059.         fatal ("Filename missing after -imacros option");
  1060.       else
  1061.         pend_files[i] = argv[i+1], i++;
  1062.     }
  1063.     if (!strcmp (argv[i], "-iprefix")) {
  1064.       if (i + 1 == argc)
  1065.         fatal ("Filename missing after -iprefix option");
  1066.       else
  1067.         include_prefix = argv[++i];
  1068.     }
  1069.     /* Add directory to end of path for includes.  */
  1070.     if (!strcmp (argv[i], "-idirafter")) {
  1071.       struct file_name_list *dirtmp;
  1072.  
  1073.       dirtmp = (struct file_name_list *)
  1074.         xmalloc (sizeof (struct file_name_list));
  1075.       dirtmp->next = 0;    /* New one goes on the end */
  1076.       dirtmp->control_macro = 0;
  1077.       if (i + 1 == argc)
  1078.         fatal ("Directory name missing after -idirafter option");
  1079.       else
  1080.         dirtmp->fname = argv[++i];
  1081.  
  1082.       if (after_include == 0)
  1083.         after_include = dirtmp;
  1084.       else
  1085.         last_after_include->next = dirtmp;
  1086.       last_after_include = dirtmp; /* Tail follows the last one */
  1087.     }
  1088.     break;
  1089.  
  1090.       case 'o':
  1091.     if (out_fname != NULL)
  1092.       fatal ("Output filename specified twice");
  1093.     if (i + 1 == argc)
  1094.       fatal ("Filename missing after -o option");
  1095.     out_fname = argv[++i];
  1096.     if (!strcmp (out_fname, "-"))
  1097.       out_fname = "";
  1098.     break;
  1099.  
  1100.       case 'p':
  1101.     if (!strcmp (argv[i], "-pedantic"))
  1102.       pedantic = 1;
  1103.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1104.       pedantic = 1;
  1105.       pedantic_errors = 1;
  1106.     } else if (!strcmp (argv[i], "-pcp")) {
  1107.       char *pcp_fname = argv[++i];
  1108.       pcp_outfile = 
  1109.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1110.          ? fopen (pcp_fname, "w")
  1111.          : fdopen (dup (fileno (stdout)), "w"));
  1112.       if (pcp_outfile == 0)
  1113.         pfatal_with_name (pcp_fname);
  1114.       no_precomp = 1;
  1115.     }
  1116.     break;
  1117.  
  1118.       case 't':
  1119.     if (!strcmp (argv[i], "-traditional")) {
  1120.       traditional = 1;
  1121.       if (dollars_in_ident > 0)
  1122.         dollars_in_ident = 1;
  1123.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1124.       no_trigraphs = 0;
  1125.     }
  1126.     break;
  1127.  
  1128.       case 'l':
  1129.     if (! strcmp (argv[i], "-lang-c"))
  1130.       cplusplus = 0, cplusplus_comments = 0, objc = 0;
  1131.     if (! strcmp (argv[i], "-lang-c++"))
  1132.       cplusplus = 1, cplusplus_comments = 1, objc = 0;
  1133.     if (! strcmp (argv[i], "-lang-objc"))
  1134.       objc = 1, cplusplus = 0, cplusplus_comments = 1;
  1135.     if (! strcmp (argv[i], "-lang-objc++"))
  1136.       objc = 1, cplusplus = 1, cplusplus_comments = 1;
  1137.      if (! strcmp (argv[i], "-lang-asm"))
  1138.        lang_asm = 1;
  1139.      if (! strcmp (argv[i], "-lint"))
  1140.        lint = 1;
  1141.     break;
  1142.  
  1143.       case '+':
  1144.     cplusplus = 1, cplusplus_comments = 1;
  1145.     break;
  1146.  
  1147.       case 'w':
  1148.     inhibit_warnings = 1;
  1149.     break;
  1150.  
  1151.       case 'W':
  1152.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1153.       warn_trigraphs = 1;
  1154.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1155.       warn_trigraphs = 0;
  1156.     else if (!strcmp (argv[i], "-Wcomment"))
  1157.       warn_comments = 1;
  1158.     else if (!strcmp (argv[i], "-Wno-comment"))
  1159.       warn_comments = 0;
  1160.     else if (!strcmp (argv[i], "-Wcomments"))
  1161.       warn_comments = 1;
  1162.     else if (!strcmp (argv[i], "-Wno-comments"))
  1163.       warn_comments = 0;
  1164.     else if (!strcmp (argv[i], "-Wtraditional"))
  1165.       warn_stringify = 1;
  1166.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1167.       warn_stringify = 0;
  1168.     else if (!strcmp (argv[i], "-Wimport"))
  1169.       warn_import = 1;
  1170.     else if (!strcmp (argv[i], "-Wno-import"))
  1171.       warn_import = 0;
  1172.     else if (!strcmp (argv[i], "-Werror"))
  1173.       warnings_are_errors = 1;
  1174.     else if (!strcmp (argv[i], "-Wno-error"))
  1175.       warnings_are_errors = 0;
  1176.     else if (!strcmp (argv[i], "-Wall"))
  1177.       {
  1178.         warn_trigraphs = 1;
  1179.         warn_comments = 1;
  1180.       }
  1181.     break;
  1182.  
  1183.       case 'M':
  1184.     if (!strcmp (argv[i], "-M"))
  1185.       print_deps = 2;
  1186.     else if (!strcmp (argv[i], "-MM"))
  1187.       print_deps = 1;
  1188.     else if (!strcmp (argv[i], "-MD"))
  1189.       print_deps = 2;
  1190.     else if (!strcmp (argv[i], "-MMD"))
  1191.       print_deps = 1;
  1192.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1193.     if (!strcmp (argv[i], "-MD")
  1194.         || !strcmp (argv[i], "-MMD")) {
  1195.       i++;
  1196.       deps_file = argv[i];
  1197.     } else {
  1198.       /* For -M and -MM, write deps on standard output
  1199.          and suppress the usual output.  */
  1200.       deps_stream = stdout;
  1201.       inhibit_output = 1;
  1202.     }      
  1203.     break;
  1204.  
  1205.       case 'd':
  1206.     {
  1207.       char *p = argv[i] + 2;
  1208.       char c;
  1209.       while (c = *p++) {
  1210.         /* Arg to -d specifies what parts of macros to dump */
  1211.         switch (c) {
  1212.         case 'M':
  1213.           dump_macros = dump_only;
  1214.           no_output = 1;
  1215.           break;
  1216.         case 'N':
  1217.           dump_macros = dump_names;
  1218.           break;
  1219.         case 'D':
  1220.           dump_macros = dump_definitions;
  1221.           break;
  1222.         }
  1223.       }
  1224.     }
  1225.     break;
  1226.  
  1227.       case 'g':
  1228.     if (argv[i][2] == '3')
  1229.       debug_output = 1;
  1230.     break;
  1231.  
  1232.       case 'v':
  1233.     fprintf (stderr, "GNU CPP version %s", version_string);
  1234. #ifdef TARGET_VERSION
  1235.     TARGET_VERSION;
  1236. #endif
  1237.     fprintf (stderr, "\n");
  1238.     break;
  1239.  
  1240.       case 'H':
  1241.     print_include_names = 1;
  1242.     break;
  1243.  
  1244.       case 'D':
  1245.     {
  1246.       char *p, *p1;
  1247.  
  1248.       if (argv[i][2] != 0)
  1249.         p = argv[i] + 2;
  1250.       else if (i + 1 == argc)
  1251.         fatal ("Macro name missing after -D option");
  1252.       else
  1253.         p = argv[++i];
  1254.  
  1255.       pend_defs[i] = p;
  1256.     }
  1257.     break;
  1258.  
  1259.       case 'A':
  1260.     {
  1261.       char *p, *p1;
  1262.  
  1263.       if (argv[i][2] != 0)
  1264.         p = argv[i] + 2;
  1265.       else if (i + 1 == argc)
  1266.         fatal ("Assertion missing after -A option");
  1267.       else
  1268.         p = argv[++i];
  1269.  
  1270.       if (!strcmp (p, "-")) {
  1271.         /* -A- eliminates all predefined macros and assertions.
  1272.            Let's include also any that were specified earlier
  1273.            on the command line.  That way we can get rid of any
  1274.            that were passed automatically in from GCC.  */
  1275.         int j;
  1276.         inhibit_predefs = 1;
  1277.         for (j = 0; j < i; j++)
  1278.           pend_defs[j] = pend_assertions[j] = 0;
  1279.       } else {
  1280.         pend_assertions[i] = p;
  1281.         pend_assertion_options[i] = "-A";
  1282.       }
  1283.     }
  1284.     break;
  1285.  
  1286.       case 'U':        /* JF #undef something */
  1287.     if (argv[i][2] != 0)
  1288.       pend_undefs[i] = argv[i] + 2;
  1289.     else if (i + 1 == argc)
  1290.       fatal ("Macro name missing after -U option");
  1291.     else
  1292.       pend_undefs[i] = argv[i+1], i++;
  1293.     break;
  1294.  
  1295.       case 'C':
  1296.     put_out_comments = 1;
  1297.     break;
  1298.  
  1299.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1300.     break;
  1301.  
  1302.       case 'P':
  1303.     no_line_commands = 1;
  1304.     break;
  1305.  
  1306.       case '$':            /* Don't include $ in identifiers.  */
  1307.     dollars_in_ident = 0;
  1308.     break;
  1309.  
  1310.       case 'I':            /* Add directory to path for includes.  */
  1311.     {
  1312.       struct file_name_list *dirtmp;
  1313.  
  1314.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1315.         ignore_srcdir = 1;
  1316.         /* Don't use any preceding -I directories for #include <...>.  */
  1317.         first_bracket_include = 0;
  1318.       }
  1319.       else {
  1320.         dirtmp = (struct file_name_list *)
  1321.           xmalloc (sizeof (struct file_name_list));
  1322.         dirtmp->next = 0;        /* New one goes on the end */
  1323.         dirtmp->control_macro = 0;
  1324.         if (argv[i][2] != 0)
  1325.           dirtmp->fname = argv[i] + 2;
  1326.         else if (i + 1 == argc)
  1327.           fatal ("Directory name missing after -I option");
  1328.         else
  1329.           dirtmp->fname = argv[++i];
  1330.         append_include_chain (dirtmp, dirtmp);
  1331.       }
  1332.     }
  1333.     break;
  1334.  
  1335.       case 'n':
  1336.     if (!strcmp (argv[i], "-nostdinc"))
  1337.       /* -nostdinc causes no default include directories.
  1338.          You must specify all include-file directories with -I.  */
  1339.       no_standard_includes = 1;
  1340.     else if (!strcmp (argv[i], "-nostdinc++"))
  1341.       /* -nostdinc++ causes no default C++-specific include directories. */
  1342.       no_standard_cplusplus_includes = 1;
  1343.     else if (!strcmp (argv[i], "-noprecomp"))
  1344.       no_precomp = 1;
  1345.     break;
  1346.  
  1347.       case 'u':
  1348.     /* Sun compiler passes undocumented switch "-undef".
  1349.        Let's assume it means to inhibit the predefined symbols.  */
  1350.     inhibit_predefs = 1;
  1351.     break;
  1352.  
  1353.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  1354.     if (in_fname == NULL) {
  1355.       in_fname = "";
  1356.       break;
  1357.     } else if (out_fname == NULL) {
  1358.       out_fname = "";
  1359.       break;
  1360.     }    /* else fall through into error */
  1361.  
  1362.       default:
  1363.     fatal ("Invalid option `%s'", argv[i]);
  1364.       }
  1365.     }
  1366.   }
  1367.  
  1368.   /* Add dirs from CPATH after dirs from -I.  */
  1369.   /* There seems to be confusion about what CPATH should do,
  1370.      so for the moment it is not documented.  */
  1371.   /* Some people say that CPATH should replace the standard include dirs,
  1372.      but that seems pointless: it comes before them, so it overrides them
  1373.      anyway.  */
  1374.   p = (char *) getenv ("CPATH");
  1375.   if (p != 0 && ! no_standard_includes)
  1376.     path_include (p);
  1377.  
  1378.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  1379.   initialize_char_syntax ();
  1380.  
  1381.   /* Initialize output buffer */
  1382.  
  1383.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  1384.   outbuf.bufp = outbuf.buf;
  1385.   outbuf.length = OUTBUF_SIZE;
  1386.  
  1387.   /* Do partial setup of input buffer for the sake of generating
  1388.      early #line directives (when -g is in effect).  */
  1389.  
  1390.   fp = &instack[++indepth];
  1391.   if (in_fname == NULL)
  1392.     in_fname = "";
  1393.   fp->nominal_fname = fp->fname = in_fname;
  1394.   fp->lineno = 0;
  1395.  
  1396.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  1397.      and option processing.  */
  1398.   initialize_builtins (fp, &outbuf);
  1399.  
  1400.   /* Do standard #defines and assertions
  1401.      that identify system and machine type.  */
  1402.  
  1403.   if (!inhibit_predefs) {
  1404.     char *p = (char *) alloca (strlen (predefs) + 1);
  1405.     strcpy (p, predefs);
  1406.     while (*p) {
  1407.       char *q;
  1408.       while (*p == ' ' || *p == '\t')
  1409.     p++;
  1410.       /* Handle -D options.  */ 
  1411.       if (p[0] == '-' && p[1] == 'D') {
  1412.     q = &p[2];
  1413.     while (*p && *p != ' ' && *p != '\t')
  1414.       p++;
  1415.     if (*p != 0)
  1416.       *p++= 0;
  1417.     if (debug_output)
  1418.       output_line_command (fp, &outbuf, 0, same_file);
  1419.     make_definition (q, &outbuf);
  1420.     while (*p == ' ' || *p == '\t')
  1421.       p++;
  1422.       } else if (p[0] == '-' && p[1] == 'A') {
  1423.     /* Handle -A options (assertions).  */ 
  1424.     char *assertion;
  1425.     char *past_name;
  1426.     char *value;
  1427.     char *past_value;
  1428.     char *termination;
  1429.     int save_char;
  1430.  
  1431.     assertion = &p[2];
  1432.     past_name = assertion;
  1433.     /* Locate end of name.  */
  1434.     while (*past_name && *past_name != ' '
  1435.            && *past_name != '\t' && *past_name != '(')
  1436.       past_name++;
  1437.     /* Locate `(' at start of value.  */
  1438.     value = past_name;
  1439.     while (*value && (*value == ' ' || *value == '\t'))
  1440.       value++;
  1441.     if (*value++ != '(')
  1442.       abort ();
  1443.     while (*value && (*value == ' ' || *value == '\t'))
  1444.       value++;
  1445.     past_value = value;
  1446.     /* Locate end of value.  */
  1447.     while (*past_value && *past_value != ' '
  1448.            && *past_value != '\t' && *past_value != ')')
  1449.       past_value++;
  1450.     termination = past_value;
  1451.     while (*termination && (*termination == ' ' || *termination == '\t'))
  1452.       termination++;
  1453.     if (*termination++ != ')')
  1454.       abort ();
  1455.     if (*termination && *termination != ' ' && *termination != '\t')
  1456.       abort ();
  1457.     /* Temporarily null-terminate the value.  */
  1458.     save_char = *termination;
  1459.     *termination = '\0';
  1460.     /* Install the assertion.  */
  1461.     make_assertion ("-A", assertion);
  1462.     *termination = (char) save_char;
  1463.     p = termination;
  1464.     while (*p == ' ' || *p == '\t')
  1465.       p++;
  1466.       } else {
  1467.     abort ();
  1468.       }
  1469.     }
  1470.   }
  1471.  
  1472.   /* Now handle the command line options.  */
  1473.  
  1474.   /* Do -U's, -D's and -A's in the order they were seen.  */
  1475.   for (i = 1; i < argc; i++) {
  1476.     if (pend_undefs[i]) {
  1477.       if (debug_output)
  1478.         output_line_command (fp, &outbuf, 0, same_file);
  1479.       make_undef (pend_undefs[i], &outbuf);
  1480.     }
  1481.     if (pend_defs[i]) {
  1482.       if (debug_output)
  1483.         output_line_command (fp, &outbuf, 0, same_file);
  1484.       make_definition (pend_defs[i], &outbuf);
  1485.     }
  1486.     if (pend_assertions[i])
  1487.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  1488.   }
  1489.  
  1490.   done_initializing = 1;
  1491.  
  1492.   { /* read the appropriate environment variable and if it exists
  1493.        replace include_defaults with the listed path. */
  1494.     char *epath = 0;
  1495.     switch ((objc << 1) + cplusplus)
  1496.       {
  1497.       case 0:
  1498.     epath = getenv ("C_INCLUDE_PATH");
  1499.     break;
  1500.       case 1:
  1501.     epath = getenv ("CPLUS_INCLUDE_PATH");
  1502.     break;
  1503.       case 2:
  1504.     epath = getenv ("OBJC_INCLUDE_PATH");
  1505.     break;
  1506.       case 3:
  1507.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  1508.     break;
  1509.       }
  1510.     /* If the environment var for this language is set,
  1511.        add to the default list of include directories.  */
  1512.     if (epath) {
  1513.       char *nstore = (char *) alloca (strlen (epath) + 2);
  1514.       int num_dirs;
  1515.       char *startp, *endp;
  1516.  
  1517.       for (num_dirs = 1, startp = epath; *startp; startp++)
  1518.     if (*startp == PATH_SEPARATOR)
  1519.       num_dirs++;
  1520.       include_defaults
  1521.     = (struct default_include *) xmalloc ((num_dirs
  1522.                            * sizeof (struct default_include))
  1523.                           + sizeof (include_defaults_array));
  1524.       startp = endp = epath;
  1525.       num_dirs = 0;
  1526.       while (1) {
  1527.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  1528.         if ((*endp == PATH_SEPARATOR
  1529. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  1530. #ifdef __MSDOS__
  1531.          && (endp-startp != 1 || !isalpha (*startp))
  1532. #endif
  1533. #endif
  1534.          )
  1535.             || *endp == 0) {
  1536.       strncpy (nstore, startp, endp-startp);
  1537.       if (endp == startp)
  1538.         strcpy (nstore, ".");
  1539.       else
  1540.         nstore[endp-startp] = '\0';
  1541.  
  1542.       include_defaults[num_dirs].fname = savestring (nstore);
  1543.       include_defaults[num_dirs].cplusplus = cplusplus;
  1544.       num_dirs++;
  1545.       if (*endp == '\0')
  1546.         break;
  1547.       endp = startp = endp + 1;
  1548.     } else
  1549.       endp++;
  1550.       }
  1551.       /* Put the usual defaults back in at the end.  */
  1552.       bcopy (include_defaults_array, &include_defaults[num_dirs],
  1553.          sizeof (include_defaults_array));
  1554.     }
  1555.   }
  1556.  
  1557.   first_system_include = 0;
  1558.   /* Unless -fnostdinc,
  1559.      tack on the standard include file dirs to the specified list */
  1560.   if (!no_standard_includes) {
  1561.     struct default_include *p = include_defaults;
  1562.     char *specd_prefix = include_prefix;
  1563.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  1564.     int default_len = 0;
  1565.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1566.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  1567.       default_len = strlen (default_prefix) - 7;
  1568.       default_prefix[default_len] = 0;
  1569.     }
  1570.     /* Search "translated" versions of GNU directories.
  1571.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  1572.     if (specd_prefix != 0 && default_len != 0)
  1573.       for (p = include_defaults; p->fname; p++) {
  1574.     /* Some standard dirs are only for C++.  */
  1575.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  1576.       /* Does this dir start with the prefix?  */
  1577.       if (!strncmp (p->fname, default_prefix, default_len)) {
  1578.         /* Yes; change prefix and add to search list.  */
  1579.         struct file_name_list *new
  1580.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  1581.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  1582.         char *str = (char *) xmalloc (this_len + 1);
  1583.         strcpy (str, specd_prefix);
  1584.         strcat (str, p->fname + default_len);
  1585.         new->fname = str;
  1586.         new->control_macro = 0;
  1587.         append_include_chain (new, new);
  1588.         if (first_system_include == 0)
  1589.           first_system_include = new;
  1590.       }
  1591.     }
  1592.       }
  1593.     /* Search ordinary names for GNU include directories.  */
  1594.     for (p = include_defaults; p->fname; p++) {
  1595.       /* Some standard dirs are only for C++.  */
  1596.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  1597.     struct file_name_list *new
  1598.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  1599.     new->control_macro = 0;
  1600.     new->fname = p->fname;
  1601.     append_include_chain (new, new);
  1602.     if (first_system_include == 0)
  1603.       first_system_include = new;
  1604.       }
  1605.     }
  1606.   }
  1607.  
  1608.   /* Tack the after_include chain at the end of the include chain.  */
  1609.   append_include_chain (after_include, last_after_include);
  1610.   if (first_system_include == 0)
  1611.     first_system_include = after_include;
  1612.  
  1613.   /* Scan the -imacros files before the main input.
  1614.      Much like #including them, but with no_output set
  1615.      so that only their macro definitions matter.  */
  1616.  
  1617.   no_output++;
  1618.   for (i = 1; i < argc; i++)
  1619.     if (pend_files[i]) {
  1620.       int fd = open (pend_files[i], O_RDONLY, 0666);
  1621.       if (fd < 0) {
  1622.     perror_with_name (pend_files[i]);
  1623.     return FAILURE_EXIT_CODE;
  1624.       }
  1625.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  1626.     }
  1627.   no_output--;
  1628.  
  1629.   /* Copy the entire contents of the main input file into
  1630.      the stacked input buffer previously allocated for it.  */
  1631.  
  1632.   /* JF check for stdin */
  1633.   if (in_fname == NULL || *in_fname == 0) {
  1634.     in_fname = "";
  1635.     f = 0;
  1636.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  1637.     goto perror;
  1638.  
  1639.   /* Either of two environment variables can specify output of deps.
  1640.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  1641.      where OUTPUT_FILE is the file to write deps info to
  1642.      and DEPS_TARGET is the target to mention in the deps.  */
  1643.  
  1644.   if (print_deps == 0
  1645.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  1646.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  1647.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  1648.     char *s;
  1649.     char *output_file;
  1650.  
  1651.     if (spec == 0) {
  1652.       spec = getenv ("SUNPRO_DEPENDENCIES");
  1653.       print_deps = 2;
  1654.     }
  1655.     else
  1656.       print_deps = 1;
  1657.  
  1658.     s = spec;
  1659.     /* Find the space before the DEPS_TARGET, if there is one.  */
  1660.     /* This should use index.  (mrs) */
  1661.     while (*s != 0 && *s != ' ') s++;
  1662.     if (*s != 0) {
  1663.       deps_target = s + 1;
  1664.       output_file = (char *) xmalloc (s - spec + 1);
  1665.       bcopy (spec, output_file, s - spec);
  1666.       output_file[s - spec] = 0;
  1667.     }
  1668.     else {
  1669.       deps_target = 0;
  1670.       output_file = spec;
  1671.     }
  1672.       
  1673.     deps_file = output_file;
  1674.   }
  1675.  
  1676.   /* For -M, print the expected object file name
  1677.      as the target of this Make-rule.  */
  1678.   if (print_deps) {
  1679.     deps_allocated_size = 200;
  1680.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  1681.     deps_buffer[0] = 0;
  1682.     deps_size = 0;
  1683.     deps_column = 0;
  1684.  
  1685.     if (deps_target) {
  1686.       deps_output (deps_target, 0);
  1687.       deps_output (":", 0);
  1688.     } else if (*in_fname == 0)
  1689.       deps_output ("-: ", 0);
  1690.     else {
  1691.       int len;
  1692.       char *p = in_fname;
  1693.       char *p1 = p;
  1694.       /* Discard all directory prefixes from P.  */
  1695. #ifdef FILE_NAME_NONDIRECTORY
  1696.       p = FILE_NAME_NONDIRECTORY (p);
  1697. #else
  1698.       while (*p1) {
  1699.     if (*p1 == '/')
  1700.       p = p1 + 1;
  1701.     p1++;
  1702.       }
  1703. #endif
  1704.       /* Output P, but remove known suffixes.  */
  1705.       len = strlen (p);
  1706.       if (p[len - 2] == '.' && p[len - 1] == 'c')
  1707.     deps_output (p, len - 2);
  1708.       else if (p[len - 2] == '.' && p[len - 1] == 'C')
  1709.     deps_output (p, len - 2);
  1710.       else if (p[len - 3] == '.'
  1711.            && p[len - 2] == 'c'
  1712.            && p[len - 1] == 'c')
  1713.     deps_output (p, len - 3);
  1714.       else if (p[len - 2] == '.' && p[len - 1] == 's')
  1715.     deps_output (p, len - 2);
  1716.       else if (p[len - 2] == '.' && p[len - 1] == 'S')
  1717.     deps_output (p, len - 2);
  1718.       else if (p[len - 2] == '.' && p[len - 1] == 'm')
  1719.     deps_output (p, len - 2);
  1720.       else
  1721.     deps_output (p, 0);
  1722.       /* Supply our own suffix.  */
  1723. #ifndef VMS
  1724.       deps_output (".o : ", 0);
  1725. #else
  1726.       deps_output (".obj : ", 0);
  1727. #endif
  1728.       deps_output (in_fname, 0);
  1729.       deps_output (" ", 0);
  1730.     }
  1731.   }
  1732.  
  1733.   file_size_and_mode (f, &st_mode, &st_size);
  1734.   fp->nominal_fname = fp->fname = in_fname;
  1735.   fp->lineno = 1;
  1736.   fp->system_header_p = 0;
  1737.   /* JF all this is mine about reading pipes and ttys */
  1738.   if (! S_ISREG (st_mode)) {
  1739.     /* Read input from a file that is not a normal disk file.
  1740.        We cannot preallocate a buffer with the correct size,
  1741.        so we must read in the file a piece at the time and make it bigger.  */
  1742.     int size;
  1743.     int bsize;
  1744.     int cnt;
  1745.     U_CHAR *bufp;
  1746.  
  1747.     bsize = 2000;
  1748.     size = 0;
  1749.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  1750.     bufp = fp->buf;
  1751.     for (;;) {
  1752.       cnt = read (f, bufp, bsize - size);
  1753.       if (cnt < 0) goto perror;    /* error! */
  1754.       if (cnt == 0) break;    /* End of file */
  1755.       size += cnt;
  1756.       bufp += cnt;
  1757.       if (bsize == size) {    /* Buffer is full! */
  1758.         bsize *= 2;
  1759.         fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  1760.     bufp = fp->buf + size;    /* May have moved */
  1761.       }
  1762.     }
  1763.     fp->length = size;
  1764.   } else {
  1765.     /* Read a file whose size we can determine in advance.
  1766.        For the sake of VMS, st_size is just an upper bound.  */
  1767.     long i;
  1768.     fp->length = 0;
  1769.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  1770.  
  1771.     while (st_size > 0) {
  1772.       i = read (f, fp->buf + fp->length, st_size);
  1773.       if (i <= 0) {
  1774.         if (i == 0) break;
  1775.     goto perror;
  1776.       }
  1777.       fp->length += i;
  1778.       st_size -= i;
  1779.     }
  1780.   }
  1781.   fp->bufp = fp->buf;
  1782.   fp->if_stack = if_stack;
  1783.  
  1784.   /* Make sure data ends with a newline.  And put a null after it.  */
  1785.  
  1786.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  1787.       /* Backslash-newline at end is not good enough.  */
  1788.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  1789.     fp->buf[fp->length++] = '\n';
  1790.     missing_newline = 1;
  1791.   }
  1792.   fp->buf[fp->length] = '\0';
  1793.  
  1794.   /* Unless inhibited, convert trigraphs in the input.  */
  1795.  
  1796.   if (!no_trigraphs)
  1797.     trigraph_pcp (fp);
  1798.  
  1799.   /* Now that we know the input file is valid, open the output.  */
  1800.  
  1801.   if (!out_fname || !strcmp (out_fname, ""))
  1802.     out_fname = "stdout";
  1803.   else if (! freopen (out_fname, "w", stdout))
  1804.     pfatal_with_name (out_fname);
  1805.  
  1806.   output_line_command (fp, &outbuf, 0, same_file);
  1807.  
  1808.   /* Scan the -include files before the main input.  */
  1809.  
  1810.   for (i = 1; i < argc; i++)
  1811.     if (pend_includes[i]) {
  1812.       int fd = open (pend_includes[i], O_RDONLY, 0666);
  1813.       if (fd < 0) {
  1814.     perror_with_name (pend_includes[i]);
  1815.     return FAILURE_EXIT_CODE;
  1816.       }
  1817.       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
  1818.     }
  1819.  
  1820.   /* Scan the input, processing macros and directives.  */
  1821.  
  1822.   rescan (&outbuf, 0);
  1823.  
  1824.   if (pedantic && missing_newline)
  1825.     pedwarn ("file does not end in newline");
  1826.  
  1827.   /* Now we have processed the entire input
  1828.      Write whichever kind of output has been requested.  */
  1829.  
  1830.   if (dump_macros == dump_only)
  1831.     dump_all_macros ();
  1832.   else if (! inhibit_output) {
  1833.     write_output ();
  1834.   }
  1835.  
  1836.   if (print_deps) {
  1837.     /* Don't actually write the deps file if compilation has failed.  */
  1838.     if (errors == 0) {
  1839.       if (deps_file && ! (deps_stream = fopen (deps_file, "a")))
  1840.     pfatal_with_name (deps_file);
  1841.       fputs (deps_buffer, deps_stream);
  1842.       putc ('\n', deps_stream);
  1843.       if (deps_file) {
  1844.     if (ferror (deps_stream) || fclose (deps_stream) != 0)
  1845.       fatal ("I/O error on output");
  1846.       }
  1847.     }
  1848.   }
  1849.  
  1850.   if (ferror (stdout) || fclose (stdout) != 0)
  1851.     fatal ("I/O error on output");
  1852.  
  1853.   if (errors)
  1854.     exit (FAILURE_EXIT_CODE);
  1855.   exit (SUCCESS_EXIT_CODE);
  1856.  
  1857.  perror:
  1858.   pfatal_with_name (in_fname);
  1859.   return 0;
  1860. }
  1861.  
  1862. /* Given a colon-separated list of file names PATH,
  1863.    add all the names to the search path for include files.  */
  1864.  
  1865. static void
  1866. path_include (path)
  1867.      char *path;
  1868. {
  1869.   char *p;
  1870.  
  1871.   p = path;
  1872.  
  1873.   if (*p)
  1874.     while (1) {
  1875.       char *q = p;
  1876.       char *name;
  1877.       struct file_name_list *dirtmp;
  1878.  
  1879.       /* Find the end of this name.  */
  1880.       while (*q != 0 && *q != PATH_SEPARATOR) q++;
  1881.       if (p == q) {
  1882.     /* An empty name in the path stands for the current directory.  */
  1883.     name = (char *) xmalloc (2);
  1884.     name[0] = '.';
  1885.     name[1] = 0;
  1886.       } else {
  1887.     /* Otherwise use the directory that is named.  */
  1888.     name = (char *) xmalloc (q - p + 1);
  1889.     bcopy (p, name, q - p);
  1890.     name[q - p] = 0;
  1891.       }
  1892.  
  1893.       dirtmp = (struct file_name_list *)
  1894.     xmalloc (sizeof (struct file_name_list));
  1895.       dirtmp->next = 0;        /* New one goes on the end */
  1896.       dirtmp->control_macro = 0;
  1897.       dirtmp->fname = name;
  1898.       append_include_chain (dirtmp, dirtmp);
  1899.  
  1900.       /* Advance past this name.  */
  1901.       p = q;
  1902.       if (*p == 0)
  1903.     break;
  1904.       /* Skip the colon.  */
  1905.       p++;
  1906.     }
  1907. }
  1908.  
  1909. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  1910.    before main CCCP processing.  Name `pcp' is also in honor of the
  1911.    drugs the trigraph designers must have been on.
  1912.  
  1913.    Using an extra pass through the buffer takes a little extra time,
  1914.    but is infinitely less hairy than trying to handle trigraphs inside
  1915.    strings, etc. everywhere, and also makes sure that trigraphs are
  1916.    only translated in the top level of processing. */
  1917.  
  1918. static void
  1919. trigraph_pcp (buf)
  1920.      FILE_BUF *buf;
  1921. {
  1922.   register U_CHAR c, *fptr, *bptr, *sptr;
  1923.   int len;
  1924.  
  1925.   fptr = bptr = sptr = buf->buf;
  1926.   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  1927.     if (*++sptr != '?')
  1928.       continue;
  1929.     switch (*++sptr) {
  1930.       case '=':
  1931.       c = '#';
  1932.       break;
  1933.     case '(':
  1934.       c = '[';
  1935.       break;
  1936.     case '/':
  1937.       c = '\\';
  1938.       break;
  1939.     case ')':
  1940.       c = ']';
  1941.       break;
  1942.     case '\'':
  1943.       c = '^';
  1944.       break;
  1945.     case '<':
  1946.       c = '{';
  1947.       break;
  1948.     case '!':
  1949.       c = '|';
  1950.       break;
  1951.     case '>':
  1952.       c = '}';
  1953.       break;
  1954.     case '-':
  1955.       c  = '~';
  1956.       break;
  1957.     case '?':
  1958.       sptr--;
  1959.       continue;
  1960.     default:
  1961.       continue;
  1962.     }
  1963.     len = sptr - fptr - 2;
  1964.     if (bptr != fptr && len > 0)
  1965.       bcopy (fptr, bptr, len);    /* BSD doc says bcopy () works right
  1966.                    for overlapping strings.  In ANSI
  1967.                    C, this will be memmove (). */
  1968.     bptr += len;
  1969.     *bptr++ = c;
  1970.     fptr = ++sptr;
  1971.   }
  1972.   len = buf->length - (fptr - buf->buf);
  1973.   if (bptr != fptr && len > 0)
  1974.     bcopy (fptr, bptr, len);
  1975.   buf->length -= fptr - bptr;
  1976.   buf->buf[buf->length] = '\0';
  1977.   if (warn_trigraphs && fptr != bptr)
  1978.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  1979. }
  1980.  
  1981. /* Move all backslash-newline pairs out of embarrassing places.
  1982.    Exchange all such pairs following BP
  1983.    with any potentially-embarrassing characters that follow them.
  1984.    Potentially-embarrassing characters are / and *
  1985.    (because a backslash-newline inside a comment delimiter
  1986.    would cause it not to be recognized).  */
  1987.  
  1988. static void
  1989. newline_fix (bp)
  1990.      U_CHAR *bp;
  1991. {
  1992.   register U_CHAR *p = bp;
  1993.   register int count = 0;
  1994.  
  1995.   /* First count the backslash-newline pairs here.  */
  1996.  
  1997.   while (1) {
  1998.     if (p[0] == '\\') {
  1999.       if (p[1] == '\n')
  2000.     p += 2, count++;
  2001.       else if (p[1] == '\r' && p[2] == '\n')
  2002.     p += 3, count++;
  2003.       else
  2004.     break;
  2005.     } else
  2006.       break;
  2007.   }
  2008.  
  2009.   /* What follows the backslash-newlines is not embarrassing.  */
  2010.  
  2011.   if (count == 0 || (*p != '/' && *p != '*'))
  2012.     return;
  2013.  
  2014.   /* Copy all potentially embarrassing characters
  2015.      that follow the backslash-newline pairs
  2016.      down to where the pairs originally started.  */
  2017.  
  2018.   while (*p == '*' || *p == '/')
  2019.     *bp++ = *p++;
  2020.  
  2021.   /* Now write the same number of pairs after the embarrassing chars.  */
  2022.   while (count-- > 0) {
  2023.     *bp++ = '\\';
  2024.     *bp++ = '\n';
  2025.   }
  2026. }
  2027.  
  2028. /* Like newline_fix but for use within a directive-name.
  2029.    Move any backslash-newlines up past any following symbol constituents.  */
  2030.  
  2031. static void
  2032. name_newline_fix (bp)
  2033.      U_CHAR *bp;
  2034. {
  2035.   register U_CHAR *p = bp;
  2036.   register int count = 0;
  2037.  
  2038.   /* First count the backslash-newline pairs here.  */
  2039.   while (1) {
  2040.     if (p[0] == '\\') {
  2041.       if (p[1] == '\n')
  2042.     p += 2, count++;
  2043.       else if (p[1] == '\r' && p[2] == '\n')
  2044.     p += 3, count++;
  2045.       else
  2046.     break;
  2047.     } else
  2048.       break;
  2049.   }
  2050.  
  2051.   /* What follows the backslash-newlines is not embarrassing.  */
  2052.  
  2053.   if (count == 0 || !is_idchar[*p])
  2054.     return;
  2055.  
  2056.   /* Copy all potentially embarrassing characters
  2057.      that follow the backslash-newline pairs
  2058.      down to where the pairs originally started.  */
  2059.  
  2060.   while (is_idchar[*p])
  2061.     *bp++ = *p++;
  2062.  
  2063.   /* Now write the same number of pairs after the embarrassing chars.  */
  2064.   while (count-- > 0) {
  2065.     *bp++ = '\\';
  2066.     *bp++ = '\n';
  2067.   }
  2068. }
  2069.  
  2070. /* Look for lint commands in comments.
  2071.  
  2072.    When we come in here, ibp points into a comment.  Limit is as one expects.
  2073.    scan within the comment -- it should start, after lwsp, with a lint command.
  2074.    If so that command is returned as a (constant) string.
  2075.  
  2076.    Upon return, any arg will be pointed to with argstart and will be
  2077.    arglen long.  Note that we don't parse that arg since it will just
  2078.    be printed out again.
  2079. */
  2080.  
  2081. static char *
  2082. get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
  2083.      register U_CHAR *ibp;
  2084.      register U_CHAR *limit;
  2085.      U_CHAR **argstart;        /* point to command arg */
  2086.      int *arglen, *cmdlen;    /* how long they are */
  2087. {
  2088.   long linsize;
  2089.   register U_CHAR *numptr;    /* temp for arg parsing */
  2090.  
  2091.   *arglen = 0;
  2092.  
  2093.   SKIP_WHITE_SPACE (ibp);
  2094.  
  2095.   if (ibp >= limit) return NULL;
  2096.  
  2097.   linsize = limit - ibp;
  2098.   
  2099.   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
  2100.   if ((linsize >= 10) && !strncmp (ibp, "NOTREACHED", 10)) {
  2101.     *cmdlen = 10;
  2102.     return "NOTREACHED";
  2103.   }
  2104.   if ((linsize >= 8) && !strncmp (ibp, "ARGSUSED", 8)) {
  2105.     *cmdlen = 8;
  2106.     return "ARGSUSED";
  2107.   }
  2108.   if ((linsize >= 11) && !strncmp (ibp, "LINTLIBRARY", 11)) {
  2109.     *cmdlen = 11;
  2110.     return "LINTLIBRARY";
  2111.   }
  2112.   if ((linsize >= 7) && !strncmp (ibp, "VARARGS", 7)) {
  2113.     *cmdlen = 7;
  2114.     ibp += 7; linsize -= 7;
  2115.     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
  2116.  
  2117.     /* OK, read a number */
  2118.     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
  2119.      numptr++);
  2120.     *arglen = numptr - *argstart;
  2121.     return "VARARGS";
  2122.   }
  2123.   return NULL;
  2124. }
  2125.  
  2126. /*
  2127.  * The main loop of the program.
  2128.  *
  2129.  * Read characters from the input stack, transferring them to the
  2130.  * output buffer OP.
  2131.  *
  2132.  * Macros are expanded and push levels on the input stack.
  2133.  * At the end of such a level it is popped off and we keep reading.
  2134.  * At the end of any other kind of level, we return.
  2135.  * #-directives are handled, except within macros.
  2136.  *
  2137.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  2138.  * and insert them when appropriate.  This is set while scanning macro
  2139.  * arguments before substitution.  It is zero when scanning for final output.
  2140.  *   There are three types of Newline markers:
  2141.  *   * Newline -  follows a macro name that was not expanded
  2142.  *     because it appeared inside an expansion of the same macro.
  2143.  *     This marker prevents future expansion of that identifier.
  2144.  *     When the input is rescanned into the final output, these are deleted.
  2145.  *     These are also deleted by ## concatenation.
  2146.  *   * Newline Space (or Newline and any other whitespace character)
  2147.  *     stands for a place that tokens must be separated or whitespace
  2148.  *     is otherwise desirable, but where the ANSI standard specifies there
  2149.  *     is no whitespace.  This marker turns into a Space (or whichever other
  2150.  *     whitespace char appears in the marker) in the final output,
  2151.  *     but it turns into nothing in an argument that is stringified with #.
  2152.  *     Such stringified arguments are the only place where the ANSI standard
  2153.  *     specifies with precision that whitespace may not appear.
  2154.  *
  2155.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  2156.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  2157.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  2158.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  2159.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  2160.  */
  2161.  
  2162. static void
  2163. rescan (op, output_marks)
  2164.      FILE_BUF *op;
  2165.      int output_marks;
  2166. {
  2167.   /* Character being scanned in main loop.  */
  2168.   register U_CHAR c;
  2169.  
  2170.   /* Length of pending accumulated identifier.  */
  2171.   register int ident_length = 0;
  2172.  
  2173.   /* Hash code of pending accumulated identifier.  */
  2174.   register int hash = 0;
  2175.  
  2176.   /* Current input level (&instack[indepth]).  */
  2177.   FILE_BUF *ip;
  2178.  
  2179.   /* Pointer for scanning input.  */
  2180.   register U_CHAR *ibp;
  2181.  
  2182.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  2183.   register U_CHAR *limit;
  2184.  
  2185.   /* Pointer for storing output.  */
  2186.   register U_CHAR *obp;
  2187.  
  2188.   /* REDO_CHAR is nonzero if we are processing an identifier
  2189.      after backing up over the terminating character.
  2190.      Sometimes we process an identifier without backing up over
  2191.      the terminating character, if the terminating character
  2192.      is not special.  Backing up is done so that the terminating character
  2193.      will be dispatched on again once the identifier is dealt with.  */
  2194.   int redo_char = 0;
  2195.  
  2196.   /* 1 if within an identifier inside of which a concatenation
  2197.      marker (Newline -) has been seen.  */
  2198.   int concatenated = 0;
  2199.  
  2200.   /* While scanning a comment or a string constant,
  2201.      this records the line it started on, for error messages.  */
  2202.   int start_line;
  2203.  
  2204.   /* Line where a newline was first seen in a string constant.  */
  2205.   int multiline_string_line = 0;
  2206.  
  2207.   /* Record position of last `real' newline.  */
  2208.   U_CHAR *beg_of_line;
  2209.  
  2210. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  2211.  
  2212. #define POPMACRO \
  2213. do { ip->macro->type = T_MACRO;        \
  2214.      if (ip->free_ptr) free (ip->free_ptr);    \
  2215.      --indepth; } while (0)
  2216.  
  2217. /* Reload `rescan's local variables that describe the current
  2218.    level of the input stack.  */
  2219.  
  2220. #define RECACHE  \
  2221. do { ip = &instack[indepth];        \
  2222.      ibp = ip->bufp;            \
  2223.      limit = ip->buf + ip->length;    \
  2224.      op->bufp = obp;            \
  2225.      check_expand (op, limit - ibp);    \
  2226.      beg_of_line = 0;            \
  2227.      obp = op->bufp; } while (0)
  2228.  
  2229.   if (no_output && instack[indepth].fname != 0)
  2230.     skip_if_group (&instack[indepth], 1);
  2231.  
  2232.   obp = op->bufp;
  2233.   RECACHE;
  2234.  
  2235.   beg_of_line = ibp;
  2236.  
  2237.   /* Our caller must always put a null after the end of
  2238.      the input at each input stack level.  */
  2239.   if (*limit != 0)
  2240.     abort ();
  2241.  
  2242.   while (1) {
  2243.     c = *ibp++;
  2244.     *obp++ = c;
  2245.  
  2246.     switch (c) {
  2247.     case '\\':
  2248.       if (ibp >= limit)
  2249.     break;
  2250.       if (*ibp == '\n') {
  2251.     /* Always merge lines ending with backslash-newline,
  2252.        even in middle of identifier.  */
  2253.     ++ibp;
  2254.     ++ip->lineno;
  2255.     --obp;        /* remove backslash from obuf */
  2256.     break;
  2257.       }
  2258.       /* Otherwise, backslash suppresses specialness of following char,
  2259.      so copy it here to prevent the switch from seeing it.
  2260.      But first get any pending identifier processed.  */
  2261.       if (ident_length > 0)
  2262.     goto specialchar;
  2263.       *obp++ = *ibp++;
  2264.       break;
  2265.  
  2266.     case '#':
  2267.       if (assertions_flag) {
  2268.     /* Copy #foo (bar lose) without macro expansion.  */
  2269.     SKIP_WHITE_SPACE (ibp);
  2270.     while (is_idchar[*ibp])
  2271.       *obp++ = *ibp++;
  2272.     SKIP_WHITE_SPACE (ibp);
  2273.     if (*ibp == '(') {
  2274.       ip->bufp = ibp;
  2275.       skip_paren_group (ip);
  2276.       bcopy (ibp, obp, ip->bufp - ibp);
  2277.       obp += ip->bufp - ibp;
  2278.       ibp = ip->bufp;
  2279.     }
  2280.       }
  2281.  
  2282.       /* If this is expanding a macro definition, don't recognize
  2283.      preprocessor directives.  */
  2284.       if (ip->macro != 0)
  2285.     goto randomchar;
  2286.       /* If this is expand_into_temp_buffer, recognize them
  2287.      only after an actual newline at this level,
  2288.      not at the beginning of the input level.  */
  2289.       if (ip->fname == 0 && beg_of_line == ip->buf)
  2290.     goto randomchar;
  2291.       if (ident_length)
  2292.     goto specialchar;
  2293.  
  2294.       
  2295.       /* # keyword: a # must be first nonblank char on the line */
  2296.       if (beg_of_line == 0)
  2297.     goto randomchar;
  2298.       {
  2299.     U_CHAR *bp;
  2300.  
  2301.     /* Scan from start of line, skipping whitespace, comments
  2302.        and backslash-newlines, and see if we reach this #.
  2303.        If not, this # is not special.  */
  2304.     bp = beg_of_line;
  2305.     /* If -traditional, require # to be at beginning of line.  */
  2306.     if (!traditional)
  2307.       while (1) {
  2308.         if (is_hor_space[*bp])
  2309.           bp++;
  2310.         else if (*bp == '\\' && bp[1] == '\n')
  2311.           bp += 2;
  2312.         else if (*bp == '/' && bp[1] == '*') {
  2313.           bp += 2;
  2314.           while (!(*bp == '*' && bp[1] == '/'))
  2315.         bp++;
  2316.           bp += 2;
  2317.         }
  2318.         else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  2319.           bp += 2;
  2320.           while (*bp++ != '\n') ;
  2321.         }
  2322.         else break;
  2323.       }
  2324.     if (bp + 1 != ibp)
  2325.       goto randomchar;
  2326.       }
  2327.  
  2328.       /* This # can start a directive.  */
  2329.  
  2330.       --obp;        /* Don't copy the '#' */
  2331.  
  2332.       ip->bufp = ibp;
  2333.       op->bufp = obp;
  2334.       if (! handle_directive (ip, op)) {
  2335. #ifdef USE_C_ALLOCA
  2336.     alloca (0);
  2337. #endif
  2338.     /* Not a known directive: treat it as ordinary text.
  2339.        IP, OP, IBP, etc. have not been changed.  */
  2340.     if (no_output && instack[indepth].fname) {
  2341.       /* If not generating expanded output,
  2342.          what we do with ordinary text is skip it.
  2343.          Discard everything until next # directive.  */
  2344.       skip_if_group (&instack[indepth], 1);
  2345.       RECACHE;
  2346.       beg_of_line = ibp;
  2347.       break;
  2348.     }
  2349.     ++obp;        /* Copy the '#' after all */
  2350.     goto randomchar;
  2351.       }
  2352. #ifdef USE_C_ALLOCA
  2353.       alloca (0);
  2354. #endif
  2355.       /* A # directive has been successfully processed.  */
  2356.       /* If not generating expanded output, ignore everything until
  2357.      next # directive.  */
  2358.       if (no_output && instack[indepth].fname)
  2359.     skip_if_group (&instack[indepth], 1);
  2360.       obp = op->bufp;
  2361.       RECACHE;
  2362.       beg_of_line = ibp;
  2363.       break;
  2364.  
  2365.     case '\"':            /* skip quoted string */
  2366.     case '\'':
  2367.       /* A single quoted string is treated like a double -- some
  2368.      programs (e.g., troff) are perverse this way */
  2369.  
  2370.       if (ident_length)
  2371.     goto specialchar;
  2372.  
  2373.       start_line = ip->lineno;
  2374.  
  2375.       /* Skip ahead to a matching quote.  */
  2376.  
  2377.       while (1) {
  2378.     if (ibp >= limit) {
  2379.       if (ip->macro != 0) {
  2380.         /* try harder: this string crosses a macro expansion boundary.
  2381.            This can happen naturally if -traditional.
  2382.            Otherwise, only -D can make a macro with an unmatched quote.  */
  2383.         POPMACRO;
  2384.         RECACHE;
  2385.         continue;
  2386.       }
  2387.       if (!traditional) {
  2388.         error_with_line (line_for_error (start_line),
  2389.                  "unterminated string or character constant");
  2390.         error_with_line (multiline_string_line,
  2391.                  "possible real start of unterminated constant");
  2392.         multiline_string_line = 0;
  2393.       }
  2394.       break;
  2395.     }
  2396.     *obp++ = *ibp;
  2397.     switch (*ibp++) {
  2398.     case '\n':
  2399.       ++ip->lineno;
  2400.       ++op->lineno;
  2401.       /* Traditionally, end of line ends a string constant with no error.
  2402.          So exit the loop and record the new line.  */
  2403.       if (traditional) {
  2404.         beg_of_line = ibp;
  2405.         goto while2end;
  2406.       }
  2407.       if (pedantic || c == '\'') {
  2408.         error_with_line (line_for_error (start_line),
  2409.                  "unterminated string or character constant");
  2410.         goto while2end;
  2411.       }
  2412.       if (multiline_string_line == 0)
  2413.         multiline_string_line = ip->lineno - 1;
  2414.       break;
  2415.  
  2416.     case '\\':
  2417.       if (ibp >= limit)
  2418.         break;
  2419.       if (*ibp == '\n') {
  2420.         /* Backslash newline is replaced by nothing at all,
  2421.            but keep the line counts correct.  */
  2422.         --obp;
  2423.         ++ibp;
  2424.         ++ip->lineno;
  2425.       } else {
  2426.         /* ANSI stupidly requires that in \\ the second \
  2427.            is *not* prevented from combining with a newline.  */
  2428.         while (*ibp == '\\' && ibp[1] == '\n') {
  2429.           ibp += 2;
  2430.           ++ip->lineno;
  2431.         }
  2432.         *obp++ = *ibp++;
  2433.       }
  2434.       break;
  2435.  
  2436.     case '\"':
  2437.     case '\'':
  2438.       if (ibp[-1] == c)
  2439.         goto while2end;
  2440.       break;
  2441.     }
  2442.       }
  2443.     while2end:
  2444.       break;
  2445.  
  2446.     case '/':
  2447.       if (*ibp == '\\' && ibp[1] == '\n')
  2448.     newline_fix (ibp);
  2449.  
  2450.       if (*ibp != '*'
  2451.       && !(cplusplus_comments && *ibp == '/'))
  2452.     goto randomchar;
  2453.       if (ip->macro != 0)
  2454.     goto randomchar;
  2455.       if (ident_length)
  2456.     goto specialchar;
  2457.  
  2458.       if (*ibp == '/') {
  2459.     /* C++ style comment... */
  2460.     start_line = ip->lineno;
  2461.  
  2462.     --ibp;            /* Back over the slash */
  2463.     --obp;
  2464.  
  2465.     /* Comments are equivalent to spaces. */
  2466.     if (! put_out_comments)
  2467.       *obp++ = ' ';
  2468.     else {
  2469.       /* must fake up a comment here */
  2470.       *obp++ = '/';
  2471.       *obp++ = '/';
  2472.     }
  2473.     {
  2474.       U_CHAR *before_bp = ibp+2;
  2475.  
  2476.       while (ibp < limit) {
  2477.         if (*ibp++ == '\n') {
  2478.           ibp--;
  2479.           if (put_out_comments) {
  2480.         bcopy (before_bp, obp, ibp - before_bp);
  2481.         obp += ibp - before_bp;
  2482.           }
  2483.           break;
  2484.         }
  2485.       }
  2486.       break;
  2487.     }
  2488.       }
  2489.  
  2490.       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
  2491.  
  2492.       start_line = ip->lineno;
  2493.  
  2494.       ++ibp;            /* Skip the star. */
  2495.  
  2496.       /* If this cpp is for lint, we peek inside the comments: */
  2497.       if (lint) {
  2498.     U_CHAR *argbp;
  2499.     int cmdlen, arglen;
  2500.     char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
  2501.  
  2502.     if (lintcmd != NULL) {
  2503.       /* I believe it is always safe to emit this newline: */
  2504.       obp[-1] = '\n';
  2505.       bcopy ("#pragma lint ", obp, 13);
  2506.       obp += 13;
  2507.       bcopy (lintcmd, obp, cmdlen);
  2508.       obp += cmdlen;
  2509.  
  2510.       if (arglen != 0) {
  2511.         *(obp++) = ' ';
  2512.         bcopy (argbp, obp, arglen);
  2513.         obp += arglen;
  2514.       }
  2515.  
  2516.       /* OK, now bring us back to the state we were in before we entered
  2517.          this branch.  We need #line b/c the newline for the pragma
  2518.          could fuck things up. */
  2519.       output_line_command (ip, op, 0, same_file);
  2520.       *(obp++) = ' ';    /* just in case, if comments are copied thru */
  2521.       *(obp++) = '/';
  2522.     }
  2523.       }
  2524.  
  2525.       /* Comments are equivalent to spaces.
  2526.      Note that we already output the slash; we might not want it.
  2527.      For -traditional, a comment is equivalent to nothing.  */
  2528.       if (! put_out_comments) {
  2529.     if (traditional)
  2530.       obp--;
  2531.     else
  2532.       obp[-1] = ' ';
  2533.       }
  2534.       else
  2535.     *obp++ = '*';
  2536.  
  2537.       {
  2538.     U_CHAR *before_bp = ibp;
  2539.  
  2540.     while (ibp < limit) {
  2541.       switch (*ibp++) {
  2542.       case '/':
  2543.         if (warn_comments && ibp < limit && *ibp == '*')
  2544.           warning("`/*' within comment");
  2545.         break;
  2546.       case '*':
  2547.         if (*ibp == '\\' && ibp[1] == '\n')
  2548.           newline_fix (ibp);
  2549.         if (ibp >= limit || *ibp == '/')
  2550.           goto comment_end;
  2551.         break;
  2552.       case '\n':
  2553.         ++ip->lineno;
  2554.         /* Copy the newline into the output buffer, in order to
  2555.            avoid the pain of a #line every time a multiline comment
  2556.            is seen.  */
  2557.         if (!put_out_comments)
  2558.           *obp++ = '\n';
  2559.         ++op->lineno;
  2560.       }
  2561.     }
  2562.       comment_end:
  2563.  
  2564.     if (ibp >= limit)
  2565.       error_with_line (line_for_error (start_line),
  2566.                "unterminated comment");
  2567.     else {
  2568.       ibp++;
  2569.       if (put_out_comments) {
  2570.         bcopy (before_bp, obp, ibp - before_bp);
  2571.         obp += ibp - before_bp;
  2572.       }
  2573.     }
  2574.       }
  2575.       break;
  2576.  
  2577.     case '$':
  2578.       if (!dollars_in_ident)
  2579.     goto randomchar;
  2580.       goto letter;
  2581.  
  2582.     case '0': case '1': case '2': case '3': case '4':
  2583.     case '5': case '6': case '7': case '8': case '9':
  2584.       /* If digit is not part of identifier, it starts a number,
  2585.      which means that following letters are not an identifier.
  2586.      "0x5" does not refer to an identifier "x5".
  2587.      So copy all alphanumerics that follow without accumulating
  2588.      as an identifier.  Periods also, for sake of "3.e7".  */
  2589.  
  2590.       if (ident_length == 0) {
  2591.     while (ibp < limit) {
  2592.       while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  2593.         ++ip->lineno;
  2594.         ibp += 2;
  2595.       }
  2596.       c = *ibp++;
  2597.       /* ".." terminates a preprocessing number.  This is useless for C
  2598.          code but useful for preprocessing other things.  */
  2599.       if (!isalnum (c) && (c != '.' || *ibp == '.') && c != '_') {
  2600.         --ibp;
  2601.         break;
  2602.       }
  2603.       *obp++ = c;
  2604.       /* A sign can be part of a preprocessing number
  2605.          if it follows an e.  */
  2606.       if (c == 'e' || c == 'E') {
  2607.         while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  2608.           ++ip->lineno;
  2609.           ibp += 2;
  2610.         }
  2611.         if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
  2612.           *obp++ = *ibp++;
  2613.           /* But traditional C does not let the token go past the sign.  */
  2614.           if (traditional)
  2615.         break;
  2616.         }
  2617.       }
  2618.     }
  2619.     break;
  2620.       }
  2621.       /* fall through */
  2622.  
  2623.     case '_':
  2624.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  2625.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  2626.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  2627.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  2628.     case 'y': case 'z':
  2629.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  2630.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  2631.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  2632.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  2633.     case 'Y': case 'Z':
  2634.     letter:
  2635.       ident_length++;
  2636.       /* Compute step of hash function, to avoid a proc call on every token */
  2637.       hash = HASHSTEP (hash, c);
  2638.       break;
  2639.  
  2640.     case '\n':
  2641.       /* If reprocessing a macro expansion, newline is a special marker.  */
  2642.       if (ip->macro != 0) {
  2643.     /* Newline White is a "funny space" to separate tokens that are
  2644.        supposed to be separate but without space between.
  2645.        Here White means any whitespace character.
  2646.        Newline - marks a recursive macro use that is not
  2647.        supposed to be expandable.  */
  2648.  
  2649.     if (*ibp == '-') {
  2650.       /* Newline - inhibits expansion of preceding token.
  2651.          If expanding a macro arg, we keep the newline -.
  2652.          In final output, it is deleted.  */
  2653.       if (! concatenated) {
  2654.         ident_length = 0;
  2655.         hash = 0;
  2656.       }
  2657.       ibp++;
  2658.       if (!output_marks) {
  2659.         obp--;
  2660.       } else {
  2661.         /* If expanding a macro arg, keep the newline -.  */
  2662.         *obp++ = '-';
  2663.       }
  2664.     } else if (is_space[*ibp]) {
  2665.       /* Newline Space does not prevent expansion of preceding token
  2666.          so expand the preceding token and then come back.  */
  2667.       if (ident_length > 0)
  2668.         goto specialchar;
  2669.  
  2670.       /* If generating final output, newline space makes a space.  */
  2671.       if (!output_marks) {
  2672.         obp[-1] = *ibp++;
  2673.         /* And Newline Newline makes a newline, so count it.  */
  2674.         if (obp[-1] == '\n')
  2675.           op->lineno++;
  2676.       } else {
  2677.         /* If expanding a macro arg, keep the newline space.
  2678.            If the arg gets stringified, newline space makes nothing.  */
  2679.         *obp++ = *ibp++;
  2680.       }
  2681.     } else abort ();    /* Newline followed by something random?  */
  2682.     break;
  2683.       }
  2684.  
  2685.       /* If there is a pending identifier, handle it and come back here.  */
  2686.       if (ident_length > 0)
  2687.     goto specialchar;
  2688.  
  2689.       beg_of_line = ibp;
  2690.  
  2691.       /* Update the line counts and output a #line if necessary.  */
  2692.       ++ip->lineno;
  2693.       ++op->lineno;
  2694.       if (ip->lineno != op->lineno) {
  2695.     op->bufp = obp;
  2696.     output_line_command (ip, op, 1, same_file);
  2697.     check_expand (op, ip->length - (ip->bufp - ip->buf));
  2698.     obp = op->bufp;
  2699.       }
  2700.       break;
  2701.  
  2702.       /* Come here either after (1) a null character that is part of the input
  2703.      or (2) at the end of the input, because there is a null there.  */
  2704.     case 0:
  2705.       if (ibp <= limit)
  2706.     /* Our input really contains a null character.  */
  2707.     goto randomchar;
  2708.  
  2709.       /* At end of a macro-expansion level, pop it and read next level.  */
  2710.       if (ip->macro != 0) {
  2711.     obp--;
  2712.     ibp--;
  2713.     /* If traditional, and we have an identifier that ends here,
  2714.        process it now, so we get the right error for recursion.  */
  2715.     if (traditional && ident_length
  2716.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  2717.       redo_char = 1;
  2718.       goto randomchar;
  2719.     }
  2720.     POPMACRO;
  2721.     RECACHE;
  2722.     break;
  2723.       }
  2724.  
  2725.       /* If we don't have a pending identifier,
  2726.      return at end of input.  */
  2727.       if (ident_length == 0) {
  2728.     obp--;
  2729.     ibp--;
  2730.     op->bufp = obp;
  2731.     ip->bufp = ibp;
  2732.     goto ending;
  2733.       }
  2734.  
  2735.       /* If we do have a pending identifier, just consider this null
  2736.      a special character and arrange to dispatch on it again.
  2737.      The second time, IDENT_LENGTH will be zero so we will return.  */
  2738.  
  2739.       /* Fall through */
  2740.  
  2741. specialchar:
  2742.  
  2743.       /* Handle the case of a character such as /, ', " or null
  2744.      seen following an identifier.  Back over it so that
  2745.      after the identifier is processed the special char
  2746.      will be dispatched on again.  */
  2747.  
  2748.       ibp--;
  2749.       obp--;
  2750.       redo_char = 1;
  2751.  
  2752.     default:
  2753.  
  2754. randomchar:
  2755.  
  2756.       if (ident_length > 0) {
  2757.     register HASHNODE *hp;
  2758.  
  2759.     /* We have just seen an identifier end.  If it's a macro, expand it.
  2760.  
  2761.        IDENT_LENGTH is the length of the identifier
  2762.        and HASH is its hash code.
  2763.  
  2764.        The identifier has already been copied to the output,
  2765.        so if it is a macro we must remove it.
  2766.  
  2767.        If REDO_CHAR is 0, the char that terminated the identifier
  2768.        has been skipped in the output and the input.
  2769.        OBP-IDENT_LENGTH-1 points to the identifier.
  2770.        If the identifier is a macro, we must back over the terminator.
  2771.  
  2772.        If REDO_CHAR is 1, the terminating char has already been
  2773.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  2774.  
  2775.     if (!pcp_outfile || pcp_inside_if) {
  2776. startagain:
  2777.       for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  2778.            hp = hp->next) {
  2779.         
  2780.         if (hp->length == ident_length) {
  2781.           int obufp_before_macroname;
  2782.           int op_lineno_before_macroname;
  2783.           register int i = ident_length;
  2784.           register U_CHAR *p = hp->name;
  2785.           register U_CHAR *q = obp - i;
  2786.           int disabled;
  2787.           
  2788.           if (! redo_char)
  2789.         q--;
  2790.           
  2791.           do {        /* All this to avoid a strncmp () */
  2792.         if (*p++ != *q++)
  2793.           goto hashcollision;
  2794.           } while (--i);
  2795.           
  2796.           /* We found a use of a macro name.
  2797.          see if the context shows it is a macro call.  */
  2798.           
  2799.           /* Back up over terminating character if not already done.  */
  2800.           if (! redo_char) {
  2801.         ibp--;
  2802.         obp--;
  2803.           }
  2804.           
  2805.           /* Save this as a displacement from the beginning of the output
  2806.          buffer.  We can not save this as a position in the output
  2807.          buffer, because it may get realloc'ed by RECACHE.  */
  2808.           obufp_before_macroname = (obp - op->buf) - ident_length;
  2809.           op_lineno_before_macroname = op->lineno;
  2810.           
  2811.           if (hp->type == T_PCSTRING) {
  2812.         pcstring_used (hp); /* Mark the definition of this key
  2813.                        as needed, ensuring that it
  2814.                        will be output.  */
  2815.         break;        /* Exit loop, since the key cannot have a
  2816.                    definition any longer.  */
  2817.           }
  2818.  
  2819.           /* Record whether the macro is disabled.  */
  2820.           disabled = hp->type == T_DISABLED;
  2821.           
  2822.           /* This looks like a macro ref, but if the macro was disabled,
  2823.          just copy its name and put in a marker if requested.  */
  2824.           
  2825.           if (disabled) {
  2826. #if 0
  2827.         /* This error check caught useful cases such as
  2828.            #define foo(x,y) bar(x(y,0), y)
  2829.            foo(foo, baz)  */
  2830.         if (traditional)
  2831.           error ("recursive use of macro `%s'", hp->name);
  2832. #endif
  2833.         
  2834.         if (output_marks) {
  2835.           check_expand (op, limit - ibp + 2);
  2836.           *obp++ = '\n';
  2837.           *obp++ = '-';
  2838.         }
  2839.         break;
  2840.           }
  2841.           
  2842.           /* If macro wants an arglist, verify that a '(' follows.
  2843.          first skip all whitespace, copying it to the output
  2844.          after the macro name.  Then, if there is no '(',
  2845.          decide this is not a macro call and leave things that way.  */
  2846.           if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  2847.           && hp->value.defn->nargs >= 0)
  2848.         {
  2849.           U_CHAR *old_ibp = ibp;
  2850.           U_CHAR *old_obp = obp;
  2851.           int old_iln = ip->lineno;
  2852.           int old_oln = op->lineno;
  2853.           
  2854.           while (1) {
  2855.             /* Scan forward over whitespace, copying it to the output.  */
  2856.             if (ibp == limit && ip->macro != 0) {
  2857.               POPMACRO;
  2858.               RECACHE;
  2859.               old_ibp = ibp;
  2860.               old_obp = obp;
  2861.               old_iln = ip->lineno;
  2862.               old_oln = op->lineno;
  2863.             }
  2864.             /* A comment: copy it unchanged or discard it.  */
  2865.             else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  2866.               if (put_out_comments) {
  2867.             *obp++ = '/';
  2868.             *obp++ = '*';
  2869.               } else if (! traditional) {
  2870.             *obp++ = ' ';
  2871.               }
  2872.               ibp += 2;
  2873.               while (ibp + 1 != limit
  2874.                  && !(ibp[0] == '*' && ibp[1] == '/')) {
  2875.             /* We need not worry about newline-marks,
  2876.                since they are never found in comments.  */
  2877.             if (*ibp == '\n') {
  2878.               /* Newline in a file.  Count it.  */
  2879.               ++ip->lineno;
  2880.               ++op->lineno;
  2881.             }
  2882.             if (put_out_comments)
  2883.               *obp++ = *ibp++;
  2884.             else
  2885.               ibp++;
  2886.               }
  2887.               ibp += 2;
  2888.               if (put_out_comments) {
  2889.             *obp++ = '*';
  2890.             *obp++ = '/';
  2891.               }
  2892.             }
  2893.             else if (is_space[*ibp]) {
  2894.               *obp++ = *ibp++;
  2895.               if (ibp[-1] == '\n') {
  2896.             if (ip->macro == 0) {
  2897.               /* Newline in a file.  Count it.  */
  2898.               ++ip->lineno;
  2899.               ++op->lineno;
  2900.             } else if (!output_marks) {
  2901.               /* A newline mark, and we don't want marks
  2902.                  in the output.  If it is newline-hyphen,
  2903.                  discard it entirely.  Otherwise, it is
  2904.                  newline-whitechar, so keep the whitechar.  */
  2905.               obp--;
  2906.               if (*ibp == '-')
  2907.                 ibp++;
  2908.               else {
  2909.                 if (*ibp == '\n')
  2910.                   ++op->lineno;
  2911.                 *obp++ = *ibp++;
  2912.               }
  2913.             } else {
  2914.               /* A newline mark; copy both chars to the output.  */
  2915.               *obp++ = *ibp++;
  2916.             }
  2917.               }
  2918.             }
  2919.             else break;
  2920.           }
  2921.           if (*ibp != '(') {
  2922.             /* It isn't a macro call.
  2923.                Put back the space that we just skipped.  */
  2924.             ibp = old_ibp;
  2925.             obp = old_obp;
  2926.             ip->lineno = old_iln;
  2927.             op->lineno = old_oln;
  2928.             /* Exit the for loop.  */
  2929.             break;
  2930.           }
  2931.         }
  2932.           
  2933.           /* This is now known to be a macro call.
  2934.          Discard the macro name from the output,
  2935.          along with any following whitespace just copied.  */
  2936.           obp = op->buf + obufp_before_macroname;
  2937.           op->lineno = op_lineno_before_macroname;
  2938.           
  2939.           /* Expand the macro, reading arguments as needed,
  2940.          and push the expansion on the input stack.  */
  2941.           ip->bufp = ibp;
  2942.           op->bufp = obp;
  2943.           macroexpand (hp, op);
  2944.           
  2945.           /* Reexamine input stack, since macroexpand has pushed
  2946.          a new level on it.  */
  2947.           obp = op->bufp;
  2948.           RECACHE;
  2949.           break;
  2950.         }
  2951. hashcollision:
  2952.         ;
  2953.       }            /* End hash-table-search loop */
  2954.     }
  2955.     ident_length = hash = 0; /* Stop collecting identifier */
  2956.     redo_char = 0;
  2957.     concatenated = 0;
  2958.       }                /* End if (ident_length > 0) */
  2959.     }                /* End switch */
  2960.   }                /* End per-char loop */
  2961.  
  2962.   /* Come here to return -- but first give an error message
  2963.      if there was an unterminated successful conditional.  */
  2964.  ending:
  2965.   if (if_stack != ip->if_stack) {
  2966.     char *str;
  2967.     switch (if_stack->type) {
  2968.     case T_IF:
  2969.       str = "if";
  2970.       break;
  2971.     case T_IFDEF:
  2972.       str = "ifdef";
  2973.       break;
  2974.     case T_IFNDEF:
  2975.       str = "ifndef";
  2976.       break;
  2977.     case T_ELSE:
  2978.       str = "else";
  2979.       break;
  2980.     case T_ELIF:
  2981.       str = "elif";
  2982.       break;
  2983.     }
  2984.     error_with_line (line_for_error (if_stack->lineno),
  2985.              "unterminated `#%s' conditional", str);
  2986.   }
  2987.   if_stack = ip->if_stack;
  2988. }
  2989.  
  2990. /*
  2991.  * Rescan a string into a temporary buffer and return the result
  2992.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  2993.  *
  2994.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  2995.  * and insert such markers when appropriate.  See `rescan' for details.
  2996.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  2997.  * before substitution; it is 0 for other uses.
  2998.  */
  2999. static FILE_BUF
  3000. expand_to_temp_buffer (buf, limit, output_marks, assertions)
  3001.      U_CHAR *buf, *limit;
  3002.      int output_marks, assertions;
  3003. {
  3004.   register FILE_BUF *ip;
  3005.   FILE_BUF obuf;
  3006.   int length = limit - buf;
  3007.   U_CHAR *buf1;
  3008.   int odepth = indepth;
  3009.   int save_assertions_flag = assertions_flag;
  3010.  
  3011.   assertions_flag = assertions;
  3012.  
  3013.   if (length < 0)
  3014.     abort ();
  3015.  
  3016.   /* Set up the input on the input stack.  */
  3017.  
  3018.   buf1 = (U_CHAR *) alloca (length + 1);
  3019.   {
  3020.     register U_CHAR *p1 = buf;
  3021.     register U_CHAR *p2 = buf1;
  3022.  
  3023.     while (p1 != limit)
  3024.       *p2++ = *p1++;
  3025.   }
  3026.   buf1[length] = 0;
  3027.  
  3028.   /* Set up to receive the output.  */
  3029.  
  3030.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  3031.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  3032.   obuf.fname = 0;
  3033.   obuf.macro = 0;
  3034.   obuf.free_ptr = 0;
  3035.  
  3036.   CHECK_DEPTH ({return obuf;});
  3037.  
  3038.   ++indepth;
  3039.  
  3040.   ip = &instack[indepth];
  3041.   ip->fname = 0;
  3042.   ip->nominal_fname = 0;
  3043.   ip->system_header_p = 0;
  3044.   ip->macro = 0;
  3045.   ip->free_ptr = 0;
  3046.   ip->length = length;
  3047.   ip->buf = ip->bufp = buf1;
  3048.   ip->if_stack = if_stack;
  3049.  
  3050.   ip->lineno = obuf.lineno = 1;
  3051.  
  3052.   /* Scan the input, create the output.  */
  3053.   rescan (&obuf, output_marks);
  3054.  
  3055.   /* Pop input stack to original state.  */
  3056.   --indepth;
  3057.  
  3058.   if (indepth != odepth)
  3059.     abort ();
  3060.  
  3061.   /* Record the output.  */
  3062.   obuf.length = obuf.bufp - obuf.buf;
  3063.  
  3064.   assertions_flag = save_assertions_flag;
  3065.   return obuf;
  3066. }
  3067.  
  3068. /*
  3069.  * Process a # directive.  Expects IP->bufp to point after the '#', as in
  3070.  * `#define foo bar'.  Passes to the command handler
  3071.  * (do_define, do_include, etc.): the addresses of the 1st and
  3072.  * last chars of the command (starting immediately after the #
  3073.  * keyword), plus op and the keyword table pointer.  If the command
  3074.  * contains comments it is copied into a temporary buffer sans comments
  3075.  * and the temporary buffer is passed to the command handler instead.
  3076.  * Likewise for backslash-newlines.
  3077.  *
  3078.  * Returns nonzero if this was a known # directive.
  3079.  * Otherwise, returns zero, without advancing the input pointer.
  3080.  */
  3081.  
  3082. static int
  3083. handle_directive (ip, op)
  3084.      FILE_BUF *ip, *op;
  3085. {
  3086.   register U_CHAR *bp, *cp;
  3087.   register struct directive *kt;
  3088.   register int ident_length;
  3089.   U_CHAR *resume_p;
  3090.  
  3091.   /* Nonzero means we must copy the entire command
  3092.      to get rid of comments or backslash-newlines.  */
  3093.   int copy_command = 0;
  3094.  
  3095.   U_CHAR *ident, *after_ident;
  3096.  
  3097.   bp = ip->bufp;
  3098.  
  3099.   /* Record where the directive started.  do_xifdef needs this.  */
  3100.   directive_start = bp - 1;
  3101.  
  3102.   /* Skip whitespace and \-newline.  */
  3103.   while (1) {
  3104.     if (is_hor_space[*bp]) {
  3105.       if ((*bp == '\f' || *bp == '\v') && pedantic)
  3106.     pedwarn ("%s in preprocessing directive",
  3107.          *bp == '\f' ? "formfeed" : "vertical tab");
  3108.       bp++;
  3109.     } else if (*bp == '/' && bp[1] == '*') {
  3110.       ip->bufp = bp;
  3111.       skip_to_end_of_comment (ip, &ip->lineno, 0);
  3112.       bp = ip->bufp;
  3113.     } else if (*bp == '\\' && bp[1] == '\n') {
  3114.       bp += 2; ip->lineno++;
  3115.     } else break;
  3116.   }
  3117.  
  3118.   /* Now find end of directive name.
  3119.      If we encounter a backslash-newline, exchange it with any following
  3120.      symbol-constituents so that we end up with a contiguous name.  */
  3121.  
  3122.   cp = bp;
  3123.   while (1) {
  3124.     if (is_idchar[*cp])
  3125.       cp++;
  3126.     else {
  3127.       if (*cp == '\\' && cp[1] == '\n')
  3128.     name_newline_fix (cp);
  3129.       if (is_idchar[*cp])
  3130.     cp++;
  3131.       else break;
  3132.     }
  3133.   }
  3134.   ident_length = cp - bp;
  3135.   ident = bp;
  3136.   after_ident = cp;
  3137.  
  3138.   /* A line of just `#' becomes blank.  */
  3139.  
  3140.   if (ident_length == 0 && *after_ident == '\n') {
  3141.     ip->bufp = after_ident;
  3142.     return 1;
  3143.   }
  3144.  
  3145.   if (ident_length == 0 || !is_idstart[*ident]) {
  3146.     U_CHAR *p = ident;
  3147.     while (is_idchar[*p]) {
  3148.       if (*p < '0' || *p > '9')
  3149.     break;
  3150.       p++;
  3151.     }
  3152.     /* Handle # followed by a line number.  */
  3153.     if (p != ident && !is_idchar[*p]) {
  3154.       static struct directive line_directive_table[] = {
  3155.     {  4, do_line, "line", T_LINE},
  3156.       };
  3157.       if (pedantic)
  3158.     pedwarn ("`#' followed by integer");
  3159.       after_ident = ident;
  3160.       kt = line_directive_table;
  3161.       goto old_linenum;
  3162.     }
  3163.  
  3164.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  3165.     if (p == ident) {
  3166.       while (*p == '#' || is_hor_space[*p]) p++;
  3167.       if (*p == '\n') {
  3168.     if (pedantic && !lang_asm)
  3169.       warning ("invalid preprocessor directive");
  3170.     return 0;
  3171.       }
  3172.     }
  3173.  
  3174.     if (!lang_asm)
  3175.       error ("invalid preprocessor directive name");
  3176.  
  3177.     return 0;
  3178.   }
  3179.  
  3180.   /*
  3181.    * Decode the keyword and call the appropriate expansion
  3182.    * routine, after moving the input pointer up to the next line.
  3183.    */
  3184.   for (kt = directive_table; kt->length > 0; kt++) {
  3185.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  3186.       register U_CHAR *buf;
  3187.       register U_CHAR *limit;
  3188.       int unterminated;
  3189.       int junk;
  3190.       int *already_output = 0;
  3191.  
  3192.       /* Nonzero means do not delete comments within the directive.
  3193.      #define needs this when -traditional.  */
  3194.       int keep_comments;
  3195.  
  3196.     old_linenum:
  3197.  
  3198.       limit = ip->buf + ip->length;
  3199.       unterminated = 0;
  3200.       keep_comments = traditional && kt->traditional_comments;
  3201.       /* #import is defined only in Objective C, or when on the NeXT.  */
  3202.       if (kt->type == T_IMPORT && !(objc || lookup ("__NeXT__", -1, -1)))
  3203.     break;
  3204.  
  3205.       /* Find the end of this command (first newline not backslashed
  3206.      and not in a string or comment).
  3207.      Set COPY_COMMAND if the command must be copied
  3208.      (it contains a backslash-newline or a comment).  */
  3209.  
  3210.       buf = bp = after_ident;
  3211.       while (bp < limit) {
  3212.     register U_CHAR c = *bp++;
  3213.     switch (c) {
  3214.     case '\\':
  3215.       if (bp < limit) {
  3216.         if (*bp == '\n') {
  3217.           ip->lineno++;
  3218.           copy_command = 1;
  3219.         }
  3220.         bp++;
  3221.       }
  3222.       break;
  3223.  
  3224.     case '\'':
  3225.     case '\"':
  3226.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  3227.       /* Don't bother calling the directive if we already got an error
  3228.          message due to unterminated string.  Skip everything and pretend
  3229.          we called the directive.  */
  3230.       if (unterminated) {
  3231.         if (traditional) {
  3232.           /* Traditional preprocessing permits unterminated strings.  */
  3233.           ip->bufp = bp;
  3234.           goto endloop1;
  3235.         }
  3236.         ip->bufp = bp;
  3237.         return 1;
  3238.       }
  3239.       break;
  3240.  
  3241.       /* <...> is special for #include.  */
  3242.     case '<':
  3243.       if (!kt->angle_brackets)
  3244.         break;
  3245.       while (*bp && *bp != '>') bp++;
  3246.       break;
  3247.  
  3248.     case '/':
  3249.       if (*bp == '\\' && bp[1] == '\n')
  3250.         newline_fix (bp);
  3251.       if (*bp == '*'
  3252.           || (cplusplus_comments && *bp == '/')) {
  3253.         U_CHAR *obp = bp - 1;
  3254.         ip->bufp = bp + 1;
  3255.         skip_to_end_of_comment (ip, &ip->lineno, 0);
  3256.         bp = ip->bufp;
  3257.         /* No need to copy the command because of a comment at the end;
  3258.            just don't include the comment in the directive.  */
  3259.         if (bp == limit || *bp == '\n') {
  3260.           bp = obp;
  3261.           goto endloop1;
  3262.         }
  3263.         /* Don't remove the comments if -traditional.  */
  3264.         if (! keep_comments)
  3265.           copy_command++;
  3266.       }
  3267.       break;
  3268.  
  3269.     case '\f':
  3270.     case '\v':
  3271.       if (pedantic)
  3272.         pedwarn ("%s in preprocessing directive",
  3273.              c == '\f' ? "formfeed" : "vertical tab");
  3274.       break;
  3275.  
  3276.     case '\n':
  3277.       --bp;        /* Point to the newline */
  3278.       ip->bufp = bp;
  3279.       goto endloop1;
  3280.     }
  3281.       }
  3282.       ip->bufp = bp;
  3283.  
  3284.     endloop1:
  3285.       resume_p = ip->bufp;
  3286.       /* BP is the end of the directive.
  3287.      RESUME_P is the next interesting data after the directive.
  3288.      A comment may come between.  */
  3289.  
  3290.       /* If a directive should be copied through, and -E was given,
  3291.      pass it through before removing comments.  */
  3292.       if (!no_output && kt->pass_thru && put_out_comments) {
  3293.         int len;
  3294.  
  3295.     /* Output directive name.  */
  3296.         check_expand (op, kt->length + 2);
  3297.     /* Make sure # is at the start of a line */
  3298.     if (op->bufp > op->buf && op->bufp[-1] != '\n') {
  3299.       op->lineno++;
  3300.       *op->bufp++ = '\n';
  3301.     }
  3302.         *op->bufp++ = '#';
  3303.         bcopy (kt->name, op->bufp, kt->length);
  3304.         op->bufp += kt->length;
  3305.  
  3306.     /* Output arguments.  */
  3307.     len = (bp - buf);
  3308.     check_expand (op, len);
  3309.     bcopy (buf, op->bufp, len);
  3310.     op->bufp += len;
  3311.     /* Take account of any (escaped) newlines just output.  */
  3312.     while (--len >= 0)
  3313.       if (buf[len] == '\n')
  3314.         op->lineno++;
  3315.  
  3316.     already_output = &junk;
  3317.       }                /* Don't we need a newline or #line? */
  3318.  
  3319.       if (copy_command) {
  3320.     register U_CHAR *xp = buf;
  3321.     /* Need to copy entire command into temp buffer before dispatching */
  3322.  
  3323.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  3324.                           some slop */
  3325.     buf = cp;
  3326.  
  3327.     /* Copy to the new buffer, deleting comments
  3328.        and backslash-newlines (and whitespace surrounding the latter).  */
  3329.  
  3330.     while (xp < bp) {
  3331.       register U_CHAR c = *xp++;
  3332.       *cp++ = c;
  3333.  
  3334.       switch (c) {
  3335.       case '\n':
  3336.         abort ();  /* A bare newline should never part of the line.  */
  3337.         break;
  3338.  
  3339.         /* <...> is special for #include.  */
  3340.       case '<':
  3341.         if (!kt->angle_brackets)
  3342.           break;
  3343.         while (xp < bp && c != '>') {
  3344.           c = *xp++;
  3345.           if (c == '\\' && xp < bp && *xp == '\n')
  3346.         xp++;
  3347.           else
  3348.         *cp++ = c;
  3349.         }
  3350.         break;
  3351.  
  3352.       case '\\':
  3353.         if (*xp == '\n') {
  3354.           xp++;
  3355.           cp--;
  3356.           if (cp != buf && is_space[cp[-1]]) {
  3357.         while (cp != buf && is_space[cp[-1]]) cp--;
  3358.         cp++;
  3359.         SKIP_WHITE_SPACE (xp);
  3360.           } else if (is_space[*xp]) {
  3361.         *cp++ = *xp++;
  3362.         SKIP_WHITE_SPACE (xp);
  3363.           }
  3364.         } else {
  3365.           *cp++ = *xp++;
  3366.         }
  3367.         break;
  3368.  
  3369.       case '\'':
  3370.       case '\"':
  3371.         {
  3372.           register U_CHAR *bp1
  3373.         = skip_quoted_string (xp - 1, bp, ip->lineno,
  3374.                       NULL_PTR, NULL_PTR, NULL_PTR);
  3375.           while (xp != bp1)
  3376.         if (*xp == '\\') {
  3377.           if (*++xp != '\n')
  3378.             *cp++ = '\\';
  3379.           else
  3380.             xp++;
  3381.         } else
  3382.           *cp++ = *xp++;
  3383.         }
  3384.         break;
  3385.  
  3386.       case '/':
  3387.         if (*xp == '*'
  3388.         || (cplusplus_comments && *xp == '/')) {
  3389.           ip->bufp = xp + 1;
  3390.           /* If we already copied the command through,
  3391.          already_output != 0 prevents outputting comment now.  */
  3392.           skip_to_end_of_comment (ip, already_output, 0);
  3393.           if (keep_comments)
  3394.         while (xp != ip->bufp)
  3395.           *cp++ = *xp++;
  3396.           /* Delete or replace the slash.  */
  3397.           else if (traditional)
  3398.         cp--;
  3399.           else
  3400.         cp[-1] = ' ';
  3401.           xp = ip->bufp;
  3402.         }
  3403.       }
  3404.     }
  3405.  
  3406.     /* Null-terminate the copy.  */
  3407.  
  3408.     *cp = 0;
  3409.       } else
  3410.     cp = bp;
  3411.  
  3412.       ip->bufp = resume_p;
  3413.  
  3414.       /* Some directives should be written out for cc1 to process,
  3415.      just as if they were not defined.  And sometimes we're copying
  3416.      definitions through.  */
  3417.  
  3418.       if (!no_output && already_output == 0
  3419.       && (kt->pass_thru
  3420.           || ((kt->type == T_DEFINE || kt->type == T_UNDEF)
  3421.           && (dump_macros == dump_names
  3422.               || dump_macros == dump_definitions)))) {
  3423.         int len;
  3424.  
  3425.     /* Output directive name.  */
  3426.         check_expand (op, kt->length + 1);
  3427.         *op->bufp++ = '#';
  3428.         bcopy (kt->name, op->bufp, kt->length);
  3429.         op->bufp += kt->length;
  3430.  
  3431.     if (kt->pass_thru || dump_macros == dump_definitions) {
  3432.       /* Output arguments.  */
  3433.       len = (cp - buf);
  3434.       check_expand (op, len);
  3435.       bcopy (buf, op->bufp, len);
  3436.       op->bufp += len;
  3437.     } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
  3438.       U_CHAR *xp = buf;
  3439.       U_CHAR *yp;
  3440.       SKIP_WHITE_SPACE (xp);
  3441.       yp = xp;
  3442.       while (is_idchar[*xp]) xp++;
  3443.       len = (xp - yp);
  3444.       check_expand (op, len + 1);
  3445.       *op->bufp++ = ' ';
  3446.       bcopy (yp, op->bufp, len);
  3447.       op->bufp += len;
  3448.     }
  3449.       }                /* Don't we need a newline or #line? */
  3450.  
  3451.       /* Call the appropriate command handler.  buf now points to
  3452.      either the appropriate place in the input buffer, or to
  3453.      the temp buffer if it was necessary to make one.  cp
  3454.      points to the first char after the contents of the (possibly
  3455.      copied) command, in either case. */
  3456.       (*kt->func) (buf, cp, op, kt);
  3457.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  3458.  
  3459.       return 1;
  3460.     }
  3461.   }
  3462.  
  3463.   /* It is deliberate that we don't warn about undefined directives.
  3464.      That is the responsibility of cc1.  */
  3465.   return 0;
  3466. }
  3467.  
  3468. static struct tm *
  3469. timestamp ()
  3470. {
  3471.   static struct tm *timebuf;
  3472.   if (!timebuf) {
  3473.     time_t t = time (0);
  3474.     timebuf = localtime (&t);
  3475.   }
  3476.   return timebuf;
  3477. }
  3478.  
  3479. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  3480.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  3481.                 };
  3482.  
  3483. /*
  3484.  * expand things like __FILE__.  Place the expansion into the output
  3485.  * buffer *without* rescanning.
  3486.  */
  3487.  
  3488. static void
  3489. special_symbol (hp, op)
  3490.      HASHNODE *hp;
  3491.      FILE_BUF *op;
  3492. {
  3493.   char *buf;
  3494.   int i, len;
  3495.   int true_indepth;
  3496.   FILE_BUF *ip = NULL;
  3497.   struct tm *timebuf;
  3498.  
  3499.   int paren = 0;        /* For special `defined' keyword */
  3500.  
  3501.   if (pcp_outfile && pcp_inside_if
  3502.       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
  3503.     error ("Predefined macro `%s' used inside `#if' during precompilation",
  3504.        hp->name);
  3505.     
  3506.   for (i = indepth; i >= 0; i--)
  3507.     if (instack[i].fname != NULL) {
  3508.       ip = &instack[i];
  3509.       break;
  3510.     }
  3511.   if (ip == NULL) {
  3512.     error ("cccp error: not in any file?!");
  3513.     return;            /* the show must go on */
  3514.   }
  3515.  
  3516.   switch (hp->type) {
  3517.   case T_FILE:
  3518.   case T_BASE_FILE:
  3519.     {
  3520.       char *string;
  3521.       if (hp->type == T_FILE)
  3522.     string = ip->nominal_fname;
  3523.       else
  3524.     string = instack[0].nominal_fname;
  3525.  
  3526.       if (string)
  3527.     {
  3528.       buf = (char *) alloca (3 + strlen (string));
  3529.       sprintf (buf, "\"%s\"", string);
  3530.     }
  3531.       else
  3532.     buf = "\"\"";
  3533.  
  3534.       break;
  3535.     }
  3536.  
  3537.   case T_INCLUDE_LEVEL:
  3538.     true_indepth = 0;
  3539.     for (i = indepth; i >= 0; i--)
  3540.       if (instack[i].fname != NULL)
  3541.         true_indepth++;
  3542.  
  3543.     buf = (char *) alloca (8);    /* Eight bytes ought to be more than enough */
  3544.     sprintf (buf, "%d", true_indepth - 1);
  3545.     break;
  3546.  
  3547.   case T_VERSION:
  3548.     buf = (char *) alloca (3 + strlen (version_string));
  3549.     sprintf (buf, "\"%s\"", version_string);
  3550.     break;
  3551.  
  3552.   case T_SIZE_TYPE:
  3553.     buf = (char *) alloca (3 + strlen (SIZE_TYPE));
  3554.     sprintf (buf, "%s", SIZE_TYPE);
  3555.     break;
  3556.  
  3557.   case T_PTRDIFF_TYPE:
  3558.     buf = (char *) alloca (3 + strlen (PTRDIFF_TYPE));
  3559.     sprintf (buf, "%s", PTRDIFF_TYPE);
  3560.     break;
  3561.  
  3562.   case T_WCHAR_TYPE:
  3563.     buf = (char *) alloca (3 + strlen (WCHAR_TYPE));
  3564.     sprintf (buf, "%s", WCHAR_TYPE);
  3565.     break;
  3566.  
  3567.   case T_CONST:
  3568.     buf = (char *) alloca (4 * sizeof (int));
  3569.     sprintf (buf, "%d", hp->value.ival);
  3570.     if (pcp_inside_if && pcp_outfile)
  3571.       /* Output a precondition for this macro use */
  3572.       fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
  3573.     break;
  3574.  
  3575.   case T_SPECLINE:
  3576.     buf = (char *) alloca (10);
  3577.     sprintf (buf, "%d", ip->lineno);
  3578.     break;
  3579.  
  3580.   case T_DATE:
  3581.   case T_TIME:
  3582.     buf = (char *) alloca (20);
  3583.     timebuf = timestamp ();
  3584.     if (hp->type == T_DATE)
  3585.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  3586.           timebuf->tm_mday, timebuf->tm_year + 1900);
  3587.     else
  3588.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  3589.           timebuf->tm_sec);
  3590.     break;
  3591.  
  3592.   case T_SPEC_DEFINED:
  3593.     buf = " 0 ";        /* Assume symbol is not defined */
  3594.     ip = &instack[indepth];
  3595.     SKIP_WHITE_SPACE (ip->bufp);
  3596.     if (*ip->bufp == '(') {
  3597.       paren++;
  3598.       ip->bufp++;            /* Skip over the paren */
  3599.       SKIP_WHITE_SPACE (ip->bufp);
  3600.     }
  3601.  
  3602.     if (!is_idstart[*ip->bufp])
  3603.       goto oops;
  3604.     if (hp = lookup (ip->bufp, -1, -1)) {
  3605.       if (pcp_outfile && pcp_inside_if
  3606.       && hp->value.defn->predefined)
  3607.     /* Output a precondition for this macro use. */
  3608.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  3609.       buf = " 1 ";
  3610.     }
  3611.     else
  3612.       if (pcp_outfile && pcp_inside_if)    {
  3613.     /* Output a precondition for this macro use */
  3614.     U_CHAR *cp = ip->bufp;
  3615.     fprintf (pcp_outfile, "#undef ");
  3616.     while (is_idchar[*cp]) /* Ick! */
  3617.       fputc (*cp++, pcp_outfile);
  3618.     putc ('\n', pcp_outfile);
  3619.       }
  3620.     while (is_idchar[*ip->bufp])
  3621.       ++ip->bufp;
  3622.     SKIP_WHITE_SPACE (ip->bufp);
  3623.     if (paren) {
  3624.       if (*ip->bufp != ')')
  3625.     goto oops;
  3626.       ++ip->bufp;
  3627.     }
  3628.     break;
  3629.  
  3630. oops:
  3631.  
  3632.     error ("`defined' without an identifier");
  3633.     break;
  3634.  
  3635.   default:
  3636.     error ("cccp error: invalid special hash type"); /* time for gdb */
  3637.     abort ();
  3638.   }
  3639.   len = strlen (buf);
  3640.   check_expand (op, len);
  3641.   bcopy (buf, op->bufp, len);
  3642.   op->bufp += len;
  3643.  
  3644.   return;
  3645. }
  3646.  
  3647.  
  3648. /* Routines to handle #directives */
  3649.  
  3650. /* Handle #include and #import.
  3651.    This function expects to see "fname" or <fname> on the input.  */
  3652.  
  3653. static int
  3654. do_include (buf, limit, op, keyword)
  3655.      U_CHAR *buf, *limit;
  3656.      FILE_BUF *op;
  3657.      struct directive *keyword;
  3658. {
  3659.   int importing = (keyword->type == T_IMPORT);
  3660.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  3661.   static int import_warning = 0;
  3662.   char *fname;        /* Dynamically allocated fname buffer */
  3663.   char *pcftry;
  3664.   char *pcfname;
  3665.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  3666.  
  3667.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  3668.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  3669.   struct file_name_list *searchptr = 0;
  3670.   int flen;
  3671.  
  3672.   int f;            /* file number */
  3673.  
  3674.   int retried = 0;        /* Have already tried macro
  3675.                    expanding the include line*/
  3676.   FILE_BUF trybuf;        /* It got expanded into here */
  3677.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  3678.   int pcf = -1;
  3679.   char *pcfbuf;
  3680.   int pcfbuflimit;
  3681.   int pcfnum;
  3682.   f= -1;            /* JF we iz paranoid! */
  3683.  
  3684.   if (importing && warn_import && !inhibit_warnings
  3685.       && !instack[indepth].system_header_p && !import_warning) {
  3686.     import_warning = 1;
  3687.     warning ("using `#import' is not recommended");
  3688.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  3689.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  3690.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  3691.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  3692.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  3693.     fprintf (stderr, "  ... <real contents of file> ...\n");
  3694.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  3695.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  3696.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  3697.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  3698.   }
  3699.  
  3700. get_filename:
  3701.  
  3702.   fbeg = buf;
  3703.   SKIP_WHITE_SPACE (fbeg);
  3704.   /* Discard trailing whitespace so we can easily see
  3705.      if we have parsed all the significant chars we were given.  */
  3706.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  3707.  
  3708.   switch (*fbeg++) {
  3709.   case '\"':
  3710.     {
  3711.       FILE_BUF *fp;
  3712.       /* Copy the operand text, concatenating the strings.  */
  3713.       {
  3714.     U_CHAR *fin = fbeg;
  3715.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  3716.     fend = fbeg;
  3717.     while (fin != limit) {
  3718.       while (fin != limit && *fin != '\"')
  3719.         *fend++ = *fin++;
  3720.       fin++;
  3721.       if (fin == limit)
  3722.         break;
  3723.       /* If not at the end, there had better be another string.  */
  3724.       /* Skip just horiz space, and don't go past limit.  */
  3725.       while (fin != limit && is_hor_space[*fin]) fin++;
  3726.       if (fin != limit && *fin == '\"')
  3727.         fin++;
  3728.       else
  3729.         goto fail;
  3730.     }
  3731.       }
  3732.       *fend++ = 0;
  3733.  
  3734.       /* We have "filename".  Figure out directory this source
  3735.      file is coming from and put it on the front of the list. */
  3736.  
  3737.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  3738.       if (ignore_srcdir) break;
  3739.  
  3740.       for (fp = &instack[indepth]; fp >= instack; fp--)
  3741.     {
  3742.       int n;
  3743.       char *ep,*nam;
  3744.  
  3745.       if ((nam = fp->nominal_fname) != NULL) {
  3746.         /* Found a named file.  Figure out dir of the file,
  3747.            and put it in front of the search list.  */
  3748.         dsp[0].next = search_start;
  3749.         search_start = dsp;
  3750. #ifndef VMS
  3751.         ep = rindex (nam, '/');
  3752.  
  3753. #ifdef amigados
  3754.         /* amigados uses unix-style directory-filename separation, but
  3755.            has VMS-style logicals as well */
  3756.  
  3757.         if (ep == NULL) 
  3758.           {
  3759.         ep = rindex (nam, ':');
  3760.         /* a ':' is part of the directory name, a '/' isn't ! */
  3761.             if (ep != NULL) ep++;
  3762.           }
  3763. #endif /* amigados */
  3764. #else                /* VMS */
  3765.         ep = rindex (nam, ']');
  3766.         if (ep == NULL) ep = rindex (nam, '>');
  3767.         if (ep == NULL) ep = rindex (nam, ':');
  3768.         if (ep != NULL) ep++;
  3769. #endif                /* VMS */
  3770.         if (ep != NULL) {
  3771.           n = ep - nam;
  3772.           dsp[0].fname = (char *) alloca (n + 1);
  3773.           strncpy (dsp[0].fname, nam, n);
  3774.           dsp[0].fname[n] = '\0';
  3775.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  3776.         max_include_len = n + INCLUDE_LEN_FUDGE;
  3777.         } else {
  3778.           dsp[0].fname = 0; /* Current directory */
  3779.         }
  3780.         break;
  3781.       }
  3782.     }
  3783.       break;
  3784.     }
  3785.  
  3786.   case '<':
  3787.     fend = fbeg;
  3788.     while (fend != limit && *fend != '>') fend++;
  3789.     if (*fend == '>' && fend + 1 == limit) {
  3790.       angle_brackets = 1;
  3791.       /* If -I-, start with the first -I dir after the -I-.  */
  3792.       if (first_bracket_include)
  3793.     search_start = first_bracket_include;
  3794.       break;
  3795.     }
  3796.     goto fail;
  3797.  
  3798.   default:
  3799.   fail:
  3800.     if (retried) {
  3801.       if (importing)
  3802.         error ("`#import' expects \"fname\" or <fname>");
  3803.       else
  3804.         error ("`#include' expects \"fname\" or <fname>");
  3805.       return 0;
  3806.     } else {
  3807.       trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  3808.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  3809.       bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
  3810.       limit = buf + (trybuf.bufp - trybuf.buf);
  3811.       free (trybuf.buf);
  3812.       retried++;
  3813.       goto get_filename;
  3814.     }
  3815.   }
  3816.  
  3817.   /* For #include_next, skip in the search path
  3818.      past the dir in which the containing file was found.  */
  3819.   if (skip_dirs) {
  3820.     FILE_BUF *fp;
  3821.     for (fp = &instack[indepth]; fp >= instack; fp--)
  3822.       if (fp->fname != NULL) {
  3823.     /* fp->dir is null if the containing file was specified
  3824.        with an absolute file name.  In that case, don't skip anything.  */
  3825.     if (fp->dir)
  3826.       search_start = fp->dir->next;
  3827.     break;
  3828.       }
  3829.   }
  3830.  
  3831.   flen = fend - fbeg;
  3832.   /* Allocate this permanently, because it gets stored in the definitions
  3833.      of macros.  */
  3834.   fname = (char *) xmalloc (max_include_len + flen + 2);
  3835.   /* + 2 above for slash and terminating null.  */
  3836.  
  3837.   /* If specified file name is absolute, just open it.  */
  3838.  
  3839. #ifndef amigados
  3840.   if (*fbeg == '/') {
  3841. #else
  3842.   if (amigados_abs_filename (fbeg, flen)) {
  3843. #endif
  3844.     strncpy (fname, fbeg, flen);
  3845.     fname[flen] = 0;
  3846.     if (redundant_include_p (fname))
  3847.       return 0;
  3848.     if (importing)
  3849.       f = lookup_import (fname);
  3850.     else
  3851.       f = open (fname, O_RDONLY, 0666);
  3852.     if (f == -2)
  3853.       return 0;        /* Already included this file */
  3854.   } else {
  3855.     /* Search directory path, trying to open the file.
  3856.        Copy each filename tried into FNAME.  */
  3857.  
  3858.     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  3859.       if (searchptr->fname) {
  3860.     /* The empty string in a search path is ignored.
  3861.        This makes it possible to turn off entirely
  3862.        a standard piece of the list.  */
  3863.     if (searchptr->fname[0] == 0)
  3864.       continue;
  3865.     strcpy (fname, searchptr->fname);
  3866.  
  3867. #ifdef amigados
  3868.     if (fname[strlen (fname) - 1] != ':')
  3869. #endif
  3870.     strcat (fname, "/");
  3871.     fname[strlen (fname) + flen] = 0;
  3872.       } else {
  3873.     fname[0] = 0;
  3874.       }
  3875.       strncat (fname, fbeg, flen);
  3876. #ifdef VMS
  3877.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  3878.          full VMS file specification */
  3879.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  3880.     /* Fix up the filename */
  3881.     hack_vms_include_specification (fname);
  3882.       } else {
  3883.           /* This is a normal VMS filespec, so use it unchanged.  */
  3884.     strncpy (fname, fbeg, flen);
  3885.     fname[flen] = 0;
  3886.       }
  3887. #endif /* VMS */
  3888.       if (importing)
  3889.     f = lookup_import (fname);
  3890.       else
  3891.     f = open (fname, O_RDONLY, 0666);
  3892.       if (f == -2)
  3893.     return 0;            /* Already included this file */
  3894.       if (redundant_include_p (fname)) {
  3895.     close (f);
  3896.     return 0;
  3897.       }
  3898.       if (f >= 0)
  3899.     break;
  3900.     }
  3901.   }
  3902.  
  3903.   if (f < 0) {
  3904.     /* A file that was not found.  */
  3905.  
  3906.     strncpy (fname, fbeg, flen);
  3907.     fname[flen] = 0;
  3908.     if (search_start)
  3909.       error_from_errno (fname);
  3910.     else
  3911.       error ("No include path in which to find %s", fname);
  3912.     
  3913.     /* For -M, add this file to the dependencies.  */
  3914.     if (print_deps > (angle_brackets || (system_include_depth > 0))) {
  3915.       /* Break the line before this.  */
  3916.       deps_output ("", 0);
  3917.  
  3918.       /* If it was requested as a system header file,
  3919.      then assume it belongs in the first place to look for such.  */
  3920.       if (angle_brackets) {
  3921.     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  3922.       if (searchptr->fname) {
  3923.         if (searchptr->fname[0] == 0)
  3924.           continue;
  3925.         deps_output (searchptr->fname, 0);
  3926.         deps_output ("/", 0);
  3927.         break;
  3928.       }
  3929.     }
  3930.       }
  3931.       /* Otherwise, omit the directory, as if the file existed
  3932.      in the directory with the source.  */
  3933.       deps_output (fbeg, flen);
  3934.       deps_output (" ", 0);
  3935.     }
  3936.   } else {
  3937.     struct stat stat_f;
  3938.  
  3939.     /* Check to see if this include file is a once-only include file.
  3940.        If so, give up.  */
  3941.  
  3942.     struct file_name_list* ptr;
  3943.  
  3944.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  3945.       if (!strcmp (ptr->fname, fname)) {
  3946.     close (f);
  3947.         return 0;                /* This file was once'd. */
  3948.       }
  3949.     }
  3950.  
  3951.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  3952.       if (!strcmp (ptr->fname, fname))
  3953.         break;                /* This file was included before. */
  3954.     }
  3955.  
  3956.     if (ptr == 0) {
  3957.       /* This is the first time for this file.  */
  3958.       /* Add it to list of files included.  */
  3959.  
  3960.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  3961.       ptr->control_macro = 0;
  3962.       ptr->next = all_include_files;
  3963.       all_include_files = ptr;
  3964.       ptr->fname = savestring (fname);
  3965.  
  3966.       /* For -M, add this file to the dependencies.  */
  3967.       if (print_deps > (angle_brackets || (system_include_depth > 0))) {
  3968.     deps_output ("", 0);
  3969.     deps_output (fname, 0);
  3970.     deps_output (" ", 0);
  3971.       }
  3972.     }   
  3973.  
  3974.     /* Handle -H option.  */
  3975.     if (print_include_names)
  3976.       fprintf (stderr, "%s\n", fname);
  3977.  
  3978.     if (angle_brackets)
  3979.       system_include_depth++;
  3980.  
  3981.     /* Actually process the file.  */
  3982.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  3983.  
  3984.     pcftry = (char *) alloca (strlen (fname) + 30);
  3985.     pcfbuf = 0;
  3986.     pcfnum = 0;
  3987.  
  3988.     fstat (f, &stat_f);
  3989.  
  3990.     if (!no_precomp)
  3991.       do {
  3992.     sprintf (pcftry, "%s%d", fname, pcfnum++);
  3993.     
  3994.     pcf = open (pcftry, O_RDONLY, 0666);
  3995.     if (pcf != -1)
  3996.       {
  3997.         struct stat s;
  3998.  
  3999.         fstat (pcf, &s);
  4000.         if (bcmp (&stat_f.st_ino, &s.st_ino, sizeof (s.st_ino))
  4001.         || stat_f.st_dev != s.st_dev)
  4002.           {
  4003.         pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  4004.         /* Don't need it any more.  */
  4005.         close (pcf);
  4006.           }
  4007.         else
  4008.           {
  4009.         /* Don't need it at all.  */
  4010.         close (pcf);
  4011.         break;
  4012.           }
  4013.       }
  4014.       } while (pcf != -1 && !pcfbuf);
  4015.     
  4016.     /* Actually process the file */
  4017.     if (pcfbuf) {
  4018.       pcfname = xmalloc (strlen (pcftry) + 1);
  4019.       strcpy (pcfname, pcftry);
  4020.       pcfinclude (pcfbuf, pcfbuflimit, fname, op);
  4021.     }
  4022.     else
  4023.       finclude (f, fname, op, is_system_include (fname), searchptr);
  4024.  
  4025.     if (angle_brackets)
  4026.       system_include_depth--;
  4027.   }
  4028.   return 0;
  4029. }
  4030.  
  4031. /* Return nonzero if there is no need to include file NAME
  4032.    because it has already been included and it contains a conditional
  4033.    to make a repeated include do nothing.  */
  4034.  
  4035. static int
  4036. redundant_include_p (name)
  4037.      char *name;
  4038. {
  4039.   struct file_name_list *l = all_include_files;
  4040.   for (; l; l = l->next)
  4041.     if (! strcmp (name, l->fname)
  4042.     && l->control_macro
  4043.     && lookup (l->control_macro, -1, -1))
  4044.       return 1;
  4045.   return 0;
  4046. }
  4047.  
  4048. /* Return nonzero if the given FILENAME is an absolute pathname which
  4049.    designates a file within one of the known "system" include file
  4050.    directories.  We assume here that if the given FILENAME looks like
  4051.    it is the name of a file which resides either directly in a "system"
  4052.    include file directory, or within any subdirectory thereof, then the
  4053.    given file must be a "system" include file.  This function tells us
  4054.    if we should suppress pedantic errors/warnings for the given FILENAME.  */
  4055.  
  4056. static int
  4057. is_system_include (filename)
  4058.     register char *filename;
  4059. {
  4060.   struct file_name_list *searchptr;
  4061.  
  4062.   for (searchptr = first_system_include; searchptr;
  4063.        searchptr = searchptr->next)
  4064.     if (searchptr->fname) {
  4065.       register char *sys_dir = searchptr->fname;
  4066.       register unsigned length = strlen (sys_dir);
  4067.  
  4068.       if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
  4069.     return 1;
  4070.     }
  4071.   return 0;
  4072. }
  4073.  
  4074. /* Process the contents of include file FNAME, already open on descriptor F,
  4075.    with output to OP.
  4076.    SYSTEM_HEADER_P is 1 if this file was specified using <...>.
  4077.    DIRPTR is the link in the dir path through which this file was found,
  4078.    or 0 if the file name was absolute.  */
  4079.  
  4080. static void
  4081. finclude (f, fname, op, system_header_p, dirptr)
  4082.      int f;
  4083.      char *fname;
  4084.      FILE_BUF *op;
  4085.      int system_header_p;
  4086.      struct file_name_list *dirptr;
  4087. {
  4088.   int st_mode;
  4089.   long st_size;
  4090.   long i;
  4091.   FILE_BUF *fp;            /* For input stack frame */
  4092.   int missing_newline = 0;
  4093.  
  4094.   CHECK_DEPTH (return;);
  4095.  
  4096.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  4097.     {
  4098.       perror_with_name (fname);
  4099.       close (f);
  4100.       return;
  4101.     }
  4102.  
  4103.   fp = &instack[indepth + 1];
  4104.   bzero (fp, sizeof (FILE_BUF));
  4105.   fp->nominal_fname = fp->fname = fname;
  4106.   fp->length = 0;
  4107.   fp->lineno = 1;
  4108.   fp->if_stack = if_stack;
  4109.   fp->system_header_p = system_header_p;
  4110.   fp->dir = dirptr;
  4111.  
  4112.   if (S_ISREG (st_mode)) {
  4113.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  4114.     fp->bufp = fp->buf;
  4115.  
  4116.     /* Read the file contents, knowing that st_size is an upper bound
  4117.        on the number of bytes we can read.  */
  4118.     while (st_size > 0) {
  4119.       i = read (f, fp->buf + fp->length, st_size);
  4120.       if (i <= 0) {
  4121.     if (i == 0) break;
  4122.     goto nope;
  4123.       }
  4124.       fp->length += i;
  4125.       st_size -= i;
  4126.     }
  4127.   }
  4128.   else {
  4129.     /* Cannot count its file size before reading.
  4130.        First read the entire file into heap and
  4131.        copy them into buffer on stack. */
  4132.  
  4133.     U_CHAR *bufp;
  4134.     U_CHAR *basep;
  4135.     int bsize = 2000;
  4136.  
  4137.     st_size = 0;
  4138.     basep = (U_CHAR *) xmalloc (bsize + 2);
  4139.     fp->buf = basep; /* So it will get freed, on error.  */
  4140.     bufp = basep;
  4141.  
  4142.     for (;;) {
  4143.       i = read (f, bufp, bsize - st_size);
  4144.       if (i < 0)
  4145.     goto nope;      /* error! */
  4146.       if (i == 0)
  4147.     break;    /* End of file */
  4148.       st_size += i;
  4149.       bufp += i;
  4150.       if (bsize == st_size) {    /* Buffer is full! */
  4151.       bsize *= 2;
  4152.       basep = (U_CHAR *) xrealloc (basep, bsize + 2);
  4153.       fp->buf = basep;
  4154.       bufp = basep + st_size;    /* May have moved */
  4155.     }
  4156.     }
  4157.     fp->bufp = fp->buf;
  4158.     fp->length = st_size;
  4159.   }
  4160.  
  4161.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  4162.   close (f);
  4163.  
  4164.   /* Must do this before calling trigraph_pcp, so that the correct file name
  4165.      will be printed in warning messages.  */
  4166.  
  4167.   indepth++;
  4168.   input_file_stack_tick++;
  4169.  
  4170.   if (!no_trigraphs)
  4171.     trigraph_pcp (fp);
  4172.  
  4173.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  4174.       /* Backslash-newline at end is not good enough.  */
  4175.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  4176.     fp->buf[fp->length++] = '\n';
  4177.     missing_newline = 1;
  4178.   }
  4179.   fp->buf[fp->length] = '\0';
  4180.  
  4181.   output_line_command (fp, op, 0, enter_file);
  4182.   rescan (op, 0);
  4183.  
  4184.   if (pedantic && missing_newline)
  4185.     pedwarn ("file does not end in newline");
  4186.  
  4187.   indepth--;
  4188.   input_file_stack_tick++;
  4189.   output_line_command (&instack[indepth], op, 0, leave_file);
  4190.   free (fp->buf);
  4191.   return;
  4192.  
  4193.  nope:
  4194.  
  4195.   perror_with_name (fname);
  4196.   close (f);
  4197.   free (fp->buf);
  4198. }
  4199.  
  4200. /* Record that inclusion of the file named FILE
  4201.    should be controlled by the macro named MACRO_NAME.
  4202.    This means that trying to include the file again
  4203.    will do something if that macro is defined.  */
  4204.  
  4205. static void
  4206. record_control_macro (file, macro_name)
  4207.      char *file;
  4208.      U_CHAR *macro_name;
  4209. {
  4210.   struct file_name_list *new;
  4211.  
  4212.   for (new = all_include_files; new; new = new->next) {
  4213.     if (!strcmp (new->fname, file)) {
  4214.       new->control_macro = macro_name;
  4215.       return;
  4216.     }
  4217.   }
  4218.  
  4219.   /* If the file is not in all_include_files, something's wrong.  */
  4220.   abort ();
  4221. }
  4222.  
  4223. /* Maintain and search list of included files, for #import.  */
  4224.  
  4225. #define IMPORT_HASH_SIZE 31
  4226.  
  4227. struct import_file {
  4228.   char *name;
  4229.   ino_t inode;
  4230.   dev_t dev;
  4231.   struct import_file *next;
  4232. };
  4233.  
  4234. /* Hash table of files already included with #include or #import.  */
  4235.  
  4236. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  4237.  
  4238. /* Hash a file name for import_hash_table.  */
  4239.  
  4240. static int 
  4241. import_hash (f)
  4242.      char *f;
  4243. {
  4244.   int val = 0;
  4245.  
  4246.   while (*f) val += *f++;
  4247.   return (val%IMPORT_HASH_SIZE);
  4248. }
  4249.  
  4250. /* Search for file FILENAME in import_hash_table.
  4251.    Return -2 if found, either a matching name or a matching inode.
  4252.    Otherwise, open the file and return a file descriptor if successful
  4253.    or -1 if unsuccessful.  */
  4254.  
  4255. static int
  4256. lookup_import (filename)
  4257.      char *filename;
  4258. {
  4259.   struct import_file *i;
  4260.   int h;
  4261.   int hashval;
  4262.   struct stat sb;
  4263.   int fd;
  4264.  
  4265.   hashval = import_hash (filename);
  4266.  
  4267.   /* Attempt to find file in list of already included files */
  4268.   i = import_hash_table[hashval];
  4269.  
  4270.   while (i) {
  4271.     if (!strcmp (filename, i->name))
  4272.       return -2;        /* return found */
  4273.     i = i->next;
  4274.   }
  4275.   /* Open it and try a match on inode/dev */
  4276.   fd = open (filename, O_RDONLY, 0666);
  4277.   if (fd < 0)
  4278.     return fd;
  4279.   fstat (fd, &sb);
  4280.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  4281.     i = import_hash_table[h];
  4282.     while (i) {
  4283.       /* Compare the inode and the device.
  4284.      Supposedly on some systems the inode is not a scalar.  */
  4285.       if (!bcmp (&i->inode, &sb.st_ino, sizeof (sb.st_ino))
  4286.       && i->dev == sb.st_dev) {
  4287.         close (fd);
  4288.         return -2;        /* return found */
  4289.       }
  4290.       i = i->next;
  4291.     }
  4292.   }
  4293.   return fd;            /* Not found, return open file */
  4294. }
  4295.  
  4296. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  4297.  
  4298. static void
  4299. add_import (fd, fname)
  4300.      int fd;
  4301.      char *fname;
  4302. {
  4303.   struct import_file *i;
  4304.   int hashval;
  4305.   struct stat sb;
  4306.  
  4307.   hashval = import_hash (fname);
  4308.   fstat (fd, &sb);
  4309.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  4310.   i->name = (char *)xmalloc (strlen (fname)+1);
  4311.   strcpy (i->name, fname);
  4312.   bcopy (&sb.st_ino, &i->inode, sizeof (sb.st_ino));
  4313.   i->dev = sb.st_dev;
  4314.   i->next = import_hash_table[hashval];
  4315.   import_hash_table[hashval] = i;
  4316. }
  4317.  
  4318. /* Load the specified precompiled header into core, and verify its
  4319.    preconditions.  PCF indicates the file descriptor to read, which must
  4320.    be a regular file.  FNAME indicates the file name of the original 
  4321.    header.  *LIMIT will be set to an address one past the end of the file.
  4322.    If the preconditions of the file are not satisfied, the buffer is 
  4323.    freed and we return 0.  If the preconditions are satisfied, return
  4324.    the address of the buffer following the preconditions.  The buffer, in
  4325.    this case, should never be freed because various pieces of it will
  4326.    be referred to until all precompiled strings are output at the end of
  4327.    the run.
  4328. */
  4329. static char *
  4330. check_precompiled (pcf, fname, limit)
  4331.      int pcf;
  4332.      char *fname;
  4333.      char **limit;
  4334. {
  4335.   int st_mode;
  4336.   long st_size;
  4337.   int length = 0;
  4338.   char *buf;
  4339.   char *dollar_loc;
  4340.   int i;
  4341.   char *cp;
  4342.  
  4343.   if (pcp_outfile)
  4344.     return 0;
  4345.   
  4346.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  4347.     return 0;
  4348.  
  4349.   if (S_ISREG (st_mode))
  4350.     {
  4351.       buf = xmalloc (st_size + 2);
  4352.       while (st_size > 0)
  4353.     {
  4354.       i = read (pcf, buf + length, st_size);
  4355.       if (i < 0)
  4356.         goto nope;
  4357.       if (i == 0)
  4358.         break;
  4359.       length += i;
  4360.       st_size -= i;
  4361.     }      
  4362.     }
  4363.   else
  4364.     abort ();
  4365.     
  4366.   if (length > 0 && buf[length-1] != '\n')
  4367.     buf[length++] = '\n';
  4368.   buf[length] = '\0';
  4369.   
  4370.   *limit = buf + length;
  4371.  
  4372.   /* File is in core.  Check the preconditions. */
  4373.   if (!check_preconditions (buf))
  4374.     goto nope;
  4375.   for (cp = buf; *cp; cp++)
  4376.     ;
  4377. #ifdef DEBUG_PCP
  4378.   fprintf (stderr, "Using preinclude %s\n", fname);
  4379. #endif
  4380.   return cp + 1;
  4381.  
  4382.  nope:
  4383. #ifdef DEBUG_PCP
  4384.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  4385. #endif
  4386.   free (buf);
  4387.   return 0;
  4388. }
  4389.  
  4390. /* PREC (null terminated) points to the preconditions of a
  4391.    precompiled header.  These are a series of #define and #undef
  4392.    lines which must match the current contents of the hash
  4393.    table.  */
  4394. static int 
  4395. check_preconditions (prec)
  4396.      char *prec;
  4397. {
  4398.   MACRODEF mdef;
  4399.   char *lineend;
  4400.   
  4401.   while (*prec) {
  4402.     lineend = (char *) index (prec, '\n');
  4403.     
  4404.     if (*prec++ != '#') {
  4405.       error ("Bad format encountered while reading precompiled file");
  4406.       return 0;
  4407.     }
  4408.     if (!strncmp (prec, "define", 6)) {
  4409.       HASHNODE *hp;
  4410.       
  4411.       prec += 6;
  4412.       mdef = create_definition (prec, lineend, NULL_PTR);
  4413.  
  4414.       if (mdef.defn == 0)
  4415.     abort();
  4416.       
  4417.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  4418.       || (hp->type != T_MACRO && hp->type != T_CONST)
  4419.       || (hp->type == T_MACRO
  4420.           && !compare_defs (mdef.defn, hp->value.defn)
  4421.           && (mdef.defn->length != 2
  4422.           || mdef.defn->expansion[0] != '\n'
  4423.           || mdef.defn->expansion[1] != ' ')))
  4424.     return 0;
  4425.     } else if (!strncmp (prec, "undef", 5)) {
  4426.       char *name;
  4427.       int len;
  4428.       
  4429.       prec += 5;
  4430.       while (is_hor_space[*prec])
  4431.     prec++;
  4432.       name = prec;
  4433.       while (is_idchar[*prec])
  4434.     prec++;
  4435.       len = prec - name;
  4436.       
  4437.       if (lookup (name, len, -1))
  4438.     return 0;
  4439.     } else {
  4440.       error ("Bad format encountered while reading precompiled file");
  4441.       return 0;
  4442.     }
  4443.     prec = lineend + 1;
  4444.   }
  4445.   /* They all passed successfully */
  4446.   return 1;
  4447. }
  4448.  
  4449. /* Process the main body of a precompiled file.  BUF points to the
  4450.    string section of the file, following the preconditions.  LIMIT is one
  4451.    character past the end.  NAME is the name of the file being read
  4452.    in.  OP is the main output buffer */
  4453. static void
  4454. pcfinclude (buf, limit, name, op)
  4455.      U_CHAR *buf, *limit, *name;
  4456.      FILE_BUF *op;
  4457. {
  4458.   FILE_BUF tmpbuf;
  4459.   int nstrings;
  4460.   U_CHAR *cp = buf;
  4461.  
  4462.   /* First in the file comes 4 bytes indicating the number of strings, */
  4463.   /* in network byte order. (MSB first).  */
  4464.   nstrings = *cp++;
  4465.   nstrings = (nstrings << 8) | *cp++;
  4466.   nstrings = (nstrings << 8) | *cp++;
  4467.   nstrings = (nstrings << 8) | *cp++;
  4468.   
  4469.   /* Looping over each string... */
  4470.   while (nstrings--) {
  4471.     U_CHAR *string_start;
  4472.     U_CHAR *endofthiskey;
  4473.     STRINGDEF *str;
  4474.     int nkeys;
  4475.     
  4476.     /* Each string starts with a STRINGDEF structure (str), followed */
  4477.     /* by the text of the string (string_start) */
  4478.  
  4479.     /* First skip to a longword boundary */
  4480.     /* ??? Why a 4-byte boundary?  On all machines? */
  4481.     /* NOTE: This works correctly even if HOST_WIDE_INT
  4482.        is narrower than a pointer.
  4483.        Do not try risky measures here to get another type to use!
  4484.        Do not include gstddef.h or stddef.h--either one will fail!  */
  4485.     if ((HOST_WIDE_INT) cp & 3)
  4486.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  4487.     
  4488.     /* Now get the string. */
  4489.     str = (STRINGDEF *) cp;
  4490.     string_start = cp += sizeof (STRINGDEF);
  4491.     
  4492.     for (; *cp; cp++)        /* skip the string */
  4493.       ;
  4494.     
  4495.     /* We need to macro expand the string here to ensure that the
  4496.        proper definition environment is in place.  If it were only
  4497.        expanded when we find out it is needed, macros necessary for
  4498.        its proper expansion might have had their definitions changed. */
  4499.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  4500.     /* Lineno is already set in the precompiled file */
  4501.     str->contents = tmpbuf.buf;
  4502.     str->len = tmpbuf.length;
  4503.     str->writeflag = 0;
  4504.     str->filename = name;
  4505.     str->output_mark = outbuf.bufp - outbuf.buf;
  4506.     
  4507.     str->chain = 0;
  4508.     *stringlist_tailp = str;
  4509.     stringlist_tailp = &str->chain;
  4510.     
  4511.     /* Next comes a fourbyte number indicating the number of keys */
  4512.     /* for this string. */
  4513.     nkeys = *cp++;
  4514.     nkeys = (nkeys << 8) | *cp++;
  4515.     nkeys = (nkeys << 8) | *cp++;
  4516.     nkeys = (nkeys << 8) | *cp++;
  4517.  
  4518.     /* If this number is -1, then the string is mandatory. */
  4519.     if (nkeys == -1)
  4520.       str->writeflag = 1;
  4521.     else
  4522.       /* Otherwise, for each key, */
  4523.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  4524.     KEYDEF *kp = (KEYDEF *) cp;
  4525.     HASHNODE *hp;
  4526.     
  4527.     /* It starts with a KEYDEF structure */
  4528.     cp += sizeof (KEYDEF);
  4529.     
  4530.     /* Find the end of the key.  At the end of this for loop we
  4531.        advance CP to the start of the next key using this variable. */
  4532.     endofthiskey = cp + strlen (cp);
  4533.     kp->str = str;
  4534.     
  4535.     /* Expand the key, and enter it into the hash table. */
  4536.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  4537.     tmpbuf.bufp = tmpbuf.buf;
  4538.     
  4539.     while (is_hor_space[*tmpbuf.bufp])
  4540.       tmpbuf.bufp++;
  4541.     if (!is_idstart[*tmpbuf.bufp]
  4542.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  4543.       str->writeflag = 1;
  4544.       continue;
  4545.     }
  4546.         
  4547.     hp = lookup (tmpbuf.bufp, -1, -1);
  4548.     if (hp == NULL) {
  4549.       kp->chain = 0;
  4550.       install (tmpbuf.bufp, -1, T_PCSTRING, 0, (char *) kp, -1);
  4551.     }
  4552.     else if (hp->type == T_PCSTRING) {
  4553.       kp->chain = hp->value.keydef;
  4554.       hp->value.keydef = kp;
  4555.     }
  4556.     else
  4557.       str->writeflag = 1;
  4558.       }
  4559.   }
  4560.   /* This output_line_command serves to switch us back to the current
  4561.      input file in case some of these strings get output (which will 
  4562.      result in line commands for the header file being output). */
  4563.   output_line_command (&instack[indepth], op, 0, enter_file);
  4564. }
  4565.  
  4566. /* Called from rescan when it hits a key for strings.  Mark them all */
  4567.  /* used and clean up. */
  4568. static void
  4569. pcstring_used (hp)
  4570.      HASHNODE *hp;
  4571. {
  4572.   KEYDEF *kp, *tmp;
  4573.   
  4574.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  4575.     kp->str->writeflag = 1;
  4576.   delete_macro (hp);
  4577. }
  4578.  
  4579. /* Write the output, interspersing precompiled strings in their */
  4580.  /* appropriate places. */
  4581. static void
  4582. write_output ()
  4583. {
  4584.   STRINGDEF *next_string;
  4585.   U_CHAR *cur_buf_loc;
  4586.   int line_command_len = 80;
  4587.   char *line_command = xmalloc (line_command_len);
  4588.   int len;
  4589.  
  4590.   /* In each run through the loop, either cur_buf_loc == */
  4591.   /* next_string_loc, in which case we print a series of strings, or */
  4592.   /* it is less than next_string_loc, in which case we write some of */
  4593.   /* the buffer. */
  4594.   cur_buf_loc = outbuf.buf; 
  4595.   next_string = stringlist;
  4596.   
  4597.   while (cur_buf_loc < outbuf.bufp || next_string) {
  4598.     if (next_string
  4599.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  4600.       if (next_string->writeflag) {
  4601.     len = strlen (next_string->filename);
  4602.     if (len > line_command_len)
  4603.       line_command = xrealloc (line_command, 
  4604.                    line_command_len *= 2);
  4605.     sprintf (line_command, "\n# %d \"%s\"\n",
  4606.          next_string->lineno, next_string->filename);
  4607.     write (fileno (stdout), line_command, 
  4608.            strlen (line_command));
  4609.     write (fileno (stdout),
  4610.            next_string->contents, next_string->len);
  4611.       }          
  4612.       next_string = next_string->chain;
  4613.     }
  4614.     else {
  4615.       len = (next_string
  4616.          ? (next_string->output_mark 
  4617.         - (cur_buf_loc - outbuf.buf))
  4618.          : outbuf.bufp - cur_buf_loc);
  4619.       
  4620.       write (fileno (stdout), cur_buf_loc, len);
  4621.       cur_buf_loc += len;
  4622.     }
  4623.   }
  4624. }
  4625.  
  4626. /* Pass a directive through to the output file.
  4627.    BUF points to the contents of the directive, as a contiguous string.
  4628.    LIMIT points to the first character past the end of the directive.
  4629.    KEYWORD is the keyword-table entry for the directive.  */
  4630.  
  4631. static void
  4632. pass_thru_directive (buf, limit, op, keyword)
  4633.      U_CHAR *buf, *limit;
  4634.      FILE_BUF *op;
  4635.      struct directive *keyword;
  4636. {
  4637.   register unsigned keyword_length = keyword->length;
  4638.  
  4639.   check_expand (op, 1 + keyword_length + (limit - buf));
  4640.   *op->bufp++ = '#';
  4641.   bcopy (keyword->name, op->bufp, keyword_length);
  4642.   op->bufp += keyword_length;
  4643.   if (limit != buf && buf[0] != ' ')
  4644.     *op->bufp++ = ' ';
  4645.   bcopy (buf, op->bufp, limit - buf);
  4646.   op->bufp += (limit - buf);
  4647. #if 0
  4648.   *op->bufp++ = '\n';
  4649.   /* Count the line we have just made in the output,
  4650.      to get in sync properly.  */
  4651.   op->lineno++;
  4652. #endif
  4653. }
  4654.  
  4655. /* The arglist structure is built by do_define to tell
  4656.    collect_definition where the argument names begin.  That
  4657.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  4658.    would contain pointers to the strings x, y, and z.
  4659.    Collect_definition would then build a DEFINITION node,
  4660.    with reflist nodes pointing to the places x, y, and z had
  4661.    appeared.  So the arglist is just convenience data passed
  4662.    between these two routines.  It is not kept around after
  4663.    the current #define has been processed and entered into the
  4664.    hash table. */
  4665.  
  4666. struct arglist {
  4667.   struct arglist *next;
  4668.   U_CHAR *name;
  4669.   int length;
  4670.   int argno;
  4671.   char rest_args;
  4672. };
  4673.  
  4674. /* Create a DEFINITION node from a #define directive.  Arguments are 
  4675.    as for do_define. */
  4676. static MACRODEF
  4677. create_definition (buf, limit, op)
  4678.      U_CHAR *buf, *limit;
  4679.      FILE_BUF *op;
  4680. {
  4681.   U_CHAR *bp;            /* temp ptr into input buffer */
  4682.   U_CHAR *symname;        /* remember where symbol name starts */
  4683.   int sym_length;        /* and how long it is */
  4684.   int line = instack[indepth].lineno;
  4685.   char *file = instack[indepth].nominal_fname;
  4686.   int rest_args = 0;
  4687.  
  4688.   DEFINITION *defn;
  4689.   int arglengths = 0;        /* Accumulate lengths of arg names
  4690.                    plus number of args.  */
  4691.   MACRODEF mdef;
  4692.  
  4693.   bp = buf;
  4694.  
  4695.   while (is_hor_space[*bp])
  4696.     bp++;
  4697.  
  4698.   symname = bp;            /* remember where it starts */
  4699.   sym_length = check_macro_name (bp, "macro");
  4700.   bp += sym_length;
  4701.  
  4702.   /* Lossage will occur if identifiers or control keywords are broken
  4703.      across lines using backslash.  This is not the right place to take
  4704.      care of that. */
  4705.  
  4706.   if (*bp == '(') {
  4707.     struct arglist *arg_ptrs = NULL;
  4708.     int argno = 0;
  4709.  
  4710.     bp++;            /* skip '(' */
  4711.     SKIP_WHITE_SPACE (bp);
  4712.  
  4713.     /* Loop over macro argument names.  */
  4714.     while (*bp != ')') {
  4715.       struct arglist *temp;
  4716.  
  4717.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  4718.       temp->name = bp;
  4719.       temp->next = arg_ptrs;
  4720.       temp->argno = argno++;
  4721.       temp->rest_args = 0;
  4722.       arg_ptrs = temp;
  4723.  
  4724.       if (rest_args)
  4725.     pedwarn ("another parameter follows `%s'",
  4726.          rest_extension);
  4727.  
  4728.       if (!is_idstart[*bp])
  4729.     pedwarn ("invalid character in macro parameter name");
  4730.       
  4731.       /* Find the end of the arg name.  */
  4732.       while (is_idchar[*bp]) {
  4733.     bp++;
  4734.     /* do we have a "special" rest-args extension here? */
  4735.     if (limit - bp > REST_EXTENSION_LENGTH &&
  4736.         strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  4737.       rest_args = 1;
  4738.       temp->rest_args = 1;
  4739.       break;
  4740.     }
  4741.       }
  4742.       temp->length = bp - temp->name;
  4743.       if (rest_args == 1)
  4744.     bp += REST_EXTENSION_LENGTH;
  4745.       arglengths += temp->length + 2;
  4746.       SKIP_WHITE_SPACE (bp);
  4747.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  4748.     error ("badly punctuated parameter list in `#define'");
  4749.     goto nope;
  4750.       }
  4751.       if (*bp == ',') {
  4752.     bp++;
  4753.     SKIP_WHITE_SPACE (bp);
  4754.       }
  4755.       if (bp >= limit) {
  4756.     error ("unterminated parameter list in `#define'");
  4757.     goto nope;
  4758.       }
  4759.       {
  4760.     struct arglist *otemp;
  4761.  
  4762.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  4763.       if (temp->length == otemp->length &&
  4764.         strncmp(temp->name, otemp->name, temp->length) == 0) {
  4765.           U_CHAR *name;
  4766.  
  4767.           name = (U_CHAR *) alloca(temp->length + 1);
  4768.           (void) strncpy(name, temp->name, temp->length);
  4769.           name[temp->length] = '\0';
  4770.           error ("duplicate argument name `%s' in `#define'", name);
  4771.           goto nope;
  4772.       }
  4773.       }
  4774.     }
  4775.  
  4776.     ++bp;            /* skip paren */
  4777.     /* Skip exactly one space or tab if any.  */
  4778.     if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
  4779.     /* now everything from bp before limit is the definition. */
  4780.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  4781.     defn->rest_args = rest_args;
  4782.  
  4783.     /* Now set defn->args.argnames to the result of concatenating
  4784.        the argument names in reverse order
  4785.        with comma-space between them.  */
  4786.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  4787.     {
  4788.       struct arglist *temp;
  4789.       int i = 0;
  4790.       for (temp = arg_ptrs; temp; temp = temp->next) {
  4791.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  4792.     i += temp->length;
  4793.     if (temp->next != 0) {
  4794.       defn->args.argnames[i++] = ',';
  4795.       defn->args.argnames[i++] = ' ';
  4796.     }
  4797.       }
  4798.       defn->args.argnames[i] = 0;
  4799.     }
  4800.   } else {
  4801.     /* simple expansion or empty definition; gobble it */
  4802.     if (is_hor_space[*bp])
  4803.       ++bp;        /* skip exactly one blank/tab char */
  4804.     /* now everything from bp before limit is the definition. */
  4805.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  4806.     defn->args.argnames = (U_CHAR *) "";
  4807.   }
  4808.  
  4809.   defn->line = line;
  4810.   defn->file = file;
  4811.  
  4812.   /* OP is null if this is a predefinition */
  4813.   defn->predefined = !op;
  4814.   mdef.defn = defn;
  4815.   mdef.symnam = symname;
  4816.   mdef.symlen = sym_length;
  4817.  
  4818.   return mdef;
  4819.  
  4820.  nope:
  4821.   mdef.defn = 0;
  4822.   return mdef;
  4823. }
  4824.  
  4825. /* Process a #define command.
  4826. BUF points to the contents of the #define command, as a contiguous string.
  4827. LIMIT points to the first character past the end of the definition.
  4828. KEYWORD is the keyword-table entry for #define.  */
  4829.  
  4830. static int
  4831. do_define (buf, limit, op, keyword)
  4832.      U_CHAR *buf, *limit;
  4833.      FILE_BUF *op;
  4834.      struct directive *keyword;
  4835. {
  4836.   int hashcode;
  4837.   MACRODEF mdef;
  4838.  
  4839.   /* If this is a precompiler run (with -pcp) pass thru #define commands.  */
  4840.   if (pcp_outfile && op)
  4841.     pass_thru_directive (buf, limit, op, keyword);
  4842.  
  4843.   mdef = create_definition (buf, limit, op);
  4844.   if (mdef.defn == 0)
  4845.     goto nope;
  4846.  
  4847.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  4848.  
  4849.   {
  4850.     HASHNODE *hp;
  4851.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  4852.       int ok = 0;
  4853.       /* Redefining a precompiled key is ok.  */
  4854.       if (hp->type == T_PCSTRING)
  4855.     ok = 1;
  4856.       /* Redefining a macro is ok if the definitions are the same.  */
  4857.       else if (hp->type == T_MACRO)
  4858.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  4859.       /* Redefining a constant is ok with -D.  */
  4860.       else if (hp->type == T_CONST)
  4861.         ok = ! done_initializing;
  4862.       /* Print the warning if it's not ok.  */
  4863.       if (!ok) {
  4864.     U_CHAR *msg;        /* what pain... */
  4865.  
  4866.         /* If we are passing through #define and #undef directives, do
  4867.        that for this re-definition now.  */
  4868.         if (debug_output && op)
  4869.       pass_thru_directive (buf, limit, op, keyword);
  4870.  
  4871.     msg = (U_CHAR *) alloca (mdef.symlen + 22);
  4872.     *msg = '`';
  4873.     bcopy (mdef.symnam, msg + 1, mdef.symlen);
  4874.     strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
  4875.     pedwarn (msg);
  4876.     if (hp->type == T_MACRO)
  4877.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  4878.                       "this is the location of the previous definition");
  4879.       }
  4880.       /* Replace the old definition.  */
  4881.       hp->type = T_MACRO;
  4882.       hp->value.defn = mdef.defn;
  4883.     } else {
  4884.       /* If we are passing through #define and #undef directives, do
  4885.      that for this new definition now.  */
  4886.       if (debug_output && op)
  4887.     pass_thru_directive (buf, limit, op, keyword);
  4888.       install (mdef.symnam, mdef.symlen, T_MACRO, 0,
  4889.            (char *) mdef.defn, hashcode);
  4890.     }
  4891.   }
  4892.  
  4893.   return 0;
  4894.  
  4895. nope:
  4896.  
  4897.   return 1;
  4898. }
  4899.  
  4900. /* Check a purported macro name SYMNAME, and yield its length.
  4901.    USAGE is the kind of name this is intended for.  */
  4902.  
  4903. static int
  4904. check_macro_name (symname, usage)
  4905.      U_CHAR *symname;
  4906.      char *usage;
  4907. {
  4908.   U_CHAR *p;
  4909.   int sym_length;
  4910.  
  4911.   for (p = symname; is_idchar[*p]; p++)
  4912.     ;
  4913.   sym_length = p - symname;
  4914.   if (sym_length == 0)
  4915.     error ("invalid %s name", usage);
  4916.   else if (!is_idstart[*symname]) {
  4917.     U_CHAR *msg;            /* what pain... */
  4918.     msg = (U_CHAR *) alloca (sym_length + 1);
  4919.     bcopy (symname, msg, sym_length);
  4920.     msg[sym_length] = 0;
  4921.     error ("invalid %s name `%s'", usage, msg);
  4922.   } else {
  4923.     if (! strncmp (symname, "defined", 7) && sym_length == 7)
  4924.       error ("invalid %s name `defined'", usage);
  4925.   }
  4926.   return sym_length;
  4927. }
  4928.  
  4929. /*
  4930.  * return zero if two DEFINITIONs are isomorphic
  4931.  */
  4932. static int
  4933. compare_defs (d1, d2)
  4934.      DEFINITION *d1, *d2;
  4935. {
  4936.   register struct reflist *a1, *a2;
  4937.   register U_CHAR *p1 = d1->expansion;
  4938.   register U_CHAR *p2 = d2->expansion;
  4939.   int first = 1;
  4940.  
  4941.   if (d1->nargs != d2->nargs)
  4942.     return 1;
  4943.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  4944.     return 1;
  4945.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  4946.        a1 = a1->next, a2 = a2->next) {
  4947.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  4948.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  4949.     || a1->argno != a2->argno
  4950.     || a1->stringify != a2->stringify
  4951.     || a1->raw_before != a2->raw_before
  4952.     || a1->raw_after != a2->raw_after)
  4953.       return 1;
  4954.     first = 0;
  4955.     p1 += a1->nchars;
  4956.     p2 += a2->nchars;
  4957.   }
  4958.   if (a1 != a2)
  4959.     return 1;
  4960.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  4961.              p2, d2->length - (p2 - d2->expansion), 1))
  4962.     return 1;
  4963.   return 0;
  4964. }
  4965.  
  4966. /* Return 1 if two parts of two macro definitions are effectively different.
  4967.    One of the parts starts at BEG1 and has LEN1 chars;
  4968.    the other has LEN2 chars at BEG2.
  4969.    Any sequence of whitespace matches any other sequence of whitespace.
  4970.    FIRST means these parts are the first of a macro definition;
  4971.     so ignore leading whitespace entirely.
  4972.    LAST means these parts are the last of a macro definition;
  4973.     so ignore trailing whitespace entirely.  */
  4974.  
  4975. static int
  4976. comp_def_part (first, beg1, len1, beg2, len2, last)
  4977.      int first;
  4978.      U_CHAR *beg1, *beg2;
  4979.      int len1, len2;
  4980.      int last;
  4981. {
  4982.   register U_CHAR *end1 = beg1 + len1;
  4983.   register U_CHAR *end2 = beg2 + len2;
  4984.   if (first) {
  4985.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  4986.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  4987.   }
  4988.   if (last) {
  4989.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  4990.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  4991.   }
  4992.   while (beg1 != end1 && beg2 != end2) {
  4993.     if (is_space[*beg1] && is_space[*beg2]) {
  4994.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  4995.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  4996.     } else if (*beg1 == *beg2) {
  4997.       beg1++; beg2++;
  4998.     } else break;
  4999.   }
  5000.   return (beg1 != end1) || (beg2 != end2);
  5001. }
  5002.  
  5003. /* Read a replacement list for a macro with parameters.
  5004.    Build the DEFINITION structure.
  5005.    Reads characters of text starting at BUF until END.
  5006.    ARGLIST specifies the formal parameters to look for
  5007.    in the text of the definition; NARGS is the number of args
  5008.    in that list, or -1 for a macro name that wants no argument list.
  5009.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  5010.    and NAMELEN is its length in characters.
  5011.    
  5012. Note that comments and backslash-newlines have already been deleted
  5013. from the argument.  */
  5014.  
  5015. /* Leading and trailing Space, Tab, etc. are converted to markers
  5016.    Newline Space, Newline Tab, etc.
  5017.    Newline Space makes a space in the final output
  5018.    but is discarded if stringified.  (Newline Tab is similar but
  5019.    makes a Tab instead.)
  5020.  
  5021.    If there is no trailing whitespace, a Newline Space is added at the end
  5022.    to prevent concatenation that would be contrary to the standard.  */
  5023.  
  5024. static DEFINITION *
  5025. collect_expansion (buf, end, nargs, arglist)
  5026.      U_CHAR *buf, *end;
  5027.      int nargs;
  5028.      struct arglist *arglist;
  5029. {
  5030.   DEFINITION *defn;
  5031.   register U_CHAR *p, *limit, *lastp, *exp_p;
  5032.   struct reflist *endpat = NULL;
  5033.   /* Pointer to first nonspace after last ## seen.  */
  5034.   U_CHAR *concat = 0;
  5035.   /* Pointer to first nonspace after last single-# seen.  */
  5036.   U_CHAR *stringify = 0;
  5037.   int maxsize;
  5038.   int expected_delimiter = '\0';
  5039.  
  5040.   /* Scan thru the replacement list, ignoring comments and quoted
  5041.      strings, picking up on the macro calls.  It does a linear search
  5042.      thru the arg list on every potential symbol.  Profiling might say
  5043.      that something smarter should happen. */
  5044.  
  5045.   if (end < buf)
  5046.     abort ();
  5047.  
  5048.   /* Find the beginning of the trailing whitespace.  */
  5049.   /* Find end of leading whitespace.  */
  5050.   limit = end;
  5051.   p = buf;
  5052.   while (p < limit && is_space[limit[-1]]) limit--;
  5053.   while (p < limit && is_space[*p]) p++;
  5054.  
  5055.   /* Allocate space for the text in the macro definition.
  5056.      Leading and trailing whitespace chars need 2 bytes each.
  5057.      Each other input char may or may not need 1 byte,
  5058.      so this is an upper bound.
  5059.      The extra 2 are for invented trailing newline-marker and final null.  */
  5060.   maxsize = (sizeof (DEFINITION)
  5061.          + 2 * (end - limit) + 2 * (p - buf)
  5062.          + (limit - p) + 3);
  5063.   defn = (DEFINITION *) xcalloc (1, maxsize);
  5064.  
  5065.   defn->nargs = nargs;
  5066.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  5067.   lastp = exp_p;
  5068.  
  5069.   p = buf;
  5070.  
  5071.   /* Convert leading whitespace to Newline-markers.  */
  5072.   while (p < limit && is_space[*p]) {
  5073.     *exp_p++ = '\n';
  5074.     *exp_p++ = *p++;
  5075.   }
  5076.  
  5077.   if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
  5078.     error ("`##' at start of macro definition");
  5079.     p += 2;
  5080.   }
  5081.  
  5082.   /* Process the main body of the definition.  */
  5083.   while (p < limit) {
  5084.     int skipped_arg = 0;
  5085.     register U_CHAR c = *p++;
  5086.  
  5087.     *exp_p++ = c;
  5088.  
  5089.     if (!traditional) {
  5090.       switch (c) {
  5091.       case '\'':
  5092.       case '\"':
  5093.         if (expected_delimiter != '\0') {
  5094.           if (c == expected_delimiter)
  5095.             expected_delimiter = '\0';
  5096.         } else
  5097.           expected_delimiter = c;
  5098.     break;
  5099.  
  5100.     /* Special hack: if a \# is written in the #define
  5101.        include a # in the definition.  This is useless for C code
  5102.        but useful for preprocessing other things.  */
  5103.  
  5104.       case '\\':
  5105.     /* \# quotes a # even outside of strings.  */
  5106.     if (p < limit && *p == '#' && !expected_delimiter) {
  5107.       exp_p--;
  5108.       *exp_p++ = *p++;
  5109.     } else if (p < limit && expected_delimiter) {
  5110.       /* In a string, backslash goes through
  5111.          and makes next char ordinary.  */
  5112.       *exp_p++ = *p++;
  5113.     }
  5114.     break;
  5115.  
  5116.       case '#':
  5117.     /* # is ordinary inside a string.  */
  5118.     if (expected_delimiter)
  5119.       break;
  5120.     if (p < limit && *p == '#') {
  5121.       /* ##: concatenate preceding and following tokens.  */
  5122.       /* Take out the first #, discard preceding whitespace.  */
  5123.       exp_p--;
  5124.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  5125.         --exp_p;
  5126.       /* Skip the second #.  */
  5127.       p++;
  5128.       /* Discard following whitespace.  */
  5129.       SKIP_WHITE_SPACE (p);
  5130.       concat = p;
  5131.       if (p == limit)
  5132.         error ("`##' at end of macro definition");
  5133.     } else {
  5134.       /* Single #: stringify following argument ref.
  5135.          Don't leave the # in the expansion.  */
  5136.       exp_p--;
  5137.       SKIP_WHITE_SPACE (p);
  5138.       if (p == limit || ! is_idstart[*p] || nargs <= 0)
  5139.         error ("`#' operator is not followed by a macro argument name");
  5140.       else
  5141.         stringify = p;
  5142.     }
  5143.     break;
  5144.       }
  5145.     } else {
  5146.       /* In -traditional mode, recognize arguments inside strings and
  5147.      and character constants, and ignore special properties of #.
  5148.      Arguments inside strings are considered "stringified", but no
  5149.      extra quote marks are supplied.  */
  5150.       switch (c) {
  5151.       case '\'':
  5152.       case '\"':
  5153.     if (expected_delimiter != '\0') {
  5154.       if (c == expected_delimiter)
  5155.         expected_delimiter = '\0';
  5156.     } else
  5157.       expected_delimiter = c;
  5158.     break;
  5159.  
  5160.       case '\\':
  5161.     /* Backslash quotes delimiters and itself, but not macro args.  */
  5162.     if (expected_delimiter != 0 && p < limit
  5163.         && (*p == expected_delimiter || *p == '\\')) {
  5164.       *exp_p++ = *p++;
  5165.       continue;
  5166.     }
  5167.     break;
  5168.  
  5169.       case '/':
  5170.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  5171.       break;
  5172.     if (*p == '*') {
  5173.       /* If we find a comment that wasn't removed by handle_directive,
  5174.          this must be -traditional.  So replace the comment with
  5175.          nothing at all.  */
  5176.       exp_p--;
  5177.       p += 1;
  5178.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  5179.         p++;
  5180. #if 0
  5181.       /* Mark this as a concatenation-point, as if it had been ##.  */
  5182.       concat = p;
  5183. #endif
  5184.     }
  5185.     break;
  5186.       }
  5187.     }
  5188.  
  5189.     /* Handle the start of a symbol.  */
  5190.     if (is_idchar[c] && nargs > 0) {
  5191.       U_CHAR *id_beg = p - 1;
  5192.       int id_len;
  5193.  
  5194.       --exp_p;
  5195.       while (p != limit && is_idchar[*p]) p++;
  5196.       id_len = p - id_beg;
  5197.  
  5198.       if (is_idstart[c]) {
  5199.     register struct arglist *arg;
  5200.  
  5201.     for (arg = arglist; arg != NULL; arg = arg->next) {
  5202.       struct reflist *tpat;
  5203.  
  5204.       if (arg->name[0] == c
  5205.           && arg->length == id_len
  5206.           && strncmp (arg->name, id_beg, id_len) == 0) {
  5207.         if (expected_delimiter && warn_stringify) {
  5208.           if (traditional) {
  5209.         warning ("macro argument `%.*s' is stringified.",
  5210.              id_len, arg->name);
  5211.           } else {
  5212.         warning ("macro arg `%.*s' would be stringified with -traditional.",
  5213.              id_len, arg->name);
  5214.           }
  5215.         }
  5216.         /* If ANSI, don't actually substitute inside a string.  */
  5217.         if (!traditional && expected_delimiter)
  5218.           break;
  5219.         /* make a pat node for this arg and append it to the end of
  5220.            the pat list */
  5221.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  5222.         tpat->next = NULL;
  5223.         tpat->raw_before = concat == id_beg;
  5224.         tpat->raw_after = 0;
  5225.         tpat->rest_args = arg->rest_args;
  5226.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  5227.                    : stringify == id_beg);
  5228.  
  5229.         if (endpat == NULL)
  5230.           defn->pattern = tpat;
  5231.         else
  5232.           endpat->next = tpat;
  5233.         endpat = tpat;
  5234.  
  5235.         tpat->argno = arg->argno;
  5236.         tpat->nchars = exp_p - lastp;
  5237.         {
  5238.           register U_CHAR *p1 = p;
  5239.           SKIP_WHITE_SPACE (p1);
  5240.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  5241.         tpat->raw_after = 1;
  5242.         }
  5243.         lastp = exp_p;    /* place to start copying from next time */
  5244.         skipped_arg = 1;
  5245.         break;
  5246.       }
  5247.     }
  5248.       }
  5249.  
  5250.       /* If this was not a macro arg, copy it into the expansion.  */
  5251.       if (! skipped_arg) {
  5252.     register U_CHAR *lim1 = p;
  5253.     p = id_beg;
  5254.     while (p != lim1)
  5255.       *exp_p++ = *p++;
  5256.     if (stringify == id_beg)
  5257.       error ("`#' operator should be followed by a macro argument name");
  5258.       }
  5259.     }
  5260.   }
  5261.  
  5262.   if (limit < end) {
  5263.     /* Convert trailing whitespace to Newline-markers.  */
  5264.     while (limit < end && is_space[*limit]) {
  5265.       *exp_p++ = '\n';
  5266.       *exp_p++ = *limit++;
  5267.     }
  5268.   } else if (!traditional && expected_delimiter == 0) {
  5269.     /* There is no trailing whitespace, so invent some in ANSI mode.
  5270.        But not if "inside a string" (which in ANSI mode
  5271.        happens only for -D option).  */
  5272.     *exp_p++ = '\n';
  5273.     *exp_p++ = ' ';
  5274.   }
  5275.  
  5276.   *exp_p = '\0';
  5277.  
  5278.   defn->length = exp_p - defn->expansion;
  5279.  
  5280.   /* Crash now if we overrun the allocated size.  */
  5281.   if (defn->length + 1 > maxsize)
  5282.     abort ();
  5283.  
  5284. #if 0
  5285. /* This isn't worth the time it takes.  */
  5286.   /* give back excess storage */
  5287.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  5288. #endif
  5289.  
  5290.   return defn;
  5291. }
  5292.  
  5293. static int
  5294. do_assert (buf, limit, op, keyword)
  5295.      U_CHAR *buf, *limit;
  5296.      FILE_BUF *op;
  5297.      struct directive *keyword;
  5298. {
  5299.   U_CHAR *bp;            /* temp ptr into input buffer */
  5300.   U_CHAR *symname;        /* remember where symbol name starts */
  5301.   int sym_length;        /* and how long it is */
  5302.   struct arglist *tokens = NULL;
  5303.  
  5304.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  5305.     pedwarn ("ANSI C does not allow `#assert'");
  5306.  
  5307.   bp = buf;
  5308.  
  5309.   while (is_hor_space[*bp])
  5310.     bp++;
  5311.  
  5312.   symname = bp;            /* remember where it starts */
  5313.   sym_length = check_macro_name (bp, "assertion");
  5314.   bp += sym_length;
  5315.   /* #define doesn't do this, but we should.  */
  5316.   SKIP_WHITE_SPACE (bp);
  5317.  
  5318.   /* Lossage will occur if identifiers or control tokens are broken
  5319.      across lines using backslash.  This is not the right place to take
  5320.      care of that. */
  5321.  
  5322.   if (*bp != '(') {
  5323.     error ("missing token-sequence in `#assert'");
  5324.     return 1;
  5325.   }
  5326.  
  5327.   {
  5328.     int error_flag = 0;
  5329.  
  5330.     bp++;            /* skip '(' */
  5331.     SKIP_WHITE_SPACE (bp);
  5332.  
  5333.     tokens = read_token_list (&bp, limit, &error_flag);
  5334.     if (error_flag)
  5335.       return 1;
  5336.     if (tokens == 0) {
  5337.       error ("empty token-sequence in `#assert'");
  5338.       return 1;
  5339.     }
  5340.  
  5341.     ++bp;            /* skip paren */
  5342.     SKIP_WHITE_SPACE (bp);
  5343.   }
  5344.  
  5345.   /* If this name isn't already an assertion name, make it one.
  5346.      Error if it was already in use in some other way.  */
  5347.  
  5348.   {
  5349.     ASSERTION_HASHNODE *hp;
  5350.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  5351.     struct tokenlist_list *value
  5352.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  5353.  
  5354.     hp = assertion_lookup (symname, sym_length, hashcode);
  5355.     if (hp == NULL) {
  5356.       if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
  5357.     error ("`defined' redefined as assertion");
  5358.       hp = assertion_install (symname, sym_length, hashcode);
  5359.     }
  5360.  
  5361.     /* Add the spec'd token-sequence to the list of such.  */
  5362.     value->tokens = tokens;
  5363.     value->next = hp->value;
  5364.     hp->value = value;
  5365.   }
  5366.  
  5367.   return 0;
  5368. }
  5369.  
  5370. static int
  5371. do_unassert (buf, limit, op, keyword)
  5372.      U_CHAR *buf, *limit;
  5373.      FILE_BUF *op;
  5374.      struct directive *keyword;
  5375. {
  5376.   U_CHAR *bp;            /* temp ptr into input buffer */
  5377.   U_CHAR *symname;        /* remember where symbol name starts */
  5378.   int sym_length;        /* and how long it is */
  5379.  
  5380.   struct arglist *tokens = NULL;
  5381.   int tokens_specified = 0;
  5382.  
  5383.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  5384.     pedwarn ("ANSI C does not allow `#unassert'");
  5385.  
  5386.   bp = buf;
  5387.  
  5388.   while (is_hor_space[*bp])
  5389.     bp++;
  5390.  
  5391.   symname = bp;            /* remember where it starts */
  5392.   sym_length = check_macro_name (bp, "assertion");
  5393.   bp += sym_length;
  5394.   /* #define doesn't do this, but we should.  */
  5395.   SKIP_WHITE_SPACE (bp);
  5396.  
  5397.   /* Lossage will occur if identifiers or control tokens are broken
  5398.      across lines using backslash.  This is not the right place to take
  5399.      care of that. */
  5400.  
  5401.   if (*bp == '(') {
  5402.     int error_flag = 0;
  5403.  
  5404.     bp++;            /* skip '(' */
  5405.     SKIP_WHITE_SPACE (bp);
  5406.  
  5407.     tokens = read_token_list (&bp, limit, &error_flag);
  5408.     if (error_flag)
  5409.       return 1;
  5410.     if (tokens == 0) {
  5411.       error ("empty token list in `#unassert'");
  5412.       return 1;
  5413.     }
  5414.  
  5415.     tokens_specified = 1;
  5416.  
  5417.     ++bp;            /* skip paren */
  5418.     SKIP_WHITE_SPACE (bp);
  5419.   }
  5420.  
  5421.   {
  5422.     ASSERTION_HASHNODE *hp;
  5423.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  5424.     struct tokenlist_list *tail, *prev;
  5425.  
  5426.     hp = assertion_lookup (symname, sym_length, hashcode);
  5427.     if (hp == NULL)
  5428.       return 1;
  5429.  
  5430.     /* If no token list was specified, then eliminate this assertion
  5431.        entirely.  */
  5432.     if (! tokens_specified) {
  5433.       struct tokenlist_list *next;
  5434.       for (tail = hp->value; tail; tail = next) {
  5435.     next = tail->next;
  5436.     free_token_list (tail->tokens);
  5437.     free (tail);
  5438.       }
  5439.       delete_assertion (hp);
  5440.     } else {
  5441.       /* If a list of tokens was given, then delete any matching list.  */
  5442.  
  5443.       tail = hp->value;
  5444.       prev = 0;
  5445.       while (tail) {
  5446.     struct tokenlist_list *next = tail->next;
  5447.     if (compare_token_lists (tail->tokens, tokens)) {
  5448.       if (prev)
  5449.         prev->next = next;
  5450.       else
  5451.         hp->value = tail->next;
  5452.       free_token_list (tail->tokens);
  5453.       free (tail);
  5454.     } else {
  5455.       prev = tail;
  5456.     }
  5457.     tail = next;
  5458.       }
  5459.     }
  5460.   }
  5461.  
  5462.   return 0;
  5463. }
  5464.  
  5465. /* Test whether there is an assertion named NAME
  5466.    and optionally whether it has an asserted token list TOKENS.
  5467.    NAME is not null terminated; its length is SYM_LENGTH.
  5468.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  5469.  
  5470. int
  5471. check_assertion (name, sym_length, tokens_specified, tokens)
  5472.      U_CHAR *name;
  5473.      int sym_length;
  5474.      int tokens_specified;
  5475.      struct arglist *tokens;
  5476. {
  5477.   ASSERTION_HASHNODE *hp;
  5478.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  5479.  
  5480.   if (pedantic && !instack[indepth].system_header_p)
  5481.     pedwarn ("ANSI C does not allow testing assertions");
  5482.  
  5483.   hp = assertion_lookup (name, sym_length, hashcode);
  5484.   if (hp == NULL)
  5485.     /* It is not an assertion; just return false.  */
  5486.     return 0;
  5487.  
  5488.   /* If no token list was specified, then value is 1.  */
  5489.   if (! tokens_specified)
  5490.     return 1;
  5491.  
  5492.   {
  5493.     struct tokenlist_list *tail;
  5494.  
  5495.     tail = hp->value;
  5496.  
  5497.     /* If a list of tokens was given,
  5498.        then succeed if the assertion records a matching list.  */
  5499.  
  5500.     while (tail) {
  5501.       if (compare_token_lists (tail->tokens, tokens))
  5502.     return 1;
  5503.       tail = tail->next;
  5504.     }
  5505.  
  5506.     /* Fail if the assertion has no matching list.  */
  5507.     return 0;
  5508.   }
  5509. }
  5510.  
  5511. /* Compare two lists of tokens for equality including order of tokens.  */
  5512.  
  5513. static int
  5514. compare_token_lists (l1, l2)
  5515.      struct arglist *l1, *l2;
  5516. {
  5517.   while (l1 && l2) {
  5518.     if (l1->length != l2->length)
  5519.       return 0;
  5520.     if (strncmp (l1->name, l2->name, l1->length))
  5521.       return 0;
  5522.     l1 = l1->next;
  5523.     l2 = l2->next;
  5524.   }
  5525.  
  5526.   /* Succeed if both lists end at the same time.  */
  5527.   return l1 == l2;
  5528. }
  5529.  
  5530. /* Read a space-separated list of tokens ending in a close parenthesis.
  5531.    Return a list of strings, in the order they were written.
  5532.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  5533.    Parse the text starting at *BPP, and update *BPP.
  5534.    Don't parse beyond LIMIT.  */
  5535.  
  5536. static struct arglist *
  5537. read_token_list (bpp, limit, error_flag)
  5538.      U_CHAR **bpp;
  5539.      U_CHAR *limit;
  5540.      int *error_flag;
  5541. {
  5542.   struct arglist *token_ptrs = 0;
  5543.   U_CHAR *bp = *bpp;
  5544.   int depth = 1;
  5545.  
  5546.   *error_flag = 0;
  5547.  
  5548.   /* Loop over the assertion value tokens.  */
  5549.   while (depth > 0) {
  5550.     struct arglist *temp;
  5551.     int eofp = 0;
  5552.     U_CHAR *beg = bp;
  5553.  
  5554.     /* Find the end of the token.  */
  5555.     if (*bp == '(') {
  5556.       bp++;
  5557.       depth++;
  5558.     } else if (*bp == ')') {
  5559.       depth--;
  5560.       if (depth == 0)
  5561.     break;
  5562.       bp++;
  5563.     } else if (*bp == '"' || *bp == '\'')
  5564.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  5565.     else
  5566.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  5567.          && *bp != '"' && *bp != '\'' && bp != limit)
  5568.     bp++;
  5569.  
  5570.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  5571.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  5572.     bcopy (beg, temp->name, bp - beg);
  5573.     temp->name[bp - beg] = 0;
  5574.     temp->next = token_ptrs;
  5575.     token_ptrs = temp;
  5576.     temp->length = bp - beg;
  5577.  
  5578.     SKIP_WHITE_SPACE (bp);
  5579.  
  5580.     if (bp >= limit) {
  5581.       error ("unterminated token sequence in `#assert' or `#unassert'");
  5582.       *error_flag = -1;
  5583.       return 0;
  5584.     }
  5585.   }
  5586.   *bpp = bp;
  5587.  
  5588.   /* We accumulated the names in reverse order.
  5589.      Now reverse them to get the proper order.  */
  5590.   {
  5591.     register struct arglist *prev = 0, *this, *next;
  5592.     for (this = token_ptrs; this; this = next) {
  5593.       next = this->next;
  5594.       this->next = prev;
  5595.       prev = this;
  5596.     }
  5597.     return prev;
  5598.   }
  5599. }
  5600.  
  5601. static void
  5602. free_token_list (tokens)
  5603.      struct arglist *tokens;
  5604. {
  5605.   while (tokens) {
  5606.     struct arglist *next = tokens->next;
  5607.     free (tokens->name);
  5608.     free (tokens);
  5609.     tokens = next;
  5610.   }
  5611. }
  5612.  
  5613. /*
  5614.  * Install a name in the assertion hash table.
  5615.  *
  5616.  * If LEN is >= 0, it is the length of the name.
  5617.  * Otherwise, compute the length by scanning the entire name.
  5618.  *
  5619.  * If HASH is >= 0, it is the precomputed hash code.
  5620.  * Otherwise, compute the hash code.
  5621.  */
  5622. static ASSERTION_HASHNODE *
  5623. assertion_install (name, len, hash)
  5624.      U_CHAR *name;
  5625.      int len;
  5626.      int hash;
  5627. {
  5628.   register ASSERTION_HASHNODE *hp;
  5629.   register int i, bucket;
  5630.   register U_CHAR *p, *q;
  5631.  
  5632.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  5633.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  5634.   bucket = hash;
  5635.   hp->bucket_hdr = &assertion_hashtab[bucket];
  5636.   hp->next = assertion_hashtab[bucket];
  5637.   assertion_hashtab[bucket] = hp;
  5638.   hp->prev = NULL;
  5639.   if (hp->next != NULL)
  5640.     hp->next->prev = hp;
  5641.   hp->length = len;
  5642.   hp->value = 0;
  5643.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  5644.   p = hp->name;
  5645.   q = name;
  5646.   for (i = 0; i < len; i++)
  5647.     *p++ = *q++;
  5648.   hp->name[len] = 0;
  5649.   return hp;
  5650. }
  5651.  
  5652. /*
  5653.  * find the most recent hash node for name name (ending with first
  5654.  * non-identifier char) installed by install
  5655.  *
  5656.  * If LEN is >= 0, it is the length of the name.
  5657.  * Otherwise, compute the length by scanning the entire name.
  5658.  *
  5659.  * If HASH is >= 0, it is the precomputed hash code.
  5660.  * Otherwise, compute the hash code.
  5661.  */
  5662. static ASSERTION_HASHNODE *
  5663. assertion_lookup (name, len, hash)
  5664.      U_CHAR *name;
  5665.      int len;
  5666.      int hash;
  5667. {
  5668.   register U_CHAR *bp;
  5669.   register ASSERTION_HASHNODE *bucket;
  5670.  
  5671.   bucket = assertion_hashtab[hash];
  5672.   while (bucket) {
  5673.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  5674.       return bucket;
  5675.     bucket = bucket->next;
  5676.   }
  5677.   return NULL;
  5678. }
  5679.  
  5680. static void
  5681. delete_assertion (hp)
  5682.      ASSERTION_HASHNODE *hp;
  5683. {
  5684.  
  5685.   if (hp->prev != NULL)
  5686.     hp->prev->next = hp->next;
  5687.   if (hp->next != NULL)
  5688.     hp->next->prev = hp->prev;
  5689.  
  5690.   /* make sure that the bucket chain header that
  5691.      the deleted guy was on points to the right thing afterwards. */
  5692.   if (hp == *hp->bucket_hdr)
  5693.     *hp->bucket_hdr = hp->next;
  5694.  
  5695.   free (hp);
  5696. }
  5697.  
  5698. /*
  5699.  * interpret #line command.  Remembers previously seen fnames
  5700.  * in its very own hash table.
  5701.  */
  5702. #define FNAME_HASHSIZE 37
  5703.  
  5704. static int
  5705. do_line (buf, limit, op, keyword)
  5706.      U_CHAR *buf, *limit;
  5707.      FILE_BUF *op;
  5708.      struct directive *keyword;
  5709. {
  5710.   register U_CHAR *bp;
  5711.   FILE_BUF *ip = &instack[indepth];
  5712.   FILE_BUF tem;
  5713.   int new_lineno;
  5714.   enum file_change_code file_change = same_file;
  5715.  
  5716.   /* Expand any macros.  */
  5717.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  5718.  
  5719.   /* Point to macroexpanded line, which is null-terminated now.  */
  5720.   bp = tem.buf;
  5721.   SKIP_WHITE_SPACE (bp);
  5722.  
  5723.   if (!isdigit (*bp)) {
  5724.     error ("invalid format `#line' command");
  5725.     return 0;
  5726.   }
  5727.  
  5728.   /* The Newline at the end of this line remains to be processed.
  5729.      To put the next line at the specified line number,
  5730.      we must store a line number now that is one less.  */
  5731.   new_lineno = atoi (bp) - 1;
  5732.  
  5733.   /* skip over the line number.  */
  5734.   while (isdigit (*bp))
  5735.     bp++;
  5736.  
  5737. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  5738.   if (*bp && !is_space[*bp]) {
  5739.     error ("invalid format `#line' command");
  5740.     return;
  5741.   }
  5742. #endif
  5743.  
  5744.   SKIP_WHITE_SPACE (bp);
  5745.  
  5746.   if (*bp == '\"') {
  5747.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  5748.     HASHNODE *hp, **hash_bucket;
  5749.     U_CHAR *fname;
  5750.     int fname_length;
  5751.  
  5752.     fname = ++bp;
  5753.  
  5754.     while (*bp && *bp != '\"')
  5755.       bp++;
  5756.     if (*bp != '\"') {
  5757.       error ("invalid format `#line' command");
  5758.       return 0;
  5759.     }
  5760.  
  5761.     fname_length = bp - fname;
  5762.  
  5763.     bp++;
  5764.     SKIP_WHITE_SPACE (bp);
  5765.     if (*bp) {
  5766.       if (*bp == '1')
  5767.     file_change = enter_file;
  5768.       else if (*bp == '2')
  5769.     file_change = leave_file;
  5770.       else if (*bp == '3')
  5771.     ip->system_header_p = 1;
  5772.       else {
  5773.     error ("invalid format `#line' command");
  5774.     return 0;
  5775.       }
  5776.  
  5777.       bp++;
  5778.       SKIP_WHITE_SPACE (bp);
  5779.       if (*bp == '3') {
  5780.     ip->system_header_p = 1;
  5781.     bp++;
  5782.     SKIP_WHITE_SPACE (bp);
  5783.       }
  5784.       if (*bp) {
  5785.     error ("invalid format `#line' command");
  5786.     return 0;
  5787.       }
  5788.     }
  5789.  
  5790.     hash_bucket =
  5791.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  5792.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  5793.       if (hp->length == fname_length &&
  5794.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  5795.     ip->nominal_fname = hp->value.cpval;
  5796.     break;
  5797.       }
  5798.     if (hp == 0) {
  5799.       /* Didn't find it; cons up a new one.  */
  5800.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  5801.       hp->next = *hash_bucket;
  5802.       *hash_bucket = hp;
  5803.  
  5804.       hp->length = fname_length;
  5805.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  5806.       bcopy (fname, hp->value.cpval, fname_length);
  5807.     }
  5808.   } else if (*bp) {
  5809.     error ("invalid format `#line' command");
  5810.     return 0;
  5811.   }
  5812.  
  5813.   ip->lineno = new_lineno;
  5814.   output_line_command (ip, op, 0, file_change);
  5815.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  5816.   return 0;
  5817. }
  5818.  
  5819. /*
  5820.  * remove the definition of a symbol from the symbol table.
  5821.  * according to un*x /lib/cpp, it is not an error to undef
  5822.  * something that has no definitions, so it isn't one here either.
  5823.  */
  5824.  
  5825. static int
  5826. do_undef (buf, limit, op, keyword)
  5827.      U_CHAR *buf, *limit;
  5828.      FILE_BUF *op;
  5829.      struct directive *keyword;
  5830. {
  5831.   int sym_length;
  5832.   HASHNODE *hp;
  5833.   U_CHAR *orig_buf = buf;
  5834.  
  5835.   /* If this is a precompiler run (with -pcp) pass thru #undef commands.  */
  5836.   if (pcp_outfile && op)
  5837.     pass_thru_directive (buf, limit, op, keyword);
  5838.  
  5839.   SKIP_WHITE_SPACE (buf);
  5840.   sym_length = check_macro_name (buf, "macro");
  5841.  
  5842.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  5843.     /* If we are generating additional info for debugging (with -g) we
  5844.        need to pass through all effective #undef commands.  */
  5845.     if (debug_output && op)
  5846.       pass_thru_directive (orig_buf, limit, op, keyword);
  5847.     if (hp->type != T_MACRO)
  5848.       warning ("undefining `%s'", hp->name);
  5849.     delete_macro (hp);
  5850.   }
  5851.  
  5852.   if (pedantic) {
  5853.     buf += sym_length;
  5854.     SKIP_WHITE_SPACE (buf);
  5855.     if (buf != limit)
  5856.       pedwarn ("garbage after `#undef' directive");
  5857.   }
  5858.   return 0;
  5859. }
  5860.  
  5861. /*
  5862.  * Report a fatal error detected by the program we are processing.
  5863.  * Use the text of the line in the error message, then terminate.
  5864.  * (We use error() because it prints the filename & line#.)
  5865.  */
  5866.  
  5867. static int
  5868. do_error (buf, limit, op, keyword)
  5869.      U_CHAR *buf, *limit;
  5870.      FILE_BUF *op;
  5871.      struct directive *keyword;
  5872. {
  5873.   int length = limit - buf;
  5874.   char *copy = (char *) xmalloc (length + 1);
  5875.   bcopy (buf, copy, length);
  5876.   copy[length] = 0;
  5877.   SKIP_WHITE_SPACE (copy);
  5878.   error ("#error %s", copy);
  5879.   exit (FAILURE_EXIT_CODE);
  5880.   /* NOTREACHED */
  5881.   return 0;
  5882. }
  5883.  
  5884. /*
  5885.  * Report a warning detected by the program we are processing.
  5886.  * Use the text of the line in the warning message, then continue.
  5887.  * (We use error() because it prints the filename & line#.)
  5888.  */
  5889.  
  5890. static int
  5891. do_warning (buf, limit, op, keyword)
  5892.      U_CHAR *buf, *limit;
  5893.      FILE_BUF *op;
  5894.      struct directive *keyword;
  5895. {
  5896.   int length = limit - buf;
  5897.   char *copy = (char *) xmalloc (length + 1);
  5898.   bcopy (buf, copy, length);
  5899.   copy[length] = 0;
  5900.   SKIP_WHITE_SPACE (copy);
  5901.   warning ("#warning %s", copy);
  5902.   return 0;
  5903. }
  5904.  
  5905. /* Remember the name of the current file being read from so that we can
  5906.    avoid ever including it again.  */
  5907.  
  5908. static int
  5909. do_once ()
  5910. {
  5911.   int i;
  5912.   FILE_BUF *ip = NULL;
  5913.  
  5914.   for (i = indepth; i >= 0; i--)
  5915.     if (instack[i].fname != NULL) {
  5916.       ip = &instack[i];
  5917.       break;
  5918.     }
  5919.  
  5920.   if (ip != NULL) {
  5921.     struct file_name_list *new;
  5922.     
  5923.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  5924.     new->next = dont_repeat_files;
  5925.     dont_repeat_files = new;
  5926.     new->fname = savestring (ip->fname);
  5927.     new->control_macro = 0;
  5928.   }
  5929.   return 0;
  5930. }
  5931.  
  5932. /* #ident has already been copied to the output file, so just ignore it.  */
  5933.  
  5934. static int
  5935. do_ident (buf, limit)
  5936.      U_CHAR *buf, *limit;
  5937. {
  5938.   /* Allow #ident in system headers, since that's not user's fault.  */
  5939.   if (pedantic && !instack[indepth].system_header_p)
  5940.     pedwarn ("ANSI C does not allow `#ident'");
  5941.   return 0;
  5942. }
  5943.  
  5944. /* #pragma and its argument line have already been copied to the output file.
  5945.    Just check for some recognized pragmas that need validation here.  */
  5946.  
  5947. static int
  5948. do_pragma (buf, limit)
  5949.      U_CHAR *buf, *limit;
  5950. {
  5951.   while (*buf == ' ' || *buf == '\t')
  5952.     buf++;
  5953.   if (!strncmp (buf, "once", 4)) {
  5954.     /* Allow #pragma once in system headers, since that's not the user's
  5955.        fault.  */
  5956.     if (!instack[indepth].system_header_p)
  5957.       warning ("`#pragma once' is obsolete");
  5958.     do_once ();
  5959.   }
  5960.  
  5961.   if (!strncmp (buf, "implementation", 14)) {
  5962.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  5963.        been included yet.  */
  5964.     struct file_name_list *ptr;
  5965.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  5966.     SKIP_WHITE_SPACE (p);
  5967.     if (*p == '\n' || *p != '\"')
  5968.       return 0;
  5969.  
  5970.     fname = p + 1;
  5971.     if (p = (U_CHAR *) index (fname, '\"'))
  5972.       *p = '\0';
  5973.     
  5974.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  5975.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  5976.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  5977.       if (inc_fname && !strcmp (inc_fname, fname))
  5978.     warning ("`#pragma implementation' for `%s' appears after file is included",
  5979.          fname);
  5980.     }
  5981.   }
  5982.  
  5983.   return 0;
  5984. }
  5985.  
  5986. #if 0
  5987. /* This was a fun hack, but #pragma seems to start to be useful.
  5988.    By failing to recognize it, we pass it through unchanged to cc1.  */
  5989.  
  5990. /*
  5991.  * the behavior of the #pragma directive is implementation defined.
  5992.  * this implementation defines it as follows.
  5993.  */
  5994.  
  5995. static int
  5996. do_pragma ()
  5997. {
  5998.   close (0);
  5999.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  6000.     goto nope;
  6001.   close (1);
  6002.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  6003.     goto nope;
  6004.   execl ("/usr/games/hack", "#pragma", 0);
  6005.   execl ("/usr/games/rogue", "#pragma", 0);
  6006.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  6007.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  6008. nope:
  6009.   fatal ("You are in a maze of twisty compiler features, all different");
  6010. }
  6011. #endif
  6012.  
  6013. /* Just ignore #sccs, on systems where we define it at all.  */
  6014.  
  6015. static int
  6016. do_sccs ()
  6017. {
  6018.   if (pedantic)
  6019.     pedwarn ("ANSI C does not allow `#sccs'");
  6020.   return 0;
  6021. }
  6022.  
  6023. /*
  6024.  * handle #if command by
  6025.  *   1) inserting special `defined' keyword into the hash table
  6026.  *    that gets turned into 0 or 1 by special_symbol (thus,
  6027.  *    if the luser has a symbol called `defined' already, it won't
  6028.  *      work inside the #if command)
  6029.  *   2) rescan the input into a temporary output buffer
  6030.  *   3) pass the output buffer to the yacc parser and collect a value
  6031.  *   4) clean up the mess left from steps 1 and 2.
  6032.  *   5) call conditional_skip to skip til the next #endif (etc.),
  6033.  *      or not, depending on the value from step 3.
  6034.  */
  6035.  
  6036. static int
  6037. do_if (buf, limit, op, keyword)
  6038.      U_CHAR *buf, *limit;
  6039.      FILE_BUF *op;
  6040.      struct directive *keyword;
  6041. {
  6042.   int value;
  6043.   FILE_BUF *ip = &instack[indepth];
  6044.  
  6045.   value = eval_if_expression (buf, limit - buf);
  6046.   conditional_skip (ip, value == 0, T_IF, NULL_PTR);
  6047.   return 0;
  6048. }
  6049.  
  6050. /*
  6051.  * handle a #elif directive by not changing  if_stack  either.
  6052.  * see the comment above do_else.
  6053.  */
  6054.  
  6055. static int
  6056. do_elif (buf, limit, op, keyword)
  6057.      U_CHAR *buf, *limit;
  6058.      FILE_BUF *op;
  6059.      struct directive *keyword;
  6060. {
  6061.   int value;
  6062.   FILE_BUF *ip = &instack[indepth];
  6063.  
  6064.   if (if_stack == instack[indepth].if_stack) {
  6065.     error ("`#elif' not within a conditional");
  6066.     return 0;
  6067.   } else {
  6068.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  6069.       error ("`#elif' after `#else'");
  6070.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  6071.       if (if_stack->fname != NULL && ip->fname != NULL &&
  6072.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  6073.     fprintf (stderr, ", file %s", if_stack->fname);
  6074.       fprintf (stderr, ")\n");
  6075.     }
  6076.     if_stack->type = T_ELIF;
  6077.   }
  6078.  
  6079.   if (if_stack->if_succeeded)
  6080.     skip_if_group (ip, 0);
  6081.   else {
  6082.     value = eval_if_expression (buf, limit - buf);
  6083.     if (value == 0)
  6084.       skip_if_group (ip, 0);
  6085.     else {
  6086.       ++if_stack->if_succeeded;    /* continue processing input */
  6087.       output_line_command (ip, op, 1, same_file);
  6088.     }
  6089.   }
  6090.   return 0;
  6091. }
  6092.  
  6093. /*
  6094.  * evaluate a #if expression in BUF, of length LENGTH,
  6095.  * then parse the result as a C expression and return the value as an int.
  6096.  */
  6097. static int
  6098. eval_if_expression (buf, length)
  6099.      U_CHAR *buf;
  6100.      int length;
  6101. {
  6102.   FILE_BUF temp_obuf;
  6103.   HASHNODE *save_defined;
  6104.   int value;
  6105.  
  6106.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
  6107.   pcp_inside_if = 1;
  6108.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  6109.   pcp_inside_if = 0;
  6110.   delete_macro (save_defined);    /* clean up special symbol */
  6111.  
  6112.   value = parse_c_expression (temp_obuf.buf);
  6113.  
  6114.   free (temp_obuf.buf);
  6115.  
  6116.   return value;
  6117. }
  6118.  
  6119. /*
  6120.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  6121.  * then do or don't skip to the #endif/#else/#elif depending
  6122.  * on what directive is actually being processed.
  6123.  */
  6124.  
  6125. static int
  6126. do_xifdef (buf, limit, op, keyword)
  6127.      U_CHAR *buf, *limit;
  6128.      FILE_BUF *op;
  6129.      struct directive *keyword;
  6130. {
  6131.   int skip;
  6132.   FILE_BUF *ip = &instack[indepth];
  6133.   U_CHAR *end; 
  6134.   int start_of_file = 0;
  6135.   U_CHAR *control_macro = 0;
  6136.  
  6137.   /* Detect a #ifndef at start of file (not counting comments).  */
  6138.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  6139.     U_CHAR *p = ip->buf;
  6140.     while (p != directive_start) {
  6141.       char c = *p++;
  6142.       if (is_space[c])
  6143.     ;
  6144.       else if (c == '/' && p != ip->bufp && *p == '*') {
  6145.     /* Skip this comment.  */
  6146.     int junk;
  6147.     U_CHAR *save_bufp = ip->bufp;
  6148.     ip->bufp = p + 1;
  6149.     p = skip_to_end_of_comment (ip, &junk, 1);
  6150.     ip->bufp = save_bufp;
  6151.       } else {
  6152.     goto fail;
  6153.       }
  6154.     }
  6155.     /* If we get here, this conditional is the beginning of the file.  */
  6156.     start_of_file = 1;
  6157.   fail: ;
  6158.   }
  6159.  
  6160.   /* Discard leading and trailing whitespace.  */
  6161.   SKIP_WHITE_SPACE (buf);
  6162.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  6163.  
  6164.   /* Find the end of the identifier at the beginning.  */
  6165.   for (end = buf; is_idchar[*end]; end++);
  6166.  
  6167.   if (end == buf) {
  6168.     skip = (keyword->type == T_IFDEF);
  6169.     if (! traditional)
  6170.       pedwarn (end == limit ? "`#%s' with no argument"
  6171.            : "`#%s' argument starts with punctuation",
  6172.            keyword->name);
  6173.   } else {
  6174.     HASHNODE *hp;
  6175.  
  6176.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  6177.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  6178.     else if (end != limit && !traditional)
  6179.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  6180.  
  6181.     hp = lookup (buf, end-buf, -1);
  6182.  
  6183.     if (pcp_outfile) {
  6184.       /* Output a precondition for this macro.  */
  6185.       if (hp && hp->value.defn->predefined)
  6186.     fprintf(pcp_outfile, "#define %s\n", hp->name);
  6187.       else {
  6188.     U_CHAR *cp = buf;
  6189.     fprintf(pcp_outfile, "#undef ");
  6190.     while (is_idchar[*cp]) /* Ick! */
  6191.       fputc (*cp++, pcp_outfile);
  6192.     putc ('\n', pcp_outfile);
  6193.       }
  6194.     }
  6195.  
  6196.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  6197.     if (start_of_file && !skip) {
  6198.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  6199.       bcopy (buf, control_macro, end - buf);
  6200.       control_macro[end - buf] = 0;
  6201.     }
  6202.   }
  6203.   
  6204.   conditional_skip (ip, skip, T_IF, control_macro);
  6205.   return 0;
  6206. }
  6207.  
  6208. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  6209.    If this is a #ifndef starting at the beginning of a file,
  6210.    CONTROL_MACRO is the macro name tested by the #ifndef.
  6211.    Otherwise, CONTROL_MACRO is 0.  */
  6212.  
  6213. static void
  6214. conditional_skip (ip, skip, type, control_macro)
  6215.      FILE_BUF *ip;
  6216.      int skip;
  6217.      enum node_type type;
  6218.      U_CHAR *control_macro;
  6219. {
  6220.   IF_STACK_FRAME *temp;
  6221.  
  6222.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  6223.   temp->fname = ip->nominal_fname;
  6224.   temp->lineno = ip->lineno;
  6225.   temp->next = if_stack;
  6226.   temp->control_macro = control_macro;
  6227.   if_stack = temp;
  6228.  
  6229.   if_stack->type = type;
  6230.  
  6231.   if (skip != 0) {
  6232.     skip_if_group (ip, 0);
  6233.     return;
  6234.   } else {
  6235.     ++if_stack->if_succeeded;
  6236.     output_line_command (ip, &outbuf, 1, same_file);
  6237.   }
  6238. }
  6239.  
  6240. /*
  6241.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  6242.  * leaves input ptr at the sharp sign found.
  6243.  * If ANY is nonzero, return at next directive of any sort.
  6244.  */
  6245. static void
  6246. skip_if_group (ip, any)
  6247.      FILE_BUF *ip;
  6248.      int any;
  6249. {
  6250.   register U_CHAR *bp = ip->bufp, *cp;
  6251.   register U_CHAR *endb = ip->buf + ip->length;
  6252.   struct directive *kt;
  6253.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  6254.   U_CHAR *beg_of_line = bp;
  6255.   register int ident_length;
  6256.   U_CHAR *ident, *after_ident;
  6257.  
  6258.   while (bp < endb) {
  6259.     switch (*bp++) {
  6260.     case '/':            /* possible comment */
  6261.       if (*bp == '\\' && bp[1] == '\n')
  6262.     newline_fix (bp);
  6263.       if (*bp == '*'
  6264.       || (cplusplus_comments && *bp == '/')) {
  6265.     ip->bufp = ++bp;
  6266.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  6267.       }
  6268.       break;
  6269.     case '\"':
  6270.     case '\'':
  6271.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  6272.                    NULL_PTR, NULL_PTR);
  6273.       break;
  6274.     case '\\':
  6275.       /* Char after backslash loses its special meaning.  */
  6276.       if (bp < endb) {
  6277.     if (*bp == '\n')
  6278.       ++ip->lineno;        /* But do update the line-count.  */
  6279.     bp++;
  6280.       }
  6281.       break;
  6282.     case '\n':
  6283.       ++ip->lineno;
  6284.       beg_of_line = bp;
  6285.       break;
  6286.     case '#':
  6287.       ip->bufp = bp - 1;
  6288.  
  6289.       /* # keyword: a # must be first nonblank char on the line */
  6290.       if (beg_of_line == 0)
  6291.     break;
  6292.       /* Scan from start of line, skipping whitespace, comments
  6293.      and backslash-newlines, and see if we reach this #.
  6294.      If not, this # is not special.  */
  6295.       bp = beg_of_line;
  6296.       while (1) {
  6297.     if (is_hor_space[*bp])
  6298.       bp++;
  6299.     else if (*bp == '\\' && bp[1] == '\n')
  6300.       bp += 2;
  6301.     else if (*bp == '/' && bp[1] == '*') {
  6302.       bp += 2;
  6303.       while (!(*bp == '*' && bp[1] == '/'))
  6304.         bp++;
  6305.       bp += 2;
  6306.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  6307.       bp += 2;
  6308.       while (*bp++ != '\n') ;
  6309.         }
  6310.     else break;
  6311.       }
  6312.       if (bp != ip->bufp) {
  6313.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  6314.     break;
  6315.       }
  6316.  
  6317.       bp = ip->bufp + 1;    /* Point after the '#' */
  6318.  
  6319.       /* Skip whitespace and \-newline.  */
  6320.       while (1) {
  6321.     if (is_hor_space[*bp])
  6322.       bp++;
  6323.     else if (*bp == '\\' && bp[1] == '\n')
  6324.       bp += 2;
  6325.     else if (*bp == '/' && bp[1] == '*') {
  6326.       bp += 2;
  6327.       while (!(*bp == '*' && bp[1] == '/')) {
  6328.         if (*bp == '\n')
  6329.           ip->lineno++;
  6330.         bp++;
  6331.       }
  6332.       bp += 2;
  6333.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  6334.       bp += 2;
  6335.       while (*bp++ != '\n') ;
  6336.         }
  6337.     else break;
  6338.       }
  6339.  
  6340.       cp = bp;
  6341.  
  6342.       /* Now find end of directive name.
  6343.      If we encounter a backslash-newline, exchange it with any following
  6344.      symbol-constituents so that we end up with a contiguous name.  */
  6345.  
  6346.       while (1) {
  6347.     if (is_idchar[*bp])
  6348.       bp++;
  6349.     else {
  6350.       if (*bp == '\\' && bp[1] == '\n')
  6351.         name_newline_fix (bp);
  6352.       if (is_idchar[*bp])
  6353.         bp++;
  6354.       else break;
  6355.     }
  6356.       }
  6357.       ident_length = bp - cp;
  6358.       ident = cp;
  6359.       after_ident = bp;
  6360.  
  6361.       /* A line of just `#' becomes blank.  */
  6362.  
  6363.       if (ident_length == 0 && *after_ident == '\n') {
  6364.     continue;
  6365.       }
  6366.  
  6367.       if (ident_length == 0 || !is_idstart[*ident]) {
  6368.     U_CHAR *p = ident;
  6369.     while (is_idchar[*p]) {
  6370.       if (*p < '0' || *p > '9')
  6371.         break;
  6372.       p++;
  6373.     }
  6374.     /* Handle # followed by a line number.  */
  6375.     if (p != ident && !is_idchar[*p]) {
  6376.       if (pedantic)
  6377.         pedwarn ("`#' followed by integer");
  6378.       continue;
  6379.     }
  6380.  
  6381.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  6382.     if (p == ident) {
  6383.       while (*p == '#' || is_hor_space[*p]) p++;
  6384.       if (*p == '\n') {
  6385.         if (pedantic && !lang_asm)
  6386.           pedwarn ("invalid preprocessor directive");
  6387.         continue;
  6388.       }
  6389.     }
  6390.  
  6391.     if (!lang_asm && pedantic)
  6392.       pedwarn ("invalid preprocessor directive name");
  6393.     continue;
  6394.       }
  6395.  
  6396.       for (kt = directive_table; kt->length >= 0; kt++) {
  6397.     IF_STACK_FRAME *temp;
  6398.     if (ident_length == kt->length
  6399.         && strncmp (cp, kt->name, kt->length) == 0) {
  6400.       /* If we are asked to return on next directive, do so now.  */
  6401.       if (any)
  6402.         return;
  6403.  
  6404.       switch (kt->type) {
  6405.       case T_IF:
  6406.       case T_IFDEF:
  6407.       case T_IFNDEF:
  6408.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  6409.         temp->next = if_stack;
  6410.         if_stack = temp;
  6411.         temp->lineno = ip->lineno;
  6412.         temp->fname = ip->nominal_fname;
  6413.         temp->type = kt->type;
  6414.         break;
  6415.       case T_ELSE:
  6416.       case T_ENDIF:
  6417.         if (pedantic && if_stack != save_if_stack)
  6418.           validate_else (bp);
  6419.       case T_ELIF:
  6420.         if (if_stack == instack[indepth].if_stack) {
  6421.           error ("`#%s' not within a conditional", kt->name);
  6422.           break;
  6423.         }
  6424.         else if (if_stack == save_if_stack)
  6425.           return;        /* found what we came for */
  6426.  
  6427.         if (kt->type != T_ENDIF) {
  6428.           if (if_stack->type == T_ELSE)
  6429.         error ("`#else' or `#elif' after `#else'");
  6430.           if_stack->type = kt->type;
  6431.           break;
  6432.         }
  6433.  
  6434.         temp = if_stack;
  6435.         if_stack = if_stack->next;
  6436.         free (temp);
  6437.         break;
  6438.       }
  6439.       break;
  6440.     }
  6441.       }
  6442.       /* Don't let erroneous code go by.  */
  6443.       if (kt->length < 0 && !lang_asm && pedantic)
  6444.     pedwarn ("invalid preprocessor directive name");
  6445.     }
  6446.   }
  6447.   ip->bufp = bp;
  6448.   /* after this returns, rescan will exit because ip->bufp
  6449.      now points to the end of the buffer.
  6450.      rescan is responsible for the error message also.  */
  6451. }
  6452.  
  6453. /*
  6454.  * handle a #else directive.  Do this by just continuing processing
  6455.  * without changing  if_stack ;  this is so that the error message
  6456.  * for missing #endif's etc. will point to the original #if.  It
  6457.  * is possible that something different would be better.
  6458.  */
  6459.  
  6460. static int
  6461. do_else (buf, limit, op, keyword)
  6462.      U_CHAR *buf, *limit;
  6463.      FILE_BUF *op;
  6464.      struct directive *keyword;
  6465. {
  6466.   FILE_BUF *ip = &instack[indepth];
  6467.  
  6468.   if (pedantic) {
  6469.     SKIP_WHITE_SPACE (buf);
  6470.     if (buf != limit)
  6471.       pedwarn ("text following `#else' violates ANSI standard");
  6472.   }
  6473.  
  6474.   if (if_stack == instack[indepth].if_stack) {
  6475.     error ("`#else' not within a conditional");
  6476.     return 0;
  6477.   } else {
  6478.     /* #ifndef can't have its special treatment for containing the whole file
  6479.        if it has a #else clause.  */
  6480.     if_stack->control_macro = 0;
  6481.  
  6482.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  6483.       error ("`#else' after `#else'");
  6484.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  6485.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  6486.     fprintf (stderr, ", file %s", if_stack->fname);
  6487.       fprintf (stderr, ")\n");
  6488.     }
  6489.     if_stack->type = T_ELSE;
  6490.   }
  6491.  
  6492.   if (if_stack->if_succeeded)
  6493.     skip_if_group (ip, 0);
  6494.   else {
  6495.     ++if_stack->if_succeeded;    /* continue processing input */
  6496.     output_line_command (ip, op, 1, same_file);
  6497.   }
  6498.   return 0;
  6499. }
  6500.  
  6501. /*
  6502.  * unstack after #endif command
  6503.  */
  6504.  
  6505. static int
  6506. do_endif (buf, limit, op, keyword)
  6507.      U_CHAR *buf, *limit;
  6508.      FILE_BUF *op;
  6509.      struct directive *keyword;
  6510. {
  6511.   if (pedantic) {
  6512.     SKIP_WHITE_SPACE (buf);
  6513.     if (buf != limit)
  6514.       pedwarn ("text following `#endif' violates ANSI standard");
  6515.   }
  6516.  
  6517.   if (if_stack == instack[indepth].if_stack)
  6518.     error ("unbalanced `#endif'");
  6519.   else {
  6520.     IF_STACK_FRAME *temp = if_stack;
  6521.     if_stack = if_stack->next;
  6522.     if (temp->control_macro != 0) {
  6523.       /* This #endif matched a #ifndef at the start of the file.
  6524.      See if it is at the end of the file.  */
  6525.       FILE_BUF *ip = &instack[indepth];
  6526.       U_CHAR *p = ip->bufp;
  6527.       U_CHAR *ep = ip->buf + ip->length;
  6528.  
  6529.       while (p != ep) {
  6530.     U_CHAR c = *p++;
  6531.     switch (c) {
  6532.     case ' ':
  6533.     case '\t':
  6534.     case '\n':
  6535.       break;
  6536.     case '/':
  6537.       if (p != ep && *p == '*') {
  6538.         /* Skip this comment.  */
  6539.         int junk;
  6540.         U_CHAR *save_bufp = ip->bufp;
  6541.         ip->bufp = p + 1;
  6542.         p = skip_to_end_of_comment (ip, &junk, 1);
  6543.         ip->bufp = save_bufp;
  6544.       }
  6545.       break;
  6546.     default:
  6547.       goto fail;
  6548.     }
  6549.       }
  6550.       /* If we get here, this #endif ends a #ifndef
  6551.      that contains all of the file (aside from whitespace).
  6552.      Arrange not to include the file again
  6553.      if the macro that was tested is defined.  */
  6554.       if (indepth != 0)
  6555.     record_control_macro (ip->fname, temp->control_macro);
  6556.     fail: ;
  6557.     }
  6558.     free (temp);
  6559.     output_line_command (&instack[indepth], op, 1, same_file);
  6560.   }
  6561.   return 0;
  6562. }
  6563.  
  6564. /* When an #else or #endif is found while skipping failed conditional,
  6565.    if -pedantic was specified, this is called to warn about text after
  6566.    the command name.  P points to the first char after the command name.  */
  6567.  
  6568. static void
  6569. validate_else (p)
  6570.      register U_CHAR *p;
  6571. {
  6572.   /* Advance P over whitespace and comments.  */
  6573.   while (1) {
  6574.     if (*p == '\\' && p[1] == '\n')
  6575.       p += 2;
  6576.     if (is_hor_space[*p])
  6577.       p++;
  6578.     else if (*p == '/') {
  6579.       if (p[1] == '\\' && p[2] == '\n')
  6580.     newline_fix (p + 1);
  6581.       if (p[1] == '*') {
  6582.     p += 2;
  6583.     /* Don't bother warning about unterminated comments
  6584.        since that will happen later.  Just be sure to exit.  */
  6585.     while (*p) {
  6586.       if (p[1] == '\\' && p[2] == '\n')
  6587.         newline_fix (p + 1);
  6588.       if (*p == '*' && p[1] == '/') {
  6589.         p += 2;
  6590.         break;
  6591.       }
  6592.       p++;
  6593.     }
  6594.       }
  6595.       else if (cplusplus_comments && p[1] == '/') {
  6596.     p += 2;
  6597.     while (*p && *p++ != '\n') ;
  6598.       }
  6599.     } else break;
  6600.   }
  6601.   if (*p && *p != '\n')
  6602.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  6603. }
  6604.  
  6605. /* Skip a comment, assuming the input ptr immediately follows the
  6606.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  6607.    (The canonical line counter is &ip->lineno.)
  6608.    Don't use this routine (or the next one) if bumping the line
  6609.    counter is not sufficient to deal with newlines in the string.
  6610.  
  6611.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  6612.    This feature is useful when processing a comment that is going to be
  6613.    processed or was processed at another point in the preprocessor,
  6614.    to avoid a duplicate warning.  */
  6615. static U_CHAR *
  6616. skip_to_end_of_comment (ip, line_counter, nowarn)
  6617.      register FILE_BUF *ip;
  6618.      int *line_counter;        /* place to remember newlines, or NULL */
  6619.      int nowarn;
  6620. {
  6621.   register U_CHAR *limit = ip->buf + ip->length;
  6622.   register U_CHAR *bp = ip->bufp;
  6623.   FILE_BUF *op = &outbuf;    /* JF */
  6624.   int output = put_out_comments && !line_counter;
  6625.  
  6626.     /* JF this line_counter stuff is a crock to make sure the
  6627.        comment is only put out once, no matter how many times
  6628.        the comment is skipped.  It almost works */
  6629.   if (output) {
  6630.     *op->bufp++ = '/';
  6631.     *op->bufp++ = '*';
  6632.   }
  6633.   if (cplusplus_comments && bp[-1] == '/') {
  6634.     if (output) {
  6635.       while (bp < limit)
  6636.     if ((*op->bufp++ = *bp++) == '\n') {
  6637.       bp--;
  6638.       break;
  6639.     }
  6640.       op->bufp[-1] = '*';
  6641.       *op->bufp++ = '/';
  6642.       *op->bufp++ = '\n';
  6643.     } else {
  6644.       while (bp < limit) {
  6645.     if (*bp++ == '\n') {
  6646.       bp--;
  6647.       break;
  6648.     }
  6649.       }
  6650.     }
  6651.     ip->bufp = bp;
  6652.     return bp;
  6653.   }
  6654.   while (bp < limit) {
  6655.     if (output)
  6656.       *op->bufp++ = *bp;
  6657.     switch (*bp++) {
  6658.     case '/':
  6659.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  6660.     warning ("`/*' within comment");
  6661.       break;
  6662.     case '\n':
  6663.       if (line_counter != NULL)
  6664.     ++*line_counter;
  6665.       if (output)
  6666.     ++op->lineno;
  6667.       break;
  6668.     case '*':
  6669.       if (*bp == '\\' && bp[1] == '\n')
  6670.     newline_fix (bp);
  6671.       if (*bp == '/') {
  6672.         if (output)
  6673.       *op->bufp++ = '/';
  6674.     ip->bufp = ++bp;
  6675.     return bp;
  6676.       }
  6677.       break;
  6678.     }
  6679.   }
  6680.   ip->bufp = bp;
  6681.   return bp;
  6682. }
  6683.  
  6684. /*
  6685.  * Skip over a quoted string.  BP points to the opening quote.
  6686.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  6687.  * START_LINE is the line number of the starting point (but it need
  6688.  * not be valid if the starting point is inside a macro expansion).
  6689.  *
  6690.  * The input stack state is not changed.
  6691.  *
  6692.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  6693.  * for each newline passed.
  6694.  *
  6695.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  6696.  * if we pass a backslash-newline.
  6697.  *
  6698.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  6699.  */
  6700. static U_CHAR *
  6701. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  6702.      register U_CHAR *bp;
  6703.      register U_CHAR *limit;
  6704.      int start_line;
  6705.      int *count_newlines;
  6706.      int *backslash_newlines_p;
  6707.      int *eofp;
  6708. {
  6709.   register U_CHAR c, match;
  6710.  
  6711.   match = *bp++;
  6712.   while (1) {
  6713.     if (bp >= limit) {
  6714.       error_with_line (line_for_error (start_line),
  6715.                "unterminated string or character constant");
  6716.       if (eofp)
  6717.     *eofp = 1;
  6718.       break;
  6719.     }
  6720.     c = *bp++;
  6721.     if (c == '\\') {
  6722.       while (*bp == '\\' && bp[1] == '\n') {
  6723.     if (backslash_newlines_p)
  6724.       *backslash_newlines_p = 1;
  6725.     if (count_newlines)
  6726.       ++*count_newlines;
  6727.     bp += 2;
  6728.       }
  6729.       if (*bp == '\n' && count_newlines) {
  6730.     if (backslash_newlines_p)
  6731.       *backslash_newlines_p = 1;
  6732.     ++*count_newlines;
  6733.       }
  6734.       bp++;
  6735.     } else if (c == '\n') {
  6736.       if (traditional) {
  6737.      /* Unterminated strings and character constants are 'legal'.  */
  6738.      bp--;    /* Don't consume the newline. */
  6739.      if (eofp)
  6740.        *eofp = 1;
  6741.      break;
  6742.       }
  6743.       if (match == '\'') {
  6744.     error_with_line (line_for_error (start_line),
  6745.              "unterminated character constant");
  6746.     bp--;
  6747.     if (eofp)
  6748.       *eofp = 1;
  6749.     break;
  6750.       }
  6751.       if (traditional) {    /* Unterminated strings are 'legal'.  */
  6752.     if (eofp)
  6753.       *eofp = 1;
  6754.     break;
  6755.       }
  6756.       /* If not traditional, then allow newlines inside strings.  */
  6757.       if (count_newlines)
  6758.     ++*count_newlines;
  6759.     } else if (c == match)
  6760.       break;
  6761.   }
  6762.   return bp;
  6763. }
  6764.  
  6765. /* Skip across a group of balanced parens, starting from IP->bufp.
  6766.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  6767.  
  6768.    This does not handle newlines, because it's used for the arg of #if,
  6769.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  6770.  
  6771. static U_CHAR *
  6772. skip_paren_group (ip)
  6773.      register FILE_BUF *ip;
  6774. {
  6775.   U_CHAR *limit = ip->buf + ip->length;
  6776.   U_CHAR *p = ip->bufp;
  6777.   int depth = 0;
  6778.   int lines_dummy = 0;
  6779.  
  6780.   while (p != limit) {
  6781.     int c = *p++;
  6782.     switch (c) {
  6783.     case '(':
  6784.       depth++;
  6785.       break;
  6786.  
  6787.     case ')':
  6788.       depth--;
  6789.       if (depth == 0)
  6790.     return ip->bufp = p;
  6791.       break;
  6792.  
  6793.     case '/':
  6794.       if (*p == '*') {
  6795.     ip->bufp = p;
  6796.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  6797.     p = ip->bufp;
  6798.       }
  6799.  
  6800.     case '"':
  6801.     case '\'':
  6802.       {
  6803.     int eofp = 0;
  6804.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  6805.     if (eofp)
  6806.       return ip->bufp = p;
  6807.       }
  6808.       break;
  6809.     }
  6810.   }
  6811.  
  6812.   ip->bufp = p;
  6813.   return p;
  6814. }
  6815.  
  6816. /*
  6817.  * write out a #line command, for instance, after an #include file.
  6818.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  6819.  * appear to be a no-op, and we can output a few newlines instead
  6820.  * if we want to increase the line number by a small amount.
  6821.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  6822.  */
  6823.  
  6824. static void
  6825. output_line_command (ip, op, conditional, file_change)
  6826.      FILE_BUF *ip, *op;
  6827.      int conditional;
  6828.      enum file_change_code file_change;
  6829. {
  6830.   int len;
  6831.   char *line_cmd_buf;
  6832.  
  6833.   if (no_line_commands
  6834.       || ip->fname == NULL
  6835.       || no_output) {
  6836.     op->lineno = ip->lineno;
  6837.     return;
  6838.   }
  6839.  
  6840.   if (conditional) {
  6841.     if (ip->lineno == op->lineno)
  6842.       return;
  6843.  
  6844.     /* If the inherited line number is a little too small,
  6845.        output some newlines instead of a #line command.  */
  6846.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  6847.       check_expand (op, 10);
  6848.       while (ip->lineno > op->lineno) {
  6849.     *op->bufp++ = '\n';
  6850.     op->lineno++;
  6851.       }
  6852.       return;
  6853.     }
  6854.   }
  6855.  
  6856.   /* Don't output a line number of 0 if we can help it.  */
  6857.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  6858.       && *ip->bufp == '\n') {
  6859.     ip->lineno++;
  6860.     ip->bufp++;
  6861.   }
  6862.  
  6863.   line_cmd_buf = (char *) alloca (strlen (ip->nominal_fname) + 100);
  6864. #ifdef OUTPUT_LINE_COMMANDS
  6865.   sprintf (line_cmd_buf, "#line %d \"%s\"", ip->lineno, ip->nominal_fname);
  6866. #else
  6867.   sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->nominal_fname);
  6868. #endif
  6869.   if (file_change != same_file)
  6870.     strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
  6871.   /* Tell cc1 if following text comes from a system header file.  */
  6872.   if (ip->system_header_p)
  6873.     strcat (line_cmd_buf, " 3");
  6874.   len = strlen (line_cmd_buf);
  6875.   line_cmd_buf[len++] = '\n';
  6876.   check_expand (op, len + 1);
  6877.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  6878.     *op->bufp++ = '\n';
  6879.   bcopy (line_cmd_buf, op->bufp, len);
  6880.   op->bufp += len;
  6881.   op->lineno = ip->lineno;
  6882. }
  6883.  
  6884. /* This structure represents one parsed argument in a macro call.
  6885.    `raw' points to the argument text as written (`raw_length' is its length).
  6886.    `expanded' points to the argument's macro-expansion
  6887.    (its length is `expand_length').
  6888.    `stringified_length' is the length the argument would have
  6889.    if stringified.
  6890.    `use_count' is the number of times this macro arg is substituted
  6891.    into the macro.  If the actual use count exceeds 10, 
  6892.    the value stored is 10.
  6893.    `free1' and `free2', if nonzero, point to blocks to be freed
  6894.    when the macro argument data is no longer needed.  */
  6895.  
  6896. struct argdata {
  6897.   U_CHAR *raw, *expanded;
  6898.   int raw_length, expand_length;
  6899.   int stringified_length;
  6900.   U_CHAR *free1, *free2;
  6901.   char newlines;
  6902.   char comments;
  6903.   char use_count;
  6904. };
  6905.  
  6906. /* Expand a macro call.
  6907.    HP points to the symbol that is the macro being called.
  6908.    Put the result of expansion onto the input stack
  6909.    so that subsequent input by our caller will use it.
  6910.  
  6911.    If macro wants arguments, caller has already verified that
  6912.    an argument list follows; arguments come from the input stack.  */
  6913.  
  6914. static void
  6915. macroexpand (hp, op)
  6916.      HASHNODE *hp;
  6917.      FILE_BUF *op;
  6918. {
  6919.   int nargs;
  6920.   DEFINITION *defn = hp->value.defn;
  6921.   register U_CHAR *xbuf;
  6922.   int xbuf_len;
  6923.   int start_line = instack[indepth].lineno;
  6924.   int rest_args, rest_zero;
  6925.  
  6926.   CHECK_DEPTH (return;);
  6927.  
  6928.   /* it might not actually be a macro.  */
  6929.   if (hp->type != T_MACRO) {
  6930.     special_symbol (hp, op);
  6931.     return;
  6932.   }
  6933.  
  6934.   /* This macro is being used inside a #if, which means it must be */
  6935.   /* recorded as a precondition.  */
  6936.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  6937.     dump_single_macro (hp, pcp_outfile);
  6938.   
  6939.   nargs = defn->nargs;
  6940.  
  6941.   if (nargs >= 0) {
  6942.     register int i;
  6943.     struct argdata *args;
  6944.     char *parse_error = 0;
  6945.  
  6946.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  6947.  
  6948.     for (i = 0; i < nargs; i++) {
  6949.       args[i].raw = args[i].expanded = (U_CHAR *) "";
  6950.       args[i].raw_length = args[i].expand_length
  6951.     = args[i].stringified_length = 0;
  6952.       args[i].free1 = args[i].free2 = 0;
  6953.       args[i].use_count = 0;
  6954.     }
  6955.  
  6956.     /* Parse all the macro args that are supplied.  I counts them.
  6957.        The first NARGS args are stored in ARGS.
  6958.        The rest are discarded.
  6959.        If rest_args is set then we assume macarg absorbed the rest of the args.
  6960.        */
  6961.     i = 0;
  6962.     rest_args = 0;
  6963.     do {
  6964.       /* Discard the open-parenthesis or comma before the next arg.  */
  6965.       ++instack[indepth].bufp;
  6966.       if (rest_args)
  6967.     continue;
  6968.       if (i < nargs || (nargs == 0 && i == 0)) {
  6969.     /* if we are working on last arg which absorbs rest of args... */
  6970.     if (i == nargs - 1 && defn->rest_args)
  6971.       rest_args = 1;
  6972.     parse_error = macarg (&args[i], rest_args);
  6973.       }
  6974.       else
  6975.     parse_error = macarg (NULL_PTR, 0);
  6976.       if (parse_error) {
  6977.     error_with_line (line_for_error (start_line), parse_error);
  6978.     break;
  6979.       }
  6980.       i++;
  6981.     } while (*instack[indepth].bufp != ')');
  6982.  
  6983.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  6984.     if (i == 1) {
  6985.       register U_CHAR *bp = args[0].raw;
  6986.       register U_CHAR *lim = bp + args[0].raw_length;
  6987.       while (bp != lim && is_space[*bp]) bp++;
  6988.       if (bp == lim)
  6989.     i = 0;
  6990.     }
  6991.  
  6992.     /* Don't output an error message if we have already output one for
  6993.        a parse error above.  */
  6994.     rest_zero = 0;
  6995.     if (nargs == 0 && i > 0) {
  6996.       if (! parse_error)
  6997.     error ("arguments given to macro `%s'", hp->name);
  6998.     } else if (i < nargs) {
  6999.       /* traditional C allows foo() if foo wants one argument.  */
  7000.       if (nargs == 1 && i == 0 && traditional)
  7001.     ;
  7002.       /* the rest args token is allowed to absorb 0 tokens */
  7003.       else if (i == nargs - 1 && defn->rest_args)
  7004.     rest_zero = 1;
  7005.       else if (parse_error)
  7006.     ;
  7007.       else if (i == 0)
  7008.     error ("macro `%s' used without args", hp->name);
  7009.       else if (i == 1)
  7010.     error ("macro `%s' used with just one arg", hp->name);
  7011.       else
  7012.     error ("macro `%s' used with only %d args", hp->name, i);
  7013.     } else if (i > nargs) {
  7014.       if (! parse_error)
  7015.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  7016.     }
  7017.  
  7018.     /* Swallow the closeparen.  */
  7019.     ++instack[indepth].bufp;
  7020.  
  7021.     /* If macro wants zero args, we parsed the arglist for checking only.
  7022.        Read directly from the macro definition.  */
  7023.     if (nargs == 0) {
  7024.       xbuf = defn->expansion;
  7025.       xbuf_len = defn->length;
  7026.     } else {
  7027.       register U_CHAR *exp = defn->expansion;
  7028.       register int offset;    /* offset in expansion,
  7029.                    copied a piece at a time */
  7030.       register int totlen;    /* total amount of exp buffer filled so far */
  7031.  
  7032.       register struct reflist *ap, *last_ap;
  7033.  
  7034.       /* Macro really takes args.  Compute the expansion of this call.  */
  7035.  
  7036.       /* Compute length in characters of the macro's expansion.
  7037.      Also count number of times each arg is used.  */
  7038.       xbuf_len = defn->length;
  7039.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  7040.     if (ap->stringify)
  7041.       xbuf_len += args[ap->argno].stringified_length;
  7042.     else if (ap->raw_before || ap->raw_after || traditional)
  7043.       xbuf_len += args[ap->argno].raw_length;
  7044.     else
  7045.       xbuf_len += args[ap->argno].expand_length;
  7046.  
  7047.     if (args[ap->argno].use_count < 10)
  7048.       args[ap->argno].use_count++;
  7049.       }
  7050.  
  7051.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  7052.  
  7053.       /* Generate in XBUF the complete expansion
  7054.      with arguments substituted in.
  7055.      TOTLEN is the total size generated so far.
  7056.      OFFSET is the index in the definition
  7057.      of where we are copying from.  */
  7058.       offset = totlen = 0;
  7059.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  7060.        last_ap = ap, ap = ap->next) {
  7061.     register struct argdata *arg = &args[ap->argno];
  7062.  
  7063.     /* add chars to XBUF unless rest_args was zero with concatenation */
  7064.     for (i = 0; i < ap->nchars; i++, offset++)
  7065.       if (! (rest_zero && ((ap->rest_args && ap->raw_before)
  7066.                    || (last_ap != NULL && last_ap->rest_args
  7067.                    && last_ap->raw_after))))
  7068.         xbuf[totlen++] = exp[offset];
  7069.  
  7070.     if (ap->stringify != 0) {
  7071.       int arglen = arg->raw_length;
  7072.       int escaped = 0;
  7073.       int in_string = 0;
  7074.       int c;
  7075.       i = 0;
  7076.       while (i < arglen
  7077.          && (c = arg->raw[i], is_space[c]))
  7078.         i++;
  7079.       while (i < arglen
  7080.          && (c = arg->raw[arglen - 1], is_space[c]))
  7081.         arglen--;
  7082.       if (!traditional)
  7083.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  7084.       for (; i < arglen; i++) {
  7085.         c = arg->raw[i];
  7086.  
  7087.         /* Special markers Newline Space
  7088.            generate nothing for a stringified argument.  */
  7089.         if (c == '\n' && arg->raw[i+1] != '\n') {
  7090.           i++;
  7091.           continue;
  7092.         }
  7093.  
  7094.         /* Internal sequences of whitespace are replaced by one space
  7095.            except within an string or char token.  */
  7096.         if (! in_string
  7097.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  7098.           while (1) {
  7099.         /* Note that Newline Space does occur within whitespace
  7100.            sequences; consider it part of the sequence.  */
  7101.         if (c == '\n' && is_space[arg->raw[i+1]])
  7102.           i += 2;
  7103.         else if (c != '\n' && is_space[c])
  7104.           i++;
  7105.         else break;
  7106.         c = arg->raw[i];
  7107.           }
  7108.           i--;
  7109.           c = ' ';
  7110.         }
  7111.  
  7112.         if (escaped)
  7113.           escaped = 0;
  7114.         else {
  7115.           if (c == '\\')
  7116.         escaped = 1;
  7117.           if (in_string) {
  7118.         if (c == in_string)
  7119.           in_string = 0;
  7120.           } else if (c == '\"' || c == '\'')
  7121.         in_string = c;
  7122.         }
  7123.  
  7124.         /* Escape these chars */
  7125.         if (c == '\"' || (in_string && c == '\\'))
  7126.           xbuf[totlen++] = '\\';
  7127.         if (isprint (c))
  7128.           xbuf[totlen++] = c;
  7129.         else {
  7130.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  7131.           totlen += 4;
  7132.         }
  7133.       }
  7134.       if (!traditional)
  7135.         xbuf[totlen++] = '\"'; /* insert ending quote */
  7136.     } else if (ap->raw_before || ap->raw_after || traditional) {
  7137.       U_CHAR *p1 = arg->raw;
  7138.       U_CHAR *l1 = p1 + arg->raw_length;
  7139.       if (ap->raw_before) {
  7140.         while (p1 != l1 && is_space[*p1]) p1++;
  7141.         while (p1 != l1 && is_idchar[*p1])
  7142.           xbuf[totlen++] = *p1++;
  7143.         /* Delete any no-reexpansion marker that follows
  7144.            an identifier at the beginning of the argument
  7145.            if the argument is concatenated with what precedes it.  */
  7146.         if (p1[0] == '\n' && p1[1] == '-')
  7147.           p1 += 2;
  7148.       }
  7149.       if (ap->raw_after) {
  7150.         /* Arg is concatenated after: delete trailing whitespace,
  7151.            whitespace markers, and no-reexpansion markers.  */
  7152.         while (p1 != l1) {
  7153.           if (is_space[l1[-1]]) l1--;
  7154.           else if (l1[-1] == '-') {
  7155.         U_CHAR *p2 = l1 - 1;
  7156.         /* If a `-' is preceded by an odd number of newlines then it
  7157.            and the last newline are a no-reexpansion marker.  */
  7158.         while (p2 != p1 && p2[-1] == '\n') p2--;
  7159.         if ((l1 - 1 - p2) & 1) {
  7160.           l1 -= 2;
  7161.         }
  7162.         else break;
  7163.           }
  7164.           else break;
  7165.         }
  7166.       }
  7167.       bcopy (p1, xbuf + totlen, l1 - p1);
  7168.       totlen += l1 - p1;
  7169.     } else {
  7170.       bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
  7171.       totlen += arg->expand_length;
  7172.       /* If a macro argument with newlines is used multiple times,
  7173.          then only expand the newlines once.  This avoids creating output
  7174.          lines which don't correspond to any input line, which confuses
  7175.          gdb and gcov.  */
  7176.       if (arg->use_count > 1 && arg->newlines > 0) {
  7177.         /* Don't bother doing delete_newlines for subsequent
  7178.            uses of arg.  */
  7179.         arg->use_count = 1;
  7180.         arg->expand_length
  7181.           = delete_newlines (arg->expanded, arg->expand_length);
  7182.       }
  7183.     }
  7184.  
  7185.     if (totlen > xbuf_len)
  7186.       abort ();
  7187.       }
  7188.  
  7189.       /* if there is anything left of the definition
  7190.      after handling the arg list, copy that in too. */
  7191.  
  7192.       for (i = offset; i < defn->length; i++) {
  7193.     /* if we've reached the end of the macro */
  7194.     if (exp[i] == ')')
  7195.       rest_zero = 0;
  7196.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  7197.            && last_ap->raw_after))
  7198.       xbuf[totlen++] = exp[i];
  7199.       }
  7200.  
  7201.       xbuf[totlen] = 0;
  7202.       xbuf_len = totlen;
  7203.  
  7204.       for (i = 0; i < nargs; i++) {
  7205.     if (args[i].free1 != 0)
  7206.       free (args[i].free1);
  7207.     if (args[i].free2 != 0)
  7208.       free (args[i].free2);
  7209.       }
  7210.     }
  7211.   } else {
  7212.     xbuf = defn->expansion;
  7213.     xbuf_len = defn->length;
  7214.   }
  7215.  
  7216.   /* Now put the expansion on the input stack
  7217.      so our caller will commence reading from it.  */
  7218.   {
  7219.     register FILE_BUF *ip2;
  7220.  
  7221.     ip2 = &instack[++indepth];
  7222.  
  7223.     ip2->fname = 0;
  7224.     ip2->nominal_fname = 0;
  7225.     ip2->lineno = 0;
  7226.     ip2->buf = xbuf;
  7227.     ip2->length = xbuf_len;
  7228.     ip2->bufp = xbuf;
  7229.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  7230.     ip2->macro = hp;
  7231.     ip2->if_stack = if_stack;
  7232.     ip2->system_header_p = 0;
  7233.  
  7234.     /* Recursive macro use sometimes works traditionally.
  7235.        #define foo(x,y) bar(x(y,0), y)
  7236.        foo(foo, baz)  */
  7237.  
  7238.     if (!traditional)
  7239.       hp->type = T_DISABLED;
  7240.   }
  7241. }
  7242.  
  7243. /*
  7244.  * Parse a macro argument and store the info on it into *ARGPTR.
  7245.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  7246.  * Return nonzero to indicate a syntax error.
  7247.  */
  7248.  
  7249. static char *
  7250. macarg (argptr, rest_args)
  7251.      register struct argdata *argptr;
  7252.      int rest_args;
  7253. {
  7254.   FILE_BUF *ip = &instack[indepth];
  7255.   int paren = 0;
  7256.   int newlines = 0;
  7257.   int comments = 0;
  7258.  
  7259.   /* Try to parse as much of the argument as exists at this
  7260.      input stack level.  */
  7261.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  7262.             &paren, &newlines, &comments, rest_args);
  7263.  
  7264.   /* If we find the end of the argument at this level,
  7265.      set up *ARGPTR to point at it in the input stack.  */
  7266.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  7267.       && bp != ip->buf + ip->length) {
  7268.     if (argptr != 0) {
  7269.       argptr->raw = ip->bufp;
  7270.       argptr->raw_length = bp - ip->bufp;
  7271.       argptr->newlines = newlines;
  7272.     }
  7273.     ip->bufp = bp;
  7274.   } else {
  7275.     /* This input stack level ends before the macro argument does.
  7276.        We must pop levels and keep parsing.
  7277.        Therefore, we must allocate a temporary buffer and copy
  7278.        the macro argument into it.  */
  7279.     int bufsize = bp - ip->bufp;
  7280.     int extra = newlines;
  7281.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  7282.     int final_start = 0;
  7283.  
  7284.     bcopy (ip->bufp, buffer, bufsize);
  7285.     ip->bufp = bp;
  7286.     ip->lineno += newlines;
  7287.  
  7288.     while (bp == ip->buf + ip->length) {
  7289.       if (instack[indepth].macro == 0) {
  7290.     free (buffer);
  7291.     return "unterminated macro call";
  7292.       }
  7293.       ip->macro->type = T_MACRO;
  7294.       if (ip->free_ptr)
  7295.     free (ip->free_ptr);
  7296.       ip = &instack[--indepth];
  7297.       newlines = 0;
  7298.       comments = 0;
  7299.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  7300.             &newlines, &comments, rest_args);
  7301.       final_start = bufsize;
  7302.       bufsize += bp - ip->bufp;
  7303.       extra += newlines;
  7304.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  7305.       bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
  7306.       ip->bufp = bp;
  7307.       ip->lineno += newlines;
  7308.     }
  7309.  
  7310.     /* Now, if arg is actually wanted, record its raw form,
  7311.        discarding comments and duplicating newlines in whatever
  7312.        part of it did not come from a macro expansion.
  7313.        EXTRA space has been preallocated for duplicating the newlines.
  7314.        FINAL_START is the index of the start of that part.  */
  7315.     if (argptr != 0) {
  7316.       argptr->raw = buffer;
  7317.       argptr->raw_length = bufsize;
  7318.       argptr->free1 = buffer;
  7319.       argptr->newlines = newlines;
  7320.       argptr->comments = comments;
  7321.       if ((newlines || comments) && ip->fname != 0)
  7322.     argptr->raw_length
  7323.       = final_start +
  7324.         discard_comments (argptr->raw + final_start,
  7325.                   argptr->raw_length - final_start,
  7326.                   newlines);
  7327.       argptr->raw[argptr->raw_length] = 0;
  7328.       if (argptr->raw_length > bufsize + extra)
  7329.     abort ();
  7330.     }
  7331.   }
  7332.  
  7333.   /* If we are not discarding this argument,
  7334.      macroexpand it and compute its length as stringified.
  7335.      All this info goes into *ARGPTR.  */
  7336.  
  7337.   if (argptr != 0) {
  7338.     FILE_BUF obuf;
  7339.     register U_CHAR *buf, *lim;
  7340.     register int totlen;
  7341.  
  7342.     obuf = expand_to_temp_buffer (argptr->raw,
  7343.                   argptr->raw + argptr->raw_length,
  7344.                   1, 0);
  7345.  
  7346.     argptr->expanded = obuf.buf;
  7347.     argptr->expand_length = obuf.length;
  7348.     argptr->free2 = obuf.buf;
  7349.  
  7350.     buf = argptr->raw;
  7351.     lim = buf + argptr->raw_length;
  7352.  
  7353.     while (buf != lim && is_space[*buf])
  7354.       buf++;
  7355.     while (buf != lim && is_space[lim[-1]])
  7356.       lim--;
  7357.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  7358.     while (buf != lim) {
  7359.       register U_CHAR c = *buf++;
  7360.       totlen++;
  7361.       /* Internal sequences of whitespace are replaced by one space
  7362.      in most cases, but not always.  So count all the whitespace
  7363.      in case we need to keep it all.  */
  7364. #if 0
  7365.       if (is_space[c])
  7366.     SKIP_ALL_WHITE_SPACE (buf);
  7367.       else
  7368. #endif
  7369.       if (c == '\"' || c == '\\') /* escape these chars */
  7370.     totlen++;
  7371.       else if (!isprint (c))
  7372.     totlen += 3;
  7373.     }
  7374.     argptr->stringified_length = totlen;
  7375.   }
  7376.   return 0;
  7377. }
  7378.  
  7379. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  7380.    counting parens in *DEPTHPTR,
  7381.    and return if reach LIMIT
  7382.    or before a `)' that would make *DEPTHPTR negative
  7383.    or before a comma when *DEPTHPTR is zero.
  7384.    Single and double quotes are matched and termination
  7385.    is inhibited within them.  Comments also inhibit it.
  7386.    Value returned is pointer to stopping place.
  7387.  
  7388.    Increment *NEWLINES each time a newline is passed.
  7389.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  7390.    Set *COMMENTS to 1 if a comment is seen.  */
  7391.  
  7392. static U_CHAR *
  7393. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  7394.      U_CHAR *start;
  7395.      register U_CHAR *limit;
  7396.      int *depthptr, *newlines, *comments;
  7397.      int rest_args;
  7398. {
  7399.   register U_CHAR *bp = start;
  7400.  
  7401.   while (bp < limit) {
  7402.     switch (*bp) {
  7403.     case '(':
  7404.       (*depthptr)++;
  7405.       break;
  7406.     case ')':
  7407.       if (--(*depthptr) < 0)
  7408.     return bp;
  7409.       break;
  7410.     case '\\':
  7411.       /* Traditionally, backslash makes following char not special.  */
  7412.       if (bp + 1 < limit && traditional)
  7413.     {
  7414.       bp++;
  7415.       /* But count source lines anyway.  */
  7416.       if (*bp == '\n')
  7417.         ++*newlines;
  7418.     }
  7419.       break;
  7420.     case '\n':
  7421.       ++*newlines;
  7422.       break;
  7423.     case '/':
  7424.       if (bp[1] == '\\' && bp[2] == '\n')
  7425.     newline_fix (bp + 1);
  7426.       if (cplusplus_comments && bp[1] == '/') {
  7427.     *comments = 1;
  7428.     bp += 2;
  7429.     while (bp < limit && *bp++ != '\n') ;
  7430.     ++*newlines;
  7431.     break;
  7432.       }
  7433.       if (bp[1] != '*' || bp + 1 >= limit)
  7434.     break;
  7435.       *comments = 1;
  7436.       bp += 2;
  7437.       while (bp + 1 < limit) {
  7438.     if (bp[0] == '*'
  7439.         && bp[1] == '\\' && bp[2] == '\n')
  7440.       newline_fix (bp + 1);
  7441.     if (bp[0] == '*' && bp[1] == '/')
  7442.       break;
  7443.     if (*bp == '\n') ++*newlines;
  7444.     bp++;
  7445.       }
  7446.       break;
  7447.     case '\'':
  7448.     case '\"':
  7449.       {
  7450.     int quotec;
  7451.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  7452.       if (*bp == '\\') {
  7453.         bp++;
  7454.         if (*bp == '\n')
  7455.           ++*newlines;
  7456.         while (*bp == '\\' && bp[1] == '\n') {
  7457.           bp += 2;
  7458.         }
  7459.       } else if (*bp == '\n') {
  7460.         ++*newlines;
  7461.         if (quotec == '\'')
  7462.           break;
  7463.       }
  7464.     }
  7465.       }
  7466.       break;
  7467.     case ',':
  7468.       /* if we've returned to lowest level and we aren't absorbing all args */
  7469.       if ((*depthptr) == 0 && rest_args == 0)
  7470.     return bp;
  7471.       break;
  7472.     }
  7473.     bp++;
  7474.   }
  7475.  
  7476.   return bp;
  7477. }
  7478.  
  7479. /* Discard comments and duplicate newlines
  7480.    in the string of length LENGTH at START,
  7481.    except inside of string constants.
  7482.    The string is copied into itself with its beginning staying fixed.  
  7483.  
  7484.    NEWLINES is the number of newlines that must be duplicated.
  7485.    We assume that that much extra space is available past the end
  7486.    of the string.  */
  7487.  
  7488. static int
  7489. discard_comments (start, length, newlines)
  7490.      U_CHAR *start;
  7491.      int length;
  7492.      int newlines;
  7493. {
  7494.   register U_CHAR *ibp;
  7495.   register U_CHAR *obp;
  7496.   register U_CHAR *limit;
  7497.   register int c;
  7498.  
  7499.   /* If we have newlines to duplicate, copy everything
  7500.      that many characters up.  Then, in the second part,
  7501.      we will have room to insert the newlines
  7502.      while copying down.
  7503.      NEWLINES may actually be too large, because it counts
  7504.      newlines in string constants, and we don't duplicate those.
  7505.      But that does no harm.  */
  7506.   if (newlines > 0) {
  7507.     ibp = start + length;
  7508.     obp = ibp + newlines;
  7509.     limit = start;
  7510.     while (limit != ibp)
  7511.       *--obp = *--ibp;
  7512.   }
  7513.  
  7514.   ibp = start + newlines;
  7515.   limit = start + length + newlines;
  7516.   obp = start;
  7517.  
  7518.   while (ibp < limit) {
  7519.     *obp++ = c = *ibp++;
  7520.     switch (c) {
  7521.     case '\n':
  7522.       /* Duplicate the newline.  */
  7523.       *obp++ = '\n';
  7524.       break;
  7525.  
  7526.     case '\\':
  7527.       if (*ibp == '\n') {
  7528.     obp--;
  7529.     ibp++;
  7530.       }
  7531.       break;
  7532.  
  7533.     case '/':
  7534.       if (*ibp == '\\' && ibp[1] == '\n')
  7535.     newline_fix (ibp);
  7536.       /* Delete any comment.  */
  7537.       if (cplusplus_comments && ibp[0] == '/') {
  7538.     obp--;
  7539.     ibp++;
  7540.     while (ibp < limit && *ibp++ != '\n') ;
  7541.     break;
  7542.       }
  7543.       if (ibp[0] != '*' || ibp + 1 >= limit)
  7544.     break;
  7545.       obp--;
  7546.       ibp++;
  7547.       while (ibp + 1 < limit) {
  7548.     if (ibp[0] == '*'
  7549.         && ibp[1] == '\\' && ibp[2] == '\n')
  7550.       newline_fix (ibp + 1);
  7551.     if (ibp[0] == '*' && ibp[1] == '/')
  7552.       break;
  7553.     ibp++;
  7554.       }
  7555.       ibp += 2;
  7556.       break;
  7557.  
  7558.     case '\'':
  7559.     case '\"':
  7560.       /* Notice and skip strings, so that we don't
  7561.      think that comments start inside them,
  7562.      and so we don't duplicate newlines in them.  */
  7563.       {
  7564.     int quotec = c;
  7565.     while (ibp < limit) {
  7566.       *obp++ = c = *ibp++;
  7567.       if (c == quotec)
  7568.         break;
  7569.       if (c == '\n' && quotec == '\'')
  7570.         break;
  7571.       if (c == '\\' && ibp < limit) {
  7572.         while (*ibp == '\\' && ibp[1] == '\n')
  7573.           ibp += 2;
  7574.         *obp++ = *ibp++;
  7575.       }
  7576.     }
  7577.       }
  7578.       break;
  7579.     }
  7580.   }
  7581.  
  7582.   return obp - start;
  7583. }
  7584.  
  7585. /* Delete newlines in the string of length LENGTH at START, except inside
  7586.    of string constants.  The string is copied into itself with its beginning
  7587.    staying fixed.  */
  7588.  
  7589. static int
  7590. delete_newlines (start, length)
  7591.      U_CHAR *start;
  7592.      int length;
  7593. {
  7594.   register U_CHAR *ibp;
  7595.   register U_CHAR *obp;
  7596.   register U_CHAR *limit;
  7597.   register int c;
  7598.  
  7599.   ibp = start;
  7600.   limit = start + length;
  7601.   obp = start;
  7602.  
  7603.   while (ibp < limit) {
  7604.     *obp++ = c = *ibp++;
  7605.     switch (c) {
  7606.     case '\n':
  7607.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  7608.      output.  Skip past the newline and its duplicate.  */
  7609.       if (*ibp == '\n')
  7610.     {
  7611.       ibp++;
  7612.       obp--;
  7613.     }
  7614.       break;
  7615.  
  7616.     case '\'':
  7617.     case '\"':
  7618.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  7619.       {
  7620.     int quotec = c;
  7621.     while (ibp < limit) {
  7622.       *obp++ = c = *ibp++;
  7623.       if (c == quotec)
  7624.         break;
  7625.       if (c == '\n' && quotec == '\'')
  7626.         break;
  7627.     }
  7628.       }
  7629.       break;
  7630.     }
  7631.   }
  7632.  
  7633.   return obp - start;
  7634. }
  7635.  
  7636. /*
  7637.  * error - print error message and increment count of errors.
  7638.  */
  7639.  
  7640. void
  7641. error (msg, arg1, arg2, arg3)
  7642.      char *msg;
  7643.      char *arg1, *arg2, *arg3;
  7644. {
  7645.   int i;
  7646.   FILE_BUF *ip = NULL;
  7647.  
  7648.   print_containing_files ();
  7649.  
  7650.   for (i = indepth; i >= 0; i--)
  7651.     if (instack[i].fname != NULL) {
  7652.       ip = &instack[i];
  7653.       break;
  7654.     }
  7655.  
  7656.   if (ip != NULL)
  7657.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  7658.   fprintf (stderr, msg, arg1, arg2, arg3);
  7659.   fprintf (stderr, "\n");
  7660.   errors++;
  7661. }
  7662.  
  7663. /* Error including a message from `errno'.  */
  7664.  
  7665. static void
  7666. error_from_errno (name)
  7667.      char *name;
  7668. {
  7669.   int i;
  7670.   FILE_BUF *ip = NULL;
  7671.  
  7672.   print_containing_files ();
  7673.  
  7674.   for (i = indepth; i >= 0; i--)
  7675.     if (instack[i].fname != NULL) {
  7676.       ip = &instack[i];
  7677.       break;
  7678.     }
  7679.  
  7680.   if (ip != NULL)
  7681.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  7682.  
  7683.   if (errno < sys_nerr)
  7684.     fprintf (stderr, "%s: %s\n", name, strerror (errno));
  7685.   else
  7686.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  7687.  
  7688.   errors++;
  7689. }
  7690.  
  7691. /* Print error message but don't count it.  */
  7692.  
  7693. void
  7694. warning (msg, arg1, arg2, arg3)
  7695.      char *msg;
  7696.      char *arg1, *arg2, *arg3;
  7697. {
  7698.   int i;
  7699.   FILE_BUF *ip = NULL;
  7700.  
  7701.   if (inhibit_warnings)
  7702.     return;
  7703.  
  7704.   if (warnings_are_errors)
  7705.     errors++;
  7706.  
  7707.   print_containing_files ();
  7708.  
  7709.   for (i = indepth; i >= 0; i--)
  7710.     if (instack[i].fname != NULL) {
  7711.       ip = &instack[i];
  7712.       break;
  7713.     }
  7714.  
  7715.   if (ip != NULL)
  7716.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  7717.   fprintf (stderr, "warning: ");
  7718.   fprintf (stderr, msg, arg1, arg2, arg3);
  7719.   fprintf (stderr, "\n");
  7720. }
  7721.  
  7722. static void
  7723. error_with_line (line, msg, arg1, arg2, arg3)
  7724.      int line;
  7725.      char *msg;
  7726.      char *arg1, *arg2, *arg3;
  7727. {
  7728.   int i;
  7729.   FILE_BUF *ip = NULL;
  7730.  
  7731.   print_containing_files ();
  7732.  
  7733.   for (i = indepth; i >= 0; i--)
  7734.     if (instack[i].fname != NULL) {
  7735.       ip = &instack[i];
  7736.       break;
  7737.     }
  7738.  
  7739.   if (ip != NULL)
  7740.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  7741.   fprintf (stderr, msg, arg1, arg2, arg3);
  7742.   fprintf (stderr, "\n");
  7743.   errors++;
  7744. }
  7745.  
  7746. /* print an error message and maybe count it.  */
  7747.  
  7748. void
  7749. pedwarn (msg, arg1, arg2, arg3)
  7750.      char *msg;
  7751.      char *arg1, *arg2, *arg3;
  7752. {
  7753.   if (pedantic_errors)
  7754.     error (msg, arg1, arg2, arg3);
  7755.   else
  7756.     warning (msg, arg1, arg2, arg3);
  7757. }
  7758.  
  7759. /* Report a warning (or an error if pedantic_errors)
  7760.    giving specified file name and line number, not current.  */
  7761.  
  7762. static void
  7763. pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
  7764.      char *file;
  7765.      int line;
  7766.      char *msg;
  7767.      char *arg1, *arg2, *arg3;
  7768. {
  7769.   int i;
  7770.   if (!pedantic_errors && inhibit_warnings)
  7771.     return;
  7772.   if (file != NULL)
  7773.     fprintf (stderr, "%s:%d: ", file, line);
  7774.   if (pedantic_errors || warnings_are_errors)
  7775.     errors++;
  7776.   if (!pedantic_errors)
  7777.     fprintf (stderr, "warning: ");
  7778.   fprintf (stderr, msg, arg1, arg2, arg3);
  7779.   fprintf (stderr, "\n");
  7780. }
  7781.  
  7782. /* Print the file names and line numbers of the #include
  7783.    commands which led to the current file.  */
  7784.  
  7785. static void
  7786. print_containing_files ()
  7787. {
  7788.   FILE_BUF *ip = NULL;
  7789.   int i;
  7790.   int first = 1;
  7791.  
  7792.   /* If stack of files hasn't changed since we last printed
  7793.      this info, don't repeat it.  */
  7794.   if (last_error_tick == input_file_stack_tick)
  7795.     return;
  7796.  
  7797.   for (i = indepth; i >= 0; i--)
  7798.     if (instack[i].fname != NULL) {
  7799.       ip = &instack[i];
  7800.       break;
  7801.     }
  7802.  
  7803.   /* Give up if we don't find a source file.  */
  7804.   if (ip == NULL)
  7805.     return;
  7806.  
  7807.   /* Find the other, outer source files.  */
  7808.   for (i--; i >= 0; i--)
  7809.     if (instack[i].fname != NULL) {
  7810.       ip = &instack[i];
  7811.       if (first) {
  7812.     first = 0;
  7813.     fprintf (stderr, "In file included");
  7814.       } else {
  7815.     fprintf (stderr, ",");
  7816.       }
  7817.  
  7818.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  7819.     }
  7820.   if (! first)
  7821.     fprintf (stderr, ":\n");
  7822.  
  7823.   /* Record we have printed the status as of this time.  */
  7824.   last_error_tick = input_file_stack_tick;
  7825. }
  7826.  
  7827. /* Return the line at which an error occurred.
  7828.    The error is not necessarily associated with the current spot
  7829.    in the input stack, so LINE says where.  LINE will have been
  7830.    copied from ip->lineno for the current input level.
  7831.    If the current level is for a file, we return LINE.
  7832.    But if the current level is not for a file, LINE is meaningless.
  7833.    In that case, we return the lineno of the innermost file.  */
  7834.  
  7835. static int
  7836. line_for_error (line)
  7837.      int line;
  7838. {
  7839.   int i;
  7840.   int line1 = line;
  7841.  
  7842.   for (i = indepth; i >= 0; ) {
  7843.     if (instack[i].fname != 0)
  7844.       return line1;
  7845.     i--;
  7846.     if (i < 0)
  7847.       return 0;
  7848.     line1 = instack[i].lineno;
  7849.   }
  7850.   abort ();
  7851.   /*NOTREACHED*/
  7852.   return 0;
  7853. }
  7854.  
  7855. /*
  7856.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  7857.  *
  7858.  * As things stand, nothing is ever placed in the output buffer to be
  7859.  * removed again except when it's KNOWN to be part of an identifier,
  7860.  * so flushing and moving down everything left, instead of expanding,
  7861.  * should work ok.
  7862.  */
  7863.  
  7864. /* You might think void was cleaner for the return type,
  7865.    but that would get type mismatch in check_expand in strict ANSI.  */
  7866. static int
  7867. grow_outbuf (obuf, needed)
  7868.      register FILE_BUF *obuf;
  7869.      register int needed;
  7870. {
  7871.   register U_CHAR *p;
  7872.   int minsize;
  7873.  
  7874.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  7875.     return 0;
  7876.  
  7877.   /* Make it at least twice as big as it is now.  */
  7878.   obuf->length *= 2;
  7879.   /* Make it have at least 150% of the free space we will need.  */
  7880.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  7881.   if (minsize > obuf->length)
  7882.     obuf->length = minsize;
  7883.  
  7884.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  7885.     memory_full ();
  7886.  
  7887.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  7888.   obuf->buf = p;
  7889.  
  7890.   return 0;
  7891. }
  7892.  
  7893. /* Symbol table for macro names and special symbols */
  7894.  
  7895. /*
  7896.  * install a name in the main hash table, even if it is already there.
  7897.  *   name stops with first non alphanumeric, except leading '#'.
  7898.  * caller must check against redefinition if that is desired.
  7899.  * delete_macro () removes things installed by install () in fifo order.
  7900.  * this is important because of the `defined' special symbol used
  7901.  * in #if, and also if pushdef/popdef directives are ever implemented.
  7902.  *
  7903.  * If LEN is >= 0, it is the length of the name.
  7904.  * Otherwise, compute the length by scanning the entire name.
  7905.  *
  7906.  * If HASH is >= 0, it is the precomputed hash code.
  7907.  * Otherwise, compute the hash code.
  7908.  */
  7909. static HASHNODE *
  7910. install (name, len, type, ivalue, value, hash)
  7911.      U_CHAR *name;
  7912.      int len;
  7913.      enum node_type type;
  7914.      int ivalue;
  7915.      char *value;
  7916.      int hash;
  7917. {
  7918.   register HASHNODE *hp;
  7919.   register int i, bucket;
  7920.   register U_CHAR *p, *q;
  7921.  
  7922.   if (len < 0) {
  7923.     p = name;
  7924.     while (is_idchar[*p])
  7925.       p++;
  7926.     len = p - name;
  7927.   }
  7928.  
  7929.   if (hash < 0)
  7930.     hash = hashf (name, len, HASHSIZE);
  7931.  
  7932.   i = sizeof (HASHNODE) + len + 1;
  7933.   hp = (HASHNODE *) xmalloc (i);
  7934.   bucket = hash;
  7935.   hp->bucket_hdr = &hashtab[bucket];
  7936.   hp->next = hashtab[bucket];
  7937.   hashtab[bucket] = hp;
  7938.   hp->prev = NULL;
  7939.   if (hp->next != NULL)
  7940.     hp->next->prev = hp;
  7941.   hp->type = type;
  7942.   hp->length = len;
  7943.   if (hp->type == T_CONST)
  7944.     hp->value.ival = ivalue;
  7945.   else
  7946.     hp->value.cpval = value;
  7947.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  7948.   p = hp->name;
  7949.   q = name;
  7950.   for (i = 0; i < len; i++)
  7951.     *p++ = *q++;
  7952.   hp->name[len] = 0;
  7953.   return hp;
  7954. }
  7955.  
  7956. /*
  7957.  * find the most recent hash node for name name (ending with first
  7958.  * non-identifier char) installed by install
  7959.  *
  7960.  * If LEN is >= 0, it is the length of the name.
  7961.  * Otherwise, compute the length by scanning the entire name.
  7962.  *
  7963.  * If HASH is >= 0, it is the precomputed hash code.
  7964.  * Otherwise, compute the hash code.
  7965.  */
  7966. HASHNODE *
  7967. lookup (name, len, hash)
  7968.      U_CHAR *name;
  7969.      int len;
  7970.      int hash;
  7971. {
  7972.   register U_CHAR *bp;
  7973.   register HASHNODE *bucket;
  7974.  
  7975.   if (len < 0) {
  7976.     for (bp = name; is_idchar[*bp]; bp++) ;
  7977.     len = bp - name;
  7978.   }
  7979.  
  7980.   if (hash < 0)
  7981.     hash = hashf (name, len, HASHSIZE);
  7982.  
  7983.   bucket = hashtab[hash];
  7984.   while (bucket) {
  7985.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  7986.       return bucket;
  7987.     bucket = bucket->next;
  7988.   }
  7989.   return NULL;
  7990. }
  7991.  
  7992. /*
  7993.  * Delete a hash node.  Some weirdness to free junk from macros.
  7994.  * More such weirdness will have to be added if you define more hash
  7995.  * types that need it.
  7996.  */
  7997.  
  7998. /* Note that the DEFINITION of a macro is removed from the hash table
  7999.    but its storage is not freed.  This would be a storage leak
  8000.    except that it is not reasonable to keep undefining and redefining
  8001.    large numbers of macros many times.
  8002.    In any case, this is necessary, because a macro can be #undef'd
  8003.    in the middle of reading the arguments to a call to it.
  8004.    If #undef freed the DEFINITION, that would crash.  */
  8005.  
  8006. static void
  8007. delete_macro (hp)
  8008.      HASHNODE *hp;
  8009. {
  8010.  
  8011.   if (hp->prev != NULL)
  8012.     hp->prev->next = hp->next;
  8013.   if (hp->next != NULL)
  8014.     hp->next->prev = hp->prev;
  8015.  
  8016.   /* make sure that the bucket chain header that
  8017.      the deleted guy was on points to the right thing afterwards. */
  8018.   if (hp == *hp->bucket_hdr)
  8019.     *hp->bucket_hdr = hp->next;
  8020.  
  8021. #if 0
  8022.   if (hp->type == T_MACRO) {
  8023.     DEFINITION *d = hp->value.defn;
  8024.     struct reflist *ap, *nextap;
  8025.  
  8026.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  8027.       nextap = ap->next;
  8028.       free (ap);
  8029.     }
  8030.     free (d);
  8031.   }
  8032. #endif
  8033.   free (hp);
  8034. }
  8035.  
  8036. /*
  8037.  * return hash function on name.  must be compatible with the one
  8038.  * computed a step at a time, elsewhere
  8039.  */
  8040. static int
  8041. hashf (name, len, hashsize)
  8042.      register U_CHAR *name;
  8043.      register int len;
  8044.      int hashsize;
  8045. {
  8046.   register int r = 0;
  8047.  
  8048.   while (len--)
  8049.     r = HASHSTEP (r, *name++);
  8050.  
  8051.   return MAKE_POS (r) % hashsize;
  8052. }
  8053.  
  8054.  
  8055. /* Dump the definition of a single macro HP to OF.  */
  8056. static void
  8057. dump_single_macro (hp, of)
  8058.      register HASHNODE *hp;
  8059.      FILE *of;
  8060. {
  8061.   register DEFINITION *defn = hp->value.defn;
  8062.   struct reflist *ap;
  8063.   int offset;
  8064.   int concat;
  8065.  
  8066.  
  8067.   /* Print the definition of the macro HP.  */
  8068.  
  8069.   fprintf (of, "#define %s", hp->name);
  8070.  
  8071.   if (defn->nargs >= 0) {
  8072.     int i;
  8073.  
  8074.     fprintf (of, "(");
  8075.     for (i = 0; i < defn->nargs; i++) {
  8076.       dump_arg_n (defn, i, of);
  8077.       if (i + 1 < defn->nargs)
  8078.     fprintf (of, ", ");
  8079.     }
  8080.     fprintf (of, ")");
  8081.   }
  8082.  
  8083.   fprintf (of, " ");
  8084.  
  8085.   offset = 0;
  8086.   concat = 0;
  8087.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  8088.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  8089.     if (ap->nchars != 0)
  8090.       concat = 0;
  8091.     offset += ap->nchars;
  8092.     if (ap->stringify)
  8093.       fprintf (of, " #");
  8094.     if (ap->raw_before && !concat)
  8095.       fprintf (of, " ## ");
  8096.     concat = 0;
  8097.     dump_arg_n (defn, ap->argno, of);
  8098.     if (ap->raw_after) {
  8099.       fprintf (of, " ## ");
  8100.       concat = 1;
  8101.     }
  8102.   }
  8103.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  8104.   fprintf (of, "\n");
  8105. }
  8106.  
  8107. /* Dump all macro definitions as #defines to stdout.  */
  8108.  
  8109. static void
  8110. dump_all_macros ()
  8111. {
  8112.   int bucket;
  8113.  
  8114.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  8115.     register HASHNODE *hp;
  8116.  
  8117.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  8118.       if (hp->type == T_MACRO)
  8119.     dump_single_macro (hp, stdout);
  8120.     }
  8121.   }
  8122. }
  8123.  
  8124. /* Output to OF a substring of a macro definition.
  8125.    BASE is the beginning of the definition.
  8126.    Output characters START thru LENGTH.
  8127.    Discard newlines outside of strings, thus
  8128.    converting funny-space markers to ordinary spaces.  */
  8129.  
  8130. static void
  8131. dump_defn_1 (base, start, length, of)
  8132.      U_CHAR *base;
  8133.      int start;
  8134.      int length;
  8135.      FILE *of;
  8136. {
  8137.   U_CHAR *p = base + start;
  8138.   U_CHAR *limit = base + start + length;
  8139.  
  8140.   while (p < limit) {
  8141.     if (*p != '\n')
  8142.       putc (*p, of);
  8143.     else if (*p == '\"' || *p =='\'') {
  8144.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  8145.                        NULL_PTR, NULL_PTR);
  8146.       fwrite (p, p1 - p, 1, of);
  8147.       p = p1 - 1;
  8148.     }
  8149.     p++;
  8150.   }
  8151. }
  8152.  
  8153. /* Print the name of argument number ARGNUM of macro definition DEFN
  8154.    to OF.
  8155.    Recall that DEFN->args.argnames contains all the arg names
  8156.    concatenated in reverse order with comma-space in between.  */
  8157.  
  8158. static void
  8159. dump_arg_n (defn, argnum, of)
  8160.      DEFINITION *defn;
  8161.      int argnum;
  8162.      FILE *of;
  8163. {
  8164.   register U_CHAR *p = defn->args.argnames;
  8165.   while (argnum + 1 < defn->nargs) {
  8166.     p = (U_CHAR *) index (p, ' ') + 1;
  8167.     argnum++;
  8168.   }
  8169.  
  8170.   while (*p && *p != ',') {
  8171.     putc (*p, of);
  8172.     p++;
  8173.   }
  8174. }
  8175.  
  8176. /* Initialize syntactic classifications of characters.  */
  8177.  
  8178. static void
  8179. initialize_char_syntax ()
  8180. {
  8181.   register int i;
  8182.  
  8183.   /*
  8184.    * Set up is_idchar and is_idstart tables.  These should be
  8185.    * faster than saying (is_alpha (c) || c == '_'), etc.
  8186.    * Set up these things before calling any routines tthat
  8187.    * refer to them.
  8188.    */
  8189.   for (i = 'a'; i <= 'z'; i++) {
  8190.     is_idchar[i - 'a' + 'A'] = 1;
  8191.     is_idchar[i] = 1;
  8192.     is_idstart[i - 'a' + 'A'] = 1;
  8193.     is_idstart[i] = 1;
  8194.   }
  8195.   for (i = '0'; i <= '9'; i++)
  8196.     is_idchar[i] = 1;
  8197.   is_idchar['_'] = 1;
  8198.   is_idstart['_'] = 1;
  8199.   is_idchar['$'] = dollars_in_ident;
  8200.   is_idstart['$'] = dollars_in_ident;
  8201.  
  8202.   /* horizontal space table */
  8203.   is_hor_space[' '] = 1;
  8204.   is_hor_space['\t'] = 1;
  8205.   is_hor_space['\v'] = 1;
  8206.   is_hor_space['\f'] = 1;
  8207.   is_hor_space['\r'] = 1;
  8208.  
  8209.   is_space[' '] = 1;
  8210.   is_space['\t'] = 1;
  8211.   is_space['\v'] = 1;
  8212.   is_space['\f'] = 1;
  8213.   is_space['\n'] = 1;
  8214.   is_space['\r'] = 1;
  8215. }
  8216.  
  8217. /* Initialize the built-in macros.  */
  8218.  
  8219. static void
  8220. initialize_builtins (inp, outp)
  8221.      FILE_BUF *inp;
  8222.      FILE_BUF *outp;
  8223. {
  8224.   install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
  8225.   install ("__DATE__", -1, T_DATE, 0, 0, -1);
  8226.   install ("__FILE__", -1, T_FILE, 0, 0, -1);
  8227.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
  8228.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
  8229.   install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
  8230.   install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
  8231.   install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
  8232.   install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
  8233.   install ("__TIME__", -1, T_TIME, 0, 0, -1);
  8234.   if (!traditional)
  8235.     install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
  8236.   if (objc)
  8237.     install ("__OBJC__", -1, T_CONST, 1, 0, -1);
  8238. /*  This is supplied using a -D by the compiler driver
  8239.     so that it is present only when truly compiling with GNU C.  */
  8240. /*  install ("__GNUC__", -1, T_CONST, 2, 0, -1);  */
  8241.  
  8242.   if (debug_output)
  8243.     {
  8244.       char directive[2048];
  8245.       register struct directive *dp = &directive_table[0];
  8246.       struct tm *timebuf = timestamp ();
  8247.  
  8248.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  8249.            instack[0].nominal_fname);
  8250.       output_line_command (inp, outp, 0, same_file);
  8251.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  8252.  
  8253.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  8254.       output_line_command (inp, outp, 0, same_file);
  8255.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  8256.  
  8257.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  8258.       output_line_command (inp, outp, 0, same_file);
  8259.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  8260.  
  8261.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  8262.       output_line_command (inp, outp, 0, same_file);
  8263.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  8264.  
  8265.       sprintf (directive, " __WCHAR_TYPE__ %s\n", WCHAR_TYPE);
  8266.       output_line_command (inp, outp, 0, same_file);
  8267.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  8268.  
  8269.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  8270.            monthnames[timebuf->tm_mon],
  8271.            timebuf->tm_mday, timebuf->tm_year + 1900);
  8272.       output_line_command (inp, outp, 0, same_file);
  8273.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  8274.  
  8275.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  8276.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  8277.       output_line_command (inp, outp, 0, same_file);
  8278.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  8279.  
  8280.       if (!traditional)
  8281.     {
  8282.           sprintf (directive, " __STDC__ 1");
  8283.           output_line_command (inp, outp, 0, same_file);
  8284.           pass_thru_directive (directive, &directive[strlen (directive)],
  8285.                    outp, dp);
  8286.     }
  8287.       if (objc)
  8288.     {
  8289.           sprintf (directive, " __OBJC__ 1");
  8290.           output_line_command (inp, outp, 0, same_file);
  8291.           pass_thru_directive (directive, &directive[strlen (directive)],
  8292.                    outp, dp);
  8293.     }
  8294.     }
  8295. }
  8296.  
  8297. /*
  8298.  * process a given definition string, for initialization
  8299.  * If STR is just an identifier, define it with value 1.
  8300.  * If STR has anything after the identifier, then it should
  8301.  * be identifier=definition.
  8302.  */
  8303.  
  8304. static void
  8305. make_definition (str, op)
  8306.      U_CHAR *str;
  8307.      FILE_BUF *op;
  8308. {
  8309.   FILE_BUF *ip;
  8310.   struct directive *kt;
  8311.   U_CHAR *buf, *p;
  8312.  
  8313.   buf = str;
  8314.   p = str;
  8315.   if (!is_idstart[*p]) {
  8316.     error ("malformed option `-D %s'", str);
  8317.     return;
  8318.   }
  8319.   while (is_idchar[*++p])
  8320.     ;
  8321.   if (*p == 0) {
  8322.     buf = (U_CHAR *) alloca (p - buf + 4);
  8323.     strcpy ((char *)buf, str);
  8324.     strcat ((char *)buf, " 1");
  8325.   } else if (*p != '=') {
  8326.     error ("malformed option `-D %s'", str);
  8327.     return;
  8328.   } else {
  8329.     U_CHAR *q;
  8330.     /* Copy the entire option so we can modify it.  */
  8331.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  8332.     strncpy (buf, str, p - str);
  8333.     /* Change the = to a space.  */
  8334.     buf[p - str] = ' ';
  8335.     /* Scan for any backslash-newline and remove it.  */
  8336.     p++;
  8337.     q = &buf[p - str];
  8338.     while (*p) {
  8339.       if (*p == '\\' && p[1] == '\n')
  8340.     p += 2;
  8341.       /* Change newline chars into newline-markers.  */
  8342.       else if (*p == '\n')
  8343.     {
  8344.       *q++ = '\n';
  8345.       *q++ = '\n';
  8346.       p++;
  8347.     }
  8348.       else
  8349.     *q++ = *p++;
  8350.     }
  8351.     *q = 0;
  8352.   }
  8353.   
  8354.   ip = &instack[++indepth];
  8355.   ip->nominal_fname = ip->fname = "*Initialization*";
  8356.  
  8357.   ip->buf = ip->bufp = buf;
  8358.   ip->length = strlen (buf);
  8359.   ip->lineno = 1;
  8360.   ip->macro = 0;
  8361.   ip->free_ptr = 0;
  8362.   ip->if_stack = if_stack;
  8363.   ip->system_header_p = 0;
  8364.  
  8365.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  8366.     ;
  8367.  
  8368.   do_define (buf, buf + strlen (buf) , op, kt);
  8369.   --indepth;
  8370. }
  8371.  
  8372. /* JF, this does the work for the -U option */
  8373.  
  8374. static void
  8375. make_undef (str, op)
  8376.      U_CHAR *str;
  8377.      FILE_BUF *op;
  8378. {
  8379.   FILE_BUF *ip;
  8380.   struct directive *kt;
  8381.  
  8382.   ip = &instack[++indepth];
  8383.   ip->nominal_fname = ip->fname = "*undef*";
  8384.  
  8385.   ip->buf = ip->bufp = str;
  8386.   ip->length = strlen (str);
  8387.   ip->lineno = 1;
  8388.   ip->macro = 0;
  8389.   ip->free_ptr = 0;
  8390.   ip->if_stack = if_stack;
  8391.   ip->system_header_p = 0;
  8392.  
  8393.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  8394.     ;
  8395.  
  8396.   do_undef (str, str + strlen (str), op, kt);
  8397.   --indepth;
  8398. }
  8399.  
  8400. /* Process the string STR as if it appeared as the body of a #assert.
  8401.    OPTION is the option name for which STR was the argument.  */
  8402.  
  8403. static void
  8404. make_assertion (option, str)
  8405.      char *option;
  8406.      U_CHAR *str;
  8407. {
  8408.   FILE_BUF *ip;
  8409.   struct directive *kt;
  8410.   U_CHAR *buf, *p, *q;
  8411.  
  8412.   /* Copy the entire option so we can modify it.  */
  8413.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  8414.   strcpy ((char *) buf, str);
  8415.   /* Scan for any backslash-newline and remove it.  */
  8416.   p = q = buf;
  8417.   while (*p) {
  8418.     if (*p == '\\' && p[1] == '\n')
  8419.       p += 2;
  8420.     else
  8421.       *q++ = *p++;
  8422.   }
  8423.   *q = 0;
  8424.  
  8425.   p = buf;
  8426.   if (!is_idstart[*p]) {
  8427.     error ("malformed option `%s %s'", option, str);
  8428.     return;
  8429.   }
  8430.   while (is_idchar[*++p])
  8431.     ;
  8432.   while (*p == ' ' || *p == '\t') p++;
  8433.   if (! (*p == 0 || *p == '(')) {
  8434.     error ("malformed option `%s %s'", option, str);
  8435.     return;
  8436.   }
  8437.   
  8438.   ip = &instack[++indepth];
  8439.   ip->nominal_fname = ip->fname = "*Initialization*";
  8440.  
  8441.   ip->buf = ip->bufp = buf;
  8442.   ip->length = strlen (buf);
  8443.   ip->lineno = 1;
  8444.   ip->macro = 0;
  8445.   ip->free_ptr = 0;
  8446.   ip->if_stack = if_stack;
  8447.   ip->system_header_p = 0;
  8448.  
  8449.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  8450.     ;
  8451.  
  8452.   /* pass NULL as output ptr to do_define since we KNOW it never
  8453.      does any output.... */
  8454.   do_assert (buf, buf + strlen (buf) , NULL_PTR, kt);
  8455.   --indepth;
  8456. }
  8457.  
  8458. /* Append a chain of `struct file_name_list's
  8459.    to the end of the main include chain.
  8460.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  8461.  
  8462. static void
  8463. append_include_chain (first, last)
  8464.      struct file_name_list *first, *last;
  8465. {
  8466.   struct file_name_list *dir;
  8467.  
  8468.   if (!first || !last)
  8469.     return;
  8470.  
  8471.   if (include == 0)
  8472.     include = first;
  8473.   else
  8474.     last_include->next = first;
  8475.  
  8476.   if (first_bracket_include == 0)
  8477.     first_bracket_include = first;
  8478.  
  8479.   for (dir = first; ; dir = dir->next) {
  8480.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  8481.     if (len > max_include_len)
  8482.       max_include_len = len;
  8483.     if (dir == last)
  8484.       break;
  8485.   }
  8486.  
  8487.   last->next = NULL;
  8488.   last_include = last;
  8489. }
  8490.  
  8491. /* Add output to `deps_buffer' for the -M switch.
  8492.    STRING points to the text to be output.
  8493.    SIZE is the number of bytes, or 0 meaning output until a null.
  8494.    Outputting the empty string breaks the line if it is long enough.  */
  8495.  
  8496. static void
  8497. deps_output (string, size)
  8498.      char *string;
  8499.      unsigned size;
  8500. {
  8501.   if (size == 0)
  8502.     size = strlen (string);
  8503.  
  8504. #ifndef MAX_OUTPUT_COLUMNS
  8505. #define MAX_OUTPUT_COLUMNS 75
  8506. #endif
  8507.   if (size == 0 && deps_column != 0
  8508.       && size + deps_column > MAX_OUTPUT_COLUMNS) {
  8509.     deps_output ("\\\n  ", 0);
  8510.     deps_column = 0;
  8511.   }
  8512.  
  8513.   if (deps_size + size + 1 > deps_allocated_size) {
  8514.     deps_allocated_size = deps_size + size + 50;
  8515.     deps_allocated_size *= 2;
  8516.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  8517.   }
  8518.   bcopy (string, &deps_buffer[deps_size], size);
  8519.   deps_size += size;
  8520.   deps_column += size;
  8521.   deps_buffer[deps_size] = 0;
  8522. }
  8523.  
  8524. #if defined(USG) || defined(VMS)
  8525. #ifndef BSTRING
  8526.  
  8527. void
  8528. bzero (b, length)
  8529.      register char *b;
  8530.      register unsigned length;
  8531. {
  8532.   while (length-- > 0)
  8533.     *b++ = 0;
  8534. }
  8535.  
  8536. void
  8537. bcopy (b1, b2, length)
  8538.      register char *b1;
  8539.      register char *b2;
  8540.      register unsigned length;
  8541. {
  8542.   while (length-- > 0)
  8543.     *b2++ = *b1++;
  8544. }
  8545.  
  8546. int
  8547. bcmp (b1, b2, length)    /* This could be a macro! */
  8548.      register char *b1;
  8549.      register char *b2;
  8550.      register unsigned length;
  8551. {
  8552.    while (length-- > 0)
  8553.      if (*b1++ != *b2++)
  8554.        return 1;
  8555.  
  8556.    return 0;
  8557. }
  8558. #endif /* not BSTRING */
  8559. #endif /* USG or VMS */
  8560.  
  8561.  
  8562. static void
  8563. fatal (str, arg)
  8564.      char *str, *arg;
  8565. {
  8566.   fprintf (stderr, "%s: ", progname);
  8567.   fprintf (stderr, str, arg);
  8568.   fprintf (stderr, "\n");
  8569.   exit (FAILURE_EXIT_CODE);
  8570. }
  8571.  
  8572. /* More 'friendly' abort that prints the line and file.
  8573.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  8574.  
  8575. void
  8576. fancy_abort ()
  8577. {
  8578.   fatal ("Internal gcc abort.");
  8579. }
  8580.  
  8581. static void
  8582. perror_with_name (name)
  8583.      char *name;
  8584. {
  8585.   int error = errno;
  8586.  
  8587.   fprintf (stderr, "%s: ", progname);
  8588.   if (error < sys_nerr)
  8589.     fprintf (stderr, "%s: %s\n", name, strerror (error));
  8590.   else
  8591.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  8592.   errors++;
  8593. }
  8594.  
  8595. static void
  8596. pfatal_with_name (name)
  8597.      char *name;
  8598. {
  8599.   perror_with_name (name);
  8600. #ifdef VMS
  8601.   exit (vaxc$errno);
  8602. #else
  8603.   exit (FAILURE_EXIT_CODE);
  8604. #endif
  8605. }
  8606.  
  8607.  
  8608. static void
  8609. memory_full ()
  8610. {
  8611.   fatal ("Memory exhausted.");
  8612. }
  8613.  
  8614.  
  8615. char *
  8616. xmalloc (size)
  8617.      unsigned size;
  8618. {
  8619.   register char *ptr = (char *) malloc (size);
  8620.   if (ptr != 0) return (ptr);
  8621.   memory_full ();
  8622.   /*NOTREACHED*/
  8623.   return 0;
  8624. }
  8625.  
  8626. static char *
  8627. xrealloc (old, size)
  8628.      char *old;
  8629.      unsigned size;
  8630. {
  8631.   register char *ptr = (char *) realloc (old, size);
  8632.   if (ptr != 0) return (ptr);
  8633.   memory_full ();
  8634.   /*NOTREACHED*/
  8635.   return 0;
  8636. }
  8637.  
  8638. static char *
  8639. xcalloc (number, size)
  8640.      unsigned number, size;
  8641. {
  8642.   register unsigned total = number * size;
  8643.   register char *ptr = (char *) malloc (total);
  8644.   if (ptr != 0) {
  8645.     if (total > 100)
  8646.       bzero (ptr, total);
  8647.     else {
  8648.       /* It's not too long, so loop, zeroing by longs.
  8649.      It must be safe because malloc values are always well aligned.  */
  8650.       register long *zp = (long *) ptr;
  8651.       register long *zl = (long *) (ptr + total - 4);
  8652.       register int i = total - 4;
  8653.       while (zp < zl)
  8654.     *zp++ = 0;
  8655.       if (i < 0)
  8656.     i = 0;
  8657.       while (i < total)
  8658.     ptr[i++] = 0;
  8659.     }
  8660.     return ptr;
  8661.   }
  8662.   memory_full ();
  8663.   /*NOTREACHED*/
  8664.   return 0;
  8665. }
  8666.  
  8667. static char *
  8668. savestring (input)
  8669.      char *input;
  8670. {
  8671.   unsigned size = strlen (input);
  8672.   char *output = xmalloc (size + 1);
  8673.   strcpy (output, input);
  8674.   return output;
  8675. }
  8676.  
  8677. /* Get the file-mode and data size of the file open on FD
  8678.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  8679.  
  8680. static int
  8681. file_size_and_mode (fd, mode_pointer, size_pointer)
  8682.      int fd;
  8683.      int *mode_pointer;
  8684.      long int *size_pointer;
  8685. {
  8686.   struct stat sbuf;
  8687.  
  8688.   if (fstat (fd, &sbuf) < 0) return (-1);
  8689.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  8690.   if (size_pointer) *size_pointer = sbuf.st_size;
  8691.   return 0;
  8692. }
  8693.  
  8694. #ifdef VMS
  8695.  
  8696. /* Under VMS we need to fix up the "include" specification
  8697.    filename so that everything following the 1st slash is
  8698.    changed into its correct VMS file specification. */
  8699.  
  8700. static void
  8701. hack_vms_include_specification (fname)
  8702.      char *fname;
  8703. {
  8704.   register char *cp, *cp1, *cp2;
  8705.   int f, check_filename_before_returning, no_prefix_seen;
  8706.   char Local[512];
  8707.  
  8708.   check_filename_before_returning = 0;
  8709.   no_prefix_seen = 0;
  8710.  
  8711.   /* Ignore leading "./"s */
  8712.   while (fname[0] == '.' && fname[1] == '/') {
  8713.     strcpy (fname, fname+2);
  8714.     no_prefix_seen = 1;        /* mark this for later */
  8715.   }
  8716.   /* Look for the boundary between the VMS and UNIX filespecs */
  8717.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  8718.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  8719.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  8720.   if (cp) {
  8721.     cp++;
  8722.   } else {
  8723.     cp = index (fname, '/');    /* Look for the "/" */
  8724.   }
  8725.  
  8726.   cp2 = Local;            /* initialize */
  8727.  
  8728.   /* We are trying to do a number of things here.  First of all, we are
  8729.      trying to hammer the filenames into a standard format, such that later
  8730.      processing can handle them.
  8731.      
  8732.      If the file name contains something like [dir.], then it recognizes this
  8733.      as a root, and strips the ".]".  Later processing will add whatever is
  8734.      needed to get things working properly.
  8735.      
  8736.      If no device is specified, then the first directory name is taken to be
  8737.      a device name (or a rooted logical). */
  8738.  
  8739.   /* See if we found that 1st slash */
  8740.   if (cp == 0) return;        /* Nothing to do!!! */
  8741.   if (*cp != '/') return;    /* Nothing to do!!! */
  8742.   /* Point to the UNIX filename part (which needs to be fixed!) */
  8743.   cp1 = cp+1;
  8744.   /* If the directory spec is not rooted, we can just copy
  8745.      the UNIX filename part and we are done */
  8746.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  8747.     if (cp[-2] != '.') {
  8748.       /*
  8749.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  8750.        * We strip the `]', and then splice the two parts of the name in the
  8751.        * usual way.  Given the default locations for include files in cccp.c,
  8752.        * we will only use this code if the user specifies alternate locations
  8753.        * with the /include (-I) switch on the command line.  */
  8754.       cp -= 1;            /* Strip "]" */
  8755.       cp1--;            /* backspace */
  8756.     } else {
  8757.       /*
  8758.        * The VMS part has a ".]" at the end, and this will not do.  Later
  8759.        * processing will add a second directory spec, and this would be a syntax
  8760.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  8761.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  8762.        * generation of the 000000 root directory spec (which does not belong here
  8763.        * in this case).
  8764.        */
  8765.       cp -= 2;            /* Strip ".]" */
  8766.       cp1--; };            /* backspace */
  8767.   } else {
  8768.  
  8769.     /* We drop in here if there is no VMS style directory specification yet.
  8770.      * If there is no device specification either, we make the first dir a
  8771.      * device and try that.  If we do not do this, then we will be essentially
  8772.      * searching the users default directory (as if they did a #include "asdf.h").
  8773.      *
  8774.      * Then all we need to do is to push a '[' into the output string. Later
  8775.      * processing will fill this in, and close the bracket.
  8776.      */
  8777.     if(cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  8778.     *cp2++ = '[';        /* Open the directory specification */
  8779.   }
  8780.  
  8781.   /* at this point we assume that we have the device spec, and (at least
  8782.      the opening "[" for a directory specification.  We may have directories
  8783.      specified already */
  8784.  
  8785.   /* If there are no other slashes then the filename will be
  8786.      in the "root" directory.  Otherwise, we need to add
  8787.      directory specifications. */
  8788.   if (index (cp1, '/') == 0) {
  8789.     /* Just add "000000]" as the directory string */
  8790.     strcpy (cp2, "000000]");
  8791.     cp2 += strlen (cp2);
  8792.     check_filename_before_returning = 1; /* we might need to fool with this later */
  8793.   } else {
  8794.     /* As long as there are still subdirectories to add, do them. */
  8795.     while (index (cp1, '/') != 0) {
  8796.       /* If this token is "." we can ignore it */
  8797.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  8798.     cp1 += 2;
  8799.     continue;
  8800.       }
  8801.       /* Add a subdirectory spec. Do not duplicate "." */
  8802.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  8803.     *cp2++ = '.';
  8804.       /* If this is ".." then the spec becomes "-" */
  8805.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  8806.     /* Add "-" and skip the ".." */
  8807.     *cp2++ = '-';
  8808.     cp1 += 3;
  8809.     continue;
  8810.       }
  8811.       /* Copy the subdirectory */
  8812.       while (*cp1 != '/') *cp2++= *cp1++;
  8813.       cp1++;            /* Skip the "/" */
  8814.     }
  8815.     /* Close the directory specification */
  8816.     if(cp2[-1] == '.')        /* no trailing periods */
  8817.       cp2--;
  8818.     *cp2++ = ']';
  8819.   }
  8820.   /* Now add the filename */
  8821.   while (*cp1) *cp2++ = *cp1++;
  8822.   *cp2 = 0;
  8823.   /* Now append it to the original VMS spec. */
  8824.   strcpy (cp, Local);
  8825.  
  8826.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  8827.      remove the [000000], and return that name.  This provides flexibility
  8828.      to the user in that they can use both rooted and non-rooted logical names
  8829.      to point to the location of the file.  */
  8830.  
  8831.   if (check_filename_before_returning && no_prefix_seen) {
  8832.     f = open (fname, O_RDONLY, 0666);
  8833.     if (f >= 0) {
  8834.       /* The file name is OK as it is, so return it as is.  */
  8835.       close (f);
  8836.       return;
  8837.     }
  8838.     /* The filename did not work.  Try to remove the [000000] from the name,
  8839.        and return it.  */
  8840.     cp = index (fname, '[');
  8841.     cp2 = index (fname, ']') + 1;
  8842.     strcpy (cp, cp2);        /* this gets rid of it */
  8843.   }
  8844.   return;
  8845. }
  8846. #endif    /* VMS */
  8847.  
  8848. #ifdef    VMS
  8849.  
  8850. /* These are the read/write replacement routines for
  8851.    VAX-11 "C".  They make read/write behave enough
  8852.    like their UNIX counterparts that CCCP will work */
  8853.  
  8854. static int
  8855. read (fd, buf, size)
  8856.      int fd;
  8857.      char *buf;
  8858.      int size;
  8859. {
  8860. #undef    read    /* Get back the REAL read routine */
  8861.   register int i;
  8862.   register int total = 0;
  8863.  
  8864.   /* Read until the buffer is exhausted */
  8865.   while (size > 0) {
  8866.     /* Limit each read to 32KB */
  8867.     i = (size > (32*1024)) ? (32*1024) : size;
  8868.     i = read (fd, buf, i);
  8869.     if (i <= 0) {
  8870.       if (i == 0) return (total);
  8871.       return(i);
  8872.     }
  8873.     /* Account for this read */
  8874.     total += i;
  8875.     buf += i;
  8876.     size -= i;
  8877.   }
  8878.   return (total);
  8879. }
  8880.  
  8881. static int
  8882. write (fd, buf, size)
  8883.      int fd;
  8884.      char *buf;
  8885.      int size;
  8886. {
  8887. #undef    write    /* Get back the REAL write routine */
  8888.   int i;
  8889.   int j;
  8890.  
  8891.   /* Limit individual writes to 32Kb */
  8892.   i = size;
  8893.   while (i > 0) {
  8894.     j = (i > (32*1024)) ? (32*1024) : i;
  8895.     if (write (fd, buf, j) < 0) return (-1);
  8896.     /* Account for the data written */
  8897.     buf += j;
  8898.     i -= j;
  8899.   }
  8900.   return (size);
  8901. }
  8902.  
  8903. /* The following wrapper functions supply additional arguments to the VMS
  8904.    I/O routines to optimize performance with file handling.  The arguments
  8905.    are:
  8906.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  8907.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  8908.      "fop=tef"- Truncate unused portions of file when closing file.
  8909.      "shr=nil"- Disallow file sharing while file is open.
  8910.  */
  8911.  
  8912. static FILE *
  8913. freopen (fname, type, oldfile)
  8914.      char *fname;
  8915.      char *type;
  8916.      FILE *oldfile;
  8917. {
  8918. #undef    freopen    /* Get back the REAL fopen routine */
  8919.   if (strcmp (type, "w") == 0)
  8920.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  8921.   return freopen (fname, type, oldfile, "mbc=16");
  8922. }
  8923.  
  8924. static FILE *
  8925. fopen (fname, type)
  8926.      char *fname;
  8927.      char *type;
  8928. {
  8929. #undef fopen    /* Get back the REAL fopen routine */
  8930.   if (strcmp (type, "w") == 0)
  8931.     return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  8932.   return fopen (fname, type, "mbc=16");
  8933. }
  8934.  
  8935. static int 
  8936. open (fname, flags, prot)
  8937.      char *fname;
  8938.      int flags;
  8939.      int prot;
  8940. {
  8941. #undef open    /* Get back the REAL open routine */
  8942.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  8943. }
  8944.  
  8945. /* Avoid run-time library bug, where copying M out of N+M characters with
  8946.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  8947.    gcc-cpp exercises this particular bug.  */
  8948.  
  8949. static char *
  8950. strncat (dst, src, cnt)
  8951.      char *dst;
  8952.      const char *src;
  8953.      unsigned cnt;
  8954. {
  8955.   register char *d = dst, *s = (char *) src;
  8956.   register int n = cnt;    /* convert to _signed_ type */
  8957.  
  8958.   while (*d) d++;    /* advance to end */
  8959.   while (--n >= 0)
  8960.     if (!(*d++ = *s++)) break;
  8961.   if (n < 0) *d = '\0';
  8962.   return dst;
  8963. }
  8964. #endif /* VMS */
  8965.  
  8966.  
  8967. #ifdef amigados
  8968.  
  8969. /* This function returns whether the LEN characters long filename FNAME 
  8970.    is an absolute path specification. */
  8971.  
  8972. static int
  8973. amigados_abs_filename (fname, len)
  8974.      char *fname;
  8975.      int len;
  8976. {
  8977.   /* we're using ixemul.library, which treats `/foo' as `foo:', so 
  8978.      fname[0] is to be considered absolute as well */
  8979.   if (fname[0] == '/')
  8980.     return 1;
  8981.  
  8982.   /* else do an index() on fname, but one which is limited to len characters */
  8983.   while (*fname && *fname != ':' && len) 
  8984.     fname++, len--;
  8985.  
  8986.   return *fname == ':';
  8987. }
  8988. #endif /* amigados */
  8989.