Skip to content

Commit

Permalink
Coloring help output of toos (<tool> -h output) (#4046)
Browse files Browse the repository at this point in the history
  • Loading branch information
byteninjaa0 authored Jan 8, 2024
1 parent 48007b4 commit 5c28709
Show file tree
Hide file tree
Showing 10 changed files with 454 additions and 291 deletions.
1 change: 1 addition & 0 deletions librz/include/rz_util/rz_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ RZ_API char *rz_print_json_human(const char *s);
RZ_API char *rz_print_json_path(const char *s, int pos);

RZ_API RZ_OWN RzStrBuf *rz_print_colorize_asm_str(RZ_BORROW RzPrint *p, const RzAsmTokenString *toks);
RZ_API void rz_print_colored_help_option(const char *option, const char *arg, const char *description, size_t maxOptionAndArgLength);
#endif

#ifdef __cplusplus
Expand Down
117 changes: 68 additions & 49 deletions librz/main/rizin.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,58 +90,77 @@ static int rz_main_version_verify(int show) {
}

static int main_help(int line) {

if (line < 2) {
printf("Usage: rizin [-ACdfLMnNqStuvwzX] [-P patch] [-p prj] [-a arch] [-b bits] [-i file]\n"
" [-s addr] [-B baddr] [-m maddr] [-c cmd] [-e k=v] file|pid|-|--|=\n");
printf("%s%s", Color_CYAN, "Usage: ");
printf(Color_RESET "rizin [-ACdfLMnNqStuvwzX] [-P patch] [-p prj] [-a arch] [-b bits] [-i file]\n"
" [-s addr] [-B baddr] [-m maddr] [-c cmd] [-e k=v] file|pid|-|--|=\n");
}
if (line != 1) {
printf(
" -- run rizin without opening any file\n"
" = same as 'rizin malloc://512'\n"
" - read file from stdin \n"
" -= perform R=! command to run all commands remotely\n"
" -0 print \\x00 after init and every command\n"
" -2 close stderr file descriptor (silent warning messages)\n"
" -a [arch] set asm.arch\n"
" -A run 'aaa' command to analyze all referenced code\n"
" -b [bits] set asm.bits\n"
" -B [baddr] set base address for PIE binaries\n"
" -c 'cmd..' execute rizin command\n"
" -C file is host:port (alias for -cR+http://%%s/cmd/)\n"
" -d debug the executable 'file' or running process 'pid'\n"
" -D [backend] enable debug mode (e cfg.debug=true)\n"
" -e k=v evaluate config var\n"
" -f block size = file size\n"
" -F [binplug] force to use that rbin plugin\n"
" -h, -hh show help message, -hh for long\n"
" -H ([var]) display variable\n"
" -i [file] run script file\n"
" -I [file] run script file before the file is opened\n"
" -k [OS/kern] set asm.os (linux, macos, w32, netbsd, ...)\n"
" -l [lib] load plugin file\n"
" -L list supported IO plugins\n"
" -m [addr] map file at given address (loadaddr)\n"
" -M do not demangle symbol names\n"
" -n, -nn do not load RzBin info (-nn only load bin structures)\n"
" -N do not load user settings and scripts\n"
" -NN do not load any script or plugin\n"
" -q quiet mode (no prompt) and quit after -i\n"
" -qq quit after running all -c and -i\n"
" -Q quiet mode (no prompt) and quit faster (quickLeak=true)\n"
" -p [p.rzdb] load project file\n"
" -r [rz-run] specify rz-run profile to load (same as -e dbg.profile=X)\n"
" -R [rule] specify custom rz-run directive\n"
" -s [addr] initial seek\n"
#if USE_THREADS && ALLOW_THREADED
" -t load rz-bin info in thread\n"
#endif
" -T do not compute file hashes\n"
" -u set bin.filter=false to get raw sym/sec/cls names\n"
" -v, -V show rizin version (-V show lib versions)\n"
" -w open file in write mode\n"
" -x open without exec-flag (asm.emu will not work), See io.exec\n"
" -X same as -e bin.usextr=false (useful for dyldcache)\n"
" -z, -zz do not load strings or load them even in raw\n");
const char *options[] = {
// clang-format off
"--", "", "run rizin without opening any file",
"=", "", "same as 'rizin malloc://512",
"- ", "", "read file from stdin",
"-=", "", "perform R=! command to run all commands remotely",
"-0", "", "print \\x00 after init and every command",
"-2", "", "close stderr file descriptor (silent warning messages)",
"-a", "[arch]", "set asm.arch",
"-A", "", "run 'aaa' command to analyze all referenced code",
"-b", "[bits]", "set asm.bits",
"-B", "[baddr]", "set base address for PIE binaries",
"-c 'cmd..'", "", "execute rizin command",
"-C", "", "file is host:port (alias for -cR+http://%%s/cmd/)",
"-d", "", "debug the executable 'file' or running process 'pid",
"-D", "[backend]", "enable debug mode (e cfg.debug=true)",
"-e k=v", "", "evaluate config var",
"-f", "", "block size = file size",
"-F", "[binplug]", "force to use that rbin plugin",
"-h, -hh", "", "show help message, -hh for long",
"-H", "([var])", "display variable",
"-i", "[file]", "run script file",
"-I", "[file]", "run script file before the file is opened",
"-k", "[OS/kern]", "set asm.os (linux, macos, w32, netbsd, ...)",
"-l", "[lib]", "load plugin file",
"-L", "", "list supported IO plugins",
"-m", "[addr]", "map file at given address (loadaddr)",
"-M", "", "do not demangle symbol names",
"-n, -nn", "", "do not load RzBin info (-nn only load bin structures)",
"-N", "", "do not load user settings and scripts",
"-NN", "", "do not load any script or plugin",
"-q", "", "quiet mode (no prompt) and quit after -i",
"-qq", "", "quit after running all -c and -i",
"-Q", "", "quiet mode (no prompt) and quit faster (quickLeak=true)",
"-p", "[p.rzdb]", "load project file",
"-r", "[rz-run]", "specify rz-run profile to load (same as -e dbg.profile=X)",
"-R", "[rule]", "specify custom rz-run directive",
"-s", "[addr]", "initial seek",
#if USE_THREADS && ALLOW_THREADED
"-t", "", "load rz-bin info in thread",
#endif
"-T", "", "do not compute file hashes",
"-u", "", "set bin.filter=false to get raw sym/sec/cls names",
"-v, -V", "", "show rizin version (-V show lib versions)",
"-w", "", "open file in write mode",
"-x", "", "open without exec-flag (asm.emu will not work), See io.exec",
"-X", "", "same as -e bin.usextr=false (useful for dyldcache)",
"-z, -zz", "", "do not load strings or load them even in raw",
// clang-format on
};
size_t maxOptionAndArgLength = 0;
for (int i = 0; i < sizeof(options) / sizeof(options[0]); i += 3) {
size_t optionLength = strlen(options[i]);
size_t argLength = strlen(options[i + 1]);
size_t totalLength = optionLength + argLength;
if (totalLength > maxOptionAndArgLength) {
maxOptionAndArgLength = totalLength;
}
}
for (int i = 0; i < sizeof(options) / sizeof(options[0]); i += 3) {
if (i + 1 < sizeof(options) / sizeof(options[0])) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxOptionAndArgLength);
}
}
}
if (line == 2) {
char *datahome = rz_path_home_prefix(RZ_DATADIR);
Expand Down
95 changes: 57 additions & 38 deletions librz/main/rz-asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,46 +169,65 @@ static int show_analinfo(RzAsmState *as, const char *arg, ut64 offset) {

static int rasm_show_help(int v) {
if (v < 2) {
printf("Usage: rz-asm [-ACdDehLBvw] [-a arch] [-b bits] [-o addr] [-s syntax]\n"
" [-f file] [-F fil:ter] [-i skip] [-l len] 'code'|hex|-\n");
}
printf("%s%s", Color_CYAN, "Usage: ");
printf(Color_RESET "rz-asm [-ACdDehLBvw] [-a arch] [-b bits] [-o addr] [-s syntax]\n"
" [-f file] [-F fil:ter] [-i skip] [-l len] 'code'|hex|-\n");
}
const char *options[] = {
// clang-format off
"-a", "[arch]", "Set architecture to assemble/disassemble (see -L)",
"-A", "", "Show Analysis information from given hexpairs",
"-b", "[bits]", "Set cpu register size (8, 16, 32, 64) (RZ_ASM_BITS)",
"-B", "", "Binary input/output (-l is mandatory for binary input)",
"-c", "[cpu]", "Select specific CPU (depends on arch)",
"-C", "", "Output in C format",
"-d, -D", "", "Disassemble from hexpair bytes (-D show hexpairs)",
"-e", "", "Use big endian instead of little endian",
"-I", "", "Display lifted RzIL code (same input as in -d, IL is also validated)",
"-E", "", "Display ESIL expression (same input as in -d)",
"-f", "[file]", "Read data from file",
"-F", "[in:out]", "Specify input and/or output filters (att2intel, x86.pseudo, ...)",
"-h, -hh", "", "Show this help, -hh for long",
"-i", "[len]", "ignore/skip N bytes of the input buffer",
"-j", "", "output in json format",
"-k", "[kernel]", "Select operating system (linux, windows, darwin, ..)",
"-l", "[len]", "Input/Output length",
"-L", "", "List Asm plugins: (a=asm, d=disasm, A=analyze, e=ESIL)",
"-o, -@", "[addr]", "Set start address for code (default 0)",
"-O", "[file]", "Output file name (rz-asm -Bf a.asm -O a)",
"-p", "", "Run SPP over input for assembly",
"-q", "", "quiet mode",
"-r", "", "output in rizin commands",
"-s", "[syntax]", "Select syntax (intel, att)",
"-v", "", "Show version information",
"-x", "", "Use hex dwords instead of hex pairs when assembling.",
"-w", "", "What's this instruction for? describe opcode",
// clang-format on
};
if (v != 1) {
printf(" -a [arch] Set architecture to assemble/disassemble (see -L)\n"
" -A Show Analysis information from given hexpairs\n"
" -b [bits] Set cpu register size (8, 16, 32, 64) (RZ_ASM_BITS)\n"
" -B Binary input/output (-l is mandatory for binary input)\n"
" -c [cpu] Select specific CPU (depends on arch)\n"
" -C Output in C format\n"
" -d, -D Disassemble from hexpair bytes (-D show hexpairs)\n"
" -e Use big endian instead of little endian\n"
" -I Display lifted RzIL code (same input as in -d, IL is also validated)\n"
" -E Display ESIL expression (same input as in -d)\n"
" -f [file] Read data from file\n"
" -F [in:out] Specify input and/or output filters (att2intel, x86.pseudo, ...)\n"
" -h, -hh Show this help, -hh for long\n"
" -i [len] ignore/skip N bytes of the input buffer\n"
" -j output in json format\n"
" -k [kernel] Select operating system (linux, windows, darwin, ..)\n"
" -l [len] Input/Output length\n"
" -L List Asm plugins: (a=asm, d=disasm, A=analyze, e=ESIL)\n"
" -o,-@ [addr] Set start address for code (default 0)\n"
" -O [file] Output file name (rz-asm -Bf a.asm -O a)\n"
" -p Run SPP over input for assembly\n"
" -q quiet mode\n"
" -r output in rizin commands\n"
" -s [syntax] Select syntax (intel, att)\n"
" -v Show version information\n"
" -x Use hex dwords instead of hex pairs when assembling.\n"
" -w What's this instruction for? describe opcode\n"
" If '-l' value is greater than output length, output is padded with nops\n"
" If the last argument is '-' reads from stdin\n");
printf("Environment:\n"
" RZ_NOPLUGINS do not load shared plugins (speedup loading)\n"
" RZ_ASM_ARCH same as rz-asm -a\n"
" RZ_ASM_BITS same as rz-asm -b\n"
" RZ_DEBUG if defined, show error messages and crash signal\n"
"");
size_t maxOptionAndArgLength = 0;
for (int i = 0; i < sizeof(options) / sizeof(options[0]); i += 3) {
size_t optionLength = strlen(options[i]);
size_t argLength = strlen(options[i + 1]);
size_t totalLength = optionLength + argLength;
if (totalLength > maxOptionAndArgLength) {
maxOptionAndArgLength = totalLength;
}
}
for (int i = 0; i < sizeof(options) / sizeof(options[0]); i += 3) {
if (i + 1 < sizeof(options) / sizeof(options[0])) {
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxOptionAndArgLength);
}
}
}
printf(" If '-l' value is greater than output length, output is padded with nops\n"
" If the last argument is '-' reads from stdin\n"
"Environment:\n"
" RZ_NOPLUGINS do not load shared plugins (speedup loading)\n"
" RZ_ASM_ARCH same as rz-asm -a\n"
" RZ_ASM_BITS same as rz-asm -b\n"
" RZ_DEBUG if defined, show error messages and crash signal\n"
"");
if (v == 2) {
printf("Supported Assembler directives:\n");
rz_asm_list_directives();
Expand Down
Loading

0 comments on commit 5c28709

Please sign in to comment.