#include #include #include enum { Nsee = 4*4, /* process this many bytes at a time; see swizz */ }; uchar odata[Nsee]; uchar data[2*Nsee]; int ndata; int nread; ulong addr; int repeats; int swizzle; int flush; int abase=2; int xd(char *, int); void xprint(char *, ...); void initarg(void), swizz(void); enum{ Narg=10, TNone=0, TAscii, TRune, }; typedef struct Arg Arg; typedef void fmtfn(char *); struct Arg { int chartype; /* TNone, TAscii, TRunes */ int loglen; /* 0==1, 1==2, 2==4, 3==8 */ int base; /* 0==8, 1==10, 2==16 */ fmtfn *fn; /* function to call with data */ char *afmt; /* format to use to print address */ char *fmt; /* format to use to print data */ }arg[Narg]; int narg; fmtfn fmt0, fmt1, fmt2, fmt3, fmtc, fmtr; fmtfn *fmt[4] = { fmt0, fmt1, fmt2, fmt3 }; char *dfmt[4][3] = { " %.3uo", " %.3ud", " %.2ux", " %.6uo", " %.5ud", " %.4ux", " %.11luo", " %.10lud", " %.8lux", " %.22lluo", " %.20llud", " %.16llux", }; char *cfmt[3][3] = { " %c", " %c", " %c", " %.3s", " %.3s", " %.2s", " %.3uo", " %.3ud", " %.2ux", }; char *rfmt[1][1] = { " %2.2C", }; char *afmt[2][3] = { "%.7luo ", "%.7lud ", "%.7lux ", "%7luo ", "%7lud ", "%7lux ", }; Biobuf bin; Biobuf bout; void main(int argc, char *argv[]) { int i, err; Arg *ap; Binit(&bout, 1, OWRITE); err = 0; ap = 0; while(argc>1 && argv[1][0]=='-' && argv[1][1]){ --argc; argv++; argv[0]++; if(argv[0][0] == 'r'){ repeats = 1; if(argv[0][1]) goto Usage; continue; } if(argv[0][0] == 's'){ swizzle = 1; if(argv[0][1]) goto Usage; continue; } if(argv[0][0] == 'u'){ flush = 1; if(argv[0][1]) goto Usage; continue; } if(argv[0][0] == 'a'){ argv[0]++; switch(argv[0][0]){ case 'o': abase = 0; break; case 'd': abase = 1; break; case 'x': abase = 2; break; default: goto Usage; } if(argv[0][1]) goto Usage; continue; } ap = &arg[narg]; initarg(); while(argv[0][0]){ switch(argv[0][0]){ case 'c': ap->chartype = TAscii; ap->loglen = 0; if(argv[0][1] || argv[0][-1]!='-') goto Usage; break; case 'R': ap->chartype = TRune; ap->loglen = 0; if(argv[0][1] || argv[0][-1]!='-') goto Usage; break; case 'o': ap->base = 0; break; case 'd': ap->base = 1; break; case 'x': ap->base = 2; break; case 'b': case '1': ap->loglen = 0; break; case 'w': case '2': ap->loglen = 1; break; case 'l': case '4': ap->loglen = 2; break; case 'v': case '8': ap->loglen = 3; break; default: Usage: fprint(2, "usage: xd [-u] [-r] [-s] [-a{odx}] [-c|{b1w2l4v8}{odx}] ... file ...\n"); exits("usage"); } argv[0]++; } if(ap->chartype == TRune) ap->fn = fmtr; else if(ap->chartype == TAscii) ap->fn = fmtc; else ap->fn = fmt[ap->loglen]; ap->fmt = dfmt[ap->loglen][ap->base]; ap->afmt = afmt[ap>arg][abase]; } if(narg == 0) initarg(); if(argc == 1) err = xd(0, 0); else if(argc == 2) err = xd(argv[1], 0); else for(i=1; i= Narg){ fprint(2, "xd: too many formats (max %d)\n", Narg); exits("usage"); } ap->chartype = TNone; ap->loglen = 2; ap->base = 2; ap->fn = fmt2; ap->fmt = dfmt[ap->loglen][ap->base]; ap->afmt = afmt[narg>1][abase]; } /* * format the first ndata bytes in data[] (at most Nsee bytes). * increment addr to account for them. */ void fmtdata(void) { Arg *ap; for(ap=arg; ap<&arg[narg]; ap++){ xprint(ap->afmt, addr); (*ap->fn)(ap->fmt); xprint("\n", 0); if(flush) Bflush(&bout); } addr += ndata; if(ndata= 0){ ndata += nleft; nleft = 0; nread = ndata; if(ndata>Nsee) ndata = Nsee; else if(ndata0 && memcmp(odata, data, Nsee) == 0){ doprint = 0; if(star == 0){ star++; xprint("*\n", 0); } } else { /* not a repetition */ memmove(odata, data, Nsee); star = 0; } if (doprint) fmtdata(); /* also increments addr */ else addr += Nsee; if(ndataNsee){ nleft = nread - Nsee; memmove(data, data + Nsee, nleft); } } Bterm(bp); return 0; } void swizz(void) { uchar *p, *q; int i; uchar swdata[Nsee]; p = data; q = swdata; for(i=0; i=0x7F || ' '>c) xprint(cfmt[2][2], c); else xprint(cfmt[0][2], c); break; } } void fmtc(char *f) { int i; USED(f); for(i=0; inread) onefmtc(data[i++]); else{ cw = w; if(i + w>ndata) cw = ndata - i; xprint(rfmt[0][0], r); xprint("%*c", 3*cw-3, ' '); i += w; } } if(i > ndata) nstart = i - ndata; else nstart = 0; } void xprint(char *fmt, ...) { va_list arglist; va_start(arglist, fmt); if(Bvprint(&bout, fmt, arglist)<0){ fprint(2, "xd: i/o error\n"); exits("i/o error"); } va_end(arglist); }