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