Repository navigation
Expand file tree
/
Copy pathifdef.c
More file actions
504 lines (436 loc) · 11.5 KB
/
Copy pathifdef.c
File metadata and controls
504 lines (436 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* ifdef - remove #ifdefs Author: Warren Toomey */
/* Copyright 1989 by Warren Toomey wkt@cs.adfa.oz.au[@uunet.uu.net]
*
* You may freely copy or distribute this code as long as this notice
* remains intact.
*
* You may modify this code, as long as this notice remains intact, and
* you add another notice indicating that the code has been modified.
*
* You may NOT sell this code or in any way profit from this code without
* prior agreement from the author.
*/
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
/* Definition of structures and constants used in ifdef.c */
/* Types of symbols */
#define DEF 1 /* Symbol is defined */
#define UNDEF 2 /* Symbol isn't defined */
#define IGN 3 /* Ignore this symbol unless defined */
/* Redef mode values */
#define MUTABLE 1 /* Symbol can change defined <-> undefined */
#define IMMUTABLE 2 /* Symbol can't change as above */
/* Processing modes */
#define NO 0 /* Don't process */
#define YES 1 /* Process */
/* Ignore (IGN), ignore but process */
struct DEFINE {
char *symbol; /* SLL of defined symbols. The redef */
char type; /* field indicates if this symbol can */
char redef; /* change from defined <-> undefined. */
struct DEFINE *next; /* Type is DEF or UNDEF. */
};
/* Global variables & structures */
FILE *zin; /* Input file for processing */
struct DEFINE *defptr; /* Defined symbols SLL */
struct DEFINE *defend; /* Ptr to last node in defptr */
struct DEFINE *deftemp; /* Ptr to last found node */
int line = 1; /* Current line number */
int table = 0; /* Don't normally want a table */
extern int optind;
extern char *optarg;
/* Prototypes. */
int main(int argc, char **argv);
char fgetarg(FILE *stream, char *cbuf);
int find(char *symd);
void defit(char *sym, int redef, int typed);
void stop(void);
void gotoeoln(void);
void prteoln(void);
void printtable(void);
char getendif(void);
void gettable(void);
void parse(void);
void usage(void);
#ifdef __STDC__
char fgetarg ( FILE *stream , char *cbuf )
#else
char fgetarg(stream, cbuf) /* Get next arg from file into cbuf, */
FILE *stream; /* returning the character that */
char *cbuf; /* terminated it. Cbuf returns 0 */
#endif
{ /* if no arg. EOF is returned if no */
int ch; /* args left in file. */
int i;
i = 0;
cbuf[i] = 0;
while (((ch = fgetc(stream)) == ' ') || (ch == '\t') || (ch == '\n'))
if (ch == '\n') return(ch); /* Bypass leading */
/* Whitespace */
if (feof(stream)) return(EOF);
cbuf[i++] = ch;
while (((ch = fgetc(stream)) != ' ') && (ch != '\t') && (ch != '\n'))
cbuf[i++] = ch; /* Get the argument */
cbuf[i] = 0;
return(ch);
}
#ifdef __STDC__
int find ( char *sym )
#else
int find(sym)
char *sym;
#endif
{ /* Return DEF if defined else UNDEF */
deftemp = defptr;
while (deftemp) { /* Search for the symbol */
if (!strcmp(deftemp->symbol, sym))
return(deftemp->type); /* Setting up the type */
deftemp = deftemp->next;
}
return(0);
}
#define Define(x,y) defit(x,y,DEF)
#define Undefine(x,y) defit(x,y,UNDEF)
#define Ignore(x,y) defit(x,y,IGN)
#ifdef __STDC__
void defit ( char *sym , int redef , int type )
#else
void defit(sym, redef, type) /* Add symbol to the define list */
char *sym;
char redef; /* Mode: MUTABLE etc */
char type; /* Type: DEF, UNDEF, IGN */
#endif
{
struct DEFINE *temp;
char c;
c = find(sym); /* First try finding the symbol */
if (type == c) return; /* Return if already declared */
if (c) { /* We have to move if from DEF <-> UNDEF */
if (deftemp->redef == IMMUTABLE)
return;
else {
deftemp->type = type;
deftemp->redef = redef;
}
} else { /* We must create a struct & add it */
/* Malloc room for the struct */
if ((temp = (struct DEFINE *)malloc(sizeof(struct DEFINE))) == NULL) {
(void)fprintf(stderr, "ifdef: could not malloc\n");
exit(1);
}
/* Malloc room for symbol */
if ((temp->symbol = (char *)malloc(strlen(sym) + 1)) == NULL) {
(void)fprintf(stderr, "ifdef: could not malloc\n");
exit(1);
}
(void)strcpy(temp->symbol, sym); /* Copy symbol into struct */
temp->redef = redef; /* and set its redef mode too */
temp->type = type; /* as well as making it defined */
/* Now add to the SLL */
if (defptr == NULL) /* If first node set */
defptr = temp; /* the pointers to it */
else
defend->next = temp; /* else add it to the */
defend = temp; /* end of the list. */
}
}
#ifdef __STDC__
void stop ( void )
#else
void stop()
#endif
{ /* Stop: Tidy up at EOF */
if (table) printtable();
(void)fclose(zin);
exit(0);
}
#define Goto { line++; if (ch!='\n') gotoeoln(); }
#define Print { line++; if (ch!='\n') prteoln(); }
#ifdef __STDC__
void gotoeoln ( void )
#else
void gotoeoln() /* Go to the end of the line */
#endif
{
int ch;
while ((ch = fgetc(zin)) != '\n')
if (ch == EOF) stop();
}
#ifdef __STDC__
void prteoln ( void )
#else
void prteoln() /* Print to the end of the line */
#endif
{
int ch;
while ((ch = fgetc(zin)) != '\n')
if (ch == EOF)
stop();
else
(void)putchar(ch);
(void)putchar('\n');
}
#ifdef __STDC__
void printtable ( void )
#else
void printtable() /* Print the defines in the SLL */
#endif
{
struct DEFINE *temp;
(void)printf("Defined\n\n");
temp = defptr;
while (temp) {
if (temp->type == DEF) (void)printf("%s\n", temp->symbol);
temp = temp->next;
}
(void)printf("\n\nUndefined\n\n");
temp = defptr;
while (temp) {
if (temp->type == UNDEF) (void)printf("%s\n", temp->symbol);
temp = temp->next;
}
}
#ifdef __STDC__
char getendif ( void )
#else
char getendif()
#endif
{ /* Find matching endif when ignoring */
char word[80]; /* Buffer for symbols */
int ch;
int skip; /* Number of skipped #ifdefs */
skip = 1;
while (1) {
/* Scan through the file looking for starting lines */
if ((ch = fgetc(zin)) == EOF)
stop(); /* Get first char on the line */
if (ch != '#') { /* If not a # ignore line */
(void)putchar(ch);
Print;
continue;
}
ch = fgetarg(zin, word); /* Get the word after the # */
if (!strcmp(word, "ifdef") || !strcmp(word, "ifndef")) skip++;
/* Keep track of ifdefs & */
if (!strcmp(word, "endif")) skip--; /* endifs */
(void)printf("#%s%c", word, ch); /* Print the line out */
Print;
if (!skip) return('\n'); /* If matching endif, return */
}
}
#ifdef __STDC__
void gettable ( void )
#else
void gettable() /* Get & print a table of defines etc. */
#endif
{
char word[80]; /* Buffer for symbols */
int ch;
while (1) {
/* Scan through the file looking for starting lines */
if ((ch = fgetc(zin)) == EOF)
stop(); /* Get first char on the line */
if (ch != '#') { /* If not a # ignore line */
Goto;
continue;
}
ch = fgetarg(zin, word); /* Get the word after the # */
if (!strcmp(word, "define")) { /* Define: Define the */
ch = fgetarg(zin, word); /* symbol, and goto */
Define(word, MUTABLE); /* the end of line */
Goto;
continue;
}
if (!strcmp(word, "undef")) { /* Undef: Undefine the */
ch = fgetarg(zin, word); /* symbol, and goto */
Undefine(word, MUTABLE); /* the end of line */
Goto;
continue;
} /* Ifdef: */
if (!strcmp(word, "ifdef") || !strcmp(word, "ifndef")) {
ch = fgetarg(zin, word); /* Get the symbol */
if (find(word) != DEF)
Undefine(word, MUTABLE); /* undefine it */
Goto;
continue;
}
Goto; /* else ignore the line */
}
}
#ifdef __STDC__
void parse ( void )
#else
void parse()
#endif
{ /* Parse & remove ifdefs from C source */
char word[80]; /* Buffer for symbols */
int ch;
int proc; /* Should we be processing this bit? */
int skip; /* Number of skipped #ifdefs */
proc = 1;
skip = 0;
while (1) {
/* Scan through the file looking for starting lines */
if ((ch = fgetc(zin)) == EOF)
stop(); /* Get first char on the line */
if (ch != '#')
if (proc) { /* If not # and we're processing */
(void)putchar(ch); /* then print the line */
Print;
continue;
} else {
Goto; /* else just skip the line */
continue;
}
ch = fgetarg(zin, word); /* Get the word after the # */
if (!strcmp(word, "define") && proc) { /* Define: Define the */
ch = fgetarg(zin, word); /* symbol, and goto */
Define(word, MUTABLE); /* the end of line */
(void)printf("#define %s%c", word, ch);
Print;
continue;
}
if (!strcmp(word, "undef") && proc) { /* Undef: Undefine the */
ch = fgetarg(zin, word); /* symbol, and goto */
Undefine(word, MUTABLE); /* the end of line */
(void)printf("#undef %s%c", word, ch);
Print;
continue;
}
if (!strcmp(word, "if")) { /* If: we cannot handle these */
if (!proc) /* at the moment, so just */
skip++; /* treat them as an ignored */
else { /* definition */
(void)printf("#%s%c",word,ch);
Print;
ch = getendif(); /* Get matching endif */
continue;
}
}
if (!strcmp(word, "ifdef")) { /* Ifdef: */
if (!proc) /* If not processing */
skip++; /* skip it */
else {
ch = fgetarg(zin, word); /* Get the symbol */
switch (find(word)) {
case DEF:
break;
case IGN:
(void)printf("#ifdef %s%c", word, ch);
Print;
ch = getendif(); /* Get matching endif */
break;
/* If symbol undefined */
default:
Undefine(word, MUTABLE); /* undefine it */
proc = 0; /* & stop processing */
}
}
Goto;
continue;
}
if (!strcmp(word, "ifndef")) {
/* Ifndef: */
if (!proc) /* If not processing */
skip++; /* skip the line */
else {
ch = fgetarg(zin, word); /* Get the symbol */
switch (find(word)) { /* If defined, stop */
case DEF:
proc = 0; /* processing */
break;
case IGN:
(void)printf("#ifdef %s%c", word, ch);
Print;
ch = getendif(); /* Get matching endif */
break;
}
}
Goto;
continue;
}
if (!strcmp(word, "else") && !skip) { /* Else: Flip processing */
proc = !proc;
Goto;
continue;
}
if (!strcmp(word, "endif")) { /* Endif: If no skipped */
/* ifdefs turn processing */
if (!skip) /* on, else decrement the */
proc = 1; /* number of skips */
else
skip--;
Goto;
continue;
}
/* The word fails all of the above tests, so if we're */
/* processing, print the line. */
if (proc) {
(void)printf("#%s%c", word, ch);
Print;
} else
Goto;
}
}
#ifdef __STDC__
void usage ( void )
#else
void usage()
#endif
{
(void)fprintf(stderr, "Usage: ifdef [-t] [-Dsymbol] [-dsymbol] [-Usymbol] [-Isymbol] <file>\n");
exit(0);
}
#ifdef __STDC__
int main(int argc , char *argv [])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
char sym[80]; /* Temp symbol storage */
int c;
if (argc == 1) usage(); /* Catch the curious user */
while ((c = getopt(argc, argv, "tD:d:U:I:")) != EOF) {
switch (c) {
case 't':
table = 1; /* Get the various options */
break;
case 'd':
(void)strcpy(sym, optarg);
Define(sym, MUTABLE);
break;
case 'D':
(void)strcpy(sym, optarg);
Define(sym, IMMUTABLE);
break;
case 'U':
(void)strcpy(sym, optarg);
Undefine(sym, IMMUTABLE);
break;
case 'I':
(void)strcpy(sym, optarg);
Ignore(sym, IMMUTABLE);
break;
default: usage();
}
}
zin = stdin; /* If a C file is named */
/* Open stdin with it */
if (*argv[argc - 1] != '-') {
(void)fclose(zin);
if ((zin = fopen(argv[argc - 1], "r")) == NULL) {
perror("ifdef");
exit(1);
}
}
if (table)
gettable(); /* Either generate a table or */
else
parse(); /* parse & replace with the file */
return(0);
}