lib_screen.c revision 1.1
1/*	$OpenBSD: lib_screen.c,v 1.1 1999/01/18 19:09:58 millert Exp $	*/
2
3/****************************************************************************
4 * Copyright (c) 1998 Free Software Foundation, Inc.                        *
5 *                                                                          *
6 * Permission is hereby granted, free of charge, to any person obtaining a  *
7 * copy of this software and associated documentation files (the            *
8 * "Software"), to deal in the Software without restriction, including      *
9 * without limitation the rights to use, copy, modify, merge, publish,      *
10 * distribute, distribute with modifications, sublicense, and/or sell       *
11 * copies of the Software, and to permit persons to whom the Software is    *
12 * furnished to do so, subject to the following conditions:                 *
13 *                                                                          *
14 * The above copyright notice and this permission notice shall be included  *
15 * in all copies or substantial portions of the Software.                   *
16 *                                                                          *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24 *                                                                          *
25 * Except as contained in this notice, the name(s) of the above copyright   *
26 * holders shall not be used in advertising or otherwise to promote the     *
27 * sale, use or other dealings in this Software without prior written       *
28 * authorization.                                                           *
29 ****************************************************************************/
30
31/****************************************************************************
32 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34 ****************************************************************************/
35
36
37#include <curses.priv.h>
38
39#include <sys/stat.h>
40#include <time.h>
41#include <term.h>	/* exit_ca_mode, non_rev_rmcup */
42
43MODULE_ID("$From: lib_screen.c,v 1.14 1999/01/02 22:56:17 tom Exp $")
44
45static time_t	dumptime;
46
47WINDOW *getwin(FILE *filep)
48{
49	WINDOW	tmp, *nwin;
50	int	n;
51
52	T((T_CALLED("getwin(%p)"), filep));
53
54	(void) fread(&tmp, sizeof(WINDOW), 1, filep);
55	if (ferror(filep))
56		returnWin(0);
57
58	if ((nwin = newwin(tmp._maxy+1, tmp._maxx+1, 0, 0)) == 0)
59		returnWin(0);
60
61	/*
62	 * We deliberately do not restore the _parx, _pary, or _parent
63	 * fields, because the window hierarchy within which they
64	 * made sense is probably gone.
65	 */
66	nwin->_curx       = tmp._curx;
67	nwin->_cury       = tmp._cury;
68	nwin->_maxy       = tmp._maxy;
69	nwin->_maxx       = tmp._maxx;
70	nwin->_begy       = tmp._begy;
71	nwin->_begx       = tmp._begx;
72	nwin->_yoffset    = tmp._yoffset;
73	nwin->_flags      = tmp._flags & ~(_SUBWIN|_ISPAD);
74
75	nwin->_attrs      = tmp._attrs;
76	nwin->_bkgd       = tmp._bkgd;
77
78	nwin->_clear      = tmp._clear;
79	nwin->_scroll     = tmp._scroll;
80	nwin->_leaveok    = tmp._leaveok;
81	nwin->_use_keypad = tmp._use_keypad;
82	nwin->_delay      = tmp._delay;
83	nwin->_immed      = tmp._immed;
84	nwin->_sync       = tmp._sync;
85
86	nwin->_regtop     = tmp._regtop;
87	nwin->_regbottom  = tmp._regbottom;
88
89	for (n = 0; n < nwin->_maxy + 1; n++)
90	{
91		(void) fread(nwin->_line[n].text,
92			      sizeof(chtype), (size_t)(nwin->_maxx + 1), filep);
93		if (ferror(filep))
94		{
95			delwin(nwin);
96			returnWin(0);
97		}
98	}
99	touchwin(nwin);
100
101	returnWin(nwin);
102}
103
104int putwin(WINDOW *win, FILE *filep)
105{
106        int code = ERR;
107	int n;
108
109	T((T_CALLED("putwin(%p,%p)"), win, filep));
110
111	if (win) {
112	  (void) fwrite(win, sizeof(WINDOW), 1, filep);
113	  if (ferror(filep))
114	    returnCode(code);
115
116	  for (n = 0; n < win->_maxy + 1; n++)
117	    {
118	      (void) fwrite(win->_line[n].text,
119			    sizeof(chtype), (size_t)(win->_maxx + 1), filep);
120	      if (ferror(filep))
121		returnCode(code);
122	    }
123	  code = OK;
124	}
125	returnCode(code);
126}
127
128int scr_restore(const char *file)
129{
130	FILE	*fp;
131
132	T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file)));
133
134	if (_nc_access(file, R_OK) < 0
135	 || (fp = fopen(file, "rb")) == 0)
136	    returnCode(ERR);
137	else
138	{
139	    delwin(newscr);
140	    newscr = getwin(fp);
141	    (void) fclose(fp);
142	    returnCode(OK);
143	}
144}
145
146int scr_dump(const char *file)
147{
148	FILE	*fp;
149
150	T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
151
152	if (_nc_access(file, W_OK) < 0
153	 || (fp = fopen(file, "wb")) == 0)
154	    returnCode(ERR);
155	else
156	{
157	    (void) putwin(newscr, fp);
158	    (void) fclose(fp);
159	    dumptime = time((time_t *)0);
160	    returnCode(OK);
161	}
162}
163
164int scr_init(const char *file)
165{
166	FILE	*fp;
167	struct stat	stb;
168
169	T((T_CALLED("scr_init(%s)"), _nc_visbuf(file)));
170
171	if (exit_ca_mode && non_rev_rmcup)
172	    returnCode(ERR);
173
174	if (_nc_access(file, R_OK) < 0
175	 || (fp = fopen(file, "rb")) == 0)
176	    returnCode(ERR);
177	else if (fstat(STDOUT_FILENO, &stb) || stb.st_mtime > dumptime)
178	    returnCode(ERR);
179	else
180	{
181	    delwin(curscr);
182	    curscr = getwin(fp);
183	    (void) fclose(fp);
184	    returnCode(OK);
185	}
186}
187
188int scr_set(const char *file)
189{
190    T((T_CALLED("scr_set(%s)"), _nc_visbuf(file)));
191
192    if (scr_init(file) == ERR)
193	returnCode(ERR);
194    else
195    {
196	delwin(newscr);
197	newscr = dupwin(curscr);
198	returnCode(OK);
199    }
200}
201