1 /* $Header: /src/pub/tcsh/tw.init.c,v 3.29 2002/06/25 19:02:12 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.29 2002/06/25 19:02:12 christos Exp $")
42 #if !defined(NSIG) && defined(SIGMAX)
43 # define NSIG (SIGMAX+1)
44 #endif /* !NSIG && SIGMAX */
45 #if !defined(NSIG) && defined(_NSIG)
47 #endif /* !NSIG && _NSIG */
52 Char
**list
, /* List of command names */
53 *buff
; /* Space holding command names */
54 int nlist
, /* Number of items */
55 nbuff
, /* Current space in name buf */
56 tlist
, /* Total space in list */
57 tbuff
; /* Total space in name buf */
61 static struct varent
*tw_vptr
= NULL
; /* Current shell variable */
62 static Char
**tw_env
= NULL
; /* Current environment variable */
63 static Char
*tw_word
; /* Current word pointer */
64 static struct KeyFuncs
*tw_bind
= NULL
; /* List of the bindings */
66 static struct limits
*tw_limit
= NULL
; /* List of the resource limits */
67 #endif /* HAVENOLIMIT */
68 static int tw_index
= 0; /* signal and job index */
69 static DIR *tw_dir_fd
= NULL
; /* Current directory descriptor */
70 static Char tw_retname
[MAXPATHLEN
+1]; /* Return buffer */
71 static int tw_cmd_got
= 0; /* What we need to do */
72 static stringlist_t tw_cmd
= { NULL
, NULL
, 0, 0, 0, 0 };
73 static stringlist_t tw_item
= { NULL
, NULL
, 0, 0, 0, 0 };
74 #define TW_FL_CMD 0x01
75 #define TW_FL_ALIAS 0x02
76 #define TW_FL_BUILTIN 0x04
77 #define TW_FL_SORT 0x08
78 #define TW_FL_REL 0x10
80 static struct { /* Current element pointer */
81 int cur
; /* Current element number */
82 Char
**pathv
; /* Current element in path */
83 DIR *dfd
; /* Current directory descriptor */
88 static sigmask_t tw_omask
;
89 # define TW_HOLD() tw_omask = sigblock(sigmask(SIGINT))
90 # define TW_RELS() (void) sigsetmask(tw_omask)
92 # define TW_HOLD() (void) sighold(SIGINT)
93 # define TW_RELS() (void) sigrelse(SIGINT)
99 if (tw_dir_fd != NULL) \
100 rewinddir(tw_dir_fd); \
103 #define CLRDIR(dfd) \
106 (void) closedir(dfd); \
111 static Char
*tw_str_add
__P((stringlist_t
*, int));
112 static void tw_str_free
__P((stringlist_t
*));
113 static Char
*tw_dir_next
__P((DIR *));
114 static void tw_cmd_add
__P((Char
*name
));
115 static void tw_cmd_cmd
__P((void));
116 static void tw_cmd_builtin
__P((void));
117 static void tw_cmd_alias
__P((void));
118 static void tw_cmd_sort
__P((void));
119 static void tw_vptr_start
__P((struct varent
*));
123 * Add an item to the string list
132 if (sl
->tlist
<= sl
->nlist
) {
134 sl
->tlist
+= TW_INCR
;
135 sl
->list
= sl
->list
?
136 (Char
**) xrealloc((ptr_t
) sl
->list
,
137 (size_t) (sl
->tlist
* sizeof(Char
*))) :
138 (Char
**) xmalloc((size_t) (sl
->tlist
* sizeof(Char
*)));
141 if (sl
->tbuff
<= sl
->nbuff
+ len
) {
146 sl
->tbuff
+= TW_INCR
+ len
;
147 sl
->buff
= sl
->buff
?
148 (Char
*) xrealloc((ptr_t
) sl
->buff
,
149 (size_t) (sl
->tbuff
* sizeof(Char
))) :
150 (Char
*) xmalloc((size_t) (sl
->tbuff
* sizeof(Char
)));
151 /* Re-thread the new pointer list, if changed */
152 if (ptr
!= NULL
&& ptr
!= sl
->buff
) {
153 int offs
= (int) (sl
->buff
- ptr
);
154 for (i
= 0; i
< sl
->nlist
; i
++)
159 ptr
= sl
->list
[sl
->nlist
++] = &sl
->buff
[sl
->nbuff
];
174 xfree((ptr_t
) sl
->list
);
176 sl
->tlist
= sl
->nlist
= 0;
179 xfree((ptr_t
) sl
->buff
);
181 sl
->tbuff
= sl
->nbuff
= 0;
184 } /* end tw_str_free */
191 register struct dirent
*dirp
;
196 if ((dirp
= readdir(dfd
)) != NULL
) {
197 (void) Strcpy(tw_retname
, str2short(dirp
->d_name
));
201 } /* end tw_dir_next */
205 * Add the name to the command list
213 len
= (int) Strlen(name
) + 2;
214 (void) Strcpy(tw_str_add(&tw_cmd
, len
), name
);
215 } /* end tw_cmd_add */
219 * Free the command list
225 tw_str_free(&tw_cmd
);
227 } /* end tw_cmd_free */
230 * Add system commands to the command list
236 register struct dirent
*dp
;
237 register Char
*dir
= NULL
, *name
;
239 struct varent
*v
= adrof(STRpath
);
240 struct varent
*recexec
= adrof(STRrecognize_only_executables
);
244 if (v
== NULL
|| v
->vec
== NULL
) /* if no path */
247 for (pv
= v
->vec
; *pv
; pv
++) {
248 if (pv
[0][0] != '/') {
249 tw_cmd_got
|= TW_FL_REL
;
253 if ((dirp
= opendir(short2str(*pv
))) == NULL
)
257 dir
= Strspl(*pv
, STRslash
);
258 while ((dp
= readdir(dirp
)) != NULL
) {
259 #if defined(_UWIN) || defined(__CYGWIN__)
260 /* Turn foo.{exe,com,bat} into foo since UWIN's readdir returns
261 * the file with the .exe, .com, .bat extension
263 size_t ext
= strlen(dp
->d_name
) - 4;
264 if ((ext
> 0) && (strcmp(&dp
->d_name
[ext
], ".exe") == 0 ||
265 strcmp(&dp
->d_name
[ext
], ".bat") == 0 ||
266 strcmp(&dp
->d_name
[ext
], ".com") == 0))
267 dp
->d_name
[ext
] = '\0';
268 #endif /* _UWIN || __CYGWIN__ */
269 /* the call to executable() may make this a bit slow */
270 name
= str2short(dp
->d_name
);
271 if (dp
->d_ino
== 0 || (recexec
&& !executable(dir
, name
, 0)))
273 len
= (int) Strlen(name
) + 2;
274 if (name
[0] == '#' || /* emacs temp files */
275 name
[0] == '.' || /* .files */
276 name
[len
- 3] == '~' || /* emacs backups */
277 name
[len
- 3] == '%') /* textedit backups */
278 continue; /* Ignore! */
281 (void) closedir(dirp
);
285 } /* end tw_cmd_cmd */
289 * Add builtins to the command list
294 register struct biltins
*bptr
;
296 for (bptr
= bfunc
; bptr
< &bfunc
[nbfunc
]; bptr
++)
298 tw_cmd_add(str2short(bptr
->bname
));
300 for (bptr
= nt_bfunc
; bptr
< &nt_bfunc
[nt_nbfunc
]; bptr
++)
302 tw_cmd_add(str2short(bptr
->bname
));
303 #endif /* WINNT_NATIVE*/
304 } /* end tw_cmd_builtin */
308 * Add aliases to the command list
313 register struct varent
*p
;
314 register struct varent
*c
;
321 if (p
->v_parent
== 0) /* is it the header? */
324 tw_cmd_add(p
->v_name
);
332 } while (p
->v_right
== c
);
335 } /* end tw_cmd_alias */
339 * Sort the command list removing duplicate elements
348 qsort((ptr_t
) tw_cmd
.list
, (size_t) tw_cmd
.nlist
, sizeof(Char
*),
349 (int (*) __P((const void *, const void *))) fcompare
);
351 /* get rid of multiple entries */
352 for (i
= 0, fwd
= 0; i
< tw_cmd
.nlist
- 1; i
++) {
353 if (Strcmp(tw_cmd
.list
[i
], tw_cmd
.list
[i
+ 1]) == 0) /* garbage */
354 fwd
++; /* increase the forward ref. count */
356 tw_cmd
.list
[i
- fwd
] = tw_cmd
.list
[i
];
358 /* Fix fencepost error -- Theodore Ts'o <tytso@athena.mit.edu> */
360 tw_cmd
.list
[i
- fwd
] = tw_cmd
.list
[i
];
363 } /* end tw_cmd_sort */
367 * Get the command list and sort it, if not done yet.
368 * Reset the current pointer to the beginning of the command list
372 tw_cmd_start(dfd
, pat
)
376 static Char
*defpath
[] = { STRNULL
, 0 };
379 if ((tw_cmd_got
& TW_FL_CMD
) == 0) {
382 tw_cmd_got
|= TW_FL_CMD
;
384 if ((tw_cmd_got
& TW_FL_ALIAS
) == 0) {
386 tw_cmd_got
&= ~TW_FL_SORT
;
387 tw_cmd_got
|= TW_FL_ALIAS
;
389 if ((tw_cmd_got
& TW_FL_BUILTIN
) == 0) {
391 tw_cmd_got
&= ~TW_FL_SORT
;
392 tw_cmd_got
|= TW_FL_BUILTIN
;
394 if ((tw_cmd_got
& TW_FL_SORT
) == 0) {
396 tw_cmd_got
|= TW_FL_SORT
;
399 tw_cmd_state
.cur
= 0;
400 CLRDIR(tw_cmd_state
.dfd
)
401 if (tw_cmd_got
& TW_FL_REL
) {
402 struct varent
*vp
= adrof(STRpath
);
404 tw_cmd_state
.pathv
= vp
->vec
;
406 tw_cmd_state
.pathv
= defpath
;
409 tw_cmd_state
.pathv
= defpath
;
414 * Return the next element in the command list or
415 * Look for commands in the relative path components
418 tw_cmd_next(dir
, flags
)
424 if (tw_cmd_state
.cur
< tw_cmd
.nlist
) {
426 return tw_cmd
.list
[tw_cmd_state
.cur
++];
430 * We need to process relatives in the path.
432 while (((tw_cmd_state
.dfd
== NULL
) ||
433 ((ptr
= tw_dir_next(tw_cmd_state
.dfd
)) == NULL
)) &&
434 (*tw_cmd_state
.pathv
!= NULL
)) {
436 CLRDIR(tw_cmd_state
.dfd
)
438 while (*tw_cmd_state
.pathv
&& tw_cmd_state
.pathv
[0][0] == '/')
439 tw_cmd_state
.pathv
++;
440 if ((ptr
= *tw_cmd_state
.pathv
) != 0) {
442 * We complete directories only on '.' should that
445 if (ptr
[0] == '\0' || (ptr
[0] == '.' && ptr
[1] == '\0')) {
447 tw_cmd_state
.dfd
= opendir(".");
448 *flags
= TW_DIR_OK
| TW_EXEC_CHK
;
451 copyn(dir
, *tw_cmd_state
.pathv
, FILSIZ
);
452 catn(dir
, STRslash
, FILSIZ
);
453 tw_cmd_state
.dfd
= opendir(short2str(*tw_cmd_state
.pathv
));
454 *flags
= TW_EXEC_CHK
;
456 tw_cmd_state
.pathv
++;
460 } /* end tw_cmd_next */
464 * Find the first variable in the variable list
470 tw_vptr
= c
; /* start at beginning of variable list */
473 while (tw_vptr
->v_left
)
474 tw_vptr
= tw_vptr
->v_left
;
476 if (tw_vptr
->v_parent
== 0) { /* is it the header? */
481 return; /* found first one */
482 if (tw_vptr
->v_right
) {
483 tw_vptr
= tw_vptr
->v_right
;
488 tw_vptr
= tw_vptr
->v_parent
;
489 } while (tw_vptr
->v_right
== c
);
492 } /* end tw_shvar_start */
496 * Return the next shell variable
500 tw_shvar_next(dir
, flags
)
504 register struct varent
*p
;
505 register struct varent
*c
;
510 if ((p
= tw_vptr
) == NULL
)
511 return (NULL
); /* just in case */
513 cp
= p
->v_name
; /* we know that this name is here now */
515 /* now find the next one */
517 if (p
->v_right
) { /* if we can go right */
522 else { /* else go up */
526 } while (p
->v_right
== c
);
528 if (p
->v_parent
== 0) { /* is it the header? */
533 tw_vptr
= p
; /* save state for the next call */
537 } /* end tw_shvar_next */
541 * Return the next environment variable
545 tw_envvar_next(dir
, flags
)
553 if (tw_env
== NULL
|| *tw_env
== NULL
)
555 for (ps
= *tw_env
, pd
= tw_retname
;
556 *ps
&& *ps
!= '=' && pd
<= &tw_retname
[MAXPATHLEN
]; *pd
++ = *ps
++)
561 } /* end tw_envvar_next */
565 * Begin the list of the shell and environment variables
569 tw_var_start(dfd
, pat
)
575 tw_vptr_start(&shvhed
);
576 tw_env
= STR_environ
;
577 } /* end tw_var_start */
581 * Begin the list of the shell aliases
585 tw_alias_start(dfd
, pat
)
591 tw_vptr_start(&aliases
);
593 } /* tw_alias_start */
596 /* tw_complete_start():
597 * Begin the list of completions
601 tw_complete_start(dfd
, pat
)
605 extern struct varent completions
;
609 tw_vptr_start(&completions
);
611 } /* end tw_complete_start */
615 * Return the next shell or environment variable
618 tw_var_next(dir
, flags
)
625 ptr
= tw_shvar_next(dir
, flags
);
627 ptr
= tw_envvar_next(dir
, flags
);
629 } /* end tw_var_next */
632 /* tw_logname_start():
633 * Initialize lognames to the beginning of the list
637 tw_logname_start(dfd
, pat
)
643 #if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
644 (void) setpwent(); /* Open passwd file */
645 #endif /* !_VMS_POSIX && !WINNT_NATIVE */
646 } /* end tw_logname_start */
649 /* tw_logname_next():
650 * Return the next entry from the passwd file
654 tw_logname_next(dir
, flags
)
658 static Char retname
[MAXPATHLEN
];
661 * We don't want to get interrupted inside getpwent()
662 * because the yellow pages code is not interruptible,
663 * and if we call endpwent() immediatetely after
664 * (in pintr()) we may be freeing an invalid pointer
669 #if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
670 /* ISC does not declare getpwent()? */
671 pw
= (struct passwd
*) getpwent();
672 #else /* _VMS_POSIX || WINNT_NATIVE */
674 #endif /* !_VMS_POSIX && !WINNT_NATIVE */
683 (void) Strcpy(retname
, str2short(pw
->pw_name
));
685 } /* end tw_logname_next */
689 * Close the passwd file to finish the logname list
697 #if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
699 #endif /* !_VMS_POSIX && !WINNT_NATIVE */
700 } /* end tw_logname_end */
703 /* tw_grpname_start():
704 * Initialize grpnames to the beginning of the list
708 tw_grpname_start(dfd
, pat
)
714 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
715 (void) setgrent(); /* Open group file */
716 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
717 } /* end tw_grpname_start */
720 /* tw_grpname_next():
721 * Return the next entry from the group file
725 tw_grpname_next(dir
, flags
)
729 static Char retname
[MAXPATHLEN
];
732 * We don't want to get interrupted inside getgrent()
733 * because the yellow pages code is not interruptible,
734 * and if we call endgrent() immediatetely after
735 * (in pintr()) we may be freeing an invalid pointer
740 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
741 gr
= (struct group
*) getgrent();
742 #else /* _VMS_POSIX || _OSD_POSIX || WINNT_NATIVE */
744 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
753 (void) Strcpy(retname
, str2short(gr
->gr_name
));
755 } /* end tw_grpname_next */
759 * Close the group file to finish the groupname list
767 #if !defined(_VMS_POSIX) && !defined(_OSD_POSIX) && !defined(WINNT_NATIVE)
769 #endif /* !_VMS_POSIX && !_OSD_POSIX && !WINNT_NATIVE */
770 } /* end tw_grpname_end */
773 * Initialize the directory for the file list
777 tw_file_start(dfd
, pat
)
784 if ((vp
= adrof(STRcdpath
)) != NULL
)
786 } /* end tw_file_start */
790 * Return the next file in the directory
793 tw_file_next(dir
, flags
)
797 Char
*ptr
= tw_dir_next(tw_dir_fd
);
798 if (ptr
== NULL
&& (*flags
& TW_DIR_OK
) != 0) {
800 while (tw_env
&& *tw_env
)
801 if ((tw_dir_fd
= opendir(short2str(*tw_env
))) != NULL
)
807 copyn(dir
, *tw_env
++, MAXPATHLEN
);
808 catn(dir
, STRslash
, MAXPATHLEN
);
809 ptr
= tw_dir_next(tw_dir_fd
);
813 } /* end tw_file_next */
817 * Clear directory related lists
823 CLRDIR(tw_cmd_state
.dfd
)
824 } /* end tw_dir_end */
833 tw_str_free(&tw_item
);
834 } /* end tw_item_free */
838 * Return the list of items
844 } /* end tw_item_get */
854 return tw_str_add(&tw_item
, len
);
859 * Find the string if it exists in the item list
868 if (tw_item
.list
== NULL
|| str
== NULL
)
871 for (i
= 0; i
< tw_item
.nlist
; i
++)
872 if (tw_item
.list
[i
] != NULL
&& Strcmp(tw_item
.list
[i
], str
) == 0)
873 return tw_item
.list
[i
];
875 } /* end tw_item_find */
879 * Initialize a variable list
882 tw_vl_start(dfd
, pat
)
887 if ((tw_vptr
= adrof(pat
)) != NULL
) {
888 tw_env
= tw_vptr
->vec
;
893 } /* end tw_vl_start */
897 * Initialize a word list
900 tw_wl_start(dfd
, pat
)
906 } /* end tw_wl_start */
910 * Return the next word from the word list
914 tw_wl_next(dir
, flags
)
919 if (tw_word
== NULL
|| tw_word
[0] == '\0')
922 while (*tw_word
&& Isspace(*tw_word
)) tw_word
++;
924 for (dir
= tw_word
; *tw_word
&& !Isspace(*tw_word
); tw_word
++)
928 return *dir
? dir
: NULL
;
929 } /* end tw_wl_next */
933 * Begin the list of the shell bindings
937 tw_bind_start(dfd
, pat
)
944 } /* end tw_bind_start */
948 * Begin the list of the shell bindings
952 tw_bind_next(dir
, flags
)
958 if (tw_bind
&& tw_bind
->name
) {
959 for (ptr
= tw_bind
->name
, dir
= tw_retname
;
960 (*dir
++ = (Char
) *ptr
++) != '\0';)
966 } /* end tw_bind_next */
970 * Begin the list of the shell limitings
974 tw_limit_start(dfd
, pat
)
982 #endif /* ! HAVENOLIMIT */
983 } /* end tw_limit_start */
987 * Begin the list of the shell limitings
991 tw_limit_next(dir
, flags
)
997 if (tw_limit
&& tw_limit
->limname
) {
998 for (ptr
= tw_limit
->limname
, dir
= tw_retname
;
999 (*dir
++ = (Char
) *ptr
++) != '\0';)
1004 #endif /* ! HAVENOLIMIT */
1007 } /* end tw_limit_next */
1011 * Begin the list of the shell sigings
1015 tw_sig_start(dfd
, pat
)
1022 } /* end tw_sig_start */
1026 * Begin the list of the shell sigings
1030 tw_sig_next(dir
, flags
)
1037 for (;tw_index
< nsig
; tw_index
++) {
1039 if (mesg
[tw_index
].iname
== NULL
)
1042 for (ptr
= mesg
[tw_index
].iname
, dir
= tw_retname
;
1043 (*dir
++ = (Char
) *ptr
++) != '\0';)
1049 } /* end tw_sig_next */
1053 * Begin the list of the shell jobings
1057 tw_job_start(dfd
, pat
)
1064 } /* end tw_job_start */
1068 * Begin the list of the shell jobings
1072 tw_job_next(dir
, flags
)
1080 for (;tw_index
<= pmaxindex
; tw_index
++) {
1081 for (j
= proclist
.p_next
; j
!= NULL
; j
= j
->p_next
)
1082 if (j
->p_index
== tw_index
&& j
->p_procid
== j
->p_jobid
)
1086 for (ptr
= j
->p_command
, dir
= tw_retname
; (*dir
++ = *ptr
++) != '\0';)
1093 } /* end tw_job_next */