1 /* $Header: /src/pub/tcsh/tw.comp.c,v 1.33 2002/06/25 19:02:11 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("$Id: tw.comp.c,v 1.33 2002/06/25 19:02:11 christos Exp $")
42 struct varent completions;
44 static int tw_result __P((Char *, Char **));
45 static Char **tw_find __P((Char *, struct varent *, int));
46 static Char *tw_tok __P((Char *));
47 static bool tw_pos __P((Char *, int));
48 static void tw_pr __P((Char **));
49 static int tw_match __P((Char *, Char *));
50 static void tw_prlist __P((struct varent *));
51 static Char *tw_dollar __P((Char *,Char **, int, Char *,
55 * Add or list completions in the completion list
63 register struct varent *vp;
70 tw_prlist(&completions);
72 vp = adrof1(strip(p), &completions);
74 tw_pr(vp->vec), xputchar('\n');
77 set1(strip(p), saveblk(v), &completions, VAR_READWRITE);
78 } /* end docomplete */
82 * Remove completions from the completion list
91 unset1(v, &completions);
92 } /* end douncomplete */
96 * Pretty print a list of variables
102 register struct varent *c;
106 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
108 (void) sigrelse(SIGINT);
115 if (p->v_parent == 0) /* is it the header? */
117 xprintf("%s\t", short2str(p->v_name));
128 } while (p->v_right == c);
131 } /* end tw_prlist */
135 * Pretty print a completion, adding single quotes around
136 * a completion argument and collapsing multiple spaces to one.
145 for (; *cmp; cmp++) {
147 for (osp = 0, ptr = *cmp; *ptr; ptr++) {
162 * Find the first matching completion.
163 * For commands we only look at names that start with -
166 tw_find(nam, vp, cmd)
168 register struct varent *vp;
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
200 if (ran[0] == '*' && ran[1] == '\0')
203 for (p = ran; *p && *p != '-'; p++)
206 if (*p == '\0') /* range == <number> */
207 return wno == getn(ran);
209 if (ran == p) /* range = - <number> */
210 return wno <= getn(&ran[1]);
213 if (*p == '\0') /* range = <number> - */
214 return getn(ran) <= wno;
215 else /* range = <number> - <number> */
216 return (getn(ran) <= wno) && (wno <= getn(p));
222 * Return the next word from string, unquoteing it.
228 static Char *bf = NULL;
233 /* skip leading spaces */
234 for (; *bf && Isspace(*bf); bf++)
237 for (str = bf; *bf && !Isspace(*bf); bf++) {
245 return *str ? str : NULL;
250 * Match a string against the pattern given.
251 * and return the number of matched characters
252 * in a prefix of the string.
259 int rv = Gnmatch(str, pat, &estr);
261 xprintf("Gnmatch(%s, ", short2str(str));
262 xprintf("%s, ", short2str(pat));
263 xprintf("%s) = %d [%d]\n", short2str(estr), rv, estr - str);
265 return (int) (rv ? estr - str : -1);
270 * Return what the completion action should be depending on the
278 static Char* res = NULL;
281 xfree((ptr_t) res), res = NULL;
283 switch (act[0] & ~QUOTE) {
285 looking = TW_COMPLETION;
294 looking = TW_BINDING;
297 looking = TW_COMMAND;
300 looking = TW_PATH | TW_COMMAND;
303 looking = TW_DIRECTORY;
306 looking = TW_PATH | TW_DIRECTORY;
318 looking = TW_PATH | TW_FILE;
321 looking = TW_GRPNAME;
333 looking = TW_SHELLVAR;
339 looking = TW_PATH | TW_TEXT;
342 looking = TW_VARIABLE;
348 looking = TW_EXPLAIN;
352 *pat = res = Strsave(&act[1]);
357 *pat = res = Strsave(&act[1]);
358 if ((act = Strchr(res, ')')) != NULL)
365 if ((act = Strchr(&res[1], '`')) != NULL)
370 * Make sure that we have some file descriptors to
371 * play with, so that the processes have at least 0, 1, 2
374 (void) dcopy(SHIN, 0);
375 (void) dcopy(SHOUT, 1);
376 (void) dcopy(SHDIAG, 2);
378 if ((act = globone(res, G_APPEND)) != NULL) {
379 xfree((ptr_t) res), res = NULL;
380 *pat = res = Strsave(act);
387 stderror(ERR_COMPCOM, short2str(act));
391 switch (act[1] & ~QUOTE) {
396 *pat = res = Strsave(&act[2]);
401 stderror(ERR_COMPCOM, short2str(act));
404 } /* end tw_result */
408 * Expand $<n> args in buffer
411 tw_dollar(str, wl, nwl, buffer, sep, msg)
418 Char *sp, *bp = buffer, *ebp = &buffer[MAXPATHLEN];
420 for (sp = str; *sp && *sp != sep && bp < ebp;)
421 if (sp[0] == '$' && sp[1] == ':' && Isdigit(sp[sp[2] == '-' ? 3 : 2])) {
428 for (num = *sp++ - '0'; Isdigit(*sp); num += 10 * num + *sp++ - '0')
432 if (num >= 0 && num < nwl) {
434 for (ptr = wl[num]; *ptr && bp < ebp - 1; *bp++ = *ptr++)
447 stderror(ERR_COMPMIS, sep, msg, short2str(str));
449 } /* end tw_dollar */
453 * Return the appropriate completion for the command
455 * valid completion strings are:
456 * p/<range>/<completion>/[<suffix>/] positional
457 * c/<pattern>/<completion>/[<suffix>/] current word ignore pattern
458 * C/<pattern>/<completion>/[<suffix>/] current word with pattern
459 * n/<pattern>/<completion>/[<suffix>/] next word
460 * N/<pattern>/<completion>/[<suffix>/] next-next word
463 tw_complete(line, word, pat, looking, suf)
464 Char *line, **word, **pat;
467 Char buf[MAXPATHLEN + 1], **vec, *ptr;
468 Char *wl[MAXPATHLEN/6];
469 static Char nomatch[2] = { (Char) ~0, 0x00 };
472 copyn(buf, line, MAXPATHLEN);
474 /* find the command */
475 if ((wl[0] = tw_tok(buf)) == NULL || wl[0] == INVPTR)
479 * look for hardwired command completions using a globbing
480 * search and for arguments using a normal search.
482 if ((vec = tw_find(wl[0], &completions, (looking == TW_COMMAND))) == NULL)
485 /* tokenize the line one more time :-( */
486 for (wordno = 1; (wl[wordno] = tw_tok(NULL)) != NULL &&
487 wl[wordno] != INVPTR; wordno++)
490 if (wl[wordno] == INVPTR) /* Found a meta character */
491 return TW_ZERO; /* de-activate completions */
495 for (i = 0; i < wordno; i++)
496 xprintf("'%s' ", short2str(wl[i]));
501 /* if the current word is empty move the last word to the next */
502 if (**word == '\0') {
511 xprintf(" w#: %d\n", wordno);
512 xprintf("line: %s\n", short2str(line));
513 xprintf(" cmd: %s\n", short2str(wl[0]));
514 xprintf("word: %s\n", short2str(*word));
515 xprintf("last: %s\n", wordno - 2 >= 0 ? short2str(wl[wordno-2]) : "n/a");
516 xprintf("this: %s\n", wordno - 1 >= 0 ? short2str(wl[wordno-1]) : "n/a");
519 for (;vec != NULL && (ptr = vec[0]) != NULL; vec++) {
520 Char ran[MAXPATHLEN+1],/* The pattern or range X/<range>/XXXX/ */
521 com[MAXPATHLEN+1],/* The completion X/XXXXX/<completion>/ */
522 *pos = NULL; /* scratch pointer */
523 int cmd, sep; /* the command and separator characters */
529 xprintf("match %s\n", short2str(ptr));
532 switch (cmd = ptr[0]) {
534 pos = (wordno - 3 < 0) ? nomatch : wl[wordno - 3];
537 pos = (wordno - 2 < 0) ? nomatch : wl[wordno - 2];
541 pos = (wordno - 1 < 0) ? nomatch : wl[wordno - 1];
546 stderror(ERR_COMPINV, CGETS(27, 1, "command"), cmd);
552 stderror(ERR_COMPINV, CGETS(27, 2, "separator"), sep);
556 ptr = tw_dollar(&ptr[2], wl, wordno, ran, sep,
557 CGETS(27, 3, "pattern"));
558 if (ran[0] == '\0') /* check for empty pattern (disallowed) */
560 stderror(ERR_COMPINC, cmd == 'p' ? CGETS(27, 4, "range") :
561 CGETS(27, 3, "pattern"), "");
565 ptr = tw_dollar(ptr, wl, wordno, com, sep, CGETS(27, 5, "completion"));
577 xprintf("command: %c\nseparator: %c\n", cmd, sep);
578 xprintf("pattern: %s\n", short2str(ran));
579 xprintf("completion: %s\n", short2str(com));
583 xprintf("*auto suffix*\n");
586 xprintf("*no suffix*\n");
589 xprintf("%c\n", *suf);
595 case 'p': /* positional completion */
597 xprintf("p: tw_pos(%s, %d) = ", short2str(ran), wordno - 1);
598 xprintf("%d\n", tw_pos(ran, wordno - 1));
600 if (!tw_pos(ran, wordno - 1))
602 return tw_result(com, pat);
604 case 'N': /* match with the next-next word */
605 case 'n': /* match with the next word */
606 case 'c': /* match with the current word */
609 xprintf("%c: ", cmd);
611 if ((n = tw_match(pos, ran)) < 0)
615 return tw_result(com, pat);
618 return TW_ZERO; /* Cannot happen */
623 } /* end tw_complete */