8 char *whitespace = " \t\r\n";
13 fprint(2, "%s [ -p ] [ -c charset ] [ file ]\n", argv0);
18 attr(char *s, char *a)
22 if((s = cistrstr(s, a)) == nil)
25 while(*s && strchr(whitespace, *s))
29 while(*s && strchr(whitespace, *s))
32 if(*s == '"' || *s == '\'')
39 if(*e == '-' || *e == '_')
44 return smprint("%.*s", (int)(e - s), s);
49 main(int argc, char *argv[])
51 int n, q, pfd[2], pflag = 0;
52 char *arg[4], *s, *g, *e, *p, *a, t;
57 cset = EARGF(usage());
68 if(open(*argv, OREAD) != 0)
72 while(nbuf < sizeof(buf)-1){
73 if((n = read(0, buf + nbuf, sizeof(buf)-1-nbuf)) <= 0)
80 if(nbuf >= 3 && memcmp(p, "\xEF\xBB\xBF", 3)==0){
86 if(nbuf >= 2 && memcmp(p, "\xFE\xFF", 2) == 0){
92 if(nbuf >= 2 && memcmp(p, "\xFF\xFE", 2) == 0){
101 if((s = strchr(s, '<')) == nil)
107 if(*s == '=' && q == 0)
109 else if(*s == '\'' || *s == '"'){
115 else if(*s == '>' && q != '\'' && q != '"'){
119 else if(q == '=' && strchr(whitespace, *s) == nil)
125 if((a = attr(g, "encoding")) != nil || (a = attr(g, "charset")) != nil)
126 if(cistrcmp(a, "utf") != 0 && cistrcmp(a, "utf-8") != 0){
136 while(s+UTFmax < p+nbuf){
137 s += chartorune(&r, s);
158 sysfatal("pipe: %r");
160 switch(rfork(RFFDG|RFREND|RFPROC)){
162 sysfatal("fork: %r");
170 arg[2] = smprint("{tcs -f %s || cat} | tcs -f html", cset);
172 exec("/bin/rc", arg);
180 if(write(1, p, nbuf) != nbuf)
181 sysfatal("write: %r");
183 if((nbuf = read(0, p, sizeof(buf))) < 0)
184 sysfatal("read: %r");