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