Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20041002080018

Diff

Diff from 1.17 to:

Annotations

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

Annotated File View

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