]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_screen.c
745a366731eb60143857fa8a24f214b3b67e09c8
[ncurses.git] / ncurses / base / lib_screen.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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 MODULE_ID("$Id: lib_screen.c,v 1.32 2009/02/15 00:39:13 tom Exp $")
39
40 #define MAX_SIZE 0x3fff         /* 16k is big enough for a window or pad */
41
42 NCURSES_EXPORT(WINDOW *)
43 getwin(FILE *filep)
44 {
45     WINDOW tmp, *nwin;
46     int n;
47
48     T((T_CALLED("getwin(%p)"), filep));
49
50     clearerr(filep);
51     (void) fread(&tmp, sizeof(WINDOW), 1, filep);
52     if (ferror(filep)
53         || tmp._maxy == 0
54         || tmp._maxy > MAX_SIZE
55         || tmp._maxx == 0
56         || tmp._maxx > MAX_SIZE)
57         returnWin(0);
58
59     if (tmp._flags & _ISPAD) {
60         nwin = newpad(tmp._maxy + 1, tmp._maxx + 1);
61     } else {
62         nwin = newwin(tmp._maxy + 1, tmp._maxx + 1, 0, 0);
63     }
64
65     /*
66      * We deliberately do not restore the _parx, _pary, or _parent
67      * fields, because the window hierarchy within which they
68      * made sense is probably gone.
69      */
70     if (nwin != 0) {
71         nwin->_curx = tmp._curx;
72         nwin->_cury = tmp._cury;
73         nwin->_maxy = tmp._maxy;
74         nwin->_maxx = tmp._maxx;
75         nwin->_begy = tmp._begy;
76         nwin->_begx = tmp._begx;
77         nwin->_yoffset = tmp._yoffset;
78         nwin->_flags = tmp._flags & ~(_SUBWIN);
79
80         WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp);
81         nwin->_nc_bkgd = tmp._nc_bkgd;
82
83         nwin->_notimeout = tmp._notimeout;
84         nwin->_clear = tmp._clear;
85         nwin->_leaveok = tmp._leaveok;
86         nwin->_idlok = tmp._idlok;
87         nwin->_idcok = tmp._idcok;
88         nwin->_immed = tmp._immed;
89         nwin->_scroll = tmp._scroll;
90         nwin->_sync = tmp._sync;
91         nwin->_use_keypad = tmp._use_keypad;
92         nwin->_delay = tmp._delay;
93
94         nwin->_regtop = tmp._regtop;
95         nwin->_regbottom = tmp._regbottom;
96
97         if (tmp._flags & _ISPAD)
98             nwin->_pad = tmp._pad;
99
100         for (n = 0; n <= nwin->_maxy; n++) {
101             clearerr(filep);
102             (void) fread(nwin->_line[n].text,
103                          sizeof(NCURSES_CH_T),
104                          (size_t) (nwin->_maxx + 1),
105                          filep);
106             if (ferror(filep)) {
107                 delwin(nwin);
108                 returnWin(0);
109             }
110         }
111         touchwin(nwin);
112     }
113     returnWin(nwin);
114 }
115
116 NCURSES_EXPORT(int)
117 putwin(WINDOW *win, FILE *filep)
118 {
119     int code = ERR;
120     int n;
121
122     T((T_CALLED("putwin(%p,%p)"), win, filep));
123
124     if (win != 0) {
125         size_t len = (size_t) (win->_maxx + 1);
126
127         clearerr(filep);
128         if (fwrite(win, sizeof(WINDOW), 1, filep) != 1
129             || ferror(filep))
130               returnCode(code);
131
132         for (n = 0; n <= win->_maxy; n++) {
133             if (fwrite(win->_line[n].text,
134                        sizeof(NCURSES_CH_T), len, filep) != len
135                 || ferror(filep)) {
136                 returnCode(code);
137             }
138         }
139         code = OK;
140     }
141     returnCode(code);
142 }
143
144 NCURSES_EXPORT(int)
145 NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file)
146 {
147     FILE *fp = 0;
148
149     T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file)));
150
151     if (_nc_access(file, R_OK) < 0
152         || (fp = fopen(file, "rb")) == 0) {
153         returnCode(ERR);
154     } else {
155         delwin(newscr);
156         SP_PARM->_newscr = getwin(fp);
157 #if !USE_REENTRANT
158         newscr = SP_PARM->_newscr;
159 #endif
160         (void) fclose(fp);
161         returnCode(OK);
162     }
163 }
164
165 #if NCURSES_SP_FUNCS
166 NCURSES_EXPORT(int)
167 scr_restore(const char *file)
168 {
169     return NCURSES_SP_NAME(scr_restore) (CURRENT_SCREEN, file);
170 }
171 #endif
172
173 NCURSES_EXPORT(int)
174 scr_dump(const char *file)
175 {
176     FILE *fp = 0;
177
178     T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
179
180     if (_nc_access(file, W_OK) < 0
181         || (fp = fopen(file, "wb")) == 0) {
182         returnCode(ERR);
183     } else {
184         (void) putwin(newscr, fp);
185         (void) fclose(fp);
186         returnCode(OK);
187     }
188 }
189
190 NCURSES_EXPORT(int)
191 NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file)
192 {
193     FILE *fp = 0;
194
195     T((T_CALLED("scr_init(%s)"), _nc_visbuf(file)));
196
197     if (exit_ca_mode && non_rev_rmcup)
198         returnCode(ERR);
199
200     if (_nc_access(file, R_OK) < 0
201         || (fp = fopen(file, "rb")) == 0) {
202         returnCode(ERR);
203     } else {
204         delwin(curscr);
205         SP_PARM->_curscr = getwin(fp);
206 #if !USE_REENTRANT
207         curscr = SP_PARM->_curscr;
208 #endif
209         (void) fclose(fp);
210         returnCode(OK);
211     }
212 }
213
214 #if NCURSES_SP_FUNCS
215 NCURSES_EXPORT(int)
216 scr_init(const char *file)
217 {
218     return NCURSES_SP_NAME(scr_init) (CURRENT_SCREEN, file);
219 }
220 #endif
221
222 NCURSES_EXPORT(int)
223 NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file)
224 {
225     T((T_CALLED("scr_set(%s)"), _nc_visbuf(file)));
226
227     if (scr_init(file) == ERR) {
228         returnCode(ERR);
229     } else {
230         delwin(newscr);
231         SP_PARM->_newscr = dupwin(curscr);
232 #if !USE_REENTRANT
233         newscr = SP_PARM->_newscr;
234 #endif
235         returnCode(OK);
236     }
237 }
238
239 #if NCURSES_SP_FUNCS
240 NCURSES_EXPORT(int)
241 scr_set(const char *file)
242 {
243     return NCURSES_SP_NAME(scr_set) (CURRENT_SCREEN, file);
244 }
245 #endif