dump.c 19 KB

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