1 /* $Header: /src/pub/tcsh/tw.init.c,v 3.34 2005/01/05 16:06:15 christos Exp $ */
3 * tw.init.c: Handle lists of things to complete
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.init.c,v 3.34 2005/01/05 16:06:15 christos Exp $")
45 Char **list, /* List of command names */
46 *buff; /* Space holding command names */
47 int nlist, /* Number of items */
48 nbuff, /* Current space in name buf */
49 tlist, /* Total space in list */
50 tbuff; /* Total space in name buf */
54 static struct varent *tw_vptr = NULL; /* Current shell variable */
55 static Char **tw_env = NULL; /* Current environment variable */
56 static Char *tw_word; /* Current word pointer */
57 static struct KeyFuncs *tw_bind = NULL; /* List of the bindings */
59 static struct limits *tw_limit = NULL; /* List of the resource limits */
60 #endif /* HAVENOLIMIT */
61 static int tw_index = 0; /* signal and job index */
62 static DIR *tw_dir_fd = NULL; /* Current directory descriptor */
63 static Char tw_retname[MAXPATHLEN+1]; /* Return buffer */
64 static int tw_cmd_got = 0; /* What we need to do */
65 static stringlist_t tw_cmd = { NULL, NULL, 0, 0, 0, 0 };
66 static stringlist_t tw_item = { NULL, NULL, 0, 0, 0, 0 };
67 #define TW_FL_CMD 0x01
68 #define TW_FL_ALIAS 0x02
69 #define TW_FL_BUILTIN 0x04
70 #define TW_FL_SORT 0x08
71 #define TW_FL_REL 0x10
73 static struct { /* Current element pointer */
74 int cur; /* Current element number */
75 Char **pathv; /* Current element in path */
76 DIR *dfd; /* Current directory descriptor */
81 static sigmask_t tw_omask;
82 # define TW_HOLD() tw_omask = sigblock(sigmask(SIGINT))
83 # define TW_RELS() (void) sigsetmask(tw_omask)
85 # define TW_HOLD() (void) sighold(SIGINT)
86 # define TW_RELS() (void) sigrelse(SIGINT)
92 if (tw_dir_fd != NULL) \
93 rewinddir(tw_dir_fd); \
99 (void) closedir(dfd); \
104 static Char *tw_str_add __P((stringlist_t *, int));
105 static void tw_str_free __P((stringlist_t *));
106 static Char *tw_dir_next __P((DIR *));
107 static void tw_cmd_add __P((const Char *name));
108 static void tw_cmd_cmd __P((void));
109 static void tw_cmd_builtin __P((void));
110 static void tw_cmd_alias __P((void));
111 static void tw_cmd_sort __P((void));
112 static void tw_vptr_start __P((struct varent *));
116 * Add an item to the string list
125 if (sl->tlist <= sl->nlist) {
127 sl->tlist += TW_INCR;
128 sl->list = sl->list ?
129 (Char **) xrealloc((ptr_t) sl->list,
130 (size_t) (sl->tlist * sizeof(Char *))) :
131 (Char **) xmalloc((size_t) (sl->tlist * sizeof(Char *)));
134 if (sl->tbuff <= sl->nbuff + len) {
139 sl->tbuff += TW_INCR + len;
140 sl->buff = sl->buff ?
141 (Char *) xrealloc((ptr_t) sl->buff,
142 (size_t) (sl->tbuff * sizeof(Char))) :
143 (Char *) xmalloc((size_t) (sl->tbuff * sizeof(Char)));
144 /* Re-thread the new pointer list, if changed */
145 if (ptr != NULL && ptr != sl->buff) {
146 intptr_t offs = sl->buff - ptr;
147 for (i = 0; i < sl->nlist; i++)
152 ptr = sl->list[sl->nlist++] = &sl->buff[sl->nbuff];
167 xfree((ptr_t) sl->list);
169 sl->tlist = sl->nlist = 0;
172 xfree((ptr_t) sl->buff);
174 sl->tbuff = sl->nbuff = 0;
177 } /* end tw_str_free */
189 if ((dirp = readdir(dfd)) != NULL) {
190 (void) Strcpy(tw_retname, str2short(dirp->d_name));
194 } /* end tw_dir_next */
198 * Add the name to the command list
206 len = (int) Strlen(name) + 2;
207 (void) Strcpy(tw_str_add(&tw_cmd, len), name);
208 } /* end tw_cmd_add */
212 * Free the command list
218 tw_str_free(&tw_cmd);
220 } /* end tw_cmd_free */
223 * Add system commands to the command list
230 Char *dir = NULL, *name;
232 struct varent *v = adrof(STRpath);
233 struct varent *recexec = adrof(STRrecognize_only_executables);
237 if (v == NULL || v->vec == NULL) /* if no path */
240 for (pv = v->vec; *pv; pv++) {
241 if (pv[0][0] != '/') {
242 tw_cmd_got |= TW_FL_REL;
246 if ((dirp = opendir(short2str(*pv))) == NULL)
250 dir = Strspl(*pv, STRslash);
251 while ((dp = readdir(dirp)) != NULL) {
252 #if defined(_UWIN) || defined(__CYGWIN__)
253 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
254 * the file with the .exe, .com, .bat extension
256 size_t ext = strlen(dp->d_name) - 4;
257 if ((ext > 0) && (strcmp(&dp->d_name[ext], ".exe") == 0 ||
258 strcmp(&dp->d_name[ext], ".bat") == 0 ||
259 strcmp(&dp->d_name[ext], ".com") == 0))
260 dp->d_name[ext] = '\0';
261 #endif /* _UWIN || __CYGWIN__ */
262 /* the call to executable() may make this a bit slow */
263 name = str2short(dp->d_name);
264 if (dp->d_ino == 0 || (recexec && !executable(dir, name, 0)))
266 len = (int) Strlen(name) + 2;
267 if (name[0] == '#' || /* emacs temp files */
268 name[0] == '.' || /* .files */
269 name[len - 3] == '~' || /* emacs backups */
270 name[len - 3] == '%') /* textedit backups */
271 continue; /* Ignore! */
274 (void) closedir(dirp);
278 } /* end tw_cmd_cmd */
282 * Add builtins to the command list
287 struct biltins *bptr;
289 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++)
291 tw_cmd_add(str2short(bptr->bname));
293 for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++)
295 tw_cmd_add(str2short(bptr->bname));
296 #endif /* WINNT_NATIVE*/
297 } /* end tw_cmd_builtin */
301 * Add aliases to the command list
314 if (p->v_parent == 0) /* is it the header? */
317 tw_cmd_add(p->v_name);
325 } while (p->v_right == c);
328 } /* end tw_cmd_alias */
332 * Sort the command list removing duplicate elements
341 qsort((ptr_t) tw_cmd.list, (size_t) tw_cmd.nlist, sizeof(Char *),
342 (int (*) __P((const void *, const void *))) fcompare);
344 /* get rid of multiple entries */
345 for (i = 0, fwd = 0; i < tw_cmd.nlist - 1; i++) {
346 if (Strcmp(tw_cmd.list[i], tw_cmd.list[i + 1]) == 0) /* garbage */
347 fwd++; /* increase the forward ref. count */
349 tw_cmd.list[i - fwd] = tw_cmd.list[i];
351 /* Fix fencepost error -- Theodore Ts'o <tytso@athena.mit.edu> */
353 tw_cmd.list[i - fwd] = tw_cmd.list[i];
356 } /* end tw_cmd_sort */
360 * Get the command list and sort it, if not done yet.
361 * Reset the current pointer to the beginning of the command list
365 tw_cmd_start(dfd, pat)
369 static Char *defpath[] = { STRNULL, 0 };
372 if ((tw_cmd_got & TW_FL_CMD) == 0) {
375 tw_cmd_got |= TW_FL_CMD;
377 if ((tw_cmd_got & TW_FL_ALIAS) == 0) {
379 tw_cmd_got &= ~TW_FL_SORT;
380 tw_cmd_got |= TW_FL_ALIAS;
382 if ((tw_cmd_got & TW_FL_BUILTIN) == 0) {
384 tw_cmd_got &= ~TW_FL_SORT;
385 tw_cmd_got |= TW_FL_BUILTIN;
387 if ((tw_cmd_got & TW_FL_SORT) == 0) {
389 tw_cmd_got |= TW_FL_SORT;
392 tw_cmd_state.cur = 0;
393 CLRDIR(tw_cmd_state.dfd)
394 if (tw_cmd_got & TW_FL_REL) {
395 struct varent *vp = adrof(STRpath);
397 tw_cmd_state.pathv = vp->vec;
399 tw_cmd_state.pathv = defpath;
402 tw_cmd_state.pathv = defpath;
407 * Return the next element in the command list or
408 * Look for commands in the relative path components
411 tw_cmd_next(dir, flags)
417 if (tw_cmd_state.cur < tw_cmd.nlist) {
419 return tw_cmd.list[tw_cmd_state.cur++];
423 * We need to process relatives in the path.
425 while (((tw_cmd_state.dfd == NULL) ||
426 ((ptr = tw_dir_next(tw_cmd_state.dfd)) == NULL)) &&
427 (*tw_cmd_state.pathv != NULL)) {
429 CLRDIR(tw_cmd_state.dfd)
431 while (*tw_cmd_state.pathv && tw_cmd_state.pathv[0][0] == '/')
432 tw_cmd_state.pathv++;
433 if ((ptr = *tw_cmd_state.pathv) != 0) {
435 * We complete directories only on '.' should that
438 if (ptr[0] == '\0' || (ptr[0] == '.' && ptr[1] == '\0')) {
440 tw_cmd_state.dfd = opendir(".");
441 *flags = TW_DIR_OK | TW_EXEC_CHK;
444 copyn(dir, *tw_cmd_state.pathv, FILSIZ);
445 catn(dir, STRslash, FILSIZ);
446 tw_cmd_state.dfd = opendir(short2str(*tw_cmd_state.pathv));
447 *flags = TW_EXEC_CHK;
449 tw_cmd_state.pathv++;
453 } /* end tw_cmd_next */
457 * Find the first variable in the variable list
463 tw_vptr = c; /* start at beginning of variable list */
466 while (tw_vptr->v_left)
467 tw_vptr = tw_vptr->v_left;
469 if (tw_vptr->v_parent == 0) { /* is it the header? */
474 return; /* found first one */
475 if (tw_vptr->v_right) {
476 tw_vptr = tw_vptr->v_right;
481 tw_vptr = tw_vptr->v_parent;
482 } while (tw_vptr->v_right == c);
485 } /* end tw_shvar_start */
489 * Return the next shell variable
493 tw_shvar_next(dir, flags)
503 if ((p = tw_vptr) == NULL)
504 return (NULL); /* just in case */
506 cp = p->v_name; /* we know that this name is here now */
508 /* now find the next one */
510 if (p->v_right) { /* if we can go right */
515 else { /* else go up */
519 } while (p->v_right == c);
521 if (p->v_parent == 0) { /* is it the header? */
526 tw_vptr = p; /* save state for the next call */
530 } /* end tw_shvar_next */
534 * Return the next environment variable
538 tw_envvar_next(dir, flags)
546 if (tw_env == NULL || *tw_env == NULL)
548 for (ps = *tw_env, pd = tw_retname;
549 *ps && *ps != '=' && pd <= &tw_retname[MAXPATHLEN]; *pd++ = *ps++)
554 } /* end tw_envvar_next */
558 * Begin the list of the shell and environment variables
562 tw_var_start(dfd, pat)
568 tw_vptr_start(&shvhed);
569 tw_env = STR_environ;
570 } /* end tw_var_start */
574 * Begin the list of the shell aliases
578 tw_alias_start(dfd, pat)
584 tw_vptr_start(&aliases);
586 } /* tw_alias_start */
589 /* tw_complete_start():
590 * Begin the list of completions
594 tw_complete_start(dfd, pat)
600 tw_vptr_start(&completions);
602 } /* end tw_complete_start */
606 * Return the next shell or environment variable
609 tw_var_next(dir, flags)
616 ptr = tw_shvar_next(dir, flags);
618 ptr = tw_envvar_next(dir, flags);
620 } /* end tw_var_next */
623 /* tw_logname_start():
624 * Initialize lognames to the beginning of the list
628 tw_logname_start(dfd, pat)
635 (void) setpwent(); /* Open passwd file */
637 } /* end tw_logname_start */
640 /* tw_logname_next():
641 * Return the next entry from the passwd file
645 tw_logname_next(dir, flags)
649 static Char retname[MAXPATHLEN];
652 * We don't want to get interrupted inside getpwent()
653 * because the yellow pages code is not interruptible,
654 * and if we call endpwent() immediatetely after
655 * (in pintr()) we may be freeing an invalid pointer
673 (void) Strcpy(retname, str2short(pw->pw_name));
675 } /* end tw_logname_next */
679 * Close the passwd file to finish the logname list
690 } /* end tw_logname_end */
693 /* tw_grpname_start():
694 * Initialize grpnames to the beginning of the list
698 tw_grpname_start(dfd, pat)
704 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
705 (void) setgrent(); /* Open group file */
706 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
707 } /* end tw_grpname_start */
710 /* tw_grpname_next():
711 * Return the next entry from the group file
715 tw_grpname_next(dir, flags)
719 static Char retname[MAXPATHLEN];
722 * We don't want to get interrupted inside getgrent()
723 * because the yellow pages code is not interruptible,
724 * and if we call endgrent() immediatetely after
725 * (in pintr()) we may be freeing an invalid pointer
730 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
731 gr = (struct group *) getgrent();
732 #else /* _VMS_POSIX || _OSD_POSIX || WINNT_NATIVE */
734 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
743 (void) Strcpy(retname, str2short(gr->gr_name));
745 } /* end tw_grpname_next */
749 * Close the group file to finish the groupname list
757 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
759 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
760 } /* end tw_grpname_end */
763 * Initialize the directory for the file list
767 tw_file_start(dfd, pat)
774 if ((vp = adrof(STRcdpath)) != NULL)
776 } /* end tw_file_start */
780 * Return the next file in the directory
783 tw_file_next(dir, flags)
787 Char *ptr = tw_dir_next(tw_dir_fd);
788 if (ptr == NULL && (*flags & TW_DIR_OK) != 0) {
790 while (tw_env && *tw_env)
791 if ((tw_dir_fd = opendir(short2str(*tw_env))) != NULL)
797 copyn(dir, *tw_env++, MAXPATHLEN);
798 catn(dir, STRslash, MAXPATHLEN);
799 ptr = tw_dir_next(tw_dir_fd);
803 } /* end tw_file_next */
807 * Clear directory related lists
813 CLRDIR(tw_cmd_state.dfd)
814 } /* end tw_dir_end */
823 tw_str_free(&tw_item);
824 } /* end tw_item_free */
828 * Return the list of items
834 } /* end tw_item_get */
844 return tw_str_add(&tw_item, len);
849 * Find the string if it exists in the item list
858 if (tw_item.list == NULL || str == NULL)
861 for (i = 0; i < tw_item.nlist; i++)
862 if (tw_item.list[i] != NULL && Strcmp(tw_item.list[i], str) == 0)
863 return tw_item.list[i];
865 } /* end tw_item_find */
869 * Initialize a variable list
872 tw_vl_start(dfd, pat)
877 if ((tw_vptr = adrof(pat)) != NULL) {
878 tw_env = tw_vptr->vec;
883 } /* end tw_vl_start */
887 * Initialize a word list
890 tw_wl_start(dfd, pat)
896 } /* end tw_wl_start */
900 * Return the next word from the word list
904 tw_wl_next(dir, flags)
909 if (tw_word == NULL || tw_word[0] == '\0')
912 while (*tw_word && Isspace(*tw_word)) tw_word++;
914 for (dir = tw_word; *tw_word && !Isspace(*tw_word); tw_word++)
918 return *dir ? dir : NULL;
919 } /* end tw_wl_next */
923 * Begin the list of the shell bindings
927 tw_bind_start(dfd, pat)
934 } /* end tw_bind_start */
938 * Begin the list of the shell bindings
942 tw_bind_next(dir, flags)
948 if (tw_bind && tw_bind->name) {
949 for (ptr = tw_bind->name, dir = tw_retname;
950 (*dir++ = (Char) *ptr++) != '\0';)
956 } /* end tw_bind_next */
960 * Begin the list of the shell limitings
964 tw_limit_start(dfd, pat)
972 #endif /* ! HAVENOLIMIT */
973 } /* end tw_limit_start */
977 * Begin the list of the shell limitings
981 tw_limit_next(dir, flags)
987 if (tw_limit && tw_limit->limname) {
988 for (ptr = tw_limit->limname, dir = tw_retname;
989 (*dir++ = (Char) *ptr++) != '\0';)
994 #endif /* ! HAVENOLIMIT */
997 } /* end tw_limit_next */
1001 * Begin the list of the shell sigings
1005 tw_sig_start(dfd, pat)
1012 } /* end tw_sig_start */
1016 * Begin the list of the shell sigings
1020 tw_sig_next(dir, flags)
1026 for (;tw_index < nsig; tw_index++) {
1028 if (mesg[tw_index].iname == NULL)
1031 for (ptr = mesg[tw_index].iname, dir = tw_retname;
1032 (*dir++ = (Char) *ptr++) != '\0';)
1038 } /* end tw_sig_next */
1042 * Begin the list of the shell jobings
1046 tw_job_start(dfd, pat)
1053 } /* end tw_job_start */
1057 * Begin the list of the shell jobings
1061 tw_job_next(dir, flags)
1069 for (;tw_index <= pmaxindex; tw_index++) {
1070 for (j = proclist.p_next; j != NULL; j = j->p_next)
1071 if (j->p_index == tw_index && j->p_procid == j->p_jobid)
1075 for (ptr = j->p_command, dir = tw_retname; (*dir++ = *ptr++) != '\0';)
1082 } /* end tw_job_next */