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