xref: /minix3/usr.bin/hexdump/conv.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: conv.c,v 1.14 2013/02/09 02:49:36 jakllsch Exp $	*/
21e33498fSThomas Cort 
31e33498fSThomas Cort /*
41e33498fSThomas Cort  * Copyright (c) 1989, 1993
51e33498fSThomas Cort  *	The Regents of the University of California.  All rights reserved.
61e33498fSThomas Cort  *
71e33498fSThomas Cort  * Redistribution and use in source and binary forms, with or without
81e33498fSThomas Cort  * modification, are permitted provided that the following conditions
91e33498fSThomas Cort  * are met:
101e33498fSThomas Cort  * 1. Redistributions of source code must retain the above copyright
111e33498fSThomas Cort  *    notice, this list of conditions and the following disclaimer.
121e33498fSThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
131e33498fSThomas Cort  *    notice, this list of conditions and the following disclaimer in the
141e33498fSThomas Cort  *    documentation and/or other materials provided with the distribution.
151e33498fSThomas Cort  * 3. Neither the name of the University nor the names of its contributors
161e33498fSThomas Cort  *    may be used to endorse or promote products derived from this software
171e33498fSThomas Cort  *    without specific prior written permission.
181e33498fSThomas Cort  *
191e33498fSThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201e33498fSThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211e33498fSThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221e33498fSThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231e33498fSThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241e33498fSThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251e33498fSThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261e33498fSThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271e33498fSThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281e33498fSThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291e33498fSThomas Cort  * SUCH DAMAGE.
301e33498fSThomas Cort  */
311e33498fSThomas Cort 
321e33498fSThomas Cort #if HAVE_NBTOOL_CONFIG_H
331e33498fSThomas Cort #include "nbtool_config.h"
341e33498fSThomas Cort #endif
351e33498fSThomas Cort 
361e33498fSThomas Cort #include <sys/cdefs.h>
371e33498fSThomas Cort #if !defined(lint)
381e33498fSThomas Cort #if 0
391e33498fSThomas Cort static char sccsid[] = "@(#)conv.c	8.1 (Berkeley) 6/6/93";
401e33498fSThomas Cort #else
41*84d9c625SLionel Sambuc __RCSID("$NetBSD: conv.c,v 1.14 2013/02/09 02:49:36 jakllsch Exp $");
421e33498fSThomas Cort #endif
431e33498fSThomas Cort #endif /* not lint */
441e33498fSThomas Cort 
451e33498fSThomas Cort #include <sys/types.h>
461e33498fSThomas Cort 
471e33498fSThomas Cort #include <stdio.h>
481e33498fSThomas Cort #include <ctype.h>
491e33498fSThomas Cort 
501e33498fSThomas Cort #include "hexdump.h"
511e33498fSThomas Cort 
521e33498fSThomas Cort void
conv_c(PR * pr,u_char * p)531e33498fSThomas Cort conv_c(PR *pr, u_char *p)
541e33498fSThomas Cort {
551e33498fSThomas Cort 	char buf[10];
561e33498fSThomas Cort 	char const *str;
571e33498fSThomas Cort 
581e33498fSThomas Cort 	switch(*p) {
591e33498fSThomas Cort 	case '\0':
601e33498fSThomas Cort 		str = "\\0";
611e33498fSThomas Cort 		goto strpr;
621e33498fSThomas Cort 	/* case '\a': */
631e33498fSThomas Cort 	case '\007':
641e33498fSThomas Cort 		if (odmode)		/* od doesn't know about \a */
651e33498fSThomas Cort 			break;
661e33498fSThomas Cort 		str = "\\a";
671e33498fSThomas Cort 		goto strpr;
681e33498fSThomas Cort 	case '\b':
691e33498fSThomas Cort 		str = "\\b";
701e33498fSThomas Cort 		goto strpr;
711e33498fSThomas Cort 	case '\f':
721e33498fSThomas Cort 		str = "\\f";
731e33498fSThomas Cort 		goto strpr;
741e33498fSThomas Cort 	case '\n':
751e33498fSThomas Cort 		str = "\\n";
761e33498fSThomas Cort 		goto strpr;
771e33498fSThomas Cort 	case '\r':
781e33498fSThomas Cort 		str = "\\r";
791e33498fSThomas Cort 		goto strpr;
801e33498fSThomas Cort 	case '\t':
811e33498fSThomas Cort 		str = "\\t";
821e33498fSThomas Cort 		goto strpr;
831e33498fSThomas Cort 	case '\v':
841e33498fSThomas Cort 		if (odmode)
851e33498fSThomas Cort 			break;
861e33498fSThomas Cort 		str = "\\v";
871e33498fSThomas Cort 		goto strpr;
881e33498fSThomas Cort 	default:
891e33498fSThomas Cort 		break;
901e33498fSThomas Cort 	}
911e33498fSThomas Cort 	if (isprint(*p)) {
921e33498fSThomas Cort 		*pr->cchar = 'c';
931e33498fSThomas Cort 		(void)printf(pr->fmt, *p);
941e33498fSThomas Cort 	} else {
951e33498fSThomas Cort 		(void)sprintf(buf, "%03o", (int)*p);
961e33498fSThomas Cort 		str = buf;
971e33498fSThomas Cort strpr:		*pr->cchar = 's';
981e33498fSThomas Cort 		(void)printf(pr->fmt, str);
991e33498fSThomas Cort 	}
1001e33498fSThomas Cort }
1011e33498fSThomas Cort 
1021e33498fSThomas Cort void
conv_u(PR * pr,u_char * p)1031e33498fSThomas Cort conv_u(PR *pr, u_char *p)
1041e33498fSThomas Cort {
1051e33498fSThomas Cort 	static const char *list[] = {
1061e33498fSThomas Cort 		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
1071e33498fSThomas Cort 		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
108*84d9c625SLionel Sambuc 		"dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
1091e33498fSThomas Cort 		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
1101e33498fSThomas Cort 	};
1111e33498fSThomas Cort 
1121e33498fSThomas Cort 						/* od used nl, not lf */
1131e33498fSThomas Cort 	if (*p <= 0x1f) {
1141e33498fSThomas Cort 		*pr->cchar = 's';
1151e33498fSThomas Cort 		if (odmode && *p == 0x0a)
1161e33498fSThomas Cort 			(void)printf(pr->fmt, "nl");
1171e33498fSThomas Cort 		else
1181e33498fSThomas Cort 			(void)printf(pr->fmt, list[*p]);
1191e33498fSThomas Cort 	} else if (*p == 0x7f) {
1201e33498fSThomas Cort 		*pr->cchar = 's';
1211e33498fSThomas Cort 		(void)printf(pr->fmt, "del");
1221e33498fSThomas Cort 	} else if (odmode && *p == 0x20) {	/* od replaces space with sp */
1231e33498fSThomas Cort 		*pr->cchar = 's';
1241e33498fSThomas Cort 		(void)printf(pr->fmt, " sp");
1251e33498fSThomas Cort 	} else if (isprint(*p)) {
1261e33498fSThomas Cort 		*pr->cchar = 'c';
1271e33498fSThomas Cort 		(void)printf(pr->fmt, *p);
1281e33498fSThomas Cort 	} else {
1291e33498fSThomas Cort 		*pr->cchar = 'x';
1301e33498fSThomas Cort 		(void)printf(pr->fmt, (int)*p);
1311e33498fSThomas Cort 	}
1321e33498fSThomas Cort }
133