fixes q order problems and temporary file names when inchar > 'z'.
[unix-history] / usr / src / usr.sbin / lpr / filters / lpf.c
index 0f5c6fd..0149fd1 100644 (file)
@@ -1,4 +1,4 @@
-/*             lpf.c   4.5     83/02/10
+/*             lpf.c   4.7     83/03/07
  *     filter which reads the output of nroff and converts lines
  *     with ^H's to overwritten lines.  Thus this works like 'ul'
  *     but is much better: it can handle more than 2 overwrites
  *     filter which reads the output of nroff and converts lines
  *     with ^H's to overwritten lines.  Thus this works like 'ul'
  *     but is much better: it can handle more than 2 overwrites
 
 char   buf[MAXREP][MAXWIDTH];
 int    maxcol[MAXREP] = {-1};
 
 char   buf[MAXREP][MAXWIDTH];
 int    maxcol[MAXREP] = {-1};
+int    lineno;
+int    width = 132;    /* default line length */
+int    length = 66;    /* page length */
+int    npages = 1;
+int    literal;        /* print control characters */
+char   *name;          /* user's login name */
+char   *host;          /* user's machine name */
+char   *acctfile;      /* accounting information file */
 
 
-main()
+main(argc, argv)
+int argc;
+char *argv[];
 {
        register FILE *p = stdin, *o = stdout;
        register int i, col;
 {
        register FILE *p = stdin, *o = stdout;
        register int i, col;
@@ -23,6 +33,36 @@ main()
        int done, linedone, maxrep;
        char ch, *limit;
 
        int done, linedone, maxrep;
        char ch, *limit;
 
+       while (--argc) {
+               if (*(cp = *++argv) == '-') {
+                       switch (cp[1]) {
+                       case 'n':
+                               argc--;
+                               name = *++argv;
+                               break;
+
+                       case 'h':
+                               argc--;
+                               host = *++argv;
+                               break;
+
+                       case 'w':
+                               if ((i = atoi(&cp[2])) > 0 && i <= MAXWIDTH)
+                                       width = i;
+                               break;
+
+                       case 'l':
+                               length = atoi(&cp[2]);
+                               break;
+
+                       case 'c':       /* Print control chars */
+                               literal++;
+                               break;
+                       }
+               } else
+                       acctfile = cp;
+       }
+
        for (cp = buf[0], limit = buf[MAXREP]; cp < limit; *cp++ = ' ');
        done = 0;
        
        for (cp = buf[0], limit = buf[MAXREP]; cp < limit; *cp++ = ' ');
        done = 0;
        
@@ -37,13 +77,13 @@ main()
                                ch = '\n';
                                break;
 
                                ch = '\n';
                                break;
 
-                       case '\031':
-                               fflush(stdout);
-                               kill(getpid(), SIGSTOP);
-                               break;
-
                        case '\f':
                        case '\f':
+                               lineno = length;
                        case '\n':
                        case '\n':
+                               if (++lineno >= length) {
+                                       npages++;
+                                       lineno = 0;
+                               }
                                linedone = 1;
                                break;
 
                                linedone = 1;
                                break;
 
@@ -60,8 +100,23 @@ main()
                                col = (col | 07) + 1;
                                break;
 
                                col = (col | 07) + 1;
                                break;
 
+                       case '\031':
+                               /*
+                                * lpd needs to use a different filter to
+                                * print data so stop what we are doing and
+                                * wait for lpd to restart us.
+                                */
+                               if ((ch = getchar()) == '\1') {
+                                       fflush(stdout);
+                                       kill(getpid(), SIGSTOP);
+                                       break;
+                               } else {
+                                       ungetc(ch, stdin);
+                                       ch = '\031';
+                               }
+
                        default:
                        default:
-                               if (col >= MAXWIDTH)
+                               if (col >= width || !literal && ch < ' ')
                                        break;
                                cp = &buf[0][col];
                                for (i = 0; i < MAXREP; i++) {
                                        break;
                                cp = &buf[0][col];
                                for (i = 0; i < MAXREP; i++) {
@@ -93,4 +148,13 @@ main()
                        maxcol[i] = -1;
                }
        }
                        maxcol[i] = -1;
                }
        }
+       if (lineno) {           /* be sure to end on a page boundary */
+               putchar('\f');
+               npages++;
+       }
+       if (name && acctfile && access(acctfile, 02) >= 0 &&
+           freopen(acctfile, "a", stdout) != NULL) {
+               printf("%7.2f\t%s:%s\n", (float)npages, host, name);
+       }
+       exit(0);
 }
 }