xref: /illumos-gate/usr/src/contrib/ast/src/lib/libpp/ppsym.c (revision 91b4b5393fe18d32505f967d482b81eef7f68d22)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1986-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
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  * cpp predefined symbol detection support
23  *
24  * with no args stdin is treated as an a.out for
25  * a Reiser derived cpp -- all strings that may
26  * be identifiers are listed on fd 3 (1 if no 3)
27  *
28  * with args the -D argument values are listed on fd 3 (1 if no 3)
29  */
30 
31 #include <ast.h>
32 #include <ctype.h>
33 
34 int
35 main(int argc, char** argv)
36 {
37 	register int	state;
38 	register int	c;
39 	register char*	s;
40 	Sfio_t*		out;
41 
42 	NoP(argc);
43 	if (dup(3) < 0 || !(out = sfnew(NiL, NiL, -1, 3, SF_WRITE)))
44 		out = sfstdout;
45 	if (*++argv)
46 	{
47 		while (s = *argv++)
48 			if (*s++ == '-' && *s++ == 'D' && isalpha(*s))
49 			{
50 				while (*s && *s != '=') sfputc(out, *s++);
51 				sfputc(out, '\n');
52 			}
53 		return 0;
54 	}
55 	state = 0;
56 	for (;;)
57 	{
58 		switch (c = sfgetc(sfstdin))
59 		{
60 		case EOF:
61 			break;
62 		case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
63 		case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
64 		case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
65 		case 's': case 't': case 'u': case 'v': case 'w': case 'x':
66 		case 'y': case 'z': case '_':
67 		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
68 		case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
69 		case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
70 		case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
71 		case 'Y': case 'Z':
72 			state++;
73 			sfputc(out, c);
74 			continue;
75 		case '0': case '1': case '2': case '3': case '4': case '5':
76 		case '6': case '7': case '8': case '9':
77 			if (state)
78 			{
79 				sfputc(out, c);
80 				continue;
81 			}
82 			/*FALLTHROUGH*/
83 		default:
84 			if (state)
85 			{
86 				sfputc(out, '\n');
87 				state = 0;
88 			}
89 			continue;
90 		}
91 		break;
92 	}
93 	return 0;
94 }
95