hexsyntax.c revision 216370
1279377Simp/*-
2279377Simp * Copyright (c) 1990, 1993
3279377Simp *	The Regents of the University of California.  All rights reserved.
4279377Simp *
5279377Simp * Redistribution and use in source and binary forms, with or without
6279377Simp * modification, are permitted provided that the following conditions
7279377Simp * are met:
8279377Simp * 1. Redistributions of source code must retain the above copyright
9279377Simp *    notice, this list of conditions and the following disclaimer.
10279377Simp * 2. Redistributions in binary form must reproduce the above copyright
11279377Simp *    notice, this list of conditions and the following disclaimer in the
12279377Simp *    documentation and/or other materials provided with the distribution.
13279377Simp * 4. Neither the name of the University nor the names of its contributors
14279377Simp *    may be used to endorse or promote products derived from this software
15279377Simp *    without specific prior written permission.
16279377Simp *
17279377Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18279377Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19279377Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20279377Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21279377Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22279377Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23279377Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24279377Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25279377Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26279377Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27279377Simp * SUCH DAMAGE.
28279377Simp */
29279377Simp
30279377Simp#ifndef lint
31279377Simp#if 0
32279377Simpstatic char sccsid[] = "@(#)hexsyntax.c	8.2 (Berkeley) 5/4/95";
33279377Simp#endif
34279377Simp#endif /* not lint */
35279377Simp#include <sys/cdefs.h>
36279377Simp__FBSDID("$FreeBSD: head/usr.bin/hexdump/hexsyntax.c 216370 2010-12-11 08:32:16Z joel $");
37279377Simp
38279377Simp#include <sys/types.h>
39279377Simp
40279377Simp#include <err.h>
41279377Simp#include <stdio.h>
42279377Simp#include <stdlib.h>
43279377Simp#include <string.h>
44279377Simp#include <unistd.h>
45279377Simp
46279377Simp#include "hexdump.h"
47279377Simp
48279377Simpoff_t skip;				/* bytes to skip */
49279377Simp
50279377Simpvoid
51279377Simpnewsyntax(int argc, char ***argvp)
52279377Simp{
53279377Simp	int ch;
54279377Simp	char *p, **argv;
55279377Simp
56279377Simp	argv = *argvp;
57279377Simp	if ((p = rindex(argv[0], 'h')) != NULL &&
58279377Simp	    strcmp(p, "hd") == 0) {
59279377Simp		/* "Canonical" format, implies -C. */
60279377Simp		add("\"%08.8_Ax\n\"");
61279377Simp		add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
62279377Simp		add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
63279377Simp	}
64279377Simp	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
65279377Simp		switch (ch) {
66279377Simp		case 'b':
67279377Simp			add("\"%07.7_Ax\n\"");
68279377Simp			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
69279377Simp			break;
70279377Simp		case 'c':
71279377Simp			add("\"%07.7_Ax\n\"");
72279377Simp			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
73279377Simp			break;
74279377Simp		case 'C':
75279377Simp			add("\"%08.8_Ax\n\"");
76279377Simp			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
77279377Simp			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
78279377Simp			break;
79279377Simp		case 'd':
80279377Simp			add("\"%07.7_Ax\n\"");
81279377Simp			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
82279377Simp			break;
83279377Simp		case 'e':
84279377Simp			add(optarg);
85279377Simp			break;
86279377Simp		case 'f':
87279377Simp			addfile(optarg);
88279377Simp			break;
89279377Simp		case 'n':
90279377Simp			if ((length = atoi(optarg)) < 0)
91279377Simp				errx(1, "%s: bad length value", optarg);
92279377Simp			break;
93		case 'o':
94			add("\"%07.7_Ax\n\"");
95			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
96			break;
97		case 's':
98			if ((skip = strtoll(optarg, &p, 0)) < 0)
99				errx(1, "%s: bad skip value", optarg);
100			switch(*p) {
101			case 'b':
102				skip *= 512;
103				break;
104			case 'k':
105				skip *= 1024;
106				break;
107			case 'm':
108				skip *= 1048576;
109				break;
110			}
111			break;
112		case 'v':
113			vflag = ALL;
114			break;
115		case 'x':
116			add("\"%07.7_Ax\n\"");
117			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
118			break;
119		case '?':
120			usage();
121		}
122
123	if (!fshead) {
124		add("\"%07.7_Ax\n\"");
125		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
126	}
127
128	*argvp += optind;
129}
130
131void
132usage(void)
133{
134	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
135"usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length]",
136"               [-s skip] [file ...]",
137"       hd      [-bcdovx]  [-e fmt] [-f fmt_file] [-n length]",
138"               [-s skip] [file ...]");
139	exit(1);
140}
141