2 * This file is part of buxton.
4 * Copyright (C) 2013 Intel Corporation
6 * buxton is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
17 * \file cli/main.c Buxton command line interface
19 * Provides a CLI to Buxton through which a variety of operations can be
33 #include "configurator.h"
39 static Hashmap *commands;
40 static BuxtonControl control;
42 static void print_version(void)
44 printf("buxtonctl " PACKAGE_VERSION "\n"
45 "Copyright (C) 2013 Intel Corporation\n"
46 "buxton is free software; you can redistribute it and/or modify\n"
47 "it under the terms of the GNU Lesser General Public License as\n"
48 "published by the Free Software Foundation; either version 2.1\n"
49 "of the License, or (at your option) any later version.\n");
52 static bool print_help(void)
58 printf("buxtonctl: Usage\n\n");
60 HASHMAP_FOREACH_KEY(command, key, commands, iterator) {
61 printf("\t%12s - %s\n", key, command->description);
67 static void print_usage(Command *command)
69 if (command->min_arguments == command->max_arguments) {
70 printf("%s takes %d arguments - %s\n", command->name, command->min_arguments, command->usage);
72 printf("%s takes at least %d arguments - %s\n", command->name, command->min_arguments, command->usage);
77 * Entry point into buxtonctl
78 * @param argc Number of arguments passed
79 * @param argv An array of string arguments
80 * @returns EXIT_SUCCESS if the operation succeeded, otherwise EXIT_FAILURE
82 int main(int argc, char **argv)
85 Command c_get_string, c_set_string;
86 Command c_get_int32, c_set_int32;
87 Command c_get_uint32, c_set_uint32;
88 Command c_get_int64, c_set_int64;
89 Command c_get_uint64, c_set_uint64;
90 Command c_get_float, c_set_float;
91 Command c_get_double, c_set_double;
92 Command c_get_bool, c_set_bool;
94 Command c_create_group, c_remove_group;
95 Command c_unset_value;
101 bool version = false;
102 control.client.direct = false;
103 char *conf_path = NULL;
104 BuxtonClient client = NULL;
106 /* libtool bites my twinkie */
107 include_configurator();
111 /* Build a command list */
112 commands = hashmap_new(string_hash_func, string_compare_func);
118 c_get_string = (Command) { "get-string", "Get a string value by key",
119 2, 3, "[layer] group name", &cli_get_value, STRING };
120 hashmap_put(commands, c_get_string.name, &c_get_string);
122 c_set_string = (Command) { "set-string", "Set a key with a string value",
123 4, 4, "layer group name value", &cli_set_value, STRING };
124 hashmap_put(commands, c_set_string.name, &c_set_string);
127 c_get_int32 = (Command) { "get-int32", "Get an int32_t value by key",
128 2, 3, "[layer] group name", &cli_get_value, INT32 };
129 hashmap_put(commands, c_get_int32.name, &c_get_int32);
131 c_set_int32 = (Command) { "set-int32", "Set a key with an int32_t value",
132 4, 4, "layer group name value", &cli_set_value, INT32 };
133 hashmap_put(commands, c_set_int32.name, &c_set_int32);
135 /* Unsigned 32bit Integers */
136 c_get_uint32 = (Command) { "get-uint32", "Get an uint32_t value by key",
137 2, 3, "[layer] group name", &cli_get_value, UINT32 };
138 hashmap_put(commands, c_get_uint32.name, &c_get_uint32);
140 c_set_uint32 = (Command) { "set-uint32", "Set a key with an uint32_t value",
141 4, 4, "layer group name value", &cli_set_value, UINT32 };
142 hashmap_put(commands, c_set_uint32.name, &c_set_uint32);
145 c_get_int64 = (Command) { "get-int64", "Get an int64_t value by key",
146 2, 3, "[layer] group name", &cli_get_value, INT64};
147 hashmap_put(commands, c_get_int64.name, &c_get_int64);
149 c_set_int64 = (Command) { "set-int64", "Set a key with an int64_t value",
150 4, 4, "layer group name value", &cli_set_value, INT64 };
151 hashmap_put(commands, c_set_int64.name, &c_set_int64);
153 /* Unsigned 32bit Integers */
154 c_get_uint64 = (Command) { "get-uint64", "Get an uint64_t value by key",
155 2, 3, "[layer] group name", &cli_get_value, UINT64};
156 hashmap_put(commands, c_get_uint64.name, &c_get_uint64);
158 c_set_uint64 = (Command) { "set-uint64", "Set a key with an uint64_t value",
159 4, 4, "layer group name value", &cli_set_value, UINT64 };
160 hashmap_put(commands, c_set_uint64.name, &c_set_uint64);
163 c_get_float = (Command) { "get-float", "Get a float point value by key",
164 2, 3, "[layer] group name", &cli_get_value, FLOAT };
165 hashmap_put(commands, c_get_float.name, &c_get_float);
167 c_set_float = (Command) { "set-float", "Set a key with a floating point value",
168 4, 4, "layer group name value", &cli_set_value, FLOAT };
169 hashmap_put(commands, c_set_float.name, &c_set_float);
172 c_get_double = (Command) { "get-double", "Get a double precision value by key",
173 2, 3, "[layer] group name", &cli_get_value, DOUBLE };
174 hashmap_put(commands, c_get_double.name, &c_get_double);
176 c_set_double = (Command) { "set-double", "Set a key with a double precision value",
177 4, 4, "layer group name value", &cli_set_value, DOUBLE };
178 hashmap_put(commands, c_set_double.name, &c_set_double);
181 c_get_bool = (Command) { "get-bool", "Get a boolean value by key",
182 2, 3, "[layer] group name", &cli_get_value, BOOLEAN };
183 hashmap_put(commands, c_get_bool.name, &c_get_bool);
185 c_set_bool = (Command) { "set-bool", "Set a key with a boolean value",
186 4, 4, "layer group name value", &cli_set_value, BOOLEAN };
187 hashmap_put(commands, c_set_bool.name, &c_set_bool);
190 c_set_label = (Command) { "set-label", "Set a value's label",
191 3, 4, "layer group [name] label", &cli_set_label, STRING };
193 hashmap_put(commands, c_set_label.name, &c_set_label);
195 /* Group management */
196 c_create_group = (Command) { "create-group", "Create a group in a layer",
197 2, 2, "layer group", &cli_create_group, STRING };
198 hashmap_put(commands, c_create_group.name, &c_create_group);
200 c_remove_group = (Command) { "remove-group", "Remove a group from a layer",
201 2, 2, "layer group", &cli_remove_group, STRING };
202 hashmap_put(commands, c_remove_group.name, &c_remove_group);
205 c_unset_value = (Command) { "unset-value", "Unset a value by key",
206 3, 3, "layer group name", &cli_unset_value, STRING };
207 hashmap_put(commands, c_unset_value.name, &c_unset_value);
209 /* Create db for layer */
210 c_create_db = (Command) { "create-db", "Create the database file for a layer",
211 1, 1, "layer", &cli_create_db, STRING };
212 hashmap_put(commands, c_create_db.name, &c_create_db);
214 static struct option opts[] = {
215 { "config-file", 1, NULL, 'c' },
216 { "direct", 0, NULL, 'd' },
217 { "help", 0, NULL, 'h' },
218 { "version", 0, NULL, 'v' },
223 c = getopt_long(argc, argv, "c:dvh", opts, &i);
231 conf_path = strdup(optarg);
237 control.client.direct = true;
253 if (optind == argc) {
258 if ((command = hashmap_get(commands, argv[optind])) == NULL) {
259 printf("Unknown command: %s\n", argv[optind]);
263 /* We now execute the command */
264 if (command->method == NULL) {
265 printf("Not yet implemented: %s\n", command->name);
270 /* Ensure we cleanup and abort when using help */
271 print_usage(command);
276 if ((argc - optind - 1 < command->min_arguments) ||
277 (argc - optind - 1 > command->max_arguments)) {
278 print_usage(command);
284 control.client.uid = geteuid();
285 if (!control.client.direct) {
287 if (buxton_set_conf_file(conf_path)) {
288 printf("Failed to set configuration file path\n");
291 if (buxton_open(&client) < 0) {
292 control.client.direct = true;
294 control.client = *(_BuxtonClient *)client;
298 if (control.client.direct) {
303 r = stat(conf_path, &st);
305 printf("Invalid configuration file path\n");
308 if (st.st_mode & S_IFDIR) {
309 printf("Configuration file given is a directory\n");
313 buxton_add_cmd_line(CONFIG_CONF_FILE, conf_path);
315 if (!buxton_direct_open(&(control))) {
316 printf("Failed to directly talk to Buxton\n");
322 /* Connected to buxton_client, execute method */
323 ret = command->method(&control, command->type,
324 optind + 1 < argc ? argv[optind + 1] : NULL,
325 optind + 2 < argc ? argv[optind + 2] : NULL,
326 optind + 3 < argc ? argv[optind + 3] : NULL,
327 optind + 4 < argc ? argv[optind + 4] : NULL);
331 hashmap_free(commands);
332 if (control.client.direct) {
333 buxton_direct_close(&control);
336 buxton_close(client);
347 * Editor modelines - http://www.wireshark.org/tools/modelines.html
352 * indent-tabs-mode: t
355 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
356 * :indentSize=8:tabSize=8:noTabs=false: