client: Fix for handling an unset callback
[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 static connmanctl_input_func_t *readline_input_handler;
47
48 void __connmanctl_quit(void)
49 {
50         if (main_loop != NULL)
51                 g_main_loop_quit(main_loop);
52 }
53
54 bool __connmanctl_is_interactive(void)
55 {
56         return interactive;
57 }
58
59 void __connmanctl_save_rl(void)
60 {
61         save_input = !RL_ISSTATE(RL_STATE_DONE);
62
63         if (save_input) {
64                 saved_point = rl_point;
65                 saved_line = rl_copy_text(0, rl_end);
66                 rl_save_prompt();
67                 rl_replace_line("", 0);
68                 rl_redisplay();
69         }
70 }
71
72 void __connmanctl_redraw_rl(void)
73 {
74         if (save_input) {
75                 rl_restore_prompt();
76                 rl_replace_line(saved_line, 0);
77                 rl_point = saved_point;
78                 rl_redisplay();
79                 free(saved_line);
80         }
81
82         save_input = 0;
83 }
84
85 static void rl_handler(char *input)
86 {
87         char **args, **trim_args;
88         int num, len, err, i;
89
90         if (input == NULL) {
91                 rl_newline(1, '\n');
92                 g_main_loop_quit(main_loop);
93                 return;
94         }
95
96         args = g_strsplit(input, " ", 0);
97         num = g_strv_length(args);
98
99         trim_args = g_new0(char *, num);
100         for (i = 0, len = 0; i < num; i++) {
101                 if (*args[i] != '\0') {
102                         trim_args[len] = args[i];
103                         len++;
104                 }
105         }
106
107         if (len > 0) {
108
109                 add_history(input);
110
111                 err = __connmanctl_commands(connection, trim_args, len);
112
113                 if (err > 0)
114                         g_main_loop_quit(main_loop);
115         }
116
117         g_strfreev(args);
118         g_free(trim_args);
119 }
120
121 static gboolean input_handler(GIOChannel *channel, GIOCondition condition,
122                 gpointer user_data)
123 {
124         if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
125                 g_main_loop_quit(main_loop);
126                 return FALSE;
127         }
128
129         if (readline_input_handler != NULL)
130                 rl_callback_read_char();
131         return TRUE;
132 }
133
134 static char **complete_agent(const char *text, int start, int end)
135 {
136         rl_attempted_completion_over = 1;
137
138         return NULL;
139 }
140
141 static char **complete_command(const char *text, int start, int end)
142 {
143         char **command = NULL;
144
145         rl_attempted_completion_over = 1;
146
147         if (start == 0)
148                 command = rl_completion_matches(text,
149                                 __connmanctl_lookup_command);
150
151         return command;
152 }
153
154 void __connmanctl_agent_mode(const char *prompt,
155                 connmanctl_input_func_t input_handler)
156 {
157         readline_input_handler = input_handler;
158
159         if (input_handler != NULL)
160                 rl_callback_handler_install(prompt, input_handler);
161         else {
162                 rl_set_prompt(prompt);
163                 rl_callback_handler_remove();
164                 rl_redisplay();
165         }
166         rl_attempted_completion_function = complete_agent;
167 }
168
169 void __connmanctl_command_mode(void)
170 {
171         readline_input_handler = rl_handler;
172
173         rl_callback_handler_install("connmanctl> ", rl_handler);
174         rl_attempted_completion_function = complete_command;
175 }
176
177 int __connmanctl_input_init(int argc, char *argv[])
178 {
179         char *help[] = {
180                 "help",
181                 NULL
182         };
183         guint source = 0;
184         int err;
185         DBusError dbus_err;
186         GIOChannel *channel;
187
188         dbus_error_init(&dbus_err);
189         connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &dbus_err);
190
191         if (dbus_error_is_set(&dbus_err)) {
192                 fprintf(stderr, "Error: %s\n", dbus_err.message);
193                 dbus_error_free(&dbus_err);
194                 return 1;
195         }
196
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);
201
202         if (argc < 2) {
203                 interactive = true;
204
205                 __connmanctl_command_mode();
206                 err = -EINPROGRESS;
207
208         } else {
209                 interactive = false;
210
211                 if (strcmp(argv[1], "--help") == 0 ||
212                                 strcmp(argv[1], "-h") == 0)
213                         err = __connmanctl_commands(connection, help, 1);
214                 else
215                         err = __connmanctl_commands(connection, argv + 1,
216                                         argc -1);
217         }
218
219         if (err == -EINPROGRESS) {
220                 main_loop = g_main_loop_new(NULL, FALSE);
221                 g_main_loop_run(main_loop);
222
223                 err = 0;
224         }
225
226         g_source_remove(source);
227
228         if (interactive == true) {
229                 rl_callback_handler_remove();
230                 rl_message("");
231         }
232
233         dbus_connection_unref(connection);
234         if (main_loop != NULL)
235                 g_main_loop_unref(main_loop);
236
237         if (err < 0)
238                 err = -err;
239         else
240                 err = 0;
241
242         return err;
243 }