xd: add -R for runewise dump
Ported from Plan 9 2013-05-21.
b377a116d1
Closes #16.
This commit is contained in:
parent
67d25b6fb0
commit
c665ab76a8
2 changed files with 119 additions and 41 deletions
|
|
@ -66,6 +66,13 @@ but print
|
||||||
.SM ASCII
|
.SM ASCII
|
||||||
representations or C escape sequences where possible.
|
representations or C escape sequences where possible.
|
||||||
.TP
|
.TP
|
||||||
|
.B -R
|
||||||
|
Format as
|
||||||
|
.B 1x
|
||||||
|
but print
|
||||||
|
.B Rune
|
||||||
|
representations or C escape sequences where possible.
|
||||||
|
.TP
|
||||||
.BI -a style
|
.BI -a style
|
||||||
Print file addresses in the given style (and size 4).
|
Print file addresses in the given style (and size 4).
|
||||||
.TP
|
.TP
|
||||||
|
|
|
||||||
129
src/cmd/xd.c
129
src/cmd/xd.c
|
|
@ -3,8 +3,9 @@
|
||||||
#include <bio.h>
|
#include <bio.h>
|
||||||
|
|
||||||
unsigned char odata[16];
|
unsigned char odata[16];
|
||||||
unsigned char data[16];
|
unsigned char data[32];
|
||||||
int ndata;
|
int ndata;
|
||||||
|
int nread;
|
||||||
unsigned long addr;
|
unsigned long addr;
|
||||||
int repeats;
|
int repeats;
|
||||||
int swizzle;
|
int swizzle;
|
||||||
|
|
@ -12,16 +13,20 @@ int swizzle8;
|
||||||
int flush;
|
int flush;
|
||||||
int abase=2;
|
int abase=2;
|
||||||
int xd(char *, int);
|
int xd(char *, int);
|
||||||
void xprint(char *, ulong);
|
void xprint(char *, ...);
|
||||||
void initarg(void), swizz(void), swizz8(void);
|
void initarg(void), swizz(void), swizz8(void);
|
||||||
enum{
|
enum{
|
||||||
Narg=10
|
Narg=10,
|
||||||
|
|
||||||
|
TNone=0,
|
||||||
|
TAscii,
|
||||||
|
TRune,
|
||||||
};
|
};
|
||||||
typedef struct Arg Arg;
|
typedef struct Arg Arg;
|
||||||
typedef void fmtfn(char *);
|
typedef void fmtfn(char *);
|
||||||
struct Arg
|
struct Arg
|
||||||
{
|
{
|
||||||
int ascii; /* 0==none, 1==ascii */
|
int chartype; /* TNone, TAscii, TRunes */
|
||||||
int loglen; /* 0==1, 1==2, 2==4, 3==8 */
|
int loglen; /* 0==1, 1==2, 2==4, 3==8 */
|
||||||
int base; /* 0==8, 1==10, 2==16 */
|
int base; /* 0==8, 1==10, 2==16 */
|
||||||
fmtfn *fn; /* function to call with data */
|
fmtfn *fn; /* function to call with data */
|
||||||
|
|
@ -30,7 +35,7 @@ struct Arg
|
||||||
}arg[Narg];
|
}arg[Narg];
|
||||||
int narg;
|
int narg;
|
||||||
|
|
||||||
fmtfn fmt0, fmt1, fmt2, fmt3, fmtc;
|
fmtfn fmt0, fmt1, fmt2, fmt3, fmtc, fmtr;
|
||||||
fmtfn *fmt[4] = {
|
fmtfn *fmt[4] = {
|
||||||
fmt0,
|
fmt0,
|
||||||
fmt1,
|
fmt1,
|
||||||
|
|
@ -51,6 +56,10 @@ char *cfmt[3][3] = {
|
||||||
" %.3uo", " %.3ud", " %.2ux",
|
" %.3uo", " %.3ud", " %.2ux",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
char *rfmt[1][1] = {
|
||||||
|
" %2.2C",
|
||||||
|
};
|
||||||
|
|
||||||
char *afmt[2][3] = {
|
char *afmt[2][3] = {
|
||||||
"%.7luo ", "%.7lud ", "%.7lux ",
|
"%.7luo ", "%.7lud ", "%.7lux ",
|
||||||
"%7luo ", "%7lud ", "%7lux ",
|
"%7luo ", "%7lud ", "%7lux ",
|
||||||
|
|
@ -120,7 +129,13 @@ main(int argc, char *argv[])
|
||||||
while(argv[0][0]){
|
while(argv[0][0]){
|
||||||
switch(argv[0][0]){
|
switch(argv[0][0]){
|
||||||
case 'c':
|
case 'c':
|
||||||
ap->ascii = 1;
|
ap->chartype = TAscii;
|
||||||
|
ap->loglen = 0;
|
||||||
|
if(argv[0][1] || argv[0][-1]!='-')
|
||||||
|
goto Usage;
|
||||||
|
break;
|
||||||
|
case 'R':
|
||||||
|
ap->chartype = TRune;
|
||||||
ap->loglen = 0;
|
ap->loglen = 0;
|
||||||
if(argv[0][1] || argv[0][-1]!='-')
|
if(argv[0][1] || argv[0][-1]!='-')
|
||||||
goto Usage;
|
goto Usage;
|
||||||
|
|
@ -157,7 +172,9 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
argv[0]++;
|
argv[0]++;
|
||||||
}
|
}
|
||||||
if(ap->ascii)
|
if(ap->chartype == TRune)
|
||||||
|
ap->fn = fmtr;
|
||||||
|
else if(ap->chartype == TAscii)
|
||||||
ap->fn = fmtc;
|
ap->fn = fmtc;
|
||||||
else
|
else
|
||||||
ap->fn = fmt[ap->loglen];
|
ap->fn = fmt[ap->loglen];
|
||||||
|
|
@ -185,7 +202,7 @@ initarg(void)
|
||||||
fprint(2, "xd: too many formats (max %d)\n", Narg);
|
fprint(2, "xd: too many formats (max %d)\n", Narg);
|
||||||
exits("usage");
|
exits("usage");
|
||||||
}
|
}
|
||||||
ap->ascii = 0;
|
ap->chartype = TNone;
|
||||||
ap->loglen = 2;
|
ap->loglen = 2;
|
||||||
ap->base = 2;
|
ap->base = 2;
|
||||||
ap->fn = fmt2;
|
ap->fn = fmt2;
|
||||||
|
|
@ -197,7 +214,7 @@ int
|
||||||
xd(char *name, int title)
|
xd(char *name, int title)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
int i, star;
|
int i, star, nsee, nleft;
|
||||||
Arg *ap;
|
Arg *ap;
|
||||||
Biobuf *bp;
|
Biobuf *bp;
|
||||||
|
|
||||||
|
|
@ -216,21 +233,29 @@ xd(char *name, int title)
|
||||||
xprint("%s\n", (long)name);
|
xprint("%s\n", (long)name);
|
||||||
addr = 0;
|
addr = 0;
|
||||||
star = 0;
|
star = 0;
|
||||||
while((ndata=Bread(bp, data, 16)) >= 0){
|
nsee = 16;
|
||||||
if(ndata < 16)
|
nleft = 0;
|
||||||
for(i=ndata; i<16; i++)
|
/* read 32 but see only 16 so that runes are happy */
|
||||||
|
while((ndata=Bread(bp, data + nleft, 32 - nleft)) >= 0){
|
||||||
|
ndata += nleft;
|
||||||
|
nleft = 0;
|
||||||
|
nread = ndata;
|
||||||
|
if(ndata>nsee)
|
||||||
|
ndata = nsee;
|
||||||
|
else if(ndata<nsee)
|
||||||
|
for(i=ndata; i<nsee; i++)
|
||||||
data[i] = 0;
|
data[i] = 0;
|
||||||
if(swizzle)
|
if(swizzle)
|
||||||
swizz();
|
swizz();
|
||||||
if(swizzle8)
|
if(swizzle8)
|
||||||
swizz8();
|
swizz8();
|
||||||
if(ndata==16 && repeats){
|
if(ndata==nsee && repeats){
|
||||||
if(addr>0 && data[0]==odata[0]){
|
if(addr>0 && data[0]==odata[0]){
|
||||||
for(i=1; i<16; i++)
|
for(i=1; i<nsee; i++)
|
||||||
if(data[i] != odata[i])
|
if(data[i] != odata[i])
|
||||||
break;
|
break;
|
||||||
if(i == 16){
|
if(i == nsee){
|
||||||
addr += 16;
|
addr += nsee;
|
||||||
if(star == 0){
|
if(star == 0){
|
||||||
star++;
|
star++;
|
||||||
xprint("*\n", 0);
|
xprint("*\n", 0);
|
||||||
|
|
@ -238,7 +263,7 @@ xd(char *name, int title)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(i=0; i<16; i++)
|
for(i=0; i<nsee; i++)
|
||||||
odata[i] = data[i];
|
odata[i] = data[i];
|
||||||
star = 0;
|
star = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -250,13 +275,17 @@ xd(char *name, int title)
|
||||||
Bflush(&bout);
|
Bflush(&bout);
|
||||||
}
|
}
|
||||||
addr += ndata;
|
addr += ndata;
|
||||||
if(ndata<16){
|
if(ndata<nsee){
|
||||||
xprint(afmt[0][abase], addr);
|
xprint(afmt[0][abase], addr);
|
||||||
xprint("\n", 0);
|
xprint("\n", 0);
|
||||||
if(flush)
|
if(flush)
|
||||||
Bflush(&bout);
|
Bflush(&bout);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if(nread>nsee){
|
||||||
|
nleft = nread - nsee;
|
||||||
|
memmove(data, data + nsee, nleft);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Bterm(bp);
|
Bterm(bp);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -353,13 +382,9 @@ fmt3(char *f)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
fmtc(char *f)
|
onefmtc(uchar c)
|
||||||
{
|
{
|
||||||
int i;
|
switch(c){
|
||||||
|
|
||||||
USED(f);
|
|
||||||
for(i=0; i<ndata; i++)
|
|
||||||
switch(data[i]){
|
|
||||||
case '\t':
|
case '\t':
|
||||||
xprint(cfmt[1][2], (long)"\\t");
|
xprint(cfmt[1][2], (long)"\\t");
|
||||||
break;
|
break;
|
||||||
|
|
@ -373,19 +398,65 @@ fmtc(char *f)
|
||||||
xprint(cfmt[1][2], (long)"\\b");
|
xprint(cfmt[1][2], (long)"\\b");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if(data[i]>=0x7F || ' '>data[i])
|
if(c>=0x7F || ' '>c)
|
||||||
xprint(cfmt[2][2], data[i]);
|
xprint(cfmt[2][2], c);
|
||||||
else
|
else
|
||||||
xprint(cfmt[0][2], data[i]);
|
xprint(cfmt[0][2], c);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
xprint(char *fmt, ulong d)
|
fmtc(char *f)
|
||||||
{
|
{
|
||||||
if(Bprint(&bout, fmt, d)<0){
|
int i;
|
||||||
|
|
||||||
|
USED(f);
|
||||||
|
for(i=0; i<ndata; i++)
|
||||||
|
onefmtc(data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
fmtr(char *f)
|
||||||
|
{
|
||||||
|
int i, w, cw;
|
||||||
|
Rune r;
|
||||||
|
static int nstart;
|
||||||
|
|
||||||
|
USED(f);
|
||||||
|
if(nstart)
|
||||||
|
xprint("%*c", 3*nstart, ' ');
|
||||||
|
for(i=nstart; i<ndata; )
|
||||||
|
if(data[i] < Runeself)
|
||||||
|
onefmtc(data[i++]);
|
||||||
|
else{
|
||||||
|
w = chartorune(&r, (char *)data+i);
|
||||||
|
if(w == 1 || i + w>nread)
|
||||||
|
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");
|
fprint(2, "xd: i/o error\n");
|
||||||
exits("i/o error");
|
exits("i/o error");
|
||||||
}
|
}
|
||||||
|
va_end(arglist);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue