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