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.
28 #include <sys/types.h>
31 #include <sys/syslog.h>
32 #include <atalk/paths.h>
39 /* Forward Declarations */
40 int pexecv(char *path, char *argv[]);
44 char psapath[] = _PATH_PSA;
45 char *psaargv[] = { "psa", 0, 0, 0, 0 };
48 * If we're not doing accounting, we just call pap as below.
49 * If we are doing accounting, we call pap twice. The first time,
50 * we call it with "-E" in arg 2, pagecount.ps in arg 3, and "-" in
51 * arg 4. The second time, we call it with "-c" in arg 2, pagecount.ps
52 * in arg 3, and 0 in arg 4.
54 char pappath[] = _PATH_PAP;
55 char *papargv[] = { "pap", "-sstatus", 0, 0, 0, 0, 0, 0 };
57 char revpath[] = _PATH_PSORDER;
58 char *revargv[] = { "psorder", "-d", 0 };
60 char *filtargv[] = { 0, 0, 0 };
62 char inbuf[ 1024 * 8 ];
65 FILE *acctfile = NULL;
67 int width = 80, length = 66, indent = 0;
68 char *prog, *name, *host;
76 { 80, 66, 8.5, 11.0 }, /* US Letter */
77 { 80, 70, 8.27, 11.69 }, /* A4 */
84 int c, rc, children = 0;
86 int psafileno, multiconn = 0, waitidle = 0, waitidle2 = 0;
90 extern int optind, opterr;
93 if (( prog = rindex( av[ 0 ], '/' )) == NULL ) {
99 openlog( prog, LOG_PID );
101 openlog( prog, LOG_PID, LOG_LPR );
104 while (( c = getopt( ac, av, "P:C:D:F:L:J:x:y:n:h:w:l:i:c" )) != EOF ) {
115 width = atoi( optarg );
118 * Some version of lpd pass 0 for the page width.
127 length = atoi( optarg );
131 indent = atoi( optarg );
134 case 'c' : /* Print control chars */
148 syslog( LOG_ERR, "bad option: %c", c );
153 if ( ac - optind > 1 ) {
154 syslog( LOG_ERR, "Too many arguments" );
158 if ( index( prog, 'w' )) {
161 if ( index( prog, 'W' )) {
164 if ( index( prog, 'm' )) {
169 syslog( LOG_INFO, "starting for %s", name ? name : "?" );
172 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) < 0 ) {
173 syslog( LOG_ERR, "read: %m" );
176 if ( inlen == 0 ) { /* nothing to be done */
177 syslog( LOG_INFO, "done" );
182 * If we've been given an accounting file, start the accounting
186 /* build arguments */
187 psaargv[ 1 ] = av[ optind ];
190 if (( c = pexecv( psapath, psaargv )) < 0 ) {
191 syslog( LOG_ERR, "%s: %m", psapath );
195 syslog( LOG_INFO, "accounting with psa[%d]", c );
199 * Check prog's name to decide what programs to execute.
201 if ( strstr( prog, "pap" ) != NULL ) {
202 if ( optind < ac ) { /* accounting */
205 psafileno = getdtablesize();
207 dup2( 1, psafileno );
213 papargv[ 5 ] = _PATH_PAGECOUNT;
216 } else if ( waitidle ) {
220 papargv[ 5 ] = _PATH_PAGECOUNT;
226 papargv[ 4 ] = _PATH_PAGECOUNT;
232 * This is how it should be done.
235 papargv[ 3 ] = _PATH_PAGECOUNT;
237 papargv[ 5 ] = _PATH_PAGECOUNT;
247 if (( c = pexecv( pappath, papargv )) < 0 ) {
248 syslog( LOG_ERR, "%s: %m", pappath );
252 syslog( LOG_INFO, "sending to pap[%d]", c );
256 * Might be a good idea to have both a "forw" and a "rev", so that
257 * reversed documents can be reordered for the printing device.
259 if ( strstr( prog, "rev" ) != NULL ) {
260 if (( c = pexecv( revpath, revargv )) < 0 ) {
261 syslog( LOG_ERR, "%s: %m", revpath );
264 syslog( LOG_INFO, "sending to rev[%d]", c );
269 * Invoke an external (script) filter to produce PostScript from
272 if ( *prog != 'i' && *prog != 'o' && *( prog + 1 ) == 'f' ) {
273 filtargv[ 0 ] = filtargv[ 1 ] = prog;
274 if (( c = pexecv( _PATH_PSFILTER, filtargv )) < 0 ) {
275 syslog( LOG_ERR, "%s: %m", _PATH_PSFILTER );
278 syslog( LOG_INFO, "external filter[%d]", c );
280 rc = copyio(); /* external filter */
282 if ( inlen >= 2 && inbuf[ 0 ] == '%' && inbuf[ 1 ] == '!' ) {
283 syslog( LOG_INFO, "PostScript" );
284 rc = copyio(); /* PostScript */
285 } else if ( inlen >= 2 && inbuf[ 0 ] == '\033' && inbuf[ 1 ] == '%' ) {
286 syslog( LOG_INFO, "PostScript w/PJL" );
287 rc = copyio(); /* PostScript */
289 syslog( LOG_INFO, "straight text" );
290 rc = textps(); /* straight text */
295 if ( strstr( prog, "pap" ) != NULL && optind < ac && multiconn ) {
296 dup2( psafileno, 1 );
301 papargv[ 4 ] = _PATH_PAGECOUNT;
303 } else if ( waitidle ) {
305 papargv[ 4 ] = _PATH_PAGECOUNT;
308 papargv[ 3 ] = _PATH_PAGECOUNT;
312 if (( c = pexecv( pappath, papargv )) < 0 ) {
313 syslog( LOG_ERR, "%s: %m", pappath );
317 syslog( LOG_INFO, "pagecount with pap[%d]", c );
325 if (( c = wait3( &status, 0, 0 )) < 0 ) {
326 syslog( LOG_ERR, "wait3: %m" );
329 if ( WIFEXITED( status )) {
331 #define WEXITSTATUS(x) ((x).w_status)
333 if ( WEXITSTATUS( status ) != 0 ) {
334 syslog( LOG_ERR, "%d died with %d", c, WEXITSTATUS( status ));
335 exit( WEXITSTATUS( status ));
337 syslog( LOG_INFO, "%d done", c );
341 syslog( LOG_ERR, "%d died badly", c );
347 syslog( LOG_INFO, "pausing" );
348 kill( getpid(), SIGSTOP );
349 syslog( LOG_INFO, "restarting" );
353 syslog( LOG_INFO, "done" );
359 /* implement the FSM needed to do the suspend. Note that
360 * the last characters will be \031\001 so don't worry
361 * Fun things: 1. \031\001 should not be written to output device
362 * 2. The \031 can be last char of one read, \001 first of next
363 * - we need to write \031 if not followed by \001
372 * First, \031 and \001 *must* be the last things in the buffer
373 * (\001 can be the first thing in the next buffer). There's no
374 * need to scan any of the intervening bytes. Second, if there's
375 * more input, the escape sequence was bogus, and we should keep
380 if ( inbuf[ 0 ] == '\001' ) {
384 if ( write( 1, "\031", 1 ) != 1 ) {
385 syslog( LOG_ERR, "write: %m" );
391 if ( inbuf[ 0 ] == '\031' ) {
397 if ( write( 1, "\031", 1 ) != 1 ) {
398 syslog( LOG_ERR, "write: %m" );
403 if ( inbuf[ inlen - 2 ] == '\031' &&
404 inbuf[ inlen - 1 ] == '\001' ) {
406 } else if ( inbuf[ inlen - 1 ] == '\031' ) {
412 if (( inlen > 0 ) && ( write( 1, inbuf, inlen ) != inlen )) {
413 syslog( LOG_ERR, "write: %m" );
419 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
426 if ( select( 1, &fdset, NULL, NULL, &tv ) != 0 ) {
427 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 ) {
434 syslog( LOG_ERR, "read: %m" );
439 if ( write( 1, "\031", 1 ) != 1 ) {
440 syslog( LOG_ERR, "write: %m" );
443 } else if ( ctl == 2 ) {
449 char *font = "Courier";
453 /GSV save def % global VM\n\
455 /SV save def % save vmstate\n\
456 dup /H exch def % save font height\n\
457 exch findfont exch scalefont setfont % select font\n\
458 ( ) stringwidth pop /W exch def % save font width\n\
459 0.5 sub 72 mul /CY exch def % save start Y\n\
460 pop 0.5 add 72 mul /CX exch def % save start X\n\
461 CX CY moveto % make current point\n\
464 /NL { CX CY H sub dup /CY exch def moveto } bind def\n\
465 /CR { CX CY moveto } bind def\n\
466 /B { W neg 0 rmoveto}bind def\n\
467 /T { W mul 0 rmoveto}bind def\n\
468 /EP { SV restore showpage } bind def\n\
473 struct papersize papersize;
474 int state = 0, line = 0, col = 0, npages = 0, rc, i;
477 #define elements(x) (sizeof(x)/sizeof((x)[0]))
478 for ( i = 0; i < elements( papersizes ); i++ ) {
479 if ( width == papersizes[ 0 ].width &&
480 length == papersizes[ 0 ].length ) {
481 papersize = papersizes[ i ];
485 if ( i >= elements( papersizes )) {
486 papersize = papersizes[ 0 ]; /* default */
489 #define ST_AVAIL (1<<0)
490 #define ST_CONTROL (1<<1)
491 #define ST_PAGE (1<<2)
493 * convert text lines to postscript.
494 * A grungy little state machine. If I was more creative, I could
495 * probably think of a better way of doing this...
501 if (( state & ST_PAGE ) == 0 && *p != '\031' && *p != '\001' ) {
503 printf( "%%!PS-Adobe-2.0\n%%%%Pages: (atend)\n" );
504 printf( "%%%%DocumentFonts: %s\n", font );
507 /* output postscript prologue: */
508 if ( write( 1, pspro, sizeof( pspro ) - 1 ) !=
509 sizeof( pspro ) - 1 ) {
510 syslog( LOG_ERR, "write prologue: %m" );
513 if ( name && host ) {
514 printf( "statusdict /jobname (%s@%s) put\n", name,
519 printf( "%%%%Page: ? %d\n", ++npages );
520 printf( "%d %f %f /%s %d SP\n", indent,
521 papersize.win, papersize.lin, font, point );
524 if ( state & ST_CONTROL && *p != '\001' ) {
526 fprintf( stderr, "unprintable character (0x%x)!\n",
527 (unsigned char)031 );
528 return( 2 ); /* Toss job */
530 printf( "\\%o", (unsigned char)031 );
531 state &= ~ST_CONTROL;
536 case '\n' : /* end of line */
537 if ( state & ST_AVAIL ) {
544 if ( line >= length ) {
551 case '\r' : /* carriage return (for overtyping) */
552 if ( state & ST_AVAIL ) {
559 case '\f' : /* form feed */
560 if ( state & ST_AVAIL ) {
570 case '\b' : /* backspace */
571 /* show line, back up one character */
572 if ( state & ST_AVAIL ) {
580 case '\t' : /* tab */
581 if ( state & ST_AVAIL ) {
585 printf( "%d T\n", 8 - ( col % 8 ));
586 col += 8 - ( col % 8 );
590 * beginning of lpr control sequence
596 case '\001' : /* lpr control sequence */
597 if ( state & ST_CONTROL ) {
606 if (( state & ST_AVAIL ) == 0 ) {
614 if (( state & ST_AVAIL ) == 0 ) {
618 if ( !isascii( *p ) || !isprint( *p )) {
620 fprintf( stderr, "unprintable character (0x%x)!\n",
622 return( 2 ); /* Toss job */
624 printf( "\\%o", (unsigned char)*p );
633 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
635 syslog( LOG_ERR, "read: %m" );
641 if ( state & ST_AVAIL ) {
646 if ( state & ST_PAGE ) {
652 printf( "%%%%Trailer\nGSV restore\n%%%%Pages: %d\n%%%%EOF\n", npages );
660 * Interface to pipe and exec, for starting children in pipelines.
662 * Manipulates file descriptors 0, 1, and 2, such that the new child
663 * is reading from the parent's output.
665 int pexecv( path, argv )
670 if ( pipe( fd ) < 0 ) {
674 switch ( c = fork()) {
680 if ( close( fd[ 1 ] ) < 0 ) {
683 if ( dup2( fd[ 0 ], 0 ) < 0 ) {
686 if ( close( fd[ 0 ] ) < 0 ) {
694 if ( close( fd[ 0 ] ) < 0 ) {
697 if ( dup2( fd[ 1 ], 1 ) < 0 ) {
700 if ( close( fd[ 1 ] ) < 0 ) {