]> git.ipfire.org Git - thirdparty/make.git/commitdiff
Create helper functions for pushing file contexts
authorPaul Smith <psmith@gnu.org>
Sun, 26 Mar 2023 21:29:50 +0000 (17:29 -0400)
committerPaul Smith <psmith@gnu.org>
Sat, 1 Apr 2023 15:13:12 +0000 (11:13 -0400)
* 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
src/variable.c
src/variable.h

index a72dfff7cf1b3ab81da024440b1d599a42988020..01fff81e4961e46237803455324e686d734c9b8d 100644 (file)
@@ -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;
 }
index 6d87390c2264aa0f2bb86f4b28a8cc67700eea9a..e2a529de6eede7d1a7cb15dcfa2cb0551f976ef1 100644 (file)
@@ -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;
+}
+
 \f
 /* Merge FROM_SET into TO_SET, freeing unused storage in FROM_SET.  */
 
index bfba63f58db214668e7c6733ff83ea39da61025a..bbda212f34fe862eb522440342d07011236a2adb 100644 (file)
@@ -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);