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 if (interactive == false)
62 save_input = !RL_ISSTATE(RL_STATE_DONE);
65 saved_point = rl_point;
66 saved_line = rl_copy_text(0, rl_end);
68 rl_replace_line("", 0);
73 void __connmanctl_redraw_rl(void)
75 if (interactive == false)
80 rl_replace_line(saved_line, 0);
81 rl_point = saved_point;
89 static void rl_handler(char *input)
91 char **args, **trim_args;
96 g_main_loop_quit(main_loop);
100 args = g_strsplit(input, " ", 0);
101 num = g_strv_length(args);
103 trim_args = g_new0(char *, num);
104 for (i = 0, len = 0; i < num; i++) {
105 if (*args[i] != '\0') {
106 trim_args[len] = args[i];
115 err = __connmanctl_commands(connection, trim_args, len);
118 g_main_loop_quit(main_loop);
125 static gboolean input_handler(GIOChannel *channel, GIOCondition condition,
128 if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
129 g_main_loop_quit(main_loop);
133 rl_callback_read_char();
137 static char **complete_command(const char *text, int start, int end)
139 char **command = NULL;
141 rl_attempted_completion_over = 1;
144 command = rl_completion_matches(text,
145 __connmanctl_lookup_command);
150 int __connmanctl_input_init(int argc, char *argv[])
160 dbus_error_init(&dbus_err);
161 connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &dbus_err);
163 if (dbus_error_is_set(&dbus_err)) {
164 fprintf(stderr, "Error: %s\n", dbus_err.message);
165 dbus_error_free(&dbus_err);
174 channel = g_io_channel_unix_new(fileno(stdin));
175 source = g_io_add_watch(channel,
176 G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
177 input_handler, NULL);
178 g_io_channel_unref(channel);
180 rl_callback_handler_install("connmanctl> ", rl_handler);
181 rl_attempted_completion_function = complete_command;
187 if (strcmp(argv[1], "--help") == 0 ||
188 strcmp(argv[1], "-h") == 0)
189 err = __connmanctl_commands(connection, help, 1);
191 err = __connmanctl_commands(connection, argv + 1,
195 if (err == -EINPROGRESS) {
196 main_loop = g_main_loop_new(NULL, FALSE);
197 g_main_loop_run(main_loop);
200 g_source_remove(source);
205 if (interactive == true) {
206 rl_callback_handler_remove();
210 dbus_connection_unref(connection);
211 if (main_loop != NULL)
212 g_main_loop_unref(main_loop);