-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
194 lines (164 loc) · 5.31 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2022 Orange
* Copyright 2022 Warsaw University of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <getopt.h>
#include <stdio.h>
#include "CLI/action_selector.h"
#include "CLI/clone_session.h"
#include "CLI/common.h"
#include "CLI/counter.h"
#include "CLI/digest.h"
#include "CLI/meter.h"
#include "CLI/multicast.h"
#include "CLI/os_validate.h"
#include "CLI/pipeline.h"
#include "CLI/register.h"
#include "CLI/table.h"
#include "CLI/value_set.h"
/* Removing this line will require too much effort or result in strange C construct (assign to const value) */
const char *program_name; /* NOLINT(cppcoreguidelines-avoid-non-const-global-variables) */
int cmd_select(const struct cmd *cmds, int argc, char **argv,
int (*help)(int, char **))
{
if (argc < 1) {
return help(argc, argv);
}
for (unsigned int i = 0; cmds[i].cmd; i++) {
if (is_keyword(*argv, cmds[i].cmd)) {
if (!cmds[i].func) {
return -1;
}
return cmds[i].func(argc - 1, argv + 1);
}
}
fprintf(stderr, "%s: unknown keyword\n", *argv);
help(argc - 1, argv + 1);
return -1;
}
static int do_help(int argc, char **argv)
{
(void) argc; (void) argv;
fprintf(stderr,
"Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
" %s help\n"
"\n"
" OBJECT := { clone-session |\n"
" multicast-group |\n"
" pipeline |\n"
" add-port |\n"
" del-port |\n"
" table |\n"
" action-selector |\n"
" action-profile |\n"
" meter |\n"
" digest |\n"
" counter |\n"
" register |\n"
" value-set |\n"
" validate-os }\n"
" OPTIONS := {}\n"
"",
program_name, program_name);
return 0;
}
static int do_pipeline(int argc, char **argv)
{
return cmd_select(pipeline_cmds, argc, argv, do_pipeline_help);
}
static int do_port_add(int argc, char **argv)
{
if (is_keyword(*argv, "help") || argc < 1) {
return do_pipeline_help(argc, argv);
}
return do_pipeline_port_add(argc, argv);
}
static int do_port_del(int argc, char **argv)
{
if (is_keyword(*argv, "help") || argc < 1) {
return do_pipeline_help(argc, argv);
}
return do_pipeline_port_del(argc, argv);
}
static int do_clone_session(int argc, char **argv)
{
if (argc < 2) {
do_clone_session_help(argc, argv);
return -1;
}
return cmd_select(clone_session_cmds, argc, argv, do_clone_session_help);
}
static int do_multicast(int argc, char **argv)
{
return cmd_select(multicast_cmds, argc, argv, do_multicast_help);
}
static int do_table(int argc, char **argv)
{
return cmd_select(table_cmds, argc, argv, do_table_help);
}
static int do_meter(int argc, char **argv)
{
return cmd_select(meter_cmds, argc, argv, do_meter_help);
}
static int do_action_selector(int argc, char **argv)
{
return cmd_select(action_selector_cmds, argc, argv, do_action_selector_help);
}
static int do_action_profile(int argc, char **argv)
{
return cmd_select(action_profile_cmds, argc, argv, do_action_profile_help);
}
static int do_digest(int argc, char **argv)
{
return cmd_select(digest_cmds, argc, argv, do_digest_help);
}
static int do_counter(int argc, char **argv)
{
return cmd_select(counter_cmds, argc, argv, do_counter_help);
}
static int do_register(int argc, char **argv)
{
return cmd_select(register_cmds, argc, argv, do_register_help);
}
static int do_value_set(int argc, char **argv)
{
return cmd_select(value_set_cmds, argc, argv, do_value_set_help);
}
static const struct cmd cmds[] = {
{ "help", do_help },
{ "pipeline", do_pipeline },
{ "add-port", do_port_add },
{ "del-port", do_port_del },
{ "clone-session", do_clone_session },
{ "multicast-group", do_multicast },
{ "table", do_table },
{ "action-selector", do_action_selector },
{ "action-profile", do_action_profile },
{ "meter", do_meter },
{ "digest", do_digest },
{ "counter", do_counter },
{ "register", do_register },
{ "value-set", do_value_set },
{ "validate-os", do_os_validate },
{ 0 }
};
int main(int argc, char **argv)
{
program_name = argv[0];
/* TODO: parse program options */
argc -= optind;
argv += optind;
return cmd_select(cmds, argc, argv, do_help);
}