summaryrefslogtreecommitdiff
path: root/appl/cmd/lookman.b
blob: 53557c8b9861a0af5864d6da8b9ea88ca48c9b36 (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
245
246
247
248
249
250
implement Lookman;
include "sys.m";
include "bufio.m";
include "draw.m";


Lookman : module {
	init : fn (ctxt : ref Draw->Context, argv : list of string);
};

sys : Sys;
bufio : Bufio;
Iobuf : import bufio;

ctype := array [256] of { * => byte 0 };

MANINDEX : con "/man/index";

init(nil : ref Draw->Context, argv : list of string)
{
	sys = load Sys Sys->PATH;
	bufio = load Bufio Bufio->PATH;

	if (bufio == nil)
		raise "init:fail";

	# setup our char conversion table
	# map upper-case to lower-case
	for (i := 'A'; i <= 'Z'; i++)
		ctype[i] = byte ((i - 'A') + 'a');

	# only allow the following chars
	okchars := "abcdefghijklmnopqrstuvwxyz0123456789+.:½ ";
	for (i = 0; i < len okchars; i++) {
		ch := okchars[i];
		ctype[ch] = byte ch;
	}

	stdout := bufio->fopen(sys->fildes(1), Sys->OWRITE);

	argv = tl argv;
	paths := lookup(argv);
	for (; paths != nil; paths = tl paths)
		stdout.puts(sys->sprint("%s\n", hd paths));
	stdout.flush();
}

lookup(words : list of string) : list of string
{
	# open the index file
	manindex := bufio->open(MANINDEX, Sys->OREAD);
	if (manindex == nil) {
		sys->print("cannot open %s: %r\n", MANINDEX);
		return nil;
	}

	# convert to lower-case and discard funny chars
	keywords : list of string;
	for (; words != nil; words = tl words) {
		word := hd words;
		kw := "";
		for (i := 0; i < len word; i++) {
			ch := word[i];
			if (ch < len ctype && ctype[ch] != byte 0)
				kw[len kw] = int ctype[ch];
		}
		if (kw != "")
			keywords = kw :: keywords;
	}

	if (keywords == nil)
		return nil;

	keywords = sortuniq(keywords);
	matches : list of list of string;

	for (; keywords != nil; keywords = tl keywords) {
		kw := hd keywords;
		matchlist := look(manindex, '\t', kw);
		pathlist : list of string = nil;
		for (; matchlist != nil; matchlist = tl matchlist) {
			line := hd matchlist;
			(n, toks) := sys->tokenize(line, "\t");
			if (n != 2)
				continue;
			pathlist = hd tl toks :: pathlist;
		}
		if (pathlist != nil)
			matches = pathlist :: matches;
	}

	return intersect(matches);
}

getentry(iob : ref Iobuf) : (string, string)
{
	while ((s := iob.gets('\n')) != nil) {
		if (s[len s -1] == '\n')
			s = s[0:len s -1];
		if (s == nil)
			continue;
		(n, toks) := sys->tokenize(s, "\t");
		if (n != 2)
			continue;
		return (hd toks, hd tl toks);
	}
	return (nil, nil);
}

sortuniq(strlist : list of string) : list of string
{
	strs := array [len strlist] of string;
	for (i := 0; strlist != nil; (i, strlist) = (i+1, tl strlist))
		strs[i] = hd strlist;

	# simple sort (greatest first)
	for (i = 0; i < len strs - 1; i++) {
		for (j := i+1; j < len strs; j++)
			if (strs[i] < strs[j])
				(strs[i], strs[j]) = (strs[j], strs[i]);
	}

	# construct list (result is ascending)
	r : list of string;
	prev := "";
	for (i = 0; i < len strs; i++) {
		if (strs[i] != prev) {
			r = strs[i] :: r;
			prev = strs[i];
		}
	}
	return r;
}

intersect(strlists : list of list of string) : list of string
{
	if (strlists == nil)
		return nil;

	okl := hd strlists;
	for (strlists = tl strlists; okl != nil && strlists != nil; strlists = tl strlists) {
		find := hd strlists;
		found : list of string = nil;
		for (; okl != nil; okl = tl okl) {
			ok := hd okl;
			for (scanl := find; scanl != nil; scanl = tl scanl) {
				scan := hd scanl;
				if (scan == ok) {
					found = ok :: found;
					break;
				}
			}
		}
		okl = found;
	}
	return sortuniq(okl);
}

# binary search for key in f.
# based on Plan 9 look.c
#
look(f: ref Iobuf, sep: int, key: string): list of string
{
	bot := mid := 0;
	top := int f.seek(big 0, Sys->SEEKEND);
	key = canon(key, sep);

	for (;;) {
		mid = (top + bot) / 2;
		f.seek(big mid, Sys->SEEKSTART);
		c: int;
		do {
			c = f.getb();
			mid++;
		} while (c != Bufio->EOF && c != Bufio->ERROR && c != '\n');
		(entry, eof) := getword(f);
		if (entry == nil && eof)
			break;
		entry = canon(entry, sep);
		case comparewords(key, entry) {
		-2 or -1 or 0 =>
			if (top <= mid)
				break;
			top = mid;
			continue;
		1 or 2 =>
			bot = mid;
			continue;
		}
		break;
	}
	matchlist : list of string;
	f.seek(big bot, Sys->SEEKSTART);
	for (;;) {
		(entry, eof) := getword(f);
		if (entry == nil && eof)
			return matchlist;
		word := canon(entry, sep);
		case comparewords(key, word) {
		-1 or 0 =>
			matchlist = entry :: matchlist;
			continue;
		1 or 2 =>
			continue;
		}
		break;
	}
	return matchlist;
}

comparewords(s, t: string): int
{
	if (s == t)
		return 0;
	i := 0;
	for (; i < len s && i < len t && s[i] == t[i]; i++)
		;
	if (i >= len s)
		return -1;
	if (i >= len t)
		return 1;
	if (s[i] < t[i])
		return -2;
	return 2;
}

getword(f: ref Iobuf): (string, int)
{
	ret := "";
	for (;;) {
		c := f.getc();
		if (c == Bufio->EOF || c == Bufio->ERROR)
			return (ret, 0);
		if (c == '\n')
			break;
		ret[len ret] = c;
	}
	return (ret, 1);
}

canon(s: string, sep: int): string
{
	if (sep < 0)
		return s;
	i := 0;
	for (; i < len s; i++)
		if (s[i] == sep)
			break;
	return s[0:i];
}