ref: 171ae9573303fe9129baf8453fb44ea088b4bb2f
parent: 0379b2da811c64c690aa35fb99f0a115aa7919e0
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Nov 6 19:52:47 EST 2024
ios: char → uint8_t; remove a few unused fields
--- a/cvalues.c
+++ b/cvalues.c
@@ -668,7 +668,7 @@
// get pointer and size for any plain-old-data value
void
-to_sized_ptr(value_t v, char **pdata, size_t *psz)
+to_sized_ptr(value_t v, uint8_t **pdata, size_t *psz)
{
if(iscvalue(v)){
cvalue_t *pcv = ptr(v);
@@ -696,10 +696,11 @@
BUILTIN("sizeof", sizeof)
{
argcount(nargs, 1);
- size_t n; char *data;
int a;
if(issymbol(args[0]) || iscons(args[0]))
return size_wrap(ctype_sizeof(args[0], &a));
+ size_t n;
+ uint8_t *data;
to_sized_ptr(args[0], &data, &n);
return size_wrap(n);
}
--- a/cvalues.h
+++ b/cvalues.h
@@ -52,7 +52,7 @@
int cvalue_array_init(fltype_t *ft, value_t arg, void *dest);
size_t cvalue_arraylen(value_t v);
size_t ctype_sizeof(value_t type, int *palign);
-void to_sized_ptr(value_t v, char **pdata, size_t *psz);
+void to_sized_ptr(value_t v, uint8_t **pdata, size_t *psz);
value_t cvalue_relocate(value_t v);
value_t cvalue_copy(value_t v);
value_t cvalue_compare(value_t a, value_t b);
--- a/dump.c
+++ b/dump.c
@@ -7,7 +7,7 @@
address label being startoffs
*/
void
-hexdump(ios_t *dest, const char *buffer, size_t len, size_t startoffs)
+hexdump(ios_t *dest, const uint8_t *buffer, size_t len, size_t startoffs)
{
size_t offs = 0;
size_t i, pos;
--- a/flisp.h
+++ b/flisp.h
@@ -317,13 +317,13 @@
size_t len; // length of *data in bytes
union {
value_t parent; // optional
- char _space[1]; // variable size
+ uint8_t _space[1]; // variable size
};
}cvalue_t;
typedef struct {
fltype_t *type;
- char _space[1];
+ uint8_t _space[1];
}cprim_t;
typedef struct {
--- a/flmain.c
+++ b/flmain.c
@@ -25,7 +25,7 @@
}
_Noreturn void
-flmain(const char *boot, int bootsz, int argc, char **argv)
+flmain(const uint8_t *boot, int bootsz, int argc, char **argv)
{
D_PNAN = D_NNAN = strtod("+NaN", nil);
D_PINF = D_NINF = strtod("+Inf", nil);
--- a/ios.c
+++ b/ios.c
@@ -32,12 +32,12 @@
// return error code, #bytes read in *nread
// these wrappers retry operations until success or a fatal error
static int
-_os_read(long fd, char *buf, size_t n, size_t *nread)
+_os_read(int fd, uint8_t *buf, size_t n, size_t *nread)
{
ssize_t r;
while(1){
- r = read((int)fd, buf, n);
+ r = read(fd, buf, n);
if(r > -1){
*nread = (size_t)r;
break;
@@ -56,7 +56,7 @@
}
static int
-_os_read_all(long fd, char *buf, size_t n, size_t *nread)
+_os_read_all(int fd, uint8_t *buf, size_t n, size_t *nread)
{
size_t got;
@@ -98,7 +98,7 @@
}
static int
-_os_write_all(long fd, const char *buf, size_t n, size_t *nwritten)
+_os_write_all(long fd, const uint8_t *buf, size_t n, size_t *nwritten)
{
size_t wrote;
@@ -117,10 +117,10 @@
/* internal utility functions */
-static char *
+static uint8_t *
_buf_realloc(ios_t *s, size_t sz)
{
- char *temp;
+ uint8_t *temp;
if((s->buf == nil || s->buf == &s->local[0]) && sz <= IOS_INLSIZE){
/* TODO: if we want to allow shrinking, see if the buffer shrank
@@ -158,7 +158,7 @@
// 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, const char *data, size_t n)
+_write_grow(ios_t *s, const void *data, size_t n)
{
size_t amt;
size_t newsize;
@@ -176,9 +176,8 @@
if(_buf_realloc(s, newsize) == nil){
/* no more space; write as much as we can */
amt = s->maxsize - s->bpos;
- if(amt > 0){
+ if(amt > 0)
memmove(&s->buf[s->bpos], data, amt);
- }
s->bpos += amt;
s->size = s->maxsize;
return amt;
@@ -196,7 +195,7 @@
/* interface functions, low level */
static size_t
-_ios_read(ios_t *s, char *dest, size_t n, int all)
+_ios_read(ios_t *s, uint8_t *dest, size_t n, int all)
{
size_t tot = 0;
size_t got, avail;
@@ -258,7 +257,7 @@
}
size_t
-ios_read(ios_t *s, char *dest, size_t n)
+ios_read(ios_t *s, void *dest, size_t n)
{
return _ios_read(s, dest, n, 0);
}
@@ -305,7 +304,7 @@
}
size_t
-ios_write(ios_t *s, const char *data, size_t n)
+ios_write(ios_t *s, const void *data, size_t n)
{
if(s->readonly)
return 0;
@@ -336,13 +335,13 @@
if(s->bm == bm_line){
char *nl;
if((nl = llt_memrchr(data, '\n', n)) != nil){
- size_t linesz = nl-data+1;
+ size_t linesz = nl-(char*)data+1;
s->bm = bm_block;
wrote += ios_write(s, data, linesz);
ios_flush(s);
s->bm = bm_line;
n -= linesz;
- data += linesz;
+ data = (char*)data + linesz;
}
}
memmove(s->buf + s->bpos, data, n);
@@ -551,10 +550,10 @@
s->size = s->bpos = 0;
}
-char *
+uint8_t *
ios_takebuf(ios_t *s, size_t *psize)
{
- char *buf;
+ uint8_t *buf;
ios_flush(s);
@@ -581,7 +580,7 @@
}
int
-ios_setbuf(ios_t *s, char *buf, size_t size, int own)
+ios_setbuf(ios_t *s, uint8_t *buf, size_t size, int own)
{
ios_flush(s);
size_t nvalid;
@@ -667,7 +666,7 @@
#define LINE_CHUNK_SIZE 160
size_t
-ios_copyuntil(ios_t *to, ios_t *from, char delim)
+ios_copyuntil(ios_t *to, ios_t *from, uint8_t delim)
{
size_t total = 0, avail = from->size - from->bpos;
int first = 1;
@@ -678,7 +677,7 @@
avail = ios_readprep(from, LINE_CHUNK_SIZE);
}
size_t written;
- char *pd = memchr(from->buf+from->bpos, delim, avail);
+ uint8_t *pd = memchr(from->buf+from->bpos, delim, avail);
if(pd == nil){
written = ios_write(to, from->buf+from->bpos, avail);
from->bpos += avail;
@@ -705,7 +704,6 @@
s->bm = bm_block;
s->state = bst_none;
s->fpos = -1;
- s->lineno = 1;
s->fd = -1;
s->ownbuf = 1;
}
@@ -760,10 +758,10 @@
}
ios_t *
-ios_static_buffer(ios_t *s, const char *buf, size_t sz)
+ios_static_buffer(ios_t *s, const uint8_t *buf, size_t sz)
{
ios_mem(s, 0);
- ios_setbuf(s, (char*)buf, sz, 0);
+ ios_setbuf(s, (uint8_t*)buf, sz, 0);
s->size = sz;
ios_set_readonly(s);
return s;
@@ -818,16 +816,12 @@
int
ios_getc(ios_t *s)
{
- char ch;
+ uint8_t ch;
if(s->state == bst_rd && s->bpos < s->size)
ch = s->buf[s->bpos++];
- else if(s->_eof)
+ else if(s->_eof || ios_read(s, &ch, 1) < 1)
return IOS_EOF;
- else if(ios_read(s, &ch, 1) < 1)
- return IOS_EOF;
- if(ch == '\n')
- s->lineno++;
- return (uint8_t)ch;
+ return ch;
}
int
--- a/ios.h
+++ b/ios.h
@@ -25,9 +25,7 @@
// in general, you can do any operation in any state.
bufstate_t state;
- int errcode;
-
- char *buf; // start of buffer
+ uint8_t *buf; // start of buffer
size_t maxsize; // space allocated to buffer
size_t size; // length of valid data in buf, >=ndirty
size_t bpos; // current position in buffer
@@ -34,7 +32,6 @@
size_t ndirty; // # bytes at &buf[0] that need to be written
off_t fpos; // cached file pos
- size_t lineno; // current line number
int fd;
@@ -57,14 +54,14 @@
// uint8_t durable:1;
// todo: mutex
- char local[IOS_INLSIZE];
+ uint8_t local[IOS_INLSIZE];
}ios_t;
void *llt_memrchr(const void *s, int c, size_t n);
/* low-level interface functions */
-size_t ios_read(ios_t *s, char *dest, size_t n);
-size_t ios_write(ios_t *s, const char *data, size_t n);
+size_t ios_read(ios_t *s, void *dest, size_t n);
+size_t ios_write(ios_t *s, const void *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
@@ -73,14 +70,14 @@
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); // null-terminate and release buffer to caller
+uint8_t *ios_takebuf(ios_t *s, size_t *psize); // null-terminate and release buffer to caller
// set buffer space to use
-int ios_setbuf(ios_t *s, char *buf, size_t size, int own);
+int ios_setbuf(ios_t *s, uint8_t *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);
+size_t ios_copyuntil(ios_t *to, ios_t *from, uint8_t delim);
// ensure at least n bytes are buffered if possible. returns # available.
size_t ios_readprep(ios_t *from, size_t n);
@@ -90,7 +87,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, const char *buf, size_t sz);
+ios_t *ios_static_buffer(ios_t *s, const uint8_t *buf, size_t sz);
ios_t *ios_fd(ios_t *s, long fd, int isfile, int own);
// todo: ios_socket
extern ios_t *ios_stdin;
@@ -103,7 +100,7 @@
int ios_printf(ios_t *s, const char *format, ...);
int ios_vprintf(ios_t *s, const char *format, va_list args);
-void hexdump(ios_t *dest, const char *buffer, size_t len, size_t startoffs);
+void hexdump(ios_t *dest, const uint8_t *buffer, size_t len, size_t startoffs);
/* high-level stream functions - input */
int ios_getutf8(ios_t *s, Rune *r);
--- a/iostream.c
+++ b/iostream.c
@@ -254,7 +254,7 @@
n = ft->size;
}
value_t cv = cvalue(ft, n);
- char *data;
+ uint8_t *data;
if(iscvalue(cv))
data = cv_data(ptr(cv));
else data = cp_data(ptr(cv));
@@ -288,7 +288,7 @@
Rune r = *(Rune*)cp_data(ptr(args[1]));
return fixnum(ios_pututf8(s, r));
}
- char *data;
+ uint8_t *data;
size_t sz, offs = 0;
to_sized_ptr(args[1], &data, &sz);
size_t nb = sz;
@@ -304,7 +304,7 @@
if(nargs < 1 || nargs > 3)
argcount(nargs, 1);
ios_t *s = toiostream(symbol_value(outstrsym));
- char *data;
+ uint8_t *data;
size_t sz, offs = 0;
to_sized_ptr(args[0], &data, &sz);
size_t nb = sz;
@@ -333,7 +333,7 @@
argcount(nargs, 2);
value_t str = cvalue_string(80);
cvalue_t *cv = ptr(str);
- char *data = cv_data(cv);
+ uint8_t *data = cv_data(cv);
ios_t dest;
ios_mem(&dest, 0);
ios_setbuf(&dest, data, 80, 0);
@@ -347,7 +347,7 @@
cv->data = ios_takebuf(&dest, &sz);
cv_autorelease(cv);
}else{
- ((char*)cv->data)[n] = '\0';
+ ((uint8_t*)cv->data)[n] = 0;
}
if(n == 0 && ios_eof(src))
return FL_EOF;
@@ -386,7 +386,7 @@
memmove(cvalue_data(str), st->buf, n);
ios_trunc(st, 0);
}else{
- char *b = ios_takebuf(st, &n); n--;
+ uint8_t *b = ios_takebuf(st, &n); n--;
b[n] = '\0';
str = cvalue_from_ref(stringtype, b, n, FL_NIL);
cv_autorelease(ptr(str));
--- a/llt.h
+++ b/llt.h
@@ -30,4 +30,4 @@
extern double D_PNAN, D_NNAN, D_PINF, D_NINF;
extern float F_PNAN, F_NNAN, F_PINF, F_NINF;
-_Noreturn void flmain(const char *boot, int bootsz, int argc, char **argv);
+_Noreturn void flmain(const uint8_t *boot, int bootsz, int argc, char **argv);
--- a/main_plan9.c
+++ b/main_plan9.c
@@ -1,6 +1,6 @@
#include "llt.h"
-static char boot[] =
+static uint8_t boot[] =
#include "flisp.boot.h"
;
--- a/main_posix.c
+++ b/main_posix.c
@@ -1,7 +1,7 @@
#include "llt.h"
#include "ieee754.h"
-static const char boot[] =
+static const uint8_t boot[] =
#include "flisp.boot.h"
;
--- a/sixel.c
+++ b/sixel.c
@@ -176,7 +176,7 @@
f->io = toiostream(args[1]);
uint8_t *pix;
size_t sz;
- to_sized_ptr(args[2], (char**)&pix, &sz);
+ to_sized_ptr(args[2], &pix, &sz);
size_t w = toulong(args[3]);
size_t h = toulong(args[4]);
if(w <= 0 || w > sz)