xref: /openbsd-src/usr.bin/hexdump/hexsyntax.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: hexsyntax.c,v 1.10 2007/02/06 20:22:19 jmc 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 #ifndef lint
34 /*static char sccsid[] = "from: @(#)hexsyntax.c	5.2 (Berkeley) 5/8/90";*/
35 static char rcsid[] = "$OpenBSD: hexsyntax.c,v 1.10 2007/02/06 20:22:19 jmc Exp $";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "hexdump.h"
48 
49 off_t skip;				/* bytes to skip */
50 
51 void
52 newsyntax(int argc, char ***argvp)
53 {
54 	int ch;
55 	char *p, **argv;
56 
57 	argv = *argvp;
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 			errno = 0;
85 			if ((length = strtol(optarg, NULL, 0)) < 0 ||
86 			    errno != 0)
87 				errx(1, "%s: bad length value", optarg);
88 			break;
89 		case 'o':
90 			add("\"%07.7_Ax\n\"");
91 			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
92 			break;
93 		case 's':
94 			errno = 0;
95 			if ((skip = (off_t)strtoll(optarg, &p, 0)) < 0 ||
96 			    errno != 0)
97 				errx(1, "%s: bad skip value", optarg);
98 			switch(*p) {
99 			case 'b':
100 				skip *= 512;
101 				break;
102 			case 'k':
103 				skip *= 1024;
104 				break;
105 			case 'm':
106 				skip *= 1048576;
107 				break;
108 			}
109 			break;
110 		case 'v':
111 			vflag = ALL;
112 			break;
113 		case 'x':
114 			add("\"%07.7_Ax\n\"");
115 			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
116 			break;
117 		case '?':
118 			usage();
119 		}
120 
121 	if (!fshead) {
122 		add("\"%07.7_Ax\n\"");
123 		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
124 	}
125 
126 	*argvp += optind;
127 }
128 
129 void
130 usage(void)
131 {
132 	extern char *__progname;
133 	fprintf(stderr, "usage: %s [-bCcdovx] [-e format_string] "
134 			"[-f format_file] [-n length]\n"
135 			"\t[-s offset] [file ...]\n", __progname);
136 	exit(1);
137 }
138