2 * Command format string parser for CLI backend.
5 * Copyright (C) 2016 Cumulus Networks, Inc.
7 * This file is part of GNU Zebra.
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // compile with debugging facilities
31 /* define parse.error verbose */
33 /* define api.prefix {cmd_yy} */
35 /* names for generated header and parser files */
36 %defines "lib/command_parse.h"
37 %output "lib/command_parse.c"
39 /* note: code blocks are output in order, to both .c and .h:
41 * 2. %union + bison forward decls
43 * command_lex.h needs to be included at 3.; it needs the union and YYSTYPE.
44 * struct parser_ctx is needed for the bison forward decls.
53 #include "command_graph.h"
58 #define YYSTYPE CMD_YYSTYPE
59 #define YYLTYPE CMD_YYLTYPE
62 /* subgraph semantic value */
64 struct graph_node *start, *end;
71 struct graph_node *node;
72 struct subgraph subgraph;
77 #include "command_lex.h"
80 extern void set_lexer_string (yyscan_t *scn, const char *string);
81 extern void cleanup_lexer (yyscan_t *scn);
86 struct cmd_element *el;
89 struct graph_node *currnode;
91 /* pointers to copy of command docstring */
92 char *docstr_start, *docstr;
96 /* union types for lexed tokens */
99 %token <string> IPV4_PREFIX
101 %token <string> IPV6_PREFIX
102 %token <string> VARIABLE
103 %token <string> RANGE
105 /* union types for parsed rules */
107 %type <node> literal_token
108 %type <node> placeholder_token
109 %type <node> placeholder_token_real
110 %type <node> simple_token
111 %type <subgraph> selector
112 %type <subgraph> selector_token
113 %type <subgraph> selector_token_seq
114 %type <subgraph> selector_seq_seq
116 %type <string> varname_token
120 /* bison declarations */
122 cmd_yyerror (CMD_YYLTYPE *locp, struct parser_ctx *ctx, char const *msg);
124 /* helper functions for parser */
126 doc_next (struct parser_ctx *ctx);
128 static struct graph_node *
129 new_token_node (struct parser_ctx *,
130 enum cmd_token_type type,
135 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
136 struct graph_node *);
139 cleanup (struct parser_ctx *ctx);
141 #define scanner ctx->scanner
144 /* yyparse parameters */
145 %lex-param {yyscan_t scanner}
146 %parse-param {struct parser_ctx *ctx}
148 /* called automatically before yyparse */
150 /* clear state pointers */
151 ctx->currnode = vector_slot (ctx->graph->nodes, 0);
153 /* copy docstring and keep a pointer to the copy */
156 // allocate a new buffer, making room for a flag
157 size_t length = (size_t) strlen (ctx->el->doc) + 2;
158 ctx->docstr = malloc (length);
159 memcpy (ctx->docstr, ctx->el->doc, strlen (ctx->el->doc));
160 // set the flag so doc_next knows when to print a warning
161 ctx->docstr[length - 2] = 0x03;
163 ctx->docstr[length - 1] = 0x00;
165 ctx->docstr_start = ctx->docstr;
173 // tack on the command element
174 terminate_graph (&@1, ctx, ctx->currnode);
176 | cmd_token_seq placeholder_token '.' '.' '.'
178 if ((ctx->currnode = graph_add_edge (ctx->currnode, $2)) != $2)
179 graph_delete_node (ctx->graph, $2);
181 ((struct cmd_token *)ctx->currnode->data)->allowrepeat = 1;
183 // adding a node as a child of itself accepts any number
184 // of the same token, which is what we want for variadics
185 graph_add_edge (ctx->currnode, ctx->currnode);
187 // tack on the command element
188 terminate_graph (&@1, ctx, ctx->currnode);
192 varname_token: '$' WORD
204 | cmd_token_seq cmd_token
210 if ((ctx->currnode = graph_add_edge (ctx->currnode, $1)) != $1)
211 graph_delete_node (ctx->graph, $1);
215 graph_add_edge (ctx->currnode, $1.start);
216 ctx->currnode = $1.end;
225 literal_token: WORD varname_token
227 $$ = new_token_node (ctx, WORD_TKN, $1, doc_next(ctx));
228 cmd_token_varname_set ($$->data, $2);
229 XFREE (MTYPE_LEX, $2);
230 XFREE (MTYPE_LEX, $1);
234 placeholder_token_real:
237 $$ = new_token_node (ctx, IPV4_TKN, $1, doc_next(ctx));
238 XFREE (MTYPE_LEX, $1);
242 $$ = new_token_node (ctx, IPV4_PREFIX_TKN, $1, doc_next(ctx));
243 XFREE (MTYPE_LEX, $1);
247 $$ = new_token_node (ctx, IPV6_TKN, $1, doc_next(ctx));
248 XFREE (MTYPE_LEX, $1);
252 $$ = new_token_node (ctx, IPV6_PREFIX_TKN, $1, doc_next(ctx));
253 XFREE (MTYPE_LEX, $1);
257 $$ = new_token_node (ctx, VARIABLE_TKN, $1, doc_next(ctx));
258 XFREE (MTYPE_LEX, $1);
262 $$ = new_token_node (ctx, RANGE_TKN, $1, doc_next(ctx));
263 struct cmd_token *token = $$->data;
265 // get the numbers out
267 token->min = strtoll (yylval.string, &yylval.string, 10);
268 strsep (&yylval.string, "-");
269 token->max = strtoll (yylval.string, &yylval.string, 10);
272 if (token->min > token->max) cmd_yyerror (&@1, ctx, "Invalid range.");
274 XFREE (MTYPE_LEX, $1);
278 placeholder_token_real varname_token
280 struct cmd_token *token = $$->data;
282 cmd_token_varname_set (token, $2);
283 XFREE (MTYPE_LEX, $2);
287 /* <selector|set> productions */
288 selector: '<' selector_seq_seq '>' varname_token
291 cmd_token_varname_set ($2.end->data, $4);
292 XFREE (MTYPE_LEX, $4);
296 selector_seq_seq '|' selector_token_seq
299 graph_add_edge ($$.start, $3.start);
300 graph_add_edge ($3.end, $$.end);
304 $$.start = new_token_node (ctx, FORK_TKN, NULL, NULL);
305 $$.end = new_token_node (ctx, JOIN_TKN, NULL, NULL);
306 ((struct cmd_token *)$$.start->data)->forkjoin = $$.end;
307 ((struct cmd_token *)$$.end->data)->forkjoin = $$.start;
309 graph_add_edge ($$.start, $1.start);
310 graph_add_edge ($1.end, $$.end);
314 /* {keyword} productions */
315 selector: '{' selector_seq_seq '}' varname_token
318 graph_add_edge ($$.end, $$.start);
319 /* there is intentionally no start->end link, for two reasons:
320 * 1) this allows "at least 1 of" semantics, which are otherwise impossible
321 * 2) this would add a start->end->start loop in the graph that the current
322 * loop-avoidal fails to handle
323 * just use [{a|b}] if neccessary, that will work perfectly fine, and reason
324 * #1 is good enough to keep it this way. */
326 cmd_token_varname_set ($2.end->data, $4);
327 XFREE (MTYPE_LEX, $4);
334 $$.start = $$.end = $1;
340 selector_token_seq selector_token
342 graph_add_edge ($1.end, $2.start);
349 /* [option] productions */
350 selector: '[' selector_seq_seq ']' varname_token
353 graph_add_edge ($$.start, $$.end);
354 cmd_token_varname_set ($2.end->data, $4);
355 XFREE (MTYPE_LEX, $4);
363 DEFINE_MTYPE(LIB, LEX, "Lexer token (temporary)")
366 cmd_graph_parse (struct graph *graph, struct cmd_element *cmd)
368 struct parser_ctx ctx = { .graph = graph, .el = cmd };
370 // set to 1 to enable parser traces
373 set_lexer_string (&ctx.scanner, cmd->string);
375 // parse command into DFA
379 cleanup_lexer (&ctx.scanner);
385 /* parser helper functions */
388 yyerror (CMD_YYLTYPE *loc, struct parser_ctx *ctx, char const *msg)
390 char *tmpstr = strdup(ctx->el->string);
395 zlog_err ("%s: FATAL parse error: %s", __func__, msg);
396 zlog_err ("%s: %d:%d-%d of this command definition:", __func__, loc->first_line, loc->first_column, loc->last_column);
401 eol = strchr(line, '\n');
405 zlog_err ("%s: | %s", __func__, line);
406 if (lineno == loc->first_line && lineno == loc->last_line
407 && loc->first_column < (int)sizeof(spacing) - 1
408 && loc->last_column < (int)sizeof(spacing) - 1) {
410 int len = loc->last_column - loc->first_column;
414 memset(spacing, ' ', loc->first_column - 1);
415 memset(spacing + loc->first_column - 1, '^', len);
416 spacing[loc->first_column - 1 + len] = '\0';
417 zlog_err ("%s: | %s", __func__, spacing);
419 } while ((line = eol));
424 cleanup (struct parser_ctx *ctx)
427 free (ctx->docstr_start);
429 /* clear state pointers */
430 ctx->currnode = NULL;
431 ctx->docstr_start = ctx->docstr = NULL;
435 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
436 struct graph_node *finalnode)
438 // end of graph should look like this
439 // * -> finalnode -> END_TKN -> cmd_element
440 struct cmd_element *element = ctx->el;
441 struct graph_node *end_token_node =
442 new_token_node (ctx, END_TKN, CMD_CR_TEXT, "");
443 struct graph_node *end_element_node =
444 graph_new_node (ctx->graph, element, NULL);
446 if (ctx->docstr && strlen (ctx->docstr) > 1) {
447 zlog_debug ("Excessive docstring while parsing '%s'", ctx->el->string);
448 zlog_debug ("----------");
449 while (ctx->docstr && ctx->docstr[1] != '\0')
450 zlog_debug ("%s", strsep(&ctx->docstr, "\n"));
451 zlog_debug ("----------\n");
454 graph_add_edge (finalnode, end_token_node);
455 graph_add_edge (end_token_node, end_element_node);
459 doc_next (struct parser_ctx *ctx)
461 const char *piece = ctx->docstr ? strsep (&ctx->docstr, "\n") : "";
464 zlog_debug ("Ran out of docstring while parsing '%s'", ctx->el->string);
471 static struct graph_node *
472 new_token_node (struct parser_ctx *ctx, enum cmd_token_type type,
473 const char *text, const char *doc)
475 struct cmd_token *token = cmd_token_new (type, ctx->el->attr, text, doc);
476 return graph_new_node (ctx->graph, token, (void (*)(void *)) &cmd_token_del);