Home | History | Annotate | Line # | Download | only in hexdump
display.c revision 1.16
      1 /*	$NetBSD: display.c,v 1.16 2003/09/12 12:59:34 cl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(__RCSID) && !defined(lint)
     34 #if 0
     35 static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
     36 #else
     37 __RCSID("$NetBSD: display.c,v 1.16 2003/09/12 12:59:34 cl Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/stat.h>
     43 
     44 #include <ctype.h>
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <inttypes.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <unistd.h>
     52 
     53 #include "hexdump.h"
     54 
     55 enum _vflag vflag = FIRST;
     56 
     57 static off_t address;			/* address/offset in stream */
     58 static off_t eaddress;			/* end address */
     59 
     60 static inline void print __P((PR *, u_char *));
     61 
     62 void
     63 display()
     64 {
     65 	FS *fs;
     66 	FU *fu;
     67 	PR *pr;
     68 	int cnt;
     69 	u_char *bp;
     70 	off_t saveaddress;
     71 	u_char savech, *savebp;
     72 
     73 	savech = 0;
     74 	while ((bp = get()) != NULL)
     75 	    for (fs = fshead, savebp = bp, saveaddress = address; fs;
     76 		fs = fs->nextfs, bp = savebp, address = saveaddress)
     77 		    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
     78 			if (fu->flags&F_IGNORE)
     79 				break;
     80 			for (cnt = fu->reps; cnt; --cnt)
     81 			    for (pr = fu->nextpr; pr; address += pr->bcnt,
     82 				bp += pr->bcnt, pr = pr->nextpr) {
     83 				    if (eaddress && address >= eaddress &&
     84 					!(pr->flags & (F_TEXT|F_BPAD)))
     85 					    bpad(pr);
     86 				    if (cnt == 1 && pr->nospace) {
     87 					savech = *pr->nospace;
     88 					*pr->nospace = '\0';
     89 				    }
     90 				    print(pr, bp);
     91 				    if (cnt == 1 && pr->nospace)
     92 					*pr->nospace = savech;
     93 			    }
     94 		    }
     95 	if (endfu) {
     96 		/*
     97 		 * If eaddress not set, error or file size was multiple of
     98 		 * blocksize, and no partial block ever found.
     99 		 */
    100 		if (!eaddress) {
    101 			if (!address)
    102 				return;
    103 			eaddress = address;
    104 		}
    105 		for (pr = endfu->nextpr; pr; pr = pr->nextpr)
    106 			switch(pr->flags) {
    107 			case F_ADDRESS:
    108 				(void)printf(pr->fmt, (int64_t)eaddress);
    109 				break;
    110 			case F_TEXT:
    111 				(void)printf("%s", pr->fmt);
    112 				break;
    113 			}
    114 	}
    115 }
    116 
    117 static inline void
    118 print(pr, bp)
    119 	PR *pr;
    120 	u_char *bp;
    121 {
    122 	   double f8;
    123 	    float f4;
    124 	  int16_t s2;
    125 	  int32_t s4;
    126 	  int64_t s8;
    127 	u_int16_t u2;
    128 	u_int32_t u4;
    129 	u_int64_t u8;
    130 
    131 	switch(pr->flags) {
    132 	case F_ADDRESS:
    133 		(void)printf(pr->fmt, (int64_t)address);
    134 		break;
    135 	case F_BPAD:
    136 		(void)printf(pr->fmt, "");
    137 		break;
    138 	case F_C:
    139 		conv_c(pr, bp);
    140 		break;
    141 	case F_CHAR:
    142 		(void)printf(pr->fmt, *bp);
    143 		break;
    144 	case F_DBL:
    145 		switch(pr->bcnt) {
    146 		case 4:
    147 			memmove(&f4, bp, sizeof(f4));
    148 			(void)printf(pr->fmt, f4);
    149 			break;
    150 		case 8:
    151 			memmove(&f8, bp, sizeof(f8));
    152 			(void)printf(pr->fmt, f8);
    153 			break;
    154 		}
    155 		break;
    156 	case F_INT:
    157 		switch(pr->bcnt) {
    158 		case 1:
    159 			(void)printf(pr->fmt, (int64_t)*bp);
    160 			break;
    161 		case 2:
    162 			memmove(&s2, bp, sizeof(s2));
    163 			(void)printf(pr->fmt, (int64_t)s2);
    164 			break;
    165 		case 4:
    166 			memmove(&s4, bp, sizeof(s4));
    167 			(void)printf(pr->fmt, (int64_t)s4);
    168 			break;
    169 		case 8:
    170 			memmove(&s8, bp, sizeof(s8));
    171 			(void)printf(pr->fmt, s8);
    172 			break;
    173 		}
    174 		break;
    175 	case F_P:
    176 		(void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
    177 		break;
    178 	case F_STR:
    179 		(void)printf(pr->fmt, (char *)bp);
    180 		break;
    181 	case F_TEXT:
    182 		(void)printf("%s", pr->fmt);
    183 		break;
    184 	case F_U:
    185 		conv_u(pr, bp);
    186 		break;
    187 	case F_UINT:
    188 		switch(pr->bcnt) {
    189 		case 1:
    190 			(void)printf(pr->fmt, (uint64_t)*bp);
    191 			break;
    192 		case 2:
    193 			memmove(&u2, bp, sizeof(u2));
    194 			(void)printf(pr->fmt, (uint64_t)u2);
    195 			break;
    196 		case 4:
    197 			memmove(&u4, bp, sizeof(u4));
    198 			(void)printf(pr->fmt, (uint64_t)u4);
    199 			break;
    200 		case 8:
    201 			memmove(&u8, bp, sizeof(u8));
    202 			(void)printf(pr->fmt, u8);
    203 			break;
    204 		}
    205 		break;
    206 	}
    207 }
    208 
    209 void
    210 bpad(pr)
    211 	PR *pr;
    212 {
    213 	static const char *spec = " -0+#";
    214 	char *p1, *p2;
    215 
    216 	/*
    217 	 * Remove all conversion flags; '-' is the only one valid
    218 	 * with %s, and it's not useful here.
    219 	 */
    220 	pr->flags = F_BPAD;
    221 	pr->cchar[0] = 's';
    222 	pr->cchar[1] = '\0';
    223 	for (p1 = pr->fmt; *p1 != '%'; ++p1);
    224 	for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
    225 	while ((*p2++ = *p1++) != '\0');
    226 }
    227 
    228 static char **_argv;
    229 
    230 u_char *
    231 get()
    232 {
    233 	static int ateof = 1;
    234 	static u_char *curp, *savp;
    235 	int n;
    236 	int need, nread;
    237 	u_char *tmpp;
    238 
    239 	if (!curp) {
    240 		curp = emalloc(blocksize);
    241 		savp = emalloc(blocksize);
    242 	} else {
    243 		tmpp = curp;
    244 		curp = savp;
    245 		savp = tmpp;
    246 		address += blocksize;
    247 	}
    248 	for (need = blocksize, nread = 0;;) {
    249 		/*
    250 		 * if read the right number of bytes, or at EOF for one file,
    251 		 * and no other files are available, zero-pad the rest of the
    252 		 * block and set the end flag.
    253 		 */
    254 		if (!length || (ateof && !next(NULL))) {
    255 			if (need == blocksize)
    256 				return(NULL);
    257 			if (!need && vflag != ALL &&
    258 			    !memcmp(curp, savp, nread)) {
    259 				if (vflag != DUP)
    260 					(void)printf("*\n");
    261 				return(NULL);
    262 			}
    263 			memset((char *)curp + nread, 0, need);
    264 			eaddress = address + nread;
    265 			return(curp);
    266 		}
    267 		n = fread((char *)curp + nread, sizeof(u_char),
    268 		    length == -1 ? need : MIN(length, need), stdin);
    269 		if (!n) {
    270 			if (ferror(stdin))
    271 				warn("%s", _argv[-1]);
    272 			ateof = 1;
    273 			continue;
    274 		}
    275 		ateof = 0;
    276 		if (length != -1)
    277 			length -= n;
    278 		if (!(need -= n)) {
    279 			if (vflag == ALL || vflag == FIRST ||
    280 			    memcmp(curp, savp, blocksize)) {
    281 				if (vflag == DUP || vflag == FIRST)
    282 					vflag = WAIT;
    283 				return(curp);
    284 			}
    285 			if (vflag == WAIT)
    286 				(void)printf("*\n");
    287 			vflag = DUP;
    288 			address += blocksize;
    289 			need = blocksize;
    290 			nread = 0;
    291 		}
    292 		else
    293 			nread += n;
    294 	}
    295 }
    296 
    297 int
    298 next(argv)
    299 	char **argv;
    300 {
    301 	static int done;
    302 	int statok;
    303 
    304 	if (argv) {
    305 		_argv = argv;
    306 		return(1);
    307 	}
    308 	for (;;) {
    309 		if (*_argv) {
    310 			if (!(freopen(*_argv, "r", stdin))) {
    311 				warn("%s", *_argv);
    312 				exitval = 1;
    313 				++_argv;
    314 				continue;
    315 			}
    316 			statok = done = 1;
    317 		} else {
    318 			if (done++)
    319 				return(0);
    320 			statok = 0;
    321 		}
    322 		if (skip)
    323 			doskip(statok ? *_argv : "stdin", statok);
    324 		if (*_argv)
    325 			++_argv;
    326 		if (!skip)
    327 			return(1);
    328 	}
    329 	/* NOTREACHED */
    330 }
    331 
    332 void
    333 doskip(fname, statok)
    334 	const char *fname;
    335 	int statok;
    336 {
    337 	int cnt;
    338 	struct stat sb;
    339 
    340 	if (statok) {
    341 		if (fstat(fileno(stdin), &sb))
    342 			err(1, "fstat %s", fname);
    343 		if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
    344 			address += sb.st_size;
    345 			skip -= sb.st_size;
    346 			return;
    347 		}
    348 	}
    349 	if (S_ISREG(sb.st_mode)) {
    350 		if (fseek(stdin, skip, SEEK_SET))
    351 			err(1, "fseek %s", fname);
    352 		address += skip;
    353 		skip = 0;
    354 	} else {
    355 		for (cnt = 0; cnt < skip; ++cnt)
    356 			if (getchar() == EOF)
    357 				break;
    358 		address += cnt;
    359 		skip -= cnt;
    360 	}
    361 }
    362 
    363 void *
    364 emalloc(allocsize)
    365 	int allocsize;
    366 {
    367 	void *p;
    368 
    369 	if ((p = malloc((u_int)allocsize)) == NULL)
    370 		nomem();
    371 	memset(p, 0, allocsize);
    372 	return(p);
    373 }
    374 
    375 void
    376 nomem()
    377 {
    378 	err(1, NULL);
    379 }
    380