#include "less.h"
#if MSDOS_COMPILER==WIN32C
#include <errno.h>
#include <windows.h>
#endif
#if HAVE_STAT_INO
#include <sys/stat.h>
extern dev_t curr_dev;
extern ino_t curr_ino;
#endif
typedef POSITION BLOCKNUM;
public int ignore_eoi;
#define LBUFSIZE 8192
struct buf {
struct buf *next, *prev;
struct buf *hnext, *hprev;
BLOCKNUM block;
unsigned int datasize;
unsigned char data[LBUFSIZE];
};
struct buflist {
struct buf *buf_next, *buf_prev;
struct buf *buf_hnext, *buf_hprev;
};
#define BUFHASH_SIZE 64
struct filestate {
struct buf *buf_next, *buf_prev;
struct buflist hashtbl[BUFHASH_SIZE];
int file;
int flags;
POSITION fpos;
int nbufs;
BLOCKNUM block;
unsigned int offset;
POSITION fsize;
};
#define ch_bufhead thisfile->buf_next
#define ch_buftail thisfile->buf_prev
#define ch_nbufs thisfile->nbufs
#define ch_block thisfile->block
#define ch_offset thisfile->offset
#define ch_fpos thisfile->fpos
#define ch_fsize thisfile->fsize
#define ch_flags thisfile->flags
#define ch_file thisfile->file
#define END_OF_CHAIN ((struct buf *)&thisfile->buf_next)
#define END_OF_HCHAIN(h) ((struct buf *)&thisfile->hashtbl[h])
#define BUFHASH(blk) ((blk) & (BUFHASH_SIZE-1))
#define FOR_BUFS_IN_CHAIN(h,bp) \
for (bp = thisfile->hashtbl[h].buf_hnext; \
bp != END_OF_HCHAIN(h); bp = bp->hnext)
#define HASH_RM(bp) \
(bp)->hnext->hprev = (bp)->hprev; \
(bp)->hprev->hnext = (bp)->hnext;
#define HASH_INS(bp,h) \
(bp)->hnext = thisfile->hashtbl[h].buf_hnext; \
(bp)->hprev = END_OF_HCHAIN(h); \
thisfile->hashtbl[h].buf_hnext->hprev = (bp); \
thisfile->hashtbl[h].buf_hnext = (bp);
static struct filestate *thisfile;
static int ch_ungotchar = -1;
static int maxbufs = -1;
extern int autobuf;
extern int sigs;
extern int secure;
extern int screen_trashed;
extern int follow_mode;
extern constant char helpdata[];
extern constant int size_helpdata;
extern IFILE curr_ifile;
#if LOGFILE
extern int logfile;
extern char *namelogfile;
#endif
static int ch_addbuf();
#define ch_get() ((ch_block == ch_bufhead->block && \
ch_offset < ch_bufhead->datasize) ? \
ch_bufhead->data[ch_offset] : fch_get())
int
fch_get()
{
register struct buf *bp;
register int n;
register int slept;
register int h;
POSITION pos;
POSITION len;
if (thisfile == NULL)
return (EOI);
slept = FALSE;
h = BUFHASH(ch_block);
FOR_BUFS_IN_CHAIN(h, bp)
{
if (bp->block == ch_block)
{
if (ch_offset >= bp->datasize)
goto read_more;
goto found;
}
}
if (ch_buftail == END_OF_CHAIN || ch_buftail->block != -1)
{
if ((autobuf && !(ch_flags & CH_CANSEEK)) ||
(maxbufs < 0 || ch_nbufs < maxbufs))
if (ch_addbuf())
autobuf = OPT_OFF;
}
bp = ch_buftail;
HASH_RM(bp);
bp->block = ch_block;
bp->datasize = 0;
HASH_INS(bp, h);
read_more:
pos = (ch_block * LBUFSIZE) + bp->datasize;
if ((len = ch_length()) != NULL_POSITION && pos >= len)
return (EOI);
if (pos != ch_fpos)
{
if (!(ch_flags & CH_CANSEEK))
return ('?');
if (lseek(ch_file, (off_t)pos, SEEK_SET) == BAD_LSEEK)
{
error("seek error", NULL_PARG);
clear_eol();
return (EOI);
}
ch_fpos = pos;
}
if (ch_ungotchar != -1)
{
bp->data[bp->datasize] = ch_ungotchar;
n = 1;
ch_ungotchar = -1;
} else if (ch_flags & CH_HELPFILE)
{
bp->data[bp->datasize] = helpdata[ch_fpos];
n = 1;
} else
{
n = iread(ch_file, &bp->data[bp->datasize],
(unsigned int)(LBUFSIZE - bp->datasize));
}
if (n == READ_INTR)
return (EOI);
if (n < 0)
{
#if MSDOS_COMPILER==WIN32C
if (errno != EPIPE)
#endif
{
error("read error", NULL_PARG);
clear_eol();
}
n = 0;
}
#if LOGFILE
if (!secure && logfile >= 0 && n > 0)
write(logfile, (char *) &bp->data[bp->datasize], n);
#endif
ch_fpos += n;
bp->datasize += n;
if (n == 0)
{
ch_fsize = pos;
if (ignore_eoi)
{
if (!slept)
{
PARG parg;
parg.p_string = wait_message();
ierror("%s", &parg);
}
#if !MSDOS_COMPILER
sleep(1);
#else
#if MSDOS_COMPILER==WIN32C
Sleep(1000);
#endif
#endif
slept = TRUE;
#if HAVE_STAT_INO
if (follow_mode == FOLLOW_NAME)
{
struct stat st;
int r = stat(get_filename(curr_ifile), &st);
if (r == 0 && (st.st_ino != curr_ino ||
st.st_dev != curr_dev))
{
screen_trashed = 2;
return (EOI);
}
}
#endif
}
if (sigs)
return (EOI);
}
found:
if (ch_bufhead != bp)
{
bp->next->prev = bp->prev;
bp->prev->next = bp->next;
bp->next = ch_bufhead;
bp->prev = END_OF_CHAIN;
ch_bufhead->prev = bp;
ch_bufhead = bp;
HASH_RM(bp);
HASH_INS(bp, h);
}
if (ch_offset >= bp->datasize)
goto read_more;
return (bp->data[ch_offset]);
}
public void
ch_ungetchar(c)
int c;
{
if (c != -1 && ch_ungotchar != -1)
error("ch_ungetchar overrun", NULL_PARG);
ch_ungotchar = c;
}
#if LOGFILE
public void
end_logfile()
{
static int tried = FALSE;
if (logfile < 0)
return;
if (!tried && ch_fsize == NULL_POSITION)
{
tried = TRUE;
ierror("Finishing logfile", NULL_PARG);
while (ch_forw_get() != EOI)
if (ABORT_SIGS())
break;
}
close(logfile);
logfile = -1;
namelogfile = NULL;
}
public void
sync_logfile()
{
register struct buf *bp;
int warned = FALSE;
BLOCKNUM block;
BLOCKNUM nblocks;
nblocks = (ch_fpos + LBUFSIZE - 1) / LBUFSIZE;
for (block = 0; block < nblocks; block++)
{
for (bp = ch_bufhead; ; bp = bp->next)
{
if (bp == END_OF_CHAIN)
{
if (!warned)
{
error("Warning: log file is incomplete",
NULL_PARG);
warned = TRUE;
}
break;
}
if (bp->block == block)
{
write(logfile, (char *) bp->data, bp->datasize);
break;
}
}
}
}
#endif
static int
buffered(block)
BLOCKNUM block;
{
register struct buf *bp;
register int h;
h = BUFHASH(block);
FOR_BUFS_IN_CHAIN(h, bp)
{
if (bp->block == block)
return (TRUE);
}
return (FALSE);
}
public int
ch_seek(pos)
register POSITION pos;
{
BLOCKNUM new_block;
POSITION len;
if (thisfile == NULL)
return (0);
len = ch_length();
if (pos < ch_zero() || (len != NULL_POSITION && pos > len))
return (1);
new_block = pos / LBUFSIZE;
if (!(ch_flags & CH_CANSEEK) && pos != ch_fpos && !buffered(new_block))
{
if (ch_fpos > pos)
return (1);
while (ch_fpos < pos)
{
if (ch_forw_get() == EOI)
return (1);
if (ABORT_SIGS())
return (1);
}
return (0);
}
ch_block = new_block;
ch_offset = pos % LBUFSIZE;
return (0);
}
public int
ch_end_seek()
{
POSITION len;
if (thisfile == NULL)
return (0);
if (ch_flags & CH_CANSEEK)
ch_fsize = filesize(ch_file);
len = ch_length();
if (len != NULL_POSITION)
return (ch_seek(len));
while (ch_forw_get() != EOI)
if (ABORT_SIGS())
return (1);
return (0);
}
public int
ch_beg_seek()
{
register struct buf *bp, *firstbp;
if (ch_seek(ch_zero()) == 0)
return (0);
firstbp = bp = ch_bufhead;
if (bp == END_OF_CHAIN)
return (1);
while ((bp = bp->next) != END_OF_CHAIN)
if (bp->block < firstbp->block)
firstbp = bp;
ch_block = firstbp->block;
ch_offset = 0;
return (0);
}
public POSITION
ch_length()
{
if (thisfile == NULL)
return (NULL_POSITION);
if (ignore_eoi)
return (NULL_POSITION);
if (ch_flags & CH_HELPFILE)
return (size_helpdata);
return (ch_fsize);
}
public POSITION
ch_tell()
{
if (thisfile == NULL)
return (NULL_POSITION);
return (ch_block * LBUFSIZE) + ch_offset;
}
public int
ch_forw_get()
{
register int c;
if (thisfile == NULL)
return (EOI);
c = ch_get();
if (c == EOI)
return (EOI);
if (ch_offset < LBUFSIZE-1)
ch_offset++;
else
{
ch_block ++;
ch_offset = 0;
}
return (c);
}
public int
ch_back_get()
{
if (thisfile == NULL)
return (EOI);
if (ch_offset > 0)
ch_offset --;
else
{
if (ch_block <= 0)
return (EOI);
if (!(ch_flags & CH_CANSEEK) && !buffered(ch_block-1))
return (EOI);
ch_block--;
ch_offset = LBUFSIZE-1;
}
return (ch_get());
}
public void
ch_setbufspace(bufspace)
int bufspace;
{
if (bufspace < 0)
maxbufs = -1;
else
{
maxbufs = ((bufspace * 1024) + LBUFSIZE-1) / LBUFSIZE;
if (maxbufs < 1)
maxbufs = 1;
}
}
public void
ch_flush()
{
register struct buf *bp;
if (thisfile == NULL)
return;
if (!(ch_flags & CH_CANSEEK))
{
ch_fsize = NULL_POSITION;
return;
}
for (bp = ch_bufhead; bp != END_OF_CHAIN; bp = bp->next)
bp->block = -1;
ch_fsize = filesize(ch_file);
ch_fpos = 0;
ch_block = 0;
ch_offset = 0;
#if 1
if (ch_fsize == 0)
{
ch_fsize = NULL_POSITION;
ch_flags &= ~CH_CANSEEK;
}
#endif
if (lseek(ch_file, (off_t)0, SEEK_SET) == BAD_LSEEK)
{
error("seek error to 0", NULL_PARG);
}
}
static int
ch_addbuf()
{
register struct buf *bp;
bp = (struct buf *) calloc(1, sizeof(struct buf));
if (bp == NULL)
return (1);
ch_nbufs++;
bp->block = -1;
bp->next = END_OF_CHAIN;
bp->prev = ch_buftail;
ch_buftail->next = bp;
ch_buftail = bp;
HASH_INS(bp, 0);
return (0);
}
static void
init_hashtbl()
{
register int h;
for (h = 0; h < BUFHASH_SIZE; h++)
{
thisfile->hashtbl[h].buf_hnext = END_OF_HCHAIN(h);
thisfile->hashtbl[h].buf_hprev = END_OF_HCHAIN(h);
}
}
static void
ch_delbufs()
{
register struct buf *bp;
while (ch_bufhead != END_OF_CHAIN)
{
bp = ch_bufhead;
bp->next->prev = bp->prev;
bp->prev->next = bp->next;
free(bp);
}
ch_nbufs = 0;
init_hashtbl();
}
public int
seekable(f)
int f;
{
#if MSDOS_COMPILER
extern int fd0;
if (f == fd0 && !isatty(fd0))
{
return (0);
}
#endif
return (lseek(f, (off_t)1, SEEK_SET) != BAD_LSEEK);
}
public void
ch_init(f, flags)
int f;
int flags;
{
thisfile = (struct filestate *) get_filestate(curr_ifile);
if (thisfile == NULL)
{
thisfile = (struct filestate *)
calloc(1, sizeof(struct filestate));
thisfile->buf_next = thisfile->buf_prev = END_OF_CHAIN;
thisfile->nbufs = 0;
thisfile->flags = 0;
thisfile->fpos = 0;
thisfile->block = 0;
thisfile->offset = 0;
thisfile->file = -1;
thisfile->fsize = NULL_POSITION;
ch_flags = flags;
init_hashtbl();
if ((flags & CH_CANSEEK) && !seekable(f))
ch_flags &= ~CH_CANSEEK;
set_filestate(curr_ifile, (void *) thisfile);
}
if (thisfile->file == -1)
thisfile->file = f;
ch_flush();
}
public void
ch_close()
{
int keepstate = FALSE;
if (thisfile == NULL)
return;
if (ch_flags & (CH_CANSEEK|CH_POPENED|CH_HELPFILE))
{
ch_delbufs();
} else
keepstate = TRUE;
if (!(ch_flags & CH_KEEPOPEN))
{
if (!(ch_flags & (CH_POPENED|CH_HELPFILE)))
close(ch_file);
ch_file = -1;
} else
keepstate = TRUE;
if (!keepstate)
{
free(thisfile);
thisfile = NULL;
set_filestate(curr_ifile, (void *) NULL);
}
}
public int
ch_getflags()
{
if (thisfile == NULL)
return (0);
return (ch_flags);
}
#if 0
public void
ch_dump(struct filestate *fs)
{
struct buf *bp;
unsigned char *s;
if (fs == NULL)
{
printf(" --no filestate\n");
return;
}
printf(" file %d, flags %x, fpos %x, fsize %x, blk/off %x/%x\n",
fs->file, fs->flags, fs->fpos,
fs->fsize, fs->block, fs->offset);
printf(" %d bufs:\n", fs->nbufs);
for (bp = fs->buf_next; bp != (struct buf *)fs; bp = bp->next)
{
printf("%x: blk %x, size %x \"",
bp, bp->block, bp->datasize);
for (s = bp->data; s < bp->data + 30; s++)
if (*s >= ' ' && *s < 0x7F)
printf("%c", *s);
else
printf(".");
printf("\"\n");
}
}
#endif