2013-10-10 04:48:01 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-01-16 04:39:38 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2013-10-10 04:48:01 -04:00
|
|
|
|
|
|
|
#include "picrin.h"
|
2013-10-15 00:21:40 -04:00
|
|
|
#include "picrin/proc.h"
|
2013-10-17 09:42:47 -04:00
|
|
|
#include "picrin/port.h"
|
2013-11-04 22:38:23 -05:00
|
|
|
#include "picrin/blob.h"
|
2013-11-27 01:56:29 -05:00
|
|
|
#include "picrin/macro.h"
|
2013-10-10 04:48:01 -04:00
|
|
|
|
2014-01-12 10:49:25 -05:00
|
|
|
pic_value
|
|
|
|
pic_eof_object()
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_init_value(v, PIC_VTYPE_EOF);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2014-01-12 10:51:19 -05:00
|
|
|
struct pic_port *
|
|
|
|
pic_stdin(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
|
|
|
|
|
|
|
proc = pic_proc_ptr(pic_ref(pic, "current-input-port"));
|
|
|
|
|
|
|
|
return pic_port_ptr(pic_apply(pic, proc, pic_nil_value()));
|
|
|
|
}
|
|
|
|
|
2014-01-12 11:47:46 -05:00
|
|
|
struct pic_port *
|
|
|
|
pic_stdout(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_proc *proc;
|
|
|
|
|
|
|
|
proc = pic_proc_ptr(pic_ref(pic, "current-output-port"));
|
|
|
|
|
|
|
|
return pic_port_ptr(pic_apply(pic, proc, pic_nil_value()));
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:36:47 -05:00
|
|
|
int
|
|
|
|
pic_setvbuf(pic_file *file, char *buf, int mode, size_t bufsiz)
|
|
|
|
{
|
|
|
|
/* FIXME: free old buf */
|
|
|
|
|
|
|
|
file->mode = mode;
|
|
|
|
if (buf) {
|
|
|
|
file->buf = buf;
|
|
|
|
file->bufsiz = bufsiz;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (mode == _IONBF) {
|
|
|
|
file->buf = NULL;
|
|
|
|
file->bufsiz = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert(bufsiz == 0);
|
|
|
|
file->buf = (char *)malloc(BUFSIZ);
|
|
|
|
file->bufsiz = BUFSIZ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file->s = file->c = file->buf;
|
|
|
|
file->e = file->buf + file->bufsiz;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:36:59 -05:00
|
|
|
int
|
|
|
|
pic_fflush(pic_file *file)
|
|
|
|
{
|
|
|
|
return file->vtable.write(file->vtable.cookie, file->s, file->c - file->s);
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:41:10 -05:00
|
|
|
int
|
|
|
|
pic_ffill(pic_file *file)
|
|
|
|
{
|
|
|
|
return file->vtable.read(file->vtable.cookie, file->c, file->e - file->c);
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:27:05 -05:00
|
|
|
pic_file *
|
|
|
|
pic_funopen(void *cookie,
|
|
|
|
int (*read)(void *, char *, int),
|
|
|
|
int (*write)(void *, const char *, int),
|
|
|
|
fpos_t (*seek)(void *, fpos_t, int),
|
|
|
|
int (*close)(void *))
|
|
|
|
{
|
|
|
|
pic_file *file;
|
|
|
|
|
|
|
|
file = (pic_file *)malloc(sizeof(pic_file));
|
|
|
|
if (! file) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-16 04:37:54 -05:00
|
|
|
/* no buffering at the beginning */
|
|
|
|
file->buf = NULL;
|
|
|
|
file->mode = _IONBF;
|
|
|
|
file->bufsiz = 0;
|
2014-01-16 04:41:55 -05:00
|
|
|
file->us = PIC_UBUFSIZ;
|
|
|
|
file->ur = 0;
|
2014-01-16 04:37:54 -05:00
|
|
|
/* set vtable */
|
2014-01-16 04:27:05 -05:00
|
|
|
file->vtable.cookie = cookie;
|
|
|
|
file->vtable.read = read;
|
|
|
|
file->vtable.write = write;
|
|
|
|
file->vtable.seek = seek;
|
|
|
|
file->vtable.close = close;
|
|
|
|
|
2014-01-16 04:37:54 -05:00
|
|
|
pic_setvbuf(file, (char *)NULL, _IOFBF, 0);
|
|
|
|
|
2014-01-16 04:27:05 -05:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:34:04 -05:00
|
|
|
static int
|
|
|
|
file_read(void *cookie, char *ptr, int size)
|
|
|
|
{
|
|
|
|
return fread(ptr, size, 1, (FILE *)cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_write(void *cookie, const char *ptr, int size)
|
|
|
|
{
|
|
|
|
return fwrite(ptr, size, 1, (FILE *)cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
static fpos_t
|
|
|
|
file_seek(void *cookie, fpos_t pos, int whence)
|
|
|
|
{
|
|
|
|
return fseek((FILE *)cookie, pos, whence);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
file_close(void *cookie)
|
|
|
|
{
|
|
|
|
return fclose((FILE *)cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
pic_file *
|
|
|
|
pic_fopen(const char *filename, const char *mode)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen(filename, mode);
|
|
|
|
if (! fp) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return pic_funopen(fp, file_read, file_write, file_seek, file_close);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pic_fclose(pic_file *file)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = file->vtable.close(file->vtable.cookie);
|
|
|
|
if (! r) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free(file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:41:10 -05:00
|
|
|
size_t
|
|
|
|
pic_fread(void *ptr, size_t block, size_t nitems, pic_file *file)
|
|
|
|
{
|
|
|
|
int size, avail;
|
|
|
|
char *dst = (char *)ptr;
|
|
|
|
|
|
|
|
size = block * nitems; /* TODO: optimize block read */
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
avail = file->c - file->s;
|
|
|
|
if (size <= avail) {
|
|
|
|
memcpy(dst, file->s, size);
|
|
|
|
file->c -= size;
|
|
|
|
memmove(file->s, file->c, avail - size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(dst, file->s, avail);
|
|
|
|
file->c -= avail;
|
|
|
|
size -= avail;
|
|
|
|
dst += avail;
|
|
|
|
pic_ffill(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return block * nitems;
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:39:03 -05:00
|
|
|
size_t
|
|
|
|
pic_fwrite(const void *ptr, size_t block, size_t nitems, pic_file *file)
|
|
|
|
{
|
|
|
|
int size, room;
|
|
|
|
char *dst = (char *)ptr;
|
|
|
|
|
|
|
|
size = block * nitems; /* TODO: optimize block write */
|
|
|
|
while (1) {
|
|
|
|
room = file->e - file->c;
|
|
|
|
if (room < size) {
|
|
|
|
memcpy(file->c, dst, room);
|
|
|
|
file->c += room;
|
|
|
|
size -= room;
|
|
|
|
dst += room;
|
|
|
|
pic_fflush(file);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(file->c, dst, size);
|
|
|
|
file->c += size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return block * nitems;
|
|
|
|
}
|
|
|
|
|
2014-01-16 04:35:22 -05:00
|
|
|
int
|
|
|
|
pic_fgetc(pic_file *file)
|
|
|
|
{
|
|
|
|
char buf[1];
|
|
|
|
|
|
|
|
pic_fread(buf, 1, 1, file);
|
|
|
|
|
|
|
|
return buf[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pic_fputc(int c, pic_file *file)
|
|
|
|
{
|
|
|
|
char buf[1];
|
|
|
|
|
|
|
|
buf[0] = c;
|
|
|
|
pic_fwrite(buf, 1, 1, file);
|
|
|
|
|
|
|
|
return buf[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pic_fputs(const char *str, pic_file *file)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
pic_fwrite(str, len, 1, file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-17 09:42:20 -04:00
|
|
|
static void write_pair(pic_state *pic, struct pic_pair *pair);
|
2013-10-20 22:12:34 -04:00
|
|
|
static void write_str(pic_state *pic, struct pic_string *str);
|
2013-10-17 09:42:20 -04:00
|
|
|
|
2013-10-17 04:57:27 -04:00
|
|
|
static void
|
|
|
|
write(pic_state *pic, pic_value obj)
|
2013-10-10 04:48:01 -04:00
|
|
|
{
|
2013-10-29 02:51:37 -04:00
|
|
|
int i;
|
|
|
|
|
2013-10-10 04:48:01 -04:00
|
|
|
switch (pic_type(obj)) {
|
|
|
|
case PIC_TT_NIL:
|
|
|
|
printf("()");
|
|
|
|
break;
|
2013-10-16 00:17:01 -04:00
|
|
|
case PIC_TT_BOOL:
|
|
|
|
if (pic_true_p(obj))
|
|
|
|
printf("#t");
|
|
|
|
else
|
|
|
|
printf("#f");
|
|
|
|
break;
|
2013-10-10 04:48:01 -04:00
|
|
|
case PIC_TT_PAIR:
|
|
|
|
printf("(");
|
2013-10-17 09:42:20 -04:00
|
|
|
write_pair(pic, pic_pair_ptr(obj));
|
2013-10-10 04:48:01 -04:00
|
|
|
printf(")");
|
|
|
|
break;
|
|
|
|
case PIC_TT_SYMBOL:
|
2013-10-28 13:11:31 -04:00
|
|
|
printf("%s", pic_symbol_name(pic, pic_sym(obj)));
|
2013-10-10 04:48:01 -04:00
|
|
|
break;
|
2013-11-04 21:37:18 -05:00
|
|
|
case PIC_TT_CHAR:
|
2013-11-14 05:45:38 -05:00
|
|
|
switch (pic_char(obj)) {
|
|
|
|
default: printf("#\\%c", pic_char(obj)); break;
|
|
|
|
case '\a': printf("#\\alarm"); break;
|
|
|
|
case '\b': printf("#\\backspace"); break;
|
|
|
|
case 0x7f: printf("#\\delete"); break;
|
|
|
|
case 0x1b: printf("#\\escape"); break;
|
|
|
|
case '\n': printf("#\\newline"); break;
|
|
|
|
case '\r': printf("#\\return"); break;
|
|
|
|
case ' ': printf("#\\space"); break;
|
|
|
|
case '\t': printf("#\\tab"); break;
|
|
|
|
}
|
2013-11-04 21:37:18 -05:00
|
|
|
break;
|
2013-10-15 07:05:12 -04:00
|
|
|
case PIC_TT_FLOAT:
|
2013-10-27 11:21:24 -04:00
|
|
|
printf("%f", pic_float(obj));
|
|
|
|
break;
|
|
|
|
case PIC_TT_INT:
|
|
|
|
printf("%d", pic_int(obj));
|
2013-10-11 11:15:46 -04:00
|
|
|
break;
|
2013-10-22 03:02:20 -04:00
|
|
|
case PIC_TT_EOF:
|
|
|
|
printf("#<eof-object>");
|
|
|
|
break;
|
2013-10-12 01:40:01 -04:00
|
|
|
case PIC_TT_UNDEF:
|
|
|
|
printf("#<undef>");
|
|
|
|
break;
|
2013-10-14 04:35:01 -04:00
|
|
|
case PIC_TT_PROC:
|
2014-01-05 04:10:33 -05:00
|
|
|
printf("#<proc %p>", pic_ptr(obj));
|
2013-10-14 04:35:01 -04:00
|
|
|
break;
|
2013-10-17 09:42:47 -04:00
|
|
|
case PIC_TT_PORT:
|
2014-01-05 04:10:33 -05:00
|
|
|
printf("#<port %p>", pic_ptr(obj));
|
2013-10-17 09:42:47 -04:00
|
|
|
break;
|
2013-10-20 19:48:55 -04:00
|
|
|
case PIC_TT_STRING:
|
2013-10-20 22:12:34 -04:00
|
|
|
printf("\"");
|
|
|
|
write_str(pic, pic_str_ptr(obj));
|
|
|
|
printf("\"");
|
2013-10-20 19:48:55 -04:00
|
|
|
break;
|
2013-10-29 02:51:37 -04:00
|
|
|
case PIC_TT_VECTOR:
|
|
|
|
printf("#(");
|
|
|
|
for (i = 0; i < pic_vec_ptr(obj)->len; ++i) {
|
|
|
|
write(pic, pic_vec_ptr(obj)->data[i]);
|
|
|
|
if (i + 1 < pic_vec_ptr(obj)->len) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(")");
|
|
|
|
break;
|
2013-11-04 22:38:23 -05:00
|
|
|
case PIC_TT_BLOB:
|
|
|
|
printf("#u8(");
|
|
|
|
for (i = 0; i < pic_blob_ptr(obj)->len; ++i) {
|
|
|
|
printf("%d", pic_blob_ptr(obj)->data[i]);
|
|
|
|
if (i + 1 < pic_blob_ptr(obj)->len) {
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(")");
|
|
|
|
break;
|
2013-11-17 03:25:26 -05:00
|
|
|
case PIC_TT_ERROR:
|
|
|
|
printf("#<error %p>", pic_ptr(obj));
|
|
|
|
break;
|
2013-10-23 13:02:07 -04:00
|
|
|
case PIC_TT_ENV:
|
2014-01-05 04:10:33 -05:00
|
|
|
printf("#<env %p>", pic_ptr(obj));
|
2013-10-29 03:44:38 -04:00
|
|
|
break;
|
2013-11-09 00:14:25 -05:00
|
|
|
case PIC_TT_CONT:
|
|
|
|
printf("#<cont %p>", pic_ptr(obj));
|
|
|
|
break;
|
2013-11-26 07:05:02 -05:00
|
|
|
case PIC_TT_SENV:
|
|
|
|
printf("#<senv %p>", pic_ptr(obj));
|
|
|
|
break;
|
|
|
|
case PIC_TT_SYNTAX:
|
2014-01-10 02:22:36 -05:00
|
|
|
printf("#<syntax %p>", pic_ptr(obj));
|
2013-11-26 07:05:02 -05:00
|
|
|
break;
|
2013-11-27 01:04:02 -05:00
|
|
|
case PIC_TT_SC:
|
2013-11-27 01:56:29 -05:00
|
|
|
printf("#<sc %p: ", pic_ptr(obj));
|
|
|
|
write(pic, pic_sc(obj)->expr);
|
|
|
|
printf(">");
|
2013-11-27 01:04:02 -05:00
|
|
|
break;
|
2013-12-07 06:58:18 -05:00
|
|
|
case PIC_TT_LIB:
|
|
|
|
printf("#<library %p>", pic_ptr(obj));
|
|
|
|
break;
|
2014-01-08 10:39:13 -05:00
|
|
|
case PIC_TT_VAR:
|
|
|
|
printf("#<var %p>", pic_ptr(obj));
|
|
|
|
break;
|
2013-10-10 04:48:01 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-15 08:14:33 -04:00
|
|
|
|
2013-10-17 09:42:20 -04:00
|
|
|
static void
|
|
|
|
write_pair(pic_state *pic, struct pic_pair *pair)
|
|
|
|
{
|
|
|
|
write(pic, pair->car);
|
|
|
|
|
|
|
|
if (pic_nil_p(pair->cdr)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pic_pair_p(pair->cdr)) {
|
|
|
|
printf(" ");
|
|
|
|
write_pair(pic, pic_pair_ptr(pair->cdr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf(" . ");
|
|
|
|
write(pic, pair->cdr);
|
|
|
|
}
|
|
|
|
|
2013-10-20 22:12:34 -04:00
|
|
|
static void
|
|
|
|
write_str(pic_state *pic, struct pic_string *str)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char *cstr = str->str;
|
|
|
|
|
|
|
|
for (i = 0; i < str->len; ++i) {
|
|
|
|
if (cstr[i] == '"' || cstr[i] == '\\') {
|
|
|
|
putchar('\\');
|
|
|
|
}
|
|
|
|
putchar(cstr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 04:57:27 -04:00
|
|
|
void
|
|
|
|
pic_debug(pic_state *pic, pic_value obj)
|
|
|
|
{
|
|
|
|
write(pic, obj);
|
|
|
|
}
|
|
|
|
|
2014-01-12 09:57:50 -05:00
|
|
|
static pic_value
|
2014-01-12 10:02:58 -05:00
|
|
|
port_new_from_fp(pic_state *pic, FILE *fp, short flags)
|
2014-01-12 09:57:50 -05:00
|
|
|
{
|
|
|
|
struct pic_port *port;
|
|
|
|
|
|
|
|
port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT);
|
2014-01-16 04:34:04 -05:00
|
|
|
port->file = pic_funopen(fp, file_read, file_write, file_seek, file_close);
|
2014-01-12 09:57:50 -05:00
|
|
|
port->flags = flags;
|
|
|
|
port->status = PIC_PORT_OPEN;
|
|
|
|
return pic_obj_value(port);
|
|
|
|
}
|
|
|
|
|
2014-01-12 10:02:58 -05:00
|
|
|
static pic_value
|
|
|
|
port_new_stdio(pic_state *pic, FILE *fp, short dir)
|
|
|
|
{
|
|
|
|
return port_new_from_fp(pic, fp, dir | PIC_PORT_TEXT);
|
|
|
|
}
|
|
|
|
|
2013-10-24 09:56:04 -04:00
|
|
|
static pic_value
|
|
|
|
pic_port_input_port_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_IN) != 0) {
|
|
|
|
return pic_true_value();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_output_port_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_OUT) != 0) {
|
|
|
|
return pic_true_value();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_textual_port_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_TEXT) != 0) {
|
|
|
|
return pic_true_value();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_binary_port_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (pic_port_p(v) && (pic_port_ptr(v)->flags & PIC_PORT_BINARY) != 0) {
|
|
|
|
return pic_true_value();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_port_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
return pic_bool_value(pic_port_p(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_input_port_open_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
struct pic_port *port;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (! pic_port_p(v))
|
|
|
|
return pic_false_value();
|
|
|
|
port = pic_port_ptr(v);
|
|
|
|
if ((port->flags & PIC_PORT_IN) == 0)
|
|
|
|
return pic_false_value();
|
|
|
|
|
|
|
|
return pic_bool_value(port->status == PIC_PORT_OPEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_output_port_open_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
struct pic_port *port;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
|
|
|
if (! pic_port_p(v))
|
|
|
|
return pic_false_value();
|
|
|
|
port = pic_port_ptr(v);
|
|
|
|
if ((port->flags & PIC_PORT_OUT) == 0)
|
|
|
|
return pic_false_value();
|
|
|
|
|
|
|
|
return pic_bool_value(port->status == PIC_PORT_OPEN);
|
|
|
|
}
|
|
|
|
|
2013-10-22 03:02:20 -04:00
|
|
|
static pic_value
|
|
|
|
pic_port_eof_object_p(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
|
2013-11-04 23:37:08 -05:00
|
|
|
if (pic_vtype(v) == PIC_VTYPE_EOF) {
|
2013-10-22 03:02:20 -04:00
|
|
|
return pic_true_value();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pic_false_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_eof_object(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_get_args(pic, "");
|
|
|
|
|
2014-01-12 10:49:25 -05:00
|
|
|
return pic_eof_object();
|
2013-10-22 03:02:20 -04:00
|
|
|
}
|
|
|
|
|
2014-01-12 02:15:04 -05:00
|
|
|
static pic_value
|
2013-12-03 11:16:13 -05:00
|
|
|
pic_port_close_port(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_port *port;
|
|
|
|
|
2014-01-12 10:50:18 -05:00
|
|
|
pic_get_args(pic, "p", &port);
|
2013-12-03 11:16:13 -05:00
|
|
|
|
2014-01-16 04:32:05 -05:00
|
|
|
if (pic_fclose(port->file) == EOF) {
|
2013-12-03 11:16:13 -05:00
|
|
|
pic_error(pic, "close-port: failure");
|
|
|
|
}
|
|
|
|
port->status = PIC_PORT_CLOSE;
|
|
|
|
|
2014-01-08 01:22:23 -05:00
|
|
|
return pic_none_value();
|
2013-12-03 11:16:13 -05:00
|
|
|
}
|
|
|
|
|
2014-01-12 11:47:15 -05:00
|
|
|
#define assert_port_profile(port, flgs, stat, caller) do { \
|
|
|
|
if ((port->flags & (flgs)) != (flgs)) { \
|
|
|
|
switch (flgs) { \
|
2014-01-12 12:06:09 -05:00
|
|
|
case PIC_PORT_IN: \
|
|
|
|
pic_error(pic, caller ": expected output port"); \
|
|
|
|
case PIC_PORT_OUT: \
|
|
|
|
pic_error(pic, caller ": expected input port"); \
|
2014-01-12 11:47:15 -05:00
|
|
|
case PIC_PORT_IN | PIC_PORT_TEXT: \
|
|
|
|
pic_error(pic, caller ": expected input/textual port"); \
|
|
|
|
case PIC_PORT_IN | PIC_PORT_BINARY: \
|
|
|
|
pic_error(pic, caller ": expected input/binary port"); \
|
|
|
|
case PIC_PORT_OUT | PIC_PORT_TEXT: \
|
|
|
|
pic_error(pic, caller ": expected output/textual port"); \
|
|
|
|
case PIC_PORT_OUT | PIC_PORT_BINARY: \
|
|
|
|
pic_error(pic, caller ": expected output/binary port"); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (port->status != stat) { \
|
|
|
|
switch (stat) { \
|
|
|
|
case PIC_PORT_OPEN: \
|
|
|
|
pic_error(pic, caller ": expected open port"); \
|
|
|
|
case PIC_PORT_CLOSE: \
|
|
|
|
pic_error(pic, caller ": expected close port"); \
|
|
|
|
} \
|
|
|
|
} \
|
2014-01-12 11:34:26 -05:00
|
|
|
} while (0)
|
|
|
|
|
2014-01-12 10:51:30 -05:00
|
|
|
static pic_value
|
|
|
|
pic_port_read_char(pic_state *pic)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
struct pic_port *port = pic_stdin(pic);
|
|
|
|
|
|
|
|
pic_get_args(pic, "|p", &port);
|
|
|
|
|
2014-01-12 11:47:15 -05:00
|
|
|
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-char");
|
2014-01-12 11:34:26 -05:00
|
|
|
|
2014-01-16 04:32:05 -05:00
|
|
|
if ((c = pic_fgetc(port->file)) == EOF) {
|
2014-01-12 11:34:26 -05:00
|
|
|
return pic_eof_object();
|
2014-01-12 10:51:30 -05:00
|
|
|
}
|
2014-01-12 11:34:26 -05:00
|
|
|
else {
|
|
|
|
return pic_char_value(c);
|
2014-01-12 10:51:30 -05:00
|
|
|
}
|
2014-01-12 11:34:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_peek_char(pic_state *pic)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
struct pic_port *port = pic_stdin(pic);
|
|
|
|
|
|
|
|
pic_get_args(pic, "|p", &port);
|
|
|
|
|
2014-01-12 11:47:15 -05:00
|
|
|
assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "peek-char");
|
2014-01-12 10:51:30 -05:00
|
|
|
|
2014-01-16 04:32:05 -05:00
|
|
|
if ((c = pic_fgetc(port->file)) == EOF) {
|
2014-01-12 10:51:30 -05:00
|
|
|
return pic_eof_object();
|
|
|
|
}
|
|
|
|
else {
|
2014-01-16 04:32:05 -05:00
|
|
|
pic_ungetc(c, port->file);
|
2014-01-12 10:51:30 -05:00
|
|
|
return pic_char_value(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-12 12:07:38 -05:00
|
|
|
static pic_value
|
|
|
|
pic_port_write_simple(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value v;
|
|
|
|
|
|
|
|
pic_get_args(pic, "o", &v);
|
|
|
|
write(pic, v);
|
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2014-01-12 11:47:46 -05:00
|
|
|
static pic_value
|
|
|
|
pic_port_newline(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_port *port = pic_stdout(pic);
|
|
|
|
|
|
|
|
pic_get_args(pic, "|p", &port);
|
|
|
|
|
|
|
|
assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "newline");
|
|
|
|
|
2014-01-16 04:32:05 -05:00
|
|
|
pic_fputs("\n", port->file);
|
2014-01-12 11:47:46 -05:00
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2014-01-12 12:06:09 -05:00
|
|
|
static pic_value
|
|
|
|
pic_port_write_char(pic_state *pic)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
struct pic_port *port = pic_stdout(pic);
|
|
|
|
|
|
|
|
pic_get_args(pic, "c|p", &c, &port);
|
|
|
|
|
|
|
|
assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "write-char");
|
|
|
|
|
2014-01-16 04:32:05 -05:00
|
|
|
pic_fputc(c, port->file);
|
2014-01-12 12:06:09 -05:00
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static pic_value
|
|
|
|
pic_port_flush(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct pic_port *port = pic_stdout(pic);
|
|
|
|
|
|
|
|
pic_get_args(pic, "|p", &port);
|
|
|
|
|
|
|
|
assert_port_profile(port, PIC_PORT_OUT, PIC_PORT_OPEN, "flush-output-port");
|
|
|
|
|
2014-01-16 04:32:05 -05:00
|
|
|
pic_fflush(port->file);
|
2014-01-12 12:06:09 -05:00
|
|
|
return pic_none_value();
|
|
|
|
}
|
|
|
|
|
2013-10-15 08:14:33 -04:00
|
|
|
void
|
|
|
|
pic_init_port(pic_state *pic)
|
|
|
|
{
|
2014-01-12 10:02:58 -05:00
|
|
|
pic_defvar(pic, "current-input-port", port_new_stdio(pic, stdin, PIC_PORT_IN));
|
|
|
|
pic_defvar(pic, "current-output-port", port_new_stdio(pic, stdout, PIC_PORT_OUT));
|
|
|
|
pic_defvar(pic, "current-error-port", port_new_stdio(pic, stderr, PIC_PORT_OUT));
|
2014-01-12 09:57:50 -05:00
|
|
|
|
2013-10-24 09:56:04 -04:00
|
|
|
pic_defun(pic, "input-port?", pic_port_input_port_p);
|
|
|
|
pic_defun(pic, "output-port?", pic_port_output_port_p);
|
|
|
|
pic_defun(pic, "textual-port?", pic_port_textual_port_p);
|
|
|
|
pic_defun(pic, "binary-port?", pic_port_binary_port_p);
|
|
|
|
pic_defun(pic, "port?", pic_port_port_p);
|
|
|
|
pic_defun(pic, "input-port-open?", pic_port_input_port_open_p);
|
|
|
|
pic_defun(pic, "output-port-open?", pic_port_output_port_open_p);
|
2013-10-22 03:02:20 -04:00
|
|
|
pic_defun(pic, "eof-object?", pic_port_eof_object_p);
|
|
|
|
pic_defun(pic, "eof-object", pic_port_eof_object);
|
2013-12-03 11:16:13 -05:00
|
|
|
pic_defun(pic, "close-port", pic_port_close_port);
|
|
|
|
pic_defun(pic, "close-input-port", pic_port_close_port);
|
|
|
|
pic_defun(pic, "close-output-port", pic_port_close_port);
|
2014-01-12 10:51:30 -05:00
|
|
|
|
|
|
|
/* input */
|
|
|
|
pic_defun(pic, "read-char", pic_port_read_char);
|
2014-01-12 11:34:26 -05:00
|
|
|
pic_defun(pic, "peek-char", pic_port_peek_char);
|
2014-01-12 10:51:30 -05:00
|
|
|
|
|
|
|
/* write */
|
2013-12-10 08:59:03 -05:00
|
|
|
pic_defun(pic, "newline", pic_port_newline);
|
2014-01-12 12:06:09 -05:00
|
|
|
pic_defun(pic, "write-char", pic_port_write_char);
|
|
|
|
pic_defun(pic, "flush-output-port", pic_port_flush);
|
2013-12-08 02:17:28 -05:00
|
|
|
|
|
|
|
DEFLIBRARY(pic, "(scheme write)")
|
|
|
|
{
|
2014-01-12 11:34:59 -05:00
|
|
|
pic_defun(pic, "write-simple", pic_port_write_simple);
|
2013-12-08 02:17:28 -05:00
|
|
|
}
|
2013-12-10 10:26:43 -05:00
|
|
|
ENDLIBRARY(pic);
|
2013-10-15 08:14:33 -04:00
|
|
|
}
|