2 * tw.init.c: Handle lists of things to complete
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
41 Char
**list
, /* List of command names */
42 *buff
; /* Space holding command names */
43 size_t nlist
, /* Number of items */
44 nbuff
, /* Current space in name buf */
45 tlist
, /* Total space in list */
46 tbuff
; /* Total space in name buf */
50 static struct varent
*tw_vptr
= NULL
; /* Current shell variable */
51 static Char
**tw_env
= NULL
; /* Current environment variable */
52 static const Char
*tw_word
; /* Current word pointer */
53 static struct KeyFuncs
*tw_bind
= NULL
; /* List of the bindings */
55 static struct limits
*tw_limit
= NULL
; /* List of the resource limits */
56 #endif /* HAVENOLIMIT */
57 static int tw_index
= 0; /* signal and job index */
58 static DIR *tw_dir_fd
= NULL
; /* Current directory descriptor */
59 static int tw_cmd_got
= 0; /* What we need to do */
60 static stringlist_t tw_cmd
= { NULL
, NULL
, 0, 0, 0, 0 };
61 static stringlist_t tw_item
= { NULL
, NULL
, 0, 0, 0, 0 };
62 #define TW_FL_CMD 0x01
63 #define TW_FL_ALIAS 0x02
64 #define TW_FL_BUILTIN 0x04
65 #define TW_FL_SORT 0x08
66 #define TW_FL_REL 0x10
68 static struct { /* Current element pointer */
69 size_t cur
; /* Current element number */
70 Char
**pathv
; /* Current element in path */
71 DIR *dfd
; /* Current directory descriptor */
78 if (tw_dir_fd != NULL) \
79 rewinddir(tw_dir_fd); \
87 disabled_cleanup(&pintr_disabled); \
90 static Char
*tw_str_add (stringlist_t
*, size_t);
91 static void tw_str_free (stringlist_t
*);
92 static int tw_dir_next (struct Strbuf
*, DIR *);
93 static void tw_cmd_add (const Char
*name
);
94 static void tw_cmd_cmd (void);
95 static void tw_cmd_builtin (void);
96 static void tw_cmd_alias (void);
97 static void tw_cmd_sort (void);
98 static void tw_vptr_start (struct varent
*);
102 * Add an item to the string list
105 tw_str_add(stringlist_t
*sl
, size_t len
)
109 if (sl
->tlist
<= sl
->nlist
) {
111 sl
->tlist
+= TW_INCR
;
112 sl
->list
= xrealloc(sl
->list
, sl
->tlist
* sizeof(Char
*));
113 disabled_cleanup(&pintr_disabled
);
115 if (sl
->tbuff
<= sl
->nbuff
+ len
) {
120 sl
->tbuff
+= TW_INCR
+ len
;
121 sl
->buff
= xrealloc(sl
->buff
, sl
->tbuff
* sizeof(Char
));
122 /* Re-thread the new pointer list, if changed */
123 if (ptr
!= NULL
&& ptr
!= sl
->buff
) {
124 for (i
= 0; i
< sl
->nlist
; i
++)
125 sl
->list
[i
] = sl
->buff
+ (sl
->list
[i
] - ptr
);
127 disabled_cleanup(&pintr_disabled
);
129 ptr
= sl
->list
[sl
->nlist
++] = &sl
->buff
[sl
->nbuff
];
139 tw_str_free(stringlist_t
*sl
)
145 sl
->tlist
= sl
->nlist
= 0;
150 sl
->tbuff
= sl
->nbuff
= 0;
152 disabled_cleanup(&pintr_disabled
);
153 } /* end tw_str_free */
157 tw_dir_next(struct Strbuf
*res
, DIR *dfd
)
164 if ((dirp
= readdir(dfd
)) != NULL
) {
165 Strbuf_append(res
, str2short(dirp
->d_name
));
169 } /* end tw_dir_next */
173 * Add the name to the command list
176 tw_cmd_add(const Char
*name
)
180 len
= Strlen(name
) + 2;
181 (void) Strcpy(tw_str_add(&tw_cmd
, len
), name
);
182 } /* end tw_cmd_add */
186 * Free the command list
192 tw_str_free(&tw_cmd
);
194 } /* end tw_cmd_free */
197 * Add system commands to the command list
204 Char
*dir
= NULL
, *name
;
206 struct varent
*v
= adrof(STRpath
);
207 struct varent
*recexec
= adrof(STRrecognize_only_executables
);
211 if (v
== NULL
|| v
->vec
== NULL
) /* if no path */
214 for (pv
= v
->vec
; *pv
; pv
++) {
215 if (pv
[0][0] != '/') {
216 tw_cmd_got
|= TW_FL_REL
;
220 if ((dirp
= opendir(short2str(*pv
))) == NULL
)
223 cleanup_push(dirp
, opendir_cleanup
);
225 dir
= Strspl(*pv
, STRslash
);
226 cleanup_push(dir
, xfree
);
228 while ((dp
= readdir(dirp
)) != NULL
) {
229 #if defined(_UWIN) || defined(__CYGWIN__)
230 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
231 * the file with the .exe, .com, .bat extension
233 * Same for Cygwin, but only for .exe and .com extension.
235 len
= strlen(dp
->d_name
);
236 if (len
> 4 && (strcmp(&dp
->d_name
[len
- 4], ".exe") == 0 ||
238 strcmp(&dp
->d_name
[len
- 4], ".bat") == 0 ||
239 #endif /* !__CYGWIN__ */
240 strcmp(&dp
->d_name
[len
- 4], ".com") == 0))
241 dp
->d_name
[len
- 4] = '\0';
242 #endif /* _UWIN || __CYGWIN__ */
243 /* the call to executable() may make this a bit slow */
244 name
= str2short(dp
->d_name
);
245 if (dp
->d_ino
== 0 || (recexec
&& !executable(dir
, name
, 0)))
248 if (name
[0] == '#' || /* emacs temp files */
249 name
[0] == '.' || /* .files */
250 name
[len
- 1] == '~' || /* emacs backups */
251 name
[len
- 1] == '%') /* textedit backups */
252 continue; /* Ignore! */
257 } /* end tw_cmd_cmd */
261 * Add builtins to the command list
266 const struct biltins
*bptr
;
268 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++)
270 tw_cmd_add(str2short(bptr
->bname
));
272 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++)
274 tw_cmd_add(str2short(bptr
->bname
));
275 #endif /* WINNT_NATIVE*/
276 } /* end tw_cmd_builtin */
280 * Add aliases to the command list
293 if (p
->v_parent
== 0) /* is it the header? */
296 tw_cmd_add(p
->v_name
);
304 } while (p
->v_right
== c
);
307 } /* end tw_cmd_alias */
311 * Sort the command list removing duplicate elements
320 qsort(tw_cmd
.list
, tw_cmd
.nlist
, sizeof(Char
*), fcompare
);
322 /* get rid of multiple entries */
323 for (i
= 0, fwd
= 0; i
+ 1 < tw_cmd
.nlist
; i
++) {
324 if (Strcmp(tw_cmd
.list
[i
], tw_cmd
.list
[i
+ 1]) == 0) /* garbage */
325 fwd
++; /* increase the forward ref. count */
327 tw_cmd
.list
[i
- fwd
] = tw_cmd
.list
[i
];
329 /* Fix fencepost error -- Theodore Ts'o <tytso@athena.mit.edu> */
331 tw_cmd
.list
[i
- fwd
] = tw_cmd
.list
[i
];
333 disabled_cleanup(&pintr_disabled
);
334 } /* end tw_cmd_sort */
338 * Get the command list and sort it, if not done yet.
339 * Reset the current pointer to the beginning of the command list
343 tw_cmd_start(DIR *dfd
, const Char
*pat
)
345 static Char
*defpath
[] = { STRNULL
, 0 };
348 if ((tw_cmd_got
& TW_FL_CMD
) == 0) {
351 tw_cmd_got
|= TW_FL_CMD
;
353 if ((tw_cmd_got
& TW_FL_ALIAS
) == 0) {
355 tw_cmd_got
&= ~TW_FL_SORT
;
356 tw_cmd_got
|= TW_FL_ALIAS
;
358 if ((tw_cmd_got
& TW_FL_BUILTIN
) == 0) {
360 tw_cmd_got
&= ~TW_FL_SORT
;
361 tw_cmd_got
|= TW_FL_BUILTIN
;
363 if ((tw_cmd_got
& TW_FL_SORT
) == 0) {
365 tw_cmd_got
|= TW_FL_SORT
;
368 tw_cmd_state
.cur
= 0;
369 CLRDIR(tw_cmd_state
.dfd
)
370 if (tw_cmd_got
& TW_FL_REL
) {
371 struct varent
*vp
= adrof(STRpath
);
373 tw_cmd_state
.pathv
= vp
->vec
;
375 tw_cmd_state
.pathv
= defpath
;
378 tw_cmd_state
.pathv
= defpath
;
383 * Return the next element in the command list or
384 * Look for commands in the relative path components
387 tw_cmd_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
392 if (tw_cmd_state
.cur
< tw_cmd
.nlist
) {
394 Strbuf_append(res
, tw_cmd
.list
[tw_cmd_state
.cur
++]);
399 * We need to process relatives in the path.
401 while ((tw_cmd_state
.dfd
== NULL
||
402 (res
->len
= 0, ret
= tw_dir_next(res
, tw_cmd_state
.dfd
)) == 0) &&
403 *tw_cmd_state
.pathv
!= NULL
) {
405 CLRDIR(tw_cmd_state
.dfd
)
407 while (*tw_cmd_state
.pathv
&& tw_cmd_state
.pathv
[0][0] == '/')
408 tw_cmd_state
.pathv
++;
409 if ((ptr
= *tw_cmd_state
.pathv
) != 0) {
411 Strbuf_append(res
, ptr
);
414 * We complete directories only on '.' should that
418 if (ptr
[0] == '\0' || (ptr
[0] == '.' && ptr
[1] == '\0')) {
419 tw_cmd_state
.dfd
= opendir(".");
420 *flags
= TW_DIR_OK
| TW_EXEC_CHK
;
423 Strbuf_append(dir
, *tw_cmd_state
.pathv
);
424 Strbuf_append1(dir
, '/');
425 tw_cmd_state
.dfd
= opendir(short2str(*tw_cmd_state
.pathv
));
426 *flags
= TW_EXEC_CHK
;
428 Strbuf_terminate(dir
);
429 tw_cmd_state
.pathv
++;
433 } /* end tw_cmd_next */
437 * Find the first variable in the variable list
440 tw_vptr_start(struct varent
*c
)
442 tw_vptr
= c
; /* start at beginning of variable list */
445 while (tw_vptr
->v_left
)
446 tw_vptr
= tw_vptr
->v_left
;
448 if (tw_vptr
->v_parent
== 0) { /* is it the header? */
453 return; /* found first one */
454 if (tw_vptr
->v_right
) {
455 tw_vptr
= tw_vptr
->v_right
;
460 tw_vptr
= tw_vptr
->v_parent
;
461 } while (tw_vptr
->v_right
== c
);
464 } /* end tw_shvar_start */
468 * Return the next shell variable
472 tw_shvar_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
479 if ((p
= tw_vptr
) == NULL
)
480 return 0; /* just in case */
482 Strbuf_append(res
, p
->v_name
); /* we know that this name is here now */
484 /* now find the next one */
486 if (p
->v_right
) { /* if we can go right */
491 else { /* else go up */
495 } while (p
->v_right
== c
);
497 if (p
->v_parent
== 0) { /* is it the header? */
502 tw_vptr
= p
; /* save state for the next call */
506 } /* end tw_shvar_next */
510 * Return the next environment variable
514 tw_envvar_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
520 if (tw_env
== NULL
|| *tw_env
== NULL
)
522 for (ps
= *tw_env
; *ps
&& *ps
!= '='; ps
++)
524 Strbuf_appendn(res
, *tw_env
, ps
- *tw_env
);
527 } /* end tw_envvar_next */
531 * Begin the list of the shell and environment variables
535 tw_var_start(DIR *dfd
, const Char
*pat
)
539 tw_vptr_start(&shvhed
);
540 tw_env
= STR_environ
;
541 } /* end tw_var_start */
545 * Begin the list of the shell aliases
549 tw_alias_start(DIR *dfd
, const Char
*pat
)
553 tw_vptr_start(&aliases
);
555 } /* tw_alias_start */
558 /* tw_complete_start():
559 * Begin the list of completions
563 tw_complete_start(DIR *dfd
, const Char
*pat
)
567 tw_vptr_start(&completions
);
569 } /* end tw_complete_start */
573 * Return the next shell or environment variable
576 tw_var_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
581 ret
= tw_shvar_next(res
, dir
, flags
);
582 if (ret
== 0 && tw_env
)
583 ret
= tw_envvar_next(res
, dir
, flags
);
585 } /* end tw_var_next */
588 /* tw_logname_start():
589 * Initialize lognames to the beginning of the list
593 tw_logname_start(DIR *dfd
, const Char
*pat
)
598 (void) setpwent(); /* Open passwd file */
600 } /* end tw_logname_start */
603 /* tw_logname_next():
604 * Return the next entry from the passwd file
608 tw_logname_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
613 * We don't want to get interrupted inside getpwent()
614 * because the yellow pages code is not interruptible,
615 * and if we call endpwent() immediatetely after
616 * (in pintr()) we may be freeing an invalid pointer
626 disabled_cleanup(&pintr_disabled
);
634 Strbuf_append(res
, str2short(pw
->pw_name
));
636 } /* end tw_logname_next */
640 * Close the passwd file to finish the logname list
651 } /* end tw_logname_end */
654 /* tw_grpname_start():
655 * Initialize grpnames to the beginning of the list
659 tw_grpname_start(DIR *dfd
, const Char
*pat
)
663 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE) && !defined (__ANDROID__)
664 (void) setgrent(); /* Open group file */
665 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
666 } /* end tw_grpname_start */
669 /* tw_grpname_next():
670 * Return the next entry from the group file
674 tw_grpname_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
679 * We don't want to get interrupted inside getgrent()
680 * because the yellow pages code is not interruptible,
681 * and if we call endgrent() immediatetely after
682 * (in pintr()) we may be freeing an invalid pointer
687 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE) && !defined(__ANDROID__)
689 while ((gr
= getgrent()) == NULL
&& errno
== EINTR
) {
690 handle_pending_signals();
693 #else /* _VMS_POSIX || _OSD_POSIX || WINNT_NATIVE */
695 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
696 disabled_cleanup(&pintr_disabled
);
704 Strbuf_append(res
, str2short(gr
->gr_name
));
706 } /* end tw_grpname_next */
710 * Close the group file to finish the groupname list
718 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE) && !defined (__ANDROID__)
720 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
721 } /* end tw_grpname_end */
724 * Initialize the directory for the file list
728 tw_file_start(DIR *dfd
, const Char
*pat
)
733 if ((vp
= adrof(STRcdpath
)) != NULL
)
735 } /* end tw_file_start */
739 * Return the next file in the directory
742 tw_file_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
744 int ret
= tw_dir_next(res
, tw_dir_fd
);
745 if (ret
== 0 && (*flags
& TW_DIR_OK
) != 0) {
747 while (tw_env
&& *tw_env
)
748 if ((tw_dir_fd
= opendir(short2str(*tw_env
))) != NULL
)
755 Strbuf_append(dir
, *tw_env
++);
756 Strbuf_append1(dir
, '/');
757 Strbuf_terminate(dir
);
758 ret
= tw_dir_next(res
, tw_dir_fd
);
762 } /* end tw_file_next */
766 * Clear directory related lists
772 CLRDIR(tw_cmd_state
.dfd
)
773 } /* end tw_dir_end */
782 tw_str_free(&tw_item
);
783 } /* end tw_item_free */
787 * Return the list of items
793 } /* end tw_item_get */
797 * Return a new item for a Strbuf_terminate()'d s
800 tw_item_add(const struct Strbuf
*s
)
804 p
= tw_str_add(&tw_item
, s
->len
+ 1);
810 * Find the string if it exists in the item list
814 tw_item_find(Char
*str
)
818 if (tw_item
.list
== NULL
|| str
== NULL
)
821 for (i
= 0; i
< tw_item
.nlist
; i
++)
822 if (tw_item
.list
[i
] != NULL
&& Strcmp(tw_item
.list
[i
], str
) == 0)
823 return tw_item
.list
[i
];
825 } /* end tw_item_find */
829 * Initialize a variable list
832 tw_vl_start(DIR *dfd
, const Char
*pat
)
835 if ((tw_vptr
= adrof(pat
)) != NULL
) {
836 tw_env
= tw_vptr
->vec
;
841 } /* end tw_vl_start */
845 * Initialize a word list
848 tw_wl_start(DIR *dfd
, const Char
*pat
)
852 } /* end tw_wl_start */
856 * Return the next word from the word list
860 tw_wl_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
866 if (tw_word
== NULL
|| tw_word
[0] == '\0')
869 while (*tw_word
&& Isspace(*tw_word
)) tw_word
++;
871 for (p
= tw_word
; *tw_word
&& !Isspace(*tw_word
); tw_word
++)
875 Strbuf_appendn(res
, p
, tw_word
- p
);
879 } /* end tw_wl_next */
883 * Begin the list of the shell bindings
887 tw_bind_start(DIR *dfd
, const Char
*pat
)
892 } /* end tw_bind_start */
896 * Begin the list of the shell bindings
900 tw_bind_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
904 if (tw_bind
&& tw_bind
->name
) {
907 for (ptr
= tw_bind
->name
; *ptr
!= '\0'; ptr
++)
908 Strbuf_append1(res
, *ptr
);
913 } /* end tw_bind_next */
917 * Begin the list of the shell limitings
921 tw_limit_start(DIR *dfd
, const Char
*pat
)
927 #endif /* ! HAVENOLIMIT */
928 } /* end tw_limit_start */
932 * Begin the list of the shell limitings
936 tw_limit_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
941 if (tw_limit
&& tw_limit
->limname
) {
944 for (ptr
= tw_limit
->limname
; *ptr
!= '\0'; ptr
++)
945 Strbuf_append1(res
, *ptr
);
949 #endif /* ! HAVENOLIMIT */
951 } /* end tw_limit_next */
955 * Begin the list of the shell sigings
959 tw_sig_start(DIR *dfd
, const Char
*pat
)
964 } /* end tw_sig_start */
968 * Begin the list of the shell sigings
972 tw_sig_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
976 for (;tw_index
< nsig
; tw_index
++) {
979 if (mesg
[tw_index
].iname
== NULL
)
982 for (ptr
= mesg
[tw_index
].iname
; *ptr
!= '\0'; ptr
++)
983 Strbuf_append1(res
, *ptr
);
988 } /* end tw_sig_next */
992 * Begin the list of the shell jobings
996 tw_job_start(DIR *dfd
, const Char
*pat
)
1001 } /* end tw_job_start */
1005 * Begin the list of the shell jobings
1009 tw_job_next(struct Strbuf
*res
, struct Strbuf
*dir
, int *flags
)
1015 for (;tw_index
<= pmaxindex
; tw_index
++) {
1016 for (j
= proclist
.p_next
; j
!= NULL
; j
= j
->p_next
)
1017 if (j
->p_index
== tw_index
&& j
->p_procid
== j
->p_jobid
)
1021 Strbuf_append(res
, j
->p_command
);
1026 } /* end tw_job_next */