tr2post.c 5.2 KB

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