]> git.ipfire.org Git - thirdparty/make.git/commitdiff
* src/expand.c (swap_variable_buffer): Swap two variable buffers
authorPaul Smith <psmith@gnu.org>
Sat, 25 Mar 2023 20:25:32 +0000 (16:25 -0400)
committerPaul Smith <psmith@gnu.org>
Sun, 26 Mar 2023 13:24:49 +0000 (09:24 -0400)
Return the current buffer instead of freeing it.
(variable_append): Use install/swap to handle variable buffers.
(allocated_variable_expand_for_file): Ditto.
* src/variable.c (shell_result): Ditto.
* src/variable.h: Declare the new function.

src/expand.c
src/makeint.h
src/variable.c
src/variable.h

index d9d903fbee49f1cfbee2851676a22416e342ef8d..b2d5074894b90d63fe70ca858da9c85e34e11408 100644 (file)
@@ -83,7 +83,7 @@ initialize_variable_output ()
 {
   /* If we don't have a variable output buffer yet, get one.  */
 
-  if (variable_buffer == 0)
+  if (variable_buffer == NULL)
     {
       variable_buffer_length = 200;
       variable_buffer = xmalloc (variable_buffer_length);
@@ -93,7 +93,48 @@ initialize_variable_output ()
 
   return variable_buffer;
 }
+
+/* Install a new variable_buffer context, returning the current one for
+   safe-keeping.  */
+
+void
+install_variable_buffer (char **bufp, size_t *lenp)
+{
+  *bufp = variable_buffer;
+  *lenp = variable_buffer_length;
+
+  variable_buffer = NULL;
+  initialize_variable_output ();
+}
+
+/* Free the current variable_buffer and restore a previously-saved one.
+ */
+
+void
+restore_variable_buffer (char *buf, size_t len)
+{
+  free (variable_buffer);
+
+  variable_buffer = buf;
+  variable_buffer_length = len;
+}
+
+/* Restore a previously-saved variable_buffer context, and return the
+   current one.
+ */
+
+char *
+swap_variable_buffer (char *buf, size_t len)
+{
+  char *p = variable_buffer;
+
+  variable_buffer = buf;
+  variable_buffer_length = len;
+
+  return p;
+}
 \f
+
 /* Recursively expand V.  The returned string is malloc'd.  */
 
 static char *allocated_variable_append (const struct variable *v);
@@ -555,23 +596,15 @@ variable_append (const char *name, size_t length,
 static char *
 allocated_variable_append (const struct variable *v)
 {
-  char *val;
-
   /* Construct the appended variable value.  */
+  char *obuf;
+  size_t olen;
 
-  char *obuf = variable_buffer;
-  size_t olen = variable_buffer_length;
-
-  variable_buffer = 0;
+  install_variable_buffer (&obuf, &olen);
 
   variable_append (v->name, strlen (v->name), current_variable_set_list, 1);
 
-  val = variable_buffer;
-
-  variable_buffer = obuf;
-  variable_buffer_length = olen;
-
-  return val;
+  return swap_variable_buffer (obuf, olen);
 }
 
 /* Like variable_expand_for_file, but the returned string is malloc'd.
@@ -580,42 +613,12 @@ allocated_variable_append (const struct variable *v)
 char *
 allocated_variable_expand_for_file (const char *line, struct file *file)
 {
-  char *value;
-
-  char *obuf = variable_buffer;
-  size_t olen = variable_buffer_length;
+  char *obuf;
+  size_t olen;
 
-  variable_buffer = 0;
+  install_variable_buffer (&obuf, &olen);
 
-  value = variable_expand_for_file (line, file);
+  variable_expand_for_file (line, file);
 
-  variable_buffer = obuf;
-  variable_buffer_length = olen;
-
-  return value;
-}
-
-/* Install a new variable_buffer context, returning the current one for
-   safe-keeping.  */
-
-void
-install_variable_buffer (char **bufp, size_t *lenp)
-{
-  *bufp = variable_buffer;
-  *lenp = variable_buffer_length;
-
-  variable_buffer = NULL;
-  initialize_variable_output ();
-}
-
-/* Restore a previously-saved variable_buffer setting (free the current one).
- */
-
-void
-restore_variable_buffer (char *buf, size_t len)
-{
-  free (variable_buffer);
-
-  variable_buffer = buf;
-  variable_buffer_length = len;
+  return swap_variable_buffer (obuf, olen);
 }
index d4e3fdd4eba0272e6e7e18cb77b79579b169c342..edac5ba25e8e34aed67c2e3c49c62b5aa3e10847 100644 (file)
@@ -172,6 +172,10 @@ unsigned int get_path_max (void);
 # define USHRT_MAX 65535
 #endif
 
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t)~(size_t)0)
+#endif
+
 /* Nonzero if the integer type T is signed.
    Use <= to avoid GCC warnings about always-false expressions.  */
 #define INTEGER_TYPE_SIGNED(t) ((t) -1 <= 0)
