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