-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.c
362 lines (325 loc) · 7.06 KB
/
app.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
/* This is the Assembler Pre-Processor
Copyright (C) 1987 Free Software Foundation, Inc.
This file is part of GAS, the GNU Assembler.
GAS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GAS; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/* App, the assembler pre-processor. This pre-processor strips out excess
spaces, turns single-quoted characters into a decimal constant, and turns
# <number> <filename> into a .line <number>;.file <filename> pair.
This needs better error-handling.
*/
#include <stdio.h>
#ifdef USG
#define bzero(s,n) memset(s,0,n)
#endif
static char lex [256];
static char symbol_chars[] =
"$._ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
extern char comment_chars[];
extern char line_comment_chars[];
#define LEX_IS_SYMBOL_COMPONENT (1)
#define LEX_IS_WHITESPACE (2)
#define LEX_IS_LINE_SEPERATOR (4)
#define LEX_IS_COMMENT_START (8) /* JF added these two */
#define LEX_IS_LINE_COMMENT_START (16)
#define IS_SYMBOL_COMPONENT(c) (lex [c] & LEX_IS_SYMBOL_COMPONENT)
#define IS_WHITESPACE(c) (lex [c] & LEX_IS_WHITESPACE)
#define IS_LINE_SEPERATOR(c) (lex [c] & LEX_IS_LINE_SEPERATOR)
#define IS_COMMENT(c) (lex [c] & LEX_IS_COMMENT_START)
#define IS_LINE_COMMENT(c) (lex [c] & LEX_IS_LINE_COMMENT_START)
void
do_scrub_begin()
{
char *p;
bzero (lex, sizeof(lex)); /* Trust NOBODY! */
lex [' '] |= LEX_IS_WHITESPACE;
lex ['\t'] |= LEX_IS_WHITESPACE;
for (p =symbol_chars;*p;++p)
lex [*p] |= LEX_IS_SYMBOL_COMPONENT;
lex ['\n'] |= LEX_IS_LINE_SEPERATOR;
#ifdef DONTDEF
lex [':'] |= LEX_IS_LINE_SEPERATOR;
#endif
lex [';'] |= LEX_IS_LINE_SEPERATOR;
for (p=comment_chars;*p;p++)
lex[*p] |= LEX_IS_COMMENT_START;
for (p=line_comment_chars;*p;p++)
lex[*p] |= LEX_IS_LINE_COMMENT_START;
}
FILE *scrub_file;
int
scrub_from_file()
{
return getc(scrub_file);
}
void
scrub_to_file(ch)
int ch;
{
ungetc(ch,scrub_file);
}
char *scrub_string;
char *scrub_last_string;
int
scrub_from_string()
{
return scrub_string == scrub_last_string ? EOF : *scrub_string++;
}
void
scrub_to_string(ch)
int ch;
{
*--scrub_string=ch;
}
int
do_scrub_next_char(get,unget)
int (*get)();
void (*unget)();
/* FILE *fp; */
{
/* State 0: beginning of normal line
1: After first whitespace on normal line (flush more white)
2: After first non-white on normal line (keep 1white)
3: after second white on normal line (flush white)
4: after putting out a .line, put out digits
5: parsing a string, then go to old-state
6: putting out \ escape in a "d string.
-1: output string in out_string and go to the state in old_state
-2: flush text until a '*' '/' is seen, then go to tate old_state
*/
static state;
static old_state;
static char *out_string;
static char out_buf[20];
static add_newlines = 0;
int ch;
if(state==-1) {
ch= *out_string++;
if(*out_string==0) {
state=old_state;
old_state=3;
}
return ch;
}
if(state==-2) {
do ch=(*get)();
while(ch!=EOF && ch!='\n' && (ch!='*' || (*get)()!='/'));
if(ch=='\n' || ch==EOF)
return ch;
else {
state=old_state;
return ' ';
}
}
if(state==4) {
ch=(*get)();
if(ch==EOF || (ch>='0' && ch<='9'))
return ch;
else {
while(ch!=EOF && IS_WHITESPACE(ch))
ch=(*get)();
if(ch=='"') {
(*unget)(ch);
out_string="; .file ";
old_state=3;
state= -1;
return *out_string++;
} else {
while(ch!=EOF && ch!='\n')
ch=(*get)();
return ch;
}
}
}
if(state==5) {
ch=(*get)();
if(ch=='"') {
state=old_state;
return '"';
} else if(ch=='\\') {
state=6;
return ch;
} else if(ch==EOF) {
as_warn("End of file in string: inserted '\"'");
state=old_state;
(*unget)('\n');
return '"';
} else {
return ch;
}
}
if(state==6) {
state=5;
ch=(*get)();
switch(ch) {
/* This is neet. Turn "string
more string" into "string\n more string"
*/
case '\n':
(*unget)('n');
add_newlines++;
return '\\';
case '"':
case '\\':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
break;
default:
as_warn("Unknown escape '\\%c' in string",ch);
break;
case EOF:
as_warn("End of file in string: '\"' inserted");
return '"';
}
return ch;
}
flushchar:
ch=(*get)();
switch(ch) {
case ' ':
case '\t':
do ch=(*get)();
while(ch!=EOF && IS_WHITESPACE(ch));
if(ch==EOF)
return ch;
if(IS_COMMENT(ch) || (state==0 && IS_LINE_COMMENT(ch)) || ch=='/' || IS_LINE_SEPERATOR(ch)) {
(*unget)(ch);
goto flushchar;
}
(*unget)(ch);
if(state==0 || state==2) {
state++;
return ' ';
} else goto flushchar;
case '/':
ch=(*get)();
if(ch=='*') {
do {
ch=(*get)();
if(ch=='\n')
add_newlines++;
} while(ch!=EOF && (ch!='*' || (*get)()!='/'));
if(ch==EOF)
as_warn("End of file in '/' '*' string");
(*unget)(' ');
goto flushchar;
} else {
if(IS_COMMENT('/') || (state==0 && IS_LINE_COMMENT('/'))) {
ch='/';
goto deal_misc;
}
if(ch!=EOF)
(*unget)(ch);
return '/';
}
break;
case '"':
old_state=state;
state=5;
return '"';
break;
case '\'':
ch=(*get)();
if(ch==EOF) {
as_warn("End-of-file after a '");
ch=0;
}
sprintf(out_buf,"(%d)",ch&0xff);
old_state=state;
state= -1;
out_string=out_buf;
return *out_string++;
case ':':
if(state!=3)
state=0;
return ch;
case '\n':
if(add_newlines) {
--add_newlines;
(*unget)(ch);
}
case ';':
state=0;
return ch;
default:
deal_misc:
if(state==0 && IS_LINE_COMMENT(ch)) {
do ch=(*get)();
while(ch!=EOF && IS_WHITESPACE(ch));
if(ch==EOF) {
as_warn("Unexpected EOF in comment");
return '\n';
}
if(ch<'0' || ch>'9') {
do ch=(*get)();
while(ch!=EOF && ch!='\n');
if(ch==EOF)
as_warn("Unexpected EOF in Comment");
state=0;
return '\n';
}
(*unget)(ch);
old_state=4;
state= -1;
out_string=".line ";
return *out_string++;
} else if(IS_COMMENT(ch)) {
do ch=(*get)();
while(ch!=EOF && ch!='\n');
if(ch==EOF)
as_warn("Unexpected EOF in comment");
state=0;
return '\n';
} else if(state==0) {
state=2;
return ch;
} else if(state==1) {
state=2;
return ch;
} else {
return ch;
}
case EOF:
if(state==0)
return ch;
as_warn("End-of-File not at end of a line");
}
return -1;
}
#ifdef TEST
char comment_chars[] = "|";
char line_comment_chars[] = "#";
main()
{
int ch;
app_begin();
while((ch=do_scrub_next_char(stdin))!=EOF)
putc(ch,stdout);
}
as_warn(str)
char *str;
{
fputs(str,stderr);
putc('\n',stderr);
}
#endif