home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / cccp-mach.c < prev    next >
C/C++ Source or Header  |  1997-04-11  |  336KB  |  12,187 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2.    Copyright (C) 1986, 87, 89, 92, 93, 1994 Free Software Foundation, Inc.
  3.    Written by Paul Rubin, June 1986
  4.    Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  In other words, you are welcome to use, share and improve this program.
  21.  You are forbidden to forbid anyone else to use, share and improve
  22.  what you give them.   Help stamp out software-hoarding!  */
  23.  
  24. #define MAX_FILENAME_LEN 256
  25. #define NEXT_FRAMEWORKS
  26. #define NEXT_CPP_SERVER
  27. #define NEXT_SEMANTICS
  28. #define NEXT_OBJC_RUNTIME
  29. #define MALLOC_INPUT_BUFFERS
  30.  
  31. typedef unsigned char U_CHAR;
  32.  
  33. #ifdef EMACS
  34. #define NO_SHORTNAMES
  35. #include "../src/config.h"
  36. #ifdef open
  37. #undef open
  38. #undef read
  39. #undef write
  40. #endif /* open */
  41. #endif /* EMACS */
  42.  
  43. /* The macro EMACS is defined when cpp is distributed as part of Emacs,
  44.    for the sake of machines with limited C compilers.  */
  45. #ifndef EMACS
  46. #include "config.h"
  47. #endif /* not EMACS */
  48.  
  49. #ifndef STANDARD_INCLUDE_DIR
  50. #define STANDARD_INCLUDE_DIR "/usr/include"
  51. #endif
  52.  
  53. #ifndef LOCAL_INCLUDE_DIR
  54. #define LOCAL_INCLUDE_DIR "/usr/local/include"
  55. #endif
  56.  
  57. #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
  58. #ifdef __STDC__
  59. #define PTR_INT_TYPE ptrdiff_t
  60. #else
  61. #define PTR_INT_TYPE long
  62. #endif
  63. #endif /* 0 */
  64.  
  65. #include "pcp.h"
  66.  
  67. #ifndef STDC_VALUE
  68. #define STDC_VALUE 1
  69. #endif
  70.  
  71. /* By default, colon separates directories in a path.  */
  72. #ifndef PATH_SEPARATOR
  73. #define PATH_SEPARATOR ':'
  74. #endif
  75.  
  76. /* In case config.h defines these.  */
  77. #undef bcopy
  78. #undef bzero
  79. #undef bcmp
  80.  
  81. #include <sys/types.h>
  82. #include <sys/stat.h>
  83. #include <ctype.h>
  84. #include <stdio.h>
  85. #include <signal.h>
  86.  
  87. #ifndef MALLOC_INPUT_BUFFERS
  88. #include <mach/mach.h>
  89. #endif
  90.  
  91. #ifdef REPORT_EVENT
  92. #include "next/make-support.h"
  93. #endif
  94.  
  95. #ifndef VMS
  96. #ifndef USG
  97. #include <sys/time.h>        /* for __DATE__ and __TIME__ */
  98. #include <sys/resource.h>
  99. #else
  100. #include <time.h>
  101. #include <fcntl.h>
  102. #endif /* USG */
  103. #endif /* not VMS */
  104.  
  105. /* This defines "errno" properly for VMS, and gives us EACCES. */
  106. #include <errno.h>
  107.  
  108. /* VMS-specific definitions */
  109. #ifdef VMS
  110. #include <time.h>
  111. #include <perror.h>        /* This defines sys_errlist/sys_nerr properly */
  112. #include <descrip.h>
  113. #ifndef NEXT_CPP_SERVER
  114. #define O_RDONLY    0    /* Open arg for Read/Only  */
  115. #define O_WRONLY    1    /* Open arg for Write/Only */
  116. #endif
  117. #define read(fd,buf,size)    VMS_read (fd,buf,size)
  118. #define write(fd,buf,size)    VMS_write (fd,buf,size)
  119. #define open(fname,mode,prot)    VMS_open (fname,mode,prot)
  120. #define fopen(fname,mode)    VMS_fopen (fname,mode)
  121. #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
  122. #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
  123. static char * VMS_strncat ();
  124. static int VMS_read ();
  125. static int VMS_write ();
  126. static int VMS_open ();
  127. static FILE * VMS_fopen ();
  128. static FILE * VMS_freopen ();
  129. static void hack_vms_include_specification ();
  130. typedef struct { unsigned :16, :16, :16; } vms_ino_t;
  131. #define ino_t vms_ino_t
  132. #define INCLUDE_LEN_FUDGE 10    /* leave room for VMS syntax conversion */
  133. #ifdef __GNUC__
  134. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  135. #endif /* __GNUC__ */
  136. #endif /* VMS */
  137.   
  138. extern char *index ();
  139. extern char *rindex ();
  140.  
  141. #ifndef NEXT_CPP_SERVER
  142. #ifndef O_RDONLY
  143. #define O_RDONLY 0
  144. #endif
  145. #endif
  146.  
  147. #undef MIN
  148. #undef MAX
  149. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  150. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  151.  
  152. /* Find the largest host integer type and set its size and type.  */
  153.  
  154. #ifndef HOST_BITS_PER_WIDE_INT
  155.  
  156. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  157. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  158. #define HOST_WIDE_INT long
  159. #else
  160. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  161. #define HOST_WIDE_INT int
  162. #endif
  163.  
  164. #endif
  165.  
  166. #ifndef S_ISREG
  167. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  168. #endif
  169.  
  170. #ifndef S_ISDIR
  171. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  172. #endif
  173.  
  174. /* Define a generic NULL if one hasn't already been defined.  */
  175.  
  176. #ifndef NULL
  177. #define NULL 0
  178. #endif
  179.  
  180. #ifndef GENERIC_PTR
  181. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  182. #define GENERIC_PTR void *
  183. #else
  184. #define GENERIC_PTR char *
  185. #endif
  186. #endif
  187.  
  188. #ifndef NULL_PTR
  189. #define NULL_PTR ((GENERIC_PTR)0)
  190. #endif
  191.  
  192. #ifndef INCLUDE_LEN_FUDGE
  193. #define INCLUDE_LEN_FUDGE 0
  194. #endif
  195.  
  196. /* Forward declarations.  */
  197.  
  198. char *xmalloc ();
  199. void error ();
  200. void warning ();
  201.  
  202. /* External declarations.  */
  203.  
  204. extern char *getenv ();
  205. extern FILE *fdopen ();
  206. extern char *version_string;
  207. extern struct tm *localtime ();
  208. extern int sys_nerr;
  209. #if defined(bsd4_4) || defined(__NetBSD__)
  210. extern const char *const sys_errlist[];
  211. #else
  212. extern char *sys_errlist[];
  213. #endif
  214. extern int parse_escape ();
  215.  
  216. #ifndef errno
  217. extern int errno;
  218. #endif
  219.  
  220. /* Forward declarations.  */
  221.  
  222. struct directive;
  223. struct file_buf;
  224. struct arglist;
  225. struct argdata;
  226.  
  227. #if defined(USG) || defined(VMS)
  228. #ifndef BSTRING
  229. void bcopy ();
  230. void bzero ();
  231. int bcmp ();
  232. #endif
  233. #endif
  234.  
  235. /* These functions are declared to return int instead of void since they
  236.    are going to be placed in a table and some old compilers have trouble with
  237.    pointers to functions returning void.  */
  238.  
  239. static int do_define ();
  240. static int do_line ();
  241. static int do_include ();
  242. static int do_undef ();
  243. static int do_error ();
  244. static int do_pragma ();
  245. static int do_ident ();
  246. static int do_if ();
  247. static int do_xifdef ();
  248. static int do_else ();
  249. static int do_elif ();
  250. static int do_endif ();
  251. static int do_sccs ();
  252. static int do_once ();
  253. static int do_assert ();
  254. static int do_unassert ();
  255. static int do_warning ();
  256.  
  257. static struct import_file *add_import ();
  258. static void append_include_chain ();
  259. #ifdef NEXT_FRAMEWORKS
  260. static void append_framework_chain ();
  261. #endif
  262. static void deps_output ();
  263. static void make_undef ();
  264. static void make_definition ();
  265. static void make_assertion ();
  266. static void path_include ();
  267. static void initialize_builtins ();
  268. static void initialize_char_syntax ();
  269. static void dump_arg_n ();
  270. static void dump_defn_1 ();
  271. static void delete_macro ();
  272. static void trigraph_pcp ();
  273. static void rescan ();
  274. static void finclude ();
  275. static void validate_else ();
  276. static int comp_def_part ();
  277. static void error_from_errno ();
  278. static void error_with_line ();
  279. void pedwarn ();
  280. void pedwarn_with_line ();
  281. static void pedwarn_with_file_and_line ();
  282. static void fatal ();
  283. void fancy_abort ();
  284. static void pfatal_with_name ();
  285. static void perror_with_name ();
  286. static void pipe_closed ();
  287. static void print_containing_files ();
  288. static int lookup_import ();
  289. static int redundant_include_p ();
  290. static is_system_include ();
  291. static struct file_name_map *read_name_map ();
  292. static char *read_filename_string ();
  293. static int open_include_file ();
  294. static int check_preconditions ();
  295. static void pcfinclude ();
  296. static void pcstring_used ();
  297. static void write_output ();
  298. #ifdef NEXT_CPP_SERVER
  299. static int write_header_output ();
  300. static time_t file_modtime ();
  301. static void handle_file_modification();
  302. #endif
  303. static int check_macro_name ();
  304. static int compare_defs ();
  305. static int compare_token_lists ();
  306. static int eval_if_expression ();
  307. static int discard_comments ();
  308. static int change_newlines ();
  309. static int line_for_error ();
  310. static int hashf ();
  311. static int file_size_and_mode ();
  312.  
  313. static struct arglist *read_token_list ();
  314. static void free_token_list ();
  315.  
  316. static struct hashnode *install ();
  317. struct hashnode *lookup ();
  318.  
  319. static struct assertion_hashnode *assertion_install ();
  320. static struct assertion_hashnode *assertion_lookup ();
  321.  
  322. static char *xrealloc ();
  323. static char *xcalloc ();
  324. static char *savestring ();
  325.  
  326. static void delete_assertion ();
  327. static void macroexpand ();
  328. static void dump_all_macros ();
  329. static void conditional_skip ();
  330. static void skip_if_group ();
  331. static void output_line_command ();
  332.  
  333. #ifdef NEXT_SEMANTICS
  334. /* convert rtf */
  335. static void buf_convert_rtf ();
  336. static const char * rtf_to_ascii ();
  337. #endif
  338.  
  339. /* Last arg to output_line_command.  */
  340. #ifdef NEXT_CPP_SERVER
  341. enum file_change_code {same_file, enter_file, leave_file,
  342.                        enter_expansion, leave_expansion};
  343. #else
  344. enum file_change_code {same_file, enter_file, leave_file};
  345. #endif
  346.  
  347. static int grow_outbuf ();
  348. static int handle_directive ();
  349. static void memory_full ();
  350.  
  351. static U_CHAR *macarg1 ();
  352. static char *macarg ();
  353.  
  354. static U_CHAR *skip_to_end_of_comment ();
  355. static U_CHAR *skip_quoted_string ();
  356. static U_CHAR *skip_paren_group ();
  357. static char *quote_string ();
  358.  
  359. static char *check_precompiled ();
  360. /* static struct macrodef create_definition ();    [moved below] */
  361. static void dump_single_macro ();
  362. static void output_dots ();
  363.  
  364. #ifndef FAILURE_EXIT_CODE
  365. #define FAILURE_EXIT_CODE 33    /* gnu cc command understands this */
  366. #endif
  367.  
  368. #ifndef SUCCESS_EXIT_CODE
  369. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  370. #endif
  371.  
  372. /* Name under which this program was invoked.  */
  373.  
  374. static char *progname;
  375.  
  376. /* Nonzero means use extra default include directories for C++.  */
  377.  
  378. static int cplusplus;
  379.  
  380. /* Nonzero means handle cplusplus style comments */
  381.  
  382. static int cplusplus_comments;
  383.  
  384. /* Nonzero means handle #import, for objective C.  */
  385.  
  386. static int objc;
  387.  
  388. /* Nonzero means this is an assembly file, and allow
  389.    unknown directives, which could be comments.  */
  390.  
  391. static int lang_asm;
  392.  
  393. /* Current maximum length of directory names in the search path
  394.    for include files.  (Altered as we get more of them.)  */
  395.  
  396. static int max_include_len;
  397.  
  398. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  399.  
  400. static int for_lint = 0;
  401.  
  402. /* Nonzero means copy comments into the output file.  */
  403.  
  404. static int put_out_comments = 0;
  405.  
  406. /* Nonzero means don't process the ANSI trigraph sequences.  */
  407.  
  408. static int no_trigraphs = 0;
  409.  
  410. /* Nonzero means print the names of included files rather than
  411.    the preprocessed output.  1 means just the #include "...",
  412.    2 means #include <...> as well.  */
  413.  
  414. static int print_deps = 0;
  415.  
  416. /* Nonzero if missing .h files in -M output are assumed to be generated
  417.    files and not errors.  */
  418.  
  419. static int print_deps_missing_files = 0;
  420.  
  421. /* Nonzero means print names of header files (-H).  */
  422.  
  423. static int print_include_names = 0;
  424.  
  425. /* Nonzero means don't output line number information.  */
  426.  
  427. static int no_line_commands;
  428.  
  429. /* dump_only means inhibit output of the preprocessed text
  430.              and instead output the definitions of all user-defined
  431.              macros in a form suitable for use as input to cccp.
  432.    dump_names means pass #define and the macro name through to output.
  433.    dump_definitions means pass the whole definition (plus #define) through
  434. */
  435.  
  436. static enum {dump_none, dump_only, dump_names, dump_definitions}
  437.      dump_macros = dump_none;
  438.  
  439. /* Nonzero means pass all #define and #undef directives which we actually
  440.    process through to the output stream.  This feature is used primarily
  441.    to allow cc1 to record the #defines and #undefs for the sake of
  442.    debuggers which understand about preprocessor macros, but it may
  443.    also be useful with -E to figure out how symbols are defined, and
  444.    where they are defined.  */
  445. static int debug_output = 0;
  446.  
  447. /* Nonzero indicates special processing used by the pcp program.  The
  448.    special effects of this mode are: 
  449.      
  450.      Inhibit all macro expansion, except those inside #if directives.
  451.  
  452.      Process #define directives normally, and output their contents 
  453.      to the output file.
  454.  
  455.      Output preconditions to pcp_outfile indicating all the relevant
  456.      preconditions for use of this file in a later cpp run.
  457. */
  458. static FILE *pcp_outfile;
  459.  
  460. /* Nonzero means we are inside an IF during a -pcp run.  In this mode
  461.    macro expansion is done, and preconditions are output for all macro
  462.    uses requiring them. */
  463. static int pcp_inside_if;
  464.  
  465. /* Nonzero means never to include precompiled files.
  466.    This is 1 since there's no way now to make precompiled files,
  467.    so it's not worth testing for them.  */
  468. static int no_precomp = 1;
  469.  
  470. /* Nonzero means give all the error messages the ANSI standard requires.  */
  471.  
  472. int pedantic;
  473.  
  474. /* Nonzero means try to make failure to fit ANSI C an error.  */
  475.  
  476. static int pedantic_errors;
  477.  
  478. /* Nonzero means don't print warning messages.  -w.  */
  479.  
  480. static int inhibit_warnings = 0;
  481.  
  482. /* Nonzero means warn if slash-star appears in a comment.  */
  483.  
  484. static int warn_comments;
  485.  
  486. /* Nonzero means warn if a macro argument is (or would be)
  487.    stringified with -traditional.  */
  488.  
  489. static int warn_stringify;
  490.  
  491. /* Nonzero means warn if there are any trigraphs.  */
  492.  
  493. static int warn_trigraphs;
  494.  
  495. /* Nonzero means warn if #import is used.  */
  496.  
  497. #ifdef NEXT_OBJC_RUNTIME
  498. static int warn_import = 0;
  499. #else
  500. static int warn_import = 1;
  501. #endif
  502.  
  503. /* Nonzero means turn warnings into errors.  */
  504.  
  505. static int warnings_are_errors;
  506.  
  507. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  508.  
  509. int traditional;
  510.  
  511. /* Nonzero causes output not to be done,
  512.    but directives such as #define that have side effects
  513.    are still obeyed.  */
  514.  
  515. static int no_output;
  516.  
  517. #ifdef NEXT_SEMANTICS
  518. /* Nonzero causes input not to have rtf codes stripped before 
  519.    further processing. */
  520.  
  521. static int no_rtf = 0;
  522. #endif
  523.  
  524. /* Nonzero means this file was included with a -imacros or -include
  525.    command line and should not be recorded as an include file.  */
  526.  
  527. static int no_record_file;
  528.  
  529. /* Nonzero means that we have finished processing the command line options.
  530.    This flag is used to decide whether or not to issue certain errors
  531.    and/or warnings.  */
  532.  
  533. static int done_initializing = 0;
  534.  
  535. /* Line where a newline was first seen in a string constant.  */
  536.  
  537. static int multiline_string_line = 0;
  538.  
  539. /* I/O buffer structure.
  540.    The `fname' field is nonzero for source files and #include files
  541.    and for the dummy text used for -D and -U.
  542.    It is zero for rescanning results of macro expansion
  543.    and for expanding macro arguments.  */
  544. #define INPUT_STACK_MAX 400
  545. static struct file_buf {
  546.   char *fname;
  547.   /* Filename specified with #line command.  */
  548.   char *nominal_fname;
  549.   /* Record where in the search path this file was found.
  550.      For #include_next.  */
  551.   struct file_name_list *dir;
  552. #ifdef NEXT_CPP_SERVER
  553.   struct file_name_list *file; /* used by segregate_output */
  554.   U_CHAR *beg_of_line;           /* used by mark_expansions */
  555.   U_CHAR *output_written_to;
  556. #endif
  557.   int lineno;
  558.   int length;
  559.   U_CHAR *buf;
  560.   U_CHAR *bufp;
  561.   /* Macro that this level is the expansion of.
  562.      Included so that we can reenable the macro
  563.      at the end of this level.  */
  564.   struct hashnode *macro;
  565.   /* Value of if_stack at start of this file.
  566.      Used to prohibit unmatched #endif (etc) in an include file.  */
  567.   struct if_stack *if_stack;
  568.   /* Object to be freed at end of input at this level.  */
  569.   U_CHAR *free_ptr;
  570.   /* True if this is a header file included using <FILENAME>.  */
  571.   char system_header_p;
  572. } instack[INPUT_STACK_MAX];
  573.  
  574. static int last_error_tick;       /* Incremented each time we print it.  */
  575. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  576.  
  577. /* Current nesting level of input sources.
  578.    `instack[indepth]' is the level currently being read.  */
  579. static int indepth = -1;
  580. #define CHECK_DEPTH(code) \
  581.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  582.     {                                    \
  583.       error_with_line (line_for_error (instack[indepth].lineno),    \
  584.                "macro or `#include' recursion too deep");    \
  585.       code;                                \
  586.     }
  587.  
  588. /* Current depth in #include directives that use <...>.  */
  589. static int system_include_depth = 0;
  590.  
  591. typedef struct file_buf FILE_BUF;
  592.  
  593. /* The output buffer.  Its LENGTH field is the amount of room allocated
  594.    for the buffer, not the number of chars actually present.  To get
  595.    that, subtract outbuf.buf from outbuf.bufp. */
  596.  
  597. #ifdef NEXT_CPP_SERVER
  598. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  599. #else
  600. #define OUTBUF_SIZE 8192  /* initial size of output buffer */
  601. #endif
  602.  
  603. static FILE_BUF outbuf;
  604.  
  605. #ifdef NEXT_CPP_SERVER
  606.  
  607. #include <sys/dir.h>
  608. #include <string.h>
  609. #include <sys/fcntl.h>
  610.  
  611. /* Non-0 means separate the preprocessed module from the headers it imports.
  612.    This is a "poor man's" precompiled headers */
  613. static int segregate_output = 0;
  614.  
  615. /* Non-0 means output line/position markers before/after each expansion.
  616.    Plain old line commands were extended (see output_line_command) */
  617. static int mark_expansions = 0;
  618.  
  619. /* Non-zero means we are reading all further commands from stdin until a 
  620.    "terminate" command is received */
  621. static int serverized = 0;
  622.  
  623. /* Non-zero specifies that the time required to cpp is output.
  624.    Note: clients that turn this on should be prepared to handle the output,
  625.    if it is being parsed. */ 
  626. static int timings = 0;
  627.  
  628. /* Non-zero means we are serverized and have already output the stat
  629.    information for the module we are processing */
  630. static int outbuf_stat_info_recorded = 0;
  631.  
  632. static FILE_BUF sys_header_outbuf; /* shared, segregated buf for headers */
  633. static FILE_BUF user_header_outbuf;
  634.  
  635. static struct hashnode *free_undefined_macros = 0;
  636.  
  637. /* List of all input and output files (now that cpp can take a list of files)
  638.    This is built as files are processed */
  639. static struct file_name_list *all_input_files = 0;
  640. static struct file_name_list *all_output_files = 0;
  641.  
  642. static void add_to_negative_cache ();
  643. static int found_in_negative_cache ();
  644. static void add_version_of_header ();
  645. static int duplicate_version_of_header ();
  646. static int aggressive_skip_if_group ();
  647. static void dump_memory_statistics ();
  648. static void clear_import ();
  649. static void clear_all_macros();
  650. static void enable_macros_from_header();
  651.  
  652. static void init_output_file_buf();
  653.  
  654. /* for debugging */
  655. static void start();
  656. static void stop();
  657. static void display(FILE * strm, int addNewLine);
  658. #endif
  659.  
  660. /* Maintain and search list of included files, for #import.  */
  661.  
  662. #define IMPORT_HASH_SIZE 1403 /* was 31 */
  663.  
  664. struct import_file {
  665. #ifdef NEXT_CPP_SERVER
  666.   struct file_name_list *name;
  667.   int visited_within_module;    /* to support multiple modules */
  668.   time_t modtime;
  669. #else
  670.   char *name;
  671. #endif
  672.   ino_t inode;
  673.   dev_t dev;
  674.   struct import_file *next;
  675. };
  676.  
  677. /* Hash table of files already included with #include or #import.  */
  678.  
  679. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  680. static int import_hash ();
  681.  
  682. /* Grow output buffer OBUF points at
  683.    so it can hold at least NEEDED more chars.  */
  684.  
  685. #define check_expand(OBUF, NEEDED)  \
  686.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  687.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  688.  
  689. struct file_name_list
  690.   {
  691.     struct file_name_list *next;
  692.     char *fname;
  693.     /* If the following is nonzero, it is a macro name.
  694.        Don't include the file again if that macro is defined.  */
  695.     U_CHAR *control_macro;
  696.     /* If the following is nonzero, it is a C-language system include
  697.        directory.  */
  698.     int c_system_include_path;
  699.     /* Mapping of file names for this directory.  */
  700.     struct file_name_map *name_map;
  701.     /* Non-zero if name_map is valid.  */
  702.     int got_name_map;
  703. #ifdef NEXT_CPP_SERVER
  704.     /* If we are processing a list of modules, cache headers 
  705.        Normally, this buffer is freed in finclude  */
  706.     int length;
  707.     U_CHAR *buf;
  708.     /* If we are segregating headers, mark if already output */
  709.     int header_output;
  710.     int has_explicit_context_dependency;
  711.     int ever_included;         /* 0 means always #imported */
  712.     /* Keep a list of macros that are a part of this header */
  713.     int macro_count;
  714.     int macro_index;
  715.     struct hashnode **macro_list;
  716.     /* Keep various markers into the input buffer (to enable interesting
  717.        optimizations in skip_if_group when preprocessing several files). */
  718.     U_CHAR *position_after_last_include; 
  719.     U_CHAR *position_of_first_ifndef; /* if control_macro != 0 */
  720.     U_CHAR *position_of_last_endif;   /* if control_macro != 0 */
  721.     /* info to exclude duplicates in the output buffer (for perverse cases
  722.        like stdtypes.h...very rare). Offsets into the output buffer are
  723.        stored relative, since the buffer is often being realloced. */
  724.     unsigned int relative_position_of_file_start; /* into a header_outbuf */
  725.     unsigned int file_length; /* in bytes */
  726.     struct file_name_list *versions;  /* diff. mutations of the same file */
  727. #endif
  728.   };
  729.  
  730. /* #include "file" looks in source file dir, then stack. */
  731. /* #include <file> just looks in the stack. */
  732. /* cplusplus = 0 -> C.  cplusplus = 1 -> C++. cplusplus = 2 -> nothing */
  733. /* -I directories are added to the end, then the defaults are added. */
  734. /* The */
  735. static struct default_include {
  736.   char *fname;            /* The name of the directory.  */
  737.   int cplusplus;        /* Only look here if we're compiling C++.  */
  738.   int cxx_aware;        /* Includes in this directory don't need to
  739.                    be wrapped in extern "C" when compiling
  740.                    C++.  */
  741. } include_defaults_array[]
  742. #ifdef INCLUDE_DEFAULTS
  743.   = INCLUDE_DEFAULTS;
  744. #else
  745.   = {
  746.     /* Pick up GNU C++ specific include files.  */
  747.     { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
  748. #ifdef CROSS_COMPILE
  749.     /* This is the dir for fixincludes.  Put it just before
  750.        the files that we fix.  */
  751.     { GCC_INCLUDE_DIR, 0, 0 },
  752.     /* For cross-compilation, this dir name is generated
  753.        automatically in Makefile.in.  */
  754.     { CROSS_INCLUDE_DIR, 0, 0 },
  755.     /* This is another place that the target system's headers might be.  */
  756.     { TOOL_INCLUDE_DIR, 0, 1 },
  757. #else /* not CROSS_COMPILE */
  758.     /* This should be /usr/local/include and should come before
  759.        the fixincludes-fixed header files.  */
  760.     { LOCAL_INCLUDE_DIR, 0, 1 },
  761.     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
  762.        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
  763.     { TOOL_INCLUDE_DIR, 0, 1 },
  764.     /* This is the dir for fixincludes.  Put it just before
  765.        the files that we fix.  */
  766.     { GCC_INCLUDE_DIR, 0, 0 },
  767.     /* Some systems have an extra dir of include files.  */
  768. #ifdef SYSTEM_INCLUDE_DIR
  769.     { SYSTEM_INCLUDE_DIR, 0, 0 },
  770. #endif
  771.     { STANDARD_INCLUDE_DIR, 0, 0 },
  772. #endif /* not CROSS_COMPILE */
  773.     { 0, 0, 0 }
  774.     };
  775. #endif /* no INCLUDE_DEFAULTS */
  776. #ifdef NEXT_FRAMEWORKS
  777. static struct default_include framework_defaults_array [] = {
  778.     {"/LocalLibrary/Frameworks", 0, 0},
  779.     {"/NextLibrary/Frameworks", 0, 0}, 
  780.     {NULL, 0, 0}
  781.  };
  782.  
  783. static struct default_include *framework_defaults=framework_defaults_array;
  784. static struct file_name_list *framework = 0;
  785.  
  786. struct framework_header {char * dirName; int dirNameLen; };
  787. static struct framework_header framework_header_dirs[] = {
  788.    { "PrivateHeaders", 14 },
  789.    { "Headers", 7 },
  790.    { NULL, 0 }
  791. };
  792. #endif
  793. /* The code looks at the defaults through this pointer, rather than through
  794.    the constant structure above.  This pointer gets changed if an environment
  795.    variable specifies other defaults.  */
  796. static struct default_include *include_defaults = include_defaults_array;
  797.  
  798. static struct file_name_list *include = 0;    /* First dir to search */
  799.  
  800.     /* First dir to search for <file> */
  801. /* This is the first element to use for #include <...>.
  802.    If it is 0, use the entire chain for such includes.  */
  803. static struct file_name_list *first_bracket_include = 0;
  804. #ifdef NEXT_FRAMEWORKS
  805. static struct file_name_list *first_bracket_framework = 0;
  806. #endif
  807. /* This is the first element in the chain that corresponds to
  808.    a directory of system header files.  */
  809. static struct file_name_list *first_system_include = 0;
  810. static struct file_name_list *last_include = 0;    /* Last in chain */
  811. #ifdef NEXT_FRAMEWORKS
  812. static struct file_name_list *last_framework = 0; /* Last in chain */
  813. #endif
  814. /* Chain of include directories to put at the end of the other chain.  */
  815. static struct file_name_list *after_include = 0;
  816. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  817.  
  818. /* Chain to put at the start of the system include files.  */
  819. static struct file_name_list *before_system = 0;
  820. static struct file_name_list *last_before_system = 0;    /* Last in chain */
  821.  
  822. /* List of included files that contained #pragma once.  */
  823. static struct file_name_list *dont_repeat_files = 0;
  824.  
  825. /* List of other included files.
  826.    If ->control_macro if nonzero, the file had a #ifndef
  827.    around the entire contents, and ->control_macro gives the macro name.  */
  828. static struct file_name_list *all_include_files = 0;
  829.  
  830. /* Directory prefix that should replace `/usr' in the standard
  831.    include file directories.  */
  832. static char *include_prefix;
  833.  
  834. /* Global list of strings read in from precompiled files.  This list
  835.    is kept in the order the strings are read in, with new strings being
  836.    added at the end through stringlist_tailp.  We use this list to output
  837.    the strings at the end of the run. 
  838. */
  839. static STRINGDEF *stringlist;
  840. static STRINGDEF **stringlist_tailp = &stringlist;
  841.  
  842.  
  843. /* Structure returned by create_definition */
  844. typedef struct macrodef MACRODEF;
  845. struct macrodef
  846. {
  847.   struct definition *defn;
  848.   U_CHAR *symnam;
  849.   int symlen;
  850. };
  851.  
  852. static struct macrodef create_definition ();
  853.  
  854.  
  855. /* Structure allocated for every #define.  For a simple replacement
  856.    such as
  857.        #define foo bar ,
  858.    nargs = -1, the `pattern' list is null, and the expansion is just
  859.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  860.    e.g.,
  861.        #define getchar() getc (stdin) .
  862.    When there are args, the expansion is the replacement text with the
  863.    args squashed out, and the reflist is a list describing how to
  864.    build the output from the input: e.g., "3 chars, then the 1st arg,
  865.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  866.    The chars here come from the expansion.  Whatever is left of the
  867.    expansion after the last arg-occurrence is copied after that arg.
  868.    Note that the reflist can be arbitrarily long---
  869.    its length depends on the number of times the arguments appear in
  870.    the replacement text, not how many args there are.  Example:
  871.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  872.    pattern list
  873.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  874.    where (x, y) means (nchars, argno). */
  875.  
  876. typedef struct definition DEFINITION;
  877. struct definition {
  878.   int nargs;
  879.   int length;            /* length of expansion string */
  880.   int predefined;        /* True if the macro was builtin or */
  881.                 /* came from the command line */
  882.   U_CHAR *expansion;
  883.   int line;            /* Line number of definition */
  884.   char *file;            /* File of definition */
  885.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  886.   struct reflist {
  887.     struct reflist *next;
  888.     char stringify;        /* nonzero if this arg was preceded by a
  889.                    # operator. */
  890.     char raw_before;        /* Nonzero if a ## operator before arg. */
  891.     char raw_after;        /* Nonzero if a ## operator after arg. */
  892.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  893.     int nchars;            /* Number of literal chars to copy before
  894.                    this arg occurrence.  */
  895.     int argno;            /* Number of arg to substitute (origin-0) */
  896.   } *pattern;
  897.   union {
  898.     /* Names of macro args, concatenated in reverse order
  899.        with comma-space between them.
  900.        The only use of this is that we warn on redefinition
  901.        if this differs between the old and new definitions.  */
  902.     U_CHAR *argnames;
  903.   } args;
  904. };
  905.  
  906. /* different kinds of things that can appear in the value field
  907.    of a hash node.  Actually, this may be useless now. */
  908. union hashval {
  909.   int ival;
  910.   char *cpval;
  911.   DEFINITION *defn;
  912.   KEYDEF *keydef;
  913. };
  914.  
  915. /*
  916.  * special extension string that can be added to the last macro argument to 
  917.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  918.  *         #define wow(a, b...)        process (b, a, b)
  919.  *        { wow (1, 2, 3); }    ->    { process (2, 3, 1, 2, 3); }
  920.  *        { wow (one, two); }    ->    { process (two, one, two); }
  921.  * if this "rest_arg" is used with the concat token '##' and if it is not
  922.  * supplied then the token attached to with ## will not be outputted.  Ex:
  923.  *         #define wow (a, b...)        process (b ## , a, ## b)
  924.  *        { wow (1, 2); }        ->    { process (2, 1, 2); }
  925.  *        { wow (one); }        ->    { process (one); {
  926.  */
  927. static char rest_extension[] = "...";
  928. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  929.  
  930. /* The structure of a node in the hash table.  The hash table
  931.    has entries for all tokens defined by #define commands (type T_MACRO),
  932.    plus some special tokens like __LINE__ (these each have their own
  933.    type, and the appropriate code is run when that type of node is seen.
  934.    It does not contain control words like "#define", which are recognized
  935.    by a separate piece of code. */
  936.  
  937. /* different flavors of hash nodes --- also used in keyword table */
  938. enum node_type {
  939.  T_DEFINE = 1,    /* the `#define' keyword */
  940.  T_INCLUDE,    /* the `#include' keyword */
  941.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  942.  T_IMPORT,      /* the `#import' keyword */
  943.  T_IFDEF,    /* the `#ifdef' keyword */
  944.  T_IFNDEF,    /* the `#ifndef' keyword */
  945.  T_IF,        /* the `#if' keyword */
  946.  T_ELSE,    /* `#else' */
  947.  T_PRAGMA,    /* `#pragma' */
  948.  T_ELIF,    /* `#elif' */
  949.  T_UNDEF,    /* `#undef' */
  950.  T_LINE,    /* `#line' */
  951.  T_ERROR,    /* `#error' */
  952.  T_WARNING,    /* `#warning' */
  953.  T_ENDIF,    /* `#endif' */
  954.  T_SCCS,    /* `#sccs', used on system V.  */
  955.  T_IDENT,    /* `#ident', used on system V.  */
  956.  T_ASSERT,    /* `#assert', taken from system V.  */
  957.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  958.  T_SPECLINE,    /* special symbol `__LINE__' */
  959.  T_DATE,    /* `__DATE__' */
  960.  T_FILE,    /* `__FILE__' */
  961.  T_BASE_FILE,    /* `__BASE_FILE__' */
  962.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  963.  T_VERSION,    /* `__VERSION__' */
  964.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  965.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  966.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  967.  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
  968.  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
  969.  T_TIME,    /* `__TIME__' */
  970.  T_CONST,    /* Constant value, used by `__STDC__' */
  971.  T_MACRO,    /* macro defined by `#define' */
  972.  T_DISABLED,    /* macro temporarily turned off for rescan */
  973.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  974.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  975.  T_UNUSED    /* Used for something not defined.  */
  976.  };
  977.  
  978. struct hashnode {
  979.   struct hashnode *next;    /* double links for easy deletion */
  980.   struct hashnode *prev;
  981.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  982.                    chain is kept, in case the node is the head
  983.                    of the chain and gets deleted. */
  984.   enum node_type type;        /* type of special token */
  985.   int length;            /* length of token, for quick comparison */
  986.   U_CHAR *name;            /* the actual name */
  987.   union hashval value;        /* pointer to expansion, or whatever */
  988. #ifdef NEXT_CPP_SERVER
  989.   int defined_within_module;    /* is it currently on? (maybe should
  990.                                    just take a bit from the type field) */
  991.   struct file_name_list *owner; /* could have added this to "DEFINITION *",
  992.                                    however decided this was lower risk and
  993.                                    provided better locality (wrt the previous
  994.                                    field. Since hashnodes are primarily used
  995.                                    for macros, the space overhead is not
  996.                                    an issue */
  997. #endif
  998. };
  999.  
  1000. typedef struct hashnode HASHNODE;
  1001.  
  1002. /* Some definitions for the hash table.  The hash function MUST be
  1003.    computed as shown in hashf () below.  That is because the rescan
  1004.    loop computes the hash value `on the fly' for most tokens,
  1005.    in order to avoid the overhead of a lot of procedure calls to
  1006.    the hashf () function.  Hashf () only exists for the sake of
  1007.    politeness, for use when speed isn't so important. */
  1008.  
  1009. #define HASHSIZE 1403
  1010. static HASHNODE *hashtab[HASHSIZE];
  1011. #define HASHSTEP(old, c) ((old << 2) + c)
  1012. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  1013.  
  1014. /* Symbols to predefine.  */
  1015.  
  1016. #ifdef CPP_PREDEFINES
  1017. static char *predefs = CPP_PREDEFINES;
  1018. #else
  1019. static char *predefs = "";
  1020. #endif
  1021.  
  1022. /* We let tm.h override the types used here, to handle trivial differences
  1023.    such as the choice of unsigned int or long unsigned int for size_t.
  1024.    When machines start needing nontrivial differences in the size type,
  1025.    it would be best to do something here to figure out automatically
  1026.    from other information what type to use.  */
  1027.  
  1028. /* The string value for __SIZE_TYPE__.  */
  1029.  
  1030. #ifndef SIZE_TYPE
  1031. #define SIZE_TYPE "long unsigned int"
  1032. #endif
  1033.  
  1034. /* The string value for __PTRDIFF_TYPE__.  */
  1035.  
  1036. #ifndef PTRDIFF_TYPE
  1037. #define PTRDIFF_TYPE "long int"
  1038. #endif
  1039.  
  1040. /* The string value for __WCHAR_TYPE__.  */
  1041.  
  1042. #ifndef WCHAR_TYPE
  1043. #define WCHAR_TYPE "int"
  1044. #endif
  1045. char * wchar_type = WCHAR_TYPE;
  1046. #undef WCHAR_TYPE
  1047.  
  1048. /* The string value for __USER_LABEL_PREFIX__ */
  1049.  
  1050. #ifndef USER_LABEL_PREFIX
  1051. #define USER_LABEL_PREFIX ""
  1052. #endif
  1053.  
  1054. /* The string value for __REGISTER_PREFIX__ */
  1055.  
  1056. #ifndef REGISTER_PREFIX
  1057. #define REGISTER_PREFIX ""
  1058. #endif
  1059.  
  1060. /* In the definition of a #assert name, this structure forms
  1061.    a list of the individual values asserted.
  1062.    Each value is itself a list of "tokens".
  1063.    These are strings that are compared by name.  */
  1064.  
  1065. struct tokenlist_list {
  1066.   struct tokenlist_list *next;
  1067.   struct arglist *tokens;
  1068. };
  1069.  
  1070. struct assertion_hashnode {
  1071.   struct assertion_hashnode *next;    /* double links for easy deletion */
  1072.   struct assertion_hashnode *prev;
  1073.   /* also, a back pointer to this node's hash
  1074.      chain is kept, in case the node is the head
  1075.      of the chain and gets deleted. */
  1076.   struct assertion_hashnode **bucket_hdr;
  1077.   int length;            /* length of token, for quick comparison */
  1078.   U_CHAR *name;            /* the actual name */
  1079.   /* List of token-sequences.  */
  1080.   struct tokenlist_list *value;
  1081. };
  1082.  
  1083. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  1084.  
  1085. /* Some definitions for the hash table.  The hash function MUST be
  1086.    computed as shown in hashf below.  That is because the rescan
  1087.    loop computes the hash value `on the fly' for most tokens,
  1088.    in order to avoid the overhead of a lot of procedure calls to
  1089.    the hashf function.  hashf only exists for the sake of
  1090.    politeness, for use when speed isn't so important. */
  1091.  
  1092. #define ASSERTION_HASHSIZE 37
  1093. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  1094.  
  1095. /* Nonzero means inhibit macroexpansion of what seem to be
  1096.    assertion tests, in rescan.  For #if.  */
  1097. static int assertions_flag;
  1098.  
  1099. /* `struct directive' defines one #-directive, including how to handle it.  */
  1100.  
  1101. struct directive {
  1102.   int length;            /* Length of name */
  1103.   int (*func)();        /* Function to handle directive */
  1104.   char *name;            /* Name of directive */
  1105.   enum node_type type;        /* Code which describes which directive. */
  1106.   char angle_brackets;        /* Nonzero => <...> is special.  */
  1107.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  1108.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  1109. };
  1110.  
  1111. /* Here is the actual list of #-directives, most-often-used first.  */
  1112.  
  1113. static struct directive directive_table[] = {
  1114.   {  6, do_define, "define", T_DEFINE, 0, 1},
  1115.   {  2, do_if, "if", T_IF},
  1116.   {  5, do_xifdef, "ifdef", T_IFDEF},
  1117.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  1118.   {  5, do_endif, "endif", T_ENDIF},
  1119.   {  4, do_else, "else", T_ELSE},
  1120.   {  4, do_elif, "elif", T_ELIF},
  1121.   {  4, do_line, "line", T_LINE},
  1122.   {  7, do_include, "include", T_INCLUDE, 1},
  1123.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  1124.   {  6, do_include, "import", T_IMPORT, 1},
  1125.   {  5, do_undef, "undef", T_UNDEF},
  1126.   {  5, do_error, "error", T_ERROR},
  1127.   {  7, do_warning, "warning", T_WARNING},
  1128. #ifdef SCCS_DIRECTIVE
  1129.   {  4, do_sccs, "sccs", T_SCCS},
  1130. #endif
  1131.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  1132.   {  5, do_ident, "ident", T_IDENT},
  1133.   {  6, do_assert, "assert", T_ASSERT},
  1134.   {  8, do_unassert, "unassert", T_UNASSERT},
  1135.   {  -1, 0, "", T_UNUSED},
  1136. };
  1137.  
  1138. /* When a directive handler is called,
  1139.    this points to the # that started the directive.  */
  1140. U_CHAR *directive_start;
  1141.  
  1142. /* table to tell if char can be part of a C identifier. */
  1143. U_CHAR is_idchar[256];
  1144. /* table to tell if char can be first char of a c identifier. */
  1145. U_CHAR is_idstart[256];
  1146. /* table to tell if c is horizontal space.  */
  1147. U_CHAR is_hor_space[256];
  1148. /* table to tell if c is horizontal or vertical space.  */
  1149. static U_CHAR is_space[256];
  1150.  
  1151. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  1152. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  1153.   
  1154. static int errors = 0;            /* Error counter for exit code */
  1155.  
  1156. /* Name of output file, for error messages.  */
  1157. static char *out_fname;
  1158.  
  1159. /* Zero means dollar signs are punctuation.
  1160.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  1161.    This must be 0 for correct processing of this ANSI C program:
  1162.     #define foo(a) #a
  1163.     #define lose(b) foo (b)
  1164.     #define test$
  1165.     lose (test)    */
  1166. static int dollars_in_ident;
  1167. #ifndef DOLLARS_IN_IDENTIFIERS
  1168. #define DOLLARS_IN_IDENTIFIERS 1
  1169. #endif
  1170.  
  1171. static FILE_BUF expand_to_temp_buffer ();
  1172.  
  1173. static DEFINITION *collect_expansion ();
  1174.  
  1175. /* Stack of conditionals currently in progress
  1176.    (including both successful and failing conditionals).  */
  1177.  
  1178. struct if_stack {
  1179.   struct if_stack *next;    /* for chaining to the next stack frame */
  1180.   char *fname;        /* copied from input when frame is made */
  1181.   int lineno;            /* similarly */
  1182.   int if_succeeded;        /* true if a leg of this if-group
  1183.                     has been passed through rescan */
  1184.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  1185.                    this is the macro name tested.  */
  1186.   enum node_type type;        /* type of last directive seen in this group */
  1187. };
  1188. typedef struct if_stack IF_STACK_FRAME;
  1189. static IF_STACK_FRAME *if_stack = NULL;
  1190.  
  1191. /* Buffer of -M output.  */
  1192. static char *deps_buffer;
  1193.  
  1194. /* Number of bytes allocated in above.  */
  1195. static int deps_allocated_size;
  1196.  
  1197. /* Number of bytes used.  */
  1198. static int deps_size;
  1199.  
  1200. /* Number of bytes since the last newline.  */
  1201. static int deps_column;
  1202.  
  1203. /* Nonzero means -I- has been seen,
  1204.    so don't look for #include "foo" the source-file directory.  */
  1205. static int ignore_srcdir;
  1206.  
  1207. /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
  1208.    retrying if necessary.  Return a negative value if an error occurs,
  1209.    otherwise return the actual number of bytes read,
  1210.    which must be LEN unless end-of-file was reached.  */
  1211.  
  1212. static int
  1213. safe_read (desc, ptr, len)
  1214.      int desc;
  1215.      char *ptr;
  1216.      int len;
  1217. {
  1218.   int left = len;
  1219.   while (left > 0) {
  1220.     int nchars = read (desc, ptr, left);
  1221.     if (nchars < 0)
  1222.       {
  1223. #ifdef EINTR
  1224.     if (errno == EINTR)
  1225.       continue;
  1226. #endif
  1227.     return nchars;
  1228.       }
  1229.     if (nchars == 0)
  1230.       break;
  1231.     ptr += nchars;
  1232.     left -= nchars;
  1233.   }
  1234.   return len - left;
  1235. }
  1236.  
  1237. /* Write LEN bytes at PTR to descriptor DESC,
  1238.    retrying if necessary, and treating any real error as fatal.  */
  1239.  
  1240. static void
  1241. safe_write (desc, ptr, len)
  1242.      int desc;
  1243.      char *ptr;
  1244.      int len;
  1245. {
  1246.   while (len > 0) {
  1247.     int written = write (desc, ptr, len);
  1248.     if (written < 0)
  1249.       {
  1250. #ifdef EINTR
  1251.     if (errno == EINTR)
  1252.       continue;
  1253. #endif
  1254.     pfatal_with_name (out_fname);
  1255.       }
  1256.     ptr += written;
  1257.     len -= written;
  1258.   }
  1259. }
  1260.  
  1261. int
  1262. main (argc, argv)
  1263.      int argc;
  1264.      char **argv;
  1265. {
  1266.   int st_mode;
  1267.   long st_size;
  1268.   char *in_fname;
  1269.   char *p;
  1270.   int f, i;
  1271.   FILE_BUF *fp;
  1272.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  1273.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  1274.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  1275.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  1276.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  1277. #ifdef NEXT_CPP_SERVER
  1278.   #define MAX_INPUT_ARGS 128  
  1279.   #define MAX_INPUT_CHARS 4096
  1280.   char command[MAX_INPUT_CHARS+1];
  1281.   struct file_name_list *include_ptr; 
  1282.   int max_argc = argc;  /* keep track of largest argc seen */
  1283.   char * header_fname;
  1284.   char * requested_cwd;
  1285.   char actual_cwd[1024];
  1286. #endif
  1287.   /* Record the option used with each element of pend_assertions.
  1288.      This is preparation for supporting more than one option for making
  1289.      an assertion.  */
  1290.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  1291.   int inhibit_predefs = 0;
  1292.   int no_standard_includes = 0;
  1293.   int no_standard_cplusplus_includes = 0;
  1294.   int missing_newline = 0;
  1295.  
  1296.   /* Non-0 means don't output the preprocessed program.  */
  1297.   int inhibit_output = 0;
  1298.   /* Non-0 means -v, so print the full set of include dirs.  */
  1299.   int verbose = 0;
  1300.  
  1301.   /* File name which deps are being written to.
  1302.      This is 0 if deps are being written to stdout.  */
  1303.   char *deps_file = 0;
  1304.   /* Fopen file mode to open deps_file with.  */
  1305.   char *deps_mode = "a";
  1306.   /* Stream on which to print the dependency information.  */
  1307.   FILE *deps_stream = 0;
  1308.   /* Target-name to write with the dependency information.  */
  1309.   char *deps_target = 0;
  1310.  
  1311. #ifndef NeXT
  1312. #ifdef RLIMIT_STACK
  1313.   /* Get rid of any avoidable limit on stack size.  */
  1314.   {
  1315.     struct rlimit rlim;
  1316.  
  1317.     /* Set the stack limit huge so that alloca (particularly stringtab
  1318.      * in dbxread.c) does not fail. */
  1319.     getrlimit (RLIMIT_STACK, &rlim);
  1320.     rlim.rlim_cur = rlim.rlim_max;
  1321.     setrlimit (RLIMIT_STACK, &rlim);
  1322.   }
  1323. #endif /* RLIMIT_STACK defined */
  1324. #endif
  1325.  
  1326. #ifdef SIGPIPE
  1327.   signal (SIGPIPE, pipe_closed);
  1328. #endif
  1329.  
  1330.   p = argv[0] + strlen (argv[0]);
  1331.   while (p != argv[0] && p[-1] != '/') --p;
  1332.   progname = p;
  1333.  
  1334. #ifdef VMS
  1335.   {
  1336.     /* Remove directories from PROGNAME.  */
  1337.     char *s;
  1338.  
  1339.     progname = savestring (argv[0]);
  1340.  
  1341.     if (!(s = rindex (progname, ']')))
  1342.       s = rindex (progname, ':');
  1343.     if (s)
  1344.       strcpy (progname, s+1);
  1345.     if (s = rindex (progname, '.'))
  1346.       *s = '\0';
  1347.   }
  1348. #endif
  1349.  
  1350.   in_fname = NULL;
  1351.   out_fname = NULL;
  1352.  
  1353.   /* Initialize is_idchar to allow $.  */
  1354.   dollars_in_ident = 1;
  1355.   initialize_char_syntax ();
  1356.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1357.  
  1358.   no_line_commands = 0;
  1359.   no_trigraphs = 1;
  1360.   dump_macros = dump_none;
  1361.   no_output = 0;
  1362.   cplusplus = 0;
  1363. #ifdef NeXT
  1364.   cplusplus_comments = 1;
  1365. #else
  1366.   cplusplus_comments = 0;
  1367. #endif
  1368.  
  1369. #ifdef NEXT_CPP_SERVER
  1370. #define FAILURE                      \
  1371.         if (serverized)              \
  1372.        goto processCommand;      \
  1373.     else                         \
  1374.        return FAILURE_EXIT_CODE
  1375.  
  1376. #define BAILOUT(name) {              \
  1377.         if (serverized) {            \
  1378.            perror_with_name (name);  \
  1379.            goto finishRequest;       \
  1380.         } else {                     \
  1381.            pfatal_with_name (name);  \
  1382.         }                            \
  1383. }
  1384.            
  1385.   /* initialize output buffers */
  1386.   init_output_file_buf(&outbuf);
  1387.   init_output_file_buf(&sys_header_outbuf);
  1388.   init_output_file_buf(&user_header_outbuf);
  1389.  
  1390.   if (argc > 1 && !strcmp(argv[1],"-serverize")) {
  1391.      /* set up for serverized cpp semantics and looping on input commands */
  1392.      serverized = 1;
  1393.      segregate_output = 1;
  1394.      dump_macros = dump_definitions;
  1395.      /* mark_expansions = 1;   only adds about 4-5% to output text size */
  1396.      if (!getwd(actual_cwd))
  1397.         pfatal_with_name(actual_cwd);
  1398.      argv = (char **) xmalloc(sizeof(char *) * MAX_INPUT_ARGS);
  1399.      fprintf(stderr,"Starting serverized cpp process.  Enter commands:\n");
  1400.   }
  1401.  
  1402.   processCommand:
  1403.   while (serverized) {
  1404.      int preprocess = 0;
  1405.      char *tok;
  1406.  
  1407.      /* read a command off of stdin and point argv at it */
  1408.      if (!fgets(command, MAX_INPUT_CHARS, stdin)) {
  1409.         fprintf(stderr,"End of server commands.  Exiting...\n");
  1410. #ifdef PROFILE
  1411.         chdir(getenv("HOME"));
  1412. #endif        
  1413.         exit(FAILURE_EXIT_CODE);
  1414.      }
  1415.      argc = 0;
  1416.      for (tok = strtok(command," \t\n");
  1417.           tok && (argc < MAX_INPUT_ARGS);
  1418.           tok = strtok(NULL," \t\n")) 
  1419.         argv[argc++] = tok;
  1420.      if (!argc)
  1421.         goto processCommand;
  1422.      else if (argc > max_argc) {
  1423.         max_argc = argc;
  1424.         pend_files = (char **) xrealloc (pend_files, argc * sizeof (char *));
  1425.         pend_defs = (char **) xrealloc (pend_defs, argc * sizeof (char *));
  1426.         pend_undefs = (char **) xrealloc (pend_undefs, argc * sizeof (char *));
  1427.         pend_assertions = (char **) xrealloc (pend_assertions, 
  1428.                                                        argc * sizeof (char *));
  1429.         pend_includes = (char **) xrealloc (pend_includes, 
  1430.                                                        argc * sizeof (char *));
  1431.         pend_assertion_options = (char **) xrealloc (pend_assertion_options,
  1432.                                                        argc * sizeof (char *));       
  1433.      }
  1434.  
  1435.      switch (argv[0][0]) {
  1436.       case 'c':
  1437.         if (!strcmp(argv[0],"cpp")) 
  1438.            preprocess = 1; /* fall on through into body of main */
  1439.         break;
  1440.         
  1441.       case 'm':
  1442.         if (!strcmp(argv[0],"modtime"))
  1443.            if (argc > 2) {
  1444.               time_t lastModTime = file_modtime (argv[2]);
  1445.               fprintf(stderr, "modtime is %d\n");
  1446.            } else {
  1447.               fprintf(stderr,"bad modtime argument: <file name>\n");
  1448.            }
  1449.         break;
  1450.            
  1451.       case 'n':
  1452.         if (!strcmp(argv[0],"notice_file_modification"))
  1453.            if (argc > 1)
  1454.               handle_file_modification(argv[1]);
  1455.            else
  1456.               fprintf(stderr,
  1457.                       "bad notice_file_modification argument: <file name>\n");           
  1458.         break;
  1459.         
  1460.       case 's':
  1461.         if (!strcmp(argv[0],"stats"))
  1462.            dump_memory_statistics();
  1463.         break;
  1464.  
  1465.       case 't':
  1466.         if (!strcmp(argv[0],"terminate"))
  1467.            exit(0);
  1468.         else if (!strcmp(argv[0],"timings"))
  1469.            if ((argc > 1) && !strcmp(argv[1],"on"))
  1470.               timings = 1;
  1471.         break;
  1472.  
  1473.       case 'w':
  1474.         if (!strcmp(argv[0],"write_sys_headers")) {
  1475.            if (argc > 2) {
  1476.               int write_all = (strcmp(argv[1], "all") == 0) ? 1 : 0;
  1477.               out_fname = argv[2];
  1478.               write_header_output (out_fname, &sys_header_outbuf, write_all, 1);
  1479.            } else {
  1480.               fprintf(stderr,
  1481.                   "bad write_sys_headers arguments: <all|new> <file name>\n");
  1482.            }
  1483.         } else if (!strcmp(argv[0],"write_user_headers")) {
  1484.            if (argc > 1) {
  1485.               int write_all = (strcmp(argv[1], "all") == 0) ? 1 : 0;
  1486.               out_fname = argv[2];
  1487.               write_header_output (out_fname, &user_header_outbuf, write_all,1);
  1488.            } else {
  1489.               fprintf(stderr,
  1490.                 "bad write_user_headers arguments: <all|new> <file name>\n");
  1491.            }
  1492.         }
  1493.         break;
  1494.            
  1495.      } /* switch(argv[0][0]) */
  1496.      if (preprocess)
  1497.         break;  /* go do regular "main" logic on this preprocess request */
  1498.   }
  1499.   errors = 0;  /* in case last iteration caused errors */
  1500.   in_fname = out_fname = header_fname = requested_cwd = NULL;
  1501.   outbuf_stat_info_recorded = 0;
  1502. #endif
  1503.      
  1504.   bzero (pend_files, argc * sizeof (char *));
  1505.   bzero (pend_defs, argc * sizeof (char *));
  1506.   bzero (pend_undefs, argc * sizeof (char *));
  1507.   bzero (pend_assertions, argc * sizeof (char *));
  1508.   bzero (pend_includes, argc * sizeof (char *));
  1509.  
  1510.   /* Process switches and find input file name.  */
  1511.  
  1512.   for (i = 1; i < argc; i++) {
  1513.     if (argv[i][0] != '-') {
  1514.       if (out_fname != NULL)
  1515.     fatal ("Usage: %s [switches] input output", argv[0]);
  1516.       else if (in_fname != NULL)
  1517.     out_fname = argv[i];
  1518.       else
  1519.     in_fname = argv[i];
  1520.     } else {
  1521.       switch (argv[i][1]) {
  1522. #ifdef NEXT_SEMANTICS
  1523.       case 'a':
  1524.     if (!strcmp (argv[i], "-arch"))    /* ignore -arch <archname> */
  1525.       i++;
  1526.     break;
  1527.  
  1528.       case 'O':
  1529.     if (! strcmp (argv[i], "-ObjC++"))
  1530.       ; /* no-op */
  1531.     break;
  1532.         
  1533.       case 'c':
  1534.     if (! strcmp (argv[i], "-c++-comments"))
  1535.       cplusplus_comments = 1;
  1536.     break;
  1537. #endif
  1538.  
  1539.       case 'i':
  1540.     if (!strcmp (argv[i], "-include")) {
  1541.       if (i + 1 == argc)
  1542.         fatal ("Filename missing after `-include' option");
  1543.       else
  1544.         pend_includes[i] = argv[i+1], i++;
  1545.     }
  1546.     else if (!strcmp (argv[i], "-imacros")) {
  1547.       if (i + 1 == argc)
  1548.         fatal ("Filename missing after `-imacros' option");
  1549.       else
  1550.         pend_files[i] = argv[i+1], i++;
  1551.     }
  1552.     else if (!strcmp (argv[i], "-iprefix")) {
  1553.       if (i + 1 == argc)
  1554.         fatal ("Filename missing after `-iprefix' option");
  1555.       else
  1556.         include_prefix = argv[++i];
  1557.     }
  1558.     else if (!strcmp (argv[i], "-isystem")) {
  1559.       struct file_name_list *dirtmp;
  1560.  
  1561.       if (i + 1 == argc)
  1562.         fatal ("Filename missing after `-isystem' option");
  1563.  
  1564.       dirtmp = (struct file_name_list *)
  1565.         xmalloc (sizeof (struct file_name_list));
  1566.       dirtmp->next = 0;
  1567.       dirtmp->control_macro = 0;
  1568.       dirtmp->c_system_include_path = 1;
  1569.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
  1570.       strcpy (dirtmp->fname, argv[++i]);
  1571.       dirtmp->got_name_map = 0;
  1572.  
  1573.       if (before_system == 0)
  1574.         before_system = dirtmp;
  1575.       else
  1576.         last_before_system->next = dirtmp;
  1577.       last_before_system = dirtmp; /* Tail follows the last one */
  1578.     }
  1579.     /* Add directory to end of path for includes,
  1580.        with the default prefix at the front of its name.  */
  1581.     else if (!strcmp (argv[i], "-iwithprefix")) {
  1582.       struct file_name_list *dirtmp;
  1583.       char *prefix;
  1584.  
  1585.       if (include_prefix != 0)
  1586.         prefix = include_prefix;
  1587.       else {
  1588.         prefix = savestring (GCC_INCLUDE_DIR);
  1589.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1590.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1591.           prefix[strlen (prefix) - 7] = 0;
  1592.       }
  1593.  
  1594.       dirtmp = (struct file_name_list *)
  1595.         xmalloc (sizeof (struct file_name_list));
  1596.       dirtmp->next = 0;    /* New one goes on the end */
  1597.       dirtmp->control_macro = 0;
  1598.       dirtmp->c_system_include_path = 0;
  1599.       if (i + 1 == argc)
  1600.         fatal ("Directory name missing after `-iwithprefix' option");
  1601.  
  1602.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
  1603.                         + strlen (prefix) + 1);
  1604.       strcpy (dirtmp->fname, prefix);
  1605.       strcat (dirtmp->fname, argv[++i]);
  1606.       dirtmp->got_name_map = 0;
  1607.  
  1608.       if (after_include == 0)
  1609.         after_include = dirtmp;
  1610.       else
  1611.         last_after_include->next = dirtmp;
  1612.       last_after_include = dirtmp; /* Tail follows the last one */
  1613.     }
  1614.     /* Add directory to main path for includes,
  1615.        with the default prefix at the front of its name.  */
  1616.     else if (!strcmp (argv[i], "-iwithprefixbefore")) {
  1617.       struct file_name_list *dirtmp;
  1618.       char *prefix;
  1619.  
  1620.       if (include_prefix != 0)
  1621.         prefix = include_prefix;
  1622.       else {
  1623.         prefix = savestring (GCC_INCLUDE_DIR);
  1624.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1625.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1626.           prefix[strlen (prefix) - 7] = 0;
  1627.       }
  1628.  
  1629.       dirtmp = (struct file_name_list *)
  1630.         xmalloc (sizeof (struct file_name_list));
  1631.       dirtmp->next = 0;    /* New one goes on the end */
  1632.       dirtmp->control_macro = 0;
  1633.       dirtmp->c_system_include_path = 0;
  1634.       if (i + 1 == argc)
  1635.         fatal ("Directory name missing after `-iwithprefixbefore' option");
  1636.  
  1637.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
  1638.                         + strlen (prefix) + 1);
  1639.       strcpy (dirtmp->fname, prefix);
  1640.       strcat (dirtmp->fname, argv[++i]);
  1641.       dirtmp->got_name_map = 0;
  1642.  
  1643.       append_include_chain (dirtmp, dirtmp);
  1644.     }
  1645.     /* Add directory to end of path for includes.  */
  1646.     else if (!strcmp (argv[i], "-idirafter")) {
  1647.       struct file_name_list *dirtmp;
  1648.  
  1649.       dirtmp = (struct file_name_list *)
  1650.         xmalloc (sizeof (struct file_name_list));
  1651.       dirtmp->next = 0;    /* New one goes on the end */
  1652.       dirtmp->control_macro = 0;
  1653.       dirtmp->c_system_include_path = 0;
  1654.       if (i + 1 == argc)
  1655.         fatal ("Directory name missing after `-idirafter' option");
  1656.       else
  1657.         dirtmp->fname = argv[++i];
  1658.       dirtmp->got_name_map = 0;
  1659.  
  1660.       if (after_include == 0)
  1661.         after_include = dirtmp;
  1662.       else
  1663.         last_after_include->next = dirtmp;
  1664.       last_after_include = dirtmp; /* Tail follows the last one */
  1665.     }
  1666. #ifdef NEXT_CPP_SERVER
  1667.         /* To simulate a make process which cd's into sub-directories
  1668.          * in order to use makefiles local to that directory, we need to
  1669.          * support the notion of requesting a particular working directory
  1670.          * for a preprocessing command.
  1671.          */
  1672.         else if (!strcmp (argv[i], "-in_directory")) {
  1673.            if (i + 1 == argc)
  1674.               fatal ("Filename missing after -output_headers option");
  1675.            requested_cwd = argv[++i];
  1676.         }
  1677. #endif        
  1678. #ifdef NEXT_OBJC_RUNTIME
  1679.     else if (argv[i][2] != 0)
  1680.       pend_files[i] = argv[i] + 2;
  1681.     else if (i + 1 == argc)
  1682.       fatal ("Filename missing after -i option");
  1683.     else
  1684.       pend_files[i] = argv[i+1], i++;
  1685. #endif
  1686.     break;
  1687.  
  1688.       case 'o':
  1689. #ifdef NEXT_CPP_SERVER
  1690.         if (!strcmp (argv[i], "-output_headers")) {
  1691.            if (i + 1 == argc)
  1692.               fatal ("Filename missing after -output_headers option");
  1693.            header_fname = argv[++i];
  1694.         } else {
  1695.            if (out_fname != NULL)
  1696.               fatal ("Output filename specified twice");
  1697.            if (i + 1 == argc)
  1698.               fatal ("Filename missing after -o option");
  1699.            out_fname = argv[++i];
  1700.            if (!strcmp (out_fname, "-"))
  1701.               out_fname = "";
  1702.         }
  1703. #else        
  1704.     if (out_fname != NULL)
  1705.       fatal ("Output filename specified twice");
  1706.     if (i + 1 == argc)
  1707.       fatal ("Filename missing after -o option");
  1708.     out_fname = argv[++i];
  1709.     if (!strcmp (out_fname, "-"))
  1710.       out_fname = "";
  1711. #endif
  1712.         break;
  1713.  
  1714.       case 'p':
  1715.     if (!strcmp (argv[i], "-pedantic"))
  1716.       pedantic = 1;
  1717.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1718.       pedantic = 1;
  1719.       pedantic_errors = 1;
  1720.     } else if (!strcmp (argv[i], "-pcp")) {
  1721.       char *pcp_fname = argv[++i];
  1722.       pcp_outfile = 
  1723.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1724.          ? fopen (pcp_fname, "w")
  1725.          : fdopen (dup (fileno (stdout)), "w"));
  1726.       if (pcp_outfile == 0)
  1727. #ifdef NEXT_CPP_SERVER
  1728.              BAILOUT (pcp_fname);
  1729. #else             
  1730.              pfatal_with_name (pcp_fname);
  1731. #endif
  1732.       no_precomp = 1;
  1733.     }
  1734.     break;
  1735.  
  1736.       case 't':
  1737.     if (!strcmp (argv[i], "-traditional")) {
  1738.       traditional = 1;
  1739.       if (dollars_in_ident > 0)
  1740.         dollars_in_ident = 1;
  1741.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1742.       no_trigraphs = 0;
  1743.     }
  1744.     break;
  1745.  
  1746. #ifdef NEXT_SEMANTICS
  1747.       case 's':
  1748.     if (!strcmp (argv[i], "-smart"))
  1749.       ;
  1750.         else if (!strcmp (argv[i], "-segregate-output"))
  1751.           segregate_output = 1;
  1752.     break;
  1753.       case 'm':
  1754.     if (!strcmp (argv[i], "-mark-expansions"))
  1755.       mark_expansions = 1;
  1756.     break;
  1757. #endif
  1758.  
  1759.       case 'l':
  1760.         if (! strcmp (argv[i], "-lang-c"))
  1761. #ifdef NeXT
  1762.             cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1763. #else
  1764.             cplusplus = 0, cplusplus_comments = 0, objc = 0; 
  1765. #endif
  1766.     if (! strcmp (argv[i], "-lang-c++"))
  1767.       cplusplus = 1, cplusplus_comments = 1, objc = 0;
  1768.     if (! strcmp (argv[i], "-lang-c-c++-comments"))
  1769.       cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1770.     if (! strcmp (argv[i], "-lang-objc"))
  1771.       objc = 1, cplusplus_comments = 1, cplusplus = 0;
  1772.     if (! strcmp (argv[i], "-lang-objc++"))
  1773.       objc = 1, cplusplus_comments = 1, cplusplus = 1;
  1774.         if (! strcmp (argv[i], "-lang-asm"))
  1775.        lang_asm = 1;
  1776.      if (! strcmp (argv[i], "-lint"))
  1777.        for_lint = 1;
  1778.     break;
  1779.  
  1780.       case '+':
  1781.     cplusplus = 1, cplusplus_comments = 1;
  1782.     break;
  1783.  
  1784.       case 'w':
  1785.     inhibit_warnings = 1;
  1786.     break;
  1787.  
  1788.       case 'W':
  1789.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1790.       warn_trigraphs = 1;
  1791.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1792.       warn_trigraphs = 0;
  1793.     else if (!strcmp (argv[i], "-Wcomment"))
  1794.       warn_comments = 1;
  1795.     else if (!strcmp (argv[i], "-Wno-comment"))
  1796.       warn_comments = 0;
  1797.     else if (!strcmp (argv[i], "-Wcomments"))
  1798.       warn_comments = 1;
  1799.     else if (!strcmp (argv[i], "-Wno-comments"))
  1800.       warn_comments = 0;
  1801.     else if (!strcmp (argv[i], "-Wtraditional"))
  1802.       warn_stringify = 1;
  1803.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1804.       warn_stringify = 0;
  1805.     else if (!strcmp (argv[i], "-Wimport"))
  1806.       warn_import = 1;
  1807.     else if (!strcmp (argv[i], "-Wno-import"))
  1808.       warn_import = 0;
  1809.     else if (!strcmp (argv[i], "-Werror"))
  1810.       warnings_are_errors = 1;
  1811.     else if (!strcmp (argv[i], "-Wno-error"))
  1812.       warnings_are_errors = 0;
  1813.     else if (!strcmp (argv[i], "-Wall") || !strcmp (argv[i], "-Wmost"))
  1814.       {
  1815.         warn_trigraphs = 1;
  1816.         warn_comments = 1;
  1817.       }
  1818.     break;
  1819.  
  1820.       case 'M':
  1821.     /* The style of the choices here is a bit mixed.
  1822.        The chosen scheme is a hybrid of keeping all options in one string
  1823.        and specifying each option in a separate argument:
  1824.        -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
  1825.        -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
  1826.        -M[M][G][D file].  This is awkward to handle in specs, and is not
  1827.        as extensible.  */
  1828.     /* ??? -MG must be specified in addition to one of -M or -MM.
  1829.        This can be relaxed in the future without breaking anything.
  1830.        The converse isn't true.  */
  1831.  
  1832.     /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
  1833.     if (!strcmp (argv[i], "-MG"))
  1834.       {
  1835.         print_deps_missing_files = 1;
  1836.         break;
  1837.       }
  1838.     if (!strcmp (argv[i], "-M"))
  1839.       print_deps = 2;
  1840.     else if (!strcmp (argv[i], "-MM"))
  1841.       print_deps = 1;
  1842.     else if (!strcmp (argv[i], "-MD"))
  1843.       print_deps = 2;
  1844.     else if (!strcmp (argv[i], "-MMD"))
  1845.       print_deps = 1;
  1846.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1847.     if (!strcmp (argv[i], "-MD")
  1848.         || !strcmp (argv[i], "-MMD")) {
  1849.       i++;
  1850.       deps_file = argv[i];
  1851.       deps_mode = "w";
  1852.     } else {
  1853.       /* For -M and -MM, write deps on standard output
  1854.          and suppress the usual output.  */
  1855.       deps_stream = stdout;
  1856.       inhibit_output = 1;
  1857.     }      
  1858.     break;
  1859.  
  1860.       case 'd':
  1861.     {
  1862.       char *p = argv[i] + 2;
  1863.       char c;
  1864.       while (c = *p++) {
  1865.         /* Arg to -d specifies what parts of macros to dump */
  1866.         switch (c) {
  1867.         case 'M':
  1868.           dump_macros = dump_only;
  1869.           no_output = 1;
  1870.           break;
  1871.         case 'N':
  1872.           dump_macros = dump_names;
  1873.           break;
  1874.         case 'D':
  1875.           dump_macros = dump_definitions;
  1876.           break;
  1877.         }
  1878.       }
  1879.     }
  1880.     break;
  1881.  
  1882.       case 'g':
  1883.     if (argv[i][2] == '3')
  1884.       debug_output = 1;
  1885.     break;
  1886.  
  1887.       case 'v':
  1888.     fprintf (stderr, "GNU CPP version %s", "2.6.0");
  1889. #ifdef TARGET_VERSION
  1890.     TARGET_VERSION;
  1891. #endif
  1892.     fprintf (stderr, "\n");
  1893.     verbose = 1;
  1894.     break;
  1895.  
  1896.       case 'H':
  1897.     print_include_names = 1;
  1898.     break;
  1899.  
  1900.       case 'D':
  1901.     if (argv[i][2] != 0)
  1902.       pend_defs[i] = argv[i] + 2;
  1903.     else if (i + 1 == argc)
  1904.       fatal ("Macro name missing after -D option");
  1905.     else
  1906.       i++, pend_defs[i] = argv[i];
  1907.     break;
  1908.  
  1909.       case 'A':
  1910.     {
  1911.       char *p;
  1912.  
  1913.       if (argv[i][2] != 0)
  1914.         p = argv[i] + 2;
  1915.       else if (i + 1 == argc)
  1916.         fatal ("Assertion missing after -A option");
  1917.       else
  1918.         p = argv[++i];
  1919.  
  1920.       if (!strcmp (p, "-")) {
  1921.         /* -A- eliminates all predefined macros and assertions.
  1922.            Let's include also any that were specified earlier
  1923.            on the command line.  That way we can get rid of any
  1924.            that were passed automatically in from GCC.  */
  1925.         int j;
  1926.         inhibit_predefs = 1;
  1927.         for (j = 0; j < i; j++)
  1928.           pend_defs[j] = pend_assertions[j] = 0;
  1929.       } else {
  1930.         pend_assertions[i] = p;
  1931.         pend_assertion_options[i] = "-A";
  1932.       }
  1933.     }
  1934.     break;
  1935.  
  1936.       case 'U':        /* JF #undef something */
  1937.     if (argv[i][2] != 0)
  1938.       pend_undefs[i] = argv[i] + 2;
  1939.     else if (i + 1 == argc)
  1940.       fatal ("Macro name missing after -U option");
  1941.     else
  1942.       pend_undefs[i] = argv[i+1], i++;
  1943.     break;
  1944.  
  1945.       case 'C':
  1946.     put_out_comments = 1;
  1947.     break;
  1948.  
  1949.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1950.     break;
  1951.  
  1952.       case 'P':
  1953.     no_line_commands = 1;
  1954.     break;
  1955.  
  1956.       case '$':            /* Don't include $ in identifiers.  */
  1957.     dollars_in_ident = 0;
  1958.     break;
  1959.  
  1960.       case 'I':            /* Add directory to path for includes.  */
  1961.     {
  1962.       struct file_name_list *dirtmp;
  1963.  
  1964.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1965.         ignore_srcdir = 1;
  1966.         /* Don't use any preceding -I directories for #include <...>.  */
  1967.         first_bracket_include = 0;
  1968.       }
  1969.       else {
  1970.         dirtmp = (struct file_name_list *)
  1971.           xmalloc (sizeof (struct file_name_list));
  1972.         dirtmp->next = 0;        /* New one goes on the end */
  1973.         dirtmp->control_macro = 0;
  1974.         dirtmp->c_system_include_path = 0;
  1975.         if (argv[i][2] != 0)
  1976.           dirtmp->fname = argv[i] + 2;
  1977.         else if (i + 1 == argc)
  1978.           fatal ("Directory name missing after -I option");
  1979.         else
  1980.           dirtmp->fname = argv[++i];
  1981.         dirtmp->got_name_map = 0;
  1982.         append_include_chain (dirtmp, dirtmp);
  1983.       }
  1984.     }
  1985.     break;
  1986. #ifdef NEXT_FRAMEWORKS
  1987.   case 'F':
  1988.   {
  1989.       struct file_name_list *dirtmp;
  1990.  
  1991.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1992.           ignore_srcdir = 1;
  1993.           /* Don't use any preceding -F directories for #include <...>.  */
  1994.           first_bracket_framework = 0;
  1995.       }
  1996.       else {
  1997.           dirtmp = (struct file_name_list *)
  1998.               xmalloc (sizeof (struct file_name_list));
  1999.           dirtmp->next = 0;        /* New one goes on the end */
  2000.           dirtmp->control_macro = 0;
  2001.           dirtmp->c_system_include_path = 0;
  2002.           if (argv[i][2] != 0)
  2003.               dirtmp->fname = argv[i] + 2;
  2004.           else if (i + 1 == argc)
  2005.               fatal ("Directory name missing after -I option");
  2006.           else
  2007.               dirtmp->fname = argv[++i];
  2008.           dirtmp->got_name_map = 0;
  2009.           append_framework_chain (dirtmp, dirtmp);
  2010.       }
  2011.   }
  2012.     break;
  2013. #endif
  2014.   case 'n':
  2015.     if (!strcmp (argv[i], "-nostdinc"))
  2016.       /* -nostdinc causes no default include directories.
  2017.          You must specify all include-file directories with -I.  */
  2018.       no_standard_includes = 1;
  2019.     else if (!strcmp (argv[i], "-nostdinc++"))
  2020.       /* -nostdinc++ causes no default C++-specific include directories. */
  2021.       no_standard_cplusplus_includes = 1;
  2022.     else if (!strcmp (argv[i], "-noprecomp"))
  2023.       no_precomp = 1;
  2024.     if (!strcmp (argv[i], "-no-c++-comments"))
  2025.       cplusplus_comments = 0;
  2026.     break;
  2027.  
  2028.     break;
  2029.  
  2030.       case 'u':
  2031.     /* Sun compiler passes undocumented switch "-undef".
  2032.        Let's assume it means to inhibit the predefined symbols.  */
  2033.     inhibit_predefs = 1;
  2034.     break;
  2035.  
  2036.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  2037.     if (in_fname == NULL) {
  2038.       in_fname = "";
  2039.       break;
  2040.     } else if (out_fname == NULL) {
  2041.       out_fname = "";
  2042.       break;
  2043.     }    /* else fall through into error */
  2044.  
  2045.       default:
  2046.     fatal ("Invalid option `%s'", argv[i]);
  2047.       }
  2048.     }
  2049.   }
  2050.  
  2051. #ifdef NEXT_CPP_SERVER
  2052.   if (serverized && requested_cwd && strcmp(actual_cwd, requested_cwd) != 0) {
  2053.      if (chdir(requested_cwd) == 0)
  2054.         strcpy(actual_cwd, requested_cwd);
  2055.   }
  2056. #endif
  2057.  
  2058.   /* Add dirs from CPATH after dirs from -I.  */
  2059.   /* There seems to be confusion about what CPATH should do,
  2060.      so for the moment it is not documented.  */
  2061.   /* Some people say that CPATH should replace the standard include dirs,
  2062.      but that seems pointless: it comes before them, so it overrides them
  2063.      anyway.  */
  2064.   p = (char *) getenv ("CPATH");
  2065.   if (p != 0 && ! no_standard_includes)
  2066.     path_include (p);
  2067.  
  2068.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  2069.   initialize_char_syntax ();
  2070.  
  2071. #ifndef NEXT_CPP_SERVER
  2072.   /* Initialize output buffer */
  2073.  
  2074.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  2075.   outbuf.bufp = outbuf.buf;
  2076.   outbuf.length = OUTBUF_SIZE;
  2077. #else
  2078.   if (timings)
  2079.      start();
  2080. #endif
  2081.   /* Do partial setup of input buffer for the sake of generating
  2082.      early #line directives (when -g is in effect).  */
  2083.  
  2084.   fp = &instack[++indepth];
  2085.   if (in_fname == NULL)
  2086.     in_fname = "";
  2087.   fp->nominal_fname = fp->fname = in_fname;
  2088.   fp->lineno = 0;
  2089.  
  2090.   /* In C++, wchar_t is a distinct basic type, and we can expect
  2091.      __wchar_t to be defined by cc1plus.  */
  2092.   if (cplusplus)
  2093.     wchar_type = "__wchar_t";
  2094.  
  2095.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  2096.      and option processing.  */
  2097.   initialize_builtins (fp, &outbuf); /* FIXME: does this malloc every time? */
  2098.  
  2099.   /* Do standard #defines and assertions
  2100.      that identify system and machine type.  */
  2101.  
  2102.   /* Normally, the compiler driver passes -undef, which causes this 
  2103.      clause to be skipped (i.e. inhibit_predefs == 1). If being run
  2104.      standalone, this clause is responsible for installing the predefined
  2105.      macros. At present, this cpp has this info compiled into it, rather
  2106.      than reading a specs file for each architecture (which is what the NeXT 
  2107.      compiler driver does) */
  2108.  
  2109.   if (!inhibit_predefs) {
  2110.     char *p = (char *) alloca (strlen (predefs) + 1);
  2111.     strcpy (p, predefs);
  2112.     while (*p) {
  2113.       char *q;
  2114.       while (*p == ' ' || *p == '\t')
  2115.     p++;
  2116.       /* Handle -D options.  */ 
  2117.       if (p[0] == '-' && p[1] == 'D') {
  2118.     q = &p[2];
  2119.     while (*p && *p != ' ' && *p != '\t')
  2120.       p++;
  2121.     if (*p != 0)
  2122.       *p++= 0;
  2123.     if (debug_output)
  2124.       output_line_command (fp, &outbuf, 0, same_file);
  2125.     make_definition (q, &outbuf);
  2126.     while (*p == ' ' || *p == '\t')
  2127.       p++;
  2128.       } else if (p[0] == '-' && p[1] == 'A') {
  2129.     /* Handle -A options (assertions).  */ 
  2130.     char *assertion;
  2131.     char *past_name;
  2132.     char *value;
  2133.     char *past_value;
  2134.     char *termination;
  2135.     int save_char;
  2136.  
  2137.     assertion = &p[2];
  2138.     past_name = assertion;
  2139.     /* Locate end of name.  */
  2140.     while (*past_name && *past_name != ' '
  2141.            && *past_name != '\t' && *past_name != '(')
  2142.       past_name++;
  2143.     /* Locate `(' at start of value.  */
  2144.     value = past_name;
  2145.     while (*value && (*value == ' ' || *value == '\t'))
  2146.       value++;
  2147.     if (*value++ != '(')
  2148.       abort ();
  2149.     while (*value && (*value == ' ' || *value == '\t'))
  2150.       value++;
  2151.     past_value = value;
  2152.     /* Locate end of value.  */
  2153.     while (*past_value && *past_value != ' '
  2154.            && *past_value != '\t' && *past_value != ')')
  2155.       past_value++;
  2156.     termination = past_value;
  2157.     while (*termination && (*termination == ' ' || *termination == '\t'))
  2158.       termination++;
  2159.     if (*termination++ != ')')
  2160.       abort ();
  2161.     if (*termination && *termination != ' ' && *termination != '\t')
  2162.       abort ();
  2163.     /* Temporarily null-terminate the value.  */
  2164.     save_char = *termination;
  2165.     *termination = '\0';
  2166.     /* Install the assertion.  */
  2167.     make_assertion ("-A", assertion);
  2168.     *termination = (char) save_char;
  2169.     p = termination;
  2170.     while (*p == ' ' || *p == '\t')
  2171.       p++;
  2172.       } else {
  2173.     abort ();
  2174.       }
  2175.     }
  2176. /* FIXME */
  2177.     /* snaroff: this code must be removed. It is here as a reminder that
  2178.        whatever machinations the driver does must be mimiced in the preceeding 
  2179.        loop (if we want the behavior to be identical) */
  2180.     make_definition ("__unix__", &outbuf);
  2181.     make_definition ("__NeXT__", &outbuf);
  2182.     make_definition ("__mc68000__", &outbuf);
  2183.     make_definition ("__m68k__", &outbuf);
  2184.     make_definition ("_NEXT_SOURCE", &outbuf);
  2185.   }
  2186.  
  2187.   /* Now handle the command line options.  */
  2188.  
  2189.   /* Do -U's, -D's and -A's in the order they were seen.  */
  2190.   for (i = 1; i < argc; i++) {
  2191.     if (pend_undefs[i]) {
  2192.       if (debug_output)
  2193.         output_line_command (fp, &outbuf, 0, same_file);
  2194.       make_undef (pend_undefs[i], &outbuf);
  2195.     }
  2196.     if (pend_defs[i]) {
  2197.       if (debug_output)
  2198.         output_line_command (fp, &outbuf, 0, same_file);
  2199.       make_definition (pend_defs[i], &outbuf);
  2200.     }
  2201.     if (pend_assertions[i])
  2202.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  2203.   }
  2204.  
  2205.   done_initializing = 1;
  2206.  
  2207.   { /* read the appropriate environment variable and if it exists
  2208.        replace include_defaults with the listed path. */
  2209.     char *epath = 0;
  2210.     switch ((objc << 1) + cplusplus)
  2211.       {
  2212.       case 0:
  2213.     epath = getenv ("C_INCLUDE_PATH");
  2214.     break;
  2215.       case 1:
  2216.     epath = getenv ("CPLUS_INCLUDE_PATH");
  2217.     break;
  2218.       case 2:
  2219.     epath = getenv ("OBJC_INCLUDE_PATH");
  2220.     break;
  2221.       case 3:
  2222.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  2223.     break;
  2224.       }
  2225.     /* If the environment var for this language is set,
  2226.        add to the default list of include directories.  */
  2227.     if (epath) {
  2228.       char *nstore = (char *) alloca (strlen (epath) + 2);
  2229.       int num_dirs;
  2230.       char *startp, *endp;
  2231.  
  2232.       for (num_dirs = 1, startp = epath; *startp; startp++)
  2233.     if (*startp == PATH_SEPARATOR)
  2234.       num_dirs++;
  2235.       include_defaults
  2236.     = (struct default_include *) xmalloc ((num_dirs
  2237.                            * sizeof (struct default_include))
  2238.                           + sizeof (include_defaults_array));
  2239.       startp = endp = epath;
  2240.       num_dirs = 0;
  2241.       while (1) {
  2242.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  2243.         if ((*endp == PATH_SEPARATOR
  2244. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  2245. #ifdef __MSDOS__
  2246.          && (endp-startp != 1 || !isalpha (*startp))
  2247. #endif
  2248. #endif
  2249.          )
  2250.             || *endp == 0) {
  2251.       strncpy (nstore, startp, endp-startp);
  2252.       if (endp == startp)
  2253.         strcpy (nstore, ".");
  2254.       else
  2255.         nstore[endp-startp] = '\0';
  2256.  
  2257.       include_defaults[num_dirs].fname = savestring (nstore);
  2258.       include_defaults[num_dirs].cplusplus = cplusplus;
  2259.       include_defaults[num_dirs].cxx_aware = 1;
  2260.       num_dirs++;
  2261.       if (*endp == '\0')
  2262.         break;
  2263.       endp = startp = endp + 1;
  2264.     } else
  2265.       endp++;
  2266.       }
  2267.       /* Put the usual defaults back in at the end.  */
  2268.       bcopy ((char *) include_defaults_array,
  2269.          (char *) &include_defaults[num_dirs],
  2270.          sizeof (include_defaults_array));
  2271.     }
  2272.   }
  2273.   append_include_chain (before_system, last_before_system);
  2274.   first_system_include = before_system;
  2275.  
  2276.   /* Unless -fnostdinc,
  2277.      tack on the standard include file dirs to the specified list */
  2278.   if (!no_standard_includes) {
  2279.     int hdr;
  2280.     struct default_include *p = include_defaults;
  2281.     char *specd_prefix = include_prefix;
  2282.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  2283.     int default_len = 0;
  2284.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  2285.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  2286.       default_len = strlen (default_prefix) - 7;
  2287.       default_prefix[default_len] = 0;
  2288.     }
  2289.     /* Search "translated" versions of GNU directories.
  2290.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  2291.     if (specd_prefix != 0 && default_len != 0)
  2292.       for (p = include_defaults; p->fname; p++) {
  2293.     /* Some standard dirs are only for C++.  */
  2294.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2295.       /* Does this dir start with the prefix?  */
  2296.       if (!strncmp (p->fname, default_prefix, default_len)) {
  2297.         /* Yes; change prefix and add to search list.  */
  2298.         struct file_name_list *new
  2299.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2300.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  2301.         char *str = (char *) xmalloc (this_len + 1);
  2302.         strcpy (str, specd_prefix);
  2303.         strcat (str, p->fname + default_len);
  2304.         new->fname = str;
  2305.         new->control_macro = 0;
  2306.         new->c_system_include_path = !p->cxx_aware;
  2307.         new->got_name_map = 0;
  2308.         append_include_chain (new, new);
  2309.         if (first_system_include == 0)
  2310.           first_system_include = new;
  2311.       }
  2312.     }
  2313.       }
  2314.     /* Search ordinary names for GNU include directories.  */
  2315.     for (p = include_defaults; p->fname; p++) {
  2316.       /* Some standard dirs are only for C++.  */
  2317.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2318.     struct file_name_list *new
  2319.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2320.     new->control_macro = 0;
  2321.     new->c_system_include_path = !p->cxx_aware;
  2322.     new->fname = p->fname;
  2323.     new->got_name_map = 0;
  2324.     append_include_chain (new, new);
  2325.  
  2326.     if (first_system_include == 0)
  2327.       first_system_include = new;
  2328.       }
  2329.     }
  2330. #ifdef NEXT_FRAMEWORKS
  2331.     for (hdr=0; framework_defaults_array[hdr].fname; hdr++) {
  2332.        struct stat dummy;
  2333.        if (stat(framework_defaults_array[hdr].fname, &dummy) == 0) {
  2334.           // frmaework directory actually exists
  2335.           struct file_name_list *new
  2336.              = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2337.           new->control_macro = 0;
  2338.           new->c_system_include_path = !framework_defaults_array[hdr].cxx_aware;
  2339.           new->fname = framework_defaults_array[hdr].fname;
  2340.           new->got_name_map = 0;
  2341.           append_framework_chain (new, new);
  2342.        }
  2343.     }
  2344.     free (default_prefix);
  2345. #endif
  2346.   }
  2347.  
  2348.   /* Tack the after_include chain at the end of the include chain.  */
  2349.   append_include_chain (after_include, last_after_include);
  2350.   if (first_system_include == 0)
  2351.     first_system_include = after_include;
  2352.  
  2353.   /* With -v, print the list of dirs to search.  */
  2354.   if (verbose) {
  2355.     struct file_name_list *p;
  2356.     fprintf (stderr, "#include \"...\" search starts here:\n");
  2357.     for (p = include; p; p = p->next) {
  2358.       if (p == first_bracket_include)
  2359.     fprintf (stderr, "#include <...> search starts here:\n");
  2360.       fprintf (stderr, " %s\n", p->fname);
  2361.     }
  2362.     fprintf (stderr, "End of search list.\n");
  2363.   }
  2364.  
  2365.   /* Scan the -imacros files before the main input.
  2366.      Much like #including them, but with no_output set
  2367.      so that only their macro definitions matter.  */
  2368.  
  2369.   no_output++; no_record_file++;
  2370.   for (i = 1; i < argc; i++)
  2371.     if (pend_files[i]) {
  2372.       int fd = open (pend_files[i], O_RDONLY, 0666);
  2373.       if (fd < 0) {
  2374.     perror_with_name (pend_files[i]);
  2375. #ifdef NEXT_CPP_SERVER
  2376.         FAILURE;
  2377. #else
  2378.     return FAILURE_EXIT_CODE;
  2379. #endif    
  2380.       }
  2381. #ifdef NEXT_CPP_SERVER
  2382.       finclude (fd, pend_files[i], &outbuf, 0, 0, 0, 0);
  2383. #else
  2384.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  2385. #endif
  2386.     }
  2387.   no_output--; no_record_file--;
  2388.  
  2389.   /* Copy the entire contents of the main input file into
  2390.      the stacked input buffer previously allocated for it.  */
  2391.  
  2392.   /* JF check for stdin */
  2393.   if (in_fname == NULL || *in_fname == 0) {
  2394.     in_fname = "";
  2395.     f = 0;
  2396.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  2397. #ifdef NEXT_CPP_SERVER
  2398.     BAILOUT (in_fname);
  2399. #else
  2400.     goto perror;
  2401. #endif
  2402.  
  2403.   /* -MG doesn't select the form of output and must be specified with one of
  2404.      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
  2405.      inhibit compilation.  */
  2406.   if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
  2407.     fatal ("-MG must be specified with one of -M or -MM");
  2408.  
  2409.   /* Either of two environment variables can specify output of deps.
  2410.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  2411.      where OUTPUT_FILE is the file to write deps info to
  2412.      and DEPS_TARGET is the target to mention in the deps.  */
  2413.  
  2414.   if (print_deps == 0
  2415.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  2416.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  2417.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  2418.     char *s;
  2419.     char *output_file;
  2420.  
  2421.     if (spec == 0) {
  2422.       spec = getenv ("SUNPRO_DEPENDENCIES");
  2423.       print_deps = 2;
  2424.     }
  2425.     else
  2426.       print_deps = 1;
  2427.  
  2428.     s = spec;
  2429.     /* Find the space before the DEPS_TARGET, if there is one.  */
  2430.     /* This should use index.  (mrs) */
  2431.     while (*s != 0 && *s != ' ') s++;
  2432.     if (*s != 0) {
  2433.       deps_target = s + 1;
  2434.       output_file = (char *) xmalloc (s - spec + 1);
  2435.       bcopy (spec, output_file, s - spec);
  2436.       output_file[s - spec] = 0;
  2437.     }
  2438.     else {
  2439.       deps_target = 0;
  2440.       output_file = spec;
  2441.     }
  2442.       
  2443.     deps_file = output_file;
  2444.     deps_mode = "a";
  2445.   }
  2446.  
  2447.   /* For -M, print the expected object file name
  2448.      as the target of this Make-rule.  */
  2449.   if (print_deps) {
  2450.     deps_allocated_size = 200;
  2451.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  2452.     deps_buffer[0] = 0;
  2453.     deps_size = 0;
  2454.     deps_column = 0;
  2455.  
  2456.     if (deps_target) {
  2457.       deps_output (deps_target, ':');
  2458.     } else if (*in_fname == 0) {
  2459.       deps_output ("-", ':');
  2460.     } else {
  2461.       char *p, *q;
  2462.       int len;
  2463.  
  2464.       /* Discard all directory prefixes from filename.  */
  2465.       if ((q = rindex (in_fname, '/')) != NULL)
  2466.     ++q;
  2467.       else
  2468.     q = in_fname;
  2469.  
  2470.       /* Copy remainder to mungable area.  */
  2471.       p = (char *) alloca (strlen(q) + 8);
  2472.       strcpy (p, q);
  2473.  
  2474.       /* Output P, but remove known suffixes.  */
  2475.       len = strlen (p);
  2476.       q = p + len;
  2477.       if (len >= 2
  2478.       && p[len - 2] == '.'
  2479.       && index("cCsSmM", p[len - 1]))
  2480.     q = p + (len - 2);
  2481.       else if (len >= 3
  2482.            && p[len - 3] == '.'
  2483.            && p[len - 2] == 'c'
  2484.            && p[len - 1] == 'c')
  2485.     q = p + (len - 3);
  2486.       else if (len >= 4
  2487.            && p[len - 4] == '.'
  2488.            && p[len - 3] == 'c'
  2489.            && p[len - 2] == 'x'
  2490.            && p[len - 1] == 'x')
  2491.     q = p + (len - 4);
  2492.       else if (len >= 4
  2493.            && p[len - 4] == '.'
  2494.            && p[len - 3] == 'c'
  2495.            && p[len - 2] == 'p'
  2496.            && p[len - 1] == 'p')
  2497.     q = p + (len - 4);
  2498.       else if (len >= 3
  2499.            && p[len - 3] == '.'
  2500.            && p[len - 2] == 'm'
  2501.            && p[len - 1] == 'm')
  2502.     q = p + (len - 3);
  2503.  
  2504.       /* Supply our own suffix.  */
  2505. #ifndef VMS
  2506.       strcpy (q, ".o");
  2507. #else
  2508.       strcpy (q, ".obj");
  2509. #endif
  2510.  
  2511.       deps_output (p, ':');
  2512.       deps_output (in_fname, ' ');
  2513.     }
  2514.   }
  2515.  
  2516.   file_size_and_mode (f, &st_mode, &st_size);
  2517.   fp->nominal_fname = fp->fname = in_fname;
  2518.   fp->lineno = 1;
  2519.   fp->system_header_p = 0;
  2520.   /* JF all this is mine about reading pipes and ttys */
  2521.   if (! S_ISREG (st_mode)) {
  2522.     /* Read input from a file that is not a normal disk file.
  2523.        We cannot preallocate a buffer with the correct size,
  2524.        so we must read in the file a piece at the time and make it bigger.  */
  2525.     int size;
  2526.     int bsize;
  2527.     int cnt;
  2528.  
  2529.     bsize = 2000;
  2530.     size = 0;
  2531.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  2532.     for (;;) {
  2533. #ifdef NEXT_CPP_SERVER
  2534.       cnt = safe_read (f, fp->buf + size, bsize - size);
  2535.       if (cnt < 0) BAILOUT (in_fname);
  2536. #else
  2537.       cnt = safe_read (f, bufp, bsize - size);
  2538.       if (cnt < 0) goto perror;    /* error! */
  2539. #endif      
  2540.       if (cnt == 0) break;    /* End of file */
  2541.       size += cnt;
  2542.       if (size != bsize) break;    /* End of file */
  2543.       bsize *= 2;
  2544.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  2545.     }
  2546.     fp->length = size;
  2547.   } else {
  2548.     /* Read a file whose size we can determine in advance.
  2549.        For the sake of VMS, st_size is just an upper bound.  */
  2550.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  2551.     
  2552.     fp->length = safe_read (f, fp->buf, st_size);
  2553.     if (fp->length < 0) 
  2554. #ifdef NEXT_CPP_SERVER
  2555.         BAILOUT (in_fname);
  2556. #else
  2557.       goto perror;
  2558. #endif
  2559.   }
  2560.   fp->bufp = fp->buf;
  2561.   fp->if_stack = if_stack;
  2562.  
  2563.   /* Make sure data ends with a newline.  And put a null after it.  */
  2564.  
  2565.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  2566.       /* Backslash-newline at end is not good enough.  */
  2567.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  2568.     fp->buf[fp->length++] = '\n';
  2569.     missing_newline = 1;
  2570.   }
  2571.   fp->buf[fp->length] = '\0';
  2572.  
  2573.   /* Unless inhibited, convert trigraphs in the input.  */
  2574.  
  2575.   if (!no_trigraphs)
  2576.     trigraph_pcp (fp);
  2577.  
  2578. #ifdef NEXT_SEMANTICS
  2579.   if (!no_rtf)
  2580.     buf_convert_rtf (fp);
  2581. #endif
  2582.  
  2583.   /* Now that we know the input file is valid, open the output.  */
  2584.  
  2585. #ifdef NEXT_CPP_SERVER
  2586.   if (!out_fname || !strcmp (out_fname, "") || !strcmp (out_fname, "stdout"))
  2587.     if (serverized)
  2588.        fatal("Can't output to stdout when serverized.");
  2589.     else
  2590.        out_fname = "stdout";     
  2591. #else
  2592.   if (!out_fname || !strcmp (out_fname, ""))
  2593.     out_fname = "stdout";
  2594.   else if (! freopen (out_fname, "w", stdout))
  2595.     pfatal_with_name (out_fname);
  2596. #endif
  2597.  
  2598.   output_line_command (fp, &outbuf, 0, same_file);
  2599.  
  2600.   /* Scan the -include files before the main input.  */
  2601.  
  2602.   no_record_file++;
  2603.   for (i = 1; i < argc; i++)
  2604.     if (pend_includes[i]) {
  2605.       int fd = open (pend_includes[i], O_RDONLY, 0666);
  2606.       if (fd < 0) {
  2607.     perror_with_name (pend_includes[i]);
  2608. #ifdef NEXT_CPP_SERVER
  2609.         FAILURE;
  2610. #else
  2611.     return FAILURE_EXIT_CODE;
  2612. #endif    
  2613.       }
  2614. #ifdef NEXT_CPP_SERVER
  2615.       finclude (fd, pend_includes[i], &outbuf, 0, 0, 0, 0);
  2616. #else
  2617.       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
  2618. #endif
  2619.     }
  2620.   no_record_file--;
  2621.  
  2622.   /* Scan the input, processing macros and directives.  */
  2623.  
  2624.   rescan (&outbuf, 0);
  2625.  
  2626.   if (missing_newline)
  2627.     fp->lineno--;
  2628.  
  2629.   if (pedantic && missing_newline)
  2630.     pedwarn ("file does not end in newline");
  2631.  
  2632.   /* Now we have processed the entire input
  2633.      Write whichever kind of output has been requested.  */
  2634.  
  2635.   if (dump_macros == dump_only)
  2636.     dump_all_macros ();
  2637.   else if (! inhibit_output) {
  2638. #ifdef NEXT_CPP_SERVER
  2639.     write_output (&outbuf, out_fname, 1);
  2640. #else
  2641.     write_output ();
  2642. #endif
  2643.   }
  2644.  
  2645.   if (print_deps) {
  2646.     /* Don't actually write the deps file if compilation has failed.  */
  2647.     if (errors == 0) {
  2648.       if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
  2649. #ifdef NEXT_CPP_SERVER
  2650.          BAILOUT (deps_file);
  2651. #else
  2652.          pfatal_with_name (deps_file);
  2653. #endif
  2654.       fputs (deps_buffer, deps_stream);
  2655.       putc ('\n', deps_stream);
  2656.       if (deps_file) {
  2657.     if (ferror (deps_stream) || fclose (deps_stream) != 0)
  2658.       fatal ("I/O error on output");
  2659.       }
  2660.     }
  2661.   }
  2662. #ifdef NEXT_CPP_SERVER
  2663. finishRequest:
  2664.   if (serverized) {
  2665.      int wrote_headers = 0;
  2666.      if (header_fname)
  2667.         wrote_headers =
  2668.                    write_header_output (header_fname, &sys_header_outbuf, 0, 0);       
  2669.     if (timings) {
  2670.        stop();
  2671.        fprintf(stderr, "Preprocessed %s%s into %s\n  in ",
  2672.                wrote_headers ? "(loading new headers) " : "",
  2673.                in_fname, out_fname);
  2674.        display(stderr, 1);
  2675.     } else {
  2676.        fprintf(stderr, "Preprocessed %s%s into %s\n",
  2677.                wrote_headers ? "(loading new headers) " : "",
  2678.                in_fname, out_fname);
  2679.     }  
  2680.     fflush(stderr);
  2681.  
  2682.     /* Reinitialize include/import stack and macros */
  2683.     indepth = -1;
  2684.     clear_all_macros();
  2685.     clear_import();
  2686.  
  2687.     /* Reinitialize output buffer */
  2688.     outbuf.bufp = outbuf.buf;
  2689.     bzero (outbuf.bufp, outbuf.length);
  2690.     outbuf.lineno = 0;
  2691.  
  2692.     close (f);
  2693.     
  2694.     /* Reinitialize input buffer */
  2695.     free (fp->buf);
  2696.     fp->buf = fp->bufp = NULL;
  2697.  
  2698.     free_undefined_macros = 0;
  2699.  
  2700.     /* Clear up old -I paths, which may be different with each cpp command */
  2701.     include_ptr = include;
  2702.     while (include_ptr) {
  2703.        struct file_name_list * next_include = include_ptr->next;
  2704.        free (include_ptr);
  2705.        include_ptr = next_include;  
  2706.     }
  2707.     first_bracket_include = 0;
  2708.     first_system_include = 0;
  2709.     include = 0;    
  2710.     last_include = 0;
  2711.     after_include = 0;
  2712.     last_after_include = 0;
  2713.  
  2714. #ifdef NEXT_FRAMEWORKS
  2715.     /* Clear up old -F paths, which also could vary by cpp request */
  2716.     include_ptr = framework;
  2717.     while (include_ptr) {
  2718.        struct file_name_list * next_include = include_ptr->next;
  2719.        free (include_ptr);
  2720.        include_ptr = next_include;
  2721.     }
  2722.     first_bracket_framework = 0;
  2723.     framework = 0;    
  2724.     last_framework = 0;
  2725. #endif
  2726.     
  2727.     done_initializing = 0;  
  2728.     goto processCommand;
  2729.   }
  2730. #endif
  2731.   if (pcp_outfile && pcp_outfile != stdout
  2732.       && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
  2733.     fatal ("I/O error on `-pcp' output");
  2734.  
  2735.   if (ferror (stdout) || fclose (stdout) != 0)
  2736.     fatal ("I/O error on output");
  2737.  
  2738.   if (errors)
  2739.     exit (FAILURE_EXIT_CODE);
  2740.   exit (SUCCESS_EXIT_CODE);
  2741.  
  2742. #ifndef NEXT_CPP_SERVER
  2743.  /* ifndef insures there are no random goto's out of the main server loop */
  2744.  perror:
  2745. #endif
  2746.   pfatal_with_name (in_fname);
  2747.   return 0;
  2748. }
  2749.  
  2750. /* Given a colon-separated list of file names PATH,
  2751.    add all the names to the search path for include files.  */
  2752.  
  2753. static void
  2754. path_include (path)
  2755.      char *path;
  2756. {
  2757.   char *p;
  2758.  
  2759.   p = path;
  2760.  
  2761.   if (*p)
  2762.     while (1) {
  2763.       char *q = p;
  2764.       char *name;
  2765.       struct file_name_list *dirtmp;
  2766.  
  2767.       /* Find the end of this name.  */
  2768.       while (*q != 0 && *q != PATH_SEPARATOR) q++;
  2769.       if (p == q) {
  2770.     /* An empty name in the path stands for the current directory.  */
  2771.     name = (char *) xmalloc (2);
  2772.     name[0] = '.';
  2773.     name[1] = 0;
  2774.       } else {
  2775.     /* Otherwise use the directory that is named.  */
  2776.     name = (char *) xmalloc (q - p + 1);
  2777.     bcopy (p, name, q - p);
  2778.     name[q - p] = 0;
  2779.       }
  2780.  
  2781.       dirtmp = (struct file_name_list *)
  2782.     xmalloc (sizeof (struct file_name_list));
  2783.       dirtmp->next = 0;        /* New one goes on the end */
  2784.       dirtmp->control_macro = 0;
  2785.       dirtmp->c_system_include_path = 0;
  2786.       dirtmp->fname = name;
  2787.       dirtmp->got_name_map = 0;
  2788.       append_include_chain (dirtmp, dirtmp);
  2789.  
  2790.       /* Advance past this name.  */
  2791.       p = q;
  2792.       if (*p == 0)
  2793.     break;
  2794.       /* Skip the colon.  */
  2795.       p++;
  2796.     }
  2797. }
  2798.  
  2799. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  2800.    before main CCCP processing.  Name `pcp' is also in honor of the
  2801.    drugs the trigraph designers must have been on.
  2802.  
  2803.    Using an extra pass through the buffer takes a little extra time,
  2804.    but is infinitely less hairy than trying to handle trigraphs inside
  2805.    strings, etc. everywhere, and also makes sure that trigraphs are
  2806.    only translated in the top level of processing. */
  2807.  
  2808. static void
  2809. trigraph_pcp (buf)
  2810.      FILE_BUF *buf;
  2811. {
  2812.   register U_CHAR c, *fptr, *bptr, *sptr;
  2813.   int len;
  2814.  
  2815.   fptr = bptr = sptr = buf->buf;
  2816.   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  2817.     if (*++sptr != '?')
  2818.       continue;
  2819.     switch (*++sptr) {
  2820.       case '=':
  2821.       c = '#';
  2822.       break;
  2823.     case '(':
  2824.       c = '[';
  2825.       break;
  2826.     case '/':
  2827.       c = '\\';
  2828.       break;
  2829.     case ')':
  2830.       c = ']';
  2831.       break;
  2832.     case '\'':
  2833.       c = '^';
  2834.       break;
  2835.     case '<':
  2836.       c = '{';
  2837.       break;
  2838.     case '!':
  2839.       c = '|';
  2840.       break;
  2841.     case '>':
  2842.       c = '}';
  2843.       break;
  2844.     case '-':
  2845.       c  = '~';
  2846.       break;
  2847.     case '?':
  2848.       sptr--;
  2849.       continue;
  2850.     default:
  2851.       continue;
  2852.     }
  2853.     len = sptr - fptr - 2;
  2854.  
  2855.     /* BSD doc says bcopy () works right for overlapping strings.  In ANSI
  2856.        C, this will be memmove (). */
  2857.     if (bptr != fptr && len > 0)
  2858.       bcopy ((char *) fptr, (char *) bptr, len);
  2859.  
  2860.     bptr += len;
  2861.     *bptr++ = c;
  2862.     fptr = ++sptr;
  2863.   }
  2864.   len = buf->length - (fptr - buf->buf);
  2865.   if (bptr != fptr && len > 0)
  2866.     bcopy ((char *) fptr, (char *) bptr, len);
  2867.   buf->length -= fptr - bptr;
  2868.   buf->buf[buf->length] = '\0';
  2869.   if (warn_trigraphs && fptr != bptr)
  2870.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  2871. }
  2872.  
  2873. /* Move all backslash-newline pairs out of embarrassing places.
  2874.    Exchange all such pairs following BP
  2875.    with any potentially-embarrassing characters that follow them.
  2876.    Potentially-embarrassing characters are / and *
  2877.    (because a backslash-newline inside a comment delimiter
  2878.    would cause it not to be recognized).  */
  2879.  
  2880. static void
  2881. newline_fix (bp)
  2882.      U_CHAR *bp;
  2883. {
  2884.   register U_CHAR *p = bp;
  2885.   register int count = 0;
  2886.  
  2887.   /* First count the backslash-newline pairs here.  */
  2888.  
  2889.   while (1) {
  2890.     if (p[0] == '\\') {
  2891.       if (p[1] == '\n')
  2892.     p += 2, count++;
  2893.       else if (p[1] == '\r' && p[2] == '\n')
  2894.     p += 3, count++;
  2895.       else
  2896.     break;
  2897.     } else
  2898.       break;
  2899.   }
  2900.  
  2901.   /* What follows the backslash-newlines is not embarrassing.  */
  2902.  
  2903.   if (count == 0 || (*p != '/' && *p != '*'))
  2904.     return;
  2905.  
  2906.   /* Copy all potentially embarrassing characters
  2907.      that follow the backslash-newline pairs
  2908.      down to where the pairs originally started.  */
  2909.  
  2910.   while (*p == '*' || *p == '/')
  2911.     *bp++ = *p++;
  2912.  
  2913.   /* Now write the same number of pairs after the embarrassing chars.  */
  2914.   while (count-- > 0) {
  2915.     *bp++ = '\\';
  2916.     *bp++ = '\n';
  2917.   }
  2918. }
  2919.  
  2920. /* Like newline_fix but for use within a directive-name.
  2921.    Move any backslash-newlines up past any following symbol constituents.  */
  2922.  
  2923. static void
  2924. name_newline_fix (bp)
  2925.      U_CHAR *bp;
  2926. {
  2927.   register U_CHAR *p = bp;
  2928.   register int count = 0;
  2929.  
  2930.   /* First count the backslash-newline pairs here.  */
  2931.   while (1) {
  2932.     if (p[0] == '\\') {
  2933.       if (p[1] == '\n')
  2934.     p += 2, count++;
  2935.       else if (p[1] == '\r' && p[2] == '\n')
  2936.     p += 3, count++;
  2937.       else
  2938.     break;
  2939.     } else
  2940.       break;
  2941.   }
  2942.  
  2943.   /* What follows the backslash-newlines is not embarrassing.  */
  2944.  
  2945.   if (count == 0 || !is_idchar[*p])
  2946.     return;
  2947.  
  2948.   /* Copy all potentially embarrassing characters
  2949.      that follow the backslash-newline pairs
  2950.      down to where the pairs originally started.  */
  2951.  
  2952.   while (is_idchar[*p])
  2953.     *bp++ = *p++;
  2954.  
  2955.   /* Now write the same number of pairs after the embarrassing chars.  */
  2956.   while (count-- > 0) {
  2957.     *bp++ = '\\';
  2958.     *bp++ = '\n';
  2959.   }
  2960. }
  2961.  
  2962. /* Look for lint commands in comments.
  2963.  
  2964.    When we come in here, ibp points into a comment.  Limit is as one expects.
  2965.    scan within the comment -- it should start, after lwsp, with a lint command.
  2966.    If so that command is returned as a (constant) string.
  2967.  
  2968.    Upon return, any arg will be pointed to with argstart and will be
  2969.    arglen long.  Note that we don't parse that arg since it will just
  2970.    be printed out again.
  2971. */
  2972.  
  2973. static char *
  2974. get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
  2975.      register U_CHAR *ibp;
  2976.      register U_CHAR *limit;
  2977.      U_CHAR **argstart;        /* point to command arg */
  2978.      int *arglen, *cmdlen;    /* how long they are */
  2979. {
  2980.   long linsize;
  2981.   register U_CHAR *numptr;    /* temp for arg parsing */
  2982.  
  2983.   *arglen = 0;
  2984.  
  2985.   SKIP_WHITE_SPACE (ibp);
  2986.  
  2987.   if (ibp >= limit) return NULL;
  2988.  
  2989.   linsize = limit - ibp;
  2990.   
  2991.   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
  2992.   if ((linsize >= 10) && !strncmp (ibp, "NOTREACHED", 10)) {
  2993.     *cmdlen = 10;
  2994.     return "NOTREACHED";
  2995.   }
  2996.   if ((linsize >= 8) && !strncmp (ibp, "ARGSUSED", 8)) {
  2997.     *cmdlen = 8;
  2998.     return "ARGSUSED";
  2999.   }
  3000.   if ((linsize >= 11) && !strncmp (ibp, "LINTLIBRARY", 11)) {
  3001.     *cmdlen = 11;
  3002.     return "LINTLIBRARY";
  3003.   }
  3004.   if ((linsize >= 7) && !strncmp (ibp, "VARARGS", 7)) {
  3005.     *cmdlen = 7;
  3006.     ibp += 7; linsize -= 7;
  3007.     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
  3008.  
  3009.     /* OK, read a number */
  3010.     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
  3011.      numptr++);
  3012.     *arglen = numptr - *argstart;
  3013.     return "VARARGS";
  3014.   }
  3015.   return NULL;
  3016. }
  3017.  
  3018. /*
  3019.  * The main loop of the program.
  3020.  *
  3021.  * Read characters from the input stack, transferring them to the
  3022.  * output buffer OP.
  3023.  *
  3024.  * Macros are expanded and push levels on the input stack.
  3025.  * At the end of such a level it is popped off and we keep reading.
  3026.  * At the end of any other kind of level, we return.
  3027.  * #-directives are handled, except within macros.
  3028.  *
  3029.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  3030.  * and insert them when appropriate.  This is set while scanning macro
  3031.  * arguments before substitution.  It is zero when scanning for final output.
  3032.  *   There are three types of Newline markers:
  3033.  *   * Newline -  follows a macro name that was not expanded
  3034.  *     because it appeared inside an expansion of the same macro.
  3035.  *     This marker prevents future expansion of that identifier.
  3036.  *     When the input is rescanned into the final output, these are deleted.
  3037.  *     These are also deleted by ## concatenation.
  3038.  *   * Newline Space (or Newline and any other whitespace character)
  3039.  *     stands for a place that tokens must be separated or whitespace
  3040.  *     is otherwise desirable, but where the ANSI standard specifies there
  3041.  *     is no whitespace.  This marker turns into a Space (or whichever other
  3042.  *     whitespace char appears in the marker) in the final output,
  3043.  *     but it turns into nothing in an argument that is stringified with #.
  3044.  *     Such stringified arguments are the only place where the ANSI standard
  3045.  *     specifies with precision that whitespace may not appear.
  3046.  *
  3047.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  3048.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  3049.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  3050.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  3051.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  3052.  */
  3053.  
  3054. static void
  3055. rescan (op, output_marks)
  3056.      FILE_BUF *op;
  3057.      int output_marks;
  3058. {
  3059.   /* Character being scanned in main loop.  */
  3060.   register U_CHAR c;
  3061.  
  3062.   /* Length of pending accumulated identifier.  */
  3063.   register int ident_length = 0;
  3064.  
  3065.   /* Hash code of pending accumulated identifier.  */
  3066.   register int hash = 0;
  3067.  
  3068.   /* Current input level (&instack[indepth]).  */
  3069.   FILE_BUF *ip;
  3070.  
  3071.   /* Pointer for scanning input.  */
  3072.   register U_CHAR *ibp;
  3073.  
  3074.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  3075.   register U_CHAR *limit;
  3076.  
  3077.   /* Pointer for storing output.  */
  3078.   register U_CHAR *obp;
  3079.  
  3080.   /* REDO_CHAR is nonzero if we are processing an identifier
  3081.      after backing up over the terminating character.
  3082.      Sometimes we process an identifier without backing up over
  3083.      the terminating character, if the terminating character
  3084.      is not special.  Backing up is done so that the terminating character
  3085.      will be dispatched on again once the identifier is dealt with.  */
  3086.   int redo_char = 0;
  3087.  
  3088.   /* 1 if within an identifier inside of which a concatenation
  3089.      marker (Newline -) has been seen.  */
  3090.   int concatenated = 0;
  3091.  
  3092.   /* While scanning a comment or a string constant,
  3093.      this records the line it started on, for error messages.  */
  3094.   int start_line;
  3095.  
  3096.   /* Record position of last `real' newline.  */
  3097.   U_CHAR *beg_of_line;
  3098.  
  3099. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  3100.  
  3101. #define POPMACRO \
  3102. do { ip->macro->type = T_MACRO;        \
  3103.      if (ip->free_ptr) free (ip->free_ptr);    \
  3104.      --indepth; } while (0)
  3105.  
  3106. /* Reload `rescan's local variables that describe the current
  3107.    level of the input stack.  */
  3108.  
  3109. #define RECACHE  \
  3110. do { ip = &instack[indepth];        \
  3111.      ibp = ip->bufp;            \
  3112.      limit = ip->buf + ip->length;    \
  3113.      op->bufp = obp;            \
  3114.      check_expand (op, limit - ibp);    \
  3115.      beg_of_line = ip->beg_of_line; /*0;*/    \
  3116.      obp = op->bufp; } while (0)
  3117.  
  3118. #ifdef NEXT_CPP_SERVER
  3119.   if (no_output && instack[indepth].fname != 0)
  3120.     skip_if_group (&instack[indepth], op, 1);
  3121.   else if (segregate_output && 
  3122.    (instack[indepth].file && instack[indepth].file->header_output))
  3123.     skip_if_group (&instack[indepth], op, 1);
  3124. #else
  3125.   if (no_output && instack[indepth].fname != 0)
  3126.     skip_if_group (&instack[indepth], 1);
  3127. #endif
  3128.  
  3129.   obp = op->bufp;
  3130.   RECACHE;
  3131.  
  3132.   beg_of_line = ibp;
  3133.  
  3134.   /* Our caller must always put a null after the end of
  3135.      the input at each input stack level.  */
  3136.   if (*limit != 0)
  3137.     abort ();
  3138.  
  3139.   while (1) {
  3140.     c = *ibp++;
  3141.     *obp++ = c;
  3142.  
  3143.     switch (c) {
  3144.     case '\\':
  3145.       if (ibp >= limit)
  3146.     break;
  3147.       if (*ibp == '\n') {
  3148.     /* Always merge lines ending with backslash-newline,
  3149.        even in middle of identifier.  */
  3150.     ++ibp;
  3151.     ++ip->lineno;
  3152.     --obp;        /* remove backslash from obuf */
  3153.     break;
  3154.       }
  3155.       /* Otherwise, backslash suppresses specialness of following char,
  3156.      so copy it here to prevent the switch from seeing it.
  3157.      But first get any pending identifier processed.  */
  3158.       if (ident_length > 0)
  3159.     goto specialchar;
  3160.       *obp++ = *ibp++;
  3161.       break;
  3162.  
  3163.     case '#':
  3164.       if (assertions_flag) {
  3165.     /* Copy #foo (bar lose) without macro expansion.  */
  3166.     SKIP_WHITE_SPACE (ibp);
  3167.     while (is_idchar[*ibp])
  3168.       *obp++ = *ibp++;
  3169.     SKIP_WHITE_SPACE (ibp);
  3170.     if (*ibp == '(') {
  3171.       ip->bufp = ibp;
  3172. #ifdef NEXT_CPP_SERVER
  3173.       skip_paren_group (ip, op);
  3174. #else
  3175.       skip_paren_group (ip);
  3176. #endif
  3177.       bcopy (ibp, obp, ip->bufp - ibp);
  3178.       obp += ip->bufp - ibp;
  3179.       ibp = ip->bufp;
  3180.     }
  3181.       }
  3182.  
  3183.       /* If this is expanding a macro definition, don't recognize
  3184.      preprocessor directives.  */
  3185.       if (ip->macro != 0)
  3186.     goto randomchar;
  3187.       /* If this is expand_into_temp_buffer, recognize them
  3188.      only after an actual newline at this level,
  3189.      not at the beginning of the input level.  */
  3190.       if (ip->fname == 0 && beg_of_line == ip->buf)
  3191.     goto randomchar;
  3192.       if (ident_length)
  3193.     goto specialchar;
  3194.  
  3195.       
  3196.       /* # keyword: a # must be first nonblank char on the line */
  3197.       if (beg_of_line == 0)
  3198.     goto randomchar;
  3199.       {
  3200.     U_CHAR *bp;
  3201.  
  3202.     /* Scan from start of line, skipping whitespace, comments
  3203.        and backslash-newlines, and see if we reach this #.
  3204.        If not, this # is not special.  */
  3205.     bp = beg_of_line;
  3206.     /* If -traditional, require # to be at beginning of line.  */
  3207.     if (!traditional)
  3208.       while (1) {
  3209.         if (is_hor_space[*bp])
  3210.           bp++;
  3211.         else if (*bp == '\\' && bp[1] == '\n')
  3212.           bp += 2;
  3213.         else if (*bp == '/' && bp[1] == '*') {
  3214.           bp += 2;
  3215.           while (!(*bp == '*' && bp[1] == '/'))
  3216.         bp++;
  3217.           bp += 2;
  3218.         }
  3219.         /* There is no point in trying to deal with C++ // comments here,
  3220.            because if there is one, then this # must be part of the
  3221.            comment and we would never reach here.  */
  3222.         else break;
  3223.       }
  3224.     if (bp + 1 != ibp)
  3225.       goto randomchar;
  3226.       }
  3227.  
  3228.       /* This # can start a directive.  */
  3229.  
  3230.       --obp;        /* Don't copy the '#' */
  3231.  
  3232.       ip->bufp = ibp;
  3233.       op->bufp = obp;
  3234.       if (! handle_directive (ip, op)) {
  3235. #ifdef USE_C_ALLOCA
  3236.     alloca (0);
  3237. #endif
  3238.     /* Not a known directive: treat it as ordinary text.
  3239.        IP, OP, IBP, etc. have not been changed.  */
  3240.     if (no_output && instack[indepth].fname) {
  3241.       /* If not generating expanded output,
  3242.          what we do with ordinary text is skip it.
  3243.          Discard everything until next # directive.  */
  3244. #ifdef NEXT_CPP_SERVER
  3245.       skip_if_group (&instack[indepth], op, 1);
  3246. #else
  3247.       skip_if_group (&instack[indepth], 1);
  3248. #endif
  3249.       RECACHE;
  3250.       beg_of_line = ibp;
  3251.       break;
  3252.     }
  3253. #ifdef NEXT_CPP_SERVER
  3254.         else if (segregate_output && 
  3255.                  (instack[indepth].file &&
  3256.                   instack[indepth].file->header_output)) {
  3257.       /* If not generating expanded output,
  3258.          what we do with ordinary text is skip it.
  3259.          Discard everything until next # directive.  */
  3260.       skip_if_group (&instack[indepth], op, 1);
  3261.       RECACHE;
  3262.       beg_of_line = ibp;
  3263.       break;
  3264.         }
  3265. #endif
  3266.     ++obp;        /* Copy the '#' after all */
  3267.     /* Don't expand an identifier that could be a macro directive.
  3268.        (Section 3.8.3 of the ANSI C standard)            */
  3269.     SKIP_WHITE_SPACE (ibp);
  3270.     if (is_idstart[*ibp])
  3271.       {
  3272.         *obp++ = *ibp++;
  3273.         while (is_idchar[*ibp])
  3274.           *obp++ = *ibp++;
  3275.       }
  3276.     goto randomchar;
  3277.       }
  3278. #ifdef USE_C_ALLOCA
  3279.       alloca (0);
  3280. #endif
  3281.       /* A # directive has been successfully processed.  */
  3282.       /* If not generating expanded output, ignore everything until
  3283.      next # directive.  */
  3284. #ifdef NEXT_CPP_SERVER
  3285.       if (no_output && instack[indepth].fname)
  3286.     skip_if_group (&instack[indepth], op, 1);
  3287.       else if (segregate_output && (instack[indepth].file && 
  3288.                           instack[indepth].file->header_output))
  3289.     skip_if_group (&instack[indepth], op, 1);
  3290. #else
  3291.       if (no_output && instack[indepth].fname)
  3292.     skip_if_group (&instack[indepth], 1);
  3293. #endif
  3294.       obp = op->bufp;
  3295.       RECACHE;
  3296.       beg_of_line = ibp;
  3297.       break;
  3298.  
  3299.     case '\"':            /* skip quoted string */
  3300.     case '\'':
  3301.       /* A single quoted string is treated like a double -- some
  3302.      programs (e.g., troff) are perverse this way */
  3303.  
  3304.       if (ident_length)
  3305.     goto specialchar;
  3306.  
  3307.       start_line = ip->lineno;
  3308.  
  3309.       /* Skip ahead to a matching quote.  */
  3310.  
  3311.       while (1) {
  3312.     if (ibp >= limit) {
  3313.       if (ip->macro != 0) {
  3314.         /* try harder: this string crosses a macro expansion boundary.
  3315.            This can happen naturally if -traditional.
  3316.            Otherwise, only -D can make a macro with an unmatched quote.  */
  3317.         POPMACRO;
  3318.         RECACHE;
  3319.         continue;
  3320.       }
  3321.       if (!traditional) {
  3322.         error_with_line (line_for_error (start_line),
  3323.                  "unterminated string or character constant");
  3324.         error_with_line (multiline_string_line,
  3325.                  "possible real start of unterminated constant");
  3326.         multiline_string_line = 0;
  3327.       }
  3328.       break;
  3329.     }
  3330.     *obp++ = *ibp;
  3331.     switch (*ibp++) {
  3332.     case '\n':
  3333.       ++ip->lineno;
  3334.       ++op->lineno;
  3335.       /* Traditionally, end of line ends a string constant with no error.
  3336.          So exit the loop and record the new line.  */
  3337.       if (traditional) {
  3338.         beg_of_line = ibp;
  3339.         goto while2end;
  3340.       }
  3341.       if (c == '\'') {
  3342.         error_with_line (line_for_error (start_line),
  3343.                  "unterminated character constant");
  3344.         goto while2end;
  3345.       }
  3346.       if (pedantic && multiline_string_line == 0) {
  3347.         pedwarn_with_line (line_for_error (start_line),
  3348.                    "string constant runs past end of line");
  3349.       }
  3350.       if (multiline_string_line == 0)
  3351.         multiline_string_line = ip->lineno - 1;
  3352.       break;
  3353.  
  3354.     case '\\':
  3355.       if (ibp >= limit)
  3356.         break;
  3357.       if (*ibp == '\n') {
  3358.         /* Backslash newline is replaced by nothing at all,
  3359.            but keep the line counts correct.  */
  3360.         --obp;
  3361.         ++ibp;
  3362.         ++ip->lineno;
  3363.       } else {
  3364.         /* ANSI stupidly requires that in \\ the second \
  3365.            is *not* prevented from combining with a newline.  */
  3366.         while (*ibp == '\\' && ibp[1] == '\n') {
  3367.           ibp += 2;
  3368.           ++ip->lineno;
  3369.         }
  3370.         *obp++ = *ibp++;
  3371.       }
  3372.       break;
  3373.  
  3374.     case '\"':
  3375.     case '\'':
  3376.       if (ibp[-1] == c)
  3377.         goto while2end;
  3378.       break;
  3379.     }
  3380.       }
  3381.     while2end:
  3382.       break;
  3383.  
  3384.     case '/':
  3385.       if (*ibp == '\\' && ibp[1] == '\n')
  3386.     newline_fix (ibp);
  3387.  
  3388.       if (*ibp != '*'
  3389.       && !(cplusplus_comments && *ibp == '/'))
  3390.     goto randomchar;
  3391.       if (ip->macro != 0)
  3392.     goto randomchar;
  3393.       if (ident_length)
  3394.     goto specialchar;
  3395.  
  3396.       if (*ibp == '/') {
  3397.     /* C++ style comment... */
  3398.     start_line = ip->lineno;
  3399.  
  3400.     --ibp;            /* Back over the slash */
  3401.     --obp;
  3402.  
  3403.     /* Comments are equivalent to spaces. */
  3404.     if (! put_out_comments)
  3405.       *obp++ = ' ';
  3406.     else {
  3407.       /* must fake up a comment here */
  3408.       *obp++ = '/';
  3409.       *obp++ = '/';
  3410.     }
  3411.     {
  3412.       U_CHAR *before_bp = ibp+2;
  3413.  
  3414.       while (ibp < limit) {
  3415.         if (ibp[-1] != '\\' && *ibp == '\n') {
  3416.           if (put_out_comments) {
  3417.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3418.         obp += ibp - before_bp;
  3419.           }
  3420.           break;
  3421.         } else {
  3422.           if (*ibp == '\n') {
  3423.         ++ip->lineno;
  3424.         /* Copy the newline into the output buffer, in order to
  3425.            avoid the pain of a #line every time a multiline comment
  3426.            is seen.  */
  3427.         if (!put_out_comments)
  3428.           *obp++ = '\n';
  3429.         ++op->lineno;
  3430.           }
  3431.           ibp++;
  3432.         }
  3433.       }
  3434.       break;
  3435.     }
  3436.       }
  3437.  
  3438.       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
  3439.  
  3440.       start_line = ip->lineno;
  3441.  
  3442.       ++ibp;            /* Skip the star. */
  3443.  
  3444.       /* If this cpp is for lint, we peek inside the comments: */
  3445.       if (for_lint) {
  3446.     U_CHAR *argbp;
  3447.     int cmdlen, arglen;
  3448.     char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
  3449.  
  3450.     if (lintcmd != NULL) {
  3451.       /* I believe it is always safe to emit this newline: */
  3452.       obp[-1] = '\n';
  3453.       bcopy ("#pragma lint ", (char *) obp, 13);
  3454.       obp += 13;
  3455.       bcopy (lintcmd, (char *) obp, cmdlen);
  3456.       obp += cmdlen;
  3457.  
  3458.       if (arglen != 0) {
  3459.         *(obp++) = ' ';
  3460.         bcopy (argbp, (char *) obp, arglen);
  3461.         obp += arglen;
  3462.       }
  3463.  
  3464.       /* OK, now bring us back to the state we were in before we entered
  3465.          this branch.  We need #line b/c the newline for the pragma
  3466.          could fuck things up. */
  3467.       output_line_command (ip, op, 0, same_file);
  3468.       *(obp++) = ' ';    /* just in case, if comments are copied thru */
  3469.       *(obp++) = '/';
  3470.     }
  3471.       }
  3472.  
  3473.       /* Comments are equivalent to spaces.
  3474.      Note that we already output the slash; we might not want it.
  3475.      For -traditional, a comment is equivalent to nothing.  */
  3476.       if (! put_out_comments) {
  3477.     if (traditional)
  3478.       obp--;
  3479.     else
  3480.       obp[-1] = ' ';
  3481.       }
  3482.       else
  3483.     *obp++ = '*';
  3484.  
  3485.       {
  3486.     U_CHAR *before_bp = ibp;
  3487.  
  3488.     while (ibp < limit) {
  3489.       switch (*ibp++) {
  3490.       case '/':
  3491.         if (warn_comments && ibp < limit && *ibp == '*')
  3492.           warning ("`/*' within comment");
  3493.         break;
  3494.       case '*':
  3495.         if (*ibp == '\\' && ibp[1] == '\n')
  3496.           newline_fix (ibp);
  3497.         if (ibp >= limit || *ibp == '/')
  3498.           goto comment_end;
  3499.         break;
  3500.       case '\n':
  3501.         ++ip->lineno;
  3502.         /* Copy the newline into the output buffer, in order to
  3503.            avoid the pain of a #line every time a multiline comment
  3504.            is seen.  */
  3505.         if (!put_out_comments)
  3506.           *obp++ = '\n';
  3507.         ++op->lineno;
  3508.       }
  3509.     }
  3510.       comment_end:
  3511.  
  3512.     if (ibp >= limit)
  3513.       error_with_line (line_for_error (start_line),
  3514.                "unterminated comment");
  3515.     else {
  3516.       ibp++;
  3517.       if (put_out_comments) {
  3518.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3519.         obp += ibp - before_bp;
  3520.       }
  3521.     }
  3522.       }
  3523.       break;
  3524.  
  3525.     case '$':
  3526.       if (!dollars_in_ident)
  3527.     goto randomchar;
  3528.       goto letter;
  3529.  
  3530.     case '0': case '1': case '2': case '3': case '4':
  3531.     case '5': case '6': case '7': case '8': case '9':
  3532.       /* If digit is not part of identifier, it starts a number,
  3533.      which means that following letters are not an identifier.
  3534.      "0x5" does not refer to an identifier "x5".
  3535.      So copy all alphanumerics that follow without accumulating
  3536.      as an identifier.  Periods also, for sake of "3.e7".  */
  3537.  
  3538.       if (ident_length == 0) {
  3539.     while (ibp < limit) {
  3540.       while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  3541.         ++ip->lineno;
  3542.         ibp += 2;
  3543.       }
  3544.       c = *ibp++;
  3545.       /* ".." terminates a preprocessing number.  This is useless for C
  3546.          code but useful for preprocessing other things.  */
  3547.       if (!isalnum (c) && (c != '.' || *ibp == '.') && c != '_') {
  3548.         --ibp;
  3549.         break;
  3550.       }
  3551.       *obp++ = c;
  3552.       /* A sign can be part of a preprocessing number
  3553.          if it follows an e.  */
  3554.       if (c == 'e' || c == 'E') {
  3555.         while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
  3556.           ++ip->lineno;
  3557.           ibp += 2;
  3558.         }
  3559.         if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
  3560.           *obp++ = *ibp++;
  3561.           /* But traditional C does not let the token go past the sign.  */
  3562.           if (traditional)
  3563.         break;
  3564.         }
  3565.       }
  3566.     }
  3567.     break;
  3568.       }
  3569.       /* fall through */
  3570.  
  3571.     case '_':
  3572.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  3573.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  3574.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  3575.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  3576.     case 'y': case 'z':
  3577.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  3578.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  3579.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  3580.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  3581.     case 'Y': case 'Z':
  3582.     letter:
  3583.       ident_length++;
  3584.       /* Compute step of hash function, to avoid a proc call on every token */
  3585.       hash = HASHSTEP (hash, c);
  3586.       break;
  3587.  
  3588.     case '\n':
  3589.       if (ip->fname == 0 && *ibp == '-') {
  3590.     /* Newline - inhibits expansion of preceding token.
  3591.        If expanding a macro arg, we keep the newline -.
  3592.        In final output, it is deleted.
  3593.        We recognize Newline - in macro bodies and macro args.  */
  3594.     if (! concatenated) {
  3595.       ident_length = 0;
  3596.       hash = 0;
  3597.     }
  3598.     ibp++;
  3599.     if (!output_marks) {
  3600.       obp--;
  3601.     } else {
  3602.       /* If expanding a macro arg, keep the newline -.  */
  3603.       *obp++ = '-';
  3604.     }
  3605.     break;
  3606.       }
  3607.  
  3608.       /* If reprocessing a macro expansion, newline is a special marker.  */
  3609.       else if (ip->macro != 0) {
  3610.     /* Newline White is a "funny space" to separate tokens that are
  3611.        supposed to be separate but without space between.
  3612.        Here White means any whitespace character.
  3613.        Newline - marks a recursive macro use that is not
  3614.        supposed to be expandable.  */
  3615.  
  3616.     if (is_space[*ibp]) {
  3617.       /* Newline Space does not prevent expansion of preceding token
  3618.          so expand the preceding token and then come back.  */
  3619.       if (ident_length > 0)
  3620.         goto specialchar;
  3621.  
  3622.       /* If generating final output, newline space makes a space.  */
  3623.       if (!output_marks) {
  3624.         obp[-1] = *ibp++;
  3625.         /* And Newline Newline makes a newline, so count it.  */
  3626.         if (obp[-1] == '\n')
  3627.           op->lineno++;
  3628.       } else {
  3629.         /* If expanding a macro arg, keep the newline space.
  3630.            If the arg gets stringified, newline space makes nothing.  */
  3631.         *obp++ = *ibp++;
  3632.       }
  3633.     } else abort ();    /* Newline followed by something random?  */
  3634.     break;
  3635.       }
  3636.  
  3637.       /* If there is a pending identifier, handle it and come back here.  */
  3638.       if (ident_length > 0)
  3639.     goto specialchar;
  3640.  
  3641.       beg_of_line = ibp;
  3642.  
  3643.       /* Update the line counts and output a #line if necessary.  */
  3644.       ++ip->lineno;
  3645.       ++op->lineno;
  3646.       if (ip->lineno != op->lineno) {
  3647.     op->bufp = obp;
  3648.     output_line_command (ip, op, 1, same_file);
  3649.     check_expand (op, ip->length - (ip->bufp - ip->buf));
  3650.     obp = op->bufp;
  3651.       }
  3652.       break;
  3653.  
  3654.       /* Come here either after (1) a null character that is part of the input
  3655.      or (2) at the end of the input, because there is a null there.  */
  3656.     case 0:
  3657.       if (ibp <= limit)
  3658.     /* Our input really contains a null character.  */
  3659.     goto randomchar;
  3660.  
  3661.       /* At end of a macro-expansion level, pop it and read next level.  */
  3662.       if (ip->macro != 0) {
  3663.     obp--;
  3664.     ibp--;
  3665.     /* If traditional, and we have an identifier that ends here,
  3666.        process it now, so we get the right error for recursion.  */
  3667.     if (traditional && ident_length
  3668.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  3669.       redo_char = 1;
  3670.       goto randomchar;
  3671.     }
  3672.     POPMACRO;
  3673.     RECACHE;
  3674. #ifdef NEXT_CPP_SERVER
  3675.         if (mark_expansions && instack[indepth].beg_of_line) {    
  3676.           output_line_command (&instack[indepth], op, 0, leave_expansion); 
  3677.           obp = op->bufp;
  3678.         }
  3679. #endif
  3680.         break;
  3681.       }
  3682.  
  3683.       /* If we don't have a pending identifier,
  3684.      return at end of input.  */
  3685.       if (ident_length == 0) {
  3686.     obp--;
  3687.     ibp--;
  3688.     op->bufp = obp;
  3689.     ip->bufp = ibp;
  3690.     goto ending;
  3691.       }
  3692.  
  3693.       /* If we do have a pending identifier, just consider this null
  3694.      a special character and arrange to dispatch on it again.
  3695.      The second time, IDENT_LENGTH will be zero so we will return.  */
  3696.  
  3697.       /* Fall through */
  3698.  
  3699. specialchar:
  3700.  
  3701.       /* Handle the case of a character such as /, ', " or null
  3702.      seen following an identifier.  Back over it so that
  3703.      after the identifier is processed the special char
  3704.      will be dispatched on again.  */
  3705.  
  3706.       ibp--;
  3707.       obp--;
  3708.       redo_char = 1;
  3709.  
  3710.     default:
  3711.  
  3712. randomchar:
  3713.  
  3714.       if (ident_length > 0) {
  3715.     register HASHNODE *hp;
  3716.  
  3717.     /* We have just seen an identifier end.  If it's a macro, expand it.
  3718.  
  3719.        IDENT_LENGTH is the length of the identifier
  3720.        and HASH is its hash code.
  3721.  
  3722.        The identifier has already been copied to the output,
  3723.        so if it is a macro we must remove it.
  3724.  
  3725.        If REDO_CHAR is 0, the char that terminated the identifier
  3726.        has been skipped in the output and the input.
  3727.        OBP-IDENT_LENGTH-1 points to the identifier.
  3728.        If the identifier is a macro, we must back over the terminator.
  3729.  
  3730.        If REDO_CHAR is 1, the terminating char has already been
  3731.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  3732.  
  3733.     if (!pcp_outfile || pcp_inside_if) {
  3734. startagain:
  3735.       for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  3736.            hp = hp->next) {
  3737.         
  3738.         if (hp->length == ident_length) {
  3739.           int obufp_before_macroname;
  3740.           int op_lineno_before_macroname;
  3741.           register int i = ident_length;
  3742.           register U_CHAR *p = hp->name;
  3743.           register U_CHAR *q = obp - i;
  3744.           int disabled;
  3745.           
  3746.           if (! redo_char)
  3747.         q--;
  3748.           
  3749.           do {        /* All this to avoid a strncmp () */
  3750.         if (*p++ != *q++)
  3751.           goto hashcollision;
  3752.           } while (--i);
  3753.           
  3754.           /* We found a use of a macro name.
  3755.          see if the context shows it is a macro call.  */
  3756. #ifdef NEXT_CPP_SERVER          
  3757.               if (hp->type == T_MACRO && !hp->defined_within_module)
  3758.                 break;
  3759. #endif
  3760.           /* Back up over terminating character if not already done.  */
  3761.           if (! redo_char) {
  3762.         ibp--;
  3763.         obp--;
  3764.           }
  3765.           
  3766.           /* Save this as a displacement from the beginning of the output
  3767.          buffer.  We can not save this as a position in the output
  3768.          buffer, because it may get realloc'ed by RECACHE.  */
  3769.           obufp_before_macroname = (obp - op->buf) - ident_length;
  3770.           op_lineno_before_macroname = op->lineno;
  3771.           
  3772.           if (hp->type == T_PCSTRING) {
  3773.         pcstring_used (hp); /* Mark the definition of this key
  3774.                        as needed, ensuring that it
  3775.                        will be output.  */
  3776.         break;        /* Exit loop, since the key cannot have a
  3777.                    definition any longer.  */
  3778.           }
  3779.  
  3780.           /* Record whether the macro is disabled.  */
  3781.           disabled = hp->type == T_DISABLED;
  3782.           
  3783.           /* This looks like a macro ref, but if the macro was disabled,
  3784.          just copy its name and put in a marker if requested.  */
  3785.           
  3786.           if (disabled) {
  3787. #if 0
  3788.         /* This error check caught useful cases such as
  3789.            #define foo(x,y) bar (x (y,0), y)
  3790.            foo (foo, baz)  */
  3791.         if (traditional)
  3792.           error ("recursive use of macro `%s'", hp->name);
  3793. #endif
  3794.         
  3795.         if (output_marks) {
  3796.           check_expand (op, limit - ibp + 2);
  3797.           *obp++ = '\n';
  3798.           *obp++ = '-';
  3799.         }
  3800.         break;
  3801.           }
  3802.           
  3803.           /* If macro wants an arglist, verify that a '(' follows.
  3804.          first skip all whitespace, copying it to the output
  3805.          after the macro name.  Then, if there is no '(',
  3806.          decide this is not a macro call and leave things that way.  */
  3807.           if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  3808.           && hp->value.defn->nargs >= 0)
  3809.         {
  3810.           U_CHAR *old_ibp = ibp;
  3811.           U_CHAR *old_obp = obp;
  3812.           int old_iln = ip->lineno;
  3813.           int old_oln = op->lineno;
  3814.           
  3815.           while (1) {
  3816.             /* Scan forward over whitespace, copying it to the output.  */
  3817.             if (ibp == limit && ip->macro != 0) {
  3818.               POPMACRO;
  3819.               RECACHE;
  3820.               old_ibp = ibp;
  3821.               old_obp = obp;
  3822.               old_iln = ip->lineno;
  3823.               old_oln = op->lineno;
  3824.             }
  3825.             /* A comment: copy it unchanged or discard it.  */
  3826.             else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
  3827.               if (put_out_comments) {
  3828.             *obp++ = '/';
  3829.             *obp++ = '*';
  3830.               } else if (! traditional) {
  3831.             *obp++ = ' ';
  3832.               }
  3833.               ibp += 2;
  3834.               while (ibp + 1 != limit
  3835.                  && !(ibp[0] == '*' && ibp[1] == '/')) {
  3836.             /* We need not worry about newline-marks,
  3837.                since they are never found in comments.  */
  3838.             if (*ibp == '\n') {
  3839.               /* Newline in a file.  Count it.  */
  3840.               ++ip->lineno;
  3841.               ++op->lineno;
  3842.             }
  3843.             if (put_out_comments)
  3844.               *obp++ = *ibp++;
  3845.             else
  3846.               ibp++;
  3847.               }
  3848.               ibp += 2;
  3849.               if (put_out_comments) {
  3850.             *obp++ = '*';
  3851.             *obp++ = '/';
  3852.               }
  3853.             }
  3854.             else if (is_space[*ibp]) {
  3855.               *obp++ = *ibp++;
  3856.               if (ibp[-1] == '\n') {
  3857.             if (ip->macro == 0) {
  3858.               /* Newline in a file.  Count it.  */
  3859.               ++ip->lineno;
  3860.               ++op->lineno;
  3861.             } else if (!output_marks) {
  3862.               /* A newline mark, and we don't want marks
  3863.                  in the output.  If it is newline-hyphen,
  3864.                  discard it entirely.  Otherwise, it is
  3865.                  newline-whitechar, so keep the whitechar.  */
  3866.               obp--;
  3867.               if (*ibp == '-')
  3868.                 ibp++;
  3869.               else {
  3870.                 if (*ibp == '\n')
  3871.                   ++op->lineno;
  3872.                 *obp++ = *ibp++;
  3873.               }
  3874.             } else {
  3875.               /* A newline mark; copy both chars to the output.  */
  3876.               *obp++ = *ibp++;
  3877.             }
  3878.               }
  3879.             }
  3880.             else break;
  3881.           }
  3882.           if (*ibp != '(') {
  3883.             /* It isn't a macro call.
  3884.                Put back the space that we just skipped.  */
  3885.             ibp = old_ibp;
  3886.             obp = old_obp;
  3887.             ip->lineno = old_iln;
  3888.             op->lineno = old_oln;
  3889.             /* Exit the for loop.  */
  3890.             break;
  3891.           }
  3892.         }
  3893.           
  3894.           /* This is now known to be a macro call.
  3895.          Discard the macro name from the output,
  3896.          along with any following whitespace just copied.  */
  3897.           obp = op->buf + obufp_before_macroname;
  3898.           op->lineno = op_lineno_before_macroname;
  3899.  
  3900.           /* Prevent accidental token-pasting with a character
  3901.          before the macro call.  */
  3902.           if (!traditional && obp != op->buf
  3903.           && (obp[-1] == '-' || obp[1] == '+' || obp[1] == '&'
  3904.               || obp[-1] == '|' || obp[1] == '<' || obp[1] == '>')) {
  3905.         /* If we are expanding a macro arg, make a newline marker
  3906.            to separate the tokens.  If we are making real output,
  3907.            a plain space will do.  */
  3908.         if (output_marks)
  3909.           *obp++ = '\n';
  3910.         *obp++ = ' ';
  3911.           }
  3912.  
  3913.           /* Expand the macro, reading arguments as needed,
  3914.          and push the expansion on the input stack.  */
  3915.           ip->bufp = ibp;
  3916.           op->bufp = obp;
  3917.  
  3918. #ifdef NEXT_CPP_SERVER
  3919.               {
  3920.               U_CHAR *start_exp = ip->bufp - ident_length;
  3921.  
  3922.           macroexpand (hp, op);
  3923.  
  3924.               if (mark_expansions &&
  3925.                   (hp->type == T_DISABLED || hp->type == T_MACRO) && 
  3926.                   (indepth == 1 || /* processing the main module */
  3927.                   (instack[indepth-1].fname && instack[indepth-1].file))) {
  3928.                 /* overload nominal_fname (for purposes of the call to
  3929.                    output_line_command). Even though this seems gross,
  3930.                    it acutally models the markers that are output */
  3931.                 char *save_name = instack[indepth-1].nominal_fname;
  3932.                 char save_char = *ip->bufp;
  3933.                 instack[indepth-1].beg_of_line = beg_of_line;
  3934.                 instack[indepth-1].nominal_fname = start_exp;
  3935.                 *ip->bufp = '\0';
  3936.                 output_line_command (&instack[indepth-1], op, 0,
  3937.                                      enter_expansion);
  3938.                 instack[indepth-1].nominal_fname = save_name;
  3939.                 *ip->bufp = save_char;
  3940.               }
  3941.               }
  3942. #else  
  3943.           macroexpand (hp, op);
  3944. #endif
  3945.           /* Reexamine input stack, since macroexpand has pushed
  3946.          a new level on it.  */
  3947.           obp = op->bufp;
  3948.           RECACHE;
  3949.           break;
  3950.         }
  3951. hashcollision:
  3952.         ;
  3953.       }            /* End hash-table-search loop */
  3954.     }
  3955.     ident_length = hash = 0; /* Stop collecting identifier */
  3956.     redo_char = 0;
  3957.     concatenated = 0;
  3958.       }                /* End if (ident_length > 0) */
  3959.     }                /* End switch */
  3960.   }                /* End per-char loop */
  3961.  
  3962.   /* Come here to return -- but first give an error message
  3963.      if there was an unterminated successful conditional.  */
  3964.  ending:
  3965.   if (if_stack != ip->if_stack)
  3966.     {
  3967.       char *str = "unknown";
  3968.  
  3969.       switch (if_stack->type)
  3970.     {
  3971.     case T_IF:
  3972.       str = "if";
  3973.       break;
  3974.     case T_IFDEF:
  3975.       str = "ifdef";
  3976.       break;
  3977.     case T_IFNDEF:
  3978.       str = "ifndef";
  3979.       break;
  3980.     case T_ELSE:
  3981.       str = "else";
  3982.       break;
  3983.     case T_ELIF:
  3984.       str = "elif";
  3985.       break;
  3986.     }
  3987.  
  3988.       error_with_line (line_for_error (if_stack->lineno),
  3989.                "unterminated `#%s' conditional", str);
  3990.   }
  3991.   if_stack = ip->if_stack;
  3992. }
  3993.  
  3994. /*
  3995.  * Rescan a string into a temporary buffer and return the result
  3996.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  3997.  *
  3998.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  3999.  * and insert such markers when appropriate.  See `rescan' for details.
  4000.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  4001.  * before substitution; it is 0 for other uses.
  4002.  */
  4003. static FILE_BUF
  4004. expand_to_temp_buffer (buf, limit, output_marks, assertions)
  4005.      U_CHAR *buf, *limit;
  4006.      int output_marks, assertions;
  4007. {
  4008.   register FILE_BUF *ip;
  4009.   FILE_BUF obuf;
  4010.   int length = limit - buf;
  4011.   U_CHAR *buf1;
  4012.   int odepth = indepth;
  4013.   int save_assertions_flag = assertions_flag;
  4014.  
  4015.   assertions_flag = assertions;
  4016.  
  4017.   if (length < 0)
  4018.     abort ();
  4019.  
  4020.   /* Set up the input on the input stack.  */
  4021.  
  4022.   buf1 = (U_CHAR *) alloca (length + 1);
  4023.   {
  4024.     register U_CHAR *p1 = buf;
  4025.     register U_CHAR *p2 = buf1;
  4026.  
  4027.     while (p1 != limit)
  4028.       *p2++ = *p1++;
  4029.   }
  4030.   buf1[length] = 0;
  4031.  
  4032.   /* Set up to receive the output.  */
  4033.  
  4034.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  4035.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  4036.   obuf.fname = 0;
  4037.   obuf.macro = 0;
  4038.   obuf.free_ptr = 0;
  4039.  
  4040.   CHECK_DEPTH ({return obuf;});
  4041.  
  4042.   ++indepth;
  4043.  
  4044.   ip = &instack[indepth];
  4045.   ip->fname = 0;
  4046.   ip->nominal_fname = 0;
  4047.   ip->system_header_p = 0;
  4048.   ip->macro = 0;
  4049.   ip->free_ptr = 0;
  4050.   ip->length = length;
  4051.   ip->buf = ip->bufp = buf1;
  4052.   ip->if_stack = if_stack;
  4053. #ifdef NEXT_CPP_SERVER
  4054.   ip->dir = ip->file = 0;
  4055.   ip->beg_of_line = 0;
  4056. #endif
  4057.  
  4058.   ip->lineno = obuf.lineno = 1;
  4059.  
  4060.   /* Scan the input, create the output.  */
  4061.   rescan (&obuf, output_marks);
  4062.  
  4063.   /* Pop input stack to original state.  */
  4064.   --indepth;
  4065.  
  4066.   if (indepth != odepth)
  4067.     abort ();
  4068.  
  4069.   /* Record the output.  */
  4070.   obuf.length = obuf.bufp - obuf.buf;
  4071.  
  4072.   assertions_flag = save_assertions_flag;
  4073.   return obuf;
  4074. }
  4075.  
  4076. /*
  4077.  * Process a # directive.  Expects IP->bufp to point after the '#', as in
  4078.  * `#define foo bar'.  Passes to the command handler
  4079.  * (do_define, do_include, etc.): the addresses of the 1st and
  4080.  * last chars of the command (starting immediately after the #
  4081.  * keyword), plus op and the keyword table pointer.  If the command
  4082.  * contains comments it is copied into a temporary buffer sans comments
  4083.  * and the temporary buffer is passed to the command handler instead.
  4084.  * Likewise for backslash-newlines.
  4085.  *
  4086.  * Returns nonzero if this was a known # directive.
  4087.  * Otherwise, returns zero, without advancing the input pointer.
  4088.  */
  4089.  
  4090. static int
  4091. handle_directive (ip, op)
  4092.      FILE_BUF *ip, *op;
  4093. {
  4094.   register U_CHAR *bp, *cp;
  4095.   register struct directive *kt;
  4096.   register int ident_length;
  4097.   U_CHAR *resume_p;
  4098.  
  4099.   /* Nonzero means we must copy the entire command
  4100.      to get rid of comments or backslash-newlines.  */
  4101.   int copy_command = 0;
  4102.  
  4103.   U_CHAR *ident, *after_ident;
  4104.  
  4105.   bp = ip->bufp;
  4106.  
  4107.   /* Record where the directive started.  do_xifdef needs this.  */
  4108.   directive_start = bp - 1;
  4109.  
  4110.   /* Skip whitespace and \-newline.  */
  4111.   while (1) {
  4112.     if (is_hor_space[*bp]) {
  4113.       if ((*bp == '\f' || *bp == '\v') && pedantic)
  4114.     pedwarn ("%s in preprocessing directive",
  4115.          *bp == '\f' ? "formfeed" : "vertical tab");
  4116.       bp++;
  4117.     } else if (*bp == '/' && (bp[1] == '*'
  4118.           || (cplusplus_comments && bp[1] == '/'))) {
  4119.       ip->bufp = bp + 2;
  4120.  
  4121. #ifdef NEXT_CPP_SERVER
  4122.       skip_to_end_of_comment (ip, op, &ip->lineno, 0);
  4123. #else
  4124.       skip_to_end_of_comment (ip, &ip->lineno, 0);
  4125. #endif
  4126.       bp = ip->bufp;
  4127.     } else if (*bp == '\\' && bp[1] == '\n') {
  4128.       bp += 2; ip->lineno++;
  4129.     } else break;
  4130.   }
  4131.  
  4132.   /* Now find end of directive name.
  4133.      If we encounter a backslash-newline, exchange it with any following
  4134.      symbol-constituents so that we end up with a contiguous name.  */
  4135.  
  4136.   cp = bp;
  4137.   while (1) {
  4138.     if (is_idchar[*cp])
  4139.       cp++;
  4140.     else {
  4141.       if (*cp == '\\' && cp[1] == '\n')
  4142.     name_newline_fix (cp);
  4143.       if (is_idchar[*cp])
  4144.     cp++;
  4145.       else break;
  4146.     }
  4147.   }
  4148.   ident_length = cp - bp;
  4149.   ident = bp;
  4150.   after_ident = cp;
  4151.  
  4152.   /* A line of just `#' becomes blank.  */
  4153.  
  4154.   if (ident_length == 0 && *after_ident == '\n') {
  4155.     ip->bufp = after_ident;
  4156.     return 1;
  4157.   }
  4158.  
  4159.   if (ident_length == 0 || !is_idstart[*ident]) {
  4160.     U_CHAR *p = ident;
  4161.     while (is_idchar[*p]) {
  4162.       if (*p < '0' || *p > '9')
  4163.     break;
  4164.       p++;
  4165.     }
  4166.     /* Handle # followed by a line number.  */
  4167.     if (p != ident && !is_idchar[*p]) {
  4168.       static struct directive line_directive_table[] = {
  4169.     {  4, do_line, "line", T_LINE},
  4170.       };
  4171.       if (pedantic)
  4172.     pedwarn ("`#' followed by integer");
  4173.       after_ident = ident;
  4174.       kt = line_directive_table;
  4175.       goto old_linenum;
  4176.     }
  4177.  
  4178.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  4179.     if (p == ident) {
  4180.       while (*p == '#' || is_hor_space[*p]) p++;
  4181.       if (*p == '\n') {
  4182.     if (pedantic && !lang_asm)
  4183.       warning ("invalid preprocessor directive");
  4184.     return 0;
  4185.       }
  4186.     }
  4187.  
  4188.     if (!lang_asm)
  4189.       error ("invalid preprocessor directive name");
  4190.  
  4191.     return 0;
  4192.   }
  4193.  
  4194.   /*
  4195.    * Decode the keyword and call the appropriate expansion
  4196.    * routine, after moving the input pointer up to the next line.
  4197.    */
  4198.   for (kt = directive_table; kt->length > 0; kt++) {
  4199.     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
  4200.       register U_CHAR *buf;
  4201.       register U_CHAR *limit;
  4202.       int unterminated;
  4203.       int junk;
  4204.       int *already_output;
  4205.  
  4206.       /* Nonzero means do not delete comments within the directive.
  4207.      #define needs this when -traditional.  */
  4208.       int keep_comments;
  4209.  
  4210.     old_linenum:
  4211.  
  4212.       limit = ip->buf + ip->length;
  4213.       unterminated = 0;
  4214.       already_output = 0;
  4215.       keep_comments = traditional && kt->traditional_comments;
  4216. #ifndef NEXT_OBJC_RUNTIME
  4217.       /* #import is defined only in Objective C, or when on the NeXT.  */
  4218.       if (kt->type == T_IMPORT && !(objc || lookup ("__NeXT__", -1, -1)))
  4219.     break;
  4220. #endif
  4221.  
  4222.       /* Find the end of this command (first newline not backslashed
  4223.      and not in a string or comment).
  4224.      Set COPY_COMMAND if the command must be copied
  4225.      (it contains a backslash-newline or a comment).  */
  4226.  
  4227.       buf = bp = after_ident;
  4228.       while (bp < limit) {
  4229.     register U_CHAR c = *bp++;
  4230.     switch (c) {
  4231.     case '\\':
  4232.       if (bp < limit) {
  4233.         if (*bp == '\n') {
  4234.           ip->lineno++;
  4235.           copy_command = 1;
  4236.         }
  4237.         bp++;
  4238.       }
  4239.       break;
  4240.  
  4241.     case '\'':
  4242.     case '\"':
  4243.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated);
  4244.       /* Don't bother calling the directive if we already got an error
  4245.          message due to unterminated string.  Skip everything and pretend
  4246.          we called the directive.  */
  4247.       if (unterminated) {
  4248.         if (traditional) {
  4249.           /* Traditional preprocessing permits unterminated strings.  */
  4250.           ip->bufp = bp;
  4251.           goto endloop1;
  4252.         }
  4253.         ip->bufp = bp;
  4254.         return 1;
  4255.       }
  4256.       break;
  4257.  
  4258.       /* <...> is special for #include.  */
  4259.     case '<':
  4260.       if (!kt->angle_brackets)
  4261.         break;
  4262.       while (*bp && *bp != '>') bp++;
  4263.       break;
  4264.  
  4265.     case '/':
  4266.       if (*bp == '\\' && bp[1] == '\n')
  4267.         newline_fix (bp);
  4268.       if (*bp == '*'
  4269.           || (cplusplus_comments && *bp == '/')) {
  4270.         U_CHAR *obp = bp - 1;
  4271.         ip->bufp = bp + 1;
  4272. #ifdef NEXT_CPP_SERVER
  4273.         skip_to_end_of_comment (ip, op, &ip->lineno, 0);
  4274. #else
  4275.         skip_to_end_of_comment (ip, &ip->lineno, 0);
  4276. #endif
  4277.         bp = ip->bufp;
  4278.         /* No need to copy the command because of a comment at the end;
  4279.            just don't include the comment in the directive.  */
  4280.         if (bp == limit || *bp == '\n') {
  4281.           bp = obp;
  4282.           goto endloop1;
  4283.         }
  4284.         /* Don't remove the comments if -traditional.  */
  4285.         if (! keep_comments)
  4286.           copy_command++;
  4287.       }
  4288.       break;
  4289.  
  4290.     case '\f':
  4291.     case '\v':
  4292.       if (pedantic)
  4293.         pedwarn ("%s in preprocessing directive",
  4294.              c == '\f' ? "formfeed" : "vertical tab");
  4295.       break;
  4296.  
  4297.     case '\n':
  4298.       --bp;        /* Point to the newline */
  4299.       ip->bufp = bp;
  4300.       goto endloop1;
  4301.     }
  4302.       }
  4303.       ip->bufp = bp;
  4304.  
  4305.     endloop1:
  4306.       resume_p = ip->bufp;
  4307.       /* BP is the end of the directive.
  4308.      RESUME_P is the next interesting data after the directive.
  4309.      A comment may come between.  */
  4310.  
  4311.       /* If a directive should be copied through, and -E was given,
  4312.      pass it through before removing comments.  */
  4313.       if (!no_output && kt->pass_thru && put_out_comments) {
  4314.         int len;
  4315.  
  4316.     /* Output directive name.  */
  4317.         check_expand (op, kt->length + 2);
  4318.     /* Make sure # is at the start of a line */
  4319.     if (op->bufp > op->buf && op->bufp[-1] != '\n') {
  4320.       op->lineno++;
  4321.       *op->bufp++ = '\n';
  4322.     }
  4323.         *op->bufp++ = '#';
  4324.         bcopy (kt->name, op->bufp, kt->length);
  4325.         op->bufp += kt->length;
  4326.  
  4327.     /* Output arguments.  */
  4328.     len = (bp - buf);
  4329.     check_expand (op, len);
  4330.     bcopy (buf, (char *) op->bufp, len);
  4331.     op->bufp += len;
  4332.     /* Take account of any (escaped) newlines just output.  */
  4333.     while (--len >= 0)
  4334.       if (buf[len] == '\n')
  4335.         op->lineno++;
  4336.  
  4337.     already_output = &junk;
  4338.       }                /* Don't we need a newline or #line? */
  4339.  
  4340.       if (copy_command) {
  4341.     register U_CHAR *xp = buf;
  4342.     /* Need to copy entire command into temp buffer before dispatching */
  4343.  
  4344.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  4345.                           some slop */
  4346.     buf = cp;
  4347.  
  4348.     /* Copy to the new buffer, deleting comments
  4349.        and backslash-newlines (and whitespace surrounding the latter).  */
  4350.  
  4351.     while (xp < bp) {
  4352.       register U_CHAR c = *xp++;
  4353.       *cp++ = c;
  4354.  
  4355.       switch (c) {
  4356.       case '\n':
  4357.         abort ();  /* A bare newline should never part of the line.  */
  4358.         break;
  4359.  
  4360.         /* <...> is special for #include.  */
  4361.       case '<':
  4362.         if (!kt->angle_brackets)
  4363.           break;
  4364.         while (xp < bp && c != '>') {
  4365.           c = *xp++;
  4366.           if (c == '\\' && xp < bp && *xp == '\n')
  4367.         xp++;
  4368.           else
  4369.         *cp++ = c;
  4370.         }
  4371.         break;
  4372.  
  4373.       case '\\':
  4374.         if (*xp == '\n') {
  4375.           xp++;
  4376.           cp--;
  4377.           if (cp != buf && is_space[cp[-1]]) {
  4378.         while (cp != buf && is_space[cp[-1]]) cp--;
  4379.         cp++;
  4380.         SKIP_WHITE_SPACE (xp);
  4381.           } else if (is_space[*xp]) {
  4382.         *cp++ = *xp++;
  4383.         SKIP_WHITE_SPACE (xp);
  4384.           }
  4385.         } else {
  4386.           *cp++ = *xp++;
  4387.         }
  4388.         break;
  4389.  
  4390.       case '\'':
  4391.       case '\"':
  4392.         {
  4393.           register U_CHAR *bp1
  4394.         = skip_quoted_string (xp - 1, bp, ip->lineno,
  4395.                       NULL_PTR, NULL_PTR, NULL_PTR);
  4396.           while (xp != bp1)
  4397.         if (*xp == '\\') {
  4398.           if (*++xp != '\n')
  4399.             *cp++ = '\\';
  4400.           else
  4401.             xp++;
  4402.         } else
  4403.           *cp++ = *xp++;
  4404.         }
  4405.         break;
  4406.  
  4407.       case '/':
  4408.         if (*xp == '*'
  4409.         || (cplusplus_comments && *xp == '/')) {
  4410.           ip->bufp = xp + 1;
  4411.           /* If we already copied the command through,
  4412.          already_output != 0 prevents outputting comment now.  */
  4413. #ifdef NEXT_CPP_SERVER
  4414.           skip_to_end_of_comment (ip, op, already_output, 0);
  4415. #else
  4416.           skip_to_end_of_comment (ip, already_output, 0);
  4417. #endif
  4418.           if (keep_comments)
  4419.         while (xp != ip->bufp)
  4420.           *cp++ = *xp++;
  4421.           /* Delete or replace the slash.  */
  4422.           else if (traditional)
  4423.         cp--;
  4424.           else
  4425.         cp[-1] = ' ';
  4426.           xp = ip->bufp;
  4427.         }
  4428.       }
  4429.     }
  4430.  
  4431.     /* Null-terminate the copy.  */
  4432.  
  4433.     *cp = 0;
  4434.       } else
  4435.     cp = bp;
  4436.  
  4437.       ip->bufp = resume_p;
  4438.  
  4439.       /* Some directives should be written out for cc1 to process,
  4440.      just as if they were not defined.  And sometimes we're copying
  4441.      definitions through.  */
  4442.  
  4443.       if (!no_output && already_output == 0
  4444.       && (kt->pass_thru
  4445.           || (kt->type == T_DEFINE
  4446.           && (dump_macros == dump_names
  4447.               || dump_macros == dump_definitions)
  4448. #ifdef NEXT_CPP_SERVER
  4449.           && (!ip->file || !ip->file->header_output)
  4450. #endif              
  4451.               ))) {
  4452.         int len;
  4453.  
  4454.     /* Output directive name.  */
  4455.         check_expand (op, kt->length + 1);
  4456.         *op->bufp++ = '#';
  4457.         bcopy (kt->name, (char *) op->bufp, kt->length);
  4458.         op->bufp += kt->length;
  4459.  
  4460.     if (kt->pass_thru || dump_macros == dump_definitions) {
  4461.       /* Output arguments.  */
  4462.       len = (cp - buf);
  4463.       check_expand (op, len);
  4464.       bcopy (buf, (char *) op->bufp, len);
  4465.       op->bufp += len;
  4466.     } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
  4467.       U_CHAR *xp = buf;
  4468.       U_CHAR *yp;
  4469.       SKIP_WHITE_SPACE (xp);
  4470.       yp = xp;
  4471.       while (is_idchar[*xp]) xp++;
  4472.       len = (xp - yp);
  4473.       check_expand (op, len + 1);
  4474.       *op->bufp++ = ' ';
  4475.       bcopy (yp, op->bufp, len);
  4476.       op->bufp += len;
  4477.     }
  4478.       }                /* Don't we need a newline or #line? */
  4479.  
  4480.       /* Call the appropriate command handler.  buf now points to
  4481.      either the appropriate place in the input buffer, or to
  4482.      the temp buffer if it was necessary to make one.  cp
  4483.      points to the first char after the contents of the (possibly
  4484.      copied) command, in either case. */
  4485.       (*kt->func) (buf, cp, op, kt);
  4486. #ifdef NEXT_CPP_SERVER
  4487.       if (segregate_output
  4488.           && (ip->file && !ip->file->header_output)) {
  4489.          if (kt->func == do_include)
  4490.            ip->file->position_after_last_include = ip->bufp;
  4491.          else if (kt->func == do_if || kt->func == do_xifdef) {
  4492.        ip->file->has_explicit_context_dependency = 1;
  4493.        if (kt->type == T_IFNDEF && !ip->file->position_of_first_ifndef)
  4494.              ip->file->position_of_first_ifndef = directive_start;
  4495.          }
  4496.       }
  4497. #endif
  4498.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  4499.  
  4500.       return 1;
  4501.     }
  4502.   }
  4503.  
  4504.   /* It is deliberate that we don't warn about undefined directives.
  4505.      That is the responsibility of cc1.  */
  4506.   return 0;
  4507. }
  4508.  
  4509. static struct tm *
  4510. timestamp ()
  4511. {
  4512.   static struct tm *timebuf;
  4513.   if (!timebuf) {
  4514.     time_t t = time (0);
  4515.     timebuf = localtime (&t);
  4516.   }
  4517.   return timebuf;
  4518. }
  4519.  
  4520. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  4521.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  4522.                 };
  4523.  
  4524. /*
  4525.  * expand things like __FILE__.  Place the expansion into the output
  4526.  * buffer *without* rescanning.
  4527.  */
  4528.  
  4529. static void
  4530. special_symbol (hp, op)
  4531.      HASHNODE *hp;
  4532.      FILE_BUF *op;
  4533. {
  4534.   char *buf;
  4535.   int i, len;
  4536.   int true_indepth;
  4537.   FILE_BUF *ip = NULL;
  4538.   struct tm *timebuf;
  4539.  
  4540.   int paren = 0;        /* For special `defined' keyword */
  4541.  
  4542.   if (pcp_outfile && pcp_inside_if
  4543.       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
  4544.     error ("Predefined macro `%s' used inside `#if' during precompilation",
  4545.        hp->name);
  4546.     
  4547.   for (i = indepth; i >= 0; i--)
  4548.     if (instack[i].fname != NULL) {
  4549.       ip = &instack[i];
  4550.       break;
  4551.     }
  4552.   if (ip == NULL) {
  4553.     error ("cccp error: not in any file?!");
  4554.     return;            /* the show must go on */
  4555.   }
  4556.  
  4557.   switch (hp->type) {
  4558.   case T_FILE:
  4559.   case T_BASE_FILE:
  4560.     {
  4561.       char *string;
  4562.       if (hp->type == T_FILE)
  4563.     string = ip->nominal_fname;
  4564.       else
  4565.     string = instack[0].nominal_fname;
  4566.  
  4567.       if (string)
  4568.     {
  4569.       buf = (char *) alloca (3 + 4 * strlen (string));
  4570.       quote_string (buf, string);
  4571.     }
  4572.       else
  4573.     buf = "\"\"";
  4574.  
  4575.       break;
  4576.     }
  4577.  
  4578.   case T_INCLUDE_LEVEL:
  4579.     true_indepth = 0;
  4580.     for (i = indepth; i >= 0; i--)
  4581.       if (instack[i].fname != NULL)
  4582.         true_indepth++;
  4583.  
  4584.     buf = (char *) alloca (8);    /* Eight bytes ought to be more than enough */
  4585.     sprintf (buf, "%d", true_indepth - 1);
  4586.     break;
  4587.  
  4588.   case T_VERSION:
  4589.     buf = (char *) alloca (3 + strlen (version_string));
  4590.     sprintf (buf, "\"%s\"", version_string);
  4591.     break;
  4592.  
  4593. #ifndef NO_BUILTIN_SIZE_TYPE
  4594.   case T_SIZE_TYPE:
  4595.     buf = SIZE_TYPE;
  4596.     break;
  4597. #endif
  4598.  
  4599. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  4600.   case T_PTRDIFF_TYPE:
  4601.     buf = PTRDIFF_TYPE;
  4602.     break;
  4603. #endif
  4604.  
  4605.   case T_WCHAR_TYPE:
  4606.     buf = wchar_type;
  4607.     break;
  4608.  
  4609.   case T_USER_LABEL_PREFIX_TYPE:
  4610.     buf = USER_LABEL_PREFIX;
  4611.     break;
  4612.  
  4613.   case T_REGISTER_PREFIX_TYPE:
  4614.     buf = REGISTER_PREFIX;
  4615.     break;
  4616.  
  4617.   case T_CONST:
  4618.     buf = (char *) alloca (4 * sizeof (int));
  4619.     sprintf (buf, "%d", hp->value.ival);
  4620.     if (pcp_inside_if && pcp_outfile)
  4621.       /* Output a precondition for this macro use */
  4622.       fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
  4623.     break;
  4624.  
  4625.   case T_SPECLINE:
  4626.     buf = (char *) alloca (10);
  4627.     sprintf (buf, "%d", ip->lineno);
  4628.     break;
  4629.  
  4630.   case T_DATE:
  4631.   case T_TIME:
  4632.     buf = (char *) alloca (20);
  4633.     timebuf = timestamp ();
  4634.     if (hp->type == T_DATE)
  4635.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  4636.           timebuf->tm_mday, timebuf->tm_year + 1900);
  4637.     else
  4638.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  4639.           timebuf->tm_sec);
  4640.     break;
  4641.  
  4642.   case T_SPEC_DEFINED:
  4643.     buf = " 0 ";        /* Assume symbol is not defined */
  4644.     ip = &instack[indepth];
  4645.     SKIP_WHITE_SPACE (ip->bufp);
  4646.     if (*ip->bufp == '(') {
  4647.       paren++;
  4648.       ip->bufp++;            /* Skip over the paren */
  4649.       SKIP_WHITE_SPACE (ip->bufp);
  4650.     }
  4651.  
  4652.     if (!is_idstart[*ip->bufp])
  4653.       goto oops;
  4654.     if (hp = lookup (ip->bufp, -1, -1)) {
  4655.       if (pcp_outfile && pcp_inside_if
  4656.       && hp->value.defn->predefined)
  4657.     /* Output a precondition for this macro use. */
  4658.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  4659.       buf = " 1 ";
  4660.     }
  4661.     else
  4662.       if (pcp_outfile && pcp_inside_if)    {
  4663.     /* Output a precondition for this macro use */
  4664.     U_CHAR *cp = ip->bufp;
  4665.     fprintf (pcp_outfile, "#undef ");
  4666.     while (is_idchar[*cp]) /* Ick! */
  4667.       fputc (*cp++, pcp_outfile);
  4668.     putc ('\n', pcp_outfile);
  4669.       }
  4670.     while (is_idchar[*ip->bufp])
  4671.       ++ip->bufp;
  4672.     SKIP_WHITE_SPACE (ip->bufp);
  4673.     if (paren) {
  4674.       if (*ip->bufp != ')')
  4675.     goto oops;
  4676.       ++ip->bufp;
  4677.     }
  4678.     break;
  4679.  
  4680. oops:
  4681.  
  4682.     error ("`defined' without an identifier");
  4683.     break;
  4684.  
  4685.   default:
  4686.     error ("cccp error: invalid special hash type"); /* time for gdb */
  4687.     abort ();
  4688.   }
  4689.   len = strlen (buf);
  4690.   check_expand (op, len);
  4691.   bcopy (buf, (char *) op->bufp, len);
  4692.   op->bufp += len;
  4693.  
  4694.   return;
  4695. }
  4696.  
  4697. typedef enum {
  4698.    NON_EXISTENT_HEADER,
  4699.    GOOD_HEADER,
  4700.    REDUNDANT_HEADER
  4701. } header_type;
  4702.  
  4703. static header_type try_include_file(path, importing, pf, pimpfile, pstatbuf)
  4704. const char * path;
  4705. int importing;
  4706. int * pf;
  4707. struct import_file ** pimpfile;
  4708. struct stat * pstatbuf;
  4709. {
  4710.    /* lookup in negative cache...opens/stats are painfully slow */
  4711.    if (found_in_negative_cache(path)) {
  4712.       return NON_EXISTENT_HEADER;
  4713.    } else {
  4714.       struct import_file * impfile = 0;
  4715.       int f = -1;
  4716.       if (importing) {
  4717.          f = lookup_import (path, pstatbuf, &impfile); // returns -2 if impfile set
  4718.       } else {
  4719.          redundant_include_p (path, &impfile);
  4720.          if (impfile)
  4721.             f = -2;
  4722.          else
  4723.             f = open (path, O_RDONLY, 0666);
  4724.       }
  4725.       if (f == -2) {
  4726.          if ((importing && impfile->visited_within_module)
  4727.             || (!importing 
  4728.                 && ((!segregate_output && impfile->name->control_macro) 
  4729.                    || (segregate_output && impfile->name->control_macro 
  4730.                                         && impfile->visited_within_module))))
  4731.                return REDUNDANT_HEADER;
  4732.       }
  4733.       else if (f == -1) {
  4734.          add_to_negative_cache(savestring(path));
  4735.       }
  4736.       else if (!importing) { /* handle the "once idiom" */
  4737.          if (impfile && impfile->name->control_macro &&
  4738.              lookup (impfile->name->control_macro, -1, -1))
  4739.             return REDUNDANT_HEADER;
  4740.       }
  4741.       if (impfile) {
  4742.          *pf = f;
  4743.          *pimpfile = impfile;
  4744.          return GOOD_HEADER;
  4745.       }
  4746.       if (f >= 0) {
  4747.          if (!importing)
  4748.             fstat (f, pstatbuf);
  4749.          *pf = f;
  4750.          *pimpfile = impfile;
  4751.          return GOOD_HEADER;
  4752.       }
  4753.       return NON_EXISTENT_HEADER;
  4754.    }
  4755. }
  4756.  
  4757. /* Routines to handle #directives */
  4758.  
  4759. /* Handle #include and #import.
  4760.    This function expects to see "fname" or <fname> on the input.  */
  4761.  
  4762. static int
  4763. do_include (buf, limit, op, keyword)
  4764.      U_CHAR *buf, *limit;
  4765.      FILE_BUF *op;
  4766.      struct directive *keyword;
  4767. {
  4768.   int importing = (keyword->type == T_IMPORT);
  4769.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  4770. #ifdef NEXT_OBJC_RUNTIME
  4771.   static int import_warning = 1;
  4772. #else
  4773.   static int import_warning = 0;
  4774. #endif
  4775.   char *fname;        /* Dynamically allocated fname buffer */
  4776.   char *pcftry;
  4777.   char *pcfname;
  4778.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  4779. #ifdef NEXT_FRAMEWORKS
  4780.   char extendedPath[MAX_FILENAME_LEN];
  4781.   U_CHAR *bufptr;
  4782.   int hasSlash;    
  4783. #endif
  4784.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  4785.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  4786.   struct file_name_list *searchptr = 0;
  4787.   int flen;
  4788.  
  4789.   int f;            /* file number */
  4790.  
  4791.   int retried = 0;        /* Have already tried macro
  4792.                    expanding the include line*/
  4793.   FILE_BUF trybuf;        /* It got expanded into here */
  4794.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  4795.   int pcf = -1;
  4796.   char *pcfbuf;
  4797.   int pcfbuflimit;
  4798.   int pcfnum;
  4799. #ifdef NEXT_OBJC_RUNTIME
  4800.   U_CHAR *partial = NULL;
  4801.   int plen = 0;
  4802. #endif
  4803.   header_type hdr_result; 
  4804.   struct stat shared_stat_f;
  4805.   struct import_file *impfile = 0;
  4806.  
  4807.   f= -1;            /* JF we iz paranoid! */
  4808.  
  4809.   if (importing && warn_import && !inhibit_warnings
  4810.       && !instack[indepth].system_header_p && !import_warning) {
  4811.     import_warning = 1;
  4812.     warning ("using `#import' is not recommended");
  4813.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  4814.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  4815.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  4816.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  4817.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  4818.     fprintf (stderr, "  ... <real contents of file> ...\n");
  4819.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  4820.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  4821.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  4822.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  4823.   }
  4824.  
  4825. get_filename:
  4826.  
  4827.   fbeg = buf;
  4828.   SKIP_WHITE_SPACE (fbeg);
  4829.   /* Discard trailing whitespace so we can easily see
  4830.      if we have parsed all the significant chars we were given.  */
  4831.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  4832.  
  4833.   switch (*fbeg++) {
  4834.   case '\"':
  4835.     {
  4836.       FILE_BUF *fp;
  4837.       /* Copy the operand text, concatenating the strings.  */
  4838.       {
  4839.     U_CHAR *fin = fbeg;
  4840.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  4841.     fend = fbeg;
  4842.     while (fin != limit) {
  4843.       while (fin != limit && *fin != '\"')
  4844.         *fend++ = *fin++;
  4845.       fin++;
  4846.       if (fin == limit)
  4847.         break;
  4848.       /* If not at the end, there had better be another string.  */
  4849.       /* Skip just horiz space, and don't go past limit.  */
  4850.       while (fin != limit && is_hor_space[*fin]) fin++;
  4851.       if (fin != limit && *fin == '\"')
  4852.         fin++;
  4853.       else
  4854.         goto fail;
  4855.     }
  4856.       }
  4857.       *fend = 0;
  4858.  
  4859.       /* We have "filename".  Figure out directory this source
  4860.      file is coming from and put it on the front of the list. */
  4861.  
  4862.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  4863.       if (ignore_srcdir) break;
  4864.  
  4865.       for (fp = &instack[indepth]; fp >= instack; fp--)
  4866.     {
  4867.       int n;
  4868.       char *ep,*nam;
  4869.  
  4870.       if ((nam = fp->nominal_fname) != NULL) {
  4871.         /* Found a named file.  Figure out dir of the file,
  4872.            and put it in front of the search list.  */
  4873.         dsp[0].next = search_start;
  4874.         search_start = dsp;
  4875. #ifndef VMS
  4876.         ep = rindex (nam, '/');
  4877. #else                /* VMS */
  4878.         ep = rindex (nam, ']');
  4879.         if (ep == NULL) ep = rindex (nam, '>');
  4880.         if (ep == NULL) ep = rindex (nam, ':');
  4881.         if (ep != NULL) ep++;
  4882. #endif                /* VMS */,
  4883.         if (ep != NULL) {
  4884.           n = ep - nam;
  4885.           dsp[0].fname = (char *) alloca (n + 1);
  4886.           strncpy (dsp[0].fname, nam, n);
  4887.           dsp[0].fname[n] = '\0';
  4888.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  4889.         max_include_len = n + INCLUDE_LEN_FUDGE;
  4890.         } else {
  4891.           dsp[0].fname = 0; /* Current directory */
  4892.         }
  4893.         dsp[0].got_name_map = 0;
  4894.         break;
  4895.       }
  4896.     }
  4897.       break;
  4898.     }
  4899.  
  4900.   case '<':
  4901.     fend = fbeg;
  4902.     while (fend != limit && *fend != '>') fend++;
  4903.     if (*fend == '>' && fend + 1 == limit) {
  4904.       angle_brackets = 1;
  4905.       flen = fend -fbeg;
  4906. #ifdef NEXT_FRAMEWORKS
  4907.       hasSlash = 0;
  4908.       for (bufptr = fbeg; bufptr != fend; bufptr++)
  4909.           if (*bufptr == '/') {
  4910.              hasSlash++;
  4911.            break;
  4912.         }
  4913.        if (hasSlash) {
  4914.          extendedPath[0] = '\0';
  4915.          for (searchptr = framework;
  4916.               f < 0 && searchptr != 0;
  4917.               searchptr = searchptr->next) {
  4918.             U_CHAR *end, *headerNameTo, *headerNameFrom;
  4919.             int index;
  4920.             
  4921.             /* path to framework */
  4922.             strcpy(extendedPath, searchptr->fname);
  4923.             end = &extendedPath[strlen(extendedPath)];
  4924.             *end++ = '/';
  4925.  
  4926.             /* add framework directory */
  4927.             for (bufptr = fbeg;
  4928.                  *bufptr != '/' && bufptr != fend;
  4929.                  bufptr++)
  4930.                *end++ = *bufptr;
  4931.             strncpy(end, ".framework/", 11);   end += 11;
  4932.             
  4933.             for (index = 0; framework_header_dirs[index].dirName; index++) {
  4934.                /* add path to headers inside framework */
  4935.                strncpy(end,
  4936.                        framework_header_dirs[index].dirName,
  4937.                        framework_header_dirs[index].dirNameLen);
  4938.                /* add name of header */
  4939.                headerNameTo = end + framework_header_dirs[index].dirNameLen;
  4940.                for (headerNameFrom = bufptr; headerNameFrom != fend; headerNameFrom++)
  4941.                   *headerNameTo++ = *headerNameFrom;
  4942.                *headerNameTo = '\0';
  4943.                /* Try looking for the file */
  4944. #ifdef NEXT_CPP_SERVER
  4945.                hdr_result = try_include_file(extendedPath, importing, &f, &impfile, &shared_stat_f);
  4946.                if (hdr_result == REDUNDANT_HEADER)
  4947.                   return 0;
  4948.                else if (hdr_result == GOOD_HEADER)
  4949.                   break;
  4950. #if 0
  4951.                /* lookup in negative cache...opens/stats are painfully slow */
  4952.                if (!found_in_negative_cache(extendedPath)) {
  4953.                   impfile = 0;
  4954.                   if (importing)
  4955.                      f = lookup_import (extendedPath, &shared_stat_f, &impfile);
  4956.                   else {
  4957.                      redundant_include_p (extendedPath, &impfile);
  4958.                      if (!impfile)
  4959.                         f = open (extendedPath, O_RDONLY, 0666);
  4960.                      else
  4961.                         f = -2;
  4962.                   }
  4963.                   if (f == -2) {
  4964.                      if (segregate_output &&
  4965.                          impfile->visited_within_module &&
  4966.                          impfile->name &&
  4967.                          !impfile->name->has_explicit_context_dependency) {
  4968.                         return 0;         /* Already included this file */
  4969.                      }
  4970.                   }
  4971.                   else if (f == -1) {
  4972.                      add_to_negative_cache(savestring(extendedPath));
  4973.                   }
  4974.                   else if (!importing) { /* handle the "once idiom" */
  4975.                      if (impfile && impfile->name->control_macro &&
  4976.                          lookup (impfile->name->control_macro, -1, -1))
  4977.                         return 0;
  4978.                   }
  4979.                   if (f >= 0 || impfile) {
  4980.                      if (!importing)
  4981.                         fstat (f, &shared_stat_f);
  4982.                      break;
  4983.                   }
  4984.                } /* else, found in negative cache...don't bother */
  4985. #endif 0
  4986. #else
  4987.                if (importing)
  4988.                   f = lookup_import (extendedPath, searchptr);
  4989.                else
  4990.                   f = open_include_file (extendedPath, searchptr);
  4991.                if (f == -2)
  4992.                   return 0;            /* Already included this file */
  4993. #ifdef EACCES
  4994.                  else if (f == -1 && errno == EACCES)
  4995.                      warning ("Header file %s exists, but is not readable",
  4996.                               extendedPath);
  4997. #endif
  4998.                  if (redundant_include_p (extendedPath)) {
  4999.                      close (f);
  5000.                      return 0;
  5001.                  }
  5002.                  if (f >= 0)
  5003.                      break;
  5004. #endif
  5005.                  
  5006.              }
  5007.           }
  5008.       }
  5009. #endif
  5010.       /* If -I-, start with the first -I dir after the -I-.  */
  5011.       if (first_bracket_include)
  5012.           search_start = first_bracket_include;
  5013.       break;
  5014.     }
  5015.     goto fail;
  5016.  
  5017.   default:
  5018. #ifdef VMS
  5019.     /*
  5020.      * Support '#include xyz' like VAX-C to allow for easy use of all the
  5021.      * decwindow include files. It defaults to '#include <xyz.h>' (so the
  5022.      * code from case '<' is repeated here) and generates a warning.
  5023.      */
  5024.     if (isalpha(*(--fbeg))) {
  5025.       fend = fbeg;
  5026.       while (fend != limit && (!isspace(*fend))) fend++;
  5027.       warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
  5028.       if (fend  == limit) {
  5029.     angle_brackets = 1;
  5030.     /* If -I-, start with the first -I dir after the -I-.  */
  5031.     if (first_bracket_include)
  5032.       search_start = first_bracket_include;
  5033.     break;
  5034.       }
  5035.     }
  5036. #endif
  5037.  
  5038.   fail:
  5039.     if (retried) {
  5040.       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
  5041.       return 0;
  5042.     } else {
  5043.       trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  5044.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  5045.       bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  5046.       limit = buf + (trybuf.bufp - trybuf.buf);
  5047.       free (trybuf.buf);
  5048.       retried++;
  5049.       goto get_filename;
  5050.     }
  5051.   } /* switch (*fbeg++) */
  5052.  
  5053.   /* For #include_next, skip in the search path
  5054.      past the dir in which the containing file was found.  */
  5055.   if (skip_dirs) {
  5056.     FILE_BUF *fp;
  5057.     for (fp = &instack[indepth]; fp >= instack; fp--)
  5058.       if (fp->fname != NULL) {
  5059.     /* fp->dir is null if the containing file was specified
  5060.        with an absolute file name.  In that case, don't skip anything.  */
  5061.     if (fp->dir)
  5062.       search_start = fp->dir->next;
  5063.     break;
  5064.       }
  5065.   }
  5066.  
  5067.   flen = fend - fbeg;
  5068.  
  5069.   if (flen == 0)
  5070.     {
  5071.       error ("empty file name in `#%s'", keyword->name);
  5072.       return 0;
  5073.     }
  5074.  
  5075. #ifdef NEXT_FRAMEWORKS
  5076.   if (f > 0 || impfile) {
  5077.      fname = savestring(extendedPath);
  5078.      goto found_in_framework;
  5079.   }
  5080. #endif
  5081.  
  5082.   /* Allocate this permanently, because it gets stored in the definitions
  5083.      of macros.  */
  5084.   fname = (char *) xmalloc (max_include_len + flen + 4);
  5085.   /* + 2 above for slash and terminating null.  */
  5086.   /* + 2 added for '.h' on VMS (to support '#include filename') */
  5087.  
  5088.   
  5089.   /* If specified file name is absolute, just open it.  */
  5090.  
  5091.   if (*fbeg == '/') {
  5092.     strncpy (fname, fbeg, flen);
  5093.     fname[flen] = 0;
  5094. #ifdef NEXT_CPP_SERVER
  5095.     if (redundant_include_p (fname, &impfile)) {
  5096.       free(fname);
  5097.       return 0;
  5098.     }
  5099.     if (importing)
  5100.       f = lookup_import (fname, &shared_stat_f, &impfile);
  5101.     else {
  5102.       f = open (fname, O_RDONLY, 0666);
  5103.       fstat (f, &shared_stat_f);
  5104.     }
  5105. #else
  5106.     if (redundant_include_p (fname))
  5107.       return 0;
  5108.     if (importing)
  5109.       f = lookup_import (fname, NULL_PTR);
  5110.     else
  5111.       f = open_include_file (fname, NULL_PTR);
  5112. #endif
  5113.     if (f == -2) {
  5114.       free(fname);
  5115.       return 0;        /* Already included this file */
  5116.     }
  5117.   } else {
  5118.     /* Search directory path, trying to open the file.
  5119.        Copy each filename tried into FNAME.  */
  5120.  
  5121.       for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  5122.       if (searchptr->fname) {
  5123.     /* The empty string in a search path is ignored.
  5124.        This makes it possible to turn off entirely
  5125.        a standard piece of the list.  */
  5126.     if (searchptr->fname[0] == 0)
  5127.       continue;
  5128.     strcpy (fname, searchptr->fname);
  5129.     strcat (fname, "/");
  5130.     fname[strlen (fname) + flen] = 0;
  5131.       } else {
  5132.     fname[0] = 0;
  5133.       }
  5134.       strncat (fname, fbeg, flen);
  5135. #ifdef VMS
  5136.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  5137.          full VMS file specification */
  5138.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  5139.     /* Fix up the filename */
  5140.     hack_vms_include_specification (fname);
  5141.       } else {
  5142.           /* This is a normal VMS filespec, so use it unchanged.  */
  5143.     strncpy (fname, fbeg, flen);
  5144.     fname[flen] = 0;
  5145.     /* if it's '#include filename', add the missing .h */
  5146.     if (index(fname,'.')==NULL) {
  5147.       strcat (fname, ".h");
  5148.     }
  5149.       }
  5150. #endif /* VMS */
  5151. #ifdef NEXT_CPP_SERVER
  5152.       hdr_result = try_include_file(fname, importing, &f, &impfile, &shared_stat_f);
  5153.       if (hdr_result == REDUNDANT_HEADER) {
  5154.          free(fname);
  5155.          return 0;
  5156.       }
  5157.       else if (hdr_result == GOOD_HEADER)
  5158.          break;
  5159. #if 0
  5160.       /* lookup in negative cache...opens/stats are painfully slow */
  5161.       if (!found_in_negative_cache(fname)) {
  5162.         impfile = 0;
  5163.         if (importing)
  5164.       f = lookup_import (fname, &shared_stat_f, &impfile);
  5165.         else {
  5166.           redundant_include_p (fname, &impfile);
  5167.           if (!impfile)
  5168.         f = open (fname, O_RDONLY, 0666);
  5169.           else
  5170.              f = -2;
  5171.         }
  5172.         if (f == -2) {
  5173.           if (segregate_output &&
  5174.               impfile->visited_within_module &&
  5175.               impfile->name && !impfile->name->has_explicit_context_dependency) {
  5176.         free(fname);
  5177.         return 0;            /* Already included this file */
  5178.       }
  5179.         }
  5180.         else if (f == -1) {
  5181.           add_to_negative_cache(savestring(fname));
  5182.         }
  5183.         else if (!importing) { /* handle the "once idiom" */
  5184.           if (impfile && impfile->name->control_macro && 
  5185.               lookup (impfile->name->control_macro, -1, -1)) {
  5186.         free(fname);
  5187.         return 0;
  5188.       }
  5189.         }
  5190.         if (f >= 0 || impfile) {
  5191.           if (!importing)
  5192.             fstat (f, &shared_stat_f);
  5193.       break;
  5194.         }
  5195.       } /* else, found in negative cache...don't bother */
  5196. #endif 0
  5197. #else
  5198.       if (importing)
  5199.     f = lookup_import (fname, searchptr);
  5200.       else
  5201.     f = open_include_file (fname, searchptr);
  5202.       if (f == -2)
  5203.     return 0;            /* Already included this file */
  5204. #ifdef EACCES
  5205.       else if (f == -1 && errno == EACCES)
  5206.     warning ("Header file %s exists, but is not readable", fname);
  5207. #endif
  5208.       if (redundant_include_p (fname)) {
  5209.     close (f);
  5210.     return 0;
  5211.       }
  5212.       if (f >= 0)
  5213.     break;
  5214. #endif
  5215.     }
  5216.   }
  5217. #ifdef NEXT_FRAMEWORKS
  5218. found_in_framework:
  5219. #endif
  5220.      
  5221. #ifdef NEXT_CPP_SERVER
  5222.   if (f < 0 && !impfile) {
  5223. #else
  5224.   if (f < 0) {
  5225. #endif
  5226.     /* A file that was not found.  */
  5227.  
  5228.     strncpy (fname, fbeg, flen);
  5229.     fname[flen] = 0;
  5230.     /* If generating dependencies and -MG was specified, we assume missing
  5231.        files are leaf files, living in the same directory as the source file
  5232.        or other similar place; these missing files may be generated from
  5233.        other files and may not exist yet (eg: y.tab.h).  */
  5234.     if (print_deps_missing_files
  5235.     && print_deps > (angle_brackets || (system_include_depth > 0)))
  5236.       {
  5237.     /* If it was requested as a system header file,
  5238.        then assume it belongs in the first place to look for such.  */
  5239.     if (angle_brackets)
  5240.       {
  5241.         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
  5242.           {
  5243.         if (searchptr->fname)
  5244.           {
  5245.             char *p;
  5246.  
  5247.             if (searchptr->fname[0] == 0)
  5248.               continue;
  5249.             p = xmalloc (strlen (searchptr->fname)
  5250.                  + strlen (fname) + 2);
  5251.             strcpy (p, searchptr->fname);
  5252.             strcat (p, "/");
  5253.             strcat (p, fname);
  5254.             deps_output (p, ' ');
  5255.             break;
  5256.           }
  5257.           }
  5258.       }
  5259.     else
  5260.       {
  5261.         /* Otherwise, omit the directory, as if the file existed
  5262.            in the directory with the source.  */
  5263.         deps_output (fname, ' ');
  5264.       }
  5265.       }
  5266.     /* If -M was specified, and this header file won't be added to the
  5267.        dependency list, then don't count this as an error, because we can
  5268.        still produce correct output.  Otherwise, we can't produce correct
  5269.        output, because there may be dependencies we need inside the missing
  5270.        file, and we don't know what directory this missing file exists in.  */
  5271.     else if (print_deps
  5272.     && (print_deps <= (angle_brackets || (system_include_depth > 0))))
  5273.       warning ("No include path in which to find %s", fname);
  5274.     else if (search_start)
  5275.       error_from_errno (fname);
  5276.     else
  5277.       error ("No include path in which to find %s", fname);
  5278.   } else {
  5279.     struct stat stat_f;
  5280.  
  5281.     /* Check to see if this include file is a once-only include file.
  5282.        If so, give up.  */
  5283.  
  5284.     struct file_name_list* ptr;
  5285.  
  5286.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  5287.       if (!strcmp (ptr->fname, fname)) {
  5288.     close (f);
  5289.         return 0;                /* This file was once'd. */
  5290.       }
  5291.     }
  5292. #ifdef NEXT_CPP_SERVER
  5293.     if (impfile) {
  5294.       ptr = impfile->name;
  5295.     }
  5296. #else
  5297.     /* this is *very* slow operation when n gets large */
  5298.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  5299.       if (!strcmp (ptr->fname, fname))
  5300.         break;                /* This file was included before. */
  5301.     }
  5302. #endif
  5303.     if (ptr == 0) {
  5304.       /* This is the first time for this file.  */
  5305.       /* Add it to list of files included.  */
  5306.  
  5307.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  5308. #ifdef NEXT_CPP_SERVER
  5309.       bzero (ptr, sizeof (struct file_name_list));
  5310. #else
  5311.       ptr->control_macro = 0;
  5312.       ptr->c_system_include_path = 0;
  5313. #endif
  5314.       ptr->next = all_include_files;
  5315.       all_include_files = ptr;
  5316.       ptr->fname = savestring (fname);
  5317.       ptr->got_name_map = 0;
  5318.  
  5319.       /* For -M, add this file to the dependencies.  */
  5320.       if (print_deps > (angle_brackets || (system_include_depth > 0)))
  5321.     deps_output (fname, ' ');
  5322.     }   
  5323.  
  5324.     /* Handle -H option.  */
  5325.     if (print_include_names) {
  5326.       output_dots (stderr, indepth);
  5327.       fprintf (stderr, "%s\n", fname);
  5328.     }
  5329.  
  5330.     if (angle_brackets)
  5331.       system_include_depth++;
  5332.  
  5333. #ifdef NEXT_CPP_SERVER
  5334.     if (impfile) {
  5335.       /* this allows the hash table to be reused across several runs of cpp */
  5336.       impfile->visited_within_module++;
  5337.     } else
  5338.       /* Actually process the file.  */
  5339.       impfile = add_import (f, ptr, &shared_stat_f);
  5340.  
  5341.     if (!importing)
  5342.       ptr->ever_included = 1;
  5343. #else
  5344.     /* Actually process the file.  */
  5345.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  5346. #endif
  5347.  
  5348.     pcftry = (char *) alloca (strlen (fname) + 30);
  5349.     pcfbuf = 0;
  5350.     pcfnum = 0;
  5351.  
  5352. #ifdef NEXT_CPP_SERVER
  5353.     if (!no_precomp) {
  5354.       fstat (f, &stat_f);
  5355. #else
  5356.     fstat (f, &stat_f);
  5357.     if (!no_precomp)
  5358. #endif
  5359.       do {
  5360.     sprintf (pcftry, "%s%d", fname, pcfnum++);
  5361.     
  5362.     pcf = open (pcftry, O_RDONLY, 0666);
  5363.     if (pcf != -1)
  5364.       {
  5365.         struct stat s;
  5366.  
  5367.         fstat (pcf, &s);
  5368.         if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
  5369.               sizeof (s.st_ino))
  5370.         || stat_f.st_dev != s.st_dev)
  5371.           {
  5372.         pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  5373.         /* Don't need it any more.  */
  5374.         close (pcf);
  5375.           }
  5376.         else
  5377.           {
  5378.         /* Don't need it at all.  */
  5379.         close (pcf);
  5380.         break;
  5381.           }
  5382.       }
  5383.       } while (pcf != -1 && !pcfbuf);
  5384. #ifdef NEXT_CPP_SERVER
  5385.     }
  5386.     /* Actually process the file */
  5387.     if (pcfbuf) {
  5388.       pcfname = xmalloc (strlen (pcftry) + 1);
  5389.       strcpy (pcfname, pcftry);
  5390.       pcfinclude (pcfbuf, pcfbuflimit, ptr->fname, op);
  5391.     }
  5392.     else {
  5393.       finclude (f, ptr->fname, op, is_system_include (ptr->fname), searchptr, 
  5394.                 ptr, &shared_stat_f);
  5395.     }
  5396. #else
  5397.     /* Actually process the file */
  5398.     if (pcfbuf) {
  5399.       pcfname = xmalloc (strlen (pcftry) + 1);
  5400.       strcpy (pcfname, pcftry);
  5401.       pcfinclude (pcfbuf, pcfbuflimit, fname, op);
  5402.     }
  5403.     else
  5404.       finclude (f, fname, op, is_system_include (fname), searchptr);
  5405. #endif
  5406.  
  5407.     if (angle_brackets)
  5408.       system_include_depth--;
  5409.   }
  5410.  
  5411.   free (fname); 
  5412.  
  5413.   return 0;
  5414. }
  5415.  
  5416. /* Return nonzero if there is no need to include file NAME
  5417.    because it has already been included and it contains a conditional
  5418.    to make a repeated include do nothing.  */
  5419.  
  5420. #ifdef NEXT_CPP_SERVER
  5421. static int
  5422. redundant_include_p (name, impfile)
  5423.      char *name;
  5424.      struct import_file **impfile; /* output parm if found */
  5425. {
  5426.   struct import_file *i;
  5427.   int hashval = import_hash (name);
  5428.  
  5429.   /* Attempt to find file in list of already included files */
  5430.   i = import_hash_table[hashval];
  5431.  
  5432.   while (i) {
  5433.     if (!strcmp (name, i->name->fname)) {
  5434.       *impfile = i;
  5435.       if (i->name->control_macro && lookup (i->name->control_macro, -1, -1))
  5436.         return 1;
  5437.       else
  5438.         return 0;
  5439.     }
  5440.     i = i->next;
  5441.   }
  5442.   return 0;
  5443. }
  5444. #else
  5445. static int
  5446. redundant_include_p (name)
  5447.      char *name;
  5448. {
  5449.   struct file_name_list *l = all_include_files;
  5450.   for (; l; l = l->next)
  5451.     if (! strcmp (name, l->fname)
  5452.     && l->control_macro
  5453.     && lookup (l->control_macro, -1, -1))
  5454.       return 1;
  5455.   return 0;
  5456. }
  5457. #endif
  5458.  
  5459. /* Return nonzero if the given FILENAME is an absolute pathname which
  5460.    designates a file within one of the known "system" include file
  5461.    directories.  We assume here that if the given FILENAME looks like
  5462.    it is the name of a file which resides either directly in a "system"
  5463.    include file directory, or within any subdirectory thereof, then the
  5464.    given file must be a "system" include file.  This function tells us
  5465.    if we should suppress pedantic errors/warnings for the given FILENAME.
  5466.  
  5467.    The value is 2 if the file is a C-language system header file
  5468.    for which C++ should (on most systems) assume `extern "C"'.  */
  5469.  
  5470. static int
  5471. is_system_include (filename)
  5472.     register char *filename;
  5473. {
  5474.   struct file_name_list *searchptr;
  5475.  
  5476.   for (searchptr = first_system_include; searchptr;
  5477.        searchptr = searchptr->next)
  5478.     if (searchptr->fname) {
  5479.       register char *sys_dir = searchptr->fname;
  5480.       register unsigned length = strlen (sys_dir);
  5481.  
  5482.       if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
  5483.     {
  5484.       if (searchptr->c_system_include_path)
  5485.         return 2;
  5486.       else
  5487.         return 1;
  5488.     }
  5489.     }
  5490.   return 0;
  5491. }
  5492.  
  5493. /* The file_name_map structure holds a mapping of file names for a
  5494.    particular directory.  This mapping is read from the file named
  5495.    FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
  5496.    map filenames on a file system with severe filename restrictions,
  5497.    such as DOS.  The format of the file name map file is just a series
  5498.    of lines with two tokens on each line.  The first token is the name
  5499.    to map, and the second token is the actual name to use.  */
  5500.  
  5501. struct file_name_map
  5502. {
  5503.   struct file_name_map *map_next;
  5504.   char *map_from;
  5505.   char *map_to;
  5506. };
  5507.  
  5508. #define FILE_NAME_MAP_FILE "header.gcc"
  5509.  
  5510. /* Read a space delimited string of unlimited length from a stdio
  5511.    file.  */
  5512.  
  5513. static char *
  5514. read_filename_string (ch, f)
  5515.      int ch;
  5516.      FILE *f;
  5517. {
  5518.   char *alloc, *set;
  5519.   int len;
  5520.  
  5521.   len = 20;
  5522.   set = alloc = xmalloc (len + 1);
  5523.   if (! is_space[ch])
  5524.     {
  5525.       *set++ = ch;
  5526.       while ((ch = getc (f)) != EOF && ! is_space[ch])
  5527.     {
  5528.       if (set - alloc == len)
  5529.         {
  5530.           len *= 2;
  5531.           alloc = xrealloc (alloc, len + 1);
  5532.           set = alloc + len / 2;
  5533.         }
  5534.       *set++ = ch;
  5535.     }
  5536.     }
  5537.   *set = '\0';
  5538.   ungetc (ch, f);
  5539.   return alloc;
  5540. }
  5541.  
  5542. /* Read the file name map file for DIRNAME.  */
  5543.  
  5544. static struct file_name_map *
  5545. read_name_map (dirname)
  5546.      char *dirname;
  5547. {
  5548.   /* This structure holds a linked list of file name maps, one per
  5549.      directory.  */
  5550.   struct file_name_map_list
  5551.     {
  5552.       struct file_name_map_list *map_list_next;
  5553.       char *map_list_name;
  5554.       struct file_name_map *map_list_map;
  5555.     };
  5556.   static struct file_name_map_list *map_list;
  5557.   register struct file_name_map_list *map_list_ptr;
  5558.   char *name;
  5559.   FILE *f;
  5560.  
  5561.   for (map_list_ptr = map_list; map_list_ptr;
  5562.        map_list_ptr = map_list_ptr->map_list_next)
  5563.     if (! strcmp (map_list_ptr->map_list_name, dirname))
  5564.       return map_list_ptr->map_list_map;
  5565.  
  5566.   map_list_ptr = ((struct file_name_map_list *)
  5567.           xmalloc (sizeof (struct file_name_map_list)));
  5568.   map_list_ptr->map_list_name = savestring (dirname);
  5569.   map_list_ptr->map_list_map = NULL;
  5570.  
  5571.   name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
  5572.   strcpy (name, dirname);
  5573.   if (*dirname)
  5574.     strcat (name, "/");
  5575.   strcat (name, FILE_NAME_MAP_FILE);
  5576.   f = fopen (name, "r");
  5577.   if (!f)
  5578.     map_list_ptr->map_list_map = NULL;
  5579.   else
  5580.     {
  5581.       int ch;
  5582.       int dirlen = strlen (dirname);
  5583.  
  5584.       while ((ch = getc (f)) != EOF)
  5585.     {
  5586.       char *from, *to;
  5587.       struct file_name_map *ptr;
  5588.  
  5589.       if (is_space[ch])
  5590.         continue;
  5591.       from = read_filename_string (ch, f);
  5592.       while ((ch = getc (f)) != EOF && is_hor_space[ch])
  5593.         ;
  5594.       to = read_filename_string (ch, f);
  5595.  
  5596.       ptr = ((struct file_name_map *)
  5597.          xmalloc (sizeof (struct file_name_map)));
  5598.       ptr->map_from = from;
  5599.  
  5600.       /* Make the real filename absolute.  */
  5601.       if (*to == '/')
  5602.         ptr->map_to = to;
  5603.       else
  5604.         {
  5605.           ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
  5606.           strcpy (ptr->map_to, dirname);
  5607.           ptr->map_to[dirlen] = '/';
  5608.           strcpy (ptr->map_to + dirlen + 1, to);
  5609.           free (to);
  5610.         }          
  5611.  
  5612.       ptr->map_next = map_list_ptr->map_list_map;
  5613.       map_list_ptr->map_list_map = ptr;
  5614.  
  5615.       while ((ch = getc (f)) != '\n')
  5616.         if (ch == EOF)
  5617.           break;
  5618.     }
  5619.       fclose (f);
  5620.     }
  5621.   
  5622.   map_list_ptr->map_list_next = map_list;
  5623.   map_list = map_list_ptr;
  5624.  
  5625.   return map_list_ptr->map_list_map;
  5626. }  
  5627.  
  5628. /* Try to open include file FILENAME.  SEARCHPTR is the directory
  5629.    being tried from the include file search path.  This function maps
  5630.    filenames on file systems based on information read by
  5631.    read_name_map.  */
  5632.  
  5633. static int
  5634. open_include_file (filename, searchptr)
  5635.      char *filename;
  5636.      struct file_name_list *searchptr;
  5637. {
  5638.   register struct file_name_map *map;
  5639.   register char *from;
  5640.   char *p, *dir;
  5641.  
  5642.   if (searchptr && ! searchptr->got_name_map)
  5643.     {
  5644.       searchptr->name_map = read_name_map (searchptr->fname
  5645.                        ? searchptr->fname : ".");
  5646.       searchptr->got_name_map = 1;
  5647.     }
  5648.  
  5649.   /* First check the mapping for the directory we are using.  */
  5650.   if (searchptr && searchptr->name_map)
  5651.     {
  5652.       from = filename;
  5653.       if (searchptr->fname)
  5654.     from += strlen (searchptr->fname) + 1;
  5655.       for (map = searchptr->name_map; map; map = map->map_next)
  5656.     {
  5657.       if (! strcmp (map->map_from, from))
  5658.         {
  5659.           /* Found a match.  */
  5660.           return open (map->map_to, O_RDONLY, 0666);
  5661.         }
  5662.     }
  5663.     }
  5664.  
  5665.   /* Try to find a mapping file for the particular directory we are
  5666.      looking in.  Thus #include <sys/types.h> will look up sys/types.h
  5667.      in /usr/include/header.gcc and look up types.h in
  5668.      /usr/include/sys/header.gcc.  */
  5669.   p = rindex (filename, '/');
  5670.   if (! p)
  5671.     p = filename;
  5672.   if (searchptr
  5673.       && searchptr->fname
  5674.       && strlen (searchptr->fname) == p - filename
  5675.       && ! strncmp (searchptr->fname, filename, p - filename))
  5676.     {
  5677.       /* FILENAME is in SEARCHPTR, which we've already checked.  */
  5678.       return open (filename, O_RDONLY, 0666);
  5679.     }
  5680.  
  5681.   if (p == filename)
  5682.     {
  5683.       dir = ".";
  5684.       from = filename;
  5685.     }
  5686.   else
  5687.     {
  5688.       dir = (char *) alloca (p - filename + 1);
  5689.       bcopy (filename, dir, p - filename);
  5690.       dir[p - filename] = '\0';
  5691.       from = p + 1;
  5692.     }
  5693.   for (map = read_name_map (dir); map; map = map->map_next)
  5694.     if (! strcmp (map->map_from, from))
  5695.       return open (map->map_to, O_RDONLY, 0666);
  5696.  
  5697.   return open (filename, O_RDONLY, 0666);
  5698. }
  5699.  
  5700. /* Process the contents of include file FNAME, already open on descriptor F,
  5701.    with output to OP.
  5702.    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
  5703.    "system" include directories (as decided by the `is_system_include'
  5704.    function above).
  5705.    DIRPTR is the link in the dir path through which this file was found,
  5706.    or 0 if the file name was absolute.  */
  5707.  
  5708. #ifdef NEXT_CPP_SERVER
  5709. static void
  5710. finclude (f, fname, op, system_header_p, dirptr, fileptr, sb)
  5711.      int f;
  5712.      char *fname;
  5713.      FILE_BUF *op;
  5714.      int system_header_p;
  5715.      struct file_name_list *dirptr;
  5716.      struct file_name_list *fileptr;
  5717.      struct stat *sb;
  5718. #else
  5719. static void
  5720. finclude (f, fname, op, system_header_p, dirptr)
  5721.      int f;
  5722.      char *fname;
  5723.      FILE_BUF *op;
  5724.      int system_header_p;
  5725.      struct file_name_list *dirptr;
  5726. #endif
  5727. {
  5728.   int st_mode;
  5729.   long st_size;
  5730.   long i;
  5731.   FILE_BUF *fp;            /* For input stack frame */
  5732.   int missing_newline = 0;
  5733. #ifdef NEXT_CPP_SERVER
  5734.   int check_for_duplicate = 0;
  5735.   U_CHAR *position_of_file_start;
  5736.   unsigned int file_length;
  5737. #ifdef SEPERATE_USER_AND_SYS_HEADERS
  5738.   FILE_BUF * header_buf = system_header_p ? &sys_header_outbuf
  5739.                                           : &user_header_outbuf;
  5740. #else
  5741.   FILE_BUF * header_buf = &sys_header_outbuf;
  5742. #endif
  5743. #endif
  5744.  
  5745.   CHECK_DEPTH (return;);
  5746.  
  5747. #ifdef NEXT_CPP_SERVER
  5748.   if (sb == 0) {
  5749.     if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  5750.       goto nope;        /* Impossible? */
  5751.   } else { /* don't redo any stat stuff */
  5752.     st_mode = sb->st_mode;
  5753.     st_size = sb->st_size;
  5754.   }
  5755. #else
  5756.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  5757.     {
  5758.       perror_with_name (fname);
  5759.       close (f);
  5760.       return;
  5761.     }
  5762. #endif
  5763.   fp = &instack[indepth + 1];
  5764.   bzero ((char *) fp, sizeof (FILE_BUF));
  5765.   fp->nominal_fname = fp->fname = fname;
  5766.   fp->length = 0;
  5767.   fp->lineno = 1;
  5768.   fp->if_stack = if_stack;
  5769.   fp->system_header_p = system_header_p;
  5770.   fp->dir = dirptr;
  5771. #ifdef NEXT_CPP_SERVER
  5772.   fp->file = fileptr;
  5773.   if (fileptr && fileptr->buf && fileptr->length) {
  5774.     /*fprintf(stderr, "reusing buffer for file %s\n", fileptr->fname);*/
  5775.     fp->buf = fileptr->buf;
  5776.     fp->length = fileptr->length;
  5777.     if (fileptr->control_macro && fileptr->position_of_first_ifndef)
  5778.       /* simple optimization to avoid processing superfluous comments
  5779.          at the top of headers (60k savings for appkit.h) */
  5780.       fp->bufp = fileptr->position_of_first_ifndef;
  5781.     else
  5782.       fp->bufp = fileptr->buf;
  5783.     goto dorescan;
  5784.   }
  5785. #endif
  5786.   if (S_ISREG (st_mode)) {
  5787. #ifdef MALLOC_INPUT_BUFFERS
  5788.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  5789.     fp->bufp = fp->buf;
  5790.  
  5791.     /* Read the file contents, knowing that st_size is an upper bound
  5792.        on the number of bytes we can read.  */
  5793.     fp->length = safe_read (f, fp->buf, st_size);
  5794. #else
  5795.     if (map_fd(f, 0, &fp->buf, TRUE, st_size+2) != KERN_SUCCESS)
  5796.        goto nope;
  5797.     fp->bufp = fp->buf;
  5798.     fp->length = st_size;    
  5799. #endif    
  5800.     if (fp->length < 0) goto nope;
  5801.   }
  5802.   else if (S_ISDIR (st_mode)) {
  5803.     error ("directory `%s' specified in #include", fname);
  5804.     close (f);
  5805.     return;
  5806.   } else {
  5807.     /* Cannot count its file size before reading.
  5808.        First read the entire file into heap and
  5809.        copy them into buffer on stack. */
  5810.  
  5811.     int bsize = 2000;
  5812.  
  5813.     st_size = 0;
  5814.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  5815.  
  5816.     for (;;) {
  5817.       i = safe_read (f, fp->buf + st_size, bsize - st_size);
  5818.       if (i < 0)
  5819.     goto nope;      /* error! */
  5820.       st_size += i;
  5821.       if (st_size != bsize)
  5822.     break;    /* End of file */
  5823.       bsize *= 2;
  5824.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  5825.     }
  5826.     fp->bufp = fp->buf;
  5827.     fp->length = st_size;
  5828.   }
  5829.  
  5830.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  5831.       /* Backslash-newline at end is not good enough.  */
  5832.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  5833.     fp->buf[fp->length++] = '\n';
  5834.     missing_newline = 1;
  5835.   }
  5836.   fp->buf[fp->length] = '\0';
  5837.  
  5838.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  5839.   close (f);
  5840.  
  5841.   /* Must do this before calling trigraph_pcp, so that the correct file name
  5842.      will be printed in warning messages.  */
  5843. #ifdef NEXT_CPP_SERVER
  5844. dorescan:
  5845.  
  5846.   if (fileptr) {
  5847.      fileptr->buf = fp->buf;
  5848.      fileptr->length = fp->length;
  5849.   }
  5850. #endif
  5851.  
  5852.   indepth++;
  5853.   input_file_stack_tick++;
  5854.  
  5855.   if (!no_trigraphs)
  5856.     trigraph_pcp (fp);
  5857.  
  5858. #ifdef NEXT_SEMANTICS
  5859.   if (!no_rtf)
  5860.     buf_convert_rtf (fp);
  5861. #endif
  5862.  
  5863. #ifdef NEXT_CPP_SERVER
  5864.   if (segregate_output) {
  5865.     output_line_command (fp, &outbuf, 0, enter_file); /* serve as a "note" */
  5866.  
  5867.     /* the following is a conservative heuristic to prevent us from
  5868.        over zealous removal of headers that need to be output more than once */
  5869.     if (fileptr->header_output &&
  5870.         fileptr->has_explicit_context_dependency &&
  5871.         (fileptr->control_macro == 0 && fileptr->ever_included)) {
  5872.       check_for_duplicate = 1;
  5873.       fileptr->header_output = 0; /* has the effect of including it again */
  5874.     }
  5875.     position_of_file_start = header_buf->bufp;
  5876.     output_line_command (fp, header_buf, 0, enter_file);
  5877.     rescan (header_buf, 0);
  5878.   } else {
  5879.     output_line_command (fp, op, 0, enter_file);
  5880.     rescan (op, 0);
  5881.   }
  5882. #else
  5883.   output_line_command (fp, op, 0, enter_file);
  5884.   rescan (op, 0);
  5885. #endif
  5886.  
  5887.   if (missing_newline)
  5888.     fp->lineno--;
  5889.  
  5890.   if (pedantic && missing_newline)
  5891.     pedwarn ("file does not end in newline");
  5892.  
  5893.   indepth--;
  5894.   input_file_stack_tick++;
  5895.  
  5896. #ifdef NEXT_CPP_SERVER
  5897.   if (segregate_output) {
  5898.  
  5899.     /* It is important to compute the length of what we just output now, since
  5900.        the length should NOT include the next end marker, which includes the
  5901.        file we are returning to */
  5902.     file_length = header_buf->bufp - position_of_file_start;
  5903.  
  5904.     if (indepth)
  5905.        output_line_command (&instack[indepth], header_buf, 0, leave_file);
  5906.     else
  5907.        output_line_command (&instack[indepth], &outbuf, 0, enter_file);
  5908.  
  5909.     if (check_for_duplicate) { 
  5910.       /* file was included twice, if it is identical remove it */
  5911.       if (duplicate_version_of_header(header_buf,
  5912.              fileptr, position_of_file_start, file_length)) {
  5913.         header_buf->bufp = position_of_file_start; /* remove it */
  5914.       } else {
  5915.         add_version_of_header(fileptr, 
  5916.                         position_of_file_start - header_buf->buf,
  5917.                         file_length);
  5918.       }
  5919.       fileptr->header_output = 1; /* put back the original value */
  5920.     } else if (!fileptr->header_output) {
  5921.       /* first time this header has been output */
  5922.       fileptr->relative_position_of_file_start = 
  5923.           position_of_file_start - header_buf->buf;
  5924.       fileptr->file_length = file_length;
  5925.       fileptr->header_output = 1; 
  5926.     }
  5927.   } else {
  5928.     output_line_command (&instack[indepth], op, 0, leave_file);
  5929.   }
  5930. #else
  5931.   output_line_command (&instack[indepth], op, 0, leave_file);
  5932.   free (fp->buf);
  5933. #endif
  5934.   return;
  5935.  
  5936.  nope:
  5937.  
  5938.   perror_with_name (fname);
  5939.   close (f);
  5940.   free (fp->buf);
  5941. }
  5942.  
  5943. /* Record that inclusion of the file named FILE
  5944.    should be controlled by the macro named MACRO_NAME.
  5945.    This means that trying to include the file again
  5946.    will do something if that macro is defined.  */
  5947.  
  5948. #ifdef NEXT_CPP_SERVER
  5949. static void
  5950. record_control_macro (file, macro_name)
  5951.      char *file;
  5952.      U_CHAR *macro_name;
  5953. {
  5954.   struct import_file *impfile;
  5955.  
  5956.   redundant_include_p(file, &impfile);
  5957.   if (impfile) {
  5958.      if (impfile->name->control_macro)
  5959.         free(impfile->name->control_macro);
  5960.     impfile->name->control_macro = macro_name;
  5961.     impfile->name->position_of_last_endif = directive_start;
  5962.     return;
  5963.   }
  5964.  
  5965.   /* If the file is not in all_include_files, something's wrong.  */
  5966.   abort ();
  5967. }
  5968. #else
  5969. static void
  5970. record_control_macro (file, macro_name)
  5971.      char *file;
  5972.      U_CHAR *macro_name;
  5973. {
  5974.   struct file_name_list *new;
  5975.  
  5976.   for (new = all_include_files; new; new = new->next) {
  5977.     if (!strcmp (new->fname, file)) {
  5978.       new->control_macro = macro_name;
  5979.       return;
  5980.     }
  5981.   }
  5982.  
  5983.   /* If the file is not in all_include_files, something's wrong.  */
  5984.   abort ();
  5985. }
  5986. #endif
  5987.  
  5988. /* Hash a file name for import_hash_table.  */
  5989.  
  5990. static int 
  5991. import_hash (f)
  5992.      char *f;
  5993. {
  5994.   int val = 0;
  5995.  
  5996.   while (*f) val += *f++;
  5997.   return (val%IMPORT_HASH_SIZE);
  5998. }
  5999.  
  6000. /* Search for file FILENAME in import_hash_table.
  6001.    Return -2 if found, either a matching name or a matching inode.
  6002.    Otherwise, open the file and return a file descriptor if successful
  6003.    or -1 if unsuccessful.  */
  6004.  
  6005. #ifdef NEXT_CPP_SERVER
  6006. static int
  6007. lookup_import (filename, sb, impfile)
  6008.      char *filename;
  6009.      struct stat *sb;
  6010.      struct import_file **impfile; /* output parm if found */
  6011. {
  6012.   struct import_file *i;
  6013.   int h;
  6014.   int hashval;
  6015.   int fd;
  6016.  
  6017.   hashval = import_hash (filename);
  6018.  
  6019.   /* Attempt to find file in list of already included files */
  6020.   i = import_hash_table[hashval];
  6021.  
  6022.   while (i) {
  6023.     if (!strcmp (filename, i->name->fname)) {
  6024.       *impfile = i;
  6025.       return -2;        /* return found */
  6026.     }
  6027.     i = i->next;
  6028.   }
  6029.   /* Open it and try a match on inode/dev */
  6030.   fd = open (filename, O_RDONLY, 0666);
  6031.   if (fd < 0)
  6032.     return fd;
  6033.   fstat (fd, sb);
  6034.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  6035.     i = import_hash_table[h];
  6036.     while (i) {
  6037.       /* Compare the inode and the device.
  6038.      Supposedly on some systems the inode is not a scalar.  */
  6039.       if (i->inode == sb->st_ino && i->dev == sb->st_dev) {
  6040.         close (fd);
  6041.     *impfile = i;
  6042.         return -2;        /* return found */
  6043.       }
  6044.       i = i->next;
  6045.     }
  6046.   }
  6047.   return fd;            /* Not found, return open file */
  6048. }
  6049. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  6050.  
  6051. static struct import_file *
  6052. add_import (fd, fnamelist, sb)
  6053.      int fd;
  6054.      struct file_name_list *fnamelist;
  6055.      struct stat *sb;
  6056. {
  6057.   struct import_file *i;
  6058.   int hashval;
  6059.  
  6060.   hashval = import_hash (fnamelist->fname);
  6061.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  6062.   i->name = fnamelist;
  6063.   bcopy (&sb->st_ino, &i->inode, sizeof (sb->st_ino));
  6064.   i->modtime = sb->st_mtime;
  6065.   i->dev = sb->st_dev;
  6066.   i->next = import_hash_table[hashval];
  6067.   i->visited_within_module = 1;
  6068.   import_hash_table[hashval] = i;
  6069.   return i;
  6070. }
  6071.  
  6072. #else
  6073. static int
  6074. lookup_import (filename, searchptr)
  6075.      char *filename;
  6076.      struct file_name_list *searchptr;
  6077. {
  6078.   struct import_file *i;
  6079.   int h;
  6080.   int hashval;
  6081.   struct stat sb;
  6082.   int fd;
  6083.  
  6084.   hashval = import_hash (filename);
  6085.  
  6086.   /* Attempt to find file in list of already included files */
  6087.   i = import_hash_table[hashval];
  6088.  
  6089.   while (i) {
  6090.     if (!strcmp (filename, i->name))
  6091.       return -2;        /* return found */
  6092.     i = i->next;
  6093.   }
  6094.   /* Open it and try a match on inode/dev */
  6095.   fd = open_include_file (filename, searchptr);
  6096.   if (fd < 0)
  6097.     return fd;
  6098.   fstat (fd, &sb);
  6099.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  6100.     i = import_hash_table[h];
  6101.     while (i) {
  6102.       /* Compare the inode and the device.
  6103.      Supposedly on some systems the inode is not a scalar.  */
  6104.       if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
  6105.       && i->dev == sb.st_dev) {
  6106.         close (fd);
  6107.         return -2;        /* return found */
  6108.       }
  6109.       i = i->next;
  6110.     }
  6111.   }
  6112.   return fd;            /* Not found, return open file */
  6113. }
  6114.  
  6115. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  6116.  
  6117. static void
  6118. add_import (fd, fname)
  6119.      int fd;
  6120.      char *fname;
  6121. {
  6122.   struct import_file *i;
  6123.   int hashval;
  6124.   struct stat sb;
  6125.  
  6126.   hashval = import_hash (fname);
  6127.   fstat (fd, &sb);
  6128.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  6129.   i->name = (char *)xmalloc (strlen (fname)+1);
  6130.   strcpy (i->name, fname);
  6131.   bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
  6132.   i->dev = sb.st_dev;
  6133.   i->next = import_hash_table[hashval];
  6134.   import_hash_table[hashval] = i;
  6135. }
  6136. #endif
  6137.  
  6138. /* Load the specified precompiled header into core, and verify its
  6139.    preconditions.  PCF indicates the file descriptor to read, which must
  6140.    be a regular file.  FNAME indicates the file name of the original 
  6141.    header.  *LIMIT will be set to an address one past the end of the file.
  6142.    If the preconditions of the file are not satisfied, the buffer is 
  6143.    freed and we return 0.  If the preconditions are satisfied, return
  6144.    the address of the buffer following the preconditions.  The buffer, in
  6145.    this case, should never be freed because various pieces of it will
  6146.    be referred to until all precompiled strings are output at the end of
  6147.    the run.
  6148. */
  6149. static char *
  6150. check_precompiled (pcf, fname, limit)
  6151.      int pcf;
  6152.      char *fname;
  6153.      char **limit;
  6154. {
  6155.   int st_mode;
  6156.   long st_size;
  6157.   int length = 0;
  6158.   char *buf;
  6159.   char *cp;
  6160.  
  6161.   if (pcp_outfile)
  6162.     return 0;
  6163.   
  6164.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  6165.     return 0;
  6166.  
  6167.   if (S_ISREG (st_mode))
  6168.     {
  6169.       buf = xmalloc (st_size + 2);
  6170.       length = safe_read (pcf, buf, st_size);
  6171.       if (length < 0)
  6172.     goto nope;
  6173.     }
  6174.   else
  6175.     abort ();
  6176.     
  6177.   if (length > 0 && buf[length-1] != '\n')
  6178.     buf[length++] = '\n';
  6179.   buf[length] = '\0';
  6180.   
  6181.   *limit = buf + length;
  6182.  
  6183.   /* File is in core.  Check the preconditions. */
  6184.   if (!check_preconditions (buf))
  6185.     goto nope;
  6186.   for (cp = buf; *cp; cp++)
  6187.     ;
  6188. #ifdef DEBUG_PCP
  6189.   fprintf (stderr, "Using preinclude %s\n", fname);
  6190. #endif
  6191.   return cp + 1;
  6192.  
  6193.  nope:
  6194. #ifdef DEBUG_PCP
  6195.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  6196. #endif
  6197.   free (buf);
  6198.   return 0;
  6199. }
  6200.  
  6201. /* PREC (null terminated) points to the preconditions of a
  6202.    precompiled header.  These are a series of #define and #undef
  6203.    lines which must match the current contents of the hash
  6204.    table.  */
  6205. static int 
  6206. check_preconditions (prec)
  6207.      char *prec;
  6208. {
  6209.   MACRODEF mdef;
  6210.   char *lineend;
  6211.   
  6212.   while (*prec) {
  6213.     lineend = (char *) index (prec, '\n');
  6214.     
  6215.     if (*prec++ != '#') {
  6216.       error ("Bad format encountered while reading precompiled file");
  6217.       return 0;
  6218.     }
  6219.     if (!strncmp (prec, "define", 6)) {
  6220.       HASHNODE *hp;
  6221.       int hashcode;
  6222.       
  6223.       prec += 6;
  6224. #ifdef NEXT_CPP_SERVER
  6225.       mdef = create_definition (prec, lineend, NULL_PTR, &hashcode, 0);
  6226. #else
  6227.       mdef = create_definition (prec, lineend, NULL_PTR);
  6228. #endif
  6229.       if (mdef.defn == 0)
  6230.     abort ();
  6231.       
  6232. #ifdef NEXT_CPP_SERVER
  6233.       if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) == NULL
  6234. #else
  6235.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  6236. #endif
  6237.       || (hp->type != T_MACRO && hp->type != T_CONST)
  6238.       || (hp->type == T_MACRO
  6239.           && !compare_defs (mdef.defn, hp->value.defn)
  6240.           && (mdef.defn->length != 2
  6241.           || mdef.defn->expansion[0] != '\n'
  6242.           || mdef.defn->expansion[1] != ' ')))
  6243.     return 0;
  6244.     } else if (!strncmp (prec, "undef", 5)) {
  6245.       char *name;
  6246.       int len;
  6247.       
  6248.       prec += 5;
  6249.       while (is_hor_space[(U_CHAR) *prec])
  6250.     prec++;
  6251.       name = prec;
  6252.       while (is_idchar[(U_CHAR) *prec])
  6253.     prec++;
  6254.       len = prec - name;
  6255.       
  6256.       if (lookup (name, len, -1))
  6257.     return 0;
  6258.     } else {
  6259.       error ("Bad format encountered while reading precompiled file");
  6260.       return 0;
  6261.     }
  6262.     prec = lineend + 1;
  6263.   }
  6264.   /* They all passed successfully */
  6265.   return 1;
  6266. }
  6267.  
  6268. /* Process the main body of a precompiled file.  BUF points to the
  6269.    string section of the file, following the preconditions.  LIMIT is one
  6270.    character past the end.  NAME is the name of the file being read
  6271.    in.  OP is the main output buffer */
  6272. static void
  6273. pcfinclude (buf, limit, name, op)
  6274.      U_CHAR *buf, *limit, *name;
  6275.      FILE_BUF *op;
  6276. {
  6277.   FILE_BUF tmpbuf;
  6278.   int nstrings;
  6279.   U_CHAR *cp = buf;
  6280.  
  6281.   /* First in the file comes 4 bytes indicating the number of strings, */
  6282.   /* in network byte order. (MSB first).  */
  6283.   nstrings = *cp++;
  6284.   nstrings = (nstrings << 8) | *cp++;
  6285.   nstrings = (nstrings << 8) | *cp++;
  6286.   nstrings = (nstrings << 8) | *cp++;
  6287.   
  6288.   /* Looping over each string... */
  6289.   while (nstrings--) {
  6290.     U_CHAR *string_start;
  6291.     U_CHAR *endofthiskey;
  6292.     STRINGDEF *str;
  6293.     int nkeys;
  6294.     
  6295.     /* Each string starts with a STRINGDEF structure (str), followed */
  6296.     /* by the text of the string (string_start) */
  6297.  
  6298.     /* First skip to a longword boundary */
  6299.     /* ??? Why a 4-byte boundary?  On all machines? */
  6300.     /* NOTE: This works correctly even if HOST_WIDE_INT
  6301.        is narrower than a pointer.
  6302.        Do not try risky measures here to get another type to use!
  6303.        Do not include stddef.h--it will fail!  */
  6304.     if ((HOST_WIDE_INT) cp & 3)
  6305.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  6306.     
  6307.     /* Now get the string. */
  6308.     str = (STRINGDEF *) cp;
  6309.     string_start = cp += sizeof (STRINGDEF);
  6310.     
  6311.     for (; *cp; cp++)        /* skip the string */
  6312.       ;
  6313.     
  6314.     /* We need to macro expand the string here to ensure that the
  6315.        proper definition environment is in place.  If it were only
  6316.        expanded when we find out it is needed, macros necessary for
  6317.        its proper expansion might have had their definitions changed. */
  6318.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  6319.     /* Lineno is already set in the precompiled file */
  6320.     str->contents = tmpbuf.buf;
  6321.     str->len = tmpbuf.length;
  6322.     str->writeflag = 0;
  6323.     str->filename = name;
  6324. #ifdef NEXT_CPP_SERVER
  6325.     str->output_mark = op->bufp - op->buf;
  6326. #else
  6327.     str->output_mark = outbuf.bufp - outbuf.buf;
  6328. #endif
  6329.     
  6330.     str->chain = 0;
  6331.     *stringlist_tailp = str;
  6332.     stringlist_tailp = &str->chain;
  6333.     
  6334.     /* Next comes a fourbyte number indicating the number of keys */
  6335.     /* for this string. */
  6336.     nkeys = *cp++;
  6337.     nkeys = (nkeys << 8) | *cp++;
  6338.     nkeys = (nkeys << 8) | *cp++;
  6339.     nkeys = (nkeys << 8) | *cp++;
  6340.  
  6341.     /* If this number is -1, then the string is mandatory. */
  6342.     if (nkeys == -1)
  6343.       str->writeflag = 1;
  6344.     else
  6345.       /* Otherwise, for each key, */
  6346.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  6347.     KEYDEF *kp = (KEYDEF *) cp;
  6348.     HASHNODE *hp;
  6349.     
  6350.     /* It starts with a KEYDEF structure */
  6351.     cp += sizeof (KEYDEF);
  6352.     
  6353.     /* Find the end of the key.  At the end of this for loop we
  6354.        advance CP to the start of the next key using this variable. */
  6355.     endofthiskey = cp + strlen (cp);
  6356.     kp->str = str;
  6357.     
  6358.     /* Expand the key, and enter it into the hash table. */
  6359.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  6360.     tmpbuf.bufp = tmpbuf.buf;
  6361.     
  6362.     while (is_hor_space[*tmpbuf.bufp])
  6363.       tmpbuf.bufp++;
  6364.     if (!is_idstart[*tmpbuf.bufp]
  6365.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  6366.       str->writeflag = 1;
  6367.       continue;
  6368.     }
  6369.         
  6370.     hp = lookup (tmpbuf.bufp, -1, -1);
  6371.     if (hp == NULL) {
  6372.       kp->chain = 0;
  6373.       install (tmpbuf.bufp, -1, T_PCSTRING, 0, (char *) kp, -1);
  6374.     }
  6375.     else if (hp->type == T_PCSTRING) {
  6376.       kp->chain = hp->value.keydef;
  6377.       hp->value.keydef = kp;
  6378.     }
  6379.     else
  6380.       str->writeflag = 1;
  6381.       }
  6382.   }
  6383.   /* This output_line_command serves to switch us back to the current
  6384.      input file in case some of these strings get output (which will 
  6385.      result in line commands for the header file being output). */
  6386.   output_line_command (&instack[indepth], op, 0, enter_file);
  6387. }
  6388.  
  6389. /* Called from rescan when it hits a key for strings.  Mark them all */
  6390.  /* used and clean up. */
  6391. static void
  6392. pcstring_used (hp)
  6393.      HASHNODE *hp;
  6394. {
  6395.   KEYDEF *kp;
  6396.   
  6397.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  6398.     kp->str->writeflag = 1;
  6399.   delete_macro (hp);
  6400. }
  6401.  
  6402. /* Write the output, interspersing precompiled strings in their */
  6403.  /* appropriate places. */
  6404. #ifdef NEXT_CPP_SERVER
  6405. static void
  6406. write_output (op, path, write_all)
  6407.      FILE_BUF *op;
  6408.      const char * path;
  6409.      int write_all;
  6410. #else
  6411. static void
  6412. write_output ()
  6413. #endif
  6414. {
  6415.   STRINGDEF *next_string;
  6416.   U_CHAR *cur_buf_loc;
  6417.   int line_command_len = 80;
  6418.   char *line_command = xmalloc (line_command_len);
  6419.   int len;
  6420.  
  6421.   /* In each run through the loop, either cur_buf_loc == */
  6422.   /* next_string_loc, in which case we print a series of strings, or */
  6423.   /* it is less than next_string_loc, in which case we write some of */
  6424.   /* the buffer. */
  6425. #ifdef NEXT_CPP_SERVER
  6426.   int output_fd;
  6427.   FILE * outs;
  6428.   if (!strcmp(path,"stdout"))
  6429.      output_fd = fileno(stdout);
  6430.   else
  6431.      output_fd = open (path, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  6432.   if (output_fd < 0) {
  6433.      fatal("Could not write file %s", path);
  6434.      return;
  6435.   }
  6436.   cur_buf_loc = write_all ? op->buf : op->output_written_to; 
  6437.   next_string = stringlist;
  6438.   
  6439.   while (cur_buf_loc < op->bufp || next_string) {
  6440.     if (next_string
  6441.     && cur_buf_loc - op->buf == next_string->output_mark) {
  6442. #else
  6443.   cur_buf_loc = outbuf.buf; 
  6444.   next_string = stringlist;
  6445.   
  6446.   while (cur_buf_loc < outbuf.bufp || next_string) {
  6447.     if (next_string
  6448.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  6449. #endif
  6450.       if (next_string->writeflag) {
  6451.     len = 4 * strlen (next_string->filename) + 32;
  6452.     while (len > line_command_len)
  6453.       line_command = xrealloc (line_command, 
  6454.                    line_command_len *= 2);
  6455.     sprintf (line_command, "\n# %d ", next_string->lineno);
  6456.     strcpy (quote_string (line_command + strlen (line_command),
  6457.                       next_string->filename),
  6458.         "\n");
  6459. #ifdef NEXT_CPP_SERVER        
  6460.     safe_write (output_fd, line_command, strlen (line_command));
  6461.     safe_write (output_fd, next_string->contents, next_string->len);
  6462. #else    
  6463.     safe_write (fileno (stdout), line_command, strlen (line_command));
  6464.     safe_write (fileno (stdout), next_string->contents, next_string->len);
  6465. #endif    
  6466.       }          
  6467.       next_string = next_string->chain;
  6468.     }
  6469.     else {
  6470.       len = (next_string
  6471.          ? (next_string->output_mark 
  6472. #ifdef NEXT_CPP_SERVER
  6473.         - (cur_buf_loc - op->buf))
  6474.          : op->bufp - cur_buf_loc);
  6475.       safe_write (output_fd, cur_buf_loc, len);
  6476. #else
  6477.         - (cur_buf_loc - outbuf.buf))
  6478.          : outbuf.bufp - cur_buf_loc);
  6479.       safe_write (fileno (stdout), cur_buf_loc, len);
  6480. #endif
  6481.       
  6482.       cur_buf_loc += len;
  6483.     }
  6484.   }
  6485.   free (line_command);
  6486. #ifdef NEXT_CPP_SERVER
  6487. #if 0
  6488.   while (fsync(output_fd)) {
  6489.      fprintf(stderr, "error calling fsync(%d) in ", output_fd);
  6490.      perror_with_name(path);
  6491.      break;  /* FIXME */
  6492.   }
  6493. #endif
  6494.    if (strcmp (path, "stdout") != 0 && close(output_fd)) {
  6495.      fprintf(stderr, "error calling close(%d) in ", output_fd);
  6496.      perror_with_name(path);
  6497.   }
  6498.   op->output_written_to = cur_buf_loc;
  6499. #endif    
  6500. }
  6501.  
  6502. #ifdef NEXT_CPP_SERVER
  6503. static int
  6504. write_header_output (const char * output_fname,
  6505.                      FILE_BUF * op,
  6506.                      int write_all,
  6507.                      int report)
  6508. {
  6509.    int result;
  6510.    if (write_all || (op->output_written_to < op->bufp)) {
  6511.       /* more output required */
  6512.       write_output (op, output_fname, write_all);
  6513.       if (report)
  6514.          fprintf(stderr,"wrote headers to %s\n", output_fname);
  6515.       result = 1;
  6516.    } else {
  6517.       /* keep output consistent at 2 lines */
  6518.       if (report)
  6519.          fprintf(stderr,"did not write headers to %s\n", output_fname);
  6520.       result = 0;
  6521.    }
  6522.    fflush(stderr);
  6523.    return result;
  6524. }
  6525.  
  6526. #endif NEXT_CPP_SERVER
  6527.  
  6528. /* Pass a directive through to the output file.
  6529.    BUF points to the contents of the directive, as a contiguous string.
  6530.    LIMIT points to the first character past the end of the directive.
  6531.    KEYWORD is the keyword-table entry for the directive.  */
  6532.  
  6533. static void
  6534. pass_thru_directive (buf, limit, op, keyword)
  6535.      U_CHAR *buf, *limit;
  6536.      FILE_BUF *op;
  6537.      struct directive *keyword;
  6538. {
  6539.   register unsigned keyword_length = keyword->length;
  6540.  
  6541.   check_expand (op, 1 + keyword_length + (limit - buf));
  6542.   *op->bufp++ = '#';
  6543.   bcopy (keyword->name, (char *) op->bufp, keyword_length);
  6544.   op->bufp += keyword_length;
  6545.   if (limit != buf && buf[0] != ' ')
  6546.     *op->bufp++ = ' ';
  6547.   bcopy ((char *) buf, (char *) op->bufp, limit - buf);
  6548.   op->bufp += (limit - buf);
  6549. #if 0
  6550.   *op->bufp++ = '\n';
  6551.   /* Count the line we have just made in the output,
  6552.      to get in sync properly.  */
  6553.   op->lineno++;
  6554. #endif
  6555. }
  6556.  
  6557. /* The arglist structure is built by do_define to tell
  6558.    collect_definition where the argument names begin.  That
  6559.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  6560.    would contain pointers to the strings x, y, and z.
  6561.    Collect_definition would then build a DEFINITION node,
  6562.    with reflist nodes pointing to the places x, y, and z had
  6563.    appeared.  So the arglist is just convenience data passed
  6564.    between these two routines.  It is not kept around after
  6565.    the current #define has been processed and entered into the
  6566.    hash table. */
  6567.  
  6568. struct arglist {
  6569.   struct arglist *next;
  6570.   U_CHAR *name;
  6571.   int length;
  6572.   int argno;
  6573.   char rest_args;
  6574. };
  6575.  
  6576. #ifdef DEBUG_REDEFINED_MACRO_LEAKS
  6577. void foo(char * file, int line) {
  6578.    fprintf(stderr,"### About to create macro NL0 for %s:%d ###\n", file, line);
  6579. }
  6580. #endif
  6581.  
  6582.  
  6583. /* Create a DEFINITION node from a #define directive.  Arguments are 
  6584.    as for do_define. */
  6585. #ifdef NEXT_CPP_SERVER
  6586. static MACRODEF
  6587. create_definition (buf, limit, op, hashcode, hashp)
  6588.      U_CHAR *buf, *limit;
  6589.      FILE_BUF *op;
  6590.      int *hashcode;
  6591.      HASHNODE **hashp;
  6592. #else
  6593. static MACRODEF
  6594. create_definition (buf, limit, op)
  6595.      U_CHAR *buf, *limit;
  6596.      FILE_BUF *op;
  6597. #endif
  6598. {
  6599.   U_CHAR *bp;            /* temp ptr into input buffer */
  6600.   U_CHAR *symname;        /* remember where symbol name starts */
  6601.   int sym_length;        /* and how long it is */
  6602.   int line = instack[indepth].lineno;
  6603.   char *file = instack[indepth].nominal_fname;
  6604.   int rest_args = 0;
  6605.  
  6606.   DEFINITION *defn;
  6607.   int arglengths = 0;        /* Accumulate lengths of arg names
  6608.                    plus number of args.  */
  6609.   MACRODEF mdef;
  6610.  
  6611.   bp = buf;
  6612.  
  6613.   while (is_hor_space[*bp])
  6614.     bp++;
  6615.  
  6616.   symname = bp;            /* remember where it starts */
  6617.   sym_length = check_macro_name (bp, "macro");
  6618.   bp += sym_length;
  6619.  
  6620. #ifdef NEXT_CPP_SERVER
  6621.   if (hashp) {
  6622.     HASHNODE *hp;
  6623.     *hashcode = hashf (symname, sym_length, HASHSIZE);
  6624.     if ((hp = lookup (symname, sym_length, *hashcode)) != NULL) {
  6625.       *hashp = hp;
  6626.       if (hp->type == T_MACRO) {
  6627.         /* now that we can process many files, the names should be unique */
  6628.         if (strcmp(file, hp->value.defn->file) == 0 &&
  6629.             line == hp->value.defn->line) {
  6630.           /* reuse macro definition...no need to advance buffer or anything!*/
  6631.           defn = hp->value.defn;
  6632.           goto reusedefn;
  6633.         } 
  6634.       }
  6635.     }
  6636.   }
  6637. #endif
  6638.  
  6639. #ifdef DEBUG_REDEFINED_MACRO_LEAKS
  6640.   /* REMOVE ME!! */
  6641.   if (!strncmp(symname, "NL0", sym_length))
  6642.      foo(file, line);
  6643. #endif
  6644.   
  6645.   /* Lossage will occur if identifiers or control keywords are broken
  6646.      across lines using backslash.  This is not the right place to take
  6647.      care of that. */
  6648.  
  6649.   if (*bp == '(') {
  6650.     struct arglist *arg_ptrs = NULL;
  6651.     int argno = 0;
  6652.  
  6653.     bp++;            /* skip '(' */
  6654.     SKIP_WHITE_SPACE (bp);
  6655.  
  6656.     /* Loop over macro argument names.  */
  6657.     while (*bp != ')') {
  6658.       struct arglist *temp;
  6659.  
  6660.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  6661.       temp->name = bp;
  6662.       temp->next = arg_ptrs;
  6663.       temp->argno = argno++;
  6664.       temp->rest_args = 0;
  6665.       arg_ptrs = temp;
  6666.  
  6667.       if (rest_args)
  6668.     pedwarn ("another parameter follows `%s'",
  6669.          rest_extension);
  6670.  
  6671.       if (!is_idstart[*bp])
  6672.     pedwarn ("invalid character in macro parameter name");
  6673.       
  6674.       /* Find the end of the arg name.  */
  6675.       while (is_idchar[*bp]) {
  6676.     bp++;
  6677.     /* do we have a "special" rest-args extension here? */
  6678.     if (limit - bp > REST_EXTENSION_LENGTH &&
  6679.         strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  6680.       rest_args = 1;
  6681.       temp->rest_args = 1;
  6682.       break;
  6683.     }
  6684.       }
  6685.       temp->length = bp - temp->name;
  6686.       if (rest_args == 1)
  6687.     bp += REST_EXTENSION_LENGTH;
  6688.       arglengths += temp->length + 2;
  6689.       SKIP_WHITE_SPACE (bp);
  6690.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  6691.     error ("badly punctuated parameter list in `#define'");
  6692.     goto nope;
  6693.       }
  6694.       if (*bp == ',') {
  6695.     bp++;
  6696.     SKIP_WHITE_SPACE (bp);
  6697.       }
  6698.       if (bp >= limit) {
  6699.     error ("unterminated parameter list in `#define'");
  6700.     goto nope;
  6701.       }
  6702.       {
  6703.     struct arglist *otemp;
  6704.  
  6705.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  6706.       if (temp->length == otemp->length &&
  6707.         strncmp (temp->name, otemp->name, temp->length) == 0) {
  6708.           U_CHAR *name;
  6709.  
  6710.           name = (U_CHAR *) alloca (temp->length + 1);
  6711.           (void) strncpy (name, temp->name, temp->length);
  6712.           name[temp->length] = '\0';
  6713.           error ("duplicate argument name `%s' in `#define'", name);
  6714.           goto nope;
  6715.       }
  6716.       }
  6717.     }
  6718.  
  6719.     ++bp;            /* skip paren */
  6720.     /* Skip spaces and tabs if any.  */
  6721.     while (bp < limit && (*bp == ' ' || *bp == '\t'))
  6722.       ++bp;
  6723.     /* now everything from bp before limit is the definition. */
  6724.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  6725.     defn->rest_args = rest_args;
  6726.  
  6727.     /* Now set defn->args.argnames to the result of concatenating
  6728.        the argument names in reverse order
  6729.        with comma-space between them.  */
  6730.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  6731.     {
  6732.       struct arglist *temp;
  6733.       int i = 0;
  6734.       for (temp = arg_ptrs; temp; temp = temp->next) {
  6735.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  6736.     i += temp->length;
  6737.     if (temp->next != 0) {
  6738.       defn->args.argnames[i++] = ',';
  6739.       defn->args.argnames[i++] = ' ';
  6740.     }
  6741.       }
  6742.       defn->args.argnames[i] = 0;
  6743.     }
  6744.   } else {
  6745.     /* Simple expansion or empty definition.  */
  6746.  
  6747.     /* Skip spaces and tabs if any.  */
  6748.     while (bp < limit && (*bp == ' ' || *bp == '\t'))
  6749.       ++bp;
  6750.     /* Now everything from bp before limit is the definition. */
  6751.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  6752.     defn->args.argnames = (U_CHAR *) "";
  6753.   }
  6754.  
  6755.   defn->line = line;
  6756.   defn->file = file;
  6757.  
  6758.   /* OP is null if this is a predefinition */
  6759.   defn->predefined = !op;
  6760. #ifdef NEXT_CPP_SERVER
  6761. reusedefn:
  6762. #endif
  6763.   mdef.defn = defn;
  6764.   mdef.symnam = symname;
  6765.   mdef.symlen = sym_length;
  6766.  
  6767.   return mdef;
  6768.  
  6769.  nope:
  6770.   mdef.defn = 0;
  6771.   return mdef;
  6772. }
  6773.  
  6774. /* Process a #define command.
  6775. BUF points to the contents of the #define command, as a contiguous string.
  6776. LIMIT points to the first character past the end of the definition.
  6777. KEYWORD is the keyword-table entry for #define.  */
  6778.  
  6779. static int
  6780. do_define (buf, limit, op, keyword)
  6781.      U_CHAR *buf, *limit;
  6782.      FILE_BUF *op;
  6783.      struct directive *keyword;
  6784. {
  6785.   int hashcode;
  6786.   MACRODEF mdef;
  6787. #ifdef NEXT_CPP_SERVER
  6788.   HASHNODE *hp = 0;
  6789. #endif
  6790.  
  6791.   /* If this is a precompiler run (with -pcp) pass thru #define commands.  */
  6792.   if (pcp_outfile && op)
  6793.     pass_thru_directive (buf, limit, op, keyword);
  6794.  
  6795. #ifdef NEXT_CPP_SERVER
  6796.   mdef = create_definition (buf, limit, op, &hashcode, &hp);
  6797. #else
  6798.   mdef = create_definition (buf, limit, op);
  6799. #endif
  6800.   if (mdef.defn == 0)
  6801.     goto nope;
  6802.  
  6803. #ifdef NEXT_CPP_SERVER
  6804.   if (hp) {
  6805.     if (hp->value.defn == mdef.defn) {
  6806.       hp->defined_within_module = 1;
  6807.     } else {
  6808. #else
  6809.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  6810.  
  6811.   {
  6812.     HASHNODE *hp;
  6813.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  6814. #endif
  6815.       int ok = 0;
  6816.       /* Redefining a precompiled key is ok.  */
  6817.       if (hp->type == T_PCSTRING)
  6818.     ok = 1;
  6819.       /* Redefining a macro is ok if the definitions are the same.  */
  6820. #ifdef NEXT_CPP_SERVER
  6821.       else if (hp->type == T_MACRO) {
  6822.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  6823.         if (!ok && !hp->defined_within_module) {
  6824.           hp->value.defn = mdef.defn;
  6825.           ok = 1; /* calm down, everything is ok now */
  6826.         }
  6827.         hp->defined_within_module = 1;
  6828.         /*fprintf(stderr, "reusing macro %.10s\n", mdef.symnam); */
  6829.       }
  6830. #else
  6831.       else if (hp->type == T_MACRO)
  6832.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  6833. #endif
  6834.       /* Redefining a constant is ok with -D.  */
  6835.       else if (hp->type == T_CONST)
  6836.         ok = ! done_initializing;
  6837.       /* Print the warning if it's not ok.  */
  6838.       if (!ok) {
  6839.     U_CHAR *msg;        /* what pain... */
  6840.  
  6841.         /* If we are passing through #define and #undef directives, do
  6842.        that for this re-definition now.  */
  6843.         if (debug_output && op)
  6844.       pass_thru_directive (buf, limit, op, keyword);
  6845.  
  6846.     msg = (U_CHAR *) alloca (mdef.symlen + 22);
  6847.     *msg = '`';
  6848.     bcopy ((char *) mdef.symnam, (char *) (msg + 1), mdef.symlen);
  6849.     strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
  6850.     pedwarn (msg);
  6851.     if (hp->type == T_MACRO)
  6852.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  6853.                       "this is the location of the previous definition");
  6854.       }
  6855.       /* Replace the old definition.  */
  6856.       hp->type = T_MACRO;
  6857.       hp->value.defn = mdef.defn;
  6858. #ifdef NEXT_CPP_SERVER
  6859.       }
  6860. #endif
  6861.     } else {
  6862.       /* If we are passing through #define and #undef directives, do
  6863.      that for this new definition now.  */
  6864.       if (debug_output && op)
  6865.     pass_thru_directive (buf, limit, op, keyword);
  6866. #ifdef NEXT_CPP_SERVER
  6867.       hp = install (mdef.symnam, mdef.symlen, T_MACRO, 0,
  6868.                     (char *) mdef.defn, hashcode);
  6869.       if (segregate_output && 
  6870.           instack[indepth].file && !instack[indepth].file->macro_list) {
  6871.         hp->owner = instack[indepth].file;
  6872.         hp->owner->macro_count++;
  6873.       }
  6874. #else
  6875.       install (mdef.symnam, mdef.symlen, T_MACRO, 0,
  6876.            (char *) mdef.defn, hashcode);
  6877. #endif
  6878.     }
  6879.  
  6880.   return 0;
  6881.  
  6882. nope:
  6883.  
  6884.   return 1;
  6885. }
  6886.  
  6887. /* Check a purported macro name SYMNAME, and yield its length.
  6888.    USAGE is the kind of name this is intended for.  */
  6889.  
  6890. static int
  6891. check_macro_name (symname, usage)
  6892.      U_CHAR *symname;
  6893.      char *usage;
  6894. {
  6895.   U_CHAR *p;
  6896.   int sym_length;
  6897.  
  6898.   for (p = symname; is_idchar[*p]; p++)
  6899.     ;
  6900.   sym_length = p - symname;
  6901.   if (sym_length == 0)
  6902.     error ("invalid %s name", usage);
  6903.   else if (!is_idstart[*symname]) {
  6904.     U_CHAR *msg;            /* what pain... */
  6905.     msg = (U_CHAR *) alloca (sym_length + 1);
  6906.     bcopy ((char *) symname, (char *) msg, sym_length);
  6907.     msg[sym_length] = 0;
  6908.     error ("invalid %s name `%s'", usage, msg);
  6909.   } else {
  6910.     if (! strncmp (symname, "defined", 7) && sym_length == 7)
  6911.       error ("invalid %s name `defined'", usage);
  6912.   }
  6913.   return sym_length;
  6914. }
  6915.  
  6916. /*
  6917.  * return zero if two DEFINITIONs are isomorphic
  6918.  */
  6919. static int
  6920. compare_defs (d1, d2)
  6921.      DEFINITION *d1, *d2;
  6922. {
  6923.   register struct reflist *a1, *a2;
  6924.   register U_CHAR *p1 = d1->expansion;
  6925.   register U_CHAR *p2 = d2->expansion;
  6926.   int first = 1;
  6927.  
  6928.   if (d1->nargs != d2->nargs)
  6929.     return 1;
  6930.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  6931.     return 1;
  6932.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  6933.        a1 = a1->next, a2 = a2->next) {
  6934.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  6935.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  6936.     || a1->argno != a2->argno
  6937.     || a1->stringify != a2->stringify
  6938.     || a1->raw_before != a2->raw_before
  6939.     || a1->raw_after != a2->raw_after)
  6940.       return 1;
  6941.     first = 0;
  6942.     p1 += a1->nchars;
  6943.     p2 += a2->nchars;
  6944.   }
  6945.   if (a1 != a2)
  6946.     return 1;
  6947.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  6948.              p2, d2->length - (p2 - d2->expansion), 1))
  6949.     return 1;
  6950.   return 0;
  6951. }
  6952.  
  6953. /* Return 1 if two parts of two macro definitions are effectively different.
  6954.    One of the parts starts at BEG1 and has LEN1 chars;
  6955.    the other has LEN2 chars at BEG2.
  6956.    Any sequence of whitespace matches any other sequence of whitespace.
  6957.    FIRST means these parts are the first of a macro definition;
  6958.     so ignore leading whitespace entirely.
  6959.    LAST means these parts are the last of a macro definition;
  6960.     so ignore trailing whitespace entirely.  */
  6961.  
  6962. static int
  6963. comp_def_part (first, beg1, len1, beg2, len2, last)
  6964.      int first;
  6965.      U_CHAR *beg1, *beg2;
  6966.      int len1, len2;
  6967.      int last;
  6968. {
  6969.   register U_CHAR *end1 = beg1 + len1;
  6970.   register U_CHAR *end2 = beg2 + len2;
  6971.   if (first) {
  6972.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  6973.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  6974.   }
  6975.   if (last) {
  6976.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  6977.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  6978.   }
  6979.   while (beg1 != end1 && beg2 != end2) {
  6980.     if (is_space[*beg1] && is_space[*beg2]) {
  6981.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  6982.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  6983.     } else if (*beg1 == *beg2) {
  6984.       beg1++; beg2++;
  6985.     } else break;
  6986.   }
  6987.   return (beg1 != end1) || (beg2 != end2);
  6988. }
  6989.  
  6990. /* Read a replacement list for a macro with parameters.
  6991.    Build the DEFINITION structure.
  6992.    Reads characters of text starting at BUF until END.
  6993.    ARGLIST specifies the formal parameters to look for
  6994.    in the text of the definition; NARGS is the number of args
  6995.    in that list, or -1 for a macro name that wants no argument list.
  6996.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  6997.    and NAMELEN is its length in characters.
  6998.    
  6999. Note that comments and backslash-newlines have already been deleted
  7000. from the argument.  */
  7001.  
  7002. /* Leading and trailing Space, Tab, etc. are converted to markers
  7003.    Newline Space, Newline Tab, etc.
  7004.    Newline Space makes a space in the final output
  7005.    but is discarded if stringified.  (Newline Tab is similar but
  7006.    makes a Tab instead.)
  7007.  
  7008.    If there is no trailing whitespace, a Newline Space is added at the end
  7009.    to prevent concatenation that would be contrary to the standard.  */
  7010.  
  7011. static DEFINITION *
  7012. collect_expansion (buf, end, nargs, arglist)
  7013.      U_CHAR *buf, *end;
  7014.      int nargs;
  7015.      struct arglist *arglist;
  7016. {
  7017.   DEFINITION *defn;
  7018.   register U_CHAR *p, *limit, *lastp, *exp_p;
  7019.   struct reflist *endpat = NULL;
  7020.   /* Pointer to first nonspace after last ## seen.  */
  7021.   U_CHAR *concat = 0;
  7022.   /* Pointer to first nonspace after last single-# seen.  */
  7023.   U_CHAR *stringify = 0;
  7024.   int maxsize;
  7025.   int expected_delimiter = '\0';
  7026.  
  7027.   /* Scan thru the replacement list, ignoring comments and quoted
  7028.      strings, picking up on the macro calls.  It does a linear search
  7029.      thru the arg list on every potential symbol.  Profiling might say
  7030.      that something smarter should happen. */
  7031.  
  7032.   if (end < buf)
  7033.     abort ();
  7034.  
  7035.   /* Find the beginning of the trailing whitespace.  */
  7036.   /* Find end of leading whitespace.  */
  7037.   limit = end;
  7038.   p = buf;
  7039.   while (p < limit && is_space[limit[-1]]) limit--;
  7040.   while (p < limit && is_space[*p]) p++;
  7041.  
  7042.   /* Allocate space for the text in the macro definition.
  7043.      Leading and trailing whitespace chars need 2 bytes each.
  7044.      Each other input char may or may not need 1 byte,
  7045.      so this is an upper bound.
  7046.      The extra 2 are for invented trailing newline-marker and final null.  */
  7047.   maxsize = (sizeof (DEFINITION)
  7048.          + 2 * (end - limit) + 2 * (p - buf)
  7049.          + (limit - p) + 3);
  7050.   defn = (DEFINITION *) xcalloc (1, maxsize);
  7051.  
  7052.   defn->nargs = nargs;
  7053.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  7054.   lastp = exp_p;
  7055.  
  7056.   p = buf;
  7057.  
  7058.   /* Convert leading whitespace to Newline-markers.  */
  7059.   while (p < limit && is_space[*p]) {
  7060.     *exp_p++ = '\n';
  7061.     *exp_p++ = *p++;
  7062.   }
  7063.  
  7064.   if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
  7065.     error ("`##' at start of macro definition");
  7066.     p += 2;
  7067.   }
  7068.  
  7069.   /* Process the main body of the definition.  */
  7070.   while (p < limit) {
  7071.     int skipped_arg = 0;
  7072.     register U_CHAR c = *p++;
  7073.  
  7074.     *exp_p++ = c;
  7075.  
  7076.     if (!traditional) {
  7077.       switch (c) {
  7078.       case '\'':
  7079.       case '\"':
  7080.         if (expected_delimiter != '\0') {
  7081.           if (c == expected_delimiter)
  7082.             expected_delimiter = '\0';
  7083.         } else
  7084.           expected_delimiter = c;
  7085.     break;
  7086.  
  7087.     /* Special hack: if a \# is written in the #define
  7088.        include a # in the definition.  This is useless for C code
  7089.        but useful for preprocessing other things.  */
  7090.  
  7091.       case '\\':
  7092.     /* \# quotes a # even outside of strings.  */
  7093.     if (p < limit && *p == '#' && !expected_delimiter) {
  7094.       exp_p--;
  7095.       *exp_p++ = *p++;
  7096.     } else if (p < limit && expected_delimiter) {
  7097.       /* In a string, backslash goes through
  7098.          and makes next char ordinary.  */
  7099.       *exp_p++ = *p++;
  7100.     }
  7101.     break;
  7102.  
  7103.       case '#':
  7104.     /* # is ordinary inside a string.  */
  7105.     if (expected_delimiter)
  7106.       break;
  7107.     if (p < limit && *p == '#') {
  7108.       /* ##: concatenate preceding and following tokens.  */
  7109.       /* Take out the first #, discard preceding whitespace.  */
  7110.       exp_p--;
  7111.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  7112.         --exp_p;
  7113.       /* Skip the second #.  */
  7114.       p++;
  7115.       /* Discard following whitespace.  */
  7116.       SKIP_WHITE_SPACE (p);
  7117.       concat = p;
  7118.       if (p == limit)
  7119.         error ("`##' at end of macro definition");
  7120.     } else if (nargs >= 0) {
  7121.       /* Single #: stringify following argument ref.
  7122.          Don't leave the # in the expansion.  */
  7123.       exp_p--;
  7124.       SKIP_WHITE_SPACE (p);
  7125.       if (p == limit || ! is_idstart[*p])
  7126.         error ("`#' operator is not followed by a macro argument name");
  7127.       else
  7128.         stringify = p;
  7129.     }
  7130.     break;
  7131.       }
  7132.     } else {
  7133.       /* In -traditional mode, recognize arguments inside strings and
  7134.      and character constants, and ignore special properties of #.
  7135.      Arguments inside strings are considered "stringified", but no
  7136.      extra quote marks are supplied.  */
  7137.       switch (c) {
  7138.       case '\'':
  7139.       case '\"':
  7140.     if (expected_delimiter != '\0') {
  7141.       if (c == expected_delimiter)
  7142.         expected_delimiter = '\0';
  7143.     } else
  7144.       expected_delimiter = c;
  7145.     break;
  7146.  
  7147.       case '\\':
  7148.     /* Backslash quotes delimiters and itself, but not macro args.  */
  7149.     if (expected_delimiter != 0 && p < limit
  7150.         && (*p == expected_delimiter || *p == '\\')) {
  7151.       *exp_p++ = *p++;
  7152.       continue;
  7153.     }
  7154.     break;
  7155.  
  7156.       case '/':
  7157.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  7158.       break;
  7159.     if (*p == '*') {
  7160.       /* If we find a comment that wasn't removed by handle_directive,
  7161.          this must be -traditional.  So replace the comment with
  7162.          nothing at all.  */
  7163.       exp_p--;
  7164.       p += 1;
  7165.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  7166.         p++;
  7167. #if 0
  7168.       /* Mark this as a concatenation-point, as if it had been ##.  */
  7169.       concat = p;
  7170. #endif
  7171.     }
  7172.     break;
  7173.       }
  7174.     }
  7175.  
  7176.     /* Handle the start of a symbol.  */
  7177.     if (is_idchar[c] && nargs > 0) {
  7178.       U_CHAR *id_beg = p - 1;
  7179.       int id_len;
  7180.  
  7181.       --exp_p;
  7182.       while (p != limit && is_idchar[*p]) p++;
  7183.       id_len = p - id_beg;
  7184.  
  7185.       if (is_idstart[c]) {
  7186.     register struct arglist *arg;
  7187.  
  7188.     for (arg = arglist; arg != NULL; arg = arg->next) {
  7189.       struct reflist *tpat;
  7190.  
  7191.       if (arg->name[0] == c
  7192.           && arg->length == id_len
  7193.           && strncmp (arg->name, id_beg, id_len) == 0) {
  7194.         if (expected_delimiter && warn_stringify) {
  7195.           if (traditional) {
  7196.         warning ("macro argument `%.*s' is stringified.",
  7197.              id_len, arg->name);
  7198.           } else {
  7199.         warning ("macro arg `%.*s' would be stringified with -traditional.",
  7200.              id_len, arg->name);
  7201.           }
  7202.         }
  7203.         /* If ANSI, don't actually substitute inside a string.  */
  7204.         if (!traditional && expected_delimiter)
  7205.           break;
  7206.         /* make a pat node for this arg and append it to the end of
  7207.            the pat list */
  7208.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  7209.         tpat->next = NULL;
  7210.         tpat->raw_before = concat == id_beg;
  7211.         tpat->raw_after = 0;
  7212.         tpat->rest_args = arg->rest_args;
  7213.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  7214.                    : stringify == id_beg);
  7215.  
  7216.         if (endpat == NULL)
  7217.           defn->pattern = tpat;
  7218.         else
  7219.           endpat->next = tpat;
  7220.         endpat = tpat;
  7221.  
  7222.         tpat->argno = arg->argno;
  7223.         tpat->nchars = exp_p - lastp;
  7224.         {
  7225.           register U_CHAR *p1 = p;
  7226.           SKIP_WHITE_SPACE (p1);
  7227.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  7228.         tpat->raw_after = 1;
  7229.         }
  7230.         lastp = exp_p;    /* place to start copying from next time */
  7231.         skipped_arg = 1;
  7232.         break;
  7233.       }
  7234.     }
  7235.       }
  7236.  
  7237.       /* If this was not a macro arg, copy it into the expansion.  */
  7238.       if (! skipped_arg) {
  7239.     register U_CHAR *lim1 = p;
  7240.     p = id_beg;
  7241.     while (p != lim1)
  7242.       *exp_p++ = *p++;
  7243.     if (stringify == id_beg)
  7244.       error ("`#' operator should be followed by a macro argument name");
  7245.       }
  7246.     }
  7247.   }
  7248.  
  7249.   if (!traditional && expected_delimiter == 0) {
  7250.     /* There is no trailing whitespace, so invent some in ANSI mode.
  7251.        But not if "inside a string" (which in ANSI mode
  7252.        happens only for -D option).  */
  7253.     *exp_p++ = '\n';
  7254.     *exp_p++ = ' ';
  7255.   }
  7256.  
  7257.   *exp_p = '\0';
  7258.  
  7259.   defn->length = exp_p - defn->expansion;
  7260.  
  7261.   /* Crash now if we overrun the allocated size.  */
  7262.   if (defn->length + 1 > maxsize)
  7263.     abort ();
  7264.  
  7265. #if 0
  7266. /* This isn't worth the time it takes.  */
  7267.   /* give back excess storage */
  7268.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  7269. #endif
  7270.  
  7271.   return defn;
  7272. }
  7273.  
  7274. static int
  7275. do_assert (buf, limit, op, keyword)
  7276.      U_CHAR *buf, *limit;
  7277.      FILE_BUF *op;
  7278.      struct directive *keyword;
  7279. {
  7280.   U_CHAR *bp;            /* temp ptr into input buffer */
  7281.   U_CHAR *symname;        /* remember where symbol name starts */
  7282.   int sym_length;        /* and how long it is */
  7283.   struct arglist *tokens = NULL;
  7284.  
  7285.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  7286.     pedwarn ("ANSI C does not allow `#assert'");
  7287.  
  7288.   bp = buf;
  7289.  
  7290.   while (is_hor_space[*bp])
  7291.     bp++;
  7292.  
  7293.   symname = bp;            /* remember where it starts */
  7294.   sym_length = check_macro_name (bp, "assertion");
  7295.   bp += sym_length;
  7296.   /* #define doesn't do this, but we should.  */
  7297.   SKIP_WHITE_SPACE (bp);
  7298.  
  7299.   /* Lossage will occur if identifiers or control tokens are broken
  7300.      across lines using backslash.  This is not the right place to take
  7301.      care of that. */
  7302.  
  7303.   if (*bp != '(') {
  7304.     error ("missing token-sequence in `#assert'");
  7305.     return 1;
  7306.   }
  7307.  
  7308.   {
  7309.     int error_flag = 0;
  7310.  
  7311.     bp++;            /* skip '(' */
  7312.     SKIP_WHITE_SPACE (bp);
  7313.  
  7314.     tokens = read_token_list (&bp, limit, &error_flag);
  7315.     if (error_flag)
  7316.       return 1;
  7317.     if (tokens == 0) {
  7318.       error ("empty token-sequence in `#assert'");
  7319.       return 1;
  7320.     }
  7321.  
  7322.     ++bp;            /* skip paren */
  7323.     SKIP_WHITE_SPACE (bp);
  7324.   }
  7325.  
  7326.   /* If this name isn't already an assertion name, make it one.
  7327.      Error if it was already in use in some other way.  */
  7328.  
  7329.   {
  7330.     ASSERTION_HASHNODE *hp;
  7331.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  7332.     struct tokenlist_list *value
  7333.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  7334.  
  7335.     hp = assertion_lookup (symname, sym_length, hashcode);
  7336.     if (hp == NULL) {
  7337.       if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
  7338.     error ("`defined' redefined as assertion");
  7339.       hp = assertion_install (symname, sym_length, hashcode);
  7340.     }
  7341.  
  7342.     /* Add the spec'd token-sequence to the list of such.  */
  7343.     value->tokens = tokens;
  7344.     value->next = hp->value;
  7345.     hp->value = value;
  7346.   }
  7347.  
  7348.   return 0;
  7349. }
  7350.  
  7351. static int
  7352. do_unassert (buf, limit, op, keyword)
  7353.      U_CHAR *buf, *limit;
  7354.      FILE_BUF *op;
  7355.      struct directive *keyword;
  7356. {
  7357.   U_CHAR *bp;            /* temp ptr into input buffer */
  7358.   U_CHAR *symname;        /* remember where symbol name starts */
  7359.   int sym_length;        /* and how long it is */
  7360.  
  7361.   struct arglist *tokens = NULL;
  7362.   int tokens_specified = 0;
  7363.  
  7364.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  7365.     pedwarn ("ANSI C does not allow `#unassert'");
  7366.  
  7367.   bp = buf;
  7368.  
  7369.   while (is_hor_space[*bp])
  7370.     bp++;
  7371.  
  7372.   symname = bp;            /* remember where it starts */
  7373.   sym_length = check_macro_name (bp, "assertion");
  7374.   bp += sym_length;
  7375.   /* #define doesn't do this, but we should.  */
  7376.   SKIP_WHITE_SPACE (bp);
  7377.  
  7378.   /* Lossage will occur if identifiers or control tokens are broken
  7379.      across lines using backslash.  This is not the right place to take
  7380.      care of that. */
  7381.  
  7382.   if (*bp == '(') {
  7383.     int error_flag = 0;
  7384.  
  7385.     bp++;            /* skip '(' */
  7386.     SKIP_WHITE_SPACE (bp);
  7387.  
  7388.     tokens = read_token_list (&bp, limit, &error_flag);
  7389.     if (error_flag)
  7390.       return 1;
  7391.     if (tokens == 0) {
  7392.       error ("empty token list in `#unassert'");
  7393.       return 1;
  7394.     }
  7395.  
  7396.     tokens_specified = 1;
  7397.  
  7398.     ++bp;            /* skip paren */
  7399.     SKIP_WHITE_SPACE (bp);
  7400.   }
  7401.  
  7402.   {
  7403.     ASSERTION_HASHNODE *hp;
  7404.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  7405.     struct tokenlist_list *tail, *prev;
  7406.  
  7407.     hp = assertion_lookup (symname, sym_length, hashcode);
  7408.     if (hp == NULL)
  7409.       return 1;
  7410.  
  7411.     /* If no token list was specified, then eliminate this assertion
  7412.        entirely.  */
  7413.     if (! tokens_specified) {
  7414.       struct tokenlist_list *next;
  7415.       for (tail = hp->value; tail; tail = next) {
  7416.     next = tail->next;
  7417.     free_token_list (tail->tokens);
  7418.     free (tail);
  7419.       }
  7420.       delete_assertion (hp);
  7421.     } else {
  7422.       /* If a list of tokens was given, then delete any matching list.  */
  7423.  
  7424.       tail = hp->value;
  7425.       prev = 0;
  7426.       while (tail) {
  7427.     struct tokenlist_list *next = tail->next;
  7428.     if (compare_token_lists (tail->tokens, tokens)) {
  7429.       if (prev)
  7430.         prev->next = next;
  7431.       else
  7432.         hp->value = tail->next;
  7433.       free_token_list (tail->tokens);
  7434.       free (tail);
  7435.     } else {
  7436.       prev = tail;
  7437.     }
  7438.     tail = next;
  7439.       }
  7440.     }
  7441.   }
  7442.  
  7443.   return 0;
  7444. }
  7445.  
  7446. /* Test whether there is an assertion named NAME
  7447.    and optionally whether it has an asserted token list TOKENS.
  7448.    NAME is not null terminated; its length is SYM_LENGTH.
  7449.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  7450.  
  7451. int
  7452. check_assertion (name, sym_length, tokens_specified, tokens)
  7453.      U_CHAR *name;
  7454.      int sym_length;
  7455.      int tokens_specified;
  7456.      struct arglist *tokens;
  7457. {
  7458.   ASSERTION_HASHNODE *hp;
  7459.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  7460.  
  7461.   if (pedantic && !instack[indepth].system_header_p)
  7462.     pedwarn ("ANSI C does not allow testing assertions");
  7463.  
  7464.   hp = assertion_lookup (name, sym_length, hashcode);
  7465.   if (hp == NULL)
  7466.     /* It is not an assertion; just return false.  */
  7467.     return 0;
  7468.  
  7469.   /* If no token list was specified, then value is 1.  */
  7470.   if (! tokens_specified)
  7471.     return 1;
  7472.  
  7473.   {
  7474.     struct tokenlist_list *tail;
  7475.  
  7476.     tail = hp->value;
  7477.  
  7478.     /* If a list of tokens was given,
  7479.        then succeed if the assertion records a matching list.  */
  7480.  
  7481.     while (tail) {
  7482.       if (compare_token_lists (tail->tokens, tokens))
  7483.     return 1;
  7484.       tail = tail->next;
  7485.     }
  7486.  
  7487.     /* Fail if the assertion has no matching list.  */
  7488.     return 0;
  7489.   }
  7490. }
  7491.  
  7492. /* Compare two lists of tokens for equality including order of tokens.  */
  7493.  
  7494. static int
  7495. compare_token_lists (l1, l2)
  7496.      struct arglist *l1, *l2;
  7497. {
  7498.   while (l1 && l2) {
  7499.     if (l1->length != l2->length)
  7500.       return 0;
  7501.     if (strncmp (l1->name, l2->name, l1->length))
  7502.       return 0;
  7503.     l1 = l1->next;
  7504.     l2 = l2->next;
  7505.   }
  7506.  
  7507.   /* Succeed if both lists end at the same time.  */
  7508.   return l1 == l2;
  7509. }
  7510.  
  7511. /* Read a space-separated list of tokens ending in a close parenthesis.
  7512.    Return a list of strings, in the order they were written.
  7513.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  7514.    Parse the text starting at *BPP, and update *BPP.
  7515.    Don't parse beyond LIMIT.  */
  7516.  
  7517. static struct arglist *
  7518. read_token_list (bpp, limit, error_flag)
  7519.      U_CHAR **bpp;
  7520.      U_CHAR *limit;
  7521.      int *error_flag;
  7522. {
  7523.   struct arglist *token_ptrs = 0;
  7524.   U_CHAR *bp = *bpp;
  7525.   int depth = 1;
  7526.  
  7527.   *error_flag = 0;
  7528.  
  7529.   /* Loop over the assertion value tokens.  */
  7530.   while (depth > 0) {
  7531.     struct arglist *temp;
  7532.     int eofp = 0;
  7533.     U_CHAR *beg = bp;
  7534.  
  7535.     /* Find the end of the token.  */
  7536.     if (*bp == '(') {
  7537.       bp++;
  7538.       depth++;
  7539.     } else if (*bp == ')') {
  7540.       depth--;
  7541.       if (depth == 0)
  7542.     break;
  7543.       bp++;
  7544.     } else if (*bp == '"' || *bp == '\'')
  7545.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  7546.     else
  7547.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  7548.          && *bp != '"' && *bp != '\'' && bp != limit)
  7549.     bp++;
  7550.  
  7551.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  7552.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  7553.     bcopy ((char *) beg, (char *) temp->name, bp - beg);
  7554.     temp->name[bp - beg] = 0;
  7555.     temp->next = token_ptrs;
  7556.     token_ptrs = temp;
  7557.     temp->length = bp - beg;
  7558.  
  7559.     SKIP_WHITE_SPACE (bp);
  7560.  
  7561.     if (bp >= limit) {
  7562.       error ("unterminated token sequence in `#assert' or `#unassert'");
  7563.       *error_flag = -1;
  7564.       return 0;
  7565.     }
  7566.   }
  7567.   *bpp = bp;
  7568.  
  7569.   /* We accumulated the names in reverse order.
  7570.      Now reverse them to get the proper order.  */
  7571.   {
  7572.     register struct arglist *prev = 0, *this, *next;
  7573.     for (this = token_ptrs; this; this = next) {
  7574.       next = this->next;
  7575.       this->next = prev;
  7576.       prev = this;
  7577.     }
  7578.     return prev;
  7579.   }
  7580. }
  7581.  
  7582. static void
  7583. free_token_list (tokens)
  7584.      struct arglist *tokens;
  7585. {
  7586.   while (tokens) {
  7587.     struct arglist *next = tokens->next;
  7588.     free (tokens->name);
  7589.     free (tokens);
  7590.     tokens = next;
  7591.   }
  7592. }
  7593.  
  7594. /*
  7595.  * Install a name in the assertion hash table.
  7596.  *
  7597.  * If LEN is >= 0, it is the length of the name.
  7598.  * Otherwise, compute the length by scanning the entire name.
  7599.  *
  7600.  * If HASH is >= 0, it is the precomputed hash code.
  7601.  * Otherwise, compute the hash code.
  7602.  */
  7603. static ASSERTION_HASHNODE *
  7604. assertion_install (name, len, hash)
  7605.      U_CHAR *name;
  7606.      int len;
  7607.      int hash;
  7608. {
  7609.   register ASSERTION_HASHNODE *hp;
  7610.   register int i, bucket;
  7611.   register U_CHAR *p, *q;
  7612.  
  7613.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  7614.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  7615.   bucket = hash;
  7616.   hp->bucket_hdr = &assertion_hashtab[bucket];
  7617.   hp->next = assertion_hashtab[bucket];
  7618.   assertion_hashtab[bucket] = hp;
  7619.   hp->prev = NULL;
  7620.   if (hp->next != NULL)
  7621.     hp->next->prev = hp;
  7622.   hp->length = len;
  7623.   hp->value = 0;
  7624.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  7625.   p = hp->name;
  7626.   q = name;
  7627.   for (i = 0; i < len; i++)
  7628.     *p++ = *q++;
  7629.   hp->name[len] = 0;
  7630.   return hp;
  7631. }
  7632.  
  7633. /*
  7634.  * find the most recent hash node for name name (ending with first
  7635.  * non-identifier char) installed by install
  7636.  *
  7637.  * If LEN is >= 0, it is the length of the name.
  7638.  * Otherwise, compute the length by scanning the entire name.
  7639.  *
  7640.  * If HASH is >= 0, it is the precomputed hash code.
  7641.  * Otherwise, compute the hash code.
  7642.  */
  7643. static ASSERTION_HASHNODE *
  7644. assertion_lookup (name, len, hash)
  7645.      U_CHAR *name;
  7646.      int len;
  7647.      int hash;
  7648. {
  7649.   register ASSERTION_HASHNODE *bucket;
  7650.  
  7651.   bucket = assertion_hashtab[hash];
  7652.   while (bucket) {
  7653.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  7654.       return bucket;
  7655.     bucket = bucket->next;
  7656.   }
  7657.   return NULL;
  7658. }
  7659.  
  7660. static void
  7661. delete_assertion (hp)
  7662.      ASSERTION_HASHNODE *hp;
  7663. {
  7664.  
  7665.   if (hp->prev != NULL)
  7666.     hp->prev->next = hp->next;
  7667.   if (hp->next != NULL)
  7668.     hp->next->prev = hp->prev;
  7669.  
  7670.   /* make sure that the bucket chain header that
  7671.      the deleted guy was on points to the right thing afterwards. */
  7672.   if (hp == *hp->bucket_hdr)
  7673.     *hp->bucket_hdr = hp->next;
  7674.  
  7675.   free (hp);
  7676. }
  7677.  
  7678. /*
  7679.  * interpret #line command.  Remembers previously seen fnames
  7680.  * in its very own hash table.
  7681.  */
  7682. #define FNAME_HASHSIZE 37
  7683.  
  7684. static int
  7685. do_line (buf, limit, op, keyword)
  7686.      U_CHAR *buf, *limit;
  7687.      FILE_BUF *op;
  7688.      struct directive *keyword;
  7689. {
  7690.   register U_CHAR *bp;
  7691.   FILE_BUF *ip = &instack[indepth];
  7692.   FILE_BUF tem;
  7693.   int new_lineno;
  7694.   enum file_change_code file_change = same_file;
  7695.  
  7696.   /* Expand any macros.  */
  7697.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  7698.  
  7699.   /* Point to macroexpanded line, which is null-terminated now.  */
  7700.   bp = tem.buf;
  7701.   SKIP_WHITE_SPACE (bp);
  7702.  
  7703.   if (!isdigit (*bp)) {
  7704.     error ("invalid format `#line' command");
  7705.     return 0;
  7706.   }
  7707.  
  7708.   /* The Newline at the end of this line remains to be processed.
  7709.      To put the next line at the specified line number,
  7710.      we must store a line number now that is one less.  */
  7711.   new_lineno = atoi (bp) - 1;
  7712.  
  7713.   /* NEW_LINENO is one less than the actual line number here.  */
  7714.   if (pedantic && new_lineno < 0)
  7715.     pedwarn ("line number out of range in `#line' command");
  7716.  
  7717.   /* skip over the line number.  */
  7718.   while (isdigit (*bp))
  7719.     bp++;
  7720.  
  7721. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  7722.   if (*bp && !is_space[*bp]) {
  7723.     error ("invalid format `#line' command");
  7724.     return;
  7725.   }
  7726. #endif
  7727.  
  7728.   SKIP_WHITE_SPACE (bp);
  7729.  
  7730.   if (*bp == '\"') {
  7731.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  7732.     HASHNODE *hp, **hash_bucket;
  7733.     U_CHAR *fname, *p;
  7734.     int fname_length;
  7735.  
  7736.     fname = ++bp;
  7737.  
  7738.     /* Turn the file name, which is a character string literal,
  7739.        into a null-terminated string.  Do this in place.  */
  7740.     p = bp;
  7741.     for (;;)
  7742.       switch ((*p++ = *bp++)) {
  7743.       case '\0':
  7744.     error ("invalid format `#line' command");
  7745.     return 0;
  7746.  
  7747.       case '\\':
  7748.     {
  7749.       char *bpc = (char *) bp;
  7750.       int c = parse_escape (&bpc);
  7751.       bp = (U_CHAR *) bpc;
  7752.       if (c < 0)
  7753.         p--;
  7754.       else
  7755.         p[-1] = c;
  7756.     }
  7757.     break;
  7758.  
  7759.       case '\"':
  7760.     p[-1] = 0;
  7761.     goto fname_done;
  7762.       }
  7763.   fname_done:
  7764.     fname_length = p - fname;
  7765.  
  7766.     SKIP_WHITE_SPACE (bp);
  7767.     if (*bp) {
  7768.       if (pedantic)
  7769.     pedwarn ("garbage at end of `#line' command");
  7770.       if (*bp == '1')
  7771.     file_change = enter_file;
  7772.       else if (*bp == '2')
  7773.     file_change = leave_file;
  7774.       else if (*bp == '3')
  7775.     ip->system_header_p = 1;
  7776.       else if (*bp == '4')
  7777.     ip->system_header_p = 2;
  7778.       else {
  7779.     error ("invalid format `#line' command");
  7780.     return 0;
  7781.       }
  7782.  
  7783.       bp++;
  7784.       SKIP_WHITE_SPACE (bp);
  7785.       if (*bp == '3') {
  7786.     ip->system_header_p = 1;
  7787.     bp++;
  7788.     SKIP_WHITE_SPACE (bp);
  7789.       }
  7790.       if (*bp == '4') {
  7791.     ip->system_header_p = 2;
  7792.     bp++;
  7793.     SKIP_WHITE_SPACE (bp);
  7794.       }
  7795.       if (*bp) {
  7796.     error ("invalid format `#line' command");
  7797.     return 0;
  7798.       }
  7799.     }
  7800.  
  7801.     hash_bucket =
  7802.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  7803.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  7804.       if (hp->length == fname_length &&
  7805.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  7806.     ip->nominal_fname = hp->value.cpval;
  7807.     break;
  7808.       }
  7809.     if (hp == 0) {
  7810.       /* Didn't find it; cons up a new one.  */
  7811.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  7812.       hp->next = *hash_bucket;
  7813.       *hash_bucket = hp;
  7814.  
  7815.       hp->length = fname_length;
  7816.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  7817.       bcopy (fname, hp->value.cpval, fname_length);
  7818.     }
  7819.   } else if (*bp) {
  7820.     error ("invalid format `#line' command");
  7821.     return 0;
  7822.   }
  7823.  
  7824.   ip->lineno = new_lineno;
  7825.   output_line_command (ip, op, 0, file_change);
  7826.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  7827.   return 0;
  7828. }
  7829.  
  7830. /*
  7831.  * remove the definition of a symbol from the symbol table.
  7832.  * according to un*x /lib/cpp, it is not an error to undef
  7833.  * something that has no definitions, so it isn't one here either.
  7834.  */
  7835.  
  7836. static int
  7837. do_undef (buf, limit, op, keyword)
  7838.      U_CHAR *buf, *limit;
  7839.      FILE_BUF *op;
  7840.      struct directive *keyword;
  7841. {
  7842.   int sym_length;
  7843.   HASHNODE *hp;
  7844.   U_CHAR *orig_buf = buf;
  7845.  
  7846.   /* If this is a precompiler run (with -pcp) pass thru #undef commands.  */
  7847.   if (pcp_outfile && op)
  7848.     pass_thru_directive (buf, limit, op, keyword);
  7849.  
  7850.   SKIP_WHITE_SPACE (buf);
  7851.   sym_length = check_macro_name (buf, "macro");
  7852.  
  7853.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  7854.     /* If we are generating additional info for debugging (with -g) we
  7855.        need to pass through all effective #undef commands.  */
  7856.     if (debug_output && op)
  7857.       pass_thru_directive (orig_buf, limit, op, keyword);
  7858.     if (hp->type != T_MACRO)
  7859.       warning ("undefining `%s'", hp->name);
  7860. #ifdef NEXT_CPP_SERVER
  7861.     if (segregate_output && hp->owner && !hp->owner->macro_list) 
  7862.       hp->owner->macro_count--;
  7863. #endif
  7864.     delete_macro (hp);
  7865.   }
  7866.  
  7867.   if (pedantic) {
  7868.     buf += sym_length;
  7869.     SKIP_WHITE_SPACE (buf);
  7870.     if (buf != limit)
  7871.       pedwarn ("garbage after `#undef' directive");
  7872.   }
  7873.   return 0;
  7874. }
  7875.  
  7876. /*
  7877.  * Report an error detected by the program we are processing.
  7878.  * Use the text of the line in the error message.
  7879.  * (We use error because it prints the filename & line#.)
  7880.  */
  7881.  
  7882. static int
  7883. do_error (buf, limit, op, keyword)
  7884.      U_CHAR *buf, *limit;
  7885.      FILE_BUF *op;
  7886.      struct directive *keyword;
  7887. {
  7888.   int length = limit - buf;
  7889.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  7890.   bcopy ((char *) buf, (char *) copy, length);
  7891.   copy[length] = 0;
  7892.   SKIP_WHITE_SPACE (copy);
  7893.   error ("#error %s", copy);
  7894.   return 0;
  7895. }
  7896.  
  7897. /*
  7898.  * Report a warning detected by the program we are processing.
  7899.  * Use the text of the line in the warning message, then continue.
  7900.  * (We use error because it prints the filename & line#.)
  7901.  */
  7902.  
  7903. static int
  7904. do_warning (buf, limit, op, keyword)
  7905.      U_CHAR *buf, *limit;
  7906.      FILE_BUF *op;
  7907.      struct directive *keyword;
  7908. {
  7909.   int length = limit - buf;
  7910.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  7911.   bcopy ((char *) buf, (char *) copy, length);
  7912.   copy[length] = 0;
  7913.   SKIP_WHITE_SPACE (copy);
  7914.   warning ("#warning %s", copy);
  7915.   return 0;
  7916. }
  7917.  
  7918. /* Remember the name of the current file being read from so that we can
  7919.    avoid ever including it again.  */
  7920.  
  7921. static int
  7922. do_once ()
  7923. {
  7924.   int i;
  7925.   FILE_BUF *ip = NULL;
  7926.  
  7927.   for (i = indepth; i >= 0; i--)
  7928.     if (instack[i].fname != NULL) {
  7929.       ip = &instack[i];
  7930.       break;
  7931.     }
  7932.  
  7933.   if (ip != NULL) {
  7934.     struct file_name_list *new;
  7935.     
  7936.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  7937.     new->next = dont_repeat_files;
  7938.     dont_repeat_files = new;
  7939.     new->fname = savestring (ip->fname);
  7940.     new->control_macro = 0;
  7941.     new->got_name_map = 0;
  7942.     new->c_system_include_path = 0;
  7943.   }
  7944.   return 0;
  7945. }
  7946.  
  7947. /* #ident has already been copied to the output file, so just ignore it.  */
  7948.  
  7949. static int
  7950. do_ident (buf, limit)
  7951.      U_CHAR *buf, *limit;
  7952. {
  7953.   FILE_BUF trybuf;
  7954.   int len;
  7955.   FILE_BUF *op = &outbuf;
  7956.  
  7957.   /* Allow #ident in system headers, since that's not user's fault.  */
  7958.   if (pedantic && !instack[indepth].system_header_p)
  7959.     pedwarn ("ANSI C does not allow `#ident'");
  7960.  
  7961.   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  7962.   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  7963.   bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  7964.   limit = buf + (trybuf.bufp - trybuf.buf);
  7965.   len = (limit - buf);
  7966.   free (trybuf.buf);
  7967.  
  7968.   /* Output directive name.  */
  7969.   check_expand (op, 8);
  7970.   bcopy ("#ident ", (char *) op->bufp, 7);
  7971.   op->bufp += 7;
  7972.  
  7973.   /* Output the expanded argument line.  */
  7974.   check_expand (op, len);
  7975.   bcopy ((char *) buf, (char *) op->bufp, len);
  7976.   op->bufp += len;
  7977.  
  7978.   return 0;
  7979. }
  7980.  
  7981. /* #pragma and its argument line have already been copied to the output file.
  7982.    Just check for some recognized pragmas that need validation here.  */
  7983.  
  7984. static int
  7985. do_pragma (buf, limit)
  7986.      U_CHAR *buf, *limit;
  7987. {
  7988.   while (*buf == ' ' || *buf == '\t')
  7989.     buf++;
  7990.   if (!strncmp (buf, "once", 4)) {
  7991.     /* Allow #pragma once in system headers, since that's not the user's
  7992.        fault.  */
  7993.     if (!instack[indepth].system_header_p)
  7994.       warning ("`#pragma once' is obsolete");
  7995.     do_once ();
  7996.   }
  7997.  
  7998.   if (!strncmp (buf, "cplusplus", 9))
  7999.     {
  8000.       if (instack[indepth].system_header_p == 2 && cplusplus)
  8001.     {
  8002.       instack[indepth].system_header_p = 1;
  8003.       output_line_command (&instack[indepth], &outbuf, 0, same_file);
  8004.     }
  8005.     }
  8006.  
  8007.   if (!strncmp (buf, "implementation", 14)) {
  8008.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  8009.        been included yet.  */
  8010.     struct file_name_list *ptr;
  8011.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  8012.     SKIP_WHITE_SPACE (p);
  8013.     if (*p == '\n' || *p != '\"')
  8014.       return 0;
  8015.  
  8016.     fname = p + 1;
  8017.     if (p = (U_CHAR *) index (fname, '\"'))
  8018.       *p = '\0';
  8019.     
  8020.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  8021.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  8022.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  8023.       if (inc_fname && !strcmp (inc_fname, fname))
  8024.     warning ("`#pragma implementation' for `%s' appears after file is included",
  8025.          fname);
  8026.     }
  8027.   }
  8028.  
  8029.   return 0;
  8030. }
  8031.  
  8032. #if 0
  8033. /* This was a fun hack, but #pragma seems to start to be useful.
  8034.    By failing to recognize it, we pass it through unchanged to cc1.  */
  8035.  
  8036. /*
  8037.  * the behavior of the #pragma directive is implementation defined.
  8038.  * this implementation defines it as follows.
  8039.  */
  8040.  
  8041. static int
  8042. do_pragma ()
  8043. {
  8044.   close (0);
  8045.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  8046.     goto nope;
  8047.   close (1);
  8048.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  8049.     goto nope;
  8050.   execl ("/usr/games/hack", "#pragma", 0);
  8051.   execl ("/usr/games/rogue", "#pragma", 0);
  8052.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  8053.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  8054. nope:
  8055.   fatal ("You are in a maze of twisty compiler features, all different");
  8056. }
  8057. #endif
  8058.  
  8059. /* Just ignore #sccs, on systems where we define it at all.  */
  8060.  
  8061. static int
  8062. do_sccs ()
  8063. {
  8064.   if (pedantic)
  8065.     pedwarn ("ANSI C does not allow `#sccs'");
  8066.   return 0;
  8067. }
  8068.  
  8069. /*
  8070.  * handle #if command by
  8071.  *   1) inserting special `defined' keyword into the hash table
  8072.  *    that gets turned into 0 or 1 by special_symbol (thus,
  8073.  *    if the luser has a symbol called `defined' already, it won't
  8074.  *      work inside the #if command)
  8075.  *   2) rescan the input into a temporary output buffer
  8076.  *   3) pass the output buffer to the yacc parser and collect a value
  8077.  *   4) clean up the mess left from steps 1 and 2.
  8078.  *   5) call conditional_skip to skip til the next #endif (etc.),
  8079.  *      or not, depending on the value from step 3.
  8080.  */
  8081.  
  8082. static int
  8083. do_if (buf, limit, op, keyword)
  8084.      U_CHAR *buf, *limit;
  8085.      FILE_BUF *op;
  8086.      struct directive *keyword;
  8087. {
  8088.   int value;
  8089.   FILE_BUF *ip = &instack[indepth];
  8090.  
  8091.   value = eval_if_expression (buf, limit - buf);
  8092. #ifdef NEXT_CPP_SERVER
  8093.   conditional_skip (ip, op, value == 0, T_IF, NULL_PTR);
  8094. #else
  8095.   conditional_skip (ip, value == 0, T_IF, NULL_PTR);
  8096. #endif
  8097.   return 0;
  8098. }
  8099.  
  8100. /*
  8101.  * handle a #elif directive by not changing  if_stack  either.
  8102.  * see the comment above do_else.
  8103.  */
  8104.  
  8105. static int
  8106. do_elif (buf, limit, op, keyword)
  8107.      U_CHAR *buf, *limit;
  8108.      FILE_BUF *op;
  8109.      struct directive *keyword;
  8110. {
  8111.   int value;
  8112.   FILE_BUF *ip = &instack[indepth];
  8113.  
  8114.   if (if_stack == instack[indepth].if_stack) {
  8115.     error ("`#elif' not within a conditional");
  8116.     return 0;
  8117.   } else {
  8118.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  8119.       error ("`#elif' after `#else'");
  8120. #ifdef REPORT_EVENT
  8121.       REPORT_EVENT (0, NULL, if_stack->fname, if_stack->lineno,
  8122.             "matching conditional", 0, 0, 0);
  8123. #endif
  8124.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  8125.       if (if_stack->fname != NULL && ip->fname != NULL &&
  8126.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  8127.     fprintf (stderr, ", file %s", if_stack->fname);
  8128.       fprintf (stderr, ")\n");
  8129.     }
  8130.     if_stack->type = T_ELIF;
  8131.   }
  8132.  
  8133.   if (if_stack->if_succeeded)
  8134. #ifdef NEXT_CPP_SERVER
  8135.     skip_if_group (ip, op, 0);
  8136. #else
  8137.     skip_if_group (ip, 0);
  8138. #endif
  8139.   else {
  8140.     value = eval_if_expression (buf, limit - buf);
  8141.     if (value == 0)
  8142. #ifdef NEXT_CPP_SERVER
  8143.       skip_if_group (ip, op, 0);
  8144. #else
  8145.       skip_if_group (ip, 0);
  8146. #endif
  8147.     else {
  8148.       ++if_stack->if_succeeded;    /* continue processing input */
  8149.       output_line_command (ip, op, 1, same_file);
  8150.     }
  8151.   }
  8152.   return 0;
  8153. }
  8154.  
  8155. /*
  8156.  * evaluate a #if expression in BUF, of length LENGTH,
  8157.  * then parse the result as a C expression and return the value as an int.
  8158.  */
  8159. static int
  8160. eval_if_expression (buf, length)
  8161.      U_CHAR *buf;
  8162.      int length;
  8163. {
  8164.   FILE_BUF temp_obuf;
  8165.   HASHNODE *save_defined;
  8166.   int value;
  8167.  
  8168.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
  8169.   pcp_inside_if = 1;
  8170.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  8171.   pcp_inside_if = 0;
  8172.   delete_macro (save_defined);    /* clean up special symbol */
  8173.  
  8174.   value = parse_c_expression (temp_obuf.buf);
  8175.  
  8176.   free (temp_obuf.buf);
  8177.  
  8178.   return value;
  8179. }
  8180.  
  8181. /*
  8182.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  8183.  * then do or don't skip to the #endif/#else/#elif depending
  8184.  * on what directive is actually being processed.
  8185.  */
  8186.  
  8187. static int
  8188. do_xifdef (buf, limit, op, keyword)
  8189.      U_CHAR *buf, *limit;
  8190.      FILE_BUF *op;
  8191.      struct directive *keyword;
  8192. {
  8193.   int skip;
  8194.   FILE_BUF *ip = &instack[indepth];
  8195.   U_CHAR *end; 
  8196.   int start_of_file = 0;
  8197.   U_CHAR *control_macro = 0;
  8198.  
  8199.   /* Detect a #ifndef at start of file (not counting comments).  */
  8200.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  8201.     U_CHAR *p = ip->buf;
  8202.     while (p != directive_start) {
  8203.       U_CHAR c = *p++;
  8204.       if (is_space[c])
  8205.     ;
  8206.       else if (c == '/' && p != ip->bufp && *p == '*') {
  8207.     /* Skip this comment.  */
  8208.     int junk = 0;
  8209.     U_CHAR *save_bufp = ip->bufp;
  8210.     ip->bufp = p + 1;
  8211. #ifdef NEXT_CPP_SERVER
  8212.     p = skip_to_end_of_comment (ip, op, &junk, 1);
  8213. #else
  8214.     p = skip_to_end_of_comment (ip, &junk, 1);
  8215. #endif
  8216.     ip->bufp = save_bufp;
  8217.       } else {
  8218.     goto fail;
  8219.       }
  8220.     }
  8221.     /* If we get here, this conditional is the beginning of the file.  */
  8222.     start_of_file = 1;
  8223.   fail: ;
  8224.   }
  8225.  
  8226.   /* Discard leading and trailing whitespace.  */
  8227.   SKIP_WHITE_SPACE (buf);
  8228.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  8229.  
  8230.   /* Find the end of the identifier at the beginning.  */
  8231.   for (end = buf; is_idchar[*end]; end++);
  8232.  
  8233.   if (end == buf) {
  8234.     skip = (keyword->type == T_IFDEF);
  8235.     if (! traditional)
  8236.       pedwarn (end == limit ? "`#%s' with no argument"
  8237.            : "`#%s' argument starts with punctuation",
  8238.            keyword->name);
  8239.   } else {
  8240.     HASHNODE *hp;
  8241.  
  8242.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  8243.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  8244.     else if (end != limit && !traditional)
  8245.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  8246.  
  8247.     hp = lookup (buf, end-buf, -1);
  8248.  
  8249.     if (pcp_outfile) {
  8250.       /* Output a precondition for this macro.  */
  8251.       if (hp && hp->value.defn->predefined)
  8252.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  8253.       else {
  8254.     U_CHAR *cp = buf;
  8255.     fprintf (pcp_outfile, "#undef ");
  8256.     while (is_idchar[*cp]) /* Ick! */
  8257.       fputc (*cp++, pcp_outfile);
  8258.     putc ('\n', pcp_outfile);
  8259.       }
  8260.     }
  8261.  
  8262.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  8263.     if (start_of_file && !skip) {
  8264.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  8265.       bcopy ((char *) buf, (char *) control_macro, end - buf);
  8266.       control_macro[end - buf] = 0;
  8267.     }
  8268.   }
  8269.   
  8270. #ifdef NEXT_CPP_SERVER
  8271.   conditional_skip (ip, op, skip, T_IF, control_macro);
  8272. #else
  8273.   conditional_skip (ip, skip, T_IF, control_macro);
  8274. #endif
  8275.   return 0;
  8276. }
  8277.  
  8278. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  8279.    If this is a #ifndef starting at the beginning of a file,
  8280.    CONTROL_MACRO is the macro name tested by the #ifndef.
  8281.    Otherwise, CONTROL_MACRO is 0.  */
  8282.  
  8283. #ifdef NEXT_CPP_SERVER
  8284. static void
  8285. conditional_skip (ip, op, skip, type, control_macro)
  8286.      FILE_BUF *ip;
  8287.      FILE_BUF *op;
  8288.      int skip;
  8289.      enum node_type type;
  8290.      U_CHAR *control_macro;
  8291. #else
  8292. static void
  8293. conditional_skip (ip, skip, type, control_macro)
  8294.      FILE_BUF *ip;
  8295.      int skip;
  8296.      enum node_type type;
  8297.      U_CHAR *control_macro;
  8298. #endif
  8299. {
  8300.   IF_STACK_FRAME *temp;
  8301.  
  8302.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  8303.   temp->fname = ip->nominal_fname;
  8304.   temp->lineno = ip->lineno;
  8305.   temp->next = if_stack;
  8306.   temp->control_macro = control_macro;
  8307.   if_stack = temp;
  8308.  
  8309.   if_stack->type = type;
  8310.  
  8311.   if (skip != 0) {
  8312. #ifdef NEXT_CPP_SERVER
  8313.     skip_if_group (ip, op, 0);
  8314. #else
  8315.     skip_if_group (ip, 0);
  8316. #endif
  8317.     return;
  8318.   } else {
  8319.     ++if_stack->if_succeeded;
  8320. #ifdef NEXT_CPP_SERVER
  8321.     output_line_command (ip, op, 1, same_file);
  8322. #else
  8323.     output_line_command (ip, &outbuf, 1, same_file);
  8324. #endif
  8325.   }
  8326. }
  8327.  
  8328. /*
  8329.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  8330.  * leaves input ptr at the sharp sign found.
  8331.  * If ANY is nonzero, return at next directive of any sort.
  8332.  */
  8333. #ifdef NEXT_CPP_SERVER
  8334. static void
  8335. skip_if_group (ip, op, any)
  8336.      FILE_BUF *ip;
  8337.      FILE_BUF *op;
  8338.      int any;
  8339. #else
  8340. static void
  8341. skip_if_group (ip, any)
  8342.      FILE_BUF *ip;
  8343.      int any;
  8344. #endif
  8345. {
  8346.   register U_CHAR *bp = ip->bufp, *cp;
  8347.   register U_CHAR *endb = ip->buf + ip->length;
  8348.   struct directive *kt;
  8349.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  8350.   U_CHAR *beg_of_line = bp;
  8351.   register int ident_length;
  8352.   U_CHAR *ident, *after_ident;
  8353.  
  8354. #ifdef NEXT_CPP_SERVER
  8355.   if (any && segregate_output && aggressive_skip_if_group(ip))
  8356.     return;
  8357. #endif
  8358.  
  8359.   while (bp < endb) {
  8360.     switch (*bp++) {
  8361.     case '/':            /* possible comment */
  8362.       if (*bp == '\\' && bp[1] == '\n')
  8363.     newline_fix (bp);
  8364.       if (*bp == '*'
  8365.       || (cplusplus_comments && *bp == '/')) {
  8366.     ip->bufp = ++bp;
  8367. #ifdef NEXT_CPP_SERVER
  8368.     bp = skip_to_end_of_comment (ip, op, &ip->lineno, 0);
  8369. #else
  8370.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  8371. #endif
  8372.       }
  8373.       break;
  8374.     case '\"':
  8375.     case '\'':
  8376.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  8377.                    NULL_PTR, NULL_PTR);
  8378.       break;
  8379.     case '\\':
  8380.       /* Char after backslash loses its special meaning.  */
  8381.       if (bp < endb) {
  8382.     if (*bp == '\n')
  8383.       ++ip->lineno;        /* But do update the line-count.  */
  8384.     bp++;
  8385.       }
  8386.       break;
  8387.     case '\n':
  8388.       ++ip->lineno;
  8389.       beg_of_line = bp;
  8390.       break;
  8391.     case '#':
  8392.       ip->bufp = bp - 1;
  8393.  
  8394.       /* # keyword: a # must be first nonblank char on the line */
  8395.       if (beg_of_line == 0)
  8396.     break;
  8397.       /* Scan from start of line, skipping whitespace, comments
  8398.      and backslash-newlines, and see if we reach this #.
  8399.      If not, this # is not special.  */
  8400.       bp = beg_of_line;
  8401.       /* If -traditional, require # to be at beginning of line.  */
  8402.       if (!traditional)
  8403.     while (1) {
  8404.       if (is_hor_space[*bp])
  8405.         bp++;
  8406.       else if (*bp == '\\' && bp[1] == '\n')
  8407.         bp += 2;
  8408.       else if (*bp == '/' && bp[1] == '*') {
  8409.         bp += 2;
  8410.         while (!(*bp == '*' && bp[1] == '/'))
  8411.           bp++;
  8412.         bp += 2;
  8413.       }
  8414.       /* There is no point in trying to deal with C++ // comments here,
  8415.          because if there is one, then this # must be part of the
  8416.          comment and we would never reach here.  */
  8417.       else break;
  8418.     }
  8419.       if (bp != ip->bufp) {
  8420.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  8421.     break;
  8422.       }
  8423.  
  8424.       bp = ip->bufp + 1;    /* Point after the '#' */
  8425.  
  8426.       /* Skip whitespace and \-newline.  */
  8427.       while (1) {
  8428.     if (is_hor_space[*bp])
  8429.       bp++;
  8430.     else if (*bp == '\\' && bp[1] == '\n')
  8431.       bp += 2;
  8432.     else if (*bp == '/' && bp[1] == '*') {
  8433.       bp += 2;
  8434.       while (!(*bp == '*' && bp[1] == '/')) {
  8435.         if (*bp == '\n')
  8436.           ip->lineno++;
  8437.         bp++;
  8438.       }
  8439.       bp += 2;
  8440.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  8441.       bp += 2;
  8442.       while (bp[-1] == '\\' || *bp != '\n') {
  8443.         if (*bp == '\n')
  8444.           ip->lineno++;
  8445.         bp++;
  8446.       }
  8447.         }
  8448.     else break;
  8449.       }
  8450.  
  8451.       cp = bp;
  8452.  
  8453.       /* Now find end of directive name.
  8454.      If we encounter a backslash-newline, exchange it with any following
  8455.      symbol-constituents so that we end up with a contiguous name.  */
  8456.  
  8457.       while (1) {
  8458.     if (is_idchar[*bp])
  8459.       bp++;
  8460.     else {
  8461.       if (*bp == '\\' && bp[1] == '\n')
  8462.         name_newline_fix (bp);
  8463.       if (is_idchar[*bp])
  8464.         bp++;
  8465.       else break;
  8466.     }
  8467.       }
  8468.       ident_length = bp - cp;
  8469.       ident = cp;
  8470.       after_ident = bp;
  8471.  
  8472.       /* A line of just `#' becomes blank.  */
  8473.  
  8474.       if (ident_length == 0 && *after_ident == '\n') {
  8475.     continue;
  8476.       }
  8477.  
  8478.       if (ident_length == 0 || !is_idstart[*ident]) {
  8479.     U_CHAR *p = ident;
  8480.     while (is_idchar[*p]) {
  8481.       if (*p < '0' || *p > '9')
  8482.         break;
  8483.       p++;
  8484.     }
  8485.     /* Handle # followed by a line number.  */
  8486.     if (p != ident && !is_idchar[*p]) {
  8487.       if (pedantic)
  8488.         pedwarn ("`#' followed by integer");
  8489.       continue;
  8490.     }
  8491.  
  8492.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  8493.     if (p == ident) {
  8494.       while (*p == '#' || is_hor_space[*p]) p++;
  8495.       if (*p == '\n') {
  8496.         if (pedantic && !lang_asm)
  8497.           pedwarn ("invalid preprocessor directive");
  8498.         continue;
  8499.       }
  8500.     }
  8501.  
  8502.     if (!lang_asm && pedantic)
  8503.       pedwarn ("invalid preprocessor directive name");
  8504.     continue;
  8505.       }
  8506.  
  8507.       for (kt = directive_table; kt->length >= 0; kt++) {
  8508.     IF_STACK_FRAME *temp;
  8509.     if (ident_length == kt->length
  8510.         && strncmp (cp, kt->name, kt->length) == 0) {
  8511.       /* If we are asked to return on next directive, do so now.  */
  8512.       if (any)
  8513.         return;
  8514.  
  8515.       switch (kt->type) {
  8516.       case T_IF:
  8517.       case T_IFDEF:
  8518.       case T_IFNDEF:
  8519.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  8520.         temp->next = if_stack;
  8521.         if_stack = temp;
  8522.         temp->lineno = ip->lineno;
  8523.         temp->fname = ip->nominal_fname;
  8524.         temp->type = kt->type;
  8525.         break;
  8526.       case T_ELSE:
  8527.       case T_ENDIF:
  8528.         if (pedantic && if_stack != save_if_stack)
  8529.           validate_else (bp);
  8530.       case T_ELIF:
  8531.         if (if_stack == instack[indepth].if_stack) {
  8532.           error ("`#%s' not within a conditional", kt->name);
  8533.           break;
  8534.         }
  8535.         else if (if_stack == save_if_stack)
  8536.           return;        /* found what we came for */
  8537.  
  8538.         if (kt->type != T_ENDIF) {
  8539.           if (if_stack->type == T_ELSE)
  8540.         error ("`#else' or `#elif' after `#else'");
  8541.           if_stack->type = kt->type;
  8542.           break;
  8543.         }
  8544.  
  8545.         temp = if_stack;
  8546.         if_stack = if_stack->next;
  8547.         free (temp);
  8548.         break;
  8549.       }
  8550.       break;
  8551.     }
  8552.       }
  8553.       /* Don't let erroneous code go by.  */
  8554.       if (kt->length < 0 && !lang_asm && pedantic)
  8555.     pedwarn ("invalid preprocessor directive name");
  8556.     }
  8557.   }
  8558.   ip->bufp = bp;
  8559.   /* after this returns, rescan will exit because ip->bufp
  8560.      now points to the end of the buffer.
  8561.      rescan is responsible for the error message also.  */
  8562. }
  8563.  
  8564. /*
  8565.  * handle a #else directive.  Do this by just continuing processing
  8566.  * without changing  if_stack ;  this is so that the error message
  8567.  * for missing #endif's etc. will point to the original #if.  It
  8568.  * is possible that something different would be better.
  8569.  */
  8570.  
  8571. static int
  8572. do_else (buf, limit, op, keyword)
  8573.      U_CHAR *buf, *limit;
  8574.      FILE_BUF *op;
  8575.      struct directive *keyword;
  8576. {
  8577.   FILE_BUF *ip = &instack[indepth];
  8578.  
  8579.   if (pedantic) {
  8580.     SKIP_WHITE_SPACE (buf);
  8581.     if (buf != limit)
  8582.       pedwarn ("text following `#else' violates ANSI standard");
  8583.   }
  8584.  
  8585.   if (if_stack == instack[indepth].if_stack) {
  8586.     error ("`#else' not within a conditional");
  8587.     return 0;
  8588.   } else {
  8589.     /* #ifndef can't have its special treatment for containing the whole file
  8590.        if it has a #else clause.  */
  8591.     if_stack->control_macro = 0;
  8592.  
  8593.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  8594.       error ("`#else' after `#else'");
  8595. #ifdef REPORT_EVENT
  8596.       REPORT_EVENT (0, NULL, if_stack->fname, if_stack->lineno,
  8597.             "matching conditional", 0, 0, 0);
  8598. #endif
  8599.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  8600.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  8601.     fprintf (stderr, ", file %s", if_stack->fname);
  8602.       fprintf (stderr, ")\n");
  8603.     }
  8604.     if_stack->type = T_ELSE;
  8605.   }
  8606.  
  8607.   if (if_stack->if_succeeded)
  8608. #ifdef NEXT_CPP_SERVER
  8609.     skip_if_group (ip, op, 0);
  8610. #else
  8611.     skip_if_group (ip, 0);
  8612. #endif
  8613.   else {
  8614.     ++if_stack->if_succeeded;    /* continue processing input */
  8615.     output_line_command (ip, op, 1, same_file);
  8616.   }
  8617.   return 0;
  8618. }
  8619.  
  8620. /*
  8621.  * unstack after #endif command
  8622.  */
  8623.  
  8624. static int
  8625. do_endif (buf, limit, op, keyword)
  8626.      U_CHAR *buf, *limit;
  8627.      FILE_BUF *op;
  8628.      struct directive *keyword;
  8629. {
  8630.   if (pedantic) {
  8631.     SKIP_WHITE_SPACE (buf);
  8632.     if (buf != limit)
  8633.       pedwarn ("text following `#endif' violates ANSI standard");
  8634.   }
  8635.  
  8636.   if (if_stack == instack[indepth].if_stack)
  8637.     error ("unbalanced `#endif'");
  8638.   else {
  8639.     IF_STACK_FRAME *temp = if_stack;
  8640.     if_stack = if_stack->next;
  8641.     if (temp->control_macro != 0) {
  8642.       /* This #endif matched a #ifndef at the start of the file.
  8643.      See if it is at the end of the file.  */
  8644.       FILE_BUF *ip = &instack[indepth];
  8645.       U_CHAR *p = ip->bufp;
  8646.       U_CHAR *ep = ip->buf + ip->length;
  8647.  
  8648.       while (p != ep) {
  8649.     U_CHAR c = *p++;
  8650.     switch (c) {
  8651.     case ' ':
  8652.     case '\t':
  8653.     case '\n':
  8654.       break;
  8655.     case '/':
  8656.       if (p != ep && *p == '*') {
  8657.         /* Skip this comment.  */
  8658.         int junk = 0;
  8659.         U_CHAR *save_bufp = ip->bufp;
  8660.         ip->bufp = p + 1;
  8661. #ifdef NEXT_CPP_SERVER
  8662.         p = skip_to_end_of_comment (ip, op, &junk, 1);
  8663. #else
  8664.         p = skip_to_end_of_comment (ip, &junk, 1);
  8665. #endif
  8666.         ip->bufp = save_bufp;
  8667.       }
  8668.       break;
  8669.     default:
  8670.       goto fail;
  8671.     }
  8672.       }
  8673.       /* If we get here, this #endif ends a #ifndef
  8674.      that contains all of the file (aside from whitespace).
  8675.      Arrange not to include the file again
  8676.      if the macro that was tested is defined.
  8677.  
  8678.      Do not do this for the top-level file in a -include or any
  8679.      file in a -imacros.  */
  8680.       if (indepth != 0
  8681.       && ! (indepth == 1 && no_record_file)
  8682.       && ! (no_record_file && no_output))
  8683.     record_control_macro (ip->fname, temp->control_macro);
  8684.     fail: ;
  8685.     }
  8686.     free (temp);
  8687.     output_line_command (&instack[indepth], op, 1, same_file);
  8688.   }
  8689.   return 0;
  8690. }
  8691.  
  8692. /* When an #else or #endif is found while skipping failed conditional,
  8693.    if -pedantic was specified, this is called to warn about text after
  8694.    the command name.  P points to the first char after the command name.  */
  8695.  
  8696. static void
  8697. validate_else (p)
  8698.      register U_CHAR *p;
  8699. {
  8700.   /* Advance P over whitespace and comments.  */
  8701.   while (1) {
  8702.     if (*p == '\\' && p[1] == '\n')
  8703.       p += 2;
  8704.     if (is_hor_space[*p])
  8705.       p++;
  8706.     else if (*p == '/') {
  8707.       if (p[1] == '\\' && p[2] == '\n')
  8708.     newline_fix (p + 1);
  8709.       if (p[1] == '*') {
  8710.     p += 2;
  8711.     /* Don't bother warning about unterminated comments
  8712.        since that will happen later.  Just be sure to exit.  */
  8713.     while (*p) {
  8714.       if (p[1] == '\\' && p[2] == '\n')
  8715.         newline_fix (p + 1);
  8716.       if (*p == '*' && p[1] == '/') {
  8717.         p += 2;
  8718.         break;
  8719.       }
  8720.       p++;
  8721.     }
  8722.       }
  8723.       else if (cplusplus_comments && p[1] == '/') {
  8724.     p += 2;
  8725.     while (*p && (*p != '\n' || p[-1] == '\\'))
  8726.       p++;
  8727.       }
  8728.     } else break;
  8729.   }
  8730.   if (*p && *p != '\n')
  8731.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  8732. }
  8733.  
  8734. /* Skip a comment, assuming the input ptr immediately follows the
  8735.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  8736.    (The canonical line counter is &ip->lineno.)
  8737.    Don't use this routine (or the next one) if bumping the line
  8738.    counter is not sufficient to deal with newlines in the string.
  8739.  
  8740.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  8741.    This feature is useful when processing a comment that is going to be
  8742.    processed or was processed at another point in the preprocessor,
  8743.    to avoid a duplicate warning.  */
  8744.  
  8745. #ifdef NEXT_CPP_SERVER
  8746. static U_CHAR *
  8747. skip_to_end_of_comment (ip, op, line_counter, nowarn)
  8748.      register FILE_BUF *ip;
  8749.      register FILE_BUF *op;
  8750.      int *line_counter;        /* place to remember newlines, or NULL */
  8751.      int nowarn;
  8752. #else
  8753. static U_CHAR *
  8754. skip_to_end_of_comment (ip, line_counter, nowarn)
  8755.      register FILE_BUF *ip;
  8756.      int *line_counter;        /* place to remember newlines, or NULL */
  8757.      int nowarn;
  8758. #endif
  8759. {
  8760.   register U_CHAR *limit = ip->buf + ip->length;
  8761.   register U_CHAR *bp = ip->bufp;
  8762. #ifndef NEXT_CPP_SERVER
  8763.   FILE_BUF *op = &outbuf;    /* JF */
  8764. #endif
  8765.   int output = put_out_comments && !line_counter;
  8766.   int start_line = line_counter ? *line_counter : 0;
  8767.  
  8768.     /* JF this line_counter stuff is a crock to make sure the
  8769.        comment is only put out once, no matter how many times
  8770.        the comment is skipped.  It almost works */
  8771.   if (output) {
  8772.     *op->bufp++ = '/';
  8773.     *op->bufp++ = '*';
  8774.   }
  8775.   if (cplusplus_comments && bp[-1] == '/') {
  8776.     if (output) {
  8777.       while (bp < limit) {
  8778.     *op->bufp++ = *bp;
  8779.     if (*bp == '\n' && bp[-1] != '\\')
  8780.       break;
  8781.     if (*bp == '\n') {
  8782.       ++*line_counter;
  8783.       ++op->lineno;
  8784.     }
  8785.     bp++;
  8786.       }
  8787.       op->bufp[-1] = '*';
  8788.       *op->bufp++ = '/';
  8789.       *op->bufp++ = '\n';
  8790.     } else {
  8791.       while (bp < limit) {
  8792.     if (bp[-1] != '\\' && *bp == '\n') {
  8793.       break;
  8794.     } else {
  8795.       if (*bp == '\n' && line_counter)
  8796.         ++*line_counter;
  8797.       bp++;
  8798.     }
  8799.       }
  8800.     }
  8801.     ip->bufp = bp;
  8802.     return bp;
  8803.   }
  8804.   while (bp < limit) {
  8805.     if (output)
  8806.       *op->bufp++ = *bp;
  8807.     switch (*bp++) {
  8808.     case '/':
  8809.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  8810.     warning ("`/*' within comment");
  8811.       break;
  8812.     case '\n':
  8813.       /* If this is the end of the file, we have an unterminated comment.
  8814.      Don't swallow the newline.  We are guaranteed that there will be a
  8815.      trailing newline and various pieces assume it's there.  */
  8816.       if (bp == limit)
  8817.     {
  8818.       --bp;
  8819.       --limit;
  8820.       break;
  8821.     }
  8822.       if (line_counter != NULL)
  8823.     ++*line_counter;
  8824.       if (output)
  8825.     ++op->lineno;
  8826.       break;
  8827.     case '*':
  8828.       if (*bp == '\\' && bp[1] == '\n')
  8829.     newline_fix (bp);
  8830.       if (*bp == '/') {
  8831.         if (output)
  8832.       *op->bufp++ = '/';
  8833.     ip->bufp = ++bp;
  8834.     return bp;
  8835.       }
  8836.       break;
  8837.     }
  8838.   }
  8839.  
  8840.   if (!nowarn)
  8841.     error_with_line (line_for_error (start_line), "unterminated comment");
  8842.   ip->bufp = bp;
  8843.   return bp;
  8844. }
  8845.  
  8846. /*
  8847.  * Skip over a quoted string.  BP points to the opening quote.
  8848.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  8849.  * START_LINE is the line number of the starting point (but it need
  8850.  * not be valid if the starting point is inside a macro expansion).
  8851.  *
  8852.  * The input stack state is not changed.
  8853.  *
  8854.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  8855.  * for each newline passed.
  8856.  *
  8857.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  8858.  * if we pass a backslash-newline.
  8859.  *
  8860.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  8861.  */
  8862. static U_CHAR *
  8863. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  8864.      register U_CHAR *bp;
  8865.      register U_CHAR *limit;
  8866.      int start_line;
  8867.      int *count_newlines;
  8868.      int *backslash_newlines_p;
  8869.      int *eofp;
  8870. {
  8871.   register U_CHAR c, match;
  8872.  
  8873.   match = *bp++;
  8874.   while (1) {
  8875.     if (bp >= limit) {
  8876.       error_with_line (line_for_error (start_line),
  8877.                "unterminated string or character constant");
  8878.       error_with_line (multiline_string_line,
  8879.                "possible real start of unterminated constant");
  8880.       multiline_string_line = 0;
  8881.       if (eofp)
  8882.     *eofp = 1;
  8883.       break;
  8884.     }
  8885.     c = *bp++;
  8886.     if (c == '\\') {
  8887.       while (*bp == '\\' && bp[1] == '\n') {
  8888.     if (backslash_newlines_p)
  8889.       *backslash_newlines_p = 1;
  8890.     if (count_newlines)
  8891.       ++*count_newlines;
  8892.     bp += 2;
  8893.       }
  8894.       if (*bp == '\n' && count_newlines) {
  8895.     if (backslash_newlines_p)
  8896.       *backslash_newlines_p = 1;
  8897.     ++*count_newlines;
  8898.       }
  8899.       bp++;
  8900.     } else if (c == '\n') {
  8901.       if (traditional) {
  8902.      /* Unterminated strings and character constants are 'legal'.  */
  8903.      bp--;    /* Don't consume the newline. */
  8904.      if (eofp)
  8905.        *eofp = 1;
  8906.      break;
  8907.       }
  8908.       if (pedantic || match == '\'') {
  8909.     error_with_line (line_for_error (start_line),
  8910.              "unterminated string or character constant");
  8911.     bp--;
  8912.     if (eofp)
  8913.       *eofp = 1;
  8914.     break;
  8915.       }
  8916.       /* If not traditional, then allow newlines inside strings.  */
  8917.       if (count_newlines)
  8918.     ++*count_newlines;
  8919.       if (multiline_string_line == 0)
  8920.     multiline_string_line = start_line;
  8921.     } else if (c == match)
  8922.       break;
  8923.   }
  8924.   return bp;
  8925. }
  8926.  
  8927. /* Place into DST a quoted string representing the string SRC.
  8928.    Return the address of DST's terminating null.  */
  8929. static char *
  8930. quote_string (dst, src)
  8931.      char *dst, *src;
  8932. {
  8933.   U_CHAR c;
  8934.  
  8935.   *dst++ = '\"';
  8936.   for (;;)
  8937.     switch ((c = *src++))
  8938.       {
  8939.       default:
  8940.         if (isprint (c))
  8941.       *dst++ = c;
  8942.     else
  8943.       {
  8944.         sprintf (dst, "\\%03o", c);
  8945.         dst += 4;
  8946.       }
  8947.     break;
  8948.  
  8949.       case '\"':
  8950.       case '\\':
  8951.     *dst++ = '\\';
  8952.     *dst++ = c;
  8953.     break;
  8954.       
  8955.       case '\0':
  8956.     *dst++ = '\"';
  8957.     *dst = '\0';
  8958.     return dst;
  8959.       }
  8960. }
  8961.  
  8962. /* Skip across a group of balanced parens, starting from IP->bufp.
  8963.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  8964.  
  8965.    This does not handle newlines, because it's used for the arg of #if,
  8966.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  8967.  
  8968. #ifdef NEXT_CPP_SERVER
  8969. static U_CHAR *
  8970. skip_paren_group (ip, op)
  8971.      register FILE_BUF *ip;
  8972.      register FILE_BUF *op;
  8973. #else
  8974. static U_CHAR *
  8975. skip_paren_group (ip)
  8976.      register FILE_BUF *ip;
  8977. #endif
  8978. {
  8979.   U_CHAR *limit = ip->buf + ip->length;
  8980.   U_CHAR *p = ip->bufp;
  8981.   int depth = 0;
  8982.   int lines_dummy = 0;
  8983.  
  8984.   while (p != limit) {
  8985.     int c = *p++;
  8986.     switch (c) {
  8987.     case '(':
  8988.       depth++;
  8989.       break;
  8990.  
  8991.     case ')':
  8992.       depth--;
  8993.       if (depth == 0)
  8994.     return ip->bufp = p;
  8995.       break;
  8996.  
  8997.     case '/':
  8998.       if (*p == '*') {
  8999.     ip->bufp = p;
  9000. #ifdef NEXT_CPP_SERVER
  9001.     p = skip_to_end_of_comment (ip, op, &lines_dummy, 0);
  9002. #else
  9003.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  9004. #endif
  9005.     p = ip->bufp;
  9006.       }
  9007.  
  9008.     case '"':
  9009.     case '\'':
  9010.       {
  9011.     int eofp = 0;
  9012.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  9013.     if (eofp)
  9014.       return ip->bufp = p;
  9015.       }
  9016.       break;
  9017.     }
  9018.   }
  9019.  
  9020.   ip->bufp = p;
  9021.   return p;
  9022. }
  9023.  
  9024. /*
  9025.  * write out a #line command, for instance, after an #include file.
  9026.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  9027.  * appear to be a no-op, and we can output a few newlines instead
  9028.  * if we want to increase the line number by a small amount.
  9029.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  9030.  */
  9031.  
  9032. static void
  9033. output_line_command (ip, op, conditional, file_change)
  9034.      FILE_BUF *ip, *op;
  9035.      int conditional;
  9036.      enum file_change_code file_change;
  9037. {
  9038.   int len;
  9039.   char *line_cmd_buf, *line_end;
  9040.  
  9041.   if (no_line_commands
  9042.       || ip->fname == NULL
  9043. #ifdef NEXT_CPP_SERVER
  9044.       || no_output
  9045.       /* allow all "notes" to main outbuf to pass through */
  9046.       || (ip->file && ip->file->header_output 
  9047.           && ((op == &sys_header_outbuf) || (op == &user_header_outbuf)))) { 
  9048. #else
  9049.       || no_output) {
  9050. #endif
  9051.     op->lineno = ip->lineno;
  9052.     return;
  9053.   }
  9054.  
  9055.   if (conditional) {
  9056.     if (ip->lineno == op->lineno)
  9057.       return;
  9058.  
  9059.     /* If the inherited line number is a little too small,
  9060.        output some newlines instead of a #line command.  */
  9061.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  9062.       check_expand (op, 10);
  9063.       while (ip->lineno > op->lineno) {
  9064.     *op->bufp++ = '\n';
  9065.     op->lineno++;
  9066.       }
  9067.       return;
  9068.     }
  9069.   }
  9070.  
  9071.   /* Don't output a line number of 0 if we can help it.  */
  9072.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  9073.       && *ip->bufp == '\n') {
  9074.     ip->lineno++;
  9075.     ip->bufp++;
  9076.   }
  9077.  
  9078.   line_cmd_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
  9079. #ifdef OUTPUT_LINE_COMMANDS
  9080.   sprintf (line_cmd_buf, "#line %d ", ip->lineno);
  9081. #else
  9082.   sprintf (line_cmd_buf, "# %d ", ip->lineno);
  9083. #endif
  9084.   line_end = quote_string (line_cmd_buf + strlen (line_cmd_buf),
  9085.                ip->nominal_fname);
  9086.  
  9087.   if (file_change != same_file) {
  9088.     *line_end++ = ' ';
  9089.     if (file_change == enter_file) *line_end++ = '1';
  9090.     else if (file_change == leave_file) *line_end++ = '2';
  9091.     else if (file_change == enter_expansion) *line_end++ = '5';
  9092.     else if (file_change == leave_expansion) *line_end++ = '6';
  9093.   }
  9094.  
  9095.   if (file_change == enter_expansion) { 
  9096.     /* note the position of macro reference */
  9097.     sprintf (line_end, " %d", (ip->bufp - strlen(ip->nominal_fname))
  9098.                               - ip->beg_of_line);
  9099.     line_end = &line_cmd_buf[strlen(line_cmd_buf)];
  9100.   }
  9101.   else if (file_change == leave_expansion) {
  9102.     /* note the original character position in the input stream */
  9103.     sprintf (line_end, " %d", ip->bufp - ip->beg_of_line);
  9104.     line_end = &line_cmd_buf[strlen(line_cmd_buf)];
  9105.   }
  9106.   /* Tell cc1 if following text comes from a system header file.  */
  9107.   else if (ip->system_header_p) {
  9108.     *line_end++ = ' ';
  9109.     *line_end++ = '3';
  9110.   }
  9111. #ifndef NO_IMPLICIT_EXTERN_C
  9112.   /* Tell cc1plus if following text should be treated as C.  */
  9113.   if (ip->system_header_p == 2 && cplusplus) {
  9114.     *line_end++ = ' ';
  9115.     *line_end++ = '4';
  9116.   }
  9117. #endif
  9118. #ifdef NEXT_CPP_SERVER
  9119.    if (serverized) {
  9120.       int record_stat_info = 0;
  9121.       if (file_change == enter_file) {
  9122.          /* header in need of being recorded */
  9123.          record_stat_info = 1;
  9124.       } else if ((file_change == same_file)
  9125.                  && (op == &outbuf)
  9126.                  && !outbuf_stat_info_recorded) {
  9127.          record_stat_info = 1;
  9128.          outbuf_stat_info_recorded = 1;
  9129.       }
  9130.       if (record_stat_info) {
  9131.          int hashval = import_hash (ip->fname);
  9132.          struct import_file *import = import_hash_table[hashval];
  9133.          /* Is the following loop slowing things down?  Needs investigation. */
  9134.          if (import) {
  9135.             while (import) {
  9136.                if (!strcmp (ip->fname, import->name->fname)) {
  9137.                   char tmp[64];
  9138.                   int len;
  9139.                   sprintf(tmp, " modtime=%d size=%d inode=%d dev=%d",
  9140.                           import->modtime, ip->length,
  9141.                           import->inode, import->dev);
  9142.                   len = strlen(tmp);
  9143.                   strncpy(line_end, tmp, len);
  9144.                   line_end += len;
  9145.                   break;
  9146.                }
  9147.                import = import->next;
  9148.             }
  9149.          } 
  9150.       }
  9151.   }
  9152. #endif
  9153.   *line_end++ = '\n';
  9154.   len = line_end - line_cmd_buf;
  9155.   check_expand (op, len + 1);
  9156.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  9157.     *op->bufp++ = '\n';
  9158.   bcopy ((char *) line_cmd_buf, (char *) op->bufp, len);
  9159.   op->bufp += len;
  9160.   op->lineno = ip->lineno;
  9161. }
  9162.  
  9163. /* This structure represents one parsed argument in a macro call.
  9164.    `raw' points to the argument text as written (`raw_length' is its length).
  9165.    `expanded' points to the argument's macro-expansion
  9166.    (its length is `expand_length').
  9167.    `stringified_length' is the length the argument would have
  9168.    if stringified.
  9169.    `use_count' is the number of times this macro arg is substituted
  9170.    into the macro.  If the actual use count exceeds 10, 
  9171.    the value stored is 10.
  9172.    `free1' and `free2', if nonzero, point to blocks to be freed
  9173.    when the macro argument data is no longer needed.  */
  9174.  
  9175. struct argdata {
  9176.   U_CHAR *raw, *expanded;
  9177.   int raw_length, expand_length;
  9178.   int stringified_length;
  9179.   U_CHAR *free1, *free2;
  9180.   char newlines;
  9181.   char comments;
  9182.   char use_count;
  9183. };
  9184.  
  9185. /* Expand a macro call.
  9186.    HP points to the symbol that is the macro being called.
  9187.    Put the result of expansion onto the input stack
  9188.    so that subsequent input by our caller will use it.
  9189.  
  9190.    If macro wants arguments, caller has already verified that
  9191.    an argument list follows; arguments come from the input stack.  */
  9192.  
  9193. static void
  9194. macroexpand (hp, op)
  9195.      HASHNODE *hp;
  9196.      FILE_BUF *op;
  9197. {
  9198.   int nargs;
  9199.   DEFINITION *defn = hp->value.defn;
  9200.   register U_CHAR *xbuf;
  9201.   int xbuf_len;
  9202.   int start_line = instack[indepth].lineno;
  9203.   int rest_args, rest_zero;
  9204.  
  9205.   CHECK_DEPTH (return;);
  9206.  
  9207.   /* it might not actually be a macro.  */
  9208.   if (hp->type != T_MACRO) {
  9209.     special_symbol (hp, op);
  9210.     return;
  9211.   }
  9212.  
  9213.   /* This macro is being used inside a #if, which means it must be */
  9214.   /* recorded as a precondition.  */
  9215.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  9216.     dump_single_macro (hp, pcp_outfile);
  9217.   
  9218.   nargs = defn->nargs;
  9219.  
  9220.   if (nargs >= 0) {
  9221.     register int i;
  9222.     struct argdata *args;
  9223.     char *parse_error = 0;
  9224.  
  9225.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  9226.  
  9227.     for (i = 0; i < nargs; i++) {
  9228.       args[i].raw = (U_CHAR *) "";
  9229.       args[i].expanded = 0;
  9230.       args[i].raw_length = args[i].expand_length
  9231.     = args[i].stringified_length = 0;
  9232.       args[i].free1 = args[i].free2 = 0;
  9233.       args[i].use_count = 0;
  9234.     }
  9235.  
  9236.     /* Parse all the macro args that are supplied.  I counts them.
  9237.        The first NARGS args are stored in ARGS.
  9238.        The rest are discarded.
  9239.        If rest_args is set then we assume macarg absorbed the rest of the args.
  9240.        */
  9241.     i = 0;
  9242.     rest_args = 0;
  9243.     do {
  9244.       /* Discard the open-parenthesis or comma before the next arg.  */
  9245.       ++instack[indepth].bufp;
  9246.       if (rest_args)
  9247.     continue;
  9248.       if (i < nargs || (nargs == 0 && i == 0)) {
  9249.     /* if we are working on last arg which absorbs rest of args... */
  9250.     if (i == nargs - 1 && defn->rest_args)
  9251.       rest_args = 1;
  9252.     parse_error = macarg (&args[i], rest_args);
  9253.       }
  9254.       else
  9255.     parse_error = macarg (NULL_PTR, 0);
  9256.       if (parse_error) {
  9257.     error_with_line (line_for_error (start_line), parse_error);
  9258.     break;
  9259.       }
  9260.       i++;
  9261.     } while (*instack[indepth].bufp != ')');
  9262.  
  9263.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  9264.     if (i == 1) {
  9265.       register U_CHAR *bp = args[0].raw;
  9266.       register U_CHAR *lim = bp + args[0].raw_length;
  9267.       /* cpp.texi says for foo ( ) we provide one argument.
  9268.      However, if foo wants just 0 arguments, treat this as 0.  */
  9269.       if (nargs == 0)
  9270.     while (bp != lim && is_space[*bp]) bp++;
  9271.       if (bp == lim)
  9272.     i = 0;
  9273.     }
  9274.  
  9275.     /* Don't output an error message if we have already output one for
  9276.        a parse error above.  */
  9277.     rest_zero = 0;
  9278.     if (nargs == 0 && i > 0) {
  9279.       if (! parse_error)
  9280.     error ("arguments given to macro `%s'", hp->name);
  9281.     } else if (i < nargs) {
  9282.       /* traditional C allows foo() if foo wants one argument.  */
  9283.       if (nargs == 1 && i == 0 && traditional)
  9284.     ;
  9285.       /* the rest args token is allowed to absorb 0 tokens */
  9286.       else if (i == nargs - 1 && defn->rest_args)
  9287.     rest_zero = 1;
  9288.       else if (parse_error)
  9289.     ;
  9290.       else if (i == 0)
  9291.     error ("macro `%s' used without args", hp->name);
  9292.       else if (i == 1)
  9293.     error ("macro `%s' used with just one arg", hp->name);
  9294.       else
  9295.     error ("macro `%s' used with only %d args", hp->name, i);
  9296.     } else if (i > nargs) {
  9297.       if (! parse_error)
  9298.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  9299.     }
  9300.  
  9301.     /* Swallow the closeparen.  */
  9302.     ++instack[indepth].bufp;
  9303.  
  9304.     /* If macro wants zero args, we parsed the arglist for checking only.
  9305.        Read directly from the macro definition.  */
  9306.     if (nargs == 0) {
  9307.       xbuf = defn->expansion;
  9308.       xbuf_len = defn->length;
  9309.     } else {
  9310.       register U_CHAR *exp = defn->expansion;
  9311.       register int offset;    /* offset in expansion,
  9312.                    copied a piece at a time */
  9313.       register int totlen;    /* total amount of exp buffer filled so far */
  9314.  
  9315.       register struct reflist *ap, *last_ap;
  9316.  
  9317.       /* Macro really takes args.  Compute the expansion of this call.  */
  9318.  
  9319.       /* Compute length in characters of the macro's expansion.
  9320.      Also count number of times each arg is used.  */
  9321.       xbuf_len = defn->length;
  9322.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  9323.     if (ap->stringify)
  9324.       xbuf_len += args[ap->argno].stringified_length;
  9325.     else if (ap->raw_before || ap->raw_after || traditional)
  9326.       /* Add 4 for two newline-space markers to prevent
  9327.          token concatenation.  */
  9328.       xbuf_len += args[ap->argno].raw_length + 4;
  9329.     else {
  9330.       /* We have an ordinary (expanded) occurrence of the arg.
  9331.          So compute its expansion, if we have not already.  */
  9332.       if (args[ap->argno].expanded == 0) {
  9333.         FILE_BUF obuf;
  9334.         obuf = expand_to_temp_buffer (args[ap->argno].raw,
  9335.                       args[ap->argno].raw + args[ap->argno].raw_length,
  9336.                       1, 0);
  9337.  
  9338.         args[ap->argno].expanded = obuf.buf;
  9339.         args[ap->argno].expand_length = obuf.length;
  9340.         args[ap->argno].free2 = obuf.buf;
  9341.       }
  9342.  
  9343.       /* Add 4 for two newline-space markers to prevent
  9344.          token concatenation.  */
  9345.       xbuf_len += args[ap->argno].expand_length + 4;
  9346.     }
  9347.     if (args[ap->argno].use_count < 10)
  9348.       args[ap->argno].use_count++;
  9349.       }
  9350.  
  9351.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  9352.  
  9353.       /* Generate in XBUF the complete expansion
  9354.      with arguments substituted in.
  9355.      TOTLEN is the total size generated so far.
  9356.      OFFSET is the index in the definition
  9357.      of where we are copying from.  */
  9358.       offset = totlen = 0;
  9359.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  9360.        last_ap = ap, ap = ap->next) {
  9361.     register struct argdata *arg = &args[ap->argno];
  9362.     int count_before = totlen;
  9363.  
  9364.     /* Add chars to XBUF.  */
  9365.     for (i = 0; i < ap->nchars; i++, offset++)
  9366.       xbuf[totlen++] = exp[offset];
  9367.  
  9368.     /* If followed by an empty rest arg with concatenation,
  9369.        delete the last run of nonwhite chars.  */
  9370.     if (rest_zero && totlen > count_before
  9371.         && ((ap->rest_args && ap->raw_before)
  9372.         || (last_ap != NULL && last_ap->rest_args
  9373.             && last_ap->raw_after))) {
  9374.       /* Delete final whitespace.  */
  9375.       while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
  9376.         totlen--;
  9377.       }
  9378.  
  9379.       /* Delete the nonwhites before them.  */
  9380.       while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
  9381.         totlen--;
  9382.       }
  9383.     }
  9384.  
  9385.     if (ap->stringify != 0) {
  9386.       int arglen = arg->raw_length;
  9387.       int escaped = 0;
  9388.       int in_string = 0;
  9389.       int c;
  9390.       i = 0;
  9391.       while (i < arglen
  9392.          && (c = arg->raw[i], is_space[c]))
  9393.         i++;
  9394.       while (i < arglen
  9395.          && (c = arg->raw[arglen - 1], is_space[c]))
  9396.         arglen--;
  9397.       if (!traditional)
  9398.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  9399.       for (; i < arglen; i++) {
  9400.         c = arg->raw[i];
  9401.  
  9402.         /* Special markers Newline Space
  9403.            generate nothing for a stringified argument.  */
  9404.         if (c == '\n' && arg->raw[i+1] != '\n') {
  9405.           i++;
  9406.           continue;
  9407.         }
  9408.  
  9409.         /* Internal sequences of whitespace are replaced by one space
  9410.            except within an string or char token.  */
  9411.         if (! in_string
  9412.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  9413.           while (1) {
  9414.         /* Note that Newline Space does occur within whitespace
  9415.            sequences; consider it part of the sequence.  */
  9416.         if (c == '\n' && is_space[arg->raw[i+1]])
  9417.           i += 2;
  9418.         else if (c != '\n' && is_space[c])
  9419.           i++;
  9420.         else break;
  9421.         c = arg->raw[i];
  9422.           }
  9423.           i--;
  9424.           c = ' ';
  9425.         }
  9426.  
  9427.         if (escaped)
  9428.           escaped = 0;
  9429.         else {
  9430.           if (c == '\\')
  9431.         escaped = 1;
  9432.           if (in_string) {
  9433.         if (c == in_string)
  9434.           in_string = 0;
  9435.           } else if (c == '\"' || c == '\'')
  9436.         in_string = c;
  9437.         }
  9438.  
  9439.         /* Escape these chars */
  9440.         if (c == '\"' || (in_string && c == '\\'))
  9441.           xbuf[totlen++] = '\\';
  9442.         if (isprint (c))
  9443.           xbuf[totlen++] = c;
  9444.         else {
  9445.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  9446.           totlen += 4;
  9447.         }
  9448.       }
  9449.       if (!traditional)
  9450.         xbuf[totlen++] = '\"'; /* insert ending quote */
  9451.     } else if (ap->raw_before || ap->raw_after || traditional) {
  9452.       U_CHAR *p1 = arg->raw;
  9453.       U_CHAR *l1 = p1 + arg->raw_length;
  9454.       if (ap->raw_before) {
  9455.         while (p1 != l1 && is_space[*p1]) p1++;
  9456.         while (p1 != l1 && is_idchar[*p1])
  9457.           xbuf[totlen++] = *p1++;
  9458.         /* Delete any no-reexpansion marker that follows
  9459.            an identifier at the beginning of the argument
  9460.            if the argument is concatenated with what precedes it.  */
  9461.         if (p1[0] == '\n' && p1[1] == '-')
  9462.           p1 += 2;
  9463.       } else if (!traditional) {
  9464.       /* Ordinary expanded use of the argument.
  9465.          Put in newline-space markers to prevent token pasting.  */
  9466.         xbuf[totlen++] = '\n';
  9467.         xbuf[totlen++] = ' ';
  9468.       }
  9469.       if (ap->raw_after) {
  9470.         /* Arg is concatenated after: delete trailing whitespace,
  9471.            whitespace markers, and no-reexpansion markers.  */
  9472.         while (p1 != l1) {
  9473.           if (is_space[l1[-1]]) l1--;
  9474.           else if (l1[-1] == '-') {
  9475.         U_CHAR *p2 = l1 - 1;
  9476.         /* If a `-' is preceded by an odd number of newlines then it
  9477.            and the last newline are a no-reexpansion marker.  */
  9478.         while (p2 != p1 && p2[-1] == '\n') p2--;
  9479.         if ((l1 - 1 - p2) & 1) {
  9480.           l1 -= 2;
  9481.         }
  9482.         else break;
  9483.           }
  9484.           else break;
  9485.         }
  9486.       }
  9487.  
  9488.       bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
  9489.       totlen += l1 - p1;
  9490.       if (!traditional && !ap->raw_after) {
  9491.         /* Ordinary expanded use of the argument.
  9492.            Put in newline-space markers to prevent token pasting.  */
  9493.         xbuf[totlen++] = '\n';
  9494.         xbuf[totlen++] = ' ';
  9495.       }
  9496.     } else {
  9497.       /* Ordinary expanded use of the argument.
  9498.          Put in newline-space markers to prevent token pasting.  */
  9499.       if (!traditional) {
  9500.         xbuf[totlen++] = '\n';
  9501.         xbuf[totlen++] = ' ';
  9502.       }
  9503.       bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
  9504.          arg->expand_length);
  9505.       totlen += arg->expand_length;
  9506.       if (!traditional) {
  9507.         xbuf[totlen++] = '\n';
  9508.         xbuf[totlen++] = ' ';
  9509.       }
  9510.       /* If a macro argument with newlines is used multiple times,
  9511.          then only expand the newlines once.  This avoids creating output
  9512.          lines which don't correspond to any input line, which confuses
  9513.          gdb and gcov.  */
  9514.       if (arg->use_count > 1 && arg->newlines > 0) {
  9515.         /* Don't bother doing change_newlines for subsequent
  9516.            uses of arg.  */
  9517.         arg->use_count = 1;
  9518.         arg->expand_length
  9519.           = change_newlines (arg->expanded, arg->expand_length);
  9520.       }
  9521.     }
  9522.  
  9523.     if (totlen > xbuf_len)
  9524.       abort ();
  9525.       }
  9526.  
  9527.       /* if there is anything left of the definition
  9528.      after handling the arg list, copy that in too. */
  9529.  
  9530.       for (i = offset; i < defn->length; i++) {
  9531.     /* if we've reached the end of the macro */
  9532.     if (exp[i] == ')')
  9533.       rest_zero = 0;
  9534.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  9535.            && last_ap->raw_after))
  9536.       xbuf[totlen++] = exp[i];
  9537.       }
  9538.  
  9539.       xbuf[totlen] = 0;
  9540.       xbuf_len = totlen;
  9541.  
  9542.       for (i = 0; i < nargs; i++) {
  9543.     if (args[i].free1 != 0)
  9544.       free (args[i].free1);
  9545.     if (args[i].free2 != 0)
  9546.       free (args[i].free2);
  9547.       }
  9548.     }
  9549.   } else {
  9550.     xbuf = defn->expansion;
  9551.     xbuf_len = defn->length;
  9552.   }
  9553.  
  9554.   /* Now put the expansion on the input stack
  9555.      so our caller will commence reading from it.  */
  9556.   {
  9557.     register FILE_BUF *ip2;
  9558.  
  9559.     ip2 = &instack[++indepth];
  9560.  
  9561.     ip2->fname = 0;
  9562.     ip2->nominal_fname = 0;
  9563.     /* This may not be exactly correct, but will give much better error
  9564.        messages for nested macro calls than using a line number of zero.  */
  9565.     ip2->lineno = start_line;
  9566.     ip2->buf = xbuf;
  9567.     ip2->length = xbuf_len;
  9568.     ip2->bufp = xbuf;
  9569.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  9570.     ip2->macro = hp;
  9571.     ip2->if_stack = if_stack;
  9572.     ip2->system_header_p = 0;
  9573.  
  9574.     /* Recursive macro use sometimes works traditionally.
  9575.        #define foo(x,y) bar (x (y,0), y)
  9576.        foo (foo, baz)  */
  9577.  
  9578.     if (!traditional)
  9579.       hp->type = T_DISABLED;
  9580.   }
  9581. }
  9582.  
  9583. /*
  9584.  * Parse a macro argument and store the info on it into *ARGPTR.
  9585.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  9586.  * Return nonzero to indicate a syntax error.
  9587.  */
  9588.  
  9589. static char *
  9590. macarg (argptr, rest_args)
  9591.      register struct argdata *argptr;
  9592.      int rest_args;
  9593. {
  9594.   FILE_BUF *ip = &instack[indepth];
  9595.   int paren = 0;
  9596.   int newlines = 0;
  9597.   int comments = 0;
  9598.  
  9599.   /* Try to parse as much of the argument as exists at this
  9600.      input stack level.  */
  9601.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  9602.             &paren, &newlines, &comments, rest_args);
  9603.  
  9604.   /* If we find the end of the argument at this level,
  9605.      set up *ARGPTR to point at it in the input stack.  */
  9606.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  9607.       && bp != ip->buf + ip->length) {
  9608.     if (argptr != 0) {
  9609.       argptr->raw = ip->bufp;
  9610.       argptr->raw_length = bp - ip->bufp;
  9611.       argptr->newlines = newlines;
  9612.     }
  9613.     ip->bufp = bp;
  9614.   } else {
  9615.     /* This input stack level ends before the macro argument does.
  9616.        We must pop levels and keep parsing.
  9617.        Therefore, we must allocate a temporary buffer and copy
  9618.        the macro argument into it.  */
  9619.     int bufsize = bp - ip->bufp;
  9620.     int extra = newlines;
  9621.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  9622.     int final_start = 0;
  9623.  
  9624.     bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
  9625.     ip->bufp = bp;
  9626.     ip->lineno += newlines;
  9627.  
  9628.     while (bp == ip->buf + ip->length) {
  9629.       if (instack[indepth].macro == 0) {
  9630.     free (buffer);
  9631.     return "unterminated macro call";
  9632.       }
  9633.       ip->macro->type = T_MACRO;
  9634.       if (ip->free_ptr)
  9635.     free (ip->free_ptr);
  9636.       ip = &instack[--indepth];
  9637.       newlines = 0;
  9638.       comments = 0;
  9639.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  9640.             &newlines, &comments, rest_args);
  9641.       final_start = bufsize;
  9642.       bufsize += bp - ip->bufp;
  9643.       extra += newlines;
  9644.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  9645.       bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
  9646.          bp - ip->bufp);
  9647.       ip->bufp = bp;
  9648.       ip->lineno += newlines;
  9649.     }
  9650.  
  9651.     /* Now, if arg is actually wanted, record its raw form,
  9652.        discarding comments and duplicating newlines in whatever
  9653.        part of it did not come from a macro expansion.
  9654.        EXTRA space has been preallocated for duplicating the newlines.
  9655.        FINAL_START is the index of the start of that part.  */
  9656.     if (argptr != 0) {
  9657.       argptr->raw = buffer;
  9658.       argptr->raw_length = bufsize;
  9659.       argptr->free1 = buffer;
  9660.       argptr->newlines = newlines;
  9661.       argptr->comments = comments;
  9662.       if ((newlines || comments) && ip->fname != 0)
  9663.     argptr->raw_length
  9664.       = final_start +
  9665.         discard_comments (argptr->raw + final_start,
  9666.                   argptr->raw_length - final_start,
  9667.                   newlines);
  9668.       argptr->raw[argptr->raw_length] = 0;
  9669.       if (argptr->raw_length > bufsize + extra)
  9670.     abort ();
  9671.     }
  9672.   }
  9673.  
  9674.   /* If we are not discarding this argument,
  9675.      macroexpand it and compute its length as stringified.
  9676.      All this info goes into *ARGPTR.  */
  9677.  
  9678.   if (argptr != 0) {
  9679.     register U_CHAR *buf, *lim;
  9680.     register int totlen;
  9681.  
  9682.     buf = argptr->raw;
  9683.     lim = buf + argptr->raw_length;
  9684.  
  9685.     while (buf != lim && is_space[*buf])
  9686.       buf++;
  9687.     while (buf != lim && is_space[lim[-1]])
  9688.       lim--;
  9689.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  9690.     while (buf != lim) {
  9691.       register U_CHAR c = *buf++;
  9692.       totlen++;
  9693.       /* Internal sequences of whitespace are replaced by one space
  9694.      in most cases, but not always.  So count all the whitespace
  9695.      in case we need to keep it all.  */
  9696. #if 0
  9697.       if (is_space[c])
  9698.     SKIP_ALL_WHITE_SPACE (buf);
  9699.       else
  9700. #endif
  9701.       if (c == '\"' || c == '\\') /* escape these chars */
  9702.     totlen++;
  9703.       else if (!isprint (c))
  9704.     totlen += 3;
  9705.     }
  9706.     argptr->stringified_length = totlen;
  9707.   }
  9708.   return 0;
  9709. }
  9710.  
  9711. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  9712.    counting parens in *DEPTHPTR,
  9713.    and return if reach LIMIT
  9714.    or before a `)' that would make *DEPTHPTR negative
  9715.    or before a comma when *DEPTHPTR is zero.
  9716.    Single and double quotes are matched and termination
  9717.    is inhibited within them.  Comments also inhibit it.
  9718.    Value returned is pointer to stopping place.
  9719.  
  9720.    Increment *NEWLINES each time a newline is passed.
  9721.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  9722.    Set *COMMENTS to 1 if a comment is seen.  */
  9723.  
  9724. static U_CHAR *
  9725. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  9726.      U_CHAR *start;
  9727.      register U_CHAR *limit;
  9728.      int *depthptr, *newlines, *comments;
  9729.      int rest_args;
  9730. {
  9731.   register U_CHAR *bp = start;
  9732.  
  9733.   while (bp < limit) {
  9734.     switch (*bp) {
  9735.     case '(':
  9736.       (*depthptr)++;
  9737.       break;
  9738.     case ')':
  9739.       if (--(*depthptr) < 0)
  9740.     return bp;
  9741.       break;
  9742.     case '\\':
  9743.       /* Traditionally, backslash makes following char not special.  */
  9744.       if (bp + 1 < limit && traditional)
  9745.     {
  9746.       bp++;
  9747.       /* But count source lines anyway.  */
  9748.       if (*bp == '\n')
  9749.         ++*newlines;
  9750.     }
  9751.       break;
  9752.     case '\n':
  9753.       ++*newlines;
  9754.       break;
  9755.     case '/':
  9756.       if (bp[1] == '\\' && bp[2] == '\n')
  9757.     newline_fix (bp + 1);
  9758.       if (cplusplus_comments && bp[1] == '/') {
  9759.     *comments = 1;
  9760.     bp += 2;
  9761.     while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
  9762.       if (*bp == '\n') ++*newlines;
  9763.       bp++;
  9764.     }
  9765.     break;
  9766.       }
  9767.       if (bp[1] != '*' || bp + 1 >= limit)
  9768.     break;
  9769.       *comments = 1;
  9770.       bp += 2;
  9771.       while (bp + 1 < limit) {
  9772.     if (bp[0] == '*'
  9773.         && bp[1] == '\\' && bp[2] == '\n')
  9774.       newline_fix (bp + 1);
  9775.     if (bp[0] == '*' && bp[1] == '/')
  9776.       break;
  9777.     if (*bp == '\n') ++*newlines;
  9778.     bp++;
  9779.       }
  9780.       break;
  9781.     case '\'':
  9782.     case '\"':
  9783.       {
  9784.     int quotec;
  9785.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  9786.       if (*bp == '\\') {
  9787.         bp++;
  9788.         if (*bp == '\n')
  9789.           ++*newlines;
  9790.         while (*bp == '\\' && bp[1] == '\n') {
  9791.           bp += 2;
  9792.         }
  9793.       } else if (*bp == '\n') {
  9794.         ++*newlines;
  9795.         if (quotec == '\'')
  9796.           break;
  9797.       }
  9798.     }
  9799.       }
  9800.       break;
  9801.     case ',':
  9802.       /* if we've returned to lowest level and we aren't absorbing all args */
  9803.       if ((*depthptr) == 0 && rest_args == 0)
  9804.     return bp;
  9805.       break;
  9806.     }
  9807.     bp++;
  9808.   }
  9809.  
  9810.   return bp;
  9811. }
  9812.  
  9813. /* Discard comments and duplicate newlines
  9814.    in the string of length LENGTH at START,
  9815.    except inside of string constants.
  9816.    The string is copied into itself with its beginning staying fixed.  
  9817.  
  9818.    NEWLINES is the number of newlines that must be duplicated.
  9819.    We assume that that much extra space is available past the end
  9820.    of the string.  */
  9821.  
  9822. static int
  9823. discard_comments (start, length, newlines)
  9824.      U_CHAR *start;
  9825.      int length;
  9826.      int newlines;
  9827. {
  9828.   register U_CHAR *ibp;
  9829.   register U_CHAR *obp;
  9830.   register U_CHAR *limit;
  9831.   register int c;
  9832.  
  9833.   /* If we have newlines to duplicate, copy everything
  9834.      that many characters up.  Then, in the second part,
  9835.      we will have room to insert the newlines
  9836.      while copying down.
  9837.      NEWLINES may actually be too large, because it counts
  9838.      newlines in string constants, and we don't duplicate those.
  9839.      But that does no harm.  */
  9840.   if (newlines > 0) {
  9841.     ibp = start + length;
  9842.     obp = ibp + newlines;
  9843.     limit = start;
  9844.     while (limit != ibp)
  9845.       *--obp = *--ibp;
  9846.   }
  9847.  
  9848.   ibp = start + newlines;
  9849.   limit = start + length + newlines;
  9850.   obp = start;
  9851.  
  9852.   while (ibp < limit) {
  9853.     *obp++ = c = *ibp++;
  9854.     switch (c) {
  9855.     case '\n':
  9856.       /* Duplicate the newline.  */
  9857.       *obp++ = '\n';
  9858.       break;
  9859.  
  9860.     case '\\':
  9861.       if (*ibp == '\n') {
  9862.     obp--;
  9863.     ibp++;
  9864.       }
  9865.       break;
  9866.  
  9867.     case '/':
  9868.       if (*ibp == '\\' && ibp[1] == '\n')
  9869.     newline_fix (ibp);
  9870.       /* Delete any comment.  */
  9871.       if (cplusplus_comments && ibp[0] == '/') {
  9872.     /* Comments are equivalent to spaces.  */
  9873.     obp[-1] = ' ';
  9874.     ibp++;
  9875.     while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
  9876.       ibp++;
  9877.     break;
  9878.       }
  9879.       if (ibp[0] != '*' || ibp + 1 >= limit)
  9880.     break;
  9881.       /* Comments are equivalent to spaces.  */
  9882.       obp[-1] = ' ';
  9883.       ibp++;
  9884.       while (ibp + 1 < limit) {
  9885.     if (ibp[0] == '*'
  9886.         && ibp[1] == '\\' && ibp[2] == '\n')
  9887.       newline_fix (ibp + 1);
  9888.     if (ibp[0] == '*' && ibp[1] == '/')
  9889.       break;
  9890.     ibp++;
  9891.       }
  9892.       ibp += 2;
  9893.       break;
  9894.  
  9895.     case '\'':
  9896.     case '\"':
  9897.       /* Notice and skip strings, so that we don't
  9898.      think that comments start inside them,
  9899.      and so we don't duplicate newlines in them.  */
  9900.       {
  9901.     int quotec = c;
  9902.     while (ibp < limit) {
  9903.       *obp++ = c = *ibp++;
  9904.       if (c == quotec)
  9905.         break;
  9906.       if (c == '\n' && quotec == '\'')
  9907.         break;
  9908.       if (c == '\\' && ibp < limit) {
  9909.         while (*ibp == '\\' && ibp[1] == '\n')
  9910.           ibp += 2;
  9911.         *obp++ = *ibp++;
  9912.       }
  9913.     }
  9914.       }
  9915.       break;
  9916.     }
  9917.   }
  9918.  
  9919.   return obp - start;
  9920. }
  9921.  
  9922. /* Turn newlines to spaces in the string of length LENGTH at START,
  9923.    except inside of string constants.
  9924.    The string is copied into itself with its beginning staying fixed.  */
  9925.  
  9926. static int
  9927. change_newlines (start, length)
  9928.      U_CHAR *start;
  9929.      int length;
  9930. {
  9931.   register U_CHAR *ibp;
  9932.   register U_CHAR *obp;
  9933.   register U_CHAR *limit;
  9934.   register int c;
  9935.  
  9936.   ibp = start;
  9937.   limit = start + length;
  9938.   obp = start;
  9939.  
  9940.   while (ibp < limit) {
  9941.     *obp++ = c = *ibp++;
  9942.     switch (c) {
  9943.     case '\n':
  9944.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  9945.      string.  Skip past the newline and its duplicate.
  9946.      Put a space in the output.  */
  9947.       if (*ibp == '\n')
  9948.     {
  9949.       ibp++;
  9950.       obp--;
  9951.       *obp++ = ' ';
  9952.     }
  9953.       break;
  9954.  
  9955.     case '\'':
  9956.     case '\"':
  9957.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  9958.       {
  9959.     int quotec = c;
  9960.     while (ibp < limit) {
  9961.       *obp++ = c = *ibp++;
  9962.       if (c == quotec)
  9963.         break;
  9964.       if (c == '\n' && quotec == '\'')
  9965.         break;
  9966.     }
  9967.       }
  9968.       break;
  9969.     }
  9970.   }
  9971.  
  9972.   return obp - start;
  9973. }
  9974.  
  9975. /*
  9976.  * error - print error message and increment count of errors.
  9977.  */
  9978.  
  9979. void
  9980. error (msg, arg1, arg2, arg3)
  9981.      char *msg;
  9982.      char *arg1, *arg2, *arg3;
  9983. {
  9984.   int i;
  9985.   FILE_BUF *ip = NULL;
  9986.  
  9987.   print_containing_files ();
  9988.  
  9989.   for (i = indepth; i >= 0; i--)
  9990.     if (instack[i].fname != NULL) {
  9991.       ip = &instack[i];
  9992.       break;
  9993.     }
  9994.  
  9995. #ifdef REPORT_EVENT
  9996.   REPORT_EVENT (0, NULL,
  9997.         ip ? ip->nominal_fname : NULL,
  9998.         ip ? ip->lineno : 0,
  9999.         msg, arg1, arg2, arg3);
  10000. #endif
  10001.  
  10002.   if (ip != NULL)
  10003.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  10004.   fprintf (stderr, msg, arg1, arg2, arg3);
  10005.   fprintf (stderr, "\n");
  10006.   errors++;
  10007. }
  10008.  
  10009. /* Error including a message from `errno'.  */
  10010.  
  10011. static void
  10012. error_from_errno (name)
  10013.      char *name;
  10014. {
  10015.   int i;
  10016.   FILE_BUF *ip = NULL;
  10017.  
  10018.   print_containing_files ();
  10019.  
  10020.   for (i = indepth; i >= 0; i--)
  10021.     if (instack[i].fname != NULL) {
  10022.       ip = &instack[i];
  10023.       break;
  10024.     }
  10025.  
  10026. #ifdef REPORT_EVENT
  10027.   REPORT_EVENT (0, NULL,
  10028.         ip ? ip->nominal_fname : NULL,
  10029.         ip ? ip->lineno : 0,
  10030.         "%s: %s", name,
  10031.         (errno < sys_nerr)
  10032.         ? sys_errlist[errno]
  10033.         : "undocumented I/O error",
  10034.         0);
  10035. #endif
  10036.  
  10037.   if (ip != NULL)
  10038.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  10039.  
  10040.   if (errno < sys_nerr)
  10041.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  10042.   else
  10043.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  10044.  
  10045.   errors++;
  10046. }
  10047.  
  10048. /* Print error message but don't count it.  */
  10049.  
  10050. void
  10051. warning (msg, arg1, arg2, arg3)
  10052.      char *msg;
  10053.      char *arg1, *arg2, *arg3;
  10054. {
  10055.   int i;
  10056.   FILE_BUF *ip = NULL;
  10057.  
  10058.   if (inhibit_warnings)
  10059.     return;
  10060.  
  10061.   if (warnings_are_errors)
  10062.     errors++;
  10063.  
  10064.   print_containing_files ();
  10065.  
  10066.   for (i = indepth; i >= 0; i--)
  10067.     if (instack[i].fname != NULL) {
  10068.       ip = &instack[i];
  10069.       break;
  10070.     }
  10071.  
  10072. #ifdef REPORT_EVENT
  10073.   REPORT_EVENT (1, NULL,
  10074.         ip ? ip->nominal_fname : NULL,
  10075.         ip ? ip->lineno : 0,
  10076.         msg, arg1, arg2, arg3);
  10077. #endif
  10078.  
  10079.   if (ip != NULL)
  10080.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  10081.   fprintf (stderr, "warning: ");
  10082.   fprintf (stderr, msg, arg1, arg2, arg3);
  10083.   fprintf (stderr, "\n");
  10084. }
  10085.  
  10086. static void
  10087. error_with_line (line, msg, arg1, arg2, arg3)
  10088.      int line;
  10089.      char *msg;
  10090.      char *arg1, *arg2, *arg3;
  10091. {
  10092.   int i;
  10093.   FILE_BUF *ip = NULL;
  10094.  
  10095.   print_containing_files ();
  10096.  
  10097.   for (i = indepth; i >= 0; i--)
  10098.     if (instack[i].fname != NULL) {
  10099.       ip = &instack[i];
  10100.       break;
  10101.     }
  10102.  
  10103. #ifdef REPORT_EVENT
  10104.   REPORT_EVENT (0, NULL,
  10105.         ip ? ip->nominal_fname : NULL,
  10106.         line, msg, arg1, arg2, arg3);
  10107. #endif
  10108.  
  10109.   if (ip != NULL)
  10110.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  10111.   fprintf (stderr, msg, arg1, arg2, arg3);
  10112.   fprintf (stderr, "\n");
  10113.   errors++;
  10114. }
  10115.  
  10116. static void
  10117. warning_with_line (line, msg, arg1, arg2, arg3)
  10118.      int line;
  10119.      char *msg;
  10120.      char *arg1, *arg2, *arg3;
  10121. {
  10122.   int i;
  10123.   FILE_BUF *ip = NULL;
  10124.  
  10125.   if (inhibit_warnings)
  10126.     return;
  10127.  
  10128.   if (warnings_are_errors)
  10129.     errors++;
  10130.  
  10131.   print_containing_files ();
  10132.  
  10133.   for (i = indepth; i >= 0; i--)
  10134.     if (instack[i].fname != NULL) {
  10135.       ip = &instack[i];
  10136.       break;
  10137.     }
  10138.  
  10139. #ifdef REPORT_EVENT
  10140.   REPORT_EVENT (0, NULL,
  10141.         ip ? ip->nominal_fname : NULL,
  10142.         line, msg, arg1, arg2, arg3);
  10143. #endif
  10144.  
  10145.   if (ip != NULL)
  10146.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  10147.   fprintf (stderr, "warning: ");
  10148.   fprintf (stderr, msg, arg1, arg2, arg3);
  10149.   fprintf (stderr, "\n");
  10150. }
  10151.  
  10152. /* print an error message and maybe count it.  */
  10153.  
  10154. void
  10155. pedwarn (msg, arg1, arg2, arg3)
  10156.      char *msg;
  10157.      char *arg1, *arg2, *arg3;
  10158. {
  10159.   if (pedantic_errors)
  10160.     error (msg, arg1, arg2, arg3);
  10161.   else
  10162.     warning (msg, arg1, arg2, arg3);
  10163. }
  10164.  
  10165. void
  10166. pedwarn_with_line (line, msg, arg1, arg2, arg3)
  10167.      int line;
  10168.      char *msg;
  10169.      char *arg1, *arg2, *arg3;
  10170. {
  10171.   if (pedantic_errors)
  10172.     error_with_line (line, msg, arg1, arg2, arg3);
  10173.   else
  10174.     warning_with_line (line, msg, arg1, arg2, arg3);
  10175. }
  10176.  
  10177. /* Report a warning (or an error if pedantic_errors)
  10178.    giving specified file name and line number, not current.  */
  10179.  
  10180. static void
  10181. pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
  10182.      char *file;
  10183.      int line;
  10184.      char *msg;
  10185.      char *arg1, *arg2, *arg3;
  10186. {
  10187. #ifdef REPORT_EVENT
  10188.   REPORT_EVENT (pedantic_errors ? 0 : 1,
  10189.         NULL, file, line, msg, arg1, arg2, arg3);
  10190. #endif
  10191.   if (!pedantic_errors && inhibit_warnings)
  10192.     return;
  10193.   if (file != NULL)
  10194.     fprintf (stderr, "%s:%d: ", file, line);
  10195.   if (pedantic_errors)
  10196.     errors++;
  10197.   if (!pedantic_errors)
  10198.     fprintf (stderr, "warning: ");
  10199.   fprintf (stderr, msg, arg1, arg2, arg3);
  10200.   fprintf (stderr, "\n");
  10201. }
  10202.  
  10203. /* Print the file names and line numbers of the #include
  10204.    commands which led to the current file.  */
  10205.  
  10206. static void
  10207. print_containing_files ()
  10208. {
  10209.   FILE_BUF *ip = NULL;
  10210.   int i;
  10211.   int first = 1;
  10212.  
  10213.   /* If stack of files hasn't changed since we last printed
  10214.      this info, don't repeat it.  */
  10215.   if (last_error_tick == input_file_stack_tick)
  10216.     return;
  10217.  
  10218.   for (i = indepth; i >= 0; i--)
  10219.     if (instack[i].fname != NULL) {
  10220.       ip = &instack[i];
  10221.       break;
  10222.     }
  10223.  
  10224.   /* Give up if we don't find a source file.  */
  10225.   if (ip == NULL)
  10226.     return;
  10227.  
  10228.   /* Find the other, outer source files.  */
  10229.   for (i--; i >= 0; i--)
  10230.     if (instack[i].fname != NULL) {
  10231.       ip = &instack[i];
  10232.       if (first) {
  10233.     first = 0;
  10234.     fprintf (stderr, "In file included");
  10235.       } else {
  10236.     fprintf (stderr, ",\n                ");
  10237.       }
  10238.  
  10239.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  10240.     }
  10241.   if (! first)
  10242.     fprintf (stderr, ":\n");
  10243.  
  10244.   /* Record we have printed the status as of this time.  */
  10245.   last_error_tick = input_file_stack_tick;
  10246. }
  10247.  
  10248. /* Return the line at which an error occurred.
  10249.    The error is not necessarily associated with the current spot
  10250.    in the input stack, so LINE says where.  LINE will have been
  10251.    copied from ip->lineno for the current input level.
  10252.    If the current level is for a file, we return LINE.
  10253.    But if the current level is not for a file, LINE is meaningless.
  10254.    In that case, we return the lineno of the innermost file.  */
  10255.  
  10256. static int
  10257. line_for_error (line)
  10258.      int line;
  10259. {
  10260.   int i;
  10261.   int line1 = line;
  10262.  
  10263.   for (i = indepth; i >= 0; ) {
  10264.     if (instack[i].fname != 0)
  10265.       return line1;
  10266.     i--;
  10267.     if (i < 0)
  10268.       return 0;
  10269.     line1 = instack[i].lineno;
  10270.   }
  10271.   abort ();
  10272.   /*NOTREACHED*/
  10273.   return 0;
  10274. }
  10275.  
  10276. /*
  10277.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  10278.  *
  10279.  * As things stand, nothing is ever placed in the output buffer to be
  10280.  * removed again except when it's KNOWN to be part of an identifier,
  10281.  * so flushing and moving down everything left, instead of expanding,
  10282.  * should work ok.
  10283.  */
  10284.  
  10285. /* You might think void was cleaner for the return type,
  10286.    but that would get type mismatch in check_expand in strict ANSI.  */
  10287. static int
  10288. grow_outbuf (obuf, needed)
  10289.      register FILE_BUF *obuf;
  10290.      register int needed;
  10291. {
  10292.   register U_CHAR *p;
  10293.   int minsize;
  10294.  
  10295.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  10296.     return 0;
  10297.  
  10298.   /* Make it at least twice as big as it is now.  */
  10299.   obuf->length *= 2;
  10300.   /* Make it have at least 150% of the free space we will need.  */
  10301.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  10302.   if (minsize > obuf->length)
  10303.     obuf->length = minsize;
  10304.  
  10305.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  10306.     memory_full ();
  10307.  
  10308.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  10309. #ifdef NEXT_CPP_SERVER
  10310.   obuf->output_written_to = p + (obuf->output_written_to - obuf->buf);
  10311. #endif
  10312.   obuf->buf = p;
  10313.  
  10314.   return 0;
  10315. }
  10316.  
  10317. /* Symbol table for macro names and special symbols */
  10318.  
  10319. /*
  10320.  * install a name in the main hash table, even if it is already there.
  10321.  *   name stops with first non alphanumeric, except leading '#'.
  10322.  * caller must check against redefinition if that is desired.
  10323.  * delete_macro () removes things installed by install () in fifo order.
  10324.  * this is important because of the `defined' special symbol used
  10325.  * in #if, and also if pushdef/popdef directives are ever implemented.
  10326.  *
  10327.  * If LEN is >= 0, it is the length of the name.
  10328.  * Otherwise, compute the length by scanning the entire name.
  10329.  *
  10330.  * If HASH is >= 0, it is the precomputed hash code.
  10331.  * Otherwise, compute the hash code.
  10332.  */
  10333. static HASHNODE *
  10334. install (name, len, type, ivalue, value, hash)
  10335.      U_CHAR *name;
  10336.      int len;
  10337.      enum node_type type;
  10338.      int ivalue;
  10339.      char *value;
  10340.      int hash;
  10341. {
  10342.   register HASHNODE *hp;
  10343.   register int i, bucket;
  10344.   register U_CHAR *p, *q;
  10345.  
  10346.   if (len < 0) {
  10347.     p = name;
  10348.     while (is_idchar[*p])
  10349.       p++;
  10350.     len = p - name;
  10351.   }
  10352.  
  10353.   if (hash < 0)
  10354.     hash = hashf (name, len, HASHSIZE);
  10355.  
  10356.   i = sizeof (HASHNODE) + len + 1;
  10357.   hp = (HASHNODE *) xmalloc (i);
  10358.   bucket = hash;
  10359.   hp->bucket_hdr = &hashtab[bucket];
  10360.   hp->next = hashtab[bucket];
  10361.   hashtab[bucket] = hp;
  10362.   hp->prev = NULL;
  10363.   if (hp->next != NULL)
  10364.     hp->next->prev = hp;
  10365.   hp->type = type;
  10366.   hp->length = len;
  10367.   if (hp->type == T_CONST)
  10368.     hp->value.ival = ivalue;
  10369.   else
  10370.     hp->value.cpval = value;
  10371.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  10372. #ifdef NEXT_CPP_SERVER
  10373.   hp->defined_within_module = 1;
  10374.   hp->owner = 0;
  10375. #endif
  10376.   p = hp->name;
  10377.   q = name;
  10378.   for (i = 0; i < len; i++)
  10379.     *p++ = *q++;
  10380.   hp->name[len] = 0;
  10381.   return hp;
  10382. }
  10383.  
  10384. /*
  10385.  * find the most recent hash node for name name (ending with first
  10386.  * non-identifier char) installed by install
  10387.  *
  10388.  * If LEN is >= 0, it is the length of the name.
  10389.  * Otherwise, compute the length by scanning the entire name.
  10390.  *
  10391.  * If HASH is >= 0, it is the precomputed hash code.
  10392.  * Otherwise, compute the hash code.
  10393.  */
  10394. HASHNODE *
  10395. lookup (name, len, hash)
  10396.      U_CHAR *name;
  10397.      int len;
  10398.      int hash;
  10399. {
  10400.   register U_CHAR *bp;
  10401.   register HASHNODE *bucket;
  10402.  
  10403.   if (len < 0) {
  10404.     for (bp = name; is_idchar[*bp]; bp++) ;
  10405.     len = bp - name;
  10406.   }
  10407. #ifdef NEXT_CPP_SERVER
  10408.   if (hash < 0) {
  10409.     hash = hashf (name, len, HASHSIZE);
  10410.  
  10411.     bucket = hashtab[hash];
  10412.     while (bucket) {
  10413.       if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  10414.         return bucket->defined_within_module ? bucket : NULL;
  10415.       bucket = bucket->next;
  10416.     }
  10417.   } else { /* snaroff: this mod should be improved */
  10418.     bucket = hashtab[hash];
  10419.     while (bucket) {
  10420.       if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  10421.         return bucket;
  10422.       bucket = bucket->next;
  10423.     }
  10424.   }
  10425. #else
  10426.   if (hash < 0)
  10427.     hash = hashf (name, len, HASHSIZE);
  10428.  
  10429.   bucket = hashtab[hash];
  10430.   while (bucket) {
  10431.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  10432.       return bucket;
  10433.     bucket = bucket->next;
  10434.   }
  10435. #endif
  10436.   return NULL;
  10437. }
  10438.  
  10439. /*
  10440.  * Delete a hash node.  Some weirdness to free junk from macros.
  10441.  * More such weirdness will have to be added if you define more hash
  10442.  * types that need it.
  10443.  */
  10444.  
  10445. /* Note that the DEFINITION of a macro is removed from the hash table
  10446.    but its storage is not freed.  This would be a storage leak
  10447.    except that it is not reasonable to keep undefining and redefining
  10448.    large numbers of macros many times.
  10449.    In any case, this is necessary, because a macro can be #undef'd
  10450.    in the middle of reading the arguments to a call to it.
  10451.    If #undef freed the DEFINITION, that would crash.  */
  10452.  
  10453. static void
  10454. delete_macro (hp)
  10455.      HASHNODE *hp;
  10456. {
  10457.  
  10458.   if (hp->prev != NULL)
  10459.     hp->prev->next = hp->next;
  10460.   if (hp->next != NULL)
  10461.     hp->next->prev = hp->prev;
  10462.  
  10463.   /* make sure that the bucket chain header that
  10464.      the deleted guy was on points to the right thing afterwards. */
  10465.   if (hp == *hp->bucket_hdr)
  10466.     *hp->bucket_hdr = hp->next;
  10467.  
  10468. #ifdef NEXT_CPP_SERVER
  10469.   /* a server cannot afford to leak data not in the shared header
  10470.      cache, so queue macros to be freed in clear_all_macros() */
  10471.   if (!hp->owner) {
  10472.      if (hp->type == T_MACRO) {
  10473.         hp->next = free_undefined_macros;
  10474.         free_undefined_macros = hp;
  10475.      } else {
  10476.         free (hp);
  10477.      }
  10478.   }
  10479. #else
  10480. #if 0
  10481.   if (hp->type == T_MACRO) {
  10482.     DEFINITION *d = hp->value.defn;
  10483.     struct reflist *ap, *nextap;
  10484.  
  10485.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  10486.       nextap = ap->next;
  10487.       free (ap);
  10488.     }
  10489.     free (d);
  10490.   }
  10491. #endif
  10492.   free (hp);
  10493. #endif
  10494. }
  10495.  
  10496. /*
  10497.  * return hash function on name.  must be compatible with the one
  10498.  * computed a step at a time, elsewhere
  10499.  */
  10500. static int
  10501. hashf (name, len, hashsize)
  10502.      register U_CHAR *name;
  10503.      register int len;
  10504.      int hashsize;
  10505. {
  10506.   register int r = 0;
  10507.  
  10508.   while (len--)
  10509.     r = HASHSTEP (r, *name++);
  10510.  
  10511.   return MAKE_POS (r) % hashsize;
  10512. }
  10513.  
  10514.  
  10515. /* Dump the definition of a single macro HP to OF.  */
  10516. static void
  10517. dump_single_macro (hp, of)
  10518.      register HASHNODE *hp;
  10519.      FILE *of;
  10520. {
  10521.   register DEFINITION *defn = hp->value.defn;
  10522.   struct reflist *ap;
  10523.   int offset;
  10524.   int concat;
  10525.  
  10526.  
  10527.   /* Print the definition of the macro HP.  */
  10528.  
  10529.   fprintf (of, "#define %s", hp->name);
  10530.  
  10531.   if (defn->nargs >= 0) {
  10532.     int i;
  10533.  
  10534.     fprintf (of, "(");
  10535.     for (i = 0; i < defn->nargs; i++) {
  10536.       dump_arg_n (defn, i, of);
  10537.       if (i + 1 < defn->nargs)
  10538.     fprintf (of, ", ");
  10539.     }
  10540.     fprintf (of, ")");
  10541.   }
  10542.  
  10543.   fprintf (of, " ");
  10544.  
  10545.   offset = 0;
  10546.   concat = 0;
  10547.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  10548.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  10549.     if (ap->nchars != 0)
  10550.       concat = 0;
  10551.     offset += ap->nchars;
  10552.     if (ap->stringify)
  10553.       fprintf (of, " #");
  10554.     if (ap->raw_before && !concat)
  10555.       fprintf (of, " ## ");
  10556.     concat = 0;
  10557.     dump_arg_n (defn, ap->argno, of);
  10558.     if (ap->raw_after) {
  10559.       fprintf (of, " ## ");
  10560.       concat = 1;
  10561.     }
  10562.   }
  10563.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  10564.   fprintf (of, "\n");
  10565. }
  10566.  
  10567. /* Dump all macro definitions as #defines to stdout.  */
  10568.  
  10569. static void
  10570. dump_all_macros ()
  10571. {
  10572.   int bucket;
  10573.  
  10574.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  10575.     register HASHNODE *hp;
  10576.  
  10577.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  10578.       if (hp->type == T_MACRO)
  10579.     dump_single_macro (hp, stdout);
  10580.     }
  10581.   }
  10582. }
  10583.  
  10584. /* Output to OF a substring of a macro definition.
  10585.    BASE is the beginning of the definition.
  10586.    Output characters START thru LENGTH.
  10587.    Discard newlines outside of strings, thus
  10588.    converting funny-space markers to ordinary spaces.  */
  10589.  
  10590. static void
  10591. dump_defn_1 (base, start, length, of)
  10592.      U_CHAR *base;
  10593.      int start;
  10594.      int length;
  10595.      FILE *of;
  10596. {
  10597.   U_CHAR *p = base + start;
  10598.   U_CHAR *limit = base + start + length;
  10599.  
  10600.   while (p < limit) {
  10601.     if (*p != '\n')
  10602.       putc (*p, of);
  10603.     else if (*p == '\"' || *p =='\'') {
  10604.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  10605.                        NULL_PTR, NULL_PTR);
  10606.       fwrite (p, p1 - p, 1, of);
  10607.       p = p1 - 1;
  10608.     }
  10609.     p++;
  10610.   }
  10611. }
  10612.  
  10613. /* Print the name of argument number ARGNUM of macro definition DEFN
  10614.    to OF.
  10615.    Recall that DEFN->args.argnames contains all the arg names
  10616.    concatenated in reverse order with comma-space in between.  */
  10617.  
  10618. static void
  10619. dump_arg_n (defn, argnum, of)
  10620.      DEFINITION *defn;
  10621.      int argnum;
  10622.      FILE *of;
  10623. {
  10624.   register U_CHAR *p = defn->args.argnames;
  10625.   while (argnum + 1 < defn->nargs) {
  10626.     p = (U_CHAR *) index (p, ' ') + 1;
  10627.     argnum++;
  10628.   }
  10629.  
  10630.   while (*p && *p != ',') {
  10631.     putc (*p, of);
  10632.     p++;
  10633.   }
  10634. }
  10635.  
  10636. /* Initialize syntactic classifications of characters.  */
  10637.  
  10638. static void
  10639. initialize_char_syntax ()
  10640. {
  10641.   register int i;
  10642.  
  10643.   /*
  10644.    * Set up is_idchar and is_idstart tables.  These should be
  10645.    * faster than saying (is_alpha (c) || c == '_'), etc.
  10646.    * Set up these things before calling any routines tthat
  10647.    * refer to them.
  10648.    */
  10649.   for (i = 'a'; i <= 'z'; i++) {
  10650.     is_idchar[i - 'a' + 'A'] = 1;
  10651.     is_idchar[i] = 1;
  10652.     is_idstart[i - 'a' + 'A'] = 1;
  10653.     is_idstart[i] = 1;
  10654.   }
  10655.   for (i = '0'; i <= '9'; i++)
  10656.     is_idchar[i] = 1;
  10657.   is_idchar['_'] = 1;
  10658.   is_idstart['_'] = 1;
  10659.   is_idchar['$'] = dollars_in_ident;
  10660.   is_idstart['$'] = dollars_in_ident;
  10661.  
  10662.   /* horizontal space table */
  10663.   is_hor_space[' '] = 1;
  10664.   is_hor_space['\t'] = 1;
  10665.   is_hor_space['\v'] = 1;
  10666.   is_hor_space['\f'] = 1;
  10667.   is_hor_space['\r'] = 1;
  10668.  
  10669.   is_space[' '] = 1;
  10670.   is_space['\t'] = 1;
  10671.   is_space['\v'] = 1;
  10672.   is_space['\f'] = 1;
  10673.   is_space['\n'] = 1;
  10674.   is_space['\r'] = 1;
  10675. }
  10676.  
  10677. /* Initialize the built-in macros.  */
  10678.  
  10679. static void
  10680. initialize_builtins (inp, outp)
  10681.      FILE_BUF *inp;
  10682.      FILE_BUF *outp;
  10683. {
  10684.   install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
  10685.   install ("__DATE__", -1, T_DATE, 0, 0, -1);
  10686.   install ("__FILE__", -1, T_FILE, 0, 0, -1);
  10687.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
  10688.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
  10689.   install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
  10690. #ifndef NO_BUILTIN_SIZE_TYPE
  10691.   install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
  10692. #endif
  10693. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  10694.   install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
  10695. #endif
  10696.   install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
  10697.   install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
  10698.   install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
  10699.   install ("__TIME__", -1, T_TIME, 0, 0, -1);
  10700.   if (!traditional)
  10701.     install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
  10702.   if (objc)
  10703.     install ("__OBJC__", -1, T_CONST, 1, 0, -1);
  10704. #ifdef NEXT_CPP_SERVER
  10705.   install ("_NEXT_SOURCE", -1, T_CONST, 1, 0, -1);
  10706.   install ("__GNU__", -1, T_CONST, 1, 0, -1);
  10707.   install ("__GNUC__", -1, T_CONST, 2, 0, -1);
  10708. #else
  10709. /*  This is supplied using a -D by the compiler driver
  10710.     so that it is present only when truly compiling with GNU C.  */
  10711. /*  install ("__GNUC__", -1, T_CONST, 2, 0, -1);  */
  10712. #endif
  10713.   if (debug_output)
  10714.     {
  10715.       char directive[2048];
  10716.       register struct directive *dp = &directive_table[0];
  10717.       struct tm *timebuf = timestamp ();
  10718.  
  10719.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  10720.            instack[0].nominal_fname);
  10721.       output_line_command (inp, outp, 0, same_file);
  10722.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10723.  
  10724.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  10725.       output_line_command (inp, outp, 0, same_file);
  10726.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10727.  
  10728. #ifndef NO_BUILTIN_SIZE_TYPE
  10729.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  10730.       output_line_command (inp, outp, 0, same_file);
  10731.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10732. #endif
  10733.  
  10734. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  10735.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  10736.       output_line_command (inp, outp, 0, same_file);
  10737.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10738. #endif
  10739.  
  10740.       sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
  10741.       output_line_command (inp, outp, 0, same_file);
  10742.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10743.  
  10744.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  10745.            monthnames[timebuf->tm_mon],
  10746.            timebuf->tm_mday, timebuf->tm_year + 1900);
  10747.       output_line_command (inp, outp, 0, same_file);
  10748.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10749.  
  10750.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  10751.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  10752.       output_line_command (inp, outp, 0, same_file);
  10753.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10754.  
  10755.       if (!traditional)
  10756.     {
  10757.           sprintf (directive, " __STDC__ 1");
  10758.           output_line_command (inp, outp, 0, same_file);
  10759.           pass_thru_directive (directive, &directive[strlen (directive)],
  10760.                    outp, dp);
  10761.     }
  10762.       if (objc)
  10763.     {
  10764.           sprintf (directive, " __OBJC__ 1");
  10765.           output_line_command (inp, outp, 0, same_file);
  10766.           pass_thru_directive (directive, &directive[strlen (directive)],
  10767.                    outp, dp);
  10768.     }
  10769.     }
  10770. }
  10771.  
  10772. /*
  10773.  * process a given definition string, for initialization
  10774.  * If STR is just an identifier, define it with value 1.
  10775.  * If STR has anything after the identifier, then it should
  10776.  * be identifier=definition.
  10777.  */
  10778.  
  10779. static void
  10780. make_definition (str, op)
  10781.      U_CHAR *str;
  10782.      FILE_BUF *op;
  10783. {
  10784.   FILE_BUF *ip;
  10785.   struct directive *kt;
  10786.   U_CHAR *buf, *p;
  10787.  
  10788.   buf = str;
  10789.   p = str;
  10790.   if (!is_idstart[*p]) {
  10791.     error ("malformed option `-D %s'", str);
  10792.     return;
  10793.   }
  10794.   while (is_idchar[*++p])
  10795.     ;
  10796.   if (*p == 0) {
  10797.     buf = (U_CHAR *) alloca (p - buf + 4);
  10798.     strcpy ((char *)buf, str);
  10799.     strcat ((char *)buf, " 1");
  10800.   } else if (*p != '=') {
  10801.     error ("malformed option `-D %s'", str);
  10802.     return;
  10803.   } else {
  10804.     U_CHAR *q;
  10805.     /* Copy the entire option so we can modify it.  */
  10806.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  10807.     strncpy (buf, str, p - str);
  10808.     /* Change the = to a space.  */
  10809.     buf[p - str] = ' ';
  10810.     /* Scan for any backslash-newline and remove it.  */
  10811.     p++;
  10812.     q = &buf[p - str];
  10813.     while (*p) {
  10814.       if (*p == '\\' && p[1] == '\n')
  10815.     p += 2;
  10816.       /* Change newline chars into newline-markers.  */
  10817.       else if (*p == '\n')
  10818.     {
  10819.       *q++ = '\n';
  10820.       *q++ = '\n';
  10821.       p++;
  10822.     }
  10823.       else
  10824.     *q++ = *p++;
  10825.     }
  10826.     *q = 0;
  10827.   }
  10828.   
  10829.   ip = &instack[++indepth];
  10830.   ip->nominal_fname = ip->fname = "*Initialization*";
  10831.  
  10832.   ip->buf = ip->bufp = buf;
  10833.   ip->length = strlen (buf);
  10834.   ip->lineno = 1;
  10835.   ip->macro = 0;
  10836.   ip->free_ptr = 0;
  10837.   ip->if_stack = if_stack;
  10838.   ip->system_header_p = 0;
  10839. #ifdef NEXT_CPP_SERVER
  10840.   ip->dir = ip->file = 0;
  10841.   ip->beg_of_line = 0;
  10842. #endif
  10843.  
  10844.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  10845.     ;
  10846.  
  10847.   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
  10848.   do_define (buf, buf + strlen (buf), NULL, kt);
  10849.   --indepth;
  10850. }
  10851.  
  10852. /* JF, this does the work for the -U option */
  10853.  
  10854. static void
  10855. make_undef (str, op)
  10856.      U_CHAR *str;
  10857.      FILE_BUF *op;
  10858. {
  10859.   FILE_BUF *ip;
  10860.   struct directive *kt;
  10861.  
  10862.   ip = &instack[++indepth];
  10863.   ip->nominal_fname = ip->fname = "*undef*";
  10864.  
  10865.   ip->buf = ip->bufp = str;
  10866.   ip->length = strlen (str);
  10867.   ip->lineno = 1;
  10868.   ip->macro = 0;
  10869.   ip->free_ptr = 0;
  10870.   ip->if_stack = if_stack;
  10871.   ip->system_header_p = 0;
  10872.  
  10873.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  10874.     ;
  10875.  
  10876.   do_undef (str, str + strlen (str), op, kt);
  10877.   --indepth;
  10878. }
  10879.  
  10880. /* Process the string STR as if it appeared as the body of a #assert.
  10881.    OPTION is the option name for which STR was the argument.  */
  10882.  
  10883. static void
  10884. make_assertion (option, str)
  10885.      char *option;
  10886.      U_CHAR *str;
  10887. {
  10888.   FILE_BUF *ip;
  10889.   struct directive *kt;
  10890.   U_CHAR *buf, *p, *q;
  10891.  
  10892.   /* Copy the entire option so we can modify it.  */
  10893.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  10894.   strcpy ((char *) buf, str);
  10895.   /* Scan for any backslash-newline and remove it.  */
  10896.   p = q = buf;
  10897.   while (*p) {
  10898.     if (*p == '\\' && p[1] == '\n')
  10899.       p += 2;
  10900.     else
  10901.       *q++ = *p++;
  10902.   }
  10903.   *q = 0;
  10904.  
  10905.   p = buf;
  10906.   if (!is_idstart[*p]) {
  10907.     error ("malformed option `%s %s'", option, str);
  10908.     return;
  10909.   }
  10910.   while (is_idchar[*++p])
  10911.     ;
  10912.   while (*p == ' ' || *p == '\t') p++;
  10913.   if (! (*p == 0 || *p == '(')) {
  10914.     error ("malformed option `%s %s'", option, str);
  10915.     return;
  10916.   }
  10917.   
  10918.   ip = &instack[++indepth];
  10919.   ip->nominal_fname = ip->fname = "*Initialization*";
  10920.  
  10921.   ip->buf = ip->bufp = buf;
  10922.   ip->length = strlen (buf);
  10923.   ip->lineno = 1;
  10924.   ip->macro = 0;
  10925.   ip->free_ptr = 0;
  10926.   ip->if_stack = if_stack;
  10927.   ip->system_header_p = 0;
  10928. #ifdef NEXT_CPP_SERVER
  10929.   ip->dir = ip->file = 0;
  10930.   ip->beg_of_line = 0;
  10931. #endif
  10932.  
  10933.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  10934.     ;
  10935.  
  10936.   /* pass NULL as output ptr to do_define since we KNOW it never
  10937.      does any output.... */
  10938.   do_assert (buf, buf + strlen (buf) , NULL_PTR, kt);
  10939.   --indepth;
  10940. }
  10941.  
  10942. /* Append a chain of `struct file_name_list's
  10943.    to the end of the main include chain.
  10944.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  10945.  
  10946. static void
  10947. append_include_chain (first, last)
  10948.      struct file_name_list *first, *last;
  10949. {
  10950.   struct file_name_list *dir;
  10951.  
  10952.   if (!first || !last)
  10953.     return;
  10954.  
  10955.   if (include == 0)
  10956.     include = first;
  10957.   else
  10958.     last_include->next = first;
  10959.  
  10960.   if (first_bracket_include == 0)
  10961.     first_bracket_include = first;
  10962.  
  10963.   for (dir = first; ; dir = dir->next) {
  10964.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  10965.     if (len > max_include_len)
  10966.       max_include_len = len;
  10967.     if (dir == last)
  10968.       break;
  10969.   }
  10970.  
  10971.   last->next = NULL;
  10972.   last_include = last;
  10973. }
  10974.  
  10975. #ifdef NEXT_FRAMEWORKS
  10976. /* Append a chain of `struct file_name_list's
  10977.    to the end of the main Frameworks chain.
  10978.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  10979.  
  10980. static void
  10981. append_framework_chain (first, last)
  10982.      struct file_name_list *first, *last;
  10983. {
  10984.   struct file_name_list *dir;
  10985.  
  10986.   if (!first || !last)
  10987.     return;
  10988.  
  10989.   if (framework == 0)
  10990.     framework = first;
  10991.   else
  10992.     last_framework->next = first;
  10993.  
  10994.   if (first_bracket_framework == 0)
  10995.     first_bracket_framework = first;
  10996.  
  10997.   for (dir = first; ; dir = dir->next) {
  10998.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  10999.     if (len > max_include_len)
  11000.       max_include_len = len;
  11001.     if (dir == last)
  11002.       break;
  11003.   }
  11004.  
  11005.   last->next = NULL;
  11006.   last_framework = last;
  11007. }
  11008.  
  11009. #endif
  11010. /* Add output to `deps_buffer' for the -M switch.
  11011.    STRING points to the text to be output.
  11012.    SPACER is ':' for targets, ' ' for dependencies, zero for text
  11013.    to be inserted literally.  */
  11014.  
  11015. static void
  11016. deps_output (string, spacer)
  11017.      char *string;
  11018.      int spacer;
  11019. {
  11020.   int size = strlen (string);
  11021.  
  11022.   if (size == 0)
  11023.     return;
  11024.  
  11025. #ifndef MAX_OUTPUT_COLUMNS
  11026. #define MAX_OUTPUT_COLUMNS 72
  11027. #endif
  11028.   if (spacer
  11029.       && deps_column > 0
  11030.       && (deps_column + size) > MAX_OUTPUT_COLUMNS)
  11031.   {
  11032.     deps_output (" \\\n  ", 0);
  11033.     deps_column = 0;
  11034.   }
  11035.  
  11036.   if (deps_size + size + 8 > deps_allocated_size) {
  11037.     deps_allocated_size = (deps_size + size + 50) * 2;
  11038.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  11039.   }
  11040.   if (spacer == ' ' && deps_column > 0)
  11041.     deps_buffer[deps_size++] = ' ';
  11042.   bcopy (string, &deps_buffer[deps_size], size);
  11043.   deps_size += size;
  11044.   deps_column += size;
  11045.   if (spacer == ':')
  11046.     deps_buffer[deps_size++] = ':';
  11047.   deps_buffer[deps_size] = 0;
  11048. }
  11049.  
  11050. #if defined(USG) || defined(VMS)
  11051. #ifndef BSTRING
  11052.  
  11053. void
  11054. bzero (b, length)
  11055.      register char *b;
  11056.      register unsigned length;
  11057. {
  11058.   while (length-- > 0)
  11059.     *b++ = 0;
  11060. }
  11061.  
  11062. void
  11063. bcopy (b1, b2, length)
  11064.      register char *b1;
  11065.      register char *b2;
  11066.      register unsigned length;
  11067. {
  11068.   while (length-- > 0)
  11069.     *b2++ = *b1++;
  11070. }
  11071.  
  11072. int
  11073. bcmp (b1, b2, length)    /* This could be a macro! */
  11074.      register char *b1;
  11075.      register char *b2;
  11076.      register unsigned length;
  11077. {
  11078.    while (length-- > 0)
  11079.      if (*b1++ != *b2++)
  11080.        return 1;
  11081.  
  11082.    return 0;
  11083. }
  11084. #endif /* not BSTRING */
  11085. #endif /* USG or VMS */
  11086.  
  11087.  
  11088. static void
  11089. fatal (str, arg)
  11090.      char *str, *arg;
  11091. {
  11092. #ifdef REPORT_EVENT
  11093.   REPORT_EVENT (0, progname, NULL, 0, str, arg, 0, 0);
  11094. #endif
  11095.   fprintf (stderr, "%s: ", progname);
  11096.   fprintf (stderr, str, arg);
  11097.   fprintf (stderr, "\n");
  11098. #ifdef NEXT_CPP_SERVER
  11099.   if (serverized)
  11100.      return;
  11101. #endif    
  11102.   exit (FAILURE_EXIT_CODE);
  11103. }
  11104.  
  11105. /* More 'friendly' abort that prints the line and file.
  11106.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  11107.  
  11108. void
  11109. fancy_abort ()
  11110. {
  11111.   fatal ("Internal gcc abort.");
  11112. }
  11113.  
  11114. static void
  11115. perror_with_name (name)
  11116.      char *name;
  11117. {
  11118. #ifdef REPORT_EVENT
  11119.   REPORT_EVENT (0, progname, NULL, 0,
  11120.         "%s: %s", name,
  11121.         (errno < sys_nerr)
  11122.         ? sys_errlist[errno]
  11123.         : "undocumented I/O error",
  11124.         0);
  11125. #endif
  11126.   fprintf (stderr, "%s: ", progname);
  11127.   if (errno < sys_nerr)
  11128.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  11129.   else
  11130.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  11131.   errors++;
  11132. }
  11133.  
  11134. static void
  11135. pfatal_with_name (name)
  11136.      char *name;
  11137. {
  11138.   perror_with_name (name);
  11139. #ifdef VMS
  11140.   exit (vaxc$errno);
  11141. #else
  11142.   exit (FAILURE_EXIT_CODE);
  11143. #endif
  11144. }
  11145.  
  11146. /* Handler for SIGPIPE.  */
  11147.  
  11148. static void
  11149. pipe_closed (signo)
  11150.      /* If this is missing, some compilers complain.  */
  11151.      int signo;
  11152. {
  11153.   fatal ("output pipe has been closed");
  11154. }
  11155.  
  11156. static void
  11157. memory_full ()
  11158. {
  11159.   fatal ("Memory exhausted.");
  11160. }
  11161.  
  11162.  
  11163. char *
  11164. xmalloc (size)
  11165.      unsigned size;
  11166. {
  11167.   register char *ptr = (char *) malloc (size);
  11168.   if (ptr != 0) return (ptr);
  11169.   memory_full ();
  11170.   /*NOTREACHED*/
  11171.   return 0;
  11172. }
  11173.  
  11174. static char *
  11175. xrealloc (old, size)
  11176.      char *old;
  11177.      unsigned size;
  11178. {
  11179.   register char *ptr = (char *) realloc (old, size);
  11180.   if (ptr != 0) return (ptr);
  11181.   memory_full ();
  11182.   /*NOTREACHED*/
  11183.   return 0;
  11184. }
  11185.  
  11186. static char *
  11187. xcalloc (number, size)
  11188.      unsigned number, size;
  11189. {
  11190.   register unsigned total = number * size;
  11191.   register char *ptr = (char *) malloc (total);
  11192.   if (ptr != 0) {
  11193.     if (total > 100)
  11194.       bzero (ptr, total);
  11195.     else {
  11196.       /* It's not too long, so loop, zeroing by longs.
  11197.      It must be safe because malloc values are always well aligned.  */
  11198.       register long *zp = (long *) ptr;
  11199.       register long *zl = (long *) (ptr + total - 4);
  11200.       register int i = total - 4;
  11201.       while (zp < zl)
  11202.     *zp++ = 0;
  11203.       if (i < 0)
  11204.     i = 0;
  11205.       while (i < total)
  11206.     ptr[i++] = 0;
  11207.     }
  11208.     return ptr;
  11209.   }
  11210.   memory_full ();
  11211.   /*NOTREACHED*/
  11212.   return 0;
  11213. }
  11214.  
  11215. static char *
  11216. savestring (input)
  11217.      char *input;
  11218. {
  11219.   unsigned size = strlen (input);
  11220.   char *output = xmalloc (size + 1);
  11221. #ifdef NEXT_FRAMEWORKS
  11222.   strncpy (output, input, size + 1);
  11223. #else  
  11224.   strcpy (output, input);
  11225. #endif
  11226.   return output;
  11227. }
  11228.  
  11229. /* Get the file-mode and data size of the file open on FD
  11230.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  11231.  
  11232. static int
  11233. file_size_and_mode (fd, mode_pointer, size_pointer)
  11234.      int fd;
  11235.      int *mode_pointer;
  11236.      long int *size_pointer;
  11237. {
  11238.   struct stat sbuf;
  11239.  
  11240.   if (fstat (fd, &sbuf) < 0) return (-1);
  11241.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  11242.   if (size_pointer) *size_pointer = sbuf.st_size;
  11243.   return 0;
  11244. }
  11245.  
  11246. static void
  11247. output_dots (fd, depth)
  11248.      FILE* fd;
  11249.      int depth;
  11250. {
  11251.   while (depth > 0) {
  11252.     putc ('.', fd);
  11253.     depth--;
  11254.   }
  11255. }
  11256.   
  11257.  
  11258. #ifdef VMS
  11259.  
  11260. /* Under VMS we need to fix up the "include" specification
  11261.    filename so that everything following the 1st slash is
  11262.    changed into its correct VMS file specification. */
  11263.  
  11264. static void
  11265. hack_vms_include_specification (fname)
  11266.      char *fname;
  11267. {
  11268.   register char *cp, *cp1, *cp2;
  11269.   int f, check_filename_before_returning, no_prefix_seen;
  11270.   char Local[512];
  11271.  
  11272.   check_filename_before_returning = 0;
  11273.   no_prefix_seen = 0;
  11274.  
  11275.   /* Ignore leading "./"s */
  11276.   while (fname[0] == '.' && fname[1] == '/') {
  11277.     strcpy (fname, fname+2);
  11278.     no_prefix_seen = 1;        /* mark this for later */
  11279.   }
  11280.   /* Look for the boundary between the VMS and UNIX filespecs */
  11281.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  11282.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  11283.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  11284.   if (cp) {
  11285.     cp++;
  11286.   } else {
  11287.     cp = index (fname, '/');    /* Look for the "/" */
  11288.   }
  11289.  
  11290.   /*
  11291.    * Check if we have a vax-c style '#include filename'
  11292.    * and add the missing .h
  11293.    */
  11294.   if (cp == 0) {
  11295.     if (index(fname,'.') == 0)
  11296.       strcat(fname, ".h");
  11297.   } else {
  11298.     if (index(cp,'.') == 0)
  11299.       strcat(cp, ".h");
  11300.   }
  11301.  
  11302.   cp2 = Local;            /* initialize */
  11303.  
  11304.   /* We are trying to do a number of things here.  First of all, we are
  11305.      trying to hammer the filenames into a standard format, such that later
  11306.      processing can handle them.
  11307.      
  11308.      If the file name contains something like [dir.], then it recognizes this
  11309.      as a root, and strips the ".]".  Later processing will add whatever is
  11310.      needed to get things working properly.
  11311.      
  11312.      If no device is specified, then the first directory name is taken to be
  11313.      a device name (or a rooted logical). */
  11314.  
  11315.   /* See if we found that 1st slash */
  11316.   if (cp == 0) return;        /* Nothing to do!!! */
  11317.   if (*cp != '/') return;    /* Nothing to do!!! */
  11318.   /* Point to the UNIX filename part (which needs to be fixed!) */
  11319.   cp1 = cp+1;
  11320.   /* If the directory spec is not rooted, we can just copy
  11321.      the UNIX filename part and we are done */
  11322.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  11323.     if (cp[-2] != '.') {
  11324.       /*
  11325.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  11326.        * We strip the `]', and then splice the two parts of the name in the
  11327.        * usual way.  Given the default locations for include files in cccp.c,
  11328.        * we will only use this code if the user specifies alternate locations
  11329.        * with the /include (-I) switch on the command line.  */
  11330.       cp -= 1;            /* Strip "]" */
  11331.       cp1--;            /* backspace */
  11332.     } else {
  11333.       /*
  11334.        * The VMS part has a ".]" at the end, and this will not do.  Later
  11335.        * processing will add a second directory spec, and this would be a syntax
  11336.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  11337.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  11338.        * generation of the 000000 root directory spec (which does not belong here
  11339.        * in this case).
  11340.        */
  11341.       cp -= 2;            /* Strip ".]" */
  11342.       cp1--; };            /* backspace */
  11343.   } else {
  11344.  
  11345.     /* We drop in here if there is no VMS style directory specification yet.
  11346.      * If there is no device specification either, we make the first dir a
  11347.      * device and try that.  If we do not do this, then we will be essentially
  11348.      * searching the users default directory (as if they did a #include "asdf.h").
  11349.      *
  11350.      * Then all we need to do is to push a '[' into the output string. Later
  11351.      * processing will fill this in, and close the bracket.
  11352.      */
  11353.     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  11354.     *cp2++ = '[';        /* Open the directory specification */
  11355.   }
  11356.  
  11357.   /* at this point we assume that we have the device spec, and (at least
  11358.      the opening "[" for a directory specification.  We may have directories
  11359.      specified already */
  11360.  
  11361.   /* If there are no other slashes then the filename will be
  11362.      in the "root" directory.  Otherwise, we need to add
  11363.      directory specifications. */
  11364.   if (index (cp1, '/') == 0) {
  11365.     /* Just add "000000]" as the directory string */
  11366.     strcpy (cp2, "000000]");
  11367.     cp2 += strlen (cp2);
  11368.     check_filename_before_returning = 1; /* we might need to fool with this later */
  11369.   } else {
  11370.     /* As long as there are still subdirectories to add, do them. */
  11371.     while (index (cp1, '/') != 0) {
  11372.       /* If this token is "." we can ignore it */
  11373.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  11374.     cp1 += 2;
  11375.     continue;
  11376.       }
  11377.       /* Add a subdirectory spec. Do not duplicate "." */
  11378.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  11379.     *cp2++ = '.';
  11380.       /* If this is ".." then the spec becomes "-" */
  11381.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  11382.     /* Add "-" and skip the ".." */
  11383.     *cp2++ = '-';
  11384.     cp1 += 3;
  11385.     continue;
  11386.       }
  11387.       /* Copy the subdirectory */
  11388.       while (*cp1 != '/') *cp2++= *cp1++;
  11389.       cp1++;            /* Skip the "/" */
  11390.     }
  11391.     /* Close the directory specification */
  11392.     if (cp2[-1] == '.')        /* no trailing periods */
  11393.       cp2--;
  11394.     *cp2++ = ']';
  11395.   }
  11396.   /* Now add the filename */
  11397.   while (*cp1) *cp2++ = *cp1++;
  11398.   *cp2 = 0;
  11399.   /* Now append it to the original VMS spec. */
  11400.   strcpy (cp, Local);
  11401.  
  11402.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  11403.      remove the [000000], and return that name.  This provides flexibility
  11404.      to the user in that they can use both rooted and non-rooted logical names
  11405.      to point to the location of the file.  */
  11406.  
  11407.   if (check_filename_before_returning && no_prefix_seen) {
  11408.     f = open (fname, O_RDONLY, 0666);
  11409.     if (f >= 0) {
  11410.       /* The file name is OK as it is, so return it as is.  */
  11411.       close (f);
  11412.       return;
  11413.     }
  11414.     /* The filename did not work.  Try to remove the [000000] from the name,
  11415.        and return it.  */
  11416.     cp = index (fname, '[');
  11417.     cp2 = index (fname, ']') + 1;
  11418.     strcpy (cp, cp2);        /* this gets rid of it */
  11419.   }
  11420.   return;
  11421. }
  11422. #endif    /* VMS */
  11423.  
  11424. #ifdef    VMS
  11425.  
  11426. /* These are the read/write replacement routines for
  11427.    VAX-11 "C".  They make read/write behave enough
  11428.    like their UNIX counterparts that CCCP will work */
  11429.  
  11430. static int
  11431. read (fd, buf, size)
  11432.      int fd;
  11433.      char *buf;
  11434.      int size;
  11435. {
  11436. #undef    read    /* Get back the REAL read routine */
  11437.   register int i;
  11438.   register int total = 0;
  11439.  
  11440.   /* Read until the buffer is exhausted */
  11441.   while (size > 0) {
  11442.     /* Limit each read to 32KB */
  11443.     i = (size > (32*1024)) ? (32*1024) : size;
  11444.     i = read (fd, buf, i);
  11445.     if (i <= 0) {
  11446.       if (i == 0) return (total);
  11447.       return (i);
  11448.     }
  11449.     /* Account for this read */
  11450.     total += i;
  11451.     buf += i;
  11452.     size -= i;
  11453.   }
  11454.   return (total);
  11455. }
  11456.  
  11457. static int
  11458. write (fd, buf, size)
  11459.      int fd;
  11460.      char *buf;
  11461.      int size;
  11462. {
  11463. #undef    write    /* Get back the REAL write routine */
  11464.   int i;
  11465.   int j;
  11466.  
  11467.   /* Limit individual writes to 32Kb */
  11468.   i = size;
  11469.   while (i > 0) {
  11470.     j = (i > (32*1024)) ? (32*1024) : i;
  11471.     if (write (fd, buf, j) < 0) return (-1);
  11472.     /* Account for the data written */
  11473.     buf += j;
  11474.     i -= j;
  11475.   }
  11476.   return (size);
  11477. }
  11478.  
  11479. /* The following wrapper functions supply additional arguments to the VMS
  11480.    I/O routines to optimize performance with file handling.  The arguments
  11481.    are:
  11482.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  11483.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  11484.      "fop=tef"- Truncate unused portions of file when closing file.
  11485.      "shr=nil"- Disallow file sharing while file is open.
  11486.  */
  11487.  
  11488. static FILE *
  11489. freopen (fname, type, oldfile)
  11490.      char *fname;
  11491.      char *type;
  11492.      FILE *oldfile;
  11493. {
  11494. #undef    freopen    /* Get back the REAL fopen routine */
  11495.   if (strcmp (type, "w") == 0)
  11496.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  11497.   return freopen (fname, type, oldfile, "mbc=16");
  11498. }
  11499.  
  11500. static FILE *
  11501. fopen (fname, type)
  11502.      char *fname;
  11503.      char *type;
  11504. {
  11505. #undef fopen    /* Get back the REAL fopen routine */
  11506.   if (strcmp (type, "w") == 0)
  11507.     return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  11508.   return fopen (fname, type, "mbc=16");
  11509. }
  11510.  
  11511. static int 
  11512. open (fname, flags, prot)
  11513.      char *fname;
  11514.      int flags;
  11515.      int prot;
  11516. {
  11517. #undef open    /* Get back the REAL open routine */
  11518.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  11519. }
  11520.  
  11521. /* Avoid run-time library bug, where copying M out of N+M characters with
  11522.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  11523.    gcc-cpp exercises this particular bug.  */
  11524.  
  11525. static char *
  11526. strncat (dst, src, cnt)
  11527.      char *dst;
  11528.      const char *src;
  11529.      unsigned cnt;
  11530. {
  11531.   register char *d = dst, *s = (char *) src;
  11532.   register int n = cnt;    /* convert to _signed_ type */
  11533.  
  11534.   while (*d) d++;    /* advance to end */
  11535.   while (--n >= 0)
  11536.     if (!(*d++ = *s++)) break;
  11537.   if (n < 0) *d = '\0';
  11538.   return dst;
  11539. }
  11540. #endif /* VMS */
  11541.  
  11542. #ifdef NEXT_SEMANTICS
  11543.  
  11544. struct negative_file {
  11545.   char *name;
  11546.   struct negative_file *next;
  11547. };
  11548. static struct negative_file *negative_hash_table[IMPORT_HASH_SIZE];
  11549.  
  11550. static void
  11551. add_to_negative_cache (fname)
  11552.      char *fname;
  11553. {
  11554.   struct negative_file *i;
  11555.   int hashval;
  11556.  
  11557.   hashval = import_hash (fname);
  11558.   i = (struct negative_file *)xmalloc (sizeof (struct negative_file));
  11559.   i->name = fname;
  11560.   i->next = negative_hash_table[hashval];
  11561.   negative_hash_table[hashval] = i;
  11562. }
  11563.  
  11564. static int
  11565. found_in_negative_cache (filename)
  11566.      char *filename;
  11567. {
  11568.   struct negative_file *i;
  11569.   int hashval;
  11570.  
  11571.   hashval = import_hash (filename);
  11572.  
  11573.   /* Attempt to find file in list of already included files */
  11574.   i = negative_hash_table[hashval];
  11575.  
  11576.   while (i) {
  11577.     if (!strcmp (filename, i->name))
  11578.       return 1;        /* return found */
  11579.     i = i->next;
  11580.   }
  11581.   return 0;
  11582. }
  11583.  
  11584. #if 0
  11585. void get_input_file_names(in_fname, in_fname_count, in_fname_list)
  11586.   char *in_fname;
  11587.   int *in_fname_count; /* output */
  11588.   struct file_name_list **in_fname_list; /* optional output */
  11589. {
  11590.   char *in_fnamep = in_fname;
  11591.   char c;
  11592.  
  11593.   *in_fname_count = 1;
  11594.   while (c = *in_fnamep++)
  11595.     if (c == ' ') (*in_fname_count)++; /* need the parens */
  11596.  
  11597.   if (*in_fname_count > 1) {
  11598.     int nelem = 0;
  11599.     *in_fname_list = (struct file_name_list *) xmalloc (*in_fname_count * 
  11600.                          sizeof (struct file_name_list));
  11601.     in_fnamep = in_fname;
  11602.     if ((*in_fname_list)[nelem++].fname = strtok(in_fnamep," "))
  11603.       while ((*in_fname_list)[nelem++].fname = strtok(NULL," "));
  11604.   } else {
  11605.     *in_fname_list = 0;
  11606.   }
  11607. }
  11608. #endif
  11609.  
  11610. static void
  11611. add_version_of_header(fileptr, start, length)
  11612.      struct file_name_list *fileptr;
  11613.      unsigned int start;
  11614.      unsigned int length;
  11615. {
  11616.    struct file_name_list *ptr;
  11617.  
  11618.    ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  11619.    bzero (ptr, sizeof (struct file_name_list));
  11620.  
  11621.    ptr->fname = fileptr->fname;
  11622.    ptr->versions = fileptr->versions;
  11623.    fileptr->versions = ptr;
  11624.    ptr->relative_position_of_file_start = start;
  11625.    ptr->file_length = length;
  11626. }
  11627.  
  11628. static int
  11629. duplicate_version_of_header(op, fileptr, position_of_file_start, 
  11630.                                      file_length)
  11631.      FILE_BUF *op;
  11632.      struct file_name_list *fileptr;
  11633.      U_CHAR *position_of_file_start;
  11634.      unsigned int file_length;
  11635. {
  11636.   int found_a_version = 0;
  11637.   do {
  11638.     if ((file_length == fileptr->file_length) &&
  11639.         (!bcmp (position_of_file_start, 
  11640.                 op->buf+fileptr->relative_position_of_file_start,
  11641.                 file_length))) {
  11642.       found_a_version = 1;
  11643.     } 
  11644.     fileptr = fileptr->versions;
  11645.   } while (fileptr && !found_a_version);
  11646.   return found_a_version;
  11647. }
  11648.  
  11649. /* This saves processing 497k for "appkit.h" (with the "include" heuristic)
  11650.    This results in a significant savings in user time (~40% I believe!) */
  11651.  
  11652. static int
  11653. aggressive_skip_if_group (ip)
  11654.      FILE_BUF *ip;
  11655. {
  11656.     if (ip->bufp == ip->file->position_after_last_include) {
  11657.  
  11658.       /* #if/#ifdef/#ifndef's are all identified by "T_IF" (see calls to
  11659.          conditional_skip in do_if/do_xifdef) */
  11660.       if (if_stack && if_stack->type == T_IF && if_stack->control_macro) {
  11661.         /* just because we are in the context of "some" #ifndef, check
  11662.            whether the input we are currently processing had one. If it did,
  11663.            make sure we skip to its matching #endif, else go to end of buf */
  11664.         if (ip->file->control_macro)
  11665.           ip->bufp = ip->file->position_of_last_endif;
  11666.         else
  11667.           ip->bufp = ip->buf + ip->length;
  11668.         enable_macros_from_header(ip->file);
  11669.         /* fprintf(stderr, "SKIP for %s: %.20s (saved %d)\n", 
  11670.          *        ip->fname, ip->bufp, total_saved += ip->bufp-sav);
  11671.          */
  11672.         return 1;
  11673.       } else {
  11674.         /* this means the last include is in the context of an arbitrary
  11675.            conditional (i.e. not the "ifndef idiom"). for now, we will just
  11676.            exclude it from this optimization. It is possible to include it, 
  11677.            but this will require more complexity */
  11678.         /* fprintf(stderr, "CAN'T SKIP for %s: %.20s\n", ip->fname, ip->bufp); */
  11679.         return 0;
  11680.       }
  11681.     } else if (!ip->file->position_after_last_include) { 
  11682.       /* no #includes, currently processing a "leaf" header */
  11683.       ip->bufp = ip->buf + ip->length;
  11684.       enable_macros_from_header(ip->file);
  11685.       /* fprintf(stderr, "NO INCLUDES for %s: %.20s (saved %d)\n", 
  11686.        *        ip->fname, ip->bufp, total_saved += ip->bufp-sav);
  11687.        */
  11688.       return 1;
  11689.     } else { /* this case is not currently optimized (obviously) */
  11690.       return 0;
  11691.     }
  11692. }
  11693.  
  11694. static void
  11695. dump_memory_statistics ()
  11696. {
  11697.   struct file_name_list *new;
  11698.   struct import_file *imp;
  11699.   struct negative_file *neg;
  11700.   int n_elements = 0, n_other = 0, size_bufs = 0, size_mlists = 0;
  11701.   int n_expansion_strings = 0, n_string_count = 0, control_names = 0;
  11702.   int bucket;
  11703.  
  11704.   fprintf(stderr, "all_input_files (chain of struct file_name_list's):\n");
  11705.   for (new = all_input_files; new; new = new->next) {
  11706.     n_elements++;
  11707.     n_string_count += (strlen(new->fname)+1);
  11708.     size_bufs += new->length;
  11709.   }
  11710.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11711.                    n_elements * sizeof(struct file_name_list),
  11712.                    n_elements, sizeof(struct file_name_list));
  11713.   fprintf(stderr, "%10d bytes for input file names\n", n_string_count);
  11714.   fprintf(stderr, "%10d bytes for input buffers\n", size_bufs);
  11715.  
  11716.   n_string_count = n_elements = size_bufs = 0;
  11717.  
  11718.   fprintf(stderr, "all_output_files (chain of struct file_name_list's):\n");
  11719.   for (new = all_output_files; new; new = new->next) {
  11720.     n_elements++;
  11721.     if (new->fname)
  11722.       n_string_count += (strlen(new->fname)+1);
  11723.     size_bufs += new->length;
  11724.   }
  11725.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11726.                    n_elements * sizeof(struct file_name_list),
  11727.                    n_elements, sizeof(struct file_name_list));
  11728.   fprintf(stderr, "%10d bytes for output file names\n", n_string_count);
  11729.   fprintf(stderr, "%10d bytes for output buffers\n", size_bufs);
  11730.  
  11731.   n_string_count = n_elements = size_bufs = 0;
  11732.   fprintf(stderr, "all_include_files (chain of struct file_name_list's):\n");
  11733.   for (new = all_include_files; new; new = new->next) {
  11734.     n_elements++;
  11735.     n_string_count += (strlen(new->fname)+1);
  11736.     if (new->control_macro)
  11737.       control_names += (strlen(new->control_macro)+1);
  11738.     size_bufs += new->length;
  11739.     size_mlists += new->macro_count * sizeof ( HASHNODE * );
  11740.     fprintf(stderr, "%s\n", new->fname);
  11741.   }
  11742.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11743.                    n_elements * sizeof(struct file_name_list),
  11744.                    n_elements, sizeof(struct file_name_list));
  11745.   fprintf(stderr, "%10d bytes for file names\n", n_string_count);
  11746.   fprintf(stderr, "%10d bytes for control macro names\n", control_names);
  11747.   fprintf(stderr, "%10d bytes for input buffers\n", size_bufs);
  11748.   fprintf(stderr, "%10d bytes for macro lists\n", size_mlists);
  11749.  
  11750.   n_string_count = n_elements = 0;
  11751.   fprintf(stderr, "include directories (chain of struct file_name_list's):\n");
  11752.   for (new = include; new; new = new->next) {
  11753.     n_elements++;
  11754.     n_string_count += (strlen(new->fname)+1);
  11755.   }
  11756.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11757.                    n_elements * sizeof(struct file_name_list),
  11758.                    n_elements, sizeof(struct file_name_list));
  11759.   fprintf(stderr, "%10d bytes for directory names\n", n_string_count);
  11760.  
  11761.   n_elements = 0;
  11762.   fprintf(stderr, "import_hash_table (struct import_file's):\n");
  11763.   for (bucket = 0; bucket < IMPORT_HASH_SIZE; bucket++) {
  11764.     imp = import_hash_table[bucket];
  11765.     while (imp) {
  11766.       n_elements++;
  11767.       imp = imp->next;
  11768.     }
  11769.   }
  11770.   fprintf(stderr, "%10d bytes for import hash table buckets (statically allocated)\n",
  11771.            sizeof(import_hash_table));
  11772.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11773.                    n_elements * sizeof(struct import_file),
  11774.                    n_elements, sizeof(struct import_file));
  11775.  
  11776.   n_elements = 0;
  11777.   n_string_count = 0;
  11778.   fprintf(stderr, "negative_hash_table (struct negative_file's):\n");
  11779.   for (bucket = 0; bucket < IMPORT_HASH_SIZE; bucket++) {
  11780.     neg = negative_hash_table[bucket];
  11781.     while (neg) {
  11782.       n_elements++;
  11783.       n_string_count += (strlen(neg->name)+1);
  11784.       neg = neg->next;
  11785.     }
  11786.   }
  11787.   fprintf(stderr, "%10d bytes for negative hash table buckets (statically allocated)\n",
  11788.            sizeof(negative_hash_table));
  11789.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11790.                    n_elements * sizeof(struct negative_file),
  11791.                    n_elements, sizeof(struct negative_file));
  11792.   fprintf(stderr, "%10d bytes for file names\n", n_string_count);
  11793.  
  11794.   fprintf(stderr, "hashtab for macro's (struct hashnode's/definition's):\n");
  11795.   n_string_count = 0;
  11796.   n_elements = 0;
  11797.   n_other = 0;
  11798.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  11799.     register HASHNODE *hp;
  11800.  
  11801.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  11802.       if (hp->type == T_MACRO) {
  11803.         n_elements++;
  11804.         if (hp->value.defn->length) {
  11805.           n_string_count++;
  11806.           n_expansion_strings += hp->value.defn->length;
  11807.         }
  11808.       } else {
  11809.         n_other++;
  11810.       }
  11811.     }
  11812.   }
  11813.   fprintf(stderr, "%10d bytes for macro hash table buckets (statically allocated)\n",
  11814.            sizeof(hashtab));
  11815.   fprintf(stderr, "%10d bytes for %d macro hashnode's (%d bytes each)\n",
  11816.                    n_elements * sizeof(struct hashnode),
  11817.                    n_elements, sizeof(struct hashnode));
  11818.   fprintf(stderr, "%10d bytes for %d macro definitions's (%d bytes each)\n",
  11819.                    n_elements * sizeof(struct definition),
  11820.                    n_elements, sizeof(struct definition));
  11821.   fprintf(stderr, "%10d bytes for %d macro expansion strings\n",
  11822.                    n_expansion_strings, n_string_count);
  11823.   fprintf(stderr, "%10d bytes for %d other hashnode's (%d bytes each)\n",
  11824.                    n_other * sizeof(struct hashnode),
  11825.                    n_other, sizeof(struct hashnode));
  11826.   return;
  11827. }
  11828.  
  11829. static void
  11830. enable_macros_from_header(entry)
  11831.    struct file_name_list *entry;
  11832. {
  11833.    int i;
  11834.  
  11835.    if (entry->macro_list) {
  11836.       /* list may not yet be filled in */
  11837.       for (i = 0; i < entry->macro_count; i++) {
  11838.          entry->macro_list[i]->defined_within_module = 1;
  11839.       }
  11840.    }
  11841. }
  11842.  
  11843. static void
  11844. clear_import()
  11845. {
  11846.   struct import_file *i;
  11847.   int h;
  11848.  
  11849.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  11850.     i = import_hash_table[h];
  11851.     while (i) {
  11852.       i->visited_within_module = 0;
  11853.       /* dump_file_name_list(i->name); */
  11854.       i = i->next;
  11855.     }
  11856.   }
  11857. }
  11858.  
  11859. static void
  11860. really_delete_macro(hp)
  11861. HASHNODE * hp;
  11862. {
  11863.    DEFINITION *d = hp->value.defn;
  11864.    struct reflist *ap, *nextap;
  11865.  
  11866.    for (ap = d->pattern; ap != NULL; ap = nextap) {
  11867.       nextap = ap->next;
  11868.       free (ap);
  11869.    }
  11870.    if ((d->nargs > 0) && d->args.argnames)
  11871.       free (d->args.argnames);
  11872.  
  11873.    free (d);
  11874.    free (hp);
  11875. }
  11876.  
  11877. static void
  11878. clear_all_macros ()
  11879. {
  11880.   int bucket;
  11881.   register HASHNODE *hp, *hp_next;
  11882.  
  11883.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  11884.  
  11885.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  11886.        if (hp->type == T_MACRO) {
  11887.         /* possible bug...are we disabling command line stuff? Probably not. */
  11888.     hp->defined_within_module = 0;
  11889.         if (hp->owner && hp->owner->macro_count) {
  11890.           if (hp->owner->macro_index < hp->owner->macro_count) { 
  11891.             /* not done filling in the list */
  11892.             if (!hp->owner->macro_list) {
  11893.               int nbytes = hp->owner->macro_count * sizeof ( HASHNODE * );
  11894.               hp->owner->macro_list = (HASHNODE **) xmalloc ( nbytes );
  11895.               bzero(hp->owner->macro_list, nbytes);
  11896.             } 
  11897.             /* stash in the next empty slot */
  11898.             hp->owner->macro_list[hp->owner->macro_index++] = hp;
  11899.           } 
  11900.           /* else, header has been filled (or it has no macros) */
  11901.         } else { 
  11902.           /* defined on the command line (not a header) */
  11903.         }
  11904.       }
  11905.     }
  11906.   }
  11907.  
  11908.   /* free macros from delete_macro (usually due to #undef) */
  11909.   for (hp = free_undefined_macros; hp; hp = hp_next) {
  11910.      hp_next = hp->next;
  11911.      really_delete_macro (hp);
  11912.   }
  11913. #if 0
  11914.   /* free macros from delete_macro (usually due to #undef) */
  11915.   for (hp = installed_builtins; hp; hp = hp_next) {
  11916.      hp_next = hp->next;
  11917.      really_delete_macro (hp);
  11918.   }
  11919. #endif  
  11920. }
  11921.  
  11922. /* Pre-C-Preprocessor to translate RTF (Rich Text Format) Level 0
  11923.    to plain ASCII before normal preprocessing.  This removes all font, color,
  11924.    and help links from the text, but leaves the actual contents unchanged.
  11925.  
  11926.    Using an extra pass through the buffer takes a little extra time,
  11927.    but Rich Source Code is rare and a separate, shared routine
  11928.    can be used.   */
  11929.  
  11930. #define RTF_HEADER     "{\\rtf0\\ansi"
  11931. #define RTF_HEADER_LEN 11
  11932.  
  11933. static void
  11934. buf_convert_rtf (buf)
  11935.      FILE_BUF *buf;
  11936. {
  11937.   int asciiTextLen;
  11938.  
  11939.   if (buf->buf 
  11940.       && buf->length > RTF_HEADER_LEN 
  11941.       && !strncmp (buf->buf, RTF_HEADER, RTF_HEADER_LEN)) 
  11942.     {
  11943.       const char * asciiText = rtf_to_ascii(buf->buf, buf->length, &asciiTextLen);
  11944.       if (asciiText) 
  11945.     {
  11946.       /* FIXME: deallocate buf->buf */
  11947.       buf->buf = buf->bufp = (char *) asciiText;
  11948.       buf->length = asciiTextLen;
  11949.     }
  11950.     }
  11951. }
  11952.  
  11953.  
  11954. /* Process rtf text. RTFTEXTLEN bytes are processed from
  11955.    RTFTEXTPTR.  The output is ASCIITEXTLEN bytes in the same buffer. */
  11956.  
  11957. #define ASCII_TERMINATOR '\0' 
  11958.  
  11959. static const char *
  11960. rtf_to_ascii (const char *rtfTextPtr, int rtfTextLen, int * asciiTextLen) 
  11961. {
  11962.   char * asciiTextBuf = (char *) xmalloc(rtfTextLen);
  11963.   char * asciiTextPtr = asciiTextBuf;
  11964.   const char *endRtfPtr = rtfTextPtr + rtfTextLen;
  11965.  
  11966.   if (!asciiTextBuf)
  11967.     return NULL;
  11968.   
  11969.   while ( rtfTextPtr != endRtfPtr ) {
  11970.     switch( *rtfTextPtr ) {
  11971.     case '\n':
  11972.       rtfTextPtr++;
  11973.       break;
  11974.     case '}':
  11975.       if ( *++rtfTextPtr == '\n' ) {
  11976.     rtfTextPtr++;
  11977.     if (*rtfTextPtr == '‹') rtfTextPtr++;
  11978.       }         
  11979.       break;
  11980.     case '{':
  11981.       if ( !strncmp( rtfTextPtr, "{\\stylesheet", 12 ) 
  11982.       || !strncmp( rtfTextPtr, "{\\fonttbl", 9 ) 
  11983.       || !strncmp( rtfTextPtr, "{\\colortbl", 10 ) 
  11984.       || !strncmp( rtfTextPtr, "{\\NeXTHelp", 10 )
  11985.       || !strncmp( rtfTextPtr, "{{\\NeXTHelp", 11 ) ) {
  11986.     int depth = 1;
  11987.     while ( depth > 0 ) {
  11988.       switch ( *++rtfTextPtr ) {
  11989.       case '{': depth++; break;
  11990.       case '}': depth--; break;
  11991.       }
  11992.     }
  11993.       }
  11994.       else
  11995.     rtfTextPtr++;
  11996.       break;
  11997.     case '\\':
  11998.       rtfTextPtr++;
  11999.       if (*rtfTextPtr == '{'  
  12000.       || *rtfTextPtr == '}'  
  12001.       ||  *rtfTextPtr == '\\'  
  12002.       ||  *rtfTextPtr == '\n' ) {
  12003.     *asciiTextPtr++ = *rtfTextPtr++;
  12004.       } else if ( *rtfTextPtr == '\'' ) {
  12005.     int v1, v2, value;
  12006.     rtfTextPtr++;
  12007.     v1 = isdigit( *rtfTextPtr ) ? (*rtfTextPtr - '0') 
  12008.       : ((*rtfTextPtr - 'a') + 10);
  12009.     rtfTextPtr++;
  12010.     v2 = isdigit( *rtfTextPtr ) ? (*rtfTextPtr - '0') 
  12011.       : ((*rtfTextPtr - 'a') + 10);
  12012.     value = (v1 << 4) | (v2);
  12013.     *asciiTextPtr++ = value;
  12014.     rtfTextPtr++;
  12015.       } else {
  12016.     while ( isalpha( *rtfTextPtr ) ) rtfTextPtr++;
  12017.     if (*rtfTextPtr == '-') rtfTextPtr++;
  12018.     while ( isdigit( *rtfTextPtr ) ) rtfTextPtr++;
  12019.     if ( *rtfTextPtr == ' ' ) rtfTextPtr++;
  12020.     else while (*rtfTextPtr == ' ') rtfTextPtr++;
  12021.       }
  12022.       break;
  12023.     default:
  12024.       *asciiTextPtr++ = *rtfTextPtr++;
  12025.     }       
  12026.   }
  12027.   *asciiTextPtr = ASCII_TERMINATOR;
  12028.   *asciiTextLen = (int)asciiTextPtr - (int)asciiTextBuf;
  12029.   return asciiTextBuf;
  12030. }
  12031.  
  12032.  
  12033. /* Returns last known modifiy time for FILENAME */
  12034.  
  12035. time_t file_modtime(const char * filename) {
  12036.   int hashval = import_hash (filename);
  12037.  
  12038.   /* Attempt to find file in list of already included files */
  12039.   struct import_file *i = import_hash_table[hashval];
  12040.  
  12041.   while (i) {
  12042.      if (!strcmp (filename, i->name->fname)) {
  12043.         return i->modtime;
  12044.      }
  12045.      i = i->next;
  12046.   }
  12047.   return 0;
  12048. }
  12049.  
  12050. static void handle_file_modification(const char * filename) {
  12051.   struct file_name_list *ptr, *next_ptr, *prev_ptr, *version, *next_version;
  12052.   int hashval = import_hash (filename);
  12053.   int multiple_versions = 0, removed_from_hash = 0, removed_from_cache = 0;
  12054.   struct import_file *prev = NULL;
  12055.   struct import_file *i = import_hash_table[hashval];
  12056.   while (i) {
  12057.      if (!strcmp (filename, i->name->fname)) {
  12058.         if (prev)
  12059.            prev->next = i->next;
  12060.         else
  12061.            import_hash_table[hashval] = i->next;
  12062.         removed_from_hash = 1;
  12063.      }
  12064.      prev = i;
  12065.      i = i->next;
  12066.   }
  12067.   if (!removed_from_hash) {
  12068.      const char * basefilename = strrchr(filename,'/');
  12069.      if (basefilename) {
  12070.         // Look for other files by the same name
  12071.         for (hashval = 0; hashval < IMPORT_HASH_SIZE; hashval++) {
  12072.        prev = NULL;
  12073.            i = import_hash_table[hashval];
  12074.            while (i) {
  12075.               const char * baseiname = strrchr(i->name->fname,'/');
  12076.               if (!strcmp (basefilename, baseiname)) {
  12077.                  if (prev)
  12078.                     prev->next = i->next;
  12079.                  else
  12080.                     import_hash_table[hashval] = i->next;
  12081.                  fprintf(stderr,"...punted %s\n",i->name->fname);
  12082.               }
  12083.               prev = i;
  12084.               i = i->next;
  12085.            }
  12086.         }
  12087.      } else {
  12088.         fprintf(stderr,"Cpp Internal Error: could not remove file '%s' from import hash table.  Looks like a bad file name!!\n", filename);
  12089.      }
  12090.   }
  12091.  
  12092.  
  12093.   /* This looks through the entire header cache for copies of this header.
  12094.      It's probably more efficient to terminate the search after finding the
  12095.      header being modified, but the conservative approach is more likely
  12096.      to be correct. */
  12097.   
  12098.   prev_ptr = NULL;
  12099.   for (ptr = next_ptr = all_include_files; ptr; ptr = next_ptr) {
  12100.      next_ptr = ptr->next;
  12101.      if (ptr->fname && !strcmp (ptr->fname, filename)) {
  12102.         if (ptr->versions) {
  12103.            multiple_versions = 1;
  12104.            for (version = next_version = ptr->versions;
  12105.                 version;
  12106.                 version = next_version) {
  12107.               next_version = version->next;
  12108.               free(version);
  12109.            }
  12110.         }
  12111.         if (prev_ptr)
  12112.            prev_ptr->next = next_ptr;
  12113.         else
  12114.            all_include_files = next_ptr;
  12115.         free(ptr->fname);
  12116.         free(ptr);
  12117.         removed_from_cache = 1;
  12118.      }
  12119.      prev_ptr = ptr;
  12120.   }
  12121.  
  12122. #ifdef DEBUG
  12123.   if (!removed_from_cache)
  12124.      fprintf(stderr,"Warning: could not remove file '%s' from import cache.\n", filename);
  12125. #endif
  12126.   
  12127.   if (multiple_versions)
  12128.      fprintf(stderr,"Removed all versions of %s from header cache\n", filename);
  12129.   else
  12130.      fprintf(stderr,"Removed %s from header cache\n", filename);
  12131.   fflush(stderr);
  12132. }
  12133.  
  12134. static void init_output_file_buf(FILE_BUF * outbuf)
  12135. {
  12136.   outbuf->buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  12137.   outbuf->length = OUTBUF_SIZE;
  12138.   outbuf->bufp = outbuf->output_written_to = outbuf->buf;
  12139.   bzero (outbuf->bufp, outbuf->length);
  12140.   outbuf->lineno = 0;
  12141. }
  12142.  
  12143.  
  12144. static int sysUsr_startTime;
  12145. static int sysUsr_finishTime;
  12146.   
  12147. static struct timeval real_startTime;
  12148. static struct timeval real_finishTime;
  12149.   
  12150.   /* Return time used so far, in microseconds.  */
  12151. static int gettime ()
  12152. {
  12153.    struct rusage rusage;
  12154.  
  12155.    getrusage (0, &rusage);
  12156.    return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  12157.         + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  12158. }
  12159.  
  12160. static void start() 
  12161. {
  12162.     sysUsr_startTime = gettime();
  12163.     gettimeofday(&real_startTime, 0);
  12164. }
  12165.  
  12166. static void stop() 
  12167. {
  12168.     sysUsr_finishTime = gettime();
  12169.     gettimeofday(&real_finishTime, 0);
  12170. }
  12171.  
  12172. static void display(FILE * strm, int addNewLine)
  12173. {
  12174.     real_finishTime.tv_sec -= real_startTime.tv_sec;
  12175.     real_finishTime.tv_usec -= real_startTime.tv_usec;
  12176.     if (real_finishTime.tv_usec < 0)
  12177.       real_finishTime.tv_sec--, real_finishTime.tv_usec += 1000000;
  12178.       
  12179.     fprintf(strm, "%3d.%03d (sys+usr) %3d.%01d (real)%s",
  12180.          (sysUsr_finishTime-sysUsr_startTime) / 1000000, 
  12181.          (sysUsr_finishTime-sysUsr_startTime) % 1000000, 
  12182.          real_finishTime.tv_sec, 
  12183.          real_finishTime.tv_usec/100000,
  12184.          addNewLine ? "\n" : "");
  12185. }
  12186. #endif
  12187.