filter.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <draw.h>
  12. #include <event.h>
  13. #include <bio.h>
  14. #include "page.h"
  15. Document*
  16. initfilt(Biobuf *b, int argc, char **argv, uint8_t *buf, int nbuf,
  17. char *type, char *cmd, int docopy)
  18. {
  19. int ofd;
  20. int p[2];
  21. char xbuf[8192];
  22. int n;
  23. if(argc > 1) {
  24. fprint(2, "can only view one %s file at a time\n", type);
  25. return nil;
  26. }
  27. fprint(2, "converting from %s to postscript...\n", type);
  28. if(docopy){
  29. if(pipe(p) < 0){
  30. fprint(2, "pipe fails: %r\n");
  31. exits("Epipe");
  32. }
  33. }else{
  34. p[0] = open("/dev/null", ORDWR);
  35. p[1] = open("/dev/null", ORDWR);
  36. }
  37. ofd = opentemp("/tmp/pagecvtXXXXXXXXX");
  38. switch(fork()){
  39. case -1:
  40. fprint(2, "fork fails: %r\n");
  41. exits("Efork");
  42. default:
  43. close(p[1]);
  44. if(docopy){
  45. write(p[0], buf, nbuf);
  46. if(b)
  47. while((n = Bread(b, xbuf, sizeof xbuf)) > 0)
  48. write(p[0], xbuf, n);
  49. else
  50. while((n = read(stdinfd, xbuf, sizeof xbuf)) > 0)
  51. write(p[0], xbuf, n);
  52. }
  53. close(p[0]);
  54. waitpid();
  55. break;
  56. case 0:
  57. close(p[0]);
  58. dup(p[1], 0);
  59. dup(ofd, 1);
  60. /* stderr shines through */
  61. execl("/bin/rc", "rc", "-c", cmd, nil);
  62. break;
  63. }
  64. if(b)
  65. Bterm(b);
  66. seek(ofd, 0, 0);
  67. b = emalloc(sizeof(Biobuf));
  68. Binit(b, ofd, OREAD);
  69. return initps(b, argc, argv, nil, 0);
  70. }
  71. Document*
  72. initdvi(Biobuf *b, int argc, char **argv, uint8_t *buf, int nbuf)
  73. {
  74. int fd;
  75. char *name;
  76. char cmd[256];
  77. char fdbuf[20];
  78. /*
  79. * Stupid DVIPS won't take standard input.
  80. */
  81. if(b == nil){ /* standard input; spool to disk (ouch) */
  82. fd = spooltodisk(buf, nbuf, &name);
  83. sprint(fdbuf, "/fd/%d", fd);
  84. b = Bopen(fdbuf, OREAD);
  85. if(b == nil){
  86. fprint(2, "cannot open disk spool file\n");
  87. wexits("Bopen temp");
  88. }
  89. argv = &name;
  90. argc = 1;
  91. }
  92. snprint(cmd, sizeof cmd, "dvips -Pps -r0 -q1 -f1 '%s'", argv[0]);
  93. return initfilt(b, argc, argv, buf, nbuf, "dvi", cmd, 0);
  94. }
  95. Document*
  96. inittroff(Biobuf *b, int argc, char **argv, uint8_t *buf, int nbuf)
  97. {
  98. /* Added -H to eliminate header page [sape] */
  99. return initfilt(b, argc, argv, buf, nbuf, "troff", "lp -H -dstdout", 1);
  100. }
  101. Document*
  102. initmsdoc(Biobuf *b, int argc, char **argv, uint8_t *buf, int nbuf)
  103. {
  104. return initfilt(b, argc, argv, buf, nbuf, "microsoft office", "doc2ps", 1);
  105. }