sled

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

чување f129b454de2a42b83ee4bbe1a901ec6a695f2a0b
родитељ 8c685a78a18c96a68d360569c54703bf5647f924
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sat,  8 Jun 2024 15:08:46 +0200

Bugfix: copy copies the rest of the line to paste buffer; adjust 
offsets to new *str*cpy logic

Diffstat:
MTODO | 5-----
MTODO.done | 6++++++
Msled.c | 25+++++++++++++++----------
Mutil.h | 8++++++--
измењених датотека: 4, додавања: 27(+), брисања: 17(-)

diff --git a/TODO b/TODO @@ -3,11 +3,6 @@ TODO [ ] *str*cpy -> memccpy? -[~] Bugfix: cursor after paste when on last line - - [ ] Bugfix: copy copies the whole line to paste buffer - -[x] Bugfix: sled /; sled /etc; report "No such file or directory" - [ ] Check (and possibly remove) leftover constants < > Make allocation errors prompt to save the file before exiting? diff --git a/TODO.done b/TODO.done @@ -1,6 +1,12 @@ Done or canceled todos ====================== +[x] Bugfix: cursor after paste when on last line + - [x] Bugfix: copy copies the whole line to paste buffer + - [x] Adjust offsets for new *str*cpy logic + +[x] Bugfix: sled /; sled /etc; report "No such file or directory" + [x] Deal with long filenames (add ... at the end and leave space for *) [x] Switch to strlcpy, strlcat; check u8_*/u32_* diff --git a/sled.c b/sled.c @@ -1902,7 +1902,7 @@ paste_from_paste(struct DrawState* state) STRLCPY(errors[ERR_CANT_ALLOC], state->error, LINE_DEFAULT); goto paste_from_paste_cleanup; } - u32_strncpy(newtext, line->text, state->coord.col + 1); + u32_strncpy(newtext, line->text, state->coord.col); u32_strncat(newtext, state->paste[0].text, newsize); assign_coord(&newpos, state->coord); @@ -1966,8 +1966,8 @@ paste_from_paste(struct DrawState* state) u32_strncat(newtext, rest, newsize); U32_SAFE_COPY(line->text, ptext, line->size, newtext, newsize, paste_from_paste_cleanup); - line->length = u32_strlen(line->text); - newpos.col = state->paste[state->paste_count - 1].length; + line->length = u32_strlen(line->text); + newpos.col = state->paste[state->paste_count - 1].length; newpos.row += state->paste_count - 1; } @@ -2312,16 +2312,20 @@ simple_yank(struct DrawState* state) if (state->paste_count == 1) { U32_SAFE_COPY(state->paste[0].text, ptext, state->paste[0].size, - state->buffer[ssr].text + ssc, sec - ssc + 1, + state->buffer[ssr].text + ssc, sec - ssc, simple_yank_alloc_error); state->paste[0].length = u32_strlen(state->paste[0].text); } else { + size_t first_line_len + = u32_strlen(state->buffer[ssr].text + ssc); + U32_SAFE_COPY(state->paste[0].text, ptext, state->paste[0].size, - state->buffer[ssr].text + ssc, sec - ssc + 1, + state->buffer[ssr].text + ssc, first_line_len, simple_yank_alloc_error); - state->paste[0].length = u32_strlen(state->paste[0].text); + state->paste[0].length = first_line_len; + for (size_t row = 1; row < state->paste_count - 1; row++) { U32_SAFE_COPY(state->paste[row].text, ptext, @@ -2332,10 +2336,10 @@ simple_yank(struct DrawState* state) state->paste[row].length = u32_strlen(state->paste[row].text); } + U32_SAFE_COPY(state->paste[state->paste_count - 1].text, ptext, state->paste[state->paste_count - 1].size, - state->buffer[ser].text, sec + 1, - simple_yank_alloc_error); + state->buffer[ser].text, sec, simple_yank_alloc_error); state->paste[state->paste_count - 1].length = u32_strlen(state->paste[state->paste_count - 1].text); } @@ -2631,9 +2635,10 @@ main(int argc, char** argv) { if (!(pdir = realloc(dir, limits[L_PATH_MAX]))) { - free(filename); - return print_error(1, + result = 1; + print_error(1, "Allocation failed (out of memory?)"); + goto general_error; } dir = pdir; *dir = 0; diff --git a/util.h b/util.h @@ -59,24 +59,28 @@ * - Make sure the size of the destination string is large enough to copy, and * copy * - Sizes are in bytes + * - We explicitly use fromsize in u8_strncpy, because we ensured that tosize is + * at least as big, and to allow a subset of from to be copied */ #define U8_SAFE_COPY(to, temp, tosize, from, fromsize, label) \ do \ { \ ENSURE_SIZE(to, temp, tosize, fromsize, fromsize, label, char); \ - u8_strncpy(to, from, tosize); \ + u8_strncpy(to, from, fromsize); \ } while (0) /* * - Make sure the size of the destination string is large enough to copy, and * copy * - Sizes are in uint32_t units + * - We explicitly use fromsize in u32_strncpy, because we ensured that tosize + * is at least as big, and to allow a subset of from to be copied */ #define U32_SAFE_COPY(to, temp, tosize, from, fromsize, label) \ do \ { \ ENSURE_SIZE(to, temp, tosize, fromsize, fromsize, label, \ uint32_t); \ - u32_strncpy(to, from, tosize); \ + u32_strncpy(to, from, fromsize); \ } while (0) enum {