forked from MidnightCommander/mc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsltoken.c
354 lines (315 loc) · 7.02 KB
/
sltoken.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
/*--------------------------------*-C-*---------------------------------*
* File: sltoken.c
*
* Descript: ---
*
* Requires: ---
*
* Public: SLexpand_escaped_char ();
* SLexpand_escaped_string ();
* SLang_extract_token ();
* SLang_guess_type ();
* SLatoi ();
*
* Private: ---
*
* Notes: ---
*
* Copyright (c) 1992, 1995 John E. Davis
* All rights reserved.
*
* You may distribute under the terms of either the GNU General Public
* License or the Perl Artistic License.
\*----------------------------------------------------------------------*/
#include "config.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#include <string.h>
#include "_slang.h"
/* There are non-zeros at positions "\t %()*,/:;[]{}" */
static const unsigned char special_chars[256] =
{
/* 0 */ 0,0,0,0,0,0,0,0, 0,'\t',0,0,0,0,0,0,
/* 16 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 32 */ ' ',0,0,0,0,'%',0,0, '(',')','*',0,',',0,0,'/',
/* 48 */ 0,0,0,0,0,0,0,0, 0,0,':',';',0,0,0,0,
/* 64 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 80 */ 0,0,0,0,0,0,0,0, 0,0,0,'[',0,']',0,0,
/* 96 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 112 */ 0,0,0,0,0,0,0,0, 0,0,0,'{',0,'}',0,0,
/* 8-bit characters */
/* 128 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 144 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 160 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 176 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 192 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 208 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 224 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
/* 240 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0
};
char *SLexpand_escaped_char(char *p, char *ch)
{
int i = 0;
int max = 0, num, base = 0;
char ch1;
ch1 = *p++;
switch (ch1)
{
default: num = ch1; break;
case 'n': num = '\n'; break;
case 't': num = '\t'; break;
case 'v': num = '\v'; break;
case 'b': num = '\b'; break;
case 'r': num = '\r'; break;
case 'f': num = '\f'; break;
case 'E': case 'e': num = 27; break;
case 'a': num = 7;
break;
/* octal */
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
max = '7';
base = 8; i = 2; num = ch1 - '0';
break;
case 'd': /* decimal -- S-Lang extension */
base = 10;
i = 3;
max = '9';
num = 0;
break;
case 'x': /* hex */
base = 16;
max = '9';
i = 2;
num = 0;
break;
}
while (i--)
{
ch1 = *p;
if ((ch1 <= max) && (ch1 >= '0'))
{
num = base * num + (ch1 - '0');
}
else if (base == 16)
{
ch1 |= 0x20;
if ((ch1 < 'a') || ((ch1 > 'f'))) break;
num = base * num + 10 + (ch1 - 'a');
}
else break;
p++;
}
*ch = (char) num;
return p;
}
void SLexpand_escaped_string (register char *s, register char *t,
register char *tmax)
{
char ch;
while (t < tmax)
{
ch = *t++;
if (ch == '\\')
{
t = SLexpand_escaped_char (t, &ch);
}
*s++ = ch;
}
*s = 0;
}
int SLang_extract_token (char **linep, char *word_parm, int byte_comp)
{
register char ch, *line, *word = word_parm;
int string;
char ch1;
char *word_max;
word_max = word + 250;
line = *linep;
/* skip white space */
while (((ch = *line) == ' ')
|| (ch == '\t')) line++;
if ((!ch) || (ch == '\n'))
{
*linep = line;
return(0);
}
*word++ = ch;
line++;
/* Look for -something and rule out --something and -= something */
if ((ch == '-') &&
(*line != '-') && (*line != '=') && ((*line > '9') || (*line < '0')))
{
*word = 0;
*linep = line;
return 1;
}
if (ch == '"') string = 1; else string = 0;
if (ch == '\'')
{
if ((ch = *line++) != 0)
{
if (ch == '\\')
{
line = SLexpand_escaped_char(line, &ch1);
ch = ch1;
}
if (*line++ == '\'')
{
--word;
sprintf(word, "%d", (int) ((unsigned char) ch));
word += strlen (word); ch = '\'';
}
else SLang_Error = SYNTAX_ERROR;
}
else SLang_Error = SYNTAX_ERROR;
}
else if (!special_chars[(unsigned char) ch])
{
while (ch = *line++,
(ch > '"') ||
((ch != '\n') && (ch != 0) && (ch != '"')))
{
if (string)
{
if (ch == '\\')
{
ch = *line++;
if ((ch == 0) || (ch == '\n')) break;
if (byte_comp) *word++ = '\\';
else
{
line = SLexpand_escaped_char(line - 1, &ch1);
ch = ch1;
}
}
}
else if (special_chars[(unsigned char) ch])
{
line--;
break;
}
*word++ = ch;
if (word > word_max)
{
SLang_doerror ("Token to large.");
break;
}
}
}
if ((!ch) || (ch == '\n')) line--;
if ((ch == '"') && string) *word++ = '"'; else if (string) SLang_Error = SYNTAX_ERROR;
*word = 0;
*linep = line;
/* massage variable-- and ++ into --variable, etc... */
if (((int) (word - word_parm) > 2)
&& (ch = *(word - 1), (ch == '+') || (ch == '-'))
&& (ch == *(word - 2)))
{
word--;
while (word >= word_parm + 2)
{
*word = *(word - 2);
word--;
}
*word-- = ch;
*word-- = ch;
}
return(1);
}
int SLang_guess_type (char *t)
{
char *p;
register char ch;
if (*t == '-') t++;
p = t;
#ifdef FLOAT_TYPE
if (*p != '.')
{
#endif
while ((*p >= '0') && (*p <= '9')) p++;
if (t == p) return(STRING_TYPE);
if ((*p == 'x') && (p == t + 1)) /* 0x?? */
{
p++;
while (ch = *p,
((ch >= '0') && (ch <= '9'))
|| (((ch | 0x20) >= 'a') && ((ch | 0x20) <= 'f'))) p++;
}
if (*p == 0) return(INT_TYPE);
#ifndef FLOAT_TYPE
return(STRING_TYPE);
#else
}
/* now down to float case */
if (*p == '.')
{
p++;
while ((*p >= '0') && (*p <= '9')) p++;
}
if (*p == 0) return(FLOAT_TYPE);
if ((*p != 'e') && (*p != 'E')) return(STRING_TYPE);
p++;
if ((*p == '-') || (*p == '+')) p++;
while ((*p >= '0') && (*p <= '9')) p++;
if (*p != 0) return(STRING_TYPE); else return(FLOAT_TYPE);
#endif
}
int SLatoi (unsigned char *s)
{
register unsigned char ch;
register unsigned int value;
register int base;
if (*s != '0') return atoi((char *) s);
/* look for 'x' which indicates hex */
s++;
if ((*s | 0x20) == 'x')
{
base = 16;
s++;
if (*s == 0)
{
SLang_Error = SYNTAX_ERROR;
return -1;
}
}
else base = 8;
value = 0;
while ((ch = *s++) != 0)
{
char ch1 = ch | 0x20;
switch (ch1)
{
default:
SLang_Error = SYNTAX_ERROR;
break;
case '8':
case '9':
if (base != 16) SLang_Error = SYNTAX_ERROR;
/* drop */
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
ch1 -= '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
if (base != 16) SLang_Error = SYNTAX_ERROR;
ch1 = (ch1 - 'a') + 10;
break;
}
value = value * base + ch1;
}
return (int) value;
}