radare2/libr/cons/editor.c

124 lines
2.5 KiB
C
Raw Normal View History

2021-08-19 16:39:54 +00:00
/* radare - LGPL - Copyright 2008-2021 - pancake */
#include <r_cons.h>
2015-12-08 12:24:21 +00:00
#define I r_cons_singleton ()
/* TODO: remove global vars */
2016-06-29 10:33:31 +00:00
static char *lines = NULL;
static char *path = NULL;
static char prompt[32];
2016-06-29 10:33:31 +00:00
static int bytes, nlines, _n = 0;
static void setnewline(int old) {
snprintf (prompt, sizeof (prompt), "%d: ", _n);
r_line_set_prompt (prompt);
2014-06-07 22:56:22 +00:00
strncpy (I->line->buffer.data, r_str_word_get0 (lines, _n),
2015-12-08 12:24:21 +00:00
sizeof (I->line->buffer.data) - 1);
2014-06-07 22:56:22 +00:00
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;
}
2015-12-08 12:24:21 +00:00
static void saveline(int n, const char *str) {
char *out;
if (!str) {
return;
}
out = r_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) {
2015-12-08 12:24:21 +00:00
int old = _n++;
setnewline (old);
return -1;
}
static void filesave(void) {
char buf[128];
int i;
if (!path) {
eprintf ("File: ");
buf[0] = 0;
2020-02-29 19:47:10 +00:00
if (fgets (buf, sizeof (buf), stdin)) {
if (buf[0]) {
r_str_trim_tail (buf);
free (path);
path = strdup (buf);
}
}
}
if (!path) {
eprintf ("No file given.\n");
return;
}
if (lines) {
2015-12-08 12:24:21 +00:00
for (i = 0; i < bytes; i++) {
2018-03-07 09:55:45 +00:00
if (lines[i] == '\0') {
2015-12-08 12:24:21 +00:00
lines[i] = '\n';
2018-03-07 09:55:45 +00:00
}
}
}
2016-06-29 10:33:31 +00:00
if (r_file_dump (path, (const ut8 *)lines, bytes, 0)) {
2017-12-27 17:33:58 +00:00
eprintf ("File '%s' saved (%d byte(s))\n", path, bytes);
2016-06-29 10:33:31 +00:00
} else {
eprintf ("Cannot save file\n");
}
nlines = r_str_split (lines, '\n');
}
2015-12-08 12:24:21 +00:00
R_API char *r_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 = r_file_slurp (file, &sz);
bytes = (int)sz;
if (!lines) {
eprintf ("Failed to load '%s'.\n", file);
R_FREE (path);
return NULL;
}
nlines = r_str_split (lines, '\n');
2017-12-27 17:33:58 +00:00
eprintf ("Loaded %d lines on %d byte(s)\n",
2015-12-08 12:24:21 +00:00
(nlines? (nlines - 1): 0), bytes);
2016-06-29 10:33:31 +00:00
} 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);
r_line_set_prompt (prompt);
line = r_line_readline ();
saveline (_n, line);
_n++;
2016-06-29 10:33:31 +00:00
if (!line) {
break;
}
}
filesave ();
2015-12-08 12:24:21 +00:00
I->line->hist_up = NULL;
I->line->hist_down = NULL;
I->line->contents = NULL;
return lines;
}