1 /* $NetBSD: odsyntax.c,v 1.24 2006/08/26 18:17:42 christos Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)odsyntax.c 8.2 (Berkeley) 5/4/95";
41 __RCSID("$NetBSD: odsyntax.c,v 1.24 2006/08/26 18:17:42 christos Exp $");
45 #include <sys/types.h>
73 static void odoffset(int, char ***);
74 static void posixtypes(char const *);
77 oldsyntax(int argc
, char ***argvp
)
79 static char empty
[] = "", padding
[] = PADDING
;
84 add("\"%07.7_Ao\n\"");
89 while ((ch
= getopt(argc
, argv
,
90 "A:aBbcDdeFfHhIij:LlN:OoPpst:wvXx")) != -1)
94 case 'd': case 'o': case 'x':
95 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = *optarg
;
96 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] =
100 fshead
->nextfu
->fmt
= empty
;
101 fshead
->nextfs
->nextfu
->fmt
= padding
;
104 errx(1, "%s: invalid address base", optarg
);
126 case 'e': /* undocumented in od */
150 if ((skip
= strtol(optarg
, &p
, 0)) < 0)
151 errx(1, "%s: bad skip value", optarg
);
165 if ((length
= atoi(optarg
)) < 0)
166 errx(1, "%s: bad length value", optarg
);
183 warnx("od(1) has been deprecated for hexdump(1).");
186 "hexdump(1) compatibility doesn't support the -%c option%s\n",
187 ch
, ch
== 's' ? "; see strings(1)." : ".");
191 if (fshead
->nextfs
->nextfs
== NULL
)
198 odoffset(argc
, argvp
);
201 /* formats used for -t */
203 static const struct odformat odftab
[] = {
204 { 'a', 1, "%3_u", 4 },
205 { 'c', 1, "%3_c", 4 },
206 { 'd', 1, "%4d", 5 },
207 { 'd', 2, "%6d", 6 },
208 { 'd', 4, "%11d", 11 },
209 { 'd', 8, "%20d", 20 },
210 { 'o', 1, "%03o", 4 },
211 { 'o', 2, "%06o", 7 },
212 { 'o', 4, "%011o", 12 },
213 { 'o', 8, "%022o", 23 },
214 { 'u', 1, "%03u" , 4 },
215 { 'u', 2, "%05u" , 6 },
216 { 'u', 4, "%010u", 11 },
217 { 'u', 8, "%020u", 21 },
218 { 'x', 1, "%02x", 3 },
219 { 'x', 2, "%04x", 5 },
220 { 'x', 4, "%08x", 9 },
221 { 'x', 8, "%016x", 17 },
222 { 'f', 4, "%14.7e", 15 },
223 { 'f', 8, "%21.14e", 22 },
228 * Interpret a POSIX-style -t argument.
231 posixtypes(char const *type_string
)
234 char *fmt
, type
, *tmp
;
235 struct odformat
const *odf
;
237 while (*type_string
) {
238 switch ((type
= *type_string
++)) {
244 if (isupper((unsigned char)*type_string
)) {
245 switch(*type_string
) {
247 nbytes
= sizeof(float);
250 nbytes
= sizeof(double);
253 nbytes
= sizeof(long double);
256 warnx("Bad type-size qualifier '%c'",
261 } else if (isdigit((unsigned char)*type_string
)) {
262 nbytes
= strtol(type_string
, &tmp
, 10);
271 if (isupper((unsigned char)*type_string
)) {
272 switch(*type_string
) {
274 nbytes
= sizeof(char);
277 nbytes
= sizeof(short);
280 nbytes
= sizeof(int);
283 nbytes
= sizeof(long);
286 warnx("Bad type-size qualifier '%c'",
291 } else if (isdigit((unsigned char)*type_string
)) {
292 nbytes
= strtol(type_string
, &tmp
, 10);
300 for (odf
= odftab
; odf
->type
!= 0; odf
++)
301 if (odf
->type
== type
&& odf
->nbytes
== nbytes
)
304 errx(1, "%c%d: format not supported", type
, nbytes
);
305 (void)easprintf(&fmt
, "%d/%d \"%*s%s \" \"\\n\"",
307 4 * nbytes
- odf
->minwidth
, "", odf
->format
);
313 odoffset(int argc
, char ***argvp
)
320 * The offset syntax of od(1) was genuinely bizarre. First, if
321 * it started with a plus it had to be an offset. Otherwise, if
322 * there were at least two arguments, a number or lower-case 'x'
323 * followed by a number makes it an offset. By default it was
324 * octal; if it started with 'x' or '0x' it was hex. If it ended
325 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
326 * multiplied the number by 512 or 1024 byte units. There was
327 * no way to assign a block count to a hex offset.
329 * We assume it's a file if the offset is bad.
331 p
= argc
== 1 ? (*argvp
)[0] : (*argvp
)[1];
335 if (*p
!= '+' && (argc
< 2 ||
336 (!isdigit((unsigned char)p
[0]) &&
337 (p
[0] != 'x' || !isxdigit((unsigned char)p
[1])))))
342 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
347 if (p
[0] == 'x' && isxdigit((unsigned char)p
[1])) {
350 } else if (p
[0] == '0' && p
[1] == 'x') {
355 /* skip over the number */
357 for (num
= p
; isxdigit((unsigned char)*p
); ++p
);
359 for (num
= p
; isdigit((unsigned char)*p
); ++p
);
361 /* check for no number */
365 /* if terminates with a '.', base is decimal */
372 skip
= strtol(num
, &end
, base
? base
: 8);
374 /* if end isn't the same as p, we got a non-octal digit */
384 } else if (*p
== 'b') {
394 * If the offset uses a non-octal base, the base of the offset
395 * is changed as well. This isn't pretty, but it's easy.
398 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = 'x';
399 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] = 'x';
400 } else if (base
== 10) {
401 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = 'd';
402 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] = 'd';
405 /* Terminate file list. */