2 * tw.comp.c: File completion builtin
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 struct varent completions
;
40 static int tw_result (const Char
*, Char
**);
41 static Char
**tw_find (Char
*, struct varent
*, int);
42 static Char
*tw_tok (Char
*);
43 static int tw_pos (Char
*, int);
44 static void tw_pr (Char
**);
45 static int tw_match (const Char
*, const Char
*, int);
46 static void tw_prlist (struct varent
*);
47 static const Char
*tw_dollar (const Char
*,Char
**, size_t, Char
**,
51 * Add or list completions in the completion list
55 docomplete(Char
**v
, struct command
*t
)
65 tw_prlist(&completions
);
67 vp
= adrof1(strip(p
), &completions
);
69 tw_pr(vp
->vec
), xputchar('\n');
73 xprintf("tw_find(%s) \n", short2str(strip(p
)));
75 pp
= tw_find(strip(p
), &completions
, FALSE
);
77 tw_pr(pp
), xputchar('\n');
81 set1(strip(p
), saveblk(v
), &completions
, VAR_READWRITE
);
82 } /* end docomplete */
86 * Remove completions from the completion list
90 douncomplete(Char
**v
, struct command
*t
)
93 unset1(v
, &completions
);
94 } /* end douncomplete */
98 * Pretty print a list of variables
101 tw_prlist(struct varent
*p
)
109 if (p
->v_parent
== 0) /* is it the header? */
112 int old_pintr_disabled
;
114 pintr_push_enable(&old_pintr_disabled
);
115 cleanup_until(&old_pintr_disabled
);
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.
144 for (; *cmp
; cmp
++) {
146 for (osp
= 0, ptr
= *cmp
; *ptr
; ptr
++) {
161 * Find the first matching completion.
162 * For commands we only look at names that start with -
165 tw_find(Char
*nam
, struct varent
*vp
, int cmd
)
169 for (vp
= vp
->v_left
; vp
; vp
= vp
->v_right
) {
170 if (vp
->v_left
&& (rv
= tw_find(nam
, vp
, cmd
)) != NULL
)
173 if (vp
->v_name
[0] != '-')
175 if (Gmatch(nam
, &vp
->v_name
[1]) && vp
->vec
!= NULL
)
179 if (Gmatch(nam
, vp
->v_name
) && vp
->vec
!= NULL
)
187 * Return true if the position is within the specified range
190 tw_pos(Char
*ran
, int wno
)
194 if (ran
[0] == '*' && ran
[1] == '\0')
197 for (p
= ran
; *p
&& *p
!= '-'; p
++)
200 if (*p
== '\0') /* range == <number> */
201 return wno
== getn(ran
);
203 if (ran
== p
) /* range = - <number> */
204 return wno
<= getn(&ran
[1]);
207 if (*p
== '\0') /* range = <number> - */
208 return getn(ran
) <= wno
;
209 else /* range = <number> - <number> */
210 return (getn(ran
) <= wno
) && (wno
<= getn(p
));
215 * Return the next word from string, unquoteing it.
220 static Char
*bf
= NULL
;
225 /* skip leading spaces */
226 for (; *bf
&& Isspace(*bf
); bf
++)
229 for (str
= bf
; *bf
&& !Isspace(*bf
); bf
++) {
237 return *str
? str
: NULL
;
242 * Match a string against the pattern given.
243 * and return the number of matched characters
244 * in a prefix of the string.
247 tw_match(const Char
*str
, const Char
*pat
, int exact
)
250 int rv
= exact
? Gmatch(estr
= str
, pat
) : Gnmatch(str
, pat
, &estr
);
252 xprintf("G%smatch(%s, ", exact
? "" : "n", short2str(str
));
253 xprintf("%s, ", short2str(pat
));
254 xprintf("%s) = %d [%" TCSH_PTRDIFF_T_FMT
"d]\n", short2str(estr
), rv
,
257 return (int) (rv
? estr
- str
: -1);
262 * Return what the completion action should be depending on the
266 tw_result(const Char
*act
, Char
**pat
)
269 static Char
* res
= NULL
;
273 xfree(res
), res
= NULL
;
275 switch (act
[0] & ~QUOTE
) {
277 looking
= TW_COMPLETION
;
286 looking
= TW_BINDING
;
289 looking
= TW_COMMAND
;
292 looking
= TW_PATH
| TW_COMMAND
;
295 looking
= TW_DIRECTORY
;
298 looking
= TW_PATH
| TW_DIRECTORY
;
310 looking
= TW_PATH
| TW_FILE
;
313 looking
= TW_GRPNAME
;
325 looking
= TW_SHELLVAR
;
331 looking
= TW_PATH
| TW_TEXT
;
334 looking
= TW_VARIABLE
;
340 looking
= TW_EXPLAIN
;
344 *pat
= res
= Strsave(&act
[1]);
349 *pat
= res
= Strsave(&act
[1]);
350 if ((p
= Strchr(res
, ')')) != NULL
)
357 if ((p
= Strchr(&res
[1], '`')) != NULL
)
362 * Make sure that we have some file descriptors to
363 * play with, so that the processes have at least 0, 1, 2
366 (void) dcopy(SHIN
, 0);
367 (void) dcopy(SHOUT
, 1);
368 (void) dcopy(SHDIAG
, 2);
370 if ((p
= globone(res
, G_APPEND
)) != NULL
) {
371 xfree(res
), res
= NULL
;
372 *pat
= res
= Strsave(p
);
379 stderror(ERR_COMPCOM
, short2str(act
));
383 switch (act
[1] & ~QUOTE
) {
388 *pat
= res
= Strsave(&act
[2]);
393 stderror(ERR_COMPCOM
, short2str(act
));
396 } /* end tw_result */
400 * Expand $<n> args in buffer
403 tw_dollar(const Char
*str
, Char
**wl
, size_t nwl
, Char
**result
, Char sep
,
406 struct Strbuf buf
= Strbuf_INIT
;
410 for (sp
= str
; *sp
&& *sp
!= sep
;)
411 if (sp
[0] == '$' && sp
[1] == ':' && Isdigit(sp
[sp
[2] == '-' ? 3 : 2])) {
418 for (num
= *sp
++ - '0'; Isdigit(*sp
); num
+= 10 * num
+ *sp
++ - '0')
422 if (num
>= 0 && (size_t)num
< nwl
)
423 Strbuf_append(&buf
, wl
[num
]);
426 Strbuf_append1(&buf
, *sp
++);
428 res
= Strbuf_finish(&buf
);
436 /* Truncates data if WIDE_STRINGS */
437 stderror(ERR_COMPMIS
, (int)sep
, msg
, short2str(str
));
439 } /* end tw_dollar */
443 * Return the appropriate completion for the command
445 * valid completion strings are:
446 * p/<range>/<completion>/[<suffix>/] positional
447 * c/<pattern>/<completion>/[<suffix>/] current word ignore pattern
448 * C/<pattern>/<completion>/[<suffix>/] current word with pattern
449 * n/<pattern>/<completion>/[<suffix>/] next word
450 * N/<pattern>/<completion>/[<suffix>/] next-next word
453 tw_complete(const Char
*line
, Char
**word
, Char
**pat
, int looking
, eChar
*suf
)
455 Char
*buf
, **vec
, **wl
;
456 static Char nomatch
[2] = { (Char
) ~0, 0x00 };
462 cleanup_push(buf
, xfree
);
463 /* Single-character words, empty current word, terminating NULL */
464 wl
= xmalloc(((Strlen(line
) + 1) / 2 + 2) * sizeof (*wl
));
465 cleanup_push(wl
, xfree
);
467 /* find the command */
468 if ((wl
[0] = tw_tok(buf
)) == NULL
|| wl
[0] == INVPTR
) {
474 * look for hardwired command completions using a globbing
475 * search and for arguments using a normal search.
477 if ((vec
= tw_find(wl
[0], &completions
, (looking
== TW_COMMAND
)))
483 /* tokenize the line one more time :-( */
484 for (wordno
= 1; (wl
[wordno
] = tw_tok(NULL
)) != NULL
&&
485 wl
[wordno
] != INVPTR
; wordno
++)
488 if (wl
[wordno
] == INVPTR
) { /* Found a meta character */
490 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#: %lu\n", (unsigned long)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 ? short2str(wl
[wordno
-2]) : "n/a");
516 xprintf("this: %s\n", wordno
>= 1 ? short2str(wl
[wordno
-1]) : "n/a");
519 for (;vec
!= NULL
&& (ptr
= vec
[0]) != NULL
; vec
++) {
520 Char
*ran
, /* The pattern or range X/<range>/XXXX/ */
521 *com
, /* The completion X/XXXXX/<completion>/ */
522 *pos
= NULL
; /* scratch pointer */
524 Char sep
; /* the command and separator characters */
531 xprintf("match %s\n", short2str(ptr
));
534 switch (cmd
= ptr
[0]) {
536 pos
= (wordno
< 3) ? nomatch
: wl
[wordno
- 3];
539 pos
= (wordno
< 2) ? nomatch
: wl
[wordno
- 2];
543 pos
= (wordno
< 1) ? nomatch
: wl
[wordno
- 1];
548 stderror(ERR_COMPINV
, CGETS(27, 1, "command"), cmd
);
554 /* Truncates data if WIDE_STRINGS */
555 stderror(ERR_COMPINV
, CGETS(27, 2, "separator"), (int)sep
);
559 ptr
= tw_dollar(&ptr
[2], wl
, wordno
, &ran
, sep
,
560 CGETS(27, 3, "pattern"));
561 cleanup_push(ran
, xfree
);
562 if (ran
[0] == '\0') /* check for empty pattern (disallowed) */
564 stderror(ERR_COMPINC
, cmd
== 'p' ? CGETS(27, 4, "range") :
565 CGETS(27, 3, "pattern"), "");
569 ptr
= tw_dollar(ptr
, wl
, wordno
, &com
, sep
,
570 CGETS(27, 5, "completion"));
571 cleanup_push(com
, xfree
);
583 xprintf("command: %c\nseparator: %c\n", cmd
, (int)sep
);
584 xprintf("pattern: %s\n", short2str(ran
));
585 xprintf("completion: %s\n", short2str(com
));
589 xprintf("*auto suffix*\n");
592 xprintf("*no suffix*\n");
595 xprintf("%c\n", (int)*suf
);
602 case 'p': /* positional completion */
604 xprintf("p: tw_pos(%s, %lu) = ", short2str(ran
),
605 (unsigned long)wordno
- 1);
606 xprintf("%d\n", tw_pos(ran
, wordno
- 1));
608 if (!tw_pos(ran
, wordno
- 1)) {
614 case 'N': /* match with the next-next word */
615 case 'n': /* match with the next word */
618 case 'c': /* match with the current word */
621 xprintf("%c: ", cmd
);
623 if ((n
= tw_match(pos
, ran
, exact
)) < 0) {
632 abort(); /* Cannot happen */
634 tsetenv(STRCOMMAND_LINE
, line
);
635 res
= tw_result(com
, pat
);
636 Unsetenv(STRCOMMAND_LINE
);
643 } /* end tw_complete */