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-1998 by Sun Microsystems, Inc.
24 * All rights reserved.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29/* LINTLIBRARY */
30
31/*
32 * getwin.c
33 *
34 * XCurses Library
35 *
36 * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
37 *
38 */
39
40#if M_RCSID
41#ifndef lint
42static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/getwin.c 1.2 "
43"1995/06/12 17:48:38 ant Exp $";
44#endif
45#endif
46
47#include <private.h>
48#include <limits.h>
49
50#undef mvwaddstr
51#undef wstandend
52
53static int
54get_cc(WINDOW *w, char *mbs, FILE *fp)
55{
56	short	co;
57	attr_t	at;
58	int	n, y, x;
59
60	if (fscanf(fp, "%d,%d,%hx,%hd,", &y, &x, &at, &co) < 4)
61		return (0);
62
63	if (fscanf(fp, "%[^\n]%n ", mbs, &n) < 1)
64		return (0);
65
66	if (wattr_set(w, at, co, (void *) 0) == ERR)
67		return (0);
68
69	if (mvwaddstr(w, y, x, mbs) == ERR)
70		return (0);
71
72	(void) wstandend(w);
73
74	return (n);
75}
76
77WINDOW *
78getwin(FILE *fp)
79{
80	char	*mbs;
81	WINDOW	*w;
82	unsigned short	flags;
83	int	by, bx, my, mx;
84
85	/* Get window dimensions and location to create a new window. */
86	if (fscanf(fp, "MAX=%d,%d BEG=%d,%d ", &my, &mx, &by, &bx) < 4)
87		goto error1;
88
89	if ((mbs = (char *) malloc((size_t) (LINE_MAX+1))) == NULL)
90		goto error1;
91
92	if ((w = newwin(my, mx, by, bx)) == NULL)
93		goto error2;
94
95	/* Read other window attributes. */
96	by = fscanf(fp,
97		"SCROLL=%hd,%hd VMIN=%hd VTIME=%hd FLAGS=%hx FG=%hx,%hd ",
98		&w->_top, &w->_bottom, &w->_vmin, &w->_vtime, &flags,
99		&w->_fg._at, &w->_fg._co);
100	if (by < 7)
101		goto error3;
102
103	w->_flags &= ~W_CONFIG_MASK;
104	w->_flags |= flags;
105
106	by = fscanf(fp, "BG=%hx,%hd,%[^\n] ", &w->_bg._at, &w->_bg._co, mbs);
107	if (by < 3)
108		goto error3;
109
110	while (get_cc(w, mbs, fp))
111		;
112
113	if (fscanf(fp, "CUR=%hd,%hd", &w->_cury, &w->_curx) < 2)
114		goto error3;
115
116	free(mbs);
117
118	return (w);
119error3:
120	(void) delwin(w);
121error2:
122	free(mbs);
123error1:
124	rewind(fp);
125
126	return (NULL);
127}
128
129static int
130put_cc(WINDOW *w, int y, int x,
131	char *mbs, int len, FILE *fp)
132{
133	int	i;
134	short	co;
135	attr_t	at;
136
137	at = w->_line[y][x]._at;
138	co = w->_line[y][x]._co;
139
140	/* Write first character as a multibyte string. */
141	(void) __m_cc_mbs(&w->_line[y][x], mbs, len);
142
143	/* Write additional characters with same colour and attributes. */
144	for (i = x; ; ) {
145		i = __m_cc_next(w, y, i);
146		if (w->_maxx <= i)
147			break;
148		if (w->_line[y][i]._at != at || w->_line[y][i]._co != co)
149			break;
150		(void) __m_cc_mbs(&w->_line[y][i], mbs, 0);
151	}
152
153	/* Terminate string. */
154	(void) __m_cc_mbs((const cchar_t *) 0, (char *) 0, 0);
155
156	(void) fprintf(fp, "%d,%d,%#x,%d,%s\n", y, x, at, co, mbs);
157
158	/* Return index of next unprocessed column. */
159	return (i);
160}
161
162int
163putwin(WINDOW *w, FILE *fp)
164{
165	char	*mbs;
166	size_t	mbs_len;
167	int	y, x;
168
169	mbs_len = columns * _M_CCHAR_MAX * MB_LEN_MAX * sizeof (*mbs) + 1;
170	if ((mbs = (char *) malloc((size_t) mbs_len)) == (char *) 0)
171		return (ERR);
172
173	(void) fprintf(fp,
174		"MAX=%d,%d\nBEG=%d,%d\nSCROLL=%d,%d\n",
175		w->_maxy, w->_maxx, w->_begy, w->_begx, w->_top, w->_bottom);
176	(void) fprintf(fp,
177		"VMIN=%d\nVTIME=%d\nFLAGS=%#x\nFG=%#x,%d\n",
178		w->_vmin, w->_vtime, w->_flags & W_CONFIG_MASK,
179		w->_fg._at, w->_fg._co);
180
181	(void) __m_cc_mbs(&w->_bg, mbs, (int)mbs_len);
182	(void) __m_cc_mbs((const cchar_t *) 0, (char *) 0, 0);
183	(void) fprintf(fp, "BG=%#x,%d,%s\n", w->_bg._at, w->_bg._co, mbs);
184
185	for (y = 0; y < w->_maxy; ++y) {
186		for (x = 0; x < w->_maxx; )
187			x = put_cc(w, y, x, mbs, (int)mbs_len, fp);
188	}
189
190	(void) fprintf(fp, "CUR=%d,%d\n", w->_curx, w->_cury);
191
192	free(mbs);
193
194	return (OK);
195}
196