diff --git a/.gitignore b/.gitignore index cba7efc..f1d8bb5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -a.out +hexitor.o diff --git a/Makefile b/Makefile index 963c447..653a060 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,18 @@ +DESTDIR?=/usr/bin +LDIR?=/usr + default: build build: - gcc -Wall main.c -lncurses -ltinfo + gcc -Wall -I$(LDIR)/include -I$(LDIR)/include/ncurses main.c -o hexitor.o -L$(LDIR)/lib -lncurses -ltinfo install: - mkdir -p ${DESTDIR}/usr/bin - cp a.out ${DESTDIR}/usr/bin/hexitor + @echo "Installing in ${DESTDIR}" + mkdir -p ${DESTDIR} + cp hexitor.o ${DESTDIR}/hexitor uninstall: - rm ${DESTDIR}/usr/bin/hexitor + rm ${DESTDIR}/hexitor clean: - rm -f a.out + rm -f hexitor.o diff --git a/README.md b/README.md index ac364fe..2f6c021 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,35 @@ To uninstall: sudo make uninstall ``` +# Installation without root + +If you don't have root access and there is not libncurses installed, +first install ncurses + +```bash +wget https://invisible-mirror.net/archives/ncurses/ncurses-6.2.tar.gz +tar -xzf ncurses-6.2.tar.gz +cd ncurses-6.2 +./configure --prefix=$HOME/ncurses +make +make install +cd .. +rm -rf ncurses-6.2* +``` + +Then compile and install hexitor + +```bash +make LDIR=$HOME/ncurses +make DESTDIR=$HOME/bin install +``` + +to uninstall + +```bash +make DESTDIR=$HOME/bin uninstall +``` + # Usage ### Opening a file diff --git a/main.c b/main.c index a7a8f7b..b5bfaa6 100644 --- a/main.c +++ b/main.c @@ -14,7 +14,6 @@ #define KEY_ESC 27 #define KEY_RETURN 10 -#define KEY_DELETE 127 #define STYLE_ERROR 13 #define STYLE_CURSOR 14 @@ -50,17 +49,17 @@ typedef struct } point; unsigned char* source = NULL; -int source_len; +long unsigned int source_len; char* original_filename; -int cursor_byte = 0; +long unsigned int cursor_byte = 0; int cursor_nibble = 0; -int scroll_start = 0; +long int scroll_start = 0; -int max_x; -int max_y; +long int max_x; +long int max_y; char command[MAX_COMMAND_LEN]; int command_len; @@ -136,39 +135,39 @@ int bytes_per_line() return panes[PANE_HEX].width / CHARS_PER_BYTE; } -int byte_in_line(int byte_offset) +long unsigned int byte_in_line(long unsigned int byte_offset) { return byte_offset / bytes_per_line(); } -int byte_in_column(int byte_offset) +long unsigned int byte_in_column(long unsigned int byte_offset) { return byte_offset % bytes_per_line() * CHARS_PER_BYTE; } -int first_byte_in_line(int line_index) +long unsigned int first_byte_in_line(long unsigned int line_index) { return line_index * bytes_per_line(); } -int last_byte_in_line(int line_index) +long unsigned int last_byte_in_line(long unsigned int line_index) { return first_byte_in_line(line_index + 1) - 1; } -int first_visible_byte() +long unsigned int first_visible_byte() { return first_byte_in_line(scroll_start); } -int last_visible_line() +long unsigned int last_visible_line() { return scroll_start + panes[PANE_HEX].height - 1; } -int last_visible_byte() +long unsigned int last_visible_byte() { - int ret = last_byte_in_line(last_visible_line()); + long unsigned int ret = last_byte_in_line(last_visible_line()); return ret < source_len ? ret : source_len - 1; } @@ -300,7 +299,9 @@ void handle_jump_offset() } // Move cursor to requested offset - cursor_byte = atoi(command + 1); + cursor_byte = atol(command + 1); + if (cursor_byte>=source_len) + cursor_byte = source_len - 1; cursor_nibble = 0; } @@ -358,22 +359,20 @@ void handle_search_next() return; } - int cur = cursor_byte + 1; + long unsigned int cur; + if ((cursor_byte + search_term_len) < source_len) + { + cur = cursor_byte + 1; + } + else + { + cur = 0; + } while (cur != cursor_byte) { - if (cur + search_term_len >= source_len) - { - cur = 0; - } - bool match = true; - if (cur + search_term_len >= source_len) - { - continue; - } - for (int i = 0; i < search_term_len; i++) { if (source[cur + i] != search_term[i]) @@ -390,7 +389,18 @@ void handle_search_next() return; } - cur++; + if ( (cur + search_term_len) < source_len) + { + cur++; + } + else + { + if (cursor_byte > cur) + { + break; + } + cur = 0; + } } set_error("Search term not found"); @@ -403,15 +413,25 @@ void handle_search_previous() return; } - int cur = cursor_byte - 1; - - while (cur != cursor_byte) + long unsigned int cur; + if (cursor_byte) { - if (cur < 0) + if ((cursor_byte + search_term_len - 2) < source_len) + { + cur = cursor_byte - 1; + } + else { cur = source_len - search_term_len; } + } + else + { + cur = source_len - search_term_len; + } + while (cur != cursor_byte) + { bool match = true; for (int i = 0; i < search_term_len; i++) @@ -430,7 +450,18 @@ void handle_search_previous() return; } - cur--; + if (cur) + { + cur--; + } + else + { + cur = source_len - search_term_len; + if (cursor_byte>cur) + { + break; + } + } } set_error("Search term not found"); @@ -493,10 +524,11 @@ void handle_command_event(int event) { switch (event) { - case KEY_ESC: handle_cancel_command(); break; - case KEY_RETURN: handle_submit_command(); break; - case KEY_DELETE: handle_backspace_command(); break; - default: handle_add_to_command(event); break; + + case KEY_ESC: handle_cancel_command(); break; + case KEY_RETURN: handle_submit_command(); break; + case KEY_BACKSPACE: handle_backspace_command(); break; + default: handle_add_to_command(event); break; } } @@ -540,8 +572,11 @@ void handle_key_left() } else { - cursor_nibble = 1; - cursor_byte--; + if(cursor_byte) + { + cursor_byte--; + cursor_nibble = 1; + } } } @@ -553,39 +588,40 @@ void handle_key_right() } else { - cursor_nibble = 0; - cursor_byte++; + if((cursor_byte+1) < source_len) + { + cursor_byte++; + cursor_nibble = 0; + } } } void handle_key_up() { - int temp = cursor_byte - bytes_per_line(); + unsigned int temp = bytes_per_line(); - if (temp >= 0) - { - cursor_byte = temp; - } + if (cursor_byte >= temp) + cursor_byte -= temp; } void handle_key_down() { - int temp = cursor_byte + bytes_per_line(); + unsigned int temp = bytes_per_line(); - if (temp < source_len) - { - cursor_byte = temp; - } + if (cursor_byte+temp < source_len) + cursor_byte += temp; } void handle_previous_byte() { - cursor_byte--; + if(cursor_byte) + cursor_byte--; } void handle_next_byte() { - cursor_byte++; + if((cursor_byte+1) < source_len) + cursor_byte++; } void handle_overwrite(int event) @@ -617,13 +653,26 @@ void handle_overwrite(int event) void handle_page_up() { - cursor_byte -= panes[PANE_HEX].height * bytes_per_line(); + unsigned int temp = panes[PANE_HEX].height * bytes_per_line(); + if (cursor_byte > temp) + cursor_byte -= temp; + else + cursor_byte = 0; } void handle_page_down() { - cursor_byte += panes[PANE_HEX].height * bytes_per_line(); - scroll_start += panes[PANE_HEX].height; + unsigned int temp = panes[PANE_HEX].height * bytes_per_line(); + + if (cursor_byte + temp