summaryrefslogtreecommitdiff
path: root/appl/cmd/lego/send.b
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /appl/cmd/lego/send.b
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'appl/cmd/lego/send.b')
-rw-r--r--appl/cmd/lego/send.b86
1 files changed, 86 insertions, 0 deletions
diff --git a/appl/cmd/lego/send.b b/appl/cmd/lego/send.b
new file mode 100644
index 00000000..e83861c3
--- /dev/null
+++ b/appl/cmd/lego/send.b
@@ -0,0 +1,86 @@
+implement Send;
+
+include "sys.m";
+include "draw.m";
+include "rcxsend.m";
+
+Send : module {
+ init : fn (ctxt : ref Draw->Context, argv : list of string);
+};
+
+sys : Sys;
+rcx : RcxSend;
+me : int;
+
+init(nil : ref Draw->Context, argv : list of string)
+{
+ sys = load Sys Sys->PATH;
+ me = sys->pctl(Sys->NEWPGRP, nil);
+
+ rcx = load RcxSend "rcxsend.dis";
+ if (rcx == nil)
+ error(sys->sprint("cannot load rcx module: %r"));
+
+ argv = tl argv;
+ if (len argv < 2)
+ error("usage: send portnum XX...");
+
+ portnum := int hd argv;
+ argv = tl argv;
+
+ cmd := array [len argv] of byte;
+ for (i := 0; i < len cmd; i++) {
+ arg := hd argv;
+ argv = tl argv;
+ if (arg == nil || len arg > 2)
+ error(sys->sprint("bad arg %s\n", arg));
+ d1, d2 : int = 0;
+ d2 = hexdigit(arg[0]);
+ if (len arg == 2) {
+ d1 = d2;
+ d2 = hexdigit(arg[1]);
+ }
+ if (d1 == -1 || d2 == -1)
+ error(sys->sprint("bad arg %s\n", arg));
+ cmd[i] = byte ((d1 << 4) + d2);
+ }
+
+ rcx->init(portnum, 1);
+ reply := rcx->send(cmd, len cmd, -1);
+ hexdump(reply);
+ killgrp(me);
+}
+
+hexdigit(h : int) : int
+{
+ if (h >= '0' && h <= '9')
+ return h - '0';
+ if (h >= 'A' && h <= 'F')
+ return 10 + h - 'A';
+ if (h >= 'a' && h <= 'f')
+ return 10 + h - 'a';
+ return -1;
+}
+
+error(msg : string)
+{
+ sys->print("%s\n", msg);
+ killgrp(me);
+}
+
+killgrp(pid : int)
+{
+ pctl := sys->open("/prog/" + string pid + "/ctl", Sys->OWRITE);
+ if (pctl != nil) {
+ poison := array of byte "killgrp";
+ sys->write(pctl, poison, len poison);
+ }
+ exit;
+}
+
+hexdump(data : array of byte)
+{
+ for (i := 0; i < len data; i++)
+ sys->print("%.2x ", int data[i]);
+ sys->print("\n");
+}