Line continuation and variables
Hi!
While playing with sh generators I found that dash and bash have different interpretations for <slash><newline> sequence.
$ dash -c 'EDIT=xxx; echo $EDIT\
Quoted 1 line
OR'
xxxOR
$ bash -c 'EDIT=xxx; echo $EDIT\ OR' /usr/bin/vim
$ dash -c 'echo "$\ (pwd)"' $(pwd)
Is it undefined behaviour in POSIX?
Signature · 1 line
WBR, Oleg Bulatov
Quoted 8 lines · Oleg Bulatov
Hi!
While playing with sh generators I found that dash and bash have different interpretations for <slash><newline> sequence.
$ dash -c 'EDIT=xxx; echo $EDIT\Quoted 1 line
OR'
xxxOR
Buggy.
Quoted 3 lines
$ bash -c 'EDIT=xxx; echo $EDIT\ OR' /usr/bin/vim
Correct behavior.
Quoted 6 lines
$ dash -c 'echo "$\ (pwd)"' $(pwd)Is it undefined behaviour in POSIX?
No, it's well-defined, and dash is buggy. POSIX says:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03
"the shell shall break its input into tokens by applying the first applicable rule below to the next character in its input"
Rule 4 covers backslash handling, while rule 5 covers locating the end of a word to be subject to $ expansion. Therefore, rule 4 should happen first. Rule 4 defers to the section on quoting, with the caveat that <newline> joining is the only substitution that happens immediately as part of the parsing:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02
"If a <newline> follows the <backslash>, the shell shall interpret this as line continuation. The <backslash> and <newline> shall be removed before splitting the input into tokens. Since the escaped <newline> is removed entirely from the input and is not replaced by any white space, it cannot serve as a token separator."
So the fact that dash is treating the elided backslash-newline as a token separator, and parsing your input as if ${EDIT}OR instead of ${EDITOR} is a bug in dash.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 46 lines · Eric Blake
Quoted 8 lines · Oleg Bulatov
Hi!
While playing with sh generators I found that dash and bash have different interpretations for <slash><newline> sequence.
$ dash -c 'EDIT=xxx; echo $EDIT\Quoted 1 line
OR'
xxxOR
Buggy.
Quoted 3 lines
$ bash -c 'EDIT=xxx; echo $EDIT\ OR' /usr/bin/vimCorrect behavior.
Quoted 6 lines
$ dash -c 'echo "$\ (pwd)"' $(pwd)Is it undefined behaviour in POSIX?
No, it's well-defined, and dash is buggy. POSIX says:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03
"the shell shall break its input into tokens by applying the first applicable rule below to the next character in its input"
Rule 4 covers backslash handling, while rule 5 covers locating the end of a word to be subject to $ expansion. Therefore, rule 4 should happen first. Rule 4 defers to the section on quoting, with the caveat that <newline> joining is the only substitution that happens immediately as part of the parsing:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02
"If a <newline> follows the <backslash>, the shell shall interpret this as line continuation. The <backslash> and <newline> shall be removed before splitting the input into tokens. Since the escaped <newline> is removed entirely from the input and is not replaced by any white space, it cannot serve as a token separator."
Quoted 3 lines
So the fact that dash is treating the elided backslash-newline as a token separator, and parsing your input as if ${EDIT}OR instead of ${EDITOR} is a bug in dash.
I agree. The following patch should fix this:
commit ef91d3d6a4c39421fd3a391e02cd82f9f3aee4a8
Author: Herbert Xu <[email protected]> Date: Mon Sep 29 22:52:41 2014 +0800
[PARSER] Handle backslash newlines properly after dollar sign
On Tue, Aug 26, 2014 at 12:34:42PM +0000, Eric Blake wrote:
> On 08/26/2014 06:15 AM, Oleg Bulatov wrote:
> > Hi!
> >
> > While playing with sh generators I found that dash and bash have different
> > interpretations for <slash><newline> sequence.
> >
> > $ dash -c 'EDIT=xxx; echo $EDIT\
> >> OR'
> > xxxOR
>
> Buggy.
>
> > $ bash -c 'EDIT=xxx; echo $EDIT\
> > OR'
> > /usr/bin/vim
>
> Correct behavior.
>
> >
> > $ dash -c 'echo "$\
> > (pwd)"'
> > $(pwd)
> >
> > Is it undefined behaviour in POSIX?
>
> No, it's well-defined, and dash is buggy. POSIX says:
>
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03
>
> "the shell shall break its input into tokens by applying the first
> applicable rule below to the next character in its input"
>
> Rule 4 covers backslash handling, while rule 5 covers locating the end
> of a word to be subject to $ expansion. Therefore, rule 4 should happen
> first. Rule 4 defers to the section on quoting, with the caveat that
> <newline> joining is the only substitution that happens immediately as
> part of the parsing:
>
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02
>
> "If a <newline> follows the <backslash>, the shell shall interpret this
> as line continuation. The <backslash> and <newline> shall be removed
> before splitting the input into tokens. Since the escaped <newline> is
> removed entirely from the input and is not replaced by any white space,
> it cannot serve as a token separator."
>
> So the fact that dash is treating the elided backslash-newline as a
> token separator, and parsing your input as if ${EDIT}OR instead of
> ${EDITOR} is a bug in dash.
I agree. This patch should resolve this problem and similar ones
affecting blackslash newlines after we encounter a dollar sign.
Signed-off-by: Herbert Xu <[email protected]>ChangeLog +1 -0
@@ -1,6 +1,7 @@ 2014-09-29 Herbert Xu <[email protected]> * Kill pgetc_macro. + * Handle backslash newlines properly after dollar sign. 2014-09-28 Herbert Xu <[email protected]>
src/parser.c +27 -9
@@ -827,6 +827,24 @@ breakloop:#undef RETURN } +static int pgetc_eatbnl (void ) +{ +int c; + +while ((c =pgetc ()) =='\\' ) { +if (pgetc () !='\n' ) { +pungetc (); +break ; + } + + plinno++; +if (doprompt) +setprompt (2 ); + } + +return c; +} +/*
@@ -1179,7 +1197,7 @@ parsesub: {char *p;static const char types[] ="}-+?=" ; - c =pgetc (); + c =pgetc_eatbnl (); if ( (checkkwd & CHKEOFMARK) || c <= PEOA ||
@@ -1188,7 +1206,7 @@ parsesub: {USTPUTC ('$' , out);pungetc (); }else if (c =='(' ) {/* $(command) or $((arith)) */ -if (pgetc () =='(' ) { +if (pgetc_eatbnl () =='(' ) {PARSEARITH (); }else {pungetc ();
@@ -1200,25 +1218,25 @@ parsesub: {STADJUST (1 , out); subtype =VSNORMAL ;if (likely (c =='{' )) { - c =pgetc (); + c =pgetc_eatbnl (); subtype =0 ; } varname:if (is_name (c)) {do {STPUTC (c, out); - c =pgetc (); + c =pgetc_eatbnl (); }while (is_in_name (c)); }else if (is_digit (c)) {do {STPUTC (c, out); - c =pgetc (); + c =pgetc_eatbnl (); }while (is_digit (c)); }else if (is_special (c)) {int cc = c; - c =pgetc (); + c =pgetc_eatbnl ();if (!subtype && cc =='#' ) { subtype =VSLENGTH ;
@@ -1227,7 +1245,7 @@ varname: goto varname; cc = c; - c =pgetc (); + c =pgetc_eatbnl ();if (cc =='}' || c !='}' ) {pungetc (); subtype =0 ;
@@ -1245,7 +1263,7 @@ varname:switch (c) {case ':' : subtype =VSNUL ; - c =pgetc (); + c =pgetc_eatbnl ();/*FALLTHROUGH*/ default : p =strchr (types, c);
@@ -1259,7 +1277,7 @@ varname:int cc = c; subtype = c =='#' ?VSTRIMLEFT :VSTRIMRIGHT ; - c =pgetc (); + c =pgetc_eatbnl ();if (c == cc) subtype++;else
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 8 lines · Herbert Xu
I agree. The following patch should fix this:
commit
ef91d3d6a4c39421fd3a391e02cd82f9f3aee4a8Author: Herbert Xu <[email protected]> Date: Mon Sep 29 22:52:41 2014 +0800[PARSER] Handle backslash newlines properly after dollar sign
Here is a small clean-up on top of it:
commit 6df87cf1d4b7c0c490ab1803b863de10579df92e
Author: Herbert Xu <[email protected]> Date: Mon Sep 29 22:53:53 2014 +0800
[PARSER] Add nlprompt/nlnoprompt helpers
This patch adds the nlprompt/nlnoprompt helpers to isolate code dealing with newlines and prompting.
ChangeLog +1 -0
@@ -2,6 +2,7 @@ * Kill pgetc_macro. * Handle backslash newlines properly after dollar sign. + * Add nlprompt/nlnoprompt helpers. 2014-09-28 Herbert Xu <[email protected]>
src/parser.c +21 -21
95 lines not on this page · raw message
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Quoted 46 lines · Herbert Xu
On Tue, Aug 26, 2014 at 12:34:42PM +0000, Eric Blake wrote:
[snip]Quoted 3 lines
So the fact that dash is treating the elided backslash-newline as a token separator, and parsing your input as if ${EDIT}OR instead of ${EDITOR} is a bug in dash.
I agree. The following patch should fix this:
commit
ef91d3d6a4c39421fd3a391e02cd82f9f3aee4a8Author: Herbert Xu <[email protected]> Date: Mon Sep 29 22:52:41 2014 +0800[PARSER] Handle backslash newlines properly after dollar sign
[snip]ChangeLog +1
-0@@ -1,6 +1,7 @@ 2014-09-29 Herbert Xu <[email protected]> * Kill pgetc_macro. + * Handle backslash newlines properly after dollar sign. 2014-09-28 Herbert Xu <[email protected]>src/parser.c +11
-0@@ -827,6 +827,24 @@ breakloop:#undef RETURN } +static int pgetc_eatbnl (void ) +{ +int c; + +while ((c =pgetc ()) =='\\' ) { +if (pgetc () !='\n' ) { +pungetc (); +break ; + } + + plinno++;
src/parser.c breakloop: · line 850
Quoted 10 lines
src/parser.c +7
-0+if (doprompt) +setprompt (2 ); + } + +return c; +} +/*
This implementation of pgetc_eatbnl() does not allow pushing back a backslash, since that would call pungetc() twice without an intervening pgetc(). However, some places do attempt to push back a backslash. As a result, a script file containing many repeated ${w#\#} will not be parsed correctly. There is a similar bug with repeated $\# but this is not specified by POSIX.
Signature · 1 line
Jilles Tjoelker
Quoted 7 lines · Jilles Tjoelker
This implementation of
pgetc_eatbnl()does not allow pushing back a backslash, since that would callpungetc()twice without an interveningpgetc(). However, some places do attempt to push back a backslash. As a result, a script file containing many repeated ${w#\#} will not be parsed correctly. There is a similar bug with repeated $\# but this is not specified by POSIX.
Good catch! I guess I'll do something similar to tokpushback to handle this.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
cover input: Allow two consecutive calls to pungetc
In reply to Jilles TjoelkerQuoted 7 lines · Jilles Tjoelker
This implementation of
pgetc_eatbnl()does not allow pushing back a backslash, since that would callpungetc()twice without an interveningpgetc(). However, some places do attempt to push back a backslash. As a result, a script file containing many repeated ${w#\#} will not be parsed correctly. There is a similar bug with repeated $\# but this is not specified by POSIX.
I finally got around to fixing this. I've decided to do things a little differently by making it possible to do two pungetc's in a row.
When I get some spare time I would like to make the parser reentrant so we can do PS4 properly and fix some other corner cases.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH 1/4 input: Make preadbuffer static
The function preadbuffer should be static as it's only used in input.c.
---
src/input.c | 4 ++-- src/input.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-)
src/input.c +2 -2
@@ -109,6 +109,7 @@ EditLine *el; /* cookie for editline package */ STATIC void pushfile(void);static int preadfd (void );static void setinputfd (int fd,int push); +static int preadbuffer (void );#ifdef mkinit INCLUDE <stdio.h>
@@ -222,8 +223,7 @@ retry:* 4) Process input up to the next newline, deleting nul characters. */ -int -preadbuffer (void ) +static int preadbuffer (void ) {char *q;int more;
src/input.h +0 -1
@@ -52,7 +52,6 @@ extern char *parsenextc; /* next character in input buffer */int pgetc (void );int pgetc2 (void ); -int preadbuffer (void );void pungetc (void );void pushstring (char *,void *);void popstring (void );
PATCH 2/4 input: Remove HETIO
In reply to Herbert XuIt hasn't been possible to build HETIO for over ten years. So let's just kill it.
---
src/Makefile.am | 2 src/hetio.c | 397 -------------------------------------------------------- src/hetio.h | 22 --- src/input.c | 9 - src/main.c | 8 - src/trap.c | 7 6 files changed, 1 insertion(+), 444 deletions(-)
src/Makefile.am +1 -1
@@ -26,7 +26,7 @@ dash_CFILES = \ dash_SOURCES = \ $(dash_CFILES) \ alias.h arith_yacc.h bltin/bltin.h cd.h error.h eval.h exec.h \ - expand.h hetio.h \ + expand.h \ init.h input.h jobs.h machdep.h mail.h main.h memalloc.h miscbltin.h \ myhistedit.h mystring.h options.h output.h parser.h redir.h shell.h \ show.h system.h trap.h var.h
src/hetio.c deleted +0 -397
397 lines not on this page · raw message
src/hetio.h deleted +0 -22
@@ -1,22 +0,0 @@ -/* -* Termios command line History and Editting for NetBSD sh (ash) -* Copyright (c) 1999 -* Main code: Adam Rogoyski <[email protected]> -* Etc: Dave Cinege <[email protected]> -* -* You may use this code as you wish, so long as the original author(s) -* are attributed in any redistributions of the source code. -* This code is 'as is' with no warranty. -* This code may safely be consumed by a BSD or GPL license. -* -* v 0.5 19990328 Initial release -* -* Future plans: Simple file and path name completion. (like BASH) -* -*/ - -void hetio_init (void ); -int hetio_read_input (int fd); -void hetio_reset_term (void ); - -extern int hetio_inter;
src/input.c +0 -9
@@ -58,10 +58,6 @@#include "myhistedit.h" #endif -#ifdef HETIO -#include "hetio.h" -#endif -#define EOF_NLEFT -99/* value of parsenleft when EOF pushed back */ #define IBUFSIZ (BUFSIZ + 1)
@@ -188,11 +184,6 @@ retry: } else#endif - -#ifdef HETIO - nr =hetio_read_input (parsefile->fd); - if (nr == -255) -#endif nr =read (parsefile->fd, buf,IBUFSIZ -1 );
src/main.c +0 -8
@@ -60,10 +60,6 @@#include "exec.h" #include "cd.h" -#ifdef HETIO -#include "hetio.h" -#endif -#define PROFILE 0int rootpid;
@@ -206,10 +202,6 @@ cmdloop(int top)int numeof =0 ;TRACE (("cmdloop(%d) called\n" , top)); -#ifdef HETIO -if (iflag && top) -hetio_init (); -#endif for (;;) {int skip;
src/trap.c +0 -3
@@ -51,10 +51,6 @@#include "trap.h" #include "mystring.h" -#ifdef HETIO -#include "hetio.h" -#endif
PATCH 3/4 input: Move all input state into parsefile
In reply to Herbert XuCurrently we maintain a copy of the input state outside of parsefile. This is redundant and makes reentrancy difficult. This patch kills the duplicate global states and now everyone simply uses parsefile.
---
src/input.c | 107 ++++++++++++++++++++---------------------------------------- src/input.h | 33 ++++++++++++++++-- 2 files changed, 67 insertions(+), 73 deletions(-)
src/input.c +37 -70
@@ -61,38 +61,7 @@#define EOF_NLEFT -99/* value of parsenleft when EOF pushed back */ #define IBUFSIZ (BUFSIZ + 1) -MKINIT -struct strpush { -struct strpush *prev;/* preceding string on stack */ -char *prevstring; -int prevnleft; -struct alias *ap;/* if push was associated with an alias */ -char *string;/* remember the string since it may change */ -}; -/* -* The parsefile structure pointed to by the global variable parsefile -* contains information about the current file being read. -*/ - -MKINIT -struct parsefile { -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 */ -char *buf;/* input buffer */ -struct strpush *strpush;/* for pushing strings at this level */ -struct strpush basestrpush;/* so pushing one is fast */ -}; - - -int plinno =1 ;/* input line number */ -int parsenleft;/* copy of parsefile->nleft */ -MKINIT int parselleft; /* copy of parsefile->lleft */ -char *parsenextc;/* copy of parsefile->nextc */ MKINIT struct parsefile basepf; /* top level input file */ MKINIT char basebuf[IBUFSIZ]; /* buffer for top level input file */struct parsefile *parsefile = &basepf;/* current input file */
@@ -114,10 +83,12 @@ INCLUDE "error.h" INIT { basepf.nextc = basepf.buf = basebuf; + basepf.linno = 1; } RESET { - parselleft = parsenleft =0 ;/* clear input buffer */ +/* clear input buffer */ + basepf.lleft = basepf.nleft =0 ;popallfiles (); }#endif
@@ -131,8 +102,8 @@ RESET {int pgetc (void ) { -if (--parsenleft >=0 ) -return (signed char )*parsenextc++; +if (--parsefile->nleft >=0 ) +return (signed char )*parsefile->nextc++;else return preadbuffer (); }
@@ -158,7 +129,7 @@ preadfd(void) {int nr;char *buf = parsefile->buf; - parsenextc = buf; + parsefile->nextc = buf; retry: #ifndef SMALL
@@ -225,29 +196,32 @@ static int preadbuffer(void)while (unlikely (parsefile->strpush)) {if ( - parsenleft ==-1 && parsefile->strpush->ap && - parsenextc[-1 ] !=' ' && parsenextc[-1 ] !='\t' + parsefile->nleft ==-1 && + parsefile->strpush->ap && + parsefile->nextc[-1 ] !=' ' && + parsefile->nextc[-1 ] !='\t' ) {return PEOA ; }popstring (); -if (--parsenleft >=0 ) -return (signed char )*parsenextc++; +if (--parsefile->nleft >=0 ) +return (signed char )*parsefile->nextc++; } -if (unlikely (parsenleft ==EOF_NLEFT || parsefile->buf ==NULL )) +if (unlikely (parsefile->nleft ==EOF_NLEFT || + parsefile->buf ==NULL ))return PEOF ;flushall (); - more = parselleft; + more = parsefile->lleft;if (more <=0 ) { again:if ((more =preadfd ()) <=0 ) { - parselleft = parsenleft =EOF_NLEFT ; + parsefile->lleft = parsefile->nleft =EOF_NLEFT ;return PEOF ; } } - q = parsenextc; + q = parsefile->nextc;/* delete nul characters */ #ifndef SMALL
@@ -265,7 +239,7 @@ again: q++;if (c =='\n' ) { - parsenleft = q - parsenextc -1 ; + parsefile->nleft = q - parsefile->nextc -1 ;break ; }
@@ -282,13 +256,13 @@ again: }if (more <=0 ) { - parsenleft = q - parsenextc -1 ; -if (parsenleft <0 ) + parsefile->nleft = q - parsefile->nextc -1 ; +if (parsefile->nleft <0 ) goto again;break ; } } - parselleft = more; + parsefile->lleft = more; savec = *q; *q ='\0' ;
@@ -298,13 +272,13 @@ again:HistEvent he;INTOFF ;history (hist, &he, whichprompt ==1 ?H_ENTER :H_APPEND , - parsenextc); + parsefile->nextc); INTON; }#endif if (vflag) { -out2str (parsenextc); +out2str (parsefile->nextc);#ifdef FLUSHERR flushout (out2);#endif
@@ -312,7 +286,7 @@ again: *q = savec; -return (signed char )*parsenextc++; +return (signed char )*parsefile->nextc++; }/*
@@ -323,8 +297,8 @@ again:void pungetc (void ) { - parsenleft++; - parsenextc--; + parsefile->nleft++; + parsefile->nextc--; }/*
@@ -346,15 +320,15 @@ pushstring(char *s, void *ap) parsefile->strpush = sp; }else sp = parsefile->strpush = &(parsefile->basestrpush); - sp->prevstring = parsenextc; - sp->prevnleft = parsenleft; + sp->prevstring = parsefile->nextc; + sp->prevnleft = parsefile->nleft; sp->ap = (struct alias *)ap;if (ap) { ((struct alias *)ap)->flag |=ALIASINUSE ; sp->string = s; } - parsenextc = s; - parsenleft = len; + parsefile->nextc = s; + parsefile->nleft = len;INTON ; }
@@ -365,7 +339,8 @@ popstring(void)INTOFF ;if (sp->ap) { -if (parsenextc[-1 ] ==' ' || parsenextc[-1 ] =='\t' ) { +if (parsefile->nextc[-1 ] ==' ' || + parsefile->nextc[-1 ] =='\t' ) { checkkwd |=CHKALIAS ; }if (sp->string != sp->ap->val) {
@@ -376,8 +351,8 @@ popstring(void)unalias (sp->ap->name); } } - parsenextc = sp->prevstring; - parsenleft = sp->prevnleft; + parsefile->nextc = sp->prevstring; + parsefile->nleft = sp->prevnleft;/*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ parsefile->strpush = sp->prev;if (sp != &(parsefile->basestrpush))
@@ -426,7 +401,7 @@ setinputfd(int fd, int push) parsefile->fd = fd;if (parsefile->buf ==NULL ) parsefile->buf =ckmalloc (IBUFSIZ ); - parselleft = parsenleft =0 ; + parsefile->lleft = parsefile->nleft =0 ; plinno =1 ; }
@@ -440,8 +415,8 @@ setinputstring(char *string) {INTOFF ;pushfile (); - parsenextc = string; - parsenleft =strlen (string); + parsefile->nextc = string; + parsefile->nleft =strlen (string); parsefile->buf =NULL ; plinno =1 ;INTON ;
@@ -459,10 +434,6 @@ pushfile(void) {struct parsefile *pf; - parsefile->nleft = parsenleft; - parsefile->lleft = parselleft; - parsefile->nextc = parsenextc; - parsefile->linno = plinno; pf = (struct parsefile *)ckmalloc (sizeof (struct parsefile )); pf->prev = parsefile; pf->fd =-1 ;
@@ -486,10 +457,6 @@ popfile(void)popstring (); parsefile = pf->prev;ckfree (pf); - parsenleft = parsefile->nleft; - parselleft = parsefile->lleft; - parsenextc = parsefile->nextc; - plinno = parsefile->linno;INTON ; }
src/input.h +30 -3
@@ -41,14 +41,41 @@ enum { INPUT_NOFILE_OK = 2, }; +struct alias ; + +struct strpush { +struct strpush *prev;/* preceding string on stack */ +char *prevstring; +int prevnleft; +struct alias *ap;/* if push was associated with an alias */ +char *string;/* remember the string since it may change */ +}; + +/* +* The parsefile structure pointed to by the global variable parsefile +* contains information about the current file being read. +*/ + +struct parsefile { +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 */ +char *buf;/* input buffer */ +struct strpush *strpush;/* for pushing strings at this level */ +struct strpush basestrpush;/* so pushing one is fast */ +}; + +extern struct parsefile *parsefile; +/* * The input line number. Input.c just defines this variable, and saves * and restores it when files are pushed and popped. The user of this * package must set its value. */ -extern int plinno; -extern int parsenleft;/* number of characters left in input buffer */ -extern char *parsenextc;/* next character in input buffer */ +#define plinno (parsefile->linno)int pgetc (void );int pgetc2 (void );
PATCH 4/4 input: Allow two consecutive calls to pungetc
In reply to Herbert XuThe commit ef91d3d6a4c39421fd3a391e02cd82f9f3aee4a8 ([PARSER] Handle backslash newlines properly after dollar sign) created cases where we make two consecutive calls to pungetc. As we don't explicitly support that there are corner cases where you end up with garbage input leading to undefined behaviour.
This patch adds explicit support for two consecutive calls to pungetc.
---
src/input.c | 30 ++++++++++++++++++++++-------- src/input.h | 12 ++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-)
src/input.c +22 -8
@@ -102,10 +102,20 @@ RESET {int pgetc (void ) { +int c; + +if (parsefile->unget) +return parsefile->lastc[--parsefile->unget]; +if (--parsefile->nleft >=0 ) -return (signed char )*parsefile->nextc++; + c = (signed char )*parsefile->nextc++;else -return preadbuffer (); + c =preadbuffer (); + + parsefile->lastc[1 ] = parsefile->lastc[0 ]; + parsefile->lastc[0 ] = c; + +return c; }
@@ -194,7 +204,7 @@ static int preadbuffer(void)#endif char savec; -while (unlikely (parsefile->strpush)) { +if (unlikely (parsefile->strpush)) { if ( parsefile->nleft == -1 && parsefile->strpush->ap &&
@@ -204,8 +214,7 @@ static int preadbuffer(void)return PEOA ; }popstring (); -if (--parsefile->nleft >=0 ) -return (signed char )*parsefile->nextc++; +return pgetc (); }if (unlikely (parsefile->nleft ==EOF_NLEFT || parsefile->buf ==NULL ))
@@ -290,15 +299,14 @@ again: }/* -* Undo the last call to pgetc. Only one character may be pushed back. +* Undo a call to pgetc. Only two characters may be pushed back. * PEOF may be pushed back. */ void pungetc (void ) { - parsefile->nleft++; - parsefile->nextc--; + parsefile->unget++; }/*
@@ -322,6 +330,8 @@ pushstring(char *s, void *ap) sp = parsefile->strpush = &(parsefile->basestrpush); sp->prevstring = parsefile->nextc; sp->prevnleft = parsefile->nleft; + sp->unget = parsefile->unget; +memcpy (sp->lastc, parsefile->lastc,sizeof (sp->lastc)); sp->ap = (struct alias *)ap;if (ap) { ((struct alias *)ap)->flag |=ALIASINUSE ;
@@ -329,6 +339,7 @@ pushstring(char *s, void *ap) } parsefile->nextc = s; parsefile->nleft = len; + parsefile->unget =0 ;INTON ; }
@@ -353,6 +364,8 @@ popstring(void) } parsefile->nextc = sp->prevstring; parsefile->nleft = sp->prevnleft; + parsefile->unget = sp->unget; +memcpy (parsefile->lastc, sp->lastc,sizeof (sp->lastc));/*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ parsefile->strpush = sp->prev;if (sp != &(parsefile->basestrpush))
@@ -439,6 +452,7 @@ pushfile(void) pf->fd =-1 ; pf->strpush =NULL ; pf->basestrpush.prev =NULL ; + pf->unget =0 ; parsefile = pf; }
src/input.h +12 -0
@@ -49,6 +49,12 @@ struct strpush {int prevnleft;struct alias *ap;/* if push was associated with an alias */ char *string;/* remember the string since it may change */ + +/* Remember last two characters for pungetc. */ +int lastc[2 ]; + +/* Number of outstanding calls to pungetc. */ +int unget; };/*
@@ -66,6 +72,12 @@ struct parsefile {char *buf;/* input buffer */ struct strpush *strpush;/* for pushing strings at this level */ struct strpush basestrpush;/* so pushing one is fast */ + +/* Remember last two characters for pungetc. */ +int lastc[2 ]; + +/* Number of outstanding calls to pungetc. */ +int unget; };extern struct parsefile *parsefile;