aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/9term/util.c
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2005-11-06 22:16:48 +0000
committerrsc <devnull@localhost>2005-11-06 22:16:48 +0000
commite830a908498c8f0270948fd08c50f6d773315880 (patch)
treea16fe62419a4e3a95ab2d8d3a70f1a870d0bb55c /src/cmd/9term/util.c
parenta6c0ff35ee294c0a808e1792eb4f43820fed8f16 (diff)
downloadplan9port-e830a908498c8f0270948fd08c50f6d773315880.tar.gz
plan9port-e830a908498c8f0270948fd08c50f6d773315880.tar.bz2
plan9port-e830a908498c8f0270948fd08c50f6d773315880.zip
New 9term using rio sources more directly.
Diffstat (limited to 'src/cmd/9term/util.c')
-rw-r--r--src/cmd/9term/util.c149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/cmd/9term/util.c b/src/cmd/9term/util.c
new file mode 100644
index 00000000..8a35d1d7
--- /dev/null
+++ b/src/cmd/9term/util.c
@@ -0,0 +1,149 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <cursor.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <frame.h>
+#include <fcall.h>
+#include "dat.h"
+#include "fns.h"
+
+void
+cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
+{
+ uchar *q;
+ Rune *s;
+ int j, w;
+
+ /*
+ * Always guaranteed that n bytes may be interpreted
+ * without worrying about partial runes. This may mean
+ * reading up to UTFmax-1 more bytes than n; the caller
+ * knows this. If n is a firm limit, the caller should
+ * set p[n] = 0.
+ */
+ q = (uchar*)p;
+ s = r;
+ for(j=0; j<n; j+=w){
+ if(*q < Runeself){
+ w = 1;
+ *s = *q++;
+ }else{
+ w = chartorune(s, (char*)q);
+ q += w;
+ }
+ if(*s)
+ s++;
+ else if(nulls)
+ *nulls = TRUE;
+ }
+ *nb = (char*)q-p;
+ *nr = s-r;
+}
+
+void
+error(char *s)
+{
+ fprint(2, "rio: %s: %r\n", s);
+ if(errorshouldabort)
+ abort();
+ threadexitsall("error");
+}
+
+void*
+erealloc(void *p, uint n)
+{
+ p = realloc(p, n);
+ if(p == nil)
+ error("realloc failed");
+ return p;
+}
+
+void*
+emalloc(uint n)
+{
+ void *p;
+
+ p = malloc(n);
+ if(p == nil)
+ error("malloc failed");
+ memset(p, 0, n);
+ return p;
+}
+
+char*
+estrdup(char *s)
+{
+ char *p;
+
+ p = malloc(strlen(s)+1);
+ if(p == nil)
+ error("strdup failed");
+ strcpy(p, s);
+ return p;
+}
+
+int
+isalnum(Rune c)
+{
+ /*
+ * Hard to get absolutely right. Use what we know about ASCII
+ * and assume anything above the Latin control characters is
+ * potentially an alphanumeric.
+ */
+ if(c <= ' ')
+ return FALSE;
+ if(0x7F<=c && c<=0xA0)
+ return FALSE;
+ if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
+ return FALSE;
+ return TRUE;
+}
+
+Rune*
+strrune(Rune *s, Rune c)
+{
+ Rune c1;
+
+ if(c == 0) {
+ while(*s++)
+ ;
+ return s-1;
+ }
+
+ while(c1 = *s++)
+ if(c1 == c)
+ return s-1;
+ return nil;
+}
+
+int
+min(int a, int b)
+{
+ if(a < b)
+ return a;
+ return b;
+}
+
+int
+max(int a, int b)
+{
+ if(a > b)
+ return a;
+ return b;
+}
+
+char*
+runetobyte(Rune *r, int n, int *ip)
+{
+ char *s;
+ int m;
+
+ s = emalloc(n*UTFmax+1);
+ m = snprint(s, n*UTFmax+1, "%.*S", n, r);
+ *ip = m;
+ return s;
+}
+