Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050404164947

Diff

Diff from 1.25 to:

Annotations

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

Annotated File View

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