[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] Add aliases (ash shell) to a tab completion feature.
From:       sergio.morlans () gmail ! com
Date:       2022-09-21 8:57:14
Message-ID: 20220921085714.3927437-1-sergio.morlans () gmail ! com
[Download RAW message or body]

From: Sergio Morlans <sergio.morlans@atlascopco.com>

---
 include/ash_alias.h | 15 +++++++++++++++
 include/libbb.h     |  7 +++++++
 libbb/lineedit.c    | 16 ++++++++++++++++
 shell/ash.c         | 12 ++++--------
 4 files changed, 42 insertions(+), 8 deletions(-)
 create mode 100644 include/ash_alias.h

diff --git a/include/ash_alias.h b/include/ash_alias.h
new file mode 100644
index 000000000..27a240a77
--- /dev/null
+++ b/include/ash_alias.h
@@ -0,0 +1,15 @@
+/*
+ * busybox alias archive data structures
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+#ifndef ASH_ALIAS_H
+#define ASH_ALIAS_H
+
+struct alias {
+	struct alias *next;
+	char *name;
+	char *val;
+	int flag;
+};
+
+#endif
\ No newline at end of file
diff --git a/include/libbb.h b/include/libbb.h
index 19ed9ec09..158860803 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -52,6 +52,9 @@
 #include <sys/wait.h>
 #include <termios.h>
 #include <time.h>
+#if ENABLE_ASH_ALIAS
+# include <ash_alias.h>
+#endif
 #include <sys/param.h>
 #include <pwd.h>
 #include <grp.h>
@@ -1931,6 +1934,10 @@ typedef struct line_input_t {
 	int timeout;
 # if ENABLE_FEATURE_TAB_COMPLETION
 #  if ENABLE_SHELL_ASH
+#   if ENABLE_ASH_ALIAS
+	struct alias **app;
+	int tab_size;
+#   endif
 	const char *path_lookup;
 #   define EDITING_HAS_path_lookup 1
 #  else
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 697f2a577..e45c9ae56 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -1205,6 +1205,10 @@ static NOINLINE void input_tab(smallint *lastWasTab)
 	char *chosen_match;
 	char *match_buf;
 	size_t len_found;
+# if ENABLE_ASH_ALIAS
+	struct alias *ap;
+	int in;
+# endif
 	/* Length of string used for matching */
 	unsigned match_pfx_len = match_pfx_len;
 	int find_type;
@@ -1266,6 +1270,18 @@ static NOINLINE void input_tab(smallint *lastWasTab)
 	if (!matches)
 		match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
 
+# if ENABLE_ASH_ALIAS
+	if(state->app != NULL)
+	{
+		for (in = 0; in < state->tab_size; in++) {
+			for (ap = state->app[in]; ap; ap = ap->next) {
+				if(is_prefixed_with(ap->name, match_buf) && find_type == 0)
+					add_match(xstrdup(ap->name));
+			}
+		}
+	}
+# endif
+
 	/* Account for backslashes which will be inserted
 	 * by quote_special_chars() later */
 	{
diff --git a/shell/ash.c b/shell/ash.c
index 326f8b2a9..2a84ba4f3 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -3405,14 +3405,6 @@ static const uint8_t syntax_index_table[] ALIGN1 = {
 #define ALIASINUSE 1
 #define ALIASDEAD  2
 
-struct alias {
-	struct alias *next;
-	char *name;
-	char *val;
-	int flag;
-};
-
-
 static struct alias **atab; // [ATABSIZE];
 #define INIT_G_alias() do { \
 	atab = xzalloc(ATABSIZE * sizeof(atab[0])); \
@@ -10812,6 +10804,10 @@ preadfd(void)
 # endif
 # if ENABLE_FEATURE_TAB_COMPLETION
 		line_input_state->path_lookup = pathval();
+#  if ENABLE_ASH_ALIAS
+		line_input_state->app = atab;
+		line_input_state->tab_size = ATABSIZE;
+#  endif
 # endif
 		reinit_unicode_for_ash();
  again:
-- 
2.25.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic