1 /* $Header: /src/pub/tcsh/tw.comp.c,v 1.37 2004/11/23 02:10:50 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.37 2004/11/23 02:10:50 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 int 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
71 tw_prlist(&completions);
73 vp = adrof1(strip(p), &completions);
75 tw_pr(vp->vec), xputchar('\n');
79 xprintf("tw_find(%s) \n", short2str(strip(p)));
81 pp = tw_find(strip(p), &completions, FALSE);
83 tw_pr(pp), xputchar('\n');
87 set1(strip(p), saveblk(v), &completions, VAR_READWRITE);
88 } /* end docomplete */
92 * Remove completions from the completion list
101 unset1(v, &completions);
102 } /* end douncomplete */
106 * Pretty print a list of variables
116 (void) sigsetmask(sigblock((sigmask_t) 0) & ~sigmask(SIGINT));
118 (void) sigrelse(SIGINT);
125 if (p->v_parent == 0) /* is it the header? */
127 xprintf("%s\t", short2str(p->v_name));
138 } while (p->v_right == c);
141 } /* end tw_prlist */
145 * Pretty print a completion, adding single quotes around
146 * a completion argument and collapsing multiple spaces to one.
155 for (; *cmp; cmp++) {
157 for (osp = 0, ptr = *cmp; *ptr; ptr++) {
172 * Find the first matching completion.
173 * For commands we only look at names that start with -
176 tw_find(nam, vp, cmd)
183 for (vp = vp->v_left; vp; vp = vp->v_right) {
184 if (vp->v_left && (rv = tw_find(nam, vp, cmd)) != NULL)
187 if (vp->v_name[0] != '-')
189 if (Gmatch(nam, &vp->v_name[1]) && vp->vec != NULL)
193 if (Gmatch(nam, vp->v_name) && vp->vec != NULL)
201 * Return true if the position is within the specified range
210 if (ran[0] == '*' && ran[1] == '\0')
213 for (p = ran; *p && *p != '-'; p++)
216 if (*p == '\0') /* range == <number> */
217 return wno == getn(ran);
219 if (ran == p) /* range = - <number> */
220 return wno <= getn(&ran[1]);
223 if (*p == '\0') /* range = <number> - */
224 return getn(ran) <= wno;
225 else /* range = <number> - <number> */
226 return (getn(ran) <= wno) && (wno <= getn(p));
232 * Return the next word from string, unquoteing it.
238 static Char *bf = NULL;
243 /* skip leading spaces */
244 for (; *bf && Isspace(*bf); bf++)
247 for (str = bf; *bf && !Isspace(*bf); bf++) {
255 return *str ? str : NULL;
260 * Match a string against the pattern given.
261 * and return the number of matched characters
262 * in a prefix of the string.
269 int rv = Gnmatch(str, pat, &estr);
271 xprintf("Gnmatch(%s, ", short2str(str));
272 xprintf("%s, ", short2str(pat));
273 xprintf("%s) = %d [%d]\n", short2str(estr), rv, estr - str);
275 return (int) (rv ? estr - str : -1);
280 * Return what the completion action should be depending on the
288 static Char* res = NULL;
291 xfree((ptr_t) res), res = NULL;
293 switch (act[0] & ~QUOTE) {
295 looking = TW_COMPLETION;
304 looking = TW_BINDING;
307 looking = TW_COMMAND;
310 looking = TW_PATH | TW_COMMAND;
313 looking = TW_DIRECTORY;
316 looking = TW_PATH | TW_DIRECTORY;
328 looking = TW_PATH | TW_FILE;
331 looking = TW_GRPNAME;
343 looking = TW_SHELLVAR;
349 looking = TW_PATH | TW_TEXT;
352 looking = TW_VARIABLE;
358 looking = TW_EXPLAIN;
362 *pat = res = Strsave(&act[1]);
367 *pat = res = Strsave(&act[1]);
368 if ((act = Strchr(res, ')')) != NULL)
375 if ((act = Strchr(&res[1], '`')) != NULL)
380 * Make sure that we have some file descriptors to
381 * play with, so that the processes have at least 0, 1, 2
384 (void) dcopy(SHIN, 0);
385 (void) dcopy(SHOUT, 1);
386 (void) dcopy(SHDIAG, 2);
388 if ((act = globone(res, G_APPEND)) != NULL) {
389 xfree((ptr_t) res), res = NULL;
390 *pat = res = Strsave(act);
397 stderror(ERR_COMPCOM, short2str(act));
401 switch (act[1] & ~QUOTE) {
406 *pat = res = Strsave(&act[2]);
411 stderror(ERR_COMPCOM, short2str(act));
414 } /* end tw_result */
418 * Expand $<n> args in buffer
421 tw_dollar(str, wl, nwl, buffer, sep, msg)
428 Char *sp, *bp = buffer, *ebp = &buffer[MAXPATHLEN];
430 for (sp = str; *sp && *sp != sep && bp < ebp;)
431 if (sp[0] == '$' && sp[1] == ':' && Isdigit(sp[sp[2] == '-' ? 3 : 2])) {
438 for (num = *sp++ - '0'; Isdigit(*sp); num += 10 * num + *sp++ - '0')
442 if (num >= 0 && num < nwl) {
444 for (ptr = wl[num]; *ptr && bp < ebp - 1; *bp++ = *ptr++)
457 /* Truncates data if WIDE_STRINGS */
458 stderror(ERR_COMPMIS, (int)sep, msg, short2str(str));
460 } /* end tw_dollar */
464 * Return the appropriate completion for the command
466 * valid completion strings are:
467 * p/<range>/<completion>/[<suffix>/] positional
468 * c/<pattern>/<completion>/[<suffix>/] current word ignore pattern
469 * C/<pattern>/<completion>/[<suffix>/] current word with pattern
470 * n/<pattern>/<completion>/[<suffix>/] next word
471 * N/<pattern>/<completion>/[<suffix>/] next-next word
474 tw_complete(line, word, pat, looking, suf)
475 Char *line, **word, **pat;
478 Char buf[MAXPATHLEN + 1], **vec, *ptr;
479 Char *wl[MAXPATHLEN/6];
480 static Char nomatch[2] = { (Char) ~0, 0x00 };
483 copyn(buf, line, MAXPATHLEN);
485 /* find the command */
486 if ((wl[0] = tw_tok(buf)) == NULL || wl[0] == INVPTR)
490 * look for hardwired command completions using a globbing
491 * search and for arguments using a normal search.
493 if ((vec = tw_find(wl[0], &completions, (looking == TW_COMMAND))) == NULL)
496 /* tokenize the line one more time :-( */
497 for (wordno = 1; (wl[wordno] = tw_tok(NULL)) != NULL &&
498 wl[wordno] != INVPTR; wordno++)
501 if (wl[wordno] == INVPTR) /* Found a meta character */
502 return TW_ZERO; /* de-activate completions */
506 for (i = 0; i < wordno; i++)
507 xprintf("'%s' ", short2str(wl[i]));
512 /* if the current word is empty move the last word to the next */
513 if (**word == '\0') {
522 xprintf(" w#: %d\n", wordno);
523 xprintf("line: %s\n", short2str(line));
524 xprintf(" cmd: %s\n", short2str(wl[0]));
525 xprintf("word: %s\n", short2str(*word));
526 xprintf("last: %s\n", wordno - 2 >= 0 ? short2str(wl[wordno-2]) : "n/a");
527 xprintf("this: %s\n", wordno - 1 >= 0 ? short2str(wl[wordno-1]) : "n/a");
530 for (;vec != NULL && (ptr = vec[0]) != NULL; vec++) {
531 Char ran[MAXPATHLEN+1],/* The pattern or range X/<range>/XXXX/ */
532 com[MAXPATHLEN+1],/* The completion X/XXXXX/<completion>/ */
533 *pos = NULL; /* scratch pointer */
535 Char sep; /* the command and separator characters */
541 xprintf("match %s\n", short2str(ptr));
544 switch (cmd = ptr[0]) {
546 pos = (wordno - 3 < 0) ? nomatch : wl[wordno - 3];
549 pos = (wordno - 2 < 0) ? nomatch : wl[wordno - 2];
553 pos = (wordno - 1 < 0) ? nomatch : wl[wordno - 1];
558 stderror(ERR_COMPINV, CGETS(27, 1, "command"), cmd);
564 /* Truncates data if WIDE_STRINGS */
565 stderror(ERR_COMPINV, CGETS(27, 2, "separator"), (int)sep);
569 ptr = tw_dollar(&ptr[2], wl, wordno, ran, sep,
570 CGETS(27, 3, "pattern"));
571 if (ran[0] == '\0') /* check for empty pattern (disallowed) */
573 stderror(ERR_COMPINC, cmd == 'p' ? CGETS(27, 4, "range") :
574 CGETS(27, 3, "pattern"), "");
578 ptr = tw_dollar(ptr, wl, wordno, com, sep, CGETS(27, 5, "completion"));
590 xprintf("command: %c\nseparator: %c\n", cmd, (int)sep);
591 xprintf("pattern: %s\n", short2str(ran));
592 xprintf("completion: %s\n", short2str(com));
596 xprintf("*auto suffix*\n");
599 xprintf("*no suffix*\n");
602 xprintf("%c\n", *suf);
608 case 'p': /* positional completion */
610 xprintf("p: tw_pos(%s, %d) = ", short2str(ran), wordno - 1);
611 xprintf("%d\n", tw_pos(ran, wordno - 1));
613 if (!tw_pos(ran, wordno - 1))
615 return tw_result(com, pat);
617 case 'N': /* match with the next-next word */
618 case 'n': /* match with the next word */
619 case 'c': /* match with the current word */
622 xprintf("%c: ", cmd);
624 if ((n = tw_match(pos, ran)) < 0)
628 return tw_result(com, pat);
631 return TW_ZERO; /* Cannot happen */
636 } /* end tw_complete */