Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <thread.h>
5 #include <ctype.h>
6 #include <plumb.h>
7 #include <9pclient.h>
8 #include "dat.h"
11 char*
12 formathtml(char *body, int *np)
13 {
14 int i, j, p[2], q[2];
15 Exec *e;
16 char buf[1024];
17 Channel *sync;
19 e = emalloc(sizeof(struct Exec));
20 if(pipe(p) < 0 || pipe(q) < 0)
21 error("can't create pipe: %r");
23 e->p[0] = p[0];
24 e->p[1] = p[1];
25 e->q[0] = q[0];
26 e->q[1] = q[1];
27 e->argv = emalloc(3*sizeof(char*));
28 e->argv[0] = estrdup("htmlfmt");
29 e->argv[1] = estrdup("-cutf-8");
30 e->argv[2] = nil;
31 e->prog = unsharp("#9/bin/htmlfmt");
32 sync = chancreate(sizeof(int), 0);
33 e->sync = sync;
34 proccreate(execproc, e, EXECSTACK);
35 recvul(sync);
36 // close(p[0]);
37 close(q[1]);
39 if((i=write(p[1], body, *np)) != *np){
40 fprint(2, "Mail: warning: htmlfmt failed: wrote %d of %d: %r\n", i, *np);
41 close(p[1]);
42 close(q[0]);
43 return body;
44 }
45 close(p[1]);
47 free(body);
48 body = nil;
49 i = 0;
50 for(;;){
51 j = read(q[0], buf, sizeof buf);
52 if(j <= 0)
53 break;
54 body = realloc(body, i+j+1);
55 if(body == nil)
56 error("realloc failed: %r");
57 memmove(body+i, buf, j);
58 i += j;
59 body[i] = '\0';
60 }
61 close(q[0]);
63 *np = i;
64 return body;
65 }
67 char*
68 readbody(char *type, char *dir, int *np)
69 {
70 char *body;
72 body = readfile(dir, "body", np);
73 if(body != nil && strcmp(type, "text/html") == 0)
74 return formathtml(body, np);
75 return body;
76 }