1 /*--------------------------------*-C-*---------------------------------*
8 * Public: SLexpand_escaped_char ();
9 * SLexpand_escaped_string ();
10 * SLang_extract_token ();
11 * SLang_guess_type ();
18 * Copyright (c) 1992, 1995 John E. Davis
19 * All rights reserved.
21 * You may distribute under the terms of either the GNU General Public
22 * License or the Perl Artistic License.
23 \*----------------------------------------------------------------------*/
36 /* There are non-zeros at positions "\t %()*,/:;[]{}" */
38 static const unsigned char special_chars
[256] =
40 /* 0 */ 0,0,0,0,0,0,0,0, 0,'\t',0,0,0,0,0,0,
41 /* 16 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
42 /* 32 */ ' ',0,0,0,0,'%',0,0, '(',')','*',0,',',0,0,'/',
43 /* 48 */ 0,0,0,0,0,0,0,0, 0,0,':',';',0,0,0,0,
44 /* 64 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
45 /* 80 */ 0,0,0,0,0,0,0,0, 0,0,0,'[',0,']',0,0,
46 /* 96 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
47 /* 112 */ 0,0,0,0,0,0,0,0, 0,0,0,'{',0,'}',0,0,
48 /* 8-bit characters */
49 /* 128 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
50 /* 144 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
51 /* 160 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
52 /* 176 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
53 /* 192 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
54 /* 208 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
55 /* 224 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
56 /* 240 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0
59 char *SLexpand_escaped_char(char *p
, char *ch
)
62 int max
= 0, num
, base
= 0;
69 default: num
= ch1
; break;
70 case 'n': num
= '\n'; break;
71 case 't': num
= '\t'; break;
72 case 'v': num
= '\v'; break;
73 case 'b': num
= '\b'; break;
74 case 'r': num
= '\r'; break;
75 case 'f': num
= '\f'; break;
76 case 'E': case 'e': num
= 27; break;
81 case '0': case '1': case '2': case '3':
82 case '4': case '5': case '6': case '7':
84 base
= 8; i
= 2; num
= ch1
- '0';
87 case 'd': /* decimal -- S-Lang extension */
106 if ((ch1
<= max
) && (ch1
>= '0'))
108 num
= base
* num
+ (ch1
- '0');
113 if ((ch1
< 'a') || ((ch1
> 'f'))) break;
114 num
= base
* num
+ 10 + (ch1
- 'a');
124 void SLexpand_escaped_string (register char *s
, register char *t
,
134 t
= SLexpand_escaped_char (t
, &ch
);
142 int SLang_extract_token (char **linep
, char *word_parm
, int byte_comp
)
144 register char ch
, *line
, *word
= word_parm
;
149 word_max
= word
+ 250;
153 /* skip white space */
154 while (((ch
= *line
) == ' ')
155 || (ch
== '\t')) line
++;
157 if ((!ch
) || (ch
== '\n'))
166 /* Look for -something and rule out --something and -= something */
168 (*line
!= '-') && (*line
!= '=') && ((*line
> '9') || (*line
< '0')))
176 if (ch
== '"') string
= 1; else string
= 0;
179 if ((ch
= *line
++) != 0)
183 line
= SLexpand_escaped_char(line
, &ch1
);
189 sprintf(word
, "%d", (int) ((unsigned char) ch
));
190 word
+= strlen (word
); ch
= '\'';
192 else SLang_Error
= SYNTAX_ERROR
;
194 else SLang_Error
= SYNTAX_ERROR
;
196 else if (!special_chars
[(unsigned char) ch
])
200 ((ch
!= '\n') && (ch
!= 0) && (ch
!= '"')))
207 if ((ch
== 0) || (ch
== '\n')) break;
208 if (byte_comp
) *word
++ = '\\';
211 line
= SLexpand_escaped_char(line
- 1, &ch1
);
216 else if (special_chars
[(unsigned char) ch
])
225 SLang_doerror ("Token to large.");
231 if ((!ch
) || (ch
== '\n')) line
--;
232 if ((ch
== '"') && string
) *word
++ = '"'; else if (string
) SLang_Error
= SYNTAX_ERROR
;
235 /* massage variable-- and ++ into --variable, etc... */
236 if (((int) (word
- word_parm
) > 2)
237 && (ch
= *(word
- 1), (ch
== '+') || (ch
== '-'))
238 && (ch
== *(word
- 2)))
241 while (word
>= word_parm
+ 2)
253 int SLang_guess_type (char *t
)
264 while ((*p
>= '0') && (*p
<= '9')) p
++;
265 if (t
== p
) return(STRING_TYPE
);
266 if ((*p
== 'x') && (p
== t
+ 1)) /* 0x?? */
270 ((ch
>= '0') && (ch
<= '9'))
271 || (((ch
| 0x20) >= 'a') && ((ch
| 0x20) <= 'f'))) p
++;
273 if (*p
== 0) return(INT_TYPE
);
279 /* now down to float case */
283 while ((*p
>= '0') && (*p
<= '9')) p
++;
285 if (*p
== 0) return(FLOAT_TYPE
);
286 if ((*p
!= 'e') && (*p
!= 'E')) return(STRING_TYPE
);
288 if ((*p
== '-') || (*p
== '+')) p
++;
289 while ((*p
>= '0') && (*p
<= '9')) p
++;
290 if (*p
!= 0) return(STRING_TYPE
); else return(FLOAT_TYPE
);
294 int SLatoi (unsigned char *s
)
296 register unsigned char ch
;
297 register unsigned int value
;
300 if (*s
!= '0') return atoi((char *) s
);
302 /* look for 'x' which indicates hex */
304 if ((*s
| 0x20) == 'x')
310 SLang_Error
= SYNTAX_ERROR
;
318 while ((ch
= *s
++) != 0)
320 char ch1
= ch
| 0x20;
324 SLang_Error
= SYNTAX_ERROR
;
328 if (base
!= 16) SLang_Error
= SYNTAX_ERROR
;
347 if (base
!= 16) SLang_Error
= SYNTAX_ERROR
;
348 ch1
= (ch1
- 'a') + 10;
351 value
= value
* base
+ ch1
;