Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20121107095159

Diff

Diff from 1.185 to:

Annotations

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

Annotated File View

plunky
1.185
1 /*      $Id: cpp.c,v 1.185 2012/11/07 09:51:59 plunky Exp $     */
ragge
1.1
2
3 /*
ragge
1.109
4  * Copyright (c) 2004,2010 Anders Magnusson (ragge@ludd.luth.se).
ragge
1.1
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /*
29  * The C preprocessor.
30  * This code originates from the V6 preprocessor with some additions
31  * from V7 cpp, and at last ansi/c99 support.
32  */
pj
1.23
33
ragge
1.74
34 #include "config.h"
pj
1.23
35
ragge
1.108
36 #include <sys/stat.h>
ragge
1.1
37
38 #include <fcntl.h>
gmcgarry
1.81
39 #ifdef HAVE_UNISTD_H
ragge
1.1
40 #include <unistd.h>
gmcgarry
1.81
41 #endif
ragge
1.1
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <string.h>
ragge
1.9
46 #include <time.h>
ragge
1.1
47
ragge
1.74
48 #include "compat.h"
ragge
1.1
49 #include "cpp.h"
plunky
1.156
50 #include "cpy.h"
ragge
1.1
51
gmcgarry
1.130
52 #ifndef S_ISDIR
53 #define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)
54 #endif
55
ragge
1.126
56 #define SBSIZE  1000000
ragge
1.1
57
plunky
1.168
58 static const char versstr[] = "PCC preprocessor version " VERSSTR "\n";
59
ragge
1.1
60 static usch     sbf[SBSIZE];
61 /* C command */
62
63 int tflag;      /* traditional cpp syntax */
plunky
1.134
64 #ifdef PCC_DEBUG
ragge
1.1
65 int dflag;      /* debug printouts */
plunky
1.135
66 static void imp(const char *);
67 static void prline(const usch *s);
68 static void prrep(const usch *s);
ragge
1.36
69 #define DPRINT(x) if (dflag) printf x
70 #define DDPRINT(x) if (dflag > 1) printf x
plunky
1.135
71 #define IMP(x) if (dflag > 1) imp(x)
ragge
1.36
72 #else
73 #define DPRINT(x)
74 #define DDPRINT(x)
plunky
1.135
75 #define IMP(x)
ragge
1.1
76 #endif
ragge
1.36
77
ragge
1.51
78 int ofd;
ragge
1.56
79 usch outbuf[CPPBUF];
ragge
1.97
80 int obufpistty;
ragge
1.151
81 int CflagMflagdMflagPflagMPflag;
82 usch *Mfile, *MPfile, *Mxfile;
ragge
1.53
83 struct initar *initar;
ragge
1.117
84 int readmaclastoch;
plunky
1.173
85 int defining;
ragge
1.1
86
ragge
1.10
87 /* include dirs */
88 struct incs {
89         struct incs *next;
ragge
1.39
90         usch *dir;
ragge
1.108
91         dev_t dev;
92         ino_t ino;
ragge
1.20
93 } *incdir[2];
ragge
1.10
94
ragge
1.1
95 static struct symtab *filloc;
96 static struct symtab *linloc;
ragge
1.65
97 static struct symtab *pragloc;
ragge
1.10
98 int     trulvl;
99 int     flslvl;
100 int     elflvl;
101 int     elslvl;
102 usch *stringbuf = sbf;
ragge
1.1
103
104 /*
105  * Macro replacement list syntax:
106  * - For object-type macros, replacement strings are stored as-is.
107  * - For function-type macros, macro args are substituted for the
108  *   character WARN followed by the argument number.
ragge
1.12
109  * - The value element points to the end of the string, to simplify
110  *   pushback onto the input queue.
ragge
1.1
111  * 
ragge
1.12
112  * The first character (from the end) in the replacement list is
113  * the number of arguments:
ragge
1.37
114  *   VARG  - ends with ellipsis, next char is argcount without ellips.
ragge
1.1
115  *   OBJCT - object-type macro
116  *   0     - empty parenthesis, foo()
117  *   1->   - number of args.
ragge
1.109
118  *
119  * WARN is used:
120  *      - in stored replacement lists to tell that an argument comes
121  *      - When expanding replacement lists to tell that the list ended.
ragge
1.116
122  *
123  * To ensure that an already expanded identifier won't get expanded
124  * again a EBLOCK char + its number is stored directly before any 
125  * expanded identifier.
ragge
1.1
126  */
127
128 /* args for lookup() */
129 #define FIND    0
130 #define ENTER   1
131
plunky
1.166
132 /*
133  * No-replacement array.  If a macro is found and exists in this array
134  * then no replacement shall occur.  This is a stack.
135  */
136 struct symtab *norep[RECMAX];   /* Symbol table index table */
137 int norepptr = 1;                       /* Top of index table */
138 unsigned short bptr[RECMAX];    /* currently active noexpand macro stack */
139 int bidx;                       /* Top of bptr stack */
140
ragge
1.112
141 static int readargs(struct symtab *spconst usch **args);
ragge
1.116
142 static void exparg(int);
ragge
1.112
143 static void subarg(struct symtab *spconst usch **argsint);
plunky
1.143
144 static void flbuf(void);
145 static void usage(void);
146 static usch *xstrdup(const usch *str);
ragge
1.108
147 static void addidir(char *idirstruct incs **ww);
plunky
1.178
148 static void vsheap(const char *, va_list);
ragge
1.1
149
150 int
151 main(int argcchar **argv)
152 {
ragge
1.53
153         struct initar *it;
ragge
1.26
154         struct symtab *nl;
155         register int ch;
ragge
1.103
156         const usch *fn1, *fn2;
ragge
1.15
157
ragge
1.94
158 #ifdef TIMING
159         struct timeval t1t2;
160
161         (void)gettimeofday(&t1NULL);
162 #endif
163
ragge
1.151
164         while ((ch = getopt(argcargv"CD:d:I:i:MPS:tU:Vvx:")) != -1) {
ragge
1.1
165                 switch (ch) {
ragge
1.22
166                 case 'C'/* Do not discard comments */
167                         Cflag++;
168                         break;
169
plunky
1.142
170                 case 'D'/* define something */
ragge
1.53
171                 case 'i'/* include */
172                 case 'U'/* undef */
ragge
1.58
173                         /* XXX should not need malloc() here */
174                         if ((it = malloc(sizeof(struct initar))) == NULL)
175                                 error("couldn't apply -%c %s"choptarg);
ragge
1.53
176                         it->type = ch;
177                         it->str = optarg;
178                         it->next = initar;
179                         initar = it;
ragge
1.7
180                         break;
181
plunky
1.142
182                 case 'd':
plunky
1.144
183                         while (*optarg) {
184                                 switch(*optarg) {
185                                 case 'M'/* display macro definitions */
186                                         dMflag = 1;
187                                         Mflag = 1;
188                                         break;
189
190                                 default/* ignore others */
191                                         break;
192                                 }
193                                 optarg++;
plunky
1.142
194                         }
195                         break;
196
197                 case 'I':
198                 case 'S':
199                         addidir(optarg, &incdir[ch == 'I' ? INCINC : SYSINC]);
200                         break;
201
ragge
1.51
202                 case 'M'/* Generate dependencies for make */
203                         Mflag++;
204                         break;
205
gmcgarry
1.85
206                 case 'P'/* Inhibit generation of line numbers */
207                         Pflag++;
208                         break;
209
plunky
1.142
210                 case 't':
211                         tflag = 1;
ragge
1.7
212                         break;
213
plunky
1.134
214 #ifdef PCC_DEBUG
ragge
1.64
215                 case 'V':
ragge
1.18
216                         dflag++;
ragge
1.1
217                         break;
218 #endif
ragge
1.64
219                 case 'v':
plunky
1.174
220                         xwrite(2versstrsizeof(versstr) - 1);
ragge
1.64
221                         break;
ragge
1.1
222
ragge
1.151
223                 case 'x':
224                         if (strcmp(optarg"MP") == 0) {
225                                 MPflag++;
226                         } else if (strncmp(optarg"MT,"3) == 0 ||
227                             strncmp(optarg"MQ,"3) == 0) {
228                                 usch *cp, *fn;
229                                 fn = stringbuf;
230                                 for (cp = (usch *)&optarg[3]; *cpcp++) {
231                                         if (*cp == '$' && optarg[1] == 'Q')
232                                                 savch('$');
233                                         savch(*cp);
234                                 }
plunky
1.152
235                                 savstr((const usch *)""); 
ragge
1.151
236                                 if (Mxfile) { savch(' '); savstr(Mxfile); }
237                                 savch(0);
238                                 Mxfile = fn;
239                         } else
240                                 usage();
241                         break;
242
ragge
1.37
243                 case '?':
plunky
1.142
244                 default:
ragge
1.37
245                         usage();
ragge
1.1
246                 }
plunky
1.142
247         }
248
ragge
1.1
249         argc -= optind;
250         argv += optind;
251
ragge
1.103
252         filloc = lookup((const usch *)"__FILE__"ENTER);
253         linloc = lookup((const usch *)"__LINE__"ENTER);
ragge
1.114
254         filloc->value = linloc->value = stringbuf;
255         savch(OBJCT);
256
257         /* create a complete macro for pragma */
ragge
1.103
258         pragloc = lookup((const usch *)"_Pragma"ENTER);
ragge
1.114
259         savch(0);
ragge
1.115
260         savstr((const usch *)"_Pragma(");
ragge
1.114
261         savch(0);
262         savch(WARN);
263         savch(')');
264         pragloc->value = stringbuf;
265         savch(1);
ragge
1.15
266
ragge
1.12
267         if (tflag == 0) {
268                 time_t t = time(NULL);
ragge
1.39
269                 usch *n = (usch *)ctime(&t);
ragge
1.12
270
271                 /*
272                  * Manually move in the predefined macros.
273                  */
ragge
1.103
274                 nl = lookup((const usch *)"__TIME__"ENTER);
ragge
1.13
275                 savch(0); savch('"');  n[19] = 0savstr(&n[11]); savch('"');
ragge
1.12
276                 savch(OBJCT);
277                 nl->value = stringbuf-1;
278
ragge
1.103
279                 nl = lookup((const usch *)"__DATE__"ENTER);
ragge
1.13
280                 savch(0); savch('"'); n[24] = n[11] = 0savstr(&n[4]);
281                 savstr(&n[20]); savch('"'); savch(OBJCT);
ragge
1.12
282                 nl->value = stringbuf-1;
283
ragge
1.103
284                 nl = lookup((const usch *)"__STDC__"ENTER);
ragge
1.13
285                 savch(0); savch('1'); savch(OBJCT);
ragge
1.12
286                 nl->value = stringbuf-1;
ragge
1.73
287
ragge
1.103
288                 nl = lookup((const usch *)"__STDC_VERSION__"ENTER);
289                 savch(0); savstr((const usch *)"199901L"); savch(OBJCT);
ragge
1.73
290                 nl->value = stringbuf-1;
ragge
1.12
291         }
ragge
1.1
292
ragge
1.60
293         if (Mflag && !dMflag) {
ragge
1.51
294                 usch *c;
295
296                 if (argc < 1)
297                         error("-M and no infile");
298                 if ((c = (usch *)strrchr(argv[0], '/')) == NULL)
299                         c = (usch *)argv[0];
300                 else
301                         c++;
302                 Mfile = stringbuf;
303                 savstr(c); savch(0);
ragge
1.151
304                 if (MPflag) {
305                         MPfile = stringbuf;
306                         savstr(c); savch(0);
307                 }
308                 if (Mxfile)
309                         Mfile = Mxfile;
ragge
1.51
310                 if ((c = (usch *)strrchr((char *)Mfile'.')) == NULL)
311                         error("-M and no extension: ");
312                 c[1] = 'o';
313                 c[2] = 0;
314         }
315
ragge
1.26
316         if (argc == 2) {
plunky
1.180
317                 if ((ofd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC0666)) < 0)
ragge
1.37
318                         error("Can't creat %s"argv[1]);
ragge
1.26
319         } else
ragge
1.37
320                 ofd = 1/* stdout */
321         istty = isatty(ofd);
ragge
1.26
322
ragge
1.102
323         if (argc && strcmp(argv[0], "-")) {
324                 fn1 = fn2 = (usch *)argv[0];
325         } else {
326                 fn1 = NULL;
ragge
1.103
327                 fn2 = (const usch *)"";
ragge
1.102
328         }
329         if (pushfile(fn1fn20NULL))
ragge
1.26
330                 error("cannot open %s"argv[0]);
331
ragge
1.37
332         flbuf();
333         close(ofd);
ragge
1.94
334 #ifdef TIMING
335         (void)gettimeofday(&t2NULL);
336         t2.tv_sec -= t1.tv_sec;
337         t2.tv_usec -= t1.tv_usec;
338         if (t2.tv_usec < 0) {
339                 t2.tv_usec += 1000000;
340                 t2.tv_sec -= 1;
341         }
342         fprintf(stderr"cpp total time: %ld s %ld us\n",
plunky
1.159
343              (long)t2.tv_sec, (long)t2.tv_usec);
ragge
1.94
344 #endif
ragge
1.37
345         return 0;
ragge
1.26
346 }
347
ragge
1.108
348 static void
349 addidir(char *idirstruct incs **ww)
350 {
351         struct incs *w;
352         struct stat st;
353
plunky
1.163
354         if (stat(idir, &st) == -1 || !S_ISDIR(st.st_mode))
ragge
1.108
355                 return/* ignore */
356         if (*ww != NULL) {
357                 for (w = *www->nextw = w->next) {
plunky
1.145
358 #ifdef os_win32
gmcgarry
1.131
359                         if (strcmp(w->diridir) == 0)
360                                 return;
361 #else
ragge
1.108
362                         if (w->dev == st.st_dev && w->ino == st.st_ino)
363                                 return;
gmcgarry
1.131
364 #endif
ragge
1.108
365                 }
plunky
1.145
366 #ifdef os_win32
gmcgarry
1.131
367                 if (strcmp(w->diridir) == 0)
368                         return;
369 #else
ragge
1.108
370                 if (w->dev == st.st_dev && w->ino == st.st_ino)
371                         return;
gmcgarry
1.131
372 #endif
ragge
1.108
373                 ww = &w->next;
374         }
375         if ((w = calloc(sizeof(struct incs), 1)) == NULL)
376                 error("couldn't add path %s"idir);
377         w->dir = (usch *)idir;
378         w->dev = st.st_dev;
379         w->ino = st.st_ino;
380         *ww = w;
381 }
382
ragge
1.1
383 void
plunky
1.143
384 line(void)
ragge
1.9
385 {
ragge
1.37
386         static usch *lbuf;
387         static int llen;
ragge
1.97
388         usch *p;
ragge
1.9
389         int c;
390
ragge
1.97
391         if ((c = yylex()) != NUMBER)
ragge
1.9
392                 goto bad;
gmcgarry
1.100
393         ifiles->lineno = (int)(yylval.node.nd_val - 1);
plunky
1.185
394         ifiles->escln = 0;
ragge
1.9
395
ragge
1.97
396         if ((c = yylex()) == '\n')
plunky
1.181
397                 goto okret;
ragge
1.97
398
399         if (c != STRING)
ragge
1.9
400                 goto bad;
ragge
1.97
401
plunky
1.160
402         p = yytext;
plunky
1.158
403         if (*p++ == 'L')
ragge
1.97
404                 p++;
405         c = strlen((char *)p);
plunky
1.158
406         p[c - 1] = '\0';
ragge
1.37
407         if (llen < c) {
plunky
1.136
408                 /* XXX may lose heap space */
ragge
1.37
409                 lbuf = stringbuf;
410                 stringbuf += c;
411                 llen = c;
plunky
1.158
412                 if (stringbuf >= &sbf[SBSIZE]) {
413                         stringbuf = sbf/* need space to write error message */
plunky
1.182
414                         error("#line filename exceeds buffer size");
plunky
1.158
415                 }
ragge
1.37
416         }
plunky
1.158
417         memcpy(lbufpc);
ragge
1.37
418         ifiles->fname = lbuf;
plunky
1.181
419         if (yylex() != '\n')
420                 goto bad;
421
422 okret:  prtline();
423         return;
ragge
1.9
424
plunky
1.182
425 bad:    error("bad #line");
ragge
1.9
426 }
427
ragge
1.10
428 /*
ragge
1.102
429  * Search for and include next file.
430  * Return 1 on success.
431  */
432 static int
ragge
1.103
433 fsrch(const usch *fnint idxstruct incs *w)
ragge
1.102
434 {
435         int i;
436
437         for (i = idxi < 2i++) {
438                 if (i > idx)
439                         w = incdir[i];
440                 for (; ww = w->next) {
441                         usch *nm = stringbuf;
442
443                         savstr(w->dir); savch('/');
444                         savstr(fn); savch(0);
445                         if (pushfile(nmfniw->next) == 0)
446                                 return 1;
447                         stringbuf = nm;
448                 }
449         }
450         return 0;
451 }
452
ragge
1.132
453 static void
454 prem(void)
455 {
456         error("premature EOF");
457 }
458
ragge
1.102
459 /*
ragge
1.10
460  * Include a file. Include order:
ragge
1.20
461  * - For <...> files, first search -I directories, then system directories.
462  * - For "..." files, first search "current" dir, then as <...> files.
ragge
1.10
463  */
ragge
1.9
464 void
plunky
1.143
465 include(void)
ragge
1.3
466 {
ragge
1.101
467         struct symtab *nl;
ragge
1.10
468         usch *osp;
ragge
1.62
469         usch *fn, *safefn;
gmcgarry
1.133
470         int c;
ragge
1.3
471
ragge
1.37
472         if (flslvl)
473                 return;
ragge
1.10
474         osp = stringbuf;
ragge
1.97
475
ragge
1.101
476         while ((c = sloscan()) == WSPACE)
477                 ;
478         if (c == IDENT) {
479                 /* sloscan() will not expand idents */
plunky
1.160
480                 if ((nl = lookup(yytextFIND)) == NULL)
ragge
1.101
481                         goto bad;
ragge
1.112
482                 if (kfind(nl))
483                         unpstr(stringbuf);
484                 else
485                         unpstr(nl->namep);
ragge
1.101
486                 stringbuf = osp;
487                 c = yylex();
488         }
ragge
1.3
489
ragge
1.97
490         if (c == '<') {
ragge
1.10
491                 fn = stringbuf;
plunky
1.183
492                 while ((c = sloscan()) != '>') {
493                         if (c == 0)
494                                 prem();
495                         if (c == '\n')
ragge
1.10
496                                 goto bad;
plunky
1.160
497                         savstr(yytext);
ragge
1.10
498                 }
499                 savch('\0');
ragge
1.97
500                 while ((c = sloscan()) == WSPACE)
ragge
1.37
501                         ;
ragge
1.132
502                 if (c == 0)
503                         prem();
ragge
1.37
504                 if (c != '\n')
505                         goto bad;
ragge
1.62
506                 safefn = fn;
plunky
1.183
507         } else if (c == STRING) {
ragge
1.20
508                 usch *nm = stringbuf;
509
plunky
1.183
510                 fn = yytext;
511                 if (*fn++ == 'L')
512                         fn++;
513                 fn[strlen((char *)fn) - 1] = 0;
ragge
1.20
514                 /* first try to open file relative to previous file */
ragge
1.53
515                 /* but only if it is not an absolute path */
516                 if (*fn != '/') {
517                         savstr(ifiles->orgfn);
518                         if ((stringbuf =
519                             (usch *)strrchr((char *)nm'/')) == NULL)
520                                 stringbuf = nm;
521                         else
522                                 stringbuf++;
523                 }
ragge
1.62
524                 safefn = stringbuf;
ragge
1.20
525                 savstr(fn); savch(0);
ragge
1.97
526                 c = yylex();
ragge
1.132
527                 if (c == 0)
528                         prem();
ragge
1.37
529                 if (c != '\n')
530                         goto bad;
ragge
1.102
531                 if (pushfile(nmsafefn0NULL) == 0)
ragge
1.97
532                         goto okret;
plunky
1.136
533                 /* XXX may lose stringbuf space */
plunky
1.183
534         } else
535                 goto bad;
ragge
1.10
536
ragge
1.102
537         if (fsrch(safefn0incdir[0]))
538                 goto okret;
ragge
1.10
539
ragge
1.62
540         error("cannot find '%s'"safefn);
ragge
1.37
541         /* error() do not return */
ragge
1.3
542
plunky
1.182
543 bad:    error("bad #include");
ragge
1.37
544         /* error() do not return */
ragge
1.97
545 okret:
546         prtline();
ragge
1.37
547 }
548
ragge
1.102
549 void
plunky
1.143
550 include_next(void)
ragge
1.102
551 {
ragge
1.105
552         struct symtab *nl;
553         usch *osp;
554         usch *fn;
555         int c;
556
ragge
1.107
557         if (flslvl)
558                 return;
ragge
1.105
559         osp = stringbuf;
560         while ((c = sloscan()) == WSPACE)
561                 ;
562         if (c == IDENT) {
563                 /* sloscan() will not expand idents */
plunky
1.160
564                 if ((nl = lookup(yytextFIND)) == NULL)
ragge
1.105
565                         goto bad;
ragge
1.112
566                 if (kfind(nl))
567                         unpstr(stringbuf);
568                 else
569                         unpstr(nl->namep);
ragge
1.105
570                 stringbuf = osp;
571                 c = yylex();
572         }
573         if (c != STRING && c != '<')
574                 goto bad;
575
576         fn = stringbuf;
577         if (c == STRING) {
plunky
1.161
578                 savstr(&yytext[1]);
ragge
1.105
579                 stringbuf[-1] = 0;
580         } else { /* < > */
581                 while ((c = sloscan()) != '>') {
582                         if (c == '\n')
583                                 goto bad;
plunky
1.160
584                         savstr(yytext);
ragge
1.105
585                 }
586                 savch('\0');
587         }
588         while ((c = sloscan()) == WSPACE)
589                 ;
590         if (c != '\n')
591                 goto bad;
592
593         if (fsrch(fnifiles->idxifiles->incs) == 0)
594                 error("cannot find '%s'"fn);
595         prtline();
596         return;
597
plunky
1.182
598 bad:    error("bad #include_next");
ragge
1.105
599         /* error() do not return */
ragge
1.102
600 }
601
ragge
1.37
602 static int
603 definp(void)
604 {
605         int c;
606
607         do
ragge
1.97
608                 c = sloscan();
ragge
1.37
609         while (c == WSPACE);
610         return c;
ragge
1.3
611 }
612
ragge
1.80
613 void
ragge
1.79
614 getcmnt(void)
615 {
616         int c;
617
plunky
1.160
618         savstr(yytext);
ragge
1.104
619         savch(cinput()); /* Lost * */
ragge
1.79
620         for (;;) {
621                 c = cinput();
622                 if (c == '*') {
623                         c = cinput();
624                         if (c == '/') {
ragge
1.103
625                                 savstr((const usch *)"*/");
ragge
1.79
626                                 return;
627                         }
628                         cunput(c);
629                         c = '*';
630                 }
631                 savch(c);
632         }
633 }
634
635 /*
636  * Compare two replacement lists, taking in account comments etc.
637  */
638 static int
ragge
1.103
639 cmprepl(const usch *oconst usch *n)
ragge
1.79
640 {
641         for (; *oo--, n--) {
642                 /* comment skip */
643                 if (*o == '/' && o[-1] == '*') {
644                         while (*o != '*' || o[-1] != '/')
645                                 o--;
646                         o -= 2;
647                 }
648                 if (*n == '/' && n[-1] == '*') {
649                         while (*n != '*' || n[-1] != '/')
650                                 n--;
651                         n -= 2;
652                 }
653                 while (*o == ' ' || *o == '\t')
654                         o--;
655                 while (*n == ' ' || *n == '\t')
656                         n--;
657                 if (*o != *n)
658                         return 1;
659         }
660         return 0;
661 }
662
ragge
1.97
663 static int
664 isell(void)
665 {
666         int ch;
667
668         if ((ch = cinput()) != '.') {
669                 cunput(ch);
670                 return 0;
671         }
672         if ((ch = cinput()) != '.') {
673                 cunput(ch);
674                 cunput('.');
675                 return 0;
676         }
677         return 1;
678 }
679
ragge
1.3
680 void
plunky
1.143
681 define(void)
ragge
1.1
682 {
683         struct symtab *np;
ragge
1.112
684         usch *args[MAXARGS+1], *ubuf, *sbeg;
ragge
1.15
685         int ciredef;
ragge
1.1
686         int mkstr = 0narg = -1;
ragge
1.37
687         int ellips = 0;
ragge
1.128
688 #ifdef GCC_COMPAT
ragge
1.86
689         usch *gccvari = NULL;
ragge
1.91
690         int wascon;
ragge
1.86
691 #endif
ragge
1.1
692
ragge
1.37
693         if (flslvl)
694                 return;
ragge
1.97
695         if (sloscan() != WSPACE || sloscan() != IDENT)
ragge
1.37
696                 goto bad;
ragge
1.36
697
plunky
1.160
698         np = lookup(yytextENTER);
ragge
1.15
699         redef = np->value != NULL;
ragge
1.1
700
ragge
1.138
701         defining = readmac = 1;
ragge
1.15
702         sbeg = stringbuf;
ragge
1.97
703         if ((c = sloscan()) == '(') {
ragge
1.1
704                 narg = 0;
705                 /* function-like macros, deal with identifiers */
ragge
1.72
706                 c = definp();
ragge
1.37
707                 for (;;) {
ragge
1.1
708                         if (c == ')')
709                                 break;
ragge
1.97
710                         if (c == '.' && isell()) {
ragge
1.37
711                                 ellips = 1;
712                                 if (definp() != ')')
713                                         goto bad;
714                                 break;
715                         }
716                         if (c == IDENT) {
ragge
1.72
717                                 /* make sure there is no arg of same name */
718                                 for (i = 0i < nargi++)
ragge
1.116
719                                         if (!strcmp((char *) args[i], (char *)yytext))
ragge
1.72
720                                                 error("Duplicate macro "
721                                                   "parameter \"%s\""yytext);
ragge
1.112
722                                 if (narg == MAXARGS)
723                                         error("Too many macro args");
ragge
1.90
724                                 args[narg++] = xstrdup(yytext);
ragge
1.72
725                                 if ((c = definp()) == ',') {
726                                         if ((c = definp()) == ')')
727                                                 goto bad;
ragge
1.37
728                                         continue;
ragge
1.72
729                                 }
ragge
1.128
730 #ifdef GCC_COMPAT
ragge
1.97
731                                 if (c == '.' && isell()) {
ragge
1.86
732                                         if (definp() != ')')
733                                                 goto bad;
734                                         gccvari = args[--narg];
735                                         break;
736                                 }
737 #endif
ragge
1.37
738                                 if (c == ')')
739                                         break;
740                         }
741                         goto bad;
ragge
1.1
742                 }
ragge
1.97
743                 c = sloscan();
ragge
1.36
744         } else if (c == '\n') {
ragge
1.8
745                 /* #define foo */
ragge
1.36
746                 ;
ragge
1.132
747         } else if (c == 0) {
748                 prem();
ragge
1.1
749         } else if (c != WSPACE)
ragge
1.37
750                 goto bad;
ragge
1.1
751
ragge
1.31
752         while (c == WSPACE)
ragge
1.97
753                 c = sloscan();
ragge
1.1
754
ragge
1.72
755         /* replacement list cannot start with ## operator */
ragge
1.97
756         if (c == '#') {
757                 if ((c = sloscan()) == '#')
758                         goto bad;
759                 savch('\0');
ragge
1.128
760 #ifdef GCC_COMPAT
ragge
1.97
761                 wascon = 0;
762 #endif
763                 goto in2;
764         }
ragge
1.72
765
ragge
1.1
766         /* parse replacement-list, substituting arguments */
767         savch('\0');
ragge
1.36
768         while (c != '\n') {
ragge
1.128
769 #ifdef GCC_COMPAT
ragge
1.91
770                 wascon = 0;
771 loop:
772 #endif
ragge
1.1
773                 switch (c) {
774                 case WSPACE:
775                         /* remove spaces if it surrounds a ## directive */
ragge
1.2
776                         ubuf = stringbuf;
plunky
1.160
777                         savstr(yytext);
ragge
1.97
778                         c = sloscan();
779                         if (c == '#') {
780                                 if ((c = sloscan()) != '#')
781                                         goto in2;
ragge
1.2
782                                 stringbuf = ubuf;
ragge
1.1
783                                 savch(CONC);
ragge
1.97
784                                 if ((c = sloscan()) == WSPACE)
785                                         c = sloscan();
ragge
1.128
786 #ifdef GCC_COMPAT
ragge
1.91
787                                 if (c == '\n')
788                                         break;
789                                 wascon = 1;
790                                 goto loop;
791 #endif
ragge
1.1
792                         }
793                         continue;
794
ragge
1.97
795                 case '#':
796                         c = sloscan();
797                         if (c == '#') {
798                                 /* concat op */
799                                 savch(CONC);
800                                 if ((c = sloscan()) == WSPACE)
801                                         c = sloscan();
ragge
1.128
802 #ifdef GCC_COMPAT
ragge
1.97
803                                 if (c == '\n')
804                                         break;
805                                 wascon = 1;
806                                 goto loop;
ragge
1.91
807 #else
ragge
1.97
808                                 continue;
ragge
1.91
809 #endif
ragge
1.97
810                         } 
811 in2:                    if (narg < 0) {
ragge
1.1
812                                 /* no meaning in object-type macro */
813                                 savch('#');
ragge
1.97
814                                 continue;
ragge
1.1
815                         }
816                         /* remove spaces between # and arg */
817                         savch(SNUFF);
ragge
1.97
818                         if (c == WSPACE)
819                                 c = sloscan(); /* whitespace, ignore */
ragge
1.1
820                         mkstr = 1;
ragge
1.116
821                         if (c == IDENT && strcmp((char *)yytext"__VA_ARGS__") == 0)
ragge
1.37
822                                 continue;
ragge
1.1
823
824                         /* FALLTHROUGH */
825                 case IDENT:
ragge
1.116
826                         if (strcmp((char *)yytext"__VA_ARGS__") == 0) {
ragge
1.97
827                                 if (ellips == 0)
828                                         error("unwanted %s"yytext);
ragge
1.128
829 #ifdef GCC_COMPAT
830                                 savch(wascon ? GCCARG : VARG);
831 #else
ragge
1.97
832                                 savch(VARG);
ragge
1.128
833 #endif
834
ragge
1.97
835                                 savch(WARN);
836                                 if (mkstr)
837                                         savch(SNUFF), mkstr = 0;
838                                 break;
839                         }
ragge
1.1
840                         if (narg < 0)
841                                 goto id/* just add it if object */
842                         /* check if its an argument */
843                         for (i = 0i < nargi++)
ragge
1.116
844                                 if (strcmp((char *)yytext, (char *)args[i]) == 0)
ragge
1.1
845                                         break;
846                         if (i == narg) {
ragge
1.128
847 #ifdef GCC_COMPAT
ragge
1.87
848                                 if (gccvari &&
ragge
1.116
849                                     strcmp((char *)yytext, (char *)gccvari) == 0) {
ragge
1.91
850                                         savch(wascon ? GCCARG : VARG);
ragge
1.86
851                                         savch(WARN);
852                                         if (mkstr)
853                                                 savch(SNUFF), mkstr = 0;
854                                         break;
855                                 }
856 #endif
ragge
1.1
857                                 if (mkstr)
858                                         error("not argument");
859                                 goto id;
860                         }
861                         savch(i);
862                         savch(WARN);
863                         if (mkstr)
864                                 savch(SNUFF), mkstr = 0;
865                         break;
866
ragge
1.79
867                 case CMNT/* save comments */
868                         getcmnt();
869                         break;
870
ragge
1.132
871                 case 0:
872                         prem();
873
ragge
1.1
874                 default:
plunky
1.160
875 id:                     savstr(yytext);
ragge
1.1
876                         break;
877                 }
ragge
1.97
878                 c = sloscan();
ragge
1.1
879         }
ragge
1.138
880         defining = readmac = 0;
ragge
1.20
881         /* remove trailing whitespace */
882         while (stringbuf > sbeg) {
883                 if (stringbuf[-1] == ' ' || stringbuf[-1] == '\t')
884                         stringbuf--;
ragge
1.72
885                 /* replacement list cannot end with ## operator */
886                 else if (stringbuf[-1] == CONC)
887                         goto bad;
ragge
1.20
888                 else
889                         break;
890         }
ragge
1.128
891 #ifdef GCC_COMPAT