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