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 static connmanctl_input_func_t *readline_input_handler;
48 void __connmanctl_quit(void)
50 if (main_loop != NULL)
51 g_main_loop_quit(main_loop);
54 bool __connmanctl_is_interactive(void)
59 void __connmanctl_save_rl(void)
61 save_input = !RL_ISSTATE(RL_STATE_DONE);
64 saved_point = rl_point;
65 saved_line = rl_copy_text(0, rl_end);
67 rl_replace_line("", 0);
72 void __connmanctl_redraw_rl(void)
76 rl_replace_line(saved_line, 0);
77 rl_point = saved_point;
85 static void rl_handler(char *input)
87 char **args, **trim_args;
92 g_main_loop_quit(main_loop);
96 args = g_strsplit(input, " ", 0);
97 num = g_strv_length(args);
99 trim_args = g_new0(char *, num + 1);
100 for (i = 0, len = 0; i < num; i++) {
101 if (*args[i] != '\0') {
102 trim_args[len] = args[i];
111 err = __connmanctl_commands(connection, trim_args, len);
114 g_main_loop_quit(main_loop);
121 static gboolean input_handler(GIOChannel *channel, GIOCondition condition,
124 if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
125 g_main_loop_quit(main_loop);
129 if (readline_input_handler != NULL)
130 rl_callback_read_char();
134 static char **complete_agent(const char *text, int start, int end)
136 rl_attempted_completion_over = 1;
141 static char **complete_command(const char *text, int start, int end)
143 char **command = NULL;
145 rl_attempted_completion_over = 1;
148 command = rl_completion_matches(text,
149 __connmanctl_lookup_command);
154 void __connmanctl_agent_mode(const char *prompt,
155 connmanctl_input_func_t input_handler)
157 readline_input_handler = input_handler;
159 if (input_handler != NULL)
160 rl_callback_handler_install(prompt, input_handler);
162 rl_set_prompt(prompt);
163 rl_callback_handler_remove();
166 rl_attempted_completion_function = complete_agent;
169 void __connmanctl_command_mode(void)
171 readline_input_handler = rl_handler;
173 rl_callback_handler_install("connmanctl> ", rl_handler);
174 rl_attempted_completion_function = complete_command;
177 int __connmanctl_input_init(int argc, char *argv[])
188 dbus_error_init(&dbus_err);
189 connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &dbus_err);
191 if (dbus_error_is_set(&dbus_err)) {
192 fprintf(stderr, "Error: %s\n", dbus_err.message);
193 dbus_error_free(&dbus_err);
197 channel = g_io_channel_unix_new(fileno(stdin));
198 source = g_io_add_watch(channel, G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
199 input_handler, NULL);
200 g_io_channel_unref(channel);
205 __connmanctl_command_mode();
211 if (strcmp(argv[1], "--help") == 0 ||
212 strcmp(argv[1], "-h") == 0)
213 err = __connmanctl_commands(connection, help, 1);
215 err = __connmanctl_commands(connection, argv + 1,
219 if (err == -EINPROGRESS) {
220 main_loop = g_main_loop_new(NULL, FALSE);
221 g_main_loop_run(main_loop);
226 g_source_remove(source);
228 if (interactive == true) {
229 rl_callback_handler_remove();
233 dbus_connection_unref(connection);
234 if (main_loop != NULL)
235 g_main_loop_unref(main_loop);