xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/xcurses/scr_dump.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 /*
29  * scr_dump.c
30  *
31  * XCurses Library
32  *
33  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
34  *
35  */
36 
37 #if M_RCSID
38 #ifndef lint
39 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/scr_dump.c 1.1 1995/06/21 16:19:43 ant Exp $";
40 #endif
41 #endif
42 
43 #include <private.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 
47 /*
48  * Save the current screen image.
49  */
50 int
51 scr_dump(f)
52 const char *f;
53 {
54 	int code;
55 	FILE *fp;
56 
57 #ifdef M_CURSES_TRACE
58 	__m_trace("scr_dump(%p=\"%s\")", f);
59 #endif
60 
61 	code = ERR;
62 
63 	if ((fp = fopen(f, "wF")) != (FILE *) 0) {
64 		code = putwin(curscr, fp);
65 		(void) fclose(fp);
66 	}
67 
68 	return __m_return_code("scr_dump", code);
69 }
70 
71 static int
72 scr_replace(w, f)
73 WINDOW *w;
74 const char *f;
75 {
76 	int i;
77 	FILE *fp;
78 	WINDOW *new;
79 
80 	if ((fp = fopen(f, "rF")) == (FILE *) 0)
81 		return ERR;
82 
83 	new = getwin(fp);
84 	(void) fclose(fp);
85 
86 	if (new == (WINDOW *) 0)
87 		return ERR;
88 
89 	if (new->_maxy != w->_maxy || new->_maxx != w->_maxx) {
90 		(void) delwin(new);
91 		return ERR;
92 	}
93 
94 	/* Replace contents of curscr window structure. */
95 	free(w->_base);
96 	free(w->_line);
97 	free(w->_first);
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 = (cchar_t *) 0;
107 	new->_line = (cchar_t **) 0;
108 	new->_first = (short *) 0;
109 	(void) delwin(new);
110 
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(f)
138 const char *f;
139 {
140 	int code;
141 
142 #ifdef M_CURSES_TRACE
143 	__m_trace("scr_restore(%p=\"%s\")", f);
144 #endif
145 
146 	code = scr_replace(__m_screen->_newscr, f);
147 
148 	return __m_return_code("scr_restore", code);
149 }
150 
151 /*
152  * Get the screen image that really reflects what is on the screen,
153  * though the applicatiion may not want it.  A subsequent doupdate()
154  * will compared and make changes against this image.
155  */
156 int
157 scr_init(f)
158 const char *f;
159 {
160 	int code;
161 	struct stat tty, dump;
162 
163 #ifdef M_CURSES_TRACE
164 	__m_trace("scr_init(%p=\"%s\")", f);
165 #endif
166 
167 	if ((non_rev_rmcup && exit_ca_mode != (char *) 0)
168 	|| stat(f, &dump) != 0 || stat(ctermid((char *) 0), &tty) != 0
169 	|| dump.st_mtime < tty.st_mtime)
170 		code = ERR;
171 	else
172 		code = scr_replace(__m_screen->_curscr, f);
173 
174 	return __m_return_code("scr_init", code);
175 }
176 
177 /*
178  * Get the screen image that is really on the screen and that the
179  * application wants on the screen.
180  */
181 int
182 scr_set(f)
183 const char *f;
184 {
185 	int code;
186 
187 #ifdef M_CURSES_TRACE
188 	__m_trace("scr_set(%p=\"%s\")", f);
189 #endif
190 
191 	if ((code = scr_init(f)) == OK)
192 		code = scr_restore(f);
193 
194 	return __m_return_code("scr_set", code);
195 }
196 
197