Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20070916082634

Diff

Diff from 1.55 to:

Annotations

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

Annotated File View

ragge
1.55
1 /*      $Id: cpp.c,v 1.55 2007/09/16 08:26:37 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.37
107 static usch outbuf[CPPBUF];
108 static 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.3
486         if (yylex() != WSPACE)
487                 goto bad;
ragge
1.10
488 again:  if ((c = yylex()) != STRING && c != '<' && c != IDENT)
ragge
1.3
489                 goto bad;
490
491         if (c == IDENT) {
ragge
1.39
492                 if ((nl = lookup((usch *)yytextFIND)) == NULL)
ragge
1.3
493                         goto bad;
ragge
1.35
494                 if (subst(nlNULL) == 0)
ragge
1.3
495                         goto bad;
496                 savch('\0');
ragge
1.10
497                 unpstr(osp);
498                 goto again;
499         } else if (c == '<') {
500                 fn = stringbuf;
ragge
1.36
501                 while ((c = yylex()) != '>' && c != '\n') {
502                         if (c == '\n')
ragge
1.10
503                                 goto bad;
ragge
1.39
504                         savstr((usch *)yytext);
ragge
1.10
505                 }
506                 savch('\0');
ragge
1.37
507                 while ((c = yylex()) == WSPACE)
508                         ;
509                 if (c != '\n')
510                         goto bad;
ragge
1.20
511                 it = SYSINC;
ragge
1.3
512         } else {
ragge
1.20
513                 usch *nm = stringbuf;
514
ragge
1.36
515                 yytext[strlen(yytext)-1] = 0;
ragge
1.39
516                 fn = (usch *)&yytext[1];
ragge
1.20
517                 /* first try to open file relative to previous file */
ragge
1.53
518                 /* but only if it is not an absolute path */
519                 if (*fn != '/') {
520                         savstr(ifiles->orgfn);
521                         if ((stringbuf =
522                             (usch *)strrchr((char *)nm'/')) == NULL)
523                                 stringbuf = nm;
524                         else
525                                 stringbuf++;
526                 }
ragge
1.20
527                 savstr(fn); savch(0);
ragge
1.37
528                 while ((c = yylex()) == WSPACE)
529                         ;
530                 if (c != '\n')
531                         goto bad;
532                 slow = 0;
ragge
1.20
533                 if (pushfile(nm) == 0)
ragge
1.37
534                         return;
ragge
1.20
535                 stringbuf = nm;
ragge
1.10
536         }
537
538         /* create search path and try to open file */
ragge
1.37
539         slow = 0;
ragge
1.20
540         for (i = 0i < 2i++) {
ragge
1.10
541                 for (w = incdir[i]; ww = w->next) {
542                         usch *nm = stringbuf;
543
544                         savstr(w->dir); savch('/');
545                         savstr(fn); savch(0);
546                         if (pushfile(nm) == 0)
ragge
1.37
547                                 return;
ragge
1.10
548                         stringbuf = nm;
549                 }
ragge
1.3
550         }
ragge
1.10
551         error("cannot find '%s'"fn);
ragge
1.37
552         /* error() do not return */
ragge
1.3
553
554 bad:    error("bad include");
ragge
1.37
555         /* error() do not return */
556 }
557
558 static int
559 definp(void)
560 {
561         int c;
562
563         do
564                 c = yylex();
565         while (c == WSPACE);
566         return c;
ragge
1.3
567 }
568
569 void
ragge
1.1
570 define()
571 {
572         struct symtab *np;
ragge
1.15
573         usch *args[MAXARG], *ubuf, *sbeg;
574         int ciredef;
ragge
1.1
575         int mkstr = 0narg = -1;
ragge
1.37
576         int ellips = 0;
ragge
1.1
577
ragge
1.37
578         if (flslvl)
579                 return;
ragge
1.36
580         slow = 1;
581         if (yylex() != WSPACE || yylex() != IDENT)
ragge
1.37
582                 goto bad;
ragge
1.36
583
ragge
1.51
584         if (isdigit((int)yytext[0]))
585                 goto bad;
586
ragge
1.39
587         np = lookup((usch *)yytextENTER);
ragge
1.15
588         redef = np->value != NULL;
ragge
1.1
589
ragge
1.15
590         sbeg = stringbuf;
ragge
1.1
591         if ((c = yylex()) == '(') {
592                 narg = 0;
593                 /* function-like macros, deal with identifiers */
ragge
1.37
594                 for (;;) {
595                         c = definp();
ragge
1.1
596                         if (c == ')')
597                                 break;
ragge
1.37
598                         if (c == ELLIPS) {
599                                 ellips = 1;
600                                 if (definp() != ')')
601                                         goto bad;
602                                 break;
603                         }
604                         if (c == IDENT) {
605                                 args[narg] = alloca(strlen(yytext)+1);
ragge
1.39
606                                 strcpy((char *)args[narg], yytext);
ragge
1.37
607                                 narg++;
608                                 if ((c = definp()) == ',')
609                                         continue;
610                                 if (c == ')')
611                                         break;
612                                 goto bad;
613                         }
614                         goto bad;
ragge
1.1
615                 }
ragge
1.36
616                 c = yylex();
617         } else if (c == '\n') {
ragge
1.8
618                 /* #define foo */
ragge
1.36
619                 ;
ragge
1.1
620         } else if (c != WSPACE)
ragge
1.37
621                 goto bad;
ragge
1.1
622
ragge
1.31
623         while (c == WSPACE)
ragge
1.1
624                 c = yylex();
625
626         /* parse replacement-list, substituting arguments */
627         savch('\0');
ragge
1.36
628         while (c != '\n') {
ragge
1.1
629                 switch (c) {
630                 case WSPACE:
631                         /* remove spaces if it surrounds a ## directive */
ragge
1.2
632                         ubuf = stringbuf;
ragge
1.39
633                         savstr((usch *)yytext);
ragge
1.1
634                         c = yylex();
635                         if (c == CONCAT) {
ragge
1.2
636                                 stringbuf = ubuf;
ragge
1.1
637                                 savch(CONC);
638                                 if ((c = yylex()) == WSPACE)
639                                         c = yylex();
640                         }
641                         continue;
642
643                 case CONCAT:
644                         /* No spaces before concat op */
645                         savch(CONC);
646                         if ((c = yylex()) == WSPACE)
647                                 c = yylex();
648                         continue;
649
650                 case MKSTR:
651                         if (narg < 0) {
652                                 /* no meaning in object-type macro */
653                                 savch('#');
654                                 break;
655                         }
656                         /* remove spaces between # and arg */
657                         savch(SNUFF);
658                         if ((c = yylex()) == WSPACE)
659                                 c = yylex(); /* whitespace, ignore */
660                         mkstr = 1;
ragge
1.37
661                         if (c == VA_ARGS)
662                                 continue;
ragge
1.1
663
664                         /* FALLTHROUGH */
665                 case IDENT:
666                         if (narg < 0)
667                                 goto id/* just add it if object */
668                         /* check if its an argument */
669                         for (i = 0i < nargi++)
ragge
1.39
670                                 if (strcmp(yytext, (char *)args[i]) == 0)
ragge
1.1
671                                         break;
672                         if (i == narg) {
673                                 if (mkstr)
674                                         error("not argument");
675                                 goto id;
676                         }
677                         savch(i);
678                         savch(WARN);
679                         if (mkstr)
680                                 savch(SNUFF), mkstr = 0;
681                         break;
682
ragge
1.37
683                 case VA_ARGS:
684                         if (ellips == 0)
685                                 error("unwanted %s"yytext);
686                         savch(VARG);
687                         savch(WARN);
688                         if (mkstr)
689                                 savch(SNUFF), mkstr = 0;
690                         break;
691
ragge
1.1
692                 default:
ragge
1.39
693 id:                     savstr((usch *)yytext);
ragge
1.1
694                         break;
695                 }
696                 c = yylex();
697         }
ragge
1.20
698         /* remove trailing whitespace */
699         while (stringbuf > sbeg) {
700                 if (stringbuf[-1] == ' ' || stringbuf[-1] == '\t')
701                         stringbuf--;
702                 else
703                         break;
704         }
ragge
1.37
705         if (ellips) {
706                 savch(narg);
707                 savch(VARG);
708         } else
709                 savch(narg < 0 ? OBJCT : narg);
ragge
1.15
710         if (redef) {
711                 usch *o = np->value, *n = stringbuf-1;
712
713                 /* Redefinition to identical replacement-list is allowed */
714                 while (*o && *o == *n)
715                         o--, n--;
716                 if (*o || *o != *n)
ragge
1.53
717                         error("%s redefined\nprevious define: %s:%d",
718                             np->namepnp->filenp->line);
ragge
1.15
719                 stringbuf = sbeg;  /* forget this space */
720         } else
721                 np->value = stringbuf-1;
ragge
1.1
722
723 #ifdef CPP_DEBUG
724         if (dflag) {
725                 usch *w = np->value;
726
727                 printf("!define: ");
728                 if (*w == OBJCT)
729                         printf("[object]");
ragge
1.37
730                 else if (*w == VARG)
731                         printf("[VARG%d]", *--w);
ragge
1.1
732                 while (*--w) {
733                         switch (*w) {
734                         case WARNprintf("<%d>", *--w); break;
735                         case CONCprintf("<##>"); break;
736                         case SNUFFprintf("<\">"); break;
737                         defaultputchar(*w); break;
738                         }
739                 }
740                 putchar('\n');
741         }
742 #endif
ragge
1.36
743         slow = 0;
ragge
1.37
744         return;
745
746 bad:    error("bad define");
ragge
1.1
747 }
748
749 void
ragge
1.37
750 xerror(usch *s)
ragge
1.1
751 {
ragge
1.37
752         usch *t;
ragge
1.1
753
ragge
1.48
754         flbuf();
ragge
1.38
755         savch(0);
ragge
1.37
756         if (ifiles != NULL) {
757                 t = sheap("%s:%d: "ifiles->fnameifiles->lineno);
ragge
1.39
758                 write (2tstrlen((char *)t));
ragge
1.37
759         }
ragge
1.39
760         write (2sstrlen((char *)s));
ragge
1.37
761         write (2"\n"1);
762         exit(1);
ragge
1.1
763 }
764
765 /*
766  * store a character into the "define" buffer.
767  */
768 void
769 savch(c)
770 {
ragge
1.49
771         if (stringbuf-sbf < SBSIZE) {
772                 *stringbuf++ = c;
773         } else {
774                 stringbuf = sbf/* need space to write error message */
775                 error("Too much defining");
776         } 
ragge
1.1
777 }
778
779 /*
780  * substitute namep for sp->value.
781  */
782 int
ragge
1.36
783 subst(sprp)
ragge
1.1
784 struct symtab *sp;
785 struct recur *rp;
786 {
787         struct recur rp2;
788         register usch *vp, *cp;
ragge
1.52
789         int crv = 0ws;
ragge
1.1
790
ragge
1.37
791         DPRINT(("subst: %s\n"sp->namep));
ragge
1.17
792         /*
793          * First check for special macros.
794          */
795         if (sp == filloc) {
ragge
1.37
796                 (void)sheap("\"%s\""ifiles->fname);
ragge
1.17
797                 return 1;
798         } else if (sp == linloc) {
ragge
1.37
799                 (void)sheap("%d"ifiles->lineno);
ragge
1.1
800                 return 1;
801         }
ragge
1.17
802         vp = sp->value;
ragge
1.1
803
804         rp2.next = rp;
805         rp2.sp = sp;
806
807         if (*vp-- != OBJCT) {
808                 int gotwarn = 0;
809
810                 /* should we be here at all? */
811                 /* check if identifier is followed by parentheses */
812                 rv = 1;
ragge
1.52
813                 ws = 0;
ragge
1.1
814                 do {
ragge
1.36
815                         c = yylex();
ragge
1.1
816                         if (c == WARN) {
817                                 gotwarn++;
818                                 if (rp == NULL)
819                                         goto noid;
ragge
1.52
820                         } else if (c == WSPACE)
821                                 ws = 1;
ragge
1.36
822                 } while (c == WSPACE || c == '\n' || c == WARN);
ragge
1.1
823
ragge
1.39
824                 cp = (usch *)yytext;
ragge
1.1
825                 while (*cp)
826                         cp++;
ragge
1.36
827                 while (cp > (usch *)yytext)
ragge
1.1
828                         cunput(*--cp);
ragge
1.36
829                 DPRINT(("c %d\n"c));
ragge
1.1
830                 if (c == '(' ) {
831                         expdef(vp, &rp2gotwarn);
ragge
1.36
832                         return rv;
ragge
1.1
833                 } else {
834                         /* restore identifier */
835 noid:                   while (gotwarn--)
836                                 cunput(WARN);
ragge
1.52
837                         if (ws)
838                                 cunput(' ');
ragge
1.1
839                         cp = sp->namep;
840                         while (*cp)
841                                 cp++;
842                         while (cp > sp->namep)
843                                 cunput(*--cp);
844                         if ((c = yylex()) != IDENT)
845                                 error("internal sync error");
846                         return 0;
847                 }
848         } else {
849                 cunput(WARN);
850                 cp = vp;
851                 while (*cp) {
852                         if (*cp != CONC)
853                                 cunput(*cp);
854                         cp--;
855                 }
856                 expmac(&rp2);
857         }
858         return 1;
859 }
860
861 /*
862  * do macro-expansion until WARN character read.
ragge
1.44
863  * read from lex buffer and store result on heap.
ragge
1.1
864  * will recurse into lookup() for recursive expansion.
865  * when returning all expansions on the token list is done.
866  */
867 void
868 expmac(struct recur *rp)
869 {
870         struct symtab *nl;
ragge
1.43
871         int cnoexp = 0orgexp;
ragge
1.36
872         usch *och, *stksv;
873         extern int yyleng;
ragge
1.1
874
ragge
1.22
875 #ifdef CPP_DEBUG
876         if (dflag) {
877                 struct recur *rp2 = rp;
878                 printf("\nexpmac\n");
879                 while (rp2) {
880                         printf("do not expand %s\n"rp->sp->namep);
881                         rp2 = rp2->next;
882                 }
883         }
884 #endif
ragge
1.1
885         while ((c = yylex()) != WARN) {
886                 switch (c) {
887                 case NOEXPnoexp++; break;
888                 case EXPANDnoexp--; break;
889
890                 case IDENT:
ragge
1.43
891                         /*
892                          * Handle argument concatenation here.
893                          * If an identifier is found and directly 
ragge
1.44
894                          * after EXPAND or NOEXP then push the
ragge
1.43
895                          * identifier back on the input stream and
896                          * call yylex() again.
897                          * Be careful to keep the noexp balance.
898                          */
899                         och = stringbuf;
900                         savstr((usch *)yytext);
901                         DDPRINT(("id: str %s\n"och));
ragge
1.44
902
903                         orgexp = 0;
904                         while ((c = yylex()) == EXPAND || c == NOEXP)
ragge
1.43
905                                 if (c == EXPAND)
ragge
1.44
906                                         orgexp--;
ragge
1.43
907                                 else
ragge
1.44
908                                         orgexp++;
909
910                         DDPRINT(("id1: noexp %d orgexp %d\n"noexporgexp));
911                         if (c == IDENT) { /* XXX numbers? */
912                                 DDPRINT(("id2: str %s\n"yytext));
913                                 /* OK to always expand here? */
914                                 savstr((usch *)yytext);
915                                 switch (orgexp) {
916                                 case 0/* been EXP+NOEXP */
ragge
1.47
917                                         if (noexp == 0)
918                                                 break;
ragge
1.44
919                                         if (noexp != 1)
920                                                 error("case 0");
921                                         cunput(NOEXP);
922                                         noexp = 0;
923                                         break;
924                                 case -1/* been EXP */
925                                         if (noexp != 1)
926                                                 error("case -1");
927                                         noexp = 0;
928                                         break;
929                                 case 1:
930                                         if (noexp != 0)
931                                                 error("case 1");
932                                         cunput(NOEXP);
933                                         break;
934                                 default:
935                                         error("orgexp = %d"orgexp);