xref: /freebsd/usr.bin/hexdump/hexsyntax.c (revision 4b9d6057)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 
34 #include <err.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "hexdump.h"
41 
42 off_t skip;				/* bytes to skip */
43 
44 void
45 newsyntax(int argc, char ***argvp)
46 {
47 	int ch;
48 	char *p, **argv;
49 
50 	argv = *argvp;
51 	if ((p = strrchr(argv[0], 'h')) != NULL &&
52 	    strcmp(p, "hd") == 0) {
53 		/* "Canonical" format, implies -C. */
54 		add("\"%08.8_Ax\n\"");
55 		add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
56 		add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
57 	}
58 	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
59 		switch (ch) {
60 		case 'b':
61 			add("\"%07.7_Ax\n\"");
62 			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
63 			break;
64 		case 'c':
65 			add("\"%07.7_Ax\n\"");
66 			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
67 			break;
68 		case 'C':
69 			add("\"%08.8_Ax\n\"");
70 			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
71 			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
72 			break;
73 		case 'd':
74 			add("\"%07.7_Ax\n\"");
75 			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
76 			break;
77 		case 'e':
78 			add(optarg);
79 			break;
80 		case 'f':
81 			addfile(optarg);
82 			break;
83 		case 'n':
84 			if ((length = atoi(optarg)) < 0)
85 				errx(1, "%s: bad length value", optarg);
86 			break;
87 		case 'o':
88 			add("\"%07.7_Ax\n\"");
89 			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
90 			break;
91 		case 's':
92 			if ((skip = strtoll(optarg, &p, 0)) < 0)
93 				errx(1, "%s: bad skip value", optarg);
94 			switch(*p) {
95 			case 'b':
96 				skip *= 512;
97 				break;
98 			case 'k':
99 				skip *= 1024;
100 				break;
101 			case 'm':
102 				skip *= 1048576;
103 				break;
104 			}
105 			break;
106 		case 'v':
107 			vflag = ALL;
108 			break;
109 		case 'x':
110 			add("\"%07.7_Ax\n\"");
111 			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
112 			break;
113 		case '?':
114 			usage();
115 		}
116 
117 	if (!fshead) {
118 		add("\"%07.7_Ax\n\"");
119 		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
120 	}
121 
122 	*argvp += optind;
123 }
124 
125 void
126 usage(void)
127 {
128 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
129 "usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length]",
130 "               [-s skip] [file ...]",
131 "       hd      [-bcdovx]  [-e fmt] [-f fmt_file] [-n length]",
132 "               [-s skip] [file ...]");
133 	exit(1);
134 }
135