summaryrefslogtreecommitdiff
path: root/appl/lib/exception.b
blob: f31fc6b4edd130d8d422a150f429a972e369ffc6 (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
implement Exception;

include "sys.m";
	sys: Sys;
include "exception.m";

getexc(pid: int): (int, string, string)
{
	loadsys();
	if(pid < 0)
		pid = sys->pctl(0, nil);
	f := "/prog/"+string pid+"/exception";
	if((fd := sys->open(f, Sys->OREAD)) == nil)
		return (0, nil, nil);
	b := array[8192] of byte;
	if((n := sys->read(fd, b, len b)) < 0)
		return (0, nil, nil);
	s := string b[0: n];
	if(s == nil)
		return (0, nil, nil);
	(m, l) := sys->tokenize(s, " ");
	if(m < 3)
		return (0, nil, nil);
	pc := int hd l;	l = tl l;
	mod := hd l;	l = tl l;
	exc := hd l;	l = tl l;
	for( ; l != nil; l = tl l)
		exc += " " + hd l;
	return (pc, mod, exc);
}

setexcmode(mode: int): int
{
	loadsys();
	pid := sys->pctl(0, nil);
	f := "/prog/" + string pid + "/ctl";
	if(mode == NOTIFYLEADER)
		return write(f, "exceptions notifyleader");
	else if(mode == PROPAGATE)
		return write(f, "exceptions propagate");
	else
		return -1;
}

loadsys()
{
	if(sys == nil)
		sys = load Sys Sys->PATH;
}

write(f: string, s: string): int
{
	if((fd := sys->open(f, Sys->OWRITE)) == nil)
		return -1;
	b := array of byte s;
	if((n := sys->write(fd, b, len b)) != len b)
		return -1;
	return 0;
}