tr2post.c 5.2 KB

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