-
Notifications
You must be signed in to change notification settings - Fork 0
/
PREPROC.C
383 lines (356 loc) · 14.9 KB
/
PREPROC.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
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
//preproc.c by Reverend Matthew B. Gambrell
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include "vcc.h"
#define LETTER 1
#define DIGIT 2
#define SPECIAL 3
char* TempPtr;
char* DataPtr;
char chr_tablebeta[256];
char tokenbeta[512];
char ParamArray[100][4][40];
int NumIdentifiers;
void ParseWhiteSpaceBeta ()
{ char c;
if(chr_tablebeta[*DataPtr]==LETTER||chr_tablebeta[*DataPtr]==DIGIT||*DataPtr=='@') return;
while (1)
{
while (*DataPtr <= ' ')
{ if (!*DataPtr) return; // EOF reached
DataPtr++;
}
if (DataPtr[0] == '/' && DataPtr[1] == '/') // Skip // comments
{ while (*DataPtr && (*DataPtr != '\n'))
DataPtr++; // Skip to next line
continue;
}
if (DataPtr[0] == '/' && DataPtr[1] == '*') // Skip /* until */
{ while (!(DataPtr[0] == '*' && DataPtr[1] == '/'))
{
DataPtr++;
if (!*DataPtr) return;
}
DataPtr += 2;
continue;
}
break; // end of whitespace
}
}
void InitCharTableBeta()
{ int i;
if (verbose) printf("Building chr_tablebeta[] for preprocessor. \n");
for (i = 0; i < 256; i++) chr_tablebeta[i] = SPECIAL;
for (i = '0'; i <= '9'; i++) chr_tablebeta[i] = DIGIT;
for (i = 'A'; i <= 'Z'; i++) chr_tablebeta[i] = LETTER;
for (i = 'a'; i <= 'z'; i++) chr_tablebeta[i] = LETTER;
chr_tablebeta[10] = 0;
chr_tablebeta[13] = 0;
chr_tablebeta[' '] = 0;
chr_tablebeta['_'] = LETTER;
}
void GetIdentifierBeta ()
{ int i;
memset(tokenbeta,0,41);
i = 0;
while ((chr_tablebeta[*DataPtr] == LETTER) || (chr_tablebeta[*DataPtr] == DIGIT))
{
tokenbeta[i] = *DataPtr;
DataPtr++;
i++;
}
tokenbeta[i] = 0;
}
void GetIdentifierDelta ()
{ int i;
char FoundFirstChar;
memset(tokenbeta,0,41);
FoundFirstChar=0;
i = 0;
while (*DataPtr!='@'&&*DataPtr!=','&&*DataPtr!=')')
{
if((chr_tablebeta[*DataPtr]==LETTER || chr_tablebeta[*DataPtr]==DIGIT)||FoundFirstChar==1)
{
FoundFirstChar=1;
tokenbeta[i] = *DataPtr;
i++;
}
DataPtr++;
}
tokenbeta[i] = 0;
if(*DataPtr==')') DataPtr--;
}
void GetIdentifierGamma ()
{ int i;
memset(tokenbeta,0,512);
i = 0;
DataPtr--;
while(*DataPtr!='('&&*DataPtr!='@')
{
ParseWhiteSpaceBeta();
DataPtr++;
}
if(*DataPtr=='@') goto NoParams;
DataPtr++;
while(*DataPtr!=')')
{
GetIdentifierDelta();
memcpy(&ParamArray[NumIdentifiers-1][i][0],tokenbeta,40);
i++;
DataPtr++;
}
while(*DataPtr!='@')
{
ParseWhiteSpaceBeta();
DataPtr++;
}
NoParams:
DataPtr++;
i=0;
while (*DataPtr!='@')
{
tokenbeta[i] = *DataPtr;
DataPtr++;
i++;
}
tokenbeta[i] = 0;
DataPtr++;
}
void FirstPass()
{
FILE* inf; //all purpose input file
FILE* outf; //all purpose output file
char IdentifierArray[100][40]; //list of macro names
char IncludeString[512]; //filename in #include directives
char errstring[512]; //used to construct an error string
char ExpansionArray[100][512]; //macro expansion code
/*The 512 element is the max number of bytes of expansion code.
a bit too constrictive? Maybe increase it.*/
char ch; //byte variable used for file copying
char TempParam[4][40]; //param list used for macro expansion
char* IncludeSpot; //keeps track of the latest byte before an
//#include directive
char* WorkingSpot; //misc ptr used in #include code
char* FinIncSpot; //ptr to the end of the current actual
//#include text i nthe src file
char tc[41];
char tc2[512];
char gross[50];
char* MagicNumber;
int x,z,i,bob;
int x2,z2;
char* IRemember;
char SehrGut;
//PostStartupFilesBeta();
memset(ParamArray,0,sizeof(ParamArray));
DataPtr=source;
NumIdentifiers=0;
InitCharTableBeta();
MagicNumber=DataPtr;
IncludeSpot=source;
outf=fopen("$$tmep$$.ma_","wb");
while(*DataPtr!=0)
{
fwrite(DataPtr,1,1,outf);
DataPtr++;
}
fclose(outf);
DataPtr=source;
while(*DataPtr!=0)
{
if(*DataPtr=='#')
{
DataPtr++;
if(*DataPtr=='D'||*DataPtr=='d')
{
DataPtr++;
if(*DataPtr=='E'||*DataPtr=='e')
{
DataPtr++;
if(*DataPtr=='F'||*DataPtr=='f')
{
DataPtr++;
if(*DataPtr=='I'||*DataPtr=='i')
{
DataPtr++;
if(*DataPtr=='N'||*DataPtr=='n')
{
DataPtr++;
if(*DataPtr=='E'||*DataPtr=='e')
{
DataPtr++;
NumIdentifiers++;
ParseWhiteSpaceBeta();
GetIdentifierBeta();
memcpy(&IdentifierArray[NumIdentifiers-1][0],tokenbeta,40);
if(verbose) printf("Found macro identifier %s\n",IdentifierArray[NumIdentifiers-1]);
GetIdentifierGamma();
memcpy(&ExpansionArray[NumIdentifiers-1][0],tokenbeta,512);
if(verbose) printf("Expands to: %s\n",ExpansionArray[NumIdentifiers-1]);
if(verbose) printf("params: %s%s%s%s\n",ParamArray[NumIdentifiers-1][0],ParamArray[NumIdentifiers-1][1],ParamArray[NumIdentifiers-1][2],ParamArray[NumIdentifiers-1][3]);
MagicNumber=DataPtr;
}
}
}
}
}
}
else
if(*DataPtr=='I'||*DataPtr=='i')
{
DataPtr++;
if(*DataPtr=='N'||*DataPtr=='n')
{
DataPtr++;
if(*DataPtr=='C'||*DataPtr=='c')
{
DataPtr++;
if(*DataPtr=='L'||*DataPtr=='l')
{
DataPtr++;
if(*DataPtr=='U'||*DataPtr=='u')
{
DataPtr++;
if(*DataPtr=='D'||*DataPtr=='d')
{
DataPtr++;
if(*DataPtr=='E'||*DataPtr=='e')
{
DataPtr++;
ParseWhiteSpaceBeta();
x=0;
while(chr_tablebeta[*DataPtr]==LETTER||chr_tablebeta[*DataPtr]==DIGIT||*DataPtr=='.')
{
IncludeString[x]=*DataPtr;
x++;
DataPtr++;
}
IncludeString[x]=0;
if (!quiet) printf("Including file %s\n",IncludeString);
if(!(inf=fopen(IncludeString,"rb")))
{
sprintf(errstring,"Include File %s not found",IncludeString);
err(errstring);
}
outf=fopen("$$tmep$$.ma_","wb");
FinIncSpot=DataPtr;
WorkingSpot=source;
while(WorkingSpot!=IncludeSpot)
{
fwrite(WorkingSpot,1,1,outf);
WorkingSpot++;
}
while(fread(&ch,1,1,inf)!=0)
{
fwrite(&ch,1,1,outf);
// WorkingSpot++;
}
WorkingSpot=FinIncSpot;
while(*WorkingSpot!=0)
{
fwrite(WorkingSpot,1,1,outf);
WorkingSpot++;
}
fclose(inf);
fclose(outf);
PostStartupFilesBeta();
DataPtr=source-1;
memset(IdentifierArray,0,sizeof(IdentifierArray));
memset(ExpansionArray,0,sizeof(ExpansionArray));
NumIdentifiers=0;
memset(ParamArray,0,sizeof(ParamArray));
}
}
}
}
}
}
}
}
DataPtr++;
IncludeSpot=DataPtr;
}
//now the tables of identifiers/expansions are set up.
//next we tear through the file finding/replacing
PostStartupFilesBeta();
if (verbose) printf("Expanding macros...\n");
outf=fopen("$$tmep$$.ma_","wb");
DataPtr=MagicNumber;
while(*DataPtr!=0)
{
IRemember=DataPtr;
z=0;
while(chr_tablebeta[*DataPtr]==LETTER || chr_tablebeta[*DataPtr]==DIGIT)
{
tc[z]=*DataPtr;
z++;
DataPtr++;
}
tc[z]=0;
SehrGut=0;
for(x=0; x<NumIdentifiers; x++)
if(strcmp(tc,IdentifierArray[x])==0)
{
TempPtr=ExpansionArray[x];
i=0;
DataPtr++;
while(*DataPtr!=')')
{
GetIdentifierDelta();
memcpy(&TempParam[i][0],tokenbeta,40);
i++;
DataPtr++;
}
tc2[0]=0;
i=0;
while(*TempPtr!=0)
{
if(chr_tablebeta[*TempPtr]==LETTER || chr_tablebeta[*TempPtr]==DIGIT)
{
tc2[i]=*TempPtr;
i++;
tc2[i]=0;
}
else
{
fwrite(tc2,strlen(tc2),1,outf);
fwrite(TempPtr,1,1,outf);
i=0;
tc2[0]=0;
}
for(x2=0; x2<4; x2++)
{
if(strcmp(tc2,ParamArray[x][x2])==0&&tc2[0]!=0)
{
fwrite(TempParam[x2],strlen(TempParam[x2]),1,outf);
i=0;
tc2[i]=0;
}
}
TempPtr++;
}
SehrGut=1;
DataPtr;
}
if(SehrGut==0)
{
fwrite(IRemember,1,1,outf);
DataPtr=IRemember;
}
DataPtr++;
}
fclose(outf);
if (verbose) printf("Preprocessing completed successfully\n");
if (verbose) printf("Compiling scripts... \n");
}
PostStartupFilesBeta()
{
FILE* inf;
if (!( inf = fopen ("$$tmep$$.ma_", "rb")))
{ if (!quiet) printf ("*error* Could not open input file. \n");
exit (-1); }
memset (source, 0, 100000);
fread (source, 1, 100000, inf);
fclose (inf);
}