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