From 23f70b0cb86208d3b9b47b0efa9707a1dac5360b Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 26 Mar 2023 17:29:50 -0400 Subject: Create helper functions for pushing file contexts * src/variable.h (install_file_context, restore_file_context): Add declarations for new functions. * src/variable.c (install_file_context, restore_file_context): Define the new functions. (lookup_variable_for_file): Call them. * src/expand.c (recursively_expand_for_file): Ditto. (allocated_expand_variable_for_file): Ditto. (expand_string_for_file): Ditto. --- src/expand.c | 33 +++++++-------------------------- src/variable.c | 40 ++++++++++++++++++++++++++++++++++++---- src/variable.h | 2 ++ 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/expand.c b/src/expand.c index a72dfff7..01fff81e 100644 --- a/src/expand.c +++ b/src/expand.c @@ -145,7 +145,7 @@ recursively_expand_for_file (struct variable *v, struct file *file) char *value; const floc *this_var; const floc **saved_varp; - struct variable_set_list *save = 0; + struct variable_set_list *savev = 0; int set_reading = 0; /* If we're expanding to put into the environment of a shell function then @@ -198,10 +198,7 @@ recursively_expand_for_file (struct variable *v, struct file *file) } if (file) - { - save = current_variable_set_list; - current_variable_set_list = file->variables; - } + install_file_context (file, &savev, NULL); v->expanding = 1; if (v->append) @@ -214,7 +211,7 @@ recursively_expand_for_file (struct variable *v, struct file *file) reading_file = 0; if (file) - current_variable_set_list = save; + restore_file_context (savev, NULL); expanding_var = saved_varp; @@ -297,19 +294,11 @@ allocated_expand_variable_for_file (const char *name, size_t length, struct file if (!file) return allocated_expand_variable (name, length); - savev = current_variable_set_list; - current_variable_set_list = file->variables; - - savef = reading_file; - if (file->cmds && file->cmds->fileinfo.filenm) - reading_file = &file->cmds->fileinfo; - else - reading_file = NULL; + install_file_context (file, &savev, &savef); result = allocated_expand_variable (name, length); - current_variable_set_list = savev; - reading_file = savef; + restore_file_context (savev, savef); return result; } @@ -585,19 +574,11 @@ expand_string_for_file (const char *string, struct file *file) if (!file) return expand_string (string); - savev = current_variable_set_list; - current_variable_set_list = file->variables; - - savef = reading_file; - if (file->cmds && file->cmds->fileinfo.filenm) - reading_file = &file->cmds->fileinfo; - else - reading_file = NULL; + install_file_context (file, &savev, &savef); result = expand_string (string); - current_variable_set_list = savev; - reading_file = savef; + restore_file_context (savev, savef); return result; } diff --git a/src/variable.c b/src/variable.c index 6d87390c..e2a529de 100644 --- a/src/variable.c +++ b/src/variable.c @@ -552,12 +552,11 @@ lookup_variable_for_file (const char *name, size_t length, struct file *file) if (file == NULL) return lookup_variable (name, length); - savev = current_variable_set_list; - current_variable_set_list = file->variables; + install_file_context (file, &savev, NULL); var = lookup_variable (name, length); - current_variable_set_list = savev; + restore_file_context (savev, NULL); return var; } @@ -733,7 +732,7 @@ push_new_variable_scope (void) global_setlist.next = current_variable_set_list; current_variable_set_list = &global_setlist; } - return (current_variable_set_list); + return current_variable_set_list; } void @@ -770,6 +769,39 @@ pop_variable_scope (void) hash_free (&set->table, 1); free (set); } + +/* Install a new global context for FILE so that errors/warnings are shown + in that context. Sets OLDLIST to the previous list, and if not NULL sets + OLDFLOC to reading_file and changes reading_file to the current FILE. + Use restore_file_context() to undo this. */ + +void +install_file_context (struct file *file, struct variable_set_list **oldlist, const floc **oldfloc) +{ + *oldlist = current_variable_set_list; + current_variable_set_list = file->variables; + + if (oldfloc) + { + *oldfloc = reading_file; + if (file->cmds && file->cmds->fileinfo.filenm) + reading_file = &file->cmds->fileinfo; + else + reading_file = NULL; + } +} + +/* Restore a saved global context from OLDLIST. If OLDFLOC is not NULL, + set reading_file back to that value. */ + +void +restore_file_context (struct variable_set_list *oldlist, const floc *oldfloc) +{ + current_variable_set_list = oldlist; + if (oldfloc) + reading_file = oldfloc; +} + /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET. */ diff --git a/src/variable.h b/src/variable.h index bfba63f5..bbda212f 100644 --- a/src/variable.h +++ b/src/variable.h @@ -160,6 +160,8 @@ struct variable_set_list *create_new_variable_set (void); void free_variable_set (struct variable_set_list *); struct variable_set_list *push_new_variable_scope (void); void pop_variable_scope (void); +void install_file_context (struct file *file, struct variable_set_list **oldlist, const floc **oldfloc); +void restore_file_context (struct variable_set_list *oldlist, const floc *oldfloc); void define_automatic_variables (void); void initialize_file_variables (struct file *file, int reading); void print_file_variables (const struct file *file); -- cgit v1.2.1