xref: /freebsd/contrib/tcsh/tc.func.c (revision 6560ac57ce879857203bc456cdc3849808dc0700)
1c80476e4SDavid E. O'Brien /*
2c80476e4SDavid E. O'Brien  * tc.func.c: New tcsh builtins.
3c80476e4SDavid E. O'Brien  */
4c80476e4SDavid E. O'Brien /*-
5c80476e4SDavid E. O'Brien  * Copyright (c) 1980, 1991 The Regents of the University of California.
6c80476e4SDavid E. O'Brien  * All rights reserved.
7c80476e4SDavid E. O'Brien  *
8c80476e4SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
9c80476e4SDavid E. O'Brien  * modification, are permitted provided that the following conditions
10c80476e4SDavid E. O'Brien  * are met:
11c80476e4SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
12c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
13c80476e4SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
14c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
15c80476e4SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
1629301572SMark Peek  * 3. Neither the name of the University nor the names of its contributors
17c80476e4SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
18c80476e4SDavid E. O'Brien  *    without specific prior written permission.
19c80476e4SDavid E. O'Brien  *
20c80476e4SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21c80476e4SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22c80476e4SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23c80476e4SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24c80476e4SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25c80476e4SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26c80476e4SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27c80476e4SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28c80476e4SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29c80476e4SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30c80476e4SDavid E. O'Brien  * SUCH DAMAGE.
31c80476e4SDavid E. O'Brien  */
32c80476e4SDavid E. O'Brien #include "sh.h"
33c80476e4SDavid E. O'Brien #include "ed.h"
34c80476e4SDavid E. O'Brien #include "ed.defns.h"		/* for the function names */
35c80476e4SDavid E. O'Brien #include "tw.h"
36c80476e4SDavid E. O'Brien #include "tc.h"
373b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
38c80476e4SDavid E. O'Brien #include "nt.const.h"
3945e5710bSMark Peek #else /* WINNT_NATIVE */
4045e5710bSMark Peek #include <sys/wait.h>
413b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
42c80476e4SDavid E. O'Brien 
43c80476e4SDavid E. O'Brien #ifdef AFS
44c80476e4SDavid E. O'Brien #include <afs/stds.h>
45c80476e4SDavid E. O'Brien #include <afs/kautils.h>
46c80476e4SDavid E. O'Brien long ka_UserAuthenticateGeneral();
47c80476e4SDavid E. O'Brien #endif /* AFS */
48c80476e4SDavid E. O'Brien 
49c80476e4SDavid E. O'Brien #ifdef TESLA
50c80476e4SDavid E. O'Brien extern int do_logout;
51c80476e4SDavid E. O'Brien #endif /* TESLA */
52c80476e4SDavid E. O'Brien extern time_t t_period;
53c80476e4SDavid E. O'Brien extern int just_signaled;
5423338178SMark Peek static int precmd_active = 0;
5523338178SMark Peek static int jobcmd_active = 0; /* GrP */
56*cc698b49SBrooks Davis int postcmd_active = 0;
5723338178SMark Peek static int periodic_active = 0;
5823338178SMark Peek static int cwdcmd_active = 0;	/* PWP: for cwd_cmd */
5923338178SMark Peek static int beepcmd_active = 0;
6045e5710bSMark Peek static void (*alm_fun)(void) = NULL;
61c80476e4SDavid E. O'Brien 
6245e5710bSMark Peek static	void	 auto_logout	(void);
6345e5710bSMark Peek static	char	*xgetpass	(const char *);
6445e5710bSMark Peek static	void	 auto_lock	(void);
65c80476e4SDavid E. O'Brien #ifdef BSDJOBS
6645e5710bSMark Peek static	void	 insert		(struct wordent *, int);
6745e5710bSMark Peek static	void	 insert_we	(struct wordent *, struct wordent *);
6845e5710bSMark Peek static	int	 inlist		(Char *, Char *);
69c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
7045e5710bSMark Peek static	int	 tildecompare	(const void *, const void *);
7145e5710bSMark Peek static  Char    *gethomedir	(const Char *);
72c80476e4SDavid E. O'Brien #ifdef REMOTEHOST
7345e5710bSMark Peek static	void	 palarm		(int);
7445e5710bSMark Peek static	void	 getremotehost	(int);
75c80476e4SDavid E. O'Brien #endif /* REMOTEHOST */
76c80476e4SDavid E. O'Brien 
77c80476e4SDavid E. O'Brien /*
78c80476e4SDavid E. O'Brien  * Tops-C shell
79c80476e4SDavid E. O'Brien  */
80c80476e4SDavid E. O'Brien 
81c80476e4SDavid E. O'Brien /*
8245e5710bSMark Peek  * expand_lex: Take the given lex and return an expanded version of it.
8345e5710bSMark Peek  * First guy in lex list is ignored; last guy is ^J which we ignore.
8445e5710bSMark Peek  * Only take lex'es from position 'from' to position 'to' inclusive
85b2be84b1SDavid E. O'Brien  *
86b2be84b1SDavid E. O'Brien  * Note: csh sometimes sets bit 8 in characters which causes all kinds
87b2be84b1SDavid E. O'Brien  * of problems if we don't mask it here. Note: excl's in lexes have been
88b2be84b1SDavid E. O'Brien  * un-back-slashed and must be re-back-slashed
89b2be84b1SDavid E. O'Brien  *
90c80476e4SDavid E. O'Brien  */
91c80476e4SDavid E. O'Brien /* PWP: this is a combination of the old sprlex() and the expand_lex from
92c80476e4SDavid E. O'Brien    the magic-space stuff */
93c80476e4SDavid E. O'Brien 
94c80476e4SDavid E. O'Brien Char   *
expand_lex(const struct wordent * sp0,int from,int to)9545e5710bSMark Peek expand_lex(const struct wordent *sp0, int from, int to)
96c80476e4SDavid E. O'Brien {
9745e5710bSMark Peek     struct Strbuf buf = Strbuf_INIT;
9845e5710bSMark Peek     const struct wordent *sp;
9945e5710bSMark Peek     Char *s;
10023338178SMark Peek     Char prev_c;
10123338178SMark Peek     int i;
102c80476e4SDavid E. O'Brien 
103c80476e4SDavid E. O'Brien     prev_c = '\0';
104c80476e4SDavid E. O'Brien 
10545e5710bSMark Peek     if (!sp0 || (sp = sp0->next) == sp0 || sp == (sp0 = sp0->prev))
10645e5710bSMark Peek 	return Strbuf_finish(&buf); /* null lex */
107c80476e4SDavid E. O'Brien 
10845e5710bSMark Peek     for (i = 0; ; i++) {
109c80476e4SDavid E. O'Brien 	if ((i >= from) && (i <= to)) {	/* if in range */
11045e5710bSMark Peek 	    for (s = sp->word; *s; s++) {
111c80476e4SDavid E. O'Brien 		/*
112c80476e4SDavid E. O'Brien 		 * bugfix by Michael Bloom: anything but the current history
113c80476e4SDavid E. O'Brien 		 * character {(PWP) and backslash} seem to be dealt with
114c80476e4SDavid E. O'Brien 		 * elsewhere.
115c80476e4SDavid E. O'Brien 		 */
116c80476e4SDavid E. O'Brien 		if ((*s & QUOTE)
1179ccc37e3SMark Peek 		    && (((*s & TRIM) == HIST && HIST != '\0') ||
118c80476e4SDavid E. O'Brien 			(((*s & TRIM) == '\'') && (prev_c != '\\')) ||
11919d2e3deSDmitry Chagin 			(((*s & TRIM) == '\"') && (prev_c != '\\')))) {
12045e5710bSMark Peek 		    Strbuf_append1(&buf, '\\');
121c80476e4SDavid E. O'Brien 		}
12219d2e3deSDmitry Chagin #if INVALID_BYTE != 0
12319d2e3deSDmitry Chagin 		if ((*s & INVALID_BYTE) != INVALID_BYTE) /* *s < INVALID_BYTE */
12445e5710bSMark Peek 		    Strbuf_append1(&buf, *s & TRIM);
12519d2e3deSDmitry Chagin 		else
12619d2e3deSDmitry Chagin 		    Strbuf_append1(&buf, *s);
12719d2e3deSDmitry Chagin #else
12819d2e3deSDmitry Chagin 		Strbuf_append1(&buf, *s & TRIM);
12919d2e3deSDmitry Chagin #endif
130c80476e4SDavid E. O'Brien 		prev_c = *s;
131c80476e4SDavid E. O'Brien 	    }
13245e5710bSMark Peek 	    Strbuf_append1(&buf, ' ');
133c80476e4SDavid E. O'Brien 	}
134c80476e4SDavid E. O'Brien 	sp = sp->next;
135c80476e4SDavid E. O'Brien 	if (sp == sp0)
136c80476e4SDavid E. O'Brien 	    break;
137c80476e4SDavid E. O'Brien     }
13845e5710bSMark Peek     if (buf.len != 0)
13945e5710bSMark Peek 	buf.len--;		/* get rid of trailing space */
140c80476e4SDavid E. O'Brien 
14145e5710bSMark Peek     return Strbuf_finish(&buf);
142c80476e4SDavid E. O'Brien }
143c80476e4SDavid E. O'Brien 
144c80476e4SDavid E. O'Brien Char   *
sprlex(const struct wordent * sp0)14545e5710bSMark Peek sprlex(const struct wordent *sp0)
146c80476e4SDavid E. O'Brien {
14745e5710bSMark Peek     return expand_lex(sp0, 0, INT_MAX);
148c80476e4SDavid E. O'Brien }
149c80476e4SDavid E. O'Brien 
150c80476e4SDavid E. O'Brien 
151c80476e4SDavid E. O'Brien Char *
Itoa(int n,size_t min_digits,Char attributes)15245e5710bSMark Peek Itoa(int n, size_t min_digits, Char attributes)
153c80476e4SDavid E. O'Brien {
154c80476e4SDavid E. O'Brien     /*
155c80476e4SDavid E. O'Brien      * The array size here is derived from
156c80476e4SDavid E. O'Brien      *	log8(UINT_MAX)
157c80476e4SDavid E. O'Brien      * which is guaranteed to be enough for a decimal
158c80476e4SDavid E. O'Brien      * representation.  We add 1 because integer divide
159c80476e4SDavid E. O'Brien      * rounds down.
160c80476e4SDavid E. O'Brien      */
161c80476e4SDavid E. O'Brien #ifndef CHAR_BIT
162c80476e4SDavid E. O'Brien # define CHAR_BIT 8
163c80476e4SDavid E. O'Brien #endif
16445e5710bSMark Peek     Char buf[CHAR_BIT * sizeof(int) / 3 + 1], *res, *p, *s;
165c80476e4SDavid E. O'Brien     unsigned int un;	/* handle most negative # too */
166c80476e4SDavid E. O'Brien     int pad = (min_digits != 0);
167c80476e4SDavid E. O'Brien 
16845e5710bSMark Peek     if (sizeof(buf) - 1 < min_digits)
169c80476e4SDavid E. O'Brien 	min_digits = sizeof(buf) - 1;
170c80476e4SDavid E. O'Brien 
171c80476e4SDavid E. O'Brien     un = n;
17245e5710bSMark Peek     if (n < 0)
173c80476e4SDavid E. O'Brien 	un = -n;
174c80476e4SDavid E. O'Brien 
175c80476e4SDavid E. O'Brien     p = buf;
176c80476e4SDavid E. O'Brien     do {
177c80476e4SDavid E. O'Brien 	*p++ = un % 10 + '0';
178c80476e4SDavid E. O'Brien 	un /= 10;
1799ccc37e3SMark Peek     } while ((pad && (ssize_t)--min_digits > 0) || un != 0);
180c80476e4SDavid E. O'Brien 
18145e5710bSMark Peek     res = xmalloc((p - buf + 2) * sizeof(*res));
18245e5710bSMark Peek     s = res;
18345e5710bSMark Peek     if (n < 0)
18445e5710bSMark Peek 	*s++ = '-';
185c80476e4SDavid E. O'Brien     while (p > buf)
186c80476e4SDavid E. O'Brien 	*s++ = *--p | attributes;
187c80476e4SDavid E. O'Brien 
188c80476e4SDavid E. O'Brien     *s = '\0';
18945e5710bSMark Peek     return res;
190c80476e4SDavid E. O'Brien }
191c80476e4SDavid E. O'Brien 
192c80476e4SDavid E. O'Brien 
193c80476e4SDavid E. O'Brien /*ARGSUSED*/
194c80476e4SDavid E. O'Brien void
dolist(Char ** v,struct command * c)19545e5710bSMark Peek dolist(Char **v, struct command *c)
196c80476e4SDavid E. O'Brien {
19745e5710bSMark Peek     Char **globbed;
19819d2e3deSDmitry Chagin     int     i, k, ret = 0;
199c80476e4SDavid E. O'Brien     struct stat st;
200c80476e4SDavid E. O'Brien 
201c80476e4SDavid E. O'Brien     USE(c);
202c80476e4SDavid E. O'Brien     if (*++v == NULL) {
20345e5710bSMark Peek 	struct Strbuf word = Strbuf_INIT;
20445e5710bSMark Peek 
20545e5710bSMark Peek 	Strbuf_terminate(&word);
20645e5710bSMark Peek 	cleanup_push(&word, Strbuf_cleanup);
20745e5710bSMark Peek 	(void) t_search(&word, LIST, TW_ZERO, 0, STRNULL, 0);
20845e5710bSMark Peek 	cleanup_until(&word);
209c80476e4SDavid E. O'Brien 	return;
210c80476e4SDavid E. O'Brien     }
21145e5710bSMark Peek     v = glob_all_or_error(v);
21245e5710bSMark Peek     globbed = v;
21345e5710bSMark Peek     cleanup_push(globbed, blk_cleanup);
214c80476e4SDavid E. O'Brien     for (k = 0; v[k] != NULL && v[k][0] != '-'; k++)
215c80476e4SDavid E. O'Brien 	continue;
216c80476e4SDavid E. O'Brien     if (v[k]) {
217c80476e4SDavid E. O'Brien 	/*
218c80476e4SDavid E. O'Brien 	 * We cannot process a flag therefore we let ls do it right.
219c80476e4SDavid E. O'Brien 	 */
220c80476e4SDavid E. O'Brien 	Char *lspath;
221c80476e4SDavid E. O'Brien 	struct command *t;
222c80476e4SDavid E. O'Brien 	struct wordent cmd, *nextword, *lastword;
223c80476e4SDavid E. O'Brien 	Char   *cp;
224c80476e4SDavid E. O'Brien 	struct varent *vp;
225c80476e4SDavid E. O'Brien 
22645e5710bSMark Peek 	if (setintr) {
22745e5710bSMark Peek 	    pintr_disabled++;
22845e5710bSMark Peek 	    cleanup_push(&pintr_disabled, disabled_cleanup);
22945e5710bSMark Peek 	}
230c80476e4SDavid E. O'Brien 	if (seterr) {
23145e5710bSMark Peek 	    xfree(seterr);
232c80476e4SDavid E. O'Brien 	    seterr = NULL;
233c80476e4SDavid E. O'Brien 	}
234c80476e4SDavid E. O'Brien 
235c80476e4SDavid E. O'Brien 	lspath = STRls;
236c80476e4SDavid E. O'Brien 	STRmCF[1] = 'C';
237c80476e4SDavid E. O'Brien 	STRmCF[3] = '\0';
238c80476e4SDavid E. O'Brien 	/* Look at listflags, to add -A to the flags, to get a path
239c80476e4SDavid E. O'Brien 	   of ls if necessary */
24029301572SMark Peek 	if ((vp = adrof(STRlistflags)) != NULL && vp->vec != NULL &&
24129301572SMark Peek 	    vp->vec[0] != STRNULL) {
242c80476e4SDavid E. O'Brien 	    if (vp->vec[1] != NULL && vp->vec[1][0] != '\0')
243c80476e4SDavid E. O'Brien 		lspath = vp->vec[1];
244c80476e4SDavid E. O'Brien 	    for (cp = vp->vec[0]; *cp; cp++)
245c80476e4SDavid E. O'Brien 		switch (*cp) {
246c80476e4SDavid E. O'Brien 		case 'x':
247c80476e4SDavid E. O'Brien 		    STRmCF[1] = 'x';
248c80476e4SDavid E. O'Brien 		    break;
249c80476e4SDavid E. O'Brien 		case 'a':
250c80476e4SDavid E. O'Brien 		    STRmCF[3] = 'a';
251c80476e4SDavid E. O'Brien 		    break;
252c80476e4SDavid E. O'Brien 		case 'A':
253c80476e4SDavid E. O'Brien 		    STRmCF[3] = 'A';
254c80476e4SDavid E. O'Brien 		    break;
255c80476e4SDavid E. O'Brien 		default:
256c80476e4SDavid E. O'Brien 		    break;
257c80476e4SDavid E. O'Brien 		}
258c80476e4SDavid E. O'Brien 	}
259c80476e4SDavid E. O'Brien 
260c80476e4SDavid E. O'Brien 	cmd.word = STRNULL;
261c80476e4SDavid E. O'Brien 	lastword = &cmd;
26245e5710bSMark Peek 	nextword = xcalloc(1, sizeof cmd);
263c80476e4SDavid E. O'Brien 	nextword->word = Strsave(lspath);
264c80476e4SDavid E. O'Brien 	lastword->next = nextword;
265c80476e4SDavid E. O'Brien 	nextword->prev = lastword;
266c80476e4SDavid E. O'Brien 	lastword = nextword;
26745e5710bSMark Peek 	nextword = xcalloc(1, sizeof cmd);
268c80476e4SDavid E. O'Brien 	nextword->word = Strsave(STRmCF);
269c80476e4SDavid E. O'Brien 	lastword->next = nextword;
270c80476e4SDavid E. O'Brien 	nextword->prev = lastword;
271c80476e4SDavid E. O'Brien #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
272c80476e4SDavid E. O'Brien 	if (dspmbyte_ls) {
273c80476e4SDavid E. O'Brien 	    lastword = nextword;
27445e5710bSMark Peek 	    nextword = xcalloc(1, sizeof cmd);
275c80476e4SDavid E. O'Brien 	    nextword->word = Strsave(STRmmliteral);
276c80476e4SDavid E. O'Brien 	    lastword->next = nextword;
277c80476e4SDavid E. O'Brien 	    nextword->prev = lastword;
278c80476e4SDavid E. O'Brien 	}
279c80476e4SDavid E. O'Brien #endif
280c80476e4SDavid E. O'Brien #ifdef COLOR_LS_F
281c80476e4SDavid E. O'Brien 	if (color_context_ls) {
282c80476e4SDavid E. O'Brien 	    lastword = nextword;
28345e5710bSMark Peek 	    nextword = xcalloc(1, sizeof cmd);
284c80476e4SDavid E. O'Brien 	    nextword->word = Strsave(STRmmcolormauto);
285c80476e4SDavid E. O'Brien 	    lastword->next = nextword;
286c80476e4SDavid E. O'Brien 	    nextword->prev = lastword;
287c80476e4SDavid E. O'Brien 	}
288c80476e4SDavid E. O'Brien #endif /* COLOR_LS_F */
289c80476e4SDavid E. O'Brien 	lastword = nextword;
290c80476e4SDavid E. O'Brien 	for (cp = *v; cp; cp = *++v) {
29145e5710bSMark Peek 	    nextword = xcalloc(1, sizeof cmd);
29229301572SMark Peek 	    nextword->word = quote(Strsave(cp));
293c80476e4SDavid E. O'Brien 	    lastword->next = nextword;
294c80476e4SDavid E. O'Brien 	    nextword->prev = lastword;
295c80476e4SDavid E. O'Brien 	    lastword = nextword;
296c80476e4SDavid E. O'Brien 	}
297c80476e4SDavid E. O'Brien 	lastword->next = &cmd;
298c80476e4SDavid E. O'Brien 	cmd.prev = lastword;
29945e5710bSMark Peek 	cleanup_push(&cmd, lex_cleanup);
300c80476e4SDavid E. O'Brien 
301c80476e4SDavid E. O'Brien 	/* build a syntax tree for the command. */
302c80476e4SDavid E. O'Brien 	t = syntax(cmd.next, &cmd, 0);
30345e5710bSMark Peek 	cleanup_push(t, syntax_cleanup);
304c80476e4SDavid E. O'Brien 	if (seterr)
305c80476e4SDavid E. O'Brien 	    stderror(ERR_OLD);
306c80476e4SDavid E. O'Brien 	/* expand aliases like process() does */
307c80476e4SDavid E. O'Brien 	/* alias(&cmd); */
308c80476e4SDavid E. O'Brien 	/* execute the parse tree. */
30929301572SMark Peek 	execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, FALSE);
310c80476e4SDavid E. O'Brien 	/* done. free the lex list and parse tree. */
31145e5710bSMark Peek 	cleanup_until(&cmd);
312c80476e4SDavid E. O'Brien 	if (setintr)
31345e5710bSMark Peek 	    cleanup_until(&pintr_disabled);
314c80476e4SDavid E. O'Brien     }
315c80476e4SDavid E. O'Brien     else {
31645e5710bSMark Peek 	Char   *dp, *tmp;
31745e5710bSMark Peek 	struct Strbuf buf = Strbuf_INIT;
318c80476e4SDavid E. O'Brien 
31945e5710bSMark Peek 	cleanup_push(&buf, Strbuf_cleanup);
320c80476e4SDavid E. O'Brien 	for (k = 0, i = 0; v[k] != NULL; k++) {
321c80476e4SDavid E. O'Brien 	    tmp = dnormalize(v[k], symlinks == SYM_IGNORE);
32245e5710bSMark Peek 	    cleanup_push(tmp, xfree);
32345e5710bSMark Peek 	    dp = Strend(tmp) - 1;
324c80476e4SDavid E. O'Brien 	    if (*dp == '/' && dp != tmp)
325c80476e4SDavid E. O'Brien #ifdef apollo
326c80476e4SDavid E. O'Brien 		if (dp != &tmp[1])
327c80476e4SDavid E. O'Brien #endif /* apollo */
328c80476e4SDavid E. O'Brien 		*dp = '\0';
329c80476e4SDavid E. O'Brien 	    if (stat(short2str(tmp), &st) == -1) {
33045e5710bSMark Peek 		int err;
33145e5710bSMark Peek 
33245e5710bSMark Peek 		err = errno;
333c80476e4SDavid E. O'Brien 		if (k != i) {
334c80476e4SDavid E. O'Brien 		    if (i != 0)
335c80476e4SDavid E. O'Brien 			xputchar('\n');
336c80476e4SDavid E. O'Brien 		    print_by_column(STRNULL, &v[i], k - i, FALSE);
337c80476e4SDavid E. O'Brien 		}
33819d2e3deSDmitry Chagin 		haderr = 1;
33945e5710bSMark Peek 		xprintf("%S: %s.\n", tmp, strerror(err));
34019d2e3deSDmitry Chagin 		haderr = 0;
341c80476e4SDavid E. O'Brien 		i = k + 1;
34219d2e3deSDmitry Chagin 		ret = 1;
343c80476e4SDavid E. O'Brien 	    }
344c80476e4SDavid E. O'Brien 	    else if (S_ISDIR(st.st_mode)) {
345c80476e4SDavid E. O'Brien 		Char   *cp;
346c80476e4SDavid E. O'Brien 
347c80476e4SDavid E. O'Brien 		if (k != i) {
348c80476e4SDavid E. O'Brien 		    if (i != 0)
349c80476e4SDavid E. O'Brien 			xputchar('\n');
350c80476e4SDavid E. O'Brien 		    print_by_column(STRNULL, &v[i], k - i, FALSE);
351c80476e4SDavid E. O'Brien 		}
352c80476e4SDavid E. O'Brien 		if (k != 0 && v[1] != NULL)
353c80476e4SDavid E. O'Brien 		    xputchar('\n');
354c80476e4SDavid E. O'Brien 		xprintf("%S:\n", tmp);
35545e5710bSMark Peek 		buf.len = 0;
35645e5710bSMark Peek 		for (cp = tmp; *cp; cp++)
35745e5710bSMark Peek 		    Strbuf_append1(&buf, (*cp | QUOTE));
35845e5710bSMark Peek 		Strbuf_terminate(&buf);
35945e5710bSMark Peek 		dp = &buf.s[buf.len - 1];
360c80476e4SDavid E. O'Brien 		if (
3613b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
36245e5710bSMark Peek 		    (*dp != (Char) (':' | QUOTE)) &&
3633b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
36445e5710bSMark Peek 		    (*dp != (Char) ('/' | QUOTE))) {
36545e5710bSMark Peek 		    Strbuf_append1(&buf, '/');
36645e5710bSMark Peek 		    Strbuf_terminate(&buf);
36745e5710bSMark Peek 		} else
36845e5710bSMark Peek 		    *dp &= TRIM;
36945e5710bSMark Peek 		(void) t_search(&buf, LIST, TW_ZERO, 0, STRNULL, 0);
370c80476e4SDavid E. O'Brien 		i = k + 1;
371c80476e4SDavid E. O'Brien 	    }
37245e5710bSMark Peek 	    cleanup_until(tmp);
373c80476e4SDavid E. O'Brien 	}
37445e5710bSMark Peek 	cleanup_until(&buf);
375c80476e4SDavid E. O'Brien 	if (k != i) {
376c80476e4SDavid E. O'Brien 	    if (i != 0)
377c80476e4SDavid E. O'Brien 		xputchar('\n');
378c80476e4SDavid E. O'Brien 	    print_by_column(STRNULL, &v[i], k - i, FALSE);
379c80476e4SDavid E. O'Brien 	}
38019d2e3deSDmitry Chagin 	if (ret)
38119d2e3deSDmitry Chagin 	    stderror(ERR_SILENT);
382c80476e4SDavid E. O'Brien     }
383c80476e4SDavid E. O'Brien 
38445e5710bSMark Peek     cleanup_until(globbed);
385c80476e4SDavid E. O'Brien }
386c80476e4SDavid E. O'Brien 
38723338178SMark Peek extern int GotTermCaps;
388c80476e4SDavid E. O'Brien 
389c80476e4SDavid E. O'Brien /*ARGSUSED*/
390c80476e4SDavid E. O'Brien void
dotelltc(Char ** v,struct command * c)39145e5710bSMark Peek dotelltc(Char **v, struct command *c)
392c80476e4SDavid E. O'Brien {
39323338178SMark Peek     USE(v);
394c80476e4SDavid E. O'Brien     USE(c);
395c80476e4SDavid E. O'Brien     if (!GotTermCaps)
396c80476e4SDavid E. O'Brien 	GetTermCaps();
39723338178SMark Peek     TellTC();
398c80476e4SDavid E. O'Brien }
399c80476e4SDavid E. O'Brien 
400c80476e4SDavid E. O'Brien /*ARGSUSED*/
401c80476e4SDavid E. O'Brien void
doechotc(Char ** v,struct command * c)40245e5710bSMark Peek doechotc(Char **v, struct command *c)
403c80476e4SDavid E. O'Brien {
40423338178SMark Peek     USE(c);
405c80476e4SDavid E. O'Brien     if (!GotTermCaps)
406c80476e4SDavid E. O'Brien 	GetTermCaps();
407c80476e4SDavid E. O'Brien     EchoTC(++v);
408c80476e4SDavid E. O'Brien }
409c80476e4SDavid E. O'Brien 
410c80476e4SDavid E. O'Brien /*ARGSUSED*/
411c80476e4SDavid E. O'Brien void
dosettc(Char ** v,struct command * c)41245e5710bSMark Peek dosettc(Char **v, struct command *c)
413c80476e4SDavid E. O'Brien {
41445e5710bSMark Peek     char    *tv[2];
415c80476e4SDavid E. O'Brien 
41623338178SMark Peek     USE(c);
417c80476e4SDavid E. O'Brien     if (!GotTermCaps)
418c80476e4SDavid E. O'Brien 	GetTermCaps();
419c80476e4SDavid E. O'Brien 
42045e5710bSMark Peek     tv[0] = strsave(short2str(v[1]));
42145e5710bSMark Peek     cleanup_push(tv[0], xfree);
42245e5710bSMark Peek     tv[1] = strsave(short2str(v[2]));
42345e5710bSMark Peek     cleanup_push(tv[1], xfree);
424c80476e4SDavid E. O'Brien     SetTC(tv[0], tv[1]);
42545e5710bSMark Peek     cleanup_until(tv[0]);
426c80476e4SDavid E. O'Brien }
427c80476e4SDavid E. O'Brien 
428c80476e4SDavid E. O'Brien /* The dowhich() is by:
429c80476e4SDavid E. O'Brien  *  Andreas Luik <luik@isaak.isa.de>
430c80476e4SDavid E. O'Brien  *  I S A  GmbH - Informationssysteme fuer computerintegrierte Automatisierung
431c80476e4SDavid E. O'Brien  *  Azenberstr. 35
432c80476e4SDavid E. O'Brien  *  D-7000 Stuttgart 1
433c80476e4SDavid E. O'Brien  *  West-Germany
434c80476e4SDavid E. O'Brien  * Thanks!!
435c80476e4SDavid E. O'Brien  */
436c80476e4SDavid E. O'Brien int
cmd_expand(Char * cmd,Char ** str)43745e5710bSMark Peek cmd_expand(Char *cmd, Char **str)
438c80476e4SDavid E. O'Brien {
439c80476e4SDavid E. O'Brien     struct wordent lexp[3];
440c80476e4SDavid E. O'Brien     struct varent *vp;
441c80476e4SDavid E. O'Brien     int rv = TRUE;
442c80476e4SDavid E. O'Brien 
443c80476e4SDavid E. O'Brien     lexp[0].next = &lexp[1];
444c80476e4SDavid E. O'Brien     lexp[1].next = &lexp[2];
445c80476e4SDavid E. O'Brien     lexp[2].next = &lexp[0];
446c80476e4SDavid E. O'Brien 
447c80476e4SDavid E. O'Brien     lexp[0].prev = &lexp[2];
448c80476e4SDavid E. O'Brien     lexp[1].prev = &lexp[0];
449c80476e4SDavid E. O'Brien     lexp[2].prev = &lexp[1];
450c80476e4SDavid E. O'Brien 
451c80476e4SDavid E. O'Brien     lexp[0].word = STRNULL;
452c80476e4SDavid E. O'Brien     lexp[2].word = STRret;
453c80476e4SDavid E. O'Brien 
45429301572SMark Peek     if ((vp = adrof1(cmd, &aliases)) != NULL && vp->vec != NULL) {
455c80476e4SDavid E. O'Brien 	if (str == NULL) {
456c80476e4SDavid E. O'Brien 	    xprintf(CGETS(22, 1, "%S: \t aliased to "), cmd);
457c80476e4SDavid E. O'Brien 	    blkpr(vp->vec);
458c80476e4SDavid E. O'Brien 	    xputchar('\n');
459c80476e4SDavid E. O'Brien 	}
460c80476e4SDavid E. O'Brien 	else
46145e5710bSMark Peek 	    *str = blkexpand(vp->vec);
462c80476e4SDavid E. O'Brien     }
463c80476e4SDavid E. O'Brien     else {
464c80476e4SDavid E. O'Brien 	lexp[1].word = cmd;
465c80476e4SDavid E. O'Brien 	rv = tellmewhat(lexp, str);
466c80476e4SDavid E. O'Brien     }
467c80476e4SDavid E. O'Brien     return rv;
468c80476e4SDavid E. O'Brien }
469c80476e4SDavid E. O'Brien 
470c80476e4SDavid E. O'Brien 
471c80476e4SDavid E. O'Brien /*ARGSUSED*/
472c80476e4SDavid E. O'Brien void
dowhich(Char ** v,struct command * c)47345e5710bSMark Peek dowhich(Char **v, struct command *c)
474c80476e4SDavid E. O'Brien {
475c80476e4SDavid E. O'Brien     int rv = TRUE;
476c80476e4SDavid E. O'Brien     USE(c);
477c80476e4SDavid E. O'Brien 
478c80476e4SDavid E. O'Brien     /*
479c80476e4SDavid E. O'Brien      * We don't want to glob dowhich args because we lose quoteing
480c80476e4SDavid E. O'Brien      * E.g. which \ls if ls is aliased will not work correctly if
481c80476e4SDavid E. O'Brien      * we glob here.
482c80476e4SDavid E. O'Brien      */
483c80476e4SDavid E. O'Brien 
484c80476e4SDavid E. O'Brien     while (*++v)
485c80476e4SDavid E. O'Brien 	rv &= cmd_expand(*v, NULL);
486c80476e4SDavid E. O'Brien 
487c80476e4SDavid E. O'Brien     if (!rv)
48845e5710bSMark Peek 	setcopy(STRstatus, STR1, VAR_READWRITE);
489c80476e4SDavid E. O'Brien }
490c80476e4SDavid E. O'Brien 
49119d2e3deSDmitry Chagin static int
findvv(Char ** vv,const char * cp)49219d2e3deSDmitry Chagin findvv(Char **vv, const char *cp)
49319d2e3deSDmitry Chagin {
49419d2e3deSDmitry Chagin     for (; vv && *vv; vv++) {
49519d2e3deSDmitry Chagin 	size_t i;
49619d2e3deSDmitry Chagin 	for (i = 0; (*vv)[i] && (*vv)[i] == cp[i]; i++)
49719d2e3deSDmitry Chagin 	    continue;
49819d2e3deSDmitry Chagin 	if ((*vv)[i] == '\0' && cp[i] == '\0')
49919d2e3deSDmitry Chagin 	    return 1;
50019d2e3deSDmitry Chagin     }
50119d2e3deSDmitry Chagin     return 0;
50219d2e3deSDmitry Chagin }
50319d2e3deSDmitry Chagin 
504c80476e4SDavid E. O'Brien /* PWP: a hack to start up your stopped editor on a single keystroke */
505c80476e4SDavid E. O'Brien /* jbs - fixed hack so it worked :-) 3/28/89 */
506c80476e4SDavid E. O'Brien 
507c80476e4SDavid E. O'Brien struct process *
find_stop_ed(void)50845e5710bSMark Peek find_stop_ed(void)
509c80476e4SDavid E. O'Brien {
51023338178SMark Peek     struct process *pp, *retp;
51119d2e3deSDmitry Chagin     const char *ep = NULL, *vp = NULL;
51223338178SMark Peek     char *cp, *p;
51319d2e3deSDmitry Chagin     size_t epl = 0, vpl = 0;
51445e5710bSMark Peek     int pstatus;
51519d2e3deSDmitry Chagin     struct varent *varp;
51619d2e3deSDmitry Chagin     Char **vv;
517c80476e4SDavid E. O'Brien 
51819d2e3deSDmitry Chagin     if (pcurrent == NULL)	/* see if we have any jobs */
51919d2e3deSDmitry Chagin 	return NULL;		/* nope */
52019d2e3deSDmitry Chagin 
52119d2e3deSDmitry Chagin     if ((varp = adrof(STReditors)) != NULL)
52219d2e3deSDmitry Chagin 	vv = varp->vec;
52319d2e3deSDmitry Chagin     else
52419d2e3deSDmitry Chagin 	vv = NULL;
52519d2e3deSDmitry Chagin 
52619d2e3deSDmitry Chagin     if (! vv) {
527c80476e4SDavid E. O'Brien 	if ((ep = getenv("EDITOR")) != NULL) {	/* if we have a value */
528c80476e4SDavid E. O'Brien 	    if ((p = strrchr(ep, '/')) != NULL) 	/* if it has a path */
529c80476e4SDavid E. O'Brien 		ep = p + 1;		/* then we want only the last part */
530c80476e4SDavid E. O'Brien 	}
531c80476e4SDavid E. O'Brien 	else
532c80476e4SDavid E. O'Brien 	    ep = "ed";
533c80476e4SDavid E. O'Brien 
534c80476e4SDavid E. O'Brien 	if ((vp = getenv("VISUAL")) != NULL) {	/* if we have a value */
535c80476e4SDavid E. O'Brien 	    if ((p = strrchr(vp, '/')) != NULL) 	/* and it has a path */
536c80476e4SDavid E. O'Brien 		vp = p + 1;		/* then we want only the last part */
537c80476e4SDavid E. O'Brien 	}
538c80476e4SDavid E. O'Brien 	else
539c80476e4SDavid E. O'Brien 	    vp = "vi";
540c80476e4SDavid E. O'Brien 
54123338178SMark Peek 	for (vpl = 0; vp[vpl] && !isspace((unsigned char)vp[vpl]); vpl++)
542c80476e4SDavid E. O'Brien 	    continue;
54323338178SMark Peek 	for (epl = 0; ep[epl] && !isspace((unsigned char)ep[epl]); epl++)
544c80476e4SDavid E. O'Brien 	    continue;
54519d2e3deSDmitry Chagin     }
546c80476e4SDavid E. O'Brien 
547c80476e4SDavid E. O'Brien     retp = NULL;
548c80476e4SDavid E. O'Brien     for (pp = proclist.p_next; pp; pp = pp->p_next)
549c80476e4SDavid E. O'Brien 	if (pp->p_procid == pp->p_jobid) {
550c80476e4SDavid E. O'Brien 
551c80476e4SDavid E. O'Brien 	    /*
552c80476e4SDavid E. O'Brien 	     * Only foreground an edit session if it is suspended.  Some GUI
553c80476e4SDavid E. O'Brien 	     * editors have may be happily running in a separate window, no
554c80476e4SDavid E. O'Brien 	     * point in foregrounding these if they're already running - webb
555c80476e4SDavid E. O'Brien 	     */
556c80476e4SDavid E. O'Brien 	    pstatus = (int) (pp->p_flags & PALLSTATES);
557c80476e4SDavid E. O'Brien 	    if (pstatus != PINTERRUPTED && pstatus != PSTOPPED &&
558c80476e4SDavid E. O'Brien 		pstatus != PSIGNALED)
559c80476e4SDavid E. O'Brien 		continue;
560c80476e4SDavid E. O'Brien 
561c80476e4SDavid E. O'Brien 	    p = short2str(pp->p_command);
562c80476e4SDavid E. O'Brien 	    /* get the first word */
563c80476e4SDavid E. O'Brien 	    for (cp = p; *cp && !isspace((unsigned char) *cp); cp++)
564c80476e4SDavid E. O'Brien 		continue;
565c80476e4SDavid E. O'Brien 	    *cp = '\0';
566c80476e4SDavid E. O'Brien 
567c80476e4SDavid E. O'Brien 	    if ((cp = strrchr(p, '/')) != NULL)	/* and it has a path */
568c80476e4SDavid E. O'Brien 		cp = cp + 1;		/* then we want only the last part */
569c80476e4SDavid E. O'Brien 	    else
570c80476e4SDavid E. O'Brien 		cp = p;			/* else we get all of it */
571c80476e4SDavid E. O'Brien 
57219d2e3deSDmitry Chagin 	    /*
57319d2e3deSDmitry Chagin 	     * If we find the current name in the $editors array (if set)
57419d2e3deSDmitry Chagin 	     * or as $EDITOR or $VISUAL (if $editors not set), fg it.
57519d2e3deSDmitry Chagin 	     */
57619d2e3deSDmitry Chagin 	    if ((vv && findvv(vv, cp)) ||
57719d2e3deSDmitry Chagin 	        (epl && strncmp(ep, cp, epl) == 0 && cp[epl] == '\0') ||
57819d2e3deSDmitry Chagin 		(vpl && strncmp(vp, cp, vpl) == 0 && cp[vpl] == '\0')) {
579c80476e4SDavid E. O'Brien 		/*
580c80476e4SDavid E. O'Brien 		 * If there is a choice, then choose the current process if
581c80476e4SDavid E. O'Brien 		 * available, or the previous process otherwise, or else
582c80476e4SDavid E. O'Brien 		 * anything will do - Robert Webb (robertw@mulga.cs.mu.oz.au).
583c80476e4SDavid E. O'Brien 		 */
584c80476e4SDavid E. O'Brien 		if (pp == pcurrent)
585c80476e4SDavid E. O'Brien 		    return pp;
586c80476e4SDavid E. O'Brien 		else if (retp == NULL || pp == pprevious)
587c80476e4SDavid E. O'Brien 		    retp = pp;
588c80476e4SDavid E. O'Brien 	    }
589c80476e4SDavid E. O'Brien 	}
590c80476e4SDavid E. O'Brien 
591c80476e4SDavid E. O'Brien     return retp;		/* Will be NULL if we didn't find a job */
592c80476e4SDavid E. O'Brien }
593c80476e4SDavid E. O'Brien 
594c80476e4SDavid E. O'Brien void
fg_proc_entry(struct process * pp)59545e5710bSMark Peek fg_proc_entry(struct process *pp)
596c80476e4SDavid E. O'Brien {
597c80476e4SDavid E. O'Brien     jmp_buf_t osetexit;
59823338178SMark Peek     int    ohaderr;
599c80476e4SDavid E. O'Brien     Char    oGettingInput;
60045e5710bSMark Peek     size_t omark;
601c80476e4SDavid E. O'Brien 
602c80476e4SDavid E. O'Brien     getexit(osetexit);
603c80476e4SDavid E. O'Brien 
60445e5710bSMark Peek     pintr_disabled++;
605c80476e4SDavid E. O'Brien     oGettingInput = GettingInput;
606c80476e4SDavid E. O'Brien     GettingInput = 0;
607c80476e4SDavid E. O'Brien 
608c80476e4SDavid E. O'Brien     ohaderr = haderr;		/* we need to ignore setting of haderr due to
609c80476e4SDavid E. O'Brien 				 * process getting stopped by a signal */
61045e5710bSMark Peek     omark = cleanup_push_mark();
611c80476e4SDavid E. O'Brien     if (setexit() == 0) {	/* come back here after pjwait */
612c80476e4SDavid E. O'Brien 	pendjob();
613c80476e4SDavid E. O'Brien 	(void) alarm(0);	/* No autologout */
61445e5710bSMark Peek 	alrmcatch_disabled = 1;
615c80476e4SDavid E. O'Brien 	if (!pstart(pp, 1)) {
616c80476e4SDavid E. O'Brien 	    pp->p_procid = 0;
617c80476e4SDavid E. O'Brien 	    stderror(ERR_BADJOB, pp->p_command, strerror(errno));
618c80476e4SDavid E. O'Brien 	}
619c80476e4SDavid E. O'Brien 	pjwait(pp);
620c80476e4SDavid E. O'Brien     }
621c80476e4SDavid E. O'Brien     setalarm(1);		/* Autologout back on */
62245e5710bSMark Peek     cleanup_pop_mark(omark);
623c80476e4SDavid E. O'Brien     resexit(osetexit);
624c80476e4SDavid E. O'Brien     haderr = ohaderr;
625c80476e4SDavid E. O'Brien     GettingInput = oGettingInput;
626c80476e4SDavid E. O'Brien 
62745e5710bSMark Peek     disabled_cleanup(&pintr_disabled);
628c80476e4SDavid E. O'Brien }
629c80476e4SDavid E. O'Brien 
630c80476e4SDavid E. O'Brien static char *
xgetpass(const char * prm)63145e5710bSMark Peek xgetpass(const char *prm)
632c80476e4SDavid E. O'Brien {
63345e5710bSMark Peek     static struct strbuf pass; /* = strbuf_INIT; */
63445e5710bSMark Peek     int fd;
63545e5710bSMark Peek     sigset_t oset, set;
63645e5710bSMark Peek     struct sigaction sa, osa;
637c80476e4SDavid E. O'Brien 
63845e5710bSMark Peek     sa.sa_handler = SIG_IGN;
63945e5710bSMark Peek     sigemptyset(&sa.sa_mask);
64045e5710bSMark Peek     sa.sa_flags = 0;
64145e5710bSMark Peek     (void)sigaction(SIGINT, &sa, &osa);
64245e5710bSMark Peek 
64345e5710bSMark Peek     sigemptyset(&set);
64445e5710bSMark Peek     sigaddset(&set, SIGINT);
64545e5710bSMark Peek     (void)sigprocmask(SIG_UNBLOCK, &set, &oset);
64645e5710bSMark Peek 
64745e5710bSMark Peek     cleanup_push(&osa, sigint_cleanup);
64845e5710bSMark Peek     cleanup_push(&oset, sigprocmask_cleanup);
649c80476e4SDavid E. O'Brien     (void) Rawmode();	/* Make sure, cause we want echo off */
65045e5710bSMark Peek     fd = xopen("/dev/tty", O_RDWR|O_LARGEFILE);
65145e5710bSMark Peek     if (fd == -1)
652c80476e4SDavid E. O'Brien 	fd = SHIN;
65345e5710bSMark Peek     else
65445e5710bSMark Peek 	cleanup_push(&fd, open_cleanup);
655c80476e4SDavid E. O'Brien 
656c80476e4SDavid E. O'Brien     xprintf("%s", prm); flush();
65745e5710bSMark Peek     pass.len = 0;
65845e5710bSMark Peek     for (;;)  {
65945e5710bSMark Peek 	char c;
66045e5710bSMark Peek 
66145e5710bSMark Peek 	if (xread(fd, &c, 1) < 1 || c == '\n')
662c80476e4SDavid E. O'Brien 	    break;
66345e5710bSMark Peek 	strbuf_append1(&pass, c);
664c80476e4SDavid E. O'Brien     }
66545e5710bSMark Peek     strbuf_terminate(&pass);
666c80476e4SDavid E. O'Brien 
6676ae8b92aSGiorgos Keramidas     cleanup_until(&osa);
668c80476e4SDavid E. O'Brien 
66945e5710bSMark Peek     return pass.s;
670c80476e4SDavid E. O'Brien }
671c80476e4SDavid E. O'Brien 
67223338178SMark Peek #ifndef NO_CRYPT
67345e5710bSMark Peek #if !HAVE_DECL_CRYPT
67445e5710bSMark Peek     extern char *crypt ();
67523338178SMark Peek #endif
67645e5710bSMark Peek #ifdef HAVE_CRYPT_H
67723338178SMark Peek #include <crypt.h>
67823338178SMark Peek #endif
67923338178SMark Peek #endif
68023338178SMark Peek 
681c80476e4SDavid E. O'Brien /*
682c80476e4SDavid E. O'Brien  * Ask the user for his login password to continue working
683c80476e4SDavid E. O'Brien  * On systems that have a shadow password, this will only
684c80476e4SDavid E. O'Brien  * work for root, but what can we do?
685c80476e4SDavid E. O'Brien  *
686c80476e4SDavid E. O'Brien  * If we fail to get the password, then we log the user out
687c80476e4SDavid E. O'Brien  * immediately
688c80476e4SDavid E. O'Brien  */
689c80476e4SDavid E. O'Brien /*ARGSUSED*/
690c80476e4SDavid E. O'Brien static void
auto_lock(void)69145e5710bSMark Peek auto_lock(void)
692c80476e4SDavid E. O'Brien {
693c80476e4SDavid E. O'Brien #ifndef NO_CRYPT
694c80476e4SDavid E. O'Brien 
695c80476e4SDavid E. O'Brien     int i;
696c80476e4SDavid E. O'Brien     char *srpp = NULL;
697c80476e4SDavid E. O'Brien     struct passwd *pw;
698c80476e4SDavid E. O'Brien 
699c80476e4SDavid E. O'Brien #undef XCRYPT
700c80476e4SDavid E. O'Brien 
70145e5710bSMark Peek #if defined(HAVE_AUTH_H) && defined(HAVE_GETAUTHUID)
702c80476e4SDavid E. O'Brien 
703c80476e4SDavid E. O'Brien     struct authorization *apw;
70445e5710bSMark Peek     extern char *crypt16 (const char *, const char *);
705c80476e4SDavid E. O'Brien 
7069ccc37e3SMark Peek # define XCRYPT(pw, a, b) crypt16(a, b)
707c80476e4SDavid E. O'Brien 
70845e5710bSMark Peek     if ((pw = xgetpwuid(euid)) != NULL &&	/* effective user passwd  */
709c80476e4SDavid E. O'Brien         (apw = getauthuid(euid)) != NULL) 	/* enhanced ultrix passwd */
710c80476e4SDavid E. O'Brien 	srpp = apw->a_password;
711c80476e4SDavid E. O'Brien 
71223338178SMark Peek #elif defined(HAVE_SHADOW_H)
713c80476e4SDavid E. O'Brien 
714c80476e4SDavid E. O'Brien     struct spwd *spw;
715c80476e4SDavid E. O'Brien 
7169ccc37e3SMark Peek # define XCRYPT(pw, a, b) crypt(a, b)
717c80476e4SDavid E. O'Brien 
71845e5710bSMark Peek     if ((pw = xgetpwuid(euid)) != NULL)	{	/* effective user passwd  */
71945e5710bSMark Peek 	errno = 0;
72045e5710bSMark Peek 	while ((spw = getspnam(pw->pw_name)) == NULL && errno == EINTR) {
72145e5710bSMark Peek 	    handle_pending_signals();
72245e5710bSMark Peek 	    errno = 0;
72345e5710bSMark Peek 	}
72445e5710bSMark Peek 	if (spw != NULL)			 /* shadowed passwd	  */
725c80476e4SDavid E. O'Brien 	    srpp = spw->sp_pwdp;
72645e5710bSMark Peek     }
727c80476e4SDavid E. O'Brien 
72823338178SMark Peek #else
729c80476e4SDavid E. O'Brien 
7309ccc37e3SMark Peek 
7319ccc37e3SMark Peek #ifdef __CYGWIN__
7329ccc37e3SMark Peek # define XCRYPT(pw, a, b) cygwin_xcrypt(pw, a, b)
7339ccc37e3SMark Peek #else
7349ccc37e3SMark Peek # define XCRYPT(pw, a, b) crypt(a, b)
7359ccc37e3SMark Peek #endif
736c80476e4SDavid E. O'Brien 
7373b6eaa7bSAndrey A. Chernov #if !defined(__MVS__)
73845e5710bSMark Peek     if ((pw = xgetpwuid(euid)) != NULL)	/* effective user passwd  */
739c80476e4SDavid E. O'Brien 	srpp = pw->pw_passwd;
7403b6eaa7bSAndrey A. Chernov #endif /* !MVS */
741c80476e4SDavid E. O'Brien 
74223338178SMark Peek #endif
743c80476e4SDavid E. O'Brien 
744c80476e4SDavid E. O'Brien     if (srpp == NULL) {
74545e5710bSMark Peek 	auto_logout();
746c80476e4SDavid E. O'Brien 	/*NOTREACHED*/
747c80476e4SDavid E. O'Brien 	return;
748c80476e4SDavid E. O'Brien     }
749c80476e4SDavid E. O'Brien 
750c80476e4SDavid E. O'Brien     setalarm(0);		/* Not for locking any more */
751c80476e4SDavid E. O'Brien     xputchar('\n');
752c80476e4SDavid E. O'Brien     for (i = 0; i < 5; i++) {
753c80476e4SDavid E. O'Brien 	const char *crpp;
754c80476e4SDavid E. O'Brien 	char *pp;
755c80476e4SDavid E. O'Brien #ifdef AFS
756c80476e4SDavid E. O'Brien 	char *afsname;
757c80476e4SDavid E. O'Brien 	Char *safs;
758c80476e4SDavid E. O'Brien 
759c80476e4SDavid E. O'Brien 	if ((safs = varval(STRafsuser)) != STRNULL)
760c80476e4SDavid E. O'Brien 	    afsname = short2str(safs);
761c80476e4SDavid E. O'Brien 	else
762c80476e4SDavid E. O'Brien 	    if ((afsname = getenv("AFSUSER")) == NULL)
763c80476e4SDavid E. O'Brien 	        afsname = pw->pw_name;
764c80476e4SDavid E. O'Brien #endif
765c80476e4SDavid E. O'Brien 	pp = xgetpass("Password:");
766c80476e4SDavid E. O'Brien 
7679ccc37e3SMark Peek 	crpp = XCRYPT(pw, pp, srpp);
76819d2e3deSDmitry Chagin 	if ((crpp && strcmp(crpp, srpp) == 0)
769c80476e4SDavid E. O'Brien #ifdef AFS
770c80476e4SDavid E. O'Brien 	    || (ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION,
771c80476e4SDavid E. O'Brien 					   afsname,     /* name */
772c80476e4SDavid E. O'Brien 					   NULL,        /* instance */
773c80476e4SDavid E. O'Brien 					   NULL,        /* realm */
774c80476e4SDavid E. O'Brien 					   pp,          /* password */
775c80476e4SDavid E. O'Brien 					   0,           /* lifetime */
776c80476e4SDavid E. O'Brien 					   0, 0,         /* spare */
777c80476e4SDavid E. O'Brien 					   NULL)        /* reason */
778c80476e4SDavid E. O'Brien 	    == 0)
779c80476e4SDavid E. O'Brien #endif /* AFS */
780c80476e4SDavid E. O'Brien 	    ) {
78145e5710bSMark Peek 	    (void) memset(pp, 0, strlen(pp));
782c80476e4SDavid E. O'Brien 	    if (GettingInput && !just_signaled) {
783c80476e4SDavid E. O'Brien 		(void) Rawmode();
784c80476e4SDavid E. O'Brien 		ClearLines();
785c80476e4SDavid E. O'Brien 		ClearDisp();
786c80476e4SDavid E. O'Brien 		Refresh();
787c80476e4SDavid E. O'Brien 	    }
788c80476e4SDavid E. O'Brien 	    just_signaled = 0;
789c80476e4SDavid E. O'Brien 	    return;
790c80476e4SDavid E. O'Brien 	}
791c80476e4SDavid E. O'Brien 	xprintf(CGETS(22, 2, "\nIncorrect passwd for %s\n"), pw->pw_name);
792c80476e4SDavid E. O'Brien     }
793c80476e4SDavid E. O'Brien #endif /* NO_CRYPT */
79445e5710bSMark Peek     auto_logout();
795c80476e4SDavid E. O'Brien }
796c80476e4SDavid E. O'Brien 
797c80476e4SDavid E. O'Brien 
798c80476e4SDavid E. O'Brien static void
auto_logout(void)79945e5710bSMark Peek auto_logout(void)
800c80476e4SDavid E. O'Brien {
801c80476e4SDavid E. O'Brien     xprintf("auto-logout\n");
802c80476e4SDavid E. O'Brien     /* Don't leave the tty in raw mode */
803c80476e4SDavid E. O'Brien     if (editing)
804c80476e4SDavid E. O'Brien 	(void) Cookedmode();
80545e5710bSMark Peek     xclose(SHIN);
80645e5710bSMark Peek     setcopy(STRlogout, STRautomatic, VAR_READWRITE);
807c80476e4SDavid E. O'Brien     child = 1;
808c80476e4SDavid E. O'Brien #ifdef TESLA
809c80476e4SDavid E. O'Brien     do_logout = 1;
810c80476e4SDavid E. O'Brien #endif /* TESLA */
811c80476e4SDavid E. O'Brien     GettingInput = FALSE; /* make flush() work to write hist files. Huber*/
812c80476e4SDavid E. O'Brien     goodbye(NULL, NULL);
813c80476e4SDavid E. O'Brien }
814c80476e4SDavid E. O'Brien 
81545e5710bSMark Peek void
alrmcatch(void)81645e5710bSMark Peek alrmcatch(void)
817c80476e4SDavid E. O'Brien {
81845e5710bSMark Peek     (*alm_fun)();
819c80476e4SDavid E. O'Brien     setalarm(1);
820c80476e4SDavid E. O'Brien }
821c80476e4SDavid E. O'Brien 
822c80476e4SDavid E. O'Brien /*
823c80476e4SDavid E. O'Brien  * Karl Kleinpaste, 21oct1983.
824c80476e4SDavid E. O'Brien  * Added precmd(), which checks for the alias
825c80476e4SDavid E. O'Brien  * precmd in aliases.  If it's there, the alias
826c80476e4SDavid E. O'Brien  * is executed as a command.  This is done
827c80476e4SDavid E. O'Brien  * after mailchk() and just before print-
828c80476e4SDavid E. O'Brien  * ing the prompt.  Useful for things like printing
829c80476e4SDavid E. O'Brien  * one's current directory just before each command.
830c80476e4SDavid E. O'Brien  */
831c80476e4SDavid E. O'Brien void
precmd(void)83245e5710bSMark Peek precmd(void)
833c80476e4SDavid E. O'Brien {
83445e5710bSMark Peek     pintr_disabled++;
83545e5710bSMark Peek     cleanup_push(&pintr_disabled, disabled_cleanup);
836c80476e4SDavid E. O'Brien     if (precmd_active) {	/* an error must have been caught */
837c80476e4SDavid E. O'Brien 	aliasrun(2, STRunalias, STRprecmd);
838a15e6f9aSMark Peek 	xprintf("%s", CGETS(22, 3, "Faulty alias 'precmd' removed.\n"));
839c80476e4SDavid E. O'Brien 	goto leave;
840c80476e4SDavid E. O'Brien     }
841c80476e4SDavid E. O'Brien     precmd_active = 1;
842c80476e4SDavid E. O'Brien     if (!whyles && adrof1(STRprecmd, &aliases))
843c80476e4SDavid E. O'Brien 	aliasrun(1, STRprecmd, NULL);
844c80476e4SDavid E. O'Brien leave:
845c80476e4SDavid E. O'Brien     precmd_active = 0;
84645e5710bSMark Peek     cleanup_until(&pintr_disabled);
847c80476e4SDavid E. O'Brien }
848c80476e4SDavid E. O'Brien 
849c80476e4SDavid E. O'Brien void
postcmd(void)85045e5710bSMark Peek postcmd(void)
851c80476e4SDavid E. O'Brien {
85245e5710bSMark Peek     pintr_disabled++;
85345e5710bSMark Peek     cleanup_push(&pintr_disabled, disabled_cleanup);
854c80476e4SDavid E. O'Brien     if (postcmd_active) {	/* an error must have been caught */
855c80476e4SDavid E. O'Brien 	aliasrun(2, STRunalias, STRpostcmd);
856a15e6f9aSMark Peek 	xprintf("%s", CGETS(22, 3, "Faulty alias 'postcmd' removed.\n"));
857c80476e4SDavid E. O'Brien 	goto leave;
858c80476e4SDavid E. O'Brien     }
859c80476e4SDavid E. O'Brien     postcmd_active = 1;
860c80476e4SDavid E. O'Brien     if (!whyles && adrof1(STRpostcmd, &aliases))
861c80476e4SDavid E. O'Brien 	aliasrun(1, STRpostcmd, NULL);
862c80476e4SDavid E. O'Brien leave:
863c80476e4SDavid E. O'Brien     postcmd_active = 0;
86445e5710bSMark Peek     cleanup_until(&pintr_disabled);
865c80476e4SDavid E. O'Brien }
866c80476e4SDavid E. O'Brien 
867c80476e4SDavid E. O'Brien /*
868c80476e4SDavid E. O'Brien  * Paul Placeway  11/24/87  Added cwd_cmd by hacking precmd() into
869c80476e4SDavid E. O'Brien  * submission...  Run every time $cwd is set (after it is set).  Useful
870c80476e4SDavid E. O'Brien  * for putting your machine and cwd (or anything else) in an xterm title
871c80476e4SDavid E. O'Brien  * space.
872c80476e4SDavid E. O'Brien  */
873c80476e4SDavid E. O'Brien void
cwd_cmd(void)87445e5710bSMark Peek cwd_cmd(void)
875c80476e4SDavid E. O'Brien {
87645e5710bSMark Peek     pintr_disabled++;
87745e5710bSMark Peek     cleanup_push(&pintr_disabled, disabled_cleanup);
878c80476e4SDavid E. O'Brien     if (cwdcmd_active) {	/* an error must have been caught */
879c80476e4SDavid E. O'Brien 	aliasrun(2, STRunalias, STRcwdcmd);
880a15e6f9aSMark Peek 	xprintf("%s", CGETS(22, 4, "Faulty alias 'cwdcmd' removed.\n"));
881c80476e4SDavid E. O'Brien 	goto leave;
882c80476e4SDavid E. O'Brien     }
883c80476e4SDavid E. O'Brien     cwdcmd_active = 1;
884c80476e4SDavid E. O'Brien     if (!whyles && adrof1(STRcwdcmd, &aliases))
885c80476e4SDavid E. O'Brien 	aliasrun(1, STRcwdcmd, NULL);
886c80476e4SDavid E. O'Brien leave:
887c80476e4SDavid E. O'Brien     cwdcmd_active = 0;
88845e5710bSMark Peek     cleanup_until(&pintr_disabled);
889c80476e4SDavid E. O'Brien }
890c80476e4SDavid E. O'Brien 
891c80476e4SDavid E. O'Brien /*
892c80476e4SDavid E. O'Brien  * Joachim Hoenig  07/16/91  Added beep_cmd, run every time tcsh wishes
893c80476e4SDavid E. O'Brien  * to beep the terminal bell. Useful for playing nice sounds instead.
894c80476e4SDavid E. O'Brien  */
895c80476e4SDavid E. O'Brien void
beep_cmd(void)89645e5710bSMark Peek beep_cmd(void)
897c80476e4SDavid E. O'Brien {
89845e5710bSMark Peek     pintr_disabled++;
89945e5710bSMark Peek     cleanup_push(&pintr_disabled, disabled_cleanup);
900c80476e4SDavid E. O'Brien     if (beepcmd_active) {	/* an error must have been caught */
901c80476e4SDavid E. O'Brien 	aliasrun(2, STRunalias, STRbeepcmd);
902a15e6f9aSMark Peek 	xprintf("%s", CGETS(22, 5, "Faulty alias 'beepcmd' removed.\n"));
903*cc698b49SBrooks Davis 	goto leave;
904c80476e4SDavid E. O'Brien     }
905c80476e4SDavid E. O'Brien     beepcmd_active = 1;
906c80476e4SDavid E. O'Brien     if (!whyles && adrof1(STRbeepcmd, &aliases))
907c80476e4SDavid E. O'Brien 	aliasrun(1, STRbeepcmd, NULL);
908*cc698b49SBrooks Davis leave:
909c80476e4SDavid E. O'Brien     beepcmd_active = 0;
91045e5710bSMark Peek     cleanup_until(&pintr_disabled);
911c80476e4SDavid E. O'Brien }
912c80476e4SDavid E. O'Brien 
913c80476e4SDavid E. O'Brien 
914c80476e4SDavid E. O'Brien /*
915c80476e4SDavid E. O'Brien  * Karl Kleinpaste, 18 Jan 1984.
916c80476e4SDavid E. O'Brien  * Added period_cmd(), which executes the alias "periodic" every
917c80476e4SDavid E. O'Brien  * $tperiod minutes.  Useful for occasional checking of msgs and such.
918c80476e4SDavid E. O'Brien  */
919c80476e4SDavid E. O'Brien void
period_cmd(void)92045e5710bSMark Peek period_cmd(void)
921c80476e4SDavid E. O'Brien {
92223338178SMark Peek     Char *vp;
923c80476e4SDavid E. O'Brien     time_t  t, interval;
924c80476e4SDavid E. O'Brien 
925*cc698b49SBrooks Davis     if (whyles)
926*cc698b49SBrooks Davis 	return;
92745e5710bSMark Peek     pintr_disabled++;
92845e5710bSMark Peek     cleanup_push(&pintr_disabled, disabled_cleanup);
929c80476e4SDavid E. O'Brien     if (periodic_active) {	/* an error must have been caught */
930c80476e4SDavid E. O'Brien 	aliasrun(2, STRunalias, STRperiodic);
931a15e6f9aSMark Peek 	xprintf("%s", CGETS(22, 6, "Faulty alias 'periodic' removed.\n"));
932c80476e4SDavid E. O'Brien 	goto leave;
933c80476e4SDavid E. O'Brien     }
934c80476e4SDavid E. O'Brien     periodic_active = 1;
935c80476e4SDavid E. O'Brien     if (!whyles && adrof1(STRperiodic, &aliases)) {
936c80476e4SDavid E. O'Brien 	vp = varval(STRtperiod);
937c80476e4SDavid E. O'Brien 	if (vp == STRNULL) {
938c80476e4SDavid E. O'Brien 	    aliasrun(1, STRperiodic, NULL);
939c80476e4SDavid E. O'Brien 	    goto leave;
940c80476e4SDavid E. O'Brien 	}
941c80476e4SDavid E. O'Brien 	interval = getn(vp);
942c80476e4SDavid E. O'Brien 	(void) time(&t);
943c80476e4SDavid E. O'Brien 	if (t - t_period >= interval * 60) {
944c80476e4SDavid E. O'Brien 	    t_period = t;
945c80476e4SDavid E. O'Brien 	    aliasrun(1, STRperiodic, NULL);
946c80476e4SDavid E. O'Brien 	}
947c80476e4SDavid E. O'Brien     }
948c80476e4SDavid E. O'Brien leave:
949c80476e4SDavid E. O'Brien     periodic_active = 0;
95045e5710bSMark Peek     cleanup_until(&pintr_disabled);
951c80476e4SDavid E. O'Brien }
952c80476e4SDavid E. O'Brien 
9536767bd61SMark Peek 
9546767bd61SMark Peek /*
9556767bd61SMark Peek  * GrP Greg Parker May 2001
9566767bd61SMark Peek  * Added job_cmd(), which is run every time a job is started or
9576767bd61SMark Peek  * foregrounded. The command is passed a single argument, the string
9586767bd61SMark Peek  * used to start the job originally. With precmd, useful for setting
9596767bd61SMark Peek  * xterm titles.
9606767bd61SMark Peek  * Cloned from cwd_cmd().
9616767bd61SMark Peek  */
9626767bd61SMark Peek void
job_cmd(Char * args)96345e5710bSMark Peek job_cmd(Char *args)
9646767bd61SMark Peek {
965*cc698b49SBrooks Davis     if (whyles)
966*cc698b49SBrooks Davis 	return;
96745e5710bSMark Peek     pintr_disabled++;
96845e5710bSMark Peek     cleanup_push(&pintr_disabled, disabled_cleanup);
9696767bd61SMark Peek     if (jobcmd_active) {	/* an error must have been caught */
9706767bd61SMark Peek 	aliasrun(2, STRunalias, STRjobcmd);
971a15e6f9aSMark Peek 	xprintf("%s", CGETS(22, 14, "Faulty alias 'jobcmd' removed.\n"));
9726767bd61SMark Peek 	goto leave;
9736767bd61SMark Peek     }
9746767bd61SMark Peek     jobcmd_active = 1;
97529301572SMark Peek     if (!whyles && adrof1(STRjobcmd, &aliases)) {
97629301572SMark Peek 	struct process *pp = pcurrjob; /* put things back after the hook */
9776767bd61SMark Peek 	aliasrun(2, STRjobcmd, args);
97829301572SMark Peek 	pcurrjob = pp;
97929301572SMark Peek     }
9806767bd61SMark Peek leave:
9816767bd61SMark Peek     jobcmd_active = 0;
98245e5710bSMark Peek     cleanup_until(&pintr_disabled);
9836767bd61SMark Peek }
9846767bd61SMark Peek 
9856767bd61SMark Peek 
986c80476e4SDavid E. O'Brien /*
987c80476e4SDavid E. O'Brien  * Karl Kleinpaste, 21oct1983.
988c80476e4SDavid E. O'Brien  * Set up a one-word alias command, for use for special things.
989c80476e4SDavid E. O'Brien  * This code is based on the mainline of process().
990c80476e4SDavid E. O'Brien  */
991c80476e4SDavid E. O'Brien void
aliasrun(int cnt,Char * s1,Char * s2)99245e5710bSMark Peek aliasrun(int cnt, Char *s1, Char *s2)
993c80476e4SDavid E. O'Brien {
994c80476e4SDavid E. O'Brien     struct wordent w, *new1, *new2;	/* for holding alias name */
995c80476e4SDavid E. O'Brien     struct command *t = NULL;
996c80476e4SDavid E. O'Brien     jmp_buf_t osetexit;
997c80476e4SDavid E. O'Brien     int status;
99845e5710bSMark Peek     size_t omark;
999c80476e4SDavid E. O'Brien 
1000c80476e4SDavid E. O'Brien     getexit(osetexit);
1001c80476e4SDavid E. O'Brien     if (seterr) {
100245e5710bSMark Peek 	xfree(seterr);
1003c80476e4SDavid E. O'Brien 	seterr = NULL;	/* don't repeatedly print err msg. */
1004c80476e4SDavid E. O'Brien     }
1005c80476e4SDavid E. O'Brien     w.word = STRNULL;
100645e5710bSMark Peek     new1 = xcalloc(1, sizeof w);
1007c80476e4SDavid E. O'Brien     new1->word = Strsave(s1);
1008c80476e4SDavid E. O'Brien     if (cnt == 1) {
1009c80476e4SDavid E. O'Brien 	/* build a lex list with one word. */
1010c80476e4SDavid E. O'Brien 	w.next = w.prev = new1;
1011c80476e4SDavid E. O'Brien 	new1->next = new1->prev = &w;
1012c80476e4SDavid E. O'Brien     }
1013c80476e4SDavid E. O'Brien     else {
1014c80476e4SDavid E. O'Brien 	/* build a lex list with two words. */
101545e5710bSMark Peek 	new2 = xcalloc(1, sizeof w);
1016c80476e4SDavid E. O'Brien 	new2->word = Strsave(s2);
1017c80476e4SDavid E. O'Brien 	w.next = new2->prev = new1;
1018c80476e4SDavid E. O'Brien 	new1->next = w.prev = new2;
1019c80476e4SDavid E. O'Brien 	new1->prev = new2->next = &w;
1020c80476e4SDavid E. O'Brien     }
102145e5710bSMark Peek     cleanup_push(&w, lex_cleanup);
1022c80476e4SDavid E. O'Brien 
1023c80476e4SDavid E. O'Brien     /* Save the old status */
1024c80476e4SDavid E. O'Brien     status = getn(varval(STRstatus));
1025c80476e4SDavid E. O'Brien 
1026c80476e4SDavid E. O'Brien     /* expand aliases like process() does. */
1027c80476e4SDavid E. O'Brien     alias(&w);
1028c80476e4SDavid E. O'Brien     /* build a syntax tree for the command. */
1029c80476e4SDavid E. O'Brien     t = syntax(w.next, &w, 0);
103045e5710bSMark Peek     cleanup_push(t, syntax_cleanup);
1031c80476e4SDavid E. O'Brien     if (seterr)
1032c80476e4SDavid E. O'Brien 	stderror(ERR_OLD);
1033c80476e4SDavid E. O'Brien 
1034c80476e4SDavid E. O'Brien     psavejob();
103545e5710bSMark Peek     cleanup_push(&cnt, psavejob_cleanup); /* cnt is used only as a marker */
1036c80476e4SDavid E. O'Brien 
1037c80476e4SDavid E. O'Brien     /* catch any errors here */
103845e5710bSMark Peek     omark = cleanup_push_mark();
1039c80476e4SDavid E. O'Brien     if (setexit() == 0)
1040c80476e4SDavid E. O'Brien 	/* execute the parse tree. */
1041c80476e4SDavid E. O'Brien 	/*
1042c80476e4SDavid E. O'Brien 	 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
1043c80476e4SDavid E. O'Brien 	 * was execute(t, tpgrp);
1044c80476e4SDavid E. O'Brien 	 */
104529301572SMark Peek 	execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, TRUE);
104645e5710bSMark Peek     /* reset the error catcher to the old place */
104745e5710bSMark Peek     cleanup_pop_mark(omark);
104845e5710bSMark Peek     resexit(osetexit);
1049c80476e4SDavid E. O'Brien     if (haderr) {
1050c80476e4SDavid E. O'Brien 	haderr = 0;
1051c80476e4SDavid E. O'Brien 	/*
1052c80476e4SDavid E. O'Brien 	 * Either precmd, or cwdcmd, or periodic had an error. Call it again so
1053c80476e4SDavid E. O'Brien 	 * that it is removed
1054c80476e4SDavid E. O'Brien 	 */
1055c80476e4SDavid E. O'Brien 	if (precmd_active)
1056c80476e4SDavid E. O'Brien 	    precmd();
1057c80476e4SDavid E. O'Brien 	if (postcmd_active)
1058c80476e4SDavid E. O'Brien 	    postcmd();
1059c80476e4SDavid E. O'Brien #ifdef notdef
1060c80476e4SDavid E. O'Brien 	/*
1061c80476e4SDavid E. O'Brien 	 * XXX: On the other hand, just interrupting them causes an error too.
1062c80476e4SDavid E. O'Brien 	 * So if we hit ^C in the middle of cwdcmd or periodic the alias gets
1063c80476e4SDavid E. O'Brien 	 * removed. We don't want that. Note that we want to remove precmd
1064c80476e4SDavid E. O'Brien 	 * though, cause that could lead into an infinite loop. This should be
1065c80476e4SDavid E. O'Brien 	 * fixed correctly, but then haderr should give us the whole exit
1066c80476e4SDavid E. O'Brien 	 * status not just true or false.
1067c80476e4SDavid E. O'Brien 	 */
1068c80476e4SDavid E. O'Brien 	else if (cwdcmd_active)
1069c80476e4SDavid E. O'Brien 	    cwd_cmd();
1070c80476e4SDavid E. O'Brien 	else if (beepcmd_active)
1071c80476e4SDavid E. O'Brien 	    beep_cmd();
1072c80476e4SDavid E. O'Brien 	else if (periodic_active)
1073c80476e4SDavid E. O'Brien 	    period_cmd();
1074c80476e4SDavid E. O'Brien #endif /* notdef */
1075c80476e4SDavid E. O'Brien     }
107645e5710bSMark Peek     cleanup_until(&w);
1077c80476e4SDavid E. O'Brien     pendjob();
1078c80476e4SDavid E. O'Brien     /* Restore status */
10799ccc37e3SMark Peek     setv(STRstatus, putn((tcsh_number_t)status), VAR_READWRITE);
1080c80476e4SDavid E. O'Brien }
1081c80476e4SDavid E. O'Brien 
1082c80476e4SDavid E. O'Brien void
setalarm(int lck)108345e5710bSMark Peek setalarm(int lck)
1084c80476e4SDavid E. O'Brien {
1085c80476e4SDavid E. O'Brien     struct varent *vp;
1086c80476e4SDavid E. O'Brien     Char   *cp;
1087c80476e4SDavid E. O'Brien     unsigned alrm_time = 0, logout_time, lock_time;
1088c80476e4SDavid E. O'Brien     time_t cl, nl, sched_dif;
1089c80476e4SDavid E. O'Brien 
109029301572SMark Peek     if ((vp = adrof(STRautologout)) != NULL && vp->vec != NULL) {
1091c80476e4SDavid E. O'Brien 	if ((cp = vp->vec[0]) != 0) {
1092c80476e4SDavid E. O'Brien 	    if ((logout_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
109323338178SMark Peek #ifdef SOLARIS2
109423338178SMark Peek 		/*
109523338178SMark Peek 		 * Solaris alarm(2) uses a timer based in clock ticks
109623338178SMark Peek 		 * internally so it multiplies our value with CLK_TCK...
109723338178SMark Peek 		 * Of course that can overflow leading to unexpected
109823338178SMark Peek 		 * results, so we clip it here. Grr. Where is that
109923338178SMark Peek 		 * documented folks?
110023338178SMark Peek 		 */
110123338178SMark Peek 		if (logout_time >= 0x7fffffff / CLK_TCK)
110223338178SMark Peek 			logout_time = 0x7fffffff / CLK_TCK;
110323338178SMark Peek #endif /* SOLARIS2 */
1104c80476e4SDavid E. O'Brien 		alrm_time = logout_time;
1105c80476e4SDavid E. O'Brien 		alm_fun = auto_logout;
1106c80476e4SDavid E. O'Brien 	    }
1107c80476e4SDavid E. O'Brien 	}
1108c80476e4SDavid E. O'Brien 	if ((cp = vp->vec[1]) != 0) {
1109c80476e4SDavid E. O'Brien 	    if ((lock_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
1110c80476e4SDavid E. O'Brien 		if (lck) {
1111c80476e4SDavid E. O'Brien 		    if (alrm_time == 0 || lock_time < alrm_time) {
1112c80476e4SDavid E. O'Brien 			alrm_time = lock_time;
1113c80476e4SDavid E. O'Brien 			alm_fun = auto_lock;
1114c80476e4SDavid E. O'Brien 		    }
1115c80476e4SDavid E. O'Brien 		}
1116c80476e4SDavid E. O'Brien 		else /* lock_time always < alrm_time */
1117c80476e4SDavid E. O'Brien 		    if (alrm_time)
1118c80476e4SDavid E. O'Brien 			alrm_time -= lock_time;
1119c80476e4SDavid E. O'Brien 	    }
1120c80476e4SDavid E. O'Brien 	}
1121c80476e4SDavid E. O'Brien     }
1122c80476e4SDavid E. O'Brien     if ((nl = sched_next()) != -1) {
1123c80476e4SDavid E. O'Brien 	(void) time(&cl);
1124c80476e4SDavid E. O'Brien 	sched_dif = nl > cl ? nl - cl : 0;
1125c80476e4SDavid E. O'Brien 	if ((alrm_time == 0) || ((unsigned) sched_dif < alrm_time)) {
1126c80476e4SDavid E. O'Brien 	    alrm_time = ((unsigned) sched_dif) + 1;
1127c80476e4SDavid E. O'Brien 	    alm_fun = sched_run;
1128c80476e4SDavid E. O'Brien 	}
1129c80476e4SDavid E. O'Brien     }
113045e5710bSMark Peek     alrmcatch_disabled = 0;
1131c80476e4SDavid E. O'Brien     (void) alarm(alrm_time);	/* Autologout ON */
1132c80476e4SDavid E. O'Brien }
1133c80476e4SDavid E. O'Brien 
1134c80476e4SDavid E. O'Brien #undef RMDEBUG			/* For now... */
1135c80476e4SDavid E. O'Brien 
1136c80476e4SDavid E. O'Brien void
rmstar(struct wordent * cp)113745e5710bSMark Peek rmstar(struct wordent *cp)
1138c80476e4SDavid E. O'Brien {
1139c80476e4SDavid E. O'Brien     struct wordent *we, *args;
114023338178SMark Peek     struct wordent *tmp, *del;
1141c80476e4SDavid E. O'Brien 
1142c80476e4SDavid E. O'Brien #ifdef RMDEBUG
1143c80476e4SDavid E. O'Brien     static Char STRrmdebug[] = {'r', 'm', 'd', 'e', 'b', 'u', 'g', '\0'};
1144c80476e4SDavid E. O'Brien     Char   *tag;
1145c80476e4SDavid E. O'Brien #endif /* RMDEBUG */
1146c80476e4SDavid E. O'Brien     Char   *charac;
114719d2e3deSDmitry Chagin     int     ask, doit, star = 0, silent = 0, opintr_disabled;
1148c80476e4SDavid E. O'Brien 
1149c80476e4SDavid E. O'Brien     if (!adrof(STRrmstar))
1150c80476e4SDavid E. O'Brien 	return;
1151c80476e4SDavid E. O'Brien #ifdef RMDEBUG
1152c80476e4SDavid E. O'Brien     tag = varval(STRrmdebug);
1153c80476e4SDavid E. O'Brien #endif /* RMDEBUG */
1154c80476e4SDavid E. O'Brien     we = cp->next;
1155c80476e4SDavid E. O'Brien     while (*we->word == ';' && we != cp)
1156c80476e4SDavid E. O'Brien 	we = we->next;
115719d2e3deSDmitry Chagin     opintr_disabled = pintr_disabled;
115819d2e3deSDmitry Chagin     pintr_disabled = 0;
1159c80476e4SDavid E. O'Brien     while (we != cp) {
1160*cc698b49SBrooks Davis 	Char *cmd = we->word;
1161*cc698b49SBrooks Davis 	if (cmd[0] == STRQNULL[0])
1162*cc698b49SBrooks Davis 	    cmd++;
1163c80476e4SDavid E. O'Brien #ifdef RMDEBUG
1164c80476e4SDavid E. O'Brien 	if (*tag)
1165*cc698b49SBrooks Davis 	    xprintf(CGETS(22, 7, "parsing command line [%S]\n"), cmd);
1166c80476e4SDavid E. O'Brien #endif /* RMDEBUG */
1167*cc698b49SBrooks Davis 	if (!StrQcmp(cmd, STRrm)) {
1168c80476e4SDavid E. O'Brien 	    args = we->next;
1169c80476e4SDavid E. O'Brien 	    ask = (*args->word != '-');
1170c80476e4SDavid E. O'Brien 	    while (*args->word == '-' && !silent) {	/* check options */
1171c80476e4SDavid E. O'Brien 		for (charac = (args->word + 1); *charac && !silent; charac++)
1172c80476e4SDavid E. O'Brien 		    silent = (*charac == 'i' || *charac == 'f');
1173c80476e4SDavid E. O'Brien 		args = args->next;
1174c80476e4SDavid E. O'Brien 	    }
1175c80476e4SDavid E. O'Brien 	    ask = (ask || (!ask && !silent));
1176c80476e4SDavid E. O'Brien 	    if (ask) {
1177c80476e4SDavid E. O'Brien 		for (; !star && *args->word != ';'
1178c80476e4SDavid E. O'Brien 		     && args != cp; args = args->next)
1179c80476e4SDavid E. O'Brien 		    if (!Strcmp(args->word, STRstar))
1180c80476e4SDavid E. O'Brien 			star = 1;
1181c80476e4SDavid E. O'Brien 		if (ask && star) {
118219d2e3deSDmitry Chagin 		    doit = getYN(CGETS(22, 8,
118319d2e3deSDmitry Chagin 			"Do you really want to delete all files? [N/y] "));
1184c80476e4SDavid E. O'Brien 		    if (!doit) {
1185c80476e4SDavid E. O'Brien 			/* remove the command instead */
1186c80476e4SDavid E. O'Brien #ifdef RMDEBUG
1187c80476e4SDavid E. O'Brien 			if (*tag)
1188c80476e4SDavid E. O'Brien 			    xprintf(CGETS(22, 9,
1189c80476e4SDavid E. O'Brien 				    "skipping deletion of files!\n"));
1190c80476e4SDavid E. O'Brien #endif /* RMDEBUG */
1191c80476e4SDavid E. O'Brien 			for (tmp = we;
1192c80476e4SDavid E. O'Brien 			     *tmp->word != '\n' &&
1193c80476e4SDavid E. O'Brien 			     *tmp->word != ';' && tmp != cp;) {
1194c80476e4SDavid E. O'Brien 			    tmp->prev->next = tmp->next;
1195c80476e4SDavid E. O'Brien 			    tmp->next->prev = tmp->prev;
119645e5710bSMark Peek 			    xfree(tmp->word);
1197c80476e4SDavid E. O'Brien 			    del = tmp;
1198c80476e4SDavid E. O'Brien 			    tmp = tmp->next;
119945e5710bSMark Peek 			    xfree(del);
1200c80476e4SDavid E. O'Brien 			}
1201c80476e4SDavid E. O'Brien 			if (*tmp->word == ';') {
1202c80476e4SDavid E. O'Brien 			    tmp->prev->next = tmp->next;
1203c80476e4SDavid E. O'Brien 			    tmp->next->prev = tmp->prev;
120445e5710bSMark Peek 			    xfree(tmp->word);
1205c80476e4SDavid E. O'Brien 			    del = tmp;
12066767bd61SMark Peek 			    tmp = tmp->next;
120745e5710bSMark Peek 			    xfree(del);
1208c80476e4SDavid E. O'Brien 			}
12096767bd61SMark Peek 			we = tmp;
12106767bd61SMark Peek 			continue;
1211c80476e4SDavid E. O'Brien 		    }
1212c80476e4SDavid E. O'Brien 		}
1213c80476e4SDavid E. O'Brien 	    }
1214c80476e4SDavid E. O'Brien 	}
1215c80476e4SDavid E. O'Brien 	for (we = we->next;
1216c80476e4SDavid E. O'Brien 	     *we->word != ';' && we != cp;
1217c80476e4SDavid E. O'Brien 	     we = we->next)
1218c80476e4SDavid E. O'Brien 	    continue;
1219c80476e4SDavid E. O'Brien 	if (*we->word == ';')
1220c80476e4SDavid E. O'Brien 	    we = we->next;
1221c80476e4SDavid E. O'Brien     }
1222c80476e4SDavid E. O'Brien #ifdef RMDEBUG
1223c80476e4SDavid E. O'Brien     if (*tag) {
1224c80476e4SDavid E. O'Brien 	xprintf(CGETS(22, 10, "command line now is:\n"));
1225c80476e4SDavid E. O'Brien 	for (we = cp->next; we != cp; we = we->next)
1226*cc698b49SBrooks Davis 	    xprintf("[%S] ", we->word);
1227c80476e4SDavid E. O'Brien     }
1228c80476e4SDavid E. O'Brien #endif /* RMDEBUG */
122919d2e3deSDmitry Chagin     pintr_disabled = opintr_disabled;
1230c80476e4SDavid E. O'Brien     return;
1231c80476e4SDavid E. O'Brien }
1232c80476e4SDavid E. O'Brien 
1233c80476e4SDavid E. O'Brien #ifdef BSDJOBS
1234c80476e4SDavid E. O'Brien /* Check if command is in continue list
1235c80476e4SDavid E. O'Brien    and do a "aliasing" if it exists as a job in background */
1236c80476e4SDavid E. O'Brien 
1237c80476e4SDavid E. O'Brien #undef CNDEBUG			/* For now */
1238c80476e4SDavid E. O'Brien void
continue_jobs(struct wordent * cp)123945e5710bSMark Peek continue_jobs(struct wordent *cp)
1240c80476e4SDavid E. O'Brien {
1241c80476e4SDavid E. O'Brien     struct wordent *we;
124223338178SMark Peek     struct process *pp, *np;
1243c80476e4SDavid E. O'Brien     Char   *cmd, *continue_list, *continue_args_list;
1244c80476e4SDavid E. O'Brien 
1245c80476e4SDavid E. O'Brien #ifdef CNDEBUG
1246c80476e4SDavid E. O'Brien     Char   *tag;
1247c80476e4SDavid E. O'Brien     static Char STRcndebug[] =
1248c80476e4SDavid E. O'Brien     {'c', 'n', 'd', 'e', 'b', 'u', 'g', '\0'};
1249c80476e4SDavid E. O'Brien #endif /* CNDEBUG */
125023338178SMark Peek     int    in_cont_list, in_cont_arg_list;
1251c80476e4SDavid E. O'Brien 
1252c80476e4SDavid E. O'Brien 
1253c80476e4SDavid E. O'Brien #ifdef CNDEBUG
1254c80476e4SDavid E. O'Brien     tag = varval(STRcndebug);
1255c80476e4SDavid E. O'Brien #endif /* CNDEBUG */
1256c80476e4SDavid E. O'Brien     continue_list = varval(STRcontinue);
1257c80476e4SDavid E. O'Brien     continue_args_list = varval(STRcontinue_args);
1258c80476e4SDavid E. O'Brien     if (*continue_list == '\0' && *continue_args_list == '\0')
1259c80476e4SDavid E. O'Brien 	return;
1260c80476e4SDavid E. O'Brien 
1261c80476e4SDavid E. O'Brien     we = cp->next;
1262c80476e4SDavid E. O'Brien     while (*we->word == ';' && we != cp)
1263c80476e4SDavid E. O'Brien 	we = we->next;
1264c80476e4SDavid E. O'Brien     while (we != cp) {
1265c80476e4SDavid E. O'Brien #ifdef CNDEBUG
1266c80476e4SDavid E. O'Brien 	if (*tag)
1267c80476e4SDavid E. O'Brien 	    xprintf(CGETS(22, 11, "parsing command line\n"));
1268c80476e4SDavid E. O'Brien #endif /* CNDEBUG */
1269c80476e4SDavid E. O'Brien 	cmd = we->word;
1270c80476e4SDavid E. O'Brien 	in_cont_list = inlist(continue_list, cmd);
1271c80476e4SDavid E. O'Brien 	in_cont_arg_list = inlist(continue_args_list, cmd);
1272c80476e4SDavid E. O'Brien 	if (in_cont_list || in_cont_arg_list) {
1273c80476e4SDavid E. O'Brien #ifdef CNDEBUG
1274c80476e4SDavid E. O'Brien 	    if (*tag)
1275c80476e4SDavid E. O'Brien 		xprintf(CGETS(22, 12, "in one of the lists\n"));
1276c80476e4SDavid E. O'Brien #endif /* CNDEBUG */
1277c80476e4SDavid E. O'Brien 	    np = NULL;
1278c80476e4SDavid E. O'Brien 	    for (pp = proclist.p_next; pp; pp = pp->p_next) {
1279c80476e4SDavid E. O'Brien 		if (prefix(cmd, pp->p_command)) {
1280c80476e4SDavid E. O'Brien 		    if (pp->p_index) {
1281c80476e4SDavid E. O'Brien 			np = pp;
1282c80476e4SDavid E. O'Brien 			break;
1283c80476e4SDavid E. O'Brien 		    }
1284c80476e4SDavid E. O'Brien 		}
1285c80476e4SDavid E. O'Brien 	    }
1286c80476e4SDavid E. O'Brien 	    if (np) {
1287c80476e4SDavid E. O'Brien 		insert(we, in_cont_arg_list);
1288c80476e4SDavid E. O'Brien 	    }
1289c80476e4SDavid E. O'Brien 	}
1290c80476e4SDavid E. O'Brien 	for (we = we->next;
1291c80476e4SDavid E. O'Brien 	     *we->word != ';' && we != cp;
1292c80476e4SDavid E. O'Brien 	     we = we->next)
1293c80476e4SDavid E. O'Brien 	    continue;
1294c80476e4SDavid E. O'Brien 	if (*we->word == ';')
1295c80476e4SDavid E. O'Brien 	    we = we->next;
1296c80476e4SDavid E. O'Brien     }
1297c80476e4SDavid E. O'Brien #ifdef CNDEBUG
1298c80476e4SDavid E. O'Brien     if (*tag) {
1299c80476e4SDavid E. O'Brien 	xprintf(CGETS(22, 13, "command line now is:\n"));
1300c80476e4SDavid E. O'Brien 	for (we = cp->next; we != cp; we = we->next)
1301c80476e4SDavid E. O'Brien 	    xprintf("%S ", we->word);
1302c80476e4SDavid E. O'Brien     }
1303c80476e4SDavid E. O'Brien #endif /* CNDEBUG */
1304c80476e4SDavid E. O'Brien     return;
1305c80476e4SDavid E. O'Brien }
1306c80476e4SDavid E. O'Brien 
1307c80476e4SDavid E. O'Brien /* The actual "aliasing" of for backgrounds() is done here
1308c80476e4SDavid E. O'Brien    with the aid of insert_we().   */
1309c80476e4SDavid E. O'Brien static void
insert(struct wordent * pl,int file_args)131045e5710bSMark Peek insert(struct wordent *pl, int file_args)
1311c80476e4SDavid E. O'Brien {
1312c80476e4SDavid E. O'Brien     struct wordent *now, *last;
1313c80476e4SDavid E. O'Brien     Char   *cmd, *bcmd, *cp1, *cp2;
131445e5710bSMark Peek     size_t cmd_len;
131523338178SMark Peek     Char   *upause = STRunderpause;
131645e5710bSMark Peek     size_t p_len = Strlen(upause);
1317c80476e4SDavid E. O'Brien 
131845e5710bSMark Peek     cmd_len = Strlen(pl->word);
131945e5710bSMark Peek     cmd = xcalloc(1, (cmd_len + 1) * sizeof(Char));
1320c80476e4SDavid E. O'Brien     (void) Strcpy(cmd, pl->word);
1321c80476e4SDavid E. O'Brien /* Do insertions at beginning, first replace command word */
1322c80476e4SDavid E. O'Brien 
1323c80476e4SDavid E. O'Brien     if (file_args) {
1324c80476e4SDavid E. O'Brien 	now = pl;
132545e5710bSMark Peek 	xfree(now->word);
132645e5710bSMark Peek 	now->word = xcalloc(1, 5 * sizeof(Char));
1327c80476e4SDavid E. O'Brien 	(void) Strcpy(now->word, STRecho);
1328c80476e4SDavid E. O'Brien 
132945e5710bSMark Peek 	now = xcalloc(1, sizeof(struct wordent));
133045e5710bSMark Peek 	now->word = xcalloc(1, 6 * sizeof(Char));
1331c80476e4SDavid E. O'Brien 	(void) Strcpy(now->word, STRbackqpwd);
1332c80476e4SDavid E. O'Brien 	insert_we(now, pl);
1333c80476e4SDavid E. O'Brien 
1334c80476e4SDavid E. O'Brien 	for (last = now; *last->word != '\n' && *last->word != ';';
1335c80476e4SDavid E. O'Brien 	     last = last->next)
1336c80476e4SDavid E. O'Brien 	    continue;
1337c80476e4SDavid E. O'Brien 
133845e5710bSMark Peek 	now = xcalloc(1, sizeof(struct wordent));
133945e5710bSMark Peek 	now->word = xcalloc(1, 2 * sizeof(Char));
1340c80476e4SDavid E. O'Brien 	(void) Strcpy(now->word, STRgt);
1341c80476e4SDavid E. O'Brien 	insert_we(now, last->prev);
1342c80476e4SDavid E. O'Brien 
134345e5710bSMark Peek 	now = xcalloc(1, sizeof(struct wordent));
134445e5710bSMark Peek 	now->word = xcalloc(1, 2 * sizeof(Char));
1345c80476e4SDavid E. O'Brien 	(void) Strcpy(now->word, STRbang);
1346c80476e4SDavid E. O'Brien 	insert_we(now, last->prev);
1347c80476e4SDavid E. O'Brien 
134845e5710bSMark Peek 	now = xcalloc(1, sizeof(struct wordent));
134945e5710bSMark Peek 	now->word = xcalloc(1, (cmd_len + p_len + 4) * sizeof(Char));
1350c80476e4SDavid E. O'Brien 	cp1 = now->word;
1351c80476e4SDavid E. O'Brien 	cp2 = cmd;
1352c80476e4SDavid E. O'Brien 	*cp1++ = '~';
1353c80476e4SDavid E. O'Brien 	*cp1++ = '/';
1354c80476e4SDavid E. O'Brien 	*cp1++ = '.';
1355c80476e4SDavid E. O'Brien 	while ((*cp1++ = *cp2++) != '\0')
1356c80476e4SDavid E. O'Brien 	    continue;
1357c80476e4SDavid E. O'Brien 	cp1--;
135823338178SMark Peek 	cp2 = upause;
1359c80476e4SDavid E. O'Brien 	while ((*cp1++ = *cp2++) != '\0')
1360c80476e4SDavid E. O'Brien 	    continue;
1361c80476e4SDavid E. O'Brien 	insert_we(now, last->prev);
1362c80476e4SDavid E. O'Brien 
136345e5710bSMark Peek 	now = xcalloc(1, sizeof(struct wordent));
136445e5710bSMark Peek 	now->word = xcalloc(1, 2 * sizeof(Char));
1365c80476e4SDavid E. O'Brien 	(void) Strcpy(now->word, STRsemi);
1366c80476e4SDavid E. O'Brien 	insert_we(now, last->prev);
136745e5710bSMark Peek 	bcmd = xcalloc(1, (cmd_len + 2) * sizeof(Char));
136845e5710bSMark Peek 	*bcmd = '%';
136945e5710bSMark Peek 	Strcpy(bcmd + 1, cmd);
137045e5710bSMark Peek 	now = xcalloc(1, sizeof(struct wordent));
1371c80476e4SDavid E. O'Brien 	now->word = bcmd;
1372c80476e4SDavid E. O'Brien 	insert_we(now, last->prev);
1373c80476e4SDavid E. O'Brien     }
1374c80476e4SDavid E. O'Brien     else {
1375c80476e4SDavid E. O'Brien 	struct wordent *del;
1376c80476e4SDavid E. O'Brien 
1377c80476e4SDavid E. O'Brien 	now = pl;
137845e5710bSMark Peek 	xfree(now->word);
137945e5710bSMark Peek 	now->word = xcalloc(1, (cmd_len + 2) * sizeof(Char));
138045e5710bSMark Peek 	*now->word = '%';
138145e5710bSMark Peek 	Strcpy(now->word + 1, cmd);
1382c80476e4SDavid E. O'Brien 	for (now = now->next;
1383c80476e4SDavid E. O'Brien 	     *now->word != '\n' && *now->word != ';' && now != pl;) {
1384c80476e4SDavid E. O'Brien 	    now->prev->next = now->next;
1385c80476e4SDavid E. O'Brien 	    now->next->prev = now->prev;
138645e5710bSMark Peek 	    xfree(now->word);
1387c80476e4SDavid E. O'Brien 	    del = now;
1388c80476e4SDavid E. O'Brien 	    now = now->next;
138945e5710bSMark Peek 	    xfree(del);
1390c80476e4SDavid E. O'Brien 	}
1391c80476e4SDavid E. O'Brien     }
1392c80476e4SDavid E. O'Brien }
1393c80476e4SDavid E. O'Brien 
1394c80476e4SDavid E. O'Brien static void
insert_we(struct wordent * new,struct wordent * where)139545e5710bSMark Peek insert_we(struct wordent *new, struct wordent *where)
1396c80476e4SDavid E. O'Brien {
1397c80476e4SDavid E. O'Brien 
1398c80476e4SDavid E. O'Brien     new->prev = where;
1399c80476e4SDavid E. O'Brien     new->next = where->next;
1400c80476e4SDavid E. O'Brien     where->next = new;
1401c80476e4SDavid E. O'Brien     new->next->prev = new;
1402c80476e4SDavid E. O'Brien }
1403c80476e4SDavid E. O'Brien 
1404c80476e4SDavid E. O'Brien static int
inlist(Char * list,Char * name)140545e5710bSMark Peek inlist(Char *list, Char *name)
1406c80476e4SDavid E. O'Brien {
140723338178SMark Peek     Char *l, *n;
1408c80476e4SDavid E. O'Brien 
1409c80476e4SDavid E. O'Brien     l = list;
1410c80476e4SDavid E. O'Brien     n = name;
1411c80476e4SDavid E. O'Brien 
1412c80476e4SDavid E. O'Brien     while (*l && *n) {
1413c80476e4SDavid E. O'Brien 	if (*l == *n) {
1414c80476e4SDavid E. O'Brien 	    l++;
1415c80476e4SDavid E. O'Brien 	    n++;
1416c80476e4SDavid E. O'Brien 	    if (*n == '\0' && (*l == ' ' || *l == '\0'))
1417c80476e4SDavid E. O'Brien 		return (1);
1418c80476e4SDavid E. O'Brien 	    else
1419c80476e4SDavid E. O'Brien 		continue;
1420c80476e4SDavid E. O'Brien 	}
1421c80476e4SDavid E. O'Brien 	else {
1422c80476e4SDavid E. O'Brien 	    while (*l && *l != ' ')
1423c80476e4SDavid E. O'Brien 		l++;		/* skip to blank */
1424c80476e4SDavid E. O'Brien 	    while (*l && *l == ' ')
1425c80476e4SDavid E. O'Brien 		l++;		/* and find first nonblank character */
1426c80476e4SDavid E. O'Brien 	    n = name;
1427c80476e4SDavid E. O'Brien 	}
1428c80476e4SDavid E. O'Brien     }
1429c80476e4SDavid E. O'Brien     return (0);
1430c80476e4SDavid E. O'Brien }
1431c80476e4SDavid E. O'Brien 
1432c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
1433c80476e4SDavid E. O'Brien 
1434c80476e4SDavid E. O'Brien 
1435c80476e4SDavid E. O'Brien /*
1436c80476e4SDavid E. O'Brien  * Implement a small cache for tilde names. This is used primarily
1437c80476e4SDavid E. O'Brien  * to expand tilde names to directories, but also
1438c80476e4SDavid E. O'Brien  * we can find users from their home directories for the tilde
1439c80476e4SDavid E. O'Brien  * prompt, on machines where yp lookup is slow this can be a big win...
1440c80476e4SDavid E. O'Brien  * As with any cache this can run out of sync, rehash can sync it again.
1441c80476e4SDavid E. O'Brien  */
1442c80476e4SDavid E. O'Brien static struct tildecache {
1443c80476e4SDavid E. O'Brien     Char   *user;
1444c80476e4SDavid E. O'Brien     Char   *home;
144545e5710bSMark Peek     size_t  hlen;
1446c80476e4SDavid E. O'Brien }      *tcache = NULL;
1447c80476e4SDavid E. O'Brien 
1448c80476e4SDavid E. O'Brien #define TILINCR 10
144945e5710bSMark Peek size_t tlength = 0;
145045e5710bSMark Peek static size_t tsize = TILINCR;
1451c80476e4SDavid E. O'Brien 
1452c80476e4SDavid E. O'Brien static int
tildecompare(const void * xp1,const void * xp2)145345e5710bSMark Peek tildecompare(const void *xp1, const void *xp2)
1454c80476e4SDavid E. O'Brien {
145545e5710bSMark Peek     const struct tildecache *p1, *p2;
145645e5710bSMark Peek 
145745e5710bSMark Peek     p1 = xp1;
145845e5710bSMark Peek     p2 = xp2;
1459c80476e4SDavid E. O'Brien     return Strcmp(p1->user, p2->user);
1460c80476e4SDavid E. O'Brien }
1461c80476e4SDavid E. O'Brien 
1462c80476e4SDavid E. O'Brien static Char *
gethomedir(const Char * us)146345e5710bSMark Peek gethomedir(const Char *us)
1464c80476e4SDavid E. O'Brien {
146523338178SMark Peek     struct passwd *pp;
1466c80476e4SDavid E. O'Brien #ifdef HESIOD
1467c80476e4SDavid E. O'Brien     char **res, **res1, *cp;
1468c80476e4SDavid E. O'Brien     Char *rp;
1469c80476e4SDavid E. O'Brien #endif /* HESIOD */
1470c80476e4SDavid E. O'Brien 
147145e5710bSMark Peek     pp = xgetpwnam(short2str(us));
1472c80476e4SDavid E. O'Brien #ifdef YPBUGS
1473c80476e4SDavid E. O'Brien     fix_yp_bugs();
1474c80476e4SDavid E. O'Brien #endif /* YPBUGS */
14756767bd61SMark Peek     if (pp != NULL) {
147629301572SMark Peek #if 0
14776767bd61SMark Peek 	/* Don't return if root */
14786767bd61SMark Peek 	if (pp->pw_dir[0] == '/' && pp->pw_dir[1] == '\0')
14796767bd61SMark Peek 	    return NULL;
14806767bd61SMark Peek 	else
148129301572SMark Peek #endif
1482c80476e4SDavid E. O'Brien 	    return Strsave(str2short(pp->pw_dir));
14836767bd61SMark Peek     }
1484c80476e4SDavid E. O'Brien #ifdef HESIOD
1485c80476e4SDavid E. O'Brien     res = hes_resolve(short2str(us), "filsys");
14866767bd61SMark Peek     rp = NULL;
14876767bd61SMark Peek     if (res != NULL) {
14886767bd61SMark Peek 	if ((*res) != NULL) {
1489c80476e4SDavid E. O'Brien 	    /*
1490c80476e4SDavid E. O'Brien 	     * Look at the first token to determine how to interpret
1491c80476e4SDavid E. O'Brien 	     * the rest of it.
1492c80476e4SDavid E. O'Brien 	     * Yes, strtok is evil (it's not thread-safe), but it's also
1493c80476e4SDavid E. O'Brien 	     * easy to use.
1494c80476e4SDavid E. O'Brien 	     */
1495c80476e4SDavid E. O'Brien 	    cp = strtok(*res, " ");
1496c80476e4SDavid E. O'Brien 	    if (strcmp(cp, "AFS") == 0) {
1497c80476e4SDavid E. O'Brien 		/* next token is AFS pathname.. */
1498c80476e4SDavid E. O'Brien 		cp = strtok(NULL, " ");
1499c80476e4SDavid E. O'Brien 		if (cp != NULL)
1500c80476e4SDavid E. O'Brien 		    rp = Strsave(str2short(cp));
1501c80476e4SDavid E. O'Brien 	    } else if (strcmp(cp, "NFS") == 0) {
1502c80476e4SDavid E. O'Brien 		cp = NULL;
1503c80476e4SDavid E. O'Brien 		if ((strtok(NULL, " ")) && /* skip remote pathname */
1504c80476e4SDavid E. O'Brien 		    (strtok(NULL, " ")) && /* skip host */
1505c80476e4SDavid E. O'Brien 		    (strtok(NULL, " ")) && /* skip mode */
1506c80476e4SDavid E. O'Brien 		    (cp = strtok(NULL, " "))) {
1507c80476e4SDavid E. O'Brien 		    rp = Strsave(str2short(cp));
1508c80476e4SDavid E. O'Brien 		}
1509c80476e4SDavid E. O'Brien 	    }
1510c80476e4SDavid E. O'Brien 	}
1511c80476e4SDavid E. O'Brien 	for (res1 = res; *res1; res1++)
1512c80476e4SDavid E. O'Brien 	    free(*res1);
151329301572SMark Peek #if 0
151429301572SMark Peek 	/* Don't return if root */
15156767bd61SMark Peek 	if (rp != NULL && rp[0] == '/' && rp[1] == '\0') {
151645e5710bSMark Peek 	    xfree(rp);
15176767bd61SMark Peek 	    rp = NULL;
15186767bd61SMark Peek 	}
151929301572SMark Peek #endif
1520c80476e4SDavid E. O'Brien 	return rp;
1521c80476e4SDavid E. O'Brien     }
1522c80476e4SDavid E. O'Brien #endif /* HESIOD */
1523c80476e4SDavid E. O'Brien     return NULL;
1524c80476e4SDavid E. O'Brien }
1525c80476e4SDavid E. O'Brien 
1526c80476e4SDavid E. O'Brien Char   *
gettilde(const Char * us)152745e5710bSMark Peek gettilde(const Char *us)
1528c80476e4SDavid E. O'Brien {
1529c80476e4SDavid E. O'Brien     struct tildecache *bp1, *bp2, *bp;
1530c80476e4SDavid E. O'Brien     Char *hd;
1531c80476e4SDavid E. O'Brien 
1532c80476e4SDavid E. O'Brien     /* Ignore NIS special names */
1533c80476e4SDavid E. O'Brien     if (*us == '+' || *us == '-')
1534c80476e4SDavid E. O'Brien 	return NULL;
1535c80476e4SDavid E. O'Brien 
1536c80476e4SDavid E. O'Brien     if (tcache == NULL)
153745e5710bSMark Peek 	tcache = xmalloc(TILINCR * sizeof(struct tildecache));
1538c80476e4SDavid E. O'Brien     /*
1539c80476e4SDavid E. O'Brien      * Binary search
1540c80476e4SDavid E. O'Brien      */
1541c80476e4SDavid E. O'Brien     for (bp1 = tcache, bp2 = tcache + tlength; bp1 < bp2;) {
154223338178SMark Peek 	int i;
1543c80476e4SDavid E. O'Brien 
1544c80476e4SDavid E. O'Brien 	bp = bp1 + ((bp2 - bp1) >> 1);
1545c80476e4SDavid E. O'Brien 	if ((i = *us - *bp->user) == 0 && (i = Strcmp(us, bp->user)) == 0)
1546c80476e4SDavid E. O'Brien 	    return (bp->home);
1547c80476e4SDavid E. O'Brien 	if (i < 0)
1548c80476e4SDavid E. O'Brien 	    bp2 = bp;
1549c80476e4SDavid E. O'Brien 	else
1550c80476e4SDavid E. O'Brien 	    bp1 = bp + 1;
1551c80476e4SDavid E. O'Brien     }
1552c80476e4SDavid E. O'Brien     /*
1553c80476e4SDavid E. O'Brien      * Not in the cache, try to get it from the passwd file
1554c80476e4SDavid E. O'Brien      */
1555c80476e4SDavid E. O'Brien     hd = gethomedir(us);
1556c80476e4SDavid E. O'Brien     if (hd == NULL)
1557c80476e4SDavid E. O'Brien 	return NULL;
1558c80476e4SDavid E. O'Brien 
1559c80476e4SDavid E. O'Brien     /*
1560c80476e4SDavid E. O'Brien      * Update the cache
1561c80476e4SDavid E. O'Brien      */
1562c80476e4SDavid E. O'Brien     tcache[tlength].user = Strsave(us);
1563c80476e4SDavid E. O'Brien     tcache[tlength].home = hd;
156445e5710bSMark Peek     tcache[tlength++].hlen = Strlen(hd);
1565c80476e4SDavid E. O'Brien 
156645e5710bSMark Peek     qsort(tcache, tlength, sizeof(struct tildecache), tildecompare);
1567c80476e4SDavid E. O'Brien 
1568c80476e4SDavid E. O'Brien     if (tlength == tsize) {
1569c80476e4SDavid E. O'Brien 	tsize += TILINCR;
157045e5710bSMark Peek 	tcache = xrealloc(tcache, tsize * sizeof(struct tildecache));
1571c80476e4SDavid E. O'Brien     }
1572c80476e4SDavid E. O'Brien     return (hd);
1573c80476e4SDavid E. O'Brien }
1574c80476e4SDavid E. O'Brien 
1575c80476e4SDavid E. O'Brien /*
1576c80476e4SDavid E. O'Brien  * Return the username if the directory path passed contains a
1577c80476e4SDavid E. O'Brien  * user's home directory in the tilde cache, otherwise return NULL
1578c80476e4SDavid E. O'Brien  * hm points to the place where the path became different.
1579c80476e4SDavid E. O'Brien  * Special case: Our own home directory.
1580c80476e4SDavid E. O'Brien  * If we are passed a null pointer, then we flush the cache.
1581c80476e4SDavid E. O'Brien  */
1582c80476e4SDavid E. O'Brien Char   *
getusername(Char ** hm)158345e5710bSMark Peek getusername(Char **hm)
1584c80476e4SDavid E. O'Brien {
1585c80476e4SDavid E. O'Brien     Char   *h, *p;
158645e5710bSMark Peek     size_t i, j;
1587c80476e4SDavid E. O'Brien 
1588c80476e4SDavid E. O'Brien     if (hm == NULL) {
1589c80476e4SDavid E. O'Brien 	for (i = 0; i < tlength; i++) {
159045e5710bSMark Peek 	    xfree(tcache[i].home);
159145e5710bSMark Peek 	    xfree(tcache[i].user);
1592c80476e4SDavid E. O'Brien 	}
159345e5710bSMark Peek 	xfree(tcache);
1594c80476e4SDavid E. O'Brien 	tlength = 0;
1595c80476e4SDavid E. O'Brien 	tsize = TILINCR;
1596c80476e4SDavid E. O'Brien 	tcache = NULL;
1597c80476e4SDavid E. O'Brien 	return NULL;
1598c80476e4SDavid E. O'Brien     }
159945e5710bSMark Peek     p = *hm;
1600c80476e4SDavid E. O'Brien     if (((h = varval(STRhome)) != STRNULL) &&
160145e5710bSMark Peek 	(Strncmp(p, h, j = Strlen(h)) == 0) &&
1602c80476e4SDavid E. O'Brien 	(p[j] == '/' || p[j] == '\0')) {
1603c80476e4SDavid E. O'Brien 	*hm = &p[j];
1604c80476e4SDavid E. O'Brien 	return STRNULL;
1605c80476e4SDavid E. O'Brien     }
1606c80476e4SDavid E. O'Brien     for (i = 0; i < tlength; i++)
160745e5710bSMark Peek 	if ((Strncmp(p, tcache[i].home, (j = tcache[i].hlen)) == 0) &&
160845e5710bSMark Peek 	    (p[j] == '/' || p[j] == '\0')) {
1609c80476e4SDavid E. O'Brien 	    *hm = &p[j];
1610c80476e4SDavid E. O'Brien 	    return tcache[i].user;
1611c80476e4SDavid E. O'Brien 	}
1612c80476e4SDavid E. O'Brien     return NULL;
1613c80476e4SDavid E. O'Brien }
1614c80476e4SDavid E. O'Brien 
1615c80476e4SDavid E. O'Brien 
1616c80476e4SDavid E. O'Brien /*
1617c80476e4SDavid E. O'Brien  * set the shell-level var to 1 or apply change to it.
1618c80476e4SDavid E. O'Brien  */
1619c80476e4SDavid E. O'Brien void
shlvl(int val)162045e5710bSMark Peek shlvl(int val)
1621c80476e4SDavid E. O'Brien {
1622c80476e4SDavid E. O'Brien     char *cp;
1623c80476e4SDavid E. O'Brien 
1624c80476e4SDavid E. O'Brien     if ((cp = getenv("SHLVL")) != NULL) {
1625c80476e4SDavid E. O'Brien 
1626c80476e4SDavid E. O'Brien 	if (loginsh)
1627c80476e4SDavid E. O'Brien 	    val = 1;
1628c80476e4SDavid E. O'Brien 	else
1629c80476e4SDavid E. O'Brien 	    val += atoi(cp);
1630c80476e4SDavid E. O'Brien 
1631c80476e4SDavid E. O'Brien 	if (val <= 0) {
1632c80476e4SDavid E. O'Brien 	    if (adrof(STRshlvl) != NULL)
1633c80476e4SDavid E. O'Brien 		unsetv(STRshlvl);
1634c80476e4SDavid E. O'Brien 	    Unsetenv(STRKSHLVL);
1635c80476e4SDavid E. O'Brien 	}
1636c80476e4SDavid E. O'Brien 	else {
163745e5710bSMark Peek 	    Char *p;
1638c80476e4SDavid E. O'Brien 
163945e5710bSMark Peek 	    p = Itoa(val, 0, 0);
164045e5710bSMark Peek 	    cleanup_push(p, xfree);
164145e5710bSMark Peek 	    setv(STRshlvl, p, VAR_READWRITE);
164245e5710bSMark Peek 	    cleanup_ignore(p);
164345e5710bSMark Peek 	    cleanup_until(p);
164445e5710bSMark Peek 	    tsetenv(STRKSHLVL, p);
1645c80476e4SDavid E. O'Brien 	}
1646c80476e4SDavid E. O'Brien     }
1647c80476e4SDavid E. O'Brien     else {
164845e5710bSMark Peek 	setcopy(STRshlvl, STR1, VAR_READWRITE);
164945e5710bSMark Peek 	tsetenv(STRKSHLVL, STR1);
1650c80476e4SDavid E. O'Brien     }
1651c80476e4SDavid E. O'Brien }
1652c80476e4SDavid E. O'Brien 
1653c80476e4SDavid E. O'Brien 
1654c80476e4SDavid E. O'Brien /* fixio():
1655c80476e4SDavid E. O'Brien  *	Try to recover from a read error
1656c80476e4SDavid E. O'Brien  */
1657c80476e4SDavid E. O'Brien int
fixio(int fd,int e)165845e5710bSMark Peek fixio(int fd, int e)
1659c80476e4SDavid E. O'Brien {
1660c80476e4SDavid E. O'Brien     switch (e) {
1661c80476e4SDavid E. O'Brien     case -1:	/* Make sure that the code is reachable */
1662c80476e4SDavid E. O'Brien 
1663c80476e4SDavid E. O'Brien #ifdef EWOULDBLOCK
1664c80476e4SDavid E. O'Brien     case EWOULDBLOCK:
1665c80476e4SDavid E. O'Brien # define FDRETRY
1666c80476e4SDavid E. O'Brien #endif /* EWOULDBLOCK */
1667c80476e4SDavid E. O'Brien 
1668c80476e4SDavid E. O'Brien #if defined(POSIX) && defined(EAGAIN)
1669c80476e4SDavid E. O'Brien # if !defined(EWOULDBLOCK) || EWOULDBLOCK != EAGAIN
1670c80476e4SDavid E. O'Brien     case EAGAIN:
1671c80476e4SDavid E. O'Brien #  define FDRETRY
1672c80476e4SDavid E. O'Brien # endif /* !EWOULDBLOCK || EWOULDBLOCK != EAGAIN */
1673c80476e4SDavid E. O'Brien #endif /* POSIX && EAGAIN */
1674c80476e4SDavid E. O'Brien 
16759ccc37e3SMark Peek 	e = -1;
1676c80476e4SDavid E. O'Brien #ifdef FDRETRY
1677c80476e4SDavid E. O'Brien # ifdef F_SETFL
1678c80476e4SDavid E. O'Brien /*
1679c80476e4SDavid E. O'Brien  * Great! we have on suns 3 flavors and 5 names...
1680c80476e4SDavid E. O'Brien  * I hope that will cover everything.
1681c80476e4SDavid E. O'Brien  * I added some more defines... many systems have different defines.
1682c80476e4SDavid E. O'Brien  * Rather than dealing with getting the right includes, we'll just
1683c80476e4SDavid E. O'Brien  * cover all the known possibilities here.  -- sterling@netcom.com
1684c80476e4SDavid E. O'Brien  */
1685c80476e4SDavid E. O'Brien #  ifndef O_NONBLOCK
1686c80476e4SDavid E. O'Brien #   define O_NONBLOCK 0
1687c80476e4SDavid E. O'Brien #  endif /* O_NONBLOCK */
1688c80476e4SDavid E. O'Brien #  ifndef O_NDELAY
1689c80476e4SDavid E. O'Brien #   define O_NDELAY 0
1690c80476e4SDavid E. O'Brien #  endif /* O_NDELAY */
1691c80476e4SDavid E. O'Brien #  ifndef FNBIO
1692c80476e4SDavid E. O'Brien #   define FNBIO 0
1693c80476e4SDavid E. O'Brien #  endif /* FNBIO */
1694c80476e4SDavid E. O'Brien #  ifndef _FNBIO
1695c80476e4SDavid E. O'Brien #   define _FNBIO 0
1696c80476e4SDavid E. O'Brien #  endif /* _FNBIO */
1697c80476e4SDavid E. O'Brien #  ifndef FNONBIO
1698c80476e4SDavid E. O'Brien #   define FNONBIO 0
1699c80476e4SDavid E. O'Brien #  endif /* FNONBIO */
1700c80476e4SDavid E. O'Brien #  ifndef FNONBLOCK
1701c80476e4SDavid E. O'Brien #   define FNONBLOCK 0
1702c80476e4SDavid E. O'Brien #  endif /* FNONBLOCK */
1703c80476e4SDavid E. O'Brien #  ifndef _FNONBLOCK
1704c80476e4SDavid E. O'Brien #   define _FNONBLOCK 0
1705c80476e4SDavid E. O'Brien #  endif /* _FNONBLOCK */
1706c80476e4SDavid E. O'Brien #  ifndef FNDELAY
1707c80476e4SDavid E. O'Brien #   define FNDELAY 0
1708c80476e4SDavid E. O'Brien #  endif /* FNDELAY */
1709c80476e4SDavid E. O'Brien #  ifndef _FNDELAY
1710c80476e4SDavid E. O'Brien #   define _FNDELAY 0
1711c80476e4SDavid E. O'Brien #  endif /* _FNDELAY */
1712c80476e4SDavid E. O'Brien #  ifndef FNDLEAY	/* Some linux versions have this typo */
1713c80476e4SDavid E. O'Brien #   define FNDLEAY 0
1714c80476e4SDavid E. O'Brien #  endif /* FNDLEAY */
1715c80476e4SDavid E. O'Brien 	if ((e = fcntl(fd, F_GETFL, 0)) == -1)
1716c80476e4SDavid E. O'Brien 	    return -1;
1717c80476e4SDavid E. O'Brien 
1718c80476e4SDavid E. O'Brien 	e &= ~(O_NDELAY|O_NONBLOCK|FNBIO|_FNBIO|FNONBIO|FNONBLOCK|_FNONBLOCK|
1719c80476e4SDavid E. O'Brien 	       FNDELAY|_FNDELAY|FNDLEAY);	/* whew! */
1720c80476e4SDavid E. O'Brien 	if (fcntl(fd, F_SETFL, e) == -1)
1721c80476e4SDavid E. O'Brien 	    return -1;
1722c80476e4SDavid E. O'Brien 	else
17239ccc37e3SMark Peek 	    e = 0;
1724c80476e4SDavid E. O'Brien # endif /* F_SETFL */
1725c80476e4SDavid E. O'Brien 
1726c80476e4SDavid E. O'Brien # ifdef FIONBIO
1727c80476e4SDavid E. O'Brien 	e = 0;
1728c80476e4SDavid E. O'Brien 	if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1)
1729c80476e4SDavid E. O'Brien 	    return -1;
1730c80476e4SDavid E. O'Brien # endif	/* FIONBIO */
1731c80476e4SDavid E. O'Brien 
1732c80476e4SDavid E. O'Brien #endif /* FDRETRY */
17339ccc37e3SMark Peek 	return e;
1734c80476e4SDavid E. O'Brien 
1735c80476e4SDavid E. O'Brien     case EINTR:
1736c80476e4SDavid E. O'Brien 	return 0;
1737c80476e4SDavid E. O'Brien 
1738c80476e4SDavid E. O'Brien     default:
1739c80476e4SDavid E. O'Brien 	return -1;
1740c80476e4SDavid E. O'Brien     }
1741c80476e4SDavid E. O'Brien }
1742c80476e4SDavid E. O'Brien 
1743c80476e4SDavid E. O'Brien /* collate():
1744c80476e4SDavid E. O'Brien  *	String collation
1745c80476e4SDavid E. O'Brien  */
1746c80476e4SDavid E. O'Brien int
collate(const Char * a,const Char * b)174745e5710bSMark Peek collate(const Char *a, const Char *b)
1748c80476e4SDavid E. O'Brien {
1749c80476e4SDavid E. O'Brien     int rv;
1750c80476e4SDavid E. O'Brien #ifdef SHORT_STRINGS
1751c80476e4SDavid E. O'Brien     /* This strips the quote bit as a side effect */
1752c80476e4SDavid E. O'Brien     char *sa = strsave(short2str(a));
1753c80476e4SDavid E. O'Brien     char *sb = strsave(short2str(b));
1754c80476e4SDavid E. O'Brien #else
1755c80476e4SDavid E. O'Brien     char *sa = strip(strsave(a));
1756c80476e4SDavid E. O'Brien     char *sb = strip(strsave(b));
1757c80476e4SDavid E. O'Brien #endif /* SHORT_STRINGS */
1758c80476e4SDavid E. O'Brien 
175945e5710bSMark Peek #if defined(NLS) && defined(HAVE_STRCOLL)
1760c80476e4SDavid E. O'Brien     errno = 0;	/* strcoll sets errno, another brain-damage */
1761c80476e4SDavid E. O'Brien 
1762c80476e4SDavid E. O'Brien     rv = strcoll(sa, sb);
1763c80476e4SDavid E. O'Brien 
1764c80476e4SDavid E. O'Brien     /*
1765c80476e4SDavid E. O'Brien      * We should be checking for errno != 0, but some systems
1766c80476e4SDavid E. O'Brien      * forget to reset errno to 0. So we only check for the
1767c80476e4SDavid E. O'Brien      * only documented valid errno value for strcoll [EINVAL]
1768c80476e4SDavid E. O'Brien      */
1769c80476e4SDavid E. O'Brien     if (errno == EINVAL) {
177045e5710bSMark Peek 	xfree(sa);
177145e5710bSMark Peek 	xfree(sb);
1772c80476e4SDavid E. O'Brien 	stderror(ERR_SYSTEM, "strcoll", strerror(errno));
1773c80476e4SDavid E. O'Brien     }
1774c80476e4SDavid E. O'Brien #else
1775c80476e4SDavid E. O'Brien     rv = strcmp(sa, sb);
177645e5710bSMark Peek #endif /* NLS && HAVE_STRCOLL */
1777c80476e4SDavid E. O'Brien 
177845e5710bSMark Peek     xfree(sa);
177945e5710bSMark Peek     xfree(sb);
1780c80476e4SDavid E. O'Brien 
1781c80476e4SDavid E. O'Brien     return rv;
1782c80476e4SDavid E. O'Brien }
1783c80476e4SDavid E. O'Brien 
1784c80476e4SDavid E. O'Brien #ifdef HASHBANG
1785c80476e4SDavid E. O'Brien /*
1786c80476e4SDavid E. O'Brien  * From: peter@zeus.dialix.oz.au (Peter Wemm)
1787c80476e4SDavid E. O'Brien  * If exec() fails look first for a #! [word] [word] ....
1788c80476e4SDavid E. O'Brien  * If it is, splice the header into the argument list and retry.
1789c80476e4SDavid E. O'Brien  */
1790c80476e4SDavid E. O'Brien #define HACKBUFSZ 1024		/* Max chars in #! vector */
1791c80476e4SDavid E. O'Brien int
hashbang(int fd,Char *** vp)179245e5710bSMark Peek hashbang(int fd, Char ***vp)
1793c80476e4SDavid E. O'Brien {
179445e5710bSMark Peek     struct blk_buf sarg = BLK_BUF_INIT;
179545e5710bSMark Peek     char lbuf[HACKBUFSZ], *p, *ws;
17963b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
1797c80476e4SDavid E. O'Brien     int fw = 0; 	/* found at least one word */
179845e5710bSMark Peek     int first_word = 1;
179945e5710bSMark Peek     char *real;
18003b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
1801c80476e4SDavid E. O'Brien 
180245e5710bSMark Peek     if (xread(fd, lbuf, HACKBUFSZ) <= 0)
1803c80476e4SDavid E. O'Brien 	return -1;
1804c80476e4SDavid E. O'Brien 
1805c80476e4SDavid E. O'Brien     ws = 0;	/* word started = 0 */
1806c80476e4SDavid E. O'Brien 
180745e5710bSMark Peek     for (p = lbuf; p < &lbuf[HACKBUFSZ]; ) {
1808c80476e4SDavid E. O'Brien 	switch (*p) {
1809c80476e4SDavid E. O'Brien 	case ' ':
1810c80476e4SDavid E. O'Brien 	case '\t':
1811a15e6f9aSMark Peek #if defined(WINNT_NATIVE) || defined (__CYGWIN__)
1812c80476e4SDavid E. O'Brien 	case '\r':
1813a15e6f9aSMark Peek #endif /* WINNT_NATIVE || __CYGWIN__ */
1814c80476e4SDavid E. O'Brien 	    if (ws) {	/* a blank after a word.. save it */
1815c80476e4SDavid E. O'Brien 		*p = '\0';
181645e5710bSMark Peek #ifdef WINNT_NATIVE
181745e5710bSMark Peek 		if (first_word) {
181845e5710bSMark Peek 		    real = hb_subst(ws);
181945e5710bSMark Peek 		    if (real != NULL)
182045e5710bSMark Peek 			ws = real;
1821c80476e4SDavid E. O'Brien 		}
1822c80476e4SDavid E. O'Brien 	    	fw = 1;
182345e5710bSMark Peek 		first_word = 0;
18243b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
182545e5710bSMark Peek 		bb_append(&sarg, SAVE(ws));
182645e5710bSMark Peek 		ws = NULL;
1827c80476e4SDavid E. O'Brien 	    }
1828c80476e4SDavid E. O'Brien 	    p++;
1829c80476e4SDavid E. O'Brien 	    continue;
1830c80476e4SDavid E. O'Brien 
1831c80476e4SDavid E. O'Brien 	case '\0':	/* Whoa!! what the hell happened */
183245e5710bSMark Peek 	    goto err;
1833c80476e4SDavid E. O'Brien 
1834c80476e4SDavid E. O'Brien 	case '\n':	/* The end of the line. */
1835c80476e4SDavid E. O'Brien 	    if (
18363b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
1837c80476e4SDavid E. O'Brien 		fw ||
18383b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
1839c80476e4SDavid E. O'Brien 		ws) {	/* terminate the last word */
1840c80476e4SDavid E. O'Brien 		*p = '\0';
184145e5710bSMark Peek #ifdef WINNT_NATIVE
184245e5710bSMark Peek 		/* deal with the 1-word case */
184345e5710bSMark Peek 		if (first_word) {
184445e5710bSMark Peek 		    real = hb_subst(ws);
184545e5710bSMark Peek 		    if (real != NULL)
184645e5710bSMark Peek 			ws = real;
1847c80476e4SDavid E. O'Brien 		}
18483b6eaa7bSAndrey A. Chernov #endif /* !WINNT_NATIVE */
184945e5710bSMark Peek 		if (ws)
185045e5710bSMark Peek 		    bb_append(&sarg, SAVE(ws));
1851c80476e4SDavid E. O'Brien 	    }
185245e5710bSMark Peek 	    if (sarg.len > 0) {
185345e5710bSMark Peek 		*vp = bb_finish(&sarg);
1854c80476e4SDavid E. O'Brien 		return 0;
1855c80476e4SDavid E. O'Brien 	    }
1856c80476e4SDavid E. O'Brien 	    else
185745e5710bSMark Peek 		goto err;
1858c80476e4SDavid E. O'Brien 
1859c80476e4SDavid E. O'Brien 	default:
1860c80476e4SDavid E. O'Brien 	    if (!ws)	/* Start a new word? */
1861c80476e4SDavid E. O'Brien 		ws = p;
1862c80476e4SDavid E. O'Brien 	    p++;
1863c80476e4SDavid E. O'Brien 	    break;
1864c80476e4SDavid E. O'Brien 	}
186545e5710bSMark Peek     }
186645e5710bSMark Peek  err:
186745e5710bSMark Peek     bb_cleanup(&sarg);
1868c80476e4SDavid E. O'Brien     return -1;
1869c80476e4SDavid E. O'Brien }
1870c80476e4SDavid E. O'Brien #endif /* HASHBANG */
1871c80476e4SDavid E. O'Brien 
1872c80476e4SDavid E. O'Brien #ifdef REMOTEHOST
1873c80476e4SDavid E. O'Brien 
187445e5710bSMark Peek static void
palarm(int snum)187545e5710bSMark Peek palarm(int snum)
1876c80476e4SDavid E. O'Brien {
1877c80476e4SDavid E. O'Brien     USE(snum);
187845e5710bSMark Peek     _exit(1);
1879c80476e4SDavid E. O'Brien }
1880c80476e4SDavid E. O'Brien 
1881c80476e4SDavid E. O'Brien static void
getremotehost(int dest_fd)188245e5710bSMark Peek getremotehost(int dest_fd)
1883c80476e4SDavid E. O'Brien {
1884c80476e4SDavid E. O'Brien     const char *host = NULL;
18853b6eaa7bSAndrey A. Chernov #ifdef INET6
18863b6eaa7bSAndrey A. Chernov     struct sockaddr_storage saddr;
18873b6eaa7bSAndrey A. Chernov     static char hbuf[NI_MAXHOST];
18883b6eaa7bSAndrey A. Chernov #else
1889c80476e4SDavid E. O'Brien     struct hostent* hp;
1890c80476e4SDavid E. O'Brien     struct sockaddr_in saddr;
18913b6eaa7bSAndrey A. Chernov #endif
189245e5710bSMark Peek     socklen_t len = sizeof(saddr);
1893c80476e4SDavid E. O'Brien 
18943b6eaa7bSAndrey A. Chernov #ifdef INET6
189529301572SMark Peek     if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
189629301572SMark Peek 	(saddr.ss_family == AF_INET6 || saddr.ss_family == AF_INET)) {
18973b6eaa7bSAndrey A. Chernov 	int flag = NI_NUMERICHOST;
18983b6eaa7bSAndrey A. Chernov 
18993b6eaa7bSAndrey A. Chernov #ifdef NI_WITHSCOPEID
19003b6eaa7bSAndrey A. Chernov 	flag |= NI_WITHSCOPEID;
19013b6eaa7bSAndrey A. Chernov #endif
19023b6eaa7bSAndrey A. Chernov 	getnameinfo((struct sockaddr *)&saddr, len, hbuf, sizeof(hbuf),
19033b6eaa7bSAndrey A. Chernov 		    NULL, 0, flag);
19043b6eaa7bSAndrey A. Chernov 	host = hbuf;
19053b6eaa7bSAndrey A. Chernov #else
190629301572SMark Peek     if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
190729301572SMark Peek 	saddr.sin_family == AF_INET) {
1908c80476e4SDavid E. O'Brien #if 0
1909c80476e4SDavid E. O'Brien 	if ((hp = gethostbyaddr((char *)&saddr.sin_addr, sizeof(struct in_addr),
1910c80476e4SDavid E. O'Brien 				AF_INET)) != NULL)
1911c80476e4SDavid E. O'Brien 	    host = hp->h_name;
1912c80476e4SDavid E. O'Brien 	else
1913c80476e4SDavid E. O'Brien #endif
1914c80476e4SDavid E. O'Brien 	    host = inet_ntoa(saddr.sin_addr);
19153b6eaa7bSAndrey A. Chernov #endif
1916c80476e4SDavid E. O'Brien     }
191723338178SMark Peek #ifdef HAVE_STRUCT_UTMP_UT_HOST
1918c80476e4SDavid E. O'Brien     else {
1919c80476e4SDavid E. O'Brien 	char *ptr;
1920c80476e4SDavid E. O'Brien 	char *name = utmphost();
1921c80476e4SDavid E. O'Brien 	/* Avoid empty names and local X displays */
1922c80476e4SDavid E. O'Brien 	if (name != NULL && *name != '\0' && *name != ':') {
19233b6eaa7bSAndrey A. Chernov 	    struct in_addr addr;
192445e5710bSMark Peek 	    char *sptr;
19253b6eaa7bSAndrey A. Chernov 
1926c80476e4SDavid E. O'Brien 	    /* Look for host:display.screen */
19273b6eaa7bSAndrey A. Chernov 	    /*
19283b6eaa7bSAndrey A. Chernov 	     * There is conflict with IPv6 address and X DISPLAY.  So,
19293b6eaa7bSAndrey A. Chernov 	     * we assume there is no IPv6 address in utmp and don't
19303b6eaa7bSAndrey A. Chernov 	     * touch here.
19313b6eaa7bSAndrey A. Chernov 	     */
1932c80476e4SDavid E. O'Brien 	    if ((sptr = strchr(name, ':')) != NULL)
1933c80476e4SDavid E. O'Brien 		*sptr = '\0';
19343b6eaa7bSAndrey A. Chernov 	    /* Leave IPv4 address as is */
19353b6eaa7bSAndrey A. Chernov 	    /*
19363b6eaa7bSAndrey A. Chernov 	     * we use inet_addr here, not inet_aton because many systems
19373b6eaa7bSAndrey A. Chernov 	     * have not caught up yet.
19383b6eaa7bSAndrey A. Chernov 	     */
19393b6eaa7bSAndrey A. Chernov 	    addr.s_addr = inet_addr(name);
1940b2d5d167SMark Peek 	    if (addr.s_addr != (unsigned int)~0)
1941c80476e4SDavid E. O'Brien 		host = name;
1942c80476e4SDavid E. O'Brien 	    else {
1943c80476e4SDavid E. O'Brien 		if (sptr != name) {
19443b6eaa7bSAndrey A. Chernov #ifdef INET6
19453b6eaa7bSAndrey A. Chernov 		    char *s, *domain;
194645e5710bSMark Peek 		    char dbuf[MAXHOSTNAMELEN];
19473b6eaa7bSAndrey A. Chernov 		    struct addrinfo hints, *res = NULL;
19483b6eaa7bSAndrey A. Chernov 
19493b6eaa7bSAndrey A. Chernov 		    memset(&hints, 0, sizeof(hints));
19503b6eaa7bSAndrey A. Chernov 		    hints.ai_family = PF_UNSPEC;
19513b6eaa7bSAndrey A. Chernov 		    hints.ai_socktype = SOCK_STREAM;
19523b6eaa7bSAndrey A. Chernov 		    hints.ai_flags = AI_PASSIVE | AI_CANONNAME;
19533b6eaa7bSAndrey A. Chernov 		    if (strlen(name) < utmphostsize())
19543b6eaa7bSAndrey A. Chernov 		    {
19553b6eaa7bSAndrey A. Chernov 			if (getaddrinfo(name, NULL, &hints, &res) != 0)
19563b6eaa7bSAndrey A. Chernov 			    res = NULL;
195745e5710bSMark Peek 		    } else if (gethostname(dbuf, sizeof(dbuf)) == 0 &&
195845e5710bSMark Peek 			       (dbuf[sizeof(dbuf)-1] = '\0', /*FIXME: ugly*/
195945e5710bSMark Peek 				(domain = strchr(dbuf, '.')) != NULL)) {
19603b6eaa7bSAndrey A. Chernov 			for (s = strchr(name, '.');
19613b6eaa7bSAndrey A. Chernov 			     s != NULL; s = strchr(s + 1, '.')) {
19623b6eaa7bSAndrey A. Chernov 			    if (*(s + 1) != '\0' &&
19633b6eaa7bSAndrey A. Chernov 				(ptr = strstr(domain, s)) != NULL) {
196445e5710bSMark Peek 			        char *cbuf;
196545e5710bSMark Peek 
19669ccc37e3SMark Peek 				cbuf = strspl(name, ptr + strlen(s));
19673b6eaa7bSAndrey A. Chernov 				if (getaddrinfo(cbuf, NULL, &hints, &res) != 0)
19683b6eaa7bSAndrey A. Chernov 				    res = NULL;
196945e5710bSMark Peek 				xfree(cbuf);
19703b6eaa7bSAndrey A. Chernov 				break;
19713b6eaa7bSAndrey A. Chernov 			    }
19723b6eaa7bSAndrey A. Chernov 			}
19733b6eaa7bSAndrey A. Chernov 		    }
19743b6eaa7bSAndrey A. Chernov 		    if (res != NULL) {
19753b6eaa7bSAndrey A. Chernov 			if (res->ai_canonname != NULL) {
19763b6eaa7bSAndrey A. Chernov 			    strncpy(hbuf, res->ai_canonname, sizeof(hbuf));
19779ccc37e3SMark Peek 			    hbuf[sizeof(hbuf) - 1] = '\0';
19783b6eaa7bSAndrey A. Chernov 			    host = hbuf;
19793b6eaa7bSAndrey A. Chernov 			}
19803b6eaa7bSAndrey A. Chernov 			freeaddrinfo(res);
19813b6eaa7bSAndrey A. Chernov 		    }
19823b6eaa7bSAndrey A. Chernov #else
1983c80476e4SDavid E. O'Brien 		    if ((hp = gethostbyname(name)) == NULL) {
1984c80476e4SDavid E. O'Brien 			/* Try again eliminating the trailing domain */
1985c80476e4SDavid E. O'Brien 			if ((ptr = strchr(name, '.')) != NULL) {
1986c80476e4SDavid E. O'Brien 			    *ptr = '\0';
1987c80476e4SDavid E. O'Brien 			    if ((hp = gethostbyname(name)) != NULL)
1988c80476e4SDavid E. O'Brien 				host = hp->h_name;
1989c80476e4SDavid E. O'Brien 			    *ptr = '.';
1990c80476e4SDavid E. O'Brien 			}
1991c80476e4SDavid E. O'Brien 		    }
1992c80476e4SDavid E. O'Brien 		    else
1993c80476e4SDavid E. O'Brien 			host = hp->h_name;
19943b6eaa7bSAndrey A. Chernov #endif
1995c80476e4SDavid E. O'Brien 		}
1996c80476e4SDavid E. O'Brien 	    }
1997c80476e4SDavid E. O'Brien 	}
1998c80476e4SDavid E. O'Brien     }
1999c80476e4SDavid E. O'Brien #endif
2000c80476e4SDavid E. O'Brien 
200145e5710bSMark Peek     if (host) {
200245e5710bSMark Peek 	size_t left;
2003c80476e4SDavid E. O'Brien 
200445e5710bSMark Peek 	left = strlen(host);
200545e5710bSMark Peek 	while (left != 0) {
200645e5710bSMark Peek 	    ssize_t res;
200745e5710bSMark Peek 
200845e5710bSMark Peek 	    res = xwrite(dest_fd, host, left);
200945e5710bSMark Peek 	    if (res < 0)
201045e5710bSMark Peek 		_exit(1);
201145e5710bSMark Peek 	    host += res;
201245e5710bSMark Peek 	    left -= res;
2013c80476e4SDavid E. O'Brien 	}
201445e5710bSMark Peek     }
201545e5710bSMark Peek     _exit(0);
201645e5710bSMark Peek }
2017c80476e4SDavid E. O'Brien 
2018c80476e4SDavid E. O'Brien /*
2019c80476e4SDavid E. O'Brien  * From: <lesv@ppvku.ericsson.se> (Lennart Svensson)
2020c80476e4SDavid E. O'Brien  */
2021c80476e4SDavid E. O'Brien void
202245e5710bSMark Peek remotehost(void)
2023c80476e4SDavid E. O'Brien {
202445e5710bSMark Peek     struct sigaction sa;
202545e5710bSMark Peek     struct strbuf hostname = strbuf_INIT;
202645e5710bSMark Peek     int fds[2], wait_options, status;
202745e5710bSMark Peek     pid_t pid, wait_res;
202845e5710bSMark Peek 
202945e5710bSMark Peek     sa.sa_handler = SIG_DFL; /* Make sure a zombie is created */
203045e5710bSMark Peek     sigemptyset(&sa.sa_mask);
203145e5710bSMark Peek     sa.sa_flags = 0;
203245e5710bSMark Peek     sigaction(SIGCHLD, &sa, NULL);
203345e5710bSMark Peek     mypipe(fds);
203445e5710bSMark Peek     pid = fork();
203545e5710bSMark Peek     if (pid == 0) {
203645e5710bSMark Peek 	sigset_t set;
203745e5710bSMark Peek 	xclose(fds[0]);
2038c80476e4SDavid E. O'Brien 	/* Don't get stuck if the resolver does not work! */
203945e5710bSMark Peek 	signal(SIGALRM, palarm);
204045e5710bSMark Peek 	sigemptyset(&set);
204145e5710bSMark Peek 	sigaddset(&set, SIGALRM);
204245e5710bSMark Peek 	(void)sigprocmask(SIG_UNBLOCK, &set, NULL);
2043c80476e4SDavid E. O'Brien 	(void)alarm(2);
204445e5710bSMark Peek 	getremotehost(fds[1]);
204545e5710bSMark Peek 	/*NOTREACHED*/
204645e5710bSMark Peek     }
204745e5710bSMark Peek     xclose(fds[1]);
204845e5710bSMark Peek     for (;;) {
204945e5710bSMark Peek 	char buf[BUFSIZE];
205045e5710bSMark Peek 	ssize_t res;
2051c80476e4SDavid E. O'Brien 
205245e5710bSMark Peek 	res = xread(fds[0], buf, sizeof(buf));
205345e5710bSMark Peek 	if (res == -1) {
205445e5710bSMark Peek 	    hostname.len = 0;
205545e5710bSMark Peek 	    wait_options = WNOHANG;
205645e5710bSMark Peek 	    goto done;
205745e5710bSMark Peek 	}
205845e5710bSMark Peek 	if (res == 0)
205945e5710bSMark Peek 	    break;
206045e5710bSMark Peek 	strbuf_appendn(&hostname, buf, res);
206145e5710bSMark Peek     }
206245e5710bSMark Peek     wait_options = 0;
206345e5710bSMark Peek  done:
20649ccc37e3SMark Peek     cleanup_push(&hostname, strbuf_cleanup);
206545e5710bSMark Peek     xclose(fds[0]);
206645e5710bSMark Peek     while ((wait_res = waitpid(pid, &status, wait_options)) == -1
206745e5710bSMark Peek 	   && errno == EINTR)
206845e5710bSMark Peek 	handle_pending_signals();
20699ccc37e3SMark Peek     if (hostname.len > 0 && wait_res == pid && WIFEXITED(status)
20709ccc37e3SMark Peek 	   && WEXITSTATUS(status) == 0) {
207145e5710bSMark Peek 	strbuf_terminate(&hostname);
207245e5710bSMark Peek 	tsetenv(STRREMOTEHOST, str2short(hostname.s));
207345e5710bSMark Peek     }
207445e5710bSMark Peek     cleanup_until(&hostname);
2075c80476e4SDavid E. O'Brien 
2076c80476e4SDavid E. O'Brien #ifdef YPBUGS
2077c80476e4SDavid E. O'Brien     /* From: casper@fwi.uva.nl (Casper H.S. Dik), for Solaris 2.3 */
2078c80476e4SDavid E. O'Brien     fix_yp_bugs();
2079c80476e4SDavid E. O'Brien #endif /* YPBUGS */
2080c80476e4SDavid E. O'Brien 
2081c80476e4SDavid E. O'Brien }
2082c80476e4SDavid E. O'Brien #endif /* REMOTEHOST */
208323338178SMark Peek 
208423338178SMark Peek #ifndef WINNT_NATIVE
208523338178SMark Peek /*
208623338178SMark Peek  * indicate if a terminal type is defined in terminfo/termcap
208723338178SMark Peek  * (by default the current term type). This allows ppl to look
208823338178SMark Peek  * for a working term type automatically in their login scripts
208923338178SMark Peek  * when using a terminal known as different things on different
209023338178SMark Peek  * platforms
209123338178SMark Peek  */
209223338178SMark Peek void
209345e5710bSMark Peek dotermname(Char **v, struct command *c)
209423338178SMark Peek {
209523338178SMark Peek     char *termtype;
209623338178SMark Peek     /*
209723338178SMark Peek      * Maximum size of a termcap record. We make it twice as large.
209823338178SMark Peek      */
209923338178SMark Peek     char termcap_buffer[2048];
210023338178SMark Peek 
210123338178SMark Peek     USE(c);
210223338178SMark Peek     /* try to find which entry we should be looking for */
210323338178SMark Peek     termtype = (v[1] == NULL ? getenv("TERM") : short2str(v[1]));
210423338178SMark Peek     if (termtype == NULL) {
210523338178SMark Peek 	/* no luck - the user didn't provide one and none is
210623338178SMark Peek 	 * specified in the environment
210723338178SMark Peek 	 */
210845e5710bSMark Peek 	setcopy(STRstatus, STR1, VAR_READWRITE);
210923338178SMark Peek 	return;
211023338178SMark Peek     }
211123338178SMark Peek 
211223338178SMark Peek     /*
211323338178SMark Peek      * we use the termcap function - if we are using terminfo we
211423338178SMark Peek      * will end up with it's compatibility function
211523338178SMark Peek      * terminfo/termcap will be initialized with the new
211623338178SMark Peek      * type but we don't care because tcsh has cached all the things
211723338178SMark Peek      * it needs.
211823338178SMark Peek      */
211923338178SMark Peek     if (tgetent(termcap_buffer, termtype) == 1) {
212023338178SMark Peek 	xprintf("%s\n", termtype);
212145e5710bSMark Peek 	setcopy(STRstatus, STR0, VAR_READWRITE);
212245e5710bSMark Peek     } else
212345e5710bSMark Peek 	setcopy(STRstatus, STR1, VAR_READWRITE);
212423338178SMark Peek }
212523338178SMark Peek #endif /* WINNT_NATIVE */
2126