xref: /illumos-gate/usr/src/lib/libxcurses2/src/libc/xcurses/scr_dump.c (revision 1e56f352c1c208679012bca47d552e127f5b1072)
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 /* LINTLIBRARY */
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[] =
40 "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
41 "libxcurses/src/libc/xcurses/rcs/scr_dump.c 1.6 1998/06/01 16:25:23 "
42 "cbates Exp $";
43 #endif
44 #endif
45 
46 #include <private.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 
50 /*
51  * Save the current screen image.
52  */
53 int
54 scr_dump(const char *f)
55 {
56 	int	code;
57 	FILE	*fp;
58 
59 	code = ERR;
60 
61 	if ((fp = fopen(f, "wF")) != NULL) {
62 		code = putwin(curscr, fp);
63 		(void) fclose(fp);
64 	}
65 
66 	return (code);
67 }
68 
69 static int
70 scr_replace(WINDOW *w, const char *f)
71 {
72 	int	i;
73 	FILE	*fp;
74 	WINDOW	*new;
75 
76 	if ((fp = fopen(f, "rF")) == NULL)
77 		return (ERR);
78 
79 	new = getwin(fp);
80 	(void) fclose(fp);
81 
82 	if (new == NULL)
83 		return (ERR);
84 
85 	if (new->_maxy != w->_maxy || new->_maxx != w->_maxx) {
86 		(void) delwin(new);
87 		return (ERR);
88 	}
89 
90 	/* Replace contents of curscr window structure. */
91 	free(w->_base);
92 	free(w->_line);
93 	free(w->_first);
94 	new->_flags &= ~W_CLEAR_WINDOW;	/* Removed default clear command */
95 	*w = *new;
96 
97 	/* Rehash the current screen? */
98 	if (w == curscr)
99 		for (i = 0; i < w->_maxy; ++i)
100 			__m_cc_hash(w, __m_screen->_hash, i);
101 
102 	/* Discard the working window. */
103 	new->_base = NULL;
104 	new->_line = NULL;
105 	new->_first = NULL;
106 	(void) delwin(new);
107 	/* Make sure we know where the cursor is */
108 	(void) __m_mvcur(-1, -1, curscr->_cury, curscr->_curx, __m_outc);
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
135 scr_restore(const char *f)
136 {
137 	int	code;
138 
139 	code = scr_replace(__m_screen->_newscr, f);
140 
141 	return (code);
142 }
143 
144 /*
145  * Get the screen image that really reflects what is on the screen,
146  * though the applicatiion may not want it.  A subsequent doupdate()
147  * will compared and make changes against this image.
148  */
149 int
150 scr_init(const char *f)
151 {
152 	int	code;
153 	struct stat	tty, dump;
154 	char	*name;
155 
156 	name = ttyname(cur_term->_ofd);
157 	if ((non_rev_rmcup && exit_ca_mode != NULL) ||
158 		stat(f, &dump) != 0 || name == NULL || stat(name, &tty) != 0)
159 		code = ERR;
160 	else {
161 		if (dump.st_mtime < tty.st_mtime)
162 			code = ERR;
163 		else {
164 			code = scr_replace(__m_screen->_curscr, f);
165 		}
166 	}
167 
168 	return (code);
169 }
170 
171 /*
172  * Get the screen image that is really on the screen and that the
173  * application wants on the screen.
174  */
175 int
176 scr_set(const char *f)
177 {
178 	int	code;
179 
180 	if ((code = scr_init(f)) == OK)
181 		code = scr_restore(f);
182 
183 	return (code);
184 }
185