|
|
1.1 root 1: /*
2: * lpf -- Line printer filter
3: */
4:
5: #include <sccs.h>
6:
7: SCCSID(@(#)lpf.c 1.4)
8:
9: #include <stdio.h>
10: #include <sgtty.h>
11: #include <signal.h>
12:
13: #define LINELN 132
14: #define EJLINE 63
15:
16: int anydone;
17: char linebuf[LINELN+2];
18: int ov;
19: char ovbuf[LINELN];
20: FILE *in = {stdin};
21: FILE *out;
22: char *ban;
23: int npages = 1;
24: char chrtab[][16];
25: int lineno;
26: struct sgttyb ttyb =
27: {
28: B9600, B9600,
29: 0, 0,
30: XTABS | ANYP
31: };
32: char obuf[BUFSIZ];
33: int onemt();
34: int avelen = 32;
35:
36: main(argc, argv)
37: char **argv;
38: {
39:
40: if ((out = fopen("/dev/lp", "w")) == NULL)
41: {
42: fprintf(stderr, "Can't open printer\n");
43: exit(1);
44: }
45: setbuf(out, obuf);
46: stty(fileno(out), &ttyb);
47: signal(SIGEMT, onemt);
48: if (argc > 2 && argv[1][0]=='-' && argv[1][1]=='b')
49: {
50: argc -= 2;
51: banner(ban = argv[2]);
52: argv += 2;
53: }
54: if (argc<=1)
55: {
56: anydone |= send();
57: if (lineno > 0)
58: {
59: fprintf(out, "\014");
60: }
61: } else while (argc>1)
62: {
63: if ((in = fopen(argv[1], "r")) == NULL)
64: {
65: fprintf(stderr, "Can't find %s\n", argv[1]);
66: argv++;
67: argc--;
68: anydone |= 01;
69: continue;
70: }
71: anydone |= send();
72: argc--;
73: argv++;
74: fclose(in);
75: /* if (argc > 1 || lineno != 0) */
76: fprintf(out, "\014");
77: }
78: fflush(out);
79: stty(fileno(out), &ttyb);
80: if (anydone==0)
81: exit(1);
82: if (ferror(out))
83: {
84: fprintf(out, "Printer IO error\n");
85: exit(1);
86: }
87: fclose(out);
88: if (ban && access("/usr/adm/lpacct", 02)>=0
89: && (out = fopen("/usr/adm/lpacct", "a"))!=NULL)
90: {
91: fprintf(out, "%4d %s\n", npages, ban);
92: }
93: return(0);
94: }
95:
96: send()
97: {
98: register int nskipped;
99:
100: lineno = 0;
101: nskipped = 0;
102: while (getline())
103: {
104: if (lineno==0 && linebuf[0]==0 && nskipped<3)
105: {
106: nskipped ++;
107: continue;
108: }
109: if (lineno >= EJLINE)
110: {
111: nskipped = 0;
112: putline(1);
113: lineno = 0;
114: } else
115: {
116: putline(0);
117: lineno++;
118: }
119: }
120: if (lineno>0)
121: npages++;
122: return(1);
123: }
124:
125: getline()
126: {
127: register int col, maxcol, c;
128:
129: ov = 0;
130: for (col=0; col<LINELN; col++)
131: {
132: linebuf[col] = ' ';
133: ovbuf[col] = 0;
134: }
135: col = 0;
136: maxcol = 0;
137: for (;;) switch (c = getc(in))
138: {
139:
140: case EOF:
141: return(0);
142:
143: default:
144: if (c>=' ')
145: {
146: if (col < LINELN)
147: {
148: if (linebuf[col]=='_')
149: {
150: ov++;
151: ovbuf[col] = '_';
152: }
153: linebuf[col++] = c;
154: if (col > maxcol)
155: maxcol = col;
156: }
157: }
158: continue;
159:
160: case '\f':
161: lineno = EJLINE;
162: continue;
163:
164: case ' ':
165: col++;
166: continue;
167:
168: case '\t':
169: col = (col|07) + 1;
170: if (col>maxcol)
171: maxcol = col;
172: continue;
173:
174: case '\r':
175: col = 0;
176: continue;
177:
178: case '_':
179: if (col>=LINELN)
180: {
181: col++;
182: continue;
183: }
184: if (linebuf[col]!=' ')
185: {
186: ovbuf[col] = '_';
187: ov++;
188: } else
189: linebuf[col] = c;
190: col++;
191: if (col>maxcol)
192: maxcol = col;
193: continue;
194:
195: case '\n':
196: if (maxcol>=LINELN)
197: maxcol = LINELN;
198: linebuf[maxcol] = 0;
199: return(1);
200:
201: case '\b':
202: if (col>0)
203: col--;
204: continue;
205: }
206: }
207:
208: putline(ff)
209: {
210: register char *lp, *ep;
211: register int c;
212: extern errno;
213: int i, j;
214:
215: errno = 0;
216: lp = linebuf;
217: while (c = *lp++)
218: putc(c, out);
219: if (lp != linebuf+1) {
220: avelen = (9 * avelen + (lp - linebuf)) / 10;
221: if (avelen < 32) {
222: for (i = 0; i < 30; i++)
223: putc(' ', out);
224: avelen += 3;
225: }
226: }
227: if (ov)
228: {
229:
230: for (ep= &ovbuf[LINELN-1]; *ep == 0; ep--)
231: continue;
232: putc('\r', out);
233: for (lp=ovbuf; lp <= ep; lp++)
234: putc(*lp ? *lp : ' ', out);
235: }
236: if (ff)
237: {
238: putc('\014', out);
239: npages++;
240: }
241: else
242: {
243: putc('\n', out);
244: }
245: if (ferror(out))
246: {
247: printf("Printer IO error\n");
248: exit(1);
249: }
250: }
251:
252: banner(s)
253: char *s;
254: {
255: long timeb;
256: register char *sp;
257: int i, j, t;
258:
259: for (i = 0; i < 8; i++)
260: {
261: putc ('\n', out);
262: }
263: for (i=0; i<16; i++)
264: {
265: fprintf(out, " ");
266: for (sp=s; *sp; sp++)
267: {
268: if (*sp<=' '|| *sp >'}')
269: continue;
270: fprintf(out, " ");
271: t = chrtab[*sp - ' '][i];
272: for (j=7; j>=0; j--)
273: if ((t>>j) & 01)
274: putc('X', out);
275: else
276: putc(' ', out);
277: }
278: putc('\n', out);
279: }
280: for (i = 0; i < 8; i++)
281: {
282: putc ('\n', out);
283: }
284: fprintf(out, " ");
285: fprintf(out, (time(&timeb), ctime(&timeb)));
286: fprintf(out, "\014");
287: }
288:
289: onemt()
290: {
291:
292: fprintf(out, "\014");
293: exit(0);
294: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.