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