-
Notifications
You must be signed in to change notification settings - Fork 24
/
pp.c
129 lines (115 loc) · 2.82 KB
/
pp.c
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
/*--------------------------------------------------------------*/
/* pp.c -- */
/* */
/* This helper program emulates m4 pre-processor behavior. */
/* */
/* As of July 2006, XCircuit no longer uses m4, having replaced */
/* it with "sed" scripts. However, since this C code was */
/* written to bypass the lack of "m4" under Windows, it is */
/* still required to bypass the lack of "sed" under Windows. */
/* The "m4" emulation is maintained. However, the m4 files */
/* have been rewritten to remove the awkward "ifelse" syntax, */
/* which has been replaced with a simpler "<variable_name>" */
/* at the beginning of each variable-dependent line. This */
/* program also handles those situations. */
/*--------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
typedef struct _pattern {
char *pattern;
char *string;
struct _pattern *next;
} pattern;
pattern* parse_args(int* argc, char ***argv)
{
int i, len = 0;
pattern *p = (pattern*)malloc(sizeof(pattern));
p->pattern = "`eval'";
p->string = "eval";
p->next = NULL;
for (i=1; i<(*argc); i++) {
if (strncmp((*argv)[i], "-D", 2) == 0) {
char *c = strchr((*argv)[i], '=');
pattern *new_p;
if (c == NULL) {
printf("Invalid argument: %s\n", (*argv[i]));
exit(-1);
}
new_p = (pattern*)malloc(sizeof(pattern));
new_p->next = p;
p = new_p;
c[0] = '\0';
p->pattern = strdup((*argv)[i]+2);
if (c[1] == '"') {
char *c2;
c += 2;
c2 = strchr(c, '"');
if (c2 != NULL)
c2[0] = '\0';
} else {
char *c2;
c++;
c2 = strchr(c, ' ');
if (c2 != NULL)
c2[0] = '\0';
}
p->string = strdup(c);
/*fprintf(stderr, "%s -> %s\n", p->pattern, p->string);*/
len++;
}
}
*argc -= len+1;
*argv += len+1;
return p;
}
int main(int argc, char **argv)
{
char buffer[4096];
char buffer2[4096];
char *c;
int i;
pattern *patterns, *p;
FILE *fin;
patterns = parse_args(&argc, &argv);
if (argc > 0) {
fin = fopen(argv[0], "r");
if (fin == NULL) {
printf("Unable to open file: %s\n", argv[0]);
exit(-1);
}
}
else
fin = stdin;
while (1) {
if (fgets(buffer, 4096, fin) == 0)
exit(0);
p = patterns;
while (p) {
while ((c = strstr(buffer, p->pattern)) != NULL) {
c[0] = '\0';
/* Handle variable-dependent lines */
if (c == buffer && !strcmp(p->string, "1")) {
strcpy(buffer2, c + strlen(p->pattern) + 1);
strcpy(buffer, buffer2);
}
else if (c == buffer && !strcmp(p->string, "0")) {
p = NULL;
break;
}
else {
strcpy(buffer2, buffer);
strcat(buffer2, p->string);
strcat(buffer2, c + strlen(p->pattern));
strcpy(buffer, buffer2);
}
}
if (p == NULL) break;
p = p->next;
}
if (buffer[0] != '\0')
printf("%s", buffer);
}
if (argc > 0)
fclose(fin);
return 0;
}