5 * Copyright (C) 2012-2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <readline/readline.h>
32 #include <readline/history.h>
38 static DBusConnection *connection;
39 static GMainLoop *main_loop;
40 static bool interactive = false;
42 static bool save_input;
43 static char *saved_line;
44 static int saved_point;
46 void __connmanctl_quit(void)
48 if (main_loop != NULL)
49 g_main_loop_quit(main_loop);
52 bool __connmanctl_is_interactive(void)
57 void __connmanctl_save_rl(void)
59 save_input = !RL_ISSTATE(RL_STATE_DONE);
62 saved_point = rl_point;
63 saved_line = rl_copy_text(0, rl_end);
65 rl_replace_line("", 0);
70 void __connmanctl_redraw_rl(void)
74 rl_replace_line(saved_line, 0);
75 rl_point = saved_point;
83 static void rl_handler(char *input)
85 char **args, **trim_args;
90 g_main_loop_quit(main_loop);
94 args = g_strsplit(input, " ", 0);
95 num = g_strv_length(args);
97 trim_args = g_new0(char *, num);
98 for (i = 0, len = 0; i < num; i++) {
99 if (*args[i] != '\0') {
100 trim_args[len] = args[i];
109 err = __connmanctl_commands(connection, trim_args, len);
112 g_main_loop_quit(main_loop);
119 static gboolean input_handler(GIOChannel *channel, GIOCondition condition,
122 if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
123 g_main_loop_quit(main_loop);
127 rl_callback_read_char();
131 static char **complete_agent(const char *text, int start, int end)
133 rl_attempted_completion_over = 1;
138 static char **complete_command(const char *text, int start, int end)
140 char **command = NULL;
142 rl_attempted_completion_over = 1;
145 command = rl_completion_matches(text,
146 __connmanctl_lookup_command);
151 void __connmanctl_agent_mode(const char *prompt,
152 connmanctl_input_func_t input_handler)
154 if (input_handler != NULL)
155 rl_callback_handler_install(prompt, input_handler);
157 rl_set_prompt(prompt);
158 rl_callback_handler_remove();
160 rl_attempted_completion_function = complete_agent;
163 void __connmanctl_command_mode(void)
165 rl_callback_handler_install("connmanctl> ", rl_handler);
166 rl_attempted_completion_function = complete_command;
169 int __connmanctl_input_init(int argc, char *argv[])
180 dbus_error_init(&dbus_err);
181 connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &dbus_err);
183 if (dbus_error_is_set(&dbus_err)) {
184 fprintf(stderr, "Error: %s\n", dbus_err.message);
185 dbus_error_free(&dbus_err);
189 channel = g_io_channel_unix_new(fileno(stdin));
190 source = g_io_add_watch(channel, G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
191 input_handler, NULL);
192 g_io_channel_unref(channel);
197 __connmanctl_command_mode();
203 if (strcmp(argv[1], "--help") == 0 ||
204 strcmp(argv[1], "-h") == 0)
205 err = __connmanctl_commands(connection, help, 1);
207 err = __connmanctl_commands(connection, argv + 1,
211 if (err == -EINPROGRESS) {
212 main_loop = g_main_loop_new(NULL, FALSE);
213 g_main_loop_run(main_loop);
218 g_source_remove(source);
220 if (interactive == true) {
221 rl_callback_handler_remove();
225 dbus_connection_unref(connection);
226 if (main_loop != NULL)
227 g_main_loop_unref(main_loop);