History

20080910-1604

Charles.Forsyth committed 18 years ago

Commit 2ea86b11 parent ae546ec10 files · +919 −39Browse files

Unified · Split

10 files
  1. CHANGES +2 −0
  2. appl/cmd/sh/mkfile +5 −0
  3. appl/cmd/sh/mload.b +348 −0
  4. appl/cmd/sh/mpexpr.b +435 −0
  5. dis/sh/mload.dis +0 −0
  6. dis/sh/mpexpr.dis +0 −0
  7. include/version.h +1 −1
  8. man/1/sh-expr +39 −11
  9. man/1/sh-mload +68 −0
  10. man/2/INDEX +23 −29
CHANGES+2−0
@@ -1,3 +1,5 @@
1+20080610
2+ push sh-mload(1) and changes to sh-expr(1)
13 20080609
24 _tas type changed from ulong to int
35 20080530
appl/cmd/sh/mkfile+5−0
@@ -3,7 +3,9 @@ · 2 unmodified lines
33 TARG=sh.dis\
44 arg.dis\
55 expr.dis\
6+ mpexpr.dis\
67 file2chan.dis\
8+ mload.dis\
79 regex.dis\
810 sexprs.dis\
911 std.dis\
@@ -16,6 +18,9 @@ TARG=sh.dis\ · 6 unmodified lines
1618 INS= $ROOT/dis/sh.dis\
1719 $ROOT/dis/sh/arg.dis\
1820 $ROOT/dis/sh/expr.dis\
21+ $ROOT/dis/sh/file2chan.dis\
22+ $ROOT/dis/sh/mload.dis\
23+ $ROOT/dis/sh/mpexpr.dis\
1924 $ROOT/dis/sh/regex.dis\
2025 $ROOT/dis/sh/std.dis\
2126 $ROOT/dis/sh/string.dis\
appl/cmd/sh/mload.b+348−0
@@ -0,0 +1,348 @@
1+implement Sh;
2+include "sys.m";
3+ sys: Sys;
4+include "draw.m";
5+include "sh.m";
6+ sh: Sh;
7+ myself: Shellbuiltin;
8+ mysh: Sh;
9+
10+Namespace: adt {
11+ name: string;
12+ madecmd: array of int;
13+ mods: list of (string, Shellbuiltin);
14+ builtins: array of list of (string, Shellbuiltin);
15+};
16+Builtin, Sbuiltin: con iota;
17+
18+namespaces: list of ref Namespace;
19+pending: list of (string, int, Shellbuiltin);
20+lock: chan of int;
21+BUILTINPATH: con "/dis/sh";
22+
23+initbuiltin(c: ref Sh->Context, shmod: Sh): string
24+{
25+ sys = load Sys Sys->PATH;
26+ sh = shmod;
27+ mysh = load Sh "$self";
28+ myself = load Shellbuiltin "$self";
29+ sh->c.addbuiltin("mload", myself);
30+ sh->c.addbuiltin("munload", myself);
31+ lock = chan[1] of int;
32+ return nil;
33+}
34+
35+runbuiltin(ctxt: ref Sh->Context, nil: Sh,
36+ argv: list of ref Sh->Listnode, last: int): string
37+{
38+ cmd := (hd argv).word;
39+ case cmd {
40+ "mload" or "munload" =>
41+ if(tl argv == nil)
42+ ctxt.fail("usage", "usage: "+cmd+" name [module...]");
43+
44+ # by doing this lock, we're relying on modules not to invoke a command
45+ # in initbuiltin that calls back into mload. since they shouldn't be running
46+ # any commands in initbuiltin anyway, this seems like a reasonable assumption.
47+ lock <-= 1;
48+ {
49+ name := (hd tl argv).word;
50+ for(argv = tl tl argv; argv != nil; argv = tl argv){
51+ if((hd argv).cmd != nil)
52+ ctxt.fail("usage", "usage: "+cmd+" namespace [module...]");
53+ if(cmd == "mload")
54+ mload(ctxt, name, (hd argv).word);
55+ else
56+ munload(ctxt, name, (hd argv).word);
57+ }
58+ }exception{
59+ "fail:*" =>
60+ <-lock;
61+ raise;
62+ }
63+ <-lock;
64+ return nil;
65+ * =>
66+ if(len argv < 2)
67+ ctxt.fail("usage", sys->sprint("usage: %s command", (hd argv).word));
68+
69+ b := lookup(ctxt, (hd argv).word, (hd tl argv).word, Builtin, nil);
70+ return b->runbuiltin(ctxt, mysh, tl argv, last);
71+ }
72+}
73+
74+mload(ctxt: ref Sh->Context, name, modname: string): string
75+{
76+ ns := nslookup(name);
77+ if(ns == nil){
78+ ns = ref Namespace(name, array[2] of {* => 0}, nil, array[2] of list of (string, Shellbuiltin));
79+ namespaces = ns :: namespaces;
80+ }
81+ for(nsm := ns.mods; nsm != nil; nsm = tl nsm)
82+ if((hd nsm).t0 == modname)
83+ return nil;
84+ path := modname;
85+ if (len path < 4 || path[len path-4:] != ".dis")
86+ path += ".dis";
87+ if (path[0] != '/' && path[0:2] != "./")
88+ path = BUILTINPATH + "/" + path;
89+ mod := load Shellbuiltin path;
90+ if (mod == nil)
91+ ctxt.fail("bad module", sys->sprint("load: cannot load %s: %r", path));
92+ s := mod->initbuiltin(ctxt, mysh);
93+ if(s != nil){
94+ munload(ctxt, name, modname);
95+ pending = nil;
96+ ctxt.fail("init", "mload: init "+modname+" failed: "+s);
97+ }
98+ mod = mod->getself();
99+ ns.mods = (modname, mod) :: ns.mods;
100+ for(; pending != nil; pending = tl pending){
101+ (cmd, which, pmod) := hd pending;
102+ if(pmod != mod)
103+ sys->fprint(sys->fildes(2), "mload: unexpected module when loading %#q", name);
104+ else
105+ lookup(ctxt, name, cmd, which, mod);
106+ }
107+
108+ return nil;
109+}
110+
111+munload(ctxt: ref Sh->Context, name, modname: string): string
112+{
113+ ns := nslookup(name);
114+ if(ns == nil){
115+ sys->fprint(sys->fildes(2), "munload: no such namespace %#q\n", name);
116+ return "fail";
117+ }
118+ nm: list of (string, Shellbuiltin);
119+ mod: Shellbuiltin;
120+ for(m := ns.mods; m != nil; m = tl m)
121+ if((hd m).t0 == modname)
122+ mod = (hd m).t1;
123+ else
124+ nm = hd m :: nm;
125+ if(mod == nil){
126+ sys->fprint(sys->fildes(2), "munload: no such module %#q\n", modname);
127+ return "fail";
128+ }
129+ ns.mods = nm;
130+ for(i := 0; i < 2; i++){
131+ nb: list of (string, Shellbuiltin) = nil;
132+ for(b := ns.builtins[i]; b != nil; b = tl b)
133+ if((hd b).t1 != mod)
134+ nb = hd b :: nb;
135+ ns.builtins[i] = nb;
136+ if(ns.builtins[i] == nil){
137+ if(i == Builtin)
138+ sh->ctxt.removebuiltin(name, myself);
139+ else
140+ sh->ctxt.removesbuiltin(name, myself);
141+ }
142+
143+ }
144+ return nil;
145+}
146+
147+
148+runsbuiltin(ctxt: ref Sh->Context, nil: Sh,
149+ argv: list of ref Sh->Listnode): list of ref Sh->Listnode
150+{
151+ if(len argv < 2)
152+ ctxt.fail("usage", sys->sprint("usage: %s command", (hd argv).word));
153+ b := lookup(ctxt, (hd argv).word, (hd tl argv).word, Sbuiltin, nil);
154+ return b->runsbuiltin(ctxt, mysh, tl argv);
155+}
156+
157+searchns(mod: Shellbuiltin): string
158+{
159+ for(m := namespaces; m != nil; m = tl m)
160+ for(b := (hd m).mods; b != nil; b = tl b)
161+ if((hd b).t1 == mod)
162+ return (hd m).name;
163+ return nil;
164+}
165+
166+lookup(ctxt: ref Sh->Context, name, cmd: string, which: int, sb: Shellbuiltin): Shellbuiltin
167+{
168+ for(m := namespaces; m != nil; m = tl m)
169+ if((hd m).name == name)
170+ break;
171+ if(m == nil)
172+ ctxt.fail("unknown", sys->sprint("unknown namespace %q", name));
173+ ns := hd m;
174+ for(b := ns.builtins[which]; b != nil; b = tl b)
175+ if((hd b).t0 == cmd)
176+ break;
177+ if(b == nil){
178+ if(sb != nil){
179+ ns.builtins[which] = (cmd, sb) :: ns.builtins[which];
180+ if(!ns.madecmd[which]){
181+ if(which == Builtin)
182+ sh->ctxt.addbuiltin(name, myself);
183+ else
184+ sh->ctxt.addsbuiltin(name, myself);
185+ ns.madecmd[which] = 1;
186+ }
187+ return sb;
188+ }
189+ ctxt.fail("unknown cmd", sys->sprint("unknown command %q", cmd));
190+ }
191+ return (hd b).t1;
192+}
193+
194+Context.addbuiltin(c: self ref Context, modname: string, mod: Shellbuiltin)
195+{
196+ name := searchns(mod);
197+ if(name == nil)
198+ pending = (modname, Builtin, mod) :: pending;
199+ else
200+ lookup(c, name, modname, Builtin, mod);
201+}
202+
203+Context.addsbuiltin(c: self ref Context, modname: string, mod: Shellbuiltin)
204+{
205+ name := searchns(mod);
206+ if(name == nil)
207+ pending = (modname, Sbuiltin, mod) :: pending;
208+ else
209+ lookup(c, name, modname, Sbuiltin, mod);
210+}
211+
212+Context.removebuiltin(c: self ref Context, nil: string, nil: Shellbuiltin)
213+{
214+ c.fail("nope", "mload: remove builtin not implemented");
215+}
216+
217+Context.removesbuiltin(c: self ref Context, nil: string, nil: Shellbuiltin)
218+{
219+ c.fail("nope", "mload: remove sbuiltin not implemented");
220+}
221+
222+Context.addmodule(nil: self ref Context, name: string, nil: Shellbuiltin)
223+{
224+ sys->fprint(sys->fildes(2), "mload: addmodule not allowed (%s)\n", name);
225+}
226+
227+nslookup(name: string): ref Namespace
228+{
229+ for(m := namespaces; m != nil; m = tl m)
230+ if((hd m).name == name)
231+ return hd m;
232+ return nil;
233+}
234+
235+whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
236+{
237+ return nil;
238+}
239+
240+getself(): Shellbuiltin
241+{
242+ return myself;
243+}
244+
245+initialise()
246+{
247+ return sh->initialise();
248+}
249+
250+init(ctxt: ref Draw->Context, argv: list of string)
251+{
252+ return sh->init(ctxt, argv);
253+}
254+
255+system(ctxt: ref Draw->Context, cmd: string): string
256+{
257+ return sh->system(ctxt, cmd);
258+}
259+
260+run(ctxt: ref Draw->Context, argv: list of string): string
261+{
262+ return sh->run(ctxt, argv);
263+}
264+
265+parse(s: string): (ref Cmd, string)
266+{
267+ return sh->parse(s);
268+}
269+
270+cmd2string(c: ref Cmd): string
271+{
272+ return sh->cmd2string(c);
273+}
274+
275+list2stringlist(nl: list of ref Listnode): list of string
276+{
277+ return sh->list2stringlist(nl);
278+}
279+
280+stringlist2list(sl: list of string): list of ref Listnode
281+{
282+ return sh->stringlist2list(sl);
283+}
284+
285+quoted(val: list of ref Listnode, quoteblocks: int): string
286+{
287+ return sh->quoted(val, quoteblocks);
288+}
289+
290+Context.new(drawcontext: ref Draw->Context): ref Context
291+{
292+ return sh->Context.new(drawcontext);
293+}
294+
295+Context.get(c: self ref Context, name: string): list of ref Listnode
296+{
297+ return sh->c.get(name);
298+}
299+
300+Context.set(c: self ref Context, name: string, val: list of ref Listnode)
301+{
302+ return sh->c.set(name, val);
303+}
304+
305+Context.setlocal(c: self ref Context, name: string, val: list of ref Listnode)
306+{
307+ return sh->c.setlocal(name, val);
308+}
309+
310+Context.envlist(c: self ref Context): list of (string, list of ref Listnode)
311+{
312+ return sh->c.envlist();
313+}
314+
315+Context.push(c: self ref Context)
316+{
317+ return sh->c.push();
318+}
319+
320+Context.pop(c: self ref Context)
321+{
322+ return sh->c.pop();
323+}
324+
325+Context.copy(c: self ref Context, copyenv: int): ref Context
326+{
327+ return sh->c.copy(copyenv);
328+}
329+
330+Context.run(c: self ref Context, args: list of ref Listnode, last: int): string
331+{
332+ return sh->c.run(args, last);
333+}
334+
335+Context.fail(c: self ref Context, ename, msg: string)
336+{
337+ return sh->c.fail(ename, msg);
338+}
339+
340+Context.options(c: self ref Context): int
341+{
342+ return sh->c.options();
343+}
344+
345+Context.setoptions(c: self ref Context, flags, on: int): int
346+{
347+ return sh->c.setoptions(flags, on);
348+}
appl/cmd/sh/mpexpr.b+435−0
@@ -0,0 +1,435 @@
1+implement Shellbuiltin;
2+
3+include "sys.m";
4+ sys: Sys;
5+include "draw.m";
6+include "keyring.m";
7+ keyring: Keyring;
8+ IPint: import keyring;
9+include "sh.m";
10+ sh: Sh;
11+ Listnode, Context: import sh;
12+ myself: Shellbuiltin;
13+
14+Big: type ref IPint;
15+Zero: Big;
16+One: Big;
17+initbuiltin(ctxt: ref Context, shmod: Sh): string
18+{
19+ sys = load Sys Sys->PATH;
20+ keyring = load Keyring Keyring->PATH;
21+ sh = shmod;
22+ myself = load Shellbuiltin "$self";
23+ if (myself == nil)
24+ ctxt.fail("bad module", sys->sprint("expr: cannot load self: %r"));
25+
26+ Zero = IPint.inttoip(0);
27+ One = IPint.inttoip(1);
28+ ctxt.addsbuiltin("expr", myself);
29+ ctxt.addbuiltin("ntest", myself);
30+ return nil;
31+}
32+
33+whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
34+{
35+ return nil;
36+}
37+
38+getself(): Shellbuiltin
39+{
40+ return myself;
41+}
42+
43+EQ, GT, LT, GE, LE, PLUS, MINUS, DIVIDE, AND, TIMES, MOD,
44+OR, XOR, UMINUS, SHL, SHR, NOT, BNOT, NEQ, REP, SEQ,
45+BITS, EXPMOD, INVERT, RAND, EXP: con iota;
46+
47+runbuiltin(ctxt: ref Sh->Context, nil: Sh,
48+ cmd: list of ref Sh->Listnode, nil: int): string
49+{
50+ case (hd cmd).word {
51+ "ntest" =>
52+ if (len cmd != 2)
53+ ctxt.fail("usage", "usage: ntest n");
54+ if(strtoip(ctxt, (hd tl cmd).word).eq(Zero))
55+ return "false";
56+ }
57+ return nil;
58+}
59+
60+runsbuiltin(ctxt: ref Sh->Context, nil: Sh,
61+ cmd: list of ref Sh->Listnode): list of ref Listnode
62+{
63+ # only one sbuiltin: expr.
64+ stk: list of Big;
65+ lastop := -1;
66+ lastn := -1;
67+ lastname := "";
68+ radix: int;
69+ (cmd, radix) = opts(ctxt, tl cmd);
70+ for (; cmd != nil; cmd = tl cmd) {
71+ w := (hd cmd).word;
72+ op := -1;
73+ nops := 2;
74+ case w {
75+ "+" =>
76+ op = PLUS;
77+ "-" =>
78+ op = MINUS;
79+ "x" or "*" or "×" =>
80+ op = TIMES;
81+ "/" =>
82+ op = DIVIDE;
83+ "%" =>
84+ op = MOD;
85+ "and" =>
86+ op = AND;
87+ "or" =>
88+ op = OR;
89+ "xor" =>
90+ op = XOR;
91+ "_"=>
92+ (op, nops) = (UMINUS, 1);
93+ "<<" or "shl" =>
94+ op = SHL;
95+ ">>" or "shr" =>
96+ op = SHR;
97+ "=" or "==" or "eq" =>
98+ op = EQ;
99+ "!=" or "neq" =>
100+ op = NEQ;
101+ ">" or "gt" =>
102+ op = GT;
103+ "<" or "lt" =>
104+ op = LT;
105+ ">=" or "ge" =>
106+ op = GE;
107+ "<=" or "le" =>
108+ op = LE;
109+ "!" or "not" =>
110+ (op, nops) = (NOT, 1);
111+ "~" =>
112+ (op, nops) = (BNOT, 1);
113+ "rep" =>
114+ (op, nops) = (REP, 0);
115+ "seq" =>
116+ (op, nops) = (SEQ, 2);
117+ "bits" =>
118+ (op, nops) = (BITS, 1);
119+ "expmod" =>
120+ (op, nops) = (EXPMOD, 3);
121+ "invert" =>
122+ (op, nops) = (INVERT, 2);
123+ "rand" =>
124+ (op, nops) = (RAND, 1);
125+ "exp" or "xx" or "**" =>
126+ (op, nops) = (EXP, 2);
127+ }
128+ if (op == -1){
129+ if (w == nil || (w[0] != '-' && (w[0] < '0' || w[0] > '9')))
130+ ctxt.fail("usage", sys->sprint("expr: unknown operator '%s'", w));
131+ stk = strtoip(ctxt, w) :: stk;
132+ }else
133+ stk = operator(ctxt, stk, op, nops, lastop, lastn, w, lastname);
134+ lastop = op;
135+ lastn = nops;
136+ lastname = w;
137+ }
138+ r: list of ref Listnode;
139+ for (; stk != nil; stk = tl stk)
140+ r = ref Listnode(nil, iptostr(hd stk, radix)) :: r;
141+ return r;
142+}
143+
144+opts(ctxt: ref Context, cmd: list of ref Listnode): (list of ref Listnode, int)
145+{
146+ if (cmd == nil)
147+ return (nil, 10);
148+ w := (hd cmd).word;
149+ if (len w < 2)
150+ return (cmd, 10);
151+ if (w[0] != '-' || (w[1] >= '0' && w[1] <= '9'))
152+ return (cmd, 10);
153+ if (w[1] != 'r')
154+ ctxt.fail("usage", "usage: expr [-r radix] [arg...]");
155+ if (len w > 2)
156+ w = w[2:];
157+ else {
158+ if (tl cmd == nil)
159+ ctxt.fail("usage", "usage: expr [-r radix] [arg...]");
160+ cmd = tl cmd;
161+ w = (hd cmd).word;
162+ }
163+ r := int w;
164+ if (r <= 0 || (r > 36 && r != 64))
165+ ctxt.fail("usage", "expr: invalid radix " + string r);
166+ return (tl cmd, int w);
167+}
168+
169+operator(ctxt: ref Context, stk: list of Big, op, nops, lastop, lastn: int,
170+ opname, lastopname: string): list of Big
171+{
172+ al: list of Big;
173+ for (i := 0; i < nops; i++) {
174+ if (stk == nil)
175+ ctxt.fail("empty stack",
176+ sys->sprint("expr: empty stack on op '%s'", opname));
177+ al = hd stk :: al;
178+ stk = tl stk;
179+ }
180+ return oper(ctxt, al, op, lastop, lastn, lastopname, stk);
181+}
182+
183+# args are in reverse order
184+oper(ctxt: ref Context, args: list of Big, op, lastop, lastn: int,
185+ lastopname: string, stk: list of Big): list of Big
186+{
187+ if (op == REP) {
188+ if (lastop == -1 || lastop == SEQ || lastn != 2)
189+ ctxt.fail("usage", "expr: bad operator for rep");
190+ if (stk == nil || tl stk == nil)
191+ return stk;
192+ while (tl stk != nil)
193+ stk = operator(ctxt, stk, lastop, 2, -1, -1, lastopname, nil);
194+ return stk;
195+ }
196+ n3 := Zero;
197+ n2 := Zero;
198+ n1 := hd args;
199+ if (tl args != nil){
200+ n2 = hd tl args;
201+ if(tl tl args != nil)
202+ n3 = hd tl tl args;
203+ }
204+ r := Zero;
205+ case op {
206+ EQ => r = mki(n1.eq(n2));
207+ NEQ => r = mki(!n1.eq(n2));
208+ GT => r = mki(n1.cmp(n2) > 0);
209+ LT => r = mki(n1.cmp(n2) < 0);
210+ GE => r = mki(n1.cmp(n2) >= 0);
211+ LE => r = mki(n1.cmp(n2) <= 0);
212+ PLUS => r = n1.add(n2);
213+ MINUS => r = n1.sub(n2);
214+ NOT => r = mki(n1.eq(Zero));
215+ DIVIDE =>
216+ if (n2.eq(Zero))
217+ ctxt.fail("divide by zero", "expr: division by zero");
218+ (r, nil) = n1.div(n2);
219+ MOD =>
220+ if (n2.eq(Zero))
221+ ctxt.fail("divide by zero", "expr: division by zero");
222+ (nil, r) = n1.div(n2);
223+ TIMES =>
224+ r = n1.mul(n2);
225+ AND => r = bitop(ipand, n1, n2);
226+ OR => r = bitop(ipor, n1, n2);
227+ XOR => r = bitop(ipxor, n1, n2);
228+ UMINUS => r = n1.neg();
229+ BNOT => r = n1.neg().sub(One);
230+ SHL => r = n1.shl(n2.iptoint());
231+ SHR => r = n1.shr(n2.iptoint());
232+ SEQ => return seq(n1, n2, stk);
233+ BITS => r = mki(n1.bits());
234+ EXPMOD => r = n1.expmod(n2, n3);
235+ EXP => r = n1.expmod(n2, nil);
236+ RAND => r = IPint.random(0, n1.iptoint());
237+ INVERT => r = n1.invert(n2);
238+ }
239+ return r :: stk;
240+}
241+
242+# won't work if op(0, 0) != 0
243+bitop(op: ref fn(n1, n2: Big): Big, n1, n2: Big): Big
244+{
245+ bits := max(n1.bits(), n2.bits());
246+ return signedmag(op(twoscomp(n1, bits), twoscomp(n2, bits)), bits);
247+}
248+
249+onebits(n: int): Big
250+{
251+ return One.shl(n).sub(One);
252+}
253+
254+# return a two's complement version of n,
255+# sign-extended to b bits if negative.
256+# sign bit is at 1<<b.
257+twoscomp(n: Big, b: int): Big
258+{
259+ if(n.cmp(Zero) >= 0)
260+ return n;
261+ return n.not().ori(onebits(b).xor(onebits(n.bits()))).add(One);
262+}
263+
264+# return conventional representation of n,
265+# where n is in two's complement form in b bits.
266+signedmag(n: Big, b: int): Big
267+{
268+ if(n.and(One.shl(b)).eq(Zero))
269+ return n;
270+ return n.sub(One).not().and(onebits(b)).neg();
271+}
272+
273+max(x, y: int): int
274+{
275+ if(x > y)
276+ return x;
277+ else
278+ return y;
279+}
280+
281+seq(n1, n2: Big, stk: list of Big): list of Big
282+{
283+ incr := mki(1);
284+ if (n2.cmp(n1) < 0)
285+ incr = mki(-1);
286+ for (; !n1.eq(n2); n1 = n1.add(incr))
287+ stk = n1 :: stk;
288+ return n1 :: stk;
289+}
290+
291+strtoip(ctxt: ref Context, s: string): Big
292+{
293+ t := s;
294+ if (neg := s[0] == '-')
295+ s = s[1:];
296+ radix := 10;
297+ for (i := 0; i < len s && i < 3; i++) {
298+ if (s[i] == 'r') {
299+ radix = int s;
300+ s = s[i+1:];
301+ break;
302+ }
303+ }
304+ if (radix == 10)
305+ return IPint.strtoip(s, 10);
306+ if (radix == 0 || (radix > 36 && radix != 64))
307+ ctxt.fail("usage", "expr: bad number " + t);
308+ n := Zero;
309+ case radix {
310+ 10 or 16 or 64 =>
311+ n = IPint.strtoip(s, radix);
312+ * =>
313+ r := mki(radix);
314+ for (i = 0; i < len s; i++) {
315+ if ('0' <= s[i] && s[i] <= '9')
316+ n = n.mul(r).add(mki(s[i] - '0'));
317+ else if ('a' <= s[i] && s[i] < 'a' + radix - 10)
318+ n = n.mul(r).add(mki(s[i] - 'a' + 10));
319+ else if ('A' <= s[i] && s[i] < 'A' + radix - 10)
320+ n = n.mul(r).add(mki(s[i] - 'A' + 10));
321+ else
322+ break;
323+ }
324+ }
325+ if(neg)
326+ return n.neg();
327+ return n;
328+}
329+
330+iptostr(n: Big, radix: int): string
331+{
332+ neg := n.cmp(Zero) < 0;
333+ t: string;
334+ case radix {
335+ 2 or 4 or 16 or 32 =>
336+ b := n.iptobebytes();
337+ rbits := log2(radix);
338+ bits := roundup(n.bits(), rbits);
339+ for(i := bits - rbits; i >= 0; i -= rbits){
340+ d := 0;
341+ for(j := 0; j < rbits; j++)
342+ d |= getbit(b, i+j) << j;
343+ t[len t] = digit(d);
344+ }
345+ 10 =>
346+ return n.iptostr(radix);
347+ 64 =>
348+ t = n.iptostr(radix);
349+ if(neg)
350+ t = t[1:];
351+ * =>
352+ if(neg)
353+ n = n.neg();
354+ r := mki(radix);
355+ s: string;
356+ do{
357+ d: Big;
358+ (n, d) = n.div(r);
359+ s[len s] = digit(d.iptoint());
360+ }while(n.cmp(Zero) > 0);
361+ t = s;
362+ for (i := len s - 1; i >= 0; i--)
363+ t[len s - 1 - i] = s[i];
364+ }
365+ t = string radix + "r" + t;
366+ if (neg)
367+ return "-" + t;
368+ return t;
369+}
370+
371+mki(i: int): Big
372+{
373+ return IPint.inttoip(i);
374+}
375+
376+b2s(b: array of byte): string
377+{
378+ s := "";
379+ for(i := 0; i < len b; i++)
380+ s += sys->sprint("%.2x", int b[i]);
381+ return s;
382+}
383+
384+# count from least significant bit.
385+getbit(b: array of byte, bit: int): int
386+{
387+ if((i := bit >> 3) >= len b){
388+ return 0;
389+ }else{
390+ return (int b[len b - i -1] >> (bit&7)) & 1;
391+ }
392+}
393+
394+digit(d: int): int
395+{
396+ if(d < 10)
397+ return '0' + d;
398+ else
399+ return 'a' + d - 10;
400+}
401+
402+log2(x: int): int
403+{
404+ case x {
405+ 2 => return 1;
406+ 4 => return 2;
407+ 8 => return 3;
408+ 16 => return 4;
409+ 32 => return 5;
410+ }
411+ return 0;
412+}
413+
414+roundup(n: int, m: int): int
415+{
416+ return m*((n+m-1)/m);
417+}
418+
419+# these functions are to get around the fact that the limbo compiler isn't
420+# currently considering ref fn(x: self X, ...) compatible with ref fn(x: X, ...).
421+ipand(n1, n2: Big): Big
422+{
423+ return n1.and(n2);
424+}
425+
426+ipor(n1, n2: Big): Big
427+{
428+ return n1.ori(n2);
429+}
430+
431+
432+ipxor(n1, n2: Big): Big
433+{
434+ return n1.xor(n2);
435+}
dis/sh/mload.dis+0−0

Binary file not shown.

dis/sh/mpexpr.dis+0−0

Binary file not shown.

include/version.h+1−1
@@ -1 +1 @@
1−#define VERSION "Fourth Edition (20080609)"
1+#define VERSION "Fourth Edition (20080610)"
man/1/sh-expr+39−11
@@ -1,8 +1,10 @@
11 .TH SH-EXPR 1
22 .SH NAME
3−expr, ntest \- shell module for simple arithmetic.
3+expr, ntest, mpexpr \- shell module for simple arithmetic.
44 .SH SYNOPSIS
55 .B load expr
6+OR
7+.B load mpexpr
68
79 .B ${expr
810 [
@@ -19,10 +21,17 @@ expr, ntest \- shell module for simple arithmetic. · 10 unmodified lines
1921 .br
2022 .SH DESCRIPTION
2123 .I Expr
22−is a loadable module for
24+and
25+.I mpexpr
26+are loadable modules for
2327 .IR sh (1)
24−that provides support for simple integer arithmetic.
25−It provides one command,
28+that provide support for integer arithmetic.
29+.I Expr
30+uses 64-bit signed integers;
31+.I mpexpr
32+uses arbitrary-precision signed integers.
33+They each provide the same interface:
34+a command
2635 .IR ntest ,
2736 which performs a simple boolean test
2837 on its integer argument, and the
@@ -54,15 +63,16 @@ Alternative names are given for some operators; · 25 unmodified lines
5463 this is to avoid the necessity of quoting operators
5564 that contain
5665 .IR sh (1)
57−metacharacters. All operations use 64-bit signed
58−integers; integers are given in the same form acceptable
66+metacharacters. Integers are given in the same form acceptable
5967 to Limbo. The relational operators yield either
6068 1 (true) or 0 (false). If the
6169 .B -r
6270 option is given,
6371 .I radix
64−specifies an output base for numbers yielded by
65−.IR expr .
72+specifies an output base for printed numbers.
73+It may be from 2 to 36;
74+.I mpexpr
75+also allows 64 to specify base64 notation.
6676 Numbers are printed in a form suitable for re-interpretation
6777 by
6878 .IR expr .
@@ -70,8 +80,9 @@ by · 1 unmodified line
7080 When all its arguments have been evaluated,
7181 .B expr
7282 yields all the values remaining on its stack, first pushed
73−first. The operators supported by expr are as follows (the number
74−of operands required in is given parentheses):
83+first. Note that bitwise operators treat their operands as if they
84+were stored in two's complement form. The operators supported by expr are as follows (the number
85+of operands required in is given parentheses).
7586 .TP 15
7687 .BR + \ (2)
7788 Addition
@@ -148,13 +159,30 @@ pushes on the stack a sequence of numbers ranging · 70 unmodified lines
148159 numerically from its first argument up to and including
149160 its second argument. If its second argument is
150161 less than its first, the sequence will descend.
162+.TP
163+.BR rand \ (1)
164+(\fImpexpr\fP only). Push a secure random number;
165+the argument value gives the size of the number, in bits.
166+.TP
167+.BR bits \ (1)
168+(\fImpexpr\fP only). Push the size, in bits, of the argument.
169+.TP
170+.BR expmod ", " invert " (2)"
171+(\fImpexpr\fP only). See
172+.IR ipint (2).
173+.TP
174+.BR exp ", " xx ", " **
175+(\fImpexpr\fP only). Exponentiation.
151176 .SH SOURCE
152177 .B /appl/cmd/sh/expr.b
153178 .SH SEE ALSO
154179 .IR sh (1),
155180 .IR sh-std (1),
156−.IR sh-tk (1)
181+.IR sh-tk (1),
182+.IR keyring-ipint (2)
157183 .SH BUGS
158184 Postfix notation can be confusing.
159185 Any operators that contain shell metacharacters (e.g. ``*'', ``>'')
160186 must be quoted to avoid interpretation by the shell.
187+Base64 notation can contain # characters, which need
188+quoting to avoid interpretation by the shell.
man/1/sh-mload+68−0
@@ -0,0 +1,68 @@
1+.TH SH-MLOAD 1
2+.SH NAME
3+mload, munload \- namespace separation for shell modules
4+.SH SYNOPSIS
5+.B load mload
6+
7+.B mload
8+.I name
9+[
10+.IR path ...
11+]
12+.br
13+.B munload
14+.I name
15+[
16+.IR path ...
17+]
18+.br
19+.SH DESCRIPTION
20+.I Mload
21+is a loadable module for
22+.IR sh (1)
23+that allows the simultaneous use of shell modules with
24+potentially clashing command name spaces.
25+.B Mload
26+creates a new namespace
27+.I name
28+and loads each
29+.I path
30+as a builtin module in the same way as
31+.B load
32+(see
33+.IR sh (1)).
34+Any commands or substitution builtins defined
35+by the modules are accessible by giving
36+the command and its arguments as arguments to
37+the
38+.I name
39+command.
40+.PP
41+.B Munload
42+unloads a module from the namespace
43+.IR name .
44+If no modules remain in the namespace,
45+.I name
46+is undefined as a command.
47+.SH EXAMPLE
48+Load
49+.I mpexpr
50+in a different namespace from
51+.I expr
52+(see
53+.IR sh-expr (1)):
54+.EX
55+load expr
56+mload mp mpexpr
57+echo ${expr 1 2 +}
58+echo ${mp expr 2 300 xx}
59+.EE
60+.SH SOURCE
61+.B /appl/cmd/sh/mload.b
62+.SH SEE ALSO
63+.IR sh (1),
64+.SH BUGS
65+Because of the way shell modules are implemented,
66+the namespaces are global across all processes that
67+share an instance of
68+.IR mload .
man/2/INDEX+23−29
@@ -150,19 +150,6 @@ readjson json · 149 unmodified lines
150150 writejson json
151151 keyring intro keyring-0intro
152152 keyring-intro keyring-0intro
153−auth keyring-auth
154−keyring keyring-auth
155−keyring-auth keyring-auth
156−readauthinfo keyring-auth
157−writeauthinfo keyring-auth
158−certtostr keyring-certtostr
159−keyring keyring-certtostr
160−keyring-certtostr keyring-certtostr
161−pktostr keyring-certtostr
162−sktostr keyring-certtostr
163−strtocert keyring-certtostr
164−strtopk keyring-certtostr
165−strtosk keyring-certtostr
166153 aescbc keyring-crypt
167154 aessetup keyring-crypt
168155 descbc keyring-crypt
@@ -173,24 +160,22 @@ ideaecb keyring-crypt · 4 unmodified lines
173160 ideasetup keyring-crypt
174161 keyring keyring-crypt
175162 keyring-crypt keyring-crypt
163+dsagen keyring-dsagen
164+eggen keyring-dsagen
165+keyring keyring-dsagen
166+keyring-dsagen keyring-dsagen
167+rsadecrypt keyring-dsagen
168+rsaencrypt keyring-dsagen
169+rsafill keyring-dsagen
170+rsagen keyring-dsagen
176171 dhparams keyring-gensk
177172 gensk keyring-gensk
178173 genskfrompk keyring-gensk
179174 keyring keyring-gensk
180175 keyring-gensk keyring-gensk
176+sign keyring-gensk
181177 sktopk keyring-gensk
182−getmsg keyring-getmsg
183−keyring keyring-getmsg
184−keyring-getmsg keyring-getmsg
185−senderrmsg keyring-getmsg
186−sendmsg keyring-getmsg
187−getbytearray keyring-getstring
188−getstring keyring-getstring
189−keyring keyring-getstring
190−keyring-getstring keyring-getstring
191−putbytearray keyring-getstring
192−puterror keyring-getstring
193−putstring keyring-getstring
178+verify keyring-gensk
194179 ipint keyring-ipint
195180 keyring keyring-ipint
196181 keyring-ipint keyring-ipint
@@ -207,8 +192,6 @@ keyring-sha1 keyring-sha1 · 10 unmodified lines
207192 md4 keyring-sha1
208193 md5 keyring-sha1
209194 sha1 keyring-sha1
210−sign keyring-sha1
211−verify keyring-sha1
212195 keyset keyset
213196 allsat lists
214197 anysat lists
@@ -346,12 +329,23 @@ secstore secstore · 131 unmodified lines
346329 intro security-0intro
347330 security-intro security-0intro
348331 auth security-auth
349−client security-auth
332+fauth security-auth
350333 init security-auth
351334 security-auth security-auth
352−server security-auth
353335 login security-login
354336 security-login security-login
337+certtostr security-oldauth
338+oldauth security-oldauth
339+pktostr security-oldauth
340+readauthinfo security-oldauth
341+security-oldauth security-oldauth
342+sign security-oldauth
343+sktostr security-oldauth
344+strtocert security-oldauth
345+strtopk security-oldauth
346+strtosk security-oldauth
347+verify security-oldauth
348+writeauthinfo security-oldauth
355349 random security-random
356350 randombuf security-random
357351 randomint security-random

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t