Home | History | Annotate | Line # | Download | only in hexdump
odsyntax.c revision 1.2
      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 
    174         p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
    175 
    176 	if (*p != '+' && (argc < 2 ||
    177 	    (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1])))))
    178 		return;
    179 
    180 	base = 0;
    181 	/*
    182 	 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
    183 	 * set base.
    184 	 */
    185 	if (p[0] == '+')
    186 		++p;
    187 	if (p[0] == 'x' && ishexdigit(p[1])) {
    188 		++p;
    189 		base = 16;
    190 	} else if (p[0] == '0' && p[1] == 'x') {
    191 		p += 2;
    192 		base = 16;
    193 	}
    194 
    195 	/* skip over the number */
    196 	if (base == 16)
    197 		for (num = p; ishexdigit(*p); ++p);
    198 	else
    199 		for (num = p; isdigit(*p); ++p);
    200 
    201 	/* check for no number */
    202 	if (num == p)
    203 		return;
    204 
    205 	/* if terminates with a '.', base is decimal */
    206 	if (*p == '.') {
    207 		if (base)
    208 			return;
    209 		base = 10;
    210 	}
    211 
    212 	skip = strtol(num, &end, base ? base : 8);
    213 
    214 	/* if end isn't the same as p, we got a non-octal digit */
    215 	if (end != p)
    216 		skip = 0;
    217 	else {
    218 		if (*p) {
    219 			if (*p == 'b')
    220 				skip *= 512;
    221 			else if (*p == 'B')
    222 				skip *= 1024;
    223 			++p;
    224 		}
    225 		if (*p)
    226 			skip = 0;
    227 		else {
    228 			++*argvp;
    229 			/*
    230 			 * If the offset uses a non-octal base, the base of
    231 			 * the offset is changed as well.  This isn't pretty,
    232 			 * but it's easy.
    233 			 */
    234 #define	TYPE_OFFSET	7
    235 			if (base == 16) {
    236 				fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
    237 				fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
    238 			} else if (base == 10) {
    239 				fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
    240 				fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
    241 			}
    242 		}
    243 	}
    244 }
    245 
    246 static void
    247 odprecede()
    248 {
    249 	static int first = 1;
    250 
    251 	if (first) {
    252 		first = 0;
    253 		add("\"%07.7_Ao\n\"");
    254 		add("\"%07.7_ao  \"");
    255 	} else
    256 		add("\"         \"");
    257 }
    258