index acb3bcbb3dda819782e9113a5647f5d844690df6..d472c03d32601f2c51ac783c4667361466db9daf 100644 (file)
@@ -1272,17 +1272,14 @@ shell_result (const char *p)
   char *buf;
   size_t len;
   char *args[2];
-  char *result;
 
   install_variable_buffer (&buf, &len);
 
   args[0] = (char *) p;
   args[1] = NULL;
   func_shell_base (variable_buffer, args, 0);
-  result = strdup (variable_buffer);
 
-  restore_variable_buffer (buf, len);
-  return result;
+  return swap_variable_buffer (buf, len);
 }
 \f
 /* Given a variable, a value, and a flavor, define the variable.
index 244da4f8608a7b5ee7ce55a7ccc9c761cddb7864..218c9964766e184d6cd3448f36fc9d5464a68c8a 100644 (file)
@@ -121,21 +121,21 @@ extern struct variable *default_goal_var;
 extern struct variable shell_var;
 
 /* expand.c */
-#ifndef SIZE_MAX
-# define SIZE_MAX ((size_t)~(size_t)0)
-#endif
-
+char *initialize_variable_output (void);
 char *variable_buffer_output (char *ptr, const char *string, size_t length);
+void install_variable_buffer (char **bufp, size_t *lenp);
+void restore_variable_buffer (char *buf, size_t len);
+char *swap_variable_buffer (char *buf, size_t len);
+
+char *variable_expand_string (char *line, const char *string, size_t length);
 char *variable_expand (const char *line);
 char *variable_expand_for_file (const char *line, struct file *file);
 char *allocated_variable_expand_for_file (const char *line, struct file *file);
 #define allocated_variable_expand(line) \
   allocated_variable_expand_for_file (line, (struct file *) 0)
 char *expand_argument (const char *str, const char *end);
-char *variable_expand_string (char *line, const char *string, size_t length);
-char *initialize_variable_output (void);
-void install_variable_buffer (char **bufp, size_t *lenp);
-void restore_variable_buffer (char *buf, size_t len);
+char *recursively_expand_for_file (struct variable *v, struct file *file);
+#define recursively_expand(v)   recursively_expand_for_file (v, NULL)
 
 /* function.c */
 int handle_function (char **op, const char **stringp);
@@ -150,10 +150,6 @@ char *patsubst_expand (char *o, const char *text, char *pattern, char *replace);
 char *func_shell_base (char *o, char **argv, int trim_newlines);
 void shell_completed (int exit_code, int exit_sig);
 
-/* expand.c */
-char *recursively_expand_for_file (struct variable *v, struct file *file);
-#define recursively_expand(v)   recursively_expand_for_file (v, NULL)
-
 /* variable.c */
 struct variable_set_list *create_new_variable_set (void);
 void free_variable_set (struct variable_set_list *);