dump.c 20 KB

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