summaryrefslogtreecommitdiff
path: root/appl/cmd/bind.b
blob: fa6c734b105744331f30a19bafb90cd30cd712cf (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
implement Bind;

include "sys.m";
	sys: Sys;

include "draw.m";

Bind: module
{
	init:	fn(ctxt: ref Draw->Context, args: list of string);
};

stderr: ref Sys->FD;

usage()
{
	sys->fprint(stderr, "usage: bind [-a|-b|-c|-ac|-bc] [-q] source target\n");
	raise "fail:usage";
}

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

	stderr = sys->fildes(2);
	flags := 0;
	qflag := 0;
	if(args != nil)
		args = tl args;
	while(args != nil && (a := hd args) != "" && a[0] == '-'){
		args = tl args;
		if(a == "--")
			break;
		for(o := 1; o < len a; o++)
			case a[o] {
			'a' =>
				flags |= Sys->MAFTER;
			'b' =>
				flags |= Sys->MBEFORE;
			'c' =>
				flags |= Sys->MCREATE;
			'q' =>
				qflag = 1;
			* =>
				usage();
			}
	}
	if(len args != 2 || flags&Sys->MAFTER && flags&Sys->MBEFORE)
		usage();

	f1 := hd args;
	f2 := hd tl args;
	if(sys->bind(f1, f2, flags) < 0){
		if(qflag)
			exit;
		#  try to improve the error message
		err := sys->sprint("%r");
		if(sys->stat(f1).t0 < 0)
			sys->fprint(stderr, "bind: %s: %r\n", f1);
		else if(sys->stat(f2).t0 < 0)
			sys->fprint(stderr, "bind: %s: %r\n", f2);
		else
			sys->fprint(stderr, "bind: cannot bind %s onto %s: %s\n", f1, f2, err);
		raise "fail:bind";
	}
}