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
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*004388ebScasper  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * scr_dump.c
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * XCurses Library
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #if M_RCSID
387c478bd9Sstevel@tonic-gate #ifndef lint
397c478bd9Sstevel@tonic-gate static char rcsID[] =
407c478bd9Sstevel@tonic-gate "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
417c478bd9Sstevel@tonic-gate "libxcurses/src/libc/xcurses/rcs/scr_dump.c 1.6 1998/06/01 16:25:23 "
427c478bd9Sstevel@tonic-gate "cbates Exp $";
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <private.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Save the current screen image.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate int
scr_dump(const char * f)547c478bd9Sstevel@tonic-gate scr_dump(const char *f)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	int	code;
577c478bd9Sstevel@tonic-gate 	FILE	*fp;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	code = ERR;
607c478bd9Sstevel@tonic-gate 
61*004388ebScasper 	if ((fp = fopen(f, "wF")) != NULL) {
627c478bd9Sstevel@tonic-gate 		code = putwin(curscr, fp);
637c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	return (code);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static int
scr_replace(WINDOW * w,const char * f)707c478bd9Sstevel@tonic-gate scr_replace(WINDOW *w, const char *f)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	int	i;
737c478bd9Sstevel@tonic-gate 	FILE	*fp;
747c478bd9Sstevel@tonic-gate 	WINDOW	*new;
757c478bd9Sstevel@tonic-gate 
76*004388ebScasper 	if ((fp = fopen(f, "rF")) == NULL)
777c478bd9Sstevel@tonic-gate 		return (ERR);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	new = getwin(fp);
807c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (new == NULL)
837c478bd9Sstevel@tonic-gate 		return (ERR);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (new->_maxy != w->_maxy || new->_maxx != w->_maxx) {
867c478bd9Sstevel@tonic-gate 		(void) delwin(new);
877c478bd9Sstevel@tonic-gate 		return (ERR);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/* Replace contents of curscr window structure. */
917c478bd9Sstevel@tonic-gate 	free(w->_base);
927c478bd9Sstevel@tonic-gate 	free(w->_line);
937c478bd9Sstevel@tonic-gate 	free(w->_first);
947c478bd9Sstevel@tonic-gate 	new->_flags &= ~W_CLEAR_WINDOW;	/* Removed default clear command */
957c478bd9Sstevel@tonic-gate 	*w = *new;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* Rehash the current screen? */
987c478bd9Sstevel@tonic-gate 	if (w == curscr)
997c478bd9Sstevel@tonic-gate 		for (i = 0; i < w->_maxy; ++i)
1007c478bd9Sstevel@tonic-gate 			__m_cc_hash(w, __m_screen->_hash, i);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/* Discard the working window. */
1037c478bd9Sstevel@tonic-gate 	new->_base = NULL;
1047c478bd9Sstevel@tonic-gate 	new->_line = NULL;
1057c478bd9Sstevel@tonic-gate 	new->_first = NULL;
1067c478bd9Sstevel@tonic-gate 	(void) delwin(new);
1077c478bd9Sstevel@tonic-gate 	/* Make sure we know where the cursor is */
1087c478bd9Sstevel@tonic-gate 	(void) __m_mvcur(-1, -1, curscr->_cury, curscr->_curx, __m_outc);
1097c478bd9Sstevel@tonic-gate 	return (OK);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * A picture of what scr_restore(), scr_init(), and scr_set() do :
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  *				scr_restore()		scr_init()
1167c478bd9Sstevel@tonic-gate  *				    |			    |
1177c478bd9Sstevel@tonic-gate  *	stdscr			    V			    V
1187c478bd9Sstevel@tonic-gate  *	+----+			 newscr			 curscr
1197c478bd9Sstevel@tonic-gate  *	|    | 			+-------+		+-------+
1207c478bd9Sstevel@tonic-gate  *	+----+  refresh() ->	|	|		|	|
1217c478bd9Sstevel@tonic-gate  *				|	| doupdate() ->	|	|
1227c478bd9Sstevel@tonic-gate  *	  w			|	| 		|	|
1237c478bd9Sstevel@tonic-gate  *	+----+  wrefresh(w) ->	|	|		|	|
1247c478bd9Sstevel@tonic-gate  *	|    | 			+-------+		+-------+
1257c478bd9Sstevel@tonic-gate  *	+----+                        ^			  ^
1267c478bd9Sstevel@tonic-gate  *				      |	                  |
1277c478bd9Sstevel@tonic-gate  *				      \---- scr_set() ----/
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Get a screen image that will appear next doupdate(),
1327c478bd9Sstevel@tonic-gate  * replacing the current screen.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate int
scr_restore(const char * f)1357c478bd9Sstevel@tonic-gate scr_restore(const char *f)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	int	code;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	code = scr_replace(__m_screen->_newscr, f);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	return (code);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * Get the screen image that really reflects what is on the screen,
1467c478bd9Sstevel@tonic-gate  * though the applicatiion may not want it.  A subsequent doupdate()
1477c478bd9Sstevel@tonic-gate  * will compared and make changes against this image.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate int
scr_init(const char * f)1507c478bd9Sstevel@tonic-gate scr_init(const char *f)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	int	code;
1537c478bd9Sstevel@tonic-gate 	struct stat	tty, dump;
1547c478bd9Sstevel@tonic-gate 	char	*name;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	name = ttyname(cur_term->_ofd);
1577c478bd9Sstevel@tonic-gate 	if ((non_rev_rmcup && exit_ca_mode != NULL) ||
1587c478bd9Sstevel@tonic-gate 		stat(f, &dump) != 0 || name == NULL || stat(name, &tty) != 0)
1597c478bd9Sstevel@tonic-gate 		code = ERR;
1607c478bd9Sstevel@tonic-gate 	else {
1617c478bd9Sstevel@tonic-gate 		if (dump.st_mtime < tty.st_mtime)
1627c478bd9Sstevel@tonic-gate 			code = ERR;
1637c478bd9Sstevel@tonic-gate 		else {
1647c478bd9Sstevel@tonic-gate 			code = scr_replace(__m_screen->_curscr, f);
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (code);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Get the screen image that is really on the screen and that the
1737c478bd9Sstevel@tonic-gate  * application wants on the screen.
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate int
scr_set(const char * f)1767c478bd9Sstevel@tonic-gate scr_set(const char *f)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	int	code;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if ((code = scr_init(f)) == OK)
1817c478bd9Sstevel@tonic-gate 		code = scr_restore(f);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (code);
1847c478bd9Sstevel@tonic-gate }
185