11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1990, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
301590Srgrimes#ifndef lint
3127315Scharnier#if 0
3223693Speterstatic char sccsid[] = "@(#)hexsyntax.c	8.2 (Berkeley) 5/4/95";
3327315Scharnier#endif
341590Srgrimes#endif /* not lint */
3599112Sobrien#include <sys/cdefs.h>
3699112Sobrien__FBSDID("$FreeBSD$");
371590Srgrimes
381590Srgrimes#include <sys/types.h>
391590Srgrimes
4027315Scharnier#include <err.h>
411590Srgrimes#include <stdio.h>
421590Srgrimes#include <stdlib.h>
431590Srgrimes#include <string.h>
4423693Speter#include <unistd.h>
4523693Speter
461590Srgrimes#include "hexdump.h"
471590Srgrimes
481590Srgrimesoff_t skip;				/* bytes to skip */
491590Srgrimes
501590Srgrimesvoid
51102944Sdwmalonenewsyntax(int argc, char ***argvp)
521590Srgrimes{
531590Srgrimes	int ch;
541590Srgrimes	char *p, **argv;
551590Srgrimes
561590Srgrimes	argv = *argvp;
57229403Sed	if ((p = strrchr(argv[0], 'h')) != NULL &&
5818550Sjoerg	    strcmp(p, "hd") == 0) {
5918550Sjoerg		/* "Canonical" format, implies -C. */
6018550Sjoerg		add("\"%08.8_Ax\n\"");
6118693Speter		add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
6218550Sjoerg		add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
6318550Sjoerg	}
6424360Simp	while ((ch = getopt(argc, argv, "bcCde:f:n:os:vx")) != -1)
651590Srgrimes		switch (ch) {
661590Srgrimes		case 'b':
671590Srgrimes			add("\"%07.7_Ax\n\"");
681590Srgrimes			add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"");
691590Srgrimes			break;
701590Srgrimes		case 'c':
711590Srgrimes			add("\"%07.7_Ax\n\"");
721590Srgrimes			add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"");
731590Srgrimes			break;
7418325Sphk		case 'C':
7518325Sphk			add("\"%08.8_Ax\n\"");
7618693Speter			add("\"%08.8_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ");
7718550Sjoerg			add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
7818325Sphk			break;
791590Srgrimes		case 'd':
801590Srgrimes			add("\"%07.7_Ax\n\"");
811590Srgrimes			add("\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"");
821590Srgrimes			break;
831590Srgrimes		case 'e':
841590Srgrimes			add(optarg);
851590Srgrimes			break;
861590Srgrimes		case 'f':
871590Srgrimes			addfile(optarg);
881590Srgrimes			break;
891590Srgrimes		case 'n':
901590Srgrimes			if ((length = atoi(optarg)) < 0)
9127315Scharnier				errx(1, "%s: bad length value", optarg);
921590Srgrimes			break;
931590Srgrimes		case 'o':
941590Srgrimes			add("\"%07.7_Ax\n\"");
951590Srgrimes			add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
961590Srgrimes			break;
971590Srgrimes		case 's':
9882766Sache			if ((skip = strtoll(optarg, &p, 0)) < 0)
9927315Scharnier				errx(1, "%s: bad skip value", optarg);
1001590Srgrimes			switch(*p) {
1011590Srgrimes			case 'b':
1021590Srgrimes				skip *= 512;
1031590Srgrimes				break;
1041590Srgrimes			case 'k':
1051590Srgrimes				skip *= 1024;
1061590Srgrimes				break;
1071590Srgrimes			case 'm':
1081590Srgrimes				skip *= 1048576;
1091590Srgrimes				break;
1101590Srgrimes			}
1111590Srgrimes			break;
1121590Srgrimes		case 'v':
1131590Srgrimes			vflag = ALL;
1141590Srgrimes			break;
1151590Srgrimes		case 'x':
1161590Srgrimes			add("\"%07.7_Ax\n\"");
1171590Srgrimes			add("\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"");
1181590Srgrimes			break;
1191590Srgrimes		case '?':
1201590Srgrimes			usage();
1211590Srgrimes		}
1221590Srgrimes
1231590Srgrimes	if (!fshead) {
1241590Srgrimes		add("\"%07.7_Ax\n\"");
1251590Srgrimes		add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
1261590Srgrimes	}
1271590Srgrimes
1281590Srgrimes	*argvp += optind;
1291590Srgrimes}
1301590Srgrimes
1311590Srgrimesvoid
132102944Sdwmaloneusage(void)
1331590Srgrimes{
13427315Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
13527315Scharnier"usage: hexdump [-bcCdovx] [-e fmt] [-f fmt_file] [-n length]",
13627315Scharnier"               [-s skip] [file ...]",
13727315Scharnier"       hd      [-bcdovx]  [-e fmt] [-f fmt_file] [-n length]",
13827315Scharnier"               [-s skip] [file ...]");
1391590Srgrimes	exit(1);
1401590Srgrimes}
141