rizinorg / rizin

UNIX-like reverse engineering framework and command-line toolset.
https://rizin.re
GNU Lesser General Public License v3.0
2.72k stars 363 forks source link

Refactor to not use calling the external commands unless necessary #1223

Open XVilka opened 3 years ago

XVilka commented 3 years ago

There is a bunch of rz_sys_cmd*() calls in the librz/ while most of them unnecessary and can be achieved with the calling of the API function.

There are two categories of such calls - calling of the internal utils, e.g. rz-bin, and calling of the external tools, e.g. cp or move.

[i] ℤ rg "rz_sys_cmd*" librz                                                                                                                                                                                                      11:49:26 
librz/cons/cons.c
    :1000: rz_sys_cmd_str_full(I.pager, CTX(buffer), NULL, NULL, NULL);
librz/util/sys.c
    :324: ret = rz_sys_cmd_str(cmd, NULL, NULL);
    :899: if (rz_sys_cmd_str_full(cmd, input, &output, len, NULL)) {
librz/util/file.c
    :1227: int rc = rz_sys_cmdf("cp -f '%s' '%s'", src2, dst2);
librz/io/p/io_winedbg.c
    :144: rz_sys_cmdf("pkill rz-run");
librz/egg/egg_Cfile.c
    :283: output = rz_sys_cmd_strf("rz-bin -o '%s.text' -O d/S/'%s' '%s'.o",
    :307: output = rz_sys_cmd_strf("'%s' -j .text -O binary '%s.o' '%s.text'",
librz/arch/p/asm/asm_x86_nasm.c
    :33: if (!rz_sys_cmdf("nasm %s -o %s", ipath, opath)) {
librz/socket/socket.c
    :258: int res = rz_sys_cmdf("rz-run system=\"%s\" listen=%d", a, port);
librz/socket/run.c
    :219: char *tmp = rz_sys_cmd_str(copy + 1, NULL, NULL);
    :227: char *tmp = rz_sys_cmd_str(copy + 1, NULL, NULL);
    :245: char *tmp = rz_sys_cmd_str(copy + 2, NULL, NULL);
librz/socket/socket_http.c
    :205: int ret = rz_sys_cmd_str_full(command, NULL, &res, &len, &err);
librz/include/rz_util/rz_sys.h
    :145: // #define rz_sys_cmd_str(cmd, input, len) rz_sys_cmd_str_full(cmd, input, len, 0)
librz/core/cmd/cmd_debug.c
    :1081: res = rz_sys_cmd_strf("env RZ_BIN_PREFIX=\"%s\" rz-bin %s-B 0x%08" PFMT64x " -S \"%s\" | grep \"%s\"", name, mode, baddr, filesc, sect);
    :1084: res = rz_sys_cmd_strf("env RZ_BIN_PREFIX=\"%s\" rz-bin %s-B 0x%08" PFMT64x " -S \"%s\"", name, mode, baddr, filesc);
librz/core/cmd/cmd.c
    :585: rz_sys_cmd_str_full(shell_cmd + 1, str, &out, &olen, NULL);
    :1311: rz_sys_cmdf("%s '%s'", editor, str);
    :5020: rcmd = buf = rz_sys_cmd_str(command, 0, &len);
librz/core/tui/define.c
    :215: rz_sys_cmdf("man %s", man);
librz/core/core.c
    :2328: rz_sys_cmdf("%s \"%s\"", editor, escaped_name);
librz/core/rtr_http.c
    :382: rz_sys_cmdf("%s http://%s:%d/%s &",
librz/main/rz-find.c
    :409: rz_sys_cmdf("rizin"
    :421: rz_sys_cmdf("rizin -qc \"/E %s\" \"%s\"", kw, efile);

Todo

Note though, that some of them are legit. Tick the once which are, otherwise link PR.

karliss commented 3 years ago

some of them are legit, e.g. the "chcp" one in librz/cons/cons.c

Couldn't that be done using SetConsoleCP and SetConsoleOutputCP API? I don't know Windows Console API so I might be wrong.

XVilka commented 3 years ago

@karliss in fact it calls SetConsoleOutputCP() right before. Not sure why it was done. Maybe @kazarmy knows more