-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdetok.c
81 lines (71 loc) · 1.77 KB
/
detok.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
#include "defs.h"
// C Lexical scanning: detokeniser
// Copyright (c) 2023 Warren Toomey, GPL3
char Text[TEXTLEN + 1]; // Last identifier scanned
extern char *Tstring[];
// Read at most count-1 characters from the
// f FILE and store them in the s buffer.
// Terminate the s buffer with a NUL.
// Return NULL if unable to read or an EOF.
// Else, return the original s pointer pointer.
char *fgetstr(char *s, size_t count, FILE * f) {
size_t i = count;
int ch;
char *ret = s;
while (i-- != 0) {
if ((ch = getc(f)) == EOF) {
if (s == ret)
return(NULL);
break;
}
*s++ = (char) ch;
if (ch == 0)
break;
}
*s = 0;
return(ferror(f) ? (char *) NULL : ret);
}
int main(int argc, char **argv) {
FILE *in;
int token;
int intval;
if (argc !=2) {
fprintf(stderr, "Usage: %s tokenfile\n", argv[0]); exit(1);
}
in= fopen(argv[1], "r");
if (in==NULL) {
fprintf(stderr, "Unable to open %s\n", argv[1]); exit(1);
}
// Read until no more tokens left
while (1) {
token = fgetc(in);
if (token == EOF)
break;
switch (token) {
case T_INTLIT:
case T_CHARLIT:
fread(&intval, sizeof(int), 1, in);
printf("%02X: %d\n", token, intval);
break;
case T_STRLIT:
fgetstr(Text, TEXTLEN + 1, in);
printf("%02X: \"%s\"\n", token, Text);
break;
case T_FILENAME:
fgetstr(Text, TEXTLEN + 1, in);
printf("%02X: filename \"%s\"\n", token, Text);
break;
case T_LINENUM:
fread(&intval, sizeof(int), 1, in);
printf("%02X: linenum %d\n", token, intval);
break;
case T_IDENT:
fgetstr(Text, TEXTLEN + 1, in);
printf("%02X: %s\n", token, Text);
break;
default:
printf("%02X: %s\n", token, Tstring[token]);
}
}
return (0);
}