diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-09-12 14:47:41 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-09-12 14:47:41 +0200 |
commit | 873273da94734fda1902b385d7f3e39daf675e35 (patch) | |
tree | 69e6db3e11ee5df46556cf74fe785fb798c88459 | |
parent | 3a0f6f23286ad4352830613c6a8aea1bd56ca2d2 (diff) | |
download | busybox-w32-873273da94734fda1902b385d7f3e39daf675e35.tar.gz busybox-w32-873273da94734fda1902b385d7f3e39daf675e35.tar.bz2 busybox-w32-873273da94734fda1902b385d7f3e39daf675e35.zip |
hush: code shrink by Dan Fandrich (dan AT coneharvesters.com)
function old new delta
find_function_slot - 47 +47
run_list 2508 2491 -17
find_function 40 8 -32
builtin_unset 227 165 -62
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/hush.c | 74 |
1 files changed, 35 insertions, 39 deletions
diff --git a/shell/hush.c b/shell/hush.c index 30eddadcc..a02a4874f 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -3079,15 +3079,21 @@ static const struct built_in_command* find_builtin(const char *name) | |||
3079 | } | 3079 | } |
3080 | 3080 | ||
3081 | #if ENABLE_HUSH_FUNCTIONS | 3081 | #if ENABLE_HUSH_FUNCTIONS |
3082 | static const struct function *find_function(const char *name) | 3082 | static struct function **find_function_slot(const char *name) |
3083 | { | 3083 | { |
3084 | const struct function *funcp = G.top_func; | 3084 | struct function **funcpp = &G.top_func; |
3085 | while (funcp) { | 3085 | while (*funcpp) { |
3086 | if (strcmp(name, funcp->name) == 0) { | 3086 | if (strcmp(name, (*funcpp)->name) == 0) { |
3087 | break; | 3087 | break; |
3088 | } | 3088 | } |
3089 | funcp = funcp->next; | 3089 | funcpp = &(*funcpp)->next; |
3090 | } | 3090 | } |
3091 | return funcpp; | ||
3092 | } | ||
3093 | |||
3094 | static const struct function *find_function(const char *name) | ||
3095 | { | ||
3096 | const struct function *funcp = *find_function_slot(name); | ||
3091 | if (funcp) | 3097 | if (funcp) |
3092 | debug_printf_exec("found function '%s'\n", name); | 3098 | debug_printf_exec("found function '%s'\n", name); |
3093 | return funcp; | 3099 | return funcp; |
@@ -3096,18 +3102,11 @@ static const struct function *find_function(const char *name) | |||
3096 | /* Note: takes ownership on name ptr */ | 3102 | /* Note: takes ownership on name ptr */ |
3097 | static struct function *new_function(char *name) | 3103 | static struct function *new_function(char *name) |
3098 | { | 3104 | { |
3099 | struct function *funcp; | 3105 | struct function **funcpp = find_function_slot(name); |
3100 | struct function **funcpp = &G.top_func; | 3106 | struct function *funcp = *funcpp; |
3101 | |||
3102 | while ((funcp = *funcpp) != NULL) { | ||
3103 | struct command *cmd; | ||
3104 | |||
3105 | if (strcmp(funcp->name, name) != 0) { | ||
3106 | funcpp = &funcp->next; | ||
3107 | continue; | ||
3108 | } | ||
3109 | 3107 | ||
3110 | cmd = funcp->parent_cmd; | 3108 | if (funcp != NULL) { |
3109 | struct command *cmd = funcp->parent_cmd; | ||
3111 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); | 3110 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
3112 | if (!cmd) { | 3111 | if (!cmd) { |
3113 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); | 3112 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
@@ -3129,39 +3128,36 @@ static struct function *new_function(char *name) | |||
3129 | cmd->group_as_string = funcp->body_as_string; | 3128 | cmd->group_as_string = funcp->body_as_string; |
3130 | # endif | 3129 | # endif |
3131 | } | 3130 | } |
3132 | goto skip; | 3131 | } else { |
3132 | debug_printf_exec("remembering new function '%s'\n", name); | ||
3133 | funcp = *funcpp = xzalloc(sizeof(*funcp)); | ||
3134 | /*funcp->next = NULL;*/ | ||
3133 | } | 3135 | } |
3134 | debug_printf_exec("remembering new function '%s'\n", name); | 3136 | |
3135 | funcp = *funcpp = xzalloc(sizeof(*funcp)); | ||
3136 | /*funcp->next = NULL;*/ | ||
3137 | skip: | ||
3138 | funcp->name = name; | 3137 | funcp->name = name; |
3139 | return funcp; | 3138 | return funcp; |
3140 | } | 3139 | } |
3141 | 3140 | ||
3142 | static void unset_func(const char *name) | 3141 | static void unset_func(const char *name) |
3143 | { | 3142 | { |
3144 | struct function *funcp; | 3143 | struct function **funcpp = find_function_slot(name); |
3145 | struct function **funcpp = &G.top_func; | 3144 | struct function *funcp = *funcpp; |
3146 | 3145 | ||
3147 | while ((funcp = *funcpp) != NULL) { | 3146 | if (funcp != NULL) { |
3148 | if (strcmp(funcp->name, name) == 0) { | 3147 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
3149 | *funcpp = funcp->next; | 3148 | *funcpp = funcp->next; |
3150 | /* funcp is unlinked now, deleting it. | 3149 | /* funcp is unlinked now, deleting it. |
3151 | * Note: if !funcp->body, the function was created by | 3150 | * Note: if !funcp->body, the function was created by |
3152 | * "-F name body", do not free ->body_as_string | 3151 | * "-F name body", do not free ->body_as_string |
3153 | * and ->name as they were not malloced. */ | 3152 | * and ->name as they were not malloced. */ |
3154 | if (funcp->body) { | 3153 | if (funcp->body) { |
3155 | free_pipe_list(funcp->body); | 3154 | free_pipe_list(funcp->body); |
3156 | free(funcp->name); | 3155 | free(funcp->name); |
3157 | # if !BB_MMU | 3156 | # if !BB_MMU |
3158 | free(funcp->body_as_string); | 3157 | free(funcp->body_as_string); |
3159 | # endif | 3158 | # endif |
3160 | } | ||
3161 | free(funcp); | ||
3162 | break; | ||
3163 | } | 3159 | } |
3164 | funcpp = &funcp->next; | 3160 | free(funcp); |
3165 | } | 3161 | } |
3166 | } | 3162 | } |
3167 | 3163 | ||