]> arthur.barton.de Git - netatalk.git/blobdiff - bin/pap/pap.c
Remove bdb env on exit
[netatalk.git] / bin / pap / pap.c
index 076d08e1b58f6c476b7629145903d0590a134415..a8c97adc501455e0479aba564575d4c572011416 100644 (file)
@@ -1,47 +1,41 @@
 /*
+ * $Id: pap.c,v 1.14 2009-10-16 01:10:59 didg Exp $
+ *
  * Copyright (c) 1990,1994 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
+#endif /* HAVE_CONFIG_H */
 
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/uio.h>
 #include <netatalk/endian.h>
 #include <netatalk/at.h>
+#include <errno.h>
 #include <atalk/atp.h>
 #include <atalk/pap.h>
 #include <atalk/nbp.h>
 #include <atalk/util.h>
+#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
+#endif /* HAVE_FCNTL_H */
 #include <stdio.h>
 #include <string.h>
 #include <string.h>
-#include <errno.h>
+#include <stdlib.h>
 
 #define FUCKED
 
 #define _PATH_PAPRC    ".paprc"
-char   *nbpfailure = "AppleTalk printer offline";
-
-/* if there is a less hacky way to do this, please do it... */
-#ifdef DEBUG
-#define EBUG
-#endif
-
-#undef DEBUG
 
-#ifdef EBUG
-#define DEBUG(x,y)     (x,y)
-#else /*EBUG*/
-#define DEBUG(x,y)
-#endif /*EBUG*/
+/* Forward Declarations */
+static void updatestatus(char *s, int len);
+static int send_file(int fd, ATP atp, int lastfile);
 
-usage( path )
-    char       *path;
+static void usage(char *path)
 {
     char       *p;
 
@@ -51,12 +45,24 @@ usage( path )
        p++;
     }
     fprintf( stderr,
-       "Usage:\t%s [ -A address ] [ -p printername ] [ -s statusfile ] [ file ] ...\n", p );
+       "Usage:\t%s [ -A address ] [ -c ] [ -d ] [ -e ] [ -E ] [ -p printer ]\n"
+       "    [ -s statusfile ] [ -w ] [ -W ] [ FILES ]\n"
+       "  -A address    - printer Appletalk address\n"
+       "  -c            - take cuts (lie about wait time)\n"
+       "  -d            - enable debug\n"
+       "  -e            - send stdout to stderr\n"
+       "  -E            - don't wait for EOF from printer\n"
+       "  -p printer    - printer name\n"
+       "  -s statusfile - put current printer status in statusfile\n"
+       "  -w            - wait for printer status = 'waiting'\n"
+       "  -W            - wait for printer status = 'idle'\n"
+       "  FILES         - send FILES to printer\n"
+       , p );
     exit( 2 );
 }
 
-char *
-paprc()
+static char *
+paprc(void)
 {
     static char        s[ 32 + 1 + 32 + 1 + 32 ];
     char       *name = NULL;
@@ -82,20 +88,20 @@ paprc()
     return( name );
 }
 
-char                   *printer = NULL;
-char                   *status = NULL;
-int                    noeof = 0;
-int                    waitforprinter = 0;
+static char                    *printer = NULL;
+static char                    *status = NULL;
+static int                     noeof = 0;
+static int                     waitforprinter = 0;
 
-unsigned char          connid, quantum, oquantum = PAP_MAXQUANTUM;
-struct sockaddr_at     sat;
+static unsigned char           connid, quantum, oquantum = PAP_MAXQUANTUM;
+static struct sockaddr_at      sat;
 
-char                   cbuf[ 8 ];
-struct nbpnve          nn;
-ATP                    satp;
+static char                    cbuf[ 8 ];
+static struct nbpnve           nn;
+static ATP                     satp;
 
