dump.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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[] = ".#-+ 0123456789";
  27. static const char size_conv_str[] =
  28. "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
  29. static const char lcc[] = "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(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 int 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. statok = done = 1;
  313. } else {
  314. if (done++)
  315. return 0;
  316. statok = 0;
  317. }
  318. if (bb_dump_skip)
  319. do_skip(statok ? *_argv : "stdin", statok);
  320. if (*_argv)
  321. ++_argv;
  322. if (!bb_dump_skip)
  323. return 1;
  324. }
  325. /* NOTREACHED */
  326. }
  327. static unsigned char *get(void)
  328. {
  329. static int ateof = 1;
  330. static unsigned char *curp=NULL, *savp; /*DBU:[dave@cray.com]initialize curp */
  331. int n;
  332. int need, nread;
  333. unsigned char *tmpp;
  334. if (!curp) {
  335. address = (off_t)0; /*DBU:[dave@cray.com] initialize,initialize..*/
  336. curp = (unsigned char *) xmalloc(bb_dump_blocksize);
  337. savp = (unsigned char *) xmalloc(bb_dump_blocksize);
  338. } else {
  339. tmpp = curp;
  340. curp = savp;
  341. savp = tmpp;
  342. address = savaddress += bb_dump_blocksize;
  343. }
  344. for (need = bb_dump_blocksize, nread = 0;;) {
  345. /*
  346. * if read the right number of bytes, or at EOF for one file,
  347. * and no other files are available, zero-pad the rest of the
  348. * block and set the end flag.
  349. */
  350. if (!bb_dump_length || (ateof && !next((char **) NULL))) {
  351. if (need == bb_dump_blocksize) {
  352. return NULL;
  353. }
  354. if (bb_dump_vflag != ALL && !memcmp(curp, savp, nread)) {
  355. if (bb_dump_vflag != DUP) {
  356. puts("*");
  357. }
  358. return NULL;
  359. }
  360. memset((char *) curp + nread, 0, need);
  361. eaddress = address + nread;
  362. return curp;
  363. }
  364. n = fread((char *) curp + nread, sizeof(unsigned char),
  365. bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
  366. if (!n) {
  367. if (ferror(stdin)) {
  368. bb_perror_msg("%s", _argv[-1]);
  369. }
  370. ateof = 1;
  371. continue;
  372. }
  373. ateof = 0;
  374. if (bb_dump_length != -1) {
  375. bb_dump_length -= n;
  376. }
  377. if (!(need -= n)) {
  378. if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
  379. || memcmp(curp, savp, bb_dump_blocksize)) {
  380. if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
  381. bb_dump_vflag = WAIT;
  382. }
  383. return curp;
  384. }
  385. if (bb_dump_vflag == WAIT) {
  386. puts("*");
  387. }
  388. bb_dump_vflag = DUP;
  389. address = savaddress += bb_dump_blocksize;
  390. need = bb_dump_blocksize;
  391. nread = 0;
  392. } else {
  393. nread += n;
  394. }
  395. }
  396. }
  397. static void bpad(PR * pr)
  398. {
  399. char *p1, *p2;
  400. /*
  401. * remove all conversion flags; '-' is the only one valid
  402. * with %s, and it's not useful here.
  403. */
  404. pr->flags = F_BPAD;
  405. *pr->cchar = 's';
  406. for (p1 = pr->fmt; *p1 != '%'; ++p1);
  407. for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1)
  408. if (pr->nospace) pr->nospace--;
  409. while ((*p2++ = *p1++) != 0);
  410. }
  411. static const char conv_str[] =
  412. "\0\\0\0"
  413. "\007\\a\0" /* \a */
  414. "\b\\b\0"
  415. "\f\\b\0"
  416. "\n\\n\0"
  417. "\r\\r\0"
  418. "\t\\t\0"
  419. "\v\\v\0"
  420. "\0";
  421. static void conv_c(PR * pr, unsigned char * p)
  422. {
  423. const char *str = conv_str;
  424. char buf[10];
  425. do {
  426. if (*p == *str) {
  427. ++str;
  428. goto strpr;
  429. }
  430. str += 4;
  431. } while (*str);
  432. if (isprint(*p)) {
  433. *pr->cchar = 'c';
  434. (void) printf(pr->fmt, *p);
  435. } else {
  436. sprintf(buf, "%03o", (int) *p);
  437. str = buf;
  438. strpr:
  439. *pr->cchar = 's';
  440. printf(pr->fmt, str);
  441. }
  442. }
  443. static void conv_u(PR * pr, unsigned char * p)
  444. {
  445. static const char list[] =
  446. "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
  447. "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
  448. "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
  449. "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
  450. /* od used nl, not lf */
  451. if (*p <= 0x1f) {
  452. *pr->cchar = 's';
  453. printf(pr->fmt, list + (4 * (int)*p));
  454. } else if (*p == 0x7f) {
  455. *pr->cchar = 's';
  456. printf(pr->fmt, "del");
  457. } else if (isprint(*p)) {
  458. *pr->cchar = 'c';
  459. printf(pr->fmt, *p);
  460. } else {
  461. *pr->cchar = 'x';
  462. printf(pr->fmt, (int) *p);
  463. }
  464. }
  465. static void display(void)
  466. {
  467. /* extern FU *endfu; */
  468. FS *fs;
  469. FU *fu;
  470. PR *pr;
  471. int cnt;
  472. unsigned char *bp;
  473. off_t saveaddress;
  474. unsigned char savech = 0, *savebp;
  475. while ((bp = get()) != NULL) {
  476. for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
  477. fs = fs->nextfs, bp = savebp, address = saveaddress) {
  478. for (fu = fs->nextfu; fu; fu = fu->nextfu) {
  479. if (fu->flags & F_IGNORE) {
  480. break;
  481. }
  482. for (cnt = fu->reps; cnt; --cnt) {
  483. for (pr = fu->nextpr; pr; address += pr->bcnt,
  484. bp += pr->bcnt, pr = pr->nextpr) {
  485. if (eaddress && address >= eaddress &&
  486. !(pr->flags & (F_TEXT | F_BPAD))) {
  487. bpad(pr);
  488. }
  489. if (cnt == 1 && pr->nospace) {
  490. savech = *pr->nospace;
  491. *pr->nospace = '\0';
  492. }
  493. /* PRINT; */
  494. switch (pr->flags) {
  495. case F_ADDRESS:
  496. printf(pr->fmt, (unsigned int) address);
  497. break;
  498. case F_BPAD:
  499. printf(pr->fmt, "");
  500. break;
  501. case F_C:
  502. conv_c(pr, bp);
  503. break;
  504. case F_CHAR:
  505. printf(pr->fmt, *bp);
  506. break;
  507. case F_DBL:{
  508. double dval;
  509. float fval;
  510. switch (pr->bcnt) {
  511. case 4:
  512. memmove((char *) &fval, (char *) bp,
  513. sizeof(fval));
  514. printf(pr->fmt, fval);
  515. break;
  516. case 8:
  517. memmove((char *) &dval, (char *) bp,
  518. sizeof(dval));
  519. printf(pr->fmt, dval);
  520. break;
  521. }
  522. break;
  523. }
  524. case F_INT:{
  525. int ival;
  526. short sval;
  527. switch (pr->bcnt) {
  528. case 1:
  529. printf(pr->fmt, (int) *bp);
  530. break;
  531. case 2:
  532. memmove((char *) &sval, (char *) bp,
  533. sizeof(sval));
  534. printf(pr->fmt, (int) sval);
  535. break;
  536. case 4:
  537. memmove((char *) &ival, (char *) bp,
  538. sizeof(ival));
  539. printf(pr->fmt, ival);
  540. break;
  541. }
  542. break;
  543. }
  544. case F_P:
  545. printf(pr->fmt, isprint(*bp) ? *bp : '.');
  546. break;
  547. case F_STR:
  548. printf(pr->fmt, (char *) bp);
  549. break;
  550. case F_TEXT:
  551. printf(pr->fmt);
  552. break;
  553. case F_U:
  554. conv_u(pr, bp);
  555. break;
  556. case F_UINT:{
  557. unsigned int ival;
  558. unsigned short sval;
  559. switch (pr->bcnt) {
  560. case 1:
  561. printf(pr->fmt, (unsigned int) * bp);
  562. break;
  563. case 2:
  564. memmove((char *) &sval, (char *) bp,
  565. sizeof(sval));
  566. printf(pr->fmt, (unsigned int) sval);
  567. break;
  568. case 4:
  569. memmove((char *) &ival, (char *) bp,
  570. sizeof(ival));
  571. printf(pr->fmt, ival);
  572. break;
  573. }
  574. break;
  575. }
  576. }
  577. if (cnt == 1 && pr->nospace) {
  578. *pr->nospace = savech;
  579. }
  580. }
  581. }
  582. }
  583. }
  584. }
  585. if (endfu) {
  586. /*
  587. * if eaddress not set, error or file bb_dump_size was multiple of
  588. * bb_dump_blocksize, and no partial block ever found.
  589. */
  590. if (!eaddress) {
  591. if (!address) {
  592. return;
  593. }
  594. eaddress = address;
  595. }
  596. for (pr = endfu->nextpr; pr; pr = pr->nextpr) {
  597. switch (pr->flags) {
  598. case F_ADDRESS:
  599. (void) printf(pr->fmt, (unsigned int) eaddress);
  600. break;
  601. case F_TEXT:
  602. (void) printf(pr->fmt);
  603. break;
  604. }
  605. }
  606. }
  607. }
  608. int bb_dump_dump(char **argv)
  609. {
  610. FS *tfs;
  611. /* figure out the data block bb_dump_size */
  612. for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
  613. tfs->bcnt = bb_dump_size(tfs);
  614. if (bb_dump_blocksize < tfs->bcnt) {
  615. bb_dump_blocksize = tfs->bcnt;
  616. }
  617. }
  618. /* rewrite the rules, do syntax checking */
  619. for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
  620. rewrite(tfs);
  621. }
  622. next(argv);
  623. display();
  624. return exitval;
  625. }
  626. void bb_dump_add(const char *fmt)
  627. {
  628. const char *p;
  629. char *p1;
  630. char *p2;
  631. static FS **nextfs;
  632. FS *tfs;
  633. FU *tfu, **nextfu;
  634. const char *savep;
  635. /* start new linked list of format units */
  636. tfs = xzalloc(sizeof(FS)); /*DBU:[dave@cray.com] start out NULL */
  637. if (!bb_dump_fshead) {
  638. bb_dump_fshead = tfs;
  639. } else {
  640. *nextfs = tfs;
  641. }
  642. nextfs = &tfs->nextfs;
  643. nextfu = &tfs->nextfu;
  644. /* take the format string and break it up into format units */
  645. for (p = fmt;;) {
  646. /* bb_dump_skip leading white space */
  647. p = skip_whitespace(p);
  648. if (!*p) {
  649. break;
  650. }
  651. /* allocate a new format unit and link it in */
  652. /* NOSTRICT */
  653. /* DBU:[dave@cray.com] calloc so that forward pointers start out NULL */
  654. tfu = xzalloc(sizeof(FU));
  655. *nextfu = tfu;
  656. nextfu = &tfu->nextfu;
  657. tfu->reps = 1;
  658. /* if leading digit, repetition count */
  659. if (isdigit(*p)) {
  660. for (savep = p; isdigit(*p); ++p);
  661. if (!isspace(*p) && *p != '/') {
  662. bb_error_msg_and_die("bad format {%s}", fmt);
  663. }
  664. /* may overwrite either white space or slash */
  665. tfu->reps = atoi(savep);
  666. tfu->flags = F_SETREP;
  667. /* bb_dump_skip trailing white space */
  668. p = skip_whitespace(++p);
  669. }
  670. /* bb_dump_skip slash and trailing white space */
  671. if (*p == '/') {
  672. p = skip_whitespace(++p);
  673. }
  674. /* byte count */
  675. if (isdigit(*p)) {
  676. for (savep = p; isdigit(*p); ++p);
  677. if (!isspace(*p)) {
  678. bb_error_msg_and_die("bad format {%s}", fmt);
  679. }
  680. tfu->bcnt = atoi(savep);
  681. /* bb_dump_skip trailing white space */
  682. p = skip_whitespace(++p);
  683. }
  684. /* format */
  685. if (*p != '"') {
  686. bb_error_msg_and_die("bad format {%s}", fmt);
  687. }
  688. for (savep = ++p; *p != '"';) {
  689. if (*p++ == 0) {
  690. bb_error_msg_and_die("bad format {%s}", fmt);
  691. }
  692. }
  693. tfu->fmt = xmalloc(p - savep + 1);
  694. strncpy(tfu->fmt, savep, p - savep);
  695. tfu->fmt[p - savep] = '\0';
  696. /* escape(tfu->fmt); */
  697. p1 = tfu->fmt;
  698. /* alphabetic escape sequences have to be done in place */
  699. for (p2 = p1;; ++p1, ++p2) {
  700. if (!*p1) {
  701. *p2 = *p1;
  702. break;
  703. }
  704. if (*p1 == '\\') {
  705. const char *cs = conv_str + 4;
  706. ++p1;
  707. *p2 = *p1;
  708. do {
  709. if (*p1 == cs[2]) {
  710. *p2 = cs[0];
  711. break;
  712. }
  713. cs += 4;
  714. } while (*cs);
  715. }
  716. }
  717. p++;
  718. }
  719. }
  720. /*
  721. * Copyright (c) 1989 The Regents of the University of California.
  722. * All rights reserved.
  723. *
  724. * Redistribution and use in source and binary forms, with or without
  725. * modification, are permitted provided that the following conditions
  726. * are met:
  727. * 1. Redistributions of source code must retain the above copyright
  728. * notice, this list of conditions and the following disclaimer.
  729. * 2. Redistributions in binary form must reproduce the above copyright
  730. * notice, this list of conditions and the following disclaimer in the
  731. * documentation and/or other materials provided with the distribution.
  732. * 3. Neither the name of the University nor the names of its contributors
  733. * may be used to endorse or promote products derived from this software
  734. * without specific prior written permission.
  735. *
  736. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  737. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  738. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  739. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  740. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  741. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  742. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  743. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  744. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  745. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  746. * SUCH DAMAGE.
  747. */