Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #if !HAVE_READLINE
21 #include <stdio.h>
22 #include <string.h>
24 char *
25 readline(const char *prompt)
26 {
27 char *ch, *line = NULL;
28 size_t linesize = 0;
29 ssize_t linelen;
31 printf("%s", prompt);
32 fflush(stdout);
34 linelen = getline(&line, &linesize, stdin);
35 if (linelen == -1)
36 return NULL;
38 if ((ch = strchr(line, '\n')) != NULL)
39 *ch = '\0';
40 return line;
41 }
43 void
44 add_history(const char *line)
45 {
46 return;
47 }
49 void
50 compl_setup(void)
51 {
52 return;
53 }
55 #else /* HAVE_READLINE */
57 #include <ctype.h>
58 #include <limits.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
63 #include <readline/readline.h>
64 #include <readline/history.h>
66 #include "kami.h"
67 #include "kamiftp.h"
69 struct compl_state {
70 size_t size;
71 size_t len;
72 char **entries;
73 };
75 static struct compl_state compl_state;
76 static char compl_prfx[PATH_MAX];
78 static void
79 compl_state_reset(void)
80 {
81 size_t i;
83 for (i = 0; i < compl_state.len; ++i)
84 free(compl_state.entries[i]);
85 free(compl_state.entries);
87 memset(&compl_state, 0, sizeof(compl_state));
88 }
90 static int
91 compl_add_entry(const struct np_stat *st)
92 {
93 const char *sufx = "";
94 char *dup;
95 int r;
97 if (compl_state.len == compl_state.size) {
98 size_t newsz = compl_state.size * 1.5;
99 void *t;
101 if (newsz == 0)
102 newsz = 16;
104 /* one for the NULL entry at the end */
105 t = recallocarray(compl_state.entries, compl_state.size,
106 newsz + 1, sizeof(char *));
107 if (t == NULL)
108 return -1;
109 compl_state.entries = t;
110 compl_state.size = newsz;
113 if (st->qid.type & QTDIR)
114 sufx = "/";
116 if (asprintf(&dup, "%s%s%s", compl_prfx, st->name, sufx) == -1)
117 return -1;
118 compl_state.entries[compl_state.len++] = dup;
119 return 0;
122 static void
123 cleanword(char *buf, int brkspc)
125 char *cmd;
126 int escape, quote;
128 while (brkspc && isspace((unsigned char)*buf))
129 memmove(buf, buf + 1, strlen(buf));
131 escape = quote = 0;
132 for (cmd = buf; *cmd != '\0'; ++cmd) {
133 if (escape) {
134 escape = 0;
135 continue;
137 if (*cmd == '\\')
138 goto skip;
139 if (*cmd == quote) {
140 quote = 0;
141 goto skip;
143 if (*cmd == '\'' || *cmd == '"') {
144 quote = *cmd;
145 goto skip;
147 if (quote)
148 continue;
149 if (brkspc && isspace((unsigned char)*cmd))
150 break;
151 continue;
153 skip:
154 memmove(cmd, cmd + 1, strlen(cmd));
155 cmd--;
157 *cmd = '\0';
160 static int
161 tellcmd(char *buf)
163 size_t i;
165 cleanword(buf, 1);
166 for (i = 0; i < nitems(cmds); ++i) {
167 if (!strcmp(cmds[i].name, buf))
168 return cmds[i].cmdtype;
171 return CMD_UNKNOWN;
174 static int
175 tell_argno(const char *cmd, int *cmdtype)
177 char cmd0[64]; /* plenty of space */
178 const char *start = cmd;
179 int escape, quote;
180 int argno = 0;
182 *cmdtype = CMD_UNKNOWN;
184 /* find which argument needs to be completed */
185 while (*cmd) {
186 while (isspace((unsigned char)*cmd))
187 cmd++;
188 if (*cmd == '\0')
189 break;
191 escape = quote = 0;
192 for (; *cmd; ++cmd) {
193 if (escape) {
194 escape = 0;
195 continue;
197 if (*cmd == '\\') {
198 escape = 1;
199 continue;
201 if (*cmd == quote) {
202 quote = 0;
203 continue;
205 if (*cmd == '\'' || *cmd == '\"') {
206 quote = *cmd;
207 continue;
209 if (quote)
210 continue;
211 if (isspace((unsigned char)*cmd))
212 break;
214 if (isspace((unsigned char)*cmd))
215 argno++;
217 if (argno == 1 && strlcpy(cmd0, start, sizeof(cmd0)) <
218 sizeof(cmd0))
219 *cmdtype = tellcmd(cmd0);
222 return argno;
225 static char *
226 ftp_cmdname_generator(const char *text, int state)
228 static size_t i, len;
229 struct cmd *cmd;
231 if (state == 0) {
232 i = 0;
233 len = strlen(text);
236 while (i < nitems(cmds)) {
237 cmd = &cmds[i++];
238 if (strncmp(text, cmd->name, len) == 0)
239 return strdup(cmd->name);
242 return NULL;
245 static char *
246 ftp_bool_generator(const char *text, int state)
248 static const char *toks[] = { "on", "off" };
249 static size_t i, len;
250 const char *tok;
252 if (state == 0) {
253 i = 0;
254 len = strlen(text);
257 while ((tok = toks[i++]) != NULL) {
258 if (strncmp(text, tok, len) == 0)
259 return strdup(tok);
261 return NULL;
264 static char *
265 ftp_dirent_generator(const char *text, int state)
267 static size_t i, len;
268 const char *entry;
270 if (state == 0) {
271 i = 0;
272 len = strlen(text);
275 while (i < compl_state.len) {
276 entry = compl_state.entries[i++];
277 if (strncmp(text, entry, len) == 0)
278 return strdup(entry);
280 return NULL;
283 static char **
284 ftp_remote_files(const char *text, int start, int end)
286 const char *dir;
287 char t[PATH_MAX];
288 char *s, *e;
290 strlcpy(t, text, sizeof(t));
291 cleanword(t, 0);
293 if (!strcmp(t, "..")) {
294 char **cs;
295 if ((cs = calloc(2, sizeof(*cs))) == NULL)
296 return NULL;
297 cs[0] = strdup("../");
298 return cs;
301 s = t;
302 if (!strncmp(s, "./", 2)) {
303 s++;
304 while (*s == '/')
305 s++;
308 if ((e = strrchr(s, '/')) != NULL)
309 e[1] = '\0';
310 dir = t;
312 if (!strcmp(dir, "."))
313 strlcpy(compl_prfx, "", sizeof(compl_prfx));
314 else
315 strlcpy(compl_prfx, dir, sizeof(compl_prfx));
317 compl_state_reset();
318 if (dir_listing(dir, compl_add_entry, 0) == -1)
319 return NULL;
320 return rl_completion_matches(text, ftp_dirent_generator);
323 static char **
324 ftp_completion(const char *text, int start, int end)
326 int argno, cmdtype;
327 char *line;
329 /* don't fall back on the default completion system by default */
330 rl_attempted_completion_over = 1;
332 if ((line = rl_copy_text(0, start)) == NULL)
333 return NULL;
335 argno = tell_argno(line, &cmdtype);
336 free(line);
337 if (argno == 0)
338 return rl_completion_matches(text, ftp_cmdname_generator);
340 switch (cmdtype) {
341 case CMD_BELL:
342 case CMD_HEXDUMP:
343 case CMD_VERBOSE:
344 if (argno != 1)
345 return NULL;
346 return rl_completion_matches(text, ftp_bool_generator);
348 case CMD_BYE:
349 case CMD_LPWD:
350 /* no args */
351 return NULL;
353 case CMD_CD:
354 case CMD_EDIT:
355 case CMD_LS:
356 case CMD_PAGE:
357 if (argno != 1)
358 return NULL;
359 /* fallthrough */
360 case CMD_RM:
361 return ftp_remote_files(text, start, end);
363 case CMD_GET:
364 if (argno > 2)
365 return NULL;
366 if (argno == 2)
367 return ftp_remote_files(text, start, end);
368 /* try local */
369 rl_attempted_completion_over = 0;
370 return NULL;
372 case CMD_LCD:
373 if (argno != 1)
374 return NULL;
375 /* try local */
376 rl_attempted_completion_over = 0;
377 return NULL;
379 case CMD_PIPE:
380 if (argno > 2)
381 return NULL;
382 if (argno == 1)
383 return ftp_remote_files(text, start, end);
384 /* try local */
385 rl_attempted_completion_over = 0;
386 return NULL;
388 case CMD_PUT:
389 if (argno > 2)
390 return NULL;
391 if (argno == 1) {
392 /* try local */
393 rl_attempted_completion_over = 0;
394 return NULL;
396 return ftp_remote_files(text, start, end);
398 case CMD_RENAME:
399 if (argno > 2)
400 return NULL;
401 return ftp_remote_files(text, start, end);
404 return NULL;
407 static int
408 ftp_quoted(char *line, int index)
410 if (index > 0 && line[index - 1] == '\\')
411 return !ftp_quoted(line, index - 1);
412 return 0;
415 void
416 compl_setup(void)
418 rl_attempted_completion_function = ftp_completion;
419 rl_completer_word_break_characters = "\t ";
420 rl_completer_quote_characters = "\"'";
421 rl_char_is_quoted_p = ftp_quoted;
424 #endif