plan9fox/sys/src/cmd/uhtml.c

182 lines
2.7 KiB
C
Raw Normal View History

#include <u.h>
#include <libc.h>
#include <ctype.h>
int nbuf;
2011-10-05 02:47:53 +00:00
char buf[64*1024+1];
char *cset = nil;
void
usage(void)
{
fprint(2, "%s [ -p ] [ -c charset ] [ file ]\n", argv0);
exits("usage");
}
char*
attr(char *s, char *a)
{
char *e, q;
if((s = cistrstr(s, a)) == nil)
return nil;
s += strlen(a);
while(strchr("\r\n\t ", *s))
s++;
if(*s++ != '=')
return nil;
while(strchr("\r\n\t ", *s))
s++;
q = 0;
if(*s == '"' || *s == '\'')
q = *s++;
for(e = s; *e; e++){
if(*e == q)
break;
if(isalnum(*e))
continue;
if(*e == '-' || *e == '_')
continue;
break;
}
if(e - s > 1)
return smprint("%.*s", (int)(e-s), s);
return nil;
}
void
main(int argc, char *argv[])
{
int n, q, pfd[2], pflag = 0;
2012-02-20 19:54:42 +00:00
char *arg[4], *s, *e, *p, *g, *a, t;
2011-10-05 02:47:53 +00:00
Rune r;
ARGBEGIN {
case 'c':
cset = EARGF(usage());
break;
case 'p':
pflag = 1;
break;
default:
usage();
} ARGEND;
if(*argv){
close(0);
if(open(*argv, OREAD) != 1)
sysfatal("open: %r");
}
2011-10-05 02:47:53 +00:00
nbuf = 0;
p = buf;
g = buf;
2011-10-05 02:47:53 +00:00
while(nbuf < sizeof(buf)-1){
if((n = read(0, buf + nbuf, sizeof(buf)-1-nbuf)) <= 0)
break;
2011-10-05 02:47:53 +00:00
nbuf += n;
buf[nbuf] = 0;
if(nbuf == n){
if(memcmp(p, "\xEF\xBB\xBF", 3)==0){
p += 3;
cset = "utf";
break;
}
if(memcmp(p, "\xFE\xFF", 2) == 0){
p += 2;
cset = "unicode-be";
break;
}
if(memcmp(p, "\xFF\xFE", 2) == 0){
p += 2;
cset = "unicode-le";
break;
}
}
s = g;
do {
if((s = strchr(s, '<')) == nil)
break;
q = 0;
g = ++s;
e = buf+nbuf;
while(s < e){
if(*s == '\'' || *s == '"'){
if(q == 0)
q = *s;
else if(q == *s)
q = 0;
} else if(*s == '>' && q == 0){
e = s;
break;
}
s++;
}
t = *e;
*e = 0;
if((a = attr(g, "encoding")) || (a = attr(g, "charset"))){
2012-02-20 19:54:42 +00:00
cset = a;
*e = t;
break;
}
*e = t;
s = ++e;
} while(t);
}
nbuf -= p - buf;
2011-10-05 02:47:53 +00:00
if(cset == nil){
cset = "utf";
s = p;
while(s+UTFmax < p+nbuf){
s += chartorune(&r, s);
if(r == Runeerror){
cset = "latin1";
break;
}
}
}
if(pflag){
print("%s\n", cset);
exits(0);
}
if(nbuf == 0){
write(1, p, 0);
exits(0);
}
if(pipe(pfd) < 0)
sysfatal("pipe: %r");
switch(rfork(RFFDG|RFREND|RFPROC)){
case -1:
sysfatal("fork: %r");
case 0:
dup(pfd[0], 0);
close(pfd[0]);
close(pfd[1]);
arg[0] = "rc";
arg[1] = "-c";
arg[2] = smprint("{tcs -f %s || cat} | tcs -f html", cset);
arg[3] = nil;
exec("/bin/rc", arg);
}
dup(pfd[1], 1);
close(pfd[0]);
close(pfd[1]);
while(nbuf > 0){
if(write(1, p, nbuf) != nbuf)
sysfatal("write: %r");
p = buf;
if((nbuf = read(0, p, sizeof(buf))) < 0)
sysfatal("read: %r");
}
close(1);
waitpid();
exits(0);
}