xref: /titanic_51/usr/src/lib/libcurses/screen/scr_reset.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	"curses_inc.h"
45 #include	<sys/types.h>
46 #include	<sys/stat.h>
47 #include	<stdlib.h>
48 
49 /*
50  * Initialize the screen image to be the image contained
51  * in the given file. This is usually used in a child process
52  * to initialize its idea of the screen image to be that of its
53  * parent.
54  *
55  * filep:	pointer to the output stream
56  * type:	0: <curses> should assume that the physical screen is
57  *		   EXACTLY as stored in the file. Therefore, we take
58  *		   special care to make sure that /dev/tty and the terminal
59  *		   did not change in any way.  This information can then
60  *		   be used in the update optimization of the new program
61  *		   so that the screen does not have to be cleared.  Instead,
62  *		   curses, by knowing what's on the screen can optimally
63  *		   update it with the information of the new program.
64  *
65  *		1: Tell <curses> that the stored image should be
66  *		   the physical image.  Sort of like a huge waddstr onto
67  *		   curscr.  This can be used when a library wants to save
68  *		   a screen image and restore it at a later time.
69  *
70  *		2: Tell <curses> that the stored image is the physical
71  *		   image and also it is what the new program wants on the
72  *		   screen.  This can be be thought of as a screen inheritance
73  *		   function.
74  */
75 
76 int
77 scr_reset(FILE *filep, int type)
78 {
79 	WINDOW		*win = NULL, *win1 = NULL;
80 	int		*hash, y;
81 	char		clearit = FALSE;
82 	short		magic;
83 	struct	stat	statbuf;
84 	time_t		ttytime;
85 
86 	if (type != 1 && exit_ca_mode && *exit_ca_mode && non_rev_rmcup) {
87 		if (type == 0)
88 			goto err;
89 		else {
90 #ifdef	DEBUG
91 			if (outf)
92 				fprintf(outf, "clear it because of "
93 				    "exit_ca_mode\n");
94 #endif	/* DEBUG */
95 			clearit = TRUE;
96 		}
97 	}
98 
99 	/* check magic number */
100 	if (fread((char *) &magic, sizeof (short), 1, filep) != 1)
101 		goto err;
102 	if (magic != SVR3_DUMP_MAGIC_NUMBER)
103 		goto err;
104 
105 	/* get modification time of image in file */
106 	if (fread((char *) &ttytime, sizeof (time_t), 1, filep) != 1)
107 		goto err;
108 
109 	if ((type != 1) && ((ttyname(cur_term->Filedes) == NULL) ||
110 	    (fstat(cur_term->Filedes, &statbuf) < 0) ||
111 	    (statbuf.st_mtime != ttytime))) {
112 		if (type == 0)
113 			goto err;
114 		else {
115 #ifdef	DEBUG
116 			if (outf)
117 				fprintf(outf, "Filedes = %hd, "
118 				    "statbuf.st_mtime = %d, "
119 				    "ttytime = %d\n", cur_term->Filedes,
120 				    statbuf.st_mtime, ttytime);
121 #endif	/* DEBUG */
122 			clearit = TRUE;
123 		}
124 	}
125 
126 	/* if get here, everything is ok, read the curscr image */
127 	if (((win = getwin(filep)) == NULL) ||
128 	    ((type == 2) && ((win1 = dupwin(win)) == NULL)) ||
129 	    (win->_maxy != curscr->_maxy) || (win->_maxx != curscr->_maxx) ||
130 	    /* soft labels */
131 	    (fread((char *) &magic, sizeof (int), 1, filep) != 1))
132 		goto err;
133 
134 	/*
135 	* if soft labels were dumped, we would like either read them
136 	* or advance the file pointer pass them
137 	*/
138 	if (magic) {
139 		short	i, labmax, lablen;
140 		SLK_MAP	*slk = SP->slk;
141 		/*
142 		 * Why doesn't the following line and the two below
143 		 * that access those variables work ?
144 		 */
145 		/*
146 		 * char	**labdis = SP->slk->_ldis, **labval = SP->slk->_lval;
147 		*/
148 
149 		if ((fread((char *) &labmax, sizeof (short), 1, filep) != 1) ||
150 		    (fread((char *) &lablen, sizeof (short), 1, filep) != 1)) {
151 			goto err;
152 		}
153 
154 		if (slk != NULL) {
155 			if ((labmax != slk->_num) ||
156 			    (lablen != (slk->_len + 1)))
157 				goto err;
158 
159 			for (i = 0; i < labmax; i++) {
160 				/*
161 				 * if ((fread(labdis[i], sizeof (char), lablen,
162 				 * filep) != lablen) ||
163 				 * (fread(labval[i], sizeof (char), lablen,
164 				 * filep != lablen))
165 				*/
166 				if ((fread(slk->_ldis[i], sizeof (char),
167 				    lablen, filep) != lablen) ||
168 				    (fread(slk->_lval[i],
169 				    sizeof (char), lablen, filep) != lablen)) {
170 					goto err;
171 				}
172 			}
173 			(*_do_slk_tch)();
174 		} else {
175 			if (fseek(filep, (long) (2 * labmax * lablen *
176 			    sizeof (char)), 1) != 0)
177 				goto err;
178 		}
179 	}
180 
181 	/* read the color information(if any) from the file 		*/
182 
183 	if (fread((char *) &magic, sizeof (int), 1, filep) != 1)
184 		goto err;
185 
186 	if (magic) {
187 		int  colors, color_pairs;
188 		bool could_change;
189 		int i;
190 
191 	/* if the new terminal doesn't support colors, or it supports    */
192 	/* less colors (or color_pairs) than the old terminal, or	 */
193 	/* start_color() has not been called, simply advance  the file	 */
194 	/* pointer pass the color related info.				 */
195 	/* Note: must to read the first line of color info, even if the  */
196 	/* new terminal doesn't support color, in order to know how to   */
197 	/* deal with the rest of the file				 */
198 
199 		if ((fread((char *) &colors, sizeof (int), 1, filep) != 1) ||
200 		    (fread((char *) &color_pairs, sizeof (int), 1,
201 		    filep) != 1) || (fread((char *) &could_change,
202 		    sizeof (char), 1, filep) != 1))
203 			goto err;
204 
205 		if (max_pairs == -1 || cur_term->_pairs_tbl == NULL ||
206 		    colors > max_colors || color_pairs > max_pairs) {
207 			if (fseek(filep, (long) (colors * sizeof (_Color) +
208 			    color_pairs * sizeof (_Color_pair)), 1) != 0)
209 				goto err;
210 		} else {
211 			_Color_pair *ptp, *save_ptp;
212 
213 	    /* if both old and new terminals could modify colors, read in */
214 	    /* color table, and call init_color for each color		  */
215 
216 			if (could_change) {
217 				if (can_change) {
218 					_Color	 *ctp, *save_ctp;
219 
220 					if ((save_ctp = (ctp = (_Color *)
221 					    malloc(colors *
222 					    sizeof (_Color)))) == NULL)
223 						goto err;
224 
225 					if (fread(ctp, sizeof (_Color),
226 					    colors, filep) != colors)
227 						goto err;
228 
229 					for (i = 0; i < colors; i++, ctp++) {
230 						/* LINTED */
231 						(void) init_color((short)i,
232 						    ctp->r, ctp->g, ctp->b);
233 					}
234 					free(save_ctp);
235 				} else {
236 
237 		/* the old terminal could modify colors, by the new one */
238 		/* cannot skip over color_table info.			*/
239 
240 					if (fseek(filep, (long) (colors *
241 					    sizeof (_Color)), 1) != 0)
242 						goto err;
243 				}
244 			}
245 
246 	    /* read color_pairs info. call init_pair for each pair	*/
247 
248 			if ((save_ptp = (ptp = (_Color_pair *)
249 			    malloc(color_pairs * sizeof (_Color_pair)))) ==
250 			    NULL)
251 				goto err;
252 			if (fread(ptp, sizeof (_Color_pair), color_pairs,
253 			    filep) != color_pairs) {
254 err:
255 				if (win != NULL)
256 					(void) delwin(win);
257 				if (win1 != NULL)
258 					(void) delwin(win1);
259 				if (type == 0)
260 					curscr->_clear = TRUE;
261 				return (ERR);
262 			}
263 
264 			for (i = 1, ++ptp; i <= color_pairs; i++, ptp++) {
265 				if (ptp->init)
266 					/* LINTED */
267 					(void) init_pair((short)i,
268 					     ptp->foreground, ptp->background);
269 			}
270 			free(save_ptp);
271 		}
272 	}
273 
274 	/* substitute read in window for the curscr */
275 	switch (type) {
276 		case 1:
277 		case 2:
278 			(void) delwin(_virtscr);
279 			hash = _VIRTHASH;
280 			if (type == 1) {
281 				SP->virt_scr = _virtscr = win;
282 				_VIRTTOP = 0;
283 				_VIRTBOT = curscr->_maxy - 1;
284 				break;
285 			}
286 			SP->virt_scr = _virtscr = win1;
287 			_VIRTTOP = curscr->_maxy;
288 			_VIRTBOT = -1;
289 			/* clear the hash table */
290 			for (y = curscr->_maxy; y > 0; --y)
291 				*hash++ = _NOHASH;
292 		/* LINTED */ /* Known fall-through on case statement. */
293 		case 0:
294 			{
295 			int	saveflag = curscr->_flags & _CANT_BE_IMMED;
296 
297 			(void) delwin(curscr);
298 			SP->cur_scr = curscr = win;
299 			curscr->_sync = TRUE;
300 			curscr->_flags |= saveflag;
301 			hash = _CURHASH;
302 		}
303 	}
304 
305 	/* clear the hash table */
306 	for (y = curscr->_maxy; y > 0; --y)
307 		*hash++ = _NOHASH;
308 
309 	curscr->_clear = clearit;
310 	return (OK);
311 }
312