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