2013-01-13 02:19:32 +00:00
|
|
|
/* radare - LGPL - Copyright 2010-2013 - pancake */
|
2010-03-30 22:03:59 +00:00
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_list.h>
|
|
|
|
#include <r_flags.h>
|
|
|
|
#include <r_core.h>
|
|
|
|
|
2011-11-29 11:28:02 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2010-03-30 22:03:59 +00:00
|
|
|
static char *r_core_project_file(const char *file) {
|
|
|
|
char buf[128];
|
2011-03-10 20:38:56 +00:00
|
|
|
if (!strchr (file, '/')) {
|
|
|
|
snprintf (buf, sizeof (buf), ".radare2/rdb/%s", file);
|
|
|
|
return r_str_home (buf);
|
|
|
|
}
|
|
|
|
return strdup (file);
|
2010-03-30 22:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int r_core_project_init() {
|
|
|
|
int ret;
|
2010-03-30 22:30:25 +00:00
|
|
|
char *str = r_str_home (".radare2");
|
2010-07-22 23:15:03 +00:00
|
|
|
if (str && (ret = r_sys_mkdir (str))) {
|
2010-05-17 22:20:24 +00:00
|
|
|
if (!ret) {
|
|
|
|
free (str);
|
|
|
|
str = r_str_home (".radare2/plugins");
|
|
|
|
ret = r_sys_mkdir (str);
|
2010-06-24 21:14:12 +00:00
|
|
|
if (ret) eprintf ("Cannot create ~/.radare2/plugins\n");
|
2010-05-17 22:20:24 +00:00
|
|
|
}
|
2010-03-30 22:03:59 +00:00
|
|
|
}
|
2010-07-22 23:15:03 +00:00
|
|
|
str = r_str_home (".radare2/rdb");
|
|
|
|
ret = r_sys_mkdir (str);
|
2010-03-30 22:30:25 +00:00
|
|
|
free (str);
|
2010-03-30 22:03:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:51:01 +00:00
|
|
|
R_API int r_core_project_open(RCore *core, const char *prjfile) {
|
2010-03-30 22:03:59 +00:00
|
|
|
int ret;
|
2010-05-24 16:51:01 +00:00
|
|
|
char *prj = r_core_project_file (prjfile);
|
2010-03-30 22:03:59 +00:00
|
|
|
ret = r_core_cmd_file (core, prj);
|
|
|
|
free (prj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:51:01 +00:00
|
|
|
R_API char *r_core_project_info(RCore *core, const char *prjfile) {
|
|
|
|
char buf[256], *file = NULL;
|
|
|
|
char *prj = r_core_project_file (prjfile);
|
|
|
|
FILE *fd;
|
2012-10-19 22:31:18 +00:00
|
|
|
if (prj && (fd = r_sandbox_fopen (prj, "r"))) {
|
2010-05-24 16:51:01 +00:00
|
|
|
for (;;) {
|
|
|
|
fgets (buf, sizeof (buf), fd);
|
|
|
|
if (feof (fd))
|
|
|
|
break;
|
|
|
|
if (!memcmp (buf, "e file.path = ", 14)) {
|
|
|
|
buf[strlen(buf)-1]=0;
|
2010-06-24 21:14:12 +00:00
|
|
|
file = r_str_new (buf+14);
|
2010-05-24 16:51:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose (fd);
|
|
|
|
}
|
2010-06-24 21:14:12 +00:00
|
|
|
r_cons_printf ("Project : %s\n", prj);
|
2010-05-24 16:51:01 +00:00
|
|
|
if (file)
|
2010-06-24 21:14:12 +00:00
|
|
|
r_cons_printf ("FilePath: %s\n", file);
|
2010-05-24 16:51:01 +00:00
|
|
|
free (prj);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2010-03-30 22:03:59 +00:00
|
|
|
R_API int r_core_project_save(RCore *core, const char *file) {
|
2010-03-30 23:06:26 +00:00
|
|
|
int fd, tmp, ret = R_TRUE;
|
|
|
|
char *prj;
|
|
|
|
|
|
|
|
if (file == NULL || *file == '\0')
|
|
|
|
return R_FALSE;
|
|
|
|
|
|
|
|
prj = r_core_project_file (file);
|
2010-03-30 22:03:59 +00:00
|
|
|
r_core_project_init ();
|
2012-10-19 22:31:18 +00:00
|
|
|
fd = r_sandbox_open (prj, O_BINARY|O_RDWR|O_CREAT, 0644);
|
2010-03-30 22:03:59 +00:00
|
|
|
if (fd != -1) {
|
2011-12-01 19:17:40 +00:00
|
|
|
int fdold = r_cons_singleton ()->fdout;
|
2010-03-30 22:03:59 +00:00
|
|
|
r_cons_singleton ()->fdout = fd;
|
2011-06-04 11:29:15 +00:00
|
|
|
r_cons_singleton ()->is_interactive = R_FALSE;
|
2010-03-30 22:30:25 +00:00
|
|
|
r_str_write (fd, "# r2 rdb project file\n");
|
2010-03-30 23:06:26 +00:00
|
|
|
//--
|
2010-03-30 22:30:25 +00:00
|
|
|
r_str_write (fd, "# flags\n");
|
2010-05-19 22:59:42 +00:00
|
|
|
tmp = core->flags->space_idx;
|
|
|
|
core->flags->space_idx = -1;
|
|
|
|
r_flag_list (core->flags, R_TRUE);
|
|
|
|
core->flags->space_idx = tmp;
|
2010-03-30 23:06:26 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
//--
|
2010-03-30 22:30:25 +00:00
|
|
|
r_str_write (fd, "# eval\n");
|
|
|
|
// TODO: r_str_writef (fd, "e asm.arch=%s", r_config_get ("asm.arch"));
|
2010-05-19 22:59:42 +00:00
|
|
|
r_config_list (core->config, NULL, R_TRUE);
|
2010-03-30 22:03:59 +00:00
|
|
|
r_cons_flush ();
|
2010-08-26 02:19:12 +00:00
|
|
|
r_str_write (fd, "# sections\n");
|
|
|
|
r_io_section_list (core->io, core->offset, 1);
|
|
|
|
r_cons_flush ();
|
|
|
|
r_str_write (fd, "# meta\n");
|
2012-02-01 10:49:46 +00:00
|
|
|
r_meta_list (core->anal->meta, R_META_TYPE_ANY, 1);
|
2010-08-26 02:19:12 +00:00
|
|
|
r_cons_flush ();
|
2011-06-04 11:29:15 +00:00
|
|
|
r_core_cmd (core, "ar*", 0);
|
|
|
|
r_cons_flush ();
|
|
|
|
r_core_cmd (core, "af*", 0);
|
|
|
|
r_cons_flush ();
|
2013-01-22 17:08:33 +00:00
|
|
|
r_core_cmd (core, "ah*", 0);
|
|
|
|
r_cons_flush ();
|
2010-08-26 02:19:12 +00:00
|
|
|
r_str_write (fd, "# seek\n");
|
|
|
|
r_str_writef (fd, "s 0x%08"PFMT64x, core->offset);
|
|
|
|
r_cons_flush ();
|
2010-03-30 22:03:59 +00:00
|
|
|
close (fd);
|
2011-12-01 19:17:40 +00:00
|
|
|
r_cons_singleton ()->fdout = fdold;
|
2011-06-04 11:29:15 +00:00
|
|
|
r_cons_singleton ()->is_interactive = R_TRUE;
|
2010-03-30 22:03:59 +00:00
|
|
|
} else {
|
2010-05-24 18:02:33 +00:00
|
|
|
eprintf ("Cannot open '%s' for writing\n", prj);
|
2010-03-30 22:03:59 +00:00
|
|
|
ret = R_FALSE;
|
|
|
|
}
|
|
|
|
free (prj);
|
|
|
|
return ret;
|
|
|
|
}
|