xref: /openbsd-src/usr.bin/hexdump/odsyntax.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: odsyntax.c,v 1.5 2001/07/18 17:17:39 pvalchev Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. 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 #ifndef lint
37 /*static char sccsid[] = "from: @(#)odsyntax.c	5.4 (Berkeley) 3/8/91";*/
38 static char rcsid[] = "$OpenBSD: odsyntax.c,v 1.5 2001/07/18 17:17:39 pvalchev Exp $";
39 #endif /* not lint */
40 
41 #include <sys/types.h>
42 #include <stdlib.h>
43 #include <ctype.h>
44 #include <stdio.h>
45 #include "hexdump.h"
46 
47 int deprecated;
48 
49 oldsyntax(argc, argvp)
50 	int argc;
51 	char ***argvp;
52 {
53 	extern enum _vflag vflag;
54 	extern FS *fshead;
55 	extern char *optarg;
56 	extern int optind;
57 	int ch;
58 	char **argv;
59 	static void odprecede();
60 
61 	deprecated = 1;
62 	argv = *argvp;
63 	while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != -1)
64 		switch (ch) {
65 		case 'a':
66 			odprecede();
67 			add("16/1 \"%3_u \" \"\\n\"");
68 			break;
69 		case 'B':
70 		case 'o':
71 			odprecede();
72 			add("8/2 \" %06o \" \"\\n\"");
73 			break;
74 		case 'b':
75 			odprecede();
76 			add("16/1 \"%03o \" \"\\n\"");
77 			break;
78 		case 'c':
79 			odprecede();
80 			add("16/1 \"%3_c \" \"\\n\"");
81 			break;
82 		case 'd':
83 			odprecede();
84 			add("8/2 \"  %05u \" \"\\n\"");
85 			break;
86 		case 'D':
87 			odprecede();
88 			add("4/4 \"     %010u \" \"\\n\"");
89 			break;
90 		case 'e':		/* undocumented in od */
91 		case 'F':
92 			odprecede();
93 			add("2/8 \"          %21.14e \" \"\\n\"");
94 			break;
95 
96 		case 'f':
97 			odprecede();
98 			add("4/4 \" %14.7e \" \"\\n\"");
99 			break;
100 		case 'H':
101 		case 'X':
102 			odprecede();
103 			add("4/4 \"       %08x \" \"\\n\"");
104 			break;
105 		case 'h':
106 		case 'x':
107 			odprecede();
108 			add("8/2 \"   %04x \" \"\\n\"");
109 			break;
110 		case 'I':
111 		case 'L':
112 		case 'l':
113 			odprecede();
114 			add("4/4 \"    %11d \" \"\\n\"");
115 			break;
116 		case 'i':
117 			odprecede();
118 			add("8/2 \" %6d \" \"\\n\"");
119 			break;
120 		case 'O':
121 			odprecede();
122 			add("4/4 \"    %011o \" \"\\n\"");
123 			break;
124 		case 'v':
125 			vflag = ALL;
126 			break;
127 		case 'P':
128 		case 'p':
129 		case 's':
130 		case 'w':
131 		case '?':
132 		default:
133 			(void)fprintf(stderr,
134 			    "od: od(1) has been deprecated for hexdump(1).\n");
135 			if (ch != '?')
136 				(void)fprintf(stderr,
137 "od: hexdump(1) compatibility doesn't support the -%c option%s\n",
138 				    ch, ch == 's' ? "; see strings(1)." : ".");
139 			usage();
140 		}
141 
142 	if (!fshead) {
143 		add("\"%07.7_Ao\n\"");
144 		add("\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
145 	}
146 
147 	argc -= optind;
148 	*argvp += optind;
149 
150 	odoffset(argc, argvp);
151 }
152 
153 #define	ishexdigit(c) \
154 	(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F')
155 
156 void
157 odoffset(argc, argvp)
158 	int argc;
159 	char ***argvp;
160 {
161 	extern off_t skip;
162 	register char *num, *p;
163 	int base;
164 	char *end;
165 
166 	/*
167 	 * The offset syntax of od(1) was genuinely bizarre.  First, if
168 	 * it started with a plus it had to be an offset.  Otherwise, if
169 	 * there were at least two arguments, a number or lower-case 'x'
170 	 * followed by a number makes it an offset.  By default it was
171 	 * octal; if it started with 'x' or '0x' it was hex.  If it ended
172 	 * in a '.', it was decimal.  If a 'b' or 'B' was appended, it
173 	 * multiplied the number by 512 or 1024 byte units.  There was
174 	 * no way to assign a block count to a hex offset.
175 	 *
176 	 * We assumes it's a file if the offset is bad.
177 	 */
178 	p = **argvp;
179 	if (!p)
180 		return;
181 	if (*p != '+' && (argc < 2 ||
182 	    (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1])))))
183 		return;
184 
185 	base = 0;
186 	/*
187 	 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
188 	 * set base.
189 	 */
190 	if (p[0] == '+')
191 		++p;
192 	if (p[0] == 'x' && ishexdigit(p[1])) {
193 		++p;
194 		base = 16;
195 	} else if (p[0] == '0' && p[1] == 'x') {
196 		p += 2;
197 		base = 16;
198 	}
199 
200 	/* skip over the number */
201 	if (base == 16)
202 		for (num = p; ishexdigit(*p); ++p);
203 	else
204 		for (num = p; isdigit(*p); ++p);
205 
206 	/* check for no number */
207 	if (num == p)
208 		return;
209 
210 	/* if terminates with a '.', base is decimal */
211 	if (*p == '.') {
212 		if (base)
213 			return;
214 		base = 10;
215 	}
216 
217 	skip = strtol(num, &end, base ? base : 8);
218 
219 	/* if end isn't the same as p, we got a non-octal digit */
220 	if (end != p)
221 		skip = 0;
222 	else {
223 		if (*p) {
224 			if (*p == 'b')
225 				skip *= 512;
226 			else if (*p == 'B')
227 				skip *= 1024;
228 			++p;
229 		}
230 		if (*p)
231 			skip = 0;
232 		else {
233 			++*argvp;
234 			/*
235 			 * If the offset uses a non-octal base, the base of
236 			 * the offset is changed as well.  This isn't pretty,
237 			 * but it's easy.
238 			 */
239 #define	TYPE_OFFSET	7
240 			if (base == 16) {
241 				fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
242 				fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
243 			} else if (base == 10) {
244 				fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
245 				fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
246 			}
247 		}
248 	}
249 }
250 
251 static void
252 odprecede()
253 {
254 	static int first = 1;
255 
256 	if (first) {
257 		first = 0;
258 		add("\"%07.7_Ao\n\"");
259 		add("\"%07.7_ao  \"");
260 	} else
261 		add("\"         \"");
262 }
263