Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20121029170721

Diff

Diff from 1.182 to:

Annotations

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

Annotated File View

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