xref: /openbsd-src/usr.bin/hexdump/hexsyntax.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: hexsyntax.c,v 1.8 2003/06/12 20:58:09 deraadt 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.8 2003/06/12 20:58:09 deraadt Exp $";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "hexdump.h"
47 
48 off_t skip;				/* bytes to skip */
49 
50 void
51 newsyntax(int argc, char ***argvp)
52 {
53 	int ch;
54 	char *p, **argv;
55 
56 	argv = *argvp;
57 	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
58 		switch (ch) {
59 		case 'b':
60 			add("\"%07.7_Ax\n\"");
61 			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
62 			break;
63 		case 'c':
64 			add("\"%07.7_Ax\n\"");
65 			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
66 			break;
67 		case 'C':
68 			add("\"%08.8_Ax\n\"");
69 			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
70 			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
71 			break;
72 		case 'd':
73 			add("\"%07.7_Ax\n\"");
74 			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
75 			break;
76 		case 'e':
77 			add(optarg);
78 			break;
79 		case 'f':
80 			addfile(optarg);
81 			break;
82 		case 'n':
83 			if ((length = atoi(optarg)) < 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 			if ((skip = strtol(optarg, &p, 0)) < 0)
92 				errx(1, "%s: bad skip value", optarg);
93 			switch(*p) {
94 			case 'b':
95 				skip *= 512;
96 				break;
97 			case 'k':
98 				skip *= 1024;
99 				break;
100 			case 'm':
101 				skip *= 1048576;
102 				break;
103 			}
104 			break;
105 		case 'v':
106 			vflag = ALL;
107 			break;
108 		case 'x':
109 			add("\"%07.7_Ax\n\"");
110 			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
111 			break;
112 		case '?':
113 			usage();
114 		}
115 
116 	if (!fshead) {
117 		add("\"%07.7_Ax\n\"");
118 		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
119 	}
120 
121 	*argvp += optind;
122 }
123 
124 void
125 usage(void)
126 {
127 	extern char *__progname;
128 	fprintf(stderr, "usage: %s [-bcCdovx] [-e fmt] [-f fmt_file] "
129 			"[-n length] [-s skip] [file ...]\n", __progname);
130 	exit(1);
131 }
132