]> git.lizzy.rs Git - plan9front.git/blob - sys/src/games/mix/util.c
devcons: fix permissions for reboot and sysstat
[plan9front.git] / sys / src / games / mix / util.c
1 #include <u.h>
2 #include <libc.h>
3 #include <ctype.h>
4 #include <avl.h>
5 #include <bio.h>
6 #include "mix.h"
7
8 static char buf[1024];
9
10 char*
11 strskip(char *s) {
12         while(isspace(*s))
13                 s++;
14         return s;
15 }
16
17 char*
18 strim(char *s)
19 {
20         char *t;
21
22         if(*s == '\0')
23                 return s;
24
25         t = s + strlen(s) - 1;
26         while(isspace(*t) && t > s)
27                 t--;
28         t[1] = '\0';
29         return s;
30 }
31
32 void
33 yyerror(char *s, ...)
34 {
35         char *bp;
36         va_list a;
37
38         bp = seprint(buf, buf+1024, "Assembly error: %s:%d: ", filename, line);
39         va_start(a, s);
40         bp = vseprint(bp, buf+1024, s, a);
41         va_end(a);
42         *bp++ = '\n';
43         write(2, buf, bp - buf);
44         longjmp(errjmp, 1);
45 }
46
47 void
48 vmerror(char *s, ...)
49 {
50         char *bp;
51         va_list a;
52
53         bp = seprint(buf, buf+1024, "VM error at %d: ", curpc);
54         va_start(a, s);
55         bp = vseprint(bp, buf+1024, s, a);
56         va_end(a);
57         *bp++ = '\n';
58         write(2, buf, bp - buf);
59         longjmp(errjmp, 1);
60 }
61
62 void
63 error(char *s, ...)
64 {
65         char *bp;
66         va_list a;
67
68         va_start(a, s);
69         bp = vseprint(buf, buf+1024, s, a);
70         va_end(a);
71         *bp++ = '\n';
72         write(2, buf, bp - buf);
73         exits("error");
74 }
75
76 void*
77 emalloc(ulong s)
78 {
79         void *v;
80
81         v = malloc(s);
82         if(v == nil)
83                 error("Error allocating %lud: %r\n", s);
84         setmalloctag(v, getcallerpc(&s));
85         return v;
86 }
87
88 void*
89 emallocz(ulong s)
90 {
91         void *v;
92
93         v = malloc(s);
94         if(v == nil)
95                 error("Error allocating %lud: %r", s);
96         memset(v, 0, s);
97         return v;
98 }
99
100 void*
101 erealloc(void *p, ulong s)
102 {
103         void *v;
104
105         v = realloc(p, s);
106         if(v == nil)
107                 error("Error re-allocating %lud: %r", s);
108         setrealloctag(v, getcallerpc(&s));
109         return v;
110 }
111
112 char*
113 estrdup(char *s)
114 {
115         char *n;
116
117         n = strdup(s);
118         if(n == nil)
119                 error("Error duplicating string %s: %r", s);
120         setmalloctag(n, getcallerpc(&s));
121         return n;
122 }
123
124 void*
125 bsearch(void *k, void *a, long n, int w, int (*cmp)(void*, void*))
126 {
127         void *e;
128         int c;
129
130         while(n > 0) {
131                 e = (char*)a + w*(n/2);
132                 c = cmp(k, e);
133                 if(c == 0)
134                         return e;
135
136                 if(n == 1)
137                         break;
138
139                 if(c < 0)
140                         n /= 2;
141                 else {
142                         a = e;
143                         n -= n/2;
144                 }
145         }
146         return nil;
147 }