1 /****************************************************************************
2 * Copyright (c) 1998-2015,2016 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.85 2016/09/10 18:38:36 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 0x8888 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, NULL);
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)
660 #if USE_STRING_HACKS && HAVE_SNPRINTF
663 source &= ~A_CHARTEXT;
664 prior &= ~A_CHARTEXT;
667 if (source != prior) {
674 for (n = 0; n < SIZEOF(scr_attrs); ++n) {
675 if ((source & scr_attrs[n].attr) != 0 ||
676 ((source & ALL_BUT_COLOR) == 0 &&
677 (scr_attrs[n].attr == A_NORMAL))) {
683 _nc_STRCPY(target, scr_attrs[n].name, limit);
684 target += strlen(target);
687 if ((source & A_COLOR) != (prior & A_COLOR)) {
690 _nc_SPRINTF(target, CUR_SLIMIT "C%d", PAIR_NUMBER((int) source));
691 target += strlen(target);
700 encode_cell(char *target, ARG_SLIMIT(limit) CARG_CH_T source, CARG_CH_T previous)
702 #if USE_STRING_HACKS && HAVE_SNPRINTF
709 if (previous->attr != source->attr) {
710 encode_attr(target, CUR_SLIMIT source->attr, previous->attr);
712 target += strlen(target);
713 #if NCURSES_EXT_COLORS
714 if (previous->ext_color != source->ext_color) {
715 _nc_SPRINTF(target, CUR_SLIMIT
716 "%c%cC%d%c", MARKER, L_CURL, source->ext_color, R_CURL);
719 for (n = 0; n < SIZEOF(source->chars); ++n) {
720 unsigned uch = (unsigned) source->chars[n];
729 _nc_SPRINTF(target, CUR_SLIMIT "U%08x", uch);
730 } else if (uch > 0xff) {
731 _nc_SPRINTF(target, CUR_SLIMIT "u%04x", uch);
732 } else if (uch < 32 || uch >= 127) {
733 _nc_SPRINTF(target, CUR_SLIMIT "%03o", uch & 0xff);
737 _nc_STRCPY(target, "s", limit);
745 _nc_SPRINTF(target, CUR_SLIMIT "%c", uch);
749 target += strlen(target);
752 chtype ch = CharOfD(source);
755 if (AttrOfD(previous) != AttrOfD(source)) {
756 encode_attr(target, CUR_SLIMIT AttrOfD(source), AttrOfD(previous));
758 target += strlen(target);
760 if (ch < 32 || ch >= 127) {
761 _nc_SPRINTF(target, CUR_SLIMIT "%03o", UChar(ch));
765 _nc_STRCPY(target, "s", limit);
773 _nc_SPRINTF(target, CUR_SLIMIT "%c", UChar(ch));
782 putwin(WINDOW *win, FILE *filep)
786 T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep));
788 #if NCURSES_EXT_PUTWIN
790 const char *version = curses_version();
792 NCURSES_CH_T last_cell;
795 memset(&last_cell, 0, sizeof(last_cell));
800 * Our magic number is technically nonprinting, but aside from that,
801 * all of the file is printable ASCII.
803 #define PUTS(s) if (fputs(s, filep) == EOF || ferror(filep)) returnCode(code)
807 for (y = 0; y < (int) SIZEOF(scr_params); ++y) {
808 const char *name = scr_params[y].name;
809 const char *data = (char *) win + scr_params[y].offset;
810 const void *dp = (const void *) data;
813 if (!strncmp(name, "_pad.", 5) && !(win->_flags & _ISPAD)) {
816 switch (scr_params[y].type) {
818 encode_attr(buffer, TOP_SLIMIT
819 (*(const attr_t *) dp) & ~A_CHARTEXT, A_NORMAL);
822 if (!(*(const bool *) data)) {
825 _nc_STRCPY(buffer, name, sizeof(buffer));
829 encode_attr(buffer, TOP_SLIMIT
830 * (const attr_t *) dp, A_NORMAL);
833 if (!(*(const int *) dp))
835 _nc_SPRINTF(buffer, TOP_SLIMIT
836 "%d", *(const int *) dp);
839 if (!(*(const short *) dp))
841 _nc_SPRINTF(buffer, TOP_SLIMIT
842 "%d", *(const short *) dp);
845 if (!(*(const NCURSES_SIZE_T *) dp))
847 _nc_SPRINTF(buffer, TOP_SLIMIT
848 "%d", *(const NCURSES_SIZE_T *) dp);
852 encode_cell(buffer, TOP_SLIMIT
853 (CARG_CH_T) dp, CHREF(last_cell));
858 * Only write non-default data.
860 if (*buffer != '\0') {
861 if (fprintf(filep, "%s=%s\n", name, buffer) <= 0
867 fprintf(filep, "rows:\n");
868 for (y = 0; y <= win->_maxy; y++) {
869 NCURSES_CH_T *data = win->_line[y].text;
871 if (fprintf(filep, "%d:", y + 1) <= 0
874 for (x = 0; x <= win->_maxx; x++) {
876 int len = wcwidth(data[x].chars[0]);
877 encode_cell(buffer, TOP_SLIMIT CHREF(data[x]), CHREF(last_cell));
883 encode_cell(buffer, TOP_SLIMIT CHREF(data[x]), CHREF(last_cell));
894 * This is the original putwin():
895 * A straight binary dump is simple, but its format can depend on whether
896 * ncurses is compiled with wide-character support, and also may depend
897 * on the version of ncurses, e.g., if the WINDOW structure is extended.
900 size_t len = (size_t) (win->_maxx + 1);
904 if (fwrite(win, sizeof(WINDOW), (size_t) 1, filep) != 1
908 for (y = 0; y <= win->_maxy; y++) {
909 if (fwrite(win->_line[y].text,
910 sizeof(NCURSES_CH_T), len, filep) != len
922 NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file)
927 T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
929 if (_nc_access(file, R_OK) >= 0
930 && (fp = fopen(file, "rb")) != 0) {
931 delwin(NewScreen(SP_PARM));
932 NewScreen(SP_PARM) = getwin(fp);
934 newscr = NewScreen(SP_PARM);
937 if (NewScreen(SP_PARM) != 0) {
946 scr_restore(const char *file)
948 return NCURSES_SP_NAME(scr_restore) (CURRENT_SCREEN, file);
953 scr_dump(const char *file)
958 T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
960 if (_nc_access(file, W_OK) < 0
961 || (fp = fopen(file, "wb")) == 0) {
964 (void) putwin(newscr, fp);
972 NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file)
976 T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
979 #ifdef USE_TERM_DRIVER
980 InfoOf(SP_PARM).caninit
982 !(exit_ca_mode && non_rev_rmcup)
987 if (_nc_access(file, R_OK) >= 0
988 && (fp = fopen(file, "rb")) != 0) {
989 delwin(CurScreen(SP_PARM));
990 CurScreen(SP_PARM) = getwin(fp);
992 curscr = CurScreen(SP_PARM);
995 if (CurScreen(SP_PARM) != 0) {
1003 #if NCURSES_SP_FUNCS
1005 scr_init(const char *file)
1007 return NCURSES_SP_NAME(scr_init) (CURRENT_SCREEN, file);
1012 NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file)
1016 T((T_CALLED("scr_set(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
1018 if (NCURSES_SP_NAME(scr_init) (NCURSES_SP_ARGx file) == OK) {
1019 delwin(NewScreen(SP_PARM));
1020 NewScreen(SP_PARM) = dupwin(curscr);
1022 newscr = NewScreen(SP_PARM);
1024 if (NewScreen(SP_PARM) != 0) {
1031 #if NCURSES_SP_FUNCS
1033 scr_set(const char *file)
1035 return NCURSES_SP_NAME(scr_set) (CURRENT_SCREEN, file);