diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-07-27 17:42:38 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-07-27 17:43:39 +0200 |
commit | 4b70c926bcbfaf6df6e21c98ea096b0db8629095 (patch) | |
tree | a3be088b3613fdb2f01d893e8f1be5ae5b4499de | |
parent | 9dda9270df3108bb85b29c6d382a3477aeb3344b (diff) | |
download | busybox-w32-4b70c926bcbfaf6df6e21c98ea096b0db8629095.tar.gz busybox-w32-4b70c926bcbfaf6df6e21c98ea096b0db8629095.tar.bz2 busybox-w32-4b70c926bcbfaf6df6e21c98ea096b0db8629095.zip |
hush: make "set -x" output closer to bash
function old new delta
print_optionally_squoted - 145 +145
run_pipe 1902 1919 +17
dump_cmd_in_x_mode 142 110 -32
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 162/-32) Total: 130 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/hush.c | 78 |
1 files changed, 60 insertions, 18 deletions
diff --git a/shell/hush.c b/shell/hush.c index e9212cefc..ac8467fb4 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -8030,28 +8030,67 @@ static void execvp_or_die(char **argv) | |||
8030 | } | 8030 | } |
8031 | 8031 | ||
8032 | #if ENABLE_HUSH_MODE_X | 8032 | #if ENABLE_HUSH_MODE_X |
8033 | static void print_optionally_squoted(FILE *fp, const char *str) | ||
8034 | { | ||
8035 | unsigned len; | ||
8036 | const char *cp; | ||
8037 | |||
8038 | cp = str; | ||
8039 | if (str[0] != '{' && str[0] != '(') for (;;) { | ||
8040 | if (!*cp) { | ||
8041 | /* string has no special chars */ | ||
8042 | fputs(str, fp); | ||
8043 | return; | ||
8044 | } | ||
8045 | if (*cp == '\\') break; | ||
8046 | if (*cp == '\'') break; | ||
8047 | if (*cp == '"') break; | ||
8048 | if (*cp == '$') break; | ||
8049 | if (*cp == '!') break; | ||
8050 | if (*cp == '*') break; | ||
8051 | if (*cp == '[') break; | ||
8052 | if (*cp == ']') break; | ||
8053 | #if ENABLE_HUSH_TICK | ||
8054 | if (*cp == '`') break; | ||
8055 | #endif | ||
8056 | if (isspace(*cp)) break; | ||
8057 | cp++; | ||
8058 | } | ||
8059 | |||
8060 | cp = str; | ||
8061 | for (;;) { | ||
8062 | /* print '....' up to EOL or first squote */ | ||
8063 | len = (int)(strchrnul(cp, '\'') - cp); | ||
8064 | if (len != 0) { | ||
8065 | fprintf(fp, "'%.*s'", len, cp); | ||
8066 | cp += len; | ||
8067 | } | ||
8068 | if (*cp == '\0') | ||
8069 | break; | ||
8070 | /* string contains squote(s), print them as \' */ | ||
8071 | fprintf(fp, "\\'"); | ||
8072 | cp++; | ||
8073 | } | ||
8074 | } | ||
8033 | static void dump_cmd_in_x_mode(char **argv) | 8075 | static void dump_cmd_in_x_mode(char **argv) |
8034 | { | 8076 | { |
8035 | if (G_x_mode && argv) { | 8077 | if (G_x_mode && argv) { |
8036 | /* We want to output the line in one write op */ | ||
8037 | char *buf, *p; | ||
8038 | unsigned len; | ||
8039 | unsigned n; | 8078 | unsigned n; |
8040 | 8079 | ||
8041 | len = G.x_mode_depth + 3; /* "+[+++...][ cmd...]\n\0" */ | 8080 | /* "+[+++...][ cmd...]\n\0" */ |
8042 | n = 0; | ||
8043 | while (argv[n]) | ||
8044 | len += strlen(argv[n++]) + 1; | ||
8045 | p = buf = xmalloc(len); | ||
8046 | n = G.x_mode_depth; | 8081 | n = G.x_mode_depth; |
8047 | do *p++ = '+'; while ((int)(--n) >= 0); | 8082 | do bb_putchar_stderr('+'); while ((int)(--n) >= 0); |
8048 | n = 0; | 8083 | n = 0; |
8049 | while (argv[n]) | 8084 | while (argv[n]) { |
8050 | p += sprintf(p, " %s", argv[n++]); | 8085 | if (argv[n][0] == '\0') |
8051 | *p++ = '\n'; | 8086 | fputs(" ''", stderr); |
8052 | *p = '\0'; | 8087 | else { |
8053 | fputs(buf, stderr); | 8088 | bb_putchar_stderr(' '); |
8054 | free(buf); | 8089 | print_optionally_squoted(stderr, argv[n]); |
8090 | } | ||
8091 | n++; | ||
8092 | } | ||
8093 | bb_putchar_stderr('\n'); | ||
8055 | } | 8094 | } |
8056 | } | 8095 | } |
8057 | #else | 8096 | #else |
@@ -8845,13 +8884,18 @@ static NOINLINE int run_pipe(struct pipe *pi) | |||
8845 | ); | 8884 | ); |
8846 | #if ENABLE_HUSH_MODE_X | 8885 | #if ENABLE_HUSH_MODE_X |
8847 | if (G_x_mode) { | 8886 | if (G_x_mode) { |
8887 | char *eq; | ||
8848 | if (i == 0) { | 8888 | if (i == 0) { |
8849 | unsigned n = G.x_mode_depth; | 8889 | unsigned n = G.x_mode_depth; |
8850 | do | 8890 | do |
8851 | bb_putchar_stderr('+'); | 8891 | bb_putchar_stderr('+'); |
8852 | while ((int)(--n) >= 0); | 8892 | while ((int)(--n) >= 0); |
8853 | } | 8893 | } |
8854 | fprintf(stderr, " %s", p); | 8894 | eq = strchrnul(p, '='); |
8895 | fprintf(stderr, " %.*s=", (int)(eq - p), p); | ||
8896 | if (*eq) | ||
8897 | print_optionally_squoted(stderr, eq + 1); | ||
8898 | bb_putchar_stderr('\n'); | ||
8855 | } | 8899 | } |
8856 | #endif | 8900 | #endif |
8857 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); | 8901 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
@@ -8861,8 +8905,6 @@ static NOINLINE int run_pipe(struct pipe *pi) | |||
8861 | } | 8905 | } |
8862 | i++; | 8906 | i++; |
8863 | } | 8907 | } |
8864 | if (G_x_mode) | ||
8865 | bb_putchar_stderr('\n'); | ||
8866 | /* Redirect error sets $? to 1. Otherwise, | 8908 | /* Redirect error sets $? to 1. Otherwise, |
8867 | * if evaluating assignment value set $?, retain it. | 8909 | * if evaluating assignment value set $?, retain it. |
8868 | * Else, clear $?: | 8910 | * Else, clear $?: |