From patchwork Sun May 5 09:14:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654210 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1091748F for ; Sun, 5 May 2024 09:14:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900478; cv=none; b=K7GLGGZ4Pgiv1KGQTR1N9DhpIfdRdMfNec2kPcUt8+M3qVRMTrWRMtpClEK5OspfOuWkImKKJKI5XSPCqFl676HoYFkeSRBA9Rv/QMgumtwoTCtTxukw/DI0vPQB3W3XR4rBPsmVV1xjqi9wvxCjrzO0xo360Lj2n6TSx6YX4wM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900478; c=relaxed/simple; bh=PTuY1FwaYoW0+ftpuGe503VHsrk4LYewOCTtoEdnLtQ=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=Nn1tm8TuTWsB7yK1Wc11d6Roc/nw0fJRxwUscdlJy54L1xqNZcfm5NuCZcRr6x8SPR6mq2bAY/uDeDItcL7WuEb+AAg7mFZEAySqSpdA25822sNdHNdPW8cpcAutTHnzwlTE8BLfIY4M2Sy4AX3uMFi/gFrHMOovAyE/pyvaM3c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xwv-00AaVE-2k; Sun, 05 May 2024 17:14:26 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:26 +0800 Date: Sun, 05 May 2024 17:14:26 +0800 Message-Id: In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 01/13] shell: Call setlocale To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Call setlocale to initialise locale settings for libc. Signed-off-by: Herbert Xu --- src/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.c b/src/main.c index 7beb280..1e192f8 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,7 @@ * SUCH DAMAGE. */ +#include #include #include #include @@ -101,6 +102,9 @@ main(int argc, char **argv) #if PROFILE monitor(4, etext, profile_buf, sizeof profile_buf, 50); #endif + + setlocale(LC_ALL, ""); + state = 0; if (unlikely(setjmp(main_handler.loc))) { int e; From patchwork Sun May 5 09:14:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654213 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40D7C567F for ; Sun, 5 May 2024 09:14:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900480; cv=none; b=r9NMnGvi4wR+52i9ARfRhxJx3Z+CtEGk4pXWNh7pHKNAOsqYyeXMzsXGfe9ZEMk5O11GYlEgmX2gC5fRym89eB3eSH5Irg+dsOTopJAa5TT/1AfytMK87SX0rpxYENbt8yf+7Fz7e8uNLbZdyYYF0n3BWTtrxTmRJz+siIBPgIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900480; c=relaxed/simple; bh=QSNOuGchN7VIKs3Dii5wq9S5Z13unfd2KegrKIUOKMs=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=oLe8Ml/skSOnutda4KW48wlZQUiI5RJ2jtLhZKbi1J/oo4fXSEMMZirDzzgtmSHIPje2qR/hl4bv7t6QXlCnXId4fACWBnq6+/UzrY6IbSGmwL+3wyCM0xQWzVMkPH0PzPRn8D7oxmYGMM5v/+piYPmpMSrGi7+5WpHnAUO7khI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xwy-00AaVl-0h; Sun, 05 May 2024 17:14:29 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:28 +0800 Date: Sun, 05 May 2024 17:14:28 +0800 Message-Id: <3455da09a3e822b0ac3e29e644dffa1c78a8cbef.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 02/13] shell: Use strcoll instead of strcmp where applicable To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Use strcoll instead of strcmp so that the locale is taken into account when sorting strings during pathname expansion, and for the built-in test(1) string comparison operators. Signed-off-by: Herbert Xu --- src/bltin/test.c | 8 ++++---- src/expand.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bltin/test.c b/src/bltin/test.c index fd8a43b..2db4d0f 100644 --- a/src/bltin/test.c +++ b/src/bltin/test.c @@ -353,13 +353,13 @@ binop(void) /* NOTREACHED */ #endif case STREQ: - return strcmp(opnd1, opnd2) == 0; + return strcoll(opnd1, opnd2) == 0; case STRNE: - return strcmp(opnd1, opnd2) != 0; + return strcoll(opnd1, opnd2) != 0; case STRLT: - return strcmp(opnd1, opnd2) < 0; + return strcoll(opnd1, opnd2) < 0; case STRGT: - return strcmp(opnd1, opnd2) > 0; + return strcoll(opnd1, opnd2) > 0; case INTEQ: return getn(opnd1) == getn(opnd2); case INTNE: diff --git a/src/expand.c b/src/expand.c index 0db2b29..9ac981e 100644 --- a/src/expand.c +++ b/src/expand.c @@ -1476,7 +1476,7 @@ msort(struct strlist *list, int len) p = msort(p, len - half); /* sort second half */ lpp = &list; for (;;) { - if (strcmp(p->text, q->text) < 0) { + if (strcoll(p->text, q->text) < 0) { *lpp = p; lpp = &p->next; if ((p = *lpp) == NULL) { From patchwork Sun May 5 09:14:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654209 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2036567F for ; Sun, 5 May 2024 09:14:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900478; cv=none; b=fJ519H+Z9rWqepth/aQ+cEy69UtLuvQ+QjlYLDnxWRNmyXr90ogYaGdjNKvtxuJ9DcORAATZDkPmLQ2B19n47UpU9zz9EWOmdHnntDLioCVQC+LUczJGlCvyEgFrsGadEoCR3XvBVjbrnT7+An0nYHTPAQcSZY/ZYvepjboAOOQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900478; c=relaxed/simple; bh=WM4K4qsej20xU9qC+ULpNgYM938hmoTCpH8V5sWVgEA=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=JlP+hQVK2yP3JPAvPMk29WFPLQZarvWdCE67zuGTrbZwL4Jiz7uMKLbSpQ281SkzAUDhXoBk+ewMhLr/tC82FfGJdvM2LbiKuLaZnLGP2gS0awmBDooIAXZCAHvzps9+nYW4nFknXssMdUeHf5j2ZHLiPum6kpa8T52w/x7ucOA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xx0-00AaVw-1i; Sun, 05 May 2024 17:14:31 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:30 +0800 Date: Sun, 05 May 2024 17:14:30 +0800 Message-Id: In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 03/13] expand: Count multi-byte characters for VSLENGTH To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Count multi-byte characters in variables and rather than bytes and return that as the length expansion. Signed-off-by: Herbert Xu --- src/expand.c | 62 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/expand.c b/src/expand.c index 9ac981e..ad186b0 100644 --- a/src/expand.c +++ b/src/expand.c @@ -53,6 +53,7 @@ #endif #include #include +#include /* * Routines to expand arguments to commands. We have to deal with @@ -796,6 +797,18 @@ really_record: return p; } +static char *chtodest(int c, int flags, char *out) +{ + const char *syntax = flags & EXP_QUOTED ? DQSYNTAX : BASESYNTAX; + + if ((flags & QUOTES_ESC) && + ((syntax[c] == CCTL) || + (flags & EXP_QUOTED && syntax[c] == CBACK))) + USTPUTC(CTLESC, out); + USTPUTC(c, out); + + return out; +} /* * Put a string on the stack. @@ -803,38 +816,48 @@ really_record: static size_t memtodest(const char *p, size_t len, int flags) { - const char *syntax = flags & EXP_QUOTED ? DQSYNTAX : BASESYNTAX; + size_t count = 0; char *q; - char *s; + int c; if (unlikely(!len)) return 0; q = makestrspace(len * 2, expdest); - s = q; do { - int c = (signed char)*p++; - if (c) { - if ((flags & QUOTES_ESC) && - ((syntax[c] == CCTL) || - (flags & EXP_QUOTED && syntax[c] == CBACK))) - USTPUTC(CTLESC, q); - } else if (!(flags & EXP_KEEPNUL)) + c = (signed char)*p++; + + if (c) + count++; + else if (!(flags & EXP_KEEPNUL)) continue; - USTPUTC(c, q); + + if (c < 0) { + mbstate_t mbs = {}; + + p--; + do { + q = chtodest(c, flags, q); + } while (mbrlen(p++, 1, &mbs) == -2 && + (c = *p, --len)); + if (!len) + break; + continue; + } + + q = chtodest(c, flags, q); } while (--len); expdest = q; - return q - s; + return count; } static size_t strtodest(const char *p, int flags) { size_t len = strlen(p); - memtodest(p, len, flags); - return len; + return memtodest(p, len, flags); } @@ -856,6 +879,7 @@ varvalue(char *name, int varflags, int flags, int quoted) int discard = (subtype == VSPLUS || subtype == VSLENGTH) | (flags & EXP_DISCARD); ssize_t len = 0; + size_t start; char c; if (!subtype) { @@ -865,9 +889,9 @@ varvalue(char *name, int varflags, int flags, int quoted) sh_error("Bad substitution"); } - flags |= EXP_KEEPNUL; flags &= discard ? ~QUOTES_ESC : ~0; sep = (flags & EXP_FULL) << CHAR_BIT; + start = expdest - (char *)stackblock(); switch (*name) { case '$': @@ -927,7 +951,7 @@ param: if (*ap && sep) { len++; - memtodest(&sepc, 1, flags); + memtodest(&sepc, 1, flags | EXP_KEEPNUL); } } break; @@ -957,7 +981,7 @@ value: } if (discard) - STADJUST(-len, expdest); + expdest = (char *)stackblock() + start; return len; } @@ -1758,11 +1782,13 @@ casematch(union node *pattern, char *val) static size_t cvtnum(intmax_t num, int flags) { + size_t start = expdest - (char *)stackblock(); int len = max_int_length(sizeof(num)); char buf[len]; len = fmtstr(buf, len, "%" PRIdMAX, num); - return memtodest(buf, len, flags); + memtodest(buf, len, flags); + return (expdest - (char *)stackblock()) - start; } STATIC void From patchwork Sun May 5 09:14:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654212 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02288B64C for ; Sun, 5 May 2024 09:14:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900479; cv=none; b=OPY4u0KqCV99qA/9sORi/t2EXxDWtTyzj6xYU3tmSiK7P8RJZKP6oTjiJlhgU/HXhNnElCOVG0WDntF2eqiStYYe7zKQ+Wdk08uPgATxp8k/bNpz8EGBFgBp9q6YwwqwlHiHh0yweIyDbf5L+hyIoBIShyM/CiH3XbXS5/+TWiQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900479; c=relaxed/simple; bh=pNKNV+W2oj7oHc53rjrXu/H5VZuqawu5yMMwiCJx1Dk=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=pjRv6ls45nWUuf5jTNolnQbElhTTFTTMPdZahi4vcLRs499+ZjruPA2yN1980myj0Vvcz5RoiWkcREDx4wzCtZPN5vzDhOGuOQWq1QwuTQ3O0c8qmkRVInBQiK9J0r9UT5KgGPeInzgXCOi0TvJk96CkZsh/OKuDxHVedgPet2k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xx2-00AaW7-2i; Sun, 05 May 2024 17:14:33 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:33 +0800 Date: Sun, 05 May 2024 17:14:33 +0800 Message-Id: <3c8cba020998b08221f9bfc2bf65fb525a9bf555.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 04/13] expand: Process multi-byte characters in subevalvar To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: When trimming variables in subevalvar, process multi-byte characters as one unit instead of their constituent bytes. Signed-off-by: Herbert Xu --- src/expand.c | 192 ++++++++++++++++++++++++++++++++++--------------- src/expand.h | 1 + src/mystring.c | 2 +- src/parser.h | 1 + 4 files changed, 136 insertions(+), 60 deletions(-) diff --git a/src/expand.c b/src/expand.c index ad186b0..60a51b1 100644 --- a/src/expand.c +++ b/src/expand.c @@ -32,27 +32,27 @@ * SUCH DAMAGE. */ -#include -#include -#include +#include #include -#include -#ifdef HAVE_GETPWNAM -#include -#endif -#include -#include -#include -#include -#include #ifdef HAVE_FNMATCH #include #endif #ifdef HAVE_GLOB #include #endif -#include +#include +#include +#ifdef HAVE_GETPWNAM +#include +#endif +#include #include +#include +#include +#include +#include +#include +#include #include /* @@ -550,8 +550,10 @@ static char *scanleft(char *startp, char *endp, char *rmesc, char *rmescend, loc = startp; loc2 = rmesc; do { - int match; const char *s = loc2; + unsigned ml; + int match; + c = *loc2; if (zero) { *loc2 = '\0'; @@ -560,12 +562,26 @@ static char *scanleft(char *startp, char *endp, char *rmesc, char *rmescend, match = pmatch(str, s); *loc2 = c; if (match) - return loc; - if (quotes && *loc == (char)CTLESC) + return quotes ? loc : loc2; + + if (!c) + break; + + if (*loc != (char)CTLMBCHAR) { + if (*loc == (char)CTLESC) + loc++; loc++; - loc++; - loc2++; - } while (c); + loc2++; + continue; + } + + if (*++loc == (char)CTLESC) + loc++; + + ml = (unsigned char)*loc; + loc += ml + 3; + loc2 += ml; + } while (1); return 0; } @@ -573,14 +589,16 @@ static char *scanleft(char *startp, char *endp, char *rmesc, char *rmescend, static char *scanright(char *startp, char *endp, char *rmesc, char *rmescend, char *str, int quotes, int zero ) { - int esc = 0; + size_t esc = 0; char *loc; char *loc2; for (loc = endp, loc2 = rmescend; loc >= startp; loc2--) { - int match; - char c = *loc2; const char *s = loc2; + char c = *loc2; + unsigned ml; + int match; + if (zero) { *loc2 = '\0'; s = rmesc; @@ -588,17 +606,23 @@ static char *scanright(char *startp, char *endp, char *rmesc, char *rmescend, match = pmatch(str, s); *loc2 = c; if (match) - return loc; + return quotes ? loc : loc2; loc--; - if (quotes) { - if (--esc < 0) { - esc = esclen(startp, loc); - } - if (esc % 2) { - esc--; - loc--; - } + if (!esc--) + esc = esclen(startp, loc); + if (esc % 2) { + esc--; + loc--; + continue; } + if (*loc != (char)CTLMBCHAR) + continue; + + ml = (unsigned char)*--loc; + loc -= ml + 2; + if (*loc == (char)CTLESC) + loc--; + loc2 -= ml - 1; } return 0; } @@ -652,14 +676,11 @@ static char *subevalvar(char *start, char *str, int strloc, int startloc, nstrloc = str - (char *)stackblock(); } - rmesc = startp; - if (quotes) { - rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); - if (rmesc != startp) - rmescend = expdest; - startp = stackblock() + startloc; - str = stackblock() + nstrloc; - } + rmesc = _rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); + if (rmesc != startp) + rmescend = expdest; + startp = stackblock() + startloc; + str = stackblock() + nstrloc; rmescend--; /* zero = subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX */ @@ -669,16 +690,29 @@ static char *subevalvar(char *start, char *str, int strloc, int startloc, endp = stackblock() + strloc - 1; loc = scan(startp, endp, rmesc, rmescend, str, quotes, zero); - if (loc) { - if (zero) { - memmove(startp, loc, endp - loc); - loc = startp + (endp - loc); + if (!loc) { + if (quotes) { + rmesc = startp; + rmescend = endp; } - *loc = '\0'; - } else - loc = endp; + } else if (!quotes) { + if (zero) + rmesc = loc; + else + rmescend = loc; + } else if (zero) { + rmesc = loc; + rmescend = endp; + } else { + rmesc = startp; + rmescend = loc; + } + + memmove(startp, rmesc, rmescend - rmesc); + loc = startp + (rmescend - rmesc); out: + *loc = '\0'; amount = loc - expdest; STADJUST(amount, expdest); @@ -704,6 +738,7 @@ evalvar(char *p, int flag) ssize_t varlen; int discard; int quoted; + int mbchar; varflags = *p++ & ~VSBIT; subtype = varflags & VSTYPE; @@ -713,8 +748,18 @@ evalvar(char *p, int flag) startloc = expdest - (char *)stackblock(); p = strchr(p, '=') + 1; + mbchar = 0; + switch (subtype) { + case VSTRIMLEFT: + case VSTRIMLEFTMAX: + case VSTRIMRIGHT: + case VSTRIMRIGHTMAX: + mbchar = EXP_MBCHAR; + break; + } + again: - varlen = varvalue(var, varflags, flag, quoted); + varlen = varvalue(var, varflags, flag | mbchar, quoted); if (varflags & VSNUL) varlen--; @@ -801,7 +846,7 @@ static char *chtodest(int c, int flags, char *out) { const char *syntax = flags & EXP_QUOTED ? DQSYNTAX : BASESYNTAX; - if ((flags & QUOTES_ESC) && + if ((flags & (QUOTES_ESC | EXP_MBCHAR)) && ((syntax[c] == CCTL) || (flags & EXP_QUOTED && syntax[c] == CBACK))) USTPUTC(CTLESC, out); @@ -823,9 +868,13 @@ static size_t memtodest(const char *p, size_t len, int flags) if (unlikely(!len)) return 0; - q = makestrspace(len * 2, expdest); + /* CTLMBCHAR, 2, c, c, 2, CTLMBCHAR */ + q = makestrspace(len * 3, expdest); do { + mbstate_t mbs = {}; + size_t ml; + c = (signed char)*p++; if (c) @@ -833,19 +882,30 @@ static size_t memtodest(const char *p, size_t len, int flags) else if (!(flags & EXP_KEEPNUL)) continue; - if (c < 0) { - mbstate_t mbs = {}; + if (c >= 0) + goto copy; - p--; - do { - q = chtodest(c, flags, q); - } while (mbrlen(p++, 1, &mbs) == -2 && - (c = *p, --len)); - if (!len) - break; - continue; + ml = mbrlen(p - 1, len, &mbs); + if (ml == -1 || ml == -2 || ml < 2 || ml > MB_LEN_MAX) + goto copy; + + if ((flags & (QUOTES_ESC | EXP_MBCHAR))) { + USTPUTC(CTLMBCHAR, q); + USTPUTC(ml, q); } + q = mempcpy(q, p - 1, ml); + + if ((flags & (QUOTES_ESC | EXP_MBCHAR))) { + USTPUTC(ml, q); + USTPUTC(CTLMBCHAR, q); + } + + p += ml - 1; + len -= ml - 1; + continue; + +copy: q = chtodest(c, flags, q); } while (--len); @@ -1720,6 +1780,8 @@ _rmescapes(char *str, int flag) inquotes = 0; notescaped = globbing; while (*p) { + unsigned ml; + if (*p == (char)CTLQUOTEMARK) { p++; inquotes ^= globbing; @@ -1743,6 +1805,18 @@ add_escape: } } notescaped = globbing; + + if (*p != (char)CTLMBCHAR) + goto copy; + + if (*++p == (char)CTLESC) + p++; + + ml = (unsigned char)*p++; + q = mempcpy(q, p, ml); + p += ml + 2; + continue; + copy: *q++ = *p++; } diff --git a/src/expand.h b/src/expand.h index 49a18f9..e5a990e 100644 --- a/src/expand.h +++ b/src/expand.h @@ -60,6 +60,7 @@ struct arglist { #define EXP_QUOTED 0x100 /* expand word in double quotes */ #define EXP_KEEPNUL 0x200 /* do not skip NUL characters */ #define EXP_DISCARD 0x400 /* discard result of expansion */ +#define EXP_MBCHAR 0x800 /* mark multi-byte characters */ struct jmploc; diff --git a/src/mystring.c b/src/mystring.c index 5eace6c..77b457c 100644 --- a/src/mystring.c +++ b/src/mystring.c @@ -67,7 +67,7 @@ const char cqchars[] = { #ifdef HAVE_FNMATCH '^', #endif - CTLESC, CTLQUOTEMARK, 0 + CTLESC, CTLMBCHAR, CTLQUOTEMARK, 0 }; const char illnum[] = "Illegal number: %s"; const char homestr[] = "HOME"; diff --git a/src/parser.h b/src/parser.h index 433573d..14bfc4f 100644 --- a/src/parser.h +++ b/src/parser.h @@ -44,6 +44,7 @@ union node; #define CTLVAR -126 /* variable defn */ #define CTLENDVAR -125 #define CTLBACKQ -124 +#define CTLMBCHAR -123 #define CTLARI -122 /* arithmetic expression */ #define CTLENDARI -121 #define CTLQUOTEMARK -120 From patchwork Sun May 5 09:14:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654214 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F03B5748F for ; Sun, 5 May 2024 09:14:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900481; cv=none; b=ip5xbyDafHAJbrMf+VM99mgWKgRm8Iz1L6n7EMQ3DOUcjZm/IcttTuK76GqfOpIDgrT5KhiS74sNrlKOgfTkVpZkudzbwYhN3fekLiy1dZBlspouJsg5iJaHkbxM+FMEHBVBqr7Bp6yEHk0FZtTmVbEHvAYc2v3wAPiJ0EFD9kU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900481; c=relaxed/simple; bh=+o2MMlHVZgTfWjXs652tU8pzdcHBFnXBRWwTu28m+Vc=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=FqarBuXHB06ms+lLdyNhEig27I3aFJKMlqCFmYEIBCMiyTQJfxX6w967YKHw4x701lle/2Injn6aUuWj6I2wI3aMlWc5SKV8XAiwX1HyKCCVwElHL1X360S47Cx0ahYeUg3qTnXVJ7bHV3ewM8ELSNa1XsxIiwaMd8eZdNZz1aw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xx5-00AaWI-0Z; Sun, 05 May 2024 17:14:36 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:35 +0800 Date: Sun, 05 May 2024 17:14:35 +0800 Message-Id: <3245a4aa0efcb3f503d6974ec63ba822a5d9e288.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 05/13] expand: Process multi-byte characters in expmeta To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: When glob(3) is not in use, make sure that expmeta processes multi-byte characters correctly. Signed-off-by: Herbert Xu --- src/expand.c | 107 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 34 deletions(-) diff --git a/src/expand.c b/src/expand.c index 60a51b1..0e85025 100644 --- a/src/expand.c +++ b/src/expand.c @@ -84,6 +84,7 @@ #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ +#define RMESCAPE_EMETA 0x20 /* Remove backslashes too */ /* Add CTLESC when necessary. */ #define QUOTES_ESC (EXP_FULL | EXP_CASE) @@ -1347,15 +1348,13 @@ expandmeta(struct strlist *str) savelastp = exparg.lastp; INTOFF; - p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); + p = str->text; len = strlen(p); expdir_max = len + PATH_MAX; expdir = ckmalloc(expdir_max); expmeta(p, len, 0); ckfree(expdir); - if (p != str->text) - ckfree(p); INTON; if (exparg.lastp == savelastp) { /* @@ -1376,6 +1375,41 @@ nometa: } } +static void expmeta_rmescapes(char *enddir, char *name) +{ + preglob(strcpy(enddir, name), RMESCAPE_EMETA); +} + +static unsigned mbcharlen(char *p) +{ + int esc = 0; + + if (*++p == (char)CTLESC) + esc++; + + return esc + 3 + (unsigned char)p[esc]; +} + +static int skipesc(char *p) +{ + int esc = 0; + + if (p[esc] == (char)CTLMBCHAR) + return esc + mbcharlen(p); + + if (*p == (char)CTLESC) + esc++; + + if (p[esc] == '\\' && p[esc + 1]) { + esc++; + if (p[esc] == (char)CTLMBCHAR) + return esc + mbcharlen(p + esc); + if (p[esc] == (char)CTLESC) + esc++; + } + + return esc; +} /* * Do metacharacter (i.e. *, ?, [...]) expansion. @@ -1385,17 +1419,18 @@ STATIC void expmeta(char *name, unsigned name_len, unsigned expdir_len) { char *enddir = expdir + expdir_len; - char *p; + struct stat64 statb; + struct dirent64 *dp; const char *cp; - char *start; char *endname; int metaflag; - struct stat64 statb; - DIR *dirp; - struct dirent64 *dp; - int atend; int matchdot; + char *start; + DIR *dirp; + char *pat; + char *p; int esc; + int c; metaflag = 0; start = name; @@ -1407,8 +1442,7 @@ expmeta(char *name, unsigned name_len, unsigned expdir_len) if (*q == '!') q++; for (;;) { - if (*q == '\\') - q++; + q += skipesc(q); if (*q == '/' || *q == '\0') break; if (*++q == ']') { @@ -1417,8 +1451,8 @@ expmeta(char *name, unsigned name_len, unsigned expdir_len) } } } else { - if (*p == '\\' && p[1]) - esc++; + esc = skipesc(p); + if (p[esc] == '/') { if (metaflag) break; @@ -1429,24 +1463,18 @@ expmeta(char *name, unsigned name_len, unsigned expdir_len) if (metaflag == 0) { /* we've reached the end of the file name */ if (!expdir_len) return; - p = name; - do { - if (*p == '\\' && p[1]) - p++; - *enddir++ = *p; - } while (*p++); + expmeta_rmescapes(enddir, name); if (lstat64(expdir, &statb) >= 0) addfname(expdir); return; } endname = p; if (name < start) { - p = name; - do { - if (*p == '\\' && p[1]) - p++; - *enddir++ = *p++; - } while (p < start); + c = *start; + *start = 0; + expmeta_rmescapes(enddir, name); + *start = c; + enddir += strlen(enddir); } *enddir = 0; cp = expdir; @@ -1455,25 +1483,26 @@ expmeta(char *name, unsigned name_len, unsigned expdir_len) cp = "."; if ((dirp = opendir(cp)) == NULL) return; - if (*endname == 0) { - atend = 1; - } else { - atend = 0; + c = *endname; + if (c) { *endname = '\0'; endname += esc + 1; } name_len -= endname - name; matchdot = 0; p = start; + if (*p == (char)CTLESC) + p++; if (*p == '\\') p++; if (*p == '.') matchdot++; + pat = preglob(start, RMESCAPE_ALLOC | RMESCAPE_HEAP); while (! int_pending() && (dp = readdir64(dirp)) != NULL) { if (dp->d_name[0] == '.' && ! matchdot) continue; - if (pmatch(start, dp->d_name)) { - if (atend) { + if (pmatch(pat, dp->d_name)) { + if (!c) { scopy(dp->d_name, enddir); addfname(expdir); } else { @@ -1496,9 +1525,11 @@ expmeta(char *name, unsigned name_len, unsigned expdir_len) } } } + if (pat != start) + ckfree(pat); closedir(dirp); - if (! atend) - endname[-esc - 1] = esc ? '\\' : '/'; + if (c) + endname[-esc - 1] = c; } #endif /* HAVE_GLOB */ @@ -1743,6 +1774,7 @@ _rmescapes(char *str, int flag) int notescaped; int globbing; int inquotes; + int expmeta; p = strpbrk(str, cqchars); if (!p) { @@ -1751,6 +1783,7 @@ _rmescapes(char *str, int flag) q = p; r = str; globbing = flag & RMESCAPE_GLOB; + expmeta = (flag & RMESCAPE_EMETA) ? RMESCAPE_GLOB : 0; if (flag & RMESCAPE_ALLOC) { size_t len = p - str; @@ -1790,6 +1823,10 @@ _rmescapes(char *str, int flag) if (*p == '\\') { /* naked back slash */ notescaped ^= globbing; + if (expmeta & ~notescaped) { + p++; + continue; + } goto copy; } if (FNMATCH_IS_ENABLED && *p == '^') @@ -1797,7 +1834,9 @@ _rmescapes(char *str, int flag) if (*p == (char)CTLESC) { p++; add_escape: - if (notescaped) + if (expmeta) + ; + else if (notescaped) *q++ = '\\'; else if (inquotes) { *q++ = '\\'; From patchwork Sun May 5 09:14:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654215 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB6DA567F for ; Sun, 5 May 2024 09:14:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900484; cv=none; b=kVppjsaRXJ8FJg+/quHm0WiIsUxnMyIztcq5GQgojv2+7m6TCzyFDlAH9TDqBC0bdYELeCwa5YG/GntrTMPCf+lktT6nxAsbmCPxjN9DpG/CT73Op/2kOFQvYLrdshWqkct6qKl6Y6a42NCgN+r+Py/RiMO2DkYU/dKEZZbxgNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900484; c=relaxed/simple; bh=qd9G7hnmDp8igWOW5LymgjpCbWab9ETYgYteQgf2G8Y=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=phNUO8tDEwFNnlrtPLx17HloRlmSdPA1lLA5aPbL/DZrbq65zrvUy6zdI8JJe44Va+fidETISFHnQCpIeLxfMSkE55zftfIjxStLdlZ8Id55k5jVjO8JVXw5khgOhpVbvp4Y90pGLxPft/Dnh583V+HeTCuvI5oEx45rfxKw8hw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xx7-00AaWT-26; Sun, 05 May 2024 17:14:38 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:38 +0800 Date: Sun, 05 May 2024 17:14:38 +0800 Message-Id: <11a3fa7e46ed8bd2fa6aa52b9d7075216e83e39d.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 06/13] expand: Support multi-byte characters during field splitting To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: When multi-byte characters are used in IFS, they will be used for field splitting. Signed-off-by: Herbert Xu --- src/expand.c | 201 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 140 insertions(+), 61 deletions(-) diff --git a/src/expand.c b/src/expand.c index 0e85025..dd2b71e 100644 --- a/src/expand.c +++ b/src/expand.c @@ -54,6 +54,7 @@ #include #include #include +#include /* * Routines to expand arguments to commands. We have to deal with @@ -164,6 +165,30 @@ esclen(const char *start, const char *p) { return esc; } +static __attribute__((noinline)) unsigned mbnext(const char *p) +{ + unsigned start = 0; + unsigned end = 0; + unsigned ml; + int c; + + c = p[end++]; + + switch (c) { + case CTLMBCHAR: + if (p[end] == CTLESC) + end++; + ml = (unsigned char)p[end++]; + start = end; + end = ml + 2; + break; + case CTLESC: + start++; + break; + } + + return start | end << 8; +} static inline const char *getpwhome(const char *name) { @@ -552,6 +577,7 @@ static char *scanleft(char *startp, char *endp, char *rmesc, char *rmescend, loc2 = rmesc; do { const char *s = loc2; + unsigned mb; unsigned ml; int match; @@ -568,19 +594,9 @@ static char *scanleft(char *startp, char *endp, char *rmesc, char *rmescend, if (!c) break; - if (*loc != (char)CTLMBCHAR) { - if (*loc == (char)CTLESC) - loc++; - loc++; - loc2++; - continue; - } - - if (*++loc == (char)CTLESC) - loc++; - - ml = (unsigned char)*loc; - loc += ml + 3; + mb = mbnext(loc); + loc += (mb & 0xff) + (mb >> 8); + ml = (mb >> 8) > 3 ? (mb >> 8) - 2 : 1; loc2 += ml; } while (1); return 0; @@ -930,18 +946,22 @@ static size_t strtodest(const char *p, int flags) STATIC ssize_t varvalue(char *name, int varflags, int flags, int quoted) { + int subtype = varflags & VSTYPE; + const char *seps; + ssize_t len = 0; + unsigned seplen; + size_t start; + int discard; + char sepc; + char **ap; + int sep; int num; char *p; int i; - int sep; - char sepc; - char **ap; - int subtype = varflags & VSTYPE; - int discard = (subtype == VSPLUS || subtype == VSLENGTH) | - (flags & EXP_DISCARD); - ssize_t len = 0; - size_t start; - char c; + int c; + + discard = (subtype == VSPLUS || subtype == VSLENGTH) | + (flags & EXP_DISCARD); if (!subtype) { if (discard) @@ -1004,15 +1024,27 @@ numvar: sep &= ~quoted; sep |= ifsset() ? (unsigned char)(c & ifsval()[0]) : ' '; param: - sepc = sep; if (!(ap = shellparam.p)) return -1; + sepc = sep; + seps = &sepc; + seplen = 1; + if (sepc < 0) { + mbstate_t mbs = {}; + size_t ml; + + ml = mbrlen(ifsval(), strlen(ifsval()), &mbs); + if (ml != -1 && ml != -2 && ml > 1) { + seps = ifsval(); + seplen = ml; + } + } while ((p = *ap++)) { len += strtodest(p, flags); if (*ap && sep) { len++; - memtodest(&sepc, 1, flags | EXP_KEEPNUL); + memtodest(seps, seplen, flags | EXP_KEEPNUL); } } break; @@ -1074,7 +1106,54 @@ recordregion(int start, int end, int nulonly) ifslastp->nulonly = nulonly; } +static __attribute__((noinline)) unsigned ifsisifs( + const char *p, unsigned ml, const char *ifs, size_t ifslen) +{ + bool isdefifs = false; + size_t slen = ifslen; + const char *s = ifs; + wchar_t c = *p; + bool isifs; + isifs = !c; + if (isifs) { + p = ifs; + c = *p; + slen = 0; + } + + while (slen) { + mbstate_t mbst = {}; + size_t ifsml; + wchar_t c2; + + if ((signed char)*s > 0 || + (ifsml = mbrtowc(&c2, s, slen, &mbst), + ifsml == -2 || ifsml == -1 || ifsml < 2)) { + if (c == *s) { + isifs = true; + break; + } + s++; + slen--; + continue; + } + + if (ifsml == ml && !memcmp(p, s, ifsml)) { + isifs = true; + c = c2; + break; + } + + s += ifsml; + slen -= ifsml; + } + + if (isifs) + isdefifs = iswspace(c); + + return isifs | isdefifs << 1; +} /* * Break the argument string into pieces based upon IFS and add the @@ -1086,16 +1165,16 @@ recordregion(int start, int end, int nulonly) void ifsbreakup(char *string, int maxargs, struct arglist *arglist) { + const char *ifs, *realifs; struct ifsregion *ifsp; struct strlist *sp; + char *r = NULL; + size_t ifslen; char *start; + int nulonly; + int ifsspc; char *p; char *q; - char *r = NULL; - const char *ifs, *realifs; - int ifsspc; - int nulonly; - start = string; if (ifslastp != NULL) { @@ -1110,21 +1189,27 @@ ifsbreakup(char *string, int maxargs, struct arglist *arglist) afternul = nulonly; nulonly = ifsp->nulonly; ifs = nulonly ? nullstr : realifs; + ifslen = strlen(ifs); ifsspc = 0; while (p < string + ifsp->endoff) { - int c; - bool isifs; + unsigned ifschar; + unsigned sisifs; bool isdefifs; + unsigned ml; + bool isifs; q = p; - c = *p++; - if (c == (char)CTLESC) - c = *p++; - isifs = strchr(ifs, c); - isdefifs = false; - if (isifs) - isdefifs = strchr(defifs, c); + ifschar = mbnext(p); + p += ifschar & 0xff; + ml = (ifschar >> 8) > 3 ? + (ifschar >> 8) - 2 : 0; + + sisifs = ifsisifs(p, ml, ifs, ifslen); + p += ifschar >> 8; + + isifs = sisifs & 1; + isdefifs = sisifs >> 1; /* If only reading one more argument: * If we have exactly one field, @@ -1380,32 +1465,24 @@ static void expmeta_rmescapes(char *enddir, char *name) preglob(strcpy(enddir, name), RMESCAPE_EMETA); } -static unsigned mbcharlen(char *p) -{ - int esc = 0; - - if (*++p == (char)CTLESC) - esc++; - - return esc + 3 + (unsigned char)p[esc]; -} - static int skipesc(char *p) { + unsigned short mb; int esc = 0; - if (p[esc] == (char)CTLMBCHAR) - return esc + mbcharlen(p); + mb = mbnext(p); + if ((mb >> 8) > 3) + return (mb & 0xff) + (mb >> 8) - 1; - if (*p == (char)CTLESC) - esc++; + esc = mb & 0xff; if (p[esc] == '\\' && p[esc + 1]) { esc++; - if (p[esc] == (char)CTLMBCHAR) - return esc + mbcharlen(p + esc); - if (p[esc] == (char)CTLESC) - esc++; + mb = mbnext(p + esc); + if ((mb >> 8) > 3) + return esc + (mb & 0xff) + (mb >> 8) - 1; + + esc += mb & 0xff; } return esc; @@ -1813,6 +1890,7 @@ _rmescapes(char *str, int flag) inquotes = 0; notescaped = globbing; while (*p) { + unsigned mb; unsigned ml; if (*p == (char)CTLQUOTEMARK) { @@ -1845,13 +1923,14 @@ add_escape: } notescaped = globbing; - if (*p != (char)CTLMBCHAR) + mb = mbnext(p); + ml = mb >> 8; + + if (ml <= 3) goto copy; - if (*++p == (char)CTLESC) - p++; - - ml = (unsigned char)*p++; + ml -= 2; + p += mb & 0xff; q = mempcpy(q, p, ml); p += ml + 2; continue; From patchwork Sun May 5 09:14:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654216 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3808E6FBF for ; Sun, 5 May 2024 09:14:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900486; cv=none; b=gZBXSYTJurQVfLtJcwbZcSLXCUUPvIs8z5z4IDRds/o6fykM3KvdBEU3eCAc24Maxlb7Kad/RYbe16txCgsk/EvSrJ70tDb8gEqgpvq/w/MfUluTy5J5Fi7z50MYs8kM1HM3LBcRREYD2pXPInmEYkDk3f6KKlbNdb4vEntSfd8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900486; c=relaxed/simple; bh=e6fMkV1KzRDWE3ia5oUDlujL69MRfZXpp5nblpvJEtA=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=n5jKlsf64U7TPi05xa7vrDoP2BSbnLfwRdsXS5UaNB0LdM5BGNuKh/QyIWHoR3aEuuVMcm7uvmONqkEF9CfETOykQHWJcDmfJQfTqIMmmAUXATSkjPg5y4fgXk7soydDPeLeu17e0QANOeOSJ2FBhlqUswGrVwilts5o4YQ17ys= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3XxA-00AaWe-01; Sun, 05 May 2024 17:14:41 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:40 +0800 Date: Sun, 05 May 2024 17:14:40 +0800 Message-Id: <809389416f3b39d55ff77a7f20d09f936b07d1f8.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 07/13] input: Allow MB_LEN_MAX calls to pungetc To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: In order to parse multi-byte characters which may be up to MB_LEN_MAX bytes long, allow enough calls to pungetc to undo a single multi-byte character. Also add a function pungetn to do multiple pungetc calls in a row. Signed-off-by: Herbert Xu --- src/input.c | 58 ++++++++++++++++++++++++++++++++++------------------- src/input.h | 11 +++++----- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/input.c b/src/input.c index 1c598b2..e17e067 100644 --- a/src/input.c +++ b/src/input.c @@ -56,7 +56,7 @@ #include "main.h" #include "myhistedit.h" -#define IBUFSIZ (BUFSIZ + 1) +#define IBUFSIZ (BUFSIZ + PUNGETC_MAX + 1) MKINIT struct parsefile basepf; /* top level input file */ @@ -83,13 +83,16 @@ INIT { } RESET { + int c; + /* clear input buffer */ popallfiles(); - basepf.unget = 0; - while (basepf.lastc[0] != '\n' && - basepf.lastc[0] != PEOF && - !int_pending()) - pgetc(); + + c = PEOF; + if (basepf.nextc - basebuf > basepf.unget) + c = basepf.nextc[-basepf.unget - 1]; + while (c != '\n' && c != PEOF && !int_pending()) + c = pgetc(); } FORKRESET { @@ -131,17 +134,20 @@ static int __pgetc(void) { int c; - if (parsefile->unget) - return parsefile->lastc[--parsefile->unget]; + if (parsefile->unget) { + long unget = -(long)(unsigned)parsefile->unget--; + + if (parsefile->nleft < 0) + return preadbuffer(); + + return parsefile->nextc[unget]; + } if (--parsefile->nleft >= 0) c = (signed char)*parsefile->nextc++; else c = preadbuffer(); - parsefile->lastc[1] = parsefile->lastc[0]; - parsefile->lastc[0] = c; - return c; } @@ -176,9 +182,16 @@ static int stdin_clear_nonblock(void) static int preadfd(void) { + char *buf = parsefile->buf; + int unget; int nr; - char *buf = parsefile->buf; - parsefile->nextc = buf; + + unget = parsefile->nextc - buf; + if (unget > PUNGETC_MAX) + unget = PUNGETC_MAX; + + memmove(buf, parsefile->nextc - unget, unget); + parsefile->nextc = buf += unget; retry: #ifndef SMALL @@ -196,8 +209,8 @@ retry: nr = 0; else { nr = el_len; - if (nr > IBUFSIZ - 1) - nr = IBUFSIZ - 1; + if (nr > BUFSIZ) + nr = BUFSIZ; memcpy(buf, rl_cp, nr); if (nr != el_len) { el_len -= nr; @@ -209,9 +222,9 @@ retry: } else #endif if (parsefile->fd) - nr = read(parsefile->fd, buf, IBUFSIZ - 1); + nr = read(parsefile->fd, buf, BUFSIZ); else { - unsigned len = IBUFSIZ - 1; + unsigned len = BUFSIZ; nr = 0; @@ -348,6 +361,11 @@ done: return (signed char)*parsefile->nextc++; } +void pungetn(int n) +{ + parsefile->unget += n; +} + /* * Undo a call to pgetc. Only two characters may be pushed back. * PEOF may be pushed back. @@ -356,7 +374,7 @@ done: void pungetc(void) { - parsefile->unget++; + pungetn(1); } /* @@ -383,7 +401,6 @@ pushstring(char *s, void *ap) sp->prevnleft = parsefile->nleft; sp->unget = parsefile->unget; sp->spfree = parsefile->spfree; - memcpy(sp->lastc, parsefile->lastc, sizeof(sp->lastc)); sp->ap = (struct alias *)ap; if (ap) { ((struct alias *)ap)->flag |= ALIASINUSE; @@ -413,7 +430,6 @@ static void 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; parsefile->spfree = sp; @@ -457,7 +473,7 @@ setinputfd(int fd, int push) } parsefile->fd = fd; if (parsefile->buf == NULL) - parsefile->buf = ckmalloc(IBUFSIZ); + parsefile->nextc = parsefile->buf = ckmalloc(IBUFSIZ); input_set_lleft(parsefile, parsefile->nleft = 0); plinno = 1; } diff --git a/src/input.h b/src/input.h index 1ff5773..5b4a045 100644 --- a/src/input.h +++ b/src/input.h @@ -34,12 +34,16 @@ * @(#)input.h 8.2 (Berkeley) 5/4/95 */ +#include + #ifdef SMALL #define IS_DEFINED_SMALL 1 #else #define IS_DEFINED_SMALL 0 #endif +#define PUNGETC_MAX (MB_LEN_MAX > 16 ? MB_LEN_MAX : 16) + /* PEOF (the end of file marker) is defined in syntax.h */ enum { @@ -59,9 +63,6 @@ struct strpush { /* Delay freeing so we can stop nested aliases. */ struct strpush *spfree; - /* Remember last two characters for pungetc. */ - int lastc[2]; - /* Number of outstanding calls to pungetc. */ int unget; }; @@ -87,9 +88,6 @@ struct parsefile { /* Delay freeing so we can stop nested aliases. */ struct strpush *spfree; - /* Remember last two characters for pungetc. */ - int lastc[2]; - /* Number of outstanding calls to pungetc. */ int unget; }; @@ -106,6 +104,7 @@ extern struct parsefile *parsefile; int pgetc(void); int pgetc2(void); void pungetc(void); +void pungetn(int); void pushstring(char *, void *); int setinputfile(const char *, int); void setinputstring(char *); From patchwork Sun May 5 09:14:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654217 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 672CB6FC7 for ; Sun, 5 May 2024 09:14:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900488; cv=none; b=JnCkAEIvDA6HLuHQJ0bWKQS8Zqq5dIA3LIPRGtyGL4/ZOblANl3EsGVT9R1H8vd7RAJNidsyF39Vv/bzcIzqyhIsG5+JRPewXYGsuD/YaGPxgrZ3W8/y0Ev4ADfBd2LaRQlBpzZOXX2TDDYqofBclLKYpPb9Ky5HcLN4XPZciAk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900488; c=relaxed/simple; bh=mDQ9ZCbicr5SFLv6A+EEB8nvfMcv5Am7kAM0I2z4Evs=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=tof7iHO0ri7GJTBxmQidOE/YJn4A8MNImBrzZphs2PUUw6nSDC2arSSJDaARRJZjIFrLumPRzvdhCmHXNu7tTZg03gam9c4KAM7eSeuI/L52wtXwsMFnw8I8UhmC1zQrZO5SbcYI4ocCwj1Fh+oCxJxCOl0t8ehoJm5IVcPtvKM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3XxC-00AaWp-19; Sun, 05 May 2024 17:14:43 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:42 +0800 Date: Sun, 05 May 2024 17:14:42 +0800 Message-Id: <91d6bef13f4cca0029d879ba44650dc2b15199d1.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 08/13] input: Add pgetc_eoa To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: This reintroduces PEOA in a limited way. Instead of allowing pgetc to return it, limit it to a new function pgetc_eoa so only specific callers need to deal with PEOA. Signed-off-by: Herbert Xu --- src/input.c | 8 +++++++- src/input.h | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/input.c b/src/input.c index e17e067..bedc581 100644 --- a/src/input.c +++ b/src/input.c @@ -157,7 +157,7 @@ static int __pgetc(void) * Nul characters in the input are silently discarded. */ -int pgetc(void) +int __attribute__((noinline)) pgetc(void) { struct strpush *sp = parsefile->spfree; @@ -167,6 +167,12 @@ int pgetc(void) return __pgetc(); } +int pgetc_eoa(void) +{ + return parsefile->strpush && parsefile->nleft == -1 && + parsefile->strpush->ap ? PEOA : pgetc(); +} + static int stdin_clear_nonblock(void) { int flags = fcntl(0, F_GETFL, 0); diff --git a/src/input.h b/src/input.h index 5b4a045..151b1c6 100644 --- a/src/input.h +++ b/src/input.h @@ -45,6 +45,7 @@ #define PUNGETC_MAX (MB_LEN_MAX > 16 ? MB_LEN_MAX : 16) /* PEOF (the end of file marker) is defined in syntax.h */ +#define PEOA ((PEOF) - 1) enum { INPUT_PUSH_FILE = 1, @@ -102,7 +103,7 @@ extern struct parsefile *parsefile; #define plinno (parsefile->linno) int pgetc(void); -int pgetc2(void); +int pgetc_eoa(void); void pungetc(void); void pungetn(int); void pushstring(char *, void *); From patchwork Sun May 5 09:14:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654218 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 853CB6FC5 for ; Sun, 5 May 2024 09:14:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900490; cv=none; b=P2b9hD+VMzFS/BWWZFHkl2QNlfCv6hfK+4T58Vh6leXk0bYN8poQ4syUxELa74rQqMjzMSfptY6c0VNw92iBIsn5v7faJLGR4tHCXE24fHzzvD75sopZWNVtHG3KV8rZbM4YFyJSAPLBNmeazl9AM/RUEUVznRiIsu1Q7CaGd0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900490; c=relaxed/simple; bh=QmHsZph8Q1wX9NCW6D8ApcTfhqAcnMzCtdkS7qRSe64=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=pPPWmnS7sgb6bMsYl3gSkWIAIuswB3qs4+o6fg+JJDB3EPEUeqsoCQGdzkV3mHn70R4R8okmDyl160gn20Zcl+H+9ozQx0qHMJVchZxuVwqN1TlGc3VA+lE3/e1F0riEwY0t5w3+fKHsMD9CXXFknG0zAqym8NTiliQb68anfxM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3XxE-00AaXT-2A; Sun, 05 May 2024 17:14:45 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:14:45 +0800 Date: Sun, 05 May 2024 17:14:45 +0800 Message-Id: In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 09/13] parser: Add support for multi-byte characters To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Add the requisite markers for multi-byte characters so that the expansion code can recognise them. Also allow wide blank characters to terminate words. Signed-off-by: Herbert Xu --- src/expand.c | 19 +++++++ src/mktokens | 1 + src/parser.c | 136 +++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 129 insertions(+), 27 deletions(-) diff --git a/src/expand.c b/src/expand.c index dd2b71e..402289f 100644 --- a/src/expand.c +++ b/src/expand.c @@ -265,6 +265,7 @@ static char *argstr(char *p, int flag) CTLESC, CTLVAR, CTLBACKQ, + CTLMBCHAR, CTLARI, CTLENDARI, 0 @@ -289,6 +290,8 @@ tilde: start: startloc = expdest - (char *)stackblock(); for (;;) { + unsigned ml; + unsigned mb; int end; length += strcspn(p + length, reject); @@ -351,6 +354,22 @@ addquote: startloc++; } break; + case CTLMBCHAR: + c = (signed char)*p--; + mb = mbnext(p); + ml = (mb >> 8) - 2; + if (flag & QUOTES_ESC) { + length = (mb >> 8) + (mb & 0xff); + if (c == (char)CTLESC) + startloc += length; + break; + } + if (c == CTLESC) + startloc += ml; + p += mb & 0xff; + expdest = stnputs(p, ml, expdest); + p += mb >> 8; + break; case CTLESC: startloc++; length++; diff --git a/src/mktokens b/src/mktokens index 78055be..dcef676 100644 --- a/src/mktokens +++ b/src/mktokens @@ -41,6 +41,7 @@ cat > "${TMPDIR}"/ka$$ <<\! TEOF 1 end of file +TBLANK 0 blank TNL 0 newline TSEMI 0 ";" TBACKGND 0 "&" diff --git a/src/parser.c b/src/parser.c index 27611f0..71d61f3 100644 --- a/src/parser.c +++ b/src/parser.c @@ -36,7 +36,11 @@ #include #endif +#include +#include #include +#include +#include #include "shell.h" #include "parser.h" @@ -801,6 +805,8 @@ xxreadtoken(void) setprompt(2); } for (;;) { /* until token or start of word found */ + int tok; + c = pgetc_eatbnl(); switch (c) { case ' ': case '\t': @@ -834,9 +840,10 @@ xxreadtoken(void) case ')': RETURN(TRP); } - break; + tok = readtoken1(c, BASESYNTAX, (char *)NULL, 0); + if (tok != TBLANK) + return tok; } - return readtoken1(c, BASESYNTAX, (char *)NULL, 0); #undef RETURN } @@ -876,7 +883,53 @@ static void synstack_pop(struct synstack **stack) *stack = (*stack)->next; } +static unsigned getmbc(int c, char *out, int mode) +{ + char *const start = out; + mbstate_t mbst = {}; + unsigned ml = 0; + size_t ml2; + wchar_t wc; + char *mbc; + if (likely(c >= 0)) + return 0; + + mbc = (mode & 3) < 2 ? out + 2 + (mode == 1) : out; + mbc[ml] = c; + while ((ml2 = mbrtowc(&wc, mbc + ml++, 1, &mbst)) == -2) { + if (ml >= MB_LEN_MAX) + break; + c = pgetc_eoa(); + if (c == PEOA || c == PEOF) + break; + mbc[ml] = c; + } + + if (ml2 == 1 && ml > 1) { + if (mode == 4 && iswblank(wc)) + return 1; + + if ((mode & 3) < 2) { + USTPUTC(CTLMBCHAR, out); + if (mode == 1) + USTPUTC(CTLESC, out); + USTPUTC(ml, out); + } + STADJUST(ml, out); + if ((mode & 3) < 2) { + USTPUTC(ml, out); + USTPUTC(CTLMBCHAR, out); + } + + return out - start; + } + + if (ml > 1) + pungetn(ml - 1); + + return 0; +} /* * If eofmark is NULL, read a word or a redirection symbol. If eofmark @@ -929,12 +982,29 @@ readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) } #endif CHECKEND(); /* set c to PEOF if at end of here document */ - for (;;) { /* until end of line or end of word */ - CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ + /* Until end of line or end of word */ + for (;; c = pgetc_top(synstack)) { + int fieldsplitting; + unsigned ml; + + /* Permit max(MB_LEN_MAX, 23) calls to USTPUTC. */ + CHECKSTRSPACE((MB_LEN_MAX > 16 ? MB_LEN_MAX : 16) + 7, + out); + fieldsplitting = synstack->syntax == BASESYNTAX && + !synstack->varnest ? 4 : 0; + ml = getmbc(c, out, fieldsplitting); + if (ml == 1) { + if (out == stackblock()) + return TBLANK; + c = pgetc(); + break; + } + out += ml; + if (ml) + continue; switch(synstack->syntax[c]) { case CNL: /* '\n' */ - if (synstack->syntax == BASESYNTAX && - !synstack->varnest) + if (fieldsplitting) goto endword; /* exit outer loop */ USTPUTC(c, out); nlprompt(); @@ -956,26 +1026,33 @@ readtoken1(int firstc, char const *syntax, char *eofmark, int striptabs) USTPUTC(CTLESC, out); USTPUTC('\\', out); pungetc(); - } else { - if ( - synstack->dblquote && - c != '\\' && c != '`' && - c != '$' && ( - c != '"' || - (eofmark != NULL && - !synstack->varnest) - ) && ( - c != '}' || - !synstack->varnest - ) - ) { - USTPUTC(CTLESC, out); - USTPUTC('\\', out); - } - USTPUTC(CTLESC, out); - USTPUTC(c, out); - quotef++; + break; } + + if ( + synstack->dblquote && + c != '\\' && c != '`' && + c != '$' && ( + c != '"' || + (eofmark != NULL && + !synstack->varnest) + ) && ( + c != '}' || + !synstack->varnest + ) + ) { + USTPUTC(CTLESC, out); + USTPUTC('\\', out); + } + quotef++; + + ml = getmbc(c, out, 1); + out += ml; + if (ml) + break; + + USTPUTC(CTLESC, out); + USTPUTC(c, out); break; case CSQUOTE: synstack->syntax = SQSYNTAX; @@ -1053,11 +1130,10 @@ toggledq: case CEOF: goto endword; /* exit outer loop */ default: - if (synstack->varnest == 0) + if (fieldsplitting) goto endword; /* exit outer loop */ USTPUTC(c, out); } - c = pgetc_top(synstack); } } endword: @@ -1384,6 +1460,7 @@ parsebackq: { size_t psavelen; size_t savelen; union node *n; + unsigned ml; char *pstr; char *str; @@ -1415,6 +1492,11 @@ parsebackq: { if (pc != '\\' && pc != '`' && pc != '$' && (!synstack->dblquote || pc != '"')) STPUTC('\\', pout); + CHECKSTRSPACE(MB_LEN_MAX, pout); + ml = getmbc(pc, pout, 2); + pout += ml; + if (ml) + continue; break; case PEOF: From patchwork Sun May 5 09:15:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654219 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C9AB567F for ; Sun, 5 May 2024 09:15:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900525; cv=none; b=R0QLnP05qrGEQm4G/zhAVzBnUAVYMBIu2GIwDnb6lXygmkUSjR6hPIFuFnqd82xvDwWHeIVMvlM5xxEbYDAzIeqEswPk6EJI5T2fiV9yrMjM+sYeHblACeHLAoSZ4Kns74Xthb+wKSNMCz8UFr1hya82ifxh+Zqyee4A392FMPc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900525; c=relaxed/simple; bh=a0/Bhd8zTCS6Pf4Cue7MBMpFXoAwS0W8kMwIM2hgVeM=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=NZD7F1L3JWkzEN7mq93uoXDNLz/1/tjQ8MFLe2O7El+0fV/ZPgymO006oR5HlK8qc5QEk1TivGKTNrDYzYS5lbhdWg18ZfBaKGk1Uggi4g+AK/B0LIx/svMJ94YQcHJbfB8nNC2dpbAZLksWJcs+Ju0XIP/+TqJB095eeb3VdsM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xxn-00Aab9-20; Sun, 05 May 2024 17:15:20 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:15:20 +0800 Date: Sun, 05 May 2024 17:15:20 +0800 Message-Id: <8d9afe982b262ddee001fcaca4bec5de5958ad91.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 10/13] input: Always push in setinputfile To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Push the input file even in the case of "sh file". This is because the base parsefile will be used for read(1). Signed-off-by: Herbert Xu --- src/input.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/input.c b/src/input.c index bedc581..1712e5f 100644 --- a/src/input.c +++ b/src/input.c @@ -61,6 +61,7 @@ MKINIT struct parsefile basepf; /* top level input file */ MKINIT char basebuf[IBUFSIZ]; /* buffer for top level input file */ +MKINIT struct parsefile *toppf = &basepf; struct parsefile *parsefile = &basepf; /* current input file */ int whichprompt; /* 1 == PS1, 2 == PS2 */ @@ -89,8 +90,8 @@ RESET { popallfiles(); c = PEOF; - if (basepf.nextc - basebuf > basepf.unget) - c = basepf.nextc[-basepf.unget - 1]; + if (toppf->nextc - toppf->buf > toppf->unget) + c = toppf->nextc[-toppf->unget - 1]; while (c != '\n' && c != PEOF && !int_pending()) c = pgetc(); } @@ -473,13 +474,11 @@ out: static void setinputfd(int fd, int push) { - if (push) { - pushfile(); - parsefile->buf = 0; - } + pushfile(); + if (!push) + toppf = parsefile; parsefile->fd = fd; - if (parsefile->buf == NULL) - parsefile->nextc = parsefile->buf = ckmalloc(IBUFSIZ); + parsefile->nextc = parsefile->buf = ckmalloc(IBUFSIZ); input_set_lleft(parsefile, parsefile->nleft = 0); plinno = 1; } @@ -560,5 +559,5 @@ void unwindfiles(struct parsefile *stop) void popallfiles(void) { - unwindfiles(&basepf); + unwindfiles(toppf); } From patchwork Sun May 5 09:15:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654220 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DA89567F for ; Sun, 5 May 2024 09:15:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900527; cv=none; b=QUCC6678F0HSDtdRERFIATnyYZbEMcQUegNraoetgUO46DjcpRX3NjRZCblgHI7tT76XnW3u4LB2eTFx1TvTp+dEjS5WEGZobFYwwFEOgsREy2m9xHqpTFO16aZ9mQNpJhfWYJfeSl74tBJItPPD8NDNAtRqRJoCjLQmVgytga8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900527; c=relaxed/simple; bh=5R8h8+ULyTNzfFyzruhP+HZUtKmFtp8dJvKJxrOW9h8=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=pQxPj1zwcqCuGTIN07iwLXpCDHO3UlY9KiDgauLgmmMYOWB6a+a193PX7KYXqQQ/wuMACaAsbQfbMRwAZZyTfblqAP94mFgUemBl5aUie3T6EexK9xh2LjtArs3Dyvk7+Yw+ziIkKtJxlkdc7k48wASIahgUNXm2sdwGUmheawQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xxp-00AabI-2t; Sun, 05 May 2024 17:15:22 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:15:22 +0800 Date: Sun, 05 May 2024 17:15:22 +0800 Message-Id: <2c7cd8666ab0880f282e1259ee74adb43754285a.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 11/13] memalloc: Use void * instead of pointer To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Use void * instead of the obsolete pointer typedef. Signed-off-by: Herbert Xu --- src/memalloc.c | 16 ++++++---------- src/memalloc.h | 10 +++++----- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/memalloc.c b/src/memalloc.c index 60637da..7aa8c58 100644 --- a/src/memalloc.c +++ b/src/memalloc.c @@ -47,10 +47,9 @@ * Like malloc, but returns an error when out of space. */ -pointer -ckmalloc(size_t nbytes) +void *ckmalloc(size_t nbytes) { - pointer p; + void *p; p = malloc(nbytes); if (p == NULL) @@ -63,8 +62,7 @@ ckmalloc(size_t nbytes) * Same for realloc. */ -pointer -ckrealloc(pointer p, size_t nbytes) +void *ckrealloc(void *p, size_t nbytes) { p = realloc(p, nbytes); if (p == NULL) @@ -110,8 +108,7 @@ char *stacknxt = stackbase.space; size_t stacknleft = MINSIZE; char *sstrend = stackbase.space + MINSIZE; -pointer -stalloc(size_t nbytes) +void *stalloc(size_t nbytes) { char *p; size_t aligned; @@ -144,8 +141,7 @@ stalloc(size_t nbytes) } -void -stunalloc(pointer p) +void stunalloc(void *p) { #ifdef DEBUG if (!p || (stacknxt < (char *)p) || ((char *)p < stackp->space)) { @@ -221,7 +217,7 @@ static void growstackblock(size_t min) sp = stackp; prevstackp = sp->prev; grosslen = newlen + sizeof(struct stack_block) - MINSIZE; - sp = ckrealloc((pointer)sp, grosslen); + sp = ckrealloc(sp, grosslen); sp->prev = prevstackp; stackp = sp; stacknxt = sp->space; diff --git a/src/memalloc.h b/src/memalloc.h index b9adf76..a7f7996 100644 --- a/src/memalloc.h +++ b/src/memalloc.h @@ -48,11 +48,11 @@ extern char *stacknxt; extern size_t stacknleft; extern char *sstrend; -pointer ckmalloc(size_t); -pointer ckrealloc(pointer, size_t); +void *ckmalloc(size_t); +void *ckrealloc(void *, size_t); char *savestr(const char *); -pointer stalloc(size_t); -void stunalloc(pointer); +void *stalloc(size_t); +void stunalloc(void *); void pushstackmark(struct stackmark *mark, size_t len); void setstackmark(struct stackmark *); void popstackmark(struct stackmark *); @@ -98,4 +98,4 @@ static inline char *_STPUTC(int c, char *p) { #define ungrabstackstr(s, p) stunalloc((s)) #define stackstrend() ((void *)sstrend) -#define ckfree(p) free((pointer)(p)) +#define ckfree(p) free((void *)(p)) From patchwork Sun May 5 09:15:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654221 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69D48567F for ; Sun, 5 May 2024 09:15:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900530; cv=none; b=KAZhqPZroEUS4ad9nVtmLRuBfhq+ECPQd/5Jsysacb2aSzvXjV6B81VKKWP9R0MhLObJH5y1sn8JRpV3EF7rcppm2DrTzB7UQo7z2LdqNRfF3xFVyK62Mtk8fbVdvj6c1x3eBJ9yhuLJZlj0JQVAXFNGnLfAW+HXy0x4Dw+Dui8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900530; c=relaxed/simple; bh=Fc2IWehlzBuHjHueNdATIJlsfIzWttnfPNdiYfwlIIQ=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=fdqU7Zt2KqOpW/+7wHgQm1eKYM2hFEGf2/K5fEIRmRrX6iXtF5eiH0kn5x57Bb7bOH4SpBM8jQ+JaCLqKZ66MyoE+E9KGpQzx4MpIQSkqEW9yfwpKuRxkBknxKgjcb4FNmPeDP/R8cWDmB/rL1YDTh28UJwIqBmUm7TFIBakWHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xxs-00Aabp-0g; Sun, 05 May 2024 17:15:25 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:15:24 +0800 Date: Sun, 05 May 2024 17:15:24 +0800 Message-Id: <796a2b5732f4ac033de84ce0afcab4b36ff62296.1714900377.git.herbert@gondor.apana.org.au> In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 12/13] builtin: Use pgetc in read(1) To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Use pgetc instead of read(2) in read(1). This allows any future buffering in the input layer to be used by read(1). This also allows read(1) to call helpers in the parser that may use the input layer. Signed-off-by: Herbert Xu --- src/input.c | 42 ++++++++++++++++++++++++++++-------------- src/input.h | 1 + src/miscbltin.c | 39 +++++++++++++++++++-------------------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/input.c b/src/input.c index 1712e5f..6779069 100644 --- a/src/input.c +++ b/src/input.c @@ -42,19 +42,20 @@ * This file implements the input routines used by the parser. */ -#include "eval.h" -#include "shell.h" -#include "redir.h" -#include "syntax.h" -#include "input.h" -#include "output.h" -#include "options.h" -#include "memalloc.h" -#include "error.h" #include "alias.h" -#include "parser.h" +#include "error.h" +#include "eval.h" +#include "input.h" #include "main.h" +#include "memalloc.h" #include "myhistedit.h" +#include "options.h" +#include "output.h" +#include "parser.h" +#include "redir.h" +#include "shell.h" +#include "syntax.h" +#include "trap.h" #define IBUFSIZ (BUFSIZ + PUNGETC_MAX + 1) @@ -258,7 +259,7 @@ retry: } if (nr < 0) { - if (errno == EINTR) + if (errno == EINTR && !(basepf.prev && pending_sig)) goto retry; } return nr; @@ -522,6 +523,13 @@ pushfile(void) parsefile = pf; } +void pushstdin(void) +{ + INTOFF; + basepf.prev = parsefile; + parsefile = &basepf; + INTON; +} void popfile(void) @@ -529,6 +537,11 @@ popfile(void) struct parsefile *pf = parsefile; INTOFF; + parsefile = pf->prev; + pf->prev = NULL; + if (pf == &basepf) + goto out; + if (pf->fd >= 0) close(pf->fd); if (pf->buf) @@ -539,15 +552,16 @@ popfile(void) popstring(); freestrings(parsefile->spfree); } - parsefile = pf->prev; ckfree(pf); + +out: INTON; } -void unwindfiles(struct parsefile *stop) +void __attribute__((noinline)) unwindfiles(struct parsefile *stop) { - while (parsefile != stop) + while (basepf.prev || parsefile != stop) popfile(); } diff --git a/src/input.h b/src/input.h index 151b1c6..c59d784 100644 --- a/src/input.h +++ b/src/input.h @@ -109,6 +109,7 @@ void pungetn(int); void pushstring(char *, void *); int setinputfile(const char *, int); void setinputstring(char *); +void pushstdin(void); void popfile(void); void unwindfiles(struct parsefile *); void popallfiles(void); diff --git a/src/miscbltin.c b/src/miscbltin.c index 8a0ddf4..10d256e 100644 --- a/src/miscbltin.c +++ b/src/miscbltin.c @@ -46,18 +46,20 @@ #include #include -#include "shell.h" -#include "options.h" -#include "var.h" -#include "output.h" -#include "memalloc.h" #include "error.h" +#include "expand.h" +#include "input.h" +#include "memalloc.h" #include "miscbltin.h" #include "mystring.h" #include "main.h" -#include "expand.h" +#include "options.h" +#include "output.h" #include "parser.h" +#include "shell.h" +#include "syntax.h" #include "trap.h" +#include "var.h" #undef rflag @@ -115,14 +117,13 @@ readcmd_handle_line(char *s, int ac, char **ap) int readcmd(int argc, char **argv) { - char **ap; - char c; - int rflag; char *prompt; - char *p; int startloc; int newloc; int status; + char **ap; + int rflag; + char *p; int i; rflag = 0; @@ -145,19 +146,17 @@ readcmd(int argc, char **argv) status = 0; STARTSTACKSTR(p); + pushstdin(); + goto start; for (;;) { - switch (read(0, &c, 1)) { - case 1: - break; - default: - if (errno == EINTR && !pending_sig) - continue; - /* fall through */ - case 0: + int c; + + c = pgetc(); + if (c == PEOF) { status = 1; - goto out; + break; } if (c == '\0') continue; @@ -186,7 +185,7 @@ start: newloc = startloc - 1; } } -out: + popfile(); recordregion(startloc, p - (char *)stackblock(), 0); STACKSTRNUL(p); readcmd_handle_line(p + 1, argc - (ap - argv), ap); From patchwork Sun May 5 09:15:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 13654222 X-Patchwork-Delegate: herbert@gondor.apana.org.au Received: from abb.hmeau.com (abb.hmeau.com [144.6.53.87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82C56567F for ; Sun, 5 May 2024 09:15:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=144.6.53.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900532; cv=none; b=eL8iAUbgZPZ9984fwsSRUgAvZd5Lv5a1oTwXE2nRKRyUQEvWNfuz/9gU6AfsCDDsv/LJ3H+4gwEkaYngy7vh2LLJFu0LO9M8UysxhMDou8oYdDqBGtLNA9k35PHVPOWA7fcYKcaaU/2DczGZjB5gCrGCzzyaRPJkqYXySPk0jT0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714900532; c=relaxed/simple; bh=hDxYgMZoW6xdz3Ij8l6UKInfYhu0ULqIOxuPdaYDmXM=; h=Date:Message-Id:In-Reply-To:References:From:Subject:To; b=pT5Pczm0RbpQWEuIxgla/TiMXo/MBm5K0zWwus1qXHBKMnLOEbZyuuVuMwNcdMMXLPE3T3VhBSXFsns5N0JZzGOJlCqK/FfgDiL1e/L3cXDsE3IwuSzmxDqSszhNR+7qYvHdcROCrrtZqsY9lq0QuTfvNTzWPsIKe+hJrPMstQk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au; spf=pass smtp.mailfrom=gondor.apana.org.au; arc=none smtp.client-ip=144.6.53.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gondor.apana.org.au Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gondor.apana.org.au Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.96 #2 (Debian)) id 1s3Xxu-00Aac1-1Z; Sun, 05 May 2024 17:15:27 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Sun, 05 May 2024 17:15:26 +0800 Date: Sun, 05 May 2024 17:15:26 +0800 Message-Id: In-Reply-To: References: From: Herbert Xu Subject: [v3 PATCH 13/13] builtin: Process multi-byte characters in read(1) To: DASH Mailing List Precedence: bulk X-Mailing-List: dash@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Add support for multi-byte characters in read(1) by using getmbc from the parser. Signed-off-by: Herbert Xu --- src/miscbltin.c | 19 +++++++++++++------ src/parser.c | 2 +- src/parser.h | 1 + 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/miscbltin.c b/src/miscbltin.c index 10d256e..5aa2b24 100644 --- a/src/miscbltin.c +++ b/src/miscbltin.c @@ -36,15 +36,16 @@ * Miscelaneous builtins. */ +#include +#include +#include +#include #include /* quad_t */ #include /* BSD4_4 */ #include #include #include #include -#include -#include -#include #include "error.h" #include "expand.h" @@ -151,8 +152,10 @@ readcmd(int argc, char **argv) goto start; for (;;) { + unsigned ml; int c; + CHECKSTRSPACE((MB_LEN_MAX > 16 ? MB_LEN_MAX : 16) + 4, p); c = pgetc(); if (c == PEOF) { status = 1; @@ -160,9 +163,14 @@ readcmd(int argc, char **argv) } if (c == '\0') continue; + ml = getmbc(c, p, 0); + if (ml) { + p += ml; + goto record; + } if (newloc >= startloc) { if (c == '\n') - goto resetbs; + goto record; goto put; } if (!rflag && c == '\\') { @@ -172,13 +180,12 @@ readcmd(int argc, char **argv) if (c == '\n') break; put: - CHECKSTRSPACE(2, p); if (strchr(qchars, c)) USTPUTC(CTLESC, p); USTPUTC(c, p); +record: if (newloc >= startloc) { -resetbs: recordregion(startloc, newloc, 0); start: startloc = p - (char *)stackblock(); diff --git a/src/parser.c b/src/parser.c index 71d61f3..d368adc 100644 --- a/src/parser.c +++ b/src/parser.c @@ -883,7 +883,7 @@ static void synstack_pop(struct synstack **stack) *stack = (*stack)->next; } -static unsigned getmbc(int c, char *out, int mode) +unsigned getmbc(int c, char *out, int mode) { char *const start = out; mbstate_t mbst = {}; diff --git a/src/parser.h b/src/parser.h index 14bfc4f..7a9605b 100644 --- a/src/parser.h +++ b/src/parser.h @@ -95,6 +95,7 @@ const char *getprompt(void *); const char *const *findkwd(const char *); char *endofname(const char *); const char *expandstr(const char *); +unsigned getmbc(int c, char *out, int mode); static inline int goodname(const char *p)