conv.c revision 1591
11573Srgrimes/*
21573Srgrimes * Copyright (c) 1989, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by the University of
161573Srgrimes *	California, Berkeley and its contributors.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
341573Srgrimes#ifndef lint
351573Srgrimesstatic char sccsid[] = "@(#)conv.c	8.1 (Berkeley) 6/6/93";
361573Srgrimes#endif /* not lint */
371573Srgrimes
381573Srgrimes#include <sys/types.h>
391573Srgrimes
401573Srgrimes#include <stdio.h>
411573Srgrimes#include <ctype.h>
421573Srgrimes#include "hexdump.h"
431573Srgrimes
441573Srgrimesvoid
451573Srgrimesconv_c(pr, p)
461573Srgrimes	PR *pr;
471573Srgrimes	u_char *p;
481573Srgrimes{
491573Srgrimes	extern int deprecated;
501573Srgrimes	char buf[10], *str;
511573Srgrimes
521573Srgrimes	switch(*p) {
531573Srgrimes	case '\0':
541573Srgrimes		str = "\\0";
551573Srgrimes		goto strpr;
561573Srgrimes	/* case '\a': */
571573Srgrimes	case '\007':
581573Srgrimes		if (deprecated)		/* od didn't know about \a */
591573Srgrimes			break;
601573Srgrimes		str = "\\a";
611573Srgrimes		goto strpr;
621573Srgrimes	case '\b':
631573Srgrimes		str = "\\b";
641573Srgrimes		goto strpr;
651573Srgrimes	case '\f':
666264Sjkh		str = "\\f";
671573Srgrimes		goto strpr;
686264Sjkh	case '\n':
691573Srgrimes		str = "\\n";
701573Srgrimes		goto strpr;
711573Srgrimes	case '\r':
721573Srgrimes		str = "\\r";
731573Srgrimes		goto strpr;
741573Srgrimes	case '\t':
751573Srgrimes		str = "\\t";
761573Srgrimes		goto strpr;
771573Srgrimes	case '\v':
781573Srgrimes		if (deprecated)
791573Srgrimes			break;
801573Srgrimes		str = "\\v";
811573Srgrimes		goto strpr;
821573Srgrimes	default:
831573Srgrimes		break;
841573Srgrimes	}
851573Srgrimes	if (isprint(*p)) {
861573Srgrimes		*pr->cchar = 'c';
871573Srgrimes		(void)printf(pr->fmt, *p);
881573Srgrimes	} else {
891573Srgrimes		(void)sprintf(str = buf, "%03o", (int)*p);
901573Srgrimesstrpr:		*pr->cchar = 's';
911573Srgrimes		(void)printf(pr->fmt, str);
921573Srgrimes	}
931573Srgrimes}
941573Srgrimes
951573Srgrimesvoid
961573Srgrimesconv_u(pr, p)
971573Srgrimes	PR *pr;
981573Srgrimes	u_char *p;
991573Srgrimes{
1001573Srgrimes	extern int deprecated;
1011573Srgrimes	static char *list[] = {
1021573Srgrimes		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
1031573Srgrimes		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
1041573Srgrimes		"dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
1051573Srgrimes		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
1061573Srgrimes	};
1071573Srgrimes
1081573Srgrimes						/* od used nl, not lf */
1091573Srgrimes	if (*p <= 0x1f) {
1101573Srgrimes		*pr->cchar = 's';
1111573Srgrimes		if (deprecated && *p == 0x0a)
1121573Srgrimes			(void)printf(pr->fmt, "nl");
1131573Srgrimes		else
1141573Srgrimes			(void)printf(pr->fmt, list[*p]);
1151573Srgrimes	} else if (*p == 0x7f) {
1161573Srgrimes		*pr->cchar = 's';
1171573Srgrimes		(void)printf(pr->fmt, "del");
1181573Srgrimes	} else if (deprecated && *p == 0x20) {	/* od replaced space with sp */
1191573Srgrimes		*pr->cchar = 's';
1201573Srgrimes		(void)printf(pr->fmt, " sp");
1211573Srgrimes	} else if (isprint(*p)) {
1221573Srgrimes		*pr->cchar = 'c';
1231573Srgrimes		(void)printf(pr->fmt, *p);
1241573Srgrimes	} else {
1251573Srgrimes		*pr->cchar = 'x';
1261573Srgrimes		(void)printf(pr->fmt, (int)*p);
1271573Srgrimes	}
128}
129