R_API R2Pipe *r2p_open(const char *cmd) { R2Pipe *r2p = R_NEW0 (R2Pipe); r2p->magic = R2P_MAGIC; if (cmd == NULL) { r2p->child = -1; #if __UNIX__ { char *out = r_sys_getenv ("R2PIPE_IN"); char *in = r_sys_getenv ("R2PIPE_OUT"); int done = R_FALSE; if (in && out) { int i_in = atoi (in); int i_out = atoi (out); if (i_in>=0 && i_out>=0) { r2p->input[0] = r2p->input[1] = i_in; r2p->output[0] = r2p->output[1] = i_out; done = R_TRUE; } } if (!done) { eprintf ("Cannot find R2PIPE_IN or R2PIPE_OUT environment\n"); R_FREE (r2p); } free (in); free (out); } return r2p; #else eprintf ("r2p_open(NULL) not supported on windows\n"); return NULL; #endif } #if __WINDOWS__ w32_createPipe (r2p, cmd); r2p->child = (int)(r2p->pipe); #else pipe (r2p->input); pipe (r2p->output); r2p->child = fork (); if (r2p->child == -1) { r2p_close (r2p); return NULL; } env ("R2PIPE_IN", r2p->input[0]); env ("R2PIPE_OUT", r2p->output[1]); if (r2p->child) { eprintf ("Child is %d\n", r2p->child); } else { int rc; if (cmd && *cmd) { rc = r_sandbox_system (cmd, 1); } else rc = 0; r2p_close (r2p); exit (0); return NULL; } #endif return r2p; }
static R2Pipe* r2pipe_open_spawn(R2Pipe* r2pipe) { #if __UNIX__ || defined(__CYGWIN__) char *out = r_sys_getenv ("R2PIPE_IN"); char *in = r_sys_getenv ("R2PIPE_OUT"); int done = false; if (in && out) { int i_in = atoi (in); int i_out = atoi (out); if (i_in >= 0 && i_out >= 0) { r2pipe->input[0] = r2pipe->input[1] = i_in; r2pipe->output[0] = r2pipe->output[1] = i_out; done = true; } } if (!done) { eprintf ("Cannot find R2PIPE_IN or R2PIPE_OUT environment\n"); R_FREE (r2pipe); } free (in); free (out); return r2pipe; #else eprintf ("r2pipe_open(NULL) not supported on windows\n"); return NULL; #endif }
R_API void r_egg_lang_include_path (REgg *egg, const char *path) { char *env = r_sys_getenv (R_EGG_INCDIR_ENV); if (!env || !*env) { r_egg_lang_include_init (egg); env = r_sys_getenv (R_EGG_INCDIR_ENV); } env = r_str_concatf (NULL, "%s:%s", path, env); r_sys_setenv (R_EGG_INCDIR_ENV, env); free (env); }
R_API RIO *r_io_new() { RIO *io = R_NEW0 (RIO); if (!io) { return NULL; } io->buffer = r_cache_new (); // RCache is a list of ranged buffers. maybe rename? io->write_mask_fd = -1; io->cb_printf = (void *)printf; io->bits = (sizeof (void *) == 8)? 64: 32; io->ff = true; io->Oxff = 0xff; io->aslr = 0; io->pava = false; io->raised = -1; io->autofd = true; r_io_map_init (io); r_io_desc_init (io); r_io_undo_init (io); r_io_cache_init (io); r_io_plugin_init (io); r_io_section_init (io); { char *env = r_sys_getenv ("R_IO_MAX_ALLOC"); if (env) { io->maxalloc = r_num_get (NULL, env); free (env); } } return io; }
static void parseline (char *b) { char *e = strchr (b, '='); if (!e) return; if (*b=='#') return; *e++ = 0; if (*e=='$') e = r_sys_getenv (e); if (e == NULL) return; if (!strcmp (b, "program")) _args[0] = _program = strdup (e); else if (!strcmp (b, "connect")) _connect = strdup (e); else if (!strcmp (b, "listen")) _listen = strdup (e); else if (!strcmp (b, "stdout")) _stdout = strdup (e); else if (!strcmp (b, "stdin")) _stdin = strdup (e); else if (!strcmp (b, "input")) _input = strdup (e); else if (!strcmp (b, "chdir")) _chgdir = strdup (e); else if (!strcmp (b, "chroot")) _chroot = strdup (e); else if (!strcmp (b, "preload")) _preload = strdup (e); else if (!strcmp (b, "setuid")) _setuid = strdup (e); else if (!strcmp (b, "seteuid")) _seteuid = strdup (e); else if (!strcmp (b, "setgid")) _setgid = strdup (e); else if (!strcmp (b, "setegid")) _setegid = strdup (e); else if (!memcmp (b, "arg", 3)) { int n = atoi (b+3); if (n>=0 && n<NARGS) { _args[n] = strdup (e); r_str_escape (_args[n]); } else eprintf ("Out of bounds args index: %d\n", n); } else if (!strcmp (b, "timeout")) _timeout = atoi (e); else if (!strcmp (b, "setenv")) { char *v = strchr (e, '='); if (v) { *v++ = 0; r_sys_setenv (e, v); } } }
R_API int r_cons_get_size(int *rows) { #if __UNIX__ struct winsize win; if (ioctl (1, TIOCGWINSZ, &win) == 0) { I.columns = win.ws_col; I.rows = win.ws_row-1; } else { I.columns = 80; I.rows = 23; } #else char *str = r_sys_getenv ("COLUMNS"); if (str != NULL) { I.columns = atoi (str); I.rows = 23; // XXX. windows must get console size free (str); } else { I.columns = 80; I.rows = 23; } #endif if (rows) *rows = I.rows; return I.columns; }
static char *find_include(const char *prefix, const char *file) { char *pfx = NULL, *ret = NULL, *env = r_sys_getenv (R_EGG_INCDIR_ENV); //eprintf ("find_include (%s,%s)\n", prefix, file); if (!prefix) prefix = ""; if (*prefix=='$') { char *out = r_sys_getenv (prefix+1); pfx = out? out: strdup (""); } else { pfx = strdup (prefix); if (!pfx) { free (env); return NULL; } } if (env) { char *str, *ptr = strchr (env, ':'); // eprintf ("MUST FIND IN PATH (%s)\n", env); str = env; while (str) { if (ptr) *ptr = 0; free (ret); ret = r_str_concatf (NULL, "%s/%s", pfx, file); { char *filepath = r_str_concatf (NULL, "%s/%s/%s", str, pfx, file); // eprintf ("try (%s)\n", filepath); if (r_file_exists (filepath)) { free (env); free (pfx); free (ret); return filepath; } free (filepath); } if (!ptr) break; str = ptr+1; ptr = strchr (str, ':'); } free (env); } else ret = r_str_concatf (NULL, "%s/%s", pfx, file); free (pfx); return ret; }
/* * It prints a message to the log and it provides a single point of entrance in * case of debugging. All r_return_* functions call this. */ R_API void r_assert_log(RLogLevel level, const char *fmt, ...) { va_list args; va_start (args, fmt); print_message (level, fmt, args); va_end (args); char *env = r_sys_getenv ("R_DEBUG_ASSERT"); if (env) { if (*env && atoi (env)) { r_sys_breakpoint (); } free (env); } }
R_API RLine *r_line_new() { I.hist_up = NULL; I.hist_down = NULL; I.prompt = strdup ("> "); I.contents = NULL; #if __WINDOWS__ I.ansicon = r_sys_getenv ("ANSICON"); #endif if (!r_line_dietline_init ()) { eprintf ("error: r_line_dietline_init\n"); } r_line_completion_init (&I.completion, 4096); return &I; }
static int lang_c_file(RLang *lang, const char *file) { void *lib; char *cc, *p, name[512], buf[512]; const char *libpath, *libname; if (strlen (file) > (sizeof(name)-10)) return R_FALSE; if (!strstr (file, ".c")) sprintf (name, "%s.c", file); else strcpy (name, file); if (!r_file_exists (name)) { eprintf ("file not found (%s)\n", name); return R_FALSE; } { char *a = (char*)r_str_lchr (name, '/'); if (a) { *a = 0; libpath = name; libname = a+1; } else { libpath = "."; libname = name; } } p = strstr (name, ".c"); if (p) *p=0; cc = r_sys_getenv ("CC"); if (!cc || !*cc) cc = strdup ("gcc"); snprintf (buf, sizeof (buf), "%s -fPIC -shared %s -o %s/lib%s."R_LIB_EXT " $(pkg-config --cflags --libs r_core)", cc, file, libpath, libname); free (cc); if (r_sandbox_system (buf, 1) != 0) return R_FALSE; snprintf (buf, sizeof (buf), "%s/lib%s."R_LIB_EXT, libpath, libname); lib = r_lib_dl_open (buf); if (lib!= NULL) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) fcn (lang->user); else eprintf ("Cannot find 'entry' symbol in library\n"); r_lib_dl_close (lib); } else eprintf ("Cannot open library\n"); r_file_rm (buf); // remove lib return 0; }
static void radare2_rc(RCore *r) { char* env_debug = r_sys_getenv ("R_DEBUG"); bool has_debug = false; if (env_debug) { has_debug = true; R_FREE (env_debug); } char *homerc = r_str_home (".radare2rc"); if (homerc && r_file_is_regular (homerc)) { if (has_debug) { eprintf ("USER CONFIG loaded from %s\n", homerc); } r_core_cmd_file (r, homerc); } free (homerc); homerc = r_str_home (".config/radare2/radare2rc"); if (homerc && r_file_is_regular (homerc)) { if (has_debug) { eprintf ("USER CONFIG loaded from %s\n", homerc); } r_core_cmd_file (r, homerc); } free (homerc); homerc = r_str_home (".config/radare2/radare2rc.d"); if (homerc) { if (r_file_is_directory (homerc)) { char *file; RListIter *iter; RList *files = r_sys_dir (homerc); r_list_foreach (files, iter, file) { if (*file != '.') { char *path = r_str_newf ("%s/%s", homerc, file); if (r_file_is_regular (path)) { if (has_debug) { eprintf ("USER CONFIG loaded from %s\n", homerc); } r_core_cmd_file (r, path); } free (path); } } r_list_free (files); } free (homerc); } }
R_API char *r_str_home(const char *str) { int lhome, lstr; char *dst; char *home = r_sys_getenv (R_SYS_HOME); if (home == NULL) return NULL; lhome = strlen (home); lstr = strlen (str); dst = (char *)malloc (lhome + lstr + 2); memcpy (dst, home, lhome+1); if (str && *str) { memcpy (dst+lhome, R_SYS_DIR, strlen (R_SYS_DIR)); memcpy (dst+lhome+strlen (R_SYS_DIR), str, lstr+1); } free (home); return dst; }
static void _libwrap_init() { char *web; signal (SIGUSR1, sigusr1); signal (SIGUSR2, sigusr2); printf ("libr2 initialized. send SIGUSR1 to %d in order to reach the r2 prompt\n", getpid ()); printf ("kill -USR1 %d\n", getpid ()); fflush (stdout); web = r_sys_getenv ("RARUN2_WEB"); core = r_core_new (); r_core_loadlibs (core, R_CORE_LOADLIBS_ALL, NULL); if (web) { r_core_cmd0 (core, "=H&"); r_sys_setenv ("RARUN2_WEB", NULL); free (web); } // TODO: maybe reopen every time a signal is spawned to reload memory regions information // TODO: open io_self }
static int r_vala_file(RLang *lang, const char *file) { void *lib; char *p, name[512], buf[512]; char *vapidir; if (!strstr (file, ".vala")) sprintf (name, "%s.vala", file); else strcpy (name, file); if (!r_file_exists (name)) { eprintf ("file not found (%s)\n", name); return R_FALSE; } vapidir = r_sys_getenv ("VAPIDIR"); if (vapidir) { if (*vapidir) { snprintf (buf, sizeof (buf), "valac --vapidir=%s --pkg r_core -C %s", vapidir, name); } free (vapidir); } else sprintf (buf, "valac --pkg r_core -C %s", name); if (system (buf) != 0) return R_FALSE; p = strstr (name, ".vala"); if (p) *p=0; p = strstr (name, ".gs"); if (p) *p=0; snprintf (buf, sizeof (buf), "gcc -fPIC -shared %s.c -o lib%s."R_LIB_EXT " $(pkg-config --cflags --libs r_core gobject-2.0)", name, name); if (system (buf) != 0) return R_FALSE; snprintf (buf, sizeof (buf), "./lib%s."R_LIB_EXT, name); lib = r_lib_dl_open (buf); if (lib!= NULL) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) fcn (lang->user); else eprintf ("Cannot find 'entry' symbol in library\n"); r_lib_dl_close (lib); } else eprintf ("Cannot open library\n"); r_file_rm (buf); // remove lib sprintf (buf, "%s.c", name); // remove .c r_file_rm (buf); return 0; }
R_API int r_cons_get_size(int *rows) { #if EMSCRIPTEN I.columns = 80; I.rows = 23; #elif __UNIX__ struct winsize win; if (isatty (1) && ioctl (1, TIOCGWINSZ, &win) == 0) { if (win.ws_col==0) { int fd = open ("/dev/tty", O_RDONLY); if (fd != -1) { if (ioctl (fd, TIOCGWINSZ, &win) != 0) { I.columns = 80; I.rows = 23; } close (fd); } } I.columns = win.ws_col; I.rows = win.ws_row-1; if (I.heightfix) I.rows--; } else { I.columns = 80; I.rows = 23; } #else char *str = r_sys_getenv ("COLUMNS"); if (str != NULL) { I.columns = atoi (str); I.rows = 23; // XXX. windows must get console size free (str); } else { I.columns = 80; I.rows = 23; } #endif if (rows) *rows = I.rows; if (I.widthfix) I.columns--; if (I.force_columns) I.columns = I.force_columns; if (I.force_rows) I.rows = I.force_rows; return I.columns; }
static int lang_cpipe_file(RLang *lang, const char *file) { char *a, *cc, *p, name[512]; const char *libpath, *libname; if (strlen (file) > (sizeof (name)-10)) return false; if (!strstr (file, ".c")) sprintf (name, "%s.c", file); else strcpy (name, file); if (!r_file_exists (name)) { eprintf ("file not found (%s)\n", name); return false; } a = (char*)r_str_lchr (name, '/'); if (a) { *a = 0; libpath = name; libname = a + 1; } else { libpath = "."; libname = name; } r_sys_setenv ("PKG_CONFIG_PATH", R2_LIBDIR"/pkgconfig"); p = strstr (name, ".c"); if (p) *p = 0; cc = r_sys_getenv ("CC"); if (!cc || !*cc) { free (cc); cc = strdup ("gcc"); } char *buf = r_str_newf ("%s %s -o %s/bin%s" " $(pkg-config --cflags --libs r_socket)", cc, file, libpath, libname); free (cc); if (r_sandbox_system (buf, 1) == 0) { char *binfile = r_str_newf ("%s/bin%s", libpath, libname); lang_pipe_run (lang, binfile, -1); r_file_rm (binfile); free (binfile); } free (buf); return 0; }
R_API int r_reg_set_profile(RReg *reg, const char *profile) { int ret; char *base, *file; char *str = r_file_slurp (profile, NULL); if (!str) { base = r_sys_getenv (R_LIB_ENV); if (base) { file = r_str_append (base, profile); str = r_file_slurp (file, NULL); free (file); } } if (!str) { eprintf ("r_reg_set_profile: Cannot find '%s'\n", profile); return false; } ret = r_reg_set_profile_string (reg, str); free (str); return ret; }
R_API char *r_str_home(const char *str) { char *dst, *home = r_sys_getenv (R_SYS_HOME); size_t length; if (home == NULL) return NULL; length = strlen (home) + 1; if (str) length += strlen (R_SYS_DIR) + strlen (str); dst = (char *)malloc (length); if (dst == NULL) goto fail; strcpy (dst, home); if (str) { strcat (dst, R_SYS_DIR); strcat (dst, str); } fail: free (home); return dst; }
static char* r_egg_Cfile_getCompiler(void) { size_t i; const char *compilers[] = {"llvm-gcc", "clang", "gcc"}; char *output = r_sys_getenv ("CC"); if (output) { return output; } for (i = 0; i < 3; i++) { output = r_file_path (compilers[i]); if (strcmp (output, compilers[i])) { free (output); return strdup (compilers[i]); } free (output); } eprintf ("Couldn't find a compiler ! Please, set CC.\n"); return NULL; }
R_API int r_reg_set_profile(RReg *reg, const char *profile) { int ret; char *base, *file; char *str = r_file_slurp (profile, NULL); if (!str) { // XXX we must define this varname in r_lib.h /compiletime/ base = r_sys_getenv ("LIBR_PLUGINS"); if (base) { file = r_str_concat (base, profile); str = r_file_slurp (file, NULL); free (file); } } if (!str) { eprintf ("r_reg_set_profile: Cannot find '%s'\n", profile); return false; } ret = r_reg_set_profile_string (reg, str); free (str); return ret; }
static int lang_vala_file(RLang *lang, const char *file, bool silent) { void *lib; char *p, name[512], buf[512]; char *vapidir, *srcdir, *libname; if (strlen (file)>500) return false; if (!strstr (file, ".vala")) sprintf (name, "%s.vala", file); else strcpy (name, file); if (!r_file_exists (name)) { eprintf ("file not found (%s)\n", name); return false; } srcdir = strdup (file); p = (char*)r_str_lchr (srcdir, '/'); if (p) { *p = 0; libname = strdup (p+1); if (*file!='/') { strcpy (srcdir, "."); } } else { libname = strdup (file); strcpy (srcdir, "."); } r_sys_setenv ("PKG_CONFIG_PATH", R2_LIBDIR"/pkgconfig"); vapidir = r_sys_getenv ("VAPIDIR"); char *tail = silent? " > /dev/null 2>&1": ""; if (vapidir) { if (*vapidir) { snprintf (buf, sizeof (buf)-1, "valac -d %s --vapidir=%s --pkg r_core -C %s %s", srcdir, vapidir, name, tail); } free (vapidir); } else { snprintf (buf, sizeof (buf) - 1, "valac -d %s --pkg r_core -C %s %s", srcdir, name, tail); } free (srcdir); if (r_sandbox_system (buf, 1) != 0) { free (libname); return false; } p = strstr (name, ".vala"); if (p) *p=0; p = strstr (name, ".gs"); if (p) *p=0; // TODO: use CC environ if possible snprintf (buf, sizeof (buf), "gcc -fPIC -shared %s.c -o lib%s."R_LIB_EXT " $(pkg-config --cflags --libs r_core gobject-2.0)", name, libname); if (r_sandbox_system (buf, 1) != 0) { free (libname); return false; } snprintf (buf, sizeof (buf), "./lib%s."R_LIB_EXT, libname); free (libname); lib = r_lib_dl_open (buf); if (lib != NULL) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) fcn (lang->user); else eprintf ("Cannot find 'entry' symbol in library\n"); r_lib_dl_close (lib); } else eprintf ("Cannot open library\n"); r_file_rm (buf); // remove lib sprintf (buf, "%s.c", name); // remove .c r_file_rm (buf); return 0; }
static int cmd_eval(void *data, const char *input) { char *p; RCore *core = (RCore *)data; switch (input[0]) { case 't': // env if (input[1]==' ' && input[2]) { RConfigNode *node = r_config_node_get (core->config, input+2); if (node) { const char *type = r_config_node_type (node); if (type && *type) { r_cons_printf ("%s\n", type); } } } else { eprintf ("Usage: et [varname] ; show type of eval var\n"); } break; case 'n': // env if (!strchr (input, '=')) { char *var, *p; var = strchr (input, ' '); if (var) while (*var==' ') var++; p = r_sys_getenv (var); if (p) { r_cons_printf ("%s\n", p); free (p); } else { char **e = r_sys_get_environ (); while (e && *e) { r_cons_printf ("%s\n", *e); e++; } } } else if (strlen (input)>3) { char *v, *k = strdup (input+3); if (!k) break; v = strchr (k, '='); if (v) { *v++ = 0; r_sys_setenv (k, v); } free (k); } return true; case 'x': // exit return cmd_quit (data, ""); case 'j': r_config_list (core->config, NULL, 'j'); break; case '\0': r_config_list (core->config, NULL, 0); break; case 'c': switch (input[1]) { case 'h': // echo if (( p = strchr (input, ' ') )) { r_cons_strcat (p+1); r_cons_newline (); } break; case 'd': r_cons_pal_init (NULL); break; case '?': { const char *helpmsg[] = { "Usage ec[s?] [key][[=| ]fg] [bg]","","", "ec","","list all color keys", "ec*","","same as above, but using r2 commands", "ecd","","set default palette", "ecr","","set random palette", "ecs","","show a colorful palette", "ecj","","show palette in JSON", "ecc","","show palette in CSS", "eco"," dark|white","load white color scheme template", "ecn","","load next color theme", "ec"," prompt red","change color of prompt", "ec"," prompt red blue","change color and background of prompt", ""," ","", "colors:","","rgb:000, red, green, blue, ...", "e scr.rgbcolor","=1|0","for 256 color cube (boolean)", "e scr.truecolor","=1|0","for 256*256*256 colors (boolean)", "$DATADIR/radare2/cons","","~/.config/radare2/cons ./", NULL}; r_core_cmd_help (core, helpmsg); } break; case 'o': // "eco" if (input[2] == ' ') { bool failed = false; char *home, path[512]; snprintf (path, sizeof (path), ".config/radare2/cons/%s", input+3); home = r_str_home (path); snprintf (path, sizeof (path), R2_DATDIR"/radare2/" R2_VERSION"/cons/%s", input+3); if (!r_core_cmd_file (core, home)) { if (r_core_cmd_file (core, path)) { //curtheme = r_str_dup (curtheme, path); curtheme = r_str_dup (curtheme, input + 3); } else { if (r_core_cmd_file (core, input+3)) { curtheme = r_str_dup (curtheme, input + 3); } else { eprintf ("eco: cannot open colorscheme profile (%s)\n", path); failed = true; } } } free (home); } else { nextpal (core, 'l'); } break; case 's': r_cons_pal_show (); break; case '*': r_cons_pal_list (1); break; case 'j': r_cons_pal_list ('j'); break; case 'c': r_cons_pal_list ('c'); break; case '\0': r_cons_pal_list (0); break; case 'r': // "ecr" r_cons_pal_random (); break; case 'n': // "ecn" nextpal (core, 'n'); break; default: { char *p = strdup (input + 2); char *q = strchr (p, '='); if (!q) q = strchr (p, ' '); if (q) { // set *q++ = 0; r_cons_pal_set (p, q); } else { const char *k = r_cons_pal_get (p); if (k) eprintf ("(%s)(%sCOLOR"Color_RESET")\n", p, k); } free (p); } } break; case 'e': if (input[1]==' ') { char *p; const char *val, *input2 = strchr (input+2, ' '); if (input2) input2++; else input2 = input+2; val = r_config_get (core->config, input2); p = r_core_editor (core, NULL, val); if (p) { r_str_replace_char (p, '\n', ';'); r_config_set (core->config, input2, p); } } else eprintf ("Usage: ee varname\n"); break; case '!': input = r_str_chop_ro (input+1); if (!r_config_toggle (core->config, input)) eprintf ("r_config: '%s' is not a boolean variable.\n", input); break; case '-': r_core_config_init (core); //eprintf ("BUG: 'e-' command locks the eval hashtable. patches are welcome :)\n"); break; case 'v': eprintf ("Invalid command '%s'. Use 'e?'\n", input); break; case '*': r_config_list (core->config, NULL, 1); break; case '?': switch (input[1]) { case '?': r_config_list (core->config, input+2, 2); break; default: r_config_list (core->config, input+1, 2); break; case 0:{ const char* help_msg[] = { "Usage:", "e[?] [var[=value]]", "Evaluable vars", "e","?asm.bytes", "show description", "e", "??", "list config vars with description", "e", "", "list config vars", "e-", "", "reset config vars", "e*", "", "dump config vars in r commands", "e!", "a", "invert the boolean value of 'a' var", "ee", "var", "open editor to change the value of var", "er", " [key]", "set config key as readonly. no way back", "ec", " [k] [color]", "set color for given key (prompt, offset, ...)", "et", " [key]", "show type of given config variable", "e", " a", "get value of var 'a'", "e", " a=b", "set var 'a' the 'b' value", "env", " [k[=v]]", "get/set environment variable", NULL}; r_core_cmd_help (core, help_msg); } } break; case 'r': if (input[1]) { const char *key = input+((input[1]==' ')?2:1); if (!r_config_readonly (core->config, key)) eprintf ("cannot find key '%s'\n", key); } else eprintf ("Usage: er [key]\n"); break; case ' ': r_config_eval (core->config, input+1); break; default: r_config_eval (core->config, input); break; } return 0; }
// XXX: if this function returns <0 in rows or cols expect MAYHEM R_API int r_cons_get_size(int *rows) { #if __WINDOWS__ && !__CYGWIN__ CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &csbi); I.columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; I.rows = csbi.srWindow.Bottom - csbi.srWindow.Top; // last row empty #elif EMSCRIPTEN I.columns = 80; I.rows = 23; #elif __UNIX__ || __CYGWIN__ struct winsize win; // use stdin as reference? //if (isatty (1) && ioctl (1, TIOCGWINSZ, &win) == 0) { if (isatty (0) && ioctl (0, TIOCGWINSZ, &win) == 0) { if (win.ws_col==0) { // TODO: use ttyname() ? int fd = open ("/dev/tty", O_RDONLY); if (fd != -1) { if (ioctl (fd, TIOCGWINSZ, &win) != 0) { I.columns = 80; I.rows = 23; } close (fd); } } I.columns = win.ws_col; I.rows = win.ws_row; } else { I.columns = 80; I.rows = 23; } #else char *str = r_sys_getenv ("COLUMNS"); if (str != NULL) { I.columns = atoi (str); I.rows = 23; // XXX. windows must get console size free (str); } else { I.columns = 80; I.rows = 23; } #endif #if SIMULATE_ADB_SHELL I.rows = 0; I.columns = 0; #endif #if SIMULATE_MAYHEM // expect tons of crashes I.rows = -1; I.columns = -1; #endif if (I.rows<0) I.rows = 0; if (I.columns<0) I.columns = 0; if (I.force_columns) I.columns = I.force_columns; if (I.force_rows) I.rows = I.force_rows; if (I.fix_columns) I.columns += I.fix_columns; if (I.fix_rows) I.rows += I.fix_rows; if (rows) *rows = I.rows; I.rows = R_MAX (0, I.rows); return R_MAX (0, I.columns); }
static struct cEnv_t* r_egg_Cfile_set_cEnv(const char *arch, const char *os, int bits) { struct cEnv_t *cEnv = calloc (1, sizeof (struct cEnv_t)); bool use_clang; char *buffer = NULL; char *output = NULL; if (!cEnv) { return NULL; } if (!(cEnv->CC = r_egg_Cfile_getCompiler())) { goto fail; } cEnv->SFLIBPATH = r_sys_getenv ("SFLIBPATH"); if (!cEnv->SFLIBPATH) { output = r_sys_cmd_strf ("r2 -hh | grep INCDIR | awk '{print $2}'"); if (!output || (output[0] == '\0')) { eprintf ("Cannot find SFLIBPATH env var.\n" "Please define it, or fix r2 installation.\n"); goto fail; } output[strlen (output) - 1] = '\0'; // strip the ending '\n' if (!(cEnv->SFLIBPATH = r_str_newf ("%s/sflib", output))) { goto fail; } } cEnv->JMP = r_egg_Cfile_armOrMips (arch) ? "b" : "jmp"; if (!strcmp (os, "darwin")) { cEnv->OBJCOPY = "gobjcopy"; cEnv->FMT = "mach0"; if (!strcmp (arch, "x86")) { if (bits == 32) { cEnv->CFLAGS = strdup ("-arch i386"); cEnv->LDFLAGS = strdup ("-arch i386 -shared -c"); } else { cEnv->CFLAGS = strdup ("-arch x86_64"); cEnv->LDFLAGS = strdup ("-arch x86_64 -shared -c"); } } else { cEnv->LDFLAGS = strdup ("-shared -c"); } cEnv->SHDR = r_str_newf ("\n.text\n%s _main\n", cEnv->JMP); } else { cEnv->OBJCOPY = "objcopy"; cEnv->FMT = "elf"; cEnv->SHDR = r_str_newf ("\n.section .text\n.globl main\n" "// .type main, @function\n%s main\n", cEnv->JMP); if (!strcmp (arch, "x86")) { if (bits == 32) { cEnv->CFLAGS = strdup ("-fPIC -fPIE -pie -fpic -m32"); cEnv->LDFLAGS = strdup ("-fPIC -fPIE -pie -fpic -m32"); } else { cEnv->CFLAGS = strdup ("-fPIC -fPIE -pie -fpic -m64"); cEnv->LDFLAGS = strdup ("-fPIC -fPIE -pie -fpic -m64"); } } else { cEnv->CFLAGS = strdup ("-fPIC -fPIE -pie -fpic -nostartfiles"); cEnv->LDFLAGS = strdup ("-fPIC -fPIE -pie -fpic -nostartfiles"); } } cEnv->TRIPLET = r_str_newf ("%s-%s-%d", os, arch, bits); if (!strcmp (os, "windows")) { cEnv->TEXT = ".text"; cEnv->FMT = "pe"; } else if (!strcmp (os, "darwin")) { cEnv->TEXT = "0.__TEXT.__text"; } else { cEnv->TEXT = ".text"; } use_clang = false; if (!strcmp (cEnv->TRIPLET, "darwin-arm-64")) { free (cEnv->CC); cEnv->CC = strdup ("xcrun --sdk iphoneos gcc -arch arm64 -miphoneos-version-min=0.0"); use_clang = true; cEnv->TEXT = "0.__TEXT.__text"; } else if (!strcmp (cEnv->TRIPLET, "darwin-arm-32")) { free (cEnv->CC); cEnv->CC = strdup ("xcrun --sdk iphoneos gcc -arch armv7 -miphoneos-version-min=0.0"); use_clang = true; cEnv->TEXT = "0.__TEXT.__text"; } buffer = r_str_newf ("%s -nostdinc -include '%s'/'%s'/sflib.h", cEnv->CFLAGS, cEnv->SFLIBPATH, cEnv->TRIPLET); if (!buffer) { goto fail; } free (cEnv->CFLAGS); cEnv->CFLAGS = strdup (buffer); if (use_clang) { free (buffer); buffer = r_str_newf ("%s -fomit-frame-pointer" " -fno-zero-initialized-in-bss", cEnv->CFLAGS); if (!buffer) { goto fail; } free (cEnv->CFLAGS); cEnv->CFLAGS = strdup (buffer); } else { free (buffer); buffer = r_str_newf ("%s -z execstack -fomit-frame-pointer" " -finline-functions -fno-zero-initialized-in-bss", cEnv->CFLAGS); if (!buffer) { goto fail; } free (cEnv->CFLAGS); cEnv->CFLAGS = strdup (buffer); } free (buffer); buffer = r_str_newf ("%s -nostdlib", cEnv->LDFLAGS); if (!buffer) { goto fail; } free (cEnv->LDFLAGS); cEnv->LDFLAGS = strdup (buffer); if (r_egg_Cfile_check_cEnv (cEnv)) { eprintf ("Error with cEnv allocation!\n"); goto fail; } free (buffer); free (output); return cEnv; fail: free (buffer); free (output); r_egg_Cfile_free_cEnv (cEnv); return NULL; }
static void parseline (char *b) { char *e = strchr (b, '='); if (!e) return; if (*b=='#') return; *e++ = 0; if (*e=='$') e = r_sys_getenv (e); if (e == NULL) return; if (!strcmp (b, "program")) _args[0] = _program = strdup (e); else if (!strcmp (b, "system")) _system = strdup (e); else if (!strcmp (b, "connect")) _connect = strdup (e); else if (!strcmp (b, "listen")) _listen = strdup (e); else if (!strcmp (b, "stdout")) _stdout = strdup (e); else if (!strcmp (b, "stdio")) { _stdout = _stderr = _stdin = strdup (e); } else if (!strcmp (b, "stdin")) _stdin = strdup (e); else if (!strcmp (b, "stderr")) _stderr = strdup (e); else if (!strcmp (b, "input")) _input = strdup (e); else if (!strcmp (b, "chdir")) _chgdir = strdup (e); else if (!strcmp (b, "chroot")) _chroot = strdup (e); else if (!strcmp (b, "libpath")) _libpath = strdup (e); else if (!strcmp (b, "preload")) _preload = strdup (e); else if (!strcmp (b, "r2preload")) _r2preload = \ (strcmp (e, "yes")? (strcmp (e, "true")? (strcmp (e, "1")? 0: 1): 1): 1); else if (!strcmp (b, "setuid")) _setuid = strdup (e); else if (!strcmp (b, "seteuid")) _seteuid = strdup (e); else if (!strcmp (b, "setgid")) _setgid = strdup (e); else if (!strcmp (b, "setegid")) _setegid = strdup (e); else if (!memcmp (b, "arg", 3)) { int n = atoi (b+3); if (n>=0 && n<NARGS) { _args[n] = getstr (e); } else eprintf ("Out of bounds args index: %d\n", n); } else if (!strcmp (b, "timeout")) { _timeout = atoi (e); } else if (!strcmp (b, "envfile")) { char *p, buf[1024]; FILE *fd = fopen (e, "r"); if (!fd) { eprintf ("Cannot open '%s'\n", e); return; } for (;;) { fgets (buf, sizeof (buf)-1, fd); if (feof (fd)) break; p = strchr (buf, '='); if (p) { *p = 0; r_sys_setenv (buf, p+1); } } fclose (fd); } else if (!strcmp (b, "unsetenv")) { r_sys_setenv (e, NULL); } else if (!strcmp (b, "setenv")) { char *v = strchr (e, '='); if (v) { *v++ = 0; r_sys_setenv (e, v); } } }
R_API int r_run_parseline (RRunProfile *p, char *b) { int must_free = R_FALSE; char *e = strchr (b, '='); if (!e) return 0; if (*b=='#') return 0; *e++ = 0; if (*e=='$') { must_free = R_TRUE; e = r_sys_getenv (e); } if (e == NULL) return 0; if (!strcmp (b, "program")) p->_args[0] = p->_program = strdup (e); else if (!strcmp (b, "system")) p->_system = strdup (e); else if (!strcmp (b, "aslr")) p->_aslr = parseBool (e); else if (!strcmp (b, "pid")) p->_pid = atoi (e); else if (!strcmp (b, "pidfile")) p->_pidfile = strdup (e); else if (!strcmp (b, "connect")) p->_connect = strdup (e); else if (!strcmp (b, "listen")) p->_listen = strdup (e); else if (!strcmp (b, "stdout")) p->_stdout = strdup (e); else if (!strcmp (b, "stdio")) { p->_stdout = p->_stderr = p->_stdin = strdup (e); } else if (!strcmp (b, "stdin")) p->_stdin = strdup (e); else if (!strcmp (b, "stderr")) p->_stderr = strdup (e); else if (!strcmp (b, "input")) p->_input = strdup (e); else if (!strcmp (b, "chdir")) p->_chgdir = strdup (e); else if (!strcmp (b, "core")) p->_docore = parseBool (e); else if (!strcmp (b, "sleep")) p->_r2sleep = atoi (e); else if (!strcmp (b, "maxstack")) p->_maxstack = atoi (e); else if (!strcmp (b, "maxproc")) p->_maxproc = atoi (e); else if (!strcmp (b, "maxfd")) p->_maxfd = atoi (e); else if (!strcmp (b, "bits")) p->_bits = atoi (e); else if (!strcmp (b, "chroot")) p->_chroot = strdup (e); else if (!strcmp (b, "libpath")) p->_libpath = strdup (e); else if (!strcmp (b, "preload")) p->_preload = strdup (e); else if (!strcmp (b, "r2preload")) p->_r2preload = parseBool (e); else if (!strcmp (b, "setuid")) p->_setuid = strdup (e); else if (!strcmp (b, "seteuid")) p->_seteuid = strdup (e); else if (!strcmp (b, "setgid")) p->_setgid = strdup (e); else if (!strcmp (b, "setegid")) p->_setegid = strdup (e); else if (!strcmp (b, "nice")) p->_nice = atoi (e); else if (!memcmp (b, "arg", 3)) { int n = atoi (b+3); if (n>=0 && n<R_RUN_PROFILE_NARGS) { p->_args[n] = getstr (e); } else eprintf ("Out of bounds args index: %d\n", n); } else if (!strcmp (b, "timeout")) { p->_timeout = atoi (e); } else if (!strcmp (b, "envfile")) { char *p, buf[1024]; FILE *fd = fopen (e, "r"); if (!fd) { eprintf ("Cannot open '%s'\n", e); if (must_free == R_TRUE) free (e); return 0; } for (;;) { fgets (buf, sizeof (buf)-1, fd); if (feof (fd)) break; p = strchr (buf, '='); if (p) { *p = 0; r_sys_setenv (buf, p+1); } } fclose (fd); } else if (!strcmp (b, "unsetenv")) { r_sys_setenv (e, NULL); } else if (!strcmp (b, "setenv")) { char *v = strchr (e, '='); if (v) { *v++ = 0; r_sys_setenv (e, v); } } if (must_free == R_TRUE) free (e); return 1; }
R_API bool r_run_parseline(RRunProfile *p, char *b) { int must_free = false; char *e = strchr (b, '='); if (!e || *b == '#') { return 0; } *e++ = 0; if (*e=='$') { must_free = true; e = r_sys_getenv (e); } if (!e) { return 0; } if (!strcmp (b, "program")) { p->_args[0] = p->_program = strdup (e); } else if (!strcmp (b, "system")) { p->_system = strdup (e); } else if (!strcmp (b, "runlib")) { p->_runlib = strdup (e); } else if (!strcmp (b, "runlib.fcn")) { p->_runlib_fcn = strdup (e); } else if (!strcmp (b, "aslr")) { p->_aslr = parseBool (e); } else if (!strcmp (b, "pid")) { p->_pid = atoi (e); } else if (!strcmp (b, "pidfile")) { p->_pidfile = strdup (e); } else if (!strcmp (b, "connect")) { p->_connect = strdup (e); } else if (!strcmp (b, "listen")) { p->_listen = strdup (e); } else if (!strcmp (b, "pty")) { p->_pty = parseBool (e); } else if (!strcmp (b, "stdio")) { if (e[0] == '!') { p->_stdio = strdup (e); } else { p->_stdout = strdup (e); p->_stderr = strdup (e); p->_stdin = strdup (e); } } else if (!strcmp (b, "stdout")) { p->_stdout = strdup (e); } else if (!strcmp (b, "stdin")) { p->_stdin = strdup (e); } else if (!strcmp (b, "stderr")) { p->_stderr = strdup (e); } else if (!strcmp (b, "input")) { p->_input = strdup (e); } else if (!strcmp (b, "chdir")) { p->_chgdir = strdup (e); } else if (!strcmp (b, "core")) { p->_docore = parseBool (e); } else if (!strcmp (b, "fork")) { p->_dofork = parseBool (e); } else if (!strcmp (b, "sleep")) { p->_r2sleep = atoi (e); } else if (!strcmp (b, "maxstack")) { p->_maxstack = atoi (e); } else if (!strcmp (b, "maxproc")) { p->_maxproc = atoi (e); } else if (!strcmp (b, "maxfd")) { p->_maxfd = atoi (e); } else if (!strcmp (b, "bits")) { p->_bits = atoi (e); } else if (!strcmp (b, "chroot")) { p->_chroot = strdup (e); } else if (!strcmp (b, "libpath")) { p->_libpath = strdup (e); } else if (!strcmp (b, "preload")) { p->_preload = strdup (e); } else if (!strcmp (b, "r2preload")) { p->_r2preload = parseBool (e); } else if (!strcmp (b, "r2preweb")) { r_sys_setenv ("RARUN2_WEB", "yes"); } else if (!strcmp (b, "setuid")) { p->_setuid = strdup (e); } else if (!strcmp (b, "seteuid")) { p->_seteuid = strdup (e); } else if (!strcmp (b, "setgid")) { p->_setgid = strdup (e); } else if (!strcmp (b, "setegid")) { p->_setegid = strdup (e); } else if (!strcmp (b, "nice")) { p->_nice = atoi (e); } else if (!strcmp (b, "timeout")) { p->_timeout = atoi (e); } else if (!strcmp (b, "timeoutsig")) { p->_timeout_sig = r_signal_from_string (e); } else if (!memcmp (b, "arg", 3)) { int n = atoi (b + 3); if (n >= 0 && n < R_RUN_PROFILE_NARGS) { p->_args[n] = getstr (e); p->_argc++; } else { eprintf ("Out of bounds args index: %d\n", n); } } else if (!strcmp (b, "envfile")) { char *p, buf[1024]; size_t len; FILE *fd = fopen (e, "r"); if (!fd) { eprintf ("Cannot open '%s'\n", e); if (must_free == true) { free (e); } return false; } for (;;) { if (!fgets (buf, sizeof (buf) - 1, fd)) { break; } if (feof (fd)) { break; } p = strchr (buf, '='); if (p) { *p++ = 0; len = strlen(p); if (p[len - 1] == '\n') { p[len - 1] = 0; } if (p[len - 2] == '\r') { p[len - 2] = 0; } r_sys_setenv (buf, p); } } fclose (fd); } else if (!strcmp (b, "unsetenv")) { r_sys_setenv (e, NULL); } else if (!strcmp (b, "setenv")) { char *V, *v = strchr (e, '='); if (v) { *v++ = 0; V = getstr (v); r_sys_setenv (e, V); free (V); } } else if (!strcmp(b, "clearenv")) { r_sys_clearenv (); } if (must_free == true) { free (e); } return true; }
R_API RCons *r_cons_new () { I.refcnt++; if (I.refcnt != 1) return &I; I.line = r_line_new (); I.highlight = NULL; I.event_interrupt = NULL; I.is_wine = -1; I.fps = 0; I.blankline = true; I.teefile = NULL; I.fix_columns = 0; I.fix_rows = 0; I.mouse_event = 0; I.force_rows = 0; I.force_columns = 0; I.event_resize = NULL; I.data = NULL; I.event_data = NULL; I.is_interactive = true; I.noflush = false; I.fdin = stdin; I.fdout = 1; I.breaked = false; //I.lines = 0; I.buffer = NULL; I.buffer_sz = 0; I.buffer_len = 0; r_cons_get_size (NULL); I.num = NULL; I.null = 0; #if __WINDOWS__ && !__CYGWIN__ I.ansicon = r_sys_getenv ("ANSICON"); #endif #if EMSCRIPTEN /* do nothing here :? */ #elif __UNIX__ || __CYGWIN__ tcgetattr (0, &I.term_buf); memcpy (&I.term_raw, &I.term_buf, sizeof (I.term_raw)); I.term_raw.c_iflag &= ~(BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); I.term_raw.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); I.term_raw.c_cflag &= ~(CSIZE|PARENB); I.term_raw.c_cflag |= CS8; I.term_raw.c_cc[VMIN] = 1; // Solaris stuff hehe signal (SIGWINCH, resize); #elif __WINDOWS__ h = GetStdHandle (STD_INPUT_HANDLE); GetConsoleMode (h, (PDWORD) &I.term_buf); I.term_raw = 0; if (!SetConsoleCtrlHandler ((PHANDLER_ROUTINE)__w32_control, TRUE)) eprintf ("r_cons: Cannot set control console handler\n"); #endif I.pager = NULL; /* no pager by default */ I.truecolor = 0; I.mouse = 0; r_cons_pal_null (); r_cons_pal_init (NULL); r_cons_rgb_init (); r_cons_reset (); return &I; }
// XXX: if this function returns <0 in rows or cols expect MAYHEM R_API int r_cons_get_size(int *rows) { #if __WINDOWS__ && !__CYGWIN__ char buffer[1024]; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &csbi); I.columns = (csbi.srWindow.Right - csbi.srWindow.Left) - 1; I.rows = csbi.srWindow.Bottom - csbi.srWindow.Top; // last row empty #elif EMSCRIPTEN I.columns = 80; I.rows = 23; #elif __UNIX__ || __CYGWIN__ struct winsize win = { 0 }; if (isatty (0) && !ioctl (0, TIOCGWINSZ, &win)) { if ((!win.ws_col) || (!win.ws_row)) { const char *tty = ttyname (1); int fd = open (tty? tty: "/dev/tty", O_RDONLY); if (fd != -1) { int ret = ioctl (fd, TIOCGWINSZ, &win); if (ret || !win.ws_col || !win.ws_row) { win.ws_col = 80; win.ws_row = 23; } close (fd); } } I.columns = win.ws_col; I.rows = win.ws_row; } else { I.columns = 80; I.rows = 23; } #else char *str = r_sys_getenv ("COLUMNS"); if (str) { I.columns = atoi (str); I.rows = 23; // XXX. windows must get console size free (str); } else { I.columns = 80; I.rows = 23; } #endif #if SIMULATE_ADB_SHELL I.rows = 0; I.columns = 0; #endif #if SIMULATE_MAYHEM // expect tons of crashes I.rows = -1; I.columns = -1; #endif if (I.rows < 0) { I.rows = 0; } if (I.columns < 0) { I.columns = 0; } if (I.force_columns) { I.columns = I.force_columns; } if (I.force_rows) { I.rows = I.force_rows; } if (I.fix_columns) { I.columns += I.fix_columns; } if (I.fix_rows) { I.rows += I.fix_rows; } if (rows) { *rows = I.rows; } I.rows = R_MAX (0, I.rows); return R_MAX (0, I.columns); }
int gdbr_connect(libgdbr_t *g, const char *host, int port) { const char *message = "qSupported:multiprocess+;qRelocInsn+;xmlRegisters=i386"; RStrBuf tmp; r_strbuf_init (&tmp); int ret; if (!g || !host) { return -1; } // Initial max_packet_size for remote target (minimum so far for AVR = 64) g->stub_features.pkt_sz = 64; char *env_pktsz_str; ut32 env_pktsz = 0; if ((env_pktsz_str = r_sys_getenv ("R2_GDB_PKTSZ"))) { if ((env_pktsz = (ut32) strtoul (env_pktsz_str, NULL, 10))) { g->stub_features.pkt_sz = R_MAX (env_pktsz, GDB_MAX_PKTSZ); } } ret = snprintf (tmp.buf, sizeof (tmp.buf) - 1, "%d", port); if (!ret) { return -1; } if (*host == '/') { ret = r_socket_connect_serial (g->sock, host, port, 1); } else { ret = r_socket_connect_tcp (g->sock, host, tmp.buf, 400); } if (!ret) { return -1; } if (send_ack (g) < 0) { return -1; } read_packet (g, true); // vcont=true lets us skip if we get no reply g->connected = 1; // TODO add config possibility here ret = send_msg (g, message); if (ret < 0) { return ret; } read_packet (g, false); ret = handle_qSupported (g); if (ret < 0) { return ret; } if (env_pktsz > 0) { g->stub_features.pkt_sz = R_MAX (R_MIN (env_pktsz, g->stub_features.pkt_sz), GDB_MAX_PKTSZ); } // If no-ack supported, enable no-ack mode (should speed up things) if (g->stub_features.QStartNoAckMode) { if (send_msg (g, "QStartNoAckMode") < 0) { return -1; } read_packet (g, false); if (!strncmp (g->data, "OK", 2)) { // Just in case, send ack send_ack (g); g->no_ack = true; } } if (g->remote_type == GDB_REMOTE_TYPE_LLDB) { return gdbr_connect_lldb (g); } // Query the thread / process id g->stub_features.qC = true; g->pid = g->tid = 0; ret = send_msg (g, "qC"); if (ret < 0) { return ret; } read_packet (g, false); ret = handle_qC (g); if (ret < 0) { g->stub_features.qC = false; } // Check if vCont is supported gdbr_check_vcont (g); // Set pid/thread for operations other than "step" and "continue" if (gdbr_select (g, g->pid, 0) < 0) { // return -1; } // Set thread for "step" and "continue" operations snprintf (tmp.buf, sizeof (tmp.buf) - 1, "Hc-1"); ret = send_msg (g, tmp.buf); if (ret < 0) { return ret; } read_packet (g, false); ret = send_ack (g); if (strcmp (g->data, "OK")) { // return -1; } if (g->stub_features.qXfer_features_read) { gdbr_read_target_xml (g); } reg_cache_init (g); return ret; }