1 /****************************************************************************
2 * Copyright 2019-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2017,2018 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996 on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
37 #include <curses.priv.h>
42 #define CUR SP_TERMTYPE
45 MODULE_ID("$Id: lib_screen.c,v 1.101 2021/06/26 20:43:19 tom Exp $")
47 #define MAX_SIZE 0x3fff /* 16k is big enough for a window or pad */
55 #if USE_STRING_HACKS && HAVE_SNPRINTF
56 #define ARG_SLIMIT(name) size_t name,
58 #define ARG_SLIMIT(name) /* nothing */
61 #define CUR_SLIMIT _nc_SLIMIT(limit - (size_t) (target - base))
62 #define TOP_SLIMIT _nc_SLIMIT(sizeof(buffer))
65 * Use 0x888888 as the magic number for new-format files, since it cannot be
66 * mistaken for the _cury/_curx pair of 16-bit numbers which start the old
67 * format. It happens to be unused in the file 5.22 database (2015/03/07).
69 static const char my_magic[] =
70 {'\210', '\210', '\210', '\210', 0};
72 #if NCURSES_EXT_PUTWIN
79 ,pSIZE /* NCURSES_SIZE_T */
97 #define DATA(name) { { #name }, A_##name }
98 static const SCR_ATTRS scr_attrs[] =
123 #define sizeof2(type,name) sizeof(((type *)0)->name)
124 #define DATA(name, type) { { #name }, type, sizeof2(WINDOW, name), offsetof(WINDOW, name) }
126 static const SCR_PARAMS scr_params[] =
134 DATA(_flags, pSHORT),
137 DATA(_notimeout, pBOOL),
139 DATA(_leaveok, pBOOL),
140 DATA(_scroll, pBOOL),
145 DATA(_use_keypad, pBOOL),
147 DATA(_regtop, pSIZE),
148 DATA(_regbottom, pSIZE),
149 DATA(_pad._pad_y, pSIZE),
150 DATA(_pad._pad_x, pSIZE),
151 DATA(_pad._pad_top, pSIZE),
152 DATA(_pad._pad_left, pSIZE),
153 DATA(_pad._pad_bottom, pSIZE),
154 DATA(_pad._pad_right, pSIZE),
155 DATA(_yoffset, pSIZE),
157 DATA(_bkgrnd, pCCHAR),
158 #if NCURSES_EXT_COLORS
165 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
168 * Allocate and read a line of text. Caller must free it.
174 char *result = malloc(limit);
184 if (used + 2 >= limit) {
186 buffer = realloc(result, limit);
197 result[used++] = (char) ch;
199 } while (ch != '\n');
202 result[--used] = '\0';
203 T(("READ:%s", result));
204 } else if (used == 0) {
213 decode_attr(char *source, attr_t *target, int *color)
217 T(("decode_attr '%s'", source));
220 if (source[0] == MARKER && source[1] == L_CURL) {
223 } else if (source[0] == R_CURL) {
230 if (source[0] == GUTTER) {
232 } else if (*next == 'C') {
236 while (isdigit(UChar(*next))) {
237 value = value * 10 + (*next++ - '0');
240 pair = (unsigned) ((value > 256)
242 : COLOR_PAIR(value));
246 while (isalnum(UChar(*next))) {
249 for (n = 0; n < SIZEOF(scr_attrs); ++n) {
250 if ((size_t) (next - source) == strlen(scr_attrs[n].name)) {
251 if (scr_attrs[n].attr) {
252 *target |= scr_attrs[n].attr;
269 decode_char(char *source, int *target)
273 const char digits[] = "0123456789abcdef";
275 T(("decode_char '%s'", source));
308 while (limit-- > 0) {
309 char *find = strchr(digits, *source++);
310 int ch = (find != 0) ? (int) (find - digits) : -1;
312 if (ch >= 0 && ch < base) {
326 decode_chtype(char *source, chtype fillin, chtype *target)
328 attr_t attr = ChAttrOf(fillin);
329 int color = PAIR_NUMBER((int) attr);
332 T(("decode_chtype '%s'", source));
333 source = decode_attr(source, &attr, &color);
334 source = decode_char(source, &value);
335 *target = (ChCharOf(value) | attr | (chtype) COLOR_PAIR(color));
336 /* FIXME - ignore combining characters */
342 decode_cchar(char *source, cchar_t *fillin, cchar_t *target)
345 attr_t attr = fillin->attr;
346 wchar_t chars[CCHARW_MAX];
350 T(("decode_cchar '%s'", source));
352 #if NCURSES_EXT_COLORS
353 color = fillin->ext_color;
355 color = (int) PAIR_NUMBER(attr);
357 source = decode_attr(source, &attr, &color);
358 memset(chars, 0, sizeof(chars));
359 source = decode_char(source, &value);
360 chars[0] = (wchar_t) value;
361 /* handle combining characters */
362 while (source[0] == MARKER && source[1] == APPEND) {
364 source = decode_char(source, &value);
365 if (++append < CCHARW_MAX) {
366 chars[append] = (wchar_t) value;
369 setcchar(target, chars, attr, (short) color, &color);
375 read_win(WINDOW *win, FILE *fp)
385 memset(win, 0, sizeof(WINDOW));
389 char *txt = read_txt(fp);
393 if (!strcmp(txt, "rows:")) {
398 if ((value = strchr(txt, '=')) == 0) {
403 name = !strcmp(txt, "flag") ? value : txt;
404 for (n = 0; n < SIZEOF(scr_params); ++n) {
405 if (!strcmp(name, scr_params[n].name)) {
406 void *data = (void *) ((char *) win + scr_params[n].offset);
408 switch (scr_params[n].type) {
410 (void) decode_attr(value, data, &color);
413 *(bool *) data = TRUE;
417 decode_chtype(value, prior2, data);
420 *(int *) data = atoi(value);
423 *(short *) data = (short) atoi(value);
426 *(NCURSES_SIZE_T *) data = (NCURSES_SIZE_T) atoi(value);
431 decode_cchar(value, &prior, data);
444 read_row(char *source, NCURSES_CH_T *prior, NCURSES_CH_T *target, int length)
446 while (*source != '\0' && length > 0) {
450 source = decode_cchar(source, prior, target);
451 len = _nc_wacs_width(target->chars[0]);
455 SetWidecExt(CHDEREF(target), 0);
456 for (n = 1; n < len; ++n) {
457 target[n] = target[0];
458 SetWidecExt(CHDEREF(target), n);
464 source = decode_chtype(source, *prior, target);
470 while (length-- > 0) {
473 /* FIXME - see what error conditions should apply if I need to return ERR */
476 #endif /* NCURSES_EXT_PUTWIN */
479 * Originally, getwin/putwin used fread/fwrite, because they used binary data.
480 * The new format uses printable ASCII, which does not have as predictable
481 * sizes. Consequently, we use buffered I/O, e.g., fgetc/fprintf, which need
482 * special handling if we want to read screen dumps from an older library.
485 read_block(void *target, size_t length, FILE *fp)
488 char *buffer = target;
491 while (length-- != 0) {
497 *buffer++ = (char) ch;
502 NCURSES_EXPORT(WINDOW *)
503 NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
506 bool old_format = FALSE;
508 T((T_CALLED("getwin(%p)"), (void *) filep));
515 * Read the first 4 bytes to determine first if this is an old-format
516 * screen-dump, or new-format.
518 if (read_block(&tmp, (size_t) 4, filep) < 0) {
522 * If this is a new-format file, and we do not support it, give up.
524 if (!memcmp(&tmp, my_magic, (size_t) 4)) {
525 #if NCURSES_EXT_PUTWIN
526 if (read_win(&tmp, filep) < 0)
529 } else if (read_block(((char *) &tmp) + 4, sizeof(WINDOW) - 4, filep) < 0) {
536 * Check the window-size:
538 if (tmp._maxy == 0 ||
539 tmp._maxy > MAX_SIZE ||
541 tmp._maxx > MAX_SIZE) {
545 if (tmp._flags & _ISPAD) {
546 nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx
550 nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
552 tmp._maxx + 1, 0, 0);
556 * We deliberately do not restore the _parx, _pary, or _parent
557 * fields, because the window hierarchy within which they
558 * made sense is probably gone.
562 size_t linesize = sizeof(NCURSES_CH_T) * (size_t) (tmp._maxx + 1);
564 nwin->_curx = tmp._curx;
565 nwin->_cury = tmp._cury;
566 nwin->_maxy = tmp._maxy;
567 nwin->_maxx = tmp._maxx;
568 nwin->_begy = tmp._begy;
569 nwin->_begx = tmp._begx;
570 nwin->_yoffset = tmp._yoffset;
571 nwin->_flags = tmp._flags & ~(_SUBWIN);
573 WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp);
574 nwin->_nc_bkgd = tmp._nc_bkgd;
576 nwin->_notimeout = tmp._notimeout;
577 nwin->_clear = tmp._clear;
578 nwin->_leaveok = tmp._leaveok;
579 nwin->_idlok = tmp._idlok;
580 nwin->_idcok = tmp._idcok;
581 nwin->_immed = tmp._immed;
582 nwin->_scroll = tmp._scroll;
583 nwin->_sync = tmp._sync;
584 nwin->_use_keypad = tmp._use_keypad;
585 nwin->_delay = tmp._delay;
587 nwin->_regtop = tmp._regtop;
588 nwin->_regbottom = tmp._regbottom;
590 if (tmp._flags & _ISPAD)
591 nwin->_pad = tmp._pad;
594 T(("reading old-format screen dump"));
595 for (n = 0; n <= nwin->_maxy; n++) {
596 if (read_block(nwin->_line[n].text, linesize, filep) < 0) {
602 #if NCURSES_EXT_PUTWIN
606 NCURSES_CH_T prior = blank;
608 T(("reading new-format screen dump"));
609 for (n = 0; n <= nwin->_maxy; n++) {
613 if ((txt = read_txt(filep)) == 0) {
614 T(("...failed to read string for row %d", n + 1));
618 row = strtol(txt, &next, 10);
619 if (row != (n + 1) || *next != ':') {
620 T(("...failed to read row-number %d", n + 1));
625 if (read_row(++next, &prior, nwin->_line[n].text, tmp._maxx
627 T(("...failed to read cells for row %d", n + 1));
648 NCURSES_EXPORT(WINDOW *)
651 return NCURSES_SP_NAME(getwin) (CURRENT_SCREEN, filep);
655 #if NCURSES_EXT_PUTWIN
657 encode_attr(char *target, ARG_SLIMIT(limit)
663 #if USE_STRING_HACKS && HAVE_SNPRINTF
666 source &= ~A_CHARTEXT;
667 prior &= ~A_CHARTEXT;
670 if ((source != prior) || (source_color != prior_color)) {
677 for (n = 0; n < SIZEOF(scr_attrs); ++n) {
678 if ((source & scr_attrs[n].attr) != 0 ||
679 ((source & ALL_BUT_COLOR) == 0 &&
680 (scr_attrs[n].attr == A_NORMAL))) {
686 _nc_STRCPY(target, scr_attrs[n].name, limit);
687 target += strlen(target);
690 if (source_color != prior_color) {
693 _nc_SPRINTF(target, CUR_SLIMIT "C%d", source_color);
694 target += strlen(target);
703 encode_cell(char *target, ARG_SLIMIT(limit) CARG_CH_T source, CARG_CH_T previous)
705 #if USE_STRING_HACKS && HAVE_SNPRINTF
710 int source_pair = GetPair(*source);
711 int previous_pair = GetPair(*previous);
714 if ((previous->attr != source->attr) || (previous_pair != source_pair)) {
715 encode_attr(target, CUR_SLIMIT
721 target += strlen(target);
722 #if NCURSES_EXT_COLORS
723 if (previous->ext_color != source->ext_color) {
724 _nc_SPRINTF(target, CUR_SLIMIT
725 "%c%cC%d%c", MARKER, L_CURL, source->ext_color, R_CURL);
728 for (n = 0; n < SIZEOF(source->chars); ++n) {
729 unsigned uch = (unsigned) source->chars[n];
738 _nc_SPRINTF(target, CUR_SLIMIT "U%08x", uch);
739 } else if (uch > 0xff) {
740 _nc_SPRINTF(target, CUR_SLIMIT "u%04x", uch);
741 } else if (uch < 32 || uch >= 127) {
742 _nc_SPRINTF(target, CUR_SLIMIT "%03o", uch & 0xff);
746 _nc_STRCPY(target, "s", limit);
754 _nc_SPRINTF(target, CUR_SLIMIT "%c", uch);
758 target += strlen(target);
761 chtype ch = CharOfD(source);
764 if (AttrOfD(previous) != AttrOfD(source)) {
765 encode_attr(target, CUR_SLIMIT
771 target += strlen(target);
773 if (ch < 32 || ch >= 127) {
774 _nc_SPRINTF(target, CUR_SLIMIT "%03o", UChar(ch));
778 _nc_STRCPY(target, "s", limit);
786 _nc_SPRINTF(target, CUR_SLIMIT "%c", UChar(ch));
795 putwin(WINDOW *win, FILE *filep)
799 T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep));
801 #if NCURSES_EXT_PUTWIN
803 const char *version = curses_version();
805 NCURSES_CH_T last_cell;
808 memset(&last_cell, 0, sizeof(last_cell));
813 * Our magic number is technically nonprinting, but aside from that,
814 * all of the file is printable ASCII.
816 #define PUTS(s) if (fputs(s, filep) == EOF || ferror(filep)) returnCode(code)
820 for (y = 0; y < (int) SIZEOF(scr_params); ++y) {
821 const char *name = scr_params[y].name;
822 const char *data = (char *) win + scr_params[y].offset;
823 const void *dp = (const void *) data;
827 if (!strncmp(name, "_pad.", (size_t) 5) && !(win->_flags & _ISPAD)) {
830 switch (scr_params[y].type) {
832 attr = (*(const attr_t *) dp) & ~A_CHARTEXT;
833 encode_attr(buffer, TOP_SLIMIT
834 (*(const attr_t *) dp) & ~A_CHARTEXT,
836 COLOR_PAIR((int) attr),
840 if (!(*(const bool *) data)) {
843 _nc_STRCPY(buffer, name, sizeof(buffer));
847 attr = (*(const attr_t *) dp);
848 encode_attr(buffer, TOP_SLIMIT
849 * (const attr_t *) dp,
851 COLOR_PAIR((int) attr),
855 if (!(*(const int *) dp))
857 _nc_SPRINTF(buffer, TOP_SLIMIT
858 "%d", *(const int *) dp);
861 if (!(*(const short *) dp))
863 _nc_SPRINTF(buffer, TOP_SLIMIT
864 "%d", *(const short *) dp);
867 if (!(*(const NCURSES_SIZE_T *) dp))
869 _nc_SPRINTF(buffer, TOP_SLIMIT
870 "%d", *(const NCURSES_SIZE_T *) dp);
874 encode_cell(buffer, TOP_SLIMIT
875 (CARG_CH_T) dp, CHREF(last_cell));
880 * Only write non-default data.
882 if (*buffer != '\0') {
883 if (fprintf(filep, "%s=%s\n", name, buffer) <= 0
889 fprintf(filep, "rows:\n");
890 for (y = 0; y <= win->_maxy; y++) {
891 NCURSES_CH_T *data = win->_line[y].text;
893 if (fprintf(filep, "%d:", y + 1) <= 0
896 for (x = 0; x <= win->_maxx; x++) {
898 int len = _nc_wacs_width(data[x].chars[0]);
899 encode_cell(buffer, TOP_SLIMIT CHREF(data[x]), CHREF(last_cell));
905 encode_cell(buffer, TOP_SLIMIT CHREF(data[x]), CHREF(last_cell));
916 * This is the original putwin():
917 * A straight binary dump is simple, but its format can depend on whether
918 * ncurses is compiled with wide-character support, and also may depend
919 * on the version of ncurses, e.g., if the WINDOW structure is extended.
922 size_t len = (size_t) (win->_maxx + 1);
926 if (fwrite(win, sizeof(WINDOW), (size_t) 1, filep) != 1
930 for (y = 0; y <= win->_maxy; y++) {
931 if (fwrite(win->_line[y].text,
932 sizeof(NCURSES_CH_T), len, filep) != len
944 NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file)
949 T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
951 if (_nc_access(file, R_OK) >= 0
952 && (fp = safe_fopen(file, BIN_R)) != 0) {
953 delwin(NewScreen(SP_PARM));
954 NewScreen(SP_PARM) = getwin(fp);
956 newscr = NewScreen(SP_PARM);
959 if (NewScreen(SP_PARM) != 0) {
968 scr_restore(const char *file)
970 return NCURSES_SP_NAME(scr_restore) (CURRENT_SCREEN, file);
975 scr_dump(const char *file)
980 T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
982 if (_nc_access(file, W_OK) < 0
983 || (fp = safe_fopen(file, BIN_W)) == 0) {
986 (void) putwin(newscr, fp);
994 NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file)
998 T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
1001 #ifdef USE_TERM_DRIVER
1002 InfoOf(SP_PARM).caninit
1004 !(exit_ca_mode && non_rev_rmcup)
1009 if (_nc_access(file, R_OK) >= 0
1010 && (fp = safe_fopen(file, BIN_R)) != 0) {
1011 delwin(CurScreen(SP_PARM));
1012 CurScreen(SP_PARM) = getwin(fp);
1014 curscr = CurScreen(SP_PARM);
1017 if (CurScreen(SP_PARM) != 0) {
1025 #if NCURSES_SP_FUNCS
1027 scr_init(const char *file)
1029 return NCURSES_SP_NAME(scr_init) (CURRENT_SCREEN, file);
1034 NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file)
1038 T((T_CALLED("scr_set(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
1040 if (NCURSES_SP_NAME(scr_init) (NCURSES_SP_ARGx file) == OK) {
1041 delwin(NewScreen(SP_PARM));
1042 NewScreen(SP_PARM) = dupwin(curscr);
1044 newscr = NewScreen(SP_PARM);
1046 if (NewScreen(SP_PARM) != 0) {
1053 #if NCURSES_SP_FUNCS
1055 scr_set(const char *file)
1057 return NCURSES_SP_NAME(scr_set) (CURRENT_SCREEN, file);