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 /*
27  * scr_dump.c
28  *
29  * XCurses Library
30  *
31  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
32  *
33  */
34 
35 #if M_RCSID
36 #ifndef lint
37 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/scr_dump.c 1.1 1995/06/21 16:19:43 ant Exp $";
38 #endif
39 #endif
40 
41 #include <private.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 
45 /*
46  * Save the current screen image.
47  */
48 int
scr_dump(f)49 scr_dump(f)
50 const char *f;
51 {
52 	int code;
53 	FILE *fp;
54 
55 #ifdef M_CURSES_TRACE
56 	__m_trace("scr_dump(%p=\"%s\")", f);
57 #endif
58 
59 	code = ERR;
60 
61 	if ((fp = fopen(f, "wF")) != (FILE *) 0) {
62 		code = putwin(curscr, fp);
63 		(void) fclose(fp);
64 	}
65 
66 	return __m_return_code("scr_dump", code);
67 }
68 
69 static int
scr_replace(w,f)70 scr_replace(w, f)
71 WINDOW *w;
72 const char *f;
73 {
74 	int i;
75 	FILE *fp;
76 	WINDOW *new;
77 
78 	if ((fp = fopen(f, "rF")) == (FILE *) 0)
79 		return ERR;
80 
81 	new = getwin(fp);
82 	(void) fclose(fp);
83 
84 	if (new == (WINDOW *) 0)
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 	*w = *new;
97 
98 	/* Rehash the current screen? */
99 	if (w == curscr)
100 		for (i = 0; i < w->_maxy; ++i)
101 			__m_cc_hash(w, __m_screen->_hash, i);
102 
103 	/* Discard the working window. */
104 	new->_base = (cchar_t *) 0;
105 	new->_line = (cchar_t **) 0;
106 	new->_first = (short *) 0;
107 	(void) delwin(new);
108 
109 	return OK;
110 }
111 
112 /*
113  * A picture of what scr_restore(), scr_init(), and scr_set() do :
114  *
115  *				scr_restore()		scr_init()
116  *				    |			    |
117  *	stdscr			    V			    V
118  *	+----+			 newscr			 curscr
119  *	|    | 			+-------+		+-------+
120  *	+----+  refresh() ->	|	|		|	|
121  *				|	| doupdate() ->	|	|
122  *	  w			|	| 		|	|
123  *	+----+  wrefresh(w) ->	|	|		|	|
124  *	|    | 			+-------+		+-------+
125  *	+----+                        ^			  ^
126  *				      |	                  |
127  *				      \---- scr_set() ----/
128  */
129 
130 /*
131  * Get a screen image that will appear next doupdate(),
132  * replacing the current screen.
133  */
134 int
scr_restore(f)135 scr_restore(f)
136 const char *f;
137 {
138 	int code;
139 
140 #ifdef M_CURSES_TRACE
141 	__m_trace("scr_restore(%p=\"%s\")", f);
142 #endif
143 
144 	code = scr_replace(__m_screen->_newscr, f);
145 
146 	return __m_return_code("scr_restore", code);
147 }
148 
149 /*
150  * Get the screen image that really reflects what is on the screen,
151  * though the applicatiion may not want it.  A subsequent doupdate()
152  * will compared and make changes against this image.
153  */
154 int
scr_init(f)155 scr_init(f)
156 const char *f;
157 {
158 	int code;
159 	struct stat tty, dump;
160 
161 #ifdef M_CURSES_TRACE
162 	__m_trace("scr_init(%p=\"%s\")", f);
163 #endif
164 
165 	if ((non_rev_rmcup && exit_ca_mode != (char *) 0)
166 	|| stat(f, &dump) != 0 || stat(ctermid((char *) 0), &tty) != 0
167 	|| dump.st_mtime < tty.st_mtime)
168 		code = ERR;
169 	else
170 		code = scr_replace(__m_screen->_curscr, f);
171 
172 	return __m_return_code("scr_init", code);
173 }
174 
175 /*
176  * Get the screen image that is really on the screen and that the
177  * application wants on the screen.
178  */
179 int
scr_set(f)180 scr_set(f)
181 const char *f;
182 {
183 	int code;
184 
185 #ifdef M_CURSES_TRACE
186 	__m_trace("scr_set(%p=\"%s\")", f);
187 #endif
188 
189 	if ((code = scr_init(f)) == OK)
190 		code = scr_restore(f);
191 
192 	return __m_return_code("scr_set", code);
193 }
194 
195