cleaned up input (testing for EOF)
[unix-history] / usr / src / local / ditroff / ditroff.old.okeeffe / driver / dvar.c
index d1167ca..ccf9510 100644 (file)
@@ -1,11 +1,11 @@
-/*     dvar.c  1.3     83/07/29
+/*     dvar.c  1.11    84/03/14
  *
  * Varian driver for the new troff
  *
  * Authors:    BWK(BELL)
  *             VCAT(berkley)
  *             Richard L. Hyde, Perdue University
  *
  * Varian driver for the new troff
  *
  * Authors:    BWK(BELL)
  *             VCAT(berkley)
  *             Richard L. Hyde, Perdue University
- *             and David Slattengren, Berkeley
+ *             and David Slattengren, U.C. Berkeley
  */
 
  
  */
 
  
@@ -60,22 +60,27 @@ x ..\n      device control functions:
 #include "dev.h"
 
 
 #include "dev.h"
 
 
-#define DEBUGABLE              /* Yes, debugable... */
-#define        NFONTS  25              /* total number of fonts useable */
+/* #define DEBUGABLE           /* No, not debugable... */
+#define DRIVER                 /* Yes, we're driving directly */
+/* #define FULLPAGE            /* No, don't output full pages */
+#define        NFONTS  60              /* total number of fonts useable */
 #define        MAXSTATE 6              /* number of environments rememberable */
 #define OPENREAD 0             /* mode for openning files */
 #define RESTART        1               /* upon exit, return either RESTART */
 #define ABORT  2               /*     or ABORT */
 #define        FATAL   1               /* type of error */
 #define        BMASK   0377            /* byte grabber */
 #define        MAXSTATE 6              /* number of environments rememberable */
 #define OPENREAD 0             /* mode for openning files */
 #define RESTART        1               /* upon exit, return either RESTART */
 #define ABORT  2               /*     or ABORT */
 #define        FATAL   1               /* type of error */
 #define        BMASK   0377            /* byte grabber */
-#define FONTDIR        "/usr/lib/font" /* default place to look for fonts */
+#ifndef FONTDIR
+#define FONTDIR        "/usr/lib/font" /* default place to find font descriptions */
+#endif
+#define BITDIR "/usr/lib/vfont" /* default place to look for font rasters */
 #define MAXWRIT 4096           /* max characters allowed to write at once */
 
 #define  hmot(n)       hgoto(hpos + n)
 #define  vmot(n)       vgoto(vpos + n)
 
 
 #define MAXWRIT 4096           /* max characters allowed to write at once */
 
 #define  hmot(n)       hgoto(hpos + n)
 #define  vmot(n)       vgoto(vpos + n)
 
 
-char   SccsId[]= "dvar.c       1.3     83/07/29";
+char   SccsId[]= "dvar.c       1.11    84/03/14";
 
 int    output  = 0;    /* do we do output at all? */
 int    nolist  = 0;    /* output page list if > 0 */
 
 int    output  = 0;    /* do we do output at all? */
 int    nolist  = 0;    /* output page list if > 0 */
@@ -84,31 +89,32 @@ int spage   = 9999; /* stop every spage pages */
 int    scount  = 0;
 struct dev     dev;
 struct font    *fontbase[NFONTS+1];
 int    scount  = 0;
 struct dev     dev;
 struct font    *fontbase[NFONTS+1];
-short  *pstab;         /* point size table pointer */
-int    res;            /* input was computed according to this resolution */
+short *        pstab;          /* point size table pointer */
 int    nsizes;         /* number of sizes device is capable of printing */
 int    nfonts;         /* number of fonts device is capable of printing */
 int    nsizes;         /* number of sizes device is capable of printing */
 int    nfonts;         /* number of fonts device is capable of printing */
-int    smnt;           /* index of first special font */
 int    nchtab;
 int    nchtab;
-char   *chname;
-short  *chtab;
-char   *fitab[NFONTS+1];
-char   *widtab[NFONTS+1];
-char   *codetab[NFONTS+1];     /* device codes */
-char   *fontdir = FONTDIR;
+char * chname;
+short *        chtab;
+char * fitab[NFONTS+1];        /* font inclusion table - maps ascii to ch # */
+char * widtab[NFONTS+1];       /* width table for each font */
+char * codetab[NFONTS+1];      /* device codes */
+char * fontdir = FONTDIR;      /* place to find devxxx directories */
+char * bitdir = BITDIR;        /* place to find raster fonts and fontmap */
+char * fontname[NFONTS+1];     /* table of what font is on what position */
 struct {                       /* table of what font */
 struct {                       /* table of what font */
-       char *name;             /*   name is on what */
-       int number;             /*   position in font tables */
-} fontname[NFONTS+1];
+       char fname[3];          /*   name maps to what */
+       char *ffile;            /*   filename in bitdirectory */
+} fontmap[NFONTS+1];
+
 
 #ifdef DEBUGABLE
 int    dbg     = 0;
 #endif
 
 #ifdef DEBUGABLE
 int    dbg     = 0;
 #endif
-int    size    = -1;   /* current point size being use */
+int    size    = -1;   /* current point size (internal pstable index) */
 int    font    = -1;   /* current font - not using any to start with */
 int    hpos;           /* horizontal position we are to be at next; left = 0 */
 int    vpos;           /* current vertical position (down positive) */
 int    font    = -1;   /* current font - not using any to start with */
 int    hpos;           /* horizontal position we are to be at next; left = 0 */
 int    vpos;           /* current vertical position (down positive) */
