+ tof = !pgetflag("fo");
+}
+
+/*
+ * Acquire line printer or remote connection.
+ */
+openpr()
+{
+ register int i, n;
+ int resp;
+
+ if (!sendtorem && *LP) {
+ for (i = 1; ; i = i < 32 ? i << 1 : i) {
+ pfd = open(LP, RW ? O_RDWR : O_WRONLY);
+ if (pfd >= 0)
+ break;
+ if (errno == ENOENT) {
+ syslog(LOG_ERR, "%s: %m", LP);
+ exit(1);
+ }
+ if (i == 1)
+ status("waiting for %s to become ready (offline ?)", printer);
+ sleep(i);
+ }
+ if (isatty(pfd))
+ setty();
+ status("%s is ready and printing", printer);
+ } else if (RM != NULL) {
+ for (i = 1; ; i = i < 256 ? i << 1 : i) {
+ resp = -1;
+ pfd = getport(RM);
+ if (pfd >= 0) {
+ (void) sprintf(line, "\2%s\n", RP);
+ n = strlen(line);
+ if (write(pfd, line, n) == n &&
+ (resp = response()) == '\0')
+ break;
+ (void) close(pfd);
+ }
+ if (i == 1) {
+ if (resp < 0)
+ status("waiting for %s to come up", RM);
+ else {
+ status("waiting for queue to be enabled on %s", RM);
+ i = 256;
+ }
+ }
+ sleep(i);
+ }
+ status("sending to %s", RM);
+ remote = 1;
+ } else {
+ syslog(LOG_ERR, "%s: no line printer device or host name",
+ printer);
+ exit(1);
+ }
+ /*
+ * Start up an output filter, if needed.
+ */
+ if (!remote && OF) {
+ int p[2];
+ char *cp;
+
+ pipe(p);
+ if ((ofilter = dofork(DOABORT)) == 0) { /* child */
+ dup2(p[0], 0); /* pipe is std in */
+ dup2(pfd, 1); /* printer is std out */
+ for (i = 3; i < NOFILE; i++)
+ (void) close(i);
+ if ((cp = rindex(OF, '/')) == NULL)
+ cp = OF;
+ else
+ cp++;
+ execl(OF, cp, width, length, 0);
+ syslog(LOG_ERR, "%s: %s: %m", printer, OF);
+ exit(1);
+ }
+ (void) close(p[0]); /* close input side */
+ ofd = p[1]; /* use pipe for output */
+ } else {
+ ofd = pfd;
+ ofilter = 0;
+ }