Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20070917181612

Diff

Diff from 1.60 to:

Annotations

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

Annotated File View

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