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