xref: /openbsd/usr.bin/hexdump/hexsyntax.c (revision fd84ef7e)
1 /*	$OpenBSD: hexsyntax.c,v 1.6 2001/12/30 08:17:32 pvalchev 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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)hexsyntax.c	5.2 (Berkeley) 5/8/90";*/
39 static char rcsid[] = "$OpenBSD: hexsyntax.c,v 1.6 2001/12/30 08:17:32 pvalchev Exp $";
40 #endif /* not lint */
41 
42 #include <sys/types.h>
43 
44 #include <err.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include "hexdump.h"
51 
52 off_t skip;				/* bytes to skip */
53 
54 void
55 newsyntax(argc, argvp)
56 	int argc;
57 	char ***argvp;
58 {
59 	int ch;
60 	char *p, **argv;
61 
62 	argv = *argvp;
63 	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
64 		switch (ch) {
65 		case 'b':
66 			add("\"%07.7_Ax\n\"");
67 			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
68 			break;
69 		case 'c':
70 			add("\"%07.7_Ax\n\"");
71 			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
72 			break;
73 		case 'C':
74 			add("\"%08.8_Ax\n\"");
75 			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
76 			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
77 			break;
78 		case 'd':
79 			add("\"%07.7_Ax\n\"");
80 			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
81 			break;
82 		case 'e':
83 			add(optarg);
84 			break;
85 		case 'f':
86 			addfile(optarg);
87 			break;
88 		case 'n':
89 			if ((length = atoi(optarg)) < 0)
90 				errx(1, "%s: bad length value", optarg);
91 			break;
92 		case 'o':
93 			add("\"%07.7_Ax\n\"");
94 			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
95 			break;
96 		case 's':
97 			if ((skip = strtol(optarg, &p, 0)) < 0)
98 				errx(1, "%s: bad skip value", optarg);
99 			switch(*p) {
100 			case 'b':
101 				skip *= 512;
102 				break;
103 			case 'k':
104 				skip *= 1024;
105 				break;
106 			case 'm':
107 				skip *= 1048576;
108 				break;
109 			}
110 			break;
111 		case 'v':
112 			vflag = ALL;
113 			break;
114 		case 'x':
115 			add("\"%07.7_Ax\n\"");
116 			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
117 			break;
118 		case '?':
119 			usage();
120 		}
121 
122 	if (!fshead) {
123 		add("\"%07.7_Ax\n\"");
124 		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
125 	}
126 
127 	*argvp += optind;
128 }
129 
130 void
131 usage()
132 {
133 	extern char *__progname;
134 	fprintf(stderr, "usage: %s [-bcCdovx] [-e fmt] [-f fmt_file] "
135 			"[-n length] [-s skip] [file ...]\n", __progname);
136 	exit(1);
137 }
138