Home | History | Annotate | Line # | Download | only in hexdump
odsyntax.c revision 1.10
      1 /*	$NetBSD: odsyntax.c,v 1.10 1998/12/19 16:43:39 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)odsyntax.c	8.2 (Berkeley) 5/4/95";
     40 #else
     41 __RCSID("$NetBSD: odsyntax.c,v 1.10 1998/12/19 16:43:39 christos Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 
     47 #include <ctype.h>
     48 #include <err.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <unistd.h>
     52 
     53 #include "hexdump.h"
     54 
     55 int deprecated;
     56 
     57 static void odoffset __P((int, char ***));
     58 static void odprecede __P((void));
     59 
     60 void
     61 oldsyntax(argc, argvp)
     62 	int argc;
     63 	char ***argvp;
     64 {
     65 	int ch;
     66 	char **argv;
     67 
     68 	deprecated = 1;
     69 	argv = *argvp;
     70 	while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != -1)
     71 		switch (ch) {
     72 		case 'a':
     73 			odprecede();
     74 			add("16/1 \"%3_u \" \"\\n\"");
     75 			break;
     76 		case 'B':
     77 		case 'o':
     78 			odprecede();
     79 			add("8/2 \" %06o \" \"\\n\"");
     80 			break;
     81 		case 'b':
     82 			odprecede();
     83 			add("16/1 \"%03o \" \"\\n\"");
     84 			break;
     85 		case 'c':
     86 			odprecede();
     87 			add("16/1 \"%3_c \" \"\\n\"");
     88 			break;
     89 		case 'd':
     90 			odprecede();
     91 			add("8/2 \"  %05u \" \"\\n\"");
     92 			break;
     93 		case 'D':
     94 			odprecede();
     95 			add("4/4 \"     %010u \" \"\\n\"");
     96 			break;
     97 		case 'e':		/* undocumented in od */
     98 		case 'F':
     99 			odprecede();
    100 			add("2/8 \"          %21.14e \" \"\\n\"");
    101 			break;
    102 
    103 		case 'f':
    104 			odprecede();
    105 			add("4/4 \" %14.7e \" \"\\n\"");
    106 			break;
    107 		case 'H':
    108 		case 'X':
    109 			odprecede();
    110 			add("4/4 \"       %08x \" \"\\n\"");
    111 			break;
    112 		case 'h':
    113 		case 'x':
    114 			odprecede();
    115 			add("8/2 \"   %04x \" \"\\n\"");
    116 			break;
    117 		case 'I':
    118 		case 'L':
    119 		case 'l':
    120 			odprecede();
    121 			add("4/4 \"    %11d \" \"\\n\"");
    122 			break;
    123 		case 'i':
    124 			odprecede();
    125 			add("8/2 \" %6d \" \"\\n\"");
    126 			break;
    127 		case 'O':
    128 			odprecede();
    129 			add("4/4 \"    %011o \" \"\\n\"");
    130 			break;
    131 		case 'v':
    132 			vflag = ALL;
    133 			break;
    134 		case 'P':
    135 		case 'p':
    136 		case 's':
    137 		case 'w':
    138 		case '?':
    139 		default:
    140 			warnx("od(1) has been deprecated for hexdump(1).");
    141 			if (ch != '?')
    142 				warnx(
    143 "hexdump(1) compatibility doesn't support the -%c option%s\n",
    144 				    ch, ch == 's' ? "; see strings(1)." : ".");
    145 			usage();
    146 		}
    147 
    148 	if (!fshead) {
    149 		add("\"%07.7_Ao\n\"");
    150 		add("\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
    151 	}
    152 
    153 	argc -= optind;
    154 	*argvp += optind;
    155 
    156 	if (argc)
    157 		odoffset(argc, argvp);
    158 }
    159 
    160 static void
    161 odoffset(argc, argvp)
    162 	int argc;
    163 	char ***argvp;
    164 {
    165 	char *num, *p;
    166 	int base;
    167 	char *end;
    168 
    169 	/*
    170 	 * The offset syntax of od(1) was genuinely bizarre.  First, if
    171 	 * it started with a plus it had to be an offset.  Otherwise, if
    172 	 * there were at least two arguments, a number or lower-case 'x'
    173 	 * followed by a number makes it an offset.  By default it was
    174 	 * octal; if it started with 'x' or '0x' it was hex.  If it ended
    175 	 * in a '.', it was decimal.  If a 'b' or 'B' was appended, it
    176 	 * multiplied the number by 512 or 1024 byte units.  There was
    177 	 * no way to assign a block count to a hex offset.
    178 	 *
    179 	 * We assume it's a file if the offset is bad.
    180 	 */
    181 	p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
    182 	if (!p)
    183 		return;
    184 
    185 	if (*p != '+' && (argc < 2 ||
    186 	    (!isdigit((unsigned char)p[0]) &&
    187 	    (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
    188 		return;
    189 
    190 	base = 0;
    191 	/*
    192 	 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
    193 	 * set base.
    194 	 */
    195 	if (p[0] == '+')
    196 		++p;
    197 	if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
    198 		++p;
    199 		base = 16;
    200 	} else if (p[0] == '0' && p[1] == 'x') {
    201 		p += 2;
    202 		base = 16;
    203 	}
    204 
    205 	/* skip over the number */
    206 	if (base == 16)
    207 		for (num = p; isxdigit((unsigned char)*p); ++p);
    208 	else
    209 		for (num = p; isdigit((unsigned char)*p); ++p);
    210 
    211 	/* check for no number */
    212 	if (num == p)
    213 		return;
    214 
    215 	/* if terminates with a '.', base is decimal */
    216 	if (*p == '.') {
    217 		if (base)
    218 			return;
    219 		base = 10;
    220 	}
    221 
    222 	skip = strtol(num, &end, base ? base : 8);
    223 
    224 	/* if end isn't the same as p, we got a non-octal digit */
    225 	if (end != p) {
    226 		skip = 0;
    227 		return;
    228 	}
    229 
    230 	if (*p) {
    231 		if (*p == 'B') {
    232 			skip *= 1024;
    233 			++p;
    234 		} else if (*p == 'b') {
    235 			skip *= 512;
    236 			++p;
    237 		}
    238 	}
    239 	if (*p) {
    240 		skip = 0;
    241 		return;
    242 	}
    243 	/*
    244 	 * If the offset uses a non-octal base, the base of the offset
    245 	 * is changed as well.  This isn't pretty, but it's easy.
    246 	 */
    247 #define	TYPE_OFFSET	7
    248 	if (base == 16) {
    249 		fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
    250 		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
    251 	} else if (base == 10) {
    252 		fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
    253 		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
    254 	}
    255 
    256 	/* Terminate file list. */
    257 	(*argvp)[1] = NULL;
    258 }
    259 
    260 static void
    261 odprecede()
    262 {
    263 	static int first = 1;
    264 
    265 	if (first) {
    266 		first = 0;
    267 		add("\"%07.7_Ao\n\"");
    268 		add("\"%07.7_ao  \"");
    269 	} else
    270 		add("\"         \"");
    271 }
    272