scr_dump.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright (c) 1995-1998 by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29/* LINTLIBRARY */
30
31/*
32 * scr_dump.c
33 *
34 * XCurses Library
35 *
36 * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
37 *
38 */
39
40#if M_RCSID
41#ifndef lint
42static char rcsID[] =
43"$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
44"libxcurses/src/libc/xcurses/rcs/scr_dump.c 1.6 1998/06/01 16:25:23 "
45"cbates Exp $";
46#endif
47#endif
48
49#include <private.h>
50#include <sys/types.h>
51#include <sys/stat.h>
52
53/*
54 * Save the current screen image.
55 */
56int
57scr_dump(const char *f)
58{
59	int	code;
60	FILE	*fp;
61
62	code = ERR;
63
64	if ((fp = fopen(f, "wb")) != NULL) {
65		code = putwin(curscr, fp);
66		(void) fclose(fp);
67	}
68
69	return (code);
70}
71
72static int
73scr_replace(WINDOW *w, const char *f)
74{
75	int	i;
76	FILE	*fp;
77	WINDOW	*new;
78
79	if ((fp = fopen(f, "rb")) == NULL)
80		return (ERR);
81
82	new = getwin(fp);
83	(void) fclose(fp);
84
85	if (new == NULL)
86		return (ERR);
87
88	if (new->_maxy != w->_maxy || new->_maxx != w->_maxx) {
89		(void) delwin(new);
90		return (ERR);
91	}
92
93	/* Replace contents of curscr window structure. */
94	free(w->_base);
95	free(w->_line);
96	free(w->_first);
97	new->_flags &= ~W_CLEAR_WINDOW;	/* Removed default clear command */
98	*w = *new;
99
100	/* Rehash the current screen? */
101	if (w == curscr)
102		for (i = 0; i < w->_maxy; ++i)
103			__m_cc_hash(w, __m_screen->_hash, i);
104
105	/* Discard the working window. */
106	new->_base = NULL;
107	new->_line = NULL;
108	new->_first = NULL;
109	(void) delwin(new);
110	/* Make sure we know where the cursor is */
111	(void) __m_mvcur(-1, -1, curscr->_cury, curscr->_curx, __m_outc);
112	return (OK);
113}
114
115/*
116 * A picture of what scr_restore(), scr_init(), and scr_set() do :
117 *
118 *				scr_restore()		scr_init()
119 *				    |			    |
120 *	stdscr			    V			    V
121 *	+----+			 newscr			 curscr
122 *	|    | 			+-------+		+-------+
123 *	+----+  refresh() ->	|	|		|	|
124 *				|	| doupdate() ->	|	|
125 *	  w			|	| 		|	|
126 *	+----+  wrefresh(w) ->	|	|		|	|
127 *	|    | 			+-------+		+-------+
128 *	+----+                        ^			  ^
129 *				      |	                  |
130 *				      \---- scr_set() ----/
131 */
132
133/*
134 * Get a screen image that will appear next doupdate(),
135 * replacing the current screen.
136 */
137int
138scr_restore(const char *f)
139{
140	int	code;
141
142	code = scr_replace(__m_screen->_newscr, f);
143
144	return (code);
145}
146
147/*
148 * Get the screen image that really reflects what is on the screen,
149 * though the applicatiion may not want it.  A subsequent doupdate()
150 * will compared and make changes against this image.
151 */
152int
153scr_init(const char *f)
154{
155	int	code;
156	struct stat	tty, dump;
157	char	*name;
158
159	name = ttyname(cur_term->_ofd);
160	if ((non_rev_rmcup && exit_ca_mode != NULL) ||
161		stat(f, &dump) != 0 || name == NULL || stat(name, &tty) != 0)
162		code = ERR;
163	else {
164		if (dump.st_mtime < tty.st_mtime)
165			code = ERR;
166		else {
167			code = scr_replace(__m_screen->_curscr, f);
168		}
169	}
170
171	return (code);
172}
173
174/*
175 * Get the screen image that is really on the screen and that the
176 * application wants on the screen.
177 */
178int
179scr_set(const char *f)
180{
181	int	code;
182
183	if ((code = scr_init(f)) == OK)
184		code = scr_restore(f);
185
186	return (code);
187}
188