Add r_file_is_executable and r_file_extension apis ##api

* Boolify some rdebug apis
* Make json.parse arg const
This commit is contained in:
pancake 2022-09-19 02:03:09 +02:00
parent 7020c9425c
commit 4801bbc13d
13 changed files with 71 additions and 77 deletions

View File

@ -1221,48 +1221,6 @@ static char *langFromHashbang(RCore *core, const char *file) {
return NULL;
}
#if __UNIX__
// R2_580 - move into r_file_is_executable()
static bool is_executable_header(const char *file) {
bool ret = false;
int osz = 0;
char *data = r_file_slurp_range (file, 0, 1024, &osz);
if (data && osz > 4) {
// 0xfeedface 0xcefaedfe) // 32bit
// 0xfeedfacf 0xcffaedfe) // 64bit
if (!memcmp (data, "\xca\xfe\xba\xbe", 4)) {
ret = true;
} else if (!memcmp (data, "#!/", 3)) {
ret = true;
} else if (!memcmp (data, "\x7f" "ELF", 4)) {
ret = true;
}
}
free (data);
return ret;
}
#endif
// R2_580 - move into r_file_is_executable()
static bool is_executable(const char *file) {
bool ret = false;
#if __UNIX__
struct stat buf = {0};
if (stat (file, &buf) != 0) {
return false;
}
if (buf.st_mode & 0111) {
return is_executable_header (file);
}
#elif __WINDOWS__
const char *ext = r_str_lchr (file, '.');
if (ext) {
ext++;
return !strcmp (ext, "exe") || !strcmp (ext, "com") || !strcmp (ext, "bat");
}
#endif
return ret;
}
R_API bool r_core_run_script(RCore *core, const char *file) {
bool ret = false;
RListIter *iter;
@ -1319,9 +1277,8 @@ R_API bool r_core_run_script(RCore *core, const char *file) {
#else
#define cmdstr(x) r_str_newf (x" '%s'", file);
#endif
const char *p = r_str_lchr (file, '.');
if (p) {
const char *ext = p + 1;
const char *ext = r_file_extension (file);
if (ext) {
/* TODO: handle this inside r_lang_pipe with new APIs */
if (!strcmp (ext, "js")) {
char *cmd = cmdstr ("node");
@ -1446,7 +1403,7 @@ R_API bool r_core_run_script(RCore *core, const char *file) {
R_LOG_ERROR ("Cannot find python in PATH");
}
} else {
if (is_executable (file)) {
if (r_file_is_executable (file)) {
r_core_cmdf (core, "#!pipe %s%s", (*file=='/')?"":"./", file);
ret = 1;
} else {
@ -1464,7 +1421,7 @@ R_API bool r_core_run_script(RCore *core, const char *file) {
free (cmd);
ret = 1;
} else {
if (is_executable (file)) {
if (r_file_is_executable (file)) {
r_core_cmdf (core, "#!pipe %s%s", (*file=='/')?"":"./", file);
ret = 1;
}

View File

@ -1,10 +1,7 @@
/* radare2 - LGPL - Copyright 2009-2022 - pancake */
#include <r_core.h>
#include <r_socket.h>
#include <config.h>
#include <r_config.h>
#include <r_util.h>
#if __UNIX__
#include <signal.h>
#endif
@ -2164,6 +2161,7 @@ static int autocomplete(RLineCompletion *completion, RLineBuffer *buf, RLineProm
}
R_API int r_core_fgets(char *buf, int len) {
r_return_val_if_fail (buf, -1);
RCons *cons = r_cons_singleton ();
RLine *rli = cons->line;
bool prompt = cons->context->is_interactive;

View File

@ -114,9 +114,8 @@ R_API void r_core_loadlibs_init(RCore *core) {
}
static bool __isScriptFilename(const char *name) {
const char *ext = r_str_lchr (name, '.');
const char *ext = r_file_extension (name);
if (ext) {
ext++;
if (0
|| !strcmp (ext, "c")
|| !strcmp (ext, "go")

View File

@ -2,7 +2,6 @@
#include <r_core.h>
#include <rvc.h>
#define USE_R2 1
#include <spp/spp.h>
#define PROJECT_EXPERIMENTAL 0

View File

@ -58,8 +58,7 @@ static int r_debug_drx_at(RDebug *dbg, ut64 addr) {
* r_debug_bp_hit handles stage 1.
* r_debug_recoil handles stage 2.
*/
// R2580 - return bool
static int r_debug_bp_hit(RDebug *dbg, RRegItem *pc_ri, ut64 pc, RBreakpointItem **pb) {
static bool r_debug_bp_hit(RDebug *dbg, RRegItem *pc_ri, ut64 pc, RBreakpointItem **pb) {
r_return_val_if_fail (dbg && pc_ri && pb, false);
RBreakpointItem *b = NULL;
/* initialize the output parameter */
@ -605,20 +604,20 @@ R_API bool r_debug_execute(RDebug *dbg, const ut8 *buf, int len, R_OUT ut64 *ret
return true;
}
R_API int r_debug_startv(struct r_debug_t *dbg, int argc, char **argv) {
R_API bool r_debug_startv(struct r_debug_t *dbg, int argc, char **argv) {
/* TODO : r_debug_startv unimplemented */
return false;
}
R_API int r_debug_start(RDebug *dbg, const char *cmd) {
R_API bool r_debug_start(RDebug *dbg, const char *cmd) {
/* TODO: this argc/argv parser is done in r_io */
// TODO: parse cmd and generate argc and argv
return false;
}
// 580 should be bool
R_API int r_debug_detach(RDebug *dbg, int pid) {
int ret = 0;
R_API bool r_debug_detach(RDebug *dbg, int pid) {
r_return_val_if_fail (dbg, false);
bool ret = false;
if (dbg->h && dbg->h->detach) {
ret = dbg->h->detach (dbg, pid);
if (dbg->pid == pid) {

View File

@ -438,9 +438,9 @@ R_API RDebug *r_debug_new(int hard);
R_API void r_debug_free(RDebug *dbg);
R_API bool r_debug_attach(RDebug *dbg, int pid);
R_API int r_debug_detach(RDebug *dbg, int pid);
R_API int r_debug_startv(RDebug *dbg, int argc, char **argv);
R_API int r_debug_start(RDebug *dbg, const char *cmd);
R_API bool r_debug_detach(RDebug *dbg, int pid);
R_API bool r_debug_startv(RDebug *dbg, int argc, char **argv);
R_API bool r_debug_start(RDebug *dbg, const char *cmd);
/* reason we stopped */
R_API RDebugReasonType r_debug_stop_reason(RDebug *dbg);

View File

@ -232,18 +232,9 @@ typedef struct r_run_profile_t {
bool _time;
int _pid;
char *_pidfile;
#if R2_580
// TODO more bools
bool _r2preload;
bool _docore;
bool _dofork;
/// OKK dodebug is no longer used
#else
int _r2preload;
int _docore;
int _dofork;
int _dodebug;
#endif
int _aslr;
int _maxstack;
int _maxproc;

View File

@ -20,6 +20,7 @@ R_API bool r_file_is_abspath(const char *file);
R_API bool r_file_is_c(const char *file);
R_API bool r_file_is_directory(const char *str);
R_API bool r_file_is_regular(const char *str);
R_API bool r_file_is_executable(const char *file);
R_API bool r_file_truncate(const char *filename, ut64 newsize);
R_API char *r_file_new(const char *root, ...);
@ -52,6 +53,7 @@ R_API bool r_file_touch(const char *file);
R_API bool r_file_hexdump(const char *file, const ut8 *buf, int len, int append);
R_API bool r_file_rm(const char *file);
R_API bool r_file_exists(const char *str);
R_API const char *r_file_extension(const char *str);
R_API bool r_file_fexists(const char *fmt, ...) R_PRINTF_CHECK(1, 2);
R_API char *r_file_slurp_line(const char *file, int line, int context);
R_API char *r_file_slurp_lines(const char *file, int line, int count);

View File

@ -53,7 +53,7 @@ typedef struct r_json_t {
struct r_json_t *next; // points to next child
} RJson;
R_API RJson *r_json_parse(char *text);
R_API RJson *r_json_parse(const char *text);
R_API void r_json_free(RJson *js);
R_API const RJson *r_json_get(const RJson *json, const char *key); // get object's property by key
R_API const RJson *r_json_item(const RJson *json, size_t idx); // get array element by index

View File

@ -250,7 +250,6 @@ static RRunProfile* _get_run_profile(RIO *io, int bits, char **argv) {
}
rp->_program = strdup (argv[0]);
rp->_dodebug = true;
if (io->runprofile && *io->runprofile) {
if (!r_run_parsefile (rp, io->runprofile)) {
R_LOG_ERROR ("Can't find profile '%s'", io->runprofile);

View File

@ -1458,3 +1458,50 @@ R_API RList* r_file_glob(const char *_globbed_path, int maxdepth) {
free (globbed_path);
return files;
}
#if __UNIX__
static bool is_executable_header(const char *file) {
bool ret = false;
int osz = 0;
char *data = r_file_slurp_range (file, 0, 1024, &osz);
if (data && osz > 4) {
// 0xfeedface 0xcefaedfe) // 32bit
// 0xfeedfacf 0xcffaedfe) // 64bit
if (!memcmp (data, "\xca\xfe\xba\xbe", 4)) {
ret = true;
} else if (!memcmp (data, "#!/", 3)) {
ret = true;
} else if (!memcmp (data, "\x7f" "ELF", 4)) {
ret = true;
}
}
free (data);
return ret;
}
#endif
R_API bool r_file_is_executable(const char *file) {
bool ret = false;
#if __UNIX__
struct stat buf = {0};
if (stat (file, &buf) != 0) {
return false;
}
if (buf.st_mode & 0111) {
return is_executable_header (file);
}
#elif __WINDOWS__
const char *ext = r_file_extension (file);
if (ext) {
return !strcmp (ext, "exe") || !strcmp (ext, "com") || !strcmp (ext, "bat");
}
#endif
return ret;
}
R_API const char *r_file_extension(const char *str) {
const char *dot = r_str_lchr (str, '.');
if (dot) {
return dot + 1;
}
return NULL;
}

View File

@ -366,10 +366,12 @@ static char *parse_value(RJson *parent, const char *key, char *p) {
return NULL;
}
// XXX R2_580 make this api const char *text instead of char *text
R_API RJson *r_json_parse(char *text) {
R_API RJson *r_json_parse(const char *_text) {
RJson js = {0};
if (!parse_value (&js, 0, text)) {
char *text = strdup (_text);
bool res = parse_value (&js, 0, text);
free (text);
if (!res) {
if (js.children.first) {
r_json_free (js.children.first);
}

View File

@ -1,4 +1,5 @@
#!/bin/sh
cd "$(dirname $0)"/..
# alias locdiff='echo $((`git diff |grep ^+ | wc -l`-`git diff |grep ^- | wc -l`))'
#A=`hg diff $@ | grep -v '+++' | grep ^+ |wc -l`
#B=`hg diff $@ | grep -v -- '---' | grep ^- |wc -l`