1 /* $Header: /p/tcsh/cvsroot/tcsh/tw.comp.c,v 1.42 2007/10/01 21:52:00 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.42 2007/10/01 21:52:00 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 *);
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)
254 int rv = Gnmatch(str, pat, &estr);
256 xprintf("Gnmatch(%s, ", short2str(str));
257 xprintf("%s, ", short2str(pat));
258 xprintf("%s) = %d [%d]\n", short2str(estr), rv, estr - str);
260 return (int) (rv ? estr - str : -1);
265 * Return what the completion action should be depending on the
269 tw_result(const Char *act, Char **pat)
272 static Char* res = NULL;
276 xfree(res), res = NULL;
278 switch (act[0] & ~QUOTE) {
280 looking = TW_COMPLETION;
289 looking = TW_BINDING;
292 looking = TW_COMMAND;
295 looking = TW_PATH | TW_COMMAND;
298 looking = TW_DIRECTORY;
301 looking = TW_PATH | TW_DIRECTORY;
313 looking = TW_PATH | TW_FILE;
316 looking = TW_GRPNAME;
328 looking = TW_SHELLVAR;
334 looking = TW_PATH | TW_TEXT;
337 looking = TW_VARIABLE;
343 looking = TW_EXPLAIN;
347 *pat = res = Strsave(&act[1]);
352 *pat = res = Strsave(&act[1]);
353 if ((p = Strchr(res, ')')) != NULL)
360 if ((p = Strchr(&res[1], '`')) != NULL)
365 * Make sure that we have some file descriptors to
366 * play with, so that the processes have at least 0, 1, 2
369 (void) dcopy(SHIN, 0);
370 (void) dcopy(SHOUT, 1);
371 (void) dcopy(SHDIAG, 2);
373 if ((p = globone(res, G_APPEND)) != NULL) {
374 xfree(res), res = NULL;
375 *pat = res = Strsave(p);
382 stderror(ERR_COMPCOM, short2str(act));
386 switch (act[1] & ~QUOTE) {
391 *pat = res = Strsave(&act[2]);
396 stderror(ERR_COMPCOM, short2str(act));
399 } /* end tw_result */
403 * Expand $<n> args in buffer
406 tw_dollar(const Char *str, Char **wl, size_t nwl, Char **result, Char sep,
409 struct Strbuf buf = Strbuf_INIT;
413 for (sp = str; *sp && *sp != sep;)
414 if (sp[0] == '$' && sp[1] == ':' && Isdigit(sp[sp[2] == '-' ? 3 : 2])) {
421 for (num = *sp++ - '0'; Isdigit(*sp); num += 10 * num + *sp++ - '0')
425 if (num >= 0 && (size_t)num < nwl)
426 Strbuf_append(&buf, wl[num]);
429 Strbuf_append1(&buf, *sp++);
431 res = Strbuf_finish(&buf);
439 /* Truncates data if WIDE_STRINGS */
440 stderror(ERR_COMPMIS, (int)sep, msg, short2str(str));
442 } /* end tw_dollar */
446 * Return the appropriate completion for the command
448 * valid completion strings are:
449 * p/<range>/<completion>/[<suffix>/] positional
450 * c/<pattern>/<completion>/[<suffix>/] current word ignore pattern
451 * C/<pattern>/<completion>/[<suffix>/] current word with pattern
452 * n/<pattern>/<completion>/[<suffix>/] next word
453 * N/<pattern>/<completion>/[<suffix>/] next-next word
456 tw_complete(const Char *line, Char **word, Char **pat, int looking, eChar *suf)
458 Char *buf, **vec, **wl;
459 static Char nomatch[2] = { (Char) ~0, 0x00 };
465 cleanup_push(buf, xfree);
466 /* Single-character words, empty current word, terminating NULL */
467 wl = xmalloc(((Strlen(line) + 1) / 2 + 2) * sizeof (*wl));
468 cleanup_push(wl, xfree);
470 /* find the command */
471 if ((wl[0] = tw_tok(buf)) == NULL || wl[0] == INVPTR) {
477 * look for hardwired command completions using a globbing
478 * search and for arguments using a normal search.
480 if ((vec = tw_find(wl[0], &completions, (looking == TW_COMMAND)))
486 /* tokenize the line one more time :-( */
487 for (wordno = 1; (wl[wordno] = tw_tok(NULL)) != NULL &&
488 wl[wordno] != INVPTR; wordno++)
491 if (wl[wordno] == INVPTR) { /* Found a meta character */
493 return TW_ZERO; /* de-activate completions */
498 for (i = 0; i < wordno; i++)
499 xprintf("'%s' ", short2str(wl[i]));
504 /* if the current word is empty move the last word to the next */
505 if (**word == '\0') {
514 xprintf(" w#: %lu\n", (unsigned long)wordno);
515 xprintf("line: %s\n", short2str(line));
516 xprintf(" cmd: %s\n", short2str(wl[0]));
517 xprintf("word: %s\n", short2str(*word));
518 xprintf("last: %s\n", wordno >= 2 ? short2str(wl[wordno-2]) : "n/a");
519 xprintf("this: %s\n", wordno >= 1 ? short2str(wl[wordno-1]) : "n/a");
522 for (;vec != NULL && (ptr = vec[0]) != NULL; vec++) {
523 Char *ran, /* The pattern or range X/<range>/XXXX/ */
524 *com, /* The completion X/XXXXX/<completion>/ */
525 *pos = NULL; /* scratch pointer */
527 Char sep; /* the command and separator characters */
533 xprintf("match %s\n", short2str(ptr));
536 switch (cmd = ptr[0]) {
538 pos = (wordno < 3) ? nomatch : wl[wordno - 3];
541 pos = (wordno < 2) ? nomatch : wl[wordno - 2];
545 pos = (wordno < 1) ? nomatch : wl[wordno - 1];
550 stderror(ERR_COMPINV, CGETS(27, 1, "command"), cmd);
556 /* Truncates data if WIDE_STRINGS */
557 stderror(ERR_COMPINV, CGETS(27, 2, "separator"), (int)sep);
561 ptr = tw_dollar(&ptr[2], wl, wordno, &ran, sep,
562 CGETS(27, 3, "pattern"));
563 cleanup_push(ran, xfree);
564 if (ran[0] == '\0') /* check for empty pattern (disallowed) */
566 stderror(ERR_COMPINC, cmd == 'p' ? CGETS(27, 4, "range") :
567 CGETS(27, 3, "pattern"), "");
571 ptr = tw_dollar(ptr, wl, wordno, &com, sep,
572 CGETS(27, 5, "completion"));
573 cleanup_push(com, xfree);
585 xprintf("command: %c\nseparator: %c\n", cmd, (int)sep);
586 xprintf("pattern: %s\n", short2str(ran));
587 xprintf("completion: %s\n", short2str(com));
591 xprintf("*auto suffix*\n");
594 xprintf("*no suffix*\n");
597 xprintf("%c\n", (int)*suf);
603 case 'p': /* positional completion */
605 xprintf("p: tw_pos(%s, %lu) = ", short2str(ran),
606 (unsigned long)wordno - 1);
607 xprintf("%d\n", tw_pos(ran, wordno - 1));
609 if (!tw_pos(ran, wordno - 1)) {
615 case 'N': /* match with the next-next word */
616 case 'n': /* match with the next word */
617 case 'c': /* match with the current word */
620 xprintf("%c: ", cmd);
622 if ((n = tw_match(pos, ran)) < 0) {
631 abort(); /* Cannot happen */
633 tsetenv(STRCOMMAND_LINE, line);
634 res = tw_result(com, pat);
635 Unsetenv(STRCOMMAND_LINE);
642 } /* end tw_complete */