#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "curses_inc.h"
#include "object.h"
#define OFFSET_BUFSIZE 100
#define swap(x) (((x >> 8) & 0377) + 256 * (x & 0377))
#define min(a, b) ((a) > (b) ? (b) : (a))
static char TermNames[128];
static char StringTable[2048];
static int beencalled = 0;
extern void err_abort(char *, ...);
extern void syserr_abort(char *, ...);
extern void warning(char *, ...);
int must_swap(void);
int
read_entry(char *filename, struct _bool_struct *bptr, struct _num_struct *nptr,
struct _str_struct *sptr)
{
int fd;
int numread;
int num_strings;
int cur_string;
int i;
struct header header;
unsigned char bytebuf[2];
char ch;
char *UB;
short *UN;
char **US;
char *pst;
int swapping = must_swap();
extern int BoolCount;
extern int NumCount;
extern int StrCount;
extern long lseek();
unsigned char byte[OFFSET_BUFSIZE][2];
short number[OFFSET_BUFSIZE];
fd = open(filename, 0);
if (fd < 0)
return (-1);
read(fd, &header, sizeof (header));
if (swapping) {
header.magic = swap(header.magic);
header.name_size = swap(header.name_size);
header.bool_count = swap(header.bool_count);
header.num_count = swap(header.num_count);
header.str_count = swap(header.str_count);
header.str_size = swap(header.str_size);
}
if (header.magic != MAGIC) {
close(fd);
return (-1);
}
read(fd, TermNames, min(127, header.name_size));
TermNames[127] = '\0';
if (header.name_size > 127)
lseek(fd, (long)(header.name_size - 127), 1);
UB = &(bptr->_auto_left_margin);
UN = &(nptr->_columns);
US = &(sptr->strs._back_tab);
read(fd, UB, min(BoolCount, header.bool_count));
if (header.bool_count > BoolCount)
lseek(fd, (long)(header.bool_count - BoolCount), 1);
else
for (i = header.bool_count; i < BoolCount; i++)
UB[i] = 0;
if ((header.name_size + header.bool_count) % 2 != 0)
read(fd, &ch, 1);
if (!swapping)
read(fd, (char *)UN, min(NumCount, header.num_count) * 2);
else {
for (i = 0; i < min(header.num_count, NumCount); i++) {
read(fd, (char *)bytebuf, 2);
if (bytebuf[1] == 0377) {
if (bytebuf[0] == 0376)
UN[i] = -2;
else if (bytebuf[0] == 0377)
UN[i] = -1;
else
UN[i] = bytebuf[0] + 256 * bytebuf[1];
} else
UN[i] = bytebuf[0] + 256 * bytebuf[1];
}
}
if (header.num_count > NumCount)
lseek(fd, (long)(2 * (header.num_count - NumCount)), 1);
else
for (i = header.num_count; i < NumCount; i++)
UN[i] = -1;
if (beencalled) {
pst = malloc((unsigned)header.str_size);
if (pst == NULL) {
close(fd);
return (-1);
}
} else {
pst = StringTable;
beencalled++;
}
num_strings = min(StrCount, header.str_count);
cur_string = 0;
while (num_strings > 0) {
if (swapping) {
numread = read(fd, byte, 2*min(num_strings,
OFFSET_BUFSIZE));
if (numread <= 0) {
close(fd);
return (-1);
}
for (i = 0; i < numread / 2; i++) {
if (byte[i][0] == 0377 && byte[i][1] == 0377)
US[i + cur_string] = 0;
else if (byte[i][0] == 0376 &&
byte[i][1] == 0377)
US[i + cur_string] = (char *)-1;
else
US[i + cur_string] = (byte[i][0] +
256*byte[i][1]) + pst;
}
} else {
numread = read(fd, number, 2*min(num_strings,
OFFSET_BUFSIZE));
if (numread <= 0) {
close(fd);
return (-1);
}
for (i = 0; i < numread / 2; i++) {
if (number[i] == -1)
US[i + cur_string] = 0;
else if (number[i] == -2)
US[i + cur_string] = (char *)-1;
else
US[i + cur_string] = number[i] + pst;
}
}
cur_string += numread / 2;
num_strings -= numread / 2;
}
if (header.str_count > StrCount)
lseek(fd, (long)(2 * (header.str_count - StrCount)), 1);
else for (i = header.str_count; i < StrCount; i++)
US[i] = 0;
numread = read(fd, pst, header.str_size);
close(fd);
if (numread != header.str_size)
return (-1);
return (0);
}
int
must_swap()
{
union {
short num;
char byte[2];
} test;
test.num = 1;
return (test.byte[1]);
}