Author | Jamozed <[email protected]> |
Date | 2021-11-30 01:56:57 |
Commit | 0a8f8638a20c1b465ebb108f2ef89fd47936a7d4 |
Parent | 53aa0ac5559cadba6df557f8b583a500eafb788e |
Add line and hist typedefs
Diffstat
M | src/lineread.c | | | 120 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 files changed, 60 insertions, 60 deletions
diff --git a/src/lineread.c b/src/lineread.c index 81fe185..d2066bf 100644 --- a/src/lineread.c +++ b/src/lineread.c @@ -46,53 +46,53 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include <stdlib.h> #include <string.h> -struct line { - char *s; size_t sp, sl, sc; - char *prompt; size_t pl; size_t cols; -}; +typedef struct { + char *s; UINT sp, sl, sc; + char *prompt; UINT pl, cols; +} line; -struct hist { - struct line *a; size_t ap, ah, at, al, ac; -}; +typedef struct { + line *a; UINT ap, ah, at, al, ac; +} hist; static struct termios tco, tcn; static bool rawflag = false, ateflag = false; -static struct hist h = { NULL, 0, 0, 0, 0 }; +static hist h = { NULL, 0, 0, 0, 0 }; static char *linentty(void); static char *lineedit(void); -static void line_esc(struct line *l, register int c); +static void line_esc(line *l, register int c); static inline void tcraw(void); static inline void tcrestore(void); static size_t getcols(void); -static void clearscreen(struct line *l); - -static void line_refresh(struct line *l); -static void line_reset(struct line *l); -static void line_move_left(struct line *l); -static void line_move_right(struct line *l); -static void line_move_word_home(struct line *l); -static void line_move_word_end(struct line *l); -static void line_move_home(struct line *l); -static void line_move_end(struct line *l); -static void line_push(struct line *l, char c); -static int line_insert(struct line *l, char c); -static void line_backspace(struct line *l); -static void line_delete(struct line *l); -static void line_delete_word_home(struct line *l); -static void line_delete_word_end(struct line *l); -static void line_delete_home(struct line *l); -static void line_delete_end(struct line *l); - -static int hist_init(struct hist *h); -static void hist_free(struct hist *h); -static void hist_move_prior(struct hist *h); -static void hist_move_next(struct hist *h); -static void hist_move_home(struct hist *h); -static void hist_move_end(struct hist *h); -static void hist_push(struct hist *h, struct line l); +static void clearscreen(line *l); + +static void line_refresh(line *l); +static void line_reset(line *l); +static void line_move_left(line *l); +static void line_move_right(line *l); +static void line_move_word_home(line *l); +static void line_move_word_end(line *l); +static void line_move_home(line *l); +static void line_move_end(line *l); +static void line_push(line *l, char c); +static int line_insert(line *l, char c); +static void line_backspace(line *l); +static void line_delete(line *l); +static void line_delete_word_home(line *l); +static void line_delete_word_end(line *l); +static void line_delete_home(line *l); +static void line_delete_end(line *l); + +static int hist_init(hist *h); +static void hist_free(hist *h); +static void hist_move_prior(hist *h); +static void hist_move_next(hist *h); +static void hist_move_home(hist *h); +static void hist_move_end(hist *h); +static void hist_push(hist *h, line l); /* Read a line from stdin */ char *lineread(void) { @@ -105,7 +105,7 @@ void linefree(void) { hist_free(&h); } /* Read from a non-terminal stdin */ static char *linentty(void) { - struct line l; register char *r; + line l; register char *r; l.sp = 0; l.sl = 0; l.sc = 1024; if (!(l.s = malloc(l.sc * sizeof (*l.s)))) { return NULL; } @@ -130,7 +130,7 @@ static char *lineedit(void) { { if (h.a == NULL) { if (hist_init(&h)) { return NULL; } } - struct line m = { NULL, 0, 0, 1024 }; + line m = { NULL, 0, 0, 1024 }; m.prompt = "$ "; m.pl = strlen(m.prompt); if (!(m.cols = getcols())) { return NULL; } @@ -202,7 +202,7 @@ ret:; #undef l // h.a[h.ap] /* Handle an extended ^[ sequence */ -static void line_esc(struct line *l, register int c) { +static void line_esc(line *l, register int c) { switch (c) { case '2': switch ((c = fgetc(stdin))) { case '~': { /* Insert char code */ } return; // INSERT @@ -251,12 +251,12 @@ static size_t getcols(void) { } /* Clear the screen */ -static void clearscreen(struct line *l) { +static void clearscreen(line *l) { fputs("\x1B[H\x1B[2J", stdout); line_refresh(l); return; } /* Refresh line */ -static void line_refresh(struct line *l) { +static void line_refresh(line *l) { fputs("\r\x1B[0K", stdout); fputs(l->prompt, stdout); fputs(l->s, stdout); fprintf(stdout, "\r\x1B[%zuC", l->pl + l->sp); @@ -264,54 +264,54 @@ static void line_refresh(struct line *l) { } /* Reset line */ -static void line_reset(struct line *l) { +static void line_reset(line *l) { l->s[0] = 0; l->sp = 0; l->sl = 0; fputs("^C\n", stdout); line_refresh(l); return; } /* Move cursor left */ -static void line_move_left(struct line *l) { +static void line_move_left(line *l) { if (l->sp) { --l->sp; fputs("\x1B[D", stdout); } return; } /* Move cursor right */ -static void line_move_right(struct line *l) { +static void line_move_right(line *l) { if (l->sp != l->sl) { ++l->sp; fputs("\x1B[C", stdout); } return; } /* Move cursor to the word home */ -static void line_move_word_home(struct line *l) { +static void line_move_word_home(line *l) { for (; l->sp && l->s[l->sp - 1] == ' '; --l->sp); for (; l->sp && l->s[l->sp - 1] != ' '; --l->sp); line_refresh(l); return; } /* Move cursor to the word end */ -static void line_move_word_end(struct line *l) { +static void line_move_word_end(line *l) { for (; l->sp != l->sl && l->s[l->sp] == ' '; ++l->sp); for (; l->sp != l->sl && l->s[l->sp] != ' '; ++l->sp); line_refresh(l); return; } /* Move cursor to the line home */ -static void line_move_home(struct line *l) { +static void line_move_home(line *l) { l->sp = 0; line_refresh(l); return; } /* Move cursor to the line end */ -static void line_move_end(struct line *l) { +static void line_move_end(line *l) { l->sp = l->sl; line_refresh(l); return; } /* Push character onto end of line */ -static void line_push(struct line *l, char c) { +static void line_push(line *l, char c) { if (l->sl + 1 > l->sc) { l->sc *= 2; l->s = realloc(l->s, l->sc * sizeof (*l->s)); } l->s[l->sl] = c; l->s[++l->sl] = 0; return; } /* Insert character at cursor */ -static int line_insert(struct line *l, char c) { +static int line_insert(line *l, char c) { if (l->sl + 1 == l->sc) { /* TODO expand string */ } if (l->sp == l->sl) { // Cursor is at line end l->s[l->sp++] = c; l->s[++l->sl] = 0; @@ -324,7 +324,7 @@ static int line_insert(struct line *l, char c) { } /* Delete character before cursor */ -static void line_backspace(struct line *l) { +static void line_backspace(line *l) { if (l->sp) { memmove(l->s + l->sp - 1, l->s + l->sp, l->sl - l->sp); --l->sp; l->s[--l->sl] = 0; line_refresh(l); @@ -332,7 +332,7 @@ static void line_backspace(struct line *l) { } /* Delete character at cursor */ -static void line_delete(struct line *l) { +static void line_delete(line *l) { if (l->sp != l->sl) { memmove(l->s + l->sp, l->s + l->sp + 1, l->sl - l->sp); l->s[--l->sl] = 0; line_refresh(l); @@ -340,7 +340,7 @@ static void line_delete(struct line *l) { } /* Delete the word preceeding the cursor */ -static void line_delete_word_home(struct line *l) { +static void line_delete_word_home(line *l) { size_t p = l->sp; for (; l->sp && l->s[l->sp - 1] == ' '; --l->sp); @@ -351,7 +351,7 @@ static void line_delete_word_home(struct line *l) { } /* Delete the word following the cursor */ -static void line_delete_word_end(struct line *l) { +static void line_delete_word_end(line *l) { size_t p = l->sp; for (; p != l->sl && l->s[p] == ' '; ++p); @@ -362,55 +362,55 @@ static void line_delete_word_end(struct line *l) { } /* Delete characters from cursor to home */ -static void line_delete_home(struct line *l) { +static void line_delete_home(line *l) { memmove(l->s, l->s + l->sp, l->sl - l->sp + 1); l->sl -= l->sp; l->sp = 0; line_refresh(l); return; } /* Delete characters from cursor to end */ -static void line_delete_end(struct line *l) { +static void line_delete_end(line *l) { l->s[(l->sl = l->sp)] = 0; line_refresh(l); return; } /* Initialise history */ -static int hist_init(struct hist *h) { +static int hist_init(hist *h) { h->ap = 0; h->ah = 0; h->at = 0; h->al = 0; h->ac = conf_histsize + 1; if (!(h->a = assert_calloc(h->ac, sizeof (*h->a)))) { return 1; } /* TODO load history from file */ return 0; } /* Free history */ -static void hist_free(struct hist *h) { +static void hist_free(hist *h) { for (size_t i = 0; i != h->ac; ++i) { free(h->a[i].s); } free(h->a); } /* Move backwards in history */ -static void hist_move_prior(struct hist *h) { +static void hist_move_prior(hist *h) { if (h->ap != h->ah) { h->ap == 0 ? h->ap = h->ac - 1 : --h->ap; } line_refresh(&h->a[h->ap]); return; } /* Move forwards in history */ -static void hist_move_next(struct hist *h) { +static void hist_move_next(hist *h) { register size_t an = h->ap == h->ac - 1 ? 0 : h->ap + 1; if (an != h->at) { h->ap = an; } line_refresh(&h->a[h->ap]); return; } /* Move to the start of history */ -static void hist_move_home(struct hist *h) { +static void hist_move_home(hist *h) { /* TODO */ h->ap = h->ah; line_refresh(&h->a[h->ap]); return; } /* Move to the end of history */ -static void hist_move_end(struct hist *h) { +static void hist_move_end(hist *h) { /* TODO */ // h->ap = h->at; lineRefresh(&h->a[h->ap]); return; } /* Push a line onto the end of history */ -static void hist_push(struct hist *h, struct line l) { +static void hist_push(hist *h, line l) { if (h->al != h->ac) { ++h->al; h->ap = h->at; if (++h->at == h->ac) { h->at = 0; } }