html.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <thread.h>
  13. #include <ctype.h>
  14. #include <plumb.h>
  15. #include "dat.h"
  16. char*
  17. formathtml(char *body, int *np)
  18. {
  19. int i, j, p[2], q[2];
  20. Exec *e;
  21. char buf[1024];
  22. Channel *sync;
  23. e = emalloc(sizeof(struct Exec));
  24. if(pipe(p) < 0 || pipe(q) < 0)
  25. error("can't create pipe: %r");
  26. e->p[0] = p[0];
  27. e->p[1] = p[1];
  28. e->q[0] = q[0];
  29. e->q[1] = q[1];
  30. e->argv = emalloc(3*sizeof(char*));
  31. e->argv[0] = estrdup("htmlfmt");
  32. e->argv[1] = estrdup("-cutf-8");
  33. e->argv[2] = nil;
  34. e->prog = "/bin/htmlfmt";
  35. sync = chancreate(sizeof(int), 0);
  36. e->sync = sync;
  37. proccreate(execproc, e, EXECSTACK);
  38. recvul(sync);
  39. close(p[0]);
  40. close(q[1]);
  41. if((i=write(p[1], body, *np)) != *np){
  42. fprint(2, "Mail: warning: htmlfmt failed: wrote %d of %d: %r\n", i, *np);
  43. close(p[1]);
  44. close(q[0]);
  45. return body;
  46. }
  47. close(p[1]);
  48. free(body);
  49. body = nil;
  50. i = 0;
  51. for(;;){
  52. j = read(q[0], buf, sizeof buf);
  53. if(j <= 0)
  54. break;
  55. body = realloc(body, i+j+1);
  56. if(body == nil)
  57. error("realloc failed: %r");
  58. memmove(body+i, buf, j);
  59. i += j;
  60. body[i] = '\0';
  61. }
  62. close(q[0]);
  63. *np = i;
  64. return body;
  65. }
  66. char*
  67. readbody(char *type, char *dir, int *np)
  68. {
  69. char *body;
  70. body = readfile(dir, "body", np);
  71. if(body != nil && strcmp(type, "text/html") == 0)
  72. return formathtml(body, np);
  73. return body;
  74. }