home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp / src / devtools / cpp / cpp1.c < prev    next >
Encoding:
Text File  |  1994-03-09  |  26.5 KB  |  803 lines

  1. /*
  2.  * CPP main program.
  3.  *
  4.  * Edit history
  5.  * 21-May-84    MM    "Field test" release
  6.  * 23-May-84    MM    Some minor hacks.
  7.  * 30-May-84    ARF    Didn't get enough memory for __DATE__
  8.  *            Added code to read stdin if no input
  9.  *            files are provided.
  10.  * 29-Jun-84    MM    Added ARF's suggestions, Unixifying cpp.
  11.  * 11-Jul-84    MM    "Official" first release (that's what I thought!)
  12.  * 22-Jul-84    MM/ARF/SCK Fixed line number bugs, added cpp recognition
  13.  *            of #line, fixed problems with #include.
  14.  * 23-Jul-84    MM    More (minor) include hacking, some documentation.
  15.  *            Also, redid cpp's #include files
  16.  * 25-Jul-84    MM    #line filename isn't used for #include searchlist
  17.  *            #line format is <number> <optional name>
  18.  * 25-Jul-84    ARF/MM    Various bugs, mostly serious.  Removed homemade doprint
  19.  * 01-Aug-84    MM    Fixed recursion bug, remove extra newlines and
  20.  *            leading whitespace from cpp output.
  21.  * 02-Aug-84    MM    Hacked (i.e. optimized) out blank lines and unneeded
  22.  *            whitespace in general.    Cleaned up unget()'s.
  23.  * 03-Aug-84    Keie    Several bug fixes from Ed Keizer, Vrije Universitet.
  24.  *            -- corrected arg. count in -D and pre-defined
  25.  *            macros.  Also, allow \n inside macro actual parameter
  26.  *            lists.
  27.  * 06-Aug-84    MM    If debugging, dump the preset vector at startup.
  28.  * 12-Aug-84    MM/SCK    Some small changes from Sam Kendall
  29.  * 15-Aug-84    Keie/MM cerror, cwarn, etc. take a single string arg.
  30.  *            cierror, etc. take a single int. arg.
  31.  *            changed LINE_PREFIX slightly so it can be
  32.  *            changed in the makefile.
  33.  * 31-Aug-84    MM    USENET net.sources release.
  34.  *  7-Sep-84    SCH/ado Lint complaints
  35.  * 10-Sep-84    Keie    Char's can't be signed in some implementations
  36.  * 11-Sep-84    ado    Added -C flag, pathological line number fix
  37.  * 13-Sep-84    ado    Added -E flag (does nothing) and "-" file for stdin.
  38.  * 14-Sep-84    MM    Allow # 123 as a synonym for #line 123
  39.  * 19-Sep-84    MM    scanid always reads to token, make sure #line is
  40.  *            written to a new line, even if -C switch given.
  41.  *            Also, cpp - - reads stdin, writes stdout.
  42.  * 03-Oct-84    ado/MM    Several changes to line counting and keepcomments
  43.  *            stuff.    Also a rewritten control() hasher -- much
  44.  *            simpler and no less "perfect". Note also changes
  45.  *            in cpp3.c to fix numeric scanning.
  46.  * 04-Oct-84    MM    Added recognition of macro formal parameters if
  47.  *            they are the only thing in a string, per the
  48.  *            draft standard.
  49.  * 08-Oct-84    MM    One more attack on scannumber
  50.  * 15-Oct-84    MM/ado    Added -N to disable predefined symbols.  Fixed
  51.  *            linecount if COMMENT_INVISIBLE enabled.
  52.  * 22-Oct-84    MM    Don't evaluate the #if/#ifdef argument if
  53.  *            compilation is supressed.  This prevents
  54.  *            unnecessary error messages in sequences such as
  55.  *                #ifdef FOO        -- undefined
  56.  *                #if FOO == 10    -- shouldn't print warning
  57.  * 25-Oct-84    MM    Fixed bug in false ifdef supression.  On vms,
  58.  *            #include <foo> should open foo.h -- this duplicates
  59.  *            the behavior of Vax-C
  60.  * 31-Oct-84    ado/MM    Parametized $ in indentifiers.    Added a better
  61.  *            token concatenator and took out the trial
  62.  *            concatenation code.  Also improved #ifdef code
  63.  *            and cleaned up the macro recursion tester.
  64.  *  2-Nov-84    MM/ado    Some bug fixes in token concatenation, also
  65.  *            a variety of minor (uninteresting) hacks.
  66.  *  6-Nov-84    MM    Happy Birthday.  Broke into 4 files and added
  67.  *            #if sizeof (basic_types)
  68.  *  9-Nov-84    MM    Added -S* for pointer type sizes
  69.  * 13-Nov-84    MM    Split cpp1.c, added vms defaulting
  70.  * 23-Nov-84    MM/ado    -E supresses error exit, added CPP_INCLUDE,
  71.  *            fixed strncpy bug.
  72.  *  3-Dec-84    ado/MM    Added OLD_PREPROCESSOR
  73.  *  7-Dec-84    MM    Stuff in Nov 12 Draft Standard
  74.  * 17-Dec-84    george    Fixed problems with recursive macros
  75.  * 17-Dec-84    MM    Yet another attack on #if's (f/t)level removed.
  76.  * 07-Jan-85    ado    Init defines before doing command line options
  77.  *            so -Uunix works.
  78.  * 21-Oct-85    RMS    Rename `token' to `tokenbuf'.
  79.  *            Allocate it dynamically, with size in `tokenbsize'.
  80.  * 23-Oct-85    RMS    Do not print message about number of errors.
  81.  * 14-Mar-85    FNF    Incorporate macro based C debugging package.
  82.  *            Port to Commodore AMIGA.
  83.  * 20-Aug-88    Ois    Changed format of documentation.
  84.  * 05-Oct-93    ch      Added -P option
  85.  */
  86.  
  87. /*)BUILD
  88.     $(PROGRAM)      = cpp
  89.     $(FILES)        = { cpp1 cpp2 cpp3 cpp4 cpp5 cpp6 }
  90.     $(INCLUDE)      = { cppdef.h cpp.h }
  91.     $(STACK)        = 2000
  92.     $(TKBOPTIONS)   = {
  93.         STACK    = 2000
  94.     }
  95. */
  96.  
  97. #ifdef    DOCUMENTATION
  98. .TH CPP 1 "Amiga Programmer's Manual" "1st PDC distribution"
  99. .SH NAME
  100. .bo "cpp
  101. - C Pre Processor. Macro-preprocess C programs.
  102. .SH SYNOPSIS
  103. .nf
  104. cpp [-options] [infile [outfile]]
  105. .fi
  106. .SH DESCRIPTION
  107. CPP reads a C source file, expands macros and include files, and writes an
  108. input file for the C compiler. If no file arguments are given, CPP reads
  109. from stdin and writes to stdout.  If one file argument is given, it will
  110. define the input file, while two file arguments define both input and
  111. output files.  The file name "-" is a synonym for stdin or stdout as
  112. appropriate.
  113.  
  114. The following options are supported.  Options may be given in either case.
  115. .in +5
  116. .ta +0
  117. .sp
  118. .ti -5
  119. -C@tIf set, source-file comments are written to the output file.  This
  120. allows the output of CPP to be used as the input to a program, such as
  121. lint, that expects commands embedded in specially-formatted comments.
  122. .sp
  123. .ti -5
  124. -Dname=value
  125. .br
  126. Define the name as if the programmer wrote
  127.  
  128.     #define name value
  129.  
  130. at the start of the first file.  If "=value" is not given, a value of "1"
  131. will be used.
  132.  
  133. On non-unix and non-amiga systems, all alphabetic text will be forced to
  134. upper-case.
  135. .sp
  136. .ti -5
  137. -E@tAlways return "success" to the operating system, even if errors were
  138. detected.  Note that some fatal errors, such as a missing #include file,
  139. will terminate CPP, returning "failure" even if the -E option is given.
  140. .sp
  141. .ti -5
  142. -Idirectory
  143. .br
  144. Add this directory to the list of directories searched for #include "..."
  145. and #include <...> commands.  Note that there is no space between the "-I"
  146. and the directory string.  More than one -I command is permitted.  On
  147. non-Unix systems "directory" is forced to upper-case.
  148. .sp
  149. .ti -5
  150. -N@tCPP normally predefines some symbols defining the target computer and
  151. operating system.  If -N is specified, no symbols will be predefined.  If
  152. -N -N is specified, the "always present" symbols, __LINE__, __FILE__,
  153. __TIME__ and __DATE__ are not defined.
  154. .sp
  155. .ti -5
  156. -Stext
  157. .br
  158. CPP normally assumes that the size of the target computer's basic variable
  159. types is the same as the size of these types of the host computer.  (This
  160. can be overridden when CPP is compiled, however.)  The -S option allows
  161. dynamic respecification of these values.  "text" is a string of numbers,
  162. separated by commas, that specifies correct sizes. The sizes must be
  163. specified in the exact order:
  164.  
  165.     char short int long float double
  166.  
  167. If you specify the option as "-S*text", pointers to these types will be
  168. specified.  -S* takes one additional argument for pointer to function (e.g.
  169. int (*)())
  170.  
  171. For example, to specify sizes appropriate for a PDP-11, you would write:
  172.  
  173.        c s i l f d func
  174.      -S1,2,2,2,4,8,
  175.     -S*2,2,2,2,2,2,2
  176.  
  177. Note that all values must be specified.
  178. .br;.it -1
  179. Note also that this is not allowed by the 11-Jan-88 Draft, and therefore
  180. has made into an option when compiling Cpp.
  181. .it 0
  182. .sp
  183. .ti -5
  184. -Uname
  185. .br
  186. Undefine the name as if
  187.  
  188.     #undef name
  189.  
  190. were given.  On non-Unix systems, "name" will be forced to upper-case.
  191. .sp
  192. .ti -5
  193. -Xnumber
  194. .br
  195. Enable debugging code.    If no value is given, a value of 1 will be used.
  196. (For maintenence of CPP only.)
  197. .in -5
  198. .SH Pre-Defined Variables
  199. When CPP begins processing, the following variables will have been defined
  200. (unless the -N option is specified):
  201. .sp
  202. Target computer (as appropriate):
  203. .sp
  204.     pdp11, vax, M68000 m68000 m68k, amiga
  205. .sp
  206. Target operating system (as appropriate):
  207. .sp
  208.     rsx, rt11, vms, unix, amigados
  209. .sp
  210. Target compiler (as appropriate):
  211. .sp
  212.     decus, vax11c, pdc PDC __PDC__
  213. .sp
  214. The implementor may add definitions to this list. The default definitions
  215. match the definition of the host computer, operating system, and C
  216. compiler.
  217. .sp
  218. The following are always available unless undefined (or -N was specified
  219. twice):
  220.  
  221. .in +16
  222. .ta
  223. .ta +0
  224. .ti -12
  225. __FILE__@tThe input (or #include) file being compiled (as a quoted string).
  226. .ti -12
  227. __LINE__@tThe line number being compiled.
  228. .ti -12
  229. __DATE__@tThe date of compilation as a quoted string of the format
  230. "Mmm dd yyyy".
  231. .ti -12
  232. __TIME__@tThe time of the start of compilation as a quoted string of the
  233. format "hh:mm:ss". Thus,
  234. .br
  235.     printf("Bug at line %s,", __LINE__);
  236.     printf(" source file %s", __FILE__);
  237.     printf("@ compiled@ on@ %s@ %s", __DATE__, __TIME__);
  238. .br
  239. .in -16
  240. .SH Supported directives
  241. .br;    #assert <expression>
  242. .br;    #define <token> <replacement>
  243. .br;    #elif <expression>
  244. .br;    #else <expression>
  245. .br;    #endif
  246. .br;    #error
  247. .br;    #if <expression>
  248. .br;    #ifdef <token>
  249. .br;    #ifndef <token>
  250. .br;    #include <filename>
  251. .br;    #line <number> <filename>
  252. .br;    #pragma <anything>
  253. .br;    #undef <token>
  254. .br;    #debug
  255. .br;    #nodebug
  256. .sp
  257. Unsupported # commands are copied verbatim into the output, so that maybe
  258. the compiler knows what to do with them.
  259. .SH Draft Proposed Ansi Standard Considerations
  260. The current version of the Draft Proposed Standard explicitly states that
  261. "readers are requested not to specify or claim conformance to this draft."
  262. Readers and users of Decus CPP should not assume that Decus CPP conforms to
  263. the standard, or that it will conform to the actual C Language Standard.
  264.  
  265. When CPP is itself compiled, many features of the Draft Proposed Standard
  266. that are incompatible with existing preprocessors may be disabled.  See the
  267. comments in CPP's source for details.
  268.  
  269. The latest version of the Draft Proposed Standard (as reflected in Decus
  270. CPP) is dated November 12, 1984, and bits from K&R V2 are put in by Ois.
  271.  
  272. Comments are removed from the input text.  The comment is replaced by a
  273. single space character.  The -C option preserves comments, writing them to
  274. the output file.
  275.  
  276. The '$' character is considered to be a letter.  This is a permitted
  277. extension.
  278.  
  279. The following new features of C are processed by CPP:
  280. .sp
  281. .* The following .br commands are there so the preprocessor
  282. .* does not get confused when compiling cpp1.c
  283. .nf
  284. .br;    #if, #elif
  285. .br;    #elif expression    (#else #if)
  286. .br;    '\xNNN'                 (Hexadecimal constant)
  287. .br;    '\a'                    (Ascii BELL)
  288. .br;    '\v'                    (Ascii Vertical Tab)
  289. .br;    #if defined NAME    1 if defined, 0 if not
  290. .br;    #if defined (NAME)      1 if defined, 0 if not
  291. .br;    #if sizeof (basic type)
  292. .br;    #error            Generates error message
  293. .br;    unary +
  294. .br;    123U, 123LU        Unsigned ints and longs.
  295. .br;    12.3L            Long double numbers
  296. .br;    token##token        Token concatenation in macros
  297. .br;    #macro-formal        String generation in macros
  298. .br;    #include token        Expands to filename
  299. .fi
  300.  
  301. The Draft Proposed Standard has extended C, adding a constant string
  302. concatenation operator, where
  303.  
  304.     "foo" "bar"
  305.  
  306. is regarded as the single string "foobar".  (This does not affect CPP's
  307. processing but does permit a limited form of macro argument substitution
  308. into strings as will be discussed.)
  309.  
  310. The Standard Committee plans to add token concatenation to #define command
  311. lines.    One suggested implementation is as follows:  the sequence
  312. "Token1 ## Token2" is treated as if the programmer wrote "Token1Token2".
  313.  
  314. This could be used as follows:
  315.  
  316.     #line 123
  317.     #define ATLINE foo ## __LINE__
  318.  
  319. ATLINE would be defined as foo123.
  320.  
  321. Note that "Token1" and "Token2" both must be valid tokens, but the
  322. concatenation of their expanded forms may not be a _single_ valid token. In
  323. that case, K&R state that the behaviour is undefined. So if you wish to
  324. make portable use of this facility, do not do the following:
  325.  
  326.     #define cat(x, y)   x ## y
  327.     cat(foo, 1.23)
  328.  
  329. which will produce
  330.  
  331.     foo1.23
  332.  
  333. and that is not a single token.
  334.  
  335. If the tokens T1 and T2 are concatenated into T3, this implementation
  336. operates as follows:
  337.  
  338.   1.@ Expand T1 if it is a macro.
  339.   2.@ Expand T2 if it is a macro.
  340.   3.@ Join the tokens, forming T3.
  341.   4.@ Rescan T3, looking for tokens, that may be expanded again.
  342.  
  343.       A macro formal parameter will be substituted into a string or
  344. character constant if it is the only component of that constant:
  345.  
  346.     #define VECSIZE 123
  347.     #define vprint(name, size) \
  348.       printf("name" "[" "size" "] = {\n")
  349.       ... vprint(vector, VECSIZE);
  350.  
  351. expands (effectively) to
  352.  
  353.       vprint("vector[123] = {\n");
  354.  
  355. Note that this will be useful if your C compiler supports the new string
  356. concatenation operation noted above. As implemented here, if you write
  357.  
  358.     #define string(arg) "arg"
  359.       ... string("foo") ...
  360.  
  361. This implementation generates "foo", rather than the strictly correct
  362. ""foo"" (which will probably generate an error message). This is, strictly
  363. speaking, an error in CPP and may be removed from future releases.
  364. .it -1
  365. Note that the above feature of replacing macro formals inside quotes is
  366. non-standard. The Committee thought the following to be better:
  367. .it 0
  368. .sp
  369. If a macro formal argument is immediately preceeded by a #, the argument
  370. supplied when expanding the macro will be surrounded by double quotes, and
  371. each double quote or backslash will be preceeded by a backslash. An example
  372. of this would be:
  373.  
  374.     #define string(arg) #arg
  375.       ... string(foo) ...
  376.  
  377. which produces "foo". Note that this feature is useless if you want your
  378. string to contain an odd number of quotes:
  379.  
  380.     string(Hello");
  381.  
  382. will produce an error message, complaining about unterminated strings.
  383.  
  384. .SH Error messages
  385. Many.  CPP prints warning or error messages if you try to use multiple-byte
  386. character constants (non-transportable) if you #undef a symbol that was not
  387. defined, or if your program has potentially nested comments.
  388. .SH Author
  389. Martin Minow, minor changes by RMS, FNF, OIS.
  390. .SH Bugs
  391. The #if expression processor uses signed integers only.
  392. I.e, #if 0xFFFFu < 0 may be TRUE.
  393. #endif    /* DOCUMENTATION */
  394.  
  395. #include    <stdio.h>
  396. #include    <ctype.h>
  397. #include    "cppdef.h"
  398. #include    "cpp.h"
  399.  
  400. #ifdef __STDC__
  401. FILE_LOCAL void cppmain(void);
  402. FILE_LOCAL void sharp(void);
  403. #endif
  404.  
  405. /*
  406.  * Commonly used global variables:
  407.  * line     is the current input line number.
  408.  * wrongline    is set in many places when the actual output
  409.  *        line is out of sync with the numbering, e.g,
  410.  *        when expanding a macro with an embedded newline.
  411.  *
  412.  * tokenbuf    holds the last identifier scanned (which might
  413.  *        be a candidate for macro expansion).
  414.  * errors    is the running cpp error counter.
  415.  * infile    is the head of a linked list of input files (extended by
  416.  *        #include and macros being expanded).  infile always points
  417.  *        to the current file/macro.  infile->parent to the includer,
  418.  *        etc.  infile->fd is NULL if this input stream is a macro.
  419.  */
  420. int        line;            /* Current line number        */
  421. int        wrongline;        /* Force #line to compiler    */
  422. char        *tokenbuf;        /* Buffer for current input token */
  423. int        tokenbsize;        /* Allocated size of tokenbuf, */
  424.                     /* not counting zero at end.  */
  425. int        errors;         /* cpp error counter        */
  426. FILEINFO    *infile = NULL;     /* Current input file        */
  427. #if DEBUG
  428. int        debug;            /* TRUE if debugging now    */
  429. #endif
  430. /*
  431.  * This counter is incremented when a macro expansion is initiated.
  432.  * If it exceeds a built-in value, the expansion stops -- this tests
  433.  * for a runaway condition:
  434.  *    #define X Y
  435.  *    #define Y X
  436.  *    X
  437.  * This can be disabled by falsifying rec_recover.  (Nothing does this
  438.  * currently: it is a hook for an eventual invocation flag.)
  439.  */
  440. int        recursion;        /* Infinite recursion counter    */
  441. int        rec_recover = TRUE;    /* Unwind recursive macros    */
  442.  
  443. /*
  444.  * instring is set TRUE when a string is scanned.  It modifies the
  445.  * behavior of the "get next character" routine, causing all characters
  446.  * to be passed to the caller (except <DEF_MAGIC>).  Note especially that
  447.  * comments and \<newline> are not removed from the source.  (This
  448.  * prevents cpp output lines from being arbitrarily long).
  449.  *
  450.  * inmacro is set by #define -- it absorbs comments and converts
  451.  * form-feed and vertical-tab to space, but returns \<newline>
  452.  * to the caller.  Strictly speaking, this is a bug as \<newline>
  453.  * shouldn't delimit tokens, but we'll worry about that some other
  454.  * time -- it is more important to prevent infinitly long output lines.
  455.  *
  456.  * instring and inmarcor are parameters to the get() routine which
  457.  * were made global for speed.
  458.  */
  459. int        instring = FALSE;    /* TRUE if scanning string    */
  460. int        inmacro = FALSE;    /* TRUE if #defining a macro    */
  461.  
  462. /*
  463.  * work[] and workp are used to store one piece of text in a temporay
  464.  * buffer.  To initialize storage, set workp = work.  To store one
  465.  * character, call save(c);  (This will fatally exit if there isn't
  466.  * room.)  To terminate the string, call save(EOS).  Note that
  467.  * the work buffer is used by several subroutines -- be sure your
  468.  * data won't be overwritten.  The extra byte in the allocation is
  469.  * needed for string formal replacement.
  470.  */
  471. char        work[NWORK + 1];    /* Work buffer            */
  472. char        *workp;         /* Work buffer pointer        */
  473.  
  474. /*
  475.  * keepcomments is set TRUE by the -C option.  If TRUE, comments
  476.  * are written directly to the output stream.  This is needed if
  477.  * the output from cpp is to be passed to lint (which uses commands
  478.  * embedded in comments).  cflag contains the permanent state of the
  479.  * -C flag.  keepcomments is always falsified when processing #control
  480.  * commands and when compilation is supressed by a false #if
  481.  *
  482.  * If eflag is set, CPP returns "success" even if non-fatal errors
  483.  * were detected.
  484.  *
  485.  * If nflag is non-zero, no symbols are predefined except __LINE__.
  486.  * __FILE__, and __DATE__.  If nflag > 1, absolutely no symbols
  487.  * are predefined.
  488.  */
  489. int        keepcomments = FALSE;    /* Write out comments flag    */
  490. int        cflag = FALSE;        /* -C option (keep comments)    */
  491. int        eflag = FALSE;        /* -E option (never fail)       */
  492. int        nflag = 0;        /* -N option (no predefines)    */
  493. int        pflag = FALSE;        /* -P option (no #line)         */
  494. int        wflag = FALSE;        /* -W option (write #defines)   */
  495.  
  496. /*
  497.  * ifstack[] holds information about nested #if's.  It is always
  498.  * accessed via *ifptr.  The information is as follows:
  499.  *    WAS_COMPILING    state of compiling flag at outer level.
  500.  *    ELSE_SEEN    set TRUE when #else seen to prevent 2nd #else.
  501.  *    TRUE_SEEN    set TRUE when #if or #elif succeeds
  502.  * ifstack[0] holds the compiling flag.  It is TRUE if compilation
  503.  * is currently enabled.  Note that this must be initialized TRUE.
  504.  */
  505. char        ifstack[BLK_NEST] = { TRUE };    /* #if information    */
  506. char        *ifptr = ifstack;        /* -> current ifstack[] */
  507.  
  508. /*
  509.  * incdir[] stores the -i directories (and the system-specific
  510.  * #include <...> directories.
  511.  */
  512. char    *incdir[NINCLUDE];        /* -i directories        */
  513. char    **incend = incdir;        /* -> free space in incdir[]    */
  514.  
  515. /*
  516.  * This is the table used to predefine target machine and operating
  517.  * system designators.    It may need hacking for specific circumstances.
  518.  * Note: it is not clear that this is part of the Ansi Standard.
  519.  * The -N option supresses preset definitions.
  520.  */
  521. char    *preset[] = {            /* names defined at cpp start    */
  522. #ifdef    MACHINE
  523.     MACHINE,
  524. #endif
  525. #ifdef    SYSTEM
  526.     SYSTEM,
  527. #endif
  528. #ifdef    COMPILER
  529.     COMPILER,
  530. #endif
  531. #if    DEBUG
  532.     "decus_cpp",                    /* Ourselves!                   */
  533. #endif
  534.     NULL                /* Must be last         */
  535. };
  536.  
  537. /*
  538.  * The value of these predefined symbols must be recomputed whenever
  539.  * they are evaluated.    The order must not be changed.
  540.  */
  541. char    *magic[] = {            /* Note: order is important    */
  542.     "__LINE__",
  543.     "__FILE__",
  544.     NULL                /* Must be last         */
  545. };
  546.  
  547. main(argc, argv)
  548. int        argc;
  549. char        *argv[];
  550. {
  551.     register int    i;
  552.     extern FILE *freopen ();
  553.  
  554.     DBUG_ENTER ("main");
  555.     DBUG_PUSH ("d:t");
  556. #if HOST == SYS_VMS
  557.     argc = getredirection(argc, argv);      /* vms >file and <file  */
  558. #endif
  559.     initdefines();                          /* O.S. specific def's  */
  560.     i = dooptions(argc, argv);              /* Command line -flags  */
  561.     switch (i) {
  562.     case 3:
  563.         /*
  564.          * Get output file, "-" means use stdout.
  565.          */
  566.         if (!streq(argv[2], "-")) {
  567. #if HOST == SYS_VMS
  568.         /*
  569.          * On vms, reopen stdout with "vanilla rms" attributes.
  570.          */
  571.         if ((i = creat(argv[2], 0, "rat=cr", "rfm=var")) == -1
  572.          || dup2(i, fileno(stdout)) == -1) {
  573. #else
  574.         if (freopen(argv[2], "w", stdout) == NULL) {
  575. #endif
  576.             perror(argv[2]);
  577.             cerror("Can't open output file \"%s\"", argv[2]);
  578.             exit(IO_ERROR);
  579.         }
  580.         }                /* Continue by opening input    */
  581.     case 2:             /* One file -> stdin        */
  582.         /*
  583.          * Open input file, "-" means use stdin.
  584.          */
  585.         if (!streq(argv[1], "-")) {
  586.         if (freopen(argv[1], "r", stdin) == NULL) {
  587.             perror(argv[1]);
  588.             cerror("Can't open input file \"%s\"", argv[1]);
  589.             exit(IO_ERROR);
  590.         }
  591.         strcpy(work, argv[1]);  /* Remember input filename      */
  592.         break;
  593.         }                /* Else, just get stdin     */
  594.     case 0:             /* No args?            */
  595.     case 1:             /* No files, stdin -> stdout    */
  596. #if HOST == SYS_UNIX || HOST == SYS_AMIGADOS
  597.         work[0] = EOS;        /* Unix can't find stdin name   */
  598. #else
  599.         fgetname(stdin, work);      /* Vax-11C, Decus C know name   */
  600. #endif
  601.         break;
  602.  
  603.     default:
  604.         exit(IO_ERROR);             /* Can't happen                 */
  605.     }
  606.     setincdirs();                   /* Setup -I include directories */
  607.     addfile(stdin, work);           /* "open" main input file       */
  608. #if DEBUG
  609.     if (debug > 0)
  610.         dumpdef("preset #define symbols");
  611. #endif
  612.     cppmain();                      /* Process main file            */
  613.     if ((i = (ifptr - &ifstack[0])) != 0) {
  614. #if OLD_PREPROCESSOR
  615.         ciwarn("Inside #ifdef block at end of input, depth = %d", i);
  616. #else
  617.         cierror("Inside #ifdef block at end of input, depth = %d", i);
  618. #endif
  619.     }
  620.     fflush(stdout);
  621.     fclose(stdout);
  622.     if (errors > 0 && !eflag)
  623.       DBUG_RETURN(IO_ERROR);
  624.     DBUG_RETURN(IO_NORMAL);         /* No errors or -E option set   */
  625. }
  626.  
  627. FILE_LOCAL void
  628. cppmain()
  629. /*
  630.  * Main process for cpp -- copies tokens from the current input
  631.  * stream (main file, include file, or a macro) to the output
  632.  * file.
  633.  */
  634. {
  635.     register int        c;        /* Current character    */
  636.     register int        counter;    /* newlines and spaces    */
  637.     extern void        output();       /* Output one character */
  638.  
  639.     DBUG_ENTER ("cppmain");
  640.     /* Initialize for reading tokens */
  641.     tokenbsize = 50;
  642.     tokenbuf = getmem (tokenbsize + 1);
  643.  
  644.     /*
  645.      * Explicitly output a #line at the start of cpp output so
  646.      * that lint (etc.) knows the name of the original source
  647.      * file.  If we don't do this explicitly, we may get
  648.      * the name of the first #include file instead.
  649.      */
  650.     sharp();
  651.     /*
  652.      * This loop is started "from the top" at the beginning of each line
  653.      * wrongline is set TRUE in many places if it is necessary to write
  654.      * a #line record.  (But we don't write them when expanding macros.)
  655.      *
  656.      * The counter variable has two different uses:  at
  657.      * the start of a line, it counts the number of blank lines that
  658.      * have been skipped over.  These are then either output via
  659.      * #line records or by outputting explicit blank lines.
  660.      * When expanding tokens within a line, the counter remembers
  661.      * whether a blank/tab has been output.  These are dropped
  662.      * at the end of the line, and replaced by a single blank
  663.      * within lines.
  664.      */
  665.     for (;;) {
  666.         counter = 0;            /* Count empty lines    */
  667.         for (;;) {                          /* For each line, ...   */
  668.         while (type[(c = get())] == SPA) /* Skip leading blanks */
  669.             ;                /* in this line.    */
  670.         if (c == '\n')                  /* If line's all blank, */
  671.             ++counter;            /* Do nothing now    */
  672.         else if (c == '#') {            /* Is 1st non-space '#' */
  673.             keepcomments = FALSE;    /* Don't pass comments  */
  674.             counter = control(counter); /* Yes, do a #command   */
  675.             keepcomments = (cflag && compiling);
  676.         }
  677.         else if (c == EOF_CHAR)         /* At end of file?      */
  678.             break;
  679.         else if (!compiling) {          /* #ifdef false?        */
  680.             skipnl();                   /* Skip to newline      */
  681.             counter++;            /* Count it, too.    */
  682.         }
  683.         else {
  684.             break;            /* Actual token     */
  685.         }
  686.         }
  687.         if (c == EOF_CHAR)                  /* Exit process at      */
  688.         break;                /* End of file        */
  689.         /*
  690.          * If the loop didn't terminate because of end of file, we
  691.          * know there is a token to compile.  First, clean up after
  692.          * absorbing newlines.  counter has the number we skipped.
  693.          */
  694.         if ((wrongline && infile->fp != NULL) || counter > 4)
  695.         sharp();                        /* Output # line number */
  696.         else {                /* If just a few, stuff */
  697.         while (--counter >= 0)          /* them out ourselves   */
  698.             putchar('\n');
  699.         }
  700.         /*
  701.          * Process each token on this line.
  702.          */
  703.         unget();                            /* Reread the char.     */
  704.         for (;;) {                          /* For the whole line,  */
  705.         do {                /* Token concat. loop    */
  706.             for (counter = 0; (type[(c = get())] == SPA);) {
  707. #if COMMENT_INVISIBLE
  708.             if (c != COM_SEP)
  709.                 counter++;
  710. #else
  711.             counter++;        /* Skip over blanks    */
  712. #endif
  713.             }
  714.             if (c == EOF_CHAR || c == '\n')
  715.             goto end_line;        /* Exit line loop    */
  716.             else if (counter > 0)       /* If we got any spaces */
  717.             putchar(' ');           /* Output one space     */
  718.             c = macroid(c);             /* Grab the token       */
  719.         } while (type[c] == LET && catenate());
  720.         if (c == EOF_CHAR || c == '\n') /* From macro exp error */
  721.             goto end_line;        /* Exit line loop    */
  722.         switch (type[c]) {
  723.         case LET:
  724.             fputs(tokenbuf, stdout);    /* Quite ordinary token */
  725.             break;
  726.  
  727.  
  728.         case DIG:            /* Output a number    */
  729.         case DOT:            /* Dot may begin floats */
  730.             scannumber(c, output);
  731.             break;
  732.  
  733.         case QUO:            /* char or string const */
  734.             scanstring(c, output);      /* Copy it to output    */
  735.             break;
  736.  
  737.         default:            /* Some other character */
  738.             cput(c);                    /* Just output it       */
  739.             break;
  740.         }                /* Switch ends        */
  741.         }                    /* Line for loop    */
  742. end_line:   if (c == '\n') {                    /* Compiling at EOL?    */
  743.         putchar('\n');                  /* Output newline, if   */
  744.         if (infile->fp == NULL)         /* Expanding a macro,   */
  745.             wrongline = TRUE;        /* Output # line later    */
  746.         }
  747.     }                    /* Continue until EOF    */
  748.     if (wflag)
  749.         outdefines();                       /* Write out #defines   */
  750.     DBUG_VOID_RETURN;
  751. }
  752.  
  753. #ifdef __STDC__
  754. void output(int c)
  755. #else
  756. output(c)
  757. int        c;
  758. #endif
  759. /*
  760.  * Output one character to stdout -- output() is passed as an
  761.  * argument to scanstring()
  762.  */
  763. {
  764. #if COMMENT_INVISIBLE
  765.     if (c != TOK_SEP && c != COM_SEP)
  766. #else
  767.     if (c != TOK_SEP)
  768. #endif
  769.         putchar(c);
  770. }
  771.  
  772. static char    *sharpfilename = NULL;
  773.  
  774. FILE_LOCAL void
  775. sharp()
  776. /*
  777.  * Output a line number line.
  778.  */
  779. {
  780.     register char        *name;
  781.  
  782.     DBUG_ENTER ("sharp");
  783.     if (keepcomments)                       /* Make sure # comes on */
  784.         putchar('\n');                      /* a fresh, new line.   */
  785.     if(pflag) /* no #line */
  786.         return;
  787.     printf("#%s %d", LINE_PREFIX, line);
  788.     if (infile->fp != NULL) {
  789.         name = (infile->progname != NULL)
  790.         ? infile->progname : infile->filename;
  791.         if (sharpfilename == NULL
  792.          || sharpfilename != NULL && !streq(name, sharpfilename)) {
  793.         if (sharpfilename != NULL)
  794.             free(sharpfilename);
  795.         sharpfilename = savestring(name);
  796.         printf(" \"%s\"", name);
  797.          }
  798.     }
  799.     putchar('\n');
  800.     wrongline = FALSE;
  801.     DBUG_VOID_RETURN;
  802. }
  803.