2 * $Id: psf.c,v 1.13 2009-10-16 01:50:50 didg 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 */
45 #include <atalk/paths.h>
53 /* Forward Declarations */
54 int pexecv(char *path, char *argv[]);
58 static char psapath[] = _PATH_PSA;
59 static char *psaargv[] = { "psa", NULL, NULL, NULL, NULL };
62 * If we're not doing accounting, we just call pap as below.
63 * If we are doing accounting, we call pap twice. The first time,
64 * we call it with "-E" in arg 2, pagecount.ps in arg 3, and "-" in
65 * arg 4. The second time, we call it with "-c" in arg 2, pagecount.ps
66 * in arg 3, and 0 in arg 4.
68 static char pappath[] = _PATH_PAP;
69 static char *papargv[] = { "pap", "-sstatus", NULL, NULL, NULL, NULL, NULL, NULL };
71 static char revpath[] = _PATH_PSORDER;
72 static char *revargv[] = { "psorder", "-d", NULL };
74 static char *filtargv[] = { NULL, NULL, NULL };
76 static char inbuf[ 1024 * 8 ];
80 static int width = 80, length = 66, indent = 0;
81 static char *prog, *name, *host;
83 static struct papersize {
89 { 80, 66, 8.5, 11.0 }, /* US Letter */
90 { 80, 70, 8.27, 11.69 }, /* A4 */
93 int main( int ac, char **av)
95 int c, rc, children = 0;
97 int psafileno = 0, multiconn = 0, waitidle = 0, waitidle2 = 0;
101 extern int optind, opterr;
104 if (( prog = rindex( av[ 0 ], '/' )) == NULL ) {
110 openlog( prog, LOG_PID );
112 openlog( prog, LOG_PID, LOG_LPR );
115 while (( c = getopt( ac, av, "P:C:D:F:L:J:x:y:n:h:w:l:i:c" )) != EOF ) {
126 width = atoi( optarg );
129 * Some version of lpd pass 0 for the page width.
134 #endif /* ZEROWIDTH */
138 length = atoi( optarg );
142 indent = atoi( optarg );
145 case 'c' : /* Print control chars */
159 syslog( LOG_ERR, "bad option: %c", c );
164 if ( ac - optind > 1 ) {
165 syslog( LOG_ERR, "Too many arguments" );
169 if ( index( prog, 'w' )) {
172 if ( index( prog, 'W' )) {
175 if ( index( prog, 'm' )) {
180 syslog( LOG_INFO, "starting for %s", name ? name : "?" );
183 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) < 0 ) {
184 syslog( LOG_ERR, "read: %s", strerror(errno) );
187 if ( inlen == 0 ) { /* nothing to be done */
188 syslog( LOG_INFO, "done" );
193 * If we've been given an accounting file, start the accounting
197 /* build arguments */
198 psaargv[ 1 ] = av[ optind ];
201 if (( c = pexecv( psapath, psaargv )) < 0 ) {
202 syslog( LOG_ERR, "%s: %s", psapath, strerror(errno) );
206 syslog( LOG_INFO, "accounting with psa[%d]", c );
210 * Check prog's name to decide what programs to execute.
212 if ( strstr( prog, "pap" ) != NULL ) {
213 if ( optind < ac ) { /* accounting */
216 psafileno = getdtablesize();
218 dup2( 1, psafileno );
224 papargv[ 5 ] = _PATH_PAGECOUNT;
227 } else if ( waitidle ) {
231 papargv[ 5 ] = _PATH_PAGECOUNT;
237 papargv[ 4 ] = _PATH_PAGECOUNT;
243 * This is how it should be done.
246 papargv[ 3 ] = _PATH_PAGECOUNT;
248 papargv[ 5 ] = _PATH_PAGECOUNT;
258 if (( c = pexecv( pappath, papargv )) < 0 ) {
259 syslog( LOG_ERR, "%s: %s", pappath, strerror(errno) );
263 syslog( LOG_INFO, "sending to pap[%d]", c );
267 * Might be a good idea to have both a "forw" and a "rev", so that
268 * reversed documents can be reordered for the printing device.
270 if ( strstr( prog, "rev" ) != NULL ) {
271 if (( c = pexecv( revpath, revargv )) < 0 ) {
272 syslog( LOG_ERR, "%s: %s", revpath, strerror(errno) );
275 syslog( LOG_INFO, "sending to rev[%d]", c );
280 * Invoke an external (script) filter to produce PostScript from
283 if ( *prog != 'i' && *prog != 'o' && *( prog + 1 ) == 'f' ) {
284 filtargv[ 0 ] = filtargv[ 1 ] = prog;
285 if (( c = pexecv( _PATH_PSFILTER, filtargv )) < 0 ) {
286 syslog( LOG_ERR, "%s: %s", _PATH_PSFILTER, strerror(errno) );
289 syslog( LOG_INFO, "external filter[%d]", c );
291 rc = copyio(); /* external filter */
293 if ( inlen >= 2 && inbuf[ 0 ] == '%' && inbuf[ 1 ] == '!' ) {
294 syslog( LOG_INFO, "PostScript" );
295 rc = copyio(); /* PostScript */
296 } else if ( inlen >= 2 && inbuf[ 0 ] == '\033' && inbuf[ 1 ] == '%' ) {
297 syslog( LOG_INFO, "PostScript w/PJL" );
298 rc = copyio(); /* PostScript */
300 syslog( LOG_INFO, "straight text" );
301 rc = textps(); /* straight text */
306 if ( strstr( prog, "pap" ) != NULL && optind < ac && multiconn ) {
307 dup2( psafileno, 1 );
312 papargv[ 4 ] = _PATH_PAGECOUNT;
314 } else if ( waitidle ) {
316 papargv[ 4 ] = _PATH_PAGECOUNT;
319 papargv[ 3 ] = _PATH_PAGECOUNT;
323 if (( c = pexecv( pappath, papargv )) < 0 ) {
324 syslog( LOG_ERR, "%s: %s", pappath, strerror(errno) );
328 syslog( LOG_INFO, "pagecount with pap[%d]", c );
336 if (( c = wait3( &status, 0, NULL )) < 0 ) {
337 syslog( LOG_ERR, "wait3: %s", strerror(errno) );
340 if ( WIFEXITED( status )) {
342 #define WEXITSTATUS(x) ((x).w_status)
343 #endif /* WEXITSTATUS */
344 if ( WEXITSTATUS( status ) != 0 ) {
345 syslog( LOG_ERR, "%d died with %d", c, WEXITSTATUS( status ));
346 exit( WEXITSTATUS( status ));
348 syslog( LOG_INFO, "%d done", c );
352 syslog( LOG_ERR, "%d died badly", c );
358 syslog( LOG_INFO, "pausing" );
359 kill( getpid(), SIGSTOP );
360 syslog( LOG_INFO, "restarting" );
364 syslog( LOG_INFO, "done" );
370 /* implement the FSM needed to do the suspend. Note that
371 * the last characters will be \031\001 so don't worry
372 * Fun things: 1. \031\001 should not be written to output device
373 * 2. The \031 can be last char of one read, \001 first of next
374 * - we need to write \031 if not followed by \001
383 * First, \031 and \001 *must* be the last things in the buffer
384 * (\001 can be the first thing in the next buffer). There's no
385 * need to scan any of the intervening bytes. Second, if there's
386 * more input, the escape sequence was bogus, and we should keep
391 if ( inbuf[ 0 ] == '\001' ) {
395 if ( write( 1, "\031", 1 ) != 1 ) {
396 syslog( LOG_ERR, "write: %s", strerror(errno) );
402 if ( inbuf[ 0 ] == '\031' ) {
408 if ( write( 1, "\031", 1 ) != 1 ) {
409 syslog( LOG_ERR, "write: %s", strerror(errno) );
414 if ( inbuf[ inlen - 2 ] == '\031' &&
415 inbuf[ inlen - 1 ] == '\001' ) {
417 } else if ( inbuf[ inlen - 1 ] == '\031' ) {
423 if (( inlen > 0 ) && ( write( 1, inbuf, inlen ) != inlen )) {
424 syslog( LOG_ERR, "write: %s", strerror(errno) );
430 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
437 if ( select( 1, &fdset, NULL, NULL, &tv ) != 0 ) {
438 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 ) {
445 syslog( LOG_ERR, "read: %s", strerror(errno) );
450 if ( write( 1, "\031", 1 ) != 1 ) {
451 syslog( LOG_ERR, "write: %s", strerror(errno) );
454 } else if ( ctl == 2 ) {
460 static char *font = "Courier";
461 static int point = 11;
463 static char pspro[] = "\
464 /GSV save def % global VM\n\
466 /SV save def % save vmstate\n\
467 dup /H exch def % save font height\n\
468 exch findfont exch scalefont setfont % select font\n\
469 ( ) stringwidth pop /W exch def % save font width\n\
470 0.5 sub 72 mul /CY exch def % save start Y\n\
471 pop 0.5 add 72 mul /CX exch def % save start X\n\
472 CX CY moveto % make current point\n\
475 /NL { CX CY H sub dup /CY exch def moveto } bind def\n\
476 /CR { CX CY moveto } bind def\n\
477 /B { W neg 0 rmoveto}bind def\n\
478 /T { W mul 0 rmoveto}bind def\n\
479 /EP { SV restore showpage } bind def\n\
484 struct papersize papersize;
485 int state = 0, line = 0, col = 0, npages = 0, rc;
489 #define elements(x) (sizeof(x)/sizeof((x)[0]))
490 for ( i = 0; i < elements( papersizes ); i++ ) {
491 if ( width == papersizes[ 0 ].width &&
492 length == papersizes[ 0 ].length ) {
493 papersize = papersizes[ i ];
497 if ( i >= elements( papersizes )) {
498 papersize = papersizes[ 0 ]; /* default */
501 #define ST_AVAIL (1<<0)
502 #define ST_CONTROL (1<<1)
503 #define ST_PAGE (1<<2)
505 * convert text lines to postscript.
506 * A grungy little state machine. If I was more creative, I could
507 * probably think of a better way of doing this...
513 if (( state & ST_PAGE ) == 0 && *p != '\031' && *p != '\001' ) {
515 printf( "%%!PS-Adobe-2.0\n%%%%Pages: (atend)\n" );
516 printf( "%%%%DocumentFonts: %s\n", font );
519 /* output postscript prologue: */
520 if ( write( 1, pspro, sizeof( pspro ) - 1 ) !=
521 sizeof( pspro ) - 1 ) {
522 syslog( LOG_ERR, "write prologue: %s", strerror(errno) );
525 if ( name && host ) {
526 printf( "statusdict /jobname (%s@%s) put\n", name,
531 printf( "%%%%Page: ? %d\n", ++npages );
532 printf( "%d %f %f /%s %d SP\n", indent,
533 papersize.win, papersize.lin, font, point );
536 if ( state & ST_CONTROL && *p != '\001' ) {
537 /* It is a very bad thing to toss a job because it contains
538 * unprintable characters. Instead, we will convert them to
539 * question marks. This is adapted from a solution described
540 * by Werner Eugster <eugster@giub.unibe.ch> on his ApplePrint
541 * webpage (http://www.giub.unibe.ch/~eugster/appleprint.html).
543 * Note that this is rather ugly code. The same change is
544 * applied identically at two different locations in this file.
545 * It would be better someday to combine the two.
549 "unprintable character (0x%x) converted to ?!\n",
551 putchar( '?' ); /* Replace unprintable char with a question mark. */
553 printf( "\\%o", (unsigned char)031 );
555 state &= ~ST_CONTROL;
560 case '\n' : /* end of line */
561 if ( state & ST_AVAIL ) {
568 if ( line >= length ) {
575 case '\r' : /* carriage return (for overtyping) */
576 if ( state & ST_AVAIL ) {
583 case '\f' : /* form feed */
584 if ( state & ST_AVAIL ) {
594 case '\b' : /* backspace */
595 /* show line, back up one character */
596 if ( state & ST_AVAIL ) {
604 case '\t' : /* tab */
605 if ( state & ST_AVAIL ) {
609 printf( "%d T\n", 8 - ( col % 8 ));
610 col += 8 - ( col % 8 );
614 * beginning of lpr control sequence
620 case '\001' : /* lpr control sequence */
621 if ( state & ST_CONTROL ) {
630 if (( state & ST_AVAIL ) == 0 ) {
638 if (( state & ST_AVAIL ) == 0 ) {
642 if ( !isascii( *p ) || !isprint( *p )) {
645 "unprintable character (0x%x) converted to ?!\n",
647 putchar( '?' ); /* Replace unprintable char with a question mark. */
649 printf( "\\%o", (unsigned char)*p );
659 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
661 syslog( LOG_ERR, "read: %s", strerror(errno) );
667 if ( state & ST_AVAIL ) {
672 if ( state & ST_PAGE ) {
678 printf( "%%%%Trailer\nGSV restore\n%%%%Pages: %d\n%%%%EOF\n", npages );
686 * Interface to pipe and exec, for starting children in pipelines.
688 * Manipulates file descriptors 0, 1, and 2, such that the new child
689 * is reading from the parent's output.
691 int pexecv( char *path, char *argv[])
695 if ( pipe( fd ) < 0 ) {
699 switch ( c = fork()) {
705 if ( close( fd[ 1 ] ) < 0 ) {
708 if ( dup2( fd[ 0 ], 0 ) < 0 ) {
711 if ( close( fd[ 0 ] ) < 0 ) {
719 if ( close( fd[ 0 ] ) < 0 ) {
722 if ( dup2( fd[ 1 ], 1 ) < 0 ) {
725 if ( close( fd[ 1 ] ) < 0 ) {