]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/uhtml.c
mothra/uhtml: properly handle quoting in tags
[plan9front.git] / sys / src / cmd / uhtml.c
1 #include <u.h>
2 #include <libc.h>
3 #include <ctype.h>
4
5 int nbuf;
6 char buf[64*1024+1];
7 char *cset = nil;
8
9 void
10 usage(void)
11 {
12         fprint(2, "%s [ -p ] [ -c charset ] [ file ]\n", argv0);
13         exits("usage");
14 }
15
16 char*
17 attr(char *s, char *a)
18 {
19         char *e, q;
20
21         if((s = cistrstr(s, a)) == nil)
22                 return nil;
23         s += strlen(a);
24         while(strchr("\r\n\t ", *s))
25                 s++;
26         if(*s++ != '=')
27                 return nil;
28         while(strchr("\r\n\t ", *s))
29                 s++;
30         q = 0;
31         if(*s == '"' || *s == '\'')
32                 q = *s++;
33         for(e = s; *e; e++){
34                 if(*e == q)
35                         break;
36                 if(isalnum(*e))
37                         continue;
38                 if(*e == '-' || *e == '_')
39                         continue;
40                 break;
41         }
42         if(e - s > 1)
43                 return smprint("%.*s", (int)(e-s), s);
44         return nil;
45 }
46
47 void
48 main(int argc, char *argv[])
49 {
50         int n, q, pfd[2], pflag = 0;
51         char *arg[4], *s, *e, *p, *g, *a, t;
52         Rune r;
53
54         ARGBEGIN {
55         case 'c':
56                 cset = EARGF(usage());
57                 break;
58         case 'p':
59                 pflag = 1;
60                 break;
61         default:
62                 usage();
63         } ARGEND;
64
65         if(*argv){
66                 close(0);
67                 if(open(*argv, OREAD) != 1)
68                         sysfatal("open: %r");
69         }
70         nbuf = 0;
71         p = buf;
72         g = buf;
73         while(nbuf < sizeof(buf)-1){
74                 if((n = read(0, buf + nbuf, sizeof(buf)-1-nbuf)) <= 0)
75                         break;
76                 nbuf += n;
77                 buf[nbuf] = 0;
78                 if(nbuf == n){
79                         if(memcmp(p, "\xEF\xBB\xBF", 3)==0){
80                                 p += 3;
81                                 cset = "utf";
82                                 break;
83                         }
84                         if(memcmp(p, "\xFE\xFF", 2) == 0){
85                                 p += 2;
86                                 cset = "unicode-be";
87                                 break;
88                         }
89                         if(memcmp(p, "\xFF\xFE", 2) == 0){
90                                 p += 2;
91                                 cset = "unicode-le";
92                                 break;
93                         }
94                 }
95                 s = g;
96                 do {
97                         if((s = strchr(s, '<')) == nil)
98                                 break;
99                         q = 0;
100                         g = ++s;
101                         e = buf+nbuf;
102                         while(s < e){
103                                 if(*s == '\'' || *s == '"'){
104                                         if(q == 0)
105                                                 q = *s;
106                                         else if(q == *s)
107                                                 q = 0;
108                                 } else if(*s == '>' && q == 0){
109                                         e = s;
110                                         break;
111                                 }
112                                 s++;
113                         }
114                         t = *e;
115                         *e = 0;
116                         if((a = attr(g, "encoding")) || (a = attr(g, "charset"))){
117                                 cset = a;
118                                 *e = t;
119                                 break;
120                         }
121                         *e = t;
122                         s = ++e;
123                 } while(t);
124         }
125         nbuf -= p - buf;
126
127         if(cset == nil){
128                 cset = "utf";
129                 s = p;
130                 while(s+UTFmax < p+nbuf){
131                         s += chartorune(&r, s);
132                         if(r == Runeerror){
133                                 cset = "latin1";
134                                 break;
135                         }
136                 }
137         }
138
139         if(pflag){
140                 print("%s\n", cset);
141                 exits(0);
142         }
143
144         if(nbuf == 0){
145                 write(1, p, 0);
146                 exits(0);
147         }
148
149         if(pipe(pfd) < 0)
150                 sysfatal("pipe: %r");
151
152         switch(rfork(RFFDG|RFREND|RFPROC)){
153         case -1:
154                 sysfatal("fork: %r");
155         case 0:
156                 dup(pfd[0], 0);
157                 close(pfd[0]);
158                 close(pfd[1]);
159
160                 arg[0] = "rc";
161                 arg[1] = "-c";
162                 arg[2] = smprint("{tcs -f %s || cat} | tcs -f html", cset);
163                 arg[3] = nil;
164                 exec("/bin/rc", arg);
165         }
166
167         dup(pfd[1], 1);
168         close(pfd[0]);
169         close(pfd[1]);
170
171         while(nbuf > 0){
172                 if(write(1, p, nbuf) != nbuf)
173                         sysfatal("write: %r");
174                 p = buf;
175                 if((nbuf = read(0, p, sizeof(buf))) < 0)
176                         sysfatal("read: %r");
177         }
178         close(1);
179         waitpid();
180         exits(0);
181 }