]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/uhtml.c
merge
[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) != 0)
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 == '=' && q == 0)
108                                         q = '=';
109                                 else if(*s == '\'' || *s == '"'){
110                                         if(q == '=')
111                                                 q = *s;
112                                         else if(q == *s)
113                                                 q = 0;
114                                 }
115                                 else if(*s == '>' && q != '\'' && q != '"'){
116                                         e = s;
117                                         break;
118                                 }
119                                 else if(q == '=' && strchr(whitespace, *s) == nil)
120                                         q = 0;
121                                 s++;
122                         }
123                         t = *e;
124                         *e = 0;
125                         if((a = attr(g, "encoding")) || (a = attr(g, "charset"))){
126                                 *e = t;
127                                 cset = a;
128                                 goto Found;
129                         }
130                         *e = t;
131                         s = ++e;
132                 } while(t);
133         }
134         s = p;
135         while(s+UTFmax < p+nbuf){
136                 s += chartorune(&r, s);
137                 if(r == Runeerror){
138                         cset = "latin1";
139                         goto Found;
140                 }
141         }
142         cset = "utf";
143 Found:
144         if(pflag){
145                 print("%s\n", cset);
146                 exits(0);
147         }
148
149         if(nbuf == 0){
150                 write(1, p, 0);
151                 exits(0);
152         }
153
154         if(pipe(pfd) < 0)
155                 sysfatal("pipe: %r");
156
157         switch(rfork(RFFDG|RFREND|RFPROC)){
158         case -1:
159                 sysfatal("fork: %r");
160         case 0:
161                 dup(pfd[0], 0);
162                 close(pfd[0]);
163                 close(pfd[1]);
164
165                 arg[0] = "rc";
166                 arg[1] = "-c";
167                 arg[2] = smprint("{tcs -f %s || cat} | tcs -f html", cset);
168                 arg[3] = nil;
169                 exec("/bin/rc", arg);
170         }
171
172         dup(pfd[1], 1);
173         close(pfd[0]);
174         close(pfd[1]);
175
176         while(nbuf > 0){
177                 if(write(1, p, nbuf) != nbuf)
178                         sysfatal("write: %r");
179                 p = buf;
180                 if((nbuf = read(0, p, sizeof(buf))) < 0)
181                         sysfatal("read: %r");
182         }
183         close(1);
184         waitpid();
185         exits(0);
186 }