scr_reset.c revision 2
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * University Copyright- Copyright (c) 1982, 1986, 1988
2N/A * The Regents of the University of California
2N/A * All Rights Reserved
2N/A *
2N/A * University Acknowledgment- Portions of this document are derived from
2N/A * software developed by the University of California, Berkeley, and its
2N/A * contributors.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*LINTLIBRARY*/
2N/A
2N/A#include "curses_inc.h"
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <stdlib.h>
2N/A
2N/A/*
2N/A * Initialize the screen image to be the image contained
2N/A * in the given file. This is usually used in a child process
2N/A * to initialize its idea of the screen image to be that of its
2N/A * parent.
2N/A *
2N/A * filep: pointer to the output stream
2N/A * type: 0: <curses> should assume that the physical screen is
2N/A * EXACTLY as stored in the file. Therefore, we take
2N/A * special care to make sure that /dev/tty and the terminal
2N/A * did not change in any way. This information can then
2N/A * be used in the update optimization of the new program
2N/A * so that the screen does not have to be cleared. Instead,
2N/A * curses, by knowing what's on the screen can optimally
2N/A * update it with the information of the new program.
2N/A *
2N/A * 1: Tell <curses> that the stored image should be
2N/A * the physical image. Sort of like a huge waddstr onto
2N/A * curscr. This can be used when a library wants to save
2N/A * a screen image and restore it at a later time.
2N/A *
2N/A * 2: Tell <curses> that the stored image is the physical
2N/A * image and also it is what the new program wants on the
2N/A * screen. This can be be thought of as a screen inheritance
2N/A * function.
2N/A */
2N/A
2N/Aint
2N/Ascr_reset(FILE *filep, int type)
2N/A{
2N/A WINDOW *win = NULL, *win1 = NULL;
2N/A int *hash, y;
2N/A char clearit = FALSE;
2N/A short magic;
2N/A struct stat statbuf;
2N/A time_t ttytime;
2N/A
2N/A if (type != 1 && exit_ca_mode && *exit_ca_mode && non_rev_rmcup) {
2N/A if (type == 0)
2N/A goto err;
2N/A else {
2N/A#ifdef DEBUG
2N/A if (outf)
2N/A fprintf(outf, "clear it because of "
2N/A "exit_ca_mode\n");
2N/A#endif /* DEBUG */
2N/A clearit = TRUE;
2N/A }
2N/A }
2N/A
2N/A /* check magic number */
2N/A if (fread((char *) &magic, sizeof (short), 1, filep) != 1)
2N/A goto err;
2N/A if (magic != SVR3_DUMP_MAGIC_NUMBER)
2N/A goto err;
2N/A
2N/A /* get modification time of image in file */
2N/A if (fread((char *) &ttytime, sizeof (time_t), 1, filep) != 1)
2N/A goto err;
2N/A
2N/A if ((type != 1) && ((ttyname(cur_term->Filedes) == NULL) ||
2N/A (fstat(cur_term->Filedes, &statbuf) < 0) ||
2N/A (statbuf.st_mtime != ttytime))) {
2N/A if (type == 0)
2N/A goto err;
2N/A else {
2N/A#ifdef DEBUG
2N/A if (outf)
2N/A fprintf(outf, "Filedes = %hd, "
2N/A "statbuf.st_mtime = %d, "
2N/A "ttytime = %d\n", cur_term->Filedes,
2N/A statbuf.st_mtime, ttytime);
2N/A#endif /* DEBUG */
2N/A clearit = TRUE;
2N/A }
2N/A }
2N/A
2N/A /* if get here, everything is ok, read the curscr image */
2N/A if (((win = getwin(filep)) == NULL) ||
2N/A ((type == 2) && ((win1 = dupwin(win)) == NULL)) ||
2N/A (win->_maxy != curscr->_maxy) || (win->_maxx != curscr->_maxx) ||
2N/A /* soft labels */
2N/A (fread((char *) &magic, sizeof (int), 1, filep) != 1))
2N/A goto err;
2N/A
2N/A /*
2N/A * if soft labels were dumped, we would like either read them
2N/A * or advance the file pointer pass them
2N/A */
2N/A if (magic) {
2N/A short i, labmax, lablen;
2N/A SLK_MAP *slk = SP->slk;
2N/A /*
2N/A * Why doesn't the following line and the two below
2N/A * that access those variables work ?
2N/A */
2N/A /*
2N/A * char **labdis = SP->slk->_ldis, **labval = SP->slk->_lval;
2N/A */
2N/A
2N/A if ((fread((char *) &labmax, sizeof (short), 1, filep) != 1) ||
2N/A (fread((char *) &lablen, sizeof (short), 1, filep) != 1)) {
2N/A goto err;
2N/A }
2N/A
2N/A if (slk != NULL) {
2N/A if ((labmax != slk->_num) ||
2N/A (lablen != (slk->_len + 1)))
2N/A goto err;
2N/A
2N/A for (i = 0; i < labmax; i++) {
2N/A /*
2N/A * if ((fread(labdis[i], sizeof (char), lablen,
2N/A * filep) != lablen) ||
2N/A * (fread(labval[i], sizeof (char), lablen,
2N/A * filep != lablen))
2N/A */
2N/A if ((fread(slk->_ldis[i], sizeof (char),
2N/A lablen, filep) != lablen) ||
2N/A (fread(slk->_lval[i],
2N/A sizeof (char), lablen, filep) != lablen)) {
2N/A goto err;
2N/A }
2N/A }
2N/A (*_do_slk_tch)();
2N/A } else {
2N/A if (fseek(filep, (long) (2 * labmax * lablen *
2N/A sizeof (char)), 1) != 0)
2N/A goto err;
2N/A }
2N/A }
2N/A
2N/A /* read the color information(if any) from the file */
2N/A
2N/A if (fread((char *) &magic, sizeof (int), 1, filep) != 1)
2N/A goto err;
2N/A
2N/A if (magic) {
2N/A int colors, color_pairs;
2N/A bool could_change;
2N/A int i;
2N/A
2N/A /* if the new terminal doesn't support colors, or it supports */
2N/A /* less colors (or color_pairs) than the old terminal, or */
2N/A /* start_color() has not been called, simply advance the file */
2N/A /* pointer pass the color related info. */
2N/A /* Note: must to read the first line of color info, even if the */
2N/A /* new terminal doesn't support color, in order to know how to */
2N/A /* deal with the rest of the file */
2N/A
2N/A if ((fread((char *) &colors, sizeof (int), 1, filep) != 1) ||
2N/A (fread((char *) &color_pairs, sizeof (int), 1,
2N/A filep) != 1) || (fread((char *) &could_change,
2N/A sizeof (char), 1, filep) != 1))
2N/A goto err;
2N/A
2N/A if (max_pairs == -1 || cur_term->_pairs_tbl == NULL ||
2N/A colors > max_colors || color_pairs > max_pairs) {
2N/A if (fseek(filep, (long) (colors * sizeof (_Color) +
2N/A color_pairs * sizeof (_Color_pair)), 1) != 0)
2N/A goto err;
2N/A } else {
2N/A _Color_pair *ptp, *save_ptp;
2N/A
2N/A /* if both old and new terminals could modify colors, read in */
2N/A /* color table, and call init_color for each color */
2N/A
2N/A if (could_change) {
2N/A if (can_change) {
2N/A _Color *ctp, *save_ctp;
2N/A
2N/A if ((save_ctp = (ctp = (_Color *)
2N/A malloc(colors *
2N/A sizeof (_Color)))) == NULL)
2N/A goto err;
2N/A
2N/A if (fread(ctp, sizeof (_Color),
2N/A colors, filep) != colors)
2N/A goto err;
2N/A
2N/A for (i = 0; i < colors; i++, ctp++) {
2N/A /* LINTED */
2N/A (void) init_color((short)i,
2N/A ctp->r, ctp->g, ctp->b);
2N/A }
2N/A free(save_ctp);
2N/A } else {
2N/A
2N/A /* the old terminal could modify colors, by the new one */
2N/A /* cannot skip over color_table info. */
2N/A
2N/A if (fseek(filep, (long) (colors *
2N/A sizeof (_Color)), 1) != 0)
2N/A goto err;
2N/A }
2N/A }
2N/A
2N/A /* read color_pairs info. call init_pair for each pair */
2N/A
2N/A if ((save_ptp = (ptp = (_Color_pair *)
2N/A malloc(color_pairs * sizeof (_Color_pair)))) ==
2N/A NULL)
2N/A goto err;
2N/A if (fread(ptp, sizeof (_Color_pair), color_pairs,
2N/A filep) != color_pairs) {
2N/Aerr:
2N/A if (win != NULL)
2N/A (void) delwin(win);
2N/A if (win1 != NULL)
2N/A (void) delwin(win1);
2N/A if (type == 0)
2N/A curscr->_clear = TRUE;
2N/A return (ERR);
2N/A }
2N/A
2N/A for (i = 1, ++ptp; i <= color_pairs; i++, ptp++) {
2N/A if (ptp->init)
2N/A /* LINTED */
2N/A (void) init_pair((short)i,
2N/A ptp->foreground, ptp->background);
2N/A }
2N/A free(save_ptp);
2N/A }
2N/A }
2N/A
2N/A /* substitute read in window for the curscr */
2N/A switch (type) {
2N/A case 1:
2N/A case 2:
2N/A (void) delwin(_virtscr);
2N/A hash = _VIRTHASH;
2N/A if (type == 1) {
2N/A SP->virt_scr = _virtscr = win;
2N/A _VIRTTOP = 0;
2N/A _VIRTBOT = curscr->_maxy - 1;
2N/A break;
2N/A }
2N/A SP->virt_scr = _virtscr = win1;
2N/A _VIRTTOP = curscr->_maxy;
2N/A _VIRTBOT = -1;
2N/A /* clear the hash table */
2N/A for (y = curscr->_maxy; y > 0; --y)
2N/A *hash++ = _NOHASH;
2N/A /* LINTED */ /* Known fall-through on case statement. */
2N/A case 0:
2N/A {
2N/A int saveflag = curscr->_flags & _CANT_BE_IMMED;
2N/A
2N/A (void) delwin(curscr);
2N/A SP->cur_scr = curscr = win;
2N/A curscr->_sync = TRUE;
2N/A curscr->_flags |= saveflag;
2N/A hash = _CURHASH;
2N/A }
2N/A }
2N/A
2N/A /* clear the hash table */
2N/A for (y = curscr->_maxy; y > 0; --y)
2N/A *hash++ = _NOHASH;
2N/A
2N/A curscr->_clear = clearit;
2N/A return (OK);
2N/A}