2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vlib/parse.h>
16 #include <vlib/unix/unix.h>
18 static u8 * format_value_v4_address (u8 * s, va_list * args)
20 vlib_parse_value_t *v = va_arg (*args, vlib_parse_value_t *);
21 u32 a = v->value.as_uword;
23 s = format (s, "%d.%d.%d.%d",
32 static vlib_parse_match_t
33 v4_address_match (vlib_parse_main_t *pm, vlib_parse_type_t *type,
34 vlib_lex_token_t *t, vlib_parse_value_t *valuep)
40 if (vec_len (pm->tokens) - (t - pm->tokens) < 7)
41 return VLIB_PARSE_MATCH_FAIL;
43 /* NUMBER DOT NUMBER DOT NUMBER DOT NUMBER */
45 for (i = 0; i < 7; i++) {
47 if (t[i].token != VLIB_LEX_number)
48 return VLIB_PARSE_MATCH_FAIL;
49 if (t[i].value.as_uword > 0xff)
50 return VLIB_PARSE_MATCH_FAIL;
51 digit = t[i].value.as_uword;
52 value = (value << 8) | digit;
54 if (t[i].token != VLIB_LEX_dot)
55 return VLIB_PARSE_MATCH_FAIL;
58 /* note: caller advances by 1 */
59 pm->current_token_index += 6;
60 valuep->value.as_uword = value;
61 return VLIB_PARSE_MATCH_VALUE;
64 PARSE_TYPE_INIT (v4_address, v4_address_match, 0, format_value_v4_address)
66 static u8 * format_value_v4_address_and_mask (u8 * s, va_list * args)
68 vlib_parse_value_t *v = va_arg (*args, vlib_parse_value_t *);
69 u32 * a = v->value.as_pointer;
71 s = format (s, "%d.%d.%d.%d",
76 s = format (s, "/%d", a[1]);
81 static vlib_parse_match_t
82 v4_address_and_mask_match (vlib_parse_main_t *pm, vlib_parse_type_t *type,
83 vlib_lex_token_t *t, vlib_parse_value_t *valuep)
90 if (vec_len (pm->tokens) - (t - pm->tokens) < 9)
91 return VLIB_PARSE_MATCH_FAIL;
93 /* NUMBER DOT NUMBER DOT NUMBER DOT NUMBER */
95 for (i = 0; i < 7; i++) {
97 if (t[i].token != VLIB_LEX_number)
98 return VLIB_PARSE_MATCH_FAIL;
99 if (t[i].value.as_uword > 0xff)
100 return VLIB_PARSE_MATCH_FAIL;
101 digit = t[i].value.as_uword;
102 address = (address << 8) | digit;
104 if (t[i].token != VLIB_LEX_dot)
105 return VLIB_PARSE_MATCH_FAIL;
109 if (t[7].token != VLIB_LEX_slash || t[8].token != VLIB_LEX_number)
110 return VLIB_PARSE_MATCH_FAIL;
112 vec_add1 (rv, address);
113 vec_add1 (rv, t[8].value.as_uword);
115 /* note: caller advances by 1 */
116 pm->current_token_index += 8;
117 valuep->value.as_pointer = rv;
118 return VLIB_PARSE_MATCH_VALUE;
121 void v4_address_and_mask_cleanup (vlib_parse_value_t *valuep)
123 u32 * trash = valuep->value.as_pointer;
127 PARSE_TYPE_INIT (v4_address_and_mask, v4_address_and_mask_match,
128 v4_address_and_mask_cleanup,
129 format_value_v4_address_and_mask)
131 vlib_lex_main_t vlib_lex_main;
135 vlib_parse_match_t eval_factor0 (vlib_parse_main_t *pm,
136 vlib_parse_item_t *item,
137 vlib_parse_value_t *value)
139 clib_warning ("%U", format_vlib_parse_value, pm);
140 return VLIB_PARSE_MATCH_RULE;
142 vlib_parse_match_t eval_factor1 (vlib_parse_main_t *pm,
143 vlib_parse_item_t *item,
144 vlib_parse_value_t *value)
146 clib_warning ("%U", format_vlib_parse_value, pm);
147 return VLIB_PARSE_MATCH_RULE;
149 vlib_parse_match_t eval_factor2 (vlib_parse_main_t *pm,
150 vlib_parse_item_t *item,
151 vlib_parse_value_t *value)
154 int index = vec_len (pm->parse_value)-1;
156 a = pm->parse_value [index].value.as_word;
158 pm->parse_value[index].value.as_word = -a;
159 return VLIB_PARSE_MATCH_RULE;
161 vlib_parse_match_t eval_term0 (vlib_parse_main_t *pm,
162 vlib_parse_item_t *item,
163 vlib_parse_value_t *value)
165 clib_warning ("%U", format_vlib_parse_value, pm);
166 return VLIB_PARSE_MATCH_RULE;
168 vlib_parse_match_t eval_term1 (vlib_parse_main_t *pm,
169 vlib_parse_item_t *item,
170 vlib_parse_value_t *value)
173 int index = vec_len (pm->parse_value)-2;
175 a = pm->parse_value [index].value.as_uword;
176 b = pm->parse_value [index+1].value.as_uword;
178 pm->parse_value[index].value.as_uword = a * b;
179 _vec_len (pm->parse_value) -= 1;
180 clib_warning ("%U", format_vlib_parse_value, pm);
182 return VLIB_PARSE_MATCH_RULE;
184 vlib_parse_match_t eval_term2 (vlib_parse_main_t *pm,
185 vlib_parse_item_t *item,
186 vlib_parse_value_t *value)
189 int index = vec_len (pm->parse_value)-2;
191 a = pm->parse_value [index].value.as_uword;
192 b = pm->parse_value [index+1].value.as_uword;
194 pm->parse_value[index].value.as_uword = a / b;
195 _vec_len (pm->parse_value) -= 1;
196 clib_warning ("%U", format_vlib_parse_value, pm);
198 return VLIB_PARSE_MATCH_RULE;
200 vlib_parse_match_t eval_exp0 (vlib_parse_main_t *pm,
201 vlib_parse_item_t *item,
202 vlib_parse_value_t *value)
204 return VLIB_PARSE_MATCH_RULE;
206 vlib_parse_match_t eval_exp1 (vlib_parse_main_t *pm,
207 vlib_parse_item_t *item,
208 vlib_parse_value_t *value)
211 int index = vec_len (pm->parse_value)-2;
213 a = pm->parse_value [index].value.as_uword;
214 b = pm->parse_value [index+1].value.as_uword;
216 pm->parse_value[index].value.as_uword = a + b;
217 _vec_len (pm->parse_value) -= 1;
218 clib_warning ("%U", format_vlib_parse_value, pm);
220 return VLIB_PARSE_MATCH_RULE;
222 vlib_parse_match_t eval_exp2 (vlib_parse_main_t *pm,
223 vlib_parse_item_t *item,
224 vlib_parse_value_t *value)
227 int index = vec_len (pm->parse_value)-2;
229 a = pm->parse_value [index].value.as_uword;
230 b = pm->parse_value [index+1].value.as_uword;
232 pm->parse_value[index].value.as_uword = a - b;
233 _vec_len (pm->parse_value) -= 1;
234 clib_warning ("%U", format_vlib_parse_value, pm);
236 return VLIB_PARSE_MATCH_RULE;
239 vlib_parse_match_t eval_result (vlib_parse_main_t *pm,
240 vlib_parse_item_t *item,
241 vlib_parse_value_t *value)
243 clib_warning ("%U", format_vlib_parse_value, pm);
244 return VLIB_PARSE_MATCH_DONE;
247 vlib_parse_match_t noop_match_rule (vlib_parse_main_t *pm,
248 vlib_parse_item_t *item,
249 vlib_parse_value_t *value)
251 clib_warning ("%U", format_vlib_parse_value, pm);
252 return VLIB_PARSE_MATCH_RULE;
256 PARSE_INIT (t1, "moo", eval0);
257 PARSE_INIT (t2, "moo cow mumble", eval1);
258 PARSE_INIT (t3, "moo cow", eval2);
259 PARSE_INIT (t4, "moo cow mumble grunch", eval3);
263 PARSE_INIT (r1, "eval <exp>", eval_result);
265 PARSE_INIT (r2, "<exp> = <term><exp2>", eval_exp0);
266 PARSE_INIT (r3, "<exp2> = <plus> <exp>", eval_exp1);
267 PARSE_INIT (r4, "<exp2> = <minus> <exp>", eval_exp2);
268 PARSE_INIT (r5, "<exp2> = ", noop_match_rule);
269 PARSE_TYPE_INIT (exp, rule_match, 0, 0);
270 PARSE_TYPE_INIT (exp2, rule_match, 0, 0);
272 PARSE_INIT (r6, "<term> = <factor><term2>", eval_term0);
273 PARSE_INIT (r7, "<term2> = <star> <term>", eval_term1);
274 PARSE_INIT (r8, "<term2> = <slash> <term>", eval_term2);
275 PARSE_INIT (r9, "<term2> = ", noop_match_rule);
276 PARSE_TYPE_INIT (term, rule_match, 0, 0);
277 PARSE_TYPE_INIT (term2, rule_match, 0, 0);
279 PARSE_INIT (r11, "<factor> = <lpar> <exp> <rpar>", eval_factor1);
280 PARSE_INIT (r10, "<factor> = <number>", eval_factor0);
281 PARSE_INIT (r12, "<factor> = <minus> <factor>", eval_factor2);
283 PARSE_TYPE_INIT (factor, rule_match, 0, 0);
286 PARSE_INIT (r1, "eval <exp>", eval_result);
289 PARSE_INIT (r2, "<exp> = <term><exp2>", eval_exp0);
290 PARSE_INIT (r3, "<exp2> = <plus> <exp>", eval_exp1);
291 PARSE_INIT (r4, "<exp2> = <minus> <exp>", eval_exp2);
292 PARSE_INIT (r5, "<exp2> = ", noop_match_rule);
293 PARSE_TYPE_INIT (exp, rule_match, 0, 0);
294 PARSE_TYPE_INIT (exp2, rule_match, 0, 0);
296 PARSE_INIT (r6, "<term> = <factor><term2>", eval_term0);
297 PARSE_INIT (r7, "<term2> = <star> <term>", eval_term1);
298 PARSE_INIT (r8, "<term2> = <slash> <term>", eval_term2);
299 PARSE_INIT (r9, "<term2> = ", noop_match_rule);
300 PARSE_TYPE_INIT (term, rule_match, 0, 0);
301 PARSE_TYPE_INIT (term2, rule_match, 0, 0);
303 PARSE_INIT (r11, "<factor> = <lpar> <exp> <rpar>", eval_factor1);
304 PARSE_INIT (r10, "<factor> = <number>", eval_factor0);
305 PARSE_INIT (r12, "<factor> = <minus> <factor>", eval_factor2);
307 PARSE_TYPE_INIT (factor, rule_match, 0, 0);
311 PARSE_TYPE_INIT (exp, rule_match, 0, 0);
312 PARSE_INIT (r6, "<exp> = a b", eval_term0);
313 PARSE_INIT (r7, "<exp> = c d", eval_term1);
314 PARSE_INIT (r9, "<exp> = ", noop_match_rule);
318 #define foreach_rule_evaluator \
325 vlib_parse_match_t eval##n (vlib_parse_main_t *pm, \
326 vlib_parse_item_t *item, \
327 vlib_parse_value_t *value) \
329 clib_warning ("%U", format_vlib_parse_value, pm); \
330 return VLIB_PARSE_MATCH_DONE; \
332 foreach_rule_evaluator
336 PARSE_INIT (r1, "eval <moo>", eval_result);
338 PARSE_INIT (r2, "<moo> = cow", eval0);
339 PARSE_INIT (r4, "<moo> = ", eval1);
340 PARSE_TYPE_INIT (moo, rule_match, 0, 0);
344 clib_error_t *test_init (vlib_main_t *vm)
348 if ((error = vlib_call_init_function (vm, parse_init)))
354 VLIB_INIT_FUNCTION (test_init);
357 vlib_stdlex_init (vlib_main_t * vm)
359 vlib_lex_main_t * lm = &vlib_lex_main;
361 u16 slash_index, slash_star_index, slash_slash_index, slash_star_star_index;
364 u16 zero_index, octal_index, decimal_index, hex_index, binary_index;
366 top_index = vlib_lex_add_table ("top");
368 #define foreach_top_level_single_character_token \
386 vlib_lex_set_action_range(top_index,c,c,VLIB_LEX_RETURN,vlib_lex_add_token(lm, #t), top_index);
387 foreach_top_level_single_character_token;
391 zero_index = vlib_lex_add_table ("zero");
392 octal_index = vlib_lex_add_table ("octal");
393 decimal_index = vlib_lex_add_table ("decimal");
394 hex_index = vlib_lex_add_table ("hex");
395 binary_index = vlib_lex_add_table ("binary");
397 /* Support 0x 0b 0t and 0123 [octal] */
398 vlib_lex_set_action_range (top_index, '0', '0', VLIB_LEX_START_NUMBER, 10, zero_index);
399 vlib_lex_set_action_range (top_index, '1', '9', VLIB_LEX_START_NUMBER, 10, decimal_index);
401 vlib_lex_set_action_range (zero_index, 0, 0x7F, VLIB_LEX_RETURN_AND_RESCAN, VLIB_LEX_number, top_index);
403 vlib_lex_set_action_range (zero_index, 'x', 'x', VLIB_LEX_IGNORE, ~0, hex_index);
404 vlib_lex_set_action_range (zero_index, 'b', 'b', VLIB_LEX_IGNORE, ~0, binary_index);
405 vlib_lex_set_action_range (zero_index, 't', 't', VLIB_LEX_IGNORE, ~0, decimal_index);
406 vlib_lex_set_action_range (zero_index, '0', '7', VLIB_LEX_START_NUMBER, 8, octal_index);
409 vlib_lex_set_action_range (octal_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN,
410 VLIB_LEX_number, top_index);
411 vlib_lex_set_action_range (octal_index, '0', '7', VLIB_LEX_ADD_TO_NUMBER, 8,
415 vlib_lex_set_action_range (decimal_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN,
416 VLIB_LEX_number, top_index);
417 vlib_lex_set_action_range (decimal_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, 10,
421 vlib_lex_set_action_range (hex_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN,
422 VLIB_LEX_number, top_index);
423 vlib_lex_set_action_range (hex_index, '0', '9', VLIB_LEX_ADD_TO_NUMBER, 16,
425 vlib_lex_set_action_range (hex_index, 'a', 'f', VLIB_LEX_ADD_TO_NUMBER, 16,
427 vlib_lex_set_action_range (hex_index, 'A', 'F', VLIB_LEX_ADD_TO_NUMBER, 16,
431 vlib_lex_set_action_range (binary_index, 0, 0x7f, VLIB_LEX_RETURN_AND_RESCAN,
432 VLIB_LEX_number, top_index);
433 vlib_lex_set_action_range (binary_index, '0', '1', VLIB_LEX_ADD_TO_NUMBER, 2,
436 /* c/c++ comment syntax is the worst... */
438 slash_index = vlib_lex_add_table ("slash");
439 slash_star_index = vlib_lex_add_table ("slash_star");
440 slash_star_star_index = vlib_lex_add_table ("slash_star_star");
441 slash_slash_index = vlib_lex_add_table ("slash_slash");
442 slash_token = vlib_lex_add_token (lm, "slash");
444 /* Top level: see a slash, ignore, go to slash table */
445 vlib_lex_set_action_range (top_index, '/', '/', VLIB_LEX_IGNORE, ~0, slash_index);
447 /* default for slash table: return SLASH, go to top table */
448 vlib_lex_set_action_range (slash_index, 1, 0x7F, VLIB_LEX_RETURN_AND_RESCAN, slash_token,
450 /* see slash-slash, go to s-s table */
451 vlib_lex_set_action_range (slash_index, '/', '/', VLIB_LEX_IGNORE, ~0,
453 /* see slash-star, go to s-* table */
454 vlib_lex_set_action_range (slash_index, '*', '*', VLIB_LEX_IGNORE, ~0,
457 /* EOL in s-s table, ignore, go to top table */
458 vlib_lex_set_action_range (slash_slash_index, '\n', '\n', VLIB_LEX_IGNORE, ~0,
461 /* slash-star blah blah star */
462 vlib_lex_set_action_range (slash_star_index, '*', '*', VLIB_LEX_IGNORE, ~0,
463 slash_star_star_index);
465 /* slash star blah blah star slash */
466 vlib_lex_set_action_range (slash_star_star_index, '/', '/', VLIB_LEX_IGNORE, ~0,
470 vlib_lex_set_action_range (top_index, '<', '<', VLIB_LEX_RETURN, VLIB_LEX_lt, top_index);
471 vlib_lex_set_action_range (top_index, '=', '=', VLIB_LEX_RETURN, VLIB_LEX_equals,
473 vlib_lex_set_action_range (top_index, '>', '>', VLIB_LEX_RETURN, VLIB_LEX_gt, top_index);
475 /* words, key and otherwise */
476 word_index = vlib_lex_add_table ("word");
478 vlib_lex_set_action_range (top_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index);
479 vlib_lex_set_action_range (top_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index);
481 vlib_lex_set_action_range (word_index, 0, 0x7f, VLIB_LEX_KEYWORD_CHECK, ~0, top_index);
483 vlib_lex_set_action_range (word_index, 'a', 'z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index);
484 vlib_lex_set_action_range (word_index, 'A', 'Z', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index);
485 vlib_lex_set_action_range (word_index, '_', '_', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index);
486 vlib_lex_set_action_range (word_index, '0', '9', VLIB_LEX_ADD_TO_TOKEN, ~0, word_index);