Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20110517014912

Diff

Diff from 1.131 to:

Annotations

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

Annotated File View

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