dump.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Support code for the hexdump and od applets,
  4. * based on code from util-linux v 2.11l
  5. *
  6. * Copyright (c) 1989
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  10. *
  11. * Original copyright notice is retained at the end of this file.
  12. */
  13. #include "libbb.h"
  14. #include "dump.h"
  15. static const char index_str[] ALIGN1 = ".#-+ 0123456789";
  16. static const char size_conv_str[] ALIGN1 =
  17. "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
  18. static const char lcc[] ALIGN1 = "diouxX";
  19. typedef struct priv_dumper_t {
  20. dumper_t pub;
  21. char **argv;
  22. FU *endfu;
  23. off_t savaddress; /* saved address/offset in stream */
  24. off_t eaddress; /* end address */
  25. off_t address; /* address/offset in stream */
  26. int blocksize;
  27. smallint exitval; /* final exit value */
  28. /* former statics */
  29. smallint next__done;
  30. smallint get__ateof; // = 1;
  31. unsigned char *get__curp;
  32. unsigned char *get__savp;
  33. } priv_dumper_t;
  34. dumper_t* FAST_FUNC alloc_dumper(void)
  35. {
  36. priv_dumper_t *dumper = xzalloc(sizeof(*dumper));
  37. dumper->pub.dump_length = -1;
  38. dumper->pub.dump_vflag = FIRST;
  39. dumper->get__ateof = 1;
  40. return &dumper->pub;
  41. }
  42. static NOINLINE int bb_dump_size(FS *fs)
  43. {
  44. FU *fu;
  45. int bcnt, cur_size;
  46. char *fmt;
  47. const char *p;
  48. int prec;
  49. /* figure out the data block bb_dump_size needed for each format unit */
  50. for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
  51. if (fu->bcnt) {
  52. cur_size += fu->bcnt * fu->reps;
  53. continue;
  54. }
  55. for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
  56. if (*fmt != '%')
  57. continue;
  58. /*
  59. * skip any special chars -- save precision in
  60. * case it's a %s format.
  61. */
  62. while (strchr(index_str + 1, *++fmt));
  63. if (*fmt == '.' && isdigit(*++fmt)) {
  64. prec = atoi(fmt);
  65. while (isdigit(*++fmt))
  66. continue;
  67. }
  68. p = strchr(size_conv_str + 12, *fmt);
  69. if (!p) {
  70. if (*fmt == 's') {
  71. bcnt += prec;
  72. } else if (*fmt == '_') {
  73. ++fmt;
  74. if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
  75. bcnt += 1;
  76. }
  77. }
  78. } else {
  79. bcnt += size_conv_str[p - (size_conv_str + 12)];
  80. }
  81. }
  82. cur_size += bcnt * fu->reps;
  83. }
  84. return cur_size;
  85. }
  86. static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
  87. {
  88. enum { NOTOKAY, USEBCNT, USEPREC } sokay;
  89. PR *pr, **nextpr = NULL;
  90. FU *fu;
  91. char *p1, *p2, *p3;
  92. char savech, *fmtp;
  93. const char *byte_count_str;
  94. int nconv, prec = 0;
  95. for (fu = fs->nextfu; fu; fu = fu->nextfu) {
  96. /*
  97. * break each format unit into print units; each
  98. * conversion character gets its own.
  99. */
  100. for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
  101. /* NOSTRICT */
  102. /* DBU:[dvae@cray.com] zalloc so that forward ptrs start out NULL*/
  103. pr = xzalloc(sizeof(PR));
  104. if (!fu->nextpr)
  105. fu->nextpr = pr;
  106. /* ignore nextpr -- its unused inside the loop and is
  107. * uninitialized 1st time through.
  108. */
  109. /* skip preceding text and up to the next % sign */
  110. for (p1 = fmtp; *p1 && *p1 != '%'; ++p1)
  111. continue;
  112. /* only text in the string */
  113. if (!*p1) {
  114. pr->fmt = fmtp;
  115. pr->flags = F_TEXT;
  116. break;
  117. }
  118. /*
  119. * get precision for %s -- if have a byte count, don't
  120. * need it.
  121. */
  122. if (fu->bcnt) {
  123. sokay = USEBCNT;
  124. /* skip to conversion character */
  125. for (++p1; strchr(index_str, *p1); ++p1)
  126. continue;
  127. } else {
  128. /* skip any special chars, field width */
  129. while (strchr(index_str + 1, *++p1))
  130. continue;
  131. if (*p1 == '.' && isdigit(*++p1)) {
  132. sokay = USEPREC;
  133. prec = atoi(p1);
  134. while (isdigit(*++p1))
  135. continue;
  136. } else
  137. sokay = NOTOKAY;
  138. }
  139. p2 = p1 + 1; /* set end pointer */
  140. /*
  141. * figure out the byte count for each conversion;
  142. * rewrite the format as necessary, set up blank-
  143. * pbb_dump_adding for end of data.
  144. */
  145. if (*p1 == 'c') {
  146. pr->flags = F_CHAR;
  147. DO_BYTE_COUNT_1:
  148. byte_count_str = "\001";
  149. DO_BYTE_COUNT:
  150. if (fu->bcnt) {
  151. do {
  152. if (fu->bcnt == *byte_count_str) {
  153. break;
  154. }
  155. } while (*++byte_count_str);
  156. }
  157. /* Unlike the original, output the remainder of the format string. */
  158. if (!*byte_count_str) {
  159. bb_error_msg_and_die("bad byte count for conversion character %s", p1);
  160. }
  161. pr->bcnt = *byte_count_str;
  162. } else if (*p1 == 'l') {
  163. ++p2;
  164. ++p1;
  165. DO_INT_CONV:
  166. {
  167. const char *e;
  168. e = strchr(lcc, *p1);
  169. if (!e) {
  170. goto DO_BAD_CONV_CHAR;
  171. }
  172. pr->flags = F_INT;
  173. if (e > lcc + 1) {
  174. pr->flags = F_UINT;
  175. }
  176. byte_count_str = "\004\002\001";
  177. goto DO_BYTE_COUNT;
  178. }
  179. /* NOTREACHED */
  180. } else if (strchr(lcc, *p1)) {
  181. goto DO_INT_CONV;
  182. } else if (strchr("eEfgG", *p1)) {
  183. pr->flags = F_DBL;
  184. byte_count_str = "\010\004";
  185. goto DO_BYTE_COUNT;
  186. } else if (*p1 == 's') {
  187. pr->flags = F_STR;
  188. if (sokay == USEBCNT) {
  189. pr->bcnt = fu->bcnt;
  190. } else if (sokay == USEPREC) {
  191. pr->bcnt = prec;
  192. } else { /* NOTOKAY */
  193. bb_error_msg_and_die("%%s requires a precision or a byte count");
  194. }
  195. } else if (*p1 == '_') {
  196. ++p2;
  197. switch (p1[1]) {
  198. case 'A':
  199. dumper->endfu = fu;
  200. fu->flags |= F_IGNORE;
  201. /* FALLTHROUGH */
  202. case 'a':
  203. pr->flags = F_ADDRESS;
  204. ++p2;
  205. if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
  206. goto DO_BAD_CONV_CHAR;
  207. }
  208. *p1 = p1[2];
  209. break;
  210. case 'c':
  211. pr->flags = F_C;
  212. /* *p1 = 'c'; set in conv_c */
  213. goto DO_BYTE_COUNT_1;
  214. case 'p':
  215. pr->flags = F_P;
  216. *p1 = 'c';
  217. goto DO_BYTE_COUNT_1;
  218. case 'u':
  219. pr->flags = F_U;
  220. /* *p1 = 'c'; set in conv_u */
  221. goto DO_BYTE_COUNT_1;
  222. default:
  223. goto DO_BAD_CONV_CHAR;
  224. }
  225. } else {
  226. DO_BAD_CONV_CHAR:
  227. bb_error_msg_and_die("bad conversion character %%%s", p1);
  228. }
  229. /*
  230. * copy to PR format string, set conversion character
  231. * pointer, update original.
  232. */
  233. savech = *p2;
  234. p1[1] = '\0';
  235. pr->fmt = xstrdup(fmtp);
  236. *p2 = savech;
  237. //Too early! xrealloc can move pr->fmt!
  238. //pr->cchar = pr->fmt + (p1 - fmtp);
  239. /* DBU:[dave@cray.com] w/o this, trailing fmt text, space is lost.
  240. * Skip subsequent text and up to the next % sign and tack the
  241. * additional text onto fmt: eg. if fmt is "%x is a HEX number",
  242. * we lose the " is a HEX number" part of fmt.
  243. */
  244. for (p3 = p2; *p3 && *p3 != '%'; p3++)
  245. continue;
  246. if (p3 > p2) {
  247. savech = *p3;
  248. *p3 = '\0';
  249. pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt) + (p3-p2) + 1);
  250. strcat(pr->fmt, p2);
  251. *p3 = savech;
  252. p2 = p3;
  253. }
  254. pr->cchar = pr->fmt + (p1 - fmtp);
  255. fmtp = p2;
  256. /* only one conversion character if byte count */
  257. if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
  258. bb_error_msg_and_die("byte count with multiple conversion characters");
  259. }
  260. }
  261. /*
  262. * if format unit byte count not specified, figure it out
  263. * so can adjust rep count later.
  264. */
  265. if (!fu->bcnt)
  266. for (pr = fu->nextpr; pr; pr = pr->nextpr)
  267. fu->bcnt += pr->bcnt;
  268. }
  269. /*
  270. * if the format string interprets any data at all, and it's
  271. * not the same as the blocksize, and its last format unit
  272. * interprets any data at all, and has no iteration count,
  273. * repeat it as necessary.
  274. *
  275. * if, rep count is greater than 1, no trailing whitespace
  276. * gets output from the last iteration of the format unit.
  277. */
  278. for (fu = fs->nextfu; fu; fu = fu->nextfu) {
  279. if (!fu->nextfu && fs->bcnt < dumper->blocksize
  280. && !(fu->flags & F_SETREP) && fu->bcnt
  281. ) {
  282. fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
  283. }
  284. if (fu->reps > 1) {
  285. for (pr = fu->nextpr;; pr = pr->nextpr)
  286. if (!pr->nextpr)
  287. break;
  288. for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
  289. p2 = isspace(*p1) ? p1 : NULL;
  290. if (p2)
  291. pr->nospace = p2;
  292. }
  293. if (!fu->nextfu)
  294. break;
  295. }
  296. }
  297. static void do_skip(priv_dumper_t *dumper, const char *fname, int statok)
  298. {
  299. struct stat sbuf;
  300. if (statok) {
  301. if (fstat(STDIN_FILENO, &sbuf)) {
  302. bb_simple_perror_msg_and_die(fname);
  303. }
  304. if (!(S_ISCHR(sbuf.st_mode) || S_ISBLK(sbuf.st_mode) || S_ISFIFO(sbuf.st_mode))
  305. && dumper->pub.dump_skip >= sbuf.st_size
  306. ) {
  307. /* If bb_dump_size valid and pub.dump_skip >= size */
  308. dumper->pub.dump_skip -= sbuf.st_size;
  309. dumper->address += sbuf.st_size;
  310. return;
  311. }
  312. }
  313. if (fseek(stdin, dumper->pub.dump_skip, SEEK_SET)) {
  314. bb_simple_perror_msg_and_die(fname);
  315. }
  316. dumper->address += dumper->pub.dump_skip;
  317. dumper->savaddress = dumper->address;
  318. dumper->pub.dump_skip = 0;
  319. }
  320. static NOINLINE int next(priv_dumper_t *dumper)
  321. {
  322. int statok;
  323. for (;;) {
  324. if (*dumper->argv) {
  325. if (!(freopen(*dumper->argv, "r", stdin))) {
  326. bb_simple_perror_msg(*dumper->argv);
  327. dumper->exitval = 1;
  328. ++dumper->argv;
  329. continue;
  330. }
  331. dumper->next__done = statok = 1;
  332. } else {
  333. if (dumper->next__done)
  334. return 0;
  335. dumper->next__done = 1;
  336. statok = 0;
  337. }
  338. if (dumper->pub.dump_skip)
  339. do_skip(dumper, statok ? *dumper->argv : "stdin", statok);
  340. if (*dumper->argv)
  341. ++dumper->argv;
  342. if (!dumper->pub.dump_skip)
  343. return 1;
  344. }
  345. /* NOTREACHED */
  346. }
  347. static unsigned char *get(priv_dumper_t *dumper)
  348. {
  349. int n;
  350. int need, nread;
  351. int blocksize = dumper->blocksize;
  352. if (!dumper->get__curp) {
  353. dumper->address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
  354. dumper->get__curp = xmalloc(blocksize);
  355. dumper->get__savp = xzalloc(blocksize); /* need to be initialized */
  356. } else {
  357. unsigned char *tmp = dumper->get__curp;
  358. dumper->get__curp = dumper->get__savp;
  359. dumper->get__savp = tmp;
  360. dumper->savaddress += blocksize;
  361. dumper->address = dumper->savaddress;
  362. }
  363. need = blocksize;
  364. nread = 0;
  365. while (1) {
  366. /*
  367. * if read the right number of bytes, or at EOF for one file,
  368. * and no other files are available, zero-pad the rest of the
  369. * block and set the end flag.
  370. */
  371. if (!dumper->pub.dump_length || (dumper->get__ateof && !next(dumper))) {
  372. if (need == blocksize) {
  373. return NULL;
  374. }
  375. if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) {
  376. if (dumper->pub.dump_vflag != DUP) {
  377. puts("*");
  378. }
  379. return NULL;
  380. }
  381. memset(dumper->get__curp + nread, 0, need);
  382. dumper->eaddress = dumper->address + nread;
  383. return dumper->get__curp;
  384. }
  385. n = fread(dumper->get__curp + nread, sizeof(unsigned char),
  386. dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
  387. if (!n) {
  388. if (ferror(stdin)) {
  389. bb_simple_perror_msg(dumper->argv[-1]);
  390. }
  391. dumper->get__ateof = 1;
  392. continue;
  393. }
  394. dumper->get__ateof = 0;
  395. if (dumper->pub.dump_length != -1) {
  396. dumper->pub.dump_length -= n;
  397. }
  398. need -= n;
  399. if (!need) {
  400. if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST
  401. || memcmp(dumper->get__curp, dumper->get__savp, blocksize)
  402. ) {
  403. if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
  404. dumper->pub.dump_vflag = WAIT;
  405. }
  406. return dumper->get__curp;
  407. }
  408. if (dumper->pub.dump_vflag == WAIT) {
  409. puts("*");
  410. }
  411. dumper->pub.dump_vflag = DUP;
  412. dumper->savaddress += blocksize;
  413. dumper->address = dumper->savaddress;
  414. need = blocksize;
  415. nread = 0;
  416. } else {
  417. nread += n;
  418. }
  419. }
  420. }
  421. static void bpad(PR *pr)
  422. {
  423. char *p1, *p2;
  424. /*
  425. * remove all conversion flags; '-' is the only one valid
  426. * with %s, and it's not useful here.
  427. */
  428. pr->flags = F_BPAD;
  429. *pr->cchar = 's';
  430. for (p1 = pr->fmt; *p1 != '%'; ++p1)
  431. continue;
  432. for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
  433. if (pr->nospace)
  434. pr->nospace--;
  435. while ((*p2++ = *p1++) != 0)
  436. continue;
  437. }
  438. static const char conv_str[] ALIGN1 =
  439. "\0\\0\0"
  440. "\007\\a\0" /* \a */
  441. "\b\\b\0"
  442. "\f\\b\0"
  443. "\n\\n\0"
  444. "\r\\r\0"
  445. "\t\\t\0"
  446. "\v\\v\0"
  447. ;
  448. static void conv_c(PR *pr, unsigned char *p)
  449. {
  450. const char *str = conv_str;
  451. char buf[10];
  452. do {
  453. if (*p == *str) {
  454. ++str;
  455. goto strpr;
  456. }
  457. str += 4;
  458. } while (*str);
  459. if (isprint_asciionly(*p)) {
  460. *pr->cchar = 'c';
  461. printf(pr->fmt, *p);
  462. } else {
  463. sprintf(buf, "%03o", (int) *p);
  464. str = buf;
  465. strpr:
  466. *pr->cchar = 's';
  467. printf(pr->fmt, str);
  468. }
  469. }
  470. static void conv_u(PR *pr, unsigned char *p)
  471. {
  472. static const char list[] ALIGN1 =
  473. "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
  474. "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
  475. "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
  476. "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
  477. /* od used nl, not lf */
  478. if (*p <= 0x1f) {
  479. *pr->cchar = 's';
  480. printf(pr->fmt, list + (4 * (int)*p));
  481. } else if (*p == 0x7f) {
  482. *pr->cchar = 's';
  483. printf(pr->fmt, "del");
  484. } else if (*p < 0x7f) { /* isprint() */
  485. *pr->cchar = 'c';
  486. printf(pr->fmt, *p);
  487. } else {
  488. *pr->cchar = 'x';
  489. printf(pr->fmt, (int) *p);
  490. }
  491. }
  492. static void display(priv_dumper_t* dumper)
  493. {
  494. FS *fs;
  495. FU *fu;
  496. PR *pr;
  497. int cnt;
  498. unsigned char *bp, *savebp;
  499. off_t saveaddress;
  500. unsigned char savech = '\0';
  501. while ((bp = get(dumper)) != NULL) {
  502. fs = dumper->pub.fshead;
  503. savebp = bp;
  504. saveaddress = dumper->address;
  505. for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) {
  506. for (fu = fs->nextfu; fu; fu = fu->nextfu) {
  507. if (fu->flags & F_IGNORE) {
  508. break;
  509. }
  510. for (cnt = fu->reps; cnt; --cnt) {
  511. for (pr = fu->nextpr; pr; dumper->address += pr->bcnt,
  512. bp += pr->bcnt, pr = pr->nextpr) {
  513. if (dumper->eaddress && dumper->address >= dumper->eaddress
  514. && !(pr->flags & (F_TEXT | F_BPAD))
  515. ) {
  516. bpad(pr);
  517. }
  518. if (cnt == 1 && pr->nospace) {
  519. savech = *pr->nospace;
  520. *pr->nospace = '\0';
  521. }
  522. /* PRINT; */
  523. switch (pr->flags) {
  524. case F_ADDRESS:
  525. printf(pr->fmt, (unsigned) dumper->address);
  526. break;
  527. case F_BPAD:
  528. printf(pr->fmt, "");
  529. break;
  530. case F_C:
  531. conv_c(pr, bp);
  532. break;
  533. case F_CHAR:
  534. printf(pr->fmt, *bp);
  535. break;
  536. case F_DBL: {
  537. double dval;
  538. float fval;
  539. switch (pr->bcnt) {
  540. case 4:
  541. memcpy(&fval, bp, sizeof(fval));
  542. printf(pr->fmt, fval);
  543. break;
  544. case 8:
  545. memcpy(&dval, bp, sizeof(dval));
  546. printf(pr->fmt, dval);
  547. break;
  548. }
  549. break;
  550. }
  551. case F_INT: {
  552. int ival;
  553. short sval;
  554. switch (pr->bcnt) {
  555. case 1:
  556. printf(pr->fmt, (int) *bp);
  557. break;
  558. case 2:
  559. memcpy(&sval, bp, sizeof(sval));
  560. printf(pr->fmt, (int) sval);
  561. break;
  562. case 4:
  563. memcpy(&ival, bp, sizeof(ival));
  564. printf(pr->fmt, ival);
  565. break;
  566. }
  567. break;
  568. }
  569. case F_P:
  570. printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.');
  571. break;
  572. case F_STR:
  573. printf(pr->fmt, (char *) bp);
  574. break;
  575. case F_TEXT:
  576. printf(pr->fmt);
  577. break;
  578. case F_U:
  579. conv_u(pr, bp);
  580. break;
  581. case F_UINT: {
  582. unsigned ival;
  583. unsigned short sval;
  584. switch (pr->bcnt) {
  585. case 1:
  586. printf(pr->fmt, (unsigned) *bp);
  587. break;
  588. case 2:
  589. memcpy(&sval, bp, sizeof(sval));
  590. printf(pr->fmt, (unsigned) sval);
  591. break;
  592. case 4:
  593. memcpy(&ival, bp, sizeof(ival));
  594. printf(pr->fmt, ival);
  595. break;
  596. }
  597. break;
  598. }
  599. }
  600. if (cnt == 1 && pr->nospace) {
  601. *pr->nospace = savech;
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. if (dumper->endfu) {
  609. /*
  610. * if eaddress not set, error or file size was multiple
  611. * of blocksize, and no partial block ever found.
  612. */
  613. if (!dumper->eaddress) {
  614. if (!dumper->address) {
  615. return;
  616. }
  617. dumper->eaddress = dumper->address;
  618. }
  619. for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) {
  620. switch (pr->flags) {
  621. case F_ADDRESS:
  622. printf(pr->fmt, (unsigned) dumper->eaddress);
  623. break;
  624. case F_TEXT:
  625. printf(pr->fmt);
  626. break;
  627. }
  628. }
  629. }
  630. }
  631. #define dumper ((priv_dumper_t*)pub_dumper)
  632. int FAST_FUNC bb_dump_dump(dumper_t *pub_dumper, char **argv)
  633. {
  634. FS *tfs;
  635. int blocksize;
  636. /* figure out the data block bb_dump_size */
  637. blocksize = 0;
  638. tfs = dumper->pub.fshead;
  639. while (tfs) {
  640. tfs->bcnt = bb_dump_size(tfs);
  641. if (blocksize < tfs->bcnt) {
  642. blocksize = tfs->bcnt;
  643. }
  644. tfs = tfs->nextfs;
  645. }
  646. dumper->blocksize = blocksize;
  647. /* rewrite the rules, do syntax checking */
  648. for (tfs = dumper->pub.fshead; tfs; tfs = tfs->nextfs) {
  649. rewrite(dumper, tfs);
  650. }
  651. dumper->argv = argv;
  652. display(dumper);
  653. return dumper->exitval;
  654. }
  655. void FAST_FUNC bb_dump_add(dumper_t* pub_dumper, const char *fmt)
  656. {
  657. const char *p;
  658. char *p1;
  659. char *p2;
  660. FS *tfs;
  661. FU *tfu, **nextfupp;
  662. const char *savep;
  663. /* start new linked list of format units */
  664. tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
  665. if (!dumper->pub.fshead) {
  666. dumper->pub.fshead = tfs;
  667. } else {
  668. FS *fslast = dumper->pub.fshead;
  669. while (fslast->nextfs)
  670. fslast = fslast->nextfs;
  671. fslast->nextfs = tfs;
  672. }
  673. nextfupp = &tfs->nextfu;
  674. /* take the format string and break it up into format units */
  675. p = fmt;
  676. for (;;) {
  677. p = skip_whitespace(p);
  678. if (!*p) {
  679. break;
  680. }
  681. /* allocate a new format unit and link it in */
  682. /* NOSTRICT */
  683. /* DBU:[dave@cray.com] zalloc so that forward pointers start out NULL */
  684. tfu = xzalloc(sizeof(FU));
  685. *nextfupp = tfu;
  686. nextfupp = &tfu->nextfu;
  687. tfu->reps = 1;
  688. /* if leading digit, repetition count */
  689. if (isdigit(*p)) {
  690. for (savep = p; isdigit(*p); ++p)
  691. continue;
  692. if (!isspace(*p) && *p != '/') {
  693. bb_error_msg_and_die("bad format {%s}", fmt);
  694. }
  695. /* may overwrite either white space or slash */
  696. tfu->reps = atoi(savep);
  697. tfu->flags = F_SETREP;
  698. /* skip trailing white space */
  699. p = skip_whitespace(++p);
  700. }
  701. /* skip slash and trailing white space */
  702. if (*p == '/') {
  703. p = skip_whitespace(++p);
  704. }
  705. /* byte count */
  706. if (isdigit(*p)) {
  707. // TODO: use bb_strtou
  708. savep = p;
  709. while (isdigit(*++p))
  710. continue;
  711. if (!isspace(*p)) {
  712. bb_error_msg_and_die("bad format {%s}", fmt);
  713. }
  714. tfu->bcnt = atoi(savep);
  715. /* skip trailing white space */
  716. p = skip_whitespace(++p);
  717. }
  718. /* format */
  719. if (*p != '"') {
  720. bb_error_msg_and_die("bad format {%s}", fmt);
  721. }
  722. for (savep = ++p; *p != '"';) {
  723. if (*p++ == 0) {
  724. bb_error_msg_and_die("bad format {%s}", fmt);
  725. }
  726. }
  727. tfu->fmt = xstrndup(savep, p - savep);
  728. /* escape(tfu->fmt); */
  729. p1 = tfu->fmt;
  730. /* alphabetic escape sequences have to be done in place */
  731. for (p2 = p1;; ++p1, ++p2) {
  732. if (!*p1) {
  733. *p2 = *p1;
  734. break;
  735. }
  736. if (*p1 == '\\') {
  737. const char *cs = conv_str + 4;
  738. ++p1;
  739. *p2 = *p1;
  740. do {
  741. if (*p1 == cs[2]) {
  742. *p2 = cs[0];
  743. break;
  744. }
  745. cs += 4;
  746. } while (*cs);
  747. }
  748. }
  749. p++;
  750. }
  751. }
  752. /*
  753. * Copyright (c) 1989 The Regents of the University of California.
  754. * All rights reserved.
  755. *
  756. * Redistribution and use in source and binary forms, with or without
  757. * modification, are permitted provided that the following conditions
  758. * are met:
  759. * 1. Redistributions of source code must retain the above copyright
  760. * notice, this list of conditions and the following disclaimer.
  761. * 2. Redistributions in binary form must reproduce the above copyright
  762. * notice, this list of conditions and the following disclaimer in the
  763. * documentation and/or other materials provided with the distribution.
  764. * 3. Neither the name of the University nor the names of its contributors
  765. * may be used to endorse or promote products derived from this software
  766. * without specific prior written permission.
  767. *
  768. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  769. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  770. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  771. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  772. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  773. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  774. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  775. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  776. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  777. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  778. * SUCH DAMAGE.
  779. */