ncurses 5.0
[ncurses.git] / ncurses / base / lib_screen.c
1 /****************************************************************************
2  * Copyright (c) 1998 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  ****************************************************************************/
33
34
35 #include <curses.priv.h>
36
37 #include <sys/stat.h>
38 #include <time.h>
39 #include <term.h>       /* exit_ca_mode, non_rev_rmcup */
40
41 MODULE_ID("$Id: lib_screen.c,v 1.15 1999/07/24 20:05:29 tom Exp $")
42
43 static time_t   dumptime;
44
45 WINDOW *getwin(FILE *filep)
46 {
47         WINDOW  tmp, *nwin;
48         int     n;
49
50         T((T_CALLED("getwin(%p)"), filep));
51
52         (void) fread(&tmp, sizeof(WINDOW), 1, filep);
53         if (ferror(filep))
54                 returnWin(0);
55
56         if ((nwin = newwin(tmp._maxy+1, tmp._maxx+1, 0, 0)) == 0)
57                 returnWin(0);
58
59         /*
60          * We deliberately do not restore the _parx, _pary, or _parent
61          * fields, because the window hierarchy within which they
62          * made sense is probably gone.
63          */
64         nwin->_curx       = tmp._curx;
65         nwin->_cury       = tmp._cury;
66         nwin->_maxy       = tmp._maxy;
67         nwin->_maxx       = tmp._maxx;
68         nwin->_begy       = tmp._begy;
69         nwin->_begx       = tmp._begx;
70         nwin->_yoffset    = tmp._yoffset;
71         nwin->_flags      = tmp._flags & ~(_SUBWIN|_ISPAD);
72
73         nwin->_attrs      = tmp._attrs;
74         nwin->_bkgd       = tmp._bkgd;
75
76         nwin->_clear      = tmp._clear;
77         nwin->_scroll     = tmp._scroll;
78         nwin->_leaveok    = tmp._leaveok;
79         nwin->_use_keypad = tmp._use_keypad;
80         nwin->_delay      = tmp._delay;
81         nwin->_immed      = tmp._immed;
82         nwin->_sync       = tmp._sync;
83
84         nwin->_regtop     = tmp._regtop;
85         nwin->_regbottom  = tmp._regbottom;
86
87         for (n = 0; n < nwin->_maxy + 1; n++)
88         {
89                 (void) fread(nwin->_line[n].text,
90                               sizeof(chtype), (size_t)(nwin->_maxx + 1), filep);
91                 if (ferror(filep))
92                 {
93                         delwin(nwin);
94                         returnWin(0);
95                 }
96         }
97         touchwin(nwin);
98
99         returnWin(nwin);
100 }
101
102 int putwin(WINDOW *win, FILE *filep)
103 {
104         int code = ERR; 
105         int n;
106
107         T((T_CALLED("putwin(%p,%p)"), win, filep));
108
109         if (win) {
110           (void) fwrite(win, sizeof(WINDOW), 1, filep);
111           if (ferror(filep))
112             returnCode(code);
113
114           for (n = 0; n < win->_maxy + 1; n++)
115             {
116               (void) fwrite(win->_line[n].text,
117                             sizeof(chtype), (size_t)(win->_maxx + 1), filep);
118               if (ferror(filep))
119                 returnCode(code);
120             }
121           code = OK;
122         }
123         returnCode(code);
124 }
125
126 int scr_restore(const char *file)
127 {
128         FILE    *fp = 0;
129
130         T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file)));
131
132         if (_nc_access(file, R_OK) < 0
133          || (fp = fopen(file, "rb")) == 0)
134             returnCode(ERR);
135         else
136         {
137             delwin(newscr);
138             newscr = getwin(fp);
139             (void) fclose(fp);
140             returnCode(OK);
141         }
142 }
143
144 int scr_dump(const char *file)
145 {
146         FILE    *fp = 0;
147
148         T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
149
150         if (_nc_access(file, W_OK) < 0
151          || (fp = fopen(file, "wb")) == 0)
152             returnCode(ERR);
153         else
154         {
155             (void) putwin(newscr, fp);
156             (void) fclose(fp);
157             dumptime = time((time_t *)0);
158             returnCode(OK);
159         }
160 }
161
162 int scr_init(const char *file)
163 {
164         FILE    *fp = 0;
165         struct stat     stb;
166
167         T((T_CALLED("scr_init(%s)"), _nc_visbuf(file)));
168
169         if (exit_ca_mode && non_rev_rmcup)
170             returnCode(ERR);
171
172         if (_nc_access(file, R_OK) < 0
173          || (fp = fopen(file, "rb")) == 0)
174             returnCode(ERR);
175         else if (fstat(STDOUT_FILENO, &stb) || stb.st_mtime > dumptime)
176             returnCode(ERR);
177         else
178         {
179             delwin(curscr);
180             curscr = getwin(fp);
181             (void) fclose(fp);
182             returnCode(OK);
183         }
184 }
185
186 int scr_set(const char *file)
187 {
188     T((T_CALLED("scr_set(%s)"), _nc_visbuf(file)));
189
190     if (scr_init(file) == ERR)
191         returnCode(ERR);
192     else
193     {
194         delwin(newscr);
195         newscr = dupwin(curscr);
196         returnCode(OK);
197     }
198 }