Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20121029171246

Diff

Diff from 1.183 to:

Annotations

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

Annotated File View

plunky
1.183
1 /*      $Id: cpp.c,v 1.183 2012/10/29 17:12:46 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);
ragge
1.9
394
ragge
1.97
395         if ((c = yylex()) == '\n')
plunky
1.181
396                 goto okret;
ragge
1.97
397
398         if (c != STRING)
ragge
1.9
399                 goto bad;
ragge
1.97
400
plunky
1.160
401         p = yytext;
plunky
1.158
402         if (*p++ == 'L')
ragge
1.97
403                 p++;
404         c = strlen((char *)p);
plunky
1.158
405         p[c - 1] = '\0';
ragge
1.37
406         if (llen < c) {
plunky
1.136
407                 /* XXX may lose heap space */
ragge
1.37
408                 lbuf = stringbuf;
409                 stringbuf += c;
410                 llen = c;
plunky
1.158
411                 if (stringbuf >= &sbf[SBSIZE]) {
412                         stringbuf = sbf/* need space to write error message */
plunky
1.182
413                         error("#line filename exceeds buffer size");
plunky
1.158
414                 }
ragge
1.37
415         }
plunky
1.158
416         memcpy(lbufpc);
ragge
1.37
417         ifiles->fname = lbuf;
plunky
1.181
418         if (yylex() != '\n')
419                 goto bad;
420
421 okret:  prtline();
422         return;
ragge
1.9
423
plunky
1.182
424 bad:    error("bad #line");
ragge
1.9
425 }
426
ragge
1.10
427 /*
ragge
1.102
428  * Search for and include next file.
429  * Return 1 on success.
430  */
431 static int
ragge
1.103
432 fsrch(const usch *fnint idxstruct incs *w)
ragge
1.102
433 {
434         int i;
435
436         for (i = idxi < 2i++) {
437                 if (i > idx)
438                         w = incdir[i];
439                 for (; ww = w->next) {
440                         usch *nm = stringbuf;
441
442                         savstr(w->dir); savch('/');
443                         savstr(fn); savch(0);
444                         if (pushfile(nmfniw->next) == 0)
445                                 return 1;
446                         stringbuf = nm;
447                 }
448         }
449         return 0;
450 }
451
ragge
1.132
452 static void
453 prem(void)
454 {
455         error("premature EOF");
456 }
457
ragge
1.102
458 /*
ragge
1.10
459  * Include a file. Include order:
ragge
1.20
460  * - For <...> files, first search -I directories, then system directories.
461  * - For "..." files, first search "current" dir, then as <...> files.
ragge
1.10
462  */
ragge
1.9
463 void
plunky
1.143
464 include(void)
ragge
1.3
465 {
ragge
1.101
466         struct symtab *nl;
ragge
1.10
467         usch *osp;
ragge
1.62
468         usch *fn, *safefn;
gmcgarry
1.133
469         int c;
ragge
1.3
470
ragge
1.37
471         if (flslvl)
472                 return;
ragge
1.10
473         osp = stringbuf;
ragge
1.97
474
ragge
1.101
475         while ((c = sloscan()) == WSPACE)
476                 ;
477         if (c == IDENT) {
478                 /* sloscan() will not expand idents */
plunky
1.160
479                 if ((nl = lookup(yytextFIND)) == NULL)
ragge
1.101
480                         goto bad;
ragge
1.112
481                 if (kfind(nl))
482                         unpstr(stringbuf);
483                 else
484                         unpstr(nl->namep);
ragge
1.101
485                 stringbuf = osp;
486                 c = yylex();
487         }
ragge
1.3
488
ragge
1.97
489         if (c == '<') {
ragge
1.10
490                 fn = stringbuf;
plunky
1.183
491                 while ((c = sloscan()) != '>') {
492                         if (c == 0)
493                                 prem();
494                         if (c == '\n')
ragge
1.10
495                                 goto bad;
plunky
1.160
496                         savstr(yytext);
ragge
1.10
497                 }
498                 savch('\0');
ragge
1.97
499                 while ((c = sloscan()) == WSPACE)
ragge
1.37
500                         ;
ragge
1.132
501                 if (c == 0)
502                         prem();
ragge
1.37
503                 if (c != '\n')
504                         goto bad;
ragge
1.62
505                 safefn = fn;
plunky
1.183
506         } else if (c == STRING) {
ragge
1.20
507                 usch *nm = stringbuf;
508
plunky
1.183
509                 fn = yytext;
510                 if (*fn++ == 'L')
511                         fn++;
512                 fn[strlen((char *)fn) - 1] = 0;
ragge
1.20
513                 /* first try to open file relative to previous file */
ragge
1.53
514                 /* but only if it is not an absolute path */
515                 if (*fn != '/') {
516                         savstr(ifiles->orgfn);
517                         if ((stringbuf =
518                             (usch *)strrchr((char *)nm'/')) == NULL)
519                                 stringbuf = nm;
520                         else
521                                 stringbuf++;
522                 }
ragge
1.62
523                 safefn = stringbuf;
ragge
1.20
524                 savstr(fn); savch(0);
ragge
1.97
525                 c = yylex();
ragge
1.132
526                 if (c == 0)
527                         prem();
ragge
1.37
528                 if (c != '\n')
529                         goto bad;
ragge
1.102
530                 if (pushfile(nmsafefn0NULL) == 0)
ragge
1.97
531                         goto okret;
plunky
1.136
532                 /* XXX may lose stringbuf space */
plunky
1.183
533         } else
534                 goto bad;
ragge
1.10
535
ragge
1.102
536         if (fsrch(safefn0incdir[0]))
537                 goto okret;
ragge
1.10
538
ragge
1.62
539         error("cannot find '%s'"safefn);
ragge
1.37
540         /* error() do not return */
ragge
1.3
541
plunky
1.182
542 bad:    error("bad #include");
ragge
1.37
543         /* error() do not return */
ragge
1.97
544 okret:
545         prtline();
ragge
1.37
546 }
547
ragge
1.102
548 void
plunky
1.143
549 include_next(void)
ragge
1.102
550 {
ragge
1.105
551         struct symtab *nl;
552         usch *osp;
553         usch *fn;
554         int c;
555
ragge
1.107
556         if (flslvl)
557                 return;
ragge
1.105
558         osp = stringbuf;
559         while ((c = sloscan()) == WSPACE)
560                 ;
561         if (c == IDENT) {
562                 /* sloscan() will not expand idents */
plunky
1.160
563                 if ((nl = lookup(yytextFIND)) == NULL)
ragge
1.105
564                         goto bad;
ragge
1.112
565                 if (kfind(nl))
566                         unpstr(stringbuf);
567                 else
568                         unpstr(nl->namep);
ragge
1.105
569                 stringbuf = osp;
570                 c = yylex();
571         }
572         if (c != STRING && c != '<')
573                 goto bad;
574
575         fn = stringbuf;
576         if (c == STRING) {
plunky
1.161
577                 savstr(&yytext[1]);
ragge
1.105
578                 stringbuf[-1] = 0;
579         } else { /* < > */
580                 while ((c = sloscan()) != '>') {
581                         if (c == '\n')
582                                 goto bad;
plunky
1.160
583                         savstr(yytext);
ragge
1.105
584                 }
585                 savch('\0');
586         }
587         while ((c = sloscan()) == WSPACE)
588                 ;
589         if (c != '\n')
590                 goto bad;
591
592         if (fsrch(fnifiles->idxifiles->incs) == 0)
593                 error("cannot find '%s'"fn);
594         prtline();
595         return;
596
plunky
1.182
597 bad:    error("bad #include_next");
ragge
1.105
598         /* error() do not return */
ragge
1.102
599 }
600
ragge
1.37
601 static int
602 definp(void)
603 {
604         int c;
605
606         do
ragge
1.97
607                 c = sloscan();
ragge
1.37
608         while (c == WSPACE);
609         return c;
ragge
1.3
610 }
611
ragge
1.80
612 void
ragge
1.79
613 getcmnt(void)
614 {
615         int c;
616
plunky
1.160
617         savstr(yytext);
ragge
1.104
618         savch(cinput()); /* Lost * */
ragge
1.79
619         for (;;) {
620                 c = cinput();
621                 if (c == '*') {
622                         c = cinput();
623                         if (c == '/') {
ragge
1.103
624                                 savstr((const usch *)"*/");
ragge
1.79
625                                 return;
626                         }
627                         cunput(c);
628                         c = '*';
629                 }
630                 savch(c);
631         }
632 }
633
634 /*
635  * Compare two replacement lists, taking in account comments etc.
636  */
637 static int
ragge
1.103
638 cmprepl(const usch *oconst usch *n)
ragge
1.79
639 {
640         for (; *oo--, n--) {
641                 /* comment skip */
642                 if (*o == '/' && o[-1] == '*') {
643                         while (*o != '*' || o[-1] != '/')
644                                 o--;
645                         o -= 2;
646                 }
647                 if (*n == '/' && n[-1] == '*') {
648                         while (*n != '*' || n[-1] != '/')
649                                 n--;
650                         n -= 2;
651                 }
652                 while (*o == ' ' || *o == '\t')
653                         o--;
654                 while (*n == ' ' || *n == '\t')
655                         n--;
656                 if (*o != *n)
657                         return 1;
658         }
659         return 0;
660 }
661
ragge
1.97
662 static int
663 isell(void)
664 {
665         int ch;
666
667         if ((ch = cinput()) != '.') {
668                 cunput(ch);
669                 return 0;
670         }
671         if ((ch = cinput()) != '.') {
672                 cunput(ch);
673                 cunput('.');
674                 return 0;
675         }
676         return 1;
677 }
678
ragge
1.3
679 void
plunky
1.143
680 define(void)
ragge
1.1
681 {
682         struct symtab *np;
ragge
1.112
683         usch *args[MAXARGS+1], *ubuf, *sbeg;
ragge
1.15
684         int ciredef;
ragge
1.1
685         int mkstr = 0narg = -1;
ragge
1.37
686         int ellips = 0;
ragge
1.128
687 #ifdef GCC_COMPAT
ragge
1.86
688         usch *gccvari = NULL;
ragge
1.91
689         int wascon;
ragge
1.86
690 #endif
ragge
1.1
691
ragge
1.37
692         if (flslvl)
693                 return;
ragge
1.97
694         if (sloscan() != WSPACE || sloscan() != IDENT)
ragge
1.37
695                 goto bad;
ragge
1.36
696
plunky
1.160
697         np = lookup(yytextENTER);
ragge
1.15
698         redef = np->value != NULL;
ragge
1.1
699
ragge
1.138
700         defining = readmac = 1;
ragge
1.15
701         sbeg = stringbuf;
ragge
1.97
702         if ((c = sloscan()) == '(') {
ragge
1.1
703                 narg = 0;
704                 /* function-like macros, deal with identifiers */
ragge
1.72
705                 c = definp();
ragge
1.37
706                 for (;;) {
ragge
1.1
707                         if (c == ')')
708                                 break;
ragge
1.97
709                         if (c == '.' && isell()) {
ragge
1.37
710                                 ellips = 1;
711                                 if (definp() != ')')
712                                         goto bad;
713                                 break;
714                         }
715                         if (c == IDENT) {
ragge
1.72
716                                 /* make sure there is no arg of same name */
717                                 for (i = 0i < nargi++)
ragge
1.116
718                                         if (!strcmp((char *) args[i], (char *)yytext))
ragge
1.72
719                                                 error("Duplicate macro "
720                                                   "parameter \"%s\""yytext);
ragge
1.112
721                                 if (narg == MAXARGS)
722                                         error("Too many macro args");
ragge
1.90
723                                 args[narg++] = xstrdup(yytext);
ragge
1.72
724                                 if ((c = definp()) == ',') {
725                                         if ((c = definp()) == ')')
726                                                 goto bad;
ragge
1.37
727                                         continue;
ragge
1.72
728                                 }
ragge
1.128
729 #ifdef GCC_COMPAT
ragge
1.97
730                                 if (c == '.' && isell()) {
ragge
1.86
731                                         if (definp() != ')')
732                                                 goto bad;
733                                         gccvari = args[--narg];
734                                         break;
735                                 }
736 #endif
ragge
1.37
737                                 if (c == ')')
738                                         break;
739                         }
740                         goto bad;
ragge
1.1
741                 }
ragge
1.97
742                 c = sloscan();
ragge
1.36
743         } else if (c == '\n') {
ragge
1.8
744                 /* #define foo */
ragge
1.36
745                 ;
ragge
1.132
746         } else if (c == 0) {
747                 prem();
ragge
1.1
748         } else if (c != WSPACE)
ragge
1.37
749                 goto bad;
ragge
1.1
750
ragge
1.31
751         while (c == WSPACE)
ragge
1.97
752                 c = sloscan();
ragge
1.1
753
ragge
1.72
754         /* replacement list cannot start with ## operator */
ragge
1.97
755         if (c == '#') {
756                 if ((c = sloscan()) == '#')
757                         goto bad;
758                 savch('\0');
ragge
1.128
759 #ifdef GCC_COMPAT
ragge
1.97
760                 wascon = 0;
761 #endif
762                 goto in2;
763         }
ragge
1.72
764
ragge
1.1
765         /* parse replacement-list, substituting arguments */
766         savch('\0');
ragge
1.36
767         while (c != '\n') {
ragge
1.128
768 #ifdef GCC_COMPAT
ragge
1.91
769                 wascon = 0;
770 loop:
771 #endif
ragge
1.1
772                 switch (c) {
773                 case WSPACE:
774                         /* remove spaces if it surrounds a ## directive */
ragge
1.2
775                         ubuf = stringbuf;
plunky
1.160
776                         savstr(yytext);
ragge
1.97
777                         c = sloscan();
778                         if (c == '#') {
779                                 if ((c = sloscan()) != '#')
780                                         goto in2;
ragge
1.2
781                                 stringbuf = ubuf;
ragge
1.1
782                                 savch(CONC);
ragge
1.97
783                                 if ((c = sloscan()) == WSPACE)
784                                         c = sloscan();
ragge
1.128
785 #ifdef GCC_COMPAT
ragge
1.91
786                                 if (c == '\n')
787                                         break;
788                                 wascon = 1;
789                                 goto loop;
790 #endif
ragge
1.1
791                         }
792                         continue;
793
ragge
1.97
794                 case '#':
795                         c = sloscan();
796                         if (c == '#') {
797                                 /* concat op */
798                                 savch(CONC);
799                                 if ((c = sloscan()) == WSPACE)
800                                         c = sloscan();
ragge
1.128
801 #ifdef GCC_COMPAT
ragge
1.97
802                                 if (c == '\n')
803                                         break;
804                                 wascon = 1;
805                                 goto loop;
ragge
1.91
806 #else
ragge
1.97
807                                 continue;
ragge
1.91
808 #endif
ragge
1.97
809                         } 
810 in2:                    if (narg < 0) {
ragge
1.1
811                                 /* no meaning in object-type macro */
812                                 savch('#');
ragge
1.97
813                                 continue;
ragge
1.1
814                         }
815                         /* remove spaces between # and arg */
816                         savch(SNUFF);
ragge
1.97
817                         if (c == WSPACE)
818                                 c = sloscan(); /* whitespace, ignore */
ragge
1.1
819                         mkstr = 1;
ragge
1.116
820                         if (c == IDENT && strcmp((char *)yytext"__VA_ARGS__") == 0)
ragge
1.37
821                                 continue;
ragge
1.1
822
823                         /* FALLTHROUGH */
824                 case IDENT:
ragge
1.116
825                         if (strcmp((char *)yytext"__VA_ARGS__") == 0) {
ragge
1.97
826                                 if (ellips == 0)
827                                         error("unwanted %s"yytext);
ragge
1.128
828 #ifdef GCC_COMPAT
829                                 savch(wascon ? GCCARG : VARG);
830 #else
ragge
1.97
831                                 savch(VARG);
ragge
1.128
832 #endif
833
ragge
1.97
834                                 savch(WARN);
835                                 if (mkstr)
836                                         savch(SNUFF), mkstr = 0;
837                                 break;
838                         }
ragge
1.1
839                         if (narg < 0)
840                                 goto id/* just add it if object */
841                         /* check if its an argument */
842                         for (i = 0i < nargi++)
ragge
1.116
843                                 if (strcmp((char *)yytext, (char *)args[i]) == 0)
ragge
1.1
844                                         break;
845                         if (i == narg) {
ragge
1.128
846 #ifdef GCC_COMPAT
ragge
1.87
847                                 if (gccvari &&
ragge
1.116
848                                     strcmp((char *)yytext, (char *)gccvari) == 0) {
ragge
1.91
849                                         savch(wascon ? GCCARG : VARG);
ragge
1.86
850                                         savch(WARN);
851                                         if (mkstr)
852                                                 savch(SNUFF), mkstr = 0;
853                                         break;
854                                 }
855 #endif
ragge
1.1
856                                 if (mkstr)
857                                         error("not argument");
858                                 goto id;
859                         }
860                         savch(i);
861                         savch(WARN);
862                         if (mkstr)
863                                 savch(SNUFF), mkstr = 0;
864                         break;
865
ragge
1.79
866                 case CMNT/* save comments */
867                         getcmnt();
868                         break;
869
ragge
1.132
870                 case 0:
871                         prem();
872
ragge
1.1
873                 default:
plunky
1.160
874 id:                     savstr(yytext);
ragge
1.1
875                         break;
876                 }
ragge
1.97
877                 c = sloscan();
ragge
1.1
878         }
ragge
1.138
879         defining = readmac = 0;
ragge
1.20
880         /* remove trailing whitespace */
881         while (stringbuf > sbeg) {
882                 if (stringbuf[-1] == ' ' || stringbuf[-1] == '\t')
883                         stringbuf--;
ragge
1.72
884                 /* replacement list cannot end with ## operator */
885                 else if (stringbuf[-1] == CONC)
886                         goto bad;
ragge
1.20
887                 else
888                         break;
889         }
ragge
1.128
890 #ifdef GCC_COMPAT
ragge
1.86
891