xref: /minix3/minix/commands/ifdef/ifdef.c (revision d0055759dd8892194db7fce6acc5085d5c9aeaee)
1433d6423SLionel Sambuc /* ifdef - remove #ifdefs		Author: Warren Toomey */
2433d6423SLionel Sambuc 
3433d6423SLionel Sambuc /* Copyright 1989 by Warren Toomey	wkt@cs.adfa.oz.au[@uunet.uu.net]
4433d6423SLionel Sambuc  *
5433d6423SLionel Sambuc  * You may freely copy or distribute this code as long as this notice
6433d6423SLionel Sambuc  * remains intact.
7433d6423SLionel Sambuc  *
8433d6423SLionel Sambuc  * You may modify this code, as long as this notice remains intact, and
9433d6423SLionel Sambuc  * you add another notice indicating that the code has been modified.
10433d6423SLionel Sambuc  *
11433d6423SLionel Sambuc  * You may NOT sell this code or in any way profit from this code without
12433d6423SLionel Sambuc  * prior agreement from the author.
13433d6423SLionel Sambuc  */
14433d6423SLionel Sambuc 
15433d6423SLionel Sambuc #include <sys/types.h>
16433d6423SLionel Sambuc #include <string.h>
17433d6423SLionel Sambuc #include <stdlib.h>
18433d6423SLionel Sambuc #include <stdio.h>
19433d6423SLionel Sambuc #include <unistd.h>
20433d6423SLionel Sambuc 
21433d6423SLionel Sambuc /* Definition of structures and constants used in ifdef.c  */
22433d6423SLionel Sambuc 
23433d6423SLionel Sambuc /* Types of symbols */
24433d6423SLionel Sambuc #define DEF	  1		/* Symbol is defined    */
25433d6423SLionel Sambuc #define UNDEF	  2		/* Symbol isn't defined */
26433d6423SLionel Sambuc #define IGN	  3		/* Ignore this symbol unless defined */
27433d6423SLionel Sambuc 
28433d6423SLionel Sambuc /* Redef mode values */
29433d6423SLionel Sambuc #define MUTABLE   1		/* Symbol can change defined <-> undefined */
30433d6423SLionel Sambuc #define IMMUTABLE 2		/* Symbol can't change as above            */
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc /* Processing modes */
33433d6423SLionel Sambuc #define NO	0		/* Don't process */
34433d6423SLionel Sambuc #define YES	1		/* Process */
35433d6423SLionel Sambuc 
36433d6423SLionel Sambuc /* Ignore (IGN), ignore but process */
37433d6423SLionel Sambuc struct DEFINE {
38433d6423SLionel Sambuc   char *symbol;			/* SLL of defined symbols. The redef  */
39433d6423SLionel Sambuc   char type;			/* field indicates if this symbol can */
40433d6423SLionel Sambuc   char redef;			/* change from defined <-> undefined. */
41433d6423SLionel Sambuc   struct DEFINE *next;		/* Type is DEF or UNDEF.	      */
42433d6423SLionel Sambuc };
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc /* Global variables & structures */
45433d6423SLionel Sambuc FILE *zin;			/* Input file for processing  */
46433d6423SLionel Sambuc struct DEFINE *defptr;		/* Defined symbols SLL        */
47433d6423SLionel Sambuc struct DEFINE *defend;		/* Ptr to last node in defptr */
48433d6423SLionel Sambuc struct DEFINE *deftemp;		/* Ptr to last found node     */
49433d6423SLionel Sambuc int line = 1;			/* Current line number        */
50433d6423SLionel Sambuc int table = 0;			/* Don't normally want a table */
51433d6423SLionel Sambuc 
52433d6423SLionel Sambuc extern int optind;
53433d6423SLionel Sambuc extern char *optarg;
54433d6423SLionel Sambuc 
55433d6423SLionel Sambuc /* Prototypes. */
56433d6423SLionel Sambuc int main(int argc, char **argv);
57433d6423SLionel Sambuc char fgetarg(FILE *stream, char *cbuf);
58433d6423SLionel Sambuc int find(char *symd);
59433d6423SLionel Sambuc void defit(char *sym, int redef, int typed);
60433d6423SLionel Sambuc void stop(void);
61433d6423SLionel Sambuc void gotoeoln(void);
62433d6423SLionel Sambuc void prteoln(void);
63433d6423SLionel Sambuc void printtable(void);
64433d6423SLionel Sambuc char getendif(void);
65433d6423SLionel Sambuc void gettable(void);
66433d6423SLionel Sambuc void parse(void);
67433d6423SLionel Sambuc void usage(void);
68433d6423SLionel Sambuc 
69433d6423SLionel Sambuc #ifdef __STDC__
fgetarg(FILE * stream,char * cbuf)70433d6423SLionel Sambuc char fgetarg ( FILE *stream , char *cbuf )
71433d6423SLionel Sambuc #else
72433d6423SLionel Sambuc char fgetarg(stream, cbuf)	/* Get next arg from file into cbuf, */
73433d6423SLionel Sambuc FILE *stream;			/* returning the character that      */
74433d6423SLionel Sambuc char *cbuf;			/* terminated it. Cbuf returns 0     */
75433d6423SLionel Sambuc #endif
76433d6423SLionel Sambuc {				/* if no arg. EOF is returned if no  */
77433d6423SLionel Sambuc   int ch;			/* args left in file.                */
78433d6423SLionel Sambuc   int i;
79433d6423SLionel Sambuc 
80433d6423SLionel Sambuc   i = 0;
81433d6423SLionel Sambuc   cbuf[i] = 0;
82433d6423SLionel Sambuc 
83433d6423SLionel Sambuc   while (((ch = fgetc(stream)) == ' ') || (ch == '\t') || (ch == '\n'))
84433d6423SLionel Sambuc 	if (ch == '\n') return(ch);	/* Bypass leading */
85433d6423SLionel Sambuc 					/* Whitespace     */
86433d6423SLionel Sambuc   if (feof(stream)) return(EOF);
87433d6423SLionel Sambuc 
88433d6423SLionel Sambuc   cbuf[i++] = ch;
89433d6423SLionel Sambuc 
90433d6423SLionel Sambuc   while (((ch = fgetc(stream)) != ' ') && (ch != '\t') && (ch != '\n'))
91433d6423SLionel Sambuc 	cbuf[i++] = ch;			/* Get the argument */
92433d6423SLionel Sambuc 
93433d6423SLionel Sambuc   cbuf[i] = 0;
94433d6423SLionel Sambuc   return(ch);
95433d6423SLionel Sambuc }
96433d6423SLionel Sambuc 
97433d6423SLionel Sambuc 
98433d6423SLionel Sambuc #ifdef __STDC__
find(char * sym)99433d6423SLionel Sambuc int find ( char *sym )
100433d6423SLionel Sambuc #else
101433d6423SLionel Sambuc int find(sym)
102433d6423SLionel Sambuc char *sym;
103433d6423SLionel Sambuc #endif
104433d6423SLionel Sambuc {				/* Return DEF if defined else UNDEF */
105433d6423SLionel Sambuc 
106433d6423SLionel Sambuc   deftemp = defptr;
107433d6423SLionel Sambuc   while (deftemp) {			/* Search for the symbol */
108433d6423SLionel Sambuc 	if (!strcmp(deftemp->symbol, sym))
109433d6423SLionel Sambuc 		return(deftemp->type);	/* Setting up the type */
110433d6423SLionel Sambuc 	deftemp = deftemp->next;
111433d6423SLionel Sambuc   }
112433d6423SLionel Sambuc   return(0);
113433d6423SLionel Sambuc }
114433d6423SLionel Sambuc 
115433d6423SLionel Sambuc 
116433d6423SLionel Sambuc 
117433d6423SLionel Sambuc #define Define(x,y)	defit(x,y,DEF)
118433d6423SLionel Sambuc #define Undefine(x,y)	defit(x,y,UNDEF)
119433d6423SLionel Sambuc #define Ignore(x,y)	defit(x,y,IGN)
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc #ifdef __STDC__
defit(char * sym,int redef,int type)122433d6423SLionel Sambuc void defit ( char *sym , int redef , int type )
123433d6423SLionel Sambuc #else
124433d6423SLionel Sambuc void defit(sym, redef, type)	/* Add symbol to the define list */
125433d6423SLionel Sambuc char *sym;
126433d6423SLionel Sambuc char redef;			/* Mode: MUTABLE etc      */
127433d6423SLionel Sambuc char type;			/* Type: DEF, UNDEF, IGN  */
128433d6423SLionel Sambuc #endif
129433d6423SLionel Sambuc {
130433d6423SLionel Sambuc   struct DEFINE *temp;
131433d6423SLionel Sambuc   char c;
132433d6423SLionel Sambuc 
133433d6423SLionel Sambuc   c = find(sym);		/* First try finding the symbol */
134433d6423SLionel Sambuc   if (type == c) return;	/* Return if already declared */
135433d6423SLionel Sambuc   if (c) {			/* We have to move if from DEF <-> UNDEF */
136433d6423SLionel Sambuc 	if (deftemp->redef == IMMUTABLE)
137433d6423SLionel Sambuc 		return;
138433d6423SLionel Sambuc 	else {
139433d6423SLionel Sambuc 		deftemp->type = type;
140433d6423SLionel Sambuc 		deftemp->redef = redef;
141433d6423SLionel Sambuc 	}
142433d6423SLionel Sambuc   } else {			/* We must create a struct & add it */
143433d6423SLionel Sambuc 				/* Malloc room for the struct */
144433d6423SLionel Sambuc 	if ((temp = (struct DEFINE *)malloc(sizeof(struct DEFINE))) == NULL) {
145433d6423SLionel Sambuc 		(void)fprintf(stderr, "ifdef: could not malloc\n");
146433d6423SLionel Sambuc 		exit(1);
147433d6423SLionel Sambuc 	}
148433d6423SLionel Sambuc 
149433d6423SLionel Sambuc 					/* Malloc room for symbol */
150433d6423SLionel Sambuc 	if ((temp->symbol = (char *)malloc(strlen(sym) + 1)) == NULL) {
151433d6423SLionel Sambuc 		(void)fprintf(stderr, "ifdef: could not malloc\n");
152433d6423SLionel Sambuc 		exit(1);
153433d6423SLionel Sambuc 	}
154433d6423SLionel Sambuc 	(void)strcpy(temp->symbol, sym); /* Copy symbol into struct      */
155433d6423SLionel Sambuc 	temp->redef = redef;		/* and set its redef mode too   */
156433d6423SLionel Sambuc 	temp->type = type;		/* as well as making it defined */
157433d6423SLionel Sambuc 
158433d6423SLionel Sambuc 
159433d6423SLionel Sambuc 					/* Now add to the SLL */
160433d6423SLionel Sambuc 	if (defptr == NULL)		/* If first node set  */
161433d6423SLionel Sambuc 		defptr = temp;		/* the pointers to it */
162433d6423SLionel Sambuc 	else
163433d6423SLionel Sambuc 		defend->next = temp;	/* else add it to the */
164433d6423SLionel Sambuc 	defend = temp;			/* end of the list.   */
165433d6423SLionel Sambuc   }
166433d6423SLionel Sambuc }
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc 
169433d6423SLionel Sambuc 
170433d6423SLionel Sambuc #ifdef __STDC__
stop(void)171433d6423SLionel Sambuc void stop ( void )
172433d6423SLionel Sambuc #else
173433d6423SLionel Sambuc void stop()
174433d6423SLionel Sambuc #endif
175433d6423SLionel Sambuc {				/* Stop: Tidy up at EOF */
176433d6423SLionel Sambuc   if (table) printtable();
177433d6423SLionel Sambuc   (void)fclose(zin);
178433d6423SLionel Sambuc   exit(0);
179433d6423SLionel Sambuc }
180433d6423SLionel Sambuc 
181433d6423SLionel Sambuc #define Goto	{ line++; if (ch!='\n') gotoeoln(); }
182433d6423SLionel Sambuc #define Print	{ line++; if (ch!='\n') prteoln();  }
183433d6423SLionel Sambuc 
184433d6423SLionel Sambuc #ifdef __STDC__
gotoeoln(void)185433d6423SLionel Sambuc void gotoeoln ( void )
186433d6423SLionel Sambuc #else
187433d6423SLionel Sambuc void gotoeoln()			/* Go to the end of the line */
188433d6423SLionel Sambuc #endif
189433d6423SLionel Sambuc {
190433d6423SLionel Sambuc   int ch;
191433d6423SLionel Sambuc   while ((ch = fgetc(zin)) != '\n')
192433d6423SLionel Sambuc 	if (ch == EOF) stop();
193433d6423SLionel Sambuc }
194433d6423SLionel Sambuc 
195433d6423SLionel Sambuc 
196433d6423SLionel Sambuc #ifdef __STDC__
prteoln(void)197433d6423SLionel Sambuc void prteoln ( void )
198433d6423SLionel Sambuc #else
199433d6423SLionel Sambuc void prteoln()			/* Print to the end of the line */
200433d6423SLionel Sambuc #endif
201433d6423SLionel Sambuc {
202433d6423SLionel Sambuc   int ch;
203433d6423SLionel Sambuc   while ((ch = fgetc(zin)) != '\n')
204433d6423SLionel Sambuc 	if (ch == EOF)
205433d6423SLionel Sambuc 		stop();
206433d6423SLionel Sambuc 	else
207433d6423SLionel Sambuc 		(void)putchar(ch);
208433d6423SLionel Sambuc   (void)putchar('\n');
209433d6423SLionel Sambuc }
210433d6423SLionel Sambuc 
211433d6423SLionel Sambuc 
212433d6423SLionel Sambuc #ifdef __STDC__
printtable(void)213433d6423SLionel Sambuc void printtable ( void )
214433d6423SLionel Sambuc #else
215433d6423SLionel Sambuc void printtable()		/* Print the defines in the SLL */
216433d6423SLionel Sambuc #endif
217433d6423SLionel Sambuc {
218433d6423SLionel Sambuc   struct DEFINE *temp;
219433d6423SLionel Sambuc 
220433d6423SLionel Sambuc   (void)printf("Defined\n\n");
221433d6423SLionel Sambuc 
222433d6423SLionel Sambuc   temp = defptr;
223433d6423SLionel Sambuc   while (temp) {
224433d6423SLionel Sambuc 	if (temp->type == DEF) (void)printf("%s\n", temp->symbol);
225433d6423SLionel Sambuc 	temp = temp->next;
226433d6423SLionel Sambuc   }
227433d6423SLionel Sambuc 
228433d6423SLionel Sambuc   (void)printf("\n\nUndefined\n\n");
229433d6423SLionel Sambuc 
230433d6423SLionel Sambuc   temp = defptr;
231433d6423SLionel Sambuc   while (temp) {
232433d6423SLionel Sambuc 	if (temp->type == UNDEF) (void)printf("%s\n", temp->symbol);
233433d6423SLionel Sambuc 	temp = temp->next;
234433d6423SLionel Sambuc   }
235433d6423SLionel Sambuc }
236433d6423SLionel Sambuc 
237433d6423SLionel Sambuc #ifdef __STDC__
getendif(void)238433d6423SLionel Sambuc char getendif ( void )
239433d6423SLionel Sambuc #else
240433d6423SLionel Sambuc char getendif()
241433d6423SLionel Sambuc #endif
242433d6423SLionel Sambuc {				/* Find matching endif when ignoring */
243433d6423SLionel Sambuc   char word[80];		/* Buffer for symbols */
244433d6423SLionel Sambuc   int ch;
245433d6423SLionel Sambuc   int skip;			/* Number of skipped #ifdefs */
246433d6423SLionel Sambuc 
247433d6423SLionel Sambuc   skip = 1;
248433d6423SLionel Sambuc 
249433d6423SLionel Sambuc   while (1) {
250433d6423SLionel Sambuc 			/* Scan through the file looking for starting lines */
251433d6423SLionel Sambuc 	if ((ch = fgetc(zin)) == EOF)
252433d6423SLionel Sambuc 		stop();		/* Get first char on the line */
253433d6423SLionel Sambuc 	if (ch != '#') {	/* If not a # ignore line     */
254433d6423SLionel Sambuc 		(void)putchar(ch);
255433d6423SLionel Sambuc 		Print;
256433d6423SLionel Sambuc 		continue;
257433d6423SLionel Sambuc 	}
258433d6423SLionel Sambuc 	ch = fgetarg(zin, word);	/* Get the word after the # */
259433d6423SLionel Sambuc 
260433d6423SLionel Sambuc 	if (!strcmp(word, "ifdef") || !strcmp(word, "ifndef")) skip++;
261433d6423SLionel Sambuc 						/* Keep track of ifdefs & */
262433d6423SLionel Sambuc 	if (!strcmp(word, "endif")) skip--;	/* endifs		  */
263433d6423SLionel Sambuc 
264433d6423SLionel Sambuc 	(void)printf("#%s%c", word, ch);	/* Print the line out 	  */
265433d6423SLionel Sambuc 	Print;
266433d6423SLionel Sambuc 	if (!skip) return('\n');	/* If matching endif, return */
267433d6423SLionel Sambuc   }
268433d6423SLionel Sambuc }
269433d6423SLionel Sambuc 
270433d6423SLionel Sambuc 
271433d6423SLionel Sambuc #ifdef __STDC__
gettable(void)272433d6423SLionel Sambuc void gettable ( void )
273433d6423SLionel Sambuc #else
274433d6423SLionel Sambuc void gettable()			/* Get & print a table of defines etc.  */
275433d6423SLionel Sambuc #endif
276433d6423SLionel Sambuc {
277433d6423SLionel Sambuc 
278433d6423SLionel Sambuc   char word[80];		/* Buffer for symbols */
279433d6423SLionel Sambuc   int ch;
280433d6423SLionel Sambuc 
281433d6423SLionel Sambuc   while (1) {
282433d6423SLionel Sambuc 			/* Scan through the file looking for starting lines */
283433d6423SLionel Sambuc 	if ((ch = fgetc(zin)) == EOF)
284433d6423SLionel Sambuc 		stop();		/* Get first char on the line */
285433d6423SLionel Sambuc 	if (ch != '#') {	/* If not a # ignore line     */
286433d6423SLionel Sambuc 		Goto;
287433d6423SLionel Sambuc 		continue;
288433d6423SLionel Sambuc 	}
289433d6423SLionel Sambuc 	ch = fgetarg(zin, word);	/* Get the word after the # */
290433d6423SLionel Sambuc 
291433d6423SLionel Sambuc 	if (!strcmp(word, "define")) {		/* Define: Define the */
292433d6423SLionel Sambuc 		ch = fgetarg(zin, word);	/* symbol, and goto   */
293433d6423SLionel Sambuc 		Define(word, MUTABLE);		/* the end of line    */
294433d6423SLionel Sambuc 		Goto;
295433d6423SLionel Sambuc 		continue;
296433d6423SLionel Sambuc 	}
297433d6423SLionel Sambuc 	if (!strcmp(word, "undef")) {		/* Undef: Undefine the */
298433d6423SLionel Sambuc 		ch = fgetarg(zin, word);	/* symbol, and goto    */
299433d6423SLionel Sambuc 		Undefine(word, MUTABLE);	/* the end of line     */
300433d6423SLionel Sambuc 		Goto;
301433d6423SLionel Sambuc 		continue;
302433d6423SLionel Sambuc 	}					/* Ifdef:            */
303433d6423SLionel Sambuc 	if (!strcmp(word, "ifdef") || !strcmp(word, "ifndef")) {
304433d6423SLionel Sambuc 		ch = fgetarg(zin, word);	/* Get the symbol */
305433d6423SLionel Sambuc 		if (find(word) != DEF)
306433d6423SLionel Sambuc 			Undefine(word, MUTABLE);	/* undefine it */
307433d6423SLionel Sambuc 		Goto;
308433d6423SLionel Sambuc 		continue;
309433d6423SLionel Sambuc 	}
310433d6423SLionel Sambuc 	Goto;				/* else ignore the line */
311433d6423SLionel Sambuc   }
312433d6423SLionel Sambuc }
313433d6423SLionel Sambuc 
314433d6423SLionel Sambuc 
315433d6423SLionel Sambuc 
316433d6423SLionel Sambuc #ifdef __STDC__
parse(void)317433d6423SLionel Sambuc void parse ( void )
318433d6423SLionel Sambuc #else
319433d6423SLionel Sambuc void parse()
320433d6423SLionel Sambuc #endif
321433d6423SLionel Sambuc {				/* Parse & remove ifdefs from C source */
322433d6423SLionel Sambuc   char word[80];		/* Buffer for symbols */
323433d6423SLionel Sambuc   int ch;
324433d6423SLionel Sambuc   int proc;			/* Should we be processing this bit?    */
325433d6423SLionel Sambuc   int skip;			/* Number of skipped #ifdefs		 */
326433d6423SLionel Sambuc 
327433d6423SLionel Sambuc   proc = 1;
328433d6423SLionel Sambuc   skip = 0;
329433d6423SLionel Sambuc 
330433d6423SLionel Sambuc   while (1) {
331433d6423SLionel Sambuc 			/* Scan through the file looking for starting lines */
332433d6423SLionel Sambuc 	if ((ch = fgetc(zin)) == EOF)
333433d6423SLionel Sambuc 		stop();		/* Get first char on the line */
334*d0055759SDavid van Moolenbroek 	if (ch != '#') {
335433d6423SLionel Sambuc 		if (proc) {	/* If not # and  we're processing */
336433d6423SLionel Sambuc 			(void)putchar(ch); /* then print the line */
337433d6423SLionel Sambuc 			Print;
338433d6423SLionel Sambuc 			continue;
339433d6423SLionel Sambuc 		} else {
340433d6423SLionel Sambuc 			Goto;	/* else just skip the line  */
341433d6423SLionel Sambuc 			continue;
342433d6423SLionel Sambuc 		}
343*d0055759SDavid van Moolenbroek 	}
344433d6423SLionel Sambuc 
345433d6423SLionel Sambuc 	ch = fgetarg(zin, word);	/* Get the word after the # */
346433d6423SLionel Sambuc 
347433d6423SLionel Sambuc 	if (!strcmp(word, "define") && proc) {	/* Define: Define the */
348433d6423SLionel Sambuc 		ch = fgetarg(zin, word);	/* symbol, and goto   */
349433d6423SLionel Sambuc 		Define(word, MUTABLE);		/* the end of line    */
350433d6423SLionel Sambuc 		(void)printf("#define %s%c", word, ch);
351433d6423SLionel Sambuc 		Print;
352433d6423SLionel Sambuc 		continue;
353433d6423SLionel Sambuc 	}
354433d6423SLionel Sambuc 	if (!strcmp(word, "undef") && proc) {	/* Undef: Undefine the */
355433d6423SLionel Sambuc 		ch = fgetarg(zin, word);	/* symbol, and goto    */
356433d6423SLionel Sambuc 		Undefine(word, MUTABLE);	/* the end of line     */
357433d6423SLionel Sambuc 		(void)printf("#undef %s%c", word, ch);
358433d6423SLionel Sambuc 		Print;
359433d6423SLionel Sambuc 		continue;
360433d6423SLionel Sambuc 	}
361433d6423SLionel Sambuc 	if (!strcmp(word, "if")) {	/* If: we cannot handle these */
362433d6423SLionel Sambuc 		if (!proc)		/* at the moment, so just */
363433d6423SLionel Sambuc 			skip++;		/* treat them as an ignored */
364433d6423SLionel Sambuc 		else {			/* definition */
365433d6423SLionel Sambuc 			(void)printf("#%s%c",word,ch);
366433d6423SLionel Sambuc 			Print;
367433d6423SLionel Sambuc 			ch = getendif();	/* Get matching endif */
368433d6423SLionel Sambuc 			continue;
369433d6423SLionel Sambuc 		     	}
370433d6423SLionel Sambuc 	}
371433d6423SLionel Sambuc 	if (!strcmp(word, "ifdef")) {	/* Ifdef:	     */
372433d6423SLionel Sambuc 		if (!proc)		/* If not processing */
373433d6423SLionel Sambuc 			skip++;		/* skip it           */
374433d6423SLionel Sambuc 		else {
375433d6423SLionel Sambuc 			ch = fgetarg(zin, word); /* Get the symbol */
376433d6423SLionel Sambuc 			switch (find(word)) {
377433d6423SLionel Sambuc 			    case DEF:
378433d6423SLionel Sambuc 				break;
379433d6423SLionel Sambuc 			    case IGN:
380433d6423SLionel Sambuc 				(void)printf("#ifdef %s%c", word, ch);
381433d6423SLionel Sambuc 				Print;
382433d6423SLionel Sambuc 				ch = getendif(); /* Get matching endif */
383433d6423SLionel Sambuc 				break;
384433d6423SLionel Sambuc 						/* If symbol undefined */
385433d6423SLionel Sambuc 			    default:
386433d6423SLionel Sambuc 				Undefine(word, MUTABLE); /* undefine it */
387433d6423SLionel Sambuc 				proc = 0;	/* & stop processing */
388433d6423SLionel Sambuc 			}
389433d6423SLionel Sambuc 		}
390433d6423SLionel Sambuc 		Goto;
391433d6423SLionel Sambuc 		continue;
392433d6423SLionel Sambuc 	}
393433d6423SLionel Sambuc 	if (!strcmp(word, "ifndef")) {
394433d6423SLionel Sambuc 		/* Ifndef: */
395433d6423SLionel Sambuc 		if (!proc)	/* If not processing */
396433d6423SLionel Sambuc 			skip++;	/* skip the line     */
397433d6423SLionel Sambuc 		else {
398433d6423SLionel Sambuc 			ch = fgetarg(zin, word); /* Get the symbol */
399433d6423SLionel Sambuc 			switch (find(word)) {	/* If defined, stop */
400433d6423SLionel Sambuc 			    case DEF:
401433d6423SLionel Sambuc 				proc = 0;	/* processing       */
402433d6423SLionel Sambuc 				break;
403433d6423SLionel Sambuc 			    case IGN:
404433d6423SLionel Sambuc 				(void)printf("#ifdef %s%c", word, ch);
405433d6423SLionel Sambuc 				Print;
406433d6423SLionel Sambuc 				ch = getendif(); /* Get matching endif */
407433d6423SLionel Sambuc 				break;
408433d6423SLionel Sambuc 			}
409433d6423SLionel Sambuc 		}
410433d6423SLionel Sambuc 		Goto;
411433d6423SLionel Sambuc 		continue;
412433d6423SLionel Sambuc 	}
413433d6423SLionel Sambuc 	if (!strcmp(word, "else") && !skip) {	/* Else: Flip processing */
414433d6423SLionel Sambuc 		proc = !proc;
415433d6423SLionel Sambuc 		Goto;
416433d6423SLionel Sambuc 		continue;
417433d6423SLionel Sambuc 	}
418433d6423SLionel Sambuc 	if (!strcmp(word, "endif")) {	/* Endif: If no skipped   */
419433d6423SLionel Sambuc 					/* ifdefs turn processing */
420433d6423SLionel Sambuc 		if (!skip)		/* on, else decrement the */
421433d6423SLionel Sambuc 			proc = 1;	/* number of skips        */
422433d6423SLionel Sambuc 		else
423433d6423SLionel Sambuc 			skip--;
424433d6423SLionel Sambuc 		Goto;
425433d6423SLionel Sambuc 		continue;
426433d6423SLionel Sambuc 	}
427433d6423SLionel Sambuc 		/* The word fails all of the above tests, so if we're */
428433d6423SLionel Sambuc 		/* processing, print the line. */
429433d6423SLionel Sambuc 	if (proc) {
430433d6423SLionel Sambuc 		(void)printf("#%s%c", word, ch);
431433d6423SLionel Sambuc 		Print;
432433d6423SLionel Sambuc 	} else
433433d6423SLionel Sambuc 		Goto;
434433d6423SLionel Sambuc   }
435433d6423SLionel Sambuc }
436433d6423SLionel Sambuc 
437433d6423SLionel Sambuc 
438433d6423SLionel Sambuc #ifdef __STDC__
usage(void)439433d6423SLionel Sambuc void usage ( void )
440433d6423SLionel Sambuc #else
441433d6423SLionel Sambuc void usage()
442433d6423SLionel Sambuc #endif
443433d6423SLionel Sambuc {
444433d6423SLionel Sambuc   (void)fprintf(stderr, "Usage: ifdef [-t] [-Dsymbol] [-dsymbol] [-Usymbol] [-Isymbol] <file>\n");
445433d6423SLionel Sambuc   exit(0);
446433d6423SLionel Sambuc }
447433d6423SLionel Sambuc 
448433d6423SLionel Sambuc 
449433d6423SLionel Sambuc #ifdef __STDC__
main(int argc,char * argv[])450433d6423SLionel Sambuc int main(int argc , char *argv [])
451433d6423SLionel Sambuc #else
452433d6423SLionel Sambuc int main(argc, argv)
453433d6423SLionel Sambuc int argc;
454433d6423SLionel Sambuc char *argv[];
455433d6423SLionel Sambuc #endif
456433d6423SLionel Sambuc {
457433d6423SLionel Sambuc   char sym[80];			/* Temp symbol storage */
458433d6423SLionel Sambuc   int c;
459433d6423SLionel Sambuc 
460433d6423SLionel Sambuc   if (argc == 1) usage();	/* Catch the curious user	 */
461433d6423SLionel Sambuc   while ((c = getopt(argc, argv, "tD:d:U:I:")) != EOF) {
462433d6423SLionel Sambuc 	switch (c) {
463433d6423SLionel Sambuc 	    case 't':
464433d6423SLionel Sambuc 		table = 1;	/* Get the various options */
465433d6423SLionel Sambuc 		break;
466433d6423SLionel Sambuc 
467433d6423SLionel Sambuc 	    case 'd':
468433d6423SLionel Sambuc 		(void)strcpy(sym, optarg);
469433d6423SLionel Sambuc 		Define(sym, MUTABLE);
470433d6423SLionel Sambuc 		break;
471433d6423SLionel Sambuc 
472433d6423SLionel Sambuc 	    case 'D':
473433d6423SLionel Sambuc 		(void)strcpy(sym, optarg);
474433d6423SLionel Sambuc 		Define(sym, IMMUTABLE);
475433d6423SLionel Sambuc 		break;
476433d6423SLionel Sambuc 
477433d6423SLionel Sambuc 	    case 'U':
478433d6423SLionel Sambuc 		(void)strcpy(sym, optarg);
479433d6423SLionel Sambuc 		Undefine(sym, IMMUTABLE);
480433d6423SLionel Sambuc 		break;
481433d6423SLionel Sambuc 
482433d6423SLionel Sambuc 	    case 'I':
483433d6423SLionel Sambuc 		(void)strcpy(sym, optarg);
484433d6423SLionel Sambuc 		Ignore(sym, IMMUTABLE);
485433d6423SLionel Sambuc 		break;
486433d6423SLionel Sambuc 
487433d6423SLionel Sambuc 	    default:	usage();
488433d6423SLionel Sambuc 	}
489433d6423SLionel Sambuc   }
490433d6423SLionel Sambuc 
491433d6423SLionel Sambuc   zin = stdin;		/* If a C file is named */
492433d6423SLionel Sambuc 			/* Open stdin with it */
493433d6423SLionel Sambuc   if (*argv[argc - 1] != '-') {
494433d6423SLionel Sambuc 	(void)fclose(zin);
495433d6423SLionel Sambuc 	if ((zin = fopen(argv[argc - 1], "r")) == NULL) {
496433d6423SLionel Sambuc 		perror("ifdef");
497433d6423SLionel Sambuc 		exit(1);
498433d6423SLionel Sambuc 	}
499433d6423SLionel Sambuc   }
500433d6423SLionel Sambuc   if (table)
501433d6423SLionel Sambuc 	gettable();		/* Either generate a table or    */
502433d6423SLionel Sambuc   else
503433d6423SLionel Sambuc 	parse();		/* parse & replace with the file */
504433d6423SLionel Sambuc   return(0);
505433d6423SLionel Sambuc }
506