Annotation of src/usr.bin/hexdump/odsyntax.c, Revision 1.26
1.26 ! schwarze 1: /* $OpenBSD: odsyntax.c,v 1.25 2014/04/19 09:28:20 sobrado 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: #include <sys/types.h>
1.9 pvalchev 34:
1.4 deraadt 35: #include <ctype.h>
1.7 mickey 36: #include <err.h>
1.9 pvalchev 37: #include <stdio.h>
38: #include <stdlib.h>
39: #include <unistd.h>
40:
1.1 deraadt 41: #include "hexdump.h"
42:
1.19 fgsch 43: #define PADDING " "
44:
1.25 sobrado 45: int odmode;
1.1 deraadt 46:
1.10 millert 47: static void odoffset(int, char ***);
48: static void posixtypes(char *);
1.19 fgsch 49: static void odadd(const char *);
1.9 pvalchev 50:
51:
52: /*
53: * formats used for -t
54: */
55: static const char *fmt[4][4] = {
56: {
57: "16/1 \"%3d \" \"\\n\"",
58: "8/2 \" %05d \" \"\\n\"",
59: "4/4 \" %010d \" \"\\n\"",
60: "2/8 \" %019d \" \"\\n\""
61: }, {
62: "16/1 \"%03o \" \"\\n\"",
63: "8/2 \" %06o \" \"\\n\"",
64: "4/4 \" %011o\" \"\\n\"",
65: "2/8 \" %022o \" \"\\n\""
66: }, {
67: "16/1 \"%03u \" \"\\n\"",
68: "8/2 \" %05u \" \"\\n\"",
69: "4/4 \" %010u \" \"\\n\"",
70: "2/8 \" %020u \" \"\\n\""
71: }, {
72: "16/1 \" %02x \" \"\\n\"",
73: "8/2 \" %04x \" \"\\n\"",
74: "4/4 \" %08x \" \"\\n\"",
75: "2/8 \" %16x \" \"\\n\""
76: }
77: };
78:
1.6 pvalchev 79: void
1.13 deraadt 80: oldsyntax(int argc, char ***argvp)
1.1 deraadt 81: {
1.19 fgsch 82: static char empty[] = "", padding[] = PADDING;
1.1 deraadt 83: int ch;
1.9 pvalchev 84: char *p, **argv;
1.1 deraadt 85:
1.19 fgsch 86: #define TYPE_OFFSET 7
87: add("\"%07.7_Ao\n\"");
88: add("\"%07.7_ao \"");
89:
1.25 sobrado 90: odmode = 1;
1.1 deraadt 91: argv = *argvp;
1.9 pvalchev 92: while ((ch = getopt(argc, argv,
1.24 sobrado 93: "A:aBbcDdeFfHhIij:LlN:Oost:vXx")) != -1)
1.1 deraadt 94: switch (ch) {
1.19 fgsch 95: case 'A':
96: switch (*optarg) {
97: case 'd': case 'o': case 'x':
98: fshead->nextfu->fmt[TYPE_OFFSET] = *optarg;
99: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] =
100: *optarg;
101: break;
102: case 'n':
103: fshead->nextfu->fmt = empty;
104: fshead->nextfs->nextfu->fmt = padding;
105: break;
106: default:
107: errx(1, "%s: invalid address base", optarg);
108: }
109: break;
1.1 deraadt 110: case 'a':
1.19 fgsch 111: odadd("16/1 \"%3_u \" \"\\n\"");
1.1 deraadt 112: break;
113: case 'B':
114: case 'o':
1.19 fgsch 115: odadd("8/2 \" %06o \" \"\\n\"");
1.1 deraadt 116: break;
117: case 'b':
1.19 fgsch 118: odadd("16/1 \"%03o \" \"\\n\"");
1.1 deraadt 119: break;
120: case 'c':
1.19 fgsch 121: odadd("16/1 \"%3_c \" \"\\n\"");
1.1 deraadt 122: break;
123: case 'd':
1.19 fgsch 124: odadd("8/2 \" %05u \" \"\\n\"");
1.1 deraadt 125: break;
126: case 'D':
1.19 fgsch 127: odadd("4/4 \" %010u \" \"\\n\"");
1.1 deraadt 128: break;
1.16 jmc 129: case 'e':
1.1 deraadt 130: case 'F':
1.19 fgsch 131: odadd("2/8 \" %21.14e \" \"\\n\"");
1.1 deraadt 132: break;
133: case 'f':
1.19 fgsch 134: odadd("4/4 \" %14.7e \" \"\\n\"");
1.1 deraadt 135: break;
136: case 'H':
137: case 'X':
1.19 fgsch 138: odadd("4/4 \" %08x \" \"\\n\"");
1.1 deraadt 139: break;
140: case 'h':
141: case 'x':
1.19 fgsch 142: odadd("8/2 \" %04x \" \"\\n\"");
1.1 deraadt 143: break;
144: case 'I':
145: case 'L':
146: case 'l':
1.19 fgsch 147: odadd("4/4 \" %11d \" \"\\n\"");
1.1 deraadt 148: break;
149: case 'i':
1.19 fgsch 150: odadd("8/2 \" %6d \" \"\\n\"");
1.1 deraadt 151: break;
1.9 pvalchev 152: case 'j':
153: if ((skip = strtol(optarg, &p, 0)) < 0)
154: errx(1, "%s: bad skip value", optarg);
155: switch(*p) {
156: case 'b':
157: skip *= 512;
158: break;
159: case 'k':
160: skip *= 1024;
161: break;
162: case 'm':
163: skip *= 1048576;
164: break;
165: }
166: break;
167: case 'N':
168: if ((length = atoi(optarg)) < 0)
169: errx(1, "%s: bad length value", optarg);
170: break;
1.1 deraadt 171: case 'O':
1.19 fgsch 172: odadd("4/4 \" %011o \" \"\\n\"");
1.1 deraadt 173: break;
1.24 sobrado 174: case 's':
175: odadd("8/2 \" %05d \" \"\\n\"");
176: break;
1.9 pvalchev 177: case 't':
178: posixtypes(optarg);
179: break;
1.1 deraadt 180: case 'v':
181: vflag = ALL;
182: break;
183: default:
1.11 millert 184: oldusage();
1.1 deraadt 185: }
186:
1.19 fgsch 187: if (fshead->nextfs->nextfs == NULL)
188: odadd(" 8/2 \"%06o \" \"\\n\"");
1.1 deraadt 189:
190: argc -= optind;
191: *argvp += optind;
192:
1.9 pvalchev 193: if (argc)
194: odoffset(argc, argvp);
1.1 deraadt 195: }
196:
1.9 pvalchev 197: /*
198: * Interpret a POSIX-style -t argument.
199: */
200: static void
1.13 deraadt 201: posixtypes(char *type_string)
1.9 pvalchev 202: {
203: int x, y, nbytes;
1.1 deraadt 204:
1.9 pvalchev 205: while (*type_string) {
206: switch (*type_string) {
207: case 'a':
208: type_string++;
1.19 fgsch 209: odadd("16/1 \"%3_u \" \"\\n\"");
1.9 pvalchev 210: break;
211: case 'c':
212: type_string++;
1.19 fgsch 213: odadd("16/1 \"%3_c \" \"\\n\"");
1.9 pvalchev 214: break;
215: case 'f':
216: type_string++;
217: if (*type_string == 'F' ||
218: *type_string == '4') {
219: type_string++;
1.19 fgsch 220: odadd("4/4 \" %14.7e\" \"\\n\"");
1.9 pvalchev 221: } else if (*type_string == 'L' ||
222: *type_string == '8') {
223: type_string++;
1.19 fgsch 224: odadd("2/8 \" %16.14e\" \"\\n\"");
1.9 pvalchev 225: } else if (*type_string == 'D')
226: /* long doubles vary in size */
1.11 millert 227: oldusage();
1.9 pvalchev 228: else
1.19 fgsch 229: odadd("2/8 \" %16.14e\" \"\\n\"");
1.9 pvalchev 230: break;
231: case 'd':
232: x = 0;
233: goto extensions;
234: case 'o':
235: x = 1;
236: goto extensions;
237: case 'u':
238: x = 2;
239: goto extensions;
240: case 'x':
241: x = 3;
242: extensions:
243: type_string++;
244: y = 2;
1.22 deraadt 245: if (isupper((unsigned char)*type_string)) {
1.9 pvalchev 246: switch(*type_string) {
247: case 'C':
248: nbytes = sizeof(char);
249: break;
250: case 'S':
251: nbytes = sizeof(short);
252: break;
253: case 'I':
254: nbytes = sizeof(int);
255: break;
256: case 'L':
257: nbytes = sizeof(long);
258: break;
259: default:
260: warnx("Bad type-size qualifier '%c'",
261: *type_string);
1.11 millert 262: oldusage();
1.9 pvalchev 263: }
264: type_string++;
1.22 deraadt 265: } else if (isdigit((unsigned char)*type_string))
1.9 pvalchev 266: nbytes = strtol(type_string, &type_string, 10);
1.18 nicm 267: else
268: nbytes = 4;
1.9 pvalchev 269:
270: switch (nbytes) {
271: case 1:
272: y = 0;
273: break;
274: case 2:
275: y = 1;
276: break;
277: case 4:
278: y = 2;
279: break;
280: case 8:
281: y = 3;
282: break;
283: default:
284: warnx("%d-byte integer formats are not "
285: "supported", nbytes);
1.11 millert 286: oldusage();
1.9 pvalchev 287: }
1.19 fgsch 288: odadd(fmt[x][y]);
1.9 pvalchev 289: break;
290: default:
1.11 millert 291: oldusage();
1.9 pvalchev 292: }
293: }
1.11 millert 294: }
295:
296: void
1.13 deraadt 297: oldusage(void)
1.11 millert 298: {
299: extern char *__progname;
1.24 sobrado 300: fprintf(stderr, "usage: %s [-aBbcDdeFfHhIiLlOosvXx] [-A base] "
1.20 sobrado 301: "[-j offset] [-N length]\n"
1.26 ! schwarze 302: "\t[-t type_string] [file ...]\n", __progname);
1.11 millert 303: exit(1);
1.9 pvalchev 304: }
305:
306: static void
1.13 deraadt 307: odoffset(int argc, char ***argvp)
1.1 deraadt 308: {
1.8 mpech 309: char *num, *p;
1.1 deraadt 310: int base;
311: char *end;
312:
313: /*
314: * The offset syntax of od(1) was genuinely bizarre. First, if
315: * it started with a plus it had to be an offset. Otherwise, if
316: * there were at least two arguments, a number or lower-case 'x'
317: * followed by a number makes it an offset. By default it was
318: * octal; if it started with 'x' or '0x' it was hex. If it ended
319: * in a '.', it was decimal. If a 'b' or 'B' was appended, it
320: * multiplied the number by 512 or 1024 byte units. There was
321: * no way to assign a block count to a hex offset.
322: *
1.9 pvalchev 323: * We assume it's a file if the offset is bad.
1.1 deraadt 324: */
1.9 pvalchev 325: p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
1.1 deraadt 326: if (!p)
327: return;
1.9 pvalchev 328:
1.1 deraadt 329: if (*p != '+' && (argc < 2 ||
1.9 pvalchev 330: (!isdigit((unsigned char)p[0]) &&
331: (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
1.1 deraadt 332: return;
333:
334: base = 0;
335: /*
336: * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
337: * set base.
338: */
339: if (p[0] == '+')
340: ++p;
1.9 pvalchev 341: if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
1.1 deraadt 342: ++p;
343: base = 16;
344: } else if (p[0] == '0' && p[1] == 'x') {
345: p += 2;
346: base = 16;
347: }
348:
349: /* skip over the number */
350: if (base == 16)
1.9 pvalchev 351: for (num = p; isxdigit((unsigned char)*p); ++p);
1.1 deraadt 352: else
1.9 pvalchev 353: for (num = p; isdigit((unsigned char)*p); ++p);
1.1 deraadt 354:
355: /* check for no number */
356: if (num == p)
357: return;
358:
359: /* if terminates with a '.', base is decimal */
360: if (*p == '.') {
361: if (base)
362: return;
363: base = 10;
364: }
365:
366: skip = strtol(num, &end, base ? base : 8);
367:
368: /* if end isn't the same as p, we got a non-octal digit */
1.9 pvalchev 369: if (end != p) {
1.1 deraadt 370: skip = 0;
1.9 pvalchev 371: return;
372: }
373:
1.26 ! schwarze 374: if (*p == '.')
! 375: ++p;
1.9 pvalchev 376: if (*p) {
377: if (*p == 'B') {
378: skip *= 1024;
379: ++p;
380: } else if (*p == 'b') {
381: skip *= 512;
1.1 deraadt 382: ++p;
383: }
1.9 pvalchev 384: }
385: if (*p) {
386: skip = 0;
387: return;
388: }
389: /*
390: * If the offset uses a non-octal base, the base of the offset
391: * is changed as well. This isn't pretty, but it's easy.
392: */
393: if (base == 16) {
394: fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
395: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
396: } else if (base == 10) {
397: fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
398: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
1.1 deraadt 399: }
1.9 pvalchev 400:
401: /* Terminate file list. */
1.26 ! schwarze 402: (*argvp)[argc > 1] = NULL;
1.1 deraadt 403: }
404:
405: static void
1.19 fgsch 406: odadd(const char *fmt)
1.1 deraadt 407: {
1.19 fgsch 408: static int needpad;
1.1 deraadt 409:
1.19 fgsch 410: if (needpad)
411: add("\""PADDING"\"");
412: add(fmt);
413: needpad = 1;
1.1 deraadt 414: }