rizinorg / rizin

UNIX-like reverse engineering framework and command-line toolset.
https://rizin.re
GNU Lesser General Public License v3.0
2.7k stars 361 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/util/syscmd.c
488:    rz_sys_cmdf("move %s >nul", input);
490:    rz_sys_cmdf("mv %s", input);

librz/util/file.c
1177:   int rc = rz_sys_cmdf("cp -f '%s' '%s'", src2, dst2);

librz/socket/socket_http.c
203:            char *res = rz_sys_cmd_str(command, NULL, &len);

librz/socket/run.c
214:                    char *ret = rz_sys_cmd_str(msg, NULL, NULL);
223:            char *a = rz_sys_cmd_str(src + 1, NULL, NULL);
229:                    ret = rz_sys_cmd_str(src + 1, NULL, NULL);

librz/socket/socket.c
214:            int res = rz_sys_cmdf("rz-run system=\"%s\" listen=%d", a, port);

librz/main/rz-agent.c
158:                            pid = rz_sys_cmdbg(cmd);

librz/main/rz-find.c
234:            rz_sys_cmdf("rz-bin -q%szzz \"%s\"", ro->json ? "j" : "", efile);
240:            rz_sys_cmdf("rizin"
252:                    rz_sys_cmdf("rizin -qc \"/E %s\" \"%s\"", kw, efile);

librz/egg/egg_Cfile.c
82:             output = rz_sys_cmd_strf("rizin -hh | grep INCDIR | awk '{print $2}'");
289:    output = rz_sys_cmd_strf("rz-bin -o '%s.text' -O d/S/'%s' '%s'.o",
313:            output = rz_sys_cmd_strf("'%s' -j .text -O binary '%s.o' '%s.text'",

librz/io/p/io_winedbg.c
142:    rz_sys_cmdf("pkill rz-run");

librz/core/rtr_http.c
82:             rz_sys_cmdf("%s http://%s:%d/%s &",

librz/core/core.c
3259:                   rz_sys_cmdf("%s \"%s\"", editor, escaped_name);

librz/core/vmenus.c
3181:                   rz_sys_cmdf("man %s", man);

librz/core/cmd.c
1401:           rz_sys_cmdf("%s", input);
1404:           rz_sys_cmdf("%s", input);
1406:           rz_sys_cmdf("%s", input);
1411:           rz_sys_cmdf("%s", input);
1416:           rz_sys_cmdf("%s", input);
1418:           rz_sys_cmdf("%s", input);
1421:           rz_sys_cmdf("%s", input);
1423:           rz_sys_cmdf("%s", input);
1426:           rz_sys_cmdf("%s", input);
1428:           rz_sys_cmdf("%s", input);
1474:           rz_sys_cmdf("rizin%s", input);
1581:           ////rz_sys_cmdf ("v%s", input);
1911:                                   ret = rz_sys_cmd_str_full(cmd + 1, NULL, &out, &olen, NULL);
2110:           rz_sys_cmd_str_full(shell_cmd + 1, str, &out, &olen, NULL);
2833:                           rz_sys_cmdf("%s '%s'", editor, str);
6425:   rcmd = buf = rz_sys_cmd_str(cmd, 0, &len);

librz/core/cmd_info.c
703:                            rz_sys_cmdf("rz-bin -O \"%s\" \"%s\"", rz_str_trim_head_ro(input + 1), desc->name);
706:                            rz_sys_cmdf("rz-bin -O help");

librz/core/cmd_debug.c
1673:                           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);
1676:                           res = rz_sys_cmd_strf("env RZ_BIN_PREFIX=\"%s\" rz-bin %s-B 0x%08" PFMT64x " -S \"%s\"", name, mode, baddr, filesc);

librz/cons/cons.c
955:                            rz_sys_cmd_str_full(I.pager, CTX(buffer), NULL, NULL, NULL);

librz/bin/mangling/swift-sd.c
144:            //char *res = rz_sys_cmd_strf ("%s -compact -simplified '%s'",
145:            char *res = rz_sys_cmd_strf("%s -compact '%s'",

librz/asm/p/asm_x86_nasm.c
33:     if (!rz_sys_cmdf("nasm %s -o %s", ipath, opath)) {

librz/asm/p/asm_gas.c
16:     ut8 *out = (ut8 *)rz_sys_cmd_str(cmd, "", &len);

Note though, that some of them are legit.

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