X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=lib%2Fcommand_graph.h;h=82d562694cd844e67219af6736f97912964abb80;hb=c52e2ecf95a9be318912caacc0851d9307e679f7;hp=8d23577d378dcfe359a03e66e3605869bad10c73;hpb=9d0662e009c0cf4532b88c9a1fb0f7c0dc174584;p=mirror_frr.git diff --git a/lib/command_graph.h b/lib/command_graph.h index 8d23577d3..82d562694 100644 --- a/lib/command_graph.h +++ b/lib/command_graph.h @@ -1,85 +1,136 @@ -#ifndef COMMAND_GRAPH_H -#define COMMAND_GRAPH_H +/* + * CLI graph handling + * + * -- + * Copyright (C) 2016 Cumulus Networks, Inc. + * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro + * Copyright (C) 2013 by Open Source Routing. + * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC") + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _FRR_COMMAND_GRAPH_H +#define _FRR_COMMAND_GRAPH_H + +#include +#include -#include "vty.h" +#include "memory.h" #include "vector.h" +#include "graph.h" + +DECLARE_MTYPE(CMD_ARG) + +struct vty; + +/** + * Types for tokens. + * + * The type determines what kind of data the token can match (in the + * matching use case) or hold (in the argv use case). + */ +/* clang-format off */ +enum cmd_token_type { + WORD_TKN, // words + VARIABLE_TKN, // almost anything + RANGE_TKN, // integer range + IPV4_TKN, // IPV4 addresses + IPV4_PREFIX_TKN, // IPV4 network prefixes + IPV6_TKN, // IPV6 prefixes + IPV6_PREFIX_TKN, // IPV6 network prefixes + MAC_TKN, // Ethernet address + MAC_PREFIX_TKN, // Ethernet address w/ CIDR mask + + /* plumbing types */ + FORK_TKN, // marks subgraph beginning + JOIN_TKN, // marks subgraph end + START_TKN, // first token in line + END_TKN, // last token in line -enum graph_node_type -{ - WORD_GN, - IPV4_GN, - IPV4_PREFIX_GN, - IPV6_GN, - IPV6_PREFIX_GN, - VARIABLE_GN, - RANGE_GN, - NUMBER_GN, - SELECTOR_GN, - OPTION_GN, - NUL_GN + SPECIAL_TKN = FORK_TKN, }; +/* clang-format on */ -struct graph_node -{ - enum graph_node_type type; - vector children; - int is_root; // true if first token in command - int is_leaf; // true if last token in command - struct graph_node * end; // pointer to end for selector & option +#define IS_VARYING_TOKEN(x) ((x) >= VARIABLE_TKN && (x) < FORK_TKN) - int (*func)(struct vty *, int, const char *[]); +/* Command attributes */ +enum { CMD_ATTR_NORMAL, + CMD_ATTR_DEPRECATED, + CMD_ATTR_HIDDEN, +}; + +/* Comamand token struct. */ +struct cmd_token { + enum cmd_token_type type; // token type + uint8_t attr; // token attributes + bool allowrepeat; // matcher allowed to match token repetively? + uint32_t refcnt; - /* various data fields for nodes */ - char* text; // for words and variables - int value; // for numbers - int min, max; // for ranges + char *text; // token text + char *desc; // token description + long long min, max; // for ranges + char *arg; // user input that matches this token + char *varname; + + struct graph_node *forkjoin; // paired FORK/JOIN for JOIN/FORK }; -/* - * Adds a child to a node. - * If the node already has the exact same child, nothing is done. This is - * decided with cmp_node. - * - * @param[in] parent node - * @param[in] child node - * @return the new child, or the existing child if the parent already has the - * new child - */ -extern struct graph_node * -add_node(struct graph_node *, struct graph_node *); +/* Structure of command element. */ +struct cmd_element { + const char *string; /* Command specification by string. */ + const char *doc; /* Documentation of this command. */ + int daemon; /* Daemon to which this command belong. */ + uint8_t attr; /* Command attributes */ + /* handler function for command */ + int (*func)(const struct cmd_element *, struct vty *, int, + struct cmd_token *[]); + + const char *name; /* symbol name for debugging */ +}; + +/* text for command */ +#define CMD_CR_TEXT "" + +/* memory management for cmd_token */ +extern struct cmd_token *cmd_token_new(enum cmd_token_type, uint8_t attr, + const char *text, const char *desc); +extern struct cmd_token *cmd_token_dup(struct cmd_token *); +extern void cmd_token_del(struct cmd_token *); +extern void cmd_token_varname_set(struct cmd_token *token, const char *varname); + +extern void cmd_graph_parse(struct graph *graph, struct cmd_element *cmd); +extern void cmd_graph_names(struct graph *graph); +extern void cmd_graph_merge(struct graph *old, struct graph *new, + int direction); /* - * Compares two nodes for parsing equivalence. - * Equivalence in this case means that a single user input token - * should be able to unambiguously match one of the two nodes. - * For example, two nodes which have all fields equal except their - * function pointers would be considered equal. + * Print callback for DOT dumping. * - * @param[in] first node to compare - * @param[in] second node to compare - * @return 1 if equal, zero otherwise. + * See graph.h for more details. */ -extern int -cmp_node(struct graph_node *, struct graph_node *); - +extern void cmd_graph_node_print_cb(struct graph_node *gn, struct buffer *buf); /* - * Create a new node. - * Initializes all fields to default values and sets the node type. + * Dump command graph to DOT. * - * @param[in] node type - * @return pointer to the newly allocated node - */ -extern struct graph_node * -new_node(enum graph_node_type); - -/** - * Walks a command DFA, printing structure to stdout. - * For debugging. + * cmdgraph + * A command graph to dump * - * @param[in] start node of graph to walk - * @param[in] graph depth for recursion, caller passes 0 + * Returns: + * String allocated with MTYPE_TMP representing this graph */ -extern void -walk_graph(struct graph_node *, int); +char *cmd_graph_dump_dot(struct graph *cmdgraph); -#endif +#endif /* _FRR_COMMAND_GRAPH_H */