odsyntax.c revision 1.21 1 1.21 dsl /* $NetBSD: odsyntax.c,v 1.21 2004/10/30 17:43:02 dsl Exp $ */
2 1.5 tls
3 1.1 cgd /*-
4 1.7 mrg * Copyright (c) 1990, 1993
5 1.7 mrg * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.18 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.20 lukem #if HAVE_NBTOOL_CONFIG_H
33 1.20 lukem #include "nbtool_config.h"
34 1.20 lukem #endif
35 1.20 lukem
36 1.8 lukem #include <sys/cdefs.h>
37 1.20 lukem #if !defined(lint)
38 1.6 mikel #if 0
39 1.7 mrg static char sccsid[] = "@(#)odsyntax.c 8.2 (Berkeley) 5/4/95";
40 1.6 mikel #else
41 1.21 dsl __RCSID("$NetBSD: odsyntax.c,v 1.21 2004/10/30 17:43:02 dsl Exp $");
42 1.6 mikel #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/types.h>
46 1.7 mrg
47 1.6 mikel #include <ctype.h>
48 1.8 lukem #include <err.h>
49 1.6 mikel #include <stdio.h>
50 1.1 cgd #include <stdlib.h>
51 1.7 mrg #include <unistd.h>
52 1.7 mrg
53 1.1 cgd #include "hexdump.h"
54 1.1 cgd
55 1.16 bjh21 struct odformat {
56 1.16 bjh21 char type;
57 1.16 bjh21 int nbytes;
58 1.16 bjh21 char const *format;
59 1.16 bjh21 int minwidth;
60 1.16 bjh21 };
61 1.16 bjh21
62 1.16 bjh21 struct odaddrformat {
63 1.16 bjh21 char type;
64 1.16 bjh21 char const *format1;
65 1.16 bjh21 char const *format2;
66 1.16 bjh21 };
67 1.16 bjh21
68 1.1 cgd int deprecated;
69 1.1 cgd
70 1.7 mrg static void odoffset __P((int, char ***));
71 1.16 bjh21 static void posixtypes __P((char const *));
72 1.6 mikel static void odprecede __P((void));
73 1.6 mikel
74 1.11 christos
75 1.6 mikel void
76 1.1 cgd oldsyntax(argc, argvp)
77 1.1 cgd int argc;
78 1.1 cgd char ***argvp;
79 1.1 cgd {
80 1.1 cgd int ch;
81 1.12 bjh21 char *p, **argv;
82 1.1 cgd
83 1.1 cgd deprecated = 1;
84 1.1 cgd argv = *argvp;
85 1.13 bjh21 while ((ch = getopt(argc, argv,
86 1.13 bjh21 "aBbcDdeFfHhIij:LlN:OoPpst:wvXx")) != -1)
87 1.1 cgd switch (ch) {
88 1.1 cgd case 'a':
89 1.16 bjh21 posixtypes("a");
90 1.1 cgd break;
91 1.1 cgd case 'B':
92 1.1 cgd case 'o':
93 1.16 bjh21 posixtypes("o2");
94 1.1 cgd break;
95 1.1 cgd case 'b':
96 1.16 bjh21 posixtypes("o1");
97 1.1 cgd break;
98 1.1 cgd case 'c':
99 1.16 bjh21 posixtypes("c");
100 1.1 cgd break;
101 1.1 cgd case 'd':
102 1.16 bjh21 posixtypes("u2");
103 1.1 cgd break;
104 1.1 cgd case 'D':
105 1.16 bjh21 posixtypes("u4");
106 1.1 cgd break;
107 1.1 cgd case 'e': /* undocumented in od */
108 1.1 cgd case 'F':
109 1.16 bjh21 posixtypes("f8");
110 1.1 cgd break;
111 1.1 cgd case 'f':
112 1.16 bjh21 posixtypes("f4");
113 1.1 cgd break;
114 1.1 cgd case 'H':
115 1.1 cgd case 'X':
116 1.16 bjh21 posixtypes("x4");
117 1.1 cgd break;
118 1.1 cgd case 'h':
119 1.1 cgd case 'x':
120 1.16 bjh21 posixtypes("x2");
121 1.1 cgd break;
122 1.1 cgd case 'I':
123 1.1 cgd case 'L':
124 1.1 cgd case 'l':
125 1.16 bjh21 posixtypes("d4");
126 1.1 cgd break;
127 1.1 cgd case 'i':
128 1.16 bjh21 posixtypes("d2");
129 1.12 bjh21 break;
130 1.12 bjh21 case 'j':
131 1.12 bjh21 if ((skip = strtol(optarg, &p, 0)) < 0)
132 1.12 bjh21 errx(1, "%s: bad skip value", optarg);
133 1.12 bjh21 switch(*p) {
134 1.12 bjh21 case 'b':
135 1.12 bjh21 skip *= 512;
136 1.12 bjh21 break;
137 1.12 bjh21 case 'k':
138 1.12 bjh21 skip *= 1024;
139 1.12 bjh21 break;
140 1.12 bjh21 case 'm':
141 1.12 bjh21 skip *= 1048576;
142 1.12 bjh21 break;
143 1.12 bjh21 }
144 1.13 bjh21 break;
145 1.13 bjh21 case 'N':
146 1.13 bjh21 if ((length = atoi(optarg)) < 0)
147 1.13 bjh21 errx(1, "%s: bad length value", optarg);
148 1.1 cgd break;
149 1.1 cgd case 'O':
150 1.16 bjh21 posixtypes("o4");
151 1.11 christos break;
152 1.11 christos case 't':
153 1.14 bjh21 posixtypes(optarg);
154 1.1 cgd break;
155 1.1 cgd case 'v':
156 1.1 cgd vflag = ALL;
157 1.1 cgd break;
158 1.1 cgd case 'P':
159 1.1 cgd case 'p':
160 1.1 cgd case 's':
161 1.1 cgd case 'w':
162 1.1 cgd case '?':
163 1.1 cgd default:
164 1.8 lukem warnx("od(1) has been deprecated for hexdump(1).");
165 1.1 cgd if (ch != '?')
166 1.8 lukem warnx(
167 1.8 lukem "hexdump(1) compatibility doesn't support the -%c option%s\n",
168 1.1 cgd ch, ch == 's' ? "; see strings(1)." : ".");
169 1.1 cgd usage();
170 1.1 cgd }
171 1.1 cgd
172 1.1 cgd if (!fshead) {
173 1.1 cgd add("\"%07.7_Ao\n\"");
174 1.1 cgd add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
175 1.1 cgd }
176 1.1 cgd
177 1.1 cgd argc -= optind;
178 1.1 cgd *argvp += optind;
179 1.1 cgd
180 1.7 mrg if (argc)
181 1.7 mrg odoffset(argc, argvp);
182 1.14 bjh21 }
183 1.14 bjh21
184 1.16 bjh21 /* formats used for -t */
185 1.16 bjh21
186 1.16 bjh21 static const struct odformat odftab[] = {
187 1.16 bjh21 { 'a', 1, "%3_u", 4 },
188 1.16 bjh21 { 'c', 1, "%3_c", 4 },
189 1.16 bjh21 { 'd', 1, "%4d", 5 },
190 1.16 bjh21 { 'd', 2, "%6d", 6 },
191 1.16 bjh21 { 'd', 4, "%11d", 11 },
192 1.16 bjh21 { 'd', 8, "%20d", 20 },
193 1.16 bjh21 { 'o', 1, "%03o", 4 },
194 1.16 bjh21 { 'o', 2, "%06o", 7 },
195 1.16 bjh21 { 'o', 4, "%011o", 12 },
196 1.16 bjh21 { 'o', 8, "%022o", 23 },
197 1.16 bjh21 { 'u', 1, "%03u" , 4 },
198 1.16 bjh21 { 'u', 2, "%05u" , 6 },
199 1.16 bjh21 { 'u', 4, "%010u", 11 },
200 1.16 bjh21 { 'u', 8, "%020u", 21 },
201 1.16 bjh21 { 'x', 1, "%02x", 3 },
202 1.16 bjh21 { 'x', 2, "%04x", 5 },
203 1.16 bjh21 { 'x', 4, "%08x", 9 },
204 1.16 bjh21 { 'x', 8, "%016x", 17 },
205 1.16 bjh21 { 'f', 4, "%14.7e", 15 },
206 1.16 bjh21 { 'f', 8, "%21.14e", 22 },
207 1.16 bjh21 { 0, 0, NULL, 0 }
208 1.16 bjh21 };
209 1.16 bjh21
210 1.14 bjh21 /*
211 1.14 bjh21 * Interpret a POSIX-style -t argument.
212 1.14 bjh21 */
213 1.14 bjh21 static void
214 1.14 bjh21 posixtypes(type_string)
215 1.16 bjh21 char const *type_string;
216 1.14 bjh21 {
217 1.16 bjh21 int nbytes;
218 1.16 bjh21 char *fmt, type, *tmp;
219 1.16 bjh21 struct odformat const *odf;
220 1.14 bjh21
221 1.14 bjh21 while (*type_string) {
222 1.14 bjh21 odprecede();
223 1.16 bjh21 switch ((type = *type_string++)) {
224 1.14 bjh21 case 'a':
225 1.14 bjh21 case 'c':
226 1.16 bjh21 nbytes = 1;
227 1.14 bjh21 break;
228 1.14 bjh21 case 'f':
229 1.21 dsl if (isupper((unsigned char)*type_string)) {
230 1.16 bjh21 switch(*type_string) {
231 1.16 bjh21 case 'F':
232 1.16 bjh21 nbytes = sizeof(float);
233 1.16 bjh21 break;
234 1.16 bjh21 case 'D':
235 1.16 bjh21 nbytes = sizeof(double);
236 1.16 bjh21 break;
237 1.16 bjh21 case 'L':
238 1.16 bjh21 nbytes = sizeof(long double);
239 1.16 bjh21 break;
240 1.16 bjh21 default:
241 1.16 bjh21 warnx("Bad type-size qualifier '%c'",
242 1.16 bjh21 *type_string);
243 1.16 bjh21 usage();
244 1.16 bjh21 }
245 1.14 bjh21 type_string++;
246 1.21 dsl } else if (isdigit((unsigned char)*type_string)) {
247 1.16 bjh21 nbytes = strtol(type_string, &tmp, 10);
248 1.16 bjh21 type_string = tmp;
249 1.16 bjh21 } else
250 1.16 bjh21 nbytes = 8;
251 1.14 bjh21 break;
252 1.14 bjh21 case 'd':
253 1.14 bjh21 case 'o':
254 1.14 bjh21 case 'u':
255 1.14 bjh21 case 'x':
256 1.21 dsl if (isupper((unsigned char)*type_string)) {
257 1.14 bjh21 switch(*type_string) {
258 1.14 bjh21 case 'C':
259 1.15 bjh21 nbytes = sizeof(char);
260 1.14 bjh21 break;
261 1.14 bjh21 case 'S':
262 1.15 bjh21 nbytes = sizeof(short);
263 1.14 bjh21 break;
264 1.14 bjh21 case 'I':
265 1.15 bjh21 nbytes = sizeof(int);
266 1.14 bjh21 break;
267 1.14 bjh21 case 'L':
268 1.15 bjh21 nbytes = sizeof(long);
269 1.14 bjh21 break;
270 1.14 bjh21 default:
271 1.14 bjh21 warnx("Bad type-size qualifier '%c'",
272 1.14 bjh21 *type_string);
273 1.14 bjh21 usage();
274 1.14 bjh21 }
275 1.14 bjh21 type_string++;
276 1.21 dsl } else if (isdigit((unsigned char)*type_string)) {
277 1.16 bjh21 nbytes = strtol(type_string, &tmp, 10);
278 1.16 bjh21 type_string = tmp;
279 1.16 bjh21 } else
280 1.16 bjh21 nbytes = 4;
281 1.14 bjh21 break;
282 1.14 bjh21 default:
283 1.14 bjh21 usage();
284 1.14 bjh21 }
285 1.16 bjh21 for (odf = odftab; odf->type != 0; odf++)
286 1.16 bjh21 if (odf->type == type && odf->nbytes == nbytes)
287 1.16 bjh21 break;
288 1.16 bjh21 if (odf->type == 0)
289 1.16 bjh21 errx(1, "%c%d: format not supported", type, nbytes);
290 1.16 bjh21 asprintf(&fmt, "%d/%d \"%*s%s \" \"\\n\"",
291 1.16 bjh21 16 / nbytes, nbytes,
292 1.16 bjh21 4 * nbytes - odf->minwidth, "", odf->format);
293 1.16 bjh21 if (fmt == NULL) nomem();
294 1.16 bjh21 add(fmt);
295 1.14 bjh21 }
296 1.1 cgd }
297 1.1 cgd
298 1.7 mrg static void
299 1.1 cgd odoffset(argc, argvp)
300 1.1 cgd int argc;
301 1.1 cgd char ***argvp;
302 1.1 cgd {
303 1.8 lukem char *num, *p;
304 1.1 cgd int base;
305 1.1 cgd char *end;
306 1.1 cgd
307 1.1 cgd /*
308 1.1 cgd * The offset syntax of od(1) was genuinely bizarre. First, if
309 1.1 cgd * it started with a plus it had to be an offset. Otherwise, if
310 1.1 cgd * there were at least two arguments, a number or lower-case 'x'
311 1.1 cgd * followed by a number makes it an offset. By default it was
312 1.1 cgd * octal; if it started with 'x' or '0x' it was hex. If it ended
313 1.1 cgd * in a '.', it was decimal. If a 'b' or 'B' was appended, it
314 1.1 cgd * multiplied the number by 512 or 1024 byte units. There was
315 1.1 cgd * no way to assign a block count to a hex offset.
316 1.1 cgd *
317 1.6 mikel * We assume it's a file if the offset is bad.
318 1.1 cgd */
319 1.7 mrg p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
320 1.3 mycroft if (!p)
321 1.3 mycroft return;
322 1.7 mrg
323 1.1 cgd if (*p != '+' && (argc < 2 ||
324 1.10 christos (!isdigit((unsigned char)p[0]) &&
325 1.10 christos (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
326 1.1 cgd return;
327 1.1 cgd
328 1.1 cgd base = 0;
329 1.1 cgd /*
330 1.1 cgd * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
331 1.1 cgd * set base.
332 1.1 cgd */
333 1.1 cgd if (p[0] == '+')
334 1.1 cgd ++p;
335 1.10 christos if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
336 1.1 cgd ++p;
337 1.1 cgd base = 16;
338 1.1 cgd } else if (p[0] == '0' && p[1] == 'x') {
339 1.1 cgd p += 2;
340 1.1 cgd base = 16;
341 1.1 cgd }
342 1.1 cgd
343 1.1 cgd /* skip over the number */
344 1.1 cgd if (base == 16)
345 1.10 christos for (num = p; isxdigit((unsigned char)*p); ++p);
346 1.1 cgd else
347 1.10 christos for (num = p; isdigit((unsigned char)*p); ++p);
348 1.1 cgd
349 1.1 cgd /* check for no number */
350 1.1 cgd if (num == p)
351 1.1 cgd return;
352 1.1 cgd
353 1.1 cgd /* if terminates with a '.', base is decimal */
354 1.1 cgd if (*p == '.') {
355 1.1 cgd if (base)
356 1.1 cgd return;
357 1.1 cgd base = 10;
358 1.1 cgd }
359 1.1 cgd
360 1.1 cgd skip = strtol(num, &end, base ? base : 8);
361 1.1 cgd
362 1.1 cgd /* if end isn't the same as p, we got a non-octal digit */
363 1.7 mrg if (end != p) {
364 1.1 cgd skip = 0;
365 1.7 mrg return;
366 1.7 mrg }
367 1.7 mrg
368 1.9 ross if (*p) {
369 1.7 mrg if (*p == 'B') {
370 1.7 mrg skip *= 1024;
371 1.7 mrg ++p;
372 1.7 mrg } else if (*p == 'b') {
373 1.7 mrg skip *= 512;
374 1.1 cgd ++p;
375 1.1 cgd }
376 1.9 ross }
377 1.7 mrg if (*p) {
378 1.7 mrg skip = 0;
379 1.7 mrg return;
380 1.7 mrg }
381 1.7 mrg /*
382 1.7 mrg * If the offset uses a non-octal base, the base of the offset
383 1.7 mrg * is changed as well. This isn't pretty, but it's easy.
384 1.7 mrg */
385 1.1 cgd #define TYPE_OFFSET 7
386 1.7 mrg if (base == 16) {
387 1.7 mrg fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
388 1.7 mrg fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
389 1.7 mrg } else if (base == 10) {
390 1.7 mrg fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
391 1.7 mrg fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
392 1.1 cgd }
393 1.7 mrg
394 1.7 mrg /* Terminate file list. */
395 1.7 mrg (*argvp)[1] = NULL;
396 1.1 cgd }
397 1.1 cgd
398 1.1 cgd static void
399 1.1 cgd odprecede()
400 1.1 cgd {
401 1.1 cgd static int first = 1;
402 1.1 cgd
403 1.1 cgd if (first) {
404 1.1 cgd first = 0;
405 1.1 cgd add("\"%07.7_Ao\n\"");
406 1.1 cgd add("\"%07.7_ao \"");
407 1.1 cgd } else
408 1.1 cgd add("\" \"");
409 1.1 cgd }
410