home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / d / gcc / gcc-2.7 / gcc-2 / gcc-2.7.0 / cpplib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-15  |  197.2 KB  |  7,529 lines

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