shithub: femtolisp

Download patch

ref: 9e07001ae0fc0f4ea1049b27d17b68405b71d9a1
parent: 0d643a05fece09a79720684bc67ecd6bf035a6c1
author: JeffBezanson <jeff.bezanson@gmail.com>
date: Sat Aug 28 01:07:02 EDT 2010

adding \e character escape
calling GC_init when appropriate
fixing some ios bugs
adding ios_static_buffer


--- a/femtolisp/flisp.c
+++ b/femtolisp/flisp.c
@@ -2266,6 +2266,9 @@
 
 void fl_init(size_t initial_heapsize)
 {
+#ifdef BOEHM_GC
+    GC_init();
+#endif
     lisp_init(initial_heapsize);
 }
 
--- a/llt/int2str.c
+++ b/llt/int2str.c
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include "dtypes.h"
+#include "utils.h"
 
 char *uint2str(char *dest, size_t len, uint64_t num, uint32_t base)
 {
--- a/llt/ios.c
+++ b/llt/ios.c
@@ -249,19 +249,19 @@
     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 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 @@
     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_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 @@
     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 @@
     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;
--- a/llt/ios.h
+++ b/llt/ios.h
@@ -93,6 +93,7 @@
 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;
--- 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 */
 
--- a/llt/socket.c
+++ b/llt/socket.c
@@ -50,8 +50,6 @@
     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;
     }
 
--- a/llt/utf8.c
+++ b/llt/utf8.c
@@ -337,6 +337,8 @@
         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 @@
         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')