|
|
1.1 root 1: /*********************************************************************
2: * COPYRIGHT NOTICE *
3: **********************************************************************
4: * This software is copyright (C) 1982 by Pavel Curtis *
5: * *
6: * Permission is granted to reproduce and distribute *
7: * this file by any means so long as no fee is charged *
8: * above a nominal handling fee and so long as this *
9: * notice is always included in the copies. *
10: * *
11: * Other rights are reserved except as explicitly granted *
12: * by written permission of the author. *
13: * Pavel Curtis *
14: * Computer Science Dept. *
15: * 405 Upson Hall *
16: * Cornell University *
17: * Ithaca, NY 14853 *
18: * *
19: * Ph- (607) 256-4934 *
20: * *
21: * Pavel.Cornell@Udel-Relay (ARPAnet) *
22: * decvax!cornell!pavel (UUCPnet) *
23: *********************************************************************/
24:
25: /*
26: * read_entry.c -- Routine for reading in a compiled terminfo file
27: *
28: * $Log: read_entry.c,v $
29: * Revision 1.1 92/03/13 10:45:52 bin
30: * Initial revision
31: *
32: * Revision 3.2 91/07/28 14:12:20 munk
33: * Made the large arrays static
34: *
35: * Revision 3.1 84/12/13 11:21:14 john
36: * Revisions by Mark Horton
37: *
38: * Revision 2.1 82/10/25 14:49:55 pavel
39: * Added Copyright Notice
40: *
41: * Revision 2.0 82/10/24 15:18:22 pavel
42: * Beta-one Test Release
43: *
44: * Revision 1.3 82/08/23 22:31:15 pavel
45: * The REAL Alpha-one Release Version
46: *
47: * Revision 1.2 82/08/19 19:11:49 pavel
48: * Alpha Test Release One
49: *
50: * Revision 1.1 82/08/12 22:25:13 pavel
51: * Initial revision
52: *
53: *
54: */
55:
56: #ifndef COHERENT
57: static char RCSid[] =
58: "$Header: /src386/usr/bin/tic/RCS/read_entry.c,v 1.1 92/03/13 10:45:52 bin Exp $";
59: #endif
60:
61: #include <sys/types.h>
62: #include <sys/stat.h>
63: #include "term.h"
64: #include "object.h"
65:
66: #define OFFSET_BUFSIZE 100
67:
68: #define min(a, b) ((a) > (b) ? (b) : (a))
69:
70: /*
71: * int
72: * read_entry(filename, ptr)
73: *
74: * Read the compiled terminfo entry in the given file into the
75: * structure pointed to by ptr, allocating space for the string
76: * table and placing its address in ptr->str_table.
77: *
78: */
79:
80: #define swap(x) (((x >> 8) & 0377) + 256 * (x & 0377))
81:
82: static char TermNames[128]; /* Buffer for terminal names for first term */
83: static char StringTable[1024]; /* String table for first terminal */
84:
85: int
86: read_entry(filename, ptr)
87: char *filename;
88: struct term *ptr;
89: {
90: int fd;
91: int numread;
92: int num_strings;
93: int cur_string;
94: char *malloc();
95: int i;
96: struct header header;
97: unsigned char bytebuf[2];
98: char ch;
99: static union
100: {
101: unsigned char byte[2];
102: short number;
103: } offset_buf[OFFSET_BUFSIZE];
104:
105: fd = open(filename, 0);
106:
107: if (fd < 0)
108: return(-1);
109:
110: read(fd, &header, sizeof(header));
111:
112: if (must_swap())
113: {
114: header.magic = swap(header.magic);
115: header.name_size = swap(header.name_size);
116: header.bool_count = swap(header.bool_count);
117: header.num_count = swap(header.num_count);
118: header.str_count = swap(header.str_count);
119: header.str_size = swap(header.str_size);
120: }
121:
122: if (header.magic != MAGIC)
123: {
124: close(fd);
125: return(-1);
126: }
127:
128: read(fd, TermNames, min(127, header.name_size));
129: TermNames[127] = '\0';
130: ptr->term_names = TermNames;
131: if (header.name_size > 127)
132: lseek(fd, (long) (header.name_size - 127), 1);
133:
134: read(fd, ptr->Booleans, min(BOOLCOUNT, header.bool_count));
135: if (header.bool_count > BOOLCOUNT)
136: lseek(fd, (long) (header.bool_count - BOOLCOUNT), 1);
137: else
138: for (i=header.bool_count; i < BOOLCOUNT; i++)
139: ptr->Booleans[i] = 0;
140:
141: if ((header.name_size + header.bool_count) % 2 != 0)
142: read(fd, &ch, 1);
143:
144: if (must_swap())
145: read(fd, ptr->Numbers, min(NUMCOUNT, header.num_count * 2));
146: else
147: {
148: for (i=0; i < min(header.num_count, NUMCOUNT); i++)
149: {
150: read(fd, bytebuf, 2);
151: if (bytebuf[0] == 0377 && bytebuf[1] == 0377)
152: ptr->Numbers[i] = -1;
153: else
154: ptr->Numbers[i] = bytebuf[0] + 256 * bytebuf[1];
155: }
156: }
157:
158: if (header.num_count > NUMCOUNT)
159: lseek(fd, (long) (2 * (header.num_count - NUMCOUNT)), 1);
160: else
161: for (i=header.num_count; i < NUMCOUNT; i++)
162: ptr->Numbers[i] = -1;
163:
164: if (cur_term) /* cur_term is non-zero only if we've been called */
165: {
166: ptr->str_table = malloc(header.str_size);
167: if (ptr->str_table == NULL)
168: {
169: close(fd);
170: return (-1);
171: }
172: }
173: else
174: ptr->str_table = StringTable;
175:
176: num_strings = min(STRCOUNT, header.str_count);
177: cur_string = 0;
178:
179: while (num_strings > 0)
180: {
181: numread = read(fd, offset_buf, 2*min(num_strings, OFFSET_BUFSIZE));
182: if (numread <= 0)
183: {
184: close(fd);
185: return(-1);
186: }
187:
188: if (must_swap())
189: {
190: for (i = 0; i < numread / 2; i++)
191: {
192: ptr->Strings[i + cur_string] =
193: (offset_buf[i].byte[0] == 0377
194: && offset_buf[i].byte[1] == 0377)
195: ? 0
196: : ((offset_buf[i].byte[0] + 256*offset_buf[i].byte[1])
197: + ptr->str_table);
198: }
199: }
200: else
201: {
202: for (i = 0; i < numread / 2; i++)
203: {
204: ptr->Strings[i + cur_string] =
205: (offset_buf[i].number == -1)
206: ?
207: 0
208: :
209: offset_buf[i].number + ptr->str_table;
210: }
211: }
212:
213: cur_string += numread / 2;
214: num_strings -= numread / 2;
215: }
216:
217: if (header.str_count > STRCOUNT)
218: lseek(fd, (long) (2 * (header.str_count - STRCOUNT)), 1);
219: else
220: for (i=header.str_count; i < STRCOUNT; i++)
221: ptr->Strings[i] = 0;
222:
223: numread = read(fd, ptr->str_table, header.str_size);
224: close(fd);
225: if (numread != header.str_size)
226: return(-1);
227:
228: return(0);
229: }
230:
231:
232:
233: /*
234: * int
235: * must_swap()
236: *
237: * Test whether this machine will need byte-swapping
238: *
239: */
240:
241: int
242: must_swap()
243: {
244: union
245: {
246: short num;
247: char byte[2];
248: } test;
249:
250: test.num = 1;
251: return(test.byte[1]);
252: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.