Home | History | Annotate | Line # | Download | only in hexdump
display.c revision 1.19
      1  1.19     perry /*	$NetBSD: display.c,v 1.19 2006/01/04 01:30:21 perry Exp $	*/
      2   1.4       tls 
      3   1.1       cgd /*
      4   1.6       mrg  * Copyright (c) 1989, 1993
      5   1.6       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.14       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.18     lukem #if HAVE_NBTOOL_CONFIG_H
     33  1.18     lukem #include "nbtool_config.h"
     34  1.18     lukem #endif
     35  1.18     lukem 
     36   1.7     lukem #include <sys/cdefs.h>
     37  1.18     lukem #if !defined(lint)
     38   1.5     mikel #if 0
     39   1.6       mrg static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
     40   1.5     mikel #else
     41  1.19     perry __RCSID("$NetBSD: display.c,v 1.19 2006/01/04 01:30:21 perry Exp $");
     42   1.5     mikel #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/param.h>
     46   1.1       cgd #include <sys/stat.h>
     47   1.6       mrg 
     48   1.7     lukem #include <ctype.h>
     49   1.7     lukem #include <err.h>
     50   1.1       cgd #include <errno.h>
     51  1.16        cl #include <inttypes.h>
     52   1.1       cgd #include <stdio.h>
     53   1.1       cgd #include <stdlib.h>
     54   1.1       cgd #include <string.h>
     55   1.5     mikel #include <unistd.h>
     56   1.6       mrg 
     57   1.1       cgd #include "hexdump.h"
     58   1.1       cgd 
     59   1.1       cgd enum _vflag vflag = FIRST;
     60   1.1       cgd 
     61   1.1       cgd static off_t address;			/* address/offset in stream */
     62   1.1       cgd static off_t eaddress;			/* end address */
     63   1.1       cgd 
     64  1.19     perry static inline void print(PR *, u_char *);
     65   1.1       cgd 
     66   1.5     mikel void
     67  1.19     perry display(void)
     68   1.1       cgd {
     69   1.7     lukem 	FS *fs;
     70   1.7     lukem 	FU *fu;
     71   1.7     lukem 	PR *pr;
     72   1.7     lukem 	int cnt;
     73   1.7     lukem 	u_char *bp;
     74   1.1       cgd 	off_t saveaddress;
     75   1.5     mikel 	u_char savech, *savebp;
     76   1.1       cgd 
     77   1.7     lukem 	savech = 0;
     78   1.5     mikel 	while ((bp = get()) != NULL)
     79   1.1       cgd 	    for (fs = fshead, savebp = bp, saveaddress = address; fs;
     80   1.1       cgd 		fs = fs->nextfs, bp = savebp, address = saveaddress)
     81   1.1       cgd 		    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
     82   1.1       cgd 			if (fu->flags&F_IGNORE)
     83   1.1       cgd 				break;
     84   1.1       cgd 			for (cnt = fu->reps; cnt; --cnt)
     85   1.1       cgd 			    for (pr = fu->nextpr; pr; address += pr->bcnt,
     86   1.1       cgd 				bp += pr->bcnt, pr = pr->nextpr) {
     87   1.1       cgd 				    if (eaddress && address >= eaddress &&
     88   1.6       mrg 					!(pr->flags & (F_TEXT|F_BPAD)))
     89   1.1       cgd 					    bpad(pr);
     90   1.1       cgd 				    if (cnt == 1 && pr->nospace) {
     91   1.1       cgd 					savech = *pr->nospace;
     92   1.1       cgd 					*pr->nospace = '\0';
     93   1.1       cgd 				    }
     94   1.6       mrg 				    print(pr, bp);
     95   1.1       cgd 				    if (cnt == 1 && pr->nospace)
     96   1.1       cgd 					*pr->nospace = savech;
     97   1.1       cgd 			    }
     98   1.1       cgd 		    }
     99   1.1       cgd 	if (endfu) {
    100   1.1       cgd 		/*
    101   1.6       mrg 		 * If eaddress not set, error or file size was multiple of
    102   1.1       cgd 		 * blocksize, and no partial block ever found.
    103   1.1       cgd 		 */
    104   1.1       cgd 		if (!eaddress) {
    105   1.1       cgd 			if (!address)
    106   1.1       cgd 				return;
    107   1.1       cgd 			eaddress = address;
    108   1.1       cgd 		}
    109   1.1       cgd 		for (pr = endfu->nextpr; pr; pr = pr->nextpr)
    110   1.1       cgd 			switch(pr->flags) {
    111   1.1       cgd 			case F_ADDRESS:
    112  1.15       wiz 				(void)printf(pr->fmt, (int64_t)eaddress);
    113   1.1       cgd 				break;
    114   1.1       cgd 			case F_TEXT:
    115   1.9    itojun 				(void)printf("%s", pr->fmt);
    116   1.1       cgd 				break;
    117   1.1       cgd 			}
    118   1.1       cgd 	}
    119   1.1       cgd }
    120   1.1       cgd 
    121   1.6       mrg static inline void
    122  1.19     perry print(PR *pr, u_char *bp)
    123   1.6       mrg {
    124   1.6       mrg 	   double f8;
    125   1.6       mrg 	    float f4;
    126   1.6       mrg 	  int16_t s2;
    127   1.6       mrg 	  int32_t s4;
    128  1.11     bjh21 	  int64_t s8;
    129   1.6       mrg 	u_int16_t u2;
    130   1.6       mrg 	u_int32_t u4;
    131   1.6       mrg 	u_int64_t u8;
    132   1.6       mrg 
    133   1.6       mrg 	switch(pr->flags) {
    134   1.6       mrg 	case F_ADDRESS:
    135  1.15       wiz 		(void)printf(pr->fmt, (int64_t)address);
    136   1.6       mrg 		break;
    137   1.6       mrg 	case F_BPAD:
    138   1.6       mrg 		(void)printf(pr->fmt, "");
    139   1.6       mrg 		break;
    140   1.6       mrg 	case F_C:
    141   1.6       mrg 		conv_c(pr, bp);
    142   1.6       mrg 		break;
    143   1.6       mrg 	case F_CHAR:
    144   1.6       mrg 		(void)printf(pr->fmt, *bp);
    145   1.6       mrg 		break;
    146   1.6       mrg 	case F_DBL:
    147   1.6       mrg 		switch(pr->bcnt) {
    148   1.6       mrg 		case 4:
    149   1.7     lukem 			memmove(&f4, bp, sizeof(f4));
    150   1.6       mrg 			(void)printf(pr->fmt, f4);
    151   1.6       mrg 			break;
    152   1.6       mrg 		case 8:
    153   1.7     lukem 			memmove(&f8, bp, sizeof(f8));
    154   1.6       mrg 			(void)printf(pr->fmt, f8);
    155   1.6       mrg 			break;
    156   1.6       mrg 		}
    157   1.6       mrg 		break;
    158   1.6       mrg 	case F_INT:
    159   1.6       mrg 		switch(pr->bcnt) {
    160   1.6       mrg 		case 1:
    161  1.15       wiz 			(void)printf(pr->fmt, (int64_t)*bp);
    162   1.6       mrg 			break;
    163   1.6       mrg 		case 2:
    164   1.7     lukem 			memmove(&s2, bp, sizeof(s2));
    165  1.15       wiz 			(void)printf(pr->fmt, (int64_t)s2);
    166   1.6       mrg 			break;
    167   1.6       mrg 		case 4:
    168   1.7     lukem 			memmove(&s4, bp, sizeof(s4));
    169  1.15       wiz 			(void)printf(pr->fmt, (int64_t)s4);
    170   1.6       mrg 			break;
    171   1.6       mrg 		case 8:
    172   1.7     lukem 			memmove(&s8, bp, sizeof(s8));
    173   1.6       mrg 			(void)printf(pr->fmt, s8);
    174   1.6       mrg 			break;
    175   1.6       mrg 		}
    176   1.6       mrg 		break;
    177   1.6       mrg 	case F_P:
    178   1.6       mrg 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
    179   1.6       mrg 		break;
    180   1.6       mrg 	case F_STR:
    181   1.6       mrg 		(void)printf(pr->fmt, (char *)bp);
    182   1.6       mrg 		break;
    183   1.6       mrg 	case F_TEXT:
    184   1.9    itojun 		(void)printf("%s", pr->fmt);
    185   1.6       mrg 		break;
    186   1.6       mrg 	case F_U:
    187   1.6       mrg 		conv_u(pr, bp);
    188   1.6       mrg 		break;
    189   1.6       mrg 	case F_UINT:
    190   1.6       mrg 		switch(pr->bcnt) {
    191   1.6       mrg 		case 1:
    192  1.15       wiz 			(void)printf(pr->fmt, (uint64_t)*bp);
    193   1.6       mrg 			break;
    194   1.6       mrg 		case 2:
    195   1.7     lukem 			memmove(&u2, bp, sizeof(u2));
    196  1.15       wiz 			(void)printf(pr->fmt, (uint64_t)u2);
    197   1.6       mrg 			break;
    198   1.6       mrg 		case 4:
    199   1.7     lukem 			memmove(&u4, bp, sizeof(u4));
    200  1.15       wiz 			(void)printf(pr->fmt, (uint64_t)u4);
    201   1.6       mrg 			break;
    202   1.6       mrg 		case 8:
    203   1.7     lukem 			memmove(&u8, bp, sizeof(u8));
    204   1.6       mrg 			(void)printf(pr->fmt, u8);
    205   1.6       mrg 			break;
    206   1.6       mrg 		}
    207   1.6       mrg 		break;
    208   1.6       mrg 	}
    209   1.6       mrg }
    210   1.6       mrg 
    211   1.5     mikel void
    212  1.19     perry bpad(PR *pr)
    213   1.1       cgd {
    214   1.5     mikel 	static const char *spec = " -0+#";
    215   1.7     lukem 	char *p1, *p2;
    216   1.1       cgd 
    217   1.1       cgd 	/*
    218   1.6       mrg 	 * Remove all conversion flags; '-' is the only one valid
    219   1.1       cgd 	 * with %s, and it's not useful here.
    220   1.1       cgd 	 */
    221   1.1       cgd 	pr->flags = F_BPAD;
    222   1.6       mrg 	pr->cchar[0] = 's';
    223   1.6       mrg 	pr->cchar[1] = '\0';
    224   1.1       cgd 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
    225   1.5     mikel 	for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
    226   1.5     mikel 	while ((*p2++ = *p1++) != '\0');
    227   1.1       cgd }
    228   1.1       cgd 
    229   1.1       cgd static char **_argv;
    230   1.1       cgd 
    231   1.1       cgd u_char *
    232  1.19     perry get(void)
    233   1.1       cgd {
    234   1.1       cgd 	static int ateof = 1;
    235   1.1       cgd 	static u_char *curp, *savp;
    236   1.7     lukem 	int n;
    237   1.1       cgd 	int need, nread;
    238   1.1       cgd 	u_char *tmpp;
    239   1.1       cgd 
    240   1.1       cgd 	if (!curp) {
    241   1.6       mrg 		curp = emalloc(blocksize);
    242   1.6       mrg 		savp = emalloc(blocksize);
    243   1.1       cgd 	} else {
    244   1.1       cgd 		tmpp = curp;
    245   1.1       cgd 		curp = savp;
    246   1.1       cgd 		savp = tmpp;
    247   1.6       mrg 		address += blocksize;
    248   1.1       cgd 	}
    249   1.1       cgd 	for (need = blocksize, nread = 0;;) {
    250   1.1       cgd 		/*
    251   1.1       cgd 		 * if read the right number of bytes, or at EOF for one file,
    252   1.1       cgd 		 * and no other files are available, zero-pad the rest of the
    253   1.1       cgd 		 * block and set the end flag.
    254   1.1       cgd 		 */
    255   1.5     mikel 		if (!length || (ateof && !next(NULL))) {
    256   1.1       cgd 			if (need == blocksize)
    257   1.5     mikel 				return(NULL);
    258  1.10  christos 			if (!need && vflag != ALL &&
    259  1.10  christos 			    !memcmp(curp, savp, nread)) {
    260   1.1       cgd 				if (vflag != DUP)
    261   1.1       cgd 					(void)printf("*\n");
    262   1.5     mikel 				return(NULL);
    263   1.1       cgd 			}
    264   1.7     lukem 			memset((char *)curp + nread, 0, need);
    265   1.1       cgd 			eaddress = address + nread;
    266   1.1       cgd 			return(curp);
    267   1.1       cgd 		}
    268   1.1       cgd 		n = fread((char *)curp + nread, sizeof(u_char),
    269   1.1       cgd 		    length == -1 ? need : MIN(length, need), stdin);
    270   1.1       cgd 		if (!n) {
    271   1.1       cgd 			if (ferror(stdin))
    272   1.7     lukem 				warn("%s", _argv[-1]);
    273   1.1       cgd 			ateof = 1;
    274   1.1       cgd 			continue;
    275   1.1       cgd 		}
    276   1.1       cgd 		ateof = 0;
    277   1.1       cgd 		if (length != -1)
    278   1.1       cgd 			length -= n;
    279   1.1       cgd 		if (!(need -= n)) {
    280   1.1       cgd 			if (vflag == ALL || vflag == FIRST ||
    281   1.7     lukem 			    memcmp(curp, savp, blocksize)) {
    282   1.1       cgd 				if (vflag == DUP || vflag == FIRST)
    283   1.1       cgd 					vflag = WAIT;
    284   1.1       cgd 				return(curp);
    285   1.1       cgd 			}
    286   1.1       cgd 			if (vflag == WAIT)
    287   1.1       cgd 				(void)printf("*\n");
    288   1.1       cgd 			vflag = DUP;
    289   1.6       mrg 			address += blocksize;
    290   1.1       cgd 			need = blocksize;
    291   1.1       cgd 			nread = 0;
    292   1.1       cgd 		}
    293   1.1       cgd 		else
    294   1.1       cgd 			nread += n;
    295   1.1       cgd 	}
    296   1.1       cgd }
    297   1.1       cgd 
    298   1.5     mikel int
    299  1.19     perry next(char **argv)
    300   1.1       cgd {
    301   1.1       cgd 	static int done;
    302   1.1       cgd 	int statok;
    303   1.1       cgd 
    304   1.1       cgd 	if (argv) {
    305   1.1       cgd 		_argv = argv;
    306   1.1       cgd 		return(1);
    307   1.1       cgd 	}
    308   1.1       cgd 	for (;;) {
    309   1.1       cgd 		if (*_argv) {
    310   1.1       cgd 			if (!(freopen(*_argv, "r", stdin))) {
    311   1.7     lukem 				warn("%s", *_argv);
    312   1.1       cgd 				exitval = 1;
    313   1.1       cgd 				++_argv;
    314   1.1       cgd 				continue;
    315   1.1       cgd 			}
    316   1.1       cgd 			statok = done = 1;
    317   1.1       cgd 		} else {
    318   1.1       cgd 			if (done++)
    319   1.1       cgd 				return(0);
    320   1.1       cgd 			statok = 0;
    321   1.1       cgd 		}
    322   1.1       cgd 		if (skip)
    323   1.1       cgd 			doskip(statok ? *_argv : "stdin", statok);
    324   1.1       cgd 		if (*_argv)
    325   1.1       cgd 			++_argv;
    326   1.1       cgd 		if (!skip)
    327   1.1       cgd 			return(1);
    328   1.1       cgd 	}
    329   1.1       cgd 	/* NOTREACHED */
    330   1.1       cgd }
    331   1.1       cgd 
    332   1.5     mikel void
    333  1.19     perry doskip(const char *fname, int statok)
    334   1.1       cgd {
    335   1.7     lukem 	int cnt;
    336   1.6       mrg 	struct stat sb;
    337   1.1       cgd 
    338   1.1       cgd 	if (statok) {
    339   1.6       mrg 		if (fstat(fileno(stdin), &sb))
    340   1.7     lukem 			err(1, "fstat %s", fname);
    341   1.6       mrg 		if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
    342   1.6       mrg 			address += sb.st_size;
    343   1.6       mrg 			skip -= sb.st_size;
    344   1.1       cgd 			return;
    345   1.1       cgd 		}
    346   1.1       cgd 	}
    347   1.6       mrg 	if (S_ISREG(sb.st_mode)) {
    348   1.6       mrg 		if (fseek(stdin, skip, SEEK_SET))
    349   1.7     lukem 			err(1, "fseek %s", fname);
    350   1.6       mrg 		address += skip;
    351   1.6       mrg 		skip = 0;
    352   1.6       mrg 	} else {
    353   1.6       mrg 		for (cnt = 0; cnt < skip; ++cnt)
    354   1.6       mrg 			if (getchar() == EOF)
    355   1.6       mrg 				break;
    356   1.6       mrg 		address += cnt;
    357   1.6       mrg 		skip -= cnt;
    358   1.1       cgd 	}
    359   1.1       cgd }
    360   1.1       cgd 
    361   1.6       mrg void *
    362  1.19     perry emalloc(int allocsize)
    363   1.1       cgd {
    364   1.6       mrg 	void *p;
    365   1.1       cgd 
    366  1.12     bjh21 	if ((p = malloc((u_int)allocsize)) == NULL)
    367   1.1       cgd 		nomem();
    368  1.12     bjh21 	memset(p, 0, allocsize);
    369   1.1       cgd 	return(p);
    370   1.1       cgd }
    371   1.1       cgd 
    372   1.5     mikel void
    373  1.19     perry nomem(void)
    374   1.1       cgd {
    375   1.8  drochner 	err(1, NULL);
    376   1.1       cgd }
    377