-extern linethickness;  /* thickness, in pixels, of any drawn object */
+extern linethickness;  /* thickness (in pixels) of any drawn object */
 extern linmod;         /* line style (a bit mask - dotted, etc.) of objects */
 int    lastw;          /* width of last character printed */
 
 extern linmod;         /* line style (a bit mask - dotted, etc.) of objects */
 int    lastw;          /* width of last character printed */
 
@@ -117,23 +123,33 @@ int       lastw;          /* width of last character printed */
 #define CHARMASK       (DISPATCHSIZE-1)
 #define DSIZ           ((sizeof *dispatch)*DISPATCHSIZE)
 #define OUTFILE        fileno (stdout)
 #define CHARMASK       (DISPATCHSIZE-1)
 #define DSIZ           ((sizeof *dispatch)*DISPATCHSIZE)
 #define OUTFILE        fileno (stdout)
-#define        RES             200             /* resolution of the device */
-#define        TRAILER         (10*RES+RES/2)  /* position of trailer */
+#define        RES             200             /* resolution of the device (dots/in) */
 
 #define RASTER_LENGTH  2112                    /* device line length */
 #define BYTES_PER_LINE (RASTER_LENGTH/8)
 
 #define RASTER_LENGTH  2112                    /* device line length */
 #define BYTES_PER_LINE (RASTER_LENGTH/8)
-#define NLINES         1700                    /* page width, 8.5 inches */
+#ifndef FULLPAGE
+#      define NLINES   1600                    /* page width, 8 inches */
+#endif
+#ifdef FULLPAGE
+#      define NLINES   1700                    /* page width, 8.5 inches */
+#endif
 #define BUFFER_SIZE    (NLINES*BYTES_PER_LINE) /* number of chars in picture */
 
 
 int    pltmode[] = { VPLOT };
 int    prtmode[] = { VPRINT };
 #define BUFFER_SIZE    (NLINES*BYTES_PER_LINE) /* number of chars in picture */
 
 
 int    pltmode[] = { VPLOT };
 int    prtmode[] = { VPRINT };
-char   buffer[BUFFER_SIZE];    /* Big line buffers  */
-char   *buf0p = &buffer[0];    /* Zero origin in circular buffer  */
+char   buffer1[BUFFER_SIZE];   /* Big line buffers  */
+char   buffer2[BUFFER_SIZE];
+char * fill = &buffer1[0];     /* Zero origin in filling buffer */
+char * empty = &buffer2[0];    /* Zero origin in emptying buffer */
+char * elevel = &buffer2[0];   /* current position in emptying buffer */
+int    emptypos = NLINES;      /* amount of emptying done (initially "done") */
+
 
 
-char   *calloc();
-char   *nalloc();
-char   *allpanic();
+char * calloc();
+char * nalloc();
+char * allpanic();
+char * operand();
 
 struct header {
        short   magic;
 
 struct header {
        short   magic;
@@ -154,14 +170,11 @@ struct    dispatch{
 };
 
 struct fontdes {
 };
 
 struct fontdes {
-       int     fnum;
+       int     fnum;           /* if == -1, then this position is empty */
        int     psize;
        struct  dispatch *disp;
        char    *bits;
        int     psize;
        struct  dispatch *disp;
        char    *bits;
-} fontdes[NFONTS] = {
-       -1,
-       -1
-};
+} fontdes[NFONTS+1];           /* initialized at program start */
 
 struct dispatch *dispatch;
 int    cfnum = -1;
 
 struct dispatch *dispatch;
 int    cfnum = -1;
@@ -177,53 +190,76 @@ int       npsize = 10;
 main(argc, argv)
 char *argv[];
 {
 main(argc, argv)
 char *argv[];
 {
-       FILE *fp;
+       register FILE *fp;
+       register int i;
 
 
-       while (argc > 1 && argv[1][0] == '-') {
-               switch (argv[1][1]) {
-               case 'f':
+       for (i = 0; i <= NFONTS; fontdes[i++].fnum = -1);
+       while (--argc > 0 && **++argv == '-') {
+               switch ((*argv)[1]) {
                case 'F':
                case 'F':
-                       fontdir = argv[2];
-                       argv++;
-                       argc--;
+                       bitdir = operand(&argc, &argv);
+                       break;
+               case 'f':
+                       fontdir = operand(&argc, &argv);
                        break;
                case 'o':
                        break;
                case 'o':
-                       outlist(&argv[1][2]);
+                       outlist(operand(&argc, &argv));
                        break;
 #ifdef DEBUGABLE
                case 'd':
                        break;
 #ifdef DEBUGABLE
                case 'd':
-                       dbg = atoi(&argv[1][2]);
+                       dbg = atoi(operand(&argc, &argv));
                        if (dbg == 0) dbg = 1;
                        break;
 #endif
                case 's':
                        if (dbg == 0) dbg = 1;
                        break;
 #endif
                case 's':
-                       spage = atoi(&argv[1][2]);
+                       spage = atoi(operand(&argc, &argv));
                        if (spage <= 0)
                                spage = 9999;
                        break;
                }
                        if (spage <= 0)
                                spage = 9999;
                        break;
                }
-               argc--;
-               argv++;
        }
 
        }
 
-/* noversatec
+#ifdef DRIVER
        ioctl(OUTFILE, VSETSTATE, pltmode);
        ioctl(OUTFILE, VSETSTATE, pltmode);
-noversatec */
+#endif
 
 
-       if (argc <= 1)
+       if (argc < 1)
                conv(stdin);
        else
                conv(stdin);
        else
-               while (--argc > 0) {
-                       if (strcmp(*++argv, "-") == 0)
+               while (argc--) {
+                       if (strcmp(*argv, "-") == 0)
                                fp = stdin;
                        else if ((fp = fopen(*argv, "r")) == NULL)
                                error(FATAL, "can't open %s", *argv);
                        conv(fp);
                        fclose(fp);
                                fp = stdin;
                        else if ((fp = fopen(*argv, "r")) == NULL)
                                error(FATAL, "can't open %s", *argv);
                        conv(fp);
                        fclose(fp);
+                       argv++;
                }
        exit(0);
 }
 
                }
        exit(0);
 }
 
+
+/*----------------------------------------------------------------------------*
+ | Routine:    char  * operand (& argc,  & argv)
+ |
+ | Results:    returns address of the operand given with a command-line
+ |             option.  It uses either "-Xoperand" or "-X operand", whichever
+ |             is present.  The program is terminated if no option is present.
+ |
+ | Side Efct:  argc and argv are updated as necessary.
+ *----------------------------------------------------------------------------*/
+
+char *operand(argcp, argvp)
+int * argcp;
+char ***argvp;
+{
+       if ((**argvp)[2]) return(**argvp + 2); /* operand immediately follows */
+       if ((--*argcp) <= 0)                    /* no operand */
+           error (FATAL, "command-line option operand missing.\n");
+       return(*(++(*argvp)));                  /* operand next word */
+}
+
+
 outlist(s)     /* process list of page numbers to be printed */
 char *s;
 {
 outlist(s)     /* process list of page numbers to be printed */
 char *s;
 {
@@ -299,7 +335,8 @@ register FILE *fp;
                        t_text(buf);
                        break;
                case 'D':       /* draw function */
                        t_text(buf);
                        break;
                case 'D':       /* draw function */
-                       fgets(buf, sizeof(buf), fp);
+                       if (fgets(buf, sizeof(buf), fp) == NULL)
+                           error(FATAL, "unexpected end of input");;
                        switch (buf[0]) {
                        case 'l':       /* draw a line */
                            sscanf(buf+1, "%d %d", &n, &m);
                        switch (buf[0]) {
                        case 'l':       /* draw a line */
                            sscanf(buf+1, "%d %d", &n, &m);
@@ -378,25 +415,35 @@ register FILE *fp;
                        t_page(n);
                        break;
                case 'n':       /* end of line */
                        t_page(n);
                        break;
                case 'n':       /* end of line */
-                       while (getc(fp) != '\n')
-                               ;
                        t_newline();
                        t_newline();
-                       break;
+
                case '#':       /* comment */
                case '#':       /* comment */
-                       while (getc(fp) != '\n')
-                               ;
+                       do
+                               c = getc(fp);
+                       while (c != '\n' && c != EOF);
                        break;
                case 'x':       /* device control */
                        break;
                case 'x':       /* device control */
-                       devcntrl(fp);
+                       if (devcntrl(fp)) return;
                        break;
                default:
                        error(FATAL, "unknown input character %o %c", c, c);
                }
                        break;
                default:
                        error(FATAL, "unknown input character %o %c", c, c);
                }
+               if (emptypos < NLINES) {        /* for each input operation */
+                       slop_lines(1);          /* put out an output line */
+#ifdef DRIVER
+                       if (emptypos == NLINES) {
+                               ioctl(OUTFILE, VSETSTATE, prtmode);
+                               if (write(OUTFILE, "\f", 2) != 2)
+                                       exit(RESTART);
+                               ioctl(OUTFILE, VSETSTATE, pltmode);
+                       }
+#endif
+               }
        }
 }
 
        }
 }
 
