Home | History | Annotate | Line # | Download | only in hexdump
odsyntax.c revision 1.10
      1  1.10  christos /*	$NetBSD: odsyntax.c,v 1.10 1998/12/19 16:43:39 christos 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.10  christos __RCSID("$NetBSD: odsyntax.c,v 1.10 1998/12/19 16:43:39 christos 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.6     mikel void
     61   1.1       cgd oldsyntax(argc, argvp)
     62   1.1       cgd 	int argc;
     63   1.1       cgd 	char ***argvp;
     64   1.1       cgd {
     65   1.1       cgd 	int ch;
     66   1.1       cgd 	char **argv;
     67   1.1       cgd 
     68   1.1       cgd 	deprecated = 1;
     69   1.1       cgd 	argv = *argvp;
     70   1.6     mikel 	while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != -1)
     71   1.1       cgd 		switch (ch) {
     72   1.1       cgd 		case 'a':
     73   1.1       cgd 			odprecede();
     74   1.1       cgd 			add("16/1 \"%3_u \" \"\\n\"");
     75   1.1       cgd 			break;
     76   1.1       cgd 		case 'B':
     77   1.1       cgd 		case 'o':
     78   1.1       cgd 			odprecede();
     79   1.1       cgd 			add("8/2 \" %06o \" \"\\n\"");
     80   1.1       cgd 			break;
     81   1.1       cgd 		case 'b':
     82   1.1       cgd 			odprecede();
     83   1.1       cgd 			add("16/1 \"%03o \" \"\\n\"");
     84   1.1       cgd 			break;
     85   1.1       cgd 		case 'c':
     86   1.1       cgd 			odprecede();
     87   1.1       cgd 			add("16/1 \"%3_c \" \"\\n\"");
     88   1.1       cgd 			break;
     89   1.1       cgd 		case 'd':
     90   1.1       cgd 			odprecede();
     91   1.1       cgd 			add("8/2 \"  %05u \" \"\\n\"");
     92   1.1       cgd 			break;
     93   1.1       cgd 		case 'D':
     94   1.1       cgd 			odprecede();
     95   1.1       cgd 			add("4/4 \"     %010u \" \"\\n\"");
     96   1.1       cgd 			break;
     97   1.1       cgd 		case 'e':		/* undocumented in od */
     98   1.1       cgd 		case 'F':
     99   1.1       cgd 			odprecede();
    100   1.1       cgd 			add("2/8 \"          %21.14e \" \"\\n\"");
    101   1.1       cgd 			break;
    102   1.1       cgd 
    103   1.1       cgd 		case 'f':
    104   1.1       cgd 			odprecede();
    105   1.1       cgd 			add("4/4 \" %14.7e \" \"\\n\"");
    106   1.1       cgd 			break;
    107   1.1       cgd 		case 'H':
    108   1.1       cgd 		case 'X':
    109   1.1       cgd 			odprecede();
    110   1.1       cgd 			add("4/4 \"       %08x \" \"\\n\"");
    111   1.1       cgd 			break;
    112   1.1       cgd 		case 'h':
    113   1.1       cgd 		case 'x':
    114   1.1       cgd 			odprecede();
    115   1.1       cgd 			add("8/2 \"   %04x \" \"\\n\"");
    116   1.1       cgd 			break;
    117   1.1       cgd 		case 'I':
    118   1.1       cgd 		case 'L':
    119   1.1       cgd 		case 'l':
    120   1.1       cgd 			odprecede();
    121   1.1       cgd 			add("4/4 \"    %11d \" \"\\n\"");
    122   1.1       cgd 			break;
    123   1.1       cgd 		case 'i':
    124   1.1       cgd 			odprecede();
    125   1.1       cgd 			add("8/2 \" %6d \" \"\\n\"");
    126   1.1       cgd 			break;
    127   1.1       cgd 		case 'O':
    128   1.1       cgd 			odprecede();
    129   1.1       cgd 			add("4/4 \"    %011o \" \"\\n\"");
    130   1.1       cgd 			break;
    131   1.1       cgd 		case 'v':
    132   1.1       cgd 			vflag = ALL;
    133   1.1       cgd 			break;
    134   1.1       cgd 		case 'P':
    135   1.1       cgd 		case 'p':
    136   1.1       cgd 		case 's':
    137   1.1       cgd 		case 'w':
    138   1.1       cgd 		case '?':
    139   1.1       cgd 		default:
    140   1.8     lukem 			warnx("od(1) has been deprecated for hexdump(1).");
    141   1.1       cgd 			if (ch != '?')
    142   1.8     lukem 				warnx(
    143   1.8     lukem "hexdump(1) compatibility doesn't support the -%c option%s\n",
    144   1.1       cgd 				    ch, ch == 's' ? "; see strings(1)." : ".");
    145   1.1       cgd 			usage();
    146   1.1       cgd 		}
    147   1.1       cgd 
    148   1.1       cgd 	if (!fshead) {
    149   1.1       cgd 		add("\"%07.7_Ao\n\"");
    150   1.1       cgd 		add("\"%07.7_ao  \" 8/2 \"%06o \" \"\\n\"");
    151   1.1       cgd 	}
    152   1.1       cgd 
    153   1.1       cgd 	argc -= optind;
    154   1.1       cgd 	*argvp += optind;
    155   1.1       cgd 
    156   1.7       mrg 	if (argc)
    157   1.7       mrg 		odoffset(argc, argvp);
    158   1.1       cgd }
    159   1.1       cgd 
    160   1.7       mrg static void
    161   1.1       cgd odoffset(argc, argvp)
    162   1.1       cgd 	int argc;
    163   1.1       cgd 	char ***argvp;
    164   1.1       cgd {
    165   1.8     lukem 	char *num, *p;
    166   1.1       cgd 	int base;
    167   1.1       cgd 	char *end;
    168   1.1       cgd 
    169   1.1       cgd 	/*
    170   1.1       cgd 	 * The offset syntax of od(1) was genuinely bizarre.  First, if
    171   1.1       cgd 	 * it started with a plus it had to be an offset.  Otherwise, if
    172   1.1       cgd 	 * there were at least two arguments, a number or lower-case 'x'
    173   1.1       cgd 	 * followed by a number makes it an offset.  By default it was
    174   1.1       cgd 	 * octal; if it started with 'x' or '0x' it was hex.  If it ended
    175   1.1       cgd 	 * in a '.', it was decimal.  If a 'b' or 'B' was appended, it
    176   1.1       cgd 	 * multiplied the number by 512 or 1024 byte units.  There was
    177   1.1       cgd 	 * no way to assign a block count to a hex offset.
    178   1.1       cgd 	 *
    179   1.6     mikel 	 * We assume it's a file if the offset is bad.
    180   1.1       cgd 	 */
    181   1.7       mrg 	p = argc == 1 ? (*argvp)[0] : (*argvp)[1];
    182   1.3   mycroft 	if (!p)
    183   1.3   mycroft 		return;
    184   1.7       mrg 
    185   1.1       cgd 	if (*p != '+' && (argc < 2 ||
    186  1.10  christos 	    (!isdigit((unsigned char)p[0]) &&
    187  1.10  christos 	    (p[0] != 'x' || !isxdigit((unsigned char)p[1])))))
    188   1.1       cgd 		return;
    189   1.1       cgd 
    190   1.1       cgd 	base = 0;
    191   1.1       cgd 	/*
    192   1.1       cgd 	 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
    193   1.1       cgd 	 * set base.
    194   1.1       cgd 	 */
    195   1.1       cgd 	if (p[0] == '+')
    196   1.1       cgd 		++p;
    197  1.10  christos 	if (p[0] == 'x' && isxdigit((unsigned char)p[1])) {
    198   1.1       cgd 		++p;
    199   1.1       cgd 		base = 16;
    200   1.1       cgd 	} else if (p[0] == '0' && p[1] == 'x') {
    201   1.1       cgd 		p += 2;
    202   1.1       cgd 		base = 16;
    203   1.1       cgd 	}
    204   1.1       cgd 
    205   1.1       cgd 	/* skip over the number */
    206   1.1       cgd 	if (base == 16)
    207  1.10  christos 		for (num = p; isxdigit((unsigned char)*p); ++p);
    208   1.1       cgd 	else
    209  1.10  christos 		for (num = p; isdigit((unsigned char)*p); ++p);
    210   1.1       cgd 
    211   1.1       cgd 	/* check for no number */
    212   1.1       cgd 	if (num == p)
    213   1.1       cgd 		return;
    214   1.1       cgd 
    215   1.1       cgd 	/* if terminates with a '.', base is decimal */
    216   1.1       cgd 	if (*p == '.') {
    217   1.1       cgd 		if (base)
    218   1.1       cgd 			return;
    219   1.1       cgd 		base = 10;
    220   1.1       cgd 	}
    221   1.1       cgd 
    222   1.1       cgd 	skip = strtol(num, &end, base ? base : 8);
    223   1.1       cgd 
    224   1.1       cgd 	/* if end isn't the same as p, we got a non-octal digit */
    225   1.7       mrg 	if (end != p) {
    226   1.1       cgd 		skip = 0;
    227   1.7       mrg 		return;
    228   1.7       mrg 	}
    229   1.7       mrg 
    230   1.9      ross 	if (*p) {
    231   1.7       mrg 		if (*p == 'B') {
    232   1.7       mrg 			skip *= 1024;
    233   1.7       mrg 			++p;
    234   1.7       mrg 		} else if (*p == 'b') {
    235   1.7       mrg 			skip *= 512;
    236   1.1       cgd 			++p;
    237   1.1       cgd 		}
    238   1.9      ross 	}
    239   1.7       mrg 	if (*p) {
    240   1.7       mrg 		skip = 0;
    241   1.7       mrg 		return;
    242   1.7       mrg 	}
    243   1.7       mrg 	/*
    244   1.7       mrg 	 * If the offset uses a non-octal base, the base of the offset
    245   1.7       mrg 	 * is changed as well.  This isn't pretty, but it's easy.
    246   1.7       mrg 	 */
    247   1.1       cgd #define	TYPE_OFFSET	7
    248   1.7       mrg 	if (base == 16) {
    249   1.7       mrg 		fshead->nextfu->fmt[TYPE_OFFSET] = 'x';
    250   1.7       mrg 		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x';
    251   1.7       mrg 	} else if (base == 10) {
    252   1.7       mrg 		fshead->nextfu->fmt[TYPE_OFFSET] = 'd';
    253   1.7       mrg 		fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd';
    254   1.1       cgd 	}
    255   1.7       mrg 
    256   1.7       mrg 	/* Terminate file list. */
    257   1.7       mrg 	(*argvp)[1] = NULL;
    258   1.1       cgd }
    259   1.1       cgd 
    260   1.1       cgd static void
    261   1.1       cgd odprecede()
    262   1.1       cgd {
    263   1.1       cgd 	static int first = 1;
    264   1.1       cgd 
    265   1.1       cgd 	if (first) {
    266   1.1       cgd 		first = 0;
    267   1.1       cgd 		add("\"%07.7_Ao\n\"");
    268   1.1       cgd 		add("\"%07.7_ao  \"");
    269   1.1       cgd 	} else
    270   1.1       cgd 		add("\"         \"");
    271   1.1       cgd }
    272