* Add filesystem prompt using ms command (Thanks Adriana)

* Add r_str_chop_path support for ../
This commit is contained in:
earada 2011-02-18 18:22:51 +01:00
parent 9646275d38
commit 4cb8868ecc
4 changed files with 122 additions and 3 deletions

View File

@ -1037,6 +1037,12 @@ static int cmd_mount(void *data, const char *_input) {
r_fs_close (core->fs, file);
} else eprintf ("Cannot open file\n");
break;
case 's':
input++;
if (input[0]==' ')
input++;
r_fs_prompt (core->fs, input);
break;
case 'y':
eprintf ("TODO\n");
break;
@ -1053,6 +1059,7 @@ static int cmd_mount(void *data, const char *_input) {
" md / ; list directory contents for path\n"
" mp ; list all supported partition types\n"
" mp msdos 0 ; show partitions in msdos format at offset 0\n"
" ms /mnt ; open filesystem prompt at /mnt\n"
" m? ; show this help\n"
"TODO: support multiple mountpoints and RFile IO's (need io+core refactor)\n"
);

View File

@ -181,7 +181,7 @@ static int parhook (struct grub_disk *disk, struct grub_partition *par) {
return 0;
}
R_API RList *r_fs_partitions(RFS *fs, const char *ptype, ut64 delta) {
R_API RList *r_fs_partitions (RFS *fs, const char *ptype, ut64 delta) {
struct grub_partition_map *gpm = NULL;
if (!strcmp (ptype, "msdos"))
gpm = &grub_msdos_partition_map;
@ -215,3 +215,94 @@ R_API RList *r_fs_partitions(RFS *fs, const char *ptype, ut64 delta) {
" msdos, apple, sun, sunpc, amiga, bsdlabel, acorn, gpt", ptype);
return NULL;
}
R_API int r_fs_prompt (RFS *fs, char *root) {
char buf[1024];
char path[1024];
char str[2048];
char *input;
RList *list;
RListIter *iter;
RFSFile *file;
strncpy (path, root, sizeof (path)-1);
for (;;) {
printf (Color_MAGENTA"%s> "Color_RESET, path);
fflush (stdout);
fgets (buf, sizeof (buf)-1, stdin);
if (feof (stdin)) break;
buf[strlen (buf)-1] = '\0';
if (!strcmp (buf, "q") || !strcmp (buf, "exit"))
return R_TRUE;
else if (!strcmp (buf, "ls")) {
list = r_fs_dir (fs, path);
if (list) {
r_list_foreach (list, iter, file)
printf ("%c %s\n", file->type, file->name);
r_list_free (list);
} else printf ("Unknown path\n");
} else if (!strncmp (buf, "cd ", 3)) {
input = buf+3;
while (input[0] == ' ')
input++;
strncpy (str, path, sizeof(str)-1);
if (input[0] == '/')
strncpy (path, root, sizeof (path)-1);
strcat (path, "/");
strcat (path, input);
r_str_chop_path (path);
list = r_fs_dir (fs, path);
if (!r_list_empty (list))
r_list_free (list);
else {
strncpy (path, str, sizeof (path)-1);
printf ("Unknown path\n");
}
} else if (!strncmp (buf, "cat ", 4)) {
input = buf+3;
while (input[0] == ' ')
input++;
if (input[0] == '/')
strncpy (str, root, sizeof (str)-1);
else
strncpy (str, path, sizeof (str)-1);
strcat (str, "/");
strcat (str, input);
file = r_fs_open (fs, str);
if (file) {
r_fs_read (fs, file, 0, file->size);
write (1, file->data, file->size);
r_fs_close (fs, file);
} else printf ("Cannot open file\n");
} else if (!strncmp (buf, "get ",4)){
input = buf+3;
while (input[0] == ' ')
input++;
if (input[0] == '/')
strncpy (str, root, sizeof (str)-1);
else
strncpy (str, path, sizeof (str)-1);
strcat (str, "/");
strcat (str, input);
file = r_fs_open (fs, str);
if (file) {
r_fs_read (fs, file, 0, file->size);
r_file_dump (input, file->data, file->size);
r_fs_close (fs, file);
} else printf ("Cannot open file\n");
} else if (!strcmp (buf, "help") || !strcmp (buf, "?")) {
printf(
"Commands:\n"
" ls ; list current directory\n"
" cd path ; change current directory\n"
" cat file ; print contents of file\n"
" q/exit ; leave prompt mode\n"
" ?/help ; show this help\n"
);
}
}
clearerr (stdin);
printf ("\n");
return R_TRUE;
}

View File

@ -4,6 +4,7 @@
#include <r_types.h>
#include <r_list.h>
#include <r_io.h>
#include <r_cons.h>
struct r_fs_plugin_t;
struct r_fs_root_t;
@ -78,6 +79,7 @@ R_API int r_fs_read (RFS* fs, RFSFile *file, ut64 addr, int len);
R_API RFSFile *r_fs_slurp(RFS* fs, const char *path);
R_API RList *r_fs_dir(RFS* fs, const char *path);
R_API RList *r_fs_partitions(RFS* fs, const char *ptype, ut64 delta);
R_API int r_fs_prompt (RFS *fs, char *root);
/* file.c */
R_API RFSFile *r_fs_file_new (RFSRoot *root, const char *path);

View File

@ -11,12 +11,31 @@ static const char *nullstr = "";
static const char *nullstr_c = "(null)";
R_API void r_str_chop_path (char *s) {
char *src, *dst;
char *src, *dst, *p;
int i = 0;
src = s+1;
dst = s+1;
while (*src) {
if (*src != '/' || *(src-1) != '/') {
if (*(src-1) == '/' && *src == '.' && *(src+1) == '.') {
if (*(src+2) == '/' || *(src+2) == '\0') {
p = dst-1;
while (s != p) {
if (*p == '/') {
if (i) {
dst = p+1;
i = 0;
break;
} i++;
}
p--;
}
src = src+2;
} else {
*dst = *src;
dst++;
}
} else if (*src != '/' || *(src-1) != '/') {
*dst = *src;
dst++;
}