xref: /original-bsd/usr.bin/hexdump/conv.c (revision 04218a6a)
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.3 (Berkeley) 05/07/90";
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 	extern int deprecated;
31 	char buf[10], *str;
32 
33 	switch(*p) {
34 	case '\0':
35 		str = "\\0";
36 		goto strpr;
37 	/* case '\a': */
38 	case '\007':
39 		if (deprecated)		/* od didn't know about \a */
40 			break;
41 		str = "\\a";
42 		goto strpr;
43 	case '\b':
44 		str = "\\b";
45 		goto strpr;
46 	case '\f':
47 		str = "\\f";
48 		goto strpr;
49 	case '\n':
50 		str = "\\n";
51 		goto strpr;
52 	case '\r':
53 		str = "\\r";
54 		goto strpr;
55 	case '\t':
56 		str = "\\t";
57 		goto strpr;
58 	case '\v':
59 		if (deprecated)
60 			break;
61 		str = "\\v";
62 		goto strpr;
63 	default:
64 		break;
65 	}
66 	if (isprint(*p)) {
67 		*pr->cchar = 'c';
68 		(void)printf(pr->fmt, *p);
69 	} else {
70 		(void)sprintf(str = buf, "%03o", (int)*p);
71 strpr:		*pr->cchar = 's';
72 		(void)printf(pr->fmt, str);
73 	}
74 }
75 
76 conv_u(pr, p)
77 	PR *pr;
78 	u_char *p;
79 {
80 	extern int deprecated;
81 	static char *list[] = {
82 		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
83 		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
84 		"dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
85 		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
86 	};
87 
88 						/* od used nl, not lf */
89 	if (*p <= 0x1f) {
90 		*pr->cchar = 's';
91 		if (deprecated && *p == 0x0a)
92 			(void)printf(pr->fmt, "nl");
93 		else
94 			(void)printf(pr->fmt, list[*p]);
95 	} else if (*p == 0x7f) {
96 		*pr->cchar = 's';
97 		(void)printf(pr->fmt, "del");
98 	} else if (deprecated && *p == 0x20) {	/* od replace space with sp */
99 		*pr->cchar = 's';
100 		(void)printf(pr->fmt, " sp");
101 	} else if (isprint(*p)) {
102 		*pr->cchar = 'c';
103 		(void)printf(pr->fmt, *p);
104 	} else {
105 		*pr->cchar = 'x';
106 		(void)printf(pr->fmt, (int)*p);
107 	}
108 }
109