]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/rio/util.c
rio: get rid of all mouse moves, fix cursor handling
[plan9front.git] / sys / src / cmd / rio / util.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include "dat.h"
11 #include "fns.h"
12
13 void
14 cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
15 {
16         uchar *q;
17         Rune *s;
18         int j, w;
19
20         /*
21          * Always guaranteed that n bytes may be interpreted
22          * without worrying about partial runes.  This may mean
23          * reading up to UTFmax-1 more bytes than n; the caller
24          * knows this.  If n is a firm limit, the caller should
25          * set p[n] = 0.
26          */
27         q = (uchar*)p;
28         s = r;
29         for(j=0; j<n; j+=w){
30                 if(*q < Runeself){
31                         w = 1;
32                         *s = *q++;
33                 }else{
34                         w = chartorune(s, (char*)q);
35                         q += w;
36                 }
37                 if(*s)
38                         s++;
39                 else if(nulls)
40                                 *nulls = TRUE;
41         }
42         *nb = (char*)q-p;
43         *nr = s-r;
44 }
45
46 void
47 error(char *s)
48 {
49         fprint(2, "rio: %s: %r\n", s);
50         if(errorshouldabort)
51                 abort();
52         threadexitsall("error");
53 }
54
55 void*
56 erealloc(void *p, uint n)
57 {
58         p = realloc(p, n);
59         if(p == nil)
60                 error("realloc failed");
61         setrealloctag(p, getcallerpc(&p));
62         return p;
63 }
64
65 void*
66 emalloc(uint n)
67 {
68         void *p;
69
70         p = malloc(n);
71         if(p == nil)
72                 error("malloc failed");
73         setmalloctag(p, getcallerpc(&n));
74         memset(p, 0, n);
75         return p;
76 }
77
78 char*
79 estrdup(char *s)
80 {
81         char *p;
82
83         p = malloc(strlen(s)+1);
84         if(p == nil)
85                 error("strdup failed");
86         setmalloctag(p, getcallerpc(&s));
87         strcpy(p, s);
88         return p;
89 }
90
91 int
92 isalnum(Rune c)
93 {
94         /*
95          * Hard to get absolutely right.  Use what we know about ASCII
96          * and assume anything above the Latin control characters is
97          * potentially an alphanumeric.
98          */
99         if(c <= ' ')
100                 return FALSE;
101         if(0x7F<=c && c<=0xA0)
102                 return FALSE;
103         if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
104                 return FALSE;
105         return TRUE;
106 }
107
108 Rune*
109 strrune(Rune *s, Rune c)
110 {
111         Rune c1;
112
113         if(c == 0) {
114                 while(*s++)
115                         ;
116                 return s-1;
117         }
118
119         while(c1 = *s++)
120                 if(c1 == c)
121                         return s-1;
122         return nil;
123 }
124
125 int
126 min(int a, int b)
127 {
128         if(a < b)
129                 return a;
130         return b;
131 }
132
133 int
134 max(int a, int b)
135 {
136         if(a > b)
137                 return a;
138         return b;
139 }
140
141 char*
142 runetobyte(Rune *r, int n, int *ip)
143 {
144         char *s;
145         int m;
146
147         s = emalloc(n*UTFmax+1);
148         m = snprint(s, n*UTFmax+1, "%.*S", n, r);
149         *ip = m;
150         return s;
151 }
152