xref: /illumos-gate/usr/src/lib/libcurses/screen/scr_reset.c (revision 59ee40951f56cfa1c0e6c5c6aeea10605267116a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
437c478bd9Sstevel@tonic-gate #include	<sys/types.h>
447c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
457c478bd9Sstevel@tonic-gate #include	<stdlib.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Initialize the screen image to be the image contained
497c478bd9Sstevel@tonic-gate  * in the given file. This is usually used in a child process
507c478bd9Sstevel@tonic-gate  * to initialize its idea of the screen image to be that of its
517c478bd9Sstevel@tonic-gate  * parent.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * filep:	pointer to the output stream
547c478bd9Sstevel@tonic-gate  * type:	0: <curses> should assume that the physical screen is
557c478bd9Sstevel@tonic-gate  *		   EXACTLY as stored in the file. Therefore, we take
567c478bd9Sstevel@tonic-gate  *		   special care to make sure that /dev/tty and the terminal
577c478bd9Sstevel@tonic-gate  *		   did not change in any way.  This information can then
587c478bd9Sstevel@tonic-gate  *		   be used in the update optimization of the new program
597c478bd9Sstevel@tonic-gate  *		   so that the screen does not have to be cleared.  Instead,
607c478bd9Sstevel@tonic-gate  *		   curses, by knowing what's on the screen can optimally
617c478bd9Sstevel@tonic-gate  *		   update it with the information of the new program.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  *		1: Tell <curses> that the stored image should be
647c478bd9Sstevel@tonic-gate  *		   the physical image.  Sort of like a huge waddstr onto
657c478bd9Sstevel@tonic-gate  *		   curscr.  This can be used when a library wants to save
667c478bd9Sstevel@tonic-gate  *		   a screen image and restore it at a later time.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *		2: Tell <curses> that the stored image is the physical
697c478bd9Sstevel@tonic-gate  *		   image and also it is what the new program wants on the
707c478bd9Sstevel@tonic-gate  *		   screen.  This can be be thought of as a screen inheritance
717c478bd9Sstevel@tonic-gate  *		   function.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate int
757c478bd9Sstevel@tonic-gate scr_reset(FILE *filep, int type)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	WINDOW		*win = NULL, *win1 = NULL;
787c478bd9Sstevel@tonic-gate 	int		*hash, y;
797c478bd9Sstevel@tonic-gate 	char		clearit = FALSE;
807c478bd9Sstevel@tonic-gate 	short		magic;
817c478bd9Sstevel@tonic-gate 	struct	stat	statbuf;
827c478bd9Sstevel@tonic-gate 	time_t		ttytime;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (type != 1 && exit_ca_mode && *exit_ca_mode && non_rev_rmcup) {
857c478bd9Sstevel@tonic-gate 		if (type == 0)
867c478bd9Sstevel@tonic-gate 			goto err;
877c478bd9Sstevel@tonic-gate 		else {
887c478bd9Sstevel@tonic-gate #ifdef	DEBUG
897c478bd9Sstevel@tonic-gate 			if (outf)
907c478bd9Sstevel@tonic-gate 				fprintf(outf, "clear it because of "
917c478bd9Sstevel@tonic-gate 				    "exit_ca_mode\n");
927c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
937c478bd9Sstevel@tonic-gate 			clearit = TRUE;
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* check magic number */
98*59ee4095SToomas Soome 	if (fread((char *)&magic, sizeof (short), 1, filep) != 1)
997c478bd9Sstevel@tonic-gate 		goto err;
1007c478bd9Sstevel@tonic-gate 	if (magic != SVR3_DUMP_MAGIC_NUMBER)
1017c478bd9Sstevel@tonic-gate 		goto err;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/* get modification time of image in file */
104*59ee4095SToomas Soome 	if (fread((char *)&ttytime, sizeof (time_t), 1, filep) != 1)
1057c478bd9Sstevel@tonic-gate 		goto err;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if ((type != 1) && ((ttyname(cur_term->Filedes) == NULL) ||
1087c478bd9Sstevel@tonic-gate 	    (fstat(cur_term->Filedes, &statbuf) < 0) ||
1097c478bd9Sstevel@tonic-gate 	    (statbuf.st_mtime != ttytime))) {
1107c478bd9Sstevel@tonic-gate 		if (type == 0)
1117c478bd9Sstevel@tonic-gate 			goto err;
1127c478bd9Sstevel@tonic-gate 		else {
1137c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1147c478bd9Sstevel@tonic-gate 			if (outf)
1157c478bd9Sstevel@tonic-gate 				fprintf(outf, "Filedes = %hd, "
1167c478bd9Sstevel@tonic-gate 				    "statbuf.st_mtime = %d, "
1177c478bd9Sstevel@tonic-gate 				    "ttytime = %d\n", cur_term->Filedes,
1187c478bd9Sstevel@tonic-gate 				    statbuf.st_mtime, ttytime);
1197c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1207c478bd9Sstevel@tonic-gate 			clearit = TRUE;
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/* if get here, everything is ok, read the curscr image */
1257c478bd9Sstevel@tonic-gate 	if (((win = getwin(filep)) == NULL) ||
1267c478bd9Sstevel@tonic-gate 	    ((type == 2) && ((win1 = dupwin(win)) == NULL)) ||
1277c478bd9Sstevel@tonic-gate 	    (win->_maxy != curscr->_maxy) || (win->_maxx != curscr->_maxx) ||
1287c478bd9Sstevel@tonic-gate 	    /* soft labels */
129*59ee4095SToomas Soome 	    (fread((char *)&magic, sizeof (int), 1, filep) != 1))
1307c478bd9Sstevel@tonic-gate 		goto err;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
133*59ee4095SToomas Soome 	 * if soft labels were dumped, we would like either read them
134*59ee4095SToomas Soome 	 * or advance the file pointer pass them
135*59ee4095SToomas Soome 	 */
1367c478bd9Sstevel@tonic-gate 	if (magic) {
1377c478bd9Sstevel@tonic-gate 		short	i, labmax, lablen;
1387c478bd9Sstevel@tonic-gate 		SLK_MAP	*slk = SP->slk;
1397c478bd9Sstevel@tonic-gate 		/*
1407c478bd9Sstevel@tonic-gate 		 * Why doesn't the following line and the two below
1417c478bd9Sstevel@tonic-gate 		 * that access those variables work ?
1427c478bd9Sstevel@tonic-gate 		 */
1437c478bd9Sstevel@tonic-gate 		/*
1447c478bd9Sstevel@tonic-gate 		 * char	**labdis = SP->slk->_ldis, **labval = SP->slk->_lval;
145*59ee4095SToomas Soome 		 */
1467c478bd9Sstevel@tonic-gate 
147*59ee4095SToomas Soome 		if ((fread((char *)&labmax, sizeof (short), 1, filep) != 1) ||
148*59ee4095SToomas Soome 		    (fread((char *)&lablen, sizeof (short), 1, filep) != 1)) {
1497c478bd9Sstevel@tonic-gate 			goto err;
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		if (slk != NULL) {
1537c478bd9Sstevel@tonic-gate 			if ((labmax != slk->_num) ||
1547c478bd9Sstevel@tonic-gate 			    (lablen != (slk->_len + 1)))
1557c478bd9Sstevel@tonic-gate 				goto err;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 			for (i = 0; i < labmax; i++) {
1587c478bd9Sstevel@tonic-gate 				/*
1597c478bd9Sstevel@tonic-gate 				 * if ((fread(labdis[i], sizeof (char), lablen,
1607c478bd9Sstevel@tonic-gate 				 * filep) != lablen) ||
1617c478bd9Sstevel@tonic-gate 				 * (fread(labval[i], sizeof (char), lablen,
1627c478bd9Sstevel@tonic-gate 				 * filep != lablen))
163*59ee4095SToomas Soome 				 */
1647c478bd9Sstevel@tonic-gate 				if ((fread(slk->_ldis[i], sizeof (char),
1657c478bd9Sstevel@tonic-gate 				    lablen, filep) != lablen) ||
1667c478bd9Sstevel@tonic-gate 				    (fread(slk->_lval[i],
1677c478bd9Sstevel@tonic-gate 				    sizeof (char), lablen, filep) != lablen)) {
1687c478bd9Sstevel@tonic-gate 					goto err;
1697c478bd9Sstevel@tonic-gate 				}
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 			(*_do_slk_tch)();
1727c478bd9Sstevel@tonic-gate 		} else {
173*59ee4095SToomas Soome 			if (fseek(filep, (long)(2 * labmax * lablen *
1747c478bd9Sstevel@tonic-gate 			    sizeof (char)), 1) != 0)
1757c478bd9Sstevel@tonic-gate 				goto err;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* read the color information(if any) from the file 		*/
1807c478bd9Sstevel@tonic-gate 
181*59ee4095SToomas Soome 	if (fread((char *)&magic, sizeof (int), 1, filep) != 1)
1827c478bd9Sstevel@tonic-gate 		goto err;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (magic) {
1857c478bd9Sstevel@tonic-gate 		int  colors, color_pairs;
1867c478bd9Sstevel@tonic-gate 		bool could_change;
1877c478bd9Sstevel@tonic-gate 		int i;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/* if the new terminal doesn't support colors, or it supports    */
1907c478bd9Sstevel@tonic-gate 	/* less colors (or color_pairs) than the old terminal, or	 */
1917c478bd9Sstevel@tonic-gate 	/* start_color() has not been called, simply advance  the file	 */
1927c478bd9Sstevel@tonic-gate 	/* pointer pass the color related info.				 */
1937c478bd9Sstevel@tonic-gate 	/* Note: must to read the first line of color info, even if the  */
1947c478bd9Sstevel@tonic-gate 	/* new terminal doesn't support color, in order to know how to   */
1957c478bd9Sstevel@tonic-gate 	/* deal with the rest of the file				 */
1967c478bd9Sstevel@tonic-gate 
197*59ee4095SToomas Soome 		if ((fread((char *)&colors, sizeof (int), 1, filep) != 1) ||
198*59ee4095SToomas Soome 		    (fread((char *)&color_pairs, sizeof (int), 1,
199*59ee4095SToomas Soome 		    filep) != 1) || (fread((char *)&could_change,
2007c478bd9Sstevel@tonic-gate 		    sizeof (char), 1, filep) != 1))
2017c478bd9Sstevel@tonic-gate 			goto err;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		if (max_pairs == -1 || cur_term->_pairs_tbl == NULL ||
2047c478bd9Sstevel@tonic-gate 		    colors > max_colors || color_pairs > max_pairs) {
205*59ee4095SToomas Soome 			if (fseek(filep, (long)(colors * sizeof (_Color) +
2067c478bd9Sstevel@tonic-gate 			    color_pairs * sizeof (_Color_pair)), 1) != 0)
2077c478bd9Sstevel@tonic-gate 				goto err;
2087c478bd9Sstevel@tonic-gate 		} else {
2097c478bd9Sstevel@tonic-gate 			_Color_pair *ptp, *save_ptp;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	    /* if both old and new terminals could modify colors, read in */
2127c478bd9Sstevel@tonic-gate 	    /* color table, and call init_color for each color		  */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 			if (could_change) {
2157c478bd9Sstevel@tonic-gate 				if (can_change) {
2167c478bd9Sstevel@tonic-gate 					_Color	 *ctp, *save_ctp;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 					if ((save_ctp = (ctp = (_Color *)
2197c478bd9Sstevel@tonic-gate 					    malloc(colors *
2207c478bd9Sstevel@tonic-gate 					    sizeof (_Color)))) == NULL)
2217c478bd9Sstevel@tonic-gate 						goto err;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 					if (fread(ctp, sizeof (_Color),
2247c478bd9Sstevel@tonic-gate 					    colors, filep) != colors)
2257c478bd9Sstevel@tonic-gate 						goto err;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 					for (i = 0; i < colors; i++, ctp++) {
2287c478bd9Sstevel@tonic-gate 						/* LINTED */
2297c478bd9Sstevel@tonic-gate 						(void) init_color((short)i,
2307c478bd9Sstevel@tonic-gate 						    ctp->r, ctp->g, ctp->b);
2317c478bd9Sstevel@tonic-gate 					}
2327c478bd9Sstevel@tonic-gate 					free(save_ctp);
2337c478bd9Sstevel@tonic-gate 				} else {
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		/* the old terminal could modify colors, by the new one */
2367c478bd9Sstevel@tonic-gate 		/* cannot skip over color_table info.			*/
2377c478bd9Sstevel@tonic-gate 
238*59ee4095SToomas Soome 					if (fseek(filep, (long)(colors *
2397c478bd9Sstevel@tonic-gate 					    sizeof (_Color)), 1) != 0)
2407c478bd9Sstevel@tonic-gate 						goto err;
2417c478bd9Sstevel@tonic-gate 				}
2427c478bd9Sstevel@tonic-gate 			}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	    /* read color_pairs info. call init_pair for each pair	*/
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 			if ((save_ptp = (ptp = (_Color_pair *)
2477c478bd9Sstevel@tonic-gate 			    malloc(color_pairs * sizeof (_Color_pair)))) ==
2487c478bd9Sstevel@tonic-gate 			    NULL)
2497c478bd9Sstevel@tonic-gate 				goto err;
2507c478bd9Sstevel@tonic-gate 			if (fread(ptp, sizeof (_Color_pair), color_pairs,
2517c478bd9Sstevel@tonic-gate 			    filep) != color_pairs) {
2527c478bd9Sstevel@tonic-gate err:
2537c478bd9Sstevel@tonic-gate 				if (win != NULL)
2547c478bd9Sstevel@tonic-gate 					(void) delwin(win);
2557c478bd9Sstevel@tonic-gate 				if (win1 != NULL)
2567c478bd9Sstevel@tonic-gate 					(void) delwin(win1);
2577c478bd9Sstevel@tonic-gate 				if (type == 0)
2587c478bd9Sstevel@tonic-gate 					curscr->_clear = TRUE;
2597c478bd9Sstevel@tonic-gate 				return (ERR);
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 			for (i = 1, ++ptp; i <= color_pairs; i++, ptp++) {
2637c478bd9Sstevel@tonic-gate 				if (ptp->init)
2647c478bd9Sstevel@tonic-gate 					/* LINTED */
2657c478bd9Sstevel@tonic-gate 					(void) init_pair((short)i,
266*59ee4095SToomas Soome 					    ptp->foreground, ptp->background);
2677c478bd9Sstevel@tonic-gate 			}
2687c478bd9Sstevel@tonic-gate 			free(save_ptp);
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/* substitute read in window for the curscr */
2737c478bd9Sstevel@tonic-gate 	switch (type) {
2747c478bd9Sstevel@tonic-gate 		case 1:
2757c478bd9Sstevel@tonic-gate 		case 2:
2767c478bd9Sstevel@tonic-gate 			(void) delwin(_virtscr);
2777c478bd9Sstevel@tonic-gate 			hash = _VIRTHASH;
2787c478bd9Sstevel@tonic-gate 			if (type == 1) {
2797c478bd9Sstevel@tonic-gate 				SP->virt_scr = _virtscr = win;
2807c478bd9Sstevel@tonic-gate 				_VIRTTOP = 0;
2817c478bd9Sstevel@tonic-gate 				_VIRTBOT = curscr->_maxy - 1;
2827c478bd9Sstevel@tonic-gate 				break;
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 			SP->virt_scr = _virtscr = win1;
2857c478bd9Sstevel@tonic-gate 			_VIRTTOP = curscr->_maxy;
2867c478bd9Sstevel@tonic-gate 			_VIRTBOT = -1;
2877c478bd9Sstevel@tonic-gate 			/* clear the hash table */
2887c478bd9Sstevel@tonic-gate 			for (y = curscr->_maxy; y > 0; --y)
2897c478bd9Sstevel@tonic-gate 				*hash++ = _NOHASH;
290*59ee4095SToomas Soome 			/* FALLTHROUGH */
2917c478bd9Sstevel@tonic-gate 		case 0:
2927c478bd9Sstevel@tonic-gate 			{
2937c478bd9Sstevel@tonic-gate 			int	saveflag = curscr->_flags & _CANT_BE_IMMED;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 			(void) delwin(curscr);
2967c478bd9Sstevel@tonic-gate 			SP->cur_scr = curscr = win;
2977c478bd9Sstevel@tonic-gate 			curscr->_sync = TRUE;
2987c478bd9Sstevel@tonic-gate 			curscr->_flags |= saveflag;
2997c478bd9Sstevel@tonic-gate 			hash = _CURHASH;
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/* clear the hash table */
3047c478bd9Sstevel@tonic-gate 	for (y = curscr->_maxy; y > 0; --y)
3057c478bd9Sstevel@tonic-gate 		*hash++ = _NOHASH;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	curscr->_clear = clearit;
3087c478bd9Sstevel@tonic-gate 	return (OK);
3097c478bd9Sstevel@tonic-gate }
310