summaryrefslogtreecommitdiff
path: root/appl/cmd/mash/history.b
blob: 7f7cf9b633ac481d8b57789d6acbe9ae94d37ac7 (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
implement Mashbuiltin;

#
#	"history" builtin, defines:
#

include	"mash.m";
include	"mashparse.m";

mashlib:	Mashlib;
chanfill:	ChanFill;

Env:		import mashlib;
sys, bufio:	import mashlib;

Iobuf:	import bufio;

Hcmd: adt
{
	seek:	int;
	text:	array of byte;
};

Reader: adt
{
	fid:	int;
	offset:	int;
	hint:	int;
	next:	cyclic ref Reader;
};

history:	array of ref Hcmd;
lhist:		int;
nhist:		int;
seek:		int;
readers:	ref Reader;
eof :=		array[0] of byte;

#
#	Interface to catch the use as a command.
#
init(nil: ref Draw->Context, args: list of string)
{
	raise "fail: " + hd args + " not loaded";
}

#
#	Used by whatis.
#
name(): string
{
	return "history";
}

#
#	Install commands.
#
mashinit(nil: list of string, lib: Mashlib, nil: Mashbuiltin, e: ref Env)
{
	mashlib = lib;
	if (mashlib->histchan != nil)
		return;
	mashlib->startserve = 1;
	nhist = 0;
	lhist = 256;
	history = array[lhist] of ref Hcmd;
	seek = 0;
	(f, c) := e.servefile(mashlib->HISTF);
	spawn servehist(f, c);
	(f, c) = e.servefile(mashlib->MASHF);
	spawn servemash(f, c);
}

mashcmd(nil: ref Env, nil: list of string)
{
}

addhist(b: array of byte)
{
	if (nhist == lhist) {
		n := 3 * nhist / 4;
		part := history[:n];
		part[:] = history[nhist - n:];
		nhist = n;
	}
	history[nhist] = ref Hcmd(seek, b);
	nhist++;
	seek += len b;
}

getfid(fid: int, del: int): ref Reader
{
	prev: ref Reader;
	for (r := readers; r != nil; r = r.next) {
		if (r.fid == fid) {
			if (del) {
				if (prev == nil)
					readers = r.next;
				else
					prev.next = r.next;
				return nil;
			}
			return r;
		}
		prev = r;
	}
	o := 0;
	if (nhist > 0)
		o = history[0].seek;
	return readers = ref Reader(fid, o, 0, readers);
}

readhist(off, count, fid: int): (array of byte, string)
{
	r := getfid(fid, 0);
	off += r.offset;
	if (nhist == 0 || off >= seek)
		return (eof, nil);
	i := r.hint;
	if (i >= nhist)
		i = nhist - 1;
	s := history[i].seek;
	if (off == s) {
		r.hint = i + 1;
		return (history[i].text, nil);
	}
	if (off > s) {
		do {
			if (++i == nhist)
				break;
			s = history[i].seek;
		} while (off >= s);
		i--;
	} else {
		do {
			if (--i < 0)
				return (eof, "data truncated");
			s = history[i].seek;
		} while (off < s);
	}
	r.hint = i + 1;
	b := history[i].text;
	if (off != s)
		b = b[off - s:];
	return (b, nil);
}

loadhist(data: array of byte, fid: int, wc: Sys->Rwrite, c: ref Sys->FileIO)
{
	in: ref Iobuf;
	if (chanfill == nil)
		chanfill = load ChanFill ChanFill->PATH;
	if (chanfill != nil)
		in = chanfill->init(data, fid, wc, c, mashlib->bufio);
	if (in == nil) {
		in = bufio->sopen(string data);
		if (in == nil) {
			wc <-= (0, mashlib->errstr());
			return;
		}
		wc <-= (len data, nil);
	}
	while ((s := in.gets('\n')) != nil)
		addhist(array of byte s);
	in.close();
}

servehist(f: string, c: ref Sys->FileIO)
{
	mashlib->reap();
	h := chan of array of byte;
	mashlib->histchan = h;
	for (;;) {
		alt {
		b := <-h =>
			addhist(b);
		(off, count, fid, rc) := <-c.read =>
			if (rc == nil) {
				getfid(fid, 1);
				continue;
			}
			rc <-= readhist(off, count, fid);
		(off, data, fid, wc) := <-c.write =>
			if (wc != nil)
				loadhist(data, fid, wc, c);
		}
	}
}

servemash(f: string, c: ref Sys->FileIO)
{
	mashlib->reap();
	for (;;) {
		alt {
		(off, count, fid, rc) := <-c.read =>
			if (rc != nil)
				rc <-= (nil, "not supported");
		(off, data, fid, wc) := <-c.write =>
			if (wc != nil) {
				wc <-= (len data, nil);
				if (mashlib->servechan != nil && len data > 0)
					mashlib->servechan <-= data;
			}
		}
	}
}