aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2015-05-18 09:49:28 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2015-05-18 09:49:28 +0200
commitd68d1fbd6cb31a61975112acb1c792735a063847 (patch)
tree8d4ae136528752a24ba40e612dc13a2ef5d1f8d9 /shell
parente0a4e107aa9d1e03546c090b07874dff1865705e (diff)
downloadbusybox-w32-d68d1fbd6cb31a61975112acb1c792735a063847.tar.gz
busybox-w32-d68d1fbd6cb31a61975112acb1c792735a063847.tar.bz2
busybox-w32-d68d1fbd6cb31a61975112acb1c792735a063847.zip
ash: code shrink around varvalue
Based on commit c989d72 from git://git.kernel.org/pub/scm/utils/dash/dash.git by Herbert Xu function old new delta strtodest - 40 +40 memtodest 123 147 +24 parse_command 1443 1440 -3 readtoken1 3205 3199 -6 argstr 1203 1180 -23 varvalue 788 660 -128 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/4 up/down: 64/-160) Total: -96 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
-rw-r--r--shell/ash.c84
1 files changed, 38 insertions, 46 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 7af8842bb..a81922add 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -5539,6 +5539,11 @@ ash_arith(const char *s)
5539#define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ 5539#define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */
5540#define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ 5540#define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */
5541 5541
5542/* Add CTLESC when necessary. */
5543#define QUOTES_ESC (EXP_FULL | EXP_CASE | EXP_REDIR)
5544/* Do not skip NUL characters. */
5545#define QUOTES_KEEPNUL EXP_TILDE
5546
5542/* 5547/*
5543 * Structure specifying which parts of the string should be searched 5548 * Structure specifying which parts of the string should be searched
5544 * for IFS characters. 5549 * for IFS characters.
@@ -5695,29 +5700,34 @@ preglob(const char *pattern, int quoted, int flag)
5695static void 5700static void
5696memtodest(const char *p, size_t len, int syntax, int quotes) 5701memtodest(const char *p, size_t len, int syntax, int quotes)
5697{ 5702{
5698 char *q = expdest; 5703 char *q;
5704
5705 if (!len)
5706 return;
5699 5707
5700 q = makestrspace(quotes ? len * 2 : len, q); 5708 q = makestrspace((quotes & QUOTES_ESC) ? len * 2 : len, expdest);
5701 5709
5702 while (len--) { 5710 do {
5703 unsigned char c = *p++; 5711 unsigned char c = *p++;
5704 if (c == '\0') 5712 if (c) {
5705 continue;
5706 if (quotes) {
5707 int n = SIT(c, syntax); 5713 int n = SIT(c, syntax);
5708 if (n == CCTL || n == CBACK) 5714 if ((quotes & QUOTES_ESC) &&
5715 (n == CCTL || n == CBACK))
5709 USTPUTC(CTLESC, q); 5716 USTPUTC(CTLESC, q);
5710 } 5717 } else if (!(quotes & QUOTES_KEEPNUL))
5718 continue;
5711 USTPUTC(c, q); 5719 USTPUTC(c, q);
5712 } 5720 } while (--len);
5713 5721
5714 expdest = q; 5722 expdest = q;
5715} 5723}
5716 5724
5717static void 5725static size_t
5718strtodest(const char *p, int syntax, int quotes) 5726strtodest(const char *p, int syntax, int quotes)
5719{ 5727{
5720 memtodest(p, strlen(p), syntax, quotes); 5728 size_t len = strlen(p);
5729 memtodest(p, len, syntax, quotes);
5730 return len;
5721} 5731}
5722 5732
5723/* 5733/*
@@ -5790,7 +5800,7 @@ exptilde(char *startp, char *p, int flags)
5790 char *name; 5800 char *name;
5791 struct passwd *pw; 5801 struct passwd *pw;
5792 const char *home; 5802 const char *home;
5793 int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); 5803 int quotes = flags & QUOTES_ESC;
5794 5804
5795 name = p + 1; 5805 name = p + 1;
5796 5806
@@ -6043,7 +6053,7 @@ argstr(char *p, int flags, struct strlist *var_str_list)
6043 '\0' 6053 '\0'
6044 }; 6054 };
6045 const char *reject = spclchars; 6055 const char *reject = spclchars;
6046 int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ 6056 int quotes = flags & QUOTES_ESC;
6047 int breakall = flags & EXP_WORD; 6057 int breakall = flags & EXP_WORD;
6048 int inquotes; 6058 int inquotes;
6049 size_t length; 6059 size_t length;
@@ -6608,13 +6618,16 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
6608 const char *p; 6618 const char *p;
6609 int num; 6619 int num;
6610 int i; 6620 int i;
6611 int sepq = 0;
6612 ssize_t len = 0; 6621 ssize_t len = 0;
6613 int subtype = varflags & VSTYPE; 6622 int sep;
6614 int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR);
6615 int quoted = varflags & VSQUOTE; 6623 int quoted = varflags & VSQUOTE;
6624 int subtype = varflags & VSTYPE;
6625 int discard = subtype == VSPLUS || subtype == VSLENGTH;
6626 int quotes = (discard ? 0 : (flags & QUOTES_ESC)) | QUOTES_KEEPNUL;
6616 int syntax = quoted ? DQSYNTAX : BASESYNTAX; 6627 int syntax = quoted ? DQSYNTAX : BASESYNTAX;
6617 6628
6629 sep = quoted ? ((flags & EXP_FULL) << CHAR_BIT) : 0;
6630
6618 switch (*name) { 6631 switch (*name) {
6619 case '$': 6632 case '$':
6620 num = rootpid; 6633 num = rootpid;
@@ -6649,7 +6662,7 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
6649 break; 6662 break;
6650 case '@': { 6663 case '@': {
6651 char **ap; 6664 char **ap;
6652 int sep; 6665 char sepc;
6653 6666
6654 if (quoted && (flags & EXP_FULL)) { 6667 if (quoted && (flags & EXP_FULL)) {
6655 /* note: this is not meant as PEOF value */ 6668 /* note: this is not meant as PEOF value */
@@ -6659,39 +6672,20 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
6659 /* fall through */ 6672 /* fall through */
6660 case '*': 6673 case '*':
6661 sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' '; 6674 sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' ';
6662 i = SIT(sep, syntax);
6663 if (quotes && (i == CCTL || i == CBACK))
6664 sepq = 1;
6665 param: 6675 param:
6666 ap = shellparam.p; 6676 ap = shellparam.p;
6677 sepc = sep;
6667 if (!ap) 6678 if (!ap)
6668 return -1; 6679 return -1;
6669 while ((p = *ap++) != NULL) { 6680 while ((p = *ap++) != NULL) {
6670 size_t partlen; 6681 len += strtodest(p, syntax, quotes);
6671
6672 partlen = strlen(p);
6673 len += partlen;
6674
6675 if (!(subtype == VSPLUS || subtype == VSLENGTH))
6676 memtodest(p, partlen, syntax, quotes);
6677 6682
6678 if (*ap && sep) { 6683 if (*ap && sep) {
6679 char *q;
6680
6681 len++; 6684 len++;
6682 if (subtype == VSPLUS || subtype == VSLENGTH) { 6685 memtodest(&sepc, 1, syntax, quotes);
6683 continue;
6684 }
6685 q = expdest;
6686 if (sepq)
6687 STPUTC(CTLESC, q);
6688 /* note: may put NUL despite sep != 0
6689 * (see sep = 1 << CHAR_BIT above) */
6690 STPUTC(sep, q);
6691 expdest = q;
6692 } 6686 }
6693 } 6687 }
6694 return len; 6688 break;
6695 } /* case '@' and '*' */ 6689 } /* case '@' and '*' */
6696 case '0': 6690 case '0':
6697 case '1': 6691 case '1':
@@ -6740,9 +6734,7 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
6740 if (!p) 6734 if (!p)
6741 return -1; 6735 return -1;
6742 6736
6743 len = strlen(p); 6737 len = strtodest(p, syntax, quotes);
6744 if (!(subtype == VSPLUS || subtype == VSLENGTH))
6745 memtodest(p, len, syntax, quotes);
6746#if ENABLE_UNICODE_SUPPORT 6738#if ENABLE_UNICODE_SUPPORT
6747 if (subtype == VSLENGTH && len > 0) { 6739 if (subtype == VSLENGTH && len > 0) {
6748 reinit_unicode_for_ash(); 6740 reinit_unicode_for_ash();
@@ -6751,10 +6743,10 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
6751 } 6743 }
6752 } 6744 }
6753#endif 6745#endif
6754 return len; 6746 break;
6755 } 6747 }
6756 6748
6757 if (subtype == VSPLUS || subtype == VSLENGTH) 6749 if (discard)
6758 STADJUST(-len, expdest); 6750 STADJUST(-len, expdest);
6759 return len; 6751 return len;
6760} 6752}
@@ -6870,7 +6862,7 @@ evalvar(char *p, int flags, struct strlist *var_str_list)
6870 patloc = expdest - (char *)stackblock(); 6862 patloc = expdest - (char *)stackblock();
6871 if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype, 6863 if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype,
6872 startloc, varflags, 6864 startloc, varflags,
6873 /* quotes: */ flags & (EXP_FULL | EXP_CASE | EXP_REDIR), 6865 /* quotes: */ flags & QUOTES_ESC,
6874 var_str_list) 6866 var_str_list)
6875 ) { 6867 ) {
6876 int amount = expdest - ( 6868 int amount = expdest - (