Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20070808202914

Diff

Diff from 1.51 to:

Annotations

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

Annotated File View

ragge
1.51
1 /*      $Id: cpp.c,v 1.51 2007/08/08 20:29:14 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>
ragge
1.51
81 #include <ctype.h>
ragge
1.1
82
pj
1.23
83 #ifdef HAVE_ALLOCA_H
84 #include <alloca.h>
85 #endif
86
ragge
1.1
87 #include "cpp.h"
ragge
1.36
88 #include "y.tab.h"
ragge
1.1
89
90 #define MAXARG  250     /* # of args to a macro, limited by char value */
ragge
1.47
91 #define SBSIZE  400000
ragge
1.1
92
93 static usch     sbf[SBSIZE];
94 /* C command */
95
96 int tflag;      /* traditional cpp syntax */
97 #ifdef CPP_DEBUG
98 int dflag;      /* debug printouts */
ragge
1.36
99 #define DPRINT(x) if (dflag) printf x
100 #define DDPRINT(x) if (dflag > 1) printf x
101 #else
102 #define DPRINT(x)
103 #define DDPRINT(x)
ragge
1.1
104 #endif
ragge
1.36
105
ragge
1.51
106 int ofd;
ragge
1.37
107 static usch outbuf[CPPBUF];
108 static int obufpistty;
ragge
1.51
109 int CflagMflag;
110 usch *Mfile;
ragge
1.1
111
112 /* avoid recursion */
113 struct recur {
114         struct recur *next;
115         struct symtab *sp;
116 };
117
ragge
1.10
118 /* include dirs */
119 struct incs {
120         struct incs *next;
ragge
1.39
121         usch *dir;
ragge
1.20
122 } *incdir[2];
123 #define INCINC 0
124 #define SYSINC 1
ragge
1.10
125
ragge
1.1
126 static struct symtab *filloc;
127 static struct symtab *linloc;
ragge
1.10
128 int     trulvl;
129 int     flslvl;
130 int     elflvl;
131 int     elslvl;
132 usch *stringbuf = sbf;
ragge
1.1
133
134 /*
135  * Macro replacement list syntax:
136  * - For object-type macros, replacement strings are stored as-is.
137  * - For function-type macros, macro args are substituted for the
138  *   character WARN followed by the argument number.
ragge
1.12
139  * - The value element points to the end of the string, to simplify
140  *   pushback onto the input queue.
ragge
1.1
141  * 
ragge
1.12
142  * The first character (from the end) in the replacement list is
143  * the number of arguments:
ragge
1.37
144  *   VARG  - ends with ellipsis, next char is argcount without ellips.
ragge
1.1
145  *   OBJCT - object-type macro
146  *   0     - empty parenthesis, foo()
147  *   1->   - number of args.
148  */
149
ragge
1.37
150 #define VARG    0xfe    /* has varargs */
ragge
1.1
151 #define OBJCT   0xff
152 #define WARN    1       /* SOH, not legal char */
153 #define CONC    2       /* STX, not legal char */
154 #define SNUFF   3       /* ETX, not legal char */
155 #define NOEXP   4       /* EOT, not legal char */
156 #define EXPAND  5       /* ENQ, not legal char */
157
158 /* args for lookup() */
159 #define FIND    0
160 #define ENTER   1
161
162 static void expdef(usch *protostruct recur *, int gotwarn);
ragge
1.36
163 void define(void);
ragge
1.1
164 static int canexpand(struct recur *, struct symtab *np);
ragge
1.36
165 void include(void);
166 void line(void);
ragge
1.37
167 void flbuf(void);
168 void usage(void);
ragge
1.1
169
170 int
171 main(int argcchar **argv)
172 {
ragge
1.20
173         struct incs *w, *w2;
ragge
1.26
174         struct symtab *nl;
175         register int ch;
176         usch *osp;
ragge
1.15
177
ragge
1.51
178         while ((ch = getopt(argcargv"CD:I:MS:U:td")) != -1)
ragge
1.1
179                 switch (ch) {
ragge
1.22
180                 case 'C'/* Do not discard comments */
181                         Cflag++;
182                         break;
183
ragge
1.7
184                 case 'D'/* Define something */
ragge
1.39
185                         osp = (usch *)optarg;
ragge
1.7
186                         while (*osp && *osp != '=')
187                                 osp++;
188                         if (*osp == '=') {
ragge
1.10
189                                 *osp++ = 0;
190                                 while (*osp)
191                                         osp++;
192                                 *osp = OBJCT;
ragge
1.7
193                         } else {
ragge
1.39
194                                 static usch c[3] = { 0'1'OBJCT };
ragge
1.10
195                                 osp = &c[2];
ragge
1.7
196                         }
ragge
1.39
197                         nl = lookup((usch *)optargENTER);
ragge
1.42
198                         if (nl->value) {
199                                 /* check for redefinition */
200                                 usch *o = nl->value, *n = osp;
201                                 while (*o && *o == *n)
202                                         o--, n--;
203                                 if (*o || *o != *n)
204                                         error("%s redefined"optarg);
205                         }
ragge
1.7
206                         nl->value = osp;
207                         break;
208
ragge
1.51
209                 case 'M'/* Generate dependencies for make */
210                         Mflag++;
211                         break;
212
ragge
1.7
213                 case 'S':
ragge
1.10
214                 case 'I':
215                         w = calloc(sizeof(struct incs), 1);
ragge
1.39
216                         w->dir = (usch *)optarg;
ragge
1.10
217                         w2 = incdir[ch == 'I' ? INCINC : SYSINC];
218                         if (w2 != NULL) {
219                                 while (w2->next)
220                                         w2 = w2->next;
221                                 w2->next = w;
222                         } else
223                                 incdir[ch == 'I' ? INCINC : SYSINC] = w;
ragge
1.7
224                         break;
225
226                 case 'U':
ragge
1.39
227                         if ((nl = lookup((usch *)optargFIND)))
ragge
1.7
228                                 nl->value = NULL;
229                         break;
ragge
1.1
230 #ifdef CPP_DEBUG
231                 case 'd':
ragge
1.18
232                         dflag++;
ragge
1.1
233                         break;
234 #endif
235                 case 't':
236                         tflag = 1;
237                         break;
238
ragge
1.37
239                 case '?':
240                         usage();
ragge
1.1
241                 default:
ragge
1.37
242                         error("bad arg %c\n"ch);
ragge
1.1
243                 }
244         argc -= optind;
245         argv += optind;
246
ragge
1.39
247         filloc = lookup((usch *)"__FILE__"ENTER);
248         linloc = lookup((usch *)"__LINE__"ENTER);
249         filloc->value = linloc->value = (usch *)""/* Just something */
ragge
1.15
250
ragge
1.12
251         if (tflag == 0) {
252                 time_t t = time(NULL);
ragge
1.39
253                 usch *n = (usch *)ctime(&t);
ragge
1.12
254
255                 /*
256                  * Manually move in the predefined macros.
257                  */
ragge
1.39
258                 nl = lookup((usch *)"__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
ragge
1.39
263                 nl = lookup((usch *)"__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
ragge
1.39
268                 nl = lookup((usch *)"__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
ragge
1.51
273         if (Mflag) {
274                 usch *c;
275
276                 if (argc < 1)
277                         error("-M and no infile");
278                 if ((c = (usch *)strrchr(argv[0], '/')) == NULL)
279                         c = (usch *)argv[0];
280                 else
281                         c++;
282                 Mfile = stringbuf;
283                 savstr(c); savch(0);
284                 if ((c = (usch *)strrchr((char *)Mfile'.')) == NULL)
285                         error("-M and no extension: ");
286                 c[1] = 'o';
287                 c[2] = 0;
288         }
289
ragge
1.26
290         if (argc == 2) {
ragge
1.37
291                 if ((ofd = open(argv[1], O_WRONLY|O_CREAT0600)) < 0)
292                         error("Can't creat %s"argv[1]);
ragge
1.26
293         } else
ragge
1.37
294                 ofd = 1/* stdout */
295         istty = isatty(ofd);
ragge
1.26
296
ragge
1.41
297         if (pushfile((usch *)(argc && strcmp(argv[0], "-") ? argv[0] : NULL)))
ragge
1.26
298                 error("cannot open %s"argv[0]);
299
ragge
1.37
300         flbuf();
301         close(ofd);
302         return 0;
ragge
1.26
303 }
304
ragge
1.37
305 /*
306  * Expand the symbol nl read from input.
307  * Return a pointer to the fully expanded result.
308  * It is the responsibility of the caller to reset the heap usage.
309  */
310 usch *
ragge
1.36
311 gotident(struct symtab *nl)
ragge
1.35
312 {
ragge
1.36
313         struct symtab *thisnl;
ragge
1.37
314         usch *osp, *ss2, *base;
ragge
1.36
315         int c;
316
317         thisnl = NULL;
318         slow = 1;
ragge
1.37
319         base = osp = stringbuf;
ragge
1.36
320         goto found;
ragge
1.35
321
ragge
1.36
322         while ((c = yylex()) != 0) {
ragge
1.35
323                 switch (c) {
ragge
1.36
324                 case IDENT:
325                         if (flslvl)
326                                 break;
327                         osp = stringbuf;
ragge
1.35
328
ragge
1.36
329                         DPRINT(("IDENT0: %s\n"yytext));
ragge
1.39
330                         nl = lookup((usch *)yytextFIND);
ragge
1.36
331                         if (nl == 0 || thisnl == 0)
332                                 goto found;
333                         if (thisnl == nl) {
334                                 nl = 0;
335                                 goto found;
336                         }
337                         ss2 = stringbuf;
338                         if ((c = yylex()) == WSPACE) {
ragge
1.39
339                                 savstr((usch *)yytext);
ragge
1.36
340                                 c = yylex();
341                         }
342                         if (c != EXPAND) {
ragge
1.39
343                                 unpstr((usch *)yytext);
ragge
1.36
344                                 if (ss2 != stringbuf)
345                                         unpstr(ss2);
346                                 unpstr(nl->namep);
347                                 (void)yylex(); /* get yytext correct */
348                                 nl = 0/* ignore */
ragge
1.35
349                         } else {
ragge
1.36
350                                 thisnl = NULL;
351                                 if (nl->value[0] == OBJCT) {
352                                         unpstr(nl->namep);
353                                         (void)yylex(); /* get yytext correct */
354                                         nl = 0;
ragge
1.35
355                                 }
356                         }
ragge
1.36
357                         stringbuf = ss2;
358
359 found:                  if (nl == 0 || subst(nlNULL) == 0) {
ragge
1.37
360                                 if (nl)
361                                         savstr(nl->namep);
362                                 else
ragge
1.39
363                                         savstr((usch *)yytext);
ragge
1.36
364                         } else if (osp != stringbuf) {
365                                 DPRINT(("IDENT1: unput osp %p stringbuf %p\n",
366                                     ospstringbuf));
367                                 ss2 = stringbuf;
368                                 cunput(EXPAND);
369                                 while (ss2 > osp)
370                                         cunput(*--ss2);
371                                 thisnl = nl;
ragge
1.37
372                                 stringbuf = osp/* clean up heap */
ragge
1.35
373                         }
374                         break;
375
ragge
1.36
376                 case EXPAND:
377                         DPRINT(("EXPAND!\n"));
378                         thisnl = NULL;
ragge
1.35
379                         break;
380
ragge
1.36
381                 case STRING:
382                 case '\n':
383                 case NUMBER:
384                 case FPOINT:
385                 case WSPACE:
ragge
1.39
386                         savstr((usch *)yytext);
ragge
1.35
387                         break;
388
ragge
1.36
389                 default:
ragge
1.39
390                         if (c < 256)
391                                 savch(c);
392                         else
393                                 savstr((usch *)yytext);
ragge
1.35
394                         break;
ragge
1.36
395                 }
396                 if (thisnl == NULL) {
397                         slow = 0;
ragge
1.37
398                         savch(0);
399                         return base;
ragge
1.1
400                 }
401         }
ragge
1.37
402         error("preamture EOF");
403         /* NOTREACHED */
404         return NULL/* XXX gcc */
ragge
1.1
405 }
406
407 void
ragge
1.9
408 line()
409 {
ragge
1.37
410         static usch *lbuf;
411         static int llen;
ragge
1.9
412         struct symtab *nl;
413         int c;
414
ragge
1.37
415         slow = 1;
ragge
1.9
416         if (yylex() != WSPACE)
417                 goto bad;
418         if ((c = yylex()) == IDENT) {
419                 /* Do macro preprocessing first */
420                 usch *osp = stringbuf;
ragge
1.39
421                 if ((nl = lookup((usch *)yytextFIND)) == NULL)
ragge
1.9
422                         goto bad;
ragge
1.37
423                 unpstr(gotident(nl));
424                 stringbuf = osp;
ragge
1.9
425                 c = yylex();
426         }
427
428         if (c != NUMBER)
429                 goto bad;
ragge
1.37
430         ifiles->lineno = atoi(yytext);
ragge
1.9
431
ragge
1.36
432         if ((c = yylex()) != '\n' && c != WSPACE)
ragge
1.9
433                 goto bad;
ragge
1.37
434         if (c == '\n') {
435                 slow = 0;
436                 return;
437         }
ragge
1.9
438         if (yylex() != STRING)
439                 goto bad;
ragge
1.39
440         c = strlen((char *)yytext);
ragge
1.37
441         if (llen < c) {
442                 /* may loose heap space */
443                 lbuf = stringbuf;
444                 stringbuf += c;
445                 llen = c;
446         }
ragge
1.36
447         yytext[strlen(yytext)-1] = 0;
ragge
1.39
448         strcpy((char *)lbuf, &yytext[1]);
ragge
1.37
449         ifiles->fname = lbuf;
450         if (yylex() != '\n')
451                 goto bad;
452         slow = 0;
ragge
1.9
453         return;
454
455 bad:    error("bad line directive");
456 }
457
ragge
1.10
458 /*
459  * Include a file. Include order:
ragge
1.20
460  * - For <...> files, first search -I directories, then system directories.
461  * - For "..." files, first search "current" dir, then as <...> files.
ragge
1.10
462  */
ragge
1.9
463 void
ragge
1.3
464 include()
465 {
ragge
1.10
466         struct incs *w;
ragge
1.3
467         struct symtab *nl;
ragge
1.10
468         usch *osp;
ragge
1.39
469         usch *fn;
ragge
1.10
470         int icit;
ragge
1.3
471
ragge
1.37
472         if (flslvl)
473                 return;
ragge
1.10
474         osp = stringbuf;
ragge
1.37
475         slow = 1;
ragge
1.3
476         if (yylex() != WSPACE)
477                 goto bad;
ragge
1.10
478 again:  if ((c = yylex()) != STRING && c != '<' && c != IDENT)
ragge
1.3
479                 goto bad;
480
481         if (c == IDENT) {
ragge
1.39
482                 if ((nl = lookup((usch *)yytextFIND)) == NULL)
ragge
1.3
483                         goto bad;
ragge
1.35
484                 if (subst(nlNULL) == 0)
ragge
1.3
485                         goto bad;
486                 savch('\0');
ragge
1.10
487                 unpstr(osp);
488                 goto again;
489         } else if (c == '<') {
490                 fn = stringbuf;
ragge
1.36
491                 while ((c = yylex()) != '>' && c != '\n') {
492                         if (c == '\n')
ragge
1.10
493                                 goto bad;
ragge
1.39
494                         savstr((usch *)yytext);
ragge
1.10
495                 }
496                 savch('\0');
ragge
1.37
497                 while ((c = yylex()) == WSPACE)
498                         ;
499                 if (c != '\n')
500                         goto bad;
ragge
1.20
501                 it = SYSINC;
ragge
1.3
502         } else {
ragge
1.20
503                 usch *nm = stringbuf;
504
ragge
1.36
505                 yytext[strlen(yytext)-1] = 0;
ragge
1.39
506                 fn = (usch *)&yytext[1];
ragge
1.20
507                 /* first try to open file relative to previous file */
ragge
1.51
508                 savstr(ifiles->orgfn);
ragge
1.39
509                 if ((stringbuf = (usch *)strrchr((char *)nm'/')) == NULL)
ragge
1.20
510                         stringbuf = nm;
511                 else
512                         stringbuf++;
513                 savstr(fn); savch(0);
ragge
1.37
514                 while ((c = yylex()) == WSPACE)
515                         ;
516                 if (c != '\n')
517                         goto bad;
518                 slow = 0;
ragge
1.20
519                 if (pushfile(nm) == 0)
ragge
1.37
520                         return;
ragge
1.20
521                 stringbuf = nm;
ragge
1.10
522         }
523
524         /* create search path and try to open file */
ragge
1.37
525         slow = 0;
ragge
1.20
526         for (i = 0i < 2i++) {
ragge
1.10
527                 for (w = incdir[i]; ww = w->next) {
528                         usch *nm = stringbuf;
529
530                         savstr(w->dir); savch('/');
531                         savstr(fn); savch(0);
532                         if (pushfile(nm) == 0)
ragge
1.37
533                                 return;
ragge
1.10
534                         stringbuf = nm;
535                 }
ragge
1.3
536         }
ragge
1.10
537         error("cannot find '%s'"fn);
ragge
1.37
538         /* error() do not return */
ragge
1.3
539
540 bad:    error("bad include");
ragge
1.37
541         /* error() do not return */
542 }
543
544 static int
545 definp(void)
546 {
547         int c;
548
549         do
550                 c = yylex();
551         while (c == WSPACE);
552         return c;
ragge
1.3
553 }
554
555 void
ragge
1.1
556 define()
557 {
558         struct symtab *np;
ragge
1.15
559         usch *args[MAXARG], *ubuf, *sbeg;
560         int ciredef;
ragge
1.1
561         int mkstr = 0narg = -1;
ragge
1.37
562         int ellips = 0;
ragge
1.1
563
ragge
1.37
564         if (flslvl)
565                 return;
ragge
1.36
566         slow = 1;
567         if (yylex() != WSPACE || yylex() != IDENT)
ragge
1.37
568                 goto bad;
ragge
1.36
569
ragge
1.51
570         if (isdigit((int)yytext[0]))
571                 goto bad;
572
ragge
1.39
573         np = lookup((usch *)yytextENTER);
ragge
1.15
574         redef = np->value != NULL;
ragge
1.1
575
ragge
1.15
576         sbeg = stringbuf;
ragge
1.1
577         if ((c = yylex()) == '(') {
578                 narg = 0;
579                 /* function-like macros, deal with identifiers */
ragge
1.37
580                 for (;;) {
581                         c = definp();
ragge
1.1
582                         if (c == ')')
583                                 break;
ragge
1.37
584                         if (c == ELLIPS) {
585                                 ellips = 1;
586                                 if (definp() != ')')
587                                         goto bad;
588                                 break;
589                         }
590                         if (c == IDENT) {
591                                 args[narg] = alloca(strlen(yytext)+1);
ragge
1.39
592                                 strcpy((char *)args[narg], yytext);
ragge
1.37
593                                 narg++;
594                                 if ((c = definp()) == ',')
595                                         continue;
596                                 if (c == ')')
597                                         break;
598                                 goto bad;
599                         }
600                         goto bad;
ragge
1.1
601                 }
ragge
1.36
602                 c = yylex();
603         } else if (c == '\n') {
ragge
1.8
604                 /* #define foo */
ragge
1.36
605                 ;
ragge
1.1
606         } else if (c != WSPACE)
ragge
1.37
607                 goto bad;
ragge
1.1
608
ragge
1.31
609         while (c == WSPACE)
ragge
1.1
610                 c = yylex();
611
612         /* parse replacement-list, substituting arguments */
613         savch('\0');
ragge
1.36
614         while (c != '\n') {
ragge
1.1
615                 switch (c) {
616                 case WSPACE:
617                         /* remove spaces if it surrounds a ## directive */
ragge
1.2
618                         ubuf = stringbuf;
ragge
1.39
619                         savstr((usch *)yytext);
ragge
1.1
620                         c = yylex();
621                         if (c == CONCAT) {
ragge
1.2
622                                 stringbuf = ubuf;
ragge
1.1
623                                 savch(CONC);
624                                 if ((c = yylex()) == WSPACE)
625                                         c = yylex();
626                         }
627                         continue;
628
629                 case CONCAT:
630                         /* No spaces before concat op */
631                         savch(CONC);
632                         if ((c = yylex()) == WSPACE)
633                                 c = yylex();
634                         continue;
635
636                 case MKSTR:
637                         if (narg < 0) {
638                                 /* no meaning in object-type macro */
639                                 savch('#');
640                                 break;
641                         }
642                         /* remove spaces between # and arg */
643                         savch(SNUFF);
644                         if ((c = yylex()) == WSPACE)
645                                 c = yylex(); /* whitespace, ignore */
646                         mkstr = 1;
ragge
1.37
647                         if (c == VA_ARGS)
648                                 continue;
ragge
1.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.39
656                                 if (strcmp(yytext, (char *)args[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
ragge
1.37
669                 case VA_ARGS:
670                         if (ellips == 0)
671                                 error("unwanted %s"yytext);
672                         savch(VARG);
673                         savch(WARN);
674                         if (mkstr)
675                                 savch(SNUFF), mkstr = 0;
676                         break;
677
ragge
1.1
678                 default:
ragge
1.39
679 id:                     savstr((usch *)yytext);
ragge
1.1
680                         break;
681                 }
682                 c = yylex();
683         }
ragge
1.20
684         /* remove trailing whitespace */
685         while (stringbuf > sbeg) {
686                 if (stringbuf[-1] == ' ' || stringbuf[-1] == '\t')
687                         stringbuf--;
688                 else
689                         break;
690         }
ragge
1.37
691         if (ellips) {
692                 savch(narg);
693                 savch(VARG);
694         } else
695                 savch(narg < 0 ? OBJCT : narg);
ragge
1.15
696         if (redef) {
697                 usch *o = np->value, *n = stringbuf-1;
698
699                 /* Redefinition to identical replacement-list is allowed */
700                 while (*o && *o == *n)
701                         o--, n--;
702                 if (*o || *o != *n)
703                         error("%s redefined"np->namep);
704                 stringbuf = sbeg;  /* forget this space */
705         } else
706                 np->value = stringbuf-1;
ragge
1.1
707
708 #ifdef CPP_DEBUG
709         if (dflag) {
710                 usch *w = np->value;
711
712                 printf("!define: ");
713                 if (*w == OBJCT)
714                         printf("[object]");
ragge
1.37
715                 else if (*w == VARG)
716                         printf("[VARG%d]", *--w);
ragge
1.1
717                 while (*--w) {
718                         switch (*w) {
719                         case WARNprintf("<%d>", *--w); break;
720                         case CONCprintf("<##>"); break;
721                         case SNUFFprintf("<\">"); break;
722                         defaultputchar(*w); break;
723                         }
724                 }
725                 putchar('\n');
726         }
727 #endif
ragge
1.36
728         slow = 0;
ragge
1.37
729         return;
730
731 bad:    error("bad define");
ragge
1.1
732 }
733
734 void
ragge
1.37
735 xerror(usch *s)
ragge
1.1
736 {
ragge
1.37
737         usch *t;
ragge
1.1
738
ragge
1.48
739         flbuf();
ragge
1.38
740         savch(0);
ragge
1.37
741         if (ifiles != NULL) {
742                 t = sheap("%s:%d: "ifiles->fnameifiles->lineno);
ragge
1.39
743                 write (2tstrlen((char *)t));
ragge
1.37
744         }
ragge
1.39
745         write (2sstrlen((char *)s));
ragge
1.37
746         write (2"\n"1);
747         exit(1);
ragge
1.1
748 }
749
750 /*
751  * store a character into the "define" buffer.
752  */
753 void
754 savch(c)
755 {
ragge
1.49
756         if (stringbuf-sbf < SBSIZE) {
757                 *stringbuf++ = c;
758         } else {
759                 stringbuf = sbf/* need space to write error message */
760                 error("Too much defining");
761         } 
ragge
1.1
762 }
763
764 /*
765  * substitute namep for sp->value.
766  */
767 int
ragge
1.36
768 subst(sprp)
ragge
1.1
769 struct symtab *sp;
770 struct recur *rp;
771 {
772         struct recur rp2;
773         register usch *vp, *cp;
774         int crv = 0;
775
ragge
1.37
776         DPRINT(("subst: %s\n"sp->namep));
ragge
1.17
777         /*
778          * First check for special macros.
779          */
780         if (sp == filloc) {
ragge
1.37
781                 (void)sheap("\"%s\""ifiles->fname);
ragge
1.17
782                 return 1;
783         } else if (sp == linloc) {
ragge
1.37
784                 (void)sheap("%d"ifiles->lineno);
ragge
1.1
785                 return 1;
786         }
ragge
1.17
787         vp = sp->value;
ragge
1.1
788
789         rp2.next = rp;
790         rp2.sp = sp;
791
792         if (*vp-- != OBJCT) {
793                 int gotwarn = 0;
794
795                 /* should we be here at all? */
796                 /* check if identifier is followed by parentheses */
797                 rv = 1;
798                 do {
ragge
1.36
799                         c = yylex();
ragge
1.1
800                         if (c == WARN) {
801                                 gotwarn++;
802                                 if (rp == NULL)
803                                         goto noid;
804                         }
ragge
1.36
805                 } while (c == WSPACE || c == '\n' || c == WARN);
ragge
1.1
806
ragge
1.39
807                 cp = (usch *)yytext;
ragge
1.1
808                 while (*cp)
809                         cp++;
ragge
1.36
810                 while (cp > (usch *)yytext)
ragge
1.1
811                         cunput(*--cp);
ragge
1.36
812                 DPRINT(("c %d\n"c));
ragge
1.1
813                 if (c == '(' ) {
814                         expdef(vp, &rp2gotwarn);
ragge
1.36
815                         return rv;
ragge
1.1
816                 } else {
817                         /* restore identifier */
818 noid:                   while (gotwarn--)
819                                 cunput(WARN);
820                         cunput(' ');
821                         cp = sp->namep;
822                         while (*cp)
823                                 cp++;
824                         while (cp > sp->namep)
825                                 cunput(*--cp);
826                         if ((c = yylex()) != IDENT)
827                                 error("internal sync error");
828                         return 0;
829                 }
830         } else {
831                 cunput(WARN);
832                 cp = vp;
833                 while (*cp) {
834                         if (*cp != CONC)
835                                 cunput(*cp);
836                         cp--;
837                 }
838                 expmac(&rp2);
839         }
840         return 1;
841 }
842
843 /*
844  * do macro-expansion until WARN character read.
ragge
1.44
845  * read from lex buffer and store result on heap.
ragge
1.1
846  * will recurse into lookup() for recursive expansion.
847  * when returning all expansions on the token list is done.
848  */
849 void
850 expmac(struct recur *rp)
851 {
852         struct symtab *nl;
ragge
1.43
853         int cnoexp = 0orgexp;
ragge
1.36
854         usch *och, *stksv;
855         extern int yyleng;
ragge
1.1
856
ragge
1.22
857 #ifdef CPP_DEBUG
858         if (dflag) {
859                 struct recur *rp2 = rp;
860                 printf("\nexpmac\n");
861                 while (rp2) {
862                         printf("do not expand %s\n"rp->sp->namep);
863                         rp2 = rp2->next;
864                 }
865         }
866 #endif
ragge
1.1
867         while ((c = yylex()) != WARN) {
868                 switch (c) {
869                 case NOEXPnoexp++; break;
870                 case EXPANDnoexp--; break;
871
872                 case IDENT:
ragge
1.43
873                         /*
874                          * Handle argument concatenation here.
875                          * If an identifier is found and directly 
ragge
1.44
876                          * after EXPAND or NOEXP then push the
ragge
1.43
877                          * identifier back on the input stream and
878                          * call yylex() again.
879                          * Be careful to keep the noexp balance.
880                          */
881                         och = stringbuf;
882                         savstr((usch *)yytext);
883                         DDPRINT(("id: str %s\n"och));
ragge
1.44
884
885                         orgexp = 0;
886                         while ((c = yylex()) == EXPAND || c == NOEXP)
ragge
1.43
887                                 if (c == EXPAND)
ragge
1.44
888                                         orgexp--;
ragge
1.43
889                                 else
ragge
1.44
890                                         orgexp++;
891
892                         DDPRINT(("id1: noexp %d orgexp %d\n"noexporgexp));
893                         if (c == IDENT) { /* XXX numbers? */
894                                 DDPRINT(("id2: str %s\n"yytext));
895                                 /* OK to always expand here? */
896                                 savstr((usch *)yytext);
897                                 switch (orgexp) {
898                                 case 0/* been EXP+NOEXP */
ragge
1.47
899                                         if (noexp == 0)
900                                                 break;
ragge
1.44
901                                         if (noexp != 1)
902                                                 error("case 0");
903                                         cunput(NOEXP);
904                                         noexp = 0;
905                                         break;
906                                 case -1/* been EXP */
907                                         if (noexp != 1)
908                                                 error("case -1");
909                                         noexp = 0;
910                                         break;
911                                 case 1:
912                                         if (noexp != 0)
913                                                 error("case 1");
914                                         cunput(NOEXP);
915                                         break;
916                                 default:
917                                         error("orgexp = %d"orgexp);
918                                 }
919                                 unpstr(och);
920                                 stringbuf = och;
921                                 continue/* New longer identifier */
ragge
1.43
922                         }
ragge
1.46
923                         unpstr((usch *)yytext);
ragge
1.44
924                         if (orgexp == -1)
925                                 cunput(EXPAND);
926                         else if (orgexp == 1)
927                                 cunput(NOEXP);
ragge
1.43
928                         unpstr(och);
929                         stringbuf = och;
ragge
1.44
930
931
ragge
1.43
932                         yylex(); /* XXX reget last identifier */
ragge
1.4
933
ragge
1.39
934                         if ((nl = lookup((usch *)yytextFIND)) == NULL)
ragge
1.1
935                                 goto def;