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