2 * $Id: psf.c,v 1.5 2001-09-06 19:04:40 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 */
31 /* POSIX.1 sys/wait.h check */
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_WAIT_H
35 #endif /* HAVE_SYS_WAIT_H */
37 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
38 #endif /* ! WEXITSTATUS */
40 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
41 #endif /* ! WIFEXITED */
44 #include <sys/syslog.h>
45 #include <atalk/paths.h>
52 /* Forward Declarations */
53 int pexecv(char *path, char *argv[]);
57 char psapath[] = _PATH_PSA;
58 char *psaargv[] = { "psa", 0, 0, 0, 0 };
61 * If we're not doing accounting, we just call pap as below.
62 * If we are doing accounting, we call pap twice. The first time,
63 * we call it with "-E" in arg 2, pagecount.ps in arg 3, and "-" in
64 * arg 4. The second time, we call it with "-c" in arg 2, pagecount.ps
65 * in arg 3, and 0 in arg 4.
67 char pappath[] = _PATH_PAP;
68 char *papargv[] = { "pap", "-sstatus", 0, 0, 0, 0, 0, 0 };
70 char revpath[] = _PATH_PSORDER;
71 char *revargv[] = { "psorder", "-d", 0 };
73 char *filtargv[] = { 0, 0, 0 };
75 char inbuf[ 1024 * 8 ];
78 FILE *acctfile = NULL;
80 int width = 80, length = 66, indent = 0;
81 char *prog, *name, *host;
89 { 80, 66, 8.5, 11.0 }, /* US Letter */
90 { 80, 70, 8.27, 11.69 }, /* A4 */
97 int c, rc, children = 0;
99 int psafileno, multiconn = 0, waitidle = 0, waitidle2 = 0;
103 extern int optind, opterr;
106 if (( prog = rindex( av[ 0 ], '/' )) == NULL ) {
112 openlog( prog, LOG_PID );
114 openlog( prog, LOG_PID, LOG_LPR );
117 while (( c = getopt( ac, av, "P:C:D:F:L:J:x:y:n:h:w:l:i:c" )) != EOF ) {
128 width = atoi( optarg );
131 * Some version of lpd pass 0 for the page width.
136 #endif /* ZEROWIDTH */
140 length = atoi( optarg );
144 indent = atoi( optarg );
147 case 'c' : /* Print control chars */
161 syslog( LOG_ERR, "bad option: %c", c );
166 if ( ac - optind > 1 ) {
167 syslog( LOG_ERR, "Too many arguments" );
171 if ( index( prog, 'w' )) {
174 if ( index( prog, 'W' )) {
177 if ( index( prog, 'm' )) {
182 syslog( LOG_INFO, "starting for %s", name ? name : "?" );
185 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) < 0 ) {
186 syslog( LOG_ERR, "read: %m" );
189 if ( inlen == 0 ) { /* nothing to be done */
190 syslog( LOG_INFO, "done" );
195 * If we've been given an accounting file, start the accounting
199 /* build arguments */
200 psaargv[ 1 ] = av[ optind ];
203 if (( c = pexecv( psapath, psaargv )) < 0 ) {
204 syslog( LOG_ERR, "%s: %m", psapath );
208 syslog( LOG_INFO, "accounting with psa[%d]", c );
212 * Check prog's name to decide what programs to execute.
214 if ( strstr( prog, "pap" ) != NULL ) {
215 if ( optind < ac ) { /* accounting */
218 psafileno = getdtablesize();
220 dup2( 1, psafileno );
226 papargv[ 5 ] = _PATH_PAGECOUNT;
229 } else if ( waitidle ) {
233 papargv[ 5 ] = _PATH_PAGECOUNT;
239 papargv[ 4 ] = _PATH_PAGECOUNT;
245 * This is how it should be done.
248 papargv[ 3 ] = _PATH_PAGECOUNT;
250 papargv[ 5 ] = _PATH_PAGECOUNT;
260 if (( c = pexecv( pappath, papargv )) < 0 ) {
261 syslog( LOG_ERR, "%s: %m", pappath );
265 syslog( LOG_INFO, "sending to pap[%d]", c );
269 * Might be a good idea to have both a "forw" and a "rev", so that
270 * reversed documents can be reordered for the printing device.
272 if ( strstr( prog, "rev" ) != NULL ) {
273 if (( c = pexecv( revpath, revargv )) < 0 ) {
274 syslog( LOG_ERR, "%s: %m", revpath );
277 syslog( LOG_INFO, "sending to rev[%d]", c );
282 * Invoke an external (script) filter to produce PostScript from
285 if ( *prog != 'i' && *prog != 'o' && *( prog + 1 ) == 'f' ) {
286 filtargv[ 0 ] = filtargv[ 1 ] = prog;
287 if (( c = pexecv( _PATH_PSFILTER, filtargv )) < 0 ) {
288 syslog( LOG_ERR, "%s: %m", _PATH_PSFILTER );
291 syslog( LOG_INFO, "external filter[%d]", c );
293 rc = copyio(); /* external filter */
295 if ( inlen >= 2 && inbuf[ 0 ] == '%' && inbuf[ 1 ] == '!' ) {
296 syslog( LOG_INFO, "PostScript" );
297 rc = copyio(); /* PostScript */
298 } else if ( inlen >= 2 && inbuf[ 0 ] == '\033' && inbuf[ 1 ] == '%' ) {
299 syslog( LOG_INFO, "PostScript w/PJL" );
300 rc = copyio(); /* PostScript */
302 syslog( LOG_INFO, "straight text" );
303 rc = textps(); /* straight text */
308 if ( strstr( prog, "pap" ) != NULL && optind < ac && multiconn ) {
309 dup2( psafileno, 1 );
314 papargv[ 4 ] = _PATH_PAGECOUNT;
316 } else if ( waitidle ) {
318 papargv[ 4 ] = _PATH_PAGECOUNT;
321 papargv[ 3 ] = _PATH_PAGECOUNT;
325 if (( c = pexecv( pappath, papargv )) < 0 ) {
326 syslog( LOG_ERR, "%s: %m", pappath );
330 syslog( LOG_INFO, "pagecount with pap[%d]", c );
338 if (( c = wait3( &status, 0, 0 )) < 0 ) {
339 syslog( LOG_ERR, "wait3: %m" );
342 if ( WIFEXITED( status )) {
344 #define WEXITSTATUS(x) ((x).w_status)
345 #endif /* WEXITSTATUS */
346 if ( WEXITSTATUS( status ) != 0 ) {
347 syslog( LOG_ERR, "%d died with %d", c, WEXITSTATUS( status ));
348 exit( WEXITSTATUS( status ));
350 syslog( LOG_INFO, "%d done", c );
354 syslog( LOG_ERR, "%d died badly", c );
360 syslog( LOG_INFO, "pausing" );
361 kill( getpid(), SIGSTOP );
362 syslog( LOG_INFO, "restarting" );
366 syslog( LOG_INFO, "done" );
372 /* implement the FSM needed to do the suspend. Note that
373 * the last characters will be \031\001 so don't worry
374 * Fun things: 1. \031\001 should not be written to output device
375 * 2. The \031 can be last char of one read, \001 first of next
376 * - we need to write \031 if not followed by \001
385 * First, \031 and \001 *must* be the last things in the buffer
386 * (\001 can be the first thing in the next buffer). There's no
387 * need to scan any of the intervening bytes. Second, if there's
388 * more input, the escape sequence was bogus, and we should keep
393 if ( inbuf[ 0 ] == '\001' ) {
397 if ( write( 1, "\031", 1 ) != 1 ) {
398 syslog( LOG_ERR, "write: %m" );
404 if ( inbuf[ 0 ] == '\031' ) {
410 if ( write( 1, "\031", 1 ) != 1 ) {
411 syslog( LOG_ERR, "write: %m" );
416 if ( inbuf[ inlen - 2 ] == '\031' &&
417 inbuf[ inlen - 1 ] == '\001' ) {
419 } else if ( inbuf[ inlen - 1 ] == '\031' ) {
425 if (( inlen > 0 ) && ( write( 1, inbuf, inlen ) != inlen )) {
426 syslog( LOG_ERR, "write: %m" );
432 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
439 if ( select( 1, &fdset, NULL, NULL, &tv ) != 0 ) {
440 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 ) {
447 syslog( LOG_ERR, "read: %m" );
452 if ( write( 1, "\031", 1 ) != 1 ) {
453 syslog( LOG_ERR, "write: %m" );
456 } else if ( ctl == 2 ) {
462 char *font = "Courier";
466 /GSV save def % global VM\n\
468 /SV save def % save vmstate\n\
469 dup /H exch def % save font height\n\
470 exch findfont exch scalefont setfont % select font\n\
471 ( ) stringwidth pop /W exch def % save font width\n\
472 0.5 sub 72 mul /CY exch def % save start Y\n\
473 pop 0.5 add 72 mul /CX exch def % save start X\n\
474 CX CY moveto % make current point\n\
477 /NL { CX CY H sub dup /CY exch def moveto } bind def\n\
478 /CR { CX CY moveto } bind def\n\
479 /B { W neg 0 rmoveto}bind def\n\
480 /T { W mul 0 rmoveto}bind def\n\
481 /EP { SV restore showpage } bind def\n\
486 struct papersize papersize;
487 int state = 0, line = 0, col = 0, npages = 0, rc, i;
490 #define elements(x) (sizeof(x)/sizeof((x)[0]))
491 for ( i = 0; i < elements( papersizes ); i++ ) {
492 if ( width == papersizes[ 0 ].width &&
493 length == papersizes[ 0 ].length ) {
494 papersize = papersizes[ i ];
498 if ( i >= elements( papersizes )) {
499 papersize = papersizes[ 0 ]; /* default */
502 #define ST_AVAIL (1<<0)
503 #define ST_CONTROL (1<<1)
504 #define ST_PAGE (1<<2)
506 * convert text lines to postscript.
507 * A grungy little state machine. If I was more creative, I could
508 * probably think of a better way of doing this...
514 if (( state & ST_PAGE ) == 0 && *p != '\031' && *p != '\001' ) {
516 printf( "%%!PS-Adobe-2.0\n%%%%Pages: (atend)\n" );
517 printf( "%%%%DocumentFonts: %s\n", font );
520 /* output postscript prologue: */
521 if ( write( 1, pspro, sizeof( pspro ) - 1 ) !=
522 sizeof( pspro ) - 1 ) {
523 syslog( LOG_ERR, "write prologue: %m" );
526 if ( name && host ) {
527 printf( "statusdict /jobname (%s@%s) put\n", name,
532 printf( "%%%%Page: ? %d\n", ++npages );
533 printf( "%d %f %f /%s %d SP\n", indent,
534 papersize.win, papersize.lin, font, point );
537 if ( state & ST_CONTROL && *p != '\001' ) {
539 fprintf( stderr, "unprintable character (0x%x)!\n",
540 (unsigned char)031 );
541 return( 2 ); /* Toss job */
543 printf( "\\%o", (unsigned char)031 );
544 state &= ~ST_CONTROL;
549 case '\n' : /* end of line */
550 if ( state & ST_AVAIL ) {
557 if ( line >= length ) {
564 case '\r' : /* carriage return (for overtyping) */
565 if ( state & ST_AVAIL ) {
572 case '\f' : /* form feed */
573 if ( state & ST_AVAIL ) {
583 case '\b' : /* backspace */
584 /* show line, back up one character */
585 if ( state & ST_AVAIL ) {
593 case '\t' : /* tab */
594 if ( state & ST_AVAIL ) {
598 printf( "%d T\n", 8 - ( col % 8 ));
599 col += 8 - ( col % 8 );
603 * beginning of lpr control sequence
609 case '\001' : /* lpr control sequence */
610 if ( state & ST_CONTROL ) {
619 if (( state & ST_AVAIL ) == 0 ) {
627 if (( state & ST_AVAIL ) == 0 ) {
631 if ( !isascii( *p ) || !isprint( *p )) {
633 fprintf( stderr, "unprintable character (0x%x)!\n",
635 return( 2 ); /* Toss job */
637 printf( "\\%o", (unsigned char)*p );
646 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
648 syslog( LOG_ERR, "read: %m" );
654 if ( state & ST_AVAIL ) {
659 if ( state & ST_PAGE ) {
665 printf( "%%%%Trailer\nGSV restore\n%%%%Pages: %d\n%%%%EOF\n", npages );
673 * Interface to pipe and exec, for starting children in pipelines.
675 * Manipulates file descriptors 0, 1, and 2, such that the new child
676 * is reading from the parent's output.
678 int pexecv( path, argv )
683 if ( pipe( fd ) < 0 ) {
687 switch ( c = fork()) {
693 if ( close( fd[ 1 ] ) < 0 ) {
696 if ( dup2( fd[ 0 ], 0 ) < 0 ) {
699 if ( close( fd[ 0 ] ) < 0 ) {
707 if ( close( fd[ 0 ] ) < 0 ) {
710 if ( dup2( fd[ 1 ], 1 ) < 0 ) {
713 if ( close( fd[ 1 ] ) < 0 ) {