summaryrefslogtreecommitdiff
path: root/tiparse.myr
blob: 71e3f719c4fea6bfc235c497b8adcbfcf6770f8f (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use std
use bio
use fileutil

use "types"

pkg termdraw =
	const tiparse	: (term : byte[:] -> std.result(terminfo, err))
	const tigetbool	: (ti : terminfo#, prop : int -> std.option(bool))
	const tigetnum	: (ti : terminfo#, prop : int -> std.option(int16))
	const tigetstr	: (ti : terminfo#, prop : int -> std.option(byte[:]))
;;

const tiparse = {term -> std.result(terminfo, err)
	var ti : terminfo

	match tiopen(term)
	| `std.Err e:	-> `std.Err e
	| `std.Ok f:
		match tiparsef(f, &ti)
		| `std.Err e:	-> `std.Err e
		| `std.Ok r:	-> `std.Ok ti
		;;
	;;
}

const tigetbool = {ti, b
	if b >= 0 && b < ti.bool.len
		-> `std.Some ti.bool[b]
	else
		-> `std.None
	;;
}

const tigetnum = {ti, n
	if n >= 0 && n < ti.num.len
		-> `std.Some ti.num[n]
	else
		-> `std.None
	;;
}

const tigetstr = {ti, s
	if s >= 0 && s < ti.str.len && ti.str[s].len != 0
		-> `std.Some ti.str[s]
	else
		-> `std.None
	;;
}

const tiparsef = {f, ti
	var nnam, nbool, nnum, nstr, ntab

	match bio.getle16(f)
	| `std.Ok 0x11a:	/* normal tab */
	| `std.Ok 0x162:	/* extended tab */
	| `std.Ok badmagic:	-> `std.Err `Ecorrupt
	| `std.Err _:	-> `std.Err `Ecorrupt
	;;

	match bio.getle16(f)
	| `std.Ok nn:	nnam = nn
	| `std.Err _:	-> `std.Err `Ecorrupt
	;;

	match bio.getle16(f)
	| `std.Ok nb:	nbool = nb
	| `std.Err _:	-> `std.Err `Ecorrupt
	;;

	match bio.getle16(f)
	| `std.Ok nn:	nnum = nn
	| `std.Err _:	-> `std.Err `Ecorrupt
	;;

	match bio.getle16(f)
	| `std.Ok ns:	nstr = ns
	| `std.Err _:	-> `std.Err `Ecorrupt
	;;

	match bio.getle16(f)
	| `std.Ok nt:	ntab = nt
	| `std.Err _:	-> `std.Err `Ecorrupt
	;;

	ti# = [
		.name=std.slalloc(nnam),
		.bool=std.slalloc(nbool),
		.num=std.slalloc(nnum),
		.str=std.slalloc(nstr),
	]

	tiname(ti, f, nnam)
	tibools(ti, f, nbool)
	if (nnam + nbool & 0x1) != 0
		match bio.getc(f)
		| `std.Ok _:	/* padding */
		| _:		goto err
		;;
	;;
	tinums(ti, f, nnum)
	tistrs(ti, f, nstr, ntab)

	-> `std.Ok ""

:err
	tifree(ti)
	-> `std.Err `Ecorrupt
}

const tiname = {ti, f, nnam
	match bio.read(f, ti.name)
	| `std.Ok n:
		if n.len != nnam
			-> `std.Err `Ecorrupt
		;;
	| `std.Err _:	-> `std.Err `Eio
	;;
	-> `std.Ok ti
}

const tibools = {ti, f, nbool
	for var i = 0; i < nbool; i++
		match bio.getb(f)
		| `std.Ok 0:	ti.bool[i] = false
		| `std.Ok _:	ti.bool[i] = true
		| `std.Err _:	-> `std.Err `Eio
		;;
	;;
	-> `std.Ok void
}

const tinums = {ti, f, nnum
	for var i = 0; i < nnum; i++
		match bio.getle16(f)
		| `std.Ok n:	ti.num[i] = n
		| `std.Err _:	-> `std.Err `Eio
		;;
	;;
	-> `std.Ok void
}

const tistrs = {ti, f, nstr, ntab
	var s, i, str, tab

	str = [][:]
	for var i = 0; i < nstr; i++
		match bio.getle16(f)
		| `std.Ok sid:	std.slpush(&str, sid)
		| `std.Err _:	-> `std.Err `Eio
		;;
	;;

	tab = std.slalloc(ntab)
	match bio.read(f, tab)
	| `std.Ok t:
		if t.len != ntab
			-> `std.Err `Ecorrupt
		;;
	| `std.Err _:	-> `std.Err `Eio
	;;

	i = 0
	for off : str
		if off == 0xffff
			ti.str[i++] = ""
		elif  off >= tab.len
			i++
		else
			s = std.cstrconv(tab[off:])
			ti.str[i++] = std.sldup(s)
		;;
	;;
	std.slfree(str)
	-> `std.Ok void
}

const srchpath = [
	"/etc/terminfo/{}/{}",
	"/lib/terminfo/{}/{}", 
	"/usr/share/terminfo/{}/{}",
	"/etc/terminfo/{r}/{}",
	"/lib/terminfo/{r}/{}", 
	"/usr/share/terminfo/{r}/{}"
][:]

const tiopen = {term
	var h, p, pfx, pbuf : byte[256]

	pfx = ""
	if term.len > 0
		pfx = term[:1]
	;;

	match std.getenv("TERMINFO")
	| `std.None:	/* next */
	| `std.Some dir:
		p = std.bfmt(pbuf[:], "{}/{}/{}", dir, pfx, term)
		match bio.open(p, bio.Rd)
		| `std.Ok f:	-> `std.Ok f
		| `std.Err _:	-> `std.Err `Enoterm
		;;
	;;

	match std.getenv("TERMINFO_DIRS")
	| `std.None:	/* next */
	| `std.Some dirs:
		for dir : std.bysplit(dirs, "/")
			if dir.len == 0
				dir = "/etc/terminfo"
			;;
			p = std.bfmt(pbuf[:], "{}/{}/{}", dir, pfx, term)
			match bio.open(p, bio.Rd)
			| `std.Ok f:	-> `std.Ok f
			| `std.Err _:	/* next */
			;;
		;;
	;;

	h = fileutil.homedir()
	p = std.bfmt(pbuf[:], "{}/.terminfo/{}/{}", h, pfx, term)
	match bio.open(p, bio.Rd)
	| `std.Ok f:	-> `std.Ok f
	| `std.Err _:	/* next */
	;;

	for dir : srchpath[:]
		p = std.bfmt(pbuf[:], dir, pfx, term)
		match bio.open(p, bio.Rd)
		| `std.Ok f:	-> `std.Ok f
		| `std.Err _:	/* next */
		;;
	;;

	-> `std.Err `Enoterm
}

const tifree = {ti
	std.slfree(ti.name)
	std.slfree(ti.bool)
	std.slfree(ti.num)
	std.slfree(ti.str)
	std.free(ti)
}