From 93f08ae2b63073ba57125bbb2be375e7c78cdaa5 Mon Sep 17 00:00:00 2001 From: Krush206 <37114863+Krush206@users.noreply.github.com> Date: Wed, 22 Nov 2023 19:05:52 -0300 Subject: [PATCH 1/6] sh.parse.c: # for interactive comments --- sh.parse.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sh.parse.c b/sh.parse.c index 211a1012..06c1f482 100644 --- a/sh.parse.c +++ b/sh.parse.c @@ -273,6 +273,22 @@ syn0(const struct wordent *p1, const struct wordent *p2, int flags) t->t_dcar = t1; t->t_dcdr = syntax(p, p2, flags); return (t); + + case '#': + if (intty) { + struct wordent *p1 = (struct wordent *) p; + + *(struct wordent **) &p2->prev = p1->prev; + *(struct wordent **) &p2->prev->next = (struct wordent *) p2; + while (p1->next != p2) + p1 = p1->next; + p1->next = (struct wordent *) p; + freelex(p1 = p1->next); + xfree(p1->word); + xfree(p1); + *(struct wordent **) &p = (struct wordent *) p2->prev; + continue; + } default: break; } From 3ddb8a0efe9dd413b37551c04d53fea5c786974a Mon Sep 17 00:00:00 2001 From: Krush206 <37114863+Krush206@users.noreply.github.com> Date: Thu, 23 Nov 2023 10:23:59 -0300 Subject: [PATCH 2/6] sh.parse.c: fix freeze on interactive comments --- sh.parse.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sh.parse.c b/sh.parse.c index 06c1f482..c2682889 100644 --- a/sh.parse.c +++ b/sh.parse.c @@ -207,7 +207,7 @@ syntax(const struct wordent *p1, const struct wordent *p2, int flags) { while (p1 != p2) - if (any(";&\n", p1->word[0])) + if (any(intty ? ";&\n#" : ";&\n", p1->word[0])) p1 = p1->next; else return (syn0(p1, p2, flags)); @@ -279,15 +279,14 @@ syn0(const struct wordent *p1, const struct wordent *p2, int flags) struct wordent *p1 = (struct wordent *) p; *(struct wordent **) &p2->prev = p1->prev; - *(struct wordent **) &p2->prev->next = (struct wordent *) p2; + p2->prev->next = (struct wordent *) p2; while (p1->next != p2) p1 = p1->next; p1->next = (struct wordent *) p; freelex(p1 = p1->next); xfree(p1->word); xfree(p1); - *(struct wordent **) &p = (struct wordent *) p2->prev; - continue; + *(struct wordent **) &p = p2->prev; } default: break; From 70eec821e7965cfa728277e00d12a174829abc82 Mon Sep 17 00:00:00 2001 From: Krush206 <37114863+Krush206@users.noreply.github.com> Date: Thu, 23 Nov 2023 10:25:19 -0300 Subject: [PATCH 3/6] sh.lex.c: prevent parsing on comments --- sh.lex.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sh.lex.c b/sh.lex.c index 2de265b5..ae41c5f0 100644 --- a/sh.lex.c +++ b/sh.lex.c @@ -134,6 +134,8 @@ static Char getCtmp; time_t Htime = (time_t)0; static time_t a2time_t (Char *); +static int comment; + /* * special parsing rules apply for source -h */ @@ -148,8 +150,7 @@ lex(struct wordent *hp) int parsehtime = enterhist; int toolong = 0; - histvalid = 0; - histline.len = 0; + histvalid = comment = histline.len = 0; if (!postcmd_active) btell(&lineloc); @@ -482,6 +483,8 @@ getC1(int flag) if (c == CHAR_ERR) c = '\n'; + if (comment && c != '\n') + return c | QUOTE; if (c == '$' && (flag & DODOL)) { getdol(); continue; @@ -490,6 +493,8 @@ getC1(int flag) getexcl(0); continue; } + if (c == '#') + comment = 1; break; } return (c); From 831e820b58746dd973c1cccb302e514c32c70a5a Mon Sep 17 00:00:00 2001 From: Krush206 <37114863+Krush206@users.noreply.github.com> Date: Fri, 24 Nov 2023 11:51:10 -0300 Subject: [PATCH 4/6] Fix parsing issues --- sh.lex.c | 15 ++++++++------- sh.parse.c | 14 +++++--------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/sh.lex.c b/sh.lex.c index ae41c5f0..dbb2c2df 100644 --- a/sh.lex.c +++ b/sh.lex.c @@ -134,8 +134,6 @@ static Char getCtmp; time_t Htime = (time_t)0; static time_t a2time_t (Char *); -static int comment; - /* * special parsing rules apply for source -h */ @@ -150,7 +148,7 @@ lex(struct wordent *hp) int parsehtime = enterhist; int toolong = 0; - histvalid = comment = histline.len = 0; + histvalid = histline.len = 0; if (!postcmd_active) btell(&lineloc); @@ -359,6 +357,13 @@ word(int parsehtime) default: break; } + if (intty && c == '#') { + Strbuf_append1(&wbuf, c); + while ((c = readc(1)) != CHAR_ERR && c != '\n') + Strbuf_append1(&wbuf, c); + unreadc('\n'); + goto ret; + } c1 = 0; dolflg = DOALL; for (;;) { @@ -483,8 +488,6 @@ getC1(int flag) if (c == CHAR_ERR) c = '\n'; - if (comment && c != '\n') - return c | QUOTE; if (c == '$' && (flag & DODOL)) { getdol(); continue; @@ -493,8 +496,6 @@ getC1(int flag) getexcl(0); continue; } - if (c == '#') - comment = 1; break; } return (c); diff --git a/sh.parse.c b/sh.parse.c index c2682889..ad15c471 100644 --- a/sh.parse.c +++ b/sh.parse.c @@ -276,17 +276,13 @@ syn0(const struct wordent *p1, const struct wordent *p2, int flags) case '#': if (intty) { - struct wordent *p1 = (struct wordent *) p; - - *(struct wordent **) &p2->prev = p1->prev; - p2->prev->next = (struct wordent *) p2; - while (p1->next != p2) - p1 = p1->next; - p1->next = (struct wordent *) p; - freelex(p1 = p1->next); + struct wordent *p1 = p2->prev->prev; + xfree(p1->word); + p1 = p1->prev; + xfree(p1->next); + p = p1->next = p2->prev; xfree(p1); - *(struct wordent **) &p = p2->prev; } default: break; From a574fd671ac9c7dfbd1253806bdcb9cd3c28aae5 Mon Sep 17 00:00:00 2001 From: Krush206 <37114863+Krush206@users.noreply.github.com> Date: Fri, 24 Nov 2023 12:29:08 -0300 Subject: [PATCH 5/6] sh.parse.c: fix SIGSEGV --- sh.parse.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sh.parse.c b/sh.parse.c index ad15c471..314739af 100644 --- a/sh.parse.c +++ b/sh.parse.c @@ -276,13 +276,12 @@ syn0(const struct wordent *p1, const struct wordent *p2, int flags) case '#': if (intty) { - struct wordent *p1 = p2->prev->prev; + struct wordent *p1 = p2->prev; - xfree(p1->word); - p1 = p1->prev; + p1 = p1->prev = p1->prev->prev; + xfree(p1->next->word); xfree(p1->next); p = p1->next = p2->prev; - xfree(p1); } default: break; From e59e30adb28b683f5f46f5b66bedfe1d6366e7d4 Mon Sep 17 00:00:00 2001 From: Krush206 <37114863+Krush206@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:48:05 -0300 Subject: [PATCH 6/6] Refactor --- sh.c | 4 +++- sh.h | 1 + sh.lex.c | 16 ++++++++-------- sh.parse.c | 9 ++++++--- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/sh.c b/sh.c index 2d5565fc..181a7be2 100644 --- a/sh.c +++ b/sh.c @@ -1100,7 +1100,9 @@ main(int argc, char **argv) if (adrof(STRedit)) unsetv(STRedit); editing = 0; - } + pchrs = ";&\n"; + } else + pchrs = ";&\n#"; intty |= intact; #ifndef convex if (intty || (intact && isatty(SHOUT))) { diff --git a/sh.h b/sh.h index 19bf10d6..c941fa4b 100644 --- a/sh.h +++ b/sh.h @@ -1305,5 +1305,6 @@ extern int filec; #define TEXP_IGNORE 1 /* in ignore, it means to ignore value, just parse */ #define TEXP_NOGLOB 2 /* in ignore, it means not to globone */ +extern char *pchrs; #endif /* _h_sh */ diff --git a/sh.lex.c b/sh.lex.c index dbb2c2df..93357eaa 100644 --- a/sh.lex.c +++ b/sh.lex.c @@ -319,8 +319,15 @@ word(int parsehtime) goto ret; case '#': - if (intty || (enterhist && !parsehtime)) + if (enterhist && !parsehtime) break; + if (intty) { + do + Strbuf_append1(&wbuf, c); + while ((c = getC(0)) != CHAR_ERR && c != '\n'); + ungetC('\n'); + goto ret; + } c = 0; h = 0; do { @@ -357,13 +364,6 @@ word(int parsehtime) default: break; } - if (intty && c == '#') { - Strbuf_append1(&wbuf, c); - while ((c = readc(1)) != CHAR_ERR && c != '\n') - Strbuf_append1(&wbuf, c); - unreadc('\n'); - goto ret; - } c1 = 0; dolflg = DOALL; for (;;) { diff --git a/sh.parse.c b/sh.parse.c index 314739af..c4ed1e05 100644 --- a/sh.parse.c +++ b/sh.parse.c @@ -197,6 +197,8 @@ freenod(struct wordent *p1, struct wordent *p2) #define P_OUT 4 #define P_DIAG 8 +char *pchrs; + /* * syntax * empty @@ -205,9 +207,8 @@ freenod(struct wordent *p1, struct wordent *p2) struct command * syntax(const struct wordent *p1, const struct wordent *p2, int flags) { - while (p1 != p2) - if (any(intty ? ";&\n#" : ";&\n", p1->word[0])) + if (any(pchrs, p1->word[0])) p1 = p1->next; else return (syn0(p1, p2, flags)); @@ -281,11 +282,13 @@ syn0(const struct wordent *p1, const struct wordent *p2, int flags) p1 = p1->prev = p1->prev->prev; xfree(p1->next->word); xfree(p1->next); - p = p1->next = p2->prev; + p1->next = p2->prev; + goto out; } default: break; } +out: if (l == 0) return (syn1(p1, p2, flags)); seterror(ERR_TOOMANYLP);