7989a01318946998b7148c6301f30c88d34fc14d
[platform/upstream/connman.git] / client / interactive.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  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 <string.h>
30 #include <errno.h>
31 #include <readline/readline.h>
32 #include <readline/history.h>
33 #include <getopt.h>
34 #include <unistd.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39
40 #include <glib.h>
41 #include <gdbus.h>
42
43 #include "services.h"
44 #include "technology.h"
45 #include "data_manager.h"
46 #include "interactive.h"
47
48 static DBusConnection *interactive_conn;
49
50 static gboolean rl_handler(char *input)
51 {
52         char **long_args = NULL;
53         int num_args, error;
54         num_args = 0;
55
56         if (input == NULL) {
57                 rl_newline(1, '\n');
58                 exit(EXIT_FAILURE);
59         }
60
61         add_history(input);
62         long_args = g_strsplit(input, " ", 0);
63
64         if (long_args == NULL || long_args[0] == NULL) {
65                 g_strfreev(long_args);
66                 free(input);
67                 return FALSE;
68         }
69
70         for (num_args = 0; long_args[num_args] != NULL; num_args++);
71
72         error = commands(interactive_conn, long_args, num_args);
73
74         if ((strcmp(long_args[0], "quit") == 0)
75                                         || (strcmp(long_args[0], "exit") == 0)
76                                         || (strcmp(long_args[0], "q") == 0)) {
77                 g_strfreev(long_args);
78                 exit(EXIT_SUCCESS);
79         }
80         if (error == -1) {
81                 fprintf(stderr, "%s is not a valid command, check help.\n",
82                         long_args[0]);
83         }
84
85         g_strfreev(long_args);
86         optind = 0;
87
88         return TRUE;
89 }
90
91 static gboolean readmonitor(GIOChannel *channel, GIOCondition condition,
92                                                         gpointer user_data){
93         if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
94                 g_io_channel_unref(channel);
95                 return FALSE;
96         }
97         rl_callback_read_char();
98         return TRUE;
99 }
100
101 void show_interactive(DBusConnection *connection, GMainLoop *mainloop)
102 {
103         GIOChannel *gchan;
104         int events;
105         gchan = g_io_channel_unix_new(fileno(stdin));
106         events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
107         interactive_conn = connection;
108
109         while (TRUE) {
110                 g_io_add_watch(gchan, events, readmonitor, NULL);
111                 rl_callback_handler_install("connmanctl> ", (void *)rl_handler);
112                 g_main_loop_run(mainloop);
113
114                 rl_callback_handler_remove();
115                 g_io_channel_unref(gchan);
116         }
117 }