Remove file.path and file.lastpath and add RProject ##projects

This commit is contained in:
pancake 2021-01-02 12:25:28 +01:00 committed by pancake
parent 020c3056d0
commit cf91324937
12 changed files with 163 additions and 94 deletions

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2019 - pancake, nibble */
/* radare - LGPL - Copyright 2009-2020 - pancake, nibble */
#include <r_anal.h>
#include <r_util.h>

View File

@ -11,7 +11,7 @@ OBJS+=fortune.o hack.o vasm.o patch.o cbin.o corelog.o rtr.o cmd_api.o
OBJS+=carg.o canal.o project.o gdiff.o casm.o disasm.o cplugin.o
OBJS+=vmenus.o vmenus_graph.o vmenus_zigns.o zdiff.o citem.o
OBJS+=task.o panels.o pseudo.o vmarks.o anal_tp.o anal_objc.o blaze.o cundo.o
OBJS+=cannotated_code.o
OBJS+=cannotated_code.o cproject.o
CFLAGS+=-I../../shlr/heap/include
CFLAGS+=-I../../shlr/tree-sitter/lib/include -I../../shlr/radare2-shell-parser/src/tree_parser

View File

@ -2135,24 +2135,6 @@ static bool cb_io_oxff(void *user, void *data) {
return true;
}
static bool cb_filepath(void *user, void *data) {
RCore *core = (RCore *) user;
RConfigNode *node = (RConfigNode *) data;
char *pikaboo = strstr (node->value, "://");
if (pikaboo) {
if (pikaboo[3] == '/') {
r_config_set (core->config, "file.lastpath", node->value);
char *ovalue = node->value;
node->value = strdup (pikaboo + 3);
free (ovalue);
return true;
}
return false;
}
r_config_set (core->config, "file.lastpath", node->value);
return true;
}
static bool cb_ioautofd(void *user, void *data) {
RCore *core = (RCore *) user;
RConfigNode *node = (RConfigNode *) data;
@ -2553,6 +2535,37 @@ static bool cb_binverbose(void *user, void *data) {
return true;
}
static bool cb_prjname(void *user, void *data) {
RCore *core = (RCore *) user;
RConfigNode *node = (RConfigNode *) data;
const char *prjname = node->value;
if (*prjname == '?') {
r_core_project_list (core, 0);
return false;
}
if (r_project_is_loaded (core->prj)) {
if (*prjname) {
if (!strcmp (prjname, core->prj->name)) {
return false;
}
if (r_project_rename (core->prj, prjname)) {
return true;
}
eprintf ("Cannot rename project.\n");
} else {
r_project_close (core->prj);
}
} else {
if (*prjname) {
if (r_project_open (core->prj, prjname, NULL)) {
return true;
}
eprintf ("Cannot open project.\n");
}
}
return false;
}
static bool cb_rawstr(void *user, void *data) {
RCore *core = (RCore *) user;
RConfigNode *node = (RConfigNode *) data;
@ -3365,7 +3378,7 @@ R_API int r_core_config_init(RCore *core) {
SETCB ("bin.verbose", "false", &cb_binverbose, "Show RBin warnings when loading binaries");
/* prj */
SETPREF ("prj.name", "", "Name of current project");
SETCB ("prj.name", "", &cb_prjname, "Name of current project");
SETBPREF ("prj.files", "false", "Save the target binary inside the project directory");
{
char *p = r_file_path ("git");
@ -3860,8 +3873,6 @@ R_API int r_core_config_init(RCore *core) {
/* file */
SETBPREF ("file.info", "true", "RBin info loaded");
SETPREF ("file.offset", "", "Offset where the file will be mapped at");
SETCB ("file.path", "", &cb_filepath, "Path of current file");
SETPREF ("file.lastpath", "", "Path of current file");
SETPREF ("file.type", "", "Type of current file");
SETI ("file.loadalign", 1024, "Alignment of load addresses");
SETI ("file.openmany", 1, "Maximum number of files opened at once");

View File

@ -925,9 +925,6 @@ R_API RCoreFile *r_core_file_open(RCore *r, const char *file, int flags, ut64 lo
if (cp && *cp) {
r_core_cmd (r, cp, 0);
}
char *absfile = r_file_abspath (file);
r_config_set (r->config, "file.path", absfile);
free (absfile);
}
// check load addr to make sure its still valid
r_bin_bind (r->bin, &(fh->binb));

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2017 - pancake */
/* radare - LGPL - Copyright 2009-2020 - pancake */
#include "r_config.h"
#include "r_core.h"

View File

@ -2763,6 +2763,7 @@ R_API bool r_core_init(RCore *core) {
core->cmdremote = 0;
core->incomment = false;
core->config = NULL;
core->prj = r_project_new ();
core->http_up = false;
core->use_tree_sitter_r2cmd = false;
ZERO_FILL (core->root_cmd_descriptor);
@ -3008,6 +3009,7 @@ R_API void r_core_fini(RCore *c) {
free (c->cmdqueue);
free (c->lastcmd);
free (c->stkcmd);
r_project_free (c->prj);
r_list_free (c->visual.tabs);
free (c->block);
r_core_autocomplete_free (c->autocomplete);

58
libr/core/cproject.c Normal file
View File

@ -0,0 +1,58 @@
/* radare - LGPL - Copyright 2020 - pancake */
// project class definition to be used by project.c
#include <r_core.h>
R_API RProject *r_project_new(void) {
RProject *p = R_NEW0 (RProject);
return p;
}
R_API bool r_project_rename(RProject *p, const char *newname) {
// check if the newname is available
// move directory
// update path and file fields in p
return false;
}
R_API bool r_project_is_git(RProject *p) {
char *f = r_str_newf ("%s"R_SYS_DIR".git", p->path);
bool ig = r_file_is_directory (f);
free (f);
return ig;
}
R_API void r_project_close(RProject *p) {
// close the current project
R_FREE (p->name);
R_FREE (p->path);
}
R_API bool r_project_open(RProject *p, const char *name, const char *path) {
r_return_val_if_fail (p && !R_STR_ISEMPTY (name), false);
if (r_project_is_loaded (p)) {
return false;
}
p->name = strdup (name);
if (path) {
p->path = strdup (path);
}
return true;
}
R_API void r_project_save(RProject *p) {
// must call r_core_project_save()
}
R_API void r_project_free(RProject *p) {
if (p) {
free (p->name);
free (p->path);
free (p);
}
}
R_API bool r_project_is_loaded(RProject *p) {
return !R_STR_ISEMPTY (p->name);
}

View File

@ -2,6 +2,7 @@ r_core_sources = [
'anal_tp.c',
'anal_objc.c',
'casm.c',
'cproject.c',
'blaze.c',
'citem.c',
'canal.c',

View File

@ -7,6 +7,8 @@
#define USE_R2 1
#include <spp/spp.h>
// project apis to be used from cmd_project.c
static bool is_valid_project_name(const char *name) {
int i;
if (r_str_endswith (name, ".zip")) {
@ -33,6 +35,7 @@ static bool is_valid_project_name(const char *name) {
}
static char *get_project_script_path(RCore *core, const char *file) {
r_return_val_if_fail (core && !R_STR_ISEMPTY (file), NULL);
const char *magic = "# r2 rdb project file";
char *data, *prjfile;
if (r_file_is_abspath (file)) {
@ -45,7 +48,7 @@ static char *get_project_script_path(RCore *core, const char *file) {
prjfile = r_str_append (prjfile, R_SYS_DIR);
prjfile = r_str_append (prjfile, file);
if (!r_file_exists (prjfile) || r_file_is_directory (prjfile)) {
prjfile = r_str_append (prjfile, R_SYS_DIR "rc");
prjfile = r_str_append (prjfile, R_SYS_DIR "rc.r2");
}
}
data = r_file_slurp (prjfile, NULL);
@ -75,7 +78,7 @@ R_API bool r_core_is_project(RCore *core, const char *name) {
if (!path) {
return false;
}
if (r_str_endswith (path, R_SYS_DIR "rc") && r_file_exists (path)) {
if (r_str_endswith (path, R_SYS_DIR "rc.r2") && r_file_exists (path)) {
ret = true;
} else {
path = r_str_append (path, ".d");
@ -149,7 +152,7 @@ static inline void remove_project_file(char * path) {
}
static inline void remove_notes_file(const char *pd) {
char *notes_txt = r_str_newf ("%s%s%s", pd, R_SYS_DIR, "notes.txt");
char *notes_txt = r_file_new (pd, "notes.txt", NULL);
if (r_file_exists (notes_txt)) {
r_file_rm (notes_txt);
eprintf ("rm %s\n", notes_txt);
@ -158,7 +161,7 @@ static inline void remove_notes_file(const char *pd) {
}
static inline void remove_rop_directory(const char *prj_dir) {
char *rop_d = r_str_newf ("%s%s%s", prj_dir, R_SYS_DIR, "rop.d");
char *rop_d = r_file_new (prj_dir, "rop.d", NULL);
if (r_file_is_directory (rop_d)) {
char *f;
@ -174,7 +177,6 @@ static inline void remove_rop_directory(const char *prj_dir) {
free (filepath);
}
r_file_rm (rop_d);
eprintf ("rm %s\n", rop_d);
r_list_free (files);
@ -211,15 +213,12 @@ R_API int r_core_project_delete(RCore *core, const char *prjfile) {
}
static bool load_project_rop(RCore *core, const char *prjfile) {
r_return_val_if_fail (core && !R_STR_ISEMPTY (prjfile), false);
char *path, *db = NULL, *path_ns;
bool found = 0;
SdbListIter *it;
SdbNs *ns;
if (!prjfile || !*prjfile) {
return false;
}
Sdb *rop_db = sdb_ns (core->sdb, "rop", false);
Sdb *nop_db = sdb_ns (rop_db, "nop", false);
Sdb *mov_db = sdb_ns (rop_db, "mov", false);
@ -230,12 +229,12 @@ static bool load_project_rop(RCore *core, const char *prjfile) {
char *rc_path = get_project_script_path (core, prjfile);
char *prj_dir = r_file_dirname (rc_path);
if (r_str_endswith (prjfile, R_SYS_DIR "rc")) {
if (r_str_endswith (prjfile, R_SYS_DIR "rc.r2")) {
// XXX
eprintf ("ENDS WITH\n");
path = strdup (prjfile);
path[strlen (path) - 3] = 0;
} else if (r_file_fexists ("%s%s%src", R_SYS_DIR, prj_dir, prjfile)) {
} else if (r_file_fexists ("%s%s%src.r2", R_SYS_DIR, prj_dir, prjfile)) {
path = r_str_newf ("%s%s%s", R_SYS_DIR, prj_dir, prjfile);
} else {
if (*prjfile == R_SYS_DIR[0]) {
@ -371,8 +370,6 @@ R_API RThread *r_core_project_load_bg(RCore *core, const char *prj_name, const c
return th;
}
/*** ^^^ thready ***/
static ut64 get_project_laddr(RCore *core, const char *prjfile) {
ut64 laddr = 0;
char *buf = r_file_slurp (prjfile, NULL);
@ -507,19 +504,8 @@ R_API char *r_core_project_info(RCore *core, const char *prjfile) {
if (feof (fd)) {
break;
}
if (!strncmp (buf, "\"e file.path = ", 15)) {
if (!strncmp (buf, "\"e prj.name = ", 14)) {
buf[strlen (buf) - 2] = 0;
file = r_str_new (buf + 15);
break;
}
if (!strncmp (buf, "\"e file.lastpath = ", 19)) {
buf[strlen (buf) - 2] = 0;
file = r_str_new (buf + 19);
break;
}
// TODO: deprecate before 1.0
if (!strncmp (buf, "e file.path = ", 14)) {
buf[strlen (buf) - 1] = 0;
file = r_str_new (buf + 14);
break;
}
@ -619,7 +605,6 @@ static bool simple_project_save_script(RCore *core, const char *file, int opts)
r_cons_flush ();
}
r_cons_singleton ()->fdout = fdold;
r_cons_singleton ()->context->is_interactive = true;
@ -671,10 +656,7 @@ static bool project_save_script(RCore *core, const char *file, int opts) {
}
}
}
// Set file.path and file.lastpath to empty string to signal
// new behaviour to project load routine (see io maps below).
r_config_set (core->config, "file.path", "");
r_config_set (core->config, "file.lastpath", "");
if (opts & R_CORE_PRJ_EVAL) {
r_str_write (fd, "# eval\n");
r_config_list (core->config, NULL, true);

View File

@ -238,9 +238,24 @@ typedef struct r_core_tasks_t {
bool oneshot_running;
} RCoreTaskScheduler;
typedef struct r_core_project_t {
char *name;
char *path;
} RProject;
R_API RProject *r_project_new(void);
R_API bool r_project_rename(RProject *p, const char *newname);
R_API bool r_project_is_git(RProject *p);
R_API void r_project_close(RProject *p);
R_API bool r_project_open(RProject *p, const char *prjname, const char *path);
R_API void r_project_save(RProject *p);
R_API void r_project_free(RProject *p);
R_API bool r_project_is_loaded(RProject *p);
struct r_core_t {
RBin *bin;
RConfig *config;
RProject *prj;
ut64 offset; // current seek
ut64 prompt_offset; // temporarily set to offset to have $$ in expressions always stay the same during temp seeks
ut32 blocksize;
@ -530,8 +545,10 @@ R_API void r_core_fortune_list(RCore *core);
R_API void r_core_fortune_print_random(RCore *core);
/* project */
#if 0
R_API bool r_core_project_load(RCore *core, const char *prjfile, const char *rcfile);
R_API RThread *r_core_project_load_bg(RCore *core, const char *prjfile, const char *rcfile);
#endif
R_API void r_core_project_execute_cmds(RCore *core, const char *prjfile);
#define R_CORE_FOREIGN_ADDR -1
@ -681,13 +698,13 @@ R_API int r_core_zdiff(RCore *c, RCore *c2);
R_API int r_core_gdiff(RCore *core1, RCore *core2);
R_API int r_core_gdiff_fcn(RCore *c, ut64 addr, ut64 addr2);
R_API bool r_core_project_open(RCore *core, const char *file, bool thready);
R_API bool r_core_project_open(RCore *core, const char *file);
R_API int r_core_project_cat(RCore *core, const char *name);
R_API int r_core_project_delete(RCore *core, const char *prjfile);
R_API int r_core_project_list(RCore *core, int mode);
R_API bool r_core_project_save_script(RCore *core, const char *file, int opts);
R_API bool r_core_project_save(RCore *core, const char *file);
R_API char *r_core_project_info(RCore *core, const char *file);
R_API char *r_core_project_name(RCore *core, const char *file);
R_API char *r_core_project_notes_file (RCore *core, const char *file);
R_API char *r_core_sysenv_begin(RCore *core, const char *cmd);

View File

@ -1148,7 +1148,7 @@ R_API int r_main_radare2(int argc, const char **argv) {
} else {
const char *prj = r_config_get (r->config, "prj.name");
if (prj && *prj) {
pfile = r_core_project_info (r, prj);
pfile = r_core_project_name (r, prj);
if (pfile) {
if (!fh) {
fh = r_core_file_open (r, pfile, perms, mapaddr);
@ -1303,12 +1303,12 @@ R_API int r_main_radare2(int argc, const char **argv) {
}
r_core_seek (r, r->offset, true); // read current block
#if 0
/* check if file.path has changed */
if (iod && !strstr (iod->uri, "://")) {
const char *npath;
char *path = strdup (r_config_get (r->config, "file.path"));
has_project = r_core_project_open (r, r_config_get (r->config, "prj.name"), false);
has_project = r_core_project_open (r, r_config_get (r->config, "prj.name"));
iod = r->io ? r_io_desc_get (r->io, fh->fd) : NULL;
if (has_project) {
r_config_set (r->config, "bin.strings", "false");
@ -1319,6 +1319,7 @@ R_API int r_main_radare2(int argc, const char **argv) {
}
free (path);
}
#endif
r_list_foreach (evals, iter, cmdn) {
r_config_eval (r->config, cmdn, false);