Use the app_info->name instead of the hostname
[osmocom-bb.git] / src / vty / command.c
index 7275a3d..7525df6 100644 (file)
@@ -84,9 +84,9 @@ 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);
+       printf("%s version %s\n", host.app_info->name, host.app_info->version);
        if (print_copyright)
-               printf("\n%s\n", host.prog_copyright);
+               printf("\n%s\n", host.app_info->copyright);
 }
 
 /* Utility function to concatenate argv argument into a single string
@@ -139,6 +139,18 @@ static int cmp_desc(const void *p, const void *q)
        return strcmp(a->cmd, b->cmd);
 }
 
+static int is_config(struct vty *vty)
+{
+       if (vty->node <= CONFIG_NODE)
+               return 0;
+       else if (vty->node > CONFIG_NODE && vty->node < _LAST_OSMOVTY_NODE)
+               return 1;
+       else if (host.app_info->is_config_node)
+               return host.app_info->is_config_node(vty, vty->node);
+       else
+               return vty->node > CONFIG_NODE;
+}
+
 /* Sort each node's command element according to command string. */
 void sort_node()
 {
@@ -867,41 +879,77 @@ 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;
+       if (range[1] == '-') {
+               signed long min = 0, max = 0, val;
+
+               val = strtol(str, &endptr, 10);
+               if (*endptr != '\0')
+                       return 0;
+
+               range += 2;
+               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 = -strtol(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 = strtol(buf, &endptr, 10);
+               if (*endptr != '\0')
+                       return 0;
+
+               if (val < min || val > max)
+                       return 0;
+       } else {
+               unsigned long min, max, val;
+
+               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;
 }
@@ -1751,8 +1799,8 @@ enum node_type vty_go_parent(struct vty *vty)
 {
        assert(vty->node > CONFIG_NODE);
 
-       if (vty_go_parent_cb)
-               vty_go_parent_cb(vty);
+       if (host.app_info->go_parent_cb)
+               host.app_info->go_parent_cb(vty);
        else
                vty->node = CONFIG_NODE;
 
@@ -1911,9 +1959,9 @@ cmd_execute_command(vector vline, struct vty *vty, struct cmd_element **cmd,
        if (vtysh)
                return saved_ret;
 
-       /* This assumes all nodes above CONFIG_NODE are childs of CONFIG_NODE */
+       /* Go to parent for config nodes to attempt to find the right command */
        while (ret != CMD_SUCCESS && ret != CMD_WARNING
-              && vty->node > CONFIG_NODE) {
+              && is_config(vty)) {
                vty_go_parent(vty);
                ret = cmd_execute_command_real(vline, vty, cmd);
                tried = 1;
@@ -2061,7 +2109,7 @@ int config_from_file(struct vty *vty, FILE * fp)
                /* Try again with setting node to CONFIG_NODE */
                while (ret != CMD_SUCCESS && ret != CMD_WARNING
                       && ret != CMD_ERR_NOTHING_TODO
-                      && vty->node != CONFIG_NODE) {
+                      && vty->node != CONFIG_NODE && is_config(vty)) {
                        vty_go_parent(vty);
                        ret = cmd_execute_command_strict(vline, vty, NULL);
                }
@@ -2162,9 +2210,10 @@ gDEFUN(config_exit,
 DEFUN(show_version,
       show_version_cmd, "show version", SHOW_STR "Displays program version\n")
 {
-       vty_out(vty, "%s %s (%s).%s", host.prog_name, host.prog_version,
-               host.name ? host.name : "", VTY_NEWLINE);
-       vty_out(vty, "%s%s", host.prog_copyright, VTY_NEWLINE);
+       vty_out(vty, "%s %s (%s).%s", host.app_info->name,
+               host.app_info->version,
+               host.app_info->name ? host.app_info->name : "", VTY_NEWLINE);
+       vty_out(vty, "%s%s", host.app_info->copyright, VTY_NEWLINE);
 
        return CMD_SUCCESS;
 }
@@ -2258,7 +2307,7 @@ DEFUN(config_write_file,
 
        /* Config file header print. */
        vty_out(file_vty, "!\n! %s (%s) configuration saved from vty\n!",
-               host.prog_name, host.prog_version);
+               host.app_info->name, host.app_info->version);
        //vty_time_print (file_vty, 1);
        vty_out(file_vty, "!\n");