| 1 | --- ./variables.c 2014-09-25 21:39:38.000000000 +0000 |
| 2 | +++ ./variables.c 2014-09-25 21:39:21.000000000 +0000 |
| 3 | @@ -279,7 +279,7 @@ |
| 4 | static void propagate_temp_var __P((PTR_T)); |
| 5 | static void dispose_temporary_env __P((sh_free_func_t *)); |
| 6 | |
| 7 | -static inline char *mk_env_string __P((const char *, const char *)); |
| 8 | +static inline char *mk_env_string __P((const char *, const char *, int)); |
| 9 | static char **make_env_array_from_var_list __P((SHELL_VAR **)); |
| 10 | static char **make_var_export_array __P((VAR_CONTEXT *)); |
| 11 | static char **make_func_export_array __P((void)); |
| 12 | @@ -312,6 +312,14 @@ |
| 13 | #endif |
| 14 | } |
| 15 | |
| 16 | +/* Prefix and suffix for environment variable names which contain |
| 17 | + shell functions. */ |
| 18 | +#define FUNCDEF_PREFIX "BASH_FUNC_" |
| 19 | +#define FUNCDEF_PREFIX_LEN (strlen (FUNCDEF_PREFIX)) |
| 20 | +#define FUNCDEF_SUFFIX "()" |
| 21 | +#define FUNCDEF_SUFFIX_LEN (strlen (FUNCDEF_SUFFIX)) |
| 22 | + |
| 23 | + |
| 24 | /* Initialize the shell variables from the current environment. |
| 25 | If PRIVMODE is nonzero, don't import functions from ENV or |
| 26 | parse $SHELLOPTS. */ |
| 27 | @@ -349,22 +357,31 @@ |
| 28 | |
| 29 | /* If exported function, define it now. Don't import functions from |
| 30 | the environment in privileged mode. */ |
| 31 | - if (privmode == 0 && read_but_dont_execute == 0 && STREQN ("() {", string, 4)) |
| 32 | - { |
| 33 | - string_length = strlen (string); |
| 34 | - temp_string = (char *)xmalloc (3 + string_length + char_index); |
| 35 | + if (privmode == 0 && read_but_dont_execute == 0 |
| 36 | + && STREQN (FUNCDEF_PREFIX, name, FUNCDEF_PREFIX_LEN) |
| 37 | + && STREQ (name + char_index - FUNCDEF_SUFFIX_LEN, FUNCDEF_SUFFIX) |
| 38 | + && STREQN ("() {", string, 4)) |
| 39 | + { |
| 40 | + size_t name_length |
| 41 | + = char_index - (FUNCDEF_PREFIX_LEN + FUNCDEF_SUFFIX_LEN); |
| 42 | + char *temp_name = name + FUNCDEF_PREFIX_LEN; |
| 43 | + /* Temporarily remove the suffix. */ |
| 44 | + temp_name[name_length] = '\0'; |
| 45 | |
| 46 | - strcpy (temp_string, name); |
| 47 | - temp_string[char_index] = ' '; |
| 48 | - strcpy (temp_string + char_index + 1, string); |
| 49 | + string_length = strlen (string); |
| 50 | + temp_string = (char *)xmalloc (name_length + 1 + string_length + 1); |
| 51 | + memcpy (temp_string, temp_name, name_length); |
| 52 | + temp_string[name_length] = ' '; |
| 53 | + memcpy (temp_string + name_length + 1, string, string_length + 1); |
| 54 | |
| 55 | /* Don't import function names that are invalid identifiers from the |
| 56 | environment, though we still allow them to be defined as shell |
| 57 | variables. */ |
| 58 | - if (legal_identifier (name)) |
| 59 | - parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD); |
| 60 | + if (legal_identifier (temp_name)) |
| 61 | + parse_and_execute (temp_string, temp_name, |
| 62 | + SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD); |
| 63 | |
| 64 | - if (temp_var = find_function (name)) |
| 65 | + if (temp_var = find_function (temp_name)) |
| 66 | { |
| 67 | VSETATTR (temp_var, (att_exported|att_imported)); |
| 68 | array_needs_making = 1; |
| 69 | @@ -379,6 +396,8 @@ |
| 70 | last_command_exit_value = 1; |
| 71 | report_error (_("error importing function definition for `%s'"), name); |
| 72 | } |
| 73 | + /* Restore the original suffix. */ |
| 74 | + temp_name[name_length] = FUNCDEF_SUFFIX[0]; |
| 75 | } |
| 76 | #if defined (ARRAY_VARS) |
| 77 | # if ARRAY_EXPORT |
| 78 | @@ -2954,7 +2973,7 @@ |
| 79 | var->context = variable_context; /* XXX */ |
| 80 | |
| 81 | INVALIDATE_EXPORTSTR (var); |
| 82 | - var->exportstr = mk_env_string (name, value); |
| 83 | + var->exportstr = mk_env_string (name, value, 0); |
| 84 | |
| 85 | array_needs_making = 1; |
| 86 | |
| 87 | @@ -3851,22 +3870,43 @@ |
| 88 | /* */ |
| 89 | /* **************************************************************** */ |
| 90 | |
| 91 | +/* Returns the string NAME=VALUE if !FUNCTIONP or if VALUE == NULL (in |
| 92 | + which case it is treated as empty). Otherwise, decorate NAME with |
| 93 | + FUNCDEF_PREFIX and FUNCDEF_SUFFIX, and return a string of the form |
| 94 | + FUNCDEF_PREFIX NAME FUNCDEF_SUFFIX = VALUE (without spaces). */ |
| 95 | static inline char * |
| 96 | -mk_env_string (name, value) |
| 97 | +mk_env_string (name, value, functionp) |
| 98 | const char *name, *value; |
| 99 | + int functionp; |
| 100 | { |
| 101 | - int name_len, value_len; |
| 102 | - char *p; |
| 103 | + size_t name_len, value_len; |
| 104 | + char *p, *q; |
| 105 | |
| 106 | name_len = strlen (name); |
| 107 | value_len = STRLEN (value); |
| 108 | - p = (char *)xmalloc (2 + name_len + value_len); |
| 109 | - strcpy (p, name); |
| 110 | - p[name_len] = '='; |
| 111 | + if (functionp && value != NULL) |
| 112 | + { |
| 113 | + p = (char *)xmalloc (FUNCDEF_PREFIX_LEN + name_len + FUNCDEF_SUFFIX_LEN |
| 114 | + + 1 + value_len + 1); |
| 115 | + q = p; |
| 116 | + memcpy (q, FUNCDEF_PREFIX, FUNCDEF_PREFIX_LEN); |
| 117 | + q += FUNCDEF_PREFIX_LEN; |
| 118 | + memcpy (q, name, name_len); |
| 119 | + q += name_len; |
| 120 | + memcpy (q, FUNCDEF_SUFFIX, FUNCDEF_SUFFIX_LEN); |
| 121 | + q += FUNCDEF_SUFFIX_LEN; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + p = (char *)xmalloc (name_len + 1 + value_len + 1); |
| 126 | + memcpy (p, name, name_len); |
| 127 | + q = p + name_len; |
| 128 | + } |
| 129 | + q[0] = '='; |
| 130 | if (value && *value) |
| 131 | - strcpy (p + name_len + 1, value); |
| 132 | + memcpy (q + 1, value, value_len + 1); |
| 133 | else |
| 134 | - p[name_len + 1] = '\0'; |
| 135 | + q[1] = '\0'; |
| 136 | return (p); |
| 137 | } |
| 138 | |
| 139 | @@ -3952,7 +3992,7 @@ |
| 140 | /* Gee, I'd like to get away with not using savestring() if we're |
| 141 | using the cached exportstr... */ |
| 142 | list[list_index] = USE_EXPORTSTR ? savestring (value) |
| 143 | - : mk_env_string (var->name, value); |
| 144 | + : mk_env_string (var->name, value, function_p (var)); |
| 145 | |
| 146 | if (USE_EXPORTSTR == 0) |
| 147 | SAVE_EXPORTSTR (var, list[list_index]); |