[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

unicode.b



i rewrote the plan9/unix command unicode to run 
under inferno. it understands only the -n option.

erik

#
#
implement Unicode;

include "sys.m";
sys: Sys;

include "draw.m";
Context: import Draw;

include "lib.m";
str: String;

Unicode: module {
	init: fn(c: ref Context, v: list of string);
};

whine(l: list of string){
	for(;len l; l = tl l){
		sys->fprint(sys->fildes(2), "%s\n", hd l);
	}
	exit;
}

printList(i,j: int){
	a: int;

	if (j<i){
		a=i;
		i=j;
		j=a;
	}
	for(a=i;a<=j;a++){
		sys->print("%c", a);
	}
}

printFancyList(i,j: int){
	a: int;

	if(j<i){
		a=i;
		i=j;
		j=a;
	}
	for (a=i;a<=j;a++){
		sys->print("%4.4x %c", a, a);

		if (a%8){
			# WmShell broken sys->print("\t");
			sys->print("  ");
		}else{
			sys->print("\n");
		}
	}
	a--;
	if (a%8){
		sys->print("\n");
	}
}

#mostly stolen from sh
revList(arg: list of string): list of string{
	ret: list of string;

	for(;nil != arg; arg=tl arg){
		ret = hd arg :: ret;
	}
	return ret;
}

munchArgs(v: list of string): (int, list of string){
	w: list of string;
	dashn: int;

	if (0 == len v){
		sys->fprint(sys->fildes(2), "unicode: munchArgs called with 0 arguments\n");
	}

	dashn = 0;
	for(v=tl v; len v; v=tl v){
		case hd v{
		"-n" =>
			dashn = 1;
		* =>
			w = hd v :: w;
		}
	}
	return (dashn,revList(w));
}
	
init(c: ref Context, v: list of string){
	i,j: int;
	rem: string;
	dashn: int;

	sys = load Sys Sys->PATH;
	if (nil == sys){
		sys->fprint(sys->fildes(2), "unicode: %r\n");
		return;
	}

	str = load String String->PATH;
	if (nil == str){
		sys->fprint(sys->fildes(2), "unicode: %r\n");
		return;
	}
	
	if (nil == v){
		return;
	}
	(dashn, v) = munchArgs(v);
	if (nil == v){
		return;
	}

	for(; v != nil; v = tl v){
		(i,rem) = str->toint(hd v,16);
		if (nil != rem && '-' == rem[0]){
			rem=rem[1:];
			(j,rem) = str->toint(rem,16);
		}else{
			j=i;
		}
		if(nil != rem){
			whine(list of {"string->toint() looses"});
		}
		if(dashn){
			printFancyList(i,j);
		}else{
			printList(i,j);
		}
	}
}