Debian Codegraph @

common/parser.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 *
5 * Mini-parser.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <assert.h>
18 #include <stdarg.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23
24 #include "utils.h"
25 #include "parser.h"
26
27 #define TOKEN_MAX_SIZE 512
28
29 /* Character types */
30 enum {
31 PARSER_CHAR_BLANK,
32 PARSER_CHAR_NEWLINE,
33 PARSER_CHAR_COMMENT,
34 PARSER_CHAR_QUOTE,
35 PARSER_CHAR_OTHER,
36 };
37
38 /* Get a description given an error code */
39 char *parser_strerror(parser_context_t *ctx)
40 {
41 printf("error = %d\n",ctx->error);
42
43 switch(ctx->error) {
44 case 0:
45 return "no error";
46 case PARSER_ERROR_NOMEM:
47 return "insufficient memory";
48 case PARSER_ERROR_UNEXP_QUOTE:
49 return "unexpected quote";
50 case PARSER_ERROR_UNEXP_EOL:
51 return "unexpected end of line";
52 default:
53 return "unknown error";
54 }
55 }
56
57 /* Dump a token list */
58 void parser_dump_tokens(parser_context_t *ctx)
59 {
60 parser_token_t *tok;
61
62 for(tok=ctx->tok_head;tok;tok=tok->next)
63 printf("\"%s\" ",tok->value);
64 }
65
66 /* Map a token list to an array */
67 char **parser_map_array(parser_context_t *ctx)
68 {
69 parser_token_t *tok;
70 char **map;
71 int i;
72
73 if (ctx->tok_count <= 0)
74 return NULL;
75
76 if (!(map = calloc(ctx->tok_count,sizeof(char **))))
77 return NULL;
78
79 for(i=0,tok=ctx->tok_head;(i<ctx->tok_count) && tok;i++,tok=tok->next)
80 map[i] = tok->value;
81
82 return map;
83 }
84
85 /* Add a character to temporary token (resize if necessary) */
86 static int tmp_token_add_char(parser_context_t *ctx,char c)
87 {
88 size_t new_size;
89 char *new_str;
90
91 if (!ctx->tmp_tok || (ctx->tmp_cur_len == (ctx->tmp_tot_len - 1))) {
92 new_size = ctx->tmp_tot_len + TOKEN_MAX_SIZE;
93 new_str = realloc(ctx->tmp_tok,new_size);
94
95 if (!new_str)
96 return(-1);
97
98 ctx->tmp_tok = new_str;
99 ctx->tmp_tot_len = new_size;
100 }
101
102 ctx->tmp_tok[ctx->tmp_cur_len++] = c;
103 ctx->tmp_tok[ctx->tmp_cur_len] = 0;
104 return(0);
105 }
106
107 /* Move current token to the active token list */
108 static int parser_move_tmp_token(parser_context_t *ctx)
109 {
110 parser_token_t *tok;
111
112 /* no token ... */
113 if (!ctx->tmp_tok)
114 return(0);
115
116 if (!(tok = malloc(sizeof(*tok))))
117 return(-1);
118
119 tok->value = ctx->tmp_tok;
120 tok->next = NULL;
121
122 /* add it to the token list */
123 if (ctx->tok_last != NULL)
124 ctx->tok_last->next = tok;
125 else
126 ctx->tok_head = tok;
127
128 ctx->tok_last = tok;
129 ctx->tok_count++;
130
131 /* start a new token */
132 ctx->tmp_tok = NULL;
133 ctx->tmp_tot_len = ctx->tmp_cur_len = 0;
134 return(0);
135 }
136
137 /* Initialize parser context */
138 void parser_context_init(parser_context_t *ctx)
139 {
140 ctx->tok_head = ctx->tok_last = NULL;
141 ctx->tok_count = 0;
142
143 ctx->tmp_tok = NULL;
144 ctx->tmp_tot_len = ctx->tmp_cur_len = 0;
145
146 ctx->state = PARSER_STATE_BLANK;
147 ctx->error = 0;
148
149 ctx->consumed_len = 0;
150 }
151
152 /* Free a token list */
153 void parser_free_tokens(parser_token_t *tok_list)
154 {
155 parser_token_t *t,*next;
156
157 for(t=tok_list;t;t=next) {
158 next = t->next;
159 free(t->value);
160 free(t);
161 }
162 }
163
164 /* Free memory used by a parser context */
165 void parser_context_free(parser_context_t *ctx)
166 {
167 parser_free_tokens(ctx->tok_head);
168
169 if (ctx->tmp_tok != NULL)
170 free(ctx->tmp_tok);
171
172 parser_context_init(ctx);
173 }
174
175 /* Determine the type of the input character */
176 static int parser_get_char_type(u_char c)
177 {
178 switch(c) {
179 case '\n':
180 case '\r':
181 case 0:
182 return(PARSER_CHAR_NEWLINE);
183 case '\t':
184 //case '\r':
185 case ' ':
186 return(PARSER_CHAR_BLANK);
187 case '!':
188 case '#':
189 return(PARSER_CHAR_COMMENT);
190 case '"':
191 return(PARSER_CHAR_QUOTE);
192 default:
193 return(PARSER_CHAR_OTHER);
194 }
195 }
196
197 /* Send a buffer to the tokenizer */
198 int parser_scan_buffer(parser_context_t *ctx,char *buf,size_t buf_size)
199 {
200 int i,type;
201 u_char c;
202
203 for(i=0;(i<buf_size) && (ctx->state != PARSER_STATE_DONE);i++)
204 {
205 ctx->consumed_len++;
206 c = buf[i];
207
208 /* Determine character type */
209 type = parser_get_char_type(c);
210
211 /* Basic finite state machine */
212 switch(ctx->state) {
213 case PARSER_STATE_SKIP:
214 if (type == PARSER_CHAR_NEWLINE)
215 ctx->state = PARSER_STATE_DONE;
216
217 /* Simply ignore character until we reach end of line */
218 break;
219
220 case PARSER_STATE_BLANK:
221 switch(type) {
222 case PARSER_CHAR_BLANK:
223 /* Eat space */
224 break;
225
226 case PARSER_CHAR_COMMENT:
227 ctx->state = PARSER_STATE_SKIP;
228 break;
229
230 case PARSER_CHAR_NEWLINE:
231 ctx->state = PARSER_STATE_DONE;
232 break;
233
234 case PARSER_CHAR_QUOTE:
235 ctx->state = PARSER_STATE_QUOTED_STRING;
236 break;
237
238 default:
239 /* Begin a new string */
240 if (!tmp_token_add_char(ctx,c)) {
241 ctx->state = PARSER_STATE_STRING;
242 } else {
243 ctx->state = PARSER_STATE_SKIP;
244 ctx->error = PARSER_ERROR_NOMEM;
245 }
246 }
247 break;
248
249 case PARSER_STATE_STRING:
250 switch(type) {
251 case PARSER_CHAR_BLANK:
252 if (!parser_move_tmp_token(ctx)) {
253 ctx->state = PARSER_STATE_BLANK;
254 } else {
255 ctx->state = PARSER_STATE_SKIP;
256 ctx->error = PARSER_ERROR_NOMEM;
257 }
258 break;
259
260 case PARSER_CHAR_NEWLINE:
261 if (parser_move_tmp_token(ctx) == -1)
262 ctx->error = PARSER_ERROR_NOMEM;
263
264 ctx->state = PARSER_STATE_DONE;
265 break;
266
267 case PARSER_CHAR_COMMENT:
268 if (parser_move_tmp_token(ctx) == -1)
269 ctx->error = PARSER_ERROR_NOMEM;
270
271 ctx->state = PARSER_STATE_SKIP;
272 break;
273
274 case PARSER_CHAR_QUOTE:
275 ctx->error = PARSER_ERROR_UNEXP_QUOTE;
276 ctx->state = PARSER_STATE_SKIP;
277 break;
278
279 default:
280 /* Add the character to the buffer */
281 if (tmp_token_add_char(ctx,c) == -1) {
282 ctx->state = PARSER_STATE_SKIP;
283 ctx->error = PARSER_ERROR_NOMEM;
284 }
285 }
286 break;
287
288 case PARSER_STATE_QUOTED_STRING:
289 switch(type) {
290 case PARSER_CHAR_NEWLINE:
291 /* Unterminated string! */
292 ctx->error = PARSER_ERROR_UNEXP_EOL;
293 ctx->state = PARSER_STATE_DONE;
294 break;
295
296 case PARSER_CHAR_QUOTE:
297 if (!parser_move_tmp_token(ctx)) {
298 ctx->state = PARSER_STATE_BLANK;
299 } else {
300 ctx->state = PARSER_STATE_SKIP;
301 ctx->error = PARSER_ERROR_NOMEM;
302 }
303 break;
304
305 default:
306 /* Add the character to the buffer */
307 if (tmp_token_add_char(ctx,c) == -1) {
308 ctx->state = PARSER_STATE_SKIP;
309 ctx->error = PARSER_ERROR_NOMEM;
310 }
311 }
312 break;
313 }
314 }
315
316 return(ctx->state == PARSER_STATE_DONE);
317 }
318
319 /* Parser tests */
320 static char *parser_test_str[] = {
321 "c7200 show_hardware R1",
322 "c7200 show_hardware \"R1\"",
323 " c7200 show_hardware \"R1\" ",
324 "\"c7200\" \"show_hardware\" \"R1\"",
325 "hypervisor set_working_dir \"C:\\Program Files\\Dynamips Test\"",
326 "hypervisor # This is a comment set_working_dir \"C:\\Program Files\"",
327 "\"c7200\" \"show_hardware\" \"R1",
328 NULL,
329 };
330
331 void parser_run_tests(void)
332 {
333 parser_context_t ctx;
334 int i,res;
335
336 for(i=0;parser_test_str[i];i++) {
337 parser_context_init(&ctx);
338
339 res = parser_scan_buffer(&ctx,parser_test_str[i],
340 strlen(parser_test_str[i])+1);
341
342 printf("\n%d: Test string: [%s] => res=%d, state=%d\n",
343 i,parser_test_str[i],res,ctx.state);
344
345 if ((res != 0) && (ctx.error == 0)) {
346 if (ctx.tok_head) {
347 printf("Tokens: ");
348 parser_dump_tokens(&ctx);
349 printf("\n");
350 }
351 }
352
353 parser_context_free(&ctx);
354 }
355 }