#include "sh.h"
RCSID("$tcsh: tc.func.c,v 3.136 2006/09/01 12:51:35 christos Exp $")
#include "ed.h"
#include "ed.defns.h"
#include "tw.h"
#include "tc.h"
#ifdef WINNT_NATIVE
#include "nt.const.h"
#else
#include <sys/wait.h>
#endif
#ifdef AFS
#include <afs/stds.h>
#include <afs/kautils.h>
long ka_UserAuthenticateGeneral();
#endif
#ifdef TESLA
extern int do_logout;
#endif
extern time_t t_period;
extern int just_signaled;
static int precmd_active = 0;
static int jobcmd_active = 0;
static int postcmd_active = 0;
static int periodic_active = 0;
static int cwdcmd_active = 0;
static int beepcmd_active = 0;
static void (*alm_fun)(void) = NULL;
static void auto_logout (void);
static char *xgetpass (const char *);
static void auto_lock (void);
#ifdef BSDJOBS
static void insert (struct wordent *, int);
static void insert_we (struct wordent *, struct wordent *);
static int inlist (Char *, Char *);
#endif
static int tildecompare (const void *, const void *);
static Char *gethomedir (const Char *);
#ifdef REMOTEHOST
static void palarm (int);
static void getremotehost (int);
#endif
Char *
expand_lex(const struct wordent *sp0, int from, int to)
{
struct Strbuf buf = Strbuf_INIT;
const struct wordent *sp;
Char *s;
Char prev_c;
int i;
prev_c = '\0';
if (!sp0 || (sp = sp0->next) == sp0 || sp == (sp0 = sp0->prev))
return Strbuf_finish(&buf);
for (i = 0; ; i++) {
if ((i >= from) && (i <= to)) {
for (s = sp->word; *s; s++) {
if ((*s & QUOTE)
&& (((*s & TRIM) == HIST) ||
(((*s & TRIM) == '\'') && (prev_c != '\\')) ||
(((*s & TRIM) == '\"') && (prev_c != '\\')) ||
(((*s & TRIM) == '\\') && (prev_c != '\\')))) {
Strbuf_append1(&buf, '\\');
}
Strbuf_append1(&buf, *s & TRIM);
prev_c = *s;
}
Strbuf_append1(&buf, ' ');
}
sp = sp->next;
if (sp == sp0)
break;
}
if (buf.len != 0)
buf.len--;
return Strbuf_finish(&buf);
}
Char *
sprlex(const struct wordent *sp0)
{
return expand_lex(sp0, 0, INT_MAX);
}
Char *
Itoa(int n, size_t min_digits, Char attributes)
{
#ifndef CHAR_BIT
# define CHAR_BIT 8
#endif
Char buf[CHAR_BIT * sizeof(int) / 3 + 1], *res, *p, *s;
unsigned int un;
int pad = (min_digits != 0);
if (sizeof(buf) - 1 < min_digits)
min_digits = sizeof(buf) - 1;
un = n;
if (n < 0)
un = -n;
p = buf;
do {
*p++ = un % 10 + '0';
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 res;
}
void
dolist(Char **v, struct command *c)
{
Char **globbed;
int i, k;
struct stat st;
USE(c);
if (*++v == NULL) {
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;
}
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]) {
Char *lspath;
struct command *t;
struct wordent cmd, *nextword, *lastword;
Char *cp;
struct varent *vp;
if (setintr) {
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
}
if (seterr) {
xfree(seterr);
seterr = NULL;
}
lspath = STRls;
STRmCF[1] = 'C';
STRmCF[3] = '\0';
if ((vp = adrof(STRlistflags)) != NULL && vp->vec != NULL &&
vp->vec[0] != STRNULL) {
if (vp->vec[1] != NULL && vp->vec[1][0] != '\0')
lspath = vp->vec[1];
for (cp = vp->vec[0]; *cp; cp++)
switch (*cp) {
case 'x':
STRmCF[1] = 'x';
break;
case 'a':
STRmCF[3] = 'a';
break;
case 'A':
STRmCF[3] = 'A';
break;
default:
break;
}
}
cmd.word = STRNULL;
lastword = &cmd;
nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(lspath);
lastword->next = nextword;
nextword->prev = lastword;
lastword = nextword;
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 = xcalloc(1, sizeof cmd);
nextword->word = Strsave(STRmmliteral);
lastword->next = nextword;
nextword->prev = lastword;
}
#endif
#ifdef COLOR_LS_F
if (color_context_ls) {
lastword = nextword;
nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(STRmmcolormauto);
lastword->next = nextword;
nextword->prev = lastword;
}
#endif
lastword = nextword;
for (cp = *v; cp; cp = *++v) {
nextword = xcalloc(1, sizeof cmd);
nextword->word = quote(Strsave(cp));
lastword->next = nextword;
nextword->prev = lastword;
lastword = nextword;
}
lastword->next = &cmd;
cmd.prev = lastword;
cleanup_push(&cmd, lex_cleanup);
t = syntax(cmd.next, &cmd, 0);
cleanup_push(t, syntax_cleanup);
if (seterr)
stderror(ERR_OLD);
execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, FALSE);
cleanup_until(&cmd);
if (setintr)
cleanup_until(&pintr_disabled);
}
else {
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);
cleanup_push(tmp, xfree);
dp = Strend(tmp) - 1;
if (*dp == '/' && dp != tmp)
#ifdef apollo
if (dp != &tmp[1])
#endif
*dp = '\0';
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(err));
i = k + 1;
}
else if (S_ISDIR(st.st_mode)) {
Char *cp;
if (k != i) {
if (i != 0)
xputchar('\n');
print_by_column(STRNULL, &v[i], k - i, FALSE);
}
if (k != 0 && v[1] != NULL)
xputchar('\n');
xprintf("%S:\n", tmp);
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 != (Char) (':' | QUOTE)) &&
#endif
(*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;
}
cleanup_until(tmp);
}
cleanup_until(&buf);
if (k != i) {
if (i != 0)
xputchar('\n');
print_by_column(STRNULL, &v[i], k - i, FALSE);
}
}
cleanup_until(globbed);
}
extern int GotTermCaps;
void
dotelltc(Char **v, struct command *c)
{
USE(v);
USE(c);
if (!GotTermCaps)
GetTermCaps();
TellTC();
}
void
doechotc(Char **v, struct command *c)
{
USE(c);
if (!GotTermCaps)
GetTermCaps();
EchoTC(++v);
}
void
dosettc(Char **v, struct command *c)
{
char *tv[2];
USE(c);
if (!GotTermCaps)
GetTermCaps();
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]);
}
int
cmd_expand(Char *cmd, Char **str)
{
struct wordent lexp[3];
struct varent *vp;
int rv = TRUE;
lexp[0].next = &lexp[1];
lexp[1].next = &lexp[2];
lexp[2].next = &lexp[0];
lexp[0].prev = &lexp[2];
lexp[1].prev = &lexp[0];
lexp[2].prev = &lexp[1];
lexp[0].word = STRNULL;
lexp[2].word = STRret;
if ((vp = adrof1(cmd, &aliases)) != NULL && vp->vec != NULL) {
if (str == NULL) {
xprintf(CGETS(22, 1, "%S: \t aliased to "), cmd);
blkpr(vp->vec);
xputchar('\n');
}
else
*str = blkexpand(vp->vec);
}
else {
lexp[1].word = cmd;
rv = tellmewhat(lexp, str);
}
return rv;
}
void
dowhich(Char **v, struct command *c)
{
int rv = TRUE;
USE(c);
while (*++v)
rv &= cmd_expand(*v, NULL);
if (!rv)
setcopy(STRstatus, STR1, VAR_READWRITE);
}
struct process *
find_stop_ed(void)
{
struct process *pp, *retp;
const char *ep, *vp;
char *cp, *p;
size_t epl, vpl;
int pstatus;
if ((ep = getenv("EDITOR")) != NULL) {
if ((p = strrchr(ep, '/')) != NULL)
ep = p + 1;
}
else
ep = "ed";
if ((vp = getenv("VISUAL")) != NULL) {
if ((p = strrchr(vp, '/')) != NULL)
vp = p + 1;
}
else
vp = "vi";
for (vpl = 0; vp[vpl] && !isspace((unsigned char)vp[vpl]); vpl++)
continue;
for (epl = 0; ep[epl] && !isspace((unsigned char)ep[epl]); epl++)
continue;
if (pcurrent == NULL)
return NULL;
retp = NULL;
for (pp = proclist.p_next; pp; pp = pp->p_next)
if (pp->p_procid == pp->p_jobid) {
pstatus = (int) (pp->p_flags & PALLSTATES);
if (pstatus != PINTERRUPTED && pstatus != PSTOPPED &&
pstatus != PSIGNALED)
continue;
p = short2str(pp->p_command);
for (cp = p; *cp && !isspace((unsigned char) *cp); cp++)
continue;
*cp = '\0';
if ((cp = strrchr(p, '/')) != NULL)
cp = cp + 1;
else
cp = p;
if (strncmp(ep, cp, epl) == 0 ||
strncmp(vp, cp, vpl) == 0) {
if (pp == pcurrent)
return pp;
else if (retp == NULL || pp == pprevious)
retp = pp;
}
}
return retp;
}
void
fg_proc_entry(struct process *pp)
{
jmp_buf_t osetexit;
int ohaderr;
Char oGettingInput;
size_t omark;
getexit(osetexit);
pintr_disabled++;
oGettingInput = GettingInput;
GettingInput = 0;
ohaderr = haderr;
omark = cleanup_push_mark();
if (setexit() == 0) {
pendjob();
(void) alarm(0);
alrmcatch_disabled = 1;
if (!pstart(pp, 1)) {
pp->p_procid = 0;
stderror(ERR_BADJOB, pp->p_command, strerror(errno));
}
pjwait(pp);
}
setalarm(1);
cleanup_pop_mark(omark);
resexit(osetexit);
haderr = ohaderr;
GettingInput = oGettingInput;
disabled_cleanup(&pintr_disabled);
}
static char *
xgetpass(const char *prm)
{
static struct strbuf pass;
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);
cleanup_push(&osa, sigint_cleanup);
cleanup_push(&oset, sigprocmask_cleanup);
(void) Rawmode();
fd = xopen("/dev/tty", O_RDWR|O_LARGEFILE);
if (fd == -1)
fd = SHIN;
else
cleanup_push(&fd, open_cleanup);
xprintf("%s", prm); flush();
pass.len = 0;
for (;;) {
char c;
if (xread(fd, &c, 1) < 1 || c == '\n')
break;
strbuf_append1(&pass, c);
}
strbuf_terminate(&pass);
cleanup_until(&sa);
return pass.s;
}
#ifndef NO_CRYPT
#if !HAVE_DECL_CRYPT
extern char *crypt ();
#endif
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#endif
static void
auto_lock(void)
{
#ifndef NO_CRYPT
int i;
char *srpp = NULL;
struct passwd *pw;
#undef XCRYPT
#if defined(HAVE_AUTH_H) && defined(HAVE_GETAUTHUID)
struct authorization *apw;
extern char *crypt16 (const char *, const char *);
# define XCRYPT(a, b) crypt16(a, b)
if ((pw = xgetpwuid(euid)) != NULL &&
(apw = getauthuid(euid)) != NULL)
srpp = apw->a_password;
#elif defined(HAVE_SHADOW_H)
struct spwd *spw;
# define XCRYPT(a, b) crypt(a, b)
if ((pw = xgetpwuid(euid)) != NULL) {
errno = 0;
while ((spw = getspnam(pw->pw_name)) == NULL && errno == EINTR) {
handle_pending_signals();
errno = 0;
}
if (spw != NULL)
srpp = spw->sp_pwdp;
}
#else
#define XCRYPT(a, b) crypt(a, b)
#if !defined(__MVS__)
if ((pw = xgetpwuid(euid)) != NULL)
srpp = pw->pw_passwd;
#endif
#endif
if (srpp == NULL) {
auto_logout();
return;
}
setalarm(0);
xputchar('\n');
for (i = 0; i < 5; i++) {
const char *crpp;
char *pp;
#ifdef AFS
char *afsname;
Char *safs;
if ((safs = varval(STRafsuser)) != STRNULL)
afsname = short2str(safs);
else
if ((afsname = getenv("AFSUSER")) == NULL)
afsname = pw->pw_name;
#endif
pp = xgetpass("Password:");
crpp = XCRYPT(pp, srpp);
if ((strcmp(crpp, srpp) == 0)
#ifdef AFS
|| (ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION,
afsname,
NULL,
NULL,
pp,
0,
0, 0,
NULL)
== 0)
#endif
) {
(void) memset(pp, 0, strlen(pp));
if (GettingInput && !just_signaled) {
(void) Rawmode();
ClearLines();
ClearDisp();
Refresh();
}
just_signaled = 0;
return;
}
xprintf(CGETS(22, 2, "\nIncorrect passwd for %s\n"), pw->pw_name);
}
#endif
auto_logout();
}
static void
auto_logout(void)
{
xprintf("auto-logout\n");
if (editing)
(void) Cookedmode();
xclose(SHIN);
setcopy(STRlogout, STRautomatic, VAR_READWRITE);
child = 1;
#ifdef TESLA
do_logout = 1;
#endif
GettingInput = FALSE;
goodbye(NULL, NULL);
}
void
alrmcatch(void)
{
(*alm_fun)();
setalarm(1);
}
void
precmd(void)
{
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
if (precmd_active) {
aliasrun(2, STRunalias, STRprecmd);
xprintf(CGETS(22, 3, "Faulty alias 'precmd' removed.\n"));
goto leave;
}
precmd_active = 1;
if (!whyles && adrof1(STRprecmd, &aliases))
aliasrun(1, STRprecmd, NULL);
leave:
precmd_active = 0;
cleanup_until(&pintr_disabled);
}
void
postcmd(void)
{
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
if (postcmd_active) {
aliasrun(2, STRunalias, STRpostcmd);
xprintf(CGETS(22, 3, "Faulty alias 'postcmd' removed.\n"));
goto leave;
}
postcmd_active = 1;
if (!whyles && adrof1(STRpostcmd, &aliases))
aliasrun(1, STRpostcmd, NULL);
leave:
postcmd_active = 0;
cleanup_until(&pintr_disabled);
}
void
cwd_cmd(void)
{
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
if (cwdcmd_active) {
aliasrun(2, STRunalias, STRcwdcmd);
xprintf(CGETS(22, 4, "Faulty alias 'cwdcmd' removed.\n"));
goto leave;
}
cwdcmd_active = 1;
if (!whyles && adrof1(STRcwdcmd, &aliases))
aliasrun(1, STRcwdcmd, NULL);
leave:
cwdcmd_active = 0;
cleanup_until(&pintr_disabled);
}
void
beep_cmd(void)
{
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
if (beepcmd_active) {
aliasrun(2, STRunalias, STRbeepcmd);
xprintf(CGETS(22, 5, "Faulty alias 'beepcmd' removed.\n"));
}
else {
beepcmd_active = 1;
if (!whyles && adrof1(STRbeepcmd, &aliases))
aliasrun(1, STRbeepcmd, NULL);
}
beepcmd_active = 0;
cleanup_until(&pintr_disabled);
}
void
period_cmd(void)
{
Char *vp;
time_t t, interval;
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
if (periodic_active) {
aliasrun(2, STRunalias, STRperiodic);
xprintf(CGETS(22, 6, "Faulty alias 'periodic' removed.\n"));
goto leave;
}
periodic_active = 1;
if (!whyles && adrof1(STRperiodic, &aliases)) {
vp = varval(STRtperiod);
if (vp == STRNULL) {
aliasrun(1, STRperiodic, NULL);
goto leave;
}
interval = getn(vp);
(void) time(&t);
if (t - t_period >= interval * 60) {
t_period = t;
aliasrun(1, STRperiodic, NULL);
}
}
leave:
periodic_active = 0;
cleanup_until(&pintr_disabled);
}
void
job_cmd(Char *args)
{
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
if (jobcmd_active) {
aliasrun(2, STRunalias, STRjobcmd);
xprintf(CGETS(22, 14, "Faulty alias 'jobcmd' removed.\n"));
goto leave;
}
jobcmd_active = 1;
if (!whyles && adrof1(STRjobcmd, &aliases)) {
struct process *pp = pcurrjob;
aliasrun(2, STRjobcmd, args);
pcurrjob = pp;
}
leave:
jobcmd_active = 0;
cleanup_until(&pintr_disabled);
}
void
aliasrun(int cnt, Char *s1, Char *s2)
{
struct wordent w, *new1, *new2;
struct command *t = NULL;
jmp_buf_t osetexit;
int status;
size_t omark;
getexit(osetexit);
if (seterr) {
xfree(seterr);
seterr = NULL;
}
w.word = STRNULL;
new1 = xcalloc(1, sizeof w);
new1->word = Strsave(s1);
if (cnt == 1) {
w.next = w.prev = new1;
new1->next = new1->prev = &w;
}
else {
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);
status = getn(varval(STRstatus));
alias(&w);
t = syntax(w.next, &w, 0);
cleanup_push(t, syntax_cleanup);
if (seterr)
stderror(ERR_OLD);
psavejob();
cleanup_push(&cnt, psavejob_cleanup);
omark = cleanup_push_mark();
if (setexit() == 0)
execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, TRUE);
cleanup_pop_mark(omark);
resexit(osetexit);
if (haderr) {
haderr = 0;
if (precmd_active)
precmd();
if (postcmd_active)
postcmd();
#ifdef notdef
else if (cwdcmd_active)
cwd_cmd();
else if (beepcmd_active)
beep_cmd();
else if (periodic_active)
period_cmd();
#endif
}
cleanup_until(&w);
pendjob();
setv(STRstatus, putn(status), VAR_READWRITE);
}
void
setalarm(int lck)
{
struct varent *vp;
Char *cp;
unsigned alrm_time = 0, logout_time, lock_time;
time_t cl, nl, sched_dif;
if ((vp = adrof(STRautologout)) != NULL && vp->vec != NULL) {
if ((cp = vp->vec[0]) != 0) {
if ((logout_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
#ifdef SOLARIS2
if (logout_time >= 0x7fffffff / CLK_TCK)
logout_time = 0x7fffffff / CLK_TCK;
#endif
alrm_time = logout_time;
alm_fun = auto_logout;
}
}
if ((cp = vp->vec[1]) != 0) {
if ((lock_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
if (lck) {
if (alrm_time == 0 || lock_time < alrm_time) {
alrm_time = lock_time;
alm_fun = auto_lock;
}
}
else
if (alrm_time)
alrm_time -= lock_time;
}
}
}
if ((nl = sched_next()) != -1) {
(void) time(&cl);
sched_dif = nl > cl ? nl - cl : 0;
if ((alrm_time == 0) || ((unsigned) sched_dif < alrm_time)) {
alrm_time = ((unsigned) sched_dif) + 1;
alm_fun = sched_run;
}
}
alrmcatch_disabled = 0;
(void) alarm(alrm_time);
}
#undef RMDEBUG
void
rmstar(struct wordent *cp)
{
struct wordent *we, *args;
struct wordent *tmp, *del;
#ifdef RMDEBUG
static Char STRrmdebug[] = {'r', 'm', 'd', 'e', 'b', 'u', 'g', '\0'};
Char *tag;
#endif
Char *charac;
char c;
int ask, doit, star = 0, silent = 0;
if (!adrof(STRrmstar))
return;
#ifdef RMDEBUG
tag = varval(STRrmdebug);
#endif
we = cp->next;
while (*we->word == ';' && we != cp)
we = we->next;
while (we != cp) {
#ifdef RMDEBUG
if (*tag)
xprintf(CGETS(22, 7, "parsing command line\n"));
#endif
if (!Strcmp(we->word, STRrm)) {
args = we->next;
ask = (*args->word != '-');
while (*args->word == '-' && !silent) {
for (charac = (args->word + 1); *charac && !silent; charac++)
silent = (*charac == 'i' || *charac == 'f');
args = args->next;
}
ask = (ask || (!ask && !silent));
if (ask) {
for (; !star && *args->word != ';'
&& args != cp; args = args->next)
if (!Strcmp(args->word, STRstar))
star = 1;
if (ask && star) {
xprintf(CGETS(22, 8,
"Do you really want to delete all files? [n/y] "));
flush();
(void) force_read(SHIN, &c, 1);
doit = (strchr(CGETS(22, 14, "Yy"), c) != NULL);
while (c != '\n' && force_read(SHIN, &c, 1) == 1)
continue;
if (!doit) {
#ifdef RMDEBUG
if (*tag)
xprintf(CGETS(22, 9,
"skipping deletion of files!\n"));
#endif
for (tmp = we;
*tmp->word != '\n' &&
*tmp->word != ';' && tmp != cp;) {
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
xfree(tmp->word);
del = tmp;
tmp = tmp->next;
xfree(del);
}
if (*tmp->word == ';') {
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
xfree(tmp->word);
del = tmp;
tmp = tmp->next;
xfree(del);
}
we = tmp;
continue;
}
}
}
}
for (we = we->next;
*we->word != ';' && we != cp;
we = we->next)
continue;
if (*we->word == ';')
we = we->next;
}
#ifdef RMDEBUG
if (*tag) {
xprintf(CGETS(22, 10, "command line now is:\n"));
for (we = cp->next; we != cp; we = we->next)
xprintf("%S ", we->word);
}
#endif
return;
}
#ifdef BSDJOBS
#undef CNDEBUG
void
continue_jobs(struct wordent *cp)
{
struct wordent *we;
struct process *pp, *np;
Char *cmd, *continue_list, *continue_args_list;
#ifdef CNDEBUG
Char *tag;
static Char STRcndebug[] =
{'c', 'n', 'd', 'e', 'b', 'u', 'g', '\0'};
#endif
int in_cont_list, in_cont_arg_list;
#ifdef CNDEBUG
tag = varval(STRcndebug);
#endif
continue_list = varval(STRcontinue);
continue_args_list = varval(STRcontinue_args);
if (*continue_list == '\0' && *continue_args_list == '\0')
return;
we = cp->next;
while (*we->word == ';' && we != cp)
we = we->next;
while (we != cp) {
#ifdef CNDEBUG
if (*tag)
xprintf(CGETS(22, 11, "parsing command line\n"));
#endif
cmd = we->word;
in_cont_list = inlist(continue_list, cmd);
in_cont_arg_list = inlist(continue_args_list, cmd);
if (in_cont_list || in_cont_arg_list) {
#ifdef CNDEBUG
if (*tag)
xprintf(CGETS(22, 12, "in one of the lists\n"));
#endif
np = NULL;
for (pp = proclist.p_next; pp; pp = pp->p_next) {
if (prefix(cmd, pp->p_command)) {
if (pp->p_index) {
np = pp;
break;
}
}
}
if (np) {
insert(we, in_cont_arg_list);
}
}
for (we = we->next;
*we->word != ';' && we != cp;
we = we->next)
continue;
if (*we->word == ';')
we = we->next;
}
#ifdef CNDEBUG
if (*tag) {
xprintf(CGETS(22, 13, "command line now is:\n"));
for (we = cp->next; we != cp; we = we->next)
xprintf("%S ", we->word);
}
#endif
return;
}
static void
insert(struct wordent *pl, int file_args)
{
struct wordent *now, *last;
Char *cmd, *bcmd, *cp1, *cp2;
size_t cmd_len;
Char *upause = STRunderpause;
size_t p_len = Strlen(upause);
cmd_len = Strlen(pl->word);
cmd = xcalloc(1, (cmd_len + 1) * sizeof(Char));
(void) Strcpy(cmd, pl->word);
if (file_args) {
now = pl;
xfree(now->word);
now->word = xcalloc(1, 5 * sizeof(Char));
(void) Strcpy(now->word, STRecho);
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, 6 * sizeof(Char));
(void) Strcpy(now->word, STRbackqpwd);
insert_we(now, pl);
for (last = now; *last->word != '\n' && *last->word != ';';
last = last->next)
continue;
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRgt);
insert_we(now, last->prev);
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRbang);
insert_we(now, last->prev);
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, (cmd_len + p_len + 4) * sizeof(Char));
cp1 = now->word;
cp2 = cmd;
*cp1++ = '~';
*cp1++ = '/';
*cp1++ = '.';
while ((*cp1++ = *cp2++) != '\0')
continue;
cp1--;
cp2 = upause;
while ((*cp1++ = *cp2++) != '\0')
continue;
insert_we(now, last->prev);
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRsemi);
insert_we(now, last->prev);
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);
}
else {
struct wordent *del;
now = pl;
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(now->word);
del = now;
now = now->next;
xfree(del);
}
}
}
static void
insert_we(struct wordent *new, struct wordent *where)
{
new->prev = where;
new->next = where->next;
where->next = new;
new->next->prev = new;
}
static int
inlist(Char *list, Char *name)
{
Char *l, *n;
l = list;
n = name;
while (*l && *n) {
if (*l == *n) {
l++;
n++;
if (*n == '\0' && (*l == ' ' || *l == '\0'))
return (1);
else
continue;
}
else {
while (*l && *l != ' ')
l++;
while (*l && *l == ' ')
l++;
n = name;
}
}
return (0);
}
#endif
static struct tildecache {
Char *user;
Char *home;
size_t hlen;
} *tcache = NULL;
#define TILINCR 10
size_t tlength = 0;
static size_t tsize = TILINCR;
static int
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(const Char *us)
{
struct passwd *pp;
#ifdef HESIOD
char **res, **res1, *cp;
Char *rp;
#endif
pp = xgetpwnam(short2str(us));
#ifdef YPBUGS
fix_yp_bugs();
#endif
if (pp != NULL) {
#if 0
if (pp->pw_dir[0] == '/' && pp->pw_dir[1] == '\0')
return NULL;
else
#endif
return Strsave(str2short(pp->pw_dir));
}
#ifdef HESIOD
res = hes_resolve(short2str(us), "filsys");
rp = NULL;
if (res != NULL) {
if ((*res) != NULL) {
cp = strtok(*res, " ");
if (strcmp(cp, "AFS") == 0) {
cp = strtok(NULL, " ");
if (cp != NULL)
rp = Strsave(str2short(cp));
} else if (strcmp(cp, "NFS") == 0) {
cp = NULL;
if ((strtok(NULL, " ")) &&
(strtok(NULL, " ")) &&
(strtok(NULL, " ")) &&
(cp = strtok(NULL, " "))) {
rp = Strsave(str2short(cp));
}
}
}
for (res1 = res; *res1; res1++)
free(*res1);
#if 0
if (rp != NULL && rp[0] == '/' && rp[1] == '\0') {
xfree(rp);
rp = NULL;
}
#endif
return rp;
}
#endif
return NULL;
}
Char *
gettilde(const Char *us)
{
struct tildecache *bp1, *bp2, *bp;
Char *hd;
if (*us == '+' || *us == '-')
return NULL;
if (tcache == NULL)
tcache = xmalloc(TILINCR * sizeof(struct tildecache));
for (bp1 = tcache, bp2 = tcache + tlength; bp1 < bp2;) {
int i;
bp = bp1 + ((bp2 - bp1) >> 1);
if ((i = *us - *bp->user) == 0 && (i = Strcmp(us, bp->user)) == 0)
return (bp->home);
if (i < 0)
bp2 = bp;
else
bp1 = bp + 1;
}
hd = gethomedir(us);
if (hd == NULL)
return NULL;
tcache[tlength].user = Strsave(us);
tcache[tlength].home = hd;
tcache[tlength++].hlen = Strlen(hd);
qsort(tcache, tlength, sizeof(struct tildecache), tildecompare);
if (tlength == tsize) {
tsize += TILINCR;
tcache = xrealloc(tcache, tsize * sizeof(struct tildecache));
}
return (hd);
}
Char *
getusername(Char **hm)
{
Char *h, *p;
size_t i, j;
if (hm == NULL) {
for (i = 0; i < tlength; i++) {
xfree(tcache[i].home);
xfree(tcache[i].user);
}
xfree(tcache);
tlength = 0;
tsize = TILINCR;
tcache = NULL;
return NULL;
}
p = *hm;
if (((h = varval(STRhome)) != STRNULL) &&
(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, tcache[i].home, (j = tcache[i].hlen)) == 0) &&
(p[j] == '/' || p[j] == '\0')) {
*hm = &p[j];
return tcache[i].user;
}
return NULL;
}
void
shlvl(int val)
{
char *cp;
if ((cp = getenv("SHLVL")) != NULL) {
if (loginsh)
val = 1;
else
val += atoi(cp);
if (val <= 0) {
if (adrof(STRshlvl) != NULL)
unsetv(STRshlvl);
Unsetenv(STRKSHLVL);
}
else {
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 {
setcopy(STRshlvl, STR1, VAR_READWRITE);
tsetenv(STRKSHLVL, STR1);
}
}
int
fixio(int fd, int e)
{
switch (e) {
case -1:
#ifdef EWOULDBLOCK
case EWOULDBLOCK:
# define FDRETRY
#endif
#if defined(POSIX) && defined(EAGAIN)
# if !defined(EWOULDBLOCK) || EWOULDBLOCK != EAGAIN
case EAGAIN:
# define FDRETRY
# endif
#endif
e = 0;
#ifdef FDRETRY
# ifdef F_SETFL
# ifndef O_NONBLOCK
# define O_NONBLOCK 0
# endif
# ifndef O_NDELAY
# define O_NDELAY 0
# endif
# ifndef FNBIO
# define FNBIO 0
# endif
# ifndef _FNBIO
# define _FNBIO 0
# endif
# ifndef FNONBIO
# define FNONBIO 0
# endif
# ifndef FNONBLOCK
# define FNONBLOCK 0
# endif
# ifndef _FNONBLOCK
# define _FNONBLOCK 0
# endif
# ifndef FNDELAY
# define FNDELAY 0
# endif
# ifndef _FNDELAY
# define _FNDELAY 0
# endif
# ifndef FNDLEAY
# define FNDLEAY 0
# endif
if ((e = fcntl(fd, F_GETFL, 0)) == -1)
return -1;
e &= ~(O_NDELAY|O_NONBLOCK|FNBIO|_FNBIO|FNONBIO|FNONBLOCK|_FNONBLOCK|
FNDELAY|_FNDELAY|FNDLEAY);
if (fcntl(fd, F_SETFL, e) == -1)
return -1;
else
e = 1;
# endif
# ifdef FIONBIO
e = 0;
if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1)
return -1;
else
e = 1;
# endif
#endif
return e ? 0 : -1;
case EINTR:
return 0;
default:
return -1;
}
}
int
collate(const Char *a, const Char *b)
{
int rv;
#ifdef SHORT_STRINGS
char *sa = strsave(short2str(a));
char *sb = strsave(short2str(b));
#else
char *sa = strip(strsave(a));
char *sb = strip(strsave(b));
#endif
#if defined(NLS) && defined(HAVE_STRCOLL)
errno = 0;
rv = strcoll(sa, sb);
if (errno == EINVAL) {
xfree(sa);
xfree(sb);
stderror(ERR_SYSTEM, "strcoll", strerror(errno));
}
#else
rv = strcmp(sa, sb);
#endif
xfree(sa);
xfree(sb);
return rv;
}
#ifdef HASHBANG
#define HACKBUFSZ 1024
int
hashbang(int fd, Char ***vp)
{
struct blk_buf sarg = BLK_BUF_INIT;
char lbuf[HACKBUFSZ], *p, *ws;
#ifdef WINNT_NATIVE
int fw = 0;
int first_word = 1;
char *real;
#endif
if (xread(fd, lbuf, HACKBUFSZ) <= 0)
return -1;
ws = 0;
for (p = lbuf; p < &lbuf[HACKBUFSZ]; ) {
switch (*p) {
case ' ':
case '\t':
#ifdef WINNT_NATIVE
case '\r':
#endif
if (ws) {
*p = '\0';
#ifdef WINNT_NATIVE
if (first_word) {
real = hb_subst(ws);
if (real != NULL)
ws = real;
}
fw = 1;
first_word = 0;
#endif
bb_append(&sarg, SAVE(ws));
ws = NULL;
}
p++;
continue;
case '\0':
goto err;
case '\n':
if (
#ifdef WINNT_NATIVE
fw ||
#endif
ws) {
*p = '\0';
#ifdef WINNT_NATIVE
if (first_word) {
real = hb_subst(ws);
if (real != NULL)
ws = real;
}
#endif
if (ws)
bb_append(&sarg, SAVE(ws));
}
if (sarg.len > 0) {
*vp = bb_finish(&sarg);
return 0;
}
else
goto err;
default:
if (!ws)
ws = p;
p++;
break;
}
}
err:
bb_cleanup(&sarg);
return -1;
}
#endif
#ifdef REMOTEHOST
static void
palarm(int snum)
{
USE(snum);
_exit(1);
}
static void
getremotehost(int dest_fd)
{
const char *host = NULL;
#ifdef INET6
struct sockaddr_storage saddr;
static char hbuf[NI_MAXHOST];
#else
struct hostent* hp;
struct sockaddr_in saddr;
#endif
socklen_t len = sizeof(saddr);
#ifdef INET6
if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
(saddr.ss_family == AF_INET6 || saddr.ss_family == AF_INET)) {
int flag = NI_NUMERICHOST;
#ifdef NI_WITHSCOPEID
flag |= NI_WITHSCOPEID;
#endif
getnameinfo((struct sockaddr *)&saddr, len, hbuf, sizeof(hbuf),
NULL, 0, flag);
host = hbuf;
#else
if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
saddr.sin_family == AF_INET) {
#if 0
if ((hp = gethostbyaddr((char *)&saddr.sin_addr, sizeof(struct in_addr),
AF_INET)) != NULL)
host = hp->h_name;
else
#endif
host = inet_ntoa(saddr.sin_addr);
#endif
}
#ifdef HAVE_STRUCT_UTMP_UT_HOST
else {
char *ptr;
char *name = utmphost();
if (name != NULL && *name != '\0' && *name != ':') {
struct in_addr addr;
char *sptr;
if ((sptr = strchr(name, ':')) != NULL)
*sptr = '\0';
addr.s_addr = inet_addr(name);
if (addr.s_addr != (unsigned int)~0)
host = name;
else {
if (sptr != name) {
#ifdef INET6
char *s, *domain;
char dbuf[MAXHOSTNAMELEN];
struct addrinfo hints, *res = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE | AI_CANONNAME;
if (strlen(name) < utmphostsize())
{
if (getaddrinfo(name, NULL, &hints, &res) != 0)
res = NULL;
} else if (gethostname(dbuf, sizeof(dbuf)) == 0 &&
(dbuf[sizeof(dbuf)-1] = '\0',
(domain = strchr(dbuf, '.')) != NULL)) {
for (s = strchr(name, '.');
s != NULL; s = strchr(s + 1, '.')) {
if (*(s + 1) != '\0' &&
(ptr = strstr(domain, s)) != NULL) {
char *cbuf;
cbuf = strspl(name, ptr);
if (getaddrinfo(cbuf, NULL, &hints, &res) != 0)
res = NULL;
xfree(cbuf);
break;
}
}
}
if (res != NULL) {
if (res->ai_canonname != NULL) {
strncpy(hbuf, res->ai_canonname, sizeof(hbuf));
host = hbuf;
}
freeaddrinfo(res);
}
#else
if ((hp = gethostbyname(name)) == NULL) {
if ((ptr = strchr(name, '.')) != NULL) {
*ptr = '\0';
if ((hp = gethostbyname(name)) != NULL)
host = hp->h_name;
*ptr = '.';
}
}
else
host = hp->h_name;
#endif
}
}
}
}
#endif
if (host) {
size_t left;
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);
}
void
remotehost(void)
{
struct sigaction sa;
struct strbuf hostname = strbuf_INIT;
int fds[2], wait_options, status;
pid_t pid, wait_res;
sa.sa_handler = SIG_DFL;
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]);
signal(SIGALRM, palarm);
sigemptyset(&set);
sigaddset(&set, SIGALRM);
(void)sigprocmask(SIG_UNBLOCK, &set, NULL);
(void)alarm(2);
getremotehost(fds[1]);
}
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
fix_yp_bugs();
#endif
}
#endif
#ifndef WINNT_NATIVE
void
dotermname(Char **v, struct command *c)
{
char *termtype;
char termcap_buffer[2048];
USE(c);
termtype = (v[1] == NULL ? getenv("TERM") : short2str(v[1]));
if (termtype == NULL) {
setcopy(STRstatus, STR1, VAR_READWRITE);
return;
}
if (tgetent(termcap_buffer, termtype) == 1) {
xprintf("%s\n", termtype);
setcopy(STRstatus, STR0, VAR_READWRITE);
} else
setcopy(STRstatus, STR1, VAR_READWRITE);
}
#endif