+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, NULL);
+ 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 = wcwidth(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, 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, 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;