1 /* $NetBSD: odsyntax.c,v 1.26 2010/02/09 14:06:37 drochner 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>
40 static char sccsid
[] = "@(#)odsyntax.c 8.2 (Berkeley) 5/4/95";
42 __RCSID("$NetBSD: odsyntax.c,v 1.26 2010/02/09 14:06:37 drochner Exp $");
47 #include <sys/types.h>
75 static void odoffset(int, char ***);
76 static void posixtypes(char const *);
79 odsyntax(int argc
, char ***argvp
)
81 static char empty
[] = "", padding
[] = PADDING
;
86 add("\"%07.7_Ao\n\"");
91 while ((ch
= getopt(argc
, argv
,
92 "A:aBbcDdeFfHhIij:LlN:Oot:vXx")) != -1)
96 case 'd': case 'o': case 'x':
97 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = *optarg
;
98 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] =
102 fshead
->nextfu
->fmt
= empty
;
103 fshead
->nextfs
->nextfu
->fmt
= padding
;
106 errx(1, "%s: invalid address base", optarg
);
128 case 'e': /* undocumented in od */
152 if ((skip
= strtol(optarg
, &p
, 0)) < 0)
153 errx(1, "%s: bad skip value", optarg
);
167 if ((length
= atoi(optarg
)) < 0)
168 errx(1, "%s: bad length value", optarg
);
184 if (fshead
->nextfs
->nextfs
== NULL
)
191 odoffset(argc
, argvp
);
194 /* formats used for -t */
196 static const struct odformat odftab
[] = {
197 { 'a', 1, "%3_u", 4 },
198 { 'c', 1, "%3_c", 4 },
199 { 'd', 1, "%4d", 5 },
200 { 'd', 2, "%6d", 6 },
201 { 'd', 4, "%11d", 11 },
202 { 'd', 8, "%20d", 20 },
203 { 'o', 1, "%03o", 4 },
204 { 'o', 2, "%06o", 7 },
205 { 'o', 4, "%011o", 12 },
206 { 'o', 8, "%022o", 23 },
207 { 'u', 1, "%03u" , 4 },
208 { 'u', 2, "%05u" , 6 },
209 { 'u', 4, "%010u", 11 },
210 { 'u', 8, "%020u", 21 },
211 { 'x', 1, "%02x", 3 },
212 { 'x', 2, "%04x", 5 },
213 { 'x', 4, "%08x", 9 },
214 { 'x', 8, "%016x", 17 },
215 { 'f', 4, "%14.7e", 15 },
216 { 'f', 8, "%21.14e", 22 },
221 * Interpret a POSIX-style -t argument.
224 posixtypes(char const *type_string
)
227 char *fmt
, type
, *tmp
;
228 struct odformat
const *odf
;
230 while (*type_string
) {
231 switch ((type
= *type_string
++)) {
237 if (isupper((unsigned char)*type_string
)) {
238 switch(*type_string
) {
240 nbytes
= sizeof(float);
243 nbytes
= sizeof(double);
246 nbytes
= sizeof(long double);
249 warnx("Bad type-size qualifier '%c'",
254 } else if (isdigit((unsigned char)*type_string
)) {
255 nbytes
= strtol(type_string
, &tmp
, 10);
264 if (isupper((unsigned char)*type_string
)) {
265 switch(*type_string
) {
267 nbytes
= sizeof(char);
270 nbytes
= sizeof(short);
273 nbytes
= sizeof(int);
276 nbytes
= sizeof(long);
279 warnx("Bad type-size qualifier '%c'",
284 } else if (isdigit((unsigned char)*type_string
)) {
285 nbytes
= strtol(type_string
, &tmp
, 10);
293 for (odf
= odftab
; odf
->type
!= 0; odf
++)
294 if (odf
->type
== type
&& odf
->nbytes
== nbytes
)
297 errx(1, "%c%d: format not supported", type
, nbytes
);
298 (void)easprintf(&fmt
, "%d/%d \"%*s%s \" \"\\n\"",
300 4 * nbytes
- odf
->minwidth
, "", odf
->format
);
306 odoffset(int argc
, char ***argvp
)
313 * The offset syntax of od(1) was genuinely bizarre. First, if
314 * it started with a plus it had to be an offset. Otherwise, if
315 * there were at least two arguments, a number or lower-case 'x'
316 * followed by a number makes it an offset. By default it was
317 * octal; if it started with 'x' or '0x' it was hex. If it ended
318 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
319 * multiplied the number by 512 or 1024 byte units. There was
320 * no way to assign a block count to a hex offset.
322 * We assume it's a file if the offset is bad.
324 p
= argc
== 1 ? (*argvp
)[0] : (*argvp
)[1];
328 if (*p
!= '+' && (argc
< 2 ||
329 (!isdigit((unsigned char)p
[0]) &&
330 (p
[0] != 'x' || !isxdigit((unsigned char)p
[1])))))
335 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
340 if (p
[0] == 'x' && isxdigit((unsigned char)p
[1])) {
343 } else if (p
[0] == '0' && p
[1] == 'x') {
348 /* skip over the number */
350 for (num
= p
; isxdigit((unsigned char)*p
); ++p
);
352 for (num
= p
; isdigit((unsigned char)*p
); ++p
);
354 /* check for no number */
358 /* if terminates with a '.', base is decimal */
365 skip
= strtol(num
, &end
, base
? base
: 8);
367 /* if end isn't the same as p, we got a non-octal digit */
377 } else if (*p
== 'b') {
387 * If the offset uses a non-octal base, the base of the offset
388 * is changed as well. This isn't pretty, but it's easy.
391 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = 'x';
392 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] = 'x';
393 } else if (base
== 10) {
394 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = 'd';
395 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] = 'd';
398 /* Terminate file list. */