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