Remove editor from RzCons (#1771)

This commit is contained in:
Anton Kochkov 2021-10-01 16:34:30 +08:00 committed by GitHub
parent 72ee3eb2e3
commit fe373208eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 14 additions and 157 deletions

View file

@ -1,120 +0,0 @@
// SPDX-FileCopyrightText: 2008-2018 pancake <pancake@nopcode.org>
// SPDX-License-Identifier: LGPL-3.0-only
#include <rz_cons.h>
#define I rz_cons_singleton()
/* TODO: remove global vars */
static char *lines = NULL;
static char *path = NULL;
static char prompt[32];
static int bytes, nlines, _n = 0;
static void setnewline(int old) {
snprintf(prompt, sizeof(prompt), "%d: ", _n);
rz_line_set_prompt(prompt);
strncpy(I->line->buffer.data, rz_str_word_get0(lines, _n),
sizeof(I->line->buffer.data) - 1);
I->line->buffer.data[sizeof(I->line->buffer.data) - 1] = '\0';
I->line->buffer.index = I->line->buffer.length = strlen(I->line->buffer.data);
I->line->contents = I->line->buffer.data;
}
static void saveline(int n, const char *str) {
char *out;
if (!str) {
return;
}
out = rz_str_word_get0set(lines, bytes, _n, str, &bytes);
free(lines);
lines = out;
}
static int up(void *n) {
int old = _n;
if (_n > 0) {
_n--;
}
setnewline(old);
return -1;
}
static int down(void *n) {
int old = _n++;
setnewline(old);
return -1;
}
static void filesave(void) {
char buf[128];
int i;
if (!path) {
eprintf("File: ");
buf[0] = 0;
if (fgets(buf, sizeof(buf), stdin)) {
if (buf[0]) {
rz_str_trim_tail(buf);
free(path);
path = strdup(buf);
}
}
}
if (lines) {
for (i = 0; i < bytes; i++) {
if (lines[i] == '\0') {
lines[i] = '\n';
}
}
}
if (rz_file_dump(path, (const ut8 *)lines, bytes, 0)) {
eprintf("File '%s' saved (%d byte(s))\n", path, bytes);
} else {
eprintf("Cannot save file\n");
}
nlines = rz_str_split(lines, '\n');
}
RZ_API char *rz_cons_editor(const char *file, const char *str) {
const char *line;
_n = 0;
if (I->cb_editor) {
return I->cb_editor(I->user, file, str);
}
free(path);
if (file) {
path = strdup(file);
bytes = 0;
size_t sz = 0;
lines = rz_file_slurp(file, &sz);
bytes = (int)sz;
if (!lines) {
eprintf("Failed to load '%s'.\n", file);
RZ_FREE(path);
return NULL;
}
nlines = rz_str_split(lines, '\n');
eprintf("Loaded %d lines on %d byte(s)\n",
(nlines ? (nlines - 1) : 0), bytes);
} else {
path = NULL;
}
I->line->hist_up = up;
I->line->hist_down = down;
I->line->contents = I->line->buffer.data;
for (;;) {
setnewline(_n);
snprintf(prompt, sizeof(prompt), "%d: ", _n);
rz_line_set_prompt(prompt);
line = rz_line_readline();
saveline(_n, line);
_n++;
if (!line) {
break;
}
}
filesave();
I->line->hist_up = NULL;
I->line->hist_down = NULL;
I->line->contents = NULL;
return lines;
}

View file

@ -171,11 +171,3 @@ RZ_API int rz_cons_less_str(const char *str, const char *exitkeys) {
RZ_API void rz_cons_less(void) {
(void)rz_cons_less_str(rz_cons_singleton()->context->buffer, NULL);
}
#if 0
main (int argc, char **argv) {
char *s = rz_file_slurp (argv[1], NULL);
rz_cons_new ();
rz_cons_less (s);
}
#endif

View file

@ -4,7 +4,6 @@ rz_cons_sources = [
'cons.c',
'html.c',
# 'dietline.c',
'editor.c',
'grep.c',
'hud.c',
'input.c',

View file

@ -431,7 +431,7 @@ RZ_IPI int rz_cmd_alias(void *data, const char *input) {
if (*def) {
if (!strcmp(def, "-")) {
char *v = rz_cmd_alias_get(core->rcmd, buf, 0);
char *n = rz_cons_editor(NULL, v);
char *n = rz_core_editor(core, NULL, v);
if (n) {
rz_cmd_alias_set(core->rcmd, buf, n, 0);
free(n);

View file

@ -3205,15 +3205,20 @@ RZ_API int rz_core_search_cb(RzCore *core, ut64 from, ut64 to, RzCoreSearchCallb
return true;
}
RZ_API char *rz_core_editor(const RzCore *core, const char *file, const char *str) {
RZ_API RZ_OWN char *rz_core_editor(const RzCore *core, RZ_NULLABLE const char *file, RZ_NULLABLE const char *str) {
const bool interactive = rz_cons_is_interactive();
if (!interactive) {
return NULL;
}
const char *editor = rz_config_get(core->config, "cfg.editor");
if (RZ_STR_ISEMPTY(editor)) {
RZ_LOG_ERROR("Please set \"cfg.editor\" to run the editor");
return NULL;
}
char *name = NULL, *ret = NULL;
int fd;
if (!interactive || !editor || !*editor) {
return NULL;
}
bool readonly = false;
if (file && *file != '*') {
name = strdup(file);
@ -3246,18 +3251,10 @@ RZ_API char *rz_core_editor(const RzCore *core, const char *file, const char *st
}
close(fd);
if (name && (!editor || !*editor || !strcmp(editor, "-"))) {
RzCons *cons = rz_cons_singleton();
void *tmp = cons->cb_editor;
cons->cb_editor = NULL;
rz_cons_editor(name, NULL);
cons->cb_editor = tmp;
} else {
if (editor && name) {
char *escaped_name = rz_str_escape_sh(name);
rz_sys_cmdf("%s \"%s\"", editor, escaped_name);
free(escaped_name);
}
if (name) {
char *escaped_name = rz_str_escape_sh(name);
rz_sys_cmdf("%s \"%s\"", editor, escaped_name);
free(escaped_name);
}
size_t len = 0;
ret = name ? rz_file_slurp(name, &len) : 0;

View file

@ -888,7 +888,6 @@ RZ_API void rz_cons_context_break_push(RzConsContext *context, RzConsBreak cb, v
RZ_API void rz_cons_context_break_pop(RzConsContext *context, bool sig);
/* control */
RZ_API char *rz_cons_editor(const char *file, const char *str);
RZ_API void rz_cons_reset(void);
RZ_API void rz_cons_reset_colors(void);
RZ_API void rz_cons_goto_origin_reset(void);

View file

@ -253,15 +253,6 @@ RZ_API int rz_lang_prompt(RzLang *lang) {
if (*buf == '!') {
if (buf[1]) {
rz_sys_xsystem(buf + 1);
} else {
char *foo, *code = NULL;
do {
foo = rz_cons_editor(NULL, code);
rz_lang_run(lang, foo, 0);
free(code);
code = foo;
} while (rz_cons_yesno('y', "Edit again? (Y/n)"));
free(foo);
}
continue;
}
@ -281,7 +272,6 @@ RZ_API int rz_lang_prompt(RzLang *lang) {
RzLangDef *def;
RzListIter *iter;
eprintf(" ? - show this help message\n"
" ! - run $EDITOR\n"
" !command - run system command\n"
" . file - interpret file\n"
" q - quit prompt\n");