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