t40c term[] count fix
[minix.git] / commands / hexdump / odsyntax.c
blob5ca9769e3157a7d65d9779d92c5c4a3edc74666a
1 /* $NetBSD: odsyntax.c,v 1.26 2010/02/09 14:06:37 drochner Exp $ */
3 /*-
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
9 * are met:
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
29 * SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #if 0
38 #if !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)odsyntax.c 8.2 (Berkeley) 5/4/95";
41 #else
42 __RCSID("$NetBSD: odsyntax.c,v 1.26 2010/02/09 14:06:37 drochner Exp $");
43 #endif
44 #endif /* not lint */
45 #endif
47 #include <sys/types.h>
49 #include <ctype.h>
50 #include <err.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <util.h>
56 #include "hexdump.h"
58 #define PADDING " "
60 struct odformat {
61 char type;
62 int nbytes;
63 char const *format;
64 int minwidth;
67 struct odaddrformat {
68 char type;
69 char const *format1;
70 char const *format2;
73 int odmode;
75 static void odoffset(int, char ***);
76 static void posixtypes(char const *);
78 void
79 odsyntax(int argc, char ***argvp)
81 static char empty[] = "", padding[] = PADDING;
82 int ch;
83 char *p, **argv;
85 #define TYPE_OFFSET 7
86 add("\"%07.7_Ao\n\"");
87 add("\"%07.7_ao \"");
89 odmode = 1;
90 argv = *argvp;
91 while ((ch = getopt(argc, argv,
92 "A:aBbcDdeFfHhIij:LlN:Oot:vXx")) != -1)
93 switch (ch) {
94 case 'A':
95 switch (*optarg) {
96 case 'd': case 'o': case 'x':
97 fshead->nextfu->fmt[TYPE_OFFSET] = *optarg;
98 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] =
99 *optarg;
100 break;
101 case 'n':
102 fshead->nextfu->fmt = empty;
103 fshead->nextfs->nextfu->fmt = padding;
104 break;
105 default:
106 errx(1, "%s: invalid address base", optarg);
108 break;
109 case 'a':
110 posixtypes("a");
111 break;
112 case 'B':
113 case 'o':
114 posixtypes("o2");
115 break;
116 case 'b':
117 posixtypes("o1");
118 break;
119 case 'c':
120 posixtypes("c");
121 break;
122 case 'd':
123 posixtypes("u2");
124 break;
125 case 'D':
126 posixtypes("u4");
127 break;
128 case 'e': /* undocumented in od */
129 case 'F':
130 posixtypes("f8");
131 break;
132 case 'f':
133 posixtypes("f4");
134 break;
135 case 'H':
136 case 'X':
137 posixtypes("x4");
138 break;
139 case 'h':
140 case 'x':
141 posixtypes("x2");
142 break;
143 case 'I':
144 case 'L':
145 case 'l':
146 posixtypes("d4");
147 break;
148 case 'i':
149 posixtypes("d2");
150 break;
151 case 'j':
152 if ((skip = strtol(optarg, &p, 0)) < 0)
153 errx(1, "%s: bad skip value", optarg);
154 switch(*p) {
155 case 'b':
156 skip *= 512;
157 break;
158 case 'k':
159 skip *= 1024;
160 break;
161 case 'm':
162 skip *= 1048576;
163 break;
165 break;
166 case 'N':
167 if ((length = atoi(optarg)) < 0)
168 errx(1, "%s: bad length value", optarg);
169 break;
170 case 'O':
171 posixtypes("o4");
172 break;
173 case 't':
174 posixtypes(optarg);
175 break;
176 case 'v':
177 vflag = ALL;
178 break;
179 case '?':
180 default:
181 usage();
184 if (fshead->nextfs->nextfs == NULL)
185 posixtypes("oS");
187 argc -= optind;
188 *argvp += optind;
190 if (argc)
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 },
217 { 0, 0, NULL, 0 }
221 * Interpret a POSIX-style -t argument.
223 static void
224 posixtypes(char const *type_string)
226 int nbytes = 0;
227 char *fmt, type, *tmp;
228 struct odformat const *odf;
230 while (*type_string) {
231 switch ((type = *type_string++)) {
232 case 'a':
233 case 'c':
234 nbytes = 1;
235 break;
236 case 'f':
237 if (isupper((unsigned char)*type_string)) {
238 switch(*type_string) {
239 case 'F':
240 nbytes = sizeof(float);
241 break;
242 case 'D':
243 nbytes = sizeof(double);
244 break;
245 case 'L':
246 nbytes = sizeof(long double);
247 break;
248 default:
249 warnx("Bad type-size qualifier '%c'",
250 *type_string);
251 usage();
253 type_string++;
254 } else if (isdigit((unsigned char)*type_string)) {
255 nbytes = strtol(type_string, &tmp, 10);
256 type_string = tmp;
257 } else
258 nbytes = 8;
259 break;
260 case 'd':
261 case 'o':
262 case 'u':
263 case 'x':
264 if (isupper((unsigned char)*type_string)) {
265 switch(*type_string) {
266 case 'C':
267 nbytes = sizeof(char);
268 break;
269 case 'S':
270 nbytes = sizeof(short);
271 break;
272 case 'I':
273 nbytes = sizeof(int);
274 break;
275 case 'L':
276 nbytes = sizeof(long);
277 break;
278 default:
279 warnx("Bad type-size qualifier '%c'",
280 *type_string);
281 usage();
283 type_string++;
284 } else if (isdigit((unsigned char)*type_string)) {
285 nbytes = strtol(type_string, &tmp, 10);
286 type_string = tmp;
287 } else
288 nbytes = 4;
289 break;
290 default:
291 usage();
293 for (odf = odftab; odf->type != 0; odf++)
294 if (odf->type == type && odf->nbytes == nbytes)
295 break;
296 if (odf->type == 0)
297 errx(1, "%c%d: format not supported", type, nbytes);
298 (void)easprintf(&fmt, "%d/%d \"%*s%s \" \"\\n\"",
299 16 / nbytes, nbytes,
300 4 * nbytes - odf->minwidth, "", odf->format);
301 add(fmt);
305 static void
306 odoffset(int argc, char ***argvp)
308 char *num, *p;
309 int base;
310 char *end;
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];
325 if (!p)
326 return;
328 if (*p != '+' && (argc < 2 ||
329 (!isdigit((unsigned char)p[0]) &&
330 (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
331 return;
333 base = 0;
335 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
336 * set base.
338 if (p[0] == '+')
339 ++p;
340 if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
341 ++p;
342 base = 16;
343 } else if (p[0] == '0' && p[1] == 'x') {
344 p += 2;
345 base = 16;
348 /* skip over the number */
349 if (base == 16)
350 for (num = p; isxdigit((unsigned char)*p); ++p);
351 else
352 for (num = p; isdigit((unsigned char)*p); ++p);
354 /* check for no number */
355 if (num == p)
356 return;
358 /* if terminates with a '.', base is decimal */
359 if (*p == '.') {
360 if (base)
361 return;
362 base = 10;
365 skip = strtol(num, &end, base ? base : 8);
367 /* if end isn't the same as p, we got a non-octal digit */
368 if (end != p) {
369 skip = 0;
370 return;
373 if (*p) {
374 if (*p == 'B') {
375 skip *= 1024;
376 ++p;
377 } else if (*p == 'b') {
378 skip *= 512;
379 ++p;
382 if (*p) {
383 skip = 0;
384 return;
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.
390 if (base == 16) {
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. */
399 (*argvp)[1] = NULL;