implement Xd; # # based on Plan9 xd # include "sys.m"; include "draw.m"; include "bufio.m"; Xd: module { init: fn(ctxt: ref Draw->Context, argv: list of string); }; sys : Sys; bufio : Bufio; Iobuf : import bufio; stdin, stdout, stderr : ref Sys->FD; wbytes := array [] of { 1, 2, 4, 8, }; fmtchars : con "odx"; fmtbases := array [] of { 8, 10, 16, }; fwidths := array [] of { 3, # 1o 3, # 1d 2, # 1x 6, # 2o 5, # 2d 4, # 2x 11, # 4o 10, # 4d 8, # 4x 22, # 8o 20, # 8d 16, # 8x }; bytepos := array [16] of { * => 0 }; formats := array [10] of (int, int, int); # (nbytes, base, fieldwidth) nformats := 0; addrbase := 16; repeats := 0; swab := 0; flush := 0; addr := big 0; output : ref Iobuf; pad : string; init(nil : ref Draw->Context, argv : list of string) { sys = load Sys Sys->PATH; stdin = sys->fildes(0); stdout = sys->fildes(1); stderr = sys->fildes(2); bufio = load Bufio Bufio->PATH; if (bufio == nil) { sys->fprint(stderr, "cannot load bufio: %r\n"); sys->raise("fail:init"); } output = bufio->fopen(stdout, Sys->OWRITE); if (argv == nil) sys->raise("fail:bad argv"); pad = string array [32] of { * => byte ' ' }; for (argv = tl argv; argv != nil; argv = tl argv) { arg := hd argv; if (arg == nil) continue; if (arg[0] != '-') break; if (len arg == 2) { case arg[1] { 'c' => addformat(0, 256); 'r' => repeats = 1; 's' => swab = 1; 'u' => flush = 1; * => usage(); } continue; } if (len arg == 3) { n := 0; baseix := strchr(fmtchars,arg[2]); if (baseix == -1) usage(); case arg[1] { 'a' => addrbase = fmtbases[baseix]; continue; 'b' or '1' => n = 0; 'w' or '2' => n = 1; 'l' or '4' => n = 2; 'v' or '8' => n = 3; * => usage(); } addformat(n, baseix); continue; } usage(); } if (nformats == 0) addformat(2, 2); # "4x" if (argv == nil) dump(nil, 0); else if (tl argv == nil) dump(hd argv, 0); else { for (; argv != nil; argv = tl argv) { dump(hd argv, 1); } } } usage() { sys->fprint(stderr, "usage: xd [-u] [-r] [-s] [-a{odx}] [-c|{b1w2l4v8}{odx}] ... file ...\n"); exit; } strchr(s : string, ch : int) : int { for (ix := 0; ix < len s; ix++) if (s[ix] == ch) return ix; return -1; } addformat(widix, baseix : int) { nbytes := wbytes[widix]; if (nformats >= len formats) { sys->fprint(stderr, "xd: too many formats\n"); exit; } fw : int; if (baseix == 256) { # special -c case formats[nformats++] = (nbytes, 256, 2); fw = 2; } else { fw = fwidths[baseix + (widix *len fmtbases)]; formats[nformats++] = (nbytes, fmtbases[baseix], fw); } bpos := 0; for (ix := 0; ix < 16; ix += nbytes) { if (bytepos[ix] < bpos) bytepos[ix] = bpos; else bpos = bytepos[ix]; bpos += fw + 1; } } dump(path : string, title : int) { fd := stdin; zeros := array [16] of {* => byte 0}; obuf : array of byte; buf := array [16] of byte; if (path != nil) { fd = sys->open(path, Sys->OREAD); if (fd == nil) { sys->fprint(stderr, "cannot open %s: %r\n", path); exit; } } if (title) { output.puts(path); output.putc('\n'); } addr = big 0; star := 0; for (;;) { n := 0; while (n < 16 && (r := sys->read(fd, buf[n:], 16-n)) > 0) n += r; if (n < 16) buf[n:] = zeros[n:]; if (swab) doswab(buf); if (n == 16 && repeats) { if (obuf != nil) { for (i := 0; i < 16; i++) if (obuf[i] != buf[i]) break; if (i == 16) { addr += big 16; if (star == 0) { star++; output.puts("*\n"); } continue; } } obuf = buf; star = 0; } for (fmt := 0; fmt < nformats; fmt++) { if (fmt == 0) output.puts(big2str(addr, 7, addrbase, '0')); else output.puts(big2str(addr, 7, addrbase, ' ')); output.putc(' '); (w, b, fw) := formats[fmt]; pdata(fw, w, b, n, buf); output.putc('\n'); if (flush) output.flush(); } addr += big n; if (n < 16) { output.puts(big2str(addr, 7, addrbase, '0')); output.putc('\n'); if (flush) output.flush(); break; } } output.flush(); } hexchars : con "0123456789abcdef"; big2str(b : big, minw, base, padc : int) : string { s := ""; do { d := int (b % big base); s[len s] = hexchars[d]; b /= big base; } while (b > big 0); t := ""; if (len s < minw) t = string array [minw] of { * => byte padc }; else t = s; for (i := len s - 1; i >= 0; i--) t[len t - 1 - i] = s[i]; return t; } pdata(fw, n, base, dlen : int, data : array of byte) { nout := 0; text := ""; for (i := 0; i < dlen; i += n) { if (i != 0) { padlen := bytepos[i] - nout; output.puts(pad[0:padlen]); nout += padlen; } if (base == 256) { # special -c case ch := int data[i]; case ch { '\t' => text = "\\t"; '\r' => text = "\\r"; '\n' => text = "\\n"; '\b' => text = "\\b"; * => if (ch >= 16r7f || ' ' > ch) text = sys->sprint("%.2x", ch); else text = sys->sprint("%c", ch); } } else { v := big data[i]; for (ix := 1; ix < n; ix++) v = (v << 8) + big data[i+ix]; text = big2str(v, fw, base, '0'); } output.puts(text); nout += len text; } } doswab(b : array of byte) { ix := 0; for (i := 0; i < 4; i++) { (b[ix], b[ix+3]) = (b[ix+3], b[ix]); (b[ix+1], b[ix+2]) = (b[ix+2], b[ix+1]); ix += 4; } }