Mercurial > hg > toybox
annotate toys/posix/od.c @ 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.
author  Rob Landley <rob@landley.net> 

date  Mon, 08 Oct 2012 00:02:30 0500 
parents  6df4ccc0acbe 
children  786841fdb1e0 
rev  line source 

611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

1 /* vi: set sw=4 ts=4: 
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 * od.c  Provide octal/hex dumps of data 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

4 * 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

5 * 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

6 * Copyright 2012 Rob Landley <rob@landley.net> 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

7 * 
656
6df4ccc0acbe
Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents:
653
diff
changeset

8 * 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

9 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

10 USE_OD(NEWTOY(od, "j#vN#xsodcbA:t*", TOYFLAG_USRTOYFLAG_BIN)) 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

11 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

12 config OD 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

13 bool "od" 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

14 default y 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

15 help 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

16 usage: od [bdosxv] [j #] [N #] [A doxn] [t arg] 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

17 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

18 A Address base (decimal, octal, hexdecimal, none) 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

19 t output type(s) a (ascii) c (char) d (decimal) foux 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

20 */ 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

21 
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

22 #define FOR_od 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

23 #include "toys.h" 
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 GLOBALS( 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

26 struct arg_list *output_base; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

27 char *address_base; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

28 long max_count; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

29 long jump_bytes; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

30 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

31 unsigned types, leftover, star, address_idx; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

32 char *buf; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

33 uint64_t bufs[4]; // force 64bit alignment 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

34 off_t pos; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

35 ) 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

36 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

37 static char *ascii = "nulsohstxetxeotenqackbel bs ht nl vt ff cr so si" 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

38 "dledc1dc2dc3dc4naksynetbcan emsubesc fs gs rs us sp"; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

39 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

40 struct odtype { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

41 int type; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

42 int size; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

43 }; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

44 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

45 static void od_outline(void) 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

46 { 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

47 unsigned flags = toys.optflags; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

48 char *abases[] = {"", "%07d", "%07o", "%06x"}; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

49 struct odtype *types = (struct odtype *)toybuf, *t; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

50 int i, len; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

51 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

52 if (TT.leftover<16) memset(TT.buf+TT.leftover, 0, 16TT.leftover); 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

53 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

54 // Handle duplciate lines as * 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

55 if (!(flags&FLAG_v) && TT.jump_bytes != TT.pos && TT.leftover 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

56 && !memcmp(TT.bufs, TT.bufs + 2, 16)) 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

57 { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

58 if (!TT.star) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

59 xputs("*"); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

60 TT.star++; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

61 } 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

62 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

63 // Print line position 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

64 } else { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

65 TT.star = 0; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

66 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

67 xprintf(abases[TT.address_idx], TT.pos); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

68 if (!TT.leftover) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

69 if (TT.address_idx) xputc('\n'); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

70 return; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

71 } 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

72 } 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

73 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

74 TT.pos += len = TT.leftover; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

75 TT.leftover = 0; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

76 if (TT.star) return; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

77 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

78 // For each output type, print one line 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

79 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

80 for (i=0; i<TT.types; i++) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

81 int j = 0, pad = i ? 8 : 0; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

82 char buf[128]; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

83 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

84 t = types+i; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

85 while (j<len) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

86 unsigned k; 
622  87 int throw = 0; 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

88 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

89 // Handle ascii 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

90 if (t>type < 2) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

91 char c = TT.buf[j++]; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

92 pad += 4; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

93 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

94 if (!t>type) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

95 c &= 127; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

96 if (c<=32) sprintf(buf, "%.3s", ascii+(3*c)); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

97 else if (c==127) strcpy(buf, "del"); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

98 else sprintf(buf, "%c", c); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

99 } else { 
622  100 char *bfnrtav = "\b\f\n\r\t\a\v", *s = strchr(bfnrtav, c); 
101 if (s) sprintf(buf, "\\%c", "bfnrtav0"[sbfnrtav]);  
102 else if (c < 32  c >= 127) sprintf(buf, "%03o", c);  
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

103 else { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

104 // TODO: this should be UTF8 aware. 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

105 sprintf(buf, "%c", c); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

106 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

107 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

108 } else if (CFG_TOYBOX_FLOAT && t>type == 6) { 
622  109 long double ld; 
110 union {float f; double d; long double ld;} fdl;  
111  
112 memcpy(&fdl, TT.buf+j, t>size);  
113 j += t>size;  
114 if (sizeof(float) == t>size) {  
115 ld = fdl.f;  
116 pad += (throw = 8)+7;  
117 } else if (sizeof(double) == t>size) {  
118 ld = fdl.d;  
119 pad += (throw = 17)+8;  
120 } else if (sizeof(long double) == t>size) {  
121 ld = fdl.ld;  
122 pad += (throw = 21)+9;  
123 } else error_exit("bad tf '%d'", t>size);  
124  
125 sprintf(buf, "%.*Le", throw, ld);  
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

126 // Integer types 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

127 } else { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

128 unsigned long long ll = 0, or; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

129 char *c[] = {"%*lld", "%*llu", "%0*llo", "%0*llx"}, 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

130 *class = c[t>type2]; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

131 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

132 // Work out width of field 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

133 if (t>size == 8) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

134 or = 1LL; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

135 if (t>type == 2) or >>= 1; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

136 } else or = (1LL<<(8*t>size))1; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

137 throw = sprintf(buf, class, 0, or); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

138 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

139 // Accumulate integer based on size argument 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

140 for (k=0; k < t>size; k++) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

141 or = TT.buf[j++]; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

142 ll = or << (8*(IS_BIG_ENDIAN ? t>sizek1 : k)); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

143 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

144 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

145 // Handle negative values 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

146 if (t>type == 2) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

147 or = sizeof(or)  t>size; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

148 throw++; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

149 if (or && (ll & (1l<<((8*t>size)1)))) 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

150 ll = ((or<<(8*or))1) << (8*t>size); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

151 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

152 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

153 sprintf(buf, class, throw, ll); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

154 pad += throw+1; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

155 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

156 xprintf("%*s", pad, buf); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

157 pad = 0; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

158 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

159 xputc('\n'); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

160 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

161 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

162 // buffer toggle for "same as last time" check. 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

163 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

164 } 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

165 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

166 static void do_od(int fd, char *name) 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

167 { 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

168 // Skip input, possibly more than one entire file. 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

169 if (TT.jump_bytes < TT.pos) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

170 off_t off = lskip(fd, TT.jump_bytes); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

171 if (off > 0) TT.pos += off; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

172 if (TT.jump_bytes < TT.pos) return; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

173 } 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

174 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

175 for(;;) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

176 char *buf = TT.buf + TT.leftover; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

177 int len = 16  TT.leftover; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

178 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

179 if (toys.optflags & FLAG_N) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

180 if (!TT.max_count) break; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

181 if (TT.max_count < len) len = TT.max_count; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

182 } 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

183 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

184 len = readall(fd, buf, len); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

185 if (len < 0) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

186 perror_msg("%s", name); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

187 break; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

188 } 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

189 if (TT.max_count) TT.max_count = len; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

190 TT.leftover += len; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

191 if (TT.leftover < 16) break; 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

192 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

193 od_outline(); 
612
1121e0f42132
Some refactoring, shouldn't affect the generated code.
Rob Landley <rob@landley.net>
parents:
611
diff
changeset

194 } 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

195 } 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

196 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

197 static void append_base(char *base) 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

198 { 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

199 char *s = base; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

200 struct odtype *types = (struct odtype *)toybuf; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

201 int type; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

202 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

203 for (;;) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

204 int size = 1; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

205 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

206 if (!*s) return; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

207 if (TT.types >= sizeof(toybuf)/sizeof(struct odtype)) break; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

208 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

209 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

210 if (isdigit(*s)) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

211 size = strtol(s, &s, 10); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

212 if (type < 2 && size != 1) break; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

213 if (CFG_TOYBOX_FLOAT && type == 6 && size == sizeof(long double)); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

214 else if (size < 0  size > 8) break; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

215 } else if (CFG_TOYBOX_FLOAT && type == 6) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

216 int sizes[] = {sizeof(float), sizeof(double), sizeof(long double)}; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

217 if (1 == (size = stridx("FDL", *s))) size = sizeof(double); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

218 else { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

219 s++; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

220 size = sizes[size]; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

221 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

222 } else if (type > 1) { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

223 if (1 == (size = stridx("CSIL", *s))) size = 4; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

224 else { 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

225 s++; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

226 size = 1 << size; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

227 } 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

228 } 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

229 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

230 types[TT.types].type = type; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

231 types[TT.types].size = size; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

232 TT.types++; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

233 } 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

234 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

235 error_exit("bad t %s", base); 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

236 } 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

237 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

238 void od_main(void) 
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

239 { 
621
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

240 struct arg_list *arg; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

241 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

242 TT.buf = (char *)TT.bufs; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

243 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

244 if (!TT.address_base) TT.address_idx = 2; 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

245 else if (0>(TT.address_idx = stridx("ndox", *TT.address_base))) 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

246 error_exit("bad A '%c'", *TT.address_base); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

247 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

248 // Collect t entries 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

249 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

250 for (arg = TT.output_base; arg; arg = arg>next) append_base(arg>arg); 
612
1121e0f42132
Some refactoring, shouldn't affect the generated code.
Rob Landley <rob@landley.net>
parents:
611
diff
changeset

251 if (toys.optflags & FLAG_b) append_base("o1"); 
1121e0f42132
Some refactoring, shouldn't affect the generated code.
Rob Landley <rob@landley.net>
parents:
611
diff
changeset

252 if (toys.optflags & FLAG_d) append_base("u2"); 
1121e0f42132
Some refactoring, shouldn't affect the generated code.
Rob Landley <rob@landley.net>
parents:
611
diff
changeset

253 if (toys.optflags & FLAG_o) append_base("o2"); 
1121e0f42132
Some refactoring, shouldn't affect the generated code.
Rob Landley <rob@landley.net>
parents:
611
diff
changeset

254 if (toys.optflags & FLAG_s) append_base("d2"); 
1121e0f42132
Some refactoring, shouldn't affect the generated code.
Rob Landley <rob@landley.net>
parents:
611
diff
changeset

255 if (toys.optflags & FLAG_x) append_base("x2"); 
1121e0f42132
Some refactoring, shouldn't affect the generated code.
Rob Landley <rob@landley.net>
parents:
611
diff
changeset

256 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

257 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

258 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

259 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

260 if (TT.leftover) od_outline(); 
488032f4394a
Complete rewrite of od, mostly working. (Need to finish c and f.)
Rob Landley <rob@landley.net>
parents:
613
diff
changeset

261 od_outline(); 
611
c9865aadb9fc
First drop of od, from Andre Renaud.
Rob Landley <rob@landley.net>
parents:
diff
changeset

262 } 