From 57c68c752c42d1551fa6ea601be54e90670f03c6 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 8 Apr 2020 13:56:52 +0200 Subject: [PATCH] Fix #16389 - r2r -qv and r2 -v to show version and quiet versions (#16472) ##tools --- binr/r2r/r2r.c | 17 ++++++++++++++--- libr/include/r_util/r_str.h | 2 ++ libr/main/Makefile | 1 - libr/main/main.c | 7 +++++++ libr/main/meson.build | 3 +-- libr/main/version.c | 30 ------------------------------ libr/util/str.c | 29 +++++++++++++++++++++++++++++ man/r2r.1 | 3 +++ 8 files changed, 56 insertions(+), 36 deletions(-) delete mode 100644 libr/main/version.c diff --git a/binr/r2r/r2r.c b/binr/r2r/r2r.c index 5ad888e0e6..a1014055a6 100644 --- a/binr/r2r/r2r.c +++ b/binr/r2r/r2r.c @@ -36,11 +36,12 @@ static void print_state(R2RState *state, ut64 prev_completed); static void print_log(R2RState *state, ut64 prev_completed, ut64 prev_paths_completed); static int help(bool verbose) { - printf ("Usage: r2r [-vh] [-j threads] [test file/dir | @test-type]\n"); + printf ("Usage: r2r [-qvVnL] [-j threads] [test file/dir | @test-type]\n"); if (verbose) { printf ( " -h print this help\n" " -v show version\n" + " -q quiet\n" " -V verbose\n" " -n do nothing (don't run any test, just load/parse them)\n" " -L log mode (better printing for CI, logfiles, etc.)" @@ -133,6 +134,7 @@ int main(int argc, char **argv) { int workers_count = WORKERS_DEFAULT; bool verbose = false; bool nothing = false; + bool quiet = false; bool log_mode = false; char *radare2_cmd = NULL; char *rasm2_cmd = NULL; @@ -143,7 +145,7 @@ int main(int argc, char **argv) { int ret = 0; RGetopt opt; - r_getopt_init (&opt, argc, (const char **)argv, "hvj:r:m:f:C:LnVt:F:"); + r_getopt_init (&opt, argc, (const char **)argv, "hqvj:r:m:f:C:LnVt:F:"); int c; while ((c = r_getopt_next (&opt)) != -1) { @@ -151,8 +153,17 @@ int main(int argc, char **argv) { case 'h': ret = help (true); goto beach; + case 'q': + quiet = true; + break; case 'v': - printf (R2_VERSION "\n"); + if (quiet) { + printf (R2_VERSION "\n"); + } else { + char *s = r_str_version ("r2r"); + printf ("%s\n", s); + free (s); + } return 0; case 'V': verbose = true; diff --git a/libr/include/r_util/r_str.h b/libr/include/r_util/r_str.h index c84cf4210c..97bdebf2b4 100644 --- a/libr/include/r_util/r_str.h +++ b/libr/include/r_util/r_str.h @@ -196,6 +196,8 @@ R_API char *r_str_list_join(RList *str, const char *sep); R_API const char *r_str_sep(const char *base, const char *sep); R_API const char *r_str_rsep(const char *base, const char *p, const char *sep); R_API char *r_str_donut(int size); +R_API char *r_str_version(const char *program); + #ifdef __cplusplus } #endif diff --git a/libr/main/Makefile b/libr/main/Makefile index 8d784df9ef..1e4622c9a4 100644 --- a/libr/main/Makefile +++ b/libr/main/Makefile @@ -2,7 +2,6 @@ include ../config.mk NAME=r_main -OBJS=version.o OBJS+=main.o OBJS+=rax2.o OBJS+=rasm2.o diff --git a/libr/main/main.c b/libr/main/main.c index c56cc2c3de..9e35569b67 100644 --- a/libr/main/main.c +++ b/libr/main/main.c @@ -44,3 +44,10 @@ 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); } + +R_API int r_main_version_print(const char *progname) { + char *s = r_str_version (progname); + printf ("%s\n", s); + free (s); + return 0; +} diff --git a/libr/main/meson.build b/libr/main/meson.build index ea90eea53b..23dedab9e8 100644 --- a/libr/main/meson.build +++ b/libr/main/meson.build @@ -9,8 +9,7 @@ r_main_sources = [ 'rahash2.c', 'rarun2.c', 'rasm2.c', - 'rax2.c', - 'version.c', + 'rax2.c' ] r_main = library('r_main', r_main_sources, diff --git a/libr/main/version.c b/libr/main/version.c deleted file mode 100644 index abccf692e4..0000000000 --- a/libr/main/version.c +++ /dev/null @@ -1,30 +0,0 @@ -/* copyright 2015-2020 radare2 by pancake */ - -#include -#include - -#ifndef R2_GITTAP -#define R2_GITTAP "" -#endif - -#ifndef R2_GITTIP -#define R2_GITTIP "" -#endif - -#ifndef R2_BIRTH -#define R2_BIRTH "unknown" -#endif - -R_API int r_main_version_print(const char *program) { - printf ("%s "R2_VERSION" %d @ " - R_SYS_OS"-" - R_SYS_ARCH"-%d git.%s\n", - program, R2_VERSION_COMMIT, - (R_SYS_BITS & 8)? 64: 32, - *R2_GITTAP ? R2_GITTAP: ""); - if (*R2_GITTIP) { - printf ("commit: "R2_GITTIP" build: "R2_BIRTH"\n"); - } - return 0; -} - diff --git a/libr/util/str.c b/libr/util/str.c index d9c7238474..9d84edd769 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -3645,3 +3645,32 @@ R_API char *r_str_scale(const char *s, int w, int h) { free (str); return r_str_list_join (out, "\n"); } + +// version.c +#include +#include + +#ifndef R2_GITTAP +#define R2_GITTAP "" +#endif + +#ifndef R2_GITTIP +#define R2_GITTIP "" +#endif + +#ifndef R2_BIRTH +#define R2_BIRTH "unknown" +#endif + +R_API char *r_str_version(const char *program) { + char *s = r_str_newf ("%s "R2_VERSION" %d @ " + R_SYS_OS"-" + R_SYS_ARCH"-%d git.%s\n", + program, R2_VERSION_COMMIT, + (R_SYS_BITS & 8)? 64: 32, + *R2_GITTAP ? R2_GITTAP: ""); + if (*R2_GITTIP) { + s = r_str_appendf (s, "commit: "R2_GITTIP" build: "R2_BIRTH"\n"); + } + return s; +} diff --git a/man/r2r.1 b/man/r2r.1 index 9f5a904c2c..23ba44f34a 100644 --- a/man/r2r.1 +++ b/man/r2r.1 @@ -7,6 +7,7 @@ .Nm r2r .Op Fl h .Op Fl n +.Op Fl q .Op Fl v .Op Fl V .Op Fl j Ar threads @@ -27,6 +28,8 @@ You need radare2 to be available in $PATH. Show the help/usage message .It Fl n Do not run any test, just load/parse them +.It Fl q +Quiet mode (use like this: r2r -qv) .It Fl v Show version number .It Fl V