2 * $Id: psf.c,v 1.4 2001-06-25 20:13:45 rufustfirefly Exp $
4 * Copyright (c) 1990,1995 Regents of The University of Michigan.
5 * 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.
22 #endif /* HAVE_CONFIG_H */
28 #endif /* HAVE_UNISTD_H */
30 #include <sys/types.h>
33 #include <sys/syslog.h>
34 #include <atalk/paths.h>
41 /* Forward Declarations */
42 int pexecv(char *path, char *argv[]);
46 char psapath[] = _PATH_PSA;
47 char *psaargv[] = { "psa", 0, 0, 0, 0 };
50 * If we're not doing accounting, we just call pap as below.
51 * If we are doing accounting, we call pap twice. The first time,
52 * we call it with "-E" in arg 2, pagecount.ps in arg 3, and "-" in
53 * arg 4. The second time, we call it with "-c" in arg 2, pagecount.ps
54 * in arg 3, and 0 in arg 4.
56 char pappath[] = _PATH_PAP;
57 char *papargv[] = { "pap", "-sstatus", 0, 0, 0, 0, 0, 0 };
59 char revpath[] = _PATH_PSORDER;
60 char *revargv[] = { "psorder", "-d", 0 };
62 char *filtargv[] = { 0, 0, 0 };
64 char inbuf[ 1024 * 8 ];
67 FILE *acctfile = NULL;
69 int width = 80, length = 66, indent = 0;
70 char *prog, *name, *host;
78 { 80, 66, 8.5, 11.0 }, /* US Letter */
79 { 80, 70, 8.27, 11.69 }, /* A4 */
86 int c, rc, children = 0;
88 int psafileno, multiconn = 0, waitidle = 0, waitidle2 = 0;
92 extern int optind, opterr;
95 if (( prog = rindex( av[ 0 ], '/' )) == NULL ) {
101 openlog( prog, LOG_PID );
103 openlog( prog, LOG_PID, LOG_LPR );
106 while (( c = getopt( ac, av, "P:C:D:F:L:J:x:y:n:h:w:l:i:c" )) != EOF ) {
117 width = atoi( optarg );
120 * Some version of lpd pass 0 for the page width.
125 #endif /* ZEROWIDTH */
129 length = atoi( optarg );
133 indent = atoi( optarg );
136 case 'c' : /* Print control chars */
150 syslog( LOG_ERR, "bad option: %c", c );
155 if ( ac - optind > 1 ) {
156 syslog( LOG_ERR, "Too many arguments" );
160 if ( index( prog, 'w' )) {
163 if ( index( prog, 'W' )) {
166 if ( index( prog, 'm' )) {
171 syslog( LOG_INFO, "starting for %s", name ? name : "?" );
174 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) < 0 ) {
175 syslog( LOG_ERR, "read: %m" );
178 if ( inlen == 0 ) { /* nothing to be done */
179 syslog( LOG_INFO, "done" );
184 * If we've been given an accounting file, start the accounting
188 /* build arguments */
189 psaargv[ 1 ] = av[ optind ];
192 if (( c = pexecv( psapath, psaargv )) < 0 ) {
193 syslog( LOG_ERR, "%s: %m", psapath );
197 syslog( LOG_INFO, "accounting with psa[%d]", c );
201 * Check prog's name to decide what programs to execute.
203 if ( strstr( prog, "pap" ) != NULL ) {
204 if ( optind < ac ) { /* accounting */
207 psafileno = getdtablesize();
209 dup2( 1, psafileno );
215 papargv[ 5 ] = _PATH_PAGECOUNT;
218 } else if ( waitidle ) {
222 papargv[ 5 ] = _PATH_PAGECOUNT;
228 papargv[ 4 ] = _PATH_PAGECOUNT;
234 * This is how it should be done.
237 papargv[ 3 ] = _PATH_PAGECOUNT;
239 papargv[ 5 ] = _PATH_PAGECOUNT;
249 if (( c = pexecv( pappath, papargv )) < 0 ) {
250 syslog( LOG_ERR, "%s: %m", pappath );
254 syslog( LOG_INFO, "sending to pap[%d]", c );
258 * Might be a good idea to have both a "forw" and a "rev", so that
259 * reversed documents can be reordered for the printing device.
261 if ( strstr( prog, "rev" ) != NULL ) {
262 if (( c = pexecv( revpath, revargv )) < 0 ) {
263 syslog( LOG_ERR, "%s: %m", revpath );
266 syslog( LOG_INFO, "sending to rev[%d]", c );
271 * Invoke an external (script) filter to produce PostScript from
274 if ( *prog != 'i' && *prog != 'o' && *( prog + 1 ) == 'f' ) {
275 filtargv[ 0 ] = filtargv[ 1 ] = prog;
276 if (( c = pexecv( _PATH_PSFILTER, filtargv )) < 0 ) {
277 syslog( LOG_ERR, "%s: %m", _PATH_PSFILTER );
280 syslog( LOG_INFO, "external filter[%d]", c );
282 rc = copyio(); /* external filter */
284 if ( inlen >= 2 && inbuf[ 0 ] == '%' && inbuf[ 1 ] == '!' ) {
285 syslog( LOG_INFO, "PostScript" );
286 rc = copyio(); /* PostScript */
287 } else if ( inlen >= 2 && inbuf[ 0 ] == '\033' && inbuf[ 1 ] == '%' ) {
288 syslog( LOG_INFO, "PostScript w/PJL" );
289 rc = copyio(); /* PostScript */
291 syslog( LOG_INFO, "straight text" );
292 rc = textps(); /* straight text */
297 if ( strstr( prog, "pap" ) != NULL && optind < ac && multiconn ) {
298 dup2( psafileno, 1 );
303 papargv[ 4 ] = _PATH_PAGECOUNT;
305 } else if ( waitidle ) {
307 papargv[ 4 ] = _PATH_PAGECOUNT;
310 papargv[ 3 ] = _PATH_PAGECOUNT;
314 if (( c = pexecv( pappath, papargv )) < 0 ) {
315 syslog( LOG_ERR, "%s: %m", pappath );
319 syslog( LOG_INFO, "pagecount with pap[%d]", c );
327 if (( c = wait3( &status, 0, 0 )) < 0 ) {
328 syslog( LOG_ERR, "wait3: %m" );
331 if ( WIFEXITED( status )) {
333 #define WEXITSTATUS(x) ((x).w_status)
334 #endif /* WEXITSTATUS */
335 if ( WEXITSTATUS( status ) != 0 ) {
336 syslog( LOG_ERR, "%d died with %d", c, WEXITSTATUS( status ));
337 exit( WEXITSTATUS( status ));
339 syslog( LOG_INFO, "%d done", c );
343 syslog( LOG_ERR, "%d died badly", c );
349 syslog( LOG_INFO, "pausing" );
350 kill( getpid(), SIGSTOP );
351 syslog( LOG_INFO, "restarting" );
355 syslog( LOG_INFO, "done" );
361 /* implement the FSM needed to do the suspend. Note that
362 * the last characters will be \031\001 so don't worry
363 * Fun things: 1. \031\001 should not be written to output device
364 * 2. The \031 can be last char of one read, \001 first of next
365 * - we need to write \031 if not followed by \001
374 * First, \031 and \001 *must* be the last things in the buffer
375 * (\001 can be the first thing in the next buffer). There's no
376 * need to scan any of the intervening bytes. Second, if there's
377 * more input, the escape sequence was bogus, and we should keep
382 if ( inbuf[ 0 ] == '\001' ) {
386 if ( write( 1, "\031", 1 ) != 1 ) {
387 syslog( LOG_ERR, "write: %m" );
393 if ( inbuf[ 0 ] == '\031' ) {
399 if ( write( 1, "\031", 1 ) != 1 ) {
400 syslog( LOG_ERR, "write: %m" );
405 if ( inbuf[ inlen - 2 ] == '\031' &&
406 inbuf[ inlen - 1 ] == '\001' ) {
408 } else if ( inbuf[ inlen - 1 ] == '\031' ) {
414 if (( inlen > 0 ) && ( write( 1, inbuf, inlen ) != inlen )) {
415 syslog( LOG_ERR, "write: %m" );
421 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
428 if ( select( 1, &fdset, NULL, NULL, &tv ) != 0 ) {
429 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 ) {
436 syslog( LOG_ERR, "read: %m" );
441 if ( write( 1, "\031", 1 ) != 1 ) {
442 syslog( LOG_ERR, "write: %m" );
445 } else if ( ctl == 2 ) {
451 char *font = "Courier";
455 /GSV save def % global VM\n\
457 /SV save def % save vmstate\n\
458 dup /H exch def % save font height\n\
459 exch findfont exch scalefont setfont % select font\n\
460 ( ) stringwidth pop /W exch def % save font width\n\
461 0.5 sub 72 mul /CY exch def % save start Y\n\
462 pop 0.5 add 72 mul /CX exch def % save start X\n\
463 CX CY moveto % make current point\n\
466 /NL { CX CY H sub dup /CY exch def moveto } bind def\n\
467 /CR { CX CY moveto } bind def\n\
468 /B { W neg 0 rmoveto}bind def\n\
469 /T { W mul 0 rmoveto}bind def\n\
470 /EP { SV restore showpage } bind def\n\
475 struct papersize papersize;
476 int state = 0, line = 0, col = 0, npages = 0, rc, i;
479 #define elements(x) (sizeof(x)/sizeof((x)[0]))
480 for ( i = 0; i < elements( papersizes ); i++ ) {
481 if ( width == papersizes[ 0 ].width &&
482 length == papersizes[ 0 ].length ) {
483 papersize = papersizes[ i ];
487 if ( i >= elements( papersizes )) {
488 papersize = papersizes[ 0 ]; /* default */
491 #define ST_AVAIL (1<<0)
492 #define ST_CONTROL (1<<1)
493 #define ST_PAGE (1<<2)
495 * convert text lines to postscript.
496 * A grungy little state machine. If I was more creative, I could
497 * probably think of a better way of doing this...
503 if (( state & ST_PAGE ) == 0 && *p != '\031' && *p != '\001' ) {
505 printf( "%%!PS-Adobe-2.0\n%%%%Pages: (atend)\n" );
506 printf( "%%%%DocumentFonts: %s\n", font );
509 /* output postscript prologue: */
510 if ( write( 1, pspro, sizeof( pspro ) - 1 ) !=
511 sizeof( pspro ) - 1 ) {
512 syslog( LOG_ERR, "write prologue: %m" );
515 if ( name && host ) {
516 printf( "statusdict /jobname (%s@%s) put\n", name,
521 printf( "%%%%Page: ? %d\n", ++npages );
522 printf( "%d %f %f /%s %d SP\n", indent,
523 papersize.win, papersize.lin, font, point );
526 if ( state & ST_CONTROL && *p != '\001' ) {
528 fprintf( stderr, "unprintable character (0x%x)!\n",
529 (unsigned char)031 );
530 return( 2 ); /* Toss job */
532 printf( "\\%o", (unsigned char)031 );
533 state &= ~ST_CONTROL;
538 case '\n' : /* end of line */
539 if ( state & ST_AVAIL ) {
546 if ( line >= length ) {
553 case '\r' : /* carriage return (for overtyping) */
554 if ( state & ST_AVAIL ) {
561 case '\f' : /* form feed */
562 if ( state & ST_AVAIL ) {
572 case '\b' : /* backspace */
573 /* show line, back up one character */
574 if ( state & ST_AVAIL ) {
582 case '\t' : /* tab */
583 if ( state & ST_AVAIL ) {
587 printf( "%d T\n", 8 - ( col % 8 ));
588 col += 8 - ( col % 8 );
592 * beginning of lpr control sequence
598 case '\001' : /* lpr control sequence */
599 if ( state & ST_CONTROL ) {
608 if (( state & ST_AVAIL ) == 0 ) {
616 if (( state & ST_AVAIL ) == 0 ) {
620 if ( !isascii( *p ) || !isprint( *p )) {
622 fprintf( stderr, "unprintable character (0x%x)!\n",
624 return( 2 ); /* Toss job */
626 printf( "\\%o", (unsigned char)*p );
635 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
637 syslog( LOG_ERR, "read: %m" );
643 if ( state & ST_AVAIL ) {
648 if ( state & ST_PAGE ) {
654 printf( "%%%%Trailer\nGSV restore\n%%%%Pages: %d\n%%%%EOF\n", npages );
662 * Interface to pipe and exec, for starting children in pipelines.
664 * Manipulates file descriptors 0, 1, and 2, such that the new child
665 * is reading from the parent's output.
667 int pexecv( path, argv )
672 if ( pipe( fd ) < 0 ) {
676 switch ( c = fork()) {
682 if ( close( fd[ 1 ] ) < 0 ) {
685 if ( dup2( fd[ 0 ], 0 ) < 0 ) {
688 if ( close( fd[ 0 ] ) < 0 ) {
696 if ( close( fd[ 0 ] ) < 0 ) {
699 if ( dup2( fd[ 1 ], 1 ) < 0 ) {
702 if ( close( fd[ 1 ] ) < 0 ) {