xref: /minix3/lib/libcurses/fileio.c (revision 51ffecc181005cb45a40108612ee28d1daaeeb86)
1*51ffecc1SBen Gras /*	$NetBSD: fileio.c,v 1.4 2009/07/22 16:57:14 roy Exp $	*/
2*51ffecc1SBen Gras 
3*51ffecc1SBen Gras /*-
4*51ffecc1SBen Gras  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*51ffecc1SBen Gras  * All rights reserved.
6*51ffecc1SBen Gras  *
7*51ffecc1SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
8*51ffecc1SBen Gras  * by Julian Coleman.
9*51ffecc1SBen Gras  *
10*51ffecc1SBen Gras  * Redistribution and use in source and binary forms, with or without
11*51ffecc1SBen Gras  * modification, are permitted provided that the following conditions
12*51ffecc1SBen Gras  * are met:
13*51ffecc1SBen Gras  * 1. Redistributions of source code must retain the above copyright
14*51ffecc1SBen Gras  *    notice, this list of conditions and the following disclaimer.
15*51ffecc1SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
16*51ffecc1SBen Gras  *    notice, this list of conditions and the following disclaimer in the
17*51ffecc1SBen Gras  *    documentation and/or other materials provided with the distribution.
18*51ffecc1SBen Gras  *
19*51ffecc1SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*51ffecc1SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*51ffecc1SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*51ffecc1SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*51ffecc1SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*51ffecc1SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*51ffecc1SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*51ffecc1SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*51ffecc1SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*51ffecc1SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*51ffecc1SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
30*51ffecc1SBen Gras  */
31*51ffecc1SBen Gras 
32*51ffecc1SBen Gras #include <sys/cdefs.h>
33*51ffecc1SBen Gras #ifndef lint
34*51ffecc1SBen Gras __RCSID("$NetBSD: fileio.c,v 1.4 2009/07/22 16:57:14 roy Exp $");
35*51ffecc1SBen Gras #endif				/* not lint */
36*51ffecc1SBen Gras 
37*51ffecc1SBen Gras #include "curses.h"
38*51ffecc1SBen Gras #include "curses_private.h"
39*51ffecc1SBen Gras #include "fileio.h"
40*51ffecc1SBen Gras #include <stdio.h>
41*51ffecc1SBen Gras #include <stdlib.h>
42*51ffecc1SBen Gras 
43*51ffecc1SBen Gras #ifdef HAVE_WCHAR
44*51ffecc1SBen Gras static int __putnsp(nschar_t *, FILE *);
45*51ffecc1SBen Gras static int __getnsp(nschar_t *, FILE *);
46*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
47*51ffecc1SBen Gras 
48*51ffecc1SBen Gras #ifdef HAVE_WCHAR
49*51ffecc1SBen Gras /*
50*51ffecc1SBen Gras  * __putnsp --
51*51ffecc1SBen Gras  *	Write non-spacing character chain to file, consisting of:
52*51ffecc1SBen Gras  *	((int) 1, (wchar_t) ch) pairs followed by (int) 0.
53*51ffecc1SBen Gras  */
54*51ffecc1SBen Gras static int
__putnsp(nschar_t * nsp,FILE * fp)55*51ffecc1SBen Gras __putnsp(nschar_t *nsp, FILE *fp)
56*51ffecc1SBen Gras {
57*51ffecc1SBen Gras 	int n;
58*51ffecc1SBen Gras 
59*51ffecc1SBen Gras 	n = 1;
60*51ffecc1SBen Gras 	while (nsp != NULL) {
61*51ffecc1SBen Gras 		if (fwrite(&n, sizeof(int), 1, fp) != 1)
62*51ffecc1SBen Gras 			return ERR;
63*51ffecc1SBen Gras 		if (fwrite(&nsp->ch, sizeof(wchar_t), 1, fp) != 1)
64*51ffecc1SBen Gras 			return ERR;
65*51ffecc1SBen Gras 	}
66*51ffecc1SBen Gras 	n = 0;
67*51ffecc1SBen Gras 	if (fwrite(&n, sizeof(int), 1, fp) != 1)
68*51ffecc1SBen Gras 		return ERR;
69*51ffecc1SBen Gras 
70*51ffecc1SBen Gras 	return OK;
71*51ffecc1SBen Gras }
72*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
73*51ffecc1SBen Gras 
74*51ffecc1SBen Gras /*
75*51ffecc1SBen Gras  * putwin --
76*51ffecc1SBen Gras  *	Write window data to file
77*51ffecc1SBen Gras  */
78*51ffecc1SBen Gras int
putwin(WINDOW * win,FILE * fp)79*51ffecc1SBen Gras putwin(WINDOW *win, FILE *fp)
80*51ffecc1SBen Gras {
81*51ffecc1SBen Gras 	int major = CURSES_LIB_MAJOR;
82*51ffecc1SBen Gras 	int minor = CURSES_LIB_MINOR;
83*51ffecc1SBen Gras 	int y, x;
84*51ffecc1SBen Gras 	__LDATA *sp;
85*51ffecc1SBen Gras 
86*51ffecc1SBen Gras #ifdef DEBUG
87*51ffecc1SBen Gras 	__CTRACE(__CTRACE_FILEIO, "putwin: win %p\n", win);
88*51ffecc1SBen Gras #endif
89*51ffecc1SBen Gras 
90*51ffecc1SBen Gras 	if (win == NULL)
91*51ffecc1SBen Gras 		return ERR;
92*51ffecc1SBen Gras 
93*51ffecc1SBen Gras 	/* win can't be a subwin */
94*51ffecc1SBen Gras 	if (win->orig != NULL)
95*51ffecc1SBen Gras 		return ERR;
96*51ffecc1SBen Gras 
97*51ffecc1SBen Gras 	/* Library version */
98*51ffecc1SBen Gras 	if (fwrite(&major, sizeof(int), 1, fp) != 1)
99*51ffecc1SBen Gras 		return ERR;
100*51ffecc1SBen Gras 	if (fwrite(&minor, sizeof(int), 1, fp) != 1)
101*51ffecc1SBen Gras 		return ERR;
102*51ffecc1SBen Gras 
103*51ffecc1SBen Gras 	/* Window parameters */
104*51ffecc1SBen Gras 	if (fwrite(win, sizeof(WINDOW), 1, fp) != 1)
105*51ffecc1SBen Gras 		return ERR;
106*51ffecc1SBen Gras #ifdef HAVE_WCHAR
107*51ffecc1SBen Gras 	/* Background non-spacing character */
108*51ffecc1SBen Gras 	if (__putnsp(win->bnsp, fp) == ERR)
109*51ffecc1SBen Gras 		return ERR;
110*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
111*51ffecc1SBen Gras 
112*51ffecc1SBen Gras 	/* Lines and line data */
113*51ffecc1SBen Gras 	for (y = 0; y < win->maxy; y++)
114*51ffecc1SBen Gras 		for (sp = win->alines[y]->line, x = 0; x < win->maxx;
115*51ffecc1SBen Gras 		    x++, sp++) {
116*51ffecc1SBen Gras 			if (fwrite(&sp->ch, sizeof(wchar_t), 1, fp) != 1)
117*51ffecc1SBen Gras 				return ERR;
118*51ffecc1SBen Gras 			if (fwrite(&sp->attr, sizeof(attr_t), 1, fp) != 1)
119*51ffecc1SBen Gras 				return ERR;
120*51ffecc1SBen Gras #ifdef HAVE_WCHAR
121*51ffecc1SBen Gras 			if (sp->nsp != NULL) {
122*51ffecc1SBen Gras 				if (__putnsp(win->bnsp, fp) == ERR)
123*51ffecc1SBen Gras 					return ERR;
124*51ffecc1SBen Gras 			}
125*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
126*51ffecc1SBen Gras 		}
127*51ffecc1SBen Gras 
128*51ffecc1SBen Gras 	return OK;
129*51ffecc1SBen Gras }
130*51ffecc1SBen Gras 
131*51ffecc1SBen Gras #ifdef HAVE_WCHAR
132*51ffecc1SBen Gras /*
133*51ffecc1SBen Gras  * __getnsp --
134*51ffecc1SBen Gras  *	Read non-spacing character chain from file
135*51ffecc1SBen Gras  */
136*51ffecc1SBen Gras static int
__getnsp(nschar_t * nsp,FILE * fp)137*51ffecc1SBen Gras __getnsp(nschar_t *nsp, FILE *fp)
138*51ffecc1SBen Gras {
139*51ffecc1SBen Gras 	int n;
140*51ffecc1SBen Gras 	nschar_t *onsp, *tnsp;
141*51ffecc1SBen Gras 
142*51ffecc1SBen Gras 	if (fread(&n, sizeof(int), 1, fp) != 1)
143*51ffecc1SBen Gras 		return ERR;
144*51ffecc1SBen Gras 	onsp = nsp;
145*51ffecc1SBen Gras 	while (n != 0) {
146*51ffecc1SBen Gras 		tnsp = (nschar_t *)malloc(sizeof(nschar_t));
147*51ffecc1SBen Gras 		if (tnsp == NULL) {
148*51ffecc1SBen Gras 			__cursesi_free_nsp(nsp);
149*51ffecc1SBen Gras 			return OK;
150*51ffecc1SBen Gras 		}
151*51ffecc1SBen Gras 		if (fread(&tnsp->ch, sizeof(wchar_t), 1, fp) != 1) {
152*51ffecc1SBen Gras 			__cursesi_free_nsp(nsp);
153*51ffecc1SBen Gras 			return OK;
154*51ffecc1SBen Gras 		}
155*51ffecc1SBen Gras 		tnsp->next = NULL;
156*51ffecc1SBen Gras 		onsp->next = tnsp;
157*51ffecc1SBen Gras 		onsp = onsp->next;
158*51ffecc1SBen Gras 		if (fread(&n, sizeof(int), 1, fp) != 1) {
159*51ffecc1SBen Gras 			__cursesi_free_nsp(nsp);
160*51ffecc1SBen Gras 			return ERR;
161*51ffecc1SBen Gras 		}
162*51ffecc1SBen Gras 	}
163*51ffecc1SBen Gras 	return OK;
164*51ffecc1SBen Gras }
165*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
166*51ffecc1SBen Gras 
167*51ffecc1SBen Gras /*
168*51ffecc1SBen Gras  * getwin --
169*51ffecc1SBen Gras  *	Read window data from file
170*51ffecc1SBen Gras  */
171*51ffecc1SBen Gras WINDOW *
getwin(FILE * fp)172*51ffecc1SBen Gras getwin(FILE *fp)
173*51ffecc1SBen Gras {
174*51ffecc1SBen Gras 	int major, minor;
175*51ffecc1SBen Gras 	WINDOW *wtmp, *win;
176*51ffecc1SBen Gras 	int y, x;
177*51ffecc1SBen Gras 	__LDATA *sp;
178*51ffecc1SBen Gras 
179*51ffecc1SBen Gras #ifdef DEBUG
180*51ffecc1SBen Gras 	__CTRACE(__CTRACE_FILEIO, "getwin\n");
181*51ffecc1SBen Gras #endif
182*51ffecc1SBen Gras 
183*51ffecc1SBen Gras 	/* Check library version */
184*51ffecc1SBen Gras 	if (fread(&major, sizeof(int), 1, fp) != 1)
185*51ffecc1SBen Gras 		return NULL;
186*51ffecc1SBen Gras 	if (fread(&minor, sizeof(int), 1, fp) != 1)
187*51ffecc1SBen Gras 		return NULL;
188*51ffecc1SBen Gras 	if(major != CURSES_LIB_MAJOR || minor != CURSES_LIB_MINOR)
189*51ffecc1SBen Gras 		return NULL;
190*51ffecc1SBen Gras 
191*51ffecc1SBen Gras 	/* Window parameters */
192*51ffecc1SBen Gras 	wtmp = (WINDOW *)malloc(sizeof(WINDOW));
193*51ffecc1SBen Gras 	if (wtmp == NULL)
194*51ffecc1SBen Gras 		return NULL;
195*51ffecc1SBen Gras 	if (fread(wtmp, sizeof(WINDOW), 1, fp) != 1)
196*51ffecc1SBen Gras 		goto error0;
197*51ffecc1SBen Gras 	win = __newwin(_cursesi_screen, wtmp->maxy, wtmp->maxx,
198*51ffecc1SBen Gras 	    wtmp->begy, wtmp->begx, FALSE);
199*51ffecc1SBen Gras 	if (win == NULL)
200*51ffecc1SBen Gras 		goto error0;
201*51ffecc1SBen Gras 	win->cury = wtmp->cury;
202*51ffecc1SBen Gras 	win->curx = wtmp->curx;
203*51ffecc1SBen Gras 	win->reqy = wtmp->reqy;
204*51ffecc1SBen Gras 	win->reqx = wtmp->reqx;
205*51ffecc1SBen Gras 	win->flags = wtmp->flags;
206*51ffecc1SBen Gras 	win->delay = wtmp->delay;
207*51ffecc1SBen Gras 	win->wattr = wtmp->wattr;
208*51ffecc1SBen Gras 	win->bch = wtmp->bch;
209*51ffecc1SBen Gras 	win->battr = wtmp->battr;
210*51ffecc1SBen Gras 	win->scr_t = wtmp->scr_t;
211*51ffecc1SBen Gras 	win->scr_b = wtmp->scr_b;
212*51ffecc1SBen Gras 	free(wtmp);
213*51ffecc1SBen Gras 	wtmp = NULL;
214*51ffecc1SBen Gras 	__swflags(win);
215*51ffecc1SBen Gras 
216*51ffecc1SBen Gras #ifdef HAVE_WCHAR
217*51ffecc1SBen Gras 	if (__getnsp(win->bnsp, fp) == ERR)
218*51ffecc1SBen Gras 		goto error1;
219*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
220*51ffecc1SBen Gras 
221*51ffecc1SBen Gras 	/* Lines and line data */
222*51ffecc1SBen Gras 	for (y = 0; y < win->maxy; y++) {
223*51ffecc1SBen Gras 		for (sp = win->alines[y]->line, x = 0; x < win->maxx;
224*51ffecc1SBen Gras 		    x++, sp++) {
225*51ffecc1SBen Gras 			if (fread(&sp->ch, sizeof(wchar_t), 1, fp) != 1)
226*51ffecc1SBen Gras 				goto error1;
227*51ffecc1SBen Gras 			if (fread(&sp->attr, sizeof(attr_t), 1, fp) != 1)
228*51ffecc1SBen Gras 				goto error1;
229*51ffecc1SBen Gras #ifdef HAVE_WCHAR
230*51ffecc1SBen Gras 			if (sp->nsp != NULL) {
231*51ffecc1SBen Gras 				if (__getnsp(win->bnsp, fp) == ERR)
232*51ffecc1SBen Gras 					goto error1;
233*51ffecc1SBen Gras 			}
234*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
235*51ffecc1SBen Gras 		}
236*51ffecc1SBen Gras 		__touchline(win, y, 0, (int) win->maxx - 1);
237*51ffecc1SBen Gras 	}
238*51ffecc1SBen Gras #ifdef DEBUG
239*51ffecc1SBen Gras 	__CTRACE(__CTRACE_FILEIO, "getwin: win = 0x%p\n", win);
240*51ffecc1SBen Gras #endif
241*51ffecc1SBen Gras 	return win;
242*51ffecc1SBen Gras 
243*51ffecc1SBen Gras error1:
244*51ffecc1SBen Gras 	delwin(win);
245*51ffecc1SBen Gras error0:
246*51ffecc1SBen Gras 	if (wtmp)
247*51ffecc1SBen Gras 		free(wtmp);
248*51ffecc1SBen Gras 	return NULL;
249*51ffecc1SBen Gras }
250