]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/uhtml.c
fix strchr \0 bugs
[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 char *whitespace = " \t\r\n";
9
10 void
11 usage(void)
12 {
13         fprint(2, "%s [ -p ] [ -c charset ] [ file ]\n", argv0);
14         exits("usage");
15 }
16
17 char*
18 attr(char *s, char *a)
19 {
20         char *e, q;
21
22         if((s = cistrstr(s, a)) == nil)
23                 return nil;
24         s += strlen(a);
25         while(*s && strchr(whitespace, *s))
26                 s++;
27         if(*s++ != '=')
28                 return nil;
29         while(*s && strchr(whitespace, *s))
30                 s++;
31         q = 0;
32         if(*s == '"' || *s == '\'')
33                 q = *s++;
34         for(e = s; *e; e++){
35                 if(*e == q)
36                         break;
37                 if(isalnum(*e))
38                         continue;
39                 if(*e == '-' || *e == '_')
40                         continue;
41                 break;
42         }
43         if((e - s) > 1)
44                 return smprint("%.*s", (int)(e - s), s);
45         return nil;
46 }
47
48 void
49 main(int argc, char *argv[])
50 {
51         int n, q, pfd[2], pflag = 0;
52         char *arg[4], *s, *e, *p, *g, *a, t;
53         Rune r;
54
55         ARGBEGIN {
56         case 'c':
57                 cset = EARGF(usage());
58                 break;
59         case 'p':
60                 pflag = 1;
61                 break;
62         default:
63                 usage();
64         } ARGEND;
65
66         if(*argv){
67                 close(0);
68                 if(open(*argv, OREAD) != 1)
69                         sysfatal("open: %r");
70         }
71         nbuf = 0;
72         p = buf;
73         g = buf;
74         while(nbuf < sizeof(buf)-1){
75                 if((n = read(0, buf + nbuf, sizeof(buf)-1-nbuf)) <= 0)
76                         break;
77                 nbuf += n;
78                 buf[nbuf] = 0;
79                 if(nbuf == n){
80                         if(memcmp(p, "\xEF\xBB\xBF", 3)==0){
81                                 p += 3;
82                                 nbuf -= 3;
83                                 cset = "utf";
84                                 goto Found;
85                         }
86                         if(memcmp(p, "\xFE\xFF", 2) == 0){
87                                 p += 2;
88                                 nbuf -= 2;
89                                 cset = "unicode-be";
90                                 goto Found;
91                         }
92                         if(memcmp(p, "\xFF\xFE", 2) == 0){
93                                 p += 2;
94                                 nbuf -= 2;
95                                 cset = "unicode-le";
96                                 goto Found;
97                         }
98                 }
99                 s = g;
100                 do {
101                         if((s = strchr(s, '<')) == nil)
102                                 break;
103                         q = 0;
104                         g = ++s;
105                         e = buf+nbuf;
106                         while(s < e){
107                                 if(*s == '\'' || *s == '"'){
108                                         if(q == 0)
109                                                 q = *s;
110                                         else if(q == *s)
111                                                 q = 0;
112                                 } else if(*s == '>' && q == 0){
113                                         e = s;
114                                         break;
115                                 }
116                                 s++;
117                         }
118                         t = *e;
119                         *e = 0;
120                         if((a = attr(g, "encoding")) || (a = attr(g, "charset"))){
121                                 *e = t;
122                                 cset = a;
123                                 goto Found;
124                         }
125                         *e = t;
126                         s = ++e;
127                 } while(t);
128         }
129         s = p;
130         while(s+UTFmax < p+nbuf){
131                 s += chartorune(&r, s);
132                 if(r == Runeerror){
133                         cset = "latin1";
134                         goto Found;
135                 }
136         }
137         cset = "utf";
138 Found:
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 }