dump.c 19 KB

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