5 * Copyright (C) 2012 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>
36 #include <sys/types.h>
44 #include "technology.h"
45 #include "data_manager.h"
47 #include "interactive.h"
49 static DBusConnection *interactive_conn;
51 static gboolean rl_handler(char *input)
53 char **long_args = NULL;
63 long_args = g_strsplit(input, " ", 0);
65 if (long_args == NULL || long_args[0] == NULL) {
66 g_strfreev(long_args);
71 for (num_args = 0; long_args[num_args] != NULL; num_args++);
73 error = commands(interactive_conn, long_args, num_args);
75 error = commands_no_options(interactive_conn, long_args,
78 error = commands_options(interactive_conn, long_args,
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);
91 fprintf(stderr, "%s is not a valid command, check help.\n",
95 g_strfreev(long_args);
101 static gboolean readmonitor(GIOChannel *channel, GIOCondition condition,
103 if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
104 g_io_channel_unref(channel);
107 rl_callback_read_char();
111 void show_interactive(DBusConnection *connection, GMainLoop *mainloop)
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;
120 g_io_add_watch(gchan, events, readmonitor, NULL);
121 rl_callback_handler_install("connmanctl> ", (void *)rl_handler);
122 g_main_loop_run(mainloop);
124 rl_callback_handler_remove();
125 g_io_channel_unref(gchan);