code: 9ferno

ref: 6d69f6fba35087686f79adb2ea0d67944a62ca7b
dir: /appl/cmd/strings.b/

View raw version
#
#	initially generated by c2l
#

implement Strings;

include "draw.m";

Strings: module
{
	init: fn(nil: ref Draw->Context, argl: list of string);
};

include "sys.m";
	sys: Sys;
include "bufio.m";
	bufio: Bufio;
	Iobuf: import bufio;

MINSPAN: con 6;
BUFSIZE: con 70;

init(nil: ref Draw->Context, argl: list of string)
{
	sys = load Sys Sys->PATH;
	bufio = load Bufio Bufio->PATH;
	argc := len argl;
	if(argc < 2){
		stringit("");
		exit;
	}
	argl = tl argl;
	for(i := 1; i < argc; i++){
		if(argc > 2)
			sys->print("%s:\n", hd argl);
		stringit(hd argl);
		argl = tl argl;
	}
}

stringit(str: string)
{
	cnt := 0;
	c: int;
	buf := string array[BUFSIZE] of { * => byte 'z' };

	if(str == nil)
		fin := bufio->fopen(sys->fildes(0), Bufio->OREAD);
	else
		fin = bufio->open(str, Bufio->OREAD);
	if(fin == nil){
		sys->fprint(sys->fildes(2), "cannot open %s\n", str);
		return;
	}
	start := big -1;
	posn := fin.offset();
	while((c = fin.getc()) >= 0){
		if(isprint(c)){
			if(start == big -1)
				start = posn;
			buf[cnt++] = c;
			if(cnt == BUFSIZE){
				sys->print("%8bd: %s ...\n", start, buf[0: cnt]);
				start = big -1;
				cnt = 0;
			}
		}
		else{
			if(cnt >= MINSPAN)
				sys->print("%8bd: %s\n", start, buf[0: cnt]);
			start = big -1;
			cnt = 0;
		}
		posn = fin.offset();
	}
	if(cnt >= MINSPAN)
		sys->print("%8bd: %s\n", start, buf[0: cnt]);
	fin = nil;
}

isprint(r: int): int
{
	if(r >= ' ' && r < 16r7f || r > 16ra0)
		return 1;
	else
		return 0;
}