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)
96 g_main_loop_quit(main_loop);
102 args = g_strsplit(input, " ", 0);
103 num = g_strv_length(args);
105 err = commands(connection, args, num);
110 g_main_loop_quit(main_loop);
113 static gboolean input_handler(GIOChannel *channel, GIOCondition condition,
116 if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
117 g_main_loop_quit(main_loop);
121 rl_callback_read_char();
125 int __connmanctl_input_init(int argc, char *argv[])
135 dbus_error_init(&dbus_err);
136 connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &dbus_err);
138 if (dbus_error_is_set(&dbus_err)) {
139 fprintf(stderr, "Error: %s\n", dbus_err.message);
140 dbus_error_free(&dbus_err);
149 channel = g_io_channel_unix_new(fileno(stdin));
150 source = g_io_add_watch(channel,
151 G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
152 input_handler, NULL);
153 g_io_channel_unref(channel);
155 rl_callback_handler_install("connmanctl> ", rl_handler);
161 if (strcmp(argv[1], "--help") == 0 ||
162 strcmp(argv[1], "-h") == 0)
163 err = commands(connection, help, 1);
165 err = commands(connection, argv + 1, argc -1);
168 if (err == -EINPROGRESS) {
169 main_loop = g_main_loop_new(NULL, FALSE);
170 g_main_loop_run(main_loop);
173 g_source_remove(source);
178 if (interactive == true) {
179 rl_callback_handler_remove();
183 dbus_connection_unref(connection);
184 if (main_loop != NULL)
185 g_main_loop_unref(main_loop);