Annotation of gcc/cccp.c, revision 1.1.1.6

1.1       root        1: /* C Compatible Compiler Preprocessor (CCCP)
1.1.1.6 ! root        2: Copyright (C) 1986, 1987, 1989, 1992, 1993 Free Software Foundation, Inc.
1.1       root        3:                     Written by Paul Rubin, June 1986
                      4:                    Adapted to ANSI C, Richard Stallman, Jan 1987
                      5: 
                      6: This program is free software; you can redistribute it and/or modify it
                      7: under the terms of the GNU General Public License as published by the
                      8: Free Software Foundation; either version 2, or (at your option) any
                      9: later version.
                     10: 
                     11: This program is distributed in the hope that it will be useful,
                     12: but WITHOUT ANY WARRANTY; without even the implied warranty of
                     13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     14: GNU General Public License for more details.
                     15: 
                     16: You should have received a copy of the GNU General Public License
                     17: along with this program; if not, write to the Free Software
                     18: Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
                     19: 
                     20:  In other words, you are welcome to use, share and improve this program.
                     21:  You are forbidden to forbid anyone else to use, share and improve
                     22:  what you give them.   Help stamp out software-hoarding!  */
                     23: 
                     24: typedef unsigned char U_CHAR;
                     25: 
                     26: #ifdef EMACS
                     27: #define NO_SHORTNAMES
                     28: #include "../src/config.h"
                     29: #ifdef open
                     30: #undef open
                     31: #undef read
                     32: #undef write
                     33: #endif /* open */
                     34: #endif /* EMACS */
                     35: 
                     36: /* The macro EMACS is defined when cpp is distributed as part of Emacs,
                     37:    for the sake of machines with limited C compilers.  */
                     38: #ifndef EMACS
                     39: #include "config.h"
                     40: #endif /* not EMACS */
                     41: 
                     42: #ifndef STANDARD_INCLUDE_DIR
                     43: #define STANDARD_INCLUDE_DIR "/usr/include"
                     44: #endif
                     45: 
                     46: #ifndef LOCAL_INCLUDE_DIR
                     47: #define LOCAL_INCLUDE_DIR "/usr/local/include"
                     48: #endif
                     49: 
1.1.1.4   root       50: #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
                     51: #ifdef __STDC__
                     52: #define PTR_INT_TYPE ptrdiff_t
                     53: #else
                     54: #define PTR_INT_TYPE long
                     55: #endif
                     56: #endif /* 0 */
                     57: 
1.1       root       58: #include "pcp.h"
                     59: 
                     60: #ifndef STDC_VALUE
                     61: #define STDC_VALUE 1
                     62: #endif
                     63: 
1.1.1.3   root       64: /* By default, colon separates directories in a path.  */
                     65: #ifndef PATH_SEPARATOR
                     66: #define PATH_SEPARATOR ':'
                     67: #endif
                     68: 
1.1       root       69: /* In case config.h defines these.  */
                     70: #undef bcopy
                     71: #undef bzero
                     72: #undef bcmp
                     73: 
                     74: #include <sys/types.h>
                     75: #include <sys/stat.h>
                     76: #include <ctype.h>
                     77: #include <stdio.h>
1.1.1.5   root       78: #include <signal.h>
1.1       root       79: 
                     80: #ifndef VMS
                     81: #ifndef USG
                     82: #include <sys/time.h>          /* for __DATE__ and __TIME__ */
                     83: #include <sys/resource.h>
                     84: #else
                     85: #include <time.h>
                     86: #include <fcntl.h>
                     87: #endif /* USG */
                     88: #endif /* not VMS */
                     89: 
1.1.1.5   root       90: /* This defines "errno" properly for VMS, and gives us EACCES. */
                     91: #include <errno.h>
                     92: 
1.1       root       93: /* VMS-specific definitions */
                     94: #ifdef VMS
                     95: #include <time.h>
                     96: #include <perror.h>            /* This defines sys_errlist/sys_nerr properly */
                     97: #include <descrip.h>
                     98: #define O_RDONLY       0       /* Open arg for Read/Only  */
                     99: #define O_WRONLY       1       /* Open arg for Write/Only */
1.1.1.5   root      100: #define read(fd,buf,size)      VMS_read (fd,buf,size)
                    101: #define write(fd,buf,size)     VMS_write (fd,buf,size)
                    102: #define open(fname,mode,prot)  VMS_open (fname,mode,prot)
                    103: #define fopen(fname,mode)      VMS_fopen (fname,mode)
                    104: #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
                    105: #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
1.1.1.4   root      106: static char * VMS_strncat ();
1.1       root      107: static int VMS_read ();
                    108: static int VMS_write ();
                    109: static int VMS_open ();
                    110: static FILE * VMS_fopen ();
                    111: static FILE * VMS_freopen ();
                    112: static void hack_vms_include_specification ();
                    113: typedef struct { unsigned :16, :16, :16; } vms_ino_t;
                    114: #define ino_t vms_ino_t
1.1.1.4   root      115: #define INCLUDE_LEN_FUDGE 10   /* leave room for VMS syntax conversion */
1.1       root      116: #ifdef __GNUC__
                    117: #define BSTRING                        /* VMS/GCC supplies the bstring routines */
                    118: #endif /* __GNUC__ */
                    119: #endif /* VMS */
1.1.1.4   root      120:   
                    121: extern char *index ();
                    122: extern char *rindex ();
1.1       root      123: 
                    124: #ifndef O_RDONLY
                    125: #define O_RDONLY 0
                    126: #endif
                    127: 
                    128: #undef MIN
                    129: #undef MAX
                    130: #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
                    131: #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
                    132: 
1.1.1.4   root      133: /* Find the largest host integer type and set its size and type.  */
                    134: 
                    135: #ifndef HOST_BITS_PER_WIDE_INT
                    136: 
                    137: #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
                    138: #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
                    139: #define HOST_WIDE_INT long
                    140: #else
                    141: #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
                    142: #define HOST_WIDE_INT int
                    143: #endif
                    144: 
                    145: #endif
                    146: 
1.1       root      147: #ifndef S_ISREG
                    148: #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
                    149: #endif
                    150: 
1.1.1.5   root      151: #ifndef S_ISDIR
                    152: #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
                    153: #endif
                    154: 
1.1.1.4   root      155: /* Define a generic NULL if one hasn't already been defined.  */
                    156: 
                    157: #ifndef NULL
                    158: #define NULL 0
                    159: #endif
                    160: 
                    161: #ifndef GENERIC_PTR
                    162: #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
                    163: #define GENERIC_PTR void *
                    164: #else
                    165: #define GENERIC_PTR char *
                    166: #endif
                    167: #endif
                    168: 
                    169: #ifndef NULL_PTR
                    170: #define NULL_PTR ((GENERIC_PTR)0)
                    171: #endif
                    172: 
                    173: #ifndef INCLUDE_LEN_FUDGE
                    174: #define INCLUDE_LEN_FUDGE 0
                    175: #endif
                    176: 
                    177: /* Forward declarations.  */
1.1       root      178: 
                    179: char *xmalloc ();
                    180: void error ();
                    181: void warning ();
                    182: 
                    183: /* External declarations.  */
                    184: 
                    185: extern char *getenv ();
                    186: extern FILE *fdopen ();
                    187: extern char *version_string;
                    188: extern struct tm *localtime ();
                    189: extern int sys_nerr;
1.1.1.6 ! root      190: #if defined(bsd4_4)
        !           191: extern const char *const sys_errlist[];
        !           192: #else
1.1       root      193: extern char *sys_errlist[];
1.1.1.6 ! root      194: #endif
        !           195: extern int parse_escape ();
1.1       root      196: 
                    197: #ifndef errno
                    198: extern int errno;
                    199: #endif
                    200: 
                    201: /* Forward declarations.  */
                    202: 
                    203: struct directive;
                    204: struct file_buf;
                    205: struct arglist;
                    206: struct argdata;
                    207: 
                    208: #if defined(USG) || defined(VMS)
                    209: #ifndef BSTRING
                    210: void bcopy ();
                    211: void bzero ();
                    212: int bcmp ();
                    213: #endif
                    214: #endif
                    215: 
                    216: /* These functions are declared to return int instead of void since they
                    217:    are going to be placed in a table and some old compilers have trouble with
                    218:    pointers to functions returning void.  */
                    219: 
                    220: static int do_define ();
                    221: static int do_line ();
                    222: static int do_include ();
                    223: static int do_undef ();
                    224: static int do_error ();
                    225: static int do_pragma ();
                    226: static int do_ident ();
                    227: static int do_if ();
                    228: static int do_xifdef ();
                    229: static int do_else ();
                    230: static int do_elif ();
                    231: static int do_endif ();
                    232: static int do_sccs ();
                    233: static int do_once ();
                    234: static int do_assert ();
                    235: static int do_unassert ();
                    236: static int do_warning ();
                    237: 
                    238: static void add_import ();
1.1.1.4   root      239: static void append_include_chain ();
1.1       root      240: static void deps_output ();
                    241: static void make_undef ();
                    242: static void make_definition ();
                    243: static void make_assertion ();
                    244: static void path_include ();
                    245: static void initialize_builtins ();
                    246: static void initialize_char_syntax ();
                    247: static void dump_arg_n ();
                    248: static void dump_defn_1 ();
                    249: static void delete_macro ();
                    250: static void trigraph_pcp ();
                    251: static void rescan ();
                    252: static void finclude ();
                    253: static void validate_else ();
                    254: static int comp_def_part ();
                    255: static void error_from_errno ();
                    256: static void error_with_line ();
                    257: void pedwarn ();
1.1.1.5   root      258: void pedwarn_with_line ();
1.1       root      259: static void pedwarn_with_file_and_line ();
                    260: static void fatal ();
                    261: void fancy_abort ();
                    262: static void pfatal_with_name ();
                    263: static void perror_with_name ();
1.1.1.5   root      264: static void pipe_closed ();
1.1       root      265: static void print_containing_files ();
                    266: static int lookup_import ();
1.1.1.4   root      267: static int redundant_include_p ();
                    268: static is_system_include ();
1.1       root      269: static int check_preconditions ();
                    270: static void pcfinclude ();
                    271: static void pcstring_used ();
                    272: static void write_output ();
                    273: static int check_macro_name ();
                    274: static int compare_defs ();
                    275: static int compare_token_lists ();
                    276: static int eval_if_expression ();
                    277: static int discard_comments ();
1.1.1.5   root      278: static int change_newlines ();
1.1       root      279: static int line_for_error ();
                    280: static int hashf ();
                    281: static int file_size_and_mode ();
                    282: 
                    283: static struct arglist *read_token_list ();
                    284: static void free_token_list ();
                    285: 
                    286: static struct hashnode *install ();
                    287: struct hashnode *lookup ();
                    288: 
                    289: static struct assertion_hashnode *assertion_install ();
                    290: static struct assertion_hashnode *assertion_lookup ();
                    291: 
                    292: static char *xrealloc ();
                    293: static char *xcalloc ();
                    294: static char *savestring ();
                    295: 
                    296: static void delete_assertion ();
                    297: static void macroexpand ();
                    298: static void dump_all_macros ();
                    299: static void conditional_skip ();
                    300: static void skip_if_group ();
                    301: static void output_line_command ();
                    302: 
                    303: /* Last arg to output_line_command.  */
                    304: enum file_change_code {same_file, enter_file, leave_file};
                    305: 
                    306: static int grow_outbuf ();
                    307: static int handle_directive ();
                    308: static void memory_full ();
                    309: 
                    310: static U_CHAR *macarg1 ();
                    311: static char *macarg ();
                    312: 
                    313: static U_CHAR *skip_to_end_of_comment ();
                    314: static U_CHAR *skip_quoted_string ();
                    315: static U_CHAR *skip_paren_group ();
1.1.1.6 ! root      316: static char *quote_string ();
1.1       root      317: 
                    318: static char *check_precompiled ();
1.1.1.4   root      319: /* static struct macrodef create_definition ();        [moved below] */
1.1       root      320: static void dump_single_macro ();
1.1.1.6 ! root      321: static void output_dots ();
1.1       root      322: 
                    323: #ifndef FAILURE_EXIT_CODE
                    324: #define FAILURE_EXIT_CODE 33   /* gnu cc command understands this */
                    325: #endif
                    326: 
                    327: #ifndef SUCCESS_EXIT_CODE
                    328: #define SUCCESS_EXIT_CODE 0    /* 0 means success on Unix.  */
                    329: #endif
                    330: 
                    331: /* Name under which this program was invoked.  */
                    332: 
                    333: static char *progname;
                    334: 
1.1.1.4   root      335: /* Nonzero means use extra default include directories for C++.  */
1.1       root      336: 
                    337: static int cplusplus;
                    338: 
1.1.1.4   root      339: /* Nonzero means handle cplusplus style comments */
                    340: 
                    341: static int cplusplus_comments;
                    342: 
1.1       root      343: /* Nonzero means handle #import, for objective C.  */
                    344: 
                    345: static int objc;
                    346: 
                    347: /* Nonzero means this is an assembly file, and allow
                    348:    unknown directives, which could be comments.  */
                    349: 
                    350: static int lang_asm;
                    351: 
                    352: /* Current maximum length of directory names in the search path
                    353:    for include files.  (Altered as we get more of them.)  */
                    354: 
                    355: static int max_include_len;
                    356: 
                    357: /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
                    358: 
                    359: static int lint = 0;
                    360: 
                    361: /* Nonzero means copy comments into the output file.  */
                    362: 
                    363: static int put_out_comments = 0;
                    364: 
                    365: /* Nonzero means don't process the ANSI trigraph sequences.  */
                    366: 
                    367: static int no_trigraphs = 0;
                    368: 
                    369: /* Nonzero means print the names of included files rather than
                    370:    the preprocessed output.  1 means just the #include "...",
                    371:    2 means #include <...> as well.  */
                    372: 
                    373: static int print_deps = 0;
                    374: 
                    375: /* Nonzero means print names of header files (-H).  */
                    376: 
                    377: static int print_include_names = 0;
                    378: 
                    379: /* Nonzero means don't output line number information.  */
                    380: 
                    381: static int no_line_commands;
                    382: 
                    383: /* dump_only means inhibit output of the preprocessed text
                    384:              and instead output the definitions of all user-defined
                    385:              macros in a form suitable for use as input to cccp.
                    386:    dump_names means pass #define and the macro name through to output.
                    387:    dump_definitions means pass the whole definition (plus #define) through
                    388: */
                    389: 
                    390: static enum {dump_none, dump_only, dump_names, dump_definitions}
                    391:      dump_macros = dump_none;
                    392: 
                    393: /* Nonzero means pass all #define and #undef directives which we actually
                    394:    process through to the output stream.  This feature is used primarily
                    395:    to allow cc1 to record the #defines and #undefs for the sake of
                    396:    debuggers which understand about preprocessor macros, but it may
                    397:    also be useful with -E to figure out how symbols are defined, and
                    398:    where they are defined.  */
                    399: static int debug_output = 0;
                    400: 
                    401: /* Nonzero indicates special processing used by the pcp program.  The
                    402:    special effects of this mode are: 
                    403:      
                    404:      Inhibit all macro expansion, except those inside #if directives.
                    405: 
                    406:      Process #define directives normally, and output their contents 
                    407:      to the output file.
                    408: 
                    409:      Output preconditions to pcp_outfile indicating all the relevant
                    410:      preconditions for use of this file in a later cpp run.
                    411: */
                    412: static FILE *pcp_outfile;
                    413: 
                    414: /* Nonzero means we are inside an IF during a -pcp run.  In this mode
                    415:    macro expansion is done, and preconditions are output for all macro
                    416:    uses requiring them. */
                    417: static int pcp_inside_if;
                    418: 
1.1.1.5   root      419: /* Nonzero means never to include precompiled files.
                    420:    This is 1 since there's no way now to make precompiled files,
                    421:    so it's not worth testing for them.  */
                    422: static int no_precomp = 1;
1.1       root      423: 
                    424: /* Nonzero means give all the error messages the ANSI standard requires.  */
                    425: 
                    426: int pedantic;
                    427: 
                    428: /* Nonzero means try to make failure to fit ANSI C an error.  */
                    429: 
                    430: static int pedantic_errors;
                    431: 
                    432: /* Nonzero means don't print warning messages.  -w.  */
                    433: 
                    434: static int inhibit_warnings = 0;
                    435: 
                    436: /* Nonzero means warn if slash-star appears in a comment.  */
                    437: 
                    438: static int warn_comments;
                    439: 
                    440: /* Nonzero means warn if a macro argument is (or would be)
                    441:    stringified with -traditional.  */
                    442: 
                    443: static int warn_stringify;
                    444: 
                    445: /* Nonzero means warn if there are any trigraphs.  */
                    446: 
                    447: static int warn_trigraphs;
                    448: 
1.1.1.2   root      449: /* Nonzero means warn if #import is used.  */
                    450: 
                    451: static int warn_import = 1;
                    452: 
1.1       root      453: /* Nonzero means turn warnings into errors.  */
                    454: 
                    455: static int warnings_are_errors;
                    456: 
                    457: /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
                    458: 
                    459: int traditional;
                    460: 
                    461: /* Nonzero causes output not to be done,
                    462:    but directives such as #define that have side effects
                    463:    are still obeyed.  */
                    464: 
                    465: static int no_output;
                    466: 
                    467: /* Nonzero means that we have finished processing the command line options.
                    468:    This flag is used to decide whether or not to issue certain errors
                    469:    and/or warnings.  */
                    470: 
                    471: static int done_initializing = 0;
1.1.1.5   root      472: 
                    473: /* Line where a newline was first seen in a string constant.  */
                    474: 
                    475: static int multiline_string_line = 0;
1.1       root      476: 
                    477: /* I/O buffer structure.
                    478:    The `fname' field is nonzero for source files and #include files
                    479:    and for the dummy text used for -D and -U.
                    480:    It is zero for rescanning results of macro expansion
                    481:    and for expanding macro arguments.  */
1.1.1.6 ! root      482: #define INPUT_STACK_MAX 400
1.1       root      483: static struct file_buf {
                    484:   char *fname;
                    485:   /* Filename specified with #line command.  */
                    486:   char *nominal_fname;
                    487:   /* Record where in the search path this file was found.
                    488:      For #include_next.  */
                    489:   struct file_name_list *dir;
                    490:   int lineno;
                    491:   int length;
                    492:   U_CHAR *buf;
                    493:   U_CHAR *bufp;
                    494:   /* Macro that this level is the expansion of.
                    495:      Included so that we can reenable the macro
                    496:      at the end of this level.  */
                    497:   struct hashnode *macro;
                    498:   /* Value of if_stack at start of this file.
                    499:      Used to prohibit unmatched #endif (etc) in an include file.  */
                    500:   struct if_stack *if_stack;
                    501:   /* Object to be freed at end of input at this level.  */
                    502:   U_CHAR *free_ptr;
                    503:   /* True if this is a header file included using <FILENAME>.  */
                    504:   char system_header_p;
                    505: } instack[INPUT_STACK_MAX];
                    506: 
                    507: static int last_error_tick;       /* Incremented each time we print it.  */
                    508: static int input_file_stack_tick;  /* Incremented when the status changes.  */
                    509: 
                    510: /* Current nesting level of input sources.
                    511:    `instack[indepth]' is the level currently being read.  */
                    512: static int indepth = -1;
                    513: #define CHECK_DEPTH(code) \
                    514:   if (indepth >= (INPUT_STACK_MAX - 1))                                        \
                    515:     {                                                                  \
                    516:       error_with_line (line_for_error (instack[indepth].lineno),       \
                    517:                       "macro or `#include' recursion too deep");       \
                    518:       code;                                                            \
                    519:     }
                    520: 
                    521: /* Current depth in #include directives that use <...>.  */
                    522: static int system_include_depth = 0;
                    523: 
                    524: typedef struct file_buf FILE_BUF;
                    525: 
                    526: /* The output buffer.  Its LENGTH field is the amount of room allocated
                    527:    for the buffer, not the number of chars actually present.  To get
                    528:    that, subtract outbuf.buf from outbuf.bufp. */
                    529: 
                    530: #define OUTBUF_SIZE 10 /* initial size of output buffer */
                    531: static FILE_BUF outbuf;
                    532: 
                    533: /* Grow output buffer OBUF points at
                    534:    so it can hold at least NEEDED more chars.  */
                    535: 
                    536: #define check_expand(OBUF, NEEDED)  \
                    537:   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
                    538:    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
                    539: 
                    540: struct file_name_list
                    541:   {
                    542:     struct file_name_list *next;
                    543:     char *fname;
                    544:     /* If the following is nonzero, it is a macro name.
                    545:        Don't include the file again if that macro is defined.  */
                    546:     U_CHAR *control_macro;
1.1.1.6 ! root      547:     /* If the following is nonzero, it is a C-language system include
        !           548:        directory.  */
        !           549:     int c_system_include_path;
1.1       root      550:   };
                    551: 
                    552: /* #include "file" looks in source file dir, then stack. */
                    553: /* #include <file> just looks in the stack. */
                    554: /* -I directories are added to the end, then the defaults are added. */
                    555: static struct default_include { char *fname; int cplusplus; } include_defaults_array[]
                    556: #ifdef INCLUDE_DEFAULTS
                    557:   = INCLUDE_DEFAULTS;
                    558: #else
                    559:   = {
                    560:     /* Pick up GNU C++ specific include files.  */
                    561:     { GPLUSPLUS_INCLUDE_DIR, 1},
                    562: #ifdef CROSS_COMPILE
1.1.1.5   root      563:     /* This is the dir for fixincludes.  Put it just before
                    564:        the files that we fix.  */
                    565:     { GCC_INCLUDE_DIR, 0},
1.1       root      566:     /* For cross-compilation, this dir name is generated
                    567:        automatically in Makefile.in.  */
                    568:     { CROSS_INCLUDE_DIR, 0 },
1.1.1.5   root      569:     /* This is another place that the target system's headers might be.  */
                    570:     { TOOL_INCLUDE_DIR, 0},
1.1       root      571: #else /* not CROSS_COMPILE */
1.1.1.5   root      572:     /* This should be /use/local/include and should come before
                    573:        the fixincludes-fixed header files.  */
1.1       root      574:     { LOCAL_INCLUDE_DIR, 0},
1.1.1.5   root      575:     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
                    576:        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
                    577:     { TOOL_INCLUDE_DIR, 0},
                    578:     /* This is the dir for fixincludes.  Put it just before
                    579:        the files that we fix.  */
                    580:     { GCC_INCLUDE_DIR, 0},
1.1       root      581:     /* Some systems have an extra dir of include files.  */
                    582: #ifdef SYSTEM_INCLUDE_DIR
                    583:     { SYSTEM_INCLUDE_DIR, 0},
                    584: #endif
                    585:     { STANDARD_INCLUDE_DIR, 0},
                    586: #endif /* not CROSS_COMPILE */
                    587:     { 0, 0}
                    588:     };
                    589: #endif /* no INCLUDE_DEFAULTS */
                    590: 
                    591: /* The code looks at the defaults through this pointer, rather than through
                    592:    the constant structure above.  This pointer gets changed if an environment
                    593:    variable specifies other defaults.  */
                    594: static struct default_include *include_defaults = include_defaults_array;
                    595: 
                    596: static struct file_name_list *include = 0;     /* First dir to search */
                    597:        /* First dir to search for <file> */
1.1.1.4   root      598: /* This is the first element to use for #include <...>.
                    599:    If it is 0, use the entire chain for such includes.  */
1.1       root      600: static struct file_name_list *first_bracket_include = 0;
1.1.1.4   root      601: /* This is the first element in the chain that corresponds to
                    602:    a directory of system header files.  */
                    603: static struct file_name_list *first_system_include = 0;
1.1       root      604: static struct file_name_list *last_include = 0;        /* Last in chain */
                    605: 
                    606: /* Chain of include directories to put at the end of the other chain.  */
                    607: static struct file_name_list *after_include = 0;
                    608: static struct file_name_list *last_after_include = 0;  /* Last in chain */
                    609: 
                    610: /* List of included files that contained #pragma once.  */
                    611: static struct file_name_list *dont_repeat_files = 0;
                    612: 
                    613: /* List of other included files.
                    614:    If ->control_macro if nonzero, the file had a #ifndef
                    615:    around the entire contents, and ->control_macro gives the macro name.  */
                    616: static struct file_name_list *all_include_files = 0;
                    617: 
1.1.1.3   root      618: /* Directory prefix that should replace `/usr' in the standard
                    619:    include file directories.  */
                    620: static char *include_prefix;
                    621: 
1.1       root      622: /* Global list of strings read in from precompiled files.  This list
                    623:    is kept in the order the strings are read in, with new strings being
                    624:    added at the end through stringlist_tailp.  We use this list to output
                    625:    the strings at the end of the run. 
                    626: */
                    627: static STRINGDEF *stringlist;
                    628: static STRINGDEF **stringlist_tailp = &stringlist;
                    629: 
                    630: 
                    631: /* Structure returned by create_definition */
                    632: typedef struct macrodef MACRODEF;
                    633: struct macrodef
                    634: {
                    635:   struct definition *defn;
                    636:   U_CHAR *symnam;
                    637:   int symlen;
                    638: };
                    639: 
1.1.1.4   root      640: static struct macrodef create_definition ();
                    641: 
1.1       root      642: 
                    643: /* Structure allocated for every #define.  For a simple replacement
                    644:    such as
                    645:        #define foo bar ,
                    646:    nargs = -1, the `pattern' list is null, and the expansion is just
                    647:    the replacement text.  Nargs = 0 means a functionlike macro with no args,
                    648:    e.g.,
                    649:        #define getchar() getc (stdin) .
                    650:    When there are args, the expansion is the replacement text with the
                    651:    args squashed out, and the reflist is a list describing how to
                    652:    build the output from the input: e.g., "3 chars, then the 1st arg,
                    653:    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
                    654:    The chars here come from the expansion.  Whatever is left of the
                    655:    expansion after the last arg-occurrence is copied after that arg.
                    656:    Note that the reflist can be arbitrarily long---
                    657:    its length depends on the number of times the arguments appear in
                    658:    the replacement text, not how many args there are.  Example:
                    659:    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
                    660:    pattern list
                    661:      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
                    662:    where (x, y) means (nchars, argno). */
                    663: 
                    664: typedef struct definition DEFINITION;
                    665: struct definition {
                    666:   int nargs;
                    667:   int length;                  /* length of expansion string */
                    668:   int predefined;              /* True if the macro was builtin or */
                    669:                                /* came from the command line */
                    670:   U_CHAR *expansion;
                    671:   int line;                    /* Line number of definition */
                    672:   char *file;                  /* File of definition */
1.1.1.3   root      673:   char rest_args;              /* Nonzero if last arg. absorbs the rest */
1.1       root      674:   struct reflist {
                    675:     struct reflist *next;
                    676:     char stringify;            /* nonzero if this arg was preceded by a
                    677:                                   # operator. */
                    678:     char raw_before;           /* Nonzero if a ## operator before arg. */
                    679:     char raw_after;            /* Nonzero if a ## operator after arg. */
1.1.1.3   root      680:     char rest_args;            /* Nonzero if this arg. absorbs the rest */
1.1       root      681:     int nchars;                        /* Number of literal chars to copy before
                    682:                                   this arg occurrence.  */
                    683:     int argno;                 /* Number of arg to substitute (origin-0) */
                    684:   } *pattern;
                    685:   union {
                    686:     /* Names of macro args, concatenated in reverse order
                    687:        with comma-space between them.
                    688:        The only use of this is that we warn on redefinition
                    689:        if this differs between the old and new definitions.  */
                    690:     U_CHAR *argnames;
                    691:   } args;
                    692: };
                    693: 
                    694: /* different kinds of things that can appear in the value field
                    695:    of a hash node.  Actually, this may be useless now. */
                    696: union hashval {
                    697:   int ival;
                    698:   char *cpval;
                    699:   DEFINITION *defn;
                    700:   KEYDEF *keydef;
                    701: };
                    702: 
1.1.1.3   root      703: /*
                    704:  * special extension string that can be added to the last macro argument to 
                    705:  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
1.1.1.5   root      706:  *             #define wow(a, b...)            process (b, a, b)
                    707:  *             { wow (1, 2, 3); }      ->      { process (2, 3, 1, 2, 3); }
                    708:  *             { wow (one, two); }     ->      { process (two, one, two); }
1.1.1.3   root      709:  * if this "rest_arg" is used with the concat token '##' and if it is not
1.1.1.4   root      710:  * supplied then the token attached to with ## will not be outputted.  Ex:
1.1.1.5   root      711:  *             #define wow (a, b...)           process (b ## , a, ## b)
                    712:  *             { wow (1, 2); }         ->      { process (2, 1, 2); }
                    713:  *             { wow (one); }          ->      { process (one); {
1.1.1.3   root      714:  */
                    715: static char rest_extension[] = "...";
                    716: #define REST_EXTENSION_LENGTH  (sizeof (rest_extension) - 1)
1.1       root      717: 
                    718: /* The structure of a node in the hash table.  The hash table
                    719:    has entries for all tokens defined by #define commands (type T_MACRO),
                    720:    plus some special tokens like __LINE__ (these each have their own
                    721:    type, and the appropriate code is run when that type of node is seen.
                    722:    It does not contain control words like "#define", which are recognized
                    723:    by a separate piece of code. */
                    724: 
                    725: /* different flavors of hash nodes --- also used in keyword table */
                    726: enum node_type {
                    727:  T_DEFINE = 1, /* the `#define' keyword */
                    728:  T_INCLUDE,    /* the `#include' keyword */
                    729:  T_INCLUDE_NEXT, /* the `#include_next' keyword */
                    730:  T_IMPORT,      /* the `#import' keyword */
                    731:  T_IFDEF,      /* the `#ifdef' keyword */
                    732:  T_IFNDEF,     /* the `#ifndef' keyword */
                    733:  T_IF,         /* the `#if' keyword */
                    734:  T_ELSE,       /* `#else' */
                    735:  T_PRAGMA,     /* `#pragma' */
                    736:  T_ELIF,       /* `#elif' */
                    737:  T_UNDEF,      /* `#undef' */
                    738:  T_LINE,       /* `#line' */
                    739:  T_ERROR,      /* `#error' */
                    740:  T_WARNING,    /* `#warning' */
                    741:  T_ENDIF,      /* `#endif' */
                    742:  T_SCCS,       /* `#sccs', used on system V.  */
                    743:  T_IDENT,      /* `#ident', used on system V.  */
                    744:  T_ASSERT,     /* `#assert', taken from system V.  */
                    745:  T_UNASSERT,   /* `#unassert', taken from system V.  */
                    746:  T_SPECLINE,   /* special symbol `__LINE__' */
                    747:  T_DATE,       /* `__DATE__' */
                    748:  T_FILE,       /* `__FILE__' */
                    749:  T_BASE_FILE,  /* `__BASE_FILE__' */
                    750:  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
                    751:  T_VERSION,    /* `__VERSION__' */
                    752:  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
                    753:  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
                    754:  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
1.1.1.5   root      755:  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
                    756:  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
1.1       root      757:  T_TIME,       /* `__TIME__' */
                    758:  T_CONST,      /* Constant value, used by `__STDC__' */
                    759:  T_MACRO,      /* macro defined by `#define' */
                    760:  T_DISABLED,   /* macro temporarily turned off for rescan */
                    761:  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
                    762:  T_PCSTRING,   /* precompiled string (hashval is KEYDEF *) */
                    763:  T_UNUSED      /* Used for something not defined.  */
                    764:  };
                    765: 
                    766: struct hashnode {
                    767:   struct hashnode *next;       /* double links for easy deletion */
                    768:   struct hashnode *prev;
                    769:   struct hashnode **bucket_hdr;        /* also, a back pointer to this node's hash
                    770:                                   chain is kept, in case the node is the head
                    771:                                   of the chain and gets deleted. */
                    772:   enum node_type type;         /* type of special token */
                    773:   int length;                  /* length of token, for quick comparison */
                    774:   U_CHAR *name;                        /* the actual name */
                    775:   union hashval value;         /* pointer to expansion, or whatever */
                    776: };
                    777: 
                    778: typedef struct hashnode HASHNODE;
                    779: 
                    780: /* Some definitions for the hash table.  The hash function MUST be
                    781:    computed as shown in hashf () below.  That is because the rescan
                    782:    loop computes the hash value `on the fly' for most tokens,
                    783:    in order to avoid the overhead of a lot of procedure calls to
                    784:    the hashf () function.  Hashf () only exists for the sake of
                    785:    politeness, for use when speed isn't so important. */
                    786: 
                    787: #define HASHSIZE 1403
                    788: static HASHNODE *hashtab[HASHSIZE];
                    789: #define HASHSTEP(old, c) ((old << 2) + c)
                    790: #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
                    791: 
                    792: /* Symbols to predefine.  */
                    793: 
                    794: #ifdef CPP_PREDEFINES
                    795: static char *predefs = CPP_PREDEFINES;
                    796: #else
                    797: static char *predefs = "";
                    798: #endif
                    799: 
                    800: /* We let tm.h override the types used here, to handle trivial differences
                    801:    such as the choice of unsigned int or long unsigned int for size_t.
                    802:    When machines start needing nontrivial differences in the size type,
                    803:    it would be best to do something here to figure out automatically
                    804:    from other information what type to use.  */
                    805: 
                    806: /* The string value for __size_type__.  */
                    807: 
                    808: #ifndef SIZE_TYPE
                    809: #define SIZE_TYPE "long unsigned int"
                    810: #endif
                    811: 
                    812: /* The string value for __ptrdiff_type__.  */
                    813: 
                    814: #ifndef PTRDIFF_TYPE
                    815: #define PTRDIFF_TYPE "long int"
                    816: #endif
                    817: 
                    818: /* The string value for __wchar_type__.  */
                    819: 
                    820: #ifndef WCHAR_TYPE
                    821: #define WCHAR_TYPE "int"
                    822: #endif
1.1.1.5   root      823: 
                    824: /* The string value for __USER_LABEL_PREFIX__ */
                    825: 
                    826: #ifndef USER_LABEL_PREFIX
                    827: #define USER_LABEL_PREFIX ""
                    828: #endif
                    829: 
                    830: /* The string value for __REGISTER_PREFIX__ */
                    831: 
                    832: #ifndef REGISTER_PREFIX
                    833: #define REGISTER_PREFIX ""
                    834: #endif
1.1       root      835: 
                    836: /* In the definition of a #assert name, this structure forms
                    837:    a list of the individual values asserted.
                    838:    Each value is itself a list of "tokens".
                    839:    These are strings that are compared by name.  */
                    840: 
                    841: struct tokenlist_list {
                    842:   struct tokenlist_list *next;
                    843:   struct arglist *tokens;
                    844: };
                    845: 
                    846: struct assertion_hashnode {
                    847:   struct assertion_hashnode *next;     /* double links for easy deletion */
                    848:   struct assertion_hashnode *prev;
                    849:   /* also, a back pointer to this node's hash
                    850:      chain is kept, in case the node is the head
                    851:      of the chain and gets deleted. */
                    852:   struct assertion_hashnode **bucket_hdr;
                    853:   int length;                  /* length of token, for quick comparison */
                    854:   U_CHAR *name;                        /* the actual name */
                    855:   /* List of token-sequences.  */
                    856:   struct tokenlist_list *value;
                    857: };
                    858: 
                    859: typedef struct assertion_hashnode ASSERTION_HASHNODE;
                    860: 
                    861: /* Some definitions for the hash table.  The hash function MUST be
1.1.1.4   root      862:    computed as shown in hashf below.  That is because the rescan
1.1       root      863:    loop computes the hash value `on the fly' for most tokens,
                    864:    in order to avoid the overhead of a lot of procedure calls to
1.1.1.4   root      865:    the hashf function.  hashf only exists for the sake of
1.1       root      866:    politeness, for use when speed isn't so important. */
                    867: 
                    868: #define ASSERTION_HASHSIZE 37
                    869: static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
                    870: 
                    871: /* Nonzero means inhibit macroexpansion of what seem to be
                    872:    assertion tests, in rescan.  For #if.  */
                    873: static int assertions_flag;
                    874: 
                    875: /* `struct directive' defines one #-directive, including how to handle it.  */
                    876: 
                    877: struct directive {
                    878:   int length;                  /* Length of name */
                    879:   int (*func)();               /* Function to handle directive */
                    880:   char *name;                  /* Name of directive */
                    881:   enum node_type type;         /* Code which describes which directive. */
                    882:   char angle_brackets;         /* Nonzero => <...> is special.  */
                    883:   char traditional_comments;   /* Nonzero: keep comments if -traditional.  */
                    884:   char pass_thru;              /* Copy preprocessed directive to output file.  */
                    885: };
                    886: 
                    887: /* Here is the actual list of #-directives, most-often-used first.  */
                    888: 
                    889: static struct directive directive_table[] = {
                    890:   {  6, do_define, "define", T_DEFINE, 0, 1},
                    891:   {  2, do_if, "if", T_IF},
                    892:   {  5, do_xifdef, "ifdef", T_IFDEF},
                    893:   {  6, do_xifdef, "ifndef", T_IFNDEF},
                    894:   {  5, do_endif, "endif", T_ENDIF},
                    895:   {  4, do_else, "else", T_ELSE},
                    896:   {  4, do_elif, "elif", T_ELIF},
                    897:   {  4, do_line, "line", T_LINE},
                    898:   {  7, do_include, "include", T_INCLUDE, 1},
                    899:   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
                    900:   {  6, do_include, "import", T_IMPORT, 1},
                    901:   {  5, do_undef, "undef", T_UNDEF},
                    902:   {  5, do_error, "error", T_ERROR},
                    903:   {  7, do_warning, "warning", T_WARNING},
                    904: #ifdef SCCS_DIRECTIVE
                    905:   {  4, do_sccs, "sccs", T_SCCS},
                    906: #endif
                    907:   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
1.1.1.6 ! root      908:   {  5, do_ident, "ident", T_IDENT},
1.1       root      909:   {  6, do_assert, "assert", T_ASSERT},
                    910:   {  8, do_unassert, "unassert", T_UNASSERT},
                    911:   {  -1, 0, "", T_UNUSED},
                    912: };
                    913: 
                    914: /* When a directive handler is called,
                    915:    this points to the # that started the directive.  */
                    916: U_CHAR *directive_start;
                    917: 
                    918: /* table to tell if char can be part of a C identifier. */
                    919: U_CHAR is_idchar[256];
                    920: /* table to tell if char can be first char of a c identifier. */
                    921: U_CHAR is_idstart[256];
                    922: /* table to tell if c is horizontal space.  */
                    923: U_CHAR is_hor_space[256];
                    924: /* table to tell if c is horizontal or vertical space.  */
                    925: static U_CHAR is_space[256];
                    926: 
                    927: #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
                    928: #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
                    929:   
                    930: static int errors = 0;                 /* Error counter for exit code */
                    931: 
1.1.1.5   root      932: /* Name of output file, for error messages.  */
                    933: static char *out_fname;
                    934: 
1.1       root      935: /* Zero means dollar signs are punctuation.
                    936:    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
                    937:    This must be 0 for correct processing of this ANSI C program:
                    938:        #define foo(a) #a
1.1.1.5   root      939:        #define lose(b) foo (b)
1.1       root      940:        #define test$
1.1.1.5   root      941:        lose (test)     */
1.1       root      942: static int dollars_in_ident;
                    943: #ifndef DOLLARS_IN_IDENTIFIERS
                    944: #define DOLLARS_IN_IDENTIFIERS 1
                    945: #endif
                    946: 
                    947: static FILE_BUF expand_to_temp_buffer ();
                    948: 
                    949: static DEFINITION *collect_expansion ();
                    950: 
                    951: /* Stack of conditionals currently in progress
                    952:    (including both successful and failing conditionals).  */
                    953: 
                    954: struct if_stack {
                    955:   struct if_stack *next;       /* for chaining to the next stack frame */
                    956:   char *fname;         /* copied from input when frame is made */
                    957:   int lineno;                  /* similarly */
                    958:   int if_succeeded;            /* true if a leg of this if-group
                    959:                                    has been passed through rescan */
                    960:   U_CHAR *control_macro;       /* For #ifndef at start of file,
                    961:                                   this is the macro name tested.  */
                    962:   enum node_type type;         /* type of last directive seen in this group */
                    963: };
                    964: typedef struct if_stack IF_STACK_FRAME;
                    965: static IF_STACK_FRAME *if_stack = NULL;
                    966: 
                    967: /* Buffer of -M output.  */
                    968: static char *deps_buffer;
                    969: 
                    970: /* Number of bytes allocated in above.  */
                    971: static int deps_allocated_size;
                    972: 
                    973: /* Number of bytes used.  */
                    974: static int deps_size;
                    975: 
                    976: /* Number of bytes since the last newline.  */
                    977: static int deps_column;
                    978: 
                    979: /* Nonzero means -I- has been seen,
                    980:    so don't look for #include "foo" the source-file directory.  */
                    981: static int ignore_srcdir;
                    982: 
1.1.1.6 ! root      983: /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
        !           984:    retrying if necessary.  Return the actual number of bytes read.  */
        !           985: 
        !           986: static int
        !           987: safe_read (desc, ptr, len)
        !           988:      int desc;
        !           989:      char *ptr;
        !           990:      int len;
        !           991: {
        !           992:   int left = len;
        !           993:   while (left > 0) {
        !           994:     int nchars = read (desc, ptr, left);
        !           995:     if (nchars < 0)
        !           996:       {
        !           997: #ifdef EINTR
        !           998:        if (errno == EINTR)
        !           999:          continue;
        !          1000: #endif
        !          1001:        return nchars;
        !          1002:       }
        !          1003:     if (nchars == 0)
        !          1004:       break;
        !          1005:     ptr += nchars;
        !          1006:     left -= nchars;
        !          1007:   }
        !          1008:   return len - left;
        !          1009: }
        !          1010: 
        !          1011: /* Write LEN bytes at PTR to descriptor DESC,
        !          1012:    retrying if necessary, and treating any real error as fatal.  */
        !          1013: 
        !          1014: static void
        !          1015: safe_write (desc, ptr, len)
        !          1016:      int desc;
        !          1017:      char *ptr;
        !          1018:      int len;
        !          1019: {
        !          1020:   while (len > 0) {
        !          1021:     int written = write (desc, ptr, len);
        !          1022:     if (written < 0)
        !          1023:       {
        !          1024: #ifdef EINTR
        !          1025:        if (errno == EINTR)
        !          1026:          continue;
        !          1027: #endif
        !          1028:        pfatal_with_name (out_fname);
        !          1029:       }
        !          1030:     ptr += written;
        !          1031:     len -= written;
        !          1032:   }
        !          1033: }
        !          1034: 
1.1       root     1035: int
                   1036: main (argc, argv)
                   1037:      int argc;
                   1038:      char **argv;
                   1039: {
                   1040:   int st_mode;
                   1041:   long st_size;
1.1.1.5   root     1042:   char *in_fname;
1.1       root     1043:   char *p;
                   1044:   int f, i;
                   1045:   FILE_BUF *fp;
                   1046:   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
                   1047:   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
                   1048:   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
                   1049:   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
                   1050:   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
                   1051: 
                   1052:   /* Record the option used with each element of pend_assertions.
                   1053:      This is preparation for supporting more than one option for making
                   1054:      an assertion.  */
                   1055:   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
                   1056:   int inhibit_predefs = 0;
                   1057:   int no_standard_includes = 0;
1.1.1.3   root     1058:   int no_standard_cplusplus_includes = 0;
1.1       root     1059:   int missing_newline = 0;
                   1060: 
                   1061:   /* Non-0 means don't output the preprocessed program.  */
                   1062:   int inhibit_output = 0;
1.1.1.6 ! root     1063:   /* Non-0 means -v, so print the full set of include dirs.  */
        !          1064:   int verbose = 0;
1.1       root     1065: 
1.1.1.4   root     1066:   /* File name which deps are being written to.
                   1067:      This is 0 if deps are being written to stdout.  */
                   1068:   char *deps_file = 0;
1.1.1.5   root     1069:   /* Fopen file mode to open deps_file with.  */
                   1070:   char *deps_mode = "a";
1.1       root     1071:   /* Stream on which to print the dependency information.  */
                   1072:   FILE *deps_stream = 0;
                   1073:   /* Target-name to write with the dependency information.  */
                   1074:   char *deps_target = 0;
                   1075: 
                   1076: #ifdef RLIMIT_STACK
                   1077:   /* Get rid of any avoidable limit on stack size.  */
                   1078:   {
                   1079:     struct rlimit rlim;
                   1080: 
                   1081:     /* Set the stack limit huge so that alloca (particularly stringtab
                   1082:      * in dbxread.c) does not fail. */
                   1083:     getrlimit (RLIMIT_STACK, &rlim);
                   1084:     rlim.rlim_cur = rlim.rlim_max;
                   1085:     setrlimit (RLIMIT_STACK, &rlim);
                   1086:   }
                   1087: #endif /* RLIMIT_STACK defined */
                   1088: 
1.1.1.6 ! root     1089: #ifdef SIGPIPE
1.1.1.5   root     1090:   signal (SIGPIPE, pipe_closed);
1.1.1.6 ! root     1091: #endif
1.1.1.5   root     1092: 
                   1093:   p = argv[0] + strlen (argv[0]);
                   1094:   while (p != argv[0] && p[-1] != '/') --p;
                   1095:   progname = p;
                   1096: 
1.1       root     1097: #ifdef VMS
                   1098:   {
                   1099:     /* Remove directories from PROGNAME.  */
                   1100:     char *s;
                   1101: 
                   1102:     progname = savestring (argv[0]);
                   1103: 
                   1104:     if (!(s = rindex (progname, ']')))
                   1105:       s = rindex (progname, ':');
                   1106:     if (s)
                   1107:       strcpy (progname, s+1);
                   1108:     if (s = rindex (progname, '.'))
                   1109:       *s = '\0';
                   1110:   }
                   1111: #endif
                   1112: 
                   1113:   in_fname = NULL;
                   1114:   out_fname = NULL;
                   1115: 
                   1116:   /* Initialize is_idchar to allow $.  */
                   1117:   dollars_in_ident = 1;
                   1118:   initialize_char_syntax ();
                   1119:   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
                   1120: 
                   1121:   no_line_commands = 0;
                   1122:   no_trigraphs = 1;
                   1123:   dump_macros = dump_none;
                   1124:   no_output = 0;
                   1125:   cplusplus = 0;
1.1.1.4   root     1126:   cplusplus_comments = 0;
1.1       root     1127: 
                   1128:   bzero (pend_files, argc * sizeof (char *));
                   1129:   bzero (pend_defs, argc * sizeof (char *));
                   1130:   bzero (pend_undefs, argc * sizeof (char *));
                   1131:   bzero (pend_assertions, argc * sizeof (char *));
                   1132:   bzero (pend_includes, argc * sizeof (char *));
                   1133: 
                   1134:   /* Process switches and find input file name.  */
                   1135: 
                   1136:   for (i = 1; i < argc; i++) {
                   1137:     if (argv[i][0] != '-') {
                   1138:       if (out_fname != NULL)
                   1139:        fatal ("Usage: %s [switches] input output", argv[0]);
                   1140:       else if (in_fname != NULL)
                   1141:        out_fname = argv[i];
                   1142:       else
                   1143:        in_fname = argv[i];
                   1144:     } else {
                   1145:       switch (argv[i][1]) {
                   1146: 
                   1147:       case 'i':
                   1148:        if (!strcmp (argv[i], "-include")) {
                   1149:          if (i + 1 == argc)
1.1.1.5   root     1150:            fatal ("Filename missing after `-include' option");
1.1       root     1151:          else
                   1152:            pend_includes[i] = argv[i+1], i++;
                   1153:        }
                   1154:        if (!strcmp (argv[i], "-imacros")) {
                   1155:          if (i + 1 == argc)
1.1.1.5   root     1156:            fatal ("Filename missing after `-imacros' option");
1.1       root     1157:          else
                   1158:            pend_files[i] = argv[i+1], i++;
                   1159:        }
1.1.1.3   root     1160:        if (!strcmp (argv[i], "-iprefix")) {
                   1161:          if (i + 1 == argc)
1.1.1.5   root     1162:            fatal ("Filename missing after `-iprefix' option");
1.1.1.3   root     1163:          else
                   1164:            include_prefix = argv[++i];
                   1165:        }
1.1.1.5   root     1166:        /* Add directory to end of path for includes,
                   1167:           with the default prefix at the front of its name.  */
                   1168:        if (!strcmp (argv[i], "-iwithprefix")) {
                   1169:          struct file_name_list *dirtmp;
1.1.1.6 ! root     1170:          char *prefix;
        !          1171: 
        !          1172:          if (include_prefix != 0)
        !          1173:            prefix = include_prefix;
        !          1174:          else {
        !          1175:            prefix = savestring (GCC_INCLUDE_DIR);
        !          1176:            /* Remove the `include' from /usr/local/lib/gcc.../include.  */
        !          1177:            if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
        !          1178:              prefix[strlen (prefix) - 7] = 0;
        !          1179:          }
1.1.1.5   root     1180: 
                   1181:          dirtmp = (struct file_name_list *)
                   1182:            xmalloc (sizeof (struct file_name_list));
                   1183:          dirtmp->next = 0;     /* New one goes on the end */
                   1184:          dirtmp->control_macro = 0;
1.1.1.6 ! root     1185:          dirtmp->c_system_include_path = 0;
1.1.1.5   root     1186:          if (i + 1 == argc)
                   1187:            fatal ("Directory name missing after `-iwithprefix' option");
                   1188: 
                   1189:          dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
1.1.1.6 ! root     1190:                                            + strlen (prefix) + 1);
        !          1191:          strcpy (dirtmp->fname, prefix);
1.1.1.5   root     1192:          strcat (dirtmp->fname, argv[++i]);
                   1193: 
                   1194:          if (after_include == 0)
                   1195:            after_include = dirtmp;
                   1196:          else
                   1197:            last_after_include->next = dirtmp;
                   1198:          last_after_include = dirtmp; /* Tail follows the last one */
                   1199:        }
1.1.1.6 ! root     1200:        /* Add directory to main path for includes,
        !          1201:           with the default prefix at the front of its name.  */
        !          1202:        if (!strcmp (argv[i], "-iwithprefixbefore")) {
        !          1203:          struct file_name_list *dirtmp;
        !          1204:          char *prefix;
        !          1205: 
        !          1206:          if (include_prefix != 0)
        !          1207:            prefix = include_prefix;
        !          1208:          else {
        !          1209:            prefix = savestring (GCC_INCLUDE_DIR);
        !          1210:            /* Remove the `include' from /usr/local/lib/gcc.../include.  */
        !          1211:            if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
        !          1212:              prefix[strlen (prefix) - 7] = 0;
        !          1213:          }
        !          1214: 
        !          1215:          dirtmp = (struct file_name_list *)
        !          1216:            xmalloc (sizeof (struct file_name_list));
        !          1217:          dirtmp->next = 0;     /* New one goes on the end */
        !          1218:          dirtmp->control_macro = 0;
        !          1219:          dirtmp->c_system_include_path = 0;
        !          1220:          if (i + 1 == argc)
        !          1221:            fatal ("Directory name missing after `-iwithprefixbefore' option");
        !          1222: 
        !          1223:          dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
        !          1224:                                            + strlen (prefix) + 1);
        !          1225:          strcpy (dirtmp->fname, prefix);
        !          1226:          strcat (dirtmp->fname, argv[++i]);
        !          1227: 
        !          1228:          append_include_chain (dirtmp, dirtmp);
        !          1229:        }
1.1       root     1230:        /* Add directory to end of path for includes.  */
                   1231:        if (!strcmp (argv[i], "-idirafter")) {
                   1232:          struct file_name_list *dirtmp;
                   1233: 
                   1234:          dirtmp = (struct file_name_list *)
                   1235:            xmalloc (sizeof (struct file_name_list));
                   1236:          dirtmp->next = 0;     /* New one goes on the end */
                   1237:          dirtmp->control_macro = 0;
1.1.1.6 ! root     1238:          dirtmp->c_system_include_path = 0;
1.1       root     1239:          if (i + 1 == argc)
1.1.1.5   root     1240:            fatal ("Directory name missing after `-idirafter' option");
1.1       root     1241:          else
                   1242:            dirtmp->fname = argv[++i];
                   1243: 
1.1.1.4   root     1244:          if (after_include == 0)
                   1245:            after_include = dirtmp;
                   1246:          else
                   1247:            last_after_include->next = dirtmp;
                   1248:          last_after_include = dirtmp; /* Tail follows the last one */
1.1       root     1249:        }
                   1250:        break;
                   1251: 
                   1252:       case 'o':
                   1253:        if (out_fname != NULL)
                   1254:          fatal ("Output filename specified twice");
                   1255:        if (i + 1 == argc)
                   1256:          fatal ("Filename missing after -o option");
                   1257:        out_fname = argv[++i];
                   1258:        if (!strcmp (out_fname, "-"))
                   1259:          out_fname = "";
                   1260:        break;
                   1261: 
                   1262:       case 'p':
                   1263:        if (!strcmp (argv[i], "-pedantic"))
                   1264:          pedantic = 1;
                   1265:        else if (!strcmp (argv[i], "-pedantic-errors")) {
                   1266:          pedantic = 1;
                   1267:          pedantic_errors = 1;
                   1268:        } else if (!strcmp (argv[i], "-pcp")) {
                   1269:          char *pcp_fname = argv[++i];
                   1270:          pcp_outfile = 
                   1271:            ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
                   1272:             ? fopen (pcp_fname, "w")
                   1273:             : fdopen (dup (fileno (stdout)), "w"));
                   1274:          if (pcp_outfile == 0)
                   1275:            pfatal_with_name (pcp_fname);
                   1276:          no_precomp = 1;
                   1277:        }
                   1278:        break;
                   1279: 
                   1280:       case 't':
                   1281:        if (!strcmp (argv[i], "-traditional")) {
                   1282:          traditional = 1;
                   1283:          if (dollars_in_ident > 0)
                   1284:            dollars_in_ident = 1;
                   1285:        } else if (!strcmp (argv[i], "-trigraphs")) {
                   1286:          no_trigraphs = 0;
                   1287:        }
                   1288:        break;
                   1289: 
                   1290:       case 'l':
                   1291:        if (! strcmp (argv[i], "-lang-c"))
1.1.1.4   root     1292:          cplusplus = 0, cplusplus_comments = 0, objc = 0;
1.1       root     1293:        if (! strcmp (argv[i], "-lang-c++"))
1.1.1.4   root     1294:          cplusplus = 1, cplusplus_comments = 1, objc = 0;
1.1.1.6 ! root     1295:        if (! strcmp (argv[i], "-lang-c-c++-comments"))
        !          1296:          cplusplus = 0, cplusplus_comments = 1, objc = 0;
1.1       root     1297:        if (! strcmp (argv[i], "-lang-objc"))
1.1.1.4   root     1298:          objc = 1, cplusplus = 0, cplusplus_comments = 1;
1.1       root     1299:        if (! strcmp (argv[i], "-lang-objc++"))
1.1.1.4   root     1300:          objc = 1, cplusplus = 1, cplusplus_comments = 1;
1.1       root     1301:        if (! strcmp (argv[i], "-lang-asm"))
                   1302:          lang_asm = 1;
                   1303:        if (! strcmp (argv[i], "-lint"))
                   1304:          lint = 1;
                   1305:        break;
                   1306: 
                   1307:       case '+':
1.1.1.4   root     1308:        cplusplus = 1, cplusplus_comments = 1;
1.1       root     1309:        break;
                   1310: 
                   1311:       case 'w':
                   1312:        inhibit_warnings = 1;
                   1313:        break;
                   1314: 
                   1315:       case 'W':
                   1316:        if (!strcmp (argv[i], "-Wtrigraphs"))
                   1317:          warn_trigraphs = 1;
                   1318:        else if (!strcmp (argv[i], "-Wno-trigraphs"))
                   1319:          warn_trigraphs = 0;
                   1320:        else if (!strcmp (argv[i], "-Wcomment"))
                   1321:          warn_comments = 1;
                   1322:        else if (!strcmp (argv[i], "-Wno-comment"))
                   1323:          warn_comments = 0;
                   1324:        else if (!strcmp (argv[i], "-Wcomments"))
                   1325:          warn_comments = 1;
                   1326:        else if (!strcmp (argv[i], "-Wno-comments"))
                   1327:          warn_comments = 0;
                   1328:        else if (!strcmp (argv[i], "-Wtraditional"))
                   1329:          warn_stringify = 1;
                   1330:        else if (!strcmp (argv[i], "-Wno-traditional"))
                   1331:          warn_stringify = 0;
1.1.1.2   root     1332:        else if (!strcmp (argv[i], "-Wimport"))
                   1333:          warn_import = 1;
                   1334:        else if (!strcmp (argv[i], "-Wno-import"))
                   1335:          warn_import = 0;
1.1       root     1336:        else if (!strcmp (argv[i], "-Werror"))
                   1337:          warnings_are_errors = 1;
                   1338:        else if (!strcmp (argv[i], "-Wno-error"))
                   1339:          warnings_are_errors = 0;
                   1340:        else if (!strcmp (argv[i], "-Wall"))
                   1341:          {
                   1342:            warn_trigraphs = 1;
                   1343:            warn_comments = 1;
                   1344:          }
                   1345:        break;
                   1346: 
                   1347:       case 'M':
                   1348:        if (!strcmp (argv[i], "-M"))
                   1349:          print_deps = 2;
                   1350:        else if (!strcmp (argv[i], "-MM"))
                   1351:          print_deps = 1;
                   1352:        else if (!strcmp (argv[i], "-MD"))
                   1353:          print_deps = 2;
                   1354:        else if (!strcmp (argv[i], "-MMD"))
                   1355:          print_deps = 1;
                   1356:        /* For -MD and -MMD options, write deps on file named by next arg.  */
                   1357:        if (!strcmp (argv[i], "-MD")
                   1358:            || !strcmp (argv[i], "-MMD")) {
                   1359:          i++;
                   1360:          deps_file = argv[i];
1.1.1.5   root     1361:          deps_mode = "w";
1.1       root     1362:        } else {
                   1363:          /* For -M and -MM, write deps on standard output
                   1364:             and suppress the usual output.  */
                   1365:          deps_stream = stdout;
                   1366:          inhibit_output = 1;
                   1367:        }         
                   1368:        break;
                   1369: 
                   1370:       case 'd':
                   1371:        {
                   1372:          char *p = argv[i] + 2;
                   1373:          char c;
                   1374:          while (c = *p++) {
                   1375:            /* Arg to -d specifies what parts of macros to dump */
                   1376:            switch (c) {
                   1377:            case 'M':
                   1378:              dump_macros = dump_only;
                   1379:              no_output = 1;
                   1380:              break;
                   1381:            case 'N':
                   1382:              dump_macros = dump_names;
                   1383:              break;
                   1384:            case 'D':
                   1385:              dump_macros = dump_definitions;
                   1386:              break;
                   1387:            }
                   1388:          }
                   1389:        }
                   1390:        break;
                   1391: 
                   1392:       case 'g':
                   1393:        if (argv[i][2] == '3')
                   1394:          debug_output = 1;
                   1395:        break;
                   1396: 
                   1397:       case 'v':
                   1398:        fprintf (stderr, "GNU CPP version %s", version_string);
                   1399: #ifdef TARGET_VERSION
                   1400:        TARGET_VERSION;
                   1401: #endif
                   1402:        fprintf (stderr, "\n");
1.1.1.6 ! root     1403:        verbose = 1;
1.1       root     1404:        break;
                   1405: 
                   1406:       case 'H':
                   1407:        print_include_names = 1;
                   1408:        break;
                   1409: 
                   1410:       case 'D':
                   1411:        {
                   1412:          char *p, *p1;
                   1413: 
                   1414:          if (argv[i][2] != 0)
                   1415:            p = argv[i] + 2;
                   1416:          else if (i + 1 == argc)
                   1417:            fatal ("Macro name missing after -D option");
                   1418:          else
                   1419:            p = argv[++i];
                   1420: 
                   1421:          pend_defs[i] = p;
                   1422:        }
                   1423:        break;
                   1424: 
                   1425:       case 'A':
                   1426:        {
                   1427:          char *p, *p1;
                   1428: 
                   1429:          if (argv[i][2] != 0)
                   1430:            p = argv[i] + 2;
                   1431:          else if (i + 1 == argc)
                   1432:            fatal ("Assertion missing after -A option");
                   1433:          else
                   1434:            p = argv[++i];
                   1435: 
                   1436:          if (!strcmp (p, "-")) {
                   1437:            /* -A- eliminates all predefined macros and assertions.
                   1438:               Let's include also any that were specified earlier
                   1439:               on the command line.  That way we can get rid of any
                   1440:               that were passed automatically in from GCC.  */
                   1441:            int j;
                   1442:            inhibit_predefs = 1;
                   1443:            for (j = 0; j < i; j++)
                   1444:              pend_defs[j] = pend_assertions[j] = 0;
                   1445:          } else {
                   1446:            pend_assertions[i] = p;
                   1447:            pend_assertion_options[i] = "-A";
                   1448:          }
                   1449:        }
                   1450:        break;
                   1451: 
                   1452:       case 'U':                /* JF #undef something */
                   1453:        if (argv[i][2] != 0)
                   1454:          pend_undefs[i] = argv[i] + 2;
                   1455:        else if (i + 1 == argc)
                   1456:          fatal ("Macro name missing after -U option");
                   1457:        else
                   1458:          pend_undefs[i] = argv[i+1], i++;
                   1459:        break;
                   1460: 
                   1461:       case 'C':
                   1462:        put_out_comments = 1;
                   1463:        break;
                   1464: 
                   1465:       case 'E':                        /* -E comes from cc -E; ignore it.  */
                   1466:        break;
                   1467: 
                   1468:       case 'P':
                   1469:        no_line_commands = 1;
                   1470:        break;
                   1471: 
                   1472:       case '$':                        /* Don't include $ in identifiers.  */
                   1473:        dollars_in_ident = 0;
                   1474:        break;
                   1475: 
                   1476:       case 'I':                        /* Add directory to path for includes.  */
                   1477:        {
                   1478:          struct file_name_list *dirtmp;
                   1479: 
1.1.1.4   root     1480:          if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
1.1       root     1481:            ignore_srcdir = 1;
1.1.1.4   root     1482:            /* Don't use any preceding -I directories for #include <...>.  */
                   1483:            first_bracket_include = 0;
                   1484:          }
1.1       root     1485:          else {
                   1486:            dirtmp = (struct file_name_list *)
                   1487:              xmalloc (sizeof (struct file_name_list));
                   1488:            dirtmp->next = 0;           /* New one goes on the end */
                   1489:            dirtmp->control_macro = 0;
1.1.1.6 ! root     1490:            dirtmp->c_system_include_path = 0;
1.1       root     1491:            if (argv[i][2] != 0)
                   1492:              dirtmp->fname = argv[i] + 2;
                   1493:            else if (i + 1 == argc)
                   1494:              fatal ("Directory name missing after -I option");
                   1495:            else
                   1496:              dirtmp->fname = argv[++i];
1.1.1.4   root     1497:            append_include_chain (dirtmp, dirtmp);
                   1498:          }
1.1       root     1499:        }
                   1500:        break;
                   1501: 
                   1502:       case 'n':
                   1503:        if (!strcmp (argv[i], "-nostdinc"))
                   1504:          /* -nostdinc causes no default include directories.
                   1505:             You must specify all include-file directories with -I.  */
                   1506:          no_standard_includes = 1;
1.1.1.3   root     1507:        else if (!strcmp (argv[i], "-nostdinc++"))
                   1508:          /* -nostdinc++ causes no default C++-specific include directories. */
                   1509:          no_standard_cplusplus_includes = 1;
1.1       root     1510:        else if (!strcmp (argv[i], "-noprecomp"))
                   1511:          no_precomp = 1;
                   1512:        break;
                   1513: 
                   1514:       case 'u':
                   1515:        /* Sun compiler passes undocumented switch "-undef".
                   1516:           Let's assume it means to inhibit the predefined symbols.  */
                   1517:        inhibit_predefs = 1;
                   1518:        break;
                   1519: 
                   1520:       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
                   1521:        if (in_fname == NULL) {
                   1522:          in_fname = "";
                   1523:          break;
                   1524:        } else if (out_fname == NULL) {
                   1525:          out_fname = "";
                   1526:          break;
                   1527:        }       /* else fall through into error */
                   1528: 
                   1529:       default:
                   1530:        fatal ("Invalid option `%s'", argv[i]);
                   1531:       }
                   1532:     }
                   1533:   }
                   1534: 
                   1535:   /* Add dirs from CPATH after dirs from -I.  */
                   1536:   /* There seems to be confusion about what CPATH should do,
                   1537:      so for the moment it is not documented.  */
                   1538:   /* Some people say that CPATH should replace the standard include dirs,
                   1539:      but that seems pointless: it comes before them, so it overrides them
                   1540:      anyway.  */
                   1541:   p = (char *) getenv ("CPATH");
                   1542:   if (p != 0 && ! no_standard_includes)
                   1543:     path_include (p);
                   1544: 
                   1545:   /* Now that dollars_in_ident is known, initialize is_idchar.  */
                   1546:   initialize_char_syntax ();
                   1547: 
                   1548:   /* Initialize output buffer */
                   1549: 
                   1550:   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
                   1551:   outbuf.bufp = outbuf.buf;
                   1552:   outbuf.length = OUTBUF_SIZE;
                   1553: 
                   1554:   /* Do partial setup of input buffer for the sake of generating
                   1555:      early #line directives (when -g is in effect).  */
                   1556: 
                   1557:   fp = &instack[++indepth];
                   1558:   if (in_fname == NULL)
                   1559:     in_fname = "";
                   1560:   fp->nominal_fname = fp->fname = in_fname;
                   1561:   fp->lineno = 0;
                   1562: 
                   1563:   /* Install __LINE__, etc.  Must follow initialize_char_syntax
                   1564:      and option processing.  */
                   1565:   initialize_builtins (fp, &outbuf);
                   1566: 
                   1567:   /* Do standard #defines and assertions
                   1568:      that identify system and machine type.  */
                   1569: 
                   1570:   if (!inhibit_predefs) {
                   1571:     char *p = (char *) alloca (strlen (predefs) + 1);
                   1572:     strcpy (p, predefs);
                   1573:     while (*p) {
                   1574:       char *q;
                   1575:       while (*p == ' ' || *p == '\t')
                   1576:        p++;
                   1577:       /* Handle -D options.  */ 
                   1578:       if (p[0] == '-' && p[1] == 'D') {
                   1579:        q = &p[2];
                   1580:        while (*p && *p != ' ' && *p != '\t')
                   1581:          p++;
                   1582:        if (*p != 0)
                   1583:          *p++= 0;
                   1584:        if (debug_output)
                   1585:          output_line_command (fp, &outbuf, 0, same_file);
                   1586:        make_definition (q, &outbuf);
                   1587:        while (*p == ' ' || *p == '\t')
                   1588:          p++;
                   1589:       } else if (p[0] == '-' && p[1] == 'A') {
                   1590:        /* Handle -A options (assertions).  */ 
                   1591:        char *assertion;
                   1592:        char *past_name;
                   1593:        char *value;
                   1594:        char *past_value;
                   1595:        char *termination;
                   1596:        int save_char;
                   1597: 
                   1598:        assertion = &p[2];
                   1599:        past_name = assertion;
                   1600:        /* Locate end of name.  */
                   1601:        while (*past_name && *past_name != ' '
                   1602:               && *past_name != '\t' && *past_name != '(')
                   1603:          past_name++;
                   1604:        /* Locate `(' at start of value.  */
                   1605:        value = past_name;
                   1606:        while (*value && (*value == ' ' || *value == '\t'))
                   1607:          value++;
                   1608:        if (*value++ != '(')
                   1609:          abort ();
                   1610:        while (*value && (*value == ' ' || *value == '\t'))
                   1611:          value++;
                   1612:        past_value = value;
                   1613:        /* Locate end of value.  */
                   1614:        while (*past_value && *past_value != ' '
                   1615:               && *past_value != '\t' && *past_value != ')')
                   1616:          past_value++;
                   1617:        termination = past_value;
                   1618:        while (*termination && (*termination == ' ' || *termination == '\t'))
                   1619:          termination++;
                   1620:        if (*termination++ != ')')
                   1621:          abort ();
                   1622:        if (*termination && *termination != ' ' && *termination != '\t')
                   1623:          abort ();
                   1624:        /* Temporarily null-terminate the value.  */
                   1625:        save_char = *termination;
                   1626:        *termination = '\0';
                   1627:        /* Install the assertion.  */
                   1628:        make_assertion ("-A", assertion);
                   1629:        *termination = (char) save_char;
                   1630:        p = termination;
                   1631:        while (*p == ' ' || *p == '\t')
                   1632:          p++;
                   1633:       } else {
                   1634:        abort ();
                   1635:       }
                   1636:     }
                   1637:   }
                   1638: 
                   1639:   /* Now handle the command line options.  */
                   1640: 
1.1.1.4   root     1641:   /* Do -U's, -D's and -A's in the order they were seen.  */
                   1642:   for (i = 1; i < argc; i++) {
                   1643:     if (pend_undefs[i]) {
1.1       root     1644:       if (debug_output)
                   1645:         output_line_command (fp, &outbuf, 0, same_file);
1.1.1.4   root     1646:       make_undef (pend_undefs[i], &outbuf);
1.1       root     1647:     }
1.1.1.4   root     1648:     if (pend_defs[i]) {
1.1       root     1649:       if (debug_output)
                   1650:         output_line_command (fp, &outbuf, 0, same_file);
1.1.1.4   root     1651:       make_definition (pend_defs[i], &outbuf);
1.1       root     1652:     }
1.1.1.4   root     1653:     if (pend_assertions[i])
                   1654:       make_assertion (pend_assertion_options[i], pend_assertions[i]);
                   1655:   }
1.1       root     1656: 
                   1657:   done_initializing = 1;
                   1658: 
                   1659:   { /* read the appropriate environment variable and if it exists
                   1660:        replace include_defaults with the listed path. */
                   1661:     char *epath = 0;
                   1662:     switch ((objc << 1) + cplusplus)
                   1663:       {
                   1664:       case 0:
                   1665:        epath = getenv ("C_INCLUDE_PATH");
                   1666:        break;
                   1667:       case 1:
1.1.1.2   root     1668:        epath = getenv ("CPLUS_INCLUDE_PATH");
1.1       root     1669:        break;
                   1670:       case 2:
                   1671:        epath = getenv ("OBJC_INCLUDE_PATH");
                   1672:        break;
                   1673:       case 3:
1.1.1.2   root     1674:        epath = getenv ("OBJCPLUS_INCLUDE_PATH");
1.1       root     1675:        break;
                   1676:       }
                   1677:     /* If the environment var for this language is set,
                   1678:        add to the default list of include directories.  */
                   1679:     if (epath) {
                   1680:       char *nstore = (char *) alloca (strlen (epath) + 2);
                   1681:       int num_dirs;
                   1682:       char *startp, *endp;
                   1683: 
                   1684:       for (num_dirs = 1, startp = epath; *startp; startp++)
1.1.1.3   root     1685:        if (*startp == PATH_SEPARATOR)
1.1       root     1686:          num_dirs++;
                   1687:       include_defaults
                   1688:        = (struct default_include *) xmalloc ((num_dirs
                   1689:                                               * sizeof (struct default_include))
                   1690:                                              + sizeof (include_defaults_array));
                   1691:       startp = endp = epath;
                   1692:       num_dirs = 0;
                   1693:       while (1) {
1.1.1.2   root     1694:         /* Handle cases like c:/usr/lib:d:/gcc/lib */
1.1.1.3   root     1695:         if ((*endp == PATH_SEPARATOR
                   1696: #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
1.1.1.2   root     1697: #ifdef __MSDOS__
1.1.1.4   root     1698:             && (endp-startp != 1 || !isalpha (*startp))
1.1.1.2   root     1699: #endif
1.1.1.3   root     1700: #endif
1.1.1.2   root     1701:             )
1.1.1.3   root     1702:             || *endp == 0) {
1.1       root     1703:          strncpy (nstore, startp, endp-startp);
                   1704:          if (endp == startp)
                   1705:            strcpy (nstore, ".");
                   1706:          else
                   1707:            nstore[endp-startp] = '\0';
                   1708: 
                   1709:          include_defaults[num_dirs].fname = savestring (nstore);
                   1710:          include_defaults[num_dirs].cplusplus = cplusplus;
                   1711:          num_dirs++;
                   1712:          if (*endp == '\0')
                   1713:            break;
                   1714:          endp = startp = endp + 1;
                   1715:        } else
                   1716:          endp++;
                   1717:       }
                   1718:       /* Put the usual defaults back in at the end.  */
                   1719:       bcopy (include_defaults_array, &include_defaults[num_dirs],
                   1720:             sizeof (include_defaults_array));
                   1721:     }
                   1722:   }
                   1723: 
1.1.1.4   root     1724:   first_system_include = 0;
1.1       root     1725:   /* Unless -fnostdinc,
                   1726:      tack on the standard include file dirs to the specified list */
                   1727:   if (!no_standard_includes) {
                   1728:     struct default_include *p = include_defaults;
1.1.1.3   root     1729:     char *specd_prefix = include_prefix;
1.1       root     1730:     char *default_prefix = savestring (GCC_INCLUDE_DIR);
                   1731:     int default_len = 0;
                   1732:     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
                   1733:     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
                   1734:       default_len = strlen (default_prefix) - 7;
                   1735:       default_prefix[default_len] = 0;
                   1736:     }
                   1737:     /* Search "translated" versions of GNU directories.
                   1738:        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
                   1739:     if (specd_prefix != 0 && default_len != 0)
                   1740:       for (p = include_defaults; p->fname; p++) {
                   1741:        /* Some standard dirs are only for C++.  */
1.1.1.3   root     1742:        if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1.1       root     1743:          /* Does this dir start with the prefix?  */
                   1744:          if (!strncmp (p->fname, default_prefix, default_len)) {
                   1745:            /* Yes; change prefix and add to search list.  */
                   1746:            struct file_name_list *new
                   1747:              = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
                   1748:            int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
                   1749:            char *str = (char *) xmalloc (this_len + 1);
                   1750:            strcpy (str, specd_prefix);
                   1751:            strcat (str, p->fname + default_len);
                   1752:            new->fname = str;
                   1753:            new->control_macro = 0;
1.1.1.6 ! root     1754:            new->c_system_include_path = !p->cplusplus;
1.1.1.4   root     1755:            append_include_chain (new, new);
                   1756:            if (first_system_include == 0)
                   1757:              first_system_include = new;
1.1       root     1758:          }
                   1759:        }
                   1760:       }
                   1761:     /* Search ordinary names for GNU include directories.  */
                   1762:     for (p = include_defaults; p->fname; p++) {
                   1763:       /* Some standard dirs are only for C++.  */
1.1.1.3   root     1764:       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
1.1       root     1765:        struct file_name_list *new
                   1766:          = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
                   1767:        new->control_macro = 0;
1.1.1.6 ! root     1768:        new->c_system_include_path = !p->cplusplus;
1.1       root     1769:        new->fname = p->fname;
1.1.1.4   root     1770:        append_include_chain (new, new);
                   1771:        if (first_system_include == 0)
                   1772:          first_system_include = new;
1.1       root     1773:       }
                   1774:     }
                   1775:   }
                   1776: 
                   1777:   /* Tack the after_include chain at the end of the include chain.  */
1.1.1.4   root     1778:   append_include_chain (after_include, last_after_include);
                   1779:   if (first_system_include == 0)
                   1780:     first_system_include = after_include;
1.1       root     1781: 
1.1.1.6 ! root     1782:   /* With -v, print the list of dirs to search.  */
        !          1783:   if (verbose) {
        !          1784:     struct file_name_list *p;
        !          1785:     fprintf (stderr, "#include \"...\" search starts here:\n");
        !          1786:     for (p = include; p; p = p->next) {
        !          1787:       if (p == first_bracket_include)
        !          1788:        fprintf (stderr, "#include <...> search starts here:\n");
        !          1789:       fprintf (stderr, " %s\n", p->fname);
        !          1790:     }
        !          1791:     fprintf (stderr, "End of search list.\n");
        !          1792:   }
        !          1793: 
1.1       root     1794:   /* Scan the -imacros files before the main input.
                   1795:      Much like #including them, but with no_output set
                   1796:      so that only their macro definitions matter.  */
                   1797: 
                   1798:   no_output++;
                   1799:   for (i = 1; i < argc; i++)
                   1800:     if (pend_files[i]) {
                   1801:       int fd = open (pend_files[i], O_RDONLY, 0666);
                   1802:       if (fd < 0) {
                   1803:        perror_with_name (pend_files[i]);
                   1804:        return FAILURE_EXIT_CODE;
                   1805:       }
1.1.1.4   root     1806:       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
1.1       root     1807:     }
                   1808:   no_output--;
                   1809: 
                   1810:   /* Copy the entire contents of the main input file into
                   1811:      the stacked input buffer previously allocated for it.  */
                   1812: 
                   1813:   /* JF check for stdin */
                   1814:   if (in_fname == NULL || *in_fname == 0) {
                   1815:     in_fname = "";
                   1816:     f = 0;
                   1817:   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
                   1818:     goto perror;
                   1819: 
                   1820:   /* Either of two environment variables can specify output of deps.
                   1821:      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
                   1822:      where OUTPUT_FILE is the file to write deps info to
                   1823:      and DEPS_TARGET is the target to mention in the deps.  */
                   1824: 
                   1825:   if (print_deps == 0
                   1826:       && (getenv ("SUNPRO_DEPENDENCIES") != 0
                   1827:          || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
                   1828:     char *spec = getenv ("DEPENDENCIES_OUTPUT");
                   1829:     char *s;
                   1830:     char *output_file;
                   1831: 
                   1832:     if (spec == 0) {
                   1833:       spec = getenv ("SUNPRO_DEPENDENCIES");
                   1834:       print_deps = 2;
                   1835:     }
                   1836:     else
                   1837:       print_deps = 1;
                   1838: 
                   1839:     s = spec;
                   1840:     /* Find the space before the DEPS_TARGET, if there is one.  */
1.1.1.4   root     1841:     /* This should use index.  (mrs) */
1.1       root     1842:     while (*s != 0 && *s != ' ') s++;
                   1843:     if (*s != 0) {
                   1844:       deps_target = s + 1;
                   1845:       output_file = (char *) xmalloc (s - spec + 1);
                   1846:       bcopy (spec, output_file, s - spec);
                   1847:       output_file[s - spec] = 0;
                   1848:     }
                   1849:     else {
                   1850:       deps_target = 0;
                   1851:       output_file = spec;
                   1852:     }
                   1853:       
                   1854:     deps_file = output_file;
1.1.1.5   root     1855:     deps_mode = "a";
1.1       root     1856:   }
                   1857: 
                   1858:   /* For -M, print the expected object file name
                   1859:      as the target of this Make-rule.  */
                   1860:   if (print_deps) {
                   1861:     deps_allocated_size = 200;
                   1862:     deps_buffer = (char *) xmalloc (deps_allocated_size);
                   1863:     deps_buffer[0] = 0;
                   1864:     deps_size = 0;
                   1865:     deps_column = 0;
                   1866: 
                   1867:     if (deps_target) {
                   1868:       deps_output (deps_target, 0);
                   1869:       deps_output (":", 0);
                   1870:     } else if (*in_fname == 0)
                   1871:       deps_output ("-: ", 0);
                   1872:     else {
                   1873:       int len;
                   1874:       char *p = in_fname;
                   1875:       char *p1 = p;
                   1876:       /* Discard all directory prefixes from P.  */
                   1877:       while (*p1) {
                   1878:        if (*p1 == '/')
                   1879:          p = p1 + 1;
                   1880:        p1++;
                   1881:       }
                   1882:       /* Output P, but remove known suffixes.  */
                   1883:       len = strlen (p);
                   1884:       if (p[len - 2] == '.' && p[len - 1] == 'c')
                   1885:        deps_output (p, len - 2);
                   1886:       else if (p[len - 2] == '.' && p[len - 1] == 'C')
                   1887:        deps_output (p, len - 2);
                   1888:       else if (p[len - 3] == '.'
                   1889:               && p[len - 2] == 'c'
                   1890:               && p[len - 1] == 'c')
                   1891:        deps_output (p, len - 3);
1.1.1.5   root     1892:       else if (p[len - 4] == '.'
                   1893:               && p[len - 3] == 'c'
                   1894:               && p[len - 2] == 'x'
                   1895:               && p[len - 1] == 'x')
                   1896:        deps_output (p, len - 4);
1.1       root     1897:       else if (p[len - 2] == '.' && p[len - 1] == 's')
                   1898:        deps_output (p, len - 2);
                   1899:       else if (p[len - 2] == '.' && p[len - 1] == 'S')
                   1900:        deps_output (p, len - 2);
                   1901:       else if (p[len - 2] == '.' && p[len - 1] == 'm')
                   1902:        deps_output (p, len - 2);
                   1903:       else
                   1904:        deps_output (p, 0);
                   1905:       /* Supply our own suffix.  */
1.1.1.4   root     1906: #ifndef VMS
1.1       root     1907:       deps_output (".o : ", 0);
1.1.1.4   root     1908: #else
                   1909:       deps_output (".obj : ", 0);
                   1910: #endif
1.1       root     1911:       deps_output (in_fname, 0);
                   1912:       deps_output (" ", 0);
                   1913:     }
                   1914:   }
                   1915: 
                   1916:   file_size_and_mode (f, &st_mode, &st_size);
                   1917:   fp->nominal_fname = fp->fname = in_fname;
                   1918:   fp->lineno = 1;
                   1919:   fp->system_header_p = 0;
                   1920:   /* JF all this is mine about reading pipes and ttys */
                   1921:   if (! S_ISREG (st_mode)) {
                   1922:     /* Read input from a file that is not a normal disk file.
                   1923:        We cannot preallocate a buffer with the correct size,
                   1924:        so we must read in the file a piece at the time and make it bigger.  */
                   1925:     int size;
                   1926:     int bsize;
                   1927:     int cnt;
                   1928:     U_CHAR *bufp;
                   1929: 
                   1930:     bsize = 2000;
                   1931:     size = 0;
                   1932:     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
                   1933:     bufp = fp->buf;
                   1934:     for (;;) {
1.1.1.6 ! root     1935:       cnt = safe_read (f, bufp, bsize - size);
1.1       root     1936:       if (cnt < 0) goto perror;        /* error! */
                   1937:       if (cnt == 0) break;     /* End of file */
                   1938:       size += cnt;
                   1939:       bufp += cnt;
                   1940:       if (bsize == size) {     /* Buffer is full! */
                   1941:         bsize *= 2;
                   1942:         fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
                   1943:        bufp = fp->buf + size;  /* May have moved */
                   1944:       }
                   1945:     }
                   1946:     fp->length = size;
                   1947:   } else {
                   1948:     /* Read a file whose size we can determine in advance.
                   1949:        For the sake of VMS, st_size is just an upper bound.  */
                   1950:     long i;
                   1951:     fp->length = 0;
                   1952:     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
                   1953: 
                   1954:     while (st_size > 0) {
1.1.1.6 ! root     1955:       i = safe_read (f, fp->buf + fp->length, st_size);
1.1       root     1956:       if (i <= 0) {
                   1957:         if (i == 0) break;
                   1958:        goto perror;
                   1959:       }
                   1960:       fp->length += i;
                   1961:       st_size -= i;
                   1962:     }
                   1963:   }
                   1964:   fp->bufp = fp->buf;
                   1965:   fp->if_stack = if_stack;
                   1966: 
                   1967:   /* Make sure data ends with a newline.  And put a null after it.  */
                   1968: 
                   1969:   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
                   1970:       /* Backslash-newline at end is not good enough.  */
                   1971:       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
                   1972:     fp->buf[fp->length++] = '\n';
                   1973:     missing_newline = 1;
                   1974:   }
                   1975:   fp->buf[fp->length] = '\0';
                   1976: 
                   1977:   /* Unless inhibited, convert trigraphs in the input.  */
                   1978: 
                   1979:   if (!no_trigraphs)
                   1980:     trigraph_pcp (fp);
                   1981: 
                   1982:   /* Now that we know the input file is valid, open the output.  */
                   1983: 
                   1984:   if (!out_fname || !strcmp (out_fname, ""))
                   1985:     out_fname = "stdout";
                   1986:   else if (! freopen (out_fname, "w", stdout))
                   1987:     pfatal_with_name (out_fname);
                   1988: 
                   1989:   output_line_command (fp, &outbuf, 0, same_file);
                   1990: 
                   1991:   /* Scan the -include files before the main input.  */
                   1992: 
                   1993:   for (i = 1; i < argc; i++)
                   1994:     if (pend_includes[i]) {
                   1995:       int fd = open (pend_includes[i], O_RDONLY, 0666);
                   1996:       if (fd < 0) {
                   1997:        perror_with_name (pend_includes[i]);
                   1998:        return FAILURE_EXIT_CODE;
                   1999:       }
1.1.1.4   root     2000:       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
1.1       root     2001:     }
                   2002: 
                   2003:   /* Scan the input, processing macros and directives.  */
                   2004: 
                   2005:   rescan (&outbuf, 0);
                   2006: 
1.1.1.5   root     2007:   if (missing_newline)
                   2008:     fp->lineno--;
                   2009: 
1.1       root     2010:   if (pedantic && missing_newline)
                   2011:     pedwarn ("file does not end in newline");
                   2012: 
                   2013:   /* Now we have processed the entire input
                   2014:      Write whichever kind of output has been requested.  */
                   2015: 
                   2016:   if (dump_macros == dump_only)
                   2017:     dump_all_macros ();
                   2018:   else if (! inhibit_output) {
                   2019:     write_output ();
                   2020:   }
                   2021: 
                   2022:   if (print_deps) {
1.1.1.4   root     2023:     /* Don't actually write the deps file if compilation has failed.  */
                   2024:     if (errors == 0) {
1.1.1.5   root     2025:       if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
1.1.1.4   root     2026:        pfatal_with_name (deps_file);
1.1       root     2027:       fputs (deps_buffer, deps_stream);
                   2028:       putc ('\n', deps_stream);
1.1.1.4   root     2029:       if (deps_file) {
                   2030:        if (ferror (deps_stream) || fclose (deps_stream) != 0)
1.1       root     2031:          fatal ("I/O error on output");
                   2032:       }
                   2033:     }
                   2034:   }
                   2035: 
1.1.1.5   root     2036:   if (pcp_outfile && pcp_outfile != stdout
                   2037:       && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
                   2038:     fatal ("I/O error on `-pcp' output");
                   2039: 
1.1.1.4   root     2040:   if (ferror (stdout) || fclose (stdout) != 0)
1.1       root     2041:     fatal ("I/O error on output");
                   2042: 
                   2043:   if (errors)
                   2044:     exit (FAILURE_EXIT_CODE);
                   2045:   exit (SUCCESS_EXIT_CODE);
                   2046: 
                   2047:  perror:
                   2048:   pfatal_with_name (in_fname);
                   2049:   return 0;
                   2050: }
                   2051: 
                   2052: /* Given a colon-separated list of file names PATH,
                   2053:    add all the names to the search path for include files.  */
                   2054: 
                   2055: static void
                   2056: path_include (path)
                   2057:      char *path;
                   2058: {
                   2059:   char *p;
                   2060: 
                   2061:   p = path;
                   2062: 
                   2063:   if (*p)
                   2064:     while (1) {
                   2065:       char *q = p;
                   2066:       char *name;
                   2067:       struct file_name_list *dirtmp;
                   2068: 
                   2069:       /* Find the end of this name.  */
1.1.1.3   root     2070:       while (*q != 0 && *q != PATH_SEPARATOR) q++;
1.1       root     2071:       if (p == q) {
                   2072:        /* An empty name in the path stands for the current directory.  */
                   2073:        name = (char *) xmalloc (2);
                   2074:        name[0] = '.';
                   2075:        name[1] = 0;
                   2076:       } else {
                   2077:        /* Otherwise use the directory that is named.  */
                   2078:        name = (char *) xmalloc (q - p + 1);
                   2079:        bcopy (p, name, q - p);
                   2080:        name[q - p] = 0;
                   2081:       }
                   2082: 
                   2083:       dirtmp = (struct file_name_list *)
                   2084:        xmalloc (sizeof (struct file_name_list));
                   2085:       dirtmp->next = 0;                /* New one goes on the end */
                   2086:       dirtmp->control_macro = 0;
1.1.1.6 ! root     2087:       dirtmp->c_system_include_path = 0;
1.1       root     2088:       dirtmp->fname = name;
1.1.1.4   root     2089:       append_include_chain (dirtmp, dirtmp);
1.1       root     2090: 
                   2091:       /* Advance past this name.  */
                   2092:       p = q;
                   2093:       if (*p == 0)
                   2094:        break;
                   2095:       /* Skip the colon.  */
                   2096:       p++;
                   2097:     }
                   2098: }
                   2099: 
                   2100: /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
                   2101:    before main CCCP processing.  Name `pcp' is also in honor of the
                   2102:    drugs the trigraph designers must have been on.
                   2103: 
                   2104:    Using an extra pass through the buffer takes a little extra time,
                   2105:    but is infinitely less hairy than trying to handle trigraphs inside
                   2106:    strings, etc. everywhere, and also makes sure that trigraphs are
                   2107:    only translated in the top level of processing. */
                   2108: 
                   2109: static void
                   2110: trigraph_pcp (buf)
                   2111:      FILE_BUF *buf;
                   2112: {
                   2113:   register U_CHAR c, *fptr, *bptr, *sptr;
                   2114:   int len;
                   2115: 
                   2116:   fptr = bptr = sptr = buf->buf;
                   2117:   while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
                   2118:     if (*++sptr != '?')
                   2119:       continue;
                   2120:     switch (*++sptr) {
                   2121:       case '=':
                   2122:       c = '#';
                   2123:       break;
                   2124:     case '(':
                   2125:       c = '[';
                   2126:       break;
                   2127:     case '/':
                   2128:       c = '\\';
                   2129:       break;
                   2130:     case ')':
                   2131:       c = ']';
                   2132:       break;
                   2133:     case '\'':
                   2134:       c = '^';
                   2135:       break;
                   2136:     case '<':
                   2137:       c = '{';
                   2138:       break;
                   2139:     case '!':
                   2140:       c = '|';
                   2141:       break;
                   2142:     case '>':
                   2143:       c = '}';
                   2144:       break;
                   2145:     case '-':
                   2146:       c  = '~';
                   2147:       break;
                   2148:     case '?':
                   2149:       sptr--;
                   2150:       continue;
                   2151:     default:
                   2152:       continue;
                   2153:     }
                   2154:     len = sptr - fptr - 2;
                   2155:     if (bptr != fptr && len > 0)
                   2156:       bcopy (fptr, bptr, len); /* BSD doc says bcopy () works right
                   2157:                                   for overlapping strings.  In ANSI
                   2158:                                   C, this will be memmove (). */
                   2159:     bptr += len;
                   2160:     *bptr++ = c;
                   2161:     fptr = ++sptr;
                   2162:   }
                   2163:   len = buf->length - (fptr - buf->buf);
                   2164:   if (bptr != fptr && len > 0)
                   2165:     bcopy (fptr, bptr, len);
                   2166:   buf->length -= fptr - bptr;
                   2167:   buf->buf[buf->length] = '\0';
                   2168:   if (warn_trigraphs && fptr != bptr)
                   2169:     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
                   2170: }
                   2171: 
                   2172: /* Move all backslash-newline pairs out of embarrassing places.
                   2173:    Exchange all such pairs following BP
1.1.1.2   root     2174:    with any potentially-embarrassing characters that follow them.
1.1       root     2175:    Potentially-embarrassing characters are / and *
                   2176:    (because a backslash-newline inside a comment delimiter
                   2177:    would cause it not to be recognized).  */
                   2178: 
                   2179: static void
                   2180: newline_fix (bp)
                   2181:      U_CHAR *bp;
                   2182: {
                   2183:   register U_CHAR *p = bp;
                   2184:   register int count = 0;
                   2185: 
                   2186:   /* First count the backslash-newline pairs here.  */
                   2187: 
                   2188:   while (1) {
                   2189:     if (p[0] == '\\') {
                   2190:       if (p[1] == '\n')
                   2191:        p += 2, count++;
                   2192:       else if (p[1] == '\r' && p[2] == '\n')
                   2193:        p += 3, count++;
                   2194:       else
                   2195:        break;
                   2196:     } else
                   2197:       break;
                   2198:   }
                   2199: 
                   2200:   /* What follows the backslash-newlines is not embarrassing.  */
                   2201: 
                   2202:   if (count == 0 || (*p != '/' && *p != '*'))
                   2203:     return;
                   2204: 
                   2205:   /* Copy all potentially embarrassing characters
                   2206:      that follow the backslash-newline pairs
                   2207:      down to where the pairs originally started.  */
                   2208: 
                   2209:   while (*p == '*' || *p == '/')
                   2210:     *bp++ = *p++;
                   2211: 
                   2212:   /* Now write the same number of pairs after the embarrassing chars.  */
                   2213:   while (count-- > 0) {
                   2214:     *bp++ = '\\';
                   2215:     *bp++ = '\n';
                   2216:   }
                   2217: }
                   2218: 
                   2219: /* Like newline_fix but for use within a directive-name.
                   2220:    Move any backslash-newlines up past any following symbol constituents.  */
                   2221: 
                   2222: static void
                   2223: name_newline_fix (bp)
                   2224:      U_CHAR *bp;
                   2225: {
                   2226:   register U_CHAR *p = bp;
                   2227:   register int count = 0;
                   2228: 
                   2229:   /* First count the backslash-newline pairs here.  */
                   2230:   while (1) {
                   2231:     if (p[0] == '\\') {
                   2232:       if (p[1] == '\n')
                   2233:        p += 2, count++;
                   2234:       else if (p[1] == '\r' && p[2] == '\n')
                   2235:        p += 3, count++;
                   2236:       else
                   2237:        break;
                   2238:     } else
                   2239:       break;
                   2240:   }
                   2241: 
                   2242:   /* What follows the backslash-newlines is not embarrassing.  */
                   2243: 
                   2244:   if (count == 0 || !is_idchar[*p])
                   2245:     return;
                   2246: 
                   2247:   /* Copy all potentially embarrassing characters
                   2248:      that follow the backslash-newline pairs
                   2249:      down to where the pairs originally started.  */
                   2250: 
                   2251:   while (is_idchar[*p])
                   2252:     *bp++ = *p++;
                   2253: 
                   2254:   /* Now write the same number of pairs after the embarrassing chars.  */
                   2255:   while (count-- > 0) {
                   2256:     *bp++ = '\\';
                   2257:     *bp++ = '\n';
                   2258:   }
                   2259: }
                   2260: 
                   2261: /* Look for lint commands in comments.
                   2262: 
                   2263:    When we come in here, ibp points into a comment.  Limit is as one expects.
                   2264:    scan within the comment -- it should start, after lwsp, with a lint command.
                   2265:    If so that command is returned as a (constant) string.
                   2266: 
                   2267:    Upon return, any arg will be pointed to with argstart and will be
                   2268:    arglen long.  Note that we don't parse that arg since it will just
                   2269:    be printed out again.
                   2270: */
                   2271: 
                   2272: static char *
                   2273: get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
                   2274:      register U_CHAR *ibp;
                   2275:      register U_CHAR *limit;
                   2276:      U_CHAR **argstart;                /* point to command arg */
                   2277:      int *arglen, *cmdlen;     /* how long they are */
                   2278: {
                   2279:   long linsize;
                   2280:   register U_CHAR *numptr;     /* temp for arg parsing */
                   2281: 
                   2282:   *arglen = 0;
                   2283: 
                   2284:   SKIP_WHITE_SPACE (ibp);
                   2285: 
                   2286:   if (ibp >= limit) return NULL;
                   2287: 
                   2288:   linsize = limit - ibp;
                   2289:   
                   2290:   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
                   2291:   if ((linsize >= 10) && !strncmp (ibp, "NOTREACHED", 10)) {
                   2292:     *cmdlen = 10;
                   2293:     return "NOTREACHED";
                   2294:   }
                   2295:   if ((linsize >= 8) && !strncmp (ibp, "ARGSUSED", 8)) {
                   2296:     *cmdlen = 8;
                   2297:     return "ARGSUSED";
                   2298:   }
1.1.1.4   root     2299:   if ((linsize >= 11) && !strncmp (ibp, "LINTLIBRARY", 11)) {
                   2300:     *cmdlen = 11;
1.1       root     2301:     return "LINTLIBRARY";
                   2302:   }
                   2303:   if ((linsize >= 7) && !strncmp (ibp, "VARARGS", 7)) {
                   2304:     *cmdlen = 7;
                   2305:     ibp += 7; linsize -= 7;
                   2306:     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
                   2307: 
                   2308:     /* OK, read a number */
                   2309:     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
                   2310:         numptr++);
                   2311:     *arglen = numptr - *argstart;
                   2312:     return "VARARGS";
                   2313:   }
                   2314:   return NULL;
                   2315: }
                   2316: 
                   2317: /*
                   2318:  * The main loop of the program.
                   2319:  *
                   2320:  * Read characters from the input stack, transferring them to the
                   2321:  * output buffer OP.
                   2322:  *
                   2323:  * Macros are expanded and push levels on the input stack.
                   2324:  * At the end of such a level it is popped off and we keep reading.
                   2325:  * At the end of any other kind of level, we return.
                   2326:  * #-directives are handled, except within macros.
                   2327:  *
                   2328:  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
                   2329:  * and insert them when appropriate.  This is set while scanning macro
                   2330:  * arguments before substitution.  It is zero when scanning for final output.
                   2331:  *   There are three types of Newline markers:
                   2332:  *   * Newline -  follows a macro name that was not expanded
                   2333:  *     because it appeared inside an expansion of the same macro.
                   2334:  *     This marker prevents future expansion of that identifier.
                   2335:  *     When the input is rescanned into the final output, these are deleted.
                   2336:  *     These are also deleted by ## concatenation.
                   2337:  *   * Newline Space (or Newline and any other whitespace character)
                   2338:  *     stands for a place that tokens must be separated or whitespace
                   2339:  *     is otherwise desirable, but where the ANSI standard specifies there
                   2340:  *     is no whitespace.  This marker turns into a Space (or whichever other
                   2341:  *     whitespace char appears in the marker) in the final output,
                   2342:  *     but it turns into nothing in an argument that is stringified with #.
                   2343:  *     Such stringified arguments are the only place where the ANSI standard
                   2344:  *     specifies with precision that whitespace may not appear.
                   2345:  *
                   2346:  * During this function, IP->bufp is kept cached in IBP for speed of access.
                   2347:  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
                   2348:  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
                   2349:  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
                   2350:  * explicitly, and before RECACHE, since RECACHE uses OBP.
                   2351:  */
                   2352: 
                   2353: static void
                   2354: rescan (op, output_marks)
                   2355:      FILE_BUF *op;
                   2356:      int output_marks;
                   2357: {
                   2358:   /* Character being scanned in main loop.  */
                   2359:   register U_CHAR c;
                   2360: 
                   2361:   /* Length of pending accumulated identifier.  */
                   2362:   register int ident_length = 0;
                   2363: 
                   2364:   /* Hash code of pending accumulated identifier.  */
                   2365:   register int hash = 0;
                   2366: 
                   2367:   /* Current input level (&instack[indepth]).  */
                   2368:   FILE_BUF *ip;
                   2369: 
                   2370:   /* Pointer for scanning input.  */
                   2371:   register U_CHAR *ibp;
                   2372: 
                   2373:   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
                   2374:   register U_CHAR *limit;
                   2375: 
                   2376:   /* Pointer for storing output.  */
                   2377:   register U_CHAR *obp;
                   2378: 
                   2379:   /* REDO_CHAR is nonzero if we are processing an identifier
                   2380:      after backing up over the terminating character.
                   2381:      Sometimes we process an identifier without backing up over
                   2382:      the terminating character, if the terminating character
                   2383:      is not special.  Backing up is done so that the terminating character
                   2384:      will be dispatched on again once the identifier is dealt with.  */
                   2385:   int redo_char = 0;
                   2386: 
                   2387:   /* 1 if within an identifier inside of which a concatenation
                   2388:      marker (Newline -) has been seen.  */
                   2389:   int concatenated = 0;
                   2390: 
                   2391:   /* While scanning a comment or a string constant,
                   2392:      this records the line it started on, for error messages.  */
                   2393:   int start_line;
                   2394: 
                   2395:   /* Record position of last `real' newline.  */
                   2396:   U_CHAR *beg_of_line;
                   2397: 
                   2398: /* Pop the innermost input stack level, assuming it is a macro expansion.  */
                   2399: 
                   2400: #define POPMACRO \
                   2401: do { ip->macro->type = T_MACRO;                \
                   2402:      if (ip->free_ptr) free (ip->free_ptr);    \
                   2403:      --indepth; } while (0)
                   2404: 
                   2405: /* Reload `rescan's local variables that describe the current
                   2406:    level of the input stack.  */
                   2407: 
                   2408: #define RECACHE  \
                   2409: do { ip = &instack[indepth];           \
                   2410:      ibp = ip->bufp;                   \
                   2411:      limit = ip->buf + ip->length;     \
                   2412:      op->bufp = obp;                   \
                   2413:      check_expand (op, limit - ibp);   \
                   2414:      beg_of_line = 0;                  \
                   2415:      obp = op->bufp; } while (0)
                   2416: 
                   2417:   if (no_output && instack[indepth].fname != 0)
                   2418:     skip_if_group (&instack[indepth], 1);
                   2419: 
                   2420:   obp = op->bufp;
                   2421:   RECACHE;
1.1.1.4   root     2422: 
1.1       root     2423:   beg_of_line = ibp;
                   2424: 
                   2425:   /* Our caller must always put a null after the end of
                   2426:      the input at each input stack level.  */
                   2427:   if (*limit != 0)
                   2428:     abort ();
                   2429: 
                   2430:   while (1) {
                   2431:     c = *ibp++;
                   2432:     *obp++ = c;
                   2433: 
                   2434:     switch (c) {
                   2435:     case '\\':
                   2436:       if (ibp >= limit)
                   2437:        break;
                   2438:       if (*ibp == '\n') {
                   2439:        /* Always merge lines ending with backslash-newline,
                   2440:           even in middle of identifier.  */
                   2441:        ++ibp;
                   2442:        ++ip->lineno;
                   2443:        --obp;          /* remove backslash from obuf */
                   2444:        break;
                   2445:       }
                   2446:       /* Otherwise, backslash suppresses specialness of following char,
                   2447:         so copy it here to prevent the switch from seeing it.
                   2448:         But first get any pending identifier processed.  */
                   2449:       if (ident_length > 0)
                   2450:        goto specialchar;
                   2451:       *obp++ = *ibp++;
                   2452:       break;
                   2453: 
                   2454:     case '#':
                   2455:       if (assertions_flag) {
                   2456:        /* Copy #foo (bar lose) without macro expansion.  */
                   2457:        SKIP_WHITE_SPACE (ibp);
                   2458:        while (is_idchar[*ibp])
                   2459:          *obp++ = *ibp++;
                   2460:        SKIP_WHITE_SPACE (ibp);
                   2461:        if (*ibp == '(') {
                   2462:          ip->bufp = ibp;
                   2463:          skip_paren_group (ip);
                   2464:          bcopy (ibp, obp, ip->bufp - ibp);
                   2465:          obp += ip->bufp - ibp;
                   2466:          ibp = ip->bufp;
                   2467:        }
                   2468:       }
                   2469: 
                   2470:       /* If this is expanding a macro definition, don't recognize
                   2471:         preprocessor directives.  */
                   2472:       if (ip->macro != 0)
                   2473:        goto randomchar;
1.1.1.4   root     2474:       /* If this is expand_into_temp_buffer, recognize them
                   2475:         only after an actual newline at this level,
                   2476:         not at the beginning of the input level.  */
                   2477:       if (ip->fname == 0 && beg_of_line == ip->buf)
                   2478:        goto randomchar;
1.1       root     2479:       if (ident_length)
                   2480:        goto specialchar;
                   2481: 
1.1.1.4   root     2482:       
1.1       root     2483:       /* # keyword: a # must be first nonblank char on the line */
                   2484:       if (beg_of_line == 0)
                   2485:        goto randomchar;
                   2486:       {
                   2487:        U_CHAR *bp;
                   2488: 
                   2489:        /* Scan from start of line, skipping whitespace, comments
                   2490:           and backslash-newlines, and see if we reach this #.
                   2491:           If not, this # is not special.  */
                   2492:        bp = beg_of_line;
1.1.1.4   root     2493:        /* If -traditional, require # to be at beginning of line.  */
                   2494:        if (!traditional)
                   2495:          while (1) {
                   2496:            if (is_hor_space[*bp])
1.1       root     2497:              bp++;
1.1.1.4   root     2498:            else if (*bp == '\\' && bp[1] == '\n')
                   2499:              bp += 2;
                   2500:            else if (*bp == '/' && bp[1] == '*') {
                   2501:              bp += 2;
                   2502:              while (!(*bp == '*' && bp[1] == '/'))
                   2503:                bp++;
                   2504:              bp += 2;
                   2505:            }
                   2506:            else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
                   2507:              bp += 2;
                   2508:              while (*bp++ != '\n') ;
                   2509:            }
                   2510:            else break;
1.1       root     2511:          }
                   2512:        if (bp + 1 != ibp)
                   2513:          goto randomchar;
                   2514:       }
                   2515: 
                   2516:       /* This # can start a directive.  */
                   2517: 
                   2518:       --obp;           /* Don't copy the '#' */
                   2519: 
                   2520:       ip->bufp = ibp;
                   2521:       op->bufp = obp;
                   2522:       if (! handle_directive (ip, op)) {
                   2523: #ifdef USE_C_ALLOCA
                   2524:        alloca (0);
                   2525: #endif
                   2526:        /* Not a known directive: treat it as ordinary text.
                   2527:           IP, OP, IBP, etc. have not been changed.  */
                   2528:        if (no_output && instack[indepth].fname) {
                   2529:          /* If not generating expanded output,
                   2530:             what we do with ordinary text is skip it.
                   2531:             Discard everything until next # directive.  */
                   2532:          skip_if_group (&instack[indepth], 1);
                   2533:          RECACHE;
                   2534:          beg_of_line = ibp;
                   2535:          break;
                   2536:        }
                   2537:        ++obp;          /* Copy the '#' after all */
1.1.1.6 ! root     2538:        /* Don't expand an identifier that could be a macro directive.
        !          2539:           (Section 3.8.3 of the ANSI C standard)                       */
        !          2540:        SKIP_WHITE_SPACE (ibp);
        !          2541:        if (is_idstart[*ibp])
        !          2542:          {
        !          2543:            *obp++ = *ibp++;
        !          2544:            while (is_idchar[*ibp])
        !          2545:              *obp++ = *ibp++;
        !          2546:          }
1.1       root     2547:        goto randomchar;
                   2548:       }
                   2549: #ifdef USE_C_ALLOCA
                   2550:       alloca (0);
                   2551: #endif
                   2552:       /* A # directive has been successfully processed.  */
                   2553:       /* If not generating expanded output, ignore everything until
                   2554:         next # directive.  */
                   2555:       if (no_output && instack[indepth].fname)
                   2556:        skip_if_group (&instack[indepth], 1);
                   2557:       obp = op->bufp;
                   2558:       RECACHE;
                   2559:       beg_of_line = ibp;
                   2560:       break;
                   2561: 
                   2562:     case '\"':                 /* skip quoted string */
                   2563:     case '\'':
                   2564:       /* A single quoted string is treated like a double -- some
                   2565:         programs (e.g., troff) are perverse this way */
                   2566: 
                   2567:       if (ident_length)
                   2568:        goto specialchar;
                   2569: 
                   2570:       start_line = ip->lineno;
                   2571: 
                   2572:       /* Skip ahead to a matching quote.  */
                   2573: 
                   2574:       while (1) {
                   2575:        if (ibp >= limit) {
1.1.1.4   root     2576:          if (ip->macro != 0) {
                   2577:            /* try harder: this string crosses a macro expansion boundary.
                   2578:               This can happen naturally if -traditional.
                   2579:               Otherwise, only -D can make a macro with an unmatched quote.  */
                   2580:            POPMACRO;
                   2581:            RECACHE;
                   2582:            continue;
                   2583:          }
                   2584:          if (!traditional) {
1.1       root     2585:            error_with_line (line_for_error (start_line),
                   2586:                             "unterminated string or character constant");
                   2587:            error_with_line (multiline_string_line,
                   2588:                             "possible real start of unterminated constant");
                   2589:            multiline_string_line = 0;
                   2590:          }
                   2591:          break;
                   2592:        }
                   2593:        *obp++ = *ibp;
                   2594:        switch (*ibp++) {
                   2595:        case '\n':
                   2596:          ++ip->lineno;
                   2597:          ++op->lineno;
                   2598:          /* Traditionally, end of line ends a string constant with no error.
                   2599:             So exit the loop and record the new line.  */
                   2600:          if (traditional) {
                   2601:            beg_of_line = ibp;
                   2602:            goto while2end;
                   2603:          }
1.1.1.5   root     2604:          if (c == '\'') {
1.1       root     2605:            error_with_line (line_for_error (start_line),
1.1.1.5   root     2606:                             "unterminated character constant");
1.1       root     2607:            goto while2end;
                   2608:          }
1.1.1.5   root     2609:          if (pedantic && multiline_string_line == 0) {
                   2610:            pedwarn_with_line (line_for_error (start_line),
                   2611:                               "string constant runs past end of line");
                   2612:          }
1.1       root     2613:          if (multiline_string_line == 0)
                   2614:            multiline_string_line = ip->lineno - 1;
                   2615:          break;
                   2616: 
                   2617:        case '\\':
                   2618:          if (ibp >= limit)
                   2619:            break;
                   2620:          if (*ibp == '\n') {
                   2621:            /* Backslash newline is replaced by nothing at all,
                   2622:               but keep the line counts correct.  */
                   2623:            --obp;
                   2624:            ++ibp;
                   2625:            ++ip->lineno;
                   2626:          } else {
                   2627:            /* ANSI stupidly requires that in \\ the second \
                   2628:               is *not* prevented from combining with a newline.  */
                   2629:            while (*ibp == '\\' && ibp[1] == '\n') {
                   2630:              ibp += 2;
                   2631:              ++ip->lineno;
                   2632:            }
                   2633:            *obp++ = *ibp++;
                   2634:          }
                   2635:          break;
                   2636: 
                   2637:        case '\"':
                   2638:        case '\'':
                   2639:          if (ibp[-1] == c)
                   2640:            goto while2end;
                   2641:          break;
                   2642:        }
                   2643:       }
                   2644:     while2end:
                   2645:       break;
                   2646: 
                   2647:     case '/':
                   2648:       if (*ibp == '\\' && ibp[1] == '\n')
                   2649:        newline_fix (ibp);
                   2650: 
                   2651:       if (*ibp != '*'
1.1.1.4   root     2652:          && !(cplusplus_comments && *ibp == '/'))
1.1       root     2653:        goto randomchar;
                   2654:       if (ip->macro != 0)
                   2655:        goto randomchar;
                   2656:       if (ident_length)
                   2657:        goto specialchar;
                   2658: 
                   2659:       if (*ibp == '/') {
                   2660:        /* C++ style comment... */
                   2661:        start_line = ip->lineno;
                   2662: 
                   2663:        --ibp;                  /* Back over the slash */
                   2664:        --obp;
                   2665: 
                   2666:        /* Comments are equivalent to spaces. */
                   2667:        if (! put_out_comments)
                   2668:          *obp++ = ' ';
                   2669:        else {
                   2670:          /* must fake up a comment here */
                   2671:          *obp++ = '/';
                   2672:          *obp++ = '/';
                   2673:        }
                   2674:        {
                   2675:          U_CHAR *before_bp = ibp+2;
                   2676: 
                   2677:          while (ibp < limit) {
                   2678:            if (*ibp++ == '\n') {
                   2679:              ibp--;
                   2680:              if (put_out_comments) {
                   2681:                bcopy (before_bp, obp, ibp - before_bp);
                   2682:                obp += ibp - before_bp;
                   2683:              }
                   2684:              break;
                   2685:            }
                   2686:          }
                   2687:          break;
                   2688:        }
                   2689:       }
                   2690: 
                   2691:       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
                   2692: 
                   2693:       start_line = ip->lineno;
                   2694: 
                   2695:       ++ibp;                   /* Skip the star. */
                   2696: 
                   2697:       /* If this cpp is for lint, we peek inside the comments: */
                   2698:       if (lint) {
                   2699:        U_CHAR *argbp;
                   2700:        int cmdlen, arglen;
                   2701:        char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
                   2702: 
                   2703:        if (lintcmd != NULL) {
                   2704:          /* I believe it is always safe to emit this newline: */
                   2705:          obp[-1] = '\n';
                   2706:          bcopy ("#pragma lint ", obp, 13);
                   2707:          obp += 13;
                   2708:          bcopy (lintcmd, obp, cmdlen);
                   2709:          obp += cmdlen;
                   2710: 
                   2711:          if (arglen != 0) {
                   2712:            *(obp++) = ' ';
                   2713:            bcopy (argbp, obp, arglen);
                   2714:            obp += arglen;
                   2715:          }
                   2716: 
                   2717:          /* OK, now bring us back to the state we were in before we entered
                   2718:             this branch.  We need #line b/c the newline for the pragma
                   2719:             could fuck things up. */
                   2720:          output_line_command (ip, op, 0, same_file);
                   2721:          *(obp++) = ' ';       /* just in case, if comments are copied thru */
                   2722:          *(obp++) = '/';
                   2723:        }
                   2724:       }
                   2725: 
                   2726:       /* Comments are equivalent to spaces.
                   2727:         Note that we already output the slash; we might not want it.
                   2728:         For -traditional, a comment is equivalent to nothing.  */
                   2729:       if (! put_out_comments) {
                   2730:        if (traditional)
                   2731:          obp--;
                   2732:        else
                   2733:          obp[-1] = ' ';
                   2734:       }
                   2735:       else
                   2736:        *obp++ = '*';
                   2737: 
                   2738:       {
                   2739:        U_CHAR *before_bp = ibp;
                   2740: 
                   2741:        while (ibp < limit) {
                   2742:          switch (*ibp++) {
                   2743:          case '/':
                   2744:            if (warn_comments && ibp < limit && *ibp == '*')
1.1.1.5   root     2745:              warning ("`/*' within comment");
1.1       root     2746:            break;
                   2747:          case '*':
                   2748:            if (*ibp == '\\' && ibp[1] == '\n')
                   2749:              newline_fix (ibp);
                   2750:            if (ibp >= limit || *ibp == '/')
                   2751:              goto comment_end;
                   2752:            break;
                   2753:          case '\n':
                   2754:            ++ip->lineno;
                   2755:            /* Copy the newline into the output buffer, in order to
                   2756:               avoid the pain of a #line every time a multiline comment
                   2757:               is seen.  */
                   2758:            if (!put_out_comments)
                   2759:              *obp++ = '\n';
                   2760:            ++op->lineno;
                   2761:          }
                   2762:        }
                   2763:       comment_end:
                   2764: 
                   2765:        if (ibp >= limit)
                   2766:          error_with_line (line_for_error (start_line),
                   2767:                           "unterminated comment");
                   2768:        else {
                   2769:          ibp++;
                   2770:          if (put_out_comments) {
                   2771:            bcopy (before_bp, obp, ibp - before_bp);
                   2772:            obp += ibp - before_bp;
                   2773:          }
                   2774:        }
                   2775:       }
                   2776:       break;
                   2777: 
                   2778:     case '$':
                   2779:       if (!dollars_in_ident)
                   2780:        goto randomchar;
                   2781:       goto letter;
                   2782: 
                   2783:     case '0': case '1': case '2': case '3': case '4':
                   2784:     case '5': case '6': case '7': case '8': case '9':
                   2785:       /* If digit is not part of identifier, it starts a number,
                   2786:         which means that following letters are not an identifier.
                   2787:         "0x5" does not refer to an identifier "x5".
                   2788:         So copy all alphanumerics that follow without accumulating
                   2789:         as an identifier.  Periods also, for sake of "3.e7".  */
                   2790: 
                   2791:       if (ident_length == 0) {
                   2792:        while (ibp < limit) {
                   2793:          while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
                   2794:            ++ip->lineno;
                   2795:            ibp += 2;
                   2796:          }
                   2797:          c = *ibp++;
                   2798:          /* ".." terminates a preprocessing number.  This is useless for C
                   2799:             code but useful for preprocessing other things.  */
                   2800:          if (!isalnum (c) && (c != '.' || *ibp == '.') && c != '_') {
                   2801:            --ibp;
                   2802:            break;
                   2803:          }
                   2804:          *obp++ = c;
                   2805:          /* A sign can be part of a preprocessing number
                   2806:             if it follows an e.  */
                   2807:          if (c == 'e' || c == 'E') {
                   2808:            while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
                   2809:              ++ip->lineno;
                   2810:              ibp += 2;
                   2811:            }
                   2812:            if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
                   2813:              *obp++ = *ibp++;
                   2814:              /* But traditional C does not let the token go past the sign.  */
                   2815:              if (traditional)
                   2816:                break;
                   2817:            }
                   2818:          }
                   2819:        }
                   2820:        break;
                   2821:       }
                   2822:       /* fall through */
                   2823: 
                   2824:     case '_':
                   2825:     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
                   2826:     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
                   2827:     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
                   2828:     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
                   2829:     case 'y': case 'z':
                   2830:     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
                   2831:     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
                   2832:     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
                   2833:     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
                   2834:     case 'Y': case 'Z':
                   2835:     letter:
                   2836:       ident_length++;
                   2837:       /* Compute step of hash function, to avoid a proc call on every token */
                   2838:       hash = HASHSTEP (hash, c);
                   2839:       break;
                   2840: 
                   2841:     case '\n':
1.1.1.5   root     2842:       if (ip->fname == 0 && *ibp == '-') {
                   2843:        /* Newline - inhibits expansion of preceding token.
                   2844:           If expanding a macro arg, we keep the newline -.
                   2845:           In final output, it is deleted.
                   2846:           We recognize Newline - in macro bodies and macro args.  */
                   2847:        if (! concatenated) {
                   2848:          ident_length = 0;
                   2849:          hash = 0;
                   2850:        }
                   2851:        ibp++;
                   2852:        if (!output_marks) {
                   2853:          obp--;
                   2854:        } else {
                   2855:          /* If expanding a macro arg, keep the newline -.  */
                   2856:          *obp++ = '-';
                   2857:        }
                   2858:        break;
                   2859:       }
                   2860: 
1.1       root     2861:       /* If reprocessing a macro expansion, newline is a special marker.  */
1.1.1.5   root     2862:       else if (ip->macro != 0) {
1.1       root     2863:        /* Newline White is a "funny space" to separate tokens that are
                   2864:           supposed to be separate but without space between.
                   2865:           Here White means any whitespace character.
                   2866:           Newline - marks a recursive macro use that is not
                   2867:           supposed to be expandable.  */
                   2868: 
1.1.1.5   root     2869:        if (is_space[*ibp]) {
1.1       root     2870:          /* Newline Space does not prevent expansion of preceding token
                   2871:             so expand the preceding token and then come back.  */
                   2872:          if (ident_length > 0)
                   2873:            goto specialchar;
                   2874: 
                   2875:          /* If generating final output, newline space makes a space.  */
                   2876:          if (!output_marks) {
                   2877:            obp[-1] = *ibp++;
                   2878:            /* And Newline Newline makes a newline, so count it.  */
                   2879:            if (obp[-1] == '\n')
                   2880:              op->lineno++;
                   2881:          } else {
                   2882:            /* If expanding a macro arg, keep the newline space.
                   2883:               If the arg gets stringified, newline space makes nothing.  */
                   2884:            *obp++ = *ibp++;
                   2885:          }
                   2886:        } else abort ();        /* Newline followed by something random?  */
                   2887:        break;
                   2888:       }
                   2889: 
                   2890:       /* If there is a pending identifier, handle it and come back here.  */
                   2891:       if (ident_length > 0)
                   2892:        goto specialchar;
                   2893: 
                   2894:       beg_of_line = ibp;
                   2895: 
                   2896:       /* Update the line counts and output a #line if necessary.  */
                   2897:       ++ip->lineno;
                   2898:       ++op->lineno;
                   2899:       if (ip->lineno != op->lineno) {
                   2900:        op->bufp = obp;
                   2901:        output_line_command (ip, op, 1, same_file);
                   2902:        check_expand (op, ip->length - (ip->bufp - ip->buf));
                   2903:        obp = op->bufp;
                   2904:       }
                   2905:       break;
                   2906: 
                   2907:       /* Come here either after (1) a null character that is part of the input
                   2908:         or (2) at the end of the input, because there is a null there.  */
                   2909:     case 0:
                   2910:       if (ibp <= limit)
                   2911:        /* Our input really contains a null character.  */
                   2912:        goto randomchar;
                   2913: 
                   2914:       /* At end of a macro-expansion level, pop it and read next level.  */
                   2915:       if (ip->macro != 0) {
                   2916:        obp--;
                   2917:        ibp--;
                   2918:        /* If traditional, and we have an identifier that ends here,
                   2919:           process it now, so we get the right error for recursion.  */
                   2920:        if (traditional && ident_length
                   2921:            && ! is_idchar[*instack[indepth - 1].bufp]) {
                   2922:          redo_char = 1;
                   2923:          goto randomchar;
                   2924:        }
                   2925:        POPMACRO;
                   2926:        RECACHE;
                   2927:        break;
                   2928:       }
                   2929: 
                   2930:       /* If we don't have a pending identifier,
                   2931:         return at end of input.  */
                   2932:       if (ident_length == 0) {
                   2933:        obp--;
                   2934:        ibp--;
                   2935:        op->bufp = obp;
                   2936:        ip->bufp = ibp;
                   2937:        goto ending;
                   2938:       }
                   2939: 
                   2940:       /* If we do have a pending identifier, just consider this null
                   2941:         a special character and arrange to dispatch on it again.
                   2942:         The second time, IDENT_LENGTH will be zero so we will return.  */
                   2943: 
                   2944:       /* Fall through */
                   2945: 
                   2946: specialchar:
                   2947: 
                   2948:       /* Handle the case of a character such as /, ', " or null
                   2949:         seen following an identifier.  Back over it so that
                   2950:         after the identifier is processed the special char
                   2951:         will be dispatched on again.  */
                   2952: 
                   2953:       ibp--;
                   2954:       obp--;
                   2955:       redo_char = 1;
                   2956: 
                   2957:     default:
                   2958: 
                   2959: randomchar:
                   2960: 
                   2961:       if (ident_length > 0) {
                   2962:        register HASHNODE *hp;
                   2963: 
                   2964:        /* We have just seen an identifier end.  If it's a macro, expand it.
                   2965: 
                   2966:           IDENT_LENGTH is the length of the identifier
                   2967:           and HASH is its hash code.
                   2968: 
                   2969:           The identifier has already been copied to the output,
                   2970:           so if it is a macro we must remove it.
                   2971: 
                   2972:           If REDO_CHAR is 0, the char that terminated the identifier
                   2973:           has been skipped in the output and the input.
                   2974:           OBP-IDENT_LENGTH-1 points to the identifier.
                   2975:           If the identifier is a macro, we must back over the terminator.
                   2976: 
                   2977:           If REDO_CHAR is 1, the terminating char has already been
                   2978:           backed over.  OBP-IDENT_LENGTH points to the identifier.  */
                   2979: 
                   2980:        if (!pcp_outfile || pcp_inside_if) {
                   2981: startagain:
                   2982:          for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
                   2983:               hp = hp->next) {
                   2984:            
                   2985:            if (hp->length == ident_length) {
                   2986:              int obufp_before_macroname;
                   2987:              int op_lineno_before_macroname;
                   2988:              register int i = ident_length;
                   2989:              register U_CHAR *p = hp->name;
                   2990:              register U_CHAR *q = obp - i;
                   2991:              int disabled;
                   2992:              
                   2993:              if (! redo_char)
                   2994:                q--;
                   2995:              
                   2996:              do {              /* All this to avoid a strncmp () */
                   2997:                if (*p++ != *q++)
                   2998:                  goto hashcollision;
                   2999:              } while (--i);
                   3000:              
                   3001:              /* We found a use of a macro name.
                   3002:                 see if the context shows it is a macro call.  */
                   3003:              
                   3004:              /* Back up over terminating character if not already done.  */
                   3005:              if (! redo_char) {
                   3006:                ibp--;
                   3007:                obp--;
                   3008:              }
                   3009:              
                   3010:              /* Save this as a displacement from the beginning of the output
                   3011:                 buffer.  We can not save this as a position in the output
                   3012:                 buffer, because it may get realloc'ed by RECACHE.  */
                   3013:              obufp_before_macroname = (obp - op->buf) - ident_length;
                   3014:              op_lineno_before_macroname = op->lineno;
                   3015:              
                   3016:              if (hp->type == T_PCSTRING) {
                   3017:                pcstring_used (hp); /* Mark the definition of this key
                   3018:                                       as needed, ensuring that it
                   3019:                                       will be output.  */
                   3020:                break;          /* Exit loop, since the key cannot have a
                   3021:                                   definition any longer.  */
                   3022:              }
                   3023: 
                   3024:              /* Record whether the macro is disabled.  */
                   3025:              disabled = hp->type == T_DISABLED;
                   3026:              
                   3027:              /* This looks like a macro ref, but if the macro was disabled,
                   3028:                 just copy its name and put in a marker if requested.  */
                   3029:              
                   3030:              if (disabled) {
                   3031: #if 0
                   3032:                /* This error check caught useful cases such as
1.1.1.5   root     3033:                   #define foo(x,y) bar (x (y,0), y)
                   3034:                   foo (foo, baz)  */
1.1       root     3035:                if (traditional)
                   3036:                  error ("recursive use of macro `%s'", hp->name);
                   3037: #endif
                   3038:                
                   3039:                if (output_marks) {
                   3040:                  check_expand (op, limit - ibp + 2);
                   3041:                  *obp++ = '\n';
                   3042:                  *obp++ = '-';
                   3043:                }
                   3044:                break;
                   3045:              }
                   3046:              
                   3047:              /* If macro wants an arglist, verify that a '(' follows.
                   3048:                 first skip all whitespace, copying it to the output
                   3049:                 after the macro name.  Then, if there is no '(',
                   3050:                 decide this is not a macro call and leave things that way.  */
                   3051:              if ((hp->type == T_MACRO || hp->type == T_DISABLED)
                   3052:                  && hp->value.defn->nargs >= 0)
                   3053:                {
                   3054:                  U_CHAR *old_ibp = ibp;
                   3055:                  U_CHAR *old_obp = obp;
                   3056:                  int old_iln = ip->lineno;
                   3057:                  int old_oln = op->lineno;
                   3058:                  
                   3059:                  while (1) {
                   3060:                    /* Scan forward over whitespace, copying it to the output.  */
                   3061:                    if (ibp == limit && ip->macro != 0) {
                   3062:                      POPMACRO;
                   3063:                      RECACHE;
                   3064:                      old_ibp = ibp;
                   3065:                      old_obp = obp;
                   3066:                      old_iln = ip->lineno;
                   3067:                      old_oln = op->lineno;
                   3068:                    }
                   3069:                    /* A comment: copy it unchanged or discard it.  */
                   3070:                    else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
                   3071:                      if (put_out_comments) {
                   3072:                        *obp++ = '/';
                   3073:                        *obp++ = '*';
                   3074:                      } else if (! traditional) {
                   3075:                        *obp++ = ' ';
                   3076:                      }
                   3077:                      ibp += 2;
                   3078:                      while (ibp + 1 != limit
                   3079:                             && !(ibp[0] == '*' && ibp[1] == '/')) {
                   3080:                        /* We need not worry about newline-marks,
                   3081:                           since they are never found in comments.  */
                   3082:                        if (*ibp == '\n') {
                   3083:                          /* Newline in a file.  Count it.  */
                   3084:                          ++ip->lineno;
                   3085:                          ++op->lineno;
                   3086:                        }
                   3087:                        if (put_out_comments)
                   3088:                          *obp++ = *ibp++;
                   3089:                        else
                   3090:                          ibp++;
                   3091:                      }
                   3092:                      ibp += 2;
                   3093:                      if (put_out_comments) {
                   3094:                        *obp++ = '*';
                   3095:                        *obp++ = '/';
                   3096:                      }
                   3097:                    }
                   3098:                    else if (is_space[*ibp]) {
                   3099:                      *obp++ = *ibp++;
                   3100:                      if (ibp[-1] == '\n') {
                   3101:                        if (ip->macro == 0) {
                   3102:                          /* Newline in a file.  Count it.  */
                   3103:                          ++ip->lineno;
                   3104:                          ++op->lineno;
                   3105:                        } else if (!output_marks) {
                   3106:                          /* A newline mark, and we don't want marks
                   3107:                             in the output.  If it is newline-hyphen,
                   3108:                             discard it entirely.  Otherwise, it is
                   3109:                             newline-whitechar, so keep the whitechar.  */
                   3110:                          obp--;
                   3111:                          if (*ibp == '-')
                   3112:                            ibp++;
                   3113:                          else {
                   3114:                            if (*ibp == '\n')
                   3115:                              ++op->lineno;
                   3116:                            *obp++ = *ibp++;
                   3117:                          }
                   3118:                        } else {
                   3119:                          /* A newline mark; copy both chars to the output.  */
                   3120:                          *obp++ = *ibp++;
                   3121:                        }
                   3122:                      }
                   3123:                    }
                   3124:                    else break;
                   3125:                  }
                   3126:                  if (*ibp != '(') {
                   3127:                    /* It isn't a macro call.
                   3128:                       Put back the space that we just skipped.  */
                   3129:                    ibp = old_ibp;
                   3130:                    obp = old_obp;
                   3131:                    ip->lineno = old_iln;
                   3132:                    op->lineno = old_oln;
                   3133:                    /* Exit the for loop.  */
                   3134:                    break;
                   3135:                  }
                   3136:                }
                   3137:              
                   3138:              /* This is now known to be a macro call.
                   3139:                 Discard the macro name from the output,
                   3140:                 along with any following whitespace just copied.  */
                   3141:              obp = op->buf + obufp_before_macroname;
                   3142:              op->lineno = op_lineno_before_macroname;
1.1.1.5   root     3143: 
                   3144:              /* Prevent accidental token-pasting with a character
                   3145:                 before the macro call.  */
                   3146:              if (!traditional && obp != op->buf
                   3147:                  && (obp[-1] == '-' || obp[1] == '+' || obp[1] == '&'
                   3148:                      || obp[-1] == '|' || obp[1] == '<' || obp[1] == '>')) {
                   3149:                /* If we are expanding a macro arg, make a newline marker
                   3150:                   to separate the tokens.  If we are making real output,
                   3151:                   a plain space will do.  */
                   3152:                if (output_marks)
                   3153:                  *obp++ = '\n';
                   3154:                *obp++ = ' ';
                   3155:              }
                   3156: 
1.1       root     3157:              /* Expand the macro, reading arguments as needed,
                   3158:                 and push the expansion on the input stack.  */
                   3159:              ip->bufp = ibp;
                   3160:              op->bufp = obp;
                   3161:              macroexpand (hp, op);
                   3162:              
                   3163:              /* Reexamine input stack, since macroexpand has pushed
                   3164:                 a new level on it.  */
                   3165:              obp = op->bufp;
                   3166:              RECACHE;
                   3167:              break;
                   3168:            }
                   3169: hashcollision:
                   3170:            ;
                   3171:          }                     /* End hash-table-search loop */
                   3172:        }
                   3173:        ident_length = hash = 0; /* Stop collecting identifier */
                   3174:        redo_char = 0;
                   3175:        concatenated = 0;
                   3176:       }                                /* End if (ident_length > 0) */
                   3177:     }                          /* End switch */
                   3178:   }                            /* End per-char loop */
                   3179: 
                   3180:   /* Come here to return -- but first give an error message
                   3181:      if there was an unterminated successful conditional.  */
                   3182:  ending:
                   3183:   if (if_stack != ip->if_stack) {
                   3184:     char *str;
                   3185:     switch (if_stack->type) {
                   3186:     case T_IF:
                   3187:       str = "if";
                   3188:       break;
                   3189:     case T_IFDEF:
                   3190:       str = "ifdef";
                   3191:       break;
                   3192:     case T_IFNDEF:
                   3193:       str = "ifndef";
                   3194:       break;
                   3195:     case T_ELSE:
                   3196:       str = "else";
                   3197:       break;
                   3198:     case T_ELIF:
                   3199:       str = "elif";
                   3200:       break;
                   3201:     }
                   3202:     error_with_line (line_for_error (if_stack->lineno),
                   3203:                     "unterminated `#%s' conditional", str);
                   3204:   }
                   3205:   if_stack = ip->if_stack;
                   3206: }
                   3207: 
                   3208: /*
                   3209:  * Rescan a string into a temporary buffer and return the result
                   3210:  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
                   3211:  *
                   3212:  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
                   3213:  * and insert such markers when appropriate.  See `rescan' for details.
                   3214:  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
                   3215:  * before substitution; it is 0 for other uses.
                   3216:  */
                   3217: static FILE_BUF
                   3218: expand_to_temp_buffer (buf, limit, output_marks, assertions)
                   3219:      U_CHAR *buf, *limit;
                   3220:      int output_marks, assertions;
                   3221: {
                   3222:   register FILE_BUF *ip;
                   3223:   FILE_BUF obuf;
                   3224:   int length = limit - buf;
                   3225:   U_CHAR *buf1;
                   3226:   int odepth = indepth;
                   3227:   int save_assertions_flag = assertions_flag;
                   3228: 
                   3229:   assertions_flag = assertions;
                   3230: 
                   3231:   if (length < 0)
                   3232:     abort ();
                   3233: 
                   3234:   /* Set up the input on the input stack.  */
                   3235: 
                   3236:   buf1 = (U_CHAR *) alloca (length + 1);
                   3237:   {
                   3238:     register U_CHAR *p1 = buf;
                   3239:     register U_CHAR *p2 = buf1;
                   3240: 
                   3241:     while (p1 != limit)
                   3242:       *p2++ = *p1++;
                   3243:   }
                   3244:   buf1[length] = 0;
                   3245: 
                   3246:   /* Set up to receive the output.  */
                   3247: 
                   3248:   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
                   3249:   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
                   3250:   obuf.fname = 0;
                   3251:   obuf.macro = 0;
                   3252:   obuf.free_ptr = 0;
                   3253: 
                   3254:   CHECK_DEPTH ({return obuf;});
                   3255: 
                   3256:   ++indepth;
                   3257: 
                   3258:   ip = &instack[indepth];
                   3259:   ip->fname = 0;
                   3260:   ip->nominal_fname = 0;
                   3261:   ip->system_header_p = 0;
                   3262:   ip->macro = 0;
                   3263:   ip->free_ptr = 0;
                   3264:   ip->length = length;
                   3265:   ip->buf = ip->bufp = buf1;
                   3266:   ip->if_stack = if_stack;
                   3267: 
                   3268:   ip->lineno = obuf.lineno = 1;
                   3269: 
                   3270:   /* Scan the input, create the output.  */
                   3271:   rescan (&obuf, output_marks);
                   3272: 
                   3273:   /* Pop input stack to original state.  */
                   3274:   --indepth;
                   3275: 
                   3276:   if (indepth != odepth)
                   3277:     abort ();
                   3278: 
                   3279:   /* Record the output.  */
                   3280:   obuf.length = obuf.bufp - obuf.buf;
                   3281: 
                   3282:   assertions_flag = save_assertions_flag;
                   3283:   return obuf;
                   3284: }
                   3285: 
                   3286: /*
                   3287:  * Process a # directive.  Expects IP->bufp to point after the '#', as in
                   3288:  * `#define foo bar'.  Passes to the command handler
                   3289:  * (do_define, do_include, etc.): the addresses of the 1st and
                   3290:  * last chars of the command (starting immediately after the #
                   3291:  * keyword), plus op and the keyword table pointer.  If the command
                   3292:  * contains comments it is copied into a temporary buffer sans comments
                   3293:  * and the temporary buffer is passed to the command handler instead.
                   3294:  * Likewise for backslash-newlines.
                   3295:  *
                   3296:  * Returns nonzero if this was a known # directive.
                   3297:  * Otherwise, returns zero, without advancing the input pointer.
                   3298:  */
                   3299: 
                   3300: static int
                   3301: handle_directive (ip, op)
                   3302:      FILE_BUF *ip, *op;
                   3303: {
                   3304:   register U_CHAR *bp, *cp;
                   3305:   register struct directive *kt;
                   3306:   register int ident_length;
                   3307:   U_CHAR *resume_p;
                   3308: 
                   3309:   /* Nonzero means we must copy the entire command
                   3310:      to get rid of comments or backslash-newlines.  */
                   3311:   int copy_command = 0;
                   3312: 
                   3313:   U_CHAR *ident, *after_ident;
                   3314: 
                   3315:   bp = ip->bufp;
                   3316: 
                   3317:   /* Record where the directive started.  do_xifdef needs this.  */
                   3318:   directive_start = bp - 1;
                   3319: 
                   3320:   /* Skip whitespace and \-newline.  */
                   3321:   while (1) {
                   3322:     if (is_hor_space[*bp]) {
                   3323:       if ((*bp == '\f' || *bp == '\v') && pedantic)
                   3324:        pedwarn ("%s in preprocessing directive",
                   3325:                 *bp == '\f' ? "formfeed" : "vertical tab");
                   3326:       bp++;
                   3327:     } else if (*bp == '/' && bp[1] == '*') {
                   3328:       ip->bufp = bp;
1.1.1.2   root     3329:       skip_to_end_of_comment (ip, &ip->lineno, 0);
1.1       root     3330:       bp = ip->bufp;
                   3331:     } else if (*bp == '\\' && bp[1] == '\n') {
                   3332:       bp += 2; ip->lineno++;
                   3333:     } else break;
                   3334:   }
                   3335: 
                   3336:   /* Now find end of directive name.
                   3337:      If we encounter a backslash-newline, exchange it with any following
                   3338:      symbol-constituents so that we end up with a contiguous name.  */
                   3339: 
                   3340:   cp = bp;
                   3341:   while (1) {
                   3342:     if (is_idchar[*cp])
                   3343:       cp++;
                   3344:     else {
                   3345:       if (*cp == '\\' && cp[1] == '\n')
                   3346:        name_newline_fix (cp);
                   3347:       if (is_idchar[*cp])
                   3348:        cp++;
                   3349:       else break;
                   3350:     }
                   3351:   }
                   3352:   ident_length = cp - bp;
                   3353:   ident = bp;
                   3354:   after_ident = cp;
                   3355: 
                   3356:   /* A line of just `#' becomes blank.  */
                   3357: 
                   3358:   if (ident_length == 0 && *after_ident == '\n') {
                   3359:     ip->bufp = after_ident;
                   3360:     return 1;
                   3361:   }
                   3362: 
                   3363:   if (ident_length == 0 || !is_idstart[*ident]) {
                   3364:     U_CHAR *p = ident;
                   3365:     while (is_idchar[*p]) {
                   3366:       if (*p < '0' || *p > '9')
                   3367:        break;
                   3368:       p++;
                   3369:     }
                   3370:     /* Handle # followed by a line number.  */
                   3371:     if (p != ident && !is_idchar[*p]) {
                   3372:       static struct directive line_directive_table[] = {
                   3373:        {  4, do_line, "line", T_LINE},
                   3374:       };
                   3375:       if (pedantic)
                   3376:        pedwarn ("`#' followed by integer");
                   3377:       after_ident = ident;
                   3378:       kt = line_directive_table;
                   3379:       goto old_linenum;
                   3380:     }
                   3381: 
                   3382:     /* Avoid error for `###' and similar cases unless -pedantic.  */
                   3383:     if (p == ident) {
                   3384:       while (*p == '#' || is_hor_space[*p]) p++;
                   3385:       if (*p == '\n') {
                   3386:        if (pedantic && !lang_asm)
                   3387:          warning ("invalid preprocessor directive");
                   3388:        return 0;
                   3389:       }
                   3390:     }
                   3391: 
                   3392:     if (!lang_asm)
                   3393:       error ("invalid preprocessor directive name");
                   3394: 
                   3395:     return 0;
                   3396:   }
                   3397: 
                   3398:   /*
                   3399:    * Decode the keyword and call the appropriate expansion
                   3400:    * routine, after moving the input pointer up to the next line.
                   3401:    */
                   3402:   for (kt = directive_table; kt->length > 0; kt++) {
                   3403:     if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length)) {
                   3404:       register U_CHAR *buf;
                   3405:       register U_CHAR *limit;
                   3406:       int unterminated;
                   3407:       int junk;
1.1.1.5   root     3408:       int *already_output;
1.1       root     3409: 
                   3410:       /* Nonzero means do not delete comments within the directive.
                   3411:         #define needs this when -traditional.  */
                   3412:       int keep_comments;
                   3413: 
                   3414:     old_linenum:
                   3415: 
                   3416:       limit = ip->buf + ip->length;
                   3417:       unterminated = 0;
1.1.1.5   root     3418:       already_output = 0;
1.1       root     3419:       keep_comments = traditional && kt->traditional_comments;
                   3420:       /* #import is defined only in Objective C, or when on the NeXT.  */
                   3421:       if (kt->type == T_IMPORT && !(objc || lookup ("__NeXT__", -1, -1)))
                   3422:        break;
                   3423: 
                   3424:       /* Find the end of this command (first newline not backslashed
                   3425:         and not in a string or comment).
                   3426:         Set COPY_COMMAND if the command must be copied
                   3427:         (it contains a backslash-newline or a comment).  */
                   3428: 
                   3429:       buf = bp = after_ident;
                   3430:       while (bp < limit) {
                   3431:        register U_CHAR c = *bp++;
                   3432:        switch (c) {
                   3433:        case '\\':
                   3434:          if (bp < limit) {
                   3435:            if (*bp == '\n') {
                   3436:              ip->lineno++;
                   3437:              copy_command = 1;
                   3438:            }
                   3439:            bp++;
                   3440:          }
                   3441:          break;
                   3442: 
                   3443:        case '\'':
                   3444:        case '\"':
                   3445:          bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command, &unterminated);
                   3446:          /* Don't bother calling the directive if we already got an error
                   3447:             message due to unterminated string.  Skip everything and pretend
                   3448:             we called the directive.  */
                   3449:          if (unterminated) {
                   3450:            if (traditional) {
                   3451:              /* Traditional preprocessing permits unterminated strings.  */
                   3452:              ip->bufp = bp;
                   3453:              goto endloop1;
                   3454:            }
                   3455:            ip->bufp = bp;
                   3456:            return 1;
                   3457:          }
                   3458:          break;
                   3459: 
                   3460:          /* <...> is special for #include.  */
                   3461:        case '<':
                   3462:          if (!kt->angle_brackets)
                   3463:            break;
                   3464:          while (*bp && *bp != '>') bp++;
                   3465:          break;
                   3466: 
                   3467:        case '/':
                   3468:          if (*bp == '\\' && bp[1] == '\n')
                   3469:            newline_fix (bp);
                   3470:          if (*bp == '*'
1.1.1.4   root     3471:              || (cplusplus_comments && *bp == '/')) {
1.1       root     3472:            U_CHAR *obp = bp - 1;
                   3473:            ip->bufp = bp + 1;
1.1.1.2   root     3474:            skip_to_end_of_comment (ip, &ip->lineno, 0);
1.1       root     3475:            bp = ip->bufp;
                   3476:            /* No need to copy the command because of a comment at the end;
                   3477:               just don't include the comment in the directive.  */
                   3478:            if (bp == limit || *bp == '\n') {
                   3479:              bp = obp;
                   3480:              goto endloop1;
                   3481:            }
                   3482:            /* Don't remove the comments if -traditional.  */
                   3483:            if (! keep_comments)
                   3484:              copy_command++;
                   3485:          }
                   3486:          break;
                   3487: 
                   3488:        case '\f':
                   3489:        case '\v':
                   3490:          if (pedantic)
                   3491:            pedwarn ("%s in preprocessing directive",
                   3492:                     c == '\f' ? "formfeed" : "vertical tab");
                   3493:          break;
                   3494: 
                   3495:        case '\n':
                   3496:          --bp;         /* Point to the newline */
                   3497:          ip->bufp = bp;
                   3498:          goto endloop1;
                   3499:        }
                   3500:       }
                   3501:       ip->bufp = bp;
                   3502: 
                   3503:     endloop1:
                   3504:       resume_p = ip->bufp;
                   3505:       /* BP is the end of the directive.
                   3506:         RESUME_P is the next interesting data after the directive.
                   3507:         A comment may come between.  */
                   3508: 
                   3509:       /* If a directive should be copied through, and -E was given,
                   3510:         pass it through before removing comments.  */
                   3511:       if (!no_output && kt->pass_thru && put_out_comments) {
                   3512:         int len;
                   3513: 
                   3514:        /* Output directive name.  */
                   3515:         check_expand (op, kt->length + 2);
                   3516:        /* Make sure # is at the start of a line */
                   3517:        if (op->bufp > op->buf && op->bufp[-1] != '\n') {
                   3518:          op->lineno++;
                   3519:          *op->bufp++ = '\n';
                   3520:        }
                   3521:         *op->bufp++ = '#';
                   3522:         bcopy (kt->name, op->bufp, kt->length);
                   3523:         op->bufp += kt->length;
                   3524: 
                   3525:        /* Output arguments.  */
                   3526:        len = (bp - buf);
                   3527:        check_expand (op, len);
                   3528:        bcopy (buf, op->bufp, len);
                   3529:        op->bufp += len;
                   3530:        /* Take account of any (escaped) newlines just output.  */
                   3531:        while (--len >= 0)
                   3532:          if (buf[len] == '\n')
                   3533:            op->lineno++;
                   3534: 
                   3535:        already_output = &junk;
                   3536:       }                                /* Don't we need a newline or #line? */
                   3537: 
                   3538:       if (copy_command) {
                   3539:        register U_CHAR *xp = buf;
                   3540:        /* Need to copy entire command into temp buffer before dispatching */
                   3541: 
                   3542:        cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
                   3543:                                                  some slop */
                   3544:        buf = cp;
                   3545: 
                   3546:        /* Copy to the new buffer, deleting comments
                   3547:           and backslash-newlines (and whitespace surrounding the latter).  */
                   3548: 
                   3549:        while (xp < bp) {
                   3550:          register U_CHAR c = *xp++;
                   3551:          *cp++ = c;
                   3552: 
                   3553:          switch (c) {
                   3554:          case '\n':
                   3555:            abort ();  /* A bare newline should never part of the line.  */
                   3556:            break;
                   3557: 
                   3558:            /* <...> is special for #include.  */
                   3559:          case '<':
                   3560:            if (!kt->angle_brackets)
                   3561:              break;
                   3562:            while (xp < bp && c != '>') {
                   3563:              c = *xp++;
                   3564:              if (c == '\\' && xp < bp && *xp == '\n')
                   3565:                xp++;
                   3566:              else
                   3567:                *cp++ = c;
                   3568:            }
                   3569:            break;
                   3570: 
                   3571:          case '\\':
                   3572:            if (*xp == '\n') {
                   3573:              xp++;
                   3574:              cp--;
                   3575:              if (cp != buf && is_space[cp[-1]]) {
                   3576:                while (cp != buf && is_space[cp[-1]]) cp--;
                   3577:                cp++;
                   3578:                SKIP_WHITE_SPACE (xp);
                   3579:              } else if (is_space[*xp]) {
                   3580:                *cp++ = *xp++;
                   3581:                SKIP_WHITE_SPACE (xp);
                   3582:              }
1.1.1.3   root     3583:            } else {
                   3584:              *cp++ = *xp++;
1.1       root     3585:            }
                   3586:            break;
                   3587: 
                   3588:          case '\'':
                   3589:          case '\"':
                   3590:            {
                   3591:              register U_CHAR *bp1
1.1.1.4   root     3592:                = skip_quoted_string (xp - 1, bp, ip->lineno,
                   3593:                                      NULL_PTR, NULL_PTR, NULL_PTR);
1.1       root     3594:              while (xp != bp1)
                   3595:                if (*xp == '\\') {
                   3596:                  if (*++xp != '\n')
                   3597:                    *cp++ = '\\';
                   3598:                  else
                   3599:                    xp++;
                   3600:                } else
                   3601:                  *cp++ = *xp++;
                   3602:            }
                   3603:            break;
                   3604: 
                   3605:          case '/':
                   3606:            if (*xp == '*'
1.1.1.4   root     3607:                || (cplusplus_comments && *xp == '/')) {
1.1       root     3608:              ip->bufp = xp + 1;
                   3609:              /* If we already copied the command through,
                   3610:                 already_output != 0 prevents outputting comment now.  */
1.1.1.2   root     3611:              skip_to_end_of_comment (ip, already_output, 0);
1.1       root     3612:              if (keep_comments)
                   3613:                while (xp != ip->bufp)
                   3614:                  *cp++ = *xp++;
                   3615:              /* Delete or replace the slash.  */
                   3616:              else if (traditional)
                   3617:                cp--;
                   3618:              else
                   3619:                cp[-1] = ' ';
                   3620:              xp = ip->bufp;
                   3621:            }
                   3622:          }
                   3623:        }
                   3624: 
                   3625:        /* Null-terminate the copy.  */
                   3626: 
                   3627:        *cp = 0;
                   3628:       } else
                   3629:        cp = bp;
                   3630: 
                   3631:       ip->bufp = resume_p;
                   3632: 
                   3633:       /* Some directives should be written out for cc1 to process,
                   3634:         just as if they were not defined.  And sometimes we're copying
                   3635:         definitions through.  */
                   3636: 
                   3637:       if (!no_output && already_output == 0
                   3638:          && (kt->pass_thru
                   3639:              || (kt->type == T_DEFINE
                   3640:                  && (dump_macros == dump_names
                   3641:                      || dump_macros == dump_definitions)))) {
                   3642:         int len;
                   3643: 
                   3644:        /* Output directive name.  */
                   3645:         check_expand (op, kt->length + 1);
                   3646:         *op->bufp++ = '#';
                   3647:         bcopy (kt->name, op->bufp, kt->length);
                   3648:         op->bufp += kt->length;
                   3649: 
                   3650:        if (kt->pass_thru || dump_macros == dump_definitions) {
                   3651:          /* Output arguments.  */
                   3652:          len = (cp - buf);
                   3653:          check_expand (op, len);
                   3654:          bcopy (buf, op->bufp, len);
                   3655:          op->bufp += len;
1.1.1.4   root     3656:        } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
                   3657:          U_CHAR *xp = buf;
                   3658:          U_CHAR *yp;
                   3659:          SKIP_WHITE_SPACE (xp);
                   3660:          yp = xp;
                   3661:          while (is_idchar[*xp]) xp++;
                   3662:          len = (xp - yp);
                   3663:          check_expand (op, len + 1);
                   3664:          *op->bufp++ = ' ';
                   3665:          bcopy (yp, op->bufp, len);
                   3666:          op->bufp += len;
1.1       root     3667:        }
                   3668:       }                                /* Don't we need a newline or #line? */
                   3669: 
                   3670:       /* Call the appropriate command handler.  buf now points to
                   3671:         either the appropriate place in the input buffer, or to
                   3672:         the temp buffer if it was necessary to make one.  cp
                   3673:         points to the first char after the contents of the (possibly
                   3674:         copied) command, in either case. */
                   3675:       (*kt->func) (buf, cp, op, kt);
                   3676:       check_expand (op, ip->length - (ip->bufp - ip->buf));
                   3677: 
                   3678:       return 1;
                   3679:     }
                   3680:   }
                   3681: 
                   3682:   /* It is deliberate that we don't warn about undefined directives.
                   3683:      That is the responsibility of cc1.  */
                   3684:   return 0;
                   3685: }
                   3686: 
1.1.1.3   root     3687: static struct tm *
                   3688: timestamp ()
                   3689: {
                   3690:   static struct tm *timebuf;
                   3691:   if (!timebuf) {
                   3692:     time_t t = time (0);
                   3693:     timebuf = localtime (&t);
                   3694:   }
                   3695:   return timebuf;
                   3696: }
                   3697: 
1.1       root     3698: static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                   3699:                             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
                   3700:                            };
                   3701: 
                   3702: /*
                   3703:  * expand things like __FILE__.  Place the expansion into the output
                   3704:  * buffer *without* rescanning.
                   3705:  */
                   3706: 
                   3707: static void
                   3708: special_symbol (hp, op)
                   3709:      HASHNODE *hp;
                   3710:      FILE_BUF *op;
                   3711: {
                   3712:   char *buf;
                   3713:   int i, len;
                   3714:   int true_indepth;
                   3715:   FILE_BUF *ip = NULL;
1.1.1.3   root     3716:   struct tm *timebuf;
1.1       root     3717: 
                   3718:   int paren = 0;               /* For special `defined' keyword */
                   3719: 
                   3720:   if (pcp_outfile && pcp_inside_if
                   3721:       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
                   3722:     error ("Predefined macro `%s' used inside `#if' during precompilation",
                   3723:           hp->name);
                   3724:     
                   3725:   for (i = indepth; i >= 0; i--)
                   3726:     if (instack[i].fname != NULL) {
                   3727:       ip = &instack[i];
                   3728:       break;
                   3729:     }
                   3730:   if (ip == NULL) {
                   3731:     error ("cccp error: not in any file?!");
                   3732:     return;                    /* the show must go on */
                   3733:   }
                   3734: 
                   3735:   switch (hp->type) {
                   3736:   case T_FILE:
                   3737:   case T_BASE_FILE:
                   3738:     {
                   3739:       char *string;
                   3740:       if (hp->type == T_FILE)
                   3741:        string = ip->nominal_fname;
                   3742:       else
                   3743:        string = instack[0].nominal_fname;
                   3744: 
                   3745:       if (string)
                   3746:        {
1.1.1.6 ! root     3747:          buf = (char *) alloca (3 + 4 * strlen (string));
        !          3748:          quote_string (buf, string);
1.1       root     3749:        }
                   3750:       else
                   3751:        buf = "\"\"";
                   3752: 
                   3753:       break;
                   3754:     }
                   3755: 
                   3756:   case T_INCLUDE_LEVEL:
                   3757:     true_indepth = 0;
                   3758:     for (i = indepth; i >= 0; i--)
                   3759:       if (instack[i].fname != NULL)
                   3760:         true_indepth++;
                   3761: 
                   3762:     buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
                   3763:     sprintf (buf, "%d", true_indepth - 1);
                   3764:     break;
                   3765: 
                   3766:   case T_VERSION:
                   3767:     buf = (char *) alloca (3 + strlen (version_string));
                   3768:     sprintf (buf, "\"%s\"", version_string);
                   3769:     break;
                   3770: 
1.1.1.6 ! root     3771: #ifndef NO_BUILTIN_SIZE_TYPE
1.1       root     3772:   case T_SIZE_TYPE:
                   3773:     buf = (char *) alloca (3 + strlen (SIZE_TYPE));
                   3774:     sprintf (buf, "%s", SIZE_TYPE);
                   3775:     break;
1.1.1.6 ! root     3776: #endif
1.1       root     3777: 
1.1.1.6 ! root     3778: #ifndef NO_BUILTIN_PTRDIFF_TYPE
1.1       root     3779:   case T_PTRDIFF_TYPE:
                   3780:     buf = (char *) alloca (3 + strlen (PTRDIFF_TYPE));
                   3781:     sprintf (buf, "%s", PTRDIFF_TYPE);
                   3782:     break;
1.1.1.6 ! root     3783: #endif
1.1       root     3784: 
                   3785:   case T_WCHAR_TYPE:
                   3786:     buf = (char *) alloca (3 + strlen (WCHAR_TYPE));
                   3787:     sprintf (buf, "%s", WCHAR_TYPE);
                   3788:     break;
                   3789: 
1.1.1.5   root     3790:   case T_USER_LABEL_PREFIX_TYPE:
                   3791:     buf = (char *) alloca (3 + strlen (USER_LABEL_PREFIX));
                   3792:     sprintf (buf, "%s", USER_LABEL_PREFIX);
                   3793:     break;
                   3794: 
                   3795:   case T_REGISTER_PREFIX_TYPE:
                   3796:     buf = (char *) alloca (3 + strlen (REGISTER_PREFIX));
                   3797:     sprintf (buf, "%s", REGISTER_PREFIX);
                   3798:     break;
                   3799: 
1.1       root     3800:   case T_CONST:
                   3801:     buf = (char *) alloca (4 * sizeof (int));
                   3802:     sprintf (buf, "%d", hp->value.ival);
                   3803:     if (pcp_inside_if && pcp_outfile)
                   3804:       /* Output a precondition for this macro use */
                   3805:       fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
                   3806:     break;
                   3807: 
                   3808:   case T_SPECLINE:
                   3809:     buf = (char *) alloca (10);
                   3810:     sprintf (buf, "%d", ip->lineno);
                   3811:     break;
                   3812: 
                   3813:   case T_DATE:
                   3814:   case T_TIME:
                   3815:     buf = (char *) alloca (20);
1.1.1.3   root     3816:     timebuf = timestamp ();
1.1       root     3817:     if (hp->type == T_DATE)
                   3818:       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
                   3819:              timebuf->tm_mday, timebuf->tm_year + 1900);
                   3820:     else
                   3821:       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
                   3822:              timebuf->tm_sec);
                   3823:     break;
                   3824: 
                   3825:   case T_SPEC_DEFINED:
                   3826:     buf = " 0 ";               /* Assume symbol is not defined */
                   3827:     ip = &instack[indepth];
                   3828:     SKIP_WHITE_SPACE (ip->bufp);
                   3829:     if (*ip->bufp == '(') {
                   3830:       paren++;
                   3831:       ip->bufp++;                      /* Skip over the paren */
                   3832:       SKIP_WHITE_SPACE (ip->bufp);
                   3833:     }
                   3834: 
                   3835:     if (!is_idstart[*ip->bufp])
                   3836:       goto oops;
                   3837:     if (hp = lookup (ip->bufp, -1, -1)) {
                   3838:       if (pcp_outfile && pcp_inside_if
                   3839:          && hp->value.defn->predefined)
                   3840:        /* Output a precondition for this macro use. */
                   3841:        fprintf (pcp_outfile, "#define %s\n", hp->name);
                   3842:       buf = " 1 ";
                   3843:     }
                   3844:     else
                   3845:       if (pcp_outfile && pcp_inside_if)        {
                   3846:        /* Output a precondition for this macro use */
                   3847:        U_CHAR *cp = ip->bufp;
                   3848:        fprintf (pcp_outfile, "#undef ");
                   3849:        while (is_idchar[*cp]) /* Ick! */
                   3850:          fputc (*cp++, pcp_outfile);
                   3851:        putc ('\n', pcp_outfile);
                   3852:       }
                   3853:     while (is_idchar[*ip->bufp])
                   3854:       ++ip->bufp;
                   3855:     SKIP_WHITE_SPACE (ip->bufp);
                   3856:     if (paren) {
                   3857:       if (*ip->bufp != ')')
                   3858:        goto oops;
                   3859:       ++ip->bufp;
                   3860:     }
                   3861:     break;
                   3862: 
                   3863: oops:
                   3864: 
                   3865:     error ("`defined' without an identifier");
                   3866:     break;
                   3867: 
                   3868:   default:
                   3869:     error ("cccp error: invalid special hash type"); /* time for gdb */
                   3870:     abort ();
                   3871:   }
                   3872:   len = strlen (buf);
                   3873:   check_expand (op, len);
                   3874:   bcopy (buf, op->bufp, len);
                   3875:   op->bufp += len;
                   3876: 
                   3877:   return;
                   3878: }
                   3879: 
                   3880: 
                   3881: /* Routines to handle #directives */
                   3882: 
                   3883: /* Handle #include and #import.
                   3884:    This function expects to see "fname" or <fname> on the input.  */
                   3885: 
                   3886: static int
                   3887: do_include (buf, limit, op, keyword)
                   3888:      U_CHAR *buf, *limit;
                   3889:      FILE_BUF *op;
                   3890:      struct directive *keyword;
                   3891: {
                   3892:   int importing = (keyword->type == T_IMPORT);
                   3893:   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
                   3894:   static int import_warning = 0;
                   3895:   char *fname;         /* Dynamically allocated fname buffer */
                   3896:   char *pcftry;
                   3897:   char *pcfname;
                   3898:   U_CHAR *fbeg, *fend;         /* Beginning and end of fname */
                   3899: 
                   3900:   struct file_name_list *search_start = include; /* Chain of dirs to search */
                   3901:   struct file_name_list dsp[1];        /* First in chain, if #include "..." */
1.1.1.4   root     3902:   struct file_name_list *searchptr = 0;
1.1       root     3903:   int flen;
                   3904: 
                   3905:   int f;                       /* file number */
                   3906: 
                   3907:   int retried = 0;             /* Have already tried macro
                   3908:                                   expanding the include line*/
                   3909:   FILE_BUF trybuf;             /* It got expanded into here */
1.1.1.4   root     3910:   int angle_brackets = 0;      /* 0 for "...", 1 for <...> */
1.1       root     3911:   int pcf = -1;
                   3912:   char *pcfbuf;
                   3913:   int pcfbuflimit;
                   3914:   int pcfnum;
                   3915:   f= -1;                       /* JF we iz paranoid! */
                   3916: 
1.1.1.4   root     3917:   if (importing && warn_import && !inhibit_warnings
1.1.1.2   root     3918:       && !instack[indepth].system_header_p && !import_warning) {
1.1       root     3919:     import_warning = 1;
                   3920:     warning ("using `#import' is not recommended");
                   3921:     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
                   3922:     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
                   3923:     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
                   3924:     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
                   3925:     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
                   3926:     fprintf (stderr, "  ... <real contents of file> ...\n");
                   3927:     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
                   3928:     fprintf (stderr, "Then users can use `#include' any number of times.\n");
                   3929:     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
                   3930:     fprintf (stderr, "when it is equipped with such a conditional.\n");
                   3931:   }
                   3932: 
                   3933: get_filename:
                   3934: 
                   3935:   fbeg = buf;
                   3936:   SKIP_WHITE_SPACE (fbeg);
                   3937:   /* Discard trailing whitespace so we can easily see
                   3938:      if we have parsed all the significant chars we were given.  */
                   3939:   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
                   3940: 
                   3941:   switch (*fbeg++) {
                   3942:   case '\"':
1.1.1.4   root     3943:     {
1.1       root     3944:       FILE_BUF *fp;
1.1.1.4   root     3945:       /* Copy the operand text, concatenating the strings.  */
                   3946:       {
                   3947:        U_CHAR *fin = fbeg;
                   3948:        fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
                   3949:        fend = fbeg;
                   3950:        while (fin != limit) {
                   3951:          while (fin != limit && *fin != '\"')
                   3952:            *fend++ = *fin++;
                   3953:          fin++;
                   3954:          if (fin == limit)
                   3955:            break;
                   3956:          /* If not at the end, there had better be another string.  */
                   3957:          /* Skip just horiz space, and don't go past limit.  */
                   3958:          while (fin != limit && is_hor_space[*fin]) fin++;
                   3959:          if (fin != limit && *fin == '\"')
                   3960:            fin++;
                   3961:          else
                   3962:            goto fail;
                   3963:        }
                   3964:       }
1.1.1.5   root     3965:       *fend = 0;
1.1       root     3966: 
                   3967:       /* We have "filename".  Figure out directory this source
                   3968:         file is coming from and put it on the front of the list. */
                   3969: 
                   3970:       /* If -I- was specified, don't search current dir, only spec'd ones. */
                   3971:       if (ignore_srcdir) break;
                   3972: 
                   3973:       for (fp = &instack[indepth]; fp >= instack; fp--)
                   3974:        {
                   3975:          int n;
                   3976:          char *ep,*nam;
                   3977: 
                   3978:          if ((nam = fp->nominal_fname) != NULL) {
                   3979:            /* Found a named file.  Figure out dir of the file,
                   3980:               and put it in front of the search list.  */
                   3981:            dsp[0].next = search_start;
                   3982:            search_start = dsp;
                   3983: #ifndef VMS
                   3984:            ep = rindex (nam, '/');
                   3985: #else                          /* VMS */
                   3986:            ep = rindex (nam, ']');
                   3987:            if (ep == NULL) ep = rindex (nam, '>');
                   3988:            if (ep == NULL) ep = rindex (nam, ':');
                   3989:            if (ep != NULL) ep++;
                   3990: #endif                         /* VMS */
                   3991:            if (ep != NULL) {
                   3992:              n = ep - nam;
                   3993:              dsp[0].fname = (char *) alloca (n + 1);
                   3994:              strncpy (dsp[0].fname, nam, n);
                   3995:              dsp[0].fname[n] = '\0';
1.1.1.4   root     3996:              if (n + INCLUDE_LEN_FUDGE > max_include_len)
                   3997:                max_include_len = n + INCLUDE_LEN_FUDGE;
1.1       root     3998:            } else {
                   3999:              dsp[0].fname = 0; /* Current directory */
                   4000:            }
                   4001:            break;
                   4002:          }
                   4003:        }
                   4004:       break;
                   4005:     }
                   4006: 
                   4007:   case '<':
                   4008:     fend = fbeg;
                   4009:     while (fend != limit && *fend != '>') fend++;
                   4010:     if (*fend == '>' && fend + 1 == limit) {
1.1.1.4   root     4011:       angle_brackets = 1;
1.1       root     4012:       /* If -I-, start with the first -I dir after the -I-.  */
                   4013:       if (first_bracket_include)
                   4014:        search_start = first_bracket_include;
                   4015:       break;
                   4016:     }
                   4017:     goto fail;
                   4018: 
                   4019:   default:
                   4020:   fail:
                   4021:     if (retried) {
1.1.1.5   root     4022:       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1.1       root     4023:       return 0;
                   4024:     } else {
                   4025:       trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
                   4026:       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
                   4027:       bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
                   4028:       limit = buf + (trybuf.bufp - trybuf.buf);
                   4029:       free (trybuf.buf);
                   4030:       retried++;
                   4031:       goto get_filename;
                   4032:     }
                   4033:   }
                   4034: 
                   4035:   /* For #include_next, skip in the search path
                   4036:      past the dir in which the containing file was found.  */
                   4037:   if (skip_dirs) {
                   4038:     FILE_BUF *fp;
                   4039:     for (fp = &instack[indepth]; fp >= instack; fp--)
                   4040:       if (fp->fname != NULL) {
                   4041:        /* fp->dir is null if the containing file was specified
                   4042:           with an absolute file name.  In that case, don't skip anything.  */
                   4043:        if (fp->dir)
                   4044:          search_start = fp->dir->next;
                   4045:        break;
                   4046:       }
                   4047:   }
                   4048: 
                   4049:   flen = fend - fbeg;
1.1.1.5   root     4050: 
                   4051:   if (flen == 0)
                   4052:     {
                   4053:       error ("empty file name in `#%s'", keyword->name);
                   4054:       return 0;
                   4055:     }
                   4056: 
1.1       root     4057:   /* Allocate this permanently, because it gets stored in the definitions
                   4058:      of macros.  */
                   4059:   fname = (char *) xmalloc (max_include_len + flen + 2);
                   4060:   /* + 2 above for slash and terminating null.  */
                   4061: 
                   4062:   /* If specified file name is absolute, just open it.  */
                   4063: 
                   4064:   if (*fbeg == '/') {
                   4065:     strncpy (fname, fbeg, flen);
                   4066:     fname[flen] = 0;
1.1.1.4   root     4067:     if (redundant_include_p (fname))
1.1.1.2   root     4068:       return 0;
1.1       root     4069:     if (importing)
                   4070:       f = lookup_import (fname);
                   4071:     else
                   4072:       f = open (fname, O_RDONLY, 0666);
                   4073:     if (f == -2)
                   4074:       return 0;                /* Already included this file */
                   4075:   } else {
                   4076:     /* Search directory path, trying to open the file.
                   4077:        Copy each filename tried into FNAME.  */
                   4078: 
                   4079:     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
                   4080:       if (searchptr->fname) {
                   4081:        /* The empty string in a search path is ignored.
                   4082:           This makes it possible to turn off entirely
                   4083:           a standard piece of the list.  */
                   4084:        if (searchptr->fname[0] == 0)
                   4085:          continue;
                   4086:        strcpy (fname, searchptr->fname);
                   4087:        strcat (fname, "/");
                   4088:        fname[strlen (fname) + flen] = 0;
                   4089:       } else {
                   4090:        fname[0] = 0;
                   4091:       }
                   4092:       strncat (fname, fbeg, flen);
                   4093: #ifdef VMS
                   4094:       /* Change this 1/2 Unix 1/2 VMS file specification into a
                   4095:          full VMS file specification */
                   4096:       if (searchptr->fname && (searchptr->fname[0] != 0)) {
                   4097:        /* Fix up the filename */
                   4098:        hack_vms_include_specification (fname);
                   4099:       } else {
                   4100:        /* This is a normal VMS filespec, so use it unchanged.  */
                   4101:        strncpy (fname, fbeg, flen);
                   4102:        fname[flen] = 0;
                   4103:       }
                   4104: #endif /* VMS */
                   4105:       if (importing)
                   4106:        f = lookup_import (fname);
                   4107:       else
                   4108:        f = open (fname, O_RDONLY, 0666);
                   4109:       if (f == -2)
                   4110:        return 0;                       /* Already included this file */
1.1.1.5   root     4111: #ifdef EACCES
                   4112:       else if (f == -1 && errno == EACCES)
                   4113:        warning ("Header file %s exists, but is not readable", fname);
                   4114: #endif
1.1.1.4   root     4115:       if (redundant_include_p (fname)) {
1.1.1.2   root     4116:        close (f);
                   4117:        return 0;
                   4118:       }
1.1       root     4119:       if (f >= 0)
                   4120:        break;
                   4121:     }
                   4122:   }
                   4123: 
                   4124:   if (f < 0) {
                   4125:     /* A file that was not found.  */
                   4126: 
                   4127:     strncpy (fname, fbeg, flen);
                   4128:     fname[flen] = 0;
1.1.1.5   root     4129:     /* If -M was specified, and this header file won't be added to the
                   4130:        dependency list, then don't count this as an error, because we can
                   4131:        still produce correct output.  Otherwise, we can't produce correct
                   4132:        output, because there may be dependencies we need inside the missing
                   4133:        file, and we don't know what directory this missing file exists in.  */
                   4134:     if (print_deps
                   4135:        && (print_deps <= (angle_brackets || (system_include_depth > 0))))
                   4136:       warning ("No include path in which to find %s", fname);
                   4137:     else if (search_start)
1.1.1.4   root     4138:       error_from_errno (fname);
                   4139:     else
                   4140:       error ("No include path in which to find %s", fname);
1.1       root     4141:   } else {
                   4142:     struct stat stat_f;
                   4143: 
                   4144:     /* Check to see if this include file is a once-only include file.
                   4145:        If so, give up.  */
                   4146: 
                   4147:     struct file_name_list* ptr;
                   4148: 
                   4149:     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
                   4150:       if (!strcmp (ptr->fname, fname)) {
                   4151:        close (f);
                   4152:         return 0;                              /* This file was once'd. */
                   4153:       }
                   4154:     }
                   4155: 
                   4156:     for (ptr = all_include_files; ptr; ptr = ptr->next) {
                   4157:       if (!strcmp (ptr->fname, fname))
                   4158:         break;                         /* This file was included before. */
                   4159:     }
                   4160: 
                   4161:     if (ptr == 0) {
                   4162:       /* This is the first time for this file.  */
                   4163:       /* Add it to list of files included.  */
                   4164: 
                   4165:       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
                   4166:       ptr->control_macro = 0;
1.1.1.6 ! root     4167:       ptr->c_system_include_path = 0;
1.1       root     4168:       ptr->next = all_include_files;
                   4169:       all_include_files = ptr;
                   4170:       ptr->fname = savestring (fname);
                   4171: 
                   4172:       /* For -M, add this file to the dependencies.  */
1.1.1.4   root     4173:       if (print_deps > (angle_brackets || (system_include_depth > 0))) {
1.1       root     4174:        deps_output ("", 0);
                   4175:        deps_output (fname, 0);
                   4176:        deps_output (" ", 0);
                   4177:       }
                   4178:     }   
                   4179: 
                   4180:     /* Handle -H option.  */
1.1.1.6 ! root     4181:     if (print_include_names) {
        !          4182:       output_dots (stderr, indepth);
1.1       root     4183:       fprintf (stderr, "%s\n", fname);
1.1.1.6 ! root     4184:     }
1.1       root     4185: 
1.1.1.4   root     4186:     if (angle_brackets)
1.1       root     4187:       system_include_depth++;
                   4188: 
                   4189:     /* Actually process the file.  */
                   4190:     add_import (f, fname);     /* Record file on "seen" list for #import. */
                   4191: 
                   4192:     pcftry = (char *) alloca (strlen (fname) + 30);
                   4193:     pcfbuf = 0;
                   4194:     pcfnum = 0;
                   4195: 
                   4196:     fstat (f, &stat_f);
                   4197: 
                   4198:     if (!no_precomp)
                   4199:       do {
                   4200:        sprintf (pcftry, "%s%d", fname, pcfnum++);
                   4201:        
                   4202:        pcf = open (pcftry, O_RDONLY, 0666);
                   4203:        if (pcf != -1)
                   4204:          {
                   4205:            struct stat s;
                   4206: 
                   4207:            fstat (pcf, &s);
                   4208:            if (bcmp (&stat_f.st_ino, &s.st_ino, sizeof (s.st_ino))
                   4209:                || stat_f.st_dev != s.st_dev)
                   4210:              {
                   4211:                pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
                   4212:                /* Don't need it any more.  */
                   4213:                close (pcf);
                   4214:              }
                   4215:            else
                   4216:              {
                   4217:                /* Don't need it at all.  */
                   4218:                close (pcf);
                   4219:                break;
                   4220:              }
                   4221:          }
                   4222:       } while (pcf != -1 && !pcfbuf);
                   4223:     
                   4224:     /* Actually process the file */
                   4225:     if (pcfbuf) {
                   4226:       pcfname = xmalloc (strlen (pcftry) + 1);
                   4227:       strcpy (pcfname, pcftry);
                   4228:       pcfinclude (pcfbuf, pcfbuflimit, fname, op);
                   4229:     }
                   4230:     else
1.1.1.4   root     4231:       finclude (f, fname, op, is_system_include (fname), searchptr);
1.1       root     4232: 
1.1.1.4   root     4233:     if (angle_brackets)
1.1       root     4234:       system_include_depth--;
                   4235:   }
                   4236:   return 0;
                   4237: }
                   4238: 
1.1.1.2   root     4239: /* Return nonzero if there is no need to include file NAME
                   4240:    because it has already been included and it contains a conditional
                   4241:    to make a repeated include do nothing.  */
                   4242: 
                   4243: static int
1.1.1.4   root     4244: redundant_include_p (name)
1.1.1.2   root     4245:      char *name;
                   4246: {
                   4247:   struct file_name_list *l = all_include_files;
                   4248:   for (; l; l = l->next)
                   4249:     if (! strcmp (name, l->fname)
                   4250:        && l->control_macro
                   4251:        && lookup (l->control_macro, -1, -1))
                   4252:       return 1;
                   4253:   return 0;
                   4254: }
                   4255: 
1.1.1.4   root     4256: /* Return nonzero if the given FILENAME is an absolute pathname which
                   4257:    designates a file within one of the known "system" include file
                   4258:    directories.  We assume here that if the given FILENAME looks like
                   4259:    it is the name of a file which resides either directly in a "system"
                   4260:    include file directory, or within any subdirectory thereof, then the
                   4261:    given file must be a "system" include file.  This function tells us
1.1.1.6 ! root     4262:    if we should suppress pedantic errors/warnings for the given FILENAME.
        !          4263: 
        !          4264:    The value is 2 if the file is a C-language system header file
        !          4265:    for which C++ should (on most systems) assume `extern "C"'.  */
1.1.1.4   root     4266: 
                   4267: static int
                   4268: is_system_include (filename)
                   4269:     register char *filename;
                   4270: {
                   4271:   struct file_name_list *searchptr;
                   4272: 
                   4273:   for (searchptr = first_system_include; searchptr;
                   4274:        searchptr = searchptr->next)
                   4275:     if (searchptr->fname) {
                   4276:       register char *sys_dir = searchptr->fname;
                   4277:       register unsigned length = strlen (sys_dir);
                   4278: 
                   4279:       if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
1.1.1.6 ! root     4280:        {
        !          4281:          if (searchptr->c_system_include_path)
        !          4282:            return 2;
        !          4283:          else
        !          4284:            return 1;
        !          4285:        }
1.1.1.4   root     4286:     }
                   4287:   return 0;
                   4288: }
                   4289: 
1.1       root     4290: /* Process the contents of include file FNAME, already open on descriptor F,
                   4291:    with output to OP.
1.1.1.5   root     4292:    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
                   4293:    "system" include directories (as decided by the `is_system_include'
                   4294:    function above).
1.1       root     4295:    DIRPTR is the link in the dir path through which this file was found,
                   4296:    or 0 if the file name was absolute.  */
                   4297: 
                   4298: static void
                   4299: finclude (f, fname, op, system_header_p, dirptr)
                   4300:      int f;
                   4301:      char *fname;
                   4302:      FILE_BUF *op;
                   4303:      int system_header_p;
                   4304:      struct file_name_list *dirptr;
                   4305: {
                   4306:   int st_mode;
                   4307:   long st_size;
                   4308:   long i;
                   4309:   FILE_BUF *fp;                        /* For input stack frame */
                   4310:   int missing_newline = 0;
                   4311: 
                   4312:   CHECK_DEPTH (return;);
                   4313: 
                   4314:   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
1.1.1.4   root     4315:     {
                   4316:       perror_with_name (fname);
                   4317:       close (f);
                   4318:       return;
                   4319:     }
1.1       root     4320: 
                   4321:   fp = &instack[indepth + 1];
                   4322:   bzero (fp, sizeof (FILE_BUF));
                   4323:   fp->nominal_fname = fp->fname = fname;
                   4324:   fp->length = 0;
                   4325:   fp->lineno = 1;
                   4326:   fp->if_stack = if_stack;
                   4327:   fp->system_header_p = system_header_p;
                   4328:   fp->dir = dirptr;
                   4329: 
                   4330:   if (S_ISREG (st_mode)) {
1.1.1.3   root     4331:     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
1.1       root     4332:     fp->bufp = fp->buf;
                   4333: 
                   4334:     /* Read the file contents, knowing that st_size is an upper bound
                   4335:        on the number of bytes we can read.  */
                   4336:     while (st_size > 0) {
1.1.1.6 ! root     4337:       i = safe_read (f, fp->buf + fp->length, st_size);
1.1       root     4338:       if (i <= 0) {
                   4339:        if (i == 0) break;
                   4340:        goto nope;
                   4341:       }
                   4342:       fp->length += i;
                   4343:       st_size -= i;
                   4344:     }
                   4345:   }
1.1.1.5   root     4346:   else if (S_ISDIR (st_mode)) {
                   4347:     error ("directory `%s' specified in #include", fname);
                   4348:     close (f);
                   4349:     return;
                   4350:   } else {
1.1       root     4351:     /* Cannot count its file size before reading.
                   4352:        First read the entire file into heap and
                   4353:        copy them into buffer on stack. */
                   4354: 
                   4355:     U_CHAR *bufp;
                   4356:     U_CHAR *basep;
                   4357:     int bsize = 2000;
                   4358: 
                   4359:     st_size = 0;
                   4360:     basep = (U_CHAR *) xmalloc (bsize + 2);
1.1.1.4   root     4361:     fp->buf = basep; /* So it will get freed, on error.  */
1.1       root     4362:     bufp = basep;
                   4363: 
                   4364:     for (;;) {
1.1.1.6 ! root     4365:       i = safe_read (f, bufp, bsize - st_size);
1.1       root     4366:       if (i < 0)
                   4367:        goto nope;      /* error! */
                   4368:       if (i == 0)
                   4369:        break;  /* End of file */
                   4370:       st_size += i;
                   4371:       bufp += i;
                   4372:       if (bsize == st_size) {  /* Buffer is full! */
                   4373:          bsize *= 2;
                   4374:          basep = (U_CHAR *) xrealloc (basep, bsize + 2);
1.1.1.4   root     4375:          fp->buf = basep;
1.1       root     4376:          bufp = basep + st_size;       /* May have moved */
                   4377:        }
                   4378:     }
                   4379:     fp->bufp = fp->buf;
                   4380:     fp->length = st_size;
                   4381:   }
                   4382: 
1.1.1.5   root     4383:   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
                   4384:       /* Backslash-newline at end is not good enough.  */
                   4385:       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
                   4386:     fp->buf[fp->length++] = '\n';
                   4387:     missing_newline = 1;
                   4388:   }
                   4389:   fp->buf[fp->length] = '\0';
                   4390: 
1.1       root     4391:   /* Close descriptor now, so nesting does not use lots of descriptors.  */
                   4392:   close (f);
                   4393: 
1.1.1.4   root     4394:   /* Must do this before calling trigraph_pcp, so that the correct file name
                   4395:      will be printed in warning messages.  */
                   4396: 
                   4397:   indepth++;
                   4398:   input_file_stack_tick++;
                   4399: 
1.1       root     4400:   if (!no_trigraphs)
                   4401:     trigraph_pcp (fp);
                   4402: 
                   4403:   output_line_command (fp, op, 0, enter_file);
                   4404:   rescan (op, 0);
                   4405: 
1.1.1.5   root     4406:   if (missing_newline)
                   4407:     fp->lineno--;
                   4408: 
1.1       root     4409:   if (pedantic && missing_newline)
                   4410:     pedwarn ("file does not end in newline");
                   4411: 
                   4412:   indepth--;
                   4413:   input_file_stack_tick++;
                   4414:   output_line_command (&instack[indepth], op, 0, leave_file);
1.1.1.3   root     4415:   free (fp->buf);
1.1       root     4416:   return;
                   4417: 
                   4418:  nope:
                   4419: 
                   4420:   perror_with_name (fname);
                   4421:   close (f);
1.1.1.3   root     4422:   free (fp->buf);
1.1       root     4423: }
                   4424: 
                   4425: /* Record that inclusion of the file named FILE
                   4426:    should be controlled by the macro named MACRO_NAME.
                   4427:    This means that trying to include the file again
                   4428:    will do something if that macro is defined.  */
                   4429: 
                   4430: static void
                   4431: record_control_macro (file, macro_name)
                   4432:      char *file;
                   4433:      U_CHAR *macro_name;
                   4434: {
                   4435:   struct file_name_list *new;
                   4436: 
                   4437:   for (new = all_include_files; new; new = new->next) {
                   4438:     if (!strcmp (new->fname, file)) {
                   4439:       new->control_macro = macro_name;
                   4440:       return;
                   4441:     }
                   4442:   }
                   4443: 
                   4444:   /* If the file is not in all_include_files, something's wrong.  */
                   4445:   abort ();
                   4446: }
                   4447: 
                   4448: /* Maintain and search list of included files, for #import.  */
                   4449: 
                   4450: #define IMPORT_HASH_SIZE 31
                   4451: 
                   4452: struct import_file {
                   4453:   char *name;
                   4454:   ino_t inode;
                   4455:   dev_t dev;
                   4456:   struct import_file *next;
                   4457: };
                   4458: 
                   4459: /* Hash table of files already included with #include or #import.  */
                   4460: 
                   4461: static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
                   4462: 
                   4463: /* Hash a file name for import_hash_table.  */
                   4464: 
                   4465: static int 
                   4466: import_hash (f)
                   4467:      char *f;
                   4468: {
                   4469:   int val = 0;
                   4470: 
                   4471:   while (*f) val += *f++;
                   4472:   return (val%IMPORT_HASH_SIZE);
                   4473: }
                   4474: 
                   4475: /* Search for file FILENAME in import_hash_table.
                   4476:    Return -2 if found, either a matching name or a matching inode.
                   4477:    Otherwise, open the file and return a file descriptor if successful
                   4478:    or -1 if unsuccessful.  */
                   4479: 
                   4480: static int
                   4481: lookup_import (filename)
                   4482:      char *filename;
                   4483: {
                   4484:   struct import_file *i;
                   4485:   int h;
                   4486:   int hashval;
                   4487:   struct stat sb;
                   4488:   int fd;
                   4489: 
                   4490:   hashval = import_hash (filename);
                   4491: 
                   4492:   /* Attempt to find file in list of already included files */
                   4493:   i = import_hash_table[hashval];
                   4494: 
                   4495:   while (i) {
                   4496:     if (!strcmp (filename, i->name))
                   4497:       return -2;               /* return found */
                   4498:     i = i->next;
                   4499:   }
                   4500:   /* Open it and try a match on inode/dev */
                   4501:   fd = open (filename, O_RDONLY, 0666);
                   4502:   if (fd < 0)
                   4503:     return fd;
                   4504:   fstat (fd, &sb);
                   4505:   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
                   4506:     i = import_hash_table[h];
                   4507:     while (i) {
                   4508:       /* Compare the inode and the device.
                   4509:         Supposedly on some systems the inode is not a scalar.  */
                   4510:       if (!bcmp (&i->inode, &sb.st_ino, sizeof (sb.st_ino))
                   4511:          && i->dev == sb.st_dev) {
                   4512:         close (fd);
                   4513:         return -2;             /* return found */
                   4514:       }
                   4515:       i = i->next;
                   4516:     }
                   4517:   }
                   4518:   return fd;                   /* Not found, return open file */
                   4519: }
                   4520: 
                   4521: /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
                   4522: 
                   4523: static void
                   4524: add_import (fd, fname)
                   4525:      int fd;
                   4526:      char *fname;
                   4527: {
                   4528:   struct import_file *i;
                   4529:   int hashval;
                   4530:   struct stat sb;
                   4531: 
                   4532:   hashval = import_hash (fname);
                   4533:   fstat (fd, &sb);
                   4534:   i = (struct import_file *)xmalloc (sizeof (struct import_file));
                   4535:   i->name = (char *)xmalloc (strlen (fname)+1);
                   4536:   strcpy (i->name, fname);
                   4537:   bcopy (&sb.st_ino, &i->inode, sizeof (sb.st_ino));
                   4538:   i->dev = sb.st_dev;
                   4539:   i->next = import_hash_table[hashval];
                   4540:   import_hash_table[hashval] = i;
                   4541: }
                   4542: 
                   4543: /* Load the specified precompiled header into core, and verify its
                   4544:    preconditions.  PCF indicates the file descriptor to read, which must
                   4545:    be a regular file.  FNAME indicates the file name of the original 
                   4546:    header.  *LIMIT will be set to an address one past the end of the file.
                   4547:    If the preconditions of the file are not satisfied, the buffer is 
                   4548:    freed and we return 0.  If the preconditions are satisfied, return
                   4549:    the address of the buffer following the preconditions.  The buffer, in
                   4550:    this case, should never be freed because various pieces of it will
                   4551:    be referred to until all precompiled strings are output at the end of
                   4552:    the run.
                   4553: */
                   4554: static char *
                   4555: check_precompiled (pcf, fname, limit)
                   4556:      int pcf;
                   4557:      char *fname;
                   4558:      char **limit;
                   4559: {
                   4560:   int st_mode;
                   4561:   long st_size;
                   4562:   int length = 0;
                   4563:   char *buf;
                   4564:   char *dollar_loc;
                   4565:   int i;
                   4566:   char *cp;
                   4567: 
                   4568:   if (pcp_outfile)
                   4569:     return 0;
                   4570:   
                   4571:   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
                   4572:     return 0;
                   4573: 
                   4574:   if (S_ISREG (st_mode))
                   4575:     {
                   4576:       buf = xmalloc (st_size + 2);
                   4577:       while (st_size > 0)
                   4578:        {
1.1.1.6 ! root     4579:          i = safe_read (pcf, buf + length, st_size);
1.1       root     4580:          if (i < 0)
                   4581:            goto nope;
                   4582:          if (i == 0)
                   4583:            break;
                   4584:          length += i;
                   4585:          st_size -= i;
                   4586:        }         
                   4587:     }
                   4588:   else
                   4589:     abort ();
                   4590:     
                   4591:   if (length > 0 && buf[length-1] != '\n')
                   4592:     buf[length++] = '\n';
                   4593:   buf[length] = '\0';
                   4594:   
                   4595:   *limit = buf + length;
                   4596: 
                   4597:   /* File is in core.  Check the preconditions. */
                   4598:   if (!check_preconditions (buf))
                   4599:     goto nope;
                   4600:   for (cp = buf; *cp; cp++)
                   4601:     ;
                   4602: #ifdef DEBUG_PCP
                   4603:   fprintf (stderr, "Using preinclude %s\n", fname);
                   4604: #endif
                   4605:   return cp + 1;
                   4606: 
                   4607:  nope:
                   4608: #ifdef DEBUG_PCP
                   4609:   fprintf (stderr, "Cannot use preinclude %s\n", fname);
                   4610: #endif
                   4611:   free (buf);
                   4612:   return 0;
                   4613: }
                   4614: 
                   4615: /* PREC (null terminated) points to the preconditions of a
                   4616:    precompiled header.  These are a series of #define and #undef
                   4617:    lines which must match the current contents of the hash
                   4618:    table.  */
                   4619: static int 
                   4620: check_preconditions (prec)
                   4621:      char *prec;
                   4622: {
                   4623:   MACRODEF mdef;
                   4624:   char *lineend;
                   4625:   
                   4626:   while (*prec) {
                   4627:     lineend = (char *) index (prec, '\n');
                   4628:     
                   4629:     if (*prec++ != '#') {
                   4630:       error ("Bad format encountered while reading precompiled file");
                   4631:       return 0;
                   4632:     }
                   4633:     if (!strncmp (prec, "define", 6)) {
                   4634:       HASHNODE *hp;
                   4635:       
                   4636:       prec += 6;
1.1.1.4   root     4637:       mdef = create_definition (prec, lineend, NULL_PTR);
                   4638: 
1.1       root     4639:       if (mdef.defn == 0)
1.1.1.5   root     4640:        abort ();
1.1       root     4641:       
                   4642:       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
                   4643:          || (hp->type != T_MACRO && hp->type != T_CONST)
                   4644:          || (hp->type == T_MACRO
                   4645:              && !compare_defs (mdef.defn, hp->value.defn)
                   4646:              && (mdef.defn->length != 2
                   4647:                  || mdef.defn->expansion[0] != '\n'
                   4648:                  || mdef.defn->expansion[1] != ' ')))
                   4649:        return 0;
                   4650:     } else if (!strncmp (prec, "undef", 5)) {
                   4651:       char *name;
                   4652:       int len;
                   4653:       
                   4654:       prec += 5;
1.1.1.5   root     4655:       while (is_hor_space[(U_CHAR) *prec])
1.1       root     4656:        prec++;
                   4657:       name = prec;
1.1.1.5   root     4658:       while (is_idchar[(U_CHAR) *prec])
1.1       root     4659:        prec++;
                   4660:       len = prec - name;
                   4661:       
                   4662:       if (lookup (name, len, -1))
                   4663:        return 0;
                   4664:     } else {
                   4665:       error ("Bad format encountered while reading precompiled file");
                   4666:       return 0;
                   4667:     }
                   4668:     prec = lineend + 1;
                   4669:   }
                   4670:   /* They all passed successfully */
                   4671:   return 1;
                   4672: }
                   4673: 
                   4674: /* Process the main body of a precompiled file.  BUF points to the
                   4675:    string section of the file, following the preconditions.  LIMIT is one
                   4676:    character past the end.  NAME is the name of the file being read
                   4677:    in.  OP is the main output buffer */
                   4678: static void
                   4679: pcfinclude (buf, limit, name, op)
                   4680:      U_CHAR *buf, *limit, *name;
                   4681:      FILE_BUF *op;
                   4682: {
                   4683:   FILE_BUF tmpbuf;
                   4684:   int nstrings;
                   4685:   U_CHAR *cp = buf;
                   4686: 
                   4687:   /* First in the file comes 4 bytes indicating the number of strings, */
                   4688:   /* in network byte order. (MSB first).  */
                   4689:   nstrings = *cp++;
                   4690:   nstrings = (nstrings << 8) | *cp++;
                   4691:   nstrings = (nstrings << 8) | *cp++;
                   4692:   nstrings = (nstrings << 8) | *cp++;
                   4693:   
                   4694:   /* Looping over each string... */
                   4695:   while (nstrings--) {
                   4696:     U_CHAR *string_start;
                   4697:     U_CHAR *endofthiskey;
                   4698:     STRINGDEF *str;
                   4699:     int nkeys;
                   4700:     
                   4701:     /* Each string starts with a STRINGDEF structure (str), followed */
                   4702:     /* by the text of the string (string_start) */
                   4703: 
                   4704:     /* First skip to a longword boundary */
1.1.1.4   root     4705:     /* ??? Why a 4-byte boundary?  On all machines? */
                   4706:     /* NOTE: This works correctly even if HOST_WIDE_INT
                   4707:        is narrower than a pointer.
                   4708:        Do not try risky measures here to get another type to use!
                   4709:        Do not include gstddef.h or stddef.h--either one will fail!  */
                   4710:     if ((HOST_WIDE_INT) cp & 3)
                   4711:       cp += 4 - ((HOST_WIDE_INT) cp & 3);
1.1       root     4712:     
                   4713:     /* Now get the string. */
                   4714:     str = (STRINGDEF *) cp;
                   4715:     string_start = cp += sizeof (STRINGDEF);
                   4716:     
                   4717:     for (; *cp; cp++)          /* skip the string */
                   4718:       ;
                   4719:     
                   4720:     /* We need to macro expand the string here to ensure that the
                   4721:        proper definition environment is in place.  If it were only
                   4722:        expanded when we find out it is needed, macros necessary for
                   4723:        its proper expansion might have had their definitions changed. */
                   4724:     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
                   4725:     /* Lineno is already set in the precompiled file */
                   4726:     str->contents = tmpbuf.buf;
                   4727:     str->len = tmpbuf.length;
                   4728:     str->writeflag = 0;
                   4729:     str->filename = name;
                   4730:     str->output_mark = outbuf.bufp - outbuf.buf;
                   4731:     
                   4732:     str->chain = 0;
                   4733:     *stringlist_tailp = str;
                   4734:     stringlist_tailp = &str->chain;
                   4735:     
                   4736:     /* Next comes a fourbyte number indicating the number of keys */
                   4737:     /* for this string. */
                   4738:     nkeys = *cp++;
                   4739:     nkeys = (nkeys << 8) | *cp++;
                   4740:     nkeys = (nkeys << 8) | *cp++;
                   4741:     nkeys = (nkeys << 8) | *cp++;
                   4742: 
                   4743:     /* If this number is -1, then the string is mandatory. */
                   4744:     if (nkeys == -1)
                   4745:       str->writeflag = 1;
                   4746:     else
1.1.1.2   root     4747:       /* Otherwise, for each key, */
1.1       root     4748:       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
                   4749:        KEYDEF *kp = (KEYDEF *) cp;
                   4750:        HASHNODE *hp;
                   4751:        
                   4752:        /* It starts with a KEYDEF structure */
                   4753:        cp += sizeof (KEYDEF);
                   4754:        
                   4755:        /* Find the end of the key.  At the end of this for loop we
                   4756:           advance CP to the start of the next key using this variable. */
                   4757:        endofthiskey = cp + strlen (cp);
                   4758:        kp->str = str;
                   4759:        
                   4760:        /* Expand the key, and enter it into the hash table. */
                   4761:        tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
                   4762:        tmpbuf.bufp = tmpbuf.buf;
                   4763:        
                   4764:        while (is_hor_space[*tmpbuf.bufp])
                   4765:          tmpbuf.bufp++;
                   4766:        if (!is_idstart[*tmpbuf.bufp]
                   4767:            || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
                   4768:          str->writeflag = 1;
                   4769:          continue;
                   4770:        }
                   4771:            
                   4772:        hp = lookup (tmpbuf.bufp, -1, -1);
                   4773:        if (hp == NULL) {
                   4774:          kp->chain = 0;
1.1.1.4   root     4775:          install (tmpbuf.bufp, -1, T_PCSTRING, 0, (char *) kp, -1);
1.1       root     4776:        }
                   4777:        else if (hp->type == T_PCSTRING) {
                   4778:          kp->chain = hp->value.keydef;
                   4779:          hp->value.keydef = kp;
                   4780:        }
                   4781:        else
                   4782:          str->writeflag = 1;
                   4783:       }
                   4784:   }
                   4785:   /* This output_line_command serves to switch us back to the current
                   4786:      input file in case some of these strings get output (which will 
                   4787:      result in line commands for the header file being output). */
                   4788:   output_line_command (&instack[indepth], op, 0, enter_file);
                   4789: }
                   4790: 
                   4791: /* Called from rescan when it hits a key for strings.  Mark them all */
                   4792:  /* used and clean up. */
                   4793: static void
                   4794: pcstring_used (hp)
                   4795:      HASHNODE *hp;
                   4796: {
                   4797:   KEYDEF *kp, *tmp;
                   4798:   
                   4799:   for (kp = hp->value.keydef; kp; kp = kp->chain)
                   4800:     kp->str->writeflag = 1;
                   4801:   delete_macro (hp);
                   4802: }
                   4803: 
                   4804: /* Write the output, interspersing precompiled strings in their */
                   4805:  /* appropriate places. */
                   4806: static void
                   4807: write_output ()
                   4808: {
                   4809:   STRINGDEF *next_string;
                   4810:   U_CHAR *cur_buf_loc;
                   4811:   int line_command_len = 80;
                   4812:   char *line_command = xmalloc (line_command_len);
                   4813:   int len;
1.1.1.6 ! root     4814:   int written;
1.1       root     4815: 
                   4816:   /* In each run through the loop, either cur_buf_loc == */
                   4817:   /* next_string_loc, in which case we print a series of strings, or */
                   4818:   /* it is less than next_string_loc, in which case we write some of */
                   4819:   /* the buffer. */
                   4820:   cur_buf_loc = outbuf.buf; 
                   4821:   next_string = stringlist;
                   4822:   
                   4823:   while (cur_buf_loc < outbuf.bufp || next_string) {
                   4824:     if (next_string
                   4825:        && cur_buf_loc - outbuf.buf == next_string->output_mark) {
                   4826:       if (next_string->writeflag) {
1.1.1.6 ! root     4827:        len = 4 * strlen (next_string->filename) + 32;
        !          4828:        while (len > line_command_len)
1.1       root     4829:          line_command = xrealloc (line_command, 
                   4830:                                   line_command_len *= 2);
1.1.1.6 ! root     4831:        sprintf (line_command, "\n# %d ", next_string->lineno);
        !          4832:        strcpy (quote_string (line_command + strlen (line_command),
        !          4833:                              next_string->filename),
        !          4834:                "\n");
        !          4835:        safe_write (fileno (stdout), line_command, strlen (line_command));
        !          4836:        safe_write (fileno (stdout), next_string->contents, next_string->len);
1.1       root     4837:       }              
                   4838:       next_string = next_string->chain;
                   4839:     }
                   4840:     else {
                   4841:       len = (next_string
                   4842:             ? (next_string->output_mark 
                   4843:                - (cur_buf_loc - outbuf.buf))
                   4844:             : outbuf.bufp - cur_buf_loc);
                   4845:       
1.1.1.6 ! root     4846:       safe_write (fileno (stdout), cur_buf_loc, len);
1.1       root     4847:       cur_buf_loc += len;
                   4848:     }
                   4849:   }
1.1.1.5   root     4850:   free (line_command);
1.1       root     4851: }
                   4852: 
                   4853: /* Pass a directive through to the output file.
1.1.1.2   root     4854:    BUF points to the contents of the directive, as a contiguous string.
1.1       root     4855:    LIMIT points to the first character past the end of the directive.
                   4856:    KEYWORD is the keyword-table entry for the directive.  */
                   4857: 
                   4858: static void
                   4859: pass_thru_directive (buf, limit, op, keyword)
                   4860:      U_CHAR *buf, *limit;
                   4861:      FILE_BUF *op;
                   4862:      struct directive *keyword;
                   4863: {
                   4864:   register unsigned keyword_length = keyword->length;
                   4865: 
                   4866:   check_expand (op, 1 + keyword_length + (limit - buf));
                   4867:   *op->bufp++ = '#';
                   4868:   bcopy (keyword->name, op->bufp, keyword_length);
                   4869:   op->bufp += keyword_length;
                   4870:   if (limit != buf && buf[0] != ' ')
                   4871:     *op->bufp++ = ' ';
                   4872:   bcopy (buf, op->bufp, limit - buf);
                   4873:   op->bufp += (limit - buf);
1.1.1.3   root     4874: #if 0
1.1       root     4875:   *op->bufp++ = '\n';
1.1.1.3   root     4876:   /* Count the line we have just made in the output,
                   4877:      to get in sync properly.  */
                   4878:   op->lineno++;
                   4879: #endif
1.1       root     4880: }
                   4881: 
                   4882: /* The arglist structure is built by do_define to tell
                   4883:    collect_definition where the argument names begin.  That
                   4884:    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
                   4885:    would contain pointers to the strings x, y, and z.
                   4886:    Collect_definition would then build a DEFINITION node,
                   4887:    with reflist nodes pointing to the places x, y, and z had
                   4888:    appeared.  So the arglist is just convenience data passed
                   4889:    between these two routines.  It is not kept around after
                   4890:    the current #define has been processed and entered into the
                   4891:    hash table. */
                   4892: 
                   4893: struct arglist {
                   4894:   struct arglist *next;
                   4895:   U_CHAR *name;
                   4896:   int length;
                   4897:   int argno;
1.1.1.3   root     4898:   char rest_args;
1.1       root     4899: };
                   4900: 
                   4901: /* Create a DEFINITION node from a #define directive.  Arguments are 
                   4902:    as for do_define. */
                   4903: static MACRODEF
                   4904: create_definition (buf, limit, op)
                   4905:      U_CHAR *buf, *limit;
                   4906:      FILE_BUF *op;
                   4907: {
                   4908:   U_CHAR *bp;                  /* temp ptr into input buffer */
                   4909:   U_CHAR *symname;             /* remember where symbol name starts */
                   4910:   int sym_length;              /* and how long it is */
                   4911:   int line = instack[indepth].lineno;
                   4912:   char *file = instack[indepth].nominal_fname;
1.1.1.3   root     4913:   int rest_args = 0;
1.1       root     4914: 
                   4915:   DEFINITION *defn;
                   4916:   int arglengths = 0;          /* Accumulate lengths of arg names
                   4917:                                   plus number of args.  */
                   4918:   MACRODEF mdef;
                   4919: 
                   4920:   bp = buf;
                   4921: 
                   4922:   while (is_hor_space[*bp])
                   4923:     bp++;
                   4924: 
                   4925:   symname = bp;                        /* remember where it starts */
                   4926:   sym_length = check_macro_name (bp, "macro");
                   4927:   bp += sym_length;
                   4928: 
                   4929:   /* Lossage will occur if identifiers or control keywords are broken
                   4930:      across lines using backslash.  This is not the right place to take
                   4931:      care of that. */
                   4932: 
                   4933:   if (*bp == '(') {
                   4934:     struct arglist *arg_ptrs = NULL;
                   4935:     int argno = 0;
                   4936: 
                   4937:     bp++;                      /* skip '(' */
                   4938:     SKIP_WHITE_SPACE (bp);
                   4939: 
                   4940:     /* Loop over macro argument names.  */
                   4941:     while (*bp != ')') {
                   4942:       struct arglist *temp;
                   4943: 
                   4944:       temp = (struct arglist *) alloca (sizeof (struct arglist));
                   4945:       temp->name = bp;
                   4946:       temp->next = arg_ptrs;
                   4947:       temp->argno = argno++;
1.1.1.3   root     4948:       temp->rest_args = 0;
1.1       root     4949:       arg_ptrs = temp;
                   4950: 
1.1.1.3   root     4951:       if (rest_args)
                   4952:        pedwarn ("another parameter follows `%s'",
                   4953:                 rest_extension);
1.1       root     4954: 
1.1.1.3   root     4955:       if (!is_idstart[*bp])
                   4956:        pedwarn ("invalid character in macro parameter name");
                   4957:       
1.1       root     4958:       /* Find the end of the arg name.  */
                   4959:       while (is_idchar[*bp]) {
                   4960:        bp++;
1.1.1.3   root     4961:        /* do we have a "special" rest-args extension here? */
                   4962:        if (limit - bp > REST_EXTENSION_LENGTH &&
                   4963:            strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
                   4964:          rest_args = 1;
                   4965:          temp->rest_args = 1;
                   4966:          break;
                   4967:        }
1.1       root     4968:       }
                   4969:       temp->length = bp - temp->name;
1.1.1.3   root     4970:       if (rest_args == 1)
                   4971:        bp += REST_EXTENSION_LENGTH;
1.1       root     4972:       arglengths += temp->length + 2;
                   4973:       SKIP_WHITE_SPACE (bp);
                   4974:       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
                   4975:        error ("badly punctuated parameter list in `#define'");
                   4976:        goto nope;
                   4977:       }
                   4978:       if (*bp == ',') {
                   4979:        bp++;
                   4980:        SKIP_WHITE_SPACE (bp);
                   4981:       }
                   4982:       if (bp >= limit) {
                   4983:        error ("unterminated parameter list in `#define'");
                   4984:        goto nope;
                   4985:       }
                   4986:       {
                   4987:        struct arglist *otemp;
                   4988: 
                   4989:        for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
                   4990:          if (temp->length == otemp->length &&
1.1.1.5   root     4991:            strncmp (temp->name, otemp->name, temp->length) == 0) {
1.1       root     4992:              U_CHAR *name;
                   4993: 
1.1.1.5   root     4994:              name = (U_CHAR *) alloca (temp->length + 1);
                   4995:              (void) strncpy (name, temp->name, temp->length);
1.1       root     4996:              name[temp->length] = '\0';
                   4997:              error ("duplicate argument name `%s' in `#define'", name);
                   4998:              goto nope;
                   4999:          }
                   5000:       }
                   5001:     }
                   5002: 
                   5003:     ++bp;                      /* skip paren */
                   5004:     /* Skip exactly one space or tab if any.  */
                   5005:     if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
                   5006:     /* now everything from bp before limit is the definition. */
                   5007:     defn = collect_expansion (bp, limit, argno, arg_ptrs);
1.1.1.3   root     5008:     defn->rest_args = rest_args;
1.1       root     5009: 
                   5010:     /* Now set defn->args.argnames to the result of concatenating
                   5011:        the argument names in reverse order
                   5012:        with comma-space between them.  */
                   5013:     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
                   5014:     {
                   5015:       struct arglist *temp;
                   5016:       int i = 0;
                   5017:       for (temp = arg_ptrs; temp; temp = temp->next) {
                   5018:        bcopy (temp->name, &defn->args.argnames[i], temp->length);
                   5019:        i += temp->length;
                   5020:        if (temp->next != 0) {
                   5021:          defn->args.argnames[i++] = ',';
                   5022:          defn->args.argnames[i++] = ' ';
                   5023:        }
                   5024:       }
                   5025:       defn->args.argnames[i] = 0;
                   5026:     }
                   5027:   } else {
                   5028:     /* simple expansion or empty definition; gobble it */
                   5029:     if (is_hor_space[*bp])
                   5030:       ++bp;            /* skip exactly one blank/tab char */
                   5031:     /* now everything from bp before limit is the definition. */
1.1.1.4   root     5032:     defn = collect_expansion (bp, limit, -1, NULL_PTR);
1.1       root     5033:     defn->args.argnames = (U_CHAR *) "";
                   5034:   }
                   5035: 
                   5036:   defn->line = line;
                   5037:   defn->file = file;
                   5038: 
                   5039:   /* OP is null if this is a predefinition */
                   5040:   defn->predefined = !op;
                   5041:   mdef.defn = defn;
                   5042:   mdef.symnam = symname;
                   5043:   mdef.symlen = sym_length;
                   5044: 
                   5045:   return mdef;
                   5046: 
                   5047:  nope:
                   5048:   mdef.defn = 0;
                   5049:   return mdef;
                   5050: }
                   5051:  
                   5052: /* Process a #define command.
1.1.1.2   root     5053: BUF points to the contents of the #define command, as a contiguous string.
1.1       root     5054: LIMIT points to the first character past the end of the definition.
                   5055: KEYWORD is the keyword-table entry for #define.  */
                   5056: 
                   5057: static int
                   5058: do_define (buf, limit, op, keyword)
                   5059:      U_CHAR *buf, *limit;
                   5060:      FILE_BUF *op;
                   5061:      struct directive *keyword;
                   5062: {
                   5063:   int hashcode;
                   5064:   MACRODEF mdef;
                   5065: 
                   5066:   /* If this is a precompiler run (with -pcp) pass thru #define commands.  */
                   5067:   if (pcp_outfile && op)
                   5068:     pass_thru_directive (buf, limit, op, keyword);
                   5069: 
                   5070:   mdef = create_definition (buf, limit, op);
                   5071:   if (mdef.defn == 0)
                   5072:     goto nope;
                   5073: 
                   5074:   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
                   5075: 
                   5076:   {
                   5077:     HASHNODE *hp;
                   5078:     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
                   5079:       int ok = 0;
                   5080:       /* Redefining a precompiled key is ok.  */
                   5081:       if (hp->type == T_PCSTRING)
                   5082:        ok = 1;
                   5083:       /* Redefining a macro is ok if the definitions are the same.  */
                   5084:       else if (hp->type == T_MACRO)
                   5085:        ok = ! compare_defs (mdef.defn, hp->value.defn);
                   5086:       /* Redefining a constant is ok with -D.  */
                   5087:       else if (hp->type == T_CONST)
                   5088:         ok = ! done_initializing;
                   5089:       /* Print the warning if it's not ok.  */
                   5090:       if (!ok) {
                   5091:        U_CHAR *msg;            /* what pain... */
                   5092: 
                   5093:         /* If we are passing through #define and #undef directives, do
                   5094:           that for this re-definition now.  */
                   5095:         if (debug_output && op)
                   5096:          pass_thru_directive (buf, limit, op, keyword);
                   5097: 
                   5098:        msg = (U_CHAR *) alloca (mdef.symlen + 22);
                   5099:        *msg = '`';
                   5100:        bcopy (mdef.symnam, msg + 1, mdef.symlen);
                   5101:        strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
                   5102:        pedwarn (msg);
                   5103:        if (hp->type == T_MACRO)
                   5104:          pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
                   5105:                                      "this is the location of the previous definition");
                   5106:       }
                   5107:       /* Replace the old definition.  */
                   5108:       hp->type = T_MACRO;
                   5109:       hp->value.defn = mdef.defn;
                   5110:     } else {
                   5111:       /* If we are passing through #define and #undef directives, do
                   5112:         that for this new definition now.  */
                   5113:       if (debug_output && op)
                   5114:        pass_thru_directive (buf, limit, op, keyword);
1.1.1.4   root     5115:       install (mdef.symnam, mdef.symlen, T_MACRO, 0,
                   5116:               (char *) mdef.defn, hashcode);
1.1       root     5117:     }
                   5118:   }
                   5119: 
                   5120:   return 0;
                   5121: 
                   5122: nope:
                   5123: 
                   5124:   return 1;
                   5125: }
                   5126: 
                   5127: /* Check a purported macro name SYMNAME, and yield its length.
                   5128:    USAGE is the kind of name this is intended for.  */
                   5129: 
                   5130: static int
                   5131: check_macro_name (symname, usage)
                   5132:      U_CHAR *symname;
                   5133:      char *usage;
                   5134: {
                   5135:   U_CHAR *p;
                   5136:   int sym_length;
                   5137: 
                   5138:   for (p = symname; is_idchar[*p]; p++)
                   5139:     ;
                   5140:   sym_length = p - symname;
                   5141:   if (sym_length == 0)
                   5142:     error ("invalid %s name", usage);
                   5143:   else if (!is_idstart[*symname]) {
                   5144:     U_CHAR *msg;                       /* what pain... */
                   5145:     msg = (U_CHAR *) alloca (sym_length + 1);
                   5146:     bcopy (symname, msg, sym_length);
                   5147:     msg[sym_length] = 0;
                   5148:     error ("invalid %s name `%s'", usage, msg);
                   5149:   } else {
                   5150:     if (! strncmp (symname, "defined", 7) && sym_length == 7)
                   5151:       error ("invalid %s name `defined'", usage);
                   5152:   }
                   5153:   return sym_length;
                   5154: }
                   5155: 
                   5156: /*
                   5157:  * return zero if two DEFINITIONs are isomorphic
                   5158:  */
                   5159: static int
                   5160: compare_defs (d1, d2)
                   5161:      DEFINITION *d1, *d2;
                   5162: {
                   5163:   register struct reflist *a1, *a2;
                   5164:   register U_CHAR *p1 = d1->expansion;
                   5165:   register U_CHAR *p2 = d2->expansion;
                   5166:   int first = 1;
                   5167: 
                   5168:   if (d1->nargs != d2->nargs)
                   5169:     return 1;
                   5170:   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
                   5171:     return 1;
                   5172:   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
                   5173:        a1 = a1->next, a2 = a2->next) {
                   5174:     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
                   5175:          || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
                   5176:        || a1->argno != a2->argno
                   5177:        || a1->stringify != a2->stringify
                   5178:        || a1->raw_before != a2->raw_before
                   5179:        || a1->raw_after != a2->raw_after)
                   5180:       return 1;
                   5181:     first = 0;
                   5182:     p1 += a1->nchars;
                   5183:     p2 += a2->nchars;
                   5184:   }
                   5185:   if (a1 != a2)
                   5186:     return 1;
                   5187:   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
                   5188:                     p2, d2->length - (p2 - d2->expansion), 1))
                   5189:     return 1;
                   5190:   return 0;
                   5191: }
                   5192: 
                   5193: /* Return 1 if two parts of two macro definitions are effectively different.
                   5194:    One of the parts starts at BEG1 and has LEN1 chars;
                   5195:    the other has LEN2 chars at BEG2.
                   5196:    Any sequence of whitespace matches any other sequence of whitespace.
                   5197:    FIRST means these parts are the first of a macro definition;
                   5198:     so ignore leading whitespace entirely.
                   5199:    LAST means these parts are the last of a macro definition;
                   5200:     so ignore trailing whitespace entirely.  */
                   5201: 
                   5202: static int
                   5203: comp_def_part (first, beg1, len1, beg2, len2, last)
                   5204:      int first;
                   5205:      U_CHAR *beg1, *beg2;
                   5206:      int len1, len2;
                   5207:      int last;
                   5208: {
                   5209:   register U_CHAR *end1 = beg1 + len1;
                   5210:   register U_CHAR *end2 = beg2 + len2;
                   5211:   if (first) {
                   5212:     while (beg1 != end1 && is_space[*beg1]) beg1++;
                   5213:     while (beg2 != end2 && is_space[*beg2]) beg2++;
                   5214:   }
                   5215:   if (last) {
                   5216:     while (beg1 != end1 && is_space[end1[-1]]) end1--;
                   5217:     while (beg2 != end2 && is_space[end2[-1]]) end2--;
                   5218:   }
                   5219:   while (beg1 != end1 && beg2 != end2) {
                   5220:     if (is_space[*beg1] && is_space[*beg2]) {
                   5221:       while (beg1 != end1 && is_space[*beg1]) beg1++;
                   5222:       while (beg2 != end2 && is_space[*beg2]) beg2++;
                   5223:     } else if (*beg1 == *beg2) {
                   5224:       beg1++; beg2++;
                   5225:     } else break;
                   5226:   }
                   5227:   return (beg1 != end1) || (beg2 != end2);
                   5228: }
                   5229: 
                   5230: /* Read a replacement list for a macro with parameters.
                   5231:    Build the DEFINITION structure.
                   5232:    Reads characters of text starting at BUF until END.
                   5233:    ARGLIST specifies the formal parameters to look for
                   5234:    in the text of the definition; NARGS is the number of args
                   5235:    in that list, or -1 for a macro name that wants no argument list.
                   5236:    MACRONAME is the macro name itself (so we can avoid recursive expansion)
                   5237:    and NAMELEN is its length in characters.
                   5238:    
                   5239: Note that comments and backslash-newlines have already been deleted
                   5240: from the argument.  */
                   5241: 
                   5242: /* Leading and trailing Space, Tab, etc. are converted to markers
                   5243:    Newline Space, Newline Tab, etc.
                   5244:    Newline Space makes a space in the final output
                   5245:    but is discarded if stringified.  (Newline Tab is similar but
                   5246:    makes a Tab instead.)
                   5247: 
                   5248:    If there is no trailing whitespace, a Newline Space is added at the end
                   5249:    to prevent concatenation that would be contrary to the standard.  */
                   5250: 
                   5251: static DEFINITION *
                   5252: collect_expansion (buf, end, nargs, arglist)
                   5253:      U_CHAR *buf, *end;
                   5254:      int nargs;
                   5255:      struct arglist *arglist;
                   5256: {
                   5257:   DEFINITION *defn;
                   5258:   register U_CHAR *p, *limit, *lastp, *exp_p;
                   5259:   struct reflist *endpat = NULL;
                   5260:   /* Pointer to first nonspace after last ## seen.  */
                   5261:   U_CHAR *concat = 0;
                   5262:   /* Pointer to first nonspace after last single-# seen.  */
                   5263:   U_CHAR *stringify = 0;
                   5264:   int maxsize;
                   5265:   int expected_delimiter = '\0';
                   5266: 
                   5267:   /* Scan thru the replacement list, ignoring comments and quoted
                   5268:      strings, picking up on the macro calls.  It does a linear search
                   5269:      thru the arg list on every potential symbol.  Profiling might say
                   5270:      that something smarter should happen. */
                   5271: 
                   5272:   if (end < buf)
                   5273:     abort ();
                   5274: 
                   5275:   /* Find the beginning of the trailing whitespace.  */
                   5276:   /* Find end of leading whitespace.  */
                   5277:   limit = end;
                   5278:   p = buf;
                   5279:   while (p < limit && is_space[limit[-1]]) limit--;
                   5280:   while (p < limit && is_space[*p]) p++;
                   5281: 
                   5282:   /* Allocate space for the text in the macro definition.
                   5283:      Leading and trailing whitespace chars need 2 bytes each.
                   5284:      Each other input char may or may not need 1 byte,
                   5285:      so this is an upper bound.
                   5286:      The extra 2 are for invented trailing newline-marker and final null.  */
                   5287:   maxsize = (sizeof (DEFINITION)
                   5288:             + 2 * (end - limit) + 2 * (p - buf)
                   5289:             + (limit - p) + 3);
                   5290:   defn = (DEFINITION *) xcalloc (1, maxsize);
                   5291: 
                   5292:   defn->nargs = nargs;
                   5293:   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
                   5294:   lastp = exp_p;
                   5295: 
                   5296:   p = buf;
                   5297: 
                   5298:   /* Convert leading whitespace to Newline-markers.  */
                   5299:   while (p < limit && is_space[*p]) {
                   5300:     *exp_p++ = '\n';
                   5301:     *exp_p++ = *p++;
                   5302:   }
                   5303: 
                   5304:   if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
                   5305:     error ("`##' at start of macro definition");
                   5306:     p += 2;
                   5307:   }
                   5308: 
                   5309:   /* Process the main body of the definition.  */
                   5310:   while (p < limit) {
                   5311:     int skipped_arg = 0;
                   5312:     register U_CHAR c = *p++;
                   5313: 
                   5314:     *exp_p++ = c;
                   5315: 
                   5316:     if (!traditional) {
                   5317:       switch (c) {
                   5318:       case '\'':
                   5319:       case '\"':
                   5320:         if (expected_delimiter != '\0') {
                   5321:           if (c == expected_delimiter)
                   5322:             expected_delimiter = '\0';
                   5323:         } else
                   5324:           expected_delimiter = c;
                   5325:        break;
                   5326: 
                   5327:        /* Special hack: if a \# is written in the #define
                   5328:           include a # in the definition.  This is useless for C code
                   5329:           but useful for preprocessing other things.  */
                   5330: 
                   5331:       case '\\':
                   5332:        /* \# quotes a # even outside of strings.  */
                   5333:        if (p < limit && *p == '#' && !expected_delimiter) {
                   5334:          exp_p--;
                   5335:          *exp_p++ = *p++;
                   5336:        } else if (p < limit && expected_delimiter) {
                   5337:          /* In a string, backslash goes through
                   5338:             and makes next char ordinary.  */
                   5339:          *exp_p++ = *p++;
                   5340:        }
                   5341:        break;
                   5342: 
                   5343:       case '#':
                   5344:        /* # is ordinary inside a string.  */
                   5345:        if (expected_delimiter)
                   5346:          break;
                   5347:        if (p < limit && *p == '#') {
                   5348:          /* ##: concatenate preceding and following tokens.  */
                   5349:          /* Take out the first #, discard preceding whitespace.  */
                   5350:          exp_p--;
                   5351:          while (exp_p > lastp && is_hor_space[exp_p[-1]])
                   5352:            --exp_p;
                   5353:          /* Skip the second #.  */
                   5354:          p++;
                   5355:          /* Discard following whitespace.  */
                   5356:          SKIP_WHITE_SPACE (p);
                   5357:          concat = p;
                   5358:          if (p == limit)
                   5359:            error ("`##' at end of macro definition");
1.1.1.5   root     5360:        } else if (nargs >= 0) {
1.1       root     5361:          /* Single #: stringify following argument ref.
                   5362:             Don't leave the # in the expansion.  */
                   5363:          exp_p--;
                   5364:          SKIP_WHITE_SPACE (p);
1.1.1.5   root     5365:          if (p == limit || ! is_idstart[*p])
1.1       root     5366:            error ("`#' operator is not followed by a macro argument name");
                   5367:          else
                   5368:            stringify = p;
                   5369:        }
                   5370:        break;
                   5371:       }
                   5372:     } else {
                   5373:       /* In -traditional mode, recognize arguments inside strings and
                   5374:         and character constants, and ignore special properties of #.
                   5375:         Arguments inside strings are considered "stringified", but no
                   5376:         extra quote marks are supplied.  */
                   5377:       switch (c) {
                   5378:       case '\'':
                   5379:       case '\"':
                   5380:        if (expected_delimiter != '\0') {
                   5381:          if (c == expected_delimiter)
                   5382:            expected_delimiter = '\0';
                   5383:        } else
                   5384:          expected_delimiter = c;
                   5385:        break;
                   5386: 
                   5387:       case '\\':
                   5388:        /* Backslash quotes delimiters and itself, but not macro args.  */
                   5389:        if (expected_delimiter != 0 && p < limit
                   5390:            && (*p == expected_delimiter || *p == '\\')) {
                   5391:          *exp_p++ = *p++;
                   5392:          continue;
                   5393:        }
                   5394:        break;
                   5395: 
                   5396:       case '/':
                   5397:        if (expected_delimiter != '\0') /* No comments inside strings.  */
                   5398:          break;
                   5399:        if (*p == '*') {
                   5400:          /* If we find a comment that wasn't removed by handle_directive,
                   5401:             this must be -traditional.  So replace the comment with
                   5402:             nothing at all.  */
                   5403:          exp_p--;
                   5404:          p += 1;
                   5405:          while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
                   5406:            p++;
                   5407: #if 0
                   5408:          /* Mark this as a concatenation-point, as if it had been ##.  */
                   5409:          concat = p;
                   5410: #endif
                   5411:        }
                   5412:        break;
                   5413:       }
                   5414:     }
                   5415: 
                   5416:     /* Handle the start of a symbol.  */
                   5417:     if (is_idchar[c] && nargs > 0) {
                   5418:       U_CHAR *id_beg = p - 1;
                   5419:       int id_len;
                   5420: 
                   5421:       --exp_p;
                   5422:       while (p != limit && is_idchar[*p]) p++;
                   5423:       id_len = p - id_beg;
                   5424: 
                   5425:       if (is_idstart[c]) {
                   5426:        register struct arglist *arg;
                   5427: 
                   5428:        for (arg = arglist; arg != NULL; arg = arg->next) {
                   5429:          struct reflist *tpat;
                   5430: 
                   5431:          if (arg->name[0] == c
                   5432:              && arg->length == id_len
                   5433:              && strncmp (arg->name, id_beg, id_len) == 0) {
                   5434:            if (expected_delimiter && warn_stringify) {
                   5435:              if (traditional) {
                   5436:                warning ("macro argument `%.*s' is stringified.",
                   5437:                         id_len, arg->name);
                   5438:              } else {
                   5439:                warning ("macro arg `%.*s' would be stringified with -traditional.",
                   5440:                         id_len, arg->name);
                   5441:              }
                   5442:            }
                   5443:            /* If ANSI, don't actually substitute inside a string.  */
                   5444:            if (!traditional && expected_delimiter)
                   5445:              break;
                   5446:            /* make a pat node for this arg and append it to the end of
                   5447:               the pat list */
                   5448:            tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
                   5449:            tpat->next = NULL;
                   5450:            tpat->raw_before = concat == id_beg;
                   5451:            tpat->raw_after = 0;
1.1.1.3   root     5452:            tpat->rest_args = arg->rest_args;
1.1       root     5453:            tpat->stringify = (traditional ? expected_delimiter != '\0'
                   5454:                               : stringify == id_beg);
                   5455: 
                   5456:            if (endpat == NULL)
                   5457:              defn->pattern = tpat;
                   5458:            else
                   5459:              endpat->next = tpat;
                   5460:            endpat = tpat;
                   5461: 
                   5462:            tpat->argno = arg->argno;
                   5463:            tpat->nchars = exp_p - lastp;
                   5464:            {
                   5465:              register U_CHAR *p1 = p;
                   5466:              SKIP_WHITE_SPACE (p1);
                   5467:              if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
                   5468:                tpat->raw_after = 1;
                   5469:            }
                   5470:            lastp = exp_p;      /* place to start copying from next time */
                   5471:            skipped_arg = 1;
                   5472:            break;
                   5473:          }
                   5474:        }
                   5475:       }
                   5476: 
                   5477:       /* If this was not a macro arg, copy it into the expansion.  */
                   5478:       if (! skipped_arg) {
                   5479:        register U_CHAR *lim1 = p;
                   5480:        p = id_beg;
                   5481:        while (p != lim1)
                   5482:          *exp_p++ = *p++;
                   5483:        if (stringify == id_beg)
                   5484:          error ("`#' operator should be followed by a macro argument name");
                   5485:       }
                   5486:     }
                   5487:   }
                   5488: 
                   5489:   if (limit < end) {
                   5490:     /* Convert trailing whitespace to Newline-markers.  */
                   5491:     while (limit < end && is_space[*limit]) {
                   5492:       *exp_p++ = '\n';
                   5493:       *exp_p++ = *limit++;
                   5494:     }
1.1.1.4   root     5495:   } else if (!traditional && expected_delimiter == 0) {
                   5496:     /* There is no trailing whitespace, so invent some in ANSI mode.
                   5497:        But not if "inside a string" (which in ANSI mode
                   5498:        happens only for -D option).  */
1.1       root     5499:     *exp_p++ = '\n';
                   5500:     *exp_p++ = ' ';
                   5501:   }
                   5502: 
                   5503:   *exp_p = '\0';
                   5504: 
                   5505:   defn->length = exp_p - defn->expansion;
                   5506: 
                   5507:   /* Crash now if we overrun the allocated size.  */
                   5508:   if (defn->length + 1 > maxsize)
                   5509:     abort ();
                   5510: 
                   5511: #if 0
                   5512: /* This isn't worth the time it takes.  */
                   5513:   /* give back excess storage */
                   5514:   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
                   5515: #endif
                   5516: 
                   5517:   return defn;
                   5518: }
                   5519: 
                   5520: static int
                   5521: do_assert (buf, limit, op, keyword)
                   5522:      U_CHAR *buf, *limit;
                   5523:      FILE_BUF *op;
                   5524:      struct directive *keyword;
                   5525: {
                   5526:   U_CHAR *bp;                  /* temp ptr into input buffer */
                   5527:   U_CHAR *symname;             /* remember where symbol name starts */
                   5528:   int sym_length;              /* and how long it is */
                   5529:   struct arglist *tokens = NULL;
                   5530: 
                   5531:   if (pedantic && done_initializing && !instack[indepth].system_header_p)
                   5532:     pedwarn ("ANSI C does not allow `#assert'");
                   5533: 
                   5534:   bp = buf;
                   5535: 
                   5536:   while (is_hor_space[*bp])
                   5537:     bp++;
                   5538: 
                   5539:   symname = bp;                        /* remember where it starts */
                   5540:   sym_length = check_macro_name (bp, "assertion");
                   5541:   bp += sym_length;
                   5542:   /* #define doesn't do this, but we should.  */
                   5543:   SKIP_WHITE_SPACE (bp);
                   5544: 
                   5545:   /* Lossage will occur if identifiers or control tokens are broken
                   5546:      across lines using backslash.  This is not the right place to take
                   5547:      care of that. */
                   5548: 
                   5549:   if (*bp != '(') {
                   5550:     error ("missing token-sequence in `#assert'");
                   5551:     return 1;
                   5552:   }
                   5553: 
                   5554:   {
                   5555:     int error_flag = 0;
                   5556: 
                   5557:     bp++;                      /* skip '(' */
                   5558:     SKIP_WHITE_SPACE (bp);
                   5559: 
                   5560:     tokens = read_token_list (&bp, limit, &error_flag);
                   5561:     if (error_flag)
                   5562:       return 1;
                   5563:     if (tokens == 0) {
                   5564:       error ("empty token-sequence in `#assert'");
                   5565:       return 1;
                   5566:     }
                   5567: 
                   5568:     ++bp;                      /* skip paren */
                   5569:     SKIP_WHITE_SPACE (bp);
                   5570:   }
                   5571: 
                   5572:   /* If this name isn't already an assertion name, make it one.
                   5573:      Error if it was already in use in some other way.  */
                   5574: 
                   5575:   {
                   5576:     ASSERTION_HASHNODE *hp;
                   5577:     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
                   5578:     struct tokenlist_list *value
                   5579:       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
                   5580: 
                   5581:     hp = assertion_lookup (symname, sym_length, hashcode);
                   5582:     if (hp == NULL) {
                   5583:       if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
                   5584:        error ("`defined' redefined as assertion");
                   5585:       hp = assertion_install (symname, sym_length, hashcode);
                   5586:     }
                   5587: 
                   5588:     /* Add the spec'd token-sequence to the list of such.  */
                   5589:     value->tokens = tokens;
                   5590:     value->next = hp->value;
                   5591:     hp->value = value;
                   5592:   }
                   5593: 
                   5594:   return 0;
                   5595: }
                   5596: 
                   5597: static int
                   5598: do_unassert (buf, limit, op, keyword)
                   5599:      U_CHAR *buf, *limit;
                   5600:      FILE_BUF *op;
                   5601:      struct directive *keyword;
                   5602: {
                   5603:   U_CHAR *bp;                  /* temp ptr into input buffer */
                   5604:   U_CHAR *symname;             /* remember where symbol name starts */
                   5605:   int sym_length;              /* and how long it is */
                   5606: 
                   5607:   struct arglist *tokens = NULL;
                   5608:   int tokens_specified = 0;
                   5609: 
                   5610:   if (pedantic && done_initializing && !instack[indepth].system_header_p)
                   5611:     pedwarn ("ANSI C does not allow `#unassert'");
                   5612: 
                   5613:   bp = buf;
                   5614: 
                   5615:   while (is_hor_space[*bp])
                   5616:     bp++;
                   5617: 
                   5618:   symname = bp;                        /* remember where it starts */
                   5619:   sym_length = check_macro_name (bp, "assertion");
                   5620:   bp += sym_length;
                   5621:   /* #define doesn't do this, but we should.  */
                   5622:   SKIP_WHITE_SPACE (bp);
                   5623: 
                   5624:   /* Lossage will occur if identifiers or control tokens are broken
                   5625:      across lines using backslash.  This is not the right place to take
                   5626:      care of that. */
                   5627: 
                   5628:   if (*bp == '(') {
                   5629:     int error_flag = 0;
                   5630: 
                   5631:     bp++;                      /* skip '(' */
                   5632:     SKIP_WHITE_SPACE (bp);
                   5633: 
                   5634:     tokens = read_token_list (&bp, limit, &error_flag);
                   5635:     if (error_flag)
                   5636:       return 1;
                   5637:     if (tokens == 0) {
                   5638:       error ("empty token list in `#unassert'");
                   5639:       return 1;
                   5640:     }
                   5641: 
                   5642:     tokens_specified = 1;
                   5643: 
                   5644:     ++bp;                      /* skip paren */
                   5645:     SKIP_WHITE_SPACE (bp);
                   5646:   }
                   5647: 
                   5648:   {
                   5649:     ASSERTION_HASHNODE *hp;
                   5650:     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
                   5651:     struct tokenlist_list *tail, *prev;
                   5652: 
                   5653:     hp = assertion_lookup (symname, sym_length, hashcode);
                   5654:     if (hp == NULL)
                   5655:       return 1;
                   5656: 
                   5657:     /* If no token list was specified, then eliminate this assertion
                   5658:        entirely.  */
                   5659:     if (! tokens_specified) {
                   5660:       struct tokenlist_list *next;
                   5661:       for (tail = hp->value; tail; tail = next) {
                   5662:        next = tail->next;
                   5663:        free_token_list (tail->tokens);
                   5664:        free (tail);
                   5665:       }
                   5666:       delete_assertion (hp);
                   5667:     } else {
                   5668:       /* If a list of tokens was given, then delete any matching list.  */
                   5669: 
                   5670:       tail = hp->value;
                   5671:       prev = 0;
                   5672:       while (tail) {
                   5673:        struct tokenlist_list *next = tail->next;
                   5674:        if (compare_token_lists (tail->tokens, tokens)) {
                   5675:          if (prev)
                   5676:            prev->next = next;
                   5677:          else
                   5678:            hp->value = tail->next;
                   5679:          free_token_list (tail->tokens);
                   5680:          free (tail);
                   5681:        } else {
                   5682:          prev = tail;
                   5683:        }
                   5684:        tail = next;
                   5685:       }
                   5686:     }
                   5687:   }
                   5688: 
                   5689:   return 0;
                   5690: }
                   5691: 
                   5692: /* Test whether there is an assertion named NAME
                   5693:    and optionally whether it has an asserted token list TOKENS.
                   5694:    NAME is not null terminated; its length is SYM_LENGTH.
                   5695:    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
                   5696: 
                   5697: int
                   5698: check_assertion (name, sym_length, tokens_specified, tokens)
                   5699:      U_CHAR *name;
                   5700:      int sym_length;
                   5701:      int tokens_specified;
                   5702:      struct arglist *tokens;
                   5703: {
                   5704:   ASSERTION_HASHNODE *hp;
                   5705:   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
                   5706: 
                   5707:   if (pedantic && !instack[indepth].system_header_p)
                   5708:     pedwarn ("ANSI C does not allow testing assertions");
                   5709: 
                   5710:   hp = assertion_lookup (name, sym_length, hashcode);
                   5711:   if (hp == NULL)
                   5712:     /* It is not an assertion; just return false.  */
                   5713:     return 0;
                   5714: 
                   5715:   /* If no token list was specified, then value is 1.  */
                   5716:   if (! tokens_specified)
                   5717:     return 1;
                   5718: 
                   5719:   {
                   5720:     struct tokenlist_list *tail;
                   5721: 
                   5722:     tail = hp->value;
                   5723: 
                   5724:     /* If a list of tokens was given,
                   5725:        then succeed if the assertion records a matching list.  */
                   5726: 
                   5727:     while (tail) {
                   5728:       if (compare_token_lists (tail->tokens, tokens))
                   5729:        return 1;
                   5730:       tail = tail->next;
                   5731:     }
                   5732: 
                   5733:     /* Fail if the assertion has no matching list.  */
                   5734:     return 0;
                   5735:   }
                   5736: }
                   5737: 
                   5738: /* Compare two lists of tokens for equality including order of tokens.  */
                   5739: 
                   5740: static int
                   5741: compare_token_lists (l1, l2)
                   5742:      struct arglist *l1, *l2;
                   5743: {
                   5744:   while (l1 && l2) {
                   5745:     if (l1->length != l2->length)
                   5746:       return 0;
                   5747:     if (strncmp (l1->name, l2->name, l1->length))
                   5748:       return 0;
                   5749:     l1 = l1->next;
                   5750:     l2 = l2->next;
                   5751:   }
                   5752: 
                   5753:   /* Succeed if both lists end at the same time.  */
                   5754:   return l1 == l2;
                   5755: }
                   5756: 
                   5757: /* Read a space-separated list of tokens ending in a close parenthesis.
                   5758:    Return a list of strings, in the order they were written.
                   5759:    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
                   5760:    Parse the text starting at *BPP, and update *BPP.
                   5761:    Don't parse beyond LIMIT.  */
                   5762: 
                   5763: static struct arglist *
                   5764: read_token_list (bpp, limit, error_flag)
                   5765:      U_CHAR **bpp;
                   5766:      U_CHAR *limit;
                   5767:      int *error_flag;
                   5768: {
                   5769:   struct arglist *token_ptrs = 0;
                   5770:   U_CHAR *bp = *bpp;
                   5771:   int depth = 1;
                   5772: 
                   5773:   *error_flag = 0;
                   5774: 
                   5775:   /* Loop over the assertion value tokens.  */
                   5776:   while (depth > 0) {
                   5777:     struct arglist *temp;
                   5778:     int eofp = 0;
                   5779:     U_CHAR *beg = bp;
                   5780: 
                   5781:     /* Find the end of the token.  */
                   5782:     if (*bp == '(') {
                   5783:       bp++;
                   5784:       depth++;
                   5785:     } else if (*bp == ')') {
                   5786:       depth--;
                   5787:       if (depth == 0)
                   5788:        break;
                   5789:       bp++;
                   5790:     } else if (*bp == '"' || *bp == '\'')
1.1.1.4   root     5791:       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
1.1       root     5792:     else
                   5793:       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
                   5794:             && *bp != '"' && *bp != '\'' && bp != limit)
                   5795:        bp++;
                   5796: 
                   5797:     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
                   5798:     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
                   5799:     bcopy (beg, temp->name, bp - beg);
                   5800:     temp->name[bp - beg] = 0;
                   5801:     temp->next = token_ptrs;
                   5802:     token_ptrs = temp;
                   5803:     temp->length = bp - beg;
                   5804: 
                   5805:     SKIP_WHITE_SPACE (bp);
                   5806: 
                   5807:     if (bp >= limit) {
                   5808:       error ("unterminated token sequence in `#assert' or `#unassert'");
                   5809:       *error_flag = -1;
                   5810:       return 0;
                   5811:     }
                   5812:   }
                   5813:   *bpp = bp;
                   5814: 
                   5815:   /* We accumulated the names in reverse order.
                   5816:      Now reverse them to get the proper order.  */
                   5817:   {
                   5818:     register struct arglist *prev = 0, *this, *next;
                   5819:     for (this = token_ptrs; this; this = next) {
                   5820:       next = this->next;
                   5821:       this->next = prev;
                   5822:       prev = this;
                   5823:     }
                   5824:     return prev;
                   5825:   }
                   5826: }
                   5827: 
                   5828: static void
                   5829: free_token_list (tokens)
                   5830:      struct arglist *tokens;
                   5831: {
                   5832:   while (tokens) {
                   5833:     struct arglist *next = tokens->next;
                   5834:     free (tokens->name);
                   5835:     free (tokens);
                   5836:     tokens = next;
                   5837:   }
                   5838: }
                   5839: 
                   5840: /*
                   5841:  * Install a name in the assertion hash table.
                   5842:  *
                   5843:  * If LEN is >= 0, it is the length of the name.
                   5844:  * Otherwise, compute the length by scanning the entire name.
                   5845:  *
                   5846:  * If HASH is >= 0, it is the precomputed hash code.
                   5847:  * Otherwise, compute the hash code.
                   5848:  */
                   5849: static ASSERTION_HASHNODE *
                   5850: assertion_install (name, len, hash)
                   5851:      U_CHAR *name;
                   5852:      int len;
                   5853:      int hash;
                   5854: {
                   5855:   register ASSERTION_HASHNODE *hp;
                   5856:   register int i, bucket;
                   5857:   register U_CHAR *p, *q;
                   5858: 
                   5859:   i = sizeof (ASSERTION_HASHNODE) + len + 1;
                   5860:   hp = (ASSERTION_HASHNODE *) xmalloc (i);
                   5861:   bucket = hash;
                   5862:   hp->bucket_hdr = &assertion_hashtab[bucket];
                   5863:   hp->next = assertion_hashtab[bucket];
                   5864:   assertion_hashtab[bucket] = hp;
                   5865:   hp->prev = NULL;
                   5866:   if (hp->next != NULL)
                   5867:     hp->next->prev = hp;
                   5868:   hp->length = len;
                   5869:   hp->value = 0;
                   5870:   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
                   5871:   p = hp->name;
                   5872:   q = name;
                   5873:   for (i = 0; i < len; i++)
                   5874:     *p++ = *q++;
                   5875:   hp->name[len] = 0;
                   5876:   return hp;
                   5877: }
                   5878: 
                   5879: /*
                   5880:  * find the most recent hash node for name name (ending with first
                   5881:  * non-identifier char) installed by install
                   5882:  *
                   5883:  * If LEN is >= 0, it is the length of the name.
                   5884:  * Otherwise, compute the length by scanning the entire name.
                   5885:  *
                   5886:  * If HASH is >= 0, it is the precomputed hash code.
                   5887:  * Otherwise, compute the hash code.
                   5888:  */
                   5889: static ASSERTION_HASHNODE *
                   5890: assertion_lookup (name, len, hash)
                   5891:      U_CHAR *name;
                   5892:      int len;
                   5893:      int hash;
                   5894: {
                   5895:   register U_CHAR *bp;
                   5896:   register ASSERTION_HASHNODE *bucket;
                   5897: 
                   5898:   bucket = assertion_hashtab[hash];
                   5899:   while (bucket) {
                   5900:     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
                   5901:       return bucket;
                   5902:     bucket = bucket->next;
                   5903:   }
                   5904:   return NULL;
                   5905: }
                   5906: 
                   5907: static void
                   5908: delete_assertion (hp)
                   5909:      ASSERTION_HASHNODE *hp;
                   5910: {
                   5911: 
                   5912:   if (hp->prev != NULL)
                   5913:     hp->prev->next = hp->next;
                   5914:   if (hp->next != NULL)
                   5915:     hp->next->prev = hp->prev;
                   5916: 
                   5917:   /* make sure that the bucket chain header that
                   5918:      the deleted guy was on points to the right thing afterwards. */
                   5919:   if (hp == *hp->bucket_hdr)
                   5920:     *hp->bucket_hdr = hp->next;
                   5921: 
                   5922:   free (hp);
                   5923: }
                   5924: 
                   5925: /*
                   5926:  * interpret #line command.  Remembers previously seen fnames
                   5927:  * in its very own hash table.
                   5928:  */
                   5929: #define FNAME_HASHSIZE 37
                   5930: 
                   5931: static int
                   5932: do_line (buf, limit, op, keyword)
                   5933:      U_CHAR *buf, *limit;
                   5934:      FILE_BUF *op;
                   5935:      struct directive *keyword;
                   5936: {
                   5937:   register U_CHAR *bp;
                   5938:   FILE_BUF *ip = &instack[indepth];
                   5939:   FILE_BUF tem;
                   5940:   int new_lineno;
                   5941:   enum file_change_code file_change = same_file;
                   5942: 
                   5943:   /* Expand any macros.  */
                   5944:   tem = expand_to_temp_buffer (buf, limit, 0, 0);
                   5945: 
                   5946:   /* Point to macroexpanded line, which is null-terminated now.  */
                   5947:   bp = tem.buf;
                   5948:   SKIP_WHITE_SPACE (bp);
                   5949: 
                   5950:   if (!isdigit (*bp)) {
                   5951:     error ("invalid format `#line' command");
                   5952:     return 0;
                   5953:   }
                   5954: 
                   5955:   /* The Newline at the end of this line remains to be processed.
                   5956:      To put the next line at the specified line number,
                   5957:      we must store a line number now that is one less.  */
                   5958:   new_lineno = atoi (bp) - 1;
                   5959: 
1.1.1.5   root     5960:   /* NEW_LINENO is one less than the actual line number here.  */
                   5961:   if (pedantic && new_lineno < 0)
                   5962:     pedwarn ("line number out of range in `#line' command");
                   5963: 
1.1       root     5964:   /* skip over the line number.  */
                   5965:   while (isdigit (*bp))
                   5966:     bp++;
                   5967: 
                   5968: #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
                   5969:   if (*bp && !is_space[*bp]) {
                   5970:     error ("invalid format `#line' command");
                   5971:     return;
                   5972:   }
                   5973: #endif
                   5974: 
                   5975:   SKIP_WHITE_SPACE (bp);
                   5976: 
                   5977:   if (*bp == '\"') {
                   5978:     static HASHNODE *fname_table[FNAME_HASHSIZE];
                   5979:     HASHNODE *hp, **hash_bucket;
1.1.1.6 ! root     5980:     U_CHAR *fname, *p;
1.1       root     5981:     int fname_length;
                   5982: 
                   5983:     fname = ++bp;
                   5984: 
1.1.1.6 ! root     5985:     /* Turn the file name, which is a character string literal,
        !          5986:        into a null-terminated string.  Do this in place.  */
        !          5987:     p = bp;
        !          5988:     for (;;)
        !          5989:       switch ((*p++ = *bp++)) {
        !          5990:       case '\0':
        !          5991:        error ("invalid format `#line' command");
        !          5992:        return 0;
1.1       root     5993: 
1.1.1.6 ! root     5994:       case '\\':
        !          5995:        {
        !          5996:          char *bpc = (char *) bp;
        !          5997:          int c = parse_escape (&bpc);
        !          5998:          bp = (U_CHAR *) bpc;
        !          5999:          if (c < 0)
        !          6000:            p--;
        !          6001:          else
        !          6002:            p[-1] = c;
        !          6003:        }
        !          6004:        break;
        !          6005: 
        !          6006:       case '\"':
        !          6007:        p[-1] = 0;
        !          6008:        goto fname_done;
        !          6009:       }
        !          6010:   fname_done:
        !          6011:     fname_length = p - fname;
1.1       root     6012: 
                   6013:     SKIP_WHITE_SPACE (bp);
                   6014:     if (*bp) {
1.1.1.5   root     6015:       if (pedantic)
                   6016:        pedwarn ("garbage at end of `#line' command");
1.1       root     6017:       if (*bp == '1')
                   6018:        file_change = enter_file;
                   6019:       else if (*bp == '2')
                   6020:        file_change = leave_file;
1.1.1.2   root     6021:       else if (*bp == '3')
                   6022:        ip->system_header_p = 1;
1.1.1.6 ! root     6023:       else if (*bp == '4')
        !          6024:        ip->system_header_p = 2;
1.1       root     6025:       else {
                   6026:        error ("invalid format `#line' command");
                   6027:        return 0;
                   6028:       }
                   6029: 
                   6030:       bp++;
                   6031:       SKIP_WHITE_SPACE (bp);
1.1.1.2   root     6032:       if (*bp == '3') {
                   6033:        ip->system_header_p = 1;
                   6034:        bp++;
                   6035:        SKIP_WHITE_SPACE (bp);
                   6036:       }
1.1.1.6 ! root     6037:       if (*bp == '4') {
        !          6038:        ip->system_header_p = 2;
        !          6039:        bp++;
        !          6040:        SKIP_WHITE_SPACE (bp);
        !          6041:       }
1.1       root     6042:       if (*bp) {
                   6043:        error ("invalid format `#line' command");
                   6044:        return 0;
                   6045:       }
                   6046:     }
                   6047: 
                   6048:     hash_bucket =
                   6049:       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
                   6050:     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
                   6051:       if (hp->length == fname_length &&
                   6052:          strncmp (hp->value.cpval, fname, fname_length) == 0) {
                   6053:        ip->nominal_fname = hp->value.cpval;
                   6054:        break;
                   6055:       }
                   6056:     if (hp == 0) {
                   6057:       /* Didn't find it; cons up a new one.  */
                   6058:       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
                   6059:       hp->next = *hash_bucket;
                   6060:       *hash_bucket = hp;
                   6061: 
                   6062:       hp->length = fname_length;
                   6063:       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
                   6064:       bcopy (fname, hp->value.cpval, fname_length);
                   6065:     }
                   6066:   } else if (*bp) {
                   6067:     error ("invalid format `#line' command");
                   6068:     return 0;
                   6069:   }
                   6070: 
                   6071:   ip->lineno = new_lineno;
                   6072:   output_line_command (ip, op, 0, file_change);
                   6073:   check_expand (op, ip->length - (ip->bufp - ip->buf));
                   6074:   return 0;
                   6075: }
                   6076: 
                   6077: /*
                   6078:  * remove the definition of a symbol from the symbol table.
                   6079:  * according to un*x /lib/cpp, it is not an error to undef
                   6080:  * something that has no definitions, so it isn't one here either.
                   6081:  */
                   6082: 
                   6083: static int
                   6084: do_undef (buf, limit, op, keyword)
                   6085:      U_CHAR *buf, *limit;
                   6086:      FILE_BUF *op;
                   6087:      struct directive *keyword;
                   6088: {
                   6089:   int sym_length;
                   6090:   HASHNODE *hp;
                   6091:   U_CHAR *orig_buf = buf;
                   6092: 
                   6093:   /* If this is a precompiler run (with -pcp) pass thru #undef commands.  */
                   6094:   if (pcp_outfile && op)
                   6095:     pass_thru_directive (buf, limit, op, keyword);
                   6096: 
                   6097:   SKIP_WHITE_SPACE (buf);
                   6098:   sym_length = check_macro_name (buf, "macro");
                   6099: 
                   6100:   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
                   6101:     /* If we are generating additional info for debugging (with -g) we
                   6102:        need to pass through all effective #undef commands.  */
                   6103:     if (debug_output && op)
                   6104:       pass_thru_directive (orig_buf, limit, op, keyword);
                   6105:     if (hp->type != T_MACRO)
                   6106:       warning ("undefining `%s'", hp->name);
                   6107:     delete_macro (hp);
                   6108:   }
                   6109: 
                   6110:   if (pedantic) {
                   6111:     buf += sym_length;
                   6112:     SKIP_WHITE_SPACE (buf);
                   6113:     if (buf != limit)
                   6114:       pedwarn ("garbage after `#undef' directive");
                   6115:   }
                   6116:   return 0;
                   6117: }
                   6118: 
                   6119: /*
                   6120:  * Report a fatal error detected by the program we are processing.
                   6121:  * Use the text of the line in the error message, then terminate.
1.1.1.5   root     6122:  * (We use error because it prints the filename & line#.)
1.1       root     6123:  */
                   6124: 
                   6125: static int
                   6126: do_error (buf, limit, op, keyword)
                   6127:      U_CHAR *buf, *limit;
                   6128:      FILE_BUF *op;
                   6129:      struct directive *keyword;
                   6130: {
                   6131:   int length = limit - buf;
1.1.1.5   root     6132:   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
1.1       root     6133:   bcopy (buf, copy, length);
                   6134:   copy[length] = 0;
                   6135:   SKIP_WHITE_SPACE (copy);
                   6136:   error ("#error %s", copy);
                   6137:   exit (FAILURE_EXIT_CODE);
                   6138:   /* NOTREACHED */
                   6139:   return 0;
                   6140: }
                   6141: 
                   6142: /*
                   6143:  * Report a warning detected by the program we are processing.
                   6144:  * Use the text of the line in the warning message, then continue.
1.1.1.5   root     6145:  * (We use error because it prints the filename & line#.)
1.1       root     6146:  */
                   6147: 
                   6148: static int
                   6149: do_warning (buf, limit, op, keyword)
                   6150:      U_CHAR *buf, *limit;
                   6151:      FILE_BUF *op;
                   6152:      struct directive *keyword;
                   6153: {
                   6154:   int length = limit - buf;
1.1.1.5   root     6155:   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
1.1       root     6156:   bcopy (buf, copy, length);
                   6157:   copy[length] = 0;
                   6158:   SKIP_WHITE_SPACE (copy);
1.1.1.3   root     6159:   warning ("#warning %s", copy);
1.1       root     6160:   return 0;
                   6161: }
                   6162: 
                   6163: /* Remember the name of the current file being read from so that we can
                   6164:    avoid ever including it again.  */
                   6165: 
                   6166: static int
                   6167: do_once ()
                   6168: {
                   6169:   int i;
                   6170:   FILE_BUF *ip = NULL;
                   6171: 
                   6172:   for (i = indepth; i >= 0; i--)
                   6173:     if (instack[i].fname != NULL) {
                   6174:       ip = &instack[i];
                   6175:       break;
                   6176:     }
                   6177: 
                   6178:   if (ip != NULL) {
                   6179:     struct file_name_list *new;
                   6180:     
                   6181:     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
                   6182:     new->next = dont_repeat_files;
                   6183:     dont_repeat_files = new;
                   6184:     new->fname = savestring (ip->fname);
                   6185:     new->control_macro = 0;
1.1.1.6 ! root     6186:     new->c_system_include_path = 0;
1.1       root     6187:   }
                   6188:   return 0;
                   6189: }
                   6190: 
                   6191: /* #ident has already been copied to the output file, so just ignore it.  */
                   6192: 
                   6193: static int
                   6194: do_ident (buf, limit)
                   6195:      U_CHAR *buf, *limit;
                   6196: {
1.1.1.6 ! root     6197:   FILE_BUF trybuf;
        !          6198:   int len;
        !          6199:   FILE_BUF *op = &outbuf;
        !          6200: 
1.1       root     6201:   /* Allow #ident in system headers, since that's not user's fault.  */
                   6202:   if (pedantic && !instack[indepth].system_header_p)
                   6203:     pedwarn ("ANSI C does not allow `#ident'");
1.1.1.6 ! root     6204: 
        !          6205:   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
        !          6206:   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
        !          6207:   bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
        !          6208:   limit = buf + (trybuf.bufp - trybuf.buf);
        !          6209:   len = (limit - buf);
        !          6210:   free (trybuf.buf);
        !          6211: 
        !          6212:   /* Output directive name.  */
        !          6213:   check_expand (op, 8);
        !          6214:   bcopy ("#ident ", op->bufp, 7);
        !          6215:   op->bufp += 7;
        !          6216: 
        !          6217:   /* Output the expanded argument line.  */
        !          6218:   check_expand (op, len);
        !          6219:   bcopy (buf, op->bufp, len);
        !          6220:   op->bufp += len;
        !          6221: 
1.1       root     6222:   return 0;
                   6223: }
                   6224: 
                   6225: /* #pragma and its argument line have already been copied to the output file.
1.1.1.4   root     6226:    Just check for some recognized pragmas that need validation here.  */
1.1       root     6227: 
                   6228: static int
                   6229: do_pragma (buf, limit)
                   6230:      U_CHAR *buf, *limit;
                   6231: {
                   6232:   while (*buf == ' ' || *buf == '\t')
                   6233:     buf++;
                   6234:   if (!strncmp (buf, "once", 4)) {
1.1.1.3   root     6235:     /* Allow #pragma once in system headers, since that's not the user's
                   6236:        fault.  */
                   6237:     if (!instack[indepth].system_header_p)
                   6238:       warning ("`#pragma once' is obsolete");
1.1       root     6239:     do_once ();
                   6240:   }
1.1.1.4   root     6241: 
                   6242:   if (!strncmp (buf, "implementation", 14)) {
                   6243:     /* Be quiet about `#pragma implementation' for a file only if it hasn't
                   6244:        been included yet.  */
                   6245:     struct file_name_list *ptr;
                   6246:     U_CHAR *p = buf + 14, *fname, *inc_fname;
                   6247:     SKIP_WHITE_SPACE (p);
                   6248:     if (*p == '\n' || *p != '\"')
                   6249:       return 0;
                   6250: 
                   6251:     fname = p + 1;
                   6252:     if (p = (U_CHAR *) index (fname, '\"'))
                   6253:       *p = '\0';
                   6254:     
                   6255:     for (ptr = all_include_files; ptr; ptr = ptr->next) {
                   6256:       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
                   6257:       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
                   6258:       if (inc_fname && !strcmp (inc_fname, fname))
                   6259:        warning ("`#pragma implementation' for `%s' appears after file is included",
                   6260:                 fname);
                   6261:     }
                   6262:   }
                   6263: 
1.1       root     6264:   return 0;
                   6265: }
                   6266: 
                   6267: #if 0
                   6268: /* This was a fun hack, but #pragma seems to start to be useful.
                   6269:    By failing to recognize it, we pass it through unchanged to cc1.  */
                   6270: 
                   6271: /*
                   6272:  * the behavior of the #pragma directive is implementation defined.
                   6273:  * this implementation defines it as follows.
                   6274:  */
                   6275: 
                   6276: static int
                   6277: do_pragma ()
                   6278: {
                   6279:   close (0);
                   6280:   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
                   6281:     goto nope;
                   6282:   close (1);
                   6283:   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
                   6284:     goto nope;
                   6285:   execl ("/usr/games/hack", "#pragma", 0);
                   6286:   execl ("/usr/games/rogue", "#pragma", 0);
                   6287:   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
                   6288:   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
                   6289: nope:
                   6290:   fatal ("You are in a maze of twisty compiler features, all different");
                   6291: }
                   6292: #endif
                   6293: 
                   6294: /* Just ignore #sccs, on systems where we define it at all.  */
                   6295: 
                   6296: static int
                   6297: do_sccs ()
                   6298: {
                   6299:   if (pedantic)
                   6300:     pedwarn ("ANSI C does not allow `#sccs'");
                   6301:   return 0;
                   6302: }
                   6303: 
                   6304: /*
                   6305:  * handle #if command by
                   6306:  *   1) inserting special `defined' keyword into the hash table
                   6307:  *     that gets turned into 0 or 1 by special_symbol (thus,
                   6308:  *     if the luser has a symbol called `defined' already, it won't
                   6309:  *      work inside the #if command)
                   6310:  *   2) rescan the input into a temporary output buffer
                   6311:  *   3) pass the output buffer to the yacc parser and collect a value
                   6312:  *   4) clean up the mess left from steps 1 and 2.
                   6313:  *   5) call conditional_skip to skip til the next #endif (etc.),
                   6314:  *      or not, depending on the value from step 3.
                   6315:  */
                   6316: 
                   6317: static int
                   6318: do_if (buf, limit, op, keyword)
                   6319:      U_CHAR *buf, *limit;
                   6320:      FILE_BUF *op;
                   6321:      struct directive *keyword;
                   6322: {
                   6323:   int value;
                   6324:   FILE_BUF *ip = &instack[indepth];
                   6325: 
                   6326:   value = eval_if_expression (buf, limit - buf);
1.1.1.4   root     6327:   conditional_skip (ip, value == 0, T_IF, NULL_PTR);
1.1       root     6328:   return 0;
                   6329: }
                   6330: 
                   6331: /*
                   6332:  * handle a #elif directive by not changing  if_stack  either.
                   6333:  * see the comment above do_else.
                   6334:  */
                   6335: 
                   6336: static int
                   6337: do_elif (buf, limit, op, keyword)
                   6338:      U_CHAR *buf, *limit;
                   6339:      FILE_BUF *op;
                   6340:      struct directive *keyword;
                   6341: {
                   6342:   int value;
                   6343:   FILE_BUF *ip = &instack[indepth];
                   6344: 
                   6345:   if (if_stack == instack[indepth].if_stack) {
                   6346:     error ("`#elif' not within a conditional");
                   6347:     return 0;
                   6348:   } else {
                   6349:     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
                   6350:       error ("`#elif' after `#else'");
                   6351:       fprintf (stderr, " (matches line %d", if_stack->lineno);
                   6352:       if (if_stack->fname != NULL && ip->fname != NULL &&
                   6353:          strcmp (if_stack->fname, ip->nominal_fname) != 0)
                   6354:        fprintf (stderr, ", file %s", if_stack->fname);
                   6355:       fprintf (stderr, ")\n");
                   6356:     }
                   6357:     if_stack->type = T_ELIF;
                   6358:   }
                   6359: 
                   6360:   if (if_stack->if_succeeded)
                   6361:     skip_if_group (ip, 0);
                   6362:   else {
                   6363:     value = eval_if_expression (buf, limit - buf);
                   6364:     if (value == 0)
                   6365:       skip_if_group (ip, 0);
                   6366:     else {
                   6367:       ++if_stack->if_succeeded;        /* continue processing input */
                   6368:       output_line_command (ip, op, 1, same_file);
                   6369:     }
                   6370:   }
                   6371:   return 0;
                   6372: }
                   6373: 
                   6374: /*
                   6375:  * evaluate a #if expression in BUF, of length LENGTH,
                   6376:  * then parse the result as a C expression and return the value as an int.
                   6377:  */
                   6378: static int
                   6379: eval_if_expression (buf, length)
                   6380:      U_CHAR *buf;
                   6381:      int length;
                   6382: {
                   6383:   FILE_BUF temp_obuf;
                   6384:   HASHNODE *save_defined;
                   6385:   int value;
                   6386: 
1.1.1.4   root     6387:   save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, 0, -1);
1.1       root     6388:   pcp_inside_if = 1;
                   6389:   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
                   6390:   pcp_inside_if = 0;
                   6391:   delete_macro (save_defined); /* clean up special symbol */
                   6392: 
                   6393:   value = parse_c_expression (temp_obuf.buf);
                   6394: 
                   6395:   free (temp_obuf.buf);
                   6396: 
                   6397:   return value;
                   6398: }
                   6399: 
                   6400: /*
                   6401:  * routine to handle ifdef/ifndef.  Try to look up the symbol,
                   6402:  * then do or don't skip to the #endif/#else/#elif depending
                   6403:  * on what directive is actually being processed.
                   6404:  */
                   6405: 
                   6406: static int
                   6407: do_xifdef (buf, limit, op, keyword)
                   6408:      U_CHAR *buf, *limit;
                   6409:      FILE_BUF *op;
                   6410:      struct directive *keyword;
                   6411: {
                   6412:   int skip;
                   6413:   FILE_BUF *ip = &instack[indepth];
                   6414:   U_CHAR *end; 
                   6415:   int start_of_file = 0;
                   6416:   U_CHAR *control_macro = 0;
                   6417: 
                   6418:   /* Detect a #ifndef at start of file (not counting comments).  */
                   6419:   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
                   6420:     U_CHAR *p = ip->buf;
                   6421:     while (p != directive_start) {
1.1.1.5   root     6422:       U_CHAR c = *p++;
1.1.1.4   root     6423:       if (is_space[c])
                   6424:        ;
                   6425:       else if (c == '/' && p != ip->bufp && *p == '*') {
                   6426:        /* Skip this comment.  */
                   6427:        int junk;
                   6428:        U_CHAR *save_bufp = ip->bufp;
                   6429:        ip->bufp = p + 1;
                   6430:        p = skip_to_end_of_comment (ip, &junk, 1);
                   6431:        ip->bufp = save_bufp;
                   6432:       } else {
1.1       root     6433:        goto fail;
                   6434:       }
                   6435:     }
                   6436:     /* If we get here, this conditional is the beginning of the file.  */
                   6437:     start_of_file = 1;
                   6438:   fail: ;
                   6439:   }
                   6440: 
                   6441:   /* Discard leading and trailing whitespace.  */
                   6442:   SKIP_WHITE_SPACE (buf);
                   6443:   while (limit != buf && is_hor_space[limit[-1]]) limit--;
                   6444: 
                   6445:   /* Find the end of the identifier at the beginning.  */
                   6446:   for (end = buf; is_idchar[*end]; end++);
                   6447: 
                   6448:   if (end == buf) {
                   6449:     skip = (keyword->type == T_IFDEF);
                   6450:     if (! traditional)
                   6451:       pedwarn (end == limit ? "`#%s' with no argument"
                   6452:               : "`#%s' argument starts with punctuation",
                   6453:               keyword->name);
                   6454:   } else {
                   6455:     HASHNODE *hp;
                   6456: 
                   6457:     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
                   6458:       pedwarn ("`#%s' argument starts with a digit", keyword->name);
                   6459:     else if (end != limit && !traditional)
                   6460:       pedwarn ("garbage at end of `#%s' argument", keyword->name);
                   6461: 
                   6462:     hp = lookup (buf, end-buf, -1);
                   6463: 
                   6464:     if (pcp_outfile) {
                   6465:       /* Output a precondition for this macro.  */
                   6466:       if (hp && hp->value.defn->predefined)
1.1.1.5   root     6467:        fprintf (pcp_outfile, "#define %s\n", hp->name);
1.1       root     6468:       else {
                   6469:        U_CHAR *cp = buf;
1.1.1.5   root     6470:        fprintf (pcp_outfile, "#undef ");
1.1       root     6471:        while (is_idchar[*cp]) /* Ick! */
                   6472:          fputc (*cp++, pcp_outfile);
                   6473:        putc ('\n', pcp_outfile);
                   6474:       }
                   6475:     }
                   6476: 
                   6477:     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
                   6478:     if (start_of_file && !skip) {
                   6479:       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
                   6480:       bcopy (buf, control_macro, end - buf);
                   6481:       control_macro[end - buf] = 0;
                   6482:     }
                   6483:   }
                   6484:   
                   6485:   conditional_skip (ip, skip, T_IF, control_macro);
                   6486:   return 0;
                   6487: }
                   6488: 
                   6489: /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
                   6490:    If this is a #ifndef starting at the beginning of a file,
                   6491:    CONTROL_MACRO is the macro name tested by the #ifndef.
                   6492:    Otherwise, CONTROL_MACRO is 0.  */
                   6493: 
                   6494: static void
                   6495: conditional_skip (ip, skip, type, control_macro)
                   6496:      FILE_BUF *ip;
                   6497:      int skip;
                   6498:      enum node_type type;
                   6499:      U_CHAR *control_macro;
                   6500: {
                   6501:   IF_STACK_FRAME *temp;
                   6502: 
                   6503:   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
                   6504:   temp->fname = ip->nominal_fname;
                   6505:   temp->lineno = ip->lineno;
                   6506:   temp->next = if_stack;
                   6507:   temp->control_macro = control_macro;
                   6508:   if_stack = temp;
                   6509: 
                   6510:   if_stack->type = type;
                   6511: 
                   6512:   if (skip != 0) {
                   6513:     skip_if_group (ip, 0);
                   6514:     return;
                   6515:   } else {
                   6516:     ++if_stack->if_succeeded;
                   6517:     output_line_command (ip, &outbuf, 1, same_file);
                   6518:   }
                   6519: }
                   6520: 
                   6521: /*
                   6522:  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
                   6523:  * leaves input ptr at the sharp sign found.
                   6524:  * If ANY is nonzero, return at next directive of any sort.
                   6525:  */
                   6526: static void
                   6527: skip_if_group (ip, any)
                   6528:      FILE_BUF *ip;
                   6529:      int any;
                   6530: {
                   6531:   register U_CHAR *bp = ip->bufp, *cp;
                   6532:   register U_CHAR *endb = ip->buf + ip->length;
                   6533:   struct directive *kt;
                   6534:   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
                   6535:   U_CHAR *beg_of_line = bp;
                   6536:   register int ident_length;
                   6537:   U_CHAR *ident, *after_ident;
                   6538: 
                   6539:   while (bp < endb) {
                   6540:     switch (*bp++) {
                   6541:     case '/':                  /* possible comment */
                   6542:       if (*bp == '\\' && bp[1] == '\n')
                   6543:        newline_fix (bp);
                   6544:       if (*bp == '*'
1.1.1.4   root     6545:          || (cplusplus_comments && *bp == '/')) {
1.1       root     6546:        ip->bufp = ++bp;
1.1.1.2   root     6547:        bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
1.1       root     6548:       }
                   6549:       break;
                   6550:     case '\"':
                   6551:     case '\'':
1.1.1.4   root     6552:       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
                   6553:                               NULL_PTR, NULL_PTR);
1.1       root     6554:       break;
                   6555:     case '\\':
                   6556:       /* Char after backslash loses its special meaning.  */
                   6557:       if (bp < endb) {
                   6558:        if (*bp == '\n')
                   6559:          ++ip->lineno;         /* But do update the line-count.  */
                   6560:        bp++;
                   6561:       }
                   6562:       break;
                   6563:     case '\n':
                   6564:       ++ip->lineno;
                   6565:       beg_of_line = bp;
                   6566:       break;
                   6567:     case '#':
                   6568:       ip->bufp = bp - 1;
                   6569: 
                   6570:       /* # keyword: a # must be first nonblank char on the line */
                   6571:       if (beg_of_line == 0)
                   6572:        break;
                   6573:       /* Scan from start of line, skipping whitespace, comments
                   6574:         and backslash-newlines, and see if we reach this #.
                   6575:         If not, this # is not special.  */
                   6576:       bp = beg_of_line;
1.1.1.6 ! root     6577:       /* If -traditional, require # to be at beginning of line.  */
        !          6578:       if (!traditional)
        !          6579:        while (1) {
        !          6580:          if (is_hor_space[*bp])
1.1       root     6581:            bp++;
1.1.1.6 ! root     6582:          else if (*bp == '\\' && bp[1] == '\n')
        !          6583:            bp += 2;
        !          6584:          else if (*bp == '/' && bp[1] == '*') {
        !          6585:            bp += 2;
        !          6586:            while (!(*bp == '*' && bp[1] == '/'))
        !          6587:              bp++;
        !          6588:            bp += 2;
        !          6589:          } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
        !          6590:            bp += 2;
        !          6591:            while (*bp++ != '\n') ;
        !          6592:          }
        !          6593:          else break;
        !          6594:        }
1.1       root     6595:       if (bp != ip->bufp) {
                   6596:        bp = ip->bufp + 1;      /* Reset bp to after the #.  */
                   6597:        break;
                   6598:       }
                   6599: 
                   6600:       bp = ip->bufp + 1;       /* Point after the '#' */
                   6601: 
                   6602:       /* Skip whitespace and \-newline.  */
                   6603:       while (1) {
                   6604:        if (is_hor_space[*bp])
                   6605:          bp++;
                   6606:        else if (*bp == '\\' && bp[1] == '\n')
                   6607:          bp += 2;
                   6608:        else if (*bp == '/' && bp[1] == '*') {
                   6609:          bp += 2;
                   6610:          while (!(*bp == '*' && bp[1] == '/')) {
                   6611:            if (*bp == '\n')
                   6612:              ip->lineno++;
                   6613:            bp++;
                   6614:          }
                   6615:          bp += 2;
1.1.1.4   root     6616:        } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
1.1       root     6617:          bp += 2;
                   6618:          while (*bp++ != '\n') ;
                   6619:         }
                   6620:        else break;
                   6621:       }
                   6622: 
                   6623:       cp = bp;
                   6624: 
                   6625:       /* Now find end of directive name.
                   6626:         If we encounter a backslash-newline, exchange it with any following
                   6627:         symbol-constituents so that we end up with a contiguous name.  */
                   6628: 
                   6629:       while (1) {
                   6630:        if (is_idchar[*bp])
                   6631:          bp++;
                   6632:        else {
                   6633:          if (*bp == '\\' && bp[1] == '\n')
                   6634:            name_newline_fix (bp);
                   6635:          if (is_idchar[*bp])
                   6636:            bp++;
                   6637:          else break;
                   6638:        }
                   6639:       }
                   6640:       ident_length = bp - cp;
                   6641:       ident = cp;
                   6642:       after_ident = bp;
                   6643: 
                   6644:       /* A line of just `#' becomes blank.  */
                   6645: 
                   6646:       if (ident_length == 0 && *after_ident == '\n') {
                   6647:        continue;
                   6648:       }
                   6649: 
                   6650:       if (ident_length == 0 || !is_idstart[*ident]) {
                   6651:        U_CHAR *p = ident;
                   6652:        while (is_idchar[*p]) {
                   6653:          if (*p < '0' || *p > '9')
                   6654:            break;
                   6655:          p++;
                   6656:        }
                   6657:        /* Handle # followed by a line number.  */
                   6658:        if (p != ident && !is_idchar[*p]) {
                   6659:          if (pedantic)
                   6660:            pedwarn ("`#' followed by integer");
                   6661:          continue;
                   6662:        }
                   6663: 
                   6664:        /* Avoid error for `###' and similar cases unless -pedantic.  */
                   6665:        if (p == ident) {
                   6666:          while (*p == '#' || is_hor_space[*p]) p++;
                   6667:          if (*p == '\n') {
                   6668:            if (pedantic && !lang_asm)
                   6669:              pedwarn ("invalid preprocessor directive");
                   6670:            continue;
                   6671:          }
                   6672:        }
                   6673: 
                   6674:        if (!lang_asm && pedantic)
                   6675:          pedwarn ("invalid preprocessor directive name");
                   6676:        continue;
                   6677:       }
                   6678: 
                   6679:       for (kt = directive_table; kt->length >= 0; kt++) {
                   6680:        IF_STACK_FRAME *temp;
                   6681:        if (ident_length == kt->length
                   6682:            && strncmp (cp, kt->name, kt->length) == 0) {
                   6683:          /* If we are asked to return on next directive, do so now.  */
                   6684:          if (any)
                   6685:            return;
                   6686: 
                   6687:          switch (kt->type) {
                   6688:          case T_IF:
                   6689:          case T_IFDEF:
                   6690:          case T_IFNDEF:
                   6691:            temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
                   6692:            temp->next = if_stack;
                   6693:            if_stack = temp;
                   6694:            temp->lineno = ip->lineno;
                   6695:            temp->fname = ip->nominal_fname;
                   6696:            temp->type = kt->type;
                   6697:            break;
                   6698:          case T_ELSE:
                   6699:          case T_ENDIF:
                   6700:            if (pedantic && if_stack != save_if_stack)
                   6701:              validate_else (bp);
                   6702:          case T_ELIF:
                   6703:            if (if_stack == instack[indepth].if_stack) {
                   6704:              error ("`#%s' not within a conditional", kt->name);
                   6705:              break;
                   6706:            }
                   6707:            else if (if_stack == save_if_stack)
                   6708:              return;           /* found what we came for */
                   6709: 
                   6710:            if (kt->type != T_ENDIF) {
                   6711:              if (if_stack->type == T_ELSE)
                   6712:                error ("`#else' or `#elif' after `#else'");
                   6713:              if_stack->type = kt->type;
                   6714:              break;
                   6715:            }
                   6716: 
                   6717:            temp = if_stack;
                   6718:            if_stack = if_stack->next;
                   6719:            free (temp);
                   6720:            break;
                   6721:          }
                   6722:          break;
                   6723:        }
                   6724:       }
                   6725:       /* Don't let erroneous code go by.  */
                   6726:       if (kt->length < 0 && !lang_asm && pedantic)
                   6727:        pedwarn ("invalid preprocessor directive name");
                   6728:     }
                   6729:   }
                   6730:   ip->bufp = bp;
                   6731:   /* after this returns, rescan will exit because ip->bufp
                   6732:      now points to the end of the buffer.
                   6733:      rescan is responsible for the error message also.  */
                   6734: }
                   6735: 
                   6736: /*
                   6737:  * handle a #else directive.  Do this by just continuing processing
                   6738:  * without changing  if_stack ;  this is so that the error message
                   6739:  * for missing #endif's etc. will point to the original #if.  It
                   6740:  * is possible that something different would be better.
                   6741:  */
                   6742: 
                   6743: static int
                   6744: do_else (buf, limit, op, keyword)
                   6745:      U_CHAR *buf, *limit;
                   6746:      FILE_BUF *op;
                   6747:      struct directive *keyword;
                   6748: {
                   6749:   FILE_BUF *ip = &instack[indepth];
                   6750: 
                   6751:   if (pedantic) {
                   6752:     SKIP_WHITE_SPACE (buf);
                   6753:     if (buf != limit)
                   6754:       pedwarn ("text following `#else' violates ANSI standard");
                   6755:   }
                   6756: 
                   6757:   if (if_stack == instack[indepth].if_stack) {
                   6758:     error ("`#else' not within a conditional");
                   6759:     return 0;
                   6760:   } else {
                   6761:     /* #ifndef can't have its special treatment for containing the whole file
                   6762:        if it has a #else clause.  */
                   6763:     if_stack->control_macro = 0;
                   6764: 
                   6765:     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
                   6766:       error ("`#else' after `#else'");
                   6767:       fprintf (stderr, " (matches line %d", if_stack->lineno);
                   6768:       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
                   6769:        fprintf (stderr, ", file %s", if_stack->fname);
                   6770:       fprintf (stderr, ")\n");
                   6771:     }
                   6772:     if_stack->type = T_ELSE;
                   6773:   }
                   6774: 
                   6775:   if (if_stack->if_succeeded)
                   6776:     skip_if_group (ip, 0);
                   6777:   else {
                   6778:     ++if_stack->if_succeeded;  /* continue processing input */
                   6779:     output_line_command (ip, op, 1, same_file);
                   6780:   }
                   6781:   return 0;
                   6782: }
                   6783: 
                   6784: /*
                   6785:  * unstack after #endif command
                   6786:  */
                   6787: 
                   6788: static int
                   6789: do_endif (buf, limit, op, keyword)
                   6790:      U_CHAR *buf, *limit;
                   6791:      FILE_BUF *op;
                   6792:      struct directive *keyword;
                   6793: {
                   6794:   if (pedantic) {
                   6795:     SKIP_WHITE_SPACE (buf);
                   6796:     if (buf != limit)
                   6797:       pedwarn ("text following `#endif' violates ANSI standard");
                   6798:   }
                   6799: 
                   6800:   if (if_stack == instack[indepth].if_stack)
                   6801:     error ("unbalanced `#endif'");
                   6802:   else {
                   6803:     IF_STACK_FRAME *temp = if_stack;
                   6804:     if_stack = if_stack->next;
                   6805:     if (temp->control_macro != 0) {
                   6806:       /* This #endif matched a #ifndef at the start of the file.
                   6807:         See if it is at the end of the file.  */
                   6808:       FILE_BUF *ip = &instack[indepth];
                   6809:       U_CHAR *p = ip->bufp;
                   6810:       U_CHAR *ep = ip->buf + ip->length;
                   6811: 
                   6812:       while (p != ep) {
                   6813:        U_CHAR c = *p++;
                   6814:        switch (c) {
                   6815:        case ' ':
                   6816:        case '\t':
                   6817:        case '\n':
                   6818:          break;
                   6819:        case '/':
                   6820:          if (p != ep && *p == '*') {
                   6821:            /* Skip this comment.  */
                   6822:            int junk;
                   6823:            U_CHAR *save_bufp = ip->bufp;
                   6824:            ip->bufp = p + 1;
1.1.1.2   root     6825:            p = skip_to_end_of_comment (ip, &junk, 1);
1.1       root     6826:            ip->bufp = save_bufp;
                   6827:          }
                   6828:          break;
                   6829:        default:
                   6830:          goto fail;
                   6831:        }
                   6832:       }
                   6833:       /* If we get here, this #endif ends a #ifndef
                   6834:         that contains all of the file (aside from whitespace).
                   6835:         Arrange not to include the file again
                   6836:         if the macro that was tested is defined.  */
                   6837:       if (indepth != 0)
                   6838:        record_control_macro (ip->fname, temp->control_macro);
                   6839:     fail: ;
                   6840:     }
                   6841:     free (temp);
                   6842:     output_line_command (&instack[indepth], op, 1, same_file);
                   6843:   }
                   6844:   return 0;
                   6845: }
                   6846: 
                   6847: /* When an #else or #endif is found while skipping failed conditional,
                   6848:    if -pedantic was specified, this is called to warn about text after
                   6849:    the command name.  P points to the first char after the command name.  */
                   6850: 
                   6851: static void
                   6852: validate_else (p)
                   6853:      register U_CHAR *p;
                   6854: {
                   6855:   /* Advance P over whitespace and comments.  */
                   6856:   while (1) {
                   6857:     if (*p == '\\' && p[1] == '\n')
                   6858:       p += 2;
                   6859:     if (is_hor_space[*p])
                   6860:       p++;
                   6861:     else if (*p == '/') {
                   6862:       if (p[1] == '\\' && p[2] == '\n')
                   6863:        newline_fix (p + 1);
                   6864:       if (p[1] == '*') {
                   6865:        p += 2;
                   6866:        /* Don't bother warning about unterminated comments
                   6867:           since that will happen later.  Just be sure to exit.  */
                   6868:        while (*p) {
                   6869:          if (p[1] == '\\' && p[2] == '\n')
                   6870:            newline_fix (p + 1);
                   6871:          if (*p == '*' && p[1] == '/') {
                   6872:            p += 2;
                   6873:            break;
                   6874:          }
                   6875:          p++;
                   6876:        }
                   6877:       }
1.1.1.4   root     6878:       else if (cplusplus_comments && p[1] == '/') {
1.1       root     6879:        p += 2;
                   6880:        while (*p && *p++ != '\n') ;
                   6881:       }
                   6882:     } else break;
                   6883:   }
                   6884:   if (*p && *p != '\n')
                   6885:     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
                   6886: }
                   6887: 
1.1.1.2   root     6888: /* Skip a comment, assuming the input ptr immediately follows the
                   6889:    initial slash-star.  Bump *LINE_COUNTER for each newline.
                   6890:    (The canonical line counter is &ip->lineno.)
                   6891:    Don't use this routine (or the next one) if bumping the line
                   6892:    counter is not sufficient to deal with newlines in the string.
                   6893: 
                   6894:    If NOWARN is nonzero, don't warn about slash-star inside a comment.
                   6895:    This feature is useful when processing a comment that is going to be
                   6896:    processed or was processed at another point in the preprocessor,
                   6897:    to avoid a duplicate warning.  */
1.1       root     6898: static U_CHAR *
1.1.1.2   root     6899: skip_to_end_of_comment (ip, line_counter, nowarn)
1.1       root     6900:      register FILE_BUF *ip;
                   6901:      int *line_counter;                /* place to remember newlines, or NULL */
1.1.1.2   root     6902:      int nowarn;
1.1       root     6903: {
                   6904:   register U_CHAR *limit = ip->buf + ip->length;
                   6905:   register U_CHAR *bp = ip->bufp;
                   6906:   FILE_BUF *op = &outbuf;      /* JF */
                   6907:   int output = put_out_comments && !line_counter;
                   6908: 
                   6909:        /* JF this line_counter stuff is a crock to make sure the
                   6910:           comment is only put out once, no matter how many times
                   6911:           the comment is skipped.  It almost works */
                   6912:   if (output) {
                   6913:     *op->bufp++ = '/';
                   6914:     *op->bufp++ = '*';
                   6915:   }
1.1.1.4   root     6916:   if (cplusplus_comments && bp[-1] == '/') {
1.1       root     6917:     if (output) {
                   6918:       while (bp < limit)
                   6919:        if ((*op->bufp++ = *bp++) == '\n') {
                   6920:          bp--;
                   6921:          break;
                   6922:        }
                   6923:       op->bufp[-1] = '*';
                   6924:       *op->bufp++ = '/';
                   6925:       *op->bufp++ = '\n';
                   6926:     } else {
                   6927:       while (bp < limit) {
                   6928:        if (*bp++ == '\n') {
                   6929:          bp--;
                   6930:          break;
                   6931:        }
                   6932:       }
                   6933:     }
                   6934:     ip->bufp = bp;
                   6935:     return bp;
                   6936:   }
                   6937:   while (bp < limit) {
                   6938:     if (output)
                   6939:       *op->bufp++ = *bp;
                   6940:     switch (*bp++) {
                   6941:     case '/':
1.1.1.2   root     6942:       if (warn_comments && !nowarn && bp < limit && *bp == '*')
1.1       root     6943:        warning ("`/*' within comment");
                   6944:       break;
                   6945:     case '\n':
                   6946:       if (line_counter != NULL)
                   6947:        ++*line_counter;
                   6948:       if (output)
                   6949:        ++op->lineno;
                   6950:       break;
                   6951:     case '*':
                   6952:       if (*bp == '\\' && bp[1] == '\n')
                   6953:        newline_fix (bp);
                   6954:       if (*bp == '/') {
                   6955:         if (output)
                   6956:          *op->bufp++ = '/';
                   6957:        ip->bufp = ++bp;
                   6958:        return bp;
                   6959:       }
                   6960:       break;
                   6961:     }
                   6962:   }
                   6963:   ip->bufp = bp;
                   6964:   return bp;
                   6965: }
                   6966: 
                   6967: /*
                   6968:  * Skip over a quoted string.  BP points to the opening quote.
                   6969:  * Returns a pointer after the closing quote.  Don't go past LIMIT.
                   6970:  * START_LINE is the line number of the starting point (but it need
                   6971:  * not be valid if the starting point is inside a macro expansion).
                   6972:  *
                   6973:  * The input stack state is not changed.
                   6974:  *
                   6975:  * If COUNT_NEWLINES is nonzero, it points to an int to increment
                   6976:  * for each newline passed.
                   6977:  *
                   6978:  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
                   6979:  * if we pass a backslash-newline.
                   6980:  *
                   6981:  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
                   6982:  */
                   6983: static U_CHAR *
                   6984: skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
                   6985:      register U_CHAR *bp;
                   6986:      register U_CHAR *limit;
                   6987:      int start_line;
                   6988:      int *count_newlines;
                   6989:      int *backslash_newlines_p;
                   6990:      int *eofp;
                   6991: {
                   6992:   register U_CHAR c, match;
                   6993: 
                   6994:   match = *bp++;
                   6995:   while (1) {
                   6996:     if (bp >= limit) {
                   6997:       error_with_line (line_for_error (start_line),
                   6998:                       "unterminated string or character constant");
1.1.1.5   root     6999:       error_with_line (multiline_string_line,
                   7000:                       "possible real start of unterminated constant");
                   7001:       multiline_string_line = 0;
1.1       root     7002:       if (eofp)
                   7003:        *eofp = 1;
                   7004:       break;
                   7005:     }
                   7006:     c = *bp++;
                   7007:     if (c == '\\') {
                   7008:       while (*bp == '\\' && bp[1] == '\n') {
                   7009:        if (backslash_newlines_p)
                   7010:          *backslash_newlines_p = 1;
                   7011:        if (count_newlines)
                   7012:          ++*count_newlines;
                   7013:        bp += 2;
                   7014:       }
                   7015:       if (*bp == '\n' && count_newlines) {
                   7016:        if (backslash_newlines_p)
                   7017:          *backslash_newlines_p = 1;
                   7018:        ++*count_newlines;
                   7019:       }
                   7020:       bp++;
                   7021:     } else if (c == '\n') {
                   7022:       if (traditional) {
                   7023:        /* Unterminated strings and character constants are 'legal'.  */
                   7024:        bp--;   /* Don't consume the newline. */
                   7025:        if (eofp)
                   7026:          *eofp = 1;
                   7027:        break;
                   7028:       }
1.1.1.5   root     7029:       if (pedantic || match == '\'') {
1.1       root     7030:        error_with_line (line_for_error (start_line),
1.1.1.5   root     7031:                         "unterminated string or character constant");
1.1       root     7032:        bp--;
                   7033:        if (eofp)
                   7034:          *eofp = 1;
                   7035:        break;
                   7036:       }
                   7037:       /* If not traditional, then allow newlines inside strings.  */
                   7038:       if (count_newlines)
                   7039:        ++*count_newlines;
1.1.1.5   root     7040:       if (multiline_string_line == 0)
                   7041:        multiline_string_line = start_line;
1.1       root     7042:     } else if (c == match)
                   7043:       break;
                   7044:   }
                   7045:   return bp;
                   7046: }
                   7047: 
1.1.1.6 ! root     7048: /* Place into DST a quoted string representing the string SRC.
        !          7049:    Return the address of DST's terminating null.  */
        !          7050: static char *
        !          7051: quote_string (dst, src)
        !          7052:      char *dst, *src;
        !          7053: {
        !          7054:   U_CHAR c;
        !          7055: 
        !          7056:   *dst++ = '\"';
        !          7057:   for (;;)
        !          7058:     switch ((c = *src++))
        !          7059:       {
        !          7060:       default:
        !          7061:         if (isprint (c))
        !          7062:          *dst++ = c;
        !          7063:        else
        !          7064:          {
        !          7065:            sprintf (dst, "\\%03o", c);
        !          7066:            dst += 4;
        !          7067:          }
        !          7068:        break;
        !          7069: 
        !          7070:       case '\"':
        !          7071:       case '\\':
        !          7072:        *dst++ = '\\';
        !          7073:        *dst++ = c;
        !          7074:        break;
        !          7075:       
        !          7076:       case '\0':
        !          7077:        *dst++ = '\"';
        !          7078:        *dst = '\0';
        !          7079:        return dst;
        !          7080:       }
        !          7081: }
        !          7082: 
1.1       root     7083: /* Skip across a group of balanced parens, starting from IP->bufp.
                   7084:    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
                   7085: 
                   7086:    This does not handle newlines, because it's used for the arg of #if,
1.1.1.2   root     7087:    where there aren't any newlines.  Also, backslash-newline can't appear.  */
1.1       root     7088: 
                   7089: static U_CHAR *
                   7090: skip_paren_group (ip)
                   7091:      register FILE_BUF *ip;
                   7092: {
                   7093:   U_CHAR *limit = ip->buf + ip->length;
                   7094:   U_CHAR *p = ip->bufp;
                   7095:   int depth = 0;
                   7096:   int lines_dummy = 0;
                   7097: 
                   7098:   while (p != limit) {
                   7099:     int c = *p++;
                   7100:     switch (c) {
                   7101:     case '(':
                   7102:       depth++;
                   7103:       break;
                   7104: 
                   7105:     case ')':
                   7106:       depth--;
                   7107:       if (depth == 0)
                   7108:        return ip->bufp = p;
                   7109:       break;
                   7110: 
                   7111:     case '/':
                   7112:       if (*p == '*') {
                   7113:        ip->bufp = p;
1.1.1.2   root     7114:        p = skip_to_end_of_comment (ip, &lines_dummy, 0);
1.1       root     7115:        p = ip->bufp;
                   7116:       }
                   7117: 
                   7118:     case '"':
                   7119:     case '\'':
                   7120:       {
                   7121:        int eofp = 0;
1.1.1.4   root     7122:        p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
1.1       root     7123:        if (eofp)
                   7124:          return ip->bufp = p;
                   7125:       }
                   7126:       break;
                   7127:     }
                   7128:   }
                   7129: 
                   7130:   ip->bufp = p;
                   7131:   return p;
                   7132: }
                   7133: 
                   7134: /*
                   7135:  * write out a #line command, for instance, after an #include file.
                   7136:  * If CONDITIONAL is nonzero, we can omit the #line if it would
                   7137:  * appear to be a no-op, and we can output a few newlines instead
                   7138:  * if we want to increase the line number by a small amount.
                   7139:  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
                   7140:  */
                   7141: 
                   7142: static void
                   7143: output_line_command (ip, op, conditional, file_change)
                   7144:      FILE_BUF *ip, *op;
                   7145:      int conditional;
                   7146:      enum file_change_code file_change;
                   7147: {
                   7148:   int len;
1.1.1.6 ! root     7149:   char *line_cmd_buf, *line_end;
1.1       root     7150: 
                   7151:   if (no_line_commands
                   7152:       || ip->fname == NULL
                   7153:       || no_output) {
                   7154:     op->lineno = ip->lineno;
                   7155:     return;
                   7156:   }
                   7157: 
                   7158:   if (conditional) {
                   7159:     if (ip->lineno == op->lineno)
                   7160:       return;
                   7161: 
                   7162:     /* If the inherited line number is a little too small,
                   7163:        output some newlines instead of a #line command.  */
                   7164:     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
                   7165:       check_expand (op, 10);
                   7166:       while (ip->lineno > op->lineno) {
                   7167:        *op->bufp++ = '\n';
                   7168:        op->lineno++;
                   7169:       }
                   7170:       return;
                   7171:     }
                   7172:   }
                   7173: 
1.1.1.2   root     7174:   /* Don't output a line number of 0 if we can help it.  */
                   7175:   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
                   7176:       && *ip->bufp == '\n') {
                   7177:     ip->lineno++;
                   7178:     ip->bufp++;
                   7179:   }
                   7180: 
1.1.1.6 ! root     7181:   line_cmd_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
1.1       root     7182: #ifdef OUTPUT_LINE_COMMANDS
1.1.1.6 ! root     7183:   sprintf (line_cmd_buf, "#line %d ", ip->lineno);
1.1       root     7184: #else
1.1.1.6 ! root     7185:   sprintf (line_cmd_buf, "# %d ", ip->lineno);
1.1       root     7186: #endif
1.1.1.6 ! root     7187:   line_end = quote_string (line_cmd_buf + strlen (line_cmd_buf),
        !          7188:                           ip->nominal_fname);
        !          7189:   if (file_change != same_file) {
        !          7190:     *line_end++ = ' ';
        !          7191:     *line_end++ = file_change == enter_file ? '1' : '2';
        !          7192:   }
1.1       root     7193:   /* Tell cc1 if following text comes from a system header file.  */
1.1.1.6 ! root     7194:   if (ip->system_header_p) {
        !          7195:     *line_end++ = ' ';
        !          7196:     *line_end++ = '3';
        !          7197:   }
        !          7198: #ifndef NO_IMPLICIT_EXTERN_C
        !          7199:   /* Tell cc1plus if following text should be treated as C.  */
        !          7200:   if (ip->system_header_p == 2 && cplusplus) {
        !          7201:     *line_end++ = ' ';
        !          7202:     *line_end++ = '4';
        !          7203:   }
        !          7204: #endif
        !          7205:   *line_end++ = '\n';
        !          7206:   len = line_end - line_cmd_buf;
1.1       root     7207:   check_expand (op, len + 1);
                   7208:   if (op->bufp > op->buf && op->bufp[-1] != '\n')
                   7209:     *op->bufp++ = '\n';
                   7210:   bcopy (line_cmd_buf, op->bufp, len);
                   7211:   op->bufp += len;
                   7212:   op->lineno = ip->lineno;
                   7213: }
                   7214: 
                   7215: /* This structure represents one parsed argument in a macro call.
                   7216:    `raw' points to the argument text as written (`raw_length' is its length).
                   7217:    `expanded' points to the argument's macro-expansion
                   7218:    (its length is `expand_length').
                   7219:    `stringified_length' is the length the argument would have
                   7220:    if stringified.
                   7221:    `use_count' is the number of times this macro arg is substituted
                   7222:    into the macro.  If the actual use count exceeds 10, 
                   7223:    the value stored is 10.
                   7224:    `free1' and `free2', if nonzero, point to blocks to be freed
                   7225:    when the macro argument data is no longer needed.  */
                   7226: 
                   7227: struct argdata {
                   7228:   U_CHAR *raw, *expanded;
                   7229:   int raw_length, expand_length;
                   7230:   int stringified_length;
                   7231:   U_CHAR *free1, *free2;
                   7232:   char newlines;
                   7233:   char comments;
                   7234:   char use_count;
                   7235: };
                   7236: 
                   7237: /* Expand a macro call.
                   7238:    HP points to the symbol that is the macro being called.
                   7239:    Put the result of expansion onto the input stack
                   7240:    so that subsequent input by our caller will use it.
                   7241: 
                   7242:    If macro wants arguments, caller has already verified that
                   7243:    an argument list follows; arguments come from the input stack.  */
                   7244: 
                   7245: static void
                   7246: macroexpand (hp, op)
                   7247:      HASHNODE *hp;
                   7248:      FILE_BUF *op;
                   7249: {
                   7250:   int nargs;
                   7251:   DEFINITION *defn = hp->value.defn;
                   7252:   register U_CHAR *xbuf;
                   7253:   int xbuf_len;
                   7254:   int start_line = instack[indepth].lineno;
1.1.1.3   root     7255:   int rest_args, rest_zero;
1.1       root     7256: 
                   7257:   CHECK_DEPTH (return;);
                   7258: 
                   7259:   /* it might not actually be a macro.  */
                   7260:   if (hp->type != T_MACRO) {
                   7261:     special_symbol (hp, op);
                   7262:     return;
                   7263:   }
                   7264: 
                   7265:   /* This macro is being used inside a #if, which means it must be */
                   7266:   /* recorded as a precondition.  */
                   7267:   if (pcp_inside_if && pcp_outfile && defn->predefined)
                   7268:     dump_single_macro (hp, pcp_outfile);
                   7269:   
                   7270:   nargs = defn->nargs;
                   7271: 
                   7272:   if (nargs >= 0) {
                   7273:     register int i;
                   7274:     struct argdata *args;
                   7275:     char *parse_error = 0;
                   7276: 
                   7277:     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
                   7278: 
                   7279:     for (i = 0; i < nargs; i++) {
1.1.1.5   root     7280:       args[i].raw = (U_CHAR *) "";
                   7281:       args[i].expanded = 0;
1.1       root     7282:       args[i].raw_length = args[i].expand_length
                   7283:        = args[i].stringified_length = 0;
                   7284:       args[i].free1 = args[i].free2 = 0;
                   7285:       args[i].use_count = 0;
                   7286:     }
                   7287: 
                   7288:     /* Parse all the macro args that are supplied.  I counts them.
                   7289:        The first NARGS args are stored in ARGS.
1.1.1.3   root     7290:        The rest are discarded.
                   7291:        If rest_args is set then we assume macarg absorbed the rest of the args.
                   7292:        */
1.1       root     7293:     i = 0;
1.1.1.3   root     7294:     rest_args = 0;
1.1       root     7295:     do {
                   7296:       /* Discard the open-parenthesis or comma before the next arg.  */
                   7297:       ++instack[indepth].bufp;
1.1.1.3   root     7298:       if (rest_args)
                   7299:        continue;
                   7300:       if (i < nargs || (nargs == 0 && i == 0)) {
1.1.1.4   root     7301:        /* if we are working on last arg which absorbs rest of args... */
1.1.1.3   root     7302:        if (i == nargs - 1 && defn->rest_args)
                   7303:          rest_args = 1;
                   7304:        parse_error = macarg (&args[i], rest_args);
                   7305:       }
                   7306:       else
1.1.1.4   root     7307:        parse_error = macarg (NULL_PTR, 0);
1.1       root     7308:       if (parse_error) {
                   7309:        error_with_line (line_for_error (start_line), parse_error);
                   7310:        break;
                   7311:       }
                   7312:       i++;
                   7313:     } while (*instack[indepth].bufp != ')');
                   7314: 
                   7315:     /* If we got one arg but it was just whitespace, call that 0 args.  */
                   7316:     if (i == 1) {
                   7317:       register U_CHAR *bp = args[0].raw;
                   7318:       register U_CHAR *lim = bp + args[0].raw_length;
1.1.1.5   root     7319:       /* cpp.texi says for foo ( ) we provide one argument.
                   7320:         However, if foo wants just 0 arguments, treat this as 0.  */
                   7321:       if (nargs == 0)
                   7322:        while (bp != lim && is_space[*bp]) bp++;
1.1       root     7323:       if (bp == lim)
                   7324:        i = 0;
                   7325:     }
                   7326: 
1.1.1.4   root     7327:     /* Don't output an error message if we have already output one for
                   7328:        a parse error above.  */
1.1.1.3   root     7329:     rest_zero = 0;
1.1.1.4   root     7330:     if (nargs == 0 && i > 0) {
                   7331:       if (! parse_error)
                   7332:        error ("arguments given to macro `%s'", hp->name);
                   7333:     } else if (i < nargs) {
1.1       root     7334:       /* traditional C allows foo() if foo wants one argument.  */
                   7335:       if (nargs == 1 && i == 0 && traditional)
                   7336:        ;
1.1.1.3   root     7337:       /* the rest args token is allowed to absorb 0 tokens */
                   7338:       else if (i == nargs - 1 && defn->rest_args)
                   7339:        rest_zero = 1;
1.1.1.4   root     7340:       else if (parse_error)
                   7341:        ;
1.1       root     7342:       else if (i == 0)
                   7343:        error ("macro `%s' used without args", hp->name);
                   7344:       else if (i == 1)
                   7345:        error ("macro `%s' used with just one arg", hp->name);
                   7346:       else
                   7347:        error ("macro `%s' used with only %d args", hp->name, i);
1.1.1.4   root     7348:     } else if (i > nargs) {
                   7349:       if (! parse_error)
                   7350:        error ("macro `%s' used with too many (%d) args", hp->name, i);
                   7351:     }
1.1       root     7352: 
                   7353:     /* Swallow the closeparen.  */
                   7354:     ++instack[indepth].bufp;
                   7355: 
                   7356:     /* If macro wants zero args, we parsed the arglist for checking only.
                   7357:        Read directly from the macro definition.  */
                   7358:     if (nargs == 0) {
                   7359:       xbuf = defn->expansion;
                   7360:       xbuf_len = defn->length;
                   7361:     } else {
                   7362:       register U_CHAR *exp = defn->expansion;
                   7363:       register int offset;     /* offset in expansion,
                   7364:                                   copied a piece at a time */
                   7365:       register int totlen;     /* total amount of exp buffer filled so far */
                   7366: 
1.1.1.3   root     7367:       register struct reflist *ap, *last_ap;
1.1       root     7368: 
                   7369:       /* Macro really takes args.  Compute the expansion of this call.  */
                   7370: 
                   7371:       /* Compute length in characters of the macro's expansion.
                   7372:         Also count number of times each arg is used.  */
                   7373:       xbuf_len = defn->length;
                   7374:       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
                   7375:        if (ap->stringify)
                   7376:          xbuf_len += args[ap->argno].stringified_length;
                   7377:        else if (ap->raw_before || ap->raw_after || traditional)
1.1.1.5   root     7378:          /* Add 4 for two newline-space markers to prevent
                   7379:             token concatenation.  */
                   7380:          xbuf_len += args[ap->argno].raw_length + 4;
                   7381:        else {
                   7382:          /* We have an ordinary (expanded) occurrence of the arg.
                   7383:             So compute its expansion, if we have not already.  */
                   7384:          if (args[ap->argno].expanded == 0) {
                   7385:            FILE_BUF obuf;
                   7386:            obuf = expand_to_temp_buffer (args[ap->argno].raw,
                   7387:                                          args[ap->argno].raw + args[ap->argno].raw_length,
                   7388:                                          1, 0);
                   7389: 
                   7390:            args[ap->argno].expanded = obuf.buf;
                   7391:            args[ap->argno].expand_length = obuf.length;
                   7392:            args[ap->argno].free2 = obuf.buf;
                   7393:          }
                   7394: 
                   7395:          /* Add 4 for two newline-space markers to prevent
                   7396:             token concatenation.  */
                   7397:          xbuf_len += args[ap->argno].expand_length + 4;
                   7398:        }
1.1       root     7399:        if (args[ap->argno].use_count < 10)
                   7400:          args[ap->argno].use_count++;
                   7401:       }
                   7402: 
                   7403:       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
                   7404: 
                   7405:       /* Generate in XBUF the complete expansion
                   7406:         with arguments substituted in.
                   7407:         TOTLEN is the total size generated so far.
                   7408:         OFFSET is the index in the definition
                   7409:         of where we are copying from.  */
                   7410:       offset = totlen = 0;
1.1.1.3   root     7411:       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
                   7412:           last_ap = ap, ap = ap->next) {
1.1       root     7413:        register struct argdata *arg = &args[ap->argno];
1.1.1.5   root     7414:        int count_before = totlen;
1.1       root     7415: 
1.1.1.5   root     7416:        /* Add chars to XBUF.  */
1.1.1.3   root     7417:        for (i = 0; i < ap->nchars; i++, offset++)
1.1.1.5   root     7418:          xbuf[totlen++] = exp[offset];
                   7419: 
                   7420:        /* If followed by an empty rest arg with concatenation,
                   7421:           delete the last run of nonwhite chars.  */
                   7422:        if (rest_zero && totlen > count_before
                   7423:            && ((ap->rest_args && ap->raw_before)
                   7424:                || (last_ap != NULL && last_ap->rest_args
                   7425:                    && last_ap->raw_after))) {
                   7426:          /* Delete final whitespace.  */
                   7427:          while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
                   7428:            totlen--;
                   7429:          }
                   7430: 
                   7431:          /* Delete the nonwhites before them.  */
                   7432:          while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
                   7433:            totlen--;
                   7434:          }
                   7435:        }
1.1       root     7436: 
                   7437:        if (ap->stringify != 0) {
                   7438:          int arglen = arg->raw_length;
                   7439:          int escaped = 0;
                   7440:          int in_string = 0;
                   7441:          int c;
                   7442:          i = 0;
                   7443:          while (i < arglen
                   7444:                 && (c = arg->raw[i], is_space[c]))
                   7445:            i++;
                   7446:          while (i < arglen
                   7447:                 && (c = arg->raw[arglen - 1], is_space[c]))
                   7448:            arglen--;
                   7449:          if (!traditional)
                   7450:            xbuf[totlen++] = '\"'; /* insert beginning quote */
                   7451:          for (; i < arglen; i++) {
                   7452:            c = arg->raw[i];
                   7453: 
                   7454:            /* Special markers Newline Space
                   7455:               generate nothing for a stringified argument.  */
                   7456:            if (c == '\n' && arg->raw[i+1] != '\n') {
                   7457:              i++;
                   7458:              continue;
                   7459:            }
                   7460: 
                   7461:            /* Internal sequences of whitespace are replaced by one space
                   7462:               except within an string or char token.  */
                   7463:            if (! in_string
                   7464:                && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
                   7465:              while (1) {
                   7466:                /* Note that Newline Space does occur within whitespace
                   7467:                   sequences; consider it part of the sequence.  */
                   7468:                if (c == '\n' && is_space[arg->raw[i+1]])
                   7469:                  i += 2;
                   7470:                else if (c != '\n' && is_space[c])
                   7471:                  i++;
                   7472:                else break;
                   7473:                c = arg->raw[i];
                   7474:              }
                   7475:              i--;
                   7476:              c = ' ';
                   7477:            }
                   7478: 
                   7479:            if (escaped)
                   7480:              escaped = 0;
                   7481:            else {
                   7482:              if (c == '\\')
                   7483:                escaped = 1;
                   7484:              if (in_string) {
                   7485:                if (c == in_string)
                   7486:                  in_string = 0;
                   7487:              } else if (c == '\"' || c == '\'')
                   7488:                in_string = c;
                   7489:            }
                   7490: 
                   7491:            /* Escape these chars */
                   7492:            if (c == '\"' || (in_string && c == '\\'))
                   7493:              xbuf[totlen++] = '\\';
                   7494:            if (isprint (c))
                   7495:              xbuf[totlen++] = c;
                   7496:            else {
                   7497:              sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
                   7498:              totlen += 4;
                   7499:            }
                   7500:          }
                   7501:          if (!traditional)
                   7502:            xbuf[totlen++] = '\"'; /* insert ending quote */
                   7503:        } else if (ap->raw_before || ap->raw_after || traditional) {
                   7504:          U_CHAR *p1 = arg->raw;
                   7505:          U_CHAR *l1 = p1 + arg->raw_length;
                   7506:          if (ap->raw_before) {
                   7507:            while (p1 != l1 && is_space[*p1]) p1++;
                   7508:            while (p1 != l1 && is_idchar[*p1])
                   7509:              xbuf[totlen++] = *p1++;
                   7510:            /* Delete any no-reexpansion marker that follows
                   7511:               an identifier at the beginning of the argument
                   7512:               if the argument is concatenated with what precedes it.  */
                   7513:            if (p1[0] == '\n' && p1[1] == '-')
                   7514:              p1 += 2;
1.1.1.5   root     7515:          } else if (!traditional) {
                   7516:          /* Ordinary expanded use of the argument.
                   7517:             Put in newline-space markers to prevent token pasting.  */
                   7518:            xbuf[totlen++] = '\n';
                   7519:            xbuf[totlen++] = ' ';
1.1       root     7520:          }
                   7521:          if (ap->raw_after) {
                   7522:            /* Arg is concatenated after: delete trailing whitespace,
                   7523:               whitespace markers, and no-reexpansion markers.  */
                   7524:            while (p1 != l1) {
                   7525:              if (is_space[l1[-1]]) l1--;
                   7526:              else if (l1[-1] == '-') {
                   7527:                U_CHAR *p2 = l1 - 1;
                   7528:                /* If a `-' is preceded by an odd number of newlines then it
                   7529:                   and the last newline are a no-reexpansion marker.  */
                   7530:                while (p2 != p1 && p2[-1] == '\n') p2--;
                   7531:                if ((l1 - 1 - p2) & 1) {
                   7532:                  l1 -= 2;
                   7533:                }
                   7534:                else break;
                   7535:              }
                   7536:              else break;
                   7537:            }
                   7538:          }
1.1.1.5   root     7539: 
1.1       root     7540:          bcopy (p1, xbuf + totlen, l1 - p1);
                   7541:          totlen += l1 - p1;
1.1.1.5   root     7542:          if (!traditional && !ap->raw_after) {
                   7543:            /* Ordinary expanded use of the argument.
                   7544:               Put in newline-space markers to prevent token pasting.  */
                   7545:            xbuf[totlen++] = '\n';
                   7546:            xbuf[totlen++] = ' ';
                   7547:          }
1.1       root     7548:        } else {
1.1.1.5   root     7549:          /* Ordinary expanded use of the argument.
                   7550:             Put in newline-space markers to prevent token pasting.  */
                   7551:          if (!traditional) {
                   7552:            xbuf[totlen++] = '\n';
                   7553:            xbuf[totlen++] = ' ';
                   7554:          }
1.1       root     7555:          bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
                   7556:          totlen += arg->expand_length;
1.1.1.5   root     7557:          if (!traditional) {
                   7558:            xbuf[totlen++] = '\n';
                   7559:            xbuf[totlen++] = ' ';
                   7560:          }
1.1       root     7561:          /* If a macro argument with newlines is used multiple times,
                   7562:             then only expand the newlines once.  This avoids creating output
                   7563:             lines which don't correspond to any input line, which confuses
                   7564:             gdb and gcov.  */
                   7565:          if (arg->use_count > 1 && arg->newlines > 0) {
1.1.1.5   root     7566:            /* Don't bother doing change_newlines for subsequent
1.1       root     7567:               uses of arg.  */
                   7568:            arg->use_count = 1;
                   7569:            arg->expand_length
1.1.1.5   root     7570:              = change_newlines (arg->expanded, arg->expand_length);
1.1       root     7571:          }
                   7572:        }
                   7573: 
                   7574:        if (totlen > xbuf_len)
                   7575:          abort ();
                   7576:       }
                   7577: 
                   7578:       /* if there is anything left of the definition
                   7579:         after handling the arg list, copy that in too. */
                   7580: 
1.1.1.3   root     7581:       for (i = offset; i < defn->length; i++) {
                   7582:        /* if we've reached the end of the macro */
                   7583:        if (exp[i] == ')')
                   7584:          rest_zero = 0;
                   7585:        if (! (rest_zero && last_ap != NULL && last_ap->rest_args
                   7586:               && last_ap->raw_after))
                   7587:          xbuf[totlen++] = exp[i];
                   7588:       }
1.1       root     7589: 
                   7590:       xbuf[totlen] = 0;
                   7591:       xbuf_len = totlen;
                   7592: 
                   7593:       for (i = 0; i < nargs; i++) {
                   7594:        if (args[i].free1 != 0)
                   7595:          free (args[i].free1);
                   7596:        if (args[i].free2 != 0)
                   7597:          free (args[i].free2);
                   7598:       }
                   7599:     }
                   7600:   } else {
                   7601:     xbuf = defn->expansion;
                   7602:     xbuf_len = defn->length;
                   7603:   }
                   7604: 
                   7605:   /* Now put the expansion on the input stack
                   7606:      so our caller will commence reading from it.  */
                   7607:   {
                   7608:     register FILE_BUF *ip2;
                   7609: 
                   7610:     ip2 = &instack[++indepth];
                   7611: 
                   7612:     ip2->fname = 0;
                   7613:     ip2->nominal_fname = 0;
                   7614:     ip2->lineno = 0;
                   7615:     ip2->buf = xbuf;
                   7616:     ip2->length = xbuf_len;
                   7617:     ip2->bufp = xbuf;
                   7618:     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
                   7619:     ip2->macro = hp;
                   7620:     ip2->if_stack = if_stack;
                   7621:     ip2->system_header_p = 0;
                   7622: 
                   7623:     /* Recursive macro use sometimes works traditionally.
1.1.1.5   root     7624:        #define foo(x,y) bar (x (y,0), y)
                   7625:        foo (foo, baz)  */
1.1       root     7626: 
                   7627:     if (!traditional)
                   7628:       hp->type = T_DISABLED;
                   7629:   }
                   7630: }
                   7631: 
                   7632: /*
                   7633:  * Parse a macro argument and store the info on it into *ARGPTR.
1.1.1.3   root     7634:  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
1.1       root     7635:  * Return nonzero to indicate a syntax error.
                   7636:  */
                   7637: 
                   7638: static char *
1.1.1.3   root     7639: macarg (argptr, rest_args)
1.1       root     7640:      register struct argdata *argptr;
1.1.1.3   root     7641:      int rest_args;
1.1       root     7642: {
                   7643:   FILE_BUF *ip = &instack[indepth];
                   7644:   int paren = 0;
                   7645:   int newlines = 0;
                   7646:   int comments = 0;
                   7647: 
                   7648:   /* Try to parse as much of the argument as exists at this
                   7649:      input stack level.  */
                   7650:   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
1.1.1.3   root     7651:                        &paren, &newlines, &comments, rest_args);
1.1       root     7652: 
                   7653:   /* If we find the end of the argument at this level,
                   7654:      set up *ARGPTR to point at it in the input stack.  */
                   7655:   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
                   7656:       && bp != ip->buf + ip->length) {
                   7657:     if (argptr != 0) {
                   7658:       argptr->raw = ip->bufp;
                   7659:       argptr->raw_length = bp - ip->bufp;
                   7660:       argptr->newlines = newlines;
                   7661:     }
                   7662:     ip->bufp = bp;
                   7663:   } else {
                   7664:     /* This input stack level ends before the macro argument does.
                   7665:        We must pop levels and keep parsing.
                   7666:        Therefore, we must allocate a temporary buffer and copy
                   7667:        the macro argument into it.  */
                   7668:     int bufsize = bp - ip->bufp;
                   7669:     int extra = newlines;
                   7670:     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
                   7671:     int final_start = 0;
                   7672: 
                   7673:     bcopy (ip->bufp, buffer, bufsize);
                   7674:     ip->bufp = bp;
                   7675:     ip->lineno += newlines;
                   7676: 
                   7677:     while (bp == ip->buf + ip->length) {
                   7678:       if (instack[indepth].macro == 0) {
                   7679:        free (buffer);
                   7680:        return "unterminated macro call";
                   7681:       }
                   7682:       ip->macro->type = T_MACRO;
                   7683:       if (ip->free_ptr)
                   7684:        free (ip->free_ptr);
                   7685:       ip = &instack[--indepth];
                   7686:       newlines = 0;
                   7687:       comments = 0;
                   7688:       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
1.1.1.3   root     7689:                    &newlines, &comments, rest_args);
1.1       root     7690:       final_start = bufsize;
                   7691:       bufsize += bp - ip->bufp;
                   7692:       extra += newlines;
                   7693:       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
                   7694:       bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
                   7695:       ip->bufp = bp;
                   7696:       ip->lineno += newlines;
                   7697:     }
                   7698: 
                   7699:     /* Now, if arg is actually wanted, record its raw form,
                   7700:        discarding comments and duplicating newlines in whatever
                   7701:        part of it did not come from a macro expansion.
                   7702:        EXTRA space has been preallocated for duplicating the newlines.
                   7703:        FINAL_START is the index of the start of that part.  */
                   7704:     if (argptr != 0) {
                   7705:       argptr->raw = buffer;
                   7706:       argptr->raw_length = bufsize;
                   7707:       argptr->free1 = buffer;
                   7708:       argptr->newlines = newlines;
                   7709:       argptr->comments = comments;
                   7710:       if ((newlines || comments) && ip->fname != 0)
                   7711:        argptr->raw_length
                   7712:          = final_start +
                   7713:            discard_comments (argptr->raw + final_start,
                   7714:                              argptr->raw_length - final_start,
                   7715:                              newlines);
                   7716:       argptr->raw[argptr->raw_length] = 0;
                   7717:       if (argptr->raw_length > bufsize + extra)
                   7718:        abort ();
                   7719:     }
                   7720:   }
                   7721: 
                   7722:   /* If we are not discarding this argument,
                   7723:      macroexpand it and compute its length as stringified.
                   7724:      All this info goes into *ARGPTR.  */
                   7725: 
                   7726:   if (argptr != 0) {
                   7727:     register U_CHAR *buf, *lim;
                   7728:     register int totlen;
                   7729: 
                   7730:     buf = argptr->raw;
                   7731:     lim = buf + argptr->raw_length;
                   7732: 
                   7733:     while (buf != lim && is_space[*buf])
                   7734:       buf++;
                   7735:     while (buf != lim && is_space[lim[-1]])
                   7736:       lim--;
                   7737:     totlen = traditional ? 0 : 2;      /* Count opening and closing quote.  */
                   7738:     while (buf != lim) {
                   7739:       register U_CHAR c = *buf++;
                   7740:       totlen++;
                   7741:       /* Internal sequences of whitespace are replaced by one space
                   7742:         in most cases, but not always.  So count all the whitespace
                   7743:         in case we need to keep it all.  */
                   7744: #if 0
                   7745:       if (is_space[c])
                   7746:        SKIP_ALL_WHITE_SPACE (buf);
                   7747:       else
                   7748: #endif
                   7749:       if (c == '\"' || c == '\\') /* escape these chars */
                   7750:        totlen++;
                   7751:       else if (!isprint (c))
                   7752:        totlen += 3;
                   7753:     }
                   7754:     argptr->stringified_length = totlen;
                   7755:   }
                   7756:   return 0;
                   7757: }
                   7758: 
                   7759: /* Scan text from START (inclusive) up to LIMIT (exclusive),
                   7760:    counting parens in *DEPTHPTR,
                   7761:    and return if reach LIMIT
                   7762:    or before a `)' that would make *DEPTHPTR negative
                   7763:    or before a comma when *DEPTHPTR is zero.
                   7764:    Single and double quotes are matched and termination
                   7765:    is inhibited within them.  Comments also inhibit it.
                   7766:    Value returned is pointer to stopping place.
                   7767: 
                   7768:    Increment *NEWLINES each time a newline is passed.
1.1.1.3   root     7769:    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
1.1       root     7770:    Set *COMMENTS to 1 if a comment is seen.  */
                   7771: 
                   7772: static U_CHAR *
1.1.1.3   root     7773: macarg1 (start, limit, depthptr, newlines, comments, rest_args)
1.1       root     7774:      U_CHAR *start;
                   7775:      register U_CHAR *limit;
                   7776:      int *depthptr, *newlines, *comments;
1.1.1.3   root     7777:      int rest_args;
1.1       root     7778: {
                   7779:   register U_CHAR *bp = start;
                   7780: 
                   7781:   while (bp < limit) {
                   7782:     switch (*bp) {
                   7783:     case '(':
                   7784:       (*depthptr)++;
                   7785:       break;
                   7786:     case ')':
                   7787:       if (--(*depthptr) < 0)
                   7788:        return bp;
                   7789:       break;
                   7790:     case '\\':
                   7791:       /* Traditionally, backslash makes following char not special.  */
                   7792:       if (bp + 1 < limit && traditional)
                   7793:        {
                   7794:          bp++;
                   7795:          /* But count source lines anyway.  */
                   7796:          if (*bp == '\n')
                   7797:            ++*newlines;
                   7798:        }
                   7799:       break;
                   7800:     case '\n':
                   7801:       ++*newlines;
                   7802:       break;
                   7803:     case '/':
                   7804:       if (bp[1] == '\\' && bp[2] == '\n')
                   7805:        newline_fix (bp + 1);
1.1.1.4   root     7806:       if (cplusplus_comments && bp[1] == '/') {
1.1       root     7807:        *comments = 1;
                   7808:        bp += 2;
                   7809:        while (bp < limit && *bp++ != '\n') ;
                   7810:        ++*newlines;
                   7811:        break;
                   7812:       }
                   7813:       if (bp[1] != '*' || bp + 1 >= limit)
                   7814:        break;
                   7815:       *comments = 1;
                   7816:       bp += 2;
                   7817:       while (bp + 1 < limit) {
                   7818:        if (bp[0] == '*'
                   7819:            && bp[1] == '\\' && bp[2] == '\n')
                   7820:          newline_fix (bp + 1);
                   7821:        if (bp[0] == '*' && bp[1] == '/')
                   7822:          break;
                   7823:        if (*bp == '\n') ++*newlines;
                   7824:        bp++;
                   7825:       }
                   7826:       break;
                   7827:     case '\'':
                   7828:     case '\"':
                   7829:       {
                   7830:        int quotec;
                   7831:        for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
                   7832:          if (*bp == '\\') {
                   7833:            bp++;
                   7834:            if (*bp == '\n')
                   7835:              ++*newlines;
                   7836:            while (*bp == '\\' && bp[1] == '\n') {
                   7837:              bp += 2;
                   7838:            }
                   7839:          } else if (*bp == '\n') {
                   7840:            ++*newlines;
                   7841:            if (quotec == '\'')
                   7842:              break;
                   7843:          }
                   7844:        }
                   7845:       }
                   7846:       break;
                   7847:     case ',':
1.1.1.3   root     7848:       /* if we've returned to lowest level and we aren't absorbing all args */
                   7849:       if ((*depthptr) == 0 && rest_args == 0)
1.1       root     7850:        return bp;
                   7851:       break;
                   7852:     }
                   7853:     bp++;
                   7854:   }
                   7855: 
                   7856:   return bp;
                   7857: }
                   7858: 
                   7859: /* Discard comments and duplicate newlines
                   7860:    in the string of length LENGTH at START,
                   7861:    except inside of string constants.
                   7862:    The string is copied into itself with its beginning staying fixed.  
                   7863: 
                   7864:    NEWLINES is the number of newlines that must be duplicated.
                   7865:    We assume that that much extra space is available past the end
                   7866:    of the string.  */
                   7867: 
                   7868: static int
                   7869: discard_comments (start, length, newlines)
                   7870:      U_CHAR *start;
                   7871:      int length;
                   7872:      int newlines;
                   7873: {
                   7874:   register U_CHAR *ibp;
                   7875:   register U_CHAR *obp;
                   7876:   register U_CHAR *limit;
                   7877:   register int c;
                   7878: 
                   7879:   /* If we have newlines to duplicate, copy everything
                   7880:      that many characters up.  Then, in the second part,
                   7881:      we will have room to insert the newlines
                   7882:      while copying down.
                   7883:      NEWLINES may actually be too large, because it counts
                   7884:      newlines in string constants, and we don't duplicate those.
                   7885:      But that does no harm.  */
                   7886:   if (newlines > 0) {
                   7887:     ibp = start + length;
                   7888:     obp = ibp + newlines;
                   7889:     limit = start;
                   7890:     while (limit != ibp)
                   7891:       *--obp = *--ibp;
                   7892:   }
                   7893: 
                   7894:   ibp = start + newlines;
                   7895:   limit = start + length + newlines;
                   7896:   obp = start;
                   7897: 
                   7898:   while (ibp < limit) {
                   7899:     *obp++ = c = *ibp++;
                   7900:     switch (c) {
                   7901:     case '\n':
                   7902:       /* Duplicate the newline.  */
                   7903:       *obp++ = '\n';
                   7904:       break;
                   7905: 
                   7906:     case '\\':
                   7907:       if (*ibp == '\n') {
                   7908:        obp--;
                   7909:        ibp++;
                   7910:       }
                   7911:       break;
                   7912: 
                   7913:     case '/':
                   7914:       if (*ibp == '\\' && ibp[1] == '\n')
                   7915:        newline_fix (ibp);
                   7916:       /* Delete any comment.  */
1.1.1.4   root     7917:       if (cplusplus_comments && ibp[0] == '/') {
1.1       root     7918:        obp--;
                   7919:        ibp++;
                   7920:        while (ibp < limit && *ibp++ != '\n') ;
                   7921:        break;
                   7922:       }
                   7923:       if (ibp[0] != '*' || ibp + 1 >= limit)
                   7924:        break;
                   7925:       obp--;
                   7926:       ibp++;
                   7927:       while (ibp + 1 < limit) {
                   7928:        if (ibp[0] == '*'
                   7929:            && ibp[1] == '\\' && ibp[2] == '\n')
                   7930:          newline_fix (ibp + 1);
                   7931:        if (ibp[0] == '*' && ibp[1] == '/')
                   7932:          break;
                   7933:        ibp++;
                   7934:       }
                   7935:       ibp += 2;
                   7936:       break;
                   7937: 
                   7938:     case '\'':
                   7939:     case '\"':
                   7940:       /* Notice and skip strings, so that we don't
                   7941:         think that comments start inside them,
                   7942:         and so we don't duplicate newlines in them.  */
                   7943:       {
                   7944:        int quotec = c;
                   7945:        while (ibp < limit) {
                   7946:          *obp++ = c = *ibp++;
                   7947:          if (c == quotec)
                   7948:            break;
                   7949:          if (c == '\n' && quotec == '\'')
                   7950:            break;
                   7951:          if (c == '\\' && ibp < limit) {
                   7952:            while (*ibp == '\\' && ibp[1] == '\n')
                   7953:              ibp += 2;
                   7954:            *obp++ = *ibp++;
                   7955:          }
                   7956:        }
                   7957:       }
                   7958:       break;
                   7959:     }
                   7960:   }
                   7961: 
                   7962:   return obp - start;
                   7963: }
                   7964: 
1.1.1.5   root     7965: /* Turn newlines to spaces in the string of length LENGTH at START,
                   7966:    except inside of string constants.
                   7967:    The string is copied into itself with its beginning staying fixed.  */
1.1       root     7968: 
                   7969: static int
1.1.1.5   root     7970: change_newlines (start, length)
1.1       root     7971:      U_CHAR *start;
                   7972:      int length;
                   7973: {
                   7974:   register U_CHAR *ibp;
                   7975:   register U_CHAR *obp;
                   7976:   register U_CHAR *limit;
                   7977:   register int c;
                   7978: 
                   7979:   ibp = start;
                   7980:   limit = start + length;
                   7981:   obp = start;
                   7982: 
                   7983:   while (ibp < limit) {
                   7984:     *obp++ = c = *ibp++;
                   7985:     switch (c) {
                   7986:     case '\n':
                   7987:       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
1.1.1.5   root     7988:         string.  Skip past the newline and its duplicate.
                   7989:         Put a space in the output.  */
1.1       root     7990:       if (*ibp == '\n')
                   7991:        {
                   7992:          ibp++;
                   7993:          obp--;
1.1.1.5   root     7994:          *obp++ = ' ';
1.1       root     7995:        }
                   7996:       break;
                   7997: 
                   7998:     case '\'':
                   7999:     case '\"':
                   8000:       /* Notice and skip strings, so that we don't delete newlines in them.  */
                   8001:       {
                   8002:        int quotec = c;
                   8003:        while (ibp < limit) {
                   8004:          *obp++ = c = *ibp++;
                   8005:          if (c == quotec)
                   8006:            break;
                   8007:          if (c == '\n' && quotec == '\'')
                   8008:            break;
                   8009:        }
                   8010:       }
                   8011:       break;
                   8012:     }
                   8013:   }
                   8014: 
                   8015:   return obp - start;
                   8016: }
                   8017: 
                   8018: /*
                   8019:  * error - print error message and increment count of errors.
                   8020:  */
                   8021: 
                   8022: void
                   8023: error (msg, arg1, arg2, arg3)
                   8024:      char *msg;
1.1.1.4   root     8025:      char *arg1, *arg2, *arg3;
1.1       root     8026: {
                   8027:   int i;
                   8028:   FILE_BUF *ip = NULL;
                   8029: 
                   8030:   print_containing_files ();
                   8031: 
                   8032:   for (i = indepth; i >= 0; i--)
                   8033:     if (instack[i].fname != NULL) {
                   8034:       ip = &instack[i];
                   8035:       break;
                   8036:     }
                   8037: 
                   8038:   if (ip != NULL)
                   8039:     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
                   8040:   fprintf (stderr, msg, arg1, arg2, arg3);
                   8041:   fprintf (stderr, "\n");
                   8042:   errors++;
                   8043: }
                   8044: 
                   8045: /* Error including a message from `errno'.  */
                   8046: 
                   8047: static void
                   8048: error_from_errno (name)
                   8049:      char *name;
                   8050: {
                   8051:   int i;
                   8052:   FILE_BUF *ip = NULL;
                   8053: 
                   8054:   print_containing_files ();
                   8055: 
                   8056:   for (i = indepth; i >= 0; i--)
                   8057:     if (instack[i].fname != NULL) {
                   8058:       ip = &instack[i];
                   8059:       break;
                   8060:     }
                   8061: 
                   8062:   if (ip != NULL)
                   8063:     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
                   8064: 
                   8065:   if (errno < sys_nerr)
                   8066:     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
                   8067:   else
                   8068:     fprintf (stderr, "%s: undocumented I/O error\n", name);
                   8069: 
                   8070:   errors++;
                   8071: }
                   8072: 
                   8073: /* Print error message but don't count it.  */
                   8074: 
                   8075: void
                   8076: warning (msg, arg1, arg2, arg3)
                   8077:      char *msg;
1.1.1.4   root     8078:      char *arg1, *arg2, *arg3;
1.1       root     8079: {
                   8080:   int i;
                   8081:   FILE_BUF *ip = NULL;
                   8082: 
                   8083:   if (inhibit_warnings)
                   8084:     return;
                   8085: 
                   8086:   if (warnings_are_errors)
                   8087:     errors++;
                   8088: 
                   8089:   print_containing_files ();
                   8090: 
                   8091:   for (i = indepth; i >= 0; i--)
                   8092:     if (instack[i].fname != NULL) {
                   8093:       ip = &instack[i];
                   8094:       break;
                   8095:     }
                   8096: 
                   8097:   if (ip != NULL)
                   8098:     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
                   8099:   fprintf (stderr, "warning: ");
                   8100:   fprintf (stderr, msg, arg1, arg2, arg3);
                   8101:   fprintf (stderr, "\n");
                   8102: }
                   8103: 
                   8104: static void
                   8105: error_with_line (line, msg, arg1, arg2, arg3)
                   8106:      int line;
                   8107:      char *msg;
1.1.1.4   root     8108:      char *arg1, *arg2, *arg3;
1.1       root     8109: {
                   8110:   int i;
                   8111:   FILE_BUF *ip = NULL;
                   8112: 
                   8113:   print_containing_files ();
                   8114: 
                   8115:   for (i = indepth; i >= 0; i--)
                   8116:     if (instack[i].fname != NULL) {
                   8117:       ip = &instack[i];
                   8118:       break;
                   8119:     }
                   8120: 
                   8121:   if (ip != NULL)
                   8122:     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
                   8123:   fprintf (stderr, msg, arg1, arg2, arg3);
                   8124:   fprintf (stderr, "\n");
                   8125:   errors++;
                   8126: }
                   8127: 
1.1.1.5   root     8128: static void
                   8129: warning_with_line (line, msg, arg1, arg2, arg3)
                   8130:      int line;
                   8131:      char *msg;
                   8132:      char *arg1, *arg2, *arg3;
                   8133: {
                   8134:   int i;
                   8135:   FILE_BUF *ip = NULL;
                   8136: 
                   8137:   if (inhibit_warnings)
                   8138:     return;
                   8139: 
                   8140:   if (warnings_are_errors)
                   8141:     errors++;
                   8142: 
                   8143:   print_containing_files ();
                   8144: 
                   8145:   for (i = indepth; i >= 0; i--)
                   8146:     if (instack[i].fname != NULL) {
                   8147:       ip = &instack[i];
                   8148:       break;
                   8149:     }
                   8150: 
                   8151:   if (ip != NULL)
                   8152:     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
                   8153:   fprintf (stderr, "warning: ");
                   8154:   fprintf (stderr, msg, arg1, arg2, arg3);
                   8155:   fprintf (stderr, "\n");
                   8156: }
                   8157: 
1.1       root     8158: /* print an error message and maybe count it.  */
                   8159: 
                   8160: void
                   8161: pedwarn (msg, arg1, arg2, arg3)
                   8162:      char *msg;
1.1.1.4   root     8163:      char *arg1, *arg2, *arg3;
1.1       root     8164: {
                   8165:   if (pedantic_errors)
                   8166:     error (msg, arg1, arg2, arg3);
                   8167:   else
                   8168:     warning (msg, arg1, arg2, arg3);
                   8169: }
                   8170: 
1.1.1.5   root     8171: void
                   8172: pedwarn_with_line (line, msg, arg1, arg2, arg3)
                   8173:      int line;
                   8174:      char *msg;
                   8175:      char *arg1, *arg2, *arg3;
                   8176: {
                   8177:   if (pedantic_errors)
                   8178:     error_with_line (line, msg, arg1, arg2, arg3);
                   8179:   else
                   8180:     warning_with_line (line, msg, arg1, arg2, arg3);
                   8181: }
                   8182: 
1.1       root     8183: /* Report a warning (or an error if pedantic_errors)
                   8184:    giving specified file name and line number, not current.  */
                   8185: 
                   8186: static void
                   8187: pedwarn_with_file_and_line (file, line, msg, arg1, arg2, arg3)
                   8188:      char *file;
                   8189:      int line;
                   8190:      char *msg;
1.1.1.4   root     8191:      char *arg1, *arg2, *arg3;
1.1       root     8192: {
                   8193:   int i;
                   8194:   if (!pedantic_errors && inhibit_warnings)
                   8195:     return;
                   8196:   if (file != NULL)
                   8197:     fprintf (stderr, "%s:%d: ", file, line);
1.1.1.5   root     8198:   if (pedantic_errors)
1.1       root     8199:     errors++;
                   8200:   if (!pedantic_errors)
                   8201:     fprintf (stderr, "warning: ");
                   8202:   fprintf (stderr, msg, arg1, arg2, arg3);
                   8203:   fprintf (stderr, "\n");
                   8204: }
                   8205: 
                   8206: /* Print the file names and line numbers of the #include
                   8207:    commands which led to the current file.  */
                   8208: 
                   8209: static void
                   8210: print_containing_files ()
                   8211: {
                   8212:   FILE_BUF *ip = NULL;
                   8213:   int i;
                   8214:   int first = 1;
                   8215: 
                   8216:   /* If stack of files hasn't changed since we last printed
                   8217:      this info, don't repeat it.  */
                   8218:   if (last_error_tick == input_file_stack_tick)
                   8219:     return;
                   8220: 
                   8221:   for (i = indepth; i >= 0; i--)
                   8222:     if (instack[i].fname != NULL) {
                   8223:       ip = &instack[i];
                   8224:       break;
                   8225:     }
                   8226: 
                   8227:   /* Give up if we don't find a source file.  */
                   8228:   if (ip == NULL)
                   8229:     return;
                   8230: 
                   8231:   /* Find the other, outer source files.  */
                   8232:   for (i--; i >= 0; i--)
                   8233:     if (instack[i].fname != NULL) {
                   8234:       ip = &instack[i];
                   8235:       if (first) {
                   8236:        first = 0;
                   8237:        fprintf (stderr, "In file included");
                   8238:       } else {
1.1.1.6 ! root     8239:        fprintf (stderr, ",\n                ");
1.1       root     8240:       }
                   8241: 
                   8242:       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
                   8243:     }
                   8244:   if (! first)
                   8245:     fprintf (stderr, ":\n");
                   8246: 
                   8247:   /* Record we have printed the status as of this time.  */
                   8248:   last_error_tick = input_file_stack_tick;
                   8249: }
                   8250: 
                   8251: /* Return the line at which an error occurred.
                   8252:    The error is not necessarily associated with the current spot
                   8253:    in the input stack, so LINE says where.  LINE will have been
                   8254:    copied from ip->lineno for the current input level.
                   8255:    If the current level is for a file, we return LINE.
                   8256:    But if the current level is not for a file, LINE is meaningless.
                   8257:    In that case, we return the lineno of the innermost file.  */
                   8258: 
                   8259: static int
                   8260: line_for_error (line)
                   8261:      int line;
                   8262: {
                   8263:   int i;
                   8264:   int line1 = line;
                   8265: 
                   8266:   for (i = indepth; i >= 0; ) {
                   8267:     if (instack[i].fname != 0)
                   8268:       return line1;
                   8269:     i--;
                   8270:     if (i < 0)
                   8271:       return 0;
                   8272:     line1 = instack[i].lineno;
                   8273:   }
                   8274:   abort ();
                   8275:   /*NOTREACHED*/
                   8276:   return 0;
                   8277: }
                   8278: 
                   8279: /*
                   8280:  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
                   8281:  *
                   8282:  * As things stand, nothing is ever placed in the output buffer to be
                   8283:  * removed again except when it's KNOWN to be part of an identifier,
                   8284:  * so flushing and moving down everything left, instead of expanding,
                   8285:  * should work ok.
                   8286:  */
                   8287: 
                   8288: /* You might think void was cleaner for the return type,
                   8289:    but that would get type mismatch in check_expand in strict ANSI.  */
                   8290: static int
                   8291: grow_outbuf (obuf, needed)
                   8292:      register FILE_BUF *obuf;
                   8293:      register int needed;
                   8294: {
                   8295:   register U_CHAR *p;
                   8296:   int minsize;
                   8297: 
                   8298:   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
                   8299:     return 0;
                   8300: 
                   8301:   /* Make it at least twice as big as it is now.  */
                   8302:   obuf->length *= 2;
                   8303:   /* Make it have at least 150% of the free space we will need.  */
                   8304:   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
                   8305:   if (minsize > obuf->length)
                   8306:     obuf->length = minsize;
                   8307: 
                   8308:   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
                   8309:     memory_full ();
                   8310: 
                   8311:   obuf->bufp = p + (obuf->bufp - obuf->buf);
                   8312:   obuf->buf = p;
                   8313: 
                   8314:   return 0;
                   8315: }
                   8316: 
                   8317: /* Symbol table for macro names and special symbols */
                   8318: 
                   8319: /*
                   8320:  * install a name in the main hash table, even if it is already there.
                   8321:  *   name stops with first non alphanumeric, except leading '#'.
                   8322:  * caller must check against redefinition if that is desired.
                   8323:  * delete_macro () removes things installed by install () in fifo order.
                   8324:  * this is important because of the `defined' special symbol used
                   8325:  * in #if, and also if pushdef/popdef directives are ever implemented.
                   8326:  *
                   8327:  * If LEN is >= 0, it is the length of the name.
                   8328:  * Otherwise, compute the length by scanning the entire name.
                   8329:  *
                   8330:  * If HASH is >= 0, it is the precomputed hash code.
                   8331:  * Otherwise, compute the hash code.
                   8332:  */
                   8333: static HASHNODE *
1.1.1.4   root     8334: install (name, len, type, ivalue, value, hash)
1.1       root     8335:      U_CHAR *name;
                   8336:      int len;
                   8337:      enum node_type type;
1.1.1.4   root     8338:      int ivalue;
                   8339:      char *value;
1.1       root     8340:      int hash;
                   8341: {
                   8342:   register HASHNODE *hp;
                   8343:   register int i, bucket;
                   8344:   register U_CHAR *p, *q;
                   8345: 
                   8346:   if (len < 0) {
                   8347:     p = name;
                   8348:     while (is_idchar[*p])
                   8349:       p++;
                   8350:     len = p - name;
                   8351:   }
                   8352: 
                   8353:   if (hash < 0)
                   8354:     hash = hashf (name, len, HASHSIZE);
                   8355: 
                   8356:   i = sizeof (HASHNODE) + len + 1;
                   8357:   hp = (HASHNODE *) xmalloc (i);
                   8358:   bucket = hash;
                   8359:   hp->bucket_hdr = &hashtab[bucket];
                   8360:   hp->next = hashtab[bucket];
                   8361:   hashtab[bucket] = hp;
                   8362:   hp->prev = NULL;
                   8363:   if (hp->next != NULL)
                   8364:     hp->next->prev = hp;
                   8365:   hp->type = type;
                   8366:   hp->length = len;
1.1.1.4   root     8367:   if (hp->type == T_CONST)
                   8368:     hp->value.ival = ivalue;
                   8369:   else
                   8370:     hp->value.cpval = value;
1.1       root     8371:   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
                   8372:   p = hp->name;
                   8373:   q = name;
                   8374:   for (i = 0; i < len; i++)
                   8375:     *p++ = *q++;
                   8376:   hp->name[len] = 0;
                   8377:   return hp;
                   8378: }
                   8379: 
                   8380: /*
                   8381:  * find the most recent hash node for name name (ending with first
                   8382:  * non-identifier char) installed by install
                   8383:  *
                   8384:  * If LEN is >= 0, it is the length of the name.
                   8385:  * Otherwise, compute the length by scanning the entire name.
                   8386:  *
                   8387:  * If HASH is >= 0, it is the precomputed hash code.
                   8388:  * Otherwise, compute the hash code.
                   8389:  */
                   8390: HASHNODE *
                   8391: lookup (name, len, hash)
                   8392:      U_CHAR *name;
                   8393:      int len;
                   8394:      int hash;
                   8395: {
                   8396:   register U_CHAR *bp;
                   8397:   register HASHNODE *bucket;
                   8398: 
                   8399:   if (len < 0) {
                   8400:     for (bp = name; is_idchar[*bp]; bp++) ;
                   8401:     len = bp - name;
                   8402:   }
                   8403: 
                   8404:   if (hash < 0)
                   8405:     hash = hashf (name, len, HASHSIZE);
                   8406: 
                   8407:   bucket = hashtab[hash];
                   8408:   while (bucket) {
                   8409:     if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
                   8410:       return bucket;
                   8411:     bucket = bucket->next;
                   8412:   }
                   8413:   return NULL;
                   8414: }
                   8415: 
                   8416: /*
                   8417:  * Delete a hash node.  Some weirdness to free junk from macros.
                   8418:  * More such weirdness will have to be added if you define more hash
                   8419:  * types that need it.
                   8420:  */
                   8421: 
                   8422: /* Note that the DEFINITION of a macro is removed from the hash table
                   8423:    but its storage is not freed.  This would be a storage leak
                   8424:    except that it is not reasonable to keep undefining and redefining
                   8425:    large numbers of macros many times.
                   8426:    In any case, this is necessary, because a macro can be #undef'd
                   8427:    in the middle of reading the arguments to a call to it.
                   8428:    If #undef freed the DEFINITION, that would crash.  */
                   8429: 
                   8430: static void
                   8431: delete_macro (hp)
                   8432:      HASHNODE *hp;
                   8433: {
                   8434: 
                   8435:   if (hp->prev != NULL)
                   8436:     hp->prev->next = hp->next;
                   8437:   if (hp->next != NULL)
                   8438:     hp->next->prev = hp->prev;
                   8439: 
                   8440:   /* make sure that the bucket chain header that
                   8441:      the deleted guy was on points to the right thing afterwards. */
                   8442:   if (hp == *hp->bucket_hdr)
                   8443:     *hp->bucket_hdr = hp->next;
                   8444: 
                   8445: #if 0
                   8446:   if (hp->type == T_MACRO) {
                   8447:     DEFINITION *d = hp->value.defn;
                   8448:     struct reflist *ap, *nextap;
                   8449: 
                   8450:     for (ap = d->pattern; ap != NULL; ap = nextap) {
                   8451:       nextap = ap->next;
                   8452:       free (ap);
                   8453:     }
                   8454:     free (d);
                   8455:   }
                   8456: #endif
                   8457:   free (hp);
                   8458: }
                   8459: 
                   8460: /*
                   8461:  * return hash function on name.  must be compatible with the one
                   8462:  * computed a step at a time, elsewhere
                   8463:  */
                   8464: static int
                   8465: hashf (name, len, hashsize)
                   8466:      register U_CHAR *name;
                   8467:      register int len;
                   8468:      int hashsize;
                   8469: {
                   8470:   register int r = 0;
                   8471: 
                   8472:   while (len--)
                   8473:     r = HASHSTEP (r, *name++);
                   8474: 
                   8475:   return MAKE_POS (r) % hashsize;
                   8476: }
                   8477: 
                   8478: 
                   8479: /* Dump the definition of a single macro HP to OF.  */
                   8480: static void
                   8481: dump_single_macro (hp, of)
                   8482:      register HASHNODE *hp;
                   8483:      FILE *of;
                   8484: {
                   8485:   register DEFINITION *defn = hp->value.defn;
                   8486:   struct reflist *ap;
                   8487:   int offset;
                   8488:   int concat;
                   8489: 
                   8490: 
                   8491:   /* Print the definition of the macro HP.  */
                   8492: 
                   8493:   fprintf (of, "#define %s", hp->name);
                   8494: 
                   8495:   if (defn->nargs >= 0) {
                   8496:     int i;
                   8497: 
                   8498:     fprintf (of, "(");
                   8499:     for (i = 0; i < defn->nargs; i++) {
                   8500:       dump_arg_n (defn, i, of);
                   8501:       if (i + 1 < defn->nargs)
                   8502:        fprintf (of, ", ");
                   8503:     }
                   8504:     fprintf (of, ")");
                   8505:   }
                   8506: 
                   8507:   fprintf (of, " ");
                   8508: 
                   8509:   offset = 0;
                   8510:   concat = 0;
                   8511:   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
                   8512:     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
                   8513:     if (ap->nchars != 0)
                   8514:       concat = 0;
                   8515:     offset += ap->nchars;
                   8516:     if (ap->stringify)
                   8517:       fprintf (of, " #");
                   8518:     if (ap->raw_before && !concat)
                   8519:       fprintf (of, " ## ");
                   8520:     concat = 0;
                   8521:     dump_arg_n (defn, ap->argno, of);
                   8522:     if (ap->raw_after) {
                   8523:       fprintf (of, " ## ");
                   8524:       concat = 1;
                   8525:     }
                   8526:   }
                   8527:   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
                   8528:   fprintf (of, "\n");
                   8529: }
                   8530: 
                   8531: /* Dump all macro definitions as #defines to stdout.  */
                   8532: 
                   8533: static void
                   8534: dump_all_macros ()
                   8535: {
                   8536:   int bucket;
                   8537: 
                   8538:   for (bucket = 0; bucket < HASHSIZE; bucket++) {
                   8539:     register HASHNODE *hp;
                   8540: 
                   8541:     for (hp = hashtab[bucket]; hp; hp= hp->next) {
                   8542:       if (hp->type == T_MACRO)
                   8543:        dump_single_macro (hp, stdout);
                   8544:     }
                   8545:   }
                   8546: }
                   8547: 
                   8548: /* Output to OF a substring of a macro definition.
                   8549:    BASE is the beginning of the definition.
                   8550:    Output characters START thru LENGTH.
                   8551:    Discard newlines outside of strings, thus
                   8552:    converting funny-space markers to ordinary spaces.  */
                   8553: 
                   8554: static void
                   8555: dump_defn_1 (base, start, length, of)
                   8556:      U_CHAR *base;
                   8557:      int start;
                   8558:      int length;
                   8559:      FILE *of;
                   8560: {
                   8561:   U_CHAR *p = base + start;
                   8562:   U_CHAR *limit = base + start + length;
                   8563: 
                   8564:   while (p < limit) {
                   8565:     if (*p != '\n')
                   8566:       putc (*p, of);
                   8567:     else if (*p == '\"' || *p =='\'') {
1.1.1.4   root     8568:       U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
                   8569:                                       NULL_PTR, NULL_PTR);
1.1       root     8570:       fwrite (p, p1 - p, 1, of);
                   8571:       p = p1 - 1;
                   8572:     }
                   8573:     p++;
                   8574:   }
                   8575: }
                   8576: 
                   8577: /* Print the name of argument number ARGNUM of macro definition DEFN
                   8578:    to OF.
                   8579:    Recall that DEFN->args.argnames contains all the arg names
                   8580:    concatenated in reverse order with comma-space in between.  */
                   8581: 
                   8582: static void
                   8583: dump_arg_n (defn, argnum, of)
                   8584:      DEFINITION *defn;
                   8585:      int argnum;
                   8586:      FILE *of;
                   8587: {
                   8588:   register U_CHAR *p = defn->args.argnames;
                   8589:   while (argnum + 1 < defn->nargs) {
                   8590:     p = (U_CHAR *) index (p, ' ') + 1;
                   8591:     argnum++;
                   8592:   }
                   8593: 
                   8594:   while (*p && *p != ',') {
                   8595:     putc (*p, of);
                   8596:     p++;
                   8597:   }
                   8598: }
                   8599: 
                   8600: /* Initialize syntactic classifications of characters.  */
                   8601: 
                   8602: static void
                   8603: initialize_char_syntax ()
                   8604: {
                   8605:   register int i;
                   8606: 
                   8607:   /*
                   8608:    * Set up is_idchar and is_idstart tables.  These should be
                   8609:    * faster than saying (is_alpha (c) || c == '_'), etc.
                   8610:    * Set up these things before calling any routines tthat
                   8611:    * refer to them.
                   8612:    */
                   8613:   for (i = 'a'; i <= 'z'; i++) {
                   8614:     is_idchar[i - 'a' + 'A'] = 1;
                   8615:     is_idchar[i] = 1;
                   8616:     is_idstart[i - 'a' + 'A'] = 1;
                   8617:     is_idstart[i] = 1;
                   8618:   }
                   8619:   for (i = '0'; i <= '9'; i++)
                   8620:     is_idchar[i] = 1;
                   8621:   is_idchar['_'] = 1;
                   8622:   is_idstart['_'] = 1;
                   8623:   is_idchar['$'] = dollars_in_ident;
                   8624:   is_idstart['$'] = dollars_in_ident;
                   8625: 
                   8626:   /* horizontal space table */
                   8627:   is_hor_space[' '] = 1;
                   8628:   is_hor_space['\t'] = 1;
                   8629:   is_hor_space['\v'] = 1;
                   8630:   is_hor_space['\f'] = 1;
                   8631:   is_hor_space['\r'] = 1;
                   8632: 
                   8633:   is_space[' '] = 1;
                   8634:   is_space['\t'] = 1;
                   8635:   is_space['\v'] = 1;
                   8636:   is_space['\f'] = 1;
                   8637:   is_space['\n'] = 1;
                   8638:   is_space['\r'] = 1;
                   8639: }
                   8640: 
                   8641: /* Initialize the built-in macros.  */
                   8642: 
                   8643: static void
                   8644: initialize_builtins (inp, outp)
                   8645:      FILE_BUF *inp;
                   8646:      FILE_BUF *outp;
                   8647: {
1.1.1.4   root     8648:   install ("__LINE__", -1, T_SPECLINE, 0, 0, -1);
                   8649:   install ("__DATE__", -1, T_DATE, 0, 0, -1);
                   8650:   install ("__FILE__", -1, T_FILE, 0, 0, -1);
                   8651:   install ("__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
                   8652:   install ("__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
                   8653:   install ("__VERSION__", -1, T_VERSION, 0, 0, -1);
1.1.1.5   root     8654: #ifndef NO_BUILTIN_SIZE_TYPE
1.1.1.4   root     8655:   install ("__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
1.1.1.5   root     8656: #endif
                   8657: #ifndef NO_BUILTIN_PTRDIFF_TYPE
1.1.1.4   root     8658:   install ("__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
1.1.1.5   root     8659: #endif
1.1.1.4   root     8660:   install ("__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
1.1.1.5   root     8661:   install ("__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
                   8662:   install ("__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
1.1.1.4   root     8663:   install ("__TIME__", -1, T_TIME, 0, 0, -1);
1.1       root     8664:   if (!traditional)
1.1.1.4   root     8665:     install ("__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
1.1       root     8666:   if (objc)
1.1.1.4   root     8667:     install ("__OBJC__", -1, T_CONST, 1, 0, -1);
1.1       root     8668: /*  This is supplied using a -D by the compiler driver
                   8669:     so that it is present only when truly compiling with GNU C.  */
1.1.1.4   root     8670: /*  install ("__GNUC__", -1, T_CONST, 2, 0, -1);  */
1.1       root     8671: 
                   8672:   if (debug_output)
                   8673:     {
                   8674:       char directive[2048];
                   8675:       register struct directive *dp = &directive_table[0];
1.1.1.3   root     8676:       struct tm *timebuf = timestamp ();
1.1       root     8677: 
1.1.1.3   root     8678:       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
1.1       root     8679:               instack[0].nominal_fname);
                   8680:       output_line_command (inp, outp, 0, same_file);
                   8681:       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
                   8682: 
1.1.1.3   root     8683:       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
1.1       root     8684:       output_line_command (inp, outp, 0, same_file);
                   8685:       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
                   8686: 
1.1.1.6 ! root     8687: #ifndef NO_BUILTIN_SIZE_TYPE
1.1.1.3   root     8688:       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
1.1       root     8689:       output_line_command (inp, outp, 0, same_file);
                   8690:       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
1.1.1.6 ! root     8691: #endif
1.1       root     8692: 
1.1.1.6 ! root     8693: #ifndef NO_BUILTIN_PTRDIFF_TYPE
1.1.1.3   root     8694:       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
1.1       root     8695:       output_line_command (inp, outp, 0, same_file);
                   8696:       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
1.1.1.6 ! root     8697: #endif
1.1       root     8698: 
1.1.1.3   root     8699:       sprintf (directive, " __WCHAR_TYPE__ %s\n", WCHAR_TYPE);
1.1       root     8700:       output_line_command (inp, outp, 0, same_file);
                   8701:       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
                   8702: 
1.1.1.3   root     8703:       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
1.1       root     8704:               monthnames[timebuf->tm_mon],
                   8705:               timebuf->tm_mday, timebuf->tm_year + 1900);
                   8706:       output_line_command (inp, outp, 0, same_file);
                   8707:       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
                   8708: 
1.1.1.3   root     8709:       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
1.1       root     8710:               timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
                   8711:       output_line_command (inp, outp, 0, same_file);
                   8712:       pass_thru_directive (directive, &directive[strlen (directive)], outp, dp);
                   8713: 
                   8714:       if (!traditional)
                   8715:        {
                   8716:           sprintf (directive, " __STDC__ 1");
                   8717:           output_line_command (inp, outp, 0, same_file);
                   8718:           pass_thru_directive (directive, &directive[strlen (directive)],
                   8719:                               outp, dp);
                   8720:        }
                   8721:       if (objc)
                   8722:        {
                   8723:           sprintf (directive, " __OBJC__ 1");
                   8724:           output_line_command (inp, outp, 0, same_file);
                   8725:           pass_thru_directive (directive, &directive[strlen (directive)],
                   8726:                               outp, dp);
                   8727:        }
                   8728:     }
                   8729: }
                   8730: 
                   8731: /*
                   8732:  * process a given definition string, for initialization
                   8733:  * If STR is just an identifier, define it with value 1.
                   8734:  * If STR has anything after the identifier, then it should
                   8735:  * be identifier=definition.
                   8736:  */
                   8737: 
                   8738: static void
                   8739: make_definition (str, op)
                   8740:      U_CHAR *str;
                   8741:      FILE_BUF *op;
                   8742: {
                   8743:   FILE_BUF *ip;
                   8744:   struct directive *kt;
                   8745:   U_CHAR *buf, *p;
                   8746: 
                   8747:   buf = str;
                   8748:   p = str;
                   8749:   if (!is_idstart[*p]) {
                   8750:     error ("malformed option `-D %s'", str);
                   8751:     return;
                   8752:   }
                   8753:   while (is_idchar[*++p])
                   8754:     ;
                   8755:   if (*p == 0) {
                   8756:     buf = (U_CHAR *) alloca (p - buf + 4);
                   8757:     strcpy ((char *)buf, str);
                   8758:     strcat ((char *)buf, " 1");
                   8759:   } else if (*p != '=') {
                   8760:     error ("malformed option `-D %s'", str);
                   8761:     return;
                   8762:   } else {
                   8763:     U_CHAR *q;
                   8764:     /* Copy the entire option so we can modify it.  */
                   8765:     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
                   8766:     strncpy (buf, str, p - str);
                   8767:     /* Change the = to a space.  */
                   8768:     buf[p - str] = ' ';
                   8769:     /* Scan for any backslash-newline and remove it.  */
                   8770:     p++;
                   8771:     q = &buf[p - str];
                   8772:     while (*p) {
                   8773:       if (*p == '\\' && p[1] == '\n')
                   8774:        p += 2;
                   8775:       /* Change newline chars into newline-markers.  */
                   8776:       else if (*p == '\n')
                   8777:        {
                   8778:          *q++ = '\n';
                   8779:          *q++ = '\n';
                   8780:          p++;
                   8781:        }
                   8782:       else
                   8783:        *q++ = *p++;
                   8784:     }
                   8785:     *q = 0;
                   8786:   }
                   8787:   
                   8788:   ip = &instack[++indepth];
                   8789:   ip->nominal_fname = ip->fname = "*Initialization*";
                   8790: 
                   8791:   ip->buf = ip->bufp = buf;
                   8792:   ip->length = strlen (buf);
                   8793:   ip->lineno = 1;
                   8794:   ip->macro = 0;
                   8795:   ip->free_ptr = 0;
                   8796:   ip->if_stack = if_stack;
                   8797:   ip->system_header_p = 0;
                   8798: 
                   8799:   for (kt = directive_table; kt->type != T_DEFINE; kt++)
                   8800:     ;
                   8801: 
1.1.1.5   root     8802:   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
                   8803:   do_define (buf, buf + strlen (buf), NULL, kt);
1.1       root     8804:   --indepth;
                   8805: }
                   8806: 
                   8807: /* JF, this does the work for the -U option */
                   8808: 
                   8809: static void
                   8810: make_undef (str, op)
                   8811:      U_CHAR *str;
                   8812:      FILE_BUF *op;
                   8813: {
                   8814:   FILE_BUF *ip;
                   8815:   struct directive *kt;
                   8816: 
                   8817:   ip = &instack[++indepth];
                   8818:   ip->nominal_fname = ip->fname = "*undef*";
                   8819: 
                   8820:   ip->buf = ip->bufp = str;
                   8821:   ip->length = strlen (str);
                   8822:   ip->lineno = 1;
                   8823:   ip->macro = 0;
                   8824:   ip->free_ptr = 0;
                   8825:   ip->if_stack = if_stack;
                   8826:   ip->system_header_p = 0;
                   8827: 
                   8828:   for (kt = directive_table; kt->type != T_UNDEF; kt++)
                   8829:     ;
                   8830: 
                   8831:   do_undef (str, str + strlen (str), op, kt);
                   8832:   --indepth;
                   8833: }
                   8834: 
                   8835: /* Process the string STR as if it appeared as the body of a #assert.
                   8836:    OPTION is the option name for which STR was the argument.  */
                   8837: 
                   8838: static void
                   8839: make_assertion (option, str)
                   8840:      char *option;
                   8841:      U_CHAR *str;
                   8842: {
                   8843:   FILE_BUF *ip;
                   8844:   struct directive *kt;
                   8845:   U_CHAR *buf, *p, *q;
                   8846: 
                   8847:   /* Copy the entire option so we can modify it.  */
                   8848:   buf = (U_CHAR *) alloca (strlen (str) + 1);
                   8849:   strcpy ((char *) buf, str);
                   8850:   /* Scan for any backslash-newline and remove it.  */
                   8851:   p = q = buf;
                   8852:   while (*p) {
                   8853:     if (*p == '\\' && p[1] == '\n')
                   8854:       p += 2;
                   8855:     else
                   8856:       *q++ = *p++;
                   8857:   }
                   8858:   *q = 0;
                   8859: 
                   8860:   p = buf;
                   8861:   if (!is_idstart[*p]) {
                   8862:     error ("malformed option `%s %s'", option, str);
                   8863:     return;
                   8864:   }
                   8865:   while (is_idchar[*++p])
                   8866:     ;
                   8867:   while (*p == ' ' || *p == '\t') p++;
                   8868:   if (! (*p == 0 || *p == '(')) {
                   8869:     error ("malformed option `%s %s'", option, str);
                   8870:     return;
                   8871:   }
                   8872:   
                   8873:   ip = &instack[++indepth];
                   8874:   ip->nominal_fname = ip->fname = "*Initialization*";
                   8875: 
                   8876:   ip->buf = ip->bufp = buf;
                   8877:   ip->length = strlen (buf);
                   8878:   ip->lineno = 1;
                   8879:   ip->macro = 0;
                   8880:   ip->free_ptr = 0;
                   8881:   ip->if_stack = if_stack;
                   8882:   ip->system_header_p = 0;
                   8883: 
                   8884:   for (kt = directive_table; kt->type != T_ASSERT; kt++)
                   8885:     ;
                   8886: 
                   8887:   /* pass NULL as output ptr to do_define since we KNOW it never
                   8888:      does any output.... */
1.1.1.4   root     8889:   do_assert (buf, buf + strlen (buf) , NULL_PTR, kt);
1.1       root     8890:   --indepth;
                   8891: }
                   8892: 
1.1.1.4   root     8893: /* Append a chain of `struct file_name_list's
                   8894:    to the end of the main include chain.
                   8895:    FIRST is the beginning of the chain to append, and LAST is the end.  */
                   8896: 
                   8897: static void
                   8898: append_include_chain (first, last)
                   8899:      struct file_name_list *first, *last;
                   8900: {
                   8901:   struct file_name_list *dir;
                   8902: 
                   8903:   if (!first || !last)
                   8904:     return;
                   8905: 
                   8906:   if (include == 0)
                   8907:     include = first;
                   8908:   else
                   8909:     last_include->next = first;
                   8910: 
                   8911:   if (first_bracket_include == 0)
                   8912:     first_bracket_include = first;
                   8913: 
                   8914:   for (dir = first; ; dir = dir->next) {
                   8915:     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
                   8916:     if (len > max_include_len)
                   8917:       max_include_len = len;
                   8918:     if (dir == last)
                   8919:       break;
                   8920:   }
                   8921: 
                   8922:   last->next = NULL;
                   8923:   last_include = last;
                   8924: }
                   8925: 
1.1       root     8926: /* Add output to `deps_buffer' for the -M switch.
                   8927:    STRING points to the text to be output.
                   8928:    SIZE is the number of bytes, or 0 meaning output until a null.
                   8929:    Outputting the empty string breaks the line if it is long enough.  */
                   8930: 
                   8931: static void
                   8932: deps_output (string, size)
                   8933:      char *string;
                   8934:      unsigned size;
                   8935: {
                   8936:   if (size == 0)
                   8937:     size = strlen (string);
                   8938: 
                   8939: #ifndef MAX_OUTPUT_COLUMNS
                   8940: #define MAX_OUTPUT_COLUMNS 75
                   8941: #endif
                   8942:   if (size == 0 && deps_column != 0
                   8943:       && size + deps_column > MAX_OUTPUT_COLUMNS) {
                   8944:     deps_output ("\\\n  ", 0);
                   8945:     deps_column = 0;
                   8946:   }
                   8947: 
                   8948:   if (deps_size + size + 1 > deps_allocated_size) {
                   8949:     deps_allocated_size = deps_size + size + 50;
                   8950:     deps_allocated_size *= 2;
                   8951:     deps_buffer = (char *) xrealloc (deps_buffer, deps_allocated_size);
                   8952:   }
                   8953:   bcopy (string, &deps_buffer[deps_size], size);
                   8954:   deps_size += size;
                   8955:   deps_column += size;
                   8956:   deps_buffer[deps_size] = 0;
                   8957: }
                   8958: 
                   8959: #if defined(USG) || defined(VMS)
                   8960: #ifndef BSTRING
                   8961: 
                   8962: void
                   8963: bzero (b, length)
                   8964:      register char *b;
                   8965:      register unsigned length;
                   8966: {
                   8967:   while (length-- > 0)
                   8968:     *b++ = 0;
                   8969: }
                   8970: 
                   8971: void
                   8972: bcopy (b1, b2, length)
                   8973:      register char *b1;
                   8974:      register char *b2;
                   8975:      register unsigned length;
                   8976: {
                   8977:   while (length-- > 0)
                   8978:     *b2++ = *b1++;
                   8979: }
                   8980: 
                   8981: int
                   8982: bcmp (b1, b2, length)  /* This could be a macro! */
                   8983:      register char *b1;
                   8984:      register char *b2;
                   8985:      register unsigned length;
                   8986: {
                   8987:    while (length-- > 0)
                   8988:      if (*b1++ != *b2++)
                   8989:        return 1;
                   8990: 
                   8991:    return 0;
                   8992: }
                   8993: #endif /* not BSTRING */
                   8994: #endif /* USG or VMS */
                   8995: 
                   8996: 
                   8997: static void
                   8998: fatal (str, arg)
                   8999:      char *str, *arg;
                   9000: {
                   9001:   fprintf (stderr, "%s: ", progname);
                   9002:   fprintf (stderr, str, arg);
                   9003:   fprintf (stderr, "\n");
                   9004:   exit (FAILURE_EXIT_CODE);
                   9005: }
                   9006: 
                   9007: /* More 'friendly' abort that prints the line and file.
                   9008:    config.h can #define abort fancy_abort if you like that sort of thing.  */
                   9009: 
                   9010: void
                   9011: fancy_abort ()
                   9012: {
                   9013:   fatal ("Internal gcc abort.");
                   9014: }
                   9015: 
                   9016: static void
                   9017: perror_with_name (name)
                   9018:      char *name;
                   9019: {
                   9020:   fprintf (stderr, "%s: ", progname);
                   9021:   if (errno < sys_nerr)
                   9022:     fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
                   9023:   else
                   9024:     fprintf (stderr, "%s: undocumented I/O error\n", name);
                   9025:   errors++;
                   9026: }
                   9027: 
                   9028: static void
                   9029: pfatal_with_name (name)
                   9030:      char *name;
                   9031: {
                   9032:   perror_with_name (name);
                   9033: #ifdef VMS
                   9034:   exit (vaxc$errno);
                   9035: #else
                   9036:   exit (FAILURE_EXIT_CODE);
                   9037: #endif
                   9038: }
                   9039: 
1.1.1.5   root     9040: /* Handler for SIGPIPE.  */
                   9041: 
                   9042: static void
                   9043: pipe_closed (signo)
                   9044:      /* If this is missing, some compilers complain.  */
                   9045:      int signo;
                   9046: {
                   9047:   fatal ("output pipe has been closed");
                   9048: }
1.1       root     9049: 
                   9050: static void
                   9051: memory_full ()
                   9052: {
                   9053:   fatal ("Memory exhausted.");
                   9054: }
                   9055: 
                   9056: 
                   9057: char *
                   9058: xmalloc (size)
                   9059:      unsigned size;
                   9060: {
                   9061:   register char *ptr = (char *) malloc (size);
                   9062:   if (ptr != 0) return (ptr);
                   9063:   memory_full ();
                   9064:   /*NOTREACHED*/
                   9065:   return 0;
                   9066: }
                   9067: 
                   9068: static char *
                   9069: xrealloc (old, size)
                   9070:      char *old;
                   9071:      unsigned size;
                   9072: {
                   9073:   register char *ptr = (char *) realloc (old, size);
                   9074:   if (ptr != 0) return (ptr);
                   9075:   memory_full ();
                   9076:   /*NOTREACHED*/
                   9077:   return 0;
                   9078: }
                   9079: 
                   9080: static char *
                   9081: xcalloc (number, size)
                   9082:      unsigned number, size;
                   9083: {
                   9084:   register unsigned total = number * size;
                   9085:   register char *ptr = (char *) malloc (total);
                   9086:   if (ptr != 0) {
                   9087:     if (total > 100)
                   9088:       bzero (ptr, total);
                   9089:     else {
                   9090:       /* It's not too long, so loop, zeroing by longs.
                   9091:         It must be safe because malloc values are always well aligned.  */
                   9092:       register long *zp = (long *) ptr;
                   9093:       register long *zl = (long *) (ptr + total - 4);
                   9094:       register int i = total - 4;
                   9095:       while (zp < zl)
                   9096:        *zp++ = 0;
                   9097:       if (i < 0)
                   9098:        i = 0;
                   9099:       while (i < total)
                   9100:        ptr[i++] = 0;
                   9101:     }
                   9102:     return ptr;
                   9103:   }
                   9104:   memory_full ();
                   9105:   /*NOTREACHED*/
                   9106:   return 0;
                   9107: }
                   9108: 
                   9109: static char *
                   9110: savestring (input)
                   9111:      char *input;
                   9112: {
                   9113:   unsigned size = strlen (input);
                   9114:   char *output = xmalloc (size + 1);
                   9115:   strcpy (output, input);
                   9116:   return output;
                   9117: }
                   9118: 
                   9119: /* Get the file-mode and data size of the file open on FD
                   9120:    and store them in *MODE_POINTER and *SIZE_POINTER.  */
                   9121: 
                   9122: static int
                   9123: file_size_and_mode (fd, mode_pointer, size_pointer)
                   9124:      int fd;
                   9125:      int *mode_pointer;
                   9126:      long int *size_pointer;
                   9127: {
                   9128:   struct stat sbuf;
                   9129: 
                   9130:   if (fstat (fd, &sbuf) < 0) return (-1);
                   9131:   if (mode_pointer) *mode_pointer = sbuf.st_mode;
                   9132:   if (size_pointer) *size_pointer = sbuf.st_size;
                   9133:   return 0;
                   9134: }
1.1.1.6 ! root     9135: 
        !          9136: static void
        !          9137: output_dots (fd, depth)
        !          9138:      FILE* fd;
        !          9139:      int depth;
        !          9140: {
        !          9141:   while (depth > 0) {
        !          9142:     putc ('.', fd);
        !          9143:     depth--;
        !          9144:   }
        !          9145: }
        !          9146:   
1.1       root     9147: 
                   9148: #ifdef VMS
                   9149: 
                   9150: /* Under VMS we need to fix up the "include" specification
                   9151:    filename so that everything following the 1st slash is
                   9152:    changed into its correct VMS file specification. */
                   9153: 
                   9154: static void
                   9155: hack_vms_include_specification (fname)
                   9156:      char *fname;
                   9157: {
                   9158:   register char *cp, *cp1, *cp2;
                   9159:   int f, check_filename_before_returning, no_prefix_seen;
                   9160:   char Local[512];
                   9161: 
                   9162:   check_filename_before_returning = 0;
                   9163:   no_prefix_seen = 0;
                   9164: 
                   9165:   /* Ignore leading "./"s */
                   9166:   while (fname[0] == '.' && fname[1] == '/') {
                   9167:     strcpy (fname, fname+2);
                   9168:     no_prefix_seen = 1;                /* mark this for later */
                   9169:   }
                   9170:   /* Look for the boundary between the VMS and UNIX filespecs */
                   9171:   cp = rindex (fname, ']');    /* Look for end of dirspec. */
                   9172:   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto              */
                   9173:   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
                   9174:   if (cp) {
                   9175:     cp++;
                   9176:   } else {
                   9177:     cp = index (fname, '/');   /* Look for the "/" */
                   9178:   }
                   9179: 
                   9180:   cp2 = Local;                 /* initialize */
                   9181: 
                   9182:   /* We are trying to do a number of things here.  First of all, we are
                   9183:      trying to hammer the filenames into a standard format, such that later
                   9184:      processing can handle them.
                   9185:      
                   9186:      If the file name contains something like [dir.], then it recognizes this
                   9187:      as a root, and strips the ".]".  Later processing will add whatever is
                   9188:      needed to get things working properly.
                   9189:      
                   9190:      If no device is specified, then the first directory name is taken to be
                   9191:      a device name (or a rooted logical). */
                   9192: 
                   9193:   /* See if we found that 1st slash */
                   9194:   if (cp == 0) return;         /* Nothing to do!!! */
                   9195:   if (*cp != '/') return;      /* Nothing to do!!! */
                   9196:   /* Point to the UNIX filename part (which needs to be fixed!) */
                   9197:   cp1 = cp+1;
                   9198:   /* If the directory spec is not rooted, we can just copy
                   9199:      the UNIX filename part and we are done */
                   9200:   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
                   9201:     if (cp[-2] != '.') {
                   9202:       /*
1.1.1.2   root     9203:        * The VMS part ends in a `]', and the preceding character is not a `.'.
1.1       root     9204:        * We strip the `]', and then splice the two parts of the name in the
                   9205:        * usual way.  Given the default locations for include files in cccp.c,
                   9206:        * we will only use this code if the user specifies alternate locations
                   9207:        * with the /include (-I) switch on the command line.  */
                   9208:       cp -= 1;                 /* Strip "]" */
                   9209:       cp1--;                   /* backspace */
                   9210:     } else {
                   9211:       /*
                   9212:        * The VMS part has a ".]" at the end, and this will not do.  Later
                   9213:        * processing will add a second directory spec, and this would be a syntax
                   9214:        * error.  Thus we strip the ".]", and thus merge the directory specs.
                   9215:        * We also backspace cp1, so that it points to a '/'.  This inhibits the
                   9216:        * generation of the 000000 root directory spec (which does not belong here
                   9217:        * in this case).
                   9218:        */
                   9219:       cp -= 2;                 /* Strip ".]" */
                   9220:       cp1--; };                        /* backspace */
                   9221:   } else {
                   9222: 
                   9223:     /* We drop in here if there is no VMS style directory specification yet.
                   9224:      * If there is no device specification either, we make the first dir a
                   9225:      * device and try that.  If we do not do this, then we will be essentially
                   9226:      * searching the users default directory (as if they did a #include "asdf.h").
                   9227:      *
                   9228:      * Then all we need to do is to push a '[' into the output string. Later
                   9229:      * processing will fill this in, and close the bracket.
                   9230:      */
1.1.1.5   root     9231:     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
1.1       root     9232:     *cp2++ = '[';              /* Open the directory specification */
                   9233:   }
                   9234: 
                   9235:   /* at this point we assume that we have the device spec, and (at least
                   9236:      the opening "[" for a directory specification.  We may have directories
                   9237:      specified already */
                   9238: 
                   9239:   /* If there are no other slashes then the filename will be
                   9240:      in the "root" directory.  Otherwise, we need to add
                   9241:      directory specifications. */
                   9242:   if (index (cp1, '/') == 0) {
                   9243:     /* Just add "000000]" as the directory string */
                   9244:     strcpy (cp2, "000000]");
                   9245:     cp2 += strlen (cp2);
                   9246:     check_filename_before_returning = 1; /* we might need to fool with this later */
                   9247:   } else {
                   9248:     /* As long as there are still subdirectories to add, do them. */
                   9249:     while (index (cp1, '/') != 0) {
                   9250:       /* If this token is "." we can ignore it */
                   9251:       if ((cp1[0] == '.') && (cp1[1] == '/')) {
                   9252:        cp1 += 2;
                   9253:        continue;
                   9254:       }
                   9255:       /* Add a subdirectory spec. Do not duplicate "." */
                   9256:       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
                   9257:        *cp2++ = '.';
                   9258:       /* If this is ".." then the spec becomes "-" */
                   9259:       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
                   9260:        /* Add "-" and skip the ".." */
                   9261:        *cp2++ = '-';
                   9262:        cp1 += 3;
                   9263:        continue;
                   9264:       }
                   9265:       /* Copy the subdirectory */
                   9266:       while (*cp1 != '/') *cp2++= *cp1++;
                   9267:       cp1++;                   /* Skip the "/" */
                   9268:     }
                   9269:     /* Close the directory specification */
1.1.1.5   root     9270:     if (cp2[-1] == '.')                /* no trailing periods */
1.1       root     9271:       cp2--;
                   9272:     *cp2++ = ']';
                   9273:   }
                   9274:   /* Now add the filename */
                   9275:   while (*cp1) *cp2++ = *cp1++;
                   9276:   *cp2 = 0;
                   9277:   /* Now append it to the original VMS spec. */
                   9278:   strcpy (cp, Local);
                   9279: 
                   9280:   /* If we put a [000000] in the filename, try to open it first. If this fails,
                   9281:      remove the [000000], and return that name.  This provides flexibility
                   9282:      to the user in that they can use both rooted and non-rooted logical names
                   9283:      to point to the location of the file.  */
                   9284: 
                   9285:   if (check_filename_before_returning && no_prefix_seen) {
                   9286:     f = open (fname, O_RDONLY, 0666);
                   9287:     if (f >= 0) {
                   9288:       /* The file name is OK as it is, so return it as is.  */
                   9289:       close (f);
                   9290:       return;
                   9291:     }
                   9292:     /* The filename did not work.  Try to remove the [000000] from the name,
                   9293:        and return it.  */
                   9294:     cp = index (fname, '[');
                   9295:     cp2 = index (fname, ']') + 1;
                   9296:     strcpy (cp, cp2);          /* this gets rid of it */
                   9297:   }
                   9298:   return;
                   9299: }
                   9300: #endif /* VMS */
                   9301: 
                   9302: #ifdef VMS
                   9303: 
                   9304: /* These are the read/write replacement routines for
                   9305:    VAX-11 "C".  They make read/write behave enough
                   9306:    like their UNIX counterparts that CCCP will work */
                   9307: 
                   9308: static int
                   9309: read (fd, buf, size)
                   9310:      int fd;
                   9311:      char *buf;
                   9312:      int size;
                   9313: {
                   9314: #undef read    /* Get back the REAL read routine */
                   9315:   register int i;
                   9316:   register int total = 0;
                   9317: 
                   9318:   /* Read until the buffer is exhausted */
                   9319:   while (size > 0) {
                   9320:     /* Limit each read to 32KB */
                   9321:     i = (size > (32*1024)) ? (32*1024) : size;
                   9322:     i = read (fd, buf, i);
                   9323:     if (i <= 0) {
                   9324:       if (i == 0) return (total);
1.1.1.5   root     9325:       return (i);
1.1       root     9326:     }
                   9327:     /* Account for this read */
                   9328:     total += i;
                   9329:     buf += i;
                   9330:     size -= i;
                   9331:   }
                   9332:   return (total);
                   9333: }
                   9334: 
                   9335: static int
                   9336: write (fd, buf, size)
                   9337:      int fd;
                   9338:      char *buf;
                   9339:      int size;
                   9340: {
                   9341: #undef write   /* Get back the REAL write routine */
                   9342:   int i;
                   9343:   int j;
                   9344: 
                   9345:   /* Limit individual writes to 32Kb */
                   9346:   i = size;
                   9347:   while (i > 0) {
                   9348:     j = (i > (32*1024)) ? (32*1024) : i;
                   9349:     if (write (fd, buf, j) < 0) return (-1);
                   9350:     /* Account for the data written */
                   9351:     buf += j;
                   9352:     i -= j;
                   9353:   }
                   9354:   return (size);
                   9355: }
                   9356: 
                   9357: /* The following wrapper functions supply additional arguments to the VMS
                   9358:    I/O routines to optimize performance with file handling.  The arguments
                   9359:    are:
                   9360:      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
                   9361:      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
                   9362:      "fop=tef"- Truncate unused portions of file when closing file.
                   9363:      "shr=nil"- Disallow file sharing while file is open.
                   9364:  */
                   9365: 
                   9366: static FILE *
                   9367: freopen (fname, type, oldfile)
                   9368:      char *fname;
                   9369:      char *type;
                   9370:      FILE *oldfile;
                   9371: {
                   9372: #undef freopen /* Get back the REAL fopen routine */
                   9373:   if (strcmp (type, "w") == 0)
                   9374:     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
                   9375:   return freopen (fname, type, oldfile, "mbc=16");
                   9376: }
                   9377: 
                   9378: static FILE *
                   9379: fopen (fname, type)
                   9380:      char *fname;
                   9381:      char *type;
                   9382: {
                   9383: #undef fopen   /* Get back the REAL fopen routine */
                   9384:   if (strcmp (type, "w") == 0)
                   9385:     return fopen (fname, type, "mbc=16", "deq=64", "fop=tef", "shr=nil");
                   9386:   return fopen (fname, type, "mbc=16");
                   9387: }
                   9388: 
                   9389: static int 
                   9390: open (fname, flags, prot)
                   9391:      char *fname;
                   9392:      int flags;
                   9393:      int prot;
                   9394: {
                   9395: #undef open    /* Get back the REAL open routine */
                   9396:   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
                   9397: }
                   9398: 
1.1.1.4   root     9399: /* Avoid run-time library bug, where copying M out of N+M characters with
                   9400:    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
                   9401:    gcc-cpp exercises this particular bug.  */
                   9402: 
                   9403: static char *
                   9404: strncat (dst, src, cnt)
                   9405:      char *dst;
                   9406:      const char *src;
                   9407:      unsigned cnt;
                   9408: {
                   9409:   register char *d = dst, *s = (char *) src;
                   9410:   register int n = cnt;        /* convert to _signed_ type */
                   9411: 
                   9412:   while (*d) d++;      /* advance to end */
                   9413:   while (--n >= 0)
                   9414:     if (!(*d++ = *s++)) break;
                   9415:   if (n < 0) *d = '\0';
                   9416:   return dst;
                   9417: }
1.1       root     9418: #endif /* VMS */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.