-devcntrl(fp)   /* interpret device control functions */
-FILE *fp;
+int devcntrl(fp)       /* interpret device control functions */
+FILE *fp;              /* returns -1 upon "stop" command */
 {
         char str[20], str1[50], buf[50];
        int c, n;
 {
         char str[20], str1[50], buf[50];
        int c, n;
@@ -408,16 +455,16 @@ FILE *fp;
                t_init();
                break;
        case 't':       /* trailer */
                t_init();
                break;
        case 't':       /* trailer */
-               t_trailer();
                break;
        case 'p':       /* pause -- can restart */
                t_reset('p');
                break;
        case 's':       /* stop */
                t_reset('s');
                break;
        case 'p':       /* pause -- can restart */
                t_reset('p');
                break;
        case 's':       /* stop */
                t_reset('s');
-               break;
+               return -1;
        case 'r':       /* resolution assumed when prepared */
        case 'r':       /* resolution assumed when prepared */
-               fscanf(fp, "%d", &res);
+               fscanf(fp, "%d", &n);
+               if (n!=RES) error(FATAL,"Input computed with wrong resolution");
                break;
        case 'f':       /* font used */
                fscanf(fp, "%d %s", &n, str);
                break;
        case 'f':       /* font used */
                fscanf(fp, "%d %s", &n, str);
@@ -439,27 +486,63 @@ FILE *fp;
        }
        while ((c = getc(fp)) != '\n')  /* skip rest of input line */
                if (c == EOF)
        }
        while ((c = getc(fp)) != '\n')  /* skip rest of input line */
                if (c == EOF)
-                       break;
+                       return -1;
+       return 0;
 }
 
 /* fileinit:   read in font and code files, etc.
                Must open table for device, read in resolution,
 }
 
 /* fileinit:   read in font and code files, etc.
                Must open table for device, read in resolution,
-               size info, font info, etc. and set params
+               size info, font info, etc. and set params.
+               Also read in font name mapping.
 */
 */
