summaryrefslogtreecommitdiffstats
path: root/src/cli.c
blob: 11fc85abeaa2bd1b9df3fb61ce7f9079c6fb228d (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Asynchronous readline-based interactive interface
 *
 * Actually not asynchronous so far, but intended to be.
 *
 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Development of this code funded by Astaro AG (http://www.astaro.com/)
 */

#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#include <readline/history.h>
#elif defined(HAVE_LIBEDIT)
#include <editline/readline.h>
#else
#include <linenoise.h>
#endif

#include <cli.h>
#include <list.h>

#define CMDLINE_HISTFILE	".nft.history"
#define CMDLINE_PROMPT		"nft> "
#define CMDLINE_QUIT		"quit"

static char histfile[PATH_MAX];

static void
init_histfile(void)
{
	const char *home;

	home = getenv("HOME");
	if (home == NULL)
		home = "";
	snprintf(histfile, sizeof(histfile), "%s/%s", home, CMDLINE_HISTFILE);
}

#if defined(HAVE_LIBREADLINE)
static void nft_rl_prompt_save(void)
{
	rl_save_prompt();
	rl_clear_message();
	rl_set_prompt(".... ");
}
#define nft_rl_prompt_restore rl_restore_prompt
#elif defined(HAVE_LIBEDIT)
static void nft_rl_prompt_save(void)
{
	rl_set_prompt(".... ");
}
static void nft_rl_prompt_restore(void)
{
	rl_set_prompt("nft> ");
}
#endif

#if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT)

static struct nft_ctx *cli_nft;
static char *multiline;

static char *cli_append_multiline(char *line)
{
	size_t len = strlen(line);
	bool complete = false;
	char *s;

	if (len == 0)
		return NULL;

	if (line[len - 1] == '\\') {
		line[len - 1] = '\0';
		len--;
	} else if (multiline == NULL)
		return line;
	else
		complete = 1;

	if (multiline == NULL) {
		multiline = line;
		nft_rl_prompt_save();
	} else {
		len += strlen(multiline);
		s = malloc(len + 1);
		if (!s) {
			fprintf(stderr, "%s:%u: Memory allocation failure\n",
				__FILE__, __LINE__);
			cli_exit();
			exit(EXIT_FAILURE);
		}
		snprintf(s, len + 1, "%s%s", multiline, line);
		free(multiline);
		multiline = s;
	}
	line = NULL;

	if (complete) {
		line = multiline;
		multiline = NULL;
		nft_rl_prompt_restore();
	}
	return line;
}

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

	if (line == NULL) {
		printf("\n");
		cli_exit();
		exit(0);
	}

	line = cli_append_multiline(line);
	if (line == NULL)
		return;

	for (c = line; *c != '\0'; c++)
		if (!isspace(*c))
			break;
	if (*c == '\0')
		return;

	if (!strcmp(line, CMDLINE_QUIT)) {
		cli_exit();
		exit(0);
	}

	/* avoid duplicate history entries */
	hist = history_get(history_length);
	if (hist == NULL || strcmp(hist->line, line))
		add_history(line);

	nft_run_cmd_from_buffer(cli_nft, line);
	free(line);
}
#endif

#if defined(HAVE_LIBREADLINE)

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

int cli_init(struct nft_ctx *nft)
{
	cli_nft = nft;
	rl_readline_name = (char *)"nft";
	rl_instream  = stdin;
	rl_outstream = stdout;

	rl_callback_handler_install(CMDLINE_PROMPT, cli_complete);
	rl_attempted_completion_function = cli_completion;

	init_histfile();

	read_history(histfile);
	history_set_pos(history_length);

	while (true)
		rl_callback_read_char();
	return 0;
}

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

#elif defined(HAVE_LIBEDIT)

int cli_init(struct nft_ctx *nft)
{
	char *line;

	cli_nft = nft;
	rl_readline_name = (char *)"nft";
	rl_instream  = stdin;
	rl_outstream = stdout;

	init_histfile();

	read_history(histfile);
	history_set_pos(history_length);

	rl_set_prompt(CMDLINE_PROMPT);
	while ((line = readline(rl_prompt)) != NULL) {
		line = cli_append_multiline(line);
		if (!line)
			continue;

		cli_complete(line);
	}
	cli_exit();

	return 0;
}

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

#else /* HAVE_LINENOISE */

int cli_init(struct nft_ctx *nft)
{
	int quit = 0;
	char *line;

	init_histfile();
	linenoiseHistoryLoad(histfile);
	linenoiseSetMultiLine(1);

	while (!quit && (line = linenoise(CMDLINE_PROMPT)) != NULL) {
		if (strcmp(line, CMDLINE_QUIT) == 0) {
			quit = 1;
		} else if (line[0] != '\0') {
			linenoiseHistoryAdd(line);
			nft_run_cmd_from_buffer(nft, line);
		}
		linenoiseFree(line);
	}
	cli_exit();
	exit(0);
}

void cli_exit(void)
{
	linenoiseHistorySave(histfile);
}

#endif /* HAVE_LINENOISE */