2014-01-23 00:56:35 +00:00
|
|
|
/* radare - LGPL - Copyright 2010-2014 - 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>
|
|
|
|
|
2013-05-05 16:35:14 +00:00
|
|
|
static char *r_core_project_file(RCore *core, const char *file) {
|
|
|
|
char *ret = r_file_abspath (r_config_get (
|
|
|
|
core->config, "dir.projects"));
|
|
|
|
ret = r_str_concat (ret, "/");
|
|
|
|
return r_str_concat (ret, file);
|
2010-03-30 22:03:59 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 16:35:14 +00:00
|
|
|
static int r_core_project_init(RCore *core) {
|
|
|
|
char *prjdir = r_file_abspath (r_config_get (
|
|
|
|
core->config, "dir.projects"));
|
|
|
|
int ret = r_sys_rmkdir (prjdir);
|
|
|
|
if (!ret) eprintf ("Cannot mkdir dir.projects\n");
|
|
|
|
free (prjdir);
|
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;
|
2013-08-25 20:58:43 +00:00
|
|
|
char *prj;
|
|
|
|
if (!prjfile || !*prjfile)
|
|
|
|
return R_FALSE;
|
|
|
|
prj = r_core_project_file (core, prjfile);
|
2010-03-30 22:03:59 +00:00
|
|
|
ret = r_core_cmd_file (core, prj);
|
2013-04-18 01:58:44 +00:00
|
|
|
r_anal_project_load (core->anal, prjfile);
|
2010-03-30 22:03:59 +00:00
|
|
|
free (prj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:51:01 +00:00
|
|
|
R_API char *r_core_project_info(RCore *core, const char *prjfile) {
|
2013-05-05 16:35:14 +00:00
|
|
|
char buf[256], *file = NULL, *prj = r_core_project_file (core, prjfile);
|
2013-02-07 08:41:05 +00:00
|
|
|
FILE *fd = prj? r_sandbox_fopen (prj, "r"): NULL;
|
|
|
|
for (;fd;) {
|
|
|
|
fgets (buf, sizeof (buf), fd);
|
|
|
|
if (feof (fd))
|
|
|
|
break;
|
2013-12-28 01:34:15 +00:00
|
|
|
if (!memcmp (buf, "\"e file.path = ", 15)) {
|
|
|
|
buf[strlen(buf)-2]=0;
|
|
|
|
file = r_str_new (buf+15);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// TODO: deprecate before 1.0
|
2013-02-07 08:41:05 +00:00
|
|
|
if (!memcmp (buf, "e file.path = ", 14)) {
|
|
|
|
buf[strlen(buf)-1]=0;
|
|
|
|
file = r_str_new (buf+14);
|
|
|
|
break;
|
2010-05-24 16:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-18 10:15:11 +00:00
|
|
|
if (fd) fclose (fd);
|
2013-12-28 01:34:15 +00:00
|
|
|
r_cons_printf ("%s\n", prj);
|
2013-02-07 08:41:05 +00:00
|
|
|
if (file) 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) {
|
2013-02-07 08:41:05 +00:00
|
|
|
int fd, fdold, tmp, ret = R_TRUE;
|
2010-03-30 23:06:26 +00:00
|
|
|
char *prj;
|
|
|
|
|
|
|
|
if (file == NULL || *file == '\0')
|
|
|
|
return R_FALSE;
|
|
|
|
|
2013-05-05 16:35:14 +00:00
|
|
|
prj = r_core_project_file (core, file);
|
2013-12-28 01:34:15 +00:00
|
|
|
if (r_file_is_directory (prj)) {
|
|
|
|
eprintf ("Error: Target is a directory\n");
|
2014-04-29 22:59:43 +00:00
|
|
|
free (prj);
|
2013-12-28 01:34:15 +00:00
|
|
|
return R_FALSE;
|
|
|
|
}
|
2013-05-05 16:35:14 +00:00
|
|
|
r_core_project_init (core);
|
2013-04-18 01:58:44 +00:00
|
|
|
r_anal_project_save (core->anal, prj);
|
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) {
|
2013-02-07 08:41:05 +00:00
|
|
|
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");
|
|
|
|
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");
|
2014-01-23 03:00:01 +00:00
|
|
|
r_meta_list (core->anal, R_META_TYPE_ANY, 1);
|
2010-08-26 02:19:12 +00:00
|
|
|
r_cons_flush ();
|
2013-10-24 11:59:19 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
2013-12-28 01:34:15 +00:00
|
|
|
snprintf (buf, sizeof (buf), "%s.d/xrefs", prj);
|
2013-10-24 11:59:19 +00:00
|
|
|
sdb_file (core->anal->sdb_xrefs, buf);
|
|
|
|
sdb_sync (core->anal->sdb_xrefs);
|
|
|
|
}
|
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;
|
|
|
|
}
|