/****************************************************************************
- * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc. *
+ * Copyright 2019-2020,2021 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 *
#define CUR SP_TERMTYPE
#endif
-MODULE_ID("$Id: lib_screen.c,v 1.93 2018/01/14 17:39:47 tom Exp $")
+MODULE_ID("$Id: lib_screen.c,v 1.104 2021/10/23 17:12:16 tom Exp $")
#define MAX_SIZE 0x3fff /* 16k is big enough for a window or pad */
#define ARG_SLIMIT(name) /* nothing */
#endif
-#define CUR_SLIMIT _nc_SLIMIT(limit - (target - base))
+#define CUR_SLIMIT _nc_SLIMIT(limit - (size_t) (target - base))
#define TOP_SLIMIT _nc_SLIMIT(sizeof(buffer))
/*
* 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 {
if (ch == '\n') {
result[--used] = '\0';
- T(("READ:%s", result));
+ TR(TRACE_IEVENT, ("READ:%s", result));
} else if (used == 0) {
free(result);
result = 0;
{
bool found = FALSE;
- T(("decode_attr '%s'", source));
+ TR(TRACE_IEVENT, ("decode_attr '%s'", source));
while (*source) {
if (source[0] == MARKER && source[1] == L_CURL) {
int base = 16;
const char digits[] = "0123456789abcdef";
- T(("decode_char '%s'", source));
+ TR(TRACE_IEVENT, ("decode_char '%s'", source));
*target = ' ';
switch (*source) {
case MARKER:
int color = PAIR_NUMBER((int) attr);
int value;
- T(("decode_chtype '%s'", source));
+ TR(TRACE_IEVENT, ("decode_chtype '%s'", source));
source = decode_attr(source, &attr, &color);
source = decode_char(source, &value);
*target = (ChCharOf(value) | attr | (chtype) COLOR_PAIR(color));
int append = 0;
int value = 0;
- T(("decode_cchar '%s'", source));
+ TR(TRACE_IEVENT, ("decode_cchar '%s'", source));
*target = blank;
#if NCURSES_EXT_COLORS
color = fillin->ext_color;
}
static int
-read_row(char *source, NCURSES_CH_T * prior, NCURSES_CH_T * target, int length)
+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]);
+ len = _nc_wacs_width(target->chars[0]);
if (len > 1) {
int n;
* 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) {
+ 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, 4)) {
+ if (!memcmp(&tmp, my_magic, (size_t) 4)) {
#if NCURSES_EXT_PUTWIN
if (read_win(&tmp, filep) < 0)
#endif
returnWin(0);
}
- if (tmp._flags & _ISPAD) {
+ if (IS_PAD(&tmp)) {
nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx
tmp._maxy + 1,
tmp._maxx + 1);
nwin->_regtop = tmp._regtop;
nwin->_regbottom = tmp._regbottom;
- if (tmp._flags & _ISPAD)
+ if (IS_PAD(&tmp))
nwin->_pad = tmp._pad;
if (old_format) {
attr_t attr;
*buffer = '\0';
- if (!strncmp(name, "_pad.", 5) && !(win->_flags & _ISPAD)) {
+ if (!strncmp(name, "_pad.", (size_t) 5) && !IS_PAD(win)) {
continue;
}
switch (scr_params[y].type) {
returnCode(code);
for (x = 0; x <= win->_maxx; x++) {
#if NCURSES_WIDECHAR
- int len = wcwidth(data[x].chars[0]);
+ 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);
returnCode(code);
}
+/*
+ * Replace a window covering the whole screen, i.e., newscr or curscr.
+ */
+static WINDOW *
+replace_window(WINDOW *target, FILE *source)
+{
+ WINDOW *result = getwin(source);
+#if NCURSES_EXT_FUNCS
+ if (result != NULL) {
+ if (getmaxx(result) != getmaxx(target)
+ || getmaxy(result) != getmaxy(target)) {
+ int code = wresize(result,
+ 1 + getmaxy(target),
+ 1 + getmaxx(target));
+ if (code != OK) {
+ delwin(result);
+ result = NULL;
+ }
+ }
+ }
+#endif
+ delwin(target);
+ return result;
+}
+
NCURSES_EXPORT(int)
NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file)
{
T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
if (_nc_access(file, R_OK) >= 0
- && (fp = fopen(file, "rb")) != 0) {
- delwin(NewScreen(SP_PARM));
- NewScreen(SP_PARM) = getwin(fp);
+ && (fp = safe_fopen(file, BIN_R)) != 0) {
+ NewScreen(SP_PARM) = replace_window(NewScreen(SP_PARM), fp);
#if !USE_REENTRANT
newscr = NewScreen(SP_PARM);
#endif
T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
if (_nc_access(file, W_OK) < 0
- || (fp = fopen(file, "wb")) == 0) {
+ || (fp = safe_fopen(file, BIN_W)) == 0) {
result = ERR;
} else {
(void) putwin(newscr, fp);
FILE *fp = 0;
if (_nc_access(file, R_OK) >= 0
- && (fp = fopen(file, "rb")) != 0) {
- delwin(CurScreen(SP_PARM));
- CurScreen(SP_PARM) = getwin(fp);
+ && (fp = safe_fopen(file, BIN_R)) != 0) {
+ CurScreen(SP_PARM) = replace_window(CurScreen(SP_PARM), fp);
#if !USE_REENTRANT
curscr = CurScreen(SP_PARM);
#endif