xref: /csrg-svn/usr.bin/hexdump/odsyntax.c (revision 69239)
141457Sbostic /*-
262031Sbostic  * Copyright (c) 1990, 1993
362031Sbostic  *	The Regents of the University of California.  All rights reserved.
441457Sbostic  *
541457Sbostic  * %sccs.include.redist.c%
641457Sbostic  */
741457Sbostic 
841457Sbostic #ifndef lint
9*69239Sbostic static char sccsid[] = "@(#)odsyntax.c	8.2 (Berkeley) 05/04/95";
1041457Sbostic #endif /* not lint */
1141457Sbostic 
1241457Sbostic #include <sys/types.h>
1355201Sbostic 
14*69239Sbostic #include <ctype.h>
15*69239Sbostic #include <stdio.h>
1645217Sbostic #include <stdlib.h>
17*69239Sbostic #include <unistd.h>
18*69239Sbostic 
1941457Sbostic #include "hexdump.h"
2041457Sbostic 
2141457Sbostic int deprecated;
2241457Sbostic 
2355201Sbostic static void odoffset __P((int, char ***));
2455201Sbostic static void odprecede __P((void));
2555201Sbostic 
2655201Sbostic void
oldsyntax(argc,argvp)2741457Sbostic oldsyntax(argc, argvp)
2841457Sbostic 	int argc;
2941457Sbostic 	char ***argvp;
3041457Sbostic {
3141457Sbostic 	extern enum _vflag vflag;
3241457Sbostic 	extern FS *fshead;
3347154Sbostic 	int ch;
3441457Sbostic 	char **argv;
3541457Sbostic 
3641457Sbostic 	deprecated = 1;
3741457Sbostic 	argv = *argvp;
3841457Sbostic 	while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != EOF)
3941457Sbostic 		switch (ch) {
4041457Sbostic 		case 'a':
4147154Sbostic 			odprecede();
4242362Sbostic 			add("16/1 \"%3_u \" \"\\n\"");
4341457Sbostic 			break;
4441457Sbostic 		case 'B':
4541457Sbostic 		case 'o':
4647154Sbostic 			odprecede();
4742362Sbostic 			add("8/2 \" %06o \" \"\\n\"");
4841457Sbostic 			break;
4941457Sbostic 		case 'b':
5047154Sbostic 			odprecede();
5142362Sbostic 			add("16/1 \"%03o \" \"\\n\"");
5241457Sbostic 			break;
5341457Sbostic 		case 'c':
5447154Sbostic 			odprecede();
5542362Sbostic 			add("16/1 \"%3_c \" \"\\n\"");
5641457Sbostic 			break;
5741457Sbostic 		case 'd':
5847154Sbostic 			odprecede();
5942362Sbostic 			add("8/2 \"  %05u \" \"\\n\"");
6041457Sbostic 			break;
6141457Sbostic 		case 'D':
6247154Sbostic 			odprecede();
6342362Sbostic 			add("4/4 \"     %010u \" \"\\n\"");
6441457Sbostic 			break;
6541457Sbostic 		case 'e':		/* undocumented in od */
6641457Sbostic 		case 'F':
6747154Sbostic 			odprecede();
6842362Sbostic 			add("2/8 \"          %21.14e \" \"\\n\"");
6941457Sbostic 			break;
7041457Sbostic 
7141457Sbostic 		case 'f':
7247154Sbostic 			odprecede();
7342362Sbostic 			add("4/4 \" %14.7e \" \"\\n\"");
7441457Sbostic 			break;
7541457Sbostic 		case 'H':
7641457Sbostic 		case 'X':
7747154Sbostic 			odprecede();
7842362Sbostic 			add("4/4 \"       %08x \" \"\\n\"");
7941457Sbostic 			break;
8041457Sbostic 		case 'h':
8141457Sbostic 		case 'x':
8247154Sbostic 			odprecede();
8342362Sbostic 			add("8/2 \"   %04x \" \"\\n\"");
8441457Sbostic 			break;
8541457Sbostic 		case 'I':
8641457Sbostic 		case 'L':
8741457Sbostic 		case 'l':
8847154Sbostic 			odprecede();
8942362Sbostic 			add("4/4 \"    %11d \" \"\\n\"");
9041457Sbostic 			break;
9141457Sbostic 		case 'i':
9247154Sbostic 			odprecede();
9342362Sbostic 			add("8/2 \" %6d \" \"\\n\"");
9441457Sbostic 			break;
9541457Sbostic 		case 'O':
9647154Sbostic 			odprecede();
9742362Sbostic 			add("4/4 \"    %011o \" \"\\n\"");
9841457Sbostic 			break;
9941457Sbostic 		case 'v':
10041457Sbostic 			vflag = ALL;
10141457Sbostic 			break;
10241457Sbostic 		case 'P':
10341457Sbostic 		case 'p':
10441457Sbostic 		case 's':
10541457Sbostic 		case 'w':
10641457Sbostic 		case '?':
10741457Sbostic 		default:
10841457Sbostic 			(void)fprintf(stderr,
10941457Sbostic 			    "od: od(1) has been deprecated for hexdump(1).\n");
11041457Sbostic 			if (ch != '?')
11141457Sbostic 				(void)fprintf(stderr,
11241457Sbostic "od: hexdump(1) compatibility doesn't support the -%c option%s\n",
11341457Sbostic 				    ch, ch == 's' ? "; see strings(1)." : ".");
11441457Sbostic 			usage();
11541457Sbostic 		}
11641457Sbostic 
11741457Sbostic 	if (!fshead) {
11841457Sbostic 		add("\"%07.7_Ao\n\"");
11941457Sbostic 		add("\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
12041457Sbostic 	}
12141457Sbostic 
12245217Sbostic 	argc -= optind;
12341457Sbostic 	*argvp += optind;
12445217Sbostic 
12552141Sbostic 	if (argc)
12652141Sbostic 		odoffset(argc, argvp);
12741457Sbostic }
12845217Sbostic 
12955201Sbostic static void
odoffset(argc,argvp)13045217Sbostic odoffset(argc, argvp)
13145217Sbostic 	int argc;
13245217Sbostic 	char ***argvp;
13345217Sbostic {
13445217Sbostic 	extern off_t skip;
13545217Sbostic 	register char *num, *p;
13645217Sbostic 	int base;
13745217Sbostic 	char *end;
13845217Sbostic 
13945217Sbostic 	/*
14045217Sbostic 	 * The offset syntax of od(1) was genuinely bizarre.  First, if
14145217Sbostic 	 * it started with a plus it had to be an offset.  Otherwise, if
14245217Sbostic 	 * there were at least two arguments, a number or lower-case 'x'
14345217Sbostic 	 * followed by a number makes it an offset.  By default it was
14445217Sbostic 	 * octal; if it started with 'x' or '0x' it was hex.  If it ended
14545217Sbostic 	 * in a '.', it was decimal.  If a 'b' or 'B' was appended, it
14645217Sbostic 	 * multiplied the number by 512 or 1024 byte units.  There was
14745217Sbostic 	 * no way to assign a block count to a hex offset.
14845217Sbostic 	 *
14952141Sbostic 	 * We assume it's a file if the offset is bad.
15045217Sbostic 	 */
15152790Sbostic 	p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
15252790Sbostic 
15345217Sbostic 	if (*p != '+' && (argc < 2 ||
15452141Sbostic 	    (!isdigit(p[0]) && (p[0] != 'x' || !isxdigit(p[1])))))
15545217Sbostic 		return;
15645217Sbostic 
15745217Sbostic 	base = 0;
15845217Sbostic 	/*
15945217Sbostic 	 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
16045217Sbostic 	 * set base.
16145217Sbostic 	 */
16245217Sbostic 	if (p[0] == '+')
16345217Sbostic 		++p;
16452141Sbostic 	if (p[0] == 'x' && isxdigit(p[1])) {
16545217Sbostic 		++p;
16645217Sbostic 		base = 16;
16745217Sbostic 	} else if (p[0] == '0' && p[1] == 'x') {
16845217Sbostic 		p += 2;
16945217Sbostic 		base = 16;
17045217Sbostic 	}
17145217Sbostic 
17245217Sbostic 	/* skip over the number */
17345217Sbostic 	if (base == 16)
17452141Sbostic 		for (num = p; isxdigit(*p); ++p);
17545217Sbostic 	else
17645217Sbostic 		for (num = p; isdigit(*p); ++p);
17745217Sbostic 
17845217Sbostic 	/* check for no number */
17945217Sbostic 	if (num == p)
18045217Sbostic 		return;
18145217Sbostic 
18245217Sbostic 	/* if terminates with a '.', base is decimal */
18345217Sbostic 	if (*p == '.') {
18445217Sbostic 		if (base)
18545217Sbostic 			return;
18645217Sbostic 		base = 10;
18745217Sbostic 	}
18845217Sbostic 
18945217Sbostic 	skip = strtol(num, &end, base ? base : 8);
19045217Sbostic 
19145217Sbostic 	/* if end isn't the same as p, we got a non-octal digit */
19252790Sbostic 	if (end != p) {
19345217Sbostic 		skip = 0;
19452790Sbostic 		return;
19552790Sbostic 	}
19652790Sbostic 
19752790Sbostic 	if (*p)
19852790Sbostic 		if (*p == 'B') {
19952790Sbostic 			skip *= 1024;
20045217Sbostic 			++p;
20152790Sbostic 		} else if (*p == 'b') {
20252790Sbostic 			skip *= 512;
20352790Sbostic 			++p;
20445217Sbostic 		}
20552790Sbostic 
20652790Sbostic 	if (*p) {
20752790Sbostic 		skip = 0;
20852790Sbostic 		return;
20952790Sbostic 	}
21052790Sbostic 
21152790Sbostic 	/*
21252790Sbostic 	 * If the offset uses a non-octal base, the base of the offset
21352790Sbostic 	 * is changed as well.  This isn't pretty, but it's easy.
21452790Sbostic 	 */
21545217Sbostic #define	TYPE_OFFSET	7
21652790Sbostic 	if (base == 16) {
21752790Sbostic 		fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
21852790Sbostic 		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
21952790Sbostic 	} else if (base == 10) {
22052790Sbostic 		fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
22152790Sbostic 		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
22245217Sbostic 	}
22352790Sbostic 
22452790Sbostic 	/* Terminate file list. */
22552790Sbostic 	(*argvp)[1] = NULL;
22645217Sbostic }
22747154Sbostic 
22847154Sbostic static void
odprecede()22947154Sbostic odprecede()
23047154Sbostic {
23147154Sbostic 	static int first = 1;
23247154Sbostic 
23347154Sbostic 	if (first) {
23447154Sbostic 		first = 0;
23547154Sbostic 		add("\"%07.7_Ao\n\"");
23647154Sbostic 		add("\"%07.7_ao  \"");
23747154Sbostic 	} else
23847154Sbostic 		add("\"         \"");
23947154Sbostic }
240