#define MAXCOM 32
-int cont = 1;
+int main_loop_continue = 1;
static const struct TreeNode* getSubCom (char **com, int n, int *t)
const struct TreeNode *cur, *next;
- cur = &coms;
+ cur = &commands;
for (i = 0; i < n; i++) {
/* we have reached a terminal command, exit */
if (cur->sub == NULL)
break;
case 'h':
- printf("Usage: %s [-b] [-f] [-g] [-i <interface>]\n", argv[0]);
+ printf("usage: %s [-b] [-f] [-g] [-i <interface>]\n", argv[0]);
goto end;
case 't':
sigsetjmp(jmpbuf, 1);
- while (cont) {
+ while (main_loop_continue) {
line = readline("> ");
if (line == NULL)
goto end;
cur = getSubCom(com, n, &i);
- if (i < n) { /* commands left unchecked */
- if (i == 0) { /* root command */
- printf("unknown command\n");
- } else if (cur->sub != NULL) { /* intermediate command */
- printf("unknown %s subcommand\n", com[i - 1]);
- } else if (!cur->hasArgs) { /* terminal command without arguments */
- printf("%s as no subcommand and takes no parameter\n", com[i - 1]);
- } else if (cur->comfunc == NULL) { /* erroneous terminal command without function */
- printf("terminal command without function\n");
- } else { /* terminal command with arguments, left "commands" are in fact parameters */
- cur->comfunc(n - i, (const char**)&com[i], nga);
- }
- } else { /* no command left */
- if (cur->sub != NULL) { /* intermediate command */
+ if (cur->sub != NULL) {
+ /* not terminal command */
+ if (i == 0) {
+ /* root command */
+ printf("unknown command: %s\n", com[i]);
+ } else if (i < n) {
+ /* intermediate command, remaining string */
+ printf("unknown %s subcommand: %s\n", com[i - 1], com[i]);
+ } else {
+ /* intermediate command, no remaining string */
/* print available subcommands */
for (next = cur->sub; next->name != NULL; next++)
printf("%s ", next->name);
- printf("\n");
- } else if (cur->comfunc == NULL) { /* erroneous terminal command without function */
- printf("terminal command without function\n");
- } else { /* terminal command without arguments */
- cur->comfunc(0, NULL, nga);
+ putchar('\n');
}
+ } else if (cur->comfunc == NULL) {
+ /* erroneous terminal command without function */
+ printf("terminal command without function\n");
+ } else {
+ /* execute terminal command */
+ cur->comfunc(n - i, (const char**)&com[i], nga);
}
for (i = 0; com[i] != NULL; i++) {
/* helper function to analyse bitrate speed specifications */
-static int bitrate_analyse (int nb, const char **com, int *ports)
+static int bitrate_analyse (int argc, const char **argv, int *ports)
{
int i = 0, s;
- while (i < nb - 1) {
- s = parseBitrate(com[i + 1]);
- if (strcmp(com[i], "inout") == 0) {
+ while (i < argc - 1) {
+ s = parseBitrate(argv[i + 1]);
+ if (strcmp(argv[i], "inout") == 0) {
ports[0] = s;
ports[1] = s;
- } else if (strcmp(com[i], "in") == 0) {
+ } else if (strcmp(argv[i], "in") == 0) {
ports[0] = s;
- } else if (strcmp(com[i], "out") == 0) {
+ } else if (strcmp(argv[i], "out") == 0) {
ports[1] = s;
} else {
break;
}
-bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga)
+bool do_bitrate_set (int argc, const char **argv, struct ngadmin *nga)
{
int i, k = 0, defs[] = {12, 12}, p, *ports = NULL;
const struct swi_attr *sa;
bool ret = true;
- if (nb < 2) {
+ if (argc < 2) {
printf(
- "Usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n"
+ "usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n"
"SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\n"
);
ret = false;
ports = malloc(2 * sa->ports * sizeof(int));
/* get defaults if present */
- if (strcmp(com[k], "all") == 0) {
+ if (strcmp(argv[k], "all") == 0) {
k++;
- k += bitrate_analyse(nb-k, &com[k], defs);
+ k += bitrate_analyse(argc - k, &argv[k], defs);
}
/* apply defaults */
memcpy(&ports[2 * i], defs, sizeof(defs));
/* get ports specifics */
- while (k < nb) {
- p = strtol(com[k++], NULL, 0) - 1;
+ while (k < argc) {
+ p = strtol(argv[k++], NULL, 0) - 1;
if (p >= 0 && p <sa->ports)
- k += bitrate_analyse(nb - k, &com[k], &ports[2 * p]);
+ k += bitrate_analyse(argc - k, &argv[k], &ports[2 * p]);
}
/* send it to the switch */
}
-bool do_bitrate_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_bitrate_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i, ret = true, *ports = NULL;
const struct swi_attr *sa;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
#include "commands.h"
-bool do_cabletest (int nb, const char **com, struct ngadmin *nga)
+bool do_cabletest (int argc, const char **argv, struct ngadmin *nga)
{
bool ret = true;
const struct swi_attr *sa;
int i, j=0, k=0;
- if (nb < 1) {
- printf("Usage: cabletest <port1> [<port2> ...]\n");
+ if (argc < 1) {
+ printf("usage: cabletest <port1> [<port2> ...]\n");
goto end;
}
-
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
ct = malloc(sa->ports * sizeof(struct cabletest));
memset(ct, 0, sa->ports * sizeof(struct cabletest));
- while (k < nb) {
- ct[j].port = strtol(com[k++], NULL, 0);
+ while (k < argc) {
+ ct[j].port = strtol(argv[k++], NULL, 0);
if (ct[j].port >= 1 && ct[j].port <= sa->ports)
j++;
}
#include "commands.h"
-bool do_defaults (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_defaults (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i, ret = true;
const struct swi_attr *sa;
char line[16];
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
#include "commands.h"
-bool do_firmware_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_firmware_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
const struct swi_attr *sa;
bool ret = true;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
}
-bool do_firmware_upgrade (int nb, const char **com, struct ngadmin *nga)
+bool do_firmware_upgrade (int argc, const char **argv, struct ngadmin *nga)
{
const struct swi_attr *sa;
bool ret = true;
int i;
- if (nb != 1) {
- printf("Usage: firmware upgrade <file>\n");
+ if (argc != 1) {
+ printf("usage: firmware upgrade <file>\n");
ret = false;
goto end;
}
goto end;
}
- i = ngadmin_upgradeFirmware(nga, com[0]);
+ i = ngadmin_upgradeFirmware(nga, argv[0]);
printErrCode(i);
end:
#include "commands.h"
-bool do_help (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED)
+bool do_help (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED)
{
const struct TreeNode *s;
+
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
printf("Available commands: \n");
- for (s = coms.sub; s->name != NULL; s++)
+ for (s = commands.sub; s->name != NULL; s++)
printf("%s ", s->name);
putchar('\n');
#include "commands.h"
-bool do_igmp_set (int nb, const char **com, struct ngadmin *nga)
+bool do_igmp_set (int argc, const char **argv, struct ngadmin *nga)
{
int i;
struct igmp_conf ic;
- if (nb != 4) {
- printf("Usage: igmp set <enable> <vlan> <validate> <block>\n");
+ if (argc != 4) {
+ printf("usage: igmp set <enable> <vlan> <validate> <block>\n");
return false;
}
return false;
}
- ic.enable = strtol(com[0], NULL, 0);
- ic.vlan = strtol(com[1], NULL, 0);
- ic.validate = strtol(com[2], NULL, 0);
- ic.block = strtol(com[3], NULL, 0);
+ ic.enable = strtol(argv[0], NULL, 0);
+ ic.vlan = strtol(argv[1], NULL, 0);
+ ic.validate = strtol(argv[2], NULL, 0);
+ ic.block = strtol(argv[3], NULL, 0);
i = ngadmin_setIGMPConf(nga, &ic);
printErrCode(i);
}
-bool do_igmp_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_igmp_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
bool ret = true;
- sa=ngadmin_getCurrentSwitch(nga);
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
+ sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
ret = false;
#include "commands.h"
-bool do_list (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_list (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int n;
const struct swi_attr *sa;
+
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
sa = ngadmin_getSwitchTab(nga, &n);
displaySwitchTab(sa, n);
#include "commands.h"
-bool do_login (int nb, const char **com, struct ngadmin *nga)
+bool do_login (int argc, const char **argv, struct ngadmin *nga)
{
int i;
- if (nb != 1) {
- printf("Usage: login <num>\n");
+ if (argc != 1) {
+ printf("usage: login <num>\n");
return false;
}
- i = strtol(com[0], NULL, 0);
+ i = strtol(argv[0], NULL, 0);
i = ngadmin_login(nga, i);
printErrCode(i);
#include "commands.h"
-bool do_mirror_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_mirror_disable (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
if (ngadmin_getCurrentSwitch(nga) == NULL) {
printf("must be logged\n");
return false;
}
-
i = ngadmin_setMirror(nga, NULL);
printErrCode(i);
}
-bool do_mirror_set (int nb, const char **com, struct ngadmin *nga)
+bool do_mirror_set (int argc, const char **argv, struct ngadmin *nga)
{
const struct swi_attr *sa;
char *ports = NULL;
int i, k = 0;
- if (nb < 3) {
- printf("Usage: mirror set <destination port> clone <port1> [<port2> ...]\n");
+ if (argc < 3) {
+ printf("usage: mirror set <destination port> clone <port1> [<port2> ...]\n");
goto end;
}
ports = malloc((sa->ports + 1) * sizeof(char));
memset(ports, 0, sa->ports + 1);
- ports[0] = strtol(com[k++], NULL, 0);
- if (ports[0] < 1 || ports[0] > sa->ports || strcasecmp(com[k++], "clone") != 0) {
+ ports[0] = strtol(argv[k++], NULL, 0);
+ if (ports[0] < 1 || ports[0] > sa->ports || strcasecmp(argv[k++], "clone") != 0) {
printf("syntax error\n");
ret = false;
goto end;
}
- while (k < nb) {
- i = strtol(com[k++], NULL, 0);
+ while (k < argc) {
+ i = strtol(argv[k++], NULL, 0);
if (i < 1 || i > sa->ports) {
printf("port out of range\n");
ret = false;
}
-bool do_mirror_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_mirror_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
const struct swi_attr *sa;
char *ports = NULL;
int i;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
return false;
}
-
ports = malloc((sa->ports + 1) * sizeof(char));
i = ngadmin_getMirror(nga, ports);
if (i != ERR_OK) {
#include "commands.h"
-bool do_name_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_name_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
const struct swi_attr *sa;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
}
-bool do_name_set (int nb, const char **com, struct ngadmin *nga)
+bool do_name_set (int argc, const char **argv, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
- if (nb != 1) {
- printf("Usage: name set <value>\n");
+ if (argc != 1) {
+ printf("usage: name set <value>\n");
return false;
}
return false;
}
- i = ngadmin_setName(nga, com[0]);
+ i = ngadmin_setName(nga, argv[0]);
printErrCode(i);
}
-bool do_name_clear (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_name_clear (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
#include "commands.h"
-bool do_netconf_set (int nb, const char **com, struct ngadmin *nga)
+bool do_netconf_set (int argc, const char **argv, struct ngadmin *nga)
{
int i, k;
const struct swi_attr *sa;
bool ret = true;
- if (nb == 0) {
- printf("Usage: netconf set [dhcp yes|no] [ip <ip>] [mask <mask>] [gw <gw>]\n");
+ if (argc == 0) {
+ printf("usage: netconf set [dhcp yes|no] [ip <ip>] [mask <mask>] [gw <gw>]\n");
return false;
}
memset(&nc, 0, sizeof(struct net_conf));
- for (k = 0; k < nb; k += 2) {
- if (strcasecmp(com[k], "dhcp") == 0) {
- if (strcasecmp(com[k+1], "yes") == 0) {
+ for (k = 0; k < argc; k += 2) {
+ if (strcasecmp(argv[k], "dhcp") == 0) {
+ if (strcasecmp(argv[k + 1], "yes") == 0) {
nc.dhcp = true;
- } else if (strcasecmp(com[k+1], "no") == 0) {
+ } else if (strcasecmp(argv[k + 1], "no") == 0) {
nc.dhcp = false;
} else {
printf("Incorrect DHCP value\n");
ret = false;
goto end;
}
- } else if (strcasecmp(com[k], "ip") == 0) {
- if (inet_aton(com[k+1], &nc.ip) == 0) {
+ } else if (strcasecmp(argv[k], "ip") == 0) {
+ if (inet_aton(argv[k + 1], &nc.ip) == 0) {
printf("Incorrect IP value\n");
ret = false;
goto end;
}
- } else if (strcasecmp(com[k], "mask") == 0) {
+ } else if (strcasecmp(argv[k], "mask") == 0) {
/* TODO: check if it is a correct mask */
- if (inet_aton(com[k+1], &nc.netmask) == 0) {
+ if (inet_aton(argv[k + 1], &nc.netmask) == 0) {
printf("Incorrect mask value\n");
ret = false;
goto end;
}
- } else if (strcasecmp(com[k], "gw") == 0) {
- if (inet_aton(com[k+1], &nc.gw) == 0) {
+ } else if (strcasecmp(argv[k], "gw") == 0) {
+ if (inet_aton(argv[k + 1], &nc.gw) == 0) {
printf("Incorrect gateway value\n");
ret = false;
goto end;
#include "commands.h"
-bool do_password_change (int nb, const char **com, struct ngadmin *nga)
+bool do_password_change (int argc, const char **argv, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
- if (nb != 1) {
- printf("Usage: password change <value>\n");
+ if (argc != 1) {
+ printf("usage: password change <value>\n");
return false;
}
return false;
}
- i = ngadmin_changePassword(nga, com[0]);
+ i = ngadmin_changePassword(nga, argv[0]);
printErrCode(i);
}
-bool do_password_set (int nb, const char **com, struct ngadmin *nga)
+bool do_password_set (int argc, const char **argv, struct ngadmin *nga)
{
int i;
char buf[64];
const char *pass;
- if (nb > 1) {
- printf("Usage: password set [<value>]\n");
+ if (argc > 1) {
+ printf("usage: password set [<value>]\n");
return false;
}
- if (nb == 0) {
+ if (argc == 0) {
printf("Enter password: ");
fflush(stdout);
current_term.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term);
putchar('\n');
} else {
- pass = com[0];
+ pass = argv[0];
}
if (pass != NULL) {
#include "commands.h"
-bool do_ports_state (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_ports_state (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
bool ret = true;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
}
-bool do_ports_statistics_reset (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_ports_statistics_reset (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
+
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
if (ngadmin_getCurrentSwitch(nga) == NULL) {
printf("must be logged\n");
return false;
}
-bool do_ports_statistics_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_ports_statistics_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
struct port_stats *ps = NULL;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
#include "commands.h"
-bool do_qos_mode (int nb, const char **com, struct ngadmin *nga)
+bool do_qos_mode (int argc, const char **argv, struct ngadmin *nga)
{
int i, s, ret = true;
const struct swi_attr *sa;
- if (nb == 0) {
- printf("Usage: qos mode port|802.1p\n");
+ if (argc == 0) {
+ printf("usage: qos mode port|802.1p\n");
goto end;
}
goto end;
}
- if (strcasecmp(com[0], "port") == 0) {
+ if (strcasecmp(argv[0], "port") == 0) {
s = QOS_PORT;
- } else if (strcasecmp(com[0], "802.1p") == 0) {
+ } else if (strcasecmp(argv[0], "802.1p") == 0) {
s = QOS_DOT;
} else {
printf("Unknown QOS mode\n");
}
-bool do_qos_set (int nb, const char **com, struct ngadmin *nga)
+bool do_qos_set (int argc, const char **argv, struct ngadmin *nga)
{
int i, p;
const struct swi_attr *sa;
char d = PRIO_UNSPEC, *ports = NULL;
- if (nb < 2) {
- printf("Usage: qos set (all <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\n");
+ if (argc < 2) {
+ printf("usage: qos set (all <prio0>)|(<port1> <prio1> [<port2> <prio2> ...])\n");
ret = false;
goto end;
}
ports = malloc(sa->ports * sizeof(char));
- if (strcmp(com[0], "all") == 0) {
- d = parsePrio(com[1]);
- com += 2;
- nb -= 2;
+ /* read defaults */
+ if (strcmp(argv[0], "all") == 0) {
+ d = parsePrio(argv[1]);
+ argv += 2;
+ argc -= 2;
}
+ /* apply defaults */
for (i = 0; i < sa->ports; i++)
ports[i] = d;
- for (i = 0; i < nb; i += 2) {
- p = strtol(com[i], NULL, 0);
+ /* read and apply port specifics */
+ for (i = 0; i < argc; i += 2) {
+ p = strtol(argv[i], NULL, 0);
if (p < 1 || p > sa->ports)
continue;
- ports[p - 1] = parsePrio(com[i + 1]);
+ ports[p - 1] = parsePrio(argv[i + 1]);
}
+ /* send the new configuration to the switch */
i = ngadmin_setQOSValues(nga, ports);
printErrCode(i);
}
-bool do_qos_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_qos_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i, s = 0, ret = true;
const struct swi_attr *sa;
char *ports = NULL;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
#include "commands.h"
-bool do_quit (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED)
+bool do_quit (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED)
{
- cont = 0;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
+ main_loop_continue = 0;
return true;
}
#include "commands.h"
-bool do_restart (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED)
+bool do_restart (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED)
{
int i, ret = true;
const struct swi_attr *sa;
char line[16];
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
#include "commands.h"
-bool do_scan (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_scan (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
i = ngadmin_scan(nga);
if (i < 0) {
printErrCode(i);
return false;
}
- sa = ngadmin_getSwitchTab(nga, &nb);
- displaySwitchTab(sa, nb);
+ sa = ngadmin_getSwitchTab(nga, &i);
+ displaySwitchTab(sa, i);
return true;
#include "commands.h"
-bool do_stormfilter_enable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_stormfilter_enable (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
}
-bool do_stormfilter_disable (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_stormfilter_disable (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i;
const struct swi_attr *sa;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
}
-bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga)
+bool do_stormfilter_set (int argc, const char **argv, struct ngadmin *nga)
{
int i, d = BITRATE_UNSPEC, p, *ports = NULL;
const struct swi_attr *sa;
bool ret = true;
- if (nb < 2) {
- printf("Usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
+ if (argc < 2) {
+ printf("usage: stormfilt set (all <speed0>)|(<port1> <speed1> [<port2> <speed2> ...])\n");
ret = false;
goto end;
}
goto end;
}
- ports=malloc(sa->ports * sizeof(int));
+ ports = malloc(sa->ports * sizeof(int));
- if (strcmp(com[0], "all") == 0) {
- d = parseBitrate(com[1]);
- com += 2;
- nb -= 2;
+ /* read defaults */
+ if (strcmp(argv[0], "all") == 0) {
+ d = parseBitrate(argv[1]);
+ argv += 2;
+ argc -= 2;
}
+ /* apply defaults */
for (i = 0; i < sa->ports; i++)
ports[i] = d;
- for (i = 0; i < nb; i += 2) {
- p = strtol(com[i], NULL, 0);
+ /* read and apply port specifics */
+ for (i = 0; i < argc - 1; i += 2) {
+ p = strtol(argv[i], NULL, 0);
if (p < 1 || p > sa->ports)
continue;
- ports[p - 1] = parseBitrate(com[i + 1]);
+ ports[p - 1] = parseBitrate(argv[i + 1]);
}
+ /* send the new configuration to the switch */
i = ngadmin_setStormFilterValues(nga, ports);
printErrCode(i);
}
-bool do_stormfilter_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_stormfilter_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i, s, ret = true, *ports = NULL;
const struct swi_attr *sa;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
}
-bool do_tree (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga UNUSED)
+bool do_tree (int argc, const char **argv UNUSED, struct ngadmin *nga UNUSED)
{
- display_node(&coms, 0);
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ return false;
+ }
+
+ display_node(&commands, 0);
return true;
}
}
-bool do_vlan_8021q_del (int nb, const char **com, struct ngadmin *nga)
+bool do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga)
{
const struct swi_attr *sa;
unsigned short vlan;
int i;
- if (nb != 1) {
- printf("Usage: vlan 8021q del <vlan>\n");
+ if (argc != 1) {
+ printf("usage: vlan 8021q del <vlan>\n");
return false;
}
-
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
return false;
}
- vlan=strtoul(com[0], NULL, 0);
+ vlan=strtoul(argv[0], NULL, 0);
if (vlan < 1 || vlan > VLAN_MAX) {
printf("vlan out of range\n");
return false;
}
-bool do_vlan_8021q_set (int nb, const char **com, struct ngadmin *nga)
+bool do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga)
{
unsigned char *ports = NULL, p, def = VLAN_UNSPEC;
const struct swi_attr *sa;
int i, k = 0;
- if (nb == 0) {
- printf("Usage: vlan 802.1q set <vlan> [all unspec|no|untagged|tagged] [<port1> unspec|no|untagged|tagged ...]\n");
+ if (argc == 0) {
+ printf("usage: vlan 802.1q set <vlan> [all unspec|no|untagged|tagged] [<port1> unspec|no|untagged|tagged ...]\n");
ret = false;
goto end;
}
}
/* read vlan */
- vlan = strtoul(com[k++], NULL, 0);
+ vlan = strtoul(argv[k++], NULL, 0);
if (vlan < 1 || vlan > VLAN_MAX) {
printf("vlan out of range\n");
}
/* read defaults */
- if (k < nb - 1 && strcasecmp(com[k], "all") == 0) {
+ if (k < argc - 1 && strcasecmp(argv[k], "all") == 0) {
k++;
- if (strcasecmp(com[k], "tagged") == 0) {
+ if (strcasecmp(argv[k], "tagged") == 0) {
def = VLAN_TAGGED;
- } else if (strcasecmp(com[k], "untagged") == 0) {
+ } else if (strcasecmp(argv[k], "untagged") == 0) {
def = VLAN_UNTAGGED;
- } else if (strcasecmp(com[k], "no") == 0) {
+ } else if (strcasecmp(argv[k], "no") == 0) {
def = VLAN_NO;
- } else if (strcasecmp(com[k], "unspec") == 0) {
+ } else if (strcasecmp(argv[k], "unspec") == 0) {
def = VLAN_UNSPEC;
} else {
printf("incorrect type\n");
memset(ports, def, sa->ports);
/* apply port specifics */
- while (k < nb - 1) {
- p = strtoul(com[k++], NULL, 0) - 1;
+ while (k < argc - 1) {
+ p = strtoul(argv[k++], NULL, 0) - 1;
if (p >= sa->ports) {
printf("port out of range\n");
ret = false;
goto end;
}
- if (strcasecmp(com[k], "tagged") ==0) {
+ if (strcasecmp(argv[k], "tagged") ==0) {
ports[p] = VLAN_TAGGED;
- } else if (strcasecmp(com[k], "untagged") == 0) {
+ } else if (strcasecmp(argv[k], "untagged") == 0) {
ports[p] = VLAN_UNTAGGED;
- } else if (strcasecmp(com[k], "no") == 0) {
+ } else if (strcasecmp(argv[k], "no") == 0) {
ports[p] = VLAN_NO;
- } else if (strcasecmp(com[k], "unspec") == 0) {
+ } else if (strcasecmp(argv[k], "unspec") == 0) {
ports[p] = VLAN_UNSPEC;
} else {
printf("incorrect type\n");
}
-bool do_vlan_8021q_show (int nb, const char **com, struct ngadmin *nga)
+bool do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga)
{
unsigned short vl = 0, *vlans = NULL;
unsigned char *ports = NULL;
goto end;
}
- if (nb > 0)
- vl = strtoul(com[0], NULL, 0);
+ if (argc > 0)
+ vl = strtoul(argv[0], NULL, 0);
ports = malloc(sa->ports * n * sizeof(unsigned char));
}
-bool do_vlan_mode_set (int nb, const char **com, struct ngadmin *nga)
+bool do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga)
{
int mode, i;
- if (nb == 0) {
+ if (argc == 0) {
printf(
- "Usage: vlan mode set <mode>\n"
+ "usage: vlan mode set <mode>\n"
"1 - basic port based\n"
"2 - advanced port based\n"
"3 - basic 802.1Q\n"
return false;
}
- mode = strtoul(com[0], NULL, 0);
+ mode = strtoul(argv[0], NULL, 0);
if (mode < 1 || mode > 4) {
printf("mode out of range\n");
return false;
}
-bool do_vlan_mode_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
int i, t, ret = true;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
if (ngadmin_getCurrentSwitch(nga) == NULL) {
printf("must be logged\n");
ret = false;
}
-bool do_vlan_pvid_set (int nb, const char **com, struct ngadmin *nga)
+bool do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga)
{
const struct swi_attr *sa;
unsigned char port;
int i;
- if (nb != 2) {
- printf("Usage: vlan pvid set <port> <vlan>\n");
+ if (argc != 2) {
+ printf("usage: vlan pvid set <port> <vlan>\n");
return false;
}
return false;
}
- port = strtoul(com[0], NULL, 0);
- vlan = strtoul(com[1], NULL, 0);
+ port = strtoul(argv[0], NULL, 0);
+ vlan = strtoul(argv[1], NULL, 0);
if (port < 1 || port > sa->ports) {
printf("port out of range\n");
}
-bool do_vlan_pvid_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga)
+bool do_vlan_pvid_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
{
unsigned short *ports = NULL;
const struct swi_attr *sa;
bool ret = true;
+ if (argc > 0) {
+ printf("this command takes no argument\n");
+ ret = false;
+ goto end;
+ }
+
sa = ngadmin_getCurrentSwitch(nga);
if (sa == NULL) {
printf("must be logged\n");
/* bitrate */
-bool do_bitrate_set (int nb, const char **com, struct ngadmin *nga);
-bool do_bitrate_show (int nb, const char **com, struct ngadmin *nga);
+bool do_bitrate_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_bitrate_show (int argc, const char **argv, struct ngadmin *nga);
/* cabletest */
-bool do_cabletest (int nb, const char **com, struct ngadmin *nga);
+bool do_cabletest (int argc, const char **argv, struct ngadmin *nga);
/* defaults */
-bool do_defaults (int nb, const char **com, struct ngadmin *nga);
+bool do_defaults (int argc, const char **argv, struct ngadmin *nga);
/* firmware */
-bool do_firmware_show (int nb, const char **com, struct ngadmin *nga);
-bool do_firmware_upgrade (int nb, const char **com, struct ngadmin *nga);
+bool do_firmware_show (int argc, const char **argv, struct ngadmin *nga);
+bool do_firmware_upgrade (int argc, const char **argv, struct ngadmin *nga);
/* help */
-bool do_help (int nb, const char **com, struct ngadmin *nga);
+bool do_help (int argc, const char **argv, struct ngadmin *nga);
/* igmp */
-bool do_igmp_set (int nb, const char **com, struct ngadmin *nga);
-bool do_igmp_show (int nb, const char **com, struct ngadmin *nga);
+bool do_igmp_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_igmp_show (int argc, const char **argv, struct ngadmin *nga);
/* list */
-bool do_list (int nb, const char **com, struct ngadmin *nga);
+bool do_list (int argc, const char **argv, struct ngadmin *nga);
/* login */
-bool do_login (int nb, const char **com, struct ngadmin *nga);
+bool do_login (int argc, const char **argv, struct ngadmin *nga);
/* mirror */
-bool do_mirror_disable (int nb, const char **com, struct ngadmin *nga);
-bool do_mirror_set (int nb, const char **com, struct ngadmin *nga);
-bool do_mirror_show (int nb, const char **com, struct ngadmin *nga);
+bool do_mirror_disable (int argc, const char **argv, struct ngadmin *nga);
+bool do_mirror_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_mirror_show (int argc, const char **argv, struct ngadmin *nga);
/* name */
-bool do_name_show (int nb, const char **com, struct ngadmin *nga);
-bool do_name_set (int nb, const char **com, struct ngadmin *nga);
-bool do_name_clear (int nb, const char **com, struct ngadmin *nga);
+bool do_name_show (int argc, const char **argv, struct ngadmin *nga);
+bool do_name_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_name_clear (int argc, const char **argv, struct ngadmin *nga);
/* netconf */
-bool do_netconf_set (int nb, const char **com, struct ngadmin *nga);
+bool do_netconf_set (int argc, const char **argv, struct ngadmin *nga);
/* password */
-bool do_password_change (int nb, const char **com, struct ngadmin *nga);
-bool do_password_set (int nb, const char **com, struct ngadmin *nga);
+bool do_password_change (int argc, const char **argv, struct ngadmin *nga);
+bool do_password_set (int argc, const char **argv, struct ngadmin *nga);
/* ports */
-bool do_ports_state (int nb, const char **com, struct ngadmin *nga);
-bool do_ports_statistics_reset (int nb, const char **com, struct ngadmin *nga);
-bool do_ports_statistics_show (int nb, const char **com, struct ngadmin *nga);
+bool do_ports_state (int argc, const char **argv, struct ngadmin *nga);
+bool do_ports_statistics_reset (int argc, const char **argv, struct ngadmin *nga);
+bool do_ports_statistics_show (int argc, const char **argv, struct ngadmin *nga);
/* qos */
-bool do_qos_mode (int nb, const char **com, struct ngadmin *nga);
-bool do_qos_set (int nb, const char **com, struct ngadmin *nga);
-bool do_qos_show (int nb, const char **com, struct ngadmin *nga);
+bool do_qos_mode (int argc, const char **argv, struct ngadmin *nga);
+bool do_qos_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_qos_show (int argc, const char **argv, struct ngadmin *nga);
/* quit */
-bool do_quit (int nb, const char **com, struct ngadmin *nga);
+bool do_quit (int argc, const char **argv, struct ngadmin *nga);
/* restart */
-bool do_restart (int nb, const char **com, struct ngadmin *nga);
+bool do_restart (int argc, const char **argv, struct ngadmin *nga);
/* scan */
-bool do_scan (int nb, const char **com, struct ngadmin *nga);
+bool do_scan (int argc, const char **argv, struct ngadmin *nga);
/* stormfilter */
-bool do_stormfilter_enable (int nb, const char **com, struct ngadmin *nga);
-bool do_stormfilter_disable (int nb, const char **com, struct ngadmin *nga);
-bool do_stormfilter_set (int nb, const char **com, struct ngadmin *nga);
-bool do_stormfilter_show (int nb, const char **com, struct ngadmin *nga);
+bool do_stormfilter_enable (int argc, const char **argv, struct ngadmin *nga);
+bool do_stormfilter_disable (int argc, const char **argv, struct ngadmin *nga);
+bool do_stormfilter_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_stormfilter_show (int argc, const char **argv, struct ngadmin *nga);
/* tree */
-bool do_tree (int nb, const char **com, struct ngadmin *nga);
+bool do_tree (int argc, const char **argv, struct ngadmin *nga);
/* vlan */
-bool do_vlan_8021q_del (int nb, const char **com, struct ngadmin *nga);
-bool do_vlan_8021q_set (int nb, const char **com, struct ngadmin *nga);
-bool do_vlan_8021q_show (int nb, const char **com, struct ngadmin *nga);
-bool do_vlan_mode_set (int nb, const char **com, struct ngadmin *nga);
-bool do_vlan_mode_show (int nb UNUSED, const char **com UNUSED, struct ngadmin *nga);
-bool do_vlan_pvid_set (int nb, const char **com, struct ngadmin *nga);
-bool do_vlan_pvid_show (int nb, const char **com, struct ngadmin *nga);
+bool do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga);
+bool do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga);
+bool do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_vlan_mode_show (int argc, const char **argv, struct ngadmin *nga);
+bool do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga);
+bool do_vlan_pvid_show (int argc, const char **argv, struct ngadmin *nga);
/* commands structure */
-COM_ROOT_START(coms)
+COM_ROOT_START(commands)
COM_START(bitrate)
- COM_TERM(set, do_bitrate_set, true)
- COM_TERM(show, do_bitrate_show, false)
+ COM_TERM(set, do_bitrate_set)
+ COM_TERM(show, do_bitrate_show)
COM_END
- COM_TERM(cabletest, do_cabletest, true)
+ COM_TERM(cabletest, do_cabletest)
- COM_TERM(defaults, do_defaults, false)
+ COM_TERM(defaults, do_defaults)
COM_START(firmware)
- COM_TERM(show, do_firmware_show, false)
- COM_TERM(upgrade, do_firmware_upgrade, true)
+ COM_TERM(show, do_firmware_show)
+ COM_TERM(upgrade, do_firmware_upgrade)
COM_END
- COM_TERM(help, do_help, false)
+ COM_TERM(help, do_help)
COM_START(igmp)
- COM_TERM(set, do_igmp_set, true)
- COM_TERM(show, do_igmp_show, false)
+ COM_TERM(set, do_igmp_set)
+ COM_TERM(show, do_igmp_show)
COM_END
- COM_TERM(list, do_list, false)
+ COM_TERM(list, do_list)
- COM_TERM(login, do_login, true)
+ COM_TERM(login, do_login)
COM_START(mirror)
- COM_TERM(disable, do_mirror_disable, false)
- COM_TERM(set, do_mirror_set, true)
- COM_TERM(show, do_mirror_show, false)
+ COM_TERM(disable, do_mirror_disable)
+ COM_TERM(set, do_mirror_set)
+ COM_TERM(show, do_mirror_show)
COM_END
COM_START(name)
- COM_TERM(show, do_name_show, false)
- COM_TERM(set, do_name_set, true)
- COM_TERM(clear, do_name_clear, false)
+ COM_TERM(show, do_name_show)
+ COM_TERM(set, do_name_set)
+ COM_TERM(clear, do_name_clear)
COM_END
COM_START(netconf)
- COM_TERM(set, do_netconf_set, true)
+ COM_TERM(set, do_netconf_set)
COM_END
COM_START(password)
- COM_TERM(change, do_password_change, true)
- COM_TERM(set, do_password_set, true)
+ COM_TERM(change, do_password_change)
+ COM_TERM(set, do_password_set)
COM_END
COM_START(ports)
- COM_TERM(state, do_ports_state, false)
+ COM_TERM(state, do_ports_state)
COM_START(statistics)
- COM_TERM(reset, do_ports_statistics_reset, false)
- COM_TERM(show, do_ports_statistics_show, false)
+ COM_TERM(reset, do_ports_statistics_reset)
+ COM_TERM(show, do_ports_statistics_show)
COM_END
COM_END
COM_START(qos)
- COM_TERM(mode, do_qos_mode, true)
- COM_TERM(set, do_qos_set, true)
- COM_TERM(show, do_qos_show, false)
+ COM_TERM(mode, do_qos_mode)
+ COM_TERM(set, do_qos_set)
+ COM_TERM(show, do_qos_show)
COM_END
- COM_TERM(quit, do_quit, false)
+ COM_TERM(quit, do_quit)
- COM_TERM(restart, do_restart, false)
+ COM_TERM(restart, do_restart)
- COM_TERM(scan, do_scan, false)
+ COM_TERM(scan, do_scan)
COM_START(stormfilter)
- COM_TERM(enable, do_stormfilter_enable, false)
- COM_TERM(disable, do_stormfilter_disable, false)
- COM_TERM(set, do_stormfilter_set, true)
- COM_TERM(show, do_stormfilter_show, false)
+ COM_TERM(enable, do_stormfilter_enable)
+ COM_TERM(disable, do_stormfilter_disable)
+ COM_TERM(set, do_stormfilter_set)
+ COM_TERM(show, do_stormfilter_show)
COM_END
- COM_TERM(tree, do_tree, false)
+ COM_TERM(tree, do_tree)
COM_START(vlan)
COM_START(802.1q)
- COM_TERM(del, do_vlan_8021q_del, true)
- COM_TERM(set, do_vlan_8021q_set, true)
- COM_TERM(show, do_vlan_8021q_show, true)
+ COM_TERM(del, do_vlan_8021q_del)
+ COM_TERM(set, do_vlan_8021q_set)
+ COM_TERM(show, do_vlan_8021q_show)
COM_END
COM_START(mode)
- COM_TERM(set, do_vlan_mode_set, true)
- COM_TERM(show, do_vlan_mode_show, false)
+ COM_TERM(set, do_vlan_mode_set)
+ COM_TERM(show, do_vlan_mode_show)
COM_END
COM_START(port)
- COM_TERM(set, NULL, true)
- COM_TERM(show, NULL, false)
+ COM_TERM(set, NULL)
+ COM_TERM(show, NULL)
COM_END
COM_START(pvid)
- COM_TERM(set, do_vlan_pvid_set, true)
- COM_TERM(show, do_vlan_pvid_show, false)
+ COM_TERM(set, do_vlan_pvid_set)
+ COM_TERM(show, do_vlan_pvid_show)
COM_END
COM_END
COM_ROOT_END
struct TreeNode {
const char *name;
bool (* const comfunc)(int, const char**, struct ngadmin*);
- bool hasArgs;
const struct TreeNode *sub;
};
-#define COM_ROOT_START(v) const struct TreeNode v = {.name = "<root>", .comfunc = NULL, .hasArgs = false, .sub = (const struct TreeNode[]){
-#define COM_ROOT_END {.name = NULL, .comfunc = NULL, .hasArgs = false, .sub = NULL}}};
-#define COM_START(nam) {.name = #nam, .comfunc = NULL, .hasArgs = false, .sub = (const struct TreeNode[]){
-#define COM_END {.name = NULL, .comfunc = NULL, .hasArgs = false, .sub = NULL}}},
-#define COM_TERM(nam, func, args) {.name = #nam, .comfunc = func, .hasArgs = args, .sub = NULL},
+#define COM_ROOT_START(v) const struct TreeNode v = {.name = "<root>", .comfunc = NULL, .sub = (const struct TreeNode[]){
+#define COM_ROOT_END {.name = NULL, .comfunc = NULL, .sub = NULL}}};
+#define COM_START(nam) {.name = #nam, .comfunc = NULL, .sub = (const struct TreeNode[]){
+#define COM_END {.name = NULL, .comfunc = NULL, .sub = NULL}}},
+#define COM_TERM(nam, func) {.name = #nam, .comfunc = func, .sub = NULL},
-extern const struct TreeNode coms;
+extern const struct TreeNode commands;
#endif
#define NORET __attribute__((noreturn))
-extern int cont;
+extern int main_loop_continue;
extern struct termios current_term;