/*
+ * $Id: psf.c,v 1.13 2009-10-16 01:50:50 didg Exp $
+ *
* Copyright (c) 1990,1995 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
- */
-
-/*
+ *
* PostScript Filter, psf.
*
* Handles both PostScript files and text files. Files with the
#ifdef HAVE_CONFIG_H
#include "config.h"
-#endif
+#endif /* HAVE_CONFIG_H */
#define FUCKED
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif /* HAVE_UNISTD_H */
#include <sys/time.h>
+
+/* POSIX.1 sys/wait.h check */
#include <sys/types.h>
+#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
+#endif /* HAVE_SYS_WAIT_H */
+#ifndef WEXITSTATUS
+#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
+#endif /* ! WEXITSTATUS */
+#ifndef WIFEXITED
+#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
+#endif /* ! WIFEXITED */
+
#include <sys/file.h>
-#include <sys/syslog.h>
+#include <syslog.h>
#include <atalk/paths.h>
#include <stdio.h>
-#include <strings.h>
+#include <stdlib.h>
+#include <string.h>
#include <ctype.h>
#include <signal.h>
+#include <errno.h>
+
+/* Forward Declarations */
+int pexecv(char *path, char *argv[]);
+int copyio();
+int textps();
-char psapath[] = _PATH_PSA;
-char *psaargv[] = { "psa", 0, 0, 0, 0 };
+static char psapath[] = _PATH_PSA;
+static char *psaargv[] = { "psa", NULL, NULL, NULL, NULL };
/*
* If we're not doing accounting, we just call pap as below.
* arg 4. The second time, we call it with "-c" in arg 2, pagecount.ps
* in arg 3, and 0 in arg 4.
*/
-char pappath[] = _PATH_PAP;
-char *papargv[] = { "pap", "-sstatus", 0, 0, 0, 0, 0, 0 };
+static char pappath[] = _PATH_PAP;
+static char *papargv[] = { "pap", "-sstatus", NULL, NULL, NULL, NULL, NULL, NULL };
-char revpath[] = _PATH_PSORDER;
-char *revargv[] = { "psorder", "-d", 0 };
+static char revpath[] = _PATH_PSORDER;
+static char *revargv[] = { "psorder", "-d", NULL };
-char *filtargv[] = { 0, 0, 0 };
+static char *filtargv[] = { NULL, NULL, NULL };
-char inbuf[ 1024 * 8 ];
-int inlen;
+static char inbuf[ 1024 * 8 ];
+static int inlen;
-FILE *acctfile = NULL;
-int literal;
-int width = 80, length = 66, indent = 0;
-char *prog, *name, *host;
+static int literal;
+static int width = 80, length = 66, indent = 0;
+static char *prog, *name, *host;
-struct papersize {
+static struct papersize {
int width;
int length;
float win;
{ 80, 70, 8.27, 11.69 }, /* A4 */
};
-main( ac, av )
- int ac;
- char **av;
+int main( int ac, char **av)
{
int c, rc, children = 0;
#ifdef FUCKED
- int psafileno, multiconn = 0, waitidle = 0, waitidle2 = 0;
-#endif FUCKED
+ int psafileno = 0, multiconn = 0, waitidle = 0, waitidle2 = 0;
+#endif /* FUCKED */
int status;
extern char *optarg;
extern int optind, opterr;
}
#ifdef ultrix
openlog( prog, LOG_PID );
-#else ultrix
+#else /* ultrix */
openlog( prog, LOG_PID, LOG_LPR );
-#endif ultrix
+#endif /* ultrix */
while (( c = getopt( ac, av, "P:C:D:F:L:J:x:y:n:h:w:l:i:c" )) != EOF ) {
switch ( c ) {
if ( width == 0 ) {
width = 80;
}
-#endif ZEROWIDTH
+#endif /* ZEROWIDTH */
break;
case 'l' :
default :
syslog( LOG_ERR, "bad option: %c", c );
exit( 2 );
-#endif notdef
+#endif /* notdef */
}
}
if ( ac - optind > 1 ) {
if ( index( prog, 'm' )) {
multiconn++;
}
-#endif FUCKED
+#endif /* FUCKED */
syslog( LOG_INFO, "starting for %s", name ? name : "?" );
restart:
if (( inlen = read( 0, inbuf, sizeof( inbuf ))) < 0 ) {
- syslog( LOG_ERR, "read: %m" );
+ syslog( LOG_ERR, "read: %s", strerror(errno) );
exit( 1 );
}
if ( inlen == 0 ) { /* nothing to be done */
psaargv[ 2 ] = name;
psaargv[ 3 ] = host;
if (( c = pexecv( psapath, psaargv )) < 0 ) {
- syslog( LOG_ERR, "%s: %m", psapath );
+ syslog( LOG_ERR, "%s: %s", psapath, strerror(errno) );
exit( 2 );
}
children++;
papargv[ 4 ] = "-E";
papargv[ 5 ] = _PATH_PAGECOUNT;
papargv[ 6 ] = "-";
- papargv[ 7 ] = 0;
+ papargv[ 7 ] = NULL;
} else if ( waitidle ) {
papargv[ 2 ] = "-w";
papargv[ 3 ] = "-c";
papargv[ 4 ] = "-E";
papargv[ 5 ] = _PATH_PAGECOUNT;
papargv[ 6 ] = "-";
- papargv[ 7 ] = 0;
+ papargv[ 7 ] = NULL;
} else {
papargv[ 2 ] = "-c";
papargv[ 3 ] = "-E";
papargv[ 4 ] = _PATH_PAGECOUNT;
papargv[ 5 ] = "-";
- papargv[ 6 ] = 0;
+ papargv[ 6 ] = NULL;
}
} else {
/*
papargv[ 3 ] = _PATH_PAGECOUNT;
papargv[ 4 ] = "-";
papargv[ 5 ] = _PATH_PAGECOUNT;
- papargv[ 6 ] = 0;
+ papargv[ 6 ] = NULL;
}
-#endif FUCKED
+#endif /* FUCKED */
} else {
papargv[ 2 ] = "-c";
papargv[ 3 ] = "-E";
- papargv[ 4 ] = 0;
+ papargv[ 4 ] = NULL;
}
if (( c = pexecv( pappath, papargv )) < 0 ) {
- syslog( LOG_ERR, "%s: %m", pappath );
+ syslog( LOG_ERR, "%s: %s", pappath, strerror(errno) );
exit( 2 );
}
children++;
*/
if ( strstr( prog, "rev" ) != NULL ) {
if (( c = pexecv( revpath, revargv )) < 0 ) {
- syslog( LOG_ERR, "%s: %m", revpath );
+ syslog( LOG_ERR, "%s: %s", revpath, strerror(errno) );
exit( 2 );
}
syslog( LOG_INFO, "sending to rev[%d]", c );
if ( *prog != 'i' && *prog != 'o' && *( prog + 1 ) == 'f' ) {
filtargv[ 0 ] = filtargv[ 1 ] = prog;
if (( c = pexecv( _PATH_PSFILTER, filtargv )) < 0 ) {
- syslog( LOG_ERR, "%s: %m", _PATH_PSFILTER );
+ syslog( LOG_ERR, "%s: %s", _PATH_PSFILTER, strerror(errno) );
exit( 2 );
}
syslog( LOG_INFO, "external filter[%d]", c );
if ( waitidle2 ) {
papargv[ 3 ] = "-W";
papargv[ 4 ] = _PATH_PAGECOUNT;
- papargv[ 5 ] = 0;
+ papargv[ 5 ] = NULL;
} else if ( waitidle ) {
papargv[ 3 ] = "-w";
papargv[ 4 ] = _PATH_PAGECOUNT;
- papargv[ 5 ] = 0;
+ papargv[ 5 ] = NULL;
} else {
papargv[ 3 ] = _PATH_PAGECOUNT;
- papargv[ 4 ] = 0;
+ papargv[ 4 ] = NULL;
}
if (( c = pexecv( pappath, papargv )) < 0 ) {
- syslog( LOG_ERR, "%s: %m", pappath );
+ syslog( LOG_ERR, "%s: %s", pappath, strerror(errno) );
exit( 2 );
}
children++;
syslog( LOG_INFO, "pagecount with pap[%d]", c );
}
-#endif FUCKED
+#endif /* FUCKED */
if ( children ) {
close( 1 );
}
while ( children ) {
- if (( c = wait3( &status, 0, 0 )) < 0 ) {
- syslog( LOG_ERR, "wait3: %m" );
+ if (( c = wait3( &status, 0, NULL )) < 0 ) {
+ syslog( LOG_ERR, "wait3: %s", strerror(errno) );
exit( 1 );
}
if ( WIFEXITED( status )) {
#ifndef WEXITSTATUS
#define WEXITSTATUS(x) ((x).w_status)
-#endif WEXITSTATUS
+#endif /* WEXITSTATUS */
if ( WEXITSTATUS( status ) != 0 ) {
syslog( LOG_ERR, "%d died with %d", c, WEXITSTATUS( status ));
exit( WEXITSTATUS( status ));
exit( rc );
}
-copyio()
+int copyio(void)
{
/* implement the FSM needed to do the suspend. Note that
* the last characters will be \031\001 so don't worry
*/
struct timeval tv;
fd_set fdset;
- int ctl = 0, i;
+ int ctl = 0;
notdone:
do {
break;
}
if ( write( 1, "\031", 1 ) != 1 ) {
- syslog( LOG_ERR, "write: %m" );
+ syslog( LOG_ERR, "write: %s", strerror(errno) );
return( 1 );
}
ctl = 0;
} else {
if ( ctl == 1 ) {
if ( write( 1, "\031", 1 ) != 1 ) {
- syslog( LOG_ERR, "write: %m" );
+ syslog( LOG_ERR, "write: %s", strerror(errno) );
return( 1 );
}
}
inlen -= ctl;
if (( inlen > 0 ) && ( write( 1, inbuf, inlen ) != inlen )) {
- syslog( LOG_ERR, "write: %m" );
+ syslog( LOG_ERR, "write: %s", strerror(errno) );
return( 1 );
}
if ( ctl == 2 ) {
}
if ( inlen < 0 ) {
- syslog( LOG_ERR, "read: %m" );
+ syslog( LOG_ERR, "read: %s", strerror(errno) );
return( 1 );
}
if ( ctl == 1 ) {
if ( write( 1, "\031", 1 ) != 1 ) {
- syslog( LOG_ERR, "write: %m" );
+ syslog( LOG_ERR, "write: %s", strerror(errno) );
return( 1 );
}
} else if ( ctl == 2 ) {
return( 0 );
}
-char *font = "Courier";
-int point = 11;
+static char *font = "Courier";
+static int point = 11;
-char pspro[] = "\
+static char pspro[] = "\
/GSV save def % global VM\n\
/SP {\n\
/SV save def % save vmstate\n\
/EP { SV restore showpage } bind def\n\
%%EndProlog\n";
-textps()
+int textps(void)
{
struct papersize papersize;
- int state = 0, line = 0, col = 0, npages = 0, rc, i;
+ int state = 0, line = 0, col = 0, npages = 0, rc;
+ unsigned int i;
char *p, *end;
#define elements(x) (sizeof(x)/sizeof((x)[0]))
/* output postscript prologue: */
if ( write( 1, pspro, sizeof( pspro ) - 1 ) !=
sizeof( pspro ) - 1 ) {
- syslog( LOG_ERR, "write prologue: %m" );
+ syslog( LOG_ERR, "write prologue: %s", strerror(errno) );
return( 1 );
}
if ( name && host ) {
state |= ST_PAGE;
}
if ( state & ST_CONTROL && *p != '\001' ) {
+ /* It is a very bad thing to toss a job because it contains
+ * unprintable characters. Instead, we will convert them to
+ * question marks. This is adapted from a solution described
+ * by Werner Eugster <eugster@giub.unibe.ch> on his ApplePrint
+ * webpage (http://www.giub.unibe.ch/~eugster/appleprint.html).
+ *
+ * Note that this is rather ugly code. The same change is
+ * applied identically at two different locations in this file.
+ * It would be better someday to combine the two.
+ */
if ( !literal ) {
- fprintf( stderr, "unprintable character (0x%x)!\n",
- (unsigned char)031 );
- return( 2 ); /* Toss job */
+ fprintf( stderr,
+ "unprintable character (0x%x) converted to ?!\n",
+ (unsigned char)*p );
+ putchar( '?' ); /* Replace unprintable char with a question mark. */
+ } else {
+ printf( "\\%o", (unsigned char)031 );
}
- printf( "\\%o", (unsigned char)031 );
state &= ~ST_CONTROL;
col++;
}
}
if ( !isascii( *p ) || !isprint( *p )) {
if ( !literal ) {
- fprintf( stderr, "unprintable character (0x%x)!\n",
- (unsigned char)*p );
- return( 2 ); /* Toss job */
+ fprintf( stderr,
+ "unprintable character (0x%x) converted to ?!\n",
+ (unsigned char)*p );
+ putchar( '?' ); /* Replace unprintable char with a question mark. */
+ } else {
+ printf( "\\%o", (unsigned char)*p );
}
- printf( "\\%o", (unsigned char)*p );
} else {
putchar( *p );
}
}
} while (( inlen = read( 0, inbuf, sizeof( inbuf ))) > 0 );
if ( inlen < 0 ) {
- syslog( LOG_ERR, "read: %m" );
+ syslog( LOG_ERR, "read: %s", strerror(errno) );
return( 1 );
}
rc = 0;
* Manipulates file descriptors 0, 1, and 2, such that the new child
* is reading from the parent's output.
*/
-pexecv( path, argv )
- char *path, *argv[];
+int pexecv( char *path, char *argv[])
{
int fd[ 2 ], c;