-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdtokeniz.c
195 lines (179 loc) · 6.12 KB
/
dtokeniz.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
/* detokenize.c
* - routines to detokenize C64/C128 BASIC
*/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "tokenize.h"
#include "tokens.h"
#define FALSE 0
#define TRUE 1
/* The bytestream buffer used in the function (input) is from the line
* number up to the ending null character. The "next line" pointer is
* not included
*/
/* detokenize
* - detokenize a C64/C128 BASIC (in binary) line
* in: input_p - pointer to a bytestream to detokenize
* output_p - pointer to a string to put results in, MUST BE ALLOCATED
* mode - BASIC version to detokenize
* strict - flag for using strict tok64 compatibility
* out: nonzero on error
*/
int detokenize(const char *input_p, char *output_p, basic_t mode, int strict)
{
int quotemode = FALSE; /* flag for quote mode */
unsigned short i; /* loop counter */
unsigned linenumber; /* line number */
int rc = 0; /* return code */
int isspecial; /* flag for special characters */
const unsigned char *ch_p; /* pointer moving over input */
const char *escape_p; /* pointer to current escape sequence */
char numeric[4]; /* threedigit numeric escape for strict tok64
compatibility */
ch_p = (const unsigned char*) input_p;
/* First two bytes is the line number as (low,high) */
linenumber = (*ch_p) | (*(ch_p + 1)) << 8;
ch_p += 2;
/* print it to the output string, and move the character pointer beyond */
output_p += sprintf(output_p, "%u ", linenumber);
/* Next comes a bytestream of line data, ending in a null character */
while (*ch_p) {
/* Point to PETSCII sequence */
escape_p = petscii[*ch_p];
if (strict && nontok64compatible(*ch_p)) {
/* Maintain tok64 compatibility */
sprintf(numeric, "%03d", (int) *ch_p);
escape_p = numeric;
}
/* Process token */
if (quotemode) { /* quoted string? */
/* Convert from PETSCII to ASCII,
* and write repetitions as a multiple of the character.
* Repetitions of non-special characters is only written if
* there are three or more repetitions.
* Repetitions of * is not written ({**n} is not parsed correctly)
* Repetitions of " is not written (quotemode on/off)
*/
if (34 == *ch_p) { /* quote */
*(output_p ++) = '\"';
quotemode = FALSE; /* go out of quotemode */
} /* if */
else if (42 == *ch_p) { /* asterisk */
*(output_p ++) = '*';
} /* else */
else {
/* Check for special token (escape is multibyte) */
if (escape_p[1] == 0) {
isspecial = FALSE;
} /* if */
else {
isspecial = TRUE;
} /* else */
/* Check repetition if:
* current and next character match
* AND (at least) one of the following:
* <this is a special character OR space>
* OR current and third character match
* AND (at least) one of the following:
* we are not in tok64 strict compatibility mode
* OR the character is space
* OR the escape code is not a single character
*/
if (*ch_p == ch_p[1] &&
((isspecial || 32 == *ch_p) ||
*ch_p == ch_p[2]) &&
(!strict || 32 == *ch_p || strlen(escape_p) > 1)) {
/* Count repetitions */
i = 2;
while (ch_p[i] == *ch_p) i ++;
/* We know the repetition number, now print it */
if (32 == *ch_p) { /* space */
output_p += sprintf(output_p, "{space*%hd}", i);
} /* if */
else {
output_p += sprintf(output_p, "{%s*%hd}", escape_p, i);
} /* else */
ch_p += i - 1; /* point to last repetition */
} /* if */
else { /* not repetition */
if (isspecial) {
output_p += sprintf(output_p, "{%s}", escape_p);
} /* if */
else { /* normal character */
*(output_p ++) = *escape_p;
} /* else */
} /* else */
} /* else */
} /* if */
else { /* command mode */
if (*ch_p >= 128 && *ch_p <= 254) { /* Probable BASIC command */
if ((unsigned char) *ch_p <= 203) {
/* C64 BASIC 2.0 */
output_p += sprintf(output_p, "%s",
c64tokens[*ch_p - 128]);
} /* if */
else if (*ch_p == 0xCE &&
(*(ch_p + 1) >= 2 && *(ch_p + 1) <= 0xA) &&
(Basic7 == mode || Basic71 == mode)) {
/* C128 BASIC 7.0 CE prefix */
ch_p ++;
output_p += sprintf(output_p, "%s",
c128CEtokens[*ch_p]);
} /* else */
else if (*ch_p == 0xFE && *(ch_p + 1) >= 2 &&
((*(ch_p + 1) <= 0x26 && Basic7 == mode) ||
(*(ch_p + 1) <= 0x37 && Basic71 == mode))) {
/* C128 BASIC 7.0/7.1 FE prefix */
ch_p ++;
output_p += sprintf(output_p, "%s",
c128FEtokens[*ch_p]);
} /* else */
else if (Basic7 == mode || Basic71 == mode) {
/* C128 BASIC 7.0 */
output_p += sprintf(output_p, "%s",
c128tokens[*ch_p - 204]);
} /* else */
else if (Graphics52 == mode) {
/* C64 Graphics52 */
output_p += sprintf(output_p, "%s",
graphics52tokens[*ch_p - 204]);
} /* else */
else if (*ch_p <= 232 && TFC3 == mode) {
/* C64 TFC3 */
output_p += sprintf(output_p, "%s",
tfc3tokens[*ch_p - 204]);
} /* else */
else {
/* Errorneous token */
output_p += sprintf(output_p, "{%d}", *ch_p);
}
} /* if */
else { /* text */
/* PETSCII text in BASIC:
* The only possible case of text is unshifted. To increase
* readability, this is written as lowercase ASCII, whereas
* keywords are written as uppercase.
* There can also be special characters (32-64), they are
* printed as-is.
*/
if ((*ch_p >= 32 && *ch_p <= 64) || /* ' ' - '@', */
91 == *ch_p || 93 == *ch_p) { /* '[', ']' */
*(output_p ++) = *ch_p;
if (34 == *ch_p) {
quotemode = TRUE; /* go to quotemode */
} /* if */
} /* if */
else if (*ch_p >= 65 && *ch_p <= 90) { /* 'A' - 'Z' */
*(output_p ++) = tolower(*ch_p);
} /* else */
else { /* Possibly illegal character, write petscii escape */
output_p += sprintf(output_p, "{%s}", escape_p);
} /* else */
} /* else */
} /* else */
ch_p ++; /* next character */
} /* while */
*output_p = 0;
return rc;
}