Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20130226192738

Diff

Diff from 1.188 to:

Annotations

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

Annotated File View

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