]> 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[4096+1];
7 char *cset = "utf";
8
9 void
10 usage(void)
11 {
12         fprint(2, "%s [ -h ] [ -c charset ] [ file ]\n", argv0);
13         exits("usage");
14 }
15
16 char*
17 strval(char *s)
18 {
19         char *e, q;
20
21         while(strchr("\t ", *s))
22                 s++;
23         q = 0;
24         if(*s == '"' || *s == '\'')
25                 q = *s++;
26         for(e = s; *e; e++){
27                 if(*e == q)
28                         break;
29                 if(isalnum(*e))
30                         continue;
31                 if(*e == '-' || *e == '_')
32                         continue;
33                 break;
34         }
35         if(e - s > 1)
36                 return smprint("%.*s", (int)(e-s), s);
37         return nil;
38 }
39
40 void
41 main(int argc, char *argv[])
42 {
43         int pfd[2], pflag = 0;
44         char *arg[4], *s;
45
46         ARGBEGIN {
47         case 'h':
48                 usage();
49         case 'c':
50                 cset = EARGF(usage());
51                 break;
52         case 'p':
53                 pflag = 1;
54                 break;
55         } ARGEND;
56
57         if(*argv){
58                 close(0);
59                 if(open(*argv, OREAD) != 1)
60                         sysfatal("open: %r");
61         }
62         if((nbuf = read(0, buf, sizeof(buf)-1)) < 0)
63                 sysfatal("read: %r");
64         buf[nbuf] = 0;
65
66         /* useless BOM marker */
67         if(memcmp(buf, "\xEF\xBB\xBF", 3)==0)
68                 memmove(buf, buf+3, nbuf-3);
69
70         for(;;){
71                 if(s = cistrstr(buf, "encoding="))
72                         if(s = strval(s+9)){
73                                 cset = s;
74                                 break;
75                         }
76                 if(s = cistrstr(buf, "charset="))
77                         if(s = strval(s+8)){
78                                 cset = s;
79                                 break;
80                         }
81                 break;
82         }
83
84         if(pflag){
85                 print("%s\n", cset);
86                 exits(0);
87         }
88
89         if(pipe(pfd) < 0)
90                 sysfatal("pipe: %r");
91
92         if(nbuf == 0){
93                 write(1, buf, 0);
94                 exits(0);
95         }
96
97         switch(rfork(RFFDG|RFREND|RFPROC|RFNOWAIT)){
98         case -1:
99                 sysfatal("fork: %r");
100         case 0:
101                 dup(pfd[0], 0);
102                 close(pfd[0]);
103                 close(pfd[1]);
104
105                 arg[0] = "rc";
106                 arg[1] = "-c";
107                 arg[2] = smprint("{tcs -f %s | tcs -f html} || cat", cset);
108                 arg[3] = nil;
109                 exec("/bin/rc", arg);
110         }
111
112         dup(pfd[1], 1);
113         close(pfd[0]);
114         close(pfd[1]);
115
116         while(nbuf > 0){
117                 if(write(1, buf, nbuf) != nbuf)
118                         sysfatal("write: %r");
119                 if((nbuf = read(0, buf, sizeof(buf))) < 0)
120                         sysfatal("read: %r");
121         }
122         exits(0);
123 }