xref: /freebsd/usr.bin/hexdump/hexsyntax.c (revision 5e3934b1)
19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1990, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #include <sys/types.h>
339b50d902SRodney W. Grimes 
3410731702SPhilippe Charnier #include <err.h>
359b50d902SRodney W. Grimes #include <stdio.h>
369b50d902SRodney W. Grimes #include <stdlib.h>
379b50d902SRodney W. Grimes #include <string.h>
38df3f5d9dSPeter Wemm #include <unistd.h>
39df3f5d9dSPeter Wemm 
409b50d902SRodney W. Grimes #include "hexdump.h"
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes off_t skip;				/* bytes to skip */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes void
newsyntax(int argc,char *** argvp)45f4ac32deSDavid Malone newsyntax(int argc, char ***argvp)
469b50d902SRodney W. Grimes {
479b50d902SRodney W. Grimes 	int ch;
489b50d902SRodney W. Grimes 	char *p, **argv;
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes 	argv = *argvp;
51b3608ae1SEd Schouten 	if ((p = strrchr(argv[0], 'h')) != NULL &&
5238d54d33SJoerg Wunsch 	    strcmp(p, "hd") == 0) {
5338d54d33SJoerg Wunsch 		/* "Canonical" format, implies -C. */
5438d54d33SJoerg Wunsch 		add("\"%08.8_Ax\n\"");
557520d118SPeter Wemm 		add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
5638d54d33SJoerg Wunsch 		add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
5738d54d33SJoerg Wunsch 	}
581c8af878SWarner Losh 	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
599b50d902SRodney W. Grimes 		switch (ch) {
609b50d902SRodney W. Grimes 		case 'b':
619b50d902SRodney W. Grimes 			add("\"%07.7_Ax\n\"");
629b50d902SRodney W. Grimes 			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
639b50d902SRodney W. Grimes 			break;
649b50d902SRodney W. Grimes 		case 'c':
659b50d902SRodney W. Grimes 			add("\"%07.7_Ax\n\"");
669b50d902SRodney W. Grimes 			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
679b50d902SRodney W. Grimes 			break;
68f84276e9SPoul-Henning Kamp 		case 'C':
69f84276e9SPoul-Henning Kamp 			add("\"%08.8_Ax\n\"");
707520d118SPeter Wemm 			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
71f84276e9SPoul-Henning Kamp 			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
72f84276e9SPoul-Henning Kamp 			break;
739b50d902SRodney W. Grimes 		case 'd':
749b50d902SRodney W. Grimes 			add("\"%07.7_Ax\n\"");
759b50d902SRodney W. Grimes 			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
769b50d902SRodney W. Grimes 			break;
779b50d902SRodney W. Grimes 		case 'e':
789b50d902SRodney W. Grimes 			add(optarg);
799b50d902SRodney W. Grimes 			break;
809b50d902SRodney W. Grimes 		case 'f':
819b50d902SRodney W. Grimes 			addfile(optarg);
829b50d902SRodney W. Grimes 			break;
839b50d902SRodney W. Grimes 		case 'n':
849b50d902SRodney W. Grimes 			if ((length = atoi(optarg)) < 0)
8510731702SPhilippe Charnier 				errx(1, "%s: bad length value", optarg);
869b50d902SRodney W. Grimes 			break;
879b50d902SRodney W. Grimes 		case 'o':
889b50d902SRodney W. Grimes 			add("\"%07.7_Ax\n\"");
899b50d902SRodney W. Grimes 			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
909b50d902SRodney W. Grimes 			break;
919b50d902SRodney W. Grimes 		case 's':
927a27e657SAndrey A. Chernov 			if ((skip = strtoll(optarg, &p, 0)) < 0)
9310731702SPhilippe Charnier 				errx(1, "%s: bad skip value", optarg);
949b50d902SRodney W. Grimes 			switch(*p) {
959b50d902SRodney W. Grimes 			case 'b':
969b50d902SRodney W. Grimes 				skip *= 512;
979b50d902SRodney W. Grimes 				break;
989b50d902SRodney W. Grimes 			case 'k':
999b50d902SRodney W. Grimes 				skip *= 1024;
1009b50d902SRodney W. Grimes 				break;
1019b50d902SRodney W. Grimes 			case 'm':
1029b50d902SRodney W. Grimes 				skip *= 1048576;
1039b50d902SRodney W. Grimes 				break;
1049b50d902SRodney W. Grimes 			}
1059b50d902SRodney W. Grimes 			break;
1069b50d902SRodney W. Grimes 		case 'v':
1079b50d902SRodney W. Grimes 			vflag = ALL;
1089b50d902SRodney W. Grimes 			break;
1099b50d902SRodney W. Grimes 		case 'x':
1109b50d902SRodney W. Grimes 			add("\"%07.7_Ax\n\"");
1119b50d902SRodney W. Grimes 			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
1129b50d902SRodney W. Grimes 			break;
1139b50d902SRodney W. Grimes 		case '?':
1149b50d902SRodney W. Grimes 			usage();
1159b50d902SRodney W. Grimes 		}
1169b50d902SRodney W. Grimes 
1179b50d902SRodney W. Grimes 	if (!fshead) {
1189b50d902SRodney W. Grimes 		add("\"%07.7_Ax\n\"");
1199b50d902SRodney W. Grimes 		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
1209b50d902SRodney W. Grimes 	}
1219b50d902SRodney W. Grimes 
1229b50d902SRodney W. Grimes 	*argvp += optind;
1239b50d902SRodney W. Grimes }
1249b50d902SRodney W. Grimes 
1259b50d902SRodney W. Grimes void
usage(void)126f4ac32deSDavid Malone usage(void)
1279b50d902SRodney W. Grimes {
12810731702SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
12910731702SPhilippe Charnier "usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length]",
13010731702SPhilippe Charnier "               [-s skip] [file ...]",
13110731702SPhilippe Charnier "       hd      [-bcdovx]  [-e fmt] [-f fmt_file] [-n length]",
13210731702SPhilippe Charnier "               [-s skip] [file ...]");
1339b50d902SRodney W. Grimes 	exit(1);
1349b50d902SRodney W. Grimes }
135