+
 fileinit()
 {
 fileinit()
 {
-       int i, fin, nw;
-       char    *filebase, *p;
-       char    temp[60];
+       register int i;
+       register int fin;
+       register int nw;
+       register char *filebase;
+       register char *p;
+       register FILE *fp;
+       char    temp[100];
+
+               /* first, read in font map file.  The file must be of Format:
+                       XX  FILENAME  (XX = troff font name)
+                       with one entry per text line of the file.
+                  Extra stuff after FILENAME is ignored */
+
+       sprintf(temp, "%s/fontmap", bitdir);
+       if ((fp = fopen(temp, "r")) == NULL)
+               error(FATAL, "Can't open %s", temp);
+       for (i = 0; i <= NFONTS && fgets(temp, 100, fp) != NULL; i++) {
+               sscanf(temp, "%2s", fontmap[i].fname);
+               p = &temp[0];
+               while (*p != ' ' && *p != '     ') p++;
+               while (*p == ' ' || *p == '     ') p++;
+               filebase = p;
+               for (nw = 1; *p != '\n' && *p != ' ' && *p != '\t'; p++) nw++;
+               fontmap[i].ffile = nalloc(1, nw);
+               sscanf(filebase, "%s", fontmap[i].ffile);
+       }
+       fontmap[++i].fname[0] = '0';            /* finish off with zeros */
+       fontmap[i].ffile = (char *) 0;
+       fclose(fp);
+#ifdef DEBUGABLE
+       if(dbg) {
+           fprintf(stderr, "font map:\n");
+           for (i = 0; fontmap[i].ffile; i++)
+               fprintf(stderr,"%s = %s\n", fontmap[i].fname, fontmap[i].ffile);
+       }
+#endif
 
 
-       sprintf(temp, "%s/devvar/DESC.out", fontdir);
+       sprintf(temp, "%s/devva/DESC.out", fontdir);
        if ((fin = open(temp, 0)) < 0)
                error(FATAL, "can't open tables for %s", temp);
        read(fin, &dev, sizeof(struct dev));
        nfonts = dev.nfonts;
        nsizes = dev.nsizes;
        nchtab = dev.nchtab;
        if ((fin = open(temp, 0)) < 0)
                error(FATAL, "can't open tables for %s", temp);
        read(fin, &dev, sizeof(struct dev));
        nfonts = dev.nfonts;
        nsizes = dev.nsizes;
        nchtab = dev.nchtab;
-       filebase = calloc(1,dev.filesize);      /* enough room for whole file */
+       filebase = calloc(1, dev.filesize);     /* enough room for whole file */
        read(fin, filebase, dev.filesize);      /* all at once */
        pstab = (short *) filebase;
        chtab = pstab + nsizes + 1;
        read(fin, filebase, dev.filesize);      /* all at once */
        pstab = (short *) filebase;
        chtab = pstab + nsizes + 1;
@@ -468,9 +551,7 @@ fileinit()
        for (i = 1; i <= nfonts; i++) {
                fontbase[i] = (struct font *) p;
                nw = *p & BMASK;                /* 1st thing is width count */
        for (i = 1; i <= nfonts; i++) {
                fontbase[i] = (struct font *) p;
                nw = *p & BMASK;                /* 1st thing is width count */
-               if (smnt == 0 && fontbase[i]->specfont == 1)
-                       smnt = i;               /* first special font */
-               p += sizeof(struct font);       /* that is on the beginning */
+               p += sizeof(struct font);
                widtab[i] = p;
                codetab[i] = p + 2 * nw;
                fitab[i] = p + 3 * nw;
                widtab[i] = p;
                codetab[i] = p + 2 * nw;
                fitab[i] = p + 3 * nw;
@@ -485,8 +566,11 @@ fileinit()
        widtab[0] = (char *) fontbase[0] + sizeof (struct font);
        fontbase[0]->nwfont = 255;
        close(fin);
        widtab[0] = (char *) fontbase[0] + sizeof (struct font);
        fontbase[0]->nwfont = 255;
        close(fin);
+
 }
 
 }
 
+
+#ifdef DEBUGABLE
 fontprint(i)   /* debugging print of font i (0,...) */
 {
        int j, n;
 fontprint(i)   /* debugging print of font i (0,...) */
 {
        int j, n;
@@ -515,20 +599,41 @@ fontprint(i)      /* debugging print of font i (0,...) */
        }
        fprintf(stderr,"\n");
 }
        }
        fprintf(stderr,"\n");
 }
