summaryrefslogtreecommitdiff
path: root/appl/cmd/sh/arg.b
blob: a0b57b84705409a33b83b4e47ef1519b6b85bfa5 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
implement Shellbuiltin;

include "sys.m";
	sys: Sys;
include "draw.m";
include "sh.m";
	sh: Sh;
	Listnode, Context: import sh;
	myself: Shellbuiltin;

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

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

getself(): Shellbuiltin
{
	return myself;
}

runbuiltin(ctxt: ref Context, nil: Sh,
			argv: list of ref Listnode, last: int): string
{
	case (hd argv).word {
	"arg" =>
		return builtin_arg(ctxt, argv, last);
	}
	return nil;
}

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

argusage(ctxt: ref Context)
{
	ctxt.fail("usage", "usage: arg [opts {command}]... - args");
}

builtin_arg(ctxt: ref Context, argv: list of ref Listnode, nil: int): string
{
	for (args := tl argv; args != nil; args = tl tl args) {
		if ((hd args).word == "-")
			break;
		if ((hd args).cmd != nil && (hd args).word == nil)
			argusage(ctxt);
		if (tl args == nil)
			argusage(ctxt);
		if ((hd tl args).cmd == nil)
			argusage(ctxt);
	}
	if (args == nil)
		args = ctxt.get("*");
	else
		args = tl args;
	laststatus := "";
	ctxt.push();
	{
		arg := Arg.init(args);
		while ((opt := arg.opt()) != 0) {
			for (argt := tl argv; argt != nil && (hd argt).word != "-"; argt = tl tl argt) {
				w := (hd argt).word;
				argcount := 0;
				for (e := len w - 1; e >= 0; e--) {
					if (w[e] != '+')
						break;
					argcount++;
				}
				w = w[0:e+1];
				if (w == nil)
					continue;
				for (i := 0; i < len w; i++)
					if (w[i] == opt || w[i] == '*')
						break;
				if (i < len w) {
					optstr := ""; optstr[0] = opt;
					ctxt.setlocal("opt", ref Listnode(nil, optstr) :: nil);
					args = arg.arg(argcount);
					if (argcount > 0 && args == nil)
						ctxt.fail("usage", sys->sprint("option -%c requires %d arguments", opt, argcount));
					ctxt.setlocal("arg", args);
					laststatus = ctxt.run(hd tl argt :: nil, 0);
					break;
				}
			}
			if (argt == nil || (hd argt).word == "-")
				ctxt.fail("usage", sys->sprint("unknown option -%c", opt));
		}
		ctxt.pop();
		ctxt.set("args", arg.args);		# XXX backward compatibility - should go
		ctxt.set("*", arg.args);
		return laststatus;
	}
	exception e{
	"fail:*" =>
		ctxt.pop();
		if (e[5:] == "break")
			return laststatus;
		raise e;
	}
}

Arg: adt {
	args: list of ref Listnode;
	curropt: string;
	init: fn(argv: list of ref Listnode): ref Arg;
	arg: fn(ctxt: self ref Arg, n: int): list of ref Listnode;
	opt: fn(ctxt: self ref Arg): int;
};
	

Arg.init(argv: list of ref Listnode): ref Arg
{
	return ref Arg(argv, nil);
}

# get next n option arguments (nil list if not enough arguments found)
Arg.arg(ctxt: self ref Arg, n: int): list of ref Listnode
{
	if (n == 0)
		return nil;

	args: list of ref Listnode;
	while (--n >= 0) {
		if (ctxt.curropt != nil) {
			args = ref Listnode(nil, ctxt.curropt) :: args;
			ctxt.curropt = nil;
		} else if (ctxt.args == nil)
			return nil;
		else {
			args = hd ctxt.args :: args;
			ctxt.args = tl ctxt.args;
		}
	}
	r: list of ref Listnode;
	for (; args != nil; args = tl args)
		r = hd args :: r;
	return r;
}

# get next option letter
# return 0 at end of options
Arg.opt(ctxt: self ref Arg): int
{
	if (ctxt.curropt != "") {
		opt := ctxt.curropt[0];
		ctxt.curropt = ctxt.curropt[1:];
		return opt;
	}

	if (ctxt.args == nil)
		return 0;

	nextarg := (hd ctxt.args).word;
	if (len nextarg < 2 || nextarg[0] != '-')
		return 0;

	if (nextarg == "--") {
		ctxt.args = tl ctxt.args;
		return 0;
	}

	opt := nextarg[1];
	if (len nextarg > 2)
		ctxt.curropt = nextarg[2:];
	ctxt.args = tl ctxt.args;
	return opt;
}