home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / gcc / cpp / cpp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-08  |  145.2 KB  |  5,754 lines

  1. /*-
  2.  * This code is derived from software copyrighted by the Free Software
  3.  * Foundation.
  4.  *
  5.  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
  6.  */
  7.  
  8. #ifndef lint
  9. static char sccsid[] = "@(#)cpp.c    6.3 (Berkeley) 5/8/91";
  10. #endif /* not lint */
  11.  
  12. /* C Compatible Compiler Preprocessor (CCCP)
  13. Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  14.                     Written by Paul Rubin, June 1986
  15.             Adapted to ANSI C, Richard Stallman, Jan 1987
  16.  
  17. This program is free software; you can redistribute it and/or modify it
  18. under the terms of the GNU General Public License as published by the
  19. Free Software Foundation; either version 1, or (at your option) any
  20. later version.
  21.  
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. GNU General Public License for more details.
  26.  
  27. You should have received a copy of the GNU General Public License
  28. along with this program; if not, write to the Free Software
  29. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  30.  
  31.  In other words, you are welcome to use, share and improve this program.
  32.  You are forbidden to forbid anyone else to use, share and improve
  33.  what you give them.   Help stamp out software-hoarding!  */
  34.  
  35. typedef unsigned char U_CHAR;
  36.  
  37. #ifdef EMACS
  38. #define NO_SHORTNAMES
  39. #include "../src/config.h"
  40. #ifdef open
  41. #undef open
  42. #undef read
  43. #undef write
  44. #endif /* open */
  45. #endif /* EMACS */
  46.  
  47. #ifndef EMACS
  48. #include "config.h"
  49. #endif /* not EMACS */
  50.  
  51. #ifndef STDC_VALUE
  52. #define STDC_VALUE 1
  53. #endif
  54.  
  55. /* In case config.h defines these.  */
  56. #undef bcopy
  57. #undef bzero
  58. #undef bcmp
  59.  
  60. #include <sys/types.h>
  61. #include <sys/stat.h>
  62. #include <ctype.h>
  63. #include <stdio.h>
  64. #include <signal.h>
  65.  
  66. #ifndef VMS
  67. #include <sys/file.h>
  68. #ifndef USG
  69. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  70. #include <sys/resource.h>
  71. #else
  72. #define index strchr
  73. #define rindex strrchr
  74. #include <time.h>
  75. #include <fcntl.h>
  76. #endif /* USG */
  77. #endif /* not VMS */
  78.   
  79. /* VMS-specific definitions */
  80. #ifdef VMS
  81. #include <time.h>
  82. #include <errno.h>        /* This defines "errno" properly */
  83. #include <perror.h>        /* This defines sys_errlist/sys_nerr properly */
  84. #define O_RDONLY    0    /* Open arg for Read/Only  */
  85. #define O_WRONLY    1    /* Open arg for Write/Only */
  86. #define read(fd,buf,size)    VAX11_C_read(fd,buf,size)
  87. #define write(fd,buf,size)    VAX11_C_write(fd,buf,size)
  88. #ifdef __GNUC__
  89. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  90. #endif /* __GNUC__ */
  91. #endif /* VMS */
  92.  
  93. #ifndef O_RDONLY
  94. #define O_RDONLY 0
  95. #endif 
  96.  
  97. #define max(a,b) ((a) > (b) ? (a) : (b))
  98.  
  99. #ifndef S_ISREG
  100. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  101. #endif
  102.  
  103. /* External declarations.  */
  104.  
  105. void bcopy (), bzero ();
  106. int bcmp ();
  107. extern char *getenv ();
  108. extern char *version_string;
  109.  
  110. /* Forward declarations.  */
  111.  
  112. struct directive;
  113. struct file_buf;
  114. struct arglist;
  115. struct argdata;
  116.  
  117. int do_define (), do_line (), do_include (), do_undef (), do_error (),
  118.   do_pragma (), do_if (), do_xifdef (), do_else (),
  119.   do_elif (), do_endif (), do_sccs (), do_once ();
  120.  
  121. struct hashnode *install ();
  122. struct hashnode *lookup ();
  123.  
  124. char *xmalloc (), *xrealloc (), *xcalloc (), *savestring ();
  125. void fatal (), fancy_abort (), pfatal_with_name (), perror_with_name ();
  126.  
  127. void macroexpand ();
  128. void dump_all_macros ();
  129. void conditional_skip ();
  130. void skip_if_group ();
  131. void output_line_command ();
  132. /* Last arg to output_line_command.  */
  133. enum file_change_code {same_file, enter_file, leave_file};
  134.  
  135. int grow_outbuf ();
  136. int handle_directive ();
  137. void memory_full ();
  138.  
  139. U_CHAR *macarg1 ();
  140. char *macarg ();
  141.  
  142. U_CHAR *skip_to_end_of_comment ();
  143. U_CHAR *skip_quoted_string ();
  144.  
  145. #ifndef FATAL_EXIT_CODE
  146. #define FATAL_EXIT_CODE 33    /* gnu cc command understands this */
  147. #endif
  148.  
  149. #ifndef SUCCESS_EXIT_CODE
  150. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  151. #endif
  152.  
  153. /* Name under which this program was invoked.  */
  154.  
  155. char *progname;
  156.  
  157. /* Nonzero means handle C++ comment syntax and use
  158.    extra default include directories for C++.  */
  159.  
  160. int cplusplus;
  161.  
  162. /* Current maximum length of directory names in the search path
  163.    for include files.  (Altered as we get more of them.)  */
  164.  
  165. int max_include_len;
  166.  
  167. /* Nonzero means copy comments into the output file.  */
  168.  
  169. int put_out_comments = 0;
  170.  
  171. /* Nonzero means don't process the ANSI trigraph sequences.  */
  172.  
  173. int no_trigraphs = 0;
  174.  
  175. /* Nonzero means print the names of included files rather than
  176.    the preprocessed output.  1 means just the #include "...",
  177.    2 means #include <...> as well.  */
  178.  
  179. int print_deps = 0;
  180.  
  181. /* Nonzero means don't output line number information.  */
  182.  
  183. int no_line_commands;
  184.  
  185. /* Nonzero means inhibit output of the preprocessed text
  186.    and instead output the definitions of all user-defined macros
  187.    in a form suitable for use as input to cccp.  */
  188.  
  189. int dump_macros;
  190.  
  191. /* Nonzero means give all the error messages the ANSI standard requires.  */
  192.  
  193. int pedantic;
  194.  
  195. /* Nonzero means don't print warning messages.  -w.  */
  196.  
  197. int inhibit_warnings = 0;
  198.  
  199. /* Nonzero means warn if slash-star appears in a comment.  */
  200.  
  201. int warn_comments;
  202.  
  203. /* Nonzero means warn if there are any trigraphs.  */
  204.  
  205. int warn_trigraphs;
  206.  
  207. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  208.  
  209. int traditional;
  210.  
  211. /* Nonzero causes output not to be done,
  212.    but directives such as #define that have side effects
  213.    are still obeyed.  */
  214.  
  215. int no_output;
  216.  
  217. /* I/O buffer structure.
  218.    The `fname' field is nonzero for source files and #include files
  219.    and for the dummy text used for -D and -U.
  220.    It is zero for rescanning results of macro expansion
  221.    and for expanding macro arguments.  */
  222. #define INPUT_STACK_MAX 200
  223. struct file_buf {
  224.   char *fname;
  225.   int lineno;
  226.   int length;
  227.   U_CHAR *buf;
  228.   U_CHAR *bufp;
  229.   /* Macro that this level is the expansion of.
  230.      Included so that we can reenable the macro
  231.      at the end of this level.  */
  232.   struct hashnode *macro;
  233.   /* Value of if_stack at start of this file.
  234.      Used to prohibit unmatched #endif (etc) in an include file.  */
  235.   struct if_stack *if_stack;
  236.   /* Object to be freed at end of input at this level.  */
  237.   U_CHAR *free_ptr;
  238. } instack[INPUT_STACK_MAX];
  239.  
  240. /* Current nesting level of input sources.
  241.    `instack[indepth]' is the level currently being read.  */
  242. int indepth = -1;
  243. #define CHECK_DEPTH(code) \
  244.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  245.     {                                    \
  246.       error_with_line (line_for_error (instack[indepth].lineno),    \
  247.                "macro or #include recursion too deep");        \
  248.       code;                                \
  249.     }
  250.  
  251. /* Current depth in #include directives that use <...>.  */
  252. int system_include_depth = 0;
  253.  
  254. typedef struct file_buf FILE_BUF;
  255.  
  256. /* The output buffer.  Its LENGTH field is the amount of room allocated
  257.    for the buffer, not the number of chars actually present.  To get
  258.    that, subtract outbuf.buf from outbuf.bufp. */
  259.  
  260. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  261. FILE_BUF outbuf;
  262.  
  263. /* Grow output buffer OBUF points at
  264.    so it can hold at least NEEDED more chars.  */
  265.  
  266. #define check_expand(OBUF, NEEDED)  \
  267.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  268.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  269.  
  270. struct file_name_list
  271.   {
  272.     struct file_name_list *next;
  273.     char *fname;
  274.   };
  275.  
  276. /* #include "file" looks in source file dir, then stack. */
  277. /* #include <file> just looks in the stack. */
  278. /* -I directories are added to the end, then the defaults are added. */
  279. struct file_name_list include_defaults[] =
  280.   {
  281. #ifndef VMS
  282.     { &include_defaults[1], GCC_INCLUDE_DIR },
  283.     { &include_defaults[2], "/usr/include" },
  284.     { 0, "/usr/local/include" }
  285. #else
  286.     { &include_defaults[1], "GNU_CC_INCLUDE:" },       /* GNU includes */
  287.     { &include_defaults[2], "SYS$SYSROOT:[SYSLIB.]" }, /* VAX-11 "C" includes */
  288.     { 0, "" },    /* This makes normal VMS filespecs work OK */
  289. #endif /* VMS */
  290.   };
  291.  
  292. /* These are used instead of the above, for C++.  */
  293. struct file_name_list cplusplus_include_defaults[] =
  294.   {
  295. #ifndef VMS
  296.     /* Pick up GNU C++ specific include files.  */
  297.     { &cplusplus_include_defaults[1], GPLUSPLUS_INCLUDE_DIR },
  298.     /* Use GNU CC specific header files.  */
  299.     { &cplusplus_include_defaults[2], GCC_INCLUDE_DIR },
  300.     { 0, "/usr/include" }
  301. #else
  302.     { &cplusplus_include_defaults[1], "GNU_GXX_INCLUDE:" },
  303.     { &cplusplus_include_defaults[2], "GNU_CC_INCLUDE:" },
  304.     /* VAX-11 C includes */
  305.     { &cplusplus_include_defaults[3], "SYS$SYSROOT:[SYSLIB.]" },
  306.     { 0, "" },    /* This makes normal VMS filespecs work OK */
  307. #endif /* VMS */
  308.   };
  309.  
  310. struct file_name_list *include = 0;    /* First dir to search */
  311.     /* First dir to search for <file> */
  312. struct file_name_list *first_bracket_include = 0;
  313. struct file_name_list *last_include = 0;    /* Last in chain */
  314.  
  315. /* List of included files that contained #once.  */
  316. struct file_name_list *dont_repeat_files = 0;
  317.  
  318. /* List of other included files.  */
  319. struct file_name_list *all_include_files = 0;
  320.  
  321. /* Structure allocated for every #define.  For a simple replacement
  322.    such as
  323.        #define foo bar ,
  324.    nargs = -1, the `pattern' list is null, and the expansion is just
  325.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  326.    e.g.,
  327.        #define getchar() getc (stdin) .
  328.    When there are args, the expansion is the replacement text with the
  329.    args squashed out, and the reflist is a list describing how to
  330.    build the output from the input: e.g., "3 chars, then the 1st arg,
  331.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  332.    The chars here come from the expansion.  Whatever is left of the
  333.    expansion after the last arg-occurrence is copied after that arg.
  334.    Note that the reflist can be arbitrarily long---
  335.    its length depends on the number of times the arguments appear in
  336.    the replacement text, not how many args there are.  Example:
  337.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  338.    pattern list
  339.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  340.    where (x, y) means (nchars, argno). */
  341.  
  342. typedef struct definition DEFINITION;
  343. struct definition {
  344.   int nargs;
  345.   int length;            /* length of expansion string */
  346.   U_CHAR *expansion;
  347.   struct reflist {
  348.     struct reflist *next;
  349.     char stringify;        /* nonzero if this arg was preceded by a
  350.                    # operator. */
  351.     char raw_before;        /* Nonzero if a ## operator before arg. */
  352.     char raw_after;        /* Nonzero if a ## operator after arg. */
  353.     int nchars;            /* Number of literal chars to copy before
  354.                    this arg occurrence.  */
  355.     int argno;            /* Number of arg to substitute (origin-0) */
  356.   } *pattern;
  357.   /* Names of macro args, concatenated in reverse order
  358.      with comma-space between them.
  359.      The only use of this is that we warn on redefinition
  360.      if this differs between the old and new definitions.  */
  361.   U_CHAR *argnames;
  362. };
  363.  
  364. /* different kinds of things that can appear in the value field
  365.    of a hash node.  Actually, this may be useless now. */
  366. union hashval {
  367.   int ival;
  368.   char *cpval;
  369.   DEFINITION *defn;
  370. };
  371.  
  372.  
  373. /* The structure of a node in the hash table.  The hash table
  374.    has entries for all tokens defined by #define commands (type T_MACRO),
  375.    plus some special tokens like __LINE__ (these each have their own
  376.    type, and the appropriate code is run when that type of node is seen.
  377.    It does not contain control words like "#define", which are recognized
  378.    by a separate piece of code. */
  379.  
  380. /* different flavors of hash nodes --- also used in keyword table */
  381. enum node_type {
  382.  T_DEFINE = 1,    /* the `#define' keyword */
  383.  T_INCLUDE,    /* the `#include' keyword */
  384.  T_IFDEF,    /* the `#ifdef' keyword */
  385.  T_IFNDEF,    /* the `#ifndef' keyword */
  386.  T_IF,        /* the `#if' keyword */
  387.  T_ELSE,    /* `#else' */
  388.  T_PRAGMA,    /* `#pragma' */
  389.  T_ELIF,    /* `#else' */
  390.  T_UNDEF,    /* `#undef' */
  391.  T_LINE,    /* `#line' */
  392.  T_ERROR,    /* `#error' */
  393.  T_ENDIF,    /* `#endif' */
  394.  T_SCCS,    /* `#sccs', used on system V.  */
  395.  T_IDENT,    /* `#ident', used on system V.  */
  396.  T_SPECLINE,    /* special symbol `__LINE__' */
  397.  T_DATE,    /* `__DATE__' */
  398.  T_FILE,    /* `__FILE__' */
  399.  T_BASE_FILE,    /* `__BASE_FILE__' */
  400.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  401.  T_VERSION,    /* `__VERSION__' */
  402.  T_TIME,    /* `__TIME__' */
  403.  T_CONST,    /* Constant value, used by `__STDC__' */
  404.  T_MACRO,    /* macro defined by `#define' */
  405.  T_DISABLED,    /* macro temporarily turned off for rescan */
  406.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  407.  T_UNUSED    /* Used for something not defined.  */
  408.  };
  409.  
  410. struct hashnode {
  411.   struct hashnode *next;    /* double links for easy deletion */
  412.   struct hashnode *prev;
  413.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  414.                    chain is kept, in case the node is the head
  415.                    of the chain and gets deleted. */
  416.   enum node_type type;        /* type of special token */
  417.   int length;            /* length of token, for quick comparison */
  418.   U_CHAR *name;            /* the actual name */
  419.   union hashval value;        /* pointer to expansion, or whatever */
  420. };
  421.  
  422. typedef struct hashnode HASHNODE;
  423.  
  424. /* Some definitions for the hash table.  The hash function MUST be
  425.    computed as shown in hashf () below.  That is because the rescan
  426.    loop computes the hash value `on the fly' for most tokens,
  427.    in order to avoid the overhead of a lot of procedure calls to
  428.    the hashf () function.  Hashf () only exists for the sake of
  429.    politeness, for use when speed isn't so important. */
  430.  
  431. #define HASHSIZE 1403
  432. HASHNODE *hashtab[HASHSIZE];
  433. #define HASHSTEP(old, c) ((old << 2) + c)
  434. #define MAKE_POS(v) (v & ~0x80000000) /* make number positive */
  435.  
  436. /* Symbols to predefine.  */
  437.  
  438. #ifdef CPP_PREDEFINES
  439. char *predefs = CPP_PREDEFINES;
  440. #else
  441. char *predefs = "";
  442. #endif
  443.  
  444. /* `struct directive' defines one #-directive, including how to handle it.  */
  445.  
  446. struct directive {
  447.   int length;            /* Length of name */
  448.   int (*func)();        /* Function to handle directive */
  449.   char *name;            /* Name of directive */
  450.   enum node_type type;        /* Code which describes which directive. */
  451.   char angle_brackets;        /* Nonzero => <...> is special.  */
  452.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  453.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  454. };
  455.  
  456. /* Here is the actual list of #-directives, most-often-used first.  */
  457.  
  458. struct directive directive_table[] = {
  459.   {  6, do_define, "define", T_DEFINE, 0, 1},
  460.   {  2, do_if, "if", T_IF},
  461.   {  5, do_xifdef, "ifdef", T_IFDEF},
  462.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  463.   {  5, do_endif, "endif", T_ENDIF},
  464.   {  4, do_else, "else", T_ELSE},
  465.   {  4, do_elif, "elif", T_ELIF},
  466.   {  4, do_line, "line", T_LINE},
  467.   {  7, do_include, "include", T_INCLUDE, 1},
  468.   {  5, do_undef, "undef", T_UNDEF},
  469.   {  5, do_error, "error", T_ERROR},
  470. #ifdef SCCS_DIRECTIVE
  471.   {  4, do_sccs, "sccs", T_SCCS},
  472. #endif
  473.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  474.   {  -1, 0, "", T_UNUSED},
  475. };
  476.  
  477. /* table to tell if char can be part of a C identifier. */
  478. U_CHAR is_idchar[256];
  479. /* table to tell if char can be first char of a c identifier. */
  480. U_CHAR is_idstart[256];
  481. /* table to tell if c is horizontal space.  */
  482. U_CHAR is_hor_space[256];
  483. /* table to tell if c is horizontal or vertical space.  */
  484. U_CHAR is_space[256];
  485.  
  486. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  487. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  488.   
  489. int errors = 0;            /* Error counter for exit code */
  490.  
  491. /* Zero means dollar signs are punctuation.
  492.    -$ stores 0; -traditional, stores 1.  Default is 1 for VMS, 0 otherwise.
  493.    This must be 0 for correct processing of this ANSI C program:
  494.     #define foo(a) #a
  495.     #define lose(b) foo(b)
  496.     #define test$
  497.     lose(test)    */
  498. #ifndef DOLLARS_IN_IDENTIFIERS
  499. #define DOLLARS_IN_IDENTIFIERS 0
  500. #endif
  501. int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  502.  
  503. FILE_BUF expand_to_temp_buffer ();
  504.  
  505. DEFINITION *collect_expansion ();
  506.  
  507. /* Stack of conditionals currently in progress
  508.    (including both successful and failing conditionals).  */
  509.  
  510. struct if_stack {
  511.   struct if_stack *next;    /* for chaining to the next stack frame */
  512.   char *fname;        /* copied from input when frame is made */
  513.   int lineno;            /* similarly */
  514.   int if_succeeded;        /* true if a leg of this if-group
  515.                     has been passed through rescan */
  516.   enum node_type type;        /* type of last directive seen in this group */
  517. };
  518. typedef struct if_stack IF_STACK_FRAME;
  519. IF_STACK_FRAME *if_stack = NULL;
  520.  
  521. /* Buffer of -M output.  */
  522.  
  523. char *deps_buffer;
  524.  
  525. /* Number of bytes allocated in above.  */
  526. int deps_allocated_size;
  527.  
  528. /* Number of bytes used.  */
  529. int deps_size;
  530.  
  531. /* Number of bytes since the last newline.  */
  532. int deps_column;
  533.  
  534. /* Nonzero means -I- has been seen,
  535.    so don't look for #include "foo" the source-file directory.  */
  536. int ignore_srcdir;
  537.  
  538. /* Handler for SIGPIPE.  */
  539.  
  540. static void
  541. pipe_closed ()
  542. {
  543.   fatal ("output pipe has been closed");
  544. }
  545.  
  546. int
  547. main (argc, argv)
  548.      int argc;
  549.      char **argv;
  550. {
  551.   int st_mode;
  552.   long st_size;
  553.   char *in_fname, *out_fname;
  554.   int f, i;
  555.   FILE_BUF *fp;
  556.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  557.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  558.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  559.   int inhibit_predefs = 0;
  560.   int no_standard_includes = 0;
  561.  
  562.   /* Non-0 means don't output the preprocessed program.  */
  563.   int inhibit_output = 0;
  564.  
  565.   /* Stream on which to print the dependency information.  */
  566.   FILE *deps_stream = 0;
  567.   /* Target-name to write with the dependency information.  */
  568.   char *deps_target = 0;
  569.  
  570. #ifdef RLIMIT_STACK
  571.   /* Get rid of any avoidable limit on stack size.  */
  572.   {
  573.     struct rlimit rlim;
  574.  
  575.     /* Set the stack limit huge so that alloca (particularly stringtab
  576.      * in dbxread.c) does not fail. */
  577.     getrlimit (RLIMIT_STACK, &rlim);
  578.     rlim.rlim_cur = rlim.rlim_max;
  579.     setrlimit (RLIMIT_STACK, &rlim);
  580.   }
  581. #endif /* RLIMIT_STACK defined */
  582.  
  583.   progname = argv[0];
  584. #ifdef VMS
  585.   {
  586.     /* Remove directories from PROGNAME.  */
  587.     char *s;
  588.     extern char *rindex ();
  589.  
  590.     progname = savestring (argv[0]);
  591.  
  592.     if (!(s = rindex (progname, ']')))
  593.       s = rindex (progname, ':');
  594.     if (s)
  595.       strcpy (progname, s+1);
  596.     if (s = rindex (progname, '.'))
  597.       *s = '\0';
  598.   }
  599. #endif
  600.  
  601.   in_fname = NULL;
  602.   out_fname = NULL;
  603.  
  604.   /* Initialize is_idchar to allow $.  */
  605.   dollars_in_ident = 1;
  606.   initialize_char_syntax ();
  607.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
  608.  
  609.   no_line_commands = 0;
  610.   no_trigraphs = 1;
  611.   dump_macros = 0;
  612.   no_output = 0;
  613.   cplusplus = 0;
  614. #ifdef CPLUSPLUS
  615.   cplusplus = 1;
  616. #endif
  617.  
  618.   signal (SIGPIPE, pipe_closed);
  619.  
  620. #ifndef VMS
  621.   max_include_len
  622.     = max (max (sizeof (GCC_INCLUDE_DIR),
  623.         sizeof (GPLUSPLUS_INCLUDE_DIR)),
  624.        sizeof ("/usr/include/CC"));
  625. #else /* VMS */
  626.   max_include_len
  627.     = sizeof("SYS$SYSROOT:[SYSLIB.]");
  628. #endif /* VMS */
  629.  
  630.   bzero (pend_files, argc * sizeof (char *));
  631.   bzero (pend_defs, argc * sizeof (char *));
  632.   bzero (pend_undefs, argc * sizeof (char *));
  633.  
  634.   /* Process switches and find input file name.  */
  635.  
  636.   for (i = 1; i < argc; i++) {
  637.     if (argv[i][0] != '-') {
  638.       if (out_fname != NULL)
  639.     fatal ("Usage: %s [switches] input output", argv[0]);
  640.       else if (in_fname != NULL)
  641.     out_fname = argv[i];
  642.       else
  643.     in_fname = argv[i];
  644.     } else {
  645.       switch (argv[i][1]) {
  646.  
  647.       case 'i':
  648.     if (argv[i][2] != 0)
  649.       pend_files[i] = argv[i] + 2;
  650.     else if (i + 1 == argc)
  651.       fatal ("Filename missing after -i option");
  652.     else
  653.       pend_files[i] = argv[i+1], i++;
  654.     break;
  655.  
  656.       case 'o':
  657.     if (out_fname != NULL)
  658.       fatal ("Output filename specified twice");
  659.     if (i + 1 == argc)
  660.       fatal ("Filename missing after -o option");
  661.     out_fname = argv[++i];
  662.     if (!strcmp (out_fname, "-"))
  663.       out_fname = "";
  664.     break;
  665.  
  666.       case 'p':
  667.     pedantic = 1;
  668.     break;
  669.  
  670.       case 't':
  671.     if (!strcmp (argv[i], "-traditional")) {
  672.       traditional = 1;
  673.       dollars_in_ident = 1;
  674.     } else if (!strcmp (argv[i], "-trigraphs")) {
  675.       no_trigraphs = 0;
  676.     }
  677.     break;
  678.  
  679.       case '+':
  680.     cplusplus = 1;
  681.     break;
  682.  
  683.       case 'w':
  684.     inhibit_warnings = 1;
  685.     break;
  686.  
  687.       case 'W':
  688.     if (!strcmp (argv[i], "-Wtrigraphs")) {
  689.       warn_trigraphs = 1;
  690.     }
  691.     if (!strcmp (argv[i], "-Wcomments"))
  692.       warn_comments = 1;
  693.     if (!strcmp (argv[i], "-Wcomment"))
  694.       warn_comments = 1;
  695.     if (!strcmp (argv[i], "-Wall")) {
  696.       warn_trigraphs = 1;
  697.       warn_comments = 1;
  698.     }
  699.     break;
  700.  
  701.       case 'M':
  702.     if (!strcmp (argv[i], "-M"))
  703.       print_deps = 2;
  704.     else if (!strcmp (argv[i], "-MM"))
  705.       print_deps = 1;
  706.     inhibit_output = 1;
  707.     break;
  708.  
  709.       case 'd':
  710.     dump_macros = 1;
  711.     no_output = 1;
  712.     break;
  713.  
  714.       case 'v':
  715.     fprintf (stderr, "GNU CPP version %s\n", version_string);
  716.     break;
  717.  
  718.       case 'D':
  719.     {
  720.       char *p, *p1;
  721.  
  722.       if (argv[i][2] != 0)
  723.         p = argv[i] + 2;
  724.       else if (i + 1 == argc)
  725.         fatal ("Macro name missing after -D option");
  726.       else
  727.         p = argv[++i];
  728.  
  729.       if ((p1 = (char *) index (p, '=')) != NULL)
  730.         *p1 = ' ';
  731.       pend_defs[i] = p;
  732.     }
  733.     break;
  734.  
  735.       case 'U':        /* JF #undef something */
  736.     if (argv[i][2] != 0)
  737.       pend_undefs[i] = argv[i] + 2;
  738.     else if (i + 1 == argc)
  739.       fatal ("Macro name missing after -U option");
  740.     else
  741.       pend_undefs[i] = argv[i+1], i++;
  742.     break;
  743.  
  744.       case 'C':
  745.     put_out_comments = 1;
  746.     break;
  747.  
  748.       case 'E':            /* -E comes from cc -E; ignore it.  */
  749.     break;
  750.  
  751.       case 'P':
  752.     no_line_commands = 1;
  753.     break;
  754.  
  755.       case '$':            /* Don't include $ in identifiers.  */
  756.     dollars_in_ident = 0;
  757.     break;
  758.  
  759.       case 'I':            /* Add directory to path for includes.  */
  760.     {
  761.       struct file_name_list *dirtmp;
  762.  
  763.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
  764.         ignore_srcdir = 1;
  765.       else {
  766.         dirtmp = (struct file_name_list *)
  767.           xmalloc (sizeof (struct file_name_list));
  768.         dirtmp->next = 0;        /* New one goes on the end */
  769.         if (include == 0)
  770.           include = dirtmp;
  771.         else
  772.           last_include->next = dirtmp;
  773.         last_include = dirtmp;    /* Tail follows the last one */
  774.         if (argv[i][2] != 0)
  775.           dirtmp->fname = argv[i] + 2;
  776.         else if (i + 1 == argc)
  777.           fatal ("Directory name missing after -I option");
  778.         else
  779.           dirtmp->fname = argv[++i];
  780.         if (strlen (dirtmp->fname) > max_include_len)
  781.           max_include_len = strlen (dirtmp->fname);
  782.         if (dirtmp->fname[0] == '.' && dirtmp->fname[1] == '\0')
  783.           dirtmp->fname = 0;    /* Current directory */
  784.         if (ignore_srcdir && first_bracket_include == 0)
  785.           first_bracket_include = dirtmp;
  786.       }
  787.     }
  788.     break;
  789.  
  790.       case 'n':
  791.     /* -nostdinc causes no default include directories.
  792.        You must specify all include-file directories with -I.  */
  793.     no_standard_includes = 1;
  794.     break;
  795.  
  796.       case 'u':
  797.     /* Sun compiler passes undocumented switch "-undef".
  798.        Let's assume it means to inhibit the predefined symbols.  */
  799.     inhibit_predefs = 1;
  800.     break;
  801.  
  802.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  803.     if (in_fname == NULL) {
  804.       in_fname = "";
  805.       break;
  806.     } else if (out_fname == NULL) {
  807.       out_fname = "";
  808.       break;
  809.     }    /* else fall through into error */
  810.  
  811.       default:
  812.     fatal ("Invalid option `%s'", argv[i]);
  813.       }
  814.     }
  815.   }
  816.  
  817.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  818.   initialize_char_syntax ();
  819.  
  820.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  821.      and option processing.  */
  822.   initialize_builtins ();
  823.  
  824.   /* Do standard #defines that identify processor type.  */
  825.  
  826.   if (!inhibit_predefs) {
  827.     char *p = (char *) alloca (strlen (predefs) + 1);
  828.     strcpy (p, predefs);
  829.     while (*p) {
  830.       char *q;
  831.       if (p[0] != '-' || p[1] != 'D')
  832.     abort ();
  833.       q = &p[2];
  834.       while (*p && *p != ' ') p++;
  835.       if (*p != 0)
  836.     *p++= 0;
  837.       make_definition (q);
  838.     }
  839.   }
  840.  
  841.   /* Do defines specified with -D.  */
  842.   for (i = 1; i < argc; i++)
  843.     if (pend_defs[i])
  844.       make_definition (pend_defs[i]);
  845.  
  846.   /* Do undefines specified with -U.  */
  847.   for (i = 1; i < argc; i++)
  848.     if (pend_undefs[i])
  849.       make_undef (pend_undefs[i]);
  850.  
  851.   /* Unless -fnostdinc,
  852.      tack on the standard include file dirs to the specified list */
  853.   if (!no_standard_includes) {
  854.     if (include == 0)
  855.       include = (cplusplus ? cplusplus_include_defaults : include_defaults);
  856.     else
  857.       last_include->next
  858.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  859.     /* Make sure the list for #include <...> also has the standard dirs.  */
  860.     if (ignore_srcdir && first_bracket_include == 0)
  861.       first_bracket_include
  862.     = (cplusplus ? cplusplus_include_defaults : include_defaults);
  863.   }
  864.  
  865.   /* Initialize output buffer */
  866.  
  867.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  868.   outbuf.bufp = outbuf.buf;
  869.   outbuf.length = OUTBUF_SIZE;
  870.  
  871.   /* Scan the -i files before the main input.
  872.      Much like #including them, but with no_output set
  873.      so that only their macro definitions matter.  */
  874.  
  875.   no_output++;
  876.   for (i = 1; i < argc; i++)
  877.     if (pend_files[i]) {
  878.       int fd = open (pend_files[i], O_RDONLY, 0666);
  879.       if (fd < 0) {
  880.     perror_with_name (pend_files[i]);
  881.     return FATAL_EXIT_CODE;
  882.       }
  883.       finclude (fd, pend_files[i], &outbuf);
  884.     }
  885.   no_output--;
  886.  
  887.   /* Create an input stack level for the main input file
  888.      and copy the entire contents of the file into it.  */
  889.  
  890.   fp = &instack[++indepth];
  891.  
  892.   /* JF check for stdin */
  893.   if (in_fname == NULL || *in_fname == 0) {
  894.     in_fname = "";
  895.     f = 0;
  896.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  897.     goto perror;
  898.  
  899.   /* Either of two environment variables can specify output of deps.
  900.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  901.      where OUTPUT_FILE is the file to write deps info to
  902.      and DEPS_TARGET is the target to mention in the deps.  */
  903.  
  904.   if (print_deps == 0
  905.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  906.       || getenv ("DEPENDENCIES_OUTPUT") != 0))
  907.     {
  908.       char *spec = getenv ("DEPENDENCIES_OUTPUT");
  909.       char *s;
  910.       char *output_file;
  911.  
  912.       if (spec == 0)
  913.     {
  914.       spec = getenv ("SUNPRO_DEPENDENCIES");
  915.       print_deps = 2;
  916.     }
  917.       else
  918.     print_deps = 1;
  919.  
  920.       s = spec;
  921.       /* Find the space before the DEPS_TARGET, if there is one.  */
  922.       /* Don't use `index'; that causes trouble on USG.  */
  923.       while (*s != 0 && *s != ' ') s++;
  924.       if (*s != 0)
  925.     {
  926.       deps_target = s + 1;
  927.       output_file = (char *) xmalloc (s - spec + 1);
  928.       bcopy (spec, output_file, s - spec);
  929.       output_file[s - spec] = 0;
  930.     }
  931.       else
  932.     {
  933.       deps_target = 0;
  934.       output_file = spec;
  935.     }
  936.       
  937.       deps_stream = fopen (output_file, "a");
  938.       if (deps_stream == 0)
  939.     pfatal_with_name (output_file);
  940.     }
  941.   /* If the -M option was used, output the deps to standard output.  */
  942.   else if (print_deps)
  943.     deps_stream = stdout;
  944.  
  945.   /* For -M, print the expected object file name
  946.      as the target of this Make-rule.  */
  947.   if (print_deps) {
  948.     deps_allocated_size = 200;
  949.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  950.     deps_buffer[0] = 0;
  951.     deps_size = 0;
  952.     deps_column = 0;
  953.  
  954.     if (deps_target) {
  955.       deps_output (deps_target, 0);
  956.       deps_output (":", 0);
  957.     } else if (*in_fname == 0)
  958.       deps_output ("-: ", 0);
  959.     else {
  960.       int len;
  961.       char *p = in_fname;
  962.       char *p1 = p;
  963.       /* Discard all directory prefixes from P.  */
  964.       while (*p1) {
  965.     if (*p1 == '/')
  966.       p = p1 + 1;
  967.     p1++;
  968.       }
  969.       /* Output P, but remove known suffixes.  */
  970.       len = strlen (p);
  971.       if (p[len - 2] == '.'
  972.       && (p[len - 1] == 'c' || p[len - 1] == 'C' || p[len - 1] == 'S'))
  973.     deps_output (p, len - 2);
  974.       else if (p[len - 3] == '.'
  975.            && p[len - 2] == 'c'
  976.            && p[len - 1] == 'c')
  977.     deps_output (p, len - 3);
  978.       else
  979.     deps_output (p, 0);
  980.       /* Supply our own suffix.  */
  981.       deps_output (".o : ", 0);
  982.       deps_output (in_fname, 0);
  983.       deps_output (" ", 0);
  984.     }
  985.   }
  986.  
  987.   file_size_and_mode (f, &st_mode, &st_size);
  988.   fp->fname = in_fname;
  989.   fp->lineno = 1;
  990.   /* JF all this is mine about reading pipes and ttys */
  991.   if (!S_ISREG (st_mode)) {
  992.     /* Read input from a file that is not a normal disk file.
  993.        We cannot preallocate a buffer with the correct size,
  994.        so we must read in the file a piece at the time and make it bigger.  */
  995.     int size;
  996.     int bsize;
  997.     int cnt;
  998.     U_CHAR *bufp;
  999.  
  1000.     bsize = 2000;
  1001.     size = 0;
  1002.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  1003.     bufp = fp->buf;
  1004.     for (;;) {
  1005.       cnt = read (f, bufp, bsize - size);
  1006.       if (cnt < 0) goto perror;    /* error! */
  1007.       if (cnt == 0) break;    /* End of file */
  1008.       size += cnt;
  1009.       bufp += cnt;
  1010.       if (bsize == size) {    /* Buffer is full! */
  1011.         bsize *= 2;
  1012.         fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  1013.     bufp = fp->buf + size;    /* May have moved */
  1014.       }
  1015.     }
  1016.     fp->length = size;
  1017.   } else {
  1018.     /* Read a file whose size we can determine in advance.
  1019.        For the sake of VMS, st_size is just an upper bound.  */
  1020.     long i;
  1021.     fp->length = 0;
  1022.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  1023.  
  1024.     while (st_size > 0) {
  1025.       i = read (f, fp->buf + fp->length, st_size);
  1026.       if (i <= 0) {
  1027.         if (i == 0) break;
  1028.     goto perror;
  1029.       }
  1030.       fp->length += i;
  1031.       st_size -= i;
  1032.     }
  1033.   }
  1034.   fp->bufp = fp->buf;
  1035.   fp->if_stack = if_stack;
  1036.   
  1037.   /* Unless inhibited, convert trigraphs in the input.  */
  1038.  
  1039.   if (!no_trigraphs)
  1040.     trigraph_pcp (fp);
  1041.  
  1042.   /* Make sure data ends with a newline.  And put a null after it.  */
  1043.  
  1044.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  1045.     fp->buf[fp->length++] = '\n';
  1046.   fp->buf[fp->length] = '\0';
  1047.  
  1048.   /* Now that we know the input file is valid, open the output.  */
  1049.  
  1050.   if (!out_fname || !strcmp (out_fname, ""))
  1051.     out_fname = "stdout";
  1052.   else if (! freopen (out_fname, "w", stdout))
  1053.     pfatal_with_name (out_fname);
  1054.  
  1055.   output_line_command (fp, &outbuf, 0, same_file);
  1056.  
  1057.   /* Scan the input, processing macros and directives.  */
  1058.  
  1059.   rescan (&outbuf, 0);
  1060.  
  1061.   /* Now we have processed the entire input
  1062.      Write whichever kind of output has been requested.  */
  1063.  
  1064.  
  1065.   if (dump_macros)
  1066.     dump_all_macros ();
  1067.   else if (! inhibit_output && deps_stream != stdout) {
  1068.     if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0)
  1069.       fatal ("I/O error on output");
  1070.   }
  1071.  
  1072.   if (print_deps) {
  1073.     fputs (deps_buffer, deps_stream);
  1074.     putc ('\n', deps_stream);
  1075.     if (deps_stream != stdout) {
  1076.       fclose (deps_stream);
  1077.       if (ferror (deps_stream))
  1078.     fatal ("I/O error on output");
  1079.     }
  1080.   }
  1081.  
  1082.   if (ferror (stdout))
  1083.     fatal ("I/O error on output");
  1084.  
  1085.   if (errors)
  1086.     exit (FATAL_EXIT_CODE);
  1087.   exit (SUCCESS_EXIT_CODE);
  1088.  
  1089.  perror:
  1090.   pfatal_with_name (in_fname);
  1091. }
  1092.  
  1093. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  1094.    before main CCCP processing.  Name `pcp' is also in honor of the
  1095.    drugs the trigraph designers must have been on.
  1096.  
  1097.    Using an extra pass through the buffer takes a little extra time,
  1098.    but is infinitely less hairy than trying to handle ??/" inside
  1099.    strings, etc. everywhere, and also makes sure that trigraphs are
  1100.    only translated in the top level of processing. */
  1101.  
  1102. trigraph_pcp (buf)
  1103.      FILE_BUF *buf;
  1104. {
  1105.   register U_CHAR c, *fptr, *bptr, *sptr;
  1106.   int len;
  1107.  
  1108.   fptr = bptr = sptr = buf->buf;
  1109.   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  1110.     if (*++sptr != '?')
  1111.       continue;
  1112.     switch (*++sptr) {
  1113.       case '=':
  1114.       c = '#';
  1115.       break;
  1116.     case '(':
  1117.       c = '[';
  1118.       break;
  1119.     case '/':
  1120.       c = '\\';
  1121.       break;
  1122.     case ')':
  1123.       c = ']';
  1124.       break;
  1125.     case '\'':
  1126.       c = '^';
  1127.       break;
  1128.     case '<':
  1129.       c = '{';
  1130.       break;
  1131.     case '!':
  1132.       c = '|';
  1133.       break;
  1134.     case '>':
  1135.       c = '}';
  1136.       break;
  1137.     case '-':
  1138.       c  = '~';
  1139.       break;
  1140.     case '?':
  1141.       sptr--;
  1142.       continue;
  1143.     default:
  1144.       continue;
  1145.     }
  1146.     len = sptr - fptr - 2;
  1147.     if (bptr != fptr && len > 0)
  1148.       bcopy (fptr, bptr, len);    /* BSD doc says bcopy () works right
  1149.                    for overlapping strings.  In ANSI
  1150.                    C, this will be memmove (). */
  1151.     bptr += len;
  1152.     *bptr++ = c;
  1153.     fptr = ++sptr;
  1154.   }
  1155.   len = buf->length - (fptr - buf->buf);
  1156.   if (bptr != fptr && len > 0)
  1157.     bcopy (fptr, bptr, len);
  1158.   buf->length -= fptr - bptr;
  1159.   buf->buf[buf->length] = '\0';
  1160.   if (warn_trigraphs && fptr != bptr)
  1161.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  1162. }
  1163.  
  1164. /* Move all backslash-newline pairs out of embarrassing places.
  1165.    Exchange all such pairs following BP
  1166.    with any potentially-embarrasing characters that follow them.
  1167.    Potentially-embarrassing characters are / and *
  1168.    (because a backslash-newline inside a comment delimiter
  1169.    would cause it not to be recognized).  */
  1170.  
  1171. newline_fix (bp)
  1172.      U_CHAR *bp;
  1173. {
  1174.   register U_CHAR *p = bp;
  1175.   register int count = 0;
  1176.  
  1177.   /* First count the backslash-newline pairs here.  */
  1178.  
  1179.   while (*p++ == '\\' && *p++ == '\n')
  1180.     count++;
  1181.  
  1182.   p = bp + count * 2;
  1183.  
  1184.   /* Exit if what follows the backslash-newlines is not embarrassing.  */
  1185.  
  1186.   if (count == 0 || (*p != '/' && *p != '*'))
  1187.     return;
  1188.  
  1189.   /* Copy all potentially embarrassing characters
  1190.      that follow the backslash-newline pairs
  1191.      down to where the pairs originally started.  */
  1192.  
  1193.   while (*p == '*' || *p == '/')
  1194.     *bp++ = *p++;
  1195.  
  1196.   /* Now write the same number of pairs after the embarrassing chars.  */
  1197.   while (count-- > 0) {
  1198.     *bp++ = '\\';
  1199.     *bp++ = '\n';
  1200.   }
  1201. }
  1202.  
  1203. /* Like newline_fix but for use within a directive-name.
  1204.    Move any backslash-newlines up past any following symbol constituents.  */
  1205.  
  1206. name_newline_fix (bp)
  1207.      U_CHAR *bp;
  1208. {
  1209.   register U_CHAR *p = bp;
  1210.   register int count = 0;
  1211.  
  1212.   /* First count the backslash-newline pairs here.  */
  1213.  
  1214.   while (*p++ == '\\' && *p++ == '\n')
  1215.     count++;
  1216.  
  1217.   p = bp + count * 2;
  1218.  
  1219.   /* What follows the backslash-newlines is not embarrassing.  */
  1220.  
  1221.   if (count == 0 || !is_idchar[*p])
  1222.     return;
  1223.  
  1224.   /* Copy all potentially embarrassing characters
  1225.      that follow the backslash-newline pairs
  1226.      down to where the pairs originally started.  */
  1227.  
  1228.   while (is_idchar[*p])
  1229.     *bp++ = *p++;
  1230.  
  1231.   /* Now write the same number of pairs after the embarrassing chars.  */
  1232.   while (count-- > 0) {
  1233.     *bp++ = '\\';
  1234.     *bp++ = '\n';
  1235.   }
  1236. }
  1237.  
  1238. /*
  1239.  * The main loop of the program.
  1240.  *
  1241.  * Read characters from the input stack, transferring them to the
  1242.  * output buffer OP.
  1243.  *
  1244.  * Macros are expanded and push levels on the input stack.
  1245.  * At the end of such a level it is popped off and we keep reading.
  1246.  * At the end of any other kind of level, we return.
  1247.  * #-directives are handled, except within macros.
  1248.  *
  1249.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  1250.  * and insert them when appropriate.  This is set while scanning macro
  1251.  * arguments before substitution.  It is zero when scanning for final output.
  1252.  *   There are three types of Newline markers:
  1253.  *   * Newline -  follows a macro name that was not expanded
  1254.  *     because it appeared inside an expansion of the same macro.
  1255.  *     This marker prevents future expansion of that identifier.
  1256.  *     When the input is rescanned into the final output, these are deleted.
  1257.  *     These are also deleted by ## concatenation.
  1258.  *   * Newline Space (or Newline and any other whitespace character)
  1259.  *     stands for a place that tokens must be separated or whitespace
  1260.  *     is otherwise desirable, but where the ANSI standard specifies there
  1261.  *     is no whitespace.  This marker turns into a Space (or whichever other
  1262.  *     whitespace char appears in the marker) in the final output,
  1263.  *     but it turns into nothing in an argument that is stringified with #.
  1264.  *     Such stringified arguments are the only place where the ANSI standard
  1265.  *     specifies with precision that whitespace may not appear.
  1266.  *
  1267.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  1268.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  1269.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  1270.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  1271.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  1272.  */
  1273.  
  1274. rescan (op, output_marks)
  1275.      FILE_BUF *op;
  1276.      int output_marks;
  1277. {
  1278.   /* Character being scanned in main loop.  */
  1279.   register U_CHAR c;
  1280.  
  1281.   /* Length of pending accumulated identifier.  */
  1282.   register int ident_length = 0;
  1283.  
  1284.   /* Hash code of pending accumulated identifier.  */
  1285.   register int hash = 0;
  1286.  
  1287.   /* Current input level (&instack[indepth]).  */
  1288.   FILE_BUF *ip;
  1289.  
  1290.   /* Pointer for scanning input.  */
  1291.   register U_CHAR *ibp;
  1292.  
  1293.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  1294.   register U_CHAR *limit;
  1295.  
  1296.   /* Pointer for storing output.  */
  1297.   register U_CHAR *obp;
  1298.  
  1299.   /* REDO_CHAR is nonzero if we are processing an identifier
  1300.      after backing up over the terminating character.
  1301.      Sometimes we process an identifier without backing up over
  1302.      the terminating character, if the terminating character
  1303.      is not special.  Backing up is done so that the terminating character
  1304.      will be dispatched on again once the identifier is dealt with.  */
  1305.   int redo_char = 0;
  1306.  
  1307.   /* 1 if within an identifier inside of which a concatenation
  1308.      marker (Newline -) has been seen.  */
  1309.   int concatenated = 0;
  1310.  
  1311.   /* While scanning a comment or a string constant,
  1312.      this records the line it started on, for error messages.  */
  1313.   int start_line;
  1314.  
  1315.   /* Record position of last `real' newline.  */
  1316.   U_CHAR *beg_of_line;
  1317.  
  1318. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  1319.  
  1320. #define POPMACRO \
  1321. do { ip->macro->type = T_MACRO;        \
  1322.      if (ip->free_ptr) free (ip->free_ptr);    \
  1323.      --indepth; } while (0)
  1324.  
  1325. /* Reload `rescan's local variables that describe the current
  1326.    level of the input stack.  */
  1327.  
  1328. #define RECACHE  \
  1329. do { ip = &instack[indepth];        \
  1330.      ibp = ip->bufp;            \
  1331.      limit = ip->buf + ip->length;    \
  1332.      op->bufp = obp;            \
  1333.      check_expand (op, limit - ibp);    \
  1334.      beg_of_line = 0;            \
  1335.      obp = op->bufp; } while (0)
  1336.  
  1337.   if (no_output && instack[indepth].fname != 0)
  1338.     skip_if_group (&instack[indepth], 1);
  1339.  
  1340.   obp = op->bufp;
  1341.   RECACHE;
  1342.   beg_of_line = ibp;
  1343.  
  1344.   /* Our caller must always put a null after the end of
  1345.      the input at each input stack level.  */
  1346.   if (*limit != 0)
  1347.     abort ();
  1348.  
  1349.   while (1) {
  1350.     c = *ibp++;
  1351.     *obp++ = c;
  1352.  
  1353.     switch (c) {
  1354.     case '\\':
  1355.       if (ibp >= limit)
  1356.     break;
  1357.       if (*ibp == '\n') {
  1358.     /* Always merge lines ending with backslash-newline,
  1359.        even in middle of identifier.  */
  1360.     ++ibp;
  1361.     ++ip->lineno;
  1362.     --obp;        /* remove backslash from obuf */
  1363.     break;
  1364.       }
  1365.       /* Otherwise, backslash suppresses specialness of following char,
  1366.      so copy it here to prevent the switch from seeing it.
  1367.      But first get any pending identifier processed.  */
  1368.       if (ident_length > 0)
  1369.     goto specialchar;
  1370.       *obp++ = *ibp++;
  1371.       break;
  1372.  
  1373.     case '#':
  1374.       /* If this is expanding a macro definition, don't recognize
  1375.      preprocessor directives.  */
  1376.       if (ip->macro != 0)
  1377.     goto randomchar;
  1378.       if (ident_length)
  1379.     goto specialchar;
  1380.  
  1381.       /* # keyword: a # must be first nonblank char on the line */
  1382.       if (beg_of_line == 0)
  1383.     goto randomchar;
  1384.       {
  1385.     U_CHAR *bp;
  1386.  
  1387.     /* Scan from start of line, skipping whitespace, comments
  1388.        and backslash-newlines, and see if we reach this #.
  1389.        If not, this # is not special.  */
  1390.     bp = beg_of_line;
  1391.     while (1) {
  1392.       if (is_hor_space[*bp])
  1393.         bp++;
  1394.       else if (*bp == '\\' && bp[1] == '\n')
  1395.         bp += 2;
  1396.       else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
  1397.         bp += 2;
  1398.         while (!(*bp == '*' && (newline_fix (bp + 1), bp[1]) == '/'))
  1399.           bp++;
  1400.         bp += 1;
  1401.       }
  1402.       else if (cplusplus && *bp == '/' && (newline_fix (bp + 1), bp[1]) == '/') {
  1403.         bp += 2;
  1404.         while (*bp++ != '\n') ;
  1405.       }
  1406.       else break;
  1407.     }
  1408.     if (bp + 1 != ibp)
  1409.       goto randomchar;
  1410.       }
  1411.  
  1412.       /* This # can start a directive.  */
  1413.  
  1414.       --obp;        /* Don't copy the '#' */
  1415.  
  1416.       ip->bufp = ibp;
  1417.       op->bufp = obp;
  1418.       if (! handle_directive (ip, op)) {
  1419. #ifdef USE_C_ALLOCA
  1420.     alloca (0);
  1421. #endif
  1422.     /* Not a known directive: treat it as ordinary text.
  1423.        IP, OP, IBP, etc. have not been changed.  */
  1424.     if (no_output && instack[indepth].fname) {
  1425.       /* If not generating expanded output,
  1426.          what we do with ordinary text is skip it.
  1427.          Discard everything until next # directive.  */
  1428.       skip_if_group (&instack[indepth], 1);
  1429.       RECACHE;
  1430.       beg_of_line = ibp;
  1431.       break;
  1432.     }
  1433.     ++obp;        /* Copy the '#' after all */
  1434.     goto randomchar;
  1435.       }
  1436. #ifdef USE_C_ALLOCA
  1437.       alloca (0);
  1438. #endif
  1439.       /* A # directive has been successfully processed.  */
  1440.       /* If not generating expanded output, ignore everything until
  1441.      next # directive.  */
  1442.       if (no_output && instack[indepth].fname)
  1443.     skip_if_group (&instack[indepth], 1);
  1444.       obp = op->bufp;
  1445.       RECACHE;
  1446.       beg_of_line = ibp;
  1447.       break;
  1448.  
  1449.     case '\"':            /* skip quoted string */
  1450.     case '\'':
  1451.       /* A single quoted string is treated like a double -- some
  1452.      programs (e.g., troff) are perverse this way */
  1453.  
  1454.       if (ident_length)
  1455.     goto specialchar;
  1456.  
  1457.       start_line = ip->lineno;
  1458.  
  1459.       /* Skip ahead to a matching quote.  */
  1460.  
  1461.       while (1) {
  1462.     if (ibp >= limit) {
  1463.       if (traditional) {
  1464.         if (ip->macro != 0) {
  1465.           /* try harder: this string crosses a macro expansion boundary */
  1466.           POPMACRO;
  1467.           RECACHE;
  1468.           continue;
  1469.         }
  1470.       } else
  1471.         error_with_line (line_for_error (start_line),
  1472.                  "unterminated string or character constant");
  1473.       break;
  1474.     }
  1475.     *obp++ = *ibp;
  1476.     switch (*ibp++) {
  1477.     case '\n':
  1478.       ++ip->lineno;
  1479.       ++op->lineno;
  1480.       /* Traditionally, end of line ends a string constant with no error.
  1481.          So exit the loop and record the new line.  */
  1482.       if (traditional) {
  1483.         beg_of_line = ibp;
  1484.         goto while2end;
  1485.       }
  1486.       if (pedantic || c == '\'') {
  1487.         error_with_line (line_for_error (start_line),
  1488.                  "unterminated string or character constant");
  1489.         goto while2end;
  1490.       }
  1491.       break;
  1492.  
  1493.     case '\\':
  1494.       if (ibp >= limit)
  1495.         break;
  1496.       if (*ibp == '\n') {
  1497.         /* Backslash newline is replaced by nothing at all,
  1498.            but keep the line counts correct.  */
  1499.         --obp;
  1500.         ++ibp;
  1501.         ++ip->lineno;
  1502.       } else {
  1503.         /* ANSI stupidly requires that in \\ the second \
  1504.            is *not* prevented from combining with a newline.  */
  1505.         while (*ibp == '\\' && ibp[1] == '\n') {
  1506.           ibp += 2;
  1507.           ++ip->lineno;
  1508.         }
  1509.         *obp++ = *ibp++;
  1510.       }
  1511.       break;
  1512.  
  1513.     case '\"':
  1514.     case '\'':
  1515.       if (ibp[-1] == c)
  1516.         goto while2end;
  1517.       break;
  1518.     }
  1519.       }
  1520.     while2end:
  1521.       break;
  1522.  
  1523.     case '/':
  1524.       if (*ibp == '\\' && ibp[1] == '\n')
  1525.     newline_fix (ibp);
  1526.       /* Don't look for comments inside a macro definition.  */
  1527.       if (ip->macro != 0)
  1528.     goto randomchar;
  1529.       /* A comment constitutes white space, so it can terminate an identifier.
  1530.      Process the identifier, if any.  */
  1531.       if (ident_length)
  1532.     goto specialchar;
  1533.       if (cplusplus && *ibp == '/') {
  1534.     /* C++ style comment... */
  1535.     start_line = ip->lineno;
  1536.  
  1537.     --ibp;            /* Back over the slash */
  1538.     --obp;
  1539.  
  1540.     /* Comments are equivalent to spaces. */
  1541.     if (! put_out_comments)
  1542.       *obp++ = ' ';
  1543.     else {
  1544.       /* must fake up a comment here */
  1545.       *obp++ = '/';
  1546.       *obp++ = '/';
  1547.     }
  1548.     {
  1549.       U_CHAR *before_bp = ibp+2;
  1550.  
  1551.       while (ibp < limit) {
  1552.         if (*ibp == '\\' && ibp[1] == '\n') {
  1553.           ip->lineno++;
  1554.           ibp += 2;
  1555.         } else if (*ibp++ == '\n') {
  1556.           ibp--;
  1557.           if (put_out_comments) {
  1558.         bcopy (before_bp, obp, ibp - before_bp);
  1559.         obp += ibp - before_bp;
  1560.           }
  1561.           break;
  1562.         }
  1563.       }
  1564.       break;
  1565.     }
  1566.       }
  1567.       if (*ibp != '*')
  1568.     goto randomchar;
  1569.  
  1570.       /* We have a comment.  Skip it, optionally copying it to output.  */
  1571.  
  1572.       start_line = ip->lineno;
  1573.  
  1574.       ++ibp;            /* Skip the star. */
  1575.  
  1576.       /* Comments are equivalent to spaces.
  1577.      Note that we already output the slash; we might not want it.
  1578.      For -traditional, a comment is equivalent to nothing.  */
  1579.       if (! put_out_comments) {
  1580.     if (traditional)
  1581.       obp--;
  1582.     else
  1583.       obp[-1] = ' ';
  1584.       }
  1585.       else
  1586.     *obp++ = '*';
  1587.  
  1588.       {
  1589.     U_CHAR *before_bp = ibp;
  1590.  
  1591.     while (ibp < limit) {
  1592.       switch (*ibp++) {
  1593.       case '/':
  1594.         if (warn_comments && ibp < limit && *ibp == '*')
  1595.           warning("`/*' within comment");
  1596.         break;
  1597.       case '*':
  1598.         if (*ibp == '\\' && ibp[1] == '\n')
  1599.           newline_fix (ibp);
  1600.         if (ibp >= limit || *ibp == '/')
  1601.           goto comment_end;
  1602.         break;
  1603.       case '\n':
  1604.         ++ip->lineno;
  1605.         /* Copy the newline into the output buffer, in order to
  1606.            avoid the pain of a #line every time a multiline comment
  1607.            is seen.  */
  1608.         if (!put_out_comments)
  1609.           *obp++ = '\n';
  1610.         ++op->lineno;
  1611.       }
  1612.     }
  1613.       comment_end:
  1614.  
  1615.     if (ibp >= limit)
  1616.       error_with_line (line_for_error (start_line),
  1617.                "unterminated comment");
  1618.     else {
  1619.       ibp++;
  1620.       if (put_out_comments) {
  1621.         bcopy (before_bp, obp, ibp - before_bp);
  1622.         obp += ibp - before_bp;
  1623.       }
  1624.     }
  1625.       }
  1626.       break;
  1627.  
  1628.     case '$':
  1629.       if (!dollars_in_ident)
  1630.     goto randomchar;
  1631.       goto letter;
  1632.  
  1633.     case '0': case '1': case '2': case '3': case '4':
  1634.     case '5': case '6': case '7': case '8': case '9':
  1635.       /* If digit is not part of identifier, it starts a number,
  1636.      which means that following letters are not an identifier.
  1637.      "0x5" does not refer to an identifier "x5".
  1638.      So copy all alphanumerics that follow without accumulating
  1639.      as an identifier.  Periods also, for sake of "3.e7".  */
  1640.  
  1641.       if (ident_length == 0) {
  1642.     while (ibp < limit) {
  1643.       while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  1644.         ++ip->lineno;
  1645.         ibp += 2;
  1646.       }
  1647.       c = *ibp++;
  1648.       if (!isalnum (c) && c != '.' && c != '_') {
  1649.         --ibp;
  1650.         break;
  1651.       }
  1652.       *obp++ = c;
  1653.       /* A sign can be part of a preprocessing number
  1654.          if it follows an e.  */
  1655.       if (c == 'e' || c == 'E') {
  1656.         while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  1657.           ++ip->lineno;
  1658.           ibp += 2;
  1659.         }
  1660.         if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
  1661.           *obp++ = *ibp++;
  1662.           /* But traditional C does not let the token go past the sign.  */
  1663.           if (traditional)
  1664.         break;
  1665.         }
  1666.       }
  1667.     }
  1668.     break;
  1669.       }
  1670.       /* fall through */
  1671.  
  1672.     case '_':
  1673.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  1674.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  1675.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  1676.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  1677.     case 'y': case 'z':
  1678.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  1679.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  1680.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  1681.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  1682.     case 'Y': case 'Z':
  1683.     letter:
  1684.       ident_length++;
  1685.       /* Compute step of hash function, to avoid a proc call on every token */
  1686.       hash = HASHSTEP (hash, c);
  1687.       break;
  1688.  
  1689.     case '\n':
  1690.       /* If reprocessing a macro expansion, newline is a special marker.  */
  1691.       if (ip->macro != 0) {
  1692.     /* Newline White is a "funny space" to separate tokens that are
  1693.        supposed to be separate but without space between.
  1694.        Here White means any horizontal whitespace character.
  1695.        Newline - marks a recursive macro use that is not
  1696.        supposed to be expandable.  */
  1697.  
  1698.     if (*ibp == '-') {
  1699.       /* Newline - inhibits expansion of preceding token.
  1700.          If expanding a macro arg, we keep the newline -.
  1701.          In final output, it is deleted.  */
  1702.       if (! concatenated) {
  1703.         ident_length = 0;
  1704.         hash = 0;
  1705.       }
  1706.       ibp++;
  1707.       if (!output_marks) {
  1708.         obp--;
  1709.       } else {
  1710.         /* If expanding a macro arg, keep the newline -.  */
  1711.         *obp++ = '-';
  1712.       }
  1713.     } else if (is_space[*ibp]) {
  1714.       /* Newline Space does not prevent expansion of preceding token
  1715.          so expand the preceding token and then come back.  */
  1716.       if (ident_length > 0)
  1717.         goto specialchar;
  1718.  
  1719.       /* If generating final output, newline space makes a space.  */
  1720.       if (!output_marks) {
  1721.         obp[-1] = *ibp++;
  1722.         /* And Newline Newline makes a newline, so count it.  */
  1723.         if (obp[-1] == '\n')
  1724.           op->lineno++;
  1725.       } else {
  1726.         /* If expanding a macro arg, keep the newline space.
  1727.            If the arg gets stringified, newline space makes nothing.  */
  1728.         *obp++ = *ibp++;
  1729.       }
  1730.     } else abort ();    /* Newline followed by something random?  */
  1731.     break;
  1732.       }
  1733.  
  1734.       /* If there is a pending identifier, handle it and come back here.  */
  1735.       if (ident_length > 0)
  1736.     goto specialchar;
  1737.  
  1738.       beg_of_line = ibp;
  1739.  
  1740.       /* Update the line counts and output a #line if necessary.  */
  1741.       ++ip->lineno;
  1742.       ++op->lineno;
  1743.       if (ip->lineno != op->lineno) {
  1744.     op->bufp = obp;
  1745.     output_line_command (ip, op, 1, same_file);
  1746.     check_expand (op, ip->length - (ip->bufp - ip->buf));
  1747.     obp = op->bufp;
  1748.       }
  1749.       break;
  1750.  
  1751.       /* Come here either after (1) a null character that is part of the input
  1752.      or (2) at the end of the input, because there is a null there.  */
  1753.     case 0:
  1754.       if (ibp <= limit)
  1755.     /* Our input really contains a null character.  */
  1756.     goto randomchar;
  1757.  
  1758.       /* At end of a macro-expansion level, pop it and read next level.  */
  1759.       if (ip->macro != 0) {
  1760.     obp--;
  1761.     ibp--;
  1762.     /* If traditional, and we have an identifier that ends here,
  1763.        process it now, so we get the right error for recursion.  */
  1764.     if (traditional && ident_length
  1765.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  1766.       redo_char = 1;
  1767.       goto randomchar;
  1768.     }
  1769.     POPMACRO;
  1770.     RECACHE;
  1771.     break;
  1772.       }
  1773.  
  1774.       /* If we don't have a pending identifier,
  1775.      return at end of input.  */
  1776.       if (ident_length == 0) {
  1777.     obp--;
  1778.     ibp--;
  1779.     op->bufp = obp;
  1780.     ip->bufp = ibp;
  1781.     goto ending;
  1782.       }
  1783.  
  1784.       /* If we do have a pending identifier, just consider this null
  1785.      a special character and arrange to dispatch on it again.
  1786.      The second time, IDENT_LENGTH will be zero so we will return.  */
  1787.  
  1788.       /* Fall through */
  1789.  
  1790. specialchar:
  1791.  
  1792.       /* Handle the case of a character such as /, ', " or null
  1793.      seen following an identifier.  Back over it so that
  1794.      after the identifier is processed the special char
  1795.      will be dispatched on again.  */
  1796.  
  1797.       ibp--;
  1798.       obp--;
  1799.       redo_char = 1;
  1800.  
  1801.     default:
  1802.  
  1803. randomchar:
  1804.  
  1805.       if (ident_length > 0) {
  1806.     register HASHNODE *hp;
  1807.  
  1808.     /* We have just seen an identifier end.  If it's a macro, expand it.
  1809.  
  1810.        IDENT_LENGTH is the length of the identifier
  1811.        and HASH is its hash code.
  1812.  
  1813.        The identifier has already been copied to the output,
  1814.        so if it is a macro we must remove it.
  1815.  
  1816.        If REDO_CHAR is 0, the char that terminated the identifier
  1817.        has been skipped in the output and the input.
  1818.        OBP-IDENT_LENGTH-1 points to the identifier.
  1819.        If the identifier is a macro, we must back over the terminator.
  1820.  
  1821.        If REDO_CHAR is 1, the terminating char has already been
  1822.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  1823.  
  1824.     for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  1825.          hp = hp->next) {
  1826.  
  1827.       if (hp->length == ident_length) {
  1828.         U_CHAR *obufp_before_macroname;
  1829.         int op_lineno_before_macroname;
  1830.         register int i = ident_length;
  1831.         register U_CHAR *p = hp->name;
  1832.         register U_CHAR *q = obp - i;
  1833.         int disabled;
  1834.  
  1835.         if (! redo_char)
  1836.           q--;
  1837.  
  1838.         do {        /* All this to avoid a strncmp () */
  1839.           if (*p++ != *q++)
  1840.         goto hashcollision;
  1841.         } while (--i);
  1842.  
  1843.         /* We found a use of a macro name.
  1844.            see if the context shows it is a macro call.  */
  1845.  
  1846.         /* Back up over terminating character if not already done.  */
  1847.         if (! redo_char) {
  1848.           ibp--;
  1849.           obp--;
  1850.         }
  1851.  
  1852.         obufp_before_macroname = obp - ident_length;
  1853.         op_lineno_before_macroname = op->lineno;
  1854.  
  1855.         /* Record whether the macro is disabled.  */
  1856.         disabled = hp->type == T_DISABLED;
  1857.  
  1858.         /* This looks like a macro ref, but if the macro was disabled,
  1859.            just copy its name and put in a marker if requested.  */
  1860.  
  1861.         if (disabled) {
  1862. #if 0
  1863.           /* This error check caught useful cases such as
  1864.          #define foo(x,y) bar(x(y,0), y)
  1865.          foo(foo, baz)  */
  1866.           if (traditional)
  1867.         error ("recursive use of macro `%s'", hp->name);
  1868. #endif
  1869.  
  1870.           if (output_marks) {
  1871.         check_expand (op, limit - ibp + 2);
  1872.         *obp++ = '\n';
  1873.         *obp++ = '-';
  1874.           }
  1875.           break;
  1876.         }
  1877.  
  1878.         /* If macro wants an arglist, verify that a '(' follows.
  1879.            first skip all whitespace, copying it to the output
  1880.            after the macro name.  Then, if there is no '(',
  1881.            decide this is not a macro call and leave things that way.  */
  1882.         if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  1883.         && hp->value.defn->nargs >= 0)
  1884.           {
  1885.         while (1) {
  1886.           /* Scan forward over whitespace, copying it to the output.  */
  1887.           if (ibp == limit && ip->macro != 0) {
  1888.             POPMACRO;
  1889.             RECACHE;
  1890.           }
  1891.           /* A comment: copy it unchanged or discard it.  */
  1892.           else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  1893.             if (put_out_comments) {
  1894.               *obp++ = '/';
  1895.               *obp++ = '*';
  1896.             } else if (! traditional) {
  1897.               *obp++ = ' ';
  1898.             }
  1899.             ibp += 2;
  1900.             while (ibp + 1 != limit
  1901.                && !(ibp[0] == '*' && ibp[1] == '/')) {
  1902.               /* We need not worry about newline-marks,
  1903.              since they are never found in comments.  */
  1904.               if (*ibp == '\n') {
  1905.             /* Newline in a file.  Count it.  */
  1906.             ++ip->lineno;
  1907.             ++op->lineno;
  1908.               }
  1909.               if (put_out_comments)
  1910.             *obp++ = *ibp++;
  1911.               else
  1912.             ibp++;
  1913.             }
  1914.             ibp += 2;
  1915.             if (put_out_comments) {
  1916.               *obp++ = '*';
  1917.               *obp++ = '/';
  1918.             }
  1919.           }
  1920.           else if (is_space[*ibp]) {
  1921.             *obp++ = *ibp++;
  1922.             if (ibp[-1] == '\n') {
  1923.               if (ip->macro == 0) {
  1924.             /* Newline in a file.  Count it.  */
  1925.             ++ip->lineno;
  1926.             ++op->lineno;
  1927.               } else if (!output_marks) {
  1928.             /* A newline mark, and we don't want marks
  1929.                in the output.  If it is newline-hyphen,
  1930.                discard it entirely.  Otherwise, it is
  1931.                newline-whitechar, so keep the whitechar.  */
  1932.             obp--;
  1933.             if (*ibp == '-')
  1934.               ibp++;
  1935.             else {
  1936.               if (*ibp == '\n')
  1937.                 ++op->lineno;
  1938.               *obp++ = *ibp++;
  1939.             }
  1940.               } else {
  1941.             /* A newline mark; copy both chars to the output.  */
  1942.             *obp++ = *ibp++;
  1943.               }
  1944.             }
  1945.           }
  1946.           else break;
  1947.         }
  1948.         if (*ibp != '(')
  1949.           break;
  1950.           }
  1951.  
  1952.         /* This is now known to be a macro call.
  1953.            Discard the macro name from the output,
  1954.            along with any following whitespace just copied.  */
  1955.         obp = obufp_before_macroname;
  1956.         op->lineno = op_lineno_before_macroname;
  1957.  
  1958.         /* Expand the macro, reading arguments as needed,
  1959.            and push the expansion on the input stack.  */
  1960.         ip->bufp = ibp;
  1961.         op->bufp = obp;
  1962.         macroexpand (hp, op);
  1963.  
  1964.         /* Reexamine input stack, since macroexpand has pushed
  1965.            a new level on it.  */
  1966.         obp = op->bufp;
  1967.         RECACHE;
  1968.         break;
  1969.       }
  1970. hashcollision:
  1971.            ;
  1972.     }            /* End hash-table-search loop */
  1973.     ident_length = hash = 0; /* Stop collecting identifier */
  1974.     redo_char = 0;
  1975.     concatenated = 0;
  1976.       }                /* End if (ident_length > 0) */
  1977.     }                /* End switch */
  1978.   }                /* End per-char loop */
  1979.  
  1980.   /* Come here to return -- but first give an error message
  1981.      if there was an unterminated successful conditional.  */
  1982.  ending:
  1983.   if (if_stack != ip->if_stack) {
  1984.     char *str;
  1985.     switch (if_stack->type) {
  1986.     case T_IF:
  1987.       str = "if";
  1988.       break;
  1989.     case T_IFDEF:
  1990.       str = "ifdef";
  1991.       break;
  1992.     case T_IFNDEF:
  1993.       str = "ifndef";
  1994.       break;
  1995.     case T_ELSE:
  1996.       str = "else";
  1997.       break;
  1998.     case T_ELIF:
  1999.       str = "elif";
  2000.       break;
  2001.     }
  2002.     error_with_line (line_for_error (if_stack->lineno),
  2003.              "unterminated #%s conditional", str);
  2004.   }
  2005.   if_stack = ip->if_stack;
  2006. }
  2007.  
  2008. /*
  2009.  * Rescan a string into a temporary buffer and return the result
  2010.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  2011.  *
  2012.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  2013.  * and insert such markers when appropriate.  See `rescan' for details.
  2014.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  2015.  * before substitution; it is 0 for other uses.
  2016.  */
  2017. FILE_BUF
  2018. expand_to_temp_buffer (buf, limit, output_marks)
  2019.      U_CHAR *buf, *limit;
  2020.      int output_marks;
  2021. {
  2022.   register FILE_BUF *ip;
  2023.   FILE_BUF obuf;
  2024.   int length = limit - buf;
  2025.   U_CHAR *buf1;
  2026.   int odepth = indepth;
  2027.  
  2028.   if (length < 0)
  2029.     abort ();
  2030.  
  2031.   /* Set up the input on the input stack.  */
  2032.  
  2033.   buf1 = (U_CHAR *) alloca (length + 1);
  2034.   {
  2035.     register U_CHAR *p1 = buf;
  2036.     register U_CHAR *p2 = buf1;
  2037.  
  2038.     while (p1 != limit)
  2039.       *p2++ = *p1++;
  2040.   }
  2041.   buf1[length] = 0;
  2042.  
  2043.   /* Set up to receive the output.  */
  2044.  
  2045.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  2046.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  2047.   obuf.fname = 0;
  2048.   obuf.macro = 0;
  2049.   obuf.free_ptr = 0;
  2050.  
  2051.   CHECK_DEPTH ({return obuf;});
  2052.  
  2053.   ++indepth;
  2054.  
  2055.   ip = &instack[indepth];
  2056.   ip->fname = 0;
  2057.   ip->macro = 0;
  2058.   ip->free_ptr = 0;
  2059.   ip->length = length;
  2060.   ip->buf = ip->bufp = buf1;
  2061.   ip->if_stack = if_stack;
  2062.  
  2063.   ip->lineno = obuf.lineno = 1;
  2064.  
  2065.   /* Scan the input, create the output.  */
  2066.  
  2067.   rescan (&obuf, output_marks);
  2068.  
  2069.   /* Pop input stack to original state.  */
  2070.   --indepth;
  2071.  
  2072.   if (indepth != odepth)
  2073.     abort ();
  2074.  
  2075.   /* Record the output.  */
  2076.   obuf.length = obuf.bufp - obuf.buf;
  2077.  
  2078.   return obuf;
  2079. }
  2080.  
  2081. /*
  2082.  * Process a # directive.  Expects IP->bufp to point to the '#', as in
  2083.  * `#define foo bar'.  Passes to the command handler
  2084.  * (do_define, do_include, etc.): the addresses of the 1st and
  2085.  * last chars of the command (starting immediately after the #
  2086.  * keyword), plus op and the keyword table pointer.  If the command
  2087.  * contains comments it is copied into a temporary buffer sans comments
  2088.  * and the temporary buffer is passed to the command handler instead.
  2089.  * Likewise for backslash-newlines.
  2090.  *
  2091.  * Returns nonzero if this was a known # directive.
  2092.  * Otherwise, returns zero, without advancing the input pointer.
  2093.  */
  2094.  
  2095. int
  2096. handle_directive (ip, op)
  2097.      FILE_BUF *ip, *op;
  2098. {
  2099.   register U_CHAR *bp, *cp;
  2100.   register struct directive *kt;
  2101.   register int ident_length;
  2102.   U_CHAR *resume_p;
  2103.  
  2104.   /* Nonzero means we must copy the entire command
  2105.      to get rid of comments or backslash-newlines.  */
  2106.   int copy_command = 0;
  2107.  
  2108.   U_CHAR *ident, *after_ident;
  2109.  
  2110.   bp = ip->bufp;
  2111.   /* Skip whitespace and \-newline.  */
  2112.   while (1) {
  2113.     if (is_hor_space[*bp])
  2114.       bp++;
  2115.     else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
  2116.       ip->bufp = bp;
  2117.       skip_to_end_of_comment (ip, &ip->lineno);
  2118.       bp = ip->bufp;
  2119.     } else if (*bp == '\\' && bp[1] == '\n') {
  2120.       bp += 2; ip->lineno++;
  2121.     } else break;
  2122.   }
  2123.  
  2124.   /* Now find end of directive name.
  2125.      If we encounter a backslash-newline, exchange it with any following
  2126.      symbol-constituents so that we end up with a contiguous name.  */
  2127.  
  2128.   cp = bp;
  2129.   while (1) {
  2130.     if (is_idchar[*cp])
  2131.       cp++;
  2132.     else {
  2133.       if (*cp == '\\' && cp[1] == '\n')
  2134.     name_newline_fix (cp);
  2135.       if (is_idchar[*cp])
  2136.     cp++;
  2137.       else break;
  2138.     }
  2139.   }
  2140.   ident_length = cp - bp;
  2141.   ident = bp;
  2142.   after_ident = cp;
  2143.  
  2144.   /* A line of just `#' becomes blank.  */
  2145.  
  2146.   if (ident_length == 0 && *after_ident == '\n') {
  2147.     ip->bufp = after_ident;
  2148.     return 1;
  2149.   }
  2150.  
  2151.   /*
  2152.    * Decode the keyword and call the appropriate expansion
  2153.    * routine, after moving the input pointer up to the next line.
  2154.    */
  2155.   for (kt = directive_table; kt->length > 0; kt++) {
  2156.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  2157.       register U_CHAR *buf;
  2158.       register U_CHAR *limit = ip->buf + ip->length;
  2159.       int unterminated = 0;
  2160.  
  2161.       /* Nonzero means do not delete comments within the directive.
  2162.      #define needs this when -traditional.  */
  2163.       int keep_comments = traditional && kt->traditional_comments;
  2164.  
  2165.       /* Find the end of this command (first newline not backslashed
  2166.      and not in a string or comment).
  2167.      Set COPY_COMMAND if the command must be copied
  2168.      (it contains a backslash-newline or a comment).  */
  2169.  
  2170.       buf = bp = after_ident;
  2171.       while (bp < limit) {
  2172.     register U_CHAR c = *bp++;
  2173.     switch (c) {
  2174.     case '\\':
  2175.       if (bp < limit) {
  2176.         if (*bp == '\n') {
  2177.           ip->lineno++;
  2178.           copy_command = 1;
  2179.         }
  2180.         bp++;
  2181.       }
  2182.       break;
  2183.  
  2184.     case '\'':
  2185.     case '\"':
  2186.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  2187.       /* Don't bother calling the directive if we already got an error
  2188.          message due to unterminated string.  Skip everything and pretend
  2189.          we called the directive.  */
  2190.       if (unterminated) {
  2191.         if (traditional) {
  2192.           /* Traditional preprocessing permits unterminated strings.  */
  2193.           ip->bufp = bp;
  2194.           goto endloop1;
  2195.         }
  2196.         ip->bufp = bp;
  2197.         return 1;
  2198.       }
  2199.       break;
  2200.  
  2201.       /* <...> is special for #include.  */
  2202.     case '<':
  2203.       if (!kt->angle_brackets)
  2204.         break;
  2205.       while (*bp && *bp != '>') bp++;
  2206.       break;
  2207.  
  2208.     case '/':
  2209.       if (*bp == '\\' && bp[1] == '\n')
  2210.         newline_fix (bp);
  2211.       if (*bp == '*'
  2212.           || (cplusplus && *bp == '/')) {
  2213.         U_CHAR *obp = bp - 1;
  2214.         ip->bufp = bp + 1;
  2215.         skip_to_end_of_comment (ip, &ip->lineno);
  2216.         bp = ip->bufp;
  2217.         /* No need to copy the command because of a comment at the end;
  2218.            just don't include the comment in the directive.  */
  2219.         if (bp == limit || *bp == '\n') {
  2220.           bp = obp;
  2221.           goto endloop1;
  2222.         }
  2223.         /* Don't remove the comments if -traditional.  */
  2224.         if (! keep_comments)
  2225.           copy_command++;
  2226.       }
  2227.       break;
  2228.  
  2229.     case '\n':
  2230.       --bp;        /* Point to the newline */
  2231.       ip->bufp = bp;
  2232.       goto endloop1;
  2233.     }
  2234.       }
  2235.       ip->bufp = bp;
  2236.  
  2237.     endloop1:
  2238.       resume_p = ip->bufp;
  2239.       /* BP is the end of the directive.
  2240.      RESUME_P is the next interesting data after the directive.
  2241.      A comment may come between.  */
  2242.  
  2243.       if (copy_command) {
  2244.     register U_CHAR *xp = buf;
  2245.     /* Need to copy entire command into temp buffer before dispatching */
  2246.  
  2247.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  2248.                           some slop */
  2249.     buf = cp;
  2250.  
  2251.     /* Copy to the new buffer, deleting comments
  2252.        and backslash-newlines (and whitespace surrounding the latter).  */
  2253.  
  2254.     while (xp < bp) {
  2255.       register U_CHAR c = *xp++;
  2256.       *cp++ = c;
  2257.  
  2258.       switch (c) {
  2259.       case '\n':
  2260.         break;
  2261.  
  2262.         /* <...> is special for #include.  */
  2263.       case '<':
  2264.         if (!kt->angle_brackets)
  2265.           break;
  2266.         while (xp < bp && c != '>') {
  2267.           c = *xp++;
  2268.           if (c == '\\' && xp < bp && *xp == '\n')
  2269.         xp++, ip->lineno++;
  2270.           else
  2271.         *cp++ = c;
  2272.         }
  2273.         break;
  2274.  
  2275.       case '\\':
  2276.         if (*xp == '\n') {
  2277.           xp++;
  2278.           cp--;
  2279.           if (cp != buf && is_space[cp[-1]]) {
  2280.         while (cp != buf && is_space[cp[-1]]) cp--;
  2281.         cp++;
  2282.         SKIP_WHITE_SPACE (xp);
  2283.           } else if (is_space[*xp]) {
  2284.         *cp++ = *xp++;
  2285.         SKIP_WHITE_SPACE (xp);
  2286.           }
  2287.         }
  2288.         break;
  2289.  
  2290.       case '\'':
  2291.       case '\"':
  2292.         {
  2293.           register U_CHAR *bp1
  2294.         = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
  2295.           while (xp != bp1)
  2296.         *cp++ = *xp++;
  2297.         }
  2298.         break;
  2299.  
  2300.       case '/':
  2301.         if (*xp == '*'
  2302.         || (cplusplus && *xp == '/')) {
  2303.           ip->bufp = xp + 1;
  2304.           skip_to_end_of_comment (ip, 0);
  2305.           if (keep_comments)
  2306.         while (xp != ip->bufp)
  2307.           *cp++ = *xp++;
  2308.           /* Delete or replace the slash.  */
  2309.           else if (traditional)
  2310.         cp--;
  2311.           else
  2312.         cp[-1] = ' ';
  2313.           xp = ip->bufp;
  2314.         }
  2315.       }
  2316.     }
  2317.  
  2318.     /* Null-terminate the copy.  */
  2319.  
  2320.     *cp = 0;
  2321.       }
  2322.       else
  2323.     cp = bp;
  2324.  
  2325.       ip->bufp = resume_p;
  2326.  
  2327.       /* Some directives should be written out for cc1 to process,
  2328.      just as if they were not defined.  */
  2329.  
  2330.       if (kt->pass_thru) {
  2331.         int len;
  2332.  
  2333.     /* Output directive name.  */
  2334.         check_expand (op, kt->length+1);
  2335.         *op->bufp++ = '#';
  2336.         bcopy (kt->name, op->bufp, kt->length);
  2337.         op->bufp += kt->length;
  2338.  
  2339.     /* Output arguments.  */
  2340.         len = (cp - buf);
  2341.         check_expand (op, len);
  2342.         bcopy (buf, op->bufp, len);
  2343.         op->bufp += len;
  2344.       }
  2345.  
  2346.       /* Call the appropriate command handler.  buf now points to
  2347.      either the appropriate place in the input buffer, or to
  2348.      the temp buffer if it was necessary to make one.  cp
  2349.      points to the first char after the contents of the (possibly
  2350.      copied) command, in either case. */
  2351.       (*kt->func) (buf, cp, op, kt);
  2352.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  2353.  
  2354.       return 1;
  2355.     }
  2356.   }
  2357.  
  2358.   return 0;
  2359. }
  2360.  
  2361. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  2362.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  2363.                 };
  2364.  
  2365. /*
  2366.  * expand things like __FILE__.  Place the expansion into the output
  2367.  * buffer *without* rescanning.
  2368.  */
  2369. special_symbol (hp, op)
  2370.      HASHNODE *hp;
  2371.      FILE_BUF *op;
  2372. {
  2373.   char *buf;
  2374.   time_t t;
  2375.   int i, len;
  2376.   int true_indepth;
  2377.   FILE_BUF *ip = NULL;
  2378.   static struct tm *timebuf = NULL;
  2379.   struct tm *localtime ();
  2380.  
  2381.   int paren = 0;        /* For special `defined' keyword */
  2382.  
  2383.   for (i = indepth; i >= 0; i--)
  2384.     if (instack[i].fname != NULL) {
  2385.       ip = &instack[i];
  2386.       break;
  2387.     }
  2388.   if (ip == NULL) {
  2389.     error ("cccp error: not in any file?!");
  2390.     return;            /* the show must go on */
  2391.   }
  2392.  
  2393.   switch (hp->type) {
  2394.   case T_FILE:
  2395.   case T_BASE_FILE:
  2396.     {
  2397.       char *string;
  2398.       if (hp->type == T_FILE)
  2399.     string = ip->fname;
  2400.       else
  2401.     string = instack[0].fname;
  2402.  
  2403.       if (string)
  2404.     {
  2405.       buf = (char *) alloca (3 + strlen (string));
  2406.       sprintf (buf, "\"%s\"", string);
  2407.     }
  2408.       else
  2409.     buf = "\"\"";
  2410.  
  2411.       break;
  2412.     }
  2413.  
  2414.   case T_INCLUDE_LEVEL:
  2415.     true_indepth = 0;
  2416.     for (i = indepth; i >= 0; i--)
  2417.       if (instack[i].fname != NULL)
  2418.         true_indepth++;
  2419.  
  2420.     buf = (char *) alloca (8);    /* Eigth bytes ought to be more than enough */
  2421.     sprintf (buf, "%d", true_indepth - 1);
  2422.     break;
  2423.  
  2424.   case T_VERSION:
  2425.     buf = (char *) alloca (3 + strlen (version_string));
  2426.     sprintf (buf, "\"%s\"", version_string);
  2427.     break;
  2428.  
  2429.   case T_CONST:
  2430.     buf = (char *) alloca (4 * sizeof (int));
  2431.     sprintf (buf, "%d", hp->value.ival);
  2432.     break;
  2433.  
  2434.   case T_SPECLINE:
  2435.     buf = (char *) alloca (10);
  2436.     sprintf (buf, "%d", ip->lineno);
  2437.     break;
  2438.  
  2439.   case T_DATE:
  2440.   case T_TIME:
  2441.     if (timebuf == NULL) {
  2442.       t = time (0);
  2443.       timebuf = localtime (&t);
  2444.     }
  2445.     buf = (char *) alloca (20);
  2446.     if (hp->type == T_DATE)
  2447.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  2448.           timebuf->tm_mday, timebuf->tm_year + 1900);
  2449.     else
  2450.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  2451.           timebuf->tm_sec);
  2452.     break;
  2453.  
  2454.   case T_SPEC_DEFINED:
  2455.     buf = " 0 ";        /* Assume symbol is not defined */
  2456.     ip = &instack[indepth];
  2457.     SKIP_WHITE_SPACE (ip->bufp);
  2458.     if (*ip->bufp == '(') {
  2459.       paren++;
  2460.       ip->bufp++;            /* Skip over the paren */
  2461.       SKIP_WHITE_SPACE (ip->bufp);
  2462.     }
  2463.  
  2464.     if (!is_idstart[*ip->bufp])
  2465.       goto oops;
  2466.     if (lookup (ip->bufp, -1, -1))
  2467.       buf = " 1 ";
  2468.     while (is_idchar[*ip->bufp])
  2469.       ++ip->bufp;
  2470.     SKIP_WHITE_SPACE (ip->bufp);
  2471.     if (paren) {
  2472.       if (*ip->bufp != ')')
  2473.     goto oops;
  2474.       ++ip->bufp;
  2475.     }
  2476.     break;
  2477.  
  2478. oops:
  2479.  
  2480.     error ("`defined' must be followed by ident or (ident)");
  2481.     break;
  2482.  
  2483.   default:
  2484.     error ("cccp error: invalid special hash type"); /* time for gdb */
  2485.     abort ();
  2486.   }
  2487.   len = strlen (buf);
  2488.   check_expand (op, len);
  2489.   bcopy (buf, op->bufp, len);
  2490.   op->bufp += len;
  2491.  
  2492.   return;
  2493. }
  2494.  
  2495.  
  2496. /* Routines to handle #directives */
  2497.  
  2498. /*
  2499.  * Process include file by reading it in and calling rescan.
  2500.  * Expects to see "fname" or <fname> on the input.
  2501.  */
  2502.  
  2503. do_include (buf, limit, op, keyword)
  2504.      U_CHAR *buf, *limit;
  2505.      FILE_BUF *op;
  2506.      struct directive *keyword;
  2507. {
  2508.   char *fname;        /* Dynamically allocated fname buffer */
  2509.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  2510.  
  2511.   struct file_name_list *stackp = include; /* Chain of dirs to search */
  2512.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  2513.   int flen;
  2514.  
  2515.   int f;            /* file number */
  2516.  
  2517.   int retried = 0;        /* Have already tried macro
  2518.                    expanding the include line*/
  2519.   FILE_BUF trybuf;        /* It got expanded into here */
  2520.   int system_header_p = 0;    /* 0 for "...", 1 for <...> */
  2521.  
  2522.   f= -1;            /* JF we iz paranoid! */
  2523.  
  2524. get_filename:
  2525.  
  2526.   fbeg = buf;
  2527.   SKIP_WHITE_SPACE (fbeg);
  2528.   /* Discard trailing whitespace so we can easily see
  2529.      if we have parsed all the significant chars we were given.  */
  2530.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  2531.  
  2532.   switch (*fbeg++) {
  2533.   case '\"':
  2534.     fend = fbeg;
  2535.     while (fend != limit && *fend != '\"')
  2536.       fend++;
  2537.     if (*fend == '\"' && fend + 1 == limit) {
  2538.       FILE_BUF *fp;
  2539.  
  2540.       /* We have "filename".  Figure out directory this source
  2541.      file is coming from and put it on the front of the list. */
  2542.  
  2543.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  2544.       if (ignore_srcdir) break;
  2545.  
  2546.       for (fp = &instack[indepth]; fp >= instack; fp--)
  2547.     {
  2548.       int n;
  2549.       char *ep,*nam;
  2550.       extern char *rindex ();
  2551.  
  2552.       if ((nam = fp->fname) != NULL) {
  2553.         /* Found a named file.  Figure out dir of the file,
  2554.            and put it in front of the search list.  */
  2555.         dsp[0].next = stackp;
  2556.         stackp = dsp;
  2557. #ifndef VMS
  2558.         ep = rindex (nam, '/');
  2559. #else                /* VMS */
  2560.         ep = rindex (nam, ']');
  2561.         if (ep == NULL) ep = rindex (nam, '>');
  2562.         if (ep == NULL) ep = rindex (nam, ':');
  2563.         if (ep != NULL) ep++;
  2564. #endif                /* VMS */
  2565.         if (ep != NULL) {
  2566.           n = ep - nam;
  2567.           dsp[0].fname = (char *) alloca (n + 1);
  2568.           strncpy (dsp[0].fname, nam, n);
  2569.           dsp[0].fname[n] = '\0';
  2570.           if (n > max_include_len) max_include_len = n;
  2571.         } else {
  2572.           dsp[0].fname = 0; /* Current directory */
  2573.         }
  2574.         break;
  2575.       }
  2576.     }
  2577.       break;
  2578.     }
  2579.     goto fail;
  2580.  
  2581.   case '<':
  2582.     fend = fbeg;
  2583.     while (fend != limit && *fend != '>') fend++;
  2584.     if (*fend == '>' && fend + 1 == limit) {
  2585.       system_header_p = 1;
  2586.       /* If -I-, start with the first -I dir after the -I-.  */
  2587.       if (first_bracket_include)
  2588.     stackp = first_bracket_include;
  2589.       break;
  2590.     }
  2591.     goto fail;
  2592.  
  2593.   default:
  2594.   fail:
  2595.     if (retried) {
  2596.       error ("#include expects \"fname\" or <fname>");
  2597.       return;
  2598.     } else {
  2599.       trybuf = expand_to_temp_buffer (buf, limit, 0);
  2600.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  2601.       bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
  2602.       limit = buf + (trybuf.bufp - trybuf.buf);
  2603.       free (trybuf.buf);
  2604.       retried++;
  2605.       goto get_filename;
  2606.     }
  2607.   }
  2608.  
  2609.   flen = fend - fbeg;
  2610.   fname = (char *) alloca (max_include_len + flen + 2);
  2611.   /* + 2 above for slash and terminating null.  */
  2612.  
  2613.   /* If specified file name is absolute, just open it.  */
  2614.  
  2615.   if (*fbeg == '/') {
  2616.     strncpy (fname, fbeg, flen);
  2617.     fname[flen] = 0;
  2618.     f = open (fname, O_RDONLY, 0666);
  2619.   } else {
  2620.     /* Search directory path, trying to open the file.
  2621.        Copy each filename tried into FNAME.  */
  2622.  
  2623.     for (; stackp; stackp = stackp->next) {
  2624.       if (stackp->fname) {
  2625.     strcpy (fname, stackp->fname);
  2626.     strcat (fname, "/");
  2627.     fname[strlen (fname) + flen] = 0;
  2628.       } else {
  2629.     fname[0] = 0;
  2630.       }
  2631.       strncat (fname, fbeg, flen);
  2632. #ifdef VMS
  2633.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  2634.          full VMS file specification */
  2635.       if (stackp->fname && (stackp->fname[0] != 0)) {
  2636.     /* Fix up the filename */
  2637.     hack_vms_include_specification (fname);
  2638.       } else {
  2639.           /* This is a normal VMS filespec, so use it unchanged.  */
  2640.     strncpy (fname, fbeg, flen);
  2641.     fname[flen] = 0;
  2642.       }
  2643. #endif /* VMS */
  2644.       if ((f = open (fname, O_RDONLY, 0666)) >= 0)
  2645.     break;
  2646.     }
  2647.   }
  2648.  
  2649.   if (f < 0) {
  2650.     strncpy (fname, fbeg, flen);
  2651.     fname[flen] = 0;
  2652.     error_from_errno (fname);
  2653.  
  2654.     /* For -M, add this file to the dependencies.  */
  2655.     if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2656.       if (system_header_p)
  2657.     warning ("nonexistent file <%.*s> omitted from dependency output",
  2658.          fend - fbeg, fbeg);
  2659.       else
  2660.     {
  2661.       deps_output (fbeg, fend - fbeg);
  2662.       deps_output (" ", 0);
  2663.     }
  2664.     }
  2665.   } else {
  2666.  
  2667.     /* Check to see if this include file is a once-only include file.
  2668.        If so, give up.  */
  2669.  
  2670.     struct file_name_list* ptr;
  2671.  
  2672.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  2673.       if (!strcmp (ptr->fname, fname)) {
  2674.     close (f);
  2675.         return;                /* This file was once'd. */
  2676.       }
  2677.     }
  2678.  
  2679.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  2680.       if (!strcmp (ptr->fname, fname))
  2681.         break;                /* This file was included before. */
  2682.     }
  2683.  
  2684.     if (ptr == 0) {
  2685.       /* This is the first time for this file.  */
  2686.       /* Add it to list of files included.  */
  2687.  
  2688.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2689.       ptr->next = all_include_files;
  2690.       all_include_files = ptr;
  2691.       ptr->fname = savestring (fname);
  2692.  
  2693.       /* For -M, add this file to the dependencies.  */
  2694.       if (print_deps > (system_header_p || (system_include_depth > 0))) {
  2695.     deps_output (fname, strlen (fname));
  2696.     deps_output (" ", 0);
  2697.       }
  2698.     }   
  2699.  
  2700.     if (system_header_p)
  2701.       system_include_depth++;
  2702.  
  2703.     /* Actually process the file.  */
  2704.     finclude (f, fname, op);
  2705.  
  2706.     if (system_header_p)
  2707.       system_include_depth--;
  2708.  
  2709.     close (f);
  2710.   }
  2711. }
  2712.  
  2713. /* Process the contents of include file FNAME, already open on descriptor F,
  2714.    with output to OP.  */
  2715.  
  2716. finclude (f, fname, op)
  2717.      int f;
  2718.      char *fname;
  2719.      FILE_BUF *op;
  2720. {
  2721.   int st_mode;
  2722.   long st_size;
  2723.   long i;
  2724.   FILE_BUF *fp;            /* For input stack frame */
  2725.   int success = 0;
  2726.  
  2727.   CHECK_DEPTH (return;);
  2728.  
  2729.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  2730.     goto nope;        /* Impossible? */
  2731.  
  2732.   fp = &instack[indepth + 1];
  2733.   bzero (fp, sizeof (FILE_BUF));
  2734.   fp->fname = fname;
  2735.   fp->length = 0;
  2736.   fp->lineno = 1;
  2737.   fp->if_stack = if_stack;
  2738.  
  2739.   if (S_ISREG (st_mode)) {
  2740.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2741.     fp->bufp = fp->buf;
  2742.  
  2743.     /* Read the file contents, knowing that st_size is an upper bound
  2744.        on the number of bytes we can read.  */
  2745.     while (st_size > 0) {
  2746.       i = read (f, fp->buf + fp->length, st_size);
  2747.       if (i <= 0) {
  2748.     if (i == 0) break;
  2749.     goto nope;
  2750.       }
  2751.       fp->length += i;
  2752.       st_size -= i;
  2753.     }
  2754.   }
  2755.   else {
  2756.     /* Cannot count its file size before reading.
  2757.        First read the entire file into heap and
  2758.        copy them into buffer on stack. */
  2759.  
  2760.     U_CHAR *bufp;
  2761.     U_CHAR *basep;
  2762.     int bsize = 2000;
  2763.  
  2764.     st_size = 0;
  2765.     basep = (U_CHAR *) xmalloc (bsize + 2);
  2766.     bufp = basep;
  2767.  
  2768.     for (;;) {
  2769.       i = read (f, bufp, bsize - st_size);
  2770.       if (i < 0)
  2771.     goto nope;      /* error! */
  2772.       if (i == 0)
  2773.     break;    /* End of file */
  2774.       st_size += i;
  2775.       bufp += i;
  2776.       if (bsize == st_size) {    /* Buffer is full! */
  2777.       bsize *= 2;
  2778.       basep = (U_CHAR *) xrealloc (basep, bsize + 2);
  2779.       bufp = basep + st_size;    /* May have moved */
  2780.     }
  2781.     }
  2782.     fp->buf = (U_CHAR *) alloca (st_size + 2);
  2783.     fp->bufp = fp->buf;
  2784.     bcopy (basep, fp->buf, st_size);
  2785.     fp->length = st_size;
  2786.     free (basep);
  2787.   }
  2788.  
  2789.   if (!no_trigraphs)
  2790.     trigraph_pcp (fp);
  2791.  
  2792.   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  2793.     fp->buf[fp->length++] = '\n';
  2794.   fp->buf[fp->length] = '\0';
  2795.  
  2796.   success = 1;
  2797.   indepth++;
  2798.  
  2799.   output_line_command (fp, op, 0, enter_file);
  2800.   rescan (op, 0);
  2801.   indepth--;
  2802.   output_line_command (&instack[indepth], op, 0, leave_file);
  2803.  
  2804. nope:
  2805.  
  2806.   if (!success)
  2807.     perror_with_name (fname);
  2808.  
  2809.   close (f);
  2810. }
  2811.  
  2812. /* The arglist structure is built by do_define to tell
  2813.    collect_definition where the argument names begin.  That
  2814.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  2815.    would contain pointers to the strings x, y, and z.
  2816.    Collect_definition would then build a DEFINITION node,
  2817.    with reflist nodes pointing to the places x, y, and z had
  2818.    appeared.  So the arglist is just convenience data passed
  2819.    between these two routines.  It is not kept around after
  2820.    the current #define has been processed and entered into the
  2821.    hash table. */
  2822.  
  2823. struct arglist {
  2824.   struct arglist *next;
  2825.   U_CHAR *name;
  2826.   int length;
  2827.   int argno;
  2828. };
  2829.  
  2830. /* Process a #define command.
  2831. BUF points to the contents of the #define command, as a continguous string.
  2832. LIMIT points to the first character past the end of the definition.
  2833. KEYWORD is the keyword-table entry for #define.  */
  2834.  
  2835. do_define (buf, limit, op, keyword)
  2836.      U_CHAR *buf, *limit;
  2837.      FILE_BUF *op;
  2838.      struct directive *keyword;
  2839. {
  2840.   U_CHAR *bp;            /* temp ptr into input buffer */
  2841.   U_CHAR *symname;        /* remember where symbol name starts */
  2842.   int sym_length;        /* and how long it is */
  2843.  
  2844.   DEFINITION *defn;
  2845.   int arglengths = 0;        /* Accumulate lengths of arg names
  2846.                    plus number of args.  */
  2847.   int hashcode;
  2848.  
  2849.   bp = buf;
  2850.  
  2851.   while (is_hor_space[*bp])
  2852.     bp++;
  2853.  
  2854.   symname = bp;            /* remember where it starts */
  2855.   while (is_idchar[*bp] && bp < limit) {
  2856.     bp++;
  2857.   }
  2858.   sym_length = bp - symname;
  2859.   if (sym_length == 0)
  2860.     error ("invalid macro name");
  2861.   else if (!is_idstart[*symname]) {
  2862.     U_CHAR *msg;            /* what pain... */
  2863.     msg = (U_CHAR *) alloca (sym_length + 1);
  2864.     bcopy (symname, msg, sym_length);
  2865.     msg[sym_length] = 0;
  2866.     error ("invalid macro name `%s'", msg);
  2867.   } else {
  2868.     if (! strncmp (symname, "defined", 7) && sym_length == 7)
  2869.       error ("defining `defined' as a macro");
  2870.   }
  2871.  
  2872.   /* lossage will occur if identifiers or control keywords are broken
  2873.      across lines using backslash.  This is not the right place to take
  2874.      care of that. */
  2875.  
  2876.   if (*bp == '(') {
  2877.     struct arglist *arg_ptrs = NULL;
  2878.     int argno = 0;
  2879.  
  2880.     bp++;            /* skip '(' */
  2881.     SKIP_WHITE_SPACE (bp);
  2882.  
  2883.     /* Loop over macro argument names.  */
  2884.     while (*bp != ')') {
  2885.       struct arglist *temp;
  2886.  
  2887.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  2888.       temp->name = bp;
  2889.       temp->next = arg_ptrs;
  2890.       temp->argno = argno++;
  2891.       arg_ptrs = temp;
  2892.  
  2893.       if (!is_idstart[*bp])
  2894.     warning ("parameter name starts with a digit in #define");
  2895.  
  2896.       /* Find the end of the arg name.  */
  2897.       while (is_idchar[*bp]) {
  2898.     bp++;
  2899.       }
  2900.       temp->length = bp - temp->name;
  2901.       arglengths += temp->length + 2;
  2902.       SKIP_WHITE_SPACE (bp);
  2903.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  2904.     error ("badly punctuated parameter list in #define");
  2905.     goto nope;
  2906.       }
  2907.       if (*bp == ',') {
  2908.     bp++;
  2909.     SKIP_WHITE_SPACE (bp);
  2910.       }
  2911.       if (bp >= limit) {
  2912.     error ("unterminated parameter list in #define");
  2913.     goto nope;
  2914.       }
  2915.     }
  2916.  
  2917.     ++bp;            /* skip paren */
  2918.     /* Skip exactly one space or tab if any.  */
  2919.     if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
  2920.     /* now everything from bp before limit is the definition. */
  2921.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  2922.  
  2923.     /* Now set defn->argnames to the result of concatenating
  2924.        the argument names in reverse order
  2925.        with comma-space between them.  */
  2926.     defn->argnames = (U_CHAR *) xmalloc (arglengths + 1);
  2927.     {
  2928.       struct arglist *temp;
  2929.       int i = 0;
  2930.       for (temp = arg_ptrs; temp; temp = temp->next) {
  2931.     bcopy (temp->name, &defn->argnames[i], temp->length);
  2932.     i += temp->length;
  2933.     if (temp->next != 0) {
  2934.       defn->argnames[i++] = ',';
  2935.       defn->argnames[i++] = ' ';
  2936.     }
  2937.       }
  2938.       defn->argnames[i] = 0;
  2939.     }
  2940.   } else {
  2941.     /* simple expansion or empty definition; gobble it */
  2942.     if (is_hor_space[*bp])
  2943.       ++bp;        /* skip exactly one blank/tab char */
  2944.     /* now everything from bp before limit is the definition. */
  2945.     defn = collect_expansion (bp, limit, -1, 0);
  2946.     defn->argnames = (U_CHAR *) "";
  2947.   }
  2948.  
  2949.   hashcode = hashf (symname, sym_length, HASHSIZE);
  2950.  
  2951.   {
  2952.     HASHNODE *hp;
  2953.     if ((hp = lookup (symname, sym_length, hashcode)) != NULL) {
  2954.       if (hp->type != T_MACRO
  2955.       || compare_defs (defn, hp->value.defn)) {
  2956.     U_CHAR *msg;            /* what pain... */
  2957.     msg = (U_CHAR *) alloca (sym_length + 20);
  2958.     bcopy (symname, msg, sym_length);
  2959.     strcpy ((char *) (msg + sym_length), " redefined");
  2960.     warning (msg);
  2961.       }
  2962.       /* Replace the old definition.  */
  2963.       hp->type = T_MACRO;
  2964.       hp->value.defn = defn;
  2965.     } else
  2966.       install (symname, sym_length, T_MACRO, defn, hashcode);
  2967.   }
  2968.  
  2969.   return 0;
  2970.  
  2971. nope:
  2972.  
  2973.   return 1;
  2974. }
  2975.  
  2976. /*
  2977.  * return zero if two DEFINITIONs are isomorphic
  2978.  */
  2979. int
  2980. compare_defs (d1, d2)
  2981.      DEFINITION *d1, *d2;
  2982. {
  2983.   register struct reflist *a1, *a2;
  2984.   register U_CHAR *p1 = d1->expansion;
  2985.   register U_CHAR *p2 = d2->expansion;
  2986.   int first = 1;
  2987.  
  2988.   if (d1->nargs != d2->nargs)
  2989.     return 1;
  2990.   if (strcmp ((char *)d1->argnames, (char *)d2->argnames))
  2991.     return 1;
  2992.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  2993.        a1 = a1->next, a2 = a2->next) {
  2994.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  2995.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  2996.     || a1->argno != a2->argno
  2997.     || a1->stringify != a2->stringify
  2998.     || a1->raw_before != a2->raw_before
  2999.     || a1->raw_after != a2->raw_after)
  3000.       return 1;
  3001.     first = 0;
  3002.     p1 += a1->nchars;
  3003.     p2 += a2->nchars;
  3004.   }
  3005.   if (a1 != a2)
  3006.     return 1;
  3007.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  3008.              p2, d2->length - (p2 - d2->expansion), 1))
  3009.     return 1;
  3010.   return 0;
  3011. }
  3012.  
  3013. /* Return 1 if two parts of two macro definitions are effectively different.
  3014.    One of the parts starts at BEG1 and has LEN1 chars;
  3015.    the other has LEN2 chars at BEG2.
  3016.    Any sequence of whitespace matches any other sequence of whitespace.
  3017.    FIRST means these parts are the first of a macro definition;
  3018.     so ignore leading whitespace entirely.
  3019.    LAST means these parts are the last of a macro definition;
  3020.     so ignore trailing whitespace entirely.  */
  3021.  
  3022. comp_def_part (first, beg1, len1, beg2, len2, last)
  3023.      int first;
  3024.      U_CHAR *beg1, *beg2;
  3025.      int len1, len2;
  3026.      int last;
  3027. {
  3028.   register U_CHAR *end1 = beg1 + len1;
  3029.   register U_CHAR *end2 = beg2 + len2;
  3030.   if (first) {
  3031.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  3032.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  3033.   }
  3034.   if (last) {
  3035.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  3036.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  3037.   }
  3038.   while (beg1 != end1 && beg2 != end2) {
  3039.     if (is_space[*beg1] && is_space[*beg2]) {
  3040.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  3041.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  3042.     } else if (*beg1 == *beg2) {
  3043.       beg1++; beg2++;
  3044.     } else break;
  3045.   }
  3046.   return (beg1 != end1) || (beg2 != end2);
  3047. }
  3048.  
  3049. /* Read a replacement list for a macro with parameters.
  3050.    Build the DEFINITION structure.
  3051.    Reads characters of text starting at BUF until LIMIT.
  3052.    ARGLIST specifies the formal parameters to look for
  3053.    in the text of the definition; NARGS is the number of args
  3054.    in that list, or -1 for a macro name that wants no argument list.
  3055.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  3056.    and NAMELEN is its length in characters.
  3057.    
  3058. Note that comments and backslash-newlines have already been deleted
  3059. from the argument.  */
  3060.  
  3061. /* Leading and trailing Space, Tab, etc. are converted to markers
  3062.    Newline Space, Newline Tab, etc.
  3063.    Newline Space makes a space in the final output
  3064.    but is discarded if stringified.  (Newline Tab is similar but
  3065.    makes a Tab instead.)
  3066.  
  3067.    If there is no trailing whitespace, a Newline Space is added at the end
  3068.    to prevent concatenation that would be contrary to the standard.  */
  3069.  
  3070. DEFINITION *
  3071. collect_expansion (buf, end, nargs, arglist)
  3072.      U_CHAR *buf, *end;
  3073.      int nargs;
  3074.      struct arglist *arglist;
  3075. {
  3076.   DEFINITION *defn;
  3077.   register U_CHAR *p, *limit, *lastp, *exp_p;
  3078.   struct reflist *endpat = NULL;
  3079.   /* Pointer to first nonspace after last ## seen.  */
  3080.   U_CHAR *concat = 0;
  3081.   /* Pointer to first nonspace after last single-# seen.  */
  3082.   U_CHAR *stringify = 0;
  3083.   int maxsize;
  3084.   int expected_delimiter = '\0';
  3085.  
  3086.   /* Scan thru the replacement list, ignoring comments and quoted
  3087.      strings, picking up on the macro calls.  It does a linear search
  3088.      thru the arg list on every potential symbol.  Profiling might say
  3089.      that something smarter should happen. */
  3090.  
  3091.   if (end < buf)
  3092.     abort ();
  3093.  
  3094.   /* Find the beginning of the trailing whitespace.  */
  3095.   /* Find end of leading whitespace.  */
  3096.   limit = end;
  3097.   p = buf;
  3098.   while (p < limit && is_space[limit[-1]]) limit--;
  3099.   while (p < limit && is_space[*p]) p++;
  3100.  
  3101.   /* Allocate space for the text in the macro definition.
  3102.      Leading and trailing whitespace chars need 2 bytes each.
  3103.      Each other input char may or may not need 1 byte,
  3104.      so this is an upper bound.
  3105.      The extra 2 are for invented trailing newline-marker and final null.  */
  3106.   maxsize = (sizeof (DEFINITION)
  3107.          + 2 * (end - limit) + 2 * (p - buf)
  3108.          + (limit - p) + 3);
  3109.   defn = (DEFINITION *) xcalloc (1, maxsize);
  3110.  
  3111.   defn->nargs = nargs;
  3112.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  3113.   lastp = exp_p;
  3114.  
  3115.   p = buf;
  3116.  
  3117.   /* Convert leading whitespace to Newline-markers.  */
  3118.   while (p < limit && is_space[*p]) {
  3119.     *exp_p++ = '\n';
  3120.     *exp_p++ = *p++;
  3121.   }
  3122.  
  3123.   if (p + 1 < limit && p[0] == '#' && p[1] == '#') {
  3124.     error ("## operator at start of macro definition");
  3125.     p += 2;
  3126.   }
  3127.  
  3128.   /* Process the main body of the definition.  */
  3129.   while (p < limit) {
  3130.     int skipped_arg = 0;
  3131.     register U_CHAR c = *p++;
  3132.  
  3133.     *exp_p++ = c;
  3134.  
  3135.     if (!traditional) {
  3136.       switch (c) {
  3137.       case '\'':
  3138.       case '\"':
  3139.     for (; p < limit && *p != c; p++) {
  3140.       *exp_p++ = *p;
  3141.       if (*p == '\\') {
  3142.         *exp_p++ = *++p;
  3143.       }
  3144.     }
  3145.     *exp_p++ = *p++;
  3146.     break;
  3147.  
  3148.     /* Special hack: if a \# is written in the #define
  3149.        include a # in the definition.  This is useless for C code
  3150.        but useful for preprocessing other things.  */
  3151.  
  3152.       case '\\':
  3153.     if (p < limit && *p == '#') {
  3154.       /* Pass through this # */
  3155.       exp_p--;
  3156.       *exp_p++ = *p++;
  3157.     } else if (p < limit) {
  3158.       /* Otherwise backslash goes through but makes next char ordinary.  */
  3159.       *exp_p++ = *p++;
  3160.     }
  3161.     break;
  3162.  
  3163.       case '#':
  3164.     if (p < limit && *p == '#') {
  3165.       /* ##: concatenate preceding and following tokens.  */
  3166.       /* Take out the first #, discard preceding whitespace.  */
  3167.       exp_p--;
  3168.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  3169.         --exp_p;
  3170.       /* Skip the second #.  */
  3171.       p++;
  3172.       /* Discard following whitespace.  */
  3173.       SKIP_WHITE_SPACE (p);
  3174.       concat = p;
  3175.       if (limit <= p)
  3176.         error ("## operator at end of macro definition");
  3177.     } else {
  3178.       /* Single #: stringify following argument ref.
  3179.          Don't leave the # in the expansion.  */
  3180.       exp_p--;
  3181.       SKIP_WHITE_SPACE (p);
  3182.       if (p == limit || ! is_idstart[*p] || nargs <= 0)
  3183.         error ("# operator should be followed by a macro argument name");
  3184.       else
  3185.         stringify = p;
  3186.     }
  3187.     break;
  3188.       }
  3189.     } else {
  3190.       /* In -traditional mode, recognize arguments inside strings and
  3191.      and character constants, and ignore special properties of #.
  3192.      Arguments inside strings are considered "stringified", but no
  3193.      extra quote marks are supplied.  */
  3194.       switch (c) {
  3195.       case '\'':
  3196.       case '\"':
  3197.     if (expected_delimiter != '\0') {
  3198.       if (c == expected_delimiter)
  3199.         expected_delimiter = '\0';
  3200.     } else
  3201.       expected_delimiter = c;
  3202.     break;
  3203.  
  3204.       case '\\':
  3205.     /* Backslash quotes delimiters and itself, but not macro args.  */
  3206.     if (expected_delimiter != 0 && p < limit
  3207.         && (*p == expected_delimiter || *p == '\\')) {
  3208.       *exp_p++ = *p++;
  3209.       continue;
  3210.     }
  3211.     break;
  3212.  
  3213.       case '/':
  3214.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  3215.       break;
  3216.     if (*p == '*') {
  3217.       /* If we find a comment that wasn't removed by handle_directive,
  3218.          this must be -traditional.  So replace the comment with
  3219.          nothing at all.  */
  3220.       exp_p--;
  3221.       p += 1;
  3222.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  3223.         p++;
  3224. #if 0
  3225.       /* Mark this as a concatenation-point, as if it had been ##.  */
  3226.       concat = p;
  3227. #endif
  3228.     }
  3229.     break;
  3230.       }
  3231.     }
  3232.  
  3233.     if (is_idchar[c] && nargs > 0) {
  3234.       U_CHAR *id_beg = p - 1;
  3235.       int id_len;
  3236.  
  3237.       --exp_p;
  3238.       while (p != limit && is_idchar[*p]) p++;
  3239.       id_len = p - id_beg;
  3240.  
  3241.       if (is_idstart[c]) {
  3242.     register struct arglist *arg;
  3243.  
  3244.     for (arg = arglist; arg != NULL; arg = arg->next) {
  3245.       struct reflist *tpat;
  3246.  
  3247.       if (arg->name[0] == c
  3248.           && arg->length == id_len
  3249.           && strncmp (arg->name, id_beg, id_len) == 0) {
  3250.         /* make a pat node for this arg and append it to the end of
  3251.            the pat list */
  3252.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  3253.         tpat->next = NULL;
  3254.         tpat->raw_before = concat == id_beg;
  3255.         tpat->raw_after = 0;
  3256.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  3257.                    : stringify == id_beg);
  3258.  
  3259.         if (endpat == NULL)
  3260.           defn->pattern = tpat;
  3261.         else
  3262.           endpat->next = tpat;
  3263.         endpat = tpat;
  3264.  
  3265.         tpat->argno = arg->argno;
  3266.         tpat->nchars = exp_p - lastp;
  3267.         {
  3268.           register U_CHAR *p1 = p;
  3269.           SKIP_WHITE_SPACE (p1);
  3270.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  3271.         tpat->raw_after = 1;
  3272.         }
  3273.         lastp = exp_p;    /* place to start copying from next time */
  3274.         skipped_arg = 1;
  3275.         break;
  3276.       }
  3277.     }
  3278.       }
  3279.  
  3280.       /* If this was not a macro arg, copy it into the expansion.  */
  3281.       if (! skipped_arg) {
  3282.     register U_CHAR *lim1 = p;
  3283.     p = id_beg;
  3284.     while (p != lim1)
  3285.       *exp_p++ = *p++;
  3286.     if (stringify == id_beg)
  3287.       error ("# operator should be followed by a macro argument name");
  3288.       }
  3289.     }
  3290.   }
  3291.  
  3292.   if (limit < end) {
  3293.     /* Convert trailing whitespace to Newline-markers.  */
  3294.     while (limit < end && is_space[*limit]) {
  3295.       *exp_p++ = '\n';
  3296.       *exp_p++ = *limit++;
  3297.     }
  3298.   } else if (!traditional) {
  3299.     /* There is no trailing whitespace, so invent some.  */
  3300.     *exp_p++ = '\n';
  3301.     *exp_p++ = ' ';
  3302.   }
  3303.  
  3304.   *exp_p = '\0';
  3305.  
  3306.   defn->length = exp_p - defn->expansion;
  3307.  
  3308.   /* Crash now if we overrun the allocated size.  */
  3309.   if (defn->length + 1 > maxsize)
  3310.     abort ();
  3311.  
  3312. #if 0
  3313. /* This isn't worth the time it takes.  */
  3314.   /* give back excess storage */
  3315.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  3316. #endif
  3317.  
  3318.   return defn;
  3319. }
  3320.  
  3321. /*
  3322.  * interpret #line command.  Remembers previously seen fnames
  3323.  * in its very own hash table.
  3324.  */
  3325. #define FNAME_HASHSIZE 37
  3326.  
  3327. do_line (buf, limit, op, keyword)
  3328.      U_CHAR *buf, *limit;
  3329.      FILE_BUF *op;
  3330.      struct directive *keyword;
  3331. {
  3332.   register U_CHAR *bp;
  3333.   FILE_BUF *ip = &instack[indepth];
  3334.   FILE_BUF tem;
  3335.   int new_lineno;
  3336.   enum file_change_code file_change = same_file;
  3337.  
  3338.   /* Expand any macros.  */
  3339.   tem = expand_to_temp_buffer (buf, limit, 0);
  3340.  
  3341.   /* Point to macroexpanded line, which is null-terminated now.  */
  3342.   bp = tem.buf;
  3343.   SKIP_WHITE_SPACE (bp);
  3344.  
  3345.   if (!isdigit (*bp)) {
  3346.     error ("invalid format #line command");
  3347.     return;
  3348.   }
  3349.  
  3350.   /* The Newline at the end of this line remains to be processed.
  3351.      To put the next line at the specified line number,
  3352.      we must store a line number now that is one less.  */
  3353.   new_lineno = atoi (bp) - 1;
  3354.  
  3355.   /* skip over the line number.  */
  3356.   while (isdigit (*bp))
  3357.     bp++;
  3358.  
  3359. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  3360.   if (*bp && !is_space[*bp]) {
  3361.     error ("invalid format #line command");
  3362.     return;
  3363.   }
  3364. #endif
  3365.  
  3366.   SKIP_WHITE_SPACE (bp);
  3367.  
  3368.   if (*bp == '\"') {
  3369.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  3370.     HASHNODE *hp, **hash_bucket;
  3371.     U_CHAR *fname;
  3372.     int fname_length;
  3373.  
  3374.     fname = ++bp;
  3375.  
  3376.     while (*bp && *bp != '\"')
  3377.       bp++;
  3378.     if (*bp != '\"') {
  3379.       error ("invalid format #line command");
  3380.       return;
  3381.     }
  3382.  
  3383.     fname_length = bp - fname;
  3384.  
  3385.     bp++;
  3386.     SKIP_WHITE_SPACE (bp);
  3387.     if (*bp) {
  3388.       if (*bp == '1')
  3389.     file_change = enter_file;
  3390.       else if (*bp == '2')
  3391.     file_change = leave_file;
  3392.       else {
  3393.     error ("invalid format #line command");
  3394.     return;
  3395.       }
  3396.  
  3397.       bp++;
  3398.       SKIP_WHITE_SPACE (bp);
  3399.       if (*bp) {
  3400.     error ("invalid format #line command");
  3401.     return;
  3402.       }
  3403.     }
  3404.  
  3405.     hash_bucket =
  3406.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  3407.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  3408.       if (hp->length == fname_length &&
  3409.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  3410.     ip->fname = hp->value.cpval;
  3411.     break;
  3412.       }
  3413.     if (hp == 0) {
  3414.       /* Didn't find it; cons up a new one.  */
  3415.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  3416.       hp->next = *hash_bucket;
  3417.       *hash_bucket = hp;
  3418.  
  3419.       hp->length = fname_length;
  3420.       ip->fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  3421.       bcopy (fname, hp->value.cpval, fname_length);
  3422.     }
  3423.   } else if (*bp) {
  3424.     error ("invalid format #line command");
  3425.     return;
  3426.   }
  3427.  
  3428.   ip->lineno = new_lineno;
  3429.   output_line_command (ip, op, 0, file_change);
  3430.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  3431. }
  3432.  
  3433. /*
  3434.  * remove all definitions of symbol from symbol table.
  3435.  * according to un*x /lib/cpp, it is not an error to undef
  3436.  * something that has no definitions, so it isn't one here either.
  3437.  */
  3438. do_undef (buf, limit, op, keyword)
  3439.      U_CHAR *buf, *limit;
  3440.      FILE_BUF *op;
  3441.      struct directive *keyword;
  3442. {
  3443.   HASHNODE *hp;
  3444.  
  3445.   SKIP_WHITE_SPACE (buf);
  3446.  
  3447.   if (! strncmp (buf, "defined", 7) && ! is_idchar[buf[7]])
  3448.     warning ("undefining `defined'");
  3449.  
  3450.   while ((hp = lookup (buf, -1, -1)) != NULL) {
  3451.     if (hp->type != T_MACRO)
  3452.       warning ("undefining `%s'", hp->name);
  3453.     delete_macro (hp);
  3454.   }
  3455. }
  3456.  
  3457. /*
  3458.  * Report a fatal error detected by the program we are processing.
  3459.  * Use the text of the line in the error message, then terminate.
  3460.  * (We use error() because it prints the filename & line#.)
  3461.  */
  3462. do_error (buf, limit, op, keyword)
  3463.      U_CHAR *buf, *limit;
  3464.      FILE_BUF *op;
  3465.      struct directive *keyword;
  3466. {
  3467.   int length = limit - buf;
  3468.   char *copy = (char *) xmalloc (length + 1);
  3469.   bcopy (buf, copy, length);
  3470.   copy[length] = 0;
  3471.   SKIP_WHITE_SPACE (copy);
  3472.   error ("#error %s", copy);
  3473.   exit (FATAL_EXIT_CODE);
  3474. }
  3475.  
  3476. /* Remember the name of the current file being read from so that we can
  3477.    avoid ever including it again.  */
  3478.  
  3479. do_once ()
  3480. {
  3481.   int i;
  3482.   FILE_BUF *ip = NULL;
  3483.  
  3484.   for (i = indepth; i >= 0; i--)
  3485.     if (instack[i].fname != NULL) {
  3486.       ip = &instack[i];
  3487.       break;
  3488.     }
  3489.  
  3490.   if (ip != NULL)
  3491.     {
  3492.       struct file_name_list *new;
  3493.  
  3494.       new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  3495.       new->next = dont_repeat_files;
  3496.       dont_repeat_files = new;
  3497.       new->fname = savestring (ip->fname);
  3498.     }
  3499. }
  3500.  
  3501. /* #pragma and its argument line have already been copied to the output file.
  3502.    Here just check for recognized pragmas.  */
  3503.  
  3504. do_pragma (buf, limit)
  3505.      U_CHAR *buf, *limit;
  3506. {
  3507.   while (*buf == ' ' || *buf == '\t')
  3508.     buf++;
  3509.   if (!strncmp (buf, "once", 4))
  3510.     do_once ();
  3511. }
  3512.  
  3513. #if 0
  3514. /* This was a fun hack, but #pragma seems to start to be useful.
  3515.    By failing to recognize it, we pass it through unchanged to cc1.  */
  3516.  
  3517. /*
  3518.  * the behavior of the #pragma directive is implementation defined.
  3519.  * this implementation defines it as follows.
  3520.  */
  3521. do_pragma ()
  3522. {
  3523.   close (0);
  3524.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  3525.     goto nope;
  3526.   close (1);
  3527.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  3528.     goto nope;
  3529.   execl ("/usr/games/hack", "#pragma", 0);
  3530.   execl ("/usr/games/rogue", "#pragma", 0);
  3531.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  3532.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  3533. nope:
  3534.   fatal ("You are in a maze of twisty compiler features, all different");
  3535. }
  3536. #endif
  3537.  
  3538. /* Just ignore #sccs, on systems where we define it at all.  */
  3539. do_sccs ()
  3540. {
  3541.   if (pedantic)
  3542.     error ("ANSI C does not allow #sccs");
  3543. }
  3544.  
  3545. /*
  3546.  * handle #if command by
  3547.  *   1) inserting special `defined' keyword into the hash table
  3548.  *    that gets turned into 0 or 1 by special_symbol (thus,
  3549.  *    if the luser has a symbol called `defined' already, it won't
  3550.  *      work inside the #if command)
  3551.  *   2) rescan the input into a temporary output buffer
  3552.  *   3) pass the output buffer to the yacc parser and collect a value
  3553.  *   4) clean up the mess left from steps 1 and 2.
  3554.  *   5) call conditional_skip to skip til the next #endif (etc.),
  3555.  *      or not, depending on the value from step 3.
  3556.  */
  3557.  
  3558. do_if (buf, limit, op, keyword)
  3559.      U_CHAR *buf, *limit;
  3560.      FILE_BUF *op;
  3561.      struct directive *keyword;
  3562. {
  3563.   int value;
  3564.   FILE_BUF *ip = &instack[indepth];
  3565.  
  3566.   value = eval_if_expression (buf, limit - buf);
  3567.   conditional_skip (ip, value == 0, T_IF);
  3568. }
  3569.  
  3570. /*
  3571.  * handle a #elif directive by not changing  if_stack  either.
  3572.  * see the comment above do_else.
  3573.  */
  3574.  
  3575. do_elif (buf, limit, op, keyword)
  3576.      U_CHAR *buf, *limit;
  3577.      FILE_BUF *op;
  3578.      struct directive *keyword;
  3579. {
  3580.   int value;
  3581.   FILE_BUF *ip = &instack[indepth];
  3582.  
  3583.   if (if_stack == instack[indepth].if_stack) {
  3584.     error ("#elif not within a conditional");
  3585.     return;
  3586.   } else {
  3587.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3588.       error ("#elif after #else");
  3589.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3590.       if (if_stack->fname != NULL && ip->fname != NULL &&
  3591.       strcmp (if_stack->fname, ip->fname) != 0)
  3592.     fprintf (stderr, ", file %s", if_stack->fname);
  3593.       fprintf (stderr, ")\n");
  3594.     }
  3595.     if_stack->type = T_ELIF;
  3596.   }
  3597.  
  3598.   if (if_stack->if_succeeded)
  3599.     skip_if_group (ip, 0);
  3600.   else {
  3601.     value = eval_if_expression (buf, limit - buf);
  3602.     if (value == 0)
  3603.       skip_if_group (ip, 0);
  3604.     else {
  3605.       ++if_stack->if_succeeded;    /* continue processing input */
  3606.       output_line_command (ip, op, 1, same_file);
  3607.     }
  3608.   }
  3609. }
  3610.  
  3611. /*
  3612.  * evaluate a #if expression in BUF, of length LENGTH,
  3613.  * then parse the result as a C expression and return the value as an int.
  3614.  */
  3615. int
  3616. eval_if_expression (buf, length)
  3617.      U_CHAR *buf;
  3618.      int length;
  3619. {
  3620.   FILE_BUF temp_obuf;
  3621.   HASHNODE *save_defined;
  3622.   int value;
  3623.  
  3624.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, -1);
  3625.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
  3626.   delete_macro (save_defined);    /* clean up special symbol */
  3627.  
  3628.   value = parse_c_expression (temp_obuf.buf);
  3629.  
  3630.   free (temp_obuf.buf);
  3631.  
  3632.   return value;
  3633. }
  3634.  
  3635. /*
  3636.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  3637.  * then do or don't skip to the #endif/#else/#elif depending
  3638.  * on what directive is actually being processed.
  3639.  */
  3640. do_xifdef (buf, limit, op, keyword)
  3641.      U_CHAR *buf, *limit;
  3642.      FILE_BUF *op;
  3643.      struct directive *keyword;
  3644. {
  3645.   int skip;
  3646.   FILE_BUF *ip = &instack[indepth];
  3647.   U_CHAR *end; 
  3648.  
  3649.   /* Discard leading and trailing whitespace.  */
  3650.   SKIP_WHITE_SPACE (buf);
  3651.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  3652.  
  3653.   /* Find the end of the identifier at the beginning.  */
  3654.   for (end = buf; is_idchar[*end]; end++);
  3655.  
  3656.   if (end == buf) {
  3657.     skip = (keyword->type == T_IFDEF);
  3658.     if (! traditional)
  3659.       warning (end == limit ? "#%s with no argument"
  3660.            : "#%s argument starts with punctuation",
  3661.            keyword->name);
  3662.   } else {
  3663.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  3664.       warning ("#%s argument starts with a digit", keyword->name);
  3665.     else if (end != limit && !traditional)
  3666.       warning ("garbage at end of #%s argument", keyword->name);
  3667.  
  3668.     skip = (lookup (buf, end-buf, -1) == NULL) ^ (keyword->type == T_IFNDEF);
  3669.   }
  3670.  
  3671.   conditional_skip (ip, skip, T_IF);
  3672. }
  3673.  
  3674. /*
  3675.  * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  3676.  */
  3677. void
  3678. conditional_skip (ip, skip, type)
  3679.      FILE_BUF *ip;
  3680.      int skip;
  3681.      enum node_type type;
  3682. {
  3683.   IF_STACK_FRAME *temp;
  3684.  
  3685.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3686.   temp->fname = ip->fname;
  3687.   temp->lineno = ip->lineno;
  3688.   temp->next = if_stack;
  3689.   if_stack = temp;
  3690.  
  3691.   if_stack->type = type;
  3692.  
  3693.   if (skip != 0) {
  3694.     skip_if_group (ip, 0);
  3695.     return;
  3696.   } else {
  3697.     ++if_stack->if_succeeded;
  3698.     output_line_command (ip, &outbuf, 1, same_file);
  3699.   }
  3700. }
  3701.  
  3702. /*
  3703.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  3704.  * leaves input ptr at the sharp sign found.
  3705.  * If ANY is nonzero, return at next directive of any sort.
  3706.  */
  3707. void
  3708. skip_if_group (ip, any)
  3709.      FILE_BUF *ip;
  3710.      int any;
  3711. {
  3712.   register U_CHAR *bp = ip->bufp, *cp;
  3713.   register U_CHAR *endb = ip->buf + ip->length;
  3714.   struct directive *kt;
  3715.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  3716.   U_CHAR *beg_of_line = bp;
  3717.  
  3718.   while (bp < endb) {
  3719.     switch (*bp++) {
  3720.     case '/':            /* possible comment */
  3721.       if (*bp == '\\' && bp[1] == '\n')
  3722.     newline_fix (bp);
  3723.       if (*bp == '*'
  3724.       || (cplusplus && *bp == '/')) {
  3725.     ip->bufp = ++bp;
  3726.     bp = skip_to_end_of_comment (ip, &ip->lineno);
  3727.       }
  3728.       break;
  3729.     case '\"':
  3730.     case '\'':
  3731.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
  3732.       break;
  3733.     case '\\':
  3734.       /* Char after backslash loses its special meaning.  */
  3735.       if (bp < endb) {
  3736.     if (*bp == '\n')
  3737.       ++ip->lineno;        /* But do update the line-count.  */
  3738.     bp++;
  3739.       }
  3740.       break;
  3741.     case '\n':
  3742.       ++ip->lineno;
  3743.       beg_of_line = bp;
  3744.       break;
  3745.     case '#':
  3746.       ip->bufp = bp - 1;
  3747.  
  3748.       /* # keyword: a # must be first nonblank char on the line */
  3749.       if (beg_of_line == 0)
  3750.     break;
  3751.       /* Scan from start of line, skipping whitespace, comments
  3752.      and backslash-newlines, and see if we reach this #.
  3753.      If not, this # is not special.  */
  3754.       bp = beg_of_line;
  3755.       while (1) {
  3756.     if (is_hor_space[*bp])
  3757.       bp++;
  3758.     else if (*bp == '\\' && bp[1] == '\n')
  3759.       bp += 2;
  3760.     else if (*bp == '/' && bp[1] == '*') {
  3761.       bp += 2;
  3762.       while (!(*bp == '*' && bp[1] == '/'))
  3763.         bp++;
  3764.       bp += 2;
  3765.     }
  3766.     else if (cplusplus && *bp == '/' && bp[1] == '/') {
  3767.       bp += 2;
  3768.       while (*bp++ != '\n') ;
  3769.         }
  3770.     else break;
  3771.       }
  3772.       if (bp != ip->bufp) {
  3773.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  3774.     break;
  3775.       }
  3776.  
  3777.       bp = ip->bufp + 1;        /* point at '#' */
  3778.  
  3779.       /* Skip whitespace and \-newline.  */
  3780.       while (1) {
  3781.     if (is_hor_space[*bp])
  3782.       bp++;
  3783.     else if (*bp == '\\' && bp[1] == '\n')
  3784.       bp += 2;
  3785.     else break;
  3786.       }
  3787.  
  3788.       cp = bp;
  3789.  
  3790.       /* Now find end of directive name.
  3791.      If we encounter a backslash-newline, exchange it with any following
  3792.      symbol-constituents so that we end up with a contiguous name.  */
  3793.  
  3794.       while (1) {
  3795.     if (is_idchar[*bp])
  3796.       bp++;
  3797.     else {
  3798.       if (*bp == '\\' && bp[1] == '\n')
  3799.         name_newline_fix (bp);
  3800.       if (is_idchar[*bp])
  3801.         bp++;
  3802.       else break;
  3803.     }
  3804.       }
  3805.  
  3806.       for (kt = directive_table; kt->length >= 0; kt++) {
  3807.     IF_STACK_FRAME *temp;
  3808.     if (strncmp (cp, kt->name, kt->length) == 0
  3809.         && !is_idchar[cp[kt->length]]) {
  3810.  
  3811.       /* If we are asked to return on next directive,
  3812.          do so now.  */
  3813.       if (any)
  3814.         return;
  3815.  
  3816.       switch (kt->type) {
  3817.       case T_IF:
  3818.       case T_IFDEF:
  3819.       case T_IFNDEF:
  3820.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  3821.         temp->next = if_stack;
  3822.         if_stack = temp;
  3823.         temp->lineno = ip->lineno;
  3824.         temp->fname = ip->fname;
  3825.         temp->type = kt->type;
  3826.         break;
  3827.       case T_ELSE:
  3828.       case T_ENDIF:
  3829.         if (pedantic && if_stack != save_if_stack)
  3830.           validate_else (bp);
  3831.       case T_ELIF:
  3832.         if (if_stack == instack[indepth].if_stack) {
  3833.           error ("#%s not within a conditional", kt->name);
  3834.           break;
  3835.         }
  3836.         else if (if_stack == save_if_stack)
  3837.           return;        /* found what we came for */
  3838.  
  3839.         if (kt->type != T_ENDIF) {
  3840.           if (if_stack->type == T_ELSE)
  3841.         error ("#else or #elif after #else");
  3842.           if_stack->type = kt->type;
  3843.           break;
  3844.         }
  3845.  
  3846.         temp = if_stack;
  3847.         if_stack = if_stack->next;
  3848.         free (temp);
  3849.         break;
  3850.       }
  3851.       break;
  3852.     }
  3853.       }
  3854.     }
  3855.   }
  3856.   ip->bufp = bp;
  3857.   /* after this returns, rescan will exit because ip->bufp
  3858.      now points to the end of the buffer.
  3859.      rescan is responsible for the error message also.  */
  3860. }
  3861.  
  3862. /*
  3863.  * handle a #else directive.  Do this by just continuing processing
  3864.  * without changing  if_stack ;  this is so that the error message
  3865.  * for missing #endif's etc. will point to the original #if.  It
  3866.  * is possible that something different would be better.
  3867.  */
  3868. do_else (buf, limit, op, keyword)
  3869.      U_CHAR *buf, *limit;
  3870.      FILE_BUF *op;
  3871.      struct directive *keyword;
  3872. {
  3873.   FILE_BUF *ip = &instack[indepth];
  3874.  
  3875.   if (pedantic) {
  3876.     SKIP_WHITE_SPACE (buf);
  3877.     if (buf != limit)
  3878.       warning ("text following #else violates ANSI standard");
  3879.   }
  3880.  
  3881.   if (if_stack == instack[indepth].if_stack) {
  3882.     error ("#else not within a conditional");
  3883.     return;
  3884.   } else {
  3885.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  3886.       error ("#else after #else");
  3887.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  3888.       if (strcmp (if_stack->fname, ip->fname) != 0)
  3889.     fprintf (stderr, ", file %s", if_stack->fname);
  3890.       fprintf (stderr, ")\n");
  3891.     }
  3892.     if_stack->type = T_ELSE;
  3893.   }
  3894.  
  3895.   if (if_stack->if_succeeded)
  3896.     skip_if_group (ip, 0);
  3897.   else {
  3898.     ++if_stack->if_succeeded;    /* continue processing input */
  3899.     output_line_command (ip, op, 1, same_file);
  3900.   }
  3901. }
  3902.  
  3903. /*
  3904.  * unstack after #endif command
  3905.  */
  3906. do_endif (buf, limit, op, keyword)
  3907.      U_CHAR *buf, *limit;
  3908.      FILE_BUF *op;
  3909.      struct directive *keyword;
  3910. {
  3911.   if (pedantic) {
  3912.     SKIP_WHITE_SPACE (buf);
  3913.     if (buf != limit)
  3914.       warning ("text following #endif violates ANSI standard");
  3915.   }
  3916.  
  3917.   if (if_stack == instack[indepth].if_stack)
  3918.     error ("unbalanced #endif");
  3919.   else {
  3920.     IF_STACK_FRAME *temp = if_stack;
  3921.     if_stack = if_stack->next;
  3922.     free (temp);
  3923.     output_line_command (&instack[indepth], op, 1, same_file);
  3924.   }
  3925. }
  3926.  
  3927. /* When an #else or #endif is found while skipping failed conditional,
  3928.    if -pedantic was specified, this is called to warn about text after
  3929.    the command name.  P points to the first char after the command name.  */
  3930.  
  3931. validate_else (p)
  3932.      register U_CHAR *p;
  3933. {
  3934.   /* Advance P over whitespace and comments.  */
  3935.   while (1) {
  3936.     if (*p == '\\' && p[1] == '\n')
  3937.       p += 2;
  3938.     if (is_hor_space[*p])
  3939.       p++;
  3940.     else if (*p == '/') {
  3941.       if (p[1] == '\\' && p[2] == '\n')
  3942.     newline_fix (p + 1);
  3943.       if (p[1] == '*') {
  3944.     p += 2;
  3945.     /* Don't bother warning about unterminated comments
  3946.        since that will happen later.  Just be sure to exit.  */
  3947.     while (*p) {
  3948.       if (p[1] == '\\' && p[2] == '\n')
  3949.         newline_fix (p + 1);
  3950.       if (*p == '*' && p[1] == '/') {
  3951.         p += 2;
  3952.         break;
  3953.       }
  3954.       p++;
  3955.     }
  3956.       }
  3957.       else if (cplusplus && p[1] == '/') {
  3958.     p += 2;
  3959.     while (*p && *p++ != '\n') ;
  3960.       }
  3961.     } else break;
  3962.   }
  3963.   if (*p && *p != '\n')
  3964.     warning ("text following #else or #endif violates ANSI standard");
  3965. }
  3966.  
  3967. /*
  3968.  * Skip a comment, assuming the input ptr immediately follows the
  3969.  * initial slash-star.  Bump line counter as necessary.
  3970.  * (The canonical line counter is &ip->lineno).
  3971.  * Don't use this routine (or the next one) if bumping the line
  3972.  * counter is not sufficient to deal with newlines in the string.
  3973.  */
  3974. U_CHAR *
  3975. skip_to_end_of_comment (ip, line_counter)
  3976.      register FILE_BUF *ip;
  3977.      int *line_counter;        /* place to remember newlines, or NULL */
  3978. {
  3979.   register U_CHAR *limit = ip->buf + ip->length;
  3980.   register U_CHAR *bp = ip->bufp;
  3981.   FILE_BUF *op = &outbuf;    /* JF */
  3982.   int output = put_out_comments && !line_counter;
  3983.  
  3984.     /* JF this line_counter stuff is a crock to make sure the
  3985.        comment is only put out once, no matter how many times
  3986.        the comment is skipped.  It almost works */
  3987.   if (output) {
  3988.     *op->bufp++ = '/';
  3989.     *op->bufp++ = '*';
  3990.   }
  3991.   if (cplusplus && bp[-1] == '/') {
  3992.     if (output) {
  3993.       while (bp < limit)
  3994.     if ((*op->bufp++ = *bp++) == '\n') {
  3995.       bp--;
  3996.       break;
  3997.     }
  3998.       op->bufp[-1] = '*';
  3999.       *op->bufp++ = '/';
  4000.       *op->bufp++ = '\n';
  4001.     } else {
  4002.       while (bp < limit) {
  4003.     if (*bp++ == '\n') {
  4004.       bp--;
  4005.       break;
  4006.     }
  4007.       }
  4008.     }
  4009.     ip->bufp = bp;
  4010.     return bp;
  4011.   }
  4012.   while (bp < limit) {
  4013.     if (output)
  4014.       *op->bufp++ = *bp;
  4015.     switch (*bp++) {
  4016.     case '\n':
  4017.       if (line_counter != NULL)
  4018.     ++*line_counter;
  4019.       if (output)
  4020.     ++op->lineno;
  4021.       break;
  4022.     case '*':
  4023.       if (*bp == '\\' && bp[1] == '\n')
  4024.     newline_fix (bp);
  4025.       if (*bp == '/') {
  4026.         if (output)
  4027.       *op->bufp++ = '/';
  4028.     ip->bufp = ++bp;
  4029.     return bp;
  4030.       }
  4031.       break;
  4032.     }
  4033.   }
  4034.   ip->bufp = bp;
  4035.   return bp;
  4036. }
  4037.  
  4038. /*
  4039.  * Skip over a quoted string.  BP points to the opening quote.
  4040.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  4041.  * START_LINE is the line number of the starting point (but it need
  4042.  * not be valid if the starting point is inside a macro expansion).
  4043.  *
  4044.  * The input stack state is not changed.
  4045.  *
  4046.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  4047.  * for each newline passed.
  4048.  *
  4049.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  4050.  * if we pass a backslash-newline.
  4051.  *
  4052.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  4053.  */
  4054. U_CHAR *
  4055. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  4056.      register U_CHAR *bp;
  4057.      register U_CHAR *limit;
  4058.      int start_line;
  4059.      int *count_newlines;
  4060.      int *backslash_newlines_p;
  4061.      int *eofp;
  4062. {
  4063.   register U_CHAR c, match;
  4064.  
  4065.   match = *bp++;
  4066.   while (1) {
  4067.     if (bp >= limit) {
  4068.       error_with_line (line_for_error (start_line),
  4069.                "unterminated string or character constant");
  4070.       if (eofp)
  4071.     *eofp = 1;
  4072.       break;
  4073.     }
  4074.     c = *bp++;
  4075.     if (c == '\\') {
  4076.       while (*bp == '\\' && bp[1] == '\n') {
  4077.     if (backslash_newlines_p)
  4078.       *backslash_newlines_p = 1;
  4079.     if (count_newlines)
  4080.       ++*count_newlines;
  4081.     bp += 2;
  4082.       }
  4083.       if (*bp == '\n' && count_newlines) {
  4084.     if (backslash_newlines_p)
  4085.       *backslash_newlines_p = 1;
  4086.     ++*count_newlines;
  4087.       }
  4088.       bp++;
  4089.     } else if (c == '\n') {
  4090.       if (traditional) {
  4091.      /* Unterminated strings and character constants are 'legal'.  */
  4092.      bp--;    /* Don't consume the newline. */
  4093.      if (eofp)
  4094.        *eofp = 1;
  4095.      break;
  4096.       }
  4097.       if (match == '\'') {
  4098.     error_with_line (line_for_error (start_line),
  4099.              "unterminated character constant");
  4100.     bp--;
  4101.     if (eofp)
  4102.       *eofp = 1;
  4103.     break;
  4104.       }
  4105.       if (traditional) {    /* Unterminated strings are 'legal'.  */
  4106.     if (eofp)
  4107.       *eofp = 1;
  4108.     break;
  4109.       }
  4110.       /* If not traditional, then allow newlines inside strings.  */
  4111.       if (count_newlines)
  4112.     ++*count_newlines;
  4113.     } else if (c == match)
  4114.       break;
  4115.   }
  4116.   return bp;
  4117. }
  4118.  
  4119. /*
  4120.  * write out a #line command, for instance, after an #include file.
  4121.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  4122.  * appear to be a no-op, and we can output a few newlines instead
  4123.  * if we want to increase the line number by a small amount.
  4124.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  4125.  */
  4126.  
  4127. void
  4128. output_line_command (ip, op, conditional, file_change)
  4129.      FILE_BUF *ip, *op;
  4130.      int conditional;
  4131.      enum file_change_code file_change;
  4132. {
  4133.   int len;
  4134.   char line_cmd_buf[500];
  4135.  
  4136.   if (no_line_commands
  4137.       || ip->fname == NULL
  4138.       || no_output) {
  4139.     op->lineno = ip->lineno;
  4140.     return;
  4141.   }
  4142.  
  4143.   if (conditional) {
  4144.     if (ip->lineno == op->lineno)
  4145.       return;
  4146.  
  4147.     /* If the inherited line number is a little too small,
  4148.        output some newlines instead of a #line command.  */
  4149.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  4150.       check_expand (op, 10);
  4151.       while (ip->lineno > op->lineno) {
  4152.     *op->bufp++ = '\n';
  4153.     op->lineno++;
  4154.       }
  4155.       return;
  4156.     }
  4157.   }
  4158.  
  4159. #ifdef OUTPUT_LINE_COMMANDS
  4160.   sprintf (line_cmd_buf, "#line %d \"%s\"", ip->lineno, ip->fname);
  4161. #else
  4162.   sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
  4163. #endif
  4164.   if (file_change != same_file)
  4165.     strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
  4166.   len = strlen (line_cmd_buf);
  4167.   line_cmd_buf[len++] = '\n';
  4168.   check_expand (op, len + 1);
  4169.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  4170.     *op->bufp++ = '\n';
  4171.   bcopy (line_cmd_buf, op->bufp, len);
  4172.   op->bufp += len;
  4173.   op->lineno = ip->lineno;
  4174. }
  4175.  
  4176. /* This structure represents one parsed argument in a macro call.
  4177.    `raw' points to the argument text as written (`raw_length' is its length).
  4178.    `expanded' points to the argument's macro-expansion
  4179.    (its length is `expand_length').
  4180.    `stringified_length' is the length the argument would have
  4181.    if stringified.
  4182.    `free1' and `free2', if nonzero, point to blocks to be freed
  4183.    when the macro argument data is no longer needed.  */
  4184.  
  4185. struct argdata {
  4186.   U_CHAR *raw, *expanded;
  4187.   int raw_length, expand_length;
  4188.   int stringified_length;
  4189.   U_CHAR *free1, *free2;
  4190.   char newlines;
  4191.   char comments;
  4192. };
  4193.  
  4194. /* Expand a macro call.
  4195.    HP points to the symbol that is the macro being called.
  4196.    Put the result of expansion onto the input stack
  4197.    so that subsequent input by our caller will use it.
  4198.  
  4199.    If macro wants arguments, caller has already verified that
  4200.    an argument list follows; arguments come from the input stack.  */
  4201.  
  4202. void
  4203. macroexpand (hp, op)
  4204.      HASHNODE *hp;
  4205.      FILE_BUF *op;
  4206. {
  4207.   int nargs;
  4208.   DEFINITION *defn = hp->value.defn;
  4209.   register U_CHAR *xbuf;
  4210.   int xbuf_len;
  4211.   int start_line = instack[indepth].lineno;
  4212.  
  4213.   CHECK_DEPTH (return;);
  4214.  
  4215.   /* it might not actually be a macro.  */
  4216.   if (hp->type != T_MACRO) {
  4217.     special_symbol (hp, op);
  4218.     return;
  4219.   }
  4220.  
  4221.   nargs = defn->nargs;
  4222.  
  4223.   if (nargs >= 0) {
  4224.     register int i;
  4225.     struct argdata *args;
  4226.     char *parse_error = 0;
  4227.  
  4228.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  4229.  
  4230.     for (i = 0; i < nargs; i++) {
  4231.       args[i].raw = args[i].expanded = (U_CHAR *) "";
  4232.       args[i].raw_length = args[i].expand_length
  4233.     = args[i].stringified_length = 0;
  4234.       args[i].free1 = args[i].free2 = 0;
  4235.     }
  4236.  
  4237.     /* Parse all the macro args that are supplied.  I counts them.
  4238.        The first NARGS args are stored in ARGS.
  4239.        The rest are discarded.  */
  4240.     i = 0;
  4241.     do {
  4242.       /* Discard the open-parenthesis or comma before the next arg.  */
  4243.       ++instack[indepth].bufp;
  4244.       parse_error
  4245.     = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
  4246.       if (parse_error)
  4247.     {
  4248.       error_with_line (line_for_error (start_line), parse_error);
  4249.       break;
  4250.     }
  4251.       i++;
  4252.     } while (*instack[indepth].bufp != ')');
  4253.  
  4254.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  4255.     if (i == 1) {
  4256.       register U_CHAR *bp = args[0].raw;
  4257.       register U_CHAR *lim = bp + args[0].raw_length;
  4258.       while (bp != lim && is_space[*bp]) bp++;
  4259.       if (bp == lim)
  4260.     i = 0;
  4261.     }
  4262.  
  4263.     if (nargs == 0 && i > 0)
  4264.       error ("arguments given to macro `%s'", hp->name);
  4265.     else if (i < nargs) {
  4266.       /* traditional C allows foo() if foo wants one argument.  */
  4267.       if (nargs == 1 && i == 0 && traditional)
  4268.     ;
  4269.       else if (i == 0)
  4270.     error ("no args to macro `%s'", hp->name);
  4271.       else if (i == 1)
  4272.     error ("only 1 arg to macro `%s'", hp->name);
  4273.       else
  4274.     error ("only %d args to macro `%s'", i, hp->name);
  4275.     } else if (i > nargs)
  4276.       error ("too many (%d) args to macro `%s'", i, hp->name);
  4277.  
  4278.     /* Swallow the closeparen.  */
  4279.     ++instack[indepth].bufp;
  4280.  
  4281.     /* If macro wants zero args, we parsed the arglist for checking only.
  4282.        Read directly from the macro definition.  */
  4283.     if (nargs == 0) {
  4284.       xbuf = defn->expansion;
  4285.       xbuf_len = defn->length;
  4286.     } else {
  4287.       register U_CHAR *exp = defn->expansion;
  4288.       register int offset;    /* offset in expansion,
  4289.                    copied a piece at a time */
  4290.       register int totlen;    /* total amount of exp buffer filled so far */
  4291.  
  4292.       register struct reflist *ap;
  4293.  
  4294.       /* Macro really takes args.  Compute the expansion of this call.  */
  4295.  
  4296.       /* Compute length in characters of the macro's expansion.  */
  4297.       xbuf_len = defn->length;
  4298.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4299.     if (ap->stringify)
  4300.       xbuf_len += args[ap->argno].stringified_length;
  4301.     else if (ap->raw_before || ap->raw_after || traditional)
  4302.       xbuf_len += args[ap->argno].raw_length;
  4303.     else
  4304.       xbuf_len += args[ap->argno].expand_length;
  4305.       }
  4306.  
  4307.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  4308.  
  4309.       /* Generate in XBUF the complete expansion
  4310.      with arguments substituted in.
  4311.      TOTLEN is the total size generated so far.
  4312.      OFFSET is the index in the definition
  4313.      of where we are copying from.  */
  4314.       offset = totlen = 0;
  4315.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  4316.     register struct argdata *arg = &args[ap->argno];
  4317.  
  4318.     for (i = 0; i < ap->nchars; i++)
  4319.       xbuf[totlen++] = exp[offset++];
  4320.  
  4321.     if (ap->stringify != 0) {
  4322.       int arglen = arg->raw_length;
  4323.       int escaped = 0;
  4324.       int in_string = 0;
  4325.       int c;
  4326.       i = 0;
  4327.       while (i < arglen
  4328.          && (c = arg->raw[i], is_space[c]))
  4329.         i++;
  4330.       while (i < arglen
  4331.          && (c = arg->raw[arglen - 1], is_space[c]))
  4332.         arglen--;
  4333.       if (!traditional)
  4334.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  4335.       for (; i < arglen; i++) {
  4336.         c = arg->raw[i];
  4337.  
  4338.         /* Special markers Newline Space
  4339.            generate nothing for a stringified argument.  */
  4340.         if (c == '\n' && arg->raw[i+1] != '\n') {
  4341.           i++;
  4342.           continue;
  4343.         }
  4344.  
  4345.         /* Internal sequences of whitespace are replaced by one space.  */
  4346.         if (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c]) {
  4347.           while (1) {
  4348.         /* Note that Newline Space does occur within whitespace
  4349.            sequences; consider it part of the sequence.  */
  4350.         if (c == '\n' && is_space[arg->raw[i+1]])
  4351.           i += 2;
  4352.         else if (c != '\n' && is_space[c])
  4353.           i++;
  4354.         else break;
  4355.         c = arg->raw[i];
  4356.           }
  4357.           i--;
  4358.           c = ' ';
  4359.         }
  4360.  
  4361.         if (escaped)
  4362.           escaped = 0;
  4363.         else {
  4364.           if (c == '\\')
  4365.         escaped = 1;
  4366.           if (in_string) {
  4367.         if (c == in_string)
  4368.           in_string = 0;
  4369.           } else if (c == '\"' || c == '\'')
  4370.         in_string = c;
  4371.         }
  4372.  
  4373.         /* Escape these chars */
  4374.         if (c == '\"' || (in_string && c == '\\'))
  4375.           xbuf[totlen++] = '\\';
  4376.         if (isprint (c))
  4377.           xbuf[totlen++] = c;
  4378.         else {
  4379.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  4380.           totlen += 4;
  4381.         }
  4382.       }
  4383.       if (!traditional)
  4384.         xbuf[totlen++] = '\"'; /* insert ending quote */
  4385.     } else if (ap->raw_before || ap->raw_after || traditional) {
  4386.       U_CHAR *p1 = arg->raw;
  4387.       U_CHAR *l1 = p1 + arg->raw_length;
  4388.  
  4389.       if (ap->raw_before) {
  4390.         while (p1 != l1 && is_space[*p1]) p1++;
  4391.         while (p1 != l1 && is_idchar[*p1])
  4392.           xbuf[totlen++] = *p1++;
  4393.         /* Delete any no-reexpansion marker that follows
  4394.            an identifier at the beginning of the argument
  4395.            if the argument is concatenated with what precedes it.  */
  4396.         if (p1[0] == '\n' && p1[1] == '-')
  4397.           p1 += 2;
  4398.       }
  4399.       if (ap->raw_after) {
  4400.         /* Arg is concatenated after: delete trailing whitespace,
  4401.            whitespace markers, and no-reexpansion markers.  */
  4402.         while (p1 != l1) {
  4403.           if (is_space[l1[-1]]) l1--;
  4404.           else if (l1[-1] == '-') {
  4405.         U_CHAR *p2 = l1 - 1;
  4406.         /* If a `-' is preceded by an odd number of newlines then it
  4407.            and the last newline are a no-reexpansion marker.  */
  4408.         while (p2 != p1 && p2[-1] == '\n') p2--;
  4409.         if ((l1 - 1 - p2) & 1) {
  4410.           l1 -= 2;
  4411.         }
  4412.         else break;
  4413.           }
  4414.           else break;
  4415.         }
  4416.       }
  4417.       bcopy (p1, xbuf + totlen, l1 - p1);
  4418.       totlen += l1 - p1;
  4419.     } else {
  4420.       bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
  4421.       totlen += arg->expand_length;
  4422.     }
  4423.  
  4424.     if (totlen > xbuf_len)
  4425.       abort ();
  4426.       }
  4427.  
  4428.       /* if there is anything left of the definition
  4429.      after handling the arg list, copy that in too. */
  4430.  
  4431.       for (i = offset; i < defn->length; i++)
  4432.     xbuf[totlen++] = exp[i];
  4433.  
  4434.       xbuf[totlen] = 0;
  4435.       xbuf_len = totlen;
  4436.  
  4437.       for (i = 0; i < nargs; i++) {
  4438.     if (args[i].free1 != 0)
  4439.       free (args[i].free1);
  4440.     if (args[i].free2 != 0)
  4441.       free (args[i].free2);
  4442.       }
  4443.     }
  4444.   } else {
  4445.     xbuf = defn->expansion;
  4446.     xbuf_len = defn->length;
  4447.   }
  4448.  
  4449.   /* Now put the expansion on the input stack
  4450.      so our caller will commence reading from it.  */
  4451.   {
  4452.     register FILE_BUF *ip2;
  4453.  
  4454.     ip2 = &instack[++indepth];
  4455.  
  4456.     ip2->fname = 0;
  4457.     ip2->lineno = 0;
  4458.     ip2->buf = xbuf;
  4459.     ip2->length = xbuf_len;
  4460.     ip2->bufp = xbuf;
  4461.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  4462.     ip2->macro = hp;
  4463.     ip2->if_stack = if_stack;
  4464.  
  4465.     /* Recursive macro use sometimes works traditionally.
  4466.        #define foo(x,y) bar(x(y,0), y)
  4467.        foo(foo, baz)  */
  4468.  
  4469.     if (!traditional)
  4470.       hp->type = T_DISABLED;
  4471.   }
  4472. }
  4473.  
  4474. /*
  4475.  * Parse a macro argument and store the info on it into *ARGPTR.
  4476.  * Return nonzero to indicate a syntax error.
  4477.  */
  4478.  
  4479. char *
  4480. macarg (argptr)
  4481.      register struct argdata *argptr;
  4482. {
  4483.   FILE_BUF *ip = &instack[indepth];
  4484.   int paren = 0;
  4485.   int newlines = 0;
  4486.   int comments = 0;
  4487.  
  4488.   /* Try to parse as much of the argument as exists at this
  4489.      input stack level.  */
  4490.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  4491.             &paren, &newlines, &comments);
  4492.  
  4493.   /* If we find the end of the argument at this level,
  4494.      set up *ARGPTR to point at it in the input stack.  */
  4495.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  4496.       && bp != ip->buf + ip->length) {
  4497.     if (argptr != 0) {
  4498.       argptr->raw = ip->bufp;
  4499.       argptr->raw_length = bp - ip->bufp;
  4500.     }
  4501.     ip->bufp = bp;
  4502.   } else {
  4503.     /* This input stack level ends before the macro argument does.
  4504.        We must pop levels and keep parsing.
  4505.        Therefore, we must allocate a temporary buffer and copy
  4506.        the macro argument into it.  */
  4507.     int bufsize = bp - ip->bufp;
  4508.     int extra = newlines;
  4509.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  4510.     int final_start = 0;
  4511.  
  4512.     bcopy (ip->bufp, buffer, bufsize);
  4513.     ip->bufp = bp;
  4514.     ip->lineno += newlines;
  4515.  
  4516.     while (bp == ip->buf + ip->length) {
  4517.       if (instack[indepth].macro == 0) {
  4518.     free (buffer);
  4519.     return "unterminated macro call";
  4520.       }
  4521.       ip->macro->type = T_MACRO;
  4522.       if (ip->free_ptr)
  4523.     free (ip->free_ptr);
  4524.       ip = &instack[--indepth];
  4525.       newlines = 0;
  4526.       comments = 0;
  4527.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  4528.             &newlines, &comments);
  4529.       final_start = bufsize;
  4530.       bufsize += bp - ip->bufp;
  4531.       extra += newlines;
  4532.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  4533.       bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
  4534.       ip->bufp = bp;
  4535.       ip->lineno += newlines;
  4536.     }
  4537.  
  4538.     /* Now, if arg is actually wanted, record its raw form,
  4539.        discarding comments and duplicating newlines in whatever
  4540.        part of it did not come from a macro expansion.
  4541.        EXTRA space has been preallocated for duplicating the newlines.
  4542.        FINAL_START is the index of the start of that part.  */
  4543.     if (argptr != 0) {
  4544.       argptr->raw = buffer;
  4545.       argptr->raw_length = bufsize;
  4546.       argptr->free1 = buffer;
  4547.       argptr->newlines = newlines;
  4548.       argptr->comments = comments;
  4549.       if ((newlines || comments) && ip->fname != 0)
  4550.     argptr->raw_length
  4551.       = final_start +
  4552.         discard_comments (argptr->raw + final_start,
  4553.                   argptr->raw_length - final_start,
  4554.                   newlines);
  4555.       argptr->raw[argptr->raw_length] = 0;
  4556.       if (argptr->raw_length > bufsize + extra)
  4557.     abort ();
  4558.     }
  4559.   }
  4560.  
  4561.   /* If we are not discarding this argument,
  4562.      macroexpand it and compute its length as stringified.
  4563.      All this info goes into *ARGPTR.  */
  4564.  
  4565.   if (argptr != 0) {
  4566.     FILE_BUF obuf;
  4567.     register U_CHAR *buf, *lim;
  4568.     register int totlen;
  4569.  
  4570.     obuf = expand_to_temp_buffer (argptr->raw,
  4571.                   argptr->raw + argptr->raw_length,
  4572.                   1);
  4573.  
  4574.     argptr->expanded = obuf.buf;
  4575.     argptr->expand_length = obuf.length;
  4576.     argptr->free2 = obuf.buf;
  4577.  
  4578.     buf = argptr->raw;
  4579.     lim = buf + argptr->raw_length;
  4580.  
  4581.     /* If ANSI, discard leading and trailing space.  */
  4582.     if (!traditional) {
  4583.       while (buf != lim && is_space[*buf])
  4584.     buf++;
  4585.       while (buf != lim && is_space[lim[-1]])
  4586.     lim--;
  4587.     }
  4588.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  4589.     while (buf != lim) {
  4590.       register U_CHAR c = *buf++;
  4591.       totlen++;
  4592.       /* If ANSI, replace internal sequences of whitespace with one space.  */
  4593.       if (is_space[c] && !traditional)
  4594.     SKIP_ALL_WHITE_SPACE (buf);
  4595.       else if (c == '\"' || c == '\\') /* escape these chars */
  4596.     totlen++;
  4597.       else if (!isprint (c))
  4598.     totlen += 3;
  4599.     }
  4600.     argptr->stringified_length = totlen;
  4601.   }
  4602.   return 0;
  4603. }
  4604.  
  4605. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  4606.    counting parens in *DEPTHPTR,
  4607.    and return if reach LIMIT
  4608.    or before a `)' that would make *DEPTHPTR negative
  4609.    or before a comma when *DEPTHPTR is zero.
  4610.    Single and double quotes are matched and termination
  4611.    is inhibited within them.  Comments also inhibit it.
  4612.    Value returned is pointer to stopping place.
  4613.  
  4614.    Increment *NEWLINES each time a newline is passed.
  4615.    Set *COMMENTS to 1 if a comment is seen.  */
  4616.  
  4617. U_CHAR *
  4618. macarg1 (start, limit, depthptr, newlines, comments)
  4619.      U_CHAR *start;
  4620.      register U_CHAR *limit;
  4621.      int *depthptr, *newlines, *comments;
  4622. {
  4623.   register U_CHAR *bp = start;
  4624.  
  4625.   while (bp < limit) {
  4626.     switch (*bp) {
  4627.     case '(':
  4628.       (*depthptr)++;
  4629.       break;
  4630.     case ')':
  4631.       if (--(*depthptr) < 0)
  4632.     return bp;
  4633.       break;
  4634.     case '\\':
  4635.       /* Traditionally, backslash makes following char not special.  */
  4636.       if (!traditional)
  4637.     break;
  4638.       if (bp + 1 < limit)
  4639.     {
  4640.       bp++;
  4641.       /* But count source lines anyway.  */
  4642.       if (*bp == '\n')
  4643.         ++*newlines;
  4644.     }
  4645.       break;
  4646.     case '\n':
  4647.       ++*newlines;
  4648.       break;
  4649.     case '/':
  4650.       if (bp[1] == '\\' && bp[2] == '\n')
  4651.     newline_fix (bp + 1);
  4652.       if (cplusplus && bp[1] == '/') {
  4653.     *comments = 1;
  4654.     bp += 2;
  4655.     while (bp < limit && *bp++ != '\n') ;
  4656.     ++*newlines;
  4657.     break;
  4658.       }
  4659.       if (bp[1] != '*' || bp + 1 >= limit)
  4660.     break;
  4661.       *comments = 1;
  4662.       bp += 2;
  4663.       while (bp + 1 < limit) {
  4664.     if (bp[0] == '*'
  4665.         && bp[1] == '\\' && bp[2] == '\n')
  4666.       newline_fix (bp + 1);
  4667.     if (bp[0] == '*' && bp[1] == '/')
  4668.       break;
  4669.     if (*bp == '\n') ++*newlines;
  4670.     bp++;
  4671.       }
  4672.       bp += 1;
  4673.       break;
  4674.     case '\'':
  4675.     case '\"':
  4676.       {
  4677.     int quotec;
  4678.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  4679.       if (*bp == '\\') {
  4680.         bp++;
  4681.         if (*bp == '\n')
  4682.           ++*newlines;
  4683.         while (*bp == '\\' && bp[1] == '\n') {
  4684.           bp += 2;
  4685.         }
  4686.       } else if (*bp == '\n') {
  4687.         ++*newlines;
  4688.         if (quotec == '\'')
  4689.           break;
  4690.       }
  4691.     }
  4692.       }
  4693.       break;
  4694.     case ',':
  4695.       if ((*depthptr) == 0)
  4696.     return bp;
  4697.       break;
  4698.     }
  4699.     bp++;
  4700.   }
  4701.  
  4702.   return bp;
  4703. }
  4704.  
  4705. /* Discard comments and duplicate newlines
  4706.    in the string of length LENGTH at START,
  4707.    except inside of string constants.
  4708.    The string is copied into itself with its beginning staying fixed.  
  4709.  
  4710.    NEWLINES is the number of newlines that must be duplicated.
  4711.    We assume that that much extra space is available past the end
  4712.    of the string.  */
  4713.  
  4714. int
  4715. discard_comments (start, length, newlines)
  4716.      U_CHAR *start;
  4717.      int length;
  4718.      int newlines;
  4719. {
  4720.   register U_CHAR *ibp;
  4721.   register U_CHAR *obp;
  4722.   register U_CHAR *limit;
  4723.   register int c;
  4724.  
  4725.   /* If we have newlines to duplicate, copy everything
  4726.      that many characters up.  Then, in the second part,
  4727.      we will have room to insert the newlines
  4728.      while copying down.
  4729.      NEWLINES may actually be too large, because it counts
  4730.      newlines in string constants, and we don't duplicate those.
  4731.      But that does no harm.  */
  4732.   if (newlines > 0) {
  4733.     ibp = start + length;
  4734.     obp = ibp + newlines;
  4735.     limit = start;
  4736.     while (limit != ibp)
  4737.       *--obp = *--ibp;
  4738.   }
  4739.  
  4740.   ibp = start + newlines;
  4741.   limit = start + length + newlines;
  4742.   obp = start;
  4743.  
  4744.   while (ibp < limit) {
  4745.     *obp++ = c = *ibp++;
  4746.     switch (c) {
  4747.     case '\n':
  4748.       /* Duplicate the newline.  */
  4749.       *obp++ = '\n';
  4750.       break;
  4751.  
  4752.     case '\\':
  4753.       if (*ibp == '\n') {
  4754.     obp--;
  4755.     ibp++;
  4756.       }
  4757.       break;
  4758.  
  4759.     case '/':
  4760.       if (*ibp == '\\' && ibp[1] == '\n')
  4761.     newline_fix (ibp);
  4762.       /* Delete any comment.  */
  4763.       if (cplusplus && ibp[0] == '/') {
  4764.     obp--;
  4765.     ibp++;
  4766.     while (ibp < limit && *ibp++ != '\n') ;
  4767.     break;
  4768.       }
  4769.       if (ibp[0] != '*' || ibp + 1 >= limit)
  4770.     break;
  4771.       obp--;
  4772.       ibp++;
  4773.       while (ibp + 1 < limit) {
  4774.     if (ibp[0] == '*'
  4775.         && ibp[1] == '\\' && ibp[2] == '\n')
  4776.       newline_fix (ibp + 1);
  4777.     if (ibp[0] == '*' && ibp[1] == '/')
  4778.       break;
  4779.     ibp++;
  4780.       }
  4781.       ibp += 2;
  4782.       break;
  4783.  
  4784.     case '\'':
  4785.     case '\"':
  4786.       /* Notice and skip strings, so that we don't
  4787.      think that comments start inside them,
  4788.      and so we don't duplicate newlines in them.  */
  4789.       {
  4790.     int quotec = c;
  4791.     while (ibp < limit) {
  4792.       *obp++ = c = *ibp++;
  4793.       if (c == quotec)
  4794.         break;
  4795.       if (c == '\n' && quotec == '\'')
  4796.         break;
  4797.       if (c == '\\' && ibp < limit) {
  4798.         while (*ibp == '\\' && ibp[1] == '\n')
  4799.           ibp += 2;
  4800.         *obp++ = *ibp++;
  4801.       }
  4802.     }
  4803.       }
  4804.       break;
  4805.     }
  4806.   }
  4807.  
  4808.   return obp - start;
  4809. }
  4810.  
  4811. /*
  4812.  * error - print error message and increment count of errors.
  4813.  */
  4814. error (msg, arg1, arg2, arg3)
  4815.      char *msg;
  4816. {
  4817.   int i;
  4818.   FILE_BUF *ip = NULL;
  4819.  
  4820.   for (i = indepth; i >= 0; i--)
  4821.     if (instack[i].fname != NULL) {
  4822.       ip = &instack[i];
  4823.       break;
  4824.     }
  4825.  
  4826.   if (ip != NULL)
  4827.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4828.   fprintf (stderr, msg, arg1, arg2, arg3);
  4829.   fprintf (stderr, "\n");
  4830.   errors++;
  4831.   return 0;
  4832. }
  4833.  
  4834. /* Error including a message from `errno'.  */
  4835.  
  4836. error_from_errno (name)
  4837.      char *name;
  4838. {
  4839.   int i;
  4840.   FILE_BUF *ip = NULL;
  4841.   extern int errno, sys_nerr;
  4842.   extern char *sys_errlist[];
  4843.  
  4844.   for (i = indepth; i >= 0; i--)
  4845.     if (instack[i].fname != NULL) {
  4846.       ip = &instack[i];
  4847.       break;
  4848.     }
  4849.  
  4850.   if (ip != NULL)
  4851.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4852.  
  4853.   if (errno < sys_nerr)
  4854.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  4855.   else
  4856.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  4857.  
  4858.   errors++;
  4859.   return 0;
  4860. }
  4861.  
  4862. /* Print error message but don't count it.  */
  4863.  
  4864. warning (msg, arg1, arg2, arg3)
  4865.      char *msg;
  4866. {
  4867.   int i;
  4868.   FILE_BUF *ip = NULL;
  4869.  
  4870.   if (inhibit_warnings)
  4871.     return 0;
  4872.  
  4873.   for (i = indepth; i >= 0; i--)
  4874.     if (instack[i].fname != NULL) {
  4875.       ip = &instack[i];
  4876.       break;
  4877.     }
  4878.  
  4879.   if (ip != NULL)
  4880.     fprintf (stderr, "%s:%d: ", ip->fname, ip->lineno);
  4881.   fprintf (stderr, "warning: ");
  4882.   fprintf (stderr, msg, arg1, arg2, arg3);
  4883.   fprintf (stderr, "\n");
  4884.   return 0;
  4885. }
  4886.  
  4887. error_with_line (line, msg, arg1, arg2, arg3)
  4888.      int line;
  4889.      char *msg;
  4890. {
  4891.   int i;
  4892.   FILE_BUF *ip = NULL;
  4893.  
  4894.   for (i = indepth; i >= 0; i--)
  4895.     if (instack[i].fname != NULL) {
  4896.       ip = &instack[i];
  4897.       break;
  4898.     }
  4899.  
  4900.   if (ip != NULL)
  4901.     fprintf (stderr, "%s:%d: ", ip->fname, line);
  4902.   fprintf (stderr, msg, arg1, arg2, arg3);
  4903.   fprintf (stderr, "\n");
  4904.   errors++;
  4905.   return 0;
  4906. }
  4907.  
  4908. /* Return the line at which an error occurred.
  4909.    The error is not necessarily associated with the current spot
  4910.    in the input stack, so LINE says where.  LINE will have been
  4911.    copied from ip->lineno for the current input level.
  4912.    If the current level is for a file, we return LINE.
  4913.    But if the current level is not for a file, LINE is meaningless.
  4914.    In that case, we return the lineno of the innermost file.  */
  4915. int
  4916. line_for_error (line)
  4917.      int line;
  4918. {
  4919.   int i;
  4920.   int line1 = line;
  4921.  
  4922.   for (i = indepth; i >= 0; ) {
  4923.     if (instack[i].fname != 0)
  4924.       return line1;
  4925.     i--;
  4926.     if (i < 0)
  4927.       return 0;
  4928.     line1 = instack[i].lineno;
  4929.   }
  4930. }
  4931.  
  4932. /*
  4933.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  4934.  *
  4935.  * As things stand, nothing is ever placed in the output buffer to be
  4936.  * removed again except when it's KNOWN to be part of an identifier,
  4937.  * so flushing and moving down everything left, instead of expanding,
  4938.  * should work ok.
  4939.  */
  4940.  
  4941. int
  4942. grow_outbuf (obuf, needed)
  4943.      register FILE_BUF *obuf;
  4944.      register int needed;
  4945. {
  4946.   register U_CHAR *p;
  4947.   int minsize;
  4948.  
  4949.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  4950.     return;
  4951.  
  4952.   /* Make it at least twice as big as it is now.  */
  4953.   obuf->length *= 2;
  4954.   /* Make it have at least 150% of the free space we will need.  */
  4955.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  4956.   if (minsize > obuf->length)
  4957.     obuf->length = minsize;
  4958.  
  4959.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  4960.     memory_full ();
  4961.  
  4962.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  4963.   obuf->buf = p;
  4964. }
  4965.  
  4966. /* Symbol table for macro names and special symbols */
  4967.  
  4968. /*
  4969.  * install a name in the main hash table, even if it is already there.
  4970.  *   name stops with first non alphanumeric, except leading '#'.
  4971.  * caller must check against redefinition if that is desired.
  4972.  * delete_macro () removes things installed by install () in fifo order.
  4973.  * this is important because of the `defined' special symbol used
  4974.  * in #if, and also if pushdef/popdef directives are ever implemented.
  4975.  *
  4976.  * If LEN is >= 0, it is the length of the name.
  4977.  * Otherwise, compute the length by scanning the entire name.
  4978.  *
  4979.  * If HASH is >= 0, it is the precomputed hash code.
  4980.  * Otherwise, compute the hash code.
  4981.  */
  4982. HASHNODE *
  4983. install (name, len, type, value, hash)
  4984.      U_CHAR *name;
  4985.      int len;
  4986.      enum node_type type;
  4987.      int value;
  4988.      int hash;
  4989.         /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
  4990. {
  4991.   register HASHNODE *hp;
  4992.   register int i, bucket;
  4993.   register U_CHAR *p, *q;
  4994.  
  4995.   if (len < 0) {
  4996.     p = name;
  4997.     while (is_idchar[*p])
  4998.       p++;
  4999.     len = p - name;
  5000.   }
  5001.  
  5002.   if (hash < 0)
  5003.     hash = hashf (name, len, HASHSIZE);
  5004.  
  5005.   i = sizeof (HASHNODE) + len + 1;
  5006.   hp = (HASHNODE *) xmalloc (i);
  5007.   bucket = hash;
  5008.   hp->bucket_hdr = &hashtab[bucket];
  5009.   hp->next = hashtab[bucket];
  5010.   hashtab[bucket] = hp;
  5011.   hp->prev = NULL;
  5012.   if (hp->next != NULL)
  5013.     hp->next->prev = hp;
  5014.   hp->type = type;
  5015.   hp->length = len;
  5016.   hp->value.ival = value;
  5017.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  5018.   p = hp->name;
  5019.   q = name;
  5020.   for (i = 0; i < len; i++)
  5021.     *p++ = *q++;
  5022.   hp->name[len] = 0;
  5023.   return hp;
  5024. }
  5025.  
  5026. /*
  5027.  * find the most recent hash node for name name (ending with first
  5028.  * non-identifier char) installed by install
  5029.  *
  5030.  * If LEN is >= 0, it is the length of the name.
  5031.  * Otherwise, compute the length by scanning the entire name.
  5032.  *
  5033.  * If HASH is >= 0, it is the precomputed hash code.
  5034.  * Otherwise, compute the hash code.
  5035.  */
  5036. HASHNODE *
  5037. lookup (name, len, hash)
  5038.      U_CHAR *name;
  5039.      int len;
  5040.      int hash;
  5041. {
  5042.   register U_CHAR *bp;
  5043.   register HASHNODE *bucket;
  5044.  
  5045.   if (len < 0) {
  5046.     for (bp = name; is_idchar[*bp]; bp++) ;
  5047.     len = bp - name;
  5048.   }
  5049.  
  5050.   if (hash < 0)
  5051.     hash = hashf (name, len, HASHSIZE);
  5052.  
  5053.   bucket = hashtab[hash];
  5054.   while (bucket) {
  5055.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  5056.       return bucket;
  5057.     bucket = bucket->next;
  5058.   }
  5059.   return NULL;
  5060. }
  5061.  
  5062. /*
  5063.  * Delete a hash node.  Some weirdness to free junk from macros.
  5064.  * More such weirdness will have to be added if you define more hash
  5065.  * types that need it.
  5066.  */
  5067.  
  5068. /* Note that the DEFINITION of a macro is removed from the hash table
  5069.    but its storage is not freed.  This would be a storage leak
  5070.    except that it is not reasonable to keep undefining and redefining
  5071.    large numbers of macros many times.
  5072.    In any case, this is necessary, because a macro can be #undef'd
  5073.    in the middle of reading the arguments to a call to it.
  5074.    If #undef freed the DEFINITION, that would crash.  */
  5075.  
  5076. delete_macro (hp)
  5077.      HASHNODE *hp;
  5078. {
  5079.  
  5080.   if (hp->prev != NULL)
  5081.     hp->prev->next = hp->next;
  5082.   if (hp->next != NULL)
  5083.     hp->next->prev = hp->prev;
  5084.  
  5085.   /* make sure that the bucket chain header that
  5086.      the deleted guy was on points to the right thing afterwards. */
  5087.   if (hp == *hp->bucket_hdr)
  5088.     *hp->bucket_hdr = hp->next;
  5089.  
  5090. #if 0
  5091.   if (hp->type == T_MACRO) {
  5092.     DEFINITION *d = hp->value.defn;
  5093.     struct reflist *ap, *nextap;
  5094.  
  5095.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  5096.       nextap = ap->next;
  5097.       free (ap);
  5098.     }
  5099.     free (d);
  5100.   }
  5101. #endif
  5102.   free (hp);
  5103. }
  5104.  
  5105. /*
  5106.  * return hash function on name.  must be compatible with the one
  5107.  * computed a step at a time, elsewhere
  5108.  */
  5109. int
  5110. hashf (name, len, hashsize)
  5111.      register U_CHAR *name;
  5112.      register int len;
  5113.      int hashsize;
  5114. {
  5115.   register int r = 0;
  5116.  
  5117.   while (len--)
  5118.     r = HASHSTEP (r, *name++);
  5119.  
  5120.   return MAKE_POS (r) % hashsize;
  5121. }
  5122.  
  5123. /* Dump all macro definitions as #defines to stdout.  */
  5124.  
  5125. void
  5126. dump_all_macros ()
  5127. {
  5128.   int bucket;
  5129.  
  5130.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  5131.     register HASHNODE *hp;
  5132.  
  5133.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  5134.       if (hp->type == T_MACRO) {
  5135.     register DEFINITION *defn = hp->value.defn;
  5136.     struct reflist *ap;
  5137.     int offset;
  5138.     int concat;
  5139.  
  5140.  
  5141.     /* Print the definition of the macro HP.  */
  5142.  
  5143.     printf ("#define %s", hp->name);
  5144.     if (defn->nargs >= 0) {
  5145.       int i;
  5146.  
  5147.       printf ("(");
  5148.       for (i = 0; i < defn->nargs; i++) {
  5149.         dump_arg_n (defn, i);
  5150.         if (i + 1 < defn->nargs)
  5151.           printf (", ");
  5152.       }
  5153.       printf (")");
  5154.     }
  5155.  
  5156.     printf (" ");
  5157.  
  5158.     offset = 0;
  5159.     concat = 0;
  5160.     for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  5161.       dump_defn_1 (defn->expansion, offset, ap->nchars);
  5162.       if (ap->nchars != 0)
  5163.         concat = 0;
  5164.       offset += ap->nchars;
  5165.       if (ap->stringify)
  5166.         printf (" #");
  5167.       if (ap->raw_before && !concat)
  5168.         printf (" ## ");
  5169.       concat = 0;
  5170.       dump_arg_n (defn, ap->argno);
  5171.       if (ap->raw_after) {
  5172.         printf (" ## ");
  5173.         concat = 1;
  5174.       }
  5175.     }
  5176.     dump_defn_1 (defn->expansion, offset, defn->length - offset);
  5177.     printf ("\n");
  5178.       }
  5179.     }
  5180.   }
  5181. }
  5182.  
  5183. /* Output to stdout a substring of a macro definition.
  5184.    BASE is the beginning of the definition.
  5185.    Output characters START thru LENGTH.
  5186.    Discard newlines outside of strings, thus
  5187.    converting funny-space markers to ordinary spaces.  */
  5188.  
  5189. dump_defn_1 (base, start, length)
  5190.      U_CHAR *base;
  5191.      int start;
  5192.      int length;
  5193. {
  5194.   U_CHAR *p = base + start;
  5195.   U_CHAR *limit = base + start + length;
  5196.  
  5197.   while (p < limit) {
  5198.     if (*p != '\n')
  5199.       putchar (*p);
  5200.     else if (*p == '\"' || *p =='\'') {
  5201.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
  5202.       fwrite (p, p1 - p, 1, stdout);
  5203.       p = p1 - 1;
  5204.     }
  5205.     p++;
  5206.   }
  5207. }
  5208.  
  5209. /* Print the name of argument number ARGNUM of macro definition DEFN.
  5210.    Recall that DEFN->argnames contains all the arg names
  5211.    concatenated in reverse order with comma-space in between.  */
  5212.  
  5213. dump_arg_n (defn, argnum)
  5214.      DEFINITION *defn;
  5215.      int argnum;
  5216. {
  5217.   register U_CHAR *p = defn->argnames;
  5218.   while (argnum + 1 < defn->nargs) {
  5219.     p = (U_CHAR *) index (p, ' ') + 1;
  5220.     argnum++;
  5221.   }
  5222.  
  5223.   while (*p && *p != ',') {
  5224.     putchar (*p);
  5225.     p++;
  5226.   }
  5227. }
  5228.  
  5229. /* Initialize syntactic classifications of characters.  */
  5230.  
  5231. initialize_char_syntax ()
  5232. {
  5233.   register int i;
  5234.  
  5235.   /*
  5236.    * Set up is_idchar and is_idstart tables.  These should be
  5237.    * faster than saying (is_alpha (c) || c == '_'), etc.
  5238.    * Must do set up these things before calling any routines tthat
  5239.    * refer to them.
  5240.    */
  5241.   for (i = 'a'; i <= 'z'; i++) {
  5242.     is_idchar[i - 'a' + 'A'] = 1;
  5243.     is_idchar[i] = 1;
  5244.     is_idstart[i - 'a' + 'A'] = 1;
  5245.     is_idstart[i] = 1;
  5246.   }
  5247.   for (i = '0'; i <= '9'; i++)
  5248.     is_idchar[i] = 1;
  5249.   is_idchar['_'] = 1;
  5250.   is_idstart['_'] = 1;
  5251.   is_idchar['$'] = dollars_in_ident;
  5252.   is_idstart['$'] = dollars_in_ident;
  5253.  
  5254.   /* horizontal space table */
  5255.   is_hor_space[' '] = 1;
  5256.   is_hor_space['\t'] = 1;
  5257.   is_hor_space['\v'] = 1;
  5258.   is_hor_space['\f'] = 1;
  5259.   is_hor_space['\r'] = 1;
  5260.  
  5261.   is_space[' '] = 1;
  5262.   is_space['\t'] = 1;
  5263.   is_space['\v'] = 1;
  5264.   is_space['\f'] = 1;
  5265.   is_space['\n'] = 1;
  5266.   is_space['\r'] = 1;
  5267. }
  5268.  
  5269. /* Initialize the built-in macros.  */
  5270.  
  5271. initialize_builtins ()
  5272. {
  5273.   install ("__LINE__", -1, T_SPECLINE, 0, -1);
  5274.   install ("__DATE__", -1, T_DATE, 0, -1);
  5275.   install ("__FILE__", -1, T_FILE, 0, -1);
  5276.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, -1);
  5277.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, -1);
  5278.   install ("__VERSION__", -1, T_VERSION, 0, -1);
  5279.   install ("__TIME__", -1, T_TIME, 0, -1);
  5280.   if (!traditional)
  5281.     install ("__STDC__", -1, T_CONST, STDC_VALUE, -1);
  5282. /*  install ("__GNU__", -1, T_CONST, 1, -1);  */
  5283. /*  This is supplied using a -D by the compiler driver
  5284.     so that it is present only when truly compiling with GNU C.  */
  5285. }
  5286.  
  5287. /*
  5288.  * process a given definition string, for initialization
  5289.  * If STR is just an identifier, define it with value 1.
  5290.  * If STR has anything after the identifier, then it should
  5291.  * be identifier-space-definition.
  5292.  */
  5293. make_definition (str)
  5294.      U_CHAR *str;
  5295. {
  5296.   FILE_BUF *ip;
  5297.   struct directive *kt;
  5298.   U_CHAR *buf, *p;
  5299.  
  5300.   buf = str;
  5301.   p = str;
  5302.   while (is_idchar[*p]) p++;
  5303.   if (p == str) {
  5304.     error ("malformed option `-D %s'", str);
  5305.     return;
  5306.   }
  5307.   if (*p == 0) {
  5308.     buf = (U_CHAR *) alloca (p - buf + 4);
  5309.     strcpy ((char *)buf, str);
  5310.     strcat ((char *)buf, " 1");
  5311.   } else if (*p != ' ') {
  5312.     error ("malformed option `-D %s'", str);
  5313.     return;
  5314.   } else {
  5315.     U_CHAR *q;
  5316.     /* Copy the entire option so we can modify it.  */
  5317.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  5318.     strncpy (buf, str, p - str);
  5319.     /* Change the = to a space.  */
  5320.     buf[p - str] = ' ';
  5321.     /* Scan for any backslash-newline and remove it.  */
  5322.     p++;
  5323.     q = &buf[p - str];
  5324.     while (*p) {
  5325.       if (*p == '\\' && p[1] == '\n')
  5326.     p += 2;
  5327.       /* Change newline chars into newline-markers.  */
  5328.       else if (*p == '\n')
  5329.     {
  5330.       *q++ = '\n';
  5331.       *q++ = '\n';
  5332.       p++;
  5333.     }
  5334.       else
  5335.     *q++ = *p++;
  5336.     }
  5337.     *q = 0;
  5338.   }
  5339.   
  5340.   ip = &instack[++indepth];
  5341.   ip->fname = "*Initialization*";
  5342.  
  5343.   ip->buf = ip->bufp = buf;
  5344.   ip->length = strlen (buf);
  5345.   ip->lineno = 1;
  5346.   ip->macro = 0;
  5347.   ip->free_ptr = 0;
  5348.   ip->if_stack = if_stack;
  5349.  
  5350.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  5351.     ;
  5352.  
  5353.   /* pass NULL as output ptr to do_define since we KNOW it never
  5354.      does any output.... */
  5355.   do_define (buf, buf + strlen (buf) , NULL, kt);
  5356.   --indepth;
  5357. }
  5358.  
  5359. /* JF, this does the work for the -U option */
  5360. make_undef (str)
  5361.      U_CHAR *str;
  5362. {
  5363.   FILE_BUF *ip;
  5364.   struct directive *kt;
  5365.  
  5366.   ip = &instack[++indepth];
  5367.   ip->fname = "*undef*";
  5368.  
  5369.   ip->buf = ip->bufp = str;
  5370.   ip->length = strlen (str);
  5371.   ip->lineno = 1;
  5372.   ip->macro = 0;
  5373.   ip->free_ptr = 0;
  5374.   ip->if_stack = if_stack;
  5375.  
  5376.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  5377.     ;
  5378.  
  5379.   do_undef (str,str + strlen (str) - 1, NULL, kt);
  5380.   --indepth;
  5381. }
  5382.  
  5383. /* Add output to `deps_buffer' for the -M switch.
  5384.    STRING points to the text to be output.
  5385.    SIZE is the number of bytes, or 0 meaning output until a null.
  5386.    If SIZE is nonzero, we break the line first, if it is long enough.  */
  5387.  
  5388. deps_output (string, size)
  5389.      char *string;
  5390.      int size;
  5391. {
  5392. #ifndef MAX_OUTPUT_COLUMNS
  5393. #define MAX_OUTPUT_COLUMNS 75
  5394. #endif
  5395.   if (size != 0 && deps_column != 0
  5396.       && size + deps_column > MAX_OUTPUT_COLUMNS) {
  5397.     deps_output ("\\\n  ", 0);
  5398.     deps_column = 0;
  5399.   }
  5400.  
  5401.   if (size == 0)
  5402.     size = strlen (string);
  5403.  
  5404.   if (deps_size + size + 1 > deps_allocated_size) {
  5405.     deps_allocated_size = deps_size + size + 50;
  5406.     deps_allocated_size *= 2;
  5407.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  5408.   }
  5409.   bcopy (string, &deps_buffer[deps_size], size);
  5410.   deps_size += size;
  5411.   deps_column += size;
  5412.   deps_buffer[deps_size] = 0;
  5413. }
  5414.  
  5415. #ifndef BSD
  5416. #ifndef BSTRING
  5417.  
  5418. void
  5419. bzero (b, length)
  5420.      register char *b;
  5421.      register int length;
  5422. {
  5423. #ifdef VMS
  5424.   short zero = 0;
  5425.   long max_str = 65535;
  5426.  
  5427.   while (length > max_str) {
  5428.     (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  5429.     length -= max_str;
  5430.     b += max_str;
  5431.   }
  5432.   (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
  5433. #else
  5434.   while (length-- > 0)
  5435.     *b++ = 0;
  5436. #endif /* not VMS */
  5437. }
  5438.  
  5439. void
  5440. bcopy (b1, b2, length)
  5441.      register char *b1;
  5442.      register char *b2;
  5443.      register int length;
  5444. {
  5445. #ifdef VMS
  5446.   long max_str = 65535;
  5447.  
  5448.   while (length > max_str) {
  5449.     (void) LIB$MOVC3 (&max_str, b1, b2);
  5450.     length -= max_str;
  5451.     b1 += max_str;
  5452.     b2 += max_str;
  5453.   }
  5454.   (void) LIB$MOVC3 (&length, b1, b2);
  5455. #else
  5456.   while (length-- > 0)
  5457.     *b2++ = *b1++;
  5458. #endif /* not VMS */
  5459. }
  5460.  
  5461. int
  5462. bcmp (b1, b2, length)    /* This could be a macro! */
  5463.      register char *b1;
  5464.      register char *b2;
  5465.       register int length;
  5466. {
  5467. #ifdef VMS
  5468.    struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
  5469.    struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
  5470.  
  5471.    return STR$COMPARE (&src1, &src2);
  5472. #else
  5473.    while (length-- > 0)
  5474.      if (*b1++ != *b2++)
  5475.        return 1;
  5476.  
  5477.    return 0;
  5478. #endif /* not VMS */
  5479. }
  5480. #endif /* not BSTRING */
  5481. #endif /* not BSD */
  5482.  
  5483.  
  5484. void
  5485. fatal (str, arg)
  5486.      char *str, *arg;
  5487. {
  5488.   fprintf (stderr, "%s: ", progname);
  5489.   fprintf (stderr, str, arg);
  5490.   fprintf (stderr, "\n");
  5491.   exit (FATAL_EXIT_CODE);
  5492. }
  5493.  
  5494. /* More 'friendly' abort that prints the line and file.
  5495.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  5496.  
  5497. void
  5498. fancy_abort ()
  5499. {
  5500.   fatal ("Internal gcc abort.");
  5501. }
  5502.  
  5503. void
  5504. perror_with_name (name)
  5505.      char *name;
  5506. {
  5507.   extern int errno, sys_nerr;
  5508.   extern char *sys_errlist[];
  5509.  
  5510.   fprintf (stderr, "%s: ", progname);
  5511.   if (errno < sys_nerr)
  5512.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  5513.   else
  5514.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  5515.   errors++;
  5516. }
  5517.  
  5518. void
  5519. pfatal_with_name (name)
  5520.      char *name;
  5521. {
  5522.   perror_with_name (name);
  5523. #ifdef VMS
  5524.   exit (vaxc$errno);
  5525. #else
  5526.   exit (FATAL_EXIT_CODE);
  5527. #endif
  5528. }
  5529.  
  5530.  
  5531. void
  5532. memory_full ()
  5533. {
  5534.   fatal ("Memory exhausted.");
  5535. }
  5536.  
  5537.  
  5538. char *
  5539. xmalloc (size)
  5540.      int size;
  5541. {
  5542.   extern char *malloc ();
  5543.   register char *ptr = malloc (size);
  5544.   if (ptr != 0) return (ptr);
  5545.   memory_full ();
  5546.   /*NOTREACHED*/
  5547. }
  5548.  
  5549. char *
  5550. xrealloc (old, size)
  5551.      char *old;
  5552.      int size;
  5553. {
  5554.   extern char *realloc ();
  5555.   register char *ptr = realloc (old, size);
  5556.   if (ptr != 0) return (ptr);
  5557.   memory_full ();
  5558.   /*NOTREACHED*/
  5559. }
  5560.  
  5561. char *
  5562. xcalloc (number, size)
  5563.      int number, size;
  5564. {
  5565.   extern char *malloc ();
  5566.   register int total = number * size;
  5567.   register char *ptr = malloc (total);
  5568.   if (ptr != 0) {
  5569.     if (total > 100)
  5570.       bzero (ptr, total);
  5571.     else {
  5572.       /* It's not too long, so loop, zeroing by longs.
  5573.      It must be safe because malloc values are always well aligned.  */
  5574.       register long *zp = (long *) ptr;
  5575.       register long *zl = (long *) (ptr + total - 4);
  5576.       register int i = total - 4;
  5577.       while (zp < zl)
  5578.     *zp++ = 0;
  5579.       if (i < 0)
  5580.     i = 0;
  5581.       while (i < total)
  5582.     ptr[i++] = 0;
  5583.     }
  5584.     return ptr;
  5585.   }
  5586.   memory_full ();
  5587.   /*NOTREACHED*/
  5588. }
  5589.  
  5590. char *
  5591. savestring (input)
  5592.      char *input;
  5593. {
  5594.   int size = strlen (input);
  5595.   char *output = xmalloc (size + 1);
  5596.   strcpy (output, input);
  5597.   return output;
  5598. }
  5599.  
  5600. /* Get the file-mode and data size of the file open on FD
  5601.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  5602.  
  5603. int
  5604. file_size_and_mode (fd, mode_pointer, size_pointer)
  5605.      int fd;
  5606.      int *mode_pointer;
  5607.      long int *size_pointer;
  5608. {
  5609.   struct stat sbuf;
  5610.  
  5611.   if (fstat (fd, &sbuf) < 0) return (-1);
  5612.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  5613.   if (size_pointer) *size_pointer = sbuf.st_size;
  5614.   return 0;
  5615. }
  5616.  
  5617. #ifdef    VMS
  5618.  
  5619. /* Under VMS we need to fix up the "include" specification
  5620.    filename so that everything following the 1st slash is
  5621.    changed into its correct VMS file specification. */
  5622.  
  5623. hack_vms_include_specification (fname)
  5624.      char *fname;
  5625. {
  5626.   register char *cp, *cp1, *cp2;
  5627.   char Local[512];
  5628.   extern char *index (), *rindex ();
  5629.  
  5630.   /* Ignore leading "./"s */
  5631.   while (fname[0] == '.' && fname[1] == '/')
  5632.     strcpy (fname, fname+2);
  5633.   /* Look for the boundary between the VMS and UNIX filespecs */
  5634.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  5635.   if (cp == 0) cp == rindex (fname, '>'); /* ... Ditto            */
  5636.   if (cp == 0) cp == rindex (fname, ':'); /* Look for end of devspec. */
  5637.   if (cp) {
  5638.     cp++;
  5639.   } else {
  5640.     cp = index (fname, '/');    /* Look for the "/" */
  5641.   }
  5642.   /* See if we found that 1st slash */
  5643.   if (cp == 0) return;        /* Nothing to do!!! */
  5644.   if (*cp != '/') return;    /* Nothing to do!!! */
  5645.   /* Point to the UNIX filename part (which needs to be fixed!) */
  5646.   cp1 = cp+1;
  5647.   /* If the directory spec is not rooted, we can just copy
  5648.      the UNIX filename part and we are done */
  5649.   if (((cp - fname) > 2)
  5650.       && ((cp[-1] == ']') || (cp[-1] == '>'))
  5651.       && (cp[-2] != '.')) {
  5652.     strcpy (cp, cp1);
  5653.     return;
  5654.   }
  5655.   /* If there are no other slashes then the filename will be
  5656.      in the "root" directory.  Otherwise, we need to add
  5657.      directory specifications. */
  5658.   if (index (cp1, '/') == 0) {
  5659.     /* Just add "[000000]" as the directory string */
  5660.     strcpy (Local, "[000000]");
  5661.     cp2 = Local + strlen (Local);
  5662.   } else {
  5663.     /* Open the directory specification */
  5664.     cp2 = Local;
  5665.     *cp2++ = '[';
  5666.     /* As long as there are still subdirectories to add, do them. */
  5667.     while (index (cp1, '/') != 0) {
  5668.       /* If this token is "." we can ignore it */
  5669.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  5670.     cp1 += 2;
  5671.     continue;
  5672.       }
  5673.       /* Add a subdirectory spec. */
  5674.       if (cp2 != Local+1) *cp2++ = '.';
  5675.       /* If this is ".." then the spec becomes "-" */
  5676.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  5677.     /* Add "-" and skip the ".." */
  5678.     *cp2++ = '-';
  5679.     cp1 += 3;
  5680.     continue;
  5681.       }
  5682.       /* Copy the subdirectory */
  5683.       while (*cp1 != '/') *cp2++= *cp1++;
  5684.       cp1++;            /* Skip the "/" */
  5685.     }
  5686.     /* Close the directory specification */
  5687.     *cp2++ = ']';
  5688.   }
  5689.   /* Now add the filename */
  5690.   while (*cp1) *cp2++ = *cp1++;
  5691.   *cp2 = 0;
  5692.   /* Now append it to the original VMS spec. */
  5693.   strcpy (cp, Local);
  5694.   return;
  5695. }
  5696. #endif    /* VMS */
  5697.  
  5698. #ifdef    VMS
  5699.  
  5700. /* These are the read/write replacement routines for
  5701.    VAX-11 "C".  They make read/write behave enough
  5702.    like their UNIX counterparts that CCCP will work */
  5703.  
  5704. int
  5705. read (fd, buf, size)
  5706.      int fd;
  5707.      char *buf;
  5708.      int size;
  5709. {
  5710. #undef    read    /* Get back the REAL read routine */
  5711.   register int i;
  5712.   register int total = 0;
  5713.  
  5714.   /* Read until the buffer is exhausted */
  5715.   while (size > 0) {
  5716.     /* Limit each read to 32KB */
  5717.     i = (size > (32*1024)) ? (32*1024) : size;
  5718.     i = read (fd, buf, i);
  5719.     if (i <= 0) {
  5720.       if (i == 0) return (total);
  5721.       return(i);
  5722.     }
  5723.     /* Account for this read */
  5724.     total += i;
  5725.     buf += i;
  5726.     size -= i;
  5727.   }
  5728.   return (total);
  5729. }
  5730.  
  5731. int
  5732. write (fd, buf, size)
  5733.      int fd;
  5734.      char *buf;
  5735.      int size;
  5736. {
  5737. #undef    write    /* Get back the REAL write routine */
  5738.   int i;
  5739.   int j;
  5740.  
  5741.   /* Limit individual writes to 32Kb */
  5742.   i = size;
  5743.   while (i > 0) {
  5744.     j = (i > (32*1024)) ? (32*1024) : i;
  5745.     if (write (fd, buf, j) < 0) return (-1);
  5746.     /* Account for the data written */
  5747.     buf += j;
  5748.     i -= j;
  5749.   }
  5750.   return (size);
  5751. }
  5752.  
  5753. #endif /* VMS */
  5754.