]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/htmlroff/util.c
ndb/dns: remove single-ip-address assuptions
[plan9front.git] / sys / src / cmd / htmlroff / util.c
1 #include "a.h"
2
3 void*
4 emalloc(uint n)
5 {
6         void *v;
7         
8         v = mallocz(n, 1);
9         if(v == nil)
10                 sysfatal("out of memory");
11         return v;
12 }
13
14 char*
15 estrdup(char *s)
16 {
17         char *t;
18         
19         t = strdup(s);
20         if(t == nil)
21                 sysfatal("out of memory");
22         return t;
23 }
24
25 Rune*
26 erunestrdup(Rune *s)
27 {
28         Rune *t;
29
30         t = emalloc(sizeof(Rune)*(runestrlen(s)+1));
31         if(t == nil)
32                 sysfatal("out of memory");
33         runestrcpy(t, s);
34         return t;
35 }
36
37 void*
38 erealloc(void *ov, uint n)
39 {
40         void *v;
41         
42         v = realloc(ov, n);
43         if(v == nil)
44                 sysfatal("out of memory");
45         return v;
46 }
47
48 Rune*
49 erunesmprint(char *fmt, ...)
50 {
51         Rune *s;
52         va_list arg;
53         
54         va_start(arg, fmt);
55         s = runevsmprint(fmt, arg);
56         va_end(arg);
57         if(s == nil)
58                 sysfatal("out of memory");
59         return s;
60 }
61
62 char*
63 esmprint(char *fmt, ...)
64 {
65         char *s;
66         va_list arg;
67         
68         va_start(arg, fmt);
69         s = vsmprint(fmt, arg);
70         va_end(arg);
71         if(s == nil)
72                 sysfatal("out of memory");
73         return s;
74 }
75
76 void
77 warn(char *fmt, ...)
78 {
79         va_list arg;
80         
81         fprint(2, "htmlroff: %L: ");
82         va_start(arg, fmt);
83         vfprint(2, fmt, arg);
84         va_end(arg);
85         fprint(2, "\n");
86 }
87
88 /*
89  * For non-Unicode compilers, so we can say
90  * L("asdf") and get a Rune string.  Assumes strings
91  * are identified by their pointers, so no mutable strings!
92  */
93 typedef struct Lhash Lhash;
94 struct Lhash
95 {
96         char *s;
97         Lhash *next;
98         Rune r[1];
99 };
100 static Lhash *hash[1127];
101
102 Rune*
103 L(char *s)
104 {
105         Rune *p;
106         Lhash *l;
107         uint h;
108
109         h = (uintptr)s%nelem(hash);
110         for(l=hash[h]; l; l=l->next)
111                 if(l->s == s)
112                         return l->r;
113         l = emalloc(sizeof *l+(utflen(s)+1)*sizeof(Rune));
114         p = l->r;
115         l->s = s;
116         while(*s)
117                 s += chartorune(p++, s);
118         *p = 0;
119         l->next = hash[h];
120         hash[h] = l;
121         return l->r;
122 }
123