From f2fb37e520c411e1612f1cbd4ad1888f53a3bf28 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Fri, 9 Aug 2019 19:26:20 +0300 Subject: [PATCH] Replace ios_t with struct --- c/cvalues.h | 2 +- c/dump.c | 3 +- c/flisp.c | 4 +- c/flisp.h | 12 +++--- c/ios.c | 100 ++++++++++++++++++++++--------------------- c/ios.h | 117 ++++++++++++++++++++++++++------------------------- c/iostream.c | 91 ++++++++++++++++++++------------------- c/print.h | 32 +++++++------- c/read.h | 4 +- c/string.c | 2 +- c/table.c | 2 +- 11 files changed, 189 insertions(+), 180 deletions(-) diff --git a/c/cvalues.h b/c/cvalues.h index bd27d35..e9a8b87 100644 --- a/c/cvalues.h +++ b/c/cvalues.h @@ -574,7 +574,7 @@ void to_sized_ptr(value_t v, char *fname, char **pdata, size_t *psz) { if (iscvalue(v)) { cvalue_t *pcv = (cvalue_t *)ptr(v); - ios_t *x = value2c(ios_t *, v); + struct ios *x = value2c(struct ios *, v); if (cv_class(pcv) == iostreamtype && (x->bm == bm_mem)) { *pdata = x->buf; *psz = x->size; diff --git a/c/dump.c b/c/dump.c index 860ac7f..c4b150c 100644 --- a/c/dump.c +++ b/c/dump.c @@ -11,7 +11,8 @@ static char hexdig[] = "0123456789abcdef"; display a given number of bytes from a buffer, with the first address label being startoffs */ -void hexdump(ios_t *dest, const char *buffer, size_t len, size_t startoffs) +void hexdump(struct ios *dest, const char *buffer, size_t len, + size_t startoffs) { size_t offs = 0; size_t i, pos; diff --git a/c/flisp.c b/c/flisp.c index 5d724f2..6b1e6a8 100644 --- a/c/flisp.c +++ b/c/flisp.c @@ -2608,7 +2608,7 @@ int fl_load_system_image(value_t sys_image_iostream) { while (1) { e = fl_read_sexpr(Stack[SP - 1]); - if (ios_eof(value2c(ios_t *, Stack[SP - 1]))) + if (ios_eof(value2c(struct ios *, Stack[SP - 1]))) break; if (isfunction(e)) { // stage 0 format: series of thunks @@ -2635,7 +2635,7 @@ int fl_load_system_image(value_t sys_image_iostream) ios_putc('\n', ios_stderr); return 1; } - ios_close(value2c(ios_t *, Stack[SP - 1])); + ios_close(value2c(struct ios *, Stack[SP - 1])); POPN(1); return 0; } diff --git a/c/flisp.h b/c/flisp.h index 2f12b42..5c436f1 100644 --- a/c/flisp.h +++ b/c/flisp.h @@ -133,7 +133,7 @@ extern value_t FL_NIL, FL_T, FL_F, FL_EOF; /* read, eval, print main entry points */ value_t fl_read_sexpr(value_t f); -void fl_print(ios_t *f, value_t v); +void fl_print(struct ios *f, value_t v); value_t fl_toplevel_eval(value_t expr); value_t fl_apply(value_t f, value_t l); value_t fl_applyn(uint32_t n, value_t f, ...); @@ -212,7 +212,7 @@ static inline void argcount(char *fname, uint32_t nargs, uint32_t c) } typedef struct { - void (*print)(value_t self, ios_t *f); + void (*print)(value_t self, struct ios *f); void (*relocate)(value_t oldv, value_t newv); void (*finalize)(value_t self); void (*print_traverse)(value_t self); @@ -244,9 +244,9 @@ typedef enum { value_t relocate_lispvalue(value_t v); void print_traverse(value_t v); -void fl_print_chr(char c, ios_t *f); -void fl_print_str(char *s, ios_t *f); -void fl_print_child(ios_t *f, value_t v); +void fl_print_chr(char c, struct ios *f); +void fl_print_str(char *s, struct ios *f); +void fl_print_child(struct ios *f, value_t v); typedef int (*cvinitfunc_t)(struct _fltype_t *, value_t, void *); @@ -367,7 +367,7 @@ int fl_isstring(value_t v); int fl_isnumber(value_t v); int fl_isgensym(value_t v); int fl_isiostream(value_t v); -ios_t *fl_toiostream(value_t v, char *fname); +struct ios *fl_toiostream(value_t v, char *fname); value_t cvalue_compare(value_t a, value_t b); int numeric_compare(value_t a, value_t b, int eq, int eqnans, char *fname); diff --git a/c/ios.c b/c/ios.c index 48a8579..793f3fd 100644 --- a/c/ios.c +++ b/c/ios.c @@ -147,7 +147,7 @@ static int _os_write_all(long fd, void *buf, size_t n, size_t *nwritten) /* internal utility functions */ -static char *_buf_realloc(ios_t *s, size_t sz) +static char *_buf_realloc(struct ios *s, size_t sz) { char *temp; @@ -186,7 +186,7 @@ static char *_buf_realloc(ios_t *s, size_t sz) // write a block of data into the buffer at the current position, resizing // if necessary. returns # written. -static size_t _write_grow(ios_t *s, char *data, size_t n) +static size_t _write_grow(struct ios *s, char *data, size_t n) { size_t amt; size_t newsize; @@ -222,7 +222,7 @@ static size_t _write_grow(ios_t *s, char *data, size_t n) /* interface functions, low level */ -static size_t _ios_read(ios_t *s, char *dest, size_t n, int all) +static size_t _ios_read(struct ios *s, char *dest, size_t n, int all) { size_t tot = 0; size_t got, avail; @@ -283,17 +283,17 @@ static size_t _ios_read(ios_t *s, char *dest, size_t n, int all) return tot; } -size_t ios_read(ios_t *s, char *dest, size_t n) +size_t ios_read(struct ios *s, char *dest, size_t n) { return _ios_read(s, dest, n, 0); } -size_t ios_readall(ios_t *s, char *dest, size_t n) +size_t ios_readall(struct ios *s, char *dest, size_t n) { return _ios_read(s, dest, n, 1); } -size_t ios_readprep(ios_t *s, size_t n) +size_t ios_readprep(struct ios *s, size_t n) { if (s->state == bst_wr && s->bm != bm_mem) { ios_flush(s); @@ -324,7 +324,7 @@ size_t ios_readprep(ios_t *s, size_t n) return s->size - s->bpos; } -static void _write_update_pos(ios_t *s) +static void _write_update_pos(struct ios *s) { if (s->bpos > s->ndirty) s->ndirty = s->bpos; @@ -332,7 +332,7 @@ static void _write_update_pos(ios_t *s) s->size = s->bpos; } -size_t ios_write(ios_t *s, char *data, size_t n) +size_t ios_write(struct ios *s, char *data, size_t n) { if (s->readonly) return 0; @@ -388,7 +388,7 @@ size_t ios_write(ios_t *s, char *data, size_t n) return wrote; } -off_t ios_seek(ios_t *s, off_t pos) +off_t ios_seek(struct ios *s, off_t pos) { s->_eof = 0; if (s->bm == bm_mem) { @@ -405,7 +405,7 @@ off_t ios_seek(ios_t *s, off_t pos) return 0; } -off_t ios_seek_end(ios_t *s) +off_t ios_seek_end(struct ios *s) { s->_eof = 1; if (s->bm == bm_mem) { @@ -420,7 +420,7 @@ off_t ios_seek_end(ios_t *s) return 0; } -off_t ios_skip(ios_t *s, off_t offs) +off_t ios_skip(struct ios *s, off_t offs) { if (offs != 0) { if (offs > 0) { @@ -454,7 +454,7 @@ off_t ios_skip(ios_t *s, off_t offs) return 0; } -off_t ios_pos(ios_t *s) +off_t ios_pos(struct ios *s) { if (s->bm == bm_mem) return (off_t)s->bpos; @@ -474,7 +474,7 @@ off_t ios_pos(ios_t *s) return fdpos; } -size_t ios_trunc(ios_t *s, size_t size) +size_t ios_trunc(struct ios *s, size_t size) { if (s->bm == bm_mem) { if (size == s->size) @@ -493,7 +493,7 @@ size_t ios_trunc(ios_t *s, size_t size) return 0; } -int ios_eof(ios_t *s) +int ios_eof(struct ios *s) { if (s->bm == bm_mem) return (s->_eof ? 1 : 0); @@ -504,7 +504,7 @@ int ios_eof(ios_t *s) return 0; } -int ios_flush(ios_t *s) +int ios_flush(struct ios *s) { if (s->ndirty == 0 || s->bm == bm_mem || s->buf == NULL) return 0; @@ -548,7 +548,7 @@ int ios_flush(ios_t *s) return 0; } -void ios_close(ios_t *s) +void ios_close(struct ios *s) { ios_flush(s); if (s->fd != -1 && s->ownfd) @@ -560,7 +560,7 @@ void ios_close(ios_t *s) s->size = s->maxsize = s->bpos = 0; } -static void _buf_init(ios_t *s, bufmode_t bm) +static void _buf_init(struct ios *s, bufmode_t bm) { s->bm = bm; if (s->bm == bm_mem || s->bm == bm_none) { @@ -573,7 +573,7 @@ static void _buf_init(ios_t *s, bufmode_t bm) s->size = s->bpos = 0; } -char *ios_takebuf(ios_t *s, size_t *psize) +char *ios_takebuf(struct ios *s, size_t *psize) { char *buf; @@ -598,7 +598,7 @@ char *ios_takebuf(ios_t *s, size_t *psize) return buf; } -int ios_setbuf(ios_t *s, char *buf, size_t size, int own) +int ios_setbuf(struct ios *s, char *buf, size_t size, int own) { ios_flush(s); size_t nvalid = 0; @@ -620,7 +620,7 @@ int ios_setbuf(ios_t *s, char *buf, size_t size, int own) return 0; } -int ios_bufmode(ios_t *s, bufmode_t mode) +int ios_bufmode(struct ios *s, bufmode_t mode) { // no fd; can only do mem-only buffering if (s->fd == -1 && mode != bm_mem) @@ -629,7 +629,7 @@ int ios_bufmode(ios_t *s, bufmode_t mode) return 0; } -void ios_set_readonly(ios_t *s) +void ios_set_readonly(struct ios *s) { if (s->readonly) return; @@ -638,7 +638,8 @@ void ios_set_readonly(ios_t *s) s->readonly = 1; } -static size_t ios_copy_(ios_t *to, ios_t *from, size_t nbytes, bool_t all) +static size_t ios_copy_(struct ios *to, struct ios *from, size_t nbytes, + bool_t all) { size_t total = 0, avail; if (!ios_eof(from)) { @@ -666,19 +667,19 @@ static size_t ios_copy_(ios_t *to, ios_t *from, size_t nbytes, bool_t all) return total; } -size_t ios_copy(ios_t *to, ios_t *from, size_t nbytes) +size_t ios_copy(struct ios *to, struct ios *from, size_t nbytes) { return ios_copy_(to, from, nbytes, 0); } -size_t ios_copyall(ios_t *to, ios_t *from) +size_t ios_copyall(struct ios *to, struct ios *from) { return ios_copy_(to, from, 0, 1); } #define LINE_CHUNK_SIZE 160 -size_t ios_copyuntil(ios_t *to, ios_t *from, char delim) +size_t ios_copyuntil(struct ios *to, struct ios *from, char delim) { size_t total = 0, avail = from->size - from->bpos; int first = 1; @@ -708,7 +709,7 @@ size_t ios_copyuntil(ios_t *to, ios_t *from, char delim) return total; } -static void _ios_init(ios_t *s) +static void _ios_init(struct ios *s) { // put all fields in a sane initial state s->bm = bm_block; @@ -731,7 +732,8 @@ static void _ios_init(ios_t *s) /* stream object initializers. we do no allocation. */ -ios_t *ios_file(ios_t *s, char *fname, int rd, int wr, int create, int trunc) +struct ios *ios_file(struct ios *s, char *fname, int rd, int wr, int create, + int trunc) { int fd; if (!(rd || wr)) @@ -754,7 +756,7 @@ open_file_err: return NULL; } -ios_t *ios_mem(ios_t *s, size_t initsize) +struct ios *ios_mem(struct ios *s, size_t initsize) { _ios_init(s); s->bm = bm_mem; @@ -762,7 +764,7 @@ ios_t *ios_mem(ios_t *s, size_t initsize) return s; } -ios_t *ios_str(ios_t *s, char *str) +struct ios *ios_str(struct ios *s, char *str) { size_t n = strlen(str); if (ios_mem(s, n + 1) == NULL) @@ -772,7 +774,7 @@ ios_t *ios_str(ios_t *s, char *str) return s; } -ios_t *ios_static_buffer(ios_t *s, char *buf, size_t sz) +struct ios *ios_static_buffer(struct ios *s, char *buf, size_t sz) { ios_mem(s, 0); ios_setbuf(s, buf, sz, 0); @@ -781,7 +783,7 @@ ios_t *ios_static_buffer(ios_t *s, char *buf, size_t sz) return s; } -ios_t *ios_fd(ios_t *s, long fd, int isfile, int own) +struct ios *ios_fd(struct ios *s, long fd, int isfile, int own) { _ios_init(s); s->fd = fd; @@ -794,27 +796,27 @@ ios_t *ios_fd(ios_t *s, long fd, int isfile, int own) return s; } -ios_t *ios_stdin = NULL; -ios_t *ios_stdout = NULL; -ios_t *ios_stderr = NULL; +struct ios *ios_stdin = NULL; +struct ios *ios_stdout = NULL; +struct ios *ios_stderr = NULL; void ios_init_stdstreams() { - ios_stdin = LLT_ALLOC(sizeof(ios_t)); + ios_stdin = LLT_ALLOC(sizeof(struct ios)); ios_fd(ios_stdin, STDIN_FILENO, 0, 0); - ios_stdout = LLT_ALLOC(sizeof(ios_t)); + ios_stdout = LLT_ALLOC(sizeof(struct ios)); ios_fd(ios_stdout, STDOUT_FILENO, 0, 0); ios_stdout->bm = bm_line; - ios_stderr = LLT_ALLOC(sizeof(ios_t)); + ios_stderr = LLT_ALLOC(sizeof(struct ios)); ios_fd(ios_stderr, STDERR_FILENO, 0, 0); ios_stderr->bm = bm_none; } /* higher level interface */ -int ios_putc(int c, ios_t *s) +int ios_putc(int c, struct ios *s) { char ch = (char)c; @@ -828,7 +830,7 @@ int ios_putc(int c, ios_t *s) return (int)ios_write(s, &ch, 1); } -int ios_getc(ios_t *s) +int ios_getc(struct ios *s) { char ch; if (s->state == bst_rd && s->bpos < s->size) { @@ -844,7 +846,7 @@ int ios_getc(ios_t *s) return (unsigned char)ch; } -int ios_peekc(ios_t *s) +int ios_peekc(struct ios *s) { if (s->bpos < s->size) return (unsigned char)s->buf[s->bpos]; @@ -856,7 +858,7 @@ int ios_peekc(ios_t *s) return (unsigned char)s->buf[s->bpos]; } -int ios_ungetc(int c, ios_t *s) +int ios_ungetc(int c, struct ios *s) { if (s->state == bst_wr) return IOS_EOF; @@ -877,7 +879,7 @@ int ios_ungetc(int c, ios_t *s) return c; } -int ios_getutf8(ios_t *s, uint32_t *pwc) +int ios_getutf8(struct ios *s, uint32_t *pwc) { int c; size_t sz; @@ -904,7 +906,7 @@ int ios_getutf8(ios_t *s, uint32_t *pwc) return 1; } -int ios_peekutf8(ios_t *s, uint32_t *pwc) +int ios_peekutf8(struct ios *s, uint32_t *pwc) { int c; size_t sz; @@ -926,7 +928,7 @@ int ios_peekutf8(ios_t *s, uint32_t *pwc) return 1; } -int ios_pututf8(ios_t *s, uint32_t wc) +int ios_pututf8(struct ios *s, uint32_t wc) { char buf[8]; if (wc < 0x80) @@ -935,16 +937,16 @@ int ios_pututf8(ios_t *s, uint32_t wc) return ios_write(s, buf, n); } -void ios_purge(ios_t *s) +void ios_purge(struct ios *s) { if (s->state == bst_rd) { s->bpos = s->size; } } -char *ios_readline(ios_t *s) +char *ios_readline(struct ios *s) { - ios_t dest; + struct ios dest; ios_mem(&dest, 0); ios_copyuntil(&dest, s, '\n'); size_t n; @@ -953,7 +955,7 @@ char *ios_readline(ios_t *s) int vasprintf(char **strp, const char *fmt, va_list ap); -int ios_vprintf(ios_t *s, const char *format, va_list args) +int ios_vprintf(struct ios *s, const char *format, va_list args) { char *str = NULL; int c; @@ -989,7 +991,7 @@ int ios_vprintf(ios_t *s, const char *format, va_list args) return c; } -int ios_printf(ios_t *s, const char *format, ...) +int ios_printf(struct ios *s, const char *format, ...) { va_list args; int c; diff --git a/c/ios.h b/c/ios.h index 1774c01..c143a75 100644 --- a/c/ios.h +++ b/c/ios.h @@ -11,7 +11,7 @@ typedef enum { bst_none, bst_rd, bst_wr } bufstate_t; #define IOS_INLSIZE 54 #define IOS_BUFSIZE 131072 -typedef struct { +struct ios { bufmode_t bm; // the state only indicates where the underlying file position is relative @@ -54,82 +54,85 @@ typedef struct { // todo: mutex char local[IOS_INLSIZE]; -} ios_t; +}; /* low-level interface functions */ -size_t ios_read(ios_t *s, char *dest, size_t n); -size_t ios_readall(ios_t *s, char *dest, size_t n); -size_t ios_write(ios_t *s, char *data, size_t n); -off_t ios_seek(ios_t *s, off_t pos); // absolute seek -off_t ios_seek_end(ios_t *s); -off_t ios_skip(ios_t *s, off_t offs); // relative seek -off_t ios_pos(ios_t *s); // get current position -size_t ios_trunc(ios_t *s, size_t size); -int ios_eof(ios_t *s); -int ios_flush(ios_t *s); -void ios_close(ios_t *s); -char *ios_takebuf(ios_t *s, size_t *psize); // release buffer to caller +size_t ios_read(struct ios *s, char *dest, size_t n); +size_t ios_readall(struct ios *s, char *dest, size_t n); +size_t ios_write(struct ios *s, char *data, size_t n); +off_t ios_seek(struct ios *s, off_t pos); // absolute seek +off_t ios_seek_end(struct ios *s); +off_t ios_skip(struct ios *s, off_t offs); // relative seek +off_t ios_pos(struct ios *s); // get current position +size_t ios_trunc(struct ios *s, size_t size); +int ios_eof(struct ios *s); +int ios_flush(struct ios *s); +void ios_close(struct ios *s); +char *ios_takebuf(struct ios *s, + size_t *psize); // release buffer to caller // set buffer space to use -int ios_setbuf(ios_t *s, char *buf, size_t size, int own); -int ios_bufmode(ios_t *s, bufmode_t mode); -void ios_set_readonly(ios_t *s); -size_t ios_copy(ios_t *to, ios_t *from, size_t nbytes); -size_t ios_copyall(ios_t *to, ios_t *from); -size_t ios_copyuntil(ios_t *to, ios_t *from, char delim); +int ios_setbuf(struct ios *s, char *buf, size_t size, int own); +int ios_bufmode(struct ios *s, bufmode_t mode); +void ios_set_readonly(struct ios *s); +size_t ios_copy(struct ios *to, struct ios *from, size_t nbytes); +size_t ios_copyall(struct ios *to, struct ios *from); +size_t ios_copyuntil(struct ios *to, struct ios *from, char delim); // ensure at least n bytes are buffered if possible. returns # available. -size_t ios_readprep(ios_t *from, size_t n); -// void ios_lock(ios_t *s); -// int ios_trylock(ios_t *s); -// int ios_unlock(ios_t *s); +size_t ios_readprep(struct ios *from, size_t n); +// void ios_lock(struct ios *s); +// int struct iosrylock(struct ios *s); +// int ios_unlock(struct ios *s); /* stream creation */ -ios_t *ios_file(ios_t *s, char *fname, int rd, int wr, int create, int trunc); -ios_t *ios_mem(ios_t *s, size_t initsize); -ios_t *ios_str(ios_t *s, char *str); -ios_t *ios_static_buffer(ios_t *s, char *buf, size_t sz); -ios_t *ios_fd(ios_t *s, long fd, int isfile, int own); +struct ios *ios_file(struct ios *s, char *fname, int rd, int wr, int create, + int trunc); +struct ios *ios_mem(struct ios *s, size_t initsize); +struct ios *ios_str(struct ios *s, char *str); +struct ios *ios_static_buffer(struct ios *s, char *buf, size_t sz); +struct ios *ios_fd(struct ios *s, long fd, int isfile, int own); // todo: ios_socket -extern ios_t *ios_stdin; -extern ios_t *ios_stdout; -extern ios_t *ios_stderr; +extern struct ios *ios_stdin; +extern struct ios *ios_stdout; +extern struct ios *ios_stderr; void ios_init_stdstreams(); /* high-level functions - output */ -int ios_putnum(ios_t *s, char *data, uint32_t type); -int ios_putint(ios_t *s, int n); -int ios_pututf8(ios_t *s, uint32_t wc); -int ios_putstringz(ios_t *s, char *str, bool_t do_write_nulterm); -int ios_printf(ios_t *s, const char *format, ...); -int ios_vprintf(ios_t *s, const char *format, va_list args); +int ios_putnum(struct ios *s, char *data, uint32_t type); +int ios_putint(struct ios *s, int n); +int ios_pututf8(struct ios *s, uint32_t wc); +int ios_putstringz(struct ios *s, char *str, bool_t do_write_nulterm); +int ios_printf(struct ios *s, const char *format, ...); +int ios_vprintf(struct ios *s, const char *format, va_list args); -void hexdump(ios_t *dest, const char *buffer, size_t len, size_t startoffs); +void hexdump(struct ios *dest, const char *buffer, size_t len, + size_t startoffs); /* high-level stream functions - input */ -int ios_getnum(ios_t *s, char *data, uint32_t type); -int ios_getutf8(ios_t *s, uint32_t *pwc); -int ios_peekutf8(ios_t *s, uint32_t *pwc); -int ios_ungetutf8(ios_t *s, uint32_t wc); -int ios_getstringz(ios_t *dest, ios_t *src); -int ios_getstringn(ios_t *dest, ios_t *src, size_t nchars); -int ios_getline(ios_t *s, char **pbuf, size_t *psz); -char *ios_readline(ios_t *s); +int ios_getnum(struct ios *s, char *data, uint32_t type); +int ios_getutf8(struct ios *s, uint32_t *pwc); +int ios_peekutf8(struct ios *s, uint32_t *pwc); +int ios_ungetutf8(struct ios *s, uint32_t wc); +int ios_getstringz(struct ios *dest, struct ios *src); +int ios_getstringn(struct ios *dest, struct ios *src, size_t nchars); +int ios_getline(struct ios *s, char **pbuf, size_t *psz); +char *ios_readline(struct ios *s); // discard data buffered for reading -void ios_purge(ios_t *s); +void ios_purge(struct ios *s); // seek by utf8 sequence increments -int ios_nextutf8(ios_t *s); -int ios_prevutf8(ios_t *s); +int ios_nextutf8(struct ios *s); +int ios_prevutf8(struct ios *s); /* stdio-style functions */ #define IOS_EOF (-1) -int ios_putc(int c, ios_t *s); -// wint_t ios_putwc(ios_t *s, wchar_t wc); -int ios_getc(ios_t *s); -int ios_peekc(ios_t *s); -// wint_t ios_getwc(ios_t *s); -int ios_ungetc(int c, ios_t *s); -// wint_t ios_ungetwc(ios_t *s, wint_t wc); +int ios_putc(int c, struct ios *s); +// wint_t ios_putwc(struct ios *s, wchar_t wc); +int ios_getc(struct ios *s); +int ios_peekc(struct ios *s); +// wint_t ios_getwc(struct ios *s); +int ios_ungetc(int c, struct ios *s); +// wint_t ios_ungetwc(struct ios *s, wint_t wc); #define ios_puts(str, s) ios_write(s, str, strlen(str)) /* diff --git a/c/iostream.c b/c/iostream.c index e137b6b..3616061 100644 --- a/c/iostream.c +++ b/c/iostream.c @@ -27,7 +27,7 @@ static value_t iostreamsym, rdsym, wrsym, apsym, crsym, truncsym; static value_t instrsym, outstrsym; fltype_t *iostreamtype; -void print_iostream(value_t v, ios_t *f) +void print_iostream(value_t v, struct ios *f) { (void)v; fl_print_str("#", f); @@ -35,14 +35,14 @@ void print_iostream(value_t v, ios_t *f) void free_iostream(value_t self) { - ios_t *s = value2c(ios_t *, self); + struct ios *s = value2c(struct ios *, self); ios_close(s); } void relocate_iostream(value_t oldv, value_t newv) { - ios_t *olds = value2c(ios_t *, oldv); - ios_t *news = value2c(ios_t *, newv); + struct ios *olds = value2c(struct ios *, oldv); + struct ios *news = value2c(struct ios *, newv); if (news->buf == &olds->local[0]) { news->buf = &news->local[0]; } @@ -75,14 +75,17 @@ value_t fl_eof_objectp(value_t *args, uint32_t nargs) return (FL_EOF == args[0]) ? FL_T : FL_F; } -static ios_t *toiostream(value_t v, char *fname) +static struct ios *toiostream(value_t v, char *fname) { if (!fl_isiostream(v)) type_error(fname, "iostream", v); - return value2c(ios_t *, v); + return value2c(struct ios *, v); } -ios_t *fl_toiostream(value_t v, char *fname) { return toiostream(v, fname); } +struct ios *fl_toiostream(value_t v, char *fname) +{ + return toiostream(v, fname); +} value_t fl_file(value_t *args, uint32_t nargs) { @@ -106,9 +109,9 @@ value_t fl_file(value_t *args, uint32_t nargs) } if ((r | w | c | t | a) == 0) r = 1; // default to reading - value_t f = cvalue(iostreamtype, sizeof(ios_t)); + value_t f = cvalue(iostreamtype, sizeof(struct ios)); char *fname = tostring(args[0], "file"); - ios_t *s = value2c(ios_t *, f); + struct ios *s = value2c(struct ios *, f); if (ios_file(s, fname, r, w, c, t) == NULL) lerrorf(IOError, "file: could not open \"%s\"", fname); if (a) @@ -120,8 +123,8 @@ value_t fl_buffer(value_t *args, u_int32_t nargs) { argcount("buffer", nargs, 0); (void)args; - value_t f = cvalue(iostreamtype, sizeof(ios_t)); - ios_t *s = value2c(ios_t *, f); + value_t f = cvalue(iostreamtype, sizeof(struct ios)); + struct ios *s = value2c(struct ios *, f); if (ios_mem(s, 0) == NULL) lerror(MemoryError, "buffer: could not allocate stream"); return f; @@ -141,7 +144,7 @@ value_t fl_read(value_t *args, u_int32_t nargs) fl_gc_handle(&arg); value_t v = fl_read_sexpr(arg); fl_free_gc_handles(1); - if (ios_eof(value2c(ios_t *, arg))) + if (ios_eof(value2c(struct ios *, arg))) return FL_EOF; return v; } @@ -149,7 +152,7 @@ value_t fl_read(value_t *args, u_int32_t nargs) value_t fl_iogetc(value_t *args, u_int32_t nargs) { argcount("io.getc", nargs, 1); - ios_t *s = toiostream(args[0], "io.getc"); + struct ios *s = toiostream(args[0], "io.getc"); uint32_t wc; if (ios_getutf8(s, &wc) == IOS_EOF) // lerror(IOError, "io.getc: end of file reached"); @@ -160,7 +163,7 @@ value_t fl_iogetc(value_t *args, u_int32_t nargs) value_t fl_iopeekc(value_t *args, u_int32_t nargs) { argcount("io.peekc", nargs, 1); - ios_t *s = toiostream(args[0], "io.peekc"); + struct ios *s = toiostream(args[0], "io.peekc"); uint32_t wc; if (ios_peekutf8(s, &wc) == IOS_EOF) return FL_EOF; @@ -170,7 +173,7 @@ value_t fl_iopeekc(value_t *args, u_int32_t nargs) value_t fl_ioputc(value_t *args, u_int32_t nargs) { argcount("io.putc", nargs, 2); - ios_t *s = toiostream(args[0], "io.putc"); + struct ios *s = toiostream(args[0], "io.putc"); if (!iscprim(args[1]) || ((cprim_t *)ptr(args[1]))->type != wchartype) type_error("io.putc", "wchar", args[1]); uint32_t wc = *(uint32_t *)cp_data((cprim_t *)ptr(args[1])); @@ -180,7 +183,7 @@ value_t fl_ioputc(value_t *args, u_int32_t nargs) value_t fl_ioungetc(value_t *args, u_int32_t nargs) { argcount("io.ungetc", nargs, 2); - ios_t *s = toiostream(args[0], "io.ungetc"); + struct ios *s = toiostream(args[0], "io.ungetc"); if (!iscprim(args[1]) || ((cprim_t *)ptr(args[1]))->type != wchartype) type_error("io.ungetc", "wchar", args[1]); uint32_t wc = *(uint32_t *)cp_data((cprim_t *)ptr(args[1])); @@ -193,7 +196,7 @@ value_t fl_ioungetc(value_t *args, u_int32_t nargs) value_t fl_ioflush(value_t *args, u_int32_t nargs) { argcount("io.flush", nargs, 1); - ios_t *s = toiostream(args[0], "io.flush"); + struct ios *s = toiostream(args[0], "io.flush"); if (ios_flush(s) != 0) return FL_F; return FL_T; @@ -202,7 +205,7 @@ value_t fl_ioflush(value_t *args, u_int32_t nargs) value_t fl_ioclose(value_t *args, u_int32_t nargs) { argcount("io.close", nargs, 1); - ios_t *s = toiostream(args[0], "io.close"); + struct ios *s = toiostream(args[0], "io.close"); ios_close(s); return FL_T; } @@ -210,7 +213,7 @@ value_t fl_ioclose(value_t *args, u_int32_t nargs) value_t fl_iopurge(value_t *args, u_int32_t nargs) { argcount("io.discardbuffer", nargs, 1); - ios_t *s = toiostream(args[0], "io.discardbuffer"); + struct ios *s = toiostream(args[0], "io.discardbuffer"); ios_purge(s); return FL_T; } @@ -218,14 +221,14 @@ value_t fl_iopurge(value_t *args, u_int32_t nargs) value_t fl_ioeof(value_t *args, u_int32_t nargs) { argcount("io.eof?", nargs, 1); - ios_t *s = toiostream(args[0], "io.eof?"); + struct ios *s = toiostream(args[0], "io.eof?"); return (ios_eof(s) ? FL_T : FL_F); } value_t fl_ioseek(value_t *args, u_int32_t nargs) { argcount("io.seek", nargs, 2); - ios_t *s = toiostream(args[0], "io.seek"); + struct ios *s = toiostream(args[0], "io.seek"); size_t pos = toulong(args[1], "io.seek"); off_t res = ios_seek(s, (off_t)pos); if (res == -1) @@ -236,7 +239,7 @@ value_t fl_ioseek(value_t *args, u_int32_t nargs) value_t fl_iopos(value_t *args, u_int32_t nargs) { argcount("io.pos", nargs, 1); - ios_t *s = toiostream(args[0], "io.pos"); + struct ios *s = toiostream(args[0], "io.pos"); off_t res = ios_pos(s); if (res == -1) return FL_F; @@ -247,7 +250,7 @@ value_t fl_write(value_t *args, u_int32_t nargs) { if (nargs < 1 || nargs > 2) argcount("write", nargs, 1); - ios_t *s; + struct ios *s; if (nargs == 2) s = toiostream(args[1], "write"); else @@ -279,7 +282,7 @@ value_t fl_ioread(value_t *args, u_int32_t nargs) data = cv_data((cvalue_t *)ptr(cv)); else data = cp_data((cprim_t *)ptr(cv)); - size_t got = ios_read(value2c(ios_t *, args[0]), data, n); + size_t got = ios_read(value2c(struct ios *, args[0]), data, n); if (got < n) // lerror(IOError, "io.read: end of input reached"); return FL_EOF; @@ -305,7 +308,7 @@ value_t fl_iowrite(value_t *args, u_int32_t nargs) { if (nargs < 2 || nargs > 4) argcount("io.write", nargs, 2); - ios_t *s = toiostream(args[0], "io.write"); + struct ios *s = toiostream(args[0], "io.write"); if (iscprim(args[1]) && ((cprim_t *)ptr(args[1]))->type == wchartype) { if (nargs > 2) lerror(ArgError, @@ -328,7 +331,7 @@ value_t fl_dump(value_t *args, u_int32_t nargs) { if (nargs < 1 || nargs > 3) argcount("dump", nargs, 1); - ios_t *s = toiostream(symbol_value(outstrsym), "dump"); + struct ios *s = toiostream(symbol_value(outstrsym), "dump"); char *data; size_t sz, offs = 0; to_sized_ptr(args[0], "dump", &data, &sz); @@ -359,11 +362,11 @@ value_t fl_ioreaduntil(value_t *args, u_int32_t nargs) value_t str = cvalue_string(80); cvalue_t *cv = (cvalue_t *)ptr(str); char *data = cv_data(cv); - ios_t dest; + struct ios dest; ios_mem(&dest, 0); ios_setbuf(&dest, data, 80, 0); char delim = get_delim_arg(args[1], "io.readuntil"); - ios_t *src = toiostream(args[0], "io.readuntil"); + struct ios *src = toiostream(args[0], "io.readuntil"); size_t n = ios_copyuntil(&dest, src, delim); cv->len = n; if (dest.buf != data) { @@ -382,8 +385,8 @@ value_t fl_ioreaduntil(value_t *args, u_int32_t nargs) value_t fl_iocopyuntil(value_t *args, u_int32_t nargs) { argcount("io.copyuntil", nargs, 3); - ios_t *dest = toiostream(args[0], "io.copyuntil"); - ios_t *src = toiostream(args[1], "io.copyuntil"); + struct ios *dest = toiostream(args[0], "io.copyuntil"); + struct ios *src = toiostream(args[1], "io.copyuntil"); char delim = get_delim_arg(args[2], "io.copyuntil"); return size_wrap(ios_copyuntil(dest, src, delim)); } @@ -392,8 +395,8 @@ value_t fl_iocopy(value_t *args, u_int32_t nargs) { if (nargs < 2 || nargs > 3) argcount("io.copy", nargs, 2); - ios_t *dest = toiostream(args[0], "io.copy"); - ios_t *src = toiostream(args[1], "io.copy"); + struct ios *dest = toiostream(args[0], "io.copy"); + struct ios *src = toiostream(args[1], "io.copy"); if (nargs == 3) { size_t n = toulong(args[2], "io.copy"); return size_wrap(ios_copy(dest, src, n)); @@ -405,12 +408,12 @@ value_t stream_to_string(value_t *ps) { value_t str; size_t n; - ios_t *st = value2c(ios_t *, *ps); + struct ios *st = value2c(struct ios *, *ps); if (st->buf == &st->local[0]) { n = st->size; str = cvalue_string(n); - memcpy(cvalue_data(str), value2c(ios_t *, *ps)->buf, n); - ios_trunc(value2c(ios_t *, *ps), 0); + memcpy(cvalue_data(str), value2c(struct ios *, *ps)->buf, n); + ios_trunc(value2c(struct ios *, *ps), 0); } else { char *b = ios_takebuf(st, &n); n--; @@ -426,7 +429,7 @@ value_t stream_to_string(value_t *ps) value_t fl_iotostring(value_t *args, u_int32_t nargs) { argcount("io.tostring!", nargs, 1); - ios_t *src = toiostream(args[0], "io.tostring!"); + struct ios *src = toiostream(args[0], "io.tostring!"); if (src->bm != bm_mem) lerror(ArgError, "io.tostring!: requires memory stream"); return stream_to_string(&args[0]); @@ -471,14 +474,14 @@ void iostream_init(void) truncsym = symbol(":truncate"); instrsym = symbol("*input-stream*"); outstrsym = symbol("*output-stream*"); - iostreamtype = - define_opaque_type(iostreamsym, sizeof(ios_t), &iostream_vtable, NULL); + iostreamtype = define_opaque_type(iostreamsym, sizeof(struct ios), + &iostream_vtable, NULL); assign_global_builtins(iostreamfunc_info); - setc(symbol("*stdout*"), - cvalue_from_ref(iostreamtype, ios_stdout, sizeof(ios_t), FL_NIL)); - setc(symbol("*stderr*"), - cvalue_from_ref(iostreamtype, ios_stderr, sizeof(ios_t), FL_NIL)); - setc(symbol("*stdin*"), - cvalue_from_ref(iostreamtype, ios_stdin, sizeof(ios_t), FL_NIL)); + setc(symbol("*stdout*"), cvalue_from_ref(iostreamtype, ios_stdout, + sizeof(struct ios), FL_NIL)); + setc(symbol("*stderr*"), cvalue_from_ref(iostreamtype, ios_stderr, + sizeof(struct ios), FL_NIL)); + setc(symbol("*stdin*"), cvalue_from_ref(iostreamtype, ios_stdin, + sizeof(struct ios), FL_NIL)); } diff --git a/c/print.h b/c/print.h index 0fd6c7f..a4e10e4 100644 --- a/c/print.h +++ b/c/print.h @@ -10,7 +10,7 @@ static fixnum_t P_LEVEL; static int SCR_WIDTH = 80; static int HPOS = 0, VPOS; -static void outc(char c, ios_t *f) +static void outc(char c, struct ios *f) { ios_putc(c, f); if (c == '\n') @@ -18,17 +18,17 @@ static void outc(char c, ios_t *f) else HPOS++; } -static void outs(char *s, ios_t *f) +static void outs(char *s, struct ios *f) { ios_puts(s, f); HPOS += u8_strwidth(s); } -static void outsn(char *s, ios_t *f, size_t n) +static void outsn(char *s, struct ios *f, size_t n) { ios_write(f, s, n); HPOS += u8_strwidth(s); } -static int outindent(int n, ios_t *f) +static int outindent(int n, struct ios *f) { // move back to left margin if we get too indented if (n > SCR_WIDTH - 12) @@ -44,9 +44,9 @@ static int outindent(int n, ios_t *f) return n0; } -void fl_print_chr(char c, ios_t *f) { outc(c, f); } +void fl_print_chr(char c, struct ios *f) { outc(c, f); } -void fl_print_str(char *s, ios_t *f) { outs(s, f); } +void fl_print_str(char *s, struct ios *f) { outs(s, f); } void print_traverse(value_t v) { @@ -96,7 +96,7 @@ void print_traverse(value_t v) } } -static void print_symbol_name(ios_t *f, char *name) +static void print_symbol_name(struct ios *f, char *name) { int i, escape = 0, charescape = 0; @@ -243,7 +243,7 @@ static int blockindent(value_t v) return (allsmallp(v) > 9); } -static void print_pair(ios_t *f, value_t v) +static void print_pair(struct ios *f, value_t v) { value_t cd; char *op = NULL; @@ -334,9 +334,9 @@ static void print_pair(ios_t *f, value_t v) } } -static void cvalue_print(ios_t *f, value_t v); +static void cvalue_print(struct ios *f, value_t v); -static int print_circle_prefix(ios_t *f, value_t v) +static int print_circle_prefix(struct ios *f, value_t v) { value_t label; if ((label = (value_t)ptrhash_get(&printconses, (void *)v)) != @@ -352,7 +352,7 @@ static int print_circle_prefix(ios_t *f, value_t v) return 0; } -void fl_print_child(ios_t *f, value_t v) +void fl_print_child(struct ios *f, value_t v) { char *name; if (print_level >= 0 && P_LEVEL >= print_level && @@ -469,7 +469,7 @@ void fl_print_child(ios_t *f, value_t v) P_LEVEL--; } -static void print_string(ios_t *f, char *str, size_t sz) +static void print_string(struct ios *f, char *str, size_t sz) { char buf[512]; size_t i = 0; @@ -593,8 +593,8 @@ static numerictype_t sym_to_numtype(value_t type); // for example #int32(0) can be printed as just 0. this is used // printing in a context where a type is already implied, e.g. inside // an array. -static void cvalue_printdata(ios_t *f, void *data, size_t len, value_t type, - int weak) +static void cvalue_printdata(struct ios *f, void *data, size_t len, + value_t type, int weak) { if (type == bytesym) { unsigned char ch = *(unsigned char *)data; @@ -778,7 +778,7 @@ static void cvalue_printdata(ios_t *f, void *data, size_t len, value_t type, } } -static void cvalue_print(ios_t *f, value_t v) +static void cvalue_print(struct ios *f, value_t v) { cvalue_t *cv = (cvalue_t *)ptr(v); void *data = cptr(v); @@ -817,7 +817,7 @@ static void set_print_width(void) SCR_WIDTH = numval(pw); } -void fl_print(ios_t *f, value_t v) +void fl_print(struct ios *f, value_t v) { print_pretty = (symbol_value(printprettysym) != FL_F); if (print_pretty) diff --git a/c/read.h b/c/read.h index a5b692e..f1c22b1 100644 --- a/c/read.h +++ b/c/read.h @@ -22,7 +22,7 @@ enum { TOK_DOUBLEQUOTE }; -#define F value2c(ios_t *, readstate->source) +#define F value2c(struct ios *, readstate->source) // defines which characters are ordinary symbol characters. // exceptions are '.', which is an ordinary symbol character @@ -121,7 +121,7 @@ static char nextchar(void) { int ch; char c; - ios_t *f = F; + struct ios *f = F; do { if (f->bpos < f->size) { diff --git a/c/string.c b/c/string.c index 3ed171a..1f9c6af 100644 --- a/c/string.c +++ b/c/string.c @@ -144,7 +144,7 @@ value_t fl_string(value_t *args, u_int32_t nargs) return args[0]; value_t arg, buf = fl_buffer(NULL, 0); fl_gc_handle(&buf); - ios_t *s = value2c(ios_t *, buf); + struct ios *s = value2c(struct ios *, buf); uint32_t i; value_t oldpr = symbol_value(printreadablysym); value_t oldpp = symbol_value(printprettysym); diff --git a/c/table.c b/c/table.c index ab2df5b..62e436f 100644 --- a/c/table.c +++ b/c/table.c @@ -27,7 +27,7 @@ static value_t tablesym; static fltype_t *tabletype; -void print_htable(value_t v, ios_t *f) +void print_htable(value_t v, struct ios *f) { struct htable *h = (struct htable *)cv_data((cvalue_t *)ptr(v)); size_t i;