Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2014 Nicira, Inc.
3 : : *
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:
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
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.
15 : : */
16 : :
17 : : /* The mother of all test programs that links with libopevswitch.la */
18 : :
19 : : #include <config.h>
20 : : #undef NDEBUG
21 : : #include <inttypes.h>
22 : : #include <limits.h>
23 : : #include <stdlib.h>
24 : : #include "command-line.h"
25 : : #include "openvswitch/dynamic-string.h"
26 : : #include "ovstest.h"
27 : : #include "util.h"
28 : :
29 : : static struct ovs_cmdl_command *commands = NULL;
30 : : static size_t n_commands = 0;
31 : : static size_t allocated_commands = 0;
32 : :
33 : : static void
34 : 23480 : add_command(struct ovs_cmdl_command *cmd)
35 : : {
36 : 23480 : const struct ovs_cmdl_command nil = {NULL, NULL, 0, 0, NULL, OVS_RO};
37 : :
38 [ + + ]: 27589 : while (n_commands + 1 >= allocated_commands) {
39 : 4109 : commands = x2nrealloc(commands, &allocated_commands,
40 : : sizeof *cmd);
41 : : }
42 : :
43 : 23480 : commands[n_commands] = *cmd;
44 : 23480 : commands[n_commands + 1] = nil;
45 : 23480 : n_commands++;
46 : 23480 : }
47 : :
48 : : #define OVSTEST_USAGE \
49 : : "TEST [TESTARGS] where 'TEST' is a string, 'TESTARGS' are optional \n"\
50 : : "arguments of the TEST"
51 : :
52 : : static void
53 : 0 : flush_help_string(struct ds *ds)
54 : : {
55 [ # # ]: 0 : if (ds->length > 2 ) {
56 : 0 : ds->length -= 2;
57 : 0 : printf ("%s\n", ds_cstr(ds));
58 : 0 : ds_clear(ds);
59 : : }
60 : 0 : }
61 : :
62 : : static void
63 : 0 : help(struct ovs_cmdl_context *ctx OVS_UNUSED)
64 : : {
65 : : const struct ovs_cmdl_command *p;
66 : 0 : struct ds test_names = DS_EMPTY_INITIALIZER;
67 : 0 : const int linesize = 70;
68 : :
69 : 0 : 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);
73 : :
74 [ # # ]: 0 : for(p = commands; p->name != NULL; p++) {
75 [ # # ]: 0 : if (*p->name != '-') { /* Skip internal commands */
76 : 0 : ds_put_format(&test_names, "%s, ", p->name);
77 [ # # ]: 0 : if ((test_names.length) >= linesize) {
78 : 0 : flush_help_string(&test_names);
79 : : }
80 : : }
81 : : }
82 : 0 : flush_help_string(&test_names);
83 : 0 : ds_destroy(&test_names);
84 : 0 : }
85 : :
86 : : static void
87 : 587 : add_top_level_commands(void)
88 : : {
89 : 587 : struct ovs_cmdl_command help_cmd = {"--help", NULL, 0, 0, help, OVS_RO };
90 : :
91 : 587 : add_command(&help_cmd);
92 : 587 : }
93 : :
94 : : void
95 : 22893 : ovstest_register(const char *test_name, ovs_cmdl_handler f)
96 : : {
97 : : struct ovs_cmdl_command test_cmd;
98 : :
99 : 22893 : test_cmd.name = test_name;
100 : 22893 : test_cmd.usage = NULL;
101 : 22893 : test_cmd.min_args = 0;
102 : 22893 : test_cmd.max_args = INT_MAX;
103 : 22893 : test_cmd.handler = f;
104 : :
105 : 22893 : add_command(&test_cmd);
106 : 22893 : }
107 : :
108 : : static void
109 : 476 : cleanup(void)
110 : : {
111 [ + - ]: 476 : if (allocated_commands) {
112 : 476 : free(commands);
113 : : }
114 : 476 : }
115 : :
116 : : int
117 : 587 : main(int argc, char *argv[])
118 : : {
119 : 587 : set_program_name(argv[0]);
120 : :
121 [ - + ]: 587 : if (argc < 2) {
122 : 0 : ovs_fatal(0, "expect test program to be specified; "
123 : : "use --help for usage");
124 : : }
125 : :
126 : 587 : add_top_level_commands();
127 [ + - ]: 587 : if (argc > 1) {
128 : 1761 : struct ovs_cmdl_context ctx = {
129 : 587 : .argc = argc - 1,
130 : 587 : .argv = argv + 1,
131 : : };
132 : 587 : ovs_cmdl_run_command(&ctx, commands);
133 : : }
134 : 476 : cleanup();
135 : :
136 : 476 : return 0;
137 : : }
|