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 "command_parse.h"
37 %output "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.
51 #include "command_graph.h"
56 #define YYSTYPE CMD_YYSTYPE
57 #define YYLTYPE CMD_YYLTYPE
60 /* subgraph semantic value */
62 struct graph_node *start, *end;
69 struct graph_node *node;
70 struct subgraph subgraph;
75 #include "command_lex.h"
78 extern void set_lexer_string (yyscan_t *scn, const char *string);
79 extern void cleanup_lexer (yyscan_t *scn);
84 struct cmd_element *el;
87 struct graph_node *currnode;
89 /* pointers to copy of command docstring */
90 char *docstr_start, *docstr;
94 /* union types for lexed tokens */
97 %token <string> IPV4_PREFIX
99 %token <string> IPV6_PREFIX
100 %token <string> VARIABLE
101 %token <string> RANGE
103 /* union types for parsed rules */
105 %type <node> literal_token
106 %type <node> placeholder_token
107 %type <node> placeholder_token_real
108 %type <node> simple_token
109 %type <subgraph> selector
110 %type <subgraph> selector_token
111 %type <subgraph> selector_token_seq
112 %type <subgraph> selector_seq_seq
114 %type <string> varname_token
118 /* bison declarations */
120 cmd_yyerror (CMD_YYLTYPE *locp, struct parser_ctx *ctx, char const *msg);
122 /* helper functions for parser */
124 doc_next (struct parser_ctx *ctx);
126 static struct graph_node *
127 new_token_node (struct parser_ctx *,
128 enum cmd_token_type type,
133 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
134 struct graph_node *);
137 cleanup (struct parser_ctx *ctx);
139 #define scanner ctx->scanner
142 /* yyparse parameters */
143 %lex-param {yyscan_t scanner}
144 %parse-param {struct parser_ctx *ctx}
146 /* called automatically before yyparse */
148 /* clear state pointers */
149 ctx->currnode = vector_slot (ctx->graph->nodes, 0);
151 /* copy docstring and keep a pointer to the copy */
154 // allocate a new buffer, making room for a flag
155 size_t length = (size_t) strlen (ctx->el->doc) + 2;
156 ctx->docstr = malloc (length);
157 memcpy (ctx->docstr, ctx->el->doc, strlen (ctx->el->doc));
158 // set the flag so doc_next knows when to print a warning
159 ctx->docstr[length - 2] = 0x03;
161 ctx->docstr[length - 1] = 0x00;
163 ctx->docstr_start = ctx->docstr;
171 // tack on the command element
172 terminate_graph (&@1, ctx, ctx->currnode);
174 | cmd_token_seq placeholder_token '.' '.' '.'
176 if ((ctx->currnode = graph_add_edge (ctx->currnode, $2)) != $2)
177 graph_delete_node (ctx->graph, $2);
179 ((struct cmd_token *)ctx->currnode->data)->allowrepeat = 1;
181 // adding a node as a child of itself accepts any number
182 // of the same token, which is what we want for variadics
183 graph_add_edge (ctx->currnode, ctx->currnode);
185 // tack on the command element
186 terminate_graph (&@1, ctx, ctx->currnode);
190 varname_token: '$' WORD
192 $$ = XSTRDUP (MTYPE_LEX, $2);
202 | cmd_token_seq cmd_token
208 if ((ctx->currnode = graph_add_edge (ctx->currnode, $1)) != $1)
209 graph_delete_node (ctx->graph, $1);
213 graph_add_edge (ctx->currnode, $1.start);
214 ctx->currnode = $1.end;
223 literal_token: WORD varname_token
225 $$ = new_token_node (ctx, WORD_TKN, $1, doc_next(ctx));
226 cmd_token_varname_set ($$->data, $2);
227 XFREE (MTYPE_LEX, $2);
228 XFREE (MTYPE_LEX, $1);
232 placeholder_token_real:
235 $$ = new_token_node (ctx, IPV4_TKN, $1, doc_next(ctx));
236 XFREE (MTYPE_LEX, $1);
240 $$ = new_token_node (ctx, IPV4_PREFIX_TKN, $1, doc_next(ctx));
241 XFREE (MTYPE_LEX, $1);
245 $$ = new_token_node (ctx, IPV6_TKN, $1, doc_next(ctx));
246 XFREE (MTYPE_LEX, $1);
250 $$ = new_token_node (ctx, IPV6_PREFIX_TKN, $1, doc_next(ctx));
251 XFREE (MTYPE_LEX, $1);
255 $$ = new_token_node (ctx, VARIABLE_TKN, $1, doc_next(ctx));
256 XFREE (MTYPE_LEX, $1);
260 $$ = new_token_node (ctx, RANGE_TKN, $1, doc_next(ctx));
261 struct cmd_token *token = $$->data;
263 // get the numbers out
265 token->min = strtoll (yylval.string, &yylval.string, 10);
266 strsep (&yylval.string, "-");
267 token->max = strtoll (yylval.string, &yylval.string, 10);
270 if (token->min > token->max) cmd_yyerror (&@1, ctx, "Invalid range.");
272 XFREE (MTYPE_LEX, $1);
276 placeholder_token_real varname_token
278 struct cmd_token *token = $$->data;
280 cmd_token_varname_set (token, $2);
281 XFREE (MTYPE_LEX, $2);
285 /* <selector|set> productions */
286 selector: '<' selector_seq_seq '>' varname_token
289 cmd_token_varname_set ($2.end->data, $4);
290 XFREE (MTYPE_LEX, $4);
294 selector_seq_seq '|' selector_token_seq
297 graph_add_edge ($$.start, $3.start);
298 graph_add_edge ($3.end, $$.end);
302 $$.start = new_token_node (ctx, FORK_TKN, NULL, NULL);
303 $$.end = new_token_node (ctx, JOIN_TKN, NULL, NULL);
304 ((struct cmd_token *)$$.start->data)->forkjoin = $$.end;
305 ((struct cmd_token *)$$.end->data)->forkjoin = $$.start;
307 graph_add_edge ($$.start, $1.start);
308 graph_add_edge ($1.end, $$.end);
312 /* {keyword} productions */
313 selector: '{' selector_seq_seq '}' varname_token
316 graph_add_edge ($$.end, $$.start);
317 /* there is intentionally no start->end link, for two reasons:
318 * 1) this allows "at least 1 of" semantics, which are otherwise impossible
319 * 2) this would add a start->end->start loop in the graph that the current
320 * loop-avoidal fails to handle
321 * just use [{a|b}] if neccessary, that will work perfectly fine, and reason
322 * #1 is good enough to keep it this way. */
324 cmd_token_varname_set ($2.end->data, $4);
325 XFREE (MTYPE_LEX, $4);
332 $$.start = $$.end = $1;
338 selector_token_seq selector_token
340 graph_add_edge ($1.end, $2.start);
347 /* [option] productions */
348 selector: '[' selector_seq_seq ']' varname_token
351 graph_add_edge ($$.start, $$.end);
352 cmd_token_varname_set ($2.end->data, $4);
353 XFREE (MTYPE_LEX, $4);
361 DEFINE_MTYPE(LIB, LEX, "Lexer token (temporary)")
364 cmd_graph_parse (struct graph *graph, struct cmd_element *cmd)
366 struct parser_ctx ctx = { .graph = graph, .el = cmd };
368 // set to 1 to enable parser traces
371 set_lexer_string (&ctx.scanner, cmd->string);
373 // parse command into DFA
377 cleanup_lexer (&ctx.scanner);
383 /* parser helper functions */
386 yyerror (CMD_YYLTYPE *loc, struct parser_ctx *ctx, char const *msg)
388 char *tmpstr = strdup(ctx->el->string);
393 zlog_err ("%s: FATAL parse error: %s", __func__, msg);
394 zlog_err ("%s: %d:%d-%d of this command definition:", __func__, loc->first_line, loc->first_column, loc->last_column);
399 eol = strchr(line, '\n');
403 zlog_err ("%s: | %s", __func__, line);
404 if (lineno == loc->first_line && lineno == loc->last_line
405 && loc->first_column < (int)sizeof(spacing) - 1
406 && loc->last_column < (int)sizeof(spacing) - 1) {
408 int len = loc->last_column - loc->first_column;
412 memset(spacing, ' ', loc->first_column - 1);
413 memset(spacing + loc->first_column - 1, '^', len);
414 spacing[loc->first_column - 1 + len] = '\0';
415 zlog_err ("%s: | %s", __func__, spacing);
417 } while ((line = eol));
422 cleanup (struct parser_ctx *ctx)
425 free (ctx->docstr_start);
427 /* clear state pointers */
428 ctx->currnode = NULL;
429 ctx->docstr_start = ctx->docstr = NULL;
433 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
434 struct graph_node *finalnode)
436 // end of graph should look like this
437 // * -> finalnode -> END_TKN -> cmd_element
438 struct cmd_element *element = ctx->el;
439 struct graph_node *end_token_node =
440 new_token_node (ctx, END_TKN, CMD_CR_TEXT, "");
441 struct graph_node *end_element_node =
442 graph_new_node (ctx->graph, element, NULL);
444 graph_add_edge (finalnode, end_token_node);
445 graph_add_edge (end_token_node, end_element_node);
449 doc_next (struct parser_ctx *ctx)
451 const char *piece = ctx->docstr ? strsep (&ctx->docstr, "\n") : "";
454 zlog_debug ("Ran out of docstring while parsing '%s'", ctx->el->string);
461 static struct graph_node *
462 new_token_node (struct parser_ctx *ctx, enum cmd_token_type type,
463 const char *text, const char *doc)
465 struct cmd_token *token = cmd_token_new (type, ctx->el->attr, text, doc);
466 return graph_new_node (ctx->graph, token, (void (*)(void *)) &cmd_token_del);