aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/htmlroff/util.c
blob: 99e99543f16d229d322dfe99f2f7d194769ec68a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "a.h"

void*
emalloc(uint n)
{
	void *v;
	
	v = mallocz(n, 1);
	if(v == nil)
		sysfatal("out of memory");
	return v;
}

char*
estrdup(char *s)
{
	char *t;
	
	t = strdup(s);
	if(t == nil)
		sysfatal("out of memory");
	return t;
}

Rune*
erunestrdup(Rune *s)
{
	Rune *t;

	t = emalloc(sizeof(Rune)*(runestrlen(s)+1));
	if(t == nil)
		sysfatal("out of memory");
	runestrcpy(t, s);
	return t;
}

void*
erealloc(void *ov, uint n)
{
	void *v;
	
	v = realloc(ov, n);
	if(v == nil)
		sysfatal("out of memory");
	return v;
}

Rune*
erunesmprint(char *fmt, ...)
{
	Rune *s;
	va_list arg;
	
	va_start(arg, fmt);
	s = runevsmprint(fmt, arg);
	va_end(arg);
	if(s == nil)
		sysfatal("out of memory");
	return s;
}

char*
esmprint(char *fmt, ...)
{
	char *s;
	va_list arg;
	
	va_start(arg, fmt);
	s = vsmprint(fmt, arg);
	va_end(arg);
	if(s == nil)
		sysfatal("out of memory");
	return s;
}

void
warn(char *fmt, ...)
{
	va_list arg;
	
	fprint(2, "htmlroff: %L: ");
	va_start(arg, fmt);
	vfprint(2, fmt, arg);
	va_end(arg);
	fprint(2, "\n");
}

/*
 * For non-Unicode compilers, so we can say
 * L("asdf") and get a Rune string.  Assumes strings
 * are identified by their pointers, so no mutable strings!
 */
typedef struct Lhash Lhash;
struct Lhash
{
	char *s;
	Lhash *next;
	Rune r[1];
};
static Lhash *hash[1127];

Rune*
L(char *s)
{
	Rune *p;
	Lhash *l;
	uint h;

	h = (uintptr)s%nelem(hash);
	for(l=hash[h]; l; l=l->next)
		if(l->s == s)
			return l->r;
	l = emalloc(sizeof *l+(utflen(s)+1)*sizeof(Rune));
	p = l->r;
	l->s = s;
	while(*s)
		s += chartorune(p++, s);
	*p = 0;
	l->next = hash[h];
	hash[h] = l;
	return l->r;
}