tttm

git clone https://orangeshoelaces.net/git/tttm.git

34a60db2e7d199bda762d286b816a54ed5476d6f

Author: Vasily Kolobkov on 05/29/2016

Committer: Vasily Kolobkov on 05/29/2016

Make all symbol buffers page long

Stats

errors.c  |  1 +
errors.h  |  3 +-
imap.c    | 22 ++++-
imap.h    |  6 +-
laxsrc.c  | 37 ++++++--
laxsrc.h  |  6 +-
pshades.c |  3 +-
tttm.c    | 29 ++++--
8 files changed, 78 insertions(+), 29 deletions(-)

Patch

diff --git a/errors.c b/errors.c
index 8e0064a..35b9c59 100644
--- a/errors.c
+++ b/errors.c
@@ -16,6 +16,7 @@ const char *errmsgs[] = {
 	[TE_NIL]        = "got NIL message",
 	[TE_CMDFMT]     = "command draws unsupported conversion specifier",
 	[TE_XLCMD]      = "command is too long",
+	[TE_NOMEM]      = "not enough memory",
 	[TE_IN]         = "input error",
 	[TE_OUT]        = "output error",
 	[TE_VM]         = "cache vm mapping",
diff --git a/errors.h b/errors.h
index 914aa31..3448016 100644
--- a/errors.h
+++ b/errors.h
@@ -20,7 +20,8 @@ enum {
 	TE_XLCMD,
 
 	TE_ERRNO,
-	TE_IN = TE_ERRNO,
+	TE_NOMEM = TE_ERRNO,
+	TE_IN,
 	TE_OUT,
 	TE_VM,
 	TE_CACHEIO,
diff --git a/imap.c b/imap.c
index 5fa99d7..8b779db 100644
--- a/imap.c
+++ b/imap.c
@@ -4,6 +4,7 @@
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <strings.h>
 #include <unistd.h>
@@ -91,13 +92,16 @@ static struct respcard fetresp[] = { { IP_FETCH, dig_fetch }, { -1 } };
 static struct respcard byeresp[] = { { IP_RESPBYE, dig_bye, 1 }, { -1 } };
 
 int
-imap_init(struct imapctx *con, int in, int out)
+imap_init(struct imapctx *con, int in, int out, size_t buflen)
 {
 	int e;
 
 	bzero(con, sizeof(*con));
+	if (!(con->rep = malloc(buflen)))
+		goto emem;
+	con->replen = buflen;
 	con->state = IS_GREET;
-	laxsrc_init(&con->in, in);
+	laxsrc_init(&con->in, in, buflen);
 	con->out = out;
 	if ((e = imap_recvgreets(con)))
 		goto exit;
@@ -105,6 +109,16 @@ imap_init(struct imapctx *con, int in, int out)
 		e = imap_discocap(con);
  exit:
 	return e;
+ emem:
+	e = TE_NOMEM;
+	goto exit;
+}
+
+void
+imap_free(struct imapctx *con)
+{
+	laxsrc_free(&con->in);
+	free(con->rep);
 }
 
 int
@@ -343,14 +357,14 @@ imap_parcaps(struct imapctx *con, union parnode *n)
 int
 imap_readln(struct imapctx *con)
 {
-	return par_readln(&con->in, con->rep, LEN(con->rep),
+	return par_readln(&con->in, con->rep, con->replen,
 	    -1, 0, con->par, LEN(con->par));
 }
 
 int
 imap_readlnc(struct imapctx *con, int cache, int *buf)
 {
-	return par_readln(&con->in, con->rep, LEN(con->rep),
+	return par_readln(&con->in, con->rep, con->replen,
 	    cache, buf, con->par, LEN(con->par));
 }
 
diff --git a/imap.h b/imap.h
index d9565f0..6c465d9 100644
--- a/imap.h
+++ b/imap.h
@@ -8,7 +8,8 @@ struct imapctx {
 	int           out;
 
 	unsigned int  tag;
-	char          rep[1024];
+	char         *rep;
+	size_t        replen;
 	union parnode par[1024];
 
 	int           caps;
@@ -22,7 +23,8 @@ struct msgd {
 	size_t len;
 };
 
-int	imap_init(struct imapctx *, int, int);
+int	imap_init(struct imapctx *, int, int, size_t);
+void	imap_free(struct imapctx *);
 int	imap_discocap(struct imapctx *);
 int	imap_login(struct imapctx *, const char *, const char *);
 int	imap_select(struct imapctx *, const char *);
diff --git a/laxsrc.c b/laxsrc.c
index d6f57cb..d3fd407 100644
--- a/laxsrc.c
+++ b/laxsrc.c
@@ -1,6 +1,7 @@
 #include <errno.h>
 #include <poll.h>
 #include <stddef.h>
+#include <stdlib.h>
 #include <string.h>
 #include <strings.h>
 #include <unistd.h>
@@ -8,16 +9,32 @@
 #include "errors.h"
 #include "laxsrc.h"
 
-#define LEN(x) (sizeof(x) / sizeof((x)[0]))
 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
 
 static int timeout = 10000;
 
-void
-laxsrc_init(struct laxsrc *s, int fd)
+int
+laxsrc_init(struct laxsrc *s, int fd, size_t buflen)
 {
+	int e;
+
+	e = 0;
 	bzero(s, sizeof(*s));
 	s->fd = fd;
+	if (!(s->buf = malloc(buflen)))
+		goto emem;
+	s->buflen = buflen;
+ exit:
+	return e;
+ emem:
+	e = TE_NOMEM;
+	goto exit;
+}
+
+void
+laxsrc_free(struct laxsrc *s)
+{
+	free(s->buf);
 }
 
 int
@@ -27,7 +44,7 @@ laxsrc_ishot(struct laxsrc *s)
 	struct pollfd pd;
 
 	e = 0;
-	if (s->buflen > 0)
+	if (s->bufill > 0)
 		goto exit;
 	pd.fd = s->fd;
 	pd.events = POLLIN | POLLHUP;
@@ -54,11 +71,11 @@ laxsrc_read(struct laxsrc *s, char *dst, size_t lo, size_t hi, int *e)
 	pd.fd = s->fd;
 	pd.events = POLLIN | POLLHUP;
 
-	if (s->buflen > 0) {
-		boff = LEN(s->buf) - s->buflen;
+	if (s->bufill > 0) {
+		boff = s->buflen - s->bufill;
 		len = MIN(s->buflen, hi);
 		memcpy(dst, s->buf + boff, len);
-		s->buflen -= len;
+		s->bufill -= len;
 	}
 
 	if (s->eof) goto eof;
@@ -109,10 +126,10 @@ laxsrc_putback(struct laxsrc *s, char *chunk, size_t len)
 	size_t boff;
 
 	e = TE_OK;
-	if (s->buflen + len > LEN(s->buf))
+	if (s->bufill + len > s->buflen)
 		goto eoflow;
-	s->buflen += len;
-	boff = LEN(s->buf) - s->buflen;
+	s->bufill += len;
+	boff = s->buflen - s->bufill;
 	memcpy(s->buf + boff, chunk, len);
  exit:
 	return e;
diff --git a/laxsrc.h b/laxsrc.h
index 51c829d..04f524b 100644
--- a/laxsrc.h
+++ b/laxsrc.h
@@ -1,11 +1,13 @@
 struct laxsrc {
 	int    fd;
 	int    eof;
-	char   buf[1024];
+	char  *buf;
 	size_t buflen;
+	size_t bufill;
 };
 
-void	laxsrc_init(struct laxsrc *, int);
+int	laxsrc_init(struct laxsrc *, int, size_t);
+void	laxsrc_free(struct laxsrc *);
 int	laxsrc_ishot(struct laxsrc *);
 int	laxsrc_read(struct laxsrc *, char *, size_t, size_t, int *);
 int	laxsrc_putback(struct laxsrc *, char *, size_t);
diff --git a/pshades.c b/pshades.c
index c821eb9..0409f3c 100644
--- a/pshades.c
+++ b/pshades.c
@@ -54,11 +54,12 @@ main(int argc, char **argv)
 	char ln[1024];
 	union parnode pt[1024];
 
-	laxsrc_init(&in, STDIN_FILENO);
+	laxsrc_init(&in, STDIN_FILENO, LEN(ln));
 	if ((e = par_readln(&in, ln, LEN(ln), -1, 0, pt, LEN(pt))))
 		errx(1, "error parsing input: %s",  errmsgs[e]);
 
 	ptprint(pt, ln, 0);
+	laxsrc_free(&in);
 	return 0;
 }
 
diff --git a/tttm.c b/tttm.c
index 41677ea..1c42bd0 100644
--- a/tttm.c
+++ b/tttm.c
@@ -31,6 +31,7 @@ static int	sink_init(const char *, pid_t *, int *);
 static int	sink_free(pid_t, int);
 static void	fin(struct imapctx *, int, const char *);
 static void	terr(int, const char *);
+static void	twarn(int, const char *);
 
 extern char **environ;
 
@@ -51,7 +52,7 @@ main(int argc, char **argv)
 		err(1, "failed to find out page size");
 	page = sv;
 
-	if ((e = imap_init(&con, STDIN_FILENO, STDOUT_FILENO))) {
+	if ((e = imap_init(&con, STDIN_FILENO, STDOUT_FILENO, page))) {
 		terr(e, "failed to initialize session");
 	}
 	if (con.state == IS_AUTH) {
@@ -123,6 +124,7 @@ main(int argc, char **argv)
 	if ((e = imap_logout(&con))) {
 		fin(&con, e, "failed to log out");
 	}
+	imap_free(&con);
 	return 0;
 }
 
@@ -264,23 +266,32 @@ sink_free(pid_t sid, int in)
 void
 fin(struct imapctx *con, int e, const char *msg)
 {
+	int loerr;
+
 	if (e == TE_NO || e == TE_BAD || e == TE_NIL) {
-		warnx("%s: %s", msg, errmsgs[e]);
-		if ((e = imap_logout(con))) {
-			terr(e, "failed to log out");
-		}
-	} else {
-		terr(e, msg);
+		if ((loerr = imap_logout(con)))
+			twarn(loerr, "failed to log out");
 	}
-	exit(1);
+	imap_free(con);
+	terr(e, msg);
 }
 
 void
 terr(int e, const char *msg)
 {
-	if (e > TE_ERRNO) {
+	if (e >= TE_ERRNO) {
 		err(1, "%s: %s", msg, errmsgs[e]);
 	} else {
 		errx(1, "%s: %s", msg, errmsgs[e]);
 	}
 }
+
+void
+twarn(int e, const char *msg)
+{
+	if (e >= TE_ERRNO) {
+		warn("%s: %s", msg, errmsgs[e]);
+	} else {
+		warnx("%s: %s", msg, errmsgs[e]);
+	}
+}