summaryrefslogtreecommitdiff
path: root/appl/cmd/ns.b
blob: 38bb86afc34ffc2f4dbd67a5826ba15fe7c7715d (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
# ns - display the construction of the current namespace (loosely based on plan 9's ns)
implement Ns;

include "sys.m";
	sys: Sys;

include "draw.m";

include "arg.m";

Ns: module
{
	init: fn(nil: ref Draw->Context, argv: list of string);
};

SHELLMETA: con "' \t\\$#";

usage()
{
	sys->fprint(sys->fildes(2), "usage: ns [-r] [pid]\n");
	raise "fail:usage";
}

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;

	arg := load Arg Arg->PATH;
	if(arg == nil){
		sys->fprint(sys->fildes(2), "ns: can't load %s: %r\n", Arg->PATH);
		raise "fail:load";
	}
	arg->init(args);
	pid := sys->pctl(0, nil);
	raw := 0;
	while((o := arg->opt()) != 0)
		case o {
		'r' =>
			raw = 1;
		* =>
			usage();
		}
	args = arg->argv();
	arg = nil;

	if(len args > 1)
		usage();
	if(len args > 0)
		pid = int hd args;

	nsname := sys->sprint("/prog/%d/ns", pid);
	nsfd := sys->open(nsname, Sys->OREAD);
	if(nsfd == nil) {
		sys->fprint(sys->fildes(2), "ns: can't open %s: %r\n", nsname);
		raise "fail:open";
	}

	buf := array[2048] of byte;
	while((l := sys->read(nsfd, buf, len buf)) > 0){
		(nstr, lstr) := sys->tokenize(string buf[0:l], " \n");
		if(nstr < 2)
			continue;
		cmd := hd lstr;
		lstr = tl lstr;
		if(cmd == "cd" && lstr != nil){
			sys->print("%s %s\n", cmd, quoted(hd lstr));
			continue;
		}

		sflag := "";
		if((hd lstr)[0] == '-') {
			sflag = hd lstr + " ";
			lstr = tl lstr;
		}
		if(len lstr < 2)
			continue;

		src := hd lstr;
		lstr = tl lstr;
		if(len src >= 3 && (src[0:2] == "#/" || src[0:2] == "#U")) # remove unnecesary #/'s and #U's
			src = src[2:];

		# remove "#." from beginning of destination path
		dest := hd lstr;
		if(dest == "#M") {
			dest = dest[2:];
			if(dest == "")
				dest = "/";
		}

		if(cmd == "mount" && !raw)
			src = netaddr(src);	# optionally rewrite network files to network address

		# quote arguments if "#" found
		sys->print("%s %s%s %s\n", cmd, sflag, quoted(src), quoted(dest));
	} 
	if(l < 0)
		sys->fprint(sys->fildes(2), "ns: error reading %s: %r\n", nsname);
}

netaddr(f: string): string
{
	if(len f < 1 || f[0] != '/')
		return f;
	(nf, flds) := sys->tokenize(f, "/");	# expect /net[.alt]/proto/2/data
	if(nf < 4)
		return f;
	netdir := hd flds;
	if(netdir != "net" && netdir != "net.alt")
		return f;
	proto := hd tl flds;
	d := hd tl tl flds;
	if(hd tl tl tl flds != "data")
		return f;
	fd := sys->open(sys->sprint("/%s/%s/%s/remote", hd flds, proto, d), Sys->OREAD);
	if(fd == nil)
		return f;
	buf := array[256] of byte;
	n := sys->read(fd, buf, len buf);
	if(n <= 0)
		return f;
	if(buf[n-1] == byte '\n')
		n--;
	if(netdir != "net")
		proto = "/"+netdir+"/"+proto;
	return sys->sprint("%s!%s", proto, string buf[0:n]);
}

any(c: int, t: string): int
{
	for(j := 0; j < len t; j++)
		if(c == t[j])
			return 1;
	return 0;
}

contains(s: string, t: string): int
{
	for(i := 0; i<len s; i++)
		if(any(s[i], t))
			return 1;
	return 0;
}

quoted(s: string): string
{
	if(!contains(s, SHELLMETA))
		return s;
	r := "'";
	for(i := 0; i < len s; i++){
		if(s[i] == '\'')
			r[len r] = '\'';
		r[len r] = s[i];
	}
	r[len r] = '\'';
	return r;
}