xref: /inferno-os/os/init/shell.b (revision 74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a)
1implement InitShell;
2
3include "sys.m";
4include "draw.m";
5
6sys: Sys;
7
8InitShell: module
9{
10	init:	fn(nil: ref Draw->Context, nil: list of string);
11};
12
13Sh: module
14{
15	init:	fn(ctxt: ref Draw->Context, argv: list of string);
16};
17
18init(nil: ref Draw->Context, nil: list of string)
19{
20	shell := load Sh "/dis/sh.dis";
21
22	sys = load Sys Sys->PATH;
23
24	if(sys != nil)
25		sys->print("init: starting shell\n");
26
27#	sys->bind("#I", "/net", sys->MAFTER);	# IP
28	sys->bind("#p", "/prog", sys->MREPL);	# prog device
29	sys->bind("#d", "/fd", Sys->MREPL);
30	sys->bind("#i", "/dev", sys->MREPL); 	# draw device
31	sys->bind("#t", "/dev", sys->MAFTER);	# serial line
32	sys->bind("#c", "/dev", sys->MAFTER); 	# console device
33	sys->bind("#W","/dev",sys->MAFTER);	# Flash
34#	sys->bind("#O", "/dev", sys->MAFTER);	# Modem
35#	sys->bind("#T","/dev",sys->MAFTER);	# Touchscreen
36
37	srv();
38
39	spawn shell->init(nil, nil);
40}
41
42srv()
43{
44	remotedebug := sysenv("remotedebug");
45	if(remotedebug != "1")
46		return;
47
48	sys->print("srv...");
49	if(echoto("#t/eia0ctl", "b38400") < 0)
50		return;
51
52	fd := sys->open("/dev/eia0", Sys->ORDWR);
53	if (fd == nil) {
54		sys->print("eia data open: %r\n");
55		return;
56	}
57	if (sys->export(fd, "/", Sys->EXPASYNC) < 0) {
58		sys->print("export: %r\n");
59		return;
60	}
61	sys->print("ok\n");
62}
63
64sysenv(param: string): string
65{
66	fd := sys->open("#c/sysenv", sys->OREAD);
67	if (fd == nil)
68		return(nil);
69	buf := array[4096] of byte;
70	nb := sys->read(fd, buf, len buf);
71	(nfl,fl) := sys->tokenize(string buf, "\n");
72	while (fl != nil) {
73		pair := hd fl;
74		(npl, pl) := sys->tokenize(pair, "=");
75		if (npl > 1) {
76			if ((hd pl) == param)
77				return hd tl pl;
78		}
79		fl = tl fl;
80	}
81	return nil ;
82}
83
84echoto(fname, str: string): int
85{
86	fd := sys->open(fname, Sys->OWRITE);
87	if(fd == nil) {
88		sys->print("%s: %r\n", fname);
89		return -1;
90	}
91	x := array of byte str;
92	if(sys->write(fd, x, len x) == -1) {
93		sys->print("write: %r\n");
94		return -1;
95	}
96	return 0;
97}
98