Annotation of src/usr.bin/hexdump/odsyntax.c, Revision 1.8
1.8 ! mpech 1: /* $OpenBSD: odsyntax.c,v 1.7 2001/11/02 19:41:06 mickey Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1990 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: /*static char sccsid[] = "from: @(#)odsyntax.c 5.4 (Berkeley) 3/8/91";*/
1.8 ! mpech 38: static char rcsid[] = "$OpenBSD: odsyntax.c,v 1.7 2001/11/02 19:41:06 mickey Exp $";
1.1 deraadt 39: #endif /* not lint */
40:
41: #include <sys/types.h>
42: #include <stdlib.h>
1.7 mickey 43: #include <stdio.h>
1.4 deraadt 44: #include <ctype.h>
1.7 mickey 45: #include <err.h>
1.1 deraadt 46: #include "hexdump.h"
47:
48: int deprecated;
49:
1.6 pvalchev 50: void
1.1 deraadt 51: oldsyntax(argc, argvp)
52: int argc;
53: char ***argvp;
54: {
55: extern enum _vflag vflag;
56: extern FS *fshead;
57: extern char *optarg;
1.5 pvalchev 58: extern int optind;
1.1 deraadt 59: int ch;
60: char **argv;
61: static void odprecede();
62:
63: deprecated = 1;
64: argv = *argvp;
1.3 millert 65: while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != -1)
1.1 deraadt 66: switch (ch) {
67: case 'a':
68: odprecede();
69: add("16/1 \"%3_u \" \"\\n\"");
70: break;
71: case 'B':
72: case 'o':
73: odprecede();
74: add("8/2 \" %06o \" \"\\n\"");
75: break;
76: case 'b':
77: odprecede();
78: add("16/1 \"%03o \" \"\\n\"");
79: break;
80: case 'c':
81: odprecede();
82: add("16/1 \"%3_c \" \"\\n\"");
83: break;
84: case 'd':
85: odprecede();
86: add("8/2 \" %05u \" \"\\n\"");
87: break;
88: case 'D':
89: odprecede();
90: add("4/4 \" %010u \" \"\\n\"");
91: break;
92: case 'e': /* undocumented in od */
93: case 'F':
94: odprecede();
95: add("2/8 \" %21.14e \" \"\\n\"");
96: break;
97:
98: case 'f':
99: odprecede();
100: add("4/4 \" %14.7e \" \"\\n\"");
101: break;
102: case 'H':
103: case 'X':
104: odprecede();
105: add("4/4 \" %08x \" \"\\n\"");
106: break;
107: case 'h':
108: case 'x':
109: odprecede();
110: add("8/2 \" %04x \" \"\\n\"");
111: break;
112: case 'I':
113: case 'L':
114: case 'l':
115: odprecede();
116: add("4/4 \" %11d \" \"\\n\"");
117: break;
118: case 'i':
119: odprecede();
120: add("8/2 \" %6d \" \"\\n\"");
121: break;
122: case 'O':
123: odprecede();
124: add("4/4 \" %011o \" \"\\n\"");
125: break;
126: case 'v':
127: vflag = ALL;
128: break;
129: case 'P':
130: case 'p':
131: case 's':
132: case 'w':
133: case '?':
134: default:
1.7 mickey 135: warnx("od(1) has been deprecated for hexdump(1)");
1.1 deraadt 136: if (ch != '?')
1.7 mickey 137: warnx("hexdump(1) compatibility doesn't"
138: "support the -%c option%s",
1.1 deraadt 139: ch, ch == 's' ? "; see strings(1)." : ".");
140: usage();
141: }
142:
143: if (!fshead) {
144: add("\"%07.7_Ao\n\"");
145: add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
146: }
147:
148: argc -= optind;
149: *argvp += optind;
150:
151: odoffset(argc, argvp);
152: }
153:
154: #define ishexdigit(c) \
155: (c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F')
156:
1.4 deraadt 157: void
1.1 deraadt 158: odoffset(argc, argvp)
159: int argc;
160: char ***argvp;
161: {
162: extern off_t skip;
1.8 ! mpech 163: char *num, *p;
1.1 deraadt 164: int base;
165: char *end;
166:
167: /*
168: * The offset syntax of od(1) was genuinely bizarre. First, if
169: * it started with a plus it had to be an offset. Otherwise, if
170: * there were at least two arguments, a number or lower-case 'x'
171: * followed by a number makes it an offset. By default it was
172: * octal; if it started with 'x' or '0x' it was hex. If it ended
173: * in a '.', it was decimal. If a 'b' or 'B' was appended, it
174: * multiplied the number by 512 or 1024 byte units. There was
175: * no way to assign a block count to a hex offset.
176: *
177: * We assumes it's a file if the offset is bad.
178: */
179: p = **argvp;
180: if (!p)
181: return;
182: if (*p != '+' && (argc < 2 ||
183: (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1])))))
184: return;
185:
186: base = 0;
187: /*
188: * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
189: * set base.
190: */
191: if (p[0] == '+')
192: ++p;
193: if (p[0] == 'x' && ishexdigit(p[1])) {
194: ++p;
195: base = 16;
196: } else if (p[0] == '0' && p[1] == 'x') {
197: p += 2;
198: base = 16;
199: }
200:
201: /* skip over the number */
202: if (base == 16)
203: for (num = p; ishexdigit(*p); ++p);
204: else
205: for (num = p; isdigit(*p); ++p);
206:
207: /* check for no number */
208: if (num == p)
209: return;
210:
211: /* if terminates with a '.', base is decimal */
212: if (*p == '.') {
213: if (base)
214: return;
215: base = 10;
216: }
217:
218: skip = strtol(num, &end, base ? base : 8);
219:
220: /* if end isn't the same as p, we got a non-octal digit */
221: if (end != p)
222: skip = 0;
223: else {
224: if (*p) {
225: if (*p == 'b')
226: skip *= 512;
227: else if (*p == 'B')
228: skip *= 1024;
229: ++p;
230: }
231: if (*p)
232: skip = 0;
233: else {
234: ++*argvp;
235: /*
236: * If the offset uses a non-octal base, the base of
237: * the offset is changed as well. This isn't pretty,
238: * but it's easy.
239: */
240: #define TYPE_OFFSET 7
241: if (base == 16) {
242: fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
243: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
244: } else if (base == 10) {
245: fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
246: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
247: }
248: }
249: }
250: }
251:
252: static void
253: odprecede()
254: {
255: static int first = 1;
256:
257: if (first) {
258: first = 0;
259: add("\"%07.7_Ao\n\"");
260: add("\"%07.7_ao \"");
261: } else
262: add("\" \"");
263: }