client: Factor out connect command
[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 "monitor.h"
47 #include "interactive.h"
48
49 static DBusConnection *interactive_conn;
50
51 static gboolean rl_handler(char *input)
52 {
53         char **long_args = NULL;
54         int num_args, error;
55         num_args = 0;
56
57         if (input == NULL) {
58                 rl_newline(1, '\n');
59                 exit(EXIT_FAILURE);
60         }
61
62         add_history(input);
63         long_args = g_strsplit(input, " ", 0);
64
65         if (long_args == NULL || long_args[0] == NULL) {
66                 g_strfreev(long_args);
67                 free(input);
68                 return FALSE;
69         }
70
71         for (num_args = 0; long_args[num_args] != NULL; num_args++);
72
73         error = commands(interactive_conn, long_args, num_args);
74         if (error == -1) {
75                 error = commands_no_options(interactive_conn, long_args,
76                                 num_args);
77                 if (error == -1)
78                         error = commands_options(interactive_conn, long_args,
79                                         num_args);
80                 else
81                         return error;
82         }
83
84         if ((strcmp(long_args[0], "quit") == 0)
85                                         || (strcmp(long_args[0], "exit") == 0)
86                                         || (strcmp(long_args[0], "q") == 0)) {
87                 g_strfreev(long_args);
88                 exit(EXIT_SUCCESS);
89         }
90         if (error == -1) {
91                 fprintf(stderr, "%s is not a valid command, check help.\n",
92                         long_args[0]);
93         }
94
95         g_strfreev(long_args);
96         optind = 0;
97
98         return TRUE;
99 }
100
101 static gboolean readmonitor(GIOChannel *channel, GIOCondition condition,
102                                                         gpointer user_data){
103         if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
104                 g_io_channel_unref(channel);
105                 return FALSE;
106         }
107         rl_callback_read_char();
108         return TRUE;
109 }
110
111 void show_interactive(DBusConnection *connection, GMainLoop *mainloop)
112 {
113         GIOChannel *gchan;
114         int events;
115         gchan = g_io_channel_unix_new(fileno(stdin));
116         events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
117         interactive_conn = connection;
118
119         while (TRUE) {
120                 g_io_add_watch(gchan, events, readmonitor, NULL);
121                 rl_callback_handler_install("connmanctl> ", (void *)rl_handler);
122                 g_main_loop_run(mainloop);
123
124                 rl_callback_handler_remove();
125                 g_io_channel_unref(gchan);
126         }
127 }