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.
27 #include <sys/types.h>
30 #include <sys/syslog.h>
31 #include <atalk/paths.h>
37 char psapath[] = _PATH_PSA;
38 char *psaargv[] = { "psa", 0, 0, 0, 0 };
41 * If we're not doing accounting, we just call pap as below.
42 * If we are doing accounting, we call pap twice. The first time,
43 * we call it with "-E" in arg 2, pagecount.ps in arg 3, and "-" in
44 * arg 4. The second time, we call it with "-c" in arg 2, pagecount.ps
45 * in arg 3, and 0 in arg 4.
47 char pappath[] = _PATH_PAP;
48 char *papargv[] = { "pap", "-sstatus", 0, 0, 0, 0, 0, 0 };
50 char revpath[] = _PATH_PSORDER;
51 char *revargv[] = { "psorder", "-d", 0 };
53 char *filtargv[] = { 0, 0, 0 };
55 char inbuf[ 1024 * 8 ];
58 FILE *acctfile = NULL;
60 int width = 80, length = 66, indent = 0;
61 char *prog, *name, *host;
69 { 80, 66, 8.5, 11.0 }, /* US Letter */
70 { 80, 70, 8.27, 11.69 }, /* A4 */
77 int c, rc, children = 0;
79 int psafileno, multiconn = 0, waitidle = 0, waitidle2 = 0;
83 extern int optind, opterr;
86 if (( prog = rindex( av[ 0 ], '/' )) == NULL ) {
92 openlog( prog, LOG_PID );
94 openlog( prog, LOG_PID, LOG_LPR );
97 while (( c = getopt( ac, av, "P:C:D:F:L:J:x:y:n:h:w:l:i:c" )) != EOF ) {
108 width = atoi( optarg );
111 * Some version of lpd pass 0 for the page width.
120 length = atoi( optarg );
124 indent = atoi( optarg );
127 case 'c' : /* Print control chars */
141 syslog( LOG_ERR, "bad option: %c", c );
146 if ( ac - optind > 1 ) {
147 syslog( LOG_ERR, "Too many arguments" );
151 if ( index( prog, 'w' )) {
154 if ( index( prog, 'W' )) {
157 if ( index( prog, 'm' )) {
162 syslog( LOG_INFO, "starting for %s", name ? name : "?" );
165 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) < 0 ) {
166 syslog( LOG_ERR, "read: %m" );
169 if ( inlen == 0 ) { /* nothing to be done */
170 syslog( LOG_INFO, "done" );
175 * If we've been given an accounting file, start the accounting
179 /* build arguments */
180 psaargv[ 1 ] = av[ optind ];
183 if (( c = pexecv( psapath, psaargv )) < 0 ) {
184 syslog( LOG_ERR, "%s: %m", psapath );
188 syslog( LOG_INFO, "accounting with psa[%d]", c );
192 * Check prog's name to decide what programs to execute.
194 if ( strstr( prog, "pap" ) != NULL ) {
195 if ( optind < ac ) { /* accounting */
198 psafileno = getdtablesize();
200 dup2( 1, psafileno );
206 papargv[ 5 ] = _PATH_PAGECOUNT;
209 } else if ( waitidle ) {
213 papargv[ 5 ] = _PATH_PAGECOUNT;
219 papargv[ 4 ] = _PATH_PAGECOUNT;
225 * This is how it should be done.
228 papargv[ 3 ] = _PATH_PAGECOUNT;
230 papargv[ 5 ] = _PATH_PAGECOUNT;
240 if (( c = pexecv( pappath, papargv )) < 0 ) {
241 syslog( LOG_ERR, "%s: %m", pappath );
245 syslog( LOG_INFO, "sending to pap[%d]", c );
249 * Might be a good idea to have both a "forw" and a "rev", so that
250 * reversed documents can be reordered for the printing device.
252 if ( strstr( prog, "rev" ) != NULL ) {
253 if (( c = pexecv( revpath, revargv )) < 0 ) {
254 syslog( LOG_ERR, "%s: %m", revpath );
257 syslog( LOG_INFO, "sending to rev[%d]", c );
262 * Invoke an external (script) filter to produce PostScript from
265 if ( *prog != 'i' && *prog != 'o' && *( prog + 1 ) == 'f' ) {
266 filtargv[ 0 ] = filtargv[ 1 ] = prog;
267 if (( c = pexecv( _PATH_PSFILTER, filtargv )) < 0 ) {
268 syslog( LOG_ERR, "%s: %m", _PATH_PSFILTER );
271 syslog( LOG_INFO, "external filter[%d]", c );
273 rc = copyio(); /* external filter */
275 if ( inlen >= 2 && inbuf[ 0 ] == '%' && inbuf[ 1 ] == '!' ) {
276 syslog( LOG_INFO, "PostScript" );
277 rc = copyio(); /* PostScript */
278 } else if ( inlen >= 2 && inbuf[ 0 ] == '\033' && inbuf[ 1 ] == '%' ) {
279 syslog( LOG_INFO, "PostScript w/PJL" );
280 rc = copyio(); /* PostScript */
282 syslog( LOG_INFO, "straight text" );
283 rc = textps(); /* straight text */
288 if ( strstr( prog, "pap" ) != NULL && optind < ac && multiconn ) {
289 dup2( psafileno, 1 );
294 papargv[ 4 ] = _PATH_PAGECOUNT;
296 } else if ( waitidle ) {
298 papargv[ 4 ] = _PATH_PAGECOUNT;
301 papargv[ 3 ] = _PATH_PAGECOUNT;
305 if (( c = pexecv( pappath, papargv )) < 0 ) {
306 syslog( LOG_ERR, "%s: %m", pappath );
310 syslog( LOG_INFO, "pagecount with pap[%d]", c );
318 if (( c = wait3( &status, 0, 0 )) < 0 ) {
319 syslog( LOG_ERR, "wait3: %m" );
322 if ( WIFEXITED( status )) {
324 #define WEXITSTATUS(x) ((x).w_status)
326 if ( WEXITSTATUS( status ) != 0 ) {
327 syslog( LOG_ERR, "%d died with %d", c, WEXITSTATUS( status ));
328 exit( WEXITSTATUS( status ));
330 syslog( LOG_INFO, "%d done", c );
334 syslog( LOG_ERR, "%d died badly", c );
340 syslog( LOG_INFO, "pausing" );
341 kill( getpid(), SIGSTOP );
342 syslog( LOG_INFO, "restarting" );
346 syslog( LOG_INFO, "done" );
352 /* implement the FSM needed to do the suspend. Note that
353 * the last characters will be \031\001 so don't worry
354 * Fun things: 1. \031\001 should not be written to output device
355 * 2. The \031 can be last char of one read, \001 first of next
356 * - we need to write \031 if not followed by \001
365 * First, \031 and \001 *must* be the last things in the buffer
366 * (\001 can be the first thing in the next buffer). There's no
367 * need to scan any of the intervening bytes. Second, if there's
368 * more input, the escape sequence was bogus, and we should keep
373 if ( inbuf[ 0 ] == '\001' ) {
377 if ( write( 1, "\031", 1 ) != 1 ) {
378 syslog( LOG_ERR, "write: %m" );
384 if ( inbuf[ 0 ] == '\031' ) {
390 if ( write( 1, "\031", 1 ) != 1 ) {
391 syslog( LOG_ERR, "write: %m" );
396 if ( inbuf[ inlen - 2 ] == '\031' &&
397 inbuf[ inlen - 1 ] == '\001' ) {
399 } else if ( inbuf[ inlen - 1 ] == '\031' ) {
405 if (( inlen > 0 ) && ( write( 1, inbuf, inlen ) != inlen )) {
406 syslog( LOG_ERR, "write: %m" );
412 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
419 if ( select( 1, &fdset, NULL, NULL, &tv ) != 0 ) {
420 if (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 ) {
427 syslog( LOG_ERR, "read: %m" );
432 if ( write( 1, "\031", 1 ) != 1 ) {
433 syslog( LOG_ERR, "write: %m" );
436 } else if ( ctl == 2 ) {
442 char *font = "Courier";
446 /GSV save def % global VM\n\
448 /SV save def % save vmstate\n\
449 dup /H exch def % save font height\n\
450 exch findfont exch scalefont setfont % select font\n\
451 ( ) stringwidth pop /W exch def % save font width\n\
452 0.5 sub 72 mul /CY exch def % save start Y\n\
453 pop 0.5 add 72 mul /CX exch def % save start X\n\
454 CX CY moveto % make current point\n\
457 /NL { CX CY H sub dup /CY exch def moveto } bind def\n\
458 /CR { CX CY moveto } bind def\n\
459 /B { W neg 0 rmoveto}bind def\n\
460 /T { W mul 0 rmoveto}bind def\n\
461 /EP { SV restore showpage } bind def\n\
466 struct papersize papersize;
467 int state = 0, line = 0, col = 0, npages = 0, rc, i;
470 #define elements(x) (sizeof(x)/sizeof((x)[0]))
471 for ( i = 0; i < elements( papersizes ); i++ ) {
472 if ( width == papersizes[ 0 ].width &&
473 length == papersizes[ 0 ].length ) {
474 papersize = papersizes[ i ];
478 if ( i >= elements( papersizes )) {
479 papersize = papersizes[ 0 ]; /* default */
482 #define ST_AVAIL (1<<0)
483 #define ST_CONTROL (1<<1)
484 #define ST_PAGE (1<<2)
486 * convert text lines to postscript.
487 * A grungy little state machine. If I was more creative, I could
488 * probably think of a better way of doing this...
494 if (( state & ST_PAGE ) == 0 && *p != '\031' && *p != '\001' ) {
496 printf( "%%!PS-Adobe-2.0\n%%%%Pages: (atend)\n" );
497 printf( "%%%%DocumentFonts: %s\n", font );
500 /* output postscript prologue: */
501 if ( write( 1, pspro, sizeof( pspro ) - 1 ) !=
502 sizeof( pspro ) - 1 ) {
503 syslog( LOG_ERR, "write prologue: %m" );
506 if ( name && host ) {
507 printf( "statusdict /jobname (%s@%s) put\n", name,
512 printf( "%%%%Page: ? %d\n", ++npages );
513 printf( "%d %f %f /%s %d SP\n", indent,
514 papersize.win, papersize.lin, font, point );
517 if ( state & ST_CONTROL && *p != '\001' ) {
519 fprintf( stderr, "unprintable character (0x%x)!\n",
520 (unsigned char)031 );
521 return( 2 ); /* Toss job */
523 printf( "\\%o", (unsigned char)031 );
524 state &= ~ST_CONTROL;
529 case '\n' : /* end of line */
530 if ( state & ST_AVAIL ) {
537 if ( line >= length ) {
544 case '\r' : /* carriage return (for overtyping) */
545 if ( state & ST_AVAIL ) {
552 case '\f' : /* form feed */
553 if ( state & ST_AVAIL ) {
563 case '\b' : /* backspace */
564 /* show line, back up one character */
565 if ( state & ST_AVAIL ) {
573 case '\t' : /* tab */
574 if ( state & ST_AVAIL ) {
578 printf( "%d T\n", 8 - ( col % 8 ));
579 col += 8 - ( col % 8 );
583 * beginning of lpr control sequence
589 case '\001' : /* lpr control sequence */
590 if ( state & ST_CONTROL ) {
599 if (( state & ST_AVAIL ) == 0 ) {
607 if (( state & ST_AVAIL ) == 0 ) {
611 if ( !isascii( *p ) || !isprint( *p )) {
613 fprintf( stderr, "unprintable character (0x%x)!\n",
615 return( 2 ); /* Toss job */
617 printf( "\\%o", (unsigned char)*p );
626 } while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
628 syslog( LOG_ERR, "read: %m" );
634 if ( state & ST_AVAIL ) {
639 if ( state & ST_PAGE ) {
645 printf( "%%%%Trailer\nGSV restore\n%%%%Pages: %d\n%%%%EOF\n", npages );
653 * Interface to pipe and exec, for starting children in pipelines.
655 * Manipulates file descriptors 0, 1, and 2, such that the new child
656 * is reading from the parent's output.
663 if ( pipe( fd ) < 0 ) {
667 switch ( c = fork()) {
673 if ( close( fd[ 1 ] ) < 0 ) {
676 if ( dup2( fd[ 0 ], 0 ) < 0 ) {
679 if ( close( fd[ 0 ] ) < 0 ) {
687 if ( close( fd[ 0 ] ) < 0 ) {
690 if ( dup2( fd[ 1 ], 1 ) < 0 ) {
693 if ( close( fd[ 1 ] ) < 0 ) {