Beispiel #1
0
void
main(int argc, char **argv)
{
	int i, dostdin;
	char *p;
	Rune *r;
	Rune buf[2];
	
	Binit(&bout, 1, OWRITE);
	fmtinstall('L', linefmt);
	quotefmtinstall();
	
	tmacdir = unsharp("#9/tmac");
	dostdin = 0;
	ARGBEGIN{
	case 'i':
		dostdin = 1;
		break;
	case 'm':
		r = erunesmprint("%s/tmac.%s", tmacdir, EARGF(usage()));
		if(queueinputfile(r) < 0)
			fprint(2, "%S: %r\n", r);
		break;
	case 'r':
		p = EARGF(usage());
		p += chartorune(buf, p);
		buf[1] = 0;
		_nr(buf, erunesmprint("%s", p+1));
		break;
	case 'u':
		utf8 = 1;
		break;
	case 'v':
		verbose = 1;
		break;
	default:
		usage();
	}ARGEND

	for(i=0; i<argc; i++){
		if(strcmp(argv[i], "-") == 0)
			queuestdin();
		else
			queueinputfile(erunesmprint("%s", argv[i]));
	}
	if(argc == 0 || dostdin)
		queuestdin();
	
	run();
	Bprint(&bout, "\n");
	Bterm(&bout);
	exits(nil);
}
Beispiel #2
0
/*
 * Translate Unicode to HTML by asking tcs(1).
 * This way we don't have yet another table.
 */
Rune*
rune2html(Rune r)
{
	static Biobuf b;
	static int fd = -1;
	static Rune **tcscache[256];
	int p[2];
	char *q;
	
	if(r == '\n')
		return L("\n");

	if(((uint)r&~0xFFFF) != 0){
		/* The cache must grow a lot to handle them */
		fprint(2, "%s: can't handle rune '%C'\n", argv0, r);
		return L("?");
	}

	if(tcscache[r>>8] && tcscache[r>>8][r&0xFF])
		return tcscache[r>>8][r&0xFF];

	if(fd < 0){
		if(pipe(p) < 0)
			sysfatal("pipe: %r");
		switch(fork()){
		case -1:
			sysfatal("fork: %r");
		case 0:
			dup2(p[0], 0);
			dup2(p[1], 1);
			close(p[0]);
			close(p[1]);
			execl(BINDIR "/tcs", "tcs", "-t", "html", nil);
			_exits(0);
		default:
			fd = p[1];
			Binit(&b, p[0], OREAD);
			break;
		}
	}
	/* HACK: extra newlines force rune+\n through tcs now */
	fprint(fd, "%C\n\n\n\n", r);
	q = Brdline(&(b.Biobufhdr), '\n');
	while (q != nil && *q == '\n')
		q = Brdline(&(b.Biobufhdr), '\n');
	if(q == nil)
		sysfatal("tcs: early eof");
	q[Blinelen(&(b.Biobufhdr))-1] = 0;
	if(tcscache[r>>8] == nil)
		tcscache[r>>8] = emalloc(256*sizeof tcscache[0][0]);
	tcscache[r>>8][r&0xFF] = erunesmprint("%s", q);
	return tcscache[r>>8][r&0xFF];
}