xref: /original-bsd/usr.bin/hexdump/conv.c (revision e59fb703)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)conv.c	5.4 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <ctype.h>
14 #include "hexdump.h"
15 
16 conv_c(pr, p)
17 	PR *pr;
18 	u_char *p;
19 {
20 	extern int deprecated;
21 	char buf[10], *str;
22 
23 	switch(*p) {
24 	case '\0':
25 		str = "\\0";
26 		goto strpr;
27 	/* case '\a': */
28 	case '\007':
29 		if (deprecated)		/* od didn't know about \a */
30 			break;
31 		str = "\\a";
32 		goto strpr;
33 	case '\b':
34 		str = "\\b";
35 		goto strpr;
36 	case '\f':
37 		str = "\\f";
38 		goto strpr;
39 	case '\n':
40 		str = "\\n";
41 		goto strpr;
42 	case '\r':
43 		str = "\\r";
44 		goto strpr;
45 	case '\t':
46 		str = "\\t";
47 		goto strpr;
48 	case '\v':
49 		if (deprecated)
50 			break;
51 		str = "\\v";
52 		goto strpr;
53 	default:
54 		break;
55 	}
56 	if (isprint(*p)) {
57 		*pr->cchar = 'c';
58 		(void)printf(pr->fmt, *p);
59 	} else {
60 		(void)sprintf(str = buf, "%03o", (int)*p);
61 strpr:		*pr->cchar = 's';
62 		(void)printf(pr->fmt, str);
63 	}
64 }
65 
66 conv_u(pr, p)
67 	PR *pr;
68 	u_char *p;
69 {
70 	extern int deprecated;
71 	static char *list[] = {
72 		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
73 		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
74 		"dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
75 		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
76 	};
77 
78 						/* od used nl, not lf */
79 	if (*p <= 0x1f) {
80 		*pr->cchar = 's';
81 		if (deprecated && *p == 0x0a)
82 			(void)printf(pr->fmt, "nl");
83 		else
84 			(void)printf(pr->fmt, list[*p]);
85 	} else if (*p == 0x7f) {
86 		*pr->cchar = 's';
87 		(void)printf(pr->fmt, "del");
88 	} else if (deprecated && *p == 0x20) {	/* od replace space with sp */
89 		*pr->cchar = 's';
90 		(void)printf(pr->fmt, " sp");
91 	} else if (isprint(*p)) {
92 		*pr->cchar = 'c';
93 		(void)printf(pr->fmt, *p);
94 	} else {
95 		*pr->cchar = 'x';
96 		(void)printf(pr->fmt, (int)*p);
97 	}
98 }
99