Commit 2fc04768 authored by Mart Lubbers's avatar Mart Lubbers
Browse files

reduce timeout, malloc clean strings

parent 72164e16
test: test.icl TTY.icl TTY.dcl Clean\ System\ Files/tty.o test: test.icl TTY.icl TTY.dcl Clean\ System\ Files/tty.o
clm -desc -exl -tst -ns -nt $(basename $<) -o $@ clm -desc -exl -tst -ns -nt -dynamics -I /opt/clean/lib/Dynamics $(basename $<) -o $@
Clean\ System\ Files/tty.o: tty.c Clean\ System\ Files/tty.o: tty.c
gcc -g -c $< -o "$@" gcc -g -DDEBUG -c $< -o "$@"
clean: clean:
$(RM) -r Clean\ System\ Files/* test $(RM) -r Clean\ System\ Files/* test
...@@ -67,7 +67,7 @@ TTYavailable _ = code { ...@@ -67,7 +67,7 @@ TTYavailable _ = code {
TTYerror :: !*env -> (!String, !*env) TTYerror :: !*env -> (!String, !*env)
TTYerror _ = code { TTYerror _ = code {
ccall ttyerror ":S:A" ccall ttyerror ":VS:A"
} }
Start =0 Start =0
...@@ -10,8 +10,15 @@ ...@@ -10,8 +10,15 @@
#include "Clean.h" #include "Clean.h"
#ifdef DEBUG
#define debug(s) {puts(s); fflush(stdout);}
#else
#define debug(s) ;
#endif
#define INITIAL_BUFFERSIZE 2 #define INITIAL_BUFFERSIZE 2
#define DEVICE_TIMEOUT {5, 0} #define DEVICE_TIMEOUT {0, 0}
#define die(s) {perror(s);exit(EXIT_FAILURE);} #define die(s) {perror(s);exit(EXIT_FAILURE);}
static speed_t baudrates[] = {B0, B50, B75, B110, B134, B150, B200, B300, B600, static speed_t baudrates[] = {B0, B50, B75, B110, B134, B150, B200, B300, B600,
...@@ -29,6 +36,14 @@ struct termioslist ...@@ -29,6 +36,14 @@ struct termioslist
struct termioslist *head = NULL; struct termioslist *head = NULL;
static void *my_malloc(size_t s)
{
void *r = malloc(s);
if(r == NULL)
die("malloc");
return r;
}
static struct termios *getTermios(int fd) static struct termios *getTermios(int fd)
{ {
struct termioslist *h = head; struct termioslist *h = head;
...@@ -59,9 +74,7 @@ static void remTermios(int fd) ...@@ -59,9 +74,7 @@ static void remTermios(int fd)
static char *cleanStringToCString(CleanString s) static char *cleanStringToCString(CleanString s)
{ {
unsigned long len = CleanStringLength(s); unsigned long len = CleanStringLength(s);
char *cs = (char *)malloc(len+1); char *cs = (char *)my_malloc(len+1);
if(cs == NULL)
die("malloc");
memcpy(cs, CleanStringCharacters(s), len); memcpy(cs, CleanStringCharacters(s), len);
cs[len] = '\0'; cs[len] = '\0';
return cs; return cs;
...@@ -69,9 +82,7 @@ static char *cleanStringToCString(CleanString s) ...@@ -69,9 +82,7 @@ static char *cleanStringToCString(CleanString s)
static void addTermios(int fd, struct termios *t) static void addTermios(int fd, struct termios *t)
{ {
struct termioslist *new = malloc(sizeof(struct termioslist)); struct termioslist *new = my_malloc(sizeof(struct termioslist));
if(new == NULL)
die("malloc");
new->fd = fd; new->fd = fd;
memcpy(&new->to, t, sizeof(struct termios)); memcpy(&new->to, t, sizeof(struct termios));
new->next = NULL; new->next = NULL;
...@@ -88,6 +99,7 @@ static void addTermios(int fd, struct termios *t) ...@@ -88,6 +99,7 @@ static void addTermios(int fd, struct termios *t)
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity, void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
int stopbits, int xonoff, int *status, int *fd) int stopbits, int xonoff, int *status, int *fd)
{ {
debug("ttyopen");
struct termios tio; struct termios tio;
char *cs_fn = cleanStringToCString(fn); char *cs_fn = cleanStringToCString(fn);
*fd = open(cs_fn, O_RDWR | O_NOCTTY); *fd = open(cs_fn, O_RDWR | O_NOCTTY);
...@@ -136,18 +148,27 @@ void ttyopen(CleanString fn, int baudrate, int bytesize, int parity, ...@@ -136,18 +148,27 @@ void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
error = strerror(errno); error = strerror(errno);
} }
free(cs_fn); free(cs_fn);
debug("ttyopen-done");
} }
unsigned long *errcl = NULL;
void ttyerror(CleanString *result) void ttyerror(CleanString *result)
{ {
CleanStringVariable(clean_string, strlen(error)); debug("ttyerror");
*result = (CleanString) clean_string; if(errcl == NULL)
memcpy(CleanStringCharacters(clean_string), error, strlen(error)); free(errcl);
CleanStringLength(clean_string) = strlen(error); errcl = my_malloc(
sizeof(unsigned long)*CleanStringSizeInts(strlen(error)));
*result = (CleanString) errcl;
memcpy(CleanStringCharacters(errcl), error, strlen(error));
CleanStringLength(errcl) = strlen(error);
debug("ttyerror-done");
} }
unsigned long *readlinecl = NULL;
void ttyreadline(int fd, CleanString *result, int *fdo) void ttyreadline(int fd, CleanString *result, int *fdo)
{ {
debug("ttyreadline");
size_t bufsize = INITIAL_BUFFERSIZE; size_t bufsize = INITIAL_BUFFERSIZE;
char *buf = NULL; char *buf = NULL;
ssize_t cr, charsread = 0; ssize_t cr, charsread = 0;
...@@ -160,17 +181,22 @@ void ttyreadline(int fd, CleanString *result, int *fdo) ...@@ -160,17 +181,22 @@ void ttyreadline(int fd, CleanString *result, int *fdo)
} }
buf[charsread] = '\0'; buf[charsread] = '\0';
CleanStringVariable(cleanOutput, charsread); if(readlinecl == NULL)
*result = (CleanString) cleanOutput; free(readlinecl);
memcpy(CleanStringCharacters(cleanOutput), buf, charsread); readlinecl = my_malloc(
CleanStringLength(cleanOutput) = charsread; sizeof(unsigned long)*CleanStringSizeInts(charsread));
*result = (CleanString) readlinecl;
memcpy(CleanStringCharacters(readlinecl), buf, charsread);
CleanStringLength(readlinecl) = charsread;
*fdo = fd; *fdo = fd;
free(buf); free(buf);
debug("ttyreadline-done");
} }
void ttyavailable(int fd, int *r, int *fdo) void ttyavailable(int fd, int *r, int *fdo)
{ {
debug("ttyavailable");
fd_set fds; fd_set fds;
struct timeval tv = DEVICE_TIMEOUT; struct timeval tv = DEVICE_TIMEOUT;
...@@ -181,21 +207,26 @@ void ttyavailable(int fd, int *r, int *fdo) ...@@ -181,21 +207,26 @@ void ttyavailable(int fd, int *r, int *fdo)
if(*r == -1) if(*r == -1)
die("select"); die("select");
*fdo = fd; *fdo = fd;
debug("ttyavailable-done");
} }
int ttywrite(int fd, CleanString s) int ttywrite(int fd, CleanString s)
{ {
debug("ttywrite");
write(fd, CleanStringCharacters(s), CleanStringLength(s)); write(fd, CleanStringCharacters(s), CleanStringLength(s));
tcdrain(fd); tcdrain(fd);
debug("ttywrite-done");
return fd; return fd;
} }
int ttyclose(int fd) int ttyclose(int fd)
{ {
debug("ttyclose");
struct termios *to = getTermios(fd); struct termios *to = getTermios(fd);
tcsetattr(fd, TCSANOW, to); tcsetattr(fd, TCSANOW, to);
remTermios(fd); remTermios(fd);
int ret = close(fd); int ret = close(fd);
error = strerror(errno); error = strerror(errno);
debug("ttyclose-done");
return ret + 1; return ret + 1;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment