1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Command format string parser for CLI backend.
6 * Copyright (C) 2016 Cumulus Networks, Inc.
10 // compile with debugging facilities
15 /* define parse.error verbose */
17 /* define api.prefix {cmd_yy} */
19 /* names for generated header and parser files */
20 %defines "lib/command_parse.h"
21 %output "lib/command_parse.c"
23 /* note: code blocks are output in order, to both .c and .h:
25 * 2. %union + bison forward decls
27 * command_lex.h needs to be included at 3.; it needs the union and YYSTYPE.
28 * struct parser_ctx is needed for the bison forward decls.
38 #include "command_graph.h"
43 #define YYSTYPE CMD_YYSTYPE
44 #define YYLTYPE CMD_YYLTYPE
47 /* subgraph semantic value */
49 struct graph_node *start, *end;
56 struct graph_node *node;
57 struct subgraph subgraph;
62 #include "lib/command_lex.h"
65 extern void set_lexer_string (yyscan_t *scn, const char *string);
66 extern void cleanup_lexer (yyscan_t *scn);
71 const struct cmd_element *el;
74 struct graph_node *currnode;
76 /* pointers to copy of command docstring */
77 char *docstr_start, *docstr;
81 /* union types for lexed tokens */
84 %token <string> IPV4_PREFIX
86 %token <string> IPV6_PREFIX
87 %token <string> VARIABLE
90 %token <string> MAC_PREFIX
93 /* special syntax, value is irrelevant */
94 %token <string> EXCL_BRACKET
96 /* union types for parsed rules */
98 %type <node> literal_token
99 %type <node> placeholder_token
100 %type <node> placeholder_token_real
101 %type <node> simple_token
102 %type <subgraph> selector
103 %type <subgraph> selector_token
104 %type <subgraph> selector_token_seq
105 %type <subgraph> selector_seq_seq
107 %type <string> varname_token
111 /* bison declarations */
113 cmd_yyerror (CMD_YYLTYPE *locp, struct parser_ctx *ctx, char const *msg);
115 /* helper functions for parser */
117 doc_next (struct parser_ctx *ctx);
119 static struct graph_node *
120 new_token_node (struct parser_ctx *,
121 enum cmd_token_type type,
126 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
127 struct graph_node *);
130 cleanup (struct parser_ctx *ctx);
132 static void loopcheck(struct parser_ctx *ctx, struct subgraph *sg);
134 #define scanner ctx->scanner
137 /* yyparse parameters */
138 %lex-param {yyscan_t scanner}
139 %parse-param {struct parser_ctx *ctx}
141 /* called automatically before yyparse */
143 /* clear state pointers */
144 ctx->currnode = vector_slot (ctx->graph->nodes, 0);
146 /* copy docstring and keep a pointer to the copy */
149 // allocate a new buffer, making room for a flag
150 size_t length = (size_t) strlen (ctx->el->doc) + 2;
151 ctx->docstr = malloc (length);
152 memcpy (ctx->docstr, ctx->el->doc, strlen (ctx->el->doc));
153 // set the flag so doc_next knows when to print a warning
154 ctx->docstr[length - 2] = 0x03;
156 ctx->docstr[length - 1] = 0x00;
158 ctx->docstr_start = ctx->docstr;
166 // tack on the command element
167 terminate_graph (&@1, ctx, ctx->currnode);
169 | cmd_token_seq placeholder_token '.' '.' '.'
171 if ((ctx->currnode = graph_add_edge (ctx->currnode, $2)) != $2)
172 graph_delete_node (ctx->graph, $2);
174 ((struct cmd_token *)ctx->currnode->data)->allowrepeat = 1;
176 // adding a node as a child of itself accepts any number
177 // of the same token, which is what we want for variadics
178 graph_add_edge (ctx->currnode, ctx->currnode);
180 // tack on the command element
181 terminate_graph (&@1, ctx, ctx->currnode);
185 varname_token: '$' WORD
197 | cmd_token_seq cmd_token
203 if ((ctx->currnode = graph_add_edge (ctx->currnode, $1)) != $1)
204 graph_delete_node (ctx->graph, $1);
205 cmd_token_varname_seqappend($1);
209 graph_add_edge (ctx->currnode, $1.start);
210 cmd_token_varname_seqappend($1.start);
211 ctx->currnode = $1.end;
220 literal_token: WORD varname_token
222 $$ = new_token_node (ctx, WORD_TKN, $1, doc_next(ctx));
223 cmd_token_varname_set ($$->data, $2);
224 XFREE (MTYPE_LEX, $2);
225 XFREE (MTYPE_LEX, $1);
229 placeholder_token_real:
232 $$ = new_token_node (ctx, IPV4_TKN, $1, doc_next(ctx));
233 XFREE (MTYPE_LEX, $1);
237 $$ = new_token_node (ctx, IPV4_PREFIX_TKN, $1, doc_next(ctx));
238 XFREE (MTYPE_LEX, $1);
242 $$ = new_token_node (ctx, IPV6_TKN, $1, doc_next(ctx));
243 XFREE (MTYPE_LEX, $1);
247 $$ = new_token_node (ctx, IPV6_PREFIX_TKN, $1, doc_next(ctx));
248 XFREE (MTYPE_LEX, $1);
252 $$ = new_token_node (ctx, VARIABLE_TKN, $1, doc_next(ctx));
253 XFREE (MTYPE_LEX, $1);
257 $$ = new_token_node (ctx, RANGE_TKN, $1, doc_next(ctx));
258 struct cmd_token *token = $$->data;
260 // get the numbers out
262 token->min = strtoll (yylval.string, &yylval.string, 10);
263 strsep (&yylval.string, "-");
264 token->max = strtoll (yylval.string, &yylval.string, 10);
267 if (token->min > token->max) cmd_yyerror (&@1, ctx, "Invalid range.");
269 XFREE (MTYPE_LEX, $1);
273 $$ = new_token_node (ctx, MAC_TKN, $1, doc_next(ctx));
274 XFREE (MTYPE_LEX, $1);
278 $$ = new_token_node (ctx, MAC_PREFIX_TKN, $1, doc_next(ctx));
279 XFREE (MTYPE_LEX, $1);
283 $$ = new_token_node (ctx, ASNUM_TKN, $1, doc_next(ctx));
284 XFREE (MTYPE_LEX, $1);
288 placeholder_token_real varname_token
291 cmd_token_varname_set ($$->data, $2);
292 XFREE (MTYPE_LEX, $2);
296 /* <selector|set> productions */
297 selector: '<' selector_seq_seq '>' varname_token
300 cmd_token_varname_join ($2.end, $4);
301 XFREE (MTYPE_LEX, $4);
305 selector_seq_seq '|' selector_token_seq
308 graph_add_edge ($$.start, $3.start);
309 graph_add_edge ($3.end, $$.end);
313 $$.start = new_token_node (ctx, FORK_TKN, NULL, NULL);
314 $$.end = new_token_node (ctx, JOIN_TKN, NULL, NULL);
315 ((struct cmd_token *)$$.start->data)->forkjoin = $$.end;
316 ((struct cmd_token *)$$.end->data)->forkjoin = $$.start;
318 graph_add_edge ($$.start, $1.start);
319 graph_add_edge ($1.end, $$.end);
323 /* {keyword} productions */
324 selector: '{' selector_seq_seq '}' varname_token
327 graph_add_edge ($$.end, $$.start);
328 /* there is intentionally no start->end link, for two reasons:
329 * 1) this allows "at least 1 of" semantics, which are otherwise impossible
330 * 2) this would add a start->end->start loop in the graph that the current
331 * loop-avoidal fails to handle
332 * just use [{a|b}] if necessary, that will work perfectly fine, and reason
333 * #1 is good enough to keep it this way. */
336 cmd_token_varname_join ($2.end, $4);
337 XFREE (MTYPE_LEX, $4);
344 $$.start = $$.end = $1;
350 selector_token_seq selector_token
352 graph_add_edge ($1.end, $2.start);
353 cmd_token_varname_seqappend($2.start);
360 /* [option] productions */
361 selector: '[' selector_seq_seq ']' varname_token
364 graph_add_edge ($$.start, $$.end);
365 cmd_token_varname_join ($2.end, $4);
366 XFREE (MTYPE_LEX, $4);
370 /* ![option] productions */
371 selector: EXCL_BRACKET selector_seq_seq ']' varname_token
373 struct graph_node *neg_only = new_token_node (ctx, NEG_ONLY_TKN, NULL, NULL);
376 graph_add_edge ($$.start, neg_only);
377 graph_add_edge (neg_only, $$.end);
378 cmd_token_varname_join ($2.end, $4);
379 XFREE (MTYPE_LEX, $4);
387 DEFINE_MTYPE(LIB, LEX, "Lexer token (temporary)");
390 cmd_graph_parse (struct graph *graph, const struct cmd_element *cmd)
392 struct parser_ctx ctx = { .graph = graph, .el = cmd };
394 // set to 1 to enable parser traces
397 set_lexer_string (&ctx.scanner, cmd->string);
399 // parse command into DFA
403 cleanup_lexer (&ctx.scanner);
409 /* parser helper functions */
411 static bool loopcheck_inner(struct graph_node *start, struct graph_node *node,
412 struct graph_node *end, size_t depth)
421 for (i = 0; i < vector_active(node->to); i++) {
422 struct graph_node *next = vector_slot(node->to, i);
423 struct cmd_token *tok = next->data;
425 if (next == end || next == start)
427 if (tok->type < SPECIAL_TKN)
429 ret = loopcheck_inner(start, next, end, depth);
436 static void loopcheck(struct parser_ctx *ctx, struct subgraph *sg)
438 if (loopcheck_inner(sg->start, sg->start, sg->end, 0))
439 zlog_err("FATAL: '%s': {} contains an empty path! Use [{...}]",
444 yyerror (CMD_YYLTYPE *loc, struct parser_ctx *ctx, char const *msg)
446 char *tmpstr = strdup(ctx->el->string);
451 zlog_notice ("%s: FATAL parse error: %s", __func__, msg);
452 zlog_notice ("%s: %d:%d-%d of this command definition:", __func__, loc->first_line, loc->first_column, loc->last_column);
457 eol = strchr(line, '\n');
461 zlog_notice ("%s: | %s", __func__, line);
462 if (lineno == loc->first_line && lineno == loc->last_line
463 && loc->first_column < (int)sizeof(spacing) - 1
464 && loc->last_column < (int)sizeof(spacing) - 1) {
466 int len = loc->last_column - loc->first_column;
470 memset(spacing, ' ', loc->first_column - 1);
471 memset(spacing + loc->first_column - 1, '^', len);
472 spacing[loc->first_column - 1 + len] = '\0';
473 zlog_notice ("%s: | %s", __func__, spacing);
475 } while ((line = eol));
480 cleanup (struct parser_ctx *ctx)
483 free (ctx->docstr_start);
485 /* clear state pointers */
486 ctx->currnode = NULL;
487 ctx->docstr_start = ctx->docstr = NULL;
491 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
492 struct graph_node *finalnode)
494 // end of graph should look like this
495 // * -> finalnode -> END_TKN -> cmd_element
496 const struct cmd_element *element = ctx->el;
497 struct graph_node *end_token_node =
498 new_token_node (ctx, END_TKN, CMD_CR_TEXT, "");
499 struct graph_node *end_element_node =
500 graph_new_node (ctx->graph, (void *)element, NULL);
502 if (ctx->docstr && strlen (ctx->docstr) > 1) {
503 zlog_err ("Excessive docstring while parsing '%s'", ctx->el->string);
504 zlog_err ("----------");
505 while (ctx->docstr && ctx->docstr[1] != '\0')
506 zlog_err ("%s", strsep(&ctx->docstr, "\n"));
507 zlog_err ("----------");
510 graph_add_edge (finalnode, end_token_node);
511 graph_add_edge (end_token_node, end_element_node);
515 doc_next (struct parser_ctx *ctx)
517 const char *piece = ctx->docstr ? strsep (&ctx->docstr, "\n") : "";
520 zlog_err ("Ran out of docstring while parsing '%s'", ctx->el->string);
527 static struct graph_node *
528 new_token_node (struct parser_ctx *ctx, enum cmd_token_type type,
529 const char *text, const char *doc)
531 struct cmd_token *token = cmd_token_new (type, ctx->el->attr, text, doc);
532 return graph_new_node (ctx->graph, token, (void (*)(void *)) &cmd_token_del);