2017-09-13 22:50:27 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
//config:config HEXEDIT
|
|
|
|
//config: bool "hexedit"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: Edit file in hexadecimal.
|
|
|
|
|
|
|
|
//applet:IF_HEXEDIT(APPLET(hexedit, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_HEXEDIT) += hexedit.o
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2017-09-14 13:12:40 +05:30
|
|
|
#define ESC "\033"
|
|
|
|
#define HOME ESC"[H"
|
2017-09-14 14:21:12 +05:30
|
|
|
#define CLEAR ESC"[J"
|
|
|
|
#define CLEAR_TILL_EOL ESC"[K"
|
2017-09-14 13:12:40 +05:30
|
|
|
#define SET_ALT_SCR ESC"[?1049h"
|
|
|
|
#define POP_ALT_SCR ESC"[?1049l"
|
|
|
|
|
|
|
|
#undef CTRL
|
|
|
|
#define CTRL(c) ((c) & (uint8_t)~0x60)
|
2017-09-13 22:50:27 +05:30
|
|
|
|
|
|
|
struct globals {
|
|
|
|
smallint half;
|
2017-09-14 13:12:40 +05:30
|
|
|
smallint in_read_key;
|
2017-09-13 22:50:27 +05:30
|
|
|
int fd;
|
|
|
|
unsigned height;
|
2017-09-14 13:12:40 +05:30
|
|
|
unsigned row;
|
2017-09-15 17:30:41 +05:30
|
|
|
unsigned pagesize;
|
2017-09-14 14:21:12 +05:30
|
|
|
uint8_t *baseaddr;
|
2017-09-13 22:50:27 +05:30
|
|
|
uint8_t *current_byte;
|
|
|
|
uint8_t *eof_byte;
|
|
|
|
off_t size;
|
|
|
|
off_t offset;
|
2017-09-14 13:12:40 +05:30
|
|
|
/* needs to be zero-inited, thus keeping it in G: */
|
|
|
|
char read_key_buffer[KEYCODE_BUFFER_SIZE];
|
2017-09-13 22:50:27 +05:30
|
|
|
struct termios orig_termios;
|
|
|
|
};
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define INIT_G() do { \
|
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
|
|
|
} while (0)
|
|
|
|
|
2017-09-15 17:30:41 +05:30
|
|
|
//TODO: move to libbb
|
|
|
|
#if defined(__x86_64__) || defined(i386)
|
|
|
|
# define G_pagesize 4096
|
|
|
|
# define INIT_PAGESIZE() ((void)0)
|
|
|
|
#else
|
|
|
|
# define G_pagesize (G.pagesize)
|
|
|
|
# define INIT_PAGESIZE() ((void)(G.pagesize = getpagesize()))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* hopefully there aren't arches with PAGE_SIZE > 64k */
|
|
|
|
#define G_mapsize (64*1024)
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 03:34:16 +05:30
|
|
|
/* "12ef5670 (xx )*16 _1_3_5_7_9abcdef\n"NUL */
|
|
|
|
#define LINEBUF_SIZE (8 + 1 + 3*16 + 16 + 1 + 1 /*paranoia:*/ + 13)
|
2017-09-14 01:08:55 +05:30
|
|
|
|
2017-09-14 13:12:40 +05:30
|
|
|
static void restore_term(void)
|
|
|
|
{
|
|
|
|
tcsetattr_stdin_TCSANOW(&G.orig_termios);
|
|
|
|
printf(POP_ALT_SCR);
|
|
|
|
fflush_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_catcher(int sig)
|
|
|
|
{
|
|
|
|
if (!G.in_read_key) {
|
|
|
|
/* now it's not safe to do I/O, just inform the main loop */
|
|
|
|
bb_got_signal = sig;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
restore_term();
|
|
|
|
kill_myself_with_sig(sig);
|
|
|
|
}
|
|
|
|
|
2017-09-14 01:08:55 +05:30
|
|
|
static int format_line(char *hex, uint8_t *data, off_t offset)
|
2017-09-13 22:50:27 +05:30
|
|
|
{
|
2017-09-14 01:08:55 +05:30
|
|
|
int ofs_pos;
|
|
|
|
char *text;
|
2017-09-13 22:50:27 +05:30
|
|
|
uint8_t *end, *end1;
|
|
|
|
|
2017-09-14 01:08:55 +05:30
|
|
|
#if 1
|
|
|
|
/* Can be more than 4Gb, thus >8 chars, thus use a variable - don't assume 8! */
|
|
|
|
ofs_pos = sprintf(hex, "%08"OFF_FMT"x ", offset);
|
|
|
|
#else
|
|
|
|
if (offset <= 0xffff)
|
|
|
|
ofs_pos = sprintf(hex, "%04"OFF_FMT"x ", offset);
|
|
|
|
else
|
|
|
|
ofs_pos = sprintf(hex, "%08"OFF_FMT"x ", offset);
|
|
|
|
#endif
|
|
|
|
hex += ofs_pos;
|
|
|
|
|
2017-09-15 17:30:41 +05:30
|
|
|
text = hex + 16 * 3;
|
2017-09-13 22:50:27 +05:30
|
|
|
end1 = data + 15;
|
|
|
|
if ((G.size - offset) > 0) {
|
|
|
|
end = end1;
|
|
|
|
if ((G.size - offset) <= 15)
|
|
|
|
end = data + (G.size - offset) - 1;
|
|
|
|
while (data <= end) {
|
|
|
|
uint8_t c = *data++;
|
|
|
|
*hex++ = bb_hexdigits_upcase[c >> 4];
|
|
|
|
*hex++ = bb_hexdigits_upcase[c & 0xf];
|
|
|
|
*hex++ = ' ';
|
|
|
|
if (c < ' ' || c > 0x7e)
|
|
|
|
c = '.';
|
|
|
|
*text++ = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (data <= end1) {
|
|
|
|
*hex++ = ' ';
|
|
|
|
*hex++ = ' ';
|
|
|
|
*hex++ = ' ';
|
|
|
|
*text++ = ' ';
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
*text = '\0';
|
2017-09-14 01:08:55 +05:30
|
|
|
|
|
|
|
return ofs_pos;
|
2017-09-13 22:50:27 +05:30
|
|
|
}
|
|
|
|
|
2017-09-15 17:30:41 +05:30
|
|
|
static void redraw(unsigned cursor)
|
2017-09-13 22:50:27 +05:30
|
|
|
{
|
|
|
|
uint8_t *data;
|
|
|
|
off_t offset;
|
2017-09-14 14:21:12 +05:30
|
|
|
unsigned i, pos;
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 14:21:12 +05:30
|
|
|
printf(HOME CLEAR);
|
2017-09-15 17:30:41 +05:30
|
|
|
|
|
|
|
/* if cursor is past end of screen, how many lines to move down? */
|
|
|
|
i = (cursor / 16) - G.height + 1;
|
|
|
|
if ((int)i < 0)
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
data = G.baseaddr + i * 16;
|
|
|
|
offset = G.offset + i * 16;
|
|
|
|
cursor -= i * 16;
|
2017-09-14 14:21:12 +05:30
|
|
|
pos = i = 0;
|
2017-09-13 22:50:27 +05:30
|
|
|
while (i < G.height) {
|
2017-09-14 01:08:55 +05:30
|
|
|
char buf[LINEBUF_SIZE];
|
2017-09-14 14:21:12 +05:30
|
|
|
pos = format_line(buf, data, offset);
|
2017-09-13 22:50:27 +05:30
|
|
|
printf(
|
2017-09-15 17:30:41 +05:30
|
|
|
"\r\n%s" + (!i) * 2, /* print \r\n only on 2nd line and later */
|
2017-09-14 01:08:55 +05:30
|
|
|
buf
|
2017-09-13 22:50:27 +05:30
|
|
|
);
|
|
|
|
data += 16;
|
|
|
|
offset += 16;
|
|
|
|
i++;
|
|
|
|
}
|
2017-09-15 17:30:41 +05:30
|
|
|
|
|
|
|
printf(ESC"[%u;%uH", 1 + cursor / 16, 1 + pos + (cursor & 0xf) * 3);
|
2017-09-13 22:50:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void redraw_cur_line(void)
|
|
|
|
{
|
2017-09-14 01:08:55 +05:30
|
|
|
char buf[LINEBUF_SIZE];
|
2017-09-13 22:50:27 +05:30
|
|
|
uint8_t *data;
|
2017-09-14 01:08:55 +05:30
|
|
|
off_t offset;
|
|
|
|
int column;
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 01:08:55 +05:30
|
|
|
column = (0xf & (uintptr_t)G.current_byte);
|
|
|
|
data = G.current_byte - column;
|
2017-09-14 14:21:12 +05:30
|
|
|
offset = G.offset + (data - G.baseaddr);
|
2017-09-14 01:08:55 +05:30
|
|
|
|
|
|
|
column = column*3 + G.half;
|
|
|
|
column += format_line(buf, data, offset);
|
|
|
|
printf("%s"
|
|
|
|
"\r"
|
|
|
|
"%.*s",
|
|
|
|
buf + column,
|
|
|
|
column, buf
|
2017-09-13 22:50:27 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-14 14:31:37 +05:30
|
|
|
/* if remappers return 0, no change was done */
|
|
|
|
static int remap(unsigned cur_pos)
|
2017-09-13 22:50:27 +05:30
|
|
|
{
|
2017-09-14 14:21:12 +05:30
|
|
|
if (G.baseaddr)
|
|
|
|
munmap(G.baseaddr, G_mapsize);
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 14:21:12 +05:30
|
|
|
G.baseaddr = mmap(NULL,
|
2017-09-13 22:50:27 +05:30
|
|
|
G_mapsize,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
|
|
|
G.fd,
|
|
|
|
G.offset
|
|
|
|
);
|
2017-09-14 14:21:12 +05:30
|
|
|
if (G.baseaddr == MAP_FAILED) {
|
2017-09-14 13:12:40 +05:30
|
|
|
restore_term();
|
2017-09-13 22:50:27 +05:30
|
|
|
bb_perror_msg_and_die("mmap");
|
2017-09-14 13:12:40 +05:30
|
|
|
}
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 14:21:12 +05:30
|
|
|
G.current_byte = G.baseaddr + cur_pos;
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 14:21:12 +05:30
|
|
|
G.eof_byte = G.baseaddr + G_mapsize;
|
2017-09-13 22:50:27 +05:30
|
|
|
if ((G.size - G.offset) < G_mapsize) {
|
|
|
|
/* mapping covers tail of the file */
|
|
|
|
/* we do have a mapped byte which is past eof */
|
2017-09-14 14:21:12 +05:30
|
|
|
G.eof_byte = G.baseaddr + (G.size - G.offset);
|
2017-09-13 22:50:27 +05:30
|
|
|
}
|
2017-09-14 14:31:37 +05:30
|
|
|
return 1;
|
2017-09-13 22:50:27 +05:30
|
|
|
}
|
2017-09-14 14:31:37 +05:30
|
|
|
static int move_mapping_further(void)
|
2017-09-13 22:50:27 +05:30
|
|
|
{
|
|
|
|
unsigned pos;
|
|
|
|
unsigned pagesize;
|
|
|
|
|
|
|
|
if ((G.size - G.offset) < G_mapsize)
|
2017-09-14 14:31:37 +05:30
|
|
|
return 0; /* can't move mapping even further, it's at the end already */
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-15 17:30:41 +05:30
|
|
|
pagesize = G_pagesize; /* constant on most arches */
|
2017-09-14 14:21:12 +05:30
|
|
|
pos = G.current_byte - G.baseaddr;
|
2017-09-13 22:50:27 +05:30
|
|
|
if (pos >= pagesize) {
|
2017-09-14 03:34:16 +05:30
|
|
|
/* move offset up until current position is in 1st page */
|
2017-09-13 22:50:27 +05:30
|
|
|
do {
|
|
|
|
G.offset += pagesize;
|
|
|
|
if (G.offset == 0) { /* whoops */
|
|
|
|
G.offset -= pagesize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos -= pagesize;
|
|
|
|
} while (pos >= pagesize);
|
2017-09-14 14:31:37 +05:30
|
|
|
return remap(pos);
|
2017-09-13 22:50:27 +05:30
|
|
|
}
|
2017-09-14 14:31:37 +05:30
|
|
|
return 0;
|
2017-09-13 22:50:27 +05:30
|
|
|
}
|
2017-09-14 14:31:37 +05:30
|
|
|
static int move_mapping_lower(void)
|
2017-09-13 22:50:27 +05:30
|
|
|
{
|
|
|
|
unsigned pos;
|
|
|
|
unsigned pagesize;
|
|
|
|
|
|
|
|
if (G.offset == 0)
|
2017-09-14 14:31:37 +05:30
|
|
|
return 0; /* we are at 0 already */
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-15 17:30:41 +05:30
|
|
|
pagesize = G_pagesize; /* constant on most arches */
|
2017-09-14 14:21:12 +05:30
|
|
|
pos = G.current_byte - G.baseaddr;
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 03:34:16 +05:30
|
|
|
/* move offset down until current position is in last page */
|
2017-09-13 22:50:27 +05:30
|
|
|
pos += pagesize;
|
|
|
|
while (pos < G_mapsize) {
|
|
|
|
pos += pagesize;
|
|
|
|
G.offset -= pagesize;
|
|
|
|
if (G.offset == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos -= pagesize;
|
|
|
|
|
2017-09-14 14:31:37 +05:30
|
|
|
return remap(pos);
|
2017-09-13 22:50:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//usage:#define hexedit_trivial_usage
|
|
|
|
//usage: "FILE"
|
|
|
|
//usage:#define hexedit_full_usage "\n\n"
|
|
|
|
//usage: "Edit FILE in hexadecimal"
|
|
|
|
int hexedit_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int hexedit_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
INIT_G();
|
2017-09-15 17:30:41 +05:30
|
|
|
INIT_PAGESIZE();
|
2017-09-13 22:50:27 +05:30
|
|
|
|
|
|
|
get_terminal_width_height(-1, NULL, &G.height);
|
2017-09-14 03:34:16 +05:30
|
|
|
if (1) {
|
|
|
|
/* reduce number of write() syscalls while PgUp/Down: fully buffered output */
|
|
|
|
unsigned sz = (G.height | 0xf) * LINEBUF_SIZE;
|
|
|
|
setvbuf(stdout, xmalloc(sz), _IOFBF, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
getopt32(argv, "^" "" "\0" "=1"/*one arg*/);
|
|
|
|
argv += optind;
|
2017-09-13 22:50:27 +05:30
|
|
|
|
|
|
|
G.fd = xopen(*argv, O_RDWR);
|
|
|
|
G.size = xlseek(G.fd, 0, SEEK_END);
|
|
|
|
|
|
|
|
/* TERMIOS_RAW_CRNL suppresses \n -> \r\n translation, helps with down-arrow */
|
2017-09-14 14:21:12 +05:30
|
|
|
printf(SET_ALT_SCR);
|
2017-09-13 22:50:27 +05:30
|
|
|
set_termios_to_raw(STDIN_FILENO, &G.orig_termios, TERMIOS_RAW_CRNL);
|
|
|
|
bb_signals(BB_FATAL_SIGS, sig_catcher);
|
|
|
|
|
|
|
|
remap(0);
|
2017-09-15 17:30:41 +05:30
|
|
|
redraw(0);
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 02:32:51 +05:30
|
|
|
//TODO: //Home/End: start/end of line; '<'/'>': start/end of file
|
2017-09-13 22:50:27 +05:30
|
|
|
//Backspace: undo
|
|
|
|
//Ctrl-L: redraw
|
|
|
|
//Ctrl-Z: suspend
|
|
|
|
//'/', Ctrl-S: search
|
|
|
|
//TODO: detect window resize
|
|
|
|
|
|
|
|
for (;;) {
|
2017-09-14 02:32:51 +05:30
|
|
|
unsigned cnt;
|
2017-09-14 14:50:36 +05:30
|
|
|
int32_t key = key; /* for compiler */
|
2017-09-13 22:50:27 +05:30
|
|
|
uint8_t byte;
|
|
|
|
|
|
|
|
fflush_all();
|
2017-09-14 13:12:40 +05:30
|
|
|
G.in_read_key = 1;
|
|
|
|
if (!bb_got_signal)
|
|
|
|
key = read_key(STDIN_FILENO, G.read_key_buffer, -1);
|
|
|
|
G.in_read_key = 0;
|
|
|
|
if (bb_got_signal)
|
|
|
|
key = CTRL('X');
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 02:32:51 +05:30
|
|
|
cnt = 1;
|
2017-09-14 14:50:36 +05:30
|
|
|
if ((unsigned)(key - 'A') <= 'Z' - 'A')
|
|
|
|
key |= 0x20; /* convert A-Z to a-z */
|
2017-09-13 22:50:27 +05:30
|
|
|
switch (key) {
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
2017-09-14 14:50:36 +05:30
|
|
|
/* convert to '0'+10...15 */
|
|
|
|
key = key - ('a' - '0' - 10);
|
2017-09-13 22:50:27 +05:30
|
|
|
/* fall through */
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
if (G.current_byte == G.eof_byte) {
|
2017-09-14 14:31:37 +05:30
|
|
|
if (!move_mapping_further()) {
|
|
|
|
/* already at EOF; extend the file */
|
2017-09-13 22:50:27 +05:30
|
|
|
if (++G.size <= 0 /* overflow? */
|
|
|
|
|| ftruncate(G.fd, G.size) != 0 /* error extending? (e.g. block dev) */
|
|
|
|
) {
|
|
|
|
G.size--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
G.eof_byte++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
key -= '0';
|
|
|
|
byte = *G.current_byte & 0xf0;
|
|
|
|
if (!G.half) {
|
|
|
|
byte = *G.current_byte & 0x0f;
|
|
|
|
key <<= 4;
|
|
|
|
}
|
|
|
|
*G.current_byte = byte + key;
|
|
|
|
/* can't just print one updated hex char: need to update right-hand ASCII too */
|
|
|
|
redraw_cur_line();
|
|
|
|
/* fall through */
|
|
|
|
case KEYCODE_RIGHT:
|
|
|
|
if (G.current_byte == G.eof_byte)
|
|
|
|
break; /* eof - don't allow going past it */
|
|
|
|
byte = *G.current_byte;
|
|
|
|
if (!G.half) {
|
|
|
|
G.half = 1;
|
|
|
|
putchar(bb_hexdigits_upcase[byte >> 4]);
|
|
|
|
} else {
|
|
|
|
G.half = 0;
|
|
|
|
G.current_byte++;
|
|
|
|
if ((0xf & (uintptr_t)G.current_byte) == 0) {
|
|
|
|
/* rightmost pos, wrap to next line */
|
|
|
|
if (G.current_byte == G.eof_byte)
|
|
|
|
move_mapping_further();
|
|
|
|
printf(ESC"[46D"); /* cursor left 3*15 + 1 chars */
|
|
|
|
goto down;
|
|
|
|
}
|
|
|
|
putchar(bb_hexdigits_upcase[byte & 0xf]);
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
break;
|
2017-09-14 02:32:51 +05:30
|
|
|
case KEYCODE_PAGEDOWN:
|
|
|
|
cnt = G.height;
|
2017-09-13 22:50:27 +05:30
|
|
|
case KEYCODE_DOWN:
|
2017-09-14 02:32:51 +05:30
|
|
|
k_down:
|
2017-09-13 22:50:27 +05:30
|
|
|
G.current_byte += 16;
|
|
|
|
if (G.current_byte >= G.eof_byte) {
|
|
|
|
move_mapping_further();
|
|
|
|
if (G.current_byte > G.eof_byte) {
|
2017-09-14 14:31:37 +05:30
|
|
|
/* _after_ eof - don't allow this */
|
2017-09-13 22:50:27 +05:30
|
|
|
G.current_byte -= 16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
down:
|
|
|
|
putchar('\n'); /* down one line, possibly scroll screen */
|
2017-09-14 13:12:40 +05:30
|
|
|
G.row++;
|
|
|
|
if (G.row >= G.height) {
|
|
|
|
G.row--;
|
2017-09-13 22:50:27 +05:30
|
|
|
redraw_cur_line();
|
|
|
|
}
|
2017-09-14 02:32:51 +05:30
|
|
|
if (--cnt)
|
|
|
|
goto k_down;
|
2017-09-13 22:50:27 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case KEYCODE_LEFT:
|
|
|
|
if (G.half) {
|
|
|
|
G.half = 0;
|
|
|
|
printf(ESC"[D");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((0xf & (uintptr_t)G.current_byte) == 0) {
|
|
|
|
/* leftmost pos, wrap to prev line */
|
2017-09-14 14:21:12 +05:30
|
|
|
if (G.current_byte == G.baseaddr) {
|
2017-09-14 14:31:37 +05:30
|
|
|
if (!move_mapping_lower())
|
2017-09-14 01:08:55 +05:30
|
|
|
break; /* first line, don't do anything */
|
|
|
|
}
|
2017-09-13 22:50:27 +05:30
|
|
|
G.half = 1;
|
|
|
|
G.current_byte--;
|
|
|
|
printf(ESC"[46C"); /* cursor right 3*15 + 1 chars */
|
|
|
|
goto up;
|
|
|
|
}
|
|
|
|
G.half = 1;
|
|
|
|
G.current_byte--;
|
|
|
|
printf(ESC"[2D");
|
|
|
|
break;
|
2017-09-14 02:32:51 +05:30
|
|
|
case KEYCODE_PAGEUP:
|
|
|
|
cnt = G.height;
|
2017-09-13 22:50:27 +05:30
|
|
|
case KEYCODE_UP:
|
2017-09-14 02:32:51 +05:30
|
|
|
k_up:
|
2017-09-14 14:21:12 +05:30
|
|
|
if ((G.current_byte - G.baseaddr) < 16) {
|
2017-09-14 14:31:37 +05:30
|
|
|
if (!move_mapping_lower())
|
|
|
|
break; /* already at 0, stop */
|
2017-09-13 22:50:27 +05:30
|
|
|
}
|
|
|
|
G.current_byte -= 16;
|
|
|
|
up:
|
2017-09-14 13:12:40 +05:30
|
|
|
if (G.row != 0) {
|
|
|
|
G.row--;
|
2017-09-13 22:50:27 +05:30
|
|
|
printf(ESC"[A"); /* up (won't scroll) */
|
|
|
|
} else {
|
|
|
|
//printf(ESC"[T"); /* scroll up */ - not implemented on Linux VT!
|
|
|
|
printf(ESC"M"); /* scroll up */
|
|
|
|
redraw_cur_line();
|
|
|
|
}
|
2017-09-14 02:32:51 +05:30
|
|
|
if (--cnt)
|
|
|
|
goto k_up;
|
2017-09-13 22:50:27 +05:30
|
|
|
break;
|
2017-09-14 14:21:12 +05:30
|
|
|
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
/* [Enter]: goto specified position */
|
|
|
|
{
|
|
|
|
char buf[sizeof(G.offset)*3 + 4];
|
|
|
|
printf(ESC"[999;1H" CLEAR_TILL_EOL); /* go to last line */
|
2017-09-15 17:30:41 +05:30
|
|
|
if (read_line_input(NULL, "Go to (dec,0Xhex,0oct): ", buf, sizeof(buf)) > 0) {
|
2017-09-14 14:21:12 +05:30
|
|
|
off_t t;
|
2017-09-15 18:53:47 +05:30
|
|
|
unsigned cursor;
|
2017-09-14 14:21:12 +05:30
|
|
|
|
|
|
|
t = bb_strtoull(buf, NULL, 0);
|
|
|
|
if (t >= G.size)
|
|
|
|
t = G.size - 1;
|
2017-09-15 18:53:47 +05:30
|
|
|
cursor = t & (G_pagesize - 1);
|
|
|
|
t -= cursor;
|
2017-09-14 14:21:12 +05:30
|
|
|
if (t < 0)
|
2017-09-15 18:53:47 +05:30
|
|
|
cursor = t = 0;
|
|
|
|
if (t != 0 && cursor < 0x1ff) {
|
2017-09-15 17:30:41 +05:30
|
|
|
/* very close to end of page, possibly to EOF */
|
|
|
|
/* move one page lower */
|
|
|
|
t -= G_pagesize;
|
2017-09-15 18:53:47 +05:30
|
|
|
cursor += G_pagesize;
|
2017-09-15 17:30:41 +05:30
|
|
|
}
|
2017-09-14 14:21:12 +05:30
|
|
|
G.offset = t;
|
2017-09-15 18:53:47 +05:30
|
|
|
remap(cursor);
|
|
|
|
redraw(cursor);
|
2017-09-14 14:21:12 +05:30
|
|
|
break;
|
|
|
|
}
|
2017-09-15 18:53:47 +05:30
|
|
|
/* ^C/EOF/error: fall through to exiting */
|
2017-09-14 14:21:12 +05:30
|
|
|
}
|
2017-09-14 13:12:40 +05:30
|
|
|
case CTRL('X'):
|
|
|
|
restore_term();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
} /* switch */
|
|
|
|
} /* for (;;) */
|
2017-09-13 22:50:27 +05:30
|
|
|
|
2017-09-14 13:12:40 +05:30
|
|
|
/* not reached */
|
2017-09-13 22:50:27 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|