1 /** $MirOS: src/usr.bin/hexdump/hexsyntax.c,v 1.3 2008/11/08 23:04:28 tg Exp $ */
2 /* $OpenBSD: hexsyntax.c,v 1.8 2003/06/12 20:58:09 deraadt Exp $ */
3 /* $NetBSD: hexsyntax.c,v 1.8 1998/04/08 23:48:57 jeremy Exp $ */
4
5 /*-
6 * Copyright (c) 1990, 1993
7 * The Regents of the University of California. All rights reserved.
8 * Copyright (c) 2002, 2003, 2004
9 * Thorsten "mirabilos" Glaser <tg@mirbsd.org>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/types.h>
37
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include "hexdump.h"
45
46 __SCCSID("@(#)hexsyntax.c 5.2 (Berkeley) 5/8/90");
47 __RCSID("$MirOS: src/usr.bin/hexdump/hexsyntax.c,v 1.3 2008/11/08 23:04:28 tg Exp $");
48
49 off_t skip; /* bytes to skip */
50
51 void
newsyntax(int argc,char *** argvp)52 newsyntax(int argc, char ***argvp)
53 {
54 int ch, hexmode = 0;
55 char *p, **argv, *myname;
56
57 argv = *argvp;
58 myname = strrchr(argv[0], '/');
59 myname = (myname ? (myname + 1) : argv[0]);
60 if (!strcmp(myname, "hd"))
61 ++hexmode;
62
63 while ((ch = getopt(argc, argv, "bcCde:f:Hn:os:vx")) != -1)
64 switch (ch) {
65 case 'b':
66 add("\"%07.7_Ax\n\"");
67 add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
68 break;
69 case 'c':
70 add("\"%07.7_Ax\n\"");
71 add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
72 break;
73 case 'C':
74 add("\"%08.8_Ax\n\"");
75 add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
76 add("\" |\" 16/1 \"%_p\" \"|\\n\"");
77 break;
78 case 'd':
79 add("\"%07.7_Ax\n\"");
80 add("\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"");
81 break;
82 case 'e':
83 add(optarg);
84 break;
85 case 'f':
86 addfile(optarg);
87 break;
88 case 'H':
89 add("\"%08.8_aX \" 8/1 \"%02X \" \" - \" 8/1 \"%02X \"");
90 add("\" |\" \"%_p\"");
91 add("\"|\n\"");
92 break;
93 case 'n':
94 if ((length = atoi(optarg)) < 0)
95 errx(1, "%s: bad length value", optarg);
96 break;
97 case 'o':
98 add("\"%07.7_Ax\n\"");
99 add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
100 break;
101 case 's':
102 if ((skip = strtol(optarg, &p, 0)) < 0)
103 errx(1, "%s: bad skip value", optarg);
104 switch(*p) {
105 case 'b':
106 skip *= 512;
107 break;
108 case 'k':
109 skip *= 1024;
110 break;
111 case 'm':
112 skip *= 1048576;
113 break;
114 }
115 break;
116 case 'v':
117 vflag = ALL;
118 break;
119 case 'x':
120 add("\"%07.7_Ax\n\"");
121 add("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"");
122 break;
123 case '?':
124 usage();
125 }
126
127 if (!fshead) {
128 if (hexmode) {
129 add("\"%08.8_aX \" 8/1 \"%02X \" \" - \" 8/1 \"%02X \"");
130 add("\" |\" \"%_p\"");
131 add("\"|\n\"");
132 } else {
133 add("\"%07.7_Ax\n\"");
134 add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
135 }
136 }
137
138 *argvp += optind;
139 }
140
141 void
usage(void)142 usage(void)
143 {
144 extern char *__progname;
145 fprintf(stderr, "usage: %s [-bcCdovx] [-e fmt] [-f fmt_file] "
146 "[-n length] [-s skip] [file ...]\n", __progname);
147 exit(1);
148 }
149