]> git.proxmox.com Git - mirror_frr.git/blob - lib/command_parse.y
Merge remote-tracking branch 'origin/stable/2.0'
[mirror_frr.git] / lib / command_parse.y
1 /*
2 * Command format string parser for CLI backend.
3 *
4 * --
5 * Copyright (C) 2016 Cumulus Networks, Inc.
6 *
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.
23 */
24
25 %{
26 // compile with debugging facilities
27 #define YYDEBUG 1
28 %}
29
30 %locations
31 /* define parse.error verbose */
32 %define api.pure full
33 /* define api.prefix {cmd_yy} */
34
35 /* names for generated header and parser files */
36 %defines "command_parse.h"
37 %output "command_parse.c"
38
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 */
46 %code requires {
47 #include "stdlib.h"
48 #include "string.h"
49 #include "memory.h"
50 #include "command.h"
51 #include "log.h"
52 #include "graph.h"
53
54 DECLARE_MTYPE(LEX)
55
56 #define YYSTYPE CMD_YYSTYPE
57 #define YYLTYPE CMD_YYLTYPE
58 struct parser_ctx;
59
60 /* subgraph semantic value */
61 struct subgraph {
62 struct graph_node *start, *end;
63 };
64 }
65
66 %union {
67 long long number;
68 char *string;
69 struct graph_node *node;
70 struct subgraph subgraph;
71 }
72
73 %code provides {
74 #ifndef FLEX_SCANNER
75 #include "command_lex.h"
76 #endif
77
78 extern void set_lexer_string (yyscan_t *scn, const char *string);
79 extern void cleanup_lexer (yyscan_t *scn);
80
81 struct parser_ctx {
82 yyscan_t scanner;
83
84 struct cmd_element *el;
85
86 struct graph *graph;
87 struct graph_node *currnode;
88
89 /* pointers to copy of command docstring */
90 char *docstr_start, *docstr;
91 };
92 }
93
94 /* union types for lexed tokens */
95 %token <string> WORD
96 %token <string> IPV4
97 %token <string> IPV4_PREFIX
98 %token <string> IPV6
99 %token <string> IPV6_PREFIX
100 %token <string> VARIABLE
101 %token <string> RANGE
102
103 /* union types for parsed rules */
104 %type <node> start
105 %type <node> literal_token
106 %type <node> placeholder_token
107 %type <node> simple_token
108 %type <subgraph> selector
109 %type <subgraph> selector_token
110 %type <subgraph> selector_token_seq
111 %type <subgraph> selector_seq_seq
112
113 %code {
114
115 /* bison declarations */
116 void
117 cmd_yyerror (CMD_YYLTYPE *locp, struct parser_ctx *ctx, char const *msg);
118
119 /* helper functions for parser */
120 static const char *
121 doc_next (struct parser_ctx *ctx);
122
123 static struct graph_node *
124 new_token_node (struct parser_ctx *,
125 enum cmd_token_type type,
126 const char *text,
127 const char *doc);
128
129 static void
130 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
131 struct graph_node *);
132
133 static void
134 cleanup (struct parser_ctx *ctx);
135
136 #define scanner ctx->scanner
137 }
138
139 /* yyparse parameters */
140 %lex-param {yyscan_t scanner}
141 %parse-param {struct parser_ctx *ctx}
142
143 /* called automatically before yyparse */
144 %initial-action {
145 /* clear state pointers */
146 ctx->currnode = vector_slot (ctx->graph->nodes, 0);
147
148 /* copy docstring and keep a pointer to the copy */
149 if (ctx->el->doc)
150 {
151 // allocate a new buffer, making room for a flag
152 size_t length = (size_t) strlen (ctx->el->doc) + 2;
153 ctx->docstr = malloc (length);
154 memcpy (ctx->docstr, ctx->el->doc, strlen (ctx->el->doc));
155 // set the flag so doc_next knows when to print a warning
156 ctx->docstr[length - 2] = 0x03;
157 // null terminate
158 ctx->docstr[length - 1] = 0x00;
159 }
160 ctx->docstr_start = ctx->docstr;
161 }
162
163 %%
164
165 start:
166 cmd_token_seq
167 {
168 // tack on the command element
169 terminate_graph (&@1, ctx, ctx->currnode);
170 }
171 | cmd_token_seq placeholder_token '.' '.' '.'
172 {
173 if ((ctx->currnode = graph_add_edge (ctx->currnode, $2)) != $2)
174 graph_delete_node (ctx->graph, $2);
175
176 ((struct cmd_token *)ctx->currnode->data)->allowrepeat = 1;
177
178 // adding a node as a child of itself accepts any number
179 // of the same token, which is what we want for variadics
180 graph_add_edge (ctx->currnode, ctx->currnode);
181
182 // tack on the command element
183 terminate_graph (&@1, ctx, ctx->currnode);
184 }
185 ;
186
187 cmd_token_seq:
188 /* empty */
189 | cmd_token_seq cmd_token
190 ;
191
192 cmd_token:
193 simple_token
194 {
195 if ((ctx->currnode = graph_add_edge (ctx->currnode, $1)) != $1)
196 graph_delete_node (ctx->graph, $1);
197 }
198 | selector
199 {
200 graph_add_edge (ctx->currnode, $1.start);
201 ctx->currnode = $1.end;
202 }
203 ;
204
205 simple_token:
206 literal_token
207 | placeholder_token
208 ;
209
210 literal_token: WORD
211 {
212 $$ = new_token_node (ctx, WORD_TKN, $1, doc_next(ctx));
213 XFREE (MTYPE_LEX, $1);
214 }
215 ;
216
217 placeholder_token:
218 IPV4
219 {
220 $$ = new_token_node (ctx, IPV4_TKN, $1, doc_next(ctx));
221 XFREE (MTYPE_LEX, $1);
222 }
223 | IPV4_PREFIX
224 {
225 $$ = new_token_node (ctx, IPV4_PREFIX_TKN, $1, doc_next(ctx));
226 XFREE (MTYPE_LEX, $1);
227 }
228 | IPV6
229 {
230 $$ = new_token_node (ctx, IPV6_TKN, $1, doc_next(ctx));
231 XFREE (MTYPE_LEX, $1);
232 }
233 | IPV6_PREFIX
234 {
235 $$ = new_token_node (ctx, IPV6_PREFIX_TKN, $1, doc_next(ctx));
236 XFREE (MTYPE_LEX, $1);
237 }
238 | VARIABLE
239 {
240 $$ = new_token_node (ctx, VARIABLE_TKN, $1, doc_next(ctx));
241 XFREE (MTYPE_LEX, $1);
242 }
243 | RANGE
244 {
245 $$ = new_token_node (ctx, RANGE_TKN, $1, doc_next(ctx));
246 struct cmd_token *token = $$->data;
247
248 // get the numbers out
249 yylval.string++;
250 token->min = strtoll (yylval.string, &yylval.string, 10);
251 strsep (&yylval.string, "-");
252 token->max = strtoll (yylval.string, &yylval.string, 10);
253
254 // validate range
255 if (token->min > token->max) cmd_yyerror (&@1, ctx, "Invalid range.");
256
257 XFREE (MTYPE_LEX, $1);
258 }
259
260 /* <selector|set> productions */
261 selector: '<' selector_seq_seq '>'
262 {
263 $$ = $2;
264 };
265
266 selector_seq_seq:
267 selector_seq_seq '|' selector_token_seq
268 {
269 $$ = $1;
270 graph_add_edge ($$.start, $3.start);
271 graph_add_edge ($3.end, $$.end);
272 }
273 | selector_token_seq
274 {
275 $$.start = new_token_node (ctx, FORK_TKN, NULL, NULL);
276 $$.end = new_token_node (ctx, JOIN_TKN, NULL, NULL);
277 ((struct cmd_token *)$$.start->data)->forkjoin = $$.end;
278 ((struct cmd_token *)$$.end->data)->forkjoin = $$.start;
279
280 graph_add_edge ($$.start, $1.start);
281 graph_add_edge ($1.end, $$.end);
282 }
283 ;
284
285 /* {keyword} productions */
286 selector: '{' selector_seq_seq '}'
287 {
288 $$ = $2;
289 graph_add_edge ($$.end, $$.start);
290 /* there is intentionally no start->end link, for two reasons:
291 * 1) this allows "at least 1 of" semantics, which are otherwise impossible
292 * 2) this would add a start->end->start loop in the graph that the current
293 * loop-avoidal fails to handle
294 * just use [{a|b}] if neccessary, that will work perfectly fine, and reason
295 * #1 is good enough to keep it this way. */
296 };
297
298
299 selector_token:
300 simple_token
301 {
302 $$.start = $$.end = $1;
303 }
304 | selector
305 ;
306
307 selector_token_seq:
308 selector_token_seq selector_token
309 {
310 graph_add_edge ($1.end, $2.start);
311 $$.start = $1.start;
312 $$.end = $2.end;
313 }
314 | selector_token
315 ;
316
317 /* [option] productions */
318 selector: '[' selector_seq_seq ']'
319 {
320 $$ = $2;
321 graph_add_edge ($$.start, $$.end);
322 }
323 ;
324
325 %%
326
327 #undef scanner
328
329 DEFINE_MTYPE(LIB, LEX, "Lexer token (temporary)")
330
331 void
332 command_parse_format (struct graph *graph, struct cmd_element *cmd)
333 {
334 struct parser_ctx ctx = { .graph = graph, .el = cmd };
335
336 // set to 1 to enable parser traces
337 yydebug = 0;
338
339 set_lexer_string (&ctx.scanner, cmd->string);
340
341 // parse command into DFA
342 cmd_yyparse (&ctx);
343
344 /* cleanup lexer */
345 cleanup_lexer (&ctx.scanner);
346
347 // cleanup
348 cleanup (&ctx);
349 }
350
351 /* parser helper functions */
352
353 void
354 yyerror (CMD_YYLTYPE *loc, struct parser_ctx *ctx, char const *msg)
355 {
356 char *tmpstr = strdup(ctx->el->string);
357 char *line, *eol;
358 char spacing[256];
359 int lineno = 0;
360
361 zlog_err ("%s: FATAL parse error: %s", __func__, msg);
362 zlog_err ("%s: %d:%d-%d of this command definition:", __func__, loc->first_line, loc->first_column, loc->last_column);
363
364 line = tmpstr;
365 do {
366 lineno++;
367 eol = strchr(line, '\n');
368 if (eol)
369 *eol++ = '\0';
370
371 zlog_err ("%s: | %s", __func__, line);
372 if (lineno == loc->first_line && lineno == loc->last_line
373 && loc->first_column < (int)sizeof(spacing) - 1
374 && loc->last_column < (int)sizeof(spacing) - 1) {
375
376 int len = loc->last_column - loc->first_column;
377 if (len == 0)
378 len = 1;
379
380 memset(spacing, ' ', loc->first_column - 1);
381 memset(spacing + loc->first_column - 1, '^', len);
382 spacing[loc->first_column - 1 + len] = '\0';
383 zlog_err ("%s: | %s", __func__, spacing);
384 }
385 } while ((line = eol));
386 free(tmpstr);
387 }
388
389 static void
390 cleanup (struct parser_ctx *ctx)
391 {
392 /* free resources */
393 free (ctx->docstr_start);
394
395 /* clear state pointers */
396 ctx->currnode = NULL;
397 ctx->docstr_start = ctx->docstr = NULL;
398 }
399
400 static void
401 terminate_graph (CMD_YYLTYPE *locp, struct parser_ctx *ctx,
402 struct graph_node *finalnode)
403 {
404 // end of graph should look like this
405 // * -> finalnode -> END_TKN -> cmd_element
406 struct cmd_element *element = ctx->el;
407 struct graph_node *end_token_node =
408 new_token_node (ctx, END_TKN, CMD_CR_TEXT, "");
409 struct graph_node *end_element_node =
410 graph_new_node (ctx->graph, element, NULL);
411
412 graph_add_edge (finalnode, end_token_node);
413 graph_add_edge (end_token_node, end_element_node);
414 }
415
416 static const char *
417 doc_next (struct parser_ctx *ctx)
418 {
419 const char *piece = ctx->docstr ? strsep (&ctx->docstr, "\n") : "";
420 if (*piece == 0x03)
421 {
422 zlog_debug ("Ran out of docstring while parsing '%s'", ctx->el->string);
423 piece = "";
424 }
425
426 return piece;
427 }
428
429 static struct graph_node *
430 new_token_node (struct parser_ctx *ctx, enum cmd_token_type type,
431 const char *text, const char *doc)
432 {
433 struct cmd_token *token = new_cmd_token (type, ctx->el->attr, text, doc);
434 return graph_new_node (ctx->graph, token, (void (*)(void *)) &del_cmd_token);
435 }