8d6ecca7bf8e4f7a1efc3d19b50ee6b64e21507e
[platform/upstream/connman.git] / client / input.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012-2013  Intel Corporation. All rights reserved.
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <glib.h>
31 #include <readline/readline.h>
32 #include <readline/history.h>
33
34 #include <gdbus.h>
35 #include "input.h"
36 #include "commands.h"
37
38 static DBusConnection *connection;
39 static GMainLoop *main_loop;
40 static bool interactive = false;
41
42 static bool save_input;
43 static char *saved_line;
44 static int saved_point;
45
46 void __connmanctl_quit(void)
47 {
48         if (main_loop != NULL)
49                 g_main_loop_quit(main_loop);
50 }
51
52 bool __connmanctl_is_interactive(void)
53 {
54         return interactive;
55 }
56
57 void __connmanctl_save_rl(void)
58 {
59         save_input = !RL_ISSTATE(RL_STATE_DONE);
60
61         if (save_input) {
62                 saved_point = rl_point;
63                 saved_line = rl_copy_text(0, rl_end);
64                 rl_save_prompt();
65                 rl_replace_line("", 0);
66                 rl_redisplay();
67         }
68 }
69
70 void __connmanctl_redraw_rl(void)
71 {
72         if (save_input) {
73                 rl_restore_prompt();
74                 rl_replace_line(saved_line, 0);
75                 rl_point = saved_point;
76                 rl_redisplay();
77                 free(saved_line);
78         }
79
80         save_input = 0;
81 }
82
83 static void rl_handler(char *input)
84 {
85         char **args, **trim_args;
86         int num, len, err, i;
87
88         if (input == NULL) {
89                 rl_newline(1, '\n');
90                 g_main_loop_quit(main_loop);
91                 return;
92         }
93
94         args = g_strsplit(input, " ", 0);
95         num = g_strv_length(args);
96
97         trim_args = g_new0(char *, num + 1);
98         for (i = 0, len = 0; i < num; i++) {
99                 if (*args[i] != '\0') {
100                         trim_args[len] = args[i];
101                         len++;
102                 }
103         }
104
105         if (len > 0) {
106
107                 add_history(input);
108
109                 err = __connmanctl_commands(connection, trim_args, len);
110
111                 if (err > 0)
112                         g_main_loop_quit(main_loop);
113         }
114
115         g_strfreev(args);
116         g_free(trim_args);
117 }
118
119 static gboolean input_handler(GIOChannel *channel, GIOCondition condition,
120                 gpointer user_data)
121 {
122         if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
123                 g_main_loop_quit(main_loop);
124                 return FALSE;
125         }
126
127         rl_callback_read_char();
128         return TRUE;
129 }
130
131 static char **complete_agent(const char *text, int start, int end)
132 {
133         rl_attempted_completion_over = 1;
134
135         return NULL;
136 }
137
138 static char **complete_command(const char *text, int start, int end)
139 {
140         char **command = NULL;
141
142         rl_attempted_completion_over = 1;
143
144         if (start == 0)
145                 command = rl_completion_matches(text,
146                                 __connmanctl_lookup_command);
147
148         return command;
149 }
150
151 static struct {
152         connmanctl_input_func_t cb;
153         void *user_data;
154 } agent_handler;
155
156 static void rl_agent_handler(char *input)
157 {
158         agent_handler.cb(input, agent_handler.user_data);
159 }
160
161 void __connmanctl_agent_mode(const char *prompt,
162                 connmanctl_input_func_t input_handler, void *user_data)
163 {
164         agent_handler.cb = input_handler;
165         agent_handler.user_data = user_data;
166
167         if (input_handler != NULL)
168                 rl_callback_handler_install(prompt, rl_agent_handler);
169         else {
170                 rl_set_prompt(prompt);
171                 rl_callback_handler_remove();
172                 rl_redisplay();
173         }
174         rl_attempted_completion_function = complete_agent;
175 }
176
177 void __connmanctl_command_mode(void)
178 {
179         rl_callback_handler_install("connmanctl> ", rl_handler);
180         rl_attempted_completion_function = complete_command;
181 }
182
183 int __connmanctl_input_init(int argc, char *argv[])
184 {
185         char *help[] = {
186                 "help",
187                 NULL
188         };
189         guint source = 0;
190         int err;
191         DBusError dbus_err;
192         GIOChannel *channel;
193
194         dbus_error_init(&dbus_err);
195         connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &dbus_err);
196
197         if (dbus_error_is_set(&dbus_err)) {
198                 fprintf(stderr, "Error: %s\n", dbus_err.message);
199                 dbus_error_free(&dbus_err);
200                 return 1;
201         }
202
203         channel = g_io_channel_unix_new(fileno(stdin));
204         source = g_io_add_watch(channel, G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
205                         input_handler, NULL);
206         g_io_channel_unref(channel);
207
208         if (argc < 2) {
209                 interactive = true;
210
211                 __connmanctl_command_mode();
212                 err = -EINPROGRESS;
213
214         } else {
215                 interactive = false;
216
217                 if (strcmp(argv[1], "--help") == 0 ||
218                                 strcmp(argv[1], "-h") == 0)
219                         err = __connmanctl_commands(connection, help, 1);
220                 else
221                         err = __connmanctl_commands(connection, argv + 1,
222                                         argc -1);
223         }
224
225         if (err == -EINPROGRESS) {
226                 main_loop = g_main_loop_new(NULL, FALSE);
227                 g_main_loop_run(main_loop);
228
229                 err = 0;
230         }
231
232         g_source_remove(source);
233
234         if (interactive == true) {
235                 rl_callback_handler_remove();
236                 rl_message("");
237         }
238
239         dbus_connection_unref(connection);
240         if (main_loop != NULL)
241                 g_main_loop_unref(main_loop);
242
243         if (err < 0)
244                 err = -err;
245         else
246                 err = 0;
247
248         return err;
249 }