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. All advertising materials mentioning features or use of this software
17
* must display the following acknowledgement:
18
* This product includes software developed by the University of
19
* California, Berkeley and its contributors.
20
* 4. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45
#if !defined(NSIG) && defined(SIGMAX)
46
# define NSIG (SIGMAX+1)
47
#endif /* !NSIG && SIGMAX */
48
#if !defined(NSIG) && defined(_NSIG)
50
#endif /* !NSIG && _NSIG */
55
Char **list, /* List of command names */
56
*buff; /* Space holding command names */
57
int nlist, /* Number of items */
58
nbuff, /* Current space in name buf */
59
tlist, /* Total space in list */
60
tbuff; /* Total space in name buf */
64
static struct varent *tw_vptr = NULL; /* Current shell variable */
65
static Char **tw_env = NULL; /* Current environment variable */
66
static Char *tw_word; /* Current word pointer */
67
static struct KeyFuncs *tw_bind = NULL; /* List of the bindings */
69
static struct limits *tw_limit = NULL; /* List of the resource limits */
70
#endif /* HAVENOLIMIT */
71
static int tw_index = 0; /* signal and job index */
72
static DIR *tw_dir_fd = NULL; /* Current directory descriptor */
73
static Char tw_retname[MAXPATHLEN+1]; /* Return buffer */
74
static int tw_cmd_got = 0; /* What we need to do */
75
static stringlist_t tw_cmd = { NULL, NULL, 0, 0, 0, 0 };
76
static stringlist_t tw_item = { NULL, NULL, 0, 0, 0, 0 };
77
#define TW_FL_CMD 0x01
78
#define TW_FL_ALIAS 0x02
79
#define TW_FL_BUILTIN 0x04
80
#define TW_FL_SORT 0x08
81
#define TW_FL_REL 0x10
83
static struct { /* Current element pointer */
84
int cur; /* Current element number */
85
Char **pathv; /* Current element in path */
86
DIR *dfd; /* Current directory descriptor */
91
static sigmask_t tw_omask;
92
# define TW_HOLD() tw_omask = sigblock(sigmask(SIGINT))
93
# define TW_RELS() (void) sigsetmask(tw_omask)
95
# define TW_HOLD() (void) sighold(SIGINT)
96
# define TW_RELS() (void) sigrelse(SIGINT)
102
if (tw_dir_fd != NULL) \
103
rewinddir(tw_dir_fd); \
106
#define CLRDIR(dfd) \
109
(void) closedir(dfd); \
114
static Char *tw_str_add __P((stringlist_t *, int));
115
static void tw_str_free __P((stringlist_t *));
116
static Char *tw_dir_next __P((DIR *));
117
static void tw_cmd_add __P((Char *name));
118
static void tw_cmd_cmd __P((void));
119
static void tw_cmd_builtin __P((void));
120
static void tw_cmd_alias __P((void));
121
static void tw_cmd_sort __P((void));
122
static void tw_vptr_start __P((struct varent *));
126
* Add an item to the string list
135
if (sl->tlist <= sl->nlist) {
137
sl->tlist += TW_INCR;
138
sl->list = sl->list ?
139
(Char **) xrealloc((ptr_t) sl->list,
140
(size_t) (sl->tlist * sizeof(Char *))) :
141
(Char **) xmalloc((size_t) (sl->tlist * sizeof(Char *)));
144
if (sl->tbuff <= sl->nbuff + len) {
149
sl->tbuff += TW_INCR + len;
150
sl->buff = sl->buff ?
151
(Char *) xrealloc((ptr_t) sl->buff,
152
(size_t) (sl->tbuff * sizeof(Char))) :
153
(Char *) xmalloc((size_t) (sl->tbuff * sizeof(Char)));
154
/* Re-thread the new pointer list, if changed */
155
if (ptr != NULL && ptr != sl->buff) {
156
int offs = (int) (sl->buff - ptr);
157
for (i = 0; i < sl->nlist; i++)
162
ptr = sl->list[sl->nlist++] = &sl->buff[sl->nbuff];
177
xfree((ptr_t) sl->list);
179
sl->tlist = sl->nlist = 0;
182
xfree((ptr_t) sl->buff);
184
sl->tbuff = sl->nbuff = 0;
187
} /* end tw_str_free */
194
register struct dirent *dirp;
199
if ((dirp = readdir(dfd)) != NULL) {
200
(void) Strcpy(tw_retname, str2short(dirp->d_name));
204
} /* end tw_dir_next */
208
* Add the name to the command list
216
len = (int) Strlen(name) + 2;
217
(void) Strcpy(tw_str_add(&tw_cmd, len), name);
218
} /* end tw_cmd_add */
222
* Free the command list
228
tw_str_free(&tw_cmd);
230
} /* end tw_cmd_free */
233
* Add system commands to the command list
239
register struct dirent *dp;
240
register Char *dir = NULL, *name;
242
struct varent *v = adrof(STRpath);
243
struct varent *recexec = adrof(STRrecognize_only_executables);
247
if (v == NULL) /* if no path */
250
for (pv = v->vec; *pv; pv++) {
251
if (pv[0][0] != '/') {
252
tw_cmd_got |= TW_FL_REL;
256
if ((dirp = opendir(short2str(*pv))) == NULL)
260
dir = Strspl(*pv, STRslash);
261
while ((dp = readdir(dirp)) != NULL) {
262
/* the call to executable() may make this a bit slow */
263
name = str2short(dp->d_name);
265
if (dp->d_ino == 0 || !executable(dir, name, 0))
267
if (dp->d_ino == 0 || (recexec && !executable(dir, name, 0)))
270
len = (int) Strlen(name) + 2;
271
if (name[0] == '#' || /* emacs temp files */
272
name[0] == '.' || /* .files */
273
name[len - 3] == '~' || /* emacs backups */
274
name[len - 3] == '%') /* textedit backups */
275
continue; /* Ignore! */
278
(void) closedir(dirp);
282
} /* end tw_cmd_cmd */
286
* Add builtins to the command list
291
register struct biltins *bptr;
293
for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++)
295
tw_cmd_add(str2short(bptr->bname));
297
for (bptr = nt_bfunc; bptr < &nt_bfunc[nt_nbfunc]; bptr++)
299
tw_cmd_add(str2short(bptr->bname));
301
} /* end tw_cmd_builtin */
305
* Add aliases to the command list
310
register struct varent *p;
311
register struct varent *c;
318
if (p->v_parent == 0) /* is it the header? */
321
tw_cmd_add(p->v_name);
329
} while (p->v_right == c);
332
} /* end tw_cmd_alias */
336
* Sort the command list removing duplicate elements
345
qsort((ptr_t) tw_cmd.list, (size_t) tw_cmd.nlist, sizeof(Char *),
346
(int (*) __P((const void *, const void *))) fcompare);
348
/* get rid of multiple entries */
349
for (i = 0, fwd = 0; i < tw_cmd.nlist - 1; i++) {
350
if (Strcmp(tw_cmd.list[i], tw_cmd.list[i + 1]) == 0) /* garbage */
351
fwd++; /* increase the forward ref. count */
353
tw_cmd.list[i - fwd] = tw_cmd.list[i];
355
/* Fix fencepost error -- Theodore Ts'o <tytso@athena.mit.edu> */
357
tw_cmd.list[i - fwd] = tw_cmd.list[i];
360
} /* end tw_cmd_sort */
364
* Get the command list and sort it, if not done yet.
365
* Reset the current pointer to the beginning of the command list
369
tw_cmd_start(dfd, pat)
373
static Char *defpath[] = { STRNULL, 0 };
376
if ((tw_cmd_got & TW_FL_CMD) == 0) {
379
tw_cmd_got |= TW_FL_CMD;
381
if ((tw_cmd_got & TW_FL_ALIAS) == 0) {
383
tw_cmd_got &= ~TW_FL_SORT;
384
tw_cmd_got |= TW_FL_ALIAS;
386
if ((tw_cmd_got & TW_FL_BUILTIN) == 0) {
388
tw_cmd_got &= ~TW_FL_SORT;
389
tw_cmd_got |= TW_FL_BUILTIN;
391
if ((tw_cmd_got & TW_FL_SORT) == 0) {
393
tw_cmd_got |= TW_FL_SORT;
396
tw_cmd_state.cur = 0;
397
CLRDIR(tw_cmd_state.dfd)
398
if (tw_cmd_got & TW_FL_REL) {
399
struct varent *vp = adrof(STRpath);
401
tw_cmd_state.pathv = vp->vec;
403
tw_cmd_state.pathv = defpath;
406
tw_cmd_state.pathv = defpath;
411
* Return the next element in the command list or
412
* Look for commands in the relative path components
415
tw_cmd_next(dir, flags)
421
if (tw_cmd_state.cur < tw_cmd.nlist) {
423
return tw_cmd.list[tw_cmd_state.cur++];
427
* We need to process relatives in the path.
429
while (((tw_cmd_state.dfd == NULL) ||
430
((ptr = tw_dir_next(tw_cmd_state.dfd)) == NULL)) &&
431
(*tw_cmd_state.pathv != NULL)) {
433
CLRDIR(tw_cmd_state.dfd)
435
while (*tw_cmd_state.pathv && tw_cmd_state.pathv[0][0] == '/')
436
tw_cmd_state.pathv++;
437
if ((ptr = *tw_cmd_state.pathv) != 0) {
439
* We complete directories only on '.' should that
442
if (ptr[0] == '\0' || (ptr[0] == '.' && ptr[1] == '\0')) {
444
tw_cmd_state.dfd = opendir(".");
445
*flags = TW_DIR_OK | TW_EXEC_CHK;
448
copyn(dir, *tw_cmd_state.pathv, FILSIZ);
449
catn(dir, STRslash, FILSIZ);
450
tw_cmd_state.dfd = opendir(short2str(*tw_cmd_state.pathv));
451
*flags = TW_EXEC_CHK;
453
tw_cmd_state.pathv++;
457
} /* end tw_cmd_next */
461
* Find the first variable in the variable list
467
tw_vptr = c; /* start at beginning of variable list */
470
while (tw_vptr->v_left)
471
tw_vptr = tw_vptr->v_left;
473
if (tw_vptr->v_parent == 0) { /* is it the header? */
478
return; /* found first one */
479
if (tw_vptr->v_right) {
480
tw_vptr = tw_vptr->v_right;
485
tw_vptr = tw_vptr->v_parent;
486
} while (tw_vptr->v_right == c);
489
} /* end tw_shvar_start */
493
* Return the next shell variable
497
tw_shvar_next(dir, flags)
501
register struct varent *p;
502
register struct varent *c;
507
if ((p = tw_vptr) == NULL)
508
return (NULL); /* just in case */
510
cp = p->v_name; /* we know that this name is here now */
512
/* now find the next one */
514
if (p->v_right) { /* if we can go right */
519
else { /* else go up */
523
} while (p->v_right == c);
525
if (p->v_parent == 0) { /* is it the header? */
530
tw_vptr = p; /* save state for the next call */
534
} /* end tw_shvar_next */
538
* Return the next environment variable
542
tw_envvar_next(dir, flags)
550
if (tw_env == NULL || *tw_env == NULL)
552
for (ps = *tw_env, pd = tw_retname;
553
*ps && *ps != '=' && pd <= &tw_retname[MAXPATHLEN]; *pd++ = *ps++)
558
} /* end tw_envvar_next */
562
* Begin the list of the shell and environment variables
566
tw_var_start(dfd, pat)
572
tw_vptr_start(&shvhed);
573
tw_env = STR_environ;
574
} /* end tw_var_start */
578
* Begin the list of the shell aliases
582
tw_alias_start(dfd, pat)
588
tw_vptr_start(&aliases);
590
} /* tw_alias_start */
593
/* tw_complete_start():
594
* Begin the list of completions
598
tw_complete_start(dfd, pat)
602
extern struct varent completions;
606
tw_vptr_start(&completions);
608
} /* end tw_complete_start */
612
* Return the next shell or environment variable
615
tw_var_next(dir, flags)
622
ptr = tw_shvar_next(dir, flags);
624
ptr = tw_envvar_next(dir, flags);
626
} /* end tw_var_next */
629
/* tw_logname_start():
630
* Initialize lognames to the beginning of the list
634
tw_logname_start(dfd, pat)
640
#if !defined(_VMS_POSIX) && !defined(WINNT)
641
(void) setpwent(); /* Open passwd file */
642
#endif /* !_VMS_POSIX && !WINNT */
643
} /* end tw_logname_start */
646
/* tw_logname_next():
647
* Return the next entry from the passwd file
651
tw_logname_next(dir, flags)
655
static Char retname[MAXPATHLEN];
658
* We don't want to get interrupted inside getpwent()
659
* because the yellow pages code is not interruptible,
660
* and if we call endpwent() immediatetely after
661
* (in pintr()) we may be freeing an invalid pointer
666
#if !defined(_VMS_POSIX) && !defined(WINNT)
667
/* ISC does not declare getpwent()? */
668
pw = (struct passwd *) getpwent();
669
#else /* _VMS_POSIX || WINNT */
671
#endif /* !_VMS_POSIX && !WINNT */
680
(void) Strcpy(retname, str2short(pw->pw_name));
682
} /* end tw_logname_next */
686
* Close the passwd file to finish the logname list
694
#if !defined(_VMS_POSIX) && !defined(WINNT)
696
#endif /* !_VMS_POSIX && !WINNT */
697
} /* end tw_logname_end */
700
/* tw_grpname_start():
701
* Initialize grpnames to the beginning of the list
705
tw_grpname_start(dfd, pat)
711
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT)
712
(void) setgrent(); /* Open group file */
713
#endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT */
714
} /* end tw_grpname_start */
717
/* tw_grpname_next():
718
* Return the next entry from the group file
722
tw_grpname_next(dir, flags)
726
static Char retname[MAXPATHLEN];
729
* We don't want to get interrupted inside getgrent()
730
* because the yellow pages code is not interruptible,
731
* and if we call endgrent() immediatetely after
732
* (in pintr()) we may be freeing an invalid pointer
737
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT)
739
gr = (struct group *) getgrent_nomembers();
741
gr = (struct group *) getgrent();
743
#else /* _VMS_POSIX || _OSD_POSIX || WINNT */
745
#endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT */
754
(void) Strcpy(retname, str2short(gr->gr_name));
756
} /* end tw_grpname_next */
760
* Close the group file to finish the groupname list
768
#if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT)
770
#endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT */
771
} /* end tw_grpname_end */
774
* Initialize the directory for the file list
778
tw_file_start(dfd, pat)
785
if ((vp = adrof(STRcdpath)) != NULL)
787
} /* end tw_file_start */
791
* Return the next file in the directory
794
tw_file_next(dir, flags)
798
Char *ptr = tw_dir_next(tw_dir_fd);
799
if (ptr == NULL && (*flags & TW_DIR_OK) != 0) {
801
while (tw_env && *tw_env)
802
if ((tw_dir_fd = opendir(short2str(*tw_env))) != NULL)
808
copyn(dir, *tw_env++, MAXPATHLEN);
809
catn(dir, STRslash, MAXPATHLEN);
810
ptr = tw_dir_next(tw_dir_fd);
814
} /* end tw_file_next */
818
* Clear directory related lists
824
CLRDIR(tw_cmd_state.dfd)
825
} /* end tw_dir_end */
834
tw_str_free(&tw_item);
835
} /* end tw_item_free */
839
* Return the list of items
845
} /* end tw_item_get */
855
return tw_str_add(&tw_item, len);
860
* Find the string if it exists in the item list
869
if (tw_item.list == NULL || str == NULL)
872
for (i = 0; i < tw_item.nlist; i++)
873
if (tw_item.list[i] != NULL && Strcmp(tw_item.list[i], str) == 0)
874
return tw_item.list[i];
876
} /* end tw_item_find */
880
* Initialize a variable list
883
tw_vl_start(dfd, pat)
888
if ((tw_vptr = adrof(pat)) != NULL) {
889
tw_env = tw_vptr->vec;
894
} /* end tw_vl_start */
898
* Initialize a word list
901
tw_wl_start(dfd, pat)
907
} /* end tw_wl_start */
911
* Return the next word from the word list
915
tw_wl_next(dir, flags)
920
if (tw_word == NULL || tw_word[0] == '\0')
923
while (*tw_word && Isspace(*tw_word)) tw_word++;
925
for (dir = tw_word; *tw_word && !Isspace(*tw_word); tw_word++)
929
return *dir ? dir : NULL;
930
} /* end tw_wl_next */
934
* Begin the list of the shell bindings
938
tw_bind_start(dfd, pat)
945
} /* end tw_bind_start */
949
* Begin the list of the shell bindings
953
tw_bind_next(dir, flags)
959
if (tw_bind && tw_bind->name) {
960
for (ptr = tw_bind->name, dir = tw_retname;
961
(*dir++ = (Char) *ptr++) != '\0';)
967
} /* end tw_bind_next */
971
* Begin the list of the shell limitings
975
tw_limit_start(dfd, pat)
983
#endif /* ! HAVENOLIMIT */
984
} /* end tw_limit_start */
988
* Begin the list of the shell limitings
992
tw_limit_next(dir, flags)
998
if (tw_limit && tw_limit->limname) {
999
for (ptr = tw_limit->limname, dir = tw_retname;
1000
(*dir++ = (Char) *ptr++) != '\0';)
1005
#endif /* ! HAVENOLIMIT */
1008
} /* end tw_limit_next */
1012
* Begin the list of the shell sigings
1016
tw_sig_start(dfd, pat)
1023
} /* end tw_sig_start */
1027
* Begin the list of the shell sigings
1031
tw_sig_next(dir, flags)
1038
for (;tw_index < nsig; tw_index++) {
1040
if (mesg[tw_index].iname == NULL)
1043
for (ptr = mesg[tw_index].iname, dir = tw_retname;
1044
(*dir++ = (Char) *ptr++) != '\0';)
1050
} /* end tw_sig_next */
1054
* Begin the list of the shell jobings
1058
tw_job_start(dfd, pat)
1065
} /* end tw_job_start */
1069
* Begin the list of the shell jobings
1073
tw_job_next(dir, flags)
1081
for (;tw_index <= pmaxindex; tw_index++) {
1082
for (j = proclist.p_next; j != NULL; j = j->p_next)
1083
if (j->p_index == tw_index && j->p_procid == j->p_jobid)
1087
for (ptr = j->p_command, dir = tw_retname; (*dir++ = *ptr++) != '\0';)
1094
} /* end tw_job_next */