Blob


1 #include "a.h"
3 void*
4 emalloc(uint n)
5 {
6 void *v;
8 v = mallocz(n, 1);
9 if(v == nil)
10 sysfatal("out of memory");
11 return v;
12 }
14 char*
15 estrdup(char *s)
16 {
17 char *t;
19 t = strdup(s);
20 if(t == nil)
21 sysfatal("out of memory");
22 return t;
23 }
25 Rune*
26 erunestrdup(Rune *s)
27 {
28 Rune *t;
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 }
37 void*
38 erealloc(void *ov, uint n)
39 {
40 void *v;
42 v = realloc(ov, n);
43 if(v == nil)
44 sysfatal("out of memory");
45 return v;
46 }
48 Rune*
49 erunesmprint(char *fmt, ...)
50 {
51 Rune *s;
52 va_list arg;
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 }
62 char*
63 esmprint(char *fmt, ...)
64 {
65 char *s;
66 va_list arg;
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 }
76 void
77 warn(char *fmt, ...)
78 {
79 va_list arg;
81 fprint(2, "htmlroff: %L: ");
82 va_start(arg, fmt);
83 vfprint(2, fmt, arg);
84 va_end(arg);
85 fprint(2, "\n");
86 }
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];
102 Rune*
103 L(char *s)
105 Rune *p;
106 Lhash *l;
107 uint h;
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;