plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

dump.c (1033B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <ip.h>
      4 #include <ctype.h>
      5 #include "dat.h"
      6 #include "protos.h"
      7 
      8 static char tohex[16] = {
      9 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
     10 	'a', 'b', 'c', 'd', 'e', 'f'
     11 };
     12 
     13 static int
     14 p_seprint(Msg *m)
     15 {
     16 	int c, i, n, isstring;
     17 	uchar *ps = m->ps;
     18 	char *p = m->p;
     19 	char *e = m->e;
     20 
     21 	n = m->pe - ps;
     22 	if(n > Nflag)
     23 		n = Nflag;
     24 
     25 	isstring = 1;
     26 	for(i = 0; i < n; i++){
     27 		c = ps[i];
     28 		if(!isprint(c) && !isspace(c)){
     29 			isstring = 0;
     30 			break;
     31 		}
     32 	}
     33 
     34 	if(isstring){
     35 		for(i = 0; i < n && p+1<e; i++){
     36 			c = ps[i];
     37 			switch(c){
     38 			case '\t':
     39 				*p++ = '\\';
     40 				*p++ = 't';
     41 				break;
     42 			case '\r':
     43 				*p++ = '\\';
     44 				*p++ = 'r';
     45 				break;
     46 			case '\n':
     47 				*p++ = '\\';
     48 				*p++ = 'n';
     49 				break;
     50 			default:
     51 				*p++ = c;
     52 			}
     53 		}
     54 	} else {
     55 		for(i = 0; i < n && p+1<e; i++){
     56 			c = ps[i];
     57 			*p++ = tohex[c>>4];
     58 			*p++ = tohex[c&0xf];
     59 		}
     60 	}
     61 
     62 	m->pr = nil;
     63 	m->p = p;
     64 	m->ps = ps;
     65 
     66 	return 0;
     67 }
     68 
     69 Proto dump =
     70 {
     71 	"dump",
     72 	nil,
     73 	nil,
     74 	p_seprint,
     75 	nil,
     76 	nil,
     77 	nil,
     78 	defaultframer
     79 };