+#endif
+
 
 loadfont(n, s, s1)     /* load font info for font s on position n (0...) */
 int n;
 char *s, *s1;
 {
        char temp[60];
 
 loadfont(n, s, s1)     /* load font info for font s on position n (0...) */
 int n;
 char *s, *s1;
 {
        char temp[60];
-       int fin, nw, norig;
+       register int fin;
+       register int nw;
+       register int norig;
 
        if (n < 0 || n > NFONTS)
                error(FATAL, "illegal fp command %d %s", n, s);
        if (strcmp(s, fontbase[n]->namefont) == 0)
                return;
 
        if (n < 0 || n > NFONTS)
                error(FATAL, "illegal fp command %d %s", n, s);
        if (strcmp(s, fontbase[n]->namefont) == 0)
                return;
+
+       for (fin = 1; fin <= NFONTS; fin++)     /* first check to see if the */
+           if (strcmp(s, fontbase[fin]->namefont) == 0) {  /* font is loaded */
+               register char *c;                           /* somewhere else */
+
+#define ptrswap(x, y) { c = (char*) (x); x = y; y = c; }
+#define ptrfswap(x, y) { c = (char*) (x); x = y; y = (struct font *) c; }
+
+               ptrfswap(fontbase[n], fontbase[fin]);
+               ptrswap(codetab[n], codetab[fin]);
+               ptrswap(widtab[n], widtab[fin]);
+               ptrswap(fitab[n], fitab[fin]);
+               t_fp(n, fontbase[n]->namefont, fontbase[n]->intname);
+               t_fp(fin, fontbase[fin]->namefont, fontbase[fin]->intname);
+               return;
+           }
+
        if (s1 == NULL || s1[0] == '\0')
        if (s1 == NULL || s1[0] == '\0')
-               sprintf(temp, "%s/devvar/%s.out", fontdir, s);
+               sprintf(temp, "%s/devva/%s.out", fontdir, s);
        else
                sprintf(temp, "%s/%s.out", s1, s);
        if ((fin = open(temp, 0)) < 0)
        else
                sprintf(temp, "%s/%s.out", s1, s);
        if ((fin = open(temp, 0)) < 0)
@@ -555,8 +660,7 @@ error(f, s, a1, a2, a3, a4, a5, a6, a7) {
        fprintf(stderr, "dvar: ");
        fprintf(stderr, s, a1, a2, a3, a4, a5, a6, a7);
        fprintf(stderr, "\n");
        fprintf(stderr, "dvar: ");
        fprintf(stderr, s, a1, a2, a3, a4, a5, a6, a7);
        fprintf(stderr, "\n");
-       if (f)
-               exit(1);
+       if (f) exit(ABORT);
 }
 
 
 }
 
 
@@ -590,10 +694,8 @@ t_push()   /* begin a new block */
        statep->sthick = linethickness;
        statep->shpos = hpos;
        statep->svpos = vpos;
        statep->sthick = linethickness;
        statep->shpos = hpos;
        statep->svpos = vpos;
-       hpos = vpos = 0;
        if (statep++ >= state+MAXSTATE)
                error(FATAL, "{ nested too deep");
        if (statep++ >= state+MAXSTATE)
                error(FATAL, "{ nested too deep");
-       hpos = vpos = 0;
 }
 
 t_pop()        /* pop to previous state */
 }
 
 t_pop()        /* pop to previous state */
@@ -613,26 +715,26 @@ t_page(n) /* do whatever new page functions */
        int i;
 
 
        int i;
 
 
-       if (output) {
-               if (++scount >= spage) {
-                       t_reset('p');
-                       scount = 0;
-               }
-               slop_lines(NLINES);
-/* noversatec
+       if (emptypos < NLINES) {                /* finish off last page, if */
+               slop_lines(NLINES - emptypos);  /* it's not done yet */
+#ifdef DRIVER
                ioctl(OUTFILE, VSETSTATE, prtmode);
                if (write(OUTFILE, "\f", 2) != 2)
                        exit(RESTART);
                ioctl(OUTFILE, VSETSTATE, pltmode);
                ioctl(OUTFILE, VSETSTATE, prtmode);
                if (write(OUTFILE, "\f", 2) != 2)
                        exit(RESTART);
                ioctl(OUTFILE, VSETSTATE, pltmode);
-noversatec */
-               vclear(buf0p, BYTES_PER_LINE * NLINES);
-               buf0p = buffer;
+#endif
+       }
+       if (output) {
+               emptypos = 0;           /* set emptying to be started */
+               elevel = fill;          /* swap buffer pointers */
+               fill = empty;
+               empty = elevel;
        }
 
        vpos = 0;
        output = 1;
        if (nolist == 0)
        }
 
        vpos = 0;
        output = 1;
        if (nolist == 0)
-               return; /* no -o specified */
+               return;         /* no -o specified */
        output = 0;
        for (i = 0; i < nolist; i += 2)
                if (n >= olist[i] && n <= olist[i+1]) {
        output = 0;
        for (i = 0; i < nolist; i += 2)
                if (n >= olist[i] && n <= olist[i+1]) {
@@ -652,19 +754,19 @@ int n;
        int i;
 
        if (n <= pstab[0])
        int i;
 
        if (n <= pstab[0])
-               return(1);
-       else if (n >= pstab[nsizes-1])
-               return(nsizes);
+               return(0);
+       else if (n >= pstab[nsizes - 1])
+               return(nsizes - 1);
        for (i = 0; n > pstab[i]; i++)
                ;
        for (i = 0; n > pstab[i]; i++)
                ;
-       return(i+1);
+       return(i);
 }
 
 t_charht(n)    /* set character height to n */
 int n;
 {
 #ifdef DEBUGABLE
 }
 
 t_charht(n)    /* set character height to n */
 int n;
 {
 #ifdef DEBUGABLE
-       if (dbg) error(!FATAL, "can't set height on varian yet");
+       if (dbg) error(!FATAL, "can't set height on varian");
 #endif
 }
 
 #endif
 }
 
@@ -672,7 +774,7 @@ t_slant(n)  /* set slant to n */
 int n;
 {
 #ifdef DEBUGABLE
 int n;
 {
 #ifdef DEBUGABLE
-       if (dbg) error(!FATAL, "can't set slant on varian yet");
+       if (dbg) error(!FATAL, "can't set slant on varian");
 #endif
 }
 
 #endif
 }
 
@@ -719,37 +821,21 @@ char *s;
        }
 }
 
        }
 }
 
+
 t_reset(c)
 {      
 t_reset(c)
 {      
-       output = 1;
-       switch(c){
-       case 'p':
-               slop_lines(NLINES);
-               buf0p = buffer;
-               break;
-       case 's':
-               slop_lines(NLINES);
-               t_done();
-               break; /* no Return */
+       if (c == 's') {
+               t_page();
+               output = 0;
+               t_page();
+#ifdef DRIVER
+               ioctl(OUTFILE, VSETSTATE, prtmode);
+               if (write(OUTFILE, "\f", 2) != 2)
+                       exit(RESTART);
+#endif
        }
 }
 
        }
 }
 
-t_done()       /* clean up and get ready to die */
-{
-/* noversatec
-       ioctl(OUTFILE, VSETSTATE, prtmode);
-       if (write(OUTFILE, "\f", 2) != 2)
-               exit(RESTART);
-noversatec */
-}
-
-t_trailer()
-{
-       vpos = 0;
-       vgoto(TRAILER);
-       vpos = 0;
-}
-
 
 /*----------------------------------------------------------------------------*
  | Routine:    hgoto (horizontal_spot)
 
 /*----------------------------------------------------------------------------*
  | Routine:    hgoto (horizontal_spot)
@@ -818,7 +904,7 @@ int c;
 #ifdef DEBUGABLE
                if (dbg) fprintf(stderr,"non-exist 0%o\n", c + 32);
 #endif
 #ifdef DEBUGABLE
                if (dbg) fprintf(stderr,"non-exist 0%o\n", c + 32);
 #endif
-               lastw = (widtab[font][0] * pstab[size-1] + dev.unitwidth/2)
+               lastw = (widtab[font][0] * pstab[size] + dev.unitwidth/2)
                                                                / dev.unitwidth;
                return;
        }
                                                                / dev.unitwidth;
                return;
        }
@@ -827,11 +913,11 @@ int c;
        if (i != 0) {                   /* it's on this font */
                p = codetab[font];      /* get the printing value of ch */
                pw = widtab[font];      /* get the width */
        if (i != 0) {                   /* it's on this font */
                p = codetab[font];      /* get the printing value of ch */
                pw = widtab[font];      /* get the width */
-       } else if (smnt > 0) {          /* on special (we hope) */
-               for (k=smnt, j=0; j <= nfonts; j++, k = (k+1) % (nfonts+1)){
+       } else {                /* on another font - run down the font list */
+               for (j=0; j++ <= nfonts; k = (k+1) % (nfonts+1)) {
                        if (fitab[k] == 0)
                                continue;
                        if (fitab[k] == 0)
                                continue;
-                       if ((i = fitab[k][c] & BMASK) != 0) {
+                       if ((i=fitab[k][c] & BMASK) != 0) {
                                p = codetab[k];
                                pw = widtab[k];
                                setfont(k);
                                p = codetab[k];
                                pw = widtab[k];
                                setfont(k);
@@ -839,12 +925,14 @@ int c;
                        }
                }
        }
                        }
                }
        }
-       if (i == 0 || (code = p[i] & BMASK) == 0 || k > nfonts) {
+
+       if (i == 0) {
 #ifdef DEBUGABLE
                if (dbg) fprintf(stderr,"not found 0%o\n", c+32);
 #endif
                return;
        }
 #ifdef DEBUGABLE
                if (dbg) fprintf(stderr,"not found 0%o\n", c+32);
 #endif
                return;
        }
+       code = p[i] & BMASK;
 #ifdef DEBUGABLE
        if (dbg) {
                if (isprint(c+32))
 #ifdef DEBUGABLE
        if (dbg) {
                if (isprint(c+32))
@@ -856,7 +944,7 @@ int c;
        outc(code);     /* character is < 254 */
        if (font != ofont)
                setfont(ofont);
        outc(code);     /* character is < 254 */
        if (font != ofont)
                setfont(ofont);
-       lastw = ((pw[i]&077) * pstab[size-1] + dev.unitwidth/2) / dev.unitwidth;
+       lastw = ((pw[i]&077) * pstab[size] + dev.unitwidth/2) / dev.unitwidth;
 }
 
 
 }
 
 
@@ -867,32 +955,45 @@ int n;
 
        if (n == size)
                return; /* already there */
 
        if (n == size)
                return; /* already there */
-       if (vloadfont(font,pstab[n-1]) != -1)
+       if (vloadfont(font, pstab[n]) != -1)
                size = n;
 }
 
 t_fp(n, s, si) /* font position n now contains font s, intname si */
                size = n;
 }
 
 t_fp(n, s, si) /* font position n now contains font s, intname si */
-int n;
+int n;         /* internal name is ignored */
 char *s, *si;
 {
 char *s, *si;
 {
-       register        i;
+       register int i;
 
 
-       fontname[n].name = s;
-       fontname[n].number = atoi(si);
-       for(i = 0;i < NFONTS;i++)/* free the bits of that font */
+
+                       /* first convert s to filename if possible */
+       for (i = 0; fontmap[i].ffile != (char *) 0; i++) {
+#ifdef DEBUGABLE
+               if(dbg>1)fprintf(stderr,"testing :%s:%s:\n",s,fontmap[i].fname);
+#endif
+               if (strcmp(s, fontmap[i].fname) == 0) {
+                       s = fontmap[i].ffile;
+#ifdef DEBUGABLE
+                       if(dbg)fprintf(stderr, "found :%s:\n",fontmap[i].ffile);
+#endif
+                       break;
+               }
+       }
+       fontname[n] = s;
+       for(i = 0;i <= NFONTS;i++)      /* free the bits of that font */
                if (fontdes[i].fnum == n){
                        nfree(fontdes[i].bits);
                if (fontdes[i].fnum == n){
                        nfree(fontdes[i].bits);
-                       fontdes[i].bits = 0;
                        fontdes[i].fnum = -1;
                }
 }
 
                        fontdes[i].fnum = -1;
                }
 }
 
+
 setfont(n)     /* set font to n */
 int n;
 {
        if (n < 0 || n > NFONTS)
                error(FATAL, "illegal font %d", n);
 setfont(n)     /* set font to n */
 int n;
 {
        if (n < 0 || n > NFONTS)
                error(FATAL, "illegal font %d", n);
-       if (vloadfont(n,pstab[size - 1]) != -1)
+       if (vloadfont(n,pstab[size]) != -1)
                font = n;
 }
 
                font = n;
 }
 
@@ -916,9 +1017,9 @@ register int fsize;
                }
        }
                /* this is a new font */
                }
        }
                /* this is a new font */
-       if (fnum < 0 || fnum > NFONTS || fontname[fnum].name == 0) {
+       if (fnum < 0 || fnum > NFONTS || fontname[fnum] == 0) {
            fprintf(stderr, "Internal error: illegal font %d name %s size\n",
            fprintf(stderr, "Internal error: illegal font %d name %s size\n",
-                           fontname[fnum].name,fnum,fsize);
+                           fontname[fnum], fnum, fsize);
            return(-1);
        }
                /* Need to verify the existance of that font/size here*/
            return(-1);
        }
                /* Need to verify the existance of that font/size here*/
@@ -931,20 +1032,41 @@ register int fsize;
 
 getfont()
 {
 
 getfont()
 {
-       register int fnum, fsize, fontd;
-       int d;
+       register int fnum;
+       register int fsize;
+       register int fontd;
+       register int d;
+       register int sizehunt = size;
        char cbuf[BUFSIZ];
 
        fnum = nfontnum;
        fsize = npsize;
        char cbuf[BUFSIZ];
 
        fnum = nfontnum;
        fsize = npsize;
-       sprintf(cbuf, "/usr/lib/vfont/%s.%dr",fontname[fnum].name, fsize);
-       fontd = open(cbuf, OPENREAD);
-       if (fontd == -1) {
-               perror(cbuf);
-               error(!FATAL,"fnum = %d size = %d name = %s",
-                       fnum,fsize,fontname[fnum]);
-               fontwanted = 0;
-               return (-1);
+                       /* try to open font file - if unsuccessful, hunt for */
+                       /* a file of same style, different size to substitute */
+       d = -1;  /* direction to look in pstab (smaller first) */
+       do {
+           sprintf(cbuf, "%s/%s.%dr", bitdir, fontname[fnum], fsize);
+           fontd = open(cbuf, OPENREAD);
+           if (fontd == -1) {          /* File wasn't found. Try another ps */
+               sizehunt += d;
+               if (sizehunt < 0) {     /* past beginning - look higher */
+                   d = 1;
+                   sizehunt = size + 1;
+               }
+               if (sizehunt > nsizes) {        /* past top - forget it */
+                   d = 0;
+               } else {
+                   fsize = pstab[sizehunt];
+               }
+           }
+       } while (fontd == -1 && d != 0);
+
+       if (fontd == -1) {              /* completely unsuccessful */
+           perror(cbuf);
+           error(!FATAL,"fnum = %d, psize = %d, name = %s",
+               fnum, npsize, fontname[fnum]);
+           fontwanted = 0;
+           return (-1);
        }
        if (read(fontd, &header, sizeof  (header)) != sizeof (header)
                                                || header.magic != 0436)
        }
        if (read(fontd, &header, sizeof  (header)) != sizeof (header)
                                                || header.magic != 0436)
@@ -953,8 +1075,7 @@ getfont()
                cfont = relfont();
                if ((bits=nalloc(header.size+DSIZ+1,1))== NULL)
                        if ((bits=allpanic(header.size+DSIZ+1))== NULL) {
                cfont = relfont();
                if ((bits=nalloc(header.size+DSIZ+1,1))== NULL)
                        if ((bits=allpanic(header.size+DSIZ+1))== NULL) {
-                               fprintf(stderr,"%s: ran out of memory\n", cbuf);
-                               exit(ABORT);
+                               error(FATAL,"%s: ran out of memory", cbuf);
                        }
 
                        /*
                        }
 
                        /*
@@ -993,11 +1114,10 @@ relfont()
     register int newfont;
 
     for (newfont = 0; newfont < NFONTS; newfont++)
     register int newfont;
 
     for (newfont = 0; newfont < NFONTS; newfont++)
-       if (fontdes [newfont].bits == (char *) -1  ||  !fontdes [newfont].bits)
+       if (fontdes [newfont].fnum == -1)
            break;
            break;
-    if (fontdes [newfont].bits != (char *) -1  &&  fontdes [newfont].bits) {
+    if (fontdes [newfont].fnum != -1) {
        nfree (fontdes [newfont].bits);
        nfree (fontdes [newfont].bits);
-       fontdes [newfont].bits = (char *)0;
 #ifdef DEBUGABLE
        if (dbg) fprintf (stderr, "freeing position %d\n", newfont);
     } else {
 #ifdef DEBUGABLE
        if (dbg) fprintf (stderr, "freeing position %d\n", newfont);
     } else {
@@ -1005,7 +1125,7 @@ relfont()
            fprintf (stderr, "taking, not freeing, position %d\n", newfont);
 #endif
     }
            fprintf (stderr, "taking, not freeing, position %d\n", newfont);
 #endif
     }
-    fontdes[newfont].bits = 0;
+    fontdes[newfont].fnum = -1;
     return (newfont);
 }
 
     return (newfont);
 }
 
@@ -1014,12 +1134,9 @@ int nbytes;
 {
        register int i;
 
 {
        register int i;
 
-       for (i = 0; i <= NFONTS; i++)
-           if (fontdes[i].bits != (char *)-1 && fontdes[i].bits != (char *)0)
-               nfree(fontdes[i].bits);
        for (i = 0; i <= NFONTS; i++) {
        for (i = 0; i <= NFONTS; i++) {
-               fontdes[i].fnum = fontdes[i].psize = -1;
-               fontdes[i].bits = 0;
+               if (fontdes[i].fnum != -1) nfree(fontdes[i].bits);
+               fontdes[i].fnum = -1;
                cfnum = cpsize = -1;
        }
        return(nalloc(nbytes,1));
                cfnum = cpsize = -1;
        }
        return(nalloc(nbytes,1));
@@ -1041,7 +1158,7 @@ int code;         /* character to print */
     register char *scanp;      /* ptr to output buffer */
     int scanp_inc;             /* increment to start of next buffer */
     int offset;                        /* bit offset to start of font data */
     register char *scanp;      /* ptr to output buffer */
     int scanp_inc;             /* increment to start of next buffer */
     int offset;                        /* bit offset to start of font data */
-    int i;                     /* loop counter */
+    register int i;            /* loop counter */
     register int count;                /* font data ptr */
     register unsigned fontdata;        /* font data temporary */
     register int off8;         /* offset + 8 */
     register int count;                /* font data ptr */
     register unsigned fontdata;        /* font data temporary */
     register int off8;         /* offset + 8 */
@@ -1053,18 +1170,18 @@ int code;               /* character to print */
        addr = bits + dis->addr;
        llen = (dis->up + dis->down + 7) >> 3;
        nlines = dis->right + dis->left;
        addr = bits + dis->addr;
        llen = (dis->up + dis->down + 7) >> 3;
        nlines = dis->right + dis->left;
-       scanp = buf0p + (hpos - dis->left) * BYTES_PER_LINE
-                       - (1 + ((dis->down + vpos) >> 3));
-       if (scanp < &buffer[0])
-           scanp += sizeof buffer;
+       scanp = fill + (hpos + 1 - dis->left) * BYTES_PER_LINE
+                       - (1 + ((dis->down + vpos - 1) >> 3));
+       if (scanp < fill)
+           scanp += BUFFER_SIZE;
        scanp_inc = BYTES_PER_LINE - llen;
        scanp_inc = BYTES_PER_LINE - llen;
-       off8 = ((dis->down + vpos) &07);
+       off8 = ((dis->down + vpos - 1) &07);
        offset = off8 - 8;
        for (i = 0; i < nlines; i++) {
        offset = off8 - 8;
        for (i = 0; i < nlines; i++) {
-           if (scanp >= &buffer[BUFFER_SIZE])
-               scanp -= sizeof buffer;
+           if (scanp >= fill + BUFFER_SIZE)
+               scanp -= BUFFER_SIZE;
            count = llen;
            count = llen;
-           if (scanp + count <= &buffer[BUFFER_SIZE]) {
+           if (scanp + count < fill + BUFFER_SIZE) {
                do {
                    fontdata = *(unsigned *)addr;
                    addr += 4;
                do {
                    fontdata = *(unsigned *)addr;
                    addr += 4;
@@ -1089,20 +1206,19 @@ slop_lines(nlines)
 int nlines;
 
 /* Output "nlines" lines from the buffer, and clear that section of the  */
 int nlines;
 
 /* Output "nlines" lines from the buffer, and clear that section of the  */
-/* buffer.     */
+/* buffer.     Also updates the pointers to the emptying buffer */
 
 {
        unsigned usize;
 
        usize = BYTES_PER_LINE * nlines;
 
 {
        unsigned usize;
 
        usize = BYTES_PER_LINE * nlines;
-       writev(buf0p,usize);
-       vclear(buf0p, usize);
-/* noversatec
-       ioctl(OUTFILE, VSETSTATE, pltmode);
-noversatec */
+       vwrite(elevel, usize);
+       vclear(elevel, usize);
+       elevel += usize;
+       emptypos += nlines;
 }
 
 }
 
-writev(buf,usize)
+vwrite(buf,usize)
 char *buf;
 unsigned usize;
 {
 char *buf;
 unsigned usize;
 {
@@ -1176,14 +1292,14 @@ char *cp;
 /*
  * Points should be in the range 0 <= x < RASTER_LENGTH, 0 <= y < NLINES.
  * The origin is the top left-hand corner with increasing x towards the
 /*
  * Points should be in the range 0 <= x < RASTER_LENGTH, 0 <= y < NLINES.
  * The origin is the top left-hand corner with increasing x towards the
- * right and increasing y going down.
- * The output array is NLINES x BYTES_PER_LINE pixels.
+ * right and increasing y going down.  X and Y should be sent as (0,0) being
+ * at the bottom left.  The output array is NLINES x BYTES_PER_LINE pixels.
  */
 point(x, y)
 register int x;
 register int y;
 {
     if ((unsigned)(y=RASTER_LENGTH-y) < RASTER_LENGTH && (unsigned)x < NLINES) {
  */
 point(x, y)
 register int x;
 register int y;
 {
     if ((unsigned)(y=RASTER_LENGTH-y) < RASTER_LENGTH && (unsigned)x < NLINES) {
-       buffer [x * BYTES_PER_LINE + (y >> 3)] |= 1 << (7 - (y & 07));
+       *(fill + x * BYTES_PER_LINE + (y >> 3)) |= 1 << (7 - (y & 07));
     }
 }
     }
 }