lib_screen.c revision 302408
1/****************************************************************************
2 * Copyright (c) 1998-2009,2011 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 *     and: Juergen Pfeifer                         2009                    *
34 ****************************************************************************/
35
36#include <curses.priv.h>
37
38#ifndef CUR
39#define CUR SP_TERMTYPE
40#endif
41
42MODULE_ID("$Id: lib_screen.c,v 1.41 2011/10/22 15:03:11 tom Exp $")
43
44#define MAX_SIZE 0x3fff		/* 16k is big enough for a window or pad */
45
46NCURSES_EXPORT(WINDOW *)
47NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
48{
49    WINDOW tmp, *nwin;
50    int n;
51
52    T((T_CALLED("getwin(%p)"), (void *) filep));
53
54    if (filep == 0) {
55	returnWin(0);
56    }
57    clearerr(filep);
58    if (fread(&tmp, (size_t) 1, sizeof(WINDOW), filep) < sizeof(WINDOW)
59	|| ferror(filep)
60	|| tmp._maxy == 0
61	|| tmp._maxy > MAX_SIZE
62	|| tmp._maxx == 0
63	|| tmp._maxx > MAX_SIZE) {
64	returnWin(0);
65    }
66
67    if (tmp._flags & _ISPAD) {
68	nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx
69					tmp._maxy + 1,
70					tmp._maxx + 1);
71    } else {
72	nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
73					tmp._maxy + 1,
74					tmp._maxx + 1, 0, 0);
75    }
76
77    /*
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.
81     */
82    if (nwin != 0) {
83	size_t linesize = sizeof(NCURSES_CH_T) * (size_t) (tmp._maxx + 1);
84
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);
93
94	WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp);
95	nwin->_nc_bkgd = tmp._nc_bkgd;
96
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;
107
108	nwin->_regtop = tmp._regtop;
109	nwin->_regbottom = tmp._regbottom;
110
111	if (tmp._flags & _ISPAD)
112	    nwin->_pad = tmp._pad;
113
114	for (n = 0; n <= nwin->_maxy; n++) {
115	    clearerr(filep);
116	    if (fread(nwin->_line[n].text, (size_t) 1, linesize, filep) < linesize
117		|| ferror(filep)) {
118		delwin(nwin);
119		returnWin(0);
120	    }
121	}
122	touchwin(nwin);
123    }
124    returnWin(nwin);
125}
126
127#if NCURSES_SP_FUNCS
128NCURSES_EXPORT(WINDOW *)
129getwin(FILE *filep)
130{
131    return NCURSES_SP_NAME(getwin) (CURRENT_SCREEN, filep);
132}
133#endif
134
135NCURSES_EXPORT(int)
136putwin(WINDOW *win, FILE *filep)
137{
138    int code = ERR;
139    int n;
140
141    T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep));
142
143    if (win != 0) {
144	size_t len = (size_t) (win->_maxx + 1);
145
146	clearerr(filep);
147	if (fwrite(win, sizeof(WINDOW), (size_t) 1, filep) != 1
148	    || ferror(filep))
149	      returnCode(code);
150
151	for (n = 0; n <= win->_maxy; n++) {
152	    if (fwrite(win->_line[n].text,
153		       sizeof(NCURSES_CH_T), len, filep) != len
154		|| ferror(filep)) {
155		returnCode(code);
156	    }
157	}
158	code = OK;
159    }
160    returnCode(code);
161}
162
163NCURSES_EXPORT(int)
164NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file)
165{
166    FILE *fp = 0;
167
168    T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
169
170    if (_nc_access(file, R_OK) < 0
171	|| (fp = fopen(file, "rb")) == 0) {
172	returnCode(ERR);
173    } else {
174	delwin(NewScreen(SP_PARM));
175	NewScreen(SP_PARM) = getwin(fp);
176#if !USE_REENTRANT
177	newscr = NewScreen(SP_PARM);
178#endif
179	(void) fclose(fp);
180	returnCode(OK);
181    }
182}
183
184#if NCURSES_SP_FUNCS
185NCURSES_EXPORT(int)
186scr_restore(const char *file)
187{
188    return NCURSES_SP_NAME(scr_restore) (CURRENT_SCREEN, file);
189}
190#endif
191
192NCURSES_EXPORT(int)
193scr_dump(const char *file)
194{
195    int result;
196    FILE *fp = 0;
197
198    T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
199
200    if (_nc_access(file, W_OK) < 0
201	|| (fp = fopen(file, "wb")) == 0) {
202	result = ERR;
203    } else {
204	(void) putwin(newscr, fp);
205	(void) fclose(fp);
206	result = OK;
207    }
208    returnCode(result);
209}
210
211NCURSES_EXPORT(int)
212NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file)
213{
214    FILE *fp = 0;
215    int code = ERR;
216
217    T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
218
219    if (SP_PARM != 0 &&
220#ifdef USE_TERM_DRIVER
221	InfoOf(SP_PARM).caninit
222#else
223	!(exit_ca_mode && non_rev_rmcup)
224#endif
225	) {
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);
230#if !USE_REENTRANT
231	    curscr = CurScreen(SP_PARM);
232#endif
233	    (void) fclose(fp);
234	    code = OK;
235	}
236    }
237    returnCode(code);
238}
239
240#if NCURSES_SP_FUNCS
241NCURSES_EXPORT(int)
242scr_init(const char *file)
243{
244    return NCURSES_SP_NAME(scr_init) (CURRENT_SCREEN, file);
245}
246#endif
247
248NCURSES_EXPORT(int)
249NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file)
250{
251    T((T_CALLED("scr_set(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
252
253    if (NCURSES_SP_NAME(scr_init) (NCURSES_SP_ARGx file) == ERR) {
254	returnCode(ERR);
255    } else {
256	delwin(NewScreen(SP_PARM));
257	NewScreen(SP_PARM) = dupwin(curscr);
258#if !USE_REENTRANT
259	newscr = NewScreen(SP_PARM);
260#endif
261	returnCode(OK);
262    }
263}
264
265#if NCURSES_SP_FUNCS
266NCURSES_EXPORT(int)
267scr_set(const char *file)
268{
269    return NCURSES_SP_NAME(scr_set) (CURRENT_SCREEN, file);
270}
271#endif
272