* Autocomplete 'o' and '.' with filename in filesystem

- Leaks a lot.. but mostly works :)
This commit is contained in:
pancake 2011-02-13 01:37:02 +01:00
parent 13b4c2d9f1
commit 8d60673253
3 changed files with 68 additions and 0 deletions

View File

@ -84,7 +84,9 @@ static const char *radare_argv[] = {
"q",
"f", "fr", "f-", "f*", "fs", "fS", "fr", "f?",
"m", "m*", "ml", "m-", "my", "mg", "md", "mp", "m?",
"o", "o-",
"x",
".",
"r", "r+", "r-",
"b", "bf", "b?",
"/", "//", "/a", "/c", "/m", "/x", "/v",
@ -119,6 +121,54 @@ static int autocomplete(RLine *line) {
line->completion.argc = i;
line->completion.argv = tmp_argv;
} else
if ((!memcmp (line->buffer.data, "o ", 2)) ||
!memcmp (line->buffer.data, ". ", 2)) {
// XXX: SO MANY FUCKING MEMORY LEAKS
char *str, *p, *path;
RList *list;
int n, i = 0;
int sdelta = 2; //(line->buffer.data[1]==' ')?2:3;
if (!line->buffer.data[sdelta]) {
path = r_sys_getcwd ();
} else {
path = strdup (line->buffer.data+sdelta);
}
p = r_str_lchr (path, '/');
if (p) {
if (p==path) {
path = "/"; // path[1]=0;
} else *p = 0;
p++;
}
if (p && !*p)
p = "";
n = strlen (p);
#if 0
printf ("DIR(%s)\n", path);
printf ("FILE(%s)\n", p);
printf ("FILEN %d\n", n);
#endif
list = r_sys_dir (path);
if (list) {
char buf[4096];
r_list_foreach (list, iter, str) {
if (*str == '.')
continue;
if (!*p || !memcmp (str, p, n)) {
snprintf (buf, sizeof (buf), "%s%s%s",
path, strlen(path)>1?"/":"", str);
tmp_argv[i++] = strdup (buf);
if (i==TMP_ARGV_SZ)
break;
}
}
// XXX LEAK r_list_destroy (list);
} else eprintf ("\nInvalid directory\n");
tmp_argv[i] = NULL;
line->completion.argc = i;
line->completion.argv = tmp_argv;
//free (path);
} else
if ((!memcmp (line->buffer.data, "s ", 2)) ||
(!memcmp (line->buffer.data, "b ", 2)) ||
(!memcmp (line->buffer.data, "/v ", 3)) ||

View File

@ -290,6 +290,7 @@ R_API boolt r_file_exist(const char *str);
R_API char *r_file_slurp_line(const char *file, int line, int context);
R_API ut64 r_sys_now();
R_API RList *r_sys_dir(const char *path);
R_API void r_sys_perror(const char *fun);
#if __WINDOWS__
#define r_sys_mkdir(x) (CreateDirectory(x,NULL)!=0)

View File

@ -28,6 +28,23 @@ R_API ut64 r_sys_now(void) {
return ret;
}
R_API RList *r_sys_dir(const char *path) {
DIR *dir = opendir (path);
struct dirent *entry;
if (dir) {
RList *list = r_list_new ();
if (list) {
list->free = free;
while ((entry = readdir (dir))) {
r_list_append (list, strdup (entry->d_name));
}
closedir (dir);
return list;
}
}
return NULL;
}
R_API char *r_sys_cmd_strf(const char *fmt, ...) {
char *ret, cmd[4096];
va_list ap;