1 /* $Header: /p/tcsh/cvsroot/tcsh/tw.init.c,v 3.42 2011/04/17 14:49:30 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("$tcsh: tw.init.c,v 3.42 2011/04/17 14:49:30 christos Exp $")
45 Char **list, /* List of command names */
46 *buff; /* Space holding command names */
47 size_t 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 const 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 int tw_cmd_got = 0; /* What we need to do */
64 static stringlist_t tw_cmd = { NULL, NULL, 0, 0, 0, 0 };
65 static stringlist_t tw_item = { NULL, NULL, 0, 0, 0, 0 };
66 #define TW_FL_CMD 0x01
67 #define TW_FL_ALIAS 0x02
68 #define TW_FL_BUILTIN 0x04
69 #define TW_FL_SORT 0x08
70 #define TW_FL_REL 0x10
72 static struct { /* Current element pointer */
73 size_t cur; /* Current element number */
74 Char **pathv; /* Current element in path */
75 DIR *dfd; /* Current directory descriptor */
82 if (tw_dir_fd != NULL) \
83 rewinddir(tw_dir_fd); \
91 disabled_cleanup(&pintr_disabled); \
94 static Char *tw_str_add (stringlist_t *, size_t);
95 static void tw_str_free (stringlist_t *);
96 static int tw_dir_next (struct Strbuf *, DIR *);
97 static void tw_cmd_add (const Char *name);
98 static void tw_cmd_cmd (void);
99 static void tw_cmd_builtin (void);
100 static void tw_cmd_alias (void);
101 static void tw_cmd_sort (void);
102 static void tw_vptr_start (struct varent *);
106 * Add an item to the string list
109 tw_str_add(stringlist_t *sl, size_t len)
113 if (sl->tlist <= sl->nlist) {
115 sl->tlist += TW_INCR;
116 sl->list = xrealloc(sl->list, sl->tlist * sizeof(Char *));
117 disabled_cleanup(&pintr_disabled);
119 if (sl->tbuff <= sl->nbuff + len) {
124 sl->tbuff += TW_INCR + len;
125 sl->buff = xrealloc(sl->buff, sl->tbuff * sizeof(Char));
126 /* Re-thread the new pointer list, if changed */
127 if (ptr != NULL && ptr != sl->buff) {
128 for (i = 0; i < sl->nlist; i++)
129 sl->list[i] = sl->buff + (sl->list[i] - ptr);
131 disabled_cleanup(&pintr_disabled);
133 ptr = sl->list[sl->nlist++] = &sl->buff[sl->nbuff];
143 tw_str_free(stringlist_t *sl)
149 sl->tlist = sl->nlist = 0;
154 sl->tbuff = sl->nbuff = 0;
156 disabled_cleanup(&pintr_disabled);
157 } /* end tw_str_free */
161 tw_dir_next(struct Strbuf *res, DIR *dfd)
168 if ((dirp = readdir(dfd)) != NULL) {
169 Strbuf_append(res, str2short(dirp->d_name));
173 } /* end tw_dir_next */
177 * Add the name to the command list
180 tw_cmd_add(const Char *name)
184 len = Strlen(name) + 2;
185 (void) Strcpy(tw_str_add(&tw_cmd, len), name);
186 } /* end tw_cmd_add */
190 * Free the command list
196 tw_str_free(&tw_cmd);
198 } /* end tw_cmd_free */
201 * Add system commands to the command list
208 Char *dir = NULL, *name;
210 struct varent *v = adrof(STRpath);
211 struct varent *recexec = adrof(STRrecognize_only_executables);
215 if (v == NULL || v->vec == NULL) /* if no path */
218 for (pv = v->vec; *pv; pv++) {
219 if (pv[0][0] != '/') {
220 tw_cmd_got |= TW_FL_REL;
224 if ((dirp = opendir(short2str(*pv))) == NULL)
227 cleanup_push(dirp, opendir_cleanup);
229 dir = Strspl(*pv, STRslash);
230 cleanup_push(dir, xfree);
232 while ((dp = readdir(dirp)) != NULL) {
233 #if defined(_UWIN) || defined(__CYGWIN__)
234 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
235 * the file with the .exe, .com, .bat extension
237 * Same for Cygwin, but only for .exe and .com extension.
239 len = strlen(dp->d_name);
240 if (len > 4 && (strcmp(&dp->d_name[len - 4], ".exe") == 0 ||
242 strcmp(&dp->d_name[len - 4], ".bat") == 0 ||
243 #endif /* !__CYGWIN__ */
244 strcmp(&dp->d_name[len - 4], ".com") == 0))
245 dp->d_name[len - 4] = '\0';
246 #endif /* _UWIN || __CYGWIN__ */
247 /* the call to executable() may make this a bit slow */
248 name = str2short(dp->d_name);
249 if (dp->d_ino == 0 || (recexec && !executable(dir, name, 0)))
252 if (name[0] == '#' || /* emacs temp files */
253 name[0] == '.' || /* .files */
254 name[len - 1] == '~' || /* emacs backups */
255 name[len - 1] == '%') /* textedit backups */
256 continue; /* Ignore! */
261 } /* end tw_cmd_cmd */
265 * Add builtins to the command list
270 const struct biltins *bptr;
272 for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++)
274 tw_cmd_add(str2short(bptr->bname));
276 for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++)
278 tw_cmd_add(str2short(bptr->bname));
279 #endif /* WINNT_NATIVE*/
280 } /* end tw_cmd_builtin */
284 * Add aliases to the command list
297 if (p->v_parent == 0) /* is it the header? */
300 tw_cmd_add(p->v_name);
308 } while (p->v_right == c);
311 } /* end tw_cmd_alias */
315 * Sort the command list removing duplicate elements
324 qsort(tw_cmd.list, tw_cmd.nlist, sizeof(Char *), fcompare);
326 /* get rid of multiple entries */
327 for (i = 0, fwd = 0; i + 1 < tw_cmd.nlist; i++) {
328 if (Strcmp(tw_cmd.list[i], tw_cmd.list[i + 1]) == 0) /* garbage */
329 fwd++; /* increase the forward ref. count */
331 tw_cmd.list[i - fwd] = tw_cmd.list[i];
333 /* Fix fencepost error -- Theodore Ts'o <tytso@athena.mit.edu> */
335 tw_cmd.list[i - fwd] = tw_cmd.list[i];
337 disabled_cleanup(&pintr_disabled);
338 } /* end tw_cmd_sort */
342 * Get the command list and sort it, if not done yet.
343 * Reset the current pointer to the beginning of the command list
347 tw_cmd_start(DIR *dfd, const Char *pat)
349 static Char *defpath[] = { STRNULL, 0 };
352 if ((tw_cmd_got & TW_FL_CMD) == 0) {
355 tw_cmd_got |= TW_FL_CMD;
357 if ((tw_cmd_got & TW_FL_ALIAS) == 0) {
359 tw_cmd_got &= ~TW_FL_SORT;
360 tw_cmd_got |= TW_FL_ALIAS;
362 if ((tw_cmd_got & TW_FL_BUILTIN) == 0) {
364 tw_cmd_got &= ~TW_FL_SORT;
365 tw_cmd_got |= TW_FL_BUILTIN;
367 if ((tw_cmd_got & TW_FL_SORT) == 0) {
369 tw_cmd_got |= TW_FL_SORT;
372 tw_cmd_state.cur = 0;
373 CLRDIR(tw_cmd_state.dfd)
374 if (tw_cmd_got & TW_FL_REL) {
375 struct varent *vp = adrof(STRpath);
377 tw_cmd_state.pathv = vp->vec;
379 tw_cmd_state.pathv = defpath;
382 tw_cmd_state.pathv = defpath;
387 * Return the next element in the command list or
388 * Look for commands in the relative path components
391 tw_cmd_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
396 if (tw_cmd_state.cur < tw_cmd.nlist) {
398 Strbuf_append(res, tw_cmd.list[tw_cmd_state.cur++]);
403 * We need to process relatives in the path.
405 while ((tw_cmd_state.dfd == NULL ||
406 (res->len = 0, ret = tw_dir_next(res, tw_cmd_state.dfd)) == 0) &&
407 *tw_cmd_state.pathv != NULL) {
409 CLRDIR(tw_cmd_state.dfd)
411 while (*tw_cmd_state.pathv && tw_cmd_state.pathv[0][0] == '/')
412 tw_cmd_state.pathv++;
413 if ((ptr = *tw_cmd_state.pathv) != 0) {
415 Strbuf_append(res, ptr);
418 * We complete directories only on '.' should that
422 if (ptr[0] == '\0' || (ptr[0] == '.' && ptr[1] == '\0')) {
423 tw_cmd_state.dfd = opendir(".");
424 *flags = TW_DIR_OK | TW_EXEC_CHK;
427 Strbuf_append(dir, *tw_cmd_state.pathv);
428 Strbuf_append1(dir, '/');
429 tw_cmd_state.dfd = opendir(short2str(*tw_cmd_state.pathv));
430 *flags = TW_EXEC_CHK;
432 Strbuf_terminate(dir);
433 tw_cmd_state.pathv++;
437 } /* end tw_cmd_next */
441 * Find the first variable in the variable list
444 tw_vptr_start(struct varent *c)
446 tw_vptr = c; /* start at beginning of variable list */
449 while (tw_vptr->v_left)
450 tw_vptr = tw_vptr->v_left;
452 if (tw_vptr->v_parent == 0) { /* is it the header? */
457 return; /* found first one */
458 if (tw_vptr->v_right) {
459 tw_vptr = tw_vptr->v_right;
464 tw_vptr = tw_vptr->v_parent;
465 } while (tw_vptr->v_right == c);
468 } /* end tw_shvar_start */
472 * Return the next shell variable
476 tw_shvar_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
483 if ((p = tw_vptr) == NULL)
484 return 0; /* just in case */
486 Strbuf_append(res, p->v_name); /* we know that this name is here now */
488 /* now find the next one */
490 if (p->v_right) { /* if we can go right */
495 else { /* else go up */
499 } while (p->v_right == c);
501 if (p->v_parent == 0) { /* is it the header? */
506 tw_vptr = p; /* save state for the next call */
510 } /* end tw_shvar_next */
514 * Return the next environment variable
518 tw_envvar_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
524 if (tw_env == NULL || *tw_env == NULL)
526 for (ps = *tw_env; *ps && *ps != '='; ps++)
528 Strbuf_appendn(res, *tw_env, ps - *tw_env);
531 } /* end tw_envvar_next */
535 * Begin the list of the shell and environment variables
539 tw_var_start(DIR *dfd, const Char *pat)
543 tw_vptr_start(&shvhed);
544 tw_env = STR_environ;
545 } /* end tw_var_start */
549 * Begin the list of the shell aliases
553 tw_alias_start(DIR *dfd, const Char *pat)
557 tw_vptr_start(&aliases);
559 } /* tw_alias_start */
562 /* tw_complete_start():
563 * Begin the list of completions
567 tw_complete_start(DIR *dfd, const Char *pat)
571 tw_vptr_start(&completions);
573 } /* end tw_complete_start */
577 * Return the next shell or environment variable
580 tw_var_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
585 ret = tw_shvar_next(res, dir, flags);
586 if (ret == 0 && tw_env)
587 ret = tw_envvar_next(res, dir, flags);
589 } /* end tw_var_next */
592 /* tw_logname_start():
593 * Initialize lognames to the beginning of the list
597 tw_logname_start(DIR *dfd, const Char *pat)
602 (void) setpwent(); /* Open passwd file */
604 } /* end tw_logname_start */
607 /* tw_logname_next():
608 * Return the next entry from the passwd file
612 tw_logname_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
617 * We don't want to get interrupted inside getpwent()
618 * because the yellow pages code is not interruptible,
619 * and if we call endpwent() immediatetely after
620 * (in pintr()) we may be freeing an invalid pointer
630 disabled_cleanup(&pintr_disabled);
638 Strbuf_append(res, str2short(pw->pw_name));
640 } /* end tw_logname_next */
644 * Close the passwd file to finish the logname list
655 } /* end tw_logname_end */
658 /* tw_grpname_start():
659 * Initialize grpnames to the beginning of the list
663 tw_grpname_start(DIR *dfd, const Char *pat)
667 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE) && !defined (__ANDROID__)
668 (void) setgrent(); /* Open group file */
669 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
670 } /* end tw_grpname_start */
673 /* tw_grpname_next():
674 * Return the next entry from the group file
678 tw_grpname_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
683 * We don't want to get interrupted inside getgrent()
684 * because the yellow pages code is not interruptible,
685 * and if we call endgrent() immediatetely after
686 * (in pintr()) we may be freeing an invalid pointer
691 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE) && !defined(__ANDROID__)
693 while ((gr = getgrent()) == NULL && errno == EINTR) {
694 handle_pending_signals();
697 #else /* _VMS_POSIX || _OSD_POSIX || WINNT_NATIVE */
699 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
700 disabled_cleanup(&pintr_disabled);
708 Strbuf_append(res, str2short(gr->gr_name));
710 } /* end tw_grpname_next */
714 * Close the group file to finish the groupname list
722 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE) && !defined (__ANDROID__)
724 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
725 } /* end tw_grpname_end */
728 * Initialize the directory for the file list
732 tw_file_start(DIR *dfd, const Char *pat)
737 if ((vp = adrof(STRcdpath)) != NULL)
739 } /* end tw_file_start */
743 * Return the next file in the directory
746 tw_file_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
748 int ret = tw_dir_next(res, tw_dir_fd);
749 if (ret == 0 && (*flags & TW_DIR_OK) != 0) {
751 while (tw_env && *tw_env)
752 if ((tw_dir_fd = opendir(short2str(*tw_env))) != NULL)
759 Strbuf_append(dir, *tw_env++);
760 Strbuf_append1(dir, '/');
761 Strbuf_terminate(dir);
762 ret = tw_dir_next(res, tw_dir_fd);
766 } /* end tw_file_next */
770 * Clear directory related lists
776 CLRDIR(tw_cmd_state.dfd)
777 } /* end tw_dir_end */
786 tw_str_free(&tw_item);
787 } /* end tw_item_free */
791 * Return the list of items
797 } /* end tw_item_get */
801 * Return a new item for a Strbuf_terminate()'d s
804 tw_item_add(const struct Strbuf *s)
808 p = tw_str_add(&tw_item, s->len + 1);
814 * Find the string if it exists in the item list
818 tw_item_find(Char *str)
822 if (tw_item.list == NULL || str == NULL)
825 for (i = 0; i < tw_item.nlist; i++)
826 if (tw_item.list[i] != NULL && Strcmp(tw_item.list[i], str) == 0)
827 return tw_item.list[i];
829 } /* end tw_item_find */
833 * Initialize a variable list
836 tw_vl_start(DIR *dfd, const Char *pat)
839 if ((tw_vptr = adrof(pat)) != NULL) {
840 tw_env = tw_vptr->vec;
845 } /* end tw_vl_start */
849 * Initialize a word list
852 tw_wl_start(DIR *dfd, const Char *pat)
856 } /* end tw_wl_start */
860 * Return the next word from the word list
864 tw_wl_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
870 if (tw_word == NULL || tw_word[0] == '\0')
873 while (*tw_word && Isspace(*tw_word)) tw_word++;
875 for (p = tw_word; *tw_word && !Isspace(*tw_word); tw_word++)
879 Strbuf_appendn(res, p, tw_word - p);
883 } /* end tw_wl_next */
887 * Begin the list of the shell bindings
891 tw_bind_start(DIR *dfd, const Char *pat)
896 } /* end tw_bind_start */
900 * Begin the list of the shell bindings
904 tw_bind_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
908 if (tw_bind && tw_bind->name) {
911 for (ptr = tw_bind->name; *ptr != '\0'; ptr++)
912 Strbuf_append1(res, *ptr);
917 } /* end tw_bind_next */
921 * Begin the list of the shell limitings
925 tw_limit_start(DIR *dfd, const Char *pat)
931 #endif /* ! HAVENOLIMIT */
932 } /* end tw_limit_start */
936 * Begin the list of the shell limitings
940 tw_limit_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
945 if (tw_limit && tw_limit->limname) {
948 for (ptr = tw_limit->limname; *ptr != '\0'; ptr++)
949 Strbuf_append1(res, *ptr);
953 #endif /* ! HAVENOLIMIT */
955 } /* end tw_limit_next */
959 * Begin the list of the shell sigings
963 tw_sig_start(DIR *dfd, const Char *pat)
968 } /* end tw_sig_start */
972 * Begin the list of the shell sigings
976 tw_sig_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
980 for (;tw_index < nsig; tw_index++) {
983 if (mesg[tw_index].iname == NULL)
986 for (ptr = mesg[tw_index].iname; *ptr != '\0'; ptr++)
987 Strbuf_append1(res, *ptr);
992 } /* end tw_sig_next */
996 * Begin the list of the shell jobings
1000 tw_job_start(DIR *dfd, const Char *pat)
1005 } /* end tw_job_start */
1009 * Begin the list of the shell jobings
1013 tw_job_next(struct Strbuf *res, struct Strbuf *dir, int *flags)
1019 for (;tw_index <= pmaxindex; tw_index++) {
1020 for (j = proclist.p_next; j != NULL; j = j->p_next)
1021 if (j->p_index == tw_index && j->p_procid == j->p_jobid)
1025 Strbuf_append(res, j->p_command);
1030 } /* end tw_job_next */