fatbase

portable OpenBSD tools
git clone git://git.2f30.org/fatbase
Log | Files | Refs

display.c (7945B)


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