xref: /netbsd-src/usr.bin/hexdump/hexsyntax.c (revision e45d81596048059cf412e70f3b6cfdf4cc643513)
1*e45d8159Schristos /*	$NetBSD: hexsyntax.c,v 1.14 2010/11/27 20:46:38 christos Exp $	*/
29d225a17Stls 
361f28255Scgd /*-
473973984Smrg  * Copyright (c) 1990, 1993
573973984Smrg  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32171d6532Slukem #if HAVE_NBTOOL_CONFIG_H
33171d6532Slukem #include "nbtool_config.h"
34171d6532Slukem #endif
35171d6532Slukem 
36c6810a02Slukem #include <sys/cdefs.h>
37171d6532Slukem #if !defined(lint)
38769693f9Smikel #if 0
3973973984Smrg static char sccsid[] = "@(#)hexsyntax.c	8.2 (Berkeley) 5/4/95";
40769693f9Smikel #else
41*e45d8159Schristos __RCSID("$NetBSD: hexsyntax.c,v 1.14 2010/11/27 20:46:38 christos Exp $");
42769693f9Smikel #endif
4361f28255Scgd #endif /* not lint */
4461f28255Scgd 
4561f28255Scgd #include <sys/types.h>
4673973984Smrg 
47c6810a02Slukem #include <err.h>
4861f28255Scgd #include <stdio.h>
49769693f9Smikel #include <stdlib.h>
5073973984Smrg #include <string.h>
5173973984Smrg #include <unistd.h>
5273973984Smrg 
5361f28255Scgd #include "hexdump.h"
5461f28255Scgd 
5561f28255Scgd off_t skip;				/* bytes to skip */
5661f28255Scgd 
57769693f9Smikel void
hexsyntax(int argc,char *** argvp)58*e45d8159Schristos hexsyntax(int argc, char ***argvp)
5961f28255Scgd {
6061f28255Scgd 	int ch;
6161f28255Scgd 	char *p, **argv;
6261f28255Scgd 
6361f28255Scgd 	argv = *argvp;
64757abd14Sjeremy 	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
6561f28255Scgd 		switch (ch) {
6661f28255Scgd 		case 'b':
6761f28255Scgd 			add("\"%07.7_Ax\n\"");
6861f28255Scgd 			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
6961f28255Scgd 			break;
7061f28255Scgd 		case 'c':
7161f28255Scgd 			add("\"%07.7_Ax\n\"");
7261f28255Scgd 			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
7361f28255Scgd 			break;
74757abd14Sjeremy 		case 'C':
75757abd14Sjeremy 			add("\"%08.8_Ax\n\"");
76757abd14Sjeremy 			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
77757abd14Sjeremy 			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
78757abd14Sjeremy 			break;
7961f28255Scgd 		case 'd':
8061f28255Scgd 			add("\"%07.7_Ax\n\"");
8161f28255Scgd 			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
8261f28255Scgd 			break;
8361f28255Scgd 		case 'e':
8461f28255Scgd 			add(optarg);
8561f28255Scgd 			break;
8661f28255Scgd 		case 'f':
8761f28255Scgd 			addfile(optarg);
8861f28255Scgd 			break;
8961f28255Scgd 		case 'n':
9073973984Smrg 			if ((length = atoi(optarg)) < 0)
91c6810a02Slukem 				errx(1, "%s: bad length value", optarg);
9261f28255Scgd 			break;
9361f28255Scgd 		case 'o':
9461f28255Scgd 			add("\"%07.7_Ax\n\"");
9561f28255Scgd 			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
9661f28255Scgd 			break;
9761f28255Scgd 		case 's':
9873973984Smrg 			if ((skip = strtol(optarg, &p, 0)) < 0)
99c6810a02Slukem 				errx(1, "%s: bad skip value", optarg);
10061f28255Scgd 			switch(*p) {
10161f28255Scgd 			case 'b':
10261f28255Scgd 				skip *= 512;
10361f28255Scgd 				break;
10461f28255Scgd 			case 'k':
10561f28255Scgd 				skip *= 1024;
10661f28255Scgd 				break;
10761f28255Scgd 			case 'm':
10861f28255Scgd 				skip *= 1048576;
10961f28255Scgd 				break;
11061f28255Scgd 			}
11161f28255Scgd 			break;
11261f28255Scgd 		case 'v':
11361f28255Scgd 			vflag = ALL;
11461f28255Scgd 			break;
11561f28255Scgd 		case 'x':
11661f28255Scgd 			add("\"%07.7_Ax\n\"");
11761f28255Scgd 			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
11861f28255Scgd 			break;
11961f28255Scgd 		case '?':
12061f28255Scgd 			usage();
12161f28255Scgd 		}
12261f28255Scgd 
12361f28255Scgd 	if (!fshead) {
12461f28255Scgd 		add("\"%07.7_Ax\n\"");
12561f28255Scgd 		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
12661f28255Scgd 	}
12761f28255Scgd 
12861f28255Scgd 	*argvp += optind;
12961f28255Scgd }
130