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