aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-05-25 02:46:01 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2007-05-25 02:46:01 +0000
commit1e83fbc474caf0e566a06b849107942961c4b846 (patch)
tree7aafa32621cbad4f3c322855ba21b9959f5bebe6
parentca6d7600a72a08b9af10c299b2c92ffb017b6125 (diff)
downloadbusybox-w32-1e83fbc474caf0e566a06b849107942961c4b846.tar.gz
busybox-w32-1e83fbc474caf0e566a06b849107942961c4b846.tar.bz2
busybox-w32-1e83fbc474caf0e566a06b849107942961c4b846.zip
hush: we can't do without variable->value member, saving 25 bytes of code
and some runtime memory. Rename few variables. git-svn-id: svn://busybox.net/trunk/busybox@18688 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--shell/hush.c80
1 files changed, 38 insertions, 42 deletions
diff --git a/shell/hush.c b/shell/hush.c
index 0b8e7d68d..f40284cbb 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -286,8 +286,7 @@ struct close_me {
286#define setenv(...) setenv_is_leaky_dont_use() 286#define setenv(...) setenv_is_leaky_dont_use()
287struct variable { 287struct variable {
288 struct variable *next; 288 struct variable *next;
289 char *name; /* points to "name=" portion */ 289 char *varstr; /* points to "name=" portion */
290 char *value; /* points directly after "=" */
291 int max_len; /* if > 0, name is part of initial env; else name is malloced */ 290 int max_len; /* if > 0, name is part of initial env; else name is malloced */
292 smallint flg_export; /* putenv should be done on this var */ 291 smallint flg_export; /* putenv should be done on this var */
293 smallint flg_read_only; 292 smallint flg_read_only;
@@ -334,8 +333,7 @@ static const char version_str[] = "HUSH_VERSION="HUSH_VER_STR;
334 333
335static const struct variable const_shell_ver = { 334static const struct variable const_shell_ver = {
336 .next = NULL, 335 .next = NULL,
337 .name = (char*)version_str, 336 .varstr = (char*)version_str,
338 .value = (char*)version_str + sizeof("HUSH_VERSION=")-1,
339 .max_len = 1, /* 0 can provoke free(name) */ 337 .max_len = 1, /* 0 can provoke free(name) */
340 .flg_export = 1, 338 .flg_export = 1,
341 .flg_read_only = 1, 339 .flg_read_only = 1,
@@ -554,8 +552,8 @@ static char **expand_strvec_to_strvec(char **argv);
554static char *expand_strvec_to_string(char **argv); 552static char *expand_strvec_to_string(char **argv);
555/* used for expansion of right hand of assignments */ 553/* used for expansion of right hand of assignments */
556static char *expand_string_to_string(const char *str); 554static char *expand_string_to_string(const char *str);
557static struct variable *get_local_var(const char *var); 555static struct variable *get_local_var(const char *name);
558static int set_local_var(char *s, int flg_export); 556static int set_local_var(char *str, int flg_export);
559static void unset_local_var(const char *name); 557static void unset_local_var(const char *name);
560 558
561/* Table of built-in functions. They can be forked or not, depending on 559/* Table of built-in functions. They can be forked or not, depending on
@@ -831,7 +829,7 @@ static int builtin_export(char **argv)
831 var = get_local_var(name); 829 var = get_local_var(name);
832 if (var) { 830 if (var) {
833 var->flg_export = 1; 831 var->flg_export = 1;
834 putenv(var->name); 832 putenv(var->varstr);
835 } 833 }
836 /* bash does not return an error when trying to export 834 /* bash does not return an error when trying to export
837 * an undefined variable. Do likewise. */ 835 * an undefined variable. Do likewise. */
@@ -976,7 +974,7 @@ static int builtin_set(char **argv)
976 974
977 if (temp == NULL) 975 if (temp == NULL)
978 for (e = top_var; e; e = e->next) 976 for (e = top_var; e; e = e->next)
979 puts(e->name); 977 puts(e->varstr);
980 else 978 else
981 set_local_var(xstrdup(temp), 0); 979 set_local_var(xstrdup(temp), 0);
982 980
@@ -2676,39 +2674,39 @@ static char* expand_strvec_to_string(char **argv)
2676} 2674}
2677 2675
2678/* This is used to get/check local shell variables */ 2676/* This is used to get/check local shell variables */
2679static struct variable *get_local_var(const char *s) 2677static struct variable *get_local_var(const char *name)
2680{ 2678{
2681 struct variable *cur; 2679 struct variable *cur;
2682 int len; 2680 int len;
2683 2681
2684 if (!s) 2682 if (!name)
2685 return NULL; 2683 return NULL;
2686 len = strlen(s); 2684 len = strlen(name);
2687 for (cur = top_var; cur; cur = cur->next) { 2685 for (cur = top_var; cur; cur = cur->next) {
2688 if (strncmp(cur->name, s, len) == 0 && cur->name[len] == '=') 2686 if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=')
2689 return cur; 2687 return cur;
2690 } 2688 }
2691 return NULL; 2689 return NULL;
2692} 2690}
2693 2691
2694/* name holds "NAME=VAL" and is expected to be malloced. 2692/* str holds "NAME=VAL" and is expected to be malloced.
2695 * We take ownership of it. */ 2693 * We take ownership of it. */
2696static int set_local_var(char *name, int flg_export) 2694static int set_local_var(char *str, int flg_export)
2697{ 2695{
2698 struct variable *cur; 2696 struct variable *cur;
2699 char *value; 2697 char *value;
2700 int name_len; 2698 int name_len;
2701 2699
2702 value = strchr(name, '='); 2700 value = strchr(str, '=');
2703 if (!value) { /* not expected to ever happen? */ 2701 if (!value) { /* not expected to ever happen? */
2704 free(name); 2702 free(str);
2705 return -1; 2703 return -1;
2706 } 2704 }
2707 2705
2708 name_len = value - name; 2706 name_len = value - str;
2709 cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */ 2707 cur = top_var; /* cannot be NULL (we have HUSH_VERSION and it's RO) */
2710 while (1) { 2708 while (1) {
2711 if (strncmp(cur->name, name, name_len) != 0 || cur->name[name_len] != '=') { 2709 if (strncmp(cur->varstr, str, name_len) != 0 || cur->varstr[name_len] != '=') {
2712 if (!cur->next) { 2710 if (!cur->next) {
2713 /* cur points to last var in linked list */ 2711 /* cur points to last var in linked list */
2714 break; 2712 break;
@@ -2716,45 +2714,44 @@ static int set_local_var(char *name, int flg_export)
2716 cur = cur->next; 2714 cur = cur->next;
2717 continue; 2715 continue;
2718 } 2716 }
2719 /* We already have a var with this name */ 2717 /* We found an existing var with this name */
2718 *value = '\0';
2720 if (cur->flg_read_only) { 2719 if (cur->flg_read_only) {
2721 bb_error_msg("%s: readonly variable", name); 2720 bb_error_msg("%s: readonly variable", str);
2722 free(name); 2721 free(str);
2723 return -1; 2722 return -1;
2724 } 2723 }
2725 *value = '\0'; 2724 unsetenv(str); /* just in case */
2726 unsetenv(name); /* just in case */ 2725 *value = '=';
2727 *value++ = '='; 2726 if (strcmp(cur->varstr, str) == 0) {
2728 if (strcmp(cur->value, value) == 0) {
2729 free_and_exp: 2727 free_and_exp:
2730 free(name); 2728 free(str);
2731 goto exp; 2729 goto exp;
2732 } 2730 }
2733 if (cur->max_len >= strlen(name)) { 2731 if (cur->max_len >= strlen(str)) {
2734 /* This one is from startup env, reuse space */ 2732 /* This one is from startup env, reuse space */
2735 strcpy(cur->name, name); 2733 strcpy(cur->varstr, str);
2736 goto free_and_exp; 2734 goto free_and_exp;
2737 } 2735 }
2738 /* max_len == 0 signifies "malloced" var, which we can 2736 /* max_len == 0 signifies "malloced" var, which we can
2739 * (and has to) free */ 2737 * (and has to) free */
2740 if (!cur->max_len) 2738 if (!cur->max_len)
2741 free(cur->name); 2739 free(cur->varstr);
2742 cur->max_len = 0; 2740 cur->max_len = 0;
2743 goto set_name_and_exp; 2741 goto set_str_and_exp;
2744 } 2742 }
2745 2743
2746 /* Not found - create next variable struct */ 2744 /* Not found - create next variable struct */
2747 cur->next = xzalloc(sizeof(*cur)); 2745 cur->next = xzalloc(sizeof(*cur));
2748 cur = cur->next; 2746 cur = cur->next;
2749 2747
2750 set_name_and_exp: 2748 set_str_and_exp:
2751 cur->name = name; 2749 cur->varstr = str;
2752 exp: 2750 exp:
2753 cur->value = cur->name + name_len + 1;
2754 if (flg_export) 2751 if (flg_export)
2755 cur->flg_export = 1; 2752 cur->flg_export = 1;
2756 if (cur->flg_export) 2753 if (cur->flg_export)
2757 return putenv(cur->name); 2754 return putenv(cur->varstr);
2758 return 0; 2755 return 0;
2759} 2756}
2760 2757
@@ -2769,7 +2766,7 @@ static void unset_local_var(const char *name)
2769 name_len = strlen(name); 2766 name_len = strlen(name);
2770 cur = top_var; 2767 cur = top_var;
2771 while (cur) { 2768 while (cur) {
2772 if (strncmp(cur->name, name, name_len) == 0 && cur->name[name_len] == '=') { 2769 if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') {
2773 if (cur->flg_read_only) { 2770 if (cur->flg_read_only) {
2774 bb_error_msg("%s: readonly variable", name); 2771 bb_error_msg("%s: readonly variable", name);
2775 return; 2772 return;
@@ -2777,9 +2774,9 @@ static void unset_local_var(const char *name)
2777 /* prev is ok to use here because 1st variable, HUSH_VERSION, 2774 /* prev is ok to use here because 1st variable, HUSH_VERSION,
2778 * is ro, and we cannot reach this code on the 1st pass */ 2775 * is ro, and we cannot reach this code on the 1st pass */
2779 prev->next = cur->next; 2776 prev->next = cur->next;
2780 unsetenv(cur->name); 2777 unsetenv(cur->varstr);
2781 if (!cur->max_len) 2778 if (!cur->max_len)
2782 free(cur->name); 2779 free(cur->varstr);
2783 free(cur); 2780 free(cur);
2784 return; 2781 return;
2785 } 2782 }
@@ -3256,7 +3253,7 @@ static const char *lookup_param(const char *src)
3256{ 3253{
3257 struct variable *var = get_local_var(src); 3254 struct variable *var = get_local_var(src);
3258 if (var) 3255 if (var)
3259 return var->value; 3256 return strchr(var->varstr, '=') + 1;
3260 return NULL; 3257 return NULL;
3261} 3258}
3262 3259
@@ -3682,14 +3679,13 @@ int hush_main(int argc, char **argv)
3682 if (value) { /* paranoia */ 3679 if (value) { /* paranoia */
3683 cur_var->next = xzalloc(sizeof(*cur_var)); 3680 cur_var->next = xzalloc(sizeof(*cur_var));
3684 cur_var = cur_var->next; 3681 cur_var = cur_var->next;
3685 cur_var->name = *e; 3682 cur_var->varstr = *e;
3686 cur_var->value = value + 1;
3687 cur_var->max_len = strlen(*e); 3683 cur_var->max_len = strlen(*e);
3688 cur_var->flg_export = 1; 3684 cur_var->flg_export = 1;
3689 } 3685 }
3690 e++; 3686 e++;
3691 } 3687 }
3692 putenv(shell_ver.name); 3688 putenv(shell_ver.varstr);
3693 3689
3694#if ENABLE_FEATURE_EDITING 3690#if ENABLE_FEATURE_EDITING
3695 line_input_state = new_line_input_t(FOR_SHELL); 3691 line_input_state = new_line_input_t(FOR_SHELL);
@@ -3827,7 +3823,7 @@ int hush_main(int argc, char **argv)
3827 while (cur_var) { 3823 while (cur_var) {
3828 struct variable *tmp = cur_var; 3824 struct variable *tmp = cur_var;
3829 if (!cur_var->max_len) 3825 if (!cur_var->max_len)
3830 free(cur_var->name); 3826 free(cur_var->varstr);
3831 cur_var = cur_var->next; 3827 cur_var = cur_var->next;
3832 free(tmp); 3828 free(tmp);
3833 } 3829 }