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