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