sh: Make parseredir() a proper function instead of an emulated nested

function.
This commit is contained in:
Jilles Tjoelker 2014-10-15 21:26:09 +00:00
parent 622fdf3236
commit 3f9b4e9ab8
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=273153

View File

@ -976,6 +976,63 @@ checkend(int c, const char *eofmark, int striptabs)
}
/*
* Parse a redirection operator. The variable "out" points to a string
* specifying the fd to be redirected. The variable "c" contains the
* first character of the redirection operator.
*/
static void
parseredir(char *out, int c)
{
char fd = *out;
union node *np;
np = (union node *)stalloc(sizeof (struct nfile));
if (c == '>') {
np->nfile.fd = 1;
c = pgetc();
if (c == '>')
np->type = NAPPEND;
else if (c == '&')
np->type = NTOFD;
else if (c == '|')
np->type = NCLOBBER;
else {
np->type = NTO;
pungetc();
}
} else { /* c == '<' */
np->nfile.fd = 0;
c = pgetc();
if (c == '<') {
if (sizeof (struct nfile) != sizeof (struct nhere)) {
np = (union node *)stalloc(sizeof (struct nhere));
np->nfile.fd = 0;
}
np->type = NHERE;
heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
heredoc->here = np;
if ((c = pgetc()) == '-') {
heredoc->striptabs = 1;
} else {
heredoc->striptabs = 0;
pungetc();
}
} else if (c == '&')
np->type = NFROMFD;
else if (c == '>')
np->type = NFROMTO;
else {
np->type = NFROM;
pungetc();
}
}
if (fd != '\0')
np->nfile.fd = digit_val(fd);
redirnode = np;
}
/*
* Called to parse command substitutions.
*/
@ -1306,7 +1363,6 @@ readcstyleesc(char *out)
* will run code that appears at the end of readtoken1.
*/
#define PARSEREDIR() {goto parseredir; parseredir_return:;}
#define PARSESUB() {goto parsesub; parsesub_return:;}
#define PARSEARITH() {goto parsearith; parsearith_return:;}
@ -1506,7 +1562,7 @@ readtoken1(int firstc, char const *initialsyntax, const char *eofmark,
&& quotef == 0
&& len <= 2
&& (*out == '\0' || is_digit(*out))) {
PARSEREDIR();
parseredir(out, c);
return lasttoken = TREDIR;
} else {
pungetc();
@ -1520,63 +1576,6 @@ readtoken1(int firstc, char const *initialsyntax, const char *eofmark,
/* end of readtoken routine */
/*
* Parse a redirection operator. The variable "out" points to a string
* specifying the fd to be redirected. The variable "c" contains the
* first character of the redirection operator.
*/
parseredir: {
char fd = *out;
union node *np;
np = (union node *)stalloc(sizeof (struct nfile));
if (c == '>') {
np->nfile.fd = 1;
c = pgetc();
if (c == '>')
np->type = NAPPEND;
else if (c == '&')
np->type = NTOFD;
else if (c == '|')
np->type = NCLOBBER;
else {
np->type = NTO;
pungetc();
}
} else { /* c == '<' */
np->nfile.fd = 0;
c = pgetc();
if (c == '<') {
if (sizeof (struct nfile) != sizeof (struct nhere)) {
np = (union node *)stalloc(sizeof (struct nhere));
np->nfile.fd = 0;
}
np->type = NHERE;
heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
heredoc->here = np;
if ((c = pgetc()) == '-') {
heredoc->striptabs = 1;
} else {
heredoc->striptabs = 0;
pungetc();
}
} else if (c == '&')
np->type = NFROMFD;
else if (c == '>')
np->type = NFROMTO;
else {
np->type = NFROM;
pungetc();
}
}
if (fd != '\0')
np->nfile.fd = digit_val(fd);
redirnode = np;
goto parseredir_return;
}
/*
* Parse a substitution. At this point, we have read the dollar sign
* and nothing else.