2 * Copyright (c) 1990,1995 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
7 * PostScript Filter, psf.
9 * Handles both PostScript files and text files. Files with the
10 * '%!' PostScript header are sent directly to the printer,
11 * unmodified. Text files are first converted to PostScript,
12 * then sent. Printers may be directly attached or on an AppleTalk
13 * network. Other media are possible. Currently, psf invokes
14 * pap to send files to AppleTalk-ed printers. Replace the pap*
15 * variables to use another program for communication. psf only
16 * converts plain-text. If called as "tf" or "df", psf will invoke
17 * a troff or dvi to PostScript converter.
23 #include <sys/types.h>
26 #include <sys/syslog.h>
27 #include <atalk/paths.h>
33 char psapath[] = _PATH_PSA;
34 char *psaargv[] = { "psa", 0, 0, 0, 0 };
37 * If we're not doing accounting, we just call pap as below.
38 * If we are doing accounting, we call pap twice. The first time,
39 * we call it with "-E" in arg 2, pagecount.ps in arg 3, and "-" in
40 * arg 4. The second time, we call it with "-c" in arg 2, pagecount.ps
41 * in arg 3, and 0 in arg 4.
43 char pappath[] = _PATH_PAP;
44 char *papargv[] = { "pap", "-sstatus", 0, 0, 0, 0, 0, 0 };
46 char revpath[] = _PATH_PSORDER;
47 char *revargv[] = { "psorder", "-d", 0 };
49 char *filtargv[] = { 0, 0, 0 };
51 char inbuf[ 1024 * 8 ];
54 FILE *acctfile = NULL;
56 int width = 80, length = 66, indent = 0;
57 char *prog, *name, *host;
65 { 80, 66, 8.5, 11.0 }, /* US Letter */
66 { 80, 70, 8.27, 11.69 }, /* A4 */
73 int c, rc, children = 0;
75 int psafileno, multiconn = 0, waitidle = 0, waitidle2 = 0;
79 extern int optind, opterr;
82 if (( prog = rindex( av[ 0 ], '/' )) == NULL ) {
88 openlog( prog, LOG_PID );
90 openlog( prog, LOG_PID, LOG_LPR );
93 while (( c = getopt( ac, av, "P:C:D:F:L:J:x:y:n:h:w:l:i:c" )) != EOF ) {
104 width = atoi( optarg );
107 * Some version of lpd pass 0 for the page width.
116 length = atoi( optarg );
120 indent = atoi( optarg );
123 case 'c' : /* Print control chars */
137 syslog( LOG_ERR, "bad option: %c", c );
142 if ( ac - optind > 1 ) {
143 syslog( LOG_ERR, "Too many arguments" );
147 if ( index( prog, 'w' )) {
150 if ( index( prog, 'W' )) {
153 if ( index( prog, 'm' )) {
158 syslog( LOG_INFO, "starting for %s", name ? name : "?" );
161 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) < 0 ) {
162 syslog( LOG_ERR, "read: %m" );
165 if ( inlen == 0 ) { /* nothing to be done */
166 syslog( LOG_INFO, "done" );
171 * If we've been given an accounting file, start the accounting
175 /* build arguments */
176 psaargv[ 1 ] = av[ optind ];
179 if (( c = pexecv( psapath, psaargv )) < 0 ) {
180 syslog( LOG_ERR, "%s: %m", psapath );
184 syslog( LOG_INFO, "accounting with psa[%d]", c );
188 * Check prog's name to decide what programs to execute.
190 if ( strstr( prog, "pap" ) != NULL ) {
191 if ( optind < ac ) { /* accounting */
194 psafileno = getdtablesize();
196 dup2( 1, psafileno );
202 papargv[ 5 ] = _PATH_PAGECOUNT;
205 } else if ( waitidle ) {
209 papargv[ 5 ] = _PATH_PAGECOUNT;
215 papargv[ 4 ] = _PATH_PAGECOUNT;
221 * This is how it should be done.
224 papargv[ 3 ] = _PATH_PAGECOUNT;
226 papargv[ 5 ] = _PATH_PAGECOUNT;
236 if (( c = pexecv( pappath, papargv )) < 0 ) {
237 syslog( LOG_ERR, "%s: %m", pappath );
241 syslog( LOG_INFO, "sending to pap[%d]", c );
245 * Might be a good idea to have both a "forw" and a "rev", so that
246 * reversed documents can be reordered for the printing device.
248 if ( strstr( prog, "rev" ) != NULL ) {
249 if (( c = pexecv( revpath, revargv )) < 0 ) {
250 syslog( LOG_ERR, "%s: %m", revpath );
253 syslog( LOG_INFO, "sending to rev[%d]", c );
258 * Invoke an external (script) filter to produce PostScript from
261 if ( *prog != 'i' && *prog != 'o' && *( prog + 1 ) == 'f' ) {
262 filtargv[ 0 ] = filtargv[ 1 ] = prog;
263 if (( c = pexecv( _PATH_PSFILTER, filtargv )) < 0 ) {
264 syslog( LOG_ERR, "%s: %m", _PATH_PSFILTER );
267 syslog( LOG_INFO, "external filter[%d]", c );
269 rc = copyio(); /* external filter */
271 if ( inlen >= 2 && inbuf[ 0 ] == '%' && inbuf[ 1 ] == '!' ) {
272 syslog( LOG_INFO, "PostScript" );
273 rc = copyio(); /* PostScript */
274 } else if ( inlen >= 2 && inbuf[ 0 ] == '\033' && inbuf[ 1 ] == '%' ) {
275 syslog( LOG_INFO, "PostScript w/PJL" );
276 rc = copyio(); /* PostScript */
278 syslog( LOG_INFO, "straight text" );
279 rc = textps(); /* straight text */
284 if ( strstr( prog, "pap" ) != NULL && optind < ac && multiconn ) {
285 dup2( psafileno, 1 );
290 papargv[ 4 ] = _PATH_PAGECOUNT;
292 } else if ( waitidle ) {
294 papargv[ 4 ] = _PATH_PAGECOUNT;
297 papargv[ 3 ] = _PATH_PAGECOUNT;
301 if (( c = pexecv( pappath, papargv )) < 0 ) {
302 syslog( LOG_ERR, "%s: %m", pappath );
306 syslog( LOG_INFO, "pagecount with pap[%d]", c );
314 if (( c = wait3( &status, 0, 0 )) < 0 ) {
315 syslog( LOG_ERR, "wait3: %m" );
318 if ( WIFEXITED( status )) {
320 #define WEXITSTATUS(x) ((x).w_status)
322 if ( WEXITSTATUS( status ) != 0 ) {
323 syslog( LOG_ERR, "%d died with %d", c, WEXITSTATUS( status ));
324 exit( WEXITSTATUS( status ));
326 syslog( LOG_INFO, "%d done", c );
330 syslog( LOG_ERR, "%d died badly", c );
336 syslog( LOG_INFO, "pausing" );
337 kill( getpid(), SIGSTOP );
338 syslog( LOG_INFO, "restarting" );
342 syslog( LOG_INFO, "done" );
348 /* implement the FSM needed to do the suspend. Note that
349 * the last characters will be \031\001 so don't worry
350 * Fun things: 1. \031\001 should not be written to output device
351 * 2. The \031 can be last char of one read, \001 first of next
352 * - we need to write \031 if not followed by \001
361 * First, \031 and \001 *must* be the last things in the buffer
362 * (\001 can be the first thing in the next buffer). There's no
363 * need to scan any of the intervening bytes. Second, if there's
364 * more input, the escape sequence was bogus, and we should keep
369 if ( inbuf[ 0 ] == '\001' ) {
373 if ( write( 1, "\031", 1 ) != 1 ) {
374 syslog( LOG_ERR, "write: %m" );
380 if ( inbuf[ 0 ] == '\031' ) {
386 if ( write( 1, "\031", 1 ) != 1 ) {
387 syslog( LOG_ERR, "write: %m" );
392 if ( inbuf[ inlen - 2 ] == '\031' &&
393 inbuf[ inlen - 1 ] == '\001' ) {
395 } else if ( inbuf[ inlen - 1 ] == '\031' ) {
401 if (( inlen > 0 ) && ( write( 1, inbuf, inlen ) != inlen )) {
402 syslog( LOG_ERR, "write: %m" );
408 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
415 if ( select( 1, &fdset, NULL, NULL, &tv ) != 0 ) {
416 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 ) {
423 syslog( LOG_ERR, "read: %m" );
428 if ( write( 1, "\031", 1 ) != 1 ) {
429 syslog( LOG_ERR, "write: %m" );
432 } else if ( ctl == 2 ) {
438 char *font = "Courier";
442 /GSV save def % global VM\n\
444 /SV save def % save vmstate\n\
445 dup /H exch def % save font height\n\
446 exch findfont exch scalefont setfont % select font\n\
447 ( ) stringwidth pop /W exch def % save font width\n\
448 0.5 sub 72 mul /CY exch def % save start Y\n\
449 pop 0.5 add 72 mul /CX exch def % save start X\n\
450 CX CY moveto % make current point\n\
453 /NL { CX CY H sub dup /CY exch def moveto } bind def\n\
454 /CR { CX CY moveto } bind def\n\
455 /B { W neg 0 rmoveto}bind def\n\
456 /T { W mul 0 rmoveto}bind def\n\
457 /EP { SV restore showpage } bind def\n\
462 struct papersize papersize;
463 int state = 0, line = 0, col = 0, npages = 0, rc, i;
466 #define elements(x) (sizeof(x)/sizeof((x)[0]))
467 for ( i = 0; i < elements( papersizes ); i++ ) {
468 if ( width == papersizes[ 0 ].width &&
469 length == papersizes[ 0 ].length ) {
470 papersize = papersizes[ i ];
474 if ( i >= elements( papersizes )) {
475 papersize = papersizes[ 0 ]; /* default */
478 #define ST_AVAIL (1<<0)
479 #define ST_CONTROL (1<<1)
480 #define ST_PAGE (1<<2)
482 * convert text lines to postscript.
483 * A grungy little state machine. If I was more creative, I could
484 * probably think of a better way of doing this...
490 if (( state & ST_PAGE ) == 0 && *p != '\031' && *p != '\001' ) {
492 printf( "%%!PS-Adobe-2.0\n%%%%Pages: (atend)\n" );
493 printf( "%%%%DocumentFonts: %s\n", font );
496 /* output postscript prologue: */
497 if ( write( 1, pspro, sizeof( pspro ) - 1 ) !=
498 sizeof( pspro ) - 1 ) {
499 syslog( LOG_ERR, "write prologue: %m" );
502 if ( name && host ) {
503 printf( "statusdict /jobname (%s@%s) put\n", name,
508 printf( "%%%%Page: ? %d\n", ++npages );
509 printf( "%d %f %f /%s %d SP\n", indent,
510 papersize.win, papersize.lin, font, point );
513 if ( state & ST_CONTROL && *p != '\001' ) {
515 fprintf( stderr, "unprintable character (0x%x)!\n",
516 (unsigned char)031 );
517 return( 2 ); /* Toss job */
519 printf( "\\%o", (unsigned char)031 );
520 state &= ~ST_CONTROL;
525 case '\n' : /* end of line */
526 if ( state & ST_AVAIL ) {
533 if ( line >= length ) {
540 case '\r' : /* carriage return (for overtyping) */
541 if ( state & ST_AVAIL ) {
548 case '\f' : /* form feed */
549 if ( state & ST_AVAIL ) {
559 case '\b' : /* backspace */
560 /* show line, back up one character */
561 if ( state & ST_AVAIL ) {
569 case '\t' : /* tab */
570 if ( state & ST_AVAIL ) {
574 printf( "%d T\n", 8 - ( col % 8 ));
575 col += 8 - ( col % 8 );
579 * beginning of lpr control sequence
585 case '\001' : /* lpr control sequence */
586 if ( state & ST_CONTROL ) {
595 if (( state & ST_AVAIL ) == 0 ) {
603 if (( state & ST_AVAIL ) == 0 ) {
607 if ( !isascii( *p ) || !isprint( *p )) {
609 fprintf( stderr, "unprintable character (0x%x)!\n",
611 return( 2 ); /* Toss job */
613 printf( "\\%o", (unsigned char)*p );
622 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
624 syslog( LOG_ERR, "read: %m" );
630 if ( state & ST_AVAIL ) {
635 if ( state & ST_PAGE ) {
641 printf( "%%%%Trailer\nGSV restore\n%%%%Pages: %d\n%%%%EOF\n", npages );
649 * Interface to pipe and exec, for starting children in pipelines.
651 * Manipulates file descriptors 0, 1, and 2, such that the new child
652 * is reading from the parent's output.
659 if ( pipe( fd ) < 0 ) {
663 switch ( c = fork()) {
669 if ( close( fd[ 1 ] ) < 0 ) {
672 if ( dup2( fd[ 0 ], 0 ) < 0 ) {
675 if ( close( fd[ 0 ] ) < 0 ) {
683 if ( close( fd[ 0 ] ) < 0 ) {
686 if ( dup2( fd[ 1 ], 1 ) < 0 ) {
689 if ( close( fd[ 1 ] ) < 0 ) {