summaryrefslogtreecommitdiffstats
path: root/common/complete.c
diff options
context:
space:
mode:
authorAlexander Aring <a.aring@phytec.de>2011-12-21 09:07:57 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2011-12-21 12:38:14 +0100
commit56d0d5012f2771fe6a5667bec1ef2334c5a6c005 (patch)
tree44e1edac7ec8f771062e0052967173a94b357aff /common/complete.c
parent84f0a81899f92b8fb7800136a9fbd61e75a9745a (diff)
downloadbarebox-56d0d5012f2771fe6a5667bec1ef2334c5a6c005.tar.gz
auto-completion: add auto-completion for path files
Add auto-completion for path files. Signed-off-by: Alexander Aring <a.aring@phytec.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/complete.c')
-rw-r--r--common/complete.c70
1 files changed, 68 insertions, 2 deletions
diff --git a/common/complete.c b/common/complete.c
index 46ba871..6d5349b 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -27,6 +27,7 @@
#include <libgen.h>
#include <command.h>
#include <stringlist.h>
+#include <environment.h>
static int file_complete(struct string_list *sl, char *instr)
{
@@ -55,7 +56,7 @@ static int file_complete(struct string_list *sl, char *instr)
strcat(tmp, "/");
else
strcat(tmp, " ");
- string_list_add(sl, tmp);
+ string_list_add_sorted(sl, tmp);
}
}
@@ -67,6 +68,69 @@ out:
return 0;
}
+static int path_command_complete(struct string_list *sl, char *instr)
+{
+ struct stat s;
+ DIR *dir;
+ struct dirent *d;
+ char tmp[PATH_MAX];
+ char *path, *p, *n;
+
+ p = path = strdup(getenv("PATH"));
+
+ if (!path)
+ return -1;
+
+ while (p) {
+ n = strchr(p, ':');
+ if (n)
+ *n++ = '\0';
+ if (*p == '\0') {
+ p = n;
+ continue;
+ }
+ dir = opendir(p);
+
+ /* We need to check all PATH dirs, so if one failed,
+ * try next */
+ if (!dir) {
+ p = n;
+ continue;
+ }
+
+ while ((d = readdir(dir))) {
+ if (!strcmp(d->d_name, ".") ||
+ !strcmp(d->d_name, ".."))
+ continue;
+
+ if (!strncmp(instr, d->d_name, strlen(instr))) {
+ strcpy(tmp, d->d_name);
+ if (!stat(tmp, &s) &&
+ S_ISDIR(s.st_mode))
+ continue;
+ else
+ strcat(tmp, " ");
+
+ /* This function is called
+ * after command_complete,
+ * so we check if a double
+ * entry exist */
+ if (string_list_contains
+ (sl, tmp) == 0) {
+ string_list_add_sorted(sl, tmp);
+ }
+ }
+ }
+
+ closedir(dir);
+ p = n;
+ }
+
+ free(path);
+
+ return 0;
+}
+
static int command_complete(struct string_list *sl, char *instr)
{
struct command *cmdtp;
@@ -121,8 +185,10 @@ int complete(char *instr, char **outstr)
t++;
file_complete(&sl, t);
instr = t;
- } else
+ } else {
command_complete(&sl, instr);
+ path_command_complete(&sl, instr);
+ }
pos = strlen(instr);