]> git.proxmox.com Git - mirror_frr.git/blame - lib/command_parse.y
bgpd: Zebra lib for Graceful Restart.
[mirror_frr.git] / lib / command_parse.y
CommitLineData
9d0662e0 1/*
1ab84bf3 2 * Command format string parser for CLI backend.
9d0662e0 3 *
1ab84bf3 4 * --
9547b5d0 5 * Copyright (C) 2016 Cumulus Networks, Inc.
9d0662e0 6 *
1ab84bf3
QY
7 * This file is part of GNU Zebra.
8 *
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
12 * later version.
13 *
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.
18 *
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
22 * 02111-1307, USA.
9d0662e0
QY
23 */
24
92055a92 25%{
1ab84bf3
QY
26// compile with debugging facilities
27#define YYDEBUG 1
51fc9379 28%}
782d9789 29
8bb647a8 30%locations
05dbb7df 31/* define parse.error verbose */
e9484f70 32%define api.pure full
0d37f9f3 33/* define api.prefix {cmd_yy} */
e9484f70 34
51fc9379 35/* names for generated header and parser files */
4a121f99
DL
36%defines "lib/command_parse.h"
37%output "lib/command_parse.c"
5a8bbed0 38
afc3a6ce
DL
39/* note: code blocks are output in order, to both .c and .h:
40 * 1. %code requires
41 * 2. %union + bison forward decls
42 * 3. %code provides
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.
45 */
eceb1066 46%code requires {
92e50261
DL
47 #include "config.h"
48
eb44e1aa 49 #include <stdbool.h>
5894e76d
DL
50 #include <stdlib.h>
51 #include <string.h>
52 #include <ctype.h>
53
54 #include "command_graph.h"
a9958674 55 #include "log.h"
51fc9379 56
55b7f20f
DL
57 DECLARE_MTYPE(LEX)
58
afc3a6ce 59 #define YYSTYPE CMD_YYSTYPE
8bb647a8 60 #define YYLTYPE CMD_YYLTYPE
afc3a6ce 61 struct parser_ctx;
2020b1c8
DL
62
63 /* subgraph semantic value */
64 struct subgraph {
65 struct graph_node *start, *end;
66 };
afc3a6ce
DL
67}
68
69%union {
70 long long number;
71 char *string;
72 struct graph_node *node;
2020b1c8 73 struct subgraph subgraph;
afc3a6ce
DL
74}
75
76%code provides {
77 #ifndef FLEX_SCANNER
78 #include "command_lex.h"
79 #endif
80
81 extern void set_lexer_string (yyscan_t *scn, const char *string);
82 extern void cleanup_lexer (yyscan_t *scn);
83
b07a15f8 84 struct parser_ctx {
e9484f70
DL
85 yyscan_t scanner;
86
154e9ca1 87 const struct cmd_element *el;
b07a15f8
DL
88
89 struct graph *graph;
fd19e7a2 90 struct graph_node *currnode;
b07a15f8
DL
91
92 /* pointers to copy of command docstring */
93 char *docstr_start, *docstr;
94 };
92055a92
QY
95}
96
51fc9379 97/* union types for lexed tokens */
5a5d576b
QY
98%token <string> WORD
99%token <string> IPV4
100%token <string> IPV4_PREFIX
101%token <string> IPV6
102%token <string> IPV6_PREFIX
103%token <string> VARIABLE
104%token <string> RANGE
9779e3f1
QY
105%token <string> MAC
106%token <string> MAC_PREFIX
782d9789 107
51fc9379 108/* union types for parsed rules */
782d9789 109%type <node> start
782d9789
QY
110%type <node> literal_token
111%type <node> placeholder_token
16705ecc 112%type <node> placeholder_token_real
9286efab 113%type <node> simple_token
9286efab
QY
114%type <subgraph> selector
115%type <subgraph> selector_token
116%type <subgraph> selector_token_seq
117%type <subgraph> selector_seq_seq
782d9789 118
16705ecc
DL
119%type <string> varname_token
120
51fc9379 121%code {
e9484f70 122
51fc9379
QY
123 /* bison declarations */
124 void
8bb647a8 125 cmd_yyerror (CMD_YYLTYPE *locp, struct parser_ctx *ctx, char const *msg);
1eb5e8dc 126
51fc9379 127 /* helper functions for parser */
83364d20 128 static const char *
b07a15f8 129 doc_next (struct parser_ctx *ctx);
51fc9379 130
1eb5e8dc 131 static struct graph_node *
b07a15f8 132 new_token_node (struct parser_ctx *,
d0bfb22c 133 enum cmd_token_type type,
83364d20
DL
134 const char *text,
135 const char *doc);
51fc9379
QY
136
137 static void
8bb647a8 138 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
b07a15f8 139 struct graph_node *);
51fc9379
QY
140
141 static void
b07a15f8 142 cleanup (struct parser_ctx *ctx);
e9484f70 143
eb44e1aa
DL
144 static void loopcheck(struct parser_ctx *ctx, struct subgraph *sg);
145
e9484f70 146 #define scanner ctx->scanner
51fc9379
QY
147}
148
149/* yyparse parameters */
e9484f70
DL
150%lex-param {yyscan_t scanner}
151%parse-param {struct parser_ctx *ctx}
51fc9379
QY
152
153/* called automatically before yyparse */
154%initial-action {
155 /* clear state pointers */
fd19e7a2 156 ctx->currnode = vector_slot (ctx->graph->nodes, 0);
51fc9379 157
51fc9379 158 /* copy docstring and keep a pointer to the copy */
b07a15f8 159 if (ctx->el->doc)
a9958674
QY
160 {
161 // allocate a new buffer, making room for a flag
b07a15f8
DL
162 size_t length = (size_t) strlen (ctx->el->doc) + 2;
163 ctx->docstr = malloc (length);
164 memcpy (ctx->docstr, ctx->el->doc, strlen (ctx->el->doc));
a9958674 165 // set the flag so doc_next knows when to print a warning
b07a15f8 166 ctx->docstr[length - 2] = 0x03;
a9958674 167 // null terminate
b07a15f8 168 ctx->docstr[length - 1] = 0x00;
a9958674 169 }
b07a15f8 170 ctx->docstr_start = ctx->docstr;
51fc9379 171}
92055a92 172
92055a92
QY
173%%
174
88255c7c 175start:
fd19e7a2 176 cmd_token_seq
880e24a1 177{
51fc9379 178 // tack on the command element
8bb647a8 179 terminate_graph (&@1, ctx, ctx->currnode);
880e24a1 180}
fd19e7a2 181| cmd_token_seq placeholder_token '.' '.' '.'
88255c7c 182{
6432969d 183 if ((ctx->currnode = graph_add_edge (ctx->currnode, $2)) != $2)
fd19e7a2 184 graph_delete_node (ctx->graph, $2);
88255c7c 185
4d94b292 186 ((struct cmd_token *)ctx->currnode->data)->allowrepeat = 1;
7d5718c1 187
1ab84bf3 188 // adding a node as a child of itself accepts any number
a9958674 189 // of the same token, which is what we want for variadics
6432969d 190 graph_add_edge (ctx->currnode, ctx->currnode);
88255c7c 191
51fc9379 192 // tack on the command element
8bb647a8 193 terminate_graph (&@1, ctx, ctx->currnode);
88255c7c 194}
9286efab 195;
92055a92 196
16705ecc
DL
197varname_token: '$' WORD
198{
4d85f868 199 $$ = $2;
16705ecc
DL
200}
201| /* empty */
202{
203 $$ = NULL;
204}
205;
206
9286efab 207cmd_token_seq:
0d4aa1b1 208 /* empty */
9286efab
QY
209| cmd_token_seq cmd_token
210;
92055a92 211
782d9789 212cmd_token:
9286efab 213 simple_token
5a5d576b 214{
6432969d 215 if ((ctx->currnode = graph_add_edge (ctx->currnode, $1)) != $1)
b07a15f8 216 graph_delete_node (ctx->graph, $1);
5a5d576b 217}
663982cd 218| selector
5a5d576b 219{
2020b1c8
DL
220 graph_add_edge (ctx->currnode, $1.start);
221 ctx->currnode = $1.end;
478bdaeb 222}
9286efab
QY
223;
224
225simple_token:
226 literal_token
227| placeholder_token
228;
229
16705ecc 230literal_token: WORD varname_token
9286efab 231{
83364d20 232 $$ = new_token_node (ctx, WORD_TKN, $1, doc_next(ctx));
5894e76d 233 cmd_token_varname_set ($$->data, $2);
16705ecc 234 XFREE (MTYPE_LEX, $2);
55b7f20f 235 XFREE (MTYPE_LEX, $1);
9286efab 236}
782d9789
QY
237;
238
16705ecc 239placeholder_token_real:
478bdaeb 240 IPV4
4b0abf24 241{
83364d20 242 $$ = new_token_node (ctx, IPV4_TKN, $1, doc_next(ctx));
55b7f20f 243 XFREE (MTYPE_LEX, $1);
4b0abf24 244}
478bdaeb 245| IPV4_PREFIX
340a2b4a 246{
83364d20 247 $$ = new_token_node (ctx, IPV4_PREFIX_TKN, $1, doc_next(ctx));
55b7f20f 248 XFREE (MTYPE_LEX, $1);
4b0abf24 249}
478bdaeb 250| IPV6
340a2b4a 251{
83364d20 252 $$ = new_token_node (ctx, IPV6_TKN, $1, doc_next(ctx));
55b7f20f 253 XFREE (MTYPE_LEX, $1);
4b0abf24 254}
478bdaeb 255| IPV6_PREFIX
340a2b4a 256{
83364d20 257 $$ = new_token_node (ctx, IPV6_PREFIX_TKN, $1, doc_next(ctx));
55b7f20f 258 XFREE (MTYPE_LEX, $1);
4b0abf24 259}
478bdaeb 260| VARIABLE
340a2b4a 261{
83364d20 262 $$ = new_token_node (ctx, VARIABLE_TKN, $1, doc_next(ctx));
55b7f20f 263 XFREE (MTYPE_LEX, $1);
4b0abf24 264}
478bdaeb
QY
265| RANGE
266{
83364d20 267 $$ = new_token_node (ctx, RANGE_TKN, $1, doc_next(ctx));
d0bfb22c 268 struct cmd_token *token = $$->data;
478bdaeb
QY
269
270 // get the numbers out
b3899769 271 yylval.string++;
7a6ded40 272 token->min = strtoll (yylval.string, &yylval.string, 10);
ef955a80 273 strsep (&yylval.string, "-");
7a6ded40 274 token->max = strtoll (yylval.string, &yylval.string, 10);
ef955a80
QY
275
276 // validate range
8bb647a8 277 if (token->min > token->max) cmd_yyerror (&@1, ctx, "Invalid range.");
5a5d576b 278
55b7f20f 279 XFREE (MTYPE_LEX, $1);
478bdaeb 280}
9779e3f1
QY
281| MAC
282{
283 $$ = new_token_node (ctx, MAC_TKN, $1, doc_next(ctx));
284 XFREE (MTYPE_LEX, $1);
285}
286| MAC_PREFIX
287{
288 $$ = new_token_node (ctx, MAC_PREFIX_TKN, $1, doc_next(ctx));
289 XFREE (MTYPE_LEX, $1);
290}
782d9789 291
16705ecc
DL
292placeholder_token:
293 placeholder_token_real varname_token
294{
295 struct cmd_token *token = $$->data;
296 $$ = $1;
5894e76d 297 cmd_token_varname_set (token, $2);
16705ecc
DL
298 XFREE (MTYPE_LEX, $2);
299};
300
301
4b0abf24 302/* <selector|set> productions */
16705ecc 303selector: '<' selector_seq_seq '>' varname_token
2a23ca6e 304{
663982cd 305 $$ = $2;
5894e76d 306 cmd_token_varname_set ($2.end->data, $4);
16705ecc 307 XFREE (MTYPE_LEX, $4);
2a23ca6e 308};
782d9789 309
9286efab
QY
310selector_seq_seq:
311 selector_seq_seq '|' selector_token_seq
478bdaeb 312{
663982cd 313 $$ = $1;
2020b1c8
DL
314 graph_add_edge ($$.start, $3.start);
315 graph_add_edge ($3.end, $$.end);
9286efab 316}
663982cd 317| selector_token_seq
9286efab 318{
663982cd
DL
319 $$.start = new_token_node (ctx, FORK_TKN, NULL, NULL);
320 $$.end = new_token_node (ctx, JOIN_TKN, NULL, NULL);
321 ((struct cmd_token *)$$.start->data)->forkjoin = $$.end;
322 ((struct cmd_token *)$$.end->data)->forkjoin = $$.start;
323
2020b1c8
DL
324 graph_add_edge ($$.start, $1.start);
325 graph_add_edge ($1.end, $$.end);
478bdaeb 326}
9286efab 327;
782d9789 328
7d5718c1 329/* {keyword} productions */
16705ecc 330selector: '{' selector_seq_seq '}' varname_token
7d5718c1 331{
663982cd
DL
332 $$ = $2;
333 graph_add_edge ($$.end, $$.start);
334 /* there is intentionally no start->end link, for two reasons:
335 * 1) this allows "at least 1 of" semantics, which are otherwise impossible
336 * 2) this would add a start->end->start loop in the graph that the current
337 * loop-avoidal fails to handle
338 * just use [{a|b}] if neccessary, that will work perfectly fine, and reason
339 * #1 is good enough to keep it this way. */
16705ecc 340
eb44e1aa 341 loopcheck(ctx, &$$);
5894e76d 342 cmd_token_varname_set ($2.end->data, $4);
16705ecc 343 XFREE (MTYPE_LEX, $4);
7d5718c1
DL
344};
345
346
535ef556 347selector_token:
9286efab 348 simple_token
4b0abf24 349{
2020b1c8 350 $$.start = $$.end = $1;
9286efab 351}
4d12266b 352| selector
9286efab 353;
782d9789 354
663982cd
DL
355selector_token_seq:
356 selector_token_seq selector_token
2a23ca6e 357{
2020b1c8
DL
358 graph_add_edge ($1.end, $2.start);
359 $$.start = $1.start;
360 $$.end = $2.end;
2a23ca6e 361}
663982cd 362| selector_token
9286efab 363;
478bdaeb 364
663982cd 365/* [option] productions */
16705ecc 366selector: '[' selector_seq_seq ']' varname_token
9286efab 367{
663982cd
DL
368 $$ = $2;
369 graph_add_edge ($$.start, $$.end);
5894e76d 370 cmd_token_varname_set ($2.end->data, $4);
16705ecc 371 XFREE (MTYPE_LEX, $4);
9286efab 372}
782d9789
QY
373;
374
92055a92 375%%
4b0abf24 376
e9484f70
DL
377#undef scanner
378
55b7f20f
DL
379DEFINE_MTYPE(LIB, LEX, "Lexer token (temporary)")
380
1eb5e8dc 381void
154e9ca1 382cmd_graph_parse (struct graph *graph, const struct cmd_element *cmd)
51fc9379 383{
b07a15f8
DL
384 struct parser_ctx ctx = { .graph = graph, .el = cmd };
385
1ab84bf3 386 // set to 1 to enable parser traces
51fc9379
QY
387 yydebug = 0;
388
e9484f70
DL
389 set_lexer_string (&ctx.scanner, cmd->string);
390
51fc9379 391 // parse command into DFA
e9484f70
DL
392 cmd_yyparse (&ctx);
393
394 /* cleanup lexer */
395 cleanup_lexer (&ctx.scanner);
07079d78 396
7a6ded40 397 // cleanup
b07a15f8 398 cleanup (&ctx);
51fc9379
QY
399}
400
401/* parser helper functions */
402
eb44e1aa
DL
403static bool loopcheck_inner(struct graph_node *start, struct graph_node *node,
404 struct graph_node *end, size_t depth)
405{
406 size_t i;
407 bool ret;
408
409 /* safety check */
410 if (depth++ == 64)
411 return true;
412
413 for (i = 0; i < vector_active(node->to); i++) {
414 struct graph_node *next = vector_slot(node->to, i);
415 struct cmd_token *tok = next->data;
416
417 if (next == end || next == start)
418 return true;
419 if (tok->type < SPECIAL_TKN)
420 continue;
421 ret = loopcheck_inner(start, next, end, depth);
422 if (ret)
423 return true;
424 }
425 return false;
426}
427
428static void loopcheck(struct parser_ctx *ctx, struct subgraph *sg)
429{
430 if (loopcheck_inner(sg->start, sg->start, sg->end, 0))
431 zlog_err("FATAL: '%s': {} contains an empty path! Use [{...}]",
432 ctx->el->string);
433}
434
51fc9379 435void
8bb647a8 436yyerror (CMD_YYLTYPE *loc, struct parser_ctx *ctx, char const *msg)
51fc9379 437{
8bb647a8
DL
438 char *tmpstr = strdup(ctx->el->string);
439 char *line, *eol;
440 char spacing[256];
441 int lineno = 0;
442
b87478cb
DS
443 zlog_notice ("%s: FATAL parse error: %s", __func__, msg);
444 zlog_notice ("%s: %d:%d-%d of this command definition:", __func__, loc->first_line, loc->first_column, loc->last_column);
8bb647a8
DL
445
446 line = tmpstr;
447 do {
448 lineno++;
449 eol = strchr(line, '\n');
450 if (eol)
451 *eol++ = '\0';
452
b87478cb 453 zlog_notice ("%s: | %s", __func__, line);
8bb647a8
DL
454 if (lineno == loc->first_line && lineno == loc->last_line
455 && loc->first_column < (int)sizeof(spacing) - 1
456 && loc->last_column < (int)sizeof(spacing) - 1) {
457
458 int len = loc->last_column - loc->first_column;
459 if (len == 0)
460 len = 1;
461
462 memset(spacing, ' ', loc->first_column - 1);
463 memset(spacing + loc->first_column - 1, '^', len);
464 spacing[loc->first_column - 1 + len] = '\0';
b87478cb 465 zlog_notice ("%s: | %s", __func__, spacing);
8bb647a8
DL
466 }
467 } while ((line = eol));
468 free(tmpstr);
92055a92 469}
782d9789 470
51fc9379 471static void
b07a15f8 472cleanup (struct parser_ctx *ctx)
782d9789 473{
51fc9379 474 /* free resources */
b07a15f8 475 free (ctx->docstr_start);
478bdaeb 476
4b0abf24 477 /* clear state pointers */
b07a15f8
DL
478 ctx->currnode = NULL;
479 ctx->docstr_start = ctx->docstr = NULL;
51fc9379 480}
4b0abf24 481
51fc9379 482static void
8bb647a8
DL
483terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
484 struct graph_node *finalnode)
51fc9379 485{
1eb5e8dc
QY
486 // end of graph should look like this
487 // * -> finalnode -> END_TKN -> cmd_element
154e9ca1 488 const struct cmd_element *element = ctx->el;
d8074cc0 489 struct graph_node *end_token_node =
83364d20 490 new_token_node (ctx, END_TKN, CMD_CR_TEXT, "");
1eb5e8dc 491 struct graph_node *end_element_node =
154e9ca1 492 graph_new_node (ctx->graph, (void *)element, NULL);
7a6ded40 493
ebb08130
QY
494 if (ctx->docstr && strlen (ctx->docstr) > 1) {
495 zlog_debug ("Excessive docstring while parsing '%s'", ctx->el->string);
496 zlog_debug ("----------");
497 while (ctx->docstr && ctx->docstr[1] != '\0')
498 zlog_debug ("%s", strsep(&ctx->docstr, "\n"));
499 zlog_debug ("----------\n");
500 }
501
1eb5e8dc
QY
502 graph_add_edge (finalnode, end_token_node);
503 graph_add_edge (end_token_node, end_element_node);
782d9789 504}
f4b0c72e 505
83364d20 506static const char *
b07a15f8 507doc_next (struct parser_ctx *ctx)
51fc9379 508{
b07a15f8 509 const char *piece = ctx->docstr ? strsep (&ctx->docstr, "\n") : "";
a9958674
QY
510 if (*piece == 0x03)
511 {
b07a15f8 512 zlog_debug ("Ran out of docstring while parsing '%s'", ctx->el->string);
a9958674
QY
513 piece = "";
514 }
515
83364d20 516 return piece;
51fc9379
QY
517}
518
519static struct graph_node *
b07a15f8 520new_token_node (struct parser_ctx *ctx, enum cmd_token_type type,
83364d20 521 const char *text, const char *doc)
7a6ded40 522{
5894e76d
DL
523 struct cmd_token *token = cmd_token_new (type, ctx->el->attr, text, doc);
524 return graph_new_node (ctx->graph, token, (void (*)(void *)) &cmd_token_del);
7a6ded40 525}