Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com> |
| 3 | * |
| 4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 5 | */ |
| 6 | //config:config HEXEDIT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 7 | //config: bool "hexedit (21 kb)" |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 8 | //config: default y |
| 9 | //config: help |
| 10 | //config: Edit file in hexadecimal. |
| 11 | |
| 12 | //applet:IF_HEXEDIT(APPLET(hexedit, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 13 | |
| 14 | //kbuild:lib-$(CONFIG_HEXEDIT) += hexedit.o |
| 15 | |
| 16 | #include "libbb.h" |
| 17 | |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 18 | #define ESC "\033" |
| 19 | #define HOME ESC"[H" |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 20 | #define CLEAR ESC"[J" |
| 21 | #define CLEAR_TILL_EOL ESC"[K" |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 22 | #define SET_ALT_SCR ESC"[?1049h" |
| 23 | #define POP_ALT_SCR ESC"[?1049l" |
| 24 | |
| 25 | #undef CTRL |
| 26 | #define CTRL(c) ((c) & (uint8_t)~0x60) |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 27 | |
| 28 | struct globals { |
| 29 | smallint half; |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 30 | smallint in_read_key; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 31 | int fd; |
| 32 | unsigned height; |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 33 | unsigned row; |
Denys Vlasenko | c7b858f | 2020-12-14 18:49:23 +0100 | [diff] [blame] | 34 | IF_VARIABLE_ARCH_PAGESIZE(unsigned pagesize;) |
| 35 | #define G_pagesize cached_pagesize(G.pagesize) |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 36 | uint8_t *baseaddr; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 37 | uint8_t *current_byte; |
| 38 | uint8_t *eof_byte; |
| 39 | off_t size; |
| 40 | off_t offset; |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 41 | /* needs to be zero-inited, thus keeping it in G: */ |
| 42 | char read_key_buffer[KEYCODE_BUFFER_SIZE]; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 43 | struct termios orig_termios; |
| 44 | }; |
| 45 | #define G (*ptr_to_globals) |
| 46 | #define INIT_G() do { \ |
| 47 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 48 | } while (0) |
| 49 | |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 50 | /* hopefully there aren't arches with PAGE_SIZE > 64k */ |
| 51 | #define G_mapsize (64*1024) |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 52 | |
Denys Vlasenko | d72e804 | 2017-09-14 00:04:16 +0200 | [diff] [blame] | 53 | /* "12ef5670 (xx )*16 _1_3_5_7_9abcdef\n"NUL */ |
| 54 | #define LINEBUF_SIZE (8 + 1 + 3*16 + 16 + 1 + 1 /*paranoia:*/ + 13) |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 55 | |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 56 | static void restore_term(void) |
| 57 | { |
| 58 | tcsetattr_stdin_TCSANOW(&G.orig_termios); |
| 59 | printf(POP_ALT_SCR); |
| 60 | fflush_all(); |
| 61 | } |
| 62 | |
| 63 | static void sig_catcher(int sig) |
| 64 | { |
| 65 | if (!G.in_read_key) { |
| 66 | /* now it's not safe to do I/O, just inform the main loop */ |
| 67 | bb_got_signal = sig; |
| 68 | return; |
| 69 | } |
| 70 | restore_term(); |
| 71 | kill_myself_with_sig(sig); |
| 72 | } |
| 73 | |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 74 | static int format_line(char *hex, uint8_t *data, off_t offset) |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 75 | { |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 76 | int ofs_pos; |
| 77 | char *text; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 78 | uint8_t *end, *end1; |
| 79 | |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 80 | #if 1 |
| 81 | /* Can be more than 4Gb, thus >8 chars, thus use a variable - don't assume 8! */ |
| 82 | ofs_pos = sprintf(hex, "%08"OFF_FMT"x ", offset); |
| 83 | #else |
| 84 | if (offset <= 0xffff) |
| 85 | ofs_pos = sprintf(hex, "%04"OFF_FMT"x ", offset); |
| 86 | else |
| 87 | ofs_pos = sprintf(hex, "%08"OFF_FMT"x ", offset); |
| 88 | #endif |
| 89 | hex += ofs_pos; |
| 90 | |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 91 | text = hex + 16 * 3; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 92 | end1 = data + 15; |
| 93 | if ((G.size - offset) > 0) { |
| 94 | end = end1; |
| 95 | if ((G.size - offset) <= 15) |
| 96 | end = data + (G.size - offset) - 1; |
| 97 | while (data <= end) { |
| 98 | uint8_t c = *data++; |
| 99 | *hex++ = bb_hexdigits_upcase[c >> 4]; |
| 100 | *hex++ = bb_hexdigits_upcase[c & 0xf]; |
| 101 | *hex++ = ' '; |
| 102 | if (c < ' ' || c > 0x7e) |
| 103 | c = '.'; |
| 104 | *text++ = c; |
| 105 | } |
| 106 | } |
| 107 | while (data <= end1) { |
| 108 | *hex++ = ' '; |
| 109 | *hex++ = ' '; |
| 110 | *hex++ = ' '; |
| 111 | *text++ = ' '; |
| 112 | data++; |
| 113 | } |
| 114 | *text = '\0'; |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 115 | |
| 116 | return ofs_pos; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 119 | static void redraw(unsigned cursor) |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 120 | { |
| 121 | uint8_t *data; |
| 122 | off_t offset; |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 123 | unsigned i, pos; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 124 | |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 125 | printf(HOME CLEAR); |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 126 | |
| 127 | /* if cursor is past end of screen, how many lines to move down? */ |
| 128 | i = (cursor / 16) - G.height + 1; |
| 129 | if ((int)i < 0) |
| 130 | i = 0; |
| 131 | |
| 132 | data = G.baseaddr + i * 16; |
| 133 | offset = G.offset + i * 16; |
| 134 | cursor -= i * 16; |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 135 | pos = i = 0; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 136 | while (i < G.height) { |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 137 | char buf[LINEBUF_SIZE]; |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 138 | pos = format_line(buf, data, offset); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 139 | printf( |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 140 | "\r\n%s" + (!i) * 2, /* print \r\n only on 2nd line and later */ |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 141 | buf |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 142 | ); |
| 143 | data += 16; |
| 144 | offset += 16; |
| 145 | i++; |
| 146 | } |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 147 | |
Denys Vlasenko | ca2f831 | 2018-07-23 14:21:07 +0200 | [diff] [blame] | 148 | G.row = cursor / 16; |
| 149 | printf(ESC"[%u;%uH", 1 + G.row, 1 + pos + (cursor & 0xf) * 3); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void redraw_cur_line(void) |
| 153 | { |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 154 | char buf[LINEBUF_SIZE]; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 155 | uint8_t *data; |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 156 | off_t offset; |
| 157 | int column; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 158 | |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 159 | column = (0xf & (uintptr_t)G.current_byte); |
| 160 | data = G.current_byte - column; |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 161 | offset = G.offset + (data - G.baseaddr); |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 162 | |
| 163 | column = column*3 + G.half; |
| 164 | column += format_line(buf, data, offset); |
| 165 | printf("%s" |
| 166 | "\r" |
| 167 | "%.*s", |
| 168 | buf + column, |
| 169 | column, buf |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 170 | ); |
| 171 | } |
| 172 | |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 173 | /* if remappers return 0, no change was done */ |
| 174 | static int remap(unsigned cur_pos) |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 175 | { |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 176 | if (G.baseaddr) |
| 177 | munmap(G.baseaddr, G_mapsize); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 178 | |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 179 | G.baseaddr = mmap(NULL, |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 180 | G_mapsize, |
| 181 | PROT_READ | PROT_WRITE, |
| 182 | MAP_SHARED, |
| 183 | G.fd, |
| 184 | G.offset |
| 185 | ); |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 186 | if (G.baseaddr == MAP_FAILED) { |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 187 | restore_term(); |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 188 | bb_simple_perror_msg_and_die("mmap"); |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 189 | } |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 190 | |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 191 | G.current_byte = G.baseaddr + cur_pos; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 192 | |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 193 | G.eof_byte = G.baseaddr + G_mapsize; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 194 | if ((G.size - G.offset) < G_mapsize) { |
| 195 | /* mapping covers tail of the file */ |
| 196 | /* we do have a mapped byte which is past eof */ |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 197 | G.eof_byte = G.baseaddr + (G.size - G.offset); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 198 | } |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 199 | return 1; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 200 | } |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 201 | static int move_mapping_further(void) |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 202 | { |
| 203 | unsigned pos; |
| 204 | unsigned pagesize; |
| 205 | |
| 206 | if ((G.size - G.offset) < G_mapsize) |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 207 | return 0; /* can't move mapping even further, it's at the end already */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 208 | |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 209 | pagesize = G_pagesize; /* constant on most arches */ |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 210 | pos = G.current_byte - G.baseaddr; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 211 | if (pos >= pagesize) { |
Denys Vlasenko | d72e804 | 2017-09-14 00:04:16 +0200 | [diff] [blame] | 212 | /* move offset up until current position is in 1st page */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 213 | do { |
| 214 | G.offset += pagesize; |
| 215 | if (G.offset == 0) { /* whoops */ |
| 216 | G.offset -= pagesize; |
| 217 | break; |
| 218 | } |
| 219 | pos -= pagesize; |
| 220 | } while (pos >= pagesize); |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 221 | return remap(pos); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 222 | } |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 223 | return 0; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 224 | } |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 225 | static int move_mapping_lower(void) |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 226 | { |
| 227 | unsigned pos; |
| 228 | unsigned pagesize; |
| 229 | |
| 230 | if (G.offset == 0) |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 231 | return 0; /* we are at 0 already */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 232 | |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 233 | pagesize = G_pagesize; /* constant on most arches */ |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 234 | pos = G.current_byte - G.baseaddr; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 235 | |
Denys Vlasenko | d72e804 | 2017-09-14 00:04:16 +0200 | [diff] [blame] | 236 | /* move offset down until current position is in last page */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 237 | pos += pagesize; |
| 238 | while (pos < G_mapsize) { |
| 239 | pos += pagesize; |
| 240 | G.offset -= pagesize; |
| 241 | if (G.offset == 0) |
| 242 | break; |
| 243 | } |
| 244 | pos -= pagesize; |
| 245 | |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 246 | return remap(pos); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 249 | //usage:#define hexedit_trivial_usage |
| 250 | //usage: "FILE" |
| 251 | //usage:#define hexedit_full_usage "\n\n" |
| 252 | //usage: "Edit FILE in hexadecimal" |
| 253 | int hexedit_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 254 | int hexedit_main(int argc UNUSED_PARAM, char **argv) |
| 255 | { |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 256 | INIT_G(); |
Denys Vlasenko | c7b858f | 2020-12-14 18:49:23 +0100 | [diff] [blame] | 257 | INIT_PAGESIZE(G.pagesize); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 258 | |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 259 | get_terminal_width_height(-1, NULL, &G.height); |
Denys Vlasenko | d72e804 | 2017-09-14 00:04:16 +0200 | [diff] [blame] | 260 | if (1) { |
| 261 | /* reduce number of write() syscalls while PgUp/Down: fully buffered output */ |
| 262 | unsigned sz = (G.height | 0xf) * LINEBUF_SIZE; |
| 263 | setvbuf(stdout, xmalloc(sz), _IOFBF, sz); |
| 264 | } |
| 265 | |
| 266 | getopt32(argv, "^" "" "\0" "=1"/*one arg*/); |
| 267 | argv += optind; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 268 | |
| 269 | G.fd = xopen(*argv, O_RDWR); |
| 270 | G.size = xlseek(G.fd, 0, SEEK_END); |
| 271 | |
| 272 | /* TERMIOS_RAW_CRNL suppresses \n -> \r\n translation, helps with down-arrow */ |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 273 | printf(SET_ALT_SCR); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 274 | set_termios_to_raw(STDIN_FILENO, &G.orig_termios, TERMIOS_RAW_CRNL); |
| 275 | bb_signals(BB_FATAL_SIGS, sig_catcher); |
| 276 | |
| 277 | remap(0); |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 278 | redraw(0); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 279 | |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 280 | //TODO: //Home/End: start/end of line; '<'/'>': start/end of file |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 281 | //Backspace: undo |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 282 | //Ctrl-L: redraw |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 283 | //Ctrl-Z: suspend |
| 284 | //'/', Ctrl-S: search |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 285 | //TODO: detect window resize |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 286 | |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 287 | for (;;) { |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 288 | unsigned cnt; |
Denys Vlasenko | 44cb166 | 2017-09-14 11:20:36 +0200 | [diff] [blame] | 289 | int32_t key = key; /* for compiler */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 290 | uint8_t byte; |
| 291 | |
| 292 | fflush_all(); |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 293 | G.in_read_key = 1; |
| 294 | if (!bb_got_signal) |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 295 | key = safe_read_key(STDIN_FILENO, G.read_key_buffer, -1); |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 296 | G.in_read_key = 0; |
| 297 | if (bb_got_signal) |
| 298 | key = CTRL('X'); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 299 | |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 300 | cnt = 1; |
Denys Vlasenko | 44cb166 | 2017-09-14 11:20:36 +0200 | [diff] [blame] | 301 | if ((unsigned)(key - 'A') <= 'Z' - 'A') |
| 302 | key |= 0x20; /* convert A-Z to a-z */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 303 | switch (key) { |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 304 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': |
Denys Vlasenko | 44cb166 | 2017-09-14 11:20:36 +0200 | [diff] [blame] | 305 | /* convert to '0'+10...15 */ |
| 306 | key = key - ('a' - '0' - 10); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 307 | /* fall through */ |
| 308 | case '0': case '1': case '2': case '3': case '4': |
| 309 | case '5': case '6': case '7': case '8': case '9': |
| 310 | if (G.current_byte == G.eof_byte) { |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 311 | if (!move_mapping_further()) { |
| 312 | /* already at EOF; extend the file */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 313 | if (++G.size <= 0 /* overflow? */ |
| 314 | || ftruncate(G.fd, G.size) != 0 /* error extending? (e.g. block dev) */ |
| 315 | ) { |
| 316 | G.size--; |
| 317 | break; |
| 318 | } |
| 319 | G.eof_byte++; |
| 320 | } |
| 321 | } |
| 322 | key -= '0'; |
| 323 | byte = *G.current_byte & 0xf0; |
| 324 | if (!G.half) { |
| 325 | byte = *G.current_byte & 0x0f; |
| 326 | key <<= 4; |
| 327 | } |
| 328 | *G.current_byte = byte + key; |
| 329 | /* can't just print one updated hex char: need to update right-hand ASCII too */ |
| 330 | redraw_cur_line(); |
| 331 | /* fall through */ |
| 332 | case KEYCODE_RIGHT: |
| 333 | if (G.current_byte == G.eof_byte) |
| 334 | break; /* eof - don't allow going past it */ |
| 335 | byte = *G.current_byte; |
| 336 | if (!G.half) { |
| 337 | G.half = 1; |
| 338 | putchar(bb_hexdigits_upcase[byte >> 4]); |
| 339 | } else { |
| 340 | G.half = 0; |
| 341 | G.current_byte++; |
| 342 | if ((0xf & (uintptr_t)G.current_byte) == 0) { |
| 343 | /* rightmost pos, wrap to next line */ |
| 344 | if (G.current_byte == G.eof_byte) |
| 345 | move_mapping_further(); |
| 346 | printf(ESC"[46D"); /* cursor left 3*15 + 1 chars */ |
| 347 | goto down; |
| 348 | } |
| 349 | putchar(bb_hexdigits_upcase[byte & 0xf]); |
| 350 | putchar(' '); |
| 351 | } |
| 352 | break; |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 353 | case KEYCODE_PAGEDOWN: |
| 354 | cnt = G.height; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 355 | case KEYCODE_DOWN: |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 356 | k_down: |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 357 | G.current_byte += 16; |
| 358 | if (G.current_byte >= G.eof_byte) { |
| 359 | move_mapping_further(); |
| 360 | if (G.current_byte > G.eof_byte) { |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 361 | /* _after_ eof - don't allow this */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 362 | G.current_byte -= 16; |
Denys Vlasenko | ca2f831 | 2018-07-23 14:21:07 +0200 | [diff] [blame] | 363 | if (G.current_byte < G.baseaddr) |
| 364 | move_mapping_lower(); |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | } |
| 368 | down: |
| 369 | putchar('\n'); /* down one line, possibly scroll screen */ |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 370 | G.row++; |
| 371 | if (G.row >= G.height) { |
| 372 | G.row--; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 373 | redraw_cur_line(); |
| 374 | } |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 375 | if (--cnt) |
| 376 | goto k_down; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 377 | break; |
| 378 | |
| 379 | case KEYCODE_LEFT: |
| 380 | if (G.half) { |
| 381 | G.half = 0; |
| 382 | printf(ESC"[D"); |
| 383 | break; |
| 384 | } |
| 385 | if ((0xf & (uintptr_t)G.current_byte) == 0) { |
| 386 | /* leftmost pos, wrap to prev line */ |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 387 | if (G.current_byte == G.baseaddr) { |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 388 | if (!move_mapping_lower()) |
Denys Vlasenko | 136946c | 2017-09-13 21:38:55 +0200 | [diff] [blame] | 389 | break; /* first line, don't do anything */ |
| 390 | } |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 391 | G.half = 1; |
| 392 | G.current_byte--; |
| 393 | printf(ESC"[46C"); /* cursor right 3*15 + 1 chars */ |
| 394 | goto up; |
| 395 | } |
| 396 | G.half = 1; |
| 397 | G.current_byte--; |
| 398 | printf(ESC"[2D"); |
| 399 | break; |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 400 | case KEYCODE_PAGEUP: |
| 401 | cnt = G.height; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 402 | case KEYCODE_UP: |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 403 | k_up: |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 404 | if ((G.current_byte - G.baseaddr) < 16) { |
Denys Vlasenko | 8838c6d | 2017-09-14 11:01:37 +0200 | [diff] [blame] | 405 | if (!move_mapping_lower()) |
| 406 | break; /* already at 0, stop */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 407 | } |
| 408 | G.current_byte -= 16; |
| 409 | up: |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 410 | if (G.row != 0) { |
| 411 | G.row--; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 412 | printf(ESC"[A"); /* up (won't scroll) */ |
| 413 | } else { |
| 414 | //printf(ESC"[T"); /* scroll up */ - not implemented on Linux VT! |
| 415 | printf(ESC"M"); /* scroll up */ |
| 416 | redraw_cur_line(); |
| 417 | } |
Denys Vlasenko | 6264301 | 2017-09-13 23:02:51 +0200 | [diff] [blame] | 418 | if (--cnt) |
| 419 | goto k_up; |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 420 | break; |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 421 | |
| 422 | case '\n': |
| 423 | case '\r': |
| 424 | /* [Enter]: goto specified position */ |
| 425 | { |
| 426 | char buf[sizeof(G.offset)*3 + 4]; |
| 427 | printf(ESC"[999;1H" CLEAR_TILL_EOL); /* go to last line */ |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 428 | if (read_line_input(NULL, "Go to (dec,0Xhex,0oct): ", buf, sizeof(buf)) > 0) { |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 429 | off_t t; |
Denys Vlasenko | e58b447 | 2017-09-15 15:23:47 +0200 | [diff] [blame] | 430 | unsigned cursor; |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 431 | |
| 432 | t = bb_strtoull(buf, NULL, 0); |
| 433 | if (t >= G.size) |
| 434 | t = G.size - 1; |
Denys Vlasenko | e58b447 | 2017-09-15 15:23:47 +0200 | [diff] [blame] | 435 | cursor = t & (G_pagesize - 1); |
| 436 | t -= cursor; |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 437 | if (t < 0) |
Denys Vlasenko | e58b447 | 2017-09-15 15:23:47 +0200 | [diff] [blame] | 438 | cursor = t = 0; |
| 439 | if (t != 0 && cursor < 0x1ff) { |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 440 | /* very close to end of page, possibly to EOF */ |
| 441 | /* move one page lower */ |
| 442 | t -= G_pagesize; |
Denys Vlasenko | e58b447 | 2017-09-15 15:23:47 +0200 | [diff] [blame] | 443 | cursor += G_pagesize; |
Denys Vlasenko | f3fa865 | 2017-09-15 14:00:41 +0200 | [diff] [blame] | 444 | } |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 445 | G.offset = t; |
Denys Vlasenko | e58b447 | 2017-09-15 15:23:47 +0200 | [diff] [blame] | 446 | remap(cursor); |
| 447 | redraw(cursor); |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 448 | break; |
| 449 | } |
Denys Vlasenko | e58b447 | 2017-09-15 15:23:47 +0200 | [diff] [blame] | 450 | /* ^C/EOF/error: fall through to exiting */ |
Denys Vlasenko | d54f58d | 2017-09-14 10:51:12 +0200 | [diff] [blame] | 451 | } |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 452 | case CTRL('X'): |
| 453 | restore_term(); |
| 454 | return EXIT_SUCCESS; |
| 455 | } /* switch */ |
| 456 | } /* for (;;) */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 457 | |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame] | 458 | /* not reached */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 459 | return EXIT_SUCCESS; |
| 460 | } |