diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-10 21:57:50 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-10 21:57:50 +0000 |
commit | 6ba6f546ac8016a3acccb19b2eff8b6878a3a87c (patch) | |
tree | a25450be2f7896b95430813ad325eb77e5c13df3 | |
parent | c0ea329297862003a32570a7a38fa2bcb20e57f1 (diff) | |
download | busybox-w32-6ba6f546ac8016a3acccb19b2eff8b6878a3a87c.tar.gz busybox-w32-6ba6f546ac8016a3acccb19b2eff8b6878a3a87c.tar.bz2 busybox-w32-6ba6f546ac8016a3acccb19b2eff8b6878a3a87c.zip |
hush: pass $n to functions
function old new delta
run_pipe 1183 1325 +142
run_list 1219 1220 +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 143/0) Total: 143 bytes
-rw-r--r-- | shell/hush.c | 62 |
1 files changed, 50 insertions, 12 deletions
diff --git a/shell/hush.c b/shell/hush.c index dd5031951..40fcb7be4 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -2644,6 +2644,7 @@ static const struct function *find_function(const char *name) | |||
2644 | debug_printf_exec("found function '%s'\n", name); | 2644 | debug_printf_exec("found function '%s'\n", name); |
2645 | return funcp; | 2645 | return funcp; |
2646 | } | 2646 | } |
2647 | |||
2647 | static void exec_function(const struct function *funcp, char **argv) NORETURN; | 2648 | static void exec_function(const struct function *funcp, char **argv) NORETURN; |
2648 | static void exec_function(const struct function *funcp, char **argv) | 2649 | static void exec_function(const struct function *funcp, char **argv) |
2649 | { | 2650 | { |
@@ -2660,7 +2661,49 @@ static void exec_function(const struct function *funcp, char **argv) | |||
2660 | _exit(n); | 2661 | _exit(n); |
2661 | # else | 2662 | # else |
2662 | re_execute_shell(funcp->body_as_string, G.global_argv[0], argv + 1); | 2663 | re_execute_shell(funcp->body_as_string, G.global_argv[0], argv + 1); |
2663 | #endif | 2664 | # endif |
2665 | } | ||
2666 | |||
2667 | static int run_function(const struct function *funcp, char **argv) | ||
2668 | { | ||
2669 | int n; | ||
2670 | char **pp; | ||
2671 | char *sv_argv0; | ||
2672 | smallint sv_g_malloced; | ||
2673 | int sv_g_argc; | ||
2674 | char **sv_g_argv; | ||
2675 | |||
2676 | sv_argv0 = argv[0]; | ||
2677 | sv_g_malloced = G.global_args_malloced; | ||
2678 | sv_g_argc = G.global_argc; | ||
2679 | sv_g_argv = G.global_argv; | ||
2680 | |||
2681 | pp = argv; | ||
2682 | n = 1; | ||
2683 | while (*++pp) | ||
2684 | n++; | ||
2685 | |||
2686 | argv[0] = G.global_argv[0]; /* retain $0 */ | ||
2687 | G.global_args_malloced = 0; | ||
2688 | G.global_argc = n; | ||
2689 | G.global_argv = argv; | ||
2690 | |||
2691 | n = run_list(funcp->body); | ||
2692 | |||
2693 | if (G.global_args_malloced) { | ||
2694 | /* function ran "set -- arg1 arg2 ..." */ | ||
2695 | pp = G.global_argv; | ||
2696 | while (*++pp) | ||
2697 | free(*pp); | ||
2698 | free(G.global_argv); | ||
2699 | } | ||
2700 | |||
2701 | argv[0] = sv_argv0; | ||
2702 | G.global_args_malloced = sv_g_malloced; | ||
2703 | G.global_argc = sv_g_argc; | ||
2704 | G.global_argv = sv_g_argv; | ||
2705 | |||
2706 | return n; | ||
2664 | } | 2707 | } |
2665 | #endif | 2708 | #endif |
2666 | 2709 | ||
@@ -3238,7 +3281,7 @@ static int run_pipe(struct pipe *pi) | |||
3238 | else { | 3281 | else { |
3239 | debug_printf_exec(": function '%s' '%s'...\n", | 3282 | debug_printf_exec(": function '%s' '%s'...\n", |
3240 | funcp->name, argv_expanded[1]); | 3283 | funcp->name, argv_expanded[1]); |
3241 | rcode = run_list(funcp->body) & 0xff; | 3284 | rcode = run_function(funcp, argv_expanded) & 0xff; |
3242 | } | 3285 | } |
3243 | #endif | 3286 | #endif |
3244 | } | 3287 | } |
@@ -6436,19 +6479,14 @@ static int builtin_set(char **argv) | |||
6436 | ++argv; | 6479 | ++argv; |
6437 | goto set_argv; | 6480 | goto set_argv; |
6438 | } | 6481 | } |
6439 | 6482 | if (arg[0] != '+' && arg[0] != '-') | |
6440 | if (arg[0] == '+' || arg[0] == '-') { | 6483 | break; |
6441 | for (n = 1; arg[n]; ++n) | 6484 | for (n = 1; arg[n]; ++n) |
6442 | if (set_mode(arg[0], arg[n])) | 6485 | if (set_mode(arg[0], arg[n])) |
6443 | goto error; | 6486 | goto error; |
6444 | continue; | ||
6445 | } | ||
6446 | |||
6447 | break; | ||
6448 | } while ((arg = *++argv) != NULL); | 6487 | } while ((arg = *++argv) != NULL); |
6449 | /* Now argv[0] is 1st argument */ | 6488 | /* Now argv[0] is 1st argument */ |
6450 | 6489 | ||
6451 | /* Only reset global_argv if we didn't process anything */ | ||
6452 | if (arg == NULL) | 6490 | if (arg == NULL) |
6453 | return EXIT_SUCCESS; | 6491 | return EXIT_SUCCESS; |
6454 | set_argv: | 6492 | set_argv: |