-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpp.c
235 lines (214 loc) · 5.27 KB
/
vpp.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
/* Copyright (C) 1996 Himanshu M. Thaker
This file is part of vpp.
Vpp is distributed in the hope that it will be useful,
but without any warranty. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.
Everyone is granted permission to copy, modify and redistribute
vpp, but only under the conditions described in the
document "vpp copying permission notice". An exact copy
of the document is supposed to have been given to you along with
vpp so that you can know how you may redistribute it all.
It should be in a file named COPYING. Among other things, the
copyright notice and this notice must be preserved on all copies. */
/*
* Program : vpp
* Author : Himanshu M. Thaker
* Date : Apr. 16, 1995
* Description :
*/
#include "common.h"
#include "yacc_stuff.h"
#include "proto.h"
#include <string.h>
#include <stdio.h>
struct vpp_filename
{
struct vpp_filename *next;
char *name;
};
struct vpp_filename *vpp_incdir;
int output_quite;
int
main(int argc, char *argv[])
{
int i, arglen;
struct vpp_filename *t, *filehead, *filecurr, *idcurr;
#ifdef YYDEBUG
yydebug = TRUE;
#endif
/* Default to old behavior. */
output_line_directive = FALSE;
output_ifdef_directive = TRUE;
filehead = filecurr = idcurr = NULL;
for (i = 1; i < argc; ++i)
{
arglen = strlen(argv[i]);
if (arglen >= 8 && !strncmp(argv[i],"+incdir+",8))
{
if (arglen == 8)
{
/* Perhaps we don't even need to notify user. */
fprintf(stderr, "** No path for +incdir+ in argument %d.\n", i);
}
else
{
char *lname, *tok;
lname = (char *)NMALLOC(arglen-8+1, char);
strcpy(lname, argv[i]+8);
tok = strtok(lname, "+");
while (tok)
{
int toklen = strlen(tok);
t = CALLOC(1, struct vpp_filename);
t->name = NMALLOC(toklen+1, char);
strcpy(t->name, tok);
if (!vpp_incdir)
vpp_incdir = t;
else
idcurr->next = t;
idcurr = t;
tok = strtok(NULL, "+");
}
free(lname);
}
}
else if (arglen >= 2 && !strncmp(argv[i], "-D", 2))
{
if (arglen == 2)
{
/* Perhaps we don't even need to notify user. */
fprintf(stderr, "** No define for -D in argument %d.\n", i);
}
else
{
char *dname, *pnt;
dname = NMALLOC(arglen+8, char);
sprintf(dname, "`define %s", argv[i]+2);
pnt = dname+8;
while (*pnt)
{
if (*pnt == '=')
{
*pnt = ' ';
break;
}
pnt++;
}
store_define(dname);
free(dname);
}
}
else if (arglen >= 2 && !strncmp(argv[i], "-E", 2))
{
/* Enable C style preprocessing of ifdef and friends. */
output_ifdef_directive = FALSE;
}
else if (arglen >= 2 && !strncmp(argv[i], "-L", 2))
{
/* Write out `line directives for debugging. */
output_line_directive = TRUE;
}
else if (arglen >= 2 && !strncmp(argv[i], "-h", 2))
{
printf("syntax: %s [options] filename\n", argv[0]);
printf("options:\n");
printf(" +incdir+...+ Search directory for `include files.\n");
printf(" -D Define macro.\n");
printf(" -E Perform C style preprocessing.\n");
printf(" -L Output `line directive.\n");
printf(" -h Print help message and exit.\n");
printf(" -q Suppress status message.\n");
return 0;
}
else if (arglen >= 2 && !strncmp(argv[i], "-q", 2))
{
/* Quite, take out unnecessary output. */
output_quite = TRUE;
}
else /* filename only.. */
{
t = CALLOC(1,struct vpp_filename);
t->name = NMALLOC(strlen(argv[i])+1, char);
strcpy(t->name, argv[i]);
if (!filehead)
filehead = t;
else
filecurr->next = t;
filecurr = t;
}
}
/*
* check to see if the file can be opened for reading...
*/
if (!filehead)
{
yyin = stdin;
current_file = NMALLOC(strlen("<stdin>")+1, char);
strcpy(current_file, "<stdin>");
nl_count = 1;
if (!output_quite)
fprintf(stderr, "Preprocessing '%s'\n", current_file);
do_comment_count(TRUE, nl_count);
yyparse();
fclose(yyin);
free(current_file);
}
else
{
filecurr = filehead;
while (filecurr)
{
yyin = fopen(filecurr->name, "r");
if (yyin == NULL)
{
fprintf(stderr, "** %s: open failed\n", filecurr->name);
++yyerror_count;
}
else
{
current_file = NMALLOC(strlen(filecurr->name)+1, char);
strcpy(current_file, filecurr->name);
nl_count = 1;
if (!output_quite) {
fprintf(stderr, "vpp 1.2.0, see 'https://github.com/balanx/vbpp'\n");
fprintf(stderr, "Preprocessing '%s'\n", current_file);
}
do_comment_count(TRUE, nl_count);
yyparse();
fclose(yyin);
free(current_file);
}
t = filecurr;
filecurr = filecurr->next;
free(t->name);
free(t);
}
filehead = NULL;
}
return yyerror_count;
}
FILE *
incdir_fopen(char *name)
{
int len;
struct vpp_filename *l;
char *path;
FILE *fp = NULL;
len = strlen(name);
l = vpp_incdir;
while (l)
{
path = (char *)malloc(strlen(l->name) + 1 + len + 1);
sprintf(path, "%s/%s", l->name, name);
fp = fopen(path, "r");
free(path);
if (fp)
break;
l = l->next;
}
if (!fp)
fp = fopen(name, "r");
return(fp);
}