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