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