From e6d46d21e704886cd050d9e16eda2b71c4b8dd40 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 17 Sep 2019 14:33:59 -0400 Subject: [PATCH] Dynamically allocate the ungetch buffer, and resize it to accomodate the full size of a PDC_getclipboard() paste. --- curses.h | 5 ++++- pdcurses/getch.c | 23 ++++++++++++++--------- pdcurses/initscr.c | 8 ++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/curses.h b/curses.h index 0255229b..b62e243f 100644 --- a/curses.h +++ b/curses.h @@ -28,7 +28,7 @@ Defined by this header: **man-end****************************************************************/ #define PDCURSES 1 -#define PDC_BUILD 3902 +#define PDC_BUILD 3903 #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_ungch; /* array of ungotten chars */ + int c_ungind; /* ungetch() push index */ + int c_ungmax; /* allocated size of ungetch() buffer */ } SCREEN; /*---------------------------------------------------------------------- diff --git a/pdcurses/getch.c b/pdcurses/getch.c index 5b29098c..740415e0 100644 --- a/pdcurses/getch.c +++ b/pdcurses/getch.c @@ -94,12 +94,9 @@ getch #include #define _INBUFSIZ 512 /* size of terminal input buffer */ -#define NUNGETCH 256 /* max # chars to ungetch() */ static int c_pindex = 0; /* putter index */ static int c_gindex = 1; /* getter index */ -static int c_ungind = 0; /* ungetch() push index */ -static int c_ungch[NUNGETCH]; /* array of ungotten chars */ static int _get_box(int *y_start, int *y_end, int *x_start, int *x_end) { @@ -204,7 +201,7 @@ static int _paste(void) # define PASTE paste #endif char *paste; - long len; + long len, newmax; int key; key = PDC_getclipboard(&paste, &len); @@ -215,6 +212,14 @@ static int _paste(void) wpaste = malloc(len * sizeof(wchar_t)); len = PDC_mbstowcs(wpaste, paste, len); #endif + newmax = len + SP->c_ungind; + if (newmax > SP->c_ungmax) + { + SP->c_ungch = realloc(SP->c_ungch, newmax * sizeof(int)); + if (!SP->c_ungch) + return -1; + SP->c_ungmax = newmax; + } while (len > 1) PDC_ungetch(PASTE[--len]); key = *PASTE; @@ -357,8 +362,8 @@ int wgetch(WINDOW *win) /* if ungotten char exists, remove and return it */ - if (c_ungind) - return c_ungch[--c_ungind]; + if (SP->c_ungind) + return SP->c_ungch[--(SP->c_ungind)]; /* if normal and data in buffer */ @@ -491,10 +496,10 @@ int PDC_ungetch(int ch) { PDC_LOG(("ungetch() - called\n")); - if (c_ungind >= NUNGETCH) /* pushback stack full */ + if (SP->c_ungind >= SP->c_ungmax) /* pushback stack full */ return ERR; - c_ungch[c_ungind++] = ch; + SP->c_ungch[SP->c_ungind++] = ch; return OK; } @@ -507,7 +512,7 @@ int flushinp(void) c_gindex = 1; /* set indices to kill buffer */ c_pindex = 0; - c_ungind = 0; /* clear c_ungch array */ + SP->c_ungind = 0; /* clear SP->c_ungch array */ return OK; } diff --git a/pdcurses/initscr.c b/pdcurses/initscr.c index 7409aa36..9b4234fb 100644 --- a/pdcurses/initscr.c +++ b/pdcurses/initscr.c @@ -103,6 +103,8 @@ 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 */ @@ -240,6 +242,10 @@ WINDOW *initscr(void) sprintf(ttytype, "pdcurses|PDCurses for %s", PDC_sysname()); + SP->c_ungch = malloc(NUNGETCH * sizeof(int)); + SP->c_ungind = 0; + SP->c_ungmax = NUNGETCH; + return stdscr; } @@ -297,6 +303,8 @@ void delscreen(SCREEN *sp) if (!SP || sp != SP) return; + free(SP->c_ungch); + PDC_slk_free(); /* free the soft label keys, if needed */ delwin(stdscr);