Skip to content

Commit

Permalink
Make rz_diff_buffers_unified() use the Subprocess API
Browse files Browse the repository at this point in the history
  • Loading branch information
thestr4ng3r authored and XVilka committed Apr 8, 2021
1 parent a936651 commit be6198f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
10 changes: 8 additions & 2 deletions binrz/rz-test/rz-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,10 @@ static RzThreadFunctionRet worker_th(RzThread *th) {
static void print_diff(const char *actual, const char *expected, bool diffchar, const char *regexp) {
RzDiff *d = rz_diff_new();
#ifdef __WINDOWS__
d->diff_cmd = "git diff --no-index";
static const char *diff_cmd[] = {
"git", "diff", "--no-index", NULL
};
d->diff_cmd = diff_cmd;
#endif
const char *output = actual;
if (regexp) {
Expand All @@ -623,7 +626,10 @@ static void print_diff(const char *actual, const char *expected, bool diffchar,
rz_diffchar_free(diff);
goto cleanup;
}
d->diff_cmd = "git diff --no-index --word-diff=porcelain --word-diff-regex=.";
static const char *diff_cmd_char[] = {
"git", "diff", "--no-index", "--word-diff=porcelain", "--word-diff-regex=.", NULL
};
d->diff_cmd = diff_cmd_char;
}
char *uni = rz_diff_buffers_to_string(d, (const ut8 *)expected, (int)strlen(expected),
(const ut8 *)output, (int)strlen(output));
Expand Down
2 changes: 1 addition & 1 deletion librz/include/rz_diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ typedef struct rz_diff_t {
void *user;
bool verbose;
int type;
const char *diff_cmd;
const char **diff_cmd; // null-terminated array of cmd+args
int (*callback)(struct rz_diff_t *diff, void *user, RzDiffOp *op);
} RzDiff;

Expand Down
3 changes: 3 additions & 0 deletions librz/main/rz-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,9 @@ RZ_API int rz_main_rz_diff(int argc, const char **argv) {
RzDiff *d;
RzGetopt opt;

rz_subprocess_init();
atexit(rz_subprocess_fini);

rzdiff_options_init(&ro);
rz_getopt_init(&opt, argc, argv, "Aa:b:BCDe:npg:m:G:OijrhcdsS:uUvVxXt:zqZ");
while ((o = rz_getopt_next(&opt)) != -1) {
Expand Down
47 changes: 28 additions & 19 deletions librz/util/udiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
// the non-system-diff doesnt work well
#define USE_SYSTEM_DIFF 1

static const char *diff_cmd_default[] = {
"diff", "-u", NULL
};

RZ_API RzDiff *rz_diff_new_from(ut64 off_a, ut64 off_b) {
RzDiff *d = RZ_NEW0(RzDiff);
if (d) {
d->delta = 1;
d->user = NULL;
d->off_a = off_a;
d->off_b = off_b;
d->diff_cmd = "diff -u";
d->diff_cmd = diff_cmd_default;
}
return d;
}
Expand Down Expand Up @@ -135,30 +139,35 @@ RZ_API int rz_diff_buffers_static(RzDiff *d, const ut8 *a, int la, const ut8 *b,
return 0;
}

// XXX: temporary files are
// XXX: temporary files are bad
RZ_API char *rz_diff_buffers_unified(RzDiff *d, const ut8 *a, int la, const ut8 *b, int lb) {
rz_return_val_if_fail(d && d->diff_cmd && *d->diff_cmd && a && b, NULL);
rz_file_dump(".a", a, la, 0);
rz_file_dump(".b", b, lb, 0);
#if 0
if (rz_mem_is_printable (a, RZ_MIN (5, la))) {
rz_file_dump (".a", a, la, 0);
rz_file_dump (".b", b, lb, 0);
} else {
rz_file_hexdump (".a", a, la, 0);
rz_file_hexdump (".b", b, lb, 0);
}
#endif
char *err = NULL;
char *out = NULL;
int out_len;
char *diff_cmdline = rz_str_newf("%s .a .b", d->diff_cmd);
if (diff_cmdline) {
(void)rz_sys_cmd_str_full(diff_cmdline, NULL, &out, &out_len, &err);
free(diff_cmdline);
}
RzPVector args;
rz_pvector_init(&args, NULL);
for (const char **i = d->diff_cmd; *i; i++) {
rz_pvector_push(&args, (void *)*i);
}
rz_pvector_push(&args, ".a");
rz_pvector_push(&args, ".b");
RzSubprocess *proc = rz_subprocess_start(rz_pvector_at(&args, 0),
(const char **)rz_pvector_index_ptr(&args, 1), rz_pvector_len(&args) - 1, NULL, NULL, 0);
if (!proc) {
goto terria;
}
rz_subprocess_wait(proc, 500);
RzSubprocessOutput *pout = rz_subprocess_drain(proc);
rz_subprocess_free(proc);
if (pout) {
out = pout->out;
pout->out = NULL;
rz_subprocess_output_free(pout);
}
terria:
rz_file_rm(".a");
rz_file_rm(".b");
free(err);
return out;
}

Expand Down

0 comments on commit be6198f

Please sign in to comment.