sled

Једноставни уређивач текста
git clone https://git.sr.ht/~strahinja/sled
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување dc80ec8d36e9feeeb06c3af7439e5ba69b45cccb
родитељ 582c5af07589e2b5f5834456d14a68e4d9e755ae
Аутор: Страхиња Радић <sr@strahinja.org>
Датум:   Sun, 27 Oct 2024 11:42:07 +0100

Add open_binary option (by default disabled)

Diffstat:
MTODO | 2+-
Mconfig.def.h | 2++
Mdraw.c | 3++-
Msled.1.in | 5+++++
Msled.c | 20+++++++++++++++++++-
Mutil.h | 3++-
измењених датотека: 6, додавања: 31(+), брисања: 4(-)

diff --git a/TODO b/TODO @@ -1,7 +1,7 @@ TODO ==== -[ ] Recognize binary files (having NUL bytes) and refuse to open them; possibly +[x] Recognize binary files (having NUL bytes) and refuse to open them; possibly make it a config.h option? [ ] Fix removing blank lines when wrapping selected text containing multiple diff --git a/config.def.h b/config.def.h @@ -36,6 +36,7 @@ extern const int create_backups; extern const int enable_wrap; extern const int line_numbers; extern const int line_numbers_width; +extern const int open_binary; extern const int wrap_width; #ifdef SLED_C_GLOBALS @@ -48,6 +49,7 @@ const char* backup_suffix = "~"; const int create_backups = 1; /* comment out to not create backups */ const int enable_wrap = 1; const int line_numbers = 0; /* draw line numbers? initial value */ +const int open_binary = 0; /* open binary files? */ /* - ideally, wrap_width + line_numbers_width (automatic) <= 80 */ /* - wrap width of 72 is recommended on https://useplaintext.email/ */ const int wrap_width = 72; diff --git a/draw.c b/draw.c @@ -70,7 +70,8 @@ const char* errors[] = { [ERR_CANT_COPY] = "Cannot create the backup file", [ERR_INPUT_NONNUM] = "Input must be numerical", [ERR_INVALID_NUM] = "Invalid input", - [ERR_NOT_IMP] = "Function not implemented/configured"}; + [ERR_NOT_IMP] = "Function not implemented/configured", + [ERR_BINARY] = "Not opening a binary file"}; const char* actions[] = { [ACT_WRITING] = "Writing...", [ACT_READING] = "Reading...", diff --git a/sled.1.in b/sled.1.in @@ -213,4 +213,9 @@ This is a design decision. If backup creation is enabled .Pq Dv create_backups No in Pa config.h , the directory of the edited file must be writable by the user to open the file. +.It +Opening binary files (containing NUL bytes) while +.Dv open_binary +is set to a nonzero value can lead to an incorrectly drawn display. +This was the reason for introducing that option, by default set to zero. .El diff --git a/sled.c b/sled.c @@ -44,6 +44,7 @@ enum { LOAD_ERR_NONE = 0, LOAD_ERR_CANT_ALLOC = 1, LOAD_ERR_CANT_OPEN_FILE = 2, + LOAD_ERR_BINARY = 3 }; enum { @@ -1350,13 +1351,17 @@ load_file(struct DrawState* state) while (!feof(input)) { - char* eol = NULL; + char* eol = NULL; + char* nulbyte = NULL; if (!fgets(input_line, input_size, input)) continue; eol = strchr(input_line, '\n'); if (eol) *eol = 0; + nulbyte = strchr(input_line, 0); + if (!open_binary && nulbyte && (nulbyte != eol)) + goto load_file_binary; if (state->rows_count + 1 == state->buffer_size) { @@ -1405,6 +1410,10 @@ load_file(struct DrawState* state) load_file_alloc_error: free(input_line); return LOAD_ERR_CANT_ALLOC; + +load_file_binary: + free(input_line); + return LOAD_ERR_BINARY; } static int @@ -2023,6 +2032,11 @@ reread_file(struct DrawState* state) MEMCCPY(state->error, errors[ERR_CANT_OPEN], LINE_DEFAULT, temp); state->running = 0; break; + case LOAD_ERR_BINARY: + MEMCCPY(state->error, errors[ERR_BINARY], LINE_DEFAULT, temp); + errno = LOAD_ERR_BINARY; + state->running = 0; + break; default: recalculate_line_numbers(state); } @@ -2952,6 +2966,7 @@ main(int argc, char** argv) printf(" enable_wrap:\t\t%s\n", enable_wrap ? "set" : "unset"); printf(" line_numbers:\t\t%s\n", line_numbers ? "set" : "unset"); + printf(" open_binary:\t\t%s\n", open_binary ? "set" : "unset"); printf(" wrap_width:\t\t%d\n", wrap_width); return 0; } @@ -3208,6 +3223,9 @@ main_skip_backups: case LOAD_ERR_CANT_OPEN_FILE: result = print_error(result, "Cannot open file `%s'", filename); goto main_cleanup; + case LOAD_ERR_BINARY: + result = print_error(result, "Not opening a binary file"); + goto main_cleanup; default:; } goto init_termbox; diff --git a/util.h b/util.h @@ -111,7 +111,8 @@ enum { ERR_CANT_COPY, ERR_INPUT_NONNUM, ERR_INVALID_NUM, - ERR_NOT_IMP + ERR_NOT_IMP, + ERR_BINARY }; enum { ACT_WRITING, ACT_READING, ACT_WRAPPING };