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
27 typedef union CMD_YYSTYPE CMD_YYSTYPE;
28 #define YYSTYPE CMD_YYSTYPE
29 #include "command_lex.h"
31 // compile with debugging facilities
36 %define api.prefix {cmd_yy}
38 /* names for generated header and parser files */
39 %defines "command_parse.h"
40 %output "command_parse.c"
42 /* required external units */
53 struct cmd_element *el;
56 struct graph_node *currnode, *startnode;
58 /* pointers to copy of command docstring */
59 char *docstr_start, *docstr;
62 extern void set_lexer_string (yyscan_t *scn, const char *string);
63 extern void cleanup_lexer (yyscan_t *scn);
66 /* functionality this unit exports */
68 /* maximum length of a number, lexer will not match anything longer */
69 #define DECIMAL_STRLEN_MAX 20
72 /* valid semantic types for tokens and rules */
76 struct graph_node *node;
77 struct subgraph *subgraph;
80 /* union types for lexed tokens */
83 %token <string> IPV4_PREFIX
85 %token <string> IPV6_PREFIX
86 %token <string> VARIABLE
89 /* union types for parsed rules */
91 %type <node> sentence_root
92 %type <node> literal_token
93 %type <node> placeholder_token
94 %type <node> simple_token
95 %type <subgraph> option
96 %type <subgraph> option_token
97 %type <subgraph> option_token_seq
98 %type <subgraph> selector
99 %type <subgraph> selector_token
100 %type <subgraph> selector_token_seq
101 %type <subgraph> selector_seq_seq
102 %type <subgraph> compound_token
106 /* bison declarations */
108 cmd_yyerror (struct parser_ctx *ctx, char const *msg);
110 /* subgraph semantic value */
112 struct graph_node *start, *end;
115 /* helper functions for parser */
117 doc_next (struct parser_ctx *ctx);
119 static struct graph_node *
120 node_adjacent (struct graph_node *, struct graph_node *);
122 static struct graph_node *
123 add_edge_dedup (struct graph_node *, struct graph_node *);
126 cmp_token (struct cmd_token *, struct cmd_token *);
128 static struct graph_node *
129 new_token_node (struct parser_ctx *,
130 enum cmd_token_type type,
135 terminate_graph (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 = ctx->startnode = NULL;
153 ctx->startnode = vector_slot (ctx->graph->nodes, 0);
155 /* copy docstring and keep a pointer to the copy */
158 // allocate a new buffer, making room for a flag
159 size_t length = (size_t) strlen (ctx->el->doc) + 2;
160 ctx->docstr = malloc (length);
161 memcpy (ctx->docstr, ctx->el->doc, strlen (ctx->el->doc));
162 // set the flag so doc_next knows when to print a warning
163 ctx->docstr[length - 2] = 0x03;
165 ctx->docstr[length - 1] = 0x00;
167 ctx->docstr_start = ctx->docstr;
173 sentence_root cmd_token_seq
175 // tack on the command element
176 terminate_graph (ctx, ctx->currnode);
178 | sentence_root cmd_token_seq placeholder_token '.' '.' '.'
180 if ((ctx->currnode = add_edge_dedup (ctx->currnode, $3)) != $3)
181 graph_delete_node (ctx->graph, $3);
183 ctx->currnode->allowrepeat = 1;
185 // adding a node as a child of itself accepts any number
186 // of the same token, which is what we want for variadics
187 add_edge_dedup (ctx->currnode, ctx->currnode);
189 // tack on the command element
190 terminate_graph (ctx, ctx->currnode);
196 struct graph_node *root =
197 new_token_node (ctx, WORD_TKN, strdup ($1), doc_next(ctx));
199 if ((ctx->currnode = add_edge_dedup (ctx->startnode, root)) != root)
200 graph_delete_node (ctx->graph, root);
209 | cmd_token_seq cmd_token
215 if ((ctx->currnode = add_edge_dedup (ctx->currnode, $1)) != $1)
216 graph_delete_node (ctx->graph, $1);
220 graph_add_edge (ctx->currnode, $1->start);
221 ctx->currnode = $1->end;
238 $$ = new_token_node (ctx, WORD_TKN, strdup($1), doc_next(ctx));
246 $$ = new_token_node (ctx, IPV4_TKN, strdup($1), doc_next(ctx));
251 $$ = new_token_node (ctx, IPV4_PREFIX_TKN, strdup($1), doc_next(ctx));
256 $$ = new_token_node (ctx, IPV6_TKN, strdup($1), doc_next(ctx));
261 $$ = new_token_node (ctx, IPV6_PREFIX_TKN, strdup($1), doc_next(ctx));
266 $$ = new_token_node (ctx, VARIABLE_TKN, strdup($1), doc_next(ctx));
271 $$ = new_token_node (ctx, RANGE_TKN, strdup($1), doc_next(ctx));
272 struct cmd_token *token = $$->data;
274 // get the numbers out
276 token->min = strtoll (yylval.string, &yylval.string, 10);
277 strsep (&yylval.string, "-");
278 token->max = strtoll (yylval.string, &yylval.string, 10);
281 if (token->min > token->max) cmd_yyerror (ctx, "Invalid range.");
286 /* <selector|set> productions */
287 selector: '<' selector_seq_seq '>'
289 $$ = malloc (sizeof (struct subgraph));
290 $$->start = new_token_node (ctx, SELECTOR_TKN, NULL, NULL);
291 $$->end = new_token_node (ctx, NUL_TKN, NULL, NULL);
292 for (unsigned int i = 0; i < vector_active ($2->start->to); i++)
294 struct graph_node *sn = vector_slot ($2->start->to, i),
295 *en = vector_slot ($2->end->from, i);
296 graph_add_edge ($$->start, sn);
297 graph_add_edge (en, $$->end);
299 graph_delete_node (ctx->graph, $2->start);
300 graph_delete_node (ctx->graph, $2->end);
305 selector_seq_seq '|' selector_token_seq
307 $$ = malloc (sizeof (struct subgraph));
308 $$->start = graph_new_node (ctx->graph, NULL, NULL);
309 $$->end = graph_new_node (ctx->graph, NULL, NULL);
311 // link in last sequence
312 graph_add_edge ($$->start, $3->start);
313 graph_add_edge ($3->end, $$->end);
315 for (unsigned int i = 0; i < vector_active ($1->start->to); i++)
317 struct graph_node *sn = vector_slot ($1->start->to, i),
318 *en = vector_slot ($1->end->from, i);
319 graph_add_edge ($$->start, sn);
320 graph_add_edge (en, $$->end);
322 graph_delete_node (ctx->graph, $1->start);
323 graph_delete_node (ctx->graph, $1->end);
327 | selector_token_seq '|' selector_token_seq
329 $$ = malloc (sizeof (struct subgraph));
330 $$->start = graph_new_node (ctx->graph, NULL, NULL);
331 $$->end = graph_new_node (ctx->graph, NULL, NULL);
332 graph_add_edge ($$->start, $1->start);
333 graph_add_edge ($1->end, $$->end);
334 graph_add_edge ($$->start, $3->start);
335 graph_add_edge ($3->end, $$->end);
341 /* {keyword} productions */
342 selector: '{' selector_seq_seq '}'
344 $$ = malloc (sizeof (struct subgraph));
345 $$->start = new_token_node (ctx, SELECTOR_TKN, NULL, NULL);
346 $$->end = new_token_node (ctx, NUL_TKN, NULL, NULL);
347 graph_add_edge ($$->start, $$->end);
348 for (unsigned int i = 0; i < vector_active ($2->start->to); i++)
350 struct graph_node *sn = vector_slot ($2->start->to, i),
351 *en = vector_slot ($2->end->from, i);
352 graph_add_edge ($$->start, sn);
353 graph_add_edge (en, $$->start);
355 graph_delete_node (ctx->graph, $2->start);
356 graph_delete_node (ctx->graph, $2->end);
364 $$ = malloc (sizeof (struct subgraph));
365 $$->start = $$->end = $1;
367 | selector_token_seq selector_token
369 $$ = malloc (sizeof (struct subgraph));
370 graph_add_edge ($1->end, $2->start);
371 $$->start = $1->start;
381 $$ = malloc (sizeof (struct subgraph));
382 $$->start = $$->end = $1;
388 /* [option] productions */
389 option: '[' option_token_seq ']'
392 $$ = malloc (sizeof (struct subgraph));
393 $$->start = new_token_node (ctx, OPTION_TKN, NULL, NULL);
394 $$->end = new_token_node (ctx, NUL_TKN, NULL, NULL);
395 // add a path through the sequence to the end
396 graph_add_edge ($$->start, $2->start);
397 graph_add_edge ($2->end, $$->end);
398 // add a path directly from the start to the end
399 graph_add_edge ($$->start, $$->end);
406 | option_token_seq option_token
408 $$ = malloc (sizeof (struct subgraph));
409 graph_add_edge ($1->end, $2->start);
410 $$->start = $1->start;
420 $$ = malloc (sizeof (struct subgraph));
421 $$->start = $$->end = $1;
431 command_parse_format (struct graph *graph, struct cmd_element *cmd)
433 struct parser_ctx ctx = { .graph = graph, .el = cmd };
435 // set to 1 to enable parser traces
438 set_lexer_string (&ctx.scanner, cmd->string);
440 // parse command into DFA
444 cleanup_lexer (&ctx.scanner);
450 /* parser helper functions */
453 yyerror (struct parser_ctx *ctx, char const *msg)
455 zlog_err ("%s: FATAL parse error: %s", __func__, msg);
456 zlog_err ("while parsing this command definition: \n\t%s\n", ctx->el->string);
457 //exit(EXIT_FAILURE);
461 cleanup (struct parser_ctx *ctx)
464 free (ctx->docstr_start);
466 /* clear state pointers */
467 ctx->currnode = NULL;
468 ctx->docstr_start = ctx->docstr = NULL;
472 terminate_graph (struct parser_ctx *ctx, struct graph_node *finalnode)
474 // end of graph should look like this
475 // * -> finalnode -> END_TKN -> cmd_element
476 struct cmd_element *element = ctx->el;
477 struct graph_node *end_token_node =
480 strdup (CMD_CR_TEXT),
482 struct graph_node *end_element_node =
483 graph_new_node (ctx->graph, element, NULL);
485 if (node_adjacent (finalnode, end_token_node))
486 cmd_yyerror (ctx, "Duplicate command.");
488 graph_add_edge (finalnode, end_token_node);
489 graph_add_edge (end_token_node, end_element_node);
493 doc_next (struct parser_ctx *ctx)
495 const char *piece = ctx->docstr ? strsep (&ctx->docstr, "\n") : "";
498 zlog_debug ("Ran out of docstring while parsing '%s'", ctx->el->string);
502 return strdup (piece);
505 static struct graph_node *
506 new_token_node (struct parser_ctx *ctx, enum cmd_token_type type,
507 char *text, char *doc)
509 struct cmd_token *token = new_cmd_token (type, ctx->el->attr, text, doc);
510 return graph_new_node (ctx->graph, token, (void (*)(void *)) &del_cmd_token);
514 * Determines if there is an out edge from the first node to the second
516 static struct graph_node *
517 node_adjacent (struct graph_node *first, struct graph_node *second)
519 struct graph_node *adj;
520 for (unsigned int i = 0; i < vector_active (first->to); i++)
522 adj = vector_slot (first->to, i);
523 struct cmd_token *ftok = adj->data,
524 *stok = second->data;
525 if (cmp_token (ftok, stok))
532 * Creates an edge betwen two nodes, unless there is already an edge to an
535 * The first node's out edges are searched to see if any of them point to a
536 * node that is equivalent to the second node. If such a node exists, it is
537 * returned. Otherwise an edge is created from the first node to the second.
539 * @param from start node for edge
540 * @param to end node for edge
541 * @return the node which the new edge points to
543 static struct graph_node *
544 add_edge_dedup (struct graph_node *from, struct graph_node *to)
546 struct graph_node *existing = node_adjacent (from, to);
549 struct cmd_token *ex_tok = existing->data;
550 struct cmd_token *to_tok = to->data;
551 // NORMAL takes precedence over DEPRECATED takes precedence over HIDDEN
552 ex_tok->attr = (ex_tok->attr < to_tok->attr) ? ex_tok->attr : to_tok->attr;
556 return graph_add_edge (from, to);
560 * Compares two cmd_token's for equality,
562 * As such, this function is the working definition of token equality
563 * for parsing purposes and determines overall graph structure.
566 cmp_token (struct cmd_token *first, struct cmd_token *second)
569 if (first->type != second->type) return 0;
571 switch (first->type) {
574 if (first->text && second->text)
576 if (strcmp (first->text, second->text))
579 else if (first->text != second->text) return 0;
582 if (first->min != second->min || first->max != second->max)
585 /* selectors and options should be equal if their subgraphs are equal,
586 * but the graph isomorphism problem is not known to be solvable in
587 * polynomial time so we consider selectors and options inequal in all
588 * cases; ultimately this forks the graph, but the matcher can handle
595 /* end nodes are always considered equal, since each node may only
596 * have one END_TKN child at a time