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 @@
120080610
2 push sh-mload(1) and changes to sh-expr(1)
120080609320080609
2 _tas type changed from ulong to int4 _tas type changed from ulong to int
320080530520080530
appl/cmd/sh/mkfile+5−0
@@ -3,7 +3,9 @@ · 2 unmodified lines
3TARG=sh.dis\3TARG=sh.dis\
4 arg.dis\4 arg.dis\
5 expr.dis\5 expr.dis\
6 mpexpr.dis\
6 file2chan.dis\7 file2chan.dis\
8 mload.dis\
7 regex.dis\9 regex.dis\
8 sexprs.dis\10 sexprs.dis\
9 std.dis\11 std.dis\
@@ -16,6 +18,9 @@ TARG=sh.dis\ · 6 unmodified lines
16INS= $ROOT/dis/sh.dis\18INS= $ROOT/dis/sh.dis\
17 $ROOT/dis/sh/arg.dis\19 $ROOT/dis/sh/arg.dis\
18 $ROOT/dis/sh/expr.dis\20 $ROOT/dis/sh/expr.dis\
21 $ROOT/dis/sh/file2chan.dis\
22 $ROOT/dis/sh/mload.dis\
23 $ROOT/dis/sh/mpexpr.dis\
19 $ROOT/dis/sh/regex.dis\24 $ROOT/dis/sh/regex.dis\
20 $ROOT/dis/sh/std.dis\25 $ROOT/dis/sh/std.dis\
21 $ROOT/dis/sh/string.dis\26 $ROOT/dis/sh/string.dis\
appl/cmd/sh/mload.b+348−0
@@ -0,0 +1,348 @@
1implement Sh;
2include "sys.m";
3 sys: Sys;
4include "draw.m";
5include "sh.m";
6 sh: Sh;
7 myself: Shellbuiltin;
8 mysh: Sh;
9
10Namespace: adt {
11 name: string;
12 madecmd: array of int;
13 mods: list of (string, Shellbuiltin);
14 builtins: array of list of (string, Shellbuiltin);
15};
16Builtin, Sbuiltin: con iota;
17
18namespaces: list of ref Namespace;
19pending: list of (string, int, Shellbuiltin);
20lock: chan of int;
21BUILTINPATH: con "/dis/sh";
22
23initbuiltin(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
35runbuiltin(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
74mload(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
111munload(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
148runsbuiltin(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
157searchns(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
166lookup(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
194Context.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
203Context.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
212Context.removebuiltin(c: self ref Context, nil: string, nil: Shellbuiltin)
213{
214 c.fail("nope", "mload: remove builtin not implemented");
215}
216
217Context.removesbuiltin(c: self ref Context, nil: string, nil: Shellbuiltin)
218{
219 c.fail("nope", "mload: remove sbuiltin not implemented");
220}
221
222Context.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
227nslookup(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
235whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
236{
237 return nil;
238}
239
240getself(): Shellbuiltin
241{
242 return myself;
243}
244
245initialise()
246{
247 return sh->initialise();
248}
249
250init(ctxt: ref Draw->Context, argv: list of string)
251{
252 return sh->init(ctxt, argv);
253}
254
255system(ctxt: ref Draw->Context, cmd: string): string
256{
257 return sh->system(ctxt, cmd);
258}
259
260run(ctxt: ref Draw->Context, argv: list of string): string
261{
262 return sh->run(ctxt, argv);
263}
264
265parse(s: string): (ref Cmd, string)
266{
267 return sh->parse(s);
268}
269
270cmd2string(c: ref Cmd): string
271{
272 return sh->cmd2string(c);
273}
274
275list2stringlist(nl: list of ref Listnode): list of string
276{
277 return sh->list2stringlist(nl);
278}
279
280stringlist2list(sl: list of string): list of ref Listnode
281{
282 return sh->stringlist2list(sl);
283}
284
285quoted(val: list of ref Listnode, quoteblocks: int): string
286{
287 return sh->quoted(val, quoteblocks);
288}
289
290Context.new(drawcontext: ref Draw->Context): ref Context
291{
292 return sh->Context.new(drawcontext);
293}
294
295Context.get(c: self ref Context, name: string): list of ref Listnode
296{
297 return sh->c.get(name);
298}
299
300Context.set(c: self ref Context, name: string, val: list of ref Listnode)
301{
302 return sh->c.set(name, val);
303}
304
305Context.setlocal(c: self ref Context, name: string, val: list of ref Listnode)
306{
307 return sh->c.setlocal(name, val);
308}
309
310Context.envlist(c: self ref Context): list of (string, list of ref Listnode)
311{
312 return sh->c.envlist();
313}
314
315Context.push(c: self ref Context)
316{
317 return sh->c.push();
318}
319
320Context.pop(c: self ref Context)
321{
322 return sh->c.pop();
323}
324
325Context.copy(c: self ref Context, copyenv: int): ref Context
326{
327 return sh->c.copy(copyenv);
328}
329
330Context.run(c: self ref Context, args: list of ref Listnode, last: int): string
331{
332 return sh->c.run(args, last);
333}
334
335Context.fail(c: self ref Context, ename, msg: string)
336{
337 return sh->c.fail(ename, msg);
338}
339
340Context.options(c: self ref Context): int
341{
342 return sh->c.options();
343}
344
345Context.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 @@
1implement Shellbuiltin;
2
3include "sys.m";
4 sys: Sys;
5include "draw.m";
6include "keyring.m";
7 keyring: Keyring;
8 IPint: import keyring;
9include "sh.m";
10 sh: Sh;
11 Listnode, Context: import sh;
12 myself: Shellbuiltin;
13
14Big: type ref IPint;
15Zero: Big;
16One: Big;
17initbuiltin(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
33whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
34{
35 return nil;
36}
37
38getself(): Shellbuiltin
39{
40 return myself;
41}
42
43EQ, GT, LT, GE, LE, PLUS, MINUS, DIVIDE, AND, TIMES, MOD,
44OR, XOR, UMINUS, SHL, SHR, NOT, BNOT, NEQ, REP, SEQ,
45BITS, EXPMOD, INVERT, RAND, EXP: con iota;
46
47runbuiltin(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
60runsbuiltin(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
144opts(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
169operator(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
184oper(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
243bitop(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
249onebits(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.
257twoscomp(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.
266signedmag(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
273max(x, y: int): int
274{
275 if(x > y)
276 return x;
277 else
278 return y;
279}
280
281seq(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
291strtoip(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
330iptostr(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
371mki(i: int): Big
372{
373 return IPint.inttoip(i);
374}
375
376b2s(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.
385getbit(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
394digit(d: int): int
395{
396 if(d < 10)
397 return '0' + d;
398 else
399 return 'a' + d - 10;
400}
401
402log2(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
414roundup(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, ...).
421ipand(n1, n2: Big): Big
422{
423 return n1.and(n2);
424}
425
426ipor(n1, n2: Big): Big
427{
428 return n1.ori(n2);
429}
430
431
432ipxor(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 @@
1.TH SH-EXPR 11.TH SH-EXPR 1
2.SH NAME2.SH NAME
3expr, ntest \- shell module for simple arithmetic.3expr, ntest, mpexpr \- shell module for simple arithmetic.
4.SH SYNOPSIS4.SH SYNOPSIS
5.B load expr5.B load expr
6OR
7.B load mpexpr
6 8
7.B ${expr9.B ${expr
8[10[
@@ -19,10 +21,17 @@ expr, ntest \- shell module for simple arithmetic. · 10 unmodified lines
19.br21.br
20.SH DESCRIPTION22.SH DESCRIPTION
21.I Expr23.I Expr
22is a loadable module for24and
25.I mpexpr
26are loadable modules for
23.IR sh (1)27.IR sh (1)
24that provides support for simple integer arithmetic.28that provide support for integer arithmetic.
25It provides one command,29.I Expr
30uses 64-bit signed integers;
31.I mpexpr
32uses arbitrary-precision signed integers.
33They each provide the same interface:
34a command
26.IR ntest ,35.IR ntest ,
27which performs a simple boolean test36which performs a simple boolean test
28on its integer argument, and the37on its integer argument, and the
@@ -54,15 +63,16 @@ Alternative names are given for some operators; · 25 unmodified lines
54this is to avoid the necessity of quoting operators63this is to avoid the necessity of quoting operators
55that contain64that contain
56.IR sh (1)65.IR sh (1)
57metacharacters. All operations use 64-bit signed66metacharacters. Integers are given in the same form acceptable
58integers; integers are given in the same form acceptable
59to Limbo. The relational operators yield either67to Limbo. The relational operators yield either
601 (true) or 0 (false). If the681 (true) or 0 (false). If the
61.B -r69.B -r
62option is given,70option is given,
63.I radix71.I radix
64specifies an output base for numbers yielded by72specifies an output base for printed numbers.
65.IR expr .73It may be from 2 to 36;
74.I mpexpr
75also allows 64 to specify base64 notation.
66Numbers are printed in a form suitable for re-interpretation76Numbers are printed in a form suitable for re-interpretation
67by77by
68.IR expr .78.IR expr .
@@ -70,8 +80,9 @@ by · 1 unmodified line
70When all its arguments have been evaluated,80When all its arguments have been evaluated,
71.B expr81.B expr
72yields all the values remaining on its stack, first pushed82yields all the values remaining on its stack, first pushed
73first. The operators supported by expr are as follows (the number83first. Note that bitwise operators treat their operands as if they
74of operands required in is given parentheses):84were stored in two's complement form. The operators supported by expr are as follows (the number
85of operands required in is given parentheses).
75.TP 1586.TP 15
76.BR + \ (2)87.BR + \ (2)
77Addition88Addition
@@ -148,13 +159,30 @@ pushes on the stack a sequence of numbers ranging · 70 unmodified lines
148numerically from its first argument up to and including159numerically from its first argument up to and including
149its second argument. If its second argument is160its second argument. If its second argument is
150less than its first, the sequence will descend.161less than its first, the sequence will descend.
162.TP
163.BR rand \ (1)
164(\fImpexpr\fP only). Push a secure random number;
165the 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.
151.SH SOURCE176.SH SOURCE
152.B /appl/cmd/sh/expr.b177.B /appl/cmd/sh/expr.b
153.SH SEE ALSO178.SH SEE ALSO
154.IR sh (1),179.IR sh (1),
155.IR sh-std (1),180.IR sh-std (1),
156.IR sh-tk (1)181.IR sh-tk (1),
182.IR keyring-ipint (2)
157.SH BUGS183.SH BUGS
158Postfix notation can be confusing.184Postfix notation can be confusing.
159Any operators that contain shell metacharacters (e.g. ``*'', ``>'')185Any operators that contain shell metacharacters (e.g. ``*'', ``>'')
160must be quoted to avoid interpretation by the shell.186must be quoted to avoid interpretation by the shell.
187Base64 notation can contain # characters, which need
188quoting to avoid interpretation by the shell.
man/1/sh-mload+68−0
@@ -0,0 +1,68 @@
1.TH SH-MLOAD 1
2.SH NAME
3mload, 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
21is a loadable module for
22.IR sh (1)
23that allows the simultaneous use of shell modules with
24potentially clashing command name spaces.
25.B Mload
26creates a new namespace
27.I name
28and loads each
29.I path
30as a builtin module in the same way as
31.B load
32(see
33.IR sh (1)).
34Any commands or substitution builtins defined
35by the modules are accessible by giving
36the command and its arguments as arguments to
37the
38.I name
39command.
40.PP
41.B Munload
42unloads a module from the namespace
43.IR name .
44If no modules remain in the namespace,
45.I name
46is undefined as a command.
47.SH EXAMPLE
48Load
49.I mpexpr
50in a different namespace from
51.I expr
52(see
53.IR sh-expr (1)):
54.EX
55load expr
56mload mp mpexpr
57echo ${expr 1 2 +}
58echo ${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
65Because of the way shell modules are implemented,
66the namespaces are global across all processes that
67share an instance of
68.IR mload .
man/2/INDEX+23−29
@@ -150,19 +150,6 @@ readjson json · 149 unmodified lines
150writejson json150writejson json
151keyring intro keyring-0intro151keyring intro keyring-0intro
152keyring-intro keyring-0intro152keyring-intro keyring-0intro
153auth keyring-auth
154keyring keyring-auth
155keyring-auth keyring-auth
156readauthinfo keyring-auth
157writeauthinfo keyring-auth
158certtostr keyring-certtostr
159keyring keyring-certtostr
160keyring-certtostr keyring-certtostr
161pktostr keyring-certtostr
162sktostr keyring-certtostr
163strtocert keyring-certtostr
164strtopk keyring-certtostr
165strtosk keyring-certtostr
166aescbc keyring-crypt153aescbc keyring-crypt
167aessetup keyring-crypt154aessetup keyring-crypt
168descbc keyring-crypt155descbc keyring-crypt
@@ -173,24 +160,22 @@ ideaecb keyring-crypt · 4 unmodified lines
173ideasetup keyring-crypt160ideasetup keyring-crypt
174keyring keyring-crypt161keyring keyring-crypt
175keyring-crypt keyring-crypt162keyring-crypt keyring-crypt
163dsagen keyring-dsagen
164eggen keyring-dsagen
165keyring keyring-dsagen
166keyring-dsagen keyring-dsagen
167rsadecrypt keyring-dsagen
168rsaencrypt keyring-dsagen
169rsafill keyring-dsagen
170rsagen keyring-dsagen
176dhparams keyring-gensk171dhparams keyring-gensk
177gensk keyring-gensk172gensk keyring-gensk
178genskfrompk keyring-gensk173genskfrompk keyring-gensk
179keyring keyring-gensk174keyring keyring-gensk
180keyring-gensk keyring-gensk175keyring-gensk keyring-gensk
176sign keyring-gensk
181sktopk keyring-gensk177sktopk keyring-gensk
182getmsg keyring-getmsg178verify keyring-gensk
183keyring keyring-getmsg
184keyring-getmsg keyring-getmsg
185senderrmsg keyring-getmsg
186sendmsg keyring-getmsg
187getbytearray keyring-getstring
188getstring keyring-getstring
189keyring keyring-getstring
190keyring-getstring keyring-getstring
191putbytearray keyring-getstring
192puterror keyring-getstring
193putstring keyring-getstring
194ipint keyring-ipint179ipint keyring-ipint
195keyring keyring-ipint180keyring keyring-ipint
196keyring-ipint keyring-ipint181keyring-ipint keyring-ipint
@@ -207,8 +192,6 @@ keyring-sha1 keyring-sha1 · 10 unmodified lines
207md4 keyring-sha1192md4 keyring-sha1
208md5 keyring-sha1193md5 keyring-sha1
209sha1 keyring-sha1194sha1 keyring-sha1
210sign keyring-sha1
211verify keyring-sha1
212keyset keyset195keyset keyset
213allsat lists196allsat lists
214anysat lists197anysat lists
@@ -346,12 +329,23 @@ secstore secstore · 131 unmodified lines
346intro security-0intro329intro security-0intro
347security-intro security-0intro330security-intro security-0intro
348auth security-auth331auth security-auth
349client security-auth332fauth security-auth
350init security-auth333init security-auth
351security-auth security-auth334security-auth security-auth
352server security-auth
353login security-login335login security-login
354security-login security-login336security-login security-login
337certtostr security-oldauth
338oldauth security-oldauth
339pktostr security-oldauth
340readauthinfo security-oldauth
341security-oldauth security-oldauth
342sign security-oldauth
343sktostr security-oldauth
344strtocert security-oldauth
345strtopk security-oldauth
346strtosk security-oldauth
347verify security-oldauth
348writeauthinfo security-oldauth
355random security-random349random security-random
356randombuf security-random350randombuf security-random
357randomint security-random351randomint 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