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