1 /****************************************************************************
2 * Copyright (c) 1998-2018,2019 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996 on *
33 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
36 #include <curses.priv.h>
41 #define CUR SP_TERMTYPE
44 MODULE_ID("$Id: lib_screen.c,v 1.94 2019/03/23 23:47:16 tom Exp $")
46 #define MAX_SIZE 0x3fff /* 16k is big enough for a window or pad */
54 #if USE_STRING_HACKS && HAVE_SNPRINTF
55 #define ARG_SLIMIT(name) size_t name,
57 #define ARG_SLIMIT(name) /* nothing */
60 #define CUR_SLIMIT _nc_SLIMIT(limit - (target - base))
61 #define TOP_SLIMIT _nc_SLIMIT(sizeof(buffer))
64 * Use 0x888888 as the magic number for new-format files, since it cannot be
65 * mistaken for the _cury/_curx pair of 16-bit numbers which start the old
66 * format. It happens to be unused in the file 5.22 database (2015/03/07).
68 static const char my_magic[] =
69 {'\210', '\210', '\210', '\210'};
71 #if NCURSES_EXT_PUTWIN
78 ,pSIZE /* NCURSES_SIZE_T */
96 #define DATA(name) { { #name }, A_##name }
97 static const SCR_ATTRS scr_attrs[] =
122 #define sizeof2(type,name) sizeof(((type *)0)->name)
123 #define DATA(name, type) { { #name }, type, sizeof2(WINDOW, name), offsetof(WINDOW, name) }
125 static const SCR_PARAMS scr_params[] =
133 DATA(_flags, pSHORT),
136 DATA(_notimeout, pBOOL),
138 DATA(_leaveok, pBOOL),
139 DATA(_scroll, pBOOL),
144 DATA(_use_keypad, pBOOL),
146 DATA(_regtop, pSIZE),
147 DATA(_regbottom, pSIZE),
148 DATA(_pad._pad_y, pSIZE),
149 DATA(_pad._pad_x, pSIZE),
150 DATA(_pad._pad_top, pSIZE),
151 DATA(_pad._pad_left, pSIZE),
152 DATA(_pad._pad_bottom, pSIZE),
153 DATA(_pad._pad_right, pSIZE),
154 DATA(_yoffset, pSIZE),
156 DATA(_bkgrnd, pCCHAR),
157 #if NCURSES_EXT_COLORS
164 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT);
167 * Allocate and read a line of text. Caller must free it.
173 char *result = malloc(limit);
183 if (used + 2 >= limit) {
185 buffer = realloc(result, limit);
196 result[used++] = (char) ch;
198 } while (ch != '\n');
201 result[--used] = '\0';
202 T(("READ:%s", result));
203 } else if (used == 0) {
212 decode_attr(char *source, attr_t *target, int *color)
216 T(("decode_attr '%s'", source));
219 if (source[0] == MARKER && source[1] == L_CURL) {
222 } else if (source[0] == R_CURL) {
229 if (source[0] == GUTTER) {
231 } else if (*next == 'C') {
235 while (isdigit(UChar(*next))) {
236 value = value * 10 + (*next++ - '0');
239 pair = (unsigned) ((value > 256)
241 : COLOR_PAIR(value));
245 while (isalnum(UChar(*next))) {
248 for (n = 0; n < SIZEOF(scr_attrs); ++n) {
249 if ((size_t) (next - source) == strlen(scr_attrs[n].name)) {
250 if (scr_attrs[n].attr) {
251 *target |= scr_attrs[n].attr;
268 decode_char(char *source, int *target)
272 const char digits[] = "0123456789abcdef";
274 T(("decode_char '%s'", source));
307 while (limit-- > 0) {
308 char *find = strchr(digits, *source++);
309 int ch = (find != 0) ? (int) (find - digits) : -1;
311 if (ch >= 0 && ch < base) {
325 decode_chtype(char *source, chtype fillin, chtype *target)
327 attr_t attr = ChAttrOf(fillin);
328 int color = PAIR_NUMBER((int) attr);
331 T(("decode_chtype '%s'", source));
332 source = decode_attr(source, &attr, &color);
333 source = decode_char(source, &value);
334 *target = (ChCharOf(value) | attr | (chtype) COLOR_PAIR(color));
335 /* FIXME - ignore combining characters */
341 decode_cchar(char *source, cchar_t *fillin, cchar_t *target)
344 attr_t attr = fillin->attr;
345 wchar_t chars[CCHARW_MAX];
349 T(("decode_cchar '%s'", source));
351 #if NCURSES_EXT_COLORS
352 color = fillin->ext_color;
354 color = (int) PAIR_NUMBER(attr);
356 source = decode_attr(source, &attr, &color);
357 memset(chars, 0, sizeof(chars));
358 source = decode_char(source, &value);
359 chars[0] = (wchar_t) value;
360 /* handle combining characters */
361 while (source[0] == MARKER && source[1] == APPEND) {
363 source = decode_char(source, &value);
364 if (++append < CCHARW_MAX) {
365 chars[append] = (wchar_t) value;
368 setcchar(target, chars, attr, (short) color, &color);
374 read_win(WINDOW *win, FILE *fp)
384 memset(win, 0, sizeof(WINDOW));
388 char *txt = read_txt(fp);
392 if (!strcmp(txt, "rows:")) {
397 if ((value = strchr(txt, '=')) == 0) {
402 name = !strcmp(txt, "flag") ? value : txt;
403 for (n = 0; n < SIZEOF(scr_params); ++n) {
404 if (!strcmp(name, scr_params[n].name)) {
405 void *data = (void *) ((char *) win + scr_params[n].offset);
407 switch (scr_params[n].type) {
409 (void) decode_attr(value, data, &color);
412 *(bool *) data = TRUE;
416 decode_chtype(value, prior2, data);
419 *(int *) data = atoi(value);
422 *(short *) data = (short) atoi(value);
425 *(NCURSES_SIZE_T *) data = (NCURSES_SIZE_T) atoi(value);
430 decode_cchar(value, &prior, data);
443 read_row(char *source, NCURSES_CH_T * prior, NCURSES_CH_T * target, int length)
445 while (*source != '\0' && length > 0) {
449 source = decode_cchar(source, prior, target);
450 len = wcwidth(target->chars[0]);
454 SetWidecExt(CHDEREF(target), 0);
455 for (n = 1; n < len; ++n) {
456 target[n] = target[0];
457 SetWidecExt(CHDEREF(target), n);
463 source = decode_chtype(source, *prior, target);
469 while (length-- > 0) {
472 /* FIXME - see what error conditions should apply if I need to return ERR */
475 #endif /* NCURSES_EXT_PUTWIN */
478 * Originally, getwin/putwin used fread/fwrite, because they used binary data.
479 * The new format uses printable ASCII, which does not have as predictable
480 * sizes. Consequently, we use buffered I/O, e.g., fgetc/fprintf, which need
481 * special handling if we want to read screen dumps from an older library.
484 read_block(void *target, size_t length, FILE *fp)
487 char *buffer = target;
490 while (length-- != 0) {
496 *buffer++ = (char) ch;
501 NCURSES_EXPORT(WINDOW *)
502 NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
505 bool old_format = FALSE;
507 T((T_CALLED("getwin(%p)"), (void *) filep));
514 * Read the first 4 bytes to determine first if this is an old-format
515 * screen-dump, or new-format.
517 if (read_block(&tmp, 4, filep) < 0) {
521 * If this is a new-format file, and we do not support it, give up.
523 if (!memcmp(&tmp, my_magic, 4)) {
524 #if NCURSES_EXT_PUTWIN
525 if (read_win(&tmp, filep) < 0)
528 } else if (read_block(((char *) &tmp) + 4, sizeof(WINDOW) - 4, filep) < 0) {
535 * Check the window-size:
537 if (tmp._maxy == 0 ||
538 tmp._maxy > MAX_SIZE ||
540 tmp._maxx > MAX_SIZE) {
544 if (tmp._flags & _ISPAD) {
545 nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx
549 nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
551 tmp._maxx + 1, 0, 0);
555 * We deliberately do not restore the _parx, _pary, or _parent
556 * fields, because the window hierarchy within which they
557 * made sense is probably gone.
561 size_t linesize = sizeof(NCURSES_CH_T) * (size_t) (tmp._maxx + 1);
563 nwin->_curx = tmp._curx;
564 nwin->_cury = tmp._cury;
565 nwin->_maxy = tmp._maxy;
566 nwin->_maxx = tmp._maxx;
567 nwin->_begy = tmp._begy;
568 nwin->_begx = tmp._begx;
569 nwin->_yoffset = tmp._yoffset;
570 nwin->_flags = tmp._flags & ~(_SUBWIN);
572 WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp);
573 nwin->_nc_bkgd = tmp._nc_bkgd;
575 nwin->_notimeout = tmp._notimeout;
576 nwin->_clear = tmp._clear;
577 nwin->_leaveok = tmp._leaveok;
578 nwin->_idlok = tmp._idlok;
579 nwin->_idcok = tmp._idcok;
580 nwin->_immed = tmp._immed;
581 nwin->_scroll = tmp._scroll;
582 nwin->_sync = tmp._sync;
583 nwin->_use_keypad = tmp._use_keypad;
584 nwin->_delay = tmp._delay;
586 nwin->_regtop = tmp._regtop;
587 nwin->_regbottom = tmp._regbottom;
589 if (tmp._flags & _ISPAD)
590 nwin->_pad = tmp._pad;
593 T(("reading old-format screen dump"));
594 for (n = 0; n <= nwin->_maxy; n++) {
595 if (read_block(nwin->_line[n].text, linesize, filep) < 0) {
601 #if NCURSES_EXT_PUTWIN
605 NCURSES_CH_T prior = blank;
607 T(("reading new-format screen dump"));
608 for (n = 0; n <= nwin->_maxy; n++) {
612 if ((txt = read_txt(filep)) == 0) {
613 T(("...failed to read string for row %d", n + 1));
617 row = strtol(txt, &next, 10);
618 if (row != (n + 1) || *next != ':') {
619 T(("...failed to read row-number %d", n + 1));
624 if (read_row(++next, &prior, nwin->_line[n].text, tmp._maxx
626 T(("...failed to read cells for row %d", n + 1));
647 NCURSES_EXPORT(WINDOW *)
650 return NCURSES_SP_NAME(getwin) (CURRENT_SCREEN, filep);
654 #if NCURSES_EXT_PUTWIN
656 encode_attr(char *target, ARG_SLIMIT(limit)
662 #if USE_STRING_HACKS && HAVE_SNPRINTF
665 source &= ~A_CHARTEXT;
666 prior &= ~A_CHARTEXT;
669 if ((source != prior) || (source_color != prior_color)) {
676 for (n = 0; n < SIZEOF(scr_attrs); ++n) {
677 if ((source & scr_attrs[n].attr) != 0 ||
678 ((source & ALL_BUT_COLOR) == 0 &&
679 (scr_attrs[n].attr == A_NORMAL))) {
685 _nc_STRCPY(target, scr_attrs[n].name, limit);
686 target += strlen(target);
689 if (source_color != prior_color) {
692 _nc_SPRINTF(target, CUR_SLIMIT "C%d", source_color);
693 target += strlen(target);
702 encode_cell(char *target, ARG_SLIMIT(limit) CARG_CH_T source, CARG_CH_T previous)
704 #if USE_STRING_HACKS && HAVE_SNPRINTF
709 int source_pair = GetPair(*source);
710 int previous_pair = GetPair(*previous);
713 if ((previous->attr != source->attr) || (previous_pair != source_pair)) {
714 encode_attr(target, CUR_SLIMIT
720 target += strlen(target);
721 #if NCURSES_EXT_COLORS
722 if (previous->ext_color != source->ext_color) {
723 _nc_SPRINTF(target, CUR_SLIMIT
724 "%c%cC%d%c", MARKER, L_CURL, source->ext_color, R_CURL);
727 for (n = 0; n < SIZEOF(source->chars); ++n) {
728 unsigned uch = (unsigned) source->chars[n];
737 _nc_SPRINTF(target, CUR_SLIMIT "U%08x", uch);
738 } else if (uch > 0xff) {
739 _nc_SPRINTF(target, CUR_SLIMIT "u%04x", uch);
740 } else if (uch < 32 || uch >= 127) {
741 _nc_SPRINTF(target, CUR_SLIMIT "%03o", uch & 0xff);
745 _nc_STRCPY(target, "s", limit);
753 _nc_SPRINTF(target, CUR_SLIMIT "%c", uch);
757 target += strlen(target);
760 chtype ch = CharOfD(source);
763 if (AttrOfD(previous) != AttrOfD(source)) {
764 encode_attr(target, CUR_SLIMIT
770 target += strlen(target);
772 if (ch < 32 || ch >= 127) {
773 _nc_SPRINTF(target, CUR_SLIMIT "%03o", UChar(ch));
777 _nc_STRCPY(target, "s", limit);
785 _nc_SPRINTF(target, CUR_SLIMIT "%c", UChar(ch));
794 putwin(WINDOW *win, FILE *filep)
798 T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep));
800 #if NCURSES_EXT_PUTWIN
802 const char *version = curses_version();
804 NCURSES_CH_T last_cell;
807 memset(&last_cell, 0, sizeof(last_cell));
812 * Our magic number is technically nonprinting, but aside from that,
813 * all of the file is printable ASCII.
815 #define PUTS(s) if (fputs(s, filep) == EOF || ferror(filep)) returnCode(code)
819 for (y = 0; y < (int) SIZEOF(scr_params); ++y) {
820 const char *name = scr_params[y].name;
821 const char *data = (char *) win + scr_params[y].offset;
822 const void *dp = (const void *) data;
826 if (!strncmp(name, "_pad.", 5) && !(win->_flags & _ISPAD)) {
829 switch (scr_params[y].type) {
831 attr = (*(const attr_t *) dp) & ~A_CHARTEXT;
832 encode_attr(buffer, TOP_SLIMIT
833 (*(const attr_t *) dp) & ~A_CHARTEXT,
835 COLOR_PAIR((int) attr),
839 if (!(*(const bool *) data)) {
842 _nc_STRCPY(buffer, name, sizeof(buffer));
846 attr = (*(const attr_t *) dp);
847 encode_attr(buffer, TOP_SLIMIT
848 * (const attr_t *) dp,
850 COLOR_PAIR((int) attr),
854 if (!(*(const int *) dp))
856 _nc_SPRINTF(buffer, TOP_SLIMIT
857 "%d", *(const int *) dp);
860 if (!(*(const short *) dp))
862 _nc_SPRINTF(buffer, TOP_SLIMIT
863 "%d", *(const short *) dp);
866 if (!(*(const NCURSES_SIZE_T *) dp))
868 _nc_SPRINTF(buffer, TOP_SLIMIT
869 "%d", *(const NCURSES_SIZE_T *) dp);
873 encode_cell(buffer, TOP_SLIMIT
874 (CARG_CH_T) dp, CHREF(last_cell));
879 * Only write non-default data.
881 if (*buffer != '\0') {
882 if (fprintf(filep, "%s=%s\n", name, buffer) <= 0
888 fprintf(filep, "rows:\n");
889 for (y = 0; y <= win->_maxy; y++) {
890 NCURSES_CH_T *data = win->_line[y].text;
892 if (fprintf(filep, "%d:", y + 1) <= 0
895 for (x = 0; x <= win->_maxx; x++) {
897 int len = wcwidth(data[x].chars[0]);
898 encode_cell(buffer, TOP_SLIMIT CHREF(data[x]), CHREF(last_cell));
904 encode_cell(buffer, TOP_SLIMIT CHREF(data[x]), CHREF(last_cell));
915 * This is the original putwin():
916 * A straight binary dump is simple, but its format can depend on whether
917 * ncurses is compiled with wide-character support, and also may depend
918 * on the version of ncurses, e.g., if the WINDOW structure is extended.
921 size_t len = (size_t) (win->_maxx + 1);
925 if (fwrite(win, sizeof(WINDOW), (size_t) 1, filep) != 1
929 for (y = 0; y <= win->_maxy; y++) {
930 if (fwrite(win->_line[y].text,
931 sizeof(NCURSES_CH_T), len, filep) != len
943 NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file)
948 T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
950 if (_nc_access(file, R_OK) >= 0
951 && (fp = fopen(file, BIN_R)) != 0) {
952 delwin(NewScreen(SP_PARM));
953 NewScreen(SP_PARM) = getwin(fp);
955 newscr = NewScreen(SP_PARM);
958 if (NewScreen(SP_PARM) != 0) {
967 scr_restore(const char *file)
969 return NCURSES_SP_NAME(scr_restore) (CURRENT_SCREEN, file);
974 scr_dump(const char *file)
979 T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
981 if (_nc_access(file, W_OK) < 0
982 || (fp = fopen(file, BIN_W)) == 0) {
985 (void) putwin(newscr, fp);
993 NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file)
997 T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
1000 #ifdef USE_TERM_DRIVER
1001 InfoOf(SP_PARM).caninit
1003 !(exit_ca_mode && non_rev_rmcup)
1008 if (_nc_access(file, R_OK) >= 0
1009 && (fp = fopen(file, BIN_R)) != 0) {
1010 delwin(CurScreen(SP_PARM));
1011 CurScreen(SP_PARM) = getwin(fp);
1013 curscr = CurScreen(SP_PARM);
1016 if (CurScreen(SP_PARM) != 0) {
1024 #if NCURSES_SP_FUNCS
1026 scr_init(const char *file)
1028 return NCURSES_SP_NAME(scr_init) (CURRENT_SCREEN, file);
1033 NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file)
1037 T((T_CALLED("scr_set(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
1039 if (NCURSES_SP_NAME(scr_init) (NCURSES_SP_ARGx file) == OK) {
1040 delwin(NewScreen(SP_PARM));
1041 NewScreen(SP_PARM) = dupwin(curscr);
1043 newscr = NewScreen(SP_PARM);
1045 if (NewScreen(SP_PARM) != 0) {
1052 #if NCURSES_SP_FUNCS
1054 scr_set(const char *file)
1056 return NCURSES_SP_NAME(scr_set) (CURRENT_SCREEN, file);