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>
This commit is contained in:
Denys Vlasenko 2009-09-12 14:47:41 +02:00
parent 3a0f6f2328
commit 873273da94

View File

@ -3079,15 +3079,21 @@ static const struct built_in_command* find_builtin(const char *name)
} }
#if ENABLE_HUSH_FUNCTIONS #if ENABLE_HUSH_FUNCTIONS
static const struct function *find_function(const char *name) static struct function **find_function_slot(const char *name)
{ {
const struct function *funcp = G.top_func; struct function **funcpp = &G.top_func;
while (funcp) { while (*funcpp) {
if (strcmp(name, funcp->name) == 0) { if (strcmp(name, (*funcpp)->name) == 0) {
break; break;
} }
funcp = funcp->next; funcpp = &(*funcpp)->next;
} }
return funcpp;
}
static const struct function *find_function(const char *name)
{
const struct function *funcp = *find_function_slot(name);
if (funcp) if (funcp)
debug_printf_exec("found function '%s'\n", name); debug_printf_exec("found function '%s'\n", name);
return funcp; return funcp;
@ -3096,18 +3102,11 @@ static const struct function *find_function(const char *name)
/* Note: takes ownership on name ptr */ /* Note: takes ownership on name ptr */
static struct function *new_function(char *name) static struct function *new_function(char *name)
{ {
struct function *funcp; struct function **funcpp = find_function_slot(name);
struct function **funcpp = &G.top_func; struct function *funcp = *funcpp;
while ((funcp = *funcpp) != NULL) { if (funcp != NULL) {
struct command *cmd; struct command *cmd = funcp->parent_cmd;
if (strcmp(funcp->name, name) != 0) {
funcpp = &funcp->next;
continue;
}
cmd = funcp->parent_cmd;
debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd);
if (!cmd) { if (!cmd) {
debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); debug_printf_exec("freeing & replacing function '%s'\n", funcp->name);
@ -3129,39 +3128,36 @@ static struct function *new_function(char *name)
cmd->group_as_string = funcp->body_as_string; cmd->group_as_string = funcp->body_as_string;
# endif # endif
} }
goto skip; } else {
debug_printf_exec("remembering new function '%s'\n", name);
funcp = *funcpp = xzalloc(sizeof(*funcp));
/*funcp->next = NULL;*/
} }
debug_printf_exec("remembering new function '%s'\n", name);
funcp = *funcpp = xzalloc(sizeof(*funcp));
/*funcp->next = NULL;*/
skip:
funcp->name = name; funcp->name = name;
return funcp; return funcp;
} }
static void unset_func(const char *name) static void unset_func(const char *name)
{ {
struct function *funcp; struct function **funcpp = find_function_slot(name);
struct function **funcpp = &G.top_func; struct function *funcp = *funcpp;
while ((funcp = *funcpp) != NULL) { if (funcp != NULL) {
if (strcmp(funcp->name, name) == 0) { debug_printf_exec("freeing function '%s'\n", funcp->name);
*funcpp = funcp->next; *funcpp = funcp->next;
/* funcp is unlinked now, deleting it. /* funcp is unlinked now, deleting it.
* Note: if !funcp->body, the function was created by * Note: if !funcp->body, the function was created by
* "-F name body", do not free ->body_as_string * "-F name body", do not free ->body_as_string
* and ->name as they were not malloced. */ * and ->name as they were not malloced. */
if (funcp->body) { if (funcp->body) {
free_pipe_list(funcp->body); free_pipe_list(funcp->body);
free(funcp->name); free(funcp->name);
# if !BB_MMU # if !BB_MMU
free(funcp->body_as_string); free(funcp->body_as_string);
# endif # endif
}
free(funcp);
break;
} }
funcpp = &funcp->next; free(funcp);
} }
} }