-char           fbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
-struct iovec   rfiov[ PAP_MAXQUANTUM ] = {
+static char            fbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
+static struct iovec    rfiov[ PAP_MAXQUANTUM ] = {
     { fbuf[ 0 ] + 4,   0 },
     { fbuf[ 1 ] + 4,   0 },
     { fbuf[ 2 ] + 4,   0 },
@@ -105,7 +111,8 @@ struct iovec        rfiov[ PAP_MAXQUANTUM ] = {
     { fbuf[ 6 ] + 4,   0 },
     { fbuf[ 7 ] + 4,   0 },
 };
-struct iovec   sniov[ PAP_MAXQUANTUM ] = {
+
+static struct iovec    sniov[ PAP_MAXQUANTUM ] = {
     { fbuf[ 0 ],       0 },
     { fbuf[ 1 ],       0 },
     { fbuf[ 2 ],       0 },
@@ -116,8 +123,8 @@ struct iovec        sniov[ PAP_MAXQUANTUM ] = {
     { fbuf[ 7 ],       0 },
 };
 
-char           nbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
-struct iovec   rniov[ PAP_MAXQUANTUM ] = {
+static char            nbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
+static struct iovec    rniov[ PAP_MAXQUANTUM ] = {
     { nbuf[ 0 ],       0 },
     { nbuf[ 1 ],       0 },
     { nbuf[ 2 ],       0 },
@@ -127,7 +134,8 @@ struct iovec        rniov[ PAP_MAXQUANTUM ] = {
     { nbuf[ 6 ],       0 },
     { nbuf[ 7 ],       0 },
 };
-struct iovec   sfiov[ PAP_MAXQUANTUM ] = {
+
+static struct iovec    sfiov[ PAP_MAXQUANTUM ] = {
     { nbuf[ 0 ] + 4,   0 },
     { nbuf[ 1 ] + 4,   0 },
     { nbuf[ 2 ] + 4,   0 },
@@ -138,9 +146,9 @@ struct iovec        sfiov[ PAP_MAXQUANTUM ] = {
     { nbuf[ 7 ] + 4,   0 },
 };
 
-main( ac, av )
-    int                ac;
-    char       **av;
+static int debug;
+
+int main( int ac, char **av)
 {
     ATP                        atp;
     struct atp_block   atpb;
@@ -158,7 +166,7 @@ main( ac, av )
     extern int         optind;
 
     memset(&addr, 0, sizeof(addr));
-    while (( c = getopt( ac, av, "Wwcep:s:EA:" )) != EOF ) {
+    while (( c = getopt( ac, av, "dWwcep:s:EA:" )) != EOF ) {
        switch ( c ) {
 #ifdef FUCKED
        case 'w' :
@@ -168,7 +176,12 @@ main( ac, av )
        case 'W' :
            waitforidle = 1;
            break;
-#endif /*FUCKED*/
+#endif /* FUCKED */
+
+       /* enable debugging */
+       case 'd' :
+           debug++;
+           break;
 
        case 'c' :
            cuts++;
@@ -230,7 +243,7 @@ main( ac, av )
        exit( 1 );
     }
 
-    if ( isatty( 0 )) {
+    if ( isatty( 1 )) {
        printf( "Trying %u.%d:%d ...\n", ntohs( nn.nn_sat.sat_addr.s_net ),
                nn.nn_sat.sat_addr.s_node, nn.nn_sat.sat_port );
     }
@@ -260,7 +273,7 @@ main( ac, av )
            exit( 1 );
        }
 
-DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
+       if(debug){ printf( "SENDSTATUS >\n" ), fflush( stdout );}
 
        atpb.atp_saddr = &nn.nn_sat;
        rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
@@ -280,7 +293,7 @@ DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
            fprintf( stderr, "Bad status response!\n" );
            exit( 1 );
        }
-#endif /*NONZEROSTATUS*/
+#endif /* NONZEROSTATUS */
 
        if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
                atpb.atp_rresiovcnt != 1 ) {
@@ -288,11 +301,11 @@ DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
            exit( 1 );
        }
 
-DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
+       if(debug){ printf( "< STATUS\n" ), fflush( stdout );}
 
        memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9, 
                ((char *)rniov[ 0 ].iov_base)[ 8 ] );
-       st_buf[ ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
+       st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
        if ( strstr( st_buf, "idle" ) != NULL ) {
            waitforidle = 0;
        } else {
@@ -307,7 +320,7 @@ DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
     cbuf[ 2 ] = cbuf[ 3 ] = 0;
     cbuf[ 4 ] = atp_sockaddr( atp )->sat_port;
     cbuf[ 5 ] = oquantum;      /* flow quantum */
-    if ( gettimeofday( &stv, 0 ) < 0 ) {
+    if ( gettimeofday( &stv, NULL ) < 0 ) {
        perror( "gettimeofday" );
        exit( 2 );
     }
@@ -315,7 +328,7 @@ DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
        if ( cuts ) {
            waiting = 0xffff;
        } else {
-           if ( gettimeofday( &tv, 0 ) < 0 ) {
+           if ( gettimeofday( &tv, NULL ) < 0 ) {
                perror( "gettimeofday" );
                exit( 2 );
            }
@@ -333,7 +346,7 @@ DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
            exit( 1 );
        }
 
-DEBUG( printf( "OPEN >\n" ), fflush( stdout ));
+       if(debug){ printf( "OPEN >\n" ), fflush( stdout );}
 
        iov.iov_base = rbuf;
        iov.iov_len = sizeof( rbuf );
@@ -355,10 +368,10 @@ DEBUG( printf( "OPEN >\n" ), fflush( stdout ));
            continue;   /* This is weird, since TIDs must match... */
        }
 
-DEBUG( printf( "< OPENREPLY\n" ), fflush( stdout ));
+       if(debug){ printf( "< OPENREPLY\n" ), fflush( stdout );}
 
        if ( isatty( 1 )) {
-           printf( "%.*s\n", iov.iov_len - 9, (char *) iov.iov_base + 9 );
+           printf( "%.*s\n", (int)iov.iov_len - 9, (char *) iov.iov_base + 9 );
        }
        updatestatus( (char *) iov.iov_base + 9, iov.iov_len - 9 );
 
@@ -414,7 +427,7 @@ DEBUG( printf( "< OPENREPLY\n" ), fflush( stdout ));
        exit( 1 );
     }
 
-DEBUG( printf( "CLOSE >\n" ), fflush( stdout ));
+       if(debug){ printf( "CLOSE >\n" ), fflush( stdout );}
 
     iov.iov_base = rbuf;
     iov.iov_len = sizeof( rbuf );
@@ -439,9 +452,9 @@ DEBUG( printf( "CLOSE >\n" ), fflush( stdout ));
        fprintf( stderr, "Bad connid in close!\n" );
        exit( 1 );
     }
-#endif /*ZEROCONNID*/
+#endif /* ZEROCONNID */
 
-DEBUG( printf( "< CLOSEREPLY\n" ), fflush( stdout ));
+       if(debug){ printf( "< CLOSEREPLY\n" ), fflush( stdout );}
 
     if ( isatty( 1 )) {
        printf( "Connection closed.\n" );
@@ -449,14 +462,11 @@ DEBUG( printf( "< CLOSEREPLY\n" ), fflush( stdout ));
     exit( 0 );
 }
 
-int            data = 0;
-unsigned char  port;
-u_int16_t       seq = 0, rseq = 1;
+static int             data = 0;
+static unsigned char   port;
+static u_int16_t       seq = 0;
 
-send_file( fd, atp, lastfile )
-    int                        fd;
-    ATP                        atp;
-    int                        lastfile;
+static int send_file( int fd, ATP atp, int lastfile)
 {
     struct timeval     stv, tv;
     struct sockaddr_at ssat;
@@ -466,7 +476,7 @@ send_file( fd, atp, lastfile )
     int                        cc, i;
     unsigned short     netseq;
 
-    if ( gettimeofday( &stv, 0 ) < 0 ) {
+    if ( gettimeofday( &stv, NULL ) < 0 ) {
        perror( "gettimeofday" );
        exit( 2 );
     }
@@ -489,10 +499,10 @@ send_file( fd, atp, lastfile )
        exit( 1 );
     }
 
-DEBUG( printf( "READ %d >\n", seq ), fflush( stdout ));
+       if(debug){ printf( "READ %d >\n", seq ), fflush( stdout );}
 
     for (;;) {
-       if ( gettimeofday( &tv, 0 ) < 0 ) {
+       if ( gettimeofday( &tv, NULL ) < 0 ) {
            perror( "gettimeofday" );
            exit( 2 );
        }
@@ -516,7 +526,7 @@ DEBUG( printf( "READ %d >\n", seq ), fflush( stdout ));
                exit( 1 );
            }
 
-DEBUG( printf( "TICKLE >\n" ), fflush( stdout ));
+       if(debug){ printf( "TICKLE >\n" ), fflush( stdout );}
        }
 
        tv.tv_sec = stv.tv_sec + 60 - tv.tv_sec;
@@ -528,7 +538,7 @@ DEBUG( printf( "TICKLE >\n" ), fflush( stdout ));
        }
        FD_SET( atp_fileno( atp ), &fds );
 
-       if (( cc = select( FD_SETSIZE, &fds, 0, 0, &tv )) < 0 ) {
+       if (( cc = select( FD_SETSIZE, &fds, NULL, NULL, &tv )) < 0 ) {
            perror( "select" );
            exit( 2 );
        }
@@ -588,16 +598,16 @@ DEBUG( printf( "TICKLE >\n" ), fflush( stdout ));
                switch ( cbuf[ 1 ] ) {
                case PAP_READ :
                    memcpy( cbuf +  2, &netseq, sizeof( netseq ));
-DEBUG( printf( "< READ %d\n", ntohs( netseq )), fflush( stdout ));
+       if(debug){ printf( "< READ %d\n", ntohs( netseq )), fflush( stdout );}
 #ifdef notdef
                    if ( netseq != 0 ) {
                        if ( rseq != ntohs( netseq )) {
-DEBUG( printf( "| DUP %d\n", rseq ), fflush( stdout ));
+       if(debug){ printf( "| DUP %d\n", rseq ), fflush( stdout );}
                            break;
                        }
                        if ( rseq++ == 0xffff ) rseq = 1;
                    }
-#endif /*notdef*/
+#endif /* notdef */
 
                    data = 1;
                    port = ssat.sat_port;
@@ -605,7 +615,7 @@ DEBUG( printf( "| DUP %d\n", rseq ), fflush( stdout ));
 
                case PAP_CLOSE :
 
-DEBUG( printf( "< CLOSE\n" ), fflush( stdout ));
+       if(debug){ printf( "< CLOSE\n" ), fflush( stdout );}
 
                    /*
                     * Respond to the close request, and fail.
@@ -622,14 +632,14 @@ DEBUG( printf( "< CLOSE\n" ), fflush( stdout ));
                        exit( 1 );
                    }
 
-DEBUG( printf( "CLOSEREPLY >\n" ), fflush( stdout ));
+       if(debug){ printf( "CLOSEREPLY >\n" ), fflush( stdout );}
 
                    fprintf( stderr, "Connection closed by foreign host.\n" );
                    exit( 1 );
 
                case PAP_TICKLE :
 
-DEBUG( printf( "< TICKLE\n" ), fflush( stdout ));
+       if(debug){ printf( "< TICKLE\n" ), fflush( stdout );}
 
                    break;
                default :
@@ -662,7 +672,7 @@ DEBUG( printf( "< TICKLE\n" ), fflush( stdout ));
                    fprintf( stderr, "Bad data response!\n" );
                    exit( 1 );
                }
-#endif /*ZEROCONNID*/
+#endif /* ZEROCONNID */
                if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_DATA ) {
                    fprintf( stderr, "Bad data response!\n" );
                    exit( 1 );
@@ -680,12 +690,12 @@ DEBUG( printf( "< TICKLE\n" ), fflush( stdout ));
                /* eof */
                if ( ((char *)rniov[ 0 ].iov_base)[ 2 ] ) {
 
-DEBUG( printf( "< DATA (eof)\n" ), fflush( stdout ));
+       if(debug){ printf( "< DATA (eof)\n" ), fflush( stdout );}
 
                    return( 0 );
                }
 
-DEBUG( printf( "< DATA\n" ), fflush( stdout ));
+       if(debug){ printf( "< DATA\n" ), fflush( stdout );}
 
 
                /*
@@ -706,13 +716,13 @@ DEBUG( printf( "< DATA\n" ), fflush( stdout ));
                    exit( 1 );
                }
 
-DEBUG( printf( "READ %d >\n", seq ), fflush( stdout ));
+       if(debug){ printf( "READ %d >\n", seq ), fflush( stdout );}
 
                break;
 
            case 0:
 
-DEBUG( printf( "| RETRANS\n" ), fflush( stdout ));
+       if(debug){ printf( "| RETRANS\n" ), fflush( stdout );}
 
                break;
 
@@ -751,7 +761,7 @@ DEBUG( printf( "| RETRANS\n" ), fflush( stdout ));
            }
            data = fiovcnt = 0;
 
-DEBUG( printf( "DATA %s\n", eof ? "(eof) >" : ">" ), fflush( stdout ));
+       if(debug){ printf( "DATA %s\n", eof ? "(eof) >" : ">" ), fflush( stdout );}
 
            /*
             * The Apple LaserWriter IIf, the HP LWIIISi, and IV, don't
@@ -781,7 +791,7 @@ DEBUG( printf( "DATA %s\n", eof ? "(eof) >" : ">" ), fflush( stdout ));
                exit( 1 );
            }
 
-DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
+       if(debug){ printf( "SENDSTATUS >\n" ), fflush( stdout );}
 
            atpb.atp_saddr = &nn.nn_sat;
            rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
@@ -801,7 +811,7 @@ DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
                fprintf( stderr, "Bad status response!\n" );
                exit( 1 );
            }
-#endif /*NONZEROSTATUS*/
+#endif /* NONZEROSTATUS */
 
            if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
                    atpb.atp_rresiovcnt != 1 ) {
@@ -809,7 +819,7 @@ DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
                exit( 1 );
            }
 
-DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
+       if(debug){ printf( "< STATUS\n" ), fflush( stdout );}
 
 #ifdef FUCKED
            if ( waitforprinter ) {
@@ -817,12 +827,12 @@ DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
 
                memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9, 
                        ((char *)rniov[ 0 ].iov_base)[ 8 ] );
-               st_buf[ ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
+               st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
                if ( strstr( st_buf, "waiting" ) != NULL ) {
                    waitforprinter = 0;
                }
            }
-#endif /*FUCKED*/
+#endif /* FUCKED */
 
            updatestatus( (char *) rniov[ 0 ].iov_base + 9,
                    ((char *)rniov[ 0 ].iov_base)[ 8 ] );
@@ -830,26 +840,31 @@ DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
     }
 }
 
-updatestatus( s, len )
-    char       *s;
-    int                len;
+static void updatestatus(char *s, int len)
 {
-    int                        fd;
-    struct iovec       iov[ 2 ];
+    int                        fd = -1;
+    struct iovec       iov[ 3 ];
 
-    if ( !status ) {
-       return;
+    if ( status ) {
+       if (( fd = open( status, O_WRONLY|O_TRUNC )) < 0 ) {
+           perror( status );
+           status = NULL;
+       }
     }
 
-    if (( fd = open( status, O_WRONLY|O_TRUNC )) < 0 ) {
-       perror( status );
-       status = NULL;
-       return;
+    if ( fd < 0 ) {
+       fd = 2;
+    }
+
+    iov[ 0 ].iov_base = "%%[ ";
+    iov[ 0 ].iov_len = 4;
+    iov[ 1 ].iov_base = s;
+    iov[ 1 ].iov_len = len;
+    iov[ 2 ].iov_base = " ]%%\n";
+    iov[ 2 ].iov_len = 5;
+
+    writev( fd, iov, 3 );
+    if ( status ) {
+       close( fd );
     }
-    iov[ 0 ].iov_base = s;
-    iov[ 0 ].iov_len = len;
-    iov[ 1 ].iov_base = "\n";
-    iov[ 1 ].iov_len = 1;
-    writev( fd, iov, 2 );
-    close( fd );
 }