/*
* ash shell port for busybox
*
+ * This code is derived from software contributed to Berkeley by
+ * Kenneth Almquist.
+ *
+ * Original BSD copyright notice is retained at the end of this file.
+ *
* Copyright (c) 1989, 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
* was re-ported from NetBSD and debianized.
*
- * This code is derived from software contributed to Berkeley by
- * Kenneth Almquist.
- *
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
- *
- * Original BSD copyright notice is retained at the end of this file.
*/
/*
#define PROFILE 0
-#define IFS_BROKEN
-
#define JOBS ENABLE_ASH_JOB_CONTROL
#if DEBUG
#include <paths.h>
#include <setjmp.h>
#include <fnmatch.h>
+
+#include "shell_common.h"
+#include "builtin_read.h"
#include "math.h"
#if ENABLE_ASH_RANDOM_SUPPORT
# include "random.h"
"b" "notify",
"u" "nounset",
"\0" "vi"
+#if ENABLE_ASH_BASH_COMPAT
+ ,"\0" "pipefail"
+#endif
#if DEBUG
,"\0" "nolog"
,"\0" "debug"
#endif
};
-#define optletters(n) optletters_optnames[(n)][0]
-#define optnames(n) (&optletters_optnames[(n)][1])
+#define optletters(n) optletters_optnames[n][0]
+#define optnames(n) (optletters_optnames[n] + 1)
enum { NOPTS = ARRAY_SIZE(optletters_optnames) };
#define bflag optlist[11]
#define uflag optlist[12]
#define viflag optlist[13]
+#if ENABLE_ASH_BASH_COMPAT
+# define pipefail optlist[14]
+#else
+# define pipefail 0
+#endif
#if DEBUG
-#define nolog optlist[14]
-#define debug optlist[15]
+# define nolog optlist[14 + ENABLE_ASH_BASH_COMPAT]
+# define debug optlist[15 + ENABLE_ASH_BASH_COMPAT]
#endif
/* trap handler commands */
/* ============ Parser structures */
/* control characters in argument strings */
+#define CTL_FIRST CTLESC
#define CTLESC ((unsigned char)'\201') /* escape next character */
#define CTLVAR ((unsigned char)'\202') /* variable defn */
#define CTLENDVAR ((unsigned char)'\203')
#define CTLARI ((unsigned char)'\206') /* arithmetic expression */
#define CTLENDARI ((unsigned char)'\207')
#define CTLQUOTEMARK ((unsigned char)'\210')
+#define CTL_LAST CTLQUOTEMARK
/* variable substitution byte (follows CTLVAR) */
#define VSTYPE 0x0f /* type of variable substitution */
# define VDYNAMIC 0
#endif
-#ifdef IFS_BROKEN
-static const char defifsvar[] ALIGN1 = "IFS= \t\n";
-#define defifs (defifsvar + 4)
-#else
-static const char defifs[] ALIGN1 = " \t\n";
-#endif
-
/* Need to be before varinit_data[] */
#if ENABLE_LOCALE_SUPPORT
const char *text;
void (*func)(const char *) FAST_FUNC;
} varinit_data[] = {
-#ifdef IFS_BROKEN
+#if IFS_BROKEN
{ VSTRFIXED|VTEXTFIXED , defifsvar , NULL },
#else
{ VSTRFIXED|VTEXTFIXED|VUNSET, "IFS\0" , NULL },
#define CSPCL 13 /* these terminate a word */
#define CIGN 14 /* character should be ignored */
+#define PEOF 256
#if ENABLE_ASH_ALIAS
-# define PEOA 256
-# define PEOF 257
-#else
-# define PEOF 256
+# define PEOA 257
#endif
-/* number syntax index */
-#define BASESYNTAX 0 /* not in quotes */
-#define DQSYNTAX 1 /* in double quotes */
-#define SQSYNTAX 2 /* in single quotes */
-#define ARISYNTAX 3 /* in arithmetic */
-#define PSSYNTAX 4 /* prompt */
-
-#if ENABLE_ASH_OPTIMIZE_FOR_SIZE
-# define USE_SIT_FUNCTION
-#endif
+#define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE
#if ENABLE_SH_MATH_SUPPORT
# define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12))
SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */
SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */
SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */
-#ifndef USE_SIT_FUNCTION
+#if !USE_SIT_FUNCTION
SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */
SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */
SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */
/* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF,
* caller must ensure proper cast on it if c is *char_ptr!
*/
+/* Values for syntax param */
+#define BASESYNTAX 0 /* not in quotes */
+#define DQSYNTAX 1 /* in double quotes */
+#define SQSYNTAX 2 /* in single quotes */
+#define ARISYNTAX 3 /* in arithmetic */
+#define PSSYNTAX 4 /* prompt. never passed to SIT() */
-#ifdef USE_SIT_FUNCTION
+#if USE_SIT_FUNCTION
static int
SIT(int c, int syntax)
const char *s;
int indx;
- if (c == PEOF) { /* 2^8+2 */
+ if (c == PEOF)
return CENDFILE;
- }
# if ENABLE_ASH_ALIAS
- if (c == PEOA) { /* 2^8+1 */
+ if (c == PEOA)
indx = 0;
- } else
+ else
# endif
{
- if ((unsigned char)c >= CTLESC
- && (unsigned char)c <= CTLQUOTEMARK
+ /* Cast is purely for paranoia here,
+ * just in case someone passed signed char to us */
+ if ((unsigned char)c >= CTL_FIRST
+ && (unsigned char)c <= CTL_LAST
) {
return CCTL;
}
s = strchrnul(spec_symbls, c);
- if (*s == '\0') {
+ if (*s == '\0')
return CWORD;
- }
indx = syntax_index_table[s - spec_symbls];
}
return (S_I_T[indx] >> (syntax*4)) & 0xf;
/* 253 */ CWORD_CWORD_CWORD_CWORD,
/* 254 */ CWORD_CWORD_CWORD_CWORD,
/* 255 */ CWORD_CWORD_CWORD_CWORD,
+ /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE,
# if ENABLE_ASH_ALIAS
/* PEOA */ CSPCL_CIGN_CIGN_CIGN,
# endif
- /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE,
};
-# define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> (syntax*4)) & 0xf)
+# define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf)
#endif /* !USE_SIT_FUNCTION */
/* ============ jobs.c */
/* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */
-#define FORK_FG 0
-#define FORK_BG 1
+#define FORK_FG 0
+#define FORK_BG 1
#define FORK_NOJOB 2
/* mode flags for showjob(s) */
* array of pids.
*/
struct procstat {
- pid_t pid; /* process id */
- int status; /* last process status from wait() */
- char *cmd; /* text of command being run */
+ pid_t ps_pid; /* process id */
+ int ps_status; /* last process status from wait() */
+ char *ps_cmd; /* text of command being run */
};
struct job {
found = NULL;
while (jp) {
- if (match(jp->ps[0].cmd, p)) {
+ if (match(jp->ps[0].ps_cmd, p)) {
if (found)
goto err;
found = jp;
INT_OFF;
for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) {
- if (ps->cmd != nullstr)
- free(ps->cmd);
+ if (ps->ps_cmd != nullstr)
+ free(ps->ps_cmd);
}
if (jp->ps != &jp->ps0)
free(jp->ps);
do {
if (argv[i][0] == '%') {
struct job *jp = getjob(argv[i], 0);
- unsigned pid = jp->ps[0].pid;
+ unsigned pid = jp->ps[0].ps_pid;
/* Enough space for ' -NNN<nul>' */
argv[i] = alloca(sizeof(int)*3 + 3);
/* kill_main has matching code to expect
}
static void
-showpipe(struct job *jp, FILE *out)
+showpipe(struct job *jp /*, FILE *out*/)
{
- struct procstat *sp;
- struct procstat *spend;
+ struct procstat *ps;
+ struct procstat *psend;
- spend = jp->ps + jp->nprocs;
- for (sp = jp->ps + 1; sp < spend; sp++)
- fprintf(out, " | %s", sp->cmd);
- outcslow('\n', out);
+ psend = jp->ps + jp->nprocs;
+ for (ps = jp->ps + 1; ps < psend; ps++)
+ printf(" | %s", ps->ps_cmd);
+ outcslow('\n', stdout);
flush_stdout_stderr();
}
if (jp->state == JOBDONE)
goto out;
jp->state = JOBRUNNING;
- pgid = jp->ps->pid;
+ pgid = jp->ps[0].ps_pid;
if (mode == FORK_FG)
xtcsetpgrp(ttyfd, pgid);
killpg(pgid, SIGCONT);
ps = jp->ps;
i = jp->nprocs;
do {
- if (WIFSTOPPED(ps->status)) {
- ps->status = -1;
+ if (WIFSTOPPED(ps->ps_status)) {
+ ps->ps_status = -1;
}
ps++;
} while (--i);
fg_bgcmd(int argc UNUSED_PARAM, char **argv)
{
struct job *jp;
- FILE *out;
int mode;
int retval;
mode = (**argv == 'f') ? FORK_FG : FORK_BG;
nextopt(nullstr);
argv = argptr;
- out = stdout;
do {
jp = getjob(*argv, 1);
if (mode == FORK_BG) {
set_curjob(jp, CUR_RUNNING);
- fprintf(out, "[%d] ", jobno(jp));
+ printf("[%d] ", jobno(jp));
}
- outstr(jp->ps->cmd, out);
- showpipe(jp, out);
+ out1str(jp->ps[0].ps_cmd);
+ showpipe(jp /*, stdout*/);
retval = restartjob(jp, mode);
} while (*argv && *++argv);
return retval;
/* Do a wait system call. If job control is compiled in, we accept
* stopped processes. wait_flags may have WNOHANG, preventing blocking.
* NB: _not_ safe_waitpid, we need to detect EINTR */
- pid = waitpid(-1, &status,
- (doing_jobctl ? (wait_flags | WUNTRACED) : wait_flags));
+ if (doing_jobctl)
+ wait_flags |= WUNTRACED;
+ pid = waitpid(-1, &status, wait_flags);
TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n",
pid, status, errno, strerror(errno)));
if (pid <= 0)
INT_OFF;
thisjob = NULL;
for (jp = curjob; jp; jp = jp->prev_job) {
- struct procstat *sp;
- struct procstat *spend;
+ struct procstat *ps;
+ struct procstat *psend;
if (jp->state == JOBDONE)
continue;
state = JOBDONE;
- spend = jp->ps + jp->nprocs;
- sp = jp->ps;
+ ps = jp->ps;
+ psend = ps + jp->nprocs;
do {
- if (sp->pid == pid) {
+ if (ps->ps_pid == pid) {
TRACE(("Job %d: changing status of proc %d "
"from 0x%x to 0x%x\n",
- jobno(jp), pid, sp->status, status));
- sp->status = status;
+ jobno(jp), pid, ps->ps_status, status));
+ ps->ps_status = status;
thisjob = jp;
}
- if (sp->status == -1)
+ if (ps->ps_status == -1)
state = JOBRUNNING;
#if JOBS
if (state == JOBRUNNING)
continue;
- if (WIFSTOPPED(sp->status)) {
- jp->stopstatus = sp->status;
+ if (WIFSTOPPED(ps->ps_status)) {
+ jp->stopstatus = ps->ps_status;
state = JOBSTOPPED;
}
#endif
- } while (++sp < spend);
+ } while (++ps < psend);
if (thisjob)
goto gotjob;
}
if (mode & SHOW_ONLY_PGID) { /* jobs -p */
/* just output process (group) id of pipeline */
- fprintf(out, "%d\n", ps->pid);
+ fprintf(out, "%d\n", ps->ps_pid);
return;
}
s[col - 3] = '-';
if (mode & SHOW_PIDS)
- col += fmtstr(s + col, 16, "%d ", ps->pid);
+ col += fmtstr(s + col, 16, "%d ", ps->ps_pid);
psend = ps + jp->nprocs;
strcpy(s + col, "Running");
col += sizeof("Running") - 1;
} else {
- int status = psend[-1].status;
+ int status = psend[-1].ps_status;
if (jp->state == JOBSTOPPED)
status = jp->stopstatus;
col += sprint_status(s + col, status, 0);
* making it impossible to know 1st process status.
*/
goto start;
- while (1) {
+ do {
/* for each process */
s[0] = '\0';
col = 33;
if (mode & SHOW_PIDS)
- col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->pid) - 1;
+ col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1;
start:
- fprintf(out, "%s%*c", s, 33 - col >= 0 ? 33 - col : 0, ' ');
- if (ps != jp->ps)
- fprintf(out, "| ");
- fprintf(out, "%s", ps->cmd);
- if (++ps == psend)
- break;
- }
+ fprintf(out, "%s%*c%s%s",
+ s,
+ 33 - col >= 0 ? 33 - col : 0, ' ',
+ ps == jp->ps ? "" : "| ",
+ ps->ps_cmd
+ );
+ } while (++ps != psend);
outcslow('\n', out);
jp->changed = 0;
do
showjob(stdout, getjob(*argv, 0), mode);
while (*++argv);
- } else
+ } else {
showjobs(stdout, mode);
+ }
return 0;
}
#endif /* JOBS */
+/* Called only on finished or stopped jobs (no members are running) */
static int
getstatus(struct job *job)
{
int status;
int retval;
+ struct procstat *ps;
+
+ /* Fetch last member's status */
+ ps = job->ps + job->nprocs - 1;
+ status = ps->ps_status;
+ if (pipefail) {
+ /* "set -o pipefail" mode: use last _nonzero_ status */
+ while (status == 0 && --ps >= job->ps)
+ status = ps->ps_status;
+ }
- status = job->ps[job->nprocs - 1].status;
retval = WEXITSTATUS(status);
if (!WIFEXITED(status)) {
#if JOBS
while (1) {
if (!job)
goto repeat;
- if (job->ps[job->nprocs - 1].pid == pid)
+ if (job->ps[job->nprocs - 1].ps_pid == pid)
break;
job = job->prev_job;
}
};
const char *p, *str;
- char cc[2] = " ";
+ char cc[2];
char *nextc;
unsigned char c;
unsigned char subtype = 0;
int quoted = 0;
+ cc[1] = '\0';
nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc);
p = s;
- while ((c = *p++) != 0) {
+ while ((c = *p++) != '\0') {
str = NULL;
switch (c) {
case CTLESC:
while ((c = *str++) != '\0') {
USTPUTC(c, nextc);
}
- }
+ } /* while *p++ not NUL */
+
if (quoted & 1) {
USTPUTC('"', nextc);
}
if (jp->nprocs == 0)
pgrp = getpid();
else
- pgrp = jp->ps[0].pid;
+ pgrp = jp->ps[0].ps_pid;
/* this can fail because we are doing it in the parent also */
setpgid(0, pgrp);
if (mode == FORK_FG)
if (jp->nprocs == 0)
pgrp = pid;
else
- pgrp = jp->ps[0].pid;
+ pgrp = jp->ps[0].ps_pid;
/* This can fail because we are doing it in the child also */
setpgid(pid, pgrp);
}
}
if (jp) {
struct procstat *ps = &jp->ps[jp->nprocs++];
- ps->pid = pid;
- ps->status = -1;
- ps->cmd = nullstr;
+ ps->ps_pid = pid;
+ ps->ps_status = -1;
+ ps->ps_cmd = nullstr;
#if JOBS
if (doing_jobctl && n)
- ps->cmd = commandtext(n);
+ ps->ps_cmd = commandtext(n);
#endif
}
}
do {
sv_pos++;
#if ENABLE_ASH_BASH_COMPAT
- if (redir->nfile.type == NTO2)
+ if (tmp->nfile.type == NTO2)
sv_pos++;
#endif
tmp = tmp->nfile.next;
static NOINLINE ssize_t
varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
{
- int num;
const char *p;
+ int num;
int i;
- int sep = 0;
int sepq = 0;
ssize_t len = 0;
- char **ap;
- int syntax;
- int quoted = varflags & VSQUOTE;
int subtype = varflags & VSTYPE;
int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR);
+ int quoted = varflags & VSQUOTE;
+ int syntax = quoted ? DQSYNTAX : BASESYNTAX;
- if (quoted && (flags & EXP_FULL))
- sep = 1 << CHAR_BIT;
-
- syntax = quoted ? DQSYNTAX : BASESYNTAX;
switch (*name) {
case '$':
num = rootpid;
raise_error_syntax("bad substitution");
#endif
break;
- case '@':
- if (sep)
+ case '@': {
+ char **ap;
+ int sep;
+
+ if (quoted && (flags & EXP_FULL)) {
+ /* note: this is not meant as PEOF value */
+ sep = 1 << CHAR_BIT;
goto param;
+ }
/* fall through */
case '*':
sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' ';
- if (quotes && (SIT(sep, syntax) == CCTL || SIT(sep, syntax) == CBACK))
+ i = SIT(sep, syntax);
+ if (quotes && (i == CCTL || i == CBACK))
sepq = 1;
param:
ap = shellparam.p;
q = expdest;
if (sepq)
STPUTC(CTLESC, q);
+ /* note: may put NUL despite sep != 0
+ * (see sep = 1 << CHAR_BIT above) */
STPUTC(sep, q);
expdest = q;
}
}
return len;
+ } /* case '@' and '*' */
case '0':
case '1':
case '2':
break;
eq++;
if (name_len == (unsigned)(eq - str)
- && strncmp(str, name, name_len) == 0) {
+ && strncmp(str, name, name_len) == 0
+ ) {
p = eq;
/* goto value; - WRONG! */
/* think "A=1 A=2 B=$A" */
return 127;
}
out:
- outstr("\n", stdout);
+ out1str("\n");
return 0;
}
}
#if ENABLE_ASH_OPTIMIZE_FOR_SIZE
-#define pgetc_fast() pgetc()
+# define pgetc_fast() pgetc()
#else
-#define pgetc_fast() pgetc_as_macro()
+# define pgetc_fast() pgetc_as_macro()
#endif
-/*
- * Same as pgetc(), but ignores PEOA.
- */
#if ENABLE_ASH_ALIAS
static int
-pgetc2(void)
+pgetc_without_PEOA(void)
{
int c;
do {
return c;
}
#else
-#define pgetc2() pgetc()
+# define pgetc_without_PEOA() pgetc()
#endif
/*
int c;
while (--nleft > 0) {
- c = pgetc2();
+ c = pgetc_without_PEOA();
if (c == PEOF) {
if (p == line)
return NULL;
USTPUTC(c, out);
break;
case CBACK: /* backslash */
- c = pgetc2();
+ c = pgetc_without_PEOA();
if (c == PEOF) {
USTPUTC(CTLESC, out);
USTPUTC('\\', out);
#endif
goto endword; /* exit outer loop */
}
-#if ENABLE_ASH_ALIAS
- if (c != PEOA)
-#endif
+ IF_ASH_ALIAS(if (c != PEOA))
USTPUTC(c, out);
}
checkend: {
if (eofmark) {
#if ENABLE_ASH_ALIAS
- if (c == PEOA) {
- c = pgetc2();
- }
+ if (c == PEOA)
+ c = pgetc_without_PEOA();
#endif
if (striptabs) {
while (c == '\t') {
- c = pgetc2();
+ c = pgetc_without_PEOA();
}
}
if (c == *eofmark) {
/* fall through */
case PEOF:
-#if ENABLE_ASH_ALIAS
- case PEOA:
-#endif
+ IF_ASH_ALIAS(case PEOA:)
startlinno = g_parsefile->linno;
raise_error_syntax("EOF in backquote substitution");
c = pgetc_fast();
switch (c) {
case ' ': case '\t':
-#if ENABLE_ASH_ALIAS
- case PEOA:
-#endif
+ IF_ASH_ALIAS(case PEOA:)
continue;
case '#':
while ((c = pgetc()) != '\n' && c != PEOF)
}
/*
- * Read and execute commands. "Top" is nonzero for the top level command
- * loop; it turns on prompting if the shell is interactive.
+ * Read and execute commands.
+ * "Top" is nonzero for the top level command loop;
+ * it turns on prompting if the shell is interactive.
*/
static int
cmdloop(int top)
static int FAST_FUNC
readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
{
- static const char *const arg_REPLY[] = { "REPLY", NULL };
-
- char **ap;
- int backslash;
- char c;
- int rflag;
- char *prompt;
- const char *ifs;
- char *p;
- int startword;
- int status;
+ char *opt_n = NULL;
+ char *opt_p = NULL;
+ char *opt_t = NULL;
+ char *opt_u = NULL;
+ int read_flags = 0;
+ const char *r;
int i;
- int fd = 0;
-#if ENABLE_ASH_READ_NCHARS
- int nchars = 0; /* if != 0, -n is in effect */
- int silent = 0;
- struct termios tty, old_tty;
-#endif
-#if ENABLE_ASH_READ_TIMEOUT
- unsigned end_ms = 0;
- unsigned timeout = 0;
-#endif
-
- rflag = 0;
- prompt = NULL;
- while ((i = nextopt("p:u:r"
- IF_ASH_READ_TIMEOUT("t:")
- IF_ASH_READ_NCHARS("n:s")
- )) != '\0') {
+
+ while ((i = nextopt("p:u:rt:n:s")) != '\0') {
switch (i) {
case 'p':
- prompt = optionarg;
+ opt_p = optionarg;
break;
-#if ENABLE_ASH_READ_NCHARS
case 'n':
- nchars = bb_strtou(optionarg, NULL, 10);
- if (nchars < 0 || errno)
- ash_msg_and_raise_error("invalid count");
- /* nchars == 0: off (bash 3.2 does this too) */
+ opt_n = optionarg;
break;
case 's':
- silent = 1;
+ read_flags |= BUILTIN_READ_SILENT;
break;
-#endif
-#if ENABLE_ASH_READ_TIMEOUT
case 't':
- timeout = bb_strtou(optionarg, NULL, 10);
- if (errno || timeout > UINT_MAX / 2048)
- ash_msg_and_raise_error("invalid timeout");
- timeout *= 1000;
-#if 0 /* even bash have no -t N.NNN support */
- ts.tv_sec = bb_strtou(optionarg, &p, 10);
- ts.tv_usec = 0;
- /* EINVAL means number is ok, but not terminated by NUL */
- if (*p == '.' && errno == EINVAL) {
- char *p2;
- if (*++p) {
- int scale;
- ts.tv_usec = bb_strtou(p, &p2, 10);
- if (errno)
- ash_msg_and_raise_error("invalid timeout");
- scale = p2 - p;
- /* normalize to usec */
- if (scale > 6)
- ash_msg_and_raise_error("invalid timeout");
- while (scale++ < 6)
- ts.tv_usec *= 10;
- }
- } else if (ts.tv_sec < 0 || errno) {
- ash_msg_and_raise_error("invalid timeout");
- }
- if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
- ash_msg_and_raise_error("invalid timeout");
- }
-#endif /* if 0 */
+ opt_t = optionarg;
break;
-#endif
case 'r':
- rflag = 1;
+ read_flags |= BUILTIN_READ_RAW;
break;
case 'u':
- fd = bb_strtou(optionarg, NULL, 10);
- if (fd < 0 || errno)
- ash_msg_and_raise_error("invalid file descriptor");
+ opt_u = optionarg;
break;
default:
break;
}
}
- if (prompt && isatty(fd)) {
- out2str(prompt);
- }
- ap = argptr;
- if (*ap == NULL)
- ap = (char**)arg_REPLY;
- ifs = bltinlookup("IFS");
- if (ifs == NULL)
- ifs = defifs;
-#if ENABLE_ASH_READ_NCHARS
- tcgetattr(fd, &tty);
- old_tty = tty;
- if (nchars || silent) {
- if (nchars) {
- tty.c_lflag &= ~ICANON;
- tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
- }
- if (silent) {
- tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
- }
- /* if tcgetattr failed, tcsetattr will fail too.
- * Ignoring, it's harmless. */
- tcsetattr(fd, TCSANOW, &tty);
- }
-#endif
- status = 0;
- startword = 2;
- backslash = 0;
-#if ENABLE_ASH_READ_TIMEOUT
- if (timeout) /* NB: ensuring end_ms is nonzero */
- end_ms = ((unsigned)(monotonic_us() / 1000) + timeout) | 1;
-#endif
- STARTSTACKSTR(p);
- do {
- const char *is_ifs;
-
-#if ENABLE_ASH_READ_TIMEOUT
- if (end_ms) {
- struct pollfd pfd[1];
- pfd[0].fd = fd;
- pfd[0].events = POLLIN;
- timeout = end_ms - (unsigned)(monotonic_us() / 1000);
- if ((int)timeout <= 0 /* already late? */
- || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
- ) { /* timed out! */
-#if ENABLE_ASH_READ_NCHARS
- tcsetattr(fd, TCSANOW, &old_tty);
-#endif
- return 1;
- }
- }
-#endif
- if (nonblock_safe_read(fd, &c, 1) != 1) {
- status = 1;
- break;
- }
- if (c == '\0')
- continue;
- if (backslash) {
- backslash = 0;
- if (c != '\n')
- goto put;
- continue;
- }
- if (!rflag && c == '\\') {
- backslash = 1;
- continue;
- }
- if (c == '\n')
- break;
- /* $IFS splitting */
-/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
- is_ifs = strchr(ifs, c);
- if (startword && is_ifs) {
- if (isspace(c))
- continue;
- /* it is a non-space ifs char */
- startword--;
- if (startword == 1) /* first one? */
- continue; /* yes, it is not next word yet */
- }
- startword = 0;
- if (ap[1] != NULL && is_ifs) {
- const char *beg;
- STACKSTRNUL(p);
- beg = stackblock();
- setvar(*ap, beg, 0);
- ap++;
- /* can we skip one non-space ifs char? (2: yes) */
- startword = isspace(c) ? 2 : 1;
- STARTSTACKSTR(p);
- continue;
- }
- put:
- STPUTC(c, p);
- }
-/* end of do {} while: */
-#if ENABLE_ASH_READ_NCHARS
- while (--nchars);
-#else
- while (1);
-#endif
+ r = builtin_read(setvar,
+ argptr,
+ bltinlookup("IFS"), /* can be NULL */
+ read_flags,
+ opt_n,
+ opt_p,
+ opt_t,
+ opt_u
+ );
-#if ENABLE_ASH_READ_NCHARS
- tcsetattr(fd, TCSANOW, &old_tty);
-#endif
+ if ((uintptr_t)r > 1)
+ ash_msg_and_raise_error(r);
- STACKSTRNUL(p);
- /* Remove trailing space ifs chars */
- while ((char *)stackblock() <= --p && isspace(*p) && strchr(ifs, *p) != NULL)
- *p = '\0';
- setvar(*ap, stackblock(), 0);
- while (*++ap != NULL)
- setvar(*ap, nullstr, 0);
- return status;
+ return (uintptr_t)r;
}
static int FAST_FUNC
/* from trap.c: */
signal(SIGCHLD, SIG_DFL);
+ /* bash re-enables SIGHUP which is SIG_IGNed on entry.
+ * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$"
+ */
+ signal(SIGHUP, SIG_DFL);
/* from var.c: */
{