Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20121002091313

Diff

Diff from 1.160 to:

Annotations

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

Annotated File View

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