Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20140530192701

Diff

Diff from 1.193 to:

Annotations

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

Annotated File View

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