rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 1 | #include <u.h> |
| 2 | #include <libc.h> |
| 3 | #include <bio.h> |
| 4 | #include <stdio.h> |
| 5 | #include "common.h" |
| 6 | #include "tr2post.h" |
| 7 | #include "comments.h" |
| 8 | #include "path.h" |
| 9 | |
| 10 | int formsperpage = 1; |
| 11 | int picflag = 1; |
| 12 | double aspectratio = 1.0; |
| 13 | int copies = 1; |
| 14 | int landscape = 0; |
| 15 | double magnification = 1.0; |
| 16 | int linesperpage = 66; |
| 17 | int pointsize = 10; |
| 18 | double xoffset = .25; |
| 19 | double yoffset = .25; |
| 20 | char *passthrough = 0; |
| 21 | |
| 22 | Biobuf binp, *bstdout, bstderr; |
| 23 | Biobuf *Bstdin, *Bstdout, *Bstderr; |
| 24 | int debug = 0; |
| 25 | |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 26 | #ifndef MAXPATHLEN |
| 27 | #define MAXPATHLEN 255 |
| 28 | #endif |
| 29 | |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 30 | char tmpfilename[MAXPATHLEN+1]; |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 31 | char copybuf[BUFSIZ]; |
| 32 | |
| 33 | |
| 34 | struct charent **build_char_list = 0; |
| 35 | int build_char_cnt = 0; |
| 36 | |
| 37 | void |
| 38 | prologues(void) { |
| 39 | int i; |
| 40 | char charlibname[MAXTOKENSIZE]; |
| 41 | |
| 42 | Bprint(Bstdout, "%s", CONFORMING); |
| 43 | Bprint(Bstdout, "%s %s\n", VERSION, PROGRAMVERSION); |
| 44 | Bprint(Bstdout, "%s %s\n", DOCUMENTFONTS, ATEND); |
| 45 | Bprint(Bstdout, "%s %s\n", PAGES, ATEND); |
| 46 | Bprint(Bstdout, "%s", ENDCOMMENTS); |
| 47 | |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 48 | if (cat(unsharp(DPOST))) { |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 49 | Bprint(Bstderr, "can't read %s\n", DPOST); |
| 50 | exits("dpost prologue"); |
| 51 | } |
| 52 | |
| 53 | if (drawflag) { |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 54 | if (cat(unsharp(DRAW))) { |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 55 | Bprint(Bstderr, "can't read %s\n", DRAW); |
| 56 | exits("draw prologue"); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (DOROUND) |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 61 | cat(unsharp(ROUNDPAGE)); |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 62 | |
| 63 | Bprint(Bstdout, "%s", ENDPROLOG); |
| 64 | Bprint(Bstdout, "%s", BEGINSETUP); |
| 65 | Bprint(Bstdout, "mark\n"); |
| 66 | if (formsperpage > 1) { |
| 67 | Bprint(Bstdout, "%s %d\n", FORMSPERPAGE, formsperpage); |
| 68 | Bprint(Bstdout, "/formsperpage %d def\n", formsperpage); |
| 69 | } |
| 70 | if (aspectratio != 1) Bprint(Bstdout, "/aspectratio %g def\n", aspectratio); |
| 71 | if (copies != 1) Bprint(Bstdout, "/#copies %d store\n", copies); |
| 72 | if (landscape) Bprint(Bstdout, "/landscape true def\n"); |
| 73 | if (magnification != 1) Bprint(Bstdout, "/magnification %g def\n", magnification); |
| 74 | if (pointsize != 10) Bprint(Bstdout, "/pointsize %d def\n", pointsize); |
| 75 | if (xoffset != .25) Bprint(Bstdout, "/xoffset %g def\n", xoffset); |
| 76 | if (yoffset != .25) Bprint(Bstdout, "/yoffset %g def\n", yoffset); |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 77 | cat(unsharp(ENCODINGDIR"/Latin1.enc")); |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 78 | if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough); |
| 79 | |
| 80 | Bprint(Bstdout, "setup\n"); |
| 81 | if (formsperpage > 1) { |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 82 | cat(unsharp(FORMFILE)); |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 83 | Bprint(Bstdout, "%d setupforms \n", formsperpage); |
| 84 | } |
| 85 | /* output Build character info from charlib if necessary. */ |
| 86 | |
| 87 | for (i=0; i<build_char_cnt; i++) { |
| 88 | sprint(charlibname, "%s/%s", CHARLIB, build_char_list[i]->name); |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 89 | if (cat(unsharp(charlibname))) |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 90 | Bprint(Bstderr, "cannot open %s\n", charlibname); |
| 91 | } |
| 92 | |
| 93 | Bprint(Bstdout, "%s", ENDSETUP); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | cleanup(void) { |
| 98 | remove(tmpfilename); |
| 99 | } |
| 100 | |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 101 | int |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 102 | main(int argc, char *argv[]) { |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 103 | Biobuf btmp; |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 104 | Biobuf *binp; |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 105 | Biobuf *Binp; |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 106 | int i, tot, ifd, fd; |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 107 | char *t; |
| 108 | |
| 109 | programname = argv[0]; |
| 110 | if (Binit(&bstderr, 2, OWRITE) == Beof) { |
| 111 | exits("Binit"); |
| 112 | } |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 113 | Bstderr = &bstderr; /* &bstderr.Biobufhdr; */ |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 114 | |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 115 | bstdout = &btmp; |
| 116 | fd = safe_tmpnam(tmpfilename); |
| 117 | if ((Binit(bstdout, fd, OWRITE)) == Beof) { |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 118 | Bprint(Bstderr, "cannot open temporary file %s\n", tmpfilename); |
| 119 | exits("Bopen"); |
| 120 | } |
| 121 | atexit(cleanup); |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 122 | Bstdout = bstdout; /* &bstdout->Biobufhdr; */ |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 123 | |
| 124 | ARGBEGIN{ |
| 125 | case 'a': /* aspect ratio */ |
| 126 | aspectratio = atof(ARGF()); |
| 127 | break; |
| 128 | case 'c': /* copies */ |
| 129 | copies = atoi(ARGF()); |
| 130 | break; |
| 131 | case 'd': |
| 132 | debug = 1; |
| 133 | break; |
| 134 | case 'm': /* magnification */ |
| 135 | magnification = atof(ARGF()); |
| 136 | break; |
| 137 | case 'n': /* forms per page */ |
| 138 | formsperpage = atoi(ARGF()); |
| 139 | break; |
| 140 | case 'o': /* output page list */ |
| 141 | pagelist(ARGF()); |
| 142 | break; |
| 143 | case 'p': /* landscape or portrait mode */ |
| 144 | if ( ARGF()[0] == 'l' ) |
| 145 | landscape = 1; |
| 146 | else |
| 147 | landscape = 0; |
| 148 | break; |
| 149 | case 'x': /* shift things horizontally */ |
| 150 | xoffset = atof(ARGF()); |
| 151 | break; |
| 152 | case 'y': /* and vertically on the page */ |
| 153 | yoffset = atof(ARGF()); |
| 154 | break; |
| 155 | case 'P': /* PostScript pass through */ |
| 156 | t = ARGF(); |
| 157 | i = strlen(t) + 1; |
| 158 | passthrough = malloc(i); |
| 159 | if (passthrough == 0) { |
| 160 | Bprint(Bstderr, "cannot allocate memory for argument string\n"); |
| 161 | exits("malloc"); |
| 162 | } |
| 163 | strncpy(passthrough, t, i); |
| 164 | break; |
| 165 | default: /* don't know what to do for ch */ |
| 166 | Bprint(Bstderr, "unknown option %C\n", ARGC()); |
| 167 | break; |
| 168 | }ARGEND; |
| 169 | readDESC(); |
| 170 | if (argc == 0) { |
| 171 | if ((binp = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0) { |
| 172 | Bprint(Bstderr, "malloc failed.\n"); |
| 173 | exits("malloc"); |
| 174 | } |
| 175 | if (Binit(binp, 0, OREAD) == Beof) { |
| 176 | Bprint(Bstderr, "Binit of <stdin> failed.\n"); |
| 177 | exits("Binit"); |
| 178 | } |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 179 | Binp = binp; /* &(binp->Biobufhdr); */ |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 180 | if (debug) Bprint(Bstderr, "using standard input\n"); |
| 181 | conv(Binp); |
| 182 | Bterm(Binp); |
| 183 | } |
| 184 | for (i=0; i<argc; i++) { |
| 185 | if ((binp=Bopen(argv[i], OREAD)) == 0) { |
| 186 | Bprint(Bstderr, "cannot open file %s\n", argv[i]); |
| 187 | continue; |
| 188 | } |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 189 | Binp = binp; /* &(binp->Biobufhdr); */ |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 190 | inputfilename = argv[i]; |
| 191 | conv(Binp); |
| 192 | Bterm(Binp); |
| 193 | } |
| 194 | Bterm(Bstdout); |
| 195 | |
| 196 | if ((ifd=open(tmpfilename, OREAD)) < 0) { |
| 197 | Bprint(Bstderr, "open of %s failed.\n", tmpfilename); |
| 198 | exits("open"); |
| 199 | } |
| 200 | |
| 201 | bstdout = galloc(0, sizeof(Biobuf), "bstdout"); |
| 202 | if (Binit(bstdout, 1, OWRITE) == Beof) { |
| 203 | Bprint(Bstderr, "Binit of <stdout> failed.\n"); |
| 204 | exits("Binit"); |
| 205 | } |
wkj | b855148 | 2004-05-16 07:54:22 +0000 | [diff] [blame] | 206 | Bstdout = bstdout; /* &(bstdout->Biobufhdr); */ |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 207 | prologues(); |
| 208 | Bflush(Bstdout); |
| 209 | tot = 0; i = 0; |
| 210 | while ((i=read(ifd, copybuf, BUFSIZ)) > 0) { |
| 211 | if (write(1, copybuf, i) != i) { |
| 212 | Bprint(Bstderr, "write error on copying from temp file.\n"); |
| 213 | exits("write"); |
| 214 | } |
| 215 | tot += i; |
| 216 | } |
| 217 | if (debug) Bprint(Bstderr, "copied %d bytes to final output i=%d\n", tot, i); |
| 218 | if (i < 0) { |
| 219 | Bprint(Bstderr, "read error on copying from temp file.\n"); |
| 220 | exits("read"); |
| 221 | } |
| 222 | finish(); |
| 223 | |
| 224 | exits(""); |
wkj | e8fb1d3 | 2004-05-17 02:24:15 +0000 | [diff] [blame^] | 225 | return 0; |
rsc | 61f5c35 | 2004-05-15 23:55:53 +0000 | [diff] [blame] | 226 | } |