1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1986-2009 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * *
19 ***********************************************************************/
20 #pragma prototyped
21 /*
22 * Glenn Fowler
23 * AT&T Research
24 *
25 * common preprocessor command line argument parse
26 * called by optjoin()
27 */
28
29 static const char usage[] =
30 "[-?\n@(#)$Id: cpp (AT&T Research) 2009-02-02 $\n]"
31 USAGE_LICENSE
32 "[+NAME?cpp - C language preprocessor]"
33 "[+DESCRIPTION?\bcpp\b is the preprocessor for all C language dialects. It is"
34 " a standalone version of the \blibpp\b(3) preprocessor library. The"
35 " C dialect implemented by \bcpp\b is determined by probing \bcc\b(1)"
36 " using \bprobe\b(1). The path of the emulated compiler can be changed"
37 " by the \b-D-X\b command line option.]"
38 "[+?If \aoutput\a is omitted then the standard output is written; if \ainput\a"
39 " is also omitted then the standard input is read. NOTE: this is an"
40 " ancient, non-standard, non-intuitiive file operand syntax that is"
41 " required by \bcc\b(1); use shell file name expansion at your peril.]"
42 "[+?\bcpp\b specific options are set by the \b-D-\b and \b-I-\b options.]"
43
44 "[C:comments?Pass comments to the output. By default comments are omitted.]"
45 "[D:define?Define the macro \aname\a to have \avalue\a; \b1\b is assumed if"
46 " \b=\b\avalue\a is omitted. If \aname\a begins with \b:\b then it is"
47 " interpreted as a \blibpp\b(3) \b#pragma pp:\b statement; if \aname\a"
48 " begins with \b%\b then it is interpreted as a \blibpp\b(3) \b#\b"
49 " directive statement; if \aname\a begins with \b-\b or \b+\b then it is"
50 " interpreted as a \blibpp\b(3) option; \b-\b turns the option on,"
51 " \b+\b turns it off. Most options have a \b#pragma\b counterpart that"
52 " is listed with the option definition. Right, this is ugly, but its the"
53 " only portable way to pass options through \bcc\b(1) to"
54 " \bcpp\b:]:[name[=value]]]{"
55 " [+-D-C, pp::compatibility?Preprocess for K&R compatibility.]"
56 " [+-D-D\alevel\a, \bpp::debug\b \alevel\a?Set the debug trace level."
57 " Higher levels produce more output. Levels higher than 3"
58 " enabled only in \b-g\b compiled versions.]"
59 " [+-D-F\aname\a?Set the main input file name to \aname\a. This only"
60 " affects error message and line sync output.]"
61 " [+-D-H, pp::hosted?All directories are hosted; compatibility"
62 " warning messages from hosted directory headers are suppressed.]"
63 " [+-D-I, pp::cdir?All directories contain C headers; used only with"
64 " \b-D-+\b.]"
65 " [+-D-K, pp::keyargs?Enable the non-standard \aname=value\a macro"
66 " argument mode.]"
67 " [+-D-L\b[\aid\a]], \bpp::lineid\b [\aid\a]]?Set the line sync directive"
68 " id to \aid\a or null if omitted.]"
69 " [+-D-M, pp::nomultiple?Disable multiple include detection.]"
70 " [+-D-P, pp::passthrough?Enable the non-standard passthrough mode; may"
71 " be useful for processing non-C input.]"
72 " [+-D-Q, pp::dump?Dump macro definitions to the output so that the"
73 " output may be passed through \bcpp\b again. Used for"
74 " generating precompiled headers.]"
75 " [+-D-R, pp::transition?Enable the transition preprocessing mode. Used"
76 " for compilers that can't make up their semantics between"
77 " K&R and ISO.]"
78 " [+-D-S, pp::strict?Enable strict preprocessing semantics and warnings."
79 " Works with any mode (compatibiliy, transition,"
80 " or the default ISO).]"
81 " [+-D-T\atest\a, \bpp::test\b \atest\a?Enable implementation specific"
82 " test code according to \atest\a.]"
83 " [+-D-W, pp::warn?Enable warnings in non-hosted files.]"
84 " [+-D-X\b[\acc\a]]?Preprocess for the compiler \acc\a which must be"
85 " an executable path or an executable on \b$PATH\b.]"
86 " [+-D-Y, pp::pedantic?Enable pedantic \bpp::warn\b warnings in"
87 " non-hosted files.]"
88 " [+-D-Z, pp::pool?Enable pool mode. See \blibpp\b(3).]"
89 " [+-D-d?List canonicalized \b#define\b statements for non-predefined"
90 " macros in the output. ]"
91 " [+-D-m?List canonicalized \b#define\b statements for all macros. All"
92 " other output is disabled.]"
93 " [+-D-+, pp::plusplus?Preprocess for the C++ dialect.]"
94 "}"
95 "[I:include?Append \adirectory\a to the list of directories searched for"
96 " \b#include\b files. If \adirectory\a is \b-\b then: (1) \b-I\b"
97 " directories before \b-I-\b are searched only for \"...\" include"
98 " files; (2) \b-I\b directories after \b-I-\b are searched for"
99 " \"...\" and <...> include files; (3) the directory \b.\b is searched"
100 " only if it is explicitly specified by a \b-I\b option.]:?[directory]{"
101 " [+-I-C\adirectory\a, \bpp::cdir\b \adirectory\a?Mark \adirectory\a"
102 " as a C header directory. Used with \bpp:plusplus\b.]"
103 " [+-I-D[\afile\a]]?Read the default \bprobe\b(1) definitions from"
104 " \afile\a, or ignore the default definitions if \afile\a"
105 " is omitted.]"
106 " [+-I-H\adirectory\a, \bpp::hostdir\b \adirectory\a?Mark \adirectory\a"
107 " as a hosted directory. Headers from hosted directories have"
108 " compatibility warnings disabled.]"
109 " [+-I-I\aheader\a, \bpp::ignore\b \aheader\a?Add \aheader\a to the"
110 " list of ignored headers.]"
111 " [+-I-M\afile\a?\afile\a contains a sequence of \aheader\a"
112 " [= \"\amap\a\" ]] lines, where \aheader\a is either <\aname\a>"
113 " or \"\aname\a\", and \"\amap\a\" is an explicit binding"
114 " for \aheader\a. \aheader\a is ignored if = \"\amap\a\" is"
115 " omitted.]"
116 " [+-I-R\afile\a?Include \afile\a but do not emit text or line syncs.]"
117 " [+-I-S\adirectory\a?Add \adirectory\a to the default standard include"
118 " directory list.]"
119 " [+-I-T\afile\a?Include \afile\a and emit text to the output file.]"
120 "}"
121 "[M:dependencies?Generate \bmake\b(1) dependencies. Not needed with"
122 " \bnmake\b(1). \b-M\b may be followed by optional \aflags\a to change"
123 " dependency output styles:]{"
124 " [+D?Generate dependencies in a separate \b.d\b file. Preprocessed"
125 " output is still written to \aoutput\a, or the standard output"
126 " if \aoutput\a is omitted.]"
127 " [+G?Generate missing dependencies too.]"
128 " [+M?Only generate local header dependencies; \ahosted\a headers are"
129 " omitted. Note that \ahosted\a headers are determined by"
130 " \b-I-H\b and the \bpp:hosted\b and \bpp:hostdir\b pragmas;"
131 " no special distiction is made between \"\" and <> \binclude\b"
132 " styles.]"
133 "}"
134 "[P!:sync?Emit line syncs.]"
135 "[U:undefine?Remove the definition for the macro \aname\a.]:[name]"
136
137 "[A:assert?Enter the assertion via \b#assert\b for system V"
138 " compatibility.]:[assertion]"
139 "[E:preprocess?Ignored for compatibility with ancient compilers.]"
140 "[H:include-reference?Emit \b#include\b file paths on the standard error,"
141 " one per line, indented to show nesting.]"
142 "[T?If not \bgcc\b(1) then truncate identifiers to \alength\a"
143 " characters for compatibility with old AT&T (I guess only Lucent needs"
144 " them now) compilers.]#?[length]"
145 "[V:version?Emit the \blibpp\b(3) version.]"
146 "[X:argmode?Enable \aname\a=\avalue\a macro arguments for \beasel\b(1)"
147 " compatibility.]"
148 "[Y:standard?Add \adirectory\a to the list searched for"
149 " \b#include\b \b<...>\b files.]:[directory]"
150
151 "\n"
152 "\n[ input [ output ] ]\n"
153 "\n"
154
155 "[+SEE ALSO?\bcc\b(1), \bgcc\b(1), \blibpp\b(3)]"
156 ;
157
158 #include "pplib.h"
159
160 #include <ctype.h>
161
162 /*
163 * convert lint comments to pragmas
164 */
165
166 static void
pplint(char * head,char * comment,char * tail,int line)167 pplint(char* head, char* comment, char* tail, int line)
168 {
169 NoP(line);
170 if (strmatch(comment, "(ARGSUSED|PRINTFLIKE|PROTOLIB|SCANFLIKE|VARARGS)*([0-9])|CONSTCOND|CONSTANTCOND|CONSTANTCONDITION|EMPTY|FALLTHRU|FALLTHROUGH|LINTLIBRARY|LINTED*|NOTREACHED"))
171 {
172 strncopy(pp.token, comment, MAXTOKEN);
173 ppprintf("\n#%s %s:%s\n", dirname(PRAGMA), pp.pass, pp.token);
174 ppline(error_info.line, NiL);
175 }
176 }
177
178 /*
179 * if last!=0 then argv[opt_info.index]==0 with return(0)
180 * else if argv[opt_info.index]==0 then return(0)
181 * otherwise argv[opt_info.index] is the first unrecognized
182 * option with return(1)
183 *
184 * use last=0 if the preprocessor is combined with other passes
185 * so that unknown options may be interpreted for those passes
186 */
187
188 int
ppargs(char ** argv,int last)189 ppargs(char** argv, int last)
190 {
191 register char* s;
192 register int c;
193 register int n;
194 char* p;
195
196 /*
197 * check the args and initialize
198 */
199
200 if (!error_info.id)
201 error_info.id = "cpp";
202 for (;;)
203 {
204 for (; c = optget(argv, usage); last = 0) switch (c)
205 {
206 case 'C':
207 ppop(PP_COMMENT, ppcomment);
208 break;
209 case 'D':
210 /*
211 * this allows single arg pp option extensions
212 * without touching cc
213 * (not all cc wrappers have -W...)
214 */
215
216 switch (*(s = opt_info.arg))
217 {
218 case '-':
219 case '+':
220 n = (*s++ == '-');
221 while (c = *s++) switch (c)
222 {
223 case 'C':
224 ppop(PP_COMPATIBILITY, n);
225 break;
226 case 'D':
227 if (n && ((c = strtol(s, &p, 0)) || p != s))
228 {
229 s = p;
230 n = c;
231 }
232 ppop(PP_DEBUG, -n);
233 break;
234 case 'F':
235 ppop(PP_FILENAME, n ? s : NiL);
236 goto hasarg;
237 case 'H':
238 ppop(PP_HOSTDIR, "-", n);
239 break;
240 case 'I':
241 ppop(PP_CDIR, "-", n);
242 break;
243 case 'K':
244 ppop(PP_KEYARGS, n);
245 break;
246 case 'L':
247 ppop(PP_LINEID, n && *s ? s : "line");
248 goto hasarg;
249 case 'M':
250 ppop(PP_MULTIPLE, !n);
251 break;
252 case 'P':
253 ppop(PP_PASSTHROUGH, n);
254 break;
255 case 'Q':
256 ppop(PP_DUMP, n);
257 break;
258 case 'R':
259 ppop(PP_TRANSITION, n);
260 break;
261 case 'S':
262 ppop(PP_STRICT, n);
263 break;
264 case 'T':
265 ppop(PP_TEST, s);
266 goto hasarg;
267 case 'V':
268 ppop(PP_VENDOR, "-", n);
269 break;
270 case 'W':
271 ppop(PP_WARN, n);
272 break;
273 case 'X':
274 ppop(PP_PROBE, n && *s ? s : 0);
275 goto hasarg;
276 case 'Y':
277 ppop(PP_PEDANTIC, n);
278 break;
279 case 'Z':
280 ppop(PP_POOL, n);
281 break;
282 case 'd':
283 pp.option |= DEFINITIONS;
284 break;
285 case 'm':
286 pp.state |= NOTEXT;
287 pp.option |= KEEPNOTEXT|DEFINITIONS|PREDEFINITIONS;
288 pp.linesync = 0;
289 break;
290 case '+':
291 ppop(PP_PLUSPLUS, n);
292 break;
293 default:
294 if (pp.optarg)
295 {
296 if ((c = (*pp.optarg)(n, c, s)) > 0) goto hasarg;
297 else if (!c) break;
298 }
299 error(1, "%c%s: unknown -D option overload", n ? '-' : '+', s - 1);
300 goto hasarg;
301 }
302 hasarg:
303 break;
304 case ':':
305 ppop(PP_OPTION, s + 1);
306 break;
307 case '%':
308 ppop(PP_DIRECTIVE, s + 1);
309 break;
310 case '_':
311 if (strmatch(s, "__GNUC__*"))
312 pp.arg_style |= STYLE_gnu;
313 else if (strmatch(s, "__(ANSI|STDC|STRICT)__*") || !(pp.arg_style & STYLE_gnu) && strmatch(s, "__STRICT_ANSI__*"))
314 ppop(PP_STRICT, 1);
315 else if (strmatch(s, "__cplusplus*"))
316 ppop(PP_PLUSPLUS, 1);
317 /*FALLTHROUGH*/
318 default:
319 ppop(PP_DEFINE, s);
320 break;
321 }
322 break;
323 case 'E':
324 /* historically ignored */
325 break;
326 case 'I':
327 if (!(s = opt_info.arg))
328 {
329 /*
330 * some compilers interpret `-I ...' as
331 * `-I-S' and arg ... while others interpret
332 * it as `-I...'
333 */
334
335 p = "-S";
336 if ((s = argv[opt_info.index]) && ((n = *s++) == '-' || n == '+') && *s++ == 'D')
337 {
338 if (isalpha(*s) || *s == '_')
339 while (isalnum(*++s) || *s == '_');
340 if (*s && *s != '=' && *s != '-' && *s != '+')
341 p = argv[opt_info.index++];
342 }
343 s = p;
344 }
345 switch (*s)
346 {
347 case '-':
348 case '+':
349 n = *(p = s++) == '-';
350 c = *s++;
351 if (!n && !*s) s = 0;
352 switch (c)
353 {
354 case 0:
355 ppop(PP_LOCAL);
356 break;
357 case 'C':
358 ppop(PP_CDIR, s, n);
359 break;
360 case 'D':
361 ppop(PP_DEFAULT, s);
362 break;
363 case 'H':
364 ppop(PP_HOSTDIR, s, n);
365 break;
366 case 'I':
367 ppop(PP_IGNORE, s);
368 break;
369 case 'M':
370 ppop(PP_IGNORELIST, s);
371 break;
372 case 'R':
373 ppop(PP_READ, s);
374 break;
375 case 'S':
376 ppop(PP_STANDARD, s);
377 break;
378 case 'T':
379 ppop(PP_TEXT, s);
380 break;
381 case 'V':
382 ppop(PP_VENDOR, s, n);
383 break;
384 default:
385 error(1, "%s: unknown -I option overload", p);
386 break;
387 }
388 break;
389 default:
390 ppop(PP_INCLUDE, s);
391 break;
392 }
393 break;
394 case 'M':
395 for (n = PP_deps; argv[opt_info.index]; opt_info.offset++)
396 {
397 switch (argv[opt_info.index][opt_info.offset])
398 {
399 case 'D':
400 n |= PP_deps_file;
401 continue;
402 case 'G':
403 n |= PP_deps_generated;
404 continue;
405 case 'M':
406 n |= PP_deps_local;
407 continue;
408 }
409 break;
410 }
411 ppop(PP_FILEDEPS, n);
412 break;
413 case 'P':
414 ppop(PP_LINE, (PPLINESYNC)0);
415 break;
416 case 'U':
417 ppop(PP_UNDEF, opt_info.arg);
418 break;
419
420 /*
421 * System V CCS compatibility
422 */
423
424 case 'A':
425 if (isalpha(opt_info.arg[0]) || opt_info.arg[0] == '_' || opt_info.arg[0] == '$')
426 ppop(PP_ASSERT, opt_info.arg);
427 break;
428 case 'H':
429 ppop(PP_INCREF, ppincref);
430 break;
431 case 'T':
432 if (!(pp.arg_style & STYLE_gnu))
433 ppop(PP_TRUNCATE, TRUNCLENGTH);
434 /* else enable ANSI trigraphs -- default */
435 break;
436 case 'V':
437 error(0, "%s", pp.version);
438 break;
439 case 'X':
440 pp.arg_mode = (*(opt_info.arg + 1) || pp.arg_mode && pp.arg_mode != *opt_info.arg) ? '-' : *opt_info.arg;
441 break;
442 case 'Y':
443 if (*(s = opt_info.arg) && *(s + 1) == ',')
444 {
445 if (*s != 'I') break;
446 s += 2;
447 }
448 ppop(PP_STANDARD, s);
449 break;
450
451 /*
452 * errors
453 */
454
455 case '?':
456 error(ERROR_USAGE|4, "%s", opt_info.arg);
457 break;
458 case ':':
459 if (!last)
460 {
461 opt_info.again = 1;
462 return(1);
463 }
464
465 /*
466 * cross your fingers
467 */
468
469 if (!(s = argv[opt_info.index]))
470 error(3, "%s", opt_info.arg);
471 if (opt_info.offset == 2 && (pp.arg_style & STYLE_gnu))
472 {
473 p = argv[opt_info.index + 1];
474 if (streq(s, "-$"))
475 {
476 ppop(PP_OPTION, "noid \"$\"");
477 goto ignore;
478 }
479 else if (streq(s, "-dD"))
480 {
481 pp.option |= DEFINITIONS;
482 goto ignore;
483 }
484 else if (streq(s, "-dM"))
485 {
486 pp.state |= NOTEXT;
487 pp.option |= KEEPNOTEXT|DEFINITIONS|PREDEFINITIONS;
488 pp.linesync = 0;
489 goto ignore;
490 }
491 else if (streq(s, "-imacros"))
492 {
493 if (p)
494 {
495 ppop(PP_READ, p);
496 opt_info.index++;
497 opt_info.offset = 0;
498 }
499 goto ignore;
500 }
501 else if (streq(s, "-include"))
502 {
503 if (p)
504 {
505 ppop(PP_TEXT, p);
506 opt_info.index++;
507 opt_info.offset = 0;
508 }
509 opt_info.offset = 0;
510 goto ignore;
511 }
512 else if (strneq(s, "-lang-", 6))
513 {
514 s += 6;
515 if (streq(s, "c"))
516 c = 0;
517 else if (streq(s, "c++"))
518 c = 1;
519 else if (streq(s, "objc"))
520 c = 2;
521 else if (streq(s, "objc++"))
522 c = 3;
523 ppop(PP_PLUSPLUS, c & 1);
524 if (c & 2)
525 ppop(PP_DIRECTIVE, "pragma pp:map \"/#(pragma )?import>/\" \"/#(pragma )?import(.*)/__STDPP__IMPORT__(\\2)/\"\n\
526 #macdef __STDPP__IMPORT__(x)\n\
527 #pragma pp:noallmultiple\n\
528 #include x\n\
529 #pragma pp:allmultiple\n\
530 #endmac");
531 goto ignore;
532 }
533 else if (streq(s, "-lint"))
534 {
535 ppop(PP_COMMENT, pplint);
536 goto ignore;
537 }
538 }
539 s += opt_info.offset - 1;
540 if (strmatch(s, "i*.h"))
541 ppop((pp.arg_style & STYLE_gnu) || s[1] == '/' ? PP_READ : PP_TEXT, s + 1);
542 else if (strmatch(s, "*@(nostandard|nostdinc)*"))
543 ppop(PP_STANDARD, "");
544 else if (strmatch(s, "*@(exten|xansi)*|std"))
545 {
546 ppop(PP_COMPATIBILITY, 0);
547 ppop(PP_TRANSITION, 1);
548 }
549 else if (strmatch(s, "*@(ansi|conform|pedantic|stand|std1|strict[!-])*"))
550 {
551 ppop(PP_COMPATIBILITY, 0);
552 ppop(PP_STRICT, 1);
553 if (strmatch(s, "*pedantic*"))
554 ppop(PP_PEDANTIC, 1);
555 }
556 else if (strmatch(s, "*@(trans)*"))
557 {
558 ppop(PP_COMPATIBILITY, 1);
559 ppop(PP_TRANSITION, 1);
560 }
561 else if (strmatch(s, "*@(classic|compat|std0|tradition|[kK][n&+][rR])*"))
562 {
563 ppop(PP_COMPATIBILITY, 1);
564 ppop(PP_TRANSITION, 0);
565 }
566 else if (strmatch(s, "*@(plusplus|++)*"))
567 ppop(PP_PLUSPLUS, 1);
568 else if (strmatch(s, "*@(warn)*"))
569 ppop(PP_WARN, 1);
570
571 /*
572 * ignore unknown options
573 * the probe info takes care of these
574 * fails if an option value is in the next arg
575 * and this is the last option
576 */
577
578 if (argv[opt_info.index + 1] && argv[opt_info.index + 1][0] != '-' && argv[opt_info.index + 2] && argv[opt_info.index + 2][0] == '-')
579 {
580 opt_info.index++;
581 opt_info.offset = 0;
582 }
583 ignore:
584 while (argv[opt_info.index][opt_info.offset]) opt_info.offset++;
585 break;
586 }
587 if (!(s = argv[opt_info.index])) return(0);
588 switch (pp.arg_file)
589 {
590 case 0:
591 if (*s != '-' || *(s + 1)) ppop(PP_INPUT, s);
592 break;
593 case 1:
594 if (*s != '-' || *(s + 1)) ppop(PP_OUTPUT, s);
595 break;
596 default:
597 if (!last) return(1);
598 error(1, "%s: extraneous argument ignored", s);
599 break;
600 }
601 pp.arg_file++;
602 if (!argv[++opt_info.index]) return(0);
603
604 /*
605 * old versions allow options after file args
606 */
607 }
608 }
609