hexsyntax.c revision 259065
190075Sobrien/*-
2132718Skan * Copyright (c) 1990, 1993
390075Sobrien *	The Regents of the University of California.  All rights reserved.
490075Sobrien *
5132718Skan * Redistribution and use in source and binary forms, with or without
690075Sobrien * modification, are permitted provided that the following conditions
7132718Skan * are met:
8132718Skan * 1. Redistributions of source code must retain the above copyright
9132718Skan *    notice, this list of conditions and the following disclaimer.
10132718Skan * 2. Redistributions in binary form must reproduce the above copyright
1190075Sobrien *    notice, this list of conditions and the following disclaimer in the
12132718Skan *    documentation and/or other materials provided with the distribution.
13132718Skan * 4. Neither the name of the University nor the names of its contributors
14132718Skan *    may be used to endorse or promote products derived from this software
15132718Skan *    without specific prior written permission.
1690075Sobrien *
17132718Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18132718Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19169706Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20169706Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2190075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2290075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2390075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24209867Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25209867Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26209867Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27209867Snwhitehorn * SUCH DAMAGE.
28209867Snwhitehorn */
29209867Snwhitehorn
30209867Snwhitehorn#ifndef lint
31209867Snwhitehorn#if 0
3290075Sobrienstatic char sccsid[] = "@(#)hexsyntax.c	8.2 (Berkeley) 5/4/95";
33209867Snwhitehorn#endif
34147823Sscottl#endif /* not lint */
35209867Snwhitehorn#include <sys/cdefs.h>
36209867Snwhitehorn__FBSDID("$FreeBSD: releng/10.0/usr.bin/hexdump/hexsyntax.c 229403 2012-01-03 18:51:58Z ed $");
37209867Snwhitehorn
38209867Snwhitehorn#include <sys/types.h>
39209867Snwhitehorn
40218824Snwhitehorn#include <err.h>
41218824Snwhitehorn#include <stdio.h>
42218824Snwhitehorn#include <stdlib.h>
43218824Snwhitehorn#include <string.h>
44218824Snwhitehorn#include <unistd.h>
45218824Snwhitehorn
46218824Snwhitehorn#include "hexdump.h"
47209867Snwhitehorn
48209867Snwhitehornoff_t skip;				/* bytes to skip */
49209867Snwhitehorn
50209867Snwhitehornvoid
51209867Snwhitehornnewsyntax(int argc, char ***argvp)
52209867Snwhitehorn{
53209867Snwhitehorn	int ch;
54209867Snwhitehorn	char *p, **argv;
55209867Snwhitehorn
56209867Snwhitehorn	argv = *argvp;
57209867Snwhitehorn	if ((p = strrchr(argv[0], 'h')) != NULL &&
58209867Snwhitehorn	    strcmp(p, "hd") == 0) {
59209867Snwhitehorn		/* "Canonical" format, implies -C. */
60209867Snwhitehorn		add("\"%08.8_Ax\n\"");
61209867Snwhitehorn		add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
62209867Snwhitehorn		add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
63209867Snwhitehorn	}
64209867Snwhitehorn	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
65209867Snwhitehorn		switch (ch) {
66209867Snwhitehorn		case 'b':
67209867Snwhitehorn			add("\"%07.7_Ax\n\"");
68209867Snwhitehorn			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
69209867Snwhitehorn			break;
70209867Snwhitehorn		case 'c':
71209867Snwhitehorn			add("\"%07.7_Ax\n\"");
72209867Snwhitehorn			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
73209867Snwhitehorn			break;
74209867Snwhitehorn		case 'C':
75209867Snwhitehorn			add("\"%08.8_Ax\n\"");
76209867Snwhitehorn			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
77209867Snwhitehorn			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
78209867Snwhitehorn			break;
79209867Snwhitehorn		case 'd':
80209867Snwhitehorn			add("\"%07.7_Ax\n\"");
81209867Snwhitehorn			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
82209867Snwhitehorn			break;
83209867Snwhitehorn		case 'e':
84209867Snwhitehorn			add(optarg);
85209867Snwhitehorn			break;
86209867Snwhitehorn		case 'f':
87209867Snwhitehorn			addfile(optarg);
88209867Snwhitehorn			break;
89209867Snwhitehorn		case 'n':
90209867Snwhitehorn			if ((length = atoi(optarg)) < 0)
91209867Snwhitehorn				errx(1, "%s: bad length value", optarg);
92209867Snwhitehorn			break;
93209867Snwhitehorn		case 'o':
94209867Snwhitehorn			add("\"%07.7_Ax\n\"");
95209867Snwhitehorn			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
96209867Snwhitehorn			break;
97209867Snwhitehorn		case 's':
98209867Snwhitehorn			if ((skip = strtoll(optarg, &p, 0)) < 0)
99209867Snwhitehorn				errx(1, "%s: bad skip value", optarg);
100209867Snwhitehorn			switch(*p) {
101209867Snwhitehorn			case 'b':
102209867Snwhitehorn				skip *= 512;
103209867Snwhitehorn				break;
104209867Snwhitehorn			case 'k':
105209867Snwhitehorn				skip *= 1024;
106209867Snwhitehorn				break;
107209867Snwhitehorn			case 'm':
108209867Snwhitehorn				skip *= 1048576;
109209867Snwhitehorn				break;
110209867Snwhitehorn			}
111209867Snwhitehorn			break;
11290075Sobrien		case 'v':
11390075Sobrien			vflag = ALL;
11490075Sobrien			break;
11590075Sobrien		case 'x':
11690075Sobrien			add("\"%07.7_Ax\n\"");
11790075Sobrien			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
11890075Sobrien			break;
11990075Sobrien		case '?':
12090075Sobrien			usage();
12190075Sobrien		}
12290075Sobrien
12390075Sobrien	if (!fshead) {
12490075Sobrien		add("\"%07.7_Ax\n\"");
12590075Sobrien		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
12690075Sobrien	}
127117395Skan
128132718Skan	*argvp += optind;
129117395Skan}
130117395Skan
13190075Sobrienvoid
132117395Skanusage(void)
13390075Sobrien{
13490075Sobrien	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
13590075Sobrien"usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length]",
13690075Sobrien"               [-s skip] [file ...]",
13790075Sobrien"       hd      [-bcdovx]  [-e fmt] [-f fmt_file] [-n length]",
13890075Sobrien"               [-s skip] [file ...]");
139117395Skan	exit(1);
140209867Snwhitehorn}
141117395Skan