1 /****************************************************************************
2 * Copyright (c) 1998-2009,2011 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>
39 #define CUR SP_TERMTYPE
42 MODULE_ID("$Id: lib_screen.c,v 1.41 2011/10/22 15:03:11 tom Exp $")
44 #define MAX_SIZE 0x3fff /* 16k is big enough for a window or pad */
46 NCURSES_EXPORT(WINDOW *)
47 NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
52 T((T_CALLED("getwin(%p)"), (void *) filep));
58 if (fread(&tmp, (size_t) 1, sizeof(WINDOW), filep) < sizeof(WINDOW)
61 || tmp._maxy > MAX_SIZE
63 || tmp._maxx > MAX_SIZE) {
67 if (tmp._flags & _ISPAD) {
68 nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx
72 nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
78 * We deliberately do not restore the _parx, _pary, or _parent
79 * fields, because the window hierarchy within which they
80 * made sense is probably gone.
83 size_t linesize = sizeof(NCURSES_CH_T) * (size_t) (tmp._maxx + 1);
85 nwin->_curx = tmp._curx;
86 nwin->_cury = tmp._cury;
87 nwin->_maxy = tmp._maxy;
88 nwin->_maxx = tmp._maxx;
89 nwin->_begy = tmp._begy;
90 nwin->_begx = tmp._begx;
91 nwin->_yoffset = tmp._yoffset;
92 nwin->_flags = tmp._flags & ~(_SUBWIN);
94 WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp);
95 nwin->_nc_bkgd = tmp._nc_bkgd;
97 nwin->_notimeout = tmp._notimeout;
98 nwin->_clear = tmp._clear;
99 nwin->_leaveok = tmp._leaveok;
100 nwin->_idlok = tmp._idlok;
101 nwin->_idcok = tmp._idcok;
102 nwin->_immed = tmp._immed;
103 nwin->_scroll = tmp._scroll;
104 nwin->_sync = tmp._sync;
105 nwin->_use_keypad = tmp._use_keypad;
106 nwin->_delay = tmp._delay;
108 nwin->_regtop = tmp._regtop;
109 nwin->_regbottom = tmp._regbottom;
111 if (tmp._flags & _ISPAD)
112 nwin->_pad = tmp._pad;
114 for (n = 0; n <= nwin->_maxy; n++) {
116 if (fread(nwin->_line[n].text, (size_t) 1, linesize, filep) < linesize
128 NCURSES_EXPORT(WINDOW *)
131 return NCURSES_SP_NAME(getwin) (CURRENT_SCREEN, filep);
136 putwin(WINDOW *win, FILE *filep)
141 T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep));
144 size_t len = (size_t) (win->_maxx + 1);
147 if (fwrite(win, sizeof(WINDOW), (size_t) 1, filep) != 1
151 for (n = 0; n <= win->_maxy; n++) {
152 if (fwrite(win->_line[n].text,
153 sizeof(NCURSES_CH_T), len, filep) != len
164 NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file)
168 T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
170 if (_nc_access(file, R_OK) < 0
171 || (fp = fopen(file, "rb")) == 0) {
174 delwin(NewScreen(SP_PARM));
175 NewScreen(SP_PARM) = getwin(fp);
177 newscr = NewScreen(SP_PARM);
186 scr_restore(const char *file)
188 return NCURSES_SP_NAME(scr_restore) (CURRENT_SCREEN, file);
193 scr_dump(const char *file)
198 T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
200 if (_nc_access(file, W_OK) < 0
201 || (fp = fopen(file, "wb")) == 0) {
204 (void) putwin(newscr, fp);
212 NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file)
217 T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
220 #ifdef USE_TERM_DRIVER
221 InfoOf(SP_PARM).caninit
223 !(exit_ca_mode && non_rev_rmcup)
226 if (_nc_access(file, R_OK) >= 0
227 && (fp = fopen(file, "rb")) != 0) {
228 delwin(CurScreen(SP_PARM));
229 CurScreen(SP_PARM) = getwin(fp);
231 curscr = CurScreen(SP_PARM);
242 scr_init(const char *file)
244 return NCURSES_SP_NAME(scr_init) (CURRENT_SCREEN, file);
249 NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file)
251 T((T_CALLED("scr_set(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
253 if (NCURSES_SP_NAME(scr_init) (NCURSES_SP_ARGx file) == ERR) {
256 delwin(NewScreen(SP_PARM));
257 NewScreen(SP_PARM) = dupwin(curscr);
259 newscr = NewScreen(SP_PARM);
267 scr_set(const char *file)
269 return NCURSES_SP_NAME(scr_set) (CURRENT_SCREEN, file);