1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright (c) 1995, by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29/*
30 * getwin.c
31 *
32 * XCurses Library
33 *
34 * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35 *
36 */
37
38#if M_RCSID
39#ifndef lint
40static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/getwin.c 1.2 1995/06/12 17:48:38 ant Exp $";
41#endif
42#endif
43
44#include <private.h>
45#include <limits.h>
46
47static int
48get_cc(w, mbs, fp)
49WINDOW *w;
50char *mbs;
51FILE *fp;
52{
53	short co;
54	attr_t at;
55	int i, n, y, x;
56
57	if (fscanf(fp, "%d,%d,%hx,%hd,", &y, &x, &at, &co) < 4)
58		return 0;
59
60	if (fscanf(fp, "%[^\n]%n ", mbs, &n) < 1)
61		return 0;
62
63	if (wattr_set(w, at, co, (void *) 0) == ERR)
64		return 0;
65
66	if (mvwaddstr(w, y, x, mbs) == ERR)
67		return 0;
68
69	(void) wstandend(w);
70
71	return n;
72}
73
74WINDOW *
75getwin(fp)
76FILE *fp;
77{
78	char *mbs;
79	WINDOW *w;
80	short flags;
81	int by, bx, my, mx;
82
83#ifdef M_CURSES_TRACE
84	__m_trace("getwin(%p)", fp);
85#endif
86
87	/* Get window dimensions and location to create a new window. */
88	if (fscanf(fp, "MAX=%d,%d BEG=%d,%d ", &my, &mx, &by, &bx) < 4)
89		goto error1;
90
91	if ((mbs = (char *) malloc((size_t) (LINE_MAX+1))) == (char *) 0)
92		goto error1;
93
94	if ((w = newwin(my, mx, by, bx)) == (WINDOW *) 0)
95		goto error2;
96
97	/* Read other window attributes. */
98	by = fscanf(
99		fp, "SCROLL=%hd,%hd VMIN=%hd VTIME=%hd FLAGS=%hx FG=%hx,%hd ",
100		&w->_top, &w->_bottom, &w->_vmin, &w->_vtime, &flags,
101		&w->_fg._at, &w->_fg._co
102	);
103	if (by < 7)
104		goto error3;
105
106	w->_flags &= ~W_CONFIG_MASK;
107	w->_flags |= flags;
108
109	by = fscanf( fp, "BG=%hx,%hd,%[^\n] ", &w->_bg._at, &w->_bg._co, mbs);
110	if (by < 3)
111		goto error3;
112
113	while (get_cc(w, mbs, fp))
114		;
115
116	if (fscanf(fp, "CUR=%hd,%hd", &w->_cury, &w->_curx) < 2)
117		goto error3;
118
119	free(mbs);
120
121	return __m_return_pointer("getwin", w);
122error3:
123	(void) delwin(w);
124error2:
125	free(mbs);
126error1:
127	rewind(fp);
128
129	return __m_return_pointer("getwin", (WINDOW *) 0);
130}
131
132static int
133put_cc(w, y, x, mbs, len, fp)
134WINDOW *w;
135int y, x;
136char *mbs;
137int len;
138FILE *fp;
139{
140	int i;
141	short co;
142	attr_t at;
143
144	at = w->_line[y][x]._at;
145	co = w->_line[y][x]._co;
146
147	/* Write first character as a multibyte string. */
148	(void) __m_cc_mbs(&w->_line[y][x], mbs, len);
149
150	/* Write additional characters with same colour and attributes. */
151	for (i = x;;) {
152		i = __m_cc_next(w, y, i);
153		if (w->_maxx <= i)
154			break;
155		if (w->_line[y][i]._at != at || w->_line[y][i]._co != co)
156			break;
157		(void) __m_cc_mbs(&w->_line[y][i], mbs, 0);
158	}
159
160	/* Terminate string. */
161	(void) __m_cc_mbs((const cchar_t *) 0, (char *) 0, 0);
162
163	(void) fprintf(fp, "%d,%d,%#x,%d,%s\n", y, x, at, co, mbs);
164
165	/* Return index of next unprocessed column. */
166	return i;
167}
168
169int
170putwin(w, fp)
171WINDOW *w;
172FILE *fp;
173{
174	char *mbs;
175	int y, x, mbs_len;
176
177#ifdef M_CURSES_TRACE
178	__m_trace("putwin(%p, %p)", w, fp);
179#endif
180
181	mbs_len = columns * M_CCHAR_MAX * MB_LEN_MAX * sizeof *mbs + 1;
182	if ((mbs = (char *) malloc((size_t) mbs_len)) == (char *) 0)
183		return __m_return_code("putwin", ERR);
184
185	(void) fprintf(
186		fp, "MAX=%d,%d\nBEG=%d,%d\nSCROLL=%d,%d\n",
187		w->_maxy, w->_maxx, w->_begy, w->_begx, w->_top, w->_bottom
188	);
189	(void) fprintf(
190		fp, "VMIN=%d\nVTIME=%d\nFLAGS=%#x\nFG=%#x,%d\n",
191		w->_vmin, w->_vtime, w->_flags & W_CONFIG_MASK,
192		w->_fg._at, w->_fg._co
193	);
194
195	(void) __m_cc_mbs(&w->_bg, mbs, mbs_len);
196	(void) __m_cc_mbs((const cchar_t *) 0, (char *) 0, 0);
197	(void) fprintf(fp, "BG=%#x,%d,%s\n", w->_bg._at, w->_bg._co, mbs);
198
199	for (y = 0; y < w->_maxy; ++y) {
200		for (x = 0; x < w->_maxx; )
201			x = put_cc(w, y, x, mbs, mbs_len, fp);
202	}
203
204	(void) fprintf(fp, "CUR=%d,%d\n", w->_curx, w->_cury);
205
206	free(mbs);
207
208	return __m_return_code("putwin", OK);
209}
210
211