Home | History | Annotate | Line # | Download | only in hexdump
odsyntax.c revision 1.23
      1  1.23     perry /*	$NetBSD: odsyntax.c,v 1.23 2006/01/04 01:30:21 perry Exp $	*/
      2   1.5       tls 
      3   1.1       cgd /*-
      4   1.7       mrg  * Copyright (c) 1990, 1993
      5   1.7       mrg  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.18       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.20     lukem #if HAVE_NBTOOL_CONFIG_H
     33  1.20     lukem #include "nbtool_config.h"
     34  1.20     lukem #endif
     35  1.20     lukem 
     36   1.8     lukem #include <sys/cdefs.h>
     37  1.20     lukem #if !defined(lint)
     38   1.6     mikel #if 0
     39   1.7       mrg static char sccsid[] = "@(#)odsyntax.c	8.2 (Berkeley) 5/4/95";
     40   1.6     mikel #else
     41  1.23     perry __RCSID("$NetBSD: odsyntax.c,v 1.23 2006/01/04 01:30:21 perry Exp $");
     42   1.6     mikel #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/types.h>
     46   1.7       mrg 
     47   1.6     mikel #include <ctype.h>
     48   1.8     lukem #include <err.h>
     49   1.6     mikel #include <stdio.h>
     50   1.1       cgd #include <stdlib.h>
     51   1.7       mrg #include <unistd.h>
     52   1.7       mrg 
     53   1.1       cgd #include "hexdump.h"
     54   1.1       cgd 
     55  1.16     bjh21 struct odformat {
     56  1.16     bjh21 	char type;
     57  1.16     bjh21 	int nbytes;
     58  1.16     bjh21 	char const *format;
     59  1.16     bjh21 	int minwidth;
     60  1.16     bjh21 };
     61  1.16     bjh21 
     62  1.16     bjh21 struct odaddrformat {
     63  1.16     bjh21 	char type;
     64  1.16     bjh21 	char const *format1;
     65  1.16     bjh21 	char const *format2;
     66  1.16     bjh21 };
     67  1.16     bjh21 
     68   1.1       cgd int deprecated;
     69   1.1       cgd 
     70  1.23     perry static void odoffset(int, char ***);
     71  1.23     perry static void posixtypes(char const *);
     72  1.23     perry static void odprecede(void);
     73   1.6     mikel 
     74  1.11  christos 
     75   1.6     mikel void
     76  1.23     perry oldsyntax(int argc, char ***argvp)
     77   1.1       cgd {
     78   1.1       cgd 	int ch;
     79  1.12     bjh21 	char *p, **argv;
     80   1.1       cgd 
     81   1.1       cgd 	deprecated = 1;
     82   1.1       cgd 	argv = *argvp;
     83  1.13     bjh21 	while ((ch = getopt(argc, argv,
     84  1.13     bjh21 	    "aBbcDdeFfHhIij:LlN:OoPpst:wvXx")) != -1)
     85   1.1       cgd 		switch (ch) {
     86   1.1       cgd 		case 'a':
     87  1.16     bjh21 			posixtypes("a");
     88   1.1       cgd 			break;
     89   1.1       cgd 		case 'B':
     90   1.1       cgd 		case 'o':
     91  1.16     bjh21 			posixtypes("o2");
     92   1.1       cgd 			break;
     93   1.1       cgd 		case 'b':
     94  1.16     bjh21 			posixtypes("o1");
     95   1.1       cgd 			break;
     96   1.1       cgd 		case 'c':
     97  1.16     bjh21 			posixtypes("c");
     98   1.1       cgd 			break;
     99   1.1       cgd 		case 'd':
    100  1.16     bjh21 			posixtypes("u2");
    101   1.1       cgd 			break;
    102   1.1       cgd 		case 'D':
    103  1.16     bjh21 			posixtypes("u4");
    104   1.1       cgd 			break;
    105   1.1       cgd 		case 'e':		/* undocumented in od */
    106   1.1       cgd 		case 'F':
    107  1.16     bjh21 			posixtypes("f8");
    108   1.1       cgd 			break;
    109   1.1       cgd 		case 'f':
    110  1.16     bjh21 			posixtypes("f4");
    111   1.1       cgd 			break;
    112   1.1       cgd 		case 'H':
    113   1.1       cgd 		case 'X':
    114  1.16     bjh21 			posixtypes("x4");
    115   1.1       cgd 			break;
    116   1.1       cgd 		case 'h':
    117   1.1       cgd 		case 'x':
    118  1.16     bjh21 			posixtypes("x2");
    119   1.1       cgd 			break;
    120   1.1       cgd 		case 'I':
    121   1.1       cgd 		case 'L':
    122   1.1       cgd 		case 'l':
    123  1.16     bjh21 			posixtypes("d4");
    124   1.1       cgd 			break;
    125   1.1       cgd 		case 'i':
    126  1.16     bjh21 			posixtypes("d2");
    127  1.12     bjh21 			break;
    128  1.12     bjh21 		case 'j':
    129  1.12     bjh21 			if ((skip = strtol(optarg, &p, 0)) < 0)
    130  1.12     bjh21 				errx(1, "%s: bad skip value", optarg);
    131  1.12     bjh21 			switch(*p) {
    132  1.12     bjh21 			case 'b':
    133  1.12     bjh21 				skip *= 512;
    134  1.12     bjh21 				break;
    135  1.12     bjh21 			case 'k':
    136  1.12     bjh21 				skip *= 1024;
    137  1.12     bjh21 				break;
    138  1.12     bjh21 			case 'm':
    139  1.12     bjh21 				skip *= 1048576;
    140  1.12     bjh21 				break;
    141  1.12     bjh21 			}
    142  1.13     bjh21 			break;
    143  1.13     bjh21 		case 'N':
    144  1.13     bjh21 			if ((length = atoi(optarg)) < 0)
    145  1.13     bjh21 				errx(1, "%s: bad length value", optarg);
    146   1.1       cgd 			break;
    147   1.1       cgd 		case 'O':
    148  1.16     bjh21 			posixtypes("o4");
    149  1.11  christos 			break;
    150  1.11  christos 		case 't':
    151  1.14     bjh21 			posixtypes(optarg);
    152   1.1       cgd 			break;
    153   1.1       cgd 		case 'v':
    154   1.1       cgd 			vflag = ALL;
    155   1.1       cgd 			break;
    156   1.1       cgd 		case 'P':
    157   1.1       cgd 		case 'p':
    158   1.1       cgd 		case 's':
    159   1.1       cgd 		case 'w':
    160   1.1       cgd 		case '?':
    161   1.1       cgd 		default:
    162   1.8     lukem 			warnx("od(1) has been deprecated for hexdump(1).");
    163   1.1       cgd 			if (ch != '?')
    164   1.8     lukem 				warnx(
    165   1.8     lukem "hexdump(1) compatibility doesn't support the -%c option%s\n",
    166   1.1       cgd 				    ch, ch == 's' ? "; see strings(1)." : ".");
    167   1.1       cgd 			usage();
    168   1.1       cgd 		}
    169   1.1       cgd 
    170   1.1       cgd 	if (!fshead) {
    171   1.1       cgd 		add("\"%07.7_Ao\n\"");
    172   1.1       cgd 		add("\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
    173   1.1       cgd 	}
    174   1.1       cgd 
    175   1.1       cgd 	argc -= optind;
    176   1.1       cgd 	*argvp += optind;
    177   1.1       cgd 
    178   1.7       mrg 	if (argc)
    179   1.7       mrg 		odoffset(argc, argvp);
    180  1.14     bjh21 }
    181  1.14     bjh21 
    182  1.16     bjh21 /* formats used for -t */
    183  1.16     bjh21 
    184  1.16     bjh21 static const struct odformat odftab[] = {
    185  1.16     bjh21 	{ 'a', 1, "%3_u",  4 },
    186  1.16     bjh21 	{ 'c', 1, "%3_c",  4 },
    187  1.16     bjh21 	{ 'd', 1, "%4d",   5 },
    188  1.16     bjh21 	{ 'd', 2, "%6d",   6 },
    189  1.16     bjh21 	{ 'd', 4, "%11d", 11 },
    190  1.16     bjh21 	{ 'd', 8, "%20d", 20 },
    191  1.16     bjh21 	{ 'o', 1, "%03o",  4 },
    192  1.16     bjh21 	{ 'o', 2, "%06o",  7 },
    193  1.16     bjh21 	{ 'o', 4, "%011o", 12 },
    194  1.16     bjh21 	{ 'o', 8, "%022o", 23 },
    195  1.16     bjh21 	{ 'u', 1, "%03u" , 4 },
    196  1.16     bjh21 	{ 'u', 2, "%05u" , 6 },
    197  1.16     bjh21 	{ 'u', 4, "%010u", 11 },
    198  1.16     bjh21 	{ 'u', 8, "%020u", 21 },
    199  1.16     bjh21 	{ 'x', 1, "%02x",  3 },
    200  1.16     bjh21 	{ 'x', 2, "%04x",  5 },
    201  1.16     bjh21 	{ 'x', 4, "%08x",  9 },
    202  1.16     bjh21 	{ 'x', 8, "%016x", 17 },
    203  1.16     bjh21 	{ 'f', 4, "%14.7e",  15 },
    204  1.16     bjh21 	{ 'f', 8, "%21.14e", 22 },
    205  1.16     bjh21 	{ 0, 0, NULL, 0 }
    206  1.16     bjh21 };
    207  1.16     bjh21 
    208  1.14     bjh21 /*
    209  1.14     bjh21  * Interpret a POSIX-style -t argument.
    210  1.14     bjh21  */
    211  1.14     bjh21 static void
    212  1.23     perry posixtypes(char const *type_string)
    213  1.14     bjh21 {
    214  1.22     lukem 	int nbytes = 0;
    215  1.16     bjh21 	char *fmt, type, *tmp;
    216  1.16     bjh21 	struct odformat const *odf;
    217  1.14     bjh21 
    218  1.14     bjh21 	while (*type_string) {
    219  1.14     bjh21 		odprecede();
    220  1.16     bjh21 		switch ((type = *type_string++)) {
    221  1.14     bjh21 		case 'a':
    222  1.14     bjh21 		case 'c':
    223  1.16     bjh21 			nbytes = 1;
    224  1.14     bjh21 			break;
    225  1.14     bjh21 		case 'f':
    226  1.21       dsl 			if (isupper((unsigned char)*type_string)) {
    227  1.16     bjh21 				switch(*type_string) {
    228  1.16     bjh21 				case 'F':
    229  1.16     bjh21 					nbytes = sizeof(float);
    230  1.16     bjh21 					break;
    231  1.16     bjh21 				case 'D':
    232  1.16     bjh21 					nbytes = sizeof(double);
    233  1.16     bjh21 					break;
    234  1.16     bjh21 				case 'L':
    235  1.16     bjh21 					nbytes = sizeof(long double);
    236  1.16     bjh21 					break;
    237  1.16     bjh21 				default:
    238  1.16     bjh21 					warnx("Bad type-size qualifier '%c'",
    239  1.16     bjh21 					    *type_string);
    240  1.16     bjh21 					usage();
    241  1.16     bjh21 				}
    242  1.14     bjh21 				type_string++;
    243  1.21       dsl 			} else if (isdigit((unsigned char)*type_string)) {
    244  1.16     bjh21 				nbytes = strtol(type_string, &tmp, 10);
    245  1.16     bjh21 				type_string = tmp;
    246  1.16     bjh21 			} else
    247  1.16     bjh21 				nbytes = 8;
    248  1.14     bjh21 			break;
    249  1.14     bjh21 		case 'd':
    250  1.14     bjh21 		case 'o':
    251  1.14     bjh21 		case 'u':
    252  1.14     bjh21 		case 'x':
    253  1.21       dsl 			if (isupper((unsigned char)*type_string)) {
    254  1.14     bjh21 				switch(*type_string) {
    255  1.14     bjh21 				case 'C':
    256  1.15     bjh21 					nbytes = sizeof(char);
    257  1.14     bjh21 					break;
    258  1.14     bjh21 				case 'S':
    259  1.15     bjh21 					nbytes = sizeof(short);
    260  1.14     bjh21 					break;
    261  1.14     bjh21 				case 'I':
    262  1.15     bjh21 					nbytes = sizeof(int);
    263  1.14     bjh21 					break;
    264  1.14     bjh21 				case 'L':
    265  1.15     bjh21 					nbytes = sizeof(long);
    266  1.14     bjh21 					break;
    267  1.14     bjh21 				default:
    268  1.14     bjh21 					warnx("Bad type-size qualifier '%c'",
    269  1.14     bjh21 					    *type_string);
    270  1.14     bjh21 					usage();
    271  1.14     bjh21 				}
    272  1.14     bjh21 				type_string++;
    273  1.21       dsl 			} else if (isdigit((unsigned char)*type_string)) {
    274  1.16     bjh21 				nbytes = strtol(type_string, &tmp, 10);
    275  1.16     bjh21 				type_string = tmp;
    276  1.16     bjh21 			} else
    277  1.16     bjh21 				nbytes = 4;
    278  1.14     bjh21 			break;
    279  1.14     bjh21 		default:
    280  1.14     bjh21 			usage();
    281  1.14     bjh21 		}
    282  1.16     bjh21 		for (odf = odftab; odf->type != 0; odf++)
    283  1.16     bjh21 			if (odf->type == type && odf->nbytes == nbytes)
    284  1.16     bjh21 				break;
    285  1.16     bjh21 		if (odf->type == 0)
    286  1.16     bjh21 			errx(1, "%c%d: format not supported", type, nbytes);
    287  1.16     bjh21 		asprintf(&fmt, "%d/%d  \"%*s%s \" \"\\n\"",
    288  1.16     bjh21 		    16 / nbytes, nbytes,
    289  1.16     bjh21 		    4 * nbytes - odf->minwidth, "", odf->format);
    290  1.16     bjh21 		if (fmt == NULL) nomem();
    291  1.16     bjh21 		add(fmt);
    292  1.14     bjh21 	}
    293   1.1       cgd }
    294   1.1       cgd 
    295   1.7       mrg static void
    296  1.23     perry odoffset(int argc, char ***argvp)
    297   1.1       cgd {
    298   1.8     lukem 	char *num, *p;
    299   1.1       cgd 	int base;
    300   1.1       cgd 	char *end;
    301   1.1       cgd 
    302   1.1       cgd 	/*
    303   1.1       cgd 	 * The offset syntax of od(1) was genuinely bizarre.  First, if
    304   1.1       cgd 	 * it started with a plus it had to be an offset.  Otherwise, if
    305   1.1       cgd 	 * there were at least two arguments, a number or lower-case 'x'
    306   1.1       cgd 	 * followed by a number makes it an offset.  By default it was
    307   1.1       cgd 	 * octal; if it started with 'x' or '0x' it was hex.  If it ended
    308   1.1       cgd 	 * in a '.', it was decimal.  If a 'b' or 'B' was appended, it
    309   1.1       cgd 	 * multiplied the number by 512 or 1024 byte units.  There was
    310   1.1       cgd 	 * no way to assign a block count to a hex offset.
    311   1.1       cgd 	 *
    312   1.6     mikel 	 * We assume it's a file if the offset is bad.
    313   1.1       cgd 	 */
    314   1.7       mrg 	p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
    315   1.3   mycroft 	if (!p)
    316   1.3   mycroft 		return;
    317   1.7       mrg 
    318   1.1       cgd 	if (*p != '+' && (argc < 2 ||
    319  1.10  christos 	    (!isdigit((unsigned char)p[0]) &&
    320  1.10  christos 	    (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
    321   1.1       cgd 		return;
    322   1.1       cgd 
    323   1.1       cgd 	base = 0;
    324   1.1       cgd 	/*
    325   1.1       cgd 	 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
    326   1.1       cgd 	 * set base.
    327   1.1       cgd 	 */
    328   1.1       cgd 	if (p[0] == '+')
    329   1.1       cgd 		++p;
    330  1.10  christos 	if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
    331   1.1       cgd 		++p;
    332   1.1       cgd 		base = 16;
    333   1.1       cgd 	} else if (p[0] == '0' && p[1] == 'x') {
    334   1.1       cgd 		p += 2;
    335   1.1       cgd 		base = 16;
    336   1.1       cgd 	}
    337   1.1       cgd 
    338   1.1       cgd 	/* skip over the number */
    339   1.1       cgd 	if (base == 16)
    340  1.10  christos 		for (num = p; isxdigit((unsigned char)*p); ++p);
    341   1.1       cgd 	else
    342  1.10  christos 		for (num = p; isdigit((unsigned char)*p); ++p);
    343   1.1       cgd 
    344   1.1       cgd 	/* check for no number */
    345   1.1       cgd 	if (num == p)
    346   1.1       cgd 		return;
    347   1.1       cgd 
    348   1.1       cgd 	/* if terminates with a '.', base is decimal */
    349   1.1       cgd 	if (*p == '.') {
    350   1.1       cgd 		if (base)
    351   1.1       cgd 			return;
    352   1.1       cgd 		base = 10;
    353   1.1       cgd 	}
    354   1.1       cgd 
    355   1.1       cgd 	skip = strtol(num, &end, base ? base : 8);
    356   1.1       cgd 
    357   1.1       cgd 	/* if end isn't the same as p, we got a non-octal digit */
    358   1.7       mrg 	if (end != p) {
    359   1.1       cgd 		skip = 0;
    360   1.7       mrg 		return;
    361   1.7       mrg 	}
    362   1.7       mrg 
    363   1.9      ross 	if (*p) {
    364   1.7       mrg 		if (*p == 'B') {
    365   1.7       mrg 			skip *= 1024;
    366   1.7       mrg 			++p;
    367   1.7       mrg 		} else if (*p == 'b') {
    368   1.7       mrg 			skip *= 512;
    369   1.1       cgd 			++p;
    370   1.1       cgd 		}
    371   1.9      ross 	}
    372   1.7       mrg 	if (*p) {
    373   1.7       mrg 		skip = 0;
    374   1.7       mrg 		return;
    375   1.7       mrg 	}
    376   1.7       mrg 	/*
    377   1.7       mrg 	 * If the offset uses a non-octal base, the base of the offset
    378   1.7       mrg 	 * is changed as well.  This isn't pretty, but it's easy.
    379   1.7       mrg 	 */
    380   1.1       cgd #define	TYPE_OFFSET	7
    381   1.7       mrg 	if (base == 16) {
    382   1.7       mrg 		fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
    383   1.7       mrg 		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
    384   1.7       mrg 	} else if (base == 10) {
    385   1.7       mrg 		fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
    386   1.7       mrg 		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
    387   1.1       cgd 	}
    388   1.7       mrg 
    389   1.7       mrg 	/* Terminate file list. */
    390   1.7       mrg 	(*argvp)[1] = NULL;
    391   1.1       cgd }
    392   1.1       cgd 
    393   1.1       cgd static void
    394  1.23     perry odprecede(void)
    395   1.1       cgd {
    396   1.1       cgd 	static int first = 1;
    397   1.1       cgd 
    398   1.1       cgd 	if (first) {
    399   1.1       cgd 		first = 0;
    400   1.1       cgd 		add("\"%07.7_Ao\n\"");
    401   1.1       cgd 		add("\"%07.7_ao  \"");
    402   1.1       cgd 	} else
    403   1.1       cgd 		add("\"         \"");
    404   1.1       cgd }
    405