summaryrefslogtreecommitdiffstats
path: root/src/vty/command.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2010-05-19 19:02:52 +0200
committerHarald Welte <laforge@gnumonks.org>2010-05-25 22:28:39 +0200
commit3fb0b6f26e24370490f1051e2c795ba8fa6ac892 (patch)
tree1f3b86f491e7f0812f5424daaffd4db9cf8ac452 /src/vty/command.c
parent75bd69bc914d4ecd1b19f65662889af173aca64c (diff)
Create libosmovty as library from OpenBSC VTY functions
Diffstat (limited to 'src/vty/command.c')
-rw-r--r--src/vty/command.c3176
1 files changed, 3176 insertions, 0 deletions
diff --git a/src/vty/command.c b/src/vty/command.c
new file mode 100644
index 00000000..7275a3d4
--- /dev/null
+++ b/src/vty/command.c
@@ -0,0 +1,3176 @@
+/*
+ $Id: command.c,v 1.47 2005/04/25 16:26:42 paul Exp $
+
+ Command interpreter routine for virtual terminal [aka TeletYpe]
+ Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
+
+This file is part of GNU Zebra.
+
+GNU Zebra 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, or (at your
+option) any later version.
+
+GNU Zebra 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 GNU Zebra; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+#include <errno.h>
+#define _XOPEN_SOURCE
+#include <unistd.h>
+#include <assert.h>
+#include <ctype.h>
+#include <time.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+
+#include <osmocom/vty/vector.h>
+#include <osmocom/vty/vty.h>
+#include <osmocom/vty/command.h>
+
+#include <osmocore/talloc.h>
+
+#define CONFIGFILE_MASK 022
+
+void *tall_vty_cmd_ctx;
+
+/* Command vector which includes some level of command lists. Normally
+ each daemon maintains each own cmdvec. */
+vector cmdvec;
+
+/* Host information structure. */
+struct host host;
+
+/* Standard command node structures. */
+struct cmd_node auth_node = {
+ AUTH_NODE,
+ "Password: ",
+};
+
+struct cmd_node view_node = {
+ VIEW_NODE,
+ "%s> ",
+};
+
+struct cmd_node auth_enable_node = {
+ AUTH_ENABLE_NODE,
+ "Password: ",
+};
+
+struct cmd_node enable_node = {
+ ENABLE_NODE,
+ "%s# ",
+};
+
+struct cmd_node config_node = {
+ CONFIG_NODE,
+ "%s(config)# ",
+ 1
+};
+
+/* Default motd string. */
+const char *default_motd = "";
+
+/* This is called from main when a daemon is invoked with -v or --version. */
+void print_version(int print_copyright)
+{
+ printf("%s version %s\n", host.prog_name, host.prog_version);
+ if (print_copyright)
+ printf("\n%s\n", host.prog_copyright);
+}
+
+/* Utility function to concatenate argv argument into a single string
+ with inserting ' ' character between each argument. */
+char *argv_concat(const char **argv, int argc, int shift)
+{
+ int i;
+ size_t len;
+ char *str;
+ char *p;
+
+ len = 0;
+ for (i = shift; i < argc; i++)
+ len += strlen(argv[i]) + 1;
+ if (!len)
+ return NULL;
+ p = str = _talloc_zero(tall_vty_cmd_ctx, len, "arvg_concat");
+ for (i = shift; i < argc; i++) {
+ size_t arglen;
+ memcpy(p, argv[i], (arglen = strlen(argv[i])));
+ p += arglen;
+ *p++ = ' ';
+ }
+ *(p - 1) = '\0';
+ return str;
+}
+
+/* Install top node of command vector. */
+void install_node(struct cmd_node *node, int (*func) (struct vty *))
+{
+ vector_set_index(cmdvec, node->node, node);
+ node->func = func;
+ node->cmd_vector = vector_init(VECTOR_MIN_SIZE);
+}
+
+/* Compare two command's string. Used in sort_node (). */
+static int cmp_node(const void *p, const void *q)
+{
+ struct cmd_element *a = *(struct cmd_element **)p;
+ struct cmd_element *b = *(struct cmd_element **)q;
+
+ return strcmp(a->string, b->string);
+}
+
+static int cmp_desc(const void *p, const void *q)
+{
+ struct desc *a = *(struct desc **)p;
+ struct desc *b = *(struct desc **)q;
+
+ return strcmp(a->cmd, b->cmd);
+}
+
+/* Sort each node's command element according to command string. */
+void sort_node()
+{
+ unsigned int i, j;
+ struct cmd_node *cnode;
+ vector descvec;
+ struct cmd_element *cmd_element;
+
+ for (i = 0; i < vector_active(cmdvec); i++)
+ if ((cnode = vector_slot(cmdvec, i)) != NULL) {
+ vector cmd_vector = cnode->cmd_vector;
+ qsort(cmd_vector->index, vector_active(cmd_vector),
+ sizeof(void *), cmp_node);
+
+ for (j = 0; j < vector_active(cmd_vector); j++)
+ if ((cmd_element =
+ vector_slot(cmd_vector, j)) != NULL
+ && vector_active(cmd_element->strvec)) {
+ descvec =
+ vector_slot(cmd_element->strvec,
+ vector_active
+ (cmd_element->strvec) -
+ 1);
+ qsort(descvec->index,
+ vector_active(descvec),
+ sizeof(void *), cmp_desc);
+ }
+ }
+}
+
+/* Breaking up string into each command piece. I assume given
+ character is separated by a space character. Return value is a
+ vector which includes char ** data element. */
+vector cmd_make_strvec(const char *string)
+{
+ const char *cp, *start;
+ char *token;
+ int strlen;
+ vector strvec;
+
+ if (string == NULL)
+ return NULL;
+
+ cp = string;
+
+ /* Skip white spaces. */
+ while (isspace((int)*cp) && *cp != '\0')
+ cp++;
+
+ /* Return if there is only white spaces */
+ if (*cp == '\0')
+ return NULL;
+
+ if (*cp == '!' || *cp == '#')
+ return NULL;
+
+ /* Prepare return vector. */
+ strvec = vector_init(VECTOR_MIN_SIZE);
+
+ /* Copy each command piece and set into vector. */
+ while (1) {
+ start = cp;
+ while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
+ *cp != '\0')
+ cp++;
+ strlen = cp - start;
+ token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "make_strvec");
+ memcpy(token, start, strlen);
+ *(token + strlen) = '\0';
+ vector_set(strvec, token);
+
+ while ((isspace((int)*cp) || *cp == '\n' || *cp == '\r') &&
+ *cp != '\0')
+ cp++;
+
+ if (*cp == '\0')
+ return strvec;
+ }
+}
+
+/* Free allocated string vector. */
+void cmd_free_strvec(vector v)
+{
+ unsigned int i;
+ char *cp;
+
+ if (!v)
+ return;
+
+ for (i = 0; i < vector_active(v); i++)
+ if ((cp = vector_slot(v, i)) != NULL)
+ talloc_free(cp);
+
+ vector_free(v);
+}
+
+/* Fetch next description. Used in cmd_make_descvec(). */
+static char *cmd_desc_str(const char **string)
+{
+ const char *cp, *start;
+ char *token;
+ int strlen;
+
+ cp = *string;
+
+ if (cp == NULL)
+ return NULL;
+
+ /* Skip white spaces. */
+ while (isspace((int)*cp) && *cp != '\0')
+ cp++;
+
+ /* Return if there is only white spaces */
+ if (*cp == '\0')
+ return NULL;
+
+ start = cp;
+
+ while (!(*cp == '\r' || *cp == '\n') && *cp != '\0')
+ cp++;
+
+ strlen = cp - start;
+ token = _talloc_zero(tall_vty_cmd_ctx, strlen + 1, "cmd_desc_str");
+ memcpy(token, start, strlen);
+ *(token + strlen) = '\0';
+
+ *string = cp;
+
+ return token;
+}
+
+/* New string vector. */
+static vector cmd_make_descvec(const char *string, const char *descstr)
+{
+ int multiple = 0;
+ const char *sp;
+ char *token;
+ int len;
+ const char *cp;
+ const char *dp;
+ vector allvec;
+ vector strvec = NULL;
+ struct desc *desc;
+
+ cp = string;
+ dp = descstr;
+
+ if (cp == NULL)
+ return NULL;
+
+ allvec = vector_init(VECTOR_MIN_SIZE);
+
+ while (1) {
+ while (isspace((int)*cp) && *cp != '\0')
+ cp++;
+
+ if (*cp == '(') {
+ multiple = 1;
+ cp++;
+ }
+ if (*cp == ')') {
+ multiple = 0;
+ cp++;
+ }
+ if (*cp == '|') {
+ if (!multiple) {
+ fprintf(stderr, "Command parse error!: %s\n",
+ string);
+ exit(1);
+ }
+ cp++;
+ }
+
+ while (isspace((int)*cp) && *cp != '\0')
+ cp++;
+
+ if (*cp == '(') {
+ multiple = 1;
+ cp++;
+ }
+
+ if (*cp == '\0')
+ return allvec;
+
+ sp = cp;
+
+ while (!
+ (isspace((int)*cp) || *cp == '\r' || *cp == '\n'
+ || *cp == ')' || *cp == '|') && *cp != '\0')
+ cp++;
+
+ len = cp - sp;
+
+ token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec");
+ memcpy(token, sp, len);
+ *(token + len) = '\0';
+
+ desc = talloc_zero(tall_vty_cmd_ctx, struct desc);
+ desc->cmd = token;
+ desc->str = cmd_desc_str(&dp);
+
+ if (multiple) {
+ if (multiple == 1) {
+ strvec = vector_init(VECTOR_MIN_SIZE);
+ vector_set(allvec, strvec);
+ }
+ multiple++;
+ } else {
+ strvec = vector_init(VECTOR_MIN_SIZE);
+ vector_set(allvec, strvec);
+ }
+ vector_set(strvec, desc);
+ }
+}
+
+/* Count mandantory string vector size. This is to determine inputed
+ command has enough command length. */
+static int cmd_cmdsize(vector strvec)
+{
+ unsigned int i;
+ int size = 0;
+ vector descvec;
+ struct desc *desc;
+
+ for (i = 0; i < vector_active(strvec); i++)
+ if ((descvec = vector_slot(strvec, i)) != NULL) {
+ if ((vector_active(descvec)) == 1
+ && (desc = vector_slot(descvec, 0)) != NULL) {
+ if (desc->cmd == NULL || CMD_OPTION(desc->cmd))
+ return size;
+ else
+ size++;
+ } else
+ size++;
+ }
+ return size;
+}
+
+/* Return prompt character of specified node. */
+const char *cmd_prompt(enum node_type node)
+{
+ struct cmd_node *cnode;
+
+ cnode = vector_slot(cmdvec, node);
+ return cnode->prompt;
+}
+
+/* Install a command into a node. */
+void install_element(enum node_type ntype, struct cmd_element *cmd)
+{
+ struct cmd_node *cnode;
+
+ cnode = vector_slot(cmdvec, ntype);
+
+ if (cnode == NULL) {
+ fprintf(stderr,
+ "Command node %d doesn't exist, please check it\n",
+ ntype);
+ exit(1);
+ }
+
+ vector_set(cnode->cmd_vector, cmd);
+
+ cmd->strvec = cmd_make_descvec(cmd->string, cmd->doc);
+ cmd->cmdsize = cmd_cmdsize(cmd->strvec);
+}
+
+/* Install a command into VIEW and ENABLE node */
+void install_element_ve(struct cmd_element *cmd)
+{
+ install_element(VIEW_NODE, cmd);
+ install_element(ENABLE_NODE, cmd);
+}
+
+#ifdef VTY_CRYPT_PW
+static unsigned char itoa64[] =
+ "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+
+static void to64(char *s, long v, int n)
+{
+ while (--n >= 0) {
+ *s++ = itoa64[v & 0x3f];
+ v >>= 6;
+ }
+}
+
+static char *zencrypt(const char *passwd)
+{
+ char salt[6];
+ struct timeval tv;
+ char *crypt(const char *, const char *);
+
+ gettimeofday(&tv, 0);
+
+ to64(&salt[0], random(), 3);
+ to64(&salt[3], tv.tv_usec, 3);
+ salt[5] = '\0';
+
+ return crypt(passwd, salt);
+}
+#endif
+
+/* This function write configuration of this host. */
+static int config_write_host(struct vty *vty)
+{
+ if (host.name)
+ vty_out(vty, "hostname %s%s", host.name, VTY_NEWLINE);
+
+ if (host.encrypt) {
+ if (host.password_encrypt)
+ vty_out(vty, "password 8 %s%s", host.password_encrypt,
+ VTY_NEWLINE);
+ if (host.enable_encrypt)
+ vty_out(vty, "enable password 8 %s%s",
+ host.enable_encrypt, VTY_NEWLINE);
+ } else {
+ if (host.password)
+ vty_out(vty, "password %s%s", host.password,
+ VTY_NEWLINE);
+ if (host.enable)
+ vty_out(vty, "enable password %s%s", host.enable,
+ VTY_NEWLINE);
+ }
+
+ if (host.advanced)
+ vty_out(vty, "service advanced-vty%s", VTY_NEWLINE);
+
+ if (host.encrypt)
+ vty_out(vty, "service password-encryption%s", VTY_NEWLINE);
+
+ if (host.lines >= 0)
+ vty_out(vty, "service terminal-length %d%s", host.lines,
+ VTY_NEWLINE);
+
+ if (host.motdfile)
+ vty_out(vty, "banner motd file %s%s", host.motdfile,
+ VTY_NEWLINE);
+ else if (!host.motd)
+ vty_out(vty, "no banner motd%s", VTY_NEWLINE);
+
+ return 1;
+}
+
+/* Utility function for getting command vector. */
+static vector cmd_node_vector(vector v, enum node_type ntype)
+{
+ struct cmd_node *cnode = vector_slot(v, ntype);
+ return cnode->cmd_vector;
+}
+
+/* Completion match types. */
+enum match_type {
+ no_match,
+ extend_match,
+ ipv4_prefix_match,
+ ipv4_match,
+ ipv6_prefix_match,
+ ipv6_match,
+ range_match,
+ vararg_match,
+ partly_match,
+ exact_match
+};
+
+static enum match_type cmd_ipv4_match(const char *str)
+{
+ const char *sp;
+ int dots = 0, nums = 0;
+ char buf[4];
+
+ if (str == NULL)
+ return partly_match;
+
+ for (;;) {
+ memset(buf, 0, sizeof(buf));
+ sp = str;
+ while (*str != '\0') {
+ if (*str == '.') {
+ if (dots >= 3)
+ return no_match;
+
+ if (*(str + 1) == '.')
+ return no_match;
+
+ if (*(str + 1) == '\0')
+ return partly_match;
+
+ dots++;
+ break;
+ }
+ if (!isdigit((int)*str))
+ return no_match;
+
+ str++;
+ }
+
+ if (str - sp > 3)
+ return no_match;
+
+ strncpy(buf, sp, str - sp);
+ if (atoi(buf) > 255)
+ return no_match;
+
+ nums++;
+
+ if (*str == '\0')
+ break;
+
+ str++;
+ }
+
+ if (nums < 4)
+ return partly_match;
+
+ return exact_match;
+}
+
+static enum match_type cmd_ipv4_prefix_match(const char *str)
+{
+ const char *sp;
+ int dots = 0;
+ char buf[4];
+
+ if (str == NULL)
+ return partly_match;
+
+ for (;;) {
+ memset(buf, 0, sizeof(buf));
+ sp = str;
+ while (*str != '\0' && *str != '/') {
+ if (*str == '.') {
+ if (dots == 3)
+ return no_match;
+
+ if (*(str + 1) == '.' || *(str + 1) == '/')
+ return no_match;
+
+ if (*(str + 1) == '\0')
+ return partly_match;
+
+ dots++;
+ break;
+ }
+
+ if (!isdigit((int)*str))
+ return no_match;
+
+ str++;
+ }
+
+ if (str - sp > 3)
+ return no_match;
+
+ strncpy(buf, sp, str - sp);
+ if (atoi(buf) > 255)
+ return no_match;
+
+ if (dots == 3) {
+ if (*str == '/') {
+ if (*(str + 1) == '\0')
+ return partly_match;
+
+ str++;
+ break;
+ } else if (*str == '\0')
+ return partly_match;
+ }
+
+ if (*str == '\0')
+ return partly_match;
+
+ str++;
+ }
+
+ sp = str;
+ while (*str != '\0') {
+ if (!isdigit((int)*str))
+ return no_match;
+
+ str++;
+ }
+
+ if (atoi(sp) > 32)
+ return no_match;
+
+ return exact_match;
+}
+
+#define IPV6_ADDR_STR "0123456789abcdefABCDEF:.%"
+#define IPV6_PREFIX_STR "0123456789abcdefABCDEF:.%/"
+#define STATE_START 1
+#define STATE_COLON 2
+#define STATE_DOUBLE 3
+#define STATE_ADDR 4
+#define STATE_DOT 5
+#define STATE_SLASH 6
+#define STATE_MASK 7
+
+#ifdef HAVE_IPV6
+
+static enum match_type cmd_ipv6_match(const char *str)
+{
+ int state = STATE_START;
+ int colons = 0, nums = 0, double_colon = 0;
+ const char *sp = NULL;
+ struct sockaddr_in6 sin6_dummy;
+ int ret;
+
+ if (str == NULL)
+ return partly_match;
+
+ if (strspn(str, IPV6_ADDR_STR) != strlen(str))
+ return no_match;
+
+ /* use inet_pton that has a better support,
+ * for example inet_pton can support the automatic addresses:
+ * ::1.2.3.4
+ */
+ ret = inet_pton(AF_INET6, str, &sin6_dummy.sin6_addr);
+
+ if (ret == 1)
+ return exact_match;
+
+ while (*str != '\0') {
+ switch (state) {
+ case STATE_START:
+ if (*str == ':') {
+ if (*(str + 1) != ':' && *(str + 1) != '\0')
+ return no_match;
+ colons--;
+ state = STATE_COLON;
+ } else {
+ sp = str;
+ state = STATE_ADDR;
+ }
+
+ continue;
+ case STATE_COLON:
+ colons++;
+ if (*(str + 1) == ':')
+ state = STATE_DOUBLE;
+ else {
+ sp = str + 1;
+ state = STATE_ADDR;
+ }
+ break;
+ case STATE_DOUBLE:
+ if (double_colon)
+ return no_match;
+
+ if (*(str + 1) == ':')
+ return no_match;
+ else {
+ if (*(str + 1) != '\0')
+ colons++;
+ sp = str + 1;
+ state = STATE_ADDR;
+ }
+
+ double_colon++;
+ nums++;
+ break;
+ case STATE_ADDR:
+ if (*(str + 1) == ':' || *(str + 1) == '\0') {
+ if (str - sp > 3)
+ return no_match;
+
+ nums++;
+ state = STATE_COLON;
+ }
+ if (*(str + 1) == '.')
+ state = STATE_DOT;
+ break;
+ case STATE_DOT:
+ state = STATE_ADDR;
+ break;
+ default:
+ break;
+ }
+
+ if (nums > 8)
+ return no_match;
+
+ if (colons > 7)
+ return no_match;
+
+ str++;
+ }
+
+#if 0
+ if (nums < 11)
+ return partly_match;
+#endif /* 0 */
+
+ return exact_match;
+}
+
+static enum match_type cmd_ipv6_prefix_match(const char *str)
+{
+ int state = STATE_START;
+ int colons = 0, nums = 0, double_colon = 0;
+ int mask;
+ const char *sp = NULL;
+ char *endptr = NULL;
+
+ if (str == NULL)
+ return partly_match;
+
+ if (strspn(str, IPV6_PREFIX_STR) != strlen(str))
+ return no_match;
+
+ while (*str != '\0' && state != STATE_MASK) {
+ switch (state) {
+ case STATE_START:
+ if (*str == ':') {
+ if (*(str + 1) != ':' && *(str + 1) != '\0')
+ return no_match;
+ colons--;
+ state = STATE_COLON;
+ } else {
+ sp = str;
+ state = STATE_ADDR;
+ }
+
+ continue;
+ case STATE_COLON:
+ colons++;
+ if (*(str + 1) == '/')
+ return no_match;
+ else if (*(str + 1) == ':')
+ state = STATE_DOUBLE;
+ else {
+ sp = str + 1;
+ state = STATE_ADDR;
+ }
+ break;
+ case STATE_DOUBLE:
+ if (double_colon)
+ return no_match;
+
+ if (*(str + 1) == ':')
+ return no_match;
+ else {
+ if (*(str + 1) != '\0' && *(str + 1) != '/')
+ colons++;
+ sp = str + 1;
+
+ if (*(str + 1) == '/')
+ state = STATE_SLASH;
+ else
+ state = STATE_ADDR;
+ }
+
+ double_colon++;
+ nums += 1;
+ break;
+ case STATE_ADDR:
+ if (*(str + 1) == ':' || *(str + 1) == '.'
+ || *(str + 1) == '\0' || *(str + 1) == '/') {
+ if (str - sp > 3)
+ return no_match;
+
+ for (; sp <= str; sp++)
+ if (*sp == '/')
+ return no_match;
+
+ nums++;
+
+ if (*(str + 1) == ':')
+ state = STATE_COLON;
+ else if (*(str + 1) == '.')
+ state = STATE_DOT;
+ else if (*(str + 1) == '/')
+ state = STATE_SLASH;
+ }
+ break;
+ case STATE_DOT:
+ state = STATE_ADDR;
+ break;
+ case STATE_SLASH:
+ if (*(str + 1) == '\0')
+ return partly_match;
+
+ state = STATE_MASK;
+ break;
+ default:
+ break;
+ }
+
+ if (nums > 11)
+ return no_match;
+
+ if (colons > 7)
+ return no_match;
+
+ str++;
+ }
+
+ if (state < STATE_MASK)
+ return partly_match;
+
+ mask = strtol(str, &endptr, 10);
+ if (*endptr != '\0')
+ return no_match;
+
+ if (mask < 0 || mask > 128)
+ return no_match;
+
+/* I don't know why mask < 13 makes command match partly.
+ Forgive me to make this comments. I Want to set static default route
+ because of lack of function to originate default in ospf6d; sorry
+ yasu
+ if (mask < 13)
+ return partly_match;
+*/
+
+ return exact_match;
+}
+
+#endif /* HAVE_IPV6 */
+
+#define DECIMAL_STRLEN_MAX 10
+
+static int cmd_range_match(const char *range, const char *str)
+{
+ char *p;
+ char buf[DECIMAL_STRLEN_MAX + 1];
+ char *endptr = NULL;
+ unsigned long min, max, val;
+
+ if (str == NULL)
+ return 1;
+
+ val = strtoul(str, &endptr, 10);
+ if (*endptr != '\0')
+ return 0;
+
+ range++;
+ p = strchr(range, '-');
+ if (p == NULL)
+ return 0;
+ if (p - range > DECIMAL_STRLEN_MAX)
+ return 0;
+ strncpy(buf, range, p - range);
+ buf[p - range] = '\0';
+ min = strtoul(buf, &endptr, 10);
+ if (*endptr != '\0')
+ return 0;
+
+ range = p + 1;
+ p = strchr(range, '>');
+ if (p == NULL)
+ return 0;
+ if (p - range > DECIMAL_STRLEN_MAX)
+ return 0;
+ strncpy(buf, range, p - range);
+ buf[p - range] = '\0';
+ max = strtoul(buf, &endptr, 10);
+ if (*endptr != '\0')
+ return 0;
+
+ if (val < min || val > max)
+ return 0;
+
+ return 1;
+}
+
+/* Make completion match and return match type flag. */
+static enum match_type
+cmd_filter_by_completion(char *command, vector v, unsigned int index)
+{
+ unsigned int i;
+ const char *str;
+ struct cmd_element *cmd_element;
+ enum match_type match_type;
+ vector descvec;
+ struct desc *desc;
+
+ match_type = no_match;
+
+ /* If command and cmd_element string does not match set NULL to vector */
+ for (i = 0; i < vector_active(v); i++)
+ if ((cmd_element = vector_slot(v, i)) != NULL) {
+ if (index >= vector_active(cmd_element->strvec))
+ vector_slot(v, i) = NULL;
+ else {
+ unsigned int j;
+ int matched = 0;
+
+ descvec =
+ vector_slot(cmd_element->strvec, index);
+
+ for (j = 0; j < vector_active(descvec); j++)
+ if ((desc = vector_slot(descvec, j))) {
+ str = desc->cmd;
+
+ if (CMD_VARARG(str)) {
+ if (match_type <
+ vararg_match)
+ match_type =
+ vararg_match;
+ matched++;
+ } else if (CMD_RANGE(str)) {
+ if (cmd_range_match
+ (str, command)) {
+ if (match_type <
+ range_match)
+ match_type
+ =
+ range_match;
+
+ matched++;
+ }
+ }
+#ifdef HAVE_IPV6
+ else if (CMD_IPV6(str)) {
+ if (cmd_ipv6_match
+ (command)) {
+ if (match_type <
+ ipv6_match)
+ match_type
+ =
+ ipv6_match;
+
+ matched++;
+ }
+ } else if (CMD_IPV6_PREFIX(str)) {
+ if (cmd_ipv6_prefix_match(command)) {
+ if (match_type <
+ ipv6_prefix_match)
+ match_type
+ =
+ ipv6_prefix_match;
+
+ matched++;
+ }
+ }
+#endif /* HAVE_IPV6 */
+ else if (CMD_IPV4(str)) {
+ if (cmd_ipv4_match
+ (command)) {
+ if (match_type <
+ ipv4_match)
+ match_type
+ =
+ ipv4_match;
+
+ matched++;
+ }
+ } else if (CMD_IPV4_PREFIX(str)) {
+ if (cmd_ipv4_prefix_match(command)) {
+ if (match_type <
+ ipv4_prefix_match)
+ match_type
+ =
+ ipv4_prefix_match;
+ matched++;
+ }
+ } else
+ /* Check is this point's argument optional ? */
+ if (CMD_OPTION(str)
+ ||
+ CMD_VARIABLE(str)) {
+ if (match_type <
+ extend_match)
+ match_type =
+ extend_match;
+ matched++;
+ } else
+ if (strncmp
+ (command, str,
+ strlen(command)) ==
+ 0) {
+ if (strcmp(command, str)
+ == 0)
+ match_type =
+ exact_match;
+ else {
+ if (match_type <
+ partly_match)
+ match_type
+ =
+ partly_match;
+ }
+ matched++;
+ }
+ }
+ if (!matched)
+ vector_slot(v, i) = NULL;
+ }
+ }
+ return match_type;
+}
+
+/* Filter vector by command character with index. */
+static enum match_type
+cmd_filter_by_string(char *command, vector v, unsigned int index)
+{
+ unsigned int i;
+ const char *str;
+ struct cmd_element *cmd_element;
+ enum match_type match_type;
+ vector descvec;
+ struct desc *desc;
+
+ match_type = no_match;
+
+ /* If command and cmd_element string does not match set NULL to vector */
+ for (i = 0; i < vector_active(v); i++)
+ if ((cmd_element = vector_slot(v, i)) != NULL) {
+ /* If given index is bigger than max string vector of command,
+ set NULL */
+ if (index >= vector_active(cmd_element->strvec))
+ vector_slot(v, i) = NULL;
+ else {
+ unsigned int j;
+ int matched = 0;
+
+ descvec =
+ vector_slot(cmd_element->strvec, index);
+
+ for (j = 0; j < vector_active(descvec); j++)
+ if ((desc = vector_slot(descvec, j))) {
+ str = desc->cmd;
+
+ if (CMD_VARARG(str)) {
+ if (match_type <
+ vararg_match)
+ match_type =
+ vararg_match;
+ matched++;
+ } else if (CMD_RANGE(str)) {
+ if (cmd_range_match
+ (str, command)) {
+ if (match_type <
+ range_match)
+ match_type
+ =
+ range_match;
+ matched++;
+ }
+ }
+#ifdef HAVE_IPV6
+ else if (CMD_IPV6(str)) {
+ if (cmd_ipv6_match
+ (command) ==
+ exact_match) {
+ if (match_type <
+ ipv6_match)
+ match_type
+ =
+ ipv6_match;
+ matched++;
+ }
+ } else if (CMD_IPV6_PREFIX(str)) {
+ if (cmd_ipv6_prefix_match(command) == exact_match) {
+ if (match_type <
+ ipv6_prefix_match)
+ match_type
+ =
+ ipv6_prefix_match;
+ matched++;
+ }
+ }
+#endif /* HAVE_IPV6 */
+ else if (CMD_IPV4(str)) {
+ if (cmd_ipv4_match
+ (command) ==
+ exact_match) {
+ if (match_type <
+ ipv4_match)
+ match_type
+ =
+ ipv4_match;
+ matched++;
+ }
+ } else if (CMD_IPV4_PREFIX(str)) {
+ if (cmd_ipv4_prefix_match(command) == exact_match) {
+ if (match_type <
+ ipv4_prefix_match)
+ match_type
+ =
+ ipv4_prefix_match;
+ matched++;
+ }
+ } else if (CMD_OPTION(str)
+ || CMD_VARIABLE(str))
+ {
+ if (match_type <
+ extend_match)
+ match_type =
+ extend_match;
+ matched++;
+ } else {
+ if (strcmp(command, str)
+ == 0) {
+ match_type =
+ exact_match;
+ matched++;
+ }
+ }
+ }
+ if (!matched)
+ vector_slot(v, i) = NULL;
+ }
+ }
+ return match_type;
+}
+
+/* Check ambiguous match */
+static int
+is_cmd_ambiguous(char *command, vector v, int index, enum match_type type)
+{
+ unsigned int i;
+ unsigned int j;
+ const char *str = NULL;
+ struct cmd_element *cmd_element;
+ const char *matched = NULL;
+ vector descvec;
+ struct desc *desc;
+
+ for (i = 0; i < vector_active(v); i++)
+ if ((cmd_element = vector_slot(v, i)) != NULL) {
+ int match = 0;
+
+ descvec = vector_slot(cmd_element->strvec, index);
+
+ for (j = 0; j < vector_active(descvec); j++)
+ if ((desc = vector_slot(descvec, j))) {
+ enum match_type ret;
+
+ str = desc->cmd;
+
+ switch (type) {
+ case exact_match:
+ if (!
+ (CMD_OPTION(str)
+ || CMD_VARIABLE(str))
+&& strcmp(command, str) == 0)
+ match++;
+ break;
+ case partly_match:
+ if (!
+ (CMD_OPTION(str)
+ || CMD_VARIABLE(str))
+&& strncmp(command, str, strlen(command)) == 0) {
+ if (matched
+ && strcmp(matched,
+ str) != 0)
+ return 1; /* There is ambiguous match. */
+ else
+ matched = str;
+ match++;
+ }
+ break;
+ case range_match:
+ if (cmd_range_match
+ (str, command)) {
+ if (matched
+ && strcmp(matched,
+ str) != 0)
+ return 1;
+ else
+ matched = str;
+ match++;
+ }
+ break;
+#ifdef HAVE_IPV6
+ case ipv6_match:
+ if (CMD_IPV6(str))
+ match++;
+ break;
+ case ipv6_prefix_match:
+ if ((ret =
+ cmd_ipv6_prefix_match
+ (command)) != no_match) {
+ if (ret == partly_match)
+ return 2; /* There is incomplete match. */
+
+ match++;
+ }
+ break;
+#endif /* HAVE_IPV6 */
+ case ipv4_match:
+ if (CMD_IPV4(str))
+ match++;
+ break;
+ case ipv4_prefix_match:
+ if ((ret =
+ cmd_ipv4_prefix_match
+ (command)) != no_match) {
+ if (ret == partly_match)
+ return 2; /* There is incomplete match. */
+
+ match++;
+ }
+ break;
+ case extend_match:
+ if (CMD_OPTION(str)
+ || CMD_VARIABLE(str))
+ match++;
+ break;
+ case no_match:
+ default:
+ break;
+ }
+ }
+ if (!match)
+ vector_slot(v, i) = NULL;
+ }
+ return 0;
+}
+
+/* If src matches dst return dst string, otherwise return NULL */
+static const char *cmd_entry_function(const char *src, const char *dst)
+{
+ /* Skip variable arguments. */
+ if (CMD_OPTION(dst) || CMD_VARIABLE(dst) || CMD_VARARG(dst) ||
+ CMD_IPV4(dst) || CMD_IPV4_PREFIX(dst) || CMD_RANGE(dst))
+ return NULL;
+
+ /* In case of 'command \t', given src is NULL string. */
+ if (src == NULL)
+ return dst;
+
+ /* Matched with input string. */
+ if (strncmp(src, dst, strlen(src)) == 0)
+ return dst;
+
+ return NULL;
+}
+
+/* If src matches dst return dst string, otherwise return NULL */
+/* This version will return the dst string always if it is
+ CMD_VARIABLE for '?' key processing */
+static const char *cmd_entry_function_desc(const char *src, const char *dst)
+{
+ if (CMD_VARARG(dst))
+ return dst;
+
+ if (CMD_RANGE(dst)) {
+ if (cmd_range_match(dst, src))
+ return dst;
+ else
+ return NULL;
+ }
+#ifdef HAVE_IPV6
+ if (CMD_IPV6(dst)) {
+ if (cmd_ipv6_match(src))
+ return dst;
+ else
+ return NULL;
+ }
+
+ if (CMD_IPV6_PREFIX(dst)) {
+ if (cmd_ipv6_prefix_match(src))
+ return dst;
+ else
+ return NULL;
+ }
+#endif /* HAVE_IPV6 */
+
+ if (CMD_IPV4(dst)) {
+ if (cmd_ipv4_match(src))
+ return dst;
+ else
+ return NULL;
+ }
+
+ if (CMD_IPV4_PREFIX(dst)) {
+ if (cmd_ipv4_prefix_match(src))
+ return dst;
+ else
+ return NULL;
+ }
+
+ /* Optional or variable commands always match on '?' */
+ if (CMD_OPTION(dst) || CMD_VARIABLE(dst))
+ return dst;
+
+ /* In case of 'command \t', given src is NULL string. */
+ if (src == NULL)
+ return dst;
+
+ if (strncmp(src, dst, strlen(src)) == 0)
+ return dst;
+ else
+ return NULL;
+}
+
+/* Check same string element existence. If it isn't there return
+ 1. */
+static int cmd_unique_string(vector v, const char *str)
+{
+ unsigned int i;