Browse Source

Add command completion

Yann Weber 1 year ago
parent
commit
8d91517584
2 changed files with 74 additions and 5 deletions
  1. 69
    5
      completion.c
  2. 5
    0
      completion.h

+ 69
- 5
completion.c View File

@@ -20,8 +20,28 @@ char **asmsh_completion(const char *buf, const char *text, int start, int end)
20 20
 {
21 21
 	if(end == 0)
22 22
 	{
23
-		// TODO catenate all !
24
-		return asmsh_compl_instr(text);
23
+		char **instr, **cmds, **ptr;
24
+		int ilen, clen;
25
+		cmds = asmsh_compl_cmds(text);
26
+		instr = asmsh_compl_instr(text);
27
+
28
+		ilen=clen=0;
29
+		for(ptr=cmds; *ptr; ptr++) { clen++; }
30
+		for(ptr=instr; *ptr; ptr++) { ilen++; }
31
+
32
+		ptr = realloc(cmds, sizeof(*cmds) * (ilen+clen+1));
33
+		if(!ptr)
34
+		{
35
+			perror("Unable to realloc all completions");
36
+			free(cmds);
37
+			free(instr);
38
+			return NULL;
39
+		}
40
+		cmds=ptr;
41
+		ptr=&cmds[clen];
42
+		memcpy(&cmds[clen], instr, ilen*sizeof(*instr));
43
+		cmds[ilen+clen]=NULL;
44
+		return cmds;
25 45
 	}
26 46
 	if(*buf != '.')
27 47
 	{
@@ -31,8 +51,7 @@ char **asmsh_completion(const char *buf, const char *text, int start, int end)
31 51
 		}
32 52
 		return asmsh_compl_arg(buf, text, start);
33 53
 	}
34
-	// command completion handling
35
-	return NULL;
54
+	return asmsh_compl_cmds(text);
36 55
 }
37 56
 
38 57
 
@@ -45,12 +64,12 @@ char **asmsh_compl_instr(const char *start)
45 64
 	char **ret;
46 65
 	int start_len, cmp;
47 66
 	ret = malloc(icount * 2 * sizeof(char*));
48
-	bzero(ret, icount * sizeof(char*));
49 67
 	if(!ret)
50 68
 	{
51 69
 		perror("Unable to allocate completion result");
52 70
 		return NULL;
53 71
 	}
72
+	bzero(ret, icount * 2 * sizeof(char*));
54 73
 	if(!start || !*start)
55 74
 	{
56 75
 		i=0;
@@ -131,6 +150,51 @@ err_strdup:
131 150
 }
132 151
 
133 152
 
153
+char **asmsh_compl_cmds(const char *start)
154
+{
155
+	const size_t ccount = sizeof(asmsh_CMDS)/sizeof(*asmsh_CMDS);
156
+	int i, slen, cmp;
157
+	char **ret;
158
+	const asmsh_cmd_t *ccmd;
159
+
160
+	ret = malloc(sizeof(*ret) * (ccount+1));
161
+	if(!ret)
162
+	{
163
+		perror("Unable to allocate cmd completion result");
164
+		return NULL;
165
+	}
166
+	bzero(ret, sizeof(*ret) * (ccount+1));
167
+
168
+	slen = (!start || !*start)?0:strlen(start);
169
+	for(i=0; i<ccount; i++)
170
+	{
171
+		ccmd = &asmsh_CMDS[i];
172
+		if(!ccmd->str) { break; }
173
+		if(!slen || !(cmp = strncmp(start, ccmd->str, slen)))
174
+		{
175
+			if(!(ret[i] = strdup(ccmd->str)))
176
+			{
177
+				perror("Unable to strdup cmd completion");
178
+				goto errdup;
179
+			}
180
+		}
181
+		if(slen && cmp < 0)
182
+		{
183
+			break;
184
+		}
185
+	}
186
+	return ret;
187
+
188
+errdup:
189
+	i--;
190
+	for(; i>=0; i--)
191
+	{
192
+		free(ret[i]);
193
+	}
194
+	free(ret);
195
+	return NULL;
196
+}
197
+
134 198
 
135 199
 /// match against macro
136 200
 static int _completion_macro_match(const char *macro, const char *buf, int mlen);

+ 5
- 0
completion.h View File

@@ -26,6 +26,8 @@
26 26
 #include <readline/history.h>
27 27
 #endif
28 28
 
29
+#include "shell_cmds.h"
30
+
29 31
 #define ASHCOMP_NOARG	0x00
30 32
 #define ASHCOMP_REG8	0x01
31 33
 #define ASHCOMP_REG16	0x02
@@ -66,6 +68,9 @@ char **asmsh_rl_completion(const char *text, int start, int end);
66 68
 /** Return all possible instructions given a start */
67 69
 char **asmsh_compl_instr(const char *start);
68 70
 
71
+/** Return possible commands given a start */
72
+char **asmsh_compl_cmds(const char *start);
73
+
69 74
 char **asmsh_compl_arg(const char *buf, const char *text, int start);
70 75
 
71 76
 static const char * const M_conds[] = {"o","no","b","c","nae","ae","nb","nc","e","z","ne","nz","be","na","a","nbe","s","ns","p","pe","np","po","l","nge","ge","nl","le","ng","g","nle", NULL};

Loading…
Cancel
Save