GCC Code Coverage Report


Directory: ./
File: src/cli.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 48 81 59.3%
Functions: 5 7 71.4%
Branches: 15 32 46.9%

Line Branch Exec Source
1 /*
2 * Asynchronous readline-based interactive interface
3 *
4 * Actually not asynchronous so far, but intended to be.
5 *
6 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Development of this code funded by Astaro AG (http://www.astaro.com/)
13 */
14
15 #include <nft.h>
16
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #ifdef HAVE_LIBREADLINE
24 #include <readline/readline.h>
25 #include <readline/history.h>
26 #elif defined(HAVE_LIBEDIT)
27 #include <editline/readline.h>
28 #else
29 #include <linenoise.h>
30 #endif
31
32 #include <cli.h>
33 #include <list.h>
34
35 #define CMDLINE_HISTFILE ".nft.history"
36 #define CMDLINE_PROMPT "nft> "
37 #define CMDLINE_QUIT "quit"
38
39 static bool cli_quit;
40 static int cli_rc;
41
42 − static void __cli_exit(int rc)
43 {
44 − cli_quit = true;
45 − cli_rc = rc;
46 − }
47
48 static char histfile[PATH_MAX];
49
50 static void
51 21 init_histfile(void)
52 {
53 const char *home;
54
55 21 home = getenv("HOME");
56
1/2
✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 21 times.
21 if (home == NULL)
57 ✗ home = "";
58 21 snprintf(histfile, sizeof(histfile), "%s/%s", home, CMDLINE_HISTFILE);
59 21 }
60
61 #if defined(HAVE_LIBREADLINE)
62 ✗ static void nft_rl_prompt_save(void)
63 {
64 ✗ rl_save_prompt();
65 ✗ rl_clear_message();
66 ✗ rl_set_prompt(".... ");
67 ✗ }
68 #define nft_rl_prompt_restore rl_restore_prompt
69 #elif defined(HAVE_LIBEDIT)
70 static void nft_rl_prompt_save(void)
71 {
72 rl_set_prompt(".... ");
73 }
74 static void nft_rl_prompt_restore(void)
75 {
76 rl_set_prompt("nft> ");
77 }
78 #endif
79
80 #if defined(HAVE_LIBREADLINE) || defined(HAVE_LIBEDIT)
81
82 static struct nft_ctx *cli_nft;
83 static char *multiline;
84
85 68 static char *cli_append_multiline(char *line)
86 {
87 68 size_t len = strlen(line);
88 68 bool complete = false;
89 char *s;
90
91
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 68 times.
68 if (len == 0)
92 ✗ return NULL;
93
94
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 68 times.
68 if (line[len - 1] == '\\') {
95 ✗ line[len - 1] = '\0';
96 ✗ len--;
97
1/2
✓ Branch 0 (6→7) taken 68 times.
✗ Branch 1 (6→8) not taken.
68 } else if (multiline == NULL)
98 68 return line;
99 else
100 ✗ complete = 1;
101
102 ✗ if (multiline == NULL) {
103 ✗ multiline = line;
104 ✗ nft_rl_prompt_save();
105 } else {
106 ✗ len += strlen(multiline);
107 ✗ s = malloc(len + 1);
108 ✗ if (!s) {
109 ✗ fprintf(stderr, "%s:%u: Memory allocation failure\n",
110 __FILE__, __LINE__);
111 ✗ cli_exit(EXIT_FAILURE);
112 ✗ return NULL;
113 }
114 ✗ snprintf(s, len + 1, "%s%s", multiline, line);
115 ✗ free(multiline);
116 ✗ multiline = s;
117 }
118 ✗ line = NULL;
119
120 ✗ if (complete) {
121 ✗ line = multiline;
122 ✗ multiline = NULL;
123 ✗ nft_rl_prompt_restore();
124 }
125 ✗ return line;
126 }
127
128 89 static void cli_complete(char *line)
129 {
130 const HIST_ENTRY *hist;
131 const char *c;
132 89 LIST_HEAD(msgs);
133
134
2/2
✓ Branch 0 (2→3) taken 21 times.
✓ Branch 1 (2→6) taken 68 times.
89 if (line == NULL) {
135 21 printf("\n");
136 21 return cli_exit(0);
137 }
138
139 68 line = cli_append_multiline(line);
140
1/2
✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→9) taken 68 times.
68 if (line == NULL)
141 ✗ return;
142
143
1/2
✓ Branch 0 (13→10) taken 68 times.
✗ Branch 1 (13→14) not taken.
68 for (c = line; *c != '\0'; c++)
144
1/2
✓ Branch 0 (10→11) taken 68 times.
✗ Branch 1 (10→12) not taken.
68 if (!isspace(*c))
145 68 break;
146
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→16) taken 68 times.
68 if (*c == '\0')
147 ✗ return;
148
149
1/2
✗ Branch 0 (16→17) not taken.
✓ Branch 1 (16→19) taken 68 times.
68 if (!strcmp(line, CMDLINE_QUIT))
150 ✗ return cli_exit(0);
151
152 /* avoid duplicate history entries */
153 68 hist = history_get(history_length);
154
2/4
✓ Branch 0 (20→21) taken 68 times.
✗ Branch 1 (20→22) not taken.
✓ Branch 2 (21→22) taken 68 times.
✗ Branch 3 (21→23) not taken.
68 if (hist == NULL || strcmp(hist->line, line))
155 68 add_history(line);
156
157 68 nft_run_cmd_from_buffer(cli_nft, line);
158 68 free(line);
159 }
160 #endif
161
162 #if defined(HAVE_LIBREADLINE)
163
164 ✗ static char **cli_completion(const char *text, int start, int end)
165 {
166 ✗ return NULL;
167 }
168
169 21 int cli_init(struct nft_ctx *nft)
170 {
171 21 cli_nft = nft;
172 21 rl_readline_name = (char *)"nft";
173 21 rl_instream = stdin;
174 21 rl_outstream = stdout;
175
176 21 rl_callback_handler_install(CMDLINE_PROMPT, cli_complete);
177 21 rl_attempted_completion_function = cli_completion;
178
179 21 init_histfile();
180
181 21 read_history(histfile);
182 21 history_set_pos(history_length);
183
184
2/2
✓ Branch 0 (8→7) taken 89 times.
✓ Branch 1 (8→9) taken 21 times.
110 while (!cli_quit)
185 89 rl_callback_read_char();
186
187 21 return cli_rc;
188 }
189
190 21 void cli_exit(int rc)
191 {
192 21 rl_callback_handler_remove();
193 21 rl_deprep_terminal();
194 21 write_history(histfile);
195
196 21 __cli_exit(rc);
197 21 }
198
199 #elif defined(HAVE_LIBEDIT)
200
201 int cli_init(struct nft_ctx *nft)
202 {
203 char *line;
204
205 cli_nft = nft;
206 rl_readline_name = (char *)"nft";
207 rl_instream = stdin;
208 rl_outstream = stdout;
209
210 init_histfile();
211
212 read_history(histfile);
213 history_set_pos(history_length);
214
215 rl_set_prompt(CMDLINE_PROMPT);
216 while (!cli_quit) {
217 line = readline(rl_prompt);
218 if (!line) {
219 cli_exit(0);
220 break;
221 }
222 line = cli_append_multiline(line);
223 if (!line)
224 continue;
225
226 cli_complete(line);
227 }
228
229 return cli_rc;
230 }
231
232 void cli_exit(int rc)
233 {
234 rl_deprep_terminal();
235 write_history(histfile);
236
237 __cli_exit(rc);
238 }
239
240 #else /* HAVE_LINENOISE */
241
242 int cli_init(struct nft_ctx *nft)
243 {
244 char *line;
245
246 init_histfile();
247 linenoiseHistoryLoad(histfile);
248 linenoiseSetMultiLine(1);
249
250 while (!cli_quit) {
251 line = linenoise(CMDLINE_PROMPT);
252 if (!line) {
253 cli_exit(0);
254 break;
255 }
256 if (strcmp(line, CMDLINE_QUIT) == 0) {
257 cli_exit(0);
258 } else if (line[0] != '\0') {
259 linenoiseHistoryAdd(line);
260 nft_run_cmd_from_buffer(nft, line);
261 }
262 linenoiseFree(line);
263 }
264
265 return cli_rc;
266 }
267
268 void cli_exit(int rc)
269 {
270 linenoiseHistorySave(histfile);
271
272 __cli_exit(rc);
273 }
274
275 #endif /* HAVE_LINENOISE */
276