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