2018-01-12 15:41:27 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2018-01-12 15:41:27 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
#define _DEFAULT_SOURCE
|
|
|
|
#include <stdio.h>
|
2018-01-12 15:41:27 +00:00
|
|
|
#include <dst/dst.h>
|
2018-05-11 12:13:26 +00:00
|
|
|
#include <errno.h>
|
2018-01-06 16:09:15 +00:00
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
#define IO_WRITE 1
|
|
|
|
#define IO_READ 2
|
|
|
|
#define IO_APPEND 4
|
|
|
|
#define IO_UPDATE 8
|
|
|
|
#define IO_NOT_CLOSEABLE 16
|
|
|
|
#define IO_CLOSED 32
|
|
|
|
#define IO_BINARY 64
|
|
|
|
#define IO_SERIALIZABLE 128
|
2018-05-11 12:13:26 +00:00
|
|
|
#define IO_PIPED 256
|
2018-01-16 01:14:21 +00:00
|
|
|
|
|
|
|
typedef struct IOFile IOFile;
|
|
|
|
struct IOFile {
|
|
|
|
FILE *file;
|
|
|
|
int flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int dst_io_gc(void *p, size_t len);
|
|
|
|
|
|
|
|
DstAbstractType dst_io_filetype = {
|
2018-02-03 22:22:04 +00:00
|
|
|
":core.file",
|
2018-01-17 04:18:45 +00:00
|
|
|
dst_io_gc,
|
|
|
|
NULL
|
2018-01-06 16:09:15 +00:00
|
|
|
};
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
/* Check argupments to fopen */
|
|
|
|
static int checkflags(const uint8_t *str, int32_t len) {
|
|
|
|
int flags = 0;
|
|
|
|
int32_t i;
|
|
|
|
if (!len || len > 3) return -1;
|
|
|
|
switch (*str) {
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
case 'w':
|
|
|
|
flags |= IO_WRITE;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
flags |= IO_APPEND;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
flags |= IO_READ;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 1; i < len; i++) {
|
|
|
|
switch (str[i]) {
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
case '+':
|
|
|
|
if (flags & IO_UPDATE) return -1;
|
|
|
|
flags |= IO_UPDATE;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if (flags & IO_BINARY) return -1;
|
|
|
|
flags |= IO_BINARY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2018-01-06 18:25:45 +00:00
|
|
|
/* Check file argument */
|
2018-01-16 01:14:21 +00:00
|
|
|
static IOFile *checkfile(DstArgs args, int32_t n) {
|
|
|
|
IOFile *iof;
|
|
|
|
if (n >= args.n) {
|
2018-04-30 00:13:04 +00:00
|
|
|
*args.ret = dst_cstringv("expected core.file");
|
2018-01-06 18:25:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
if (!dst_checktype(args.v[n], DST_ABSTRACT)) {
|
2018-04-30 00:13:04 +00:00
|
|
|
*args.ret = dst_cstringv("expected core.file");
|
2018-01-06 18:25:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
iof = (IOFile *) dst_unwrap_abstract(args.v[n]);
|
|
|
|
if (dst_abstract_type(iof) != &dst_io_filetype) {
|
2018-04-30 00:13:04 +00:00
|
|
|
*args.ret = dst_cstringv("expected core.file");
|
2018-01-06 18:25:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
return iof;
|
2018-01-06 18:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check buffer argument */
|
2018-01-16 01:14:21 +00:00
|
|
|
static DstBuffer *checkbuffer(DstArgs args, int32_t n, int optional) {
|
|
|
|
if (optional && n == args.n) {
|
2018-01-06 18:25:45 +00:00
|
|
|
return dst_buffer(0);
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
if (n >= args.n) {
|
2018-04-30 00:13:04 +00:00
|
|
|
*args.ret = dst_cstringv("expected buffer");
|
2018-01-06 18:25:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
if (!dst_checktype(args.v[n], DST_BUFFER)) {
|
2018-04-30 00:13:04 +00:00
|
|
|
*args.ret = dst_cstringv("expected buffer");
|
2018-01-06 18:25:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
return dst_unwrap_abstract(args.v[n]);
|
2018-01-06 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
static Dst makef(FILE *f, int flags) {
|
|
|
|
IOFile *iof = (IOFile *) dst_abstract(&dst_io_filetype, sizeof(IOFile));
|
|
|
|
iof->file = f;
|
|
|
|
iof->flags = flags;
|
|
|
|
return dst_wrap_abstract(iof);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 12:13:26 +00:00
|
|
|
/* Open a process */
|
|
|
|
static int dst_io_popen(DstArgs args) {
|
|
|
|
const uint8_t *fname, *fmode;
|
|
|
|
int32_t modelen;
|
|
|
|
FILE *f;
|
|
|
|
int flags;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_MINARITY(args, 1);
|
|
|
|
DST_MAXARITY(args, 2);
|
|
|
|
DST_ARG_STRING(fname, args, 0);
|
2018-05-11 12:13:26 +00:00
|
|
|
if (args.n == 2) {
|
|
|
|
if (!dst_checktype(args.v[1], DST_STRING) &&
|
|
|
|
!dst_checktype(args.v[1], DST_SYMBOL))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected string mode");
|
2018-05-11 12:13:26 +00:00
|
|
|
fmode = dst_unwrap_string(args.v[1]);
|
|
|
|
modelen = dst_string_length(fmode);
|
|
|
|
} else {
|
|
|
|
fmode = (const uint8_t *)"r";
|
|
|
|
modelen = 1;
|
|
|
|
}
|
|
|
|
if (fmode[0] == ':') {
|
|
|
|
fmode++;
|
|
|
|
modelen--;
|
|
|
|
}
|
|
|
|
if (modelen != 1 || !(fmode[0] == 'r' || fmode[0] == 'w')) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "invalid file mode");
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
|
|
|
flags = (fmode[0] == 'r') ? IO_PIPED | IO_READ : IO_PIPED | IO_WRITE;
|
|
|
|
#ifdef DST_WINDOWS
|
2018-07-11 00:01:39 +00:00
|
|
|
#define popen _popen
|
2018-05-11 12:13:26 +00:00
|
|
|
#endif
|
2018-07-11 00:01:39 +00:00
|
|
|
f = popen((const char *)fname, (const char *)fmode);
|
2018-05-11 12:13:26 +00:00
|
|
|
if (!f) {
|
|
|
|
if (errno == EMFILE) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "too many streams are open");
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "could not open file");
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, makef(f, flags));
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
/* Open a a file and return a userdata wrapper around the C file API. */
|
|
|
|
static int dst_io_fopen(DstArgs args) {
|
|
|
|
const uint8_t *fname, *fmode;
|
|
|
|
int32_t modelen;
|
2018-01-06 16:09:15 +00:00
|
|
|
FILE *f;
|
2018-01-16 01:14:21 +00:00
|
|
|
int flags;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_MINARITY(args, 1);
|
|
|
|
DST_MAXARITY(args, 2);
|
|
|
|
DST_ARG_STRING(fname, args, 0);
|
2018-01-16 01:14:21 +00:00
|
|
|
if (args.n == 2) {
|
2018-02-04 17:58:40 +00:00
|
|
|
if (!dst_checktype(args.v[1], DST_STRING) &&
|
|
|
|
!dst_checktype(args.v[1], DST_SYMBOL))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected string mode");
|
2018-01-16 01:14:21 +00:00
|
|
|
fmode = dst_unwrap_string(args.v[1]);
|
|
|
|
modelen = dst_string_length(fmode);
|
|
|
|
} else {
|
|
|
|
fmode = (const uint8_t *)"r";
|
|
|
|
modelen = 1;
|
|
|
|
}
|
2018-02-04 17:58:40 +00:00
|
|
|
if (fmode[0] == ':') {
|
|
|
|
fmode++;
|
|
|
|
modelen--;
|
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
if ((flags = checkflags(fmode, modelen)) < 0) {
|
|
|
|
DST_THROW(args, "invalid file mode");
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
f = fopen((const char *)fname, (const char *)fmode);
|
2018-05-19 00:53:19 +00:00
|
|
|
DST_RETURN(args, f ? makef(f, flags) : dst_wrap_nil());
|
2018-01-06 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 13:37:34 +00:00
|
|
|
/* Read up to n bytes into buffer. Return error string if error. */
|
|
|
|
static const char *read_chunk(IOFile *iof, DstBuffer *buffer, int32_t nBytesMax) {
|
|
|
|
if (!(iof->flags & (IO_READ | IO_UPDATE)))
|
|
|
|
return "file is not readable";
|
|
|
|
/* Ensure buffer size */
|
|
|
|
if (dst_buffer_extra(buffer, nBytesMax))
|
|
|
|
return "buffer overflow";
|
|
|
|
size_t ntoread = nBytesMax;
|
|
|
|
size_t nread = fread((char *)(buffer->data + buffer->count), 1, ntoread, iof->file);
|
|
|
|
if (nread != ntoread && ferror(iof->file))
|
|
|
|
return "could not read file";
|
2018-08-06 01:32:32 +00:00
|
|
|
buffer->count += (int32_t) nread;
|
2018-06-26 13:37:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-06 16:09:15 +00:00
|
|
|
/* Read a certain number of bytes into memory */
|
2018-01-16 01:14:21 +00:00
|
|
|
static int dst_io_fread(DstArgs args) {
|
2018-01-06 16:09:15 +00:00
|
|
|
DstBuffer *b;
|
2018-01-16 01:14:21 +00:00
|
|
|
IOFile *iof = checkfile(args, 0);
|
|
|
|
if (!iof) return 1;
|
2018-05-11 12:13:26 +00:00
|
|
|
if (iof->flags & IO_CLOSED)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "file is closed");
|
2018-01-16 01:14:21 +00:00
|
|
|
b = checkbuffer(args, 2, 1);
|
2018-01-06 18:25:45 +00:00
|
|
|
if (!b) return 1;
|
2018-02-04 05:54:38 +00:00
|
|
|
if (dst_checktype(args.v[1], DST_SYMBOL)) {
|
|
|
|
const uint8_t *sym = dst_unwrap_symbol(args.v[1]);
|
|
|
|
if (!dst_cstrcmp(sym, ":all")) {
|
|
|
|
/* Read whole file */
|
2018-06-26 13:37:34 +00:00
|
|
|
int status = fseek(iof->file, 0, SEEK_SET);
|
|
|
|
if (status) {
|
|
|
|
/* backwards fseek did not work (stream like popen) */
|
|
|
|
int32_t sizeBefore;
|
|
|
|
do {
|
|
|
|
sizeBefore = b->count;
|
|
|
|
const char *maybeErr = read_chunk(iof, b, 1024);
|
|
|
|
if (maybeErr) DST_THROW(args, maybeErr);
|
|
|
|
} while (sizeBefore < b->count);
|
|
|
|
} else {
|
|
|
|
fseek(iof->file, 0, SEEK_END);
|
|
|
|
long fsize = ftell(iof->file);
|
|
|
|
fseek(iof->file, 0, SEEK_SET);
|
|
|
|
if (fsize > INT32_MAX) DST_THROW(args, "buffer overflow");
|
|
|
|
const char *maybeErr = read_chunk(iof, b, (int32_t) fsize);;
|
|
|
|
if (maybeErr) DST_THROW(args, maybeErr);
|
|
|
|
}
|
2018-02-04 05:54:38 +00:00
|
|
|
} else if (!dst_cstrcmp(sym, ":line")) {
|
|
|
|
for (;;) {
|
|
|
|
int x = fgetc(iof->file);
|
2018-05-13 00:31:28 +00:00
|
|
|
if (x != EOF && dst_buffer_push_u8(b, (uint8_t)x))
|
|
|
|
DST_THROW(args, "buffer overflow");
|
2018-02-06 15:31:42 +00:00
|
|
|
if (x == EOF || x == '\n') break;
|
2018-02-04 05:54:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected one of :all, :line");
|
2018-05-20 01:16:00 +00:00
|
|
|
}
|
2018-02-04 05:54:38 +00:00
|
|
|
} else if (!dst_checktype(args.v[1], DST_INTEGER)) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected positive integer");
|
2018-02-04 05:54:38 +00:00
|
|
|
} else {
|
2018-06-26 13:37:34 +00:00
|
|
|
int32_t len = dst_unwrap_integer(args.v[1]);
|
2018-05-13 00:31:28 +00:00
|
|
|
if (len < 0) DST_THROW(args, "expected positive integer");
|
2018-06-26 13:37:34 +00:00
|
|
|
const char *maybeErr = read_chunk(iof, b, len);
|
|
|
|
if (maybeErr) DST_THROW(args, maybeErr);
|
2018-02-04 05:54:38 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, dst_wrap_buffer(b));
|
2018-01-06 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write bytes to a file */
|
2018-01-16 01:14:21 +00:00
|
|
|
static int dst_io_fwrite(DstArgs args) {
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t len, i;
|
|
|
|
const uint8_t *str;
|
2018-01-16 01:14:21 +00:00
|
|
|
IOFile *iof = checkfile(args, 0);
|
|
|
|
if (!iof) return 1;
|
2018-05-11 12:13:26 +00:00
|
|
|
if (iof->flags & IO_CLOSED)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "file is closed");
|
2018-01-16 01:14:21 +00:00
|
|
|
if (!(iof->flags & (IO_WRITE | IO_APPEND | IO_UPDATE)))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "file is not writeable");
|
|
|
|
for (i = 1; i < args.n; i++) {
|
|
|
|
DST_CHECKMANY(args, i, DST_TFLAG_BYTES);
|
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
for (i = 1; i < args.n; i++) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_ARG_BYTES(str, len, args, i);
|
2018-05-07 03:25:59 +00:00
|
|
|
if (len) {
|
2018-05-13 00:31:28 +00:00
|
|
|
if (!fwrite(str, len, 1, iof->file)) DST_THROW(args, "error writing to file");
|
2018-05-07 03:25:59 +00:00
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, dst_wrap_abstract(iof));
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the bytes in the file */
|
|
|
|
static int dst_io_fflush(DstArgs args) {
|
|
|
|
IOFile *iof = checkfile(args, 0);
|
|
|
|
if (!iof) return 1;
|
2018-05-11 12:13:26 +00:00
|
|
|
if (iof->flags & IO_CLOSED)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "file is closed");
|
2018-01-16 01:14:21 +00:00
|
|
|
if (!(iof->flags & (IO_WRITE | IO_APPEND | IO_UPDATE)))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "file is not flushable");
|
|
|
|
if (fflush(iof->file)) DST_THROW(args, "could not flush file");
|
|
|
|
DST_RETURN(args, dst_wrap_abstract(iof));
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup a file */
|
|
|
|
static int dst_io_gc(void *p, size_t len) {
|
|
|
|
(void) len;
|
|
|
|
IOFile *iof = (IOFile *)p;
|
|
|
|
if (!(iof->flags & (IO_NOT_CLOSEABLE | IO_CLOSED))) {
|
|
|
|
return fclose(iof->file);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2018-01-06 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close a file */
|
2018-01-16 01:14:21 +00:00
|
|
|
static int dst_io_fclose(DstArgs args) {
|
|
|
|
IOFile *iof = checkfile(args, 0);
|
|
|
|
if (!iof) return 1;
|
2018-03-11 19:35:23 +00:00
|
|
|
if (iof->flags & (IO_CLOSED))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "file already closed");
|
2018-03-11 19:35:23 +00:00
|
|
|
if (iof->flags & (IO_NOT_CLOSEABLE))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "file not closable");
|
2018-05-11 12:13:26 +00:00
|
|
|
if (iof->flags & IO_PIPED) {
|
|
|
|
#ifdef DST_WINDOWS
|
2018-07-11 00:01:39 +00:00
|
|
|
#define pclose _pclose
|
2018-05-11 12:13:26 +00:00
|
|
|
#endif
|
2018-07-11 00:01:39 +00:00
|
|
|
if (pclose(iof->file)) DST_THROW(args, "could not close file");
|
2018-05-11 12:13:26 +00:00
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
if (fclose(iof->file)) DST_THROW(args, "could not close file");
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
iof->flags |= IO_CLOSED;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, dst_wrap_abstract(iof));
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 17:58:40 +00:00
|
|
|
/* Seek a file */
|
|
|
|
static int dst_io_fseek(DstArgs args) {
|
|
|
|
long int offset = 0;
|
|
|
|
int whence = SEEK_CUR;
|
|
|
|
IOFile *iof = checkfile(args, 0);
|
|
|
|
if (!iof) return 1;
|
2018-05-11 12:13:26 +00:00
|
|
|
if (iof->flags & IO_CLOSED)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "file is closed");
|
2018-02-04 17:58:40 +00:00
|
|
|
if (args.n >= 2) {
|
|
|
|
const uint8_t *whence_sym;
|
|
|
|
if (!dst_checktype(args.v[1], DST_SYMBOL))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected symbol");
|
2018-02-04 17:58:40 +00:00
|
|
|
whence_sym = dst_unwrap_symbol(args.v[1]);
|
|
|
|
if (!dst_cstrcmp(whence_sym, ":cur")) {
|
|
|
|
whence = SEEK_CUR;
|
|
|
|
} else if (!dst_cstrcmp(whence_sym, ":set")) {
|
|
|
|
whence = SEEK_SET;
|
|
|
|
} else if (!dst_cstrcmp(whence_sym, ":end")) {
|
|
|
|
whence = SEEK_END;
|
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected one of :cur, :set, :end");
|
2018-02-04 17:58:40 +00:00
|
|
|
}
|
|
|
|
if (args.n >= 3) {
|
|
|
|
if (!dst_checktype(args.v[2], DST_INTEGER))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected integer");
|
2018-02-04 17:58:40 +00:00
|
|
|
offset = dst_unwrap_integer(args.v[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fseek(iof->file, offset, whence))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "error seeking file");
|
|
|
|
DST_RETURN(args, args.v[0]);
|
2018-02-04 17:58:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 21:43:19 +00:00
|
|
|
static const DstReg cfuns[] = {
|
2018-05-08 23:40:28 +00:00
|
|
|
{"file.open", dst_io_fopen},
|
|
|
|
{"file.close", dst_io_fclose},
|
|
|
|
{"file.read", dst_io_fread},
|
|
|
|
{"file.write", dst_io_fwrite},
|
|
|
|
{"file.flush", dst_io_fflush},
|
|
|
|
{"file.seek", dst_io_fseek},
|
2018-05-11 12:13:26 +00:00
|
|
|
{"file.popen", dst_io_popen},
|
2018-01-19 21:43:19 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
/* Module entry point */
|
2018-01-18 22:25:45 +00:00
|
|
|
int dst_lib_io(DstArgs args) {
|
|
|
|
DstTable *env = dst_env_arg(args);
|
2018-01-19 21:43:19 +00:00
|
|
|
dst_env_cfuns(env, cfuns);
|
2018-01-16 01:14:21 +00:00
|
|
|
|
|
|
|
/* stdout */
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_env_def(env, "stdout",
|
2018-01-16 01:14:21 +00:00
|
|
|
makef(stdout, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE));
|
|
|
|
|
|
|
|
/* stderr */
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_env_def(env, "stderr",
|
2018-01-16 01:14:21 +00:00
|
|
|
makef(stderr, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE));
|
|
|
|
|
|
|
|
/* stdin */
|
2018-01-18 22:25:45 +00:00
|
|
|
dst_env_def(env, "stdin",
|
2018-01-16 01:14:21 +00:00
|
|
|
makef(stdin, IO_READ | IO_NOT_CLOSEABLE | IO_SERIALIZABLE));
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
return 0;
|
2018-01-06 16:09:15 +00:00
|
|
|
}
|