1 /* $Header: /p/tcsh/cvsroot/tcsh/tw.comp.c,v 1.44 2014/07/24 10:50:15 christos Exp $ */
3 * tw.comp.c: File completion builtin
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$tcsh: tw.comp.c,v 1.44 2014/07/24 10:50:15 christos Exp $")
42 struct varent completions;
44 static int tw_result (const Char *, Char **);
45 static Char **tw_find (Char *, struct varent *, int);
46 static Char *tw_tok (Char *);
47 static int tw_pos (Char *, int);
48 static void tw_pr (Char **);
49 static int tw_match (const Char *, const Char *, int);
50 static void tw_prlist (struct varent *);
51 static const Char *tw_dollar (const Char *,Char **, size_t, Char **,
55 * Add or list completions in the completion list
59 docomplete(Char **v, struct command *t)
69 tw_prlist(&completions);
71 vp = adrof1(strip(p), &completions);
73 tw_pr(vp->vec), xputchar('\n');
77 xprintf("tw_find(%s) \n", short2str(strip(p)));
79 pp = tw_find(strip(p), &completions, FALSE);
81 tw_pr(pp), xputchar('\n');
85 set1(strip(p), saveblk(v), &completions, VAR_READWRITE);
86 } /* end docomplete */
90 * Remove completions from the completion list
94 douncomplete(Char **v, struct command *t)
97 unset1(v, &completions);
98 } /* end douncomplete */
102 * Pretty print a list of variables
105 tw_prlist(struct varent *p)
113 if (p->v_parent == 0) /* is it the header? */
116 int old_pintr_disabled;
118 pintr_push_enable(&old_pintr_disabled);
119 cleanup_until(&old_pintr_disabled);
121 xprintf("%s\t", short2str(p->v_name));
132 } while (p->v_right == c);
135 } /* end tw_prlist */
139 * Pretty print a completion, adding single quotes around
140 * a completion argument and collapsing multiple spaces to one.
148 for (; *cmp; cmp++) {
150 for (osp = 0, ptr = *cmp; *ptr; ptr++) {
165 * Find the first matching completion.
166 * For commands we only look at names that start with -
169 tw_find(Char *nam, struct varent *vp, int cmd)
173 for (vp = vp->v_left; vp; vp = vp->v_right) {
174 if (vp->v_left && (rv = tw_find(nam, vp, cmd)) != NULL)
177 if (vp->v_name[0] != '-')
179 if (Gmatch(nam, &vp->v_name[1]) && vp->vec != NULL)
183 if (Gmatch(nam, vp->v_name) && vp->vec != NULL)
191 * Return true if the position is within the specified range
194 tw_pos(Char *ran, int wno)
198 if (ran[0] == '*' && ran[1] == '\0')
201 for (p = ran; *p && *p != '-'; p++)
204 if (*p == '\0') /* range == <number> */
205 return wno == getn(ran);
207 if (ran == p) /* range = - <number> */
208 return wno <= getn(&ran[1]);
211 if (*p == '\0') /* range = <number> - */
212 return getn(ran) <= wno;
213 else /* range = <number> - <number> */
214 return (getn(ran) <= wno) && (wno <= getn(p));
219 * Return the next word from string, unquoteing it.
224 static Char *bf = NULL;
229 /* skip leading spaces */
230 for (; *bf && Isspace(*bf); bf++)
233 for (str = bf; *bf && !Isspace(*bf); bf++) {
241 return *str ? str : NULL;
246 * Match a string against the pattern given.
247 * and return the number of matched characters
248 * in a prefix of the string.
251 tw_match(const Char *str, const Char *pat, int exact)
254 int rv = exact ? Gmatch(str, pat) : Gnmatch(str, pat, &estr);
256 xprintf("G%smatch(%s, ", exact ? "" : "n", short2str(str));
257 xprintf("%s, ", short2str(pat));
258 xprintf("%s) = %d [%" TCSH_PTRDIFF_T_FMT "d]\n", short2str(estr), rv,
261 return (int) (rv ? estr - str : -1);
266 * Return what the completion action should be depending on the
270 tw_result(const Char *act, Char **pat)
273 static Char* res = NULL;
277 xfree(res), res = NULL;
279 switch (act[0] & ~QUOTE) {
281 looking = TW_COMPLETION;
290 looking = TW_BINDING;
293 looking = TW_COMMAND;
296 looking = TW_PATH | TW_COMMAND;
299 looking = TW_DIRECTORY;
302 looking = TW_PATH | TW_DIRECTORY;
314 looking = TW_PATH | TW_FILE;
317 looking = TW_GRPNAME;
329 looking = TW_SHELLVAR;
335 looking = TW_PATH | TW_TEXT;
338 looking = TW_VARIABLE;
344 looking = TW_EXPLAIN;
348 *pat = res = Strsave(&act[1]);
353 *pat = res = Strsave(&act[1]);
354 if ((p = Strchr(res, ')')) != NULL)
361 if ((p = Strchr(&res[1], '`')) != NULL)
366 * Make sure that we have some file descriptors to
367 * play with, so that the processes have at least 0, 1, 2
370 (void) dcopy(SHIN, 0);
371 (void) dcopy(SHOUT, 1);
372 (void) dcopy(SHDIAG, 2);
374 if ((p = globone(res, G_APPEND)) != NULL) {
375 xfree(res), res = NULL;
376 *pat = res = Strsave(p);
383 stderror(ERR_COMPCOM, short2str(act));
387 switch (act[1] & ~QUOTE) {
392 *pat = res = Strsave(&act[2]);
397 stderror(ERR_COMPCOM, short2str(act));
400 } /* end tw_result */
404 * Expand $<n> args in buffer
407 tw_dollar(const Char *str, Char **wl, size_t nwl, Char **result, Char sep,
410 struct Strbuf buf = Strbuf_INIT;
414 for (sp = str; *sp && *sp != sep;)
415 if (sp[0] == '$' && sp[1] == ':' && Isdigit(sp[sp[2] == '-' ? 3 : 2])) {
422 for (num = *sp++ - '0'; Isdigit(*sp); num += 10 * num + *sp++ - '0')
426 if (num >= 0 && (size_t)num < nwl)
427 Strbuf_append(&buf, wl[num]);
430 Strbuf_append1(&buf, *sp++);
432 res = Strbuf_finish(&buf);
440 /* Truncates data if WIDE_STRINGS */
441 stderror(ERR_COMPMIS, (int)sep, msg, short2str(str));
443 } /* end tw_dollar */
447 * Return the appropriate completion for the command
449 * valid completion strings are:
450 * p/<range>/<completion>/[<suffix>/] positional
451 * c/<pattern>/<completion>/[<suffix>/] current word ignore pattern
452 * C/<pattern>/<completion>/[<suffix>/] current word with pattern
453 * n/<pattern>/<completion>/[<suffix>/] next word
454 * N/<pattern>/<completion>/[<suffix>/] next-next word
457 tw_complete(const Char *line, Char **word, Char **pat, int looking, eChar *suf)
459 Char *buf, **vec, **wl;
460 static Char nomatch[2] = { (Char) ~0, 0x00 };
466 cleanup_push(buf, xfree);
467 /* Single-character words, empty current word, terminating NULL */
468 wl = xmalloc(((Strlen(line) + 1) / 2 + 2) * sizeof (*wl));
469 cleanup_push(wl, xfree);
471 /* find the command */
472 if ((wl[0] = tw_tok(buf)) == NULL || wl[0] == INVPTR) {
478 * look for hardwired command completions using a globbing
479 * search and for arguments using a normal search.
481 if ((vec = tw_find(wl[0], &completions, (looking == TW_COMMAND)))
487 /* tokenize the line one more time :-( */
488 for (wordno = 1; (wl[wordno] = tw_tok(NULL)) != NULL &&
489 wl[wordno] != INVPTR; wordno++)
492 if (wl[wordno] == INVPTR) { /* Found a meta character */
494 return TW_ZERO; /* de-activate completions */
499 for (i = 0; i < wordno; i++)
500 xprintf("'%s' ", short2str(wl[i]));
505 /* if the current word is empty move the last word to the next */
506 if (**word == '\0') {
515 xprintf(" w#: %lu\n", (unsigned long)wordno);
516 xprintf("line: %s\n", short2str(line));
517 xprintf(" cmd: %s\n", short2str(wl[0]));
518 xprintf("word: %s\n", short2str(*word));
519 xprintf("last: %s\n", wordno >= 2 ? short2str(wl[wordno-2]) : "n/a");
520 xprintf("this: %s\n", wordno >= 1 ? short2str(wl[wordno-1]) : "n/a");
523 for (;vec != NULL && (ptr = vec[0]) != NULL; vec++) {
524 Char *ran, /* The pattern or range X/<range>/XXXX/ */
525 *com, /* The completion X/XXXXX/<completion>/ */
526 *pos = NULL; /* scratch pointer */
528 Char sep; /* the command and separator characters */
535 xprintf("match %s\n", short2str(ptr));
538 switch (cmd = ptr[0]) {
540 pos = (wordno < 3) ? nomatch : wl[wordno - 3];
543 pos = (wordno < 2) ? nomatch : wl[wordno - 2];
547 pos = (wordno < 1) ? nomatch : wl[wordno - 1];
552 stderror(ERR_COMPINV, CGETS(27, 1, "command"), cmd);
558 /* Truncates data if WIDE_STRINGS */
559 stderror(ERR_COMPINV, CGETS(27, 2, "separator"), (int)sep);
563 ptr = tw_dollar(&ptr[2], wl, wordno, &ran, sep,
564 CGETS(27, 3, "pattern"));
565 cleanup_push(ran, xfree);
566 if (ran[0] == '\0') /* check for empty pattern (disallowed) */
568 stderror(ERR_COMPINC, cmd == 'p' ? CGETS(27, 4, "range") :
569 CGETS(27, 3, "pattern"), "");
573 ptr = tw_dollar(ptr, wl, wordno, &com, sep,
574 CGETS(27, 5, "completion"));
575 cleanup_push(com, xfree);
587 xprintf("command: %c\nseparator: %c\n", cmd, (int)sep);
588 xprintf("pattern: %s\n", short2str(ran));
589 xprintf("completion: %s\n", short2str(com));
593 xprintf("*auto suffix*\n");
596 xprintf("*no suffix*\n");
599 xprintf("%c\n", (int)*suf);
606 case 'p': /* positional completion */
608 xprintf("p: tw_pos(%s, %lu) = ", short2str(ran),
609 (unsigned long)wordno - 1);
610 xprintf("%d\n", tw_pos(ran, wordno - 1));
612 if (!tw_pos(ran, wordno - 1)) {
618 case 'N': /* match with the next-next word */
619 case 'n': /* match with the next word */
622 case 'c': /* match with the current word */
625 xprintf("%c: ", cmd);
627 if ((n = tw_match(pos, ran, exact)) < 0) {
636 abort(); /* Cannot happen */
638 tsetenv(STRCOMMAND_LINE, line);
639 res = tw_result(com, pat);
640 Unsetenv(STRCOMMAND_LINE);
647 } /* end tw_complete */