From 6a07479b9eac213c707e04cdf090f1fdb14e16a0 Mon Sep 17 00:00:00 2001 From: Patrik Flykt Date: Tue, 12 Feb 2013 13:55:34 +0200 Subject: [PATCH] client: Create prototypes for all commands Provide the infrastructure to factor out the commands. --- client/commands.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ client/interactive.c | 15 ++++--- client/interactive.h | 1 + client/main.c | 14 ++++--- 4 files changed, 133 insertions(+), 11 deletions(-) diff --git a/client/commands.c b/client/commands.c index 75856cd..cbc440f 100644 --- a/client/commands.c +++ b/client/commands.c @@ -64,6 +64,8 @@ static char *proxy_simple[] = { NULL }; +static int cmd_help(char *args[], int num, struct option *options); + void show_help(void) { printf("Usage: connmanctl [args]\n" @@ -278,6 +280,118 @@ int monitor_switch(int argc, char *argv[], int c, DBusConnection *conn) return 0; } +static int cmd_enable(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_disable(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_state(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_services(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_technologies(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_scan(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_connect(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_disconnect(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_config(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_monitor(char *args[], int num, struct option *options) +{ + return -1; +} + +static int cmd_exit(char *args[], int num, struct option *options) +{ + return 0; +} + +static const struct { + const char *cmd; + const char *argument; + struct option *options; + const char **options_desc; + int (*func) (char *args[], int num, struct option *options); + const char *desc; +} cmd_table[] = { + { "enable", "|offline", NULL, NULL, + cmd_enable, "Enables given technology or offline mode" }, + { "disable", "|offline", NULL, NULL, + cmd_disable, "Disables given technology or offline mode"}, + { "state", NULL, NULL, NULL, + cmd_state, "Shows if the system is online or offline" }, + { "services", NULL, NULL, NULL, + cmd_services, "Display services" }, + { "technologies", NULL, NULL, NULL, + cmd_technologies, "Display technologies" }, + { "scan", "", NULL, NULL, + cmd_scan, "Scans for new services for given technology" }, + { "connect", "", NULL, NULL, + cmd_connect, "Connect a given service" }, + { "disconnect", "", NULL, NULL, + cmd_disconnect, "Disconnect a given service" }, + { "config", "", NULL, NULL, + cmd_config, "Set service configuration options" }, + { "monitor", NULL, NULL, NULL, + cmd_monitor, "Monitor signals from interfaces" }, + { "help", NULL, NULL, NULL, + cmd_help, "Show help" }, + { "exit", NULL, NULL, NULL, + cmd_exit, "Exit" }, + { "quit", NULL, NULL, NULL, + cmd_exit, "Quit" }, + { NULL, }, +}; + +static int cmd_help(char *args[], int num, struct option *options) +{ + return -1; +} + +int commands(DBusConnection *connection, char *argv[], int argc) +{ + int i; + + for (i = 0; cmd_table[i].cmd != NULL; i++) { + if (g_strcmp0(cmd_table[i].cmd, argv[0]) == 0 && + cmd_table[i].func != NULL) { + return cmd_table[i].func(argv, argc, + cmd_table[i].options); + } + } + + return -1; +} + int commands_no_options(DBusConnection *connection, char *argv[], int argc) { DBusMessage *message = NULL; diff --git a/client/interactive.c b/client/interactive.c index d2c4f79..5f9c77a 100644 --- a/client/interactive.c +++ b/client/interactive.c @@ -87,13 +87,16 @@ static gboolean rl_handler(char *input) free(input); exit(EXIT_FAILURE); } else { - error = commands_no_options(interactive_conn, + error = commands(interactive_conn, long_args, num_args); + if (error == -1) { + error = commands_no_options(interactive_conn, + long_args, num_args); + if (error == -1) + error = commands_options(interactive_conn, long_args, num_args); - if (error == -1) - error = commands_options(interactive_conn, long_args, - num_args); - else - return error; + else + return error; + } } if ((strcmp(long_args[0], "quit") == 0) || (strcmp(long_args[0], "exit") == 0) diff --git a/client/interactive.h b/client/interactive.h index a2bd051..a09aca3 100644 --- a/client/interactive.h +++ b/client/interactive.h @@ -23,6 +23,7 @@ #include void show_interactive(DBusConnection *connection, GMainLoop *mainloop); +int commands(DBusConnection *connection, char *argv[], int argc); int commands_no_options(DBusConnection *connection, char *argv[], int argc); int commands_options(DBusConnection *connection, char *argv[], int argc); void show_help(void); diff --git a/client/main.c b/client/main.c index ab64277..d54d148 100644 --- a/client/main.c +++ b/client/main.c @@ -95,13 +95,17 @@ int main(int argc, char *argv[]) if (argc < 2) show_interactive(connection, main_loop); - error = commands_no_options(connection, argv + 1, argc - 1); + error = commands(connection, argv + 1, argc -1); if (error == -1) { - error = commands_options(connection, argv + 1, argc - 1); - if (strcmp(argv[1], "monitor") != 0) + error = commands_no_options(connection, argv + 1, argc - 1); + if (error == -1) { + error = commands_options(connection, argv + 1, + argc - 1); + if (strcmp(argv[1], "monitor") != 0) + return error; + } else { return error; - } else { - return error; + } } if (error == -1) { -- 2.7.4