nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* source: nestlex.c */ |
2 | /* Copyright Gerhard Rieger 2006-2010 */ |
||
3 | /* Published under the GNU General Public License V.2, see file COPYING */ |
||
4 | |||
5 | /* a function for lexical scanning of nested character patterns */ |
||
6 | |||
7 | #include "config.h" |
||
8 | #include "mytypes.h" |
||
9 | |||
10 | #include "sysincludes.h" |
||
11 | |||
12 | |||
13 | /* sub: scan a string and copy its value to output string |
||
14 | end scanning when an unescaped, unnested string from ends array is found |
||
15 | does not copy the end pattern |
||
16 | does not write a trailing \0 to token |
||
17 | allows escaping with \ and quoting (\ and quotes are removed) |
||
18 | allows nesting with div. parens |
||
19 | returns -1 if out string was too small |
||
20 | returns 1 if addr ended unexpectedly |
||
21 | returns 0 if token could be extracted successfully |
||
22 | */ |
||
23 | int nestlex(const char **addr, /* input string; aft points to end token */ |
||
24 | char **token, /* output token; aft points to first unwritten |
||
25 | char (caller might want to set it to \0) */ |
||
26 | size_t *len, /* remaining bytes in token space (incl. \0) */ |
||
27 | const char *ends[], /* list of end strings */ |
||
28 | const char *hquotes[],/* list of strings that quote (hard qu.) */ |
||
29 | const char *squotes[],/* list of strings that quote softly */ |
||
30 | const char *nests[],/* list of strings that start nesting; |
||
31 | every second one is matching end */ |
||
32 | bool dropspace, /* drop trailing space before end token */ |
||
33 | bool dropquotes, /* drop the outermost quotes */ |
||
34 | bool c_esc, /* solve C char escapes: \n \t \0 etc */ |
||
35 | bool html_esc /* solve HTML char escapes: %0d %08 etc */ |
||
36 | ) { |
||
37 | const char *in = *addr; /* pointer into input string */ |
||
38 | const char **endx; /* loops over end patterns */ |
||
39 | const char **quotx; /* loops over quote patterns */ |
||
40 | const char **nestx; /* loops over nest patterns */ |
||
41 | char *out = *token; /* pointer into output token */ |
||
42 | char *lastnonspace = out; |
||
43 | char c; |
||
44 | int i; |
||
45 | int result; |
||
46 | |||
47 | while (true) { |
||
48 | |||
49 | /* is this end of input string? */ |
||
50 | if (*in == 0) { |
||
51 | if (dropspace) { |
||
52 | out = lastnonspace; |
||
53 | } |
||
54 | break; /* end of string */ |
||
55 | } |
||
56 | |||
57 | /* first check the end patterns (e.g. for ']') */ |
||
58 | endx = ends; i = 0; |
||
59 | while (*endx) { |
||
60 | if (!strncmp(in, *endx, strlen(*endx))) { |
||
61 | /* this end pattern matches */ |
||
62 | if (dropspace) { |
||
63 | out = lastnonspace; |
||
64 | } |
||
65 | *addr = in; |
||
66 | *token = out; |
||
67 | return 0; |
||
68 | } |
||
69 | ++endx; |
||
70 | } |
||
71 | |||
72 | /* check for hard quoting pattern */ |
||
73 | quotx = hquotes; |
||
74 | while (hquotes && *quotx) { |
||
75 | if (!strncmp(in, *quotx, strlen(*quotx))) { |
||
76 | /* this quote pattern matches */ |
||
77 | const char *endnest[2]; |
||
78 | if (dropquotes) { |
||
79 | /* we strip this quote */ |
||
80 | in += strlen(*quotx); |
||
81 | } else { |
||
82 | for (i = strlen(*quotx); i > 0; --i) { |
||
83 | *out++ = *in++; |
||
84 | if (--*len <= 0) { *addr = in; *token = out; return -1; } |
||
85 | } |
||
86 | } |
||
87 | /* we call nestlex recursively */ |
||
88 | endnest[0] = *quotx; |
||
89 | endnest[1] = NULL; |
||
90 | result = |
||
91 | nestlex(&in, &out, len, endnest, NULL/*hquotes*/, |
||
92 | NULL/*squotes*/, NULL/*nests*/, |
||
93 | false, false, c_esc, html_esc); |
||
94 | if (result == 0 && dropquotes) { |
||
95 | /* we strip this quote */ |
||
96 | in += strlen(*quotx); |
||
97 | } else { |
||
98 | /* we copy the trailing quote */ |
||
99 | for (i = strlen(*quotx); i > 0; --i) { |
||
100 | *out++ = *in++; |
||
101 | if (--*len <= 0) { *addr = in; *token = out; return -1; } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | break; |
||
106 | } |
||
107 | ++quotx; |
||
108 | } |
||
109 | if (hquotes && *quotx != NULL) { |
||
110 | /* there was a quote; string might continue with hard quote */ |
||
111 | lastnonspace = out; |
||
112 | continue; |
||
113 | } |
||
114 | |||
115 | /* check for soft quoting pattern */ |
||
116 | quotx = squotes; |
||
117 | while (squotes && *quotx) { |
||
118 | if (!strncmp(in, *quotx, strlen(*quotx))) { |
||
119 | /* this quote pattern matches */ |
||
120 | /* we strip this quote */ |
||
121 | /* we call nestlex recursively */ |
||
122 | const char *endnest[2]; |
||
123 | if (dropquotes) { |
||
124 | /* we strip this quote */ |
||
125 | in += strlen(*quotx); |
||
126 | } else { |
||
127 | for (i = strlen(*quotx); i > 0; --i) { |
||
128 | *out++ = *in++; |
||
129 | if (--*len <= 0) { *addr = in; *token = out; return -1; } |
||
130 | } |
||
131 | } |
||
132 | endnest[0] = *quotx; |
||
133 | endnest[1] = NULL; |
||
134 | result = |
||
135 | nestlex(&in, &out, len, endnest, hquotes, |
||
136 | squotes, nests, |
||
137 | false, false, c_esc, html_esc); |
||
138 | |||
139 | if (result == 0 && dropquotes) { |
||
140 | /* we strip the trailing quote */ |
||
141 | in += strlen(*quotx); |
||
142 | } else { |
||
143 | /* we copy the trailing quote */ |
||
144 | for (i = strlen(*quotx); i > 0; --i) { |
||
145 | *out++ = *in++; |
||
146 | if (--*len <= 0) { *addr = in; *token = out; return -1; } |
||
147 | } |
||
148 | } |
||
149 | break; |
||
150 | } |
||
151 | ++quotx; |
||
152 | } |
||
153 | if (squotes && *quotx != NULL) { |
||
154 | /* there was a soft quote; string might continue with any quote */ |
||
155 | lastnonspace = out; |
||
156 | continue; |
||
157 | } |
||
158 | |||
159 | /* check patterns that start a nested clause */ |
||
160 | nestx = nests; i = 0; |
||
161 | while (nests && *nestx) { |
||
162 | if (!strncmp(in, *nestx, strlen(*nestx))) { |
||
163 | /* this nest pattern matches */ |
||
164 | const char *endnest[2]; |
||
165 | endnest[0] = nestx[1]; |
||
166 | endnest[1] = NULL; |
||
167 | |||
168 | for (i = strlen(nestx[1]); i > 0; --i) { |
||
169 | *out++ = *in++; |
||
170 | if (--*len <= 0) { *addr = in; *token = out; return -1; } |
||
171 | } |
||
172 | |||
173 | result = |
||
174 | nestlex(&in, &out, len, endnest, hquotes, squotes, nests, |
||
175 | false, false, c_esc, html_esc); |
||
176 | if (result == 0) { |
||
177 | /* copy endnest */ |
||
178 | i = strlen(nestx[1]); while (i > 0) { |
||
179 | *out++ = *in++; |
||
180 | if (--*len <= 0) { |
||
181 | *addr = in; |
||
182 | *token = out; |
||
183 | return -1; |
||
184 | } |
||
185 | --i; |
||
186 | } |
||
187 | } |
||
188 | break; |
||
189 | } |
||
190 | nestx += 2; /* skip matching end pattern in table */ |
||
191 | } |
||
192 | if (nests && *nestx) { |
||
193 | /* we handled a nested expression, continue loop */ |
||
194 | lastnonspace = out; |
||
195 | continue; |
||
196 | } |
||
197 | |||
198 | /* "normal" data, possibly escaped */ |
||
199 | c = *in++; |
||
200 | if (c == '\\') { |
||
201 | /* found a plain \ escaped part */ |
||
202 | c = *in++; |
||
203 | if (c == 0) { /* Warn("trailing '\\'");*/ break; } |
||
204 | if (c_esc) { /* solve C char escapes: \n \t \0 etc */ |
||
205 | switch (c) { |
||
206 | case '0': c = '\0'; break; |
||
207 | case 'a': c = '\a'; break; |
||
208 | case 'b': c = '\b'; break; |
||
209 | case 'f': c = '\f'; break; |
||
210 | case 'n': c = '\n'; break; |
||
211 | case 'r': c = '\r'; break; |
||
212 | case 't': c = '\t'; break; |
||
213 | case 'v': c = '\v'; break; |
||
214 | #if LATER |
||
215 | case 'x': !!! 1 to 2 hex digits; break; |
||
216 | case 'u': !!! 4 hex digits?; break; |
||
217 | case 'U': !!! 8 hex digits?; break; |
||
218 | #endif |
||
219 | default: break; |
||
220 | } |
||
221 | } |
||
222 | *out++ = c; |
||
223 | --*len; |
||
224 | if (*len == 0) { |
||
225 | *addr = in; |
||
226 | *token = out; |
||
227 | return -1; /* output overflow */ |
||
228 | } |
||
229 | lastnonspace = out; |
||
230 | continue; |
||
231 | } |
||
232 | |||
233 | /* just a simple char */ |
||
234 | *out++ = c; |
||
235 | --*len; |
||
236 | if (*len == 0) { |
||
237 | *addr = in; |
||
238 | *token = out; |
||
239 | return -1; /* output overflow */ |
||
240 | } |
||
241 | if (!isspace(c)) { |
||
242 | lastnonspace = out; |
||
243 | } |
||
244 | |||
245 | } |
||
246 | /* never come here? */ |
||
247 | |||
248 | *addr = in; |
||
249 | *token = out; |
||
250 | return 0; /* OK */ |
||
251 | } |
||
252 | |||
253 | int skipsp(const char **text) { |
||
254 | while (isspace(**text)) { |
||
255 | ++(*text); |
||
256 | } |
||
257 | return 0; |
||
258 | } |