2019-08-17 17:09:43 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2008-06-30 21:53:51 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2019-08-09 18:02:27 -04:00
|
|
|
#include <limits.h>
|
2019-08-22 17:38:10 -04:00
|
|
|
#include <math.h>
|
|
|
|
#include <setjmp.h>
|
2019-08-09 18:02:27 -04:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
2019-08-09 07:02:02 -04:00
|
|
|
#include <stdio.h> // for printf
|
2019-08-09 18:02:27 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wchar.h>
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2019-08-09 17:12:33 -04:00
|
|
|
#ifdef _WIN32
|
2008-06-30 21:53:51 -04:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2019-08-21 17:05:57 -04:00
|
|
|
#include <sys/stat.h>
|
2008-06-30 21:53:51 -04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/select.h>
|
2008-08-16 23:47:32 -04:00
|
|
|
#include <fcntl.h>
|
2008-06-30 21:53:51 -04:00
|
|
|
#endif
|
|
|
|
|
2019-08-26 15:12:15 -04:00
|
|
|
#include "scheme.h"
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
#define MOST_OF(x) ((x) - ((x) >> 4))
|
2008-08-16 17:15:36 -04:00
|
|
|
|
2019-08-09 17:02:14 -04:00
|
|
|
static void *our_memrchr(const void *s, int c, size_t n)
|
2009-08-23 21:11:36 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
const unsigned char *src;
|
|
|
|
unsigned char uc;
|
|
|
|
|
|
|
|
src = (unsigned char *)s + n;
|
|
|
|
uc = c;
|
2019-08-09 07:02:02 -04:00
|
|
|
while (--src >= (unsigned char *)s)
|
2009-08-23 21:11:36 -04:00
|
|
|
if (*src == uc)
|
2019-08-09 07:02:02 -04:00
|
|
|
return (void *)src;
|
2009-08-23 21:11:36 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-29 00:35:42 -04:00
|
|
|
#if 0
|
2008-06-30 21:53:51 -04:00
|
|
|
// poll for read, unless forwrite!=0
|
|
|
|
static void _fd_poll(long fd, int forwrite)
|
|
|
|
{
|
2019-08-09 17:12:33 -04:00
|
|
|
#ifndef _WIN32
|
2008-06-30 21:53:51 -04:00
|
|
|
fd_set set;
|
|
|
|
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(fd, &set);
|
|
|
|
if (forwrite)
|
|
|
|
select(fd+1, NULL, &set, NULL, NULL);
|
|
|
|
else
|
|
|
|
select(fd+1, &set, NULL, NULL, NULL);
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
}
|
2013-03-29 00:35:42 -04:00
|
|
|
#endif
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2019-08-18 18:18:36 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
static int _enonfatal(int err)
|
|
|
|
{
|
|
|
|
(void)err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2008-06-30 21:53:51 -04:00
|
|
|
static int _enonfatal(int err)
|
|
|
|
{
|
|
|
|
return (err == EAGAIN || err == EINPROGRESS || err == EINTR ||
|
|
|
|
err == EWOULDBLOCK);
|
|
|
|
}
|
2019-08-18 18:18:36 -04:00
|
|
|
#endif
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
#define SLEEP_TIME 5 // ms
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
// return error code, #bytes read in *nread
|
|
|
|
// these wrappers retry operations until success or a fatal error
|
|
|
|
static int _os_read(long fd, void *buf, size_t n, size_t *nread)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
r = read((int)fd, buf, n);
|
|
|
|
if (r > -1) {
|
|
|
|
*nread = (size_t)r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!_enonfatal(errno)) {
|
|
|
|
*nread = 0;
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
sleep_ms(SLEEP_TIME);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _os_read_all(long fd, void *buf, size_t n, size_t *nread)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
unsigned char *ubuf;
|
2008-06-30 21:53:51 -04:00
|
|
|
size_t got;
|
2019-08-18 18:14:09 -04:00
|
|
|
int err;
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
ubuf = buf;
|
2008-06-30 21:53:51 -04:00
|
|
|
*nread = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
while (n > 0) {
|
2019-08-18 18:14:09 -04:00
|
|
|
err = _os_read(fd, ubuf, n, &got);
|
2008-06-30 21:53:51 -04:00
|
|
|
n -= got;
|
|
|
|
*nread += got;
|
2019-08-18 18:14:09 -04:00
|
|
|
ubuf += got;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (err || got == 0)
|
2008-06-30 21:53:51 -04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _os_write(long fd, void *buf, size_t n, size_t *nwritten)
|
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
r = write((int)fd, buf, n);
|
|
|
|
if (r > -1) {
|
|
|
|
*nwritten = (size_t)r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!_enonfatal(errno)) {
|
|
|
|
*nwritten = 0;
|
|
|
|
return errno;
|
|
|
|
}
|
2008-08-16 17:15:36 -04:00
|
|
|
sleep_ms(SLEEP_TIME);
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _os_write_all(long fd, void *buf, size_t n, size_t *nwritten)
|
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
unsigned char *ubuf;
|
2008-06-30 21:53:51 -04:00
|
|
|
size_t wrote;
|
2019-08-18 18:14:09 -04:00
|
|
|
int err;
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
ubuf = buf;
|
2008-06-30 21:53:51 -04:00
|
|
|
*nwritten = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
while (n > 0) {
|
2019-08-18 18:14:09 -04:00
|
|
|
err = _os_write(fd, ubuf, n, &wrote);
|
2008-06-30 21:53:51 -04:00
|
|
|
n -= wrote;
|
|
|
|
*nwritten += wrote;
|
2019-08-18 18:14:09 -04:00
|
|
|
ubuf += wrote;
|
2008-06-30 21:53:51 -04:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* internal utility functions */
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
static char *_buf_realloc(struct ios *s, size_t sz)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
char *temp;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if ((s->buf == NULL || s->buf == &s->local[0]) && (sz <= IOS_INLSIZE)) {
|
2008-06-30 21:53:51 -04:00
|
|
|
/* TODO: if we want to allow shrinking, see if the buffer shrank
|
|
|
|
down to this size, in which case we need to copy. */
|
|
|
|
s->buf = &s->local[0];
|
|
|
|
s->maxsize = IOS_INLSIZE;
|
|
|
|
s->ownbuf = 1;
|
|
|
|
return s->buf;
|
|
|
|
}
|
2008-12-27 01:02:53 -05:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if (sz <= s->maxsize)
|
|
|
|
return s->buf;
|
2008-12-27 01:02:53 -05:00
|
|
|
|
|
|
|
if (s->ownbuf && s->buf != &s->local[0]) {
|
2008-06-30 21:53:51 -04:00
|
|
|
// if we own the buffer we're free to resize it
|
|
|
|
// always allocate 1 bigger in case user wants to add a NUL
|
|
|
|
// terminator after taking over the buffer
|
2019-08-26 18:25:11 -04:00
|
|
|
temp = realloc(s->buf, sz + 1);
|
2008-06-30 21:53:51 -04:00
|
|
|
if (temp == NULL)
|
|
|
|
return NULL;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2019-08-26 18:25:11 -04:00
|
|
|
temp = malloc(sz + 1);
|
2008-06-30 21:53:51 -04:00
|
|
|
if (temp == NULL)
|
|
|
|
return NULL;
|
|
|
|
s->ownbuf = 1;
|
|
|
|
if (s->size > 0)
|
|
|
|
memcpy(temp, s->buf, s->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->buf = temp;
|
|
|
|
s->maxsize = sz;
|
|
|
|
return s->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write a block of data into the buffer at the current position, resizing
|
|
|
|
// if necessary. returns # written.
|
2019-08-09 12:26:20 -04:00
|
|
|
static size_t _write_grow(struct ios *s, char *data, size_t n)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
size_t amt;
|
|
|
|
size_t newsize;
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (s->bpos + n > s->size) {
|
|
|
|
if (s->bpos + n > s->maxsize) {
|
|
|
|
/* TODO: here you might want to add a mechanism for limiting
|
|
|
|
the growth of the stream. */
|
2008-12-27 01:02:53 -05:00
|
|
|
newsize = s->maxsize ? s->maxsize * 2 : 8;
|
2008-06-30 21:53:51 -04:00
|
|
|
while (s->bpos + n > newsize)
|
|
|
|
newsize *= 2;
|
|
|
|
if (_buf_realloc(s, newsize) == NULL) {
|
|
|
|
/* no more space; write as much as we can */
|
|
|
|
amt = s->maxsize - s->bpos;
|
|
|
|
if (amt > 0) {
|
|
|
|
memcpy(&s->buf[s->bpos], data, amt);
|
|
|
|
}
|
|
|
|
s->bpos += amt;
|
|
|
|
s->size = s->maxsize;
|
|
|
|
return amt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->size = s->bpos + n;
|
|
|
|
}
|
2008-08-16 17:15:36 -04:00
|
|
|
memcpy(s->buf + s->bpos, data, n);
|
2008-06-30 21:53:51 -04:00
|
|
|
s->bpos += n;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* interface functions, low level */
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
static size_t _ios_read(struct ios *s, char *dest, size_t n, int all)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
size_t tot = 0;
|
|
|
|
size_t got, avail;
|
|
|
|
|
|
|
|
while (n > 0) {
|
|
|
|
avail = s->size - s->bpos;
|
2019-08-09 07:02:02 -04:00
|
|
|
|
2008-06-30 21:53:51 -04:00
|
|
|
if (avail > 0) {
|
2010-08-28 01:07:02 -04:00
|
|
|
size_t ncopy = (avail >= n) ? n : avail;
|
|
|
|
memcpy(dest, s->buf + s->bpos, ncopy);
|
|
|
|
s->bpos += ncopy;
|
|
|
|
if (ncopy >= n) {
|
|
|
|
s->state = bst_rd;
|
2019-08-09 07:02:02 -04:00
|
|
|
return tot + ncopy;
|
2010-08-28 01:07:02 -04:00
|
|
|
}
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
if (s->bm == bm_mem || s->fd == -1) {
|
|
|
|
// can't get any more data
|
2010-08-28 01:07:02 -04:00
|
|
|
s->state = bst_rd;
|
|
|
|
if (avail == 0)
|
2009-08-23 01:07:46 -04:00
|
|
|
s->_eof = 1;
|
2008-06-30 21:53:51 -04:00
|
|
|
return avail;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
|
2008-08-28 23:27:59 -04:00
|
|
|
dest += avail;
|
|
|
|
n -= avail;
|
|
|
|
tot += avail;
|
2019-08-09 07:02:02 -04:00
|
|
|
|
2008-08-28 23:27:59 -04:00
|
|
|
ios_flush(s);
|
|
|
|
s->bpos = s->size = 0;
|
|
|
|
s->state = bst_rd;
|
2019-08-09 07:02:02 -04:00
|
|
|
|
2012-01-16 23:31:08 -05:00
|
|
|
s->fpos = -1;
|
2008-08-16 17:15:36 -04:00
|
|
|
if (n > MOST_OF(s->maxsize)) {
|
2008-06-30 21:53:51 -04:00
|
|
|
// doesn't fit comfortably in buffer; go direct
|
|
|
|
if (all)
|
2013-03-29 00:35:42 -04:00
|
|
|
_os_read_all(s->fd, dest, n, &got);
|
2008-06-30 21:53:51 -04:00
|
|
|
else
|
2013-03-29 00:35:42 -04:00
|
|
|
_os_read(s->fd, dest, n, &got);
|
2008-08-16 17:15:36 -04:00
|
|
|
tot += got;
|
2012-01-16 23:31:08 -05:00
|
|
|
if (got == 0)
|
2008-08-16 17:15:36 -04:00
|
|
|
s->_eof = 1;
|
|
|
|
return tot;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2008-06-30 21:53:51 -04:00
|
|
|
// refill buffer
|
|
|
|
if (_os_read(s->fd, s->buf, s->maxsize, &got)) {
|
2008-08-16 17:15:36 -04:00
|
|
|
s->_eof = 1;
|
2008-06-30 21:53:51 -04:00
|
|
|
return tot;
|
|
|
|
}
|
|
|
|
if (got == 0) {
|
2012-01-16 23:31:08 -05:00
|
|
|
s->_eof = 1;
|
|
|
|
return tot;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
s->size = got;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tot;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
size_t ios_read(struct ios *s, char *dest, size_t n)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
|
|
|
return _ios_read(s, dest, n, 0);
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
size_t ios_readall(struct ios *s, char *dest, size_t n)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
|
|
|
return _ios_read(s, dest, n, 1);
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
size_t ios_readprep(struct ios *s, size_t n)
|
2008-08-17 14:16:31 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t got, space;
|
|
|
|
int result;
|
|
|
|
|
2009-08-08 19:43:12 -04:00
|
|
|
if (s->state == bst_wr && s->bm != bm_mem) {
|
|
|
|
ios_flush(s);
|
|
|
|
s->bpos = s->size = 0;
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
space = s->size - s->bpos;
|
2009-07-21 23:50:57 -04:00
|
|
|
s->state = bst_rd;
|
2008-08-17 14:16:31 -04:00
|
|
|
if (space >= n || s->bm == bm_mem || s->fd == -1)
|
|
|
|
return space;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (s->maxsize < s->bpos + n) {
|
2009-03-02 22:16:30 -05:00
|
|
|
// it won't fit. grow buffer or move data back.
|
2019-08-09 07:02:02 -04:00
|
|
|
if (n <= s->maxsize && space <= ((s->maxsize) >> 2)) {
|
2009-03-02 22:16:30 -05:00
|
|
|
if (space)
|
2019-08-09 07:02:02 -04:00
|
|
|
memmove(s->buf, s->buf + s->bpos, space);
|
2009-03-02 22:16:30 -05:00
|
|
|
s->size -= s->bpos;
|
|
|
|
s->bpos = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
if (_buf_realloc(s, s->bpos + n) == NULL)
|
2009-03-02 22:16:30 -05:00
|
|
|
return space;
|
|
|
|
}
|
2008-08-17 14:16:31 -04:00
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
result = _os_read(s->fd, s->buf + s->size, s->maxsize - s->size, &got);
|
2008-08-17 14:16:31 -04:00
|
|
|
if (result)
|
|
|
|
return space;
|
|
|
|
s->size += got;
|
|
|
|
return s->size - s->bpos;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
static void _write_update_pos(struct ios *s)
|
2008-08-28 23:27:59 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
if (s->bpos > s->ndirty)
|
|
|
|
s->ndirty = s->bpos;
|
|
|
|
if (s->bpos > s->size)
|
|
|
|
s->size = s->bpos;
|
2008-08-28 23:27:59 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
size_t ios_write(struct ios *s, char *data, size_t n)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t space, wrote;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if (s->readonly)
|
|
|
|
return 0;
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
2008-08-16 17:15:36 -04:00
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
wrote = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (s->state == bst_none)
|
|
|
|
s->state = bst_wr;
|
2010-08-28 01:07:02 -04:00
|
|
|
if (s->state == bst_rd) {
|
|
|
|
if (!s->rereadable) {
|
|
|
|
s->size = 0;
|
|
|
|
s->bpos = 0;
|
|
|
|
}
|
2008-08-16 17:15:36 -04:00
|
|
|
space = s->size - s->bpos;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2010-08-28 01:07:02 -04:00
|
|
|
space = s->maxsize - s->bpos;
|
|
|
|
}
|
2008-08-16 17:15:36 -04:00
|
|
|
|
|
|
|
if (s->bm == bm_mem) {
|
2008-08-28 23:27:59 -04:00
|
|
|
wrote = _write_grow(s, data, n);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (s->bm == bm_none) {
|
2012-01-16 23:31:08 -05:00
|
|
|
s->fpos = -1;
|
2010-05-02 14:28:53 -04:00
|
|
|
_os_write_all(s->fd, data, n, &wrote);
|
2008-08-16 17:15:36 -04:00
|
|
|
return wrote;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (n <= space) {
|
2008-08-16 23:47:32 -04:00
|
|
|
if (s->bm == bm_line) {
|
|
|
|
char *nl;
|
2019-08-09 17:02:14 -04:00
|
|
|
if ((nl = (char *)our_memrchr(data, '\n', n)) != NULL) {
|
2019-08-09 07:02:02 -04:00
|
|
|
size_t linesz = nl - data + 1;
|
2008-08-16 23:47:32 -04:00
|
|
|
s->bm = bm_block;
|
|
|
|
wrote += ios_write(s, data, linesz);
|
|
|
|
ios_flush(s);
|
|
|
|
s->bm = bm_line;
|
|
|
|
n -= linesz;
|
|
|
|
data += linesz;
|
|
|
|
}
|
|
|
|
}
|
2008-08-16 17:15:36 -04:00
|
|
|
memcpy(s->buf + s->bpos, data, n);
|
|
|
|
s->bpos += n;
|
2008-08-16 23:47:32 -04:00
|
|
|
wrote += n;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2008-08-16 17:15:36 -04:00
|
|
|
s->state = bst_wr;
|
|
|
|
ios_flush(s);
|
|
|
|
if (n > MOST_OF(s->maxsize)) {
|
2010-05-02 14:28:53 -04:00
|
|
|
_os_write_all(s->fd, data, n, &wrote);
|
2008-08-16 17:15:36 -04:00
|
|
|
return wrote;
|
|
|
|
}
|
|
|
|
return ios_write(s, data, n);
|
|
|
|
}
|
2008-08-28 23:27:59 -04:00
|
|
|
_write_update_pos(s);
|
2008-08-16 17:15:36 -04:00
|
|
|
return wrote;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
off_t ios_seek(struct ios *s, off_t pos)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
off_t fdpos;
|
|
|
|
|
2008-08-16 17:15:36 -04:00
|
|
|
s->_eof = 0;
|
2008-08-16 23:47:32 -04:00
|
|
|
if (s->bm == bm_mem) {
|
|
|
|
if ((size_t)pos > s->size)
|
|
|
|
return -1;
|
|
|
|
s->bpos = pos;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2012-01-16 23:31:08 -05:00
|
|
|
ios_flush(s);
|
2019-08-18 18:14:09 -04:00
|
|
|
fdpos = lseek(s->fd, pos, SEEK_SET);
|
2012-01-16 23:31:08 -05:00
|
|
|
if (fdpos == (off_t)-1)
|
|
|
|
return fdpos;
|
|
|
|
s->bpos = s->size = 0;
|
|
|
|
}
|
2010-05-02 14:28:53 -04:00
|
|
|
return 0;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
off_t ios_seek_end(struct ios *s)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
off_t fdpos;
|
|
|
|
|
2008-08-16 17:15:36 -04:00
|
|
|
s->_eof = 1;
|
2012-01-16 23:31:08 -05:00
|
|
|
if (s->bm == bm_mem) {
|
|
|
|
s->bpos = s->size;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2012-01-16 23:31:08 -05:00
|
|
|
ios_flush(s);
|
2019-08-18 18:14:09 -04:00
|
|
|
fdpos = lseek(s->fd, 0, SEEK_END);
|
2012-01-16 23:31:08 -05:00
|
|
|
if (fdpos == (off_t)-1)
|
|
|
|
return fdpos;
|
|
|
|
s->bpos = s->size = 0;
|
|
|
|
}
|
2010-05-02 14:28:53 -04:00
|
|
|
return 0;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
off_t ios_skip(struct ios *s, off_t offs)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
off_t fdpos;
|
|
|
|
|
2012-01-16 23:31:08 -05:00
|
|
|
if (offs != 0) {
|
|
|
|
if (offs > 0) {
|
2019-08-09 07:02:02 -04:00
|
|
|
if (offs <= (off_t)(s->size - s->bpos)) {
|
2012-01-16 23:31:08 -05:00
|
|
|
s->bpos += offs;
|
|
|
|
return 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (s->bm == bm_mem) {
|
2012-01-16 23:31:08 -05:00
|
|
|
// TODO: maybe grow buffer
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (offs < 0) {
|
2012-01-16 23:31:08 -05:00
|
|
|
if (-offs <= (off_t)s->bpos) {
|
|
|
|
s->bpos += offs;
|
|
|
|
s->_eof = 0;
|
|
|
|
return 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (s->bm == bm_mem) {
|
2012-01-16 23:31:08 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ios_flush(s);
|
|
|
|
if (s->state == bst_wr)
|
|
|
|
offs += s->bpos;
|
|
|
|
else if (s->state == bst_rd)
|
|
|
|
offs -= (s->size - s->bpos);
|
2019-08-18 18:14:09 -04:00
|
|
|
fdpos = lseek(s->fd, offs, SEEK_CUR);
|
2012-01-16 23:31:08 -05:00
|
|
|
if (fdpos == (off_t)-1)
|
|
|
|
return fdpos;
|
|
|
|
s->bpos = s->size = 0;
|
2008-08-16 17:15:36 -04:00
|
|
|
s->_eof = 0;
|
2012-01-16 23:31:08 -05:00
|
|
|
}
|
2010-05-02 14:28:53 -04:00
|
|
|
return 0;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
off_t ios_pos(struct ios *s)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
off_t fdpos;
|
|
|
|
|
2008-06-30 21:53:51 -04:00
|
|
|
if (s->bm == bm_mem)
|
|
|
|
return (off_t)s->bpos;
|
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
fdpos = s->fpos;
|
2012-01-16 23:31:08 -05:00
|
|
|
if (fdpos == (off_t)-1) {
|
|
|
|
fdpos = lseek(s->fd, 0, SEEK_CUR);
|
|
|
|
if (fdpos == (off_t)-1)
|
|
|
|
return fdpos;
|
|
|
|
s->fpos = fdpos;
|
|
|
|
}
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
if (s->state == bst_wr)
|
|
|
|
fdpos += s->bpos;
|
|
|
|
else if (s->state == bst_rd)
|
|
|
|
fdpos -= (s->size - s->bpos);
|
|
|
|
return fdpos;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
size_t ios_trunc(struct ios *s, size_t size)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2008-08-16 17:15:36 -04:00
|
|
|
if (s->bm == bm_mem) {
|
|
|
|
if (size == s->size)
|
|
|
|
return s->size;
|
|
|
|
if (size < s->size) {
|
|
|
|
if (s->bpos > size)
|
|
|
|
s->bpos = size;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
if (_buf_realloc(s, size) == NULL)
|
2008-08-16 17:15:36 -04:00
|
|
|
return s->size;
|
|
|
|
}
|
|
|
|
s->size = size;
|
|
|
|
return size;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
// todo
|
2008-08-16 17:15:36 -04:00
|
|
|
return 0;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_eof(struct ios *s)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
if (s->bm == bm_mem)
|
2009-08-23 01:07:46 -04:00
|
|
|
return (s->_eof ? 1 : 0);
|
2008-06-30 21:53:51 -04:00
|
|
|
if (s->fd == -1)
|
|
|
|
return 1;
|
2008-08-16 17:15:36 -04:00
|
|
|
if (s->_eof)
|
|
|
|
return 1;
|
2008-08-17 14:16:31 -04:00
|
|
|
return 0;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_flush(struct ios *s)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t nw, ntowrite;
|
|
|
|
int err;
|
|
|
|
|
2008-06-30 21:53:51 -04:00
|
|
|
if (s->ndirty == 0 || s->bm == bm_mem || s->buf == NULL)
|
|
|
|
return 0;
|
|
|
|
if (s->fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (s->state == bst_rd) {
|
|
|
|
if (lseek(s->fd, -(off_t)s->size, SEEK_CUR) == (off_t)-1) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 18:14:09 -04:00
|
|
|
ntowrite = s->ndirty;
|
2012-01-16 23:31:08 -05:00
|
|
|
s->fpos = -1;
|
2019-08-18 18:14:09 -04:00
|
|
|
err = _os_write_all(s->fd, s->buf, ntowrite, &nw);
|
2008-06-30 21:53:51 -04:00
|
|
|
// todo: try recovering from some kinds of errors (e.g. retry)
|
|
|
|
|
|
|
|
if (s->state == bst_rd) {
|
|
|
|
if (lseek(s->fd, s->size - nw, SEEK_CUR) == (off_t)-1) {
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (s->state == bst_wr) {
|
2008-08-16 17:15:36 -04:00
|
|
|
if (s->bpos != nw &&
|
|
|
|
lseek(s->fd, (off_t)s->bpos - (off_t)nw, SEEK_CUR) == (off_t)-1) {
|
|
|
|
}
|
2012-01-16 23:31:08 -05:00
|
|
|
// now preserve the invariant that data to write
|
|
|
|
// begins at the beginning of the buffer, and s->size refers
|
|
|
|
// to how much valid file data is stored in the buffer.
|
|
|
|
if (s->size > s->ndirty) {
|
|
|
|
size_t delta = s->size - s->ndirty;
|
|
|
|
memmove(s->buf, s->buf + s->ndirty, delta);
|
|
|
|
}
|
|
|
|
s->size -= s->ndirty;
|
|
|
|
s->bpos = 0;
|
2008-08-16 17:15:36 -04:00
|
|
|
}
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
s->ndirty = 0;
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (nw < ntowrite)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
void ios_close(struct ios *s)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
ios_flush(s);
|
|
|
|
if (s->fd != -1 && s->ownfd)
|
|
|
|
close(s->fd);
|
|
|
|
s->fd = -1;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (s->buf != NULL && s->ownbuf && s->buf != &s->local[0])
|
2019-08-26 18:25:11 -04:00
|
|
|
free(s->buf);
|
2009-02-18 22:31:40 -05:00
|
|
|
s->buf = NULL;
|
|
|
|
s->size = s->maxsize = s->bpos = 0;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
static void _buf_init(struct ios *s, bufmode_t bm)
|
2008-08-16 17:15:36 -04:00
|
|
|
{
|
|
|
|
s->bm = bm;
|
|
|
|
if (s->bm == bm_mem || s->bm == bm_none) {
|
|
|
|
s->buf = &s->local[0];
|
|
|
|
s->maxsize = IOS_INLSIZE;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2008-08-16 17:15:36 -04:00
|
|
|
s->buf = NULL;
|
|
|
|
_buf_realloc(s, IOS_BUFSIZE);
|
|
|
|
}
|
|
|
|
s->size = s->bpos = 0;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
char *ios_takebuf(struct ios *s, size_t *psize)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
ios_flush(s);
|
|
|
|
|
|
|
|
if (s->buf == &s->local[0]) {
|
2019-08-26 18:25:11 -04:00
|
|
|
buf = malloc(s->size + 1);
|
2008-06-30 21:53:51 -04:00
|
|
|
if (buf == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (s->size)
|
|
|
|
memcpy(buf, s->buf, s->size);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2008-06-30 21:53:51 -04:00
|
|
|
buf = s->buf;
|
|
|
|
}
|
2010-12-23 01:49:37 -05:00
|
|
|
buf[s->size] = '\0';
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
*psize = s->size + 1; // buffer is actually 1 bigger for terminating NUL
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
/* empty stream and reinitialize */
|
2008-08-16 17:15:36 -04:00
|
|
|
_buf_init(s, s->bm);
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_setbuf(struct ios *s, char *buf, size_t size, int own)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
size_t nvalid = 0;
|
2019-08-18 18:14:09 -04:00
|
|
|
ios_flush(s);
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
nvalid = (size < s->size) ? size : s->size;
|
2009-03-01 23:26:16 -05:00
|
|
|
if (nvalid > 0)
|
|
|
|
memcpy(buf, s->buf, nvalid);
|
2008-06-30 21:53:51 -04:00
|
|
|
if (s->bpos > nvalid) {
|
|
|
|
// truncated
|
|
|
|
s->bpos = nvalid;
|
|
|
|
}
|
|
|
|
s->size = nvalid;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if (s->buf != NULL && s->ownbuf && s->buf != &s->local[0])
|
2019-08-26 18:25:11 -04:00
|
|
|
free(s->buf);
|
2008-06-30 21:53:51 -04:00
|
|
|
s->buf = buf;
|
|
|
|
s->maxsize = size;
|
|
|
|
s->ownbuf = own;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_bufmode(struct ios *s, bufmode_t mode)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
|
|
|
// no fd; can only do mem-only buffering
|
|
|
|
if (s->fd == -1 && mode != bm_mem)
|
|
|
|
return -1;
|
|
|
|
s->bm = mode;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
void ios_set_readonly(struct ios *s)
|
2009-03-01 23:26:16 -05:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
if (s->readonly)
|
|
|
|
return;
|
2009-03-01 23:26:16 -05:00
|
|
|
ios_flush(s);
|
|
|
|
s->state = bst_none;
|
|
|
|
s->readonly = 1;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
static size_t ios_copy_(struct ios *to, struct ios *from, size_t nbytes,
|
2019-08-26 18:17:52 -04:00
|
|
|
int all)
|
2008-08-16 17:15:36 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t total, avail, written, ntowrite;
|
|
|
|
|
|
|
|
total = 0;
|
2009-08-08 19:43:12 -04:00
|
|
|
if (!ios_eof(from)) {
|
|
|
|
do {
|
2019-08-09 07:02:02 -04:00
|
|
|
avail = ios_readprep(from, IOS_BUFSIZE / 2);
|
2009-08-08 19:43:12 -04:00
|
|
|
if (avail == 0) {
|
|
|
|
from->_eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ntowrite = (avail <= nbytes || all) ? avail : nbytes;
|
2019-08-09 07:02:02 -04:00
|
|
|
written = ios_write(to, from->buf + from->bpos, ntowrite);
|
2009-08-08 19:43:12 -04:00
|
|
|
// TODO: should this be +=written instead?
|
|
|
|
from->bpos += ntowrite;
|
|
|
|
total += written;
|
|
|
|
if (!all) {
|
|
|
|
nbytes -= written;
|
|
|
|
if (nbytes == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (written < ntowrite)
|
|
|
|
break;
|
|
|
|
} while (!ios_eof(from));
|
|
|
|
}
|
|
|
|
return total;
|
2008-08-16 17:15:36 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
size_t ios_copy(struct ios *to, struct ios *from, size_t nbytes)
|
2008-08-16 17:15:36 -04:00
|
|
|
{
|
|
|
|
return ios_copy_(to, from, nbytes, 0);
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
size_t ios_copyall(struct ios *to, struct ios *from)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2008-08-16 17:15:36 -04:00
|
|
|
return ios_copy_(to, from, 0, 1);
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2009-03-02 22:16:30 -05:00
|
|
|
#define LINE_CHUNK_SIZE 160
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
size_t ios_copyuntil(struct ios *to, struct ios *from, char delim)
|
2009-03-02 22:16:30 -05:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t total, avail, ntowrite, written;
|
|
|
|
char *pd;
|
|
|
|
int first;
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
avail = from->size - from->bpos;
|
|
|
|
first = 1;
|
2009-03-02 22:16:30 -05:00
|
|
|
if (!ios_eof(from)) {
|
|
|
|
do {
|
2012-01-16 23:31:08 -05:00
|
|
|
if (avail == 0) {
|
|
|
|
first = 0;
|
|
|
|
avail = ios_readprep(from, LINE_CHUNK_SIZE);
|
|
|
|
}
|
2019-08-18 18:14:09 -04:00
|
|
|
pd = (char *)memchr(from->buf + from->bpos, delim, avail);
|
2009-03-02 22:16:30 -05:00
|
|
|
if (pd == NULL) {
|
2019-08-09 07:02:02 -04:00
|
|
|
written = ios_write(to, from->buf + from->bpos, avail);
|
2009-03-02 22:16:30 -05:00
|
|
|
from->bpos += avail;
|
|
|
|
total += written;
|
2012-01-16 23:31:08 -05:00
|
|
|
avail = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2019-08-18 18:14:09 -04:00
|
|
|
ntowrite = pd - (from->buf + from->bpos) + 1;
|
2019-08-09 07:02:02 -04:00
|
|
|
written = ios_write(to, from->buf + from->bpos, ntowrite);
|
2009-03-02 22:16:30 -05:00
|
|
|
from->bpos += ntowrite;
|
|
|
|
total += written;
|
|
|
|
return total;
|
|
|
|
}
|
2012-01-16 23:31:08 -05:00
|
|
|
} while (!ios_eof(from) && (first || avail >= LINE_CHUNK_SIZE));
|
2009-03-02 22:16:30 -05:00
|
|
|
}
|
|
|
|
from->_eof = 1;
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
static void _ios_init(struct ios *s)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
|
|
|
// put all fields in a sane initial state
|
|
|
|
s->bm = bm_block;
|
|
|
|
s->state = bst_none;
|
|
|
|
s->errcode = 0;
|
|
|
|
s->buf = NULL;
|
|
|
|
s->maxsize = 0;
|
|
|
|
s->size = 0;
|
|
|
|
s->bpos = 0;
|
|
|
|
s->ndirty = 0;
|
2012-01-16 23:31:08 -05:00
|
|
|
s->fpos = -1;
|
|
|
|
s->lineno = 1;
|
2008-08-16 23:47:32 -04:00
|
|
|
s->fd = -1;
|
2008-08-28 23:27:59 -04:00
|
|
|
s->ownbuf = 1;
|
2008-08-16 23:47:32 -04:00
|
|
|
s->ownfd = 0;
|
|
|
|
s->_eof = 0;
|
|
|
|
s->rereadable = 0;
|
2009-03-04 22:48:17 -05:00
|
|
|
s->readonly = 0;
|
2008-08-16 23:47:32 -04:00
|
|
|
}
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
/* stream object initializers. we do no allocation. */
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
struct ios *ios_file(struct ios *s, char *fname, int rd, int wr, int create,
|
|
|
|
int trunc)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
int fd, flags;
|
|
|
|
|
2009-02-08 22:22:31 -05:00
|
|
|
if (!(rd || wr))
|
|
|
|
// must specify read and/or write
|
|
|
|
goto open_file_err;
|
2019-08-18 18:14:09 -04:00
|
|
|
flags = wr ? (rd ? O_RDWR : O_WRONLY) : O_RDONLY;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (create)
|
|
|
|
flags |= O_CREAT;
|
|
|
|
if (trunc)
|
|
|
|
flags |= O_TRUNC;
|
|
|
|
fd = open(fname, flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /*644*/);
|
2014-05-22 18:09:49 -04:00
|
|
|
s = ios_fd(s, fd, 1, 1);
|
2009-02-08 22:22:31 -05:00
|
|
|
if (fd == -1)
|
|
|
|
goto open_file_err;
|
2009-03-01 23:26:16 -05:00
|
|
|
if (!wr)
|
|
|
|
s->readonly = 1;
|
2008-08-16 23:47:32 -04:00
|
|
|
return s;
|
2019-08-09 07:02:02 -04:00
|
|
|
open_file_err:
|
2009-02-08 22:22:31 -05:00
|
|
|
s->fd = -1;
|
|
|
|
return NULL;
|
2008-06-30 21:53:51 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
struct ios *ios_mem(struct ios *s, size_t initsize)
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2008-08-16 23:47:32 -04:00
|
|
|
_ios_init(s);
|
|
|
|
s->bm = bm_mem;
|
|
|
|
_buf_realloc(s, initsize);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
struct ios *ios_str(struct ios *s, char *str)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t n;
|
|
|
|
|
|
|
|
n = strlen(str);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (ios_mem(s, n + 1) == NULL)
|
|
|
|
return NULL;
|
|
|
|
ios_write(s, str, n + 1);
|
2008-08-16 23:47:32 -04:00
|
|
|
ios_seek(s, 0);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
struct ios *ios_static_buffer(struct ios *s, char *buf, size_t sz)
|
2010-08-28 01:07:02 -04:00
|
|
|
{
|
|
|
|
ios_mem(s, 0);
|
|
|
|
ios_setbuf(s, buf, sz, 0);
|
|
|
|
s->size = sz;
|
|
|
|
ios_set_readonly(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
struct ios *ios_fd(struct ios *s, long fd, int isfile, int own)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
|
|
|
_ios_init(s);
|
|
|
|
s->fd = fd;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (isfile)
|
|
|
|
s->rereadable = 1;
|
2008-08-16 23:47:32 -04:00
|
|
|
_buf_init(s, bm_block);
|
2012-01-16 23:31:08 -05:00
|
|
|
s->ownfd = own;
|
|
|
|
if (fd == STDERR_FILENO)
|
|
|
|
s->bm = bm_none;
|
2008-08-16 23:47:32 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
struct ios *ios_stdin = NULL;
|
|
|
|
struct ios *ios_stdout = NULL;
|
|
|
|
struct ios *ios_stderr = NULL;
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2008-08-17 14:16:31 -04:00
|
|
|
void ios_init_stdstreams()
|
2008-06-30 21:53:51 -04:00
|
|
|
{
|
2019-08-26 18:25:11 -04:00
|
|
|
ios_stdin = malloc(sizeof(struct ios));
|
2012-01-16 23:31:08 -05:00
|
|
|
ios_fd(ios_stdin, STDIN_FILENO, 0, 0);
|
2008-06-30 21:53:51 -04:00
|
|
|
|
2019-08-26 18:25:11 -04:00
|
|
|
ios_stdout = malloc(sizeof(struct ios));
|
2012-01-16 23:31:08 -05:00
|
|
|
ios_fd(ios_stdout, STDOUT_FILENO, 0, 0);
|
2008-08-17 14:16:31 -04:00
|
|
|
ios_stdout->bm = bm_line;
|
|
|
|
|
2019-08-26 18:25:11 -04:00
|
|
|
ios_stderr = malloc(sizeof(struct ios));
|
2012-01-16 23:31:08 -05:00
|
|
|
ios_fd(ios_stderr, STDERR_FILENO, 0, 0);
|
2008-08-17 14:16:31 -04:00
|
|
|
ios_stderr->bm = bm_none;
|
2008-08-16 23:47:32 -04:00
|
|
|
}
|
2008-06-30 21:53:51 -04:00
|
|
|
|
|
|
|
/* higher level interface */
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_putc(int c, struct ios *s)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
|
|
|
char ch = (char)c;
|
|
|
|
|
2008-08-28 23:27:59 -04:00
|
|
|
if (s->state == bst_wr && s->bpos < s->maxsize && s->bm != bm_none) {
|
|
|
|
s->buf[s->bpos++] = ch;
|
|
|
|
_write_update_pos(s);
|
|
|
|
if (s->bm == bm_line && ch == '\n')
|
|
|
|
ios_flush(s);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-08-16 23:47:32 -04:00
|
|
|
return (int)ios_write(s, &ch, 1);
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_getc(struct ios *s)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
|
|
|
char ch;
|
2019-08-18 18:14:09 -04:00
|
|
|
|
2010-08-28 01:07:02 -04:00
|
|
|
if (s->state == bst_rd && s->bpos < s->size) {
|
|
|
|
ch = s->buf[s->bpos++];
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
|
|
|
if (s->_eof)
|
|
|
|
return IOS_EOF;
|
2010-08-28 01:07:02 -04:00
|
|
|
if (ios_read(s, &ch, 1) < 1)
|
|
|
|
return IOS_EOF;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
if (ch == '\n')
|
|
|
|
s->lineno++;
|
2010-08-28 01:07:02 -04:00
|
|
|
return (unsigned char)ch;
|
2008-08-16 23:47:32 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_peekc(struct ios *s)
|
2008-11-05 23:04:04 -05:00
|
|
|
{
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t n;
|
|
|
|
|
2008-11-05 23:04:04 -05:00
|
|
|
if (s->bpos < s->size)
|
2010-08-28 01:07:02 -04:00
|
|
|
return (unsigned char)s->buf[s->bpos];
|
2019-08-09 07:02:02 -04:00
|
|
|
if (s->_eof)
|
|
|
|
return IOS_EOF;
|
2019-08-18 18:14:09 -04:00
|
|
|
n = ios_readprep(s, 1);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (n == 0)
|
|
|
|
return IOS_EOF;
|
2010-08-28 01:07:02 -04:00
|
|
|
return (unsigned char)s->buf[s->bpos];
|
2008-11-05 23:04:04 -05:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_ungetc(int c, struct ios *s)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
|
|
|
if (s->state == bst_wr)
|
|
|
|
return IOS_EOF;
|
|
|
|
if (s->bpos > 0) {
|
|
|
|
s->bpos--;
|
|
|
|
s->buf[s->bpos] = (char)c;
|
2009-08-23 01:07:46 -04:00
|
|
|
s->_eof = 0;
|
2008-08-16 23:47:32 -04:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
if (s->size == s->maxsize) {
|
2019-08-09 07:02:02 -04:00
|
|
|
if (_buf_realloc(s, s->maxsize * 2) == NULL)
|
2008-08-16 23:47:32 -04:00
|
|
|
return IOS_EOF;
|
|
|
|
}
|
|
|
|
memmove(s->buf + 1, s->buf, s->size);
|
|
|
|
s->buf[0] = (char)c;
|
|
|
|
s->size++;
|
2009-08-23 01:07:46 -04:00
|
|
|
s->_eof = 0;
|
2008-08-16 23:47:32 -04:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_getutf8(struct ios *s, uint32_t *pwc)
|
2008-08-17 14:16:31 -04:00
|
|
|
{
|
|
|
|
int c;
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t sz, i;
|
2008-08-17 14:16:31 -04:00
|
|
|
char c0;
|
|
|
|
char buf[8];
|
|
|
|
|
|
|
|
c = ios_getc(s);
|
|
|
|
if (c == IOS_EOF)
|
|
|
|
return IOS_EOF;
|
|
|
|
c0 = (char)c;
|
2010-08-28 01:07:02 -04:00
|
|
|
if ((unsigned char)c0 < 0x80) {
|
|
|
|
*pwc = (uint32_t)(unsigned char)c0;
|
2008-08-17 14:16:31 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
sz = u8_seqlen(&c0) - 1;
|
2008-08-17 14:16:31 -04:00
|
|
|
if (ios_ungetc(c, s) == IOS_EOF)
|
|
|
|
return IOS_EOF;
|
|
|
|
if (ios_readprep(s, sz) < sz)
|
|
|
|
// NOTE: this can return EOF even if some bytes are available
|
|
|
|
return IOS_EOF;
|
2019-08-18 18:14:09 -04:00
|
|
|
i = s->bpos;
|
2008-08-17 14:16:31 -04:00
|
|
|
*pwc = u8_nextchar(s->buf, &i);
|
2019-08-09 07:02:02 -04:00
|
|
|
ios_read(s, buf, sz + 1);
|
2008-08-17 14:16:31 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_peekutf8(struct ios *s, uint32_t *pwc)
|
2009-08-23 01:07:46 -04:00
|
|
|
{
|
|
|
|
int c;
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t sz, i;
|
2009-08-23 01:07:46 -04:00
|
|
|
char c0;
|
|
|
|
|
|
|
|
c = ios_peekc(s);
|
|
|
|
if (c == IOS_EOF)
|
|
|
|
return IOS_EOF;
|
|
|
|
c0 = (char)c;
|
2010-08-28 01:07:02 -04:00
|
|
|
if ((unsigned char)c0 < 0x80) {
|
|
|
|
*pwc = (uint32_t)(unsigned char)c0;
|
2009-08-23 01:07:46 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
sz = u8_seqlen(&c0) - 1;
|
2009-08-23 01:07:46 -04:00
|
|
|
if (ios_readprep(s, sz) < sz)
|
|
|
|
return IOS_EOF;
|
2019-08-18 18:14:09 -04:00
|
|
|
i = s->bpos;
|
2009-08-23 01:07:46 -04:00
|
|
|
*pwc = u8_nextchar(s->buf, &i);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
int ios_pututf8(struct ios *s, uint32_t wc)
|
2009-03-04 22:48:17 -05:00
|
|
|
{
|
|
|
|
char buf[8];
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t n;
|
|
|
|
|
2009-08-09 00:04:31 -04:00
|
|
|
if (wc < 0x80)
|
|
|
|
return ios_putc((int)wc, s);
|
2019-08-18 18:14:09 -04:00
|
|
|
n = u8_toutf8(buf, 8, &wc, 1);
|
2009-03-04 22:48:17 -05:00
|
|
|
return ios_write(s, buf, n);
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
void ios_purge(struct ios *s)
|
2008-11-05 23:04:04 -05:00
|
|
|
{
|
|
|
|
if (s->state == bst_rd) {
|
|
|
|
s->bpos = s->size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:20 -04:00
|
|
|
char *ios_readline(struct ios *s)
|
2012-01-16 23:31:08 -05:00
|
|
|
{
|
2019-08-09 12:26:20 -04:00
|
|
|
struct ios dest;
|
2019-08-18 18:14:09 -04:00
|
|
|
size_t n;
|
|
|
|
|
2012-01-16 23:31:08 -05:00
|
|
|
ios_mem(&dest, 0);
|
|
|
|
ios_copyuntil(&dest, s, '\n');
|
|
|
|
return ios_takebuf(&dest, &n);
|
|
|
|
}
|
|
|
|
|
2019-08-22 17:38:10 -04:00
|
|
|
int ios_printf(struct ios *s, const char *format, ...)
|
2008-08-16 23:47:32 -04:00
|
|
|
{
|
2019-08-22 17:38:10 -04:00
|
|
|
static char purkka[4096];
|
|
|
|
va_list args;
|
2019-08-21 15:10:13 -04:00
|
|
|
int len;
|
2008-08-16 23:47:32 -04:00
|
|
|
|
2019-08-22 17:38:10 -04:00
|
|
|
va_start(args, format);
|
|
|
|
ios_flush(s);
|
|
|
|
memset(purkka, 0, sizeof(purkka));
|
|
|
|
len = vsnprintf(purkka, sizeof(purkka), format, args);
|
|
|
|
if (len < 0) {
|
2019-08-21 15:10:13 -04:00
|
|
|
len = 0;
|
2010-02-24 23:37:33 -05:00
|
|
|
}
|
2019-08-22 17:40:41 -04:00
|
|
|
if (len >= (int)sizeof(purkka)) {
|
2019-08-22 17:38:10 -04:00
|
|
|
lerror(MemoryError, "out of memory in ios_printf buffer");
|
2019-08-21 15:10:13 -04:00
|
|
|
}
|
2019-08-22 17:38:10 -04:00
|
|
|
ios_write(s, purkka, len);
|
2010-02-24 23:37:33 -05:00
|
|
|
va_end(args);
|
2019-08-22 17:38:10 -04:00
|
|
|
return len;
|
2008-08-16 23:47:32 -04:00
|
|
|
}
|