Skip to contentSkip to conversation

Tab completion for haltest tool

PATCH In review Jerzy Kasenberg 2 participants 5 messages Oct 18, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Tab completion for haltest tool

This patchset add tab completion to haltest so using this tool will be much easier.

It allows to build for andriod 4.3 and for host with autotools.

Jerzy Kasenberg (3):
android: Add tab completion to haltest
android: Fix error in draw line in haltest tool
android: Add completion for adapter to haltest

 Makefile.android               |    2 +
 android/Android.mk             |    1 +
 android/client/haltest.c       |   11 +-
 android/client/if-bt.c         |  331 ++++++++++++++++++++++++++++++++++------
 android/client/if-main.h       |   44 +++++-
 android/client/tabcompletion.c |  313 +++++++++++++++++++++++++++++++++++++
 android/client/terminal.c      |    4 +-
 android/client/textconv.c      |   21 +++
 android/client/textconv.h      |    4 +
 9 files changed, 673 insertions(+), 58 deletions(-)
 create mode 100644 android/client/tabcompletion.c
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/3 android: Add tab completion to haltest

This patch adds tab completion to interfaces, methods and parameters in haltest.

---
 Makefile.android               |    2 +
 android/Android.mk             |    1 +
 android/client/if-main.h       |   28 +++-
 android/client/tabcompletion.c |  313 ++++++++++++++++++++++++++++++++++++++++
 android/client/terminal.c      |    2 +-
 5 files changed, 342 insertions(+), 4 deletions(-)
 create mode 100644 android/client/tabcompletion.c
Makefile.android +2 -0
index 56caf78..dcaca3c 100644
@@ -42,6 +42,7 @@ android_haltest_SOURCES = android/client/haltest.c \
 				android/client/terminal.c \
 				android/client/history.c \
 				android/client/textconv.c \
+				android/client/tabcompletion.c \
 				android/client/if-bt.c \
 				android/client/hwmodule.c
 
@@ -67,6 +68,7 @@ EXTRA_DIST += android/client/terminal.c \
 		android/client/history.c \
 		android/client/if-bt.c \
 		android/client/textconv.c \
+		android/client/tabcompletion.c \
 		android/client/textconv.h \
 		android/client/if-main.h \
 		android/client/pollhandler.h \
android/Android.mk +1 -0
index 5858c3d..30b2169 100644
@@ -78,6 +78,7 @@ LOCAL_SRC_FILES := \
 	client/terminal.c \
 	client/history.c \
 	client/textconv.c \
+	client/tabcompletion.c \
 	client/if-bt.c \
 
 LOCAL_SHARED_LIBRARIES := libhardware
android/client/if-main.h +25 -3
index 9cac7ef..3fb3007 100644
@@ -56,9 +56,12 @@ extern const struct interface bluetooth_if;
 /* Interfaces that will show up in tool (first part of command line) */
 extern const struct interface *interfaces[];
 
-#define METHOD(name, func) {name, func}
-#define STD_METHOD(m) {#m, m##_p}
-#define END_METHOD {"", NULL}
+#define METHOD(name, func, comp, help) {name, func, comp, help}
+#define STD_METHOD(m) {#m, m##_p, NULL, NULL}
+#define STD_METHODC(m) {#m, m##_p, m##_c, NULL}
+#define STD_METHODH(m, h) {#m, m##_p, NULL, h}
+#define STD_METHODCH(m, h) {#m, m##_p, m##_c, h}
+#define END_METHOD {"", NULL, NULL, NULL}
 
 /*
  * Function to parse argument for function, argv[0] and argv[1] are already
@@ -69,12 +72,31 @@ extern const struct interface *interfaces[];
 typedef void (*parse_and_call)(int argc, const char **argv);
 
 /*
+ * This is prototype of function that will return string for given number.
+ * Purpose is to enumerate string for auto completion.
+ * Function of this type will always be called in loop.
+ * First time function is called i = 0, then if function returns non-NULL
+ * it will be called again till for some value of i it will return NULL
+ */
+typedef const char *(*enum_func)(void *user, int i);
+
+/*
+ * This is prototype of function that when given argc, argv will
+ * fill penum_func with pointer to function that will enumerate
+ * parameters for argc argument, puser will be passed to penum_func.
+ */
+typedef void (*tab_complete)(int argc, const char **argv,
+					enum_func *penum_func, void **puser);
+
+/*
  * For each method there is name and two functions to parse command line
  * and call proper hal function on.
  */
 struct method {
 	const char *name;
 	parse_and_call func;
+	tab_complete complete;
+	const char *help;
 };
 
 int haltest_error(const char *format, ...);
android/client/tabcompletion.c new file +313 -0

313 lines not on this page · raw message

android/client/terminal.c +1 -1
index b484ef6..0421633 100644
@@ -453,7 +453,7 @@ void terminal_process_char(int c, void (*process_line)(char *line))
 		break;
 	case '\t':
 		/* tab processing */
-		/* TODO Add completion here */
+		process_tab(line_buf, line_buf_ix);
 		break;
 	case KEY_BACKSPACE:
 		if (line_buf_ix <= 0)
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 2/3 android: Fix error in draw line in haltest tool

In reply to Jerzy Kasenberg

This fixes small error that showed up when tab completion inserted characters before end of line.
Cursor in line was not moved correctly.

---
 android/client/terminal.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
android/client/terminal.c +1 -1
index 0421633..8dd3a25 100644
@@ -140,7 +140,7 @@ void terminal_draw_command_line(void)
 		putchar('>');
 
 	/* move cursor to it's place */
-	terminal_move_cursor(line_len - line_buf_ix);
+	terminal_move_cursor(line_buf_ix - line_len);
 }
 
 /* inserts string into command line at cursor position */
Signature · 1 line
1.7.9.5
#3 lore raw

PATCH 3/3 android: Add completion for adapter to haltest

In reply to Jerzy Kasenberg

This patch adds all completion functions for adapter methods. It also adds short help lines for all methods that require arguments.

---
 android/client/haltest.c  |   11 +-
 android/client/if-bt.c    |  331 ++++++++++++++++++++++++++++++++++++++-------
 android/client/if-main.h  |   16 +++
 android/client/textconv.c |   21 +++
 android/client/textconv.h |    4 +
 5 files changed, 330 insertions(+), 53 deletions(-)
android/client/haltest.c +6 -5
index 6dcabbc..6b37f33 100644
@@ -103,11 +103,12 @@ static void process_line(char *line_buffer)
 			continue;
 		}
 		if (argc < 2 || strcmp(argv[1], "?") == 0) {
-			j = 0;
-			while (strcmp(interfaces[i]->methods[j].name, "")) {
-				haltest_info("%s %s\n", argv[0],
-						interfaces[i]->methods[j].name);
-				++j;
+			struct method *m = &interfaces[i]->methods[0];
+			while (strcmp(m->name, "")) {
+				haltest_info("%s %s %s\n", argv[0],
+						m->name,
+						(m->help ? m->help : ""));
+				m++;
 			}
 			return;
 		}
android/client/if-bt.c +283 -48

520 lines not on this page · raw message

android/client/if-main.h +16 -0
index 3fb3007..b6bbf05 100644
@@ -103,6 +103,12 @@ int haltest_error(const char *format, ...);
 int haltest_info(const char *format, ...);
 int haltest_warn(const char *format, ...);
 
+/*
+ * Enumerator for discovered devices, to be used as tab completion enum_func
+ */
+const char *enum_devices(void *v, int i);
+void add_remote_device(const bt_bdaddr_t *addr);
+
 /* Helper macro for executing function on interface and printing BT_STATUS */
 #define EXEC(f, ...) \
 	{ \
@@ -119,3 +125,13 @@ int haltest_warn(const char *format, ...);
 
 #define RETURN_IF_NULL(x) \
 	do { if (!x) { haltest_error("%s is NULL\n", #x); return; } } while (0)
+
+#define VERIFY_ADDR_ARG(n, adr) \
+	do { \
+		if (n < argc) \
+			str2bt_bdaddr_t(argv[n], adr); \
+		else { \
+			haltest_error("No address specified\n");\
+			return;\
+		} \
+	} while (0)
android/client/textconv.c +21 -0
index eebad70..3493b1c 100644
@@ -203,3 +203,24 @@ void str2bt_uuid_t(const char *str, bt_uuid_t *uuid)
 		str += 2;
 	}
 }
+
+const char *enum_defines(void *v, int i)
+{
+	const struct int2str *m = v;
+
+	return m[i].str != NULL ? m[i].str : NULL;
+}
+
+const char *enum_strings(void *v, int i)
+{
+	const char **m = v;
+
+	return m[i] != NULL ? m[i] : NULL;
+}
+
+const char *enum_one_string(void *v, int i)
+{
+	const char *m = v;
+
+	return (i == 0) && (m[0] != 0) ? m : NULL;
+}
android/client/textconv.h +4 -0
index 8fe976c..88da641 100644
@@ -54,7 +54,11 @@ struct int2str {
 
 int int2str_findint(int v, const struct int2str m[]);
 int int2str_findstr(const char *str, const struct int2str m[]);
+const char *enum_defines(void *v, int i);
+const char *enum_strings(void *v, int i);
+const char *enum_one_string(void *v, int i);
 
+#define TYPE_ENUM(type) ((void *)&__##type##2str[0])
 #define DECINTMAP(type) \
 extern struct int2str __##type##2str[]; \
 const char *type##2##str(type v); \
Signature · 1 line
1.7.9.5
#4 lore raw
In reply to Jerzy Kasenberg

Hi Jerzy,

Quoted 10 lines · Jerzy Kasenberg

This patchset add tab completion to haltest so using this tool will be much easier.

It allows to build for andriod 4.3 and for host with autotools.

Jerzy Kasenberg (3):
android: Add tab completion to haltest
android: Fix error in draw line in haltest tool
android: Add completion for adapter to haltest

Quoted 11 lines
 Makefile.android               |    2 +
 android/Android.mk             |    1 +
 android/client/haltest.c       |   11 +-
 android/client/if-bt.c         |  331 ++++++++++++++++++++++++++++++++++------
 android/client/if-main.h       |   44 +++++-
 android/client/tabcompletion.c |  313 +++++++++++++++++++++++++++++++++++++
 android/client/terminal.c      |    4 +-
 android/client/textconv.c      |   21 +++
 android/client/textconv.h      |    4 +
 9 files changed, 673 insertions(+), 58 deletions(-)
 create mode 100644 android/client/tabcompletion.c

All three patches have been applied. Thanks.

Johan