summaryrefslogtreecommitdiffstats
path: root/src/cli.c
blob: c853183de6dde12330eb01cb691bcb6386c1ebbb (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <dectmon.h>
#include <cli.h>

#define DECTMON_HISTFILE	".dectmon_history"

static struct event cli_event;
static char histfile[PATH_MAX];
static struct parser_state state;
static void *scanner;

void cli_display(const char *fmt, va_list ap)
{
	int point, end;

	point = rl_point;
	end   = rl_end;
	rl_point = rl_end = 0;
	rl_save_prompt();
	rl_clear_message();

	vfprintf(rl_outstream, fmt, ap);

	rl_restore_prompt();
	rl_point = point;
	rl_end   = end;
	rl_forced_update_display();
}

static void cli_read_callback(int fd, short mask, void *data)
{
	rl_callback_read_char();
}

static void cli_complete(char *line)
{
	HIST_ENTRY *hist;
	char *c;

	if (line == NULL)
		return;

	/* avoid empty lines in history */
	for (c = line; c != '\0'; c++) {
		if (!isspace(*c))
			break;
	}
	if (*c == '\0')
		return;

	/* avoid duplicate lines in history */
	hist = history_get(where_history());
	if (hist == NULL ||
	    ((where_history() != history_length - 1) ||
	     strcmp(hist->line, line)))
		add_history(line);

	rl_replace_line("", 1);
	scanner_push_buffer(scanner, line);
	yyparse(scanner, &state);
	rl_crlf();
	free(line);
}

static const char *keywords[] = {
	"debug",
	"cluster",
	"portable",
	"tbc",
	"show",
	"set",
	"on",
	"off",
	"lce",
	"cc",
	"ss",
	"mm",
	"clms",
	"llme",
	"MNCC_SETUP-req",
	"MNCC_INFO-req",
	"MNSS_FACILITY-req",
	"portable-identity",
	"sending-complete",
	"keypad",
	"basic-service",
	"escape-to-proprietary",
	"IPEI",
	"info",
	"service",
	"class",
	"LIA",
	"message",
	"dect/isdn",
	"normal",
	"internal",
	"emergency",
	"external-handover",
	"QA&M",
	"basic-speech",
	"GSM",
	"UMTS",
	"LRMS",
	"GSM-SMS",
	"wideband-speech",
	"SUOTA-class-4",
	"SUOTA-class-3",
	"other",
	"emc",
	"content",
	NULL
};

static char *cli_command_generator(const char *text, int state)
{
	static unsigned int idx, len;
	const char *name;

	if (state == 0) {
		idx = 0;
		len = strlen(text);
	}

	while ((name = keywords[idx]) != NULL) {
		idx++;
		if (!strncasecmp(name, text, len))
			return strdup(name);
	}
	return NULL;
}

static char **cli_completion(const char *text, int start, int end)
{
	return rl_completion_matches(text, cli_command_generator);
}

int cli_init(FILE *file)
{
	const char *prompt = NULL;
	const char *home;
	int fd;

	parser_init(&state);
	scanner = scanner_init(&state);

	rl_instream = file;
	fd = fileno(rl_instream);
	rl_readline_name = "dectmon";
	if (isatty(fd))
		prompt = "dectmon > ";

	rl_outstream = stdout;

	rl_callback_handler_install(prompt, cli_complete);
	rl_attempted_completion_function = cli_completion;

	home = getenv("HOME");
	if (home == NULL)
		home = ".";
	snprintf(histfile, sizeof(histfile), "%s/%s", home, DECTMON_HISTFILE);
	read_history(histfile);
	history_set_pos(history_length);

	event_set(&cli_event, fd, EV_READ | EV_PERSIST, cli_read_callback, NULL);
	event_add(&cli_event, NULL);

	return 0;
}

void cli_exit(void)
{
	rl_callback_handler_remove();
	rl_deprep_terminal();
	write_history(histfile);
}