1 /* $NetBSD: odsyntax.c,v 1.28 2010/11/27 20:46:38 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.28 2010/11/27 20:46:38 christos Exp $");
45 #include <sys/types.h>
73 static void odoffset(int, char ***);
74 static void posixtypes(char const *);
77 odsyntax(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:Oot:vXx")) != -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
);
182 if (fshead
->nextfs
->nextfs
== NULL
)
189 odoffset(argc
, argvp
);
192 /* formats used for -t */
194 static const struct odformat odftab
[] = {
195 { 'a', 1, "%3_u", 4 },
196 { 'c', 1, "%3_c", 4 },
197 { 'd', 1, "%4d", 5 },
198 { 'd', 2, "%6d", 6 },
199 { 'd', 4, "%11d", 11 },
200 { 'd', 8, "%20d", 20 },
201 { 'o', 1, "%03o", 4 },
202 { 'o', 2, "%06o", 7 },
203 { 'o', 4, "%011o", 12 },
204 { 'o', 8, "%022o", 23 },
205 { 'u', 1, "%03u" , 4 },
206 { 'u', 2, "%05u" , 6 },
207 { 'u', 4, "%010u", 11 },
208 { 'u', 8, "%020u", 21 },
209 { 'x', 1, "%02x", 3 },
210 { 'x', 2, "%04x", 5 },
211 { 'x', 4, "%08x", 9 },
212 { 'x', 8, "%016x", 17 },
213 { 'f', 4, "%14.7e", 15 },
214 { 'f', 8, "%21.14e", 22 },
219 * Interpret a POSIX-style -t argument.
222 posixtypes(char const *type_string
)
225 char *fmt
, type
, *tmp
;
226 struct odformat
const *odf
;
228 while (*type_string
) {
229 switch ((type
= *type_string
++)) {
235 if (isupper((unsigned char)*type_string
)) {
236 switch(*type_string
) {
238 nbytes
= sizeof(float);
241 nbytes
= sizeof(double);
244 nbytes
= sizeof(long double);
247 warnx("Bad type-size qualifier '%c'",
252 } else if (isdigit((unsigned char)*type_string
)) {
253 nbytes
= strtol(type_string
, &tmp
, 10);
262 if (isupper((unsigned char)*type_string
)) {
263 switch(*type_string
) {
265 nbytes
= sizeof(char);
268 nbytes
= sizeof(short);
271 nbytes
= sizeof(int);
274 nbytes
= sizeof(long);
277 warnx("Bad type-size qualifier '%c'",
282 } else if (isdigit((unsigned char)*type_string
)) {
283 nbytes
= strtol(type_string
, &tmp
, 10);
291 for (odf
= odftab
; odf
->type
!= 0; odf
++)
292 if (odf
->type
== type
&& odf
->nbytes
== nbytes
)
295 errx(1, "%c%d: format not supported", type
, nbytes
);
296 (void)easprintf(&fmt
, "%d/%d \"%*s%s \" \"\\n\"",
298 4 * nbytes
- odf
->minwidth
, "", odf
->format
);
304 odoffset(int argc
, char ***argvp
)
311 * The offset syntax of od(1) was genuinely bizarre. First, if
312 * it started with a plus it had to be an offset. Otherwise, if
313 * there were at least two arguments, a number or lower-case 'x'
314 * followed by a number makes it an offset. By default it was
315 * octal; if it started with 'x' or '0x' it was hex. If it ended
316 * in a '.', it was decimal. If a 'b' or 'B' was appended, it
317 * multiplied the number by 512 or 1024 byte units. There was
318 * no way to assign a block count to a hex offset.
320 * We assume it's a file if the offset is bad.
322 p
= argc
== 1 ? (*argvp
)[0] : (*argvp
)[1];
326 if (*p
!= '+' && (argc
< 2 ||
327 (!isdigit((unsigned char)p
[0]) &&
328 (p
[0] != 'x' || !isxdigit((unsigned char)p
[1])))))
333 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
338 if (p
[0] == 'x' && isxdigit((unsigned char)p
[1])) {
341 } else if (p
[0] == '0' && p
[1] == 'x') {
346 /* skip over the number */
348 for (num
= p
; isxdigit((unsigned char)*p
); ++p
);
350 for (num
= p
; isdigit((unsigned char)*p
); ++p
);
352 /* check for no number */
356 /* if terminates with a '.', base is decimal */
363 skip
= strtol(num
, &end
, base
? base
: 8);
365 /* if end isn't the same as p, we got a non-octal digit */
375 } else if (*p
== 'b') {
385 * If the offset uses a non-octal base, the base of the offset
386 * is changed as well. This isn't pretty, but it's easy.
389 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = 'x';
390 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] = 'x';
391 } else if (base
== 10) {
392 fshead
->nextfu
->fmt
[TYPE_OFFSET
] = 'd';
393 fshead
->nextfs
->nextfu
->fmt
[TYPE_OFFSET
] = 'd';
396 /* Terminate file list. */