odsyntax.c revision 1.24 1 1.24 christos /* $NetBSD: odsyntax.c,v 1.24 2006/08/26 18:17:42 christos 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.24 christos __RCSID("$NetBSD: odsyntax.c,v 1.24 2006/08/26 18:17:42 christos 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.24 christos #include <util.h>
53 1.7 mrg
54 1.1 cgd #include "hexdump.h"
55 1.1 cgd
56 1.16 bjh21 struct odformat {
57 1.16 bjh21 char type;
58 1.16 bjh21 int nbytes;
59 1.16 bjh21 char const *format;
60 1.16 bjh21 int minwidth;
61 1.16 bjh21 };
62 1.16 bjh21
63 1.16 bjh21 struct odaddrformat {
64 1.16 bjh21 char type;
65 1.16 bjh21 char const *format1;
66 1.16 bjh21 char const *format2;
67 1.16 bjh21 };
68 1.16 bjh21
69 1.1 cgd int deprecated;
70 1.1 cgd
71 1.23 perry static void odoffset(int, char ***);
72 1.23 perry static void posixtypes(char const *);
73 1.23 perry static void odprecede(void);
74 1.6 mikel
75 1.11 christos
76 1.6 mikel void
77 1.23 perry oldsyntax(int argc, char ***argvp)
78 1.1 cgd {
79 1.1 cgd int ch;
80 1.12 bjh21 char *p, **argv;
81 1.1 cgd
82 1.1 cgd deprecated = 1;
83 1.1 cgd argv = *argvp;
84 1.13 bjh21 while ((ch = getopt(argc, argv,
85 1.13 bjh21 "aBbcDdeFfHhIij:LlN:OoPpst:wvXx")) != -1)
86 1.1 cgd switch (ch) {
87 1.1 cgd case 'a':
88 1.16 bjh21 posixtypes("a");
89 1.1 cgd break;
90 1.1 cgd case 'B':
91 1.1 cgd case 'o':
92 1.16 bjh21 posixtypes("o2");
93 1.1 cgd break;
94 1.1 cgd case 'b':
95 1.16 bjh21 posixtypes("o1");
96 1.1 cgd break;
97 1.1 cgd case 'c':
98 1.16 bjh21 posixtypes("c");
99 1.1 cgd break;
100 1.1 cgd case 'd':
101 1.16 bjh21 posixtypes("u2");
102 1.1 cgd break;
103 1.1 cgd case 'D':
104 1.16 bjh21 posixtypes("u4");
105 1.1 cgd break;
106 1.1 cgd case 'e': /* undocumented in od */
107 1.1 cgd case 'F':
108 1.16 bjh21 posixtypes("f8");
109 1.1 cgd break;
110 1.1 cgd case 'f':
111 1.16 bjh21 posixtypes("f4");
112 1.1 cgd break;
113 1.1 cgd case 'H':
114 1.1 cgd case 'X':
115 1.16 bjh21 posixtypes("x4");
116 1.1 cgd break;
117 1.1 cgd case 'h':
118 1.1 cgd case 'x':
119 1.16 bjh21 posixtypes("x2");
120 1.1 cgd break;
121 1.1 cgd case 'I':
122 1.1 cgd case 'L':
123 1.1 cgd case 'l':
124 1.16 bjh21 posixtypes("d4");
125 1.1 cgd break;
126 1.1 cgd case 'i':
127 1.16 bjh21 posixtypes("d2");
128 1.12 bjh21 break;
129 1.12 bjh21 case 'j':
130 1.12 bjh21 if ((skip = strtol(optarg, &p, 0)) < 0)
131 1.12 bjh21 errx(1, "%s: bad skip value", optarg);
132 1.12 bjh21 switch(*p) {
133 1.12 bjh21 case 'b':
134 1.12 bjh21 skip *= 512;
135 1.12 bjh21 break;
136 1.12 bjh21 case 'k':
137 1.12 bjh21 skip *= 1024;
138 1.12 bjh21 break;
139 1.12 bjh21 case 'm':
140 1.12 bjh21 skip *= 1048576;
141 1.12 bjh21 break;
142 1.12 bjh21 }
143 1.13 bjh21 break;
144 1.13 bjh21 case 'N':
145 1.13 bjh21 if ((length = atoi(optarg)) < 0)
146 1.13 bjh21 errx(1, "%s: bad length value", optarg);
147 1.1 cgd break;
148 1.1 cgd case 'O':
149 1.16 bjh21 posixtypes("o4");
150 1.11 christos break;
151 1.11 christos case 't':
152 1.14 bjh21 posixtypes(optarg);
153 1.1 cgd break;
154 1.1 cgd case 'v':
155 1.1 cgd vflag = ALL;
156 1.1 cgd break;
157 1.1 cgd case 'P':
158 1.1 cgd case 'p':
159 1.1 cgd case 's':
160 1.1 cgd case 'w':
161 1.1 cgd case '?':
162 1.1 cgd default:
163 1.8 lukem warnx("od(1) has been deprecated for hexdump(1).");
164 1.1 cgd if (ch != '?')
165 1.8 lukem warnx(
166 1.8 lukem "hexdump(1) compatibility doesn't support the -%c option%s\n",
167 1.1 cgd ch, ch == 's' ? "; see strings(1)." : ".");
168 1.1 cgd usage();
169 1.1 cgd }
170 1.1 cgd
171 1.1 cgd if (!fshead) {
172 1.1 cgd add("\"%07.7_Ao\n\"");
173 1.1 cgd add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
174 1.1 cgd }
175 1.1 cgd
176 1.1 cgd argc -= optind;
177 1.1 cgd *argvp += optind;
178 1.1 cgd
179 1.7 mrg if (argc)
180 1.7 mrg odoffset(argc, argvp);
181 1.14 bjh21 }
182 1.14 bjh21
183 1.16 bjh21 /* formats used for -t */
184 1.16 bjh21
185 1.16 bjh21 static const struct odformat odftab[] = {
186 1.16 bjh21 { 'a', 1, "%3_u", 4 },
187 1.16 bjh21 { 'c', 1, "%3_c", 4 },
188 1.16 bjh21 { 'd', 1, "%4d", 5 },
189 1.16 bjh21 { 'd', 2, "%6d", 6 },
190 1.16 bjh21 { 'd', 4, "%11d", 11 },
191 1.16 bjh21 { 'd', 8, "%20d", 20 },
192 1.16 bjh21 { 'o', 1, "%03o", 4 },
193 1.16 bjh21 { 'o', 2, "%06o", 7 },
194 1.16 bjh21 { 'o', 4, "%011o", 12 },
195 1.16 bjh21 { 'o', 8, "%022o", 23 },
196 1.16 bjh21 { 'u', 1, "%03u" , 4 },
197 1.16 bjh21 { 'u', 2, "%05u" , 6 },
198 1.16 bjh21 { 'u', 4, "%010u", 11 },
199 1.16 bjh21 { 'u', 8, "%020u", 21 },
200 1.16 bjh21 { 'x', 1, "%02x", 3 },
201 1.16 bjh21 { 'x', 2, "%04x", 5 },
202 1.16 bjh21 { 'x', 4, "%08x", 9 },
203 1.16 bjh21 { 'x', 8, "%016x", 17 },
204 1.16 bjh21 { 'f', 4, "%14.7e", 15 },
205 1.16 bjh21 { 'f', 8, "%21.14e", 22 },
206 1.16 bjh21 { 0, 0, NULL, 0 }
207 1.16 bjh21 };
208 1.16 bjh21
209 1.14 bjh21 /*
210 1.14 bjh21 * Interpret a POSIX-style -t argument.
211 1.14 bjh21 */
212 1.14 bjh21 static void
213 1.23 perry posixtypes(char const *type_string)
214 1.14 bjh21 {
215 1.22 lukem int nbytes = 0;
216 1.16 bjh21 char *fmt, type, *tmp;
217 1.16 bjh21 struct odformat const *odf;
218 1.14 bjh21
219 1.14 bjh21 while (*type_string) {
220 1.14 bjh21 odprecede();
221 1.16 bjh21 switch ((type = *type_string++)) {
222 1.14 bjh21 case 'a':
223 1.14 bjh21 case 'c':
224 1.16 bjh21 nbytes = 1;
225 1.14 bjh21 break;
226 1.14 bjh21 case 'f':
227 1.21 dsl if (isupper((unsigned char)*type_string)) {
228 1.16 bjh21 switch(*type_string) {
229 1.16 bjh21 case 'F':
230 1.16 bjh21 nbytes = sizeof(float);
231 1.16 bjh21 break;
232 1.16 bjh21 case 'D':
233 1.16 bjh21 nbytes = sizeof(double);
234 1.16 bjh21 break;
235 1.16 bjh21 case 'L':
236 1.16 bjh21 nbytes = sizeof(long double);
237 1.16 bjh21 break;
238 1.16 bjh21 default:
239 1.16 bjh21 warnx("Bad type-size qualifier '%c'",
240 1.16 bjh21 *type_string);
241 1.16 bjh21 usage();
242 1.16 bjh21 }
243 1.14 bjh21 type_string++;
244 1.21 dsl } else if (isdigit((unsigned char)*type_string)) {
245 1.16 bjh21 nbytes = strtol(type_string, &tmp, 10);
246 1.16 bjh21 type_string = tmp;
247 1.16 bjh21 } else
248 1.16 bjh21 nbytes = 8;
249 1.14 bjh21 break;
250 1.14 bjh21 case 'd':
251 1.14 bjh21 case 'o':
252 1.14 bjh21 case 'u':
253 1.14 bjh21 case 'x':
254 1.21 dsl if (isupper((unsigned char)*type_string)) {
255 1.14 bjh21 switch(*type_string) {
256 1.14 bjh21 case 'C':
257 1.15 bjh21 nbytes = sizeof(char);
258 1.14 bjh21 break;
259 1.14 bjh21 case 'S':
260 1.15 bjh21 nbytes = sizeof(short);
261 1.14 bjh21 break;
262 1.14 bjh21 case 'I':
263 1.15 bjh21 nbytes = sizeof(int);
264 1.14 bjh21 break;
265 1.14 bjh21 case 'L':
266 1.15 bjh21 nbytes = sizeof(long);
267 1.14 bjh21 break;
268 1.14 bjh21 default:
269 1.14 bjh21 warnx("Bad type-size qualifier '%c'",
270 1.14 bjh21 *type_string);
271 1.14 bjh21 usage();
272 1.14 bjh21 }
273 1.14 bjh21 type_string++;
274 1.21 dsl } else if (isdigit((unsigned char)*type_string)) {
275 1.16 bjh21 nbytes = strtol(type_string, &tmp, 10);
276 1.16 bjh21 type_string = tmp;
277 1.16 bjh21 } else
278 1.16 bjh21 nbytes = 4;
279 1.14 bjh21 break;
280 1.14 bjh21 default:
281 1.14 bjh21 usage();
282 1.14 bjh21 }
283 1.16 bjh21 for (odf = odftab; odf->type != 0; odf++)
284 1.16 bjh21 if (odf->type == type && odf->nbytes == nbytes)
285 1.16 bjh21 break;
286 1.16 bjh21 if (odf->type == 0)
287 1.16 bjh21 errx(1, "%c%d: format not supported", type, nbytes);
288 1.24 christos (void)easprintf(&fmt, "%d/%d \"%*s%s \" \"\\n\"",
289 1.16 bjh21 16 / nbytes, nbytes,
290 1.16 bjh21 4 * nbytes - odf->minwidth, "", odf->format);
291 1.16 bjh21 add(fmt);
292 1.14 bjh21 }
293 1.1 cgd }
294 1.1 cgd
295 1.7 mrg static void
296 1.23 perry odoffset(int argc, char ***argvp)
297 1.1 cgd {
298 1.8 lukem char *num, *p;
299 1.1 cgd int base;
300 1.1 cgd char *end;
301 1.1 cgd
302 1.1 cgd /*
303 1.1 cgd * The offset syntax of od(1) was genuinely bizarre. First, if
304 1.1 cgd * it started with a plus it had to be an offset. Otherwise, if
305 1.1 cgd * there were at least two arguments, a number or lower-case 'x'
306 1.1 cgd * followed by a number makes it an offset. By default it was
307 1.1 cgd * octal; if it started with 'x' or '0x' it was hex. If it ended
308 1.1 cgd * in a '.', it was decimal. If a 'b' or 'B' was appended, it
309 1.1 cgd * multiplied the number by 512 or 1024 byte units. There was
310 1.1 cgd * no way to assign a block count to a hex offset.
311 1.1 cgd *
312 1.6 mikel * We assume it's a file if the offset is bad.
313 1.1 cgd */
314 1.7 mrg p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
315 1.3 mycroft if (!p)
316 1.3 mycroft return;
317 1.7 mrg
318 1.1 cgd if (*p != '+' && (argc < 2 ||
319 1.10 christos (!isdigit((unsigned char)p[0]) &&
320 1.10 christos (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
321 1.1 cgd return;
322 1.1 cgd
323 1.1 cgd base = 0;
324 1.1 cgd /*
325 1.1 cgd * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
326 1.1 cgd * set base.
327 1.1 cgd */
328 1.1 cgd if (p[0] == '+')
329 1.1 cgd ++p;
330 1.10 christos if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
331 1.1 cgd ++p;
332 1.1 cgd base = 16;
333 1.1 cgd } else if (p[0] == '0' && p[1] == 'x') {
334 1.1 cgd p += 2;
335 1.1 cgd base = 16;
336 1.1 cgd }
337 1.1 cgd
338 1.1 cgd /* skip over the number */
339 1.1 cgd if (base == 16)
340 1.10 christos for (num = p; isxdigit((unsigned char)*p); ++p);
341 1.1 cgd else
342 1.10 christos for (num = p; isdigit((unsigned char)*p); ++p);
343 1.1 cgd
344 1.1 cgd /* check for no number */
345 1.1 cgd if (num == p)
346 1.1 cgd return;
347 1.1 cgd
348 1.1 cgd /* if terminates with a '.', base is decimal */
349 1.1 cgd if (*p == '.') {
350 1.1 cgd if (base)
351 1.1 cgd return;
352 1.1 cgd base = 10;
353 1.1 cgd }
354 1.1 cgd
355 1.1 cgd skip = strtol(num, &end, base ? base : 8);
356 1.1 cgd
357 1.1 cgd /* if end isn't the same as p, we got a non-octal digit */
358 1.7 mrg if (end != p) {
359 1.1 cgd skip = 0;
360 1.7 mrg return;
361 1.7 mrg }
362 1.7 mrg
363 1.9 ross if (*p) {
364 1.7 mrg if (*p == 'B') {
365 1.7 mrg skip *= 1024;
366 1.7 mrg ++p;
367 1.7 mrg } else if (*p == 'b') {
368 1.7 mrg skip *= 512;
369 1.1 cgd ++p;
370 1.1 cgd }
371 1.9 ross }
372 1.7 mrg if (*p) {
373 1.7 mrg skip = 0;
374 1.7 mrg return;
375 1.7 mrg }
376 1.7 mrg /*
377 1.7 mrg * If the offset uses a non-octal base, the base of the offset
378 1.7 mrg * is changed as well. This isn't pretty, but it's easy.
379 1.7 mrg */
380 1.1 cgd #define TYPE_OFFSET 7
381 1.7 mrg if (base == 16) {
382 1.7 mrg fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
383 1.7 mrg fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
384 1.7 mrg } else if (base == 10) {
385 1.7 mrg fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
386 1.7 mrg fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
387 1.1 cgd }
388 1.7 mrg
389 1.7 mrg /* Terminate file list. */
390 1.7 mrg (*argvp)[1] = NULL;
391 1.1 cgd }
392 1.1 cgd
393 1.1 cgd static void
394 1.23 perry odprecede(void)
395 1.1 cgd {
396 1.1 cgd static int first = 1;
397 1.1 cgd
398 1.1 cgd if (first) {
399 1.1 cgd first = 0;
400 1.1 cgd add("\"%07.7_Ao\n\"");
401 1.1 cgd add("\"%07.7_ao \"");
402 1.1 cgd } else
403 1.1 cgd add("\" \"");
404 1.1 cgd }
405