Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050108083552

Diff

Diff from 1.21 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/cc/cpp/cpp.c

Annotated File View

ragge
1.21
1 /*      $Id: cpp.c,v 1.21 2005/01/08 08:35:52 ragge Exp $       */
ragge
1.1
2
3 /*
4  * Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /*
31  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  *
37  * Redistributions of source code and documentation must retain the above
38  * copyright notice, this list of conditions and the following disclaimer.
39  * Redistributions in binary form must reproduce the above copyright
40  * notice, this list of conditions and the following disclaimer in the
41  * documentation and/or other materials provided with the distribution.
42  * All advertising materials mentioning features or use of this software
43  * must display the following acknowledgement:
44  *      This product includes software developed or owned by Caldera
45  *      International, Inc.
46  * Neither the name of Caldera International, Inc. nor the names of other
47  * contributors may be used to endorse or promote products derived from
48  * this software without specific prior written permission.
49  *
50  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
51  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
55  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
59  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
60  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
61  * POSSIBILITY OF SUCH DAMAGE.
62  */
63 /*
64  * The C preprocessor.
65  * This code originates from the V6 preprocessor with some additions
66  * from V7 cpp, and at last ansi/c99 support.
67  */
68 #include <sys/wait.h>
69
70 #include <fcntl.h>
71 #include <unistd.h>
72 #include <stdio.h>
73 #include <stdarg.h>
74 #include <stdlib.h>
75 #include <string.h>
ragge
1.9
76 #include <time.h>
ragge
1.1
77 #include <unistd.h>
78
79 #include "cpp.h"
80
81 #define MAXARG  250     /* # of args to a macro, limited by char value */
ragge
1.16
82 #define SBSIZE  200000
83 #define SYMSIZ  6000
ragge
1.1
84
85 static usch     sbf[SBSIZE];
86 /* C command */
87
88 int tflag;      /* traditional cpp syntax */
89 #ifdef CPP_DEBUG
90 int dflag;      /* debug printouts */
91 #endif
92 FILE *obuf;
93 static int exfail;
ragge
1.10
94 struct symtab symtab[SYMSIZ];
ragge
1.1
95
96 /* avoid recursion */
97 struct recur {
98         struct recur *next;
99         struct symtab *sp;
100 };
101
ragge
1.10
102 /* include dirs */
103 struct incs {
104         struct incs *next;
105         char *dir;
ragge
1.20
106 } *incdir[2];
107 #define INCINC 0
108 #define SYSINC 1
ragge
1.10
109
ragge
1.1
110 static struct symtab *filloc;
111 static struct symtab *linloc;
ragge
1.10
112 int     trulvl;
113 int     flslvl;
114 int     elflvl;
115 int     elslvl;
116 usch *stringbuf = sbf;
ragge
1.1
117
118 /*
119  * Macro replacement list syntax:
120  * - For object-type macros, replacement strings are stored as-is.
121  * - For function-type macros, macro args are substituted for the
122  *   character WARN followed by the argument number.
ragge
1.12
123  * - The value element points to the end of the string, to simplify
124  *   pushback onto the input queue.
ragge
1.1
125  * 
ragge
1.12
126  * The first character (from the end) in the replacement list is
127  * the number of arguments:
ragge
1.1
128  *   OBJCT - object-type macro
129  *   0     - empty parenthesis, foo()
130  *   1->   - number of args.
131  */
132
133 #define OBJCT   0xff
134 #define WARN    1       /* SOH, not legal char */
135 #define CONC    2       /* STX, not legal char */
136 #define SNUFF   3       /* ETX, not legal char */
137 #define NOEXP   4       /* EOT, not legal char */
138 #define EXPAND  5       /* ENQ, not legal char */
139
140 /* args for lookup() */
141 #define FIND    0
142 #define ENTER   1
143
144 static void expdef(usch *protostruct recur *, int gotwarn);
145 static void savch(int c);
146 static void control(void);
147 static usch *savstr(usch *str);
148 static void define(void);
149 static void expmac(struct recur *);
150 static int canexpand(struct recur *, struct symtab *np);
ragge
1.3
151 static void include(void);
ragge
1.9
152 static void line(void);
ragge
1.1
153
154 int
155 main(int argcchar **argv)
156 {
ragge
1.20
157         struct incs *w, *w2;
ragge
1.7
158         struct symtab *nl, *thisnl;
159         register int cgotspcch;
160         usch *osp;
ragge
1.15
161
ragge
1.7
162         while ((ch = getopt(argcargv"D:I:S:U:td")) != -1)
ragge
1.1
163                 switch (ch) {
ragge
1.7
164                 case 'D'/* Define something */
165                         osp = optarg;
166                         while (*osp && *osp != '=')
167                                 osp++;
168                         if (*osp == '=') {
ragge
1.10
169                                 *osp++ = 0;
170                                 while (*osp)
171                                         osp++;
172                                 *osp = OBJCT;
ragge
1.7
173                         } else {
ragge
1.10
174                                 static char c[3] = { 0'1'OBJCT };
175                                 osp = &c[2];
ragge
1.7
176                         }
177                         nl = lookup(optargENTER);
178                         if (nl->value)
179                                 error("%s redefined"optarg);
180                         nl->value = osp;
181                         break;
182
183                 case 'S':
ragge
1.10
184                 case 'I':
185                         w = calloc(sizeof(struct incs), 1);
186                         w->dir = optarg;
187                         w2 = incdir[ch == 'I' ? INCINC : SYSINC];
188                         if (w2 != NULL) {
189                                 while (w2->next)
190                                         w2 = w2->next;
191                                 w2->next = w;
192                         } else
193                                 incdir[ch == 'I' ? INCINC : SYSINC] = w;
ragge
1.7
194                         break;
195
196                 case 'U':
197                         nl = lookup(optargFIND);
ragge
1.15
198                         if ((nl = lookup(optargFIND)))
ragge
1.7
199                                 nl->value = NULL;
200                         break;
ragge
1.1
201 #ifdef CPP_DEBUG
202                 case 'd':
ragge
1.18
203                         dflag++;
ragge
1.1
204                         break;
205 #endif
206                 case 't':
207                         tflag = 1;
208                         break;
209
210                 default:
211                         fprintf(stderr"bad arg %c\n"ch);
212                         exit(1);
213                 }
214         argc -= optind;
215         argv += optind;
216
217         exfail = 0;
ragge
1.7
218         if (argc) {
219                 if (freopen(argv[0], "r"stdin) == NULL) {
220                         fprintf(stderr"Can't open %s"argv[0]);
ragge
1.1
221                         exit(8);
222                 }
223         }
ragge
1.16
224
ragge
1.10
225         if (pushfile(argc ? argv[0] : "<stdin>"))
226                 error("cannot open %s"argv[0]);
ragge
1.1
227
ragge
1.7
228         if (argc == 2) {
229                 if ((obuf = fopen(argv[1], "w")) == 0) {
230                         fprintf(stderr"Can't creat %s\n"argv[1]);
ragge
1.1
231                         exit(8);
232                 }
233         } else
234                 obuf = stdout;
235
236         prtline();
237
ragge
1.12
238         filloc = lookup("__FILE__"ENTER);
239         linloc = lookup("__LINE__"ENTER);
ragge
1.15
240         filloc->value = linloc->value = ""/* Just something */
241
ragge
1.12
242         if (tflag == 0) {
243                 time_t t = time(NULL);
244                 char *n = ctime(&t);
245
246                 /*
247                  * Manually move in the predefined macros.
248                  */
249                 nl = lookup("__TIME__"ENTER);
ragge
1.13
250                 savch(0); savch('"');  n[19] = 0savstr(&n[11]); savch('"');
ragge
1.12
251                 savch(OBJCT);
252                 nl->value = stringbuf-1;
253
254                 nl = lookup("__DATE__"ENTER);
ragge
1.13
255                 savch(0); savch('"'); n[24] = n[11] = 0savstr(&n[4]);
256                 savstr(&n[20]); savch('"'); savch(OBJCT);
ragge
1.12
257                 nl->value = stringbuf-1;
258
259                 nl = lookup("__STDC__"ENTER);
ragge
1.13
260                 savch(0); savch('1'); savch(OBJCT);
ragge
1.12
261                 nl->value = stringbuf-1;
262         }
ragge
1.1
263
264         thisnl = NULL;
265         while ((c = yylex()) != 0) {
266                 switch (c) {
267                 case CONTROL:
268                         control();
269                         break;
270
271                 case IDENT:
ragge
1.8
272                         if (flslvl)
273                                 break;
ragge
1.1
274                         osp = stringbuf;
ragge
1.15
275                         nl = lookup(yystrFIND);
ragge
1.1
276                         if (nl == 0 || thisnl == 0)
277                                 goto found;
278                         if (thisnl == nl) {
279                                 nl = 0;
280                                 goto found;
281                         }
ragge
1.2
282                         gotspc = 0;
ragge
1.1
283                         if ((c = yylex()) == WSPACE)
ragge
1.2
284                                 gotspc = 1c = yylex();
ragge
1.1
285                         if (c != EXPAND) {
ragge
1.15
286                                 unpstr(yystr);
ragge
1.2
287                                 if (gotspc)
288                                         cunput(' ');
ragge
1.1
289                                 unpstr(nl->namep);
ragge
1.15
290                                 (void)yylex(); /* get yystr correct */
ragge
1.1
291                                 nl = 0/* ignore */
ragge
1.21
292                         } else {
ragge
1.1
293                                 thisnl = NULL;
ragge
1.21
294                                 if (nl->value[0] == OBJCT) {
295                                         unpstr(nl->namep);
296                                         (void)yylex(); /* get yystr correct */
297                                         nl = 0;
298                                 }
299                         }
ragge
1.1
300
ragge
1.15
301 found:                  if (nl == 0 || subst(yystrnlNULL) == 0) {
302                                 fputs(yystrobuf);
ragge
1.1
303                         } else if (osp != stringbuf) {
304                                 cunput(EXPAND);
305                                 while (stringbuf > osp)
306                                         cunput(*--stringbuf);
307                                 thisnl = nl;
308                         }
309                         stringbuf = osp/* clean up heap */
310                         break;
311
312                 case EXPAND:
313                         thisnl = NULL;
314                         break;
315
ragge
1.15
316                 case NL:
317                         if (flslvl == 0)
318                                 putc('\n'obuf);
319                         break;
320
ragge
1.1
321                 case CHARCON:
322                 case NUMBER:
323                 case FPOINT:
324                 case STRING:
325                 case WSPACE:
ragge
1.3
326                 default:
ragge
1.8
327                         if (flslvl == 0)
ragge
1.15
328                                 fputs(yystrobuf);
ragge
1.1
329                         break;
330                 }
331         }
332         fclose(obuf);
ragge
1.16
333         if (trulvl || flslvl)
334                 error("unterminated conditional");
ragge
1.7
335         return exfail;
ragge
1.1
336 }
337
338 /*
339  * do something when a '#' is found.
340  */
341 void
342 control()
343 {
344         struct symtab *np;
345         int t;
346
ragge
1.15
347 #define CHECK(x) (yystr[0] == #x[0]) && strcmp(yystr, #x) == 0
ragge
1.8
348
ragge
1.1
349         if ((t = yylex()) == WSPACE)
350                 t = yylex();
ragge
1.14
351         if (t == NL) {
352                 /* Just ignore */
353                 putc('\n'obuf);
354                 return;
355         }
ragge
1.1
356         if (t != IDENT)
ragge
1.15
357                 return error("bad control '%s'"yystr);
ragge
1.1
358
ragge
1.8
359         if (CHECK(include)) {
ragge
1.1
360                 if (flslvl)
361                         goto exit;
ragge
1.3
362                 include();
ragge
1.1
363                 return;
ragge
1.8
364         } else if (CHECK(else)) {
ragge
1.1
365                 if (flslvl) {
ragge
1.10
366                         if (elflvl > trulvl)
367                                 ;
368                         else if (--flslvl!=0) {
ragge
1.1
369                                 flslvl++;
ragge
1.8
370                         } else {
ragge
1.1
371                                 trulvl++;
ragge
1.8
372                                 prtline();
373                         }
ragge
1.1
374                 } else if (trulvl) {
375                         flslvl++;
376                         trulvl--;
377                 } else
378                         error("If-less else");
ragge
1.10
379                 if (elslvl==trulvl+flslvlerror("Too many else");
380                 elslvl=trulvl+flslvl;
ragge
1.8
381         } else if (CHECK(endif)) {
382                 if (flslvl) {
383                         flslvl--;
384                         if (flslvl == 0)
385                                 prtline();
386                 } else if (trulvl)
387                         trulvl--;
388                 else
389                         error("If-less endif");
ragge
1.10
390                 if (flslvl == 0)
391                         elflvl = 0;
392                 elslvl = 0;
ragge
1.8
393         } else if (CHECK(error)) {
394                 usch *ch = stringbuf;
ragge
1.11
395                 if (flslvl)
396                         goto exit;
ragge
1.8
397                 while (yylex() != NL)
ragge
1.15
398                         savstr(yystr);
ragge
1.8
399                 savch('\n');
400                 error("error: %s"ch);
ragge
1.9
401 #define GETID() if (yylex() != WSPACE || yylex() != IDENT) goto cfail
402         } else if (CHECK(define)) {
ragge
1.10
403                 if (flslvl)
404                         goto exit;
ragge
1.9
405                 GETID();
406                 define();
407         } else if (CHECK(ifdef)) {
408                 GETID();
ragge
1.15
409                 if (flslvl == 0 && lookup(yystrFIND) != 0)
ragge
1.9
410                         trulvl++;
411                 else
412                         flslvl++;
413         } else if (CHECK(ifndef)) {
414                 GETID();
ragge
1.15
415                 if (flslvl == 0 && lookup(yystrFIND) == 0)
ragge
1.9
416                         trulvl++;
417                 else
418                         flslvl++;
419         } else if (CHECK(undef)) {
420                 GETID();
ragge
1.15
421                 if (flslvl == 0 && (np = lookup(yystrFIND)))
ragge
1.9
422                         np->value = 0;
423         } else if (CHECK(line)) {
ragge
1.10
424                 if (flslvl)
425                         goto exit;
ragge
1.9
426                 line();
ragge
1.10
427         } else if (CHECK(if)) {
428                 if (flslvl==0 && yyparse())
429                         ++trulvl;
430                 else
431                         ++flslvl;
ragge
1.16
432         } else if (CHECK(pragma)) {
433                 goto exit;
ragge
1.10
434         } else if (CHECK(elif)) {
435                 if (flslvl == 0)
436                         elflvl = trulvl;
437                 if (flslvl) {
438                         if (elflvl > trulvl)
439                                 ;
440                         else if (--flslvl!=0)
441                                 ++flslvl;
442                         else {
443                                 if (yyparse()) {
444                                         ++trulvl;
445                                         prtline();
446                                 } else
447                                         ++flslvl;
448                         }
449                 } else if (trulvl) {
450                         ++flslvl;
451                         --trulvl;
452                 } else
453                         error("If-less elif");
ragge
1.9
454         } else
ragge
1.17
455 #if 0
ragge
1.15
456                 error("undefined control '%s'"yystr);
ragge
1.17
457 #else
458                 goto exit;
459 #endif
ragge
1.9
460
461         return;
462
463 cfail:
464         error("control line syntax error");
ragge
1.1
465
466 exit:
467         while (yylex() != NL)
468                 ;
ragge
1.10
469         putc('\n'obuf);
ragge
1.8
470 #undef CHECK
ragge
1.1
471 }
472
473 void
ragge
1.9
474 line()
475 {
476         struct symtab *nl;
477         int c;
478
479         if (yylex() != WSPACE)
480                 goto bad;
481         if ((c = yylex()) == IDENT) {
482                 /* Do macro preprocessing first */
483                 usch *osp = stringbuf;
ragge
1.15
484                 if ((nl = lookup(yystrFIND)) == NULL)
ragge
1.9
485                         goto bad;
ragge
1.15
486                 if (subst(yystrnlNULL) == 0)
ragge
1.9
487                         goto bad;
488                 while (stringbuf > osp)
489                         cunput(*--stringbuf);
490                 c = yylex();
491         }
492
493         if (c != NUMBER)
494                 goto bad;
ragge
1.15
495         setline(atoi(yystr));
ragge
1.9
496
497         if ((c = yylex()) != NL && c != WSPACE)
498                 goto bad;
499         if (c == NL)
500                 return setline(curline()+1);
501         if (yylex() != STRING)
502                 goto bad;
ragge
1.15
503         yystr[strlen(yystr)-1] = 0;
504         setfile(&yystr[1]);
ragge
1.9
505         return;
506
507 bad:    error("bad line directive");
508 }
509
ragge
1.10
510 /*
511  * Include a file. Include order:
ragge
1.20
512  * - For <...> files, first search -I directories, then system directories.
513  * - For "..." files, first search "current" dir, then as <...> files.
ragge
1.10
514  */
ragge
1.9
515 void
ragge
1.3
516 include()
517 {
ragge
1.10
518         struct incs *w;
ragge
1.3
519         struct symtab *nl;
ragge
1.10
520         usch *osp;
521         char *fn;
522         int icit;
ragge
1.3
523
ragge
1.10
524         osp = stringbuf;
ragge
1.3
525         if (yylex() != WSPACE)
526                 goto bad;
ragge
1.10
527 again:  if ((c = yylex()) != STRING && c != '<' && c != IDENT)
ragge
1.3
528                 goto bad;
529
530         if (c == IDENT) {
ragge
1.15
531                 if ((nl = lookup(yystrFIND)) == NULL)
ragge
1.3
532                         goto bad;
ragge
1.15
533                 if (subst(yystrnlNULL) == 0)
ragge
1.3
534                         goto bad;
535                 savch('\0');
ragge
1.10
536                 unpstr(osp);
537                 goto again;
538         } else if (c == '<') {
539                 fn = stringbuf;
540                 while ((c = yylex()) != '>' && c != NL) {
541                         if (c == NL)
542                                 goto bad;
ragge
1.15
543                         savstr(yystr);
ragge
1.10
544                 }
545                 savch('\0');
ragge
1.20
546                 it = SYSINC;
ragge
1.3
547         } else {
ragge
1.20
548                 usch *nm = stringbuf;
549
ragge
1.15
550                 yystr[strlen(yystr)-1] = 0;
551                 fn = &yystr[1];
ragge
1.20
552                 /* first try to open file relative to previous file */
553                 savstr(curfile());
554                 if ((stringbuf = strrchr(nm'/')) == NULL)
555                         stringbuf = nm;
556                 else
557                         stringbuf++;
558                 savstr(fn); savch(0);
559                 if (pushfile(nm) == 0)
560                         goto ret;
561                 stringbuf = nm;
ragge
1.10
562         }
563
564         /* create search path and try to open file */
ragge
1.20
565         for (i = 0i < 2i++) {
ragge
1.10
566                 for (w = incdir[i]; ww = w->next) {
567                         usch *nm = stringbuf;
568
569                         savstr(w->dir); savch('/');
570                         savstr(fn); savch(0);
571                         if (pushfile(nm) == 0)
572                                 goto ret;
573                         stringbuf = nm;
574                 }
ragge
1.3
575         }
ragge
1.10
576         error("cannot find '%s'"fn);
577         stringbuf = osp;
ragge
1.3
578         return;
579
580 bad:    error("bad include");
ragge
1.10
581 ret:    prtline();
582         stringbuf = osp;
ragge
1.3
583 }
584
585 void
ragge
1.1
586 define()
587 {
588         struct symtab *np;
ragge
1.15
589         usch *args[MAXARG], *ubuf, *sbeg;
590         int ciredef;
ragge
1.1
591         int mkstr = 0narg = -1;
592
ragge
1.15
593         np = lookup(yystrENTER);
594         redef = np->value != NULL;
ragge
1.1
595
ragge
1.15
596         sbeg = stringbuf;
ragge
1.1
597         if ((c = yylex()) == '(') {
598                 narg = 0;
599                 /* function-like macros, deal with identifiers */
600                 while ((c = yylex()) != ')') {
601                         if (c == WSPACEc = yylex();
602                         if (c == ','c = yylex();
603                         if (c == WSPACEc = yylex();
604                         if (c == ')')
605                                 break;
ragge
1.15
606                         if (c != IDENT)
607                                 error("define error");
608                         args[narg] = alloca(strlen(yystr)+1);
609                         strcpy(args[narg], yystr);
ragge
1.1
610                         narg++;
611                 }
ragge
1.8
612         } else if (c == NL) {
613                 /* #define foo */
614                 cunput('\n');
ragge
1.1
615         } else if (c != WSPACE)
616                 error("bad define");
617
618         if ((c = yylex()) == WSPACE)
619                 c = yylex();
620
621         /* parse replacement-list, substituting arguments */
622         savch('\0');
623         while (c != NL) {
624                 switch (c) {
625                 case WSPACE:
626                         /* remove spaces if it surrounds a ## directive */
ragge
1.2
627                         ubuf = stringbuf;
ragge
1.15
628                         savstr(yystr);
ragge
1.1
629                         c = yylex();
630                         if (c == CONCAT) {
ragge
1.2
631                                 stringbuf = ubuf;
ragge
1.1
632                                 savch(CONC);
633                                 if ((c = yylex()) == WSPACE)
634                                         c = yylex();
635                         }
636                         continue;
637
638                 case CONCAT:
639                         /* No spaces before concat op */
640                         savch(CONC);
641                         if ((c = yylex()) == WSPACE)
642                                 c = yylex();
643                         continue;
644
645                 case MKSTR:
646                         if (narg < 0) {
647                                 /* no meaning in object-type macro */
648                                 savch('#');
649                                 break;
650                         }
651                         /* remove spaces between # and arg */
652                         savch(SNUFF);
653                         if ((c = yylex()) == WSPACE)
654                                 c = yylex(); /* whitespace, ignore */
655                         mkstr = 1;
656
657                         /* FALLTHROUGH */
658                 case IDENT:
659                         if (narg < 0)
660                                 goto id/* just add it if object */
661                         /* check if its an argument */
662                         for (i = 0i < nargi++)
ragge
1.15
663                                 if (strcmp(yystrargs[i]) == 0)
ragge
1.1
664                                         break;
665                         if (i == narg) {
666                                 if (mkstr)
667                                         error("not argument");
668                                 goto id;
669                         }
670                         savch(i);
671                         savch(WARN);
672                         if (mkstr)
673                                 savch(SNUFF), mkstr = 0;
674                         break;
675
676                 default:
ragge
1.15
677 id:                     savstr(yystr);
ragge
1.1
678                         break;
679                 }
680                 c = yylex();
681         }
ragge
1.20
682         /* remove trailing whitespace */
683         while (stringbuf > sbeg) {
684                 if (stringbuf[-1] == ' ' || stringbuf[-1] == '\t')
685                         stringbuf--;
686                 else
687                         break;
688         }
ragge
1.1
689         savch(narg < 0 ? OBJCT : narg);
ragge
1.15
690         if (redef) {
691                 usch *o = np->value, *n = stringbuf-1;
692
693                 /* Redefinition to identical replacement-list is allowed */
694                 while (*o && *o == *n)
695                         o--, n--;
696                 if (*o || *o != *n)
697                         error("%s redefined"np->namep);
698                 stringbuf = sbeg;  /* forget this space */
699         } else
700                 np->value = stringbuf-1;
ragge
1.2
701         putc('\n'obuf);
ragge
1.1
702
703 #ifdef CPP_DEBUG
704         if (dflag) {
705                 usch *w = np->value;
706
707                 printf("!define: ");
708                 if (*w == OBJCT)
709                         printf("[object]");
710                 else
711                         printf("[%d]", *w);
712                 while (*--w) {
713                         switch (*w) {
714                         case WARNprintf("<%d>", *--w); break;
715                         case CONCprintf("<##>"); break;
716                         case SNUFFprintf("<\">"); break;
717                         defaultputchar(*w); break;
718                         }
719                 }
720                 putchar('\n');
721         }
722 #endif
723 }
724
725 void
726 error(char *s, ...)
727 {
728         va_list ap;
729
730         va_start(aps);
731         fprintf(stderr"%s:%d: "curfile(), curline());
732         vfprintf(stderrsap);
733         fputc('\n'stderr);
734         exfail++;
735         va_end(ap);
736         exit(8);
737 }
738
739 /*
740  * store a character into the "define" buffer.
741  */
742 void
743 savch(c)
744 {
745         *stringbuf++ = c;
746         if (stringbuf-sbf < SBSIZE)
747                 return;
748         error("Too much defining");
749         exit(1);
750 }
751
752 /*
753  * Do a symbol lookup.
ragge
1.15
754  * If enterf == ENTER, create a new entry.
755  * will return NULL if symbol not found and FIND is given.
ragge
1.1
756  */
757 struct symtab *
758 lookup(namepenterf)
759         char *namep;
760 {
761         register char *np;
762         register struct symtab *sp;
763         int icaround;
764
ragge
1.3
765 if (dflag)printf("lookup '%s'\n"namep);
ragge
1.1
766         np = namep;
767         around = i = 0;
768         while ((c = *np++))
ragge
1.18
769                 i += c;
ragge
1.1
770         i %= SYMSIZ;
771         sp = &symtab[i];
772
773         while (sp->namep) {
774                 if (*sp->namep == *namep && strcmp(sp->namepnamep) == 0)
ragge
1.15
775                         return sp->value || enterf == ENTER ? sp : NULL;
ragge
1.1
776                 if (++sp >= &symtab[SYMSIZ]) {
777                         if (around++)
778                                 error("too many defines");
779                         else
780                                 sp = symtab;
781                 }
782         }
ragge
1.15
783         if (enterf == ENTER)
ragge
1.12
784                 sp->namep = savstr(namep), savch('\0'), sp->value = NULL;
ragge
1.15
785
ragge
1.1
786         return(sp->namep ? sp : 0);
787 }
788
789 /*
790  * substitute namep for sp->value.
791  */
792 int
793 subst(npsprp)
794 char *np;
795 struct symtab *sp;
796 struct recur *rp;
797 {
798         struct recur rp2;
799         register usch *vp, *cp;
800         int crv = 0;
801
ragge
1.17
802 if (dflag)printf("subst: %s\n"sp->namep);
803         /*
804          * First check for special macros.
805          */
806         if (sp == filloc) {
807                 savch('"');
808                 savstr(curfile());
809                 savch('"');
810                 return 1;
811         } else if (sp == linloc) {
812                 char buf[12];
813                 sprintf(buf"%d"curline());
814                 savstr(buf);
ragge
1.1
815                 return 1;
816         }
ragge
1.17
817         vp = sp->value;
ragge
1.1
818
819         rp2.next = rp;
820         rp2.sp = sp;
821
822         if (*vp-- != OBJCT) {
823                 int gotwarn = 0;
824
825                 /* should we be here at all? */
826                 /* check if identifier is followed by parentheses */
827                 rv = 1;
828                 do {
829                         if ((c = yylex()) == NL)
830                                 putc('\n'obuf);
831                         if (c == WARN) {
832                                 gotwarn++;
833                                 if (rp == NULL)
834                                         goto noid;
835                         }
836                 } while (c == WSPACE || c == NL || c == WARN);
837
ragge
1.15
838                 cp = yystr;
ragge
1.1
839                 while (*cp)
840                         cp++;
ragge
1.15
841                 while (cp > (usch *)yystr)
ragge
1.1
842                         cunput(*--cp);
843 if (dflag)printf("c %d\n"c);
844                 if (c == '(' ) {
845                         expdef(vp, &rp2gotwarn);
846                         return rv;
847                 } else {
848                         /* restore identifier */
849 noid:                   while (gotwarn--)
850                                 cunput(WARN);
851                         cunput(' ');
852                         cp = sp->namep;
853                         while (*cp)
854                                 cp++;
855                         while (cp > sp->namep)
856                                 cunput(*--cp);
857                         if ((c = yylex()) != IDENT)
858                                 error("internal sync error");
859                         return 0;
860                 }
861         } else {
862                 cunput(WARN);
863                 cp = vp;
864                 while (*cp) {
865                         if (*cp != CONC)
866                                 cunput(*cp);
867                         cp--;
868                 }
869                 expmac(&rp2);
870         }
871         return 1;
872 }
873
874 /*
875  * do macro-expansion until WARN character read.
876  * will recurse into lookup() for recursive expansion.
877  * when returning all expansions on the token list is done.
878  */
879 void
880 expmac(struct recur *rp)
881 {
882         struct symtab *nl;
ragge
1.2
883         int cnoexp = 0gotspc;
ragge
1.4
884         usch *och;
ragge
1.1
885
886 if (dflag)printf("expmac\n");
887 if (dflag && rp)printf("do not expand %s\n"rp->sp->namep);
888         while ((c = yylex()) != WARN) {
889                 switch (c) {
890                 case NOEXPnoexp++; break;
891                 case EXPANDnoexp--; break;
892
893                 case IDENT:
ragge
1.4
894                         /* workaround if an arg will be concatenated */
895                         och = stringbuf;
ragge
1.15
896                         savstr(yystr);
ragge
1.4
897                         savch('\0');
ragge
1.18
898 if (dflag > 1)printf("id: str %s\n"och);
ragge
1.4
899                         if ((c = yylex()) == EXPAND) {
ragge
1.18
900 if (dflag > 1)printf("funnet expand\n");
ragge
1.4
901                                 if ((c = yylex()) == NOEXP) {
ragge
1.18
902 if (dflag > 1)printf("funnet noexp\n");
ragge
1.4
903                                         if ((c = yylex()) == IDENT) {
ragge
1.18
904 yid:
905 if (dflag > 1)printf("funnet ident %s%s\n"ochyystr);
ragge
1.4
906                                                 stringbuf--;
ragge
1.15
907                                                 savstr(yystr);
ragge
1.4
908                                                 savch('\0');
909                                                 cunput(NOEXP);
910                                                 unpstr(och);
911                                                 noexp--;
912                                                 stringbuf = och;
913                                                 continue;
914                                         } else {
ragge
1.18
915 if (dflag > 1)printf("ofunnet ident\n");
ragge
1.15
916                                                 unpstr(yystr);
ragge
1.4
917                                                 unpstr(och);
918                                                 stringbuf = och;
919                                                 continue;
920                                         }
921                                 } else {
ragge
1.18
922                                         if (c == IDENT)
923                                                 goto yid;
924 if (dflag > 1)printf("ofunnet inoexp\n");
ragge
1.15
925                                         unpstr(yystr);
ragge
1.4
926                                         cunput(EXPAND);
927                                         unpstr(och);
928                                         yylex();
929                                 }
930                         } else {
ragge
1.18
931 if (dflag > 1)printf("ofunnet expand got (%d)\n"c);
932                                 if (c == NOEXP) {
933                                         if ((c = yylex()) == IDENT) {
934                                                 noexp++;
935                                                 goto yid;
936                                         }
937                                         unpstr(yystr);
938                                         cunput(NOEXP);
939                                 } else
940                                         unpstr(yystr);
941 if (dflag > 1)printf("ofunnet expand yys (%d)\n", *yystr);
ragge
1.4
942                                 unpstr(och);
943                                 yylex();
ragge
1.18
944 if (dflag > 1)printf("ofunnet expand: yystr %s\n"yystr);
ragge
1.4
945                         }
946                         stringbuf = och;
947
ragge
1.15
948                         if ((nl = lookup(yystrFIND)) == NULL)
ragge
1.1
949                                 goto def;
ragge
1.4
950
ragge
1.1
951                         if (canexpand(rpnl) == 0)
952                                 goto def;
953                         if (noexp == 0) {
954                                 if ((c = subst(nl->namepnlrp)) == 0)
955                                         goto def;
956                                 break;
957                         }
958                         if (noexp != 1)
959                                 error("bad noexp %d"noexp);
ragge
1.2
960                         gotspc = 0;
ragge
1.1
961                         if ((c = yylex()) == WSPACE)
ragge
1.2
962                                 gotspc = 1c = yylex();
ragge
1.1
963                         if (c == EXPAND) {
964                                 noexp--;
965                                 if (subst(nl->namepnlrp))
966                                         break;
967                                 savstr(nl->namep);
ragge
1.2
968                                 if (gotspc)
969                                         savch(' ');
ragge
1.1
970                         } else {
ragge
1.15
971                                 unpstr(yystr);
ragge
1.2
972                                 if (gotspc)
973                                         cunput(' ');
ragge
1.1
974                                 savstr(nl->namep);
975                         }
976                         break;
977
ragge
1.15
978                 case STRING:
979                         /* remove EXPAND/NOEXP from strings */
980                         if (yystr[1] == NOEXP) {
981                                 savch('"');
982                                 och = &yystr[2];
983                                 while (*och != EXPAND)
984