]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/hexdump/display.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / hexdump / display.c
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 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
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 void print(PR *, u_char *);
59
60 void
61 display(void)
62 {
63         FS *fs;
64         FU *fu;
65         PR *pr;
66         int cnt;
67         u_char *bp;
68         off_t saveaddress;
69         u_char savech, *savebp;
70
71         savech = 0;
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("%s", pr->fmt);
110                                 break;
111                         }
112         }
113 }
114
115 static void
116 print(PR *pr, u_char *bp)
117 {
118         long double ldbl;
119            double f8;
120             float f4;
121           int16_t s2;
122            int8_t s8;
123           int32_t s4;
124         u_int16_t u2;
125         u_int32_t u4;
126         u_int64_t u8;
127
128         switch(pr->flags) {
129         case F_ADDRESS:
130                 (void)printf(pr->fmt, (quad_t)address);
131                 break;
132         case F_BPAD:
133                 (void)printf(pr->fmt, "");
134                 break;
135         case F_C:
136                 conv_c(pr, bp, eaddress ? eaddress - address :
137                     blocksize - address % blocksize);
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                 default:
153                         if (pr->bcnt == sizeof(long double)) {
154                                 bcopy(bp, &ldbl, sizeof(ldbl));
155                                 (void)printf(pr->fmt, ldbl);
156                         }
157                         break;
158                 }
159                 break;
160         case F_INT:
161                 switch(pr->bcnt) {
162                 case 1:
163                         (void)printf(pr->fmt, (quad_t)(signed char)*bp);
164                         break;
165                 case 2:
166                         bcopy(bp, &s2, sizeof(s2));
167                         (void)printf(pr->fmt, (quad_t)s2);
168                         break;
169                 case 4:
170                         bcopy(bp, &s4, sizeof(s4));
171                         (void)printf(pr->fmt, (quad_t)s4);
172                         break;
173                 case 8:
174                         bcopy(bp, &s8, sizeof(s8));
175                         (void)printf(pr->fmt, s8);
176                         break;
177                 }
178                 break;
179         case F_P:
180                 (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
181                 break;
182         case F_STR:
183                 (void)printf(pr->fmt, (char *)bp);
184                 break;
185         case F_TEXT:
186                 (void)printf("%s", pr->fmt);
187                 break;
188         case F_U:
189                 conv_u(pr, bp);
190                 break;
191         case F_UINT:
192                 switch(pr->bcnt) {
193                 case 1:
194                         (void)printf(pr->fmt, (u_quad_t)*bp);
195                         break;
196                 case 2:
197                         bcopy(bp, &u2, sizeof(u2));
198                         (void)printf(pr->fmt, (u_quad_t)u2);
199                         break;
200                 case 4:
201                         bcopy(bp, &u4, sizeof(u4));
202                         (void)printf(pr->fmt, (u_quad_t)u4);
203                         break;
204                 case 8:
205                         bcopy(bp, &u8, sizeof(u8));
206                         (void)printf(pr->fmt, u8);
207                         break;
208                 }
209                 break;
210         }
211 }
212
213 void
214 bpad(PR *pr)
215 {
216         static char const *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 && index(spec, *p1); ++p1);
228         while ((*p2++ = *p1++));
229 }
230
231 static char **_argv;
232
233 u_char *
234 get(void)
235 {
236         static int ateof = 1;
237         static u_char *curp, *savp;
238         int n;
239         int need, nread;
240         int valid_save = 0;
241         u_char *tmpp;
242
243         if (!curp) {
244                 if ((curp = calloc(1, blocksize)) == NULL)
245                         err(1, NULL);
246                 if ((savp = calloc(1, blocksize)) == NULL)
247                         err(1, NULL);
248         } else {
249                 tmpp = curp;
250                 curp = savp;
251                 savp = tmpp;
252                 address += blocksize;
253                 valid_save = 1;
254         }
255         for (need = blocksize, nread = 0;;) {
256                 /*
257                  * if read the right number of bytes, or at EOF for one file,
258                  * and no other files are available, zero-pad the rest of the
259                  * block and set the end flag.
260                  */
261                 if (!length || (ateof && !next((char **)NULL))) {
262                         if (odmode && address < skip)
263                                 errx(1, "cannot skip past end of input");
264                         if (need == blocksize)
265                                 return((u_char *)NULL);
266                         /*
267                          * XXX bcmp() is not quite right in the presence
268                          * of multibyte characters.
269                          */
270                         if (vflag != ALL && 
271                             valid_save && 
272                             bcmp(curp, savp, nread) == 0) {
273                                 if (vflag != DUP)
274                                         (void)printf("*\n");
275                                 return((u_char *)NULL);
276                         }
277                         bzero((char *)curp + nread, need);
278                         eaddress = address + nread;
279                         return(curp);
280                 }
281                 n = fread((char *)curp + nread, sizeof(u_char),
282                     length == -1 ? need : MIN(length, need), stdin);
283                 if (!n) {
284                         if (ferror(stdin))
285                                 warn("%s", _argv[-1]);
286                         ateof = 1;
287                         continue;
288                 }
289                 ateof = 0;
290                 if (length != -1)
291                         length -= n;
292                 if (!(need -= n)) {
293                         /*
294                          * XXX bcmp() is not quite right in the presence
295                          * of multibyte characters.
296                          */
297                         if (vflag == ALL || vflag == FIRST ||
298                             valid_save == 0 ||
299                             bcmp(curp, savp, blocksize) != 0) {
300                                 if (vflag == DUP || vflag == FIRST)
301                                         vflag = WAIT;
302                                 return(curp);
303                         }
304                         if (vflag == WAIT)
305                                 (void)printf("*\n");
306                         vflag = DUP;
307                         address += blocksize;
308                         need = blocksize;
309                         nread = 0;
310                 }
311                 else
312                         nread += n;
313         }
314 }
315
316 size_t
317 peek(u_char *buf, size_t nbytes)
318 {
319         size_t n, nread;
320         int c;
321
322         if (length != -1 && nbytes > (unsigned int)length)
323                 nbytes = length;
324         nread = 0;
325         while (nread < nbytes && (c = getchar()) != EOF) {
326                 *buf++ = c;
327                 nread++;
328         }
329         n = nread;
330         while (n-- > 0) {
331                 c = *--buf;
332                 ungetc(c, stdin);
333         }
334         return (nread);
335 }
336
337 int
338 next(char **argv)
339 {
340         static int done;
341         int statok;
342
343         if (argv) {
344                 _argv = argv;
345                 return(1);
346         }
347         for (;;) {
348                 if (*_argv) {
349                         done = 1;
350                         if (!(freopen(*_argv, "r", stdin))) {
351                                 warn("%s", *_argv);
352                                 exitval = 1;
353                                 ++_argv;
354                                 continue;
355                         }
356                         statok = 1;
357                 } else {
358                         if (done++)
359                                 return(0);
360                         statok = 0;
361                 }
362                 if (skip)
363                         doskip(statok ? *_argv : "stdin", statok);
364                 if (*_argv)
365                         ++_argv;
366                 if (!skip)
367                         return(1);
368         }
369         /* NOTREACHED */
370 }
371
372 void
373 doskip(const char *fname, int statok)
374 {
375         int cnt;
376         struct stat sb;
377
378         if (statok) {
379                 if (fstat(fileno(stdin), &sb))
380                         err(1, "%s", fname);
381                 if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
382                         address += sb.st_size;
383                         skip -= sb.st_size;
384                         return;
385                 }
386         }
387         if (S_ISREG(sb.st_mode)) {
388                 if (fseeko(stdin, skip, SEEK_SET))
389                         err(1, "%s", fname);
390                 address += skip;
391                 skip = 0;
392         } else {
393                 for (cnt = 0; cnt < skip; ++cnt)
394                         if (getchar() == EOF)
395                                 break;
396                 address += cnt;
397                 skip -= cnt;
398         }
399 }