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