]> arthur.barton.de Git - netatalk.git/blob - bin/pap/pap.c
Warning fixes.
[netatalk.git] / bin / pap / pap.c
1 /*
2  * $Id: pap.c,v 1.7 2001-07-31 19:49:02 srittau Exp $
3  *
4  * Copyright (c) 1990,1994 Regents of The University of Michigan.
5  * All Rights Reserved.  See COPYRIGHT.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <sys/types.h>
13 #include <sys/time.h>
14 #include <sys/uio.h>
15 #include <netatalk/endian.h>
16 #include <netatalk/at.h>
17 #include <atalk/atp.h>
18 #include <atalk/pap.h>
19 #include <atalk/nbp.h>
20 #include <atalk/util.h>
21 #ifdef HAVE_FCNTL_H
22 #include <fcntl.h>
23 #endif /* HAVE_FCNTL_H */
24 #include <stdio.h>
25 #include <string.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <errno.h>
29
30 #define FUCKED
31
32 #define _PATH_PAPRC     ".paprc"
33 char    *nbpfailure = "AppleTalk printer offline";
34
35 /* Forward Declarations */
36 void updatestatus(char *s, int len);
37 int send_file(int fd, ATP atp, int lastfile);
38
39 /* if there is a less hacky way to do this, please do it... */
40 #ifdef DEBUG
41 #define EBUG
42 #endif /* DEBUG */
43
44 #undef DEBUG
45
46 #ifdef EBUG
47 #define DEBUG(x,y)      (x,y)
48 #else /*EBUG*/
49 #define DEBUG(x,y)
50 #endif /*EBUG*/
51
52 void usage( path )
53     char        *path;
54 {
55     char        *p;
56
57     if (( p = strrchr( path, '/' )) == NULL ) {
58         p = path;
59     } else {
60         p++;
61     }
62     fprintf( stderr,
63         "Usage:\t%s [ -A address ] [ -p printername ] [ -s statusfile ] [ file ] ...\n", p );
64     exit( 2 );
65 }
66
67 char *
68 paprc()
69 {
70     static char s[ 32 + 1 + 32 + 1 + 32 ];
71     char        *name = NULL;
72     FILE        *f;
73
74     if (( f = fopen( _PATH_PAPRC, "r" )) == NULL ) {
75         if ( errno == ENOENT ) {
76             return( NULL );
77         } else {
78             perror( _PATH_PAPRC );
79             exit( 2 );
80         }
81     }
82     while ( fgets( s, sizeof( s ), f ) != NULL ) {
83         s[ strlen( s ) - 1 ] = '\0';    /* remove trailing newline */
84         if ( *s == '#' ) {
85             continue;
86         }
87         name = s;
88         break;
89     }
90     fclose( f );
91     return( name );
92 }
93
94 char                    *printer = NULL;
95 char                    *status = NULL;
96 int                     noeof = 0;
97 int                     waitforprinter = 0;
98
99 unsigned char           connid, quantum, oquantum = PAP_MAXQUANTUM;
100 struct sockaddr_at      sat;
101
102 char                    cbuf[ 8 ];
103 struct nbpnve           nn;
104 ATP                     satp;
105
106 char            fbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
107 struct iovec    rfiov[ PAP_MAXQUANTUM ] = {
108     { fbuf[ 0 ] + 4,    0 },
109     { fbuf[ 1 ] + 4,    0 },
110     { fbuf[ 2 ] + 4,    0 },
111     { fbuf[ 3 ] + 4,    0 },
112     { fbuf[ 4 ] + 4,    0 },
113     { fbuf[ 5 ] + 4,    0 },
114     { fbuf[ 6 ] + 4,    0 },
115     { fbuf[ 7 ] + 4,    0 },
116 };
117 struct iovec    sniov[ PAP_MAXQUANTUM ] = {
118     { fbuf[ 0 ],        0 },
119     { fbuf[ 1 ],        0 },
120     { fbuf[ 2 ],        0 },
121     { fbuf[ 3 ],        0 },
122     { fbuf[ 4 ],        0 },
123     { fbuf[ 5 ],        0 },
124     { fbuf[ 6 ],        0 },
125     { fbuf[ 7 ],        0 },
126 };
127
128 char            nbuf[ PAP_MAXQUANTUM ][ 4 + PAP_MAXDATA ];
129 struct iovec    rniov[ PAP_MAXQUANTUM ] = {
130     { nbuf[ 0 ],        0 },
131     { nbuf[ 1 ],        0 },
132     { nbuf[ 2 ],        0 },
133     { nbuf[ 3 ],        0 },
134     { nbuf[ 4 ],        0 },
135     { nbuf[ 5 ],        0 },
136     { nbuf[ 6 ],        0 },
137     { nbuf[ 7 ],        0 },
138 };
139 struct iovec    sfiov[ PAP_MAXQUANTUM ] = {
140     { nbuf[ 0 ] + 4,    0 },
141     { nbuf[ 1 ] + 4,    0 },
142     { nbuf[ 2 ] + 4,    0 },
143     { nbuf[ 3 ] + 4,    0 },
144     { nbuf[ 4 ] + 4,    0 },
145     { nbuf[ 5 ] + 4,    0 },
146     { nbuf[ 6 ] + 4,    0 },
147     { nbuf[ 7 ] + 4,    0 },
148 };
149
150 int main( ac, av )
151     int         ac;
152     char        **av;
153 {
154     ATP                 atp;
155     struct atp_block    atpb;
156     int                 c, err = 0, fd, cuts = 0;
157     char                *obj = NULL, *type = "LaserWriter", *zone = "*";
158     struct timeval      stv, tv;
159     char                rbuf[ ATP_MAXDATA ];
160     struct iovec        iov;
161     unsigned short      waiting, result;
162     int                 connattempts = 10;
163     int                 waitforidle = 0;
164     struct at_addr      addr;
165
166     extern char         *optarg;
167     extern int          optind;
168
169     memset(&addr, 0, sizeof(addr));
170     while (( c = getopt( ac, av, "Wwcep:s:EA:" )) != EOF ) {
171         switch ( c ) {
172 #ifdef FUCKED
173         case 'w' :
174             waitforprinter = 1;
175             break;
176
177         case 'W' :
178             waitforidle = 1;
179             break;
180 #endif /* FUCKED */
181
182         case 'c' :
183             cuts++;
184             break;
185
186         case 'e' :      /* send stdout to stderr */
187             dup2( 2, 1 );
188             break;
189
190         case 'p' :
191             printer = optarg;
192             break;
193
194         case 's' :
195             status = optarg;
196             break;
197
198         case 'E' :
199             noeof = 1;
200             break;
201            
202         case 'A':
203             if (!atalk_aton(optarg, &addr)) {
204               fprintf(stderr, "Bad address.\n");
205               exit(1);
206             }
207             break;
208
209         default :
210             err++;
211         }
212     }
213     if ( err ) {
214         usage( *av );
215     }
216     if ( printer == NULL && (( printer = paprc()) == NULL )) {
217         fprintf( stderr, "No printer specified and ./.paprc not found.\n" );
218         exit( 2 );
219     }
220
221     /*
222      * Open connection.
223      */
224     if ( nbp_name( printer, &obj, &type, &zone ) < 0 ) {
225         fprintf( stderr, "%s: Bad name\n", printer );
226         exit( 2 );
227     }
228     if ( obj == NULL ) {
229         fprintf( stderr, "%s: Bad name\n", printer );
230         exit( 2 );
231     }
232
233     if ( nbp_lookup( obj, type, zone, &nn, 1, &addr ) <= 0 ) {
234         if ( errno != 0 ) {
235             perror( "nbp_lookup" );
236             exit( 2 );
237         }
238         fprintf( stderr, "%s:%s@%s: NBP Lookup failed\n", obj, type, zone );
239         exit( 1 );
240     }
241
242     if ( isatty( 0 )) {
243         printf( "Trying %u.%d:%d ...\n", ntohs( nn.nn_sat.sat_addr.s_net ),
244                 nn.nn_sat.sat_addr.s_node, nn.nn_sat.sat_port );
245     }
246
247     if (( atp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
248         perror( "atp_open" );
249         exit( 2 );
250     }
251     if (( satp = atp_open( ATADDR_ANYPORT, &addr )) == NULL ) {
252         perror( "atp_open" );
253         exit( 2 );
254     }
255
256     while ( waitforidle ) {
257         char    st_buf[ 1024 ]; /* XXX too big */
258
259         cbuf[ 0 ] = 0;
260         cbuf[ 1 ] = PAP_SENDSTATUS;
261         cbuf[ 2 ] = cbuf[ 3 ] = 0;
262         atpb.atp_saddr = &nn.nn_sat;
263         atpb.atp_sreqdata = cbuf;
264         atpb.atp_sreqdlen = 4;  /* bytes in SendStatus request */
265         atpb.atp_sreqto = 2;            /* retry timer */
266         atpb.atp_sreqtries = 5;         /* retry count */
267         if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
268             perror( "atp_sreq" );
269             exit( 1 );
270         }
271
272 DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
273
274         atpb.atp_saddr = &nn.nn_sat;
275         rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
276         atpb.atp_rresiov = rniov;
277         atpb.atp_rresiovcnt = 1;
278         if ( atp_rresp( satp, &atpb ) < 0 ) {
279             perror( "atp_rresp" );
280             continue;
281         }
282
283 #ifndef NONZEROSTATUS
284         /*
285          * The stinking LaserWriter IINTX puts crap in this
286          * field.
287          */
288         if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
289             fprintf( stderr, "Bad status response!\n" );
290             exit( 1 );
291         }
292 #endif /* NONZEROSTATUS */
293
294         if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
295                 atpb.atp_rresiovcnt != 1 ) {
296             fprintf( stderr, "Bad status response!\n" );
297             exit( 1 );
298         }
299
300 DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
301
302         memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9, 
303                 ((char *)rniov[ 0 ].iov_base)[ 8 ] );
304         st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
305         if ( strstr( st_buf, "idle" ) != NULL ) {
306             waitforidle = 0;
307         } else {
308             updatestatus( (char *) rniov[ 0 ].iov_base + 9,
309                     ((char *)rniov[ 0 ].iov_base)[ 8 ] );
310             sleep( 5 );
311         }
312     }
313
314     cbuf[ 0 ] = connid = getpid() & 0xff;
315     cbuf[ 1 ] = PAP_OPEN;
316     cbuf[ 2 ] = cbuf[ 3 ] = 0;
317     cbuf[ 4 ] = atp_sockaddr( atp )->sat_port;
318     cbuf[ 5 ] = oquantum;       /* flow quantum */
319     if ( gettimeofday( &stv, 0 ) < 0 ) {
320         perror( "gettimeofday" );
321         exit( 2 );
322     }
323     for (;;) {
324         if ( cuts ) {
325             waiting = 0xffff;
326         } else {
327             if ( gettimeofday( &tv, 0 ) < 0 ) {
328                 perror( "gettimeofday" );
329                 exit( 2 );
330             }
331             waiting = htons( tv.tv_sec - stv.tv_sec );
332         }
333         memcpy(cbuf +  6, &waiting, sizeof( waiting ));
334
335         atpb.atp_saddr = &nn.nn_sat;
336         atpb.atp_sreqdata = cbuf;
337         atpb.atp_sreqdlen = 8;          /* bytes in OpenConn request */
338         atpb.atp_sreqto = 2;            /* retry timer */
339         atpb.atp_sreqtries = 5;         /* retry count */
340         if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
341             perror( "atp_sreq" );
342             exit( 1 );
343         }
344
345 DEBUG( printf( "OPEN >\n" ), fflush( stdout ));
346
347         iov.iov_base = rbuf;
348         iov.iov_len = sizeof( rbuf );
349         atpb.atp_rresiov = &iov;
350         atpb.atp_rresiovcnt = 1;
351         if ( atp_rresp( atp, &atpb ) < 0 ) {
352             perror( "atp_rresp" );
353             if ( connattempts-- <= 0 ) {
354                 fprintf( stderr, "Can't connect!\n" );
355                 exit( 1 );
356             }
357             continue;
358         }
359
360         /* sanity */
361         if ( iov.iov_len < 8 || (unsigned char)rbuf[ 0 ] != connid ||
362                 rbuf[ 1 ] != PAP_OPENREPLY ) {
363             fprintf( stderr, "Bad response!\n" );
364             continue;   /* This is weird, since TIDs must match... */
365         }
366
367 DEBUG( printf( "< OPENREPLY\n" ), fflush( stdout ));
368
369         if ( isatty( 1 )) {
370             printf( "%.*s\n", (int)iov.iov_len - 9, (char *) iov.iov_base + 9 );
371         }
372         updatestatus( (char *) iov.iov_base + 9, iov.iov_len - 9 );
373
374         memcpy( &result, rbuf +  6,  sizeof( result ));
375         if ( result != 0 ) {
376             sleep( 2 );
377         } else {
378             memcpy( &sat, &nn.nn_sat, sizeof( struct sockaddr_at ));
379             sat.sat_port = rbuf[ 4 ];
380             quantum = rbuf[ 5 ];
381             break;
382         }
383     }
384
385     if ( isatty( 1 )) {
386         printf( "Connected to %.*s:%.*s@%.*s.\n",
387                 nn.nn_objlen, nn.nn_obj,
388                 nn.nn_typelen, nn.nn_type,
389                 nn.nn_zonelen, nn.nn_zone );
390     }
391
392     if ( optind == ac ) {
393         send_file( 0, atp, 1 );
394     } else {
395         for (; optind < ac; optind++ ) {
396             if ( strcmp( av[ optind ], "-" ) == 0 ) {
397                 fd = 0;
398             } else if (( fd = open( av[ optind ], O_RDONLY )) < 0 ) {
399                 perror( av[ optind ] );
400                 continue;
401             }
402             send_file( fd, atp, ( optind == ac - 1 ) ? 1 : 0 );
403             if ( fd != 0 ) {
404                 close( fd );
405             }
406         }
407     }
408
409     /*
410      * Close connection.
411      */
412     cbuf[ 0 ] = connid;
413     cbuf[ 1 ] = PAP_CLOSE;
414     cbuf[ 2 ] = cbuf[ 3 ] = 0;
415
416     atpb.atp_saddr = &sat;
417     atpb.atp_sreqdata = cbuf;
418     atpb.atp_sreqdlen = 4;              /* bytes in CloseConn request */
419     atpb.atp_sreqto = 2;                /* retry timer */
420     atpb.atp_sreqtries = 5;             /* retry count */
421     if ( atp_sreq( atp, &atpb, 1, ATP_XO ) < 0 ) {
422         perror( "atp_sreq" );
423         exit( 1 );
424     }
425
426 DEBUG( printf( "CLOSE >\n" ), fflush( stdout ));
427
428     iov.iov_base = rbuf;
429     iov.iov_len = sizeof( rbuf );
430     atpb.atp_rresiov = &iov;
431     atpb.atp_rresiovcnt = 1;
432     if ( atp_rresp( atp, &atpb ) < 0 ) {
433         perror( "atp_rresp" );
434         exit( 1 );
435     }
436
437     /* sanity */
438     if ( iov.iov_len != 4 || rbuf[ 1 ] != PAP_CLOSEREPLY ) {
439         fprintf( stderr, "Bad response!\n" );
440         exit( 1 );
441     }
442
443 #ifndef ZEROCONNID
444     /*
445      * The AGFA Viper Rip doesn't have the connection id in the close request.
446      */
447     if ((unsigned char)rbuf[ 0 ] != connid ) {
448         fprintf( stderr, "Bad connid in close!\n" );
449         exit( 1 );
450     }
451 #endif /* ZEROCONNID */
452
453 DEBUG( printf( "< CLOSEREPLY\n" ), fflush( stdout ));
454
455     if ( isatty( 1 )) {
456         printf( "Connection closed.\n" );
457     }
458     exit( 0 );
459 }
460
461 int             data = 0;
462 unsigned char   port;
463 u_int16_t       seq = 0, rseq = 1;
464
465 int send_file( fd, atp, lastfile )
466     int                 fd;
467     ATP                 atp;
468     int                 lastfile;
469 {
470     struct timeval      stv, tv;
471     struct sockaddr_at  ssat;
472     struct atp_block    atpb;
473     fd_set              fds;
474     int                 fiovcnt = 0, eof = 0, senteof = 0, to = 0;
475     int                 cc, i;
476     unsigned short      netseq;
477
478     if ( gettimeofday( &stv, 0 ) < 0 ) {
479         perror( "gettimeofday" );
480         exit( 2 );
481     }
482
483     /*
484      * Ask for more data.
485      */
486     cbuf[ 0 ] = connid;
487     cbuf[ 1 ] = PAP_READ;
488     if ( ++seq == 0 ) seq = 1;
489     netseq = htons( seq );
490     memcpy( cbuf +  2, &netseq, sizeof( netseq ));
491     atpb.atp_saddr = &sat;
492     atpb.atp_sreqdata = cbuf;
493     atpb.atp_sreqdlen = 4;              /* bytes in SendData request */
494     atpb.atp_sreqto = 15;               /* retry timer */
495     atpb.atp_sreqtries = -1;            /* retry count */
496     if ( atp_sreq( atp, &atpb, oquantum, ATP_XO ) < 0 ) {
497         perror( "atp_sreq" );
498         exit( 1 );
499     }
500
501 DEBUG( printf( "READ %d >\n", seq ), fflush( stdout ));
502
503     for (;;) {
504         if ( gettimeofday( &tv, 0 ) < 0 ) {
505             perror( "gettimeofday" );
506             exit( 2 );
507         }
508
509         if (( tv.tv_sec - stv.tv_sec ) >= 60 ) {
510             stv = tv;
511
512             /*
513              * Send a tickle.
514              */
515             cbuf[ 0 ] = connid;
516             cbuf[ 1 ] = PAP_TICKLE;
517             cbuf[ 2 ] = cbuf[ 3 ] = 0;
518             atpb.atp_saddr = &sat;
519             atpb.atp_sreqdata = cbuf;
520             atpb.atp_sreqdlen = 4;              /* bytes in Tickle request */
521             atpb.atp_sreqto = 0;                /* retry timer */
522             atpb.atp_sreqtries = 1;             /* retry count */
523             if ( atp_sreq( satp, &atpb, 0, 0 ) < 0 ) {
524                 perror( "atp_sreq" );
525                 exit( 1 );
526             }
527
528 DEBUG( printf( "TICKLE >\n" ), fflush( stdout ));
529         }
530
531         tv.tv_sec = stv.tv_sec + 60 - tv.tv_sec;
532         tv.tv_usec = 0;
533
534         FD_ZERO( &fds );
535         if ( !waitforprinter && !eof && fiovcnt == 0 ) {
536             FD_SET( fd, &fds );
537         }
538         FD_SET( atp_fileno( atp ), &fds );
539
540         if (( cc = select( FD_SETSIZE, &fds, 0, 0, &tv )) < 0 ) {
541             perror( "select" );
542             exit( 2 );
543         }
544
545         /*
546          * A timeout has occured. Keep track of it.
547          */
548         if ( cc == 0 ) {
549             if ( to++ > 2 ) {
550                 fprintf( stderr, "Connection timed out.\n" );
551                 exit( 1 );
552             }
553             continue;
554         }
555
556         /*
557          * Read data.
558          */
559         if ( !fiovcnt && FD_ISSET( fd, &fds )) {
560             for ( i = 0; i < quantum; i++ ) {
561                 rfiov[ i ].iov_len = PAP_MAXDATA;
562             }
563             if (( cc = readv( fd, rfiov, quantum )) < 0 ) {
564                 perror( "readv" );
565                 exit( 2 );
566             }
567             if ( cc == 0 ) {
568                 eof = 1;
569             }
570             fiovcnt = cc / PAP_MAXDATA + ( cc % PAP_MAXDATA > 0 );
571             for ( i = 0; cc > 0; i++ ) {
572                 rfiov[ i ].iov_len = ( cc > PAP_MAXDATA ) ? PAP_MAXDATA : cc;
573                 cc -= ( cc > PAP_MAXDATA ) ? PAP_MAXDATA : cc;
574             }
575         }
576
577         if ( FD_ISSET( atp_fileno( atp ), &fds )) {
578             ssat = sat;
579             ssat.sat_port = ATADDR_ANYPORT;
580             switch( atp_rsel( atp, &ssat, ATP_TRESP | ATP_TREQ )) {
581             case ATP_TREQ :
582                 atpb.atp_saddr = &ssat;
583                 atpb.atp_rreqdata = cbuf;
584                 atpb.atp_rreqdlen = sizeof( cbuf );
585                 if ( atp_rreq( atp, &atpb ) < 0 ) {
586                     perror( "atp_rreq" );
587                     exit( 1 );
588                 }
589
590                 if ( (unsigned char)cbuf[ 0 ] != connid ) {
591                     break;
592                 }
593
594                 /* reset timeout counter for all valid requests */
595                 to = 0;
596
597                 switch ( cbuf[ 1 ] ) {
598                 case PAP_READ :
599                     memcpy( cbuf +  2, &netseq, sizeof( netseq ));
600 DEBUG( printf( "< READ %d\n", ntohs( netseq )), fflush( stdout ));
601 #ifdef notdef
602                     if ( netseq != 0 ) {
603                         if ( rseq != ntohs( netseq )) {
604 DEBUG( printf( "| DUP %d\n", rseq ), fflush( stdout ));
605                             break;
606                         }
607                         if ( rseq++ == 0xffff ) rseq = 1;
608                     }
609 #endif /* notdef */
610
611                     data = 1;
612                     port = ssat.sat_port;
613                     break;
614
615                 case PAP_CLOSE :
616
617 DEBUG( printf( "< CLOSE\n" ), fflush( stdout ));
618
619                     /*
620                      * Respond to the close request, and fail.
621                      */
622                     sniov[ 0 ].iov_len = 4;
623                     ((char *)sniov[ 0 ].iov_base)[ 0 ] = connid;
624                     ((char *)sniov[ 0 ].iov_base)[ 1 ] = PAP_CLOSEREPLY;
625                     ((char *)sniov[ 0 ].iov_base)[ 2 ] =
626                             ((char *)sniov[ 0 ].iov_base)[ 3 ] = 0;
627                     atpb.atp_sresiov = sniov;
628                     atpb.atp_sresiovcnt = 1;
629                     if ( atp_sresp( atp, &atpb ) < 0 ) {
630                         perror( "atp_sresp" );
631                         exit( 1 );
632                     }
633
634 DEBUG( printf( "CLOSEREPLY >\n" ), fflush( stdout ));
635
636                     fprintf( stderr, "Connection closed by foreign host.\n" );
637                     exit( 1 );
638
639                 case PAP_TICKLE :
640
641 DEBUG( printf( "< TICKLE\n" ), fflush( stdout ));
642
643                     break;
644                 default :
645                     fprintf( stderr, "Bad PAP request!\n" );
646                     exit( 1 );
647                 }
648                 break;
649
650             case ATP_TRESP :
651                 /* reset timeout counter for all valid requests */
652                 to = 0;
653
654                 atpb.atp_saddr = &ssat;
655                 for ( i = 0; i < oquantum; i++ ) {
656                     rniov[ i ].iov_len = PAP_MAXDATA + 4;
657                 }
658                 atpb.atp_rresiov = rniov;
659                 atpb.atp_rresiovcnt = oquantum;
660                 if ( atp_rresp( atp, &atpb ) < 0 ) {
661                     perror( "atp_rresp" );
662                     exit( 1 );
663                 }
664
665 #ifndef ZEROCONNID
666                 /*
667                  * The HP LJIIISI w/ BridgePort LocalTalk card sends
668                  * zero instead of the connid.
669                  */
670                 if ( ((unsigned char *)rniov[ 0 ].iov_base)[ 0 ] != connid ) {
671                     fprintf( stderr, "Bad data response!\n" );
672                     exit( 1 );
673                 }
674 #endif /* ZEROCONNID */
675                 if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_DATA ) {
676                     fprintf( stderr, "Bad data response!\n" );
677                     exit( 1 );
678                 }
679
680                 for ( cc = 0, i = 0; i < atpb.atp_rresiovcnt; i++ ) {
681                     sfiov[ i ].iov_len = rniov[ i ].iov_len - 4;
682                     cc += sfiov[ i ].iov_len;
683                 }
684                 if ( cc && writev( 1, sfiov, atpb.atp_rresiovcnt ) < cc ) {
685                     perror( "writev" );
686                     exit( 2 );
687                 }
688
689                 /* eof */
690                 if ( ((char *)rniov[ 0 ].iov_base)[ 2 ] ) {
691
692 DEBUG( printf( "< DATA (eof)\n" ), fflush( stdout ));
693
694                     return( 0 );
695                 }
696
697 DEBUG( printf( "< DATA\n" ), fflush( stdout ));
698
699
700                 /*
701                  * Ask for more data.
702                  */
703                 cbuf[ 0 ] = connid;
704                 cbuf[ 1 ] = PAP_READ;
705                 if ( ++seq == 0 ) seq = 1;
706                 netseq = htons( seq );
707                 memcpy( cbuf +  2, &netseq, sizeof( netseq ));
708                 atpb.atp_saddr = &sat;
709                 atpb.atp_sreqdata = cbuf;
710                 atpb.atp_sreqdlen = 4;          /* bytes in SendData request */
711                 atpb.atp_sreqto = 15;           /* retry timer */
712                 atpb.atp_sreqtries = -1;        /* retry count */
713                 if ( atp_sreq( atp, &atpb, oquantum, ATP_XO ) < 0 ) {
714                     perror( "atp_sreq" );
715                     exit( 1 );
716                 }
717
718 DEBUG( printf( "READ %d >\n", seq ), fflush( stdout ));
719
720                 break;
721
722             case 0:
723
724 DEBUG( printf( "| RETRANS\n" ), fflush( stdout ));
725
726                 break;
727
728             default:
729                 perror( "atp_rsel" );
730                 exit( 1 );
731             }
732         }
733
734         /*
735          * Send whatever is pending.
736          */
737         if ( !waitforprinter && !senteof && data && ( fiovcnt || eof )) {
738             ssat.sat_port = port;
739             atpb.atp_saddr = &ssat;
740             if ( fiovcnt ) {
741                 for ( i = 0; i < fiovcnt; i++ ) {
742                     sniov[ i ].iov_len = rfiov[ i ].iov_len + 4;
743                     ((char *)sniov[ i ].iov_base)[ 0 ] = connid;
744                     ((char *)sniov[ i ].iov_base)[ 1 ] = PAP_DATA;
745                     senteof = ((char *)sniov[ i ].iov_base)[ 2 ] = eof;
746                     ((char *)sniov[ i ].iov_base)[ 3 ] = 0;
747                 }
748             } else {
749                 sniov[ 0 ].iov_len = 4;
750                 ((char *)sniov[ 0 ].iov_base)[ 0 ] = connid;
751                 ((char *)sniov[ 0 ].iov_base)[ 1 ] = PAP_DATA;
752                 senteof = ((char *)sniov[ 0 ].iov_base)[ 2 ] = eof;
753                 ((char *)sniov[ 0 ].iov_base)[ 3 ] = 0;
754             }
755             atpb.atp_sresiov = sniov;
756             atpb.atp_sresiovcnt = fiovcnt ? fiovcnt : 1;
757             if ( atp_sresp( atp, &atpb ) < 0 ) {
758                 perror( "atp_sresp" );
759                 exit( 1 );
760             }
761             data = fiovcnt = 0;
762
763 DEBUG( printf( "DATA %s\n", eof ? "(eof) >" : ">" ), fflush( stdout ));
764
765             /*
766              * The Apple LaserWriter IIf, the HP LWIIISi, and IV, don't
767              * seem to send us an EOF on large jobs.  To work around
768              * this heinous protocol violation, we won't wait for their
769              * EOF before closing.
770              */
771             if ( eof && noeof && lastfile ) {
772                 return( 0 );
773             }
774         } else {
775             /*
776              * If we can't send data right now, go ahead and get the
777              * status. This is cool, because we get here reliably
778              * if there is a problem.
779              */
780             cbuf[ 0 ] = 0;
781             cbuf[ 1 ] = PAP_SENDSTATUS;
782             cbuf[ 2 ] = cbuf[ 3 ] = 0;
783             atpb.atp_saddr = &nn.nn_sat;
784             atpb.atp_sreqdata = cbuf;
785             atpb.atp_sreqdlen = 4;      /* bytes in SendStatus request */
786             atpb.atp_sreqto = 2;                /* retry timer */
787             atpb.atp_sreqtries = 5;             /* retry count */
788             if ( atp_sreq( satp, &atpb, 1, 0 ) < 0 ) {
789                 perror( "atp_sreq" );
790                 exit( 1 );
791             }
792
793 DEBUG( printf( "SENDSTATUS >\n" ), fflush( stdout ));
794
795             atpb.atp_saddr = &nn.nn_sat;
796             rniov[ 0 ].iov_len = PAP_MAXDATA + 4;
797             atpb.atp_rresiov = rniov;
798             atpb.atp_rresiovcnt = 1;
799             if ( atp_rresp( satp, &atpb ) < 0 ) {
800                 perror( "atp_rresp" );
801                 continue;
802             }
803
804 #ifndef NONZEROSTATUS
805             /*
806              * The stinking LaserWriter IINTX puts crap in this
807              * field.
808              */
809             if ( ((char *)rniov[ 0 ].iov_base)[ 0 ] != 0 ) {
810                 fprintf( stderr, "Bad status response!\n" );
811                 exit( 1 );
812             }
813 #endif /* NONZEROSTATUS */
814
815             if ( ((char *)rniov[ 0 ].iov_base)[ 1 ] != PAP_STATUS ||
816                     atpb.atp_rresiovcnt != 1 ) {
817                 fprintf( stderr, "Bad status response!\n" );
818                 exit( 1 );
819             }
820
821 DEBUG( printf( "< STATUS\n" ), fflush( stdout ));
822
823 #ifdef FUCKED
824             if ( waitforprinter ) {
825                 char    st_buf[ 1024 ]; /* XXX too big */
826
827                 memcpy( st_buf, (char *) rniov[ 0 ].iov_base + 9, 
828                         ((char *)rniov[ 0 ].iov_base)[ 8 ] );
829                 st_buf[ (int) ((char *)rniov[ 0 ].iov_base)[ 8 ]] = '\0';
830                 if ( strstr( st_buf, "waiting" ) != NULL ) {
831                     waitforprinter = 0;
832                 }
833             }
834 #endif /* FUCKED */
835
836             updatestatus( (char *) rniov[ 0 ].iov_base + 9,
837                     ((char *)rniov[ 0 ].iov_base)[ 8 ] );
838         }
839     }
840 }
841
842 void updatestatus( s, len )
843     char        *s;
844     int         len;
845 {
846     int                 fd;
847     struct iovec        iov[ 2 ];
848
849     if ( !status ) {
850         return;
851     }
852
853     if (( fd = open( status, O_WRONLY|O_TRUNC )) < 0 ) {
854         perror( status );
855         status = NULL;
856         return;
857     }
858     iov[ 0 ].iov_base = s;
859     iov[ 0 ].iov_len = len;
860     iov[ 1 ].iov_base = "\n";
861     iov[ 1 ].iov_len = 1;
862     writev( fd, iov, 2 );
863     close( fd );
864 }