2000-04-15 04:41:27 +00:00
|
|
|
/*
|
|
|
|
* tw.comp.c: File completion builtin
|
|
|
|
*/
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1980, 1991 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
2002-07-24 16:23:10 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2000-04-15 04:41:27 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
|
|
|
*/
|
|
|
|
#include "sh.h"
|
|
|
|
#include "tw.h"
|
|
|
|
#include "ed.h"
|
|
|
|
#include "tc.h"
|
|
|
|
|
|
|
|
/* #define TDEBUG */
|
|
|
|
struct varent completions;
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
static int tw_result (const Char *, Char **);
|
|
|
|
static Char **tw_find (Char *, struct varent *, int);
|
|
|
|
static Char *tw_tok (Char *);
|
|
|
|
static int tw_pos (Char *, int);
|
|
|
|
static void tw_pr (Char **);
|
2017-03-18 21:41:53 +00:00
|
|
|
static int tw_match (const Char *, const Char *, int);
|
2007-03-11 22:33:41 +00:00
|
|
|
static void tw_prlist (struct varent *);
|
|
|
|
static const Char *tw_dollar (const Char *,Char **, size_t, Char **,
|
|
|
|
Char, const char *);
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
/* docomplete():
|
|
|
|
* Add or list completions in the completion list
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
|
|
|
void
|
2007-03-11 22:33:41 +00:00
|
|
|
docomplete(Char **v, struct command *t)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
struct varent *vp;
|
|
|
|
Char *p;
|
2004-07-11 02:17:56 +00:00
|
|
|
Char **pp;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
USE(t);
|
|
|
|
v++;
|
|
|
|
p = *v++;
|
|
|
|
if (p == 0)
|
|
|
|
tw_prlist(&completions);
|
|
|
|
else if (*v == 0) {
|
|
|
|
vp = adrof1(strip(p), &completions);
|
2002-07-24 16:23:10 +00:00
|
|
|
if (vp && vp->vec)
|
2000-04-15 04:41:27 +00:00
|
|
|
tw_pr(vp->vec), xputchar('\n');
|
2004-07-11 02:17:56 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef TDEBUG
|
|
|
|
xprintf("tw_find(%s) \n", short2str(strip(p)));
|
|
|
|
#endif /* TDEBUG */
|
|
|
|
pp = tw_find(strip(p), &completions, FALSE);
|
|
|
|
if (pp)
|
|
|
|
tw_pr(pp), xputchar('\n');
|
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
set1(strip(p), saveblk(v), &completions, VAR_READWRITE);
|
|
|
|
} /* end docomplete */
|
|
|
|
|
|
|
|
|
|
|
|
/* douncomplete():
|
|
|
|
* Remove completions from the completion list
|
|
|
|
*/
|
|
|
|
/*ARGSUSED*/
|
|
|
|
void
|
2007-03-11 22:33:41 +00:00
|
|
|
douncomplete(Char **v, struct command *t)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
USE(t);
|
|
|
|
unset1(v, &completions);
|
|
|
|
} /* end douncomplete */
|
|
|
|
|
|
|
|
|
|
|
|
/* tw_prlist():
|
|
|
|
* Pretty print a list of variables
|
|
|
|
*/
|
|
|
|
static void
|
2007-03-11 22:33:41 +00:00
|
|
|
tw_prlist(struct varent *p)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
struct varent *c;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
while (p->v_left)
|
|
|
|
p = p->v_left;
|
|
|
|
x:
|
|
|
|
if (p->v_parent == 0) /* is it the header? */
|
2007-03-11 22:33:41 +00:00
|
|
|
break;
|
|
|
|
if (setintr) {
|
|
|
|
int old_pintr_disabled;
|
|
|
|
|
|
|
|
pintr_push_enable(&old_pintr_disabled);
|
|
|
|
cleanup_until(&old_pintr_disabled);
|
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
xprintf("%s\t", short2str(p->v_name));
|
2002-07-24 16:23:10 +00:00
|
|
|
if (p->vec)
|
|
|
|
tw_pr(p->vec);
|
2000-04-15 04:41:27 +00:00
|
|
|
xputchar('\n');
|
|
|
|
if (p->v_right) {
|
|
|
|
p = p->v_right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
c = p;
|
|
|
|
p = p->v_parent;
|
|
|
|
} while (p->v_right == c);
|
|
|
|
goto x;
|
|
|
|
}
|
|
|
|
} /* end tw_prlist */
|
|
|
|
|
|
|
|
|
|
|
|
/* tw_pr():
|
|
|
|
* Pretty print a completion, adding single quotes around
|
|
|
|
* a completion argument and collapsing multiple spaces to one.
|
|
|
|
*/
|
|
|
|
static void
|
2007-03-11 22:33:41 +00:00
|
|
|
tw_pr(Char **cmp)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
int sp, osp;
|
2000-04-15 04:41:27 +00:00
|
|
|
Char *ptr;
|
|
|
|
|
|
|
|
for (; *cmp; cmp++) {
|
|
|
|
xputchar('\'');
|
|
|
|
for (osp = 0, ptr = *cmp; *ptr; ptr++) {
|
|
|
|
sp = Isspace(*ptr);
|
|
|
|
if (sp && osp)
|
|
|
|
continue;
|
2005-04-24 19:41:08 +00:00
|
|
|
xputwchar(*ptr);
|
2000-04-15 04:41:27 +00:00
|
|
|
osp = sp;
|
|
|
|
}
|
|
|
|
xputchar('\'');
|
|
|
|
if (cmp[1])
|
|
|
|
xputchar(' ');
|
|
|
|
}
|
|
|
|
} /* end tw_pr */
|
|
|
|
|
|
|
|
|
|
|
|
/* tw_find():
|
|
|
|
* Find the first matching completion.
|
|
|
|
* For commands we only look at names that start with -
|
|
|
|
*/
|
|
|
|
static Char **
|
2007-03-11 22:33:41 +00:00
|
|
|
tw_find(Char *nam, struct varent *vp, int cmd)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2005-04-24 19:41:08 +00:00
|
|
|
Char **rv;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
for (vp = vp->v_left; vp; vp = vp->v_right) {
|
|
|
|
if (vp->v_left && (rv = tw_find(nam, vp, cmd)) != NULL)
|
|
|
|
return rv;
|
|
|
|
if (cmd) {
|
|
|
|
if (vp->v_name[0] != '-')
|
|
|
|
continue;
|
|
|
|
if (Gmatch(nam, &vp->v_name[1]) && vp->vec != NULL)
|
|
|
|
return vp->vec;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (Gmatch(nam, vp->v_name) && vp->vec != NULL)
|
|
|
|
return vp->vec;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
} /* end tw_find */
|
|
|
|
|
|
|
|
|
|
|
|
/* tw_pos():
|
|
|
|
* Return true if the position is within the specified range
|
|
|
|
*/
|
2005-04-24 19:41:08 +00:00
|
|
|
static int
|
2007-03-11 22:33:41 +00:00
|
|
|
tw_pos(Char *ran, int wno)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
Char *p;
|
|
|
|
|
|
|
|
if (ran[0] == '*' && ran[1] == '\0')
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (p = ran; *p && *p != '-'; p++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p == '\0') /* range == <number> */
|
|
|
|
return wno == getn(ran);
|
|
|
|
|
|
|
|
if (ran == p) /* range = - <number> */
|
|
|
|
return wno <= getn(&ran[1]);
|
|
|
|
*p++ = '\0';
|
|
|
|
|
|
|
|
if (*p == '\0') /* range = <number> - */
|
|
|
|
return getn(ran) <= wno;
|
|
|
|
else /* range = <number> - <number> */
|
|
|
|
return (getn(ran) <= wno) && (wno <= getn(p));
|
|
|
|
} /* end tw_pos */
|
|
|
|
|
|
|
|
|
|
|
|
/* tw_tok():
|
|
|
|
* Return the next word from string, unquoteing it.
|
|
|
|
*/
|
|
|
|
static Char *
|
2007-03-11 22:33:41 +00:00
|
|
|
tw_tok(Char *str)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
static Char *bf = NULL;
|
|
|
|
|
|
|
|
if (str != NULL)
|
|
|
|
bf = str;
|
|
|
|
|
|
|
|
/* skip leading spaces */
|
|
|
|
for (; *bf && Isspace(*bf); bf++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (str = bf; *bf && !Isspace(*bf); bf++) {
|
2005-04-24 19:41:08 +00:00
|
|
|
if (ismetahash(*bf))
|
2000-04-15 04:41:27 +00:00
|
|
|
return INVPTR;
|
|
|
|
*bf = *bf & ~QUOTE;
|
|
|
|
}
|
|
|
|
if (*bf != '\0')
|
|
|
|
*bf++ = '\0';
|
|
|
|
|
|
|
|
return *str ? str : NULL;
|
|
|
|
} /* end tw_tok */
|
|
|
|
|
|
|
|
|
|
|
|
/* tw_match():
|
|
|
|
* Match a string against the pattern given.
|
|
|
|
* and return the number of matched characters
|
|
|
|
* in a prefix of the string.
|
|
|
|
*/
|
|
|
|
static int
|
2017-03-18 21:41:53 +00:00
|
|
|
tw_match(const Char *str, const Char *pat, int exact)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2007-03-11 22:33:41 +00:00
|
|
|
const Char *estr;
|
2017-03-18 21:41:53 +00:00
|
|
|
int rv = exact ? Gmatch(estr = str, pat) : Gnmatch(str, pat, &estr);
|
2000-04-15 04:41:27 +00:00
|
|
|
#ifdef TDEBUG
|
2017-03-18 21:41:53 +00:00
|
|
|
xprintf("G%smatch(%s, ", exact ? "" : "n", short2str(str));
|
2000-04-15 04:41:27 +00:00
|
|
|
xprintf("%s, ", short2str(pat));
|
2017-03-18 21:41:53 +00:00
|
|
|
xprintf("%s) = %d [%" TCSH_PTRDIFF_T_FMT "d]\n", short2str(estr), rv,
|
|
|
|
estr - str);
|
2000-04-15 04:41:27 +00:00
|
|
|
#endif /* TDEBUG */
|
|
|
|
return (int) (rv ? estr - str : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* tw_result():
|
|
|
|
* Return what the completion action should be depending on the
|
|
|
|
* string
|
|
|
|
*/
|
|
|
|
static int
|
2007-03-11 22:33:41 +00:00
|
|
|
tw_result(const Char *act, Char **pat)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
|
|
|
int looking;
|
|
|
|
static Char* res = NULL;
|
2007-03-11 22:33:41 +00:00
|
|
|
Char *p;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (res != NULL)
|
2007-03-11 22:33:41 +00:00
|
|
|
xfree(res), res = NULL;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
switch (act[0] & ~QUOTE) {
|
|
|
|
case 'X':
|
|
|
|
looking = TW_COMPLETION;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
looking = TW_SIGNAL;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
looking = TW_ALIAS;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
looking = TW_BINDING;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
looking = TW_COMMAND;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
looking = TW_PATH | TW_COMMAND;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
looking = TW_DIRECTORY;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
looking = TW_PATH | TW_DIRECTORY;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
looking = TW_ENVVAR;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
looking = TW_FILE;
|
|
|
|
break;
|
|
|
|
#ifdef COMPAT
|
|
|
|
case 'p':
|
|
|
|
#endif /* COMPAT */
|
|
|
|
case 'F':
|
|
|
|
looking = TW_PATH | TW_FILE;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
looking = TW_GRPNAME;
|
|
|
|
break;
|
|
|
|
case 'j':
|
|
|
|
looking = TW_JOB;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
looking = TW_LIMIT;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
looking = TW_NONE;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
looking = TW_SHELLVAR;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
looking = TW_TEXT;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
looking = TW_PATH | TW_TEXT;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
looking = TW_VARIABLE;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
looking = TW_USER;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
looking = TW_EXPLAIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
*pat = res = Strsave(&act[1]);
|
|
|
|
(void) strip(res);
|
|
|
|
return(TW_VARLIST);
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
*pat = res = Strsave(&act[1]);
|
2007-03-11 22:33:41 +00:00
|
|
|
if ((p = Strchr(res, ')')) != NULL)
|
|
|
|
*p = '\0';
|
2000-04-15 04:41:27 +00:00
|
|
|
(void) strip(res);
|
|
|
|
return TW_WORDLIST;
|
|
|
|
|
|
|
|
case '`':
|
|
|
|
res = Strsave(act);
|
2007-03-11 22:33:41 +00:00
|
|
|
if ((p = Strchr(&res[1], '`')) != NULL)
|
|
|
|
*++p = '\0';
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (didfds == 0) {
|
|
|
|
/*
|
|
|
|
* Make sure that we have some file descriptors to
|
|
|
|
* play with, so that the processes have at least 0, 1, 2
|
|
|
|
* open
|
|
|
|
*/
|
|
|
|
(void) dcopy(SHIN, 0);
|
|
|
|
(void) dcopy(SHOUT, 1);
|
|
|
|
(void) dcopy(SHDIAG, 2);
|
|
|
|
}
|
2007-03-11 22:33:41 +00:00
|
|
|
if ((p = globone(res, G_APPEND)) != NULL) {
|
|
|
|
xfree(res), res = NULL;
|
|
|
|
*pat = res = Strsave(p);
|
|
|
|
xfree(p);
|
2000-04-15 04:41:27 +00:00
|
|
|
return TW_WORDLIST;
|
|
|
|
}
|
|
|
|
return TW_ZERO;
|
|
|
|
|
|
|
|
default:
|
|
|
|
stderror(ERR_COMPCOM, short2str(act));
|
|
|
|
return TW_ZERO;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (act[1] & ~QUOTE) {
|
|
|
|
case '\0':
|
|
|
|
return looking;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
*pat = res = Strsave(&act[2]);
|
|
|
|
(void) strip(res);
|
|
|
|
return looking;
|
|
|
|
|
|
|
|
default:
|
|
|
|
stderror(ERR_COMPCOM, short2str(act));
|
|
|
|
return TW_ZERO;
|
|
|
|
}
|
|
|
|
} /* end tw_result */
|
2007-03-11 22:33:41 +00:00
|
|
|
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
/* tw_dollar():
|
|
|
|
* Expand $<n> args in buffer
|
|
|
|
*/
|
2007-03-11 22:33:41 +00:00
|
|
|
static const Char *
|
|
|
|
tw_dollar(const Char *str, Char **wl, size_t nwl, Char **result, Char sep,
|
|
|
|
const char *msg)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2007-03-11 22:33:41 +00:00
|
|
|
struct Strbuf buf = Strbuf_INIT;
|
|
|
|
Char *res;
|
|
|
|
const Char *sp;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
for (sp = str; *sp && *sp != sep;)
|
2000-04-15 04:41:27 +00:00
|
|
|
if (sp[0] == '$' && sp[1] == ':' && Isdigit(sp[sp[2] == '-' ? 3 : 2])) {
|
|
|
|
int num, neg = 0;
|
|
|
|
sp += 2;
|
|
|
|
if (*sp == '-') {
|
|
|
|
neg = 1;
|
|
|
|
sp++;
|
|
|
|
}
|
|
|
|
for (num = *sp++ - '0'; Isdigit(*sp); num += 10 * num + *sp++ - '0')
|
|
|
|
continue;
|
|
|
|
if (neg)
|
|
|
|
num = nwl - num - 1;
|
2007-03-11 22:33:41 +00:00
|
|
|
if (num >= 0 && (size_t)num < nwl)
|
|
|
|
Strbuf_append(&buf, wl[num]);
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
|
|
|
else
|
2007-03-11 22:33:41 +00:00
|
|
|
Strbuf_append1(&buf, *sp++);
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
res = Strbuf_finish(&buf);
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
if (*sp++ == sep) {
|
|
|
|
*result = res;
|
2000-04-15 04:41:27 +00:00
|
|
|
return sp;
|
2007-03-11 22:33:41 +00:00
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
xfree(res);
|
2005-04-24 19:41:08 +00:00
|
|
|
/* Truncates data if WIDE_STRINGS */
|
|
|
|
stderror(ERR_COMPMIS, (int)sep, msg, short2str(str));
|
2000-04-15 04:41:27 +00:00
|
|
|
return --sp;
|
|
|
|
} /* end tw_dollar */
|
2007-03-11 22:33:41 +00:00
|
|
|
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
/* tw_complete():
|
|
|
|
* Return the appropriate completion for the command
|
|
|
|
*
|
|
|
|
* valid completion strings are:
|
|
|
|
* p/<range>/<completion>/[<suffix>/] positional
|
|
|
|
* c/<pattern>/<completion>/[<suffix>/] current word ignore pattern
|
|
|
|
* C/<pattern>/<completion>/[<suffix>/] current word with pattern
|
|
|
|
* n/<pattern>/<completion>/[<suffix>/] next word
|
|
|
|
* N/<pattern>/<completion>/[<suffix>/] next-next word
|
|
|
|
*/
|
|
|
|
int
|
2007-03-11 22:33:41 +00:00
|
|
|
tw_complete(const Char *line, Char **word, Char **pat, int looking, eChar *suf)
|
2000-04-15 04:41:27 +00:00
|
|
|
{
|
2007-03-11 22:33:41 +00:00
|
|
|
Char *buf, **vec, **wl;
|
2000-04-15 04:41:27 +00:00
|
|
|
static Char nomatch[2] = { (Char) ~0, 0x00 };
|
2007-03-11 22:33:41 +00:00
|
|
|
const Char *ptr;
|
|
|
|
size_t wordno;
|
|
|
|
int n;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
buf = Strsave(line);
|
|
|
|
cleanup_push(buf, xfree);
|
|
|
|
/* Single-character words, empty current word, terminating NULL */
|
|
|
|
wl = xmalloc(((Strlen(line) + 1) / 2 + 2) * sizeof (*wl));
|
|
|
|
cleanup_push(wl, xfree);
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
/* find the command */
|
2007-03-11 22:33:41 +00:00
|
|
|
if ((wl[0] = tw_tok(buf)) == NULL || wl[0] == INVPTR) {
|
|
|
|
cleanup_until(buf);
|
2000-04-15 04:41:27 +00:00
|
|
|
return TW_ZERO;
|
2007-03-11 22:33:41 +00:00
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* look for hardwired command completions using a globbing
|
|
|
|
* search and for arguments using a normal search.
|
|
|
|
*/
|
2007-03-11 22:33:41 +00:00
|
|
|
if ((vec = tw_find(wl[0], &completions, (looking == TW_COMMAND)))
|
|
|
|
== NULL) {
|
|
|
|
cleanup_until(buf);
|
2000-04-15 04:41:27 +00:00
|
|
|
return looking;
|
2007-03-11 22:33:41 +00:00
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
/* tokenize the line one more time :-( */
|
|
|
|
for (wordno = 1; (wl[wordno] = tw_tok(NULL)) != NULL &&
|
|
|
|
wl[wordno] != INVPTR; wordno++)
|
|
|
|
continue;
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
if (wl[wordno] == INVPTR) { /* Found a meta character */
|
|
|
|
cleanup_until(buf);
|
2000-04-15 04:41:27 +00:00
|
|
|
return TW_ZERO; /* de-activate completions */
|
2007-03-11 22:33:41 +00:00
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
#ifdef TDEBUG
|
|
|
|
{
|
2007-03-11 22:33:41 +00:00
|
|
|
size_t i;
|
2000-04-15 04:41:27 +00:00
|
|
|
for (i = 0; i < wordno; i++)
|
|
|
|
xprintf("'%s' ", short2str(wl[i]));
|
|
|
|
xprintf("\n");
|
|
|
|
}
|
|
|
|
#endif /* TDEBUG */
|
|
|
|
|
|
|
|
/* if the current word is empty move the last word to the next */
|
|
|
|
if (**word == '\0') {
|
|
|
|
wl[wordno] = *word;
|
|
|
|
wordno++;
|
|
|
|
}
|
|
|
|
wl[wordno] = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef TDEBUG
|
|
|
|
xprintf("\r\n");
|
2007-03-11 22:33:41 +00:00
|
|
|
xprintf(" w#: %lu\n", (unsigned long)wordno);
|
2000-04-15 04:41:27 +00:00
|
|
|
xprintf("line: %s\n", short2str(line));
|
|
|
|
xprintf(" cmd: %s\n", short2str(wl[0]));
|
|
|
|
xprintf("word: %s\n", short2str(*word));
|
2007-03-11 22:33:41 +00:00
|
|
|
xprintf("last: %s\n", wordno >= 2 ? short2str(wl[wordno-2]) : "n/a");
|
|
|
|
xprintf("this: %s\n", wordno >= 1 ? short2str(wl[wordno-1]) : "n/a");
|
2000-04-15 04:41:27 +00:00
|
|
|
#endif /* TDEBUG */
|
|
|
|
|
|
|
|
for (;vec != NULL && (ptr = vec[0]) != NULL; vec++) {
|
2007-03-11 22:33:41 +00:00
|
|
|
Char *ran, /* The pattern or range X/<range>/XXXX/ */
|
|
|
|
*com, /* The completion X/XXXXX/<completion>/ */
|
2000-04-15 04:41:27 +00:00
|
|
|
*pos = NULL; /* scratch pointer */
|
2007-03-11 22:33:41 +00:00
|
|
|
int cmd, res;
|
2005-04-24 19:41:08 +00:00
|
|
|
Char sep; /* the command and separator characters */
|
2017-03-18 21:41:53 +00:00
|
|
|
int exact;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (ptr[0] == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#ifdef TDEBUG
|
|
|
|
xprintf("match %s\n", short2str(ptr));
|
|
|
|
#endif /* TDEBUG */
|
|
|
|
|
|
|
|
switch (cmd = ptr[0]) {
|
|
|
|
case 'N':
|
2007-03-11 22:33:41 +00:00
|
|
|
pos = (wordno < 3) ? nomatch : wl[wordno - 3];
|
2000-04-15 04:41:27 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2007-03-11 22:33:41 +00:00
|
|
|
pos = (wordno < 2) ? nomatch : wl[wordno - 2];
|
2000-04-15 04:41:27 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
case 'C':
|
2007-03-11 22:33:41 +00:00
|
|
|
pos = (wordno < 1) ? nomatch : wl[wordno - 1];
|
2000-04-15 04:41:27 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stderror(ERR_COMPINV, CGETS(27, 1, "command"), cmd);
|
|
|
|
return TW_ZERO;
|
|
|
|
}
|
|
|
|
|
|
|
|
sep = ptr[1];
|
|
|
|
if (!Ispunct(sep)) {
|
2005-04-24 19:41:08 +00:00
|
|
|
/* Truncates data if WIDE_STRINGS */
|
|
|
|
stderror(ERR_COMPINV, CGETS(27, 2, "separator"), (int)sep);
|
2000-04-15 04:41:27 +00:00
|
|
|
return TW_ZERO;
|
|
|
|
}
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
ptr = tw_dollar(&ptr[2], wl, wordno, &ran, sep,
|
2000-04-15 04:41:27 +00:00
|
|
|
CGETS(27, 3, "pattern"));
|
2007-03-11 22:33:41 +00:00
|
|
|
cleanup_push(ran, xfree);
|
2000-04-15 04:41:27 +00:00
|
|
|
if (ran[0] == '\0') /* check for empty pattern (disallowed) */
|
|
|
|
{
|
|
|
|
stderror(ERR_COMPINC, cmd == 'p' ? CGETS(27, 4, "range") :
|
|
|
|
CGETS(27, 3, "pattern"), "");
|
|
|
|
return TW_ZERO;
|
|
|
|
}
|
|
|
|
|
2007-03-11 22:33:41 +00:00
|
|
|
ptr = tw_dollar(ptr, wl, wordno, &com, sep,
|
|
|
|
CGETS(27, 5, "completion"));
|
|
|
|
cleanup_push(com, xfree);
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
if (*ptr != '\0') {
|
|
|
|
if (*ptr == sep)
|
2007-03-11 22:33:41 +00:00
|
|
|
*suf = CHAR_ERR;
|
2000-04-15 04:41:27 +00:00
|
|
|
else
|
|
|
|
*suf = *ptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*suf = '\0';
|
|
|
|
|
|
|
|
#ifdef TDEBUG
|
2005-04-24 19:41:08 +00:00
|
|
|
xprintf("command: %c\nseparator: %c\n", cmd, (int)sep);
|
2000-04-15 04:41:27 +00:00
|
|
|
xprintf("pattern: %s\n", short2str(ran));
|
|
|
|
xprintf("completion: %s\n", short2str(com));
|
|
|
|
xprintf("suffix: ");
|
|
|
|
switch (*suf) {
|
|
|
|
case 0:
|
|
|
|
xprintf("*auto suffix*\n");
|
|
|
|
break;
|
2007-03-11 22:33:41 +00:00
|
|
|
case CHAR_ERR:
|
2000-04-15 04:41:27 +00:00
|
|
|
xprintf("*no suffix*\n");
|
|
|
|
break;
|
|
|
|
default:
|
2007-03-11 22:33:41 +00:00
|
|
|
xprintf("%c\n", (int)*suf);
|
2000-04-15 04:41:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* TDEBUG */
|
|
|
|
|
2017-03-18 21:41:53 +00:00
|
|
|
exact = 0;
|
2000-04-15 04:41:27 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case 'p': /* positional completion */
|
|
|
|
#ifdef TDEBUG
|
2007-03-11 22:33:41 +00:00
|
|
|
xprintf("p: tw_pos(%s, %lu) = ", short2str(ran),
|
|
|
|
(unsigned long)wordno - 1);
|
2000-04-15 04:41:27 +00:00
|
|
|
xprintf("%d\n", tw_pos(ran, wordno - 1));
|
|
|
|
#endif /* TDEBUG */
|
2007-03-11 22:33:41 +00:00
|
|
|
if (!tw_pos(ran, wordno - 1)) {
|
|
|
|
cleanup_until(ran);
|
2000-04-15 04:41:27 +00:00
|
|
|
continue;
|
2007-03-11 22:33:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
case 'N': /* match with the next-next word */
|
|
|
|
case 'n': /* match with the next word */
|
2017-03-18 21:41:53 +00:00
|
|
|
exact = 1;
|
|
|
|
/*FALLTHROUGH*/
|
2000-04-15 04:41:27 +00:00
|
|
|
case 'c': /* match with the current word */
|
|
|
|
case 'C':
|
|
|
|
#ifdef TDEBUG
|
|
|
|
xprintf("%c: ", cmd);
|
|
|
|
#endif /* TDEBUG */
|
2017-03-18 21:41:53 +00:00
|
|
|
if ((n = tw_match(pos, ran, exact)) < 0) {
|
2007-03-11 22:33:41 +00:00
|
|
|
cleanup_until(ran);
|
2000-04-15 04:41:27 +00:00
|
|
|
continue;
|
2007-03-11 22:33:41 +00:00
|
|
|
}
|
2000-04-15 04:41:27 +00:00
|
|
|
if (cmd == 'c')
|
|
|
|
*word += n;
|
2007-03-11 22:33:41 +00:00
|
|
|
break;
|
2000-04-15 04:41:27 +00:00
|
|
|
|
|
|
|
default:
|
2007-03-11 22:33:41 +00:00
|
|
|
abort(); /* Cannot happen */
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
2009-07-10 21:47:20 +00:00
|
|
|
tsetenv(STRCOMMAND_LINE, line);
|
2007-03-11 22:33:41 +00:00
|
|
|
res = tw_result(com, pat);
|
2009-07-10 21:47:20 +00:00
|
|
|
Unsetenv(STRCOMMAND_LINE);
|
2007-03-11 22:33:41 +00:00
|
|
|
cleanup_until(buf);
|
|
|
|
return res;
|
2000-04-15 04:41:27 +00:00
|
|
|
}
|
2007-03-11 22:33:41 +00:00
|
|
|
cleanup_until(buf);
|
2000-04-15 04:41:27 +00:00
|
|
|
*suf = '\0';
|
|
|
|
return TW_ZERO;
|
|
|
|
} /* end tw_complete */
|