Annotation of src/usr.bin/hexdump/hexsyntax.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: hexsyntax.c,v 1.10 2007/02/06 20:22:19 jmc Exp $ */
1.6 pvalchev 2: /* $NetBSD: hexsyntax.c,v 1.8 1998/04/08 23:48:57 jeremy Exp $ */
1.2 deraadt 3:
1.1 deraadt 4: /*-
1.6 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.7 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.6 pvalchev 34:
35: #include <err.h>
1.9 miod 36: #include <errno.h>
1.1 deraadt 37: #include <stdio.h>
1.4 deraadt 38: #include <stdlib.h>
1.6 pvalchev 39: #include <string.h>
1.4 deraadt 40: #include <unistd.h>
1.6 pvalchev 41:
1.1 deraadt 42: #include "hexdump.h"
43:
44: off_t skip; /* bytes to skip */
45:
1.4 deraadt 46: void
1.8 deraadt 47: newsyntax(int argc, char ***argvp)
1.1 deraadt 48: {
49: int ch;
50: char *p, **argv;
51:
52: argv = *argvp;
1.6 pvalchev 53: while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
1.1 deraadt 54: switch (ch) {
55: case 'b':
56: add("\"%07.7_Ax\n\"");
57: add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
58: break;
59: case 'c':
60: add("\"%07.7_Ax\n\"");
61: add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
62: break;
1.6 pvalchev 63: case 'C':
64: add("\"%08.8_Ax\n\"");
65: add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
66: add("\" |\" 16/1 \"%_p\" \"|\\n\"");
67: break;
1.1 deraadt 68: case 'd':
69: add("\"%07.7_Ax\n\"");
70: add("\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"");
71: break;
72: case 'e':
73: add(optarg);
74: break;
75: case 'f':
76: addfile(optarg);
77: break;
78: case 'n':
1.9 miod 79: errno = 0;
80: if ((length = strtol(optarg, NULL, 0)) < 0 ||
81: errno != 0)
1.6 pvalchev 82: errx(1, "%s: bad length value", optarg);
1.1 deraadt 83: break;
84: case 'o':
85: add("\"%07.7_Ax\n\"");
86: add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
87: break;
88: case 's':
1.9 miod 89: errno = 0;
90: if ((skip = (off_t)strtoll(optarg, &p, 0)) < 0 ||
91: errno != 0)
1.6 pvalchev 92: errx(1, "%s: bad skip value", optarg);
1.1 deraadt 93: switch(*p) {
94: case 'b':
95: skip *= 512;
96: break;
97: case 'k':
98: skip *= 1024;
99: break;
100: case 'm':
101: skip *= 1048576;
102: break;
103: }
104: break;
105: case 'v':
106: vflag = ALL;
107: break;
108: case 'x':
109: add("\"%07.7_Ax\n\"");
110: add("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"");
111: break;
112: case '?':
113: usage();
114: }
115:
116: if (!fshead) {
117: add("\"%07.7_Ax\n\"");
118: add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
119: }
120:
121: *argvp += optind;
122: }
123:
1.4 deraadt 124: void
1.8 deraadt 125: usage(void)
1.1 deraadt 126: {
1.5 mickey 127: extern char *__progname;
1.10 jmc 128: fprintf(stderr, "usage: %s [-bCcdovx] [-e format_string] "
129: "[-f format_file] [-n length]\n"
130: "\t[-s offset] [file ...]\n", __progname);
1.1 deraadt 131: exit(1);
132: }