bug-bash
[Top][All Lists]
Advanced

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

[PATCH 4/9] bashgetopt: define long option shortener function


From: Matheus Afonso Martins Moreira
Subject: [PATCH 4/9] bashgetopt: define long option shortener function
Date: Sun, 5 May 2024 06:54:57 -0300

Define the shorten_long_options helper function that converts
long options to short options which are supported by bash's
builting getopt function.

Signed-off-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>
---
 builtins/bashgetopt.c | 24 ++++++++++++++++++++++++
 builtins/bashgetopt.h |  7 +++++++
 2 files changed, 31 insertions(+)

diff --git a/builtins/bashgetopt.c b/builtins/bashgetopt.c
index d40ffa23..1b527331 100644
--- a/builtins/bashgetopt.c
+++ b/builtins/bashgetopt.c
@@ -192,3 +192,27 @@ reset_internal_getopt ()
        lhead = lcurrent = loptend = (WORD_LIST *)NULL;
        sp = 1;
 }
+
+/* Converts long options into short options for getopt.
+   The conversion happens in place. */
+void
+shorten_long_options (list, conversions)
+     WORD_LIST *list;
+     struct option_conversion *conversions;
+{
+  struct option_conversion *current;
+  char *word;
+
+  for (; list && (word = list->word->word)[0] == '-'; list = list->next)
+    {
+      for (current = conversions; current && current->from; ++current)
+        {
+          if (STREQ(word, current->from))
+            {
+              list->word->word[1] = current->to;
+              list->word->word[2] = '\0';
+              break;
+            }
+        }
+    }
+}
diff --git a/builtins/bashgetopt.h b/builtins/bashgetopt.h
index 79be3431..256804a8 100644
--- a/builtins/bashgetopt.h
+++ b/builtins/bashgetopt.h
@@ -40,4 +40,11 @@ extern WORD_LIST *loptend;
 extern int internal_getopt PARAMS((WORD_LIST *, char *));
 extern void reset_internal_getopt PARAMS((void));
 
+struct option_conversion {
+  const char *from;
+  const char to;
+};
+
+extern void shorten_long_options PARAMS((WORD_LIST *, struct option_conversion 
*));
+
 #endif /* !__BASH_GETOPT_H */
-- 
2.44.0




reply via email to

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