diff --git a/binr/r2r/r2r.c b/binr/r2r/r2r.c index d82ccf99f8..8d49321748 100644 --- a/binr/r2r/r2r.c +++ b/binr/r2r/r2r.c @@ -31,10 +31,11 @@ static int help(bool verbose) { } int main(int argc, char **argv) { - int workers_count = 4; // TODO: read from arg + int c, workers_count = 4; // TODO: read from arg bool verbose = false; - int c; - while ((c = r_getopt (argc, argv, "hv")) != -1) { + RGetopt opt; + r_getopt_init (&opt, argc, (const char **)argv, "hv"); + while ((c = r_getopt_next (&opt)) != -1) { switch (c) { case 'h': return help (true); @@ -71,10 +72,10 @@ int main(int argc, char **argv) { return -1; } - if (r_optind < argc) { + if (opt.ind < argc) { // Manually specified path(s) int i; - for (i = r_optind; i < argc; i++) { + for (i = opt.ind; i < argc; i++) { if (!r2r_test_database_load (state.db, argv[i])) { eprintf ("Failed to load tests from \"%s\"\n", argv[i]); r2r_test_database_free (state.db); diff --git a/libr/core/linux_heap_glibc.c b/libr/core/linux_heap_glibc.c index fd7d1bc1a1..214f1acc2d 100644 --- a/libr/core/linux_heap_glibc.c +++ b/libr/core/linux_heap_glibc.c @@ -789,7 +789,6 @@ static void GH(print_tcache_instance)(RCore *core, GHT m_arena, MallocState *mai GHT brk_start = GHT_MAX, brk_end = GHT_MAX, tcache_fd = GHT_MAX, initial_brk = GHT_MAX ; GH(get_brks) (core, &brk_start, &brk_end); GHT tcache_tmp = GHT_MAX, tcache_start = GHT_MAX; - const int offset = r_config_get_i (core->config, "dbg.glibc.fc_offset"); RConsPrintablePalette *pal = &r_cons_singleton ()->context->pal; tcache_start = ((brk_start >> 12) << 12) + GH(HDR_SZ); diff --git a/libr/include/r_core.h b/libr/include/r_core.h index 9651188ebb..3e3f9a193e 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -333,13 +333,13 @@ typedef struct r_core_t { RMainCallback r_main_radare2; // int (*r_main_radare2)(int argc, char **argv); - int (*r_main_rafind2)(int argc, char **argv); - int (*r_main_radiff2)(int argc, char **argv); - int (*r_main_rabin2)(int argc, char **argv); - int (*r_main_rarun2)(int argc, char **argv); - int (*r_main_ragg2)(int argc, char **argv); - int (*r_main_rasm2)(int argc, char **argv); - int (*r_main_rax2)(int argc, char **argv); + int (*r_main_rafind2)(int argc, const char **argv); + int (*r_main_radiff2)(int argc, const char **argv); + int (*r_main_rabin2)(int argc, const char **argv); + int (*r_main_rarun2)(int argc, const char **argv); + int (*r_main_ragg2)(int argc, const char **argv); + int (*r_main_rasm2)(int argc, const char **argv); + int (*r_main_rax2)(int argc, const char **argv); } RCore; // maybe move into RAnal diff --git a/libr/include/r_getopt.h b/libr/include/r_getopt.h index 5ef9d2f87f..6fb3ae621d 100644 --- a/libr/include/r_getopt.h +++ b/libr/include/r_getopt.h @@ -1,29 +1,21 @@ +#ifndef R_GETOPT_H +#define R_GETOPT_H 1 + #include -/* +typedef struct r_getopt_t { + int err; + int ind; + int opt; + int reset; + const char *arg; + // ... + int argc; + const char **argv; + const char *ostr; +} RGetopt; -At some point we should have a working and reliable implementation -of getopt to work on any operating system independently of the libc - -Duplicated code in here: - -* libr/util/getopt.c - -*/ - -#if __WINDOWS__ - -#ifndef GETOPT_C -__declspec(dllimport) int r_optind; -__declspec(dllimport) char *r_optarg; -#endif -R_API int r_getopt(int nargc, char * const *nargv, const char *ostr); - -#else - -#include -#define r_getopt getopt -#define r_optind optind -#define r_optarg optarg +R_API void r_getopt_init(RGetopt *go, int argc, const char **argv, const char *ostr); +R_API int r_getopt_next(RGetopt *opt); #endif diff --git a/libr/include/r_main.h b/libr/include/r_main.h index 7dcb4dae55..fb302ef8cf 100644 --- a/libr/include/r_main.h +++ b/libr/include/r_main.h @@ -1,34 +1,35 @@ -/* radare - LGPL - Copyright 2008-2019 - pancake */ +/* radare - LGPL - Copyright 2008-2020 - pancake */ #ifndef R2_MAIN_H #define R2_MAIN_H #include +#include R_LIB_VERSION_HEADER(r_main); typedef struct r_main_t { - char *name; - int (*main)(int argc, char **argv); + const char *name; + int (*main)(int argc, const char **argv); // stdin/stdout } RMain; -typedef int (*RMainCallback)(int argc, char **argv); +typedef int (*RMainCallback)(int argc, const char **argv); R_API RMain *r_main_new(const char *name); R_API void r_main_free(RMain *m); -R_API int r_main_run(RMain *m, int argc, char **argv); +R_API int r_main_run(RMain *m, int argc, const char **argv); R_API int r_main_version_print(const char *program); -R_API int r_main_rax2(int argc, char **argv); -R_API int r_main_rarun2(int argc, char **argv); -R_API int r_main_rahash2(int argc, char **argv); -R_API int r_main_rabin2(int argc, char **argv); -R_API int r_main_radare2(int argc, char **argv); -R_API int r_main_rasm2(int argc, char **argv); -R_API int r_main_r2agent(int argc, char **argv); -R_API int r_main_rafind2(int argc, char **argv); -R_API int r_main_radiff2(int argc, char **argv); -R_API int r_main_ragg2(int argc, char **argv); +R_API int r_main_rax2(int argc, const char **argv); +R_API int r_main_rarun2(int argc, const char **argv); +R_API int r_main_rahash2(int argc, const char **argv); +R_API int r_main_rabin2(int argc, const char **argv); +R_API int r_main_radare2(int argc, const char **argv); +R_API int r_main_rasm2(int argc, const char **argv); +R_API int r_main_r2agent(int argc, const char **argv); +R_API int r_main_rafind2(int argc, const char **argv); +R_API int r_main_radiff2(int argc, const char **argv); +R_API int r_main_ragg2(int argc, const char **argv); #endif diff --git a/libr/include/r_socket.h b/libr/include/r_socket.h index fe9ceaca8b..1223449412 100644 --- a/libr/include/r_socket.h +++ b/libr/include/r_socket.h @@ -248,7 +248,7 @@ typedef struct r_run_profile_t { R_API RRunProfile *r_run_new(const char *str); R_API bool r_run_parse(RRunProfile *pf, const char *profile); R_API void r_run_free(RRunProfile *r); -R_API bool r_run_parseline(RRunProfile *p, char *b); +R_API bool r_run_parseline(RRunProfile *p, const char *b); R_API const char *r_run_help(void); R_API int r_run_config_env(RRunProfile *p); R_API int r_run_start(RRunProfile *p); diff --git a/libr/main/main.c b/libr/main/main.c index d357048e8f..c56cc2c3de 100644 --- a/libr/main/main.c +++ b/libr/main/main.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2012-2019 - pancake */ +/* radare - LGPL - Copyright 2012-2020 - pancake */ #include #include @@ -37,11 +37,10 @@ R_API RMain *r_main_new (const char *name) { } R_API void r_main_free(RMain *m) { - free (m->name); free (m); } -R_API int r_main_run(RMain *m, int argc, char **argv) { +R_API int r_main_run(RMain *m, int argc, const char **argv) { r_return_val_if_fail (m && m->main, -1); return m->main (argc, argv); } diff --git a/libr/main/r2agent.c b/libr/main/r2agent.c index deb9ec2667..5369163b90 100644 --- a/libr/main/r2agent.c +++ b/libr/main/r2agent.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2013-2019 - pancake */ +/* radare2 - LGPL - Copyright 2013-2020 - pancake */ #include "index.h" #include @@ -30,7 +30,7 @@ static int showversion() { return r_main_version_print ("r2agent"); } -R_API int r_main_r2agent(int argc, char **argv) { +R_API int r_main_r2agent(int argc, const char **argv) { RSocket *s; RSocketHTTPOptions so; RSocketHTTPRequest *rs; @@ -43,7 +43,9 @@ R_API int r_main_r2agent(int argc, char **argv) { char *pfile = NULL; memset (&so, 0, sizeof (so)); - while ((c = r_getopt (argc, argv, "adhup:t:sv")) != -1) { + RGetopt opt; + r_getopt_init (&opt, argc, argv, "adhup:t:sv"); + while ((c = r_getopt_next (&opt)) != -1) { switch (c) { case 'a': listenlocal = false; @@ -62,16 +64,16 @@ R_API int r_main_r2agent(int argc, char **argv) { so.httpauth = true; break; case 't': - httpauthfile = r_optarg; + httpauthfile = opt.arg; break; case 'p': - port = r_optarg; + port = opt.arg; break; default: return usage (0); } } - if (r_optind != argc) { + if (opt.ind != argc) { return usage (0); } diff --git a/libr/main/rabin2.c b/libr/main/rabin2.c index 22776cb174..dca80f59e0 100644 --- a/libr/main/rabin2.c +++ b/libr/main/rabin2.c @@ -537,11 +537,11 @@ static void __listPlugins(RBin *bin, const char* plugin_name, int rad) { } } -R_API int r_main_rabin2(int argc, char **argv) { - char *name = NULL; - char *file = NULL; +R_API int r_main_rabin2(int argc, const char **argv) { RBin *bin = NULL; - char *output = NULL; + const char *name = NULL; + const char *file = NULL; + const char *output = NULL; int rad = 0; ut64 laddr = UT64_MAX; ut64 baddr = UT64_MAX; @@ -551,7 +551,8 @@ R_API int r_main_rabin2(int argc, char **argv) { char* create = NULL; bool va = true; ut64 action = R_BIN_REQ_UNK; - char *tmp, *ptr, *arch = NULL, *arch_name = NULL; + char *tmp, *ptr, *arch_name = NULL; + const char *arch = NULL; const char *forcebin = NULL; const char *chksum = NULL; const char *op = NULL; @@ -642,7 +643,9 @@ R_API int r_main_rabin2(int argc, char **argv) { #define is_active(x) (action & (x)) #define set_action(x) { actions++; action |= (x); } #define unset_action(x) action &= ~x - while ((c = r_getopt (argc, argv, "DjgAf:F:a:B:G:b:cC:k:K:dD:Mm:n:N:@:isSVIHeEUlRwO:o:pPqQrTtvLhuxXzZ")) != -1) { + RGetopt opt; + r_getopt_init (&opt, argc, argv, "DjgAf:F:a:B:G:b:cC:k:K:dD:Mm:n:N:@:isSVIHeEUlRwO:o:pPqQrTtvLhuxXzZ"); + while ((c = r_getopt_next (&opt)) != -1) { switch (c) { case 'g': set_action (R_BIN_REQ_CLASSES); @@ -673,14 +676,14 @@ R_API int r_main_rabin2(int argc, char **argv) { break; case 'j': rad = R_MODE_JSON; break; case 'A': set_action (R_BIN_REQ_LISTARCHS); break; - case 'a': arch = r_optarg; break; + case 'a': arch = opt.arg; break; case 'C': set_action (R_BIN_REQ_CREATE); - create = strdup (r_optarg); + create = strdup (opt.arg); break; case 'u': bin->filter = 0; break; - case 'k': query = r_optarg; break; - case 'K': chksum = r_optarg; break; + case 'k': query = opt.arg; break; + case 'K': chksum = opt.arg; break; case 'c': if (is_active (R_BIN_REQ_CLASSES)) { rad = R_MODE_CLASSDUMP; @@ -688,11 +691,11 @@ R_API int r_main_rabin2(int argc, char **argv) { set_action (R_BIN_REQ_CLASSES); } break; - case 'f': arch_name = strdup (r_optarg); break; - case 'F': forcebin = r_optarg; break; - case 'b': bits = r_num_math (NULL, r_optarg); break; + case 'f': arch_name = strdup (opt.arg); break; + case 'F': forcebin = opt.arg; break; + case 'b': bits = r_num_math (NULL, opt.arg); break; case 'm': - at = r_num_math (NULL, r_optarg); + at = r_num_math (NULL, opt.arg); set_action (R_BIN_REQ_SRCLINE); break; case 'i': @@ -739,13 +742,13 @@ R_API int r_main_rabin2(int argc, char **argv) { } break; case 'D': - if (argv[r_optind] && argv[r_optind+1] && \ - (!argv[r_optind+1][0] || !strcmp (argv[r_optind+1], "all"))) { - r_config_set (core.config, "bin.lang", argv[r_optind]); + if (argv[opt.ind] && argv[opt.ind+1] && \ + (!argv[opt.ind+1][0] || !strcmp (argv[opt.ind+1], "all"))) { + r_config_set (core.config, "bin.lang", argv[opt.ind]); r_config_set (core.config, "bin.demangle", "true"); - r_optind += 2; + opt.ind += 2; } else { - do_demangle = argv[r_optind]; + do_demangle = argv[opt.ind]; } break; case 'e': @@ -765,7 +768,7 @@ R_API int r_main_rabin2(int argc, char **argv) { case 'x': set_action (R_BIN_REQ_EXTRACT); break; case 'X': set_action (R_BIN_REQ_PACKAGE); break; case 'O': - op = r_optarg; + op = opt.arg; set_action (R_BIN_REQ_OPERATION); if (*op == 'c') { r_sys_setenv ("RABIN2_CODESIGN_VERBOSE", "1"); @@ -784,13 +787,13 @@ R_API int r_main_rabin2(int argc, char **argv) { r_core_fini (&core); return 0; } - if (r_optind == argc) { + if (opt.ind == argc) { eprintf ("Missing filename\n"); r_core_fini (&core); return 1; } break; - case 'o': output = r_optarg; break; + case 'o': output = opt.arg; break; case 'p': va = false; break; case 'r': rad = true; break; case 'v': @@ -800,26 +803,26 @@ R_API int r_main_rabin2(int argc, char **argv) { set_action (R_BIN_REQ_LISTPLUGINS); break; case 'G': - laddr = r_num_math (NULL, r_optarg); + laddr = r_num_math (NULL, opt.arg); if (laddr == UT64_MAX) { va = false; } break; case 'B': - baddr = r_num_math (NULL, r_optarg); + baddr = r_num_math (NULL, opt.arg); break; case '@': - at = r_num_math (NULL, r_optarg); - if (at == 0LL && *r_optarg != '0') { + at = r_num_math (NULL, opt.arg); + if (at == 0LL && *opt.arg != '0') { at = UT64_MAX; } break; case 'n': - name = r_optarg; + name = opt.arg; break; case 'N': - tmp = strchr (r_optarg, ':'); - r_config_set (core.config, "bin.minstr", r_optarg); + tmp = strchr (opt.arg, ':'); + r_config_set (core.config, "bin.minstr", opt.arg); if (tmp) { r_config_set (core.config, "bin.maxstr", tmp + 1); } @@ -835,8 +838,8 @@ R_API int r_main_rabin2(int argc, char **argv) { if (is_active (R_BIN_REQ_LISTPLUGINS)) { const char* plugin_name = NULL; - if (r_optind < argc) { - plugin_name = argv[r_optind]; + if (opt.ind < argc) { + plugin_name = argv[opt.ind]; } __listPlugins (bin, plugin_name, rad); r_core_fini (&core); @@ -846,12 +849,12 @@ R_API int r_main_rabin2(int argc, char **argv) { if (do_demangle) { char *res = NULL; int type; - if ((argc - r_optind) < 2) { + if ((argc - opt.ind) < 2) { r_core_fini (&core); return rabin_show_help (0); } type = r_bin_demangle_type (do_demangle); - file = argv[r_optind + 1]; + file = argv[opt.ind + 1]; if (!strcmp (file, "-")) { for (;;) { file = stdin_gets (false); @@ -889,7 +892,7 @@ R_API int r_main_rabin2(int argc, char **argv) { r_core_fini (&core); return 1; } - file = argv[r_optind]; + file = argv[opt.ind]; if (!query) { if (action & R_BIN_REQ_HELP || action == R_BIN_REQ_UNK || !file) { r_core_fini (&core); @@ -989,20 +992,20 @@ R_API int r_main_rabin2(int argc, char **argv) { } if (action & R_BIN_REQ_PACKAGE) { RList *files = r_list_newf (NULL); - const char *format = argv[r_optind]; - const char *file = argv[r_optind + 1]; + const char *format = argv[opt.ind]; + const char *file = argv[opt.ind + 1]; int i, rc = 0; - if (r_optind + 3 > argc) { + if (opt.ind + 3 > argc) { eprintf ("Usage: rabin2 -X [fat|zip] foo.zip a b c\n"); r_core_fini (&core); return 1; } eprintf ("FMT %s\n", format); eprintf ("PKG %s\n", file); - for (i = r_optind + 2; i < argc; i++) { + for (i = opt.ind + 2; i < argc; i++) { eprintf ("ADD %s\n", argv[i]); - r_list_append (files, argv[i]); + r_list_append (files, (void*)argv[i]); } RBuffer *buf = r_bin_package (core.bin, format, file, files); /* TODO: return bool or something to catch errors\n") */ @@ -1039,11 +1042,11 @@ R_API int r_main_rabin2(int argc, char **argv) { r_bin_force_plugin (bin, forcebin); r_bin_load_filter (bin, action); - RBinOptions opt; - r_bin_options_init (&opt, fd, baddr, laddr, rawstr); - opt.xtr_idx = xtr_idx; + RBinOptions bo; + r_bin_options_init (&bo, fd, baddr, laddr, rawstr); + bo.xtr_idx = xtr_idx; - if (!r_bin_open (bin, file, &opt)) { + if (!r_bin_open (bin, file, &bo)) { //if this return false means that we did not return a valid bin object //but we have yet the chance that this file is a fat binary if (!bin->cur || !bin->cur->xtr_data) { diff --git a/libr/main/radare2.c b/libr/main/radare2.c index e04baa1dd3..ad2175f091 100644 --- a/libr/main/radare2.c +++ b/libr/main/radare2.c @@ -321,7 +321,7 @@ static void set_color_default(RCore *r) { free (tmp); } -R_API int r_main_radare2(int argc, char **argv) { +R_API int r_main_radare2(int argc, const char **argv) { RCore *r; bool forcequit = false; bool haveRarunProfile = false; @@ -346,7 +346,8 @@ R_API int r_main_radare2(int argc, char **argv) { ut64 baddr = UT64_MAX; ut64 seek = UT64_MAX; bool do_list_io_plugins = false; - char *pfile = NULL, *file = NULL; + char *file = NULL; + char *pfile = NULL; const char *debugbackend = "native"; const char *asmarch = NULL; const char *asmos = NULL; @@ -427,11 +428,9 @@ R_API int r_main_radare2(int argc, char **argv) { set_color_default (r); - while ((c = r_getopt (argc, argv, "=02AMCwxfF:H:hm:e:nk:NdqQs:p:b:B:a:Lui:I:l:P:R:r:c:D:vVSTzuX" -#if USE_THREADS -"t" -#endif - )) != -1) { + RGetopt opt; + r_getopt_init (&opt, argc, argv, "=02AMCwxfF:H:hm:e:nk:NdqQs:p:b:B:a:Lui:I:l:P:R:r:c:D:vVSTzuXt"); + while ((c = r_getopt_next (&opt)) != -1) { switch (c) { case '=': r->cmdremote = 1; @@ -451,7 +450,7 @@ R_API int r_main_radare2(int argc, char **argv) { r_config_set (r->config, "bin.filter", "false"); break; case 'a': - asmarch = r_optarg; + asmarch = opt.arg; break; case 'z': zflag++; @@ -460,16 +459,16 @@ R_API int r_main_radare2(int argc, char **argv) { do_analysis += do_analysis ? 1: 2; break; case 'b': - asmbits = r_optarg; + asmbits = opt.arg; break; case 'B': - baddr = r_num_math (r->num, r_optarg); + baddr = r_num_math (r->num, opt.arg); break; case 'X': r_config_set (r->config, "bin.usextr", "false"); break; case 'c': - r_list_append (cmds, r_optarg); + r_list_append (cmds, (void*)opt.arg); break; case 'C': do_connect = true; @@ -481,8 +480,8 @@ R_API int r_main_radare2(int argc, char **argv) { #endif case 'D': debug = 2; - debugbackend = r_optarg; - if (!strcmp (r_optarg, "?")) { + debugbackend = opt.arg; + if (!strcmp (opt.arg, "?")) { r_debug_plugin_list (r->dbg, 'q'); r_cons_flush(); LISTS_FREE (); @@ -490,44 +489,44 @@ R_API int r_main_radare2(int argc, char **argv) { } break; case 'e': - if (!strcmp (r_optarg, "q")) { + if (!strcmp (opt.arg, "q")) { r_core_cmd0 (r, "eq"); } else { - r_config_eval (r->config, r_optarg, false); - r_list_append (evals, r_optarg); + r_config_eval (r->config, opt.arg, false); + r_list_append (evals, (void*)opt.arg); } break; case 'f': fullfile = true; break; case 'F': - forcebin = r_optarg; + forcebin = opt.arg; break; case 'h': help++; break; case 'H': - main_print_var (r_optarg); + main_print_var (opt.arg); LISTS_FREE (); return 0; case 'i': - r_list_append (files, r_optarg); + r_list_append (files, (void*)opt.arg); break; case 'I': - r_list_append (prefiles, r_optarg); + r_list_append (prefiles, (void*)opt.arg); break; case 'k': - asmos = r_optarg; + asmos = opt.arg; break; case 'l': - r_lib_open (r->lib, r_optarg); + r_lib_open (r->lib, opt.arg); break; case 'L': do_list_io_plugins = true; break; case 'm': - mapaddr = r_num_math (r->num, r_optarg); - s_seek = r_optarg; + mapaddr = r_num_math (r->num, opt.arg); + s_seek = opt.arg; r_config_set_i (r->config, "file.offset", mapaddr); break; case 'M': @@ -546,16 +545,16 @@ R_API int r_main_radare2(int argc, char **argv) { run_rc = false; break; case 'p': - if (!strcmp (r_optarg, "?")) { + if (!strcmp (opt.arg, "?")) { r_core_project_list (r, 0); r_cons_flush (); LISTS_FREE (); return 0; } - r_config_set (r->config, "prj.name", r_optarg); + r_config_set (r->config, "prj.name", opt.arg); break; case 'P': - patchfile = r_optarg; + patchfile = opt.arg; break; case 'Q': quiet = true; @@ -572,13 +571,13 @@ R_API int r_main_radare2(int argc, char **argv) { break; case 'r': haveRarunProfile = true; - r_config_set (r->config, "dbg.profile", r_optarg); + r_config_set (r->config, "dbg.profile", opt.arg); break; case 'R': - customRarunProfile = r_str_appendf (customRarunProfile, "%s\n", r_optarg); + customRarunProfile = r_str_appendf (customRarunProfile, "%s\n", opt.arg); break; case 's': - s_seek = r_optarg; + s_seek = opt.arg; break; case 'S': sandbox = true; @@ -670,7 +669,7 @@ R_API int r_main_radare2(int argc, char **argv) { pfile = NULL; //strdup (""); } } else { - pfile = argv[r_optind] ? strdup (argv[r_optind]) : NULL; + pfile = argv[opt.ind] ? strdup (argv[opt.ind]) : NULL; } } if (do_list_io_plugins) { @@ -708,12 +707,12 @@ R_API int r_main_radare2(int argc, char **argv) { free (tfn); } if (debug == 1) { - if (r_optind >= argc && !haveRarunProfile) { + if (opt.ind >= argc && !haveRarunProfile) { eprintf ("Missing argument for -d\n"); LISTS_FREE (); return 1; } - const char *src = haveRarunProfile? pfile: argv[r_optind]; + const char *src = haveRarunProfile? pfile: argv[opt.ind]; if (src && *src) { char *uri = strdup (src); if (uri) { @@ -754,8 +753,8 @@ R_API int r_main_radare2(int argc, char **argv) { } if (do_connect) { - const char *uri = argv[r_optind]; - if (r_optind >= argc) { + const char *uri = argv[opt.ind]; + if (opt.ind >= argc) { eprintf ("Missing URI for -C\n"); LISTS_FREE (); return 1; @@ -763,7 +762,7 @@ R_API int r_main_radare2(int argc, char **argv) { if (strstr (uri, "://")) { r_core_cmdf (r, "=+%s", uri); } else { - r_core_cmdf (r, "=+http://%s/cmd/", argv[r_optind]); + r_core_cmdf (r, "=+http://%s/cmd/", argv[opt.ind]); } r_core_cmd0 (r, "=!="); //LISTS_FREE (); @@ -817,13 +816,13 @@ R_API int r_main_radare2(int argc, char **argv) { free (pfile); return 1; } - if (r_sys_chdir (argv[r_optind])) { + if (r_sys_chdir (argv[opt.ind])) { eprintf ("[d] Cannot open directory\n"); LISTS_FREE (); free (pfile); return 1; } - } else if (argv[r_optind] && !strcmp (argv[r_optind], "=")) { + } else if (argv[opt.ind] && !strcmp (argv[opt.ind], "=")) { int sz; /* stdin/batch mode */ ut8 *buf = (ut8 *)r_stdin_slurp (&sz); @@ -861,7 +860,7 @@ R_API int r_main_radare2(int argc, char **argv) { LISTS_FREE (); return 1; } - } else if (strcmp (argv[r_optind - 1], "--") && !(r_config_get (r->config, "prj.name") && r_config_get (r->config, "prj.name")[0]) ) { + } else if (strcmp (argv[opt.ind - 1], "--") && !(r_config_get (r->config, "prj.name") && r_config_get (r->config, "prj.name")[0]) ) { if (debug) { if (asmbits) { r_config_set (r->config, "asm.bits", asmbits); @@ -869,7 +868,7 @@ R_API int r_main_radare2(int argc, char **argv) { r_config_set (r->config, "search.in", "dbg.map"); // implicit? r_config_set (r->config, "cfg.debug", "true"); perms = R_PERM_RWX; - if (r_optind >= argc) { + if (opt.ind >= argc) { eprintf ("No program given to -d\n"); LISTS_FREE (); return 1; @@ -879,12 +878,12 @@ R_API int r_main_radare2(int argc, char **argv) { r_config_set (r->config, "dbg.backend", debugbackend); if (strcmp (debugbackend, "native")) { if (!haveRarunProfile) { - pfile = strdup (argv[r_optind++]); + pfile = strdup (argv[opt.ind++]); } perms = R_PERM_RX; // XXX. should work with rw too debug = 2; if (!strstr (pfile, "://")) { - r_optind--; // take filename + opt.ind--; // take filename } #if __WINDOWS__ pfile = r_acp_to_utf8 (pfile); @@ -931,7 +930,7 @@ R_API int r_main_radare2(int argc, char **argv) { } } } else { - const char *f = (haveRarunProfile && pfile)? pfile: argv[r_optind]; + const char *f = (haveRarunProfile && pfile)? pfile: argv[opt.ind]; is_gdb = (!memcmp (f, "gdb://", R_MIN (f? strlen (f):0, 6))); if (!is_gdb) { pfile = strdup ("dbg://"); @@ -968,13 +967,13 @@ R_API int r_main_radare2(int argc, char **argv) { file = pfile; // r_str_append (file, escaped_path); } #endif - r_optind++; - while (r_optind < argc) { - char *escaped_arg = r_str_arg_escape (argv[r_optind]); + opt.ind++; + while (opt.ind < argc) { + char *escaped_arg = r_str_arg_escape (argv[opt.ind]); file = r_str_append (file, " "); file = r_str_append (file, escaped_arg); free (escaped_arg); - r_optind++; + opt.ind++; } pfile = file; } @@ -991,19 +990,19 @@ R_API int r_main_radare2(int argc, char **argv) { if (!debug || debug == 2) { const char *dbg_profile = r_config_get (r->config, "dbg.profile"); - if (r_optind == argc && dbg_profile && *dbg_profile) { + if (opt.ind == argc && dbg_profile && *dbg_profile) { fh = r_core_file_open (r, pfile, perms, mapaddr); if (fh) { r_core_bin_load (r, pfile, baddr); } } - if (r_optind < argc) { + if (opt.ind < argc) { R_FREE (pfile); - while (r_optind < argc) { - pfile = argv[r_optind++]; + while (opt.ind < argc) { + pfile = strdup (argv[opt.ind++]); #if __WINDOWS__ pfile = r_acp_to_utf8 (pfile); -#endif // __WINDOWS__ +#endif fh = r_core_file_open (r, pfile, perms, mapaddr); if (!fh && perms & R_PERM_W) { perms |= R_PERM_CREAT; diff --git a/libr/main/radiff2.c b/libr/main/radiff2.c index c272470784..8e340cca14 100644 --- a/libr/main/radiff2.c +++ b/libr/main/radiff2.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2019 - pancake */ +/* radare - LGPL - Copyright 2009-2020 - pancake */ #include #include @@ -37,8 +37,8 @@ enum { }; static bool zignatures = false; -static char *file = NULL; -static char *file2 = NULL; +static const char *file = NULL; +static const char *file2 = NULL; static ut32 count = 0; static int showcount = 0; static int useva = true; @@ -911,7 +911,7 @@ static void __print_diff_graph(RCore *c, ut64 off, int gmode) { } } -R_API int r_main_radiff2(int argc, char **argv) { +R_API int r_main_radiff2(int argc, const char **argv) { const char *columnSort = NULL; const char *addr = NULL; RCore *c = NULL, *c2 = NULL; @@ -926,22 +926,24 @@ R_API int r_main_radiff2(int argc, char **argv) { double sim = 0.0; evals = r_list_newf (NULL); - while ((o = r_getopt (argc, argv, "Aa:b:BCDe:npg:m:G:OijrhcdsS:uUvVxXt:zqZ")) != -1) { + RGetopt opt; + r_getopt_init (&opt, argc, argv, "Aa:b:BCDe:npg:m:G:OijrhcdsS:uUvVxXt:zqZ"); + while ((o = r_getopt_next (&opt)) != -1) { switch (o) { case 'a': - arch = r_optarg; + arch = opt.arg; break; case 'A': anal_all++; break; case 'b': - bits = atoi (r_optarg); + bits = atoi (opt.arg); break; case 'B': diffmode = 'B'; break; case 'e': - r_list_append (evals, r_optarg); + r_list_append (evals, (void*)opt.arg); break; case 'p': useva = false; @@ -951,10 +953,10 @@ R_API int r_main_radiff2(int argc, char **argv) { break; case 'g': mode = MODE_GRAPH; - addr = r_optarg; + addr = opt.arg; break; case 'm':{ - char *tmp = r_optarg; + const char *tmp = opt.arg; switch(tmp[0]) { case 'i': gmode = GRAPH_INTERACTIVE_MODE; break; case 'k': gmode = GRAPH_SDB_MODE; break; @@ -969,7 +971,7 @@ R_API int r_main_radiff2(int argc, char **argv) { } } break; case 'G': - runcmd = r_optarg; + runcmd = opt.arg; break; case 'c': showcount = 1; @@ -987,8 +989,8 @@ R_API int r_main_radiff2(int argc, char **argv) { diffops = 1; break; case 't': - threshold = atoi (r_optarg); - printf ("%s\n", r_optarg); + threshold = atoi (opt.arg); + printf ("%s\n", opt.arg); break; case 'd': delta = 1; @@ -1014,7 +1016,7 @@ R_API int r_main_radiff2(int argc, char **argv) { } break; case 'S': - columnSort = r_optarg; + columnSort = opt.arg; break; case 'x': mode = MODE_COLS; @@ -1050,11 +1052,11 @@ R_API int r_main_radiff2(int argc, char **argv) { } } - if (argc < 3 || r_optind + 2 > argc) { + if (argc < 3 || opt.ind + 2 > argc) { return show_help (0); } - file = (r_optind < argc)? argv[r_optind]: NULL; - file2 = (r_optind + 1 < argc)? argv[r_optind + 1]: NULL; + file = (opt.ind < argc)? argv[opt.ind]: NULL; + file2 = (opt.ind + 1 < argc)? argv[opt.ind + 1]: NULL; switch (mode) { case MODE_GRAPH: diff --git a/libr/main/rafind2.c b/libr/main/rafind2.c index 2e1ce3deb1..539ceeba58 100644 --- a/libr/main/rafind2.c +++ b/libr/main/rafind2.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2019 - pancake */ +/* radare - LGPL - Copyright 2009-2020 - pancake */ #include #include @@ -12,11 +12,12 @@ #include #include +// XXX kill those globals static int showstr = 0; static int rad = 0; static int align = 0; static ut64 from = 0LL, to = -1; -static char *mask = NULL; +static const char *mask = NULL; static int nonstop = 0; static bool identify = false; static bool quiet = false; @@ -117,7 +118,7 @@ static int hit(RSearchKeyword *kw, void *user, ut64 addr) { return 1; } -static int show_help(char *argv0, int line) { +static int show_help(const char *argv0, int line) { printf ("Usage: %s [-mXnzZhqv] [-a align] [-b sz] [-f/t from/to] [-[e|s|S] str] [-x hex] -|file|dir ..\n", argv0); if (line) { return 0; @@ -324,14 +325,16 @@ static int rafind_open_dir(const char *dir) { return 0; } -R_API int r_main_rafind2(int argc, char **argv) { +R_API int r_main_rafind2(int argc, const char **argv) { int c; - keywords = r_list_new (); - while ((c = r_getopt (argc, argv, "a:ie:b:jmM:s:S:x:Xzf:t:E:rqnhvZ")) != -1) { + keywords = r_list_newf (NULL); + RGetopt opt; + r_getopt_init (&opt, argc, argv, "a:ie:b:jmM:s:S:x:Xzf:t:E:rqnhvZ"); + while ((c = r_getopt_next (&opt)) != -1) { switch (c) { case 'a': - align = r_num_math (NULL, r_optarg); + align = r_num_math (NULL, opt.arg); break; case 'r': rad = 1; @@ -351,42 +354,42 @@ R_API int r_main_rafind2(int argc, char **argv) { case 'e': mode = R_SEARCH_REGEXP; hexstr = 0; - r_list_append (keywords, r_optarg); + r_list_append (keywords, (void*)opt.arg); break; case 'E': mode = R_SEARCH_ESIL; - r_list_append (keywords, r_optarg); + r_list_append (keywords, (void*)opt.arg); break; case 's': mode = R_SEARCH_KEYWORD; hexstr = 0; widestr = 0; - r_list_append (keywords, r_optarg); + r_list_append (keywords, (void*)opt.arg); break; case 'S': mode = R_SEARCH_KEYWORD; hexstr = 0; widestr = 1; - r_list_append (keywords, r_optarg); + r_list_append (keywords, (void*)opt.arg); break; case 'b': - bsize = r_num_math (NULL, r_optarg); + bsize = r_num_math (NULL, opt.arg); break; case 'x': mode = R_SEARCH_KEYWORD; hexstr = 1; widestr = 0; - r_list_append (keywords, r_optarg); + r_list_append (keywords, (void*)opt.arg); break; case 'M': // XXX should be from hexbin - mask = r_optarg; + mask = opt.arg; break; case 'f': - from = r_num_math (NULL, r_optarg); + from = r_num_math (NULL, opt.arg); break; case 't': - to = r_num_math (NULL, r_optarg); + to = r_num_math (NULL, opt.arg); break; case 'X': pr = r_print_new (); @@ -408,18 +411,18 @@ R_API int r_main_rafind2(int argc, char **argv) { return show_help (argv[0], 1); } } - if (r_optind == argc) { + if (opt.ind == argc) { return show_help (argv[0], 1); } /* Enable quiet mode if searching just a single file */ - if (r_optind + 1 == argc && !r_file_is_directory (argv[r_optind])) { + if (opt.ind + 1 == argc && !r_file_is_directory (argv[opt.ind])) { quiet = true; } if (json) { printf ("["); } - for (; r_optind < argc; r_optind++) { - rafind_open (argv[r_optind]); + for (; opt.ind < argc; opt.ind++) { + rafind_open (argv[opt.ind]); } if (json) { printf ("]\n"); diff --git a/libr/main/ragg2.c b/libr/main/ragg2.c index e0e01e576b..d3bd658c26 100644 --- a/libr/main/ragg2.c +++ b/libr/main/ragg2.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2011-2019 - pancake */ +/* radare - LGPL - Copyright 2011-2020 - pancake */ #include #include @@ -113,7 +113,7 @@ static int openfile(const char *f, int x) { } #define ISEXEC (fmt!='r') -R_API int r_main_ragg2(int argc, char **argv) { +R_API int r_main_ragg2(int argc, const char **argv) { const char *file = NULL; const char *padding = NULL; const char *pattern = NULL; @@ -122,7 +122,7 @@ R_API int r_main_ragg2(int argc, char **argv) { const char *contents = NULL; const char *arch = R_SYS_ARCH; const char *os = R_EGG_OS_NAME; - char *format = "raw"; + const char *format = "raw"; bool show_execute = false; bool show_execute_rop = false; int show_hex = 1; @@ -131,8 +131,8 @@ R_API int r_main_ragg2(int argc, char **argv) { int append = 0; int show_str = 0; ut64 get_offset = 0; - char *shellcode = NULL; - char *encoder = NULL; + const char *shellcode = NULL; + const char *encoder = NULL; char *sequence = NULL; int bits = (R_SYS_BITS & R_SYS_BITS_64)? 64: 32; int fmt = 0; @@ -142,37 +142,39 @@ R_API int r_main_ragg2(int argc, char **argv) { int c, i; REgg *egg = r_egg_new (); - while ((c = r_getopt (argc, argv, "n:N:he:a:b:f:o:sxXrk:FOI:Li:c:p:P:B:C:vd:D:w:zq:S:")) != -1) { + RGetopt opt; + r_getopt_init (&opt, argc, argv, "n:N:he:a:b:f:o:sxXrk:FOI:Li:c:p:P:B:C:vd:D:w:zq:S:"); + while ((c = r_getopt_next (&opt)) != -1) { switch (c) { case 'a': - arch = r_optarg; + arch = opt.arg; if (!strcmp (arch, "trace")) { show_asm = 1; show_hex = 0; } break; case 'e': - encoder = r_optarg; + encoder = opt.arg; break; case 'b': - bits = atoi (r_optarg); + bits = atoi (opt.arg); break; case 'B': - bytes = r_str_append (bytes, r_optarg); + bytes = r_str_append (bytes, opt.arg); break; case 'C': - contents = r_optarg; + contents = opt.arg; break; case 'w': { - char *arg = strdup (r_optarg); + char *arg = strdup (opt.arg); char *p = strchr (arg, ':'); if (p) { int len, off; ut8 *b; *p++ = 0; off = r_num_math (NULL, arg); - b = malloc (strlen (r_optarg) + 1); + b = malloc (strlen (opt.arg) + 1); len = r_hex_str2bin (p, b); if (len > 0) { r_egg_patch (egg, off, (const ut8*)b, len); @@ -188,14 +190,14 @@ R_API int r_main_ragg2(int argc, char **argv) { break; case 'n': { - ut32 n = r_num_math (NULL, r_optarg); + ut32 n = r_num_math (NULL, opt.arg); append = 1; r_egg_patch (egg, -1, (const ut8*)&n, 4); } break; case 'N': { - ut64 n = r_num_math (NULL, r_optarg); + ut64 n = r_num_math (NULL, opt.arg); r_egg_patch (egg, -1, (const ut8*)&n, 8); append = 1; } @@ -203,10 +205,10 @@ R_API int r_main_ragg2(int argc, char **argv) { case 'd': { ut32 off, n; - char *p = strchr (r_optarg, ':'); + char *p = strchr (opt.arg, ':'); if (p) { *p = 0; - off = r_num_math (NULL, r_optarg); + off = r_num_math (NULL, opt.arg); n = r_num_math (NULL, p + 1); *p = ':'; // TODO: honor endianness here @@ -218,9 +220,9 @@ R_API int r_main_ragg2(int argc, char **argv) { break; case 'D': { - char *p = strchr (r_optarg, ':'); + char *p = strchr (opt.arg, ':'); if (p) { - ut64 n, off = r_num_math (NULL, r_optarg); + ut64 n, off = r_num_math (NULL, opt.arg); n = r_num_math (NULL, p + 1); // TODO: honor endianness here r_egg_patch (egg, off, (const ut8*)&n, 8); @@ -230,34 +232,34 @@ R_API int r_main_ragg2(int argc, char **argv) { } break; case 'S': - str = r_optarg; + str = opt.arg; break; case 'o': - ofile = r_optarg; + ofile = opt.arg; break; case 'O': ofileauto = 1; break; case 'I': - r_egg_lang_include_path (egg, r_optarg); + r_egg_lang_include_path (egg, opt.arg); break; case 'i': - shellcode = r_optarg; - break; + shellcode = opt.arg; + break; case 'p': - padding = r_optarg; + padding = opt.arg; break; case 'P': - pattern = r_optarg; + pattern = opt.arg; break; case 'c': { - char *p = strchr (r_optarg, '='); + char *p = strchr (opt.arg, '='); if (p) { *p++ = 0; - r_egg_option_set (egg, r_optarg, p); + r_egg_option_set (egg, opt.arg, p); } else { - r_egg_option_set (egg, r_optarg, "true"); + r_egg_option_set (egg, opt.arg, "true"); } } break; @@ -272,7 +274,7 @@ R_API int r_main_ragg2(int argc, char **argv) { show_asm = 0; break; case 'f': - format = r_optarg; + format = opt.arg; show_asm = 0; break; case 's': @@ -280,7 +282,7 @@ R_API int r_main_ragg2(int argc, char **argv) { show_hex = 0; break; case 'k': - os = r_optarg; + os = opt.arg; break; case 'r': show_raw = 1; @@ -312,7 +314,7 @@ R_API int r_main_ragg2(int argc, char **argv) { break; case 'q': get_offset = 1; - sequence = strdup (r_optarg); + sequence = strdup (opt.arg); break; default: free (sequence); @@ -321,12 +323,12 @@ R_API int r_main_ragg2(int argc, char **argv) { } } - if (r_optind == argc && !shellcode && !bytes && !contents && !encoder && !padding && !pattern && !append && !get_offset && !str) { + if (opt.ind == argc && !shellcode && !bytes && !contents && !encoder && !padding && !pattern && !append && !get_offset && !str) { free (sequence); r_egg_free (egg); return usage (0); } else { - file = argv[r_optind]; + file = argv[opt.ind]; } if (bits == 64) { @@ -463,7 +465,7 @@ R_API int r_main_ragg2(int argc, char **argv) { int fd; if (file) { char *o, *q, *p = strdup (file); - if ( (o = strchr (p, '.')) ) { + if ((o = strchr (p, '.'))) { while ( (q = strchr (o + 1, '.')) ) { o = q; } @@ -477,7 +479,7 @@ R_API int r_main_ragg2(int argc, char **argv) { fd = openfile ("a.out", ISEXEC); } if (fd == -1) { - eprintf ("cannot open file '%s'\n", r_optarg); + eprintf ("cannot open file '%s'\n", opt.arg); goto fail; } } diff --git a/libr/main/rahash2.c b/libr/main/rahash2.c index 049c759902..96de69763b 100644 --- a/libr/main/rahash2.c +++ b/libr/main/rahash2.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2019 - pancake */ +/* radare - LGPL - Copyright 2009-2020 - pancake */ #include #include @@ -320,12 +320,12 @@ static void algolist() { } #define setHashString(x, y) {\ - if (hashstr) {\ - eprintf ("Hashstring already defined\n");\ - return 1;\ - }\ - hashstr_hex = y;\ - hashstr = x;\ + if (hashstr) {\ + eprintf ("Hashstring already defined\n");\ + return 1;\ + }\ + hashstr_hex = y;\ + hashstr = (char*)x;\ } static bool is_power_of_two(const ut64 x) { @@ -370,7 +370,7 @@ static int encrypt_or_decrypt(const char *algo, int direction, const char *hashs return 1; } -static int encrypt_or_decrypt_file(const char *algo, int direction, char *filename, const ut8 *iv, int ivlen, int mode) { +static int encrypt_or_decrypt_file(const char *algo, int direction, const char *filename, const ut8 *iv, int ivlen, int mode) { bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo) || !strcmp ("punycode", algo); // TODO: generalise this for all non key encoding/decoding. if (no_key_mode || s.len > 0) { RCrypto *cry = r_crypto_new (); @@ -420,7 +420,7 @@ static int encrypt_or_decrypt_file(const char *algo, int direction, char *filena return 1; } -int r_main_rahash2(int argc, char **argv) { +R_API int r_main_rahash2(int argc, const char **argv) { ut64 i; int ret, c, rad = 0, bsize = 0, numblocks = 0, ule = 0; const char *algo = "sha256"; /* default hashing algorithm */ @@ -430,7 +430,7 @@ int r_main_rahash2(int argc, char **argv) { char *hashstr = NULL; ut8 *iv = NULL; int ivlen = -1; - char *ivseed = NULL; + const char *ivseed = NULL; const char *compareStr = NULL; const char *ptype = NULL; ut8 *compareBin = NULL; @@ -441,37 +441,39 @@ int r_main_rahash2(int argc, char **argv) { RHash *ctx; RIO *io; - while ((c = r_getopt (argc, argv, "p:jD:rveE:a:i:I:S:s:x:b:nBhf:t:kLqc:")) != -1) { + RGetopt opt; + r_getopt_init (&opt, argc, argv, "p:jD:rveE:a:i:I:S:s:x:b:nBhf:t:kLqc:"); + while ((c = r_getopt_next (&opt)) != -1) { switch (c) { case 'q': quiet++; break; case 'i': - iterations = atoi (r_optarg); + iterations = atoi (opt.arg); if (iterations < 0) { eprintf ("error: -i argument must be positive\n"); return 1; } break; case 'j': rad = 'j'; break; - case 'S': seed = r_optarg; break; - case 'I': ivseed = r_optarg; break; + case 'S': seed = opt.arg; break; + case 'I': ivseed = opt.arg; break; case 'n': numblocks = 1; break; - case 'D': decrypt = r_optarg; break; - case 'E': encrypt = r_optarg; break; + case 'D': decrypt = opt.arg; break; + case 'E': encrypt = opt.arg; break; case 'L': algolist (); return 0; case 'e': ule = 1; break; case 'r': rad = 1; break; case 'k': rad = 2; break; - case 'p': ptype = r_optarg; break; - case 'a': algo = r_optarg; break; + case 'p': ptype = opt.arg; break; + case 'a': algo = opt.arg; break; case 'B': incremental = false; break; - case 'b': bsize = (int) r_num_math (NULL, r_optarg); break; - case 'f': from = r_num_math (NULL, r_optarg); break; - case 't': to = 1 + r_num_math (NULL, r_optarg); break; + case 'b': bsize = (int) r_num_math (NULL, opt.arg); break; + case 'f': from = r_num_math (NULL, opt.arg); break; + case 't': to = 1 + r_num_math (NULL, opt.arg); break; case 'v': return r_main_version_print ("rahash2"); case 'h': return do_help (0); - case 's': setHashString (r_optarg, 0); break; - case 'x': setHashString (r_optarg, 1); break; - case 'c': compareStr = r_optarg; break; + case 's': setHashString (opt.arg, 0); break; + case 'x': setHashString (opt.arg, 1); break; + case 'c': compareStr = opt.arg; break; default: return do_help (0); } } @@ -532,7 +534,7 @@ int r_main_rahash2(int argc, char **argv) { // TODO: support p=%s (horizontal bars) // TODO: list supported statistical metrics // TODO: support -f and -t - for (i = r_optind; i < argc; i++) { + for (i = opt.ind; i < argc; i++) { printf ("%s:\n", argv[i]); r_sys_cmdf ("r2 -qfnc \"p==%s 100\" \"%s\"", ptype, argv[i]); } @@ -655,7 +657,7 @@ int r_main_rahash2(int argc, char **argv) { return ret; } } - if (r_optind >= argc) { + if (opt.ind >= argc) { free (iv); return do_help (1); } @@ -668,7 +670,7 @@ int r_main_rahash2(int argc, char **argv) { } io = r_io_new (); - for (ret = 0, i = r_optind; i < argc; i++) { + for (ret = 0, i = opt.ind; i < argc; i++) { if (encrypt) {// for encrytion when files are provided int rt = encrypt_or_decrypt_file (encrypt, 0, argv[i], iv, ivlen, 0); if (rt == -1) { diff --git a/libr/main/rarun2.c b/libr/main/rarun2.c index 5ab1f4a53f..f4c7e2aaa1 100644 --- a/libr/main/rarun2.c +++ b/libr/main/rarun2.c @@ -1,4 +1,4 @@ -/* radare2 - Copyleft 2011-2019 - pancake */ +/* radare2 - Copyleft 2011-2020 - pancake */ #include #include @@ -12,8 +12,8 @@ static void fwd(int sig) { static void rarun2_tty() { /* TODO: Implement in native code */ r_sys_cmd ("tty"); - close(1); - dup2(2, 1); + close (1); + dup2 (2, 1); r_sys_signal (SIGINT, fwd); for (;;) { sleep (1); @@ -21,8 +21,7 @@ static void rarun2_tty() { } #endif -R_API int r_main_rarun2(int argc, char **argv) { - char *file; +R_API int r_main_rarun2(int argc, const char **argv) { RRunProfile *p; int i, ret; if (argc == 1 || !strcmp (argv[1], "-h")) { @@ -33,7 +32,7 @@ R_API int r_main_rarun2(int argc, char **argv) { if (!strcmp (argv[1], "-v")) { return r_main_version_print ("rarun2"); } - file = argv[1]; + const char *file = argv[1]; if (!strcmp (file, "-t")) { #if __UNIX__ rarun2_tty (); diff --git a/libr/main/rasm2.c b/libr/main/rasm2.c index abab3f6e6c..a27e2bfc61 100644 --- a/libr/main/rasm2.c +++ b/libr/main/rasm2.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2019 - pancake, nibble, maijin */ +/* radare - LGPL - Copyright 2009-2020 - pancake, nibble, maijin */ #include #include @@ -24,7 +24,6 @@ static void __load_plugins(RAsmState *as); static void __as_set_archbits(RAsmState *as) { r_asm_use (as->a, R_SYS_ARCH); r_anal_use (as->anal, R_SYS_ARCH); - int sysbits = (R_SYS_BITS & R_SYS_BITS_64)? 64: 32; r_asm_set_bits (as->a, sysbits); r_anal_set_bits (as->anal, sysbits); @@ -32,14 +31,13 @@ static void __as_set_archbits(RAsmState *as) { static RAsmState *__as_new() { RAsmState *as = R_NEW0 (RAsmState); - if (!as) { - return NULL; + if (as) { + as->l = r_lib_new (NULL, NULL); + as->a = r_asm_new (); + as->anal = r_anal_new (); + __load_plugins (as); + __as_set_archbits (as); } - as->l = r_lib_new (NULL, NULL); - as->a = r_asm_new (); - as->anal = r_anal_new (); - __load_plugins (as); - __as_set_archbits (as); return as; } @@ -61,7 +59,6 @@ static char *stackop2str(int type) { return strdup ("unknown"); } -// TODO: use pj static int showanal(RAsmState *as, RAnalOp *op, ut64 offset, ut8 *buf, int len, PJ *pj) { int ret = r_anal_op (as->anal, op, offset, buf, len, R_ANAL_OP_MASK_ESIL); if (ret < 1) { @@ -480,8 +477,7 @@ static int __lib_anal_cb(RLibPlugin *pl, void *user, void *data) { } static int print_assembly_output(RAsmState *as, const char *buf, ut64 offset, ut64 len, int bits, - int bin, bool use_spp, bool rad, bool hexwords, char *arch) { - int ret = 0; + int bin, bool use_spp, bool rad, bool hexwords, const char *arch) { if (rad) { printf ("e asm.arch=%s\n", arch? arch: R_SYS_ARCH); printf ("e asm.bits=%d\n", bits); @@ -490,7 +486,7 @@ static int print_assembly_output(RAsmState *as, const char *buf, ut64 offset, ut } printf ("wx "); } - ret = rasm_asm (as, (char *)buf, offset, len, as->a->bits, bin, use_spp, hexwords); + int ret = rasm_asm (as, (char *)buf, offset, len, as->a->bits, bin, use_spp, hexwords); if (rad) { printf ("f entry = $$\n"); printf ("f label.main = $$ + 1\n"); @@ -533,10 +529,14 @@ static void __load_plugins(RAsmState *as) { free (path); } -R_API int r_main_rasm2(int argc, char *argv[]) { +R_API int r_main_rasm2(int argc, const char *argv[]) { const char *env_arch = r_sys_getenv ("RASM2_ARCH"); const char *env_bits = r_sys_getenv ("RASM2_BITS"); - char *arch = NULL, *file = NULL, *filters = NULL, *kernel = NULL, *cpu = NULL; + const char *arch = NULL; + const char *cpu = NULL; + const char *kernel = NULL; + const char *filters = NULL; + const char *file = NULL; bool isbig = false; bool rad = false; bool use_spp = false; @@ -565,22 +565,24 @@ R_API int r_main_rasm2(int argc, char *argv[]) { free (r2bits); } - while ((c = r_getopt (argc, argv, "a:Ab:Bc:CdDeEf:F:hi:jk:l:L@:o:O:pqrs:vwx")) != -1) { + RGetopt opt; + r_getopt_init (&opt, argc, argv, "a:Ab:Bc:CdDeEf:F:hi:jk:l:L@:o:O:pqrs:vwx"); + while ((c = r_getopt_next (&opt)) != -1) { switch (c) { case 'a': - arch = r_optarg; + arch = opt.arg; break; case 'A': analinfo = true; break; case 'b': - bits = r_num_math (NULL, r_optarg); + bits = r_num_math (NULL, opt.arg); break; case 'B': bin = 1; break; case 'c': - cpu = r_optarg; + cpu = opt.arg; break; case 'C': as->coutput = true; @@ -598,35 +600,35 @@ R_API int r_main_rasm2(int argc, char *argv[]) { dis = 3; break; case 'f': - file = r_optarg; + file = opt.arg; break; case 'F': - filters = r_optarg; + filters = opt.arg; break; case 'h': help++; case 'i': - skip = r_num_math (NULL, r_optarg); + skip = r_num_math (NULL, opt.arg); break; case 'j': as->json = true; break; case 'k': - kernel = r_optarg; + kernel = opt.arg; break; case 'l': - len = r_num_math (NULL, r_optarg); + len = r_num_math (NULL, opt.arg); break; case 'L': - rasm2_list (as, argv[r_optind]); + rasm2_list (as, argv[opt.ind]); ret = 1; goto beach; case '@': case 'o': - offset = r_num_math (NULL, r_optarg); + offset = r_num_math (NULL, opt.arg); break; case 'O': - fd = open (r_optarg, O_TRUNC | O_RDWR | O_CREAT, 0644); + fd = open (opt.arg, O_TRUNC | O_RDWR | O_CREAT, 0644); if (fd != -1) { dup2 (fd, 1); } @@ -641,12 +643,12 @@ R_API int r_main_rasm2(int argc, char *argv[]) { rad = true; break; case 's': - if (*r_optarg == '?') { + if (*opt.arg == '?') { printf ("att\nintel\nmasm\njz\nregnum\n"); __as_free (as); return 0; } else { - int syntax = r_asm_syntax_from_string (r_optarg); + int syntax = r_asm_syntax_from_string (opt.arg); if (syntax == -1) { __as_free (as); return 1; @@ -712,7 +714,7 @@ R_API int r_main_rasm2(int argc, char *argv[]) { r_anal_set_big_endian (as->anal, canbebig); } if (whatsop) { - const char *s = r_asm_describe (as->a, argv[r_optind]); + const char *s = r_asm_describe (as->a, argv[opt.ind]); ret = 1; if (s) { printf ("%s\n", s); @@ -801,8 +803,8 @@ R_API int r_main_rasm2(int argc, char *argv[]) { ret = 1; } } - } else if (argv[r_optind]) { - if (!strcmp (argv[r_optind], "-")) { + } else if (argv[opt.ind]) { + if (!strcmp (argv[opt.ind], "-")) { int length; do { char buf[1024]; // TODO: use(implement) r_stdin_line() or so @@ -846,7 +848,7 @@ R_API int r_main_rasm2(int argc, char *argv[]) { goto beach; } if (dis) { - char *usrstr = argv[r_optind]; + char *usrstr = strdup (argv[opt.ind]); len = strlen (usrstr); if (skip && len > skip) { skip *= 2; @@ -855,8 +857,9 @@ R_API int r_main_rasm2(int argc, char *argv[]) { len -= skip; usrstr[len] = 0; } + // XXX this is a wrong usage of endianness if (!strncmp (usrstr, "0x", 2)) { - usrstr += 2; + memmove (usrstr, usrstr + 2, strlen (usrstr) + 1); } if (rad) { as->oneliner = true; @@ -866,10 +869,11 @@ R_API int r_main_rasm2(int argc, char *argv[]) { } ret = rasm_disasm (as, (char *)usrstr, offset, len, as->a->bits, ascii, bin, dis - 1); + free (usrstr); } else if (analinfo) { - ret = show_analinfo (as, (const char *)argv[r_optind], offset); + ret = show_analinfo (as, (const char *)argv[opt.ind], offset); } else { - ret = print_assembly_output (as, argv[r_optind], offset, len, as->a->bits, + ret = print_assembly_output (as, argv[opt.ind], offset, len, as->a->bits, bin, use_spp, rad, hexwords, arch); } if (!ret) { diff --git a/libr/main/rax2.c b/libr/main/rax2.c index 1a6b374e85..48fc325209 100644 --- a/libr/main/rax2.c +++ b/libr/main/rax2.c @@ -605,7 +605,7 @@ dotherax: return true; } -R_API int r_main_rax2(int argc, char **argv) { +R_API int r_main_rax2(int argc, const char **argv) { int i, fm = 0; RNum *num = r_num_new (NULL, NULL, NULL); if (argc == 1) { @@ -613,8 +613,9 @@ R_API int r_main_rax2(int argc, char **argv) { } else { ut64 flags = 0; for (i = 1; i < argc; i++) { - r_str_unescape (argv[i]); - rax (num, argv[i], 0, i == argc - 1, &flags, &fm); + char *argv_i = strdup (argv[i]); + r_str_unescape (argv_i); + rax (num, argv_i, 0, i == argc - 1, &flags, &fm); } } r_num_free (num); diff --git a/libr/main/version.c b/libr/main/version.c index 40aee902be..abccf692e4 100644 --- a/libr/main/version.c +++ b/libr/main/version.c @@ -1,4 +1,4 @@ -/* copyright 2015-2019 radare2 by pancake */ +/* copyright 2015-2020 radare2 by pancake */ #include #include diff --git a/libr/socket/run.c b/libr/socket/run.c index 20d62257c2..bdea82d32c 100644 --- a/libr/socket/run.c +++ b/libr/socket/run.c @@ -439,7 +439,7 @@ R_API int r_run_parsefile(RRunProfile *p, const char *b) { return 0; } -R_API bool r_run_parseline(RRunProfile *p, char *b) { +R_API bool r_run_parseline(RRunProfile *p, const char *b) { int must_free = false; char *e = strchr (b, '='); if (!e || *b == '#') { diff --git a/libr/util/getopt.c b/libr/util/getopt.c index d6082ac973..45a2747cca 100644 --- a/libr/util/getopt.c +++ b/libr/util/getopt.c @@ -1,122 +1,84 @@ -/* radare - MIT - Copyright 2019 - pancake */ - -#define GETOPT_C -#include - -#if __WINDOWS__ - +/* radare - MIT - Copyright 2019-2020 - pancake */ /* * Copyright (c) 1987, 1993, 1994 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * The Regents of the University of California. All rights reserved. + * $Id: getopt.c,v 1.2 1998/01/21 22:27:05 billm Exp $ * */ -#if defined(LIBC_SCCS) && !defined(lint) -/* static char sccsid[] = "from: @(#)getopt.c 8.2 (Berkeley) 4/2/94"; */ -static char *rcsid = "$Id: getopt.c,v 1.2 1998/01/21 22:27:05 billm Exp $"; -#endif /* LIBC_SCCS and not lint */ - -#include -#include -#include - -R_API int r_opterr = 1; /* if error message should be printed */ -R_API int r_optind = 1; /* index into parent argv vector */ -R_API int r_optopt; /* character checked for validity */ -R_API int r_optreset; /* reset getopt */ -R_API char *r_optarg = NULL; /* argument associated with option */ +#include #define BADCH (int)'?' #define BADARG (int)':' #define EMSG "" -/* - * getopt -- - * Parse argc/argv argument vector. - */ -R_API int r_getopt(int nargc, char * const *nargv, const char *ostr) { - static char *place = EMSG; /* option letter processing */ - char *oli; /* option letter list index */ +R_API void r_getopt_init(RGetopt *opt, int argc, const char **argv, const char *ostr) { + memset (opt, 0, sizeof (RGetopt)); + opt->err = 1; + opt->ind = 1; + opt->opt = 0; + opt->reset = 0; + opt->arg = NULL; + opt->argc = argc; + opt->argv = argv; + opt->ostr = ostr; +} - if (r_optreset || !*place) { /* update scanning pointer */ - r_optreset = 0; - if (r_optind >= nargc || *(place = nargv[r_optind]) != '-') { +R_API int r_getopt_next(RGetopt *opt) { + static const char *place = EMSG; // option letter processing + const char *oli; // option letter list index + + if (opt->reset || !*place) { // update scanning pointer + opt->reset = 0; + if (opt->ind >= opt->argc || *(place = opt->argv[opt->ind]) != '-') { place = EMSG; return -1; } - if (place[1] && *++place == '-') { /* found "--" */ - r_optind++; + if (place[1] && *++place == '-') { // found "--" + opt->ind++; place = EMSG; return -1; } } /* option letter okay? */ - if ((r_optopt = (int)*place++) == (int)':' || !(oli = strchr (ostr, r_optopt))) { + if ((opt->opt = (int)*place++) == (int)':' || !(oli = strchr (opt->ostr, opt->opt))) { /* * if the user didn't specify '-' as an option, * assume it means -1. */ - if (r_optopt == (int)'-') { + if (opt->opt == '-') { return -1; } if (!*place) { - r_optind++; + opt->ind++; } - if (r_opterr && *ostr != ':') { - (void)eprintf("%s: illegal option -- %c\n", nargv[0], r_optopt); + if (opt->err && *opt->ostr != ':') { + (void)eprintf ("%s: illegal option -- %c\n", opt->argv[0], opt->opt); } return BADCH; } - if (*++oli != ':') { /* don't need argument */ - r_optarg = NULL; - if (!*place) { - r_optind++; - } - } else { /* need an argument */ + if (*++oli == ':') { /* need argument */ if (*place) { /* no white space */ - r_optarg = place; - } else if (nargc <= ++r_optind) { /* no arg */ + opt->arg = place; + } else if (opt->argc <= ++opt->ind) { /* no arg */ place = EMSG; - if (*ostr == ':') { + if (*opt->ostr == ':') { return BADARG; } - if (r_opterr) { - (void)eprintf("%s: option requires an argument -- %c\n", nargv[0], r_optopt); + if (opt->err) { + (void)eprintf ("%s: option requires an argument -- %c\n", opt->argv[0], opt->opt); } return BADCH; } else { /* white space */ - r_optarg = nargv[r_optind]; + opt->arg = opt->argv[opt->ind]; } place = EMSG; - r_optind++; + opt->ind++; + } else { + opt->arg = NULL; + if (!*place) { + opt->ind++; + } } - return r_optopt; /* dump back option letter */ + // dump back option letter + return opt->opt; } -#endif