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)
63 error = cfg_set_file(config_file);
65 ovs_fatal(error, "failed to add configuration file \"%s\"",
69 error = cfg_lock(NULL, timeout);
71 ovs_fatal(error, "could not lock configuration file\n");
74 cfg_get_all(&orig_cfg);
84 cfg_get_all_strings(&vals, "%s", key);
86 for (i=0; i<vals.n; i++) {
87 printf("%s\n", vals.names[i]);
94 struct svec new_cfg, removed, added;
98 cfg_get_all(&new_cfg);
99 svec_diff(&orig_cfg, &new_cfg, &removed, NULL, &added);
100 if (removed.n || added.n) {
101 VLOG_INFO("configuration changes:");
102 for (i = 0; i < removed.n; i++) {
103 VLOG_INFO("-%s", removed.names[i]);
105 for (i = 0; i < added.n; i++) {
106 VLOG_INFO("+%s", added.names[i]);
109 VLOG_INFO("configuration unchanged");
111 svec_destroy(&added);
112 svec_destroy(&removed);
113 svec_swap(&new_cfg, &orig_cfg);
114 svec_destroy(&new_cfg);
117 int main(int argc, char *argv[])
120 OPT_DEL_MATCH = UCHAR_MAX + 1,
122 static const struct option long_options[] = {
123 {"config-file", required_argument, 0, 'F'},
124 {"timeout", required_argument, 0, 'T'},
125 {"add", required_argument, 0, 'a'},
126 {"del-entry", required_argument, 0, 'd'},
127 {"del-section", required_argument, 0, 'D'},
128 {"del-match", required_argument, 0, OPT_DEL_MATCH},
129 {"query", required_argument, 0, 'q'},
130 {"changes", no_argument, 0, 'c'},
131 {"verbose", optional_argument, 0, 'v'},
132 {"help", no_argument, 0, 'h'},
133 {"version", no_argument, 0, 'V'},
137 bool config_set = false;
138 int timeout = INT_MAX;
140 set_program_name(argv[0]);
144 short_options = long_options_to_short_options(long_options);
148 option = getopt_long(argc, argv, short_options, long_options, NULL);
153 if ((option > UCHAR_MAX || !strchr("FhVv?", option))
154 && config_set == false) {
155 ovs_fatal(0, "no config file specified (use --help for help)");
161 ovs_fatal(0, "--timeout or -T must be specified "
162 "before --file or -F");
164 timeout = atoi(optarg);
168 open_config(optarg, timeout);
173 cfg_add_entry("%s", optarg);
177 cfg_del_entry("%s", optarg);
181 cfg_del_section("%s", optarg);
185 cfg_del_match("%s", optarg);
197 usage(argv[0], EXIT_SUCCESS);
201 OVS_PRINT_VERSION(0, 0);
205 vlog_set_verbosity(optarg);
217 if (optind != argc) {
218 ovs_fatal(0, "non-option arguments not accepted "
219 "(use --help for help)");
222 if (cfg_is_dirty()) {