aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/ip/snoopy/dump.c
blob: ec2a3ce7cab4fdb33d0fd7ea299306e3c5db4673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <u.h>
#include <libc.h>
#include <ip.h>
#include <ctype.h>
#include "dat.h"
#include "protos.h"

static char tohex[16] = {
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
	'a', 'b', 'c', 'd', 'e', 'f'
};

static int
p_seprint(Msg *m)
{
	int c, i, n, isstring;
	uchar *ps = m->ps;
	char *p = m->p;
	char *e = m->e;

	n = m->pe - ps;
	if(n > Nflag)
		n = Nflag;

	isstring = 1;
	for(i = 0; i < n; i++){
		c = ps[i];
		if(!isprint(c) && !isspace(c)){
			isstring = 0;
			break;
		}
	}

	if(isstring){
		for(i = 0; i < n && p+1<e; i++){
			c = ps[i];
			switch(c){
			case '\t':
				*p++ = '\\';
				*p++ = 't';
				break;
			case '\r':
				*p++ = '\\';
				*p++ = 'r';
				break;
			case '\n':
				*p++ = '\\';
				*p++ = 'n';
				break;
			default:
				*p++ = c;
			}
		}
	} else {
		for(i = 0; i < n && p+1<e; i++){
			c = ps[i];
			*p++ = tohex[c>>4];
			*p++ = tohex[c&0xf]; 
		}
	}

	m->pr = nil;
	m->p = p;
	m->ps = ps;

	return 0;
}

Proto dump =
{
	"dump",
	nil,
	nil,
	p_seprint,
	nil,
	nil,
	nil,
	defaultframer
};