1 /* Copyright (c) 2008, 2009 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
26 #include "command-line.h"
31 #define THIS_MODULE VLM_cfg_mod
34 /* Configuration when we first read the configuration file. */
35 static struct svec orig_cfg = SVEC_EMPTY_INITIALIZER;
38 usage(char *prog_name, int exit_code)
40 printf("Usage: %s --config-file=FILE ACTIONS\n"
42 " -T, --timeout=MS wait at most MS milliseconds for lock\n"
43 " -F, --config-file=FILE use configuration FILE\n"
45 " -a, --add=ENTRY add ENTRY\n"
46 " -d, --del-entry=ENTRY delete ENTRY\n"
47 " -D, --del-section=KEY delete section matching KEY\n"
48 " --del-match=PATTERN delete entries matching shell PATTERN\n"
49 " -q, --query=KEY return all entries matching KEY\n"
50 " -c, --log-changes log changes up to this point\n"
52 " -h, --help display this help message\n"
53 " -V, --version display version information\n",
59 open_config(char *config_file, int timeout)
64 error = cfg_set_file(config_file);
66 ovs_fatal(error, "failed to add configuration file \"%s\"",
70 error = cfg_lock(NULL, timeout);
72 ovs_fatal(error, "could not lock configuration file\n");
75 cfg_get_all(&orig_cfg);
85 cfg_get_all_strings(&vals, "%s", key);
87 for (i=0; i<vals.n; i++) {
88 printf("%s\n", vals.names[i]);
95 struct svec new_cfg, removed, added;
99 cfg_get_all(&new_cfg);
100 svec_diff(&orig_cfg, &new_cfg, &removed, NULL, &added);
101 if (removed.n || added.n) {
102 VLOG_INFO("configuration changes:");
103 for (i = 0; i < removed.n; i++) {
104 VLOG_INFO("-%s", removed.names[i]);
106 for (i = 0; i < added.n; i++) {
107 VLOG_INFO("+%s", added.names[i]);
110 VLOG_INFO("configuration unchanged");
112 svec_destroy(&added);
113 svec_destroy(&removed);
114 svec_swap(&new_cfg, &orig_cfg);
115 svec_destroy(&new_cfg);
118 int main(int argc, char *argv[])
121 OPT_DEL_MATCH = UCHAR_MAX + 1,
123 static const struct option long_options[] = {
124 {"config-file", required_argument, 0, 'F'},
125 {"timeout", required_argument, 0, 'T'},
126 {"add", required_argument, 0, 'a'},
127 {"del-entry", required_argument, 0, 'd'},
128 {"del-section", required_argument, 0, 'D'},
129 {"del-match", required_argument, 0, OPT_DEL_MATCH},
130 {"query", required_argument, 0, 'q'},
131 {"changes", no_argument, 0, 'c'},
132 {"verbose", optional_argument, 0, 'v'},
133 {"help", no_argument, 0, 'h'},
134 {"version", no_argument, 0, 'V'},
138 bool config_set = false;
139 int timeout = INT_MAX;
141 set_program_name(argv[0]);
145 short_options = long_options_to_short_options(long_options);
149 option = getopt_long(argc, argv, short_options, long_options, NULL);
154 if ((option > UCHAR_MAX || !strchr("FhVv?", option))
155 && config_set == false) {
156 ovs_fatal(0, "no config file specified (use --help for help)");
162 ovs_fatal(0, "--timeout or -T must be specified "
163 "before --file or -F");
165 timeout = atoi(optarg);
169 open_config(optarg, timeout);
174 cfg_add_entry("%s", optarg);
178 cfg_del_entry("%s", optarg);
182 cfg_del_section("%s", optarg);
186 cfg_del_match("%s", optarg);
198 usage(argv[0], EXIT_SUCCESS);
202 OVS_PRINT_VERSION(0, 0);
206 vlog_set_verbosity(optarg);
218 if (optind != argc) {
219 ovs_fatal(0, "non-option arguments not accepted "
220 "(use --help for help)");
223 if (cfg_is_dirty()) {