bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 5/8] builtins/source: parse the -i option


From: Matheus Afonso Martins Moreira
Subject: [PATCH v2 5/8] builtins/source: parse the -i option
Date: Mon, 13 May 2024 07:37:23 -0300

Passing the -i option to the source builtin
enables isolated sourcing mode which restricts
its search path to the directories defined by
the BASH_SOURCE_PATH variable. This also has
the added benefit of not touching PATH at all.

To maximize compatibility, the option will only
be recognized outside POSIXly correct mode.

Signed-off-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>
---
 builtins/source.def | 30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/builtins/source.def b/builtins/source.def
index ba5e1596..26040e70 100644
--- a/builtins/source.def
+++ b/builtins/source.def
@@ -182,10 +182,34 @@ int
 source_builtin (WORD_LIST *list)
 {
   char *filename, *x;
+  int opt, isolated_mode = 0;
 
-  if (no_options (list))
-    return (EX_USAGE);
-  list = loptend;
+  if (posixly_correct)
+    {
+      /* Don't parse options in POSIX mode */
+      if (no_options (list))
+        return (EX_USAGE);
+    }
+  else
+    {
+      reset_internal_getopt ();
+
+      while ((opt = internal_getopt (list, "i")) != -1)
+        {
+          switch (opt)
+            {
+            case 'i':
+              isolated_mode = 1;
+              break;
+            CASE_HELPOPT;
+            default:
+              builtin_usage ();
+              return (EX_USAGE);
+            }
+        }
+
+      list = loptend;
+    }
 
   if (list == 0)
     {
-- 
2.44.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]