Annotation of src/usr.bin/hexdump/odsyntax.c, Revision 1.23
1.23 ! sobrado 1: /* $OpenBSD: odsyntax.c,v 1.22 2013/11/20 21:21:50 deraadt 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.1 deraadt 45: int deprecated;
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.1 deraadt 90: deprecated = 1;
91: argv = *argvp;
1.9 pvalchev 92: while ((ch = getopt(argc, argv,
1.23 ! sobrado 93: "A:aBbcDdeFfHhIij:LlN:Oot: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.9 pvalchev 174: case 't':
175: posixtypes(optarg);
176: break;
1.1 deraadt 177: case 'v':
178: vflag = ALL;
179: break;
180: default:
1.11 millert 181: oldusage();
1.1 deraadt 182: }
183:
1.19 fgsch 184: if (fshead->nextfs->nextfs == NULL)
185: odadd(" 8/2 \"%06o \" \"\\n\"");
1.1 deraadt 186:
187: argc -= optind;
188: *argvp += optind;
189:
1.9 pvalchev 190: if (argc)
191: odoffset(argc, argvp);
1.1 deraadt 192: }
193:
1.9 pvalchev 194: /*
195: * Interpret a POSIX-style -t argument.
196: */
197: static void
1.13 deraadt 198: posixtypes(char *type_string)
1.9 pvalchev 199: {
200: int x, y, nbytes;
1.1 deraadt 201:
1.9 pvalchev 202: while (*type_string) {
203: switch (*type_string) {
204: case 'a':
205: type_string++;
1.19 fgsch 206: odadd("16/1 \"%3_u \" \"\\n\"");
1.9 pvalchev 207: break;
208: case 'c':
209: type_string++;
1.19 fgsch 210: odadd("16/1 \"%3_c \" \"\\n\"");
1.9 pvalchev 211: break;
212: case 'f':
213: type_string++;
214: if (*type_string == 'F' ||
215: *type_string == '4') {
216: type_string++;
1.19 fgsch 217: odadd("4/4 \" %14.7e\" \"\\n\"");
1.9 pvalchev 218: } else if (*type_string == 'L' ||
219: *type_string == '8') {
220: type_string++;
1.19 fgsch 221: odadd("2/8 \" %16.14e\" \"\\n\"");
1.9 pvalchev 222: } else if (*type_string == 'D')
223: /* long doubles vary in size */
1.11 millert 224: oldusage();
1.9 pvalchev 225: else
1.19 fgsch 226: odadd("2/8 \" %16.14e\" \"\\n\"");
1.9 pvalchev 227: break;
228: case 'd':
229: x = 0;
230: goto extensions;
231: case 'o':
232: x = 1;
233: goto extensions;
234: case 'u':
235: x = 2;
236: goto extensions;
237: case 'x':
238: x = 3;
239: extensions:
240: type_string++;
241: y = 2;
1.22 deraadt 242: if (isupper((unsigned char)*type_string)) {
1.9 pvalchev 243: switch(*type_string) {
244: case 'C':
245: nbytes = sizeof(char);
246: break;
247: case 'S':
248: nbytes = sizeof(short);
249: break;
250: case 'I':
251: nbytes = sizeof(int);
252: break;
253: case 'L':
254: nbytes = sizeof(long);
255: break;
256: default:
257: warnx("Bad type-size qualifier '%c'",
258: *type_string);
1.11 millert 259: oldusage();
1.9 pvalchev 260: }
261: type_string++;
1.22 deraadt 262: } else if (isdigit((unsigned char)*type_string))
1.9 pvalchev 263: nbytes = strtol(type_string, &type_string, 10);
1.18 nicm 264: else
265: nbytes = 4;
1.9 pvalchev 266:
267: switch (nbytes) {
268: case 1:
269: y = 0;
270: break;
271: case 2:
272: y = 1;
273: break;
274: case 4:
275: y = 2;
276: break;
277: case 8:
278: y = 3;
279: break;
280: default:
281: warnx("%d-byte integer formats are not "
282: "supported", nbytes);
1.11 millert 283: oldusage();
1.9 pvalchev 284: }
1.19 fgsch 285: odadd(fmt[x][y]);
1.9 pvalchev 286: break;
287: default:
1.11 millert 288: oldusage();
1.9 pvalchev 289: }
290: }
1.11 millert 291: }
292:
293: void
1.13 deraadt 294: oldusage(void)
1.11 millert 295: {
296: extern char *__progname;
1.19 fgsch 297: fprintf(stderr, "usage: %s [-aBbcDdeFfHhIiLlOovXx] [-A base] "
1.20 sobrado 298: "[-j offset] [-N length]\n"
299: "\t[-t type_string] [[+]offset[.][Bb]] [file ...]\n", __progname);
1.11 millert 300: exit(1);
1.9 pvalchev 301: }
302:
303: static void
1.13 deraadt 304: odoffset(int argc, char ***argvp)
1.1 deraadt 305: {
1.8 mpech 306: char *num, *p;
1.1 deraadt 307: int base;
308: char *end;
309:
310: /*
311: * The offset syntax of od(1) was genuinely bizarre. First, if
312: * it started with a plus it had to be an offset. Otherwise, if
313: * there were at least two arguments, a number or lower-case 'x'
314: * followed by a number makes it an offset. By default it was
315: * octal; if it started with 'x' or '0x' it was hex. If it ended
316: * in a '.', it was decimal. If a 'b' or 'B' was appended, it
317: * multiplied the number by 512 or 1024 byte units. There was
318: * no way to assign a block count to a hex offset.
319: *
1.9 pvalchev 320: * We assume it's a file if the offset is bad.
1.1 deraadt 321: */
1.9 pvalchev 322: p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
1.1 deraadt 323: if (!p)
324: return;
1.9 pvalchev 325:
1.1 deraadt 326: if (*p != '+' && (argc < 2 ||
1.9 pvalchev 327: (!isdigit((unsigned char)p[0]) &&
328: (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
1.1 deraadt 329: return;
330:
331: base = 0;
332: /*
333: * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
334: * set base.
335: */
336: if (p[0] == '+')
337: ++p;
1.9 pvalchev 338: if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
1.1 deraadt 339: ++p;
340: base = 16;
341: } else if (p[0] == '0' && p[1] == 'x') {
342: p += 2;
343: base = 16;
344: }
345:
346: /* skip over the number */
347: if (base == 16)
1.9 pvalchev 348: for (num = p; isxdigit((unsigned char)*p); ++p);
1.1 deraadt 349: else
1.9 pvalchev 350: for (num = p; isdigit((unsigned char)*p); ++p);
1.1 deraadt 351:
352: /* check for no number */
353: if (num == p)
354: return;
355:
356: /* if terminates with a '.', base is decimal */
357: if (*p == '.') {
358: if (base)
359: return;
360: base = 10;
361: }
362:
363: skip = strtol(num, &end, base ? base : 8);
364:
365: /* if end isn't the same as p, we got a non-octal digit */
1.9 pvalchev 366: if (end != p) {
1.1 deraadt 367: skip = 0;
1.9 pvalchev 368: return;
369: }
370:
371: if (*p) {
372: if (*p == 'B') {
373: skip *= 1024;
374: ++p;
375: } else if (*p == 'b') {
376: skip *= 512;
1.1 deraadt 377: ++p;
378: }
1.9 pvalchev 379: }
380: if (*p) {
381: skip = 0;
382: return;
383: }
384: /*
385: * If the offset uses a non-octal base, the base of the offset
386: * is changed as well. This isn't pretty, but it's easy.
387: */
388: if (base == 16) {
389: fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
390: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
391: } else if (base == 10) {
392: fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
393: fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
1.1 deraadt 394: }
1.9 pvalchev 395:
396: /* Terminate file list. */
397: (*argvp)[1] = NULL;
1.1 deraadt 398: }
399:
400: static void
1.19 fgsch 401: odadd(const char *fmt)
1.1 deraadt 402: {
1.19 fgsch 403: static int needpad;
1.1 deraadt 404:
1.19 fgsch 405: if (needpad)
406: add("\""PADDING"\"");
407: add(fmt);
408: needpad = 1;
1.1 deraadt 409: }