xd.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. uint8_t odata[16];
  13. uint8_t data[32];
  14. int ndata;
  15. int nread;
  16. uint32_t addr;
  17. int repeats;
  18. int swizzle;
  19. int flush;
  20. int abase=2;
  21. int xd(char *, int);
  22. void xprint(char *, ...);
  23. void initarg(void), swizz(void);
  24. enum{
  25. Narg=10,
  26. TNone=0,
  27. TAscii,
  28. TRune,
  29. };
  30. typedef struct Arg Arg;
  31. typedef void fmtfn(char *);
  32. struct Arg
  33. {
  34. int chartype; /* TNone, TAscii, TRunes */
  35. int loglen; /* 0==1, 1==2, 2==4, 3==8 */
  36. int base; /* 0==8, 1==10, 2==16 */
  37. fmtfn *fn; /* function to call with data */
  38. char *afmt; /* format to use to print address */
  39. char *fmt; /* format to use to print data */
  40. }arg[Narg];
  41. int narg;
  42. fmtfn fmt0, fmt1, fmt2, fmt3, fmtc, fmtr;
  43. fmtfn *fmt[4] = {
  44. fmt0,
  45. fmt1,
  46. fmt2,
  47. fmt3
  48. };
  49. char *dfmt[4][3] = {
  50. " %.3uo", " %.3ud", " %.2ux",
  51. " %.6uo", " %.5ud", " %.4ux",
  52. " %.11luo", " %.10lud", " %.8lux",
  53. " %.22lluo", " %.20llud", " %.16llux",
  54. };
  55. char *cfmt[3][3] = {
  56. " %c", " %c", " %c",
  57. " %.3s", " %.3s", " %.2s",
  58. " %.3uo", " %.3ud", " %.2ux",
  59. };
  60. char *rfmt[1][1] = {
  61. " %2.2C",
  62. };
  63. char *afmt[2][3] = {
  64. "%.7luo ", "%.7lud ", "%.7lux ",
  65. "%7luo ", "%7lud ", "%7lux ",
  66. };
  67. Biobuf bin;
  68. Biobuf bout;
  69. void
  70. main(int argc, char *argv[])
  71. {
  72. int i, err;
  73. Arg *ap;
  74. Binit(&bout, 1, OWRITE);
  75. err = 0;
  76. ap = 0;
  77. while(argc>1 && argv[1][0]=='-' && argv[1][1]){
  78. --argc;
  79. argv++;
  80. argv[0]++;
  81. if(argv[0][0] == 'r'){
  82. repeats = 1;
  83. if(argv[0][1])
  84. goto Usage;
  85. continue;
  86. }
  87. if(argv[0][0] == 's'){
  88. swizzle = 1;
  89. if(argv[0][1])
  90. goto Usage;
  91. continue;
  92. }
  93. if(argv[0][0] == 'u'){
  94. flush = 1;
  95. if(argv[0][1])
  96. goto Usage;
  97. continue;
  98. }
  99. if(argv[0][0] == 'a'){
  100. argv[0]++;
  101. switch(argv[0][0]){
  102. case 'o':
  103. abase = 0;
  104. break;
  105. case 'd':
  106. abase = 1;
  107. break;
  108. case 'x':
  109. abase = 2;
  110. break;
  111. default:
  112. goto Usage;
  113. }
  114. if(argv[0][1])
  115. goto Usage;
  116. continue;
  117. }
  118. ap = &arg[narg];
  119. initarg();
  120. while(argv[0][0]){
  121. switch(argv[0][0]){
  122. case 'c':
  123. ap->chartype = TAscii;
  124. ap->loglen = 0;
  125. if(argv[0][1] || argv[0][-1]!='-')
  126. goto Usage;
  127. break;
  128. case 'R':
  129. ap->chartype = TRune;
  130. ap->loglen = 0;
  131. if(argv[0][1] || argv[0][-1]!='-')
  132. goto Usage;
  133. break;
  134. case 'o':
  135. ap->base = 0;
  136. break;
  137. case 'd':
  138. ap->base = 1;
  139. break;
  140. case 'x':
  141. ap->base = 2;
  142. break;
  143. case 'b':
  144. case '1':
  145. ap->loglen = 0;
  146. break;
  147. case 'w':
  148. case '2':
  149. ap->loglen = 1;
  150. break;
  151. case 'l':
  152. case '4':
  153. ap->loglen = 2;
  154. break;
  155. case 'v':
  156. case '8':
  157. ap->loglen = 3;
  158. break;
  159. default:
  160. Usage:
  161. fprint(2, "usage: xd [-u] [-r] [-s] [-a{odx}] [-c|{b1w2l4v8}{odx}] ... file ...\n");
  162. exits("usage");
  163. }
  164. argv[0]++;
  165. }
  166. if(ap->chartype == TRune)
  167. ap->fn = fmtr;
  168. else if(ap->chartype == TAscii)
  169. ap->fn = fmtc;
  170. else
  171. ap->fn = fmt[ap->loglen];
  172. ap->fmt = dfmt[ap->loglen][ap->base];
  173. ap->afmt = afmt[ap>arg][abase];
  174. }
  175. if(narg == 0)
  176. initarg();
  177. if(argc == 1)
  178. err = xd(0, 0);
  179. else if(argc == 2)
  180. err = xd(argv[1], 0);
  181. else for(i=1; i<argc; i++)
  182. err |= xd(argv[i], 1);
  183. exits(err? "error" : 0);
  184. }
  185. void
  186. initarg(void)
  187. {
  188. Arg *ap;
  189. ap = &arg[narg++];
  190. if(narg >= Narg){
  191. fprint(2, "xd: too many formats (max %d)\n", Narg);
  192. exits("usage");
  193. }
  194. ap->chartype = TNone;
  195. ap->loglen = 2;
  196. ap->base = 2;
  197. ap->fn = fmt2;
  198. ap->fmt = dfmt[ap->loglen][ap->base];
  199. ap->afmt = afmt[narg>1][abase];
  200. }
  201. int
  202. xd(char *name, int title)
  203. {
  204. int fd;
  205. int i, star, nsee, nleft;
  206. Arg *ap;
  207. Biobuf *bp;
  208. fd = 0;
  209. if(name){
  210. bp = Bopen(name, OREAD);
  211. if(bp == 0){
  212. fprint(2, "xd: can't open %s\n", name);
  213. return 1;
  214. }
  215. }else{
  216. bp = &bin;
  217. Binit(bp, fd, OREAD);
  218. }
  219. if(title)
  220. xprint("%s\n", name);
  221. addr = 0;
  222. star = 0;
  223. nsee = 16;
  224. nleft = 0;
  225. /* read 32 but see only 16 so that runes are happy */
  226. while((ndata=Bread(bp, data + nleft, 32 - nleft)) >= 0){
  227. ndata += nleft;
  228. nleft = 0;
  229. nread = ndata;
  230. if(ndata>nsee)
  231. ndata = nsee;
  232. else if(ndata<nsee)
  233. for(i=ndata; i<nsee; i++)
  234. data[i] = 0;
  235. if(swizzle)
  236. swizz();
  237. if(ndata==nsee && repeats){
  238. if(addr>0 && data[0]==odata[0]){
  239. for(i=1; i<nsee; i++)
  240. if(data[i] != odata[i])
  241. break;
  242. if(i == nsee){
  243. addr += nsee;
  244. if(star == 0){
  245. star++;
  246. xprint("*\n", 0);
  247. }
  248. continue;
  249. }
  250. }
  251. for(i=0; i<nsee; i++)
  252. odata[i] = data[i];
  253. star = 0;
  254. }
  255. for(ap=arg; ap<&arg[narg]; ap++){
  256. xprint(ap->afmt, addr);
  257. (*ap->fn)(ap->fmt);
  258. xprint("\n", 0);
  259. if(flush)
  260. Bflush(&bout);
  261. }
  262. addr += ndata;
  263. if(ndata<nsee){
  264. xprint(afmt[0][abase], addr);
  265. xprint("\n", 0);
  266. if(flush)
  267. Bflush(&bout);
  268. break;
  269. }
  270. if(nread>nsee){
  271. nleft = nread - nsee;
  272. memmove(data, data + nsee, nleft);
  273. }
  274. }
  275. Bterm(bp);
  276. return 0;
  277. }
  278. void
  279. swizz(void)
  280. {
  281. uint8_t *p, *q;
  282. int i;
  283. uint8_t swdata[16];
  284. p = data;
  285. q = swdata;
  286. for(i=0; i<16; i++)
  287. *q++ = *p++;
  288. p = data;
  289. q = swdata;
  290. for(i=0; i<4; i++){
  291. p[0] = q[3];
  292. p[1] = q[2];
  293. p[2] = q[1];
  294. p[3] = q[0];
  295. p += 4;
  296. q += 4;
  297. }
  298. }
  299. void
  300. fmt0(char *f)
  301. {
  302. int i;
  303. for(i=0; i<ndata; i++)
  304. xprint(f, data[i]);
  305. }
  306. void
  307. fmt1(char *f)
  308. {
  309. int i;
  310. for(i=0; i<ndata; i+=sizeof(ushort))
  311. xprint(f, (data[i]<<8)|data[i+1]);
  312. }
  313. void
  314. fmt2(char *f)
  315. {
  316. int i;
  317. for(i=0; i<ndata; i+=sizeof(uint32_t))
  318. xprint(f, (data[i]<<24)|(data[i+1]<<16)|(data[i+2]<<8)|data[i+3]);
  319. }
  320. void
  321. fmt3(char *f)
  322. {
  323. int i;
  324. uint64_t v;
  325. for(i=0; i<ndata; i+=sizeof(uint64_t)){
  326. v = (data[i]<<24)|(data[i+1]<<16)|(data[i+2]<<8)|data[i+3];
  327. v <<= 32;
  328. v |= (data[i+4]<<24)|(data[i+1+4]<<16)|(data[i+2+4]<<8)|data[i+3+4];
  329. if(Bprint(&bout, f, v)<0){
  330. fprint(2, "xd: i/o error\n");
  331. exits("i/o error");
  332. }
  333. }
  334. }
  335. void
  336. onefmtc(uint8_t c)
  337. {
  338. switch(c){
  339. case '\t':
  340. xprint(cfmt[1][2], "\\t");
  341. break;
  342. case '\r':
  343. xprint(cfmt[1][2], "\\r");
  344. break;
  345. case '\n':
  346. xprint(cfmt[1][2], "\\n");
  347. break;
  348. case '\b':
  349. xprint(cfmt[1][2], "\\b");
  350. break;
  351. default:
  352. if(c>=0x7F || ' '>c)
  353. xprint(cfmt[2][2], c);
  354. else
  355. xprint(cfmt[0][2], c);
  356. break;
  357. }
  358. }
  359. void
  360. fmtc(char *f)
  361. {
  362. int i;
  363. USED(f);
  364. for(i=0; i<ndata; i++)
  365. onefmtc(data[i]);
  366. }
  367. void
  368. fmtr(char *f)
  369. {
  370. int i, w, cw;
  371. Rune r;
  372. static int nstart;
  373. USED(f);
  374. if(nstart)
  375. xprint("%*c", 3*nstart, ' ');
  376. for(i=nstart; i<ndata; )
  377. if(data[i] < Runeself)
  378. onefmtc(data[i++]);
  379. else{
  380. w = chartorune(&r, (char *)data+i);
  381. if(w == 1 || i + w>nread)
  382. onefmtc(data[i++]);
  383. else{
  384. cw = w;
  385. if(i + w>ndata)
  386. cw = ndata - i;
  387. xprint(rfmt[0][0], r);
  388. xprint("%*c", 3*cw-3, ' ');
  389. i += w;
  390. }
  391. }
  392. if(i > ndata)
  393. nstart = i - ndata;
  394. else
  395. nstart = 0;
  396. }
  397. void
  398. xprint(char *fmt, ...)
  399. {
  400. va_list arglist;
  401. va_start(arglist, fmt);
  402. if(Bvprint(&bout, fmt, arglist)<0){
  403. fprint(2, "xd: i/o error\n");
  404. exits("i/o error");
  405. }
  406. va_end(arglist);
  407. }