ash: expand: Merge syntax/quotes in memtodest with flags

Upstream commit:

    Date: Mon, 28 May 2018 00:17:39 +0800
    expand: Merge syntax/quotes in memtodest with flags

    The function arguments syntax and quotes are both derived from
    the expansion flags.  As syntax is only used by memtodest we do
    not need to maintain it outside of the function at all.

    The only place that uses something other than BASESYNTAX or DQSYNTAX
    is exptilde.  However in that case DQSYNTAX has exactly the same
    effect as SQSYNTAX.

    This patch merges these two arguments into a single flags.  The
    macro QUOTES_KEEPNUL has been renamed to EXP_KEEPNUL in order
    to keep the namespace separate.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2020-02-20 10:06:20 +01:00
parent e2dd2afc8e
commit ecc85832f8

View File

@ -6030,6 +6030,8 @@ static int substr_atoi(const char *s)
#define EXP_VARTILDE2 0x20 /* expand tildes after colons only */ #define EXP_VARTILDE2 0x20 /* expand tildes after colons only */
#define EXP_WORD 0x40 /* expand word in parameter expansion */ #define EXP_WORD 0x40 /* expand word in parameter expansion */
#define EXP_QUOTED 0x100 /* expand word in double quotes */ #define EXP_QUOTED 0x100 /* expand word in double quotes */
#define EXP_KEEPNUL 0x200 /* do not skip NUL characters */
/* /*
* rmescape() flags * rmescape() flags
*/ */
@ -6040,8 +6042,6 @@ static int substr_atoi(const char *s)
/* Add CTLESC when necessary. */ /* Add CTLESC when necessary. */
#define QUOTES_ESC (EXP_FULL | EXP_CASE) #define QUOTES_ESC (EXP_FULL | EXP_CASE)
/* Do not skip NUL characters. */
#define QUOTES_KEEPNUL EXP_TILDE
/* /*
* Structure specifying which parts of the string should be searched * Structure specifying which parts of the string should be searched
@ -6348,27 +6348,28 @@ preglob(const char *pattern, int flag)
* Put a string on the stack. * Put a string on the stack.
*/ */
static void static void
memtodest(const char *p, size_t len, int syntax, int quotes) memtodest(const char *p, size_t len, int flags)
{ {
int syntax = flags & EXP_QUOTED ? DQSYNTAX : BASESYNTAX;
char *q; char *q;
if (!len) if (!len)
return; return;
q = makestrspace((quotes & QUOTES_ESC) ? len * 2 : len, expdest); q = makestrspace(len * 2, expdest);
do { do {
unsigned char c = *p++; unsigned char c = *p++;
if (c) { if (c) {
if (quotes & QUOTES_ESC) { if (flags & QUOTES_ESC) {
int n = SIT(c, syntax); int n = SIT(c, syntax);
if (n == CCTL if (n == CCTL
|| (syntax != BASESYNTAX && n == CBACK) || ((flags & EXP_QUOTED) && n == CBACK)
) { ) {
USTPUTC(CTLESC, q); USTPUTC(CTLESC, q);
} }
} }
} else if (!(quotes & QUOTES_KEEPNUL)) } else if (!(flags & EXP_KEEPNUL))
continue; continue;
USTPUTC(c, q); USTPUTC(c, q);
} while (--len); } while (--len);
@ -6377,10 +6378,10 @@ memtodest(const char *p, size_t len, int syntax, int quotes)
} }
static size_t static size_t
strtodest(const char *p, int syntax, int quotes) strtodest(const char *p, int flags)
{ {
size_t len = strlen(p); size_t len = strlen(p);
memtodest(p, len, syntax, quotes); memtodest(p, len, flags);
return len; return len;
} }
@ -6448,13 +6449,12 @@ removerecordregions(int endoff)
} }
static char * static char *
exptilde(char *startp, char *p, int flags) exptilde(char *startp, char *p, int flag)
{ {
unsigned char c; unsigned char c;
char *name; char *name;
struct passwd *pw; struct passwd *pw;
const char *home; const char *home;
int quotes = flags & QUOTES_ESC;
name = p + 1; name = p + 1;
@ -6465,7 +6465,7 @@ exptilde(char *startp, char *p, int flags)
case CTLQUOTEMARK: case CTLQUOTEMARK:
return startp; return startp;
case ':': case ':':
if (flags & EXP_VARTILDE) if (flag & EXP_VARTILDE)
goto done; goto done;
break; break;
case '/': case '/':
@ -6486,7 +6486,7 @@ exptilde(char *startp, char *p, int flags)
if (!home) if (!home)
goto lose; goto lose;
*p = c; *p = c;
strtodest(home, SQSYNTAX, quotes); strtodest(home, flag | EXP_QUOTED);
return p; return p;
lose: lose:
*p = c; *p = c;
@ -6586,7 +6586,6 @@ expbackq(union node *cmd, int flag)
char *p; char *p;
char *dest; char *dest;
int startloc; int startloc;
int syntax = flag & EXP_QUOTED ? DQSYNTAX : BASESYNTAX;
struct stackmark smark; struct stackmark smark;
INT_OFF; INT_OFF;
@ -6600,7 +6599,7 @@ expbackq(union node *cmd, int flag)
if (i == 0) if (i == 0)
goto read; goto read;
for (;;) { for (;;) {
memtodest(p, i, syntax, flag & QUOTES_ESC); memtodest(p, i, flag);
read: read:
if (in.fd < 0) if (in.fd < 0)
break; break;
@ -7309,11 +7308,10 @@ varvalue(char *name, int varflags, int flags, int quoted)
int sep; int sep;
int subtype = varflags & VSTYPE; int subtype = varflags & VSTYPE;
int discard = subtype == VSPLUS || subtype == VSLENGTH; int discard = subtype == VSPLUS || subtype == VSLENGTH;
int quotes = (discard ? 0 : (flags & QUOTES_ESC)) | QUOTES_KEEPNUL;
int syntax;
flags |= EXP_KEEPNUL;
flags &= discard ? ~QUOTES_ESC : ~0;
sep = (flags & EXP_FULL) << CHAR_BIT; sep = (flags & EXP_FULL) << CHAR_BIT;
syntax = quoted ? DQSYNTAX : BASESYNTAX;
switch (*name) { switch (*name) {
case '$': case '$':
@ -7379,11 +7377,11 @@ varvalue(char *name, int varflags, int flags, int quoted)
if (!ap) if (!ap)
return -1; return -1;
while ((p = *ap++) != NULL) { while ((p = *ap++) != NULL) {
len += strtodest(p, syntax, quotes); len += strtodest(p, flags);
if (*ap && sep) { if (*ap && sep) {
len++; len++;
memtodest(&sepc, 1, syntax, quotes); memtodest(&sepc, 1, flags);
} }
} }
break; break;
@ -7410,7 +7408,7 @@ varvalue(char *name, int varflags, int flags, int quoted)
if (!p) if (!p)
return -1; return -1;
len = strtodest(p, syntax, quotes); len = strtodest(p, flags);
#if ENABLE_UNICODE_SUPPORT #if ENABLE_UNICODE_SUPPORT
if (subtype == VSLENGTH && len > 0) { if (subtype == VSLENGTH && len > 0) {
reinit_unicode_for_ash(); reinit_unicode_for_ash();