You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.3 KiB

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