Annotation of src/usr.bin/hexdump/odsyntax.c, Revision 1.12
1.12 ! millert 1: /* $OpenBSD: odsyntax.c,v 1.11 2002/04/08 15:59:05 millert Exp $ */
1.9 pvalchev 2: /* $NetBSD: odsyntax.c,v 1.15 2001/12/07 15:14:29 bjh21 Exp $ */
1.2 deraadt 3:
1.1 deraadt 4: /*-
1.9 pvalchev 5: * Copyright (c) 1990, 1993
6: * The Regents of the University of California. All rights reserved.
1.1 deraadt 7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.12 ! millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: /*static char sccsid[] = "from: @(#)odsyntax.c 5.4 (Berkeley) 3/8/91";*/
1.12 ! millert 35: static char rcsid[] = "$OpenBSD: odsyntax.c,v 1.11 2002/04/08 15:59:05 millert Exp $";
1.1 deraadt 36: #endif /* not lint */
37:
38: #include <sys/types.h>
1.9 pvalchev 39:
1.4 deraadt 40: #include <ctype.h>
1.7 mickey 41: #include <err.h>
1.9 pvalchev 42: #include <stdio.h>
43: #include <stdlib.h>
44: #include <unistd.h>
45:
1.1 deraadt 46: #include "hexdump.h"
47:
48: int deprecated;
49:
1.10 millert 50: static void odoffset(int, char ***);
51: static void posixtypes(char *);
52: static void odprecede(void);
1.9 pvalchev 53:
54:
55: /*
56: * formats used for -t
57: */
58: static const char *fmt[4][4] = {
59: {
60: "16/1 \"%3d \" \"\\n\"",
61: "8/2 \" %05d \" \"\\n\"",
62: "4/4 \" %010d \" \"\\n\"",
63: "2/8 \" %019d \" \"\\n\""
64: }, {
65: "16/1 \"%03o \" \"\\n\"",
66: "8/2 \" %06o \" \"\\n\"",
67: "4/4 \" %011o\" \"\\n\"",
68: "2/8 \" %022o \" \"\\n\""
69: }, {
70: "16/1 \"%03u \" \"\\n\"",
71: "8/2 \" %05u \" \"\\n\"",
72: "4/4 \" %010u \" \"\\n\"",
73: "2/8 \" %020u \" \"\\n\""
74: }, {
75: "16/1 \" %02x \" \"\\n\"",
76: "8/2 \" %04x \" \"\\n\"",
77: "4/4 \" %08x \" \"\\n\"",
78: "2/8 \" %16x \" \"\\n\""
79: }
80: };
81:
1.6 pvalchev 82: void
1.1 deraadt 83: oldsyntax(argc, argvp)
84: int argc;
85: char ***argvp;
86: {
87: int ch;
1.9 pvalchev 88: char *p, **argv;
1.1 deraadt 89:
90: deprecated = 1;
91: argv = *argvp;
1.9 pvalchev 92: while ((ch = getopt(argc, argv,
93: "aBbcDdeFfHhIij:LlN:OoPpst:wvXx")) != -1)
1.1 deraadt 94: switch (ch) {
95: case 'a':
96: odprecede();
97: add("16/1 \"%3_u \" \"\\n\"");
98: break;
99: case 'B':
100: case 'o':
101: odprecede();
102: add("8/2 \" %06o \" \"\\n\"");
103: break;
104: case 'b':
105: odprecede();
106: add("16/1 \"%03o \" \"\\n\"");
107: break;
108: case 'c':
109: odprecede();
110: add("16/1 \"%3_c \" \"\\n\"");
111: break;
112: case 'd':
113: odprecede();
114: add("8/2 \" %05u \" \"\\n\"");
115: break;
116: case 'D':
117: odprecede();
118: add("4/4 \" %010u \" \"\\n\"");
119: break;
120: case 'e': /* undocumented in od */
121: case 'F':
122: odprecede();
123: add("2/8 \" %21.14e \" \"\\n\"");
124: break;
125:
126: case 'f':
127: odprecede();
128: add("4/4 \" %14.7e \" \"\\n\"");
129: break;
130: case 'H':
131: case 'X':
132: odprecede();
133: add("4/4 \" %08x \" \"\\n\"");
134: break;
135: case 'h':
136: case 'x':
137: odprecede();
138: add("8/2 \" %04x \" \"\\n\"");
139: break;
140: case 'I':
141: case 'L':
142: case 'l':
143: odprecede();
144: add("4/4 \" %11d \" \"\\n\"");
145: break;
146: case 'i':
147: odprecede();
148: add("8/2 \" %6d \" \"\\n\"");
149: break;
1.9 pvalchev 150: case 'j':
151: if ((skip = strtol(optarg, &p, 0)) < 0)
152: errx(1, "%s: bad skip value", optarg);
153: switch(*p) {
154: case 'b':
155: skip *= 512;
156: break;
157: case 'k':
158: skip *= 1024;
159: break;
160: case 'm':
161: skip *= 1048576;
162: break;
163: }
164: break;
165: case 'N':
166: if ((length = atoi(optarg)) < 0)
167: errx(1, "%s: bad length value", optarg);
168: break;
1.1 deraadt 169: case 'O':
170: odprecede();
171: add("4/4 \" %011o \" \"\\n\"");
172: break;
1.9 pvalchev 173: case 't':
174: posixtypes(optarg);
175: break;
1.1 deraadt 176: case 'v':
177: vflag = ALL;
178: break;
179: case 'P':
180: case 'p':
181: case 's':
182: case 'w':
183: case '?':
184: default:
1.9 pvalchev 185: warnx("od(1) has been deprecated for hexdump(1).");
1.1 deraadt 186: if (ch != '?')
1.9 pvalchev 187: warnx(
188: "hexdump(1) compatibility doesn't support the -%c option%s\n",
1.1 deraadt 189: ch, ch == 's' ? "; see strings(1)." : ".");
1.11 millert 190: oldusage();
1.1 deraadt 191: }
192:
193: if (!fshead) {
194: add("\"%07.7_Ao\n\"");
195: add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
196: }
197:
198: argc -= optind;
199: *argvp += optind;
200:
1.9 pvalchev 201: if (argc)
202: odoffset(argc, argvp);
1.1 deraadt 203: }
204:
1.9 pvalchev 205: /*
206: * Interpret a POSIX-style -t argument.
207: */
208: static void
209: posixtypes(type_string)
210: char *type_string;
211: {
212: int x, y, nbytes;
1.1 deraadt 213:
1.9 pvalchev 214: while (*type_string) {
215: odprecede();
216: switch (*type_string) {
217: case 'a':
218: type_string++;
219: add("16/1 \"%3_u \" \"\\n\"");
220: break;
221: case 'c':
222: type_string++;
223: add("16/1 \"%3_c \" \"\\n\"");
224: break;
225: case 'f':
226: type_string++;
227: if (*type_string == 'F' ||
228: *type_string == '4') {
229: type_string++;
230: add("4/4 \" %14.7e\" \"\\n\"");
231: } else if (*type_string == 'L' ||
232: *type_string == '8') {
233: type_string++;
234: add("2/8 \" %16.14e\" \"\\n\"");
235: } else if (*type_string == 'D')
236: /* long doubles vary in size */
1.11 millert 237: oldusage();
1.9 pvalchev 238: else
239: add("2/8 \" %16.14e\" \"\\n\"");
240: break;
241: case 'd':
242: x = 0;
243: goto extensions;
244: case 'o':
245: x = 1;
246: goto extensions;
247: case 'u':
248: x = 2;
249: goto extensions;
250: case 'x':
251: x = 3;
252: extensions:
253: type_string++;
254: y = 2;
255: if (isupper(*type_string)) {
256: switch(*type_string) {
257: case 'C':
258: nbytes = sizeof(char);
259: break;
260: case 'S':
261: nbytes = sizeof(short);
262: break;
263: case 'I':
264: nbytes = sizeof(int);
265: break;
266: case 'L':
267: nbytes = sizeof(long);
268: break;
269: default:
270: warnx("Bad type-size qualifier '%c'",
271: *type_string);
1.11 millert 272: oldusage();
1.9 pvalchev 273: }
274: type_string++;
275: } else if (isdigit(*type_string))
276: nbytes = strtol(type_string, &type_string, 10);
277:
278: switch (nbytes) {
279: case 1:
280: y = 0;
281: break;
282: case 2:
283: y = 1;
284: break;
285: case 4:
286: y = 2;
287: break;
288: case 8:
289: y = 3;
290: break;
291: default:
292: warnx("%d-byte integer formats are not "
293: "supported", nbytes);
1.11 millert 294: oldusage();
1.9 pvalchev 295: }
296: add(fmt[x][y]);
297: break;
298: default:
1.11 millert 299: oldusage();
1.9 pvalchev 300: }
301: }
1.11 millert 302: }
303:
304: void
305: oldusage()
306: {
307: extern char *__progname;
308: fprintf(stderr, "usage: %s [-aBbcDdeFfHhIiLlOovXx] [-j skip] "
309: "[-N length] [-t type_string] "
310: "[[+]offset[.][Bb]] [file ...]\n", __progname);
311: exit(1);
1.9 pvalchev 312: }
313:
314: static void
1.1 deraadt 315: odoffset(argc, argvp)
316: int argc;
317: char ***argvp;
318: {
1.8 mpech 319: char *num, *p;
1.1 deraadt 320: int base;
321: char *end;
322:
323: /*
324: * The offset syntax of od(1) was genuinely bizarre. First, if
325: * it started with a plus it had to be an offset. Otherwise, if
326: * there were at least two arguments, a number or lower-case 'x'
327: * followed by a number makes it an offset. By default it was
328: * octal; if it started with 'x' or '0x' it was hex. If it ended
329: * in a '.', it was decimal. If a 'b' or 'B' was appended, it
330: * multiplied the number by 512 or 1024 byte units. There was
331: * no way to assign a block count to a hex offset.
332: *
1.9 pvalchev 333: * We assume it's a file if the offset is bad.
1.1 deraadt 334: */
1.9 pvalchev 335: p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
1.1 deraadt 336: if (!p)
337: return;
1.9 pvalchev 338:
1.1 deraadt 339: if (*p != '+' && (argc < 2 ||
1.9 pvalchev 340: (!isdigit((unsigned char)p[0]) &&
341: (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
1.1 deraadt 342: return;
343:
344: base = 0;
345: /*
346: * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
347: * set base.
348: */
349: if (p[0] == '+')
350: ++p;
1.9 pvalchev 351: if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
1.1 deraadt 352: ++p;
353: base = 16;
354: } else if (p[0] == '0' && p[1] == 'x') {
355: p += 2;
356: base = 16;
357: }
358:
359: /* skip over the number */
360: if (base == 16)
1.9 pvalchev 361: for (num = p; isxdigit((unsigned char)*p); ++p);
1.1 deraadt 362: else
1.9 pvalchev 363: for (num = p; isdigit((unsigned char)*p); ++p);
1.1 deraadt 364:
365: /* check for no number */
366: if (num == p)
367: return;
368:
369: /* if terminates with a '.', base is decimal */
370: if (*p == '.') {
371: if (base)
372: return;
373: base = 10;
374: }
375:
376: skip = strtol(num, &end, base ? base : 8);
377:
378: /* if end isn't the same as p, we got a non-octal digit */
1.9 pvalchev 379: if (end != p) {
1.1 deraadt 380: skip = 0;
1.9 pvalchev 381: return;
382: }
383:
384: if (*p) {
385: if (*p == 'B') {
386: skip *= 1024;
387: ++p;
388: } else if (*p == 'b') {
389: skip *= 512;
1.1 deraadt 390: ++p;
391: }
1.9 pvalchev 392: }
393: if (*p) {
394: skip = 0;
395: return;
396: }
397: /*
398: * If the offset uses a non-octal base, the base of the offset
399: * is changed as well. This isn't pretty, but it's easy.
400: */
1.1 deraadt 401: #define TYPE_OFFSET 7
1.9 pvalchev 402: if (base == 16) {
403: fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
404: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
405: } else if (base == 10) {
406: fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
407: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
1.1 deraadt 408: }
1.9 pvalchev 409:
410: /* Terminate file list. */
411: (*argvp)[1] = NULL;
1.1 deraadt 412: }
413:
414: static void
415: odprecede()
416: {
417: static int first = 1;
418:
419: if (first) {
420: first = 0;
421: add("\"%07.7_Ao\n\"");
422: add("\"%07.7_ao \"");
423: } else
424: add("\" \"");
425: }