xref: /titanic_44/usr/src/cmd/tic/tic_read.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 (c) 1996-1997 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
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 /*
32*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
33*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
34*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
37*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
38*7c478bd9Sstevel@tonic-gate  * contributors.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate  * ********************************************************************
45*7c478bd9Sstevel@tonic-gate  *                         COPYRIGHT NOTICE                           *
46*7c478bd9Sstevel@tonic-gate  * ********************************************************************
47*7c478bd9Sstevel@tonic-gate  *        This software is copyright (C) 1982 by Pavel Curtis         *
48*7c478bd9Sstevel@tonic-gate  *                                                                    *
49*7c478bd9Sstevel@tonic-gate  *        Permission is granted to reproduce and distribute           *
50*7c478bd9Sstevel@tonic-gate  *        this file by any means so long as no fee is charged         *
51*7c478bd9Sstevel@tonic-gate  *        above a nominal handling fee and so long as this            *
52*7c478bd9Sstevel@tonic-gate  *        notice is always included in the copies.                    *
53*7c478bd9Sstevel@tonic-gate  *                                                                    *
54*7c478bd9Sstevel@tonic-gate  *        Other rights are reserved except as explicitly granted      *
55*7c478bd9Sstevel@tonic-gate  *        by written permission of the author.                        *
56*7c478bd9Sstevel@tonic-gate  *                Pavel Curtis                                        *
57*7c478bd9Sstevel@tonic-gate  *                Computer Science Dept.                              *
58*7c478bd9Sstevel@tonic-gate  *                405 Upson Hall                                      *
59*7c478bd9Sstevel@tonic-gate  *                Cornell University                                  *
60*7c478bd9Sstevel@tonic-gate  *                Ithaca, NY 14853                                    *
61*7c478bd9Sstevel@tonic-gate  *                                                                    *
62*7c478bd9Sstevel@tonic-gate  *                Ph- (607) 256-4934                                  *
63*7c478bd9Sstevel@tonic-gate  *                                                                    *
64*7c478bd9Sstevel@tonic-gate  *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
65*7c478bd9Sstevel@tonic-gate  *                decvax!cornell!pavel       (UUCPnet)                *
66*7c478bd9Sstevel@tonic-gate  * ********************************************************************
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate /*
70*7c478bd9Sstevel@tonic-gate  *	read_entry.c -- Routine for reading in a compiled terminfo file
71*7c478bd9Sstevel@tonic-gate  *
72*7c478bd9Sstevel@tonic-gate  *  $Log:	RCS/read_entry.v $
73*7c478bd9Sstevel@tonic-gate  * Revision 2.1  82/10/25  14:49:55  pavel
74*7c478bd9Sstevel@tonic-gate  * Added Copyright Notice
75*7c478bd9Sstevel@tonic-gate  *
76*7c478bd9Sstevel@tonic-gate  * Revision 2.0  82/10/24  15:18:22  pavel
77*7c478bd9Sstevel@tonic-gate  * Beta-one Test Release
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  * Revision 1.3  82/08/23  22:31:15  pavel
80*7c478bd9Sstevel@tonic-gate  * The REAL Alpha-one Release Version
81*7c478bd9Sstevel@tonic-gate  *
82*7c478bd9Sstevel@tonic-gate  * Revision 1.2  82/08/19  19:11:49  pavel
83*7c478bd9Sstevel@tonic-gate  * Alpha Test Release One
84*7c478bd9Sstevel@tonic-gate  *
85*7c478bd9Sstevel@tonic-gate  * Revision 1.1  82/08/12  22:25:13  pavel
86*7c478bd9Sstevel@tonic-gate  * Initial revision
87*7c478bd9Sstevel@tonic-gate  *
88*7c478bd9Sstevel@tonic-gate  *
89*7c478bd9Sstevel@tonic-gate  */
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
93*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
94*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
95*7c478bd9Sstevel@tonic-gate #include "curses_inc.h"
96*7c478bd9Sstevel@tonic-gate #include "object.h"
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate #define	OFFSET_BUFSIZE	100
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate #define	swap(x)		(((x >> 8) & 0377) + 256 * (x & 0377))
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate #define	min(a, b)	((a) > (b)  ?  (b)  :  (a))
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  *	int
106*7c478bd9Sstevel@tonic-gate  *	read_entry(filename, ptr)
107*7c478bd9Sstevel@tonic-gate  *
108*7c478bd9Sstevel@tonic-gate  *	Read the compiled terminfo entry in the given file into the
109*7c478bd9Sstevel@tonic-gate  *	structure pointed to by ptr, allocating space for the string
110*7c478bd9Sstevel@tonic-gate  *	table and placing its address in ptr->str_table.
111*7c478bd9Sstevel@tonic-gate  *
112*7c478bd9Sstevel@tonic-gate  */
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate static char	TermNames[128];	/* Buffer for terminal names for first term */
115*7c478bd9Sstevel@tonic-gate static char	StringTable[2048];	/* String table for first terminal  */
116*7c478bd9Sstevel@tonic-gate static int	beencalled = 0;	/* read_entry has been called before	    */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate int
read_entry(filename,bptr,nptr,sptr)119*7c478bd9Sstevel@tonic-gate read_entry(filename, bptr, nptr, sptr)
120*7c478bd9Sstevel@tonic-gate char			*filename;
121*7c478bd9Sstevel@tonic-gate struct _bool_struct	*bptr;
122*7c478bd9Sstevel@tonic-gate struct _num_struct	*nptr;
123*7c478bd9Sstevel@tonic-gate struct _str_struct	*sptr;
124*7c478bd9Sstevel@tonic-gate {
125*7c478bd9Sstevel@tonic-gate 	int		fd;
126*7c478bd9Sstevel@tonic-gate 	int		numread;
127*7c478bd9Sstevel@tonic-gate 	int		num_strings;
128*7c478bd9Sstevel@tonic-gate 	int		cur_string;
129*7c478bd9Sstevel@tonic-gate 	int		i;
130*7c478bd9Sstevel@tonic-gate 	struct header	header;
131*7c478bd9Sstevel@tonic-gate 	unsigned char	bytebuf[2];
132*7c478bd9Sstevel@tonic-gate 	char		ch;
133*7c478bd9Sstevel@tonic-gate 	char *UB;	/* booleans */
134*7c478bd9Sstevel@tonic-gate 	short *UN;	/* numbers */
135*7c478bd9Sstevel@tonic-gate 	char **US;	/* strings */
136*7c478bd9Sstevel@tonic-gate 	char *pst;	/* pointer to string table */
137*7c478bd9Sstevel@tonic-gate 	int swapping = must_swap();
138*7c478bd9Sstevel@tonic-gate 	extern int BoolCount;
139*7c478bd9Sstevel@tonic-gate 	extern int NumCount;
140*7c478bd9Sstevel@tonic-gate 	extern int StrCount;
141*7c478bd9Sstevel@tonic-gate 	extern long lseek();
142*7c478bd9Sstevel@tonic-gate 	unsigned char byte[OFFSET_BUFSIZE][2];
143*7c478bd9Sstevel@tonic-gate 	short number[OFFSET_BUFSIZE];
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	fd = open(filename, 0);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if (fd < 0)
148*7c478bd9Sstevel@tonic-gate 		return (-1);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	read(fd, &header, sizeof (header));
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	if (swapping) {
153*7c478bd9Sstevel@tonic-gate 		header.magic = swap(header.magic);
154*7c478bd9Sstevel@tonic-gate 		header.name_size = swap(header.name_size);
155*7c478bd9Sstevel@tonic-gate 		header.bool_count = swap(header.bool_count);
156*7c478bd9Sstevel@tonic-gate 		header.num_count = swap(header.num_count);
157*7c478bd9Sstevel@tonic-gate 		header.str_count = swap(header.str_count);
158*7c478bd9Sstevel@tonic-gate 		header.str_size = swap(header.str_size);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if (header.magic != MAGIC) {
162*7c478bd9Sstevel@tonic-gate 		close(fd);
163*7c478bd9Sstevel@tonic-gate 		return (-1);
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	read(fd, TermNames, min(127, header.name_size));
167*7c478bd9Sstevel@tonic-gate 	TermNames[127] = '\0';
168*7c478bd9Sstevel@tonic-gate 	if (header.name_size > 127)
169*7c478bd9Sstevel@tonic-gate 		lseek(fd, (long)(header.name_size - 127), 1);
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	UB = &(bptr->_auto_left_margin);
172*7c478bd9Sstevel@tonic-gate 	UN = &(nptr->_columns);
173*7c478bd9Sstevel@tonic-gate 	US = &(sptr->strs._back_tab);
174*7c478bd9Sstevel@tonic-gate 	read(fd, UB, min(BoolCount, header.bool_count));
175*7c478bd9Sstevel@tonic-gate 	if (header.bool_count > BoolCount)
176*7c478bd9Sstevel@tonic-gate 		lseek(fd, (long)(header.bool_count - BoolCount), 1);
177*7c478bd9Sstevel@tonic-gate 	else
178*7c478bd9Sstevel@tonic-gate 		for (i = header.bool_count; i < BoolCount; i++)
179*7c478bd9Sstevel@tonic-gate 			UB[i] = 0;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	if ((header.name_size + header.bool_count) % 2 != 0)
182*7c478bd9Sstevel@tonic-gate 		read(fd, &ch, 1);
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (!swapping)
185*7c478bd9Sstevel@tonic-gate 		read(fd, (char *)UN, min(NumCount, header.num_count) * 2);
186*7c478bd9Sstevel@tonic-gate 	else {
187*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < min(header.num_count, NumCount); i++) {
188*7c478bd9Sstevel@tonic-gate 			read(fd, (char *)bytebuf, 2);
189*7c478bd9Sstevel@tonic-gate 			if (bytebuf[1] == 0377) {
190*7c478bd9Sstevel@tonic-gate 				if (bytebuf[0] == 0376)	/* -2 == cancelled */
191*7c478bd9Sstevel@tonic-gate 					UN[i] = -2;
192*7c478bd9Sstevel@tonic-gate 				else if (bytebuf[0] == 0377)
193*7c478bd9Sstevel@tonic-gate 					/* -1 == not there */
194*7c478bd9Sstevel@tonic-gate 					UN[i] = -1;
195*7c478bd9Sstevel@tonic-gate 				else
196*7c478bd9Sstevel@tonic-gate 					UN[i] = bytebuf[0] + 256 * bytebuf[1];
197*7c478bd9Sstevel@tonic-gate 			} else
198*7c478bd9Sstevel@tonic-gate 				UN[i] = bytebuf[0] + 256 * bytebuf[1];
199*7c478bd9Sstevel@tonic-gate 		}
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	if (header.num_count > NumCount)
203*7c478bd9Sstevel@tonic-gate 		lseek(fd, (long)(2 * (header.num_count - NumCount)), 1);
204*7c478bd9Sstevel@tonic-gate 	else
205*7c478bd9Sstevel@tonic-gate 		for (i = header.num_count; i < NumCount; i++)
206*7c478bd9Sstevel@tonic-gate 			UN[i] = -1;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	if (beencalled) {
209*7c478bd9Sstevel@tonic-gate 		/* beencalled is non-zero only if we've been called */
210*7c478bd9Sstevel@tonic-gate 		pst = malloc((unsigned)header.str_size);
211*7c478bd9Sstevel@tonic-gate 		if (pst == NULL) {
212*7c478bd9Sstevel@tonic-gate 			close(fd);
213*7c478bd9Sstevel@tonic-gate 			return (-1);
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate 	} else {
216*7c478bd9Sstevel@tonic-gate 		pst = StringTable;
217*7c478bd9Sstevel@tonic-gate 		beencalled++;
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	num_strings = min(StrCount, header.str_count);
221*7c478bd9Sstevel@tonic-gate 	cur_string = 0;
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	while (num_strings > 0) {
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 		if (swapping) {
226*7c478bd9Sstevel@tonic-gate 			numread = read(fd, byte, 2*min(num_strings,
227*7c478bd9Sstevel@tonic-gate 							OFFSET_BUFSIZE));
228*7c478bd9Sstevel@tonic-gate 			if (numread <= 0) {
229*7c478bd9Sstevel@tonic-gate 				close(fd);
230*7c478bd9Sstevel@tonic-gate 				return (-1);
231*7c478bd9Sstevel@tonic-gate 			}
232*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < numread / 2; i++) {
233*7c478bd9Sstevel@tonic-gate 				if (byte[i][0] == 0377 && byte[i][1] == 0377)
234*7c478bd9Sstevel@tonic-gate 									/* -1 */
235*7c478bd9Sstevel@tonic-gate 					US[i + cur_string] = 0;
236*7c478bd9Sstevel@tonic-gate 				else if (byte[i][0] == 0376 &&
237*7c478bd9Sstevel@tonic-gate 							byte[i][1] == 0377)
238*7c478bd9Sstevel@tonic-gate 								/* -2 */
239*7c478bd9Sstevel@tonic-gate 					US[i + cur_string] = (char *)-1;
240*7c478bd9Sstevel@tonic-gate 				else
241*7c478bd9Sstevel@tonic-gate 					US[i + cur_string] = (byte[i][0] +
242*7c478bd9Sstevel@tonic-gate 							256*byte[i][1]) + pst;
243*7c478bd9Sstevel@tonic-gate 			}
244*7c478bd9Sstevel@tonic-gate 		} else {
245*7c478bd9Sstevel@tonic-gate 			numread = read(fd, number, 2*min(num_strings,
246*7c478bd9Sstevel@tonic-gate 							OFFSET_BUFSIZE));
247*7c478bd9Sstevel@tonic-gate 			if (numread <= 0) {
248*7c478bd9Sstevel@tonic-gate 				close(fd);
249*7c478bd9Sstevel@tonic-gate 				return (-1);
250*7c478bd9Sstevel@tonic-gate 			}
251*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < numread / 2; i++) {
252*7c478bd9Sstevel@tonic-gate 				if (number[i] == -1)	/* not there */
253*7c478bd9Sstevel@tonic-gate 					US[i + cur_string] = 0;
254*7c478bd9Sstevel@tonic-gate 				else if (number[i] == -2)	/* cancelled */
255*7c478bd9Sstevel@tonic-gate 					US[i + cur_string] = (char *)-1;
256*7c478bd9Sstevel@tonic-gate 				else
257*7c478bd9Sstevel@tonic-gate 					US[i + cur_string] = number[i] + pst;
258*7c478bd9Sstevel@tonic-gate 			}
259*7c478bd9Sstevel@tonic-gate 		}
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 		cur_string += numread / 2;
262*7c478bd9Sstevel@tonic-gate 		num_strings -= numread / 2;
263*7c478bd9Sstevel@tonic-gate 	}
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	if (header.str_count > StrCount)
266*7c478bd9Sstevel@tonic-gate 		lseek(fd, (long)(2 * (header.str_count - StrCount)), 1);
267*7c478bd9Sstevel@tonic-gate 	else for (i = header.str_count; i < StrCount; i++)
268*7c478bd9Sstevel@tonic-gate 		US[i] = 0;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 	numread = read(fd, pst, header.str_size);
271*7c478bd9Sstevel@tonic-gate 	close(fd);
272*7c478bd9Sstevel@tonic-gate 	if (numread != header.str_size)
273*7c478bd9Sstevel@tonic-gate 		return (-1);
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	return (0);
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  *	int
280*7c478bd9Sstevel@tonic-gate  *	must_swap()
281*7c478bd9Sstevel@tonic-gate  *
282*7c478bd9Sstevel@tonic-gate  *	Test whether this machine will need byte-swapping
283*7c478bd9Sstevel@tonic-gate  *
284*7c478bd9Sstevel@tonic-gate  */
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate int
must_swap()287*7c478bd9Sstevel@tonic-gate must_swap()
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate 	union {
290*7c478bd9Sstevel@tonic-gate 	    short num;
291*7c478bd9Sstevel@tonic-gate 	    char  byte[2];
292*7c478bd9Sstevel@tonic-gate 	} test;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	test.num = 1;
295*7c478bd9Sstevel@tonic-gate 	return (test.byte[1]);
296*7c478bd9Sstevel@tonic-gate }
297