From d727feea46ff3efa0c82f8a3eb1b73dee6042c61 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 17 Sep 2019 16:46:39 -0400 Subject: [PATCH] Dynamically allocate the keyboard buffer; consolidate magic numbers. --- curses.h | 5 ++++- curspriv.h | 7 ++++--- pdcurses/getch.c | 31 ++++++++++++++----------------- pdcurses/initscr.c | 11 +++++++++-- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/curses.h b/curses.h index b62e243f..26428a45 100644 --- a/curses.h +++ b/curses.h @@ -28,7 +28,7 @@ Defined by this header: **man-end****************************************************************/ #define PDCURSES 1 -#define PDC_BUILD 3903 +#define PDC_BUILD 3904 #define PDC_VER_MAJOR 3 #define PDC_VER_MINOR 9 #define PDC_VERDOT "3.9" @@ -355,6 +355,9 @@ typedef struct bool dirty; /* redraw on napms() after init_color() */ int sel_start; /* start of selection (y * COLS + x) */ int sel_end; /* end of selection */ + int *c_buffer; /* character buffer */ + int c_pindex; /* putter index */ + int c_gindex; /* getter index */ int *c_ungch; /* array of ungotten chars */ int c_ungind; /* ungetch() push index */ int c_ungmax; /* allocated size of ungetch() buffer */ diff --git a/curspriv.h b/curspriv.h index f9fb3fad..a5d1ac44 100644 --- a/curspriv.h +++ b/curspriv.h @@ -102,8 +102,6 @@ size_t PDC_wcstombs(char *, const wchar_t *, size_t); /* Internal macros for attributes */ -#define PDC_COLOR_PAIRS 256 - #ifndef max # define max(a,b) (((a) > (b)) ? (a) : (b)) #endif @@ -115,7 +113,10 @@ size_t PDC_wcstombs(char *, const wchar_t *, size_t); #define PDC_CLICK_PERIOD 150 /* time to wait for a click, if not set by mouseinterval() */ +#define PDC_COLOR_PAIRS 256 +#define PDC_MAXCOL 768 /* maximum possible COLORS; may be less */ -#define PDC_MAXCOL 768 /* maximum possible COLORS; may be less */ +#define _INBUFSIZ 512 /* size of terminal input buffer */ +#define NUNGETCH 256 /* max # chars to ungetch() */ #endif /* __CURSES_INTERNALS__ */ diff --git a/pdcurses/getch.c b/pdcurses/getch.c index 740415e0..58a44c27 100644 --- a/pdcurses/getch.c +++ b/pdcurses/getch.c @@ -93,11 +93,6 @@ getch #include -#define _INBUFSIZ 512 /* size of terminal input buffer */ - -static int c_pindex = 0; /* putter index */ -static int c_gindex = 1; /* getter index */ - static int _get_box(int *y_start, int *y_end, int *x_start, int *x_end) { int start, end; @@ -327,7 +322,6 @@ static int _mouse_key(void) int wgetch(WINDOW *win) { - static int buffer[_INBUFSIZ]; /* character buffer */ int key, waitcount; PDC_LOG(("wgetch() - called\n")); @@ -367,13 +361,13 @@ int wgetch(WINDOW *win) /* if normal and data in buffer */ - if ((!SP->raw_inp && !SP->cbreak) && (c_gindex < c_pindex)) - return buffer[c_gindex++]; + if ((!SP->raw_inp && !SP->cbreak) && (SP->c_gindex < SP->c_pindex)) + return SP->c_buffer[SP->c_gindex++]; /* prepare to buffer data */ - c_pindex = 0; - c_gindex = 0; + SP->c_pindex = 0; + SP->c_gindex = 0; /* to get here, no keys are buffered. go and get one. */ @@ -458,17 +452,17 @@ int wgetch(WINDOW *win) if (key == '\b') { - if (c_pindex > c_gindex) - c_pindex--; + if (SP->c_pindex > SP->c_gindex) + SP->c_pindex--; } else - if (c_pindex < _INBUFSIZ - 2) - buffer[c_pindex++] = key; + if (SP->c_pindex < _INBUFSIZ - 2) + SP->c_buffer[SP->c_pindex++] = key; /* if we got a line */ if (key == '\n' || key == '\r') - return buffer[c_gindex++]; + return SP->c_buffer[SP->c_gindex++]; } } @@ -508,10 +502,13 @@ int flushinp(void) { PDC_LOG(("flushinp() - called\n")); + if (!SP) + return ERR; + PDC_flushinp(); - c_gindex = 1; /* set indices to kill buffer */ - c_pindex = 0; + SP->c_gindex = 1; /* set indices to kill buffer */ + SP->c_pindex = 0; SP->c_ungind = 0; /* clear SP->c_ungch array */ return OK; diff --git a/pdcurses/initscr.c b/pdcurses/initscr.c index 9b4234fb..06a077b5 100644 --- a/pdcurses/initscr.c +++ b/pdcurses/initscr.c @@ -103,8 +103,6 @@ char ttytype[128]; const char *_curses_notice = "PDCurses " PDC_VERDOT " - " __DATE__; -#define NUNGETCH 256 /* max # chars to ungetch() */ - SCREEN *SP = (SCREEN*)NULL; /* curses variables */ WINDOW *curscr = (WINDOW *)NULL; /* the current screen image */ WINDOW *stdscr = (WINDOW *)NULL; /* the default screen window */ @@ -242,7 +240,15 @@ WINDOW *initscr(void) sprintf(ttytype, "pdcurses|PDCurses for %s", PDC_sysname()); + SP->c_buffer = malloc(_INBUFSIZ * sizeof(int)); + if (!SP->c_buffer) + return NULL; + SP->c_pindex = 0; + SP->c_gindex = 1; + SP->c_ungch = malloc(NUNGETCH * sizeof(int)); + if (!SP->c_ungch) + return NULL; SP->c_ungind = 0; SP->c_ungmax = NUNGETCH; @@ -304,6 +310,7 @@ void delscreen(SCREEN *sp) return; free(SP->c_ungch); + free(SP->c_buffer); PDC_slk_free(); /* free the soft label keys, if needed */