-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.c
More file actions
530 lines (456 loc) · 13.2 KB
/
parser.c
File metadata and controls
530 lines (456 loc) · 13.2 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* PARSES.C -- subroutines used in command parsing */
#include "mixit.h"
#ifndef gcc
#include <stdarg.h>
#endif
#include "port.h"
static int numbad(char base);
static int valbad(char base);
static int addrnfg(GPF *gpfp);
static int addrbad(GPF *gpfp);
static int typebad(int input_parse, GPF *gpfp);
static int outqualbad(int input_parse, GPF *gpfp);
static int qualbad(int input_parse, GPF *gpfp);
/* global to these routines */
extern char ttybuf[BUFSIZ], *ttp; /* cmd buf & remainder ptr */
extern char token[BUFSIZ]; /* current token and its work pointer */
static uint32_t num; /* value found by numbad()|valbad() */
/* parsing constructs */
#define SYMNAM "%256[A-Za-z0-9$_]"
#if defined(VMS)
#define FILESPEC "%256[]-[.:;A-Za-z0-9$_]"
#endif
#if defined(MS_DOS)
#define FILESPEC "%256[\\.:A-Za-z0-9$_]"
#endif
#if !defined(VMS) && !defined(MS_DOS)
#define FILESPEC "%256[!-~]"
#endif
#define QUALIFIER "-/" /* either a dash or a slash works as qualifier delim */
#define wasbad(why) showbad(1,why)
#define isbad(why) showbad(0,why)
/*==========================================================================*
* skip white space at start of text, returning ptr to 1st significant char *
*==========================================================================*/
char* sig(char *text)
{
while ( isspace(*text) )
text++;
return text;
} /* end sig */
/*==========================================================================*/
int showbad(int before, char *why)
{
char *cp = ttybuf - 1;
if ( in(&ttybuf[0], ttp, &ttybuf[BUFSIZ - 1]) ) /* ttp is valid */
{
fprintf(errFile, "%s:\n", why);
fprintf(errFile, "in your line: %s", ttybuf);
*ttp = '\0'; /* end the string here */
while ( *++cp )
if ( *cp != '\t' )
*cp = ' ';
fprintf(errFile, "at or %s:%s^\n\n", before ? "before" : "after", ttybuf);
}
else
fprintf(errFile, "%s\n\n", why);
return 1;
} /* end showbad */
/*==========================================================================*/
static int numbad(char base) /* name's eaten, including '=' or ':' */
{
char newbase[] = { '%', 'l', 0, '\0' }; /* conversion area */
char *str = 0; /* to decide format string for given base*/
char *cp0, *cp1, *cp2; /* work pointers for double checking */
newbase[2] = base;
if ( ttp[0] == '0' && (ttp[1] == 'x' || ttp[1] == 'X') )
{
newbase[2] = 'x';
ttp += 2;
}
else if ( *ttp == '%' )
{
if ( subscanf(++ttp, "%1[oOdDxX]", &newbase[2]) != 1 )
return isbad("Grody radix on number");
ttp++;
}
newbase[2] = tolower(newbase[2]);
#ifndef vax
if ( newbase[2] == 'd' )
newbase[2] = 'u'; /* no signed numbers */
#endif
if ( sscanf(ttp, newbase, &num) != 1 )
return isbad("Invalid numeric value, or number too big");
switch (newbase[2])
{
case 'o':
str = "%100[0-7]";
break;
case 'u':
str = "%100[0-9]";
break;
case 'd':
str = "%100[0-9]";
break;
case 'x':
str = "%100[0-9a-fA-F]";
break;
default:
isbad("LOGIC ERROR!! conversion default can't happen");
exit(FATAL);
}
if ( subscanf(ttp, str, token) != 1 )
{
isbad("LOGIC ERROR!! can't find number just converted");
exit(FATAL);
}
cp1 = ttp + strlen(token); /* just past real number */
if ( subscanf(ttp, SYMNAM, token) != 1 )
{
isbad("LOGIC ERROR!! can't find supernumber just converted");
exit(FATAL);
}
cp2 = ttp + strlen(token); /* just past real token */
if ( (cp0 = sig(cp1)) != sig(cp2) )
return isbad("Garbage in numeric value");
ttp = cp0; /* move past number */
return 0; /* we liked it! */
} /* end numbad */
/*==========================================================================*/
static int valbad(char base) /* name's eaten, but not '=' or ':' */
{
if ( *ttp == ':' || *ttp == '=' )
ttp = sig(++ttp); /* move to value */
else
return wasbad("Value required on qualifier");
return (numbad(base));
} /* end valbad */
/*==========================================================================*/
static int addrnfg(GPF *gpfp) /* consist. ck after qual was parsed */
{
if ( (gpfp->flags & GPF_M_START) && (gpfp->flags & GPF_M_END) )
if ( gpfp->low_limit > gpfp->high_limit )
return wasbad("Conflicting START and END addresses");
return 0; /* Was ok */
} /* end addrnfg */
/*==========================================================================*/
static int addrbad(GPF *gpfp) /* name was eaten, but not ':' or '=' */
{
if ( *ttp == ':' || *ttp == '=' )
ttp = sig(++ttp); /* move to value */
else
return wasbad("Value(s) required on qualifier");
if ( *ttp == '(' )
while ( 1 )
{ /* long form */
ttp = sig(++ttp); /* move to keyword */
if ( subscanf(ttp, SYMNAM, token) != 1 )
return !isbad("Invalid keyword");
ttp = sig(ttp + strlen(token)); /* move on */
switch (lookup_token(token, "START", "END", "OUTPUT", NULL))
{
case 0: /* START */
gpfp->low_limit = num;
gpfp->flags |= GPF_M_START;
break;
case 1: /* END */
gpfp->high_limit = num;
gpfp->flags |= GPF_M_END;
break;
case 2: /* OUTPUT */
gpfp->out_add = num;
gpfp->flags |= GPF_M_MOVE;
break;
default:
if ( valbad('x') )
return 1;
isbad("Unrecognized option");
return 1;
}
if ( *ttp == ')' )
{
ttp = sig(++ttp);
return addrnfg(gpfp); /* one way or another */
}
if ( *ttp != ',' )
{
isbad("Comma or close parenthesis required");
return 1;
}
} /* end of long form (and while) */
else
{ /* short form */
if ( numbad('x') )
return 1;
gpfp->low_limit = num;
gpfp->flags |= GPF_M_START;
if ( *ttp == ':' )
ttp = sig(++ttp); /* move to value */
else
return addrnfg(gpfp); /* one way or another */
if ( numbad('x') )
return 1;
gpfp->high_limit = num;
gpfp->flags |= GPF_M_END;
if ( *ttp == ':' )
ttp = sig(++ttp); /* move to value */
else
return addrnfg(gpfp); /* one way or another */
if ( numbad('x') )
return 1;
gpfp->out_add = num;
gpfp->flags |= GPF_M_MOVE;
return addrnfg(gpfp); /* one way or another */
} /* end of short form */
} /* end addrbad */
/*==========================================================================*/
static int typebad(int input_parse, GPF *gpfp) /* slash already eaten */
{
/* returns -1 if qual not recognized, 0 for accepted, 1 for bad */
switch (lookup_token(token, "LDA", "ROM", "MAC", "TEKHEX", "HEX",
"MOSTECH", "VLDA", "IMAGE", "IMG", "ASM", "GNU",
"INTEL", "MOTOROLA", "COFF", "ELF", "DIO", "CPE", NULL))
{
case 0:
gpfp->rec_type = GPF_K_LDA;
break;
case 1:
gpfp->rec_type = GPF_K_ROM;
break;
case 2:
if ( !input_parse )
gpfp->rec_type = GPF_K_MAC;
break;
case 3:
case 4:
gpfp->rec_type = GPF_K_HEX;
break;
case 5:
gpfp->rec_type = GPF_K_DLD;
break;
case 6:
gpfp->rec_type = GPF_K_VLDA;
break;
case 7:
case 8:
gpfp->rec_type = GPF_K_IMG;
break;
case 9:
case 10:
if ( !input_parse )
gpfp->rec_type = GPF_K_GNU;
break;
case 11:
gpfp->rec_type = GPF_K_INTEL;
break;
case 12:
gpfp->rec_type = GPF_K_MOT;
break;
case 13:
if ( !input_parse )
{
fprintf(errFile, "Sorry, COFF is not supported for output just yet\n");
return 1;
}
gpfp->rec_type = GPF_K_COFF;
break;
case 14:
if ( !input_parse )
{
fprintf(errFile, "Sorry, ELF32 is not supported for output just yet\n");
return 1;
}
gpfp->rec_type = GPF_K_ELF;
break;
case 15:
gpfp->rec_type = GPF_K_DIO;
break;
case 16:
gpfp->rec_type = GPF_K_CPE;
break;
default:
return -1;
}
return 0;
} /* end typebad */
/*==========================================================================*/
static int outqualbad(int input_parse, GPF *gpfp) /* slash already eaten */
{
/*
* returns -1 if qual not recognized, 0 for accepted, 1 for bad
* only called if output parse or first input parse
*/
switch (lookup_token(token, "FILL", "MODE",
"RECORDSIZE", "RECORD_SIZE", "NOSYMBOL",
"WORDSIZE", "WORD_SIZE", "NOPAD",
"EVEN_HALF", "ODD_HALF", NULL))
{
case 0: /* FILL */
if ( valbad('x') )
return 1;
gpfp->fill_char = (uint8_t)num;
gpfp->flags |= GPF_M_FILL;
if ( input_parse && (outgpf.flags & GPF_M_FILL) )
{
if ( outgpf.fill_char == num )
return 0; /* they match */
return wasbad("Value conflicts with OUTPUT");
}
break;
case 1: /* MODE */
if ( *ttp == ':' || *ttp == '=' )
ttp = sig(++ttp); /* move to value */
else
return wasbad("Value required on qualifier");
if ( subscanf(ttp, SYMNAM, token) != 1 )
return isbad("Invalid keyword");
ttp = sig(ttp + strlen(token)); /* eat keyword */
switch (lookup_token(token, "WORD", "BYTE", NULL))
{
case 0:
/* WORD */ gpfp->flags |= GPF_M_WORD;
break;
case 1:
/* BYTE */ gpfp->flags &= ~GPF_M_WORD;
break;
default:
return isbad("Unrecognized MODE keyword");
}
if ( !input_parse && (outgpf.flags & GPF_M_MAU) )
return isbad("MODE and WORD_SIZE cannot both be specified in OUTPUT commands");
gpfp->flags |= GPF_M_MODESPEC;
if ( input_parse && (outgpf.flags & GPF_M_MODESPEC) )
{
if ( (outgpf.flags & GPF_M_WORD) == (gpfp->flags & GPF_M_WORD) )
return 0; /* they match */
return wasbad("Value conflicts with OUTPUT");
}
break;
case 2: /* RECORDSIZE */
case 3: /* RECORD_SIZE */
if ( valbad('d') )
return 1;
gpfp->rec_size = (uint16_t)num;
break;
case 4: /* NOSYMBOL */
gpfp->flags &= ~GPF_M_SYMBOL;
break;
case 5: /* WORDSIZE */
case 6: /* WORD_SIZE */
if ( valbad('d') )
return 1;
if ( num & 7 )
return wasbad("WORD_SIZE must be a multiple of 8");
if ( !input_parse && (outgpf.flags & GPF_M_MODESPEC) )
return isbad("MODE and WORD_SIZE cannot both be specified in OUTPUT commands");
gpfp->flags |= GPF_M_MAU;
gpfp->bits_per_word = (uint8_t)num;
if ( input_parse && (outgpf.flags & GPF_M_MAU) )
{
if ( outgpf.bits_per_word == num )
return 0; /* they match */
return wasbad("Value conflicts with OUTPUT");
}
break;
case 7: /* NOPAD */
gpfp->flags |= GPF_M_NOPAD;
break;
case 8: /* EVEN_WORDS */
gpfp->flags |= GPF_M_EVENW;
break;
case 9: /* ODD_WORDS */
gpfp->flags |= GPF_M_ODDW;
break;
default:
return -1; /* not recognized */
}
return 0;
} /* end outqualbad */
/*==========================================================================*/
static int qualbad(int input_parse, GPF *gpfp) /* slash already eaten */
{
int t;
if ( !(t = typebad(input_parse, gpfp)) || t == 1 )
return t; /* 1 is bad, 0 is good */
#if 0
if ( !input_parse || !inspec[0] ); /* only if no input read yet */
#endif
if ( !(t = outqualbad(input_parse, gpfp)) || t == 1 )
return t; /* 1 is bad, 0 is good */
switch (lookup_token(token, "ADDRESS", "GROUP", "SWAP",
"WORDSIZE", "WORD_SIZE", NULL))
{
case 0: /* ADDRESS */
if ( input_parse )
return addrbad(gpfp);
break;
case 1: /* GROUP */
if ( !input_parse )
return wasbad("GROUP is only valid as an input qualifier");
if ( valbad('d') )
return 1;
if ( num & 7 )
return wasbad("GROUP must be a multiple of 8");
gpfp->group_code = (uint8_t)(num >> 3); /* Make it a byte number */
gpfp->flags |= GPF_M_GROUP;
break;
case 2: /* SWAP */
gpfp->flags |= GPF_M_SWAP;
break;
case 3: /* WORDSIZE */
case 4: /* WORD_SIZE */
if ( !input_parse )
return wasbad("WORD_SIZE is only valid as an input qualifier");
if ( valbad('d') )
return 1;
if ( num & 7 )
return wasbad("WORD_SIZE must be a multiple of 8");
gpfp->flags |= GPF_M_MAU;
gpfp->bits_per_word = (uint8_t)num;
if ( input_parse && (outgpf.flags & GPF_M_MAU) )
{
if ( outgpf.bits_per_word == num )
return 0; /* they match */
return wasbad("Value conflicts with OUTPUT");
}
break;
default:
return wasbad("Unrecognized qualifier");
}
return 0;
} /* end qualbad */
/*==========================================================================*/
int ioparsebad(int input_parse, GPF *gpfp)
{
/* logical flags for this pass only */
int gotspec = 0;
while ( 1 )
{ /* we exit only with a return */
if ( *ttp == '\0' || *ttp == '!' )
{ /* parse complete */
if ( gotspec == 1 )
return 0; /* 'twerked fine */
return wasbad("Need a filename");
}
if ( gotspec == 0 )
{
if ( subscanf(ttp, FILESPEC, filespec) != 1 )
return isbad("Unrecognized command; invalid character(s)");
gotspec++;
ttp = sig(ttp + strlen(filespec)); /* move past filename */
continue;
}
if ( strchr(QUALIFIER, *ttp) )
{ /* option found */
ttp++; /* skip the opt delimiter */
if ( subscanf(ttp, SYMNAM, token) != 1 )
return isbad("Invalid qualifier");
ttp = sig(ttp + strlen(token)); /* move past name */
if ( qualbad(input_parse, gpfp) )
return 1; /* already told */
continue;
}
else
return isbad("Too many parameters (only one filename allowed)");
}
} /* end ioparsebad */