summaryrefslogtreecommitdiff
path: root/appl/alphabet/main/read.b
blob: ebc4d156a64d68f8e419b8d386c6072c30e89002 (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
implement Read,Mainmodule;
include "sys.m";
	sys: Sys;
include "draw.m";
include "sh.m";
include "alphabet/reports.m";
	reports: Reports;
		Report, report: import reports;
include "alphabet.m";
	alphabet: Alphabet;
		Value: import alphabet;

Read: module{};

typesig(): string
{
	return "fs";
}

init()
{
	sys = load Sys Sys->PATH;
	alphabet = load Alphabet Alphabet->PATH;
	reports = load Reports Reports->PATH;
}

quit()
{
}

run(nil: ref Draw->Context, r: ref Reports->Report, errorc: chan of string,
		nil: list of (int, list of ref Alphabet->Value),
		args: list of ref Alphabet->Value): ref Alphabet->Value
{
	f := chan of ref Sys->FD;
	file := (hd args).s().i;
	if((fd0 := sys->open(file, Sys->OREAD)) == nil){
		report(errorc, sys->sprint("cannot open %q: %r", file));
		return nil;
	}
	spawn readproc(f, fd0, r.start("read"));
	return ref Value.Vf(f);
}

readproc(f: chan of ref Sys->FD, fd0: ref Sys->FD, errorc: chan of string)
{
	f <-= fd0;
	fd1 := <-f;
	if(fd1 == nil)
		reports->quit(errorc);
	buf := array[8192] of byte;
	while((n := sys->read(fd0, buf, len buf)) > 0)
		sys->write(fd1, buf, n);
	sys->write(fd1, array[0] of byte, 0);
	reports->quit(errorc);
}