forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp2.shr
More file actions
1763 lines (1763 loc) · 52.3 KB
/
cpp2.shr
File metadata and controls
1763 lines (1763 loc) · 52.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
# cpp1.c
# cpp3.c
# cpp4.c
#
echo x - cpp1.c
sed 's/^X//' >cpp1.c << 'END-of-cpp1.c'
X/*
X * CPP main program.
X *
X * Edit history
X * 21-May-84 MM "Field test" release
X * 23-May-84 MM Some minor hacks.
X * 30-May-84 ARF Didn't get enough memory for __DATE__
X * Added code to read stdin if no input
X * files are provided.
X * 29-Jun-84 MM Added ARF's suggestions, Unixifying cpp.
X * 11-Jul-84 MM "Official" first release (that's what I thought!)
X * 22-Jul-84 MM/ARF/SCK Fixed line number bugs, added cpp recognition
X * of #line, fixed problems with #include.
X * 23-Jul-84 MM More (minor) include hacking, some documentation.
X * Also, redid cpp's #include files
X * 25-Jul-84 MM #line filename isn't used for #include searchlist
X * #line format is <number> <optional name>
X * 25-Jul-84 ARF/MM Various bugs, mostly serious. Removed homemade doprint
X * 01-Aug-84 MM Fixed recursion bug, remove extra newlines and
X * leading whitespace from cpp output.
X * 02-Aug-84 MM Hacked (i.e. optimized) out blank lines and unneeded
X * whitespace in general. Cleaned up unget()'s.
X * 03-Aug-84 Keie Several bug fixes from Ed Keizer, Vrije Universitet.
X * -- corrected arg. count in -D and pre-defined
X * macros. Also, allow \n inside macro actual parameter
X * lists.
X * 06-Aug-84 MM If debugging, dump the preset vector at startup.
X * 12-Aug-84 MM/SCK Some small changes from Sam Kendall
X * 15-Aug-84 Keie/MM cerror, cwarn, etc. take a single string arg.
X * cierror, etc. take a single int. arg.
X * changed LINE_PREFIX slightly so it can be
X * changed in the makefile.
X * 31-Aug-84 MM USENET net.sources release.
X * 7-Sep-84 SCH/ado Lint complaints
X * 10-Sep-84 Keie Char's can't be signed in some implementations
X * 11-Sep-84 ado Added -C flag, pathological line number fix
X * 13-Sep-84 ado Added -E flag (does nothing) and "-" file for stdin.
X * 14-Sep-84 MM Allow # 123 as a synonym for #line 123
X * 19-Sep-84 MM scanid always reads to token, make sure #line is
X * written to a new line, even if -C switch given.
X * Also, cpp - - reads stdin, writes stdout.
X * 03-Oct-84 ado/MM Several changes to line counting and keepcomments
X * stuff. Also a rewritten control() hasher -- much
X * simpler and no less "perfect". Note also changes
X * in cpp3.c to fix numeric scanning.
X * 04-Oct-84 MM Added recognition of macro formal parameters if
X * they are the only thing in a string, per the
X * draft standard.
X * 08-Oct-84 MM One more attack on scannumber
X * 15-Oct-84 MM/ado Added -N to disable predefined symbols. Fixed
X * linecount if COMMENT_INVISIBLE enabled.
X * 22-Oct-84 MM Don't evaluate the #if/#ifdef argument if
X * compilation is supressed. This prevents
X * unnecessary error messages in sequences such as
X * #ifdef FOO -- undefined
X * #if FOO == 10 -- shouldn't print warning
X * 25-Oct-84 MM Fixed bug in false ifdef supression. On vms,
X * #include <foo> should open foo.h -- this duplicates
X * the behavior of Vax-C
X * 31-Oct-84 ado/MM Parametized $ in indentifiers. Added a better
X * token concatenator and took out the trial
X * concatenation code. Also improved #ifdef code
X * and cleaned up the macro recursion tester.
X * 2-Nov-84 MM/ado Some bug fixes in token concatenation, also
X * a variety of minor (uninteresting) hacks.
X * 6-Nov-84 MM Happy Birthday. Broke into 4 files and added
X * #if sizeof (basic_types)
X * 9-Nov-84 MM Added -S* for pointer type sizes
X * 13-Nov-84 MM Split cpp1.c, added vms defaulting
X * 23-Nov-84 MM/ado -E supresses error exit, added CPP_INCLUDE,
X * fixed strncpy bug.
X * 3-Dec-84 ado/MM Added OLD_PREPROCESSOR
X * 7-Dec-84 MM Stuff in Nov 12 Draft Standard
X * 17-Dec-84 george Fixed problems with recursive macros
X * 17-Dec-84 MM Yet another attack on #if's (f/t)level removed.
X * 07-Jan-85 ado Init defines before doing command line options
X * so -Uunix works.
X */
X
X/*)BUILD
X $(PROGRAM) = cpp
X $(FILES) = { cpp1 cpp2 cpp3 cpp4 cpp5 cpp6 }
X $(INCLUDE) = { cppdef.h cpp.h }
X $(STACK) = 2000
X $(TKBOPTIONS) = {
X STACK = 2000
X }
X*/
X
X#ifdef DOCUMENTATION
X
Xtitle cpp C Pre-Processor
Xindex C pre-processor
X
Xsynopsis
X .s.nf
X cpp [-options] [infile [outfile]]
X .s.f
Xdescription
X
X CPP reads a C source file, expands macros and include
X files, and writes an input file for the C compiler.
X If no file arguments are given, CPP reads from stdin
X and writes to stdout. If one file argument is given,
X it will define the input file, while two file arguments
X define both input and output files. The file name "-"
X is a synonym for stdin or stdout as appropriate.
X
X The following options are supported. Options may
X be given in either case.
X .lm +16
X .p -16
X -C If set, source-file comments are written
X to the output file. This allows the output of CPP to be
X used as the input to a program, such as lint, that expects
X commands embedded in specially-formatted comments.
X .p -16
X -Dname=value Define the name as if the programmer wrote
X
X #define name value
X
X at the start of the first file. If "=value" is not
X given, a value of "1" will be used.
X
X On non-unix systems, all alphabetic text will be forced
X to upper-case.
X .p -16
X -E Always return "success" to the operating
X system, even if errors were detected. Note that some fatal
X errors, such as a missing #include file, will terminate
X CPP, returning "failure" even if the -E option is given.
X .p -16
X -Idirectory Add this directory to the list of
X directories searched for #include "..." and #include <...>
X commands. Note that there is no space between the
X "-I" and the directory string. More than one -I command
X is permitted. On non-Unix systems "directory" is forced
X to upper-case.
X .p -16
X -N CPP normally predefines some symbols defining
X the target computer and operating system. If -N is specified,
X no symbols will be predefined. If -N -N is specified, the
X "always present" symbols, __LINE__, __FILE__, and __DATE__
X are not defined.
X .p -16
X -Stext CPP normally assumes that the size of
X the target computer's basic variable types is the same as the size
X of these types of the host computer. (This can be overridden
X when CPP is compiled, however.) The -S option allows dynamic
X respecification of these values. "text" is a string of
X numbers, separated by commas, that specifies correct sizes.
X The sizes must be specified in the exact order:
X
X char short int long float double
X
X If you specify the option as "-S*text", pointers to these
X types will be specified. -S* takes one additional argument
X for pointer to function (e.g. int (*)())
X
X For example, to specify sizes appropriate for a PDP-11,
X you would write:
X
X c s i l f d func
X -S1,2,2,2,4,8,
X -S*2,2,2,2,2,2,2
X
X Note that all values must be specified.
X .p -16
X -Uname Undefine the name as if
X
X #undef name
X
X were given. On non-Unix systems, "name" will be forced to
X upper-case.
X .p -16
X -Xnumber Enable debugging code. If no value is
X given, a value of 1 will be used. (For maintenence of
X CPP only.)
X .s.lm -16
X
XPre-Defined Variables
X
X When CPP begins processing, the following variables will
X have been defined (unless the -N option is specified):
X .s
X Target computer (as appropriate):
X .s
X pdp11, vax, M68000 m68000 m68k
X .s
X Target operating system (as appropriate):
X .s
X rsx, rt11, vms, unix
X .s
X Target compiler (as appropriate):
X .s
X decus, vax11c
X .s
X The implementor may add definitions to this list.
X The default definitions match the definition of the
X host computer, operating system, and C compiler.
X .s
X The following are always available unless undefined (or
X -N was specified twice):
X .lm +16
X .p -12
X __FILE__ The input (or #include) file being compiled
X (as a quoted string).
X .p -12
X __LINE__ The line number being compiled.
X .p -12
X __DATE__ The date and time of compilation as
X a Unix ctime quoted string (the trailing newline is removed).
X Thus,
X .s
X printf("Bug at line %s,", __LINE__);
X printf(" source file %s", __FILE__);
X printf(" compiled on %s", __DATE__);
X .s.lm -16
X
XDraft Proposed Ansi Standard Considerations
X
X The current version of the Draft Proposed Standard
X explicitly states that "readers are requested not to specify
X or claim conformance to this draft." Readers and users
X of Decus CPP should not assume that Decus CPP conforms
X to the standard, or that it will conform to the actual
X C Language Standard.
X
X When CPP is itself compiled, many features of the Draft
X Proposed Standard that are incompatible with existing
X preprocessors may be disabled. See the comments in CPP's
X source for details.
X
X The latest version of the Draft Proposed Standard (as reflected
X in Decus CPP) is dated November 12, 1984.
X
X Comments are removed from the input text. The comment
X is replaced by a single space character. The -C option
X preserves comments, writing them to the output file.
X
X The '$' character is considered to be a letter. This is
X a permitted extension.
X
X The following new features of C are processed by CPP:
X .s.comment Note: significant spaces, not tabs, .br quotes #if, #elif
X .br;####_#elif expression (_#else _#if)
X .br;####'_\xNNN' (Hexadecimal constant)
X .br;####'_\a' (Ascii BELL)
X .br;####'_\v' (Ascii Vertical Tab)
X .br;####_#if defined NAME 1 if defined, 0 if not
X .br;####_#if defined (NAME) 1 if defined, 0 if not
X .br;####_#if sizeof (basic type)
X .br;####unary +
X .br;####123U, 123LU Unsigned ints and longs.
X .br;####12.3L Long double numbers
X .br;####token_#token Token concatenation
X .br;####_#include token Expands to filename
X
X The Draft Proposed Standard has extended C, adding a constant
X string concatenation operator, where
X
X "foo" "bar"
X
X is regarded as the single string "foobar". (This does not
X affect CPP's processing but does permit a limited form of
X macro argument substitution into strings as will be discussed.)
X
X The Standard Committee plans to add token concatenation
X to #define command lines. One suggested implementation
X is as follows: the sequence "Token1#Token2" is treated
X as if the programmer wrote "Token1Token2". This could
X be used as follows:
X
X #line 123
X #define ATLINE foo#__LINE__
X
X ATLINE would be defined as foo123.
X
X Note that "Token2" must either have the format of an
X identifier or be a string of digits. Thus, the string
X
X #define ATLINE foo#1x3
X
X generates two tokens: "foo1" and "x3".
X
X If the tokens T1 and T2 are concatenated into T3,
X this implementation operates as follows:
X
X 1. Expand T1 if it is a macro.
X 2. Expand T2 if it is a macro.
X 3. Join the tokens, forming T3.
X 4. Expand T3 if it is a macro.
X
X A macro formal parameter will be substituted into a string
X or character constant if it is the only component of that
X constant:
X
X #define VECSIZE 123
X #define vprint(name, size) \
X printf("name" "[" "size" "] = {\n")
X ... vprint(vector, VECSIZE);
X
X expands (effectively) to
X
X vprint("vector[123] = {\n");
X
X Note that this will be useful if your C compiler supports
X the new string concatenation operation noted above.
X As implemented here, if you write
X
X #define string(arg) "arg"
X ... string("foo") ...
X
X This implementation generates "foo", rather than the strictly
X correct ""foo"" (which will probably generate an error message).
X This is, strictly speaking, an error in CPP and may be removed
X from future releases.
X
Xerror messages
X
X Many. CPP prints warning or error messages if you try to
X use multiple-byte character constants (non-transportable)
X if you #undef a symbol that was not defined, or if your
X program has potentially nested comments.
X
Xauthor
X
X Martin Minow
X
Xbugs
X
X The #if expression processor uses signed integers only.
X I.e, #if 0xFFFFu < 0 may be TRUE.
X
X#endif
X
X#include <stdio.h>
X#include <ctype.h>
X#include "cppdef.h"
X#include "cpp.h"
X
X/*
X * Commonly used global variables:
X * line is the current input line number.
X * wrongline is set in many places when the actual output
X * line is out of sync with the numbering, e.g,
X * when expanding a macro with an embedded newline.
X *
X * token holds the last identifier scanned (which might
X * be a candidate for macro expansion).
X * errors is the running cpp error counter.
X * infile is the head of a linked list of input files (extended by
X * #include and macros being expanded). infile always points
X * to the current file/macro. infile->parent to the includer,
X * etc. infile->fd is NULL if this input stream is a macro.
X */
Xint line; /* Current line number */
Xint wrongline; /* Force #line to compiler */
Xchar token[IDMAX + 1]; /* Current input token */
Xint errors; /* cpp error counter */
XFILEINFO *infile = NULL; /* Current input file */
X#if DEBUG
Xint debug; /* TRUE if debugging now */
X#endif
X/*
X * This counter is incremented when a macro expansion is initiated.
X * If it exceeds a built-in value, the expansion stops -- this tests
X * for a runaway condition:
X * #define X Y
X * #define Y X
X * X
X * This can be disabled by falsifying rec_recover. (Nothing does this
X * currently: it is a hook for an eventual invocation flag.)
X */
Xint recursion; /* Infinite recursion counter */
Xint rec_recover = TRUE; /* Unwind recursive macros */
X
X/*
X * instring is set TRUE when a string is scanned. It modifies the
X * behavior of the "get next character" routine, causing all characters
X * to be passed to the caller (except <DEF_MAGIC>). Note especially that
X * comments and \<newline> are not removed from the source. (This
X * prevents cpp output lines from being arbitrarily long).
X *
X * inmacro is set by #define -- it absorbs comments and converts
X * form-feed and vertical-tab to space, but returns \<newline>
X * to the caller. Strictly speaking, this is a bug as \<newline>
X * shouldn't delimit tokens, but we'll worry about that some other
X * time -- it is more important to prevent infinitly long output lines.
X *
X * instring and inmarcor are parameters to the get() routine which
X * were made global for speed.
X */
Xint instring = FALSE; /* TRUE if scanning string */
Xint inmacro = FALSE; /* TRUE if #defining a macro */
X
X/*
X * work[] and workp are used to store one piece of text in a temporay
X * buffer. To initialize storage, set workp = work. To store one
X * character, call save(c); (This will fatally exit if there isn't
X * room.) To terminate the string, call save(EOS). Note that
X * the work buffer is used by several subroutines -- be sure your
X * data won't be overwritten. The extra byte in the allocation is
X * needed for string formal replacement.
X */
Xchar work[NWORK + 1]; /* Work buffer */
Xchar *workp; /* Work buffer pointer */
X
X/*
X * keepcomments is set TRUE by the -C option. If TRUE, comments
X * are written directly to the output stream. This is needed if
X * the output from cpp is to be passed to lint (which uses commands
X * embedded in comments). cflag contains the permanent state of the
X * -C flag. keepcomments is always falsified when processing #control
X * commands and when compilation is supressed by a false #if
X *
X * If eflag is set, CPP returns "success" even if non-fatal errors
X * were detected.
X *
X * If nflag is non-zero, no symbols are predefined except __LINE__.
X * __FILE__, and __DATE__. If nflag > 1, absolutely no symbols
X * are predefined.
X */
Xint keepcomments = FALSE; /* Write out comments flag */
Xint cflag = FALSE; /* -C option (keep comments) */
Xint eflag = FALSE; /* -E option (never fail) */
Xint nflag = 0; /* -N option (no predefines) */
X
X/*
X * ifstack[] holds information about nested #if's. It is always
X * accessed via *ifptr. The information is as follows:
X * WAS_COMPILING state of compiling flag at outer level.
X * ELSE_SEEN set TRUE when #else seen to prevent 2nd #else.
X * TRUE_SEEN set TRUE when #if or #elif succeeds
X * ifstack[0] holds the compiling flag. It is TRUE if compilation
X * is currently enabled. Note that this must be initialized TRUE.
X */
Xchar ifstack[BLK_NEST] = { TRUE }; /* #if information */
Xchar *ifptr = ifstack; /* -> current ifstack[] */
X
X/*
X * incdir[] stores the -i directories (and the system-specific
X * #include <...> directories.
X */
Xchar *incdir[NINCLUDE]; /* -i directories */
Xchar **incend = incdir; /* -> free space in incdir[] */
X
X/*
X * This is the table used to predefine target machine and operating
X * system designators. It may need hacking for specific circumstances.
X * Note: it is not clear that this is part of the Ansi Standard.
X * The -N option supresses preset definitions.
X */
Xchar *preset[] = { /* names defined at cpp start */
X#ifdef MACHINE
X MACHINE,
X#endif
X#ifdef SYSTEM
X SYSTEM,
X#endif
X#ifdef COMPILER
X COMPILER,
X#endif
X#if DEBUG
X "decus_cpp", /* Ourselves! */
X#endif
X NULL /* Must be last */
X};
X
X/*
X * The value of these predefined symbols must be recomputed whenever
X * they are evaluated. The order must not be changed.
X */
Xchar *magic[] = { /* Note: order is important */
X "__LINE__",
X "__FILE__",
X NULL /* Must be last */
X};
X
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X register int i;
X
X#if HOST == SYS_VMS
X argc = getredirection(argc, argv); /* vms >file and <file */
X#endif
X initdefines(); /* O.S. specific def's */
X i = dooptions(argc, argv); /* Command line -flags */
X switch (i) {
X case 3:
X /*
X * Get output file, "-" means use stdout.
X */
X if (!streq(argv[2], "-")) {
X#if HOST == SYS_VMS
X /*
X * On vms, reopen stdout with "vanilla rms" attributes.
X */
X if ((i = creat(argv[2], 0, "rat=cr", "rfm=var")) == -1
X || dup2(i, fileno(stdout)) == -1) {
X#else
X if (freopen(argv[2], "w", stdout) == NULL) {
X#endif
X perror(argv[2]);
X cerror("Can't open output file \"%s\"", argv[2]);
X exit(IO_ERROR);
X }
X } /* Continue by opening input */
X case 2: /* One file -> stdin */
X /*
X * Open input file, "-" means use stdin.
X */
X if (!streq(argv[1], "-")) {
X if (freopen(argv[1], "r", stdin) == NULL) {
X perror(argv[1]);
X cerror("Can't open input file \"%s\"", argv[1]);
X exit(IO_ERROR);
X }
X strcpy(work, argv[1]); /* Remember input filename */
X break;
X } /* Else, just get stdin */
X case 0: /* No args? */
X case 1: /* No files, stdin -> stdout */
X#if HOST == SYS_UNIX
X work[0] = EOS; /* Unix can't find stdin name */
X#else
X fgetname(stdin, work); /* Vax-11C, Decus C know name */
X#endif
X break;
X
X default:
X exit(IO_ERROR); /* Can't happen */
X }
X setincdirs(); /* Setup -I include directories */
X addfile(stdin, work); /* "open" main input file */
X#if DEBUG
X if (debug > 0)
X dumpdef("preset #define symbols");
X#endif
X cppmain(); /* Process main file */
X if ((i = (ifptr - &ifstack[0])) != 0) {
X#if OLD_PREPROCESSOR
X ciwarn("Inside #ifdef block at end of input, depth = %d", i);
X#else
X cierror("Inside #ifdef block at end of input, depth = %d", i);
X#endif
X }
X fclose(stdout);
X if (errors > 0) {
X fprintf(stderr, (errors == 1)
X ? "%d error in preprocessor\n"
X : "%d errors in preprocessor\n", errors);
X if (!eflag)
X exit(IO_ERROR);
X }
X exit(IO_NORMAL); /* No errors or -E option set */
X}
X
XFILE_LOCAL
Xcppmain()
X/*
X * Main process for cpp -- copies tokens from the current input
X * stream (main file, include file, or a macro) to the output
X * file.
X */
X{
X register int c; /* Current character */
X register int counter; /* newlines and spaces */
X extern int output(); /* Output one character */
X
X /*
X * Explicitly output a #line at the start of cpp output so
X * that lint (etc.) knows the name of the original source
X * file. If we don't do this explicitly, we may get
X * the name of the first #include file instead.
X */
X sharp();
X /*
X * This loop is started "from the top" at the beginning of each line
X * wrongline is set TRUE in many places if it is necessary to write
X * a #line record. (But we don't write them when expanding macros.)
X *
X * The counter variable has two different uses: at
X * the start of a line, it counts the number of blank lines that
X * have been skipped over. These are then either output via
X * #line records or by outputting explicit blank lines.
X * When expanding tokens within a line, the counter remembers
X * whether a blank/tab has been output. These are dropped
X * at the end of the line, and replaced by a single blank
X * within lines.
X */
X for (;;) {
X counter = 0; /* Count empty lines */
X for (;;) { /* For each line, ... */
X while (type[(c = get())] == SPA) /* Skip leading blanks */
X ; /* in this line. */
X if (c == '\n') /* If line's all blank, */
X ++counter; /* Do nothing now */
X else if (c == '#') { /* Is 1st non-space '#' */
X keepcomments = FALSE; /* Don't pass comments */
X counter = control(counter); /* Yes, do a #command */
X keepcomments = (cflag && compiling);
X }
X else if (c == EOF_CHAR) /* At end of file? */
X break;
X else if (!compiling) { /* #ifdef false? */
X skipnl(); /* Skip to newline */
X counter++; /* Count it, too. */
X }
X else {
X break; /* Actual token */
X }
X }
X if (c == EOF_CHAR) /* Exit process at */
X break; /* End of file */
X /*
X * If the loop didn't terminate because of end of file, we
X * know there is a token to compile. First, clean up after
X * absorbing newlines. counter has the number we skipped.
X */
X if ((wrongline && infile->fp != NULL) || counter > 4)
X sharp(); /* Output # line number */
X else { /* If just a few, stuff */
X while (--counter >= 0) /* them out ourselves */
X putchar('\n');
X }
X /*
X * Process each token on this line.
X */
X unget(); /* Reread the char. */
X for (;;) { /* For the whole line, */
X do { /* Token concat. loop */
X for (counter = 0; (type[(c = get())] == SPA);) {
X#if COMMENT_INVISIBLE
X if (c != COM_SEP)
X counter++;
X#else
X counter++; /* Skip over blanks */
X#endif
X }
X if (c == EOF_CHAR || c == '\n')
X goto end_line; /* Exit line loop */
X else if (counter > 0) /* If we got any spaces */
X putchar(' '); /* Output one space */
X c = macroid(c); /* Grab the token */
X } while (type[c] == LET && catenate());
X if (c == EOF_CHAR || c == '\n') /* From macro exp error */
X goto end_line; /* Exit line loop */
X switch (type[c]) {
X case LET:
X fputs(token, stdout); /* Quite ordinary token */
X break;
X
X
X case DIG: /* Output a number */
X case DOT: /* Dot may begin floats */
X scannumber(c, output);
X break;
X
X case QUO: /* char or string const */
X scanstring(c, output); /* Copy it to output */
X break;
X
X default: /* Some other character */
X cput(c); /* Just output it */
X break;
X } /* Switch ends */
X } /* Line for loop */
Xend_line: if (c == '\n') { /* Compiling at EOL? */
X putchar('\n'); /* Output newline, if */
X if (infile->fp == NULL) /* Expanding a macro, */
X wrongline = TRUE; /* Output # line later */
X }
X } /* Continue until EOF */
X}
X
Xoutput(c)
Xint c;
X/*
X * Output one character to stdout -- output() is passed as an
X * argument to scanstring()
X */
X{
X#if COMMENT_INVISIBLE
X if (c != TOK_SEP && c != COM_SEP)
X#else
X if (c != TOK_SEP)
X#endif
X putchar(c);
X}
X
Xstatic char *sharpfilename = NULL;
X
XFILE_LOCAL
Xsharp()
X/*
X * Output a line number line.
X */
X{
X register char *name;
X
X if (keepcomments) /* Make sure # comes on */
X putchar('\n'); /* a fresh, new line. */
X printf("#%s %d", LINE_PREFIX, line);
X if (infile->fp != NULL) {
X name = (infile->progname != NULL)
X ? infile->progname : infile->filename;
X if (sharpfilename == NULL
X || sharpfilename != NULL && !streq(name, sharpfilename)) {
X if (sharpfilename != NULL)
X free(sharpfilename);
X sharpfilename = savestring(name);
X printf(" \"%s\"", name);
X }
X }
X putchar('\n');
X wrongline = FALSE;
X}
END-of-cpp1.c
echo x - cpp3.c
sed 's/^X//' >cpp3.c << 'END-of-cpp3.c'
X/*
X * C P P 3 . C
X *
X * File open and command line options
X *
X * Edit history
X * 13-Nov-84 MM Split from cpp1.c
X */
X
X#include <stdio.h>
X#include <ctype.h>
X#include "cppdef.h"
X#include "cpp.h"
X#if DEBUG && (HOST == SYS_VMS || HOST == SYS_UNIX)
X#include <signal.h>
Xextern int abort(); /* For debugging */
X#endif
X
Xint
Xopenfile(filename)
Xchar *filename;
X/*
X * Open a file, add it to the linked list of open files.
X * This is called only from openfile() above.
X */
X{
X register FILE *fp;
X
X if ((fp = fopen(filename, "r")) == NULL) {
X#if DEBUG
X perror(filename);
X#endif
X return (FALSE);
X }
X#if DEBUG
X if (debug)
X fprintf(stderr, "Reading from \"%s\"\n", filename);
X#endif
X addfile(fp, filename);
X return (TRUE);
X}
X
Xaddfile(fp, filename)
XFILE *fp; /* Open file pointer */
Xchar *filename; /* Name of the file */
X/*
X * Initialize tables for this open file. This is called from openfile()
X * above (for #include files), and from the entry to cpp to open the main
X * input file. It calls a common routine, getfile() to build the FILEINFO
X * structure which is used to read characters. (getfile() is also called
X * to setup a macro replacement.)
X */
X{
X register FILEINFO *file;
X extern FILEINFO *getfile();
X
X file = getfile(NBUFF, filename);
X file->fp = fp; /* Better remember FILE * */
X file->buffer[0] = EOS; /* Initialize for first read */
X line = 1; /* Working on line 1 now */
X wrongline = TRUE; /* Force out initial #line */
X}
X
Xsetincdirs()
X/*
X * Append system-specific directories to the include directory list.
X * Called only when cpp is started.
X */
X{
X
X#ifdef CPP_INCLUDE
X *incend++ = CPP_INCLUDE;
X#define IS_INCLUDE 1
X#else
X#define IS_INCLUDE 0
X#endif
X
X#if HOST == SYS_UNIX
X *incend++ = "/usr/include";
X#define MAXINCLUDE (NINCLUDE - 1 - IS_INCLUDE)
X#endif
X
X#if HOST == SYS_VMS
X extern char *getenv();
X
X if (getenv("C$LIBRARY") != NULL)
X *incend++ = "C$LIBRARY:";
X *incend++ = "SYS$LIBRARY:";
X#define MAXINCLUDE (NINCLUDE - 2 - IS_INCLUDE)
X#endif
X
X#if HOST == SYS_RSX
X extern int $$rsts; /* TRUE on RSTS/E */
X extern int $$pos; /* TRUE on PRO-350 P/OS */
X extern int $$vms; /* TRUE on VMS compat. */
X
X if ($$pos) { /* P/OS? */
X *incend++ = "SY:[ZZDECUSC]"; /* C #includes */
X *incend++ = "LB:[1,5]"; /* RSX library */
X }
X else if ($$rsts) { /* RSTS/E? */
X *incend++ = "SY:@"; /* User-defined account */
X *incend++ = "C:"; /* Decus-C library */
X *incend++ = "LB:[1,1]"; /* RSX library */
X }
X else if ($$vms) { /* VMS compatibility? */
X *incend++ = "C:";
X }
X else { /* Plain old RSX/IAS */
X *incend++ = "LB:[1,1]";
X }
X#define MAXINCLUDE (NINCLUDE - 3 - IS_INCLUDE)
X#endif
X
X#if HOST == SYS_RT11
X extern int $$rsts; /* RSTS/E emulation? */
X
X if ($$rsts)
X *incend++ = "SY:@"; /* User-defined account */
X *incend++ = "C:"; /* Decus-C library disk */
X *incend++ = "SY:"; /* System (boot) disk */
X#define MAXINCLUDE (NINCLUDE - 3 - IS_INCLUDE)
X#endif
X}
X
Xint
Xdooptions(argc, argv)
Xint argc;
Xchar *argv[];
X/*
X * dooptions is called to process command line arguments (-Detc).
X * It is called only at cpp startup.
X */
X{
X register char *ap;
X register DEFBUF *dp;
X register int c;
X int i, j;
X char *arg;
X SIZES *sizp; /* For -S */
X int size; /* For -S */
X int isdatum; /* FALSE for -S* */
X int endtest; /* For -S */
X
X for (i = j = 1; i < argc; i++) {
X arg = ap = argv[i];
X if (*ap++ != '-' || *ap == EOS)
X argv[j++] = argv[i];
X else {
X c = *ap++; /* Option byte */
X if (islower(c)) /* Normalize case */
X c = toupper(c);
X switch (c) { /* Command character */
X case 'C': /* Keep comments */
X cflag = TRUE;
X keepcomments = TRUE;
X break;
X
X case 'D': /* Define symbol */
X#if HOST != SYS_UNIX
X zap_uc(ap); /* Force define to U.C. */
X#endif
X /*
X * If the option is just "-Dfoo", make it -Dfoo=1
X */
X while (*ap != EOS && *ap != '=')
X ap++;
X if (*ap == EOS)
X ap = "1";
X else
X *ap++ = EOS;
X /*
X * Now, save the word and its definition.
X */
X dp = defendel(argv[i] + 2, FALSE);
X dp->repl = savestring(ap);
X dp->nargs = DEF_NOARGS;
X break;
X
X case 'E': /* Ignore non-fatal */
X eflag = TRUE; /* errors. */
X break;
X
X case 'I': /* Include directory */
X if (incend >= &incdir[MAXINCLUDE])
X cfatal("Too many include directories", NULLST);
X *incend++ = ap;
X break;
X
X case 'N': /* No predefineds */
X nflag++; /* Repeat to undefine */
X break; /* __LINE__, etc. */
X
X case 'S':
X sizp = size_table;
X if (isdatum = (*ap != '*')) /* If it's just -S, */
X endtest = T_FPTR; /* Stop here */
X else { /* But if it's -S* */
X ap++; /* Step over '*' */
X endtest = 0; /* Stop at end marker */
X }
X while (sizp->bits != endtest && *ap != EOS) {
X if (!isdigit(*ap)) { /* Skip to next digit */
X ap++;
X continue;
X }
X size = 0; /* Compile the value */
X while (isdigit(*ap)) {
X size *= 10;
X size += (*ap++ - '0');
X }
X if (isdatum)
X sizp->size = size; /* Datum size */
X else
X sizp->psize = size; /* Pointer size */
X sizp++;
X }
X if (sizp->bits != endtest)
X cwarn("-S, too few values specified in %s", argv[i]);
X else if (*ap != EOS)
X cwarn("-S, too many values, \"%s\" unused", ap);
X break;
X
X case 'U': /* Undefine symbol */
X#if HOST != SYS_UNIX
X zap_uc(ap);
X#endif
X if (defendel(ap, TRUE) == NULL)
X cwarn("\"%s\" wasn't defined", ap);
X break;
X
X#if DEBUG
X case 'X': /* Debug */
X debug = (isdigit(*ap)) ? atoi(ap) : 1;
X#if (HOST == SYS_VMS || HOST == SYS_UNIX)
X signal(SIGINT, abort); /* Trap "interrupt" */
X#endif
X fprintf(stderr, "Debug set to %d\n", debug);
X break;
X#endif
X
X default: /* What is this one? */
X cwarn("Unknown option \"%s\"", arg);
X fprintf(stderr, "The following options are valid:\n\
X -C\t\t\tWrite source file comments to output\n\
X -Dsymbol=value\tDefine a symbol with the given (optional) value\n\
X -Idirectory\t\tAdd a directory to the #include search list\n\
X -N\t\t\tDon't predefine target-specific names\n\
X -Stext\t\tSpecify sizes for #if sizeof\n\
X -Usymbol\t\tUndefine symbol\n");
X#if DEBUG
X fprintf(stderr, " -Xvalue\t\tSet internal debug flag\n");
X#endif
X break;
X } /* Switch on all options */
X } /* If it's a -option */
X } /* For all arguments */
X if (j > 3) {
X cerror(
X "Too many file arguments. Usage: cpp [input [output]]",
X NULLST);
X }
X return (j); /* Return new argc */
X}