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