2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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.
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.
25 readline(const char *prompt)
27 char *ch, *line = NULL;
34 linelen = getline(&line, &linesize, stdin);
38 if ((ch = strchr(line, '\n')) != NULL)
44 add_history(const char *line)
55 #else /* HAVE_READLINE */
63 #include <readline/readline.h>
64 #include <readline/history.h>
75 static struct compl_state compl_state;
76 static char compl_prfx[PATH_MAX];
79 compl_state_reset(void)
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));
91 compl_add_entry(const struct np_stat *st)
93 const char *sufx = "";
96 if (compl_state.len == compl_state.size) {
97 size_t newsz = compl_state.size * 1.5;
103 /* one for the NULL entry at the end */
104 t = recallocarray(compl_state.entries, compl_state.size,
105 newsz + 1, sizeof(char *));
108 compl_state.entries = t;
109 compl_state.size = newsz;
112 if (st->qid.type & QTDIR)
115 if (asprintf(&dup, "%s%s%s", compl_prfx, st->name, sufx) == -1)
117 compl_state.entries[compl_state.len++] = dup;
122 cleanword(char *buf, int brkspc)
127 while (brkspc && isspace((unsigned char)*buf))
128 memmove(buf, buf + 1, strlen(buf));
131 for (cmd = buf; *cmd != '\0'; ++cmd) {
142 if (*cmd == '\'' || *cmd == '"') {
148 if (brkspc && isspace((unsigned char)*cmd))
153 memmove(cmd, cmd + 1, strlen(cmd));
165 for (i = 0; i < nitems(cmds); ++i) {
166 if (!strcmp(cmds[i].name, buf))
167 return cmds[i].cmdtype;
174 tell_argno(const char *cmd, int *cmdtype)
176 char cmd0[64]; /* plenty of space */
177 const char *start = cmd;
181 *cmdtype = CMD_UNKNOWN;
183 /* find which argument needs to be completed */
185 while (isspace((unsigned char)*cmd))
191 for (; *cmd; ++cmd) {
204 if (*cmd == '\'' || *cmd == '\"') {
210 if (isspace((unsigned char)*cmd))
213 if (isspace((unsigned char)*cmd))
216 if (argno == 1 && strlcpy(cmd0, start, sizeof(cmd0)) <
218 *cmdtype = tellcmd(cmd0);
225 ftp_cmdname_generator(const char *text, int state)
227 static size_t i, len;
235 while (i < nitems(cmds)) {
237 if (strncmp(text, cmd->name, len) == 0)
238 return strdup(cmd->name);
245 ftp_bool_generator(const char *text, int state)
247 static const char *toks[] = { "on", "off" };
248 static size_t i, len;
256 while ((tok = toks[i++]) != NULL) {
257 if (strncmp(text, tok, len) == 0)
264 ftp_dirent_generator(const char *text, int state)
266 static size_t i, len;
274 while (i < compl_state.len) {
275 entry = compl_state.entries[i++];
276 if (strncmp(text, entry, len) == 0)
277 return strdup(entry);
283 ftp_remote_files(const char *text, int start, int end)
289 strlcpy(t, text, sizeof(t));
292 if (!strcmp(t, "..")) {
294 if ((cs = calloc(2, sizeof(*cs))) == NULL)
296 cs[0] = strdup("../");
301 if (!strncmp(s, "./", 2)) {
307 if ((e = strrchr(s, '/')) != NULL)
311 if (!strcmp(dir, "."))
312 strlcpy(compl_prfx, "", sizeof(compl_prfx));
314 strlcpy(compl_prfx, dir, sizeof(compl_prfx));
317 if (dir_listing(dir, compl_add_entry, 0) == -1)
319 return rl_completion_matches(text, ftp_dirent_generator);
323 ftp_completion(const char *text, int start, int end)
328 /* don't fall back on the default completion system by default */
329 rl_attempted_completion_over = 1;
331 if ((line = rl_copy_text(0, start)) == NULL)
334 argno = tell_argno(line, &cmdtype);
337 return rl_completion_matches(text, ftp_cmdname_generator);
345 return rl_completion_matches(text, ftp_bool_generator);
360 return ftp_remote_files(text, start, end);
366 return ftp_remote_files(text, start, end);
368 rl_attempted_completion_over = 0;
375 rl_attempted_completion_over = 0;
382 return ftp_remote_files(text, start, end);
384 rl_attempted_completion_over = 0;
392 rl_attempted_completion_over = 0;
395 return ftp_remote_files(text, start, end);
400 return ftp_remote_files(text, start, end);
407 ftp_quoted(char *line, int index)
409 if (index > 0 && line[index - 1] == '\\')
410 return !ftp_quoted(line, index - 1);
417 rl_attempted_completion_function = ftp_completion;
418 rl_completer_word_break_characters = "\t ";
419 rl_completer_quote_characters = "\"'";
420 rl_char_is_quoted_p = ftp_quoted;