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