diff --git a/femtolisp/flisp.c b/femtolisp/flisp.c index 1e73d69..5bba6ba 100644 --- a/femtolisp/flisp.c +++ b/femtolisp/flisp.c @@ -2266,6 +2266,9 @@ value_t fl_toplevel_eval(value_t expr) void fl_init(size_t initial_heapsize) { +#ifdef BOEHM_GC + GC_init(); +#endif lisp_init(initial_heapsize); } diff --git a/llt/int2str.c b/llt/int2str.c index f3abec3..9a8084e 100644 --- a/llt/int2str.c +++ b/llt/int2str.c @@ -1,5 +1,6 @@ #include #include "dtypes.h" +#include "utils.h" char *uint2str(char *dest, size_t len, uint64_t num, uint32_t base) { diff --git a/llt/ios.c b/llt/ios.c index 73f3eda..e8f7bb5 100644 --- a/llt/ios.c +++ b/llt/ios.c @@ -249,19 +249,19 @@ static size_t _ios_read(ios_t *s, char *dest, size_t n, int all) while (n > 0) { avail = s->size - s->bpos; - if (avail >= n) { - memcpy(dest, s->buf + s->bpos, n); - s->bpos += n; - return tot+n; - } - if (avail > 0) { - memcpy(dest, s->buf + s->bpos, avail); + size_t ncopy = (avail >= n) ? n : avail; + memcpy(dest, s->buf + s->bpos, ncopy); + s->bpos += ncopy; + if (ncopy >= n) { + s->state = bst_rd; + return tot+ncopy; + } } if (s->bm == bm_mem || s->fd == -1) { // can't get any more data - s->bpos += avail; - if (avail == 0 && n > 0) + s->state = bst_rd; + if (avail == 0) s->_eof = 1; return avail; } @@ -362,10 +362,16 @@ size_t ios_write(ios_t *s, char *data, size_t n) size_t wrote = 0; if (s->state == bst_none) s->state = bst_wr; - if (s->state == bst_wr) - space = s->maxsize - s->bpos; - else + if (s->state == bst_rd) { + if (!s->rereadable) { + s->size = 0; + s->bpos = 0; + } space = s->size - s->bpos; + } + else { + space = s->maxsize - s->bpos; + } if (s->bm == bm_mem) { wrote = _write_grow(s, data, n); @@ -766,6 +772,15 @@ ios_t *ios_str(ios_t *s, char *str) return s; } +ios_t *ios_static_buffer(ios_t *s, char *buf, size_t sz) +{ + ios_mem(s, 0); + ios_setbuf(s, buf, sz, 0); + s->size = sz; + ios_set_readonly(s); + return s; +} + ios_t *ios_fd(ios_t *s, long fd, int isfile) { _ios_init(s); @@ -812,23 +827,26 @@ int ios_putc(int c, ios_t *s) int ios_getc(ios_t *s) { - if (s->bpos < s->size) - return s->buf[s->bpos++]; - if (s->_eof) return IOS_EOF; char ch; - if (ios_read(s, &ch, 1) < 1) - return IOS_EOF; - return (int)ch; + if (s->state == bst_rd && s->bpos < s->size) { + ch = s->buf[s->bpos++]; + } + else { + if (s->_eof) return IOS_EOF; + if (ios_read(s, &ch, 1) < 1) + return IOS_EOF; + } + return (unsigned char)ch; } int ios_peekc(ios_t *s) { if (s->bpos < s->size) - return s->buf[s->bpos]; + return (unsigned char)s->buf[s->bpos]; if (s->_eof) return IOS_EOF; size_t n = ios_readprep(s, 1); if (n == 0) return IOS_EOF; - return s->buf[s->bpos]; + return (unsigned char)s->buf[s->bpos]; } int ios_ungetc(int c, ios_t *s) @@ -863,11 +881,11 @@ int ios_getutf8(ios_t *s, uint32_t *pwc) if (c == IOS_EOF) return IOS_EOF; c0 = (char)c; - sz = u8_seqlen(&c0)-1; - if (sz == 0) { - *pwc = (uint32_t)c0; + if ((unsigned char)c0 < 0x80) { + *pwc = (uint32_t)(unsigned char)c0; return 1; } + sz = u8_seqlen(&c0)-1; if (ios_ungetc(c, s) == IOS_EOF) return IOS_EOF; if (ios_readprep(s, sz) < sz) @@ -889,11 +907,11 @@ int ios_peekutf8(ios_t *s, uint32_t *pwc) if (c == IOS_EOF) return IOS_EOF; c0 = (char)c; - sz = u8_seqlen(&c0)-1; - if (sz == 0) { - *pwc = (uint32_t)c0; + if ((unsigned char)c0 < 0x80) { + *pwc = (uint32_t)(unsigned char)c0; return 1; } + sz = u8_seqlen(&c0)-1; if (ios_readprep(s, sz) < sz) return IOS_EOF; size_t i = s->bpos; diff --git a/llt/ios.h b/llt/ios.h index 1a402b4..f55b4b2 100644 --- a/llt/ios.h +++ b/llt/ios.h @@ -93,6 +93,7 @@ size_t ios_readprep(ios_t *from, size_t n); 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); // todo: ios_socket extern ios_t *ios_stdin; diff --git a/llt/mt19937ar.c b/llt/mt19937ar.c index 5b806d8..454b515 100644 --- a/llt/mt19937ar.c +++ b/llt/mt19937ar.c @@ -46,7 +46,7 @@ /* Period parameters */ #define mtN 624 #define mtM 397 -#define MATRIX_A 0x9908b0dfU /* constant vector a */ +#define MATRIX_A 0x9908b0dfU /* constant vector a */ #define UPPER_MASK 0x80000000U /* most significant w-r bits */ #define LOWER_MASK 0x7fffffffU /* least significant r bits */ diff --git a/llt/socket.c b/llt/socket.c index 306a07f..aff16b9 100644 --- a/llt/socket.c +++ b/llt/socket.c @@ -50,8 +50,6 @@ int open_tcp_port(short portno) serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { - fprintf(stderr, "could not bind to port %d.\n", - portno); return -1; } diff --git a/llt/utf8.c b/llt/utf8.c index a6dd07e..082f78f 100644 --- a/llt/utf8.c +++ b/llt/utf8.c @@ -337,6 +337,8 @@ char read_escape_control_char(char c) return '\t'; else if (c == 'r') return '\r'; + else if (c == 'e') + return '\e'; else if (c == 'b') return '\b'; else if (c == 'f') @@ -432,6 +434,8 @@ int u8_escape_wchar(char *buf, size_t sz, u_int32_t ch) return buf_put2c(buf, "\\t"); else if (ch == L'\r') return buf_put2c(buf, "\\r"); + else if (ch == L'\e') + return buf_put2c(buf, "\\e"); else if (ch == L'\b') return buf_put2c(buf, "\\b"); else if (ch == L'\f')