summaryrefslogtreecommitdiff
path: root/appl/cmd/sh/test.b
blob: d8a6b62a4140a52309a355f3df301259f4b9d29f (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
implement Shellbuiltin;

include "sys.m";
	sys: Sys;
include "draw.m";
include "sh.m";
	sh: Sh;
	Listnode, Context: import sh;
	myself: Shellbuiltin;
include "itslib.m";
	itslib: Itslib;
	Tconfig, S_INFO, S_WARN, S_ERROR, S_FATAL: import itslib;

tconf: ref Tconfig;

initbuiltin(ctxt: ref Context, shmod: Sh): string
{
	sys = load Sys Sys->PATH;
	itslib = load Itslib Itslib->PATH;
	if (itslib != nil)
		tconf = itslib->init();
	sh = shmod;
	myself = load Shellbuiltin "$self";
	if (myself == nil)
		ctxt.fail("bad module", sys->sprint("its: cannot load self: %r"));
	ctxt.addbuiltin("report", myself);
	return nil;
}

getself(): Shellbuiltin
{
	return myself;
}


whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
{
	return nil;
}



runbuiltin(ctxt: ref Sh->Context, nil: Sh,
			cmd: list of ref Sh->Listnode, nil: int): string
{
	case (hd cmd).word {
	"report" =>
		if (len cmd < 4)
			rusage(ctxt);
		cmd = tl cmd;
		sevstr := (hd cmd).word;
		sev := sevtran(sevstr);
		if (sev < 0)
			rusage(ctxt);
		cmd = tl cmd;
		verb := (hd cmd).word;
		cmd = tl cmd;
		mtext := "";
		i := 0;
		while (len cmd) {
			msg :=  (hd cmd).word;
			cmd = tl cmd;
			if (i++ > 0)
				mtext = mtext + " ";
			mtext = mtext + msg;
		}
		if (tconf != nil)
			tconf.report(int sev, int verb, mtext);
		else
			sys->fprint(sys->fildes(2), "[itslib missing] %s %s\n", sevstr, mtext);
	}
	return nil;
}


runsbuiltin(nil: ref Sh->Context, nil: Sh,
			nil: list of ref Sh->Listnode): list of ref Listnode
{
	return nil;
}


sevtran(sname: string): int
{
	SEVMAP :=  array[] of {"INF", "WRN", "ERR", "FTL"};
	for (i:=0; i<len SEVMAP; i++)
		if (sname == SEVMAP[i])
			return i;
	return -1;
}

rusage(ctxt: ref Context)
{
	ctxt.fail("usage", "usage: report INF|WRN|ERR|FTL verbosity message[...]");
}