home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / cccp.c < prev    next >
Text File  |  1996-12-21  |  319KB  |  11,620 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2.    Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 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, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.
  20.  
  21.  In other words, you are welcome to use, share and improve this program.
  22.  You are forbidden to forbid anyone else to use, share and improve
  23.  what you give them.   Help stamp out software-hoarding!  */
  24.  
  25. #ifdef _WIN32
  26. #include "cccp-winnt.c"
  27. #elif defined (NeXT_PDO)
  28. #include "cccp-pdo.c"
  29. #else
  30. #include "cccp-mach.c"
  31. #endif
  32.  
  33. #if 0
  34.  
  35. #ifdef NeXT_PDO
  36. #define HAVE_VPRINTF
  37. #define MAX_FILENAME_LEN 256
  38. #define NEXT_FRAMEWORKS
  39. #define NEXT_SEMANTICS
  40. #define NEXT_OBJC_RUNTIME
  41. #endif
  42.  
  43. typedef unsigned char U_CHAR;
  44.  
  45. #ifdef EMACS
  46. #define NO_SHORTNAMES
  47. #include "../src/config.h"
  48. #ifdef open
  49. #undef open
  50. #undef read
  51. #undef write
  52. #endif /* open */
  53. #endif /* EMACS */
  54.  
  55. /* The macro EMACS is defined when cpp is distributed as part of Emacs,
  56.    for the sake of machines with limited C compilers.  */
  57. #ifndef EMACS
  58. #include "config.h"
  59. #endif /* not EMACS */
  60.  
  61. #ifndef STANDARD_INCLUDE_DIR
  62. #define STANDARD_INCLUDE_DIR "/usr/include"
  63. #endif
  64.  
  65. #ifndef LOCAL_INCLUDE_DIR
  66. #define LOCAL_INCLUDE_DIR "/usr/local/include"
  67. #endif
  68.  
  69. #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
  70. #ifdef __STDC__
  71. #define PTR_INT_TYPE ptrdiff_t
  72. #else
  73. #define PTR_INT_TYPE long
  74. #endif
  75. #endif /* 0 */
  76.  
  77. #include "pcp.h"
  78.  
  79. /* By default, colon separates directories in a path.  */
  80. #ifndef PATH_SEPARATOR
  81. #define PATH_SEPARATOR ':'
  82. #endif
  83.  
  84. #include <sys/types.h>
  85. #include <sys/stat.h>
  86. #include <ctype.h>
  87. #include <stdio.h>
  88. #include <signal.h>
  89.  
  90. /* The following symbols should be autoconfigured:
  91.     HAVE_FCNTL_H
  92.     HAVE_STDLIB_H
  93.     HAVE_SYS_TIME_H
  94.     HAVE_UNISTD_H
  95.     STDC_HEADERS
  96.     TIME_WITH_SYS_TIME
  97.    In the mean time, we'll get by with approximations based
  98.    on existing GCC configuration symbols.  */
  99.  
  100. #ifdef POSIX
  101. # ifndef HAVE_STDLIB_H
  102. # define HAVE_STDLIB_H 1
  103. # endif
  104. # ifndef HAVE_UNISTD_H
  105. # define HAVE_UNISTD_H 1
  106. # endif
  107. # ifndef STDC_HEADERS
  108. # define STDC_HEADERS 1
  109. # endif
  110. #endif /* defined (POSIX) */
  111.  
  112. #if defined (POSIX) || (defined (USG) && !defined (VMS))
  113. # ifndef HAVE_FCNTL_H
  114. # define HAVE_FCNTL_H 1
  115. # endif
  116. #endif
  117.  
  118. #ifndef RLIMIT_STACK
  119. # include <time.h>
  120. #else
  121. # if TIME_WITH_SYS_TIME
  122. #  include <sys/time.h>
  123. #  include <time.h>
  124. # else
  125. #  if HAVE_SYS_TIME_H
  126. #   include <sys/time.h>
  127. #  else
  128. #   include <time.h>
  129. #  endif
  130. # endif
  131. # include <sys/resource.h>
  132. #endif
  133.  
  134. #if HAVE_FCNTL_H
  135. # include <fcntl.h>
  136. #endif
  137.  
  138. /* This defines "errno" properly for VMS, and gives us EACCES. */
  139. #include <errno.h>
  140.  
  141. #if HAVE_STDLIB_H
  142. # include <stdlib.h>
  143. #else
  144. char *getenv ();
  145. #endif
  146.  
  147. #if STDC_HEADERS
  148. # include <string.h>
  149. # ifndef bcmp
  150. # define bcmp(a, b, n) memcmp (a, b, n)
  151. # endif
  152. # ifndef bcopy
  153. # define bcopy(s, d, n) memcpy (d, s, n)
  154. # endif
  155. # ifndef bzero
  156. # define bzero(d, n) memset (d, 0, n)
  157. # endif
  158. #else /* !STDC_HEADERS */
  159. char *index ();
  160. char *rindex ();
  161.  
  162. # if !defined (BSTRING) && (defined (USG) || defined (VMS))
  163.  
  164. #  ifndef bcmp
  165. #  define bcmp my_bcmp
  166. static int
  167. my_bcmp (a, b, n)
  168.      register char *a;
  169.      register char *b;
  170.      register unsigned n;
  171. {
  172.    while (n-- > 0)
  173.      if (*a++ != *b++)
  174.        return 1;
  175.  
  176.    return 0;
  177. }
  178. #  endif /* !defined (bcmp) */
  179.  
  180. #  ifndef bcopy
  181. #  define bcopy my_bcopy
  182. static void
  183. my_bcopy (s, d, n)
  184.      register char *s;
  185.      register char *d;
  186.      register unsigned n;
  187. {
  188.   while (n-- > 0)
  189.     *d++ = *s++;
  190. }
  191. #  endif /* !defined (bcopy) */
  192.  
  193. #  ifndef bzero
  194. #  define bzero my_bzero
  195. static void
  196. my_bzero (b, length)
  197.      register char *b;
  198.      register unsigned length;
  199. {
  200.   while (length-- > 0)
  201.     *b++ = 0;
  202. }
  203. #  endif /* !defined (bzero) */
  204.  
  205. # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */
  206. #endif /* ! STDC_HEADERS */
  207.  
  208. #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6)
  209. # define __attribute__(x)
  210. #endif
  211.  
  212. #ifndef PROTO
  213. # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  214. #  define PROTO(ARGS) ARGS
  215. # else
  216. #  define PROTO(ARGS) ()
  217. # endif
  218. #endif
  219.  
  220. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  221. # include <stdarg.h>
  222. # define VA_START(va_list, var) va_start (va_list, var)
  223. # define PRINTF_ALIST(msg) char *msg, ...
  224. # define PRINTF_DCL(msg)
  225. # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (printf, m, n)))
  226. #else
  227. # include <varargs.h>
  228. # define VA_START(va_list, var) va_start (va_list)
  229. # define PRINTF_ALIST(msg) msg, va_alist
  230. # define PRINTF_DCL(msg) char *msg; va_dcl
  231. # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (printf, m, n)))
  232. # define vfprintf(file, msg, args) \
  233.     { \
  234.       char *a0 = va_arg(args, char *); \
  235.       char *a1 = va_arg(args, char *); \
  236.       char *a2 = va_arg(args, char *); \
  237.       char *a3 = va_arg(args, char *); \
  238.       fprintf (file, msg, a0, a1, a2, a3); \
  239.     }
  240. #endif
  241.  
  242. #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
  243. #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
  244. #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
  245.  
  246. #if HAVE_UNISTD_H
  247. # include <unistd.h>
  248. #endif
  249.  
  250. /* VMS-specific definitions */
  251. #ifdef VMS
  252. #include <descrip.h>
  253. #define O_RDONLY    0    /* Open arg for Read/Only  */
  254. #define O_WRONLY    1    /* Open arg for Write/Only */
  255. #define read(fd,buf,size)    VMS_read (fd,buf,size)
  256. #define write(fd,buf,size)    VMS_write (fd,buf,size)
  257. #define open(fname,mode,prot)    VMS_open (fname,mode,prot)
  258. #define fopen(fname,mode)    VMS_fopen (fname,mode)
  259. #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
  260. #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
  261. #define fstat(fd,stbuf)        VMS_fstat (fd,stbuf)
  262. static int VMS_fstat (), VMS_stat ();
  263. static char * VMS_strncat ();
  264. static int VMS_read ();
  265. static int VMS_write ();
  266. static int VMS_open ();
  267. static FILE * VMS_fopen ();
  268. static FILE * VMS_freopen ();
  269. static void hack_vms_include_specification ();
  270. typedef struct { unsigned :16, :16, :16; } vms_ino_t;
  271. #define ino_t vms_ino_t
  272. #define INCLUDE_LEN_FUDGE 10    /* leave room for VMS syntax conversion */
  273. #ifdef __GNUC__
  274. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  275. #endif /* __GNUC__ */
  276. #endif /* VMS */
  277.  
  278. #ifndef O_RDONLY
  279. #define O_RDONLY 0
  280. #endif
  281.  
  282. #undef MIN
  283. #undef MAX
  284. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  285. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  286.  
  287. /* Find the largest host integer type and set its size and type.  */
  288.  
  289. #ifndef HOST_BITS_PER_WIDE_INT
  290.  
  291. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  292. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  293. #define HOST_WIDE_INT long
  294. #else
  295. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  296. #define HOST_WIDE_INT int
  297. #endif
  298.  
  299. #endif
  300.  
  301. #ifndef S_ISREG
  302. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  303. #endif
  304.  
  305. #ifndef S_ISDIR
  306. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  307. #endif
  308.  
  309. /* Define a generic NULL if one hasn't already been defined.  */
  310.  
  311. #ifndef NULL
  312. #define NULL 0
  313. #endif
  314.  
  315. #ifndef GENERIC_PTR
  316. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  317. #define GENERIC_PTR void *
  318. #else
  319. #define GENERIC_PTR char *
  320. #endif
  321. #endif
  322.  
  323. #ifndef NULL_PTR
  324. #define NULL_PTR ((GENERIC_PTR)0)
  325. #endif
  326.  
  327. #ifndef INCLUDE_LEN_FUDGE
  328. #define INCLUDE_LEN_FUDGE 0
  329. #endif
  330.  
  331. #ifdef WINNT
  332. #define fstat _fstat
  333. #define fdopen _fdopen
  334. #define fileno _fileno
  335. #import <stdlib.h>
  336. #import <windows.h>
  337. #endif /* WINNT */
  338.  
  339. #ifdef REPORT_EVENT
  340. #include "next/make-support.h"
  341. #endif
  342.  
  343. /* External declarations.  */
  344.  
  345. extern char *version_string;
  346. #ifndef VMS
  347. #ifndef HAVE_STRERROR
  348. extern int sys_nerr;
  349. #if defined(bsd4_4)
  350. extern const char *const sys_errlist[];
  351. #else
  352. #ifndef WINNT
  353. extern char *sys_errlist[];
  354. #endif /* WINNT */
  355. #endif
  356. #else    /* HAVE_STRERROR */
  357. char *strerror ();
  358. #endif
  359. #else    /* VMS */
  360. char *strerror (int,...);
  361. #endif
  362. int parse_escape PROTO((char **));
  363. HOST_WIDE_INT parse_c_expression PROTO((char *));
  364.  
  365. #ifndef errno
  366. extern int errno;
  367. #endif
  368.  
  369. /* Name under which this program was invoked.  */
  370.  
  371. static char *progname;
  372.  
  373. /* Nonzero means use extra default include directories for C++.  */
  374.  
  375. static int cplusplus;
  376.  
  377. /* Nonzero means handle cplusplus style comments */
  378.  
  379. static int cplusplus_comments;
  380.  
  381. /* Nonzero means handle #import, for objective C.  */
  382.  
  383. static int objc;
  384.  
  385. /* Nonzero means this is an assembly file, and allow
  386.    unknown directives, which could be comments.  */
  387.  
  388. static int lang_asm;
  389.  
  390. /* Current maximum length of directory names in the search path
  391.    for include files.  (Altered as we get more of them.)  */
  392.  
  393. static int max_include_len;
  394.  
  395. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  396.  
  397. static int for_lint = 0;
  398.  
  399. /* Nonzero means copy comments into the output file.  */
  400.  
  401. static int put_out_comments = 0;
  402.  
  403. /* Nonzero means don't process the ANSI trigraph sequences.  */
  404.  
  405. static int no_trigraphs = 0;
  406.  
  407. /* Nonzero means print the names of included files rather than
  408.    the preprocessed output.  1 means just the #include "...",
  409.    2 means #include <...> as well.  */
  410.  
  411. static int print_deps = 0;
  412.  
  413. /* Nonzero if missing .h files in -M output are assumed to be generated
  414.    files and not errors.  */
  415.  
  416. static int print_deps_missing_files = 0;
  417.  
  418. /* Nonzero means print names of header files (-H).  */
  419.  
  420. static int print_include_names = 0;
  421.  
  422. /* Nonzero means don't output line number information.  */
  423.  
  424. static int no_line_directives;
  425.  
  426. /* Nonzero means output the text in failing conditionals,
  427.    inside #failed ... #endfailed.  */
  428.  
  429. static int output_conditionals;
  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. #if defined (NEXT_SEMANTICS) || defined(NEXT_PDO)
  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 directive.  */
  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.   int lineno;
  555.   int length;
  556.   U_CHAR *buf;
  557.   U_CHAR *bufp;
  558.   /* Macro that this level is the expansion of.
  559.      Included so that we can reenable the macro
  560.      at the end of this level.  */
  561.   struct hashnode *macro;
  562.   /* Value of if_stack at start of this file.
  563.      Used to prohibit unmatched #endif (etc) in an include file.  */
  564.   struct if_stack *if_stack;
  565.   /* Object to be freed at end of input at this level.  */
  566.   U_CHAR *free_ptr;
  567.   /* True if this is a header file included using <FILENAME>.  */
  568.   char system_header_p;
  569. } instack[INPUT_STACK_MAX];
  570.  
  571. static int last_error_tick;       /* Incremented each time we print it.  */
  572. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  573.  
  574. /* Current nesting level of input sources.
  575.    `instack[indepth]' is the level currently being read.  */
  576. static int indepth = -1;
  577. #define CHECK_DEPTH(code) \
  578.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  579.     {                                    \
  580.       error_with_line (line_for_error (instack[indepth].lineno),    \
  581.                "macro or `#include' recursion too deep");    \
  582.       code;                                \
  583.     }
  584.  
  585. /* Current depth in #include directives that use <...>.  */
  586. static int system_include_depth = 0;
  587.  
  588. typedef struct file_buf FILE_BUF;
  589.  
  590. /* The output buffer.  Its LENGTH field is the amount of room allocated
  591.    for the buffer, not the number of chars actually present.  To get
  592.    that, subtract outbuf.buf from outbuf.bufp. */
  593.  
  594. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  595. static FILE_BUF outbuf;
  596.  
  597. /* Grow output buffer OBUF points at
  598.    so it can hold at least NEEDED more chars.  */
  599.  
  600. #define check_expand(OBUF, NEEDED)  \
  601.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  602.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  603.  
  604. struct file_name_list
  605.   {
  606.     struct file_name_list *next;
  607.     char *fname;
  608.     /* If the following is nonzero, it is a macro name.
  609.        Don't include the file again if that macro is defined.  */
  610.     U_CHAR *control_macro;
  611.     /* If the following is nonzero, it is a C-language system include
  612.        directory.  */
  613.     int c_system_include_path;
  614.     /* Mapping of file names for this directory.  */
  615.     struct file_name_map *name_map;
  616.     /* Non-zero if name_map is valid.  */
  617.     int got_name_map;
  618.   };
  619.  
  620. /* #include "file" looks in source file dir, then stack. */
  621. /* #include <file> just looks in the stack. */
  622. /* -I directories are added to the end, then the defaults are added. */
  623. /* The */
  624. static struct default_include {
  625.   char *fname;            /* The name of the directory.  */
  626.   int cplusplus;        /* Only look here if we're compiling C++.  */
  627.   int cxx_aware;        /* Includes in this directory don't need to
  628.                    be wrapped in extern "C" when compiling
  629.                    C++.  */
  630. } include_defaults_array[]
  631. #ifdef INCLUDE_DEFAULTS
  632.   = INCLUDE_DEFAULTS;
  633. #else
  634.   = {
  635.     /* Pick up GNU C++ specific include files.  */
  636.     { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
  637. #ifdef CROSS_COMPILE
  638.     /* This is the dir for fixincludes.  Put it just before
  639.        the files that we fix.  */
  640.     { GCC_INCLUDE_DIR, 0, 0 },
  641.     /* For cross-compilation, this dir name is generated
  642.        automatically in Makefile.in.  */
  643.     { CROSS_INCLUDE_DIR, 0, 0 },
  644.     /* This is another place that the target system's headers might be.  */
  645.     { TOOL_INCLUDE_DIR, 0, 0 },
  646. #else /* not CROSS_COMPILE */
  647.     /* This should be /usr/local/include and should come before
  648.        the fixincludes-fixed header files.  */
  649.     { LOCAL_INCLUDE_DIR, 0, 1 },
  650.     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
  651.        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
  652.     { TOOL_INCLUDE_DIR, 0, 0 },
  653.     /* This is the dir for fixincludes.  Put it just before
  654.        the files that we fix.  */
  655.     { GCC_INCLUDE_DIR, 0, 0 },
  656.     /* Some systems have an extra dir of include files.  */
  657. #ifdef SYSTEM_INCLUDE_DIR
  658.     { SYSTEM_INCLUDE_DIR, 0, 0 },
  659. #endif
  660.     { STANDARD_INCLUDE_DIR, 0, 0 },
  661. #endif /* not CROSS_COMPILE */
  662.     { 0, 0, 0 }
  663.     };
  664. #endif /* no INCLUDE_DEFAULTS */
  665.  
  666. #ifdef NEXT_FRAMEWORKS
  667. static struct default_include framework_defaults_array [] = {
  668.     {"/LocalLibrary/Frameworks", 0, 0},
  669.     {"/NextLibrary/Frameworks", 0, 0}, 
  670.     {NULL, 0, 0}
  671.  };
  672.  
  673. static struct default_include *framework_defaults=framework_defaults_array;
  674. static struct file_name_list *framework = 0;
  675.  
  676. struct framework_header {char * dirName; int dirNameLen; };
  677. static struct framework_header framework_header_dirs[] = {
  678.    { "PrivateHeaders", 14 },
  679.    { "Headers", 7 },
  680.    { NULL, 0 }
  681. };
  682. #endif
  683.  
  684. /* The code looks at the defaults through this pointer, rather than through
  685.    the constant structure above.  This pointer gets changed if an environment
  686.    variable specifies other defaults.  */
  687. static struct default_include *include_defaults = include_defaults_array;
  688.  
  689. static struct file_name_list *include = 0;    /* First dir to search */
  690.     /* First dir to search for <file> */
  691. /* This is the first element to use for #include <...>.
  692.    If it is 0, use the entire chain for such includes.  */
  693. static struct file_name_list *first_bracket_include = 0;
  694. #ifdef NEXT_FRAMEWORKS
  695. static struct file_name_list *first_bracket_framework = 0;
  696. static struct file_name_list *last_framework = 0; /* Last in chain */
  697. #endif
  698. /* This is the first element in the chain that corresponds to
  699.    a directory of system header files.  */
  700. static struct file_name_list *first_system_include = 0;
  701. static struct file_name_list *last_include = 0;    /* Last in chain */
  702.  
  703. /* Chain of include directories to put at the end of the other chain.  */
  704. static struct file_name_list *after_include = 0;
  705. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  706.  
  707. /* Chain to put at the start of the system include files.  */
  708. static struct file_name_list *before_system = 0;
  709. static struct file_name_list *last_before_system = 0;    /* Last in chain */
  710.  
  711. /* List of included files that contained #pragma once.  */
  712. static struct file_name_list *dont_repeat_files = 0;
  713.  
  714. /* List of other included files.
  715.    If ->control_macro if nonzero, the file had a #ifndef
  716.    around the entire contents, and ->control_macro gives the macro name.  */
  717. static struct file_name_list *all_include_files = 0;
  718.  
  719. /* Directory prefix that should replace `/usr' in the standard
  720.    include file directories.  */
  721. static char *include_prefix;
  722.  
  723. /* Global list of strings read in from precompiled files.  This list
  724.    is kept in the order the strings are read in, with new strings being
  725.    added at the end through stringlist_tailp.  We use this list to output
  726.    the strings at the end of the run. 
  727. */
  728. static STRINGDEF *stringlist;
  729. static STRINGDEF **stringlist_tailp = &stringlist;
  730.  
  731.  
  732. /* Structure returned by create_definition */
  733. typedef struct macrodef MACRODEF;
  734. struct macrodef
  735. {
  736.   struct definition *defn;
  737.   U_CHAR *symnam;
  738.   int symlen;
  739. };
  740.  
  741. enum sharp_token_type {
  742.   NO_SHARP_TOKEN = 0,        /* token not present */
  743.  
  744.   SHARP_TOKEN = '#',        /* token spelled with # only */
  745.   WHITE_SHARP_TOKEN,        /* token spelled with # and white space */
  746.  
  747.   PERCENT_COLON_TOKEN = '%',    /* token spelled with %: only */
  748.   WHITE_PERCENT_COLON_TOKEN    /* token spelled with %: and white space */
  749. };
  750.  
  751. /* Structure allocated for every #define.  For a simple replacement
  752.    such as
  753.        #define foo bar ,
  754.    nargs = -1, the `pattern' list is null, and the expansion is just
  755.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  756.    e.g.,
  757.        #define getchar() getc (stdin) .
  758.    When there are args, the expansion is the replacement text with the
  759.    args squashed out, and the reflist is a list describing how to
  760.    build the output from the input: e.g., "3 chars, then the 1st arg,
  761.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  762.    The chars here come from the expansion.  Whatever is left of the
  763.    expansion after the last arg-occurrence is copied after that arg.
  764.    Note that the reflist can be arbitrarily long---
  765.    its length depends on the number of times the arguments appear in
  766.    the replacement text, not how many args there are.  Example:
  767.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  768.    pattern list
  769.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  770.    where (x, y) means (nchars, argno). */
  771.  
  772. typedef struct definition DEFINITION;
  773. struct definition {
  774.   int nargs;
  775.   int length;            /* length of expansion string */
  776.   int predefined;        /* True if the macro was builtin or */
  777.                 /* came from the command line */
  778.   U_CHAR *expansion;
  779.   int line;            /* Line number of definition */
  780.   char *file;            /* File of definition */
  781.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  782.   struct reflist {
  783.     struct reflist *next;
  784.  
  785.     enum sharp_token_type stringify;    /* set if a # operator before arg */
  786.     enum sharp_token_type raw_before;    /* set if a ## operator before arg */
  787.     enum sharp_token_type raw_after;    /* set if a ## operator after arg */
  788.  
  789.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  790.     int nchars;            /* Number of literal chars to copy before
  791.                    this arg occurrence.  */
  792.     int argno;            /* Number of arg to substitute (origin-0) */
  793.   } *pattern;
  794.   union {
  795.     /* Names of macro args, concatenated in reverse order
  796.        with comma-space between them.
  797.        The only use of this is that we warn on redefinition
  798.        if this differs between the old and new definitions.  */
  799.     U_CHAR *argnames;
  800.   } args;
  801. };
  802.  
  803. /* different kinds of things that can appear in the value field
  804.    of a hash node.  Actually, this may be useless now. */
  805. union hashval {
  806.   char *cpval;
  807.   DEFINITION *defn;
  808.   KEYDEF *keydef;
  809. };
  810.  
  811. /*
  812.  * special extension string that can be added to the last macro argument to 
  813.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  814.  *         #define wow(a, b...)        process (b, a, b)
  815.  *        { wow (1, 2, 3); }    ->    { process (2, 3, 1, 2, 3); }
  816.  *        { wow (one, two); }    ->    { process (two, one, two); }
  817.  * if this "rest_arg" is used with the concat token '##' and if it is not
  818.  * supplied then the token attached to with ## will not be outputted.  Ex:
  819.  *         #define wow (a, b...)        process (b ## , a, ## b)
  820.  *        { wow (1, 2); }        ->    { process (2, 1, 2); }
  821.  *        { wow (one); }        ->    { process (one); {
  822.  */
  823. static char rest_extension[] = "...";
  824. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  825.  
  826. /* The structure of a node in the hash table.  The hash table
  827.    has entries for all tokens defined by #define directives (type T_MACRO),
  828.    plus some special tokens like __LINE__ (these each have their own
  829.    type, and the appropriate code is run when that type of node is seen.
  830.    It does not contain control words like "#define", which are recognized
  831.    by a separate piece of code. */
  832.  
  833. /* different flavors of hash nodes --- also used in keyword table */
  834. enum node_type {
  835.  T_DEFINE = 1,    /* the `#define' keyword */
  836.  T_INCLUDE,    /* the `#include' keyword */
  837.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  838.  T_IMPORT,      /* the `#import' keyword */
  839.  T_IFDEF,    /* the `#ifdef' keyword */
  840.  T_IFNDEF,    /* the `#ifndef' keyword */
  841.  T_IF,        /* the `#if' keyword */
  842.  T_ELSE,    /* `#else' */
  843.  T_PRAGMA,    /* `#pragma' */
  844.  T_ELIF,    /* `#elif' */
  845.  T_UNDEF,    /* `#undef' */
  846.  T_LINE,    /* `#line' */
  847.  T_ERROR,    /* `#error' */
  848.  T_WARNING,    /* `#warning' */
  849.  T_ENDIF,    /* `#endif' */
  850.  T_SCCS,    /* `#sccs', used on system V.  */
  851.  T_IDENT,    /* `#ident', used on system V.  */
  852.  T_ASSERT,    /* `#assert', taken from system V.  */
  853.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  854.  T_SPECLINE,    /* special symbol `__LINE__' */
  855.  T_DATE,    /* `__DATE__' */
  856.  T_FILE,    /* `__FILE__' */
  857.  T_BASE_FILE,    /* `__BASE_FILE__' */
  858.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  859.  T_VERSION,    /* `__VERSION__' */
  860.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  861.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  862.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  863.  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
  864.  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
  865.  T_IMMEDIATE_PREFIX_TYPE,  /* `__IMMEDIATE_PREFIX__' */
  866.  T_TIME,    /* `__TIME__' */
  867.  T_CONST,    /* Constant value, used by `__STDC__' */
  868.  T_MACRO,    /* macro defined by `#define' */
  869.  T_DISABLED,    /* macro temporarily turned off for rescan */
  870.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  871.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  872.  T_UNUSED    /* Used for something not defined.  */
  873.  };
  874.  
  875. struct hashnode {
  876.   struct hashnode *next;    /* double links for easy deletion */
  877.   struct hashnode *prev;
  878.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  879.                    chain is kept, in case the node is the head
  880.                    of the chain and gets deleted. */
  881.   enum node_type type;        /* type of special token */
  882.   int length;            /* length of token, for quick comparison */
  883.   U_CHAR *name;            /* the actual name */
  884.   union hashval value;        /* pointer to expansion, or whatever */
  885. };
  886.  
  887. typedef struct hashnode HASHNODE;
  888.  
  889. /* Some definitions for the hash table.  The hash function MUST be
  890.    computed as shown in hashf () below.  That is because the rescan
  891.    loop computes the hash value `on the fly' for most tokens,
  892.    in order to avoid the overhead of a lot of procedure calls to
  893.    the hashf () function.  Hashf () only exists for the sake of
  894.    politeness, for use when speed isn't so important. */
  895.  
  896. #define HASHSIZE 1403
  897. static HASHNODE *hashtab[HASHSIZE];
  898. #define HASHSTEP(old, c) ((old << 2) + c)
  899. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  900.  
  901. /* Symbols to predefine.  */
  902.  
  903. #ifdef CPP_PREDEFINES
  904. static char *predefs = CPP_PREDEFINES;
  905. #else
  906. static char *predefs = "";
  907. #endif
  908.  
  909. /* We let tm.h override the types used here, to handle trivial differences
  910.    such as the choice of unsigned int or long unsigned int for size_t.
  911.    When machines start needing nontrivial differences in the size type,
  912.    it would be best to do something here to figure out automatically
  913.    from other information what type to use.  */
  914.  
  915. /* The string value for __SIZE_TYPE__.  */
  916.  
  917. #ifndef SIZE_TYPE
  918. #define SIZE_TYPE "long unsigned int"
  919. #endif
  920.  
  921. /* The string value for __PTRDIFF_TYPE__.  */
  922.  
  923. #ifndef PTRDIFF_TYPE
  924. #define PTRDIFF_TYPE "long int"
  925. #endif
  926.  
  927. /* The string value for __WCHAR_TYPE__.  */
  928.  
  929. #ifndef WCHAR_TYPE
  930. #define WCHAR_TYPE "int"
  931. #endif
  932. char * wchar_type = WCHAR_TYPE;
  933. #undef WCHAR_TYPE
  934.  
  935. /* The string value for __USER_LABEL_PREFIX__ */
  936.  
  937. #ifndef USER_LABEL_PREFIX
  938. #define USER_LABEL_PREFIX ""
  939. #endif
  940.  
  941. /* The string value for __REGISTER_PREFIX__ */
  942.  
  943. #ifndef REGISTER_PREFIX
  944. #define REGISTER_PREFIX ""
  945. #endif
  946.  
  947. /* The string value for __IMMEDIATE_PREFIX__ */
  948.  
  949. #ifndef IMMEDIATE_PREFIX
  950. #define IMMEDIATE_PREFIX ""
  951. #endif
  952.  
  953. /* In the definition of a #assert name, this structure forms
  954.    a list of the individual values asserted.
  955.    Each value is itself a list of "tokens".
  956.    These are strings that are compared by name.  */
  957.  
  958. struct tokenlist_list {
  959.   struct tokenlist_list *next;
  960.   struct arglist *tokens;
  961. };
  962.  
  963. struct assertion_hashnode {
  964.   struct assertion_hashnode *next;    /* double links for easy deletion */
  965.   struct assertion_hashnode *prev;
  966.   /* also, a back pointer to this node's hash
  967.      chain is kept, in case the node is the head
  968.      of the chain and gets deleted. */
  969.   struct assertion_hashnode **bucket_hdr;
  970.   int length;            /* length of token, for quick comparison */
  971.   U_CHAR *name;            /* the actual name */
  972.   /* List of token-sequences.  */
  973.   struct tokenlist_list *value;
  974. };
  975.  
  976. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  977.  
  978. /* Some definitions for the hash table.  The hash function MUST be
  979.    computed as shown in hashf below.  That is because the rescan
  980.    loop computes the hash value `on the fly' for most tokens,
  981.    in order to avoid the overhead of a lot of procedure calls to
  982.    the hashf function.  hashf only exists for the sake of
  983.    politeness, for use when speed isn't so important. */
  984.  
  985. #define ASSERTION_HASHSIZE 37
  986. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  987.  
  988. /* Nonzero means inhibit macroexpansion of what seem to be
  989.    assertion tests, in rescan.  For #if.  */
  990. static int assertions_flag;
  991.  
  992. /* `struct directive' defines one #-directive, including how to handle it.  */
  993.  
  994. #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
  995.  
  996. struct directive {
  997.   int length;            /* Length of name */
  998.   int (*func) DO_PROTO;    /* Function to handle directive */
  999.   char *name;            /* Name of directive */
  1000.   enum node_type type;        /* Code which describes which directive. */
  1001.   char angle_brackets;        /* Nonzero => <...> is special.  */
  1002.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  1003.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  1004. };
  1005.  
  1006. /* These functions are declared to return int instead of void since they
  1007.    are going to be placed in the table and some old compilers have trouble with
  1008.    pointers to functions returning void.  */
  1009.  
  1010. static int do_assert DO_PROTO;
  1011. static int do_define DO_PROTO;
  1012. static int do_elif DO_PROTO;
  1013. static int do_else DO_PROTO;
  1014. static int do_endif DO_PROTO;
  1015. static int do_error DO_PROTO;
  1016. static int do_ident DO_PROTO;
  1017. static int do_if DO_PROTO;
  1018. static int do_include DO_PROTO;
  1019. static int do_line DO_PROTO;
  1020. static int do_pragma DO_PROTO;
  1021. #ifdef SCCS_DIRECTIVE
  1022. static int do_sccs DO_PROTO;
  1023. #endif
  1024. static int do_unassert DO_PROTO;
  1025. static int do_undef DO_PROTO;
  1026. static int do_warning DO_PROTO;
  1027. static int do_xifdef DO_PROTO;
  1028.  
  1029. /* Here is the actual list of #-directives, most-often-used first.  */
  1030.  
  1031. static struct directive directive_table[] = {
  1032.   {  6, do_define, "define", T_DEFINE, 0, 1},
  1033.   {  2, do_if, "if", T_IF},
  1034.   {  5, do_xifdef, "ifdef", T_IFDEF},
  1035.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  1036.   {  5, do_endif, "endif", T_ENDIF},
  1037.   {  4, do_else, "else", T_ELSE},
  1038.   {  4, do_elif, "elif", T_ELIF},
  1039.   {  4, do_line, "line", T_LINE},
  1040.   {  7, do_include, "include", T_INCLUDE, 1},
  1041.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  1042.   {  6, do_include, "import", T_IMPORT, 1},
  1043.   {  5, do_undef, "undef", T_UNDEF},
  1044.   {  5, do_error, "error", T_ERROR},
  1045.   {  7, do_warning, "warning", T_WARNING},
  1046. #ifdef SCCS_DIRECTIVE
  1047.   {  4, do_sccs, "sccs", T_SCCS},
  1048. #endif
  1049.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  1050.   {  5, do_ident, "ident", T_IDENT},
  1051.   {  6, do_assert, "assert", T_ASSERT},
  1052.   {  8, do_unassert, "unassert", T_UNASSERT},
  1053.   {  -1, 0, "", T_UNUSED},
  1054. };
  1055.  
  1056. /* When a directive handler is called,
  1057.    this points to the # (or the : of the %:) that started the directive.  */
  1058. U_CHAR *directive_start;
  1059.  
  1060. /* table to tell if char can be part of a C identifier. */
  1061. U_CHAR is_idchar[256];
  1062. /* table to tell if char can be first char of a c identifier. */
  1063. U_CHAR is_idstart[256];
  1064. /* table to tell if c is horizontal space.  */
  1065. U_CHAR is_hor_space[256];
  1066. /* table to tell if c is horizontal or vertical space.  */
  1067. static U_CHAR is_space[256];
  1068. /* names of some characters */
  1069. static char *char_name[256];
  1070.  
  1071. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  1072. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  1073.   
  1074. static int errors = 0;            /* Error counter for exit code */
  1075.  
  1076. /* Name of output file, for error messages.  */
  1077. static char *out_fname;
  1078.  
  1079. /* Zero means dollar signs are punctuation.
  1080.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  1081.    This must be 0 for correct processing of this ANSI C program:
  1082.     #define foo(a) #a
  1083.     #define lose(b) foo (b)
  1084.     #define test$
  1085.     lose (test)    */
  1086. static int dollars_in_ident;
  1087. #ifndef DOLLARS_IN_IDENTIFIERS
  1088. #define DOLLARS_IN_IDENTIFIERS 1
  1089. #endif
  1090.  
  1091.  
  1092. /* Stack of conditionals currently in progress
  1093.    (including both successful and failing conditionals).  */
  1094.  
  1095. struct if_stack {
  1096.   struct if_stack *next;    /* for chaining to the next stack frame */
  1097.   char *fname;        /* copied from input when frame is made */
  1098.   int lineno;            /* similarly */
  1099.   int if_succeeded;        /* true if a leg of this if-group
  1100.                     has been passed through rescan */
  1101.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  1102.                    this is the macro name tested.  */
  1103.   enum node_type type;        /* type of last directive seen in this group */
  1104. };
  1105. typedef struct if_stack IF_STACK_FRAME;
  1106. static IF_STACK_FRAME *if_stack = NULL;
  1107.  
  1108. /* Buffer of -M output.  */
  1109. static char *deps_buffer;
  1110.  
  1111. /* Number of bytes allocated in above.  */
  1112. static int deps_allocated_size;
  1113.  
  1114. /* Number of bytes used.  */
  1115. static int deps_size;
  1116.  
  1117. /* Number of bytes since the last newline.  */
  1118. static int deps_column;
  1119.  
  1120. /* Nonzero means -I- has been seen,
  1121.    so don't look for #include "foo" the source-file directory.  */
  1122. static int ignore_srcdir;
  1123.  
  1124. static int safe_read PROTO((int, char *, int));
  1125. static void safe_write PROTO((int, char *, int));
  1126.  
  1127. int main PROTO((int, char **));
  1128.  
  1129. static void path_include PROTO((char *));
  1130.  
  1131. static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
  1132.  
  1133. static void trigraph_pcp PROTO((FILE_BUF *));
  1134.  
  1135. static void newline_fix PROTO((U_CHAR *));
  1136. static void name_newline_fix PROTO((U_CHAR *));
  1137.  
  1138. static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
  1139.  
  1140. static void rescan PROTO((FILE_BUF *, int));
  1141.  
  1142. static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
  1143.  
  1144. static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
  1145.  
  1146. static struct tm *timestamp PROTO((void));
  1147. static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
  1148.  
  1149. static int redundant_include_p PROTO((char *));
  1150. static int is_system_include PROTO((char *));
  1151. static char *skip_redundant_dir_prefix PROTO((char *));
  1152.  
  1153. static char *read_filename_string PROTO((int, FILE *));
  1154. static struct file_name_map *read_name_map PROTO((char *));
  1155. static int open_include_file PROTO((char *, struct file_name_list *));
  1156.  
  1157. static void finclude PROTO((int, char *, FILE_BUF *, int, struct file_name_list *));
  1158. static void record_control_macro PROTO((char *, U_CHAR *));
  1159.  
  1160. static int import_hash PROTO((char *));
  1161. static int lookup_import PROTO((char *, struct file_name_list *));
  1162. static void add_import PROTO((int, char *));
  1163.  
  1164. static char *check_precompiled PROTO((int, char *, char **));
  1165. static int check_preconditions PROTO((char *));
  1166. static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *));
  1167. static void pcstring_used PROTO((HASHNODE *));
  1168. static void write_output PROTO((void));
  1169. static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
  1170.  
  1171. static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
  1172.  
  1173. static int check_macro_name PROTO((U_CHAR *, char *));
  1174. static int compare_defs PROTO((DEFINITION *, DEFINITION *));
  1175. static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
  1176.  
  1177. static DEFINITION *collect_expansion  PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
  1178.  
  1179. int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
  1180. static int compare_token_lists PROTO((struct arglist *, struct arglist *));
  1181.  
  1182. static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
  1183. static void free_token_list PROTO((struct arglist *));
  1184.  
  1185. static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
  1186. static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
  1187. static void delete_assertion PROTO((ASSERTION_HASHNODE *));
  1188.  
  1189. static void do_once PROTO((void));
  1190.  
  1191. static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
  1192. static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
  1193. static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
  1194. static void validate_else PROTO((U_CHAR *));
  1195.  
  1196. static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
  1197. static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
  1198. static char *quote_string PROTO((char *, char *));
  1199. static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
  1200.  
  1201. #if defined (NEXT_SEMANTICS) || defined(NEXT_PDO)
  1202. /* convert rtf */
  1203. static void buf_convert_rtf ();
  1204. static const char * rtf_to_ascii ();
  1205. #endif
  1206.  
  1207. /* Last arg to output_line_directive.  */
  1208. enum file_change_code {same_file, enter_file, leave_file};
  1209. static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
  1210.  
  1211. static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
  1212.  
  1213. struct argdata;
  1214. static char *macarg PROTO((struct argdata *, int));
  1215.  
  1216. static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, int *, int *, int *, int));
  1217.  
  1218. static int discard_comments PROTO((U_CHAR *, int, int));
  1219.  
  1220. static int change_newlines PROTO((U_CHAR *, int));
  1221.  
  1222. char *my_strerror PROTO((int));
  1223. void error PRINTF_PROTO_1((char *, ...));
  1224. static void verror PROTO((char *, va_list));
  1225. static void error_from_errno PROTO((char *));
  1226. void warning PRINTF_PROTO_1((char *, ...));
  1227. static void vwarning PROTO((char *, va_list));
  1228. static void error_with_line PRINTF_PROTO_2((int, char *, ...));
  1229. static void verror_with_line PROTO((int, char *, va_list));
  1230. static void vwarning_with_line PROTO((int, char *, va_list));
  1231. static void warning_with_line PRINTF_PROTO_2((int, char *, ...));
  1232. void pedwarn PRINTF_PROTO_1((char *, ...));
  1233. void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
  1234. static void pedwarn_with_file_and_line PRINTF_PROTO_3((char *, int, char *, ...));
  1235.  
  1236. static void print_containing_files PROTO((void));
  1237.  
  1238. static int line_for_error PROTO((int));
  1239. static int grow_outbuf PROTO((FILE_BUF *, int));
  1240.  
  1241. static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
  1242. HASHNODE *lookup PROTO((U_CHAR *, int, int));
  1243. static void delete_macro PROTO((HASHNODE *));
  1244. static int hashf PROTO((U_CHAR *, int, int));
  1245.  
  1246. static void dump_single_macro PROTO((HASHNODE *, FILE *));
  1247. static void dump_all_macros PROTO((void));
  1248. static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
  1249. static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
  1250.  
  1251. static void initialize_char_syntax PROTO((void));
  1252. static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
  1253.  
  1254. static void make_definition PROTO((char *, FILE_BUF *));
  1255. static void make_undef PROTO((char *, FILE_BUF *));
  1256.  
  1257. static void make_assertion PROTO((char *, char *));
  1258.  
  1259. static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
  1260. #ifdef NEXT_FRAMEWORKS
  1261. static void append_framework_chain ();
  1262. #endif
  1263. static void deps_output PROTO((char *, int));
  1264.  
  1265. static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
  1266. void fancy_abort PROTO((void)) __attribute__ ((noreturn));
  1267. static void perror_with_name PROTO((char *));
  1268. static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
  1269. static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
  1270.  
  1271. static void memory_full PROTO((void)) __attribute__ ((noreturn));
  1272. GENERIC_PTR xmalloc PROTO((size_t));
  1273. static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t));
  1274. static GENERIC_PTR xcalloc PROTO((size_t, size_t));
  1275. static char *savestring PROTO((char *));
  1276.  
  1277. static int file_size_and_mode PROTO((int, int *, long int *));
  1278.  
  1279. /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
  1280.    retrying if necessary.  Return a negative value if an error occurs,
  1281.    otherwise return the actual number of bytes read,
  1282.    which must be LEN unless end-of-file was reached.  */
  1283.  
  1284. static int
  1285. safe_read (desc, ptr, len)
  1286.      int desc;
  1287.      char *ptr;
  1288.      int len;
  1289. {
  1290.   int left = len;
  1291.   while (left > 0) {
  1292.     int nchars = read (desc, ptr, left);
  1293.     if (nchars < 0)
  1294.       {
  1295. #ifdef EINTR
  1296.     if (errno == EINTR)
  1297.       continue;
  1298. #endif
  1299.     return nchars;
  1300.       }
  1301.     if (nchars == 0)
  1302.       break;
  1303.     ptr += nchars;
  1304.     left -= nchars;
  1305.   }
  1306.   return len - left;
  1307. }
  1308.  
  1309. /* Write LEN bytes at PTR to descriptor DESC,
  1310.    retrying if necessary, and treating any real error as fatal.  */
  1311.  
  1312. static void
  1313. safe_write (desc, ptr, len)
  1314.      int desc;
  1315.      char *ptr;
  1316.      int len;
  1317. {
  1318.   while (len > 0) {
  1319.     int written = write (desc, ptr, len);
  1320.     if (written < 0)
  1321.       {
  1322. #ifdef EINTR
  1323.     if (errno == EINTR)
  1324.       continue;
  1325. #endif
  1326.     pfatal_with_name (out_fname);
  1327.       }
  1328.     ptr += written;
  1329.     len -= written;
  1330.   }
  1331. }
  1332.  
  1333. int
  1334. main (argc, argv)
  1335.      int argc;
  1336.      char **argv;
  1337. {
  1338.   int st_mode;
  1339.   long st_size;
  1340.   char *in_fname;
  1341.   char *cp;
  1342.   int f, i;
  1343.   FILE_BUF *fp;
  1344.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  1345.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  1346.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  1347.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  1348.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  1349.  
  1350.   /* Record the option used with each element of pend_assertions.
  1351.      This is preparation for supporting more than one option for making
  1352.      an assertion.  */
  1353.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  1354.   int inhibit_predefs = 0;
  1355.   int no_standard_includes = 0;
  1356.   int no_standard_cplusplus_includes = 0;
  1357.   int missing_newline = 0;
  1358.  
  1359.   /* Non-0 means don't output the preprocessed program.  */
  1360.   int inhibit_output = 0;
  1361.   /* Non-0 means -v, so print the full set of include dirs.  */
  1362.   int verbose = 0;
  1363.  
  1364.   /* File name which deps are being written to.
  1365.      This is 0 if deps are being written to stdout.  */
  1366.   char *deps_file = 0;
  1367.   /* Fopen file mode to open deps_file with.  */
  1368.   char *deps_mode = "a";
  1369.   /* Stream on which to print the dependency information.  */
  1370.   FILE *deps_stream = 0;
  1371.   /* Target-name to write with the dependency information.  */
  1372.   char *deps_target = 0;
  1373.   struct file_name_list *include_ptr; 
  1374.  
  1375. #ifdef RLIMIT_STACK
  1376.   /* Get rid of any avoidable limit on stack size.  */
  1377.   {
  1378.     struct rlimit rlim;
  1379.  
  1380.     /* Set the stack limit huge so that alloca (particularly stringtab
  1381.      * in dbxread.c) does not fail. */
  1382.     getrlimit (RLIMIT_STACK, &rlim);
  1383.     rlim.rlim_cur = rlim.rlim_max;
  1384.     setrlimit (RLIMIT_STACK, &rlim);
  1385.   }
  1386. #endif /* RLIMIT_STACK defined */
  1387.  
  1388. #ifdef SIGPIPE
  1389.   signal (SIGPIPE, pipe_closed);
  1390. #endif
  1391.  
  1392.   cp = argv[0] + strlen (argv[0]);
  1393.   while (cp != argv[0] && cp[-1] != '/'
  1394. #ifdef DIR_SEPARATOR
  1395.      && cp[-1] != DIR_SEPARATOR
  1396. #endif
  1397.      )
  1398.     --cp;
  1399.   progname = cp;
  1400.  
  1401. #ifdef VMS
  1402.   {
  1403.     /* Remove directories from PROGNAME.  */
  1404.     char *p;
  1405.     char *s = progname;
  1406.  
  1407.     if ((p = rindex (s, ':')) != 0) s = p + 1;    /* skip device */
  1408.     if ((p = rindex (s, ']')) != 0) s = p + 1;    /* skip directory */
  1409.     if ((p = rindex (s, '>')) != 0) s = p + 1;    /* alternate (int'n'l) dir */
  1410.     s = progname = savestring (s);
  1411.     if ((p = rindex (s, ';')) != 0) *p = '\0';    /* strip version number */
  1412.     if ((p = rindex (s, '.')) != 0        /* strip type iff ".exe" */
  1413.     && (p[1] == 'e' || p[1] == 'E')
  1414.     && (p[2] == 'x' || p[2] == 'X')
  1415.     && (p[3] == 'e' || p[3] == 'E')
  1416.     && !p[4])
  1417.       *p = '\0';
  1418.   }
  1419. #endif
  1420.  
  1421.   in_fname = NULL;
  1422.   out_fname = NULL;
  1423.  
  1424.   /* Initialize is_idchar to allow $.  */
  1425.   dollars_in_ident = 1;
  1426.   initialize_char_syntax ();
  1427.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1428.  
  1429.   no_line_directives = 0;
  1430.   no_trigraphs = 1;
  1431.   dump_macros = dump_none;
  1432.   no_output = 0;
  1433.   cplusplus = 0;
  1434.   cplusplus_comments = 1;
  1435.  
  1436.   bzero ((char *) pend_files, argc * sizeof (char *));
  1437.   bzero ((char *) pend_defs, argc * sizeof (char *));
  1438.   bzero ((char *) pend_undefs, argc * sizeof (char *));
  1439.   bzero ((char *) pend_assertions, argc * sizeof (char *));
  1440.   bzero ((char *) pend_includes, argc * sizeof (char *));
  1441.  
  1442.   /* Process switches and find input file name.  */
  1443.  
  1444.   for (i = 1; i < argc; i++) {
  1445.     if (argv[i][0] != '-') {
  1446.       if (out_fname != NULL)
  1447.     fatal ("Usage: %s [switches] input output", argv[0]);
  1448.       else if (in_fname != NULL)
  1449.     out_fname = argv[i];
  1450.       else
  1451.     in_fname = argv[i];
  1452.     } else {
  1453.       switch (argv[i][1]) {
  1454. #ifdef NEXT_SEMANTICS
  1455.       case 'a':
  1456.     if (!strcmp (argv[i], "-arch"))    /* ignore -arch <archname> */
  1457.       i++;
  1458.     break;
  1459.  
  1460.       case 'O':
  1461.     if (! strcmp (argv[i], "-ObjC++"))
  1462.       ; /* no-op */
  1463.     break;
  1464.         
  1465.       case 'c':
  1466.     if (! strcmp (argv[i], "-c++-comments"))
  1467.       cplusplus_comments = 1;
  1468.     break;
  1469. #endif
  1470.  
  1471.       case 'i':
  1472.     if (!strcmp (argv[i], "-include")) {
  1473.       if (i + 1 == argc)
  1474.         fatal ("Filename missing after `-include' option");
  1475.       else
  1476.         pend_includes[i] = argv[i+1], i++;
  1477.     }
  1478.     if (!strcmp (argv[i], "-imacros")) {
  1479.       if (i + 1 == argc)
  1480.         fatal ("Filename missing after `-imacros' option");
  1481.       else
  1482.         pend_files[i] = argv[i+1], i++;
  1483.     }
  1484.     if (!strcmp (argv[i], "-iprefix")) {
  1485.       if (i + 1 == argc)
  1486.         fatal ("Filename missing after `-iprefix' option");
  1487.       else
  1488.         include_prefix = argv[++i];
  1489.     }
  1490.     if (!strcmp (argv[i], "-ifoutput")) {
  1491.       output_conditionals = 1;
  1492.     }
  1493.     if (!strcmp (argv[i], "-isystem")) {
  1494.       struct file_name_list *dirtmp;
  1495.  
  1496.       if (i + 1 == argc)
  1497.         fatal ("Filename missing after `-isystem' option");
  1498.  
  1499.       dirtmp = (struct file_name_list *)
  1500.         xmalloc (sizeof (struct file_name_list));
  1501.       dirtmp->next = 0;
  1502.       dirtmp->control_macro = 0;
  1503.       dirtmp->c_system_include_path = 1;
  1504.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + 1);
  1505.       strcpy (dirtmp->fname, argv[++i]);
  1506.       dirtmp->got_name_map = 0;
  1507.  
  1508.       if (before_system == 0)
  1509.         before_system = dirtmp;
  1510.       else
  1511.         last_before_system->next = dirtmp;
  1512.       last_before_system = dirtmp; /* Tail follows the last one */
  1513.     }
  1514.     /* Add directory to end of path for includes,
  1515.        with the default prefix at the front of its name.  */
  1516.     if (!strcmp (argv[i], "-iwithprefix")) {
  1517.       struct file_name_list *dirtmp;
  1518.       char *prefix;
  1519.  
  1520.       if (include_prefix != 0)
  1521.         prefix = include_prefix;
  1522.       else {
  1523.         prefix = savestring (GCC_INCLUDE_DIR);
  1524.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1525.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1526.           prefix[strlen (prefix) - 7] = 0;
  1527.       }
  1528.  
  1529.       dirtmp = (struct file_name_list *)
  1530.         xmalloc (sizeof (struct file_name_list));
  1531.       dirtmp->next = 0;    /* New one goes on the end */
  1532.       dirtmp->control_macro = 0;
  1533.       dirtmp->c_system_include_path = 0;
  1534.       if (i + 1 == argc)
  1535.         fatal ("Directory name missing after `-iwithprefix' option");
  1536.  
  1537.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1);
  1538.       strcpy (dirtmp->fname, prefix);
  1539.       strcat (dirtmp->fname, argv[++i]);
  1540.       dirtmp->got_name_map = 0;
  1541.  
  1542.       if (after_include == 0)
  1543.         after_include = dirtmp;
  1544.       else
  1545.         last_after_include->next = dirtmp;
  1546.       last_after_include = dirtmp; /* Tail follows the last one */
  1547.     }
  1548.     /* Add directory to main path for includes,
  1549.        with the default prefix at the front of its name.  */
  1550.     if (!strcmp (argv[i], "-iwithprefixbefore")) {
  1551.       struct file_name_list *dirtmp;
  1552.       char *prefix;
  1553.  
  1554.       if (include_prefix != 0)
  1555.         prefix = include_prefix;
  1556.       else {
  1557.         prefix = savestring (GCC_INCLUDE_DIR);
  1558.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1559.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1560.           prefix[strlen (prefix) - 7] = 0;
  1561.       }
  1562.  
  1563.       dirtmp = (struct file_name_list *)
  1564.         xmalloc (sizeof (struct file_name_list));
  1565.       dirtmp->next = 0;    /* New one goes on the end */
  1566.       dirtmp->control_macro = 0;
  1567.       dirtmp->c_system_include_path = 0;
  1568.       if (i + 1 == argc)
  1569.         fatal ("Directory name missing after `-iwithprefixbefore' option");
  1570.  
  1571.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1);
  1572.       strcpy (dirtmp->fname, prefix);
  1573.       strcat (dirtmp->fname, argv[++i]);
  1574.       dirtmp->got_name_map = 0;
  1575.  
  1576.       append_include_chain (dirtmp, dirtmp);
  1577.     }
  1578.     /* Add directory to end of path for includes.  */
  1579.     if (!strcmp (argv[i], "-idirafter")) {
  1580.       struct file_name_list *dirtmp;
  1581.  
  1582.       dirtmp = (struct file_name_list *)
  1583.         xmalloc (sizeof (struct file_name_list));
  1584.       dirtmp->next = 0;    /* New one goes on the end */
  1585.       dirtmp->control_macro = 0;
  1586.       dirtmp->c_system_include_path = 0;
  1587.       if (i + 1 == argc)
  1588.         fatal ("Directory name missing after `-idirafter' option");
  1589.       else
  1590.         dirtmp->fname = argv[++i];
  1591.       dirtmp->got_name_map = 0;
  1592.  
  1593.       if (after_include == 0)
  1594.         after_include = dirtmp;
  1595.       else
  1596.         last_after_include->next = dirtmp;
  1597.       last_after_include = dirtmp; /* Tail follows the last one */
  1598.     }
  1599. #ifdef NEXT_OBJC_RUNTIME
  1600.     else if (argv[i][2] != 0)
  1601.       pend_files[i] = argv[i] + 2;
  1602.     else if (i + 1 == argc)
  1603.       fatal ("Filename missing after -i option");
  1604.     else
  1605.       pend_files[i] = argv[i+1], i++;
  1606. #endif
  1607.     break;
  1608.  
  1609.       case 'o':
  1610.     if (out_fname != NULL)
  1611.       fatal ("Output filename specified twice");
  1612.     if (i + 1 == argc)
  1613.       fatal ("Filename missing after -o option");
  1614.     out_fname = argv[++i];
  1615.     if (!strcmp (out_fname, "-"))
  1616.       out_fname = "";
  1617.     break;
  1618.  
  1619.       case 'p':
  1620.     if (!strcmp (argv[i], "-pedantic"))
  1621.       pedantic = 1;
  1622.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1623.       pedantic = 1;
  1624.       pedantic_errors = 1;
  1625.     } else if (!strcmp (argv[i], "-pcp")) {
  1626.       char *pcp_fname;
  1627.       if (i + 1 == argc)
  1628.         fatal ("Filename missing after -pcp option");
  1629.       pcp_fname = argv[++i];
  1630.       pcp_outfile = 
  1631.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1632.          ? fopen (pcp_fname, "w")
  1633.          : stdout);
  1634.       if (pcp_outfile == 0)
  1635.         pfatal_with_name (pcp_fname);
  1636.       no_precomp = 1;
  1637.     }
  1638.     break;
  1639.  
  1640.       case 't':
  1641.     if (!strcmp (argv[i], "-traditional")) {
  1642.       traditional = 1;
  1643.       cplusplus_comments = 0;
  1644.       if (dollars_in_ident > 0)
  1645.         dollars_in_ident = 1;
  1646.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1647.       no_trigraphs = 0;
  1648.     }
  1649.     break;
  1650.  
  1651. #if defined(NEXT_SEMANTICS) && !defined(NeXT_PDO)
  1652.       case 's':
  1653.     if (!strcmp (argv[i], "-smart"))
  1654.       ;
  1655.         else if (!strcmp (argv[i], "-segregate-output"))
  1656.           segregate_output = 1;
  1657.     break;
  1658.       case 'm':
  1659.     if (!strcmp (argv[i], "-mark-expansions"))
  1660.       mark_expansions = 1;
  1661.     break;
  1662. #endif
  1663.  
  1664.       case 'l':
  1665.     if (! strcmp (argv[i], "-lang-c"))
  1666.       cplusplus = 0, cplusplus_comments = 1, objc = 1;
  1667.     if (! strcmp (argv[i], "-lang-c89"))
  1668.       cplusplus = 0, cplusplus_comments = 0, objc = 0;
  1669.     if (! strcmp (argv[i], "-lang-c++"))
  1670.       cplusplus = 1, cplusplus_comments = 1, objc = 1;
  1671.     if (! strcmp (argv[i], "-lang-objc"))
  1672.       objc = 1, cplusplus = 0, cplusplus_comments = 1;
  1673.     if (! strcmp (argv[i], "-lang-objc++"))
  1674.       objc = 1, cplusplus = 1, cplusplus_comments = 1;
  1675.      if (! strcmp (argv[i], "-lang-asm"))
  1676.        lang_asm = 1;
  1677.      if (! strcmp (argv[i], "-lint"))
  1678.        for_lint = 1;
  1679.     break;
  1680.  
  1681.       case '+':
  1682.     cplusplus = 1, cplusplus_comments = 1;
  1683.     break;
  1684.  
  1685.       case 'w':
  1686.     inhibit_warnings = 1;
  1687.     break;
  1688.  
  1689.       case 'W':
  1690.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1691.       warn_trigraphs = 1;
  1692.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1693.       warn_trigraphs = 0;
  1694.     else if (!strcmp (argv[i], "-Wcomment"))
  1695.       warn_comments = 1;
  1696.     else if (!strcmp (argv[i], "-Wno-comment"))
  1697.       warn_comments = 0;
  1698.     else if (!strcmp (argv[i], "-Wcomments"))
  1699.       warn_comments = 1;
  1700.     else if (!strcmp (argv[i], "-Wno-comments"))
  1701.       warn_comments = 0;
  1702.     else if (!strcmp (argv[i], "-Wtraditional"))
  1703.       warn_stringify = 1;
  1704.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1705.       warn_stringify = 0;
  1706.     else if (!strcmp (argv[i], "-Wimport"))
  1707.       warn_import = 1;
  1708.     else if (!strcmp (argv[i], "-Wno-import"))
  1709.       warn_import = 0;
  1710.     else if (!strcmp (argv[i], "-Werror"))
  1711.       warnings_are_errors = 1;
  1712.     else if (!strcmp (argv[i], "-Wno-error"))
  1713.       warnings_are_errors = 0;
  1714. #if defined (NEXT_SEMANTICS) || defined (NEXT_PDO)
  1715.     else if (!strcmp (argv[i], "-Wmost"))
  1716.       {
  1717.         warn_trigraphs = 1;
  1718.         warn_comments = 0;
  1719.       }
  1720. #endif
  1721.     else if (!strcmp (argv[i], "-Wall"))
  1722.       {
  1723.         warn_trigraphs = 1;
  1724.         warn_comments = 1;
  1725.       }
  1726.     break;
  1727.  
  1728.       case 'M':
  1729.     /* The style of the choices here is a bit mixed.
  1730.        The chosen scheme is a hybrid of keeping all options in one string
  1731.        and specifying each option in a separate argument:
  1732.        -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
  1733.        -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
  1734.        -M[M][G][D file].  This is awkward to handle in specs, and is not
  1735.        as extensible.  */
  1736.     /* ??? -MG must be specified in addition to one of -M or -MM.
  1737.        This can be relaxed in the future without breaking anything.
  1738.        The converse isn't true.  */
  1739.  
  1740.     /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
  1741.     if (!strcmp (argv[i], "-MG"))
  1742.       {
  1743.         print_deps_missing_files = 1;
  1744.         break;
  1745.       }
  1746.     if (!strcmp (argv[i], "-M"))
  1747.       print_deps = 2;
  1748.     else if (!strcmp (argv[i], "-MM"))
  1749.       print_deps = 1;
  1750.     else if (!strcmp (argv[i], "-MD"))
  1751.       print_deps = 2;
  1752.     else if (!strcmp (argv[i], "-MMD"))
  1753.       print_deps = 1;
  1754.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1755.     if (!strcmp (argv[i], "-MD")
  1756.         || !strcmp (argv[i], "-MMD")) {
  1757.       if (i + 1 == argc)
  1758.         fatal ("Filename missing after %s option", argv[i]);
  1759.       i++;
  1760.       deps_file = argv[i];
  1761.       deps_mode = "w";
  1762.     } else {
  1763.       /* For -M and -MM, write deps on standard output
  1764.          and suppress the usual output.  */
  1765.       deps_stream = stdout;
  1766.       inhibit_output = 1;
  1767.     }      
  1768.     break;
  1769.  
  1770.       case 'd':
  1771.     {
  1772.       char *p = argv[i] + 2;
  1773.       char c;
  1774.       while ((c = *p++)) {
  1775.         /* Arg to -d specifies what parts of macros to dump */
  1776.         switch (c) {
  1777.         case 'M':
  1778.           dump_macros = dump_only;
  1779.           no_output = 1;
  1780.           break;
  1781.         case 'N':
  1782.           dump_macros = dump_names;
  1783.           break;
  1784.         case 'D':
  1785.           dump_macros = dump_definitions;
  1786.           break;
  1787.         }
  1788.       }
  1789.     }
  1790.     break;
  1791.  
  1792.       case 'g':
  1793.     if (argv[i][2] == '3')
  1794.       debug_output = 1;
  1795.     break;
  1796.  
  1797.       case 'v':
  1798.     fprintf (stderr, "GNU CPP version %s", version_string);
  1799. #ifdef TARGET_VERSION
  1800.     TARGET_VERSION;
  1801. #endif
  1802.     fprintf (stderr, "\n");
  1803.     verbose = 1;
  1804.     break;
  1805.  
  1806.       case 'H':
  1807.     print_include_names = 1;
  1808.     break;
  1809.  
  1810.       case 'D':
  1811.     if (argv[i][2] != 0)
  1812.       pend_defs[i] = argv[i] + 2;
  1813.     else if (i + 1 == argc)
  1814.       fatal ("Macro name missing after -D option");
  1815.     else
  1816.       i++, pend_defs[i] = argv[i];
  1817.     break;
  1818.  
  1819.       case 'A':
  1820.     {
  1821.       char *p;
  1822.  
  1823.       if (argv[i][2] != 0)
  1824.         p = argv[i] + 2;
  1825.       else if (i + 1 == argc)
  1826.         fatal ("Assertion missing after -A option");
  1827.       else
  1828.         p = argv[++i];
  1829.  
  1830.       if (!strcmp (p, "-")) {
  1831.         /* -A- eliminates all predefined macros and assertions.
  1832.            Let's include also any that were specified earlier
  1833.            on the command line.  That way we can get rid of any
  1834.            that were passed automatically in from GCC.  */
  1835.         int j;
  1836.         inhibit_predefs = 1;
  1837.         for (j = 0; j < i; j++)
  1838.           pend_defs[j] = pend_assertions[j] = 0;
  1839.       } else {
  1840.         pend_assertions[i] = p;
  1841.         pend_assertion_options[i] = "-A";
  1842.       }
  1843.     }
  1844.     break;
  1845.  
  1846.       case 'U':        /* JF #undef something */
  1847.     if (argv[i][2] != 0)
  1848.       pend_undefs[i] = argv[i] + 2;
  1849.     else if (i + 1 == argc)
  1850.       fatal ("Macro name missing after -U option");
  1851.     else
  1852.       pend_undefs[i] = argv[i+1], i++;
  1853.     break;
  1854.  
  1855.       case 'C':
  1856.     put_out_comments = 1;
  1857.     break;
  1858.  
  1859.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1860.     break;
  1861.  
  1862.       case 'P':
  1863.     no_line_directives = 1;
  1864.     break;
  1865.  
  1866.       case '$':            /* Don't include $ in identifiers.  */
  1867.     dollars_in_ident = 0;
  1868.     break;
  1869.  
  1870.       case 'I':            /* Add directory to path for includes.  */
  1871.     {
  1872.       struct file_name_list *dirtmp;
  1873.  
  1874.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1875.         ignore_srcdir = 1;
  1876.         /* Don't use any preceding -I directories for #include <...>.  */
  1877.         first_bracket_include = 0;
  1878.       }
  1879.       else {
  1880.         dirtmp = (struct file_name_list *)
  1881.           xmalloc (sizeof (struct file_name_list));
  1882.         dirtmp->next = 0;        /* New one goes on the end */
  1883.         dirtmp->control_macro = 0;
  1884.         dirtmp->c_system_include_path = 0;
  1885.         if (argv[i][2] != 0)
  1886.           dirtmp->fname = argv[i] + 2;
  1887.         else if (i + 1 == argc)
  1888.           fatal ("Directory name missing after -I option");
  1889.         else
  1890.           dirtmp->fname = argv[++i];
  1891.         dirtmp->got_name_map = 0;
  1892.         append_include_chain (dirtmp, dirtmp);
  1893.       }
  1894.     }
  1895.     break;
  1896.  
  1897. #ifdef NEXT_FRAMEWORKS
  1898.   case 'F':
  1899.   {
  1900.       struct file_name_list *dirtmp;
  1901.  
  1902.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1903.           ignore_srcdir = 1;
  1904.           /* Don't use any preceding -F directories for #include <...>.  */
  1905.           first_bracket_framework = 0;
  1906.       }
  1907.       else {
  1908.           dirtmp = (struct file_name_list *)
  1909.               xmalloc (sizeof (struct file_name_list));
  1910.           dirtmp->next = 0;        /* New one goes on the end */
  1911.           dirtmp->control_macro = 0;
  1912.           dirtmp->c_system_include_path = 0;
  1913.           if (argv[i][2] != 0)
  1914.               dirtmp->fname = argv[i] + 2;
  1915.           else if (i + 1 == argc)
  1916.               fatal ("Directory name missing after -I option");
  1917.           else
  1918.               dirtmp->fname = argv[++i];
  1919.           dirtmp->got_name_map = 0;
  1920.           append_framework_chain (dirtmp, dirtmp);
  1921.       }
  1922.   }
  1923.     break;
  1924. #endif
  1925.  
  1926.       case 'n':
  1927.     if (!strcmp (argv[i], "-nostdinc"))
  1928.       /* -nostdinc causes no default include directories.
  1929.          You must specify all include-file directories with -I.  */
  1930.       no_standard_includes = 1;
  1931.     else if (!strcmp (argv[i], "-nostdinc++"))
  1932.       /* -nostdinc++ causes no default C++-specific include directories. */
  1933.       no_standard_cplusplus_includes = 1;
  1934.     else if (!strcmp (argv[i], "-noprecomp"))
  1935.       no_precomp = 1;
  1936.     break;
  1937.  
  1938.       case 'u':
  1939.     /* Sun compiler passes undocumented switch "-undef".
  1940.        Let's assume it means to inhibit the predefined symbols.  */
  1941.     inhibit_predefs = 1;
  1942.     break;
  1943.  
  1944.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  1945.     if (in_fname == NULL) {
  1946.       in_fname = "";
  1947.       break;
  1948.     } else if (out_fname == NULL) {
  1949.       out_fname = "";
  1950.       break;
  1951.     }    /* else fall through into error */
  1952.  
  1953.       default:
  1954.     fatal ("Invalid option `%s'", argv[i]);
  1955.       }
  1956.     }
  1957.   }
  1958.  
  1959.   /* Add dirs from CPATH after dirs from -I.  */
  1960.   /* There seems to be confusion about what CPATH should do,
  1961.      so for the moment it is not documented.  */
  1962.   /* Some people say that CPATH should replace the standard include dirs,
  1963.      but that seems pointless: it comes before them, so it overrides them
  1964.      anyway.  */
  1965.   cp = getenv ("CPATH");
  1966.   if (cp && ! no_standard_includes)
  1967.     path_include (cp);
  1968.  
  1969.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  1970.   initialize_char_syntax ();
  1971.  
  1972.   /* Initialize output buffer */
  1973.  
  1974.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  1975.   outbuf.bufp = outbuf.buf;
  1976.   outbuf.length = OUTBUF_SIZE;
  1977.  
  1978.   /* Do partial setup of input buffer for the sake of generating
  1979.      early #line directives (when -g is in effect).  */
  1980.  
  1981.   fp = &instack[++indepth];
  1982.   if (in_fname == NULL)
  1983.     in_fname = "";
  1984.   fp->nominal_fname = fp->fname = in_fname;
  1985.   fp->lineno = 0;
  1986.  
  1987.   /* In C++, wchar_t is a distinct basic type, and we can expect
  1988.      __wchar_t to be defined by cc1plus.  */
  1989.   if (cplusplus)
  1990.     wchar_type = "__wchar_t";
  1991.  
  1992.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  1993.      and option processing.  */
  1994.   initialize_builtins (fp, &outbuf);
  1995.  
  1996.   /* Do standard #defines and assertions
  1997.      that identify system and machine type.  */
  1998.  
  1999.   if (!inhibit_predefs) {
  2000.     char *p = (char *) alloca (strlen (predefs) + 1);
  2001.     strcpy (p, predefs);
  2002.     while (*p) {
  2003.       char *q;
  2004.       while (*p == ' ' || *p == '\t')
  2005.     p++;
  2006.       /* Handle -D options.  */ 
  2007.       if (p[0] == '-' && p[1] == 'D') {
  2008.     q = &p[2];
  2009.     while (*p && *p != ' ' && *p != '\t')
  2010.       p++;
  2011.     if (*p != 0)
  2012.       *p++= 0;
  2013.     if (debug_output)
  2014.       output_line_directive (fp, &outbuf, 0, same_file);
  2015.     make_definition (q, &outbuf);
  2016.     while (*p == ' ' || *p == '\t')
  2017.       p++;
  2018.       } else if (p[0] == '-' && p[1] == 'A') {
  2019.     /* Handle -A options (assertions).  */ 
  2020.     char *assertion;
  2021.     char *past_name;
  2022.     char *value;
  2023.     char *past_value;
  2024.     char *termination;
  2025.     int save_char;
  2026.  
  2027.     assertion = &p[2];
  2028.     past_name = assertion;
  2029.     /* Locate end of name.  */
  2030.     while (*past_name && *past_name != ' '
  2031.            && *past_name != '\t' && *past_name != '(')
  2032.       past_name++;
  2033.     /* Locate `(' at start of value.  */
  2034.     value = past_name;
  2035.     while (*value && (*value == ' ' || *value == '\t'))
  2036.       value++;
  2037.     if (*value++ != '(')
  2038.       abort ();
  2039.     while (*value && (*value == ' ' || *value == '\t'))
  2040.       value++;
  2041.     past_value = value;
  2042.     /* Locate end of value.  */
  2043.     while (*past_value && *past_value != ' '
  2044.            && *past_value != '\t' && *past_value != ')')
  2045.       past_value++;
  2046.     termination = past_value;
  2047.     while (*termination && (*termination == ' ' || *termination == '\t'))
  2048.       termination++;
  2049.     if (*termination++ != ')')
  2050.       abort ();
  2051.     if (*termination && *termination != ' ' && *termination != '\t')
  2052.       abort ();
  2053.     /* Temporarily null-terminate the value.  */
  2054.     save_char = *termination;
  2055.     *termination = '\0';
  2056.     /* Install the assertion.  */
  2057.     make_assertion ("-A", assertion);
  2058.     *termination = (char) save_char;
  2059.     p = termination;
  2060.     while (*p == ' ' || *p == '\t')
  2061.       p++;
  2062.       } else {
  2063.     abort ();
  2064.       }
  2065.     }
  2066.   }
  2067.  
  2068.   /* Now handle the command line options.  */
  2069.  
  2070.   /* Do -U's, -D's and -A's in the order they were seen.  */
  2071.   for (i = 1; i < argc; i++) {
  2072.     if (pend_undefs[i]) {
  2073.       if (debug_output)
  2074.         output_line_directive (fp, &outbuf, 0, same_file);
  2075.       make_undef (pend_undefs[i], &outbuf);
  2076.     }
  2077.     if (pend_defs[i]) {
  2078.       if (debug_output)
  2079.         output_line_directive (fp, &outbuf, 0, same_file);
  2080.       make_definition (pend_defs[i], &outbuf);
  2081.     }
  2082.     if (pend_assertions[i])
  2083.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  2084.   }
  2085.  
  2086.   done_initializing = 1;
  2087.  
  2088.   { /* read the appropriate environment variable and if it exists
  2089.        replace include_defaults with the listed path. */
  2090.     char *epath = 0;
  2091.     switch ((objc << 1) + cplusplus)
  2092.       {
  2093.       case 0:
  2094.     epath = getenv ("C_INCLUDE_PATH");
  2095.     break;
  2096.       case 1:
  2097.     epath = getenv ("CPLUS_INCLUDE_PATH");
  2098.     break;
  2099.       case 2:
  2100.     epath = getenv ("OBJC_INCLUDE_PATH");
  2101.     break;
  2102.       case 3:
  2103.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  2104.     break;
  2105.       }
  2106.     /* If the environment var for this language is set,
  2107.        add to the default list of include directories.  */
  2108.     if (epath) {
  2109.       char *nstore = (char *) alloca (strlen (epath) + 2);
  2110.       int num_dirs;
  2111.       char *startp, *endp;
  2112.  
  2113.       for (num_dirs = 1, startp = epath; *startp; startp++)
  2114.     if (*startp == PATH_SEPARATOR)
  2115.       num_dirs++;
  2116.       include_defaults
  2117.     = (struct default_include *) xmalloc ((num_dirs
  2118.                            * sizeof (struct default_include))
  2119.                           + sizeof (include_defaults_array));
  2120.       startp = endp = epath;
  2121.       num_dirs = 0;
  2122.       while (1) {
  2123.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  2124.         if ((*endp == PATH_SEPARATOR
  2125. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  2126. #ifdef __MSDOS__
  2127.          && (endp-startp != 1 || !isalpha (*startp))
  2128. #endif
  2129. #endif
  2130.          )
  2131.             || *endp == 0) {
  2132.       strncpy (nstore, startp, endp-startp);
  2133.       if (endp == startp)
  2134.         strcpy (nstore, ".");
  2135.       else
  2136.         nstore[endp-startp] = '\0';
  2137.  
  2138.       include_defaults[num_dirs].fname = savestring (nstore);
  2139.       include_defaults[num_dirs].cplusplus = cplusplus;
  2140.       include_defaults[num_dirs].cxx_aware = 1;
  2141.       num_dirs++;
  2142.       if (*endp == '\0')
  2143.         break;
  2144.       endp = startp = endp + 1;
  2145.     } else
  2146.       endp++;
  2147.       }
  2148.       /* Put the usual defaults back in at the end.  */
  2149.       bcopy ((char *) include_defaults_array,
  2150.          (char *) &include_defaults[num_dirs],
  2151.          sizeof (include_defaults_array));
  2152.     }
  2153.   }
  2154.  
  2155. #ifdef WIN32
  2156. {
  2157.     struct default_include *fix_includes = include_defaults;
  2158.     char* next_root = getenv( "NEXT_ROOT" );
  2159.     int i = 0;
  2160.     
  2161.     if (next_root && next_root[strlen(next_root)-1] == '/')
  2162.         next_root[strlen(next_root)-1] = '\0';
  2163.     if( next_root && *next_root )
  2164.     {
  2165.         while( fix_includes[i].fname )
  2166.         {
  2167.             char* new_fname = (char*)xmalloc(strlen(next_root) + strlen(fix_includes[i].fname) + 1);
  2168.             sprintf (new_fname, "%s%s", next_root, fix_includes[i].fname);
  2169.  
  2170.             if (new_fname && *new_fname) 
  2171.                 fix_includes[i].fname = new_fname;
  2172.             i++;
  2173.         }
  2174.     }
  2175. }
  2176.  
  2177. #endif
  2178.  
  2179.   append_include_chain (before_system, last_before_system);
  2180.   first_system_include = before_system;
  2181.  
  2182.   /* Unless -fnostdinc,
  2183.      tack on the standard include file dirs to the specified list */
  2184.   if (!no_standard_includes) {
  2185.     int hdr;
  2186.     struct default_include *p = include_defaults;
  2187.     char *specd_prefix = include_prefix;
  2188.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  2189.     int default_len = 0;
  2190.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  2191.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  2192.       default_len = strlen (default_prefix) - 7;
  2193.       default_prefix[default_len] = 0;
  2194.     }
  2195.     /* Search "translated" versions of GNU directories.
  2196.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  2197.     if (specd_prefix != 0 && default_len != 0)
  2198.       for (p = include_defaults; p->fname; p++) {
  2199.     /* Some standard dirs are only for C++.  */
  2200.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2201.       /* Does this dir start with the prefix?  */
  2202.       if (!strncmp (p->fname, default_prefix, default_len)) {
  2203.         /* Yes; change prefix and add to search list.  */
  2204.         struct file_name_list *new
  2205.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2206.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  2207.         char *str = xmalloc (this_len + 1);
  2208.         strcpy (str, specd_prefix);
  2209.         strcat (str, p->fname + default_len);
  2210.         new->fname = str;
  2211.         new->control_macro = 0;
  2212.         new->c_system_include_path = !p->cxx_aware;
  2213.         new->got_name_map = 0;
  2214.         append_include_chain (new, new);
  2215.         if (first_system_include == 0)
  2216.           first_system_include = new;
  2217.       }
  2218.     }
  2219.       }
  2220.     /* Search ordinary names for GNU include directories.  */
  2221.     for (p = include_defaults; p->fname; p++) {
  2222.       /* Some standard dirs are only for C++.  */
  2223.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2224.     struct file_name_list *new
  2225.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2226.     new->control_macro = 0;
  2227.     new->c_system_include_path = !p->cxx_aware;
  2228.     new->fname = p->fname;
  2229.     new->got_name_map = 0;
  2230.     append_include_chain (new, new);
  2231.     if (first_system_include == 0)
  2232.       first_system_include = new;
  2233.       }
  2234.     }
  2235. #ifdef NEXT_FRAMEWORKS
  2236.     for (hdr=0; framework_defaults_array[hdr].fname; hdr++) {
  2237.        struct stat dummy;
  2238.  
  2239. #ifdef WIN32
  2240.     {
  2241.         char* next_root = getenv( "NEXT_ROOT" );
  2242.         char* new_name = (char*)xmalloc(strlen(next_root) + strlen(framework_defaults_array[hdr].fname) + 1);
  2243.  
  2244.         if (next_root && next_root[strlen(next_root)-1] == '/')
  2245.             next_root[strlen(next_root)-1] = '\0';
  2246.         sprintf (new_name, "%s%s", next_root, framework_defaults_array[hdr].fname);
  2247.  
  2248.         framework_defaults_array[hdr].fname = new_name;
  2249.     }
  2250. #endif /* WIN32 */
  2251.  
  2252.        if (stat(framework_defaults_array[hdr].fname, &dummy) == 0) {
  2253.           /*  Frmaework directory actually exists */
  2254.           struct file_name_list *new
  2255.              = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2256.           new->control_macro = 0;
  2257.           new->c_system_include_path = !framework_defaults_array[hdr].cxx_aware;
  2258.           new->fname = framework_defaults_array[hdr].fname;
  2259.           new->got_name_map = 0;
  2260.           append_framework_chain (new, new);
  2261.        }
  2262.     }
  2263.     free (default_prefix);
  2264. #endif
  2265.   }
  2266.  
  2267.   /* Tack the after_include chain at the end of the include chain.  */
  2268.   append_include_chain (after_include, last_after_include);
  2269.   if (first_system_include == 0)
  2270.     first_system_include = after_include;
  2271.  
  2272.   /* With -v, print the list of dirs to search.  */
  2273.   if (verbose) {
  2274.     struct file_name_list *p;
  2275.     fprintf (stderr, "#include \"...\" search starts here:\n");
  2276.     for (p = include; p; p = p->next) {
  2277.       if (p == first_bracket_include)
  2278.     fprintf (stderr, "#include <...> search starts here:\n");
  2279.       fprintf (stderr, " %s\n", p->fname);
  2280.     }
  2281.     fprintf (stderr, "End of search list.\n");
  2282.   }
  2283.  
  2284.   /* Scan the -imacros files before the main input.
  2285.      Much like #including them, but with no_output set
  2286.      so that only their macro definitions matter.  */
  2287.  
  2288.   no_output++; no_record_file++;
  2289.   for (i = 1; i < argc; i++)
  2290.     if (pend_files[i]) {
  2291.       int fd = open (pend_files[i], O_RDONLY, 0666);
  2292.       if (fd < 0) {
  2293.     perror_with_name (pend_files[i]);
  2294.     return FATAL_EXIT_CODE;
  2295.       }
  2296.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  2297.     }
  2298.   no_output--; no_record_file--;
  2299.  
  2300.   /* Copy the entire contents of the main input file into
  2301.      the stacked input buffer previously allocated for it.  */
  2302.  
  2303.   /* JF check for stdin */
  2304.   if (in_fname == NULL || *in_fname == 0) {
  2305.     in_fname = "";
  2306.     f = 0;
  2307.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  2308.     goto perror;
  2309.  
  2310.   /* -MG doesn't select the form of output and must be specified with one of
  2311.      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
  2312.      inhibit compilation.  */
  2313.   if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
  2314.     fatal ("-MG must be specified with one of -M or -MM");
  2315.  
  2316.   /* Either of two environment variables can specify output of deps.
  2317.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  2318.      where OUTPUT_FILE is the file to write deps info to
  2319.      and DEPS_TARGET is the target to mention in the deps.  */
  2320.  
  2321.   if (print_deps == 0
  2322.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  2323.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  2324.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  2325.     char *s;
  2326.     char *output_file;
  2327.  
  2328.     if (spec == 0) {
  2329.       spec = getenv ("SUNPRO_DEPENDENCIES");
  2330.       print_deps = 2;
  2331.     }
  2332.     else
  2333.       print_deps = 1;
  2334.  
  2335.     s = spec;
  2336.     /* Find the space before the DEPS_TARGET, if there is one.  */
  2337.     /* This should use index.  (mrs) */
  2338.     while (*s != 0 && *s != ' ') s++;
  2339.     if (*s != 0) {
  2340.       deps_target = s + 1;
  2341.       output_file = xmalloc (s - spec + 1);
  2342.       bcopy (spec, output_file, s - spec);
  2343.       output_file[s - spec] = 0;
  2344.     }
  2345.     else {
  2346.       deps_target = 0;
  2347.       output_file = spec;
  2348.     }
  2349.       
  2350.     deps_file = output_file;
  2351.     deps_mode = "a";
  2352.   }
  2353.  
  2354.   /* For -M, print the expected object file name
  2355.      as the target of this Make-rule.  */
  2356.   if (print_deps) {
  2357.     deps_allocated_size = 200;
  2358.     deps_buffer = xmalloc (deps_allocated_size);
  2359.     deps_buffer[0] = 0;
  2360.     deps_size = 0;
  2361.     deps_column = 0;
  2362.  
  2363.     if (deps_target) {
  2364.       deps_output (deps_target, ':');
  2365.     } else if (*in_fname == 0) {
  2366.       deps_output ("-", ':');
  2367.     } else {
  2368.       char *p, *q;
  2369.       int len;
  2370.  
  2371.       /* Discard all directory prefixes from filename.  */
  2372.       if ((q = rindex (in_fname, '/')) != NULL
  2373. #ifdef DIR_SEPARATOR
  2374.       && (q = rindex (in_fname, DIR_SEPARATOR)) != NULL
  2375. #endif
  2376.       )
  2377.     ++q;
  2378.       else
  2379.     q = in_fname;
  2380.  
  2381.       /* Copy remainder to mungable area.  */
  2382.       p = (char *) alloca (strlen(q) + 8);
  2383.       strcpy (p, q);
  2384.  
  2385.       /* Output P, but remove known suffixes.  */
  2386.       len = strlen (p);
  2387.       q = p + len;
  2388.       if (len >= 2
  2389.       && p[len - 2] == '.'
  2390.       && index("cCsSmM", p[len - 1]))
  2391.     q = p + (len - 2);
  2392.       else if (len >= 3
  2393.            && p[len - 3] == '.'
  2394.            && p[len - 2] == 'c'
  2395.            && p[len - 1] == 'c')
  2396.     q = p + (len - 3);
  2397.       else if (len >= 4
  2398.            && p[len - 4] == '.'
  2399.            && p[len - 3] == 'c'
  2400.            && p[len - 2] == 'x'
  2401.            && p[len - 1] == 'x')
  2402.     q = p + (len - 4);
  2403.       else if (len >= 4
  2404.            && p[len - 4] == '.'
  2405.            && p[len - 3] == 'c'
  2406.            && p[len - 2] == 'p'
  2407.            && p[len - 1] == 'p')
  2408.     q = p + (len - 4);
  2409.       else if (len >= 3
  2410.            && p[len - 3] == '.'
  2411.            && p[len - 2] == 'm'
  2412.            && p[len - 1] == 'm')
  2413.     q = p + (len - 3);
  2414.  
  2415.       /* Supply our own suffix.  */
  2416. #ifndef VMS
  2417.       strcpy (q, ".o");
  2418. #else
  2419.       strcpy (q, ".obj");
  2420. #endif
  2421.  
  2422.       deps_output (p, ':');
  2423.       deps_output (in_fname, ' ');
  2424.     }
  2425.   }
  2426.  
  2427.   file_size_and_mode (f, &st_mode, &st_size);
  2428.   fp->nominal_fname = fp->fname = in_fname;
  2429.   fp->lineno = 1;
  2430.   fp->system_header_p = 0;
  2431.   /* JF all this is mine about reading pipes and ttys */
  2432.   if (! S_ISREG (st_mode)) {
  2433.     /* Read input from a file that is not a normal disk file.
  2434.        We cannot preallocate a buffer with the correct size,
  2435.        so we must read in the file a piece at the time and make it bigger.  */
  2436.     int size;
  2437.     int bsize;
  2438.     int cnt;
  2439.  
  2440.     bsize = 2000;
  2441.     size = 0;
  2442.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  2443.     for (;;) {
  2444.       cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
  2445.       if (cnt < 0) goto perror;    /* error! */
  2446.       size += cnt;
  2447.       if (size != bsize) break;    /* End of file */
  2448.       bsize *= 2;
  2449.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  2450.     }
  2451.     fp->length = size;
  2452.   } else {
  2453.     /* Read a file whose size we can determine in advance.
  2454.        For the sake of VMS, st_size is just an upper bound.  */
  2455.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  2456.     fp->length = safe_read (f, (char *) fp->buf, st_size);
  2457.     if (fp->length < 0) goto perror;
  2458.   }
  2459.   fp->bufp = fp->buf;
  2460.   fp->if_stack = if_stack;
  2461.  
  2462.   /* Make sure data ends with a newline.  And put a null after it.  */
  2463.  
  2464.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  2465.       /* Backslash-newline at end is not good enough.  */
  2466.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  2467.     fp->buf[fp->length++] = '\n';
  2468.     missing_newline = 1;
  2469.   }
  2470.   fp->buf[fp->length] = '\0';
  2471.  
  2472.   /* Unless inhibited, convert trigraphs in the input.  */
  2473.  
  2474.   if (!no_trigraphs)
  2475.     trigraph_pcp (fp);
  2476.  
  2477. #if defined (NEXT_SEMANTICS) || defined(NEXT_PDO)
  2478.   if (!no_rtf)
  2479.     buf_convert_rtf (fp);
  2480. #endif
  2481.  
  2482.   /* Now that we know the input file is valid, open the output.  */
  2483.  
  2484.   if (!out_fname || !strcmp (out_fname, ""))
  2485.     out_fname = "stdout";
  2486.   else if (! freopen (out_fname, "w", stdout))
  2487.     pfatal_with_name (out_fname);
  2488.  
  2489.   output_line_directive (fp, &outbuf, 0, same_file);
  2490.  
  2491.   /* Scan the -include files before the main input.  */
  2492.  
  2493.   no_record_file++;
  2494.   for (i = 1; i < argc; i++)
  2495.     if (pend_includes[i]) {
  2496.       int fd = open (pend_includes[i], O_RDONLY, 0666);
  2497.       if (fd < 0) {
  2498.     perror_with_name (pend_includes[i]);
  2499.     return FATAL_EXIT_CODE;
  2500.       }
  2501.       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
  2502.     }
  2503.   no_record_file--;
  2504.  
  2505.   /* Scan the input, processing macros and directives.  */
  2506.  
  2507.   rescan (&outbuf, 0);
  2508.  
  2509.   if (missing_newline)
  2510.     fp->lineno--;
  2511.  
  2512.   if (pedantic && missing_newline)
  2513.     pedwarn ("file does not end in newline");
  2514.  
  2515.   /* Now we have processed the entire input
  2516.      Write whichever kind of output has been requested.  */
  2517.  
  2518.   if (dump_macros == dump_only)
  2519.     dump_all_macros ();
  2520.   else if (! inhibit_output) {
  2521.     write_output ();
  2522.   }
  2523.  
  2524.   if (print_deps) {
  2525.     /* Don't actually write the deps file if compilation has failed.  */
  2526.     if (errors == 0) {
  2527.       if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
  2528.     pfatal_with_name (deps_file);
  2529.       fputs (deps_buffer, deps_stream);
  2530.       putc ('\n', deps_stream);
  2531.       if (deps_file) {
  2532.     if (ferror (deps_stream) || fclose (deps_stream) != 0)
  2533.       fatal ("I/O error on output");
  2534.       }
  2535.     }
  2536.   }
  2537.  
  2538. #ifdef NEXT_FRAMEWORKS
  2539.     /* Clear up old -F paths, which also could vary by cpp request */
  2540.     include_ptr = framework;
  2541.     while (include_ptr) {
  2542.        struct file_name_list * next_include = include_ptr->next;
  2543.        free (include_ptr);
  2544.        include_ptr = next_include;
  2545.     }
  2546.     first_bracket_framework = 0;
  2547.     framework = 0;    
  2548.     last_framework = 0;
  2549. #endif
  2550.  
  2551.   if (pcp_outfile && pcp_outfile != stdout
  2552.       && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
  2553.     fatal ("I/O error on `-pcp' output");
  2554.  
  2555.   if (ferror (stdout) || fclose (stdout) != 0)
  2556.     fatal ("I/O error on output");
  2557.  
  2558.   if (errors)
  2559.     exit (FATAL_EXIT_CODE);
  2560.   exit (SUCCESS_EXIT_CODE);
  2561.  
  2562.  perror:
  2563.   pfatal_with_name (in_fname);
  2564.   return 0;
  2565. }
  2566.  
  2567. /* Given a colon-separated list of file names PATH,
  2568.    add all the names to the search path for include files.  */
  2569.  
  2570. static void
  2571. path_include (path)
  2572.      char *path;
  2573. {
  2574.   char *p;
  2575.  
  2576.   p = path;
  2577.  
  2578.   if (*p)
  2579.     while (1) {
  2580.       char *q = p;
  2581.       char *name;
  2582.       struct file_name_list *dirtmp;
  2583.  
  2584.       /* Find the end of this name.  */
  2585.       while (*q != 0 && *q != PATH_SEPARATOR) q++;
  2586.       if (p == q) {
  2587.     /* An empty name in the path stands for the current directory.  */
  2588.     name = xmalloc (2);
  2589.     name[0] = '.';
  2590.     name[1] = 0;
  2591.       } else {
  2592.     /* Otherwise use the directory that is named.  */
  2593.     name = xmalloc (q - p + 1);
  2594.     bcopy (p, name, q - p);
  2595.     name[q - p] = 0;
  2596.       }
  2597.  
  2598.       dirtmp = (struct file_name_list *)
  2599.     xmalloc (sizeof (struct file_name_list));
  2600.       dirtmp->next = 0;        /* New one goes on the end */
  2601.       dirtmp->control_macro = 0;
  2602.       dirtmp->c_system_include_path = 0;
  2603.       dirtmp->fname = name;
  2604.       dirtmp->got_name_map = 0;
  2605.       append_include_chain (dirtmp, dirtmp);
  2606.  
  2607.       /* Advance past this name.  */
  2608.       p = q;
  2609.       if (*p == 0)
  2610.     break;
  2611.       /* Skip the colon.  */
  2612.       p++;
  2613.     }
  2614. }
  2615.  
  2616. /* Return the address of the first character in S that equals C.
  2617.    S is an array of length N, possibly containing '\0's, and followed by '\0'.
  2618.    Return 0 if there is no such character.  Assume that C itself is not '\0'.
  2619.    If we knew we could use memchr, we could just invoke memchr (S, C, N),
  2620.    but unfortunately memchr isn't autoconfigured yet.  */
  2621.  
  2622. static U_CHAR *
  2623. index0 (s, c, n)
  2624.      U_CHAR *s;
  2625.      int c;
  2626.      size_t n;
  2627. {
  2628.   char *p = (char *) s;
  2629.   for (;;) {
  2630.     char *q = index (p, c);
  2631.     if (q)
  2632.       return (U_CHAR *) q;
  2633.     else {
  2634.       size_t l = strlen (p);
  2635.       if (l == n)
  2636.     return 0;
  2637.       l++;
  2638.       p += l;
  2639.       n -= l;
  2640.     }
  2641.   }
  2642. }
  2643.  
  2644. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  2645.    before main CCCP processing.  Name `pcp' is also in honor of the
  2646.    drugs the trigraph designers must have been on.
  2647.  
  2648.    Using an extra pass through the buffer takes a little extra time,
  2649.    but is infinitely less hairy than trying to handle trigraphs inside
  2650.    strings, etc. everywhere, and also makes sure that trigraphs are
  2651.    only translated in the top level of processing. */
  2652.  
  2653. static void
  2654. trigraph_pcp (buf)
  2655.      FILE_BUF *buf;
  2656. {
  2657.   register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
  2658.   int len;
  2659.  
  2660.   fptr = bptr = sptr = buf->buf;
  2661.   lptr = fptr + buf->length;
  2662.   while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
  2663.     if (*++sptr != '?')
  2664.       continue;
  2665.     switch (*++sptr) {
  2666.       case '=':
  2667.       c = '#';
  2668.       break;
  2669.     case '(':
  2670.       c = '[';
  2671.       break;
  2672.     case '/':
  2673.       c = '\\';
  2674.       break;
  2675.     case ')':
  2676.       c = ']';
  2677.       break;
  2678.     case '\'':
  2679.       c = '^';
  2680.       break;
  2681.     case '<':
  2682.       c = '{';
  2683.       break;
  2684.     case '!':
  2685.       c = '|';
  2686.       break;
  2687.     case '>':
  2688.       c = '}';
  2689.       break;
  2690.     case '-':
  2691.       c  = '~';
  2692.       break;
  2693.     case '?':
  2694.       sptr--;
  2695.       continue;
  2696.     default:
  2697.       continue;
  2698.     }
  2699.     len = sptr - fptr - 2;
  2700.  
  2701.     /* BSD doc says bcopy () works right for overlapping strings.  In ANSI
  2702.        C, this will be memmove (). */
  2703.     if (bptr != fptr && len > 0)
  2704.       bcopy ((char *) fptr, (char *) bptr, len);
  2705.  
  2706.     bptr += len;
  2707.     *bptr++ = c;
  2708.     fptr = ++sptr;
  2709.   }
  2710.   len = buf->length - (fptr - buf->buf);
  2711.   if (bptr != fptr && len > 0)
  2712.     bcopy ((char *) fptr, (char *) bptr, len);
  2713.   buf->length -= fptr - bptr;
  2714.   buf->buf[buf->length] = '\0';
  2715.   if (warn_trigraphs && fptr != bptr)
  2716.     warning_with_line (0, "%d trigraph(s) encountered", (fptr - bptr) / 2);
  2717. }
  2718.  
  2719. /* Move all backslash-newline pairs out of embarrassing places.
  2720.    Exchange all such pairs following BP
  2721.    with any potentially-embarrassing characters that follow them.
  2722.    Potentially-embarrassing characters are / and *
  2723.    (because a backslash-newline inside a comment delimiter
  2724.    would cause it not to be recognized).  */
  2725.  
  2726. static void
  2727. newline_fix (bp)
  2728.      U_CHAR *bp;
  2729. {
  2730.   register U_CHAR *p = bp;
  2731.  
  2732.   /* First count the backslash-newline pairs here.  */
  2733.  
  2734.   while (p[0] == '\\' && p[1] == '\n')
  2735.     p += 2;
  2736.  
  2737.   /* What follows the backslash-newlines is not embarrassing.  */
  2738.  
  2739.   if (*p != '/' && *p != '*')
  2740.     return;
  2741.  
  2742.   /* Copy all potentially embarrassing characters
  2743.      that follow the backslash-newline pairs
  2744.      down to where the pairs originally started.  */
  2745.  
  2746.   while (*p == '*' || *p == '/')
  2747.     *bp++ = *p++;
  2748.  
  2749.   /* Now write the same number of pairs after the embarrassing chars.  */
  2750.   while (bp < p) {
  2751.     *bp++ = '\\';
  2752.     *bp++ = '\n';
  2753.   }
  2754. }
  2755.  
  2756. /* Like newline_fix but for use within a directive-name.
  2757.    Move any backslash-newlines up past any following symbol constituents.  */
  2758.  
  2759. static void
  2760. name_newline_fix (bp)
  2761.      U_CHAR *bp;
  2762. {
  2763.   register U_CHAR *p = bp;
  2764.  
  2765.   /* First count the backslash-newline pairs here.  */
  2766.   while (p[0] == '\\' && p[1] == '\n')
  2767.     p += 2;
  2768.  
  2769.   /* What follows the backslash-newlines is not embarrassing.  */
  2770.  
  2771.   if (!is_idchar[*p])
  2772.     return;
  2773.  
  2774.   /* Copy all potentially embarrassing characters
  2775.      that follow the backslash-newline pairs
  2776.      down to where the pairs originally started.  */
  2777.  
  2778.   while (is_idchar[*p])
  2779.     *bp++ = *p++;
  2780.  
  2781.   /* Now write the same number of pairs after the embarrassing chars.  */
  2782.   while (bp < p) {
  2783.     *bp++ = '\\';
  2784.     *bp++ = '\n';
  2785.   }
  2786. }
  2787.  
  2788. /* Look for lint commands in comments.
  2789.  
  2790.    When we come in here, ibp points into a comment.  Limit is as one expects.
  2791.    scan within the comment -- it should start, after lwsp, with a lint command.
  2792.    If so that command is returned as a (constant) string.
  2793.  
  2794.    Upon return, any arg will be pointed to with argstart and will be
  2795.    arglen long.  Note that we don't parse that arg since it will just
  2796.    be printed out again.
  2797. */
  2798.  
  2799. static char *
  2800. get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
  2801.      register U_CHAR *ibp;
  2802.      register U_CHAR *limit;
  2803.      U_CHAR **argstart;        /* point to command arg */
  2804.      int *arglen, *cmdlen;    /* how long they are */
  2805. {
  2806.   long linsize;
  2807.   register U_CHAR *numptr;    /* temp for arg parsing */
  2808.  
  2809.   *arglen = 0;
  2810.  
  2811.   SKIP_WHITE_SPACE (ibp);
  2812.  
  2813.   if (ibp >= limit) return NULL;
  2814.  
  2815.   linsize = limit - ibp;
  2816.   
  2817.   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
  2818.   if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
  2819.     *cmdlen = 10;
  2820.     return "NOTREACHED";
  2821.   }
  2822.   if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
  2823.     *cmdlen = 8;
  2824.     return "ARGSUSED";
  2825.   }
  2826.   if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
  2827.     *cmdlen = 11;
  2828.     return "LINTLIBRARY";
  2829.   }
  2830.   if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
  2831.     *cmdlen = 7;
  2832.     ibp += 7; linsize -= 7;
  2833.     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
  2834.  
  2835.     /* OK, read a number */
  2836.     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
  2837.      numptr++);
  2838.     *arglen = numptr - *argstart;
  2839.     return "VARARGS";
  2840.   }
  2841.   return NULL;
  2842. }
  2843.  
  2844. /*
  2845.  * The main loop of the program.
  2846.  *
  2847.  * Read characters from the input stack, transferring them to the
  2848.  * output buffer OP.
  2849.  *
  2850.  * Macros are expanded and push levels on the input stack.
  2851.  * At the end of such a level it is popped off and we keep reading.
  2852.  * At the end of any other kind of level, we return.
  2853.  * #-directives are handled, except within macros.
  2854.  *
  2855.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  2856.  * and insert them when appropriate.  This is set while scanning macro
  2857.  * arguments before substitution.  It is zero when scanning for final output.
  2858.  *   There are three types of Newline markers:
  2859.  *   * Newline -  follows a macro name that was not expanded
  2860.  *     because it appeared inside an expansion of the same macro.
  2861.  *     This marker prevents future expansion of that identifier.
  2862.  *     When the input is rescanned into the final output, these are deleted.
  2863.  *     These are also deleted by ## concatenation.
  2864.  *   * Newline Space (or Newline and any other whitespace character)
  2865.  *     stands for a place that tokens must be separated or whitespace
  2866.  *     is otherwise desirable, but where the ANSI standard specifies there
  2867.  *     is no whitespace.  This marker turns into a Space (or whichever other
  2868.  *     whitespace char appears in the marker) in the final output,
  2869.  *     but it turns into nothing in an argument that is stringified with #.
  2870.  *     Such stringified arguments are the only place where the ANSI standard
  2871.  *     specifies with precision that whitespace may not appear.
  2872.  *
  2873.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  2874.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  2875.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  2876.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  2877.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  2878.  */
  2879.  
  2880. static void
  2881. rescan (op, output_marks)
  2882.      FILE_BUF *op;
  2883.      int output_marks;
  2884. {
  2885.   /* Character being scanned in main loop.  */
  2886.   register U_CHAR c;
  2887.  
  2888.   /* Length of pending accumulated identifier.  */
  2889.   register int ident_length = 0;
  2890.  
  2891.   /* Hash code of pending accumulated identifier.  */
  2892.   register int hash = 0;
  2893.  
  2894.   /* Current input level (&instack[indepth]).  */
  2895.   FILE_BUF *ip;
  2896.  
  2897.   /* Pointer for scanning input.  */
  2898.   register U_CHAR *ibp;
  2899.  
  2900.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  2901.   register U_CHAR *limit;
  2902.  
  2903.   /* Pointer for storing output.  */
  2904.   register U_CHAR *obp;
  2905.  
  2906.   /* REDO_CHAR is nonzero if we are processing an identifier
  2907.      after backing up over the terminating character.
  2908.      Sometimes we process an identifier without backing up over
  2909.      the terminating character, if the terminating character
  2910.      is not special.  Backing up is done so that the terminating character
  2911.      will be dispatched on again once the identifier is dealt with.  */
  2912.   int redo_char = 0;
  2913.  
  2914.   /* 1 if within an identifier inside of which a concatenation
  2915.      marker (Newline -) has been seen.  */
  2916.   int concatenated = 0;
  2917.  
  2918.   /* While scanning a comment or a string constant,
  2919.      this records the line it started on, for error messages.  */
  2920.   int start_line;
  2921.  
  2922.   /* Record position of last `real' newline.  */
  2923.   U_CHAR *beg_of_line;
  2924.  
  2925. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  2926.  
  2927. #define POPMACRO \
  2928. do { ip->macro->type = T_MACRO;        \
  2929.      if (ip->free_ptr) free (ip->free_ptr);    \
  2930.      --indepth; } while (0)
  2931.  
  2932. /* Reload `rescan's local variables that describe the current
  2933.    level of the input stack.  */
  2934.  
  2935. #define RECACHE  \
  2936. do { ip = &instack[indepth];        \
  2937.      ibp = ip->bufp;            \
  2938.      limit = ip->buf + ip->length;    \
  2939.      op->bufp = obp;            \
  2940.      check_expand (op, limit - ibp);    \
  2941.      beg_of_line = 0;            \
  2942.      obp = op->bufp; } while (0)
  2943.  
  2944.   if (no_output && instack[indepth].fname != 0)
  2945.     skip_if_group (&instack[indepth], 1, NULL);
  2946.  
  2947.   obp = op->bufp;
  2948.   RECACHE;
  2949.  
  2950.   beg_of_line = ibp;
  2951.  
  2952.   /* Our caller must always put a null after the end of
  2953.      the input at each input stack level.  */
  2954.   if (*limit != 0)
  2955.     abort ();
  2956.  
  2957.   while (1) {
  2958.     c = *ibp++;
  2959.     *obp++ = c;
  2960.  
  2961.     switch (c) {
  2962.     case '\\':
  2963.       if (*ibp == '\n' && !ip->macro) {
  2964.     /* At the top level, always merge lines ending with backslash-newline,
  2965.        even in middle of identifier.  But do not merge lines in a macro,
  2966.        since backslash might be followed by a newline-space marker.  */
  2967.     ++ibp;
  2968.     ++ip->lineno;
  2969.     --obp;        /* remove backslash from obuf */
  2970.     break;
  2971.       }
  2972.       /* If ANSI, backslash is just another character outside a string.  */
  2973.       if (!traditional)
  2974.     goto randomchar;
  2975.       /* Otherwise, backslash suppresses specialness of following char,
  2976.      so copy it here to prevent the switch from seeing it.
  2977.      But first get any pending identifier processed.  */
  2978.       if (ident_length > 0)
  2979.     goto specialchar;
  2980.       if (ibp < limit)
  2981.     *obp++ = *ibp++;
  2982.       break;
  2983.  
  2984.     case '%':
  2985.       if (ident_length || ip->macro || traditional)
  2986.     goto randomchar;
  2987.       while (*ibp == '\\' && ibp[1] == '\n') {
  2988.     ibp += 2;
  2989.     ++ip->lineno;
  2990.       }
  2991.       if (*ibp != ':')
  2992.     break;
  2993.       /* Treat this %: digraph as if it were #.  */
  2994.       /* Fall through.  */
  2995.  
  2996.     case '#':
  2997.       if (assertions_flag) {
  2998.     if (ident_length)
  2999.       goto specialchar;
  3000.     /* Copy #foo (bar lose) without macro expansion.  */
  3001.     obp[-1] = '#';    /* In case it was '%'. */
  3002.     SKIP_WHITE_SPACE (ibp);
  3003.     while (is_idchar[*ibp])
  3004.       *obp++ = *ibp++;
  3005.     SKIP_WHITE_SPACE (ibp);
  3006.     if (*ibp == '(') {
  3007.       ip->bufp = ibp;
  3008.       skip_paren_group (ip);
  3009.       bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
  3010.       obp += ip->bufp - ibp;
  3011.       ibp = ip->bufp;
  3012.     }
  3013.     break;
  3014.       }
  3015.  
  3016.       /* If this is expanding a macro definition, don't recognize
  3017.      preprocessing directives.  */
  3018.       if (ip->macro != 0)
  3019.     goto randomchar;
  3020.       /* If this is expand_into_temp_buffer,
  3021.      don't recognize them either.  Warn about them
  3022.      only after an actual newline at this level,
  3023.      not at the beginning of the input level.  */
  3024.       if (! ip->fname) {
  3025.     if (ip->buf != beg_of_line)
  3026.       warning ("preprocessing directive not recognized within macro arg");
  3027.     goto randomchar;
  3028.       }
  3029.       if (ident_length)
  3030.     goto specialchar;
  3031.  
  3032.       
  3033.       /* # keyword: a # must be first nonblank char on the line */
  3034.       if (beg_of_line == 0)
  3035.     goto randomchar;
  3036.       {
  3037.     U_CHAR *bp;
  3038.  
  3039.     /* Scan from start of line, skipping whitespace, comments
  3040.        and backslash-newlines, and see if we reach this #.
  3041.        If not, this # is not special.  */
  3042.     bp = beg_of_line;
  3043.     /* If -traditional, require # to be at beginning of line.  */
  3044.     if (!traditional) {
  3045.       while (1) {
  3046.         if (is_hor_space[*bp])
  3047.           bp++;
  3048.         else if (*bp == '\\' && bp[1] == '\n')
  3049.           bp += 2;
  3050.         else if (*bp == '/' && bp[1] == '*') {
  3051.           bp += 2;
  3052.           while (!(*bp == '*' && bp[1] == '/'))
  3053.         bp++;
  3054.           bp += 2;
  3055.         }
  3056.         /* There is no point in trying to deal with C++ // comments here,
  3057.            because if there is one, then this # must be part of the
  3058.            comment and we would never reach here.  */
  3059.         else break;
  3060.       }
  3061.       if (c == '%') {
  3062.         if (bp[0] != '%')
  3063.           break;
  3064.         while (bp[1] == '\\' && bp[2] == '\n')
  3065.           bp += 2;
  3066.         if (bp + 1 != ibp)
  3067.           break;
  3068.         /* %: appears at start of line; skip past the ':' too.  */
  3069.         bp++;
  3070.         ibp++;
  3071.       }
  3072.     }
  3073.     if (bp + 1 != ibp)
  3074.       goto randomchar;
  3075.       }
  3076.  
  3077.       /* This # can start a directive.  */
  3078.  
  3079.       --obp;        /* Don't copy the '#' */
  3080.  
  3081.       ip->bufp = ibp;
  3082.       op->bufp = obp;
  3083.       if (! handle_directive (ip, op)) {
  3084. #ifdef USE_C_ALLOCA
  3085.     alloca (0);
  3086. #endif
  3087.     /* Not a known directive: treat it as ordinary text.
  3088.        IP, OP, IBP, etc. have not been changed.  */
  3089.     if (no_output && instack[indepth].fname) {
  3090.       /* If not generating expanded output,
  3091.          what we do with ordinary text is skip it.
  3092.          Discard everything until next # directive.  */
  3093.       skip_if_group (&instack[indepth], 1, 0);
  3094.       RECACHE;
  3095.       beg_of_line = ibp;
  3096.       break;
  3097.     }
  3098.     *obp++ = '#';    /* Copy # (even if it was originally %:).  */
  3099.     /* Don't expand an identifier that could be a macro directive.
  3100.        (Section 3.8.3 of the ANSI C standard)            */
  3101.     SKIP_WHITE_SPACE (ibp);
  3102.     if (is_idstart[*ibp])
  3103.       {
  3104.         *obp++ = *ibp++;
  3105.         while (is_idchar[*ibp])
  3106.           *obp++ = *ibp++;
  3107.       }
  3108.     goto randomchar;
  3109.       }
  3110. #ifdef USE_C_ALLOCA
  3111.       alloca (0);
  3112. #endif
  3113.       /* A # directive has been successfully processed.  */
  3114.       /* If not generating expanded output, ignore everything until
  3115.      next # directive.  */
  3116.       if (no_output && instack[indepth].fname)
  3117.     skip_if_group (&instack[indepth], 1, 0);
  3118.       obp = op->bufp;
  3119.       RECACHE;
  3120.       beg_of_line = ibp;
  3121.       break;
  3122.  
  3123.     case '\"':            /* skip quoted string */
  3124.     case '\'':
  3125.       /* A single quoted string is treated like a double -- some
  3126.      programs (e.g., troff) are perverse this way */
  3127.  
  3128.       if (ident_length)
  3129.     goto specialchar;
  3130.  
  3131.       start_line = ip->lineno;
  3132.  
  3133.       /* Skip ahead to a matching quote.  */
  3134.  
  3135.       while (1) {
  3136.     if (ibp >= limit) {
  3137.       if (ip->macro != 0) {
  3138.         /* try harder: this string crosses a macro expansion boundary.
  3139.            This can happen naturally if -traditional.
  3140.            Otherwise, only -D can make a macro with an unmatched quote.  */
  3141.         POPMACRO;
  3142.         RECACHE;
  3143.         continue;
  3144.       }
  3145.       if (!traditional) {
  3146.         error_with_line (line_for_error (start_line),
  3147.                  "unterminated string or character constant");
  3148.         error_with_line (multiline_string_line,
  3149.                  "possible real start of unterminated constant");
  3150.         multiline_string_line = 0;
  3151.       }
  3152.       break;
  3153.     }
  3154.     *obp++ = *ibp;
  3155.     switch (*ibp++) {
  3156.     case '\n':
  3157.       ++ip->lineno;
  3158.       ++op->lineno;
  3159.       /* Traditionally, end of line ends a string constant with no error.
  3160.          So exit the loop and record the new line.  */
  3161.       if (traditional) {
  3162.         beg_of_line = ibp;
  3163.         goto while2end;
  3164.       }
  3165.       if (c == '\'') {
  3166.         error_with_line (line_for_error (start_line),
  3167.                  "unterminated character constant");
  3168.         goto while2end;
  3169.       }
  3170.       if (pedantic && multiline_string_line == 0) {
  3171.         pedwarn_with_line (line_for_error (start_line),
  3172.                    "string constant runs past end of line");
  3173.       }
  3174.       if (multiline_string_line == 0)
  3175.         multiline_string_line = ip->lineno - 1;
  3176.       break;
  3177.  
  3178.     case '\\':
  3179.       if (ibp >= limit)
  3180.         break;
  3181.       if (*ibp == '\n') {
  3182.         /* Backslash newline is replaced by nothing at all,
  3183.            but keep the line counts correct.  */
  3184.         --obp;
  3185.         ++ibp;
  3186.         ++ip->lineno;
  3187.       } else {
  3188.         /* ANSI stupidly requires that in \\ the second \
  3189.            is *not* prevented from combining with a newline.  */
  3190.         while (*ibp == '\\' && ibp[1] == '\n') {
  3191.           ibp += 2;
  3192.           ++ip->lineno;
  3193.         }
  3194.         *obp++ = *ibp++;
  3195.       }
  3196.       break;
  3197.  
  3198.     case '\"':
  3199.     case '\'':
  3200.       if (ibp[-1] == c)
  3201.         goto while2end;
  3202.       break;
  3203.     }
  3204.       }
  3205.     while2end:
  3206.       break;
  3207.  
  3208.     case '/':
  3209.       if (*ibp == '\\' && ibp[1] == '\n')
  3210.     newline_fix (ibp);
  3211.  
  3212.       if (*ibp != '*'
  3213.       && !(cplusplus_comments && *ibp == '/'))
  3214.     goto randomchar;
  3215.       if (ip->macro != 0)
  3216.     goto randomchar;
  3217.       if (ident_length)
  3218.     goto specialchar;
  3219.  
  3220.       if (*ibp == '/') {
  3221.     /* C++ style comment... */
  3222.     start_line = ip->lineno;
  3223.  
  3224.     /* Comments are equivalent to spaces. */
  3225.     if (! put_out_comments)
  3226.       obp[-1] = ' ';
  3227.  
  3228.     {
  3229.       U_CHAR *before_bp = ibp;
  3230.  
  3231.       while (++ibp < limit) {
  3232.         if (*ibp == '\n') {
  3233.           if (ibp[-1] != '\\') {
  3234.         if (put_out_comments) {
  3235.           bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3236.           obp += ibp - before_bp;
  3237.         }
  3238.         break;
  3239.           }
  3240.           ++ip->lineno;
  3241.           /* Copy the newline into the output buffer, in order to
  3242.          avoid the pain of a #line every time a multiline comment
  3243.          is seen.  */
  3244.           if (!put_out_comments)
  3245.         *obp++ = '\n';
  3246.           ++op->lineno;
  3247.         }
  3248.       }
  3249.       break;
  3250.     }
  3251.       }
  3252.  
  3253.       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
  3254.  
  3255.       start_line = ip->lineno;
  3256.  
  3257.       ++ibp;            /* Skip the star. */
  3258.  
  3259.       /* If this cpp is for lint, we peek inside the comments: */
  3260.       if (for_lint) {
  3261.     U_CHAR *argbp;
  3262.     int cmdlen, arglen;
  3263.     char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
  3264.  
  3265.     if (lintcmd != NULL) {
  3266.       op->bufp = obp;
  3267.       check_expand (op, cmdlen + arglen + 14);
  3268.       obp = op->bufp;
  3269.       /* I believe it is always safe to emit this newline: */
  3270.       obp[-1] = '\n';
  3271.       bcopy ("#pragma lint ", (char *) obp, 13);
  3272.       obp += 13;
  3273.       bcopy (lintcmd, (char *) obp, cmdlen);
  3274.       obp += cmdlen;
  3275.  
  3276.       if (arglen != 0) {
  3277.         *(obp++) = ' ';
  3278.         bcopy (argbp, (char *) obp, arglen);
  3279.         obp += arglen;
  3280.       }
  3281.  
  3282.       /* OK, now bring us back to the state we were in before we entered
  3283.          this branch.  We need #line because the #pragma's newline always
  3284.          messes up the line count.  */
  3285.       op->bufp = obp;
  3286.       output_line_directive (ip, op, 0, same_file);
  3287.       check_expand (op, limit - ibp + 2);
  3288.       obp = op->bufp;
  3289.       *(obp++) = '/';
  3290.     }
  3291.       }
  3292.  
  3293.       /* Comments are equivalent to spaces.
  3294.      Note that we already output the slash; we might not want it.
  3295.      For -traditional, a comment is equivalent to nothing.  */
  3296.       if (! put_out_comments) {
  3297.     if (traditional)
  3298.       obp--;
  3299.     else
  3300.       obp[-1] = ' ';
  3301.       }
  3302.       else
  3303.     *obp++ = '*';
  3304.  
  3305.       {
  3306.     U_CHAR *before_bp = ibp;
  3307.  
  3308.     while (ibp < limit) {
  3309.       switch (*ibp++) {
  3310.       case '/':
  3311.         if (warn_comments && *ibp == '*')
  3312.           warning ("`/*' within comment");
  3313.         break;
  3314.       case '*':
  3315.         if (*ibp == '\\' && ibp[1] == '\n')
  3316.           newline_fix (ibp);
  3317.         if (ibp >= limit || *ibp == '/')
  3318.           goto comment_end;
  3319.         break;
  3320.       case '\n':
  3321.         ++ip->lineno;
  3322.         /* Copy the newline into the output buffer, in order to
  3323.            avoid the pain of a #line every time a multiline comment
  3324.            is seen.  */
  3325.         if (!put_out_comments)
  3326.           *obp++ = '\n';
  3327.         ++op->lineno;
  3328.       }
  3329.     }
  3330.       comment_end:
  3331.  
  3332.     if (ibp >= limit)
  3333.       error_with_line (line_for_error (start_line),
  3334.                "unterminated comment");
  3335.     else {
  3336.       ibp++;
  3337.       if (put_out_comments) {
  3338.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3339.         obp += ibp - before_bp;
  3340.       }
  3341.     }
  3342.       }
  3343.       break;
  3344.  
  3345.     case '$':
  3346.       if (!dollars_in_ident)
  3347.     goto randomchar;
  3348.       goto letter;
  3349.  
  3350.     case '0': case '1': case '2': case '3': case '4':
  3351.     case '5': case '6': case '7': case '8': case '9':
  3352.       /* If digit is not part of identifier, it starts a number,
  3353.      which means that following letters are not an identifier.
  3354.      "0x5" does not refer to an identifier "x5".
  3355.      So copy all alphanumerics that follow without accumulating
  3356.      as an identifier.  Periods also, for sake of "3.e7".  */
  3357.  
  3358.       if (ident_length == 0) {
  3359.     for (;;) {
  3360.       while (ibp[0] == '\\' && ibp[1] == '\n') {
  3361.         ++ip->lineno;
  3362.         ibp += 2;
  3363.       }
  3364.       c = *ibp++;
  3365.       if (!is_idchar[c] && c != '.') {
  3366.         --ibp;
  3367.         break;
  3368.       }
  3369.       *obp++ = c;
  3370.       /* A sign can be part of a preprocessing number
  3371.          if it follows an e.  */
  3372.       if (c == 'e' || c == 'E') {
  3373.         while (ibp[0] == '\\' && ibp[1] == '\n') {
  3374.           ++ip->lineno;
  3375.           ibp += 2;
  3376.         }
  3377.         if (*ibp == '+' || *ibp == '-') {
  3378.           *obp++ = *ibp++;
  3379.           /* But traditional C does not let the token go past the sign.  */
  3380.           if (traditional)
  3381.         break;
  3382.         }
  3383.       }
  3384.     }
  3385.     break;
  3386.       }
  3387.       /* fall through */
  3388.  
  3389.     case '_':
  3390.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  3391.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  3392.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  3393.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  3394.     case 'y': case 'z':
  3395.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  3396.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  3397.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  3398.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  3399.     case 'Y': case 'Z':
  3400.     letter:
  3401.       ident_length++;
  3402.       /* Compute step of hash function, to avoid a proc call on every token */
  3403.       hash = HASHSTEP (hash, c);
  3404.       break;
  3405.  
  3406.     case '\n':
  3407.       if (ip->fname == 0 && *ibp == '-') {
  3408.     /* Newline - inhibits expansion of preceding token.
  3409.        If expanding a macro arg, we keep the newline -.
  3410.        In final output, it is deleted.
  3411.        We recognize Newline - in macro bodies and macro args.  */
  3412.     if (! concatenated) {
  3413.       ident_length = 0;
  3414.       hash = 0;
  3415.     }
  3416.     ibp++;
  3417.     if (!output_marks) {
  3418.       obp--;
  3419.     } else {
  3420.       /* If expanding a macro arg, keep the newline -.  */
  3421.       *obp++ = '-';
  3422.     }
  3423.     break;
  3424.       }
  3425.  
  3426.       /* If reprocessing a macro expansion, newline is a special marker.  */
  3427.       else if (ip->macro != 0) {
  3428.     /* Newline White is a "funny space" to separate tokens that are
  3429.        supposed to be separate but without space between.
  3430.        Here White means any whitespace character.
  3431.        Newline - marks a recursive macro use that is not
  3432.        supposed to be expandable.  */
  3433.  
  3434.     if (is_space[*ibp]) {
  3435.       /* Newline Space does not prevent expansion of preceding token
  3436.          so expand the preceding token and then come back.  */
  3437.       if (ident_length > 0)
  3438.         goto specialchar;
  3439.  
  3440.       /* If generating final output, newline space makes a space.  */
  3441.       if (!output_marks) {
  3442.         obp[-1] = *ibp++;
  3443.         /* And Newline Newline makes a newline, so count it.  */
  3444.         if (obp[-1] == '\n')
  3445.           op->lineno++;
  3446.       } else {
  3447.         /* If expanding a macro arg, keep the newline space.
  3448.            If the arg gets stringified, newline space makes nothing.  */
  3449.         *obp++ = *ibp++;
  3450.       }
  3451.     } else abort ();    /* Newline followed by something random?  */
  3452.     break;
  3453.       }
  3454.  
  3455.       /* If there is a pending identifier, handle it and come back here.  */
  3456.       if (ident_length > 0)
  3457.     goto specialchar;
  3458.  
  3459.       beg_of_line = ibp;
  3460.  
  3461.       /* Update the line counts and output a #line if necessary.  */
  3462.       ++ip->lineno;
  3463.       ++op->lineno;
  3464.       if (ip->lineno != op->lineno) {
  3465.     op->bufp = obp;
  3466.     output_line_directive (ip, op, 1, same_file);
  3467.     check_expand (op, limit - ibp);
  3468.     obp = op->bufp;
  3469.       }
  3470.       break;
  3471.  
  3472.       /* Come here either after (1) a null character that is part of the input
  3473.      or (2) at the end of the input, because there is a null there.  */
  3474.     case 0:
  3475.       if (ibp <= limit)
  3476.     /* Our input really contains a null character.  */
  3477.     goto randomchar;
  3478.  
  3479.       /* At end of a macro-expansion level, pop it and read next level.  */
  3480.       if (ip->macro != 0) {
  3481.     obp--;
  3482.     ibp--;
  3483.     /* If traditional, and we have an identifier that ends here,
  3484.        process it now, so we get the right error for recursion.  */
  3485.     if (traditional && ident_length
  3486.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  3487.       redo_char = 1;
  3488.       goto randomchar;
  3489.     }
  3490.     POPMACRO;
  3491.     RECACHE;
  3492.     break;
  3493.       }
  3494.  
  3495.       /* If we don't have a pending identifier,
  3496.      return at end of input.  */
  3497.       if (ident_length == 0) {
  3498.     obp--;
  3499.     ibp--;
  3500.     op->bufp = obp;
  3501.     ip->bufp = ibp;
  3502.     goto ending;
  3503.       }
  3504.  
  3505.       /* If we do have a pending identifier, just consider this null
  3506.      a special character and arrange to dispatch on it again.
  3507.      The second time, IDENT_LENGTH will be zero so we will return.  */
  3508.  
  3509.       /* Fall through */
  3510.  
  3511. specialchar:
  3512.  
  3513.       /* Handle the case of a character such as /, ', " or null
  3514.      seen following an identifier.  Back over it so that
  3515.      after the identifier is processed the special char
  3516.      will be dispatched on again.  */
  3517.  
  3518.       ibp--;
  3519.       obp--;
  3520.       redo_char = 1;
  3521.  
  3522.     default:
  3523.  
  3524. randomchar:
  3525.  
  3526.       if (ident_length > 0) {
  3527.     register HASHNODE *hp;
  3528.  
  3529.     /* We have just seen an identifier end.  If it's a macro, expand it.
  3530.  
  3531.        IDENT_LENGTH is the length of the identifier
  3532.        and HASH is its hash code.
  3533.  
  3534.        The identifier has already been copied to the output,
  3535.        so if it is a macro we must remove it.
  3536.  
  3537.        If REDO_CHAR is 0, the char that terminated the identifier
  3538.        has been skipped in the output and the input.
  3539.        OBP-IDENT_LENGTH-1 points to the identifier.
  3540.        If the identifier is a macro, we must back over the terminator.
  3541.  
  3542.        If REDO_CHAR is 1, the terminating char has already been
  3543.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  3544.  
  3545.     if (!pcp_outfile || pcp_inside_if) {
  3546.       for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  3547.            hp = hp->next) {
  3548.         
  3549.         if (hp->length == ident_length) {
  3550.           int obufp_before_macroname;
  3551.           int op_lineno_before_macroname;
  3552.           register int i = ident_length;
  3553.           register U_CHAR *p = hp->name;
  3554.           register U_CHAR *q = obp - i;
  3555.           int disabled;
  3556.           
  3557.           if (! redo_char)
  3558.         q--;
  3559.           
  3560.           do {        /* All this to avoid a strncmp () */
  3561.         if (*p++ != *q++)
  3562.           goto hashcollision;
  3563.           } while (--i);
  3564.           
  3565.           /* We found a use of a macro name.
  3566.          see if the context shows it is a macro call.  */
  3567.           
  3568.           /* Back up over terminating character if not already done.  */
  3569.           if (! redo_char) {
  3570.         ibp--;
  3571.         obp--;
  3572.           }
  3573.           
  3574.           /* Save this as a displacement from the beginning of the output
  3575.          buffer.  We can not save this as a position in the output
  3576.          buffer, because it may get realloc'ed by RECACHE.  */
  3577.           obufp_before_macroname = (obp - op->buf) - ident_length;
  3578.           op_lineno_before_macroname = op->lineno;
  3579.           
  3580.           if (hp->type == T_PCSTRING) {
  3581.         pcstring_used (hp); /* Mark the definition of this key
  3582.                        as needed, ensuring that it
  3583.                        will be output.  */
  3584.         break;        /* Exit loop, since the key cannot have a
  3585.                    definition any longer.  */
  3586.           }
  3587.  
  3588.           /* Record whether the macro is disabled.  */
  3589.           disabled = hp->type == T_DISABLED;
  3590.           
  3591.           /* This looks like a macro ref, but if the macro was disabled,
  3592.          just copy its name and put in a marker if requested.  */
  3593.           
  3594.           if (disabled) {
  3595. #if 0
  3596.         /* This error check caught useful cases such as
  3597.            #define foo(x,y) bar (x (y,0), y)
  3598.            foo (foo, baz)  */
  3599.         if (traditional)
  3600.           error ("recursive use of macro `%s'", hp->name);
  3601. #endif
  3602.         
  3603.         if (output_marks) {
  3604.           check_expand (op, limit - ibp + 2);
  3605.           *obp++ = '\n';
  3606.           *obp++ = '-';
  3607.         }
  3608.         break;
  3609.           }
  3610.           
  3611.           /* If macro wants an arglist, verify that a '(' follows.
  3612.          first skip all whitespace, copying it to the output
  3613.          after the macro name.  Then, if there is no '(',
  3614.          decide this is not a macro call and leave things that way.  */
  3615.           if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  3616.           && hp->value.defn->nargs >= 0)
  3617.         {
  3618.           U_CHAR *old_ibp = ibp;
  3619.           U_CHAR *old_obp = obp;
  3620.           int old_iln = ip->lineno;
  3621.           int old_oln = op->lineno;
  3622.           
  3623.           while (1) {
  3624.             /* Scan forward over whitespace, copying it to the output.  */
  3625.             if (ibp == limit && ip->macro != 0) {
  3626.               POPMACRO;
  3627.               RECACHE;
  3628.               old_ibp = ibp;
  3629.               old_obp = obp;
  3630.               old_iln = ip->lineno;
  3631.               old_oln = op->lineno;
  3632.             }
  3633.             /* A comment: copy it unchanged or discard it.  */
  3634.             else if (*ibp == '/' && ibp[1] == '*') {
  3635.               if (put_out_comments) {
  3636.             *obp++ = '/';
  3637.             *obp++ = '*';
  3638.               } else if (! traditional) {
  3639.             *obp++ = ' ';
  3640.               }
  3641.               ibp += 2;
  3642.               while (ibp + 1 != limit
  3643.                  && !(ibp[0] == '*' && ibp[1] == '/')) {
  3644.             /* We need not worry about newline-marks,
  3645.                since they are never found in comments.  */
  3646.             if (*ibp == '\n') {
  3647.               /* Newline in a file.  Count it.  */
  3648.               ++ip->lineno;
  3649.               ++op->lineno;
  3650.             }
  3651.             if (put_out_comments)
  3652.               *obp++ = *ibp++;
  3653.             else
  3654.               ibp++;
  3655.               }
  3656.               ibp += 2;
  3657.               if (put_out_comments) {
  3658.             *obp++ = '*';
  3659.             *obp++ = '/';
  3660.               }
  3661.             }
  3662.             else if (is_space[*ibp]) {
  3663.               *obp++ = *ibp++;
  3664.               if (ibp[-1] == '\n') {
  3665.             if (ip->macro == 0) {
  3666.               /* Newline in a file.  Count it.  */
  3667.               ++ip->lineno;
  3668.               ++op->lineno;
  3669.             } else if (!output_marks) {
  3670.               /* A newline mark, and we don't want marks
  3671.                  in the output.  If it is newline-hyphen,
  3672.                  discard it entirely.  Otherwise, it is
  3673.                  newline-whitechar, so keep the whitechar.  */
  3674.               obp--;
  3675.               if (*ibp == '-')
  3676.                 ibp++;
  3677.               else {
  3678.                 if (*ibp == '\n')
  3679.                   ++op->lineno;
  3680.                 *obp++ = *ibp++;
  3681.               }
  3682.             } else {
  3683.               /* A newline mark; copy both chars to the output.  */
  3684.               *obp++ = *ibp++;
  3685.             }
  3686.               }
  3687.             }
  3688.             else break;
  3689.           }
  3690.           if (*ibp != '(') {
  3691.             /* It isn't a macro call.
  3692.                Put back the space that we just skipped.  */
  3693.             ibp = old_ibp;
  3694.             obp = old_obp;
  3695.             ip->lineno = old_iln;
  3696.             op->lineno = old_oln;
  3697.             /* Exit the for loop.  */
  3698.             break;
  3699.           }
  3700.         }
  3701.           
  3702.           /* This is now known to be a macro call.
  3703.          Discard the macro name from the output,
  3704.          along with any following whitespace just copied,
  3705.          but preserve newlines if not outputting marks since this
  3706.          is more likely to do the right thing with line numbers.  */
  3707.           obp = op->buf + obufp_before_macroname;
  3708.           if (output_marks)
  3709.         op->lineno = op_lineno_before_macroname;
  3710.           else {
  3711.         int newlines = op->lineno - op_lineno_before_macroname;
  3712.         while (0 < newlines--)
  3713.           *obp++ = '\n';
  3714.           }
  3715.  
  3716.           /* Prevent accidental token-pasting with a character
  3717.          before the macro call.  */
  3718.           if (!traditional && obp != op->buf) {
  3719.         switch (obp[-1]) {
  3720.         case '!':  case '%':  case '&':  case '*':
  3721.         case '+':  case '-':  case '/':  case ':':
  3722.         case '<':  case '=':  case '>':  case '^':
  3723.         case '|':
  3724.           /* If we are expanding a macro arg, make a newline marker
  3725.              to separate the tokens.  If we are making real output,
  3726.              a plain space will do.  */
  3727.           if (output_marks)
  3728.             *obp++ = '\n';
  3729.           *obp++ = ' ';
  3730.         }
  3731.           }
  3732.  
  3733.           /* Expand the macro, reading arguments as needed,
  3734.          and push the expansion on the input stack.  */
  3735.           ip->bufp = ibp;
  3736.           op->bufp = obp;
  3737.           macroexpand (hp, op);
  3738.           
  3739.           /* Reexamine input stack, since macroexpand has pushed
  3740.          a new level on it.  */
  3741.           obp = op->bufp;
  3742.           RECACHE;
  3743.           break;
  3744.         }
  3745. hashcollision:
  3746.         ;
  3747.       }            /* End hash-table-search loop */
  3748.     }
  3749.     ident_length = hash = 0; /* Stop collecting identifier */
  3750.     redo_char = 0;
  3751.     concatenated = 0;
  3752.       }                /* End if (ident_length > 0) */
  3753.     }                /* End switch */
  3754.   }                /* End per-char loop */
  3755.  
  3756.   /* Come here to return -- but first give an error message
  3757.      if there was an unterminated successful conditional.  */
  3758.  ending:
  3759.   if (if_stack != ip->if_stack)
  3760.     {
  3761.       char *str;
  3762.  
  3763.       switch (if_stack->type)
  3764.     {
  3765.     case T_IF:
  3766.       str = "if";
  3767.       break;
  3768.     case T_IFDEF:
  3769.       str = "ifdef";
  3770.       break;
  3771.     case T_IFNDEF:
  3772.       str = "ifndef";
  3773.       break;
  3774.     case T_ELSE:
  3775.       str = "else";
  3776.       break;
  3777.     case T_ELIF:
  3778.       str = "elif";
  3779.       break;
  3780.     default:
  3781.       abort ();
  3782.     }
  3783.  
  3784.       error_with_line (line_for_error (if_stack->lineno),
  3785.                "unterminated `#%s' conditional", str);
  3786.   }
  3787.   if_stack = ip->if_stack;
  3788. }
  3789.  
  3790. /*
  3791.  * Rescan a string into a temporary buffer and return the result
  3792.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  3793.  *
  3794.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  3795.  * and insert such markers when appropriate.  See `rescan' for details.
  3796.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  3797.  * before substitution; it is 0 for other uses.
  3798.  */
  3799. static FILE_BUF
  3800. expand_to_temp_buffer (buf, limit, output_marks, assertions)
  3801.      U_CHAR *buf, *limit;
  3802.      int output_marks, assertions;
  3803. {
  3804.   register FILE_BUF *ip;
  3805.   FILE_BUF obuf;
  3806.   int length = limit - buf;
  3807.   U_CHAR *buf1;
  3808.   int odepth = indepth;
  3809.   int save_assertions_flag = assertions_flag;
  3810.  
  3811.   assertions_flag = assertions;
  3812.  
  3813.   if (length < 0)
  3814.     abort ();
  3815.  
  3816.   /* Set up the input on the input stack.  */
  3817.  
  3818.   buf1 = (U_CHAR *) alloca (length + 1);
  3819.   {
  3820.     register U_CHAR *p1 = buf;
  3821.     register U_CHAR *p2 = buf1;
  3822.  
  3823.     while (p1 != limit)
  3824.       *p2++ = *p1++;
  3825.   }
  3826.   buf1[length] = 0;
  3827.  
  3828.   /* Set up to receive the output.  */
  3829.  
  3830.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  3831.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  3832.   obuf.fname = 0;
  3833.   obuf.macro = 0;
  3834.   obuf.free_ptr = 0;
  3835.  
  3836.   CHECK_DEPTH ({return obuf;});
  3837.  
  3838.   ++indepth;
  3839.  
  3840.   ip = &instack[indepth];
  3841.   ip->fname = 0;
  3842.   ip->nominal_fname = 0;
  3843.   ip->system_header_p = 0;
  3844.   ip->macro = 0;
  3845.   ip->free_ptr = 0;
  3846.   ip->length = length;
  3847.   ip->buf = ip->bufp = buf1;
  3848.   ip->if_stack = if_stack;
  3849.  
  3850.   ip->lineno = obuf.lineno = 1;
  3851.  
  3852.   /* Scan the input, create the output.  */
  3853.   rescan (&obuf, output_marks);
  3854.  
  3855.   /* Pop input stack to original state.  */
  3856.   --indepth;
  3857.  
  3858.   if (indepth != odepth)
  3859.     abort ();
  3860.  
  3861.   /* Record the output.  */
  3862.   obuf.length = obuf.bufp - obuf.buf;
  3863.  
  3864.   assertions_flag = save_assertions_flag;
  3865.   return obuf;
  3866. }
  3867.  
  3868. /*
  3869.  * Process a # directive.  Expects IP->bufp to point after the '#', as in
  3870.  * `#define foo bar'.  Passes to the directive handler
  3871.  * (do_define, do_include, etc.): the addresses of the 1st and
  3872.  * last chars of the directive (starting immediately after the #
  3873.  * keyword), plus op and the keyword table pointer.  If the directive
  3874.  * contains comments it is copied into a temporary buffer sans comments
  3875.  * and the temporary buffer is passed to the directive handler instead.
  3876.  * Likewise for backslash-newlines.
  3877.  *
  3878.  * Returns nonzero if this was a known # directive.
  3879.  * Otherwise, returns zero, without advancing the input pointer.
  3880.  */
  3881.  
  3882. static int
  3883. handle_directive (ip, op)
  3884.      FILE_BUF *ip, *op;
  3885. {
  3886.   register U_CHAR *bp, *cp;
  3887.   register struct directive *kt;
  3888.   register int ident_length;
  3889.   U_CHAR *resume_p;
  3890.  
  3891.   /* Nonzero means we must copy the entire directive
  3892.      to get rid of comments or backslash-newlines.  */
  3893.   int copy_directive = 0;
  3894.  
  3895.   U_CHAR *ident, *after_ident;
  3896.  
  3897.   bp = ip->bufp;
  3898.  
  3899.   /* Record where the directive started.  do_xifdef needs this.  */
  3900.   directive_start = bp - 1;
  3901.  
  3902.   /* Skip whitespace and \-newline.  */
  3903.   while (1) {
  3904.     if (is_hor_space[*bp]) {
  3905.       if (*bp != ' ' && *bp != '\t' && pedantic)
  3906.     pedwarn ("%s in preprocessing directive", char_name[*bp]);
  3907.       bp++;
  3908.     } else if (*bp == '/' && (bp[1] == '*'
  3909.                   || (cplusplus_comments && bp[1] == '/'))) {
  3910.       ip->bufp = bp + 2;
  3911.       skip_to_end_of_comment (ip, &ip->lineno, 0);
  3912.       bp = ip->bufp;
  3913.     } else if (*bp == '\\' && bp[1] == '\n') {
  3914.       bp += 2; ip->lineno++;
  3915.     } else break;
  3916.   }
  3917.  
  3918.   /* Now find end of directive name.
  3919.      If we encounter a backslash-newline, exchange it with any following
  3920.      symbol-constituents so that we end up with a contiguous name.  */
  3921.  
  3922.   cp = bp;
  3923.   while (1) {
  3924.     if (is_idchar[*cp])
  3925.       cp++;
  3926.     else {
  3927.       if (*cp == '\\' && cp[1] == '\n')
  3928.     name_newline_fix (cp);
  3929.       if (is_idchar[*cp])
  3930.     cp++;
  3931.       else break;
  3932.     }
  3933.   }
  3934.   ident_length = cp - bp;
  3935.   ident = bp;
  3936.   after_ident = cp;
  3937.  
  3938.   /* A line of just `#' becomes blank.  */
  3939.  
  3940.   if (ident_length == 0 && *after_ident == '\n') {
  3941.     ip->bufp = after_ident;
  3942.     return 1;
  3943.   }
  3944.  
  3945.   if (ident_length == 0 || !is_idstart[*ident]) {
  3946.     U_CHAR *p = ident;
  3947.     while (is_idchar[*p]) {
  3948.       if (*p < '0' || *p > '9')
  3949.     break;
  3950.       p++;
  3951.     }
  3952.     /* Handle # followed by a line number.  */
  3953.     if (p != ident && !is_idchar[*p]) {
  3954.       static struct directive line_directive_table[] = {
  3955.     {  4, do_line, "line", T_LINE},
  3956.       };
  3957.       if (pedantic)
  3958.     pedwarn ("`#' followed by integer");
  3959.       after_ident = ident;
  3960.       kt = line_directive_table;
  3961.       goto old_linenum;
  3962.     }
  3963.  
  3964.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  3965.     if (p == ident) {
  3966.       while (*p == '#' || is_hor_space[*p]) p++;
  3967.       if (*p == '\n') {
  3968.     if (pedantic && !lang_asm)
  3969.       warning ("invalid preprocessing directive");
  3970.     return 0;
  3971.       }
  3972.     }
  3973.  
  3974.     if (!lang_asm)
  3975.       error ("invalid preprocessing directive name");
  3976.  
  3977.     return 0;
  3978.   }
  3979.  
  3980.   /*
  3981.    * Decode the keyword and call the appropriate expansion
  3982.    * routine, after moving the input pointer up to the next line.
  3983.    */
  3984.   for (kt = directive_table; kt->length > 0; kt++) {
  3985.     if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
  3986.       register U_CHAR *buf;
  3987.       register U_CHAR *limit;
  3988.       int unterminated;
  3989.       int junk;
  3990.       int *already_output;
  3991.  
  3992.       /* Nonzero means do not delete comments within the directive.
  3993.      #define needs this when -traditional.  */
  3994.       int keep_comments;
  3995.  
  3996.     old_linenum:
  3997.  
  3998.       limit = ip->buf + ip->length;
  3999.       unterminated = 0;
  4000.       already_output = 0;
  4001.       keep_comments = traditional && kt->traditional_comments;
  4002.       /* #import is defined only in Objective C, or when on the NeXT.  */
  4003.       if (kt->type == T_IMPORT
  4004.       && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1)))
  4005.     break;
  4006.  
  4007.       /* Find the end of this directive (first newline not backslashed
  4008.      and not in a string or comment).
  4009.      Set COPY_DIRECTIVE if the directive must be copied
  4010.      (it contains a backslash-newline or a comment).  */
  4011.  
  4012.       buf = bp = after_ident;
  4013.       while (bp < limit) {
  4014.     register U_CHAR c = *bp++;
  4015.     switch (c) {
  4016.     case '\\':
  4017.       if (bp < limit) {
  4018.         if (*bp == '\n') {
  4019.           ip->lineno++;
  4020.           copy_directive = 1;
  4021.           bp++;
  4022.         } else if (traditional)
  4023.           bp++;
  4024.       }
  4025.       break;
  4026.  
  4027.     case '\'':
  4028.     case '\"':
  4029.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_directive, &unterminated);
  4030.       /* Don't bother calling the directive if we already got an error
  4031.          message due to unterminated string.  Skip everything and pretend
  4032.          we called the directive.  */
  4033.       if (unterminated) {
  4034.         if (traditional) {
  4035.           /* Traditional preprocessing permits unterminated strings.  */
  4036.           ip->bufp = bp;
  4037.           goto endloop1;
  4038.         }
  4039.         ip->bufp = bp;
  4040.         return 1;
  4041.       }
  4042.       break;
  4043.  
  4044.       /* <...> is special for #include.  */
  4045.     case '<':
  4046.       if (!kt->angle_brackets)
  4047.         break;
  4048.       while (bp < limit && *bp != '>' && *bp != '\n') {
  4049.         if (*bp == '\\' && bp[1] == '\n') {
  4050.           ip->lineno++;
  4051.           copy_directive = 1;
  4052.           bp++;
  4053.         }
  4054.         bp++;
  4055.       }
  4056.       break;
  4057.  
  4058.     case '/':
  4059.       if (*bp == '\\' && bp[1] == '\n')
  4060.         newline_fix (bp);
  4061.       if (*bp == '*'
  4062.           || (cplusplus_comments && *bp == '/')) {
  4063.         U_CHAR *obp = bp - 1;
  4064.         ip->bufp = bp + 1;
  4065.         skip_to_end_of_comment (ip, &ip->lineno, 0);
  4066.         bp = ip->bufp;
  4067.         /* No need to copy the directive because of a comment at the end;
  4068.            just don't include the comment in the directive.  */
  4069.         if (bp == limit || *bp == '\n') {
  4070.           bp = obp;
  4071.           goto endloop1;
  4072.         }
  4073.         /* Don't remove the comments if -traditional.  */
  4074.         if (! keep_comments)
  4075.           copy_directive++;
  4076.       }
  4077.       break;
  4078.  
  4079.     case '\f':
  4080.     case '\r':
  4081.     case '\v':
  4082.       if (pedantic)
  4083.         pedwarn ("%s in preprocessing directive", char_name[c]);
  4084.       break;
  4085.  
  4086.     case '\n':
  4087.       --bp;        /* Point to the newline */
  4088.       ip->bufp = bp;
  4089.       goto endloop1;
  4090.     }
  4091.       }
  4092.       ip->bufp = bp;
  4093.  
  4094.     endloop1:
  4095.       resume_p = ip->bufp;
  4096.       /* BP is the end of the directive.
  4097.      RESUME_P is the next interesting data after the directive.
  4098.      A comment may come between.  */
  4099.  
  4100.       /* If a directive should be copied through, and -E was given,
  4101.      pass it through before removing comments.  */
  4102.       if (!no_output && kt->pass_thru && put_out_comments) {
  4103.         int len;
  4104.  
  4105.     /* Output directive name.  */
  4106.         check_expand (op, kt->length + 2);
  4107.     /* Make sure # is at the start of a line */
  4108.     if (op->bufp > op->buf && op->bufp[-1] != '\n') {
  4109.       op->lineno++;
  4110.       *op->bufp++ = '\n';
  4111.     }
  4112.         *op->bufp++ = '#';
  4113.         bcopy (kt->name, op->bufp, kt->length);
  4114.         op->bufp += kt->length;
  4115.  
  4116.     /* Output arguments.  */
  4117.     len = (bp - buf);
  4118.     check_expand (op, len);
  4119.     bcopy (buf, (char *) op->bufp, len);
  4120.     op->bufp += len;
  4121.     /* Take account of any (escaped) newlines just output.  */
  4122.     while (--len >= 0)
  4123.       if (buf[len] == '\n')
  4124.         op->lineno++;
  4125.  
  4126.     already_output = &junk;
  4127.       }                /* Don't we need a newline or #line? */
  4128.  
  4129.       if (copy_directive) {
  4130.     register U_CHAR *xp = buf;
  4131.     /* Need to copy entire directive into temp buffer before dispatching */
  4132.  
  4133.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for directive plus
  4134.                           some slop */
  4135.     buf = cp;
  4136.  
  4137.     /* Copy to the new buffer, deleting comments
  4138.        and backslash-newlines (and whitespace surrounding the latter).  */
  4139.  
  4140.     while (xp < bp) {
  4141.       register U_CHAR c = *xp++;
  4142.       *cp++ = c;
  4143.  
  4144.       switch (c) {
  4145.       case '\n':
  4146.         abort ();  /* A bare newline should never part of the line.  */
  4147.         break;
  4148.  
  4149.         /* <...> is special for #include.  */
  4150.       case '<':
  4151.         if (!kt->angle_brackets)
  4152.           break;
  4153.         while (xp < bp && c != '>') {
  4154.           c = *xp++;
  4155.           if (c == '\\' && xp < bp && *xp == '\n')
  4156.         xp++;
  4157.           else
  4158.         *cp++ = c;
  4159.         }
  4160.         break;
  4161.  
  4162.       case '\\':
  4163.         if (*xp == '\n') {
  4164.           xp++;
  4165.           cp--;
  4166.           if (cp != buf && is_hor_space[cp[-1]]) {
  4167.         while (cp - 1 != buf && is_hor_space[cp[-2]])
  4168.           cp--;
  4169.         SKIP_WHITE_SPACE (xp);
  4170.           } else if (is_hor_space[*xp]) {
  4171.         *cp++ = *xp++;
  4172.         SKIP_WHITE_SPACE (xp);
  4173.           }
  4174.         } else if (traditional && xp < bp) {
  4175.           *cp++ = *xp++;
  4176.         }
  4177.         break;
  4178.  
  4179.       case '\'':
  4180.       case '\"':
  4181.         {
  4182.           register U_CHAR *bp1
  4183.         = skip_quoted_string (xp - 1, bp, ip->lineno,
  4184.                       NULL_PTR, NULL_PTR, NULL_PTR);
  4185.           while (xp != bp1)
  4186.         if (*xp == '\\') {
  4187.           if (*++xp != '\n')
  4188.             *cp++ = '\\';
  4189.           else
  4190.             xp++;
  4191.         } else
  4192.           *cp++ = *xp++;
  4193.         }
  4194.         break;
  4195.  
  4196.       case '/':
  4197.         if (*xp == '*'
  4198.         || (cplusplus_comments && *xp == '/')) {
  4199.           ip->bufp = xp + 1;
  4200.           /* If we already copied the directive through,
  4201.          already_output != 0 prevents outputting comment now.  */
  4202.           skip_to_end_of_comment (ip, already_output, 0);
  4203.           if (keep_comments)
  4204.         while (xp != ip->bufp)
  4205.           *cp++ = *xp++;
  4206.           /* Delete or replace the slash.  */
  4207.           else if (traditional)
  4208.         cp--;
  4209.           else
  4210.         cp[-1] = ' ';
  4211.           xp = ip->bufp;
  4212.         }
  4213.       }
  4214.     }
  4215.  
  4216.     /* Null-terminate the copy.  */
  4217.  
  4218.     *cp = 0;
  4219.       } else
  4220.     cp = bp;
  4221.  
  4222.       ip->bufp = resume_p;
  4223.  
  4224.       /* Some directives should be written out for cc1 to process,
  4225.      just as if they were not defined.  And sometimes we're copying
  4226.      definitions through.  */
  4227.  
  4228.       if (!no_output && already_output == 0
  4229.       && (kt->pass_thru
  4230.           || (kt->type == T_DEFINE
  4231.           && (dump_macros == dump_names
  4232.               || dump_macros == dump_definitions)))) {
  4233.         int len;
  4234.  
  4235.     /* Output directive name.  */
  4236.         check_expand (op, kt->length + 1);
  4237.         *op->bufp++ = '#';
  4238.         bcopy (kt->name, (char *) op->bufp, kt->length);
  4239.         op->bufp += kt->length;
  4240.  
  4241.     if (kt->pass_thru || dump_macros == dump_definitions) {
  4242.       /* Output arguments.  */
  4243.       len = (cp - buf);
  4244.       check_expand (op, len);
  4245.       bcopy (buf, (char *) op->bufp, len);
  4246.       op->bufp += len;
  4247.     } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
  4248.       U_CHAR *xp = buf;
  4249.       U_CHAR *yp;
  4250.       SKIP_WHITE_SPACE (xp);
  4251.       yp = xp;
  4252.       while (is_idchar[*xp]) xp++;
  4253.       len = (xp - yp);
  4254.       check_expand (op, len + 1);
  4255.       *op->bufp++ = ' ';
  4256.       bcopy (yp, op->bufp, len);
  4257.       op->bufp += len;
  4258.     }
  4259.       }                /* Don't we need a newline or #line? */
  4260.  
  4261.       /* Call the appropriate directive handler.  buf now points to
  4262.      either the appropriate place in the input buffer, or to
  4263.      the temp buffer if it was necessary to make one.  cp
  4264.      points to the first char after the contents of the (possibly
  4265.      copied) directive, in either case. */
  4266.       (*kt->func) (buf, cp, op, kt);
  4267.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  4268.  
  4269.       return 1;
  4270.     }
  4271.   }
  4272.  
  4273.   /* It is deliberate that we don't warn about undefined directives.
  4274.      That is the responsibility of cc1.  */
  4275.   return 0;
  4276. }
  4277.  
  4278. static struct tm *
  4279. timestamp ()
  4280. {
  4281.   static struct tm *timebuf;
  4282.   if (!timebuf) {
  4283.     time_t t = time ((time_t *)0);
  4284.     timebuf = localtime (&t);
  4285.   }
  4286.   return timebuf;
  4287. }
  4288.  
  4289. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  4290.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  4291.                 };
  4292.  
  4293. /*
  4294.  * expand things like __FILE__.  Place the expansion into the output
  4295.  * buffer *without* rescanning.
  4296.  */
  4297.  
  4298. static void
  4299. special_symbol (hp, op)
  4300.      HASHNODE *hp;
  4301.      FILE_BUF *op;
  4302. {
  4303.   char *buf;
  4304.   int i, len;
  4305.   int true_indepth;
  4306.   FILE_BUF *ip = NULL;
  4307.   struct tm *timebuf;
  4308.  
  4309.   int paren = 0;        /* For special `defined' keyword */
  4310.  
  4311.   if (pcp_outfile && pcp_inside_if
  4312.       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
  4313.     error ("Predefined macro `%s' used inside `#if' during precompilation",
  4314.        hp->name);
  4315.     
  4316.   for (i = indepth; i >= 0; i--)
  4317.     if (instack[i].fname != NULL) {
  4318.       ip = &instack[i];
  4319.       break;
  4320.     }
  4321.   if (ip == NULL) {
  4322.     error ("cccp error: not in any file?!");
  4323.     return;            /* the show must go on */
  4324.   }
  4325.  
  4326.   switch (hp->type) {
  4327.   case T_FILE:
  4328.   case T_BASE_FILE:
  4329.     {
  4330.       char *string;
  4331.       if (hp->type == T_FILE)
  4332.     string = ip->nominal_fname;
  4333.       else
  4334.     string = instack[0].nominal_fname;
  4335.  
  4336.       if (string)
  4337.     {
  4338.       buf = (char *) alloca (3 + 4 * strlen (string));
  4339.       quote_string (buf, string);
  4340.     }
  4341.       else
  4342.     buf = "\"\"";
  4343.  
  4344.       break;
  4345.     }
  4346.  
  4347.   case T_INCLUDE_LEVEL:
  4348.     true_indepth = 0;
  4349.     for (i = indepth; i >= 0; i--)
  4350.       if (instack[i].fname != NULL)
  4351.         true_indepth++;
  4352.  
  4353.     buf = (char *) alloca (8);    /* Eight bytes ought to be more than enough */
  4354.     sprintf (buf, "%d", true_indepth - 1);
  4355.     break;
  4356.  
  4357.   case T_VERSION:
  4358.     buf = (char *) alloca (3 + strlen (version_string));
  4359.     sprintf (buf, "\"%s\"", version_string);
  4360.     break;
  4361.  
  4362. #ifndef NO_BUILTIN_SIZE_TYPE
  4363.   case T_SIZE_TYPE:
  4364.     buf = SIZE_TYPE;
  4365.     break;
  4366. #endif
  4367.  
  4368. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  4369.   case T_PTRDIFF_TYPE:
  4370.     buf = PTRDIFF_TYPE;
  4371.     break;
  4372. #endif
  4373.  
  4374.   case T_WCHAR_TYPE:
  4375.     buf = wchar_type;
  4376.     break;
  4377.  
  4378.   case T_USER_LABEL_PREFIX_TYPE:
  4379.     buf = USER_LABEL_PREFIX;
  4380.     break;
  4381.  
  4382.   case T_REGISTER_PREFIX_TYPE:
  4383.     buf = REGISTER_PREFIX;
  4384.     break;
  4385.  
  4386.   case T_IMMEDIATE_PREFIX_TYPE:
  4387.     buf = IMMEDIATE_PREFIX;
  4388.     break;
  4389.  
  4390.   case T_CONST:
  4391.     buf = hp->value.cpval;
  4392.     if (pcp_inside_if && pcp_outfile)
  4393.       /* Output a precondition for this macro use */
  4394.       fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
  4395.     break;
  4396.  
  4397.   case T_SPECLINE:
  4398.     buf = (char *) alloca (10);
  4399.     sprintf (buf, "%d", ip->lineno);
  4400.     break;
  4401.  
  4402.   case T_DATE:
  4403.   case T_TIME:
  4404.     buf = (char *) alloca (20);
  4405.     timebuf = timestamp ();
  4406.     if (hp->type == T_DATE)
  4407.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  4408.           timebuf->tm_mday, timebuf->tm_year + 1900);
  4409.     else
  4410.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  4411.           timebuf->tm_sec);
  4412.     break;
  4413.  
  4414.   case T_SPEC_DEFINED:
  4415.     buf = " 0 ";        /* Assume symbol is not defined */
  4416.     ip = &instack[indepth];
  4417.     SKIP_WHITE_SPACE (ip->bufp);
  4418.     if (*ip->bufp == '(') {
  4419.       paren++;
  4420.       ip->bufp++;            /* Skip over the paren */
  4421.       SKIP_WHITE_SPACE (ip->bufp);
  4422.     }
  4423.  
  4424.     if (!is_idstart[*ip->bufp])
  4425.       goto oops;
  4426.     if ((hp = lookup (ip->bufp, -1, -1))) {
  4427.       if (pcp_outfile && pcp_inside_if
  4428.       && (hp->type == T_CONST
  4429.           || (hp->type == T_MACRO && hp->value.defn->predefined)))
  4430.     /* Output a precondition for this macro use. */
  4431.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  4432.       buf = " 1 ";
  4433.     }
  4434.     else
  4435.       if (pcp_outfile && pcp_inside_if)    {
  4436.     /* Output a precondition for this macro use */
  4437.     U_CHAR *cp = ip->bufp;
  4438.     fprintf (pcp_outfile, "#undef ");
  4439.     while (is_idchar[*cp]) /* Ick! */
  4440.       fputc (*cp++, pcp_outfile);
  4441.     putc ('\n', pcp_outfile);
  4442.       }
  4443.     while (is_idchar[*ip->bufp])
  4444.       ++ip->bufp;
  4445.     SKIP_WHITE_SPACE (ip->bufp);
  4446.     if (paren) {
  4447.       if (*ip->bufp != ')')
  4448.     goto oops;
  4449.       ++ip->bufp;
  4450.     }
  4451.     break;
  4452.  
  4453. oops:
  4454.  
  4455.     error ("`defined' without an identifier");
  4456.     break;
  4457.  
  4458.   default:
  4459.     error ("cccp error: invalid special hash type"); /* time for gdb */
  4460.     abort ();
  4461.   }
  4462.   len = strlen (buf);
  4463.   check_expand (op, len);
  4464.   bcopy (buf, (char *) op->bufp, len);
  4465.   op->bufp += len;
  4466.  
  4467.   return;
  4468. }
  4469.  
  4470.  
  4471. /* Routines to handle #directives */
  4472.  
  4473. /* Handle #include and #import.
  4474.    This function expects to see "fname" or <fname> on the input.  */
  4475.  
  4476. static int
  4477. do_include (buf, limit, op, keyword)
  4478.      U_CHAR *buf, *limit;
  4479.      FILE_BUF *op;
  4480.      struct directive *keyword;
  4481. {
  4482.   int importing = (keyword->type == T_IMPORT);
  4483.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  4484. #ifdef NEXT_OBJC_RUNTIME
  4485.   static int import_warning = 1;
  4486. #else
  4487.   static int import_warning = 0;
  4488. #endif
  4489.   char *fname;        /* Dynamically allocated fname buffer */
  4490.   char *pcftry;
  4491.   char *pcfname;
  4492.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  4493. #ifdef NEXT_FRAMEWORKS
  4494.   char extendedPath[MAX_FILENAME_LEN];
  4495.   U_CHAR *bufptr;
  4496.   int hasSlash;    
  4497. #endif
  4498.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  4499.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  4500.   struct file_name_list *searchptr = 0;
  4501.   size_t flen;
  4502.   struct import_file *impfile = 0;
  4503.  
  4504.   int f;            /* file number */
  4505.  
  4506.   int retried = 0;        /* Have already tried macro
  4507.                    expanding the include line*/
  4508.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  4509.   int pcf = -1;
  4510.   char *pcfbuf;
  4511.   char *pcfbuflimit;
  4512.   int pcfnum;
  4513.   f= -1;            /* JF we iz paranoid! */
  4514.  
  4515.   if (importing && warn_import && !inhibit_warnings
  4516.       && !instack[indepth].system_header_p && !import_warning) {
  4517.     import_warning = 1;
  4518.     warning ("using `#import' is not recommended");
  4519.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  4520.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  4521.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  4522.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  4523.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  4524.     fprintf (stderr, "  ... <real contents of file> ...\n");
  4525.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  4526.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  4527.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  4528.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  4529.   }
  4530.  
  4531. get_filename:
  4532.  
  4533.   fbeg = buf;
  4534.   SKIP_WHITE_SPACE (fbeg);
  4535.   /* Discard trailing whitespace so we can easily see
  4536.      if we have parsed all the significant chars we were given.  */
  4537.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  4538.  
  4539.   switch (*fbeg++) {
  4540.   case '\"':
  4541.     {
  4542.       FILE_BUF *fp;
  4543.       /* Copy the operand text, concatenating the strings.  */
  4544.       {
  4545.     U_CHAR *fin = fbeg;
  4546.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  4547.     fend = fbeg;
  4548.     while (fin != limit) {
  4549.       while (fin != limit && *fin != '\"')
  4550.         *fend++ = *fin++;
  4551.       fin++;
  4552.       if (fin == limit)
  4553.         break;
  4554.       /* If not at the end, there had better be another string.  */
  4555.       /* Skip just horiz space, and don't go past limit.  */
  4556.       while (fin != limit && is_hor_space[*fin]) fin++;
  4557.       if (fin != limit && *fin == '\"')
  4558.         fin++;
  4559.       else
  4560.         goto fail;
  4561.     }
  4562.       }
  4563.       *fend = 0;
  4564.  
  4565.       /* We have "filename".  Figure out directory this source
  4566.      file is coming from and put it on the front of the list. */
  4567.  
  4568.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  4569.       if (ignore_srcdir) break;
  4570.  
  4571.       for (fp = &instack[indepth]; fp >= instack; fp--)
  4572.     {
  4573.       int n;
  4574.       char *ep,*nam;
  4575.  
  4576.       if ((nam = fp->nominal_fname) != NULL) {
  4577.         /* Found a named file.  Figure out dir of the file,
  4578.            and put it in front of the search list.  */
  4579.         dsp[0].next = search_start;
  4580.         search_start = dsp;
  4581. #ifndef VMS
  4582.         ep = rindex (nam, '/');
  4583. #ifdef DIR_SEPARATOR
  4584.         if (ep == NULL) ep = rindex (nam, DIR_SEPARATOR);
  4585.         else {
  4586.           char *tmp = rindex (nam, DIR_SEPARATOR);
  4587.           if (tmp != NULL && tmp > ep) ep = tmp;
  4588.         }
  4589. #endif
  4590. #else                /* VMS */
  4591.         ep = rindex (nam, ']');
  4592.         if (ep == NULL) ep = rindex (nam, '>');
  4593.         if (ep == NULL) ep = rindex (nam, ':');
  4594.         if (ep != NULL) ep++;
  4595. #endif                /* VMS */
  4596.         if (ep != NULL) {
  4597.           n = ep - nam;
  4598.           dsp[0].fname = (char *) alloca (n + 1);
  4599.           strncpy (dsp[0].fname, nam, n);
  4600.           dsp[0].fname[n] = '\0';
  4601.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  4602.         max_include_len = n + INCLUDE_LEN_FUDGE;
  4603.         } else {
  4604.           dsp[0].fname = 0; /* Current directory */
  4605.         }
  4606.         dsp[0].got_name_map = 0;
  4607.         break;
  4608.       }
  4609.     }
  4610.       break;
  4611.     }
  4612.  
  4613.   case '<':
  4614.     fend = fbeg;
  4615.     while (fend != limit && *fend != '>') fend++;
  4616.     if (*fend == '>' && fend + 1 == limit) {
  4617.       angle_brackets = 1;
  4618. #ifdef NEXT_FRAMEWORKS
  4619.       hasSlash = 0;
  4620.       for (bufptr = fbeg; bufptr != fend; bufptr++)
  4621.           if (*bufptr == '/') {
  4622.              hasSlash++;
  4623.            break;
  4624.         }
  4625.        if (hasSlash) {
  4626.          extendedPath[0] = '\0';
  4627.          for (searchptr = framework;
  4628.               f < 0 && searchptr != 0;
  4629.               searchptr = searchptr->next) {
  4630.             U_CHAR *end, *headerNameTo, *headerNameFrom;
  4631.             int index;
  4632.             
  4633.             /* path to framework */
  4634.             strcpy(extendedPath, searchptr->fname);
  4635.             end = &extendedPath[strlen(extendedPath)];
  4636.             *end++ = '/';
  4637.  
  4638.             /* add framework directory */
  4639.             for (bufptr = fbeg;
  4640.                  *bufptr != '/' && bufptr != fend;
  4641.                  bufptr++)
  4642.                *end++ = *bufptr;
  4643.             strncpy(end, ".framework/", 11);   end += 11;
  4644.             
  4645.             for (index = 0; framework_header_dirs[index].dirName; index++) {
  4646.                /* add path to headers inside framework */
  4647.                strncpy(end,
  4648.                        framework_header_dirs[index].dirName,
  4649.                        framework_header_dirs[index].dirNameLen);
  4650.                /* add name of header */
  4651.                headerNameTo = end + framework_header_dirs[index].dirNameLen;
  4652.                for (headerNameFrom = bufptr; headerNameFrom != fend; headerNameFrom++)
  4653.                   *headerNameTo++ = *headerNameFrom;
  4654.                *headerNameTo = '\0';
  4655.                /* Try looking for the file */
  4656. #ifdef NEXT_CPP_SERVER
  4657.                hdr_result = try_include_file(extendedPath, importing, &f, &impfile, &shared_stat_f);
  4658.                if (hdr_result == REDUNDANT_HEADER)
  4659.                   return 0;
  4660.                else if (hdr_result == GOOD_HEADER)
  4661.                   break;
  4662. #if 0
  4663.                /* lookup in negative cache...opens/stats are painfully slow */
  4664.                if (!found_in_negative_cache(extendedPath)) {
  4665.                   impfile = 0;
  4666.                   if (importing)
  4667.                      f = lookup_import (extendedPath, &shared_stat_f, &impfile);
  4668.                   else {
  4669.                      redundant_include_p (extendedPath, &impfile);
  4670.                      if (!impfile)
  4671.                         f = open (extendedPath, O_RDONLY, 0666);
  4672.                      else
  4673.                         f = -2;
  4674.                   }
  4675.                   if (f == -2) {
  4676.                      if (segregate_output &&
  4677.                          impfile->visited_within_module &&
  4678.                          impfile->name &&
  4679.                          !impfile->name->has_explicit_context_dependency) {
  4680.                         return 0;         /* Already included this file */
  4681.                      }
  4682.                   }
  4683.                   else if (f == -1) {
  4684.                      add_to_negative_cache(savestring(extendedPath));
  4685.                   }
  4686.                   else if (!importing) { /* handle the "once idiom" */
  4687.                      if (impfile && impfile->name->control_macro &&
  4688.                          lookup (impfile->name->control_macro, -1, -1))
  4689.                         return 0;
  4690.                   }
  4691.                   if (f >= 0 || impfile) {
  4692.                      if (!importing)
  4693.                         fstat (f, &shared_stat_f);
  4694.                      break;
  4695.                   }
  4696.                } /* else, found in negative cache...don't bother */
  4697. #endif 0
  4698. #else
  4699.                if (importing)
  4700.                   f = lookup_import (extendedPath, searchptr);
  4701.                else
  4702.                   f = open_include_file (extendedPath, searchptr);
  4703.                if (f == -2)
  4704.                   return 0;            /* Already included this file */
  4705. #ifdef EACCES
  4706.                  else if (f == -1 && errno == EACCES)
  4707.                      warning ("Header file %s exists, but is not readable",
  4708.                               extendedPath);
  4709. #endif
  4710.                  if (redundant_include_p (extendedPath)) {
  4711.                      close (f);
  4712.                      return 0;
  4713.                  }
  4714.                  if (f >= 0)
  4715.                      break;
  4716. #endif
  4717.                  
  4718.              }
  4719.           }
  4720.       }
  4721. #endif
  4722.  
  4723.       /* If -I-, start with the first -I dir after the -I-.  */
  4724.       if (first_bracket_include)
  4725.     search_start = first_bracket_include;
  4726.       break;
  4727.     }
  4728.     goto fail;
  4729.  
  4730.   default:
  4731. #ifdef VMS
  4732.     /*
  4733.      * Support '#include xyz' like VAX-C to allow for easy use of all the
  4734.      * decwindow include files. It defaults to '#include <xyz.h>' (so the
  4735.      * code from case '<' is repeated here) and generates a warning.
  4736.      * (Note: macro expansion of `xyz' takes precedence.)
  4737.      */
  4738.     if (retried && isalpha(*(--fbeg))) {
  4739.       fend = fbeg;
  4740.       while (fend != limit && (!isspace(*fend))) fend++;
  4741.       warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
  4742.       if (fend  == limit) {
  4743.     angle_brackets = 1;
  4744.     /* If -I-, start with the first -I dir after the -I-.  */
  4745.     if (first_bracket_include)
  4746.       search_start = first_bracket_include;
  4747.     break;
  4748.       }
  4749.     }
  4750. #endif
  4751.  
  4752.   fail:
  4753.     if (retried) {
  4754.       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
  4755.       return 0;
  4756.     } else {
  4757.       /* Expand buffer and then remove any newline markers.
  4758.      We can't just tell expand_to_temp_buffer to omit the markers,
  4759.      since it would put extra spaces in include file names.  */
  4760.       FILE_BUF trybuf;
  4761.       U_CHAR *src;
  4762.       trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
  4763.       src = trybuf.buf;
  4764.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  4765.       limit = buf;
  4766.       while (src != trybuf.bufp) {
  4767.     switch ((*limit++ = *src++)) {
  4768.       case '\n':
  4769.         limit--;
  4770.         src++;
  4771.         break;
  4772.  
  4773.       case '\'':
  4774.       case '\"':
  4775.         {
  4776.           U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
  4777.                          NULL_PTR, NULL_PTR, NULL_PTR);
  4778.           while (src != src1)
  4779.         *limit++ = *src++;
  4780.         }
  4781.         break;
  4782.     }
  4783.       }
  4784.       *limit = 0;
  4785.       free (trybuf.buf);
  4786.       retried++;
  4787.       goto get_filename;
  4788.     }
  4789.   }
  4790.  
  4791.   /* For #include_next, skip in the search path
  4792.      past the dir in which the containing file was found.  */
  4793.   if (skip_dirs) {
  4794.     FILE_BUF *fp;
  4795.     for (fp = &instack[indepth]; fp >= instack; fp--)
  4796.       if (fp->fname != NULL) {
  4797.     /* fp->dir is null if the containing file was specified
  4798.        with an absolute file name.  In that case, don't skip anything.  */
  4799.     if (fp->dir)
  4800.       search_start = fp->dir->next;
  4801.     break;
  4802.       }
  4803.   }
  4804.  
  4805.   flen = fend - fbeg;
  4806.  
  4807.   if (flen == 0)
  4808.     {
  4809.       error ("empty file name in `#%s'", keyword->name);
  4810.       return 0;
  4811.     }
  4812.  
  4813. #ifdef NEXT_FRAMEWORKS
  4814.   if (f > 0 || impfile) {
  4815.      fname = savestring(extendedPath);
  4816.      goto found_in_framework;
  4817.   }
  4818. #endif
  4819.  
  4820.   /* Allocate this permanently, because it gets stored in the definitions
  4821.      of macros.  */
  4822.   fname = xmalloc (max_include_len + flen + 4);
  4823.   /* + 2 above for slash and terminating null.  */
  4824.   /* + 2 added for '.h' on VMS (to support '#include filename') */
  4825.  
  4826.   /* If specified file name is absolute, just open it.  */
  4827.  
  4828.   if (*fbeg == '/'
  4829. #ifdef DIR_SEPARATOR
  4830.       || *fbeg == DIR_SEPARATOR
  4831. #endif
  4832.       ) {
  4833.     strncpy (fname, (char *) fbeg, flen);
  4834.     fname[flen] = 0;
  4835.     if (redundant_include_p (fname))
  4836.       return 0;
  4837.     if (importing)
  4838.       f = lookup_import (fname, NULL_PTR);
  4839.     else
  4840.       f = open_include_file (fname, NULL_PTR);
  4841.     if (f == -2)
  4842.       return 0;        /* Already included this file */
  4843.   } else {
  4844.     /* Search directory path, trying to open the file.
  4845.        Copy each filename tried into FNAME.  */
  4846.  
  4847.     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  4848.       if (searchptr->fname) {
  4849.     /* The empty string in a search path is ignored.
  4850.        This makes it possible to turn off entirely
  4851.        a standard piece of the list.  */
  4852.     if (searchptr->fname[0] == 0)
  4853.       continue;
  4854.     strcpy (fname, skip_redundant_dir_prefix (searchptr->fname));
  4855.     if (fname[0] && fname[strlen (fname) - 1] != '/')
  4856.       strcat (fname, "/");
  4857.       } else {
  4858.     fname[0] = 0;
  4859.       }
  4860.       strncat (fname, (char *) fbeg, flen);
  4861. #ifdef VMS
  4862.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  4863.          full VMS file specification */
  4864.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  4865.     /* Fix up the filename */
  4866.     hack_vms_include_specification (fname);
  4867.       } else {
  4868.           /* This is a normal VMS filespec, so use it unchanged.  */
  4869.     strncpy (fname, fbeg, flen);
  4870.     fname[flen] = 0;
  4871.     /* if it's '#include filename', add the missing .h */
  4872.     if (index(fname,'.')==NULL) {
  4873.       strcat (fname, ".h");
  4874.     }
  4875.       }
  4876. #endif /* VMS */
  4877.       /* ??? There are currently 3 separate mechanisms for avoiding processing
  4878.      of redundant include files: #import, #pragma once, and
  4879.      redundant_include_p.  It would be nice if they were unified.  */
  4880.       if (redundant_include_p (fname))
  4881.     return 0;
  4882.       if (importing)
  4883.     f = lookup_import (fname, searchptr);
  4884.       else
  4885.     f = open_include_file (fname, searchptr);
  4886.       if (f == -2)
  4887.     return 0;            /* Already included this file */
  4888. #ifdef EACCES
  4889.       else if (f == -1 && errno == EACCES)
  4890.     warning ("Header file %s exists, but is not readable", fname);
  4891. #endif
  4892.       if (f >= 0)
  4893.     break;
  4894.     }
  4895.   }
  4896.  
  4897. #ifdef NEXT_FRAMEWORKS
  4898. found_in_framework:
  4899. #endif
  4900.  
  4901.   if (f < 0) {
  4902.     /* A file that was not found.  */
  4903.  
  4904.     strncpy (fname, (char *) fbeg, flen);
  4905.     fname[flen] = 0;
  4906.     /* If generating dependencies and -MG was specified, we assume missing
  4907.        files are leaf files, living in the same directory as the source file
  4908.        or other similar place; these missing files may be generated from
  4909.        other files and may not exist yet (eg: y.tab.h).  */
  4910.     if (print_deps_missing_files
  4911.     && print_deps > (angle_brackets || (system_include_depth > 0)))
  4912.       {
  4913.     /* If it was requested as a system header file,
  4914.        then assume it belongs in the first place to look for such.  */
  4915.     if (angle_brackets)
  4916.       {
  4917.         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
  4918.           {
  4919.         if (searchptr->fname)
  4920.           {
  4921.             char *p;
  4922.  
  4923.             if (searchptr->fname[0] == 0)
  4924.               continue;
  4925.             p = (char *) alloca (strlen (searchptr->fname)
  4926.                      + strlen (fname) + 2);
  4927.             strcpy (p, skip_redundant_dir_prefix (searchptr->fname));
  4928.             if (p[0] && p[strlen (p) - 1] != '/')
  4929.               strcat (p, "/");
  4930.             strcat (p, fname);
  4931.             deps_output (p, ' ');
  4932.             break;
  4933.           }
  4934.           }
  4935.       }
  4936.     else
  4937.       {
  4938.         /* Otherwise, omit the directory, as if the file existed
  4939.            in the directory with the source.  */
  4940.         deps_output (fname, ' ');
  4941.       }
  4942.       }
  4943.     /* If -M was specified, and this header file won't be added to the
  4944.        dependency list, then don't count this as an error, because we can
  4945.        still produce correct output.  Otherwise, we can't produce correct
  4946.        output, because there may be dependencies we need inside the missing
  4947.        file, and we don't know what directory this missing file exists in.  */
  4948.     else if (print_deps
  4949.     && (print_deps <= (angle_brackets || (system_include_depth > 0))))
  4950.       warning ("No include path in which to find %s", fname);
  4951.     else if (search_start)
  4952.       error_from_errno (fname);
  4953.     else
  4954.       error ("No include path in which to find %s", fname);
  4955.   } else {
  4956.     /* Check to see if this include file is a once-only include file.
  4957.        If so, give up.  */
  4958.  
  4959.     struct file_name_list* ptr;
  4960.  
  4961.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  4962.       if (!strcmp (ptr->fname, fname)) {
  4963.     close (f);
  4964.         return 0;                /* This file was once'd. */
  4965.       }
  4966.     }
  4967.  
  4968.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  4969.       if (!strcmp (ptr->fname, fname))
  4970.         break;                /* This file was included before. */
  4971.     }
  4972.  
  4973.     if (ptr == 0) {
  4974.       /* This is the first time for this file.  */
  4975.       /* Add it to list of files included.  */
  4976.  
  4977.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  4978.       ptr->control_macro = 0;
  4979.       ptr->c_system_include_path = 0;
  4980.       ptr->next = all_include_files;
  4981.       all_include_files = ptr;
  4982.       ptr->fname = savestring (fname);
  4983.       ptr->got_name_map = 0;
  4984.  
  4985.       /* For -M, add this file to the dependencies.  */
  4986.       if (print_deps > (angle_brackets || (system_include_depth > 0)))
  4987.     deps_output (fname, ' ');
  4988.     }   
  4989.  
  4990.     /* Handle -H option.  */
  4991.     if (print_include_names)
  4992.       fprintf (stderr, "%*s%s\n", indepth, "", fname);
  4993.  
  4994.     if (angle_brackets)
  4995.       system_include_depth++;
  4996.  
  4997.     /* Actually process the file.  */
  4998.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  4999.  
  5000.     pcftry = (char *) alloca (strlen (fname) + 30);
  5001.     pcfbuf = 0;
  5002.     pcfnum = 0;
  5003.  
  5004.     if (!no_precomp)
  5005.       {
  5006.     struct stat stat_f;
  5007.  
  5008.     fstat (f, &stat_f);
  5009.  
  5010.     do {
  5011.       sprintf (pcftry, "%s%d", fname, pcfnum++);
  5012.  
  5013.       pcf = open (pcftry, O_RDONLY, 0666);
  5014.       if (pcf != -1)
  5015.         {
  5016.           struct stat s;
  5017.  
  5018.           fstat (pcf, &s);
  5019.           if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
  5020.             sizeof (s.st_ino))
  5021.           || stat_f.st_dev != s.st_dev)
  5022.         {
  5023.           pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  5024.           /* Don't need it any more.  */
  5025.           close (pcf);
  5026.         }
  5027.           else
  5028.         {
  5029.           /* Don't need it at all.  */
  5030.           close (pcf);
  5031.           break;
  5032.         }
  5033.         }
  5034.     } while (pcf != -1 && !pcfbuf);
  5035.       }
  5036.     
  5037.     /* Actually process the file */
  5038.     if (pcfbuf) {
  5039.       pcfname = xmalloc (strlen (pcftry) + 1);
  5040.       strcpy (pcfname, pcftry);
  5041.       pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) pcfbuflimit,
  5042.           (U_CHAR *) fname, op);
  5043.     }
  5044.     else
  5045.       finclude (f, fname, op, is_system_include (fname), searchptr);
  5046.  
  5047.     if (angle_brackets)
  5048.       system_include_depth--;
  5049.   }
  5050.   return 0;
  5051. }
  5052.  
  5053. /* Return nonzero if there is no need to include file NAME
  5054.    because it has already been included and it contains a conditional
  5055.    to make a repeated include do nothing.  */
  5056.  
  5057. static int
  5058. redundant_include_p (name)
  5059.      char *name;
  5060. {
  5061.   struct file_name_list *l = all_include_files;
  5062.   for (; l; l = l->next)
  5063.     if (! strcmp (name, l->fname)
  5064.     && l->control_macro
  5065.     && lookup (l->control_macro, -1, -1))
  5066.       return 1;
  5067.   return 0;
  5068. }
  5069.  
  5070. /* Return nonzero if the given FILENAME is an absolute pathname which
  5071.    designates a file within one of the known "system" include file
  5072.    directories.  We assume here that if the given FILENAME looks like
  5073.    it is the name of a file which resides either directly in a "system"
  5074.    include file directory, or within any subdirectory thereof, then the
  5075.    given file must be a "system" include file.  This function tells us
  5076.    if we should suppress pedantic errors/warnings for the given FILENAME.
  5077.  
  5078.    The value is 2 if the file is a C-language system header file
  5079.    for which C++ should (on most systems) assume `extern "C"'.  */
  5080.  
  5081. static int
  5082. is_system_include (filename)
  5083.     register char *filename;
  5084. {
  5085.   struct file_name_list *searchptr;
  5086.  
  5087.   for (searchptr = first_system_include; searchptr;
  5088.        searchptr = searchptr->next)
  5089.     if (searchptr->fname) {
  5090.       register char *sys_dir = skip_redundant_dir_prefix (searchptr->fname);
  5091.       register unsigned length = strlen (sys_dir);
  5092.  
  5093.       if (! strncmp (sys_dir, filename, length)
  5094.       && (filename[length] == '/'
  5095. #ifdef DIR_SEPARATOR
  5096.           || filename[length] == DIR_SEPARATOR
  5097. #endif
  5098.           )) {
  5099.     if (searchptr->c_system_include_path)
  5100.       return 2;
  5101.     else
  5102.       return 1;
  5103.       }
  5104.     }
  5105.   return 0;
  5106. }
  5107.  
  5108. /* Skip leading "./" from a directory name.
  5109.    This may yield the empty string, which represents the current directory.  */
  5110.  
  5111. static char *
  5112. skip_redundant_dir_prefix (dir)
  5113.      char *dir;
  5114. {
  5115.   while (dir[0] == '.' && dir[1] == '/')
  5116.     for (dir += 2; *dir == '/'; dir++)
  5117.       continue;
  5118.   if (dir[0] == '.' && !dir[1])
  5119.     dir++;
  5120.   return dir;
  5121. }
  5122.  
  5123. /* The file_name_map structure holds a mapping of file names for a
  5124.    particular directory.  This mapping is read from the file named
  5125.    FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
  5126.    map filenames on a file system with severe filename restrictions,
  5127.    such as DOS.  The format of the file name map file is just a series
  5128.    of lines with two tokens on each line.  The first token is the name
  5129.    to map, and the second token is the actual name to use.  */
  5130.  
  5131. struct file_name_map
  5132. {
  5133.   struct file_name_map *map_next;
  5134.   char *map_from;
  5135.   char *map_to;
  5136. };
  5137.  
  5138. #define FILE_NAME_MAP_FILE "header.gcc"
  5139.  
  5140. /* Read a space delimited string of unlimited length from a stdio
  5141.    file.  */
  5142.  
  5143. static char *
  5144. read_filename_string (ch, f)
  5145.      int ch;
  5146.      FILE *f;
  5147. {
  5148.   char *alloc, *set;
  5149.   int len;
  5150.  
  5151.   len = 20;
  5152.   set = alloc = xmalloc (len + 1);
  5153.   if (! is_space[ch])
  5154.     {
  5155.       *set++ = ch;
  5156.       while ((ch = getc (f)) != EOF && ! is_space[ch])
  5157.     {
  5158.       if (set - alloc == len)
  5159.         {
  5160.           len *= 2;
  5161.           alloc = xrealloc (alloc, len + 1);
  5162.           set = alloc + len / 2;
  5163.         }
  5164.       *set++ = ch;
  5165.     }
  5166.     }
  5167.   *set = '\0';
  5168.   ungetc (ch, f);
  5169.   return alloc;
  5170. }
  5171.  
  5172. /* Read the file name map file for DIRNAME.  */
  5173.  
  5174. static struct file_name_map *
  5175. read_name_map (dirname)
  5176.      char *dirname;
  5177. {
  5178.   /* This structure holds a linked list of file name maps, one per
  5179.      directory.  */
  5180.   struct file_name_map_list
  5181.     {
  5182.       struct file_name_map_list *map_list_next;
  5183.       char *map_list_name;
  5184.       struct file_name_map *map_list_map;
  5185.     };
  5186.   static struct file_name_map_list *map_list;
  5187.   register struct file_name_map_list *map_list_ptr;
  5188.   char *name;
  5189.   FILE *f;
  5190.   size_t dirlen;
  5191.   int separator_needed;
  5192.  
  5193.   dirname = skip_redundant_dir_prefix (dirname);
  5194.  
  5195.   for (map_list_ptr = map_list; map_list_ptr;
  5196.        map_list_ptr = map_list_ptr->map_list_next)
  5197.     if (! strcmp (map_list_ptr->map_list_name, dirname))
  5198.       return map_list_ptr->map_list_map;
  5199.  
  5200.   map_list_ptr = ((struct file_name_map_list *)
  5201.           xmalloc (sizeof (struct file_name_map_list)));
  5202.   map_list_ptr->map_list_name = savestring (dirname);
  5203.   map_list_ptr->map_list_map = NULL;
  5204.  
  5205.   dirlen = strlen (dirname);
  5206.   separator_needed = dirlen != 0 && dirname[dirlen - 1] != '/';
  5207.   name = (char *) alloca (dirlen + strlen (FILE_NAME_MAP_FILE) + 2);
  5208.   strcpy (name, dirname);
  5209.   name[dirlen] = '/';
  5210.   strcpy (name + dirlen + separator_needed, FILE_NAME_MAP_FILE);
  5211.   f = fopen (name, "r");
  5212.   if (!f)
  5213.     map_list_ptr->map_list_map = NULL;
  5214.   else
  5215.     {
  5216.       int ch;
  5217.  
  5218.       while ((ch = getc (f)) != EOF)
  5219.     {
  5220.       char *from, *to;
  5221.       struct file_name_map *ptr;
  5222.  
  5223.       if (is_space[ch])
  5224.         continue;
  5225.       from = read_filename_string (ch, f);
  5226.       while ((ch = getc (f)) != EOF && is_hor_space[ch])
  5227.         ;
  5228.       to = read_filename_string (ch, f);
  5229.  
  5230.       ptr = ((struct file_name_map *)
  5231.          xmalloc (sizeof (struct file_name_map)));
  5232.       ptr->map_from = from;
  5233.  
  5234.       /* Make the real filename absolute.  */
  5235.       if (*to == '/')
  5236.         ptr->map_to = to;
  5237.       else
  5238.         {
  5239.           ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
  5240.           strcpy (ptr->map_to, dirname);
  5241.           ptr->map_to[dirlen] = '/';
  5242.           strcpy (ptr->map_to + dirlen + separator_needed, to);
  5243.           free (to);
  5244.         }          
  5245.  
  5246.       ptr->map_next = map_list_ptr->map_list_map;
  5247.       map_list_ptr->map_list_map = ptr;
  5248.  
  5249.       while ((ch = getc (f)) != '\n')
  5250.         if (ch == EOF)
  5251.           break;
  5252.     }
  5253.       fclose (f);
  5254.     }
  5255.   
  5256.   map_list_ptr->map_list_next = map_list;
  5257.   map_list = map_list_ptr;
  5258.  
  5259.   return map_list_ptr->map_list_map;
  5260. }  
  5261.  
  5262. /* Try to open include file FILENAME.  SEARCHPTR is the directory
  5263.    being tried from the include file search path.  This function maps
  5264.    filenames on file systems based on information read by
  5265.    read_name_map.  */
  5266.  
  5267. static int
  5268. open_include_file (filename, searchptr)
  5269.      char *filename;
  5270.      struct file_name_list *searchptr;
  5271. {
  5272.   register struct file_name_map *map;
  5273.   register char *from;
  5274.   char *p, *dir;
  5275.  
  5276.   if (searchptr && ! searchptr->got_name_map)
  5277.     {
  5278.       searchptr->name_map = read_name_map (searchptr->fname
  5279.                        ? searchptr->fname : ".");
  5280.       searchptr->got_name_map = 1;
  5281.     }
  5282.  
  5283.   /* First check the mapping for the directory we are using.  */
  5284.   if (searchptr && searchptr->name_map)
  5285.     {
  5286.       from = filename;
  5287.       if (searchptr->fname)
  5288.     from += strlen (searchptr->fname) + 1;
  5289.       for (map = searchptr->name_map; map; map = map->map_next)
  5290.     {
  5291.       if (! strcmp (map->map_from, from))
  5292.         {
  5293.           /* Found a match.  */
  5294.           return open (map->map_to, O_RDONLY, 0666);
  5295.         }
  5296.     }
  5297.     }
  5298.  
  5299.   /* Try to find a mapping file for the particular directory we are
  5300.      looking in.  Thus #include <sys/types.h> will look up sys/types.h
  5301.      in /usr/include/header.gcc and look up types.h in
  5302.      /usr/include/sys/header.gcc.  */
  5303.   p = rindex (filename, '/');
  5304. #ifdef DIR_SEPARATOR
  5305.   if (! p) p = rindex (filename, DIR_SEPARATOR);
  5306.   else {
  5307.     char *tmp = rindex (filename, DIR_SEPARATOR);
  5308.     if (tmp != NULL && tmp > p) p = tmp;
  5309.   }
  5310. #endif
  5311.   if (! p)
  5312.     p = filename;
  5313.   if (searchptr
  5314.       && searchptr->fname
  5315.       && strlen (searchptr->fname) == p - filename
  5316.       && ! strncmp (searchptr->fname, filename, p - filename))
  5317.     {
  5318.       /* FILENAME is in SEARCHPTR, which we've already checked.  */
  5319.       return open (filename, O_RDONLY, 0666);
  5320.     }
  5321.  
  5322.   if (p == filename)
  5323.     {
  5324.       dir = ".";
  5325.       from = filename;
  5326.     }
  5327.   else
  5328.     {
  5329.       dir = (char *) alloca (p - filename + 1);
  5330.       bcopy (filename, dir, p - filename);
  5331.       dir[p - filename] = '\0';
  5332.       from = p + 1;
  5333.     }
  5334.   for (map = read_name_map (dir); map; map = map->map_next)
  5335.     if (! strcmp (map->map_from, from))
  5336.       return open (map->map_to, O_RDONLY, 0666);
  5337.  
  5338.   return open (filename, O_RDONLY, 0666);
  5339. }
  5340.  
  5341. /* Process the contents of include file FNAME, already open on descriptor F,
  5342.    with output to OP.
  5343.    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
  5344.    "system" include directories (as decided by the `is_system_include'
  5345.    function above).
  5346.    DIRPTR is the link in the dir path through which this file was found,
  5347.    or 0 if the file name was absolute.  */
  5348.  
  5349. static void
  5350. finclude (f, fname, op, system_header_p, dirptr)
  5351.      int f;
  5352.      char *fname;
  5353.      FILE_BUF *op;
  5354.      int system_header_p;
  5355.      struct file_name_list *dirptr;
  5356. {
  5357.   int st_mode;
  5358.   long st_size;
  5359.   long i;
  5360.   FILE_BUF *fp;            /* For input stack frame */
  5361.   int missing_newline = 0;
  5362.  
  5363.   CHECK_DEPTH (return;);
  5364.  
  5365.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  5366.     {
  5367.       perror_with_name (fname);
  5368.       close (f);
  5369.       return;
  5370.     }
  5371.  
  5372.   fp = &instack[indepth + 1];
  5373.   bzero ((char *) fp, sizeof (FILE_BUF));
  5374.   fp->nominal_fname = fp->fname = fname;
  5375.   fp->length = 0;
  5376.   fp->lineno = 1;
  5377.   fp->if_stack = if_stack;
  5378.   fp->system_header_p = system_header_p;
  5379.   fp->dir = dirptr;
  5380.  
  5381.   if (S_ISREG (st_mode)) {
  5382.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  5383.     fp->bufp = fp->buf;
  5384.  
  5385.     /* Read the file contents, knowing that st_size is an upper bound
  5386.        on the number of bytes we can read.  */
  5387.     fp->length = safe_read (f, (char *) fp->buf, st_size);
  5388.     if (fp->length < 0) goto nope;
  5389.   }
  5390.   else if (S_ISDIR (st_mode)) {
  5391.     error ("directory `%s' specified in #include", fname);
  5392.     close (f);
  5393.     return;
  5394.   } else {
  5395.     /* Cannot count its file size before reading.
  5396.        First read the entire file into heap and
  5397.        copy them into buffer on stack. */
  5398.  
  5399.     int bsize = 2000;
  5400.  
  5401.     st_size = 0;
  5402.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  5403.  
  5404.     for (;;) {
  5405.       i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
  5406.       if (i < 0)
  5407.     goto nope;      /* error! */
  5408.       st_size += i;
  5409.       if (st_size != bsize)
  5410.     break;    /* End of file */
  5411.       bsize *= 2;
  5412.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  5413.     }
  5414.     fp->bufp = fp->buf;
  5415.     fp->length = st_size;
  5416.   }
  5417.  
  5418.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  5419.       /* Backslash-newline at end is not good enough.  */
  5420.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  5421.     fp->buf[fp->length++] = '\n';
  5422.     missing_newline = 1;
  5423.   }
  5424.   fp->buf[fp->length] = '\0';
  5425.  
  5426.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  5427.   close (f);
  5428.  
  5429.   /* Must do this before calling trigraph_pcp, so that the correct file name
  5430.      will be printed in warning messages.  */
  5431.  
  5432.   indepth++;
  5433.   input_file_stack_tick++;
  5434.  
  5435.   if (!no_trigraphs)
  5436.     trigraph_pcp (fp);
  5437.  
  5438. #if defined (NEXT_SEMANTICS) || defined(NEXT_PDO)
  5439.   if (!no_rtf)
  5440.     buf_convert_rtf (fp);
  5441. #endif
  5442.  
  5443.   output_line_directive (fp, op, 0, enter_file);
  5444.   rescan (op, 0);
  5445.  
  5446.   if (missing_newline)
  5447.     fp->lineno--;
  5448.  
  5449.   if (pedantic && missing_newline)
  5450.     pedwarn ("file does not end in newline");
  5451.  
  5452.   indepth--;
  5453.   input_file_stack_tick++;
  5454.   output_line_directive (&instack[indepth], op, 0, leave_file);
  5455.   free (fp->buf);
  5456.   return;
  5457.  
  5458.  nope:
  5459.  
  5460.   perror_with_name (fname);
  5461.   close (f);
  5462.   free (fp->buf);
  5463. }
  5464.  
  5465. /* Record that inclusion of the file named FILE
  5466.    should be controlled by the macro named MACRO_NAME.
  5467.    This means that trying to include the file again
  5468.    will do something if that macro is defined.  */
  5469.  
  5470. static void
  5471. record_control_macro (file, macro_name)
  5472.      char *file;
  5473.      U_CHAR *macro_name;
  5474. {
  5475.   struct file_name_list *new;
  5476.  
  5477.   for (new = all_include_files; new; new = new->next) {
  5478.     if (!strcmp (new->fname, file)) {
  5479.       new->control_macro = macro_name;
  5480.       return;
  5481.     }
  5482.   }
  5483.  
  5484.   /* If the file is not in all_include_files, something's wrong.  */
  5485.   abort ();
  5486. }
  5487.  
  5488. /* Maintain and search list of included files, for #import.  */
  5489.  
  5490. #define IMPORT_HASH_SIZE 31
  5491.  
  5492. struct import_file {
  5493.   char *name;
  5494.   ino_t inode;
  5495.   dev_t dev;
  5496. #ifdef WIN32
  5497.   _off_t size;
  5498.   time_t mtime;
  5499.   time_t ctime;
  5500. #endif WIN32
  5501.   struct import_file *next;
  5502. };
  5503.  
  5504. /* Hash table of files already included with #include or #import.  */
  5505.  
  5506. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  5507.  
  5508. /* Hash a file name for import_hash_table.  */
  5509.  
  5510. static int 
  5511. import_hash (f)
  5512.      char *f;
  5513. {
  5514.   int val = 0;
  5515.  
  5516.   while (*f) val += *f++;
  5517.   return (val%IMPORT_HASH_SIZE);
  5518. }
  5519.  
  5520. #ifdef WINNT
  5521. /* Check to see if two filenames really refer to the same file.
  5522.    Returs 1 if they are, otherwise 0 if they aren't. */
  5523.  
  5524. static int
  5525. check_import_name (char* name1, char* name2)
  5526. {
  5527.     char fullpath1[_MAX_PATH];
  5528.     char fullpath2[_MAX_PATH];
  5529.     char *file1;
  5530.     char *file2;
  5531.  
  5532.     if (GetFullPathName (name1, _MAX_PATH, fullpath1, &file1))
  5533.     {
  5534.         if (GetFullPathName (name2, _MAX_PATH, fullpath2, &file2))
  5535.         {
  5536.             if ( stricmp (fullpath1, fullpath2) == 0)
  5537.             {
  5538.                 return 1;
  5539.             }
  5540.         }
  5541.     }
  5542.  
  5543.     return 0;
  5544. }
  5545. #endif /* WINNT */
  5546.  
  5547. /* Search for file FILENAME in import_hash_table.
  5548.    Return -2 if found, either a matching name or a matching inode.
  5549.    Otherwise, open the file and return a file descriptor if successful
  5550.    or -1 if unsuccessful.  */
  5551.  
  5552. static int
  5553. lookup_import (filename, searchptr)
  5554.      char *filename;
  5555.      struct file_name_list *searchptr;
  5556. {
  5557.   struct import_file *i;
  5558.   int h;
  5559.   int hashval;
  5560.   struct stat sb;
  5561.   int fd;
  5562.  
  5563.   hashval = import_hash (filename);
  5564.  
  5565.   /* Attempt to find file in list of already included files */
  5566.   i = import_hash_table[hashval];
  5567.  
  5568.   while (i) {
  5569.     if (!strcmp (filename, i->name))
  5570.       return -2;        /* return found */
  5571.     i = i->next;
  5572.   }
  5573.   /* Open it and try a match on inode/dev */
  5574.   fd = open_include_file (filename, searchptr);
  5575.   if (fd < 0)
  5576.     return fd;
  5577.   fstat (fd, &sb);
  5578.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  5579.     i = import_hash_table[h];
  5580.     while (i) {
  5581.       /* Compare the inode and the device.
  5582.      Supposedly on some systems the inode is not a scalar.  */
  5583. #if defined(WINNT)
  5584.     /* Windows doesn't have inodes or devices.  The structure that fstat
  5585.        fills in will have zeros in those fields so this call will
  5586.        always fail.  Because of this, the only thing we can really 
  5587.        compare is the size, creation time, modification time, and 
  5588.        filename.  */
  5589.       if ( (i->size == sb.st_size) 
  5590.                 && (i->ctime == sb.st_ctime)
  5591.                 && (i->mtime == sb.st_mtime)
  5592.                 && (check_import_name (i->name, filename))) {
  5593. #else
  5594.       if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
  5595.       && i->dev == sb.st_dev) {
  5596. #endif
  5597.         close (fd);
  5598.         return -2;        /* return found */
  5599.       }
  5600.       i = i->next;
  5601.     }
  5602.   }
  5603.   return fd;            /* Not found, return open file */
  5604. }
  5605.  
  5606. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  5607.  
  5608. static void
  5609. add_import (fd, fname)
  5610.      int fd;
  5611.      char *fname;
  5612. {
  5613.   struct import_file *i;
  5614.   int hashval;
  5615.   struct stat sb;
  5616.  
  5617.   hashval = import_hash (fname);
  5618.   fstat (fd, &sb);
  5619.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  5620.   i->name = xmalloc (strlen (fname)+1);
  5621.   strcpy (i->name, fname);
  5622.   bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
  5623.   i->dev = sb.st_dev;
  5624.   i->next = import_hash_table[hashval];
  5625. #ifdef WIN32
  5626.   i->size = sb.st_size;
  5627.   i->mtime = sb.st_mtime;
  5628.   i->ctime = sb.st_ctime;
  5629. #endif WIN32
  5630.   import_hash_table[hashval] = i;
  5631. }
  5632.  
  5633. /* Load the specified precompiled header into core, and verify its
  5634.    preconditions.  PCF indicates the file descriptor to read, which must
  5635.    be a regular file.  FNAME indicates the file name of the original 
  5636.    header.  *LIMIT will be set to an address one past the end of the file.
  5637.    If the preconditions of the file are not satisfied, the buffer is 
  5638.    freed and we return 0.  If the preconditions are satisfied, return
  5639.    the address of the buffer following the preconditions.  The buffer, in
  5640.    this case, should never be freed because various pieces of it will
  5641.    be referred to until all precompiled strings are output at the end of
  5642.    the run.
  5643. */
  5644. static char *
  5645. check_precompiled (pcf, fname, limit)
  5646.      int pcf;
  5647.      char *fname;
  5648.      char **limit;
  5649. {
  5650.   int st_mode;
  5651.   long st_size;
  5652.   int length = 0;
  5653.   char *buf;
  5654.   char *cp;
  5655.  
  5656.   if (pcp_outfile)
  5657.     return 0;
  5658.   
  5659.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  5660.     return 0;
  5661.  
  5662.   if (S_ISREG (st_mode))
  5663.     {
  5664.       buf = xmalloc (st_size + 2);
  5665.       length = safe_read (pcf, buf, st_size);
  5666.       if (length < 0)
  5667.     goto nope;
  5668.     }
  5669.   else
  5670.     abort ();
  5671.     
  5672.   if (length > 0 && buf[length-1] != '\n')
  5673.     buf[length++] = '\n';
  5674.   buf[length] = '\0';
  5675.   
  5676.   *limit = buf + length;
  5677.  
  5678.   /* File is in core.  Check the preconditions. */
  5679.   if (!check_preconditions (buf))
  5680.     goto nope;
  5681.   for (cp = buf; *cp; cp++)
  5682.     ;
  5683. #ifdef DEBUG_PCP
  5684.   fprintf (stderr, "Using preinclude %s\n", fname);
  5685. #endif
  5686.   return cp + 1;
  5687.  
  5688.  nope:
  5689. #ifdef DEBUG_PCP
  5690.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  5691. #endif
  5692.   free (buf);
  5693.   return 0;
  5694. }
  5695.  
  5696. /* PREC (null terminated) points to the preconditions of a
  5697.    precompiled header.  These are a series of #define and #undef
  5698.    lines which must match the current contents of the hash
  5699.    table.  */
  5700. static int 
  5701. check_preconditions (prec)
  5702.      char *prec;
  5703. {
  5704.   MACRODEF mdef;
  5705.   char *lineend;
  5706.   
  5707.   while (*prec) {
  5708.     lineend = index (prec, '\n');
  5709.     
  5710.     if (*prec++ != '#') {
  5711.       error ("Bad format encountered while reading precompiled file");
  5712.       return 0;
  5713.     }
  5714.     if (!strncmp (prec, "define", 6)) {
  5715.       HASHNODE *hp;
  5716.       
  5717.       prec += 6;
  5718.       mdef = create_definition ((U_CHAR *) prec, (U_CHAR *) lineend, NULL_PTR);
  5719.  
  5720.       if (mdef.defn == 0)
  5721.     abort ();
  5722.       
  5723.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  5724.       || (hp->type != T_MACRO && hp->type != T_CONST)
  5725.       || (hp->type == T_MACRO
  5726.           && !compare_defs (mdef.defn, hp->value.defn)
  5727.           && (mdef.defn->length != 2
  5728.           || mdef.defn->expansion[0] != '\n'
  5729.           || mdef.defn->expansion[1] != ' ')))
  5730.     return 0;
  5731.     } else if (!strncmp (prec, "undef", 5)) {
  5732.       char *name;
  5733.       int len;
  5734.       
  5735.       prec += 5;
  5736.       while (is_hor_space[(U_CHAR) *prec])
  5737.     prec++;
  5738.       name = prec;
  5739.       while (is_idchar[(U_CHAR) *prec])
  5740.     prec++;
  5741.       len = prec - name;
  5742.       
  5743.       if (lookup ((U_CHAR *) name, len, -1))
  5744.     return 0;
  5745.     } else {
  5746.       error ("Bad format encountered while reading precompiled file");
  5747.       return 0;
  5748.     }
  5749.     prec = lineend + 1;
  5750.   }
  5751.   /* They all passed successfully */
  5752.   return 1;
  5753. }
  5754.  
  5755. /* Process the main body of a precompiled file.  BUF points to the
  5756.    string section of the file, following the preconditions.  LIMIT is one
  5757.    character past the end.  NAME is the name of the file being read
  5758.    in.  OP is the main output buffer */
  5759. static void
  5760. pcfinclude (buf, limit, name, op)
  5761.      U_CHAR *buf, *limit, *name;
  5762.      FILE_BUF *op;
  5763. {
  5764.   FILE_BUF tmpbuf;
  5765.   int nstrings;
  5766.   U_CHAR *cp = buf;
  5767.  
  5768.   /* First in the file comes 4 bytes indicating the number of strings, */
  5769.   /* in network byte order. (MSB first).  */
  5770.   nstrings = *cp++;
  5771.   nstrings = (nstrings << 8) | *cp++;
  5772.   nstrings = (nstrings << 8) | *cp++;
  5773.   nstrings = (nstrings << 8) | *cp++;
  5774.   
  5775.   /* Looping over each string... */
  5776.   while (nstrings--) {
  5777.     U_CHAR *string_start;
  5778.     U_CHAR *endofthiskey;
  5779.     STRINGDEF *str;
  5780.     int nkeys;
  5781.     
  5782.     /* Each string starts with a STRINGDEF structure (str), followed */
  5783.     /* by the text of the string (string_start) */
  5784.  
  5785.     /* First skip to a longword boundary */
  5786.     /* ??? Why a 4-byte boundary?  On all machines? */
  5787.     /* NOTE: This works correctly even if HOST_WIDE_INT
  5788.        is narrower than a pointer.
  5789.        Do not try risky measures here to get another type to use!
  5790.        Do not include stddef.h--it will fail!  */
  5791.     if ((HOST_WIDE_INT) cp & 3)
  5792.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  5793.     
  5794.     /* Now get the string. */
  5795.     str = (STRINGDEF *) (GENERIC_PTR) cp;
  5796.     string_start = cp += sizeof (STRINGDEF);
  5797.     
  5798.     for (; *cp; cp++)        /* skip the string */
  5799.       ;
  5800.     
  5801.     /* We need to macro expand the string here to ensure that the
  5802.        proper definition environment is in place.  If it were only
  5803.        expanded when we find out it is needed, macros necessary for
  5804.        its proper expansion might have had their definitions changed. */
  5805.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  5806.     /* Lineno is already set in the precompiled file */
  5807.     str->contents = tmpbuf.buf;
  5808.     str->len = tmpbuf.length;
  5809.     str->writeflag = 0;
  5810.     str->filename = name;
  5811.     str->output_mark = outbuf.bufp - outbuf.buf;
  5812.     
  5813.     str->chain = 0;
  5814.     *stringlist_tailp = str;
  5815.     stringlist_tailp = &str->chain;
  5816.     
  5817.     /* Next comes a fourbyte number indicating the number of keys */
  5818.     /* for this string. */
  5819.     nkeys = *cp++;
  5820.     nkeys = (nkeys << 8) | *cp++;
  5821.     nkeys = (nkeys << 8) | *cp++;
  5822.     nkeys = (nkeys << 8) | *cp++;
  5823.  
  5824.     /* If this number is -1, then the string is mandatory. */
  5825.     if (nkeys == -1)
  5826.       str->writeflag = 1;
  5827.     else
  5828.       /* Otherwise, for each key, */
  5829.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  5830.     KEYDEF *kp = (KEYDEF *) (GENERIC_PTR) cp;
  5831.     HASHNODE *hp;
  5832.     
  5833.     /* It starts with a KEYDEF structure */
  5834.     cp += sizeof (KEYDEF);
  5835.     
  5836.     /* Find the end of the key.  At the end of this for loop we
  5837.        advance CP to the start of the next key using this variable. */
  5838.     endofthiskey = cp + strlen ((char *) cp);
  5839.     kp->str = str;
  5840.     
  5841.     /* Expand the key, and enter it into the hash table. */
  5842.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  5843.     tmpbuf.bufp = tmpbuf.buf;
  5844.     
  5845.     while (is_hor_space[*tmpbuf.bufp])
  5846.       tmpbuf.bufp++;
  5847.     if (!is_idstart[*tmpbuf.bufp]
  5848.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  5849.       str->writeflag = 1;
  5850.       continue;
  5851.     }
  5852.         
  5853.     hp = lookup (tmpbuf.bufp, -1, -1);
  5854.     if (hp == NULL) {
  5855.       kp->chain = 0;
  5856.       install (tmpbuf.bufp, -1, T_PCSTRING, (char *) kp, -1);
  5857.     }
  5858.     else if (hp->type == T_PCSTRING) {
  5859.       kp->chain = hp->value.keydef;
  5860.       hp->value.keydef = kp;
  5861.     }
  5862.     else
  5863.       str->writeflag = 1;
  5864.       }
  5865.   }
  5866.   /* This output_line_directive serves to switch us back to the current
  5867.      input file in case some of these strings get output (which will 
  5868.      result in line directives for the header file being output). */
  5869.   output_line_directive (&instack[indepth], op, 0, enter_file);
  5870. }
  5871.  
  5872. /* Called from rescan when it hits a key for strings.  Mark them all */
  5873.  /* used and clean up. */
  5874. static void
  5875. pcstring_used (hp)
  5876.      HASHNODE *hp;
  5877. {
  5878.   KEYDEF *kp;
  5879.   
  5880.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  5881.     kp->str->writeflag = 1;
  5882.   delete_macro (hp);
  5883. }
  5884.  
  5885. /* Write the output, interspersing precompiled strings in their */
  5886.  /* appropriate places. */
  5887. static void
  5888. write_output ()
  5889. {
  5890.   STRINGDEF *next_string;
  5891.   U_CHAR *cur_buf_loc;
  5892.   int line_directive_len = 80;
  5893.   char *line_directive = xmalloc (line_directive_len);
  5894.   int len;
  5895.  
  5896.   /* In each run through the loop, either cur_buf_loc == */
  5897.   /* next_string_loc, in which case we print a series of strings, or */
  5898.   /* it is less than next_string_loc, in which case we write some of */
  5899.   /* the buffer. */
  5900.   cur_buf_loc = outbuf.buf; 
  5901.   next_string = stringlist;
  5902.   
  5903.   while (cur_buf_loc < outbuf.bufp || next_string) {
  5904.     if (next_string
  5905.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  5906.       if (next_string->writeflag) {
  5907.     len = 4 * strlen ((char *) next_string->filename) + 32;
  5908.     while (len > line_directive_len)
  5909.       line_directive = xrealloc (line_directive, 
  5910.                      line_directive_len *= 2);
  5911.     sprintf (line_directive, "\n# %d ", next_string->lineno);
  5912.     strcpy (quote_string (line_directive + strlen (line_directive),
  5913.                   (char *) next_string->filename),
  5914.         "\n");
  5915.     safe_write (fileno (stdout), line_directive, strlen (line_directive));
  5916.     safe_write (fileno (stdout),
  5917.             (char *) next_string->contents, next_string->len);
  5918.       }          
  5919.       next_string = next_string->chain;
  5920.     }
  5921.     else {
  5922.       len = (next_string
  5923.          ? (next_string->output_mark 
  5924.         - (cur_buf_loc - outbuf.buf))
  5925.          : outbuf.bufp - cur_buf_loc);
  5926.       
  5927.       safe_write (fileno (stdout), (char *) cur_buf_loc, len);
  5928.       cur_buf_loc += len;
  5929.     }
  5930.   }
  5931.   free (line_directive);
  5932. }
  5933.  
  5934. /* Pass a directive through to the output file.
  5935.    BUF points to the contents of the directive, as a contiguous string.
  5936.    LIMIT points to the first character past the end of the directive.
  5937.    KEYWORD is the keyword-table entry for the directive.  */
  5938.  
  5939. static void
  5940. pass_thru_directive (buf, limit, op, keyword)
  5941.      U_CHAR *buf, *limit;
  5942.      FILE_BUF *op;
  5943.      struct directive *keyword;
  5944. {
  5945.   register unsigned keyword_length = keyword->length;
  5946.  
  5947.   check_expand (op, 1 + keyword_length + (limit - buf));
  5948.   *op->bufp++ = '#';
  5949.   bcopy (keyword->name, (char *) op->bufp, keyword_length);
  5950.   op->bufp += keyword_length;
  5951.   if (limit != buf && buf[0] != ' ')
  5952.     *op->bufp++ = ' ';
  5953.   bcopy ((char *) buf, (char *) op->bufp, limit - buf);
  5954.   op->bufp += (limit - buf);
  5955. #if 0
  5956.   *op->bufp++ = '\n';
  5957.   /* Count the line we have just made in the output,
  5958.      to get in sync properly.  */
  5959.   op->lineno++;
  5960. #endif
  5961. }
  5962.  
  5963. /* The arglist structure is built by do_define to tell
  5964.    collect_definition where the argument names begin.  That
  5965.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  5966.    would contain pointers to the strings x, y, and z.
  5967.    Collect_definition would then build a DEFINITION node,
  5968.    with reflist nodes pointing to the places x, y, and z had
  5969.    appeared.  So the arglist is just convenience data passed
  5970.    between these two routines.  It is not kept around after
  5971.    the current #define has been processed and entered into the
  5972.    hash table. */
  5973.  
  5974. struct arglist {
  5975.   struct arglist *next;
  5976.   U_CHAR *name;
  5977.   int length;
  5978.   int argno;
  5979.   char rest_args;
  5980. };
  5981.  
  5982. /* Create a DEFINITION node from a #define directive.  Arguments are 
  5983.    as for do_define. */
  5984. static MACRODEF
  5985. create_definition (buf, limit, op)
  5986.      U_CHAR *buf, *limit;
  5987.      FILE_BUF *op;
  5988. {
  5989.   U_CHAR *bp;            /* temp ptr into input buffer */
  5990.   U_CHAR *symname;        /* remember where symbol name starts */
  5991.   int sym_length;        /* and how long it is */
  5992.   int line = instack[indepth].lineno;
  5993.   char *file = instack[indepth].nominal_fname;
  5994.   int rest_args = 0;
  5995.  
  5996.   DEFINITION *defn;
  5997.   int arglengths = 0;        /* Accumulate lengths of arg names
  5998.                    plus number of args.  */
  5999.   MACRODEF mdef;
  6000.  
  6001.   bp = buf;
  6002.  
  6003.   while (is_hor_space[*bp])
  6004.     bp++;
  6005.  
  6006.   symname = bp;            /* remember where it starts */
  6007.   sym_length = check_macro_name (bp, "macro");
  6008.   bp += sym_length;
  6009.  
  6010.   /* Lossage will occur if identifiers or control keywords are broken
  6011.      across lines using backslash.  This is not the right place to take
  6012.      care of that. */
  6013.  
  6014.   if (*bp == '(') {
  6015.     struct arglist *arg_ptrs = NULL;
  6016.     int argno = 0;
  6017.  
  6018.     bp++;            /* skip '(' */
  6019.     SKIP_WHITE_SPACE (bp);
  6020.  
  6021.     /* Loop over macro argument names.  */
  6022.     while (*bp != ')') {
  6023.       struct arglist *temp;
  6024.  
  6025.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  6026.       temp->name = bp;
  6027.       temp->next = arg_ptrs;
  6028.       temp->argno = argno++;
  6029.       temp->rest_args = 0;
  6030.       arg_ptrs = temp;
  6031.  
  6032.       if (rest_args)
  6033.     pedwarn ("another parameter follows `%s'",
  6034.          rest_extension);
  6035.  
  6036.       if (!is_idstart[*bp])
  6037.     pedwarn ("invalid character in macro parameter name");
  6038.       
  6039.       /* Find the end of the arg name.  */
  6040.       while (is_idchar[*bp]) {
  6041.     bp++;
  6042.     /* do we have a "special" rest-args extension here? */
  6043.     if (limit - bp > REST_EXTENSION_LENGTH &&
  6044.         bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  6045.       rest_args = 1;
  6046.       temp->rest_args = 1;
  6047.       break;
  6048.     }
  6049.       }
  6050.       temp->length = bp - temp->name;
  6051.       if (rest_args == 1)
  6052.     bp += REST_EXTENSION_LENGTH;
  6053.       arglengths += temp->length + 2;
  6054.       SKIP_WHITE_SPACE (bp);
  6055.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  6056.     error ("badly punctuated parameter list in `#define'");
  6057.     goto nope;
  6058.       }
  6059.       if (*bp == ',') {
  6060.     bp++;
  6061.     SKIP_WHITE_SPACE (bp);
  6062.     /* A comma at this point can only be followed by an identifier.  */
  6063.     if (!is_idstart[*bp]) {
  6064.       error ("badly punctuated parameter list in `#define'");
  6065.       goto nope;
  6066.     }
  6067.       }
  6068.       if (bp >= limit) {
  6069.     error ("unterminated parameter list in `#define'");
  6070.     goto nope;
  6071.       }
  6072.       {
  6073.     struct arglist *otemp;
  6074.  
  6075.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  6076.       if (temp->length == otemp->length &&
  6077.           bcmp (temp->name, otemp->name, temp->length) == 0) {
  6078.           error ("duplicate argument name `%.*s' in `#define'",
  6079.              temp->length, temp->name);
  6080.           goto nope;
  6081.       }
  6082.       }
  6083.     }
  6084.  
  6085.     ++bp;            /* skip paren */
  6086.     SKIP_WHITE_SPACE (bp);
  6087.     /* now everything from bp before limit is the definition. */
  6088.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  6089.     defn->rest_args = rest_args;
  6090.  
  6091.     /* Now set defn->args.argnames to the result of concatenating
  6092.        the argument names in reverse order
  6093.        with comma-space between them.  */
  6094.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  6095.     {
  6096.       struct arglist *temp;
  6097.       int i = 0;
  6098.       for (temp = arg_ptrs; temp; temp = temp->next) {
  6099.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  6100.     i += temp->length;
  6101.     if (temp->next != 0) {
  6102.       defn->args.argnames[i++] = ',';
  6103.       defn->args.argnames[i++] = ' ';
  6104.     }
  6105.       }
  6106.       defn->args.argnames[i] = 0;
  6107.     }
  6108.   } else {
  6109.     /* Simple expansion or empty definition.  */
  6110.  
  6111.     if (bp < limit)
  6112.       {
  6113.     if (is_hor_space[*bp]) {
  6114.       bp++;
  6115.       SKIP_WHITE_SPACE (bp);
  6116.     } else {
  6117.       switch (*bp) {
  6118.         case '!':  case '"':  case '#':  case '%':  case '&':  case '\'':
  6119.         case ')':  case '*':  case '+':  case ',':  case '-':  case '.':
  6120.         case '/':  case ':':  case ';':  case '<':  case '=':  case '>':
  6121.         case '?':  case '[':  case '\\': case ']':  case '^':  case '{':
  6122.         case '|':  case '}':  case '~':
  6123.           warning ("missing white space after `#define %.*s'",
  6124.                sym_length, symname);
  6125.           break;
  6126.  
  6127.         default:
  6128.           pedwarn ("missing white space after `#define %.*s'",
  6129.                sym_length, symname);
  6130.           break;
  6131.       }
  6132.     }
  6133.       }
  6134.     /* Now everything from bp before limit is the definition. */
  6135.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  6136.     defn->args.argnames = (U_CHAR *) "";
  6137.   }
  6138.  
  6139.   defn->line = line;
  6140.   defn->file = file;
  6141.  
  6142.   /* OP is null if this is a predefinition */
  6143.   defn->predefined = !op;
  6144.   mdef.defn = defn;
  6145.   mdef.symnam = symname;
  6146.   mdef.symlen = sym_length;
  6147.  
  6148.   return mdef;
  6149.  
  6150.  nope:
  6151.   mdef.defn = 0;
  6152.   return mdef;
  6153. }
  6154.  
  6155. /* Process a #define directive.
  6156. BUF points to the contents of the #define directive, as a contiguous string.
  6157. LIMIT points to the first character past the end of the definition.
  6158. KEYWORD is the keyword-table entry for #define.  */
  6159.  
  6160. static int
  6161. do_define (buf, limit, op, keyword)
  6162.      U_CHAR *buf, *limit;
  6163.      FILE_BUF *op;
  6164.      struct directive *keyword;
  6165. {
  6166.   int hashcode;
  6167.   MACRODEF mdef;
  6168.  
  6169.   /* If this is a precompiler run (with -pcp) pass thru #define directives.  */
  6170.   if (pcp_outfile && op)
  6171.     pass_thru_directive (buf, limit, op, keyword);
  6172.  
  6173.   mdef = create_definition (buf, limit, op);
  6174.   if (mdef.defn == 0)
  6175.     goto nope;
  6176.  
  6177.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  6178.  
  6179.   {
  6180.     HASHNODE *hp;
  6181.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  6182.       int ok = 0;
  6183.       /* Redefining a precompiled key is ok.  */
  6184.       if (hp->type == T_PCSTRING)
  6185.     ok = 1;
  6186.       /* Redefining a macro is ok if the definitions are the same.  */
  6187.       else if (hp->type == T_MACRO)
  6188.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  6189.       /* Redefining a constant is ok with -D.  */
  6190.       else if (hp->type == T_CONST)
  6191.         ok = ! done_initializing;
  6192.       /* Print the warning if it's not ok.  */
  6193.       if (!ok) {
  6194.         /* If we are passing through #define and #undef directives, do
  6195.        that for this re-definition now.  */
  6196.         if (debug_output && op)
  6197.       pass_thru_directive (buf, limit, op, keyword);
  6198.  
  6199.     pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
  6200.     if (hp->type == T_MACRO)
  6201.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  6202.                       "this is the location of the previous definition");
  6203.       }
  6204.       /* Replace the old definition.  */
  6205.       hp->type = T_MACRO;
  6206.       hp->value.defn = mdef.defn;
  6207.     } else {
  6208.       /* If we are passing through #define and #undef directives, do
  6209.      that for this new definition now.  */
  6210.       if (debug_output && op)
  6211.     pass_thru_directive (buf, limit, op, keyword);
  6212.       install (mdef.symnam, mdef.symlen, T_MACRO,
  6213.            (char *) mdef.defn, hashcode);
  6214.     }
  6215.   }
  6216.  
  6217.   return 0;
  6218.  
  6219. nope:
  6220.  
  6221.   return 1;
  6222. }
  6223.  
  6224. /* Check a purported macro name SYMNAME, and yield its length.
  6225.    USAGE is the kind of name this is intended for.  */
  6226.  
  6227. static int
  6228. check_macro_name (symname, usage)
  6229.      U_CHAR *symname;
  6230.      char *usage;
  6231. {
  6232.   U_CHAR *p;
  6233.   int sym_length;
  6234.  
  6235.   for (p = symname; is_idchar[*p]; p++)
  6236.     ;
  6237.   sym_length = p - symname;
  6238.   if (sym_length == 0)
  6239.     error ("invalid %s name", usage);
  6240.   else if (!is_idstart[*symname]
  6241.        || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
  6242.     error ("invalid %s name `%.*s'", usage, sym_length, symname);
  6243.   return sym_length;
  6244. }
  6245.  
  6246. /*
  6247.  * return zero if two DEFINITIONs are isomorphic
  6248.  */
  6249. static int
  6250. compare_defs (d1, d2)
  6251.      DEFINITION *d1, *d2;
  6252. {
  6253.   register struct reflist *a1, *a2;
  6254.   register U_CHAR *p1 = d1->expansion;
  6255.   register U_CHAR *p2 = d2->expansion;
  6256.   int first = 1;
  6257.  
  6258.   if (d1->nargs != d2->nargs)
  6259.     return 1;
  6260.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  6261.     return 1;
  6262.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  6263.        a1 = a1->next, a2 = a2->next) {
  6264.     if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
  6265.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  6266.     || a1->argno != a2->argno
  6267.     || a1->stringify != a2->stringify
  6268.     || a1->raw_before != a2->raw_before
  6269.     || a1->raw_after != a2->raw_after)
  6270.       return 1;
  6271.     first = 0;
  6272.     p1 += a1->nchars;
  6273.     p2 += a2->nchars;
  6274.   }
  6275.   if (a1 != a2)
  6276.     return 1;
  6277.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  6278.              p2, d2->length - (p2 - d2->expansion), 1))
  6279.     return 1;
  6280.   return 0;
  6281. }
  6282.  
  6283. /* Return 1 if two parts of two macro definitions are effectively different.
  6284.    One of the parts starts at BEG1 and has LEN1 chars;
  6285.    the other has LEN2 chars at BEG2.
  6286.    Any sequence of whitespace matches any other sequence of whitespace.
  6287.    FIRST means these parts are the first of a macro definition;
  6288.     so ignore leading whitespace entirely.
  6289.    LAST means these parts are the last of a macro definition;
  6290.     so ignore trailing whitespace entirely.  */
  6291.  
  6292. static int
  6293. comp_def_part (first, beg1, len1, beg2, len2, last)
  6294.      int first;
  6295.      U_CHAR *beg1, *beg2;
  6296.      int len1, len2;
  6297.      int last;
  6298. {
  6299.   register U_CHAR *end1 = beg1 + len1;
  6300.   register U_CHAR *end2 = beg2 + len2;
  6301.   if (first) {
  6302.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  6303.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  6304.   }
  6305.   if (last) {
  6306.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  6307.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  6308.   }
  6309.   while (beg1 != end1 && beg2 != end2) {
  6310.     if (is_space[*beg1] && is_space[*beg2]) {
  6311.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  6312.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  6313.     } else if (*beg1 == *beg2) {
  6314.       beg1++; beg2++;
  6315.     } else break;
  6316.   }
  6317.   return (beg1 != end1) || (beg2 != end2);
  6318. }
  6319.  
  6320. /* Read a replacement list for a macro with parameters.
  6321.    Build the DEFINITION structure.
  6322.    Reads characters of text starting at BUF until END.
  6323.    ARGLIST specifies the formal parameters to look for
  6324.    in the text of the definition; NARGS is the number of args
  6325.    in that list, or -1 for a macro name that wants no argument list.
  6326.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  6327.    and NAMELEN is its length in characters.
  6328.    
  6329. Note that comments, backslash-newlines, and leading white space
  6330. have already been deleted from the argument.  */
  6331.  
  6332. /* If there is no trailing whitespace, a Newline Space is added at the end
  6333.    to prevent concatenation that would be contrary to the standard.  */
  6334.  
  6335. static DEFINITION *
  6336. collect_expansion (buf, end, nargs, arglist)
  6337.      U_CHAR *buf, *end;
  6338.      int nargs;
  6339.      struct arglist *arglist;
  6340. {
  6341.   DEFINITION *defn;
  6342.   register U_CHAR *p, *limit, *lastp, *exp_p;
  6343.   struct reflist *endpat = NULL;
  6344.   /* Pointer to first nonspace after last ## seen.  */
  6345.   U_CHAR *concat = 0;
  6346.   /* Pointer to first nonspace after last single-# seen.  */
  6347.   U_CHAR *stringify = 0;
  6348.   /* How those tokens were spelled.  */
  6349.   enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
  6350.   enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
  6351.   int maxsize;
  6352.   int expected_delimiter = '\0';
  6353.  
  6354.   /* Scan thru the replacement list, ignoring comments and quoted
  6355.      strings, picking up on the macro calls.  It does a linear search
  6356.      thru the arg list on every potential symbol.  Profiling might say
  6357.      that something smarter should happen. */
  6358.  
  6359.   if (end < buf)
  6360.     abort ();
  6361.  
  6362.   /* Find the beginning of the trailing whitespace.  */
  6363.   limit = end;
  6364.   p = buf;
  6365.   while (p < limit && is_space[limit[-1]]) limit--;
  6366.  
  6367.   /* Allocate space for the text in the macro definition.
  6368.      Each input char may or may not need 1 byte,
  6369.      so this is an upper bound.
  6370.      The extra 3 are for invented trailing newline-marker and final null.  */
  6371.   maxsize = (sizeof (DEFINITION)
  6372.          + (limit - p) + 3);
  6373.   defn = (DEFINITION *) xcalloc (1, maxsize);
  6374.  
  6375.   defn->nargs = nargs;
  6376.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  6377.   lastp = exp_p;
  6378.  
  6379.   if (p[0] == '#'
  6380.       ? p[1] == '#'
  6381.       : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
  6382.     error ("`##' at start of macro definition");
  6383.     p += p[0] == '#' ? 2 : 4;
  6384.   }
  6385.  
  6386.   /* Process the main body of the definition.  */
  6387.   while (p < limit) {
  6388.     int skipped_arg = 0;
  6389.     register U_CHAR c = *p++;
  6390.  
  6391.     *exp_p++ = c;
  6392.  
  6393.     if (!traditional) {
  6394.       switch (c) {
  6395.       case '\'':
  6396.       case '\"':
  6397.         if (expected_delimiter != '\0') {
  6398.           if (c == expected_delimiter)
  6399.             expected_delimiter = '\0';
  6400.         } else
  6401.           expected_delimiter = c;
  6402.     break;
  6403.  
  6404.       case '\\':
  6405.     if (p < limit && expected_delimiter) {
  6406.       /* In a string, backslash goes through
  6407.          and makes next char ordinary.  */
  6408.       *exp_p++ = *p++;
  6409.     }
  6410.     break;
  6411.  
  6412.       case '%':
  6413.     if (!expected_delimiter && *p == ':') {
  6414.       /* %: is not a digraph if preceded by an odd number of '<'s.  */
  6415.       U_CHAR *p0 = p - 1;
  6416.       while (buf < p0 && p0[-1] == '<')
  6417.         p0--;
  6418.       if ((p - p0) & 1) {
  6419.         /* Treat %:%: as ## and %: as #.  */
  6420.         if (p[1] == '%' && p[2] == ':') {
  6421.           p += 2;
  6422.           goto sharp_sharp_token;
  6423.         }
  6424.         if (nargs >= 0) {
  6425.           p++;
  6426.           goto sharp_token;
  6427.         }
  6428.       }
  6429.     }
  6430.     break;
  6431.  
  6432.       case '#':
  6433.     /* # is ordinary inside a string.  */
  6434.     if (expected_delimiter)
  6435.       break;
  6436.     if (*p == '#') {
  6437.     sharp_sharp_token:
  6438.       /* ##: concatenate preceding and following tokens.  */
  6439.       /* Take out the first #, discard preceding whitespace.  */
  6440.       exp_p--;
  6441.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  6442.         --exp_p;
  6443.       /* Skip the second #.  */
  6444.       p++;
  6445.       concat_sharp_token_type = c;
  6446.       if (is_hor_space[*p]) {
  6447.         concat_sharp_token_type = c + 1;
  6448.         p++;
  6449.         SKIP_WHITE_SPACE (p);
  6450.       }
  6451.       concat = p;
  6452.       if (p == limit)
  6453.         error ("`##' at end of macro definition");
  6454.     } else if (nargs >= 0) {
  6455.       /* Single #: stringify following argument ref.
  6456.          Don't leave the # in the expansion.  */
  6457.     sharp_token:
  6458.       exp_p--;
  6459.       stringify_sharp_token_type = c;
  6460.       if (is_hor_space[*p]) {
  6461.         stringify_sharp_token_type = c + 1;
  6462.         p++;
  6463.         SKIP_WHITE_SPACE (p);
  6464.       }
  6465.       if (! is_idstart[*p] || nargs == 0)
  6466.         error ("`#' operator is not followed by a macro argument name");
  6467.       else
  6468.         stringify = p;
  6469.     }
  6470.     break;
  6471.       }
  6472.     } else {
  6473.       /* In -traditional mode, recognize arguments inside strings and
  6474.      and character constants, and ignore special properties of #.
  6475.      Arguments inside strings are considered "stringified", but no
  6476.      extra quote marks are supplied.  */
  6477.       switch (c) {
  6478.       case '\'':
  6479.       case '\"':
  6480.     if (expected_delimiter != '\0') {
  6481.       if (c == expected_delimiter)
  6482.         expected_delimiter = '\0';
  6483.     } else
  6484.       expected_delimiter = c;
  6485.     break;
  6486.  
  6487.       case '\\':
  6488.     /* Backslash quotes delimiters and itself, but not macro args.  */
  6489.     if (expected_delimiter != 0 && p < limit
  6490.         && (*p == expected_delimiter || *p == '\\')) {
  6491.       *exp_p++ = *p++;
  6492.       continue;
  6493.     }
  6494.     break;
  6495.  
  6496.       case '/':
  6497.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  6498.       break;
  6499.     if (*p == '*') {
  6500.       /* If we find a comment that wasn't removed by handle_directive,
  6501.          this must be -traditional.  So replace the comment with
  6502.          nothing at all.  */
  6503.       exp_p--;
  6504.       p += 1;
  6505.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  6506.         p++;
  6507. #if 0
  6508.       /* Mark this as a concatenation-point, as if it had been ##.  */
  6509.       concat = p;
  6510. #endif
  6511.     }
  6512.     break;
  6513.       }
  6514.     }
  6515.  
  6516.     /* Handle the start of a symbol.  */
  6517.     if (is_idchar[c] && nargs > 0) {
  6518.       U_CHAR *id_beg = p - 1;
  6519.       int id_len;
  6520.  
  6521.       --exp_p;
  6522.       while (p != limit && is_idchar[*p]) p++;
  6523.       id_len = p - id_beg;
  6524.  
  6525.       if (is_idstart[c]) {
  6526.     register struct arglist *arg;
  6527.  
  6528.     for (arg = arglist; arg != NULL; arg = arg->next) {
  6529.       struct reflist *tpat;
  6530.  
  6531.       if (arg->name[0] == c
  6532.           && arg->length == id_len
  6533.           && bcmp (arg->name, id_beg, id_len) == 0) {
  6534.         enum sharp_token_type tpat_stringify;
  6535.         if (expected_delimiter) {
  6536.           if (warn_stringify) {
  6537.         if (traditional) {
  6538.           warning ("macro argument `%.*s' is stringified.",
  6539.                id_len, arg->name);
  6540.         } else {
  6541.           warning ("macro arg `%.*s' would be stringified with -traditional.",
  6542.                id_len, arg->name);
  6543.         }
  6544.           }
  6545.           /* If ANSI, don't actually substitute inside a string.  */
  6546.           if (!traditional)
  6547.         break;
  6548.           tpat_stringify = SHARP_TOKEN;
  6549.         } else {
  6550.           tpat_stringify
  6551.         = (stringify == id_beg
  6552.            ? stringify_sharp_token_type : NO_SHARP_TOKEN);
  6553.         }
  6554.         /* make a pat node for this arg and append it to the end of
  6555.            the pat list */
  6556.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  6557.         tpat->next = NULL;
  6558.         tpat->raw_before
  6559.           = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
  6560.         tpat->raw_after = NO_SHARP_TOKEN;
  6561.         tpat->rest_args = arg->rest_args;
  6562.         tpat->stringify = tpat_stringify;
  6563.  
  6564.         if (endpat == NULL)
  6565.           defn->pattern = tpat;
  6566.         else
  6567.           endpat->next = tpat;
  6568.         endpat = tpat;
  6569.  
  6570.         tpat->argno = arg->argno;
  6571.         tpat->nchars = exp_p - lastp;
  6572.         {
  6573.           register U_CHAR *p1 = p;
  6574.           SKIP_WHITE_SPACE (p1);
  6575.           if (p1[0]=='#'
  6576.               ? p1[1]=='#'
  6577.           : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
  6578.         tpat->raw_after = p1[0] + (p != p1);
  6579.         }
  6580.         lastp = exp_p;    /* place to start copying from next time */
  6581.         skipped_arg = 1;
  6582.         break;
  6583.       }
  6584.     }
  6585.       }
  6586.  
  6587.       /* If this was not a macro arg, copy it into the expansion.  */
  6588.       if (! skipped_arg) {
  6589.     register U_CHAR *lim1 = p;
  6590.     p = id_beg;
  6591.     while (p != lim1)
  6592.       *exp_p++ = *p++;
  6593.     if (stringify == id_beg)
  6594.       error ("`#' operator should be followed by a macro argument name");
  6595.       }
  6596.     }
  6597.   }
  6598.  
  6599.   if (!traditional && expected_delimiter == 0) {
  6600.     /* If ANSI, put in a newline-space marker to prevent token pasting.
  6601.        But not if "inside a string" (which in ANSI mode happens only for
  6602.        -D option).  */
  6603.     *exp_p++ = '\n';
  6604.     *exp_p++ = ' ';
  6605.   }
  6606.  
  6607.   *exp_p = '\0';
  6608.  
  6609.   defn->length = exp_p - defn->expansion;
  6610.  
  6611.   /* Crash now if we overrun the allocated size.  */
  6612.   if (defn->length + 1 > maxsize)
  6613.     abort ();
  6614.  
  6615. #if 0
  6616. /* This isn't worth the time it takes.  */
  6617.   /* give back excess storage */
  6618.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  6619. #endif
  6620.  
  6621.   return defn;
  6622. }
  6623.  
  6624. static int
  6625. do_assert (buf, limit, op, keyword)
  6626.      U_CHAR *buf, *limit;
  6627.      FILE_BUF *op;
  6628.      struct directive *keyword;
  6629. {
  6630.   U_CHAR *bp;            /* temp ptr into input buffer */
  6631.   U_CHAR *symname;        /* remember where symbol name starts */
  6632.   int sym_length;        /* and how long it is */
  6633.   struct arglist *tokens = NULL;
  6634.  
  6635.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6636.     pedwarn ("ANSI C does not allow `#assert'");
  6637.  
  6638.   bp = buf;
  6639.  
  6640.   while (is_hor_space[*bp])
  6641.     bp++;
  6642.  
  6643.   symname = bp;            /* remember where it starts */
  6644.   sym_length = check_macro_name (bp, "assertion");
  6645.   bp += sym_length;
  6646.   /* #define doesn't do this, but we should.  */
  6647.   SKIP_WHITE_SPACE (bp);
  6648.  
  6649.   /* Lossage will occur if identifiers or control tokens are broken
  6650.      across lines using backslash.  This is not the right place to take
  6651.      care of that. */
  6652.  
  6653.   if (*bp != '(') {
  6654.     error ("missing token-sequence in `#assert'");
  6655.     return 1;
  6656.   }
  6657.  
  6658.   {
  6659.     int error_flag = 0;
  6660.  
  6661.     bp++;            /* skip '(' */
  6662.     SKIP_WHITE_SPACE (bp);
  6663.  
  6664.     tokens = read_token_list (&bp, limit, &error_flag);
  6665.     if (error_flag)
  6666.       return 1;
  6667.     if (tokens == 0) {
  6668.       error ("empty token-sequence in `#assert'");
  6669.       return 1;
  6670.     }
  6671.  
  6672.     ++bp;            /* skip paren */
  6673.     SKIP_WHITE_SPACE (bp);
  6674.   }
  6675.  
  6676.   /* If this name isn't already an assertion name, make it one.
  6677.      Error if it was already in use in some other way.  */
  6678.  
  6679.   {
  6680.     ASSERTION_HASHNODE *hp;
  6681.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6682.     struct tokenlist_list *value
  6683.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  6684.  
  6685.     hp = assertion_lookup (symname, sym_length, hashcode);
  6686.     if (hp == NULL) {
  6687.       if (sym_length == 7 && ! bcmp (symname, "defined", 7))
  6688.     error ("`defined' redefined as assertion");
  6689.       hp = assertion_install (symname, sym_length, hashcode);
  6690.     }
  6691.  
  6692.     /* Add the spec'd token-sequence to the list of such.  */
  6693.     value->tokens = tokens;
  6694.     value->next = hp->value;
  6695.     hp->value = value;
  6696.   }
  6697.  
  6698.   return 0;
  6699. }
  6700.  
  6701. static int
  6702. do_unassert (buf, limit, op, keyword)
  6703.      U_CHAR *buf, *limit;
  6704.      FILE_BUF *op;
  6705.      struct directive *keyword;
  6706. {
  6707.   U_CHAR *bp;            /* temp ptr into input buffer */
  6708.   U_CHAR *symname;        /* remember where symbol name starts */
  6709.   int sym_length;        /* and how long it is */
  6710.  
  6711.   struct arglist *tokens = NULL;
  6712.   int tokens_specified = 0;
  6713.  
  6714.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6715.     pedwarn ("ANSI C does not allow `#unassert'");
  6716.  
  6717.   bp = buf;
  6718.  
  6719.   while (is_hor_space[*bp])
  6720.     bp++;
  6721.  
  6722.   symname = bp;            /* remember where it starts */
  6723.   sym_length = check_macro_name (bp, "assertion");
  6724.   bp += sym_length;
  6725.   /* #define doesn't do this, but we should.  */
  6726.   SKIP_WHITE_SPACE (bp);
  6727.  
  6728.   /* Lossage will occur if identifiers or control tokens are broken
  6729.      across lines using backslash.  This is not the right place to take
  6730.      care of that. */
  6731.  
  6732.   if (*bp == '(') {
  6733.     int error_flag = 0;
  6734.  
  6735.     bp++;            /* skip '(' */
  6736.     SKIP_WHITE_SPACE (bp);
  6737.  
  6738.     tokens = read_token_list (&bp, limit, &error_flag);
  6739.     if (error_flag)
  6740.       return 1;
  6741.     if (tokens == 0) {
  6742.       error ("empty token list in `#unassert'");
  6743.       return 1;
  6744.     }
  6745.  
  6746.     tokens_specified = 1;
  6747.  
  6748.     ++bp;            /* skip paren */
  6749.     SKIP_WHITE_SPACE (bp);
  6750.   }
  6751.  
  6752.   {
  6753.     ASSERTION_HASHNODE *hp;
  6754.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6755.     struct tokenlist_list *tail, *prev;
  6756.  
  6757.     hp = assertion_lookup (symname, sym_length, hashcode);
  6758.     if (hp == NULL)
  6759.       return 1;
  6760.  
  6761.     /* If no token list was specified, then eliminate this assertion
  6762.        entirely.  */
  6763.     if (! tokens_specified) {
  6764.       struct tokenlist_list *next;
  6765.       for (tail = hp->value; tail; tail = next) {
  6766.     next = tail->next;
  6767.     free_token_list (tail->tokens);
  6768.     free (tail);
  6769.       }
  6770.       delete_assertion (hp);
  6771.     } else {
  6772.       /* If a list of tokens was given, then delete any matching list.  */
  6773.  
  6774.       tail = hp->value;
  6775.       prev = 0;
  6776.       while (tail) {
  6777.     struct tokenlist_list *next = tail->next;
  6778.     if (compare_token_lists (tail->tokens, tokens)) {
  6779.       if (prev)
  6780.         prev->next = next;
  6781.       else
  6782.         hp->value = tail->next;
  6783.       free_token_list (tail->tokens);
  6784.       free (tail);
  6785.     } else {
  6786.       prev = tail;
  6787.     }
  6788.     tail = next;
  6789.       }
  6790.     }
  6791.   }
  6792.  
  6793.   return 0;
  6794. }
  6795.  
  6796. /* Test whether there is an assertion named NAME
  6797.    and optionally whether it has an asserted token list TOKENS.
  6798.    NAME is not null terminated; its length is SYM_LENGTH.
  6799.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  6800.  
  6801. int
  6802. check_assertion (name, sym_length, tokens_specified, tokens)
  6803.      U_CHAR *name;
  6804.      int sym_length;
  6805.      int tokens_specified;
  6806.      struct arglist *tokens;
  6807. {
  6808.   ASSERTION_HASHNODE *hp;
  6809.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  6810.  
  6811.   if (pedantic && !instack[indepth].system_header_p)
  6812.     pedwarn ("ANSI C does not allow testing assertions");
  6813.  
  6814.   hp = assertion_lookup (name, sym_length, hashcode);
  6815.   if (hp == NULL)
  6816.     /* It is not an assertion; just return false.  */
  6817.     return 0;
  6818.  
  6819.   /* If no token list was specified, then value is 1.  */
  6820.   if (! tokens_specified)
  6821.     return 1;
  6822.  
  6823.   {
  6824.     struct tokenlist_list *tail;
  6825.  
  6826.     tail = hp->value;
  6827.  
  6828.     /* If a list of tokens was given,
  6829.        then succeed if the assertion records a matching list.  */
  6830.  
  6831.     while (tail) {
  6832.       if (compare_token_lists (tail->tokens, tokens))
  6833.     return 1;
  6834.       tail = tail->next;
  6835.     }
  6836.  
  6837.     /* Fail if the assertion has no matching list.  */
  6838.     return 0;
  6839.   }
  6840. }
  6841.  
  6842. /* Compare two lists of tokens for equality including order of tokens.  */
  6843.  
  6844. static int
  6845. compare_token_lists (l1, l2)
  6846.      struct arglist *l1, *l2;
  6847. {
  6848.   while (l1 && l2) {
  6849.     if (l1->length != l2->length)
  6850.       return 0;
  6851.     if (bcmp (l1->name, l2->name, l1->length))
  6852.       return 0;
  6853.     l1 = l1->next;
  6854.     l2 = l2->next;
  6855.   }
  6856.  
  6857.   /* Succeed if both lists end at the same time.  */
  6858.   return l1 == l2;
  6859. }
  6860.  
  6861. /* Read a space-separated list of tokens ending in a close parenthesis.
  6862.    Return a list of strings, in the order they were written.
  6863.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  6864.    Parse the text starting at *BPP, and update *BPP.
  6865.    Don't parse beyond LIMIT.  */
  6866.  
  6867. static struct arglist *
  6868. read_token_list (bpp, limit, error_flag)
  6869.      U_CHAR **bpp;
  6870.      U_CHAR *limit;
  6871.      int *error_flag;
  6872. {
  6873.   struct arglist *token_ptrs = 0;
  6874.   U_CHAR *bp = *bpp;
  6875.   int depth = 1;
  6876.  
  6877.   *error_flag = 0;
  6878.  
  6879.   /* Loop over the assertion value tokens.  */
  6880.   while (depth > 0) {
  6881.     struct arglist *temp;
  6882.     int eofp = 0;
  6883.     U_CHAR *beg = bp;
  6884.  
  6885.     /* Find the end of the token.  */
  6886.     if (*bp == '(') {
  6887.       bp++;
  6888.       depth++;
  6889.     } else if (*bp == ')') {
  6890.       depth--;
  6891.       if (depth == 0)
  6892.     break;
  6893.       bp++;
  6894.     } else if (*bp == '"' || *bp == '\'')
  6895.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  6896.     else
  6897.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  6898.          && *bp != '"' && *bp != '\'' && bp != limit)
  6899.     bp++;
  6900.  
  6901.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  6902.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  6903.     bcopy ((char *) beg, (char *) temp->name, bp - beg);
  6904.     temp->name[bp - beg] = 0;
  6905.     temp->next = token_ptrs;
  6906.     token_ptrs = temp;
  6907.     temp->length = bp - beg;
  6908.  
  6909.     SKIP_WHITE_SPACE (bp);
  6910.  
  6911.     if (bp >= limit) {
  6912.       error ("unterminated token sequence in `#assert' or `#unassert'");
  6913.       *error_flag = -1;
  6914.       return 0;
  6915.     }
  6916.   }
  6917.   *bpp = bp;
  6918.  
  6919.   /* We accumulated the names in reverse order.
  6920.      Now reverse them to get the proper order.  */
  6921.   {
  6922.     register struct arglist *prev = 0, *this, *next;
  6923.     for (this = token_ptrs; this; this = next) {
  6924.       next = this->next;
  6925.       this->next = prev;
  6926.       prev = this;
  6927.     }
  6928.     return prev;
  6929.   }
  6930. }
  6931.  
  6932. static void
  6933. free_token_list (tokens)
  6934.      struct arglist *tokens;
  6935. {
  6936.   while (tokens) {
  6937.     struct arglist *next = tokens->next;
  6938.     free (tokens->name);
  6939.     free (tokens);
  6940.     tokens = next;
  6941.   }
  6942. }
  6943.  
  6944. /*
  6945.  * Install a name in the assertion hash table.
  6946.  *
  6947.  * If LEN is >= 0, it is the length of the name.
  6948.  * Otherwise, compute the length by scanning the entire name.
  6949.  *
  6950.  * If HASH is >= 0, it is the precomputed hash code.
  6951.  * Otherwise, compute the hash code.
  6952.  */
  6953. static ASSERTION_HASHNODE *
  6954. assertion_install (name, len, hash)
  6955.      U_CHAR *name;
  6956.      int len;
  6957.      int hash;
  6958. {
  6959.   register ASSERTION_HASHNODE *hp;
  6960.   register int i, bucket;
  6961.   register U_CHAR *p, *q;
  6962.  
  6963.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  6964.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  6965.   bucket = hash;
  6966.   hp->bucket_hdr = &assertion_hashtab[bucket];
  6967.   hp->next = assertion_hashtab[bucket];
  6968.   assertion_hashtab[bucket] = hp;
  6969.   hp->prev = NULL;
  6970.   if (hp->next != NULL)
  6971.     hp->next->prev = hp;
  6972.   hp->length = len;
  6973.   hp->value = 0;
  6974.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  6975.   p = hp->name;
  6976.   q = name;
  6977.   for (i = 0; i < len; i++)
  6978.     *p++ = *q++;
  6979.   hp->name[len] = 0;
  6980.   return hp;
  6981. }
  6982.  
  6983. /*
  6984.  * find the most recent hash node for name name (ending with first
  6985.  * non-identifier char) installed by install
  6986.  *
  6987.  * If LEN is >= 0, it is the length of the name.
  6988.  * Otherwise, compute the length by scanning the entire name.
  6989.  *
  6990.  * If HASH is >= 0, it is the precomputed hash code.
  6991.  * Otherwise, compute the hash code.
  6992.  */
  6993. static ASSERTION_HASHNODE *
  6994. assertion_lookup (name, len, hash)
  6995.      U_CHAR *name;
  6996.      int len;
  6997.      int hash;
  6998. {
  6999.   register ASSERTION_HASHNODE *bucket;
  7000.  
  7001.   bucket = assertion_hashtab[hash];
  7002.   while (bucket) {
  7003.     if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
  7004.       return bucket;
  7005.     bucket = bucket->next;
  7006.   }
  7007.   return NULL;
  7008. }
  7009.  
  7010. static void
  7011. delete_assertion (hp)
  7012.      ASSERTION_HASHNODE *hp;
  7013. {
  7014.  
  7015.   if (hp->prev != NULL)
  7016.     hp->prev->next = hp->next;
  7017.   if (hp->next != NULL)
  7018.     hp->next->prev = hp->prev;
  7019.  
  7020.   /* make sure that the bucket chain header that
  7021.      the deleted guy was on points to the right thing afterwards. */
  7022.   if (hp == *hp->bucket_hdr)
  7023.     *hp->bucket_hdr = hp->next;
  7024.  
  7025.   free (hp);
  7026. }
  7027.  
  7028. /*
  7029.  * interpret #line directive.  Remembers previously seen fnames
  7030.  * in its very own hash table.
  7031.  */
  7032. #define FNAME_HASHSIZE 37
  7033.  
  7034. static int
  7035. do_line (buf, limit, op, keyword)
  7036.      U_CHAR *buf, *limit;
  7037.      FILE_BUF *op;
  7038.      struct directive *keyword;
  7039. {
  7040.   register U_CHAR *bp;
  7041.   FILE_BUF *ip = &instack[indepth];
  7042.   FILE_BUF tem;
  7043.   int new_lineno;
  7044.   enum file_change_code file_change = same_file;
  7045.  
  7046.   /* Expand any macros.  */
  7047.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  7048.  
  7049.   /* Point to macroexpanded line, which is null-terminated now.  */
  7050.   bp = tem.buf;
  7051.   SKIP_WHITE_SPACE (bp);
  7052.  
  7053.   if (!isdigit (*bp)) {
  7054.     error ("invalid format `#line' directive");
  7055.     return 0;
  7056.   }
  7057.  
  7058.   /* The Newline at the end of this line remains to be processed.
  7059.      To put the next line at the specified line number,
  7060.      we must store a line number now that is one less.  */
  7061.   new_lineno = atoi ((char *) bp) - 1;
  7062.  
  7063.   /* NEW_LINENO is one less than the actual line number here.  */
  7064.   if (pedantic && new_lineno < 0)
  7065.     pedwarn ("line number out of range in `#line' directive");
  7066.  
  7067.   /* skip over the line number.  */
  7068.   while (isdigit (*bp))
  7069.     bp++;
  7070.  
  7071. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  7072.   if (*bp && !is_space[*bp]) {
  7073.     error ("invalid format `#line' directive");
  7074.     return;
  7075.   }
  7076. #endif
  7077.  
  7078.   SKIP_WHITE_SPACE (bp);
  7079.  
  7080.   if (*bp == '\"') {
  7081.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  7082.     HASHNODE *hp, **hash_bucket;
  7083.     U_CHAR *fname, *p;
  7084.     int fname_length;
  7085.  
  7086.     fname = ++bp;
  7087.  
  7088.     /* Turn the file name, which is a character string literal,
  7089.        into a null-terminated string.  Do this in place.  */
  7090.     p = bp;
  7091.     for (;;)
  7092.       switch ((*p++ = *bp++)) {
  7093.       case '\0':
  7094.     error ("invalid format `#line' directive");
  7095.     return 0;
  7096.  
  7097.       case '\\':
  7098.     {
  7099.       char *bpc = (char *) bp;
  7100.       int c = parse_escape (&bpc);
  7101.       bp = (U_CHAR *) bpc;
  7102.       if (c < 0)
  7103.         p--;
  7104.       else
  7105.         p[-1] = c;
  7106.     }
  7107.     break;
  7108.  
  7109.       case '\"':
  7110.     p[-1] = 0;
  7111.     goto fname_done;
  7112.       }
  7113.   fname_done:
  7114.     fname_length = p - fname;
  7115.  
  7116.     SKIP_WHITE_SPACE (bp);
  7117.     if (*bp) {
  7118.       if (pedantic)
  7119.     pedwarn ("garbage at end of `#line' directive");
  7120.       if (*bp == '1')
  7121.     file_change = enter_file;
  7122.       else if (*bp == '2')
  7123.     file_change = leave_file;
  7124.       else if (*bp == '3')
  7125.     ip->system_header_p = 1;
  7126.       else if (*bp == '4')
  7127.     ip->system_header_p = 2;
  7128.       else {
  7129.     error ("invalid format `#line' directive");
  7130.     return 0;
  7131.       }
  7132.  
  7133.       bp++;
  7134.       SKIP_WHITE_SPACE (bp);
  7135.       if (*bp == '3') {
  7136.     ip->system_header_p = 1;
  7137.     bp++;
  7138.     SKIP_WHITE_SPACE (bp);
  7139.       }
  7140.       if (*bp == '4') {
  7141.     ip->system_header_p = 2;
  7142.     bp++;
  7143.     SKIP_WHITE_SPACE (bp);
  7144.       }
  7145.       if (*bp) {
  7146.     error ("invalid format `#line' directive");
  7147.     return 0;
  7148.       }
  7149.     }
  7150.  
  7151.     hash_bucket =
  7152.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  7153.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  7154.       if (hp->length == fname_length &&
  7155.       bcmp (hp->value.cpval, fname, fname_length) == 0) {
  7156.     ip->nominal_fname = hp->value.cpval;
  7157.     break;
  7158.       }
  7159.     if (hp == 0) {
  7160.       /* Didn't find it; cons up a new one.  */
  7161.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  7162.       hp->next = *hash_bucket;
  7163.       *hash_bucket = hp;
  7164.  
  7165.       hp->length = fname_length;
  7166.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  7167.       bcopy (fname, hp->value.cpval, fname_length);
  7168.     }
  7169.   } else if (*bp) {
  7170.     error ("invalid format `#line' directive");
  7171.     return 0;
  7172.   }
  7173.  
  7174.   ip->lineno = new_lineno;
  7175.   output_line_directive (ip, op, 0, file_change);
  7176.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  7177.   return 0;
  7178. }
  7179.  
  7180. /*
  7181.  * remove the definition of a symbol from the symbol table.
  7182.  * according to un*x /lib/cpp, it is not an error to undef
  7183.  * something that has no definitions, so it isn't one here either.
  7184.  */
  7185.  
  7186. static int
  7187. do_undef (buf, limit, op, keyword)
  7188.      U_CHAR *buf, *limit;
  7189.      FILE_BUF *op;
  7190.      struct directive *keyword;
  7191. {
  7192.   int sym_length;
  7193.   HASHNODE *hp;
  7194.   U_CHAR *orig_buf = buf;
  7195.  
  7196.   /* If this is a precompiler run (with -pcp) pass thru #undef directives.  */
  7197.   if (pcp_outfile && op)
  7198.     pass_thru_directive (buf, limit, op, keyword);
  7199.  
  7200.   SKIP_WHITE_SPACE (buf);
  7201.   sym_length = check_macro_name (buf, "macro");
  7202.  
  7203.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  7204.     /* If we are generating additional info for debugging (with -g) we
  7205.        need to pass through all effective #undef directives.  */
  7206.     if (debug_output && op)
  7207.       pass_thru_directive (orig_buf, limit, op, keyword);
  7208.     if (hp->type != T_MACRO)
  7209.       warning ("undefining `%s'", hp->name);
  7210.     delete_macro (hp);
  7211.   }
  7212.  
  7213.   if (pedantic) {
  7214.     buf += sym_length;
  7215.     SKIP_WHITE_SPACE (buf);
  7216.     if (buf != limit)
  7217.       pedwarn ("garbage after `#undef' directive");
  7218.   }
  7219.   return 0;
  7220. }
  7221.  
  7222. /*
  7223.  * Report an error detected by the program we are processing.
  7224.  * Use the text of the line in the error message.
  7225.  * (We use error because it prints the filename & line#.)
  7226.  */
  7227.  
  7228. static int
  7229. do_error (buf, limit, op, keyword)
  7230.      U_CHAR *buf, *limit;
  7231.      FILE_BUF *op;
  7232.      struct directive *keyword;
  7233. {
  7234.   int length = limit - buf;
  7235.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  7236.   bcopy ((char *) buf, (char *) copy, length);
  7237.   copy[length] = 0;
  7238.   SKIP_WHITE_SPACE (copy);
  7239.   error ("#error %s", copy);
  7240.   return 0;
  7241. }
  7242.  
  7243. /*
  7244.  * Report a warning detected by the program we are processing.
  7245.  * Use the text of the line in the warning message, then continue.
  7246.  * (We use error because it prints the filename & line#.)
  7247.  */
  7248.  
  7249. static int
  7250. do_warning (buf, limit, op, keyword)
  7251.      U_CHAR *buf, *limit;
  7252.      FILE_BUF *op;
  7253.      struct directive *keyword;
  7254. {
  7255.   int length = limit - buf;
  7256.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  7257.   bcopy ((char *) buf, (char *) copy, length);
  7258.   copy[length] = 0;
  7259.   SKIP_WHITE_SPACE (copy);
  7260.   warning ("#warning %s", copy);
  7261.   return 0;
  7262. }
  7263.  
  7264. /* Remember the name of the current file being read from so that we can
  7265.    avoid ever including it again.  */
  7266.  
  7267. static void
  7268. do_once ()
  7269. {
  7270.   int i;
  7271.   FILE_BUF *ip = NULL;
  7272.  
  7273.   for (i = indepth; i >= 0; i--)
  7274.     if (instack[i].fname != NULL) {
  7275.       ip = &instack[i];
  7276.       break;
  7277.     }
  7278.  
  7279.   if (ip != NULL) {
  7280.     struct file_name_list *new;
  7281.     
  7282.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  7283.     new->next = dont_repeat_files;
  7284.     dont_repeat_files = new;
  7285.     new->fname = savestring (ip->fname);
  7286.     new->control_macro = 0;
  7287.     new->got_name_map = 0;
  7288.     new->c_system_include_path = 0;
  7289.   }
  7290. }
  7291.  
  7292. /* #ident has already been copied to the output file, so just ignore it.  */
  7293.  
  7294. static int
  7295. do_ident (buf, limit, op, keyword)
  7296.      U_CHAR *buf, *limit;
  7297.      FILE_BUF *op;
  7298.      struct directive *keyword;
  7299. {
  7300.   FILE_BUF trybuf;
  7301.   int len;
  7302.  
  7303.   /* Allow #ident in system headers, since that's not user's fault.  */
  7304.   if (pedantic && !instack[indepth].system_header_p)
  7305.     pedwarn ("ANSI C does not allow `#ident'");
  7306.  
  7307.   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  7308.   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  7309.   bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  7310.   limit = buf + (trybuf.bufp - trybuf.buf);
  7311.   len = (limit - buf);
  7312.   free (trybuf.buf);
  7313.  
  7314.   /* Output directive name.  */
  7315.   check_expand (op, 7);
  7316.   bcopy ("#ident ", (char *) op->bufp, 7);
  7317.   op->bufp += 7;
  7318.  
  7319.   /* Output the expanded argument line.  */
  7320.   check_expand (op, len);
  7321.   bcopy ((char *) buf, (char *) op->bufp, len);
  7322.   op->bufp += len;
  7323.  
  7324.   return 0;
  7325. }
  7326.  
  7327. /* #pragma and its argument line have already been copied to the output file.
  7328.    Just check for some recognized pragmas that need validation here.  */
  7329.  
  7330. static int
  7331. do_pragma (buf, limit, op, keyword)
  7332.      U_CHAR *buf, *limit;
  7333.      FILE_BUF *op;
  7334.      struct directive *keyword;
  7335. {
  7336.   SKIP_WHITE_SPACE (buf);
  7337.   if (!strncmp ((char *) buf, "once", 4)) {
  7338.     /* Allow #pragma once in system headers, since that's not the user's
  7339.        fault.  */
  7340.     if (!instack[indepth].system_header_p)
  7341.       warning ("`#pragma once' is obsolete");
  7342.     do_once ();
  7343.   }
  7344.  
  7345.   if (!strncmp ((char *) buf, "implementation", 14)) {
  7346.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  7347.        been included yet.  */
  7348.     struct file_name_list *ptr;
  7349.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  7350.     SKIP_WHITE_SPACE (p);
  7351.     if (*p == '\n' || *p != '\"')
  7352.       return 0;
  7353.  
  7354.     fname = p + 1;
  7355.     if ((p = (U_CHAR *) index ((char *) fname, '\"')))
  7356.       *p = '\0';
  7357.     
  7358.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  7359.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  7360.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  7361.       if (inc_fname && !strcmp ((char *) inc_fname, (char *) fname))
  7362.     warning ("`#pragma implementation' for `%s' appears after file is included",
  7363.          fname);
  7364.     }
  7365.   }
  7366.  
  7367.   return 0;
  7368. }
  7369.  
  7370. #if 0
  7371. /* This was a fun hack, but #pragma seems to start to be useful.
  7372.    By failing to recognize it, we pass it through unchanged to cc1.  */
  7373.  
  7374. /*
  7375.  * the behavior of the #pragma directive is implementation defined.
  7376.  * this implementation defines it as follows.
  7377.  */
  7378.  
  7379. static int
  7380. do_pragma ()
  7381. {
  7382.   close (0);
  7383.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  7384.     goto nope;
  7385.   close (1);
  7386.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  7387.     goto nope;
  7388.   execl ("/usr/games/hack", "#pragma", 0);
  7389.   execl ("/usr/games/rogue", "#pragma", 0);
  7390.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  7391.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  7392. nope:
  7393.   fatal ("You are in a maze of twisty compiler features, all different");
  7394. }
  7395. #endif
  7396.  
  7397. #ifdef SCCS_DIRECTIVE
  7398.  
  7399. /* Just ignore #sccs, on systems where we define it at all.  */
  7400.  
  7401. static int
  7402. do_sccs (buf, limit, op, keyword)
  7403.      U_CHAR *buf, *limit;
  7404.      FILE_BUF *op;
  7405.      struct directive *keyword;
  7406. {
  7407.   if (pedantic)
  7408.     pedwarn ("ANSI C does not allow `#sccs'");
  7409.   return 0;
  7410. }
  7411.  
  7412. #endif /* defined (SCCS_DIRECTIVE) */
  7413.  
  7414. /*
  7415.  * handle #if directive by
  7416.  *   1) inserting special `defined' keyword into the hash table
  7417.  *    that gets turned into 0 or 1 by special_symbol (thus,
  7418.  *    if the luser has a symbol called `defined' already, it won't
  7419.  *      work inside the #if directive)
  7420.  *   2) rescan the input into a temporary output buffer
  7421.  *   3) pass the output buffer to the yacc parser and collect a value
  7422.  *   4) clean up the mess left from steps 1 and 2.
  7423.  *   5) call conditional_skip to skip til the next #endif (etc.),
  7424.  *      or not, depending on the value from step 3.
  7425.  */
  7426.  
  7427. static int
  7428. do_if (buf, limit, op, keyword)
  7429.      U_CHAR *buf, *limit;
  7430.      FILE_BUF *op;
  7431.      struct directive *keyword;
  7432. {
  7433.   HOST_WIDE_INT value;
  7434.   FILE_BUF *ip = &instack[indepth];
  7435.  
  7436.   value = eval_if_expression (buf, limit - buf);
  7437.   conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
  7438.   return 0;
  7439. }
  7440.  
  7441. /*
  7442.  * handle a #elif directive by not changing  if_stack  either.
  7443.  * see the comment above do_else.
  7444.  */
  7445.  
  7446. static int
  7447. do_elif (buf, limit, op, keyword)
  7448.      U_CHAR *buf, *limit;
  7449.      FILE_BUF *op;
  7450.      struct directive *keyword;
  7451. {
  7452.   HOST_WIDE_INT value;
  7453.   FILE_BUF *ip = &instack[indepth];
  7454.  
  7455.   if (if_stack == instack[indepth].if_stack) {
  7456.     error ("`#elif' not within a conditional");
  7457.     return 0;
  7458.   } else {
  7459.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7460.       error ("`#elif' after `#else'");
  7461. #ifdef REPORT_EVENT
  7462.       REPORT_EVENT (0, NULL, if_stack->fname, if_stack->lineno,
  7463.             "matching conditional", 0 ,0, 0);
  7464. #endif
  7465.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7466.       if (if_stack->fname != NULL && ip->fname != NULL &&
  7467.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7468.     fprintf (stderr, ", file %s", if_stack->fname);
  7469.       fprintf (stderr, ")\n");
  7470.     }
  7471.     if_stack->type = T_ELIF;
  7472.   }
  7473.  
  7474.   if (if_stack->if_succeeded)
  7475.     skip_if_group (ip, 0, op);
  7476.   else {
  7477.     value = eval_if_expression (buf, limit - buf);
  7478.     if (value == 0)
  7479.       skip_if_group (ip, 0, op);
  7480.     else {
  7481.       ++if_stack->if_succeeded;    /* continue processing input */
  7482.       output_line_directive (ip, op, 1, same_file);
  7483.     }
  7484.   }
  7485.   return 0;
  7486. }
  7487.  
  7488. /*
  7489.  * evaluate a #if expression in BUF, of length LENGTH,
  7490.  * then parse the result as a C expression and return the value as an int.
  7491.  */
  7492. static HOST_WIDE_INT
  7493. eval_if_expression (buf, length)
  7494.      U_CHAR *buf;
  7495.      int length;
  7496. {
  7497.   FILE_BUF temp_obuf;
  7498.   HASHNODE *save_defined;
  7499.   HOST_WIDE_INT value;
  7500.  
  7501.   save_defined = install ((U_CHAR *) "defined", -1, T_SPEC_DEFINED,
  7502.               NULL_PTR, -1);
  7503.   pcp_inside_if = 1;
  7504.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  7505.   pcp_inside_if = 0;
  7506.   delete_macro (save_defined);    /* clean up special symbol */
  7507.  
  7508.   value = parse_c_expression ((char *) temp_obuf.buf);
  7509.  
  7510.   free (temp_obuf.buf);
  7511.  
  7512.   return value;
  7513. }
  7514.  
  7515. /*
  7516.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  7517.  * then do or don't skip to the #endif/#else/#elif depending
  7518.  * on what directive is actually being processed.
  7519.  */
  7520.  
  7521. static int
  7522. do_xifdef (buf, limit, op, keyword)
  7523.      U_CHAR *buf, *limit;
  7524.      FILE_BUF *op;
  7525.      struct directive *keyword;
  7526. {
  7527.   int skip;
  7528.   FILE_BUF *ip = &instack[indepth];
  7529.   U_CHAR *end; 
  7530.   int start_of_file = 0;
  7531.   U_CHAR *control_macro = 0;
  7532.  
  7533.   /* Detect a #ifndef at start of file (not counting comments).  */
  7534.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  7535.     U_CHAR *p = ip->buf;
  7536.     while (p != directive_start) {
  7537.       U_CHAR c = *p++;
  7538.       if (is_space[c])
  7539.     ;
  7540.       /* Make no special provision for backslash-newline here; this is
  7541.      slower if backslash-newlines are present, but it's correct,
  7542.      and it's not worth it to tune for the rare backslash-newline.  */
  7543.       else if (c == '/'
  7544.            && (*p == '*' || (cplusplus_comments && *p == '/'))) {
  7545.     /* Skip this comment.  */
  7546.     int junk = 0;
  7547.     U_CHAR *save_bufp = ip->bufp;
  7548.     ip->bufp = p + 1;
  7549.     p = skip_to_end_of_comment (ip, &junk, 1);
  7550.     ip->bufp = save_bufp;
  7551.       } else {
  7552.     goto fail;
  7553.       }
  7554.     }
  7555.     /* If we get here, this conditional is the beginning of the file.  */
  7556.     start_of_file = 1;
  7557.   fail: ;
  7558.   }
  7559.  
  7560.   /* Discard leading and trailing whitespace.  */
  7561.   SKIP_WHITE_SPACE (buf);
  7562.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  7563.  
  7564.   /* Find the end of the identifier at the beginning.  */
  7565.   for (end = buf; is_idchar[*end]; end++);
  7566.  
  7567.   if (end == buf) {
  7568.     skip = (keyword->type == T_IFDEF);
  7569.     if (! traditional)
  7570.       pedwarn (end == limit ? "`#%s' with no argument"
  7571.            : "`#%s' argument starts with punctuation",
  7572.            keyword->name);
  7573.   } else {
  7574.     HASHNODE *hp;
  7575.  
  7576.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  7577.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  7578.     else if (end != limit && !traditional)
  7579.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  7580.  
  7581.     hp = lookup (buf, end-buf, -1);
  7582.  
  7583.     if (pcp_outfile) {
  7584.       /* Output a precondition for this macro.  */
  7585.       if (hp &&
  7586.       (hp->type == T_CONST
  7587.        || (hp->type == T_MACRO && hp->value.defn->predefined)))
  7588.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  7589.       else {
  7590.     U_CHAR *cp = buf;
  7591.     fprintf (pcp_outfile, "#undef ");
  7592.     while (is_idchar[*cp]) /* Ick! */
  7593.       fputc (*cp++, pcp_outfile);
  7594.     putc ('\n', pcp_outfile);
  7595.       }
  7596.     }
  7597.  
  7598.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  7599.     if (start_of_file && !skip) {
  7600.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  7601.       bcopy ((char *) buf, (char *) control_macro, end - buf);
  7602.       control_macro[end - buf] = 0;
  7603.     }
  7604.   }
  7605.   
  7606.   conditional_skip (ip, skip, T_IF, control_macro, op);
  7607.   return 0;
  7608. }
  7609.  
  7610. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  7611.    If this is a #ifndef starting at the beginning of a file,
  7612.    CONTROL_MACRO is the macro name tested by the #ifndef.
  7613.    Otherwise, CONTROL_MACRO is 0.  */
  7614.  
  7615. static void
  7616. conditional_skip (ip, skip, type, control_macro, op)
  7617.      FILE_BUF *ip;
  7618.      int skip;
  7619.      enum node_type type;
  7620.      U_CHAR *control_macro;
  7621.      FILE_BUF *op;
  7622. {
  7623.   IF_STACK_FRAME *temp;
  7624.  
  7625.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7626.   temp->fname = ip->nominal_fname;
  7627.   temp->lineno = ip->lineno;
  7628.   temp->next = if_stack;
  7629.   temp->control_macro = control_macro;
  7630.   if_stack = temp;
  7631.  
  7632.   if_stack->type = type;
  7633.  
  7634.   if (skip != 0) {
  7635.     skip_if_group (ip, 0, op);
  7636.     return;
  7637.   } else {
  7638.     ++if_stack->if_succeeded;
  7639.     output_line_directive (ip, &outbuf, 1, same_file);
  7640.   }
  7641. }
  7642.  
  7643. /*
  7644.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  7645.  * leaves input ptr at the sharp sign found.
  7646.  * If ANY is nonzero, return at next directive of any sort.
  7647.  */
  7648. static void
  7649. skip_if_group (ip, any, op)
  7650.      FILE_BUF *ip;
  7651.      int any;
  7652.      FILE_BUF *op;
  7653. {
  7654.   register U_CHAR *bp = ip->bufp, *cp;
  7655.   register U_CHAR *endb = ip->buf + ip->length;
  7656.   struct directive *kt;
  7657.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  7658.   U_CHAR *beg_of_line = bp;
  7659.   register int ident_length;
  7660.   U_CHAR *ident, *after_ident;
  7661.   /* Save info about where the group starts.  */
  7662.   U_CHAR *beg_of_group = bp;
  7663.   int beg_lineno = ip->lineno;
  7664.  
  7665.   if (output_conditionals && op != 0) {
  7666.     char *ptr = "#failed\n";
  7667.     int len = strlen (ptr);
  7668.  
  7669.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7670.       {
  7671.     *op->bufp++ = '\n';
  7672.     op->lineno++;
  7673.       }
  7674.     check_expand (op, len);
  7675.     bcopy (ptr, (char *) op->bufp, len);
  7676.     op->bufp += len;
  7677.     op->lineno++;
  7678.     output_line_directive (ip, op, 1, 0);
  7679.   }
  7680.  
  7681.   while (bp < endb) {
  7682.     switch (*bp++) {
  7683.     case '/':            /* possible comment */
  7684.       if (*bp == '\\' && bp[1] == '\n')
  7685.     newline_fix (bp);
  7686.       if (*bp == '*'
  7687.       || (cplusplus_comments && *bp == '/')) {
  7688.     ip->bufp = ++bp;
  7689.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  7690.       }
  7691.       break;
  7692.     case '\"':
  7693.     case '\'':
  7694.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  7695.                    NULL_PTR, NULL_PTR);
  7696.       break;
  7697.     case '\\':
  7698.       /* Char after backslash loses its special meaning.  */
  7699.       if (bp < endb) {
  7700.     if (*bp == '\n')
  7701.       ++ip->lineno;        /* But do update the line-count.  */
  7702.     bp++;
  7703.       }
  7704.       break;
  7705.     case '\n':
  7706.       ++ip->lineno;
  7707.       beg_of_line = bp;
  7708.       break;
  7709.     case '%':
  7710.       if (beg_of_line == 0 || traditional)
  7711.     break;
  7712.       ip->bufp = bp - 1;
  7713.       while (bp[0] == '\\' && bp[1] == '\n')
  7714.     bp += 2;
  7715.       if (*bp == ':')
  7716.     goto sharp_token;
  7717.       break;
  7718.     case '#':
  7719.       /* # keyword: a # must be first nonblank char on the line */
  7720.       if (beg_of_line == 0)
  7721.     break;
  7722.       ip->bufp = bp - 1;
  7723.     sharp_token:
  7724.       /* Scan from start of line, skipping whitespace, comments
  7725.      and backslash-newlines, and see if we reach this #.
  7726.      If not, this # is not special.  */
  7727.       bp = beg_of_line;
  7728.       /* If -traditional, require # to be at beginning of line.  */
  7729.       if (!traditional) {
  7730.     while (1) {
  7731.       if (is_hor_space[*bp])
  7732.         bp++;
  7733.       else if (*bp == '\\' && bp[1] == '\n')
  7734.         bp += 2;
  7735.       else if (*bp == '/' && bp[1] == '*') {
  7736.         bp += 2;
  7737.         while (!(*bp == '*' && bp[1] == '/'))
  7738.           bp++;
  7739.         bp += 2;
  7740.       }
  7741.       /* There is no point in trying to deal with C++ // comments here,
  7742.          because if there is one, then this # must be part of the
  7743.          comment and we would never reach here.  */
  7744.       else break;
  7745.     }
  7746.       }
  7747.       if (bp != ip->bufp) {
  7748.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  7749.     break;
  7750.       }
  7751.  
  7752.       bp = ip->bufp + 1;    /* Point after the '#' */
  7753.       if (ip->bufp[0] == '%') {
  7754.     /* Skip past the ':' again.  */
  7755.     while (*bp == '\\') {
  7756.       ip->lineno++;
  7757.       bp += 2;
  7758.     }
  7759.     bp++;
  7760.       }
  7761.  
  7762.       /* Skip whitespace and \-newline.  */
  7763.       while (1) {
  7764.     if (is_hor_space[*bp])
  7765.       bp++;
  7766.     else if (*bp == '\\' && bp[1] == '\n')
  7767.       bp += 2;
  7768.     else if (*bp == '/' && bp[1] == '*') {
  7769.       bp += 2;
  7770.       while (!(*bp == '*' && bp[1] == '/')) {
  7771.         if (*bp == '\n')
  7772.           ip->lineno++;
  7773.         bp++;
  7774.       }
  7775.       bp += 2;
  7776.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  7777.       bp += 2;
  7778.       while (bp[-1] == '\\' || *bp != '\n') {
  7779.         if (*bp == '\n')
  7780.           ip->lineno++;
  7781.         bp++;
  7782.       }
  7783.         }
  7784.     else break;
  7785.       }
  7786.  
  7787.       cp = bp;
  7788.  
  7789.       /* Now find end of directive name.
  7790.      If we encounter a backslash-newline, exchange it with any following
  7791.      symbol-constituents so that we end up with a contiguous name.  */
  7792.  
  7793.       while (1) {
  7794.     if (is_idchar[*bp])
  7795.       bp++;
  7796.     else {
  7797.       if (*bp == '\\' && bp[1] == '\n')
  7798.         name_newline_fix (bp);
  7799.       if (is_idchar[*bp])
  7800.         bp++;
  7801.       else break;
  7802.     }
  7803.       }
  7804.       ident_length = bp - cp;
  7805.       ident = cp;
  7806.       after_ident = bp;
  7807.  
  7808.       /* A line of just `#' becomes blank.  */
  7809.  
  7810.       if (ident_length == 0 && *after_ident == '\n') {
  7811.     continue;
  7812.       }
  7813.  
  7814.       if (ident_length == 0 || !is_idstart[*ident]) {
  7815.     U_CHAR *p = ident;
  7816.     while (is_idchar[*p]) {
  7817.       if (*p < '0' || *p > '9')
  7818.         break;
  7819.       p++;
  7820.     }
  7821.     /* Handle # followed by a line number.  */
  7822.     if (p != ident && !is_idchar[*p]) {
  7823.       if (pedantic)
  7824.         pedwarn ("`#' followed by integer");
  7825.       continue;
  7826.     }
  7827.  
  7828.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  7829.     if (p == ident) {
  7830.       while (*p == '#' || is_hor_space[*p]) p++;
  7831.       if (*p == '\n') {
  7832.         if (pedantic && !lang_asm)
  7833.           pedwarn ("invalid preprocessing directive");
  7834.         continue;
  7835.       }
  7836.     }
  7837.  
  7838.     if (!lang_asm && pedantic)
  7839.       pedwarn ("invalid preprocessing directive name");
  7840.     continue;
  7841.       }
  7842.  
  7843.       for (kt = directive_table; kt->length >= 0; kt++) {
  7844.     IF_STACK_FRAME *temp;
  7845.     if (ident_length == kt->length
  7846.         && bcmp (cp, kt->name, kt->length) == 0) {
  7847.       /* If we are asked to return on next directive, do so now.  */
  7848.       if (any)
  7849.         goto done;
  7850.  
  7851.       switch (kt->type) {
  7852.       case T_IF:
  7853.       case T_IFDEF:
  7854.       case T_IFNDEF:
  7855.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7856.         temp->next = if_stack;
  7857.         if_stack = temp;
  7858.         temp->lineno = ip->lineno;
  7859.         temp->fname = ip->nominal_fname;
  7860.         temp->type = kt->type;
  7861.         break;
  7862.       case T_ELSE:
  7863.       case T_ENDIF:
  7864.         if (pedantic && if_stack != save_if_stack)
  7865.           validate_else (bp);
  7866.       case T_ELIF:
  7867.         if (if_stack == instack[indepth].if_stack) {
  7868.           error ("`#%s' not within a conditional", kt->name);
  7869.           break;
  7870.         }
  7871.         else if (if_stack == save_if_stack)
  7872.           goto done;        /* found what we came for */
  7873.  
  7874.         if (kt->type != T_ENDIF) {
  7875.           if (if_stack->type == T_ELSE)
  7876.         error ("`#else' or `#elif' after `#else'");
  7877.           if_stack->type = kt->type;
  7878.           break;
  7879.         }
  7880.  
  7881.         temp = if_stack;
  7882.         if_stack = if_stack->next;
  7883.         free (temp);
  7884.         break;
  7885.  
  7886.        default:
  7887.         break;
  7888.       }
  7889.       break;
  7890.     }
  7891.       }
  7892.       /* Don't let erroneous code go by.  */
  7893.       if (kt->length < 0 && !lang_asm && pedantic)
  7894.     pedwarn ("invalid preprocessing directive name");
  7895.     }
  7896.   }
  7897.  
  7898.   ip->bufp = bp;
  7899.   /* after this returns, rescan will exit because ip->bufp
  7900.      now points to the end of the buffer.
  7901.      rescan is responsible for the error message also.  */
  7902.  
  7903.  done:
  7904.   if (output_conditionals && op != 0) {
  7905.     char *ptr = "#endfailed\n";
  7906.     int len = strlen (ptr);
  7907.  
  7908.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7909.       {
  7910.     *op->bufp++ = '\n';
  7911.     op->lineno++;
  7912.       }
  7913.     check_expand (op, beg_of_line - beg_of_group);
  7914.     bcopy ((char *) beg_of_group, (char *) op->bufp,
  7915.        beg_of_line - beg_of_group);
  7916.     op->bufp += beg_of_line - beg_of_group;
  7917.     op->lineno += ip->lineno - beg_lineno;
  7918.     check_expand (op, len);
  7919.     bcopy (ptr, (char *) op->bufp, len);
  7920.     op->bufp += len;
  7921.     op->lineno++;
  7922.   }
  7923. }
  7924.  
  7925. /*
  7926.  * handle a #else directive.  Do this by just continuing processing
  7927.  * without changing  if_stack ;  this is so that the error message
  7928.  * for missing #endif's etc. will point to the original #if.  It
  7929.  * is possible that something different would be better.
  7930.  */
  7931.  
  7932. static int
  7933. do_else (buf, limit, op, keyword)
  7934.      U_CHAR *buf, *limit;
  7935.      FILE_BUF *op;
  7936.      struct directive *keyword;
  7937. {
  7938.   FILE_BUF *ip = &instack[indepth];
  7939.  
  7940.   if (pedantic) {
  7941.     SKIP_WHITE_SPACE (buf);
  7942.     if (buf != limit)
  7943.       pedwarn ("text following `#else' violates ANSI standard");
  7944.   }
  7945.  
  7946.   if (if_stack == instack[indepth].if_stack) {
  7947.     error ("`#else' not within a conditional");
  7948.     return 0;
  7949.   } else {
  7950.     /* #ifndef can't have its special treatment for containing the whole file
  7951.        if it has a #else clause.  */
  7952.     if_stack->control_macro = 0;
  7953.  
  7954.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7955.       error ("`#else' after `#else'");
  7956. #ifdef REPORT_EVENT
  7957.       REPORT_EVENT (0, NULL, if_stack->fname, if_stack->lineno,
  7958.             "matching conditional", 0, 0, 0);
  7959. #endif
  7960.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7961.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7962.     fprintf (stderr, ", file %s", if_stack->fname);
  7963.       fprintf (stderr, ")\n");
  7964.     }
  7965.     if_stack->type = T_ELSE;
  7966.   }
  7967.  
  7968.   if (if_stack->if_succeeded)
  7969.     skip_if_group (ip, 0, op);
  7970.   else {
  7971.     ++if_stack->if_succeeded;    /* continue processing input */
  7972.     output_line_directive (ip, op, 1, same_file);
  7973.   }
  7974.   return 0;
  7975. }
  7976.  
  7977. /*
  7978.  * unstack after #endif directive
  7979.  */
  7980.  
  7981. static int
  7982. do_endif (buf, limit, op, keyword)
  7983.      U_CHAR *buf, *limit;
  7984.      FILE_BUF *op;
  7985.      struct directive *keyword;
  7986. {
  7987.   if (pedantic) {
  7988.     SKIP_WHITE_SPACE (buf);
  7989.     if (buf != limit)
  7990.       pedwarn ("text following `#endif' violates ANSI standard");
  7991.   }
  7992.  
  7993.   if (if_stack == instack[indepth].if_stack)
  7994.     error ("unbalanced `#endif'");
  7995.   else {
  7996.     IF_STACK_FRAME *temp = if_stack;
  7997.     if_stack = if_stack->next;
  7998.     if (temp->control_macro != 0) {
  7999.       /* This #endif matched a #ifndef at the start of the file.
  8000.      See if it is at the end of the file.  */
  8001.       FILE_BUF *ip = &instack[indepth];
  8002.       U_CHAR *p = ip->bufp;
  8003.       U_CHAR *ep = ip->buf + ip->length;
  8004.  
  8005.       while (p != ep) {
  8006.     U_CHAR c = *p++;
  8007.     if (!is_space[c]) {
  8008.       if (c == '/'
  8009.           && (*p == '*' || (cplusplus_comments && *p == '/'))) {
  8010.         /* Skip this comment.  */
  8011.         int junk = 0;
  8012.         U_CHAR *save_bufp = ip->bufp;
  8013.         ip->bufp = p + 1;
  8014.         p = skip_to_end_of_comment (ip, &junk, 1);
  8015.         ip->bufp = save_bufp;
  8016.       } else
  8017.         goto fail;
  8018.     }
  8019.       }
  8020.       /* If we get here, this #endif ends a #ifndef
  8021.      that contains all of the file (aside from whitespace).
  8022.      Arrange not to include the file again
  8023.      if the macro that was tested is defined.
  8024.  
  8025.      Do not do this for the top-level file in a -include or any
  8026.      file in a -imacros.  */
  8027.       if (indepth != 0
  8028.       && ! (indepth == 1 && no_record_file)
  8029.       && ! (no_record_file && no_output))
  8030.     record_control_macro (ip->fname, temp->control_macro);
  8031.     fail: ;
  8032.     }
  8033.     free (temp);
  8034.     output_line_directive (&instack[indepth], op, 1, same_file);
  8035.   }
  8036.   return 0;
  8037. }
  8038.  
  8039. /* When an #else or #endif is found while skipping failed conditional,
  8040.    if -pedantic was specified, this is called to warn about text after
  8041.    the directive name.  P points to the first char after the directive name.  */
  8042.  
  8043. static void
  8044. validate_else (p)
  8045.      register U_CHAR *p;
  8046. {
  8047.   /* Advance P over whitespace and comments.  */
  8048.   while (1) {
  8049.     if (*p == '\\' && p[1] == '\n')
  8050.       p += 2;
  8051.     if (is_hor_space[*p])
  8052.       p++;
  8053.     else if (*p == '/') {
  8054.       if (p[1] == '\\' && p[2] == '\n')
  8055.     newline_fix (p + 1);
  8056.       if (p[1] == '*') {
  8057.     p += 2;
  8058.     /* Don't bother warning about unterminated comments
  8059.        since that will happen later.  Just be sure to exit.  */
  8060.     while (*p) {
  8061.       if (p[1] == '\\' && p[2] == '\n')
  8062.         newline_fix (p + 1);
  8063.       if (*p == '*' && p[1] == '/') {
  8064.         p += 2;
  8065.         break;
  8066.       }
  8067.       p++;
  8068.     }
  8069.       }
  8070.       else if (cplusplus_comments && p[1] == '/') {
  8071.     p += 2;
  8072.     while (*p && (*p != '\n' || p[-1] == '\\'))
  8073.       p++;
  8074.       }
  8075.     } else break;
  8076.   }
  8077.   if (*p && *p != '\n')
  8078.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  8079. }
  8080.  
  8081. /* Skip a comment, assuming the input ptr immediately follows the
  8082.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  8083.    (The canonical line counter is &ip->lineno.)
  8084.    Don't use this routine (or the next one) if bumping the line
  8085.    counter is not sufficient to deal with newlines in the string.
  8086.  
  8087.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  8088.    This feature is useful when processing a comment that is going to be
  8089.    processed or was processed at another point in the preprocessor,
  8090.    to avoid a duplicate warning.  Likewise for unterminated comment errors.  */
  8091.  
  8092. static U_CHAR *
  8093. skip_to_end_of_comment (ip, line_counter, nowarn)
  8094.      register FILE_BUF *ip;
  8095.      int *line_counter;        /* place to remember newlines, or NULL */
  8096.      int nowarn;
  8097. {
  8098.   register U_CHAR *limit = ip->buf + ip->length;
  8099.   register U_CHAR *bp = ip->bufp;
  8100.   FILE_BUF *op = &outbuf;    /* JF */
  8101.   int output = put_out_comments && !line_counter;
  8102.   int start_line = line_counter ? *line_counter : 0;
  8103.  
  8104.     /* JF this line_counter stuff is a crock to make sure the
  8105.        comment is only put out once, no matter how many times
  8106.        the comment is skipped.  It almost works */
  8107.   if (output) {
  8108.     *op->bufp++ = '/';
  8109.     *op->bufp++ = '*';
  8110.   }
  8111.   if (cplusplus_comments && bp[-1] == '/') {
  8112.     if (output) {
  8113.       while (bp < limit) {
  8114.     *op->bufp++ = *bp;
  8115.     if (*bp == '\n' && bp[-1] != '\\')
  8116.       break;
  8117.     if (*bp == '\n') {
  8118.       ++*line_counter;
  8119.       ++op->lineno;
  8120.     }
  8121.     bp++;
  8122.       }
  8123.       op->bufp[-1] = '*';
  8124.       *op->bufp++ = '/';
  8125.       *op->bufp++ = '\n';
  8126.     } else {
  8127.       while (bp < limit) {
  8128.     if (bp[-1] != '\\' && *bp == '\n') {
  8129.       break;
  8130.     } else {
  8131.       if (*bp == '\n' && line_counter)
  8132.         ++*line_counter;
  8133.       bp++;
  8134.     }
  8135.       }
  8136.     }
  8137.     ip->bufp = bp;
  8138.     return bp;
  8139.   }
  8140.   while (bp < limit) {
  8141.     if (output)
  8142.       *op->bufp++ = *bp;
  8143.     switch (*bp++) {
  8144.     case '/':
  8145.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  8146.     warning ("`/*' within comment");
  8147.       break;
  8148.     case '\n':
  8149.       /* If this is the end of the file, we have an unterminated comment.
  8150.      Don't swallow the newline.  We are guaranteed that there will be a
  8151.      trailing newline and various pieces assume it's there.  */
  8152.       if (bp == limit)
  8153.     {
  8154.       --bp;
  8155.       --limit;
  8156.       break;
  8157.     }
  8158.       if (line_counter != NULL)
  8159.     ++*line_counter;
  8160.       if (output)
  8161.     ++op->lineno;
  8162.       break;
  8163.     case '*':
  8164.       if (*bp == '\\' && bp[1] == '\n')
  8165.     newline_fix (bp);
  8166.       if (*bp == '/') {
  8167.         if (output)
  8168.       *op->bufp++ = '/';
  8169.     ip->bufp = ++bp;
  8170.     return bp;
  8171.       }
  8172.       break;
  8173.     }
  8174.   }
  8175.  
  8176.   if (!nowarn)
  8177.     error_with_line (line_for_error (start_line), "unterminated comment");
  8178.   ip->bufp = bp;
  8179.   return bp;
  8180. }
  8181.  
  8182. /*
  8183.  * Skip over a quoted string.  BP points to the opening quote.
  8184.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  8185.  * START_LINE is the line number of the starting point (but it need
  8186.  * not be valid if the starting point is inside a macro expansion).
  8187.  *
  8188.  * The input stack state is not changed.
  8189.  *
  8190.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  8191.  * for each newline passed.
  8192.  *
  8193.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  8194.  * if we pass a backslash-newline.
  8195.  *
  8196.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  8197.  */
  8198. static U_CHAR *
  8199. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  8200.      register U_CHAR *bp;
  8201.      register U_CHAR *limit;
  8202.      int start_line;
  8203.      int *count_newlines;
  8204.      int *backslash_newlines_p;
  8205.      int *eofp;
  8206. {
  8207.   register U_CHAR c, match;
  8208.  
  8209.   match = *bp++;
  8210.   while (1) {
  8211.     if (bp >= limit) {
  8212.       error_with_line (line_for_error (start_line),
  8213.                "unterminated string or character constant");
  8214.       error_with_line (multiline_string_line,
  8215.                "possible real start of unterminated constant");
  8216.       multiline_string_line = 0;
  8217.       if (eofp)
  8218.     *eofp = 1;
  8219.       break;
  8220.     }
  8221.     c = *bp++;
  8222.     if (c == '\\') {
  8223.       while (*bp == '\\' && bp[1] == '\n') {
  8224.     if (backslash_newlines_p)
  8225.       *backslash_newlines_p = 1;
  8226.     if (count_newlines)
  8227.       ++*count_newlines;
  8228.     bp += 2;
  8229.       }
  8230.       if (*bp == '\n' && count_newlines) {
  8231.     if (backslash_newlines_p)
  8232.       *backslash_newlines_p = 1;
  8233.     ++*count_newlines;
  8234.       }
  8235.       bp++;
  8236.     } else if (c == '\n') {
  8237.       if (traditional) {
  8238.      /* Unterminated strings and character constants are 'valid'.  */
  8239.      bp--;    /* Don't consume the newline. */
  8240.      if (eofp)
  8241.        *eofp = 1;
  8242.      break;
  8243.       }
  8244.       if (pedantic || match == '\'') {
  8245.     error_with_line (line_for_error (start_line),
  8246.              "unterminated string or character constant");
  8247.     bp--;
  8248.     if (eofp)
  8249.       *eofp = 1;
  8250.     break;
  8251.       }
  8252.       /* If not traditional, then allow newlines inside strings.  */
  8253.       if (count_newlines)
  8254.     ++*count_newlines;
  8255.       if (multiline_string_line == 0)
  8256.     multiline_string_line = start_line;
  8257.     } else if (c == match)
  8258.       break;
  8259.   }
  8260.   return bp;
  8261. }
  8262.  
  8263. /* Place into DST a quoted string representing the string SRC.
  8264.    Return the address of DST's terminating null.  */
  8265. static char *
  8266. quote_string (dst, src)
  8267.      char *dst, *src;
  8268. {
  8269.   U_CHAR c;
  8270.  
  8271.   *dst++ = '\"';
  8272.   for (;;)
  8273.     switch ((c = *src++))
  8274.       {
  8275.       default:
  8276.         if (isprint (c))
  8277.       *dst++ = c;
  8278.     else
  8279.       {
  8280.         sprintf (dst, "\\%03o", c);
  8281.         dst += 4;
  8282.       }
  8283.     break;
  8284.  
  8285.       case '\"':
  8286.       case '\\':
  8287.     *dst++ = '\\';
  8288.     *dst++ = c;
  8289.     break;
  8290.       
  8291.       case '\0':
  8292.     *dst++ = '\"';
  8293.     *dst = '\0';
  8294.     return dst;
  8295.       }
  8296. }
  8297.  
  8298. /* Skip across a group of balanced parens, starting from IP->bufp.
  8299.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  8300.  
  8301.    This does not handle newlines, because it's used for the arg of #if,
  8302.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  8303.  
  8304. static U_CHAR *
  8305. skip_paren_group (ip)
  8306.      register FILE_BUF *ip;
  8307. {
  8308.   U_CHAR *limit = ip->buf + ip->length;
  8309.   U_CHAR *p = ip->bufp;
  8310.   int depth = 0;
  8311.   int lines_dummy = 0;
  8312.  
  8313.   while (p != limit) {
  8314.     int c = *p++;
  8315.     switch (c) {
  8316.     case '(':
  8317.       depth++;
  8318.       break;
  8319.  
  8320.     case ')':
  8321.       depth--;
  8322.       if (depth == 0)
  8323.     return ip->bufp = p;
  8324.       break;
  8325.  
  8326.     case '/':
  8327.       if (*p == '*') {
  8328.     ip->bufp = p;
  8329.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  8330.     p = ip->bufp;
  8331.       }
  8332.  
  8333.     case '"':
  8334.     case '\'':
  8335.       {
  8336.     int eofp = 0;
  8337.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  8338.     if (eofp)
  8339.       return ip->bufp = p;
  8340.       }
  8341.       break;
  8342.     }
  8343.   }
  8344.  
  8345.   ip->bufp = p;
  8346.   return p;
  8347. }
  8348.  
  8349. /*
  8350.  * write out a #line directive, for instance, after an #include file.
  8351.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  8352.  * appear to be a no-op, and we can output a few newlines instead
  8353.  * if we want to increase the line number by a small amount.
  8354.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  8355.  */
  8356.  
  8357. static void
  8358. output_line_directive (ip, op, conditional, file_change)
  8359.      FILE_BUF *ip, *op;
  8360.      int conditional;
  8361.      enum file_change_code file_change;
  8362. {
  8363.   int len;
  8364.   char *line_directive_buf, *line_end;
  8365.  
  8366.   if (no_line_directives
  8367.       || ip->fname == NULL
  8368.       || no_output) {
  8369.     op->lineno = ip->lineno;
  8370.     return;
  8371.   }
  8372.  
  8373.   if (conditional) {
  8374.     if (ip->lineno == op->lineno)
  8375.       return;
  8376.  
  8377.     /* If the inherited line number is a little too small,
  8378.        output some newlines instead of a #line directive.  */
  8379.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  8380.       check_expand (op, 10);
  8381.       while (ip->lineno > op->lineno) {
  8382.     *op->bufp++ = '\n';
  8383.     op->lineno++;
  8384.       }
  8385.       return;
  8386.     }
  8387.   }
  8388.  
  8389.   /* Don't output a line number of 0 if we can help it.  */
  8390.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  8391.       && *ip->bufp == '\n') {
  8392.     ip->lineno++;
  8393.     ip->bufp++;
  8394.   }
  8395.  
  8396.   line_directive_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
  8397.   sprintf (line_directive_buf, "# %d ", ip->lineno);
  8398.   line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
  8399.                ip->nominal_fname);
  8400.   if (file_change != same_file) {
  8401.     *line_end++ = ' ';
  8402.     *line_end++ = file_change == enter_file ? '1' : '2';
  8403.   }
  8404.   /* Tell cc1 if following text comes from a system header file.  */
  8405.   if (ip->system_header_p) {
  8406.     *line_end++ = ' ';
  8407.     *line_end++ = '3';
  8408.   }
  8409. #ifndef NO_IMPLICIT_EXTERN_C
  8410.   /* Tell cc1plus if following text should be treated as C.  */
  8411.   if (ip->system_header_p == 2 && cplusplus) {
  8412.     *line_end++ = ' ';
  8413.     *line_end++ = '4';
  8414.   }
  8415. #endif
  8416.   *line_end++ = '\n';
  8417.   len = line_end - line_directive_buf;
  8418.   check_expand (op, len + 1);
  8419.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  8420.     *op->bufp++ = '\n';
  8421.   bcopy ((char *) line_directive_buf, (char *) op->bufp, len);
  8422.   op->bufp += len;
  8423.   op->lineno = ip->lineno;
  8424. }
  8425.  
  8426. /* This structure represents one parsed argument in a macro call.
  8427.    `raw' points to the argument text as written (`raw_length' is its length).
  8428.    `expanded' points to the argument's macro-expansion
  8429.    (its length is `expand_length').
  8430.    `stringified_length' is the length the argument would have
  8431.    if stringified.
  8432.    `use_count' is the number of times this macro arg is substituted
  8433.    into the macro.  If the actual use count exceeds 10, 
  8434.    the value stored is 10.
  8435.    `free1' and `free2', if nonzero, point to blocks to be freed
  8436.    when the macro argument data is no longer needed.  */
  8437.  
  8438. struct argdata {
  8439.   U_CHAR *raw, *expanded;
  8440.   int raw_length, expand_length;
  8441.   int stringified_length;
  8442.   U_CHAR *free1, *free2;
  8443.   char newlines;
  8444.   char use_count;
  8445. };
  8446.  
  8447. /* Expand a macro call.
  8448.    HP points to the symbol that is the macro being called.
  8449.    Put the result of expansion onto the input stack
  8450.    so that subsequent input by our caller will use it.
  8451.  
  8452.    If macro wants arguments, caller has already verified that
  8453.    an argument list follows; arguments come from the input stack.  */
  8454.  
  8455. static void
  8456. macroexpand (hp, op)
  8457.      HASHNODE *hp;
  8458.      FILE_BUF *op;
  8459. {
  8460.   int nargs;
  8461.   DEFINITION *defn = hp->value.defn;
  8462.   register U_CHAR *xbuf;
  8463.   int xbuf_len;
  8464.   int start_line = instack[indepth].lineno;
  8465.   int rest_args, rest_zero;
  8466.  
  8467.   CHECK_DEPTH (return;);
  8468.  
  8469.   /* it might not actually be a macro.  */
  8470.   if (hp->type != T_MACRO) {
  8471.     special_symbol (hp, op);
  8472.     return;
  8473.   }
  8474.  
  8475.   /* This macro is being used inside a #if, which means it must be */
  8476.   /* recorded as a precondition.  */
  8477.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  8478.     dump_single_macro (hp, pcp_outfile);
  8479.   
  8480.   nargs = defn->nargs;
  8481.  
  8482.   if (nargs >= 0) {
  8483.     register int i;
  8484.     struct argdata *args;
  8485.     char *parse_error = 0;
  8486.  
  8487.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  8488.  
  8489.     for (i = 0; i < nargs; i++) {
  8490.       args[i].raw = (U_CHAR *) "";
  8491.       args[i].expanded = 0;
  8492.       args[i].raw_length = args[i].expand_length
  8493.     = args[i].stringified_length = 0;
  8494.       args[i].free1 = args[i].free2 = 0;
  8495.       args[i].use_count = 0;
  8496.     }
  8497.  
  8498.     /* Parse all the macro args that are supplied.  I counts them.
  8499.        The first NARGS args are stored in ARGS.
  8500.        The rest are discarded.
  8501.        If rest_args is set then we assume macarg absorbed the rest of the args.
  8502.        */
  8503.     i = 0;
  8504.     rest_args = 0;
  8505.     do {
  8506.       /* Discard the open-parenthesis or comma before the next arg.  */
  8507.       ++instack[indepth].bufp;
  8508.       if (rest_args)
  8509.     continue;
  8510.       if (i < nargs || (nargs == 0 && i == 0)) {
  8511.     /* if we are working on last arg which absorbs rest of args... */
  8512.     if (i == nargs - 1 && defn->rest_args)
  8513.       rest_args = 1;
  8514.     parse_error = macarg (&args[i], rest_args);
  8515.       }
  8516.       else
  8517.     parse_error = macarg (NULL_PTR, 0);
  8518.       if (parse_error) {
  8519.     error_with_line (line_for_error (start_line), parse_error);
  8520.     break;
  8521.       }
  8522.       i++;
  8523.     } while (*instack[indepth].bufp != ')');
  8524.  
  8525.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  8526.     if (i == 1) {
  8527.       register U_CHAR *bp = args[0].raw;
  8528.       register U_CHAR *lim = bp + args[0].raw_length;
  8529.       /* cpp.texi says for foo ( ) we provide one argument.
  8530.      However, if foo wants just 0 arguments, treat this as 0.  */
  8531.       if (nargs == 0)
  8532.     while (bp != lim && is_space[*bp]) bp++;
  8533.       if (bp == lim)
  8534.     i = 0;
  8535.     }
  8536.  
  8537.     /* Don't output an error message if we have already output one for
  8538.        a parse error above.  */
  8539.     rest_zero = 0;
  8540.     if (nargs == 0 && i > 0) {
  8541.       if (! parse_error)
  8542.     error ("arguments given to macro `%s'", hp->name);
  8543.     } else if (i < nargs) {
  8544.       /* traditional C allows foo() if foo wants one argument.  */
  8545.       if (nargs == 1 && i == 0 && traditional)
  8546.     ;
  8547.       /* the rest args token is allowed to absorb 0 tokens */
  8548.       else if (i == nargs - 1 && defn->rest_args)
  8549.     rest_zero = 1;
  8550.       else if (parse_error)
  8551.     ;
  8552.       else if (i == 0)
  8553.     error ("macro `%s' used without args", hp->name);
  8554.       else if (i == 1)
  8555.     error ("macro `%s' used with just one arg", hp->name);
  8556.       else
  8557.     error ("macro `%s' used with only %d args", hp->name, i);
  8558.     } else if (i > nargs) {
  8559.       if (! parse_error)
  8560.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  8561.     }
  8562.  
  8563.     /* Swallow the closeparen.  */
  8564.     ++instack[indepth].bufp;
  8565.  
  8566.     /* If macro wants zero args, we parsed the arglist for checking only.
  8567.        Read directly from the macro definition.  */
  8568.     if (nargs == 0) {
  8569.       xbuf = defn->expansion;
  8570.       xbuf_len = defn->length;
  8571.     } else {
  8572.       register U_CHAR *exp = defn->expansion;
  8573.       register int offset;    /* offset in expansion,
  8574.                    copied a piece at a time */
  8575.       register int totlen;    /* total amount of exp buffer filled so far */
  8576.  
  8577.       register struct reflist *ap, *last_ap;
  8578.  
  8579.       /* Macro really takes args.  Compute the expansion of this call.  */
  8580.  
  8581.       /* Compute length in characters of the macro's expansion.
  8582.      Also count number of times each arg is used.  */
  8583.       xbuf_len = defn->length;
  8584.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  8585.     if (ap->stringify)
  8586.       xbuf_len += args[ap->argno].stringified_length;
  8587.     else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
  8588.       /* Add 4 for two newline-space markers to prevent
  8589.          token concatenation.  */
  8590.       xbuf_len += args[ap->argno].raw_length + 4;
  8591.     else {
  8592.       /* We have an ordinary (expanded) occurrence of the arg.
  8593.          So compute its expansion, if we have not already.  */
  8594.       if (args[ap->argno].expanded == 0) {
  8595.         FILE_BUF obuf;
  8596.         obuf = expand_to_temp_buffer (args[ap->argno].raw,
  8597.                       args[ap->argno].raw + args[ap->argno].raw_length,
  8598.                       1, 0);
  8599.  
  8600.         args[ap->argno].expanded = obuf.buf;
  8601.         args[ap->argno].expand_length = obuf.length;
  8602.         args[ap->argno].free2 = obuf.buf;
  8603.       }
  8604.  
  8605.       /* Add 4 for two newline-space markers to prevent
  8606.          token concatenation.  */
  8607.       xbuf_len += args[ap->argno].expand_length + 4;
  8608.     }
  8609.     if (args[ap->argno].use_count < 10)
  8610.       args[ap->argno].use_count++;
  8611.       }
  8612.  
  8613.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  8614.  
  8615.       /* Generate in XBUF the complete expansion
  8616.      with arguments substituted in.
  8617.      TOTLEN is the total size generated so far.
  8618.      OFFSET is the index in the definition
  8619.      of where we are copying from.  */
  8620.       offset = totlen = 0;
  8621.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  8622.        last_ap = ap, ap = ap->next) {
  8623.     register struct argdata *arg = &args[ap->argno];
  8624.     int count_before = totlen;
  8625.  
  8626.     /* Add chars to XBUF.  */
  8627.     for (i = 0; i < ap->nchars; i++, offset++)
  8628.       xbuf[totlen++] = exp[offset];
  8629.  
  8630.     /* If followed by an empty rest arg with concatenation,
  8631.        delete the last run of nonwhite chars.  */
  8632.     if (rest_zero && totlen > count_before
  8633.         && ((ap->rest_args && ap->raw_before != 0)
  8634.         || (last_ap != NULL && last_ap->rest_args
  8635.             && last_ap->raw_after != 0))) {
  8636.       /* Delete final whitespace.  */
  8637.       while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
  8638.         totlen--;
  8639.       }
  8640.  
  8641.       /* Delete the nonwhites before them.  */
  8642.       while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
  8643.         totlen--;
  8644.       }
  8645.     }
  8646.  
  8647.     if (ap->stringify != 0) {
  8648.       int arglen = arg->raw_length;
  8649.       int escaped = 0;
  8650.       int in_string = 0;
  8651.       int c;
  8652.       i = 0;
  8653.       while (i < arglen
  8654.          && (c = arg->raw[i], is_space[c]))
  8655.         i++;
  8656.       while (i < arglen
  8657.          && (c = arg->raw[arglen - 1], is_space[c]))
  8658.         arglen--;
  8659.       if (!traditional)
  8660.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  8661.       for (; i < arglen; i++) {
  8662.         c = arg->raw[i];
  8663.  
  8664.         /* Special markers Newline Space
  8665.            generate nothing for a stringified argument.  */
  8666.         if (c == '\n' && arg->raw[i+1] != '\n') {
  8667.           i++;
  8668.           continue;
  8669.         }
  8670.  
  8671.         /* Internal sequences of whitespace are replaced by one space
  8672.            except within an string or char token.  */
  8673.         if (! in_string
  8674.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  8675.           while (1) {
  8676.         /* Note that Newline Space does occur within whitespace
  8677.            sequences; consider it part of the sequence.  */
  8678.         if (c == '\n' && is_space[arg->raw[i+1]])
  8679.           i += 2;
  8680.         else if (c != '\n' && is_space[c])
  8681.           i++;
  8682.         else break;
  8683.         c = arg->raw[i];
  8684.           }
  8685.           i--;
  8686.           c = ' ';
  8687.         }
  8688.  
  8689.         if (escaped)
  8690.           escaped = 0;
  8691.         else {
  8692.           if (c == '\\')
  8693.         escaped = 1;
  8694.           if (in_string) {
  8695.         if (c == in_string)
  8696.           in_string = 0;
  8697.           } else if (c == '\"' || c == '\'')
  8698.         in_string = c;
  8699.         }
  8700.  
  8701.         /* Escape these chars */
  8702.         if (c == '\"' || (in_string && c == '\\'))
  8703.           xbuf[totlen++] = '\\';
  8704.         if (isprint (c))
  8705.           xbuf[totlen++] = c;
  8706.         else {
  8707.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  8708.           totlen += 4;
  8709.         }
  8710.       }
  8711.       if (!traditional)
  8712.         xbuf[totlen++] = '\"'; /* insert ending quote */
  8713.     } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
  8714.       U_CHAR *p1 = arg->raw;
  8715.       U_CHAR *l1 = p1 + arg->raw_length;
  8716.       if (ap->raw_before != 0) {
  8717.         while (p1 != l1 && is_space[*p1]) p1++;
  8718.         while (p1 != l1 && is_idchar[*p1])
  8719.           xbuf[totlen++] = *p1++;
  8720.         /* Delete any no-reexpansion marker that follows
  8721.            an identifier at the beginning of the argument
  8722.            if the argument is concatenated with what precedes it.  */
  8723.         if (p1[0] == '\n' && p1[1] == '-')
  8724.           p1 += 2;
  8725.       } else if (!traditional) {
  8726.       /* Ordinary expanded use of the argument.
  8727.          Put in newline-space markers to prevent token pasting.  */
  8728.         xbuf[totlen++] = '\n';
  8729.         xbuf[totlen++] = ' ';
  8730.       }
  8731.       if (ap->raw_after != 0) {
  8732.         /* Arg is concatenated after: delete trailing whitespace,
  8733.            whitespace markers, and no-reexpansion markers.  */
  8734.         while (p1 != l1) {
  8735.           if (is_space[l1[-1]]) l1--;
  8736.           else if (l1[-1] == '-') {
  8737.         U_CHAR *p2 = l1 - 1;
  8738.         /* If a `-' is preceded by an odd number of newlines then it
  8739.            and the last newline are a no-reexpansion marker.  */
  8740.         while (p2 != p1 && p2[-1] == '\n') p2--;
  8741.         if ((l1 - 1 - p2) & 1) {
  8742.           l1 -= 2;
  8743.         }
  8744.         else break;
  8745.           }
  8746.           else break;
  8747.         }
  8748.       }
  8749.  
  8750.       bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
  8751.       totlen += l1 - p1;
  8752.       if (!traditional && ap->raw_after == 0) {
  8753.         /* Ordinary expanded use of the argument.
  8754.            Put in newline-space markers to prevent token pasting.  */
  8755.         xbuf[totlen++] = '\n';
  8756.         xbuf[totlen++] = ' ';
  8757.       }
  8758.     } else {
  8759.       /* Ordinary expanded use of the argument.
  8760.          Put in newline-space markers to prevent token pasting.  */
  8761.       if (!traditional) {
  8762.         xbuf[totlen++] = '\n';
  8763.         xbuf[totlen++] = ' ';
  8764.       }
  8765.       bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
  8766.          arg->expand_length);
  8767.       totlen += arg->expand_length;
  8768.       if (!traditional) {
  8769.         xbuf[totlen++] = '\n';
  8770.         xbuf[totlen++] = ' ';
  8771.       }
  8772.       /* If a macro argument with newlines is used multiple times,
  8773.          then only expand the newlines once.  This avoids creating output
  8774.          lines which don't correspond to any input line, which confuses
  8775.          gdb and gcov.  */
  8776.       if (arg->use_count > 1 && arg->newlines > 0) {
  8777.         /* Don't bother doing change_newlines for subsequent
  8778.            uses of arg.  */
  8779.         arg->use_count = 1;
  8780.         arg->expand_length
  8781.           = change_newlines (arg->expanded, arg->expand_length);
  8782.       }
  8783.     }
  8784.  
  8785.     if (totlen > xbuf_len)
  8786.       abort ();
  8787.       }
  8788.  
  8789.       /* if there is anything left of the definition
  8790.      after handling the arg list, copy that in too. */
  8791.  
  8792.       for (i = offset; i < defn->length; i++) {
  8793.     /* if we've reached the end of the macro */
  8794.     if (exp[i] == ')')
  8795.       rest_zero = 0;
  8796.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  8797.            && last_ap->raw_after != 0))
  8798.       xbuf[totlen++] = exp[i];
  8799.       }
  8800.  
  8801.       xbuf[totlen] = 0;
  8802.       xbuf_len = totlen;
  8803.  
  8804.       for (i = 0; i < nargs; i++) {
  8805.     if (args[i].free1 != 0)
  8806.       free (args[i].free1);
  8807.     if (args[i].free2 != 0)
  8808.       free (args[i].free2);
  8809.       }
  8810.     }
  8811.   } else {
  8812.     xbuf = defn->expansion;
  8813.     xbuf_len = defn->length;
  8814.   }
  8815.  
  8816.   /* Now put the expansion on the input stack
  8817.      so our caller will commence reading from it.  */
  8818.   {
  8819.     register FILE_BUF *ip2;
  8820.  
  8821.     ip2 = &instack[++indepth];
  8822.  
  8823.     ip2->fname = 0;
  8824.     ip2->nominal_fname = 0;
  8825.     /* This may not be exactly correct, but will give much better error
  8826.        messages for nested macro calls than using a line number of zero.  */
  8827.     ip2->lineno = start_line;
  8828.     ip2->buf = xbuf;
  8829.     ip2->length = xbuf_len;
  8830.     ip2->bufp = xbuf;
  8831.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  8832.     ip2->macro = hp;
  8833.     ip2->if_stack = if_stack;
  8834.     ip2->system_header_p = 0;
  8835.  
  8836.     /* Recursive macro use sometimes works traditionally.
  8837.        #define foo(x,y) bar (x (y,0), y)
  8838.        foo (foo, baz)  */
  8839.  
  8840.     if (!traditional)
  8841.       hp->type = T_DISABLED;
  8842.   }
  8843. }
  8844.  
  8845. /*
  8846.  * Parse a macro argument and store the info on it into *ARGPTR.
  8847.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  8848.  * Return nonzero to indicate a syntax error.
  8849.  */
  8850.  
  8851. static char *
  8852. macarg (argptr, rest_args)
  8853.      register struct argdata *argptr;
  8854.      int rest_args;
  8855. {
  8856.   FILE_BUF *ip = &instack[indepth];
  8857.   int paren = 0;
  8858.   int newlines = 0;
  8859.   int comments = 0;
  8860.   char *result = 0;
  8861.  
  8862.   /* Try to parse as much of the argument as exists at this
  8863.      input stack level.  */
  8864.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  8865.             &paren, &newlines, &comments, rest_args);
  8866.  
  8867.   /* If we find the end of the argument at this level,
  8868.      set up *ARGPTR to point at it in the input stack.  */
  8869.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  8870.       && bp != ip->buf + ip->length) {
  8871.     if (argptr != 0) {
  8872.       argptr->raw = ip->bufp;
  8873.       argptr->raw_length = bp - ip->bufp;
  8874.       argptr->newlines = newlines;
  8875.     }
  8876.     ip->bufp = bp;
  8877.   } else {
  8878.     /* This input stack level ends before the macro argument does.
  8879.        We must pop levels and keep parsing.
  8880.        Therefore, we must allocate a temporary buffer and copy
  8881.        the macro argument into it.  */
  8882.     int bufsize = bp - ip->bufp;
  8883.     int extra = newlines;
  8884.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  8885.     int final_start = 0;
  8886.  
  8887.     bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
  8888.     ip->bufp = bp;
  8889.     ip->lineno += newlines;
  8890.  
  8891.     while (bp == ip->buf + ip->length) {
  8892.       if (instack[indepth].macro == 0) {
  8893.     result = "unterminated macro call";
  8894.     break;
  8895.       }
  8896.       ip->macro->type = T_MACRO;
  8897.       if (ip->free_ptr)
  8898.     free (ip->free_ptr);
  8899.       ip = &instack[--indepth];
  8900.       newlines = 0;
  8901.       comments = 0;
  8902.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  8903.             &newlines, &comments, rest_args);
  8904.       final_start = bufsize;
  8905.       bufsize += bp - ip->bufp;
  8906.       extra += newlines;
  8907.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  8908.       bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
  8909.          bp - ip->bufp);
  8910.       ip->bufp = bp;
  8911.       ip->lineno += newlines;
  8912.     }
  8913.  
  8914.     /* Now, if arg is actually wanted, record its raw form,
  8915.        discarding comments and duplicating newlines in whatever
  8916.        part of it did not come from a macro expansion.
  8917.        EXTRA space has been preallocated for duplicating the newlines.
  8918.        FINAL_START is the index of the start of that part.  */
  8919.     if (argptr != 0) {
  8920.       argptr->raw = buffer;
  8921.       argptr->raw_length = bufsize;
  8922.       argptr->free1 = buffer;
  8923.       argptr->newlines = newlines;
  8924.       if ((newlines || comments) && ip->fname != 0)
  8925.     argptr->raw_length
  8926.       = final_start +
  8927.         discard_comments (argptr->raw + final_start,
  8928.                   argptr->raw_length - final_start,
  8929.                   newlines);
  8930.       argptr->raw[argptr->raw_length] = 0;
  8931.       if (argptr->raw_length > bufsize + extra)
  8932.     abort ();
  8933.     }
  8934.   }
  8935.  
  8936.   /* If we are not discarding this argument,
  8937.      macroexpand it and compute its length as stringified.
  8938.      All this info goes into *ARGPTR.  */
  8939.  
  8940.   if (argptr != 0) {
  8941.     register U_CHAR *buf, *lim;
  8942.     register int totlen;
  8943.  
  8944.     buf = argptr->raw;
  8945.     lim = buf + argptr->raw_length;
  8946.  
  8947.     while (buf != lim && is_space[*buf])
  8948.       buf++;
  8949.     while (buf != lim && is_space[lim[-1]])
  8950.       lim--;
  8951.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  8952.     while (buf != lim) {
  8953.       register U_CHAR c = *buf++;
  8954.       totlen++;
  8955.       /* Internal sequences of whitespace are replaced by one space
  8956.      in most cases, but not always.  So count all the whitespace
  8957.      in case we need to keep it all.  */
  8958. #if 0
  8959.       if (is_space[c])
  8960.     SKIP_ALL_WHITE_SPACE (buf);
  8961.       else
  8962. #endif
  8963.       if (c == '\"' || c == '\\') /* escape these chars */
  8964.     totlen++;
  8965.       else if (!isprint (c))
  8966.     totlen += 3;
  8967.     }
  8968.     argptr->stringified_length = totlen;
  8969.   }
  8970.   return result;
  8971. }
  8972.  
  8973. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  8974.    counting parens in *DEPTHPTR,
  8975.    and return if reach LIMIT
  8976.    or before a `)' that would make *DEPTHPTR negative
  8977.    or before a comma when *DEPTHPTR is zero.
  8978.    Single and double quotes are matched and termination
  8979.    is inhibited within them.  Comments also inhibit it.
  8980.    Value returned is pointer to stopping place.
  8981.  
  8982.    Increment *NEWLINES each time a newline is passed.
  8983.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  8984.    Set *COMMENTS to 1 if a comment is seen.  */
  8985.  
  8986. static U_CHAR *
  8987. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  8988.      U_CHAR *start;
  8989.      register U_CHAR *limit;
  8990.      int *depthptr, *newlines, *comments;
  8991.      int rest_args;
  8992. {
  8993.   register U_CHAR *bp = start;
  8994.  
  8995.   while (bp < limit) {
  8996.     switch (*bp) {
  8997.     case '(':
  8998.       (*depthptr)++;
  8999.       break;
  9000.     case ')':
  9001.       if (--(*depthptr) < 0)
  9002.     return bp;
  9003.       break;
  9004.     case '\\':
  9005.       /* Traditionally, backslash makes following char not special.  */
  9006.       if (bp + 1 < limit && traditional)
  9007.     {
  9008.       bp++;
  9009.       /* But count source lines anyway.  */
  9010.       if (*bp == '\n')
  9011.         ++*newlines;
  9012.     }
  9013.       break;
  9014.     case '\n':
  9015.       ++*newlines;
  9016.       break;
  9017.     case '/':
  9018.       if (bp[1] == '\\' && bp[2] == '\n')
  9019.     newline_fix (bp + 1);
  9020.       if (cplusplus_comments && bp[1] == '/') {
  9021.     *comments = 1;
  9022.     bp += 2;
  9023.     while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
  9024.       if (*bp == '\n') ++*newlines;
  9025.       bp++;
  9026.     }
  9027.     /* Now count the newline that we are about to skip.  */
  9028.     ++*newlines;
  9029.     break;
  9030.       }
  9031.       if (bp[1] != '*' || bp + 1 >= limit)
  9032.     break;
  9033.       *comments = 1;
  9034.       bp += 2;
  9035.       while (bp + 1 < limit) {
  9036.     if (bp[0] == '*'
  9037.         && bp[1] == '\\' && bp[2] == '\n')
  9038.       newline_fix (bp + 1);
  9039.     if (bp[0] == '*' && bp[1] == '/')
  9040.       break;
  9041.     if (*bp == '\n') ++*newlines;
  9042.     bp++;
  9043.       }
  9044.       break;
  9045.     case '\'':
  9046.     case '\"':
  9047.       {
  9048.     int quotec;
  9049.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  9050.       if (*bp == '\\') {
  9051.         bp++;
  9052.         if (*bp == '\n')
  9053.           ++*newlines;
  9054.         while (*bp == '\\' && bp[1] == '\n') {
  9055.           bp += 2;
  9056.         }
  9057.       } else if (*bp == '\n') {
  9058.         ++*newlines;
  9059.         if (quotec == '\'')
  9060.           break;
  9061.       }
  9062.     }
  9063.       }
  9064.       break;
  9065.     case ',':
  9066.       /* if we've returned to lowest level and we aren't absorbing all args */
  9067.       if ((*depthptr) == 0 && rest_args == 0)
  9068.     return bp;
  9069.       break;
  9070.     }
  9071.     bp++;
  9072.   }
  9073.  
  9074.   return bp;
  9075. }
  9076.  
  9077. /* Discard comments and duplicate newlines
  9078.    in the string of length LENGTH at START,
  9079.    except inside of string constants.
  9080.    The string is copied into itself with its beginning staying fixed.  
  9081.  
  9082.    NEWLINES is the number of newlines that must be duplicated.
  9083.    We assume that that much extra space is available past the end
  9084.    of the string.  */
  9085.  
  9086. static int
  9087. discard_comments (start, length, newlines)
  9088.      U_CHAR *start;
  9089.      int length;
  9090.      int newlines;
  9091. {
  9092.   register U_CHAR *ibp;
  9093.   register U_CHAR *obp;
  9094.   register U_CHAR *limit;
  9095.   register int c;
  9096.  
  9097.   /* If we have newlines to duplicate, copy everything
  9098.      that many characters up.  Then, in the second part,
  9099.      we will have room to insert the newlines
  9100.      while copying down.
  9101.      NEWLINES may actually be too large, because it counts
  9102.      newlines in string constants, and we don't duplicate those.
  9103.      But that does no harm.  */
  9104.   if (newlines > 0) {
  9105.     ibp = start + length;
  9106.     obp = ibp + newlines;
  9107.     limit = start;
  9108.     while (limit != ibp)
  9109.       *--obp = *--ibp;
  9110.   }
  9111.  
  9112.   ibp = start + newlines;
  9113.   limit = start + length + newlines;
  9114.   obp = start;
  9115.  
  9116.   while (ibp < limit) {
  9117.     *obp++ = c = *ibp++;
  9118.     switch (c) {
  9119.     case '\n':
  9120.       /* Duplicate the newline.  */
  9121.       *obp++ = '\n';
  9122.       break;
  9123.  
  9124.     case '\\':
  9125.       if (*ibp == '\n') {
  9126.     obp--;
  9127.     ibp++;
  9128.       }
  9129.       break;
  9130.  
  9131.     case '/':
  9132.       if (*ibp == '\\' && ibp[1] == '\n')
  9133.     newline_fix (ibp);
  9134.       /* Delete any comment.  */
  9135.       if (cplusplus_comments && ibp[0] == '/') {
  9136.     /* Comments are equivalent to spaces.  */
  9137.     obp[-1] = ' ';
  9138.     ibp++;
  9139.     while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
  9140.       ibp++;
  9141.     break;
  9142.       }
  9143.       if (ibp[0] != '*' || ibp + 1 >= limit)
  9144.     break;
  9145.       /* Comments are equivalent to spaces.
  9146.      For -traditional, a comment is equivalent to nothing.  */
  9147.       if (traditional)
  9148.     obp--;
  9149.       else
  9150.     obp[-1] = ' ';
  9151.       ibp++;
  9152.       while (ibp + 1 < limit) {
  9153.     if (ibp[0] == '*'
  9154.         && ibp[1] == '\\' && ibp[2] == '\n')
  9155.       newline_fix (ibp + 1);
  9156.     if (ibp[0] == '*' && ibp[1] == '/')
  9157.       break;
  9158.     ibp++;
  9159.       }
  9160.       ibp += 2;
  9161.       break;
  9162.  
  9163.     case '\'':
  9164.     case '\"':
  9165.       /* Notice and skip strings, so that we don't
  9166.      think that comments start inside them,
  9167.      and so we don't duplicate newlines in them.  */
  9168.       {
  9169.     int quotec = c;
  9170.     while (ibp < limit) {
  9171.       *obp++ = c = *ibp++;
  9172.       if (c == quotec)
  9173.         break;
  9174.       if (c == '\n' && quotec == '\'')
  9175.         break;
  9176.       if (c == '\\' && ibp < limit) {
  9177.         while (*ibp == '\\' && ibp[1] == '\n')
  9178.           ibp += 2;
  9179.         *obp++ = *ibp++;
  9180.       }
  9181.     }
  9182.       }
  9183.       break;
  9184.     }
  9185.   }
  9186.  
  9187.   return obp - start;
  9188. }
  9189.  
  9190. /* Turn newlines to spaces in the string of length LENGTH at START,
  9191.    except inside of string constants.
  9192.    The string is copied into itself with its beginning staying fixed.  */
  9193.  
  9194. static int
  9195. change_newlines (start, length)
  9196.      U_CHAR *start;
  9197.      int length;
  9198. {
  9199.   register U_CHAR *ibp;
  9200.   register U_CHAR *obp;
  9201.   register U_CHAR *limit;
  9202.   register int c;
  9203.  
  9204.   ibp = start;
  9205.   limit = start + length;
  9206.   obp = start;
  9207.  
  9208.   while (ibp < limit) {
  9209.     *obp++ = c = *ibp++;
  9210.     switch (c) {
  9211.     case '\n':
  9212.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  9213.      string.  Skip past the newline and its duplicate.
  9214.      Put a space in the output.  */
  9215.       if (*ibp == '\n')
  9216.     {
  9217.       ibp++;
  9218.       obp--;
  9219.       *obp++ = ' ';
  9220.     }
  9221.       break;
  9222.  
  9223.     case '\'':
  9224.     case '\"':
  9225.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  9226.       {
  9227.     int quotec = c;
  9228.     while (ibp < limit) {
  9229.       *obp++ = c = *ibp++;
  9230.       if (c == quotec)
  9231.         break;
  9232.       if (c == '\n' && quotec == '\'')
  9233.         break;
  9234.     }
  9235.       }
  9236.       break;
  9237.     }
  9238.   }
  9239.  
  9240.   return obp - start;
  9241. }
  9242.  
  9243. /*
  9244.  * my_strerror - return the descriptive text associated with an `errno' code.
  9245.  */
  9246.  
  9247. char *
  9248. my_strerror (errnum)
  9249.      int errnum;
  9250. {
  9251.   char *result;
  9252.  
  9253. #ifndef VMS
  9254. #ifndef HAVE_STRERROR
  9255.   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
  9256. #else
  9257.   result = strerror (errnum);
  9258. #endif
  9259. #else    /* VMS */
  9260.   /* VAXCRTL's strerror() takes an optional second argument, which only
  9261.      matters when the first argument is EVMSERR.  However, it's simplest
  9262.      just to pass it unconditionally.  `vaxc$errno' is declared in
  9263.      <errno.h>, and maintained by the library in parallel with `errno'.
  9264.      We assume that caller's `errnum' either matches the last setting of
  9265.      `errno' by the library or else does not have the value `EVMSERR'.  */
  9266.  
  9267.   result = strerror (errnum, vaxc$errno);
  9268. #endif
  9269.  
  9270.   if (!result)
  9271.     result = "undocumented I/O error";
  9272.  
  9273.   return result;
  9274. }
  9275.  
  9276. /*
  9277.  * error - print error message and increment count of errors.
  9278.  */
  9279.  
  9280. void
  9281. error (PRINTF_ALIST (msg))
  9282.      PRINTF_DCL (msg)
  9283. {
  9284.   va_list args;
  9285.  
  9286.   VA_START (args, msg);
  9287.   verror (msg, args);
  9288.   va_end (args);
  9289. }
  9290.  
  9291. static void
  9292. verror (msg, args)
  9293.      char *msg;
  9294.      va_list args;
  9295. {
  9296.   int i;
  9297.   FILE_BUF *ip = NULL;
  9298.  
  9299.   print_containing_files ();
  9300.  
  9301.   for (i = indepth; i >= 0; i--)
  9302.     if (instack[i].fname != NULL) {
  9303.       ip = &instack[i];
  9304.       break;
  9305.     }
  9306.  
  9307. #ifdef VA_REPORT_EVENT
  9308.   VA_REPORT_EVENT (0, NULL,
  9309.         ip ? ip->nominal_fname : NULL,
  9310.         ip ? ip->lineno : 0,
  9311.         msg, args);
  9312. #endif
  9313.  
  9314.   if (ip != NULL)
  9315.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  9316.   vfprintf (stderr, msg, args);
  9317.   fprintf (stderr, "\n");
  9318.   errors++;
  9319. }
  9320.  
  9321. /* Error including a message from `errno'.  */
  9322.  
  9323. static void
  9324. error_from_errno (name)
  9325.      char *name;
  9326. {
  9327.   int i;
  9328.   FILE_BUF *ip = NULL;
  9329.  
  9330.   print_containing_files ();
  9331.  
  9332.   for (i = indepth; i >= 0; i--)
  9333.     if (instack[i].fname != NULL) {
  9334.       ip = &instack[i];
  9335.       break;
  9336.     }
  9337.  
  9338. #ifdef REPORT_EVENT
  9339.   REPORT_EVENT (0, NULL,
  9340.         ip ? ip->nominal_fname : NULL,
  9341.         ip ? ip->lineno : 0,
  9342.         "%s: %s", name,
  9343.         (errno < sys_nerr)
  9344.         ? sys_errlist[errno]
  9345.         : "undocumented I/O error",
  9346.         0);
  9347. #endif
  9348.  
  9349.   if (ip != NULL)
  9350. #ifdef WINNT
  9351.     fprintf (stderr, "%s(%d): ", ip->nominal_fname, ip->lineno);
  9352. #else /* WINNT */
  9353.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  9354. #endif /* WINNT */
  9355.  
  9356.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  9357.  
  9358.   errors++;
  9359. }
  9360.  
  9361. /* Print error message but don't count it.  */
  9362.  
  9363. void
  9364. warning (PRINTF_ALIST (msg))
  9365.      PRINTF_DCL (msg)
  9366. {
  9367.   va_list args;
  9368.  
  9369.   VA_START (args, msg);
  9370.   vwarning (msg, args);
  9371.   va_end (args);
  9372. }
  9373.  
  9374. static void
  9375. vwarning (msg, args)
  9376.      char *msg;
  9377.      va_list args;
  9378. {
  9379.   int i;
  9380.   FILE_BUF *ip = NULL;
  9381.  
  9382.   if (inhibit_warnings)
  9383.     return;
  9384.  
  9385.   if (warnings_are_errors)
  9386.     errors++;
  9387.  
  9388.   print_containing_files ();
  9389.  
  9390.   for (i = indepth; i >= 0; i--)
  9391.     if (instack[i].fname != NULL) {
  9392.       ip = &instack[i];
  9393.       break;
  9394.     }
  9395.  
  9396. #ifdef VA_REPORT_EVENT
  9397.   VA_REPORT_EVENT (1, NULL,
  9398.         ip ? ip->nominal_fname : NULL,
  9399.         ip ? ip->lineno : 0,
  9400.         msg, args);
  9401. #endif
  9402.  
  9403.   if (ip != NULL)
  9404. #ifdef WINNT
  9405.     fprintf (stderr, "%s(%d): ", ip->nominal_fname, ip->lineno);
  9406. #else /* WINNT */
  9407.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  9408. #endif /* WINNT */
  9409.   fprintf (stderr, "warning: ");
  9410.   vfprintf (stderr, msg, args);
  9411.   fprintf (stderr, "\n");
  9412. }
  9413.  
  9414. static void
  9415. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9416. error_with_line (int line, PRINTF_ALIST (msg))
  9417. #else
  9418. error_with_line (line, PRINTF_ALIST (msg))
  9419.      int line;
  9420.      PRINTF_DCL (msg)
  9421. #endif
  9422. {
  9423.   va_list args;
  9424.  
  9425.   VA_START (args, msg);
  9426.   verror_with_line (line, msg, args);
  9427.   va_end (args);
  9428. }
  9429.  
  9430. static void
  9431. verror_with_line (line, msg, args)
  9432.      int line;
  9433.      char *msg;
  9434.      va_list args;
  9435. {
  9436.   int i;
  9437.   FILE_BUF *ip = NULL;
  9438.  
  9439.   print_containing_files ();
  9440.  
  9441.   for (i = indepth; i >= 0; i--)
  9442.     if (instack[i].fname != NULL) {
  9443.       ip = &instack[i];
  9444.       break;
  9445.     }
  9446.  
  9447. #ifdef VA_REPORT_EVENT
  9448.   VA_REPORT_EVENT (0, NULL,
  9449.         ip ? ip->nominal_fname : NULL,
  9450.         line, msg, args);
  9451. #endif
  9452.  
  9453.   if (ip != NULL)
  9454. #ifdef WINNT
  9455.     fprintf (stderr, "%s(%d): ", ip->nominal_fname, line);
  9456. #else /* WINNT */
  9457.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  9458. #endif /* WINNT */
  9459.   vfprintf (stderr, msg, args);
  9460.   fprintf (stderr, "\n");
  9461.   errors++;
  9462. }
  9463.  
  9464. static void
  9465. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9466. warning_with_line (int line, PRINTF_ALIST (msg))
  9467. #else
  9468. warning_with_line (line, PRINTF_ALIST (msg))
  9469.      int line;
  9470.      PRINTF_DCL (msg)
  9471. #endif
  9472. {
  9473.   va_list args;
  9474.  
  9475.   VA_START (args, msg);
  9476.   vwarning_with_line (line, msg, args);
  9477.   va_end (args);
  9478. }
  9479.  
  9480. static void
  9481. vwarning_with_line (line, msg, args)
  9482.      int line;
  9483.      char *msg;
  9484.      va_list args;
  9485. {
  9486.   int i;
  9487.   FILE_BUF *ip = NULL;
  9488.  
  9489.   if (inhibit_warnings)
  9490.     return;
  9491.  
  9492.   if (warnings_are_errors)
  9493.     errors++;
  9494.  
  9495.   print_containing_files ();
  9496.  
  9497.   for (i = indepth; i >= 0; i--)
  9498.     if (instack[i].fname != NULL) {
  9499.       ip = &instack[i];
  9500.       break;
  9501.     }
  9502.  
  9503. #ifdef VA_REPORT_EVENT
  9504.   VA_REPORT_EVENT (0, NULL,
  9505.         ip ? ip->nominal_fname : NULL,
  9506.         line, msg, args);
  9507. #endif
  9508.  
  9509.   if (ip != NULL)
  9510. #ifdef WINNT
  9511.     fprintf (stderr, line ? "%s(%d) " : "%s: ", ip->nominal_fname, line);
  9512. #else /* WINNT */
  9513.     fprintf (stderr, line ? "%s:%d: " : "%s: ", ip->nominal_fname, line);
  9514. #endif /* WINNT */
  9515.   fprintf (stderr, "warning: ");
  9516.   vfprintf (stderr, msg, args);
  9517.   fprintf (stderr, "\n");
  9518. }
  9519.  
  9520. /* print an error message and maybe count it.  */
  9521.  
  9522. void
  9523. pedwarn (PRINTF_ALIST (msg))
  9524.      PRINTF_DCL (msg)
  9525. {
  9526.   va_list args;
  9527.  
  9528.   VA_START (args, msg);
  9529.   if (pedantic_errors)
  9530.     verror (msg, args);
  9531.   else
  9532.     vwarning (msg, args);
  9533.   va_end (args);
  9534. }
  9535.  
  9536. void
  9537. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9538. pedwarn_with_line (int line, PRINTF_ALIST (msg))
  9539. #else
  9540. pedwarn_with_line (line, PRINTF_ALIST (msg))
  9541.      int line;
  9542.      PRINTF_DCL (msg)
  9543. #endif
  9544. {
  9545.   va_list args;
  9546.  
  9547.   VA_START (args, msg);
  9548.   if (pedantic_errors)
  9549.     verror_with_line (line, msg, args);
  9550.   else
  9551.     vwarning_with_line (line, msg, args);
  9552.   va_end (args);
  9553. }
  9554.  
  9555. /* Report a warning (or an error if pedantic_errors)
  9556.    giving specified file name and line number, not current.  */
  9557.  
  9558. static void
  9559. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9560. pedwarn_with_file_and_line (char *file, int line, PRINTF_ALIST (msg))
  9561. #else
  9562. pedwarn_with_file_and_line (file, line, PRINTF_ALIST (msg))
  9563.      char *file;
  9564.      int line;
  9565.      PRINTF_DCL (msg)
  9566. #endif
  9567. {
  9568.   va_list args;
  9569.  
  9570.   if (!pedantic_errors && inhibit_warnings)
  9571.     return;
  9572.   if (file != NULL)
  9573. #ifdef WINNT
  9574.     fprintf (stderr, "%s(%d): ", file, line);
  9575. #else /* WINNT */
  9576.     fprintf (stderr, "%s:%d: ", file, line);
  9577. #endif /* WINNT */
  9578.   if (pedantic_errors)
  9579.     errors++;
  9580.   if (!pedantic_errors)
  9581.     fprintf (stderr, "warning: ");
  9582.   VA_START (args, msg);
  9583. #ifdef VA_REPORT_EVENT
  9584.   VA_REPORT_EVENT (pedantic_errors ? 0 : 1,
  9585.         NULL, file, line, msg, args);
  9586. #endif
  9587.   vfprintf (stderr, msg, args);
  9588.   va_end (args);
  9589.   fprintf (stderr, "\n");
  9590. }
  9591.  
  9592. /* Print the file names and line numbers of the #include
  9593.    directives which led to the current file.  */
  9594.  
  9595. static void
  9596. print_containing_files ()
  9597. {
  9598.   FILE_BUF *ip = NULL;
  9599.   int i;
  9600.   int first = 1;
  9601.  
  9602.   /* If stack of files hasn't changed since we last printed
  9603.      this info, don't repeat it.  */
  9604.   if (last_error_tick == input_file_stack_tick)
  9605.     return;
  9606.  
  9607.   for (i = indepth; i >= 0; i--)
  9608.     if (instack[i].fname != NULL) {
  9609.       ip = &instack[i];
  9610.       break;
  9611.     }
  9612.  
  9613.   /* Give up if we don't find a source file.  */
  9614.   if (ip == NULL)
  9615.     return;
  9616.  
  9617.   /* Find the other, outer source files.  */
  9618.   for (i--; i >= 0; i--)
  9619.     if (instack[i].fname != NULL) {
  9620.       ip = &instack[i];
  9621.       if (first) {
  9622.     first = 0;
  9623.     fprintf (stderr, "In file included");
  9624.       } else {
  9625.     fprintf (stderr, ",\n                ");
  9626.       }
  9627.  
  9628.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  9629.     }
  9630.   if (! first)
  9631.     fprintf (stderr, ":\n");
  9632.  
  9633.   /* Record we have printed the status as of this time.  */
  9634.   last_error_tick = input_file_stack_tick;
  9635. }
  9636.  
  9637. /* Return the line at which an error occurred.
  9638.    The error is not necessarily associated with the current spot
  9639.    in the input stack, so LINE says where.  LINE will have been
  9640.    copied from ip->lineno for the current input level.
  9641.    If the current level is for a file, we return LINE.
  9642.    But if the current level is not for a file, LINE is meaningless.
  9643.    In that case, we return the lineno of the innermost file.  */
  9644.  
  9645. static int
  9646. line_for_error (line)
  9647.      int line;
  9648. {
  9649.   int i;
  9650.   int line1 = line;
  9651.  
  9652.   for (i = indepth; i >= 0; ) {
  9653.     if (instack[i].fname != 0)
  9654.       return line1;
  9655.     i--;
  9656.     if (i < 0)
  9657.       return 0;
  9658.     line1 = instack[i].lineno;
  9659.   }
  9660.   abort ();
  9661.   /*NOTREACHED*/
  9662.   return 0;
  9663. }
  9664.  
  9665. /*
  9666.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  9667.  *
  9668.  * As things stand, nothing is ever placed in the output buffer to be
  9669.  * removed again except when it's KNOWN to be part of an identifier,
  9670.  * so flushing and moving down everything left, instead of expanding,
  9671.  * should work ok.
  9672.  */
  9673.  
  9674. /* You might think void was cleaner for the return type,
  9675.    but that would get type mismatch in check_expand in strict ANSI.  */
  9676. static int
  9677. grow_outbuf (obuf, needed)
  9678.      register FILE_BUF *obuf;
  9679.      register int needed;
  9680. {
  9681.   register U_CHAR *p;
  9682.   int minsize;
  9683.  
  9684.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  9685.     return 0;
  9686.  
  9687.   /* Make it at least twice as big as it is now.  */
  9688.   obuf->length *= 2;
  9689.   /* Make it have at least 150% of the free space we will need.  */
  9690.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  9691.   if (minsize > obuf->length)
  9692.     obuf->length = minsize;
  9693.  
  9694.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  9695.     memory_full ();
  9696.  
  9697.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  9698.   obuf->buf = p;
  9699.  
  9700.   return 0;
  9701. }
  9702.  
  9703. /* Symbol table for macro names and special symbols */
  9704.  
  9705. /*
  9706.  * install a name in the main hash table, even if it is already there.
  9707.  *   name stops with first non alphanumeric, except leading '#'.
  9708.  * caller must check against redefinition if that is desired.
  9709.  * delete_macro () removes things installed by install () in fifo order.
  9710.  * this is important because of the `defined' special symbol used
  9711.  * in #if, and also if pushdef/popdef directives are ever implemented.
  9712.  *
  9713.  * If LEN is >= 0, it is the length of the name.
  9714.  * Otherwise, compute the length by scanning the entire name.
  9715.  *
  9716.  * If HASH is >= 0, it is the precomputed hash code.
  9717.  * Otherwise, compute the hash code.
  9718.  */
  9719. static HASHNODE *
  9720. install (name, len, type, value, hash)
  9721.      U_CHAR *name;
  9722.      int len;
  9723.      enum node_type type;
  9724.      char *value;
  9725.      int hash;
  9726. {
  9727.   register HASHNODE *hp;
  9728.   register int i, bucket;
  9729.   register U_CHAR *p, *q;
  9730.  
  9731.   if (len < 0) {
  9732.     p = name;
  9733.     while (is_idchar[*p])
  9734.       p++;
  9735.     len = p - name;
  9736.   }
  9737.  
  9738.   if (hash < 0)
  9739.     hash = hashf (name, len, HASHSIZE);
  9740.  
  9741.   i = sizeof (HASHNODE) + len + 1;
  9742.   hp = (HASHNODE *) xmalloc (i);
  9743.   bucket = hash;
  9744.   hp->bucket_hdr = &hashtab[bucket];
  9745.   hp->next = hashtab[bucket];
  9746.   hashtab[bucket] = hp;
  9747.   hp->prev = NULL;
  9748.   if (hp->next != NULL)
  9749.     hp->next->prev = hp;
  9750.   hp->type = type;
  9751.   hp->length = len;
  9752.   hp->value.cpval = value;
  9753.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  9754.   p = hp->name;
  9755.   q = name;
  9756.   for (i = 0; i < len; i++)
  9757.     *p++ = *q++;
  9758.   hp->name[len] = 0;
  9759.   return hp;
  9760. }
  9761.  
  9762. /*
  9763.  * find the most recent hash node for name name (ending with first
  9764.  * non-identifier char) installed by install
  9765.  *
  9766.  * If LEN is >= 0, it is the length of the name.
  9767.  * Otherwise, compute the length by scanning the entire name.
  9768.  *
  9769.  * If HASH is >= 0, it is the precomputed hash code.
  9770.  * Otherwise, compute the hash code.
  9771.  */
  9772. HASHNODE *
  9773. lookup (name, len, hash)
  9774.      U_CHAR *name;
  9775.      int len;
  9776.      int hash;
  9777. {
  9778.   register U_CHAR *bp;
  9779.   register HASHNODE *bucket;
  9780.  
  9781.   if (len < 0) {
  9782.     for (bp = name; is_idchar[*bp]; bp++) ;
  9783.     len = bp - name;
  9784.   }
  9785.  
  9786.   if (hash < 0)
  9787.     hash = hashf (name, len, HASHSIZE);
  9788.  
  9789.   bucket = hashtab[hash];
  9790.   while (bucket) {
  9791.     if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
  9792.       return bucket;
  9793.     bucket = bucket->next;
  9794.   }
  9795.   return NULL;
  9796. }
  9797.  
  9798. /*
  9799.  * Delete a hash node.  Some weirdness to free junk from macros.
  9800.  * More such weirdness will have to be added if you define more hash
  9801.  * types that need it.
  9802.  */
  9803.  
  9804. /* Note that the DEFINITION of a macro is removed from the hash table
  9805.    but its storage is not freed.  This would be a storage leak
  9806.    except that it is not reasonable to keep undefining and redefining
  9807.    large numbers of macros many times.
  9808.    In any case, this is necessary, because a macro can be #undef'd
  9809.    in the middle of reading the arguments to a call to it.
  9810.    If #undef freed the DEFINITION, that would crash.  */
  9811.  
  9812. static void
  9813. delete_macro (hp)
  9814.      HASHNODE *hp;
  9815. {
  9816.  
  9817.   if (hp->prev != NULL)
  9818.     hp->prev->next = hp->next;
  9819.   if (hp->next != NULL)
  9820.     hp->next->prev = hp->prev;
  9821.  
  9822.   /* make sure that the bucket chain header that
  9823.      the deleted guy was on points to the right thing afterwards. */
  9824.   if (hp == *hp->bucket_hdr)
  9825.     *hp->bucket_hdr = hp->next;
  9826.  
  9827. #if 0
  9828.   if (hp->type == T_MACRO) {
  9829.     DEFINITION *d = hp->value.defn;
  9830.     struct reflist *ap, *nextap;
  9831.  
  9832.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  9833.       nextap = ap->next;
  9834.       free (ap);
  9835.     }
  9836.     free (d);
  9837.   }
  9838. #endif
  9839.   free (hp);
  9840. }
  9841.  
  9842. /*
  9843.  * return hash function on name.  must be compatible with the one
  9844.  * computed a step at a time, elsewhere
  9845.  */
  9846. static int
  9847. hashf (name, len, hashsize)
  9848.      register U_CHAR *name;
  9849.      register int len;
  9850.      int hashsize;
  9851. {
  9852.   register int r = 0;
  9853.  
  9854.   while (len--)
  9855.     r = HASHSTEP (r, *name++);
  9856.  
  9857.   return MAKE_POS (r) % hashsize;
  9858. }
  9859.  
  9860.  
  9861. /* Dump the definition of a single macro HP to OF.  */
  9862. static void
  9863. dump_single_macro (hp, of)
  9864.      register HASHNODE *hp;
  9865.      FILE *of;
  9866. {
  9867.   register DEFINITION *defn = hp->value.defn;
  9868.   struct reflist *ap;
  9869.   int offset;
  9870.   int concat;
  9871.  
  9872.  
  9873.   /* Print the definition of the macro HP.  */
  9874.  
  9875.   fprintf (of, "#define %s", hp->name);
  9876.  
  9877.   if (defn->nargs >= 0) {
  9878.     int i;
  9879.  
  9880.     fprintf (of, "(");
  9881.     for (i = 0; i < defn->nargs; i++) {
  9882.       dump_arg_n (defn, i, of);
  9883.       if (i + 1 < defn->nargs)
  9884.     fprintf (of, ", ");
  9885.     }
  9886.     fprintf (of, ")");
  9887.   }
  9888.  
  9889.   fprintf (of, " ");
  9890.  
  9891.   offset = 0;
  9892.   concat = 0;
  9893.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  9894.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  9895.     offset += ap->nchars;
  9896.     if (!traditional) {
  9897.       if (ap->nchars != 0)
  9898.     concat = 0;
  9899.       if (ap->stringify) {
  9900.     switch (ap->stringify) {
  9901.      case SHARP_TOKEN: fprintf (of, "#"); break;
  9902.      case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
  9903.      case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
  9904.      case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
  9905.      default: abort ();
  9906.     }
  9907.       }
  9908.       if (ap->raw_before != 0) {
  9909.     if (concat) {
  9910.       switch (ap->raw_before) {
  9911.        case WHITE_SHARP_TOKEN:
  9912.        case WHITE_PERCENT_COLON_TOKEN:
  9913.         fprintf (of, " ");
  9914.         break;
  9915.        default:
  9916.         break;
  9917.       }
  9918.     } else {
  9919.       switch (ap->raw_before) {
  9920.        case SHARP_TOKEN: fprintf (of, "##"); break;
  9921.        case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
  9922.        case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
  9923.        case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
  9924.        default: abort ();
  9925.       }
  9926.     }
  9927.       }
  9928.       concat = 0;
  9929.     }
  9930.     dump_arg_n (defn, ap->argno, of);
  9931.     if (!traditional && ap->raw_after != 0) {
  9932.       switch (ap->raw_after) {
  9933.        case SHARP_TOKEN: fprintf (of, "##"); break;
  9934.        case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
  9935.        case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
  9936.        case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
  9937.        default: abort ();
  9938.       }
  9939.       concat = 1;
  9940.     }
  9941.   }
  9942.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  9943.   fprintf (of, "\n");
  9944. }
  9945.  
  9946. /* Dump all macro definitions as #defines to stdout.  */
  9947.  
  9948. static void
  9949. dump_all_macros ()
  9950. {
  9951.   int bucket;
  9952.  
  9953.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  9954.     register HASHNODE *hp;
  9955.  
  9956.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  9957.       if (hp->type == T_MACRO)
  9958.     dump_single_macro (hp, stdout);
  9959.     }
  9960.   }
  9961. }
  9962.  
  9963. /* Output to OF a substring of a macro definition.
  9964.    BASE is the beginning of the definition.
  9965.    Output characters START thru LENGTH.
  9966.    Unless traditional, discard newlines outside of strings, thus
  9967.    converting funny-space markers to ordinary spaces.  */
  9968.  
  9969. static void
  9970. dump_defn_1 (base, start, length, of)
  9971.      U_CHAR *base;
  9972.      int start;
  9973.      int length;
  9974.      FILE *of;
  9975. {
  9976.   U_CHAR *p = base + start;
  9977.   U_CHAR *limit = base + start + length;
  9978.  
  9979.   if (traditional)
  9980.     fwrite (p, sizeof (*p), length, of);
  9981.   else {
  9982.     while (p < limit) {
  9983.       if (*p == '\"' || *p =='\'') {
  9984.     U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  9985.                      NULL_PTR, NULL_PTR);
  9986.     fwrite (p, sizeof (*p), p1 - p, of);
  9987.     p = p1;
  9988.       } else {
  9989.     if (*p != '\n')
  9990.       putc (*p, of);
  9991.     p++;
  9992.       }
  9993.     }
  9994.   }
  9995. }
  9996.  
  9997. /* Print the name of argument number ARGNUM of macro definition DEFN
  9998.    to OF.
  9999.    Recall that DEFN->args.argnames contains all the arg names
  10000.    concatenated in reverse order with comma-space in between.  */
  10001.  
  10002. static void
  10003. dump_arg_n (defn, argnum, of)
  10004.      DEFINITION *defn;
  10005.      int argnum;
  10006.      FILE *of;
  10007. {
  10008.   register U_CHAR *p = defn->args.argnames;
  10009.   while (argnum + 1 < defn->nargs) {
  10010.     p = (U_CHAR *) index ((char *) p, ' ') + 1;
  10011.     argnum++;
  10012.   }
  10013.  
  10014.   while (*p && *p != ',') {
  10015.     putc (*p, of);
  10016.     p++;
  10017.   }
  10018. }
  10019.  
  10020. /* Initialize syntactic classifications of characters.  */
  10021.  
  10022. static void
  10023. initialize_char_syntax ()
  10024. {
  10025.   register int i;
  10026.  
  10027.   /*
  10028.    * Set up is_idchar and is_idstart tables.  These should be
  10029.    * faster than saying (is_alpha (c) || c == '_'), etc.
  10030.    * Set up these things before calling any routines tthat
  10031.    * refer to them.
  10032.    */
  10033.   for (i = 'a'; i <= 'z'; i++) {
  10034.     is_idchar[i - 'a' + 'A'] = 1;
  10035.     is_idchar[i] = 1;
  10036.     is_idstart[i - 'a' + 'A'] = 1;
  10037.     is_idstart[i] = 1;
  10038.   }
  10039.   for (i = '0'; i <= '9'; i++)
  10040.     is_idchar[i] = 1;
  10041.   is_idchar['_'] = 1;
  10042.   is_idstart['_'] = 1;
  10043.   is_idchar['$'] = dollars_in_ident;
  10044.   is_idstart['$'] = dollars_in_ident;
  10045.  
  10046.   /* horizontal space table */
  10047.   is_hor_space[' '] = 1;
  10048.   is_hor_space['\t'] = 1;
  10049.   is_hor_space['\v'] = 1;
  10050.   is_hor_space['\f'] = 1;
  10051.   is_hor_space['\r'] = 1;
  10052.  
  10053.   is_space[' '] = 1;
  10054.   is_space['\t'] = 1;
  10055.   is_space['\v'] = 1;
  10056.   is_space['\f'] = 1;
  10057.   is_space['\n'] = 1;
  10058.   is_space['\r'] = 1;
  10059.  
  10060.   char_name['\v'] = "vertical tab";
  10061.   char_name['\f'] = "formfeed";
  10062.   char_name['\r'] = "carriage return";
  10063. }
  10064.  
  10065. /* Initialize the built-in macros.  */
  10066.  
  10067. static void
  10068. initialize_builtins (inp, outp)
  10069.      FILE_BUF *inp;
  10070.      FILE_BUF *outp;
  10071. {
  10072.   install ((U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
  10073.   install ((U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
  10074.   install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
  10075.   install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
  10076.   install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
  10077.   install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
  10078. #ifndef NO_BUILTIN_SIZE_TYPE
  10079.   install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
  10080. #endif
  10081. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  10082.   install ((U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
  10083. #endif
  10084.   install ((U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
  10085.   install ((U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
  10086.        NULL_PTR, -1);
  10087.   install ((U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
  10088.        NULL_PTR, -1);
  10089.   install ((U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
  10090.        NULL_PTR, -1);
  10091.   install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
  10092.   if (!traditional) {
  10093.     install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
  10094.     install ((U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
  10095.   }
  10096.   if (objc)
  10097.     install ((U_CHAR *) "__OBJC__", -1, T_CONST, "1", -1);
  10098. /*  This is supplied using a -D by the compiler driver
  10099.     so that it is present only when truly compiling with GNU C.  */
  10100. /*  install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1);  */
  10101.  
  10102.   if (debug_output)
  10103.     {
  10104.       char directive[2048];
  10105.       U_CHAR *udirective = (U_CHAR *) directive;
  10106.       register struct directive *dp = &directive_table[0];
  10107.       struct tm *timebuf = timestamp ();
  10108.  
  10109.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  10110.            instack[0].nominal_fname);
  10111.       output_line_directive (inp, outp, 0, same_file);
  10112.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  10113.                outp, dp);
  10114.  
  10115.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  10116.       output_line_directive (inp, outp, 0, same_file);
  10117.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  10118.                outp, dp);
  10119.  
  10120. #ifndef NO_BUILTIN_SIZE_TYPE
  10121.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  10122.       output_line_directive (inp, outp, 0, same_file);
  10123.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  10124.                outp, dp);
  10125. #endif
  10126.  
  10127. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  10128.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  10129.       output_line_directive (inp, outp, 0, same_file);
  10130.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  10131.                outp, dp);
  10132. #endif
  10133.  
  10134.       sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
  10135.       output_line_directive (inp, outp, 0, same_file);
  10136.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  10137.                outp, dp);
  10138.  
  10139.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  10140.            monthnames[timebuf->tm_mon],
  10141.            timebuf->tm_mday, timebuf->tm_year + 1900);
  10142.       output_line_directive (inp, outp, 0, same_file);
  10143.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  10144.                outp, dp);
  10145.  
  10146.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  10147.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  10148.       output_line_directive (inp, outp, 0, same_file);
  10149.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  10150.                outp, dp);
  10151.  
  10152.       if (!traditional)
  10153.     {
  10154.           sprintf (directive, " __STDC__ 1");
  10155.           output_line_directive (inp, outp, 0, same_file);
  10156.           pass_thru_directive (udirective, &udirective[strlen (directive)],
  10157.                    outp, dp);
  10158.     }
  10159.       if (objc)
  10160.     {
  10161.           sprintf (directive, " __OBJC__ 1");
  10162.           output_line_directive (inp, outp, 0, same_file);
  10163.           pass_thru_directive (udirective, &udirective[strlen (directive)],
  10164.                    outp, dp);
  10165.     }
  10166.     }
  10167. }
  10168.  
  10169. /*
  10170.  * process a given definition string, for initialization
  10171.  * If STR is just an identifier, define it with value 1.
  10172.  * If STR has anything after the identifier, then it should
  10173.  * be identifier=definition.
  10174.  */
  10175.  
  10176. static void
  10177. make_definition (str, op)
  10178.      char *str;
  10179.      FILE_BUF *op;
  10180. {
  10181.   FILE_BUF *ip;
  10182.   struct directive *kt;
  10183.   U_CHAR *buf, *p;
  10184.  
  10185.   p = buf = (U_CHAR *) str;
  10186.   if (!is_idstart[*p]) {
  10187.     error ("malformed option `-D %s'", str);
  10188.     return;
  10189.   }
  10190.   while (is_idchar[*++p])
  10191.     ;
  10192.   if (*p == '(') {
  10193.     while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
  10194.       ;
  10195.     if (*p++ != ')')
  10196.       p = (U_CHAR *) str;            /* Error */
  10197.   }
  10198.   if (*p == 0) {
  10199.     buf = (U_CHAR *) alloca (p - buf + 4);
  10200.     strcpy ((char *)buf, str);
  10201.     strcat ((char *)buf, " 1");
  10202.   } else if (*p != '=') {
  10203.     error ("malformed option `-D %s'", str);
  10204.     return;
  10205.   } else {
  10206.     U_CHAR *q;
  10207.     /* Copy the entire option so we can modify it.  */
  10208.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  10209.     strncpy ((char *) buf, str, p - (U_CHAR *) str);
  10210.     /* Change the = to a space.  */
  10211.     buf[p - (U_CHAR *) str] = ' ';
  10212.     /* Scan for any backslash-newline and remove it.  */
  10213.     p++;
  10214.     q = &buf[p - (U_CHAR *) str];
  10215.     while (*p) {
  10216.       if (*p == '\"' || *p == '\'') {
  10217.     int unterminated = 0;
  10218.     U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
  10219.                      NULL_PTR, NULL_PTR, &unterminated);
  10220.     if (unterminated)
  10221.       return;
  10222.     while (p != p1)
  10223.       if (*p == '\\' && p[1] == '\n')
  10224.         p += 2;
  10225.       else
  10226.         *q++ = *p++;
  10227.       } else if (*p == '\\' && p[1] == '\n')
  10228.     p += 2;
  10229.       /* Change newline chars into newline-markers.  */
  10230.       else if (*p == '\n')
  10231.     {
  10232.       *q++ = '\n';
  10233.       *q++ = '\n';
  10234.       p++;
  10235.     }
  10236.       else
  10237.     *q++ = *p++;
  10238.     }
  10239.     *q = 0;
  10240.   }
  10241.   
  10242.   ip = &instack[++indepth];
  10243.   ip->nominal_fname = ip->fname = "*Initialization*";
  10244.  
  10245.   ip->buf = ip->bufp = buf;
  10246.   ip->length = strlen ((char *) buf);
  10247.   ip->lineno = 1;
  10248.   ip->macro = 0;
  10249.   ip->free_ptr = 0;
  10250.   ip->if_stack = if_stack;
  10251.   ip->system_header_p = 0;
  10252.  
  10253.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  10254.     ;
  10255.  
  10256.   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
  10257.   do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
  10258.   --indepth;
  10259. }
  10260.  
  10261. /* JF, this does the work for the -U option */
  10262.  
  10263. static void
  10264. make_undef (str, op)
  10265.      char *str;
  10266.      FILE_BUF *op;
  10267. {
  10268.   FILE_BUF *ip;
  10269.   struct directive *kt;
  10270.  
  10271.   ip = &instack[++indepth];
  10272.   ip->nominal_fname = ip->fname = "*undef*";
  10273.  
  10274.   ip->buf = ip->bufp = (U_CHAR *) str;
  10275.   ip->length = strlen (str);
  10276.   ip->lineno = 1;
  10277.   ip->macro = 0;
  10278.   ip->free_ptr = 0;
  10279.   ip->if_stack = if_stack;
  10280.   ip->system_header_p = 0;
  10281.  
  10282.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  10283.     ;
  10284.  
  10285.   do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
  10286.   --indepth;
  10287. }
  10288.  
  10289. /* Process the string STR as if it appeared as the body of a #assert.
  10290.    OPTION is the option name for which STR was the argument.  */
  10291.  
  10292. static void
  10293. make_assertion (option, str)
  10294.      char *option;
  10295.      char *str;
  10296. {
  10297.   FILE_BUF *ip;
  10298.   struct directive *kt;
  10299.   U_CHAR *buf, *p, *q;
  10300.  
  10301.   /* Copy the entire option so we can modify it.  */
  10302.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  10303.   strcpy ((char *) buf, str);
  10304.   /* Scan for any backslash-newline and remove it.  */
  10305.   p = q = buf;
  10306.   while (*p) {
  10307.     if (*p == '\\' && p[1] == '\n')
  10308.       p += 2;
  10309.     else
  10310.       *q++ = *p++;
  10311.   }
  10312.   *q = 0;
  10313.  
  10314.   p = buf;
  10315.   if (!is_idstart[*p]) {
  10316.     error ("malformed option `%s %s'", option, str);
  10317.     return;
  10318.   }
  10319.   while (is_idchar[*++p])
  10320.     ;
  10321.   SKIP_WHITE_SPACE (p);
  10322.   if (! (*p == 0 || *p == '(')) {
  10323.     error ("malformed option `%s %s'", option, str);
  10324.     return;
  10325.   }
  10326.   
  10327.   ip = &instack[++indepth];
  10328.   ip->nominal_fname = ip->fname = "*Initialization*";
  10329.  
  10330.   ip->buf = ip->bufp = buf;
  10331.   ip->length = strlen ((char *) buf);
  10332.   ip->lineno = 1;
  10333.   ip->macro = 0;
  10334.   ip->free_ptr = 0;
  10335.   ip->if_stack = if_stack;
  10336.   ip->system_header_p = 0;
  10337.  
  10338.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  10339.     ;
  10340.  
  10341.   /* pass NULL as output ptr to do_define since we KNOW it never
  10342.      does any output.... */
  10343.   do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
  10344.   --indepth;
  10345. }
  10346.  
  10347. /* Append a chain of `struct file_name_list's
  10348.    to the end of the main include chain.
  10349.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  10350.  
  10351. static void
  10352. append_include_chain (first, last)
  10353.      struct file_name_list *first, *last;
  10354. {
  10355.   struct file_name_list *dir;
  10356.  
  10357.   if (!first || !last)
  10358.     return;
  10359.  
  10360.   if (include == 0)
  10361.     include = first;
  10362.   else
  10363.     last_include->next = first;
  10364.  
  10365.   if (first_bracket_include == 0)
  10366.     first_bracket_include = first;
  10367.  
  10368.   for (dir = first; ; dir = dir->next) {
  10369.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  10370.     if (len > max_include_len)
  10371.       max_include_len = len;
  10372.     if (dir == last)
  10373.       break;
  10374.   }
  10375.  
  10376.   last->next = NULL;
  10377.   last_include = last;
  10378. }
  10379.  
  10380. #ifdef NEXT_FRAMEWORKS
  10381. /* Append a chain of `struct file_name_list's
  10382.    to the end of the main Frameworks chain.
  10383.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  10384.  
  10385. static void
  10386. append_framework_chain (first, last)
  10387.      struct file_name_list *first, *last;
  10388. {
  10389.   struct file_name_list *dir;
  10390.  
  10391.   if (!first || !last)
  10392.     return;
  10393.  
  10394.   if (framework == 0)
  10395.     framework = first;
  10396.   else
  10397.     last_framework->next = first;
  10398.  
  10399.   if (first_bracket_framework == 0)
  10400.     first_bracket_framework = first;
  10401.  
  10402.   for (dir = first; ; dir = dir->next) {
  10403.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  10404.     if (len > max_include_len)
  10405.       max_include_len = len;
  10406.     if (dir == last)
  10407.       break;
  10408.   }
  10409.  
  10410.   last->next = NULL;
  10411.   last_framework = last;
  10412. }
  10413.  
  10414. #endif
  10415.  
  10416. /* Add output to `deps_buffer' for the -M switch.
  10417.    STRING points to the text to be output.
  10418.    SPACER is ':' for targets, ' ' for dependencies.  */
  10419.  
  10420. static void
  10421. deps_output (string, spacer)
  10422.      char *string;
  10423.      int spacer;
  10424. {
  10425.   int size = strlen (string);
  10426.  
  10427.   if (size == 0)
  10428.     return;
  10429.  
  10430. #ifndef MAX_OUTPUT_COLUMNS
  10431. #define MAX_OUTPUT_COLUMNS 72
  10432. #endif
  10433.   if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
  10434.       && 1 < deps_column) {
  10435.     bcopy (" \\\n ", &deps_buffer[deps_size], 4);
  10436.     deps_size += 4;
  10437.     deps_column = 1;
  10438.     if (spacer == ' ')
  10439.       spacer = 0;
  10440.   }
  10441.  
  10442.   if (deps_size + size + 8 > deps_allocated_size) {
  10443.     deps_allocated_size = (deps_size + size + 50) * 2;
  10444.     deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
  10445.   }
  10446.   if (spacer == ' ') {
  10447.     deps_buffer[deps_size++] = ' ';
  10448.     deps_column++;
  10449.   }
  10450.   bcopy (string, &deps_buffer[deps_size], size);
  10451.   deps_size += size;
  10452.   deps_column += size;
  10453.   if (spacer == ':') {
  10454.     deps_buffer[deps_size++] = ':';
  10455.     deps_column++;
  10456.   }
  10457.   deps_buffer[deps_size] = 0;
  10458. }
  10459.  
  10460. static void
  10461. fatal (PRINTF_ALIST (msg))
  10462.      PRINTF_DCL (msg)
  10463. {
  10464.   va_list args;
  10465.  
  10466.   fprintf (stderr, "%s: ", progname);
  10467.   VA_START (args, msg);
  10468. #ifdef VA_REPORT_EVENT
  10469.   VA_REPORT_EVENT (0, progname, NULL, 0, msg, args);
  10470. #endif
  10471.   vfprintf (stderr, msg, args);
  10472.   va_end (args);
  10473.   fprintf (stderr, "\n");
  10474.   exit (FATAL_EXIT_CODE);
  10475. }
  10476.  
  10477. /* More 'friendly' abort that prints the line and file.
  10478.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  10479.  
  10480. void
  10481. fancy_abort ()
  10482. {
  10483.   fatal ("Internal gcc abort.");
  10484. }
  10485.  
  10486. static void
  10487. perror_with_name (name)
  10488.      char *name;
  10489. {
  10490. #ifdef REPORT_EVENT
  10491.   REPORT_EVENT (0, progname, NULL, 0,
  10492.         "%s: %s", name,
  10493.         (errno < sys_nerr)
  10494.         ? sys_errlist[errno]
  10495.         : "undocumented I/O error", 0);
  10496. #endif
  10497.   fprintf (stderr, "%s: ", progname);
  10498.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  10499.   errors++;
  10500. }
  10501.  
  10502. static void
  10503. pfatal_with_name (name)
  10504.      char *name;
  10505. {
  10506.   perror_with_name (name);
  10507. #ifdef VMS
  10508.   exit (vaxc$errno);
  10509. #else
  10510.   exit (FATAL_EXIT_CODE);
  10511. #endif
  10512. }
  10513.  
  10514. /* Handler for SIGPIPE.  */
  10515.  
  10516. static void
  10517. pipe_closed (signo)
  10518.      /* If this is missing, some compilers complain.  */
  10519.      int signo;
  10520. {
  10521.   fatal ("output pipe has been closed");
  10522. }
  10523.  
  10524. static void
  10525. memory_full ()
  10526. {
  10527.   fatal ("Memory exhausted.");
  10528. }
  10529.  
  10530.  
  10531. GENERIC_PTR
  10532. xmalloc (size)
  10533.      size_t size;
  10534. {
  10535.   register GENERIC_PTR ptr = (GENERIC_PTR) malloc (size);
  10536.   if (!ptr)
  10537.     memory_full ();
  10538.   return ptr;
  10539. }
  10540.  
  10541. static GENERIC_PTR
  10542. xrealloc (old, size)
  10543.      GENERIC_PTR old;
  10544.      size_t size;
  10545. {
  10546.   register GENERIC_PTR ptr = (GENERIC_PTR) realloc (old, size);
  10547.   if (!ptr)
  10548.     memory_full ();
  10549.   return ptr;
  10550. }
  10551.  
  10552. static GENERIC_PTR
  10553. xcalloc (number, size)
  10554.      size_t number, size;
  10555. {
  10556.   register size_t total = number * size;
  10557.   register GENERIC_PTR ptr = (GENERIC_PTR) malloc (total);
  10558.   if (!ptr)
  10559.     memory_full ();
  10560.   bzero (ptr, total);
  10561.   return ptr;
  10562. }
  10563.  
  10564. static char *
  10565. savestring (input)
  10566.      char *input;
  10567. {
  10568.   size_t size = strlen (input);
  10569.   char *output = xmalloc (size + 1);
  10570. #ifdef NEXT_FRAMEWORKS
  10571.   strncpy (output, input, size + 1);
  10572. #else  
  10573.   strcpy (output, input);
  10574. #endif
  10575.   return output;
  10576. }
  10577.  
  10578. /* Get the file-mode and data size of the file open on FD
  10579.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  10580.  
  10581. static int
  10582. file_size_and_mode (fd, mode_pointer, size_pointer)
  10583.      int fd;
  10584.      int *mode_pointer;
  10585.      long int *size_pointer;
  10586. {
  10587.   struct stat sbuf;
  10588.  
  10589.   if (fstat (fd, &sbuf) < 0) return (-1);
  10590.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  10591.   if (size_pointer) *size_pointer = sbuf.st_size;
  10592.   return 0;
  10593. }
  10594.  
  10595. #ifdef VMS
  10596.  
  10597. /* Under VMS we need to fix up the "include" specification
  10598.    filename so that everything following the 1st slash is
  10599.    changed into its correct VMS file specification. */
  10600.  
  10601. static void
  10602. hack_vms_include_specification (fname)
  10603.      char *fname;
  10604. {
  10605.   register char *cp, *cp1, *cp2;
  10606.   int f, check_filename_before_returning, no_prefix_seen;
  10607.   char Local[512];
  10608.  
  10609.   check_filename_before_returning = 0;
  10610.   no_prefix_seen = 0;
  10611.  
  10612.   /* Ignore leading "./"s */
  10613.   while (fname[0] == '.' && fname[1] == '/') {
  10614.     strcpy (fname, fname+2);
  10615.     no_prefix_seen = 1;        /* mark this for later */
  10616.   }
  10617.   /* Look for the boundary between the VMS and UNIX filespecs */
  10618.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  10619.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  10620.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  10621.   if (cp) {
  10622.     cp++;
  10623.   } else {
  10624.     cp = index (fname, '/');    /* Look for the "/" */
  10625.   }
  10626.  
  10627.   /*
  10628.    * Check if we have a vax-c style '#include filename'
  10629.    * and add the missing .h
  10630.    */
  10631.   if (cp == 0) {
  10632.     if (index(fname,'.') == 0)
  10633.       strcat(fname, ".h");
  10634.   } else {
  10635.     if (index(cp,'.') == 0)
  10636.       strcat(cp, ".h");
  10637.   }
  10638.  
  10639.   cp2 = Local;            /* initialize */
  10640.  
  10641.   /* We are trying to do a number of things here.  First of all, we are
  10642.      trying to hammer the filenames into a standard format, such that later
  10643.      processing can handle them.
  10644.      
  10645.      If the file name contains something like [dir.], then it recognizes this
  10646.      as a root, and strips the ".]".  Later processing will add whatever is
  10647.      needed to get things working properly.
  10648.      
  10649.      If no device is specified, then the first directory name is taken to be
  10650.      a device name (or a rooted logical). */
  10651.  
  10652.   /* See if we found that 1st slash */
  10653.   if (cp == 0) return;        /* Nothing to do!!! */
  10654.   if (*cp != '/') return;    /* Nothing to do!!! */
  10655.   /* Point to the UNIX filename part (which needs to be fixed!) */
  10656.   cp1 = cp+1;
  10657.   /* If the directory spec is not rooted, we can just copy
  10658.      the UNIX filename part and we are done */
  10659.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  10660.     if (cp[-2] != '.') {
  10661.       /*
  10662.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  10663.        * We strip the `]', and then splice the two parts of the name in the
  10664.        * usual way.  Given the default locations for include files in cccp.c,
  10665.        * we will only use this code if the user specifies alternate locations
  10666.        * with the /include (-I) switch on the command line.  */
  10667.       cp -= 1;            /* Strip "]" */
  10668.       cp1--;            /* backspace */
  10669.     } else {
  10670.       /*
  10671.        * The VMS part has a ".]" at the end, and this will not do.  Later
  10672.        * processing will add a second directory spec, and this would be a syntax
  10673.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  10674.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  10675.        * generation of the 000000 root directory spec (which does not belong here
  10676.        * in this case).
  10677.        */
  10678.       cp -= 2;            /* Strip ".]" */
  10679.       cp1--; };            /* backspace */
  10680.   } else {
  10681.  
  10682.     /* We drop in here if there is no VMS style directory specification yet.
  10683.      * If there is no device specification either, we make the first dir a
  10684.      * device and try that.  If we do not do this, then we will be essentially
  10685.      * searching the users default directory (as if they did a #include "asdf.h").
  10686.      *
  10687.      * Then all we need to do is to push a '[' into the output string. Later
  10688.      * processing will fill this in, and close the bracket.
  10689.      */
  10690.     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  10691.     *cp2++ = '[';        /* Open the directory specification */
  10692.   }
  10693.  
  10694.   /* at this point we assume that we have the device spec, and (at least
  10695.      the opening "[" for a directory specification.  We may have directories
  10696.      specified already */
  10697.  
  10698.   /* If there are no other slashes then the filename will be
  10699.      in the "root" directory.  Otherwise, we need to add
  10700.      directory specifications. */
  10701.   if (index (cp1, '/') == 0) {
  10702.     /* Just add "000000]" as the directory string */
  10703.     strcpy (cp2, "000000]");
  10704.     cp2 += strlen (cp2);
  10705.     check_filename_before_returning = 1; /* we might need to fool with this later */
  10706.   } else {
  10707.     /* As long as there are still subdirectories to add, do them. */
  10708.     while (index (cp1, '/') != 0) {
  10709.       /* If this token is "." we can ignore it */
  10710.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  10711.     cp1 += 2;
  10712.     continue;
  10713.       }
  10714.       /* Add a subdirectory spec. Do not duplicate "." */
  10715.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  10716.     *cp2++ = '.';
  10717.       /* If this is ".." then the spec becomes "-" */
  10718.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  10719.     /* Add "-" and skip the ".." */
  10720.     *cp2++ = '-';
  10721.     cp1 += 3;
  10722.     continue;
  10723.       }
  10724.       /* Copy the subdirectory */
  10725.       while (*cp1 != '/') *cp2++= *cp1++;
  10726.       cp1++;            /* Skip the "/" */
  10727.     }
  10728.     /* Close the directory specification */
  10729.     if (cp2[-1] == '.')        /* no trailing periods */
  10730.       cp2--;
  10731.     *cp2++ = ']';
  10732.   }
  10733.   /* Now add the filename */
  10734.   while (*cp1) *cp2++ = *cp1++;
  10735.   *cp2 = 0;
  10736.   /* Now append it to the original VMS spec. */
  10737.   strcpy (cp, Local);
  10738.  
  10739.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  10740.      remove the [000000], and return that name.  This provides flexibility
  10741.      to the user in that they can use both rooted and non-rooted logical names
  10742.      to point to the location of the file.  */
  10743.  
  10744.   if (check_filename_before_returning && no_prefix_seen) {
  10745.     f = open (fname, O_RDONLY, 0666);
  10746.     if (f >= 0) {
  10747.       /* The file name is OK as it is, so return it as is.  */
  10748.       close (f);
  10749.       return;
  10750.     }
  10751.     /* The filename did not work.  Try to remove the [000000] from the name,
  10752.        and return it.  */
  10753.     cp = index (fname, '[');
  10754.     cp2 = index (fname, ']') + 1;
  10755.     strcpy (cp, cp2);        /* this gets rid of it */
  10756.   }
  10757.   return;
  10758. }
  10759. #endif    /* VMS */
  10760.  
  10761. #ifdef    VMS
  10762.  
  10763. /* These are the read/write replacement routines for
  10764.    VAX-11 "C".  They make read/write behave enough
  10765.    like their UNIX counterparts that CCCP will work */
  10766.  
  10767. static int
  10768. read (fd, buf, size)
  10769.      int fd;
  10770.      char *buf;
  10771.      int size;
  10772. {
  10773. #undef    read    /* Get back the REAL read routine */
  10774.   register int i;
  10775.   register int total = 0;
  10776.  
  10777.   /* Read until the buffer is exhausted */
  10778.   while (size > 0) {
  10779.     /* Limit each read to 32KB */
  10780.     i = (size > (32*1024)) ? (32*1024) : size;
  10781.     i = read (fd, buf, i);
  10782.     if (i <= 0) {
  10783.       if (i == 0) return (total);
  10784.       return (i);
  10785.     }
  10786.     /* Account for this read */
  10787.     total += i;
  10788.     buf += i;
  10789.     size -= i;
  10790.   }
  10791.   return (total);
  10792. }
  10793.  
  10794. static int
  10795. write (fd, buf, size)
  10796.      int fd;
  10797.      char *buf;
  10798.      int size;
  10799. {
  10800. #undef    write    /* Get back the REAL write routine */
  10801.   int i;
  10802.   int j;
  10803.  
  10804.   /* Limit individual writes to 32Kb */
  10805.   i = size;
  10806.   while (i > 0) {
  10807.     j = (i > (32*1024)) ? (32*1024) : i;
  10808.     if (write (fd, buf, j) < 0) return (-1);
  10809.     /* Account for the data written */
  10810.     buf += j;
  10811.     i -= j;
  10812.   }
  10813.   return (size);
  10814. }
  10815.  
  10816. /* The following wrapper functions supply additional arguments to the VMS
  10817.    I/O routines to optimize performance with file handling.  The arguments
  10818.    are:
  10819.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  10820.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  10821.      "fop=tef"- Truncate unused portions of file when closing file.
  10822.      "shr=nil"- Disallow file sharing while file is open.
  10823.  */
  10824.  
  10825. static FILE *
  10826. freopen (fname, type, oldfile)
  10827.      char *fname;
  10828.      char *type;
  10829.      FILE *oldfile;
  10830. {
  10831. #undef    freopen    /* Get back the REAL fopen routine */
  10832.   if (strcmp (type, "w") == 0)
  10833.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  10834.   return freopen (fname, type, oldfile, "mbc=16");
  10835. }
  10836.  
  10837. static FILE *
  10838. fopen (fname, type)
  10839.      char *fname;
  10840.      char *type;
  10841. {
  10842. #undef fopen    /* Get back the REAL fopen routine */
  10843.   /* The gcc-vms-1.42 distribution's header files prototype fopen with two
  10844.      fixed arguments, which matches ANSI's specification but not VAXCRTL's
  10845.      pre-ANSI implementation.  This hack circumvents the mismatch problem.  */
  10846.   FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
  10847.  
  10848.   if (*type == 'w')
  10849.     return (*vmslib_fopen) (fname, type, "mbc=32",
  10850.                 "deq=64", "fop=tef", "shr=nil");
  10851.   else
  10852.     return (*vmslib_fopen) (fname, type, "mbc=32");
  10853. }
  10854.  
  10855. static int 
  10856. open (fname, flags, prot)
  10857.      char *fname;
  10858.      int flags;
  10859.      int prot;
  10860. {
  10861. #undef open    /* Get back the REAL open routine */
  10862.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  10863. }
  10864.  
  10865. /* Avoid run-time library bug, where copying M out of N+M characters with
  10866.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  10867.    gcc-cpp exercises this particular bug.  [Fixed in V5.5-2's VAXCRTL.]  */
  10868.  
  10869. static char *
  10870. strncat (dst, src, cnt)
  10871.      char *dst;
  10872.      const char *src;
  10873.      unsigned cnt;
  10874. {
  10875.   register char *d = dst, *s = (char *) src;
  10876.   register int n = cnt;    /* convert to _signed_ type */
  10877.  
  10878.   while (*d) d++;    /* advance to end */
  10879.   while (--n >= 0)
  10880.     if (!(*d++ = *s++)) break;
  10881.   if (n < 0) *d = '\0';
  10882.   return dst;
  10883. }
  10884.  
  10885. /* more VMS hackery */
  10886. #include <fab.h>
  10887. #include <nam.h>
  10888.  
  10889. extern unsigned long sys$parse(), sys$search();
  10890.  
  10891. /* Work around another library bug.  If a file is located via a searchlist,
  10892.    and if the device it's on is not the same device as the one specified
  10893.    in the first element of that searchlist, then both stat() and fstat()
  10894.    will fail to return info about it.  `errno' will be set to EVMSERR, and
  10895.    `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
  10896.    We can get around this by fully parsing the filename and then passing
  10897.    that absolute name to stat().
  10898.  
  10899.    Without this fix, we can end up failing to find header files, which is
  10900.    bad enough, but then compounding the problem by reporting the reason for
  10901.    failure as "normal successful completion."  */
  10902.  
  10903. #undef fstat    /* get back to library version */
  10904.  
  10905. static int
  10906. VMS_fstat (fd, statbuf)
  10907.      int fd;
  10908.      struct stat *statbuf;
  10909. {
  10910.   int result = fstat (fd, statbuf);
  10911.  
  10912.   if (result < 0)
  10913.     {
  10914.       FILE *fp;
  10915.       char nambuf[NAM$C_MAXRSS+1];
  10916.  
  10917.       if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
  10918.     result = VMS_stat (nambuf, statbuf);
  10919.       /* No fclose(fp) here; that would close(fd) as well.  */
  10920.     }
  10921.  
  10922.   return result;
  10923. }
  10924.  
  10925. static int
  10926. VMS_stat (name, statbuf)
  10927.      const char *name;
  10928.      struct stat *statbuf;
  10929. {
  10930.   int result = stat (name, statbuf);
  10931.  
  10932.   if (result < 0)
  10933.     {
  10934.       struct FAB fab;
  10935.       struct NAM nam;
  10936.       char exp_nam[NAM$C_MAXRSS+1],  /* expanded name buffer for sys$parse */
  10937.        res_nam[NAM$C_MAXRSS+1];  /* resultant name buffer for sys$search */
  10938.  
  10939.       fab = cc$rms_fab;
  10940.       fab.fab$l_fna = (char *) name;
  10941.       fab.fab$b_fns = (unsigned char) strlen (name);
  10942.       fab.fab$l_nam = (void *) &nam;
  10943.       nam = cc$rms_nam;
  10944.       nam.nam$l_esa = exp_nam,  nam.nam$b_ess = sizeof exp_nam - 1;
  10945.       nam.nam$l_rsa = res_nam,  nam.nam$b_rss = sizeof res_nam - 1;
  10946.       nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
  10947.       if (sys$parse (&fab) & 1)
  10948.     {
  10949.       if (sys$search (&fab) & 1)
  10950.         {
  10951.           res_nam[nam.nam$b_rsl] = '\0';
  10952.           result = stat (res_nam, statbuf);
  10953.         }
  10954.       /* Clean up searchlist context cached by the system.  */
  10955.       nam.nam$b_nop = NAM$M_SYNCHK;
  10956.       fab.fab$l_fna = 0,  fab.fab$b_fns = 0;
  10957.       (void) sys$parse (&fab);
  10958.     }
  10959.     }
  10960.  
  10961.   return result;
  10962. }
  10963. #endif /* VMS */
  10964.  
  10965. /* Pre-C-Preprocessor to translate RTF (Rich Text Format) Level 0
  10966.    to plain ASCII before normal preprocessing.  This removes all font, color,
  10967.    and help links from the text, but leaves the actual contents unchanged.
  10968.  
  10969.    Using an extra pass through the buffer takes a little extra time,
  10970.    but Rich Source Code is rare and a separate, shared routine
  10971.    can be used.   */
  10972.  
  10973. #define RTF_HEADER     "{\\rtf0\\ansi"
  10974. #define RTF_HEADER_LEN 11
  10975.  
  10976. static void
  10977. buf_convert_rtf (buf)
  10978.      FILE_BUF *buf;
  10979. {
  10980.   int asciiTextLen;
  10981.  
  10982.   if (buf->buf 
  10983.       && buf->length > RTF_HEADER_LEN 
  10984.       && !strncmp (buf->buf, RTF_HEADER, RTF_HEADER_LEN)) 
  10985.     {
  10986.       const char * asciiText = rtf_to_ascii(buf->buf, buf->length, &asciiTextLen);
  10987.       if (asciiText) 
  10988.     {
  10989.       /* FIXME: deallocate buf->buf */
  10990.       buf->buf = buf->bufp = (char *) asciiText;
  10991.       buf->length = asciiTextLen;
  10992.     }
  10993.     }
  10994. }
  10995.  
  10996.  
  10997. /* Process rtf text. RTFTEXTLEN bytes are processed from
  10998.    RTFTEXTPTR.  The output is ASCIITEXTLEN bytes in the same buffer. */
  10999.  
  11000. #define ASCII_TERMINATOR '\0' 
  11001.  
  11002. static const char *
  11003. rtf_to_ascii (const char *rtfTextPtr, int rtfTextLen, int * asciiTextLen) 
  11004. {
  11005.   char * asciiTextBuf = (char *) xmalloc(rtfTextLen);
  11006.   char * asciiTextPtr = asciiTextBuf;
  11007.   const char *endRtfPtr = rtfTextPtr + rtfTextLen;
  11008.  
  11009.   if (!asciiTextBuf)
  11010.     return NULL;
  11011.   
  11012.   while ( rtfTextPtr != endRtfPtr ) {
  11013.     switch( *rtfTextPtr ) {
  11014.     case '\n':
  11015.       rtfTextPtr++;
  11016.       break;
  11017.     case '}':
  11018.       if ( *++rtfTextPtr == '\n' ) {
  11019.     rtfTextPtr++;
  11020.     if (*rtfTextPtr == '‹') rtfTextPtr++;
  11021.       }         
  11022.       break;
  11023.     case '{':
  11024.       if ( !strncmp( rtfTextPtr, "{\\stylesheet", 12 ) 
  11025.       || !strncmp( rtfTextPtr, "{\\fonttbl", 9 ) 
  11026.       || !strncmp( rtfTextPtr, "{\\colortbl", 10 ) 
  11027.       || !strncmp( rtfTextPtr, "{\\NeXTHelp", 10 )
  11028.       || !strncmp( rtfTextPtr, "{{\\NeXTHelp", 11 ) ) {
  11029.     int depth = 1;
  11030.     while ( depth > 0 ) {
  11031.       switch ( *++rtfTextPtr ) {
  11032.       case '{': depth++; break;
  11033.       case '}': depth--; break;
  11034.       }
  11035.     }
  11036.       }
  11037.       else
  11038.     rtfTextPtr++;
  11039.       break;
  11040.     case '\\':
  11041.       rtfTextPtr++;
  11042.       if (*rtfTextPtr == '{'  
  11043.       || *rtfTextPtr == '}'  
  11044.       ||  *rtfTextPtr == '\\'  
  11045.       ||  *rtfTextPtr == '\n' ) {
  11046.     *asciiTextPtr++ = *rtfTextPtr++;
  11047.       } else if ( *rtfTextPtr == '\'' ) {
  11048.     int v1, v2, value;
  11049.     rtfTextPtr++;
  11050.     v1 = isdigit( *rtfTextPtr ) ? (*rtfTextPtr - '0') 
  11051.       : ((*rtfTextPtr - 'a') + 10);
  11052.     rtfTextPtr++;
  11053.     v2 = isdigit( *rtfTextPtr ) ? (*rtfTextPtr - '0') 
  11054.       : ((*rtfTextPtr - 'a') + 10);
  11055.     value = (v1 << 4) | (v2);
  11056.     *asciiTextPtr++ = value;
  11057.     rtfTextPtr++;
  11058.       } else {
  11059.     while ( isalpha( *rtfTextPtr ) ) rtfTextPtr++;
  11060.     if (*rtfTextPtr == '-') rtfTextPtr++;
  11061.     while ( isdigit( *rtfTextPtr ) ) rtfTextPtr++;
  11062.     if ( *rtfTextPtr == ' ' ) rtfTextPtr++;
  11063.     else while (*rtfTextPtr == ' ') rtfTextPtr++;
  11064.       }
  11065.       break;
  11066.     default:
  11067.       *asciiTextPtr++ = *rtfTextPtr++;
  11068.     }       
  11069.   }
  11070.   *asciiTextPtr = ASCII_TERMINATOR;
  11071.   *asciiTextLen = (int)asciiTextPtr - (int)asciiTextBuf;
  11072.   return asciiTextBuf;
  11073. }
  11074.  
  11075.  
  11076. #ifdef NEXT_SEMANTICS0
  11077.  
  11078. struct negative_file {
  11079.   char *name;
  11080.   struct negative_file *next;
  11081. };
  11082. static struct negative_file *negative_hash_table[IMPORT_HASH_SIZE];
  11083.  
  11084. static void
  11085. add_to_negative_cache (fname)
  11086.      char *fname;
  11087. {
  11088.   struct negative_file *i;
  11089.   int hashval;
  11090.  
  11091.   hashval = import_hash (fname);
  11092.   i = (struct negative_file *)xmalloc (sizeof (struct negative_file));
  11093.   i->name = fname;
  11094.   i->next = negative_hash_table[hashval];
  11095.   negative_hash_table[hashval] = i;
  11096. }
  11097.  
  11098. static int
  11099. found_in_negative_cache (filename)
  11100.      char *filename;
  11101. {
  11102.   struct negative_file *i;
  11103.   int hashval;
  11104.  
  11105.   hashval = import_hash (filename);
  11106.  
  11107.   /* Attempt to find file in list of already included files */
  11108.   i = negative_hash_table[hashval];
  11109.  
  11110.   while (i) {
  11111.     if (!strcmp (filename, i->name))
  11112.       return 1;        /* return found */
  11113.     i = i->next;
  11114.   }
  11115.   return 0;
  11116. }
  11117.  
  11118. #if 0
  11119. void get_input_file_names(in_fname, in_fname_count, in_fname_list)
  11120.   char *in_fname;
  11121.   int *in_fname_count; /* output */
  11122.   struct file_name_list **in_fname_list; /* optional output */
  11123. {
  11124.   char *in_fnamep = in_fname;
  11125.   char c;
  11126.  
  11127.   *in_fname_count = 1;
  11128.   while (c = *in_fnamep++)
  11129.     if (c == ' ') (*in_fname_count)++; /* need the parens */
  11130.  
  11131.   if (*in_fname_count > 1) {
  11132.     int nelem = 0;
  11133.     *in_fname_list = (struct file_name_list *) xmalloc (*in_fname_count * 
  11134.                          sizeof (struct file_name_list));
  11135.     in_fnamep = in_fname;
  11136.     if ((*in_fname_list)[nelem++].fname = strtok(in_fnamep," "))
  11137.       while ((*in_fname_list)[nelem++].fname = strtok(NULL," "));
  11138.   } else {
  11139.     *in_fname_list = 0;
  11140.   }
  11141. }
  11142. #endif
  11143.  
  11144. static void
  11145. add_version_of_header(fileptr, start, length)
  11146.      struct file_name_list *fileptr;
  11147.      unsigned int start;
  11148.      unsigned int length;
  11149. {
  11150.    struct file_name_list *ptr;
  11151.  
  11152.    ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  11153.    bzero (ptr, sizeof (struct file_name_list));
  11154.  
  11155.    ptr->fname = fileptr->fname;
  11156.    ptr->versions = fileptr->versions;
  11157.    fileptr->versions = ptr;
  11158.    ptr->relative_position_of_file_start = start;
  11159.    ptr->file_length = length;
  11160. }
  11161.  
  11162. static int
  11163. duplicate_version_of_header(op, fileptr, position_of_file_start, 
  11164.                                      file_length)
  11165.      FILE_BUF *op;
  11166.      struct file_name_list *fileptr;
  11167.      U_CHAR *position_of_file_start;
  11168.      unsigned int file_length;
  11169. {
  11170.   int found_a_version = 0;
  11171.   do {
  11172.     if ((file_length == fileptr->file_length) &&
  11173.         (!bcmp (position_of_file_start, 
  11174.                 op->buf+fileptr->relative_position_of_file_start,
  11175.                 file_length))) {
  11176.       found_a_version = 1;
  11177.     } 
  11178.     fileptr = fileptr->versions;
  11179.   } while (fileptr && !found_a_version);
  11180.   return found_a_version;
  11181. }
  11182.  
  11183. /* This saves processing 497k for "appkit.h" (with the "include" heuristic)
  11184.    This results in a significant savings in user time (~40% I believe!) */
  11185.  
  11186. static int
  11187. aggressive_skip_if_group (ip)
  11188.      FILE_BUF *ip;
  11189. {
  11190.     if (ip->bufp == ip->file->position_after_last_include) {
  11191.  
  11192.       /* #if/#ifdef/#ifndef's are all identified by "T_IF" (see calls to
  11193.          conditional_skip in do_if/do_xifdef) */
  11194.       if (if_stack && if_stack->type == T_IF && if_stack->control_macro) {
  11195.         /* just because we are in the context of "some" #ifndef, check
  11196.            whether the input we are currently processing had one. If it did,
  11197.            make sure we skip to its matching #endif, else go to end of buf */
  11198.         if (ip->file->control_macro)
  11199.           ip->bufp = ip->file->position_of_last_endif;
  11200.         else
  11201.           ip->bufp = ip->buf + ip->length;
  11202.         enable_macros_from_header(ip->file);
  11203.         /* fprintf(stderr, "SKIP for %s: %.20s (saved %d)\n", 
  11204.          *        ip->fname, ip->bufp, total_saved += ip->bufp-sav);
  11205.          */
  11206.         return 1;
  11207.       } else {
  11208.         /* this means the last include is in the context of an arbitrary
  11209.            conditional (i.e. not the "ifndef idiom"). for now, we will just
  11210.            exclude it from this optimization. It is possible to include it, 
  11211.            but this will require more complexity */
  11212.         /* fprintf(stderr, "CAN'T SKIP for %s: %.20s\n", ip->fname, ip->bufp); */
  11213.         return 0;
  11214.       }
  11215.     } else if (!ip->file->position_after_last_include) { 
  11216.       /* no #includes, currently processing a "leaf" header */
  11217.       ip->bufp = ip->buf + ip->length;
  11218.       enable_macros_from_header(ip->file);
  11219.       /* fprintf(stderr, "NO INCLUDES for %s: %.20s (saved %d)\n", 
  11220.        *        ip->fname, ip->bufp, total_saved += ip->bufp-sav);
  11221.        */
  11222.       return 1;
  11223.     } else { /* this case is not currently optimized (obviously) */
  11224.       return 0;
  11225.     }
  11226. }
  11227.  
  11228. static void
  11229. dump_memory_statistics ()
  11230. {
  11231.   struct file_name_list *new;
  11232.   struct import_file *imp;
  11233.   struct negative_file *neg;
  11234.   int n_elements = 0, n_other = 0, size_bufs = 0, size_mlists = 0;
  11235.   int n_expansion_strings = 0, n_string_count = 0, control_names = 0;
  11236.   int bucket;
  11237.  
  11238.   fprintf(stderr, "all_input_files (chain of struct file_name_list's):\n");
  11239.   for (new = all_input_files; new; new = new->next) {
  11240.     n_elements++;
  11241.     n_string_count += (strlen(new->fname)+1);
  11242.     size_bufs += new->length;
  11243.   }
  11244.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11245.                    n_elements * sizeof(struct file_name_list),
  11246.                    n_elements, sizeof(struct file_name_list));
  11247.   fprintf(stderr, "%10d bytes for input file names\n", n_string_count);
  11248.   fprintf(stderr, "%10d bytes for input buffers\n", size_bufs);
  11249.  
  11250.   n_string_count = n_elements = size_bufs = 0;
  11251.  
  11252.   fprintf(stderr, "all_output_files (chain of struct file_name_list's):\n");
  11253.   for (new = all_output_files; new; new = new->next) {
  11254.     n_elements++;
  11255.     if (new->fname)
  11256.       n_string_count += (strlen(new->fname)+1);
  11257.     size_bufs += new->length;
  11258.   }
  11259.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11260.                    n_elements * sizeof(struct file_name_list),
  11261.                    n_elements, sizeof(struct file_name_list));
  11262.   fprintf(stderr, "%10d bytes for output file names\n", n_string_count);
  11263.   fprintf(stderr, "%10d bytes for output buffers\n", size_bufs);
  11264.  
  11265.   n_string_count = n_elements = size_bufs = 0;
  11266.   fprintf(stderr, "all_include_files (chain of struct file_name_list's):\n");
  11267.   for (new = all_include_files; new; new = new->next) {
  11268.     n_elements++;
  11269.     n_string_count += (strlen(new->fname)+1);
  11270.     if (new->control_macro)
  11271.       control_names += (strlen(new->control_macro)+1);
  11272.     size_bufs += new->length;
  11273.     size_mlists += new->macro_count * sizeof ( HASHNODE * );
  11274.     fprintf(stderr, "%s\n", new->fname);
  11275.   }
  11276.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11277.                    n_elements * sizeof(struct file_name_list),
  11278.                    n_elements, sizeof(struct file_name_list));
  11279.   fprintf(stderr, "%10d bytes for file names\n", n_string_count);
  11280.   fprintf(stderr, "%10d bytes for control macro names\n", control_names);
  11281.   fprintf(stderr, "%10d bytes for input buffers\n", size_bufs);
  11282.   fprintf(stderr, "%10d bytes for macro lists\n", size_mlists);
  11283.  
  11284.   n_string_count = n_elements = 0;
  11285.   fprintf(stderr, "include directories (chain of struct file_name_list's):\n");
  11286.   for (new = include; new; new = new->next) {
  11287.     n_elements++;
  11288.     n_string_count += (strlen(new->fname)+1);
  11289.   }
  11290.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11291.                    n_elements * sizeof(struct file_name_list),
  11292.                    n_elements, sizeof(struct file_name_list));
  11293.   fprintf(stderr, "%10d bytes for directory names\n", n_string_count);
  11294.  
  11295.   n_elements = 0;
  11296.   fprintf(stderr, "import_hash_table (struct import_file's):\n");
  11297.   for (bucket = 0; bucket < IMPORT_HASH_SIZE; bucket++) {
  11298.     imp = import_hash_table[bucket];
  11299.     while (imp) {
  11300.       n_elements++;
  11301.       imp = imp->next;
  11302.     }
  11303.   }
  11304.   fprintf(stderr, "%10d bytes for import hash table buckets (statically allocated)\n",
  11305.            sizeof(import_hash_table));
  11306.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11307.                    n_elements * sizeof(struct import_file),
  11308.                    n_elements, sizeof(struct import_file));
  11309.  
  11310.   n_elements = 0;
  11311.   n_string_count = 0;
  11312.   fprintf(stderr, "negative_hash_table (struct negative_file's):\n");
  11313.   for (bucket = 0; bucket < IMPORT_HASH_SIZE; bucket++) {
  11314.     neg = negative_hash_table[bucket];
  11315.     while (neg) {
  11316.       n_elements++;
  11317.       n_string_count += (strlen(neg->name)+1);
  11318.       neg = neg->next;
  11319.     }
  11320.   }
  11321.   fprintf(stderr, "%10d bytes for negative hash table buckets (statically allocated)\n",
  11322.            sizeof(negative_hash_table));
  11323.   fprintf(stderr, "%10d bytes for %d elements (%d bytes each)\n",
  11324.                    n_elements * sizeof(struct negative_file),
  11325.                    n_elements, sizeof(struct negative_file));
  11326.   fprintf(stderr, "%10d bytes for file names\n", n_string_count);
  11327.  
  11328.   fprintf(stderr, "hashtab for macro's (struct hashnode's/definition's):\n");
  11329.   n_string_count = 0;
  11330.   n_elements = 0;
  11331.   n_other = 0;
  11332.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  11333.     register HASHNODE *hp;
  11334.  
  11335.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  11336.       if (hp->type == T_MACRO) {
  11337.         n_elements++;
  11338.         if (hp->value.defn->length) {
  11339.           n_string_count++;
  11340.           n_expansion_strings += hp->value.defn->length;
  11341.         }
  11342.       } else {
  11343.         n_other++;
  11344.       }
  11345.     }
  11346.   }
  11347.   fprintf(stderr, "%10d bytes for macro hash table buckets (statically allocated)\n",
  11348.            sizeof(hashtab));
  11349.   fprintf(stderr, "%10d bytes for %d macro hashnode's (%d bytes each)\n",
  11350.                    n_elements * sizeof(struct hashnode),
  11351.                    n_elements, sizeof(struct hashnode));
  11352.   fprintf(stderr, "%10d bytes for %d macro definitions's (%d bytes each)\n",
  11353.                    n_elements * sizeof(struct definition),
  11354.                    n_elements, sizeof(struct definition));
  11355.   fprintf(stderr, "%10d bytes for %d macro expansion strings\n",
  11356.                    n_expansion_strings, n_string_count);
  11357.   fprintf(stderr, "%10d bytes for %d other hashnode's (%d bytes each)\n",
  11358.                    n_other * sizeof(struct hashnode),
  11359.                    n_other, sizeof(struct hashnode));
  11360.   return;
  11361. }
  11362.  
  11363. static void
  11364. enable_macros_from_header(entry)
  11365.    struct file_name_list *entry;
  11366. {
  11367.    int i;
  11368.  
  11369.    if (entry->macro_list) {
  11370.       /* list may not yet be filled in */
  11371.       for (i = 0; i < entry->macro_count; i++) {
  11372.          entry->macro_list[i]->defined_within_module = 1;
  11373.       }
  11374.    }
  11375. }
  11376.  
  11377. static void
  11378. clear_import()
  11379. {
  11380.   struct import_file *i;
  11381.   int h;
  11382.  
  11383.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  11384.     i = import_hash_table[h];
  11385.     while (i) {
  11386.       i->visited_within_module = 0;
  11387.       /* dump_file_name_list(i->name); */
  11388.       i = i->next;
  11389.     }
  11390.   }
  11391. }
  11392.  
  11393. static void
  11394. really_delete_macro(hp)
  11395. HASHNODE * hp;
  11396. {
  11397.    DEFINITION *d = hp->value.defn;
  11398.    struct reflist *ap, *nextap;
  11399.  
  11400.    for (ap = d->pattern; ap != NULL; ap = nextap) {
  11401.       nextap = ap->next;
  11402.       free (ap);
  11403.    }
  11404.    if ((d->nargs > 0) && d->args.argnames)
  11405.       free (d->args.argnames);
  11406.  
  11407.    free (d);
  11408.    free (hp);
  11409. }
  11410.  
  11411. static void
  11412. clear_all_macros ()
  11413. {
  11414.   int bucket;
  11415.   register HASHNODE *hp, *hp_next;
  11416.  
  11417.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  11418.  
  11419.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  11420.        if (hp->type == T_MACRO) {
  11421.         /* possible bug...are we disabling command line stuff? Probably not. */
  11422.     hp->defined_within_module = 0;
  11423.         if (hp->owner && hp->owner->macro_count) {
  11424.           if (hp->owner->macro_index < hp->owner->macro_count) { 
  11425.             /* not done filling in the list */
  11426.             if (!hp->owner->macro_list) {
  11427.               int nbytes = hp->owner->macro_count * sizeof ( HASHNODE * );
  11428.               hp->owner->macro_list = (HASHNODE **) xmalloc ( nbytes );
  11429.               bzero(hp->owner->macro_list, nbytes);
  11430.             } 
  11431.             /* stash in the next empty slot */
  11432.             hp->owner->macro_list[hp->owner->macro_index++] = hp;
  11433.           } 
  11434.           /* else, header has been filled (or it has no macros) */
  11435.         } else { 
  11436.           /* defined on the command line (not a header) */
  11437.         }
  11438.       }
  11439.     }
  11440.   }
  11441.  
  11442.   /* free macros from delete_macro (usually due to #undef) */
  11443.   for (hp = free_undefined_macros; hp; hp = hp_next) {
  11444.      hp_next = hp->next;
  11445.      really_delete_macro (hp);
  11446.   }
  11447. #if 0
  11448.   /* free macros from delete_macro (usually due to #undef) */
  11449.   for (hp = installed_builtins; hp; hp = hp_next) {
  11450.      hp_next = hp->next;
  11451.      really_delete_macro (hp);
  11452.   }
  11453. #endif  
  11454. }
  11455.  
  11456.  
  11457.  
  11458. /* Returns last known modifiy time for FILENAME */
  11459.  
  11460. time_t file_modtime(const char * filename) {
  11461.   int hashval = import_hash (filename);
  11462.  
  11463.   /* Attempt to find file in list of already included files */
  11464.   struct import_file *i = import_hash_table[hashval];
  11465.  
  11466.   while (i) {
  11467.      if (!strcmp (filename, i->name->fname)) {
  11468.         return i->modtime;
  11469.      }
  11470.      i = i->next;
  11471.   }
  11472.   return 0;
  11473. }
  11474.  
  11475. static void handle_file_modification(const char * filename) {
  11476.   struct file_name_list *ptr, *next_ptr, *prev_ptr, *version, *next_version;
  11477.   int hashval = import_hash (filename);
  11478.   int multiple_versions = 0, removed_from_hash = 0, removed_from_cache = 0;
  11479.   struct import_file *prev = NULL;
  11480.   struct import_file *i = import_hash_table[hashval];
  11481.   while (i) {
  11482.      if (!strcmp (filename, i->name->fname)) {
  11483.         if (prev)
  11484.            prev->next = i->next;
  11485.         else
  11486.            import_hash_table[hashval] = i->next;
  11487.         removed_from_hash = 1;
  11488.      }
  11489.      prev = i;
  11490.      i = i->next;
  11491.   }
  11492.   if (!removed_from_hash) {
  11493.      const char * basefilename = strrchr(filename,'/');
  11494.      if (basefilename) {
  11495.         /*  Look for other files by the same name */
  11496.         for (hashval = 0; hashval < IMPORT_HASH_SIZE; hashval++) {
  11497.        prev = NULL;
  11498.            i = import_hash_table[hashval];
  11499.            while (i) {
  11500.               const char * baseiname = strrchr(i->name->fname,'/');
  11501.               if (!strcmp (basefilename, baseiname)) {
  11502.                  if (prev)
  11503.                     prev->next = i->next;
  11504.                  else
  11505.                     import_hash_table[hashval] = i->next;
  11506.                  fprintf(stderr,"...punted %s\n",i->name->fname);
  11507.               }
  11508.               prev = i;
  11509.               i = i->next;
  11510.            }
  11511.         }
  11512.      } else {
  11513.         fprintf(stderr,"Cpp Internal Error: could not remove file '%s' from import hash table.  Looks like a bad file name!!\n", filename);
  11514.      }
  11515.   }
  11516.  
  11517.  
  11518.   /* This looks through the entire header cache for copies of this header.
  11519.      It's probably more efficient to terminate the search after finding the
  11520.      header being modified, but the conservative approach is more likely
  11521.      to be correct. */
  11522.   
  11523.   prev_ptr = NULL;
  11524.   for (ptr = next_ptr = all_include_files; ptr; ptr = next_ptr) {
  11525.      next_ptr = ptr->next;
  11526.      if (ptr->fname && !strcmp (ptr->fname, filename)) {
  11527.         if (ptr->versions) {
  11528.            multiple_versions = 1;
  11529.            for (version = next_version = ptr->versions;
  11530.                 version;
  11531.                 version = next_version) {
  11532.               next_version = version->next;
  11533.               free(version);
  11534.            }
  11535.         }
  11536.         if (prev_ptr)
  11537.            prev_ptr->next = next_ptr;
  11538.         else
  11539.            all_include_files = next_ptr;
  11540.         free(ptr->fname);
  11541.         free(ptr);
  11542.         removed_from_cache = 1;
  11543.      }
  11544.      prev_ptr = ptr;
  11545.   }
  11546.  
  11547. #ifdef DEBUG
  11548.   if (!removed_from_cache)
  11549.      fprintf(stderr,"Warning: could not remove file '%s' from import cache.\n", filename);
  11550. #endif
  11551.   
  11552.   if (multiple_versions)
  11553.      fprintf(stderr,"Removed all versions of %s from header cache\n", filename);
  11554.   else
  11555.      fprintf(stderr,"Removed %s from header cache\n", filename);
  11556.   fflush(stderr);
  11557. }
  11558.  
  11559. static void init_output_file_buf(FILE_BUF * outbuf)
  11560. {
  11561.   outbuf->buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  11562.   outbuf->length = OUTBUF_SIZE;
  11563.   outbuf->bufp = outbuf->output_written_to = outbuf->buf;
  11564.   bzero (outbuf->bufp, outbuf->length);
  11565.   outbuf->lineno = 0;
  11566. }
  11567.  
  11568.  
  11569. static int sysUsr_startTime;
  11570. static int sysUsr_finishTime;
  11571.   
  11572. static struct timeval real_startTime;
  11573. static struct timeval real_finishTime;
  11574.  
  11575.  
  11576.   /* Return time used so far, in microseconds.  */
  11577. static int gettime ()
  11578. {
  11579. #if defined(hpux)
  11580.    struct tms tms;
  11581.    times (&tms);
  11582.    return tms.tms_utime / CLK_TCK;
  11583. #else
  11584.    struct rusage rusage;
  11585.    getrusage (0, &rusage);
  11586.    return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
  11587.         + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
  11588. #endif
  11589. }
  11590.  
  11591. static void start() 
  11592. {
  11593.     sysUsr_startTime = gettime();
  11594.     gettimeofday(&real_startTime, 0);
  11595. }
  11596.  
  11597. static void stop() 
  11598. {
  11599.     sysUsr_finishTime = gettime();
  11600.     gettimeofday(&real_finishTime, 0);
  11601. }
  11602.  
  11603. static void display(FILE * strm, int addNewLine)
  11604. {
  11605.     real_finishTime.tv_sec -= real_startTime.tv_sec;
  11606.     real_finishTime.tv_usec -= real_startTime.tv_usec;
  11607.     if (real_finishTime.tv_usec < 0)
  11608.       real_finishTime.tv_sec--, real_finishTime.tv_usec += 1000000;
  11609.       
  11610.     fprintf(strm, "%3d.%03d (sys+usr) %3d.%01d (real)%s",
  11611.          (sysUsr_finishTime-sysUsr_startTime) / 1000000, 
  11612.          (sysUsr_finishTime-sysUsr_startTime) % 1000000, 
  11613.          real_finishTime.tv_sec, 
  11614.          real_finishTime.tv_usec/100000,
  11615.          addNewLine ? "\n" : "");
  11616. }
  11617. #endif /*  NEXT_SEMANTICS */
  11618.  
  11619. #endif /* 0 */
  11620.