Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050514132813

Diff

Diff from 1.28 to:

Annotations

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

Annotated File View

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