summaryrefslogtreecommitdiff
path: root/client/interactive.c
blob: 200f3dcb611af15001117492cf6b394c3997165d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <glib.h>
#include <gdbus.h>

#include "services.h"
#include "technology.h"
#include "data_manager.h"
#include "monitor.h"
#include "interactive.h"

static DBusConnection *interactive_conn;

static gboolean rl_handler(char *input)
{
	char **long_args = NULL;
	int num_args, error;
	num_args = 0;

	if (input == NULL) {
		rl_newline(1, '\n');
		exit(EXIT_FAILURE);
	}

	add_history(input);
	long_args = g_strsplit(input, " ", 0);

	if (long_args == NULL || long_args[0] == NULL) {
		g_strfreev(long_args);
		free(input);
		return FALSE;
	}

	for (num_args = 0; long_args[num_args] != NULL; num_args++);

	error = commands(interactive_conn, long_args, num_args);
	if (error == -1) {
		error = commands_no_options(interactive_conn, long_args,
				num_args);
		if (error == -1)
			error = commands_options(interactive_conn, long_args,
					num_args);
		else
			return error;
	}

	if ((strcmp(long_args[0], "quit") == 0)
					|| (strcmp(long_args[0], "exit") == 0)
					|| (strcmp(long_args[0], "q") == 0)) {
		g_strfreev(long_args);
		exit(EXIT_SUCCESS);
	}
	if (error == -1) {
		fprintf(stderr, "%s is not a valid command, check help.\n",
			long_args[0]);
	}

	g_strfreev(long_args);
	optind = 0;

	return TRUE;
}

static gboolean readmonitor(GIOChannel *channel, GIOCondition condition,
							gpointer user_data){
	if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
		g_io_channel_unref(channel);
		return FALSE;
	}
	rl_callback_read_char();
	return TRUE;
}

void show_interactive(DBusConnection *connection, GMainLoop *mainloop)
{
	GIOChannel *gchan;
	int events;
	gchan = g_io_channel_unix_new(fileno(stdin));
	events = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
	interactive_conn = connection;

	while (TRUE) {
		g_io_add_watch(gchan, events, readmonitor, NULL);
		rl_callback_handler_install("connmanctl> ", (void *)rl_handler);
		g_main_loop_run(mainloop);

		rl_callback_handler_remove();
		g_io_channel_unref(gchan);
	}
}