annotate toys/posix/od.c @ 1178:f8432a8872e0 draft

More work on the cleanup page, partway through describing ifconfig.
author Rob Landley <rob@landley.net>
date Wed, 01 Jan 2014 15:00:44 -0600
parents 2c53be7c9f9b
children 77af6bc22fbc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* od.c - Provide octal/hex dumps of data
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
4 * Copyright 2012 Rob Landley <rob@landley.net>
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
6 * See http://opengroup.org/onlinepubs/9699919799/utilities/od.html
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
8 USE_OD(NEWTOY(od, "j#vN#xsodcbA:t*", TOYFLAG_USR|TOYFLAG_BIN))
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
9
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 config OD
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
11 bool "od"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
12 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
13 help
1070
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
14 usage: od [-bcdosxv] [-j #] [-N #] [-A doxn] [-t acdfoux[#]]
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
15
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
16 -A Address base (decimal, octal, hexdecimal, none)
1070
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
17 -j Skip this many bytes of input
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
18 -N Stop dumping after this many bytes
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
19 -t output type a(scii) c(har) d(ecimal) f(loat) o(ctal) u(nsigned) (he)x
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
20 plus optional size in bytes
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
21 aliases: -b=-t o1, -c=-t c, -d=-t u2, -o=-t o2, -s=-t d2, -x=-t x2
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
22 -v Don't collapse repeated lines together
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 */
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
25 #define FOR_od
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #include "toys.h"
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
28 GLOBALS(
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
29 struct arg_list *output_base;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
30 char *address_base;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
31 long max_count;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
32 long jump_bytes;
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
33
1139
2c53be7c9f9b Can't test <0 on an unsigned, reported by Nathan McSween.
Rob Landley <rob@landley.net>
parents: 1136
diff changeset
34 int address_idx;
2c53be7c9f9b Can't test <0 on an unsigned, reported by Nathan McSween.
Rob Landley <rob@landley.net>
parents: 1136
diff changeset
35 unsigned types, leftover, star;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
36 char *buf;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
37 uint64_t bufs[4]; // force 64-bit alignment
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
38 off_t pos;
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 )
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
40
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
41 static char *ascii = "nulsohstxetxeotenqackbel bs ht nl vt ff cr so si"
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
42 "dledc1dc2dc3dc4naksynetbcan emsubesc fs gs rs us sp";
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
43
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
44 struct odtype {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
45 int type;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
46 int size;
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
47 };
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
48
1136
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
49 static int od_out_t(struct odtype *t, char *buf, int *offset)
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
50 {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
51 unsigned k;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
52 int throw = 0, pad = 0;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
53
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
54 // Handle ascii
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
55 if (t->type < 2) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
56 char c = TT.buf[(*offset)++];
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
57 pad += 4;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
58
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
59 if (!t->type) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
60 c &= 127;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
61 if (c<=32) sprintf(buf, "%.3s", ascii+(3*c));
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
62 else if (c==127) strcpy(buf, "del");
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
63 else sprintf(buf, "%c", c);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
64 } else {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
65 char *bfnrtav = "\b\f\n\r\t\a\v", *s = strchr(bfnrtav, c);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
66 if (s) sprintf(buf, "\\%c", "bfnrtav0"[s-bfnrtav]);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
67 else if (c < 32 || c >= 127) sprintf(buf, "%03o", c);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
68 else {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
69 // TODO: this should be UTF8 aware.
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
70 sprintf(buf, "%c", c);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
71 }
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
72 }
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
73 } else if (CFG_TOYBOX_FLOAT && t->type == 6) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
74 long double ld;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
75 union {float f; double d; long double ld;} fdl;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
76
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
77 memcpy(&fdl, TT.buf+*offset, t->size);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
78 *offset += t->size;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
79 if (sizeof(float) == t->size) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
80 ld = fdl.f;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
81 pad += (throw = 8)+7;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
82 } else if (sizeof(double) == t->size) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
83 ld = fdl.d;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
84 pad += (throw = 17)+8;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
85 } else if (sizeof(long double) == t->size) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
86 ld = fdl.ld;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
87 pad += (throw = 21)+9;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
88 } else error_exit("bad -tf '%d'", t->size);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
89
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
90 sprintf(buf, "%.*Le", throw, ld);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
91 // Integer types
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
92 } else {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
93 unsigned long long ll = 0, or;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
94 char *c[] = {"%*lld", "%*llu", "%0*llo", "%0*llx"},
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
95 *class = c[t->type-2];
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
96
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
97 // Work out width of field
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
98 if (t->size == 8) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
99 or = -1LL;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
100 if (t->type == 2) or >>= 1;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
101 } else or = (1LL<<(8*t->size))-1;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
102 throw = sprintf(buf, class, 0, or);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
103
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
104 // Accumulate integer based on size argument
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
105 for (k=0; k < t->size; k++) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
106 or = TT.buf[(*offset)++];
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
107 ll |= or << (8*(IS_BIG_ENDIAN ? t->size-k-1 : k));
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
108 }
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
109
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
110 // Handle negative values
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
111 if (t->type == 2) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
112 or = sizeof(or) - t->size;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
113 throw++;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
114 if (or && (ll & (1l<<((8*t->size)-1))))
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
115 ll |= ((or<<(8*or))-1) << (8*t->size);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
116 }
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
117
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
118 sprintf(buf, class, throw, ll);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
119 pad += throw+1;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
120 }
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
121
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
122 return pad;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
123 }
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
124
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
125 static void od_outline(void)
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
127 unsigned flags = toys.optflags;
1136
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
128 char buf[128], *abases[] = {"", "%07d", "%07o", "%06x"};
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
129 struct odtype *types = (struct odtype *)toybuf;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
130 int i, j, len, pad;
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
131
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
132 if (TT.leftover<16) memset(TT.buf+TT.leftover, 0, 16-TT.leftover);
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
133
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
134 // Handle duplciate lines as *
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
135 if (!(flags&FLAG_v) && TT.jump_bytes != TT.pos && TT.leftover
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
136 && !memcmp(TT.bufs, TT.bufs + 2, 16))
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
137 {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
138 if (!TT.star) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
139 xputs("*");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
140 TT.star++;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
141 }
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
142
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
143 // Print line position
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
144 } else {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
145 TT.star = 0;
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
146
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
147 xprintf(abases[TT.address_idx], TT.pos);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
148 if (!TT.leftover) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
149 if (TT.address_idx) xputc('\n');
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
150 return;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
151 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
152 }
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
153
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
154 TT.pos += len = TT.leftover;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
155 TT.leftover = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
156 if (TT.star) return;
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
157
1136
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
158 // Find largest "pad" of the output types.
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
159 for (i = pad = 0; i<TT.types; i++) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
160 int bytes = 0;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
161
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
162 // If more than one byte of input consumed, average rounding up.
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
163 j = od_out_t(types+i, buf, &bytes);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
164 j = (j+bytes-1)/bytes;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
165
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
166 if (j > pad) pad = j;
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
167 }
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
168
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
169 // For each output type, print one line
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
170
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
171 for (i=0; i<TT.types; i++) {
1136
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
172 for (j = 0; j<len;) {
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
173 int bytes = j;
622
93ef2516f15a Fill out od -c and -f.
Rob Landley <rob@landley.net>
parents: 621
diff changeset
174
1136
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
175 // pad for as many bytes as were consumed, and indent non-numbered lines
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
176 od_out_t(types+i, buf, &bytes);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
177 xprintf("%*s", pad*(bytes-j) + 7*(!!i)*!j, buf);
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
178 j = bytes;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
179 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
180 xputc('\n');
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
181 }
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
182
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
183 // buffer toggle for "same as last time" check.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
184 TT.buf = (char *)((TT.buf == (char *)TT.bufs) ? TT.bufs+2 : TT.bufs);
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 }
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
186
1136
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
187 // Loop through input files
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 static void do_od(int fd, char *name)
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
190 // Skip input, possibly more than one entire file.
1070
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
191 if (TT.jump_bytes > TT.pos) {
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
192 off_t pos = TT.jump_bytes-TT.pos, off = lskip(fd, pos);
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
193
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
194 if (off >= 0) TT.pos += pos-off;
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
195 if (TT.jump_bytes > TT.pos) return;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
196 }
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
197
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
198 for(;;) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
199 char *buf = TT.buf + TT.leftover;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
200 int len = 16 - TT.leftover;
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
201
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
202 if (toys.optflags & FLAG_N) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
203 if (!TT.max_count) break;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
204 if (TT.max_count < len) len = TT.max_count;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
205 }
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
206
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
207 len = readall(fd, buf, len);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
208 if (len < 0) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
209 perror_msg("%s", name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
210 break;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
211 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
212 if (TT.max_count) TT.max_count -= len;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
213 TT.leftover += len;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
214 if (TT.leftover < 16) break;
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
215
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
216 od_outline();
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
217 }
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
218 }
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
219
1136
44ca17a8c83e Redo od whitespace with multiple output types so the columns align.
Rob Landley <rob@landley.net>
parents: 1070
diff changeset
220 // Handle one -t argument (including implicit ones)
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 static void append_base(char *base)
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
223 char *s = base;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
224 struct odtype *types = (struct odtype *)toybuf;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
225 int type;
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
226
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
227 for (;;) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
228 int size = 1;
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
229
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
230 if (!*s) return;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
231 if (TT.types >= sizeof(toybuf)/sizeof(struct odtype)) break;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
232 if (-1 == (type = stridx("acduox"USE_TOYBOX_FLOAT("f"), *(s++)))) break;
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
233
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
234 if (isdigit(*s)) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
235 size = strtol(s, &s, 10);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
236 if (type < 2 && size != 1) break;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
237 if (CFG_TOYBOX_FLOAT && type == 6 && size == sizeof(long double));
1070
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
238 else if (size < 1 || size > 8) break;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
239 } else if (CFG_TOYBOX_FLOAT && type == 6) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
240 int sizes[] = {sizeof(float), sizeof(double), sizeof(long double)};
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
241 if (-1 == (size = stridx("FDL", *s))) size = sizeof(double);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
242 else {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
243 s++;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
244 size = sizes[size];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
245 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
246 } else if (type > 1) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
247 if (-1 == (size = stridx("CSIL", *s))) size = 4;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
248 else {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
249 s++;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
250 size = 1 << size;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
251 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
252 }
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
253
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
254 types[TT.types].type = type;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
255 types[TT.types].size = size;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
256 TT.types++;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
257 }
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
258
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
259 error_exit("bad -t %s", base);
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 }
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
261
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 void od_main(void)
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
263 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
264 struct arg_list *arg;
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
265
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
266 TT.buf = (char *)TT.bufs;
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
267
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
268 if (!TT.address_base) TT.address_idx = 2;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
269 else if (0>(TT.address_idx = stridx("ndox", *TT.address_base)))
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
270 error_exit("bad -A '%c'", *TT.address_base);
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
271
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
272 // Collect -t entries
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
273
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
274 for (arg = TT.output_base; arg; arg = arg->next) append_base(arg->arg);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
275 if (toys.optflags & FLAG_b) append_base("o1");
1070
16167a7c1b5a Fix -t c0 and -J as reported by heehooman at gmail on the list.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
276 if (toys.optflags & FLAG_c) append_base("c");
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
277 if (toys.optflags & FLAG_d) append_base("u2");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
278 if (toys.optflags & FLAG_o) append_base("o2");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
279 if (toys.optflags & FLAG_s) append_base("d2");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
280 if (toys.optflags & FLAG_x) append_base("x2");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
281 if (!TT.output_base) append_base("o2");
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
282
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
283 loopfiles(toys.optargs, do_od);
621
488032f4394a Complete rewrite of od, mostly working. (Need to finish -c and -f.)
Rob Landley <rob@landley.net>
parents: 613
diff changeset
284
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
285 if (TT.leftover) od_outline();
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
286 od_outline();
611
c9865aadb9fc First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff changeset
287 }