odsyntax.c revision 82766
1/*-
2 * Copyright (c) 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)odsyntax.c	8.2 (Berkeley) 5/4/95";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/usr.bin/hexdump/odsyntax.c 82766 2001-09-01 22:42:47Z ache $";
40#endif /* not lint */
41
42#include <sys/types.h>
43
44#include <ctype.h>
45#include <err.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <unistd.h>
49
50#include "hexdump.h"
51
52int deprecated;
53
54static void odoffset __P((int, char ***));
55static void odprecede __P((void));
56
57void
58oldsyntax(argc, argvp)
59	int argc;
60	char ***argvp;
61{
62	extern enum _vflag vflag;
63	extern FS *fshead;
64	int ch;
65	char **argv;
66
67	deprecated = 1;
68	argv = *argvp;
69	while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != -1)
70		switch (ch) {
71		case 'a':
72			odprecede();
73			add("16/1 \"%3_u \" \"\\n\"");
74			break;
75		case 'B':
76		case 'o':
77			odprecede();
78			add("8/2 \" %06o \" \"\\n\"");
79			break;
80		case 'b':
81			odprecede();
82			add("16/1 \"%03o \" \"\\n\"");
83			break;
84		case 'c':
85			odprecede();
86			add("16/1 \"%3_c \" \"\\n\"");
87			break;
88		case 'd':
89			odprecede();
90			add("8/2 \"  %05u \" \"\\n\"");
91			break;
92		case 'D':
93			odprecede();
94			add("4/4 \"     %010u \" \"\\n\"");
95			break;
96		case 'e':		/* undocumented in od */
97		case 'F':
98			odprecede();
99			add("2/8 \"          %21.14e \" \"\\n\"");
100			break;
101
102		case 'f':
103			odprecede();
104			add("4/4 \" %14.7e \" \"\\n\"");
105			break;
106		case 'H':
107		case 'X':
108			odprecede();
109			add("4/4 \"       %08x \" \"\\n\"");
110			break;
111		case 'h':
112		case 'x':
113			odprecede();
114			add("8/2 \"   %04x \" \"\\n\"");
115			break;
116		case 'I':
117		case 'L':
118		case 'l':
119			odprecede();
120			add("4/4 \"    %11d \" \"\\n\"");
121			break;
122		case 'i':
123			odprecede();
124			add("8/2 \" %6d \" \"\\n\"");
125			break;
126		case 'O':
127			odprecede();
128			add("4/4 \"    %011o \" \"\\n\"");
129			break;
130		case 'v':
131			vflag = ALL;
132			break;
133		case 'P':
134		case 'p':
135		case 's':
136		case 'w':
137		case '?':
138		default:
139			warnx("od(1) has been deprecated for hexdump(1)");
140			if (ch != '?')
141				warnx("hexdump(1) compatibility doesn't support the -%c option%s",
142				    ch, ch == 's' ? "; see strings(1)" : "");
143			usage();
144		}
145
146	if (!fshead) {
147		add("\"%07.7_Ao\n\"");
148		add("\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
149	}
150
151	argc -= optind;
152	*argvp += optind;
153
154	if (argc)
155		odoffset(argc, argvp);
156}
157
158static void
159odoffset(argc, argvp)
160	int argc;
161	char ***argvp;
162{
163	extern off_t skip;
164	unsigned char *p, *num, *end;
165	int base;
166
167	/*
168	 * The offset syntax of od(1) was genuinely bizarre.  First, if
169	 * it started with a plus it had to be an offset.  Otherwise, if
170	 * there were at least two arguments, a number or lower-case 'x'
171	 * followed by a number makes it an offset.  By default it was
172	 * octal; if it started with 'x' or '0x' it was hex.  If it ended
173	 * in a '.', it was decimal.  If a 'b' or 'B' was appended, it
174	 * multiplied the number by 512 or 1024 byte units.  There was
175	 * no way to assign a block count to a hex offset.
176	 *
177	 * We assume it's a file if the offset is bad.
178	 */
179	p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
180
181	if (*p != '+' && (argc < 2 ||
182	    (!isdigit(p[0]) && (p[0] != 'x' || !isxdigit(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' && isxdigit(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; isxdigit(*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 = strtoll(num, (char **)&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		return;
223	}
224
225	if (*p) {
226		if (*p == 'B') {
227			skip *= 1024;
228			++p;
229		} else if (*p == 'b') {
230			skip *= 512;
231			++p;
232		}
233	}
234
235	if (*p) {
236		skip = 0;
237		return;
238	}
239
240	/*
241	 * If the offset uses a non-octal base, the base of the offset
242	 * is changed as well.  This isn't pretty, but it's easy.
243	 */
244#define	TYPE_OFFSET	7
245	if (base == 16) {
246		fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
247		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
248	} else if (base == 10) {
249		fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
250		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
251	}
252
253	/* Terminate file list. */
254	(*argvp)[1] = NULL;
255}
256
257static void
258odprecede()
259{
260	static int first = 1;
261
262	if (first) {
263		first = 0;
264		add("\"%07.7_Ao\n\"");
265		add("\"%07.7_ao  \"");
266	} else
267		add("\"         \"");
268}
269