main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <event.h>
  5. #include <bio.h>
  6. #include "proof.h"
  7. Rectangle rpage = { 0, 0, 850, 1150 };
  8. char devname[64];
  9. double mag = DEFMAG;
  10. int dbg = 0;
  11. char *track = 0;
  12. Biobuf bin;
  13. char libfont[100] = "/lib/font/bit";
  14. char mapfile[100] = "MAP";
  15. char *mapname = "MAP";
  16. void
  17. usage(void)
  18. {
  19. fprint(2, "usage: proof [-m mag] [-/ nview] [-x xoff] [-y yoff] [-M mapfile] [-F fontdir] [-dt] file...\n");
  20. exits("usage");
  21. }
  22. double
  23. getnum(char *s)
  24. {
  25. if(s == nil)
  26. usage();
  27. return atof(s);
  28. }
  29. char*
  30. getstr(char *s)
  31. {
  32. if(s == nil)
  33. usage();
  34. return s;
  35. }
  36. void
  37. main(int argc, char *argv[])
  38. {
  39. char c;
  40. int dotrack = 0;
  41. ARGBEGIN{
  42. case 'm': /* magnification */
  43. mag = getnum(ARGF());
  44. if (mag < 0.1 || mag > 10){
  45. fprint(2, "ridiculous mag argument ignored\n");
  46. mag = DEFMAG;
  47. }
  48. break;
  49. case '/':
  50. nview = getnum(ARGF());
  51. if (nview < 1 || nview > MAXVIEW)
  52. nview = 1;
  53. break;
  54. case 'x':
  55. xyoffset.x += getnum(ARGF()) * 100;
  56. break;
  57. case 'y':
  58. xyoffset.y += getnum(ARGF()) * 100;
  59. break;
  60. case 'M': /* change MAP file */
  61. strcpy(mapname, getstr(ARGF()));
  62. break;
  63. case 'F': /* change /lib/font/bit directory */
  64. strcpy(libfont, getstr(ARGF()));
  65. break;
  66. case 'd':
  67. dbg = 1;
  68. break;
  69. case 't':
  70. dotrack = 1;
  71. break;
  72. default:
  73. usage();
  74. }ARGEND
  75. if (argc > 0) {
  76. close(0);
  77. if (open(argv[0], 0) != 0) {
  78. sysfatal("can't open %s: %r\n", argv[0]);
  79. exits("open failure");
  80. }
  81. if(dotrack)
  82. track = argv[0];
  83. }
  84. Binit(&bin, 0, OREAD);
  85. sprint(mapfile, "%s/%s", libfont, mapname);
  86. readmapfile(mapfile);
  87. for (c = 0; c < NFONT; c++)
  88. loadfontname(c, "??");
  89. mapscreen();
  90. clearscreen();
  91. readpage();
  92. }
  93. /*
  94. * Input buffer to allow us to back up
  95. */
  96. #define SIZE 100000 /* 8-10 pages, typically */
  97. char bufc[SIZE];
  98. char *inc = bufc; /* where next input character goes */
  99. char *outc = bufc; /* next character to be read from buffer */
  100. int off; /* position of outc in total input stream */
  101. void
  102. addc(int c)
  103. {
  104. *inc++ = c;
  105. if(inc == &bufc[SIZE])
  106. inc = &bufc[0];
  107. }
  108. int
  109. getc(void)
  110. {
  111. int c;
  112. if(outc == inc){
  113. c = Bgetc(&bin);
  114. if(c == Beof)
  115. return Beof;
  116. addc(c);
  117. }
  118. off++;
  119. c = *outc++;
  120. if(outc == &bufc[SIZE])
  121. outc = &bufc[0];
  122. return c;
  123. }
  124. int
  125. getrune(void)
  126. {
  127. int c, n;
  128. Rune r;
  129. char buf[UTFmax];
  130. for(n=0; !fullrune(buf, n); n++){
  131. c = getc();
  132. if(c == Beof)
  133. return Beof;
  134. buf[n] = c;
  135. }
  136. chartorune(&r, buf);
  137. return r;
  138. }
  139. int
  140. nbuf(void) /* return number of buffered characters */
  141. {
  142. int ini, outi;
  143. ini = inc-bufc;
  144. outi = outc-bufc;
  145. if(ini < outi)
  146. ini += SIZE;
  147. return ini-outi;
  148. }
  149. ulong
  150. seekc(ulong o)
  151. {
  152. ulong avail;
  153. long delta;
  154. delta = off-o;
  155. if(delta < 0)
  156. return Beof;
  157. avail = SIZE-nbuf();
  158. if(delta < avail){
  159. off = o;
  160. outc -= delta;
  161. if(outc < &bufc[0])
  162. outc += SIZE;
  163. return off;
  164. }
  165. return Beof;
  166. }
  167. void
  168. ungetc(void)
  169. {
  170. if(off == 0)
  171. return;
  172. if(nbuf() == SIZE){
  173. fprint(2, "backup buffer overflow\n");
  174. return;
  175. }
  176. if(outc == &bufc[0])
  177. outc = &bufc[SIZE];
  178. --outc;
  179. --off;
  180. }
  181. ulong
  182. offsetc(void)
  183. {
  184. return off;
  185. }
  186. char*
  187. rdlinec(void)
  188. {
  189. static char buf[2048];
  190. int c, i;
  191. for(i=0; i<sizeof buf; ){
  192. c = getc();
  193. if(c == Beof)
  194. break;
  195. buf[i++] = c;
  196. if(c == '\n')
  197. break;
  198. }
  199. if(i == 0)
  200. return nil;
  201. return buf;
  202. }