1/****************************************************************************
2 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
3 *                                                                          *
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:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
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.                               *
22 *                                                                          *
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       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
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 ****************************************************************************/
34
35#include <curses.priv.h>
36
37MODULE_ID("$Id: lib_screen.c,v 1.31 2008/08/16 19:05:37 tom Exp $")
38
39#define MAX_SIZE 0x3fff		/* 16k is big enough for a window or pad */
40
41NCURSES_EXPORT(WINDOW *)
42getwin(FILE *filep)
43{
44    WINDOW tmp, *nwin;
45    int n;
46
47    T((T_CALLED("getwin(%p)"), filep));
48
49    clearerr(filep);
50    (void) fread(&tmp, sizeof(WINDOW), 1, filep);
51    if (ferror(filep)
52	|| tmp._maxy == 0
53	|| tmp._maxy > MAX_SIZE
54	|| tmp._maxx == 0
55	|| tmp._maxx > MAX_SIZE)
56	returnWin(0);
57
58    if (tmp._flags & _ISPAD) {
59	nwin = newpad(tmp._maxy + 1, tmp._maxx + 1);
60    } else {
61	nwin = newwin(tmp._maxy + 1, tmp._maxx + 1, 0, 0);
62    }
63
64    /*
65     * We deliberately do not restore the _parx, _pary, or _parent
66     * fields, because the window hierarchy within which they
67     * made sense is probably gone.
68     */
69    if (nwin != 0) {
70	nwin->_curx = tmp._curx;
71	nwin->_cury = tmp._cury;
72	nwin->_maxy = tmp._maxy;
73	nwin->_maxx = tmp._maxx;
74	nwin->_begy = tmp._begy;
75	nwin->_begx = tmp._begx;
76	nwin->_yoffset = tmp._yoffset;
77	nwin->_flags = tmp._flags & ~(_SUBWIN);
78
79	WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp);
80	nwin->_nc_bkgd = tmp._nc_bkgd;
81
82	nwin->_notimeout = tmp._notimeout;
83	nwin->_clear = tmp._clear;
84	nwin->_leaveok = tmp._leaveok;
85	nwin->_idlok = tmp._idlok;
86	nwin->_idcok = tmp._idcok;
87	nwin->_immed = tmp._immed;
88	nwin->_scroll = tmp._scroll;
89	nwin->_sync = tmp._sync;
90	nwin->_use_keypad = tmp._use_keypad;
91	nwin->_delay = tmp._delay;
92
93	nwin->_regtop = tmp._regtop;
94	nwin->_regbottom = tmp._regbottom;
95
96	if (tmp._flags & _ISPAD)
97	    nwin->_pad = tmp._pad;
98
99	for (n = 0; n <= nwin->_maxy; n++) {
100	    clearerr(filep);
101	    (void) fread(nwin->_line[n].text,
102			 sizeof(NCURSES_CH_T),
103			 (size_t) (nwin->_maxx + 1),
104			 filep);
105	    if (ferror(filep)) {
106		delwin(nwin);
107		returnWin(0);
108	    }
109	}
110	touchwin(nwin);
111    }
112    returnWin(nwin);
113}
114
115NCURSES_EXPORT(int)
116putwin(WINDOW *win, FILE *filep)
117{
118    int code = ERR;
119    int n;
120
121    T((T_CALLED("putwin(%p,%p)"), win, filep));
122
123    if (win != 0) {
124	size_t len = (size_t) (win->_maxx + 1);
125
126	clearerr(filep);
127	if (fwrite(win, sizeof(WINDOW), 1, filep) != 1
128	    || ferror(filep))
129	      returnCode(code);
130
131	for (n = 0; n <= win->_maxy; n++) {
132	    if (fwrite(win->_line[n].text,
133		       sizeof(NCURSES_CH_T), len, filep) != len
134		|| ferror(filep)) {
135		returnCode(code);
136	    }
137	}
138	code = OK;
139    }
140    returnCode(code);
141}
142
143NCURSES_EXPORT(int)
144scr_restore(const char *file)
145{
146    FILE *fp = 0;
147
148    T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file)));
149
150    if (_nc_access(file, R_OK) < 0
151	|| (fp = fopen(file, "rb")) == 0) {
152	returnCode(ERR);
153    } else {
154	delwin(newscr);
155	SP->_newscr = getwin(fp);
156#if !USE_REENTRANT
157	newscr = SP->_newscr;
158#endif
159	(void) fclose(fp);
160	returnCode(OK);
161    }
162}
163
164NCURSES_EXPORT(int)
165scr_dump(const char *file)
166{
167    FILE *fp = 0;
168
169    T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
170
171    if (_nc_access(file, W_OK) < 0
172	|| (fp = fopen(file, "wb")) == 0) {
173	returnCode(ERR);
174    } else {
175	(void) putwin(newscr, fp);
176	(void) fclose(fp);
177	returnCode(OK);
178    }
179}
180
181NCURSES_EXPORT(int)
182scr_init(const char *file)
183{
184    FILE *fp = 0;
185
186    T((T_CALLED("scr_init(%s)"), _nc_visbuf(file)));
187
188    if (exit_ca_mode && non_rev_rmcup)
189	returnCode(ERR);
190
191    if (_nc_access(file, R_OK) < 0
192	|| (fp = fopen(file, "rb")) == 0) {
193	returnCode(ERR);
194    } else {
195	delwin(curscr);
196	SP->_curscr = getwin(fp);
197#if !USE_REENTRANT
198	curscr = SP->_curscr;
199#endif
200	(void) fclose(fp);
201	returnCode(OK);
202    }
203}
204
205NCURSES_EXPORT(int)
206scr_set(const char *file)
207{
208    T((T_CALLED("scr_set(%s)"), _nc_visbuf(file)));
209
210    if (scr_init(file) == ERR) {
211	returnCode(ERR);
212    } else {
213	delwin(newscr);
214	SP->_newscr = dupwin(curscr);
215#if !USE_REENTRANT
216	newscr = SP->_newscr;
217#endif
218	returnCode(OK);
219    }
220}
221