2018-01-12 15:41:27 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 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-09-10 18:54:12 +00:00
|
|
|
/* Compiler feature test macros for things */
|
2018-07-04 03:07:35 +00:00
|
|
|
#define _DEFAULT_SOURCE
|
2018-09-10 18:54:12 +00:00
|
|
|
#define _BSD_SOURCE
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
#include <stdio.h>
|
2018-09-06 02:18:42 +00:00
|
|
|
#include <janet/janet.h>
|
2018-05-11 12:13:26 +00:00
|
|
|
#include <errno.h>
|
2019-01-06 06:49:56 +00:00
|
|
|
#include "util.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;
|
|
|
|
};
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static int janet_io_gc(void *p, size_t len);
|
2018-01-16 01:14:21 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetAbstractType janet_io_filetype = {
|
2019-01-03 00:41:07 +00:00
|
|
|
"core/file",
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_io_gc,
|
2018-01-17 04:18:45 +00:00
|
|
|
NULL
|
2018-01-06 16:09:15 +00:00
|
|
|
};
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Check arguments to fopen */
|
2019-01-06 01:09:03 +00:00
|
|
|
static int checkflags(const uint8_t *str) {
|
2018-01-16 01:14:21 +00:00
|
|
|
int flags = 0;
|
|
|
|
int32_t i;
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t len = janet_string_length(str);
|
|
|
|
if (!len || len > 3)
|
|
|
|
janet_panic("file mode must have a length between 1 and 3");
|
2018-01-16 01:14:21 +00:00
|
|
|
switch (*str) {
|
|
|
|
default:
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panicf("invalid flag %c, expected w, a, or r", *str);
|
|
|
|
break;
|
2018-01-16 01:14:21 +00:00
|
|
|
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:
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panicf("invalid flag %c, expected + or b", str[i]);
|
|
|
|
break;
|
2018-01-16 01:14:21 +00:00
|
|
|
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-09-06 02:18:42 +00:00
|
|
|
static Janet makef(FILE *f, int flags) {
|
|
|
|
IOFile *iof = (IOFile *) janet_abstract(&janet_io_filetype, sizeof(IOFile));
|
2018-01-16 01:14:21 +00:00
|
|
|
iof->file = f;
|
|
|
|
iof->flags = flags;
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_wrap_abstract(iof);
|
2018-01-12 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 12:13:26 +00:00
|
|
|
/* Open a process */
|
2019-01-06 01:52:32 +00:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
static Janet janet_io_popen(int32_t argc, Janet *argv) {
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
janet_panic("not implemented on this platform");
|
|
|
|
return janet_wrap_nil();
|
|
|
|
}
|
|
|
|
#else
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_io_popen(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
const uint8_t *fname = janet_getstring(argv, 0);
|
|
|
|
const uint8_t *fmode = NULL;
|
|
|
|
if (argc == 2) {
|
|
|
|
fmode = janet_getkeyword(argv, 1);
|
|
|
|
if (janet_string_length(fmode) != 1 ||
|
|
|
|
!(fmode[0] == 'r' || fmode[0] == 'w')) {
|
|
|
|
janet_panicf("invalid file mode :%S, expected :r or :w", fmode);
|
|
|
|
}
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
int flags = (fmode && fmode[0] == '2')
|
|
|
|
? IO_PIPED | IO_WRITE
|
|
|
|
: IO_PIPED | IO_READ;
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-07-11 00:01:39 +00:00
|
|
|
#define popen _popen
|
2018-05-11 12:13:26 +00:00
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
FILE *f = popen((const char *)fname, (const char *)fmode);
|
2018-05-11 12:13:26 +00:00
|
|
|
if (!f) {
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return makef(f, flags);
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
2019-01-06 01:52:32 +00:00
|
|
|
#endif
|
2018-05-11 12:13:26 +00:00
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_io_fopen(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
const uint8_t *fname = janet_getstring(argv, 0);
|
|
|
|
const uint8_t *fmode;
|
2018-01-16 01:14:21 +00:00
|
|
|
int flags;
|
2019-01-06 01:09:03 +00:00
|
|
|
if (argc == 2) {
|
|
|
|
fmode = janet_getkeyword(argv, 1);
|
|
|
|
flags = checkflags(fmode);
|
2018-01-16 01:14:21 +00:00
|
|
|
} else {
|
|
|
|
fmode = (const uint8_t *)"r";
|
2019-01-06 01:09:03 +00:00
|
|
|
flags = IO_READ;
|
2018-05-13 00:31:28 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
FILE *f = fopen((const char *)fname, (const char *)fmode);
|
|
|
|
return f ? makef(f, flags) : janet_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. */
|
2019-01-06 01:09:03 +00:00
|
|
|
static void read_chunk(IOFile *iof, JanetBuffer *buffer, int32_t nBytesMax) {
|
2018-06-26 13:37:34 +00:00
|
|
|
if (!(iof->flags & (IO_READ | IO_UPDATE)))
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("file is not readable");
|
|
|
|
janet_buffer_extra(buffer, nBytesMax);
|
2018-06-26 13:37:34 +00:00
|
|
|
size_t ntoread = nBytesMax;
|
|
|
|
size_t nread = fread((char *)(buffer->data + buffer->count), 1, ntoread, iof->file);
|
2018-11-16 21:24:10 +00:00
|
|
|
if (nread != ntoread && ferror(iof->file))
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("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
|
|
|
}
|
|
|
|
|
2018-01-06 16:09:15 +00:00
|
|
|
/* Read a certain number of bytes into memory */
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_io_fread(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 2, 3);
|
|
|
|
IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype);
|
|
|
|
if (iof->flags & IO_CLOSED) janet_panic("file is closed");
|
|
|
|
JanetBuffer *buffer;
|
|
|
|
if (argc == 2) {
|
|
|
|
buffer = janet_buffer(0);
|
|
|
|
} else {
|
|
|
|
buffer = janet_getbuffer(argv, 2);
|
|
|
|
}
|
|
|
|
if (janet_checktype(argv[1], JANET_KEYWORD)) {
|
|
|
|
const uint8_t *sym = janet_unwrap_keyword(argv[1]);
|
2019-01-03 00:41:07 +00:00
|
|
|
if (!janet_cstrcmp(sym, "all")) {
|
2018-02-04 05:54:38 +00:00
|
|
|
/* 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 {
|
2019-01-06 01:09:03 +00:00
|
|
|
sizeBefore = buffer->count;
|
|
|
|
read_chunk(iof, buffer, 1024);
|
|
|
|
} while (sizeBefore < buffer->count);
|
2018-06-26 13:37:34 +00:00
|
|
|
} else {
|
|
|
|
fseek(iof->file, 0, SEEK_END);
|
|
|
|
long fsize = ftell(iof->file);
|
|
|
|
fseek(iof->file, 0, SEEK_SET);
|
2019-01-06 01:09:03 +00:00
|
|
|
read_chunk(iof, buffer, (int32_t) fsize);
|
2018-06-26 13:37:34 +00:00
|
|
|
}
|
2019-01-03 00:41:07 +00:00
|
|
|
} else if (!janet_cstrcmp(sym, "line")) {
|
2018-02-04 05:54:38 +00:00
|
|
|
for (;;) {
|
|
|
|
int x = fgetc(iof->file);
|
2019-01-06 01:09:03 +00:00
|
|
|
if (x != EOF) janet_buffer_push_u8(buffer, (uint8_t)x);
|
2018-02-06 15:31:42 +00:00
|
|
|
if (x == EOF || x == '\n') break;
|
2018-02-04 05:54:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panicf("expected one of :all, :line, got %v", argv[1]);
|
2018-05-20 01:16:00 +00:00
|
|
|
}
|
2018-02-04 05:54:38 +00:00
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
int32_t len = janet_getinteger(argv, 1);
|
|
|
|
if (len < 0) janet_panic("expected positive integer");
|
|
|
|
read_chunk(iof, buffer, len);
|
2018-02-04 05:54:38 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_buffer(buffer);
|
2018-01-06 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write bytes to a file */
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_io_fwrite(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, -1);
|
|
|
|
IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype);
|
2018-05-11 12:13:26 +00:00
|
|
|
if (iof->flags & IO_CLOSED)
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("file is closed");
|
2018-01-16 01:14:21 +00:00
|
|
|
if (!(iof->flags & (IO_WRITE | IO_APPEND | IO_UPDATE)))
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("file is not writeable");
|
|
|
|
int32_t i;
|
|
|
|
/* Verify all arguments before writing to file */
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
janet_getbytes(argv, i);
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
JanetByteView view = janet_getbytes(argv, i);
|
|
|
|
if (view.len) {
|
|
|
|
if (!fwrite(view.bytes, view.len, 1, iof->file)) {
|
|
|
|
janet_panic("error writing to file");
|
|
|
|
}
|
2018-05-07 03:25:59 +00:00
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the bytes in the file */
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_io_fflush(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype);
|
2018-05-11 12:13:26 +00:00
|
|
|
if (iof->flags & IO_CLOSED)
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("file is closed");
|
2018-01-16 01:14:21 +00:00
|
|
|
if (!(iof->flags & (IO_WRITE | IO_APPEND | IO_UPDATE)))
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("file is not writeable");
|
|
|
|
if (fflush(iof->file))
|
|
|
|
janet_panic("could not flush file");
|
|
|
|
return argv[0];
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup a file */
|
2018-09-06 02:18:42 +00:00
|
|
|
static int janet_io_gc(void *p, size_t len) {
|
2018-01-16 01:14:21 +00:00
|
|
|
(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 */
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_io_fclose(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype);
|
|
|
|
if (iof->flags & IO_CLOSED)
|
|
|
|
janet_panic("file is closed");
|
2018-03-11 19:35:23 +00:00
|
|
|
if (iof->flags & (IO_NOT_CLOSEABLE))
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("file not closable");
|
2018-05-11 12:13:26 +00:00
|
|
|
if (iof->flags & IO_PIPED) {
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-07-11 00:01:39 +00:00
|
|
|
#define pclose _pclose
|
2018-05-11 12:13:26 +00:00
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
if (pclose(iof->file)) janet_panic("could not close file");
|
2018-05-11 12:13:26 +00:00
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
if (fclose(iof->file)) janet_panic("could not close file");
|
2018-05-11 12:13:26 +00:00
|
|
|
}
|
2018-01-16 01:14:21 +00:00
|
|
|
iof->flags |= IO_CLOSED;
|
2019-01-06 01:09:03 +00:00
|
|
|
return argv[0];
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 17:58:40 +00:00
|
|
|
/* Seek a file */
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet janet_io_fseek(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 2, 3);
|
|
|
|
IOFile *iof = janet_getabstract(argv, 0, &janet_io_filetype);
|
|
|
|
if (iof->flags & IO_CLOSED)
|
|
|
|
janet_panic("file is closed");
|
2018-02-04 17:58:40 +00:00
|
|
|
long int offset = 0;
|
|
|
|
int whence = SEEK_CUR;
|
2019-01-06 01:09:03 +00:00
|
|
|
if (argc >= 2) {
|
|
|
|
const uint8_t *whence_sym = janet_getkeyword(argv, 1);
|
2019-01-03 00:41:07 +00:00
|
|
|
if (!janet_cstrcmp(whence_sym, "cur")) {
|
2018-02-04 17:58:40 +00:00
|
|
|
whence = SEEK_CUR;
|
2019-01-03 00:41:07 +00:00
|
|
|
} else if (!janet_cstrcmp(whence_sym, "set")) {
|
2018-02-04 17:58:40 +00:00
|
|
|
whence = SEEK_SET;
|
2019-01-03 00:41:07 +00:00
|
|
|
} else if (!janet_cstrcmp(whence_sym, "end")) {
|
2018-02-04 17:58:40 +00:00
|
|
|
whence = SEEK_END;
|
|
|
|
} else {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panicf("expected one of :cur, :set, :end, got %v", argv[1]);
|
2018-02-04 17:58:40 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
if (argc == 3) {
|
2019-01-06 02:58:39 +00:00
|
|
|
offset = (long) janet_getinteger64(argv, 2);
|
2018-02-04 17:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
if (fseek(iof->file, offset, whence)) janet_panic("error seeking file");
|
|
|
|
return argv[0];
|
2018-02-04 17:58:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static const JanetReg cfuns[] = {
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"file/open", janet_io_fopen,
|
|
|
|
JDOC("(file/open path [,mode])\n\n"
|
2019-01-06 08:23:03 +00:00
|
|
|
"Open a file. path is an absolute or relative path, and "
|
2019-01-06 06:49:56 +00:00
|
|
|
"mode is a set of flags indicating the mode to open the file in. "
|
|
|
|
"mode is a keyword where each character represents a flag. If the file "
|
|
|
|
"cannot be opened, returns nil, otherwise returns the new file handle. "
|
|
|
|
"Mode flags:\n\n"
|
|
|
|
"\tr - allow reading from the file\n"
|
2019-01-06 08:23:03 +00:00
|
|
|
"\tw - allow writing to the file\n"
|
2019-01-06 06:49:56 +00:00
|
|
|
"\ta - append to the file\n"
|
|
|
|
"\tb - open the file in binary mode (rather than text mode)\n"
|
|
|
|
"\t+ - append to the file instead of overwriting it")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"file/close", janet_io_fclose,
|
|
|
|
JDOC("(file/close f)\n\n"
|
|
|
|
"Close a file and release all related resources. When you are "
|
|
|
|
"done reading a file, close it to prevent a resource leak and let "
|
|
|
|
"other processes read the file.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"file/read", janet_io_fread,
|
|
|
|
JDOC("(file/read f what [,buf])\n\n"
|
|
|
|
"Read a number of bytes from a file into a buffer. A buffer can "
|
2019-01-06 08:23:03 +00:00
|
|
|
"be provided as an optional fourth argument, otherwise a new buffer "
|
2019-01-06 06:49:56 +00:00
|
|
|
"is created. 'what' can either be an integer or a keyword. Returns the "
|
|
|
|
"buffer with file contents. "
|
|
|
|
"Values for 'what':\n\n"
|
|
|
|
"\t:all - read the whole file\n"
|
|
|
|
"\t:line - read up to and including the next newline character\n"
|
|
|
|
"\tn (integer) - read up to n bytes from the file")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"file/write", janet_io_fwrite,
|
|
|
|
JDOC("(file/write f bytes)\n\n"
|
|
|
|
"Writes to a file. 'bytes' must be string, buffer, or symbol. Returns the "
|
|
|
|
"file.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"file/flush", janet_io_fflush,
|
|
|
|
JDOC("(file/flush f)\n\n"
|
2019-01-06 08:23:03 +00:00
|
|
|
"Flush any buffered bytes to the file system. In most files, writes are "
|
2019-01-06 06:49:56 +00:00
|
|
|
"buffered for efficiency reasons. Returns the file handle.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"file/seek", janet_io_fseek,
|
|
|
|
JDOC("(file/seek f [,whence [,n]])\n\n"
|
|
|
|
"Jump to a relative location in the file. 'whence' must be one of\n\n"
|
|
|
|
"\t:cur - jump relative to the current file location\n"
|
|
|
|
"\t:set - jump relative to the beginning of the file\n"
|
|
|
|
"\t:end - jump relative to the end of the file\n\n"
|
|
|
|
"By default, 'whence' is :cur. Optionally a value n may be passed "
|
|
|
|
"for the relative number of bytes to seek in the file. n may be a real "
|
|
|
|
"number to handle large files of more the 4GB. Returns the file handle.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"file/popen", janet_io_popen,
|
|
|
|
JDOC("(file/popen path [,mode])\n\n"
|
|
|
|
"Open a file that is backed by a process. The file must be opened in either "
|
|
|
|
"the :r (read) or the :w (write) mode. In :r mode, the stdout of the "
|
|
|
|
"process can be read from the file. In :w mode, the stdin of the process "
|
|
|
|
"can be written to. Returns the new file.")
|
2018-11-16 07:09:38 +00:00
|
|
|
},
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-01-19 21:43:19 +00:00
|
|
|
};
|
|
|
|
|
2018-01-16 01:14:21 +00:00
|
|
|
/* Module entry point */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_io(JanetTable *env) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_cfuns(env, NULL, cfuns);
|
2018-01-16 01:14:21 +00:00
|
|
|
/* stdout */
|
2018-10-21 05:35:07 +00:00
|
|
|
janet_def(env, "stdout",
|
2018-11-16 21:24:10 +00:00
|
|
|
makef(stdout, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("The standard output file."));
|
2018-01-16 01:14:21 +00:00
|
|
|
/* stderr */
|
2018-10-21 05:35:07 +00:00
|
|
|
janet_def(env, "stderr",
|
2018-11-16 07:09:38 +00:00
|
|
|
makef(stderr, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("The standard error file."));
|
2018-01-16 01:14:21 +00:00
|
|
|
/* stdin */
|
2018-10-21 05:35:07 +00:00
|
|
|
janet_def(env, "stdin",
|
2018-11-16 07:09:38 +00:00
|
|
|
makef(stdin, IO_READ | IO_NOT_CLOSEABLE | IO_SERIALIZABLE),
|
2019-01-06 06:49:56 +00:00
|
|
|
JDOC("The standard input file."));
|
2018-01-06 16:09:15 +00:00
|
|
|
}
|