odsyntax.c revision 96787
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 96787 2002-05-17 05:20:30Z tjr $";
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 odmode;
53
54static void odoffset(int, char ***);
55static void odprecede(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	odmode = 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			if (ch != '?')
140				warnx("hexdump(1) compatibility doesn't support the -%c option%s",
141				    ch, ch == 's' ? "; see strings(1)" : "");
142			usage();
143		}
144
145	if (!fshead) {
146		add("\"%07.7_Ao\n\"");
147		add("\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
148	}
149
150	argc -= optind;
151	*argvp += optind;
152
153	if (argc)
154		odoffset(argc, argvp);
155}
156
157static void
158odoffset(argc, argvp)
159	int argc;
160	char ***argvp;
161{
162	extern off_t skip;
163	unsigned char *p, *num, *end;
164	int base;
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 assume it's a file if the offset is bad.
177	 */
178	p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
179
180	if (*p != '+' && (argc < 2 ||
181	    (!isdigit(p[0]) && (p[0] != 'x' || !isxdigit(p[1])))))
182		return;
183
184	base = 0;
185	/*
186	 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
187	 * set base.
188	 */
189	if (p[0] == '+')
190		++p;
191	if (p[0] == 'x' && isxdigit(p[1])) {
192		++p;
193		base = 16;
194	} else if (p[0] == '0' && p[1] == 'x') {
195		p += 2;
196		base = 16;
197	}
198
199	/* skip over the number */
200	if (base == 16)
201		for (num = p; isxdigit(*p); ++p);
202	else
203		for (num = p; isdigit(*p); ++p);
204
205	/* check for no number */
206	if (num == p)
207		return;
208
209	/* if terminates with a '.', base is decimal */
210	if (*p == '.') {
211		if (base)
212			return;
213		base = 10;
214	}
215
216	skip = strtoll(num, (char **)&end, base ? base : 8);
217
218	/* if end isn't the same as p, we got a non-octal digit */
219	if (end != p) {
220		skip = 0;
221		return;
222	}
223
224	if (*p) {
225		if (*p == 'B') {
226			skip *= 1024;
227			++p;
228		} else if (*p == 'b') {
229			skip *= 512;
230			++p;
231		}
232	}
233
234	if (*p) {
235		skip = 0;
236		return;
237	}
238
239	/*
240	 * If the offset uses a non-octal base, the base of the offset
241	 * is changed as well.  This isn't pretty, but it's easy.
242	 */
243#define	TYPE_OFFSET	7
244	if (base == 16) {
245		fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
246		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
247	} else if (base == 10) {
248		fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
249		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
250	}
251
252	/* Terminate file list. */
253	(*argvp)[1] = NULL;
254}
255
256static void
257odprecede()
258{
259	static int first = 1;
260
261	if (first) {
262		first = 0;
263		add("\"%07.7_Ao\n\"");
264		add("\"%07.7_ao  \"");
265	} else
266		add("\"         \"");
267}
268