Plan 9 from Bell Labs’s /usr/web/sources/plan9/sys/src/cmd/ip/snoopy/dump.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <ip.h>
#include <ctype.h>
#include "dat.h"
#include "protos.h"

static void
p_compile(Filter *)
{
}

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",
	p_compile,
	nil,
	p_seprint,
	nil,
	nil,
	nil,
	defaultframer,
};

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].