2 * Copyright (c) 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* The mother of all test programs that links with libopevswitch.la */
24 #include "command-line.h"
25 #include "dynamic-string.h"
29 static struct ovs_cmdl_command
*commands
= NULL
;
30 static size_t n_commands
= 0;
31 static size_t allocated_commands
= 0;
34 add_command(struct ovs_cmdl_command
*cmd
)
36 const struct ovs_cmdl_command nil
= {NULL
, NULL
, 0, 0, NULL
};
38 while (n_commands
+ 1 >= allocated_commands
) {
39 commands
= x2nrealloc(commands
, &allocated_commands
,
43 commands
[n_commands
] = *cmd
;
44 commands
[n_commands
+ 1] = nil
;
48 #define OVSTEST_USAGE \
49 "TEST [TESTARGS] where 'TEST' is a string, 'TESTARGS' are optional \n"\
50 "arguments of the TEST"
53 flush_help_string(struct ds
*ds
)
55 if (ds
->length
> 2 ) {
57 printf ("%s\n", ds_cstr(ds
));
63 help(struct ovs_cmdl_context
*ctx OVS_UNUSED
)
65 const struct ovs_cmdl_command
*p
;
66 struct ds test_names
= DS_EMPTY_INITIALIZER
;
67 const int linesize
= 70;
69 printf("%s: the big test executable\n"
70 "usage: %s TEST [TESTARGS]\n"
71 "where TEST is one of the following. \n\n",
72 program_name
, program_name
);
74 for(p
= commands
; p
->name
!= NULL
; p
++) {
75 if (*p
->name
!= '-') { /* Skip internal commands */
76 ds_put_format(&test_names
, "%s, ", p
->name
);
77 if ((test_names
.length
) >= linesize
) {
78 flush_help_string(&test_names
);
82 flush_help_string(&test_names
);
83 ds_destroy(&test_names
);
87 add_top_level_commands(void)
89 struct ovs_cmdl_command help_cmd
= {"--help", NULL
, 0, 0, help
};
91 add_command(&help_cmd
);
95 ovstest_register(const char *test_name
, ovs_cmdl_handler f
)
97 struct ovs_cmdl_command test_cmd
;
99 test_cmd
.name
= test_name
;
100 test_cmd
.usage
= NULL
;
101 test_cmd
.min_args
= 0;
102 test_cmd
.max_args
= INT_MAX
;
103 test_cmd
.handler
= f
;
105 add_command(&test_cmd
);
111 if (allocated_commands
) {
117 main(int argc
, char *argv
[])
119 set_program_name(argv
[0]);
122 ovs_fatal(0, "expect test program to be specified; "
123 "use --help for usage");
126 add_top_level_commands();
128 struct ovs_cmdl_context ctx
= {
132 ovs_cmdl_run_command(&ctx
, commands
);