1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate *
35*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate * contributors.
38*7c478bd9Sstevel@tonic-gate */
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #include "curses_inc.h"
45*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
47*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate * Initialize the screen image to be the image contained
51*7c478bd9Sstevel@tonic-gate * in the given file. This is usually used in a child process
52*7c478bd9Sstevel@tonic-gate * to initialize its idea of the screen image to be that of its
53*7c478bd9Sstevel@tonic-gate * parent.
54*7c478bd9Sstevel@tonic-gate *
55*7c478bd9Sstevel@tonic-gate * filep: pointer to the output stream
56*7c478bd9Sstevel@tonic-gate * type: 0: <curses> should assume that the physical screen is
57*7c478bd9Sstevel@tonic-gate * EXACTLY as stored in the file. Therefore, we take
58*7c478bd9Sstevel@tonic-gate * special care to make sure that /dev/tty and the terminal
59*7c478bd9Sstevel@tonic-gate * did not change in any way. This information can then
60*7c478bd9Sstevel@tonic-gate * be used in the update optimization of the new program
61*7c478bd9Sstevel@tonic-gate * so that the screen does not have to be cleared. Instead,
62*7c478bd9Sstevel@tonic-gate * curses, by knowing what's on the screen can optimally
63*7c478bd9Sstevel@tonic-gate * update it with the information of the new program.
64*7c478bd9Sstevel@tonic-gate *
65*7c478bd9Sstevel@tonic-gate * 1: Tell <curses> that the stored image should be
66*7c478bd9Sstevel@tonic-gate * the physical image. Sort of like a huge waddstr onto
67*7c478bd9Sstevel@tonic-gate * curscr. This can be used when a library wants to save
68*7c478bd9Sstevel@tonic-gate * a screen image and restore it at a later time.
69*7c478bd9Sstevel@tonic-gate *
70*7c478bd9Sstevel@tonic-gate * 2: Tell <curses> that the stored image is the physical
71*7c478bd9Sstevel@tonic-gate * image and also it is what the new program wants on the
72*7c478bd9Sstevel@tonic-gate * screen. This can be be thought of as a screen inheritance
73*7c478bd9Sstevel@tonic-gate * function.
74*7c478bd9Sstevel@tonic-gate */
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate int
scr_reset(FILE * filep,int type)77*7c478bd9Sstevel@tonic-gate scr_reset(FILE *filep, int type)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate WINDOW *win = NULL, *win1 = NULL;
80*7c478bd9Sstevel@tonic-gate int *hash, y;
81*7c478bd9Sstevel@tonic-gate char clearit = FALSE;
82*7c478bd9Sstevel@tonic-gate short magic;
83*7c478bd9Sstevel@tonic-gate struct stat statbuf;
84*7c478bd9Sstevel@tonic-gate time_t ttytime;
85*7c478bd9Sstevel@tonic-gate
86*7c478bd9Sstevel@tonic-gate if (type != 1 && exit_ca_mode && *exit_ca_mode && non_rev_rmcup) {
87*7c478bd9Sstevel@tonic-gate if (type == 0)
88*7c478bd9Sstevel@tonic-gate goto err;
89*7c478bd9Sstevel@tonic-gate else {
90*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
91*7c478bd9Sstevel@tonic-gate if (outf)
92*7c478bd9Sstevel@tonic-gate fprintf(outf, "clear it because of "
93*7c478bd9Sstevel@tonic-gate "exit_ca_mode\n");
94*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
95*7c478bd9Sstevel@tonic-gate clearit = TRUE;
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate /* check magic number */
100*7c478bd9Sstevel@tonic-gate if (fread((char *) &magic, sizeof (short), 1, filep) != 1)
101*7c478bd9Sstevel@tonic-gate goto err;
102*7c478bd9Sstevel@tonic-gate if (magic != SVR3_DUMP_MAGIC_NUMBER)
103*7c478bd9Sstevel@tonic-gate goto err;
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate /* get modification time of image in file */
106*7c478bd9Sstevel@tonic-gate if (fread((char *) &ttytime, sizeof (time_t), 1, filep) != 1)
107*7c478bd9Sstevel@tonic-gate goto err;
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate if ((type != 1) && ((ttyname(cur_term->Filedes) == NULL) ||
110*7c478bd9Sstevel@tonic-gate (fstat(cur_term->Filedes, &statbuf) < 0) ||
111*7c478bd9Sstevel@tonic-gate (statbuf.st_mtime != ttytime))) {
112*7c478bd9Sstevel@tonic-gate if (type == 0)
113*7c478bd9Sstevel@tonic-gate goto err;
114*7c478bd9Sstevel@tonic-gate else {
115*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
116*7c478bd9Sstevel@tonic-gate if (outf)
117*7c478bd9Sstevel@tonic-gate fprintf(outf, "Filedes = %hd, "
118*7c478bd9Sstevel@tonic-gate "statbuf.st_mtime = %d, "
119*7c478bd9Sstevel@tonic-gate "ttytime = %d\n", cur_term->Filedes,
120*7c478bd9Sstevel@tonic-gate statbuf.st_mtime, ttytime);
121*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
122*7c478bd9Sstevel@tonic-gate clearit = TRUE;
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate /* if get here, everything is ok, read the curscr image */
127*7c478bd9Sstevel@tonic-gate if (((win = getwin(filep)) == NULL) ||
128*7c478bd9Sstevel@tonic-gate ((type == 2) && ((win1 = dupwin(win)) == NULL)) ||
129*7c478bd9Sstevel@tonic-gate (win->_maxy != curscr->_maxy) || (win->_maxx != curscr->_maxx) ||
130*7c478bd9Sstevel@tonic-gate /* soft labels */
131*7c478bd9Sstevel@tonic-gate (fread((char *) &magic, sizeof (int), 1, filep) != 1))
132*7c478bd9Sstevel@tonic-gate goto err;
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate * if soft labels were dumped, we would like either read them
136*7c478bd9Sstevel@tonic-gate * or advance the file pointer pass them
137*7c478bd9Sstevel@tonic-gate */
138*7c478bd9Sstevel@tonic-gate if (magic) {
139*7c478bd9Sstevel@tonic-gate short i, labmax, lablen;
140*7c478bd9Sstevel@tonic-gate SLK_MAP *slk = SP->slk;
141*7c478bd9Sstevel@tonic-gate /*
142*7c478bd9Sstevel@tonic-gate * Why doesn't the following line and the two below
143*7c478bd9Sstevel@tonic-gate * that access those variables work ?
144*7c478bd9Sstevel@tonic-gate */
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate * char **labdis = SP->slk->_ldis, **labval = SP->slk->_lval;
147*7c478bd9Sstevel@tonic-gate */
148*7c478bd9Sstevel@tonic-gate
149*7c478bd9Sstevel@tonic-gate if ((fread((char *) &labmax, sizeof (short), 1, filep) != 1) ||
150*7c478bd9Sstevel@tonic-gate (fread((char *) &lablen, sizeof (short), 1, filep) != 1)) {
151*7c478bd9Sstevel@tonic-gate goto err;
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate if (slk != NULL) {
155*7c478bd9Sstevel@tonic-gate if ((labmax != slk->_num) ||
156*7c478bd9Sstevel@tonic-gate (lablen != (slk->_len + 1)))
157*7c478bd9Sstevel@tonic-gate goto err;
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate for (i = 0; i < labmax; i++) {
160*7c478bd9Sstevel@tonic-gate /*
161*7c478bd9Sstevel@tonic-gate * if ((fread(labdis[i], sizeof (char), lablen,
162*7c478bd9Sstevel@tonic-gate * filep) != lablen) ||
163*7c478bd9Sstevel@tonic-gate * (fread(labval[i], sizeof (char), lablen,
164*7c478bd9Sstevel@tonic-gate * filep != lablen))
165*7c478bd9Sstevel@tonic-gate */
166*7c478bd9Sstevel@tonic-gate if ((fread(slk->_ldis[i], sizeof (char),
167*7c478bd9Sstevel@tonic-gate lablen, filep) != lablen) ||
168*7c478bd9Sstevel@tonic-gate (fread(slk->_lval[i],
169*7c478bd9Sstevel@tonic-gate sizeof (char), lablen, filep) != lablen)) {
170*7c478bd9Sstevel@tonic-gate goto err;
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate (*_do_slk_tch)();
174*7c478bd9Sstevel@tonic-gate } else {
175*7c478bd9Sstevel@tonic-gate if (fseek(filep, (long) (2 * labmax * lablen *
176*7c478bd9Sstevel@tonic-gate sizeof (char)), 1) != 0)
177*7c478bd9Sstevel@tonic-gate goto err;
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate /* read the color information(if any) from the file */
182*7c478bd9Sstevel@tonic-gate
183*7c478bd9Sstevel@tonic-gate if (fread((char *) &magic, sizeof (int), 1, filep) != 1)
184*7c478bd9Sstevel@tonic-gate goto err;
185*7c478bd9Sstevel@tonic-gate
186*7c478bd9Sstevel@tonic-gate if (magic) {
187*7c478bd9Sstevel@tonic-gate int colors, color_pairs;
188*7c478bd9Sstevel@tonic-gate bool could_change;
189*7c478bd9Sstevel@tonic-gate int i;
190*7c478bd9Sstevel@tonic-gate
191*7c478bd9Sstevel@tonic-gate /* if the new terminal doesn't support colors, or it supports */
192*7c478bd9Sstevel@tonic-gate /* less colors (or color_pairs) than the old terminal, or */
193*7c478bd9Sstevel@tonic-gate /* start_color() has not been called, simply advance the file */
194*7c478bd9Sstevel@tonic-gate /* pointer pass the color related info. */
195*7c478bd9Sstevel@tonic-gate /* Note: must to read the first line of color info, even if the */
196*7c478bd9Sstevel@tonic-gate /* new terminal doesn't support color, in order to know how to */
197*7c478bd9Sstevel@tonic-gate /* deal with the rest of the file */
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate if ((fread((char *) &colors, sizeof (int), 1, filep) != 1) ||
200*7c478bd9Sstevel@tonic-gate (fread((char *) &color_pairs, sizeof (int), 1,
201*7c478bd9Sstevel@tonic-gate filep) != 1) || (fread((char *) &could_change,
202*7c478bd9Sstevel@tonic-gate sizeof (char), 1, filep) != 1))
203*7c478bd9Sstevel@tonic-gate goto err;
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate if (max_pairs == -1 || cur_term->_pairs_tbl == NULL ||
206*7c478bd9Sstevel@tonic-gate colors > max_colors || color_pairs > max_pairs) {
207*7c478bd9Sstevel@tonic-gate if (fseek(filep, (long) (colors * sizeof (_Color) +
208*7c478bd9Sstevel@tonic-gate color_pairs * sizeof (_Color_pair)), 1) != 0)
209*7c478bd9Sstevel@tonic-gate goto err;
210*7c478bd9Sstevel@tonic-gate } else {
211*7c478bd9Sstevel@tonic-gate _Color_pair *ptp, *save_ptp;
212*7c478bd9Sstevel@tonic-gate
213*7c478bd9Sstevel@tonic-gate /* if both old and new terminals could modify colors, read in */
214*7c478bd9Sstevel@tonic-gate /* color table, and call init_color for each color */
215*7c478bd9Sstevel@tonic-gate
216*7c478bd9Sstevel@tonic-gate if (could_change) {
217*7c478bd9Sstevel@tonic-gate if (can_change) {
218*7c478bd9Sstevel@tonic-gate _Color *ctp, *save_ctp;
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gate if ((save_ctp = (ctp = (_Color *)
221*7c478bd9Sstevel@tonic-gate malloc(colors *
222*7c478bd9Sstevel@tonic-gate sizeof (_Color)))) == NULL)
223*7c478bd9Sstevel@tonic-gate goto err;
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate if (fread(ctp, sizeof (_Color),
226*7c478bd9Sstevel@tonic-gate colors, filep) != colors)
227*7c478bd9Sstevel@tonic-gate goto err;
228*7c478bd9Sstevel@tonic-gate
229*7c478bd9Sstevel@tonic-gate for (i = 0; i < colors; i++, ctp++) {
230*7c478bd9Sstevel@tonic-gate /* LINTED */
231*7c478bd9Sstevel@tonic-gate (void) init_color((short)i,
232*7c478bd9Sstevel@tonic-gate ctp->r, ctp->g, ctp->b);
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate free(save_ctp);
235*7c478bd9Sstevel@tonic-gate } else {
236*7c478bd9Sstevel@tonic-gate
237*7c478bd9Sstevel@tonic-gate /* the old terminal could modify colors, by the new one */
238*7c478bd9Sstevel@tonic-gate /* cannot skip over color_table info. */
239*7c478bd9Sstevel@tonic-gate
240*7c478bd9Sstevel@tonic-gate if (fseek(filep, (long) (colors *
241*7c478bd9Sstevel@tonic-gate sizeof (_Color)), 1) != 0)
242*7c478bd9Sstevel@tonic-gate goto err;
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate }
245*7c478bd9Sstevel@tonic-gate
246*7c478bd9Sstevel@tonic-gate /* read color_pairs info. call init_pair for each pair */
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate if ((save_ptp = (ptp = (_Color_pair *)
249*7c478bd9Sstevel@tonic-gate malloc(color_pairs * sizeof (_Color_pair)))) ==
250*7c478bd9Sstevel@tonic-gate NULL)
251*7c478bd9Sstevel@tonic-gate goto err;
252*7c478bd9Sstevel@tonic-gate if (fread(ptp, sizeof (_Color_pair), color_pairs,
253*7c478bd9Sstevel@tonic-gate filep) != color_pairs) {
254*7c478bd9Sstevel@tonic-gate err:
255*7c478bd9Sstevel@tonic-gate if (win != NULL)
256*7c478bd9Sstevel@tonic-gate (void) delwin(win);
257*7c478bd9Sstevel@tonic-gate if (win1 != NULL)
258*7c478bd9Sstevel@tonic-gate (void) delwin(win1);
259*7c478bd9Sstevel@tonic-gate if (type == 0)
260*7c478bd9Sstevel@tonic-gate curscr->_clear = TRUE;
261*7c478bd9Sstevel@tonic-gate return (ERR);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate
264*7c478bd9Sstevel@tonic-gate for (i = 1, ++ptp; i <= color_pairs; i++, ptp++) {
265*7c478bd9Sstevel@tonic-gate if (ptp->init)
266*7c478bd9Sstevel@tonic-gate /* LINTED */
267*7c478bd9Sstevel@tonic-gate (void) init_pair((short)i,
268*7c478bd9Sstevel@tonic-gate ptp->foreground, ptp->background);
269*7c478bd9Sstevel@tonic-gate }
270*7c478bd9Sstevel@tonic-gate free(save_ptp);
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate }
273*7c478bd9Sstevel@tonic-gate
274*7c478bd9Sstevel@tonic-gate /* substitute read in window for the curscr */
275*7c478bd9Sstevel@tonic-gate switch (type) {
276*7c478bd9Sstevel@tonic-gate case 1:
277*7c478bd9Sstevel@tonic-gate case 2:
278*7c478bd9Sstevel@tonic-gate (void) delwin(_virtscr);
279*7c478bd9Sstevel@tonic-gate hash = _VIRTHASH;
280*7c478bd9Sstevel@tonic-gate if (type == 1) {
281*7c478bd9Sstevel@tonic-gate SP->virt_scr = _virtscr = win;
282*7c478bd9Sstevel@tonic-gate _VIRTTOP = 0;
283*7c478bd9Sstevel@tonic-gate _VIRTBOT = curscr->_maxy - 1;
284*7c478bd9Sstevel@tonic-gate break;
285*7c478bd9Sstevel@tonic-gate }
286*7c478bd9Sstevel@tonic-gate SP->virt_scr = _virtscr = win1;
287*7c478bd9Sstevel@tonic-gate _VIRTTOP = curscr->_maxy;
288*7c478bd9Sstevel@tonic-gate _VIRTBOT = -1;
289*7c478bd9Sstevel@tonic-gate /* clear the hash table */
290*7c478bd9Sstevel@tonic-gate for (y = curscr->_maxy; y > 0; --y)
291*7c478bd9Sstevel@tonic-gate *hash++ = _NOHASH;
292*7c478bd9Sstevel@tonic-gate /* LINTED */ /* Known fall-through on case statement. */
293*7c478bd9Sstevel@tonic-gate case 0:
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate int saveflag = curscr->_flags & _CANT_BE_IMMED;
296*7c478bd9Sstevel@tonic-gate
297*7c478bd9Sstevel@tonic-gate (void) delwin(curscr);
298*7c478bd9Sstevel@tonic-gate SP->cur_scr = curscr = win;
299*7c478bd9Sstevel@tonic-gate curscr->_sync = TRUE;
300*7c478bd9Sstevel@tonic-gate curscr->_flags |= saveflag;
301*7c478bd9Sstevel@tonic-gate hash = _CURHASH;
302*7c478bd9Sstevel@tonic-gate }
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate
305*7c478bd9Sstevel@tonic-gate /* clear the hash table */
306*7c478bd9Sstevel@tonic-gate for (y = curscr->_maxy; y > 0; --y)
307*7c478bd9Sstevel@tonic-gate *hash++ = _NOHASH;
308*7c478bd9Sstevel@tonic-gate
309*7c478bd9Sstevel@tonic-gate curscr->_clear = clearit;
310*7c478bd9Sstevel@tonic-gate return (OK);
311*7c478bd9Sstevel@tonic-gate }
312