home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / cccp.c < prev    next >
C/C++ Source or Header  |  1991-11-05  |  210KB  |  8,063 lines

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