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 \*----------------------------------------------------------------------*/
37 /* There are non-zeros at positions "\t %()*,/:;[]{}" */
39 static unsigned char special_chars
[256] =
41 /* 0 */ 0,0,0,0,0,0,0,0, 0,'\t',0,0,0,0,0,0,
42 /* 16 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
43 /* 32 */ ' ',0,0,0,0,'%',0,0, '(',')','*',0,',',0,0,'/',
44 /* 48 */ 0,0,0,0,0,0,0,0, 0,0,':',';',0,0,0,0,
45 /* 64 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
46 /* 80 */ 0,0,0,0,0,0,0,0, 0,0,0,'[',0,']',0,0,
47 /* 96 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
48 /* 112 */ 0,0,0,0,0,0,0,0, 0,0,0,'{',0,'}',0,0,
49 /* 8-bit characters */
50 /* 128 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
51 /* 144 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
52 /* 160 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
53 /* 176 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
54 /* 192 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
55 /* 208 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
56 /* 224 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
57 /* 240 */ 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0
60 char *SLexpand_escaped_char(char *p
, char *ch
)
63 int max
= 0, num
, base
= 0;
70 default: num
= ch1
; break;
71 case 'n': num
= '\n'; break;
72 case 't': num
= '\t'; break;
73 case 'v': num
= '\v'; break;
74 case 'b': num
= '\b'; break;
75 case 'r': num
= '\r'; break;
76 case 'f': num
= '\f'; break;
77 case 'E': case 'e': num
= 27; break;
82 case '0': case '1': case '2': case '3':
83 case '4': case '5': case '6': case '7':
85 base
= 8; i
= 2; num
= ch1
- '0';
88 case 'd': /* decimal -- S-Lang extension */
107 if ((ch1
<= max
) && (ch1
>= '0'))
109 num
= base
* num
+ (ch1
- '0');
114 if ((ch1
< 'a') || ((ch1
> 'f'))) break;
115 num
= base
* num
+ 10 + (ch1
- 'a');
125 void SLexpand_escaped_string (register char *s
, register char *t
,
135 t
= SLexpand_escaped_char (t
, &ch
);
143 int SLang_extract_token (char **linep
, char *word_parm
, int byte_comp
)
145 register char ch
, *line
, *word
= word_parm
;
150 word_max
= word
+ 250;
154 /* skip white space */
155 while (((ch
= *line
) == ' ')
156 || (ch
== '\t')) line
++;
158 if ((!ch
) || (ch
== '\n'))
167 /* Look for -something and rule out --something and -= something */
169 (*line
!= '-') && (*line
!= '=') && ((*line
> '9') || (*line
< '0')))
177 if (ch
== '"') string
= 1; else string
= 0;
180 if ((ch
= *line
++) != 0)
184 line
= SLexpand_escaped_char(line
, &ch1
);
190 sprintf(word
, "%d", (int) ((unsigned char) ch
));
191 word
+= strlen (word
); ch
= '\'';
193 else SLang_Error
= SYNTAX_ERROR
;
195 else SLang_Error
= SYNTAX_ERROR
;
197 else if (!special_chars
[(unsigned char) ch
])
201 ((ch
!= '\n') && (ch
!= 0) && (ch
!= '"')))
208 if ((ch
== 0) || (ch
== '\n')) break;
209 if (byte_comp
) *word
++ = '\\';
212 line
= SLexpand_escaped_char(line
- 1, &ch1
);
217 else if (special_chars
[(unsigned char) ch
])
226 SLang_doerror ("Token to large.");
232 if ((!ch
) || (ch
== '\n')) line
--;
233 if ((ch
== '"') && string
) *word
++ = '"'; else if (string
) SLang_Error
= SYNTAX_ERROR
;
236 /* massage variable-- and ++ into --variable, etc... */
237 if (((int) (word
- word_parm
) > 2)
238 && (ch
= *(word
- 1), (ch
== '+') || (ch
== '-'))
239 && (ch
== *(word
- 2)))
242 while (word
>= word_parm
+ 2)
254 int SLang_guess_type (char *t
)
265 while ((*p
>= '0') && (*p
<= '9')) p
++;
266 if (t
== p
) return(STRING_TYPE
);
267 if ((*p
== 'x') && (p
== t
+ 1)) /* 0x?? */
271 ((ch
>= '0') && (ch
<= '9'))
272 || (((ch
| 0x20) >= 'a') && ((ch
| 0x20) <= 'f'))) p
++;
274 if (*p
== 0) return(INT_TYPE
);
280 /* now down to float case */
284 while ((*p
>= '0') && (*p
<= '9')) p
++;
286 if (*p
== 0) return(FLOAT_TYPE
);
287 if ((*p
!= 'e') && (*p
!= 'E')) return(STRING_TYPE
);
289 if ((*p
== '-') || (*p
== '+')) p
++;
290 while ((*p
>= '0') && (*p
<= '9')) p
++;
291 if (*p
!= 0) return(STRING_TYPE
); else return(FLOAT_TYPE
);
295 int SLatoi (unsigned char *s
)
297 register unsigned char ch
;
298 register unsigned int value
;
301 if (*s
!= '0') return atoi((char *) s
);
303 /* look for 'x' which indicates hex */
305 if ((*s
| 0x20) == 'x')
311 SLang_Error
= SYNTAX_ERROR
;
319 while ((ch
= *s
++) != 0)
321 char ch1
= ch
| 0x20;
325 SLang_Error
= SYNTAX_ERROR
;
329 if (base
!= 16) SLang_Error
= SYNTAX_ERROR
;
348 if (base
!= 16) SLang_Error
= SYNTAX_ERROR
;
349 ch1
= (ch1
- 'a') + 10;
352 value
= value
* base
+ ch1
;