Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20070916182618

Diff

Diff from 1.57 to:

Annotations

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

Annotated File View

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