From 75a26404bd67d386cebd8014d90f576cc915948e Mon Sep 17 00:00:00 2001 From: Anthony Howe Date: Tue, 22 Oct 2024 13:00:15 -0400 Subject: [PATCH] Add small history buffer to aline.c. --- README.md | 3 ++- src/aline.c | 23 ++++++++++++++--------- src/aline.h | 3 +++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6b7328e..1b4490c 100755 --- a/README.md +++ b/README.md @@ -109,7 +109,8 @@ To leave Post4 either type the `EOF` terminal character, `BYE`, or `code BYE-COD Post4 implements simple TTY style line editing for the last interactive input line. GNU `readline` or BSD `libedit` are not supported. Essentially the `stty(1)` keys are supported, in addition to being able to edit the last input line. - up ^K Edit the previous input line. + up ^K Cycle to the previous input line to edit. + down ^L Cycle to the next input line to edit. left right Cursor left or right within line. ERASE ^H ^? Erase character before the cursor. WERASE Erase the previous white space delimited word. diff --git a/src/aline.c b/src/aline.c index 9dd16b7..3199aca 100644 --- a/src/aline.c +++ b/src/aline.c @@ -19,8 +19,9 @@ int is_tty; static int tty_fd = -1; static int tty_mode = 0; static struct termios tty_modes[3]; -static char lastline[256]; +static char history[ALINE_HISTORY][MAX_INPUT]; static const char *ps2; +static int lastline; #define tty_saved tty_modes[ALINE_CANONICAL] #define tty_raw tty_modes[ALINE_RAW] @@ -136,7 +137,7 @@ int alineInput(FILE *fp, const char *prompt, char *buf, size_t size) { unsigned i; - int ch, pcol, pos[2]; + int ch, pcol, pos[2], prevline = lastline; if (buf == NULL || size < 1) { return EOF; @@ -153,8 +154,8 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size) prompt = ps2; } pcol = strlen(prompt); - if (sizeof (lastline) < size) { - size = sizeof (lastline); + if (sizeof (*history) < size) { + size = sizeof (*history); } alineGetRowCol(pos); (void) printf(ANSI_SAVE_CURSOR); @@ -171,8 +172,10 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size) if (ch == '\e') { if ((ch = fgetc(stdin)) == '[') { ch = fgetc(stdin); - if (ch == 'A' || ch == 'B') { + if (ch == 'A') { ch = '\v'; + } else if (ch == 'B') { + ch = '\f'; } else if (ch == 'C') { i += i < size && buf[i] != '\0'; continue; @@ -182,10 +185,11 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size) } } } - if (ch == '\v') { + if (ch == '\v' || ch == '\f') { /* Restore intput to last input line. */ - (void) strncpy(buf, lastline, size-1); - i = strlen(lastline); + prevline = (prevline+(ch == '\v' ? -1 : 1)) & (ALINE_HISTORY-1); + (void) strncpy(buf, history[prevline], size-1); + i = strlen(history[prevline]); } else if (ch == tty_saved.c_cc[VERASE] || ch == '\b' || ch == 127) { if (0 < i) { i--; @@ -210,7 +214,8 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size) } } if (0 < i) { - (void) strncpy(lastline, buf, size-1); + (void) strncpy(history[lastline], buf, size-1); + lastline = (lastline+1) & (ALINE_HISTORY-1); } return (int) strlen(buf); } diff --git a/src/aline.h b/src/aline.h index b7974a5..f3169c2 100644 --- a/src/aline.h +++ b/src/aline.h @@ -31,6 +31,7 @@ extern "C" { #include "ansiterm.h" #include +#include #ifdef HAVE_TERMIOS_H # include @@ -50,6 +51,8 @@ extern struct winsize window; #define ALINE_RAW 1 #define ALINE_RAW_NB 2 +#define ALINE_HISTORY 4 + extern void alineInit(void); extern void alineFini(void); extern int alineReadByte(void);