-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinput.c
431 lines (366 loc) · 8.08 KB
/
input.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "defs.h"
#include "externs.h"
#include "protos.h"
#define INITIAL_PATH_SIZE 64
#define INCREMENT_BASE 16
#define INCREMENT_BASE_MASK 15
int infile_error;
int infile_num;
struct t_input_info input_file[8];
static char *incpath = NULL;
static int *str_offset = NULL;
static int remaining = 0;
static int incpathSize = 0;
static int str_offsetCount = 0;
static int incpathCount = 0;
/* ----
* void cleanup_path()
* ----
* clean up allocated paths
*/
void
cleanup_path(void)
{
if(incpath)
free(incpath);
if(str_offset)
free(str_offset);
}
/* ----
* int add_path(char*, int)
* ----
* add a path to includes
*/
int
add_path(char* path, int l)
{
/* Expand str_offset array if needed */
if(incpathCount >= str_offsetCount)
{
str_offsetCount += INCREMENT_BASE;
str_offset = (int*)realloc(str_offset, str_offsetCount * sizeof(int));
if(str_offset == NULL)
return 0;
}
/* Initialize string offset */
str_offset[incpathCount] = incpathSize - remaining;
/* Realloc string buffer if needed */
if(remaining < l)
{
remaining = incpathSize;
/* evil trick, get the greater multiple of INCREMENT_BASE closer
to (size + l). Note : this only works for INCREMENT_BASE = 2^n*/
incpathSize = ((incpathSize + l) + INCREMENT_BASE) & ~INCREMENT_BASE_MASK;
remaining = incpathSize - remaining;
incpath = (char*)realloc(incpath, incpathSize);
if(incpath == NULL)
return 0;
}
remaining -= l;
/* Copy path */
strncpy(incpath + str_offset[incpathCount], path, l);
incpath[str_offset[incpathCount] + l - 1] = '\0';
++incpathCount;
return 1;
}
#ifdef WIN32
#define ENV_PATH_SEPARATOR ';'
#else
#define ENV_PATH_SEPARATOR ':'
#endif
/* ----
* int init_path()
* ----
* init the include path
*/
int
init_path(void)
{
char *p,*pl;
int ret, l;
/* Get env variable holding PCE path*/
p = getenv(machine->include_env);
if (p == NULL)
return 2;
l = 0;
pl = p;
while(pl != NULL)
{
/* Jump to next separator */
pl = strchr(p, ENV_PATH_SEPARATOR);
/* Compute new substring size */
if(pl == NULL)
l = strlen(p) + 1;
else
l = pl - p + 1;
/* Might be empty, jump to next char */
if(l <= 1)
{
++p;
continue;
}
/* Add path */
ret = add_path(p, l);
if(!ret)
return 0;
/* Eat remaining separators */
while (*p == ENV_PATH_SEPARATOR) ++p;
p += l;
}
return 1;
}
/* ----
* readline()
* ----
* read and format an input line.
*/
int
readline(void)
{
char *ptr, *arg, num[8];
int j, n;
int i; /* pointer into prlnbuf */
int c; /* current character */
int temp; /* temp used for line number conversion */
start:
for (i = 0; i < LAST_CH_POS; i++)
prlnbuf[i] = ' ';
/* if 'expand_macro' is set get a line from macro buffer instead */
if (expand_macro) {
if (mlptr == NULL) {
while (mlptr == NULL) {
midx--;
mlptr = mstack[midx];
mcounter = mcntstack[midx];
if (midx == 0) {
mlptr = NULL;
expand_macro = 0;
break;
}
}
}
/* expand line */
if (mlptr) {
i = SFIELD;
ptr = mlptr->data;
for (;;) {
c = *ptr++;
if (c == '\0')
break;
if (c != '\\')
prlnbuf[i++] = c;
else {
c = *ptr++;
prlnbuf[i] = '\0';
/* \@ */
if (c == '@') {
n = 5;
sprintf(num, "%05i", mcounter);
arg = num;
}
/* \# */
else if (c == '#') {
for (j = 9; j > 0; j--)
if (strlen(marg[midx][j - 1]))
break;
n = 1;
sprintf(num, "%i", j);
arg = num;
}
/* \?1 - \?9 */
else if (c == '?') {
c = *ptr++;
if (c >= '1' && c <= '9') {
n = 1;
sprintf(num, "%i", macro_getargtype(marg[midx][c - '1']));
arg = num;
}
else {
error("Invalid macro argument index!");
return (-1);
}
}
/* \1 - \9 */
else if (c >= '1' && c <= '9') {
j = c - '1';
n = strlen(marg[midx][j]);
arg = marg[midx][j];
}
/* unknown macro special command */
else {
error("Invalid macro argument index!");
return (-1);
}
/* check for line overflow */
if ((i + n) >= LAST_CH_POS - 1) {
error("Invalid line length!");
return (-1);
}
/* copy macro string */
strncpy(&prlnbuf[i], arg, n);
i += n;
}
if (i >= LAST_CH_POS - 1)
i = LAST_CH_POS - 1;
}
prlnbuf[i] = '\0';
mlptr = mlptr->next;
return (0);
}
}
/* put source line number into prlnbuf */
i = 4;
temp = ++slnum;
while (temp != 0) {
prlnbuf[i--] = temp % 10 + '0';
temp /= 10;
}
/* get a line */
i = SFIELD;
c = getc(in_fp);
if (c == EOF) {
if (close_input())
return (-1);
goto start;
}
for (;;) {
/* check for the end of line */
if (c == '\r') {
c = getc(in_fp);
if (c == '\n' || c == EOF)
break;
ungetc(c, in_fp);
break;
}
if (c == '\n' || c == EOF)
break;
/* store char in the line buffer */
prlnbuf[i] = c;
i += (i < LAST_CH_POS) ? 1 : 0;
/* expand tab char to space */
if (c == '\t') {
prlnbuf[--i] = ' ';
i += (8 - ((i - SFIELD) % 8));
}
/* get next char */
c = getc(in_fp);
}
prlnbuf[i] = '\0';
return(0);
}
/* ----
* open_input()
* ----
* open input files - up to 7 levels.
*/
int
open_input(char *name)
{
FILE *fp;
char *p;
char temp[128];
int i;
/* only 7 nested input files */
if (infile_num == 7) {
error("Too many include levels, max. 7!");
return (1);
}
/* backup current input file infos */
if (infile_num) {
input_file[infile_num].lnum = slnum;
input_file[infile_num].fp = in_fp;
}
/* get a copy of the file name */
strcpy(temp, name);
/* auto add the .asm file extension */
if ((p = strrchr(temp, '.')) != NULL) {
if (strchr(p, PATH_SEPARATOR))
strcat(temp, ".asm");
}
else {
strcat(temp, ".asm");
}
/* check if this file is already opened */
if (infile_num) {
for (i = 1; i < infile_num; i++) {
if (!strcmp(input_file[i].name, temp)) {
error("Repeated include file!");
return (1);
}
}
}
/* open the file */
if ((fp = open_file(temp, "r")) == NULL)
return (-1);
/* update input file infos */
in_fp = fp;
slnum = 0;
infile_num++;
input_file[infile_num].fp = fp;
input_file[infile_num].if_level = if_level;
strcpy(input_file[infile_num].name, temp);
if ((pass == LAST_PASS) && (xlist) && (list_level))
fprintf(lst_fp, "#[%i] %s\n", infile_num, input_file[infile_num].name);
/* ok */
return (0);
}
/* ----
* close_input()
* ----
* close an input file, return -1 if no more files in the stack.
*/
int
close_input(void)
{
if (proc_ptr) {
fatal_error("Incomplete PROC!");
return (-1);
}
if (in_macro) {
fatal_error("Incomplete MACRO definition!");
return (-1);
}
if (input_file[infile_num].if_level != if_level) {
fatal_error("Incomplete IF/ENDIF statement!");
return (-1);
}
if (infile_num <= 1)
return (-1);
fclose(in_fp);
infile_num--;
infile_error = -1;
slnum = input_file[infile_num].lnum;
in_fp = input_file[infile_num].fp;
if ((pass == LAST_PASS) && (xlist) && (list_level))
fprintf(lst_fp, "#[%i] %s\n", infile_num, input_file[infile_num].name);
/* ok */
return (0);
}
/* ----
* open_file()
* ----
* open a file - browse paths
*/
FILE *
open_file(char *name, char *mode)
{
FILE *fileptr;
char testname[256];
int i;
fileptr = fopen(name, mode);
if (fileptr != NULL) return(fileptr);
for (i = 0; i < incpathCount; ++i) {
if (strlen(incpath+str_offset[i])) {
strcpy(testname, incpath+str_offset[i]);
strcat(testname, PATH_SEPARATOR_STRING);
strcat(testname, name);
fileptr = fopen(testname, mode);
if (fileptr != NULL) break;
}
}
return (fileptr);
}