xref: /illumos-gate/usr/src/cmd/tic/tic_read.c (revision 2587b3a0593a431a99cc2dc78bf1034e672a35e2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1997 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * ********************************************************************
437c478bd9Sstevel@tonic-gate  *                         COPYRIGHT NOTICE                           *
447c478bd9Sstevel@tonic-gate  * ********************************************************************
457c478bd9Sstevel@tonic-gate  *        This software is copyright (C) 1982 by Pavel Curtis         *
467c478bd9Sstevel@tonic-gate  *                                                                    *
477c478bd9Sstevel@tonic-gate  *        Permission is granted to reproduce and distribute           *
487c478bd9Sstevel@tonic-gate  *        this file by any means so long as no fee is charged         *
497c478bd9Sstevel@tonic-gate  *        above a nominal handling fee and so long as this            *
507c478bd9Sstevel@tonic-gate  *        notice is always included in the copies.                    *
517c478bd9Sstevel@tonic-gate  *                                                                    *
527c478bd9Sstevel@tonic-gate  *        Other rights are reserved except as explicitly granted      *
537c478bd9Sstevel@tonic-gate  *        by written permission of the author.                        *
547c478bd9Sstevel@tonic-gate  *                Pavel Curtis                                        *
557c478bd9Sstevel@tonic-gate  *                Computer Science Dept.                              *
567c478bd9Sstevel@tonic-gate  *                405 Upson Hall                                      *
577c478bd9Sstevel@tonic-gate  *                Cornell University                                  *
587c478bd9Sstevel@tonic-gate  *                Ithaca, NY 14853                                    *
597c478bd9Sstevel@tonic-gate  *                                                                    *
607c478bd9Sstevel@tonic-gate  *                Ph- (607) 256-4934                                  *
617c478bd9Sstevel@tonic-gate  *                                                                    *
627c478bd9Sstevel@tonic-gate  *                Pavel.Cornell@Udel-Relay   (ARPAnet)                *
637c478bd9Sstevel@tonic-gate  *                decvax!cornell!pavel       (UUCPnet)                *
647c478bd9Sstevel@tonic-gate  * ********************************************************************
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  *	read_entry.c -- Routine for reading in a compiled terminfo file
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *  $Log:	RCS/read_entry.v $
717c478bd9Sstevel@tonic-gate  * Revision 2.1  82/10/25  14:49:55  pavel
727c478bd9Sstevel@tonic-gate  * Added Copyright Notice
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * Revision 2.0  82/10/24  15:18:22  pavel
757c478bd9Sstevel@tonic-gate  * Beta-one Test Release
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * Revision 1.3  82/08/23  22:31:15  pavel
787c478bd9Sstevel@tonic-gate  * The REAL Alpha-one Release Version
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * Revision 1.2  82/08/19  19:11:49  pavel
817c478bd9Sstevel@tonic-gate  * Alpha Test Release One
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * Revision 1.1  82/08/12  22:25:13  pavel
847c478bd9Sstevel@tonic-gate  * Initial revision
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #include <sys/types.h>
917c478bd9Sstevel@tonic-gate #include <sys/stat.h>
92*2587b3a0SRichard Lowe #include <sys/types.h>
93*2587b3a0SRichard Lowe #include <sys/stat.h>
94*2587b3a0SRichard Lowe 
95*2587b3a0SRichard Lowe #include <fcntl.h>
967c478bd9Sstevel@tonic-gate #include <stdlib.h>
97*2587b3a0SRichard Lowe #include <unistd.h>
987c478bd9Sstevel@tonic-gate #include "curses_inc.h"
997c478bd9Sstevel@tonic-gate #include "object.h"
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #define	OFFSET_BUFSIZE	100
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	swap(x)		(((x >> 8) & 0377) + 256 * (x & 0377))
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #define	min(a, b)	((a) > (b)  ?  (b)  :  (a))
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  *	int
1097c478bd9Sstevel@tonic-gate  *	read_entry(filename, ptr)
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  *	Read the compiled terminfo entry in the given file into the
1127c478bd9Sstevel@tonic-gate  *	structure pointed to by ptr, allocating space for the string
1137c478bd9Sstevel@tonic-gate  *	table and placing its address in ptr->str_table.
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static char	TermNames[128];	/* Buffer for terminal names for first term */
1187c478bd9Sstevel@tonic-gate static char	StringTable[2048];	/* String table for first terminal  */
1197c478bd9Sstevel@tonic-gate static int	beencalled = 0;	/* read_entry has been called before	    */
1207c478bd9Sstevel@tonic-gate 
121*2587b3a0SRichard Lowe extern void err_abort(char *, ...);
122*2587b3a0SRichard Lowe extern void syserr_abort(char *, ...);
123*2587b3a0SRichard Lowe extern void warning(char *, ...);
124*2587b3a0SRichard Lowe 
125*2587b3a0SRichard Lowe int must_swap(void);
126*2587b3a0SRichard Lowe 
1277c478bd9Sstevel@tonic-gate int
read_entry(char * filename,struct _bool_struct * bptr,struct _num_struct * nptr,struct _str_struct * sptr)128*2587b3a0SRichard Lowe read_entry(char *filename, struct _bool_struct *bptr, struct _num_struct *nptr,
129*2587b3a0SRichard Lowe     struct _str_struct *sptr)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int		fd;
1327c478bd9Sstevel@tonic-gate 	int		numread;
1337c478bd9Sstevel@tonic-gate 	int		num_strings;
1347c478bd9Sstevel@tonic-gate 	int		cur_string;
1357c478bd9Sstevel@tonic-gate 	int		i;
1367c478bd9Sstevel@tonic-gate 	struct header	header;
1377c478bd9Sstevel@tonic-gate 	unsigned char	bytebuf[2];
1387c478bd9Sstevel@tonic-gate 	char		ch;
1397c478bd9Sstevel@tonic-gate 	char *UB;	/* booleans */
1407c478bd9Sstevel@tonic-gate 	short *UN;	/* numbers */
1417c478bd9Sstevel@tonic-gate 	char **US;	/* strings */
1427c478bd9Sstevel@tonic-gate 	char *pst;	/* pointer to string table */
1437c478bd9Sstevel@tonic-gate 	int swapping = must_swap();
1447c478bd9Sstevel@tonic-gate 	extern int BoolCount;
1457c478bd9Sstevel@tonic-gate 	extern int NumCount;
1467c478bd9Sstevel@tonic-gate 	extern int StrCount;
1477c478bd9Sstevel@tonic-gate 	extern long lseek();
1487c478bd9Sstevel@tonic-gate 	unsigned char byte[OFFSET_BUFSIZE][2];
1497c478bd9Sstevel@tonic-gate 	short number[OFFSET_BUFSIZE];
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	fd = open(filename, 0);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (fd < 0)
1547c478bd9Sstevel@tonic-gate 		return (-1);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	read(fd, &header, sizeof (header));
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (swapping) {
1597c478bd9Sstevel@tonic-gate 		header.magic = swap(header.magic);
1607c478bd9Sstevel@tonic-gate 		header.name_size = swap(header.name_size);
1617c478bd9Sstevel@tonic-gate 		header.bool_count = swap(header.bool_count);
1627c478bd9Sstevel@tonic-gate 		header.num_count = swap(header.num_count);
1637c478bd9Sstevel@tonic-gate 		header.str_count = swap(header.str_count);
1647c478bd9Sstevel@tonic-gate 		header.str_size = swap(header.str_size);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (header.magic != MAGIC) {
1687c478bd9Sstevel@tonic-gate 		close(fd);
1697c478bd9Sstevel@tonic-gate 		return (-1);
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	read(fd, TermNames, min(127, header.name_size));
1737c478bd9Sstevel@tonic-gate 	TermNames[127] = '\0';
1747c478bd9Sstevel@tonic-gate 	if (header.name_size > 127)
1757c478bd9Sstevel@tonic-gate 		lseek(fd, (long)(header.name_size - 127), 1);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	UB = &(bptr->_auto_left_margin);
1787c478bd9Sstevel@tonic-gate 	UN = &(nptr->_columns);
1797c478bd9Sstevel@tonic-gate 	US = &(sptr->strs._back_tab);
1807c478bd9Sstevel@tonic-gate 	read(fd, UB, min(BoolCount, header.bool_count));
1817c478bd9Sstevel@tonic-gate 	if (header.bool_count > BoolCount)
1827c478bd9Sstevel@tonic-gate 		lseek(fd, (long)(header.bool_count - BoolCount), 1);
1837c478bd9Sstevel@tonic-gate 	else
1847c478bd9Sstevel@tonic-gate 		for (i = header.bool_count; i < BoolCount; i++)
1857c478bd9Sstevel@tonic-gate 			UB[i] = 0;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if ((header.name_size + header.bool_count) % 2 != 0)
1887c478bd9Sstevel@tonic-gate 		read(fd, &ch, 1);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (!swapping)
1917c478bd9Sstevel@tonic-gate 		read(fd, (char *)UN, min(NumCount, header.num_count) * 2);
1927c478bd9Sstevel@tonic-gate 	else {
1937c478bd9Sstevel@tonic-gate 		for (i = 0; i < min(header.num_count, NumCount); i++) {
1947c478bd9Sstevel@tonic-gate 			read(fd, (char *)bytebuf, 2);
1957c478bd9Sstevel@tonic-gate 			if (bytebuf[1] == 0377) {
1967c478bd9Sstevel@tonic-gate 				if (bytebuf[0] == 0376)	/* -2 == cancelled */
1977c478bd9Sstevel@tonic-gate 					UN[i] = -2;
1987c478bd9Sstevel@tonic-gate 				else if (bytebuf[0] == 0377)
1997c478bd9Sstevel@tonic-gate 					/* -1 == not there */
2007c478bd9Sstevel@tonic-gate 					UN[i] = -1;
2017c478bd9Sstevel@tonic-gate 				else
2027c478bd9Sstevel@tonic-gate 					UN[i] = bytebuf[0] + 256 * bytebuf[1];
2037c478bd9Sstevel@tonic-gate 			} else
2047c478bd9Sstevel@tonic-gate 				UN[i] = bytebuf[0] + 256 * bytebuf[1];
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (header.num_count > NumCount)
2097c478bd9Sstevel@tonic-gate 		lseek(fd, (long)(2 * (header.num_count - NumCount)), 1);
2107c478bd9Sstevel@tonic-gate 	else
2117c478bd9Sstevel@tonic-gate 		for (i = header.num_count; i < NumCount; i++)
2127c478bd9Sstevel@tonic-gate 			UN[i] = -1;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (beencalled) {
2157c478bd9Sstevel@tonic-gate 		/* beencalled is non-zero only if we've been called */
2167c478bd9Sstevel@tonic-gate 		pst = malloc((unsigned)header.str_size);
2177c478bd9Sstevel@tonic-gate 		if (pst == NULL) {
2187c478bd9Sstevel@tonic-gate 			close(fd);
2197c478bd9Sstevel@tonic-gate 			return (-1);
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	} else {
2227c478bd9Sstevel@tonic-gate 		pst = StringTable;
2237c478bd9Sstevel@tonic-gate 		beencalled++;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	num_strings = min(StrCount, header.str_count);
2277c478bd9Sstevel@tonic-gate 	cur_string = 0;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	while (num_strings > 0) {
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		if (swapping) {
2327c478bd9Sstevel@tonic-gate 			numread = read(fd, byte, 2*min(num_strings,
2337c478bd9Sstevel@tonic-gate 							OFFSET_BUFSIZE));
2347c478bd9Sstevel@tonic-gate 			if (numread <= 0) {
2357c478bd9Sstevel@tonic-gate 				close(fd);
2367c478bd9Sstevel@tonic-gate 				return (-1);
2377c478bd9Sstevel@tonic-gate 			}
2387c478bd9Sstevel@tonic-gate 			for (i = 0; i < numread / 2; i++) {
2397c478bd9Sstevel@tonic-gate 				if (byte[i][0] == 0377 && byte[i][1] == 0377)
2407c478bd9Sstevel@tonic-gate 									/* -1 */
2417c478bd9Sstevel@tonic-gate 					US[i + cur_string] = 0;
2427c478bd9Sstevel@tonic-gate 				else if (byte[i][0] == 0376 &&
2437c478bd9Sstevel@tonic-gate 							byte[i][1] == 0377)
2447c478bd9Sstevel@tonic-gate 								/* -2 */
2457c478bd9Sstevel@tonic-gate 					US[i + cur_string] = (char *)-1;
2467c478bd9Sstevel@tonic-gate 				else
2477c478bd9Sstevel@tonic-gate 					US[i + cur_string] = (byte[i][0] +
2487c478bd9Sstevel@tonic-gate 							256*byte[i][1]) + pst;
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 		} else {
2517c478bd9Sstevel@tonic-gate 			numread = read(fd, number, 2*min(num_strings,
2527c478bd9Sstevel@tonic-gate 							OFFSET_BUFSIZE));
2537c478bd9Sstevel@tonic-gate 			if (numread <= 0) {
2547c478bd9Sstevel@tonic-gate 				close(fd);
2557c478bd9Sstevel@tonic-gate 				return (-1);
2567c478bd9Sstevel@tonic-gate 			}
2577c478bd9Sstevel@tonic-gate 			for (i = 0; i < numread / 2; i++) {
2587c478bd9Sstevel@tonic-gate 				if (number[i] == -1)	/* not there */
2597c478bd9Sstevel@tonic-gate 					US[i + cur_string] = 0;
2607c478bd9Sstevel@tonic-gate 				else if (number[i] == -2)	/* cancelled */
2617c478bd9Sstevel@tonic-gate 					US[i + cur_string] = (char *)-1;
2627c478bd9Sstevel@tonic-gate 				else
2637c478bd9Sstevel@tonic-gate 					US[i + cur_string] = number[i] + pst;
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		cur_string += numread / 2;
2687c478bd9Sstevel@tonic-gate 		num_strings -= numread / 2;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (header.str_count > StrCount)
2727c478bd9Sstevel@tonic-gate 		lseek(fd, (long)(2 * (header.str_count - StrCount)), 1);
2737c478bd9Sstevel@tonic-gate 	else for (i = header.str_count; i < StrCount; i++)
2747c478bd9Sstevel@tonic-gate 		US[i] = 0;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	numread = read(fd, pst, header.str_size);
2777c478bd9Sstevel@tonic-gate 	close(fd);
2787c478bd9Sstevel@tonic-gate 	if (numread != header.str_size)
2797c478bd9Sstevel@tonic-gate 		return (-1);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	return (0);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  *	int
2867c478bd9Sstevel@tonic-gate  *	must_swap()
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  *	Test whether this machine will need byte-swapping
2897c478bd9Sstevel@tonic-gate  *
2907c478bd9Sstevel@tonic-gate  */
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate int
must_swap()2937c478bd9Sstevel@tonic-gate must_swap()
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	union {
2967c478bd9Sstevel@tonic-gate 	    short num;
2977c478bd9Sstevel@tonic-gate 	    char  byte[2];
2987c478bd9Sstevel@tonic-gate 	} test;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	test.num = 1;
3017c478bd9Sstevel@tonic-gate 	return (test.byte[1]);
3027c478bd9Sstevel@tonic-gate }
303