2006-10-22 01:37:20 +04:00
|
|
|
/* $NetBSD: complete.c,v 1.9 2006/10/21 21:37:20 christos Exp $ */
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
/*-
|
2006-10-22 01:37:20 +04:00
|
|
|
* Copyright (c) 1997-2000,2005,2006 The NetBSD Foundation, Inc.
|
2006-09-18 23:48:44 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Luke Mewburn.
|
|
|
|
*
|
2006-10-22 01:37:20 +04:00
|
|
|
* Additions by Anon Ymous. (2006)
|
|
|
|
*
|
2006-09-18 23:48:44 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Most of this is derived or copied from src/usr.bin/ftp/complete.c (1.41). */
|
|
|
|
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
#ifdef USE_EDITLINE
|
|
|
|
#undef NO_EDITCOMPLETE
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2006-10-22 01:37:20 +04:00
|
|
|
__RCSID("$NetBSD: complete.c,v 1.9 2006/10/21 21:37:20 christos Exp $");
|
2006-09-18 23:48:44 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FTP user program - command and file completion routines
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <glob.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <stringlist.h>
|
2006-10-22 01:37:20 +04:00
|
|
|
#include <util.h>
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
#include "rcv.h" /* includes "glob.h" */
|
|
|
|
#include "extern.h"
|
|
|
|
#include "complete.h"
|
2006-10-22 01:37:20 +04:00
|
|
|
#ifdef MIME_SUPPORT
|
|
|
|
#include "mime.h"
|
|
|
|
#endif
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Global variables
|
|
|
|
*/
|
|
|
|
static int doglob = 1; /* glob local file names */
|
|
|
|
|
|
|
|
#define ttyout stdout
|
|
|
|
#define ttywidth screenwidth /* in "glob.h" */
|
|
|
|
#define ttyheight screenheight /* in "glob.h" */
|
|
|
|
|
|
|
|
|
|
|
|
/* This should find the first command that matches the name given or
|
|
|
|
* NULL if none. Use the routine from mail in lex.c.
|
|
|
|
*/
|
|
|
|
#define getcmd(w) lex(w)
|
|
|
|
|
|
|
|
/************************************************************************/
|
2006-10-22 01:37:20 +04:00
|
|
|
/* from src/usr.bin/ftp/utils.h (1.135) - begin */
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* List words in stringlist, vertically arranged
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
list_vertical(StringList *sl)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int columns, lines;
|
|
|
|
char *p;
|
|
|
|
size_t w, width;
|
|
|
|
|
|
|
|
width = 0;
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
for (i = 0; i < sl->sl_cur; i++) {
|
2006-09-18 23:48:44 +04:00
|
|
|
w = strlen(sl->sl_str[i]);
|
|
|
|
if (w > width)
|
|
|
|
width = w;
|
|
|
|
}
|
|
|
|
width = (width + 8) &~ 7;
|
|
|
|
|
|
|
|
columns = ttywidth / width;
|
|
|
|
if (columns == 0)
|
|
|
|
columns = 1;
|
|
|
|
lines = (sl->sl_cur + columns - 1) / columns;
|
|
|
|
for (i = 0; i < lines; i++) {
|
|
|
|
for (j = 0; j < columns; j++) {
|
|
|
|
p = sl->sl_str[j * lines + i];
|
|
|
|
if (p)
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)fputs(p, ttyout);
|
2006-09-18 23:48:44 +04:00
|
|
|
if (j * lines + i + lines >= sl->sl_cur) {
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)putc('\n', ttyout);
|
2006-09-18 23:48:44 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p) {
|
|
|
|
w = strlen(p);
|
|
|
|
while (w < width) {
|
|
|
|
w = (w + 8) &~ 7;
|
|
|
|
(void)putc('\t', ttyout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy characters from src into dst, \ quoting characters that require it
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ftpvis(char *dst, size_t dstlen, const char *src, size_t srclen)
|
|
|
|
{
|
|
|
|
int di, si;
|
|
|
|
|
|
|
|
for (di = si = 0;
|
|
|
|
src[si] != '\0' && di < dstlen && si < srclen;
|
|
|
|
di++, si++) {
|
|
|
|
switch (src[si]) {
|
|
|
|
case '\\':
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
case '"':
|
|
|
|
dst[di++] = '\\';
|
|
|
|
if (di >= dstlen)
|
|
|
|
break;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
dst[di] = src[si];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst[di] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sl_init() with inbuilt error checking
|
|
|
|
*/
|
|
|
|
static StringList *
|
2006-10-22 01:37:20 +04:00
|
|
|
mail_sl_init(void)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
StringList *p;
|
|
|
|
|
|
|
|
p = sl_init();
|
|
|
|
if (p == NULL)
|
|
|
|
err(1, "Unable to allocate memory for stringlist");
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sl_add() with inbuilt error checking
|
|
|
|
*/
|
|
|
|
static void
|
2006-10-22 01:37:20 +04:00
|
|
|
mail_sl_add(StringList *sl, char *i)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
if (sl_add(sl, i) == -1)
|
|
|
|
err(1, "Unable to add `%s' to stringlist", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Glob a local file name specification with the expectation of a single
|
|
|
|
* return value. Can't control multiple values being expanded from the
|
|
|
|
* expression, we return only the first.
|
|
|
|
* Returns NULL on error, or a pointer to a buffer containing the filename
|
|
|
|
* that's the caller's responsiblity to free(3) when finished with.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
globulize(const char *pattern)
|
|
|
|
{
|
|
|
|
glob_t gl;
|
|
|
|
int flags;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!doglob)
|
2006-10-22 01:37:20 +04:00
|
|
|
return estrdup(pattern);
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)memset(&gl, 0, sizeof(gl));
|
2006-09-18 23:48:44 +04:00
|
|
|
if (glob(pattern, flags, NULL, &gl) || gl.gl_pathc == 0) {
|
|
|
|
warnx("%s: not found", pattern);
|
|
|
|
globfree(&gl);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2006-10-22 01:37:20 +04:00
|
|
|
p = estrdup(gl.gl_pathv[0]);
|
2006-09-18 23:48:44 +04:00
|
|
|
globfree(&gl);
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
/* from src/usr.bin/ftp/utils.h (1.135) - end */
|
2006-09-18 23:48:44 +04:00
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
static int
|
|
|
|
comparstr(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return (strcmp(*(const char * const *)a, *(const char * const *)b));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if complete is ambiguous. If unique, insert.
|
|
|
|
* If no choices, error. If unambiguous prefix, insert that.
|
|
|
|
* Otherwise, list choices. words is assumed to be filtered
|
|
|
|
* to only contain possible choices.
|
|
|
|
* Args:
|
|
|
|
* word word which started the match
|
2006-10-22 01:37:20 +04:00
|
|
|
* dolist list by default
|
2006-09-18 23:48:44 +04:00
|
|
|
* words stringlist containing possible matches
|
|
|
|
* Returns a result as per el_set(EL_ADDFN, ...)
|
|
|
|
*/
|
|
|
|
static unsigned char
|
2006-10-22 01:37:20 +04:00
|
|
|
complete_ambiguous(EditLine *el, char *word, int dolist, StringList *words)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
char insertstr[MAXPATHLEN];
|
|
|
|
char *lastmatch, *p;
|
|
|
|
int i, j;
|
|
|
|
size_t matchlen, wordlen;
|
|
|
|
|
|
|
|
wordlen = strlen(word);
|
|
|
|
if (words->sl_cur == 0)
|
|
|
|
return (CC_ERROR); /* no choices available */
|
|
|
|
|
|
|
|
if (words->sl_cur == 1) { /* only once choice available */
|
|
|
|
p = words->sl_str[0] + wordlen;
|
|
|
|
if (*p == '\0') /* at end of word? */
|
|
|
|
return (CC_REFRESH);
|
|
|
|
ftpvis(insertstr, sizeof(insertstr), p, strlen(p));
|
|
|
|
if (el_insertstr(el, insertstr) == -1)
|
|
|
|
return (CC_ERROR);
|
|
|
|
else
|
|
|
|
return (CC_REFRESH);
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
if (!dolist) {
|
2006-09-18 23:48:44 +04:00
|
|
|
matchlen = 0;
|
|
|
|
lastmatch = words->sl_str[0];
|
|
|
|
matchlen = strlen(lastmatch);
|
2006-10-22 01:37:20 +04:00
|
|
|
for (i = 1; i < words->sl_cur; i++) {
|
|
|
|
for (j = wordlen; j < strlen(words->sl_str[i]); j++)
|
2006-09-18 23:48:44 +04:00
|
|
|
if (lastmatch[j] != words->sl_str[i][j])
|
|
|
|
break;
|
|
|
|
if (j < matchlen)
|
|
|
|
matchlen = j;
|
|
|
|
}
|
|
|
|
if (matchlen > wordlen) {
|
|
|
|
ftpvis(insertstr, sizeof(insertstr),
|
|
|
|
lastmatch + wordlen, matchlen - wordlen);
|
|
|
|
if (el_insertstr(el, insertstr) == -1)
|
|
|
|
return (CC_ERROR);
|
|
|
|
else
|
|
|
|
return (CC_REFRESH_BEEP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)putc('\n', ttyout);
|
2006-09-18 23:48:44 +04:00
|
|
|
qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
|
|
|
|
list_vertical(words);
|
|
|
|
return (CC_REDISPLAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Complete a mail command.
|
|
|
|
*/
|
|
|
|
static unsigned char
|
2006-10-22 01:37:20 +04:00
|
|
|
complete_command(EditLine *el, char *word, int dolist)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
const struct cmd *c;
|
|
|
|
StringList *words;
|
|
|
|
size_t wordlen;
|
|
|
|
unsigned char rv;
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
words = mail_sl_init();
|
2006-09-18 23:48:44 +04:00
|
|
|
wordlen = strlen(word);
|
|
|
|
|
|
|
|
for (c = cmdtab; c->c_name != NULL; c++) {
|
|
|
|
if (wordlen > strlen(c->c_name))
|
|
|
|
continue;
|
|
|
|
if (strncmp(word, c->c_name, wordlen) == 0)
|
2006-10-22 01:37:20 +04:00
|
|
|
mail_sl_add(words, __UNCONST(c->c_name));
|
2006-09-18 23:48:44 +04:00
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
rv = complete_ambiguous(el, word, dolist, words);
|
2006-09-18 23:48:44 +04:00
|
|
|
if (rv == CC_REFRESH) {
|
|
|
|
if (el_insertstr(el, " ") == -1)
|
|
|
|
rv = CC_ERROR;
|
|
|
|
}
|
|
|
|
sl_free(words, 0);
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Complete a local filename.
|
|
|
|
*/
|
|
|
|
static unsigned char
|
2006-10-22 01:37:20 +04:00
|
|
|
complete_filename(EditLine *el, char *word, int dolist)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
StringList *words;
|
|
|
|
char dir[MAXPATHLEN];
|
|
|
|
char *fname;
|
|
|
|
DIR *dd;
|
|
|
|
struct dirent *dp;
|
|
|
|
unsigned char rv;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if ((fname = strrchr(word, '/')) == NULL) {
|
|
|
|
dir[0] = '.';
|
|
|
|
dir[1] = '\0';
|
|
|
|
fname = word;
|
|
|
|
} else {
|
|
|
|
if (fname == word) {
|
|
|
|
dir[0] = '/';
|
|
|
|
dir[1] = '\0';
|
2006-10-22 01:37:20 +04:00
|
|
|
} else {
|
|
|
|
len = fname - word + 1;
|
|
|
|
(void)estrlcpy(dir, word, sizeof(dir));
|
|
|
|
dir[len] = '\0';
|
|
|
|
}
|
2006-09-18 23:48:44 +04:00
|
|
|
fname++;
|
|
|
|
}
|
|
|
|
if (dir[0] == '~') {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if ((p = globulize(dir)) == NULL)
|
|
|
|
return (CC_ERROR);
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)estrlcpy(dir, p, sizeof(dir));
|
2006-09-18 23:48:44 +04:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dd = opendir(dir)) == NULL)
|
|
|
|
return (CC_ERROR);
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
words = mail_sl_init();
|
2006-09-18 23:48:44 +04:00
|
|
|
len = strlen(fname);
|
|
|
|
|
|
|
|
for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
|
|
|
|
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#if defined(DIRENT_MISSING_D_NAMLEN)
|
|
|
|
if (len > strlen(dp->d_name))
|
|
|
|
continue;
|
|
|
|
#else
|
|
|
|
if (len > dp->d_namlen)
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
if (strncmp(fname, dp->d_name, len) == 0) {
|
|
|
|
char *tcp;
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
tcp = estrdup(dp->d_name);
|
|
|
|
mail_sl_add(words, tcp);
|
2006-09-18 23:48:44 +04:00
|
|
|
}
|
|
|
|
}
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)closedir(dd);
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
rv = complete_ambiguous(el, fname, dolist, words);
|
2006-09-18 23:48:44 +04:00
|
|
|
if (rv == CC_REFRESH) {
|
|
|
|
struct stat sb;
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)estrlcpy(path, dir, sizeof(path));
|
|
|
|
(void)estrlcat(path, "/", sizeof(path));
|
|
|
|
(void)estrlcat(path, words->sl_str[0], sizeof(path));
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
if (stat(path, &sb) >= 0) {
|
|
|
|
char suffix[2] = " ";
|
|
|
|
|
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
suffix[0] = '/';
|
|
|
|
if (el_insertstr(el, suffix) == -1)
|
|
|
|
rv = CC_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sl_free(words, 1);
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
find_execs(char *word, char *path, StringList *list)
|
|
|
|
{
|
|
|
|
char *sep;
|
|
|
|
char *dir=path;
|
|
|
|
DIR *dd;
|
|
|
|
struct dirent *dp;
|
2006-10-22 01:37:20 +04:00
|
|
|
size_t len = strlen(word);
|
2006-09-18 23:48:44 +04:00
|
|
|
uid_t uid = getuid();
|
|
|
|
gid_t gid = getgid();
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
for (sep = dir; sep; dir = sep + 1) {
|
2006-09-18 23:48:44 +04:00
|
|
|
if ((sep=strchr(dir, ':')) != NULL) {
|
|
|
|
*sep=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((dd = opendir(dir)) == NULL) {
|
|
|
|
perror("dir");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
|
|
|
|
|
|
|
|
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#if defined(DIRENT_MISSING_D_NAMLEN)
|
|
|
|
if (len > strlen(dp->d_name))
|
|
|
|
continue;
|
|
|
|
#else
|
|
|
|
if (len > dp->d_namlen)
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (strncmp(word, dp->d_name, len) == 0) {
|
|
|
|
struct stat sb;
|
|
|
|
char pathname[ MAXPATHLEN ];
|
|
|
|
unsigned mask;
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)snprintf(pathname, sizeof(pathname),
|
|
|
|
"%s/%s", dir, dp->d_name);
|
2006-09-18 23:48:44 +04:00
|
|
|
if (stat(pathname, &sb) != 0) {
|
|
|
|
perror(pathname);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mask = 0001;
|
|
|
|
if (sb.st_uid == uid) mask |= 0100;
|
|
|
|
if (sb.st_gid == gid) mask |= 0010;
|
|
|
|
|
|
|
|
if ((sb.st_mode & mask) != 0) {
|
|
|
|
char *tcp;
|
2006-10-22 01:37:20 +04:00
|
|
|
tcp = estrdup(dp->d_name);
|
|
|
|
mail_sl_add(list, tcp);
|
2006-09-18 23:48:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)closedir(dd);
|
2006-09-18 23:48:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Complete a local executable
|
|
|
|
*/
|
|
|
|
static unsigned char
|
2006-10-22 01:37:20 +04:00
|
|
|
complete_executable(EditLine *el, char *word, int dolist)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
StringList *words;
|
|
|
|
char dir[ MAXPATHLEN ];
|
|
|
|
char *fname;
|
|
|
|
unsigned char rv;
|
2006-10-22 01:37:20 +04:00
|
|
|
size_t len;
|
2006-09-18 23:48:44 +04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if ((fname = strrchr(word, '/')) == NULL) {
|
|
|
|
dir[0] = '\0'; /* walk the path */
|
|
|
|
fname = word;
|
|
|
|
} else {
|
|
|
|
if (fname == word) {
|
|
|
|
dir[0] = '/';
|
|
|
|
dir[1] = '\0';
|
|
|
|
} else {
|
2006-10-22 01:37:20 +04:00
|
|
|
len = fname - word;
|
|
|
|
(void)strncpy(dir, word, len);
|
2006-09-18 23:48:44 +04:00
|
|
|
dir[fname - word] = '\0';
|
|
|
|
}
|
|
|
|
fname++;
|
|
|
|
}
|
|
|
|
|
|
|
|
words = sl_init();
|
|
|
|
|
|
|
|
if (*dir == '\0') { /* walk path */
|
|
|
|
char *env;
|
|
|
|
char *path;
|
|
|
|
env = getenv("PATH");
|
|
|
|
len = strlen(env);
|
|
|
|
path = salloc(len + 1);
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)strcpy(path, env);
|
2006-09-18 23:48:44 +04:00
|
|
|
error = find_execs(word, path, words);
|
|
|
|
}
|
|
|
|
else { /* check specified dir only */
|
|
|
|
error = find_execs(word, dir, words);
|
|
|
|
}
|
|
|
|
if (error != 0)
|
|
|
|
return CC_ERROR;
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
rv = complete_ambiguous(el, fname, dolist, words);
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
if (rv == CC_REFRESH)
|
|
|
|
if (el_insertstr(el, " ") == -1)
|
|
|
|
rv = CC_ERROR;
|
|
|
|
|
|
|
|
sl_free(words, 1);
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned
|
2006-10-22 01:37:20 +04:00
|
|
|
char complete_set(EditLine *el, char *word, int dolist)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
struct var *vp;
|
|
|
|
char **ap;
|
|
|
|
char **p;
|
|
|
|
int h;
|
|
|
|
int s;
|
2006-10-22 01:37:20 +04:00
|
|
|
size_t len = strlen(word);
|
2006-09-18 23:48:44 +04:00
|
|
|
StringList *words;
|
|
|
|
unsigned char rv;
|
|
|
|
|
|
|
|
words = sl_init();
|
|
|
|
|
|
|
|
/* allocate space for variables table */
|
|
|
|
for (h = 0, s = 1; h < HSHSIZE; h++)
|
|
|
|
for (vp = variables[h]; vp != NULL; vp = vp->v_link)
|
|
|
|
s++;
|
|
|
|
ap = (char **) salloc(s * sizeof *ap);
|
|
|
|
|
|
|
|
/* save pointers */
|
|
|
|
for (h = 0, p = ap; h < HSHSIZE; h++)
|
|
|
|
for (vp = variables[h]; vp != NULL; vp = vp->v_link)
|
|
|
|
*p++ = vp->v_name;
|
|
|
|
*p = NULL;
|
|
|
|
|
|
|
|
/* sort pointers */
|
|
|
|
sort(ap);
|
|
|
|
|
|
|
|
/* complete on list */
|
|
|
|
for (p = ap; *p != NULL; p++) {
|
|
|
|
if (len == 0 || strncmp(*p, word, len) == 0) {
|
|
|
|
char *tcp;
|
2006-10-22 01:37:20 +04:00
|
|
|
tcp = estrdup(*p);
|
|
|
|
mail_sl_add(words, tcp);
|
2006-09-18 23:48:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
rv = complete_ambiguous(el, word, dolist, words);
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
sl_free(words, 1);
|
|
|
|
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned char
|
2006-10-22 01:37:20 +04:00
|
|
|
complete_alias(EditLine *el, char *word, int dolist)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
struct grouphead *gh;
|
|
|
|
char **ap;
|
|
|
|
char **p;
|
|
|
|
int h;
|
|
|
|
int s;
|
2006-10-22 01:37:20 +04:00
|
|
|
size_t len = strlen(word);
|
2006-09-18 23:48:44 +04:00
|
|
|
StringList *words;
|
|
|
|
unsigned char rv;
|
|
|
|
|
|
|
|
words = sl_init();
|
|
|
|
|
|
|
|
/* allocate space for alias table */
|
|
|
|
for (h = 0, s = 1; h < HSHSIZE; h++)
|
|
|
|
for (gh = groups[h]; gh != NULL; gh = gh->g_link)
|
|
|
|
s++;
|
|
|
|
ap = (char **) salloc(s * sizeof *ap);
|
|
|
|
|
|
|
|
/* save pointers */
|
|
|
|
for (h = 0, p = ap; h < HSHSIZE; h++)
|
|
|
|
for (gh = groups[h]; gh != NULL; gh = gh->g_link)
|
|
|
|
*p++ = gh->g_name;
|
|
|
|
*p = NULL;
|
|
|
|
|
|
|
|
/* sort pointers */
|
|
|
|
sort(ap);
|
|
|
|
|
|
|
|
/* complete on list */
|
|
|
|
for (p = ap; *p != NULL; p++) {
|
|
|
|
if (len == 0 || strncmp(*p, word, len) == 0) {
|
|
|
|
char *tcp;
|
2006-10-22 01:37:20 +04:00
|
|
|
tcp = estrdup(*p);
|
|
|
|
mail_sl_add(words, tcp);
|
2006-09-18 23:48:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
rv = complete_ambiguous(el, word, dolist, words);
|
2006-09-18 23:48:44 +04:00
|
|
|
if (rv == CC_REFRESH)
|
|
|
|
if (el_insertstr(el, " ") == -1)
|
|
|
|
rv = CC_ERROR;
|
|
|
|
|
|
|
|
sl_free(words, 1);
|
|
|
|
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *stringbase; /* current scan point in line buffer */
|
|
|
|
static char *argbase; /* current storage point in arg buffer */
|
|
|
|
static StringList *marg_sl; /* stringlist containing margv */
|
|
|
|
static int margc; /* count of arguments on input line */
|
|
|
|
#define margv (marg_sl->sl_str) /* args parsed from input line */
|
|
|
|
|
|
|
|
static char *cursor_pos;
|
|
|
|
static int cursor_argc;
|
|
|
|
static int cursor_argo;
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_complete(void)
|
|
|
|
{
|
|
|
|
marg_sl = sl_init();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************/
|
2006-10-22 01:37:20 +04:00
|
|
|
/* from /usr/src/usr.bin/ftp/main.c(1.101) - begin */
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
static int slrflag;
|
2006-10-22 01:37:20 +04:00
|
|
|
#ifdef NEED_ALTARG
|
2006-09-18 23:48:44 +04:00
|
|
|
static char *altarg; /* argv[1] with no shell-like preprocessing */
|
2006-10-22 01:37:20 +04:00
|
|
|
#endif
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
#ifdef NO_EDITCOMPLETE
|
|
|
|
#define INC_CHKCURSOR(x) (x)++
|
|
|
|
#else /* !NO_EDITCOMPLETE */
|
|
|
|
#define INC_CHKCURSOR(x) \
|
|
|
|
do { \
|
2006-10-22 01:37:20 +04:00
|
|
|
(x)++; \
|
2006-09-18 23:48:44 +04:00
|
|
|
if (x == cursor_pos) { \
|
|
|
|
cursor_argc = margc; \
|
|
|
|
cursor_argo = ap - argbase; \
|
|
|
|
cursor_pos = NULL; \
|
|
|
|
} \
|
2006-10-22 01:37:20 +04:00
|
|
|
} while(/* CONSTCOND */ 0)
|
2006-09-18 23:48:44 +04:00
|
|
|
#endif /* !NO_EDITCOMPLETE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse string into argbuf;
|
|
|
|
* implemented with FSM to
|
|
|
|
* handle quoting and strings
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
slurpstring(void)
|
|
|
|
{
|
|
|
|
int got_one = 0;
|
|
|
|
char *sb = stringbase;
|
|
|
|
char *ap = argbase;
|
|
|
|
char *tmp = argbase; /* will return this if token found */
|
|
|
|
|
|
|
|
if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
|
|
|
|
switch (slrflag) { /* and $ as token for macro invoke */
|
|
|
|
case 0:
|
|
|
|
slrflag++;
|
|
|
|
INC_CHKCURSOR(stringbase);
|
|
|
|
return __UNCONST((*sb == '!') ? "!" : "$");
|
|
|
|
/* NOTREACHED */
|
|
|
|
case 1:
|
|
|
|
slrflag++;
|
2006-10-22 01:37:20 +04:00
|
|
|
#ifdef NEED_ALTARG
|
2006-09-18 23:48:44 +04:00
|
|
|
altarg = stringbase;
|
2006-10-22 01:37:20 +04:00
|
|
|
#endif
|
2006-09-18 23:48:44 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
S0:
|
|
|
|
switch (*sb) {
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
goto OUT;
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
INC_CHKCURSOR(sb);
|
|
|
|
goto S0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
switch (slrflag) {
|
|
|
|
case 0:
|
|
|
|
slrflag++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
slrflag++;
|
2006-10-22 01:37:20 +04:00
|
|
|
#ifdef NEED_ALTARG
|
2006-09-18 23:48:44 +04:00
|
|
|
altarg = sb;
|
2006-10-22 01:37:20 +04:00
|
|
|
#endif
|
2006-09-18 23:48:44 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto S1;
|
|
|
|
}
|
|
|
|
|
|
|
|
S1:
|
|
|
|
switch (*sb) {
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\0':
|
|
|
|
goto OUT; /* end of token */
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
INC_CHKCURSOR(sb);
|
|
|
|
goto S2; /* slurp next character */
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
INC_CHKCURSOR(sb);
|
|
|
|
goto S3; /* slurp quoted string */
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* the first arg (command) is special - see execute() in lex.c */
|
2006-10-02 20:43:31 +04:00
|
|
|
if (margc == 0 && got_one &&
|
|
|
|
strchr(" \t0123456789$^.:/-+*'\"", *sb))
|
2006-09-18 23:48:44 +04:00
|
|
|
goto OUT;
|
|
|
|
|
|
|
|
*ap = *sb; /* add character to token */
|
|
|
|
ap++;
|
|
|
|
INC_CHKCURSOR(sb);
|
|
|
|
got_one = 1;
|
|
|
|
goto S1;
|
|
|
|
}
|
|
|
|
|
|
|
|
S2:
|
|
|
|
switch (*sb) {
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
goto OUT;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*ap = *sb;
|
|
|
|
ap++;
|
|
|
|
INC_CHKCURSOR(sb);
|
|
|
|
got_one = 1;
|
|
|
|
goto S1;
|
|
|
|
}
|
|
|
|
|
|
|
|
S3:
|
|
|
|
switch (*sb) {
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
goto OUT;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
INC_CHKCURSOR(sb);
|
|
|
|
goto S1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*ap = *sb;
|
|
|
|
ap++;
|
|
|
|
INC_CHKCURSOR(sb);
|
|
|
|
got_one = 1;
|
|
|
|
goto S3;
|
|
|
|
}
|
|
|
|
|
|
|
|
OUT:
|
|
|
|
if (got_one)
|
|
|
|
*ap++ = '\0';
|
|
|
|
argbase = ap; /* update storage pointer */
|
|
|
|
stringbase = sb; /* update scan pointer */
|
|
|
|
if (got_one) {
|
|
|
|
return (tmp);
|
|
|
|
}
|
|
|
|
switch (slrflag) {
|
|
|
|
case 0:
|
|
|
|
slrflag++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
slrflag++;
|
2006-10-22 01:37:20 +04:00
|
|
|
#ifdef NEED_ALTARG
|
2006-09-18 23:48:44 +04:00
|
|
|
altarg = NULL;
|
2006-10-22 01:37:20 +04:00
|
|
|
#endif
|
2006-09-18 23:48:44 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Slice a string up into argc/argv.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
makeargv(char *line)
|
|
|
|
{
|
|
|
|
static char argbuf[ LINESIZE ]; /* argument storage buffer */
|
|
|
|
char *argp;
|
|
|
|
|
|
|
|
stringbase = line; /* scan from first of buffer */
|
|
|
|
argbase = argbuf; /* store from first of buffer */
|
|
|
|
slrflag = 0;
|
|
|
|
marg_sl->sl_cur = 0; /* reset to start of marg_sl */
|
2006-10-22 01:37:20 +04:00
|
|
|
for (margc = 0; /* EMPTY */; margc++) {
|
2006-09-18 23:48:44 +04:00
|
|
|
argp = slurpstring();
|
2006-10-22 01:37:20 +04:00
|
|
|
mail_sl_add(marg_sl, argp);
|
2006-09-18 23:48:44 +04:00
|
|
|
if (argp == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifndef NO_EDITCOMPLETE
|
|
|
|
if (cursor_pos == line) {
|
|
|
|
cursor_argc = 0;
|
|
|
|
cursor_argo = 0;
|
|
|
|
} else if (cursor_pos != NULL) {
|
|
|
|
cursor_argc = margc;
|
|
|
|
cursor_argo = strlen(margv[margc-1]);
|
|
|
|
}
|
|
|
|
#endif /* !NO_EDITCOMPLETE */
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
/* from /usr/src/usr.bin/ftp/main.c(1.101) - end */
|
2006-09-18 23:48:44 +04:00
|
|
|
/************************************************************************/
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
/* Some people like to bind file completion to CTRL-D. In emacs mode,
|
|
|
|
* CTRL-D is also used to delete the current character, we have to
|
|
|
|
* special case this situation.
|
|
|
|
*/
|
|
|
|
#define EMACS_CTRL_D_BINDING_HACK
|
|
|
|
|
|
|
|
#ifdef EMACS_CTRL_D_BINDING_HACK
|
|
|
|
static int
|
|
|
|
is_emacs_mode(EditLine *el)
|
|
|
|
{
|
|
|
|
char *mode;
|
|
|
|
if (el_get(el, EL_EDITOR, &mode) == -1)
|
|
|
|
return 0;
|
|
|
|
return equal(mode, "emacs");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
emacs_ctrl_d(EditLine *el, const LineInfo *lf, int ch, size_t len)
|
|
|
|
{
|
|
|
|
static char delunder[3] = { CTRL('f'), CTRL('h'), '\0' };
|
|
|
|
if (ch == CTRL('d') && is_emacs_mode(el)) { /* CTRL-D is special */
|
|
|
|
if (len == 0)
|
|
|
|
return (CC_EOF);
|
|
|
|
if (lf->lastchar != lf->cursor) { /* delete without using ^D */
|
|
|
|
el_push(el, delunder); /* ^F^H */
|
|
|
|
return (CC_NORM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* EMACS_CTRL_D_BINDING_HACK */
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic complete routine
|
|
|
|
*/
|
|
|
|
static unsigned char
|
2006-10-22 01:37:20 +04:00
|
|
|
mail_complete(EditLine *el, int ch)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
static char line[LINESIZE]; /* input line buffer */
|
|
|
|
static char word[LINESIZE];
|
|
|
|
static int lastc_argc, lastc_argo;
|
|
|
|
|
|
|
|
const struct cmd *c;
|
|
|
|
const LineInfo *lf;
|
|
|
|
int celems, dolist, cmpltype;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
lf = el_line(el);
|
|
|
|
len = lf->lastchar - lf->buffer;
|
2006-10-22 01:37:20 +04:00
|
|
|
|
|
|
|
#ifdef EMACS_CTRL_D_BINDING_HACK
|
|
|
|
{
|
|
|
|
int cc_ret;
|
|
|
|
if ((cc_ret = emacs_ctrl_d(el, lf, ch, len)) != -1)
|
|
|
|
return cc_ret;
|
2006-09-18 23:48:44 +04:00
|
|
|
}
|
2006-10-22 01:37:20 +04:00
|
|
|
#endif /* EMACS_CTRL_D_BINDING_HACK */
|
|
|
|
|
|
|
|
if (len >= sizeof(line) - 1)
|
2006-09-18 23:48:44 +04:00
|
|
|
return (CC_ERROR);
|
2006-10-22 01:37:20 +04:00
|
|
|
|
|
|
|
(void)strlcpy(line, lf->buffer, len + 1); /* do not use estrlcpy here! */
|
2006-09-18 23:48:44 +04:00
|
|
|
cursor_pos = line + (lf->cursor - lf->buffer);
|
|
|
|
lastc_argc = cursor_argc; /* remember last cursor pos */
|
|
|
|
lastc_argo = cursor_argo;
|
|
|
|
makeargv(line); /* build argc/argv of current line */
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
if (cursor_argo >= sizeof(word) - 1)
|
2006-09-18 23:48:44 +04:00
|
|
|
return (CC_ERROR);
|
|
|
|
|
|
|
|
dolist = 0;
|
|
|
|
/* if cursor and word are the same, list alternatives */
|
|
|
|
if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
|
|
|
|
&& strncmp(word, margv[cursor_argc] ? margv[cursor_argc] : "",
|
2006-10-22 01:37:20 +04:00
|
|
|
(size_t)cursor_argo) == 0)
|
2006-09-18 23:48:44 +04:00
|
|
|
dolist = 1;
|
|
|
|
else if (cursor_argc < margc)
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)strlcpy(word, margv[cursor_argc], (size_t)cursor_argo + 1); /* do not use estrlcpy() here */
|
2006-09-18 23:48:44 +04:00
|
|
|
word[cursor_argo] = '\0';
|
|
|
|
|
|
|
|
if (cursor_argc == 0)
|
|
|
|
return (complete_command(el, word, dolist));
|
|
|
|
|
|
|
|
c = getcmd(margv[0]);
|
|
|
|
if (c == NULL)
|
|
|
|
return (CC_ERROR);
|
|
|
|
celems = strlen(c->c_complete);
|
|
|
|
|
|
|
|
/* check for 'continuation' completes (which are uppercase) */
|
|
|
|
if ((cursor_argc > celems) && (celems > 0)
|
|
|
|
&& isupper((unsigned char) c->c_complete[celems-1]))
|
|
|
|
cursor_argc = celems;
|
|
|
|
|
|
|
|
if (cursor_argc > celems)
|
|
|
|
return (CC_ERROR);
|
|
|
|
|
|
|
|
cmpltype = c->c_complete[cursor_argc - 1];
|
|
|
|
switch (cmpltype) {
|
|
|
|
case 'a': /* alias complete */
|
|
|
|
case 'A':
|
|
|
|
return (complete_alias(el, word, dolist));
|
|
|
|
|
|
|
|
case 'c': /* command complete */
|
|
|
|
case 'C':
|
|
|
|
return (complete_command(el, word, dolist));
|
|
|
|
|
|
|
|
case 'f': /* filename complete */
|
|
|
|
case 'F':
|
|
|
|
return (complete_filename(el, word, dolist));
|
|
|
|
|
|
|
|
case 'n': /* no complete */
|
|
|
|
case 'N': /* no complete */
|
|
|
|
return (CC_ERROR);
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
return (complete_set(el, word, dolist));
|
|
|
|
|
|
|
|
case 'x': /* executable complete */
|
|
|
|
case 'X':
|
|
|
|
return (complete_executable(el, word, dolist));
|
|
|
|
|
|
|
|
default:
|
|
|
|
errx(1, "unknown complete type `%c'", cmpltype);
|
|
|
|
return (CC_ERROR);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
/*
|
|
|
|
* Generic file completion routine
|
|
|
|
*/
|
|
|
|
static unsigned char
|
|
|
|
file_complete(EditLine *el, int ch)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
2006-10-22 01:37:20 +04:00
|
|
|
static char word[LINESIZE];
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
const LineInfo *lf;
|
|
|
|
size_t len, word_len;
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
lf = el_line(el);
|
|
|
|
len = lf->lastchar - lf->buffer;
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
#ifdef EMACS_CTRL_D_BINDING_HACK
|
|
|
|
{
|
|
|
|
int cc_ret;
|
|
|
|
if ((cc_ret = emacs_ctrl_d(el, lf, ch, len)) != -1)
|
|
|
|
return cc_ret;
|
|
|
|
}
|
|
|
|
#endif /* EMACS_CTRL_D_BINDING_HACK */
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
if (len >= sizeof(word) - 1)
|
|
|
|
return (CC_ERROR);
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
word_len = lf->cursor - lf->buffer;
|
|
|
|
(void)strlcpy(word, lf->buffer, word_len + 1); /* do not use estrlcpy here! */
|
|
|
|
return complete_filename(el, word, len != word_len);
|
2006-09-18 23:48:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
#ifdef MIME_SUPPORT
|
2006-09-24 18:01:48 +04:00
|
|
|
/*
|
2006-10-22 01:37:20 +04:00
|
|
|
* Complete mime_transfer_encoding type
|
2006-09-24 18:01:48 +04:00
|
|
|
*/
|
2006-10-22 01:37:20 +04:00
|
|
|
static unsigned char
|
|
|
|
mime_enc_complete(EditLine *el, int ch)
|
|
|
|
{
|
|
|
|
static char word[LINESIZE];
|
|
|
|
StringList *words;
|
|
|
|
unsigned char rv;
|
|
|
|
const LineInfo *lf;
|
|
|
|
size_t len, word_len;
|
|
|
|
|
|
|
|
lf = el_line(el);
|
|
|
|
len = lf->lastchar - lf->buffer;
|
|
|
|
|
|
|
|
#ifdef EMACS_CTRL_D_BINDING_HACK
|
|
|
|
{
|
|
|
|
int cc_ret;
|
|
|
|
if ((cc_ret = emacs_ctrl_d(el, lf, ch, len)) != -1)
|
|
|
|
return cc_ret;
|
|
|
|
}
|
|
|
|
#endif /* EMACS_CTRL_D_BINDING_HACK */
|
|
|
|
|
|
|
|
if (len >= sizeof(word) - 1)
|
|
|
|
return (CC_ERROR);
|
|
|
|
|
|
|
|
words = mail_sl_init();
|
|
|
|
word_len = lf->cursor - lf->buffer;
|
|
|
|
{
|
|
|
|
const char *ename;
|
|
|
|
const void *cookie;
|
|
|
|
cookie = NULL;
|
|
|
|
for (ename = mime_next_encoding_name(&cookie);
|
|
|
|
ename;
|
|
|
|
ename = mime_next_encoding_name(&cookie))
|
|
|
|
if (word_len == 0 ||
|
|
|
|
strncmp(lf->buffer, ename, word_len) == 0) {
|
|
|
|
char *cp;
|
|
|
|
cp = estrdup(ename);
|
|
|
|
mail_sl_add(words, cp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)strlcpy(word, lf->buffer, word_len + 1); /* do not use estrlcpy here */
|
|
|
|
|
|
|
|
rv = complete_ambiguous(el, word, len != word_len, words);
|
|
|
|
|
|
|
|
sl_free(words, 1);
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
#endif /* MIME_SUPPORT */
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Our public interface to el_gets():
|
|
|
|
*
|
|
|
|
* init_editline()
|
|
|
|
* Initializes of all editline and completion data strutures.
|
|
|
|
*
|
|
|
|
* my_gets()
|
|
|
|
* Returns the next line of input as a NULL termnated string without
|
|
|
|
* the trailing newline.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *el_prompt;
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static const char *
|
|
|
|
show_prompt(EditLine *e __unused)
|
|
|
|
{
|
|
|
|
return el_prompt;
|
|
|
|
}
|
|
|
|
|
2006-09-18 23:48:44 +04:00
|
|
|
char *
|
2006-10-22 01:37:20 +04:00
|
|
|
my_gets(el_mode_t *em, const char *prompt, char *string)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
|
|
|
int cnt;
|
2006-10-22 01:37:20 +04:00
|
|
|
size_t len;
|
|
|
|
const char *buf;
|
|
|
|
HistEvent ev;
|
|
|
|
static char line[LINE_MAX];
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
el_prompt = prompt;
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
if (string)
|
2006-10-22 01:37:20 +04:00
|
|
|
el_push(em->el, string);
|
|
|
|
|
|
|
|
buf = el_gets(em->el, &cnt);
|
2006-09-24 18:01:48 +04:00
|
|
|
|
|
|
|
if (buf == NULL || cnt <= 0) {
|
|
|
|
if (cnt == 0)
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)putc('\n', stdout);
|
|
|
|
return NULL;
|
2006-09-24 18:01:48 +04:00
|
|
|
}
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
cnt--; /* trash the trailing LF */
|
|
|
|
len = MIN(sizeof(line) - 1, cnt);
|
|
|
|
(void)memcpy(line, buf, len);
|
2006-09-18 23:48:44 +04:00
|
|
|
line[cnt] = '\0';
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
/* enter non-empty lines into history */
|
|
|
|
if (em->hist) {
|
|
|
|
const char *p;
|
|
|
|
p = skip_white(line);
|
|
|
|
if (*p && history(em->hist, &ev, H_ENTER, line) == 0)
|
|
|
|
(void)printf("Failed history entry: %s", line);
|
|
|
|
}
|
2006-09-18 23:48:44 +04:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
static el_mode_t
|
|
|
|
init_el_mode(
|
|
|
|
const char *el_editor,
|
|
|
|
unsigned char (*completer)(EditLine *, int),
|
|
|
|
struct name *keys,
|
|
|
|
int history_size)
|
2006-09-18 23:48:44 +04:00
|
|
|
{
|
2006-10-22 01:37:20 +04:00
|
|
|
el_mode_t em;
|
|
|
|
(void)memset(&em, 0, sizeof(em));
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
em.el = el_init(getprogname(), stdin, stdout, stderr);
|
|
|
|
|
|
|
|
(void)el_set(em.el, EL_PROMPT, show_prompt);
|
|
|
|
(void)el_set(em.el, EL_SIGNAL, SIGHUP);
|
2006-09-24 18:01:48 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
if (el_editor)
|
|
|
|
(void)el_set(em.el, EL_EDITOR, el_editor);
|
|
|
|
|
|
|
|
if (completer) {
|
|
|
|
struct name *np;
|
|
|
|
(void)el_set(em.el, EL_ADDFN, "mail-complete",
|
|
|
|
"Context sensitive argument completion", completer);
|
|
|
|
for (np = keys; np; np = np->n_flink)
|
|
|
|
(void)el_set(em.el, EL_BIND, np->n_name,
|
|
|
|
"mail-complete", NULL);
|
2006-09-24 18:01:48 +04:00
|
|
|
}
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
if (history_size) {
|
|
|
|
HistEvent ev;
|
|
|
|
em.hist = history_init();
|
|
|
|
if (history(em.hist, &ev, H_SETSIZE, history_size))
|
|
|
|
(void)printf("history: %s\n", ev.str);
|
|
|
|
(void)el_set(em.el, EL_HIST, history, em.hist);
|
|
|
|
}
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
(void)el_source(em.el, NULL); /* read ~/.editrc */
|
|
|
|
|
|
|
|
return em;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct el_modes_s elm = {
|
|
|
|
.command = { .el = NULL, .hist = NULL, },
|
|
|
|
.string = { .el = NULL, .hist = NULL, },
|
|
|
|
.filec = { .el = NULL, .hist = NULL, },
|
|
|
|
#ifdef MIME_SUPPORT
|
|
|
|
.mime_enc = { .el = NULL, .hist = NULL, },
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
init_editline(void)
|
|
|
|
{
|
|
|
|
const char *mode;
|
|
|
|
int hist_size;
|
|
|
|
struct name *keys;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
mode = value(ENAME_EL_EDITOR);
|
2006-09-24 18:01:48 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
cp = value(ENAME_EL_HISTORY_SIZE);
|
|
|
|
hist_size = cp ? atoi(cp) : 0;
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
cp = value(ENAME_EL_COMPLETION_KEYS);
|
|
|
|
keys = cp && *cp ? lexpand(cp, 0) : NULL;
|
2006-09-18 23:48:44 +04:00
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
elm.command = init_el_mode(mode, mail_complete, keys, hist_size);
|
|
|
|
elm.filec = init_el_mode(mode, file_complete, keys, 0);
|
|
|
|
elm.string = init_el_mode(mode, NULL, NULL, 0);
|
|
|
|
#ifdef MIME_SUPPORT
|
|
|
|
elm.mime_enc = init_el_mode(mode, mime_enc_complete, keys, 0);
|
|
|
|
#endif
|
|
|
|
init_complete();
|
2006-09-18 23:48:44 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-10-22 01:37:20 +04:00
|
|
|
#endif /* USE_EDITLINE */
|