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