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