conv.c revision 102944
113070Sasami/*
213070Sasami * Copyright (c) 1989, 1993
313044Sasami *	The Regents of the University of California.  All rights reserved.
413044Sasami *
513044Sasami * Redistribution and use in source and binary forms, with or without
613044Sasami * modification, are permitted provided that the following conditions
713044Sasami * are met:
813044Sasami * 1. Redistributions of source code must retain the above copyright
913044Sasami *    notice, this list of conditions and the following disclaimer.
1013044Sasami * 2. Redistributions in binary form must reproduce the above copyright
1113044Sasami *    notice, this list of conditions and the following disclaimer in the
1213044Sasami *    documentation and/or other materials provided with the distribution.
1313044Sasami * 3. All advertising materials mentioning features or use of this software
1413044Sasami *    must display the following acknowledgement:
1513044Sasami *	This product includes software developed by the University of
1613044Sasami *	California, Berkeley and its contributors.
1713044Sasami * 4. Neither the name of the University nor the names of its contributors
1813044Sasami *    may be used to endorse or promote products derived from this software
1913044Sasami *    without specific prior written permission.
2013044Sasami *
2113044Sasami * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2213044Sasami * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2313044Sasami * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2413044Sasami * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2513044Sasami * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2613044Sasami * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2713044Sasami * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2813044Sasami * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2913044Sasami * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3013044Sasami * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3113044Sasami * SUCH DAMAGE.
3213044Sasami */
3313044Sasami
3413044Sasami#ifndef lint
3513044Sasamistatic const char sccsid[] = "@(#)conv.c	8.1 (Berkeley) 6/6/93";
3613044Sasami#endif /* not lint */
3713044Sasami#include <sys/cdefs.h>
3813044Sasami__FBSDID("$FreeBSD: head/usr.bin/hexdump/conv.c 102944 2002-09-04 23:29:10Z dwmalone $");
3913044Sasami
4013044Sasami#include <sys/types.h>
4113044Sasami
4213044Sasami#include <stdio.h>
4313044Sasami#include <ctype.h>
4413044Sasami#include "hexdump.h"
4513044Sasami
4613044Sasamivoid
4713044Sasamiconv_c(PR *pr, u_char *p)
4813044Sasami{
4913044Sasami	char buf[10];
5013044Sasami	char const *str;
5113044Sasami
5213044Sasami	switch(*p) {
5313044Sasami	case '\0':
5413044Sasami		str = "\\0";
5513044Sasami		goto strpr;
5613052Sasami	/* case '\a': */
5713044Sasami	case '\007':
5813044Sasami		str = "\\a";
5913044Sasami		goto strpr;
6013044Sasami	case '\b':
6113044Sasami		str = "\\b";
6213044Sasami		goto strpr;
6313044Sasami	case '\f':
6413044Sasami		str = "\\f";
6513044Sasami		goto strpr;
6613044Sasami	case '\n':
6713044Sasami		str = "\\n";
6813044Sasami		goto strpr;
6913044Sasami	case '\r':
7013044Sasami		str = "\\r";
7113044Sasami		goto strpr;
7213044Sasami	case '\t':
7313044Sasami		str = "\\t";
7413044Sasami		goto strpr;
7513044Sasami	case '\v':
7613044Sasami		str = "\\v";
7713044Sasami		goto strpr;
7813044Sasami	default:
7913044Sasami		break;
8013044Sasami	}
8113044Sasami	if (isprint(*p)) {
8213044Sasami		*pr->cchar = 'c';
8313044Sasami		(void)printf(pr->fmt, *p);
8413044Sasami	} else {
8513044Sasami		(void)sprintf(buf, "%03o", (int)*p);
8613044Sasami		str = buf;
8713044Sasamistrpr:		*pr->cchar = 's';
8813044Sasami		(void)printf(pr->fmt, str);
8913044Sasami	}
9013044Sasami}
9113044Sasami
9213044Sasamivoid
9313044Sasamiconv_u(PR *pr, u_char *p)
9413044Sasami{
9513044Sasami	static char const * list[] = {
9613044Sasami		"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
9713044Sasami		 "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si",
9813044Sasami		"dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb",
9913044Sasami		"can",  "em", "sub", "esc",  "fs",  "gs",  "rs",  "us",
10013044Sasami	};
10113044Sasami
10213044Sasami						/* od used nl, not lf */
10313044Sasami	if (*p <= 0x1f) {
10413044Sasami		*pr->cchar = 's';
10513044Sasami		if (odmode && *p == 0x0a)
10613044Sasami			(void)printf(pr->fmt, "nl");
10713044Sasami		else
10813044Sasami			(void)printf(pr->fmt, list[*p]);
10913044Sasami	} else if (*p == 0x7f) {
11013044Sasami		*pr->cchar = 's';
11113044Sasami		(void)printf(pr->fmt, "del");
11213044Sasami	} else if (odmode && *p == 0x20) {	/* od replaced space with sp */
11313044Sasami		*pr->cchar = 's';
11413044Sasami		(void)printf(pr->fmt, " sp");
11513044Sasami	} else if (isprint(*p)) {
11613044Sasami		*pr->cchar = 'c';
11713044Sasami		(void)printf(pr->fmt, *p);
11813044Sasami	} else {
11913044Sasami		*pr->cchar = 'x';
12013044Sasami		(void)printf(pr->fmt, (int)*p);
12113044Sasami	}
12213044Sasami}
12313044Sasami