home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / cccp-pdo.c < prev    next >
C/C++ Source or Header  |  1997-01-21  |  336KB  |  12,194 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. #ifdef hpux
  88. #include <sys/times.h>
  89. #endif
  90.  
  91. #ifndef MALLOC_INPUT_BUFFERS
  92. #include <mach/mach.h>
  93. #endif
  94.  
  95. #ifndef VMS
  96. #if ! defined(USG) || defined(SVR4)
  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. #ifndef hpux
  150. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  151. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  152. #endif
  153.  
  154. /* Find the largest host integer type and set its size and type.  */
  155.  
  156. #ifndef HOST_BITS_PER_WIDE_INT
  157.  
  158. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  159. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  160. #define HOST_WIDE_INT long
  161. #else
  162. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  163. #define HOST_WIDE_INT int
  164. #endif
  165.  
  166. #endif
  167.  
  168. #ifndef S_ISREG
  169. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  170. #endif
  171.  
  172. #ifndef S_ISDIR
  173. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  174. #endif
  175.  
  176. /* Define a generic NULL if one hasn't already been defined.  */
  177.  
  178. #ifndef NULL
  179. #define NULL 0
  180. #endif
  181.  
  182. #ifndef GENERIC_PTR
  183. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  184. #define GENERIC_PTR void *
  185. #else
  186. #define GENERIC_PTR char *
  187. #endif
  188. #endif
  189.  
  190. #ifndef NULL_PTR
  191. #define NULL_PTR ((GENERIC_PTR)0)
  192. #endif
  193.  
  194. #ifndef INCLUDE_LEN_FUDGE
  195. #define INCLUDE_LEN_FUDGE 0
  196. #endif
  197.  
  198. /* Forward declarations.  */
  199.  
  200. char *xmalloc ();
  201. void error ();
  202. void warning ();
  203.  
  204. /* External declarations.  */
  205.  
  206. extern char *getenv ();
  207. extern FILE *fdopen ();
  208. static char *version_string = "2.6.0";
  209. extern struct tm *localtime ();
  210. extern int sys_nerr;
  211. #if defined(bsd4_4) || defined(__NetBSD__)
  212. extern const char *const sys_errlist[];
  213. #else
  214. extern char *sys_errlist[];
  215. #endif
  216. extern int parse_escape ();
  217.  
  218. #ifndef errno
  219. extern int errno;
  220. #endif
  221.  
  222. /* Forward declarations.  */
  223.  
  224. struct directive;
  225. struct file_buf;
  226. struct arglist;
  227. struct argdata;
  228.  
  229. #if defined(USG) || defined(VMS)
  230. #ifndef BSTRING
  231. void bcopy ();
  232. void bzero ();
  233. int bcmp ();
  234. #endif
  235. #endif
  236.  
  237. /* These functions are declared to return int instead of void since they
  238.    are going to be placed in a table and some old compilers have trouble with
  239.    pointers to functions returning void.  */
  240.  
  241. static int do_define ();
  242. static int do_line ();
  243. static int do_include ();
  244. static int do_undef ();
  245. static int do_error ();
  246. static int do_pragma ();
  247. static int do_ident ();
  248. static int do_if ();
  249. static int do_xifdef ();
  250. static int do_else ();
  251. static int do_elif ();
  252. static int do_endif ();
  253. static int do_sccs ();
  254. static int do_once ();
  255. static int do_assert ();
  256. static int do_unassert ();
  257. static int do_warning ();
  258.  
  259. static struct import_file *add_import ();
  260. static void append_include_chain ();
  261. #ifdef NEXT_FRAMEWORKS
  262. static void append_framework_chain ();
  263. #endif
  264. static void deps_output ();
  265. static void make_undef ();
  266. static void make_definition ();
  267. static void make_assertion ();
  268. static void path_include ();
  269. static void initialize_builtins ();
  270. static void initialize_char_syntax ();
  271. static void dump_arg_n ();
  272. static void dump_defn_1 ();
  273. static void delete_macro ();
  274. static void trigraph_pcp ();
  275. static void rescan ();
  276. static void finclude ();
  277. static void validate_else ();
  278. static int comp_def_part ();
  279. static void error_from_errno ();
  280. static void error_with_line ();
  281. void pedwarn ();
  282. void pedwarn_with_line ();
  283. static void pedwarn_with_file_and_line ();
  284. static void fatal ();
  285. void fancy_abort ();
  286. static void pfatal_with_name ();
  287. static void perror_with_name ();
  288. static void pipe_closed ();
  289. static void print_containing_files ();
  290. static int lookup_import ();
  291. static int redundant_include_p ();
  292. static is_system_include ();
  293. static struct file_name_map *read_name_map ();
  294. static char *read_filename_string ();
  295. static int open_include_file ();
  296. static int check_preconditions ();
  297. static void pcfinclude ();
  298. static void pcstring_used ();
  299. static void write_output ();
  300. #ifdef NEXT_CPP_SERVER
  301. static int write_header_output ();
  302. static time_t file_modtime ();
  303. static void handle_file_modification();
  304. #endif
  305. static int check_macro_name ();
  306. static int compare_defs ();
  307. static int compare_token_lists ();
  308. static int eval_if_expression ();
  309. static int discard_comments ();
  310. static int change_newlines ();
  311. static int line_for_error ();
  312. static int hashf ();
  313. static int file_size_and_mode ();
  314.  
  315. static struct arglist *read_token_list ();
  316. static void free_token_list ();
  317.  
  318. static struct hashnode *install ();
  319. struct hashnode *lookup ();
  320.  
  321. static struct assertion_hashnode *assertion_install ();
  322. static struct assertion_hashnode *assertion_lookup ();
  323.  
  324. static char *xrealloc ();
  325. static char *xcalloc ();
  326. static char *savestring ();
  327.  
  328. static void delete_assertion ();
  329. static void macroexpand ();
  330. static void dump_all_macros ();
  331. static void conditional_skip ();
  332. static void skip_if_group ();
  333. static void output_line_command ();
  334.  
  335. #ifdef NEXT_SEMANTICS
  336. /* convert rtf */
  337. static void buf_convert_rtf ();
  338. static const char * rtf_to_ascii ();
  339. #endif
  340.  
  341. /* Last arg to output_line_command.  */
  342. #ifdef NEXT_CPP_SERVER
  343. enum file_change_code {same_file, enter_file, leave_file,
  344.                        enter_expansion, leave_expansion};
  345. #else
  346. enum file_change_code {same_file, enter_file, leave_file};
  347. #endif
  348.  
  349. static int grow_outbuf ();
  350. static int handle_directive ();
  351. static void memory_full ();
  352.  
  353. static U_CHAR *macarg1 ();
  354. static char *macarg ();
  355.  
  356. static U_CHAR *skip_to_end_of_comment ();
  357. static U_CHAR *skip_quoted_string ();
  358. static U_CHAR *skip_paren_group ();
  359. static char *quote_string ();
  360.  
  361. static char *check_precompiled ();
  362. /* static struct macrodef create_definition ();    [moved below] */
  363. static void dump_single_macro ();
  364. static void output_dots ();
  365.  
  366. #ifndef FAILURE_EXIT_CODE
  367. #define FAILURE_EXIT_CODE 33    /* gnu cc command understands this */
  368. #endif
  369.  
  370. #ifndef SUCCESS_EXIT_CODE
  371. #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
  372. #endif
  373.  
  374. /* Name under which this program was invoked.  */
  375.  
  376. static char *progname;
  377.  
  378. /* Nonzero means use extra default include directories for C++.  */
  379.  
  380. static int cplusplus;
  381.  
  382. /* Nonzero means handle cplusplus style comments */
  383.  
  384. static int cplusplus_comments;
  385.  
  386. /* Nonzero means handle #import, for objective C.  */
  387.  
  388. static int objc;
  389.  
  390. /* Nonzero means this is an assembly file, and allow
  391.    unknown directives, which could be comments.  */
  392.  
  393. static int lang_asm;
  394.  
  395. /* Current maximum length of directory names in the search path
  396.    for include files.  (Altered as we get more of them.)  */
  397.  
  398. static int max_include_len;
  399.  
  400. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  401.  
  402. static int for_lint = 0;
  403.  
  404. /* Nonzero means copy comments into the output file.  */
  405.  
  406. static int put_out_comments = 0;
  407.  
  408. /* Nonzero means don't process the ANSI trigraph sequences.  */
  409.  
  410. static int no_trigraphs = 0;
  411.  
  412. /* Nonzero means print the names of included files rather than
  413.    the preprocessed output.  1 means just the #include "...",
  414.    2 means #include <...> as well.  */
  415.  
  416. static int print_deps = 0;
  417.  
  418. /* Nonzero if missing .h files in -M output are assumed to be generated
  419.    files and not errors.  */
  420.  
  421. static int print_deps_missing_files = 0;
  422.  
  423. /* Nonzero means print names of header files (-H).  */
  424.  
  425. static int print_include_names = 0;
  426.  
  427. /* Nonzero means don't output line number information.  */
  428.  
  429. static int no_line_commands;
  430.  
  431. /* dump_only means inhibit output of the preprocessed text
  432.              and instead output the definitions of all user-defined
  433.              macros in a form suitable for use as input to cccp.
  434.    dump_names means pass #define and the macro name through to output.
  435.    dump_definitions means pass the whole definition (plus #define) through
  436. */
  437.  
  438. static enum {dump_none, dump_only, dump_names, dump_definitions}
  439.      dump_macros = dump_none;
  440.  
  441. /* Nonzero means pass all #define and #undef directives which we actually
  442.    process through to the output stream.  This feature is used primarily
  443.    to allow cc1 to record the #defines and #undefs for the sake of
  444.    debuggers which understand about preprocessor macros, but it may
  445.    also be useful with -E to figure out how symbols are defined, and
  446.    where they are defined.  */
  447. static int debug_output = 0;
  448.  
  449. /* Nonzero indicates special processing used by the pcp program.  The
  450.    special effects of this mode are: 
  451.      
  452.      Inhibit all macro expansion, except those inside #if directives.
  453.  
  454.      Process #define directives normally, and output their contents 
  455.      to the output file.
  456.  
  457.      Output preconditions to pcp_outfile indicating all the relevant
  458.      preconditions for use of this file in a later cpp run.
  459. */
  460. static FILE *pcp_outfile;
  461.  
  462. /* Nonzero means we are inside an IF during a -pcp run.  In this mode
  463.    macro expansion is done, and preconditions are output for all macro
  464.    uses requiring them. */
  465. static int pcp_inside_if;
  466.  
  467. /* Nonzero means never to include precompiled files.
  468.    This is 1 since there's no way now to make precompiled files,
  469.    so it's not worth testing for them.  */
  470. static int no_precomp = 1;
  471.  
  472. /* Nonzero means give all the error messages the ANSI standard requires.  */
  473.  
  474. int pedantic;
  475.  
  476. /* Nonzero means try to make failure to fit ANSI C an error.  */
  477.  
  478. static int pedantic_errors;
  479.  
  480. /* Nonzero means don't print warning messages.  -w.  */
  481.  
  482. static int inhibit_warnings = 0;
  483.  
  484. /* Nonzero means warn if slash-star appears in a comment.  */
  485.  
  486. static int warn_comments;
  487.  
  488. /* Nonzero means warn if a macro argument is (or would be)
  489.    stringified with -traditional.  */
  490.  
  491. static int warn_stringify;
  492.  
  493. /* Nonzero means warn if there are any trigraphs.  */
  494.  
  495. static int warn_trigraphs;
  496.  
  497. /* Nonzero means warn if #import is used.  */
  498.  
  499. #ifdef NEXT_OBJC_RUNTIME
  500. static int warn_import = 0;
  501. #else
  502. static int warn_import = 1;
  503. #endif
  504.  
  505. /* Nonzero means turn warnings into errors.  */
  506.  
  507. static int warnings_are_errors;
  508.  
  509. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  510.  
  511. int traditional;
  512.  
  513. /* Nonzero causes output not to be done,
  514.    but directives such as #define that have side effects
  515.    are still obeyed.  */
  516.  
  517. static int no_output;
  518.  
  519. #ifdef NEXT_SEMANTICS
  520. /* Nonzero causes input not to have rtf codes stripped before 
  521.    further processing. */
  522.  
  523. static int no_rtf = 0;
  524. #endif
  525.  
  526. /* Nonzero means this file was included with a -imacros or -include
  527.    command line and should not be recorded as an include file.  */
  528.  
  529. static int no_record_file;
  530.  
  531. /* Nonzero means that we have finished processing the command line options.
  532.    This flag is used to decide whether or not to issue certain errors
  533.    and/or warnings.  */
  534.  
  535. static int done_initializing = 0;
  536.  
  537. /* Line where a newline was first seen in a string constant.  */
  538.  
  539. static int multiline_string_line = 0;
  540.  
  541. /* I/O buffer structure.
  542.    The `fname' field is nonzero for source files and #include files
  543.    and for the dummy text used for -D and -U.
  544.    It is zero for rescanning results of macro expansion
  545.    and for expanding macro arguments.  */
  546. #define INPUT_STACK_MAX 400
  547. static struct file_buf {
  548.   char *fname;
  549.   /* Filename specified with #line command.  */
  550.   char *nominal_fname;
  551.   /* Record where in the search path this file was found.
  552.      For #include_next.  */
  553.   struct file_name_list *dir;
  554. #ifdef NEXT_CPP_SERVER
  555.   struct file_name_list *file; /* used by segregate_output */
  556.   U_CHAR *beg_of_line;           /* used by mark_expansions */
  557.   U_CHAR *output_written_to;
  558. #endif
  559.   int lineno;
  560.   int length;
  561.   U_CHAR *buf;
  562.   U_CHAR *bufp;
  563.   /* Macro that this level is the expansion of.
  564.      Included so that we can reenable the macro
  565.      at the end of this level.  */
  566.   struct hashnode *macro;
  567.   /* Value of if_stack at start of this file.
  568.      Used to prohibit unmatched #endif (etc) in an include file.  */
  569.   struct if_stack *if_stack;
  570.   /* Object to be freed at end of input at this level.  */
  571.   U_CHAR *free_ptr;
  572.   /* True if this is a header file included using <FILENAME>.  */
  573.   char system_header_p;
  574. } instack[INPUT_STACK_MAX];
  575.  
  576. static int last_error_tick;       /* Incremented each time we print it.  */
  577. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  578.  
  579. /* Current nesting level of input sources.
  580.    `instack[indepth]' is the level currently being read.  */
  581. static int indepth = -1;
  582. #define CHECK_DEPTH(code) \
  583.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  584.     {                                    \
  585.       error_with_line (line_for_error (instack[indepth].lineno),    \
  586.                "macro or `#include' recursion too deep");    \
  587.       code;                                \
  588.     }
  589.  
  590. /* Current depth in #include directives that use <...>.  */
  591. static int system_include_depth = 0;
  592.  
  593. typedef struct file_buf FILE_BUF;
  594.  
  595. /* The output buffer.  Its LENGTH field is the amount of room allocated
  596.    for the buffer, not the number of chars actually present.  To get
  597.    that, subtract outbuf.buf from outbuf.bufp. */
  598.  
  599. #ifdef NEXT_CPP_SERVER
  600. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  601. #else
  602. #define OUTBUF_SIZE 8192  /* initial size of output buffer */
  603. #endif
  604.  
  605. static FILE_BUF outbuf;
  606.  
  607. #ifdef NEXT_CPP_SERVER
  608.  
  609. /* #include <sys/dir.h> */
  610. #include <string.h>
  611. #include <sys/fcntl.h>
  612.  
  613. /* Non-0 means separate the preprocessed module from the headers it imports.
  614.    This is a "poor man's" precompiled headers */
  615. static int segregate_output = 0;
  616.  
  617. /* Non-0 means output line/position markers before/after each expansion.
  618.    Plain old line commands were extended (see output_line_command) */
  619. static int mark_expansions = 0;
  620.  
  621. /* Non-zero means we are reading all further commands from stdin until a 
  622.    "terminate" command is received */
  623. static int serverized = 0;
  624.  
  625. /* Non-zero specifies that the time required to cpp is output.
  626.    Note: clients that turn this on should be prepared to handle the output,
  627.    if it is being parsed. */ 
  628. static int timings = 0;
  629.  
  630. /* Non-zero means we are serverized and have already output the stat
  631.    information for the module we are processing */
  632. static int outbuf_stat_info_recorded = 0;
  633.  
  634. static FILE_BUF sys_header_outbuf; /* shared, segregated buf for headers */
  635. static FILE_BUF user_header_outbuf;
  636.  
  637. static struct hashnode *free_undefined_macros = 0;
  638.  
  639. /* List of all input and output files (now that cpp can take a list of files)
  640.    This is built as files are processed */
  641. static struct file_name_list *all_input_files = 0;
  642. static struct file_name_list *all_output_files = 0;
  643.  
  644. static void add_to_negative_cache ();
  645. static int found_in_negative_cache ();
  646. static void add_version_of_header ();
  647. static int duplicate_version_of_header ();
  648. static int aggressive_skip_if_group ();
  649. static void dump_memory_statistics ();
  650. static void clear_import ();
  651. static void clear_all_macros();
  652. static void enable_macros_from_header();
  653.  
  654. static void init_output_file_buf();
  655.  
  656. /* for debugging */
  657. static void start();
  658. static void stop();
  659. static void display(FILE * strm, int addNewLine);
  660. #endif
  661.  
  662. /* Maintain and search list of included files, for #import.  */
  663.  
  664. #define IMPORT_HASH_SIZE 1403 /* was 31 */
  665.  
  666. struct import_file {
  667. #ifdef NEXT_CPP_SERVER
  668.   struct file_name_list *name;
  669.   int visited_within_module;    /* to support multiple modules */
  670.   time_t modtime;
  671. #else
  672.   char *name;
  673. #endif
  674.   ino_t inode;
  675.   dev_t dev;
  676.   struct import_file *next;
  677. };
  678.  
  679. /* Hash table of files already included with #include or #import.  */
  680.  
  681. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  682. static int import_hash ();
  683.  
  684. /* Grow output buffer OBUF points at
  685.    so it can hold at least NEEDED more chars.  */
  686.  
  687. #define check_expand(OBUF, NEEDED)  \
  688.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  689.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  690.  
  691. struct file_name_list
  692.   {
  693.     struct file_name_list *next;
  694.     char *fname;
  695.     /* If the following is nonzero, it is a macro name.
  696.        Don't include the file again if that macro is defined.  */
  697.     U_CHAR *control_macro;
  698.     /* If the following is nonzero, it is a C-language system include
  699.        directory.  */
  700.     int c_system_include_path;
  701.     /* Mapping of file names for this directory.  */
  702.     struct file_name_map *name_map;
  703.     /* Non-zero if name_map is valid.  */
  704.     int got_name_map;
  705. #ifdef NEXT_CPP_SERVER
  706.     /* If we are processing a list of modules, cache headers 
  707.        Normally, this buffer is freed in finclude  */
  708.     int length;
  709.     U_CHAR *buf;
  710.     /* If we are segregating headers, mark if already output */
  711.     int header_output;
  712.     int has_explicit_context_dependency;
  713.     int ever_included;         /* 0 means always #imported */
  714.     /* Keep a list of macros that are a part of this header */
  715.     int macro_count;
  716.     int macro_index;
  717.     struct hashnode **macro_list;
  718.     /* Keep various markers into the input buffer (to enable interesting
  719.        optimizations in skip_if_group when preprocessing several files). */
  720.     U_CHAR *position_after_last_include; 
  721.     U_CHAR *position_of_first_ifndef; /* if control_macro != 0 */
  722.     U_CHAR *position_of_last_endif;   /* if control_macro != 0 */
  723.     /* info to exclude duplicates in the output buffer (for perverse cases
  724.        like stdtypes.h...very rare). Offsets into the output buffer are
  725.        stored relative, since the buffer is often being realloced. */
  726.     unsigned int relative_position_of_file_start; /* into a header_outbuf */
  727.     unsigned int file_length; /* in bytes */
  728.     struct file_name_list *versions;  /* diff. mutations of the same file */
  729. #endif
  730.   };
  731.  
  732. /* #include "file" looks in source file dir, then stack. */
  733. /* #include <file> just looks in the stack. */
  734. /* cplusplus = 0 -> C.  cplusplus = 1 -> C++. cplusplus = 2 -> nothing */
  735. /* -I directories are added to the end, then the defaults are added. */
  736. /* The */
  737. static struct default_include {
  738.   char *fname;            /* The name of the directory.  */
  739.   int cplusplus;        /* Only look here if we're compiling C++.  */
  740.   int cxx_aware;        /* Includes in this directory don't need to
  741.                    be wrapped in extern "C" when compiling
  742.                    C++.  */
  743. } include_defaults_array[]
  744. #ifdef INCLUDE_DEFAULTS
  745.   = INCLUDE_DEFAULTS;
  746. #else
  747.   = {
  748.     /* Pick up GNU C++ specific include files.  */
  749.     { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
  750. #ifdef CROSS_COMPILE
  751.     /* This is the dir for fixincludes.  Put it just before
  752.        the files that we fix.  */
  753.     { GCC_INCLUDE_DIR, 0, 0 },
  754.     /* For cross-compilation, this dir name is generated
  755.        automatically in Makefile.in.  */
  756.     { CROSS_INCLUDE_DIR, 0, 0 },
  757.     /* This is another place that the target system's headers might be.  */
  758.     { TOOL_INCLUDE_DIR, 0, 1 },
  759. #else /* not CROSS_COMPILE */
  760.     /* This should be /usr/local/include and should come before
  761.        the fixincludes-fixed header files.  */
  762.     { LOCAL_INCLUDE_DIR, 0, 1 },
  763.     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
  764.        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
  765.     { TOOL_INCLUDE_DIR, 0, 1 },
  766.     /* This is the dir for fixincludes.  Put it just before
  767.        the files that we fix.  */
  768.     { GCC_INCLUDE_DIR, 0, 0 },
  769.     /* Some systems have an extra dir of include files.  */
  770. #ifdef SYSTEM_INCLUDE_DIR
  771.     { SYSTEM_INCLUDE_DIR, 0, 0 },
  772. #endif
  773.     { STANDARD_INCLUDE_DIR, 0, 0 },
  774. #endif /* not CROSS_COMPILE */
  775.     { 0, 0, 0 }
  776.     };
  777. #endif /* no INCLUDE_DEFAULTS */
  778. #ifdef NEXT_FRAMEWORKS
  779. static struct default_include framework_defaults_array [] = {
  780.     {"/LocalLibrary/Frameworks", 0, 0},
  781.     {"/NextLibrary/Frameworks", 0, 0}, 
  782.     {NULL, 0, 0}
  783.  };
  784.  
  785. static struct default_include *framework_defaults=framework_defaults_array;
  786. static struct file_name_list *framework = 0;
  787.  
  788. struct framework_header {char * dirName; int dirNameLen; };
  789. static struct framework_header framework_header_dirs[] = {
  790.    { "PrivateHeaders", 14 },
  791.    { "Headers", 7 },
  792.    { NULL, 0 }
  793. };
  794. #endif
  795. /* The code looks at the defaults through this pointer, rather than through
  796.    the constant structure above.  This pointer gets changed if an environment
  797.    variable specifies other defaults.  */
  798. static struct default_include *include_defaults = include_defaults_array;
  799.  
  800. static struct file_name_list *include = 0;    /* First dir to search */
  801.  
  802.     /* First dir to search for <file> */
  803. /* This is the first element to use for #include <...>.
  804.    If it is 0, use the entire chain for such includes.  */
  805. static struct file_name_list *first_bracket_include = 0;
  806. #ifdef NEXT_FRAMEWORKS
  807. static struct file_name_list *first_bracket_framework = 0;
  808. #endif
  809. /* This is the first element in the chain that corresponds to
  810.    a directory of system header files.  */
  811. static struct file_name_list *first_system_include = 0;
  812. static struct file_name_list *last_include = 0;    /* Last in chain */
  813. #ifdef NEXT_FRAMEWORKS
  814. static struct file_name_list *last_framework = 0; /* Last in chain */
  815. #endif
  816. /* Chain of include directories to put at the end of the other chain.  */
  817. static struct file_name_list *after_include = 0;
  818. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  819.  
  820. /* Chain to put at the start of the system include files.  */
  821. static struct file_name_list *before_system = 0;
  822. static struct file_name_list *last_before_system = 0;    /* Last in chain */
  823.  
  824. /* List of included files that contained #pragma once.  */
  825. static struct file_name_list *dont_repeat_files = 0;
  826.  
  827. /* List of other included files.
  828.    If ->control_macro if nonzero, the file had a #ifndef
  829.    around the entire contents, and ->control_macro gives the macro name.  */
  830. static struct file_name_list *all_include_files = 0;
  831.  
  832. /* Directory prefix that should replace `/usr' in the standard
  833.    include file directories.  */
  834. static char *include_prefix;
  835.  
  836. /* Global list of strings read in from precompiled files.  This list
  837.    is kept in the order the strings are read in, with new strings being
  838.    added at the end through stringlist_tailp.  We use this list to output
  839.    the strings at the end of the run. 
  840. */
  841. static STRINGDEF *stringlist;
  842. static STRINGDEF **stringlist_tailp = &stringlist;
  843.  
  844.  
  845. /* Structure returned by create_definition */
  846. typedef struct macrodef MACRODEF;
  847. struct macrodef
  848. {
  849.   struct definition *defn;
  850.   U_CHAR *symnam;
  851.   int symlen;
  852. };
  853.  
  854. static struct macrodef create_definition ();
  855.  
  856.  
  857. /* Structure allocated for every #define.  For a simple replacement
  858.    such as
  859.        #define foo bar ,
  860.    nargs = -1, the `pattern' list is null, and the expansion is just
  861.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  862.    e.g.,
  863.        #define getchar() getc (stdin) .
  864.    When there are args, the expansion is the replacement text with the
  865.    args squashed out, and the reflist is a list describing how to
  866.    build the output from the input: e.g., "3 chars, then the 1st arg,
  867.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  868.    The chars here come from the expansion.  Whatever is left of the
  869.    expansion after the last arg-occurrence is copied after that arg.
  870.    Note that the reflist can be arbitrarily long---
  871.    its length depends on the number of times the arguments appear in
  872.    the replacement text, not how many args there are.  Example:
  873.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  874.    pattern list
  875.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  876.    where (x, y) means (nchars, argno). */
  877.  
  878. typedef struct definition DEFINITION;
  879. struct definition {
  880.   int nargs;
  881.   int length;            /* length of expansion string */
  882.   int predefined;        /* True if the macro was builtin or */
  883.                 /* came from the command line */
  884.   U_CHAR *expansion;
  885.   int line;            /* Line number of definition */
  886.   char *file;            /* File of definition */
  887.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  888.   struct reflist {
  889.     struct reflist *next;
  890.     char stringify;        /* nonzero if this arg was preceded by a
  891.                    # operator. */
  892.     char raw_before;        /* Nonzero if a ## operator before arg. */
  893.     char raw_after;        /* Nonzero if a ## operator after arg. */
  894.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  895.     int nchars;            /* Number of literal chars to copy before
  896.                    this arg occurrence.  */
  897.     int argno;            /* Number of arg to substitute (origin-0) */
  898.   } *pattern;
  899.   union {
  900.     /* Names of macro args, concatenated in reverse order
  901.        with comma-space between them.
  902.        The only use of this is that we warn on redefinition
  903.        if this differs between the old and new definitions.  */
  904.     U_CHAR *argnames;
  905.   } args;
  906. };
  907.  
  908. /* different kinds of things that can appear in the value field
  909.    of a hash node.  Actually, this may be useless now. */
  910. union hashval {
  911.   int ival;
  912.   char *cpval;
  913.   DEFINITION *defn;
  914.   KEYDEF *keydef;
  915. };
  916.  
  917. /*
  918.  * special extension string that can be added to the last macro argument to 
  919.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  920.  *         #define wow(a, b...)        process (b, a, b)
  921.  *        { wow (1, 2, 3); }    ->    { process (2, 3, 1, 2, 3); }
  922.  *        { wow (one, two); }    ->    { process (two, one, two); }
  923.  * if this "rest_arg" is used with the concat token '##' and if it is not
  924.  * supplied then the token attached to with ## will not be outputted.  Ex:
  925.  *         #define wow (a, b...)        process (b ## , a, ## b)
  926.  *        { wow (1, 2); }        ->    { process (2, 1, 2); }
  927.  *        { wow (one); }        ->    { process (one); {
  928.  */
  929. static char rest_extension[] = "...";
  930. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  931.  
  932. /* The structure of a node in the hash table.  The hash table
  933.    has entries for all tokens defined by #define commands (type T_MACRO),
  934.    plus some special tokens like __LINE__ (these each have their own
  935.    type, and the appropriate code is run when that type of node is seen.
  936.    It does not contain control words like "#define", which are recognized
  937.    by a separate piece of code. */
  938.  
  939. /* different flavors of hash nodes --- also used in keyword table */
  940. enum node_type {
  941.  T_DEFINE = 1,    /* the `#define' keyword */
  942.  T_INCLUDE,    /* the `#include' keyword */
  943.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  944.  T_IMPORT,      /* the `#import' keyword */
  945.  T_IFDEF,    /* the `#ifdef' keyword */
  946.  T_IFNDEF,    /* the `#ifndef' keyword */
  947.  T_IF,        /* the `#if' keyword */
  948.  T_ELSE,    /* `#else' */
  949.  T_PRAGMA,    /* `#pragma' */
  950.  T_ELIF,    /* `#elif' */
  951.  T_UNDEF,    /* `#undef' */
  952.  T_LINE,    /* `#line' */
  953.  T_ERROR,    /* `#error' */
  954.  T_WARNING,    /* `#warning' */
  955.  T_ENDIF,    /* `#endif' */
  956.  T_SCCS,    /* `#sccs', used on system V.  */
  957.  T_IDENT,    /* `#ident', used on system V.  */
  958.  T_ASSERT,    /* `#assert', taken from system V.  */
  959.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  960.  T_SPECLINE,    /* special symbol `__LINE__' */
  961.  T_DATE,    /* `__DATE__' */
  962.  T_FILE,    /* `__FILE__' */
  963.  T_BASE_FILE,    /* `__BASE_FILE__' */
  964.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  965.  T_VERSION,    /* `__VERSION__' */
  966.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  967.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  968.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  969.  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
  970.  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
  971.  T_TIME,    /* `__TIME__' */
  972.  T_CONST,    /* Constant value, used by `__STDC__' */
  973.  T_MACRO,    /* macro defined by `#define' */
  974.  T_DISABLED,    /* macro temporarily turned off for rescan */
  975.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  976.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  977.  T_UNUSED    /* Used for something not defined.  */
  978.  };
  979.  
  980. struct hashnode {
  981.   struct hashnode *next;    /* double links for easy deletion */
  982.   struct hashnode *prev;
  983.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  984.                    chain is kept, in case the node is the head
  985.                    of the chain and gets deleted. */
  986.   enum node_type type;        /* type of special token */
  987.   int length;            /* length of token, for quick comparison */
  988.   U_CHAR *name;            /* the actual name */
  989.   union hashval value;        /* pointer to expansion, or whatever */
  990. #ifdef NEXT_CPP_SERVER
  991.   int defined_within_module;    /* is it currently on? (maybe should
  992.                                    just take a bit from the type field) */
  993.   struct file_name_list *owner; /* could have added this to "DEFINITION *",
  994.                                    however decided this was lower risk and
  995.                                    provided better locality (wrt the previous
  996.                                    field. Since hashnodes are primarily used
  997.                                    for macros, the space overhead is not
  998.                                    an issue */
  999. #endif
  1000. };
  1001.  
  1002. typedef struct hashnode HASHNODE;
  1003.  
  1004. /* Some definitions for the hash table.  The hash function MUST be
  1005.    computed as shown in hashf () below.  That is because the rescan
  1006.    loop computes the hash value `on the fly' for most tokens,
  1007.    in order to avoid the overhead of a lot of procedure calls to
  1008.    the hashf () function.  Hashf () only exists for the sake of
  1009.    politeness, for use when speed isn't so important. */
  1010.  
  1011. #define HASHSIZE 1403
  1012. static HASHNODE *hashtab[HASHSIZE];
  1013. #define HASHSTEP(old, c) ((old << 2) + c)
  1014. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  1015.  
  1016. /* Symbols to predefine.  */
  1017.  
  1018. #ifdef CPP_PREDEFINES
  1019. static char *predefs = CPP_PREDEFINES;
  1020. #else
  1021. static char *predefs = "";
  1022. #endif
  1023.  
  1024. /* We let tm.h override the types used here, to handle trivial differences
  1025.    such as the choice of unsigned int or long unsigned int for size_t.
  1026.    When machines start needing nontrivial differences in the size type,
  1027.    it would be best to do something here to figure out automatically
  1028.    from other information what type to use.  */
  1029.  
  1030. /* The string value for __SIZE_TYPE__.  */
  1031.  
  1032. #ifndef SIZE_TYPE
  1033. #define SIZE_TYPE "long unsigned int"
  1034. #endif
  1035.  
  1036. /* The string value for __PTRDIFF_TYPE__.  */
  1037.  
  1038. #ifndef PTRDIFF_TYPE
  1039. #define PTRDIFF_TYPE "long int"
  1040. #endif
  1041.  
  1042. /* The string value for __WCHAR_TYPE__.  */
  1043.  
  1044. #ifndef WCHAR_TYPE
  1045. #define WCHAR_TYPE "int"
  1046. #endif
  1047. char * wchar_type = WCHAR_TYPE;
  1048. #undef WCHAR_TYPE
  1049.  
  1050. /* The string value for __USER_LABEL_PREFIX__ */
  1051.  
  1052. #ifndef USER_LABEL_PREFIX
  1053. #define USER_LABEL_PREFIX ""
  1054. #endif
  1055.  
  1056. /* The string value for __REGISTER_PREFIX__ */
  1057.  
  1058. #ifndef REGISTER_PREFIX
  1059. #define REGISTER_PREFIX ""
  1060. #endif
  1061.  
  1062. /* In the definition of a #assert name, this structure forms
  1063.    a list of the individual values asserted.
  1064.    Each value is itself a list of "tokens".
  1065.    These are strings that are compared by name.  */
  1066.  
  1067. struct tokenlist_list {
  1068.   struct tokenlist_list *next;
  1069.   struct arglist *tokens;
  1070. };
  1071.  
  1072. struct assertion_hashnode {
  1073.   struct assertion_hashnode *next;    /* double links for easy deletion */
  1074.   struct assertion_hashnode *prev;
  1075.   /* also, a back pointer to this node's hash
  1076.      chain is kept, in case the node is the head
  1077.      of the chain and gets deleted. */
  1078.   struct assertion_hashnode **bucket_hdr;
  1079.   int length;            /* length of token, for quick comparison */
  1080.   U_CHAR *name;            /* the actual name */
  1081.   /* List of token-sequences.  */
  1082.   struct tokenlist_list *value;
  1083. };
  1084.  
  1085. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  1086.  
  1087. /* Some definitions for the hash table.  The hash function MUST be
  1088.    computed as shown in hashf below.  That is because the rescan
  1089.    loop computes the hash value `on the fly' for most tokens,
  1090.    in order to avoid the overhead of a lot of procedure calls to
  1091.    the hashf function.  hashf only exists for the sake of
  1092.    politeness, for use when speed isn't so important. */
  1093.  
  1094. #define ASSERTION_HASHSIZE 37
  1095. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  1096.  
  1097. /* Nonzero means inhibit macroexpansion of what seem to be
  1098.    assertion tests, in rescan.  For #if.  */
  1099. static int assertions_flag;
  1100.  
  1101. /* `struct directive' defines one #-directive, including how to handle it.  */
  1102.  
  1103. struct directive {
  1104.   int length;            /* Length of name */
  1105.   int (*func)();        /* Function to handle directive */
  1106.   char *name;            /* Name of directive */
  1107.   enum node_type type;        /* Code which describes which directive. */
  1108.   char angle_brackets;        /* Nonzero => <...> is special.  */
  1109.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  1110.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  1111. };
  1112.  
  1113. /* Here is the actual list of #-directives, most-often-used first.  */
  1114.  
  1115. static struct directive directive_table[] = {
  1116.   {  6, do_define, "define", T_DEFINE, 0, 1},
  1117.   {  2, do_if, "if", T_IF},
  1118.   {  5, do_xifdef, "ifdef", T_IFDEF},
  1119.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  1120.   {  5, do_endif, "endif", T_ENDIF},
  1121.   {  4, do_else, "else", T_ELSE},
  1122.   {  4, do_elif, "elif", T_ELIF},
  1123.   {  4, do_line, "line", T_LINE},
  1124.   {  7, do_include, "include", T_INCLUDE, 1},
  1125.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  1126.   {  6, do_include, "import", T_IMPORT, 1},
  1127.   {  5, do_undef, "undef", T_UNDEF},
  1128.   {  5, do_error, "error", T_ERROR},
  1129.   {  7, do_warning, "warning", T_WARNING},
  1130. #ifdef SCCS_DIRECTIVE
  1131.   {  4, do_sccs, "sccs", T_SCCS},
  1132. #endif
  1133.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  1134.   {  5, do_ident, "ident", T_IDENT},
  1135.   {  6, do_assert, "assert", T_ASSERT},
  1136.   {  8, do_unassert, "unassert", T_UNASSERT},
  1137.   {  -1, 0, "", T_UNUSED},
  1138. };
  1139.  
  1140. /* When a directive handler is called,
  1141.    this points to the # that started the directive.  */
  1142. U_CHAR *directive_start;
  1143.  
  1144. /* table to tell if char can be part of a C identifier. */
  1145. U_CHAR is_idchar[256];
  1146. /* table to tell if char can be first char of a c identifier. */
  1147. U_CHAR is_idstart[256];
  1148. /* table to tell if c is horizontal space.  */
  1149. U_CHAR is_hor_space[256];
  1150. /* table to tell if c is horizontal or vertical space.  */
  1151. static U_CHAR is_space[256];
  1152.  
  1153. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  1154. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  1155.   
  1156. static int errors = 0;            /* Error counter for exit code */
  1157.  
  1158. /* Name of output file, for error messages.  */
  1159. static char *out_fname;
  1160.  
  1161. /* Zero means dollar signs are punctuation.
  1162.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  1163.    This must be 0 for correct processing of this ANSI C program:
  1164.     #define foo(a) #a
  1165.     #define lose(b) foo (b)
  1166.     #define test$
  1167.     lose (test)    */
  1168. static int dollars_in_ident;
  1169. #ifndef DOLLARS_IN_IDENTIFIERS
  1170. #define DOLLARS_IN_IDENTIFIERS 1
  1171. #endif
  1172.  
  1173. static FILE_BUF expand_to_temp_buffer ();
  1174.  
  1175. static DEFINITION *collect_expansion ();
  1176.  
  1177. /* Stack of conditionals currently in progress
  1178.    (including both successful and failing conditionals).  */
  1179.  
  1180. struct if_stack {
  1181.   struct if_stack *next;    /* for chaining to the next stack frame */
  1182.   char *fname;        /* copied from input when frame is made */
  1183.   int lineno;            /* similarly */
  1184.   int if_succeeded;        /* true if a leg of this if-group
  1185.                     has been passed through rescan */
  1186.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  1187.                    this is the macro name tested.  */
  1188.   enum node_type type;        /* type of last directive seen in this group */
  1189. };
  1190. typedef struct if_stack IF_STACK_FRAME;
  1191. static IF_STACK_FRAME *if_stack = NULL;
  1192.  
  1193. /* Buffer of -M output.  */
  1194. static char *deps_buffer;
  1195.  
  1196. /* Number of bytes allocated in above.  */
  1197. static int deps_allocated_size;
  1198.  
  1199. /* Number of bytes used.  */
  1200. static int deps_size;
  1201.  
  1202. /* Number of bytes since the last newline.  */
  1203. static int deps_column;
  1204.  
  1205. /* Nonzero means -I- has been seen,
  1206.    so don't look for #include "foo" the source-file directory.  */
  1207. static int ignore_srcdir;
  1208.  
  1209. /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
  1210.    retrying if necessary.  Return a negative value if an error occurs,
  1211.    otherwise return the actual number of bytes read,
  1212.    which must be LEN unless end-of-file was reached.  */
  1213.  
  1214. static int
  1215. safe_read (desc, ptr, len)
  1216.      int desc;
  1217.      char *ptr;
  1218.      int len;
  1219. {
  1220.   int left = len;
  1221.   while (left > 0) {
  1222.     int nchars = read (desc, ptr, left);
  1223.     if (nchars < 0)
  1224.       {
  1225. #ifdef EINTR
  1226.     if (errno == EINTR)
  1227.       continue;
  1228. #endif
  1229.     return nchars;
  1230.       }
  1231.     if (nchars == 0)
  1232.       break;
  1233.     ptr += nchars;
  1234.     left -= nchars;
  1235.   }
  1236.   return len - left;
  1237. }
  1238.  
  1239. /* Write LEN bytes at PTR to descriptor DESC,
  1240.    retrying if necessary, and treating any real error as fatal.  */
  1241.  
  1242. static void
  1243. safe_write (desc, ptr, len)
  1244.      int desc;
  1245.      char *ptr;
  1246.      int len;
  1247. {
  1248.   while (len > 0) {
  1249.     int written = write (desc, ptr, len);
  1250.     if (written < 0)
  1251.       {
  1252. #ifdef EINTR
  1253.     if (errno == EINTR)
  1254.       continue;
  1255. #endif
  1256.     pfatal_with_name (out_fname);
  1257.       }
  1258.     ptr += written;
  1259.     len -= written;
  1260.   }
  1261. }
  1262.  
  1263. int
  1264. main (argc, argv)
  1265.      int argc;
  1266.      char **argv;
  1267. {
  1268.   int st_mode;
  1269.   long st_size;
  1270.   char *in_fname;
  1271.   char *p;
  1272.   int f, i;
  1273.   FILE_BUF *fp;
  1274.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  1275.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  1276.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  1277.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  1278.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  1279. #ifdef NEXT_CPP_SERVER
  1280.   #define MAX_INPUT_ARGS 128  
  1281.   #define MAX_INPUT_CHARS 4096
  1282.   char command[MAX_INPUT_CHARS+1];
  1283.   struct file_name_list *include_ptr; 
  1284.   int max_argc = argc;  /* keep track of largest argc seen */
  1285.   char * header_fname;
  1286.   char * requested_cwd;
  1287.   char actual_cwd[1024];
  1288. #endif
  1289.   /* Record the option used with each element of pend_assertions.
  1290.      This is preparation for supporting more than one option for making
  1291.      an assertion.  */
  1292.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  1293.   int inhibit_predefs = 0;
  1294.   int no_standard_includes = 0;
  1295.   int no_standard_cplusplus_includes = 0;
  1296.   int missing_newline = 0;
  1297.  
  1298.   /* Non-0 means don't output the preprocessed program.  */
  1299.   int inhibit_output = 0;
  1300.   /* Non-0 means -v, so print the full set of include dirs.  */
  1301.   int verbose = 0;
  1302.  
  1303.   /* File name which deps are being written to.
  1304.      This is 0 if deps are being written to stdout.  */
  1305.   char *deps_file = 0;
  1306.   /* Fopen file mode to open deps_file with.  */
  1307.   char *deps_mode = "a";
  1308.   /* Stream on which to print the dependency information.  */
  1309.   FILE *deps_stream = 0;
  1310.   /* Target-name to write with the dependency information.  */
  1311.   char *deps_target = 0;
  1312.  
  1313. #ifndef NeXT
  1314. #ifdef RLIMIT_STACK
  1315.   /* Get rid of any avoidable limit on stack size.  */
  1316.   {
  1317.     struct rlimit rlim;
  1318.  
  1319.     /* Set the stack limit huge so that alloca (particularly stringtab
  1320.      * in dbxread.c) does not fail. */
  1321.     getrlimit (RLIMIT_STACK, &rlim);
  1322.     rlim.rlim_cur = rlim.rlim_max;
  1323.     setrlimit (RLIMIT_STACK, &rlim);
  1324.   }
  1325. #endif /* RLIMIT_STACK defined */
  1326. #endif
  1327.  
  1328. #ifdef SIGPIPE
  1329.   signal (SIGPIPE, pipe_closed);
  1330. #endif
  1331.  
  1332.   p = argv[0] + strlen (argv[0]);
  1333.   while (p != argv[0] && p[-1] != '/') --p;
  1334.   progname = p;
  1335.  
  1336. #ifdef VMS
  1337.   {
  1338.     /* Remove directories from PROGNAME.  */
  1339.     char *s;
  1340.  
  1341.     progname = savestring (argv[0]);
  1342.  
  1343.     if (!(s = rindex (progname, ']')))
  1344.       s = rindex (progname, ':');
  1345.     if (s)
  1346.       strcpy (progname, s+1);
  1347.     if (s = rindex (progname, '.'))
  1348.       *s = '\0';
  1349.   }
  1350. #endif
  1351.  
  1352.   in_fname = NULL;
  1353.   out_fname = NULL;
  1354.  
  1355.   /* Initialize is_idchar to allow $.  */
  1356.   dollars_in_ident = 1;
  1357.   initialize_char_syntax ();
  1358.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1359.  
  1360.   no_line_commands = 0;
  1361.   no_trigraphs = 1;
  1362.   dump_macros = dump_none;
  1363.   no_output = 0;
  1364.   cplusplus = 0;
  1365.   cplusplus_comments = 1;
  1366.  
  1367. #ifdef NEXT_CPP_SERVER
  1368. #define FAILURE                      \
  1369.         if (serverized)              \
  1370.        goto processCommand;      \
  1371.     else                         \
  1372.        return FAILURE_EXIT_CODE
  1373.  
  1374. #define BAILOUT(name) {              \
  1375.         if (serverized) {            \
  1376.            perror_with_name (name);  \
  1377.            goto finishRequest;       \
  1378.         } else {                     \
  1379.            pfatal_with_name (name);  \
  1380.         }                            \
  1381. }
  1382.            
  1383.   /* initialize output buffers */
  1384.   init_output_file_buf(&outbuf);
  1385.   init_output_file_buf(&sys_header_outbuf);
  1386.   init_output_file_buf(&user_header_outbuf);
  1387.  
  1388.   if (argc > 1 && !strcmp(argv[1],"-serverize")) {
  1389.      /* set up for serverized cpp semantics and looping on input commands */
  1390.      serverized = 1;
  1391.      segregate_output = 1;
  1392.      dump_macros = dump_definitions;
  1393.      /* mark_expansions = 1;   only adds about 4-5% to output text size */
  1394.      if (!getcwd(actual_cwd, 1024))
  1395.         pfatal_with_name(actual_cwd);
  1396.      argv = (char **) xmalloc(sizeof(char *) * MAX_INPUT_ARGS);
  1397.      fprintf(stderr,"Starting serverized cpp process.  Enter commands:\n");
  1398.   }
  1399.  
  1400.   processCommand:
  1401.   while (serverized) {
  1402.      int preprocess = 0;
  1403.      char *tok;
  1404.  
  1405.      /* read a command off of stdin and point argv at it */
  1406.      if (!fgets(command, MAX_INPUT_CHARS, stdin)) {
  1407.         fprintf(stderr,"End of server commands.  Exiting...\n");
  1408. #ifdef PROFILE
  1409.         chdir(getenv("HOME"));
  1410. #endif        
  1411.         exit(FAILURE_EXIT_CODE);
  1412.      }
  1413.      argc = 0;
  1414.      for (tok = strtok(command," \t\n");
  1415.           tok && (argc < MAX_INPUT_ARGS);
  1416.           tok = strtok(NULL," \t\n")) 
  1417.         argv[argc++] = tok;
  1418.      if (!argc)
  1419.         goto processCommand;
  1420.      else if (argc > max_argc) {
  1421.         max_argc = argc;
  1422.         pend_files = (char **) xrealloc (pend_files, argc * sizeof (char *));
  1423.         pend_defs = (char **) xrealloc (pend_defs, argc * sizeof (char *));
  1424.         pend_undefs = (char **) xrealloc (pend_undefs, argc * sizeof (char *));
  1425.         pend_assertions = (char **) xrealloc (pend_assertions, 
  1426.                                                        argc * sizeof (char *));
  1427.         pend_includes = (char **) xrealloc (pend_includes, 
  1428.                                                        argc * sizeof (char *));
  1429.         pend_assertion_options = (char **) xrealloc (pend_assertion_options,
  1430.                                                        argc * sizeof (char *));       
  1431.      }
  1432.  
  1433.      switch (argv[0][0]) {
  1434.       case 'c':
  1435.         if (!strcmp(argv[0],"cpp")) 
  1436.            preprocess = 1; /* fall on through into body of main */
  1437.         break;
  1438.         
  1439.       case 'm':
  1440.         if (!strcmp(argv[0],"modtime"))
  1441.            if (argc > 2) {
  1442.               time_t lastModTime = file_modtime (argv[2]);
  1443.               fprintf(stderr, "modtime is %d\n");
  1444.            } else {
  1445.               fprintf(stderr,"bad modtime argument: <file name>\n");
  1446.            }
  1447.         break;
  1448.            
  1449.       case 'n':
  1450.         if (!strcmp(argv[0],"notice_file_modification"))
  1451.            if (argc > 1)
  1452.               handle_file_modification(argv[1]);
  1453.            else
  1454.               fprintf(stderr,
  1455.                       "bad notice_file_modification argument: <file name>\n");           
  1456.         break;
  1457.         
  1458.       case 's':
  1459.         if (!strcmp(argv[0],"stats"))
  1460.            dump_memory_statistics();
  1461.         break;
  1462.  
  1463.       case 't':
  1464.         if (!strcmp(argv[0],"terminate"))
  1465.            exit(0);
  1466.         else if (!strcmp(argv[0],"timings"))
  1467.            if ((argc > 1) && !strcmp(argv[1],"on"))
  1468.               timings = 1;
  1469.         break;
  1470.  
  1471.       case 'w':
  1472.         if (!strcmp(argv[0],"write_sys_headers")) {
  1473.            if (argc > 2) {
  1474.               int write_all = (strcmp(argv[1], "all") == 0) ? 1 : 0;
  1475.               out_fname = argv[2];
  1476.               write_header_output (out_fname, &sys_header_outbuf, write_all, 1);
  1477.            } else {
  1478.               fprintf(stderr,
  1479.                   "bad write_sys_headers arguments: <all|new> <file name>\n");
  1480.            }
  1481.         } else if (!strcmp(argv[0],"write_user_headers")) {
  1482.            if (argc > 1) {
  1483.               int write_all = (strcmp(argv[1], "all") == 0) ? 1 : 0;
  1484.               out_fname = argv[2];
  1485.               write_header_output (out_fname, &user_header_outbuf, write_all,1);
  1486.            } else {
  1487.               fprintf(stderr,
  1488.                 "bad write_user_headers arguments: <all|new> <file name>\n");
  1489.            }
  1490.         }
  1491.         break;
  1492.            
  1493.      } /* switch(argv[0][0]) */
  1494.      if (preprocess)
  1495.         break;  /* go do regular "main" logic on this preprocess request */
  1496.   }
  1497.   errors = 0;  /* in case last iteration caused errors */
  1498.   in_fname = out_fname = header_fname = requested_cwd = NULL;
  1499.   outbuf_stat_info_recorded = 0;
  1500. #endif
  1501.      
  1502.   bzero (pend_files, argc * sizeof (char *));
  1503.   bzero (pend_defs, argc * sizeof (char *));
  1504.   bzero (pend_undefs, argc * sizeof (char *));
  1505.   bzero (pend_assertions, argc * sizeof (char *));
  1506.   bzero (pend_includes, argc * sizeof (char *));
  1507.  
  1508.   /* Process switches and find input file name.  */
  1509.  
  1510.   for (i = 1; i < argc; i++) {
  1511.     if (argv[i][0] != '-') {
  1512.       if (out_fname != NULL)
  1513.     fatal ("Usage: %s [switches] input output", argv[0]);
  1514.       else if (in_fname != NULL)
  1515.     out_fname = argv[i];
  1516.       else
  1517.     in_fname = argv[i];
  1518.     } else {
  1519.       switch (argv[i][1]) {
  1520. #ifdef NEXT_SEMANTICS
  1521.       case 'a':
  1522.     if (!strcmp (argv[i], "-arch"))    /* ignore -arch <archname> */
  1523.       i++;
  1524.     break;
  1525.  
  1526.       case 'O':
  1527.     if (! strcmp (argv[i], "-ObjC++"))
  1528.       ; /* no-op */
  1529.     break;
  1530.         
  1531.       case 'c':
  1532.     if (! strcmp (argv[i], "-c++-comments"))
  1533.       cplusplus_comments = 1;
  1534.     break;
  1535. #endif
  1536.  
  1537.       case 'i':
  1538.     if (!strcmp (argv[i], "-include")) {
  1539.       if (i + 1 == argc)
  1540.         fatal ("Filename missing after `-include' option");
  1541.       else
  1542.         pend_includes[i] = argv[i+1], i++;
  1543.     }
  1544.     else if (!strcmp (argv[i], "-imacros")) {
  1545.       if (i + 1 == argc)
  1546.         fatal ("Filename missing after `-imacros' option");
  1547.       else
  1548.         pend_files[i] = argv[i+1], i++;
  1549.     }
  1550.     else if (!strcmp (argv[i], "-iprefix")) {
  1551.       if (i + 1 == argc)
  1552.         fatal ("Filename missing after `-iprefix' option");
  1553.       else
  1554.         include_prefix = argv[++i];
  1555.     }
  1556.     else if (!strcmp (argv[i], "-isystem")) {
  1557.       struct file_name_list *dirtmp;
  1558.  
  1559.       if (i + 1 == argc)
  1560.         fatal ("Filename missing after `-isystem' option");
  1561.  
  1562.       dirtmp = (struct file_name_list *)
  1563.         xmalloc (sizeof (struct file_name_list));
  1564.       dirtmp->next = 0;
  1565.       dirtmp->control_macro = 0;
  1566.       dirtmp->c_system_include_path = 1;
  1567.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
  1568.       strcpy (dirtmp->fname, argv[++i]);
  1569.       dirtmp->got_name_map = 0;
  1570.  
  1571.       if (before_system == 0)
  1572.         before_system = dirtmp;
  1573.       else
  1574.         last_before_system->next = dirtmp;
  1575.       last_before_system = dirtmp; /* Tail follows the last one */
  1576.     }
  1577.     /* Add directory to end of path for includes,
  1578.        with the default prefix at the front of its name.  */
  1579.     else if (!strcmp (argv[i], "-iwithprefix")) {
  1580.       struct file_name_list *dirtmp;
  1581.       char *prefix;
  1582.  
  1583.       if (include_prefix != 0)
  1584.         prefix = include_prefix;
  1585.       else {
  1586.         prefix = savestring (GCC_INCLUDE_DIR);
  1587.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1588.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1589.           prefix[strlen (prefix) - 7] = 0;
  1590.       }
  1591.  
  1592.       dirtmp = (struct file_name_list *)
  1593.         xmalloc (sizeof (struct file_name_list));
  1594.       dirtmp->next = 0;    /* New one goes on the end */
  1595.       dirtmp->control_macro = 0;
  1596.       dirtmp->c_system_include_path = 0;
  1597.       if (i + 1 == argc)
  1598.         fatal ("Directory name missing after `-iwithprefix' option");
  1599.  
  1600.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
  1601.                         + strlen (prefix) + 1);
  1602.       strcpy (dirtmp->fname, prefix);
  1603.       strcat (dirtmp->fname, argv[++i]);
  1604.       dirtmp->got_name_map = 0;
  1605.  
  1606.       if (after_include == 0)
  1607.         after_include = dirtmp;
  1608.       else
  1609.         last_after_include->next = dirtmp;
  1610.       last_after_include = dirtmp; /* Tail follows the last one */
  1611.     }
  1612.     /* Add directory to main path for includes,
  1613.        with the default prefix at the front of its name.  */
  1614.     else if (!strcmp (argv[i], "-iwithprefixbefore")) {
  1615.       struct file_name_list *dirtmp;
  1616.       char *prefix;
  1617.  
  1618.       if (include_prefix != 0)
  1619.         prefix = include_prefix;
  1620.       else {
  1621.         prefix = savestring (GCC_INCLUDE_DIR);
  1622.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1623.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1624.           prefix[strlen (prefix) - 7] = 0;
  1625.       }
  1626.  
  1627.       dirtmp = (struct file_name_list *)
  1628.         xmalloc (sizeof (struct file_name_list));
  1629.       dirtmp->next = 0;    /* New one goes on the end */
  1630.       dirtmp->control_macro = 0;
  1631.       dirtmp->c_system_include_path = 0;
  1632.       if (i + 1 == argc)
  1633.         fatal ("Directory name missing after `-iwithprefixbefore' option");
  1634.  
  1635.       dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
  1636.                         + strlen (prefix) + 1);
  1637.       strcpy (dirtmp->fname, prefix);
  1638.       strcat (dirtmp->fname, argv[++i]);
  1639.       dirtmp->got_name_map = 0;
  1640.  
  1641.       append_include_chain (dirtmp, dirtmp);
  1642.     }
  1643.     /* Add directory to end of path for includes.  */
  1644.     else if (!strcmp (argv[i], "-idirafter")) {
  1645.       struct file_name_list *dirtmp;
  1646.  
  1647.       dirtmp = (struct file_name_list *)
  1648.         xmalloc (sizeof (struct file_name_list));
  1649.       dirtmp->next = 0;    /* New one goes on the end */
  1650.       dirtmp->control_macro = 0;
  1651.       dirtmp->c_system_include_path = 0;
  1652.       if (i + 1 == argc)
  1653.         fatal ("Directory name missing after `-idirafter' option");
  1654.       else
  1655.         dirtmp->fname = argv[++i];
  1656.       dirtmp->got_name_map = 0;
  1657.  
  1658.       if (after_include == 0)
  1659.         after_include = dirtmp;
  1660.       else
  1661.         last_after_include->next = dirtmp;
  1662.       last_after_include = dirtmp; /* Tail follows the last one */
  1663.     }
  1664. #ifdef NEXT_CPP_SERVER
  1665.         /* To simulate a make process which cd's into sub-directories
  1666.          * in order to use makefiles local to that directory, we need to
  1667.          * support the notion of requesting a particular working directory
  1668.          * for a preprocessing command.
  1669.          */
  1670.         else if (!strcmp (argv[i], "-in_directory")) {
  1671.            if (i + 1 == argc)
  1672.               fatal ("Filename missing after -output_headers option");
  1673.            requested_cwd = argv[++i];
  1674.         }
  1675. #endif        
  1676. #ifdef NEXT_OBJC_RUNTIME
  1677.     else if (argv[i][2] != 0)
  1678.       pend_files[i] = argv[i] + 2;
  1679.     else if (i + 1 == argc)
  1680.       fatal ("Filename missing after -i option");
  1681.     else
  1682.       pend_files[i] = argv[i+1], i++;
  1683. #endif
  1684.     break;
  1685.  
  1686.       case 'o':
  1687. #ifdef NEXT_CPP_SERVER
  1688.         if (!strcmp (argv[i], "-output_headers")) {
  1689.            if (i + 1 == argc)
  1690.               fatal ("Filename missing after -output_headers option");
  1691.            header_fname = argv[++i];
  1692.         } else {
  1693.            if (out_fname != NULL)
  1694.               fatal ("Output filename specified twice");
  1695.            if (i + 1 == argc)
  1696.               fatal ("Filename missing after -o option");
  1697.            out_fname = argv[++i];
  1698.            if (!strcmp (out_fname, "-"))
  1699.               out_fname = "";
  1700.         }
  1701. #else        
  1702.     if (out_fname != NULL)
  1703.       fatal ("Output filename specified twice");
  1704.     if (i + 1 == argc)
  1705.       fatal ("Filename missing after -o option");
  1706.     out_fname = argv[++i];
  1707.     if (!strcmp (out_fname, "-"))
  1708.       out_fname = "";
  1709. #endif
  1710.         break;
  1711.  
  1712.       case 'p':
  1713.     if (!strcmp (argv[i], "-pedantic"))
  1714.       pedantic = 1;
  1715.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1716.       pedantic = 1;
  1717.       pedantic_errors = 1;
  1718. #ifdef NEXT_PDO
  1719.         } else if (!strcmp (argv[i], "-precomp")) {
  1720.            fatal ("-precomp option not supported");
  1721. #endif
  1722.     } else if (!strcmp (argv[i], "-pcp")) {
  1723.       char *pcp_fname = argv[++i];
  1724.       pcp_outfile = 
  1725.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1726.          ? fopen (pcp_fname, "w")
  1727.          : fdopen (dup (fileno (stdout)), "w"));
  1728.       if (pcp_outfile == 0)
  1729. #ifdef NEXT_CPP_SERVER
  1730.              BAILOUT (pcp_fname);
  1731. #else             
  1732.              pfatal_with_name (pcp_fname);
  1733. #endif
  1734.       no_precomp = 1;
  1735.     }
  1736.     break;
  1737.  
  1738.       case 't':
  1739.     if (!strcmp (argv[i], "-traditional")) {
  1740.       traditional = 1;
  1741.       if (dollars_in_ident > 0)
  1742.         dollars_in_ident = 1;
  1743.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1744.       no_trigraphs = 0;
  1745.     }
  1746.     break;
  1747.  
  1748. #ifdef NEXT_SEMANTICS
  1749.       case 's':
  1750.     if (!strcmp (argv[i], "-smart"))
  1751.       ;
  1752.         else if (!strcmp (argv[i], "-segregate-output"))
  1753.           segregate_output = 1;
  1754.     break;
  1755.       case 'm':
  1756.     if (!strcmp (argv[i], "-mark-expansions"))
  1757.       mark_expansions = 1;
  1758.     break;
  1759. #endif
  1760.  
  1761.       case 'l':
  1762.         if (! strcmp (argv[i], "-lang-c"))
  1763.             cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1764.     if (! strcmp (argv[i], "-lang-c++"))
  1765.       cplusplus = 1, cplusplus_comments = 1, objc = 0;
  1766.     if (! strcmp (argv[i], "-lang-c-c++-comments"))
  1767.       cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1768.     if (! strcmp (argv[i], "-lang-objc"))
  1769.       objc = 1, cplusplus_comments = 1, cplusplus = 0;
  1770.     if (! strcmp (argv[i], "-lang-objc++"))
  1771.       objc = 1, cplusplus_comments = 1, cplusplus = 1;
  1772.         if (! strcmp (argv[i], "-lang-asm"))
  1773.        lang_asm = 1;
  1774.      if (! strcmp (argv[i], "-lint"))
  1775.        for_lint = 1;
  1776.     break;
  1777.  
  1778.       case '+':
  1779.     cplusplus = 1, cplusplus_comments = 1;
  1780.     break;
  1781.  
  1782.       case 'w':
  1783.     inhibit_warnings = 1;
  1784.     break;
  1785.  
  1786.       case 'W':
  1787.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1788.       warn_trigraphs = 1;
  1789.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1790.       warn_trigraphs = 0;
  1791.     else if (!strcmp (argv[i], "-Wcomment"))
  1792.       warn_comments = 1;
  1793.     else if (!strcmp (argv[i], "-Wno-comment"))
  1794.       warn_comments = 0;
  1795.     else if (!strcmp (argv[i], "-Wcomments"))
  1796.       warn_comments = 1;
  1797.     else if (!strcmp (argv[i], "-Wno-comments"))
  1798.       warn_comments = 0;
  1799.     else if (!strcmp (argv[i], "-Wtraditional"))
  1800.       warn_stringify = 1;
  1801.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1802.       warn_stringify = 0;
  1803.     else if (!strcmp (argv[i], "-Wimport"))
  1804.       warn_import = 1;
  1805.     else if (!strcmp (argv[i], "-Wno-import"))
  1806.       warn_import = 0;
  1807.     else if (!strcmp (argv[i], "-Werror"))
  1808.       warnings_are_errors = 1;
  1809.     else if (!strcmp (argv[i], "-Wno-error"))
  1810.       warnings_are_errors = 0;
  1811. #ifdef NEXT_PDO
  1812.     else if (!strcmp (argv[i], "-Wmost"))
  1813.       {
  1814.         warn_trigraphs = 1;
  1815.         warn_comments = 0;
  1816.       }
  1817. #endif
  1818.     else if (!strcmp (argv[i], "-Wall"))
  1819.       {
  1820.         warn_trigraphs = 1;
  1821.         warn_comments = 1;
  1822.       }
  1823.     break;
  1824.  
  1825.       case 'M':
  1826.     /* The style of the choices here is a bit mixed.
  1827.        The chosen scheme is a hybrid of keeping all options in one string
  1828.        and specifying each option in a separate argument:
  1829.        -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
  1830.        -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
  1831.        -M[M][G][D file].  This is awkward to handle in specs, and is not
  1832.        as extensible.  */
  1833.     /* ??? -MG must be specified in addition to one of -M or -MM.
  1834.        This can be relaxed in the future without breaking anything.
  1835.        The converse isn't true.  */
  1836.  
  1837.     /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
  1838.     if (!strcmp (argv[i], "-MG"))
  1839.       {
  1840.         print_deps_missing_files = 1;
  1841.         break;
  1842.       }
  1843.     if (!strcmp (argv[i], "-M"))
  1844.       print_deps = 2;
  1845.     else if (!strcmp (argv[i], "-MM"))
  1846.       print_deps = 1;
  1847.     else if (!strcmp (argv[i], "-MD"))
  1848.       print_deps = 2;
  1849.     else if (!strcmp (argv[i], "-MMD"))
  1850.       print_deps = 1;
  1851.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1852.     if (!strcmp (argv[i], "-MD")
  1853.         || !strcmp (argv[i], "-MMD")) {
  1854.       i++;
  1855.       deps_file = argv[i];
  1856.       deps_mode = "w";
  1857.     } else {
  1858.       /* For -M and -MM, write deps on standard output
  1859.          and suppress the usual output.  */
  1860.       deps_stream = stdout;
  1861.       inhibit_output = 1;
  1862.     }      
  1863.     break;
  1864.  
  1865.       case 'd':
  1866.     {
  1867.       char *p = argv[i] + 2;
  1868.       char c;
  1869.       while (c = *p++) {
  1870.         /* Arg to -d specifies what parts of macros to dump */
  1871.         switch (c) {
  1872.         case 'M':
  1873.           dump_macros = dump_only;
  1874.           no_output = 1;
  1875.           break;
  1876.         case 'N':
  1877.           dump_macros = dump_names;
  1878.           break;
  1879.         case 'D':
  1880.           dump_macros = dump_definitions;
  1881.           break;
  1882.         }
  1883.       }
  1884.     }
  1885.     break;
  1886.  
  1887.       case 'g':
  1888.     if (argv[i][2] == '3')
  1889.       debug_output = 1;
  1890.     break;
  1891.  
  1892.       case 'v':
  1893.     fprintf (stderr, "GNU CPP version %s", version_string);
  1894. #ifdef TARGET_VERSION
  1895.     TARGET_VERSION;
  1896. #endif
  1897.     fprintf (stderr, "\n");
  1898.     verbose = 1;
  1899.     break;
  1900.  
  1901.       case 'H':
  1902.     print_include_names = 1;
  1903.     break;
  1904.  
  1905.       case 'D':
  1906.     if (argv[i][2] != 0)
  1907.       pend_defs[i] = argv[i] + 2;
  1908.     else if (i + 1 == argc)
  1909.       fatal ("Macro name missing after -D option");
  1910.     else
  1911.       i++, pend_defs[i] = argv[i];
  1912.     break;
  1913.  
  1914.       case 'A':
  1915.     {
  1916.       char *p;
  1917.  
  1918.       if (argv[i][2] != 0)
  1919.         p = argv[i] + 2;
  1920.       else if (i + 1 == argc)
  1921.         fatal ("Assertion missing after -A option");
  1922.       else
  1923.         p = argv[++i];
  1924.  
  1925.       if (!strcmp (p, "-")) {
  1926.         /* -A- eliminates all predefined macros and assertions.
  1927.            Let's include also any that were specified earlier
  1928.            on the command line.  That way we can get rid of any
  1929.            that were passed automatically in from GCC.  */
  1930.         int j;
  1931.         inhibit_predefs = 1;
  1932.         for (j = 0; j < i; j++)
  1933.           pend_defs[j] = pend_assertions[j] = 0;
  1934.       } else {
  1935.         pend_assertions[i] = p;
  1936.         pend_assertion_options[i] = "-A";
  1937.       }
  1938.     }
  1939.     break;
  1940.  
  1941.       case 'U':        /* JF #undef something */
  1942.     if (argv[i][2] != 0)
  1943.       pend_undefs[i] = argv[i] + 2;
  1944.     else if (i + 1 == argc)
  1945.       fatal ("Macro name missing after -U option");
  1946.     else
  1947.       pend_undefs[i] = argv[i+1], i++;
  1948.     break;
  1949.  
  1950.       case 'C':
  1951.     put_out_comments = 1;
  1952.     break;
  1953.  
  1954.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1955.     break;
  1956.  
  1957.       case 'P':
  1958.     no_line_commands = 1;
  1959.     break;
  1960.  
  1961.       case '$':            /* Don't include $ in identifiers.  */
  1962.     dollars_in_ident = 0;
  1963.     break;
  1964.  
  1965.       case 'I':            /* Add directory to path for includes.  */
  1966.     {
  1967.       struct file_name_list *dirtmp;
  1968.  
  1969.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1970.         ignore_srcdir = 1;
  1971.         /* Don't use any preceding -I directories for #include <...>.  */
  1972.         first_bracket_include = 0;
  1973.       }
  1974.       else {
  1975.         dirtmp = (struct file_name_list *)
  1976.           xmalloc (sizeof (struct file_name_list));
  1977.         dirtmp->next = 0;        /* New one goes on the end */
  1978.         dirtmp->control_macro = 0;
  1979.         dirtmp->c_system_include_path = 0;
  1980.         if (argv[i][2] != 0)
  1981.           dirtmp->fname = argv[i] + 2;
  1982.         else if (i + 1 == argc)
  1983.           fatal ("Directory name missing after -I option");
  1984.         else
  1985.           dirtmp->fname = argv[++i];
  1986.         dirtmp->got_name_map = 0;
  1987.         append_include_chain (dirtmp, dirtmp);
  1988.       }
  1989.     }
  1990.     break;
  1991. #ifdef NEXT_FRAMEWORKS
  1992.   case 'F':
  1993.   {
  1994.       struct file_name_list *dirtmp;
  1995.  
  1996.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1997.           ignore_srcdir = 1;
  1998.           /* Don't use any preceding -F directories for #include <...>.  */
  1999.           first_bracket_framework = 0;
  2000.       }
  2001.       else {
  2002.           dirtmp = (struct file_name_list *)
  2003.               xmalloc (sizeof (struct file_name_list));
  2004.           dirtmp->next = 0;        /* New one goes on the end */
  2005.           dirtmp->control_macro = 0;
  2006.           dirtmp->c_system_include_path = 0;
  2007.           if (argv[i][2] != 0)
  2008.               dirtmp->fname = argv[i] + 2;
  2009.           else if (i + 1 == argc)
  2010.               fatal ("Directory name missing after -I option");
  2011.           else
  2012.               dirtmp->fname = argv[++i];
  2013.           dirtmp->got_name_map = 0;
  2014.           append_framework_chain (dirtmp, dirtmp);
  2015.       }
  2016.   }
  2017.     break;
  2018. #endif
  2019.   case 'n':
  2020.     if (!strcmp (argv[i], "-nostdinc"))
  2021.       /* -nostdinc causes no default include directories.
  2022.          You must specify all include-file directories with -I.  */
  2023.       no_standard_includes = 1;
  2024.     else if (!strcmp (argv[i], "-nostdinc++"))
  2025.       /* -nostdinc++ causes no default C++-specific include directories. */
  2026.       no_standard_cplusplus_includes = 1;
  2027.     else if (!strcmp (argv[i], "-noprecomp"))
  2028.       no_precomp = 1;
  2029.     if (!strcmp (argv[i], "-no-c++-comments"))
  2030.       cplusplus_comments = 0;
  2031.     break;
  2032.  
  2033.     break;
  2034.  
  2035.       case 'u':
  2036.     /* Sun compiler passes undocumented switch "-undef".
  2037.        Let's assume it means to inhibit the predefined symbols.  */
  2038.     inhibit_predefs = 1;
  2039.     break;
  2040.  
  2041.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  2042.     if (in_fname == NULL) {
  2043.       in_fname = "";
  2044.       break;
  2045.     } else if (out_fname == NULL) {
  2046.       out_fname = "";
  2047.       break;
  2048.     }    /* else fall through into error */
  2049.  
  2050.       default:
  2051.     fatal ("Invalid option `%s'", argv[i]);
  2052.       }
  2053.     }
  2054.   }
  2055.  
  2056. #ifdef NEXT_CPP_SERVER
  2057.   if (serverized && requested_cwd && strcmp(actual_cwd, requested_cwd) != 0) {
  2058.      if (chdir(requested_cwd) == 0)
  2059.         strcpy(actual_cwd, requested_cwd);
  2060.   }
  2061. #endif
  2062.  
  2063.   /* Add dirs from CPATH after dirs from -I.  */
  2064.   /* There seems to be confusion about what CPATH should do,
  2065.      so for the moment it is not documented.  */
  2066.   /* Some people say that CPATH should replace the standard include dirs,
  2067.      but that seems pointless: it comes before them, so it overrides them
  2068.      anyway.  */
  2069.   p = (char *) getenv ("CPATH");
  2070.   if (p != 0 && ! no_standard_includes)
  2071.     path_include (p);
  2072.  
  2073.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  2074.   initialize_char_syntax ();
  2075.  
  2076. #ifndef NEXT_CPP_SERVER
  2077.   /* Initialize output buffer */
  2078.  
  2079.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  2080.   outbuf.bufp = outbuf.buf;
  2081.   outbuf.length = OUTBUF_SIZE;
  2082. #else
  2083.   if (timings)
  2084.      start();
  2085. #endif
  2086.   /* Do partial setup of input buffer for the sake of generating
  2087.      early #line directives (when -g is in effect).  */
  2088.  
  2089.   fp = &instack[++indepth];
  2090.   if (in_fname == NULL)
  2091.     in_fname = "";
  2092.   fp->nominal_fname = fp->fname = in_fname;
  2093.   fp->lineno = 0;
  2094.  
  2095.   /* In C++, wchar_t is a distinct basic type, and we can expect
  2096.      __wchar_t to be defined by cc1plus.  */
  2097.   if (cplusplus)
  2098.     wchar_type = "__wchar_t";
  2099.  
  2100.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  2101.      and option processing.  */
  2102.   initialize_builtins (fp, &outbuf); /* FIXME: does this malloc every time? */
  2103.  
  2104.   /* Do standard #defines and assertions
  2105.      that identify system and machine type.  */
  2106.  
  2107.   /* Normally, the compiler driver passes -undef, which causes this 
  2108.      clause to be skipped (i.e. inhibit_predefs == 1). If being run
  2109.      standalone, this clause is responsible for installing the predefined
  2110.      macros. At present, this cpp has this info compiled into it, rather
  2111.      than reading a specs file for each architecture (which is what the NeXT 
  2112.      compiler driver does) */
  2113.  
  2114.   if (!inhibit_predefs) {
  2115.     char *p = (char *) alloca (strlen (predefs) + 1);
  2116.     strcpy (p, predefs);
  2117.     while (*p) {
  2118.       char *q;
  2119.       while (*p == ' ' || *p == '\t')
  2120.     p++;
  2121.       /* Handle -D options.  */ 
  2122.       if (p[0] == '-' && p[1] == 'D') {
  2123.     q = &p[2];
  2124.     while (*p && *p != ' ' && *p != '\t')
  2125.       p++;
  2126.     if (*p != 0)
  2127.       *p++= 0;
  2128.     if (debug_output)
  2129.       output_line_command (fp, &outbuf, 0, same_file);
  2130.     make_definition (q, &outbuf);
  2131.     while (*p == ' ' || *p == '\t')
  2132.       p++;
  2133.       } else if (p[0] == '-' && p[1] == 'A') {
  2134.     /* Handle -A options (assertions).  */ 
  2135.     char *assertion;
  2136.     char *past_name;
  2137.     char *value;
  2138.     char *past_value;
  2139.     char *termination;
  2140.     int save_char;
  2141.  
  2142.     assertion = &p[2];
  2143.     past_name = assertion;
  2144.     /* Locate end of name.  */
  2145.     while (*past_name && *past_name != ' '
  2146.            && *past_name != '\t' && *past_name != '(')
  2147.       past_name++;
  2148.     /* Locate `(' at start of value.  */
  2149.     value = past_name;
  2150.     while (*value && (*value == ' ' || *value == '\t'))
  2151.       value++;
  2152.     if (*value++ != '(')
  2153.       abort ();
  2154.     while (*value && (*value == ' ' || *value == '\t'))
  2155.       value++;
  2156.     past_value = value;
  2157.     /* Locate end of value.  */
  2158.     while (*past_value && *past_value != ' '
  2159.            && *past_value != '\t' && *past_value != ')')
  2160.       past_value++;
  2161.     termination = past_value;
  2162.     while (*termination && (*termination == ' ' || *termination == '\t'))
  2163.       termination++;
  2164.     if (*termination++ != ')')
  2165.       abort ();
  2166.     if (*termination && *termination != ' ' && *termination != '\t')
  2167.       abort ();
  2168.     /* Temporarily null-terminate the value.  */
  2169.     save_char = *termination;
  2170.     *termination = '\0';
  2171.     /* Install the assertion.  */
  2172.     make_assertion ("-A", assertion);
  2173.     *termination = (char) save_char;
  2174.     p = termination;
  2175.     while (*p == ' ' || *p == '\t')
  2176.       p++;
  2177.       } else {
  2178.     abort ();
  2179.       }
  2180.     }
  2181. /* FIXME */
  2182.     /* snaroff: this code must be removed. It is here as a reminder that
  2183.        whatever machinations the driver does must be mimiced in the preceeding 
  2184.        loop (if we want the behavior to be identical) */
  2185.     make_definition ("__unix__", &outbuf);
  2186.     make_definition ("__NeXT__", &outbuf);
  2187.     make_definition ("__mc68000__", &outbuf);
  2188.     make_definition ("__m68k__", &outbuf);
  2189.     make_definition ("_NEXT_SOURCE", &outbuf);
  2190.   }
  2191.  
  2192.   /* Now handle the command line options.  */
  2193.  
  2194.   /* Do -U's, -D's and -A's in the order they were seen.  */
  2195.   for (i = 1; i < argc; i++) {
  2196.     if (pend_undefs[i]) {
  2197.       if (debug_output)
  2198.         output_line_command (fp, &outbuf, 0, same_file);
  2199.       make_undef (pend_undefs[i], &outbuf);
  2200.     }
  2201.     if (pend_defs[i]) {
  2202.       if (debug_output)
  2203.         output_line_command (fp, &outbuf, 0, same_file);
  2204.       make_definition (pend_defs[i], &outbuf);
  2205.     }
  2206.     if (pend_assertions[i])
  2207.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  2208.   }
  2209.  
  2210.   done_initializing = 1;
  2211.  
  2212.   { /* read the appropriate environment variable and if it exists
  2213.        replace include_defaults with the listed path. */
  2214.     char *epath = 0;
  2215.     switch ((objc << 1) + cplusplus)
  2216.       {
  2217.       case 0:
  2218.     epath = getenv ("C_INCLUDE_PATH");
  2219.     break;
  2220.       case 1:
  2221.     epath = getenv ("CPLUS_INCLUDE_PATH");
  2222.     break;
  2223.       case 2:
  2224.     epath = getenv ("OBJC_INCLUDE_PATH");
  2225.     break;
  2226.       case 3:
  2227.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  2228.     break;
  2229.       }
  2230.     /* If the environment var for this language is set,
  2231.        add to the default list of include directories.  */
  2232.     if (epath) {
  2233.       char *nstore = (char *) alloca (strlen (epath) + 2);
  2234.       int num_dirs;
  2235.       char *startp, *endp;
  2236.  
  2237.       for (num_dirs = 1, startp = epath; *startp; startp++)
  2238.     if (*startp == PATH_SEPARATOR)
  2239.       num_dirs++;
  2240.       include_defaults
  2241.     = (struct default_include *) xmalloc ((num_dirs
  2242.                            * sizeof (struct default_include))
  2243.                           + sizeof (include_defaults_array));
  2244.       startp = endp = epath;
  2245.       num_dirs = 0;
  2246.       while (1) {
  2247.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  2248.         if ((*endp == PATH_SEPARATOR
  2249. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  2250. #ifdef __MSDOS__
  2251.          && (endp-startp != 1 || !isalpha (*startp))
  2252. #endif
  2253. #endif
  2254.          )
  2255.             || *endp == 0) {
  2256.       strncpy (nstore, startp, endp-startp);
  2257.       if (endp == startp)
  2258.         strcpy (nstore, ".");
  2259.       else
  2260.         nstore[endp-startp] = '\0';
  2261.  
  2262.       include_defaults[num_dirs].fname = savestring (nstore);
  2263.       include_defaults[num_dirs].cplusplus = cplusplus;
  2264.       include_defaults[num_dirs].cxx_aware = 1;
  2265.       num_dirs++;
  2266.       if (*endp == '\0')
  2267.         break;
  2268.       endp = startp = endp + 1;
  2269.     } else
  2270.       endp++;
  2271.       }
  2272.       /* Put the usual defaults back in at the end.  */
  2273.       bcopy ((char *) include_defaults_array,
  2274.          (char *) &include_defaults[num_dirs],
  2275.          sizeof (include_defaults_array));
  2276.     }
  2277.   }
  2278.   append_include_chain (before_system, last_before_system);
  2279.   first_system_include = before_system;
  2280.  
  2281.   /* Unless -fnostdinc,
  2282.      tack on the standard include file dirs to the specified list */
  2283.   if (!no_standard_includes) {
  2284.     int hdr;
  2285.     struct default_include *p = include_defaults;
  2286.     char *specd_prefix = include_prefix;
  2287.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  2288.     int default_len = 0;
  2289.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  2290.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  2291.       default_len = strlen (default_prefix) - 7;
  2292.       default_prefix[default_len] = 0;
  2293.     }
  2294.     /* Search "translated" versions of GNU directories.
  2295.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  2296.     if (specd_prefix != 0 && default_len != 0)
  2297.       for (p = include_defaults; p->fname; p++) {
  2298.     /* Some standard dirs are only for C++.  */
  2299.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2300.       /* Does this dir start with the prefix?  */
  2301.       if (!strncmp (p->fname, default_prefix, default_len)) {
  2302.         /* Yes; change prefix and add to search list.  */
  2303.         struct file_name_list *new
  2304.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2305.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  2306.         char *str = (char *) xmalloc (this_len + 1);
  2307.         strcpy (str, specd_prefix);
  2308.         strcat (str, p->fname + default_len);
  2309.         new->fname = str;
  2310.         new->control_macro = 0;
  2311.         new->c_system_include_path = !p->cxx_aware;
  2312.         new->got_name_map = 0;
  2313.         append_include_chain (new, new);
  2314.         if (first_system_include == 0)
  2315.           first_system_include = new;
  2316.       }
  2317.     }
  2318.       }
  2319.     /* Search ordinary names for GNU include directories.  */
  2320.     for (p = include_defaults; p->fname; p++) {
  2321.       /* Some standard dirs are only for C++.  */
  2322.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2323.     struct file_name_list *new
  2324.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2325.     new->control_macro = 0;
  2326.     new->c_system_include_path = !p->cxx_aware;
  2327.     new->fname = p->fname;
  2328.     new->got_name_map = 0;
  2329.     append_include_chain (new, new);
  2330.  
  2331.     if (first_system_include == 0)
  2332.       first_system_include = new;
  2333.       }
  2334.     }
  2335. #ifdef NEXT_FRAMEWORKS
  2336.     for (hdr=0; framework_defaults_array[hdr].fname; hdr++) {
  2337.        struct stat dummy;
  2338.        if (stat(framework_defaults_array[hdr].fname, &dummy) == 0) {
  2339.           /*  Frmaework directory actually exists */
  2340.           struct file_name_list *new
  2341.              = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2342.           new->control_macro = 0;
  2343.           new->c_system_include_path = !framework_defaults_array[hdr].cxx_aware;
  2344.           new->fname = framework_defaults_array[hdr].fname;
  2345.           new->got_name_map = 0;
  2346.           append_framework_chain (new, new);
  2347.        }
  2348.     }
  2349.     free (default_prefix);
  2350. #endif
  2351.   }
  2352.  
  2353.   /* Tack the after_include chain at the end of the include chain.  */
  2354.   append_include_chain (after_include, last_after_include);
  2355.   if (first_system_include == 0)
  2356.     first_system_include = after_include;
  2357.  
  2358.   /* With -v, print the list of dirs to search.  */
  2359.   if (verbose) {
  2360.     struct file_name_list *p;
  2361.     fprintf (stderr, "#include \"...\" search starts here:\n");
  2362.     for (p = include; p; p = p->next) {
  2363.       if (p == first_bracket_include)
  2364.     fprintf (stderr, "#include <...> search starts here:\n");
  2365.       fprintf (stderr, " %s\n", p->fname);
  2366.     }
  2367.     fprintf (stderr, "End of search list.\n");
  2368.   }
  2369.  
  2370.   /* Scan the -imacros files before the main input.
  2371.      Much like #including them, but with no_output set
  2372.      so that only their macro definitions matter.  */
  2373.  
  2374.   no_output++; no_record_file++;
  2375.   for (i = 1; i < argc; i++)
  2376.     if (pend_files[i]) {
  2377.       int fd = open (pend_files[i], O_RDONLY, 0666);
  2378.       if (fd < 0) {
  2379.     perror_with_name (pend_files[i]);
  2380. #ifdef NEXT_CPP_SERVER
  2381.         FAILURE;
  2382. #else
  2383.     return FAILURE_EXIT_CODE;
  2384. #endif    
  2385.       }
  2386. #ifdef NEXT_CPP_SERVER
  2387.       finclude (fd, pend_files[i], &outbuf, 0, 0, 0, 0);
  2388. #else
  2389.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  2390. #endif
  2391.     }
  2392.   no_output--; no_record_file--;
  2393.  
  2394.   /* Copy the entire contents of the main input file into
  2395.      the stacked input buffer previously allocated for it.  */
  2396.  
  2397.   /* JF check for stdin */
  2398.   if (in_fname == NULL || *in_fname == 0) {
  2399.     in_fname = "";
  2400.     f = 0;
  2401.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  2402. #ifdef NEXT_CPP_SERVER
  2403.     BAILOUT (in_fname);
  2404. #else
  2405.     goto perror;
  2406. #endif
  2407.  
  2408.   /* -MG doesn't select the form of output and must be specified with one of
  2409.      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
  2410.      inhibit compilation.  */
  2411.   if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
  2412.     fatal ("-MG must be specified with one of -M or -MM");
  2413.  
  2414.   /* Either of two environment variables can specify output of deps.
  2415.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  2416.      where OUTPUT_FILE is the file to write deps info to
  2417.      and DEPS_TARGET is the target to mention in the deps.  */
  2418.  
  2419.   if (print_deps == 0
  2420.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  2421.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  2422.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  2423.     char *s;
  2424.     char *output_file;
  2425.  
  2426.     if (spec == 0) {
  2427.       spec = getenv ("SUNPRO_DEPENDENCIES");
  2428.       print_deps = 2;
  2429.     }
  2430.     else
  2431.       print_deps = 1;
  2432.  
  2433.     s = spec;
  2434.     /* Find the space before the DEPS_TARGET, if there is one.  */
  2435.     /* This should use index.  (mrs) */
  2436.     while (*s != 0 && *s != ' ') s++;
  2437.     if (*s != 0) {
  2438.       deps_target = s + 1;
  2439.       output_file = (char *) xmalloc (s - spec + 1);
  2440.       bcopy (spec, output_file, s - spec);
  2441.       output_file[s - spec] = 0;
  2442.     }
  2443.     else {
  2444.       deps_target = 0;
  2445.       output_file = spec;
  2446.     }
  2447.       
  2448.     deps_file = output_file;
  2449.     deps_mode = "a";
  2450.   }
  2451.  
  2452.   /* For -M, print the expected object file name
  2453.      as the target of this Make-rule.  */
  2454.   if (print_deps) {
  2455.     deps_allocated_size = 200;
  2456.     deps_buffer = (char *) xmalloc (deps_allocated_size);
  2457.     deps_buffer[0] = 0;
  2458.     deps_size = 0;
  2459.     deps_column = 0;
  2460.  
  2461.     if (deps_target) {
  2462.       deps_output (deps_target, ':');
  2463.     } else if (*in_fname == 0) {
  2464.       deps_output ("-", ':');
  2465.     } else {
  2466.       char *p, *q;
  2467.       int len;
  2468.  
  2469.       /* Discard all directory prefixes from filename.  */
  2470.       if ((q = rindex (in_fname, '/')) != NULL)
  2471.     ++q;
  2472.       else
  2473.     q = in_fname;
  2474.  
  2475.       /* Copy remainder to mungable area.  */
  2476.       p = (char *) alloca (strlen(q) + 8);
  2477.       strcpy (p, q);
  2478.  
  2479.       /* Output P, but remove known suffixes.  */
  2480.       len = strlen (p);
  2481.       q = p + len;
  2482.       if (len >= 2
  2483.       && p[len - 2] == '.'
  2484.       && index("cCsSm", p[len - 1]))
  2485.     q = p + (len - 2);
  2486.       else if (len >= 3
  2487.            && p[len - 3] == '.'
  2488.            && p[len - 2] == 'c'
  2489.            && p[len - 1] == 'c')
  2490.     q = p + (len - 3);
  2491.       else if (len >= 4
  2492.            && p[len - 4] == '.'
  2493.            && p[len - 3] == 'c'
  2494.            && p[len - 2] == 'x'
  2495.            && p[len - 1] == 'x')
  2496.     q = p + (len - 4);
  2497.       else if (len >= 4
  2498.            && p[len - 4] == '.'
  2499.            && p[len - 3] == 'c'
  2500.            && p[len - 2] == 'p'
  2501.            && p[len - 1] == 'p')
  2502.     q = p + (len - 4);
  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 (beg_of_line != ibp && 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.          /* Returns -2 if impfile set */
  4718.          f = lookup_import (path, pstatbuf, &impfile);
  4719.       } else {
  4720.          redundant_include_p (path, &impfile);
  4721.          if (impfile)
  4722.             f = -2;
  4723.          else
  4724.             f = open (path, O_RDONLY, 0666);
  4725.       }
  4726.       if (f == -2) {
  4727.          if ((importing && impfile->visited_within_module)
  4728.             || (!importing 
  4729.                 && ((!segregate_output && impfile->name->control_macro) 
  4730.                    || (segregate_output && impfile->name->control_macro 
  4731.                                         && impfile->visited_within_module))))
  4732.                return REDUNDANT_HEADER;
  4733.       }
  4734.       else if (f == -1) {
  4735.          add_to_negative_cache(savestring(path));
  4736.       }
  4737.       else if (!importing) { /* handle the "once idiom" */
  4738.          if (impfile && impfile->name->control_macro &&
  4739.              lookup (impfile->name->control_macro, -1, -1))
  4740.             return REDUNDANT_HEADER;
  4741.       }
  4742.       if (impfile) {
  4743.          *pf = f;
  4744.          *pimpfile = impfile;
  4745.          return GOOD_HEADER;
  4746.       }
  4747.       if (f >= 0) {
  4748.          if (!importing)
  4749.             fstat (f, pstatbuf);
  4750.          *pf = f;
  4751.          *pimpfile = impfile;
  4752.          return GOOD_HEADER;
  4753.       }
  4754.       return NON_EXISTENT_HEADER;
  4755.    }
  4756. }
  4757.  
  4758. /* Routines to handle #directives */
  4759.  
  4760. /* Handle #include and #import.
  4761.    This function expects to see "fname" or <fname> on the input.  */
  4762.  
  4763. static int
  4764. do_include (buf, limit, op, keyword)
  4765.      U_CHAR *buf, *limit;
  4766.      FILE_BUF *op;
  4767.      struct directive *keyword;
  4768. {
  4769.   int importing = (keyword->type == T_IMPORT);
  4770.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  4771. #ifdef NEXT_OBJC_RUNTIME
  4772.   static int import_warning = 1;
  4773. #else
  4774.   static int import_warning = 0;
  4775. #endif
  4776.   char *fname;        /* Dynamically allocated fname buffer */
  4777.   char *pcftry;
  4778.   char *pcfname;
  4779.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  4780. #ifdef NEXT_FRAMEWORKS
  4781.   char extendedPath[MAX_FILENAME_LEN];
  4782.   U_CHAR *bufptr;
  4783.   int hasSlash;    
  4784. #endif
  4785.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  4786.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  4787.   struct file_name_list *searchptr = 0;
  4788.   int flen;
  4789.  
  4790.   int f;            /* file number */
  4791.  
  4792.   int retried = 0;        /* Have already tried macro
  4793.                    expanding the include line*/
  4794.   FILE_BUF trybuf;        /* It got expanded into here */
  4795.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  4796.   int pcf = -1;
  4797.   char *pcfbuf;
  4798.   int pcfbuflimit;
  4799.   int pcfnum;
  4800. #ifdef NEXT_OBJC_RUNTIME
  4801.   U_CHAR *partial = NULL;
  4802.   int plen = 0;
  4803. #endif
  4804.   header_type hdr_result; 
  4805.   struct stat shared_stat_f;
  4806.   struct import_file *impfile = 0;
  4807.  
  4808.   f= -1;            /* JF we iz paranoid! */
  4809.  
  4810.   if (importing && warn_import && !inhibit_warnings
  4811.       && !instack[indepth].system_header_p && !import_warning) {
  4812.     import_warning = 1;
  4813.     warning ("using `#import' is not recommended");
  4814.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  4815.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  4816.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  4817.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  4818.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  4819.     fprintf (stderr, "  ... <real contents of file> ...\n");
  4820.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  4821.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  4822.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  4823.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  4824.   }
  4825.  
  4826. get_filename:
  4827.  
  4828.   fbeg = buf;
  4829.   SKIP_WHITE_SPACE (fbeg);
  4830.   /* Discard trailing whitespace so we can easily see
  4831.      if we have parsed all the significant chars we were given.  */
  4832.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  4833.  
  4834.   switch (*fbeg++) {
  4835.   case '\"':
  4836.     {
  4837.       FILE_BUF *fp;
  4838.       /* Copy the operand text, concatenating the strings.  */
  4839.       {
  4840.     U_CHAR *fin = fbeg;
  4841.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  4842.     fend = fbeg;
  4843.     while (fin != limit) {
  4844.       while (fin != limit && *fin != '\"')
  4845.         *fend++ = *fin++;
  4846.       fin++;
  4847.       if (fin == limit)
  4848.         break;
  4849.       /* If not at the end, there had better be another string.  */
  4850.       /* Skip just horiz space, and don't go past limit.  */
  4851.       while (fin != limit && is_hor_space[*fin]) fin++;
  4852.       if (fin != limit && *fin == '\"')
  4853.         fin++;
  4854.       else
  4855.         goto fail;
  4856.     }
  4857.       }
  4858.       *fend = 0;
  4859.  
  4860.       /* We have "filename".  Figure out directory this source
  4861.      file is coming from and put it on the front of the list. */
  4862.  
  4863.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  4864.       if (ignore_srcdir) break;
  4865.  
  4866.       for (fp = &instack[indepth]; fp >= instack; fp--)
  4867.     {
  4868.       int n;
  4869.       char *ep,*nam;
  4870.  
  4871.       if ((nam = fp->nominal_fname) != NULL) {
  4872.         /* Found a named file.  Figure out dir of the file,
  4873.            and put it in front of the search list.  */
  4874.         dsp[0].next = search_start;
  4875.         search_start = dsp;
  4876. #ifndef VMS
  4877.         ep = rindex (nam, '/');
  4878. #else                /* VMS */
  4879.         ep = rindex (nam, ']');
  4880.         if (ep == NULL) ep = rindex (nam, '>');
  4881.         if (ep == NULL) ep = rindex (nam, ':');
  4882.         if (ep != NULL) ep++;
  4883. #endif                /* VMS */,
  4884.         if (ep != NULL) {
  4885.           n = ep - nam;
  4886.           dsp[0].fname = (char *) alloca (n + 1);
  4887.           strncpy (dsp[0].fname, nam, n);
  4888.           dsp[0].fname[n] = '\0';
  4889.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  4890.         max_include_len = n + INCLUDE_LEN_FUDGE;
  4891.         } else {
  4892.           dsp[0].fname = 0; /* Current directory */
  4893.         }
  4894.         dsp[0].got_name_map = 0;
  4895.         break;
  4896.       }
  4897.     }
  4898.       break;
  4899.     }
  4900.  
  4901.   case '<':
  4902.     fend = fbeg;
  4903.     while (fend != limit && *fend != '>') fend++;
  4904.     if (*fend == '>' && fend + 1 == limit) {
  4905.       angle_brackets = 1;
  4906.       flen = fend -fbeg;
  4907. #ifdef NEXT_FRAMEWORKS
  4908.       hasSlash = 0;
  4909.       for (bufptr = fbeg; bufptr != fend; bufptr++)
  4910.           if (*bufptr == '/') {
  4911.              hasSlash++;
  4912.            break;
  4913.         }
  4914.        if (hasSlash) {
  4915.          extendedPath[0] = '\0';
  4916.          for (searchptr = framework;
  4917.               f < 0 && searchptr != 0;
  4918.               searchptr = searchptr->next) {
  4919.             U_CHAR *end, *headerNameTo, *headerNameFrom;
  4920.             int index;
  4921.             
  4922.             /* path to framework */
  4923.             strcpy(extendedPath, searchptr->fname);
  4924.             end = &extendedPath[strlen(extendedPath)];
  4925.             *end++ = '/';
  4926.  
  4927.             /* add framework directory */
  4928.             for (bufptr = fbeg;
  4929.                  *bufptr != '/' && bufptr != fend;
  4930.                  bufptr++)
  4931.                *end++ = *bufptr;
  4932.             strncpy(end, ".framework/", 11);   end += 11;
  4933.             
  4934.             for (index = 0; framework_header_dirs[index].dirName; index++) {
  4935.                /* add path to headers inside framework */
  4936.                strncpy(end,
  4937.                        framework_header_dirs[index].dirName,
  4938.                        framework_header_dirs[index].dirNameLen);
  4939.                /* add name of header */
  4940.                headerNameTo = end + framework_header_dirs[index].dirNameLen;
  4941.                for (headerNameFrom = bufptr; headerNameFrom != fend; headerNameFrom++)
  4942.                   *headerNameTo++ = *headerNameFrom;
  4943.                *headerNameTo = '\0';
  4944.                /* Try looking for the file */
  4945. #ifdef NEXT_CPP_SERVER
  4946.                hdr_result = try_include_file(extendedPath, importing, &f, &impfile, &shared_stat_f);
  4947.                if (hdr_result == REDUNDANT_HEADER)
  4948.                   return 0;
  4949.                else if (hdr_result == GOOD_HEADER)
  4950.                   break;
  4951. #if 0
  4952.                /* lookup in negative cache...opens/stats are painfully slow */
  4953.                if (!found_in_negative_cache(extendedPath)) {
  4954.                   impfile = 0;
  4955.                   if (importing)
  4956.                      f = lookup_import (extendedPath, &shared_stat_f, &impfile);
  4957.                   else {
  4958.                      redundant_include_p (extendedPath, &impfile);
  4959.                      if (!impfile)
  4960.                         f = open (extendedPath, O_RDONLY, 0666);
  4961.                      else
  4962.                         f = -2;
  4963.                   }
  4964.                   if (f == -2) {
  4965.                      if (segregate_output &&
  4966.                          impfile->visited_within_module &&
  4967.                          impfile->name &&
  4968.                          !impfile->name->has_explicit_context_dependency) {
  4969.                         return 0;         /* Already included this file */
  4970.                      }
  4971.                   }
  4972.                   else if (f == -1) {
  4973.                      add_to_negative_cache(savestring(extendedPath));
  4974.                   }
  4975.                   else if (!importing) { /* handle the "once idiom" */
  4976.                      if (impfile && impfile->name->control_macro &&
  4977.                          lookup (impfile->name->control_macro, -1, -1))
  4978.                         return 0;
  4979.                   }
  4980.                   if (f >= 0 || impfile) {
  4981.                      if (!importing)
  4982.                         fstat (f, &shared_stat_f);
  4983.                      break;
  4984.                   }
  4985.                } /* else, found in negative cache...don't bother */
  4986. #endif 0
  4987. #else
  4988.                if (importing)
  4989.                   f = lookup_import (extendedPath, searchptr);
  4990.                else
  4991.                   f = open_include_file (extendedPath, searchptr);
  4992.                if (f == -2)
  4993.                   return 0;            /* Already included this file */
  4994. #ifdef EACCES
  4995.                  else if (f == -1 && errno == EACCES)
  4996.                      warning ("Header file %s exists, but is not readable",
  4997.                               extendedPath);
  4998. #endif
  4999.                  if (redundant_include_p (extendedPath)) {
  5000.                      close (f);
  5001.                      return 0;
  5002.                  }
  5003.                  if (f >= 0)
  5004.                      break;
  5005. #endif
  5006.                  
  5007.              }
  5008.           }
  5009.       }
  5010. #endif
  5011.       /* If -I-, start with the first -I dir after the -I-.  */
  5012.       if (first_bracket_include)
  5013.           search_start = first_bracket_include;
  5014.       break;
  5015.     }
  5016.     goto fail;
  5017.  
  5018.   default:
  5019. #ifdef VMS
  5020.     /*
  5021.      * Support '#include xyz' like VAX-C to allow for easy use of all the
  5022.      * decwindow include files. It defaults to '#include <xyz.h>' (so the
  5023.      * code from case '<' is repeated here) and generates a warning.
  5024.      */
  5025.     if (isalpha(*(--fbeg))) {
  5026.       fend = fbeg;
  5027.       while (fend != limit && (!isspace(*fend))) fend++;
  5028.       warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
  5029.       if (fend  == limit) {
  5030.     angle_brackets = 1;
  5031.     /* If -I-, start with the first -I dir after the -I-.  */
  5032.     if (first_bracket_include)
  5033.       search_start = first_bracket_include;
  5034.     break;
  5035.       }
  5036.     }
  5037. #endif
  5038.  
  5039.   fail:
  5040.     if (retried) {
  5041.       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
  5042.       return 0;
  5043.     } else {
  5044.       trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  5045.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  5046.       bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  5047.       limit = buf + (trybuf.bufp - trybuf.buf);
  5048.       free (trybuf.buf);
  5049.       retried++;
  5050.       goto get_filename;
  5051.     }
  5052.   } /* switch (*fbeg++) */
  5053.  
  5054.   /* For #include_next, skip in the search path
  5055.      past the dir in which the containing file was found.  */
  5056.   if (skip_dirs) {
  5057.     FILE_BUF *fp;
  5058.     for (fp = &instack[indepth]; fp >= instack; fp--)
  5059.       if (fp->fname != NULL) {
  5060.     /* fp->dir is null if the containing file was specified
  5061.        with an absolute file name.  In that case, don't skip anything.  */
  5062.     if (fp->dir)
  5063.       search_start = fp->dir->next;
  5064.     break;
  5065.       }
  5066.   }
  5067.  
  5068.   flen = fend - fbeg;
  5069.  
  5070.   if (flen == 0)
  5071.     {
  5072.       error ("empty file name in `#%s'", keyword->name);
  5073.       return 0;
  5074.     }
  5075.  
  5076. #ifdef NEXT_FRAMEWORKS
  5077.   if (f > 0 || impfile) {
  5078.      fname = savestring(extendedPath);
  5079.      goto found_in_framework;
  5080.   }
  5081. #endif
  5082.  
  5083.   /* Allocate this permanently, because it gets stored in the definitions
  5084.      of macros.  */
  5085.   fname = (char *) xmalloc (max_include_len + flen + 4);
  5086.   /* + 2 above for slash and terminating null.  */
  5087.   /* + 2 added for '.h' on VMS (to support '#include filename') */
  5088.  
  5089.   
  5090.   /* If specified file name is absolute, just open it.  */
  5091.  
  5092.   if (*fbeg == '/') {
  5093.     strncpy (fname, fbeg, flen);
  5094.     fname[flen] = 0;
  5095. #ifdef NEXT_CPP_SERVER
  5096.     if (redundant_include_p (fname, &impfile)) {
  5097.       free(fname);
  5098.       return 0;
  5099.     }
  5100.     if (importing)
  5101.       f = lookup_import (fname, &shared_stat_f, &impfile);
  5102.     else {
  5103.       f = open (fname, O_RDONLY, 0666);
  5104.       fstat (f, &shared_stat_f);
  5105.     }
  5106. #else
  5107.     if (redundant_include_p (fname))
  5108.       return 0;
  5109.     if (importing)
  5110.       f = lookup_import (fname, NULL_PTR);
  5111.     else
  5112.       f = open_include_file (fname, NULL_PTR);
  5113. #endif
  5114.     if (f == -2) {
  5115.       free(fname);
  5116.       return 0;        /* Already included this file */
  5117.     }
  5118.   } else {
  5119.     /* Search directory path, trying to open the file.
  5120.        Copy each filename tried into FNAME.  */
  5121.  
  5122.       for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  5123.       if (searchptr->fname) {
  5124.     /* The empty string in a search path is ignored.
  5125.        This makes it possible to turn off entirely
  5126.        a standard piece of the list.  */
  5127.     if (searchptr->fname[0] == 0)
  5128.       continue;
  5129.     strcpy (fname, searchptr->fname);
  5130.     strcat (fname, "/");
  5131.     fname[strlen (fname) + flen] = 0;
  5132.       } else {
  5133.     fname[0] = 0;
  5134.       }
  5135.       strncat (fname, fbeg, flen);
  5136. #ifdef VMS
  5137.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  5138.          full VMS file specification */
  5139.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  5140.     /* Fix up the filename */
  5141.     hack_vms_include_specification (fname);
  5142.       } else {
  5143.           /* This is a normal VMS filespec, so use it unchanged.  */
  5144.     strncpy (fname, fbeg, flen);
  5145.     fname[flen] = 0;
  5146.     /* if it's '#include filename', add the missing .h */
  5147.     if (index(fname,'.')==NULL) {
  5148.       strcat (fname, ".h");
  5149.     }
  5150.       }
  5151. #endif /* VMS */
  5152. #ifdef NEXT_CPP_SERVER
  5153.       hdr_result = try_include_file(fname, importing, &f, &impfile, &shared_stat_f);
  5154.       if (hdr_result == REDUNDANT_HEADER) {
  5155.          free(fname);
  5156.          return 0;
  5157.       }
  5158.       else if (hdr_result == GOOD_HEADER)
  5159.          break;
  5160. #if 0
  5161.       /* lookup in negative cache...opens/stats are painfully slow */
  5162.       if (!found_in_negative_cache(fname)) {
  5163.         impfile = 0;
  5164.         if (importing)
  5165.       f = lookup_import (fname, &shared_stat_f, &impfile);
  5166.         else {
  5167.           redundant_include_p (fname, &impfile);
  5168.           if (!impfile)
  5169.         f = open (fname, O_RDONLY, 0666);
  5170.           else
  5171.              f = -2;
  5172.         }
  5173.         if (f == -2) {
  5174.           if (segregate_output &&
  5175.               impfile->visited_within_module &&
  5176.               impfile->name && !impfile->name->has_explicit_context_dependency) {
  5177.         free(fname);
  5178.         return 0;            /* Already included this file */
  5179.       }
  5180.         }
  5181.         else if (f == -1) {
  5182.           add_to_negative_cache(savestring(fname));
  5183.         }
  5184.         else if (!importing) { /* handle the "once idiom" */
  5185.           if (impfile && impfile->name->control_macro && 
  5186.               lookup (impfile->name->control_macro, -1, -1)) {
  5187.         free(fname);
  5188.         return 0;
  5189.       }
  5190.         }
  5191.         if (f >= 0 || impfile) {
  5192.           if (!importing)
  5193.             fstat (f, &shared_stat_f);
  5194.       break;
  5195.         }
  5196.       } /* else, found in negative cache...don't bother */
  5197. #endif 0
  5198. #else
  5199.       if (importing)
  5200.     f = lookup_import (fname, searchptr);
  5201.       else
  5202.     f = open_include_file (fname, searchptr);
  5203.       if (f == -2)
  5204.     return 0;            /* Already included this file */
  5205. #ifdef EACCES
  5206.       else if (f == -1 && errno == EACCES)
  5207.     warning ("Header file %s exists, but is not readable", fname);
  5208. #endif
  5209.       if (redundant_include_p (fname)) {
  5210.     close (f);
  5211.     return 0;
  5212.       }
  5213.       if (f >= 0)
  5214.     break;
  5215. #endif
  5216.     }
  5217.   }
  5218. #ifdef NEXT_FRAMEWORKS
  5219. found_in_framework:
  5220. #endif
  5221.      
  5222. #ifdef NEXT_CPP_SERVER
  5223.   if (f < 0 && !impfile) {
  5224. #else
  5225.   if (f < 0) {
  5226. #endif
  5227.     /* A file that was not found.  */
  5228.  
  5229.     strncpy (fname, fbeg, flen);
  5230.     fname[flen] = 0;
  5231.     /* If generating dependencies and -MG was specified, we assume missing
  5232.        files are leaf files, living in the same directory as the source file
  5233.        or other similar place; these missing files may be generated from
  5234.        other files and may not exist yet (eg: y.tab.h).  */
  5235.     if (print_deps_missing_files
  5236.     && print_deps > (angle_brackets || (system_include_depth > 0)))
  5237.       {
  5238.     /* If it was requested as a system header file,
  5239.        then assume it belongs in the first place to look for such.  */
  5240.     if (angle_brackets)
  5241.       {
  5242.         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
  5243.           {
  5244.         if (searchptr->fname)
  5245.           {
  5246.             char *p;
  5247.  
  5248.             if (searchptr->fname[0] == 0)
  5249.               continue;
  5250.             p = xmalloc (strlen (searchptr->fname)
  5251.                  + strlen (fname) + 2);
  5252.             strcpy (p, searchptr->fname);
  5253.             strcat (p, "/");
  5254.             strcat (p, fname);
  5255.             deps_output (p, ' ');
  5256.             break;
  5257.           }
  5258.           }
  5259.       }
  5260.     else
  5261.       {
  5262.         /* Otherwise, omit the directory, as if the file existed
  5263.            in the directory with the source.  */
  5264.         deps_output (fname, ' ');
  5265.       }
  5266.       }
  5267.     /* If -M was specified, and this header file won't be added to the
  5268.        dependency list, then don't count this as an error, because we can
  5269.        still produce correct output.  Otherwise, we can't produce correct
  5270.        output, because there may be dependencies we need inside the missing
  5271.        file, and we don't know what directory this missing file exists in.  */
  5272.     else if (print_deps
  5273.     && (print_deps <= (angle_brackets || (system_include_depth > 0))))
  5274.       warning ("No include path in which to find %s", fname);
  5275.     else if (search_start)
  5276.       error_from_errno (fname);
  5277.     else
  5278.       error ("No include path in which to find %s", fname);
  5279.   } else {
  5280.     struct stat stat_f;
  5281.  
  5282.     /* Check to see if this include file is a once-only include file.
  5283.        If so, give up.  */
  5284.  
  5285.     struct file_name_list* ptr;
  5286.  
  5287.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  5288.       if (!strcmp (ptr->fname, fname)) {
  5289.     close (f);
  5290.         return 0;                /* This file was once'd. */
  5291.       }
  5292.     }
  5293. #ifdef NEXT_CPP_SERVER
  5294.     if (impfile) {
  5295.       ptr = impfile->name;
  5296.     }
  5297. #else
  5298.     /* this is *very* slow operation when n gets large */
  5299.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  5300.       if (!strcmp (ptr->fname, fname))
  5301.         break;                /* This file was included before. */
  5302.     }
  5303. #endif
  5304.     if (ptr == 0) {
  5305.       /* This is the first time for this file.  */
  5306.       /* Add it to list of files included.  */
  5307.  
  5308.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  5309. #ifdef NEXT_CPP_SERVER
  5310.       bzero (ptr, sizeof (struct file_name_list));
  5311. #else
  5312.       ptr->control_macro = 0;
  5313.       ptr->c_system_include_path = 0;
  5314. #endif
  5315.       ptr->next = all_include_files;
  5316.       all_include_files = ptr;
  5317.       ptr->fname = savestring (fname);
  5318.       ptr->got_name_map = 0;
  5319.  
  5320.       /* For -M, add this file to the dependencies.  */
  5321.       if (print_deps > (angle_brackets || (system_include_depth > 0)))
  5322.     deps_output (fname, ' ');
  5323.     }   
  5324.  
  5325.     /* Handle -H option.  */
  5326.     if (print_include_names) {
  5327.       output_dots (stderr, indepth);
  5328.       fprintf (stderr, "%s\n", fname);
  5329.     }
  5330.  
  5331.     if (angle_brackets)
  5332.       system_include_depth++;
  5333.  
  5334. #ifdef NEXT_CPP_SERVER
  5335.     if (impfile) {
  5336.       /* this allows the hash table to be reused across several runs of cpp */
  5337.       impfile->visited_within_module++;
  5338.     } else
  5339.       /* Actually process the file.  */
  5340.       impfile = add_import (f, ptr, &shared_stat_f);
  5341.  
  5342.     if (!importing)
  5343.       ptr->ever_included = 1;
  5344. #else
  5345.     /* Actually process the file.  */
  5346.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  5347. #endif
  5348.  
  5349.     pcftry = (char *) alloca (strlen (fname) + 30);
  5350.     pcfbuf = 0;
  5351.     pcfnum = 0;
  5352.  
  5353. #ifdef NEXT_CPP_SERVER
  5354.     if (!no_precomp) {
  5355.       fstat (f, &stat_f);
  5356. #else
  5357.     fstat (f, &stat_f);
  5358.     if (!no_precomp)
  5359. #endif
  5360.       do {
  5361.     sprintf (pcftry, "%s%d", fname, pcfnum++);
  5362.     
  5363.     pcf = open (pcftry, O_RDONLY, 0666);
  5364.     if (pcf != -1)
  5365.       {
  5366.         struct stat s;
  5367.  
  5368.         fstat (pcf, &s);
  5369.         if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
  5370.               sizeof (s.st_ino))
  5371.         || stat_f.st_dev != s.st_dev)
  5372.           {
  5373.         pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  5374.         /* Don't need it any more.  */
  5375.         close (pcf);
  5376.           }
  5377.         else
  5378.           {
  5379.         /* Don't need it at all.  */
  5380.         close (pcf);
  5381.         break;
  5382.           }
  5383.       }
  5384.       } while (pcf != -1 && !pcfbuf);
  5385. #ifdef NEXT_CPP_SERVER
  5386.     }
  5387.     /* Actually process the file */
  5388.     if (pcfbuf) {
  5389.       pcfname = xmalloc (strlen (pcftry) + 1);
  5390.       strcpy (pcfname, pcftry);
  5391.       pcfinclude (pcfbuf, pcfbuflimit, ptr->fname, op);
  5392.     }
  5393.     else {
  5394.       finclude (f, ptr->fname, op, is_system_include (ptr->fname), searchptr, 
  5395.                 ptr, &shared_stat_f);
  5396.     }
  5397. #else
  5398.     /* Actually process the file */
  5399.     if (pcfbuf) {
  5400.       pcfname = xmalloc (strlen (pcftry) + 1);
  5401.       strcpy (pcfname, pcftry);
  5402.       pcfinclude (pcfbuf, pcfbuflimit, fname, op);
  5403.     }
  5404.     else
  5405.       finclude (f, fname, op, is_system_include (fname), searchptr);
  5406. #endif
  5407.  
  5408.     if (angle_brackets)
  5409.       system_include_depth--;
  5410.   }
  5411.  
  5412.   free (fname); 
  5413.  
  5414.   return 0;
  5415. }
  5416.  
  5417. /* Return nonzero if there is no need to include file NAME
  5418.    because it has already been included and it contains a conditional
  5419.    to make a repeated include do nothing.  */
  5420.  
  5421. #ifdef NEXT_CPP_SERVER
  5422. static int
  5423. redundant_include_p (name, impfile)
  5424.      char *name;
  5425.      struct import_file **impfile; /* output parm if found */
  5426. {
  5427.   struct import_file *i;
  5428.   int hashval = import_hash (name);
  5429.  
  5430.   /* Attempt to find file in list of already included files */
  5431.   i = import_hash_table[hashval];
  5432.  
  5433.   while (i) {
  5434.     if (!strcmp (name, i->name->fname)) {
  5435.       *impfile = i;
  5436.       if (i->name->control_macro && lookup (i->name->control_macro, -1, -1))
  5437.         return 1;
  5438.       else
  5439.         return 0;
  5440.     }
  5441.     i = i->next;
  5442.   }
  5443.   return 0;
  5444. }
  5445. #else
  5446. static int
  5447. redundant_include_p (name)
  5448.      char *name;
  5449. {
  5450.   struct file_name_list *l = all_include_files;
  5451.   for (; l; l = l->next)
  5452.     if (! strcmp (name, l->fname)
  5453.     && l->control_macro
  5454.     && lookup (l->control_macro, -1, -1))
  5455.       return 1;
  5456.   return 0;
  5457. }
  5458. #endif
  5459.  
  5460. /* Return nonzero if the given FILENAME is an absolute pathname which
  5461.    designates a file within one of the known "system" include file
  5462.    directories.  We assume here that if the given FILENAME looks like
  5463.    it is the name of a file which resides either directly in a "system"
  5464.    include file directory, or within any subdirectory thereof, then the
  5465.    given file must be a "system" include file.  This function tells us
  5466.    if we should suppress pedantic errors/warnings for the given FILENAME.
  5467.  
  5468.    The value is 2 if the file is a C-language system header file
  5469.    for which C++ should (on most systems) assume `extern "C"'.  */
  5470.  
  5471. static int
  5472. is_system_include (filename)
  5473.     register char *filename;
  5474. {
  5475.   struct file_name_list *searchptr;
  5476.  
  5477.   for (searchptr = first_system_include; searchptr;
  5478.        searchptr = searchptr->next)
  5479.     if (searchptr->fname) {
  5480.       register char *sys_dir = searchptr->fname;
  5481.       register unsigned length = strlen (sys_dir);
  5482.  
  5483.       if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
  5484.     {
  5485.       if (searchptr->c_system_include_path)
  5486.         return 2;
  5487.       else
  5488.         return 1;
  5489.     }
  5490.     }
  5491.   return 0;
  5492. }
  5493.  
  5494. /* The file_name_map structure holds a mapping of file names for a
  5495.    particular directory.  This mapping is read from the file named
  5496.    FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
  5497.    map filenames on a file system with severe filename restrictions,
  5498.    such as DOS.  The format of the file name map file is just a series
  5499.    of lines with two tokens on each line.  The first token is the name
  5500.    to map, and the second token is the actual name to use.  */
  5501.  
  5502. struct file_name_map
  5503. {
  5504.   struct file_name_map *map_next;
  5505.   char *map_from;
  5506.   char *map_to;
  5507. };
  5508.  
  5509. #define FILE_NAME_MAP_FILE "header.gcc"
  5510.  
  5511. /* Read a space delimited string of unlimited length from a stdio
  5512.    file.  */
  5513.  
  5514. static char *
  5515. read_filename_string (ch, f)
  5516.      int ch;
  5517.      FILE *f;
  5518. {
  5519.   char *alloc, *set;
  5520.   int len;
  5521.  
  5522.   len = 20;
  5523.   set = alloc = xmalloc (len + 1);
  5524.   if (! is_space[ch])
  5525.     {
  5526.       *set++ = ch;
  5527.       while ((ch = getc (f)) != EOF && ! is_space[ch])
  5528.     {
  5529.       if (set - alloc == len)
  5530.         {
  5531.           len *= 2;
  5532.           alloc = xrealloc (alloc, len + 1);
  5533.           set = alloc + len / 2;
  5534.         }
  5535.       *set++ = ch;
  5536.     }
  5537.     }
  5538.   *set = '\0';
  5539.   ungetc (ch, f);
  5540.   return alloc;
  5541. }
  5542.  
  5543. /* Read the file name map file for DIRNAME.  */
  5544.  
  5545. static struct file_name_map *
  5546. read_name_map (dirname)
  5547.      char *dirname;
  5548. {
  5549.   /* This structure holds a linked list of file name maps, one per
  5550.      directory.  */
  5551.   struct file_name_map_list
  5552.     {
  5553.       struct file_name_map_list *map_list_next;
  5554.       char *map_list_name;
  5555.       struct file_name_map *map_list_map;
  5556.     };
  5557.   static struct file_name_map_list *map_list;
  5558.   register struct file_name_map_list *map_list_ptr;
  5559.   char *name;
  5560.   FILE *f;
  5561.  
  5562.   for (map_list_ptr = map_list; map_list_ptr;
  5563.        map_list_ptr = map_list_ptr->map_list_next)
  5564.     if (! strcmp (map_list_ptr->map_list_name, dirname))
  5565.       return map_list_ptr->map_list_map;
  5566.  
  5567.   map_list_ptr = ((struct file_name_map_list *)
  5568.           xmalloc (sizeof (struct file_name_map_list)));
  5569.   map_list_ptr->map_list_name = savestring (dirname);
  5570.   map_list_ptr->map_list_map = NULL;
  5571.  
  5572.   name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
  5573.   strcpy (name, dirname);
  5574.   if (*dirname)
  5575.     strcat (name, "/");
  5576.   strcat (name, FILE_NAME_MAP_FILE);
  5577.   f = fopen (name, "r");
  5578.   if (!f)
  5579.     map_list_ptr->map_list_map = NULL;
  5580.   else
  5581.     {
  5582.       int ch;
  5583.       int dirlen = strlen (dirname);
  5584.  
  5585.       while ((ch = getc (f)) != EOF)
  5586.     {
  5587.       char *from, *to;
  5588.       struct file_name_map *ptr;
  5589.  
  5590.       if (is_space[ch])
  5591.         continue;
  5592.       from = read_filename_string (ch, f);
  5593.       while ((ch = getc (f)) != EOF && is_hor_space[ch])
  5594.         ;
  5595.       to = read_filename_string (ch, f);
  5596.  
  5597.       ptr = ((struct file_name_map *)
  5598.          xmalloc (sizeof (struct file_name_map)));
  5599.       ptr->map_from = from;
  5600.  
  5601.       /* Make the real filename absolute.  */
  5602.       if (*to == '/')
  5603.         ptr->map_to = to;
  5604.       else
  5605.         {
  5606.           ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
  5607.           strcpy (ptr->map_to, dirname);
  5608.           ptr->map_to[dirlen] = '/';
  5609.           strcpy (ptr->map_to + dirlen + 1, to);
  5610.           free (to);
  5611.         }          
  5612.  
  5613.       ptr->map_next = map_list_ptr->map_list_map;
  5614.       map_list_ptr->map_list_map = ptr;
  5615.  
  5616.       while ((ch = getc (f)) != '\n')
  5617.         if (ch == EOF)
  5618.           break;
  5619.     }
  5620.       fclose (f);
  5621.     }
  5622.   
  5623.   map_list_ptr->map_list_next = map_list;
  5624.   map_list = map_list_ptr;
  5625.  
  5626.   return map_list_ptr->map_list_map;
  5627. }  
  5628.  
  5629. /* Try to open include file FILENAME.  SEARCHPTR is the directory
  5630.    being tried from the include file search path.  This function maps
  5631.    filenames on file systems based on information read by
  5632.    read_name_map.  */
  5633.  
  5634. static int
  5635. open_include_file (filename, searchptr)
  5636.      char *filename;
  5637.      struct file_name_list *searchptr;
  5638. {
  5639.   register struct file_name_map *map;
  5640.   register char *from;
  5641.   char *p, *dir;
  5642.  
  5643.   if (searchptr && ! searchptr->got_name_map)
  5644.     {
  5645.       searchptr->name_map = read_name_map (searchptr->fname
  5646.                        ? searchptr->fname : ".");
  5647.       searchptr->got_name_map = 1;
  5648.     }
  5649.  
  5650.   /* First check the mapping for the directory we are using.  */
  5651.   if (searchptr && searchptr->name_map)
  5652.     {
  5653.       from = filename;
  5654.       if (searchptr->fname)
  5655.     from += strlen (searchptr->fname) + 1;
  5656.       for (map = searchptr->name_map; map; map = map->map_next)
  5657.     {
  5658.       if (! strcmp (map->map_from, from))
  5659.         {
  5660.           /* Found a match.  */
  5661.           return open (map->map_to, O_RDONLY, 0666);
  5662.         }
  5663.     }
  5664.     }
  5665.  
  5666.   /* Try to find a mapping file for the particular directory we are
  5667.      looking in.  Thus #include <sys/types.h> will look up sys/types.h
  5668.      in /usr/include/header.gcc and look up types.h in
  5669.      /usr/include/sys/header.gcc.  */
  5670.   p = rindex (filename, '/');
  5671.   if (! p)
  5672.     p = filename;
  5673.   if (searchptr
  5674.       && searchptr->fname
  5675.       && strlen (searchptr->fname) == p - filename
  5676.       && ! strncmp (searchptr->fname, filename, p - filename))
  5677.     {
  5678.       /* FILENAME is in SEARCHPTR, which we've already checked.  */
  5679.       return open (filename, O_RDONLY, 0666);
  5680.     }
  5681.  
  5682.   if (p == filename)
  5683.     {
  5684.       dir = ".";
  5685.       from = filename;
  5686.     }
  5687.   else
  5688.     {
  5689.       dir = (char *) alloca (p - filename + 1);
  5690.       bcopy (filename, dir, p - filename);
  5691.       dir[p - filename] = '\0';
  5692.       from = p + 1;
  5693.     }
  5694.   for (map = read_name_map (dir); map; map = map->map_next)
  5695.     if (! strcmp (map->map_from, from))
  5696.       return open (map->map_to, O_RDONLY, 0666);
  5697.  
  5698.   return open (filename, O_RDONLY, 0666);
  5699. }
  5700.  
  5701. /* Process the contents of include file FNAME, already open on descriptor F,
  5702.    with output to OP.
  5703.    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
  5704.    "system" include directories (as decided by the `is_system_include'
  5705.    function above).
  5706.    DIRPTR is the link in the dir path through which this file was found,
  5707.    or 0 if the file name was absolute.  */
  5708.  
  5709. #ifdef NEXT_CPP_SERVER
  5710. static void
  5711. finclude (f, fname, op, system_header_p, dirptr, fileptr, sb)
  5712.      int f;
  5713.      char *fname;
  5714.      FILE_BUF *op;
  5715.      int system_header_p;
  5716.      struct file_name_list *dirptr;
  5717.      struct file_name_list *fileptr;
  5718.      struct stat *sb;
  5719. #else
  5720. static void
  5721. finclude (f, fname, op, system_header_p, dirptr)
  5722.      int f;
  5723.      char *fname;
  5724.      FILE_BUF *op;
  5725.      int system_header_p;
  5726.      struct file_name_list *dirptr;
  5727. #endif
  5728. {
  5729.   int st_mode;
  5730.   long st_size;
  5731.   long i;
  5732.   FILE_BUF *fp;            /* For input stack frame */
  5733.   int missing_newline = 0;
  5734. #ifdef NEXT_CPP_SERVER
  5735.   int check_for_duplicate = 0;
  5736.   U_CHAR *position_of_file_start;
  5737.   unsigned int file_length;
  5738. #ifdef SEPERATE_USER_AND_SYS_HEADERS
  5739.   FILE_BUF * header_buf = system_header_p ? &sys_header_outbuf
  5740.                                           : &user_header_outbuf;
  5741. #else
  5742.   FILE_BUF * header_buf = &sys_header_outbuf;
  5743. #endif
  5744. #endif
  5745.  
  5746.   CHECK_DEPTH (return;);
  5747.  
  5748. #ifdef NEXT_CPP_SERVER
  5749.   if (sb == 0) {
  5750.     if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  5751.       goto nope;        /* Impossible? */
  5752.   } else { /* don't redo any stat stuff */
  5753.     st_mode = sb->st_mode;
  5754.     st_size = sb->st_size;
  5755.   }
  5756. #else
  5757.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  5758.     {
  5759.       perror_with_name (fname);
  5760.       close (f);
  5761.       return;
  5762.     }
  5763. #endif
  5764.   fp = &instack[indepth + 1];
  5765.   bzero ((char *) fp, sizeof (FILE_BUF));
  5766.   fp->nominal_fname = fp->fname = fname;
  5767.   fp->length = 0;
  5768.   fp->lineno = 1;
  5769.   fp->if_stack = if_stack;
  5770.   fp->system_header_p = system_header_p;
  5771.   fp->dir = dirptr;
  5772. #ifdef NEXT_CPP_SERVER
  5773.   fp->file = fileptr;
  5774.   if (fileptr && fileptr->buf && fileptr->length) {
  5775.     /*fprintf(stderr, "reusing buffer for file %s\n", fileptr->fname);*/
  5776.     fp->buf = fileptr->buf;
  5777.     fp->length = fileptr->length;
  5778.     if (fileptr->control_macro && fileptr->position_of_first_ifndef)
  5779.       /* simple optimization to avoid processing superfluous comments
  5780.          at the top of headers (60k savings for appkit.h) */
  5781.       fp->bufp = fileptr->position_of_first_ifndef;
  5782.     else
  5783.       fp->bufp = fileptr->buf;
  5784.     goto dorescan;
  5785.   }
  5786. #endif
  5787.   if (S_ISREG (st_mode)) {
  5788. #ifdef MALLOC_INPUT_BUFFERS
  5789.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  5790.     fp->bufp = fp->buf;
  5791.  
  5792.     /* Read the file contents, knowing that st_size is an upper bound
  5793.        on the number of bytes we can read.  */
  5794.     fp->length = safe_read (f, fp->buf, st_size);
  5795. #else
  5796.     if (map_fd(f, 0, &fp->buf, TRUE, st_size+2) != KERN_SUCCESS)
  5797.        goto nope;
  5798.     fp->bufp = fp->buf;
  5799.     fp->length = st_size;    
  5800. #endif    
  5801.     if (fp->length < 0) goto nope;
  5802.   }
  5803.   else if (S_ISDIR (st_mode)) {
  5804.     error ("directory `%s' specified in #include", fname);
  5805.     close (f);
  5806.     return;
  5807.   } else {
  5808.     /* Cannot count its file size before reading.
  5809.        First read the entire file into heap and
  5810.        copy them into buffer on stack. */
  5811.  
  5812.     int bsize = 2000;
  5813.  
  5814.     st_size = 0;
  5815.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  5816.  
  5817.     for (;;) {
  5818.       i = safe_read (f, fp->buf + st_size, bsize - st_size);
  5819.       if (i < 0)
  5820.     goto nope;      /* error! */
  5821.       st_size += i;
  5822.       if (st_size != bsize)
  5823.     break;    /* End of file */
  5824.       bsize *= 2;
  5825.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  5826.     }
  5827.     fp->bufp = fp->buf;
  5828.     fp->length = st_size;
  5829.   }
  5830.  
  5831.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  5832.       /* Backslash-newline at end is not good enough.  */
  5833.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  5834.     fp->buf[fp->length++] = '\n';
  5835.     missing_newline = 1;
  5836.   }
  5837.   fp->buf[fp->length] = '\0';
  5838.  
  5839.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  5840.   close (f);
  5841.  
  5842.   /* Must do this before calling trigraph_pcp, so that the correct file name
  5843.      will be printed in warning messages.  */
  5844. #ifdef NEXT_CPP_SERVER
  5845. dorescan:
  5846.  
  5847.   if (fileptr) {
  5848.      fileptr->buf = fp->buf;
  5849.      fileptr->length = fp->length;
  5850.   }
  5851. #endif
  5852.  
  5853.   indepth++;
  5854.   input_file_stack_tick++;
  5855.  
  5856.   if (!no_trigraphs)
  5857.     trigraph_pcp (fp);
  5858.  
  5859. #ifdef NEXT_SEMANTICS
  5860.   if (!no_rtf)
  5861.     buf_convert_rtf (fp);
  5862. #endif
  5863.  
  5864. #ifdef NEXT_CPP_SERVER
  5865.   if (segregate_output) {
  5866.     output_line_command (fp, &outbuf, 0, enter_file); /* serve as a "note" */
  5867.  
  5868.     /* the following is a conservative heuristic to prevent us from
  5869.        over zealous removal of headers that need to be output more than once */
  5870.     if (fileptr->header_output &&
  5871.         fileptr->has_explicit_context_dependency &&
  5872.         (fileptr->control_macro == 0 && fileptr->ever_included)) {
  5873.       check_for_duplicate = 1;
  5874.       fileptr->header_output = 0; /* has the effect of including it again */
  5875.     }
  5876.     position_of_file_start = header_buf->bufp;
  5877.     output_line_command (fp, header_buf, 0, enter_file);
  5878.     rescan (header_buf, 0);
  5879.   } else {
  5880.     output_line_command (fp, op, 0, enter_file);
  5881.     rescan (op, 0);
  5882.   }
  5883. #else
  5884.   output_line_command (fp, op, 0, enter_file);
  5885.   rescan (op, 0);
  5886. #endif
  5887.  
  5888.   if (missing_newline)
  5889.     fp->lineno--;
  5890.  
  5891.   if (pedantic && missing_newline)
  5892.     pedwarn ("file does not end in newline");
  5893.  
  5894.   indepth--;
  5895.   input_file_stack_tick++;
  5896.  
  5897. #ifdef NEXT_CPP_SERVER
  5898.   if (segregate_output) {
  5899.  
  5900.     /* It is important to compute the length of what we just output now, since
  5901.        the length should NOT include the next end marker, which includes the
  5902.        file we are returning to */
  5903.     file_length = header_buf->bufp - position_of_file_start;
  5904.  
  5905.     if (indepth)
  5906.        output_line_command (&instack[indepth], header_buf, 0, leave_file);
  5907.     else
  5908.        output_line_command (&instack[indepth], &outbuf, 0, enter_file);
  5909.  
  5910.     if (check_for_duplicate) { 
  5911.       /* file was included twice, if it is identical remove it */
  5912.       if (duplicate_version_of_header(header_buf,
  5913.              fileptr, position_of_file_start, file_length)) {
  5914.         header_buf->bufp = position_of_file_start; /* remove it */
  5915.       } else {
  5916.         add_version_of_header(fileptr, 
  5917.                         position_of_file_start - header_buf->buf,
  5918.                         file_length);
  5919.       }
  5920.       fileptr->header_output = 1; /* put back the original value */
  5921.     } else if (!fileptr->header_output) {
  5922.       /* first time this header has been output */
  5923.       fileptr->relative_position_of_file_start = 
  5924.           position_of_file_start - header_buf->buf;
  5925.       fileptr->file_length = file_length;
  5926.       fileptr->header_output = 1; 
  5927.     }
  5928.   } else {
  5929.     output_line_command (&instack[indepth], op, 0, leave_file);
  5930.   }
  5931. #else
  5932.   output_line_command (&instack[indepth], op, 0, leave_file);
  5933.   free (fp->buf);
  5934. #endif
  5935.   return;
  5936.  
  5937.  nope:
  5938.  
  5939.   perror_with_name (fname);
  5940.   close (f);
  5941.   free (fp->buf);
  5942. }
  5943.  
  5944. /* Record that inclusion of the file named FILE
  5945.    should be controlled by the macro named MACRO_NAME.
  5946.    This means that trying to include the file again
  5947.    will do something if that macro is defined.  */
  5948.  
  5949. #ifdef NEXT_CPP_SERVER
  5950. static void
  5951. record_control_macro (file, macro_name)
  5952.      char *file;
  5953.      U_CHAR *macro_name;
  5954. {
  5955.   struct import_file *impfile;
  5956.  
  5957.   redundant_include_p(file, &impfile);
  5958.   if (impfile) {
  5959.      if (impfile->name->control_macro)
  5960.         free(impfile->name->control_macro);
  5961.     impfile->name->control_macro = macro_name;
  5962.     impfile->name->position_of_last_endif = directive_start;
  5963.     return;
  5964.   }
  5965.  
  5966.   /* If the file is not in all_include_files, something's wrong.  */
  5967.   abort ();
  5968. }
  5969. #else
  5970. static void
  5971. record_control_macro (file, macro_name)
  5972.      char *file;
  5973.      U_CHAR *macro_name;
  5974. {
  5975.   struct file_name_list *new;
  5976.  
  5977.   for (new = all_include_files; new; new = new->next) {
  5978.     if (!strcmp (new->fname, file)) {
  5979.       new->control_macro = macro_name;
  5980.       return;
  5981.     }
  5982.   }
  5983.  
  5984.   /* If the file is not in all_include_files, something's wrong.  */
  5985.   abort ();
  5986. }
  5987. #endif
  5988.  
  5989. /* Hash a file name for import_hash_table.  */
  5990.  
  5991. static int 
  5992. import_hash (f)
  5993.      char *f;
  5994. {
  5995.   int val = 0;
  5996.  
  5997.   while (*f) val += *f++;
  5998.   return (val%IMPORT_HASH_SIZE);
  5999. }
  6000.  
  6001. /* Search for file FILENAME in import_hash_table.
  6002.    Return -2 if found, either a matching name or a matching inode.
  6003.    Otherwise, open the file and return a file descriptor if successful
  6004.    or -1 if unsuccessful.  */
  6005.  
  6006. #ifdef NEXT_CPP_SERVER
  6007. static int
  6008. lookup_import (filename, sb, impfile)
  6009.      char *filename;
  6010.      struct stat *sb;
  6011.      struct import_file **impfile; /* output parm if found */
  6012. {
  6013.   struct import_file *i;
  6014.   int h;
  6015.   int hashval;
  6016.   int fd;
  6017.  
  6018.   hashval = import_hash (filename);
  6019.  
  6020.   /* Attempt to find file in list of already included files */
  6021.   i = import_hash_table[hashval];
  6022.  
  6023.   while (i) {
  6024.     if (!strcmp (filename, i->name->fname)) {
  6025.       *impfile = i;
  6026.       return -2;        /* return found */
  6027.     }
  6028.     i = i->next;
  6029.   }
  6030.   /* Open it and try a match on inode/dev */
  6031.   fd = open (filename, O_RDONLY, 0666);
  6032.   if (fd < 0)
  6033.     return fd;
  6034.   fstat (fd, sb);
  6035.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  6036.     i = import_hash_table[h];
  6037.     while (i) {
  6038.       /* Compare the inode and the device.
  6039.      Supposedly on some systems the inode is not a scalar.  */
  6040.       if (i->inode == sb->st_ino && i->dev == sb->st_dev) {
  6041.         close (fd);
  6042.     *impfile = i;
  6043.         return -2;        /* return found */
  6044.       }
  6045.       i = i->next;
  6046.     }
  6047.   }
  6048.   return fd;            /* Not found, return open file */
  6049. }
  6050. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  6051.  
  6052. static struct import_file *
  6053. add_import (fd, fnamelist, sb)
  6054.      int fd;
  6055.      struct file_name_list *fnamelist;
  6056.      struct stat *sb;
  6057. {
  6058.   struct import_file *i;
  6059.   int hashval;
  6060.  
  6061.   hashval = import_hash (fnamelist->fname);
  6062.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  6063.   i->name = fnamelist;
  6064.   bcopy (&sb->st_ino, &i->inode, sizeof (sb->st_ino));
  6065.   i->modtime = sb->st_mtime;
  6066.   i->dev = sb->st_dev;
  6067.   i->next = import_hash_table[hashval];
  6068.   i->visited_within_module = 1;
  6069.   import_hash_table[hashval] = i;
  6070.   return i;
  6071. }
  6072.  
  6073. #else
  6074. static int
  6075. lookup_import (filename, searchptr)
  6076.      char *filename;
  6077.      struct file_name_list *searchptr;
  6078. {
  6079.   struct import_file *i;
  6080.   int h;
  6081.   int hashval;
  6082.   struct stat sb;
  6083.   int fd;
  6084.  
  6085.   hashval = import_hash (filename);
  6086.  
  6087.   /* Attempt to find file in list of already included files */
  6088.   i = import_hash_table[hashval];
  6089.  
  6090.   while (i) {
  6091.     if (!strcmp (filename, i->name))
  6092.       return -2;        /* return found */
  6093.     i = i->next;
  6094.   }
  6095.   /* Open it and try a match on inode/dev */
  6096.   fd = open_include_file (filename, searchptr);
  6097.   if (fd < 0)
  6098.     return fd;
  6099.   fstat (fd, &sb);
  6100.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  6101.     i = import_hash_table[h];
  6102.     while (i) {
  6103.       /* Compare the inode and the device.
  6104.      Supposedly on some systems the inode is not a scalar.  */
  6105.       if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
  6106.       && i->dev == sb.st_dev) {
  6107.         close (fd);
  6108.         return -2;        /* return found */
  6109.       }
  6110.       i = i->next;
  6111.     }
  6112.   }
  6113.   return fd;            /* Not found, return open file */
  6114. }
  6115.  
  6116. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  6117.  
  6118. static void
  6119. add_import (fd, fname)
  6120.      int fd;
  6121.      char *fname;
  6122. {
  6123.   struct import_file *i;
  6124.   int hashval;
  6125.   struct stat sb;
  6126.  
  6127.   hashval = import_hash (fname);
  6128.   fstat (fd, &sb);
  6129.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  6130.   i->name = (char *)xmalloc (strlen (fname)+1);
  6131.   strcpy (i->name, fname);
  6132.   bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
  6133.   i->dev = sb.st_dev;
  6134.   i->next = import_hash_table[hashval];
  6135.   import_hash_table[hashval] = i;
  6136. }
  6137. #endif
  6138.  
  6139. /* Load the specified precompiled header into core, and verify its
  6140.    preconditions.  PCF indicates the file descriptor to read, which must
  6141.    be a regular file.  FNAME indicates the file name of the original 
  6142.    header.  *LIMIT will be set to an address one past the end of the file.
  6143.    If the preconditions of the file are not satisfied, the buffer is 
  6144.    freed and we return 0.  If the preconditions are satisfied, return
  6145.    the address of the buffer following the preconditions.  The buffer, in
  6146.    this case, should never be freed because various pieces of it will
  6147.    be referred to until all precompiled strings are output at the end of
  6148.    the run.
  6149. */
  6150. static char *
  6151. check_precompiled (pcf, fname, limit)
  6152.      int pcf;
  6153.      char *fname;
  6154.      char **limit;
  6155. {
  6156.   int st_mode;
  6157.   long st_size;
  6158.   int length = 0;
  6159.   char *buf;
  6160.   char *cp;
  6161.  
  6162.   if (pcp_outfile)
  6163.     return 0;
  6164.   
  6165.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  6166.     return 0;
  6167.  
  6168.   if (S_ISREG (st_mode))
  6169.     {
  6170.       buf = xmalloc (st_size + 2);
  6171.       length = safe_read (pcf, buf, st_size);
  6172.       if (length < 0)
  6173.     goto nope;
  6174.     }
  6175.   else
  6176.     abort ();
  6177.     
  6178.   if (length > 0 && buf[length-1] != '\n')
  6179.     buf[length++] = '\n';
  6180.   buf[length] = '\0';
  6181.   
  6182.   *limit = buf + length;
  6183.  
  6184.   /* File is in core.  Check the preconditions. */
  6185.   if (!check_preconditions (buf))
  6186.     goto nope;
  6187.   for (cp = buf; *cp; cp++)
  6188.     ;
  6189. #ifdef DEBUG_PCP
  6190.   fprintf (stderr, "Using preinclude %s\n", fname);
  6191. #endif
  6192.   return cp + 1;
  6193.  
  6194.  nope:
  6195. #ifdef DEBUG_PCP
  6196.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  6197. #endif
  6198.   free (buf);
  6199.   return 0;
  6200. }
  6201.  
  6202. /* PREC (null terminated) points to the preconditions of a
  6203.    precompiled header.  These are a series of #define and #undef
  6204.    lines which must match the current contents of the hash
  6205.    table.  */
  6206. static int 
  6207. check_preconditions (prec)
  6208.      char *prec;
  6209. {
  6210.   MACRODEF mdef;
  6211.   char *lineend;
  6212.   
  6213.   while (*prec) {
  6214.     lineend = (char *) index (prec, '\n');
  6215.     
  6216.     if (*prec++ != '#') {
  6217.       error ("Bad format encountered while reading precompiled file");
  6218.       return 0;
  6219.     }
  6220.     if (!strncmp (prec, "define", 6)) {
  6221.       HASHNODE *hp;
  6222.       int hashcode;
  6223.       
  6224.       prec += 6;
  6225. #ifdef NEXT_CPP_SERVER
  6226.       mdef = create_definition (prec, lineend, NULL_PTR, &hashcode, 0);
  6227. #else
  6228.       mdef = create_definition (prec, lineend, NULL_PTR);
  6229. #endif
  6230.       if (mdef.defn == 0)
  6231.     abort ();
  6232.       
  6233. #ifdef NEXT_CPP_SERVER
  6234.       if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) == NULL
  6235. #else
  6236.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  6237. #endif
  6238.       || (hp->type != T_MACRO && hp->type != T_CONST)
  6239.       || (hp->type == T_MACRO
  6240.           && !compare_defs (mdef.defn, hp->value.defn)
  6241.           && (mdef.defn->length != 2
  6242.           || mdef.defn->expansion[0] != '\n'
  6243.           || mdef.defn->expansion[1] != ' ')))
  6244.     return 0;
  6245.     } else if (!strncmp (prec, "undef", 5)) {
  6246.       char *name;
  6247.       int len;
  6248.       
  6249.       prec += 5;
  6250.       while (is_hor_space[(U_CHAR) *prec])
  6251.     prec++;
  6252.       name = prec;
  6253.       while (is_idchar[(U_CHAR) *prec])
  6254.     prec++;
  6255.       len = prec - name;
  6256.       
  6257.       if (lookup (name, len, -1))
  6258.     return 0;
  6259.     } else {
  6260.       error ("Bad format encountered while reading precompiled file");
  6261.       return 0;
  6262.     }
  6263.     prec = lineend + 1;
  6264.   }
  6265.   /* They all passed successfully */
  6266.   return 1;
  6267. }
  6268.  
  6269. /* Process the main body of a precompiled file.  BUF points to the
  6270.    string section of the file, following the preconditions.  LIMIT is one
  6271.    character past the end.  NAME is the name of the file being read
  6272.    in.  OP is the main output buffer */
  6273. static void
  6274. pcfinclude (buf, limit, name, op)
  6275.      U_CHAR *buf, *limit, *name;
  6276.      FILE_BUF *op;
  6277. {
  6278.   FILE_BUF tmpbuf;
  6279.   int nstrings;
  6280.   U_CHAR *cp = buf;
  6281.  
  6282.   /* First in the file comes 4 bytes indicating the number of strings, */
  6283.   /* in network byte order. (MSB first).  */
  6284.   nstrings = *cp++;
  6285.   nstrings = (nstrings << 8) | *cp++;
  6286.   nstrings = (nstrings << 8) | *cp++;
  6287.   nstrings = (nstrings << 8) | *cp++;
  6288.   
  6289.   /* Looping over each string... */
  6290.   while (nstrings--) {
  6291.     U_CHAR *string_start;
  6292.     U_CHAR *endofthiskey;
  6293.     STRINGDEF *str;
  6294.     int nkeys;
  6295.     
  6296.     /* Each string starts with a STRINGDEF structure (str), followed */
  6297.     /* by the text of the string (string_start) */
  6298.  
  6299.     /* First skip to a longword boundary */
  6300.     /* ??? Why a 4-byte boundary?  On all machines? */
  6301.     /* NOTE: This works correctly even if HOST_WIDE_INT
  6302.        is narrower than a pointer.
  6303.        Do not try risky measures here to get another type to use!
  6304.        Do not include stddef.h--it will fail!  */
  6305.     if ((HOST_WIDE_INT) cp & 3)
  6306.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  6307.     
  6308.     /* Now get the string. */
  6309.     str = (STRINGDEF *) cp;
  6310.     string_start = cp += sizeof (STRINGDEF);
  6311.     
  6312.     for (; *cp; cp++)        /* skip the string */
  6313.       ;
  6314.     
  6315.     /* We need to macro expand the string here to ensure that the
  6316.        proper definition environment is in place.  If it were only
  6317.        expanded when we find out it is needed, macros necessary for
  6318.        its proper expansion might have had their definitions changed. */
  6319.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  6320.     /* Lineno is already set in the precompiled file */
  6321.     str->contents = tmpbuf.buf;
  6322.     str->len = tmpbuf.length;
  6323.     str->writeflag = 0;
  6324.     str->filename = name;
  6325. #ifdef NEXT_CPP_SERVER
  6326.     str->output_mark = op->bufp - op->buf;
  6327. #else
  6328.     str->output_mark = outbuf.bufp - outbuf.buf;
  6329. #endif
  6330.     
  6331.     str->chain = 0;
  6332.     *stringlist_tailp = str;
  6333.     stringlist_tailp = &str->chain;
  6334.     
  6335.     /* Next comes a fourbyte number indicating the number of keys */
  6336.     /* for this string. */
  6337.     nkeys = *cp++;
  6338.     nkeys = (nkeys << 8) | *cp++;
  6339.     nkeys = (nkeys << 8) | *cp++;
  6340.     nkeys = (nkeys << 8) | *cp++;
  6341.  
  6342.     /* If this number is -1, then the string is mandatory. */
  6343.     if (nkeys == -1)
  6344.       str->writeflag = 1;
  6345.     else
  6346.       /* Otherwise, for each key, */
  6347.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  6348.     KEYDEF *kp = (KEYDEF *) cp;
  6349.     HASHNODE *hp;
  6350.     
  6351.     /* It starts with a KEYDEF structure */
  6352.     cp += sizeof (KEYDEF);
  6353.     
  6354.     /* Find the end of the key.  At the end of this for loop we
  6355.        advance CP to the start of the next key using this variable. */
  6356.     endofthiskey = cp + strlen (cp);
  6357.     kp->str = str;
  6358.     
  6359.     /* Expand the key, and enter it into the hash table. */
  6360.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  6361.     tmpbuf.bufp = tmpbuf.buf;
  6362.     
  6363.     while (is_hor_space[*tmpbuf.bufp])
  6364.       tmpbuf.bufp++;
  6365.     if (!is_idstart[*tmpbuf.bufp]
  6366.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  6367.       str->writeflag = 1;
  6368.       continue;
  6369.     }
  6370.         
  6371.     hp = lookup (tmpbuf.bufp, -1, -1);
  6372.     if (hp == NULL) {
  6373.       kp->chain = 0;
  6374.       install (tmpbuf.bufp, -1, T_PCSTRING, 0, (char *) kp, -1);
  6375.     }
  6376.     else if (hp->type == T_PCSTRING) {
  6377.       kp->chain = hp->value.keydef;
  6378.       hp->value.keydef = kp;
  6379.     }
  6380.     else
  6381.       str->writeflag = 1;
  6382.       }
  6383.   }
  6384.   /* This output_line_command serves to switch us back to the current
  6385.      input file in case some of these strings get output (which will 
  6386.      result in line commands for the header file being output). */
  6387.   output_line_command (&instack[indepth], op, 0, enter_file);
  6388. }
  6389.  
  6390. /* Called from rescan when it hits a key for strings.  Mark them all */
  6391.  /* used and clean up. */
  6392. static void
  6393. pcstring_used (hp)
  6394.      HASHNODE *hp;
  6395. {
  6396.   KEYDEF *kp;
  6397.   
  6398.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  6399.     kp->str->writeflag = 1;
  6400.   delete_macro (hp);
  6401. }
  6402.  
  6403. /* Write the output, interspersing precompiled strings in their */
  6404.  /* appropriate places. */
  6405. #ifdef NEXT_CPP_SERVER
  6406. static void
  6407. write_output (op, path, write_all)
  6408.      FILE_BUF *op;
  6409.      const char * path;
  6410.      int write_all;
  6411. #else
  6412. static void
  6413. write_output ()
  6414. #endif
  6415. {
  6416.   STRINGDEF *next_string;
  6417.   U_CHAR *cur_buf_loc;
  6418.   int line_command_len = 80;
  6419.   char *line_command = xmalloc (line_command_len);
  6420.   int len;
  6421.  
  6422.   /* In each run through the loop, either cur_buf_loc == */
  6423.   /* next_string_loc, in which case we print a series of strings, or */
  6424.   /* it is less than next_string_loc, in which case we write some of */
  6425.   /* the buffer. */
  6426. #ifdef NEXT_CPP_SERVER
  6427.   int output_fd;
  6428.   FILE * outs;
  6429.   if (!strcmp(path,"stdout"))
  6430.      output_fd = fileno(stdout);
  6431.   else
  6432.      output_fd = open (path, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  6433.   if (output_fd < 0) {
  6434.      fatal("Could not write file %s", path);
  6435.      return;
  6436.   }
  6437.   cur_buf_loc = write_all ? op->buf : op->output_written_to; 
  6438.   next_string = stringlist;
  6439.   
  6440.   while (cur_buf_loc < op->bufp || next_string) {
  6441.     if (next_string
  6442.     && cur_buf_loc - op->buf == next_string->output_mark) {
  6443. #else
  6444.   cur_buf_loc = outbuf.buf; 
  6445.   next_string = stringlist;
  6446.   
  6447.   while (cur_buf_loc < outbuf.bufp || next_string) {
  6448.     if (next_string
  6449.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  6450. #endif
  6451.       if (next_string->writeflag) {
  6452.     len = 4 * strlen (next_string->filename) + 32;
  6453.     while (len > line_command_len)
  6454.       line_command = xrealloc (line_command, 
  6455.                    line_command_len *= 2);
  6456.     sprintf (line_command, "\n# %d ", next_string->lineno);
  6457.     strcpy (quote_string (line_command + strlen (line_command),
  6458.                       next_string->filename),
  6459.         "\n");
  6460. #ifdef NEXT_CPP_SERVER        
  6461.     safe_write (output_fd, line_command, strlen (line_command));
  6462.     safe_write (output_fd, next_string->contents, next_string->len);
  6463. #else    
  6464.     safe_write (fileno (stdout), line_command, strlen (line_command));
  6465.     safe_write (fileno (stdout), next_string->contents, next_string->len);
  6466. #endif    
  6467.       }          
  6468.       next_string = next_string->chain;
  6469.     }
  6470.     else {
  6471.       len = (next_string
  6472.          ? (next_string->output_mark 
  6473. #ifdef NEXT_CPP_SERVER
  6474.         - (cur_buf_loc - op->buf))
  6475.          : op->bufp - cur_buf_loc);
  6476.       safe_write (output_fd, cur_buf_loc, len);
  6477. #else
  6478.         - (cur_buf_loc - outbuf.buf))
  6479.          : outbuf.bufp - cur_buf_loc);
  6480.       safe_write (fileno (stdout), cur_buf_loc, len);
  6481. #endif
  6482.       
  6483.       cur_buf_loc += len;
  6484.     }
  6485.   }
  6486.   free (line_command);
  6487. #ifdef NEXT_CPP_SERVER
  6488. #if 0
  6489.   while (fsync(output_fd)) {
  6490.      fprintf(stderr, "error calling fsync(%d) in ", output_fd);
  6491.      perror_with_name(path);
  6492.      break;  /* FIXME */
  6493.   }
  6494. #endif
  6495.    if (strcmp (path, "stdout") != 0 && close(output_fd)) {
  6496.      fprintf(stderr, "error calling close(%d) in ", output_fd);
  6497.      perror_with_name(path);
  6498.   }
  6499.   op->output_written_to = cur_buf_loc;
  6500. #endif    
  6501. }
  6502.  
  6503. #ifdef NEXT_CPP_SERVER
  6504. static int
  6505. write_header_output (const char * output_fname,
  6506.                      FILE_BUF * op,
  6507.                      int write_all,
  6508.                      int report)
  6509. {
  6510.    int result;
  6511.    if (write_all || (op->output_written_to < op->bufp)) {
  6512.       /* more output required */
  6513.       write_output (op, output_fname, write_all);
  6514.       if (report)
  6515.          fprintf(stderr,"wrote headers to %s\n", output_fname);
  6516.       result = 1;
  6517.    } else {
  6518.       /* keep output consistent at 2 lines */
  6519.       if (report)
  6520.          fprintf(stderr,"did not write headers to %s\n", output_fname);
  6521.       result = 0;
  6522.    }
  6523.    fflush(stderr);
  6524.    return result;
  6525. }
  6526.  
  6527. #endif NEXT_CPP_SERVER
  6528.  
  6529. /* Pass a directive through to the output file.
  6530.    BUF points to the contents of the directive, as a contiguous string.
  6531.    LIMIT points to the first character past the end of the directive.
  6532.    KEYWORD is the keyword-table entry for the directive.  */
  6533.  
  6534. static void
  6535. pass_thru_directive (buf, limit, op, keyword)
  6536.      U_CHAR *buf, *limit;
  6537.      FILE_BUF *op;
  6538.      struct directive *keyword;
  6539. {
  6540.   register unsigned keyword_length = keyword->length;
  6541.  
  6542.   check_expand (op, 1 + keyword_length + (limit - buf));
  6543.   *op->bufp++ = '#';
  6544.   bcopy (keyword->name, (char *) op->bufp, keyword_length);
  6545.   op->bufp += keyword_length;
  6546.   if (limit != buf && buf[0] != ' ')
  6547.     *op->bufp++ = ' ';
  6548.   bcopy ((char *) buf, (char *) op->bufp, limit - buf);
  6549.   op->bufp += (limit - buf);
  6550. #if 0
  6551.   *op->bufp++ = '\n';
  6552.   /* Count the line we have just made in the output,
  6553.      to get in sync properly.  */
  6554.   op->lineno++;
  6555. #endif
  6556. }
  6557.  
  6558. /* The arglist structure is built by do_define to tell
  6559.    collect_definition where the argument names begin.  That
  6560.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  6561.    would contain pointers to the strings x, y, and z.
  6562.    Collect_definition would then build a DEFINITION node,
  6563.    with reflist nodes pointing to the places x, y, and z had
  6564.    appeared.  So the arglist is just convenience data passed
  6565.    between these two routines.  It is not kept around after
  6566.    the current #define has been processed and entered into the
  6567.    hash table. */
  6568.  
  6569. struct arglist {
  6570.   struct arglist *next;
  6571.   U_CHAR *name;
  6572.   int length;
  6573.   int argno;
  6574.   char rest_args;
  6575. };
  6576.  
  6577. #ifdef DEBUG_REDEFINED_MACRO_LEAKS
  6578. void foo(char * file, int line) {
  6579.    fprintf(stderr,"### About to create macro NL0 for %s:%d ###\n", file, line);
  6580. }
  6581. #endif
  6582.  
  6583.  
  6584. /* Create a DEFINITION node from a #define directive.  Arguments are 
  6585.    as for do_define. */
  6586. #ifdef NEXT_CPP_SERVER
  6587. static MACRODEF
  6588. create_definition (buf, limit, op, hashcode, hashp)
  6589.      U_CHAR *buf, *limit;
  6590.      FILE_BUF *op;
  6591.      int *hashcode;
  6592.      HASHNODE **hashp;
  6593. #else
  6594. static MACRODEF
  6595. create_definition (buf, limit, op)
  6596.      U_CHAR *buf, *limit;
  6597.      FILE_BUF *op;
  6598. #endif
  6599. {
  6600.   U_CHAR *bp;            /* temp ptr into input buffer */
  6601.   U_CHAR *symname;        /* remember where symbol name starts */
  6602.   int sym_length;        /* and how long it is */
  6603.   int line = instack[indepth].lineno;
  6604.   char *file = instack[indepth].nominal_fname;
  6605.   int rest_args = 0;
  6606.  
  6607.   DEFINITION *defn;
  6608.   int arglengths = 0;        /* Accumulate lengths of arg names
  6609.                    plus number of args.  */
  6610.   MACRODEF mdef;
  6611.  
  6612.   bp = buf;
  6613.  
  6614.   while (is_hor_space[*bp])
  6615.     bp++;
  6616.  
  6617.   symname = bp;            /* remember where it starts */
  6618.   sym_length = check_macro_name (bp, "macro");
  6619.   bp += sym_length;
  6620.  
  6621. #ifdef NEXT_CPP_SERVER
  6622.   if (hashp) {
  6623.     HASHNODE *hp;
  6624.     *hashcode = hashf (symname, sym_length, HASHSIZE);
  6625.     if ((hp = lookup (symname, sym_length, *hashcode)) != NULL) {
  6626.       *hashp = hp;
  6627.       if (hp->type == T_MACRO) {
  6628.         /* now that we can process many files, the names should be unique */
  6629.         if (strcmp(file, hp->value.defn->file) == 0 &&
  6630.             line == hp->value.defn->line) {
  6631.           /* reuse macro definition...no need to advance buffer or anything!*/
  6632.           defn = hp->value.defn;
  6633.           goto reusedefn;
  6634.         } 
  6635.       }
  6636.     }
  6637.   }
  6638. #endif
  6639.  
  6640. #ifdef DEBUG_REDEFINED_MACRO_LEAKS
  6641.   /* REMOVE ME!! */
  6642.   if (!strncmp(symname, "NL0", sym_length))
  6643.      foo(file, line);
  6644. #endif
  6645.   
  6646.   /* Lossage will occur if identifiers or control keywords are broken
  6647.      across lines using backslash.  This is not the right place to take
  6648.      care of that. */
  6649.  
  6650.   if (*bp == '(') {
  6651.     struct arglist *arg_ptrs = NULL;
  6652.     int argno = 0;
  6653.  
  6654.     bp++;            /* skip '(' */
  6655.     SKIP_WHITE_SPACE (bp);
  6656.  
  6657.     /* Loop over macro argument names.  */
  6658.     while (*bp != ')') {
  6659.       struct arglist *temp;
  6660.  
  6661.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  6662.       temp->name = bp;
  6663.       temp->next = arg_ptrs;
  6664.       temp->argno = argno++;
  6665.       temp->rest_args = 0;
  6666.       arg_ptrs = temp;
  6667.  
  6668.       if (rest_args)
  6669.     pedwarn ("another parameter follows `%s'",
  6670.          rest_extension);
  6671.  
  6672.       if (!is_idstart[*bp])
  6673.     pedwarn ("invalid character in macro parameter name");
  6674.       
  6675.       /* Find the end of the arg name.  */
  6676.       while (is_idchar[*bp]) {
  6677.     bp++;
  6678.     /* do we have a "special" rest-args extension here? */
  6679.     if (limit - bp > REST_EXTENSION_LENGTH &&
  6680.         strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  6681.       rest_args = 1;
  6682.       temp->rest_args = 1;
  6683.       break;
  6684.     }
  6685.       }
  6686.       temp->length = bp - temp->name;
  6687.       if (rest_args == 1)
  6688.     bp += REST_EXTENSION_LENGTH;
  6689.       arglengths += temp->length + 2;
  6690.       SKIP_WHITE_SPACE (bp);
  6691.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  6692.     error ("badly punctuated parameter list in `#define'");
  6693.     goto nope;
  6694.       }
  6695.       if (*bp == ',') {
  6696.     bp++;
  6697.     SKIP_WHITE_SPACE (bp);
  6698.       }
  6699.       if (bp >= limit) {
  6700.     error ("unterminated parameter list in `#define'");
  6701.     goto nope;
  6702.       }
  6703.       {
  6704.     struct arglist *otemp;
  6705.  
  6706.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  6707.       if (temp->length == otemp->length &&
  6708.         strncmp (temp->name, otemp->name, temp->length) == 0) {
  6709.           U_CHAR *name;
  6710.  
  6711.           name = (U_CHAR *) alloca (temp->length + 1);
  6712.           (void) strncpy (name, temp->name, temp->length);
  6713.           name[temp->length] = '\0';
  6714.           error ("duplicate argument name `%s' in `#define'", name);
  6715.           goto nope;
  6716.       }
  6717.       }
  6718.     }
  6719.  
  6720.     ++bp;            /* skip paren */
  6721.     /* Skip spaces and tabs if any.  */
  6722.     while (bp < limit && (*bp == ' ' || *bp == '\t'))
  6723.       ++bp;
  6724.     /* now everything from bp before limit is the definition. */
  6725.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  6726.     defn->rest_args = rest_args;
  6727.  
  6728.     /* Now set defn->args.argnames to the result of concatenating
  6729.        the argument names in reverse order
  6730.        with comma-space between them.  */
  6731.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  6732.     {
  6733.       struct arglist *temp;
  6734.       int i = 0;
  6735.       for (temp = arg_ptrs; temp; temp = temp->next) {
  6736.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  6737.     i += temp->length;
  6738.     if (temp->next != 0) {
  6739.       defn->args.argnames[i++] = ',';
  6740.       defn->args.argnames[i++] = ' ';
  6741.     }
  6742.       }
  6743.       defn->args.argnames[i] = 0;
  6744.     }
  6745.   } else {
  6746.     /* Simple expansion or empty definition.  */
  6747.  
  6748.     /* Skip spaces and tabs if any.  */
  6749.     while (bp < limit && (*bp == ' ' || *bp == '\t'))
  6750.       ++bp;
  6751.     /* Now everything from bp before limit is the definition. */
  6752.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  6753.     defn->args.argnames = (U_CHAR *) "";
  6754.   }
  6755.  
  6756.   defn->line = line;
  6757.   defn->file = file;
  6758.  
  6759.   /* OP is null if this is a predefinition */
  6760.   defn->predefined = !op;
  6761. #ifdef NEXT_CPP_SERVER
  6762. reusedefn:
  6763. #endif
  6764.   mdef.defn = defn;
  6765.   mdef.symnam = symname;
  6766.   mdef.symlen = sym_length;
  6767.  
  6768.   return mdef;
  6769.  
  6770.  nope:
  6771.   mdef.defn = 0;
  6772.   return mdef;
  6773. }
  6774.  
  6775. /* Process a #define command.
  6776. BUF points to the contents of the #define command, as a contiguous string.
  6777. LIMIT points to the first character past the end of the definition.
  6778. KEYWORD is the keyword-table entry for #define.  */
  6779.  
  6780. static int
  6781. do_define (buf, limit, op, keyword)
  6782.      U_CHAR *buf, *limit;
  6783.      FILE_BUF *op;
  6784.      struct directive *keyword;
  6785. {
  6786.   int hashcode;
  6787.   MACRODEF mdef;
  6788. #ifdef NEXT_CPP_SERVER
  6789.   HASHNODE *hp = 0;
  6790. #endif
  6791.  
  6792.   /* If this is a precompiler run (with -pcp) pass thru #define commands.  */
  6793.   if (pcp_outfile && op)
  6794.     pass_thru_directive (buf, limit, op, keyword);
  6795.  
  6796. #ifdef NEXT_CPP_SERVER
  6797.   mdef = create_definition (buf, limit, op, &hashcode, &hp);
  6798. #else
  6799.   mdef = create_definition (buf, limit, op);
  6800. #endif
  6801.   if (mdef.defn == 0)
  6802.     goto nope;
  6803.  
  6804. #ifdef NEXT_CPP_SERVER
  6805.   if (hp) {
  6806.     if (hp->value.defn == mdef.defn) {
  6807.       hp->defined_within_module = 1;
  6808.     } else {
  6809. #else
  6810.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  6811.  
  6812.   {
  6813.     HASHNODE *hp;
  6814.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  6815. #endif
  6816.       int ok = 0;
  6817.       /* Redefining a precompiled key is ok.  */
  6818.       if (hp->type == T_PCSTRING)
  6819.     ok = 1;
  6820.       /* Redefining a macro is ok if the definitions are the same.  */
  6821. #ifdef NEXT_CPP_SERVER
  6822.       else if (hp->type == T_MACRO) {
  6823.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  6824.         if (!ok && !hp->defined_within_module) {
  6825.           hp->value.defn = mdef.defn;
  6826.           ok = 1; /* calm down, everything is ok now */
  6827.         }
  6828.         hp->defined_within_module = 1;
  6829.         /*fprintf(stderr, "reusing macro %.10s\n", mdef.symnam); */
  6830.       }
  6831. #else
  6832.       else if (hp->type == T_MACRO)
  6833.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  6834. #endif
  6835.       /* Redefining a constant is ok with -D.  */
  6836.       else if (hp->type == T_CONST)
  6837.         ok = ! done_initializing;
  6838.       /* Print the warning if it's not ok.  */
  6839.       if (!ok) {
  6840.     U_CHAR *msg;        /* what pain... */
  6841.  
  6842.         /* If we are passing through #define and #undef directives, do
  6843.        that for this re-definition now.  */
  6844.         if (debug_output && op)
  6845.       pass_thru_directive (buf, limit, op, keyword);
  6846.  
  6847.     msg = (U_CHAR *) alloca (mdef.symlen + 22);
  6848.     *msg = '`';
  6849.     bcopy ((char *) mdef.symnam, (char *) (msg + 1), mdef.symlen);
  6850.     strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
  6851.     pedwarn (msg);
  6852.     if (hp->type == T_MACRO)
  6853.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  6854.                       "this is the location of the previous definition");
  6855.       }
  6856.       /* Replace the old definition.  */
  6857.       hp->type = T_MACRO;
  6858.       hp->value.defn = mdef.defn;
  6859. #ifdef NEXT_CPP_SERVER
  6860.       }
  6861. #endif
  6862.     } else {
  6863.       /* If we are passing through #define and #undef directives, do
  6864.      that for this new definition now.  */
  6865.       if (debug_output && op)
  6866.     pass_thru_directive (buf, limit, op, keyword);
  6867. #ifdef NEXT_CPP_SERVER
  6868.       hp = install (mdef.symnam, mdef.symlen, T_MACRO, 0,
  6869.                     (char *) mdef.defn, hashcode);
  6870.       if (segregate_output && 
  6871.           instack[indepth].file && !instack[indepth].file->macro_list) {
  6872.         hp->owner = instack[indepth].file;
  6873.         hp->owner->macro_count++;
  6874.       }
  6875. #else
  6876.       install (mdef.symnam, mdef.symlen, T_MACRO, 0,
  6877.            (char *) mdef.defn, hashcode);
  6878. #endif
  6879.     }
  6880.  
  6881.   return 0;
  6882.  
  6883. nope:
  6884.  
  6885.   return 1;
  6886. }
  6887.  
  6888. /* Check a purported macro name SYMNAME, and yield its length.
  6889.    USAGE is the kind of name this is intended for.  */
  6890.  
  6891. static int
  6892. check_macro_name (symname, usage)
  6893.      U_CHAR *symname;
  6894.      char *usage;
  6895. {
  6896.   U_CHAR *p;
  6897.   int sym_length;
  6898.  
  6899.   for (p = symname; is_idchar[*p]; p++)
  6900.     ;
  6901.   sym_length = p - symname;
  6902.   if (sym_length == 0)
  6903.     error ("invalid %s name", usage);
  6904.   else if (!is_idstart[*symname]) {
  6905.     U_CHAR *msg;            /* what pain... */
  6906.     msg = (U_CHAR *) alloca (sym_length + 1);
  6907.     bcopy ((char *) symname, (char *) msg, sym_length);
  6908.     msg[sym_length] = 0;
  6909.     error ("invalid %s name `%s'", usage, msg);
  6910.   } else {
  6911.     if (! strncmp (symname, "defined", 7) && sym_length == 7)
  6912.       error ("invalid %s name `defined'", usage);
  6913.   }
  6914.   return sym_length;
  6915. }
  6916.  
  6917. /*
  6918.  * return zero if two DEFINITIONs are isomorphic
  6919.  */
  6920. static int
  6921. compare_defs (d1, d2)
  6922.      DEFINITION *d1, *d2;
  6923. {
  6924.   register struct reflist *a1, *a2;
  6925.   register U_CHAR *p1 = d1->expansion;
  6926.   register U_CHAR *p2 = d2->expansion;
  6927.   int first = 1;
  6928.  
  6929.   if (d1->nargs != d2->nargs)
  6930.     return 1;
  6931.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  6932.     return 1;
  6933.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  6934.        a1 = a1->next, a2 = a2->next) {
  6935.     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  6936.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  6937.     || a1->argno != a2->argno
  6938.     || a1->stringify != a2->stringify
  6939.     || a1->raw_before != a2->raw_before
  6940.     || a1->raw_after != a2->raw_after)
  6941.       return 1;
  6942.     first = 0;
  6943.     p1 += a1->nchars;
  6944.     p2 += a2->nchars;
  6945.   }
  6946.   if (a1 != a2)
  6947.     return 1;
  6948.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  6949.              p2, d2->length - (p2 - d2->expansion), 1))
  6950.     return 1;
  6951.   return 0;
  6952. }
  6953.  
  6954. /* Return 1 if two parts of two macro definitions are effectively different.
  6955.    One of the parts starts at BEG1 and has LEN1 chars;
  6956.    the other has LEN2 chars at BEG2.
  6957.    Any sequence of whitespace matches any other sequence of whitespace.
  6958.    FIRST means these parts are the first of a macro definition;
  6959.     so ignore leading whitespace entirely.
  6960.    LAST means these parts are the last of a macro definition;
  6961.     so ignore trailing whitespace entirely.  */
  6962.  
  6963. static int
  6964. comp_def_part (first, beg1, len1, beg2, len2, last)
  6965.      int first;
  6966.      U_CHAR *beg1, *beg2;
  6967.      int len1, len2;
  6968.      int last;
  6969. {
  6970.   register U_CHAR *end1 = beg1 + len1;
  6971.   register U_CHAR *end2 = beg2 + len2;
  6972.   if (first) {
  6973.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  6974.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  6975.   }
  6976.   if (last) {
  6977.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  6978.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  6979.   }
  6980.   while (beg1 != end1 && beg2 != end2) {
  6981.     if (is_space[*beg1] && is_space[*beg2]) {
  6982.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  6983.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  6984.     } else if (*beg1 == *beg2) {
  6985.       beg1++; beg2++;
  6986.     } else break;
  6987.   }
  6988.   return (beg1 != end1) || (beg2 != end2);
  6989. }
  6990.  
  6991. /* Read a replacement list for a macro with parameters.
  6992.    Build the DEFINITION structure.
  6993.    Reads characters of text starting at BUF until END.
  6994.    ARGLIST specifies the formal parameters to look for
  6995.    in the text of the definition; NARGS is the number of args
  6996.    in that list, or -1 for a macro name that wants no argument list.
  6997.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  6998.    and NAMELEN is its length in characters.
  6999.    
  7000. Note that comments and backslash-newlines have already been deleted
  7001. from the argument.  */
  7002.  
  7003. /* Leading and trailing Space, Tab, etc. are converted to markers
  7004.    Newline Space, Newline Tab, etc.
  7005.    Newline Space makes a space in the final output
  7006.    but is discarded if stringified.  (Newline Tab is similar but
  7007.    makes a Tab instead.)
  7008.  
  7009.    If there is no trailing whitespace, a Newline Space is added at the end
  7010.    to prevent concatenation that would be contrary to the standard.  */
  7011.  
  7012. static DEFINITION *
  7013. collect_expansion (buf, end, nargs, arglist)
  7014.      U_CHAR *buf, *end;
  7015.      int nargs;
  7016.      struct arglist *arglist;
  7017. {
  7018.   DEFINITION *defn;
  7019.   register U_CHAR *p, *limit, *lastp, *exp_p;
  7020.   struct reflist *endpat = NULL;
  7021.   /* Pointer to first nonspace after last ## seen.  */
  7022.   U_CHAR *concat = 0;
  7023.   /* Pointer to first nonspace after last single-# seen.  */
  7024.   U_CHAR *stringify = 0;
  7025.   int maxsize;
  7026.   int expected_delimiter = '\0';
  7027.  
  7028.   /* Scan thru the replacement list, ignoring comments and quoted
  7029.      strings, picking up on the macro calls.  It does a linear search
  7030.      thru the arg list on every potential symbol.  Profiling might say
  7031.      that something smarter should happen. */
  7032.  
  7033.   if (end < buf)
  7034.     abort ();
  7035.  
  7036.   /* Find the beginning of the trailing whitespace.  */
  7037.   /* Find end of leading whitespace.  */
  7038.   limit = end;
  7039.   p = buf;
  7040.   while (p < limit && is_space[limit[-1]]) limit--;
  7041.   while (p < limit && is_space[*p]) p++;
  7042.  
  7043.   /* Allocate space for the text in the macro definition.
  7044.      Leading and trailing whitespace chars need 2 bytes each.
  7045.      Each other input char may or may not need 1 byte,
  7046.      so this is an upper bound.
  7047.      The extra 2 are for invented trailing newline-marker and final null.  */
  7048.   maxsize = (sizeof (DEFINITION)
  7049.          + 2 * (end - limit) + 2 * (p - buf)
  7050.          + (limit - p) + 3);
  7051.   defn = (DEFINITION *) xcalloc (1, maxsize);
  7052.  
  7053.   defn->nargs = nargs;
  7054.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  7055.   lastp = exp_p;
  7056.  
  7057.   p = buf;
  7058.  
  7059.   /* Convert leading whitespace to Newline-markers.  */
  7060.   while (p < limit && is_space[*p]) {
  7061.     *exp_p++ = '\n';
  7062.     *exp_p++ = *p++;
  7063.   }
  7064.  
  7065.   if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
  7066.     error ("`##' at start of macro definition");
  7067.     p += 2;
  7068.   }
  7069.  
  7070.   /* Process the main body of the definition.  */
  7071.   while (p < limit) {
  7072.     int skipped_arg = 0;
  7073.     register U_CHAR c = *p++;
  7074.  
  7075.     *exp_p++ = c;
  7076.  
  7077.     if (!traditional) {
  7078.       switch (c) {
  7079.       case '\'':
  7080.       case '\"':
  7081.         if (expected_delimiter != '\0') {
  7082.           if (c == expected_delimiter)
  7083.             expected_delimiter = '\0';
  7084.         } else
  7085.           expected_delimiter = c;
  7086.     break;
  7087.  
  7088.     /* Special hack: if a \# is written in the #define
  7089.        include a # in the definition.  This is useless for C code
  7090.        but useful for preprocessing other things.  */
  7091.  
  7092.       case '\\':
  7093.     /* \# quotes a # even outside of strings.  */
  7094.     if (p < limit && *p == '#' && !expected_delimiter) {
  7095.       exp_p--;
  7096.       *exp_p++ = *p++;
  7097.     } else if (p < limit && expected_delimiter) {
  7098.       /* In a string, backslash goes through
  7099.          and makes next char ordinary.  */
  7100.       *exp_p++ = *p++;
  7101.     }
  7102.     break;
  7103.  
  7104.       case '#':
  7105.     /* # is ordinary inside a string.  */
  7106.     if (expected_delimiter)
  7107.       break;
  7108.     if (p < limit && *p == '#') {
  7109.       /* ##: concatenate preceding and following tokens.  */
  7110.       /* Take out the first #, discard preceding whitespace.  */
  7111.       exp_p--;
  7112.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  7113.         --exp_p;
  7114.       /* Skip the second #.  */
  7115.       p++;
  7116.       /* Discard following whitespace.  */
  7117.       SKIP_WHITE_SPACE (p);
  7118.       concat = p;
  7119.       if (p == limit)
  7120.         error ("`##' at end of macro definition");
  7121.     } else if (nargs >= 0) {
  7122.       /* Single #: stringify following argument ref.
  7123.          Don't leave the # in the expansion.  */
  7124.       exp_p--;
  7125.       SKIP_WHITE_SPACE (p);
  7126.       if (p == limit || ! is_idstart[*p])
  7127.         error ("`#' operator is not followed by a macro argument name");
  7128.       else
  7129.         stringify = p;
  7130.     }
  7131.     break;
  7132.       }
  7133.     } else {
  7134.       /* In -traditional mode, recognize arguments inside strings and
  7135.      and character constants, and ignore special properties of #.
  7136.      Arguments inside strings are considered "stringified", but no
  7137.      extra quote marks are supplied.  */
  7138.       switch (c) {
  7139.       case '\'':
  7140.       case '\"':
  7141.     if (expected_delimiter != '\0') {
  7142.       if (c == expected_delimiter)
  7143.         expected_delimiter = '\0';
  7144.     } else
  7145.       expected_delimiter = c;
  7146.     break;
  7147.  
  7148.       case '\\':
  7149.     /* Backslash quotes delimiters and itself, but not macro args.  */
  7150.     if (expected_delimiter != 0 && p < limit
  7151.         && (*p == expected_delimiter || *p == '\\')) {
  7152.       *exp_p++ = *p++;
  7153.       continue;
  7154.     }
  7155.     break;
  7156.  
  7157.       case '/':
  7158.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  7159.       break;
  7160.     if (*p == '*') {
  7161.       /* If we find a comment that wasn't removed by handle_directive,
  7162.          this must be -traditional.  So replace the comment with
  7163.          nothing at all.  */
  7164.       exp_p--;
  7165.       p += 1;
  7166.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  7167.         p++;
  7168. #if 0
  7169.       /* Mark this as a concatenation-point, as if it had been ##.  */
  7170.       concat = p;
  7171. #endif
  7172.     }
  7173.     break;
  7174.       }
  7175.     }
  7176.  
  7177.     /* Handle the start of a symbol.  */
  7178.     if (is_idchar[c] && nargs > 0) {
  7179.       U_CHAR *id_beg = p - 1;
  7180.       int id_len;
  7181.  
  7182.       --exp_p;
  7183.       while (p != limit && is_idchar[*p]) p++;
  7184.       id_len = p - id_beg;
  7185.  
  7186.       if (is_idstart[c]) {
  7187.     register struct arglist *arg;
  7188.  
  7189.     for (arg = arglist; arg != NULL; arg = arg->next) {
  7190.       struct reflist *tpat;
  7191.  
  7192.       if (arg->name[0] == c
  7193.           && arg->length == id_len
  7194.           && strncmp (arg->name, id_beg, id_len) == 0) {
  7195.         if (expected_delimiter && warn_stringify) {
  7196.           if (traditional) {
  7197.         warning ("macro argument `%.*s' is stringified.",
  7198.              id_len, arg->name);
  7199.           } else {
  7200.         warning ("macro arg `%.*s' would be stringified with -traditional.",
  7201.              id_len, arg->name);
  7202.           }
  7203.         }
  7204.         /* If ANSI, don't actually substitute inside a string.  */
  7205.         if (!traditional && expected_delimiter)
  7206.           break;
  7207.         /* make a pat node for this arg and append it to the end of
  7208.            the pat list */
  7209.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  7210.         tpat->next = NULL;
  7211.         tpat->raw_before = concat == id_beg;
  7212.         tpat->raw_after = 0;
  7213.         tpat->rest_args = arg->rest_args;
  7214.         tpat->stringify = (traditional ? expected_delimiter != '\0'
  7215.                    : stringify == id_beg);
  7216.  
  7217.         if (endpat == NULL)
  7218.           defn->pattern = tpat;
  7219.         else
  7220.           endpat->next = tpat;
  7221.         endpat = tpat;
  7222.  
  7223.         tpat->argno = arg->argno;
  7224.         tpat->nchars = exp_p - lastp;
  7225.         {
  7226.           register U_CHAR *p1 = p;
  7227.           SKIP_WHITE_SPACE (p1);
  7228.           if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  7229.         tpat->raw_after = 1;
  7230.         }
  7231.         lastp = exp_p;    /* place to start copying from next time */
  7232.         skipped_arg = 1;
  7233.         break;
  7234.       }
  7235.     }
  7236.       }
  7237.  
  7238.       /* If this was not a macro arg, copy it into the expansion.  */
  7239.       if (! skipped_arg) {
  7240.     register U_CHAR *lim1 = p;
  7241.     p = id_beg;
  7242.     while (p != lim1)
  7243.       *exp_p++ = *p++;
  7244.     if (stringify == id_beg)
  7245.       error ("`#' operator should be followed by a macro argument name");
  7246.       }
  7247.     }
  7248.   }
  7249.  
  7250.   if (!traditional && expected_delimiter == 0) {
  7251.     /* There is no trailing whitespace, so invent some in ANSI mode.
  7252.        But not if "inside a string" (which in ANSI mode
  7253.        happens only for -D option).  */
  7254.     *exp_p++ = '\n';
  7255.     *exp_p++ = ' ';
  7256.   }
  7257.  
  7258.   *exp_p = '\0';
  7259.  
  7260.   defn->length = exp_p - defn->expansion;
  7261.  
  7262.   /* Crash now if we overrun the allocated size.  */
  7263.   if (defn->length + 1 > maxsize)
  7264.     abort ();
  7265.  
  7266. #if 0
  7267. /* This isn't worth the time it takes.  */
  7268.   /* give back excess storage */
  7269.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  7270. #endif
  7271.  
  7272.   return defn;
  7273. }
  7274.  
  7275. static int
  7276. do_assert (buf, limit, op, keyword)
  7277.      U_CHAR *buf, *limit;
  7278.      FILE_BUF *op;
  7279.      struct directive *keyword;
  7280. {
  7281.   U_CHAR *bp;            /* temp ptr into input buffer */
  7282.   U_CHAR *symname;        /* remember where symbol name starts */
  7283.   int sym_length;        /* and how long it is */
  7284.   struct arglist *tokens = NULL;
  7285.  
  7286.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  7287.     pedwarn ("ANSI C does not allow `#assert'");
  7288.  
  7289.   bp = buf;
  7290.  
  7291.   while (is_hor_space[*bp])
  7292.     bp++;
  7293.  
  7294.   symname = bp;            /* remember where it starts */
  7295.   sym_length = check_macro_name (bp, "assertion");
  7296.   bp += sym_length;
  7297.   /* #define doesn't do this, but we should.  */
  7298.   SKIP_WHITE_SPACE (bp);
  7299.  
  7300.   /* Lossage will occur if identifiers or control tokens are broken
  7301.      across lines using backslash.  This is not the right place to take
  7302.      care of that. */
  7303.  
  7304.   if (*bp != '(') {
  7305.     error ("missing token-sequence in `#assert'");
  7306.     return 1;
  7307.   }
  7308.  
  7309.   {
  7310.     int error_flag = 0;
  7311.  
  7312.     bp++;            /* skip '(' */
  7313.     SKIP_WHITE_SPACE (bp);
  7314.  
  7315.     tokens = read_token_list (&bp, limit, &error_flag);
  7316.     if (error_flag)
  7317.       return 1;
  7318.     if (tokens == 0) {
  7319.       error ("empty token-sequence in `#assert'");
  7320.       return 1;
  7321.     }
  7322.  
  7323.     ++bp;            /* skip paren */
  7324.     SKIP_WHITE_SPACE (bp);
  7325.   }
  7326.  
  7327.   /* If this name isn't already an assertion name, make it one.
  7328.      Error if it was already in use in some other way.  */
  7329.  
  7330.   {
  7331.     ASSERTION_HASHNODE *hp;
  7332.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  7333.     struct tokenlist_list *value
  7334.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  7335.  
  7336.     hp = assertion_lookup (symname, sym_length, hashcode);
  7337.     if (hp == NULL) {
  7338.       if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
  7339.     error ("`defined' redefined as assertion");
  7340.       hp = assertion_install (symname, sym_length, hashcode);
  7341.     }
  7342.  
  7343.     /* Add the spec'd token-sequence to the list of such.  */
  7344.     value->tokens = tokens;
  7345.     value->next = hp->value;
  7346.     hp->value = value;
  7347.   }
  7348.  
  7349.   return 0;
  7350. }
  7351.  
  7352. static int
  7353. do_unassert (buf, limit, op, keyword)
  7354.      U_CHAR *buf, *limit;
  7355.      FILE_BUF *op;
  7356.      struct directive *keyword;
  7357. {
  7358.   U_CHAR *bp;            /* temp ptr into input buffer */
  7359.   U_CHAR *symname;        /* remember where symbol name starts */
  7360.   int sym_length;        /* and how long it is */
  7361.  
  7362.   struct arglist *tokens = NULL;
  7363.   int tokens_specified = 0;
  7364.  
  7365.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  7366.     pedwarn ("ANSI C does not allow `#unassert'");
  7367.  
  7368.   bp = buf;
  7369.  
  7370.   while (is_hor_space[*bp])
  7371.     bp++;
  7372.  
  7373.   symname = bp;            /* remember where it starts */
  7374.   sym_length = check_macro_name (bp, "assertion");
  7375.   bp += sym_length;
  7376.   /* #define doesn't do this, but we should.  */
  7377.   SKIP_WHITE_SPACE (bp);
  7378.  
  7379.   /* Lossage will occur if identifiers or control tokens are broken
  7380.      across lines using backslash.  This is not the right place to take
  7381.      care of that. */
  7382.  
  7383.   if (*bp == '(') {
  7384.     int error_flag = 0;
  7385.  
  7386.     bp++;            /* skip '(' */
  7387.     SKIP_WHITE_SPACE (bp);
  7388.  
  7389.     tokens = read_token_list (&bp, limit, &error_flag);
  7390.     if (error_flag)
  7391.       return 1;
  7392.     if (tokens == 0) {
  7393.       error ("empty token list in `#unassert'");
  7394.       return 1;
  7395.     }
  7396.  
  7397.     tokens_specified = 1;
  7398.  
  7399.     ++bp;            /* skip paren */
  7400.     SKIP_WHITE_SPACE (bp);
  7401.   }
  7402.  
  7403.   {
  7404.     ASSERTION_HASHNODE *hp;
  7405.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  7406.     struct tokenlist_list *tail, *prev;
  7407.  
  7408.     hp = assertion_lookup (symname, sym_length, hashcode);
  7409.     if (hp == NULL)
  7410.       return 1;
  7411.  
  7412.     /* If no token list was specified, then eliminate this assertion
  7413.        entirely.  */
  7414.     if (! tokens_specified) {
  7415.       struct tokenlist_list *next;
  7416.       for (tail = hp->value; tail; tail = next) {
  7417.     next = tail->next;
  7418.     free_token_list (tail->tokens);
  7419.     free (tail);
  7420.       }
  7421.       delete_assertion (hp);
  7422.     } else {
  7423.       /* If a list of tokens was given, then delete any matching list.  */
  7424.  
  7425.       tail = hp->value;
  7426.       prev = 0;
  7427.       while (tail) {
  7428.     struct tokenlist_list *next = tail->next;
  7429.     if (compare_token_lists (tail->tokens, tokens)) {
  7430.       if (prev)
  7431.         prev->next = next;
  7432.       else
  7433.         hp->value = tail->next;
  7434.       free_token_list (tail->tokens);
  7435.       free (tail);
  7436.     } else {
  7437.       prev = tail;
  7438.     }
  7439.     tail = next;
  7440.       }
  7441.     }
  7442.   }
  7443.  
  7444.   return 0;
  7445. }
  7446.  
  7447. /* Test whether there is an assertion named NAME
  7448.    and optionally whether it has an asserted token list TOKENS.
  7449.    NAME is not null terminated; its length is SYM_LENGTH.
  7450.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  7451.  
  7452. int
  7453. check_assertion (name, sym_length, tokens_specified, tokens)
  7454.      U_CHAR *name;
  7455.      int sym_length;
  7456.      int tokens_specified;
  7457.      struct arglist *tokens;
  7458. {
  7459.   ASSERTION_HASHNODE *hp;
  7460.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  7461.  
  7462.   if (pedantic && !instack[indepth].system_header_p)
  7463.     pedwarn ("ANSI C does not allow testing assertions");
  7464.  
  7465.   hp = assertion_lookup (name, sym_length, hashcode);
  7466.   if (hp == NULL)
  7467.     /* It is not an assertion; just return false.  */
  7468.     return 0;
  7469.  
  7470.   /* If no token list was specified, then value is 1.  */
  7471.   if (! tokens_specified)
  7472.     return 1;
  7473.  
  7474.   {
  7475.     struct tokenlist_list *tail;
  7476.  
  7477.     tail = hp->value;
  7478.  
  7479.     /* If a list of tokens was given,
  7480.        then succeed if the assertion records a matching list.  */
  7481.  
  7482.     while (tail) {
  7483.       if (compare_token_lists (tail->tokens, tokens))
  7484.     return 1;
  7485.       tail = tail->next;
  7486.     }
  7487.  
  7488.     /* Fail if the assertion has no matching list.  */
  7489.     return 0;
  7490.   }
  7491. }
  7492.  
  7493. /* Compare two lists of tokens for equality including order of tokens.  */
  7494.  
  7495. static int
  7496. compare_token_lists (l1, l2)
  7497.      struct arglist *l1, *l2;
  7498. {
  7499.   while (l1 && l2) {
  7500.     if (l1->length != l2->length)
  7501.       return 0;
  7502.     if (strncmp (l1->name, l2->name, l1->length))
  7503.       return 0;
  7504.     l1 = l1->next;
  7505.     l2 = l2->next;
  7506.   }
  7507.  
  7508.   /* Succeed if both lists end at the same time.  */
  7509.   return l1 == l2;
  7510. }
  7511.  
  7512. /* Read a space-separated list of tokens ending in a close parenthesis.
  7513.    Return a list of strings, in the order they were written.
  7514.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  7515.    Parse the text starting at *BPP, and update *BPP.
  7516.    Don't parse beyond LIMIT.  */
  7517.  
  7518. static struct arglist *
  7519. read_token_list (bpp, limit, error_flag)
  7520.      U_CHAR **bpp;
  7521.      U_CHAR *limit;
  7522.      int *error_flag;
  7523. {
  7524.   struct arglist *token_ptrs = 0;
  7525.   U_CHAR *bp = *bpp;
  7526.   int depth = 1;
  7527.  
  7528.   *error_flag = 0;
  7529.  
  7530.   /* Loop over the assertion value tokens.  */
  7531.   while (depth > 0) {
  7532.     struct arglist *temp;
  7533.     int eofp = 0;
  7534.     U_CHAR *beg = bp;
  7535.  
  7536.     /* Find the end of the token.  */
  7537.     if (*bp == '(') {
  7538.       bp++;
  7539.       depth++;
  7540.     } else if (*bp == ')') {
  7541.       depth--;
  7542.       if (depth == 0)
  7543.     break;
  7544.       bp++;
  7545.     } else if (*bp == '"' || *bp == '\'')
  7546.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  7547.     else
  7548.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  7549.          && *bp != '"' && *bp != '\'' && bp != limit)
  7550.     bp++;
  7551.  
  7552.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  7553.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  7554.     bcopy ((char *) beg, (char *) temp->name, bp - beg);
  7555.     temp->name[bp - beg] = 0;
  7556.     temp->next = token_ptrs;
  7557.     token_ptrs = temp;
  7558.     temp->length = bp - beg;
  7559.  
  7560.     SKIP_WHITE_SPACE (bp);
  7561.  
  7562.     if (bp >= limit) {
  7563.       error ("unterminated token sequence in `#assert' or `#unassert'");
  7564.       *error_flag = -1;
  7565.       return 0;
  7566.     }
  7567.   }
  7568.   *bpp = bp;
  7569.  
  7570.   /* We accumulated the names in reverse order.
  7571.      Now reverse them to get the proper order.  */
  7572.   {
  7573.     register struct arglist *prev = 0, *this, *next;
  7574.     for (this = token_ptrs; this; this = next) {
  7575.       next = this->next;
  7576.       this->next = prev;
  7577.       prev = this;
  7578.     }
  7579.     return prev;
  7580.   }
  7581. }
  7582.  
  7583. static void
  7584. free_token_list (tokens)
  7585.      struct arglist *tokens;
  7586. {
  7587.   while (tokens) {
  7588.     struct arglist *next = tokens->next;
  7589.     free (tokens->name);
  7590.     free (tokens);
  7591.     tokens = next;
  7592.   }
  7593. }
  7594.  
  7595. /*
  7596.  * Install a name in the assertion hash table.
  7597.  *
  7598.  * If LEN is >= 0, it is the length of the name.
  7599.  * Otherwise, compute the length by scanning the entire name.
  7600.  *
  7601.  * If HASH is >= 0, it is the precomputed hash code.
  7602.  * Otherwise, compute the hash code.
  7603.  */
  7604. static ASSERTION_HASHNODE *
  7605. assertion_install (name, len, hash)
  7606.      U_CHAR *name;
  7607.      int len;
  7608.      int hash;
  7609. {
  7610.   register ASSERTION_HASHNODE *hp;
  7611.   register int i, bucket;
  7612.   register U_CHAR *p, *q;
  7613.  
  7614.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  7615.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  7616.   bucket = hash;
  7617.   hp->bucket_hdr = &assertion_hashtab[bucket];
  7618.   hp->next = assertion_hashtab[bucket];
  7619.   assertion_hashtab[bucket] = hp;
  7620.   hp->prev = NULL;
  7621.   if (hp->next != NULL)
  7622.     hp->next->prev = hp;
  7623.   hp->length = len;
  7624.   hp->value = 0;
  7625.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  7626.   p = hp->name;
  7627.   q = name;
  7628.   for (i = 0; i < len; i++)
  7629.     *p++ = *q++;
  7630.   hp->name[len] = 0;
  7631.   return hp;
  7632. }
  7633.  
  7634. /*
  7635.  * find the most recent hash node for name name (ending with first
  7636.  * non-identifier char) installed by install
  7637.  *
  7638.  * If LEN is >= 0, it is the length of the name.
  7639.  * Otherwise, compute the length by scanning the entire name.
  7640.  *
  7641.  * If HASH is >= 0, it is the precomputed hash code.
  7642.  * Otherwise, compute the hash code.
  7643.  */
  7644. static ASSERTION_HASHNODE *
  7645. assertion_lookup (name, len, hash)
  7646.      U_CHAR *name;
  7647.      int len;
  7648.      int hash;
  7649. {
  7650.   register ASSERTION_HASHNODE *bucket;
  7651.  
  7652.   bucket = assertion_hashtab[hash];
  7653.   while (bucket) {
  7654.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  7655.       return bucket;
  7656.     bucket = bucket->next;
  7657.   }
  7658.   return NULL;
  7659. }
  7660.  
  7661. static void
  7662. delete_assertion (hp)
  7663.      ASSERTION_HASHNODE *hp;
  7664. {
  7665.  
  7666.   if (hp->prev != NULL)
  7667.     hp->prev->next = hp->next;
  7668.   if (hp->next != NULL)
  7669.     hp->next->prev = hp->prev;
  7670.  
  7671.   /* make sure that the bucket chain header that
  7672.      the deleted guy was on points to the right thing afterwards. */
  7673.   if (hp == *hp->bucket_hdr)
  7674.     *hp->bucket_hdr = hp->next;
  7675.  
  7676.   free (hp);
  7677. }
  7678.  
  7679. /*
  7680.  * interpret #line command.  Remembers previously seen fnames
  7681.  * in its very own hash table.
  7682.  */
  7683. #define FNAME_HASHSIZE 37
  7684.  
  7685. static int
  7686. do_line (buf, limit, op, keyword)
  7687.      U_CHAR *buf, *limit;
  7688.      FILE_BUF *op;
  7689.      struct directive *keyword;
  7690. {
  7691.   register U_CHAR *bp;
  7692.   FILE_BUF *ip = &instack[indepth];
  7693.   FILE_BUF tem;
  7694.   int new_lineno;
  7695.   enum file_change_code file_change = same_file;
  7696.  
  7697.   /* Expand any macros.  */
  7698.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  7699.  
  7700.   /* Point to macroexpanded line, which is null-terminated now.  */
  7701.   bp = tem.buf;
  7702.   SKIP_WHITE_SPACE (bp);
  7703.  
  7704.   if (!isdigit (*bp)) {
  7705.     error ("invalid format `#line' command");
  7706.     return 0;
  7707.   }
  7708.  
  7709.   /* The Newline at the end of this line remains to be processed.
  7710.      To put the next line at the specified line number,
  7711.      we must store a line number now that is one less.  */
  7712.   new_lineno = atoi (bp) - 1;
  7713.  
  7714.   /* NEW_LINENO is one less than the actual line number here.  */
  7715.   if (pedantic && new_lineno < 0)
  7716.     pedwarn ("line number out of range in `#line' command");
  7717.  
  7718.   /* skip over the line number.  */
  7719.   while (isdigit (*bp))
  7720.     bp++;
  7721.  
  7722. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  7723.   if (*bp && !is_space[*bp]) {
  7724.     error ("invalid format `#line' command");
  7725.     return;
  7726.   }
  7727. #endif
  7728.  
  7729.   SKIP_WHITE_SPACE (bp);
  7730.  
  7731.   if (*bp == '\"') {
  7732.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  7733.     HASHNODE *hp, **hash_bucket;
  7734.     U_CHAR *fname, *p;
  7735.     int fname_length;
  7736.  
  7737.     fname = ++bp;
  7738.  
  7739.     /* Turn the file name, which is a character string literal,
  7740.        into a null-terminated string.  Do this in place.  */
  7741.     p = bp;
  7742.     for (;;)
  7743.       switch ((*p++ = *bp++)) {
  7744.       case '\0':
  7745.     error ("invalid format `#line' command");
  7746.     return 0;
  7747.  
  7748.       case '\\':
  7749.     {
  7750.       char *bpc = (char *) bp;
  7751.       int c = parse_escape (&bpc);
  7752.       bp = (U_CHAR *) bpc;
  7753.       if (c < 0)
  7754.         p--;
  7755.       else
  7756.         p[-1] = c;
  7757.     }
  7758.     break;
  7759.  
  7760.       case '\"':
  7761.     p[-1] = 0;
  7762.     goto fname_done;
  7763.       }
  7764.   fname_done:
  7765.     fname_length = p - fname;
  7766.  
  7767.     SKIP_WHITE_SPACE (bp);
  7768.     if (*bp) {
  7769.       if (pedantic)
  7770.     pedwarn ("garbage at end of `#line' command");
  7771.       if (*bp == '1')
  7772.     file_change = enter_file;
  7773.       else if (*bp == '2')
  7774.     file_change = leave_file;
  7775.       else if (*bp == '3')
  7776.     ip->system_header_p = 1;
  7777.       else if (*bp == '4')
  7778.     ip->system_header_p = 2;
  7779.       else {
  7780.     error ("invalid format `#line' command");
  7781.     return 0;
  7782.       }
  7783.  
  7784.       bp++;
  7785.       SKIP_WHITE_SPACE (bp);
  7786.       if (*bp == '3') {
  7787.     ip->system_header_p = 1;
  7788.     bp++;
  7789.     SKIP_WHITE_SPACE (bp);
  7790.       }
  7791.       if (*bp == '4') {
  7792.     ip->system_header_p = 2;
  7793.     bp++;
  7794.     SKIP_WHITE_SPACE (bp);
  7795.       }
  7796.       if (*bp) {
  7797.     error ("invalid format `#line' command");
  7798.     return 0;
  7799.       }
  7800.     }
  7801.  
  7802.     hash_bucket =
  7803.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  7804.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  7805.       if (hp->length == fname_length &&
  7806.       strncmp (hp->value.cpval, fname, fname_length) == 0) {
  7807.     ip->nominal_fname = hp->value.cpval;
  7808.     break;
  7809.       }
  7810.     if (hp == 0) {
  7811.       /* Didn't find it; cons up a new one.  */
  7812.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  7813.       hp->next = *hash_bucket;
  7814.       *hash_bucket = hp;
  7815.  
  7816.       hp->length = fname_length;
  7817.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  7818.       bcopy (fname, hp->value.cpval, fname_length);
  7819.     }
  7820.   } else if (*bp) {
  7821.     error ("invalid format `#line' command");
  7822.     return 0;
  7823.   }
  7824.  
  7825.   ip->lineno = new_lineno;
  7826.   output_line_command (ip, op, 0, file_change);
  7827.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  7828.   return 0;
  7829. }
  7830.  
  7831. /*
  7832.  * remove the definition of a symbol from the symbol table.
  7833.  * according to un*x /lib/cpp, it is not an error to undef
  7834.  * something that has no definitions, so it isn't one here either.
  7835.  */
  7836.  
  7837. static int
  7838. do_undef (buf, limit, op, keyword)
  7839.      U_CHAR *buf, *limit;
  7840.      FILE_BUF *op;
  7841.      struct directive *keyword;
  7842. {
  7843.   int sym_length;
  7844.   HASHNODE *hp;
  7845.   U_CHAR *orig_buf = buf;
  7846.  
  7847.   /* If this is a precompiler run (with -pcp) pass thru #undef commands.  */
  7848.   if (pcp_outfile && op)
  7849.     pass_thru_directive (buf, limit, op, keyword);
  7850.  
  7851.   SKIP_WHITE_SPACE (buf);
  7852.   sym_length = check_macro_name (buf, "macro");
  7853.  
  7854.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  7855.     /* If we are generating additional info for debugging (with -g) we
  7856.        need to pass through all effective #undef commands.  */
  7857.     if (debug_output && op)
  7858.       pass_thru_directive (orig_buf, limit, op, keyword);
  7859.     if (hp->type != T_MACRO)
  7860.       warning ("undefining `%s'", hp->name);
  7861. #ifdef NEXT_CPP_SERVER
  7862.     if (segregate_output && hp->owner && !hp->owner->macro_list) 
  7863.       hp->owner->macro_count--;
  7864. #endif
  7865.     delete_macro (hp);
  7866.   }
  7867.  
  7868.   if (pedantic) {
  7869.     buf += sym_length;
  7870.     SKIP_WHITE_SPACE (buf);
  7871.     if (buf != limit)
  7872.       pedwarn ("garbage after `#undef' directive");
  7873.   }
  7874.   return 0;
  7875. }
  7876.  
  7877. /*
  7878.  * Report an error detected by the program we are processing.
  7879.  * Use the text of the line in the error message.
  7880.  * (We use error because it prints the filename & line#.)
  7881.  */
  7882.  
  7883. static int
  7884. do_error (buf, limit, op, keyword)
  7885.      U_CHAR *buf, *limit;
  7886.      FILE_BUF *op;
  7887.      struct directive *keyword;
  7888. {
  7889.   int length = limit - buf;
  7890.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  7891.   bcopy ((char *) buf, (char *) copy, length);
  7892.   copy[length] = 0;
  7893.   SKIP_WHITE_SPACE (copy);
  7894.   error ("#error %s", copy);
  7895.   return 0;
  7896. }
  7897.  
  7898. /*
  7899.  * Report a warning detected by the program we are processing.
  7900.  * Use the text of the line in the warning message, then continue.
  7901.  * (We use error because it prints the filename & line#.)
  7902.  */
  7903.  
  7904. static int
  7905. do_warning (buf, limit, op, keyword)
  7906.      U_CHAR *buf, *limit;
  7907.      FILE_BUF *op;
  7908.      struct directive *keyword;
  7909. {
  7910.   int length = limit - buf;
  7911.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  7912.   bcopy ((char *) buf, (char *) copy, length);
  7913.   copy[length] = 0;
  7914.   SKIP_WHITE_SPACE (copy);
  7915.   warning ("#warning %s", copy);
  7916.   return 0;
  7917. }
  7918.  
  7919. /* Remember the name of the current file being read from so that we can
  7920.    avoid ever including it again.  */
  7921.  
  7922. static int
  7923. do_once ()
  7924. {
  7925.   int i;
  7926.   FILE_BUF *ip = NULL;
  7927.  
  7928.   for (i = indepth; i >= 0; i--)
  7929.     if (instack[i].fname != NULL) {
  7930.       ip = &instack[i];
  7931.       break;
  7932.     }
  7933.  
  7934.   if (ip != NULL) {
  7935.     struct file_name_list *new;
  7936.     
  7937.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  7938.     new->next = dont_repeat_files;
  7939.     dont_repeat_files = new;
  7940.     new->fname = savestring (ip->fname);
  7941.     new->control_macro = 0;
  7942.     new->got_name_map = 0;
  7943.     new->c_system_include_path = 0;
  7944.   }
  7945.   return 0;
  7946. }
  7947.  
  7948. /* #ident has already been copied to the output file, so just ignore it.  */
  7949.  
  7950. static int
  7951. do_ident (buf, limit)
  7952.      U_CHAR *buf, *limit;
  7953. {
  7954.   FILE_BUF trybuf;
  7955.   int len;
  7956.   FILE_BUF *op = &outbuf;
  7957.  
  7958.   /* Allow #ident in system headers, since that's not user's fault.  */
  7959.   if (pedantic && !instack[indepth].system_header_p)
  7960.     pedwarn ("ANSI C does not allow `#ident'");
  7961.  
  7962.   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  7963.   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  7964.   bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  7965.   limit = buf + (trybuf.bufp - trybuf.buf);
  7966.   len = (limit - buf);
  7967.   free (trybuf.buf);
  7968.  
  7969.   /* Output directive name.  */
  7970.   check_expand (op, 8);
  7971.   bcopy ("#ident ", (char *) op->bufp, 7);
  7972.   op->bufp += 7;
  7973.  
  7974.   /* Output the expanded argument line.  */
  7975.   check_expand (op, len);
  7976.   bcopy ((char *) buf, (char *) op->bufp, len);
  7977.   op->bufp += len;
  7978.  
  7979.   return 0;
  7980. }
  7981.  
  7982. /* #pragma and its argument line have already been copied to the output file.
  7983.    Just check for some recognized pragmas that need validation here.  */
  7984.  
  7985. static int
  7986. do_pragma (buf, limit)
  7987.      U_CHAR *buf, *limit;
  7988. {
  7989.   while (*buf == ' ' || *buf == '\t')
  7990.     buf++;
  7991.   if (!strncmp (buf, "once", 4)) {
  7992.     /* Allow #pragma once in system headers, since that's not the user's
  7993.        fault.  */
  7994.     if (!instack[indepth].system_header_p)
  7995.       warning ("`#pragma once' is obsolete");
  7996.     do_once ();
  7997.   }
  7998.  
  7999.   if (!strncmp (buf, "cplusplus", 9))
  8000.     {
  8001.       if (instack[indepth].system_header_p == 2 && cplusplus)
  8002.     {
  8003.       instack[indepth].system_header_p = 1;
  8004.       output_line_command (&instack[indepth], &outbuf, 0, same_file);
  8005.     }
  8006.     }
  8007.  
  8008.   if (!strncmp (buf, "implementation", 14)) {
  8009.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  8010.        been included yet.  */
  8011.     struct file_name_list *ptr;
  8012.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  8013.     SKIP_WHITE_SPACE (p);
  8014.     if (*p == '\n' || *p != '\"')
  8015.       return 0;
  8016.  
  8017.     fname = p + 1;
  8018.     if (p = (U_CHAR *) index (fname, '\"'))
  8019.       *p = '\0';
  8020.     
  8021.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  8022.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  8023.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  8024.       if (inc_fname && !strcmp (inc_fname, fname))
  8025.     warning ("`#pragma implementation' for `%s' appears after file is included",
  8026.          fname);
  8027.     }
  8028.   }
  8029.  
  8030.   return 0;
  8031. }
  8032.  
  8033. #if 0
  8034. /* This was a fun hack, but #pragma seems to start to be useful.
  8035.    By failing to recognize it, we pass it through unchanged to cc1.  */
  8036.  
  8037. /*
  8038.  * the behavior of the #pragma directive is implementation defined.
  8039.  * this implementation defines it as follows.
  8040.  */
  8041.  
  8042. static int
  8043. do_pragma ()
  8044. {
  8045.   close (0);
  8046.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  8047.     goto nope;
  8048.   close (1);
  8049.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  8050.     goto nope;
  8051.   execl ("/usr/games/hack", "#pragma", 0);
  8052.   execl ("/usr/games/rogue", "#pragma", 0);
  8053.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  8054.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  8055. nope:
  8056.   fatal ("You are in a maze of twisty compiler features, all different");
  8057. }
  8058. #endif
  8059.  
  8060. /* Just ignore #sccs, on systems where we define it at all.  */
  8061.  
  8062. static int
  8063. do_sccs ()
  8064. {
  8065.   if (pedantic)
  8066.     pedwarn ("ANSI C does not allow `#sccs'");
  8067.   return 0;
  8068. }
  8069.  
  8070. /*
  8071.  * handle #if command by
  8072.  *   1) inserting special `defined' keyword into the hash table
  8073.  *    that gets turned into 0 or 1 by special_symbol (thus,
  8074.  *    if the luser has a symbol called `defined' already, it won't
  8075.  *      work inside the #if command)
  8076.  *   2) rescan the input into a temporary output buffer
  8077.  *   3) pass the output buffer to the yacc parser and collect a value
  8078.  *   4) clean up the mess left from steps 1 and 2.
  8079.  *   5) call conditional_skip to skip til the next #endif (etc.),
  8080.  *      or not, depending on the value from step 3.
  8081.  */
  8082.  
  8083. static int
  8084. do_if (buf, limit, op, keyword)
  8085.      U_CHAR *buf, *limit;
  8086.      FILE_BUF *op;
  8087.      struct directive *keyword;
  8088. {
  8089.   int value;
  8090.   FILE_BUF *ip = &instack[indepth];
  8091.  
  8092.   value = eval_if_expression (buf, limit - buf);
  8093. #ifdef NEXT_CPP_SERVER
  8094.   conditional_skip (ip, op, value == 0, T_IF, NULL_PTR);
  8095. #else
  8096.   conditional_skip (ip, value == 0, T_IF, NULL_PTR);
  8097. #endif
  8098.   return 0;
  8099. }
  8100.  
  8101. /*
  8102.  * handle a #elif directive by not changing  if_stack  either.
  8103.  * see the comment above do_else.
  8104.  */
  8105.  
  8106. static int
  8107. do_elif (buf, limit, op, keyword)
  8108.      U_CHAR *buf, *limit;
  8109.      FILE_BUF *op;
  8110.      struct directive *keyword;
  8111. {
  8112.   int value;
  8113.   FILE_BUF *ip = &instack[indepth];
  8114.  
  8115.   if (if_stack == instack[indepth].if_stack) {
  8116.     error ("`#elif' not within a conditional");
  8117.     return 0;
  8118.   } else {
  8119.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  8120.       error ("`#elif' after `#else'");
  8121. #ifdef REPORT_EVENT
  8122.       REPORT_EVENT (0, NULL, if_stack->fname, if_stack->lineno,
  8123.             "matching conditional", 0, 0, 0);
  8124. #endif
  8125.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  8126.       if (if_stack->fname != NULL && ip->fname != NULL &&
  8127.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  8128.     fprintf (stderr, ", file %s", if_stack->fname);
  8129.       fprintf (stderr, ")\n");
  8130.     }
  8131.     if_stack->type = T_ELIF;
  8132.   }
  8133.  
  8134.   if (if_stack->if_succeeded)
  8135. #ifdef NEXT_CPP_SERVER
  8136.     skip_if_group (ip, op, 0);
  8137. #else
  8138.     skip_if_group (ip, 0);
  8139. #endif
  8140.   else {
  8141.     value = eval_if_expression (buf, limit - buf);
  8142.     if (value == 0)
  8143. #ifdef NEXT_CPP_SERVER
  8144.       skip_if_group (ip, op, 0);
  8145. #else
  8146.       skip_if_group (ip, 0);
  8147. #endif
  8148.     else {
  8149.       ++if_stack->if_succeeded;    /* continue processing input */
  8150.       output_line_command (ip, op, 1, same_file);
  8151.     }
  8152.   }
  8153.   return 0;
  8154. }
  8155.  
  8156. /*
  8157.  * evaluate a #if expression in BUF, of length LENGTH,
  8158.  * then parse the result as a C expression and return the value as an int.
  8159.  */
  8160. static int
  8161. eval_if_expression (buf, length)
  8162.      U_CHAR *buf;
  8163.      int length;
  8164. {
  8165.   FILE_BUF temp_obuf;
  8166.   HASHNODE *save_defined;
  8167.   int value;
  8168.  
  8169.   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
  8170.   pcp_inside_if = 1;
  8171.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  8172.   pcp_inside_if = 0;
  8173.   delete_macro (save_defined);    /* clean up special symbol */
  8174.  
  8175.   value = parse_c_expression (temp_obuf.buf);
  8176.  
  8177.   free (temp_obuf.buf);
  8178.  
  8179.   return value;
  8180. }
  8181.  
  8182. /*
  8183.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  8184.  * then do or don't skip to the #endif/#else/#elif depending
  8185.  * on what directive is actually being processed.
  8186.  */
  8187.  
  8188. static int
  8189. do_xifdef (buf, limit, op, keyword)
  8190.      U_CHAR *buf, *limit;
  8191.      FILE_BUF *op;
  8192.      struct directive *keyword;
  8193. {
  8194.   int skip;
  8195.   FILE_BUF *ip = &instack[indepth];
  8196.   U_CHAR *end; 
  8197.   int start_of_file = 0;
  8198.   U_CHAR *control_macro = 0;
  8199.  
  8200.   /* Detect a #ifndef at start of file (not counting comments).  */
  8201.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  8202.     U_CHAR *p = ip->buf;
  8203.     while (p != directive_start) {
  8204.       U_CHAR c = *p++;
  8205.       if (is_space[c])
  8206.     ;
  8207.       else if (c == '/' && p != ip->bufp && *p == '*') {
  8208.     /* Skip this comment.  */
  8209.     int junk = 0;
  8210.     U_CHAR *save_bufp = ip->bufp;
  8211.     ip->bufp = p + 1;
  8212. #ifdef NEXT_CPP_SERVER
  8213.     p = skip_to_end_of_comment (ip, op, &junk, 1);
  8214. #else
  8215.     p = skip_to_end_of_comment (ip, &junk, 1);
  8216. #endif
  8217.     ip->bufp = save_bufp;
  8218.       } else {
  8219.     goto fail;
  8220.       }
  8221.     }
  8222.     /* If we get here, this conditional is the beginning of the file.  */
  8223.     start_of_file = 1;
  8224.   fail: ;
  8225.   }
  8226.  
  8227.   /* Discard leading and trailing whitespace.  */
  8228.   SKIP_WHITE_SPACE (buf);
  8229.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  8230.  
  8231.   /* Find the end of the identifier at the beginning.  */
  8232.   for (end = buf; is_idchar[*end]; end++);
  8233.  
  8234.   if (end == buf) {
  8235.     skip = (keyword->type == T_IFDEF);
  8236.     if (! traditional)
  8237.       pedwarn (end == limit ? "`#%s' with no argument"
  8238.            : "`#%s' argument starts with punctuation",
  8239.            keyword->name);
  8240.   } else {
  8241.     HASHNODE *hp;
  8242.  
  8243.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  8244.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  8245.     else if (end != limit && !traditional)
  8246.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  8247.  
  8248.     hp = lookup (buf, end-buf, -1);
  8249.  
  8250.     if (pcp_outfile) {
  8251.       /* Output a precondition for this macro.  */
  8252.       if (hp && hp->value.defn->predefined)
  8253.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  8254.       else {
  8255.     U_CHAR *cp = buf;
  8256.     fprintf (pcp_outfile, "#undef ");
  8257.     while (is_idchar[*cp]) /* Ick! */
  8258.       fputc (*cp++, pcp_outfile);
  8259.     putc ('\n', pcp_outfile);
  8260.       }
  8261.     }
  8262.  
  8263.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  8264.     if (start_of_file && !skip) {
  8265.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  8266.       bcopy ((char *) buf, (char *) control_macro, end - buf);
  8267.       control_macro[end - buf] = 0;
  8268.     }
  8269.   }
  8270.   
  8271. #ifdef NEXT_CPP_SERVER
  8272.   conditional_skip (ip, op, skip, T_IF, control_macro);
  8273. #else
  8274.   conditional_skip (ip, skip, T_IF, control_macro);
  8275. #endif
  8276.   return 0;
  8277. }
  8278.  
  8279. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  8280.    If this is a #ifndef starting at the beginning of a file,
  8281.    CONTROL_MACRO is the macro name tested by the #ifndef.
  8282.    Otherwise, CONTROL_MACRO is 0.  */
  8283.  
  8284. #ifdef NEXT_CPP_SERVER
  8285. static void
  8286. conditional_skip (ip, op, skip, type, control_macro)
  8287.      FILE_BUF *ip;
  8288.      FILE_BUF *op;
  8289.      int skip;
  8290.      enum node_type type;
  8291.      U_CHAR *control_macro;
  8292. #else
  8293. static void
  8294. conditional_skip (ip, skip, type, control_macro)
  8295.      FILE_BUF *ip;
  8296.      int skip;
  8297.      enum node_type type;
  8298.      U_CHAR *control_macro;
  8299. #endif
  8300. {
  8301.   IF_STACK_FRAME *temp;
  8302.  
  8303.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  8304.   temp->fname = ip->nominal_fname;
  8305.   temp->lineno = ip->lineno;
  8306.   temp->next = if_stack;
  8307.   temp->control_macro = control_macro;
  8308.   if_stack = temp;
  8309.  
  8310.   if_stack->type = type;
  8311.  
  8312.   if (skip != 0) {
  8313. #ifdef NEXT_CPP_SERVER
  8314.     skip_if_group (ip, op, 0);
  8315. #else
  8316.     skip_if_group (ip, 0);
  8317. #endif
  8318.     return;
  8319.   } else {
  8320.     ++if_stack->if_succeeded;
  8321. #ifdef NEXT_CPP_SERVER
  8322.     output_line_command (ip, op, 1, same_file);
  8323. #else
  8324.     output_line_command (ip, &outbuf, 1, same_file);
  8325. #endif
  8326.   }
  8327. }
  8328.  
  8329. /*
  8330.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  8331.  * leaves input ptr at the sharp sign found.
  8332.  * If ANY is nonzero, return at next directive of any sort.
  8333.  */
  8334. #ifdef NEXT_CPP_SERVER
  8335. static void
  8336. skip_if_group (ip, op, any)
  8337.      FILE_BUF *ip;
  8338.      FILE_BUF *op;
  8339.      int any;
  8340. #else
  8341. static void
  8342. skip_if_group (ip, any)
  8343.      FILE_BUF *ip;
  8344.      int any;
  8345. #endif
  8346. {
  8347.   register U_CHAR *bp = ip->bufp, *cp;
  8348.   register U_CHAR *endb = ip->buf + ip->length;
  8349.   struct directive *kt;
  8350.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  8351.   U_CHAR *beg_of_line = bp;
  8352.   register int ident_length;
  8353.   U_CHAR *ident, *after_ident;
  8354.  
  8355. #ifdef NEXT_CPP_SERVER
  8356.   if (any && segregate_output && aggressive_skip_if_group(ip))
  8357.     return;
  8358. #endif
  8359.  
  8360.   while (bp < endb) {
  8361.     switch (*bp++) {
  8362.     case '/':            /* possible comment */
  8363.       if (*bp == '\\' && bp[1] == '\n')
  8364.     newline_fix (bp);
  8365.       if (*bp == '*'
  8366.       || (cplusplus_comments && *bp == '/')) {
  8367.     ip->bufp = ++bp;
  8368. #ifdef NEXT_CPP_SERVER
  8369.     bp = skip_to_end_of_comment (ip, op, &ip->lineno, 0);
  8370. #else
  8371.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  8372. #endif
  8373.       }
  8374.       break;
  8375.     case '\"':
  8376.     case '\'':
  8377.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  8378.                    NULL_PTR, NULL_PTR);
  8379.       break;
  8380.     case '\\':
  8381.       /* Char after backslash loses its special meaning.  */
  8382.       if (bp < endb) {
  8383.     if (*bp == '\n')
  8384.       ++ip->lineno;        /* But do update the line-count.  */
  8385.     bp++;
  8386.       }
  8387.       break;
  8388.     case '\n':
  8389.       ++ip->lineno;
  8390.       beg_of_line = bp;
  8391.       break;
  8392.     case '#':
  8393.       ip->bufp = bp - 1;
  8394.  
  8395.       /* # keyword: a # must be first nonblank char on the line */
  8396.       if (beg_of_line == 0)
  8397.     break;
  8398.       /* Scan from start of line, skipping whitespace, comments
  8399.      and backslash-newlines, and see if we reach this #.
  8400.      If not, this # is not special.  */
  8401.       bp = beg_of_line;
  8402.       /* If -traditional, require # to be at beginning of line.  */
  8403.       if (!traditional)
  8404.     while (1) {
  8405.       if (is_hor_space[*bp])
  8406.         bp++;
  8407.       else if (*bp == '\\' && bp[1] == '\n')
  8408.         bp += 2;
  8409.       else if (*bp == '/' && bp[1] == '*') {
  8410.         bp += 2;
  8411.         while (!(*bp == '*' && bp[1] == '/'))
  8412.           bp++;
  8413.         bp += 2;
  8414.       }
  8415.       /* There is no point in trying to deal with C++ // comments here,
  8416.          because if there is one, then this # must be part of the
  8417.          comment and we would never reach here.  */
  8418.       else break;
  8419.     }
  8420.       if (bp != ip->bufp) {
  8421.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  8422.     break;
  8423.       }
  8424.  
  8425.       bp = ip->bufp + 1;    /* Point after the '#' */
  8426.  
  8427.       /* Skip whitespace and \-newline.  */
  8428.       while (1) {
  8429.     if (is_hor_space[*bp])
  8430.       bp++;
  8431.     else if (*bp == '\\' && bp[1] == '\n')
  8432.       bp += 2;
  8433.     else if (*bp == '/' && bp[1] == '*') {
  8434.       bp += 2;
  8435.       while (!(*bp == '*' && bp[1] == '/')) {
  8436.         if (*bp == '\n')
  8437.           ip->lineno++;
  8438.         bp++;
  8439.       }
  8440.       bp += 2;
  8441.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  8442.       bp += 2;
  8443.       while (bp[-1] == '\\' || *bp != '\n') {
  8444.         if (*bp == '\n')
  8445.           ip->lineno++;
  8446.         bp++;
  8447.       }
  8448.         }
  8449.     else break;
  8450.       }
  8451.  
  8452.       cp = bp;
  8453.  
  8454.       /* Now find end of directive name.
  8455.      If we encounter a backslash-newline, exchange it with any following
  8456.      symbol-constituents so that we end up with a contiguous name.  */
  8457.  
  8458.       while (1) {
  8459.     if (is_idchar[*bp])
  8460.       bp++;
  8461.     else {
  8462.       if (*bp == '\\' && bp[1] == '\n')
  8463.         name_newline_fix (bp);
  8464.       if (is_idchar[*bp])
  8465.         bp++;
  8466.       else break;
  8467.     }
  8468.       }
  8469.       ident_length = bp - cp;
  8470.       ident = cp;
  8471.       after_ident = bp;
  8472.  
  8473.       /* A line of just `#' becomes blank.  */
  8474.  
  8475.       if (ident_length == 0 && *after_ident == '\n') {
  8476.     continue;
  8477.       }
  8478.  
  8479.       if (ident_length == 0 || !is_idstart[*ident]) {
  8480.     U_CHAR *p = ident;
  8481.     while (is_idchar[*p]) {
  8482.       if (*p < '0' || *p > '9')
  8483.         break;
  8484.       p++;
  8485.     }
  8486.     /* Handle # followed by a line number.  */
  8487.     if (p != ident && !is_idchar[*p]) {
  8488.       if (pedantic)
  8489.         pedwarn ("`#' followed by integer");
  8490.       continue;
  8491.     }
  8492.  
  8493.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  8494.     if (p == ident) {
  8495.       while (*p == '#' || is_hor_space[*p]) p++;
  8496.       if (*p == '\n') {
  8497.         if (pedantic && !lang_asm)
  8498.           pedwarn ("invalid preprocessor directive");
  8499.         continue;
  8500.       }
  8501.     }
  8502.  
  8503.     if (!lang_asm && pedantic)
  8504.       pedwarn ("invalid preprocessor directive name");
  8505.     continue;
  8506.       }
  8507.  
  8508.       for (kt = directive_table; kt->length >= 0; kt++) {
  8509.     IF_STACK_FRAME *temp;
  8510.     if (ident_length == kt->length
  8511.         && strncmp (cp, kt->name, kt->length) == 0) {
  8512.       /* If we are asked to return on next directive, do so now.  */
  8513.       if (any)
  8514.         return;
  8515.  
  8516.       switch (kt->type) {
  8517.       case T_IF:
  8518.       case T_IFDEF:
  8519.       case T_IFNDEF:
  8520.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  8521.         temp->next = if_stack;
  8522.         if_stack = temp;
  8523.         temp->lineno = ip->lineno;
  8524.         temp->fname = ip->nominal_fname;
  8525.         temp->type = kt->type;
  8526.         break;
  8527.       case T_ELSE:
  8528.       case T_ENDIF:
  8529.         if (pedantic && if_stack != save_if_stack)
  8530.           validate_else (bp);
  8531.       case T_ELIF:
  8532.         if (if_stack == instack[indepth].if_stack) {
  8533.           error ("`#%s' not within a conditional", kt->name);
  8534.           break;
  8535.         }
  8536.         else if (if_stack == save_if_stack)
  8537.           return;        /* found what we came for */
  8538.  
  8539.         if (kt->type != T_ENDIF) {
  8540.           if (if_stack->type == T_ELSE)
  8541.         error ("`#else' or `#elif' after `#else'");
  8542.           if_stack->type = kt->type;
  8543.           break;
  8544.         }
  8545.  
  8546.         temp = if_stack;
  8547.         if_stack = if_stack->next;
  8548.         free (temp);
  8549.         break;
  8550.       }
  8551.       break;
  8552.     }
  8553.       }
  8554.       /* Don't let erroneous code go by.  */
  8555.       if (kt->length < 0 && !lang_asm && pedantic)
  8556.     pedwarn ("invalid preprocessor directive name");
  8557.     }
  8558.   }
  8559.   ip->bufp = bp;
  8560.   /* after this returns, rescan will exit because ip->bufp
  8561.      now points to the end of the buffer.
  8562.      rescan is responsible for the error message also.  */
  8563. }
  8564.  
  8565. /*
  8566.  * handle a #else directive.  Do this by just continuing processing
  8567.  * without changing  if_stack ;  this is so that the error message
  8568.  * for missing #endif's etc. will point to the original #if.  It
  8569.  * is possible that something different would be better.
  8570.  */
  8571.  
  8572. static int
  8573. do_else (buf, limit, op, keyword)
  8574.      U_CHAR *buf, *limit;
  8575.      FILE_BUF *op;
  8576.      struct directive *keyword;
  8577. {
  8578.   FILE_BUF *ip = &instack[indepth];
  8579.  
  8580.   if (pedantic) {
  8581.     SKIP_WHITE_SPACE (buf);
  8582.     if (buf != limit)
  8583.       pedwarn ("text following `#else' violates ANSI standard");
  8584.   }
  8585.  
  8586.   if (if_stack == instack[indepth].if_stack) {
  8587.     error ("`#else' not within a conditional");
  8588.     return 0;
  8589.   } else {
  8590.     /* #ifndef can't have its special treatment for containing the whole file
  8591.        if it has a #else clause.  */
  8592.     if_stack->control_macro = 0;
  8593.  
  8594.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  8595.       error ("`#else' after `#else'");
  8596. #ifdef REPORT_EVENT
  8597.       REPORT_EVENT (0, NULL, if_stack->fname, if_stack->lineno,
  8598.             "matching conditional", 0, 0, 0);
  8599. #endif
  8600.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  8601.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  8602.     fprintf (stderr, ", file %s", if_stack->fname);
  8603.       fprintf (stderr, ")\n");
  8604.     }
  8605.     if_stack->type = T_ELSE;
  8606.   }
  8607.  
  8608.   if (if_stack->if_succeeded)
  8609. #ifdef NEXT_CPP_SERVER
  8610.     skip_if_group (ip, op, 0);
  8611. #else
  8612.     skip_if_group (ip, 0);
  8613. #endif
  8614.   else {
  8615.     ++if_stack->if_succeeded;    /* continue processing input */
  8616.     output_line_command (ip, op, 1, same_file);
  8617.   }
  8618.   return 0;
  8619. }
  8620.  
  8621. /*
  8622.  * unstack after #endif command
  8623.  */
  8624.  
  8625. static int
  8626. do_endif (buf, limit, op, keyword)
  8627.      U_CHAR *buf, *limit;
  8628.      FILE_BUF *op;
  8629.      struct directive *keyword;
  8630. {
  8631.   if (pedantic) {
  8632.     SKIP_WHITE_SPACE (buf);
  8633.     if (buf != limit)
  8634.       pedwarn ("text following `#endif' violates ANSI standard");
  8635.   }
  8636.  
  8637.   if (if_stack == instack[indepth].if_stack)
  8638.     error ("unbalanced `#endif'");
  8639.   else {
  8640.     IF_STACK_FRAME *temp = if_stack;
  8641.     if_stack = if_stack->next;
  8642.     if (temp->control_macro != 0) {
  8643.       /* This #endif matched a #ifndef at the start of the file.
  8644.      See if it is at the end of the file.  */
  8645.       FILE_BUF *ip = &instack[indepth];
  8646.       U_CHAR *p = ip->bufp;
  8647.       U_CHAR *ep = ip->buf + ip->length;
  8648.  
  8649.       while (p != ep) {
  8650.     U_CHAR c = *p++;
  8651.     switch (c) {
  8652.     case ' ':
  8653.     case '\t':
  8654.     case '\n':
  8655.       break;
  8656.     case '/':
  8657.       if (p != ep && *p == '*') {
  8658.         /* Skip this comment.  */
  8659.         int junk = 0;
  8660.         U_CHAR *save_bufp = ip->bufp;
  8661.         ip->bufp = p + 1;
  8662. #ifdef NEXT_CPP_SERVER
  8663.         p = skip_to_end_of_comment (ip, op, &junk, 1);
  8664. #else
  8665.         p = skip_to_end_of_comment (ip, &junk, 1);
  8666. #endif
  8667.         ip->bufp = save_bufp;
  8668.       }
  8669.       break;
  8670.     default:
  8671.       goto fail;
  8672.     }
  8673.       }
  8674.       /* If we get here, this #endif ends a #ifndef
  8675.      that contains all of the file (aside from whitespace).
  8676.      Arrange not to include the file again
  8677.      if the macro that was tested is defined.
  8678.  
  8679.      Do not do this for the top-level file in a -include or any
  8680.      file in a -imacros.  */
  8681.       if (indepth != 0
  8682.       && ! (indepth == 1 && no_record_file)
  8683.       && ! (no_record_file && no_output))
  8684.     record_control_macro (ip->fname, temp->control_macro);
  8685.     fail: ;
  8686.     }
  8687.     free (temp);
  8688.     output_line_command (&instack[indepth], op, 1, same_file);
  8689.   }
  8690.   return 0;
  8691. }
  8692.  
  8693. /* When an #else or #endif is found while skipping failed conditional,
  8694.    if -pedantic was specified, this is called to warn about text after
  8695.    the command name.  P points to the first char after the command name.  */
  8696.  
  8697. static void
  8698. validate_else (p)
  8699.      register U_CHAR *p;
  8700. {
  8701.   /* Advance P over whitespace and comments.  */
  8702.   while (1) {
  8703.     if (*p == '\\' && p[1] == '\n')
  8704.       p += 2;
  8705.     if (is_hor_space[*p])
  8706.       p++;
  8707.     else if (*p == '/') {
  8708.       if (p[1] == '\\' && p[2] == '\n')
  8709.     newline_fix (p + 1);
  8710.       if (p[1] == '*') {
  8711.     p += 2;
  8712.     /* Don't bother warning about unterminated comments
  8713.        since that will happen later.  Just be sure to exit.  */
  8714.     while (*p) {
  8715.       if (p[1] == '\\' && p[2] == '\n')
  8716.         newline_fix (p + 1);
  8717.       if (*p == '*' && p[1] == '/') {
  8718.         p += 2;
  8719.         break;
  8720.       }
  8721.       p++;
  8722.     }
  8723.       }
  8724.       else if (cplusplus_comments && p[1] == '/') {
  8725.     p += 2;
  8726.     while (*p && (*p != '\n' || p[-1] == '\\'))
  8727.       p++;
  8728.       }
  8729.     } else break;
  8730.   }
  8731.   if (*p && *p != '\n')
  8732.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  8733. }
  8734.  
  8735. /* Skip a comment, assuming the input ptr immediately follows the
  8736.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  8737.    (The canonical line counter is &ip->lineno.)
  8738.    Don't use this routine (or the next one) if bumping the line
  8739.    counter is not sufficient to deal with newlines in the string.
  8740.  
  8741.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  8742.    This feature is useful when processing a comment that is going to be
  8743.    processed or was processed at another point in the preprocessor,
  8744.    to avoid a duplicate warning.  */
  8745.  
  8746. #ifdef NEXT_CPP_SERVER
  8747. static U_CHAR *
  8748. skip_to_end_of_comment (ip, op, line_counter, nowarn)
  8749.      register FILE_BUF *ip;
  8750.      register FILE_BUF *op;
  8751.      int *line_counter;        /* place to remember newlines, or NULL */
  8752.      int nowarn;
  8753. #else
  8754. static U_CHAR *
  8755. skip_to_end_of_comment (ip, line_counter, nowarn)
  8756.      register FILE_BUF *ip;
  8757.      int *line_counter;        /* place to remember newlines, or NULL */
  8758.      int nowarn;
  8759. #endif
  8760. {
  8761.   register U_CHAR *limit = ip->buf + ip->length;
  8762.   register U_CHAR *bp = ip->bufp;
  8763. #ifndef NEXT_CPP_SERVER
  8764.   FILE_BUF *op = &outbuf;    /* JF */
  8765. #endif
  8766.   int output = put_out_comments && !line_counter;
  8767.   int start_line = line_counter ? *line_counter : 0;
  8768.  
  8769.     /* JF this line_counter stuff is a crock to make sure the
  8770.        comment is only put out once, no matter how many times
  8771.        the comment is skipped.  It almost works */
  8772.   if (output) {
  8773.     *op->bufp++ = '/';
  8774.     *op->bufp++ = '*';
  8775.   }
  8776.   if (cplusplus_comments && bp[-1] == '/') {
  8777.     if (output) {
  8778.       while (bp < limit) {
  8779.     *op->bufp++ = *bp;
  8780.     if (*bp == '\n' && bp[-1] != '\\')
  8781.       break;
  8782.     if (*bp == '\n') {
  8783.       ++*line_counter;
  8784.       ++op->lineno;
  8785.     }
  8786.     bp++;
  8787.       }
  8788.       op->bufp[-1] = '*';
  8789.       *op->bufp++ = '/';
  8790.       *op->bufp++ = '\n';
  8791.     } else {
  8792.       while (bp < limit) {
  8793.     if (bp[-1] != '\\' && *bp == '\n') {
  8794.       break;
  8795.     } else {
  8796.       if (*bp == '\n' && line_counter)
  8797.         ++*line_counter;
  8798.       bp++;
  8799.     }
  8800.       }
  8801.     }
  8802.     ip->bufp = bp;
  8803.     return bp;
  8804.   }
  8805.   while (bp < limit) {
  8806.     if (output)
  8807.       *op->bufp++ = *bp;
  8808.     switch (*bp++) {
  8809.     case '/':
  8810.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  8811.     warning ("`/*' within comment");
  8812.       break;
  8813.     case '\n':
  8814.       /* If this is the end of the file, we have an unterminated comment.
  8815.      Don't swallow the newline.  We are guaranteed that there will be a
  8816.      trailing newline and various pieces assume it's there.  */
  8817.       if (bp == limit)
  8818.     {
  8819.       --bp;
  8820.       --limit;
  8821.       break;
  8822.     }
  8823.       if (line_counter != NULL)
  8824.     ++*line_counter;
  8825.       if (output)
  8826.     ++op->lineno;
  8827.       break;
  8828.     case '*':
  8829.       if (*bp == '\\' && bp[1] == '\n')
  8830.     newline_fix (bp);
  8831.       if (*bp == '/') {
  8832.         if (output)
  8833.       *op->bufp++ = '/';
  8834.     ip->bufp = ++bp;
  8835.     return bp;
  8836.       }
  8837.       break;
  8838.     }
  8839.   }
  8840.  
  8841.   if (!nowarn)
  8842.     error_with_line (line_for_error (start_line), "unterminated comment");
  8843.   ip->bufp = bp;
  8844.   return bp;
  8845. }
  8846.  
  8847. /*
  8848.  * Skip over a quoted string.  BP points to the opening quote.
  8849.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  8850.  * START_LINE is the line number of the starting point (but it need
  8851.  * not be valid if the starting point is inside a macro expansion).
  8852.  *
  8853.  * The input stack state is not changed.
  8854.  *
  8855.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  8856.  * for each newline passed.
  8857.  *
  8858.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  8859.  * if we pass a backslash-newline.
  8860.  *
  8861.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  8862.  */
  8863. static U_CHAR *
  8864. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  8865.      register U_CHAR *bp;
  8866.      register U_CHAR *limit;
  8867.      int start_line;
  8868.      int *count_newlines;
  8869.      int *backslash_newlines_p;
  8870.      int *eofp;
  8871. {
  8872.   register U_CHAR c, match;
  8873.  
  8874.   match = *bp++;
  8875.   while (1) {
  8876.     if (bp >= limit) {
  8877.       error_with_line (line_for_error (start_line),
  8878.                "unterminated string or character constant");
  8879.       error_with_line (multiline_string_line,
  8880.                "possible real start of unterminated constant");
  8881.       multiline_string_line = 0;
  8882.       if (eofp)
  8883.     *eofp = 1;
  8884.       break;
  8885.     }
  8886.     c = *bp++;
  8887.     if (c == '\\') {
  8888.       while (*bp == '\\' && bp[1] == '\n') {
  8889.     if (backslash_newlines_p)
  8890.       *backslash_newlines_p = 1;
  8891.     if (count_newlines)
  8892.       ++*count_newlines;
  8893.     bp += 2;
  8894.       }
  8895.       if (*bp == '\n' && count_newlines) {
  8896.     if (backslash_newlines_p)
  8897.       *backslash_newlines_p = 1;
  8898.     ++*count_newlines;
  8899.       }
  8900.       bp++;
  8901.     } else if (c == '\n') {
  8902.       if (traditional) {
  8903.      /* Unterminated strings and character constants are 'legal'.  */
  8904.      bp--;    /* Don't consume the newline. */
  8905.      if (eofp)
  8906.        *eofp = 1;
  8907.      break;
  8908.       }
  8909.       if (pedantic || match == '\'') {
  8910.     error_with_line (line_for_error (start_line),
  8911.              "unterminated string or character constant");
  8912.     bp--;
  8913.     if (eofp)
  8914.       *eofp = 1;
  8915.     break;
  8916.       }
  8917.       /* If not traditional, then allow newlines inside strings.  */
  8918.       if (count_newlines)
  8919.     ++*count_newlines;
  8920.       if (multiline_string_line == 0)
  8921.     multiline_string_line = start_line;
  8922.     } else if (c == match)
  8923.       break;
  8924.   }
  8925.   return bp;
  8926. }
  8927.  
  8928. /* Place into DST a quoted string representing the string SRC.
  8929.    Return the address of DST's terminating null.  */
  8930. static char *
  8931. quote_string (dst, src)
  8932.      char *dst, *src;
  8933. {
  8934.   U_CHAR c;
  8935.  
  8936.   *dst++ = '\"';
  8937.   for (;;)
  8938.     switch ((c = *src++))
  8939.       {
  8940.       default:
  8941.         if (isprint (c))
  8942.       *dst++ = c;
  8943.     else
  8944.       {
  8945.         sprintf (dst, "\\%03o", c);
  8946.         dst += 4;
  8947.       }
  8948.     break;
  8949.  
  8950.       case '\"':
  8951.       case '\\':
  8952.     *dst++ = '\\';
  8953.     *dst++ = c;
  8954.     break;
  8955.       
  8956.       case '\0':
  8957.     *dst++ = '\"';
  8958.     *dst = '\0';
  8959.     return dst;
  8960.       }
  8961. }
  8962.  
  8963. /* Skip across a group of balanced parens, starting from IP->bufp.
  8964.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  8965.  
  8966.    This does not handle newlines, because it's used for the arg of #if,
  8967.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  8968.  
  8969. #ifdef NEXT_CPP_SERVER
  8970. static U_CHAR *
  8971. skip_paren_group (ip, op)
  8972.      register FILE_BUF *ip;
  8973.      register FILE_BUF *op;
  8974. #else
  8975. static U_CHAR *
  8976. skip_paren_group (ip)
  8977.      register FILE_BUF *ip;
  8978. #endif
  8979. {
  8980.   U_CHAR *limit = ip->buf + ip->length;
  8981.   U_CHAR *p = ip->bufp;
  8982.   int depth = 0;
  8983.   int lines_dummy = 0;
  8984.  
  8985.   while (p != limit) {
  8986.     int c = *p++;
  8987.     switch (c) {
  8988.     case '(':
  8989.       depth++;
  8990.       break;
  8991.  
  8992.     case ')':
  8993.       depth--;
  8994.       if (depth == 0)
  8995.     return ip->bufp = p;
  8996.       break;
  8997.  
  8998.     case '/':
  8999.       if (*p == '*') {
  9000.     ip->bufp = p;
  9001. #ifdef NEXT_CPP_SERVER
  9002.     p = skip_to_end_of_comment (ip, op, &lines_dummy, 0);
  9003. #else
  9004.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  9005. #endif
  9006.     p = ip->bufp;
  9007.       }
  9008.  
  9009.     case '"':
  9010.     case '\'':
  9011.       {
  9012.     int eofp = 0;
  9013.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  9014.     if (eofp)
  9015.       return ip->bufp = p;
  9016.       }
  9017.       break;
  9018.     }
  9019.   }
  9020.  
  9021.   ip->bufp = p;
  9022.   return p;
  9023. }
  9024.  
  9025. /*
  9026.  * write out a #line command, for instance, after an #include file.
  9027.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  9028.  * appear to be a no-op, and we can output a few newlines instead
  9029.  * if we want to increase the line number by a small amount.
  9030.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  9031.  */
  9032.  
  9033. static void
  9034. output_line_command (ip, op, conditional, file_change)
  9035.      FILE_BUF *ip, *op;
  9036.      int conditional;
  9037.      enum file_change_code file_change;
  9038. {
  9039.   int len;
  9040.   char *line_cmd_buf, *line_end;
  9041.  
  9042.   if (no_line_commands
  9043.       || ip->fname == NULL
  9044. #ifdef NEXT_CPP_SERVER
  9045.       || no_output
  9046.       /* allow all "notes" to main outbuf to pass through */
  9047.       || (ip->file && ip->file->header_output 
  9048.           && ((op == &sys_header_outbuf) || (op == &user_header_outbuf)))) { 
  9049. #else
  9050.       || no_output) {
  9051. #endif
  9052.     op->lineno = ip->lineno;
  9053.     return;
  9054.   }
  9055.  
  9056.   if (conditional) {
  9057.     if (ip->lineno == op->lineno)
  9058.       return;
  9059.  
  9060.     /* If the inherited line number is a little too small,
  9061.        output some newlines instead of a #line command.  */
  9062.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  9063.       check_expand (op, 10);
  9064.       while (ip->lineno > op->lineno) {
  9065.     *op->bufp++ = '\n';
  9066.     op->lineno++;
  9067.       }
  9068.       return;
  9069.     }
  9070.   }
  9071.  
  9072.   /* Don't output a line number of 0 if we can help it.  */
  9073.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  9074.       && *ip->bufp == '\n') {
  9075.     ip->lineno++;
  9076.     ip->bufp++;
  9077.   }
  9078.  
  9079.   line_cmd_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
  9080. #ifdef OUTPUT_LINE_COMMANDS
  9081.   sprintf (line_cmd_buf, "#line %d ", ip->lineno);
  9082. #else
  9083.   sprintf (line_cmd_buf, "# %d ", ip->lineno);
  9084. #endif
  9085.   line_end = quote_string (line_cmd_buf + strlen (line_cmd_buf),
  9086.                ip->nominal_fname);
  9087.  
  9088.   if (file_change != same_file) {
  9089.     *line_end++ = ' ';
  9090.     if (file_change == enter_file) *line_end++ = '1';
  9091.     else if (file_change == leave_file) *line_end++ = '2';
  9092.     else if (file_change == enter_expansion) *line_end++ = '5';
  9093.     else if (file_change == leave_expansion) *line_end++ = '6';
  9094.   }
  9095.  
  9096.   if (file_change == enter_expansion) { 
  9097.     /* note the position of macro reference */
  9098.     sprintf (line_end, " %d", (ip->bufp - strlen(ip->nominal_fname))
  9099.                               - ip->beg_of_line);
  9100.     line_end = &line_cmd_buf[strlen(line_cmd_buf)];
  9101.   }
  9102.   else if (file_change == leave_expansion) {
  9103.     /* note the original character position in the input stream */
  9104.     sprintf (line_end, " %d", ip->bufp - ip->beg_of_line);
  9105.     line_end = &line_cmd_buf[strlen(line_cmd_buf)];
  9106.   }
  9107.   /* Tell cc1 if following text comes from a system header file.  */
  9108.   else if (ip->system_header_p) {
  9109.     *line_end++ = ' ';
  9110.     *line_end++ = '3';
  9111.   }
  9112. #ifndef NO_IMPLICIT_EXTERN_C
  9113.   /* Tell cc1plus if following text should be treated as C.  */
  9114.   if (ip->system_header_p == 2 && cplusplus) {
  9115.     *line_end++ = ' ';
  9116.     *line_end++ = '4';
  9117.   }
  9118. #endif
  9119. #ifdef NEXT_CPP_SERVER
  9120.    if (serverized) {
  9121.       int record_stat_info = 0;
  9122.       if (file_change == enter_file) {
  9123.          /* header in need of being recorded */
  9124.          record_stat_info = 1;
  9125.       } else if ((file_change == same_file)
  9126.                  && (op == &outbuf)
  9127.                  && !outbuf_stat_info_recorded) {
  9128.          record_stat_info = 1;
  9129.          outbuf_stat_info_recorded = 1;
  9130.       }
  9131.       if (record_stat_info) {
  9132.          int hashval = import_hash (ip->fname);
  9133.          struct import_file *import = import_hash_table[hashval];
  9134.          /* Is the following loop slowing things down?  Needs investigation. */
  9135.          if (import) {
  9136.             while (import) {
  9137.                if (!strcmp (ip->fname, import->name->fname)) {
  9138.                   char tmp[64];
  9139.                   int len;
  9140.                   sprintf(tmp, " modtime=%d size=%d inode=%d dev=%d",
  9141.                           import->modtime, ip->length,
  9142.                           import->inode, import->dev);
  9143.                   len = strlen(tmp);
  9144.                   strncpy(line_end, tmp, len);
  9145.                   line_end += len;
  9146.                   break;
  9147.                }
  9148.                import = import->next;
  9149.             }
  9150.          } 
  9151.       }
  9152.   }
  9153. #endif
  9154.   *line_end++ = '\n';
  9155.   len = line_end - line_cmd_buf;
  9156.   check_expand (op, len + 1);
  9157.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  9158.     *op->bufp++ = '\n';
  9159.   bcopy ((char *) line_cmd_buf, (char *) op->bufp, len);
  9160.   op->bufp += len;
  9161.   op->lineno = ip->lineno;
  9162. }
  9163.  
  9164. /* This structure represents one parsed argument in a macro call.
  9165.    `raw' points to the argument text as written (`raw_length' is its length).
  9166.    `expanded' points to the argument's macro-expansion
  9167.    (its length is `expand_length').
  9168.    `stringified_length' is the length the argument would have
  9169.    if stringified.
  9170.    `use_count' is the number of times this macro arg is substituted
  9171.    into the macro.  If the actual use count exceeds 10, 
  9172.    the value stored is 10.
  9173.    `free1' and `free2', if nonzero, point to blocks to be freed
  9174.    when the macro argument data is no longer needed.  */
  9175.  
  9176. struct argdata {
  9177.   U_CHAR *raw, *expanded;
  9178.   int raw_length, expand_length;
  9179.   int stringified_length;
  9180.   U_CHAR *free1, *free2;
  9181.   char newlines;
  9182.   char comments;
  9183.   char use_count;
  9184. };
  9185.  
  9186. /* Expand a macro call.
  9187.    HP points to the symbol that is the macro being called.
  9188.    Put the result of expansion onto the input stack
  9189.    so that subsequent input by our caller will use it.
  9190.  
  9191.    If macro wants arguments, caller has already verified that
  9192.    an argument list follows; arguments come from the input stack.  */
  9193.  
  9194. static void
  9195. macroexpand (hp, op)
  9196.      HASHNODE *hp;
  9197.      FILE_BUF *op;
  9198. {
  9199.   int nargs;
  9200.   DEFINITION *defn = hp->value.defn;
  9201.   register U_CHAR *xbuf;
  9202.   int xbuf_len;
  9203.   int start_line = instack[indepth].lineno;
  9204.   int rest_args, rest_zero;
  9205.  
  9206.   CHECK_DEPTH (return;);
  9207.  
  9208.   /* it might not actually be a macro.  */
  9209.   if (hp->type != T_MACRO) {
  9210.     special_symbol (hp, op);
  9211.     return;
  9212.   }
  9213.  
  9214.   /* This macro is being used inside a #if, which means it must be */
  9215.   /* recorded as a precondition.  */
  9216.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  9217.     dump_single_macro (hp, pcp_outfile);
  9218.   
  9219.   nargs = defn->nargs;
  9220.  
  9221.   if (nargs >= 0) {
  9222.     register int i;
  9223.     struct argdata *args;
  9224.     char *parse_error = 0;
  9225.  
  9226.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  9227.  
  9228.     for (i = 0; i < nargs; i++) {
  9229.       args[i].raw = (U_CHAR *) "";
  9230.       args[i].expanded = 0;
  9231.       args[i].raw_length = args[i].expand_length
  9232.     = args[i].stringified_length = 0;
  9233.       args[i].free1 = args[i].free2 = 0;
  9234.       args[i].use_count = 0;
  9235.     }
  9236.  
  9237.     /* Parse all the macro args that are supplied.  I counts them.
  9238.        The first NARGS args are stored in ARGS.
  9239.        The rest are discarded.
  9240.        If rest_args is set then we assume macarg absorbed the rest of the args.
  9241.        */
  9242.     i = 0;
  9243.     rest_args = 0;
  9244.     do {
  9245.       /* Discard the open-parenthesis or comma before the next arg.  */
  9246.       ++instack[indepth].bufp;
  9247.       if (rest_args)
  9248.     continue;
  9249.       if (i < nargs || (nargs == 0 && i == 0)) {
  9250.     /* if we are working on last arg which absorbs rest of args... */
  9251.     if (i == nargs - 1 && defn->rest_args)
  9252.       rest_args = 1;
  9253.     parse_error = macarg (&args[i], rest_args);
  9254.       }
  9255.       else
  9256.     parse_error = macarg (NULL_PTR, 0);
  9257.       if (parse_error) {
  9258.     error_with_line (line_for_error (start_line), parse_error);
  9259.     break;
  9260.       }
  9261.       i++;
  9262.     } while (*instack[indepth].bufp != ')');
  9263.  
  9264.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  9265.     if (i == 1) {
  9266.       register U_CHAR *bp = args[0].raw;
  9267.       register U_CHAR *lim = bp + args[0].raw_length;
  9268.       /* cpp.texi says for foo ( ) we provide one argument.
  9269.      However, if foo wants just 0 arguments, treat this as 0.  */
  9270.       if (nargs == 0)
  9271.     while (bp != lim && is_space[*bp]) bp++;
  9272.       if (bp == lim)
  9273.     i = 0;
  9274.     }
  9275.  
  9276.     /* Don't output an error message if we have already output one for
  9277.        a parse error above.  */
  9278.     rest_zero = 0;
  9279.     if (nargs == 0 && i > 0) {
  9280.       if (! parse_error)
  9281.     error ("arguments given to macro `%s'", hp->name);
  9282.     } else if (i < nargs) {
  9283.       /* traditional C allows foo() if foo wants one argument.  */
  9284.       if (nargs == 1 && i == 0 && traditional)
  9285.     ;
  9286.       /* the rest args token is allowed to absorb 0 tokens */
  9287.       else if (i == nargs - 1 && defn->rest_args)
  9288.     rest_zero = 1;
  9289.       else if (parse_error)
  9290.     ;
  9291.       else if (i == 0)
  9292.     error ("macro `%s' used without args", hp->name);
  9293.       else if (i == 1)
  9294.     error ("macro `%s' used with just one arg", hp->name);
  9295.       else
  9296.     error ("macro `%s' used with only %d args", hp->name, i);
  9297.     } else if (i > nargs) {
  9298.       if (! parse_error)
  9299.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  9300.     }
  9301.  
  9302.     /* Swallow the closeparen.  */
  9303.     ++instack[indepth].bufp;
  9304.  
  9305.     /* If macro wants zero args, we parsed the arglist for checking only.
  9306.        Read directly from the macro definition.  */
  9307.     if (nargs == 0) {
  9308.       xbuf = defn->expansion;
  9309.       xbuf_len = defn->length;
  9310.     } else {
  9311.       register U_CHAR *exp = defn->expansion;
  9312.       register int offset;    /* offset in expansion,
  9313.                    copied a piece at a time */
  9314.       register int totlen;    /* total amount of exp buffer filled so far */
  9315.  
  9316.       register struct reflist *ap, *last_ap;
  9317.  
  9318.       /* Macro really takes args.  Compute the expansion of this call.  */
  9319.  
  9320.       /* Compute length in characters of the macro's expansion.
  9321.      Also count number of times each arg is used.  */
  9322.       xbuf_len = defn->length;
  9323.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  9324.     if (ap->stringify)
  9325.       xbuf_len += args[ap->argno].stringified_length;
  9326.     else if (ap->raw_before || ap->raw_after || traditional)
  9327.       /* Add 4 for two newline-space markers to prevent
  9328.          token concatenation.  */
  9329.       xbuf_len += args[ap->argno].raw_length + 4;
  9330.     else {
  9331.       /* We have an ordinary (expanded) occurrence of the arg.
  9332.          So compute its expansion, if we have not already.  */
  9333.       if (args[ap->argno].expanded == 0) {
  9334.         FILE_BUF obuf;
  9335.         obuf = expand_to_temp_buffer (args[ap->argno].raw,
  9336.                       args[ap->argno].raw + args[ap->argno].raw_length,
  9337.                       1, 0);
  9338.  
  9339.         args[ap->argno].expanded = obuf.buf;
  9340.         args[ap->argno].expand_length = obuf.length;
  9341.         args[ap->argno].free2 = obuf.buf;
  9342.       }
  9343.  
  9344.       /* Add 4 for two newline-space markers to prevent
  9345.          token concatenation.  */
  9346.       xbuf_len += args[ap->argno].expand_length + 4;
  9347.     }
  9348.     if (args[ap->argno].use_count < 10)
  9349.       args[ap->argno].use_count++;
  9350.       }
  9351.  
  9352.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  9353.  
  9354.       /* Generate in XBUF the complete expansion
  9355.      with arguments substituted in.
  9356.      TOTLEN is the total size generated so far.
  9357.      OFFSET is the index in the definition
  9358.      of where we are copying from.  */
  9359.       offset = totlen = 0;
  9360.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  9361.        last_ap = ap, ap = ap->next) {
  9362.     register struct argdata *arg = &args[ap->argno];
  9363.     int count_before = totlen;
  9364.  
  9365.     /* Add chars to XBUF.  */
  9366.     for (i = 0; i < ap->nchars; i++, offset++)
  9367.       xbuf[totlen++] = exp[offset];
  9368.  
  9369.     /* If followed by an empty rest arg with concatenation,
  9370.        delete the last run of nonwhite chars.  */
  9371.     if (rest_zero && totlen > count_before
  9372.         && ((ap->rest_args && ap->raw_before)
  9373.         || (last_ap != NULL && last_ap->rest_args
  9374.             && last_ap->raw_after))) {
  9375.       /* Delete final whitespace.  */
  9376.       while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
  9377.         totlen--;
  9378.       }
  9379.  
  9380.       /* Delete the nonwhites before them.  */
  9381.       while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
  9382.         totlen--;
  9383.       }
  9384.     }
  9385.  
  9386.     if (ap->stringify != 0) {
  9387.       int arglen = arg->raw_length;
  9388.       int escaped = 0;
  9389.       int in_string = 0;
  9390.       int c;
  9391.       i = 0;
  9392.       while (i < arglen
  9393.          && (c = arg->raw[i], is_space[c]))
  9394.         i++;
  9395.       while (i < arglen
  9396.          && (c = arg->raw[arglen - 1], is_space[c]))
  9397.         arglen--;
  9398.       if (!traditional)
  9399.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  9400.       for (; i < arglen; i++) {
  9401.         c = arg->raw[i];
  9402.  
  9403.         /* Special markers Newline Space
  9404.            generate nothing for a stringified argument.  */
  9405.         if (c == '\n' && arg->raw[i+1] != '\n') {
  9406.           i++;
  9407.           continue;
  9408.         }
  9409.  
  9410.         /* Internal sequences of whitespace are replaced by one space
  9411.            except within an string or char token.  */
  9412.         if (! in_string
  9413.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  9414.           while (1) {
  9415.         /* Note that Newline Space does occur within whitespace
  9416.            sequences; consider it part of the sequence.  */
  9417.         if (c == '\n' && is_space[arg->raw[i+1]])
  9418.           i += 2;
  9419.         else if (c != '\n' && is_space[c])
  9420.           i++;
  9421.         else break;
  9422.         c = arg->raw[i];
  9423.           }
  9424.           i--;
  9425.           c = ' ';
  9426.         }
  9427.  
  9428.         if (escaped)
  9429.           escaped = 0;
  9430.         else {
  9431.           if (c == '\\')
  9432.         escaped = 1;
  9433.           if (in_string) {
  9434.         if (c == in_string)
  9435.           in_string = 0;
  9436.           } else if (c == '\"' || c == '\'')
  9437.         in_string = c;
  9438.         }
  9439.  
  9440.         /* Escape these chars */
  9441.         if (c == '\"' || (in_string && c == '\\'))
  9442.           xbuf[totlen++] = '\\';
  9443.         if (isprint (c))
  9444.           xbuf[totlen++] = c;
  9445.         else {
  9446.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  9447.           totlen += 4;
  9448.         }
  9449.       }
  9450.       if (!traditional)
  9451.         xbuf[totlen++] = '\"'; /* insert ending quote */
  9452.     } else if (ap->raw_before || ap->raw_after || traditional) {
  9453.       U_CHAR *p1 = arg->raw;
  9454.       U_CHAR *l1 = p1 + arg->raw_length;
  9455.       if (ap->raw_before) {
  9456.         while (p1 != l1 && is_space[*p1]) p1++;
  9457.         while (p1 != l1 && is_idchar[*p1])
  9458.           xbuf[totlen++] = *p1++;
  9459.         /* Delete any no-reexpansion marker that follows
  9460.            an identifier at the beginning of the argument
  9461.            if the argument is concatenated with what precedes it.  */
  9462.         if (p1[0] == '\n' && p1[1] == '-')
  9463.           p1 += 2;
  9464.       } else if (!traditional) {
  9465.       /* Ordinary expanded use of the argument.
  9466.          Put in newline-space markers to prevent token pasting.  */
  9467.         xbuf[totlen++] = '\n';
  9468.         xbuf[totlen++] = ' ';
  9469.       }
  9470.       if (ap->raw_after) {
  9471.         /* Arg is concatenated after: delete trailing whitespace,
  9472.            whitespace markers, and no-reexpansion markers.  */
  9473.         while (p1 != l1) {
  9474.           if (is_space[l1[-1]]) l1--;
  9475.           else if (l1[-1] == '-') {
  9476.         U_CHAR *p2 = l1 - 1;
  9477.         /* If a `-' is preceded by an odd number of newlines then it
  9478.            and the last newline are a no-reexpansion marker.  */
  9479.         while (p2 != p1 && p2[-1] == '\n') p2--;
  9480.         if ((l1 - 1 - p2) & 1) {
  9481.           l1 -= 2;
  9482.         }
  9483.         else break;
  9484.           }
  9485.           else break;
  9486.         }
  9487.       }
  9488.  
  9489.       bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
  9490.       totlen += l1 - p1;
  9491.       if (!traditional && !ap->raw_after) {
  9492.         /* Ordinary expanded use of the argument.
  9493.            Put in newline-space markers to prevent token pasting.  */
  9494.         xbuf[totlen++] = '\n';
  9495.         xbuf[totlen++] = ' ';
  9496.       }
  9497.     } else {
  9498.       /* Ordinary expanded use of the argument.
  9499.          Put in newline-space markers to prevent token pasting.  */
  9500.       if (!traditional) {
  9501.         xbuf[totlen++] = '\n';
  9502.         xbuf[totlen++] = ' ';
  9503.       }
  9504.       bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
  9505.          arg->expand_length);
  9506.       totlen += arg->expand_length;
  9507.       if (!traditional) {
  9508.         xbuf[totlen++] = '\n';
  9509.         xbuf[totlen++] = ' ';
  9510.       }
  9511.       /* If a macro argument with newlines is used multiple times,
  9512.          then only expand the newlines once.  This avoids creating output
  9513.          lines which don't correspond to any input line, which confuses
  9514.          gdb and gcov.  */
  9515.       if (arg->use_count > 1 && arg->newlines > 0) {
  9516.         /* Don't bother doing change_newlines for subsequent
  9517.            uses of arg.  */
  9518.         arg->use_count = 1;
  9519.         arg->expand_length
  9520.           = change_newlines (arg->expanded, arg->expand_length);
  9521.       }
  9522.     }
  9523.  
  9524.     if (totlen > xbuf_len)
  9525.       abort ();
  9526.       }
  9527.  
  9528.       /* if there is anything left of the definition
  9529.      after handling the arg list, copy that in too. */
  9530.  
  9531.       for (i = offset; i < defn->length; i++) {
  9532.     /* if we've reached the end of the macro */
  9533.     if (exp[i] == ')')
  9534.       rest_zero = 0;
  9535.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  9536.            && last_ap->raw_after))
  9537.       xbuf[totlen++] = exp[i];
  9538.       }
  9539.  
  9540.       xbuf[totlen] = 0;
  9541.       xbuf_len = totlen;
  9542.  
  9543.       for (i = 0; i < nargs; i++) {
  9544.     if (args[i].free1 != 0)
  9545.       free (args[i].free1);
  9546.     if (args[i].free2 != 0)
  9547.       free (args[i].free2);
  9548.       }
  9549.     }
  9550.   } else {
  9551.     xbuf = defn->expansion;
  9552.     xbuf_len = defn->length;
  9553.   }
  9554.  
  9555.   /* Now put the expansion on the input stack
  9556.      so our caller will commence reading from it.  */
  9557.   {
  9558.     register FILE_BUF *ip2;
  9559.  
  9560.     ip2 = &instack[++indepth];
  9561.  
  9562.     ip2->fname = 0;
  9563.     ip2->nominal_fname = 0;
  9564.     /* This may not be exactly correct, but will give much better error
  9565.        messages for nested macro calls than using a line number of zero.  */
  9566.     ip2->lineno = start_line;
  9567.     ip2->buf = xbuf;
  9568.     ip2->length = xbuf_len;
  9569.     ip2->bufp = xbuf;
  9570.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  9571.     ip2->macro = hp;
  9572.     ip2->if_stack = if_stack;
  9573.     ip2->system_header_p = 0;
  9574.  
  9575.     /* Recursive macro use sometimes works traditionally.
  9576.        #define foo(x,y) bar (x (y,0), y)
  9577.        foo (foo, baz)  */
  9578.  
  9579.     if (!traditional)
  9580.       hp->type = T_DISABLED;
  9581.   }
  9582. }
  9583.  
  9584. /*
  9585.  * Parse a macro argument and store the info on it into *ARGPTR.
  9586.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  9587.  * Return nonzero to indicate a syntax error.
  9588.  */
  9589.  
  9590. static char *
  9591. macarg (argptr, rest_args)
  9592.      register struct argdata *argptr;
  9593.      int rest_args;
  9594. {
  9595.   FILE_BUF *ip = &instack[indepth];
  9596.   int paren = 0;
  9597.   int newlines = 0;
  9598.   int comments = 0;
  9599.  
  9600.   /* Try to parse as much of the argument as exists at this
  9601.      input stack level.  */
  9602.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  9603.             &paren, &newlines, &comments, rest_args);
  9604.  
  9605.   /* If we find the end of the argument at this level,
  9606.      set up *ARGPTR to point at it in the input stack.  */
  9607.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  9608.       && bp != ip->buf + ip->length) {
  9609.     if (argptr != 0) {
  9610.       argptr->raw = ip->bufp;
  9611.       argptr->raw_length = bp - ip->bufp;
  9612.       argptr->newlines = newlines;
  9613.     }
  9614.     ip->bufp = bp;
  9615.   } else {
  9616.     /* This input stack level ends before the macro argument does.
  9617.        We must pop levels and keep parsing.
  9618.        Therefore, we must allocate a temporary buffer and copy
  9619.        the macro argument into it.  */
  9620.     int bufsize = bp - ip->bufp;
  9621.     int extra = newlines;
  9622.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  9623.     int final_start = 0;
  9624.  
  9625.     bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
  9626.     ip->bufp = bp;
  9627.     ip->lineno += newlines;
  9628.  
  9629.     while (bp == ip->buf + ip->length) {
  9630.       if (instack[indepth].macro == 0) {
  9631.     free (buffer);
  9632.     return "unterminated macro call";
  9633.       }
  9634.       ip->macro->type = T_MACRO;
  9635.       if (ip->free_ptr)
  9636.     free (ip->free_ptr);
  9637.       ip = &instack[--indepth];
  9638.       newlines = 0;
  9639.       comments = 0;
  9640.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  9641.             &newlines, &comments, rest_args);
  9642.       final_start = bufsize;
  9643.       bufsize += bp - ip->bufp;
  9644.       extra += newlines;
  9645.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  9646.       bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
  9647.          bp - ip->bufp);
  9648.       ip->bufp = bp;
  9649.       ip->lineno += newlines;
  9650.     }
  9651.  
  9652.     /* Now, if arg is actually wanted, record its raw form,
  9653.        discarding comments and duplicating newlines in whatever
  9654.        part of it did not come from a macro expansion.
  9655.        EXTRA space has been preallocated for duplicating the newlines.
  9656.        FINAL_START is the index of the start of that part.  */
  9657.     if (argptr != 0) {
  9658.       argptr->raw = buffer;
  9659.       argptr->raw_length = bufsize;
  9660.       argptr->free1 = buffer;
  9661.       argptr->newlines = newlines;
  9662.       argptr->comments = comments;
  9663.       if ((newlines || comments) && ip->fname != 0)
  9664.     argptr->raw_length
  9665.       = final_start +
  9666.         discard_comments (argptr->raw + final_start,
  9667.                   argptr->raw_length - final_start,
  9668.                   newlines);
  9669.       argptr->raw[argptr->raw_length] = 0;
  9670.       if (argptr->raw_length > bufsize + extra)
  9671.     abort ();
  9672.     }
  9673.   }
  9674.  
  9675.   /* If we are not discarding this argument,
  9676.      macroexpand it and compute its length as stringified.
  9677.      All this info goes into *ARGPTR.  */
  9678.  
  9679.   if (argptr != 0) {
  9680.     register U_CHAR *buf, *lim;
  9681.     register int totlen;
  9682.  
  9683.     buf = argptr->raw;
  9684.     lim = buf + argptr->raw_length;
  9685.  
  9686.     while (buf != lim && is_space[*buf])
  9687.       buf++;
  9688.     while (buf != lim && is_space[lim[-1]])
  9689.       lim--;
  9690.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  9691.     while (buf != lim) {
  9692.       register U_CHAR c = *buf++;
  9693.       totlen++;
  9694.       /* Internal sequences of whitespace are replaced by one space
  9695.      in most cases, but not always.  So count all the whitespace
  9696.      in case we need to keep it all.  */
  9697. #if 0
  9698.       if (is_space[c])
  9699.     SKIP_ALL_WHITE_SPACE (buf);
  9700.       else
  9701. #endif
  9702.       if (c == '\"' || c == '\\') /* escape these chars */
  9703.     totlen++;
  9704.       else if (!isprint (c))
  9705.     totlen += 3;
  9706.     }
  9707.     argptr->stringified_length = totlen;
  9708.   }
  9709.   return 0;
  9710. }
  9711.  
  9712. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  9713.    counting parens in *DEPTHPTR,
  9714.    and return if reach LIMIT
  9715.    or before a `)' that would make *DEPTHPTR negative
  9716.    or before a comma when *DEPTHPTR is zero.
  9717.    Single and double quotes are matched and termination
  9718.    is inhibited within them.  Comments also inhibit it.
  9719.    Value returned is pointer to stopping place.
  9720.  
  9721.    Increment *NEWLINES each time a newline is passed.
  9722.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  9723.    Set *COMMENTS to 1 if a comment is seen.  */
  9724.  
  9725. static U_CHAR *
  9726. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  9727.      U_CHAR *start;
  9728.      register U_CHAR *limit;
  9729.      int *depthptr, *newlines, *comments;
  9730.      int rest_args;
  9731. {
  9732.   register U_CHAR *bp = start;
  9733.  
  9734.   while (bp < limit) {
  9735.     switch (*bp) {
  9736.     case '(':
  9737.       (*depthptr)++;
  9738.       break;
  9739.     case ')':
  9740.       if (--(*depthptr) < 0)
  9741.     return bp;
  9742.       break;
  9743.     case '\\':
  9744.       /* Traditionally, backslash makes following char not special.  */
  9745.       if (bp + 1 < limit && traditional)
  9746.     {
  9747.       bp++;
  9748.       /* But count source lines anyway.  */
  9749.       if (*bp == '\n')
  9750.         ++*newlines;
  9751.     }
  9752.       break;
  9753.     case '\n':
  9754.       ++*newlines;
  9755.       break;
  9756.     case '/':
  9757.       if (bp[1] == '\\' && bp[2] == '\n')
  9758.     newline_fix (bp + 1);
  9759.       if (cplusplus_comments && bp[1] == '/') {
  9760.     *comments = 1;
  9761.     bp += 2;
  9762.     while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
  9763.       if (*bp == '\n') ++*newlines;
  9764.       bp++;
  9765.     }
  9766.     break;
  9767.       }
  9768.       if (bp[1] != '*' || bp + 1 >= limit)
  9769.     break;
  9770.       *comments = 1;
  9771.       bp += 2;
  9772.       while (bp + 1 < limit) {
  9773.     if (bp[0] == '*'
  9774.         && bp[1] == '\\' && bp[2] == '\n')
  9775.       newline_fix (bp + 1);
  9776.     if (bp[0] == '*' && bp[1] == '/')
  9777.       break;
  9778.     if (*bp == '\n') ++*newlines;
  9779.     bp++;
  9780.       }
  9781.       break;
  9782.     case '\'':
  9783.     case '\"':
  9784.       {
  9785.     int quotec;
  9786.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  9787.       if (*bp == '\\') {
  9788.         bp++;
  9789.         if (*bp == '\n')
  9790.           ++*newlines;
  9791.         while (*bp == '\\' && bp[1] == '\n') {
  9792.           bp += 2;
  9793.         }
  9794.       } else if (*bp == '\n') {
  9795.         ++*newlines;
  9796.         if (quotec == '\'')
  9797.           break;
  9798.       }
  9799.     }
  9800.       }
  9801.       break;
  9802.     case ',':
  9803.       /* if we've returned to lowest level and we aren't absorbing all args */
  9804.       if ((*depthptr) == 0 && rest_args == 0)
  9805.     return bp;
  9806.       break;
  9807.     }
  9808.     bp++;
  9809.   }
  9810.  
  9811.   return bp;
  9812. }
  9813.  
  9814. /* Discard comments and duplicate newlines
  9815.    in the string of length LENGTH at START,
  9816.    except inside of string constants.
  9817.    The string is copied into itself with its beginning staying fixed.  
  9818.  
  9819.    NEWLINES is the number of newlines that must be duplicated.
  9820.    We assume that that much extra space is available past the end
  9821.    of the string.  */
  9822.  
  9823. static int
  9824. discard_comments (start, length, newlines)
  9825.      U_CHAR *start;
  9826.      int length;
  9827.      int newlines;
  9828. {
  9829.   register U_CHAR *ibp;
  9830.   register U_CHAR *obp;
  9831.   register U_CHAR *limit;
  9832.   register int c;
  9833.  
  9834.   /* If we have newlines to duplicate, copy everything
  9835.      that many characters up.  Then, in the second part,
  9836.      we will have room to insert the newlines
  9837.      while copying down.
  9838.      NEWLINES may actually be too large, because it counts
  9839.      newlines in string constants, and we don't duplicate those.
  9840.      But that does no harm.  */
  9841.   if (newlines > 0) {
  9842.     ibp = start + length;
  9843.     obp = ibp + newlines;
  9844.     limit = start;
  9845.     while (limit != ibp)
  9846.       *--obp = *--ibp;
  9847.   }
  9848.  
  9849.   ibp = start + newlines;
  9850.   limit = start + length + newlines;
  9851.   obp = start;
  9852.  
  9853.   while (ibp < limit) {
  9854.     *obp++ = c = *ibp++;
  9855.     switch (c) {
  9856.     case '\n':
  9857.       /* Duplicate the newline.  */
  9858.       *obp++ = '\n';
  9859.       break;
  9860.  
  9861.     case '\\':
  9862.       if (*ibp == '\n') {
  9863.     obp--;
  9864.     ibp++;
  9865.       }
  9866.       break;
  9867.  
  9868.     case '/':
  9869.       if (*ibp == '\\' && ibp[1] == '\n')
  9870.     newline_fix (ibp);
  9871.       /* Delete any comment.  */
  9872.       if (cplusplus_comments && ibp[0] == '/') {
  9873.     /* Comments are equivalent to spaces.  */
  9874.     obp[-1] = ' ';
  9875.     ibp++;
  9876.     while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
  9877.       ibp++;
  9878.     break;
  9879.       }
  9880.       if (ibp[0] != '*' || ibp + 1 >= limit)
  9881.     break;
  9882.       /* Comments are equivalent to spaces.  */
  9883.       obp[-1] = ' ';
  9884.       ibp++;
  9885.       while (ibp + 1 < limit) {
  9886.     if (ibp[0] == '*'
  9887.         && ibp[1] == '\\' && ibp[2] == '\n')
  9888.       newline_fix (ibp + 1);
  9889.     if (ibp[0] == '*' && ibp[1] == '/')
  9890.       break;
  9891.     ibp++;
  9892.       }
  9893.       ibp += 2;
  9894.       break;
  9895.  
  9896.     case '\'':
  9897.     case '\"':
  9898.       /* Notice and skip strings, so that we don't
  9899.      think that comments start inside them,
  9900.      and so we don't duplicate newlines in them.  */
  9901.       {
  9902.     int quotec = c;
  9903.     while (ibp < limit) {
  9904.       *obp++ = c = *ibp++;
  9905.       if (c == quotec)
  9906.         break;
  9907.       if (c == '\n' && quotec == '\'')
  9908.         break;
  9909.       if (c == '\\' && ibp < limit) {
  9910.         while (*ibp == '\\' && ibp[1] == '\n')
  9911.           ibp += 2;
  9912.         *obp++ = *ibp++;
  9913.       }
  9914.     }
  9915.       }
  9916.       break;
  9917.     }
  9918.   }
  9919.  
  9920.   return obp - start;
  9921. }
  9922.  
  9923. /* Turn newlines to spaces in the string of length LENGTH at START,
  9924.    except inside of string constants.
  9925.    The string is copied into itself with its beginning staying fixed.  */
  9926.  
  9927. static int
  9928. change_newlines (start, length)
  9929.      U_CHAR *start;
  9930.      int length;
  9931. {
  9932.   register U_CHAR *ibp;
  9933.   register U_CHAR *obp;
  9934.   register U_CHAR *limit;
  9935.   register int c;
  9936.  
  9937.   ibp = start;
  9938.   limit = start + length;
  9939.   obp = start;
  9940.  
  9941.   while (ibp < limit) {
  9942.     *obp++ = c = *ibp++;
  9943.     switch (c) {
  9944.     case '\n':
  9945.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  9946.      string.  Skip past the newline and its duplicate.
  9947.      Put a space in the output.  */
  9948.       if (*ibp == '\n')
  9949.     {
  9950.       ibp++;
  9951.       obp--;
  9952.       *obp++ = ' ';
  9953.     }
  9954.       break;
  9955.  
  9956.     case '\'':
  9957.     case '\"':
  9958.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  9959.       {
  9960.     int quotec = c;
  9961.     while (ibp < limit) {
  9962.       *obp++ = c = *ibp++;
  9963.       if (c == quotec)
  9964.         break;
  9965.       if (c == '\n' && quotec == '\'')
  9966.         break;
  9967.     }
  9968.       }
  9969.       break;
  9970.     }
  9971.   }
  9972.  
  9973.   return obp - start;
  9974. }
  9975.  
  9976. /*
  9977.  * error - print error message and increment count of errors.
  9978.  */
  9979.  
  9980. void
  9981. error (msg, arg1, arg2, arg3)
  9982.      char *msg;
  9983.      char *arg1, *arg2, *arg3;
  9984. {
  9985.   int i;
  9986.   FILE_BUF *ip = NULL;
  9987.  
  9988.   print_containing_files ();
  9989.  
  9990.   for (i = indepth; i >= 0; i--)
  9991.     if (instack[i].fname != NULL) {
  9992.       ip = &instack[i];
  9993.       break;
  9994.     }
  9995.  
  9996. #ifdef REPORT_EVENT
  9997.   REPORT_EVENT (0, NULL,
  9998.         ip ? ip->nominal_fname : NULL,
  9999.         ip ? ip->lineno : 0,
  10000.         msg, arg1, arg2, arg3);
  10001. #endif
  10002.  
  10003.   if (ip != NULL)
  10004.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  10005.   fprintf (stderr, msg, arg1, arg2, arg3);
  10006.   fprintf (stderr, "\n");
  10007.   errors++;
  10008. }
  10009.  
  10010. /* Error including a message from `errno'.  */
  10011.  
  10012. static void
  10013. error_from_errno (name)
  10014.      char *name;
  10015. {
  10016.   int i;
  10017.   FILE_BUF *ip = NULL;
  10018.  
  10019.   print_containing_files ();
  10020.  
  10021.   for (i = indepth; i >= 0; i--)
  10022.     if (instack[i].fname != NULL) {
  10023.       ip = &instack[i];
  10024.       break;
  10025.     }
  10026.  
  10027. #ifdef REPORT_EVENT
  10028.   REPORT_EVENT (0, NULL,
  10029.         ip ? ip->nominal_fname : NULL,
  10030.         ip ? ip->lineno : 0,
  10031.         "%s: %s", name,
  10032.         (errno < sys_nerr)
  10033.         ? sys_errlist[errno]
  10034.         : "undocumented I/O error",
  10035.         0);
  10036. #endif
  10037.  
  10038.   if (ip != NULL)
  10039.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  10040.  
  10041.   if (errno < sys_nerr)
  10042.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  10043.   else
  10044.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  10045.  
  10046.   errors++;
  10047. }
  10048.  
  10049. /* Print error message but don't count it.  */
  10050.  
  10051. void
  10052. warning (msg, arg1, arg2, arg3)
  10053.      char *msg;
  10054.      char *arg1, *arg2, *arg3;
  10055. {
  10056.   int i;
  10057.   FILE_BUF *ip = NULL;
  10058.  
  10059.   if (inhibit_warnings)
  10060.     return;
  10061.  
  10062.   if (warnings_are_errors)
  10063.     errors++;
  10064.  
  10065.   print_containing_files ();
  10066.  
  10067.   for (i = indepth; i >= 0; i--)
  10068.     if (instack[i].fname != NULL) {
  10069.       ip = &instack[i];
  10070.       break;
  10071.     }
  10072.  
  10073. #ifdef REPORT_EVENT
  10074.   REPORT_EVENT (1, NULL,
  10075.         ip ? ip->nominal_fname : NULL,
  10076.         ip ? ip->lineno : 0,
  10077.         msg, arg1, arg2, arg3);
  10078. #endif
  10079.  
  10080.   if (ip != NULL)
  10081.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  10082.   fprintf (stderr, "warning: ");
  10083.   fprintf (stderr, msg, arg1, arg2, arg3);
  10084.   fprintf (stderr, "\n");
  10085. }
  10086.  
  10087. static void
  10088. error_with_line (line, msg, arg1, arg2, arg3)
  10089.      int line;
  10090.      char *msg;
  10091.      char *arg1, *arg2, *arg3;
  10092. {
  10093.   int i;
  10094.   FILE_BUF *ip = NULL;
  10095.  
  10096.   print_containing_files ();
  10097.  
  10098.   for (i = indepth; i >= 0; i--)
  10099.     if (instack[i].fname != NULL) {
  10100.       ip = &instack[i];
  10101.       break;
  10102.     }
  10103.  
  10104. #ifdef REPORT_EVENT
  10105.   REPORT_EVENT (0, NULL,
  10106.         ip ? ip->nominal_fname : NULL,
  10107.         line, msg, arg1, arg2, arg3);
  10108. #endif
  10109.  
  10110.   if (ip != NULL)
  10111.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  10112.   fprintf (stderr, msg, arg1, arg2, arg3);
  10113.   fprintf (stderr, "\n");
  10114.   errors++;
  10115. }
  10116.  
  10117. static void
  10118. warning_with_line (line, msg, arg1, arg2, arg3)
  10119.      int line;
  10120.      char *msg;
  10121.      char *arg1, *arg2, *arg3;
  10122. {
  10123.   int i;
  10124.   FILE_BUF *ip = NULL;
  10125.  
  10126.   if (inhibit_warnings)
  10127.     return;
  10128.  
  10129.   if (warnings_are_errors)
  10130.     errors++;
  10131.  
  10132.   print_containing_files ();
  10133.  
  10134.   for (i = indepth; i >= 0; i--)
  10135.     if (instack[i].fname != NULL) {
  10136.       ip = &instack[i];
  10137.       break;
  10138.     }
  10139.  
  10140. #ifdef REPORT_EVENT
  10141.   REPORT_EVENT (0, NULL,
  10142.         ip ? ip->nominal_fname : NULL,
  10143.         line, msg, arg1, arg2, arg3);
  10144. #endif
  10145.  
  10146.   if (ip != NULL)
  10147.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  10148.   fprintf (stderr, "warning: ");
  10149.   fprintf (stderr, msg, arg1, arg2, arg3);
  10150.   fprintf (stderr, "\n");
  10151. }
  10152.  
  10153. /* print an error message and maybe count it.  */
  10154.  
  10155. void
  10156. pedwarn (msg, arg1, arg2, arg3)
  10157.      char *msg;
  10158.      char *arg1, *arg2, *arg3;
  10159. {
  10160.   if (pedantic_errors)
  10161.     error (msg, arg1, arg2, arg3);
  10162.   else
  10163.     warning (msg, arg1, arg2, arg3);
  10164. }
  10165.  
  10166. void
  10167. pedwarn_with_line (line, msg, arg1, arg2, arg3)
  10168.      int line;
  10169.      char *msg;
  10170.      char *arg1, *arg2, *arg3;
  10171. {
  10172.   if (pedantic_errors)
  10173.     error_with_line (line, msg, arg1, arg2, arg3);
  10174.   else
  10175.     warning_with_line (line, msg, arg1, arg2, arg3);
  10176. }
  10177.  
  10178. /* Report a warning (or an error if pedantic_errors)
  10179.    giving specified file name and line number, not current.  */
  10180.  
  10181. static void
  10182. pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
  10183.      char *file;
  10184.      int line;
  10185.      char *msg;
  10186.      char *arg1, *arg2, *arg3;
  10187. {
  10188. #ifdef REPORT_EVENT
  10189.   REPORT_EVENT (pedantic_errors ? 0 : 1,
  10190.         NULL, file, line, msg, arg1, arg2, arg3);
  10191. #endif
  10192.   if (!pedantic_errors && inhibit_warnings)
  10193.     return;
  10194.   if (file != NULL)
  10195.     fprintf (stderr, "%s:%d: ", file, line);
  10196.   if (pedantic_errors)
  10197.     errors++;
  10198.   if (!pedantic_errors)
  10199.     fprintf (stderr, "warning: ");
  10200.   fprintf (stderr, msg, arg1, arg2, arg3);
  10201.   fprintf (stderr, "\n");
  10202. }
  10203.  
  10204. /* Print the file names and line numbers of the #include
  10205.    commands which led to the current file.  */
  10206.  
  10207. static void
  10208. print_containing_files ()
  10209. {
  10210.   FILE_BUF *ip = NULL;
  10211.   int i;
  10212.   int first = 1;
  10213.  
  10214.   /* If stack of files hasn't changed since we last printed
  10215.      this info, don't repeat it.  */
  10216.   if (last_error_tick == input_file_stack_tick)
  10217.     return;
  10218.  
  10219.   for (i = indepth; i >= 0; i--)
  10220.     if (instack[i].fname != NULL) {
  10221.       ip = &instack[i];
  10222.       break;
  10223.     }
  10224.  
  10225.   /* Give up if we don't find a source file.  */
  10226.   if (ip == NULL)
  10227.     return;
  10228.  
  10229.   /* Find the other, outer source files.  */
  10230.   for (i--; i >= 0; i--)
  10231.     if (instack[i].fname != NULL) {
  10232.       ip = &instack[i];
  10233.       if (first) {
  10234.     first = 0;
  10235.     fprintf (stderr, "In file included");
  10236.       } else {
  10237.     fprintf (stderr, ",\n                ");
  10238.       }
  10239.  
  10240.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  10241.     }
  10242.   if (! first)
  10243.     fprintf (stderr, ":\n");
  10244.  
  10245.   /* Record we have printed the status as of this time.  */
  10246.   last_error_tick = input_file_stack_tick;
  10247. }
  10248.  
  10249. /* Return the line at which an error occurred.
  10250.    The error is not necessarily associated with the current spot
  10251.    in the input stack, so LINE says where.  LINE will have been
  10252.    copied from ip->lineno for the current input level.
  10253.    If the current level is for a file, we return LINE.
  10254.    But if the current level is not for a file, LINE is meaningless.
  10255.    In that case, we return the lineno of the innermost file.  */
  10256.  
  10257. static int
  10258. line_for_error (line)
  10259.      int line;
  10260. {
  10261.   int i;
  10262.   int line1 = line;
  10263.  
  10264.   for (i = indepth; i >= 0; ) {
  10265.     if (instack[i].fname != 0)
  10266.       return line1;
  10267.     i--;
  10268.     if (i < 0)
  10269.       return 0;
  10270.     line1 = instack[i].lineno;
  10271.   }
  10272.   abort ();
  10273.   /*NOTREACHED*/
  10274.   return 0;
  10275. }
  10276.  
  10277. /*
  10278.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  10279.  *
  10280.  * As things stand, nothing is ever placed in the output buffer to be
  10281.  * removed again except when it's KNOWN to be part of an identifier,
  10282.  * so flushing and moving down everything left, instead of expanding,
  10283.  * should work ok.
  10284.  */
  10285.  
  10286. /* You might think void was cleaner for the return type,
  10287.    but that would get type mismatch in check_expand in strict ANSI.  */
  10288. static int
  10289. grow_outbuf (obuf, needed)
  10290.      register FILE_BUF *obuf;
  10291.      register int needed;
  10292. {
  10293.   register U_CHAR *p;
  10294.   int minsize;
  10295.  
  10296.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  10297.     return 0;
  10298.  
  10299.   /* Make it at least twice as big as it is now.  */
  10300.   obuf->length *= 2;
  10301.   /* Make it have at least 150% of the free space we will need.  */
  10302.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  10303.   if (minsize > obuf->length)
  10304.     obuf->length = minsize;
  10305.  
  10306.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  10307.     memory_full ();
  10308.  
  10309.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  10310. #ifdef NEXT_CPP_SERVER
  10311.   obuf->output_written_to = p + (obuf->output_written_to - obuf->buf);
  10312. #endif
  10313.   obuf->buf = p;
  10314.  
  10315.   return 0;
  10316. }
  10317.  
  10318. /* Symbol table for macro names and special symbols */
  10319.  
  10320. /*
  10321.  * install a name in the main hash table, even if it is already there.
  10322.  *   name stops with first non alphanumeric, except leading '#'.
  10323.  * caller must check against redefinition if that is desired.
  10324.  * delete_macro () removes things installed by install () in fifo order.
  10325.  * this is important because of the `defined' special symbol used
  10326.  * in #if, and also if pushdef/popdef directives are ever implemented.
  10327.  *
  10328.  * If LEN is >= 0, it is the length of the name.
  10329.  * Otherwise, compute the length by scanning the entire name.
  10330.  *
  10331.  * If HASH is >= 0, it is the precomputed hash code.
  10332.  * Otherwise, compute the hash code.
  10333.  */
  10334. static HASHNODE *
  10335. install (name, len, type, ivalue, value, hash)
  10336.      U_CHAR *name;
  10337.      int len;
  10338.      enum node_type type;
  10339.      int ivalue;
  10340.      char *value;
  10341.      int hash;
  10342. {
  10343.   register HASHNODE *hp;
  10344.   register int i, bucket;
  10345.   register U_CHAR *p, *q;
  10346.  
  10347.   if (len < 0) {
  10348.     p = name;
  10349.     while (is_idchar[*p])
  10350.       p++;
  10351.     len = p - name;
  10352.   }
  10353.  
  10354.   if (hash < 0)
  10355.     hash = hashf (name, len, HASHSIZE);
  10356.  
  10357.   i = sizeof (HASHNODE) + len + 1;
  10358.   hp = (HASHNODE *) xmalloc (i);
  10359.   bucket = hash;
  10360.   hp->bucket_hdr = &hashtab[bucket];
  10361.   hp->next = hashtab[bucket];
  10362.   hashtab[bucket] = hp;
  10363.   hp->prev = NULL;
  10364.   if (hp->next != NULL)
  10365.     hp->next->prev = hp;
  10366.   hp->type = type;
  10367.   hp->length = len;
  10368.   if (hp->type == T_CONST)
  10369.     hp->value.ival = ivalue;
  10370.   else
  10371.     hp->value.cpval = value;
  10372.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  10373. #ifdef NEXT_CPP_SERVER
  10374.   hp->defined_within_module = 1;
  10375.   hp->owner = 0;
  10376. #endif
  10377.   p = hp->name;
  10378.   q = name;
  10379.   for (i = 0; i < len; i++)
  10380.     *p++ = *q++;
  10381.   hp->name[len] = 0;
  10382.   return hp;
  10383. }
  10384.  
  10385. /*
  10386.  * find the most recent hash node for name name (ending with first
  10387.  * non-identifier char) installed by install
  10388.  *
  10389.  * If LEN is >= 0, it is the length of the name.
  10390.  * Otherwise, compute the length by scanning the entire name.
  10391.  *
  10392.  * If HASH is >= 0, it is the precomputed hash code.
  10393.  * Otherwise, compute the hash code.
  10394.  */
  10395. HASHNODE *
  10396. lookup (name, len, hash)
  10397.      U_CHAR *name;
  10398.      int len;
  10399.      int hash;
  10400. {
  10401.   register U_CHAR *bp;
  10402.   register HASHNODE *bucket;
  10403.  
  10404.   if (len < 0) {
  10405.     for (bp = name; is_idchar[*bp]; bp++) ;
  10406.     len = bp - name;
  10407.   }
  10408. #ifdef NEXT_CPP_SERVER
  10409.   if (hash < 0) {
  10410.     hash = hashf (name, len, HASHSIZE);
  10411.  
  10412.     bucket = hashtab[hash];
  10413.     while (bucket) {
  10414.       if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  10415.         return bucket->defined_within_module ? bucket : NULL;
  10416.       bucket = bucket->next;
  10417.     }
  10418.   } else { /* snaroff: this mod should be improved */
  10419.     bucket = hashtab[hash];
  10420.     while (bucket) {
  10421.       if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  10422.         return bucket;
  10423.       bucket = bucket->next;
  10424.     }
  10425.   }
  10426. #else
  10427.   if (hash < 0)
  10428.     hash = hashf (name, len, HASHSIZE);
  10429.  
  10430.   bucket = hashtab[hash];
  10431.   while (bucket) {
  10432.     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  10433.       return bucket;
  10434.     bucket = bucket->next;
  10435.   }
  10436. #endif
  10437.   return NULL;
  10438. }
  10439.  
  10440. /*
  10441.  * Delete a hash node.  Some weirdness to free junk from macros.
  10442.  * More such weirdness will have to be added if you define more hash
  10443.  * types that need it.
  10444.  */
  10445.  
  10446. /* Note that the DEFINITION of a macro is removed from the hash table
  10447.    but its storage is not freed.  This would be a storage leak
  10448.    except that it is not reasonable to keep undefining and redefining
  10449.    large numbers of macros many times.
  10450.    In any case, this is necessary, because a macro can be #undef'd
  10451.    in the middle of reading the arguments to a call to it.
  10452.    If #undef freed the DEFINITION, that would crash.  */
  10453.  
  10454. static void
  10455. delete_macro (hp)
  10456.      HASHNODE *hp;
  10457. {
  10458.  
  10459.   if (hp->prev != NULL)
  10460.     hp->prev->next = hp->next;
  10461.   if (hp->next != NULL)
  10462.     hp->next->prev = hp->prev;
  10463.  
  10464.   /* make sure that the bucket chain header that
  10465.      the deleted guy was on points to the right thing afterwards. */
  10466.   if (hp == *hp->bucket_hdr)
  10467.     *hp->bucket_hdr = hp->next;
  10468.  
  10469. #ifdef NEXT_CPP_SERVER
  10470.   /* a server cannot afford to leak data not in the shared header
  10471.      cache, so queue macros to be freed in clear_all_macros() */
  10472.   if (!hp->owner) {
  10473.      if (hp->type == T_MACRO) {
  10474.         hp->next = free_undefined_macros;
  10475.         free_undefined_macros = hp;
  10476.      } else {
  10477.         free (hp);
  10478.      }
  10479.   }
  10480. #else
  10481. #if 0
  10482.   if (hp->type == T_MACRO) {
  10483.     DEFINITION *d = hp->value.defn;
  10484.     struct reflist *ap, *nextap;
  10485.  
  10486.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  10487.       nextap = ap->next;
  10488.       free (ap);
  10489.     }
  10490.     free (d);
  10491.   }
  10492. #endif
  10493.   free (hp);
  10494. #endif
  10495. }
  10496.  
  10497. /*
  10498.  * return hash function on name.  must be compatible with the one
  10499.  * computed a step at a time, elsewhere
  10500.  */
  10501. static int
  10502. hashf (name, len, hashsize)
  10503.      register U_CHAR *name;
  10504.      register int len;
  10505.      int hashsize;
  10506. {
  10507.   register int r = 0;
  10508.  
  10509.   while (len--)
  10510.     r = HASHSTEP (r, *name++);
  10511.  
  10512.   return MAKE_POS (r) % hashsize;
  10513. }
  10514.  
  10515.  
  10516. /* Dump the definition of a single macro HP to OF.  */
  10517. static void
  10518. dump_single_macro (hp, of)
  10519.      register HASHNODE *hp;
  10520.      FILE *of;
  10521. {
  10522.   register DEFINITION *defn = hp->value.defn;
  10523.   struct reflist *ap;
  10524.   int offset;
  10525.   int concat;
  10526.  
  10527.  
  10528.   /* Print the definition of the macro HP.  */
  10529.  
  10530.   fprintf (of, "#define %s", hp->name);
  10531.  
  10532.   if (defn->nargs >= 0) {
  10533.     int i;
  10534.  
  10535.     fprintf (of, "(");
  10536.     for (i = 0; i < defn->nargs; i++) {
  10537.       dump_arg_n (defn, i, of);
  10538.       if (i + 1 < defn->nargs)
  10539.     fprintf (of, ", ");
  10540.     }
  10541.     fprintf (of, ")");
  10542.   }
  10543.  
  10544.   fprintf (of, " ");
  10545.  
  10546.   offset = 0;
  10547.   concat = 0;
  10548.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  10549.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  10550.     if (ap->nchars != 0)
  10551.       concat = 0;
  10552.     offset += ap->nchars;
  10553.     if (ap->stringify)
  10554.       fprintf (of, " #");
  10555.     if (ap->raw_before && !concat)
  10556.       fprintf (of, " ## ");
  10557.     concat = 0;
  10558.     dump_arg_n (defn, ap->argno, of);
  10559.     if (ap->raw_after) {
  10560.       fprintf (of, " ## ");
  10561.       concat = 1;
  10562.     }
  10563.   }
  10564.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  10565.   fprintf (of, "\n");
  10566. }
  10567.  
  10568. /* Dump all macro definitions as #defines to stdout.  */
  10569.  
  10570. static void
  10571. dump_all_macros ()
  10572. {
  10573.   int bucket;
  10574.  
  10575.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  10576.     register HASHNODE *hp;
  10577.  
  10578.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  10579.       if (hp->type == T_MACRO)
  10580.     dump_single_macro (hp, stdout);
  10581.     }
  10582.   }
  10583. }
  10584.  
  10585. /* Output to OF a substring of a macro definition.
  10586.    BASE is the beginning of the definition.
  10587.    Output characters START thru LENGTH.
  10588.    Discard newlines outside of strings, thus
  10589.    converting funny-space markers to ordinary spaces.  */
  10590.  
  10591. static void
  10592. dump_defn_1 (base, start, length, of)
  10593.      U_CHAR *base;
  10594.      int start;
  10595.      int length;
  10596.      FILE *of;
  10597. {
  10598.   U_CHAR *p = base + start;
  10599.   U_CHAR *limit = base + start + length;
  10600.  
  10601.   while (p < limit) {
  10602.     if (*p != '\n')
  10603.       putc (*p, of);
  10604.     else if (*p == '\"' || *p =='\'') {
  10605.       U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  10606.                        NULL_PTR, NULL_PTR);
  10607.       fwrite (p, p1 - p, 1, of);
  10608.       p = p1 - 1;
  10609.     }
  10610.     p++;
  10611.   }
  10612. }
  10613.  
  10614. /* Print the name of argument number ARGNUM of macro definition DEFN
  10615.    to OF.
  10616.    Recall that DEFN->args.argnames contains all the arg names
  10617.    concatenated in reverse order with comma-space in between.  */
  10618.  
  10619. static void
  10620. dump_arg_n (defn, argnum, of)
  10621.      DEFINITION *defn;
  10622.      int argnum;
  10623.      FILE *of;
  10624. {
  10625.   register U_CHAR *p = defn->args.argnames;
  10626.   while (argnum + 1 < defn->nargs) {
  10627.     p = (U_CHAR *) index (p, ' ') + 1;
  10628.     argnum++;
  10629.   }
  10630.  
  10631.   while (*p && *p != ',') {
  10632.     putc (*p, of);
  10633.     p++;
  10634.   }
  10635. }
  10636.  
  10637. /* Initialize syntactic classifications of characters.  */
  10638.  
  10639. static void
  10640. initialize_char_syntax ()
  10641. {
  10642.   register int i;
  10643.  
  10644.   /*
  10645.    * Set up is_idchar and is_idstart tables.  These should be
  10646.    * faster than saying (is_alpha (c) || c == '_'), etc.
  10647.    * Set up these things before calling any routines tthat
  10648.    * refer to them.
  10649.    */
  10650.   for (i = 'a'; i <= 'z'; i++) {
  10651.     is_idchar[i - 'a' + 'A'] = 1;
  10652.     is_idchar[i] = 1;
  10653.     is_idstart[i - 'a' + 'A'] = 1;
  10654.     is_idstart[i] = 1;
  10655.   }
  10656.   for (i = '0'; i <= '9'; i++)
  10657.     is_idchar[i] = 1;
  10658.   is_idchar['_'] = 1;
  10659.   is_idstart['_'] = 1;
  10660.   is_idchar['$'] = dollars_in_ident;
  10661.   is_idstart['$'] = dollars_in_ident;
  10662.  
  10663.   /* horizontal space table */
  10664.   is_hor_space[' '] = 1;
  10665.   is_hor_space['\t'] = 1;
  10666.   is_hor_space['\v'] = 1;
  10667.   is_hor_space['\f'] = 1;
  10668.   is_hor_space['\r'] = 1;
  10669.  
  10670.   is_space[' '] = 1;
  10671.   is_space['\t'] = 1;
  10672.   is_space['\v'] = 1;
  10673.   is_space['\f'] = 1;
  10674.   is_space['\n'] = 1;
  10675.   is_space['\r'] = 1;
  10676. }
  10677.  
  10678. /* Initialize the built-in macros.  */
  10679.  
  10680. static void
  10681. initialize_builtins (inp, outp)
  10682.      FILE_BUF *inp;
  10683.      FILE_BUF *outp;
  10684. {
  10685.   install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
  10686.   install ("__DATE__", -1, T_DATE, 0, 0, -1);
  10687.   install ("__FILE__", -1, T_FILE, 0, 0, -1);
  10688.   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
  10689.   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
  10690.   install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
  10691. #ifndef NO_BUILTIN_SIZE_TYPE
  10692.   install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
  10693. #endif
  10694. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  10695.   install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
  10696. #endif
  10697.   install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
  10698.   install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
  10699.   install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
  10700.   install ("__TIME__", -1, T_TIME, 0, 0, -1);
  10701.   if (!traditional)
  10702.     install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
  10703.   if (objc)
  10704.     install ("__OBJC__", -1, T_CONST, 1, 0, -1);
  10705. #ifdef NEXT_CPP_SERVER
  10706.   install ("_NEXT_SOURCE", -1, T_CONST, 1, 0, -1);
  10707.   install ("__GNU__", -1, T_CONST, 1, 0, -1);
  10708.   install ("__GNUC__", -1, T_CONST, 2, 0, -1);
  10709. #else
  10710. /*  This is supplied using a -D by the compiler driver
  10711.     so that it is present only when truly compiling with GNU C.  */
  10712. /*  install ("__GNUC__", -1, T_CONST, 2, 0, -1);  */
  10713. #endif
  10714.   if (debug_output)
  10715.     {
  10716.       char directive[2048];
  10717.       register struct directive *dp = &directive_table[0];
  10718.       struct tm *timebuf = timestamp ();
  10719.  
  10720.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  10721.            instack[0].nominal_fname);
  10722.       output_line_command (inp, outp, 0, same_file);
  10723.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10724.  
  10725.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  10726.       output_line_command (inp, outp, 0, same_file);
  10727.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10728.  
  10729. #ifndef NO_BUILTIN_SIZE_TYPE
  10730.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  10731.       output_line_command (inp, outp, 0, same_file);
  10732.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10733. #endif
  10734.  
  10735. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  10736.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  10737.       output_line_command (inp, outp, 0, same_file);
  10738.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10739. #endif
  10740.  
  10741.       sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
  10742.       output_line_command (inp, outp, 0, same_file);
  10743.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10744.  
  10745.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  10746.            monthnames[timebuf->tm_mon],
  10747.            timebuf->tm_mday, timebuf->tm_year + 1900);
  10748.       output_line_command (inp, outp, 0, same_file);
  10749.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10750.  
  10751.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  10752.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  10753.       output_line_command (inp, outp, 0, same_file);
  10754.       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
  10755.  
  10756.       if (!traditional)
  10757.     {
  10758.           sprintf (directive, " __STDC__ 1");
  10759.           output_line_command (inp, outp, 0, same_file);
  10760.           pass_thru_directive (directive, &directive[strlen (directive)],
  10761.                    outp, dp);
  10762.     }
  10763.       if (objc)
  10764.     {
  10765.           sprintf (directive, " __OBJC__ 1");
  10766.           output_line_command (inp, outp, 0, same_file);
  10767.           pass_thru_directive (directive, &directive[strlen (directive)],
  10768.                    outp, dp);
  10769.     }
  10770.     }
  10771. }
  10772.  
  10773. /*
  10774.  * process a given definition string, for initialization
  10775.  * If STR is just an identifier, define it with value 1.
  10776.  * If STR has anything after the identifier, then it should
  10777.  * be identifier=definition.
  10778.  */
  10779.  
  10780. static void
  10781. make_definition (str, op)
  10782.      U_CHAR *str;
  10783.      FILE_BUF *op;
  10784. {
  10785.   FILE_BUF *ip;
  10786.   struct directive *kt;
  10787.   U_CHAR *buf, *p;
  10788.  
  10789.   buf = str;
  10790.   p = str;
  10791.   if (!is_idstart[*p]) {
  10792.     error ("malformed option `-D %s'", str);
  10793.     return;
  10794.   }
  10795.   while (is_idchar[*++p])
  10796.     ;
  10797.   if (*p == 0) {
  10798.     buf = (U_CHAR *) alloca (p - buf + 4);
  10799.     strcpy ((char *)buf, str);
  10800.     strcat ((char *)buf, " 1");
  10801.   } else if (*p != '=') {
  10802.     error ("malformed option `-D %s'", str);
  10803.     return;
  10804.   } else {
  10805.     U_CHAR *q;
  10806.     /* Copy the entire option so we can modify it.  */
  10807.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  10808.     strncpy (buf, str, p - str);
  10809.     /* Change the = to a space.  */
  10810.     buf[p - str] = ' ';
  10811.     /* Scan for any backslash-newline and remove it.  */
  10812.     p++;
  10813.     q = &buf[p - str];
  10814.     while (*p) {
  10815.       if (*p == '\\' && p[1] == '\n')
  10816.     p += 2;
  10817.       /* Change newline chars into newline-markers.  */
  10818.       else if (*p == '\n')
  10819.     {
  10820.       *q++ = '\n';
  10821.       *q++ = '\n';
  10822.       p++;
  10823.     }
  10824.       else
  10825.     *q++ = *p++;
  10826.     }
  10827.     *q = 0;
  10828.   }
  10829.   
  10830.   ip = &instack[++indepth];
  10831.   ip->nominal_fname = ip->fname = "*Initialization*";
  10832.  
  10833.   ip->buf = ip->bufp = buf;
  10834.   ip->length = strlen (buf);
  10835.   ip->lineno = 1;
  10836.   ip->macro = 0;
  10837.   ip->free_ptr = 0;
  10838.   ip->if_stack = if_stack;
  10839.   ip->system_header_p = 0;
  10840. #ifdef NEXT_CPP_SERVER
  10841.   ip->dir = ip->file = 0;
  10842.   ip->beg_of_line = 0;
  10843. #endif
  10844.  
  10845.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  10846.     ;
  10847.  
  10848.   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
  10849.   do_define (buf, buf + strlen (buf), NULL, kt);
  10850.   --indepth;
  10851. }
  10852.  
  10853. /* JF, this does the work for the -U option */
  10854.  
  10855. static void
  10856. make_undef (str, op)
  10857.      U_CHAR *str;
  10858.      FILE_BUF *op;
  10859. {
  10860.   FILE_BUF *ip;
  10861.   struct directive *kt;
  10862.  
  10863.   ip = &instack[++indepth];
  10864.   ip->nominal_fname = ip->fname = "*undef*";
  10865.  
  10866.   ip->buf = ip->bufp = str;
  10867.   ip->length = strlen (str);
  10868.   ip->lineno = 1;
  10869.   ip->macro = 0;
  10870.   ip->free_ptr = 0;
  10871.   ip->if_stack = if_stack;
  10872.   ip->system_header_p = 0;
  10873.  
  10874.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  10875.     ;
  10876.  
  10877.   do_undef (str, str + strlen (str), op, kt);
  10878.   --indepth;
  10879. }
  10880.  
  10881. /* Process the string STR as if it appeared as the body of a #assert.
  10882.    OPTION is the option name for which STR was the argument.  */
  10883.  
  10884. static void
  10885. make_assertion (option, str)
  10886.      char *option;
  10887.      U_CHAR *str;
  10888. {
  10889.   FILE_BUF *ip;
  10890.   struct directive *kt;
  10891.   U_CHAR *buf, *p, *q;
  10892.  
  10893.   /* Copy the entire option so we can modify it.  */
  10894.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  10895.   strcpy ((char *) buf, str);
  10896.   /* Scan for any backslash-newline and remove it.  */
  10897.   p = q = buf;
  10898.   while (*p) {
  10899.     if (*p == '\\' && p[1] == '\n')
  10900.       p += 2;
  10901.     else
  10902.       *q++ = *p++;
  10903.   }
  10904.   *q = 0;
  10905.  
  10906.   p = buf;
  10907.   if (!is_idstart[*p]) {
  10908.     error ("malformed option `%s %s'", option, str);
  10909.     return;
  10910.   }
  10911.   while (is_idchar[*++p])
  10912.     ;
  10913.   while (*p == ' ' || *p == '\t') p++;
  10914.   if (! (*p == 0 || *p == '(')) {
  10915.     error ("malformed option `%s %s'", option, str);
  10916.     return;
  10917.   }
  10918.   
  10919.   ip = &instack[++indepth];
  10920.   ip->nominal_fname = ip->fname = "*Initialization*";
  10921.  
  10922.   ip->buf = ip->bufp = buf;
  10923.   ip->length = strlen (buf);
  10924.   ip->lineno = 1;
  10925.   ip->macro = 0;
  10926.   ip->free_ptr = 0;
  10927.   ip->if_stack = if_stack;
  10928.   ip->system_header_p = 0;
  10929. #ifdef NEXT_CPP_SERVER
  10930.   ip->dir = ip->file = 0;
  10931.   ip->beg_of_line = 0;
  10932. #endif
  10933.  
  10934.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  10935.     ;
  10936.  
  10937.   /* pass NULL as output ptr to do_define since we KNOW it never
  10938.      does any output.... */
  10939.   do_assert (buf, buf + strlen (buf) , NULL_PTR, kt);
  10940.   --indepth;
  10941. }
  10942.  
  10943. /* Append a chain of `struct file_name_list's
  10944.    to the end of the main include chain.
  10945.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  10946.  
  10947. static void
  10948. append_include_chain (first, last)
  10949.      struct file_name_list *first, *last;
  10950. {
  10951.   struct file_name_list *dir;
  10952.  
  10953.   if (!first || !last)
  10954.     return;
  10955.  
  10956.   if (include == 0)
  10957.     include = first;
  10958.   else
  10959.     last_include->next = first;
  10960.  
  10961.   if (first_bracket_include == 0)
  10962.     first_bracket_include = first;
  10963.  
  10964.   for (dir = first; ; dir = dir->next) {
  10965.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  10966.     if (len > max_include_len)
  10967.       max_include_len = len;
  10968.     if (dir == last)
  10969.       break;
  10970.   }
  10971.  
  10972.   last->next = NULL;
  10973.   last_include = last;
  10974. }
  10975.  
  10976. #ifdef NEXT_FRAMEWORKS
  10977. /* Append a chain of `struct file_name_list's
  10978.    to the end of the main Frameworks chain.
  10979.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  10980.  
  10981. static void
  10982. append_framework_chain (first, last)
  10983.      struct file_name_list *first, *last;
  10984. {
  10985.   struct file_name_list *dir;
  10986.  
  10987.   if (!first || !last)
  10988.     return;
  10989.  
  10990.   if (framework == 0)
  10991.     framework = first;
  10992.   else
  10993.     last_framework->next = first;
  10994.  
  10995.   if (first_bracket_framework == 0)
  10996.     first_bracket_framework = first;
  10997.  
  10998.   for (dir = first; ; dir = dir->next) {
  10999.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  11000.     if (len > max_include_len)
  11001.       max_include_len = len;
  11002.     if (dir == last)
  11003.       break;
  11004.   }
  11005.  
  11006.   last->next = NULL;
  11007.   last_framework = last;
  11008. }
  11009.  
  11010. #endif
  11011. /* Add output to `deps_buffer' for the -M switch.
  11012.    STRING points to the text to be output.
  11013.    SPACER is ':' for targets, ' ' for dependencies, zero for text
  11014.    to be inserted literally.  */
  11015.  
  11016. static void
  11017. deps_output (string, spacer)
  11018.      char *string;
  11019.      int spacer;
  11020. {
  11021.   int size = strlen (string);
  11022.  
  11023.   if (size == 0)
  11024.     return;
  11025.  
  11026. #ifndef MAX_OUTPUT_COLUMNS
  11027. #define MAX_OUTPUT_COLUMNS 72
  11028. #endif
  11029.   if (spacer
  11030.       && deps_column > 0
  11031.       && (deps_column + size) > MAX_OUTPUT_COLUMNS)
  11032.   {
  11033.     deps_output (" \\\n  ", 0);
  11034.     deps_column = 0;
  11035.   }
  11036.  
  11037.   if (deps_size + size + 8 > deps_allocated_size) {
  11038.     deps_allocated_size = (deps_size + size + 50) * 2;
  11039.     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
  11040.   }
  11041.   if (spacer == ' ' && deps_column > 0)
  11042.     deps_buffer[deps_size++] = ' ';
  11043.   bcopy (string, &deps_buffer[deps_size], size);
  11044.   deps_size += size;
  11045.   deps_column += size;
  11046.   if (spacer == ':')
  11047.     deps_buffer[deps_size++] = ':';
  11048.   deps_buffer[deps_size] = 0;
  11049. }
  11050.  
  11051. #if defined(USG) || defined(VMS)
  11052. #ifndef BSTRING
  11053.  
  11054. void
  11055. bzero (b, length)
  11056.      register char *b;
  11057.      register unsigned length;
  11058. {
  11059.   while (length-- > 0)
  11060.     *b++ = 0;
  11061. }
  11062.  
  11063. void
  11064. bcopy (b1, b2, length)
  11065.      register char *b1;
  11066.      register char *b2;
  11067.      register unsigned length;
  11068. {
  11069.   while (length-- > 0)
  11070.     *b2++ = *b1++;
  11071. }
  11072.  
  11073. int
  11074. bcmp (b1, b2, length)    /* This could be a macro! */
  11075.      register char *b1;
  11076.      register char *b2;
  11077.      register unsigned length;
  11078. {
  11079.    while (length-- > 0)
  11080.      if (*b1++ != *b2++)
  11081.        return 1;
  11082.  
  11083.    return 0;
  11084. }
  11085. #endif /* not BSTRING */
  11086. #endif /* USG or VMS */
  11087.  
  11088.  
  11089. static void
  11090. fatal (str, arg)
  11091.      char *str, *arg;
  11092. {
  11093. #ifdef REPORT_EVENT
  11094.   REPORT_EVENT (0, progname, NULL, 0, str, arg, 0, 0);
  11095. #endif
  11096.   fprintf (stderr, "%s: ", progname);
  11097.   fprintf (stderr, str, arg);
  11098.   fprintf (stderr, "\n");
  11099. #ifdef NEXT_CPP_SERVER
  11100.   if (serverized)
  11101.      return;
  11102. #endif    
  11103.   exit (FAILURE_EXIT_CODE);
  11104. }
  11105.  
  11106. /* More 'friendly' abort that prints the line and file.
  11107.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  11108.  
  11109. void
  11110. fancy_abort ()
  11111. {
  11112.   fatal ("Internal gcc abort.");
  11113. }
  11114.  
  11115. static void
  11116. perror_with_name (name)
  11117.      char *name;
  11118. {
  11119. #ifdef REPORT_EVENT
  11120.   REPORT_EVENT (0, progname, NULL, 0,
  11121.         "%s: %s", name,
  11122.         (errno < sys_nerr)
  11123.         ? sys_errlist[errno]
  11124.         : "undocumented I/O error",
  11125.         0);
  11126. #endif
  11127.   fprintf (stderr, "%s: ", progname);
  11128.   if (errno < sys_nerr)
  11129.     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
  11130.   else
  11131.     fprintf (stderr, "%s: undocumented I/O error\n", name);
  11132.   errors++;
  11133. }
  11134.  
  11135. static void
  11136. pfatal_with_name (name)
  11137.      char *name;
  11138. {
  11139.   perror_with_name (name);
  11140. #ifdef VMS
  11141.   exit (vaxc$errno);
  11142. #else
  11143.   exit (FAILURE_EXIT_CODE);
  11144. #endif
  11145. }
  11146.  
  11147. /* Handler for SIGPIPE.  */
  11148.  
  11149. static void
  11150. pipe_closed (signo)
  11151.      /* If this is missing, some compilers complain.  */
  11152.      int signo;
  11153. {
  11154.   fatal ("output pipe has been closed");
  11155. }
  11156.  
  11157. static void
  11158. memory_full ()
  11159. {
  11160.   fatal ("Memory exhausted.");
  11161. }
  11162.  
  11163.  
  11164. char *
  11165. xmalloc (size)
  11166.      unsigned size;
  11167. {
  11168.   register char *ptr = (char *) malloc (size);
  11169.   if (ptr != 0) return (ptr);
  11170.   memory_full ();
  11171.   /*NOTREACHED*/
  11172.   return 0;
  11173. }
  11174.  
  11175. static char *
  11176. xrealloc (old, size)
  11177.      char *old;
  11178.      unsigned size;
  11179. {
  11180.   register char *ptr = (char *) realloc (old, size);
  11181.   if (ptr != 0) return (ptr);
  11182.   memory_full ();
  11183.   /*NOTREACHED*/
  11184.   return 0;
  11185. }
  11186.  
  11187. static char *
  11188. xcalloc (number, size)
  11189.      unsigned number, size;
  11190. {
  11191.   register unsigned total = number * size;
  11192.   register char *ptr = (char *) malloc (total);
  11193.   if (ptr != 0) {
  11194.     if (total > 100)
  11195.       bzero (ptr, total);
  11196.     else {
  11197.       /* It's not too long, so loop, zeroing by longs.
  11198.      It must be safe because malloc values are always well aligned.  */
  11199.       register long *zp = (long *) ptr;
  11200.       register long *zl = (long *) (ptr + total - 4);
  11201.       register int i = total - 4;
  11202.       while (zp < zl)
  11203.     *zp++ = 0;
  11204.       if (i < 0)
  11205.     i = 0;
  11206.       while (i < total)
  11207.     ptr[i++] = 0;
  11208.     }
  11209.     return ptr;
  11210.   }
  11211.   memory_full ();
  11212.   /*NOTREACHED*/
  11213.   return 0;
  11214. }
  11215.  
  11216. static char *
  11217. savestring (input)
  11218.      char *input;
  11219. {
  11220.   unsigned size = strlen (input);
  11221.   char *output = xmalloc (size + 1);
  11222. #ifdef NEXT_FRAMEWORKS
  11223.   strncpy (output, input, size + 1);
  11224. #else  
  11225.   strcpy (output, input);
  11226. #endif
  11227.   return output;
  11228. }
  11229.  
  11230. /* Get the file-mode and data size of the file open on FD
  11231.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  11232.  
  11233. static int
  11234. file_size_and_mode (fd, mode_pointer, size_pointer)
  11235.      int fd;
  11236.      int *mode_pointer;
  11237.      long int *size_pointer;
  11238. {
  11239.   struct stat sbuf;
  11240.  
  11241.   if (fstat (fd, &sbuf) < 0) return (-1);
  11242.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  11243.   if (size_pointer) *size_pointer = sbuf.st_size;
  11244.   return 0;
  11245. }
  11246.  
  11247. static void
  11248. output_dots (fd, depth)
  11249.      FILE* fd;
  11250.      int depth;
  11251. {
  11252.   while (depth > 0) {
  11253.     putc ('.', fd);
  11254.     depth--;
  11255.   }
  11256. }
  11257.   
  11258.  
  11259. #ifdef VMS
  11260.  
  11261. /* Under VMS we need to fix up the "include" specification
  11262.    filename so that everything following the 1st slash is
  11263.    changed into its correct VMS file specification. */
  11264.  
  11265. static void
  11266. hack_vms_include_specification (fname)
  11267.      char *fname;
  11268. {
  11269.   register char *cp, *cp1, *cp2;
  11270.   int f, check_filename_before_returning, no_prefix_seen;
  11271.   char Local[512];
  11272.  
  11273.   check_filename_before_returning = 0;
  11274.   no_prefix_seen = 0;
  11275.  
  11276.   /* Ignore leading "./"s */
  11277.   while (fname[0] == '.' && fname[1] == '/') {
  11278.     strcpy (fname, fname+2);
  11279.     no_prefix_seen = 1;        /* mark this for later */
  11280.   }
  11281.   /* Look for the boundary between the VMS and UNIX filespecs */
  11282.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  11283.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  11284.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  11285.   if (cp) {
  11286.     cp++;
  11287.   } else {
  11288.     cp = index (fname, '/');    /* Look for the "/" */
  11289.   }
  11290.  
  11291.   /*
  11292.    * Check if we have a vax-c style '#include filename'
  11293.    * and add the missing .h
  11294.    */
  11295.   if (cp == 0) {
  11296.     if (index(fname,'.') == 0)
  11297.       strcat(fname, ".h");
  11298.   } else {
  11299.     if (index(cp,'.') == 0)
  11300.       strcat(cp, ".h");
  11301.   }
  11302.  
  11303.   cp2 = Local;            /* initialize */
  11304.  
  11305.   /* We are trying to do a number of things here.  First of all, we are
  11306.      trying to hammer the filenames into a standard format, such that later
  11307.      processing can handle them.
  11308.      
  11309.      If the file name contains something like [dir.], then it recognizes this
  11310.      as a root, and strips the ".]".  Later processing will add whatever is
  11311.      needed to get things working properly.
  11312.      
  11313.      If no device is specified, then the first directory name is taken to be
  11314.      a device name (or a rooted logical). */
  11315.  
  11316.   /* See if we found that 1st slash */
  11317.   if (cp == 0) return;        /* Nothing to do!!! */
  11318.   if (*cp != '/') return;    /* Nothing to do!!! */
  11319.   /* Point to the UNIX filename part (which needs to be fixed!) */
  11320.   cp1 = cp+1;
  11321.   /* If the directory spec is not rooted, we can just copy
  11322.      the UNIX filename part and we are done */
  11323.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  11324.     if (cp[-2] != '.') {
  11325.       /*
  11326.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  11327.        * We strip the `]', and then splice the two parts of the name in the
  11328.        * usual way.  Given the default locations for include files in cccp.c,
  11329.        * we will only use this code if the user specifies alternate locations
  11330.        * with the /include (-I) switch on the command line.  */
  11331.       cp -= 1;            /* Strip "]" */
  11332.       cp1--;            /* backspace */
  11333.     } else {
  11334.       /*
  11335.        * The VMS part has a ".]" at the end, and this will not do.  Later
  11336.        * processing will add a second directory spec, and this would be a syntax
  11337.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  11338.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  11339.        * generation of the 000000 root directory spec (which does not belong here
  11340.        * in this case).
  11341.        */
  11342.       cp -= 2;            /* Strip ".]" */
  11343.       cp1--; };            /* backspace */
  11344.   } else {
  11345.  
  11346.     /* We drop in here if there is no VMS style directory specification yet.
  11347.      * If there is no device specification either, we make the first dir a
  11348.      * device and try that.  If we do not do this, then we will be essentially
  11349.      * searching the users default directory (as if they did a #include "asdf.h").
  11350.      *
  11351.      * Then all we need to do is to push a '[' into the output string. Later
  11352.      * processing will fill this in, and close the bracket.
  11353.      */
  11354.     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  11355.     *cp2++ = '[';        /* Open the directory specification */
  11356.   }
  11357.  
  11358.   /* at this point we assume that we have the device spec, and (at least
  11359.      the opening "[" for a directory specification.  We may have directories
  11360.      specified already */
  11361.  
  11362.   /* If there are no other slashes then the filename will be
  11363.      in the "root" directory.  Otherwise, we need to add
  11364.      directory specifications. */
  11365.   if (index (cp1, '/') == 0) {
  11366.     /* Just add "000000]" as the directory string */
  11367.     strcpy (cp2, "000000]");
  11368.     cp2 += strlen (cp2);
  11369.     check_filename_before_returning = 1; /* we might need to fool with this later */
  11370.   } else {
  11371.     /* As long as there are still subdirectories to add, do them. */
  11372.     while (index (cp1, '/') != 0) {
  11373.       /* If this token is "." we can ignore it */
  11374.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  11375.     cp1 += 2;
  11376.     continue;
  11377.       }
  11378.       /* Add a subdirectory spec. Do not duplicate "." */
  11379.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  11380.     *cp2++ = '.';
  11381.       /* If this is ".." then the spec becomes "-" */
  11382.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  11383.     /* Add "-" and skip the ".." */
  11384.     *cp2++ = '-';
  11385.     cp1 += 3;
  11386.     continue;
  11387.       }
  11388.       /* Copy the subdirectory */
  11389.       while (*cp1 != '/') *cp2++= *cp1++;
  11390.       cp1++;            /* Skip the "/" */
  11391.     }
  11392.     /* Close the directory specification */
  11393.     if (cp2[-1] == '.')        /* no trailing periods */
  11394.       cp2--;
  11395.     *cp2++ = ']';
  11396.   }
  11397.   /* Now add the filename */
  11398.   while (*cp1) *cp2++ = *cp1++;
  11399.   *cp2 = 0;
  11400.   /* Now append it to the original VMS spec. */
  11401.   strcpy (cp, Local);
  11402.  
  11403.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  11404.      remove the [000000], and return that name.  This provides flexibility
  11405.      to the user in that they can use both rooted and non-rooted logical names
  11406.      to point to the location of the file.  */
  11407.  
  11408.   if (check_filename_before_returning && no_prefix_seen) {
  11409.     f = open (fname, O_RDONLY, 0666);
  11410.     if (f >= 0) {
  11411.       /* The file name is OK as it is, so return it as is.  */
  11412.       close (f);
  11413.       return;
  11414.     }
  11415.     /* The filename did not work.  Try to remove the [000000] from the name,
  11416.        and return it.  */
  11417.     cp = index (fname, '[');
  11418.     cp2 = index (fname, ']') + 1;
  11419.     strcpy (cp, cp2);        /* this gets rid of it */
  11420.   }
  11421.   return;
  11422. }
  11423. #endif    /* VMS */
  11424.  
  11425. #ifdef    VMS
  11426.  
  11427. /* These are the read/write replacement routines for
  11428.    VAX-11 "C".  They make read/write behave enough
  11429.    like their UNIX counterparts that CCCP will work */
  11430.  
  11431. static int
  11432. read (fd, buf, size)
  11433.      int fd;
  11434.      char *buf;
  11435.      int size;
  11436. {
  11437. #undef    read    /* Get back the REAL read routine */
  11438.   register int i;
  11439.   register int total = 0;
  11440.  
  11441.   /* Read until the buffer is exhausted */
  11442.   while (size > 0) {
  11443.     /* Limit each read to 32KB */
  11444.     i = (size > (32*1024)) ? (32*1024) : size;
  11445.     i = read (fd, buf, i);
  11446.     if (i <= 0) {
  11447.       if (i == 0) return (total);
  11448.       return (i);
  11449.     }
  11450.     /* Account for this read */
  11451.     total += i;
  11452.     buf += i;
  11453.     size -= i;
  11454.   }
  11455.   return (total);
  11456. }
  11457.  
  11458. static int
  11459. write (fd, buf, size)
  11460.      int fd;
  11461.      char *buf;
  11462.      int size;
  11463. {
  11464. #undef    write    /* Get back the REAL write routine */
  11465.   int i;
  11466.   int j;
  11467.  
  11468.   /* Limit individual writes to 32Kb */
  11469.   i = size;
  11470.   while (i > 0) {
  11471.     j = (i > (32*1024)) ? (32*1024) : i;
  11472.     if (write (fd, buf, j) < 0) return (-1);
  11473.     /* Account for the data written */
  11474.     buf += j;
  11475.     i -= j;
  11476.   }
  11477.   return (size);
  11478. }
  11479.  
  11480. /* The following wrapper functions supply additional arguments to the VMS
  11481.    I/O routines to optimize performance with file handling.  The arguments
  11482.    are:
  11483.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  11484.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  11485.      "fop=tef"- Truncate unused portions of file when closing file.
  11486.      "shr=nil"- Disallow file sharing while file is open.
  11487.  */
  11488.  
  11489. static FILE *
  11490. freopen (fname, type, oldfile)
  11491.      char *fname;
  11492.      char *type;
  11493.      FILE *oldfile;
  11494. {
  11495. #undef    freopen    /* Get back the REAL fopen routine */
  11496.   if (strcmp (type, "w") == 0)
  11497.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  11498.   return freopen (fname, type, oldfile, "mbc=16");
  11499. }
  11500.  
  11501. static FILE *
  11502. fopen (fname, type)
  11503.      char *fname;
  11504.      char *type;
  11505. {
  11506. #undef fopen    /* Get back the REAL fopen routine */
  11507.   if (strcmp (type, "w") == 0)
  11508.     return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  11509.   return fopen (fname, type, "mbc=16");
  11510. }
  11511.  
  11512. static int 
  11513. open (fname, flags, prot)
  11514.      char *fname;
  11515.      int flags;
  11516.      int prot;
  11517. {
  11518. #undef open    /* Get back the REAL open routine */
  11519.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  11520. }
  11521.  
  11522. /* Avoid run-time library bug, where copying M out of N+M characters with
  11523.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  11524.    gcc-cpp exercises this particular bug.  */
  11525.  
  11526. static char *
  11527. strncat (dst, src, cnt)
  11528.      char *dst;
  11529.      const char *src;
  11530.      unsigned cnt;
  11531. {
  11532.   register char *d = dst, *s = (char *) src;
  11533.   register int n = cnt;    /* convert to _signed_ type */
  11534.  
  11535.   while (*d) d++;    /* advance to end */
  11536.   while (--n >= 0)
  11537.     if (!(*d++ = *s++)) break;
  11538.   if (n < 0) *d = '\0';
  11539.   return dst;
  11540. }
  11541. #endif /* VMS */
  11542.  
  11543. #ifdef NEXT_SEMANTICS
  11544.  
  11545. struct negative_file {
  11546.   char *name;
  11547.   struct negative_file *next;
  11548. };
  11549. static struct negative_file *negative_hash_table[IMPORT_HASH_SIZE];
  11550.  
  11551. static void
  11552. add_to_negative_cache (fname)
  11553.      char *fname;
  11554. {
  11555.   struct negative_file *i;
  11556.   int hashval;
  11557.  
  11558.   hashval = import_hash (fname);
  11559.   i = (struct negative_file *)xmalloc (sizeof (struct negative_file));
  11560.   i->name = fname;
  11561.   i->next = negative_hash_table[hashval];
  11562.   negative_hash_table[hashval] = i;
  11563. }
  11564.  
  11565. static int
  11566. found_in_negative_cache (filename)
  11567.      char *filename;
  11568. {
  11569.   struct negative_file *i;
  11570.   int hashval;
  11571.  
  11572.   hashval = import_hash (filename);
  11573.  
  11574.   /* Attempt to find file in list of already included files */
  11575.   i = negative_hash_table[hashval];
  11576.  
  11577.   while (i) {
  11578.     if (!strcmp (filename, i->name))
  11579.       return 1;        /* return found */
  11580.     i = i->next;
  11581.   }
  11582.   return 0;
  11583. }
  11584.  
  11585. #if 0
  11586. void get_input_file_names(in_fname, in_fname_count, in_fname_list)
  11587.   char *in_fname;
  11588.   int *in_fname_count; /* output */
  11589.   struct file_name_list **in_fname_list; /* optional output */
  11590. {
  11591.   char *in_fnamep = in_fname;
  11592.   char c;
  11593.  
  11594.   *in_fname_count = 1;
  11595.   while (c = *in_fnamep++)
  11596.     if (c == ' ') (*in_fname_count)++; /* need the parens */
  11597.  
  11598.   if (*in_fname_count > 1) {
  11599.     int nelem = 0;
  11600.     *in_fname_list = (struct file_name_list *) xmalloc (*in_fname_count * 
  11601.                          sizeof (struct file_name_list));
  11602.     in_fnamep = in_fname;
  11603.     if ((*in_fname_list)[nelem++].fname = strtok(in_fnamep," "))
  11604.       while ((*in_fname_list)[nelem++].fname = strtok(NULL," "));
  11605.   } else {
  11606.     *in_fname_list = 0;
  11607.   }
  11608. }
  11609. #endif
  11610.  
  11611. static void
  11612. add_version_of_header(fileptr, start, length)
  11613.      struct file_name_list *fileptr;
  11614.      unsigned int start;
  11615.      unsigned int length;
  11616. {
  11617.    struct file_name_list *ptr;
  11618.  
  11619.    ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  11620.    bzero (ptr, sizeof (struct file_name_list));
  11621.  
  11622.    ptr->fname = fileptr->fname;
  11623.    ptr->versions = fileptr->versions;
  11624.    fileptr->versions = ptr;
  11625.    ptr->relative_position_of_file_start = start;
  11626.    ptr->file_length = length;
  11627. }
  11628.  
  11629. static int
  11630. duplicate_version_of_header(op, fileptr, position_of_file_start, 
  11631.                                      file_length)
  11632.      FILE_BUF *op;
  11633.      struct file_name_list *fileptr;
  11634.      U_CHAR *position_of_file_start;
  11635.      unsigned int file_length;
  11636. {
  11637.   int found_a_version = 0;
  11638.   do {
  11639.     if ((file_length == fileptr->file_length) &&
  11640.         (!bcmp (position_of_file_start, 
  11641.                 op->buf+fileptr->relative_position_of_file_start,
  11642.                 file_length))) {
  11643.       found_a_version = 1;
  11644.     } 
  11645.     fileptr = fileptr->versions;
  11646.   } while (fileptr && !found_a_version);
  11647.   return found_a_version;
  11648. }
  11649.  
  11650. /* This saves processing 497k for "appkit.h" (with the "include" heuristic)
  11651.    This results in a significant savings in user time (~40% I believe!) */
  11652.  
  11653. static int
  11654. aggressive_skip_if_group (ip)
  11655.      FILE_BUF *ip;
  11656. {
  11657.     if (ip->bufp == ip->file->position_after_last_include) {
  11658.  
  11659.       /* #if/#ifdef/#ifndef's are all identified by "T_IF" (see calls to
  11660.          conditional_skip in do_if/do_xifdef) */
  11661.       if (if_stack && if_stack->type == T_IF && if_stack->control_macro) {
  11662.         /* just because we are in the context of "some" #ifndef, check
  11663.            whether the input we are currently processing had one. If it did,
  11664.            make sure we skip to its matching #endif, else go to end of buf */
  11665.         if (ip->file->control_macro)
  11666.           ip->bufp = ip->file->position_of_last_endif;
  11667.         else
  11668.           ip->bufp = ip->buf + ip->length;
  11669.         enable_macros_from_header(ip->file);
  11670.         /* fprintf(stderr, "SKIP for %s: %.20s (saved %d)\n", 
  11671.          *        ip->fname, ip->bufp, total_saved += ip->bufp-sav);
  11672.          */
  11673.         return 1;
  11674.       } else {
  11675.         /* this means the last include is in the context of an arbitrary
  11676.            conditional (i.e. not the "ifndef idiom"). for now, we will just
  11677.            exclude it from this optimization. It is possible to include it, 
  11678.            but this will require more complexity */
  11679.         /* fprintf(stderr, "CAN'T SKIP for %s: %.20s\n", ip->fname, ip->bufp); */
  11680.         return 0;
  11681.       }
  11682.     } else if (!ip->file->position_after_last_include) { 
  11683.       /* no #includes, currently processing a "leaf" header */
  11684.       ip->bufp = ip->buf + ip->length;
  11685.       enable_macros_from_header(ip->file);
  11686.       /* fprintf(stderr, "NO INCLUDES for %s: %.20s (saved %d)\n", 
  11687.        *        ip->fname, ip->bufp, total_saved += ip->bufp-sav);
  11688.        */
  11689.       return 1;
  11690.     } else { /* this case is not currently optimized (obviously) */
  11691.       return 0;
  11692.     }
  11693. }
  11694.  
  11695. static void
  11696. dump_memory_statistics ()
  11697. {
  11698.   struct file_name_list *new;
  11699.   struct import_file *imp;
  11700.   struct negative_file *neg;
  11701.   int n_elements = 0, n_other = 0, size_bufs = 0, size_mlists = 0;
  11702.   int n_expansion_strings = 0, n_string_count = 0, control_names = 0;
  11703.   int bucket;
  11704.  
  11705.   fprintf(stderr, "all_input_files (chain of struct file_name_list's):\n");
  11706.   for (new = all_input_files; new; new = new->next) {
  11707.     n_elements++;
  11708.     n_string_count += (strlen(new->fname)+1);
  11709.     size_bufs += new->length;
  11710.   }
  11711.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11712.                    n_elements * sizeof(struct file_name_list),
  11713.                    n_elements, sizeof(struct file_name_list));
  11714.   fprintf(stderr, "%10d bytes for input file names\n", n_string_count);
  11715.   fprintf(stderr, "%10d bytes for input buffers\n", size_bufs);
  11716.  
  11717.   n_string_count = n_elements = size_bufs = 0;
  11718.  
  11719.   fprintf(stderr, "all_output_files (chain of struct file_name_list's):\n");
  11720.   for (new = all_output_files; new; new = new->next) {
  11721.     n_elements++;
  11722.     if (new->fname)
  11723.       n_string_count += (strlen(new->fname)+1);
  11724.     size_bufs += new->length;
  11725.   }
  11726.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11727.                    n_elements * sizeof(struct file_name_list),
  11728.                    n_elements, sizeof(struct file_name_list));
  11729.   fprintf(stderr, "%10d bytes for output file names\n", n_string_count);
  11730.   fprintf(stderr, "%10d bytes for output buffers\n", size_bufs);
  11731.  
  11732.   n_string_count = n_elements = size_bufs = 0;
  11733.   fprintf(stderr, "all_include_files (chain of struct file_name_list's):\n");
  11734.   for (new = all_include_files; new; new = new->next) {
  11735.     n_elements++;
  11736.     n_string_count += (strlen(new->fname)+1);
  11737.     if (new->control_macro)
  11738.       control_names += (strlen(new->control_macro)+1);
  11739.     size_bufs += new->length;
  11740.     size_mlists += new->macro_count * sizeof ( HASHNODE * );
  11741.     fprintf(stderr, "%s\n", new->fname);
  11742.   }
  11743.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11744.                    n_elements * sizeof(struct file_name_list),
  11745.                    n_elements, sizeof(struct file_name_list));
  11746.   fprintf(stderr, "%10d bytes for file names\n", n_string_count);
  11747.   fprintf(stderr, "%10d bytes for control macro names\n", control_names);
  11748.   fprintf(stderr, "%10d bytes for input buffers\n", size_bufs);
  11749.   fprintf(stderr, "%10d bytes for macro lists\n", size_mlists);
  11750.  
  11751.   n_string_count = n_elements = 0;
  11752.   fprintf(stderr, "include directories (chain of struct file_name_list's):\n");
  11753.   for (new = include; new; new = new->next) {
  11754.     n_elements++;
  11755.     n_string_count += (strlen(new->fname)+1);
  11756.   }
  11757.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11758.                    n_elements * sizeof(struct file_name_list),
  11759.                    n_elements, sizeof(struct file_name_list));
  11760.   fprintf(stderr, "%10d bytes for directory names\n", n_string_count);
  11761.  
  11762.   n_elements = 0;
  11763.   fprintf(stderr, "import_hash_table (struct import_file's):\n");
  11764.   for (bucket = 0; bucket < IMPORT_HASH_SIZE; bucket++) {
  11765.     imp = import_hash_table[bucket];
  11766.     while (imp) {
  11767.       n_elements++;
  11768.       imp = imp->next;
  11769.     }
  11770.   }
  11771.   fprintf(stderr, "%10d bytes for import hash table buckets (statically allocated)\n",
  11772.            sizeof(import_hash_table));
  11773.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11774.                    n_elements * sizeof(struct import_file),
  11775.                    n_elements, sizeof(struct import_file));
  11776.  
  11777.   n_elements = 0;
  11778.   n_string_count = 0;
  11779.   fprintf(stderr, "negative_hash_table (struct negative_file's):\n");
  11780.   for (bucket = 0; bucket < IMPORT_HASH_SIZE; bucket++) {
  11781.     neg = negative_hash_table[bucket];
  11782.     while (neg) {
  11783.       n_elements++;
  11784.       n_string_count += (strlen(neg->name)+1);
  11785.       neg = neg->next;
  11786.     }
  11787.   }
  11788.   fprintf(stderr, "%10d bytes for negative hash table buckets (statically allocated)\n",
  11789.            sizeof(negative_hash_table));
  11790.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11791.                    n_elements * sizeof(struct negative_file),
  11792.                    n_elements, sizeof(struct negative_file));
  11793.   fprintf(stderr, "%10d bytes for file names\n", n_string_count);
  11794.  
  11795.   fprintf(stderr, "hashtab for macro's (struct hashnode's/definition's):\n");
  11796.   n_string_count = 0;
  11797.   n_elements = 0;
  11798.   n_other = 0;
  11799.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  11800.     register HASHNODE *hp;
  11801.  
  11802.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  11803.       if (hp->type == T_MACRO) {
  11804.         n_elements++;
  11805.         if (hp->value.defn->length) {
  11806.           n_string_count++;
  11807.           n_expansion_strings += hp->value.defn->length;
  11808.         }
  11809.       } else {
  11810.         n_other++;
  11811.       }
  11812.     }
  11813.   }
  11814.   fprintf(stderr, "%10d bytes for macro hash table buckets (statically allocated)\n",
  11815.            sizeof(hashtab));
  11816.   fprintf(stderr, "%10d bytes for %d macro hashnode's (%d bytes each)\n",
  11817.                    n_elements * sizeof(struct hashnode),
  11818.                    n_elements, sizeof(struct hashnode));
  11819.   fprintf(stderr, "%10d bytes for %d macro definitions's (%d bytes each)\n",
  11820.                    n_elements * sizeof(struct definition),
  11821.                    n_elements, sizeof(struct definition));
  11822.   fprintf(stderr, "%10d bytes for %d macro expansion strings\n",
  11823.                    n_expansion_strings, n_string_count);
  11824.   fprintf(stderr, "%10d bytes for %d other hashnode's (%d bytes each)\n",
  11825.                    n_other * sizeof(struct hashnode),
  11826.                    n_other, sizeof(struct hashnode));
  11827.   return;
  11828. }
  11829.  
  11830. static void
  11831. enable_macros_from_header(entry)
  11832.    struct file_name_list *entry;
  11833. {
  11834.    int i;
  11835.  
  11836.    if (entry->macro_list) {
  11837.       /* list may not yet be filled in */
  11838.       for (i = 0; i < entry->macro_count; i++) {
  11839.          entry->macro_list[i]->defined_within_module = 1;
  11840.       }
  11841.    }
  11842. }
  11843.  
  11844. static void
  11845. clear_import()
  11846. {
  11847.   struct import_file *i;
  11848.   int h;
  11849.  
  11850.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  11851.     i = import_hash_table[h];
  11852.     while (i) {
  11853.       i->visited_within_module = 0;
  11854.       /* dump_file_name_list(i->name); */
  11855.       i = i->next;
  11856.     }
  11857.   }
  11858. }
  11859.  
  11860. static void
  11861. really_delete_macro(hp)
  11862. HASHNODE * hp;
  11863. {
  11864.    DEFINITION *d = hp->value.defn;
  11865.    struct reflist *ap, *nextap;
  11866.  
  11867.    for (ap = d->pattern; ap != NULL; ap = nextap) {
  11868.       nextap = ap->next;
  11869.       free (ap);
  11870.    }
  11871.    if ((d->nargs > 0) && d->args.argnames)
  11872.       free (d->args.argnames);
  11873.  
  11874.    free (d);
  11875.    free (hp);
  11876. }
  11877.  
  11878. static void
  11879. clear_all_macros ()
  11880. {
  11881.   int bucket;
  11882.   register HASHNODE *hp, *hp_next;
  11883.  
  11884.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  11885.  
  11886.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  11887.        if (hp->type == T_MACRO) {
  11888.         /* possible bug...are we disabling command line stuff? Probably not. */
  11889.     hp->defined_within_module = 0;
  11890.         if (hp->owner && hp->owner->macro_count) {
  11891.           if (hp->owner->macro_index < hp->owner->macro_count) { 
  11892.             /* not done filling in the list */
  11893.             if (!hp->owner->macro_list) {
  11894.               int nbytes = hp->owner->macro_count * sizeof ( HASHNODE * );
  11895.               hp->owner->macro_list = (HASHNODE **) xmalloc ( nbytes );
  11896.               bzero(hp->owner->macro_list, nbytes);
  11897.             } 
  11898.             /* stash in the next empty slot */
  11899.             hp->owner->macro_list[hp->owner->macro_index++] = hp;
  11900.           } 
  11901.           /* else, header has been filled (or it has no macros) */
  11902.         } else { 
  11903.           /* defined on the command line (not a header) */
  11904.         }
  11905.       }
  11906.     }
  11907.   }
  11908.  
  11909.   /* free macros from delete_macro (usually due to #undef) */
  11910.   for (hp = free_undefined_macros; hp; hp = hp_next) {
  11911.      hp_next = hp->next;
  11912.      really_delete_macro (hp);
  11913.   }
  11914. #if 0
  11915.   /* free macros from delete_macro (usually due to #undef) */
  11916.   for (hp = installed_builtins; hp; hp = hp_next) {
  11917.      hp_next = hp->next;
  11918.      really_delete_macro (hp);
  11919.   }
  11920. #endif  
  11921. }
  11922.  
  11923. /* Pre-C-Preprocessor to translate RTF (Rich Text Format) Level 0
  11924.    to plain ASCII before normal preprocessing.  This removes all font, color,
  11925.    and help links from the text, but leaves the actual contents unchanged.
  11926.  
  11927.    Using an extra pass through the buffer takes a little extra time,
  11928.    but Rich Source Code is rare and a separate, shared routine
  11929.    can be used.   */
  11930.  
  11931. #define RTF_HEADER     "{\\rtf0\\ansi"
  11932. #define RTF_HEADER_LEN 11
  11933.  
  11934. static void
  11935. buf_convert_rtf (buf)
  11936.      FILE_BUF *buf;
  11937. {
  11938.   int asciiTextLen;
  11939.  
  11940.   if (buf->buf 
  11941.       && buf->length > RTF_HEADER_LEN 
  11942.       && !strncmp (buf->buf, RTF_HEADER, RTF_HEADER_LEN)) 
  11943.     {
  11944.       const char * asciiText = rtf_to_ascii(buf->buf, buf->length, &asciiTextLen);
  11945.       if (asciiText) 
  11946.     {
  11947.       /* FIXME: deallocate buf->buf */
  11948.       buf->buf = buf->bufp = (char *) asciiText;
  11949.       buf->length = asciiTextLen;
  11950.     }
  11951.     }
  11952. }
  11953.  
  11954.  
  11955. /* Process rtf text. RTFTEXTLEN bytes are processed from
  11956.    RTFTEXTPTR.  The output is ASCIITEXTLEN bytes in the same buffer. */
  11957.  
  11958. #define ASCII_TERMINATOR '\0' 
  11959.  
  11960. static const char *
  11961. rtf_to_ascii (const char *rtfTextPtr, int rtfTextLen, int * asciiTextLen) 
  11962. {
  11963.   char * asciiTextBuf = (char *) xmalloc(rtfTextLen);
  11964.   char * asciiTextPtr = asciiTextBuf;
  11965.   const char *endRtfPtr = rtfTextPtr + rtfTextLen;
  11966.  
  11967.   if (!asciiTextBuf)
  11968.     return NULL;
  11969.   
  11970.   while ( rtfTextPtr != endRtfPtr ) {
  11971.     switch( *rtfTextPtr ) {
  11972.     case '\n':
  11973.       rtfTextPtr++;
  11974.       break;
  11975.     case '}':
  11976.       if ( *++rtfTextPtr == '\n' ) {
  11977.     rtfTextPtr++;
  11978.     if (*rtfTextPtr == '‹') rtfTextPtr++;
  11979.       }         
  11980.       break;
  11981.     case '{':
  11982.       if ( !strncmp( rtfTextPtr, "{\\stylesheet", 12 ) 
  11983.       || !strncmp( rtfTextPtr, "{\\fonttbl", 9 ) 
  11984.       || !strncmp( rtfTextPtr, "{\\colortbl", 10 ) 
  11985.       || !strncmp( rtfTextPtr, "{\\NeXTHelp", 10 )
  11986.       || !strncmp( rtfTextPtr, "{{\\NeXTHelp", 11 ) ) {
  11987.     int depth = 1;
  11988.     while ( depth > 0 ) {
  11989.       switch ( *++rtfTextPtr ) {
  11990.       case '{': depth++; break;
  11991.       case '}': depth--; break;
  11992.       }
  11993.     }
  11994.       }
  11995.       else
  11996.     rtfTextPtr++;
  11997.       break;
  11998.     case '\\':
  11999.       rtfTextPtr++;
  12000.       if (*rtfTextPtr == '{'  
  12001.       || *rtfTextPtr == '}'  
  12002.       ||  *rtfTextPtr == '\\'  
  12003.       ||  *rtfTextPtr == '\n' ) {
  12004.     *asciiTextPtr++ = *rtfTextPtr++;
  12005.       } else if ( *rtfTextPtr == '\'' ) {
  12006.     int v1, v2, value;
  12007.     rtfTextPtr++;
  12008.     v1 = isdigit( *rtfTextPtr ) ? (*rtfTextPtr - '0') 
  12009.       : ((*rtfTextPtr - 'a') + 10);
  12010.     rtfTextPtr++;
  12011.     v2 = isdigit( *rtfTextPtr ) ? (*rtfTextPtr - '0') 
  12012.       : ((*rtfTextPtr - 'a') + 10);
  12013.     value = (v1 << 4) | (v2);
  12014.     *asciiTextPtr++ = value;
  12015.     rtfTextPtr++;
  12016.       } else {
  12017.     while ( isalpha( *rtfTextPtr ) ) rtfTextPtr++;
  12018.     if (*rtfTextPtr == '-') rtfTextPtr++;
  12019.     while ( isdigit( *rtfTextPtr ) ) rtfTextPtr++;
  12020.     if ( *rtfTextPtr == ' ' ) rtfTextPtr++;
  12021.     else while (*rtfTextPtr == ' ') rtfTextPtr++;
  12022.       }
  12023.       break;
  12024.     default:
  12025.       *asciiTextPtr++ = *rtfTextPtr++;
  12026.     }       
  12027.   }
  12028.   *asciiTextPtr = ASCII_TERMINATOR;
  12029.   *asciiTextLen = (int)asciiTextPtr - (int)asciiTextBuf;
  12030.   return asciiTextBuf;
  12031. }
  12032.  
  12033.  
  12034. /* Returns last known modifiy time for FILENAME */
  12035.  
  12036. time_t file_modtime(const char * filename) {
  12037.   int hashval = import_hash (filename);
  12038.  
  12039.   /* Attempt to find file in list of already included files */
  12040.   struct import_file *i = import_hash_table[hashval];
  12041.  
  12042.   while (i) {
  12043.      if (!strcmp (filename, i->name->fname)) {
  12044.         return i->modtime;
  12045.      }
  12046.      i = i->next;
  12047.   }
  12048.   return 0;
  12049. }
  12050.  
  12051. static void handle_file_modification(const char * filename) {
  12052.   struct file_name_list *ptr, *next_ptr, *prev_ptr, *version, *next_version;
  12053.   int hashval = import_hash (filename);
  12054.   int multiple_versions = 0, removed_from_hash = 0, removed_from_cache = 0;
  12055.   struct import_file *prev = NULL;
  12056.   struct import_file *i = import_hash_table[hashval];
  12057.   while (i) {
  12058.      if (!strcmp (filename, i->name->fname)) {
  12059.         if (prev)
  12060.            prev->next = i->next;
  12061.         else
  12062.            import_hash_table[hashval] = i->next;
  12063.         removed_from_hash = 1;
  12064.      }
  12065.      prev = i;
  12066.      i = i->next;
  12067.   }
  12068.   if (!removed_from_hash) {
  12069.      const char * basefilename = strrchr(filename,'/');
  12070.      if (basefilename) {
  12071.         /*  Look for other files by the same name */
  12072.         for (hashval = 0; hashval < IMPORT_HASH_SIZE; hashval++) {
  12073.        prev = NULL;
  12074.            i = import_hash_table[hashval];
  12075.            while (i) {
  12076.               const char * baseiname = strrchr(i->name->fname,'/');
  12077.               if (!strcmp (basefilename, baseiname)) {
  12078.                  if (prev)
  12079.                     prev->next = i->next;
  12080.                  else
  12081.                     import_hash_table[hashval] = i->next;
  12082.                  fprintf(stderr,"...punted %s\n",i->name->fname);
  12083.               }
  12084.               prev = i;
  12085.               i = i->next;
  12086.            }
  12087.         }
  12088.      } else {
  12089.         fprintf(stderr,"Cpp Internal Error: could not remove file '%s' from import hash table.  Looks like a bad file name!!\n", filename);
  12090.      }
  12091.   }
  12092.  
  12093.  
  12094.   /* This looks through the entire header cache for copies of this header.
  12095.      It's probably more efficient to terminate the search after finding the
  12096.      header being modified, but the conservative approach is more likely
  12097.      to be correct. */
  12098.   
  12099.   prev_ptr = NULL;
  12100.   for (ptr = next_ptr = all_include_files; ptr; ptr = next_ptr) {
  12101.      next_ptr = ptr->next;
  12102.      if (ptr->fname && !strcmp (ptr->fname, filename)) {
  12103.         if (ptr->versions) {
  12104.            multiple_versions = 1;
  12105.            for (version = next_version = ptr->versions;
  12106.                 version;
  12107.                 version = next_version) {
  12108.               next_version = version->next;
  12109.               free(version);
  12110.            }
  12111.         }
  12112.         if (prev_ptr)
  12113.            prev_ptr->next = next_ptr;
  12114.         else
  12115.            all_include_files = next_ptr;
  12116.         free(ptr->fname);
  12117.         free(ptr);
  12118.         removed_from_cache = 1;
  12119.      }
  12120.      prev_ptr = ptr;
  12121.   }
  12122.  
  12123. #ifdef DEBUG
  12124.   if (!removed_from_cache)
  12125.      fprintf(stderr,"Warning: could not remove file '%s' from import cache.\n", filename);
  12126. #endif
  12127.   
  12128.   if (multiple_versions)
  12129.      fprintf(stderr,"Removed all versions of %s from header cache\n", filename);
  12130.   else
  12131.      fprintf(stderr,"Removed %s from header cache\n", filename);
  12132.   fflush(stderr);
  12133. }
  12134.  
  12135. static void init_output_file_buf(FILE_BUF * outbuf)
  12136. {
  12137.   outbuf->buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  12138.   outbuf->length = OUTBUF_SIZE;
  12139.   outbuf->bufp = outbuf->output_written_to = outbuf->buf;
  12140.   bzero (outbuf->bufp, outbuf->length);
  12141.   outbuf->lineno = 0;
  12142. }
  12143.  
  12144.  
  12145. static int sysUsr_startTime;
  12146. static int sysUsr_finishTime;
  12147.   
  12148. static struct timeval real_startTime;
  12149. static struct timeval real_finishTime;
  12150.  
  12151.  
  12152.   /* Return time used so far, in microseconds.  */
  12153. static int gettime ()
  12154. {
  12155. #if defined(hpux)
  12156.    struct tms tms;
  12157.    times (&tms);
  12158.    return tms.tms_utime / CLK_TCK;
  12159. #else
  12160.    struct rusage rusage;
  12161.    getrusage (0, &rusage);
  12162.    return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  12163.         + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  12164. #endif
  12165. }
  12166.  
  12167. static void start() 
  12168. {
  12169.     sysUsr_startTime = gettime();
  12170.     gettimeofday(&real_startTime, 0);
  12171. }
  12172.  
  12173. static void stop() 
  12174. {
  12175.     sysUsr_finishTime = gettime();
  12176.     gettimeofday(&real_finishTime, 0);
  12177. }
  12178.  
  12179. static void display(FILE * strm, int addNewLine)
  12180. {
  12181.     real_finishTime.tv_sec -= real_startTime.tv_sec;
  12182.     real_finishTime.tv_usec -= real_startTime.tv_usec;
  12183.     if (real_finishTime.tv_usec < 0)
  12184.       real_finishTime.tv_sec--, real_finishTime.tv_usec += 1000000;
  12185.       
  12186.     fprintf(strm, "%3d.%03d (sys+usr) %3d.%01d (real)%s",
  12187.          (sysUsr_finishTime-sysUsr_startTime) / 1000000, 
  12188.          (sysUsr_finishTime-sysUsr_startTime) % 1000000, 
  12189.          real_finishTime.tv_sec, 
  12190.          real_finishTime.tv_usec/100000,
  12191.          addNewLine ? "\n" : "");
  12192. }
  12193. #endif
  12194.