1
|
1 /***************************************************************************
|
|
2 * Copyright (C) 2005 by 510 Software Group *
|
|
3 * *
|
|
4 * *
|
|
5 * This program is free software; you can redistribute it and/or modify *
|
|
6 * it under the terms of the GNU General Public License as published by *
|
|
7 * the Free Software Foundation; either version 2 of the License, or *
|
|
8 * (at your option) any later version. *
|
|
9 * *
|
|
10 * This program is distributed in the hope that it will be useful, *
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
13 * GNU General Public License for more details. *
|
|
14 * *
|
|
15 * You should have received a copy of the GNU General Public License *
|
|
16 * along with this program; if not, write to the *
|
|
17 * Free Software Foundation, Inc., *
|
|
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
19 ***************************************************************************/
|
|
20
|
|
21 #include "includes.h"
|
|
22
|
|
23 static char* tokenizer_version="$Id$";
|
|
24
|
|
25 const int maxlen = 1000; // used for snprintf buffers
|
|
26
|
|
27 enum state {s_init,
|
|
28 s_token,
|
|
29 s_string,
|
|
30 s_ignore, // whitespace
|
|
31 s_eol, // ignore to eol
|
|
32 end_state,
|
|
33
|
|
34 s_term, // token terminator
|
|
35 s_single,
|
|
36 s_string1, // first " of string
|
|
37 s_string2, // last " of string
|
|
38 s_slash // possible start of ignore to eol
|
|
39 };
|
|
40
|
|
41 typedef state PARSE[end_state];
|
|
42
|
|
43 static PARSE parse_table[256] = {
|
|
44 // s_init s_token s_string s_ignore s_eol
|
|
45 { s_single, s_term, s_string, s_single, s_eol, }, // 0x00
|
|
46 { s_single, s_term, s_string, s_single, s_eol, }, // 0x01
|
|
47 { s_single, s_term, s_string, s_single, s_eol, }, // 0x02
|
|
48 { s_single, s_term, s_string, s_single, s_eol, }, // 0x03
|
|
49 { s_single, s_term, s_string, s_single, s_eol, }, // 0x04
|
|
50 { s_single, s_term, s_string, s_single, s_eol, }, // 0x05
|
|
51 { s_single, s_term, s_string, s_single, s_eol, }, // 0x06
|
|
52 { s_single, s_term, s_string, s_single, s_eol, }, // 0x07
|
|
53 { s_single, s_term, s_string, s_single, s_eol, }, // 0x08
|
|
54 { s_ignore, s_term, s_string, s_ignore, s_eol, }, // 0x09 <tab>
|
|
55 { s_ignore, s_term, s_string2, s_ignore, s_ignore, }, // 0x0a <lf>
|
|
56 { s_single, s_term, s_string, s_single, s_eol, }, // 0x0b
|
|
57 { s_single, s_term, s_string, s_single, s_eol, }, // 0x0c
|
|
58 { s_ignore, s_term, s_string2, s_ignore, s_eol, }, // 0x0d <cr>
|
|
59 { s_single, s_term, s_string, s_single, s_eol, }, // 0x0e
|
|
60 { s_single, s_term, s_string, s_single, s_eol, }, // 0x0f
|
|
61 { s_single, s_term, s_string, s_single, s_eol, }, // 0x10
|
|
62 { s_single, s_term, s_string, s_single, s_eol, }, // 0x11 xon char
|
|
63 { s_single, s_term, s_string, s_single, s_eol, }, // 0x12
|
|
64 { s_single, s_term, s_string, s_single, s_eol, }, // 0x13 xoff char
|
|
65 { s_single, s_term, s_string, s_single, s_eol, }, // 0x14
|
|
66 { s_single, s_term, s_string, s_single, s_eol, }, // 0x15
|
|
67 { s_single, s_term, s_string, s_single, s_eol, }, // 0x16
|
|
68 { s_single, s_term, s_string, s_single, s_eol, }, // 0x17
|
|
69 { s_single, s_term, s_string, s_single, s_eol, }, // 0x18
|
|
70 { s_single, s_term, s_string, s_single, s_eol, }, // 0x19
|
|
71 { s_single, s_term, s_string, s_single, s_eol, }, // 0x1a
|
|
72 { s_single, s_term, s_string, s_single, s_eol, }, // 0x1b
|
|
73 { s_single, s_term, s_string, s_single, s_eol, }, // 0x1c
|
|
74 { s_single, s_term, s_string, s_single, s_eol, }, // 0x1d
|
|
75 { s_single, s_term, s_string, s_single, s_eol, }, // 0x1e
|
|
76 { s_single, s_term, s_string, s_single, s_eol, }, // 0x1f
|
|
77 { s_ignore, s_term, s_string, s_ignore, s_eol, }, // 0x20 space
|
|
78 { s_single, s_term, s_string, s_single, s_eol, }, // 0x21 !
|
|
79 { s_string1, s_term, s_string2, s_string1, s_eol, }, // 0x22 "
|
|
80 { s_eol, s_term, s_string, s_eol, s_eol, }, // 0x23 #
|
|
81 { s_single, s_term, s_string, s_single, s_eol, }, // 0x24 $
|
|
82 { s_single, s_term, s_string, s_single, s_eol, }, // 0x25 %
|
|
83 { s_single, s_term, s_string, s_single, s_eol, }, // 0x26 &
|
|
84 { s_single, s_term, s_string, s_single, s_eol, }, // 0x27 '
|
|
85 { s_single, s_term, s_string, s_single, s_eol, }, // 0x28 (
|
|
86 { s_single, s_term, s_string, s_single, s_eol, }, // 0x29 )
|
|
87 { s_single, s_term, s_string, s_single, s_eol, }, // 0x2A *
|
|
88 { s_single, s_token, s_string, s_single, s_eol, }, // 0x2B +
|
|
89 { s_single, s_term, s_string, s_single, s_eol, }, // 0x2C ,
|
|
90 { s_single, s_token, s_string, s_single, s_eol, }, // 0x2D -
|
|
91 { s_single, s_token, s_string, s_single, s_eol, }, // 0x2E .
|
3
|
92 { s_slash, s_term, s_string, s_slash, s_eol, }, // 0x2F /
|
1
|
93 { s_token, s_token, s_string, s_token, s_eol, }, // 0x30 0
|
|
94 { s_token, s_token, s_string, s_token, s_eol, }, // 0x31 1
|
|
95 { s_token, s_token, s_string, s_token, s_eol, }, // 0x32 2
|
|
96 { s_token, s_token, s_string, s_token, s_eol, }, // 0x33 3
|
|
97 { s_token, s_token, s_string, s_token, s_eol, }, // 0x34 4
|
|
98 { s_token, s_token, s_string, s_token, s_eol, }, // 0x35 5
|
|
99 { s_token, s_token, s_string, s_token, s_eol, }, // 0x36 6
|
|
100 { s_token, s_token, s_string, s_token, s_eol, }, // 0x37 7
|
|
101 { s_token, s_token, s_string, s_token, s_eol, }, // 0x38 8
|
|
102 { s_token, s_token, s_string, s_token, s_eol, }, // 0x39 9
|
|
103 { s_single, s_term, s_string, s_single, s_eol, }, // 0x3A :
|
|
104 { s_single, s_term, s_string, s_single, s_eol, }, // 0x3B ;
|
|
105 { s_single, s_term, s_string, s_single, s_eol, }, // 0x3C <
|
|
106 { s_single, s_token, s_string, s_single, s_eol, }, // 0x3D =
|
|
107 { s_single, s_term, s_string, s_single, s_eol, }, // 0x3E >
|
|
108 { s_single, s_term, s_string, s_single, s_eol, }, // 0x3F ?
|
|
109 { s_single, s_token, s_string, s_single, s_eol, }, // 0x40 @
|
|
110 { s_token, s_token, s_string, s_token, s_eol, }, // 0x41 A
|
|
111 { s_token, s_token, s_string, s_token, s_eol, }, // 0x42 B
|
|
112 { s_token, s_token, s_string, s_token, s_eol, }, // 0x43 C
|
|
113 { s_token, s_token, s_string, s_token, s_eol, }, // 0x44 D
|
|
114 { s_token, s_token, s_string, s_token, s_eol, }, // 0x45 E
|
|
115 { s_token, s_token, s_string, s_token, s_eol, }, // 0x46 F
|
|
116 { s_token, s_token, s_string, s_token, s_eol, }, // 0x47 G
|
|
117 { s_token, s_token, s_string, s_token, s_eol, }, // 0x48 H
|
|
118 { s_token, s_token, s_string, s_token, s_eol, }, // 0x49 I
|
|
119 { s_token, s_token, s_string, s_token, s_eol, }, // 0x4A J
|
|
120 { s_token, s_token, s_string, s_token, s_eol, }, // 0x4B K
|
|
121 { s_token, s_token, s_string, s_token, s_eol, }, // 0x4C L
|
|
122 { s_token, s_token, s_string, s_token, s_eol, }, // 0x4D M
|
|
123 { s_token, s_token, s_string, s_token, s_eol, }, // 0x4E N
|
|
124 { s_token, s_token, s_string, s_token, s_eol, }, // 0x4F O
|
|
125 { s_token, s_token, s_string, s_token, s_eol, }, // 0x50 P
|
|
126 { s_token, s_token, s_string, s_token, s_eol, }, // 0x51 Q
|
|
127 { s_token, s_token, s_string, s_token, s_eol, }, // 0x52 R
|
|
128 { s_token, s_token, s_string, s_token, s_eol, }, // 0x53 S
|
|
129 { s_token, s_token, s_string, s_token, s_eol, }, // 0x54 T
|
|
130 { s_token, s_token, s_string, s_token, s_eol, }, // 0x55 U
|
|
131 { s_token, s_token, s_string, s_token, s_eol, }, // 0x56 V
|
|
132 { s_token, s_token, s_string, s_token, s_eol, }, // 0x57 W
|
|
133 { s_token, s_token, s_string, s_token, s_eol, }, // 0x58 X
|
|
134 { s_token, s_token, s_string, s_token, s_eol, }, // 0x59 Y
|
|
135 { s_token, s_token, s_string, s_token, s_eol, }, // 0x5A Z
|
|
136 { s_single, s_term, s_string, s_single, s_eol, }, // 0x5B [
|
|
137 { s_single, s_term, s_string, s_single, s_eol, }, // 0x5C backslash
|
|
138 { s_single, s_term, s_string, s_single, s_eol, }, // 0x5D ]
|
|
139 { s_single, s_term, s_string, s_single, s_eol, }, // 0x5E ^
|
|
140 { s_single, s_token, s_string, s_single, s_eol, }, // 0x5F _
|
|
141 { s_single, s_term, s_string, s_single, s_eol, }, // 0x60 `
|
|
142 { s_token, s_token, s_string, s_token, s_eol, }, // 0x61 a
|
|
143 { s_token, s_token, s_string, s_token, s_eol, }, // 0x62 b
|
|
144 { s_token, s_token, s_string, s_token, s_eol, }, // 0x63 c
|
|
145 { s_token, s_token, s_string, s_token, s_eol, }, // 0x64 d
|
|
146 { s_token, s_token, s_string, s_token, s_eol, }, // 0x65 e
|
|
147 { s_token, s_token, s_string, s_token, s_eol, }, // 0x66 f
|
|
148 { s_token, s_token, s_string, s_token, s_eol, }, // 0x67 g
|
|
149 { s_token, s_token, s_string, s_token, s_eol, }, // 0x68 h
|
|
150 { s_token, s_token, s_string, s_token, s_eol, }, // 0x69 i
|
|
151 { s_token, s_token, s_string, s_token, s_eol, }, // 0x6A j
|
|
152 { s_token, s_token, s_string, s_token, s_eol, }, // 0x6B k
|
|
153 { s_token, s_token, s_string, s_token, s_eol, }, // 0x6C l
|
|
154 { s_token, s_token, s_string, s_token, s_eol, }, // 0x6D m
|
|
155 { s_token, s_token, s_string, s_token, s_eol, }, // 0x6E n
|
|
156 { s_token, s_token, s_string, s_token, s_eol, }, // 0x6F o
|
|
157 { s_token, s_token, s_string, s_token, s_eol, }, // 0x70 p
|
|
158 { s_token, s_token, s_string, s_token, s_eol, }, // 0x71 q
|
|
159 { s_token, s_token, s_string, s_token, s_eol, }, // 0x72 r
|
|
160 { s_token, s_token, s_string, s_token, s_eol, }, // 0x73 s
|
|
161 { s_token, s_token, s_string, s_token, s_eol, }, // 0x74 t
|
|
162 { s_token, s_token, s_string, s_token, s_eol, }, // 0x75 u
|
|
163 { s_token, s_token, s_string, s_token, s_eol, }, // 0x76 v
|
|
164 { s_token, s_token, s_string, s_token, s_eol, }, // 0x77 w
|
|
165 { s_token, s_token, s_string, s_token, s_eol, }, // 0x78 x
|
|
166 { s_token, s_token, s_string, s_token, s_eol, }, // 0x79 y
|
|
167 { s_token, s_token, s_string, s_token, s_eol, }, // 0x7A z
|
|
168 { s_single, s_term, s_string, s_single, s_eol, }, // 0x7B {
|
|
169 { s_single, s_term, s_string, s_single, s_eol, }, // 0x7C |
|
|
170 { s_single, s_term, s_string, s_single, s_eol, }, // 0x7D }
|
|
171 { s_single, s_term, s_string, s_single, s_eol, }, // 0x7E ~
|
|
172 { s_single, s_term, s_string, s_single, s_eol, }, // 0x7f
|
|
173 { s_single, s_term, s_string, s_single, s_eol, }, // 0x80
|
|
174 { s_single, s_term, s_string, s_single, s_eol, }, // 0x81
|
|
175 { s_single, s_term, s_string, s_single, s_eol, }, // 0x82
|
|
176 { s_single, s_term, s_string, s_single, s_eol, }, // 0x83
|
|
177 { s_single, s_term, s_string, s_single, s_eol, }, // 0x84
|
|
178 { s_single, s_term, s_string, s_single, s_eol, }, // 0x85
|
|
179 { s_single, s_term, s_string, s_single, s_eol, }, // 0x86
|
|
180 { s_single, s_term, s_string, s_single, s_eol, }, // 0x87
|
|
181 { s_single, s_term, s_string, s_single, s_eol, }, // 0x88
|
|
182 { s_single, s_term, s_string, s_single, s_eol, }, // 0x89
|
|
183 { s_single, s_term, s_string, s_single, s_eol, }, // 0x8a
|
|
184 { s_single, s_term, s_string, s_single, s_eol, }, // 0x8b
|
|
185 { s_single, s_term, s_string, s_single, s_eol, }, // 0x8c
|
|
186 { s_single, s_term, s_string, s_single, s_eol, }, // 0x8d
|
|
187 { s_single, s_term, s_string, s_single, s_eol, }, // 0x8e
|
|
188 { s_single, s_term, s_string, s_single, s_eol, }, // 0x8f
|
|
189 { s_single, s_term, s_string, s_single, s_eol, }, // 0x90
|
|
190 { s_single, s_term, s_string, s_single, s_eol, }, // 0x91
|
|
191 { s_single, s_term, s_string, s_single, s_eol, }, // 0x92
|
|
192 { s_single, s_term, s_string, s_single, s_eol, }, // 0x93
|
|
193 { s_single, s_term, s_string, s_single, s_eol, }, // 0x94
|
|
194 { s_single, s_term, s_string, s_single, s_eol, }, // 0x95
|
|
195 { s_single, s_term, s_string, s_single, s_eol, }, // 0x96
|
|
196 { s_single, s_term, s_string, s_single, s_eol, }, // 0x97
|
|
197 { s_single, s_term, s_string, s_single, s_eol, }, // 0x98
|
|
198 { s_single, s_term, s_string, s_single, s_eol, }, // 0x99
|
|
199 { s_single, s_term, s_string, s_single, s_eol, }, // 0x9a
|
|
200 { s_single, s_term, s_string, s_single, s_eol, }, // 0x9b
|
|
201 { s_single, s_term, s_string, s_single, s_eol, }, // 0x9c
|
|
202 { s_single, s_term, s_string, s_single, s_eol, }, // 0x9d
|
|
203 { s_single, s_term, s_string, s_single, s_eol, }, // 0x9e
|
|
204 { s_single, s_term, s_string, s_single, s_eol, }, // 0x9f
|
|
205 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa0
|
|
206 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa1
|
|
207 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa2
|
|
208 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa3
|
|
209 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa4
|
|
210 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa5
|
|
211 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa6
|
|
212 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa7
|
|
213 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa8
|
|
214 { s_single, s_term, s_string, s_single, s_eol, }, // 0xa9
|
|
215 { s_single, s_term, s_string, s_single, s_eol, }, // 0xaa
|
|
216 { s_single, s_term, s_string, s_single, s_eol, }, // 0xab
|
|
217 { s_single, s_term, s_string, s_single, s_eol, }, // 0xac
|
|
218 { s_single, s_term, s_string, s_single, s_eol, }, // 0xad
|
|
219 { s_single, s_term, s_string, s_single, s_eol, }, // 0xae
|
|
220 { s_single, s_term, s_string, s_single, s_eol, }, // 0xaf
|
|
221 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb0
|
|
222 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb1
|
|
223 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb2
|
|
224 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb3
|
|
225 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb4
|
|
226 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb5
|
|
227 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb6
|
|
228 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb7
|
|
229 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb8
|
|
230 { s_single, s_term, s_string, s_single, s_eol, }, // 0xb9
|
|
231 { s_single, s_term, s_string, s_single, s_eol, }, // 0xba
|
|
232 { s_single, s_term, s_string, s_single, s_eol, }, // 0xbb
|
|
233 { s_single, s_term, s_string, s_single, s_eol, }, // 0xbc
|
|
234 { s_single, s_term, s_string, s_single, s_eol, }, // 0xbd
|
|
235 { s_single, s_term, s_string, s_single, s_eol, }, // 0xbe
|
|
236 { s_single, s_term, s_string, s_single, s_eol, }, // 0xbf
|
|
237 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc0
|
|
238 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc1
|
|
239 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc2
|
|
240 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc3
|
|
241 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc4
|
|
242 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc5
|
|
243 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc6
|
|
244 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc7
|
|
245 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc8
|
|
246 { s_single, s_term, s_string, s_single, s_eol, }, // 0xc9
|
|
247 { s_single, s_term, s_string, s_single, s_eol, }, // 0xca
|
|
248 { s_single, s_term, s_string, s_single, s_eol, }, // 0xcb
|
|
249 { s_single, s_term, s_string, s_single, s_eol, }, // 0xcc
|
|
250 { s_single, s_term, s_string, s_single, s_eol, }, // 0xcd
|
|
251 { s_single, s_term, s_string, s_single, s_eol, }, // 0xce
|
|
252 { s_single, s_term, s_string, s_single, s_eol, }, // 0xcf
|
|
253 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd0
|
|
254 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd1
|
|
255 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd2
|
|
256 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd3
|
|
257 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd4
|
|
258 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd5
|
|
259 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd6
|
|
260 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd7
|
|
261 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd8
|
|
262 { s_single, s_term, s_string, s_single, s_eol, }, // 0xd9
|
|
263 { s_single, s_term, s_string, s_single, s_eol, }, // 0xda
|
|
264 { s_single, s_term, s_string, s_single, s_eol, }, // 0xdb
|
|
265 { s_single, s_term, s_string, s_single, s_eol, }, // 0xdc
|
|
266 { s_single, s_term, s_string, s_single, s_eol, }, // 0xdd
|
|
267 { s_single, s_term, s_string, s_single, s_eol, }, // 0xde
|
|
268 { s_single, s_term, s_string, s_single, s_eol, }, // 0xdf
|
|
269 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe0
|
|
270 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe1
|
|
271 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe2
|
|
272 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe3
|
|
273 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe4
|
|
274 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe5
|
|
275 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe6
|
|
276 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe7
|
|
277 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe8
|
|
278 { s_single, s_term, s_string, s_single, s_eol, }, // 0xe9
|
|
279 { s_single, s_term, s_string, s_single, s_eol, }, // 0xea
|
|
280 { s_single, s_term, s_string, s_single, s_eol, }, // 0xeb
|
|
281 { s_single, s_term, s_string, s_single, s_eol, }, // 0xec
|
|
282 { s_single, s_term, s_string, s_single, s_eol, }, // 0xed
|
|
283 { s_single, s_term, s_string, s_single, s_eol, }, // 0xee
|
|
284 { s_single, s_term, s_string, s_single, s_eol, }, // 0xef
|
|
285 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf0
|
|
286 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf1
|
|
287 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf2
|
|
288 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf3
|
|
289 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf4
|
|
290 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf5
|
|
291 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf6
|
|
292 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf7
|
|
293 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf8
|
|
294 { s_single, s_term, s_string, s_single, s_eol, }, // 0xf9
|
|
295 { s_single, s_term, s_string, s_single, s_eol, }, // 0xfa
|
|
296 { s_single, s_term, s_string, s_single, s_eol, }, // 0xfb
|
|
297 { s_single, s_term, s_string, s_single, s_eol, }, // 0xfc
|
|
298 { s_single, s_term, s_string, s_single, s_eol, }, // 0xfd
|
|
299 { s_single, s_term, s_string, s_single, s_eol, }, // 0xfe
|
|
300 { s_single, s_term, s_string, s_single, s_eol, }, // 0xff
|
|
301 };
|
|
302
|
|
303
|
|
304 TOKEN::TOKEN(char *fn, string_set *includes) {
|
|
305 pushed = false;
|
|
306 include_files = includes;
|
|
307 include(fn);
|
|
308 }
|
|
309
|
|
310
|
|
311 TOKEN::~TOKEN() {
|
|
312 while (!streams.empty()) pop();
|
|
313 }
|
|
314
|
|
315
|
|
316 void TOKEN::pop() {
|
|
317 ifstream *is = streams.front();
|
|
318 char *fn = filenames.front();
|
|
319 streams.pop_front();
|
|
320 filenamess.erase(fn);
|
|
321 if (filenames.size() > 1) filenames.pop_front();
|
|
322 if (linenumbers.size() > 1) linenumbers.pop_front();
|
|
323 is->close();
|
|
324 delete is;
|
|
325 }
|
|
326
|
|
327
|
|
328 void TOKEN::push_char(u_char c) {
|
|
329 pushed = true;
|
|
330 pushed_char = c;
|
|
331 }
|
|
332
|
|
333
|
|
334 bool TOKEN::next_char(u_char &uc) {
|
|
335 if (pushed) {
|
27
|
336 //uc = (u_char)tolower((char)pushed_char);
|
|
337 uc = pushed_char;
|
1
|
338 pushed = false;
|
|
339 return true;
|
|
340 }
|
|
341 while (!streams.empty() && streams.front()->eof()) {
|
|
342 pop();
|
|
343 }
|
|
344 if (streams.empty()) return false;
|
|
345 ifstream *is = streams.front();
|
|
346 uc = (u_char)is->get();
|
|
347 if (is->eof()) return next_char(uc);
|
|
348 if (uc == (u_char)'\n') {
|
|
349 int &line = linenumbers.front();
|
|
350 line++;
|
|
351 }
|
27
|
352 //uc = (u_char)tolower((char)uc);
|
1
|
353 return true;
|
|
354 }
|
|
355
|
|
356
|
|
357 bool TOKEN::include(char *fn) {
|
|
358 string_set::iterator i = filenamess.find(fn);
|
|
359 if (i != filenamess.end()) {
|
|
360 token_error("redundant or recursive include file detected");
|
|
361 return false;
|
|
362 }
|
|
363 ifstream *is = new ifstream;
|
|
364 is->open(fn);
|
|
365 if (is->fail()) {
|
|
366 char buf[maxlen];
|
|
367 snprintf(buf, sizeof(buf), "include file %s not found", fn);
|
|
368 token_error(buf);
|
|
369 return false;
|
|
370 }
|
|
371 string_set &inc = *include_files;
|
|
372 inc.insert(fn);
|
|
373 streams.push_front(is);
|
|
374 filenames.push_front(fn);
|
|
375 filenamess.insert(fn);
|
|
376 linenumbers.push_front(1);
|
|
377 return true;
|
|
378 }
|
|
379
|
|
380
|
|
381 char *TOKEN::next() {
|
|
382 if (!pending_tokens.empty()) {
|
|
383 char *t = pending_tokens.front();
|
|
384 pending_tokens.pop_front();
|
|
385 return t;
|
|
386 }
|
|
387 if (streams.empty()) return NULL;
|
|
388 const int PENDING_LIMIT = 1000;
|
|
389 static u_char buffer[PENDING_LIMIT];
|
|
390 int count = 0;
|
|
391 state st = s_init;
|
|
392 while (true) {
|
|
393 if (count == (PENDING_LIMIT-1)) {
|
|
394 token_error("token too long");
|
|
395 break;
|
|
396 }
|
|
397 if (st >= end_state) {
|
|
398 token_error("finite state machine error");
|
|
399 break;
|
|
400 }
|
|
401 u_char c;
|
|
402 if (!next_char(c)) break;
|
|
403 st = parse_table[c][st];
|
|
404 switch (st) {
|
|
405 case s_string:
|
|
406 case s_token: {
|
|
407 buffer[count++] = c;
|
|
408 } break;
|
|
409
|
|
410 case s_term: {
|
|
411 push_char(c);
|
|
412 st = s_init;
|
|
413 } break;
|
|
414
|
|
415 case s_string1: {
|
|
416 st = s_string;
|
|
417 } break;
|
|
418
|
|
419 case s_string2: {
|
|
420 st = s_init;
|
|
421 } break;
|
|
422
|
|
423 case s_single: {
|
|
424 buffer[count++] = c;
|
|
425 st = s_init;
|
|
426 } break;
|
|
427
|
|
428 case s_ignore:
|
|
429 case s_eol: {
|
|
430 } break;
|
|
431
|
|
432
|
|
433 case s_slash: {
|
|
434 buffer[count++] = c;
|
|
435 if (next_char(c)) {
|
|
436 if (c == (u_char)'/') {
|
|
437 // start of ignore to eol on //
|
|
438 count--;
|
|
439 st = s_eol;
|
|
440 }
|
|
441 else {
|
|
442 // not a // token, just return this single /
|
|
443 push_char(c);
|
|
444 st = s_init;
|
|
445 }
|
|
446 }
|
|
447 else {
|
|
448 // cannot get another char
|
|
449 st = s_init;
|
|
450 }
|
|
451 } break;
|
|
452
|
|
453 default: {
|
|
454 token_error();
|
|
455 token_error("unknown state %d %s \n", st, " ");
|
|
456 } break;
|
|
457 }
|
|
458 if (st == s_init) break;
|
|
459 }
|
|
460
|
|
461 buffer[count] = '\0';
|
|
462 if (count == 0) return NULL;
|
|
463 char *t = register_string((char*)buffer);
|
|
464 if (t == token_include) {
|
|
465 char *f = next(); // should be file name
|
|
466 char *s = next(); // should be semicolon
|
|
467 if (s == token_semi) {
|
|
468 include(f);
|
|
469 return next();
|
|
470 }
|
|
471 else {
|
|
472 push(s);
|
|
473 push(f);
|
|
474 return t;
|
|
475 }
|
|
476 }
|
|
477 return t;
|
|
478 }
|
|
479
|
|
480
|
|
481 int TOKEN::nextint() {
|
|
482 char *t = next();
|
|
483 char *e;
|
|
484 long i = strtol(t, &e, 10);
|
|
485 if (*e != '\0') {
|
|
486 token_error("integer", t);
|
|
487 return 0;
|
|
488 }
|
|
489 return (int)i;
|
|
490 }
|
|
491
|
|
492
|
|
493 void TOKEN::skipeol() {
|
|
494 while (true) {
|
|
495 u_char c;
|
|
496 if (!next_char(c)) break;
|
|
497 if (c == (u_char)'\n') break;
|
|
498 }
|
|
499 }
|
|
500
|
|
501
|
|
502 void TOKEN::token_error(const char *err) {
|
|
503 token_error();
|
|
504 char buf[maxlen];
|
|
505 snprintf(buf, sizeof(buf), "%s \n", err);
|
|
506 my_syslog(buf);
|
|
507 }
|
|
508
|
|
509
|
|
510 void TOKEN::token_error(const char *fmt, int d, const char *s) {
|
|
511 char buf[maxlen];
|
|
512 snprintf(buf, sizeof(buf), fmt, d, s);
|
|
513 my_syslog(buf);
|
|
514 }
|
|
515
|
|
516
|
|
517 void TOKEN::token_error(const char *fmt, const char *t, const char *h) {
|
|
518 if (!h) h = "null";
|
|
519 char buf[maxlen];
|
|
520 snprintf(buf, sizeof(buf), fmt, t, h);
|
|
521 my_syslog(buf);
|
|
522 }
|
|
523
|
|
524
|
|
525 void TOKEN::token_error(const char *want, const char *have) {
|
|
526 token_error();
|
3
|
527 token_error("expecting %s, found %s", want, have);
|
1
|
528 }
|
|
529
|
|
530
|
|
531 void TOKEN::token_error() {
|
|
532 token_error("syntax error at line %d in file %s -- ", cur_line(), cur_fn());
|
|
533 line_list::iterator j = linenumbers.begin();
|
|
534 string_list::iterator i = filenames.begin();
|
|
535 for (; i!=filenames.end(); i++,j++) {
|
|
536 if (i != filenames.begin()) {
|
|
537 char *fn = (*i);
|
|
538 int li = (*j);
|
3
|
539 token_error(" included from line %d in file %s -- ", li, fn);
|
1
|
540 }
|
|
541 }
|
|
542 }
|
|
543
|