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