struct strpush {
struct strpush *prev; /* preceding string on stack */
- char *prevstring;
- int prevnleft;
+ char *prev_string;
+ int prev_left_in_line;
#if ENABLE_ASH_ALIAS
struct alias *ap; /* if push was associated with an alias */
#endif
struct parsefile *prev; /* preceding file on stack */
int linno; /* current line */
int fd; /* file descriptor (or -1 if string) */
- int nleft; /* number of chars left in this line */
- int lleft; /* number of chars left in this buffer */
- char *nextc; /* next char in buffer */
+ int left_in_line; /* number of chars left in this line */
+ int left_in_buffer; /* number of chars left in this buffer past the line */
+ char *next_to_pgetc; /* next char in buffer */
char *buf; /* input buffer */
struct strpush *strpush; /* for pushing strings at this level */
struct strpush basestrpush; /* so pushing one is fast */
INPUT_NOFILE_OK = 2,
};
-static int plinno = 1; /* input line number */
-/* number of characters left in input buffer */
-static int parsenleft; /* copy of parsefile->nleft */
-static int parselleft; /* copy of parsefile->lleft */
-/* next character in input buffer */
-static char *parsenextc; /* copy of parsefile->nextc */
-
static smallint checkkwd;
/* values of checkkwd variable */
#define CHKALIAS 0x1
#define CHKKWD 0x2
#define CHKNL 0x4
+/*
+ * Push a string back onto the input at this current parsefile level.
+ * We handle aliases this way.
+ */
+#if !ENABLE_ASH_ALIAS
+#define pushstring(s, ap) pushstring(s)
+#endif
+static void
+pushstring(char *s, struct alias *ap)
+{
+ struct strpush *sp;
+ int len;
+
+ len = strlen(s);
+ INT_OFF;
+ if (g_parsefile->strpush) {
+ sp = ckzalloc(sizeof(*sp));
+ sp->prev = g_parsefile->strpush;
+ } else {
+ sp = &(g_parsefile->basestrpush);
+ }
+ g_parsefile->strpush = sp;
+ sp->prev_string = g_parsefile->next_to_pgetc;
+ sp->prev_left_in_line = g_parsefile->left_in_line;
+#if ENABLE_ASH_ALIAS
+ sp->ap = ap;
+ if (ap) {
+ ap->flag |= ALIASINUSE;
+ sp->string = s;
+ }
+#endif
+ g_parsefile->next_to_pgetc = s;
+ g_parsefile->left_in_line = len;
+ INT_ON;
+}
+
static void
popstring(void)
{
INT_OFF;
#if ENABLE_ASH_ALIAS
if (sp->ap) {
- if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') {
+ if (g_parsefile->next_to_pgetc[-1] == ' '
+ || g_parsefile->next_to_pgetc[-1] == '\t'
+ ) {
checkkwd |= CHKALIAS;
}
if (sp->string != sp->ap->val) {
}
}
#endif
- parsenextc = sp->prevstring;
- parsenleft = sp->prevnleft;
+ g_parsefile->next_to_pgetc = sp->prev_string;
+ g_parsefile->left_in_line = sp->prev_left_in_line;
g_parsefile->strpush = sp->prev;
if (sp != &(g_parsefile->basestrpush))
free(sp);
{
int nr;
char *buf = g_parsefile->buf;
- parsenextc = buf;
+ g_parsefile->next_to_pgetc = buf;
#if ENABLE_FEATURE_EDITING
retry:
if (!iflag || g_parsefile->fd != STDIN_FILENO)
* Refill the input buffer and return the next input character:
*
* 1) If a string was pushed back on the input, pop it;
- * 2) If an EOF was pushed back (parsenleft < -BIGNUM) or we are reading
- * from a string so we can't refill the buffer, return EOF.
+ * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM)
+ * or we are reading from a string so we can't refill the buffer,
+ * return EOF.
* 3) If the is more stuff in this buffer, use it else call read to fill it.
* 4) Process input up to the next newline, deleting nul characters.
*/
while (g_parsefile->strpush) {
#if ENABLE_ASH_ALIAS
- if (parsenleft == -1 && g_parsefile->strpush->ap
- && parsenextc[-1] != ' ' && parsenextc[-1] != '\t'
+ if (g_parsefile->left_in_line == -1
+ && g_parsefile->strpush->ap
+ && g_parsefile->next_to_pgetc[-1] != ' '
+ && g_parsefile->next_to_pgetc[-1] != '\t'
) {
pgetc_debug("preadbuffer PEOA");
return PEOA;
#endif
popstring();
/* try "pgetc" now: */
- pgetc_debug("internal pgetc at %d:%p'%s'", parsenleft, parsenextc, parsenextc);
- if (--parsenleft >= 0)
- return signed_char2int(*parsenextc++);
- }
- /* on both branches above parsenleft < 0.
+ pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'",
+ g_parsefile->left_in_line,
+ g_parsefile->next_to_pgetc,
+ g_parsefile->next_to_pgetc);
+ if (--g_parsefile->left_in_line >= 0)
+ return (unsigned char)(*g_parsefile->next_to_pgetc++);
+ }
+ /* on both branches above g_parsefile->left_in_line < 0.
* "pgetc" needs refilling.
*/
/* -90 is -BIGNUM. Below we use -99 to mark "EOF on read",
- * pungetc() may decrement it a few times. -90 is enough.
+ * pungetc() may increment it a few times.
+ * Assuming it won't increment it to 0.
*/
- if (parsenleft < -90 || g_parsefile->buf == NULL) {
+ if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) {
pgetc_debug("preadbuffer PEOF1");
- /* even in failure keep them in lock step,
- * for correct pungetc. */
- parsenextc++;
+ /* even in failure keep left_in_line and next_to_pgetc
+ * in lock step, for correct multi-layer pungetc.
+ * left_in_line was decremented before preadbuffer(),
+ * must inc next_to_pgetc: */
+ g_parsefile->next_to_pgetc++;
return PEOF;
}
- more = parselleft;
+ more = g_parsefile->left_in_buffer;
if (more <= 0) {
flush_stdout_stderr();
again:
more = preadfd();
if (more <= 0) {
- parselleft = parsenleft = -99;
+ /* don't try reading again */
+ g_parsefile->left_in_line = -99;
pgetc_debug("preadbuffer PEOF2");
- parsenextc++;
+ g_parsefile->next_to_pgetc++;
return PEOF;
}
}
/* Find out where's the end of line.
- * Set parsenleft/parselleft acordingly.
+ * Set g_parsefile->left_in_line
+ * and g_parsefile->left_in_buffer acordingly.
* NUL chars are deleted.
*/
- q = parsenextc;
+ q = g_parsefile->next_to_pgetc;
for (;;) {
char c;
} else {
q++;
if (c == '\n') {
- parsenleft = q - parsenextc - 1;
+ g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
break;
}
}
if (more <= 0) {
- parsenleft = q - parsenextc - 1;
- if (parsenleft < 0)
+ g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
+ if (g_parsefile->left_in_line < 0)
goto again;
break;
}
}
- parselleft = more;
+ g_parsefile->left_in_buffer = more;
if (vflag) {
char save = *q;
*q = '\0';
- out2str(parsenextc);
+ out2str(g_parsefile->next_to_pgetc);
*q = save;
}
- pgetc_debug("preadbuffer at %d:%p'%s'", parsenleft, parsenextc, parsenextc);
- return signed_char2int(*parsenextc++);
+ pgetc_debug("preadbuffer at %d:%p'%s'",
+ g_parsefile->left_in_line,
+ g_parsefile->next_to_pgetc,
+ g_parsefile->next_to_pgetc);
+ return (unsigned char)(*g_parsefile->next_to_pgetc++);
}
-#define pgetc_as_macro() (--parsenleft >= 0 ? signed_char2int(*parsenextc++) : preadbuffer())
+#define pgetc_as_macro() \
+ (--g_parsefile->left_in_line >= 0 \
+ ? (unsigned char)(*g_parsefile->next_to_pgetc++) \
+ : preadbuffer() \
+ )
static int
pgetc(void)
{
- pgetc_debug("pgetc at %d:%p'%s'", parsenleft, parsenextc, parsenextc);
+ pgetc_debug("pgetc_fast at %d:%p'%s'",
+ g_parsefile->left_in_line,
+ g_parsefile->next_to_pgetc,
+ g_parsefile->next_to_pgetc);
return pgetc_as_macro();
}
{
int c;
do {
+ pgetc_debug("pgetc_fast at %d:%p'%s'",
+ g_parsefile->left_in_line,
+ g_parsefile->next_to_pgetc,
+ g_parsefile->next_to_pgetc);
c = pgetc_fast();
} while (c == PEOA);
return c;
static void
pungetc(void)
{
- parsenleft++;
- parsenextc--;
- pgetc_debug("pushed back to %d:%p'%s'", parsenleft, parsenextc, parsenextc);
-}
-
-/*
- * Push a string back onto the input at this current parsefile level.
- * We handle aliases this way.
- */
-#if !ENABLE_ASH_ALIAS
-#define pushstring(s, ap) pushstring(s)
-#endif
-static void
-pushstring(char *s, struct alias *ap)
-{
- struct strpush *sp;
- int len;
-
- len = strlen(s);
- INT_OFF;
- if (g_parsefile->strpush) {
- sp = ckzalloc(sizeof(*sp));
- sp->prev = g_parsefile->strpush;
- } else {
- sp = &(g_parsefile->basestrpush);
- }
- g_parsefile->strpush = sp;
- sp->prevstring = parsenextc;
- sp->prevnleft = parsenleft;
-#if ENABLE_ASH_ALIAS
- sp->ap = ap;
- if (ap) {
- ap->flag |= ALIASINUSE;
- sp->string = s;
- }
-#endif
- parsenextc = s;
- parsenleft = len;
- INT_ON;
+ g_parsefile->left_in_line++;
+ g_parsefile->next_to_pgetc--;
+ pgetc_debug("pushed back to %d:%p'%s'",
+ g_parsefile->left_in_line,
+ g_parsefile->next_to_pgetc,
+ g_parsefile->next_to_pgetc);
}
/*
{
struct parsefile *pf;
- g_parsefile->nleft = parsenleft;
- g_parsefile->lleft = parselleft;
- g_parsefile->nextc = parsenextc;
- g_parsefile->linno = plinno;
pf = ckzalloc(sizeof(*pf));
pf->prev = g_parsefile;
pf->fd = -1;
popstring();
g_parsefile = pf->prev;
free(pf);
- parsenleft = g_parsefile->nleft;
- parselleft = g_parsefile->lleft;
- parsenextc = g_parsefile->nextc;
- plinno = g_parsefile->linno;
INT_ON;
}
g_parsefile->fd = fd;
if (g_parsefile->buf == NULL)
g_parsefile->buf = ckmalloc(IBUFSIZ);
- parselleft = parsenleft = 0;
- plinno = 1;
+ g_parsefile->left_in_buffer = 0;
+ g_parsefile->left_in_line = 0;
+ g_parsefile->linno = 1;
}
/*
{
INT_OFF;
pushfile();
- parsenextc = string;
- parsenleft = strlen(string);
+ g_parsefile->next_to_pgetc = string;
+ g_parsefile->left_in_line = strlen(string);
g_parsefile->buf = NULL;
- plinno = 1;
+ g_parsefile->linno = 1;
INT_ON;
}
(void) &prevsyntax;
(void) &syntax;
#endif
- startlinno = plinno;
+ startlinno = g_parsefile->linno;
bqlist = NULL;
quotef = 0;
oldstyle = 0;
if (syntax == BASESYNTAX)
goto endword; /* exit outer loop */
USTPUTC(c, out);
- plinno++;
+ g_parsefile->linno++;
if (doprompt)
setprompt(2);
c = pgetc();
if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL)
raise_error_syntax("unterminated quoted string");
if (varnest != 0) {
- startlinno = plinno;
+ startlinno = g_parsefile->linno;
/* { */
raise_error_syntax("missing '}'");
}
continue;
if (*p == '\n' && *q == '\0') {
c = PEOF;
- plinno++;
+ g_parsefile->linno++;
needprompt = doprompt;
} else {
pushstring(line, NULL);
case '\\':
pc = pgetc();
if (pc == '\n') {
- plinno++;
+ g_parsefile->linno++;
if (doprompt)
setprompt(2);
/*
#if ENABLE_ASH_ALIAS
case PEOA:
#endif
- startlinno = plinno;
+ startlinno = g_parsefile->linno;
raise_error_syntax("EOF in backquote substitution");
case '\n':
- plinno++;
+ g_parsefile->linno++;
needprompt = doprompt;
break;
if (needprompt) {
setprompt(2);
}
- startlinno = plinno;
+ startlinno = g_parsefile->linno;
for (;;) { /* until token or start of word found */
c = pgetc_fast();
if (c == ' ' || c == '\t' USE_ASH_ALIAS( || c == PEOA))
pungetc();
break; /* return readtoken1(...) */
}
- startlinno = ++plinno;
+ startlinno = ++g_parsefile->linno;
if (doprompt)
setprompt(2);
} else {
p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1;
if (c != PEOF) {
if (c == '\n') {
- plinno++;
+ g_parsefile->linno++;
needprompt = doprompt;
}
if (needprompt) {
setprompt(2);
}
- startlinno = plinno;
+ startlinno = g_parsefile->linno;
for (;;) { /* until token or start of word found */
c = pgetc_fast();
switch (c) {
continue;
case '\\':
if (pgetc() == '\n') {
- startlinno = ++plinno;
+ startlinno = ++g_parsefile->linno;
if (doprompt)
setprompt(2);
continue;
pungetc();
goto breakloop;
case '\n':
- plinno++;
+ g_parsefile->linno++;
needprompt = doprompt;
RETURN(TNL);
case PEOF:
init(void)
{
/* from input.c: */
- basepf.nextc = basepf.buf = basebuf;
+ basepf.next_to_pgetc = basepf.buf = basebuf;
/* from trap.c: */
signal(SIGCHLD, SIG_DFL);
evalskip = 0;
loopnest = 0;
/* from input.c: */
- parselleft = parsenleft = 0; /* clear input buffer */
+ g_parsefile->left_in_buffer = 0;
+ g_parsefile->left_in_line = 0; /* clear input buffer */
popallfiles();
/* from parser.c: */
tokpushback = 0;