URI:
       tfilter.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tfilter.c (2303B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <draw.h>
            4 #include <thread.h>
            5 #include <bio.h>
            6 #include <cursor.h>
            7 #include "page.h"
            8 
            9 Document*
           10 initfilt(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf, char *type, char *cmd, int docopy)
           11 {
           12         int ofd;
           13         int p[2];
           14         char xbuf[8192];
           15         int n;
           16         char template[] = "/tmp/pagecvtXXXXXXXXX";
           17 
           18         if(argc > 1) {
           19                 fprint(2, "can only view one %s file at a time\n", type);
           20                 return nil;
           21         }
           22 
           23         fprint(2, "converting from %s to postscript...\n", type);
           24 
           25         if(docopy){
           26                 if(pipe(p) < 0){
           27                         fprint(2, "pipe fails: %r\n");
           28                         threadexits("Epipe");
           29                 }
           30         }else{
           31                 p[0] = open("/dev/null", ORDWR);
           32                 p[1] = open("/dev/null", ORDWR);
           33         }
           34 
           35         ofd = opentemp(template, ORDWR|ORCLOSE);
           36         switch(fork()){
           37         case -1:
           38                 fprint(2, "fork fails: %r\n");
           39                 threadexits("Efork");
           40         default:
           41                 close(p[0]);
           42                 if(docopy){
           43                         write(p[1], buf, nbuf);
           44                         if(b)
           45                                 while((n = Bread(b, xbuf, sizeof xbuf)) > 0)
           46                                         write(p[1], xbuf, n);
           47                         else
           48                                 while((n = read(stdinfd, xbuf, sizeof xbuf)) > 0)
           49                                         write(p[1], xbuf, n);
           50                 }
           51                 close(p[1]);
           52                 waitpid();
           53                 break;
           54         case 0:
           55                 close(p[1]);
           56                 dup(p[0], 0);
           57                 dup(ofd, 1);
           58                 /* stderr shines through */
           59                 if(chatty)
           60                         fprint(2, "Execing '%s'\n", cmd);
           61                 execlp("rc", "rc", "-c", cmd, nil);
           62                 break;
           63         }
           64 
           65         if(b)
           66                 Bterm(b);
           67         seek(ofd, 0, 0);
           68         b = emalloc(sizeof(Biobuf));
           69         Binit(b, ofd, OREAD);
           70 
           71         return initps(b, argc, argv, nil, 0);
           72 }
           73 
           74 Document*
           75 initdvi(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
           76 {
           77         int fd;
           78         char *name;
           79         char cmd[256];
           80         char fdbuf[20];
           81 
           82         /*
           83          * Stupid DVIPS won't take standard input.
           84          */
           85         if(b == nil){        /* standard input; spool to disk (ouch) */
           86                 fd = spooltodisk(buf, nbuf, &name);
           87                 sprint(fdbuf, "/dev/fd/%d", fd);
           88                 b = Bopen(fdbuf, OREAD);
           89                 if(b == nil){
           90                         fprint(2, "cannot open disk spool file\n");
           91                         wexits("Bopen temp");
           92                 }
           93                 argv = &name;
           94                 argc = 1;
           95         }
           96 
           97         snprint(cmd, sizeof cmd, "dvips -Pps -r0 -q1 -f1 '%s'", argv[0]);
           98         return initfilt(b, argc, argv, buf, nbuf, "dvi", cmd, 0);
           99 }
          100 
          101 Document*
          102 inittroff(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
          103 {
          104         return initfilt(b, argc, argv, buf, nbuf, "troff", "9 tr2post | 9 psfonts", 1);
          105 }
          106 
          107 Document*
          108 initmsdoc(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
          109 {
          110         return initfilt(b, argc, argv, buf, nbuf, "microsoft office", "doc2ps", 1);
          111 }