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