/* get a line from inpfd using nonbuffered input. The line is truncated if it is too
* long for the buffer. The result is left in lnbuf and the number of characters
* read in is returned.
*/
int
readline(int inpfd)
{
register char *ap;
register int i;
ap = lnbuf;
i = 0;
do {
if (read(inpfd, ap, 1) != 1) {
error(0, "read error in readline, fd=%d\n", inpfd);
break;
}
} while ((++i < LNBFSZ - 2) && *ap++ != '\n');
if (i == LNBFSZ - 2) {
*ap = '\n';
i++;
}
*ap = '\0';
return(i);
}
#define RDSIZE 512
char jobbuf[RDSIZE];
int
pass(int inpfd, int outfd, int bsize)
{
int bcnt = 0;
int rv = 0;
void
main(int argc, char *argv[])
{
char *devdir;
int i, rv, netfd, bsize, datafd;
#ifndef plan9
void (*oldhandler)();
#endif
/* make connection */
if (argc != 2) {
fprint(stderr, "usage: %s network!destination!service\n",
argv[0]);
exits("usage");
}
/* read options line from stdin into lnbuf */
i = readline(0);
/* read stdin into tempfile to get size */
datafd = tempfile();
bsize = prereadfile(datafd);
/* network connection is opened after data is in to avoid timeout */
if ((netfd = dial(argv[1], 0, 0, 0)) < 0) {
fprint(stderr, "dialing ");
perror(argv[1]);
exits("can't dial");
}
/* write out the options we read above */
if (write(netfd, lnbuf, i) != i) {
error(0, "write error while sending options\n");
exits("write error sending options");
}
/* send the size of the file to be sent */
sprint(lnbuf, "%d\n", bsize);
i = strlen(lnbuf);
if ((rv=write(netfd, lnbuf, i)) != i) {
perror("write error while sending size");
error(0, "write returned %d\n", rv);
exits("write error sending size");
}
if (seek(datafd, 0L, 0) < 0) {
error(0, "error seeking temp file\n");
exits("seek error");
}
/* mirror performance in readfile() in lpdaemon */
dbgstate = 1;
if(!recvACK(netfd)) {
error(0, "failed to receive ACK before sending data\n");
exits("recv ack1 failed");
}
dbgstate = 2;
if ((i=pass(datafd, netfd, bsize)) != 0) {
NAK(netfd);
error(0, "failed to send %d bytes\n", i);
exits("send data failed");
}
ACK(netfd);
dbgstate = 3;
if(!recvACK(netfd)) {
error(0, "failed to receive ACK after sending data\n");
exits("recv ack2 failed");
}
/* get response, as from lp -q */
dbgstate = 4;
while((rv=read(netfd, jobbuf, RDSIZE)) > 0) {
if((write(1, jobbuf, rv)) != rv) {
error(0, "write error while sending to stdout\n");
exits("write error while sending to stdout");
}
}
dbgstate = 5;
#ifdef plan9
atnotify(alarmhandler, 0);
/* close down network connections and go away */
exits("");
#else
signal(SIGALRM, oldhandler);
exit(0);
#endif
}