diff --git a/contrib/ncurses/ncurses/base/lib_screen.c b/contrib/ncurses/ncurses/base/lib_screen.c index 164356dc41de..f46e17447127 100644 --- a/contrib/ncurses/ncurses/base/lib_screen.c +++ b/contrib/ncurses/ncurses/base/lib_screen.c @@ -1,1059 +1,1059 @@ /**************************************************************************** * Copyright 2019,2020 Thomas E. Dickey * * Copyright 1998-2017,2018 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, distribute with modifications, sublicense, and/or sell * * copies of the Software, and to permit persons to whom the Software is * * furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * * * Except as contained in this notice, the name(s) of the above copyright * * holders shall not be used in advertising or otherwise to promote the * * sale, use or other dealings in this Software without prior written * * authorization. * ****************************************************************************/ /**************************************************************************** * Author: Zeyd M. Ben-Halim 1992,1995 * * and: Eric S. Raymond * * and: Thomas E. Dickey 1996 on * * and: Juergen Pfeifer 2009 * ****************************************************************************/ #include #include #ifndef CUR #define CUR SP_TERMTYPE #endif MODULE_ID("$Id: lib_screen.c,v 1.97 2020/02/02 23:34:34 tom Exp $") #define MAX_SIZE 0x3fff /* 16k is big enough for a window or pad */ #define MARKER '\\' #define APPEND '+' #define GUTTER '|' #define L_CURL '{' #define R_CURL '}' #if USE_STRING_HACKS && HAVE_SNPRINTF #define ARG_SLIMIT(name) size_t name, #else #define ARG_SLIMIT(name) /* nothing */ #endif #define CUR_SLIMIT _nc_SLIMIT(limit - (target - base)) #define TOP_SLIMIT _nc_SLIMIT(sizeof(buffer)) /* * Use 0x888888 as the magic number for new-format files, since it cannot be * mistaken for the _cury/_curx pair of 16-bit numbers which start the old * format. It happens to be unused in the file 5.22 database (2015/03/07). */ static const char my_magic[] = -{'\210', '\210', '\210', '\210'}; +{'\210', '\210', '\210', '\210', 0}; #if NCURSES_EXT_PUTWIN typedef enum { pINT /* int */ ,pSHORT /* short */ ,pBOOL /* bool */ ,pATTR /* attr_t */ ,pCHAR /* chtype */ ,pSIZE /* NCURSES_SIZE_T */ #if NCURSES_WIDECHAR ,pCCHAR /* cchar_t */ #endif } PARAM_TYPE; typedef struct { const char name[11]; attr_t attr; } SCR_ATTRS; typedef struct { const char name[17]; PARAM_TYPE type; size_t size; size_t offset; } SCR_PARAMS; #define DATA(name) { { #name }, A_##name } static const SCR_ATTRS scr_attrs[] = { DATA(NORMAL), DATA(STANDOUT), DATA(UNDERLINE), DATA(REVERSE), DATA(BLINK), DATA(DIM), DATA(BOLD), DATA(ALTCHARSET), DATA(INVIS), DATA(PROTECT), DATA(HORIZONTAL), DATA(LEFT), DATA(LOW), DATA(RIGHT), DATA(TOP), DATA(VERTICAL), #ifdef A_ITALIC DATA(ITALIC), #endif }; #undef DATA #define sizeof2(type,name) sizeof(((type *)0)->name) #define DATA(name, type) { { #name }, type, sizeof2(WINDOW, name), offsetof(WINDOW, name) } static const SCR_PARAMS scr_params[] = { DATA(_cury, pSIZE), DATA(_curx, pSIZE), DATA(_maxy, pSIZE), DATA(_maxx, pSIZE), DATA(_begy, pSIZE), DATA(_begx, pSIZE), DATA(_flags, pSHORT), DATA(_attrs, pATTR), DATA(_bkgd, pCHAR), DATA(_notimeout, pBOOL), DATA(_clear, pBOOL), DATA(_leaveok, pBOOL), DATA(_scroll, pBOOL), DATA(_idlok, pBOOL), DATA(_idcok, pBOOL), DATA(_immed, pBOOL), DATA(_sync, pBOOL), DATA(_use_keypad, pBOOL), DATA(_delay, pINT), DATA(_regtop, pSIZE), DATA(_regbottom, pSIZE), DATA(_pad._pad_y, pSIZE), DATA(_pad._pad_x, pSIZE), DATA(_pad._pad_top, pSIZE), DATA(_pad._pad_left, pSIZE), DATA(_pad._pad_bottom, pSIZE), DATA(_pad._pad_right, pSIZE), DATA(_yoffset, pSIZE), #if NCURSES_WIDECHAR DATA(_bkgrnd, pCCHAR), #if NCURSES_EXT_COLORS DATA(_color, pINT), #endif #endif }; #undef DATA static const NCURSES_CH_T blank = NewChar(BLANK_TEXT); /* * Allocate and read a line of text. Caller must free it. */ static char * read_txt(FILE *fp) { size_t limit = 1024; char *result = malloc(limit); char *buffer; if (result != 0) { int ch = 0; size_t used = 0; clearerr(fp); result[used] = '\0'; do { if (used + 2 >= limit) { limit += 1024; buffer = realloc(result, limit); if (buffer == 0) { free(result); result = 0; break; } result = buffer; } ch = fgetc(fp); if (ch == EOF) break; result[used++] = (char) ch; result[used] = '\0'; } while (ch != '\n'); if (ch == '\n') { result[--used] = '\0'; T(("READ:%s", result)); } else if (used == 0) { free(result); result = 0; } } return result; } static char * decode_attr(char *source, attr_t *target, int *color) { bool found = FALSE; T(("decode_attr '%s'", source)); while (*source) { if (source[0] == MARKER && source[1] == L_CURL) { source += 2; found = TRUE; } else if (source[0] == R_CURL) { source++; found = FALSE; } else if (found) { size_t n; char *next = source; if (source[0] == GUTTER) { ++next; } else if (*next == 'C') { int value = 0; unsigned pair; next++; while (isdigit(UChar(*next))) { value = value * 10 + (*next++ - '0'); } *target &= ~A_COLOR; pair = (unsigned) ((value > 256) ? COLOR_PAIR(255) : COLOR_PAIR(value)); *target |= pair; *color = value; } else { while (isalnum(UChar(*next))) { ++next; } for (n = 0; n < SIZEOF(scr_attrs); ++n) { if ((size_t) (next - source) == strlen(scr_attrs[n].name)) { if (scr_attrs[n].attr) { *target |= scr_attrs[n].attr; } else { *target = A_NORMAL; } break; } } } source = next; } else { break; } } return source; } static char * decode_char(char *source, int *target) { int limit = 0; int base = 16; const char digits[] = "0123456789abcdef"; T(("decode_char '%s'", source)); *target = ' '; switch (*source) { case MARKER: switch (*++source) { case APPEND: break; case MARKER: *target = MARKER; ++source; break; case 's': *target = ' '; ++source; break; case '0': case '1': case '2': case '3': base = 8; limit = 3; break; case 'u': limit = 4; ++source; break; case 'U': limit = 8; ++source; break; } if (limit) { *target = 0; while (limit-- > 0) { char *find = strchr(digits, *source++); int ch = (find != 0) ? (int) (find - digits) : -1; *target *= base; if (ch >= 0 && ch < base) { *target += ch; } } } break; default: *target = *source++; break; } return source; } static char * decode_chtype(char *source, chtype fillin, chtype *target) { attr_t attr = ChAttrOf(fillin); int color = PAIR_NUMBER((int) attr); int value; T(("decode_chtype '%s'", source)); source = decode_attr(source, &attr, &color); source = decode_char(source, &value); *target = (ChCharOf(value) | attr | (chtype) COLOR_PAIR(color)); /* FIXME - ignore combining characters */ return source; } #if NCURSES_WIDECHAR static char * decode_cchar(char *source, cchar_t *fillin, cchar_t *target) { int color; attr_t attr = fillin->attr; wchar_t chars[CCHARW_MAX]; int append = 0; int value = 0; T(("decode_cchar '%s'", source)); *target = blank; #if NCURSES_EXT_COLORS color = fillin->ext_color; #else color = (int) PAIR_NUMBER(attr); #endif source = decode_attr(source, &attr, &color); memset(chars, 0, sizeof(chars)); source = decode_char(source, &value); chars[0] = (wchar_t) value; /* handle combining characters */ while (source[0] == MARKER && source[1] == APPEND) { source += 2; source = decode_char(source, &value); if (++append < CCHARW_MAX) { chars[append] = (wchar_t) value; } } setcchar(target, chars, attr, (short) color, &color); return source; } #endif static int read_win(WINDOW *win, FILE *fp) { int code = ERR; size_t n; int color; #if NCURSES_WIDECHAR NCURSES_CH_T prior; #endif chtype prior2; memset(win, 0, sizeof(WINDOW)); for (;;) { char *name; char *value; char *txt = read_txt(fp); if (txt == 0) break; if (!strcmp(txt, "rows:")) { free(txt); code = OK; break; } if ((value = strchr(txt, '=')) == 0) { free(txt); continue; } *value++ = '\0'; name = !strcmp(txt, "flag") ? value : txt; for (n = 0; n < SIZEOF(scr_params); ++n) { if (!strcmp(name, scr_params[n].name)) { void *data = (void *) ((char *) win + scr_params[n].offset); switch (scr_params[n].type) { case pATTR: (void) decode_attr(value, data, &color); break; case pBOOL: *(bool *) data = TRUE; break; case pCHAR: prior2 = ' '; decode_chtype(value, prior2, data); break; case pINT: *(int *) data = atoi(value); break; case pSHORT: *(short *) data = (short) atoi(value); break; case pSIZE: *(NCURSES_SIZE_T *) data = (NCURSES_SIZE_T) atoi(value); break; #if NCURSES_WIDECHAR case pCCHAR: prior = blank; decode_cchar(value, &prior, data); break; #endif } break; } } free(txt); } return code; } static int read_row(char *source, NCURSES_CH_T * prior, NCURSES_CH_T * target, int length) { while (*source != '\0' && length > 0) { #if NCURSES_WIDECHAR int len; source = decode_cchar(source, prior, target); len = _nc_wacs_width(target->chars[0]); if (len > 1) { int n; SetWidecExt(CHDEREF(target), 0); for (n = 1; n < len; ++n) { target[n] = target[0]; SetWidecExt(CHDEREF(target), n); } target += (len - 1); length -= (len - 1); } #else source = decode_chtype(source, *prior, target); #endif *prior = *target; ++target; --length; } while (length-- > 0) { *target++ = blank; } /* FIXME - see what error conditions should apply if I need to return ERR */ return 0; } #endif /* NCURSES_EXT_PUTWIN */ /* * Originally, getwin/putwin used fread/fwrite, because they used binary data. * The new format uses printable ASCII, which does not have as predictable * sizes. Consequently, we use buffered I/O, e.g., fgetc/fprintf, which need * special handling if we want to read screen dumps from an older library. */ static int read_block(void *target, size_t length, FILE *fp) { int result = 0; char *buffer = target; clearerr(fp); while (length-- != 0) { int ch = fgetc(fp); if (ch == EOF) { result = -1; break; } *buffer++ = (char) ch; } return result; } NCURSES_EXPORT(WINDOW *) NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep) { WINDOW tmp, *nwin; bool old_format = FALSE; T((T_CALLED("getwin(%p)"), (void *) filep)); if (filep == 0) { returnWin(0); } /* * Read the first 4 bytes to determine first if this is an old-format * screen-dump, or new-format. */ if (read_block(&tmp, (size_t) 4, filep) < 0) { returnWin(0); } /* * If this is a new-format file, and we do not support it, give up. */ if (!memcmp(&tmp, my_magic, (size_t) 4)) { #if NCURSES_EXT_PUTWIN if (read_win(&tmp, filep) < 0) #endif returnWin(0); } else if (read_block(((char *) &tmp) + 4, sizeof(WINDOW) - 4, filep) < 0) { returnWin(0); } else { old_format = TRUE; } /* * Check the window-size: */ if (tmp._maxy == 0 || tmp._maxy > MAX_SIZE || tmp._maxx == 0 || tmp._maxx > MAX_SIZE) { returnWin(0); } if (tmp._flags & _ISPAD) { nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx tmp._maxy + 1, tmp._maxx + 1); } else { nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx tmp._maxy + 1, tmp._maxx + 1, 0, 0); } /* * We deliberately do not restore the _parx, _pary, or _parent * fields, because the window hierarchy within which they * made sense is probably gone. */ if (nwin != 0) { int n; size_t linesize = sizeof(NCURSES_CH_T) * (size_t) (tmp._maxx + 1); nwin->_curx = tmp._curx; nwin->_cury = tmp._cury; nwin->_maxy = tmp._maxy; nwin->_maxx = tmp._maxx; nwin->_begy = tmp._begy; nwin->_begx = tmp._begx; nwin->_yoffset = tmp._yoffset; nwin->_flags = tmp._flags & ~(_SUBWIN); WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp); nwin->_nc_bkgd = tmp._nc_bkgd; nwin->_notimeout = tmp._notimeout; nwin->_clear = tmp._clear; nwin->_leaveok = tmp._leaveok; nwin->_idlok = tmp._idlok; nwin->_idcok = tmp._idcok; nwin->_immed = tmp._immed; nwin->_scroll = tmp._scroll; nwin->_sync = tmp._sync; nwin->_use_keypad = tmp._use_keypad; nwin->_delay = tmp._delay; nwin->_regtop = tmp._regtop; nwin->_regbottom = tmp._regbottom; if (tmp._flags & _ISPAD) nwin->_pad = tmp._pad; if (old_format) { T(("reading old-format screen dump")); for (n = 0; n <= nwin->_maxy; n++) { if (read_block(nwin->_line[n].text, linesize, filep) < 0) { delwin(nwin); returnWin(0); } } } #if NCURSES_EXT_PUTWIN else { char *txt = 0; bool success = TRUE; NCURSES_CH_T prior = blank; T(("reading new-format screen dump")); for (n = 0; n <= nwin->_maxy; n++) { long row; char *next; if ((txt = read_txt(filep)) == 0) { T(("...failed to read string for row %d", n + 1)); success = FALSE; break; } row = strtol(txt, &next, 10); if (row != (n + 1) || *next != ':') { T(("...failed to read row-number %d", n + 1)); success = FALSE; break; } if (read_row(++next, &prior, nwin->_line[n].text, tmp._maxx + 1) < 0) { T(("...failed to read cells for row %d", n + 1)); success = FALSE; break; } free(txt); txt = 0; } if (!success) { free(txt); delwin(nwin); returnWin(0); } } #endif touchwin(nwin); } returnWin(nwin); } #if NCURSES_SP_FUNCS NCURSES_EXPORT(WINDOW *) getwin(FILE *filep) { return NCURSES_SP_NAME(getwin) (CURRENT_SCREEN, filep); } #endif #if NCURSES_EXT_PUTWIN static void encode_attr(char *target, ARG_SLIMIT(limit) attr_t source, attr_t prior, int source_color, int prior_color) { #if USE_STRING_HACKS && HAVE_SNPRINTF char *base = target; #endif source &= ~A_CHARTEXT; prior &= ~A_CHARTEXT; *target = '\0'; if ((source != prior) || (source_color != prior_color)) { size_t n; bool first = TRUE; *target++ = MARKER; *target++ = L_CURL; for (n = 0; n < SIZEOF(scr_attrs); ++n) { if ((source & scr_attrs[n].attr) != 0 || ((source & ALL_BUT_COLOR) == 0 && (scr_attrs[n].attr == A_NORMAL))) { if (first) { first = FALSE; } else { *target++ = '|'; } _nc_STRCPY(target, scr_attrs[n].name, limit); target += strlen(target); } } if (source_color != prior_color) { if (!first) *target++ = '|'; _nc_SPRINTF(target, CUR_SLIMIT "C%d", source_color); target += strlen(target); } *target++ = R_CURL; *target = '\0'; } } static void encode_cell(char *target, ARG_SLIMIT(limit) CARG_CH_T source, CARG_CH_T previous) { #if USE_STRING_HACKS && HAVE_SNPRINTF char *base = target; #endif #if NCURSES_WIDECHAR size_t n; int source_pair = GetPair(*source); int previous_pair = GetPair(*previous); *target = '\0'; if ((previous->attr != source->attr) || (previous_pair != source_pair)) { encode_attr(target, CUR_SLIMIT source->attr, previous->attr, source_pair, previous_pair); } target += strlen(target); #if NCURSES_EXT_COLORS if (previous->ext_color != source->ext_color) { _nc_SPRINTF(target, CUR_SLIMIT "%c%cC%d%c", MARKER, L_CURL, source->ext_color, R_CURL); } #endif for (n = 0; n < SIZEOF(source->chars); ++n) { unsigned uch = (unsigned) source->chars[n]; if (uch == 0) continue; if (n) { *target++ = MARKER; *target++ = APPEND; } *target++ = MARKER; if (uch > 0xffff) { _nc_SPRINTF(target, CUR_SLIMIT "U%08x", uch); } else if (uch > 0xff) { _nc_SPRINTF(target, CUR_SLIMIT "u%04x", uch); } else if (uch < 32 || uch >= 127) { _nc_SPRINTF(target, CUR_SLIMIT "%03o", uch & 0xff); } else { switch (uch) { case ' ': _nc_STRCPY(target, "s", limit); break; case MARKER: *target++ = MARKER; *target = '\0'; break; default: --target; _nc_SPRINTF(target, CUR_SLIMIT "%c", uch); break; } } target += strlen(target); } #else chtype ch = CharOfD(source); *target = '\0'; if (AttrOfD(previous) != AttrOfD(source)) { encode_attr(target, CUR_SLIMIT AttrOfD(source), AttrOfD(previous), GetPair(source), GetPair(previous)); } target += strlen(target); *target++ = MARKER; if (ch < 32 || ch >= 127) { _nc_SPRINTF(target, CUR_SLIMIT "%03o", UChar(ch)); } else { switch (ch) { case ' ': _nc_STRCPY(target, "s", limit); break; case MARKER: *target++ = MARKER; *target = '\0'; break; default: --target; _nc_SPRINTF(target, CUR_SLIMIT "%c", UChar(ch)); break; } } #endif } #endif NCURSES_EXPORT(int) putwin(WINDOW *win, FILE *filep) { int code = ERR; T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep)); #if NCURSES_EXT_PUTWIN if (win != 0) { const char *version = curses_version(); char buffer[1024]; NCURSES_CH_T last_cell; int y; memset(&last_cell, 0, sizeof(last_cell)); clearerr(filep); /* * Our magic number is technically nonprinting, but aside from that, * all of the file is printable ASCII. */ #define PUTS(s) if (fputs(s, filep) == EOF || ferror(filep)) returnCode(code) PUTS(my_magic); PUTS(version); PUTS("\n"); for (y = 0; y < (int) SIZEOF(scr_params); ++y) { const char *name = scr_params[y].name; const char *data = (char *) win + scr_params[y].offset; const void *dp = (const void *) data; attr_t attr; *buffer = '\0'; if (!strncmp(name, "_pad.", (size_t) 5) && !(win->_flags & _ISPAD)) { continue; } switch (scr_params[y].type) { case pATTR: attr = (*(const attr_t *) dp) & ~A_CHARTEXT; encode_attr(buffer, TOP_SLIMIT (*(const attr_t *) dp) & ~A_CHARTEXT, A_NORMAL, COLOR_PAIR((int) attr), 0); break; case pBOOL: if (!(*(const bool *) data)) { continue; } _nc_STRCPY(buffer, name, sizeof(buffer)); name = "flag"; break; case pCHAR: attr = (*(const attr_t *) dp); encode_attr(buffer, TOP_SLIMIT * (const attr_t *) dp, A_NORMAL, COLOR_PAIR((int) attr), 0); break; case pINT: if (!(*(const int *) dp)) continue; _nc_SPRINTF(buffer, TOP_SLIMIT "%d", *(const int *) dp); break; case pSHORT: if (!(*(const short *) dp)) continue; _nc_SPRINTF(buffer, TOP_SLIMIT "%d", *(const short *) dp); break; case pSIZE: if (!(*(const NCURSES_SIZE_T *) dp)) continue; _nc_SPRINTF(buffer, TOP_SLIMIT "%d", *(const NCURSES_SIZE_T *) dp); break; #if NCURSES_WIDECHAR case pCCHAR: encode_cell(buffer, TOP_SLIMIT (CARG_CH_T) dp, CHREF(last_cell)); break; #endif } /* * Only write non-default data. */ if (*buffer != '\0') { if (fprintf(filep, "%s=%s\n", name, buffer) <= 0 || ferror(filep)) returnCode(code); } } /* Write row-data */ fprintf(filep, "rows:\n"); for (y = 0; y <= win->_maxy; y++) { NCURSES_CH_T *data = win->_line[y].text; int x; if (fprintf(filep, "%d:", y + 1) <= 0 || ferror(filep)) returnCode(code); for (x = 0; x <= win->_maxx; x++) { #if NCURSES_WIDECHAR int len = _nc_wacs_width(data[x].chars[0]); encode_cell(buffer, TOP_SLIMIT CHREF(data[x]), CHREF(last_cell)); last_cell = data[x]; PUTS(buffer); if (len > 1) x += (len - 1); #else encode_cell(buffer, TOP_SLIMIT CHREF(data[x]), CHREF(last_cell)); last_cell = data[x]; PUTS(buffer); #endif } PUTS("\n"); } code = OK; } #else /* * This is the original putwin(): * A straight binary dump is simple, but its format can depend on whether * ncurses is compiled with wide-character support, and also may depend * on the version of ncurses, e.g., if the WINDOW structure is extended. */ if (win != 0) { size_t len = (size_t) (win->_maxx + 1); int y; clearerr(filep); if (fwrite(win, sizeof(WINDOW), (size_t) 1, filep) != 1 || ferror(filep)) returnCode(code); for (y = 0; y <= win->_maxy; y++) { if (fwrite(win->_line[y].text, sizeof(NCURSES_CH_T), len, filep) != len || ferror(filep)) { returnCode(code); } } code = OK; } #endif returnCode(code); } NCURSES_EXPORT(int) NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file) { FILE *fp = 0; int code = ERR; T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); if (_nc_access(file, R_OK) >= 0 && (fp = fopen(file, BIN_R)) != 0) { delwin(NewScreen(SP_PARM)); NewScreen(SP_PARM) = getwin(fp); #if !USE_REENTRANT newscr = NewScreen(SP_PARM); #endif (void) fclose(fp); if (NewScreen(SP_PARM) != 0) { code = OK; } } returnCode(code); } #if NCURSES_SP_FUNCS NCURSES_EXPORT(int) scr_restore(const char *file) { return NCURSES_SP_NAME(scr_restore) (CURRENT_SCREEN, file); } #endif NCURSES_EXPORT(int) scr_dump(const char *file) { int result; FILE *fp = 0; T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file))); if (_nc_access(file, W_OK) < 0 || (fp = fopen(file, BIN_W)) == 0) { result = ERR; } else { (void) putwin(newscr, fp); (void) fclose(fp); result = OK; } returnCode(result); } NCURSES_EXPORT(int) NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file) { int code = ERR; T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); if (SP_PARM != 0 && #ifdef USE_TERM_DRIVER InfoOf(SP_PARM).caninit #else !(exit_ca_mode && non_rev_rmcup) #endif ) { FILE *fp = 0; if (_nc_access(file, R_OK) >= 0 && (fp = fopen(file, BIN_R)) != 0) { delwin(CurScreen(SP_PARM)); CurScreen(SP_PARM) = getwin(fp); #if !USE_REENTRANT curscr = CurScreen(SP_PARM); #endif (void) fclose(fp); if (CurScreen(SP_PARM) != 0) { code = OK; } } } returnCode(code); } #if NCURSES_SP_FUNCS NCURSES_EXPORT(int) scr_init(const char *file) { return NCURSES_SP_NAME(scr_init) (CURRENT_SCREEN, file); } #endif NCURSES_EXPORT(int) NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file) { int code = ERR; T((T_CALLED("scr_set(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); if (NCURSES_SP_NAME(scr_init) (NCURSES_SP_ARGx file) == OK) { delwin(NewScreen(SP_PARM)); NewScreen(SP_PARM) = dupwin(curscr); #if !USE_REENTRANT newscr = NewScreen(SP_PARM); #endif if (NewScreen(SP_PARM) != 0) { code = OK; } } returnCode(code); } #if NCURSES_SP_FUNCS NCURSES_EXPORT(int) scr_set(const char *file) { return NCURSES_SP_NAME(scr_set) (CURRENT_SCREEN, file); } #endif diff --git a/contrib/ncurses/ncurses/tinfo/lib_tparm.c b/contrib/ncurses/ncurses/tinfo/lib_tparm.c index 400cd31996f8..7a53dff3addd 100644 --- a/contrib/ncurses/ncurses/tinfo/lib_tparm.c +++ b/contrib/ncurses/ncurses/tinfo/lib_tparm.c @@ -1,890 +1,890 @@ /**************************************************************************** * Copyright 2018-2019,2020 Thomas E. Dickey * * Copyright 1998-2016,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, distribute with modifications, sublicense, and/or sell * * copies of the Software, and to permit persons to whom the Software is * * furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * * * Except as contained in this notice, the name(s) of the above copyright * * holders shall not be used in advertising or otherwise to promote the * * sale, use or other dealings in this Software without prior written * * authorization. * ****************************************************************************/ /**************************************************************************** * Author: Zeyd M. Ben-Halim 1992,1995 * * and: Eric S. Raymond * * and: Thomas E. Dickey, 1996 on * ****************************************************************************/ /* * tparm.c * */ #include #include #include MODULE_ID("$Id: lib_tparm.c,v 1.108 2020/02/02 23:34:34 tom Exp $") /* * char * * tparm(string, ...) * * Substitute the given parameters into the given string by the following * rules (taken from terminfo(5)): * * Cursor addressing and other strings requiring parame- * ters in the terminal are described by a parameterized string * capability, with escapes like %x in it. For example, to * address the cursor, the cup capability is given, using two * parameters: the row and column to address to. (Rows and * columns are numbered from zero and refer to the physical * screen visible to the user, not to any unseen memory.) If * the terminal has memory relative cursor addressing, that can * be indicated by * * The parameter mechanism uses a stack and special % * codes to manipulate it. Typically a sequence will push one * of the parameters onto the stack and then print it in some * format. Often more complex operations are necessary. * * The % encodings have the following meanings: * * %% outputs `%' * %c print pop() like %c in printf() * %s print pop() like %s in printf() * %[[:]flags][width[.precision]][doxXs] * as in printf, flags are [-+#] and space * The ':' is used to avoid making %+ or %- * patterns (see below). * * %p[1-9] push ith parm * %P[a-z] set dynamic variable [a-z] to pop() * %g[a-z] get dynamic variable [a-z] and push it * %P[A-Z] set static variable [A-Z] to pop() * %g[A-Z] get static variable [A-Z] and push it * %l push strlen(pop) * %'c' push char constant c * %{nn} push integer constant nn * * %+ %- %* %/ %m * arithmetic (%m is mod): push(pop() op pop()) * %& %| %^ bit operations: push(pop() op pop()) * %= %> %< logical operations: push(pop() op pop()) * %A %O logical and & or operations for conditionals * %! %~ unary operations push(op pop()) * %i add 1 to first two parms (for ANSI terminals) * * %? expr %t thenpart %e elsepart %; * if-then-else, %e elsepart is optional. * else-if's are possible ala Algol 68: * %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %; * * For those of the above operators which are binary and not commutative, * the stack works in the usual way, with * %gx %gy %m * resulting in x mod y, not the reverse. */ NCURSES_EXPORT_VAR(int) _nc_tparm_err = 0; #define TPS(var) _nc_prescreen.tparm_state.var #define popcount _nc_popcount /* workaround for NetBSD 6.0 defect */ #if NO_LEAKS NCURSES_EXPORT(void) _nc_free_tparm(void) { if (TPS(out_buff) != 0) { FreeAndNull(TPS(out_buff)); TPS(out_size) = 0; TPS(out_used) = 0; FreeAndNull(TPS(fmt_buff)); TPS(fmt_size) = 0; } } #endif static NCURSES_INLINE void get_space(size_t need) { need += TPS(out_used); if (need > TPS(out_size)) { TPS(out_size) = need * 2; TYPE_REALLOC(char, TPS(out_size), TPS(out_buff)); } } static NCURSES_INLINE void save_text(const char *fmt, const char *s, int len) { size_t s_len = strlen(s); if (len > (int) s_len) s_len = (size_t) len; get_space(s_len + 1); _nc_SPRINTF(TPS(out_buff) + TPS(out_used), _nc_SLIMIT(TPS(out_size) - TPS(out_used)) fmt, s); TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used)); } static NCURSES_INLINE void save_number(const char *fmt, int number, int len) { if (len < 30) len = 30; /* actually log10(MAX_INT)+1 */ get_space((size_t) len + 1); _nc_SPRINTF(TPS(out_buff) + TPS(out_used), _nc_SLIMIT(TPS(out_size) - TPS(out_used)) fmt, number); TPS(out_used) += strlen(TPS(out_buff) + TPS(out_used)); } static NCURSES_INLINE void save_char(int c) { if (c == 0) c = 0200; get_space((size_t) 1); TPS(out_buff)[TPS(out_used)++] = (char) c; } static NCURSES_INLINE void npush(int x) { if (TPS(stack_ptr) < STACKSIZE) { TPS(stack)[TPS(stack_ptr)].num_type = TRUE; TPS(stack)[TPS(stack_ptr)].data.num = x; TPS(stack_ptr)++; } else { DEBUG(2, ("npush: stack overflow: %s", _nc_visbuf(TPS(tparam_base)))); _nc_tparm_err++; } } static NCURSES_INLINE int npop(void) { int result = 0; if (TPS(stack_ptr) > 0) { TPS(stack_ptr)--; if (TPS(stack)[TPS(stack_ptr)].num_type) result = TPS(stack)[TPS(stack_ptr)].data.num; } else { DEBUG(2, ("npop: stack underflow: %s", _nc_visbuf(TPS(tparam_base)))); _nc_tparm_err++; } return result; } static NCURSES_INLINE void spush(char *x) { if (TPS(stack_ptr) < STACKSIZE) { TPS(stack)[TPS(stack_ptr)].num_type = FALSE; TPS(stack)[TPS(stack_ptr)].data.str = x; TPS(stack_ptr)++; } else { DEBUG(2, ("spush: stack overflow: %s", _nc_visbuf(TPS(tparam_base)))); _nc_tparm_err++; } } static NCURSES_INLINE char * spop(void) { static char dummy[] = ""; /* avoid const-cast */ char *result = dummy; if (TPS(stack_ptr) > 0) { TPS(stack_ptr)--; if (!TPS(stack)[TPS(stack_ptr)].num_type && TPS(stack)[TPS(stack_ptr)].data.str != 0) result = TPS(stack)[TPS(stack_ptr)].data.str; } else { DEBUG(2, ("spop: stack underflow: %s", _nc_visbuf(TPS(tparam_base)))); _nc_tparm_err++; } return result; } static NCURSES_INLINE const char * parse_format(const char *s, char *format, int *len) { *len = 0; if (format != 0) { bool done = FALSE; bool allowminus = FALSE; bool dot = FALSE; bool err = FALSE; char *fmt = format; int my_width = 0; int my_prec = 0; int value = 0; *len = 0; *format++ = '%'; while (*s != '\0' && !done) { switch (*s) { case 'c': /* FALLTHRU */ case 'd': /* FALLTHRU */ case 'o': /* FALLTHRU */ case 'x': /* FALLTHRU */ case 'X': /* FALLTHRU */ case 's': #ifdef EXP_XTERM_1005 case 'u': #endif *format++ = *s; done = TRUE; break; case '.': *format++ = *s++; if (dot) { err = TRUE; } else { /* value before '.' is the width */ dot = TRUE; my_width = value; } value = 0; break; case '#': *format++ = *s++; break; case ' ': *format++ = *s++; break; case ':': s++; allowminus = TRUE; break; case '-': if (allowminus) { *format++ = *s++; } else { done = TRUE; } break; default: if (isdigit(UChar(*s))) { value = (value * 10) + (*s - '0'); if (value > 10000) err = TRUE; *format++ = *s++; } else { done = TRUE; } } } /* * If we found an error, ignore (and remove) the flags. */ if (err) { my_width = my_prec = value = 0; format = fmt; *format++ = '%'; *format++ = *s; } /* * Any value after '.' is the precision. If we did not see '.', then * the value is the width. */ if (dot) my_prec = value; else my_width = value; *format = '\0'; /* return maximum string length in print */ *len = (my_width > my_prec) ? my_width : my_prec; } return s; } #define isUPPER(c) ((c) >= 'A' && (c) <= 'Z') #define isLOWER(c) ((c) >= 'a' && (c) <= 'z') #define tc_BUMP() if (level < 0 && number < 2) number++ /* * Analyze the string to see how many parameters we need from the varargs list, * and what their types are. We will only accept string parameters if they * appear as a %l or %s format following an explicit parameter reference (e.g., * %p2%s). All other parameters are numbers. * * 'number' counts coarsely the number of pop's we see in the string, and * 'popcount' shows the highest parameter number in the string. We would like * to simply use the latter count, but if we are reading termcap strings, there * may be cases that we cannot see the explicit parameter numbers. */ NCURSES_EXPORT(int) -_nc_tparm_analyze(const char *string, char *p_is_s[NUM_PARM], int *popcount) +_nc_tparm_analyze(const char *string, char **p_is_s, int *popcount) { size_t len2; int i; int lastpop = -1; int len; int number = 0; int level = -1; const char *cp = string; static char dummy[] = ""; if (cp == 0) return 0; if ((len2 = strlen(cp)) + 2 > TPS(fmt_size)) { TPS(fmt_size) += len2 + 2; TPS(fmt_buff) = typeRealloc(char, TPS(fmt_size), TPS(fmt_buff)); if (TPS(fmt_buff) == 0) return 0; } memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM); *popcount = 0; while ((cp - string) < (int) len2) { if (*cp == '%') { cp++; cp = parse_format(cp, TPS(fmt_buff), &len); switch (*cp) { default: break; case 'd': /* FALLTHRU */ case 'o': /* FALLTHRU */ case 'x': /* FALLTHRU */ case 'X': /* FALLTHRU */ case 'c': /* FALLTHRU */ #ifdef EXP_XTERM_1005 case 'u': #endif if (lastpop <= 0) { tc_BUMP(); } level -= 1; lastpop = -1; break; case 'l': case 's': if (lastpop > 0) { level -= 1; p_is_s[lastpop - 1] = dummy; } tc_BUMP(); break; case 'p': cp++; i = (UChar(*cp) - '0'); if (i >= 0 && i <= NUM_PARM) { ++level; lastpop = i; if (lastpop > *popcount) *popcount = lastpop; } break; case 'P': ++cp; break; case 'g': ++level; cp++; break; case S_QUOTE: ++level; cp += 2; lastpop = -1; break; case L_BRACE: ++level; cp++; while (isdigit(UChar(*cp))) { cp++; } break; case '+': case '-': case '*': case '/': case 'm': case 'A': case 'O': case '&': case '|': case '^': case '=': case '<': case '>': tc_BUMP(); level -= 1; /* pop 2, operate, push 1 */ lastpop = -1; break; case '!': case '~': tc_BUMP(); lastpop = -1; break; case 'i': /* will add 1 to first (usually two) parameters */ break; } } if (*cp != '\0') cp++; } if (number > NUM_PARM) number = NUM_PARM; return number; } static NCURSES_INLINE char * tparam_internal(int use_TPARM_ARG, const char *string, va_list ap) { char *p_is_s[NUM_PARM]; TPARM_ARG param[NUM_PARM]; int popcount = 0; int number; int num_args; int len; int level; int x, y; int i; const char *cp = string; size_t len2; bool termcap_hack; bool incremented_two; if (cp == NULL) { TR(TRACE_CALLS, ("%s: format is null", TPS(tname))); return NULL; } TPS(out_used) = 0; len2 = strlen(cp); /* * Find the highest parameter-number referred to in the format string. * Use this value to limit the number of arguments copied from the * variable-length argument list. */ number = _nc_tparm_analyze(cp, p_is_s, &popcount); if (TPS(fmt_buff) == 0) { TR(TRACE_CALLS, ("%s: error in analysis", TPS(tname))); return NULL; } incremented_two = FALSE; if (number > NUM_PARM) number = NUM_PARM; if (popcount > NUM_PARM) popcount = NUM_PARM; num_args = max(popcount, number); for (i = 0; i < num_args; i++) { /* * A few caps (such as plab_norm) have string-valued parms. * We'll have to assume that the caller knows the difference, since * a char* and an int may not be the same size on the stack. The * normal prototype for this uses 9 long's, which is consistent with * our va_arg() usage. */ if (p_is_s[i] != 0) { p_is_s[i] = va_arg(ap, char *); param[i] = 0; } else if (use_TPARM_ARG) { param[i] = va_arg(ap, TPARM_ARG); } else { param[i] = (TPARM_ARG) va_arg(ap, int); } } /* * This is a termcap compatibility hack. If there are no explicit pop * operations in the string, load the stack in such a way that * successive pops will grab successive parameters. That will make * the expansion of (for example) \E[%d;%dH work correctly in termcap * style, which means tparam() will expand termcap strings OK. */ TPS(stack_ptr) = 0; termcap_hack = FALSE; if (popcount == 0) { termcap_hack = TRUE; for (i = number - 1; i >= 0; i--) { if (p_is_s[i]) spush(p_is_s[i]); else npush((int) param[i]); } } #ifdef TRACE if (USE_TRACEF(TRACE_CALLS)) { for (i = 0; i < num_args; i++) { if (p_is_s[i] != 0) { save_text(", %s", _nc_visbuf(p_is_s[i]), 0); } else if ((long) param[i] > MAX_OF_TYPE(NCURSES_INT2) || (long) param[i] < 0) { _tracef("BUG: problem with tparm parameter #%d of %d", i + 1, num_args); break; } else { save_number(", %d", (int) param[i], 0); } } _tracef(T_CALLED("%s(%s%s)"), TPS(tname), _nc_visbuf(cp), TPS(out_buff)); TPS(out_used) = 0; _nc_unlock_global(tracef); } #endif /* TRACE */ while ((cp - string) < (int) len2) { if (*cp != '%') { save_char(UChar(*cp)); } else { TPS(tparam_base) = cp++; cp = parse_format(cp, TPS(fmt_buff), &len); switch (*cp) { default: break; case '%': save_char('%'); break; case 'd': /* FALLTHRU */ case 'o': /* FALLTHRU */ case 'x': /* FALLTHRU */ case 'X': /* FALLTHRU */ save_number(TPS(fmt_buff), npop(), len); break; case 'c': /* FALLTHRU */ save_char(npop()); break; #ifdef EXP_XTERM_1005 case 'u': { unsigned char target[10]; unsigned source = (unsigned) npop(); int rc = _nc_conv_to_utf8(target, source, (unsigned) sizeof(target)); int n; for (n = 0; n < rc; ++n) { save_char(target[n]); } } break; #endif case 'l': npush((int) strlen(spop())); break; case 's': save_text(TPS(fmt_buff), spop(), len); break; case 'p': cp++; i = (UChar(*cp) - '1'); if (i >= 0 && i < NUM_PARM) { if (p_is_s[i]) { spush(p_is_s[i]); } else { npush((int) param[i]); } } break; case 'P': cp++; if (isUPPER(*cp)) { i = (UChar(*cp) - 'A'); TPS(static_vars)[i] = npop(); } else if (isLOWER(*cp)) { i = (UChar(*cp) - 'a'); TPS(dynamic_var)[i] = npop(); } break; case 'g': cp++; if (isUPPER(*cp)) { i = (UChar(*cp) - 'A'); npush(TPS(static_vars)[i]); } else if (isLOWER(*cp)) { i = (UChar(*cp) - 'a'); npush(TPS(dynamic_var)[i]); } break; case S_QUOTE: cp++; npush(UChar(*cp)); cp++; break; case L_BRACE: number = 0; cp++; while (isdigit(UChar(*cp))) { number = (number * 10) + (UChar(*cp) - '0'); cp++; } npush(number); break; case '+': npush(npop() + npop()); break; case '-': y = npop(); x = npop(); npush(x - y); break; case '*': npush(npop() * npop()); break; case '/': y = npop(); x = npop(); npush(y ? (x / y) : 0); break; case 'm': y = npop(); x = npop(); npush(y ? (x % y) : 0); break; case 'A': y = npop(); x = npop(); npush(y && x); break; case 'O': y = npop(); x = npop(); npush(y || x); break; case '&': npush(npop() & npop()); break; case '|': npush(npop() | npop()); break; case '^': npush(npop() ^ npop()); break; case '=': y = npop(); x = npop(); npush(x == y); break; case '<': y = npop(); x = npop(); npush(x < y); break; case '>': y = npop(); x = npop(); npush(x > y); break; case '!': npush(!npop()); break; case '~': npush(~npop()); break; case 'i': /* * Increment the first two parameters -- if they are numbers * rather than strings. As a side effect, assign into the * stack; if this is termcap, then the stack was populated * using the termcap hack above rather than via the terminfo * 'p' case. */ if (!incremented_two) { incremented_two = TRUE; if (p_is_s[0] == 0) { param[0]++; if (termcap_hack) TPS(stack)[0].data.num = (int) param[0]; } if (p_is_s[1] == 0) { param[1]++; if (termcap_hack) TPS(stack)[1].data.num = (int) param[1]; } } break; case '?': break; case 't': x = npop(); if (!x) { /* scan forward for %e or %; at level zero */ cp++; level = 0; while (*cp) { if (*cp == '%') { cp++; if (*cp == '?') level++; else if (*cp == ';') { if (level > 0) level--; else break; } else if (*cp == 'e' && level == 0) break; } if (*cp) cp++; } } break; case 'e': /* scan forward for a %; at level zero */ cp++; level = 0; while (*cp) { if (*cp == '%') { cp++; if (*cp == '?') level++; else if (*cp == ';') { if (level > 0) level--; else break; } } if (*cp) cp++; } break; case ';': break; } /* endswitch (*cp) */ } /* endelse (*cp == '%') */ if (*cp == '\0') break; cp++; } /* endwhile (*cp) */ get_space((size_t) 1); TPS(out_buff)[TPS(out_used)] = '\0'; T((T_RETURN("%s"), _nc_visbuf(TPS(out_buff)))); return (TPS(out_buff)); } #if NCURSES_TPARM_VARARGS #define tparm_varargs tparm #else #define tparm_proto tparm #endif NCURSES_EXPORT(char *) tparm_varargs(const char *string, ...) { va_list ap; char *result; _nc_tparm_err = 0; va_start(ap, string); #ifdef TRACE TPS(tname) = "tparm"; #endif /* TRACE */ result = tparam_internal(TRUE, string, ap); va_end(ap); return result; } #if !NCURSES_TPARM_VARARGS NCURSES_EXPORT(char *) tparm_proto(const char *string, TPARM_ARG a1, TPARM_ARG a2, TPARM_ARG a3, TPARM_ARG a4, TPARM_ARG a5, TPARM_ARG a6, TPARM_ARG a7, TPARM_ARG a8, TPARM_ARG a9) { return tparm_varargs(string, a1, a2, a3, a4, a5, a6, a7, a8, a9); } #endif /* NCURSES_TPARM_VARARGS */ NCURSES_EXPORT(char *) tiparm(const char *string, ...) { va_list ap; char *result; _nc_tparm_err = 0; va_start(ap, string); #ifdef TRACE TPS(tname) = "tiparm"; #endif /* TRACE */ result = tparam_internal(FALSE, string, ap); va_end(ap); return result; }