Author | Jamozed <[email protected]> |
Date | 2021-09-07 12:53:26 |
Commit | 3c5297e05b3c0f1f3c88b3f1ec5b6540c4f9bf54 |
Parent | 66d0b960e79836eac1f06426b1736806c7a3dbb7 |
Reformat lineread functions
Diffstat
M | src/lineread.c | | | 239 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
M | src/lineread.h | | | 4 | +++- |
M | src/main.c | | | 4 | ++-- |
3 files changed, 125 insertions, 122 deletions
diff --git a/src/lineread.c b/src/lineread.c index ba59415..e80a3a2 100644 --- a/src/lineread.c +++ b/src/lineread.c @@ -33,6 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #include "conf.h" #include "lineread.h" +#include "cll/cll.h" #include "cll/error.h" #include <sys/ioctl.h> @@ -60,74 +61,74 @@ static bool rawflag = false, ateflag = false; static struct hist h = { NULL, 0, 0, 0, 0 }; -static char *linentty(void); -static char *lineedit(void); -static void lineESC(struct line *l, register int c); +static u8 *linentty(void); +static u8 *lineedit(void); +static void line_esc(struct 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 lineRefresh(struct line *l); -static void lineReset(struct line *l); -static void lineMoveLeft(struct line *l); -static void lineMoveRight(struct line *l); -static void lineMoveWordHome(struct line *l); -static void lineMoveWordEnd(struct line *l); -static void lineMoveHome(struct line *l); -static void lineMoveEnd(struct line *l); -static void linePush(struct line *l, char c); -static int lineInsert(struct line *l, char c); -static void lineBackspace(struct line *l); -static void lineDelete(struct line *l); -static void lineDeleteWordHome(struct line *l); -static void lineDeleteWordEnd(struct line *l); -static void lineDeleteHome(struct line *l); -static void lineDeleteEnd(struct line *l); - -static int histInit(struct hist *h); -static void histFree(struct hist *h); -static void histMovePrior(struct hist *h); -static void histMoveNext(struct hist *h); -static void histMoveHome(struct hist *h); -static void histMoveEnd(struct hist *h); -static void histPush(struct hist *h, 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); /* Read a line from stdin */ -char *lineread(void) { +u8 *lineread(void) { if (!isatty(STDIN_FILENO)) { errno = 0; return linentty(); } else { return lineedit(); } } /* Free memory allocated by lineread */ -void linefree(void) { histFree(&h); } +void linefree(void) { hist_free(&h); } /* Read from a non-terminal stdin */ -static char *linentty(void) { - struct line l; register char *r; +static u8 *linentty(void) { + struct line l; register u8 *r; l.sp = 0; l.sl = 0; l.sc = 1024; if (!(l.s = malloc(l.sc * sizeof (*l.s)))) { return NULL; } for (register int c; (c = fgetc(stdin));) { if (c == EOF) { if (l.sl) { break; } r = NULL; goto ret; } - else { linePush(&l, c); continue; } + else { line_push(&l, c); } continue; } end:; - r = strndup(l.s, l.sl); + r = (u8 *)strndup(l.s, l.sl); ret:; free(l.s); return r; } #define l h.a[h.ap] /* Dynamically read a line from stdin */ -static char *lineedit(void) { - register char *r; +static u8 *lineedit(void) { + register u8 *r; { - if (h.a == NULL) { if (histInit(&h)) { return NULL; } } + if (h.a == NULL) { if (hist_init(&h)) { return NULL; } } struct line m = { NULL, 0, 0, 1024 }; m.prompt = "$ "; m.pl = strlen(m.prompt); @@ -135,7 +136,7 @@ static char *lineedit(void) { if (!(m.cols = getcols())) { return NULL; } if (!(m.s = malloc(m.sc * sizeof (*m.s)))) { return NULL; } m.s[0] = 0; - histPush(&h, m); + hist_push(&h, m); } tcraw(); fputs(l.prompt, stdout); @@ -145,74 +146,74 @@ static char *lineedit(void) { // printf("%02X\n", c); continue; switch (c) { - case '\x01': { lineMoveHome(&l); continue; } // CTRL + A - case '\x02': { lineMoveLeft(&l); continue; } // CTRL + B - case '\x03': { lineReset(&l); continue; } // CTRL + C - case '\x04': { r = NULL; goto ret; } // CTRL + D - case '\x05': { lineMoveEnd(&l); continue; } // CTRL + E - case '\x06': { lineMoveRight(&l); continue; } // CTRL + F - case '\x07': { continue; } // IGNORE CTRL + G - case '\x08': { lineBackspace(&l); continue; } // CTRL + H - case '\x09': { continue; } // IGNORE CTRL + I - case '\x0A': { goto end; } // CTRL + J - case '\x0B': { lineDeleteEnd(&l); continue; } // CTRL + K - case '\x0C': { clearscreen(&l); continue; } // CTRL + L - case '\x0D': { goto end; } // ENTER or CTRL + M - case '\x0E': { /* Next history */ continue; } // CTRL + N - case '\x0F': { goto end; } // CTRL + O - case '\x10': { /* Prior history */ continue; } // CTRL + P - case '\x11': { /* Start output */ continue; } // CTRL + Q - case '\x12': { continue; } // IGNORE CTRL + R - case '\x13': { /* Stop output */ continue; } // CTRL + S - case '\x14': { /* Swap with prior */ continue; } // CTRL + T - case '\x15': { lineDeleteHome(&l); continue; } // CTRL + U - case '\x16': { /* Insert char code */ continue; } // CTRL + V - case '\x17': { lineDeleteWordHome(&l); continue; } // CTRL + W - case '\x18': { continue; } // IGNORE CTRL + X - case '\x19': { /* Paste deleted */ continue; } // CTRL + Y + case '\x01': { line_move_home(&l); } continue; // CTRL + A + case '\x02': { line_move_left(&l); } continue; // CTRL + B + case '\x03': { line_reset(&l); } continue; // CTRL + C + case '\x04': { r = NULL; } goto ret; // CTRL + D + case '\x05': { line_move_end(&l); } continue; // CTRL + E + case '\x06': { line_move_right(&l); } continue; // CTRL + F + case '\x07': {} continue; // IGNORE CTRL + G + case '\x08': { line_backspace(&l); } continue; // CTRL + H + case '\x09': {} continue; // IGNORE CTRL + I + case '\x0A': {} goto end; // CTRL + J + case '\x0B': { line_delete_end(&l); } continue; // CTRL + K + case '\x0C': { clearscreen(&l); } continue; // CTRL + L + case '\x0D': {} goto end; // ENTER or CTRL + M + case '\x0E': { /* Next history */ } continue; // CTRL + N + case '\x0F': {} goto end; // CTRL + O + case '\x10': { /* Prior history */ } continue; // CTRL + P + case '\x11': { /* Start output */ } continue; // CTRL + Q + case '\x12': {} continue; // IGNORE CTRL + R + case '\x13': { /* Stop output */ } continue; // CTRL + S + case '\x14': { /* Swap with prior */ } continue; // CTRL + T + case '\x15': { line_delete_home(&l); } continue; // CTRL + U + case '\x16': { /* Insert char code */ } continue; // CTRL + V + case '\x17': { line_delete_word_home(&l); } continue; // CTRL + W + case '\x18': {} continue; // IGNORE CTRL + X + case '\x19': { /* Paste deleted */ } continue; // CTRL + Y case '\x1B': switch ((c = fgetc(stdin))) { - case 'b': { lineMoveWordHome(&l); continue; } // ALT + B - case 'd': { lineDeleteWordEnd(&l); continue; } // ALT + D - case 'f': { lineMoveWordEnd(&l); continue; } // ALT + F + case 'b': { line_move_word_home(&l); } continue; // ALT + B + case 'd': { line_delete_word_end(&l); } continue; // ALT + D + case 'f': { line_move_word_end(&l); } continue; // ALT + F case '[': switch ((c = fgetc(stdin))) { case '1': case '2': case '3': case '4': case '5': case '6': - case '7': case '8': case '9': { lineESC(&l, c); continue; } - case 'A': { histMovePrior(&h); continue; } // UP - case 'B': { histMoveNext(&h); continue; } // DOWN - case 'C': { lineMoveRight(&l); continue; } // RIGHT - case 'D': { lineMoveLeft(&l); continue; } // LEFT - case 'F': { lineMoveEnd(&l); continue; } // END - case 'H': { lineMoveHome(&l); continue; } // HOME - default: { continue; } + case '7': case '8': case '9': { line_esc(&l, c); } continue; + case 'A': { hist_move_prior(&h); } continue; // UP + case 'B': { hist_move_next(&h); } continue; // DOWN + case 'C': { line_move_right(&l); } continue; // RIGHT + case 'D': { line_move_left(&l); } continue; // LEFT + case 'F': { line_move_end(&l); } continue; // END + case 'H': { line_move_home(&l); } continue; // HOME + default: {} continue; } - default: { continue; } + default: {} continue; } - case '\x7F': { lineBackspace(&l); continue; } // BACKSPACE - default: { lineInsert(&l, c); continue; } + case '\x7F': { line_backspace(&l); } continue; // BACKSPACE + default: { line_insert(&l, c); } continue; } } end:; - r = strndup(l.s, l.sl); + r = (u8 *)strndup(l.s, l.sl); ret:; tcrestore(); fputc('\n', stdout); return r; } #undef l // h.a[h.ap] /* Handle an extended ^[ sequence */ -static void lineESC(struct line *l, register int c) { +static void line_esc(struct line *l, register int c) { switch (c) { case '2': switch ((c = fgetc(stdin))) { case '~': { /* Insert char code */ break; } // INSERT } case '3': switch ((c = fgetc(stdin))) { - case '~': { lineDelete(l); break; } // DELETE + case '~': { line_delete(l); break; } // DELETE } case '5': switch ((c = fgetc(stdin))) { - case '~': { histMoveHome(&h); break; } // PAGE UP + case '~': { hist_move_home(&h); break; } // PAGE UP } case '6': switch ((c = fgetc(stdin))) { - case '~': { histMoveEnd(&h); break; } // PAGE DOWN + case '~': { hist_move_end(&h); break; } // PAGE DOWN } } } @@ -250,11 +251,11 @@ static size_t getcols(void) { /* Clear the screen */ static void clearscreen(struct line *l) { - fputs("\x1B[H\x1B[2J", stdout); lineRefresh(l); return; + fputs("\x1B[H\x1B[2J", stdout); line_refresh(l); return; } /* Refresh line */ -static void lineRefresh(struct line *l) { +static void line_refresh(struct line *l) { fputs("\r\x1B[0K", stdout); fputs(l->prompt, stdout); fputs(l->s, stdout); fprintf(stdout, "\r\x1B[%zuC", l->pl + l->sp); @@ -262,54 +263,54 @@ static void lineRefresh(struct line *l) { } /* Reset line */ -static void lineReset(struct line *l) { +static void line_reset(struct line *l) { l->s[0] = 0; l->sp = 0; l->sl = 0; - fputs("^C\n", stdout); lineRefresh(l); return; + fputs("^C\n", stdout); line_refresh(l); return; } /* Move cursor left */ -static void lineMoveLeft(struct line *l) { +static void line_move_left(struct line *l) { if (l->sp) { --l->sp; fputs("\x1B[D", stdout); } return; } /* Move cursor right */ -static void lineMoveRight(struct line *l) { +static void line_move_right(struct line *l) { if (l->sp != l->sl) { ++l->sp; fputs("\x1B[C", stdout); } return; } /* Move cursor to the word home */ -static void lineMoveWordHome(struct line *l) { +static void line_move_word_home(struct line *l) { for (; l->sp && l->s[l->sp - 1] == ' '; --l->sp); for (; l->sp && l->s[l->sp - 1] != ' '; --l->sp); - lineRefresh(l); return; + line_refresh(l); return; } /* Move cursor to the word end */ -static void lineMoveWordEnd(struct line *l) { +static void line_move_word_end(struct line *l) { for (; l->sp != l->sl && l->s[l->sp] == ' '; ++l->sp); for (; l->sp != l->sl && l->s[l->sp] != ' '; ++l->sp); - lineRefresh(l); return; + line_refresh(l); return; } /* Move cursor to the line home */ -static void lineMoveHome(struct line *l) { - l->sp = 0; lineRefresh(l); return; +static void line_move_home(struct line *l) { + l->sp = 0; line_refresh(l); return; } /* Move cursor to the line end */ -static void lineMoveEnd(struct line *l) { - l->sp = l->sl; lineRefresh(l); return; +static void line_move_end(struct line *l) { + l->sp = l->sl; line_refresh(l); return; } /* Push character onto end of line */ -static void linePush(struct line *l, char c) { +static void line_push(struct 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 lineInsert(struct line *l, char c) { +static int line_insert(struct 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; @@ -317,98 +318,98 @@ static int lineInsert(struct line *l, char c) { } else { memmove(l->s + l->sp + 1, l->s + l->sp, l->sl - l->sp); - l->s[l->sp++] = c; l->s[++l->sl] = 0; lineRefresh(l); + l->s[l->sp++] = c; l->s[++l->sl] = 0; line_refresh(l); } return c; } /* Delete character before cursor */ -static void lineBackspace(struct line *l) { +static void line_backspace(struct 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; lineRefresh(l); + --l->sp; l->s[--l->sl] = 0; line_refresh(l); } return; } /* Delete character at cursor */ -static void lineDelete(struct line *l) { +static void line_delete(struct 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; lineRefresh(l); + l->s[--l->sl] = 0; line_refresh(l); } return; } /* Delete the word preceeding the cursor */ -static void lineDeleteWordHome(struct line *l) { +static void line_delete_word_home(struct line *l) { size_t p = l->sp; for (; l->sp && l->s[l->sp - 1] == ' '; --l->sp); for (; l->sp && l->s[l->sp - 1] != ' '; --l->sp); memmove(l->s + l->sp, l->s + p, l->sl - p + 1); - l->sl -= p - l->sp; lineRefresh(l); return; + l->sl -= p - l->sp; line_refresh(l); return; } /* Delete the word following the cursor */ -static void lineDeleteWordEnd(struct line *l) { +static void line_delete_word_end(struct line *l) { size_t p = l->sp; for (; p != l->sl && l->s[p] == ' '; ++p); for (; p != l->sl && l->s[p] != ' '; ++p); memmove(l->s + l->sp, l->s + p, l->sl - p + 1); - l->sl -= p - l->sp; lineRefresh(l); return; + l->sl -= p - l->sp; line_refresh(l); return; } /* Delete characters from cursor to home */ -static void lineDeleteHome(struct line *l) { +static void line_delete_home(struct line *l) { memmove(l->s, l->s + l->sp, l->sl - l->sp + 1); - l->sl -= l->sp; l->sp = 0; lineRefresh(l); return; + l->sl -= l->sp; l->sp = 0; line_refresh(l); return; } /* Delete characters from cursor to end */ -static void lineDeleteEnd(struct line *l) { - l->s[(l->sl = l->sp)] = 0; lineRefresh(l); return; +static void line_delete_end(struct line *l) { + l->s[(l->sl = l->sp)] = 0; line_refresh(l); return; } /* Initialise history */ -static int histInit(struct hist *h) { +static int hist_init(struct hist *h) { h->ap = 0; h->ah = 0; h->at = 0; h->al = 0; h->ac = conf_histsize + 1; if (!(h->a = malloc(h->ac * sizeof (*h->a)))) { return 1; } /* TODO load history from file */ return 0; } /* Free history */ -static void histFree(struct hist *h) { +static void hist_free(struct 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 histMovePrior(struct hist *h) { +static void hist_move_prior(struct hist *h) { if (h->ap != h->ah) { h->ap == 0 ? h->ap = h->ac - 1 : --h->ap; } - lineRefresh(&h->a[h->ap]); return; + line_refresh(&h->a[h->ap]); return; } /* Move forwards in history */ -static void histMoveNext(struct hist *h) { +static void hist_move_next(struct hist *h) { register size_t an = h->ap == h->ac - 1 ? 0 : h->ap + 1; if (an != h->at) { h->ap = an; } - lineRefresh(&h->a[h->ap]); return; + line_refresh(&h->a[h->ap]); return; } /* Move to the start of history */ -static void histMoveHome(struct hist *h) { +static void hist_move_home(struct hist *h) { /* TODO */ - h->ap = h->ah; lineRefresh(&h->a[h->ap]); return; + h->ap = h->ah; line_refresh(&h->a[h->ap]); return; } /* Move to the end of history */ -static void histMoveEnd(struct hist *h) { +static void hist_move_end(struct hist *h) { /* TODO */ // h->ap = h->at; lineRefresh(&h->a[h->ap]); return; } /* Push a line onto the end of history */ -static void histPush(struct hist *h, struct line l) { +static void hist_push(struct hist *h, struct line l) { if (h->al != h->ac) { ++h->al; h->ap = h->at; if (++h->at == h->ac) { h->at = 0; } } diff --git a/src/lineread.h b/src/lineread.h index c3713d9..c8406e1 100644 --- a/src/lineread.h +++ b/src/lineread.h @@ -33,7 +33,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. #ifndef OMKOV_ESH_LINEREAD_H_RPVXY3N7 #define OMKOV_ESH_LINEREAD_H_RPVXY3N7 -extern char *lineread(void); +#include "cll/cll.h" + +extern u8 *lineread(void); extern void linefree(void); #endif // OMKOV_ESH_LINEREAD_H_RPVXY3N7 diff --git a/src/main.c b/src/main.c index 3eb22bb..df3756f 100644 --- a/src/main.c +++ b/src/main.c @@ -83,11 +83,11 @@ int main(int ac, char *av[]) { (void)(ac); A0 = av[0]; do { if (sigsetjmp(jmp, 1)) { fputc('\n', stdout); } jmpflag = true; - char *line = lineread(); + u8 *line = lineread(); if (!line) { if (errno) { warn("%s, %d", serr(), errno); } break; } if (Lflag) { lex_debug((u8 *)line); free(line); continue; } - for (char *e = line; *e;) { + for (char *e = (char *)line; *e;) { char **args = parse(e, &e); if (!args) { if (errno) { warn("%s", serr()); } break; }