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