]> git.sur5r.net Git - ngadmin/blob - cli/com_bitrate.c
Let commands handle themselves absence of arguments
[ngadmin] / cli / com_bitrate.c
1
2 #include "commands.h"
3
4
5 /* helper function to analyse bitrate speed specifications */
6 static int bitrate_analyse (int argc, const char **argv, int *ports)
7 {
8         int i = 0, s;
9         
10         
11         while (i < argc - 1) {
12                 s = parseBitrate(argv[i + 1]);
13                 if (strcmp(argv[i], "inout") == 0) {
14                         ports[0] = s;
15                         ports[1] = s;
16                 } else if (strcmp(argv[i], "in") == 0) {
17                         ports[0] = s;
18                 } else if (strcmp(argv[i], "out") == 0) {
19                         ports[1] = s;
20                 } else {
21                         break;
22                 }
23                 i += 2;
24         }
25         
26         
27         return i;
28 }
29
30
31 bool do_bitrate_set (int argc, const char **argv, struct ngadmin *nga)
32 {
33         int i, k = 0, defs[] = {12, 12}, p, *ports = NULL;
34         const struct swi_attr *sa;
35         bool ret = true;
36         
37         if (argc < 2) {
38                 printf(
39                 "usage: bitrate set [all SPEEDSPEC] <port1> SPEEDSPEC [<port2> SPEEDSPEC ...]\n"
40                 "SPEEDSPEC: [inout <speed>] [in <ispeed>] [out <ospeed>]\n"
41                 );
42                 ret = false;
43                 goto end;
44         }
45         
46         sa = ngadmin_getCurrentSwitch(nga);
47         if (sa == NULL) {
48                 printf("must be logged\n");
49                 ret = false;
50                 goto end;
51         }
52         
53         ports = malloc(2 * sa->ports * sizeof(int));
54         
55         /* get defaults if present */
56         if (strcmp(argv[k], "all") == 0) {
57                 k++;
58                 k += bitrate_analyse(argc - k, &argv[k], defs);
59         }
60         
61         /* apply defaults */
62         for (i = 0; i < sa->ports; i++)
63                 memcpy(&ports[2 * i], defs, sizeof(defs));
64         
65         /* get ports specifics */
66         while (k < argc) {
67                 p = strtol(argv[k++], NULL, 0) - 1;
68                 if (p >= 0 &&  p <sa->ports)
69                         k += bitrate_analyse(argc - k, &argv[k], &ports[2 * p]);
70         }
71         
72         /* send it to the switch */
73         i = ngadmin_setBitrateLimits(nga, ports);
74         printErrCode(i);
75         
76 end:
77         free(ports);
78         
79         return ret;
80 }
81
82
83 bool do_bitrate_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
84 {
85         int i, ret = true, *ports = NULL;
86         const struct swi_attr *sa;
87         
88         
89         if (argc > 0) {
90                 printf("this command takes no argument\n");
91                 ret = false;
92                 goto end;
93         }
94         
95         sa = ngadmin_getCurrentSwitch(nga);
96         if (sa == NULL) {
97                 printf("must be logged\n");
98                 ret = false;
99                 goto end;
100         }
101         
102         
103         ports = malloc(2 * sa->ports * sizeof(int));
104         i = ngadmin_getBitrateLimits(nga, ports);
105         if (i != ERR_OK) {
106                 printErrCode(i);
107                 ret = false;
108                 goto end;
109         }
110         
111         for (i = 0; i < sa->ports; i++)
112                 printf("port %i: in %s, out %s\n", i + 1, bitrates[ports[2 * i + 0]], bitrates[ports[2 * i + 1]]);
113         
114 end:
115         free(ports);
116         
117         return ret;
118 }
119
120
121