summaryrefslogtreecommitdiffstats
path: root/contrib/tcsh/tc.func.c
diff options
context:
space:
mode:
authormp <mp@FreeBSD.org>2007-03-11 22:33:41 +0000
committermp <mp@FreeBSD.org>2007-03-11 22:33:41 +0000
commita40980339b13e3b506c2317b5b4864127039eb2c (patch)
tree34aefea92d30b614247ef1f2671f2362f4761785 /contrib/tcsh/tc.func.c
parent32837fb336d4709f0a121130a3a78f29be0db5ed (diff)
downloadFreeBSD-src-a40980339b13e3b506c2317b5b4864127039eb2c.zip
FreeBSD-src-a40980339b13e3b506c2317b5b4864127039eb2c.tar.gz
Import of tcsh-6.15.00
Diffstat (limited to 'contrib/tcsh/tc.func.c')
-rw-r--r--contrib/tcsh/tc.func.c1055
1 files changed, 414 insertions, 641 deletions
diff --git a/contrib/tcsh/tc.func.c b/contrib/tcsh/tc.func.c
index 4f19102..e2db430 100644
--- a/contrib/tcsh/tc.func.c
+++ b/contrib/tcsh/tc.func.c
@@ -1,4 +1,4 @@
-/* $Header: /src/pub/tcsh/tc.func.c,v 3.119 2005/03/06 03:57:10 christos Exp $ */
+/* $Header: /p/tcsh/cvsroot/tcsh/tc.func.c,v 3.136 2006/09/01 12:51:35 christos Exp $ */
/*
* tc.func.c: New tcsh builtins.
*/
@@ -32,7 +32,7 @@
*/
#include "sh.h"
-RCSID("$Id: tc.func.c,v 3.119 2005/03/06 03:57:10 christos Exp $")
+RCSID("$tcsh: tc.func.c,v 3.136 2006/09/01 12:51:35 christos Exp $")
#include "ed.h"
#include "ed.defns.h" /* for the function names */
@@ -40,17 +40,14 @@ RCSID("$Id: tc.func.c,v 3.119 2005/03/06 03:57:10 christos Exp $")
#include "tc.h"
#ifdef WINNT_NATIVE
#include "nt.const.h"
+#else /* WINNT_NATIVE */
+#include <sys/wait.h>
#endif /* WINNT_NATIVE */
#ifdef AFS
-#define PASSMAX 16
#include <afs/stds.h>
#include <afs/kautils.h>
long ka_UserAuthenticateGeneral();
-#else
-#ifndef PASSMAX
-#define PASSMAX 8
-#endif
#endif /* AFS */
#ifdef TESLA
@@ -64,22 +61,21 @@ static int postcmd_active = 0;
static int periodic_active = 0;
static int cwdcmd_active = 0; /* PWP: for cwd_cmd */
static int beepcmd_active = 0;
-static signalfun_t alm_fun = NULL;
+static void (*alm_fun)(void) = NULL;
-static void auto_logout __P((int));
-static char *xgetpass __P((const char *));
-static void auto_lock __P((int));
+static void auto_logout (void);
+static char *xgetpass (const char *);
+static void auto_lock (void);
#ifdef BSDJOBS
-static void insert __P((struct wordent *, int));
-static void insert_we __P((struct wordent *, struct wordent *));
-static int inlist __P((Char *, Char *));
+static void insert (struct wordent *, int);
+static void insert_we (struct wordent *, struct wordent *);
+static int inlist (Char *, Char *);
#endif /* BSDJOBS */
-struct tildecache;
-static int tildecompare __P((struct tildecache *, struct tildecache *));
-static Char *gethomedir __P((Char *));
+static int tildecompare (const void *, const void *);
+static Char *gethomedir (const Char *);
#ifdef REMOTEHOST
-static RETSIGTYPE palarm __P((int));
-static void getremotehost __P((void));
+static void palarm (int);
+static void getremotehost (int);
#endif /* REMOTEHOST */
/*
@@ -87,69 +83,35 @@ static void getremotehost __P((void));
*/
/*
- * expand_lex: Take the given lex and put an expanded version of it in
- * the string buf. First guy in lex list is ignored; last guy is ^J
- * which we ignore. Only take lex'es from position 'from' to position
- * 'to' inclusive
+ * expand_lex: Take the given lex and return an expanded version of it.
+ * First guy in lex list is ignored; last guy is ^J which we ignore.
+ * Only take lex'es from position 'from' to position 'to' inclusive
*
* Note: csh sometimes sets bit 8 in characters which causes all kinds
* of problems if we don't mask it here. Note: excl's in lexes have been
* un-back-slashed and must be re-back-slashed
*
- * (PWP: NOTE: this returns a pointer to the END of the string expanded
- * (in other words, where the NUL is).)
*/
/* PWP: this is a combination of the old sprlex() and the expand_lex from
the magic-space stuff */
Char *
-expand_lex(buf, bufsiz, sp0, from, to)
- Char *buf;
- size_t bufsiz;
- struct wordent *sp0;
- int from, to;
+expand_lex(const struct wordent *sp0, int from, int to)
{
- struct wordent *sp;
- Char *s, *d, *e;
+ struct Strbuf buf = Strbuf_INIT;
+ const struct wordent *sp;
+ Char *s;
Char prev_c;
int i;
- /*
- * Make sure we have enough space to expand into. E.g. we may have
- * "a|b" turn to "a | b" (from 3 to 5 characters) which is the worst
- * case scenario (even "a>&! b" turns into "a > & ! b", i.e. 6 to 9
- * characters -- am I missing any other cases?).
- */
- bufsiz = bufsiz / 2;
-
- buf[0] = '\0';
prev_c = '\0';
- d = buf;
- e = &buf[bufsiz]; /* for bounds checking */
- if (!sp0)
- return (buf); /* null lex */
- if ((sp = sp0->next) == sp0)
- return (buf); /* nada */
- if (sp == (sp0 = sp0->prev))
- return (buf); /* nada */
+ if (!sp0 || (sp = sp0->next) == sp0 || sp == (sp0 = sp0->prev))
+ return Strbuf_finish(&buf); /* null lex */
- for (i = 0; i < NCARGS; i++) {
+ for (i = 0; ; i++) {
if ((i >= from) && (i <= to)) { /* if in range */
- for (s = sp->word; *s && d < e; s++) {
-
- if (s[1] & QUOTE) {
- int l = NLSSize(s, -1);
- if (l > 1) {
- while (l-- > 0) {
- if (d < e)
- *d++ = (*s & TRIM);
- prev_c = *s++;
- }
- s--;
- continue;
- }
- }
+ for (s = sp->word; *s; s++) {
/*
* bugfix by Michael Bloom: anything but the current history
* character {(PWP) and backslash} seem to be dealt with
@@ -160,44 +122,32 @@ expand_lex(buf, bufsiz, sp0, from, to)
(((*s & TRIM) == '\'') && (prev_c != '\\')) ||
(((*s & TRIM) == '\"') && (prev_c != '\\')) ||
(((*s & TRIM) == '\\') && (prev_c != '\\')))) {
- *d++ = '\\';
+ Strbuf_append1(&buf, '\\');
}
- if (d < e)
- *d++ = (*s & TRIM);
+ Strbuf_append1(&buf, *s & TRIM);
prev_c = *s;
}
- if (d < e)
- *d++ = ' ';
+ Strbuf_append1(&buf, ' ');
}
sp = sp->next;
if (sp == sp0)
break;
}
- if (d > buf)
- d--; /* get rid of trailing space */
+ if (buf.len != 0)
+ buf.len--; /* get rid of trailing space */
- return (d);
+ return Strbuf_finish(&buf);
}
Char *
-sprlex(buf, bufsiz, sp0)
- Char *buf;
- size_t bufsiz;
- struct wordent *sp0;
+sprlex(const struct wordent *sp0)
{
- Char *cp;
-
- cp = expand_lex(buf, bufsiz, sp0, 0, NCARGS);
- *cp = '\0';
- return (buf);
+ return expand_lex(sp0, 0, INT_MAX);
}
Char *
-Itoa(n, s, min_digits, attributes)
- int n;
- Char *s;
- int min_digits, attributes;
+Itoa(int n, size_t min_digits, Char attributes)
{
/*
* The array size here is derived from
@@ -209,19 +159,16 @@ Itoa(n, s, min_digits, attributes)
#ifndef CHAR_BIT
# define CHAR_BIT 8
#endif
- Char buf[CHAR_BIT * sizeof(int) / 3 + 1];
- Char *p;
+ Char buf[CHAR_BIT * sizeof(int) / 3 + 1], *res, *p, *s;
unsigned int un; /* handle most negative # too */
int pad = (min_digits != 0);
- if ((int)(sizeof(buf) - 1) < min_digits)
+ if (sizeof(buf) - 1 < min_digits)
min_digits = sizeof(buf) - 1;
un = n;
- if (n < 0) {
+ if (n < 0)
un = -n;
- *s++ = '-';
- }
p = buf;
do {
@@ -229,38 +176,39 @@ Itoa(n, s, min_digits, attributes)
un /= 10;
} while ((pad && --min_digits > 0) || un != 0);
+ res = xmalloc((p - buf + 2) * sizeof(*res));
+ s = res;
+ if (n < 0)
+ *s++ = '-';
while (p > buf)
*s++ = *--p | attributes;
*s = '\0';
- return s;
+ return res;
}
/*ARGSUSED*/
void
-dolist(v, c)
- Char **v;
- struct command *c;
+dolist(Char **v, struct command *c)
{
+ Char **globbed;
int i, k;
struct stat st;
USE(c);
if (*++v == NULL) {
- (void) t_search(STRNULL, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0);
+ struct Strbuf word = Strbuf_INIT;
+
+ Strbuf_terminate(&word);
+ cleanup_push(&word, Strbuf_cleanup);
+ (void) t_search(&word, LIST, TW_ZERO, 0, STRNULL, 0);
+ cleanup_until(&word);
return;
}
- gflag = 0;
- tglob(v);
- if (gflag) {
- v = globall(v);
- if (v == 0)
- stderror(ERR_NAME | ERR_NOMATCH);
- }
- else
- v = gargv = saveblk(v);
- trim(v);
+ v = glob_all_or_error(v);
+ globbed = v;
+ cleanup_push(globbed, blk_cleanup);
for (k = 0; v[k] != NULL && v[k][0] != '-'; k++)
continue;
if (v[k]) {
@@ -273,16 +221,12 @@ dolist(v, c)
Char *cp;
struct varent *vp;
-#ifdef BSDSIGS
- sigmask_t omask = 0;
-
- if (setintr)
- omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
-#else /* !BSDSIGS */
- (void) sighold(SIGINT);
-#endif /* BSDSIGS */
+ if (setintr) {
+ pintr_disabled++;
+ cleanup_push(&pintr_disabled, disabled_cleanup);
+ }
if (seterr) {
- xfree((ptr_t) seterr);
+ xfree(seterr);
seterr = NULL;
}
@@ -313,19 +257,19 @@ dolist(v, c)
cmd.word = STRNULL;
lastword = &cmd;
- nextword = (struct wordent *) xcalloc(1, sizeof cmd);
+ nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(lspath);
lastword->next = nextword;
nextword->prev = lastword;
lastword = nextword;
- nextword = (struct wordent *) xcalloc(1, sizeof cmd);
+ nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(STRmCF);
lastword->next = nextword;
nextword->prev = lastword;
#if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
if (dspmbyte_ls) {
lastword = nextword;
- nextword = (struct wordent *) xcalloc(1, sizeof cmd);
+ nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(STRmmliteral);
lastword->next = nextword;
nextword->prev = lastword;
@@ -334,7 +278,7 @@ dolist(v, c)
#ifdef COLOR_LS_F
if (color_context_ls) {
lastword = nextword;
- nextword = (struct wordent *) xcalloc(1, sizeof cmd);
+ nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(STRmmcolormauto);
lastword->next = nextword;
nextword->prev = lastword;
@@ -342,7 +286,7 @@ dolist(v, c)
#endif /* COLOR_LS_F */
lastword = nextword;
for (cp = *v; cp; cp = *++v) {
- nextword = (struct wordent *) xcalloc(1, sizeof cmd);
+ nextword = xcalloc(1, sizeof cmd);
nextword->word = quote(Strsave(cp));
lastword->next = nextword;
nextword->prev = lastword;
@@ -350,9 +294,11 @@ dolist(v, c)
}
lastword->next = &cmd;
cmd.prev = lastword;
+ cleanup_push(&cmd, lex_cleanup);
/* build a syntax tree for the command. */
t = syntax(cmd.next, &cmd, 0);
+ cleanup_push(t, syntax_cleanup);
if (seterr)
stderror(ERR_OLD);
/* expand aliases like process() does */
@@ -360,32 +306,34 @@ dolist(v, c)
/* execute the parse tree. */
execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, FALSE);
/* done. free the lex list and parse tree. */
- freelex(&cmd), freesyn(t);
+ cleanup_until(&cmd);
if (setintr)
-#ifdef BSDSIGS
- (void) sigsetmask(omask);
-#else /* !BSDSIGS */
- (void) sigrelse(SIGINT);
-#endif /* BSDSIGS */
+ cleanup_until(&pintr_disabled);
}
else {
- Char *dp, *tmp, buf[MAXPATHLEN];
+ Char *dp, *tmp;
+ struct Strbuf buf = Strbuf_INIT;
+ cleanup_push(&buf, Strbuf_cleanup);
for (k = 0, i = 0; v[k] != NULL; k++) {
tmp = dnormalize(v[k], symlinks == SYM_IGNORE);
- dp = &tmp[Strlen(tmp) - 1];
+ cleanup_push(tmp, xfree);
+ dp = Strend(tmp) - 1;
if (*dp == '/' && dp != tmp)
#ifdef apollo
if (dp != &tmp[1])
#endif /* apollo */
*dp = '\0';
- if (stat(short2str(tmp), &st) == -1) {
+ if (stat(short2str(tmp), &st) == -1) {
+ int err;
+
+ err = errno;
if (k != i) {
if (i != 0)
xputchar('\n');
print_by_column(STRNULL, &v[i], k - i, FALSE);
}
- xprintf("%S: %s.\n", tmp, strerror(errno));
+ xprintf("%S: %s.\n", tmp, strerror(err));
i = k + 1;
}
else if (S_ISDIR(st.st_mode)) {
@@ -399,22 +347,26 @@ dolist(v, c)
if (k != 0 && v[1] != NULL)
xputchar('\n');
xprintf("%S:\n", tmp);
- for (cp = tmp, dp = buf; *cp; *dp++ = (*cp++ | QUOTE))
- continue;
+ buf.len = 0;
+ for (cp = tmp; *cp; cp++)
+ Strbuf_append1(&buf, (*cp | QUOTE));
+ Strbuf_terminate(&buf);
+ dp = &buf.s[buf.len - 1];
if (
#ifdef WINNT_NATIVE
- (dp[-1] != (Char) (':' | QUOTE)) &&
+ (*dp != (Char) (':' | QUOTE)) &&
#endif /* WINNT_NATIVE */
- (dp[-1] != (Char) ('/' | QUOTE)))
- *dp++ = '/';
- else
- dp[-1] &= TRIM;
- *dp = '\0';
- (void) t_search(buf, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0);
+ (*dp != (Char) ('/' | QUOTE))) {
+ Strbuf_append1(&buf, '/');
+ Strbuf_terminate(&buf);
+ } else
+ *dp &= TRIM;
+ (void) t_search(&buf, LIST, TW_ZERO, 0, STRNULL, 0);
i = k + 1;
}
- xfree((ptr_t) tmp);
+ cleanup_until(tmp);
}
+ cleanup_until(&buf);
if (k != i) {
if (i != 0)
xputchar('\n');
@@ -422,19 +374,14 @@ dolist(v, c)
}
}
- if (gargv) {
- blkfree(gargv);
- gargv = 0;
- }
+ cleanup_until(globbed);
}
extern int GotTermCaps;
/*ARGSUSED*/
void
-dotelltc(v, c)
- Char **v;
- struct command *c;
+dotelltc(Char **v, struct command *c)
{
USE(v);
USE(c);
@@ -445,9 +392,7 @@ dotelltc(v, c)
/*ARGSUSED*/
void
-doechotc(v, c)
- Char **v;
- struct command *c;
+doechotc(Char **v, struct command *c)
{
USE(c);
if (!GotTermCaps)
@@ -457,19 +402,20 @@ doechotc(v, c)
/*ARGSUSED*/
void
-dosettc(v, c)
- Char **v;
- struct command *c;
+dosettc(Char **v, struct command *c)
{
- char tv[2][BUFSIZE];
+ char *tv[2];
USE(c);
if (!GotTermCaps)
GetTermCaps();
- (void) strcpy(tv[0], short2str(v[1]));
- (void) strcpy(tv[1], short2str(v[2]));
+ tv[0] = strsave(short2str(v[1]));
+ cleanup_push(tv[0], xfree);
+ tv[1] = strsave(short2str(v[2]));
+ cleanup_push(tv[1], xfree);
SetTC(tv[0], tv[1]);
+ cleanup_until(tv[0]);
}
/* The dowhich() is by:
@@ -481,9 +427,7 @@ dosettc(v, c)
* Thanks!!
*/
int
-cmd_expand(cmd, str)
- Char *cmd;
- Char *str;
+cmd_expand(Char *cmd, Char **str)
{
struct wordent lexp[3];
struct varent *vp;
@@ -506,8 +450,8 @@ cmd_expand(cmd, str)
blkpr(vp->vec);
xputchar('\n');
}
- else
- blkexpand(vp->vec, str);
+ else
+ *str = blkexpand(vp->vec);
}
else {
lexp[1].word = cmd;
@@ -519,54 +463,35 @@ cmd_expand(cmd, str)
/*ARGSUSED*/
void
-dowhich(v, c)
- Char **v;
- struct command *c;
+dowhich(Char **v, struct command *c)
{
int rv = TRUE;
USE(c);
-#ifdef notdef
- /*
+ /*
* We don't want to glob dowhich args because we lose quoteing
* E.g. which \ls if ls is aliased will not work correctly if
* we glob here.
*/
- gflag = 0, tglob(v);
- if (gflag) {
- v = globall(v);
- if (v == 0)
- stderror(ERR_NAME | ERR_NOMATCH);
- }
- else {
- v = gargv = saveblk(v);
- trim(v);
- }
-#endif
while (*++v)
rv &= cmd_expand(*v, NULL);
if (!rv)
- set(STRstatus, Strsave(STR1), VAR_READWRITE);
-
-#ifdef notdef
- /* Again look at the comment above; since we don't glob, we don't free */
- if (gargv)
- blkfree(gargv), gargv = 0;
-#endif
+ setcopy(STRstatus, STR1, VAR_READWRITE);
}
/* PWP: a hack to start up your stopped editor on a single keystroke */
/* jbs - fixed hack so it worked :-) 3/28/89 */
struct process *
-find_stop_ed()
+find_stop_ed(void)
{
struct process *pp, *retp;
const char *ep, *vp;
char *cp, *p;
- int epl, vpl, pstatus;
+ size_t epl, vpl;
+ int pstatus;
if ((ep = getenv("EDITOR")) != NULL) { /* if we have a value */
if ((p = strrchr(ep, '/')) != NULL) /* if it has a path */
@@ -616,8 +541,8 @@ find_stop_ed()
cp = p; /* else we get all of it */
/* if we find either in the current name, fg it */
- if (strncmp(ep, cp, (size_t) epl) == 0 ||
- strncmp(vp, cp, (size_t) vpl) == 0) {
+ if (strncmp(ep, cp, epl) == 0 ||
+ strncmp(vp, cp, vpl) == 0) {
/*
* If there is a choice, then choose the current process if
@@ -635,31 +560,26 @@ find_stop_ed()
}
void
-fg_proc_entry(pp)
- struct process *pp;
+fg_proc_entry(struct process *pp)
{
-#ifdef BSDSIGS
- sigmask_t omask;
-#endif
jmp_buf_t osetexit;
int ohaderr;
Char oGettingInput;
+ size_t omark;
getexit(osetexit);
-#ifdef BSDSIGS
- omask = sigblock(sigmask(SIGINT));
-#else
- (void) sighold(SIGINT);
-#endif
+ pintr_disabled++;
oGettingInput = GettingInput;
GettingInput = 0;
ohaderr = haderr; /* we need to ignore setting of haderr due to
* process getting stopped by a signal */
+ omark = cleanup_push_mark();
if (setexit() == 0) { /* come back here after pjwait */
pendjob();
(void) alarm(0); /* No autologout */
+ alrmcatch_disabled = 1;
if (!pstart(pp, 1)) {
pp->p_procid = 0;
stderror(ERR_BADJOB, pp->p_command, strerror(errno));
@@ -667,53 +587,61 @@ fg_proc_entry(pp)
pjwait(pp);
}
setalarm(1); /* Autologout back on */
+ cleanup_pop_mark(omark);
resexit(osetexit);
haderr = ohaderr;
GettingInput = oGettingInput;
-#ifdef BSDSIGS
- (void) sigsetmask(omask);
-#else /* !BSDSIGS */
- (void) sigrelse(SIGINT);
-#endif /* BSDSIGS */
-
+ disabled_cleanup(&pintr_disabled);
}
static char *
-xgetpass(prm)
- const char *prm;
+xgetpass(const char *prm)
{
- static char pass[PASSMAX + 1];
- int fd, i;
- signalfun_t sigint;
+ static struct strbuf pass; /* = strbuf_INIT; */
+ int fd;
+ sigset_t oset, set;
+ struct sigaction sa, osa;
+
+ sa.sa_handler = SIG_IGN;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ (void)sigaction(SIGINT, &sa, &osa);
+
+ sigemptyset(&set);
+ sigaddset(&set, SIGINT);
+ (void)sigprocmask(SIG_UNBLOCK, &set, &oset);
- sigint = (signalfun_t) sigset(SIGINT, SIG_IGN);
+ cleanup_push(&osa, sigint_cleanup);
+ cleanup_push(&oset, sigprocmask_cleanup);
(void) Rawmode(); /* Make sure, cause we want echo off */
- if ((fd = open("/dev/tty", O_RDWR|O_LARGEFILE)) == -1)
+ fd = xopen("/dev/tty", O_RDWR|O_LARGEFILE);
+ if (fd == -1)
fd = SHIN;
+ else
+ cleanup_push(&fd, open_cleanup);
xprintf("%s", prm); flush();
- for (i = 0;;) {
- if (read(fd, &pass[i], 1) < 1 || pass[i] == '\n')
+ pass.len = 0;
+ for (;;) {
+ char c;
+
+ if (xread(fd, &c, 1) < 1 || c == '\n')
break;
- if (i < PASSMAX)
- i++;
+ strbuf_append1(&pass, c);
}
-
- pass[i] = '\0';
+ strbuf_terminate(&pass);
- if (fd != SHIN)
- (void) close(fd);
- (void) sigset(SIGINT, sigint);
+ cleanup_until(&sa);
- return(pass);
+ return pass.s;
}
-
+
#ifndef NO_CRYPT
-#ifndef __STDC__
- extern char *crypt __P(());
+#if !HAVE_DECL_CRYPT
+ extern char *crypt ();
#endif
-#ifdef __linux__
+#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#endif
@@ -728,8 +656,7 @@ xgetpass(prm)
*/
/*ARGSUSED*/
static void
-auto_lock(n)
- int n;
+auto_lock(void)
{
#ifndef NO_CRYPT
@@ -739,14 +666,14 @@ auto_lock(n)
#undef XCRYPT
-#if defined(HAVE_AUTH_H)
+#if defined(HAVE_AUTH_H) && defined(HAVE_GETAUTHUID)
struct authorization *apw;
- extern char *crypt16 __P((const char *, const char *));
+ extern char *crypt16 (const char *, const char *);
# define XCRYPT(a, b) crypt16(a, b)
- if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */
+ if ((pw = xgetpwuid(euid)) != NULL && /* effective user passwd */
(apw = getauthuid(euid)) != NULL) /* enhanced ultrix passwd */
srpp = apw->a_password;
@@ -756,34 +683,35 @@ auto_lock(n)
# define XCRYPT(a, b) crypt(a, b)
- if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */
- (spw = getspnam(pw->pw_name)) != NULL) /* shadowed passwd */
- srpp = spw->sp_pwdp;
+ if ((pw = xgetpwuid(euid)) != NULL) { /* effective user passwd */
+ errno = 0;
+ while ((spw = getspnam(pw->pw_name)) == NULL && errno == EINTR) {
+ handle_pending_signals();
+ errno = 0;
+ }
+ if (spw != NULL) /* shadowed passwd */
+ srpp = spw->sp_pwdp;
+ }
#else
#define XCRYPT(a, b) crypt(a, b)
#if !defined(__MVS__)
- if ((pw = getpwuid(euid)) != NULL) /* effective user passwd */
+ if ((pw = xgetpwuid(euid)) != NULL) /* effective user passwd */
srpp = pw->pw_passwd;
#endif /* !MVS */
#endif
if (srpp == NULL) {
- auto_logout(0);
+ auto_logout();
/*NOTREACHED*/
return;
}
setalarm(0); /* Not for locking any more */
-#ifdef BSDSIGS
- (void) sigsetmask(sigblock(0) & ~(sigmask(SIGALRM)));
-#else /* !BSDSIGS */
- (void) sigrelse(SIGALRM);
-#endif /* BSDSIGS */
- xputchar('\n');
+ xputchar('\n');
for (i = 0; i < 5; i++) {
const char *crpp;
char *pp;
@@ -797,7 +725,7 @@ auto_lock(n)
if ((afsname = getenv("AFSUSER")) == NULL)
afsname = pw->pw_name;
#endif
- pp = xgetpass("Password:");
+ pp = xgetpass("Password:");
crpp = XCRYPT(pp, srpp);
if ((strcmp(crpp, srpp) == 0)
@@ -813,11 +741,11 @@ auto_lock(n)
== 0)
#endif /* AFS */
) {
- (void) memset(pp, 0, PASSMAX);
+ (void) memset(pp, 0, strlen(pp));
if (GettingInput && !just_signaled) {
(void) Rawmode();
- ClearLines();
- ClearDisp();
+ ClearLines();
+ ClearDisp();
Refresh();
}
just_signaled = 0;
@@ -826,22 +754,19 @@ auto_lock(n)
xprintf(CGETS(22, 2, "\nIncorrect passwd for %s\n"), pw->pw_name);
}
#endif /* NO_CRYPT */
- auto_logout(0);
- USE(n);
+ auto_logout();
}
static void
-auto_logout(n)
- int n;
+auto_logout(void)
{
- USE(n);
xprintf("auto-logout\n");
/* Don't leave the tty in raw mode */
if (editing)
(void) Cookedmode();
- (void) close(SHIN);
- set(STRlogout, Strsave(STRautomatic), VAR_READWRITE);
+ xclose(SHIN);
+ setcopy(STRlogout, STRautomatic, VAR_READWRITE);
child = 1;
#ifdef TESLA
do_logout = 1;
@@ -850,19 +775,10 @@ auto_logout(n)
goodbye(NULL, NULL);
}
-RETSIGTYPE
-/*ARGSUSED*/
-alrmcatch(snum)
-int snum;
+void
+alrmcatch(void)
{
- USE(snum);
-#ifdef UNRELSIGS
- if (snum)
- (void) sigset(SIGALRM, alrmcatch);
-#endif /* UNRELSIGS */
-
- (*alm_fun)(0);
-
+ (*alm_fun)();
setalarm(1);
}
@@ -876,15 +792,10 @@ int snum;
* one's current directory just before each command.
*/
void
-precmd()
+precmd(void)
{
-#ifdef BSDSIGS
- sigmask_t omask;
-
- omask = sigblock(sigmask(SIGINT));
-#else /* !BSDSIGS */
- (void) sighold(SIGINT);
-#endif /* BSDSIGS */
+ pintr_disabled++;
+ cleanup_push(&pintr_disabled, disabled_cleanup);
if (precmd_active) { /* an error must have been caught */
aliasrun(2, STRunalias, STRprecmd);
xprintf(CGETS(22, 3, "Faulty alias 'precmd' removed.\n"));
@@ -895,23 +806,14 @@ precmd()
aliasrun(1, STRprecmd, NULL);
leave:
precmd_active = 0;
-#ifdef BSDSIGS
- (void) sigsetmask(omask);
-#else /* !BSDSIGS */
- (void) sigrelse(SIGINT);
-#endif /* BSDSIGS */
+ cleanup_until(&pintr_disabled);
}
void
-postcmd()
+postcmd(void)
{
-#ifdef BSDSIGS
- sigmask_t omask;
-
- omask = sigblock(sigmask(SIGINT));
-#else /* !BSDSIGS */
- (void) sighold(SIGINT);
-#endif /* BSDSIGS */
+ pintr_disabled++;
+ cleanup_push(&pintr_disabled, disabled_cleanup);
if (postcmd_active) { /* an error must have been caught */
aliasrun(2, STRunalias, STRpostcmd);
xprintf(CGETS(22, 3, "Faulty alias 'postcmd' removed.\n"));
@@ -922,11 +824,7 @@ postcmd()
aliasrun(1, STRpostcmd, NULL);
leave:
postcmd_active = 0;
-#ifdef BSDSIGS
- (void) sigsetmask(omask);
-#else /* !BSDSIGS */
- (void) sigrelse(SIGINT);
-#endif /* BSDSIGS */
+ cleanup_until(&pintr_disabled);
}
/*
@@ -936,15 +834,10 @@ leave:
* space.
*/
void
-cwd_cmd()
+cwd_cmd(void)
{
-#ifdef BSDSIGS
- sigmask_t omask;
-
- omask = sigblock(sigmask(SIGINT));
-#else /* !BSDSIGS */
- (void) sighold(SIGINT);
-#endif /* BSDSIGS */
+ pintr_disabled++;
+ cleanup_push(&pintr_disabled, disabled_cleanup);
if (cwdcmd_active) { /* an error must have been caught */
aliasrun(2, STRunalias, STRcwdcmd);
xprintf(CGETS(22, 4, "Faulty alias 'cwdcmd' removed.\n"));
@@ -955,11 +848,7 @@ cwd_cmd()
aliasrun(1, STRcwdcmd, NULL);
leave:
cwdcmd_active = 0;
-#ifdef BSDSIGS
- (void) sigsetmask(omask);
-#else /* !BSDSIGS */
- (void) sigrelse(SIGINT);
-#endif /* BSDSIGS */
+ cleanup_until(&pintr_disabled);
}
/*
@@ -967,15 +856,10 @@ leave:
* to beep the terminal bell. Useful for playing nice sounds instead.
*/
void
-beep_cmd()
+beep_cmd(void)
{
-#ifdef BSDSIGS
- sigmask_t omask;
-
- omask = sigblock(sigmask(SIGINT));
-#else /* !BSDSIGS */
- (void) sighold(SIGINT);
-#endif /* BSDSIGS */
+ pintr_disabled++;
+ cleanup_push(&pintr_disabled, disabled_cleanup);
if (beepcmd_active) { /* an error must have been caught */
aliasrun(2, STRunalias, STRbeepcmd);
xprintf(CGETS(22, 5, "Faulty alias 'beepcmd' removed.\n"));
@@ -986,11 +870,7 @@ beep_cmd()
aliasrun(1, STRbeepcmd, NULL);
}
beepcmd_active = 0;
-#ifdef BSDSIGS
- (void) sigsetmask(omask);
-#else /* !BSDSIGS */
- (void) sigrelse(SIGINT);
-#endif /* BSDSIGS */
+ cleanup_until(&pintr_disabled);
}
@@ -1000,17 +880,13 @@ beep_cmd()
* $tperiod minutes. Useful for occasional checking of msgs and such.
*/
void
-period_cmd()
+period_cmd(void)
{
Char *vp;
time_t t, interval;
-#ifdef BSDSIGS
- sigmask_t omask;
- omask = sigblock(sigmask(SIGINT));
-#else /* !BSDSIGS */
- (void) sighold(SIGINT);
-#endif /* BSDSIGS */
+ pintr_disabled++;
+ cleanup_push(&pintr_disabled, disabled_cleanup);
if (periodic_active) { /* an error must have been caught */
aliasrun(2, STRunalias, STRperiodic);
xprintf(CGETS(22, 6, "Faulty alias 'periodic' removed.\n"));
@@ -1032,11 +908,7 @@ period_cmd()
}
leave:
periodic_active = 0;
-#ifdef BSDSIGS
- (void) sigsetmask(omask);
-#else /* !BSDSIGS */
- (void) sigrelse(SIGINT);
-#endif /* BSDSIGS */
+ cleanup_until(&pintr_disabled);
}
@@ -1049,16 +921,10 @@ leave:
* Cloned from cwd_cmd().
*/
void
-job_cmd(args)
- Char *args;
+job_cmd(Char *args)
{
-#ifdef BSDSIGS
- sigmask_t omask;
-
- omask = sigblock(sigmask(SIGINT));
-#else /* !BSDSIGS */
- (void) sighold(SIGINT);
-#endif /* BSDSIGS */
+ pintr_disabled++;
+ cleanup_push(&pintr_disabled, disabled_cleanup);
if (jobcmd_active) { /* an error must have been caught */
aliasrun(2, STRunalias, STRjobcmd);
xprintf(CGETS(22, 14, "Faulty alias 'jobcmd' removed.\n"));
@@ -1072,11 +938,7 @@ job_cmd(args)
}
leave:
jobcmd_active = 0;
-#ifdef BSDSIGS
- (void) sigsetmask(omask);
-#else /* !BSDSIGS */
- (void) sigrelse(SIGINT);
-#endif /* BSDSIGS */
+ cleanup_until(&pintr_disabled);
}
@@ -1086,22 +948,21 @@ leave:
* This code is based on the mainline of process().
*/
void
-aliasrun(cnt, s1, s2)
- int cnt;
- Char *s1, *s2;
+aliasrun(int cnt, Char *s1, Char *s2)
{
struct wordent w, *new1, *new2; /* for holding alias name */
struct command *t = NULL;
jmp_buf_t osetexit;
int status;
+ size_t omark;
getexit(osetexit);
if (seterr) {
- xfree((ptr_t) seterr);
+ xfree(seterr);
seterr = NULL; /* don't repeatedly print err msg. */
}
w.word = STRNULL;
- new1 = (struct wordent *) xcalloc(1, sizeof w);
+ new1 = xcalloc(1, sizeof w);
new1->word = Strsave(s1);
if (cnt == 1) {
/* build a lex list with one word. */
@@ -1110,12 +971,13 @@ aliasrun(cnt, s1, s2)
}
else {
/* build a lex list with two words. */
- new2 = (struct wordent *) xcalloc(1, sizeof w);
+ new2 = xcalloc(1, sizeof w);
new2->word = Strsave(s2);
w.next = new2->prev = new1;
new1->next = w.prev = new2;
new1->prev = new2->next = &w;
}
+ cleanup_push(&w, lex_cleanup);
/* Save the old status */
status = getn(varval(STRstatus));
@@ -1124,13 +986,15 @@ aliasrun(cnt, s1, s2)
alias(&w);
/* build a syntax tree for the command. */
t = syntax(w.next, &w, 0);
+ cleanup_push(t, syntax_cleanup);
if (seterr)
stderror(ERR_OLD);
psavejob();
-
+ cleanup_push(&cnt, psavejob_cleanup); /* cnt is used only as a marker */
/* catch any errors here */
+ omark = cleanup_push_mark();
if (setexit() == 0)
/* execute the parse tree. */
/*
@@ -1138,8 +1002,9 @@ aliasrun(cnt, s1, s2)
* was execute(t, tpgrp);
*/
execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, TRUE);
- /* done. free the lex list and parse tree. */
- freelex(&w), freesyn(t);
+ /* reset the error catcher to the old place */
+ cleanup_pop_mark(omark);
+ resexit(osetexit);
if (haderr) {
haderr = 0;
/*
@@ -1167,17 +1032,14 @@ aliasrun(cnt, s1, s2)
period_cmd();
#endif /* notdef */
}
- /* reset the error catcher to the old place */
- resexit(osetexit);
- prestjob();
+ cleanup_until(&w);
pendjob();
/* Restore status */
- set(STRstatus, putn(status), VAR_READWRITE);
+ setv(STRstatus, putn(status), VAR_READWRITE);
}
void
-setalarm(lck)
- int lck;
+setalarm(int lck)
{
struct varent *vp;
Char *cp;
@@ -1224,14 +1086,14 @@ setalarm(lck)
alm_fun = sched_run;
}
}
+ alrmcatch_disabled = 0;
(void) alarm(alrm_time); /* Autologout ON */
}
#undef RMDEBUG /* For now... */
void
-rmstar(cp)
- struct wordent *cp;
+rmstar(struct wordent *cp)
{
struct wordent *we, *args;
struct wordent *tmp, *del;
@@ -1295,18 +1157,18 @@ rmstar(cp)
*tmp->word != ';' && tmp != cp;) {
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
- xfree((ptr_t) tmp->word);
+ xfree(tmp->word);
del = tmp;
tmp = tmp->next;
- xfree((ptr_t) del);
+ xfree(del);
}
if (*tmp->word == ';') {
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
- xfree((ptr_t) tmp->word);
+ xfree(tmp->word);
del = tmp;
tmp = tmp->next;
- xfree((ptr_t) del);
+ xfree(del);
}
we = tmp;
continue;
@@ -1337,8 +1199,7 @@ rmstar(cp)
#undef CNDEBUG /* For now */
void
-continue_jobs(cp)
- struct wordent *cp;
+continue_jobs(struct wordent *cp)
{
struct wordent *we;
struct process *pp, *np;
@@ -1409,29 +1270,27 @@ continue_jobs(cp)
/* The actual "aliasing" of for backgrounds() is done here
with the aid of insert_we(). */
static void
-insert(pl, file_args)
- struct wordent *pl;
- int file_args;
+insert(struct wordent *pl, int file_args)
{
struct wordent *now, *last;
Char *cmd, *bcmd, *cp1, *cp2;
- int cmd_len;
+ size_t cmd_len;
Char *upause = STRunderpause;
- int p_len = (int) Strlen(upause);
+ size_t p_len = Strlen(upause);
- cmd_len = (int) Strlen(pl->word);
- cmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 1) * sizeof(Char)));
+ cmd_len = Strlen(pl->word);
+ cmd = xcalloc(1, (cmd_len + 1) * sizeof(Char));
(void) Strcpy(cmd, pl->word);
/* Do insertions at beginning, first replace command word */
if (file_args) {
now = pl;
- xfree((ptr_t) now->word);
- now->word = (Char *) xcalloc(1, (size_t) (5 * sizeof(Char)));
+ xfree(now->word);
+ now->word = xcalloc(1, 5 * sizeof(Char));
(void) Strcpy(now->word, STRecho);
- now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
- now->word = (Char *) xcalloc(1, (size_t) (6 * sizeof(Char)));
+ now = xcalloc(1, sizeof(struct wordent));
+ now->word = xcalloc(1, 6 * sizeof(Char));
(void) Strcpy(now->word, STRbackqpwd);
insert_we(now, pl);
@@ -1439,18 +1298,18 @@ insert(pl, file_args)
last = last->next)
continue;
- now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
- now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
+ now = xcalloc(1, sizeof(struct wordent));
+ now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRgt);
insert_we(now, last->prev);
- now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
- now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
+ now = xcalloc(1, sizeof(struct wordent));
+ now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRbang);
insert_we(now, last->prev);
- now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
- now->word = (Char *) xcalloc(1, (size_t) cmd_len + p_len + 4);
+ now = xcalloc(1, sizeof(struct wordent));
+ now->word = xcalloc(1, (cmd_len + p_len + 4) * sizeof(Char));
cp1 = now->word;
cp2 = cmd;
*cp1++ = '~';
@@ -1464,17 +1323,14 @@ insert(pl, file_args)
continue;
insert_we(now, last->prev);
- now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
- now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
+ now = xcalloc(1, sizeof(struct wordent));
+ now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRsemi);
insert_we(now, last->prev);
- bcmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 2) * sizeof(Char)));
- cp1 = bcmd;
- cp2 = cmd;
- *cp1++ = '%';
- while ((*cp1++ = *cp2++) != '\0')
- continue;
- now = (struct wordent *) xcalloc(1, (size_t) (sizeof(struct wordent)));
+ bcmd = xcalloc(1, (cmd_len + 2) * sizeof(Char));
+ *bcmd = '%';
+ Strcpy(bcmd + 1, cmd);
+ now = xcalloc(1, sizeof(struct wordent));
now->word = bcmd;
insert_we(now, last->prev);
}
@@ -1482,29 +1338,24 @@ insert(pl, file_args)
struct wordent *del;
now = pl;
- xfree((ptr_t) now->word);
- now->word = (Char *) xcalloc(1,
- (size_t) ((cmd_len + 2) * sizeof(Char)));
- cp1 = now->word;
- cp2 = cmd;
- *cp1++ = '%';
- while ((*cp1++ = *cp2++) != '\0')
- continue;
+ xfree(now->word);
+ now->word = xcalloc(1, (cmd_len + 2) * sizeof(Char));
+ *now->word = '%';
+ Strcpy(now->word + 1, cmd);
for (now = now->next;
*now->word != '\n' && *now->word != ';' && now != pl;) {
now->prev->next = now->next;
now->next->prev = now->prev;
- xfree((ptr_t) now->word);
+ xfree(now->word);
del = now;
now = now->next;
- xfree((ptr_t) del);
+ xfree(del);
}
}
}
static void
-insert_we(new, where)
- struct wordent *new, *where;
+insert_we(struct wordent *new, struct wordent *where)
{
new->prev = where;
@@ -1514,8 +1365,7 @@ insert_we(new, where)
}
static int
-inlist(list, name)
- Char *list, *name;
+inlist(Char *list, Char *name)
{
Char *l, *n;
@@ -1555,23 +1405,25 @@ inlist(list, name)
static struct tildecache {
Char *user;
Char *home;
- int hlen;
+ size_t hlen;
} *tcache = NULL;
#define TILINCR 10
-int tlength = 0;
-static int tsize = TILINCR;
+size_t tlength = 0;
+static size_t tsize = TILINCR;
static int
-tildecompare(p1, p2)
- struct tildecache *p1, *p2;
+tildecompare(const void *xp1, const void *xp2)
{
+ const struct tildecache *p1, *p2;
+
+ p1 = xp1;
+ p2 = xp2;
return Strcmp(p1->user, p2->user);
}
static Char *
-gethomedir(us)
- Char *us;
+gethomedir(const Char *us)
{
struct passwd *pp;
#ifdef HESIOD
@@ -1579,7 +1431,7 @@ gethomedir(us)
Char *rp;
#endif /* HESIOD */
- pp = getpwnam(short2str(us));
+ pp = xgetpwnam(short2str(us));
#ifdef YPBUGS
fix_yp_bugs();
#endif /* YPBUGS */
@@ -1624,7 +1476,7 @@ gethomedir(us)
#if 0
/* Don't return if root */
if (rp != NULL && rp[0] == '/' && rp[1] == '\0') {
- xfree((ptr_t)rp);
+ xfree(rp);
rp = NULL;
}
#endif
@@ -1635,8 +1487,7 @@ gethomedir(us)
}
Char *
-gettilde(us)
- Char *us;
+gettilde(const Char *us)
{
struct tildecache *bp1, *bp2, *bp;
Char *hd;
@@ -1646,8 +1497,7 @@ gettilde(us)
return NULL;
if (tcache == NULL)
- tcache = (struct tildecache *) xmalloc((size_t) (TILINCR *
- sizeof(struct tildecache)));
+ tcache = xmalloc(TILINCR * sizeof(struct tildecache));
/*
* Binary search
*/
@@ -1674,16 +1524,13 @@ gettilde(us)
*/
tcache[tlength].user = Strsave(us);
tcache[tlength].home = hd;
- tcache[tlength++].hlen = (int) Strlen(hd);
+ tcache[tlength++].hlen = Strlen(hd);
- qsort((ptr_t) tcache, (size_t) tlength, sizeof(struct tildecache),
- (int (*) __P((const void *, const void *))) tildecompare);
+ qsort(tcache, tlength, sizeof(struct tildecache), tildecompare);
if (tlength == tsize) {
tsize += TILINCR;
- tcache = (struct tildecache *) xrealloc((ptr_t) tcache,
- (size_t) (tsize *
- sizeof(struct tildecache)));
+ tcache = xrealloc(tcache, tsize * sizeof(struct tildecache));
}
return (hd);
}
@@ -1696,139 +1543,44 @@ gettilde(us)
* If we are passed a null pointer, then we flush the cache.
*/
Char *
-getusername(hm)
- Char **hm;
+getusername(Char **hm)
{
Char *h, *p;
- int i, j;
+ size_t i, j;
if (hm == NULL) {
for (i = 0; i < tlength; i++) {
- xfree((ptr_t) tcache[i].home);
- xfree((ptr_t) tcache[i].user);
+ xfree(tcache[i].home);
+ xfree(tcache[i].user);
}
- xfree((ptr_t) tcache);
+ xfree(tcache);
tlength = 0;
tsize = TILINCR;
tcache = NULL;
return NULL;
}
+ p = *hm;
if (((h = varval(STRhome)) != STRNULL) &&
- (Strncmp(p = *hm, h, (size_t) (j = (int) Strlen(h))) == 0) &&
+ (Strncmp(p, h, j = Strlen(h)) == 0) &&
(p[j] == '/' || p[j] == '\0')) {
*hm = &p[j];
return STRNULL;
}
for (i = 0; i < tlength; i++)
- if ((Strncmp(p = *hm, tcache[i].home, (size_t)
- (j = tcache[i].hlen)) == 0) && (p[j] == '/' || p[j] == '\0')) {
+ if ((Strncmp(p, tcache[i].home, (j = tcache[i].hlen)) == 0) &&
+ (p[j] == '/' || p[j] == '\0')) {
*hm = &p[j];
return tcache[i].user;
}
return NULL;
}
-#ifdef OBSOLETE
-/*
- * PWP: read a bunch of aliases out of a file QUICKLY. The format
- * is almost the same as the result of saying "alias > FILE", except
- * that saying "aliases > FILE" does not expand non-letters to printable
- * sequences.
- */
-/*ARGSUSED*/
-void
-doaliases(v, c)
- Char **v;
- struct command *c;
-{
- jmp_buf_t oldexit;
- Char **vec, *lp;
- int fd;
- Char buf[BUFSIZE], line[BUFSIZE];
- char tbuf[BUFSIZE + 1], *tmp;
- extern int output_raw; /* PWP: in sh.print.c */
-
- USE(c);
- v++;
- if (*v == 0) {
- output_raw = 1;
- plist(&aliases, VAR_ALL);
- output_raw = 0;
- return;
- }
-
- gflag = 0, tglob(v);
- if (gflag) {
- v = globall(v);
- if (v == 0)
- stderror(ERR_NAME | ERR_NOMATCH);
- }
- else {
- v = gargv = saveblk(v);
- trim(v);
- }
-
- if ((fd = open(tmp = short2str(*v), O_RDONLY|O_LARGEFILE)) < 0)
- stderror(ERR_NAME | ERR_SYSTEM, tmp, strerror(errno));
-
- getexit(oldexit);
- if (setexit() == 0) {
- for (;;) {
- Char *p = NULL;
- int n = 0;
- lp = line;
- for (;;) {
- if (n <= 0) {
- int i;
-
- if ((n = read(fd, tbuf, BUFSIZE)) <= 0) {
-#ifdef convex
- stderror(ERR_SYSTEM, progname, strerror(errno));
-#endif /* convex */
- goto eof;
- }
- for (i = 0; i < n; i++)
- buf[i] = (Char) tbuf[i];
- p = buf;
- }
- n--;
- if ((*lp++ = *p++) == '\n') {
- lp[-1] = '\0';
- break;
- }
- }
- for (lp = line; *lp; lp++) {
- if (isspc(*lp)) {
- *lp++ = '\0';
- while (isspc(*lp))
- lp++;
- vec = (Char **) xmalloc((size_t)
- (2 * sizeof(Char **)));
- vec[0] = Strsave(lp);
- vec[1] = NULL;
- setq(strip(line), vec, &aliases, VAR_READWRITE);
- break;
- }
- }
- }
- }
-
-eof:
- (void) close(fd);
- tw_cmd_free();
- if (gargv)
- blkfree(gargv), gargv = 0;
- resexit(oldexit);
-}
-#endif /* OBSOLETE */
-
/*
* set the shell-level var to 1 or apply change to it.
*/
void
-shlvl(val)
- int val;
+shlvl(int val)
{
char *cp;
@@ -1845,16 +1597,19 @@ shlvl(val)
Unsetenv(STRKSHLVL);
}
else {
- Char buff[BUFSIZE];
-
- (void) Itoa(val, buff, 0, 0);
- set(STRshlvl, Strsave(buff), VAR_READWRITE);
- tsetenv(STRKSHLVL, buff);
+ Char *p;
+
+ p = Itoa(val, 0, 0);
+ cleanup_push(p, xfree);
+ setv(STRshlvl, p, VAR_READWRITE);
+ cleanup_ignore(p);
+ cleanup_until(p);
+ tsetenv(STRKSHLVL, p);
}
}
else {
- set(STRshlvl, SAVE("1"), VAR_READWRITE);
- tsetenv(STRKSHLVL, str2short("1"));
+ setcopy(STRshlvl, STR1, VAR_READWRITE);
+ tsetenv(STRKSHLVL, STR1);
}
}
@@ -1863,8 +1618,7 @@ shlvl(val)
* Try to recover from a read error
*/
int
-fixio(fd, e)
- int fd, e;
+fixio(int fd, int e)
{
switch (e) {
case -1: /* Make sure that the code is reachable */
@@ -1955,9 +1709,7 @@ fixio(fd, e)
* String collation
*/
int
-collate(a, b)
- const Char *a;
- const Char *b;
+collate(const Char *a, const Char *b)
{
int rv;
#ifdef SHORT_STRINGS
@@ -1969,7 +1721,7 @@ collate(a, b)
char *sb = strip(strsave(b));
#endif /* SHORT_STRINGS */
-#if defined(NLS) && !defined(NOSTRCOLL)
+#if defined(NLS) && defined(HAVE_STRCOLL)
errno = 0; /* strcoll sets errno, another brain-damage */
rv = strcoll(sa, sb);
@@ -1980,16 +1732,16 @@ collate(a, b)
* only documented valid errno value for strcoll [EINVAL]
*/
if (errno == EINVAL) {
- xfree((ptr_t) sa);
- xfree((ptr_t) sb);
+ xfree(sa);
+ xfree(sb);
stderror(ERR_SYSTEM, "strcoll", strerror(errno));
}
#else
rv = strcmp(sa, sb);
-#endif /* NLS && !NOSTRCOLL */
+#endif /* NLS && HAVE_STRCOLL */
- xfree((ptr_t) sa);
- xfree((ptr_t) sb);
+ xfree(sa);
+ xfree(sb);
return rv;
}
@@ -2001,27 +1753,23 @@ collate(a, b)
* If it is, splice the header into the argument list and retry.
*/
#define HACKBUFSZ 1024 /* Max chars in #! vector */
-#define HACKVECSZ 128 /* Max words in #! vector */
int
-hashbang(fd, vp)
- int fd;
- Char ***vp;
+hashbang(int fd, Char ***vp)
{
- unsigned char lbuf[HACKBUFSZ];
- char *sargv[HACKVECSZ];
- unsigned char *p, *ws;
- int sargc = 0;
+ struct blk_buf sarg = BLK_BUF_INIT;
+ char lbuf[HACKBUFSZ], *p, *ws;
#ifdef WINNT_NATIVE
int fw = 0; /* found at least one word */
- int first_word = 0;
+ int first_word = 1;
+ char *real;
#endif /* WINNT_NATIVE */
- if (read(fd, (char *) lbuf, HACKBUFSZ) <= 0)
+ if (xread(fd, lbuf, HACKBUFSZ) <= 0)
return -1;
ws = 0; /* word started = 0 */
- for (p = lbuf; p < &lbuf[HACKBUFSZ]; )
+ for (p = lbuf; p < &lbuf[HACKBUFSZ]; ) {
switch (*p) {
case ' ':
case '\t':
@@ -2030,27 +1778,23 @@ hashbang(fd, vp)
#endif /* WINNT_NATIVE */
if (ws) { /* a blank after a word.. save it */
*p = '\0';
-#ifndef WINNT_NATIVE
- if (sargc < HACKVECSZ - 1)
- sargv[sargc++] = ws;
- ws = NULL;
-#else /* WINNT_NATIVE */
- if (sargc < HACKVECSZ - 1) {
- sargv[sargc] = first_word ? NULL: hb_subst(ws);
- if (sargv[sargc] == NULL)
- sargv[sargc] = ws;
- sargc++;
+#ifdef WINNT_NATIVE
+ if (first_word) {
+ real = hb_subst(ws);
+ if (real != NULL)
+ ws = real;
}
- ws = NULL;
fw = 1;
- first_word = 1;
+ first_word = 0;
#endif /* WINNT_NATIVE */
+ bb_append(&sarg, SAVE(ws));
+ ws = NULL;
}
p++;
continue;
case '\0': /* Whoa!! what the hell happened */
- return -1;
+ goto err;
case '\n': /* The end of the line. */
if (
@@ -2059,26 +1803,23 @@ hashbang(fd, vp)
#endif /* WINNT_NATIVE */
ws) { /* terminate the last word */
*p = '\0';
-#ifndef WINNT_NATIVE
- if (sargc < HACKVECSZ - 1)
- sargv[sargc++] = ws;
-#else /* WINNT_NATIVE */
- if (sargc < HACKVECSZ - 1) { /* deal with the 1-word case */
- sargv[sargc] = first_word? NULL : hb_subst(ws);
- if (sargv[sargc] == NULL)
- sargv[sargc] = ws;
- sargc++;
+#ifdef WINNT_NATIVE
+ /* deal with the 1-word case */
+ if (first_word) {
+ real = hb_subst(ws);
+ if (real != NULL)
+ ws = real;
}
#endif /* !WINNT_NATIVE */
+ if (ws)
+ bb_append(&sarg, SAVE(ws));
}
- sargv[sargc] = NULL;
- ws = NULL;
- if (sargc > 0) {
- *vp = blk2short(sargv);
+ if (sarg.len > 0) {
+ *vp = bb_finish(&sarg);
return 0;
}
else
- return -1;
+ goto err;
default:
if (!ws) /* Start a new word? */
@@ -2086,42 +1827,34 @@ hashbang(fd, vp)
p++;
break;
}
+ }
+ err:
+ bb_cleanup(&sarg);
return -1;
}
#endif /* HASHBANG */
#ifdef REMOTEHOST
-static RETSIGTYPE
-palarm(snum)
- int snum;
+static void
+palarm(int snum)
{
USE(snum);
-#ifdef UNRELSIGS
- if (snum)
- (void) sigset(snum, SIG_IGN);
-#endif /* UNRELSIGS */
- (void) alarm(0);
- reset();
+ _exit(1);
}
-
static void
-getremotehost()
+getremotehost(int dest_fd)
{
const char *host = NULL;
#ifdef INET6
struct sockaddr_storage saddr;
- socklen_t len = sizeof(struct sockaddr_storage);
static char hbuf[NI_MAXHOST];
#else
struct hostent* hp;
struct sockaddr_in saddr;
- socklen_t len = sizeof(struct sockaddr_in);
-#endif
-#ifdef HAVE_STRUCT_UTMP_UT_HOST
- char *sptr = NULL;
#endif
+ socklen_t len = sizeof(saddr);
#ifdef INET6
if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
@@ -2153,6 +1886,7 @@ getremotehost()
/* Avoid empty names and local X displays */
if (name != NULL && *name != '\0' && *name != ':') {
struct in_addr addr;
+ char *sptr;
/* Look for host:display.screen */
/*
@@ -2174,7 +1908,7 @@ getremotehost()
if (sptr != name) {
#ifdef INET6
char *s, *domain;
- char dbuf[MAXHOSTNAMELEN], cbuf[MAXHOSTNAMELEN];
+ char dbuf[MAXHOSTNAMELEN];
struct addrinfo hints, *res = NULL;
memset(&hints, 0, sizeof(hints));
@@ -2185,19 +1919,19 @@ getremotehost()
{
if (getaddrinfo(name, NULL, &hints, &res) != 0)
res = NULL;
- } else if (gethostname(dbuf, sizeof(dbuf) - 1) == 0 &&
- (domain = strchr(dbuf, '.')) != NULL) {
+ } else if (gethostname(dbuf, sizeof(dbuf)) == 0 &&
+ (dbuf[sizeof(dbuf)-1] = '\0', /*FIXME: ugly*/
+ (domain = strchr(dbuf, '.')) != NULL)) {
for (s = strchr(name, '.');
s != NULL; s = strchr(s + 1, '.')) {
if (*(s + 1) != '\0' &&
(ptr = strstr(domain, s)) != NULL) {
- len = s - name;
- if (len + strlen(ptr) >= sizeof(cbuf))
- break;
- strncpy(cbuf, name, len);
- strcpy(cbuf + len, ptr);
+ char *cbuf;
+
+ cbuf = strspl(name, ptr);
if (getaddrinfo(cbuf, NULL, &hints, &res) != 0)
res = NULL;
+ xfree(cbuf);
break;
}
}
@@ -2228,37 +1962,79 @@ getremotehost()
}
#endif
- if (host)
- tsetenv(STRREMOTEHOST, str2short(host));
+ if (host) {
+ size_t left;
-#ifdef HAVE_STRUCT_UTMP_UT_HOST
- if (sptr)
- *sptr = ':';
-#endif
-}
+ left = strlen(host);
+ while (left != 0) {
+ ssize_t res;
+ res = xwrite(dest_fd, host, left);
+ if (res < 0)
+ _exit(1);
+ host += res;
+ left -= res;
+ }
+ }
+ _exit(0);
+}
/*
* From: <lesv@ppvku.ericsson.se> (Lennart Svensson)
*/
-void
-remotehost()
+void
+remotehost(void)
{
- /* Don't get stuck if the resolver does not work! */
- signalfun_t osig = sigset(SIGALRM, palarm);
-
- jmp_buf_t osetexit;
- getexit(osetexit);
-
- (void) alarm(2);
-
- if (setexit() == 0)
- getremotehost();
-
- resexit(osetexit);
-
- (void) alarm(0);
- (void) sigset(SIGALRM, osig);
+ struct sigaction sa;
+ struct strbuf hostname = strbuf_INIT;
+ int fds[2], wait_options, status;
+ pid_t pid, wait_res;
+
+ sa.sa_handler = SIG_DFL; /* Make sure a zombie is created */
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = 0;
+ sigaction(SIGCHLD, &sa, NULL);
+ mypipe(fds);
+ pid = fork();
+ if (pid == 0) {
+ sigset_t set;
+ xclose(fds[0]);
+ /* Don't get stuck if the resolver does not work! */
+ signal(SIGALRM, palarm);
+ sigemptyset(&set);
+ sigaddset(&set, SIGALRM);
+ (void)sigprocmask(SIG_UNBLOCK, &set, NULL);
+ (void)alarm(2);
+ getremotehost(fds[1]);
+ /*NOTREACHED*/
+ }
+ xclose(fds[1]);
+ for (;;) {
+ char buf[BUFSIZE];
+ ssize_t res;
+
+ res = xread(fds[0], buf, sizeof(buf));
+ if (res == -1) {
+ hostname.len = 0;
+ wait_options = WNOHANG;
+ goto done;
+ }
+ if (res == 0)
+ break;
+ strbuf_appendn(&hostname, buf, res);
+ }
+ wait_options = 0;
+ done:
+ xclose(fds[0]);
+ while ((wait_res = waitpid(pid, &status, wait_options)) == -1
+ && errno == EINTR)
+ handle_pending_signals();
+ cleanup_push(&hostname, strbuf_cleanup);
+ if (wait_res == pid && WIFEXITED(status) && WEXITSTATUS(status) == 0) {
+ strbuf_terminate(&hostname);
+ tsetenv(STRREMOTEHOST, str2short(hostname.s));
+ }
+ cleanup_until(&hostname);
#ifdef YPBUGS
/* From: casper@fwi.uva.nl (Casper H.S. Dik), for Solaris 2.3 */
@@ -2277,9 +2053,7 @@ remotehost()
* platforms
*/
void
-dotermname(v, c)
- Char **v;
- struct command *c;
+dotermname(Char **v, struct command *c)
{
char *termtype;
/*
@@ -2294,7 +2068,7 @@ dotermname(v, c)
/* no luck - the user didn't provide one and none is
* specified in the environment
*/
- set(STRstatus, Strsave(STR1), VAR_READWRITE);
+ setcopy(STRstatus, STR1, VAR_READWRITE);
return;
}
@@ -2307,9 +2081,8 @@ dotermname(v, c)
*/
if (tgetent(termcap_buffer, termtype) == 1) {
xprintf("%s\n", termtype);
- set(STRstatus, Strsave(STR0), VAR_READWRITE);
- } else {
- set(STRstatus, Strsave(STR1), VAR_READWRITE);
- }
+ setcopy(STRstatus, STR0, VAR_READWRITE);
+ } else
+ setcopy(STRstatus, STR1, VAR_READWRITE);
}
#endif /* WINNT_NATIVE */
OpenPOWER on IntegriCloud