xref: /csrg-svn/usr.bin/hexdump/conv.c (revision 38854)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)conv.c	5.1 (Berkeley) 08/29/89";
20 #endif /* not lint */
21 
22 #include <sys/types.h>
23 #include <ctype.h>
24 #include "hexdump.h"
25 
26 conv_c(pr, p)
27 	PR *pr;
28 	u_char *p;
29 {
30 	char *str;
31 
32 	switch(*p) {
33 	case '\0':
34 		str = "\\0";
35 		goto strpr;
36 	/* case '\a': */
37 	case '\007':
38 		str = "\\a";
39 		goto strpr;
40 	case '\b':
41 		str = "\\b";
42 		goto strpr;
43 	case '\f':
44 		str = "\\f";
45 		goto strpr;
46 	case '\n':
47 		str = "\\n";
48 		goto strpr;
49 	case '\r':
50 		str = "\\r";
51 		goto strpr;
52 	case '\t':
53 		str = "\\t";
54 		goto strpr;
55 	case '\v':
56 		str = "\\v";
57 strpr:		*pr->cchar = 's';
58 		(void)printf(pr->fmt, str);
59 		break;
60 	default:
61 		if (isprint(*p)) {
62 			*pr->cchar = 'c';
63 			(void)printf(pr->fmt, *p);
64 		} else {
65 			*pr->cchar = 'x';
66 			(void)printf(pr->fmt, (int)*p);
67 		}
68 	}
69 }
70 
71 conv_u(pr, p)
72 	PR *pr;
73 	u_char *p;
74 {
75 	static char *list[] = {
76 		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
77 		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
78 		"dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etc",
79 		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
80 	};
81 
82 	if (*p <= 0x1f) {
83 		*pr->cchar = 's';
84 		(void)printf(pr->fmt, list[*p]);
85 	} else if (*p == 0xff) {
86 		*pr->cchar = 's';
87 		(void)printf(pr->fmt, "del");
88 	} else if (isprint(*p)) {
89 		*pr->cchar = 'c';
90 		(void)printf(pr->fmt, *p);
91 	} else {
92 		*pr->cchar = 'x';
93 		(void)printf(pr->fmt, (int)*p);
94 	}
95 }
96