From f99d0834184c97f162e965a603e88a3e10e22993 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sat, 25 Mar 2023 16:25:32 -0400 Subject: * src/expand.c (swap_variable_buffer): Swap two variable buffers 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 | 99 ++++++++++++++++++++++++++++++---------------------------- src/makeint.h | 4 +++ src/variable.c | 5 +-- src/variable.h | 20 +++++------- 4 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/expand.c b/src/expand.c index d9d903fb..b2d50748 100644 --- a/src/expand.c +++ b/src/expand.c @@ -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; +} + /* 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); } diff --git a/src/makeint.h b/src/makeint.h index d4e3fdd4..edac5ba2 100644 --- a/src/makeint.h +++ b/src/makeint.h @@ -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) diff --git a/src/variable.c b/src/variable.c index acb3bcbb..d472c03d 100644 --- a/src/variable.c +++ b/src/variable.c @@ -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); } /* Given a variable, a value, and a flavor, define the variable. diff --git a/src/variable.h b/src/variable.h index 244da4f8..218c9964 100644 --- a/src/variable.h +++ b/src/variable.h @@ -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 *); -- cgit v1.2.1