int /* O - 0 on success, -1 on error */ backendDrainOutput(int print_fd, /* I - Print file descriptor */ int device_fd) /* I - Device file descriptor */ { int nfds; /* Maximum file descriptor value + 1 */ fd_set input; /* Input set for reading */ ssize_t print_bytes, /* Print bytes read */ bytes; /* Bytes written */ char print_buffer[8192], /* Print data buffer */ *print_ptr; /* Pointer into print data buffer */ struct timeval timeout; /* Timeout for read... */ fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n", print_fd, device_fd); /* * Figure out the maximum file descriptor value to use with select()... */ nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; /* * Now loop until we are out of data from print_fd... */ for (;;) { /* * Use select() to determine whether we have data to copy around... */ FD_ZERO(&input); FD_SET(print_fd, &input); timeout.tv_sec = 0; timeout.tv_usec = 0; if (select(nfds, &input, NULL, NULL, &timeout) < 0) return (-1); if (!FD_ISSET(print_fd, &input)) return (0); if ((print_bytes = read(print_fd, print_buffer, sizeof(print_buffer))) < 0) { /* * Read error - bail if we don't see EAGAIN or EINTR... */ if (errno != EAGAIN || errno != EINTR) { fprintf(stderr, "DEBUG: Read failed: %s\n", strerror(errno)); _cupsLangPrintFilter(stderr, "ERROR", _("Unable to read print data.")); return (-1); } print_bytes = 0; } else if (print_bytes == 0) { /* * End of file, return... */ return (0); } fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", (int)print_bytes); for (print_ptr = print_buffer; print_bytes > 0;) { if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) { /* * Write error - bail if we don't see an error we can retry... */ if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN && errno != EINTR && errno != ENOTTY) { _cupsLangPrintError("ERROR", _("Unable to write print data")); return (-1); } } else { fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); print_bytes -= bytes; print_ptr += bytes; } } } }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { int status = 0; /* Exit status */ cups_file_t *fp; /* Command file */ char line[1024], /* Line from file */ *value; /* Value on line */ int linenum; /* Line number in file */ ppd_file_t *ppd; /* PPD file */ /* * Check for valid arguments... */ if (argc < 6 || argc > 7) { /* * We don't have the correct number of arguments; write an error message * and return. */ _cupsLangPrintf(stderr, _("Usage: %s job-id user title copies options [file]"), argv[0]); return (1); } /* * Open the PPD file... */ if ((ppd = ppdOpenFile(getenv("PPD"))) == NULL) { fputs("ERROR: Unable to open PPD file!\n", stderr); return (1); } /* * Open the command file as needed... */ if (argc == 7) { if ((fp = cupsFileOpen(argv[6], "r")) == NULL) { perror("ERROR: Unable to open command file - "); return (1); } } else fp = cupsFileStdin(); /* * Read the commands from the file and send the appropriate commands... */ linenum = 0; while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) { /* * Parse the command... */ if (!_cups_strcasecmp(line, "AutoConfigure")) status |= auto_configure(ppd, argv[2]); else if (!_cups_strcasecmp(line, "PrintSelfTestPage")) print_self_test_page(ppd, argv[2]); else if (!_cups_strcasecmp(line, "ReportLevels")) report_levels(ppd, argv[2]); else { _cupsLangPrintFilter(stderr, "ERROR", _("Invalid printer command \"%s\"."), line); status = 1; } } return (status); }
ssize_t /* O - Total bytes on success, -1 on error */ backendRunLoop( int print_fd, /* I - Print file descriptor */ int device_fd, /* I - Device file descriptor */ int snmp_fd, /* I - SNMP socket or -1 if none */ http_addr_t *addr, /* I - Address of device */ int use_bc, /* I - Use back-channel? */ int update_state, /* I - Update printer-state-reasons? */ _cups_sccb_t side_cb) /* I - Side-channel callback */ { int nfds; /* Maximum file descriptor value + 1 */ fd_set input, /* Input set for reading */ output; /* Output set for writing */ ssize_t print_bytes, /* Print bytes read */ bc_bytes, /* Backchannel bytes read */ total_bytes, /* Total bytes written */ bytes; /* Bytes written */ int paperout; /* "Paper out" status */ int offline; /* "Off-line" status */ char print_buffer[8192], /* Print data buffer */ *print_ptr, /* Pointer into print data buffer */ bc_buffer[1024]; /* Back-channel data buffer */ struct timeval timeout; /* Timeout for select() */ time_t curtime, /* Current time */ snmp_update = 0; #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ fprintf(stderr, "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, snmp_fd=%d, " "addr=%p, use_bc=%d, side_cb=%p)\n", print_fd, device_fd, snmp_fd, addr, use_bc, side_cb); /* * If we are printing data from a print driver on stdin, ignore SIGTERM * so that the driver can finish out any page data, e.g. to eject the * current page. We only do this for stdin printing as otherwise there * is no way to cancel a raw print job... */ if (!print_fd) { #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ sigset(SIGTERM, SIG_IGN); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); sigemptyset(&action.sa_mask); action.sa_handler = SIG_IGN; sigaction(SIGTERM, &action, NULL); #else signal(SIGTERM, SIG_IGN); #endif /* HAVE_SIGSET */ } else if (print_fd < 0) { /* * Copy print data from stdin, but don't mess with the signal handlers... */ print_fd = 0; } /* * Figure out the maximum file descriptor value to use with select()... */ nfds = (print_fd > device_fd ? print_fd : device_fd) + 1; /* * Now loop until we are out of data from print_fd... */ for (print_bytes = 0, print_ptr = print_buffer, offline = -1, paperout = -1, total_bytes = 0;;) { /* * Use select() to determine whether we have data to copy around... */ FD_ZERO(&input); if (!print_bytes) FD_SET(print_fd, &input); if (use_bc) FD_SET(device_fd, &input); if (!print_bytes && side_cb) FD_SET(CUPS_SC_FD, &input); FD_ZERO(&output); if (print_bytes || (!use_bc && !side_cb)) FD_SET(device_fd, &output); if (use_bc || side_cb) { timeout.tv_sec = 5; timeout.tv_usec = 0; if (select(nfds, &input, &output, NULL, &timeout) < 0) { /* * Pause printing to clear any pending errors... */ if (errno == ENXIO && offline != 1 && update_state) { fputs("STATE: +offline-report\n", stderr); _cupsLangPrintFilter(stderr, "INFO", _("The printer is not connected.")); offline = 1; } else if (errno == EINTR && total_bytes == 0) { fputs("DEBUG: Received an interrupt before any bytes were " "written, aborting.\n", stderr); return (0); } sleep(1); continue; } } /* * Check if we have a side-channel request ready... */ if (side_cb && FD_ISSET(CUPS_SC_FD, &input)) { /* * Do the side-channel request, then start back over in the select * loop since it may have read from print_fd... */ if ((*side_cb)(print_fd, device_fd, snmp_fd, addr, use_bc)) side_cb = NULL; continue; } /* * Check if we have back-channel data ready... */ if (FD_ISSET(device_fd, &input)) { if ((bc_bytes = read(device_fd, bc_buffer, sizeof(bc_buffer))) > 0) { fprintf(stderr, "DEBUG: Received " CUPS_LLFMT " bytes of back-channel data\n", CUPS_LLCAST bc_bytes); cupsBackChannelWrite(bc_buffer, bc_bytes, 1.0); } else if (bc_bytes < 0 && errno != EAGAIN && errno != EINTR) { fprintf(stderr, "DEBUG: Error reading back-channel data: %s\n", strerror(errno)); use_bc = 0; } else if (bc_bytes == 0) use_bc = 0; } /* * Check if we have print data ready... */ if (FD_ISSET(print_fd, &input)) { if ((print_bytes = read(print_fd, print_buffer, sizeof(print_buffer))) < 0) { /* * Read error - bail if we don't see EAGAIN or EINTR... */ if (errno != EAGAIN || errno != EINTR) { fprintf(stderr, "DEBUG: Read failed: %s\n", strerror(errno)); _cupsLangPrintFilter(stderr, "ERROR", _("Unable to read print data.")); return (-1); } print_bytes = 0; } else if (print_bytes == 0) { /* * End of file, break out of the loop... */ break; } print_ptr = print_buffer; fprintf(stderr, "DEBUG: Read %d bytes of print data...\n", (int)print_bytes); } /* * Check if the device is ready to receive data and we have data to * send... */ if (print_bytes && FD_ISSET(device_fd, &output)) { if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0) { /* * Write error - bail if we don't see an error we can retry... */ if (errno == ENOSPC) { if (paperout != 1 && update_state) { fputs("STATE: +media-empty-warning\n", stderr); fputs("DEBUG: Out of paper\n", stderr); paperout = 1; } } else if (errno == ENXIO) { if (offline != 1 && update_state) { fputs("STATE: +offline-report\n", stderr); _cupsLangPrintFilter(stderr, "INFO", _("The printer is not connected.")); offline = 1; } } else if (errno != EAGAIN && errno != EINTR && errno != ENOTTY) { _cupsLangPrintError("ERROR", _("Unable to write print data")); return (-1); } } else { if (paperout && update_state) { fputs("STATE: -media-empty-warning\n", stderr); paperout = 0; } if (offline && update_state) { fputs("STATE: -offline-report\n", stderr); _cupsLangPrintFilter(stderr, "INFO", _("The printer is now connected.")); offline = 0; } fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes); print_bytes -= bytes; print_ptr += bytes; total_bytes += bytes; } } /* * Do SNMP updates periodically... */ if (snmp_fd >= 0 && time(&curtime) >= snmp_update) { if (backendSNMPSupplies(snmp_fd, addr, NULL, NULL)) snmp_update = INT_MAX; else snmp_update = curtime + 5; } } /* * Return with success... */ return (total_bytes); }
static int /* O - Zero on success, non-zero on failure */ lpd_queue(const char *hostname, /* I - Host to connect to */ http_addrlist_t *addrlist, /* I - List of host addresses */ const char *printer, /* I - Printer/queue name */ int print_fd, /* I - File to print */ int snmp_fd, /* I - SNMP socket */ int mode, /* I - Print mode */ const char *user, /* I - Requesting user */ const char *title, /* I - Job title */ int copies, /* I - Number of copies */ int banner, /* I - Print LPD banner? */ int format, /* I - Format specifier */ int order, /* I - Order of data/control files */ int reserve, /* I - Reserve ports? */ int manual_copies,/* I - Do copies by hand... */ int timeout, /* I - Timeout... */ int contimeout, /* I - Connection timeout */ const char *orighost) /* I - job-originating-host-name */ { char localhost[255]; /* Local host name */ int error; /* Error number */ struct stat filestats; /* File statistics */ int lport; /* LPD connection local port */ int fd; /* LPD socket */ char control[10240], /* LPD control 'file' */ *cptr; /* Pointer into control file string */ char status; /* Status byte from command */ int delay; /* Delay for retries... */ char addrname[256]; /* Address name */ http_addrlist_t *addr; /* Socket address */ int have_supplies; /* Printer supports supply levels? */ int copy; /* Copies written */ time_t start_time; /* Time of first connect */ size_t nbytes; /* Number of bytes written */ off_t tbytes; /* Total bytes written */ char buffer[32768]; /* Output buffer */ #ifdef WIN32 DWORD tv; /* Timeout in milliseconds */ #else struct timeval tv; /* Timeout in secs and usecs */ #endif /* WIN32 */ /* * Remember when we started trying to connect to the printer... */ start_time = time(NULL); /* * Loop forever trying to print the file... */ while (!abort_job) { /* * First try to reserve a port for this connection... */ fprintf(stderr, "DEBUG: Connecting to %s:%d for printer %s\n", hostname, httpAddrPort(&(addrlist->addr)), printer); _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer.")); for (lport = reserve == RESERVE_RFC1179 ? 732 : 1024, addr = addrlist, delay = 5;; addr = addr->next) { /* * Stop if this job has been canceled... */ if (abort_job) return (CUPS_BACKEND_FAILED); /* * Choose the next priviledged port... */ if (!addr) addr = addrlist; lport --; if (lport < 721 && reserve == RESERVE_RFC1179) lport = 731; else if (lport < 1) lport = 1023; #ifdef HAVE_GETEUID if (geteuid() || !reserve) #else if (getuid() || !reserve) #endif /* HAVE_GETEUID */ { /* * Just create a regular socket... */ if ((fd = socket(addr->addr.addr.sa_family, SOCK_STREAM, 0)) < 0) { perror("DEBUG: Unable to create socket"); sleep(1); continue; } lport = 0; } else { /* * We're running as root and want to comply with RFC 1179. Reserve a * priviledged lport between 721 and 731... */ if ((fd = rresvport_af(&lport, addr->addr.addr.sa_family)) < 0) { perror("DEBUG: Unable to reserve port"); sleep(1); continue; } } /* * Connect to the printer or server... */ if (abort_job) { close(fd); return (CUPS_BACKEND_FAILED); } if (!connect(fd, &(addr->addr.addr), httpAddrLength(&(addr->addr)))) break; error = errno; close(fd); if (addr->next) continue; if (getenv("CLASS") != NULL) { /* * If the CLASS environment variable is set, the job was submitted * to a class and not to a specific queue. In this case, we want * to abort immediately so that the job can be requeued on the next * available printer in the class. */ _cupsLangPrintFilter(stderr, "INFO", _("Unable to contact printer, queuing on next " "printer in class.")); /* * Sleep 5 seconds to keep the job from requeuing too rapidly... */ sleep(5); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error)); if (error == ECONNREFUSED || error == EHOSTDOWN || error == EHOSTUNREACH) { if (contimeout && (time(NULL) - start_time) > contimeout) { _cupsLangPrintFilter(stderr, "ERROR", _("The printer is not responding.")); return (CUPS_BACKEND_FAILED); } switch (error) { case EHOSTDOWN : _cupsLangPrintFilter(stderr, "WARNING", _("The printer may not exist or " "is unavailable at this time.")); break; case EHOSTUNREACH : _cupsLangPrintFilter(stderr, "WARNING", _("The printer is unreachable at " "this time.")); break; case ECONNREFUSED : default : _cupsLangPrintFilter(stderr, "WARNING", _("The printer is in use.")); break; } sleep(delay); if (delay < 30) delay += 5; } else if (error == EADDRINUSE) { /* * Try on another port... */ sleep(1); } else { _cupsLangPrintFilter(stderr, "ERROR", _("The printer is not responding.")); sleep(30); } } /* * Set the timeout... */ #ifdef WIN32 tv = (DWORD)(timeout * 1000); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); #else tv.tv_sec = timeout; tv.tv_usec = 0; setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); #endif /* WIN32 */ fputs("STATE: -connecting-to-device\n", stderr); _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer.")); fprintf(stderr, "DEBUG: Connected to %s:%d (local port %d)...\n", httpAddrString(&(addr->addr), addrname, sizeof(addrname)), httpAddrPort(&(addr->addr)), lport); /* * See if the printer supports SNMP... */ if (snmp_fd >= 0) have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr), NULL, NULL); else have_supplies = 0; /* * Check for side-channel requests... */ backendCheckSideChannel(snmp_fd, &(addrlist->addr)); /* * Next, open the print file and figure out its size... */ if (print_fd) { /* * Use the size from the print file... */ if (fstat(print_fd, &filestats)) { close(fd); perror("DEBUG: unable to stat print file"); return (CUPS_BACKEND_FAILED); } filestats.st_size *= manual_copies; } else { /* * Use a "very large value" for the size so that the printer will * keep printing until we close the connection... */ #ifdef _LARGEFILE_SOURCE filestats.st_size = (size_t)(999999999999.0); #else filestats.st_size = 2147483647; #endif /* _LARGEFILE_SOURCE */ } /* * Send a job header to the printer, specifying no banner page and * literal output... */ if (lpd_command(fd, "\002%s\n", printer)) /* Receive print job(s) */ { close(fd); return (CUPS_BACKEND_FAILED); } if (orighost && _cups_strcasecmp(orighost, "localhost")) strlcpy(localhost, orighost, sizeof(localhost)); else httpGetHostname(NULL, localhost, sizeof(localhost)); snprintf(control, sizeof(control), "H%.31s\n" /* RFC 1179, Section 7.2 - host name <= 31 chars */ "P%.31s\n" /* RFC 1179, Section 7.2 - user name <= 31 chars */ "J%.99s\n", /* RFC 1179, Section 7.2 - job name <= 99 chars */ localhost, user, title); cptr = control + strlen(control); if (banner) { snprintf(cptr, sizeof(control) - (cptr - control), "C%.31s\n" /* RFC 1179, Section 7.2 - class name <= 31 chars */ "L%s\n", localhost, user); cptr += strlen(cptr); } while (copies > 0) { snprintf(cptr, sizeof(control) - (cptr - control), "%cdfA%03d%.15s\n", format, (int)getpid() % 1000, localhost); cptr += strlen(cptr); copies --; } snprintf(cptr, sizeof(control) - (cptr - control), "UdfA%03d%.15s\n" "N%.131s\n", /* RFC 1179, Section 7.2 - sourcefile name <= 131 chars */ (int)getpid() % 1000, localhost, title); fprintf(stderr, "DEBUG: Control file is:\n%s", control); if (order == ORDER_CONTROL_DATA) { /* * Check for side-channel requests... */ backendCheckSideChannel(snmp_fd, &(addr->addr)); /* * Send the control file... */ if (lpd_command(fd, "\002%d cfA%03.3d%.15s\n", strlen(control), (int)getpid() % 1000, localhost)) { close(fd); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Sending control file (%u bytes)\n", (unsigned)strlen(control)); if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) { status = errno; perror("DEBUG: Unable to write control file"); } else { if (read(fd, &status, 1) < 1) { _cupsLangPrintFilter(stderr, "WARNING", _("The printer did not respond.")); status = errno; } } if (status != 0) _cupsLangPrintFilter(stderr, "ERROR", _("Remote host did not accept control file (%d)."), status); else _cupsLangPrintFilter(stderr, "INFO", _("Control file sent successfully.")); } else status = 0; if (status == 0) { /* * Check for side-channel requests... */ backendCheckSideChannel(snmp_fd, &(addr->addr)); /* * Send the print file... */ if (lpd_command(fd, "\003" CUPS_LLFMT " dfA%03.3d%.15s\n", CUPS_LLCAST filestats.st_size, (int)getpid() % 1000, localhost)) { close(fd); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Sending data file (" CUPS_LLFMT " bytes)\n", CUPS_LLCAST filestats.st_size); tbytes = 0; for (copy = 0; copy < manual_copies; copy ++) { lseek(print_fd, 0, SEEK_SET); while ((nbytes = read(print_fd, buffer, sizeof(buffer))) > 0) { _cupsLangPrintFilter(stderr, "INFO", _("Spooling job, %.0f%% complete."), 100.0 * tbytes / filestats.st_size); if (lpd_write(fd, buffer, nbytes) < nbytes) { perror("DEBUG: Unable to send print file to printer"); break; } else tbytes += nbytes; } } if (mode == MODE_STANDARD) { if (tbytes < filestats.st_size) status = errno; else if (lpd_write(fd, "", 1) < 1) { perror("DEBUG: Unable to send trailing nul to printer"); status = errno; } else { /* * Read the status byte from the printer; if we can't read the byte * back now, we should set status to "errno", however at this point * we know the printer got the whole file and we don't necessarily * want to requeue it over and over... */ if (recv(fd, &status, 1, 0) < 1) { _cupsLangPrintFilter(stderr, "WARNING", _("The printer did not respond.")); status = 0; } } } else status = 0; if (status != 0) _cupsLangPrintFilter(stderr, "ERROR", _("Remote host did not accept data file (%d)."), status); else _cupsLangPrintFilter(stderr, "INFO", _("Data file sent successfully.")); } if (status == 0 && order == ORDER_DATA_CONTROL) { /* * Check for side-channel requests... */ backendCheckSideChannel(snmp_fd, &(addr->addr)); /* * Send control file... */ if (lpd_command(fd, "\002%d cfA%03.3d%.15s\n", strlen(control), (int)getpid() % 1000, localhost)) { close(fd); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Sending control file (%lu bytes)\n", (unsigned long)strlen(control)); if (lpd_write(fd, control, strlen(control) + 1) < (strlen(control) + 1)) { status = errno; perror("DEBUG: Unable to write control file"); } else { if (read(fd, &status, 1) < 1) { _cupsLangPrintFilter(stderr, "WARNING", _("The printer did not respond.")); status = errno; } } if (status != 0) _cupsLangPrintFilter(stderr, "ERROR", _("Remote host did not accept control file (%d)."), status); else _cupsLangPrintFilter(stderr, "INFO", _("Control file sent successfully.")); } /* * Collect the final supply levels as needed... */ if (have_supplies) backendSNMPSupplies(snmp_fd, &(addr->addr), NULL, NULL); /* * Close the socket connection and input file... */ close(fd); if (status == 0) return (CUPS_BACKEND_OK); /* * Waiting for a retry... */ sleep(30); } /* * If we get here, then the job has been canceled... */ return (CUPS_BACKEND_FAILED); }
static int /* O - Exit status */ auto_configure(ppd_file_t *ppd, /* I - PPD file */ const char *user) /* I - Printing user */ { int status = 0; /* Exit status */ ppd_option_t *option; /* Current option in PPD */ ppd_attr_t *attr; /* Query command attribute */ const char *valptr; /* Pointer into attribute value */ char buffer[1024], /* String buffer */ *bufptr; /* Pointer into buffer */ ssize_t bytes; /* Number of bytes read */ int datalen; /* Side-channel data length */ /* * See if the backend supports bidirectional I/O... */ datalen = 1; if (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI, buffer, &datalen, 30.0) != CUPS_SC_STATUS_OK || buffer[0] != CUPS_SC_BIDI_SUPPORTED) { fputs("DEBUG: Unable to auto-configure PostScript Printer - no " "bidirectional I/O available!\n", stderr); return (1); } /* * Put the printer in PostScript mode... */ begin_ps(ppd, user); /* * (STR #4028) * * As a lot of PPDs contain bad PostScript query code, we need to prevent one * bad query sequence from affecting all auto-configuration. The following * error handler allows us to log PostScript errors to cupsd. */ puts("/cups_handleerror {\n" " $error /newerror false put\n" " (:PostScript error in \") print cups_query_keyword print (\": ) " "print\n" " $error /errorname get 128 string cvs print\n" " (; offending command:) print $error /command get 128 string cvs " "print (\n) print flush\n" "} bind def\n" "errordict /timeout {} put\n" "/cups_query_keyword (?Unknown) def\n"); fflush(stdout); /* * Wait for the printer to become connected... */ do { sleep(1); datalen = 1; } while (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_CONNECTED, buffer, &datalen, 5.0) == CUPS_SC_STATUS_OK && !buffer[0]); /* * Then loop through every option in the PPD file and ask for the current * value... */ fputs("DEBUG: Auto-configuring PostScript printer...\n", stderr); for (option = ppdFirstOption(ppd); option; option = ppdNextOption(ppd)) { /* * See if we have a query command for this option... */ snprintf(buffer, sizeof(buffer), "?%s", option->keyword); if ((attr = ppdFindAttr(ppd, buffer, NULL)) == NULL || !attr->value) { fprintf(stderr, "DEBUG: Skipping %s option...\n", option->keyword); continue; } /* * Send the query code to the printer... */ fprintf(stderr, "DEBUG: Querying %s...\n", option->keyword); for (bufptr = buffer, valptr = attr->value; *valptr; valptr ++) { /* * Log the query code, breaking at newlines... */ if (*valptr == '\n') { *bufptr = '\0'; fprintf(stderr, "DEBUG: %s\\n\n", buffer); bufptr = buffer; } else if (*valptr < ' ') { if (bufptr >= (buffer + sizeof(buffer) - 4)) { *bufptr = '\0'; fprintf(stderr, "DEBUG: %s\n", buffer); bufptr = buffer; } if (*valptr == '\r') { *bufptr++ = '\\'; *bufptr++ = 'r'; } else if (*valptr == '\t') { *bufptr++ = '\\'; *bufptr++ = 't'; } else { *bufptr++ = '\\'; *bufptr++ = '0' + ((*valptr / 64) & 7); *bufptr++ = '0' + ((*valptr / 8) & 7); *bufptr++ = '0' + (*valptr & 7); } } else { if (bufptr >= (buffer + sizeof(buffer) - 1)) { *bufptr = '\0'; fprintf(stderr, "DEBUG: %s\n", buffer); bufptr = buffer; } *bufptr++ = *valptr; } } if (bufptr > buffer) { *bufptr = '\0'; fprintf(stderr, "DEBUG: %s\n", buffer); } printf("/cups_query_keyword (?%s) def\n", option->keyword); /* Set keyword for error reporting */ fputs("{ (", stdout); for (valptr = attr->value; *valptr; valptr ++) { if (*valptr == '(' || *valptr == ')' || *valptr == '\\') putchar('\\'); putchar(*valptr); } fputs(") cvx exec } stopped { cups_handleerror } if clear\n", stdout); /* Send query code */ fflush(stdout); datalen = 0; cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT, buffer, &datalen, 5.0); /* * Read the response data... */ bufptr = buffer; buffer[0] = '\0'; while ((bytes = cupsBackChannelRead(bufptr, sizeof(buffer) - (size_t)(bufptr - buffer) - 1, 10.0)) > 0) { /* * No newline at the end? Go on reading ... */ bufptr += bytes; *bufptr = '\0'; if (bytes == 0 || (bufptr > buffer && bufptr[-1] != '\r' && bufptr[-1] != '\n')) continue; /* * Trim whitespace and control characters from both ends... */ bytes = bufptr - buffer; for (bufptr --; bufptr >= buffer; bufptr --) if (isspace(*bufptr & 255) || iscntrl(*bufptr & 255)) *bufptr = '\0'; else break; for (bufptr = buffer; isspace(*bufptr & 255) || iscntrl(*bufptr & 255); bufptr ++); if (bufptr > buffer) { _cups_strcpy(buffer, bufptr); bufptr = buffer; } fprintf(stderr, "DEBUG: Got %d bytes.\n", (int)bytes); /* * Skip blank lines... */ if (!buffer[0]) continue; /* * Check the response... */ if ((bufptr = strchr(buffer, ':')) != NULL) { /* * PostScript code for this option in the PPD is broken; show the * interpreter's error message that came back... */ fprintf(stderr, "DEBUG%s\n", bufptr); break; } /* * Verify the result is a valid option choice... */ if (!ppdFindChoice(option, buffer)) { if (!strcasecmp(buffer, "Unknown")) break; bufptr = buffer; buffer[0] = '\0'; continue; } /* * Write out the result and move on to the next option... */ fprintf(stderr, "PPD: Default%s=%s\n", option->keyword, buffer); break; } /* * Printer did not answer this option's query */ if (bytes <= 0) { fprintf(stderr, "DEBUG: No answer to query for option %s within 10 seconds.\n", option->keyword); status = 1; } } /* * Finish the job... */ fflush(stdout); end_ps(ppd); /* * Return... */ if (status) _cupsLangPrintFilter(stderr, "WARNING", _("Unable to configure printer options.")); return (0); }
int /* O - Exit status */ print_device(const char *uri, /* I - Device URI */ const char *hostname, /* I - Hostname/manufacturer */ const char *resource, /* I - Resource/modelname */ char *options, /* I - Device options/serial number */ int print_fd, /* I - File descriptor to print */ int copies, /* I - Copies to print */ int argc, /* I - Number of command-line arguments (6 or 7) */ char *argv[]) /* I - Command-line arguments */ { int use_bc; /* Use backchannel path? */ int device_fd; /* USB device */ ssize_t tbytes; /* Total number of bytes written */ struct termios opts; /* Parallel port options */ (void)argc; (void)argv; /* * Open the USB port device... */ fputs("STATE: +connecting-to-device\n", stderr); do { #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) /* * *BSD's ulpt driver currently does not support the * back-channel, incorrectly returns data ready on a select(), * and locks up on read()... */ use_bc = 0; #elif defined(__sun) /* * CUPS STR #3028: Solaris' usbprn driver apparently does not support * select() or poll(), so we can't support backchannel... */ use_bc = 0; #else /* * Disable backchannel data when printing to Brother, Canon, or * Minolta USB printers - apparently these printers will return * the IEEE-1284 device ID over and over and over when they get * a read request... */ use_bc = _cups_strcasecmp(hostname, "Brother") && _cups_strcasecmp(hostname, "Canon") && _cups_strncasecmp(hostname, "Konica", 6) && _cups_strncasecmp(hostname, "Minolta", 7); #endif /* __FreeBSD__ || __NetBSD__ || __OpenBSD__ || __DragonFly__ */ if ((device_fd = open_device(uri, &use_bc)) == -1) { if (getenv("CLASS") != NULL) { /* * If the CLASS environment variable is set, the job was submitted * to a class and not to a specific queue. In this case, we want * to abort immediately so that the job can be requeued on the next * available printer in the class. */ _cupsLangPrintFilter(stderr, "INFO", _("Unable to contact printer, queuing on next " "printer in class.")); /* * Sleep 5 seconds to keep the job from requeuing too rapidly... */ sleep(5); return (CUPS_BACKEND_FAILED); } if (errno == EBUSY) { _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use.")); sleep(10); } else if (errno == ENXIO || errno == EIO || errno == ENOENT || errno == ENODEV) { sleep(30); } else { _cupsLangPrintError("ERROR", _("Unable to open device file")); return (CUPS_BACKEND_FAILED); } } } while (device_fd < 0); fputs("STATE: -connecting-to-device\n", stderr); /* * Set any options provided... */ tcgetattr(device_fd, &opts); opts.c_lflag &= ~(unsigned)(ICANON | ECHO | ISIG); /* Raw mode */ /**** No options supported yet ****/ tcsetattr(device_fd, TCSANOW, &opts); /* * Finally, send the print file... */ tbytes = 0; while (copies > 0 && tbytes >= 0) { copies --; if (print_fd != 0) { fputs("PAGE: 1 1\n", stderr); lseek(print_fd, 0, SEEK_SET); } #ifdef __sun /* * CUPS STR #3028: Solaris' usbprn driver apparently does not support * select() or poll(), so we can't support the sidechannel either... */ tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, 1, NULL); #else tbytes = backendRunLoop(print_fd, device_fd, -1, NULL, use_bc, 1, side_cb); #endif /* __sun */ if (print_fd != 0 && tbytes >= 0) _cupsLangPrintFilter(stderr, "INFO", _("Print file sent.")); } /* * Close the USB port and return... */ close(device_fd); return (CUPS_BACKEND_OK); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments (6 or 7) */ char *argv[]) /* I - Command-line arguments */ { const char *device_uri; /* Device URI */ char scheme[255], /* Scheme in URI */ hostname[1024], /* Hostname */ username[255], /* Username info */ resource[1024], /* Resource info (printer name) */ *options, /* Pointer to options */ *name, /* Name of option */ *value, /* Value of option */ sep, /* Separator character */ *filename, /* File to print */ title[256]; /* Title string */ int port; /* Port number */ char portname[256]; /* Port name (string) */ http_addrlist_t *addrlist; /* List of addresses for printer */ int snmp_enabled = 1; /* Is SNMP enabled? */ int snmp_fd; /* SNMP socket */ int fd; /* Print file */ int status; /* Status of LPD job */ int mode; /* Print mode */ int banner; /* Print banner page? */ int format; /* Print format */ int order; /* Order of control/data files */ int reserve; /* Reserve priviledged port? */ int sanitize_title; /* Sanitize title string? */ int manual_copies, /* Do manual copies? */ timeout, /* Timeout */ contimeout, /* Connection timeout */ copies; /* Number of copies */ ssize_t bytes = 0; /* Initial bytes read */ char buffer[16384]; /* Initial print buffer */ #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ int num_jobopts; /* Number of job options */ cups_option_t *jobopts = NULL; /* Job options */ /* * Make sure status messages are not buffered... */ setbuf(stderr, NULL); /* * Ignore SIGPIPE and catch SIGTERM signals... */ #ifdef HAVE_SIGSET sigset(SIGPIPE, SIG_IGN); sigset(SIGTERM, sigterm_handler); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); action.sa_handler = SIG_IGN; sigaction(SIGPIPE, &action, NULL); sigemptyset(&action.sa_mask); sigaddset(&action.sa_mask, SIGTERM); action.sa_handler = sigterm_handler; sigaction(SIGTERM, &action, NULL); #else signal(SIGPIPE, SIG_IGN); signal(SIGTERM, sigterm_handler); #endif /* HAVE_SIGSET */ /* * Check command-line... */ if (argc == 1) { printf("network lpd \"Unknown\" \"%s\"\n", _cupsLangString(cupsLangDefault(), _("LPD/LPR Host or Printer"))); return (CUPS_BACKEND_OK); } else if (argc < 6 || argc > 7) { _cupsLangPrintf(stderr, _("Usage: %s job-id user title copies options [file]"), argv[0]); return (CUPS_BACKEND_FAILED); } num_jobopts = cupsParseOptions(argv[5], 0, &jobopts); /* * Extract the hostname and printer name from the URI... */ while ((device_uri = cupsBackendDeviceURI(argv)) == NULL) { _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer.")); sleep(10); if (getenv("CLASS") != NULL) return (CUPS_BACKEND_FAILED); } httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), username, sizeof(username), hostname, sizeof(hostname), &port, resource, sizeof(resource)); if (!port) port = 515; /* Default to port 515 */ if (!username[0]) { /* * If no username is in the device URI, then use the print job user... */ strlcpy(username, argv[2], sizeof(username)); } /* * See if there are any options... */ mode = MODE_STANDARD; banner = 0; format = 'l'; order = ORDER_CONTROL_DATA; reserve = RESERVE_ANY; manual_copies = 1; timeout = 300; contimeout = 7 * 24 * 60 * 60; #ifdef __APPLE__ /* * We want to pass UTF-8 characters by default, not re-map them (3071945) */ sanitize_title = 0; #else /* * Otherwise we want to re-map UTF-8 to "safe" characters by default... */ sanitize_title = 1; #endif /* __APPLE__ */ if ((options = strchr(resource, '?')) != NULL) { /* * Yup, terminate the device name string and move to the first * character of the options... */ *options++ = '\0'; /* * Parse options... */ while (*options) { /* * Get the name... */ name = options; while (*options && *options != '=' && *options != '+' && *options != '&') options ++; if ((sep = *options) != '\0') *options++ = '\0'; if (sep == '=') { /* * Get the value... */ value = options; while (*options && *options != '+' && *options != '&') options ++; if (*options) *options++ = '\0'; } else value = (char *)""; /* * Process the option... */ if (!_cups_strcasecmp(name, "banner")) { /* * Set the banner... */ banner = !value[0] || !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true"); } else if (!_cups_strcasecmp(name, "format") && value[0]) { /* * Set output format... */ if (strchr("cdfglnoprtv", value[0])) format = value[0]; else _cupsLangPrintFilter(stderr, "ERROR", _("Unknown format character: \"%c\"."), value[0]); } else if (!_cups_strcasecmp(name, "mode") && value[0]) { /* * Set control/data order... */ if (!_cups_strcasecmp(value, "standard")) mode = MODE_STANDARD; else if (!_cups_strcasecmp(value, "stream")) mode = MODE_STREAM; else _cupsLangPrintFilter(stderr, "ERROR", _("Unknown print mode: \"%s\"."), value); } else if (!_cups_strcasecmp(name, "order") && value[0]) { /* * Set control/data order... */ if (!_cups_strcasecmp(value, "control,data")) order = ORDER_CONTROL_DATA; else if (!_cups_strcasecmp(value, "data,control")) order = ORDER_DATA_CONTROL; else _cupsLangPrintFilter(stderr, "ERROR", _("Unknown file order: \"%s\"."), value); } else if (!_cups_strcasecmp(name, "reserve")) { /* * Set port reservation mode... */ if (!value[0] || !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true") || !_cups_strcasecmp(value, "rfc1179")) reserve = RESERVE_RFC1179; else if (!_cups_strcasecmp(value, "any")) reserve = RESERVE_ANY; else reserve = RESERVE_NONE; } else if (!_cups_strcasecmp(name, "manual_copies")) { /* * Set manual copies... */ manual_copies = !value[0] || !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true"); } else if (!_cups_strcasecmp(name, "sanitize_title")) { /* * Set sanitize title... */ sanitize_title = !value[0] || !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true"); } else if (!_cups_strcasecmp(name, "snmp")) { /* * Enable/disable SNMP stuff... */ snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") || _cups_strcasecmp(value, "yes") || _cups_strcasecmp(value, "true"); } else if (!_cups_strcasecmp(name, "timeout")) { /* * Set the timeout... */ if (atoi(value) > 0) timeout = atoi(value); } else if (!_cups_strcasecmp(name, "contimeout")) { /* * Set the connection timeout... */ if (atoi(value) > 0) contimeout = atoi(value); } } } if (mode == MODE_STREAM) order = ORDER_CONTROL_DATA; /* * Find the printer... */ snprintf(portname, sizeof(portname), "%d", port); fputs("STATE: +connecting-to-device\n", stderr); fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname); while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) { _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer \"%s\"."), hostname); sleep(10); if (getenv("CLASS") != NULL) { fputs("STATE: -connecting-to-device\n", stderr); exit(CUPS_BACKEND_FAILED); } } if (snmp_enabled) snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family); else snmp_fd = -1; /* * Wait for data from the filter... */ if (argc == 6) { if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 0, backendNetworkSideCB)) return (CUPS_BACKEND_OK); else if (mode == MODE_STANDARD && (bytes = read(0, buffer, sizeof(buffer))) <= 0) return (CUPS_BACKEND_OK); } /* * If we have 7 arguments, print the file named on the command-line. * Otherwise, copy stdin to a temporary file and print the temporary * file. */ if (argc == 6 && mode == MODE_STANDARD) { /* * Copy stdin to a temporary file... */ if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0) { perror("DEBUG: Unable to create temporary file"); return (CUPS_BACKEND_FAILED); } _cupsLangPrintFilter(stderr, "INFO", _("Copying print data.")); if (bytes > 0) write(fd, buffer, bytes); backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0, backendNetworkSideCB); } else if (argc == 6) { /* * Stream from stdin... */ filename = NULL; fd = 0; } else { filename = argv[6]; fd = open(filename, O_RDONLY); if (fd == -1) { _cupsLangPrintError("ERROR", _("Unable to open print file")); return (CUPS_BACKEND_FAILED); } } /* * Sanitize the document title... */ strlcpy(title, argv[3], sizeof(title)); if (sanitize_title) { /* * Sanitize the title string so that we don't cause problems on * the remote end... */ char *ptr; for (ptr = title; *ptr; ptr ++) if (!isalnum(*ptr & 255) && !isspace(*ptr & 255)) *ptr = '_'; } /* * Queue the job... */ if (argc > 6) { if (manual_copies) { manual_copies = atoi(argv[4]); copies = 1; } else { manual_copies = 1; copies = atoi(argv[4]); } status = lpd_queue(hostname, addrlist, resource + 1, fd, snmp_fd, mode, username, title, copies, banner, format, order, reserve, manual_copies, timeout, contimeout, cupsGetOption("job-originating-host-name", num_jobopts, jobopts)); if (!status) fprintf(stderr, "PAGE: 1 %d\n", atoi(argv[4])); } else status = lpd_queue(hostname, addrlist, resource + 1, fd, snmp_fd, mode, username, title, 1, banner, format, order, reserve, 1, timeout, contimeout, cupsGetOption("job-originating-host-name", num_jobopts, jobopts)); /* * Remove the temporary file if necessary... */ if (tmpfilename[0]) unlink(tmpfilename); if (fd) close(fd); if (snmp_fd >= 0) _cupsSNMPClose(snmp_fd); /* * Return the queue status... */ return (status); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { int fd; /* File descriptor */ cups_raster_t *ras; /* Raster stream for printing */ cups_page_header2_t header; /* Page header from file */ unsigned y; /* Current line */ ppd_file_t *ppd; /* PPD file */ int num_options; /* Number of options */ cups_option_t *options; /* Options */ #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ /* * Make sure status messages are not buffered... */ setbuf(stderr, NULL); /* * Check command-line... */ if (argc < 6 || argc > 7) { /* * We don't have the correct number of arguments; write an error message * and return. */ _cupsLangPrintFilter(stderr, "ERROR", _("%s job-id user title copies options [file]"), "rastertolabel"); return (1); } /* * Open the page stream... */ if (argc == 7) { if ((fd = open(argv[6], O_RDONLY)) == -1) { _cupsLangPrintError("ERROR", _("Unable to open raster file")); sleep(1); return (1); } } else fd = 0; ras = cupsRasterOpen(fd, CUPS_RASTER_READ); /* * Register a signal handler to eject the current page if the * job is cancelled. */ Canceled = 0; #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ sigset(SIGTERM, CancelJob); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); sigemptyset(&action.sa_mask); action.sa_handler = CancelJob; sigaction(SIGTERM, &action, NULL); #else signal(SIGTERM, CancelJob); #endif /* HAVE_SIGSET */ /* * Open the PPD file and apply options... */ num_options = cupsParseOptions(argv[5], 0, &options); ppd = ppdOpenFile(getenv("PPD")); if (!ppd) { ppd_status_t status; /* PPD error */ int linenum; /* Line number */ _cupsLangPrintFilter(stderr, "ERROR", _("The PPD file could not be opened.")); status = ppdLastError(&linenum); fprintf(stderr, "DEBUG: %s on line %d.\n", ppdErrorString(status), linenum); return (1); } ppdMarkDefaults(ppd); cupsMarkOptions(ppd, num_options, options); /* * Initialize the print device... */ Setup(ppd); /* * Process pages as needed... */ Page = 0; while (cupsRasterReadHeader2(ras, &header)) { /* * Write a status message with the page number and number of copies. */ if (Canceled) break; Page ++; fprintf(stderr, "PAGE: %d 1\n", Page); _cupsLangPrintFilter(stderr, "INFO", _("Starting page %d."), Page); /* * Start the page... */ StartPage(ppd, &header); /* * Loop for each line on the page... */ for (y = 0; y < header.cupsHeight && !Canceled; y ++) { /* * Let the user know how far we have progressed... */ if (Canceled) break; if ((y & 15) == 0) { _cupsLangPrintFilter(stderr, "INFO", _("Printing page %d, %u%% complete."), Page, 100 * y / header.cupsHeight); fprintf(stderr, "ATTR: job-media-progress=%u\n", 100 * y / header.cupsHeight); } /* * Read a line of graphics... */ if (cupsRasterReadPixels(ras, Buffer, header.cupsBytesPerLine) < 1) break; /* * Write it to the printer... */ OutputLine(ppd, &header, y); } /* * Eject the page... */ _cupsLangPrintFilter(stderr, "INFO", _("Finished page %d."), Page); EndPage(ppd, &header); if (Canceled) break; } /* * Close the raster stream... */ cupsRasterClose(ras); if (fd != 0) close(fd); /* * Close the PPD file and free the options... */ ppdClose(ppd); cupsFreeOptions(num_options, options); /* * If no pages were printed, send an error message... */ if (Page == 0) { _cupsLangPrintFilter(stderr, "ERROR", _("No pages were found.")); return (1); } else return (0); }
static int /* O - File descriptor or -1 on error */ open_device(const char *uri, /* I - Device URI */ int *use_bc) /* O - Set to 0 for unidirectional */ { int fd; /* File descriptor */ /* * The generic implementation just treats the URI as a device filename... * Specific operating systems may also support using the device serial * number and/or make/model. */ if (!strncmp(uri, "usb:/dev/", 9)) #ifdef __linux { /* * Do not allow direct devices anymore... */ errno = ENODEV; return (-1); } else if (!strncmp(uri, "usb://", 6)) { /* * For Linux, try looking up the device serial number or model... */ int i; /* Looping var */ int busy; /* Are any ports busy? */ char device[255], /* Device filename */ device_id[1024], /* Device ID string */ make_model[1024], /* Make and model */ device_uri[1024]; /* Device URI string */ /* * Find the correct USB device... */ for (;;) { for (busy = 0, i = 0; i < 16; i ++) { /* * Linux has a long history of changing the standard filenames used * for USB printer devices. We get the honor of trying them all... */ sprintf(device, "/dev/usblp%d", i); if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT) { sprintf(device, "/dev/usb/lp%d", i); if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT) { sprintf(device, "/dev/usb/usblp%d", i); if ((fd = open(device, O_RDWR | O_EXCL)) < 0 && errno == ENOENT) continue; } } if (fd >= 0) { backendGetDeviceID(fd, device_id, sizeof(device_id), make_model, sizeof(make_model), "usb", device_uri, sizeof(device_uri)); } else { /* * If the open failed because it was busy, flag it so we retry * as needed... */ if (errno == EBUSY) busy = 1; device_uri[0] = '\0'; } if (!strcmp(uri, device_uri)) { /* * Yes, return this file descriptor... */ fprintf(stderr, "DEBUG: Printer using device file \"%s\"...\n", device); return (fd); } /* * This wasn't the one... */ if (fd >= 0) close(fd); } /* * If we get here and at least one of the printer ports showed up * as "busy", then sleep for a bit and retry... */ if (busy) _cupsLangPrintFilter(stderr, "INFO", _("The printer is in use.")); sleep(5); } } #elif defined(__sun) && defined(ECPPIOC_GETDEVID) { /* * Do not allow direct devices anymore... */ errno = ENODEV; return (-1); } else if (!strncmp(uri, "usb://", 6))
int /* O - Exit status */ imagetops_main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { cups_image_t *img; /* Image to print */ float xprint, /* Printable area */ yprint, xinches, /* Total size in inches */ yinches; float xsize, /* Total size in points */ ysize, xsize2, ysize2; float aspect; /* Aspect ratio */ int xpages, /* # x pages */ ypages, /* # y pages */ xpage, /* Current x page */ ypage, /* Current y page */ page; /* Current page number */ int xc0, yc0, /* Corners of the page in image coords */ xc1, yc1; cups_ib_t *row; /* Current row */ int y; /* Current Y coordinate in image */ int colorspace; /* Output colorspace */ int out_offset, /* Offset into output buffer */ out_length; /* Length of output buffer */ ppd_file_t *ppd; /* PPD file */ ppd_choice_t *choice; /* PPD option choice */ int num_options; /* Number of print options */ cups_option_t *options; /* Print options */ const char *val; /* Option value */ int slowcollate; /* Collate copies the slow way */ float g; /* Gamma correction value */ float b; /* Brightness factor */ float zoom; /* Zoom facter */ int xppi, yppi; /* Pixels-per-inch */ int hue, sat; /* Hue and saturation adjustment */ int realcopies, /* Real copies being printed */ emit_jcl; /* Emit JCL? */ float left, top; /* Left and top of image */ char filename[1024]; /* Name of file to print */ time_t curtime; /* Current time */ struct tm *curtm; /* Current date */ char curdate[255]; /* Current date string */ int psFileMode = 0; //0:CREATE New ; 1: Append int outColorDevice = 1; //0 : monochrome print , 1:color print int count = 0,numFiles=1; int defout,outfd; /* * Make sure status messages are not buffered... */ int ii=0; for(;ii<argc;ii++) LOGI("imagetops : argv:%s",argv[ii]); fflush(stdout); Copies = 1; setbuf(stderr, NULL); /* * Ignore broken pipe signals... */ signal(SIGPIPE, SIG_IGN); /* * Process command-line options and write the prolog... */ zoom = 0.0; xppi = 0; yppi = 0; hue = 0; sat = 100; g = 1.0; b = 1.0; Copies = atoi(argv[4]); options = NULL; num_options = cupsParseOptions(argv[5], 0, &options); ppdFilePath = argv[6]; ContentType= argv[7]; psFilePath = argv[8]; outColorDevice = atoi(argv[9]); numFiles = argc - 10; LOGI("imagetops : numFiles=%d",numFiles); ppd = SetCommonOptions(num_options, options, 0); if ((val = cupsGetOption("multiple-document-handling", num_options, options)) != NULL) { /* * This IPP attribute is unnecessarily complicated... * * single-document, separate-documents-collated-copies, and * single-document-new-sheet all require collated copies. * * separate-documents-uncollated-copies allows for uncollated copies. */ Collate = _cups_strcasecmp(val, "separate-documents-uncollated-copies") != 0; } if ((val = cupsGetOption("Collate", num_options, options)) != NULL && _cups_strcasecmp(val, "True") == 0) Collate = 1; if ((val = cupsGetOption("gamma", num_options, options)) != NULL) { /* * Get gamma value from 1 to 10000... */ g = atoi(val) * 0.001f; if (g < 0.001f) g = 0.001f; else if (g > 10.0f) g = 10.0f; } if ((val = cupsGetOption("brightness", num_options, options)) != NULL) { /* * Get brightness value from 10 to 1000. */ b = atoi(val) * 0.01f; if (b < 0.1f) b = 0.1f; else if (b > 10.0f) b = 10.0f; } if ((val = cupsGetOption("scaling", num_options, options)) != NULL) zoom = atoi(val) * 0.01; else if ((val = cupsGetOption("fitplot", num_options, options)) != NULL && !_cups_strcasecmp(val, "true")) zoom = 1.0; else if ((val = cupsGetOption("fit-to-page", num_options, options)) != NULL && !_cups_strcasecmp(val, "true")) zoom = 1.0; if ((val = cupsGetOption("ppi", num_options, options)) != NULL) if (sscanf(val, "%dx%d", &xppi, &yppi) < 2) yppi = xppi; if ((val = cupsGetOption("position", num_options, options)) != NULL) { if (_cups_strcasecmp(val, "center") == 0) { XPosition = 0; YPosition = 0; } else if (_cups_strcasecmp(val, "top") == 0) { XPosition = 0; YPosition = 1; } else if (_cups_strcasecmp(val, "left") == 0) { XPosition = -1; YPosition = 0; } else if (_cups_strcasecmp(val, "right") == 0) { XPosition = 1; YPosition = 0; } else if (_cups_strcasecmp(val, "top-left") == 0) { XPosition = -1; YPosition = 1; } else if (_cups_strcasecmp(val, "top-right") == 0) { XPosition = 1; YPosition = 1; } else if (_cups_strcasecmp(val, "bottom") == 0) { XPosition = 0; YPosition = -1; } else if (_cups_strcasecmp(val, "bottom-left") == 0) { XPosition = -1; YPosition = -1; } else if (_cups_strcasecmp(val, "bottom-right") == 0) { XPosition = 1; YPosition = -1; } } if ((val = cupsGetOption("saturation", num_options, options)) != NULL) sat = atoi(val); if ((val = cupsGetOption("hue", num_options, options)) != NULL) hue = atoi(val); if ((choice = ppdFindMarkedChoice(ppd, "MirrorPrint")) != NULL) { val = choice->choice; choice->marked = 0; } else val = cupsGetOption("mirror", num_options, options); if (val && (!_cups_strcasecmp(val, "true") || !_cups_strcasecmp(val, "on") || !_cups_strcasecmp(val, "yes"))) Flip = 1; if ((val = cupsGetOption("emit-jcl", num_options, options)) != NULL && (!_cups_strcasecmp(val, "false") || !_cups_strcasecmp(val, "off") || !_cups_strcasecmp(val, "no") || !strcmp(val, "0"))) emit_jcl = 0; else emit_jcl = 1; LOGI("imagetops/Copies=%d , Collate=%d, gamma=%f, brightness=%f, zoom=%f",Copies,Collate,g,b,zoom); LOGI("imagetops/xppi=%d,yppi=%d,XPosition=%d,YPosition=%d,sat=%d,hue=%d,flip=%d,emit_jcl=%d",xppi,yppi,XPosition,YPosition,sat,hue,Flip,emit_jcl); /* * Open the input image to print... */ if(outColorDevice == 1) //added @ grv colorspace = CUPS_IMAGE_RGB_CMYK; else colorspace = CUPS_IMAGE_WHITE; LOGI("imagetops/ colorspace = %d",colorspace); for(count=0;count<numFiles;count++){ Copies = atoi(argv[4]); //do everything for both image files strlcpy(filename, argv[(10 + count)], sizeof(filename)); LOGI("imagetops : count=%d , filename=%s",count,filename); img = cupsImageOpen(filename, colorspace, CUPS_IMAGE_WHITE, sat, hue, NULL); if (img == NULL) { LOGI("imagetops:error:The print file could not be opened."); _cupsLangPrintFilter(stderr, "ERROR", _("The print file could not be opened.")); ppdClose(ppd); return (1); } colorspace = cupsImageGetColorSpace(img); LOGI("imagetops: cupsImageGetColorSpace finished : colorspace :%d",colorspace); /* * Scale as necessary... */ if (zoom == 0.0 && xppi == 0) { xppi = cupsImageGetXPPI(img); yppi = cupsImageGetYPPI(img); } LOGI("imagetops:xppi=%d",xppi); LOGI("imagetops:yppi=%d",yppi); if (yppi == 0) yppi = xppi; fprintf(stderr, "DEBUG: Before scaling: xppi=%d, yppi=%d, zoom=%.2f\n", xppi, yppi, zoom); if (xppi > 0) { /* * Scale the image as neccesary to match the desired pixels-per-inch. */ if (Orientation & 1) { xprint = (PageTop - PageBottom) / 72.0; yprint = (PageRight - PageLeft) / 72.0; } else { xprint = (PageRight - PageLeft) / 72.0; yprint = (PageTop - PageBottom) / 72.0; } fprintf(stderr, "DEBUG: Before scaling: xprint=%.1f, yprint=%.1f\n", xprint, yprint); xinches = (float)cupsImageGetWidth(img) / (float)xppi; yinches = (float)cupsImageGetHeight(img) / (float)yppi; fprintf(stderr, "DEBUG: Image size is %.1f x %.1f inches...\n", xinches, yinches); if ((val = cupsGetOption("natural-scaling", num_options, options)) != NULL) { xinches = xinches * atoi(val) / 100; yinches = yinches * atoi(val) / 100; } if (cupsGetOption("orientation-requested", num_options, options) == NULL && cupsGetOption("landscape", num_options, options) == NULL) { /* * Rotate the image if it will fit landscape but not portrait... */ fputs("DEBUG: Auto orientation...\n", stderr); LOGI("imagetops.c : Auto orientation..."); if ((xinches > xprint || yinches > yprint) && xinches <= yprint && yinches <= xprint) { /* * Rotate the image as needed... */ fputs("DEBUG: Using landscape orientation...\n", stderr); LOGI("imagetops.c : Auto orientation : Using landscape orientation"); Orientation = (Orientation + 1) & 3; xsize = yprint; yprint = xprint; xprint = xsize; } } }else{ /* * Scale percentage of page size... */ xprint = (PageRight - PageLeft) / 72.0; yprint = (PageTop - PageBottom) / 72.0; aspect = (float)cupsImageGetYPPI(img) / (float)cupsImageGetXPPI(img); fprintf(stderr, "DEBUG: Before scaling: xprint=%.1f, yprint=%.1f\n", xprint, yprint); fprintf(stderr, "DEBUG: cupsImageGetXPPI(img) = %d, cupsImageGetYPPI(img) = %d, aspect = %f\n", cupsImageGetXPPI(img), cupsImageGetYPPI(img), aspect); xsize = xprint * zoom; ysize = xsize * cupsImageGetHeight(img) / cupsImageGetWidth(img) / aspect; if (ysize > (yprint * zoom)) { ysize = yprint * zoom; xsize = ysize * cupsImageGetWidth(img) * aspect / cupsImageGetHeight(img); } xsize2 = yprint * zoom; ysize2 = xsize2 * cupsImageGetHeight(img) / cupsImageGetWidth(img) / aspect; if (ysize2 > (xprint * zoom)) { ysize2 = xprint * zoom; xsize2 = ysize2 * cupsImageGetWidth(img) * aspect / cupsImageGetHeight(img); } fprintf(stderr, "DEBUG: Portrait size is %.2f x %.2f inches\n", xsize, ysize); fprintf(stderr, "DEBUG: Landscape size is %.2f x %.2f inches\n", xsize2, ysize2); if (cupsGetOption("orientation-requested", num_options, options) == NULL && cupsGetOption("landscape", num_options, options) == NULL) { /* * Choose the rotation with the largest area, but prefer * portrait if they are equal... */ fputs("DEBUG: Auto orientation...\n", stderr); if ((xsize * ysize) < (xsize2 * xsize2)) { /* * Do landscape orientation... */ fputs("DEBUG: Using landscape orientation...\n", stderr); Orientation = 1; xinches = xsize2; yinches = ysize2; xprint = (PageTop - PageBottom) / 72.0; yprint = (PageRight - PageLeft) / 72.0; } else { /* * Do portrait orientation... */ fputs("DEBUG: Using portrait orientation...\n", stderr); Orientation = 0; xinches = xsize; yinches = ysize; } } else if (Orientation & 1) { fputs("DEBUG: Using landscape orientation...\n", stderr); xinches = xsize2; yinches = ysize2; xprint = (PageTop - PageBottom) / 72.0; yprint = (PageRight - PageLeft) / 72.0; } else { fputs("DEBUG: Using portrait orientation...\n", stderr); xinches = xsize; yinches = ysize; xprint = (PageRight - PageLeft) / 72.0; yprint = (PageTop - PageBottom) / 72.0; } } /* * Compute the number of pages to print and the size of the image on each * page... */ xpages = ceil(xinches / xprint); ypages = ceil(yinches / yprint); xprint = xinches / xpages; yprint = yinches / ypages; fprintf(stderr, "DEBUG: xpages = %dx%.2fin, ypages = %dx%.2fin\n",xpages, xprint, ypages, yprint); /* * Update the page size for custom sizes... */ if ((choice = ppdFindMarkedChoice(ppd, "PageSize")) != NULL && _cups_strcasecmp(choice->choice, "Custom") == 0) { float width, /* New width in points */ length; /* New length in points */ char s[255]; /* New custom page size... */ /* * Use the correct width and length for the current orientation... */ if (Orientation & 1) { width = yprint * 72.0; length = xprint * 72.0; } else { width = xprint * 72.0; length = yprint * 72.0; } /* * Add margins to page size... */ width += ppd->custom_margins[0] + ppd->custom_margins[2]; length += ppd->custom_margins[1] + ppd->custom_margins[3]; /* * Enforce minimums... */ if (width < ppd->custom_min[0]) width = ppd->custom_min[0]; if (length < ppd->custom_min[1]) length = ppd->custom_min[1]; fprintf(stderr, "DEBUG: Updated custom page size to %.2f x %.2f inches...\n",width / 72.0, length / 72.0); /* * Set the new custom size... */ sprintf(s, "Custom.%.0fx%.0f", width, length); ppdMarkOption(ppd, "PageSize", s); /* * Update page variables... */ PageWidth = width; PageLength = length; PageLeft = ppd->custom_margins[0]; PageRight = width - ppd->custom_margins[2]; PageBottom = ppd->custom_margins[1]; PageTop = length - ppd->custom_margins[3]; } /* * See if we need to collate, and if so how we need to do it... */ if (xpages == 1 && ypages == 1) Collate = 0; slowcollate = Collate && ppdFindOption(ppd, "Collate") == NULL; if (Copies > 1 && !slowcollate) { realcopies = Copies; Copies = 1; } else realcopies = 1; /* * Write any "exit server" options that have been selected... */ //Changes to create ps file -- start if(count == 0){//single side print defout = dup(1); outfd = open(psFilePath,O_RDWR | O_CREAT,S_IRWXU |S_IRWXG | S_IRWXO); dup2(outfd,1); } //Changes to create ps file -- end ppdEmit(ppd, stdout, PPD_ORDER_EXIT); /* * Write any JCL commands that are needed to print PostScript code... */ if (emit_jcl) ppdEmitJCL(ppd, stdout, atoi(argv[1]), argv[2], argv[3]); /* * Start sending the document with any commands needed... */ curtime = time(NULL); curtm = localtime(&curtime); ////////////////// if(count == 0){ puts("%!PS-Adobe-3.0"); printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom,PageRight, PageTop); printf("%%%%LanguageLevel: %d\n", LanguageLevel); printf("%%%%Pages: %d\n", xpages * ypages * Copies * numFiles); puts("%%DocumentData: Clean7Bit"); puts("%%DocumentNeededResources: font Helvetica-Bold"); puts("%%Creator: imagetops/" CUPS_SVERSION); strftime(curdate, sizeof(curdate), "%c", curtm); printf("%%%%CreationDate: %s\n", curdate); WriteTextComment("Title", argv[3]); WriteTextComment("For", argv[2]); if (Orientation & 1) puts("%%Orientation: Landscape"); else puts("%%Orientation: Portrait"); puts("%%EndComments"); puts("%%BeginProlog"); if (ppd != NULL && ppd->patches != NULL) puts(ppd->patches); ppdEmit(ppd, stdout, PPD_ORDER_DOCUMENT); ppdEmit(ppd, stdout, PPD_ORDER_ANY); ppdEmit(ppd, stdout, PPD_ORDER_PROLOG); if (g != 1.0 || b != 1.0) printf("{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } " "ifelse %.3f mul } bind settransfer\n", g, b); WriteCommon(); switch (Orientation) { case 0 : WriteLabelProlog(cupsGetOption("page-label", num_options, options), PageBottom, PageTop, PageWidth); break; case 1 : WriteLabelProlog(cupsGetOption("page-label", num_options, options), PageLeft, PageRight, PageLength); break; case 2 : WriteLabelProlog(cupsGetOption("page-label", num_options, options), PageLength - PageTop, PageLength - PageBottom, PageWidth); break; case 3 : WriteLabelProlog(cupsGetOption("page-label", num_options, options), PageWidth - PageRight, PageWidth - PageLeft,PageLength); break; } if (realcopies > 1) { if (ppd == NULL || ppd->language_level == 1) printf("/#copies %d def\n", realcopies); else printf("<</NumCopies %d>>setpagedevice\n", realcopies); } puts("%%EndProlog"); } /* * Output the pages... */ row = malloc(cupsImageGetWidth(img) * abs(colorspace) + 3); fprintf(stderr, "DEBUG: XPosition=%d, YPosition=%d, Orientation=%d\n",XPosition, YPosition, Orientation); fprintf(stderr, "DEBUG: xprint=%.0f, yprint=%.0f\n", xprint, yprint); fprintf(stderr, "DEBUG: PageLeft=%.0f, PageRight=%.0f, PageWidth=%.0f\n",PageLeft, PageRight, PageWidth); fprintf(stderr, "DEBUG: PageBottom=%.0f, PageTop=%.0f, PageLength=%.0f\n",PageBottom, PageTop, PageLength); switch (Orientation) { default : switch (XPosition) { case -1 : left = PageLeft; break; default : left = (PageRight + PageLeft - xprint * 72) / 2; break; case 1 : left = PageRight - xprint * 72; break; } switch (YPosition) { case -1 : top = PageBottom + yprint * 72; break; default : top = (PageTop + PageBottom + yprint * 72) / 2; break; case 1 : top = PageTop; break; } break; case 1 : switch (XPosition) { case -1 : left = PageBottom; break; default : left = (PageTop + PageBottom - xprint * 72) / 2; break; case 1 : left = PageTop - xprint * 72; break; } switch (YPosition) { case -1 : top = PageLeft + yprint * 72; break; default : top = (PageRight + PageLeft + yprint * 72) / 2; break; case 1 : top = PageRight; break; } break; case 2 : switch (XPosition) { case 1 : left = PageLeft; break; default : left = (PageRight + PageLeft - xprint * 72) / 2; break; case -1 : left = PageRight - xprint * 72; break; } switch (YPosition) { case 1 : top = PageBottom + yprint * 72; break; default : top = (PageTop + PageBottom + yprint * 72) / 2; break; case -1 : top = PageTop; break; } break; case 3 : switch (XPosition) { case 1 : left = PageBottom; break; default : left = (PageTop + PageBottom - xprint * 72) / 2; break; case -1 : left = PageTop - xprint * 72; break; } switch (YPosition) { case 1 : top = PageLeft + yprint * 72; break; default : top = (PageRight + PageLeft + yprint * 72) / 2; break; case -1 : top = PageRight; break; } break; } fprintf(stderr, "DEBUG: left=%.2f, top=%.2f\n", left, top); for (page = 1; Copies > 0; Copies --) for (xpage = 0; xpage < xpages; xpage ++) for (ypage = 0; ypage < ypages; ypage ++, page ++) { if (ppd && ppd->num_filters == 0) fprintf(stderr, "PAGE: %d %d\n", page, realcopies); _cupsLangPrintFilter(stderr, "INFO", _("Printing page %d."), page); printf("%%%%Page: %d %d\n", (page * (count+1)), (page*(count+1))); //TEST_TODO if(count>=1){ puts("%%BeginPageSetup"); if (Orientation & 1) puts("%%PageOrientation: Landscape"); else puts("%%PageOrientation: Portrait"); puts("%%EndPageSetup"); } ppdEmit(ppd, stdout, PPD_ORDER_PAGE); puts("gsave"); if (Flip) printf("%.0f 0 translate -1 1 scale\n", PageWidth); switch (Orientation) { case 1 : /* Landscape */ printf("%.0f 0 translate 90 rotate\n", PageWidth); break; case 2 : /* Reverse Portrait */ printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength); break; case 3 : /* Reverse Landscape */ printf("0 %.0f translate -90 rotate\n", PageLength); break; } puts("gsave"); xc0 = cupsImageGetWidth(img) * xpage / xpages; xc1 = cupsImageGetWidth(img) * (xpage + 1) / xpages - 1; yc0 = cupsImageGetHeight(img) * ypage / ypages; yc1 = cupsImageGetHeight(img) * (ypage + 1) / ypages - 1; printf("%.1f %.1f translate\n", left, top); printf("%.3f %.3f scale\n\n",xprint * 72.0 / (xc1 - xc0 + 1),yprint * 72.0 / (yc1 - yc0 + 1)); if (LanguageLevel == 1) { printf("/picture %d string def\n", (xc1 - xc0 + 1) * abs(colorspace)); printf("%d %d 8[1 0 0 -1 0 1]", (xc1 - xc0 + 1), (yc1 - yc0 + 1)); if (colorspace == CUPS_IMAGE_WHITE) puts("{currentfile picture readhexstring pop} image"); else printf("{currentfile picture readhexstring pop} false %d colorimage\n",abs(colorspace)); for (y = yc0; y <= yc1; y ++) { cupsImageGetRow(img, xc0, y, xc1 - xc0 + 1, row); ps_hex(row, (xc1 - xc0 + 1) * abs(colorspace), y == yc1); } } else { switch (colorspace) { case CUPS_IMAGE_WHITE : puts("/DeviceGray setcolorspace"); break; case CUPS_IMAGE_RGB : puts("/DeviceRGB setcolorspace"); break; case CUPS_IMAGE_CMYK : puts("/DeviceCMYK setcolorspace"); break; } printf("<<" "/ImageType 1" "/Width %d" "/Height %d" "/BitsPerComponent 8", xc1 - xc0 + 1, yc1 - yc0 + 1); switch (colorspace) { case CUPS_IMAGE_WHITE : fputs("/Decode[0 1]", stdout); break; case CUPS_IMAGE_RGB : fputs("/Decode[0 1 0 1 0 1]", stdout); break; case CUPS_IMAGE_CMYK : fputs("/Decode[0 1 0 1 0 1 0 1]", stdout); break; } fputs("\n/DataSource currentfile/ASCII85Decode filter", stdout); if (((xc1 - xc0 + 1) / xprint) < 100.0) fputs("/Interpolate true", stdout); puts("/ImageMatrix[1 0 0 -1 0 1]>>image"); for (y = yc0, out_offset = 0; y <= yc1; y ++) { cupsImageGetRow(img, xc0, y, xc1 - xc0 + 1, row + out_offset); out_length = (xc1 - xc0 + 1) * abs(colorspace) + out_offset; out_offset = out_length & 3; ps_ascii85(row, out_length, y == yc1); if (out_offset > 0) memcpy(row, row + out_length - out_offset, out_offset); } } if(count==1){ puts("grestore"); WriteLabels(0); puts("grestore"); } puts("showpage"); puts("%%PageTrailer"); } } //end of big for loop puts("%%EOF"); /* * End the job with the appropriate JCL command or CTRL-D otherwise. */ if (emit_jcl) { if (ppd && ppd->jcl_end) ppdEmitJCLEnd(ppd, stdout); else putchar(0x04); } fflush(stdout); dup2(defout,1); close(outfd); close(defout); /* * Close files... */ LOGI("imagetops:Close files"); cupsImageClose(img); ppdClose(ppd); return (0); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments (6 or 7) */ char *argv[]) /* I - Command-line arguments */ { const char *device_uri; /* Device URI */ char scheme[255], /* Scheme in URI */ hostname[1024], /* Hostname */ username[255], /* Username info (not used) */ resource[1024], /* Resource info (not used) */ *options, /* Pointer to options */ *name, /* Name of option */ *value, /* Value of option */ sep; /* Option separator */ int print_fd; /* Print file */ int copies; /* Number of copies to print */ time_t start_time; /* Time of first connect */ #ifdef __APPLE__ time_t current_time, /* Current time */ wait_time; /* Time to wait before shutting down socket */ #endif /* __APPLE__ */ int contimeout; /* Connection timeout */ int waiteof; /* Wait for end-of-file? */ int port; /* Port number */ char portname[255]; /* Port name */ int delay; /* Delay for retries... */ int device_fd; /* AppSocket */ int error; /* Error code (if any) */ http_addrlist_t *addrlist, /* Address list */ *addr; /* Connected address */ char addrname[256]; /* Address name */ int snmp_enabled = 1; /* Is SNMP enabled? */ int snmp_fd, /* SNMP socket */ start_count, /* Page count via SNMP at start */ page_count, /* Page count via SNMP */ have_supplies; /* Printer supports supply levels? */ ssize_t bytes = 0, /* Initial bytes read */ tbytes; /* Total number of bytes written */ char buffer[1024]; /* Initial print buffer */ #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ /* * Make sure status messages are not buffered... */ setbuf(stderr, NULL); /* * Ignore SIGPIPE signals... */ #ifdef HAVE_SIGSET sigset(SIGPIPE, SIG_IGN); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); action.sa_handler = SIG_IGN; sigaction(SIGPIPE, &action, NULL); #else signal(SIGPIPE, SIG_IGN); #endif /* HAVE_SIGSET */ /* * Check command-line... */ if (argc == 1) { printf("network socket \"Unknown\" \"%s\"\n", _cupsLangString(cupsLangDefault(), _("AppSocket/HP JetDirect"))); return (CUPS_BACKEND_OK); } else if (argc < 6 || argc > 7) { _cupsLangPrintf(stderr, _("Usage: %s job-id user title copies options [file]"), argv[0]); return (CUPS_BACKEND_FAILED); } /* * If we have 7 arguments, print the file named on the command-line. * Otherwise, send stdin instead... */ if (argc == 6) { print_fd = 0; copies = 1; } else { /* * Try to open the print file... */ if ((print_fd = open(argv[6], O_RDONLY)) < 0) { _cupsLangPrintError("ERROR", _("Unable to open print file")); return (CUPS_BACKEND_FAILED); } copies = atoi(argv[4]); } /* * Extract the hostname and port number from the URI... */ while ((device_uri = cupsBackendDeviceURI(argv)) == NULL) { _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer.")); sleep(10); if (getenv("CLASS") != NULL) return (CUPS_BACKEND_FAILED); } httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme), username, sizeof(username), hostname, sizeof(hostname), &port, resource, sizeof(resource)); if (port == 0) port = 9100; /* Default to HP JetDirect/Tektronix PhaserShare */ /* * Get options, if any... */ waiteof = 1; contimeout = 7 * 24 * 60 * 60; if ((options = strchr(resource, '?')) != NULL) { /* * Yup, terminate the device name string and move to the first * character of the options... */ *options++ = '\0'; /* * Parse options... */ while (*options) { /* * Get the name... */ name = options; while (*options && *options != '=' && *options != '+' && *options != '&') options ++; if ((sep = *options) != '\0') *options++ = '\0'; if (sep == '=') { /* * Get the value... */ value = options; while (*options && *options != '+' && *options != '&') options ++; if (*options) *options++ = '\0'; } else value = (char *)""; /* * Process the option... */ if (!_cups_strcasecmp(name, "waiteof")) { /* * Set the wait-for-eof value... */ waiteof = !value[0] || !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "yes") || !_cups_strcasecmp(value, "true"); } else if (!_cups_strcasecmp(name, "snmp")) { /* * Enable/disable SNMP stuff... */ snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") || _cups_strcasecmp(value, "yes") || _cups_strcasecmp(value, "true"); } else if (!_cups_strcasecmp(name, "contimeout")) { /* * Set the connection timeout... */ if (atoi(value) > 0) contimeout = atoi(value); } } } /* * Then try finding the remote host... */ start_time = time(NULL); sprintf(portname, "%d", port); fputs("STATE: +connecting-to-device\n", stderr); fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname); while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) { _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer \"%s\"."), hostname); sleep(10); if (getenv("CLASS") != NULL) { fputs("STATE: -connecting-to-device\n", stderr); return (CUPS_BACKEND_STOP); } } /* * See if the printer supports SNMP... */ if (snmp_enabled) snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family); else snmp_fd = -1; if (snmp_fd >= 0) have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr), &start_count, NULL); else have_supplies = start_count = 0; /* * Wait for data from the filter... */ if (print_fd == 0) { if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 1, backendNetworkSideCB)) return (CUPS_BACKEND_OK); else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0) return (CUPS_BACKEND_OK); } /* * Connect to the printer... */ fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port); _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer.")); for (delay = 5;;) { if ((addr = httpAddrConnect(addrlist, &device_fd)) == NULL) { error = errno; device_fd = -1; if (getenv("CLASS") != NULL) { /* * If the CLASS environment variable is set, the job was submitted * to a class and not to a specific queue. In this case, we want * to abort immediately so that the job can be requeued on the next * available printer in the class. */ _cupsLangPrintFilter(stderr, "INFO", _("Unable to contact printer, queuing on next " "printer in class.")); /* * Sleep 5 seconds to keep the job from requeuing too rapidly... */ sleep(5); return (CUPS_BACKEND_FAILED); } fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(error)); if (error == ECONNREFUSED || error == EHOSTDOWN || error == EHOSTUNREACH) { if (contimeout && (time(NULL) - start_time) > contimeout) { _cupsLangPrintFilter(stderr, "ERROR", _("The printer is not responding.")); return (CUPS_BACKEND_FAILED); } switch (error) { case EHOSTDOWN : _cupsLangPrintFilter(stderr, "WARNING", _("The printer may not exist or " "is unavailable at this time.")); break; case EHOSTUNREACH : _cupsLangPrintFilter(stderr, "WARNING", _("The printer is unreachable at this " "time.")); break; case ECONNREFUSED : default : _cupsLangPrintFilter(stderr, "WARNING", _("The printer is in use.")); break; } sleep(delay); if (delay < 30) delay += 5; } else { _cupsLangPrintFilter(stderr, "ERROR", _("The printer is not responding.")); sleep(30); } } else break; } fputs("STATE: -connecting-to-device\n", stderr); _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer.")); fprintf(stderr, "DEBUG: Connected to %s:%d...\n", httpAddrString(&(addr->addr), addrname, sizeof(addrname)), _httpAddrPort(&(addr->addr))); /* * Print everything... */ tbytes = 0; if (bytes > 0) tbytes += write(device_fd, buffer, bytes); while (copies > 0 && tbytes >= 0) { copies --; if (print_fd != 0) { fputs("PAGE: 1 1\n", stderr); lseek(print_fd, 0, SEEK_SET); } tbytes = backendRunLoop(print_fd, device_fd, snmp_fd, &(addrlist->addr), 1, 0, backendNetworkSideCB); if (print_fd != 0 && tbytes >= 0) _cupsLangPrintFilter(stderr, "INFO", _("Print file sent.")); } #ifdef __APPLE__ /* * Wait up to 5 seconds to get any pending back-channel data... */ wait_time = time(NULL) + 5; while (wait_time >= time(¤t_time)) if (wait_bc(device_fd, wait_time - current_time) <= 0) break; #endif /* __APPLE__ */ if (waiteof) { /* * Shutdown the socket and wait for the other end to finish... */ _cupsLangPrintFilter(stderr, "INFO", _("Waiting for printer to finish.")); shutdown(device_fd, 1); while (wait_bc(device_fd, 90) > 0); } /* * Collect the final page count as needed... */ if (have_supplies && !backendSNMPSupplies(snmp_fd, &(addrlist->addr), &page_count, NULL) && page_count > start_count) fprintf(stderr, "PAGE: total %d\n", page_count - start_count); /* * Close the socket connection... */ close(device_fd); httpAddrFreeList(addrlist); /* * Close the input file and return... */ if (print_fd != 0) close(print_fd); return (CUPS_BACKEND_OK); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { cups_file_t *fp; /* File */ char buffer[8192]; /* Data buffer */ int bytes; /* Number of bytes read/written */ int copies; /* Number of copies */ /* * Check command-line... */ if (argc != 7) { _cupsLangPrintf(stderr, _("Usage: %s job-id user title copies options [file]"), argv[0]); return (1); } /* * Get the copy count; if we have no final content type, this is a * raw queue or raw print file, so we need to make copies... */ if (!getenv("FINAL_CONTENT_TYPE")) copies = atoi(argv[4]); else copies = 1; /* * Open the file... */ if ((fp = cupsFileOpen(argv[6], "r")) == NULL) { _cupsLangPrintError("ERROR", _("Unable to open print file")); return (1); } /* * Copy the file to stdout... */ while (copies > 0) { if (!getenv("FINAL_CONTENT_TYPE")) fputs("PAGE: 1 1\n", stderr); cupsFileRewind(fp); while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) if (write(1, buffer, bytes) < bytes) { _cupsLangPrintFilter(stderr, "ERROR", _("Unable to write uncompressed print data: %s"), strerror(errno)); cupsFileClose(fp); return (1); } copies --; } /* * Close the file and return... */ cupsFileClose(fp); return (0); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line args */ char *argv[]) /* I - Command-line arguments */ { int fd; /* Raster file */ cups_raster_t *inras, /* Input raster stream */ *outras; /* Output raster stream */ cups_page_header2_t inheader, /* Input raster page header */ outheader; /* Output raster page header */ int y; /* Current line */ unsigned char *line; /* Line buffer */ int page = 0, /* Current page */ page_width, /* Actual page width */ page_height, /* Actual page height */ page_top, /* Top margin */ page_bottom, /* Bottom margin */ page_left, /* Left margin */ linesize, /* Bytes per line */ lineoffset; /* Offset into line */ unsigned char white; /* White pixel */ ppd_file_t *ppd; /* PPD file */ ppd_attr_t *back; /* cupsBackSize attribute */ _ppd_cache_t *cache; /* PPD cache */ _pwg_size_t *pwg_size; /* PWG media size */ _pwg_media_t *pwg_media; /* PWG media name */ int num_options; /* Number of options */ cups_option_t *options = NULL;/* Options */ const char *val; /* Option value */ if (argc < 6 || argc > 7) { puts("Usage: rastertopwg job user title copies options [filename]"); return (1); } else if (argc == 7) { if ((fd = open(argv[6], O_RDONLY)) < 0) { perror("ERROR: Unable to open print file"); return (1); } } else fd = 0; inras = cupsRasterOpen(fd, CUPS_RASTER_READ); outras = cupsRasterOpen(1, CUPS_RASTER_WRITE_PWG); ppd = ppdOpenFile(getenv("PPD")); back = ppdFindAttr(ppd, "cupsBackSide", NULL); num_options = cupsParseOptions(argv[5], 0, &options); ppdMarkDefaults(ppd); cupsMarkOptions(ppd, num_options, options); cache = ppd ? ppd->cache : NULL; while (cupsRasterReadHeader2(inras, &inheader)) { /* * Compute the real raster size... */ page ++; fprintf(stderr, "PAGE: %d %d\n", page, inheader.NumCopies); page_width = (int)(inheader.cupsPageSize[0] * inheader.HWResolution[0] / 72.0); page_height = (int)(inheader.cupsPageSize[1] * inheader.HWResolution[1] / 72.0); page_left = (int)(inheader.cupsImagingBBox[0] * inheader.HWResolution[0] / 72.0); page_bottom = (int)(inheader.cupsImagingBBox[1] * inheader.HWResolution[1] / 72.0); page_top = page_height - page_bottom - inheader.cupsHeight; linesize = (page_width * inheader.cupsBitsPerPixel + 7) / 8; lineoffset = page_left * inheader.cupsBitsPerPixel / 8; /* Round down */ switch (inheader.cupsColorSpace) { case CUPS_CSPACE_W : case CUPS_CSPACE_RGB : case CUPS_CSPACE_SW : case CUPS_CSPACE_SRGB : case CUPS_CSPACE_ADOBERGB : white = 255; break; case CUPS_CSPACE_K : case CUPS_CSPACE_CMYK : case CUPS_CSPACE_DEVICE1 : case CUPS_CSPACE_DEVICE2 : case CUPS_CSPACE_DEVICE3 : case CUPS_CSPACE_DEVICE4 : case CUPS_CSPACE_DEVICE5 : case CUPS_CSPACE_DEVICE6 : case CUPS_CSPACE_DEVICE7 : case CUPS_CSPACE_DEVICE8 : case CUPS_CSPACE_DEVICE9 : case CUPS_CSPACE_DEVICEA : case CUPS_CSPACE_DEVICEB : case CUPS_CSPACE_DEVICEC : case CUPS_CSPACE_DEVICED : case CUPS_CSPACE_DEVICEE : case CUPS_CSPACE_DEVICEF : white = 0; break; default : _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data.")); fprintf(stderr, "DEBUG: Unsupported cupsColorSpace %d on page %d.\n", inheader.cupsColorSpace, page); return (1); } if (inheader.cupsColorOrder != CUPS_ORDER_CHUNKED) { _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data.")); fprintf(stderr, "DEBUG: Unsupported cupsColorOrder %d on page %d.\n", inheader.cupsColorOrder, page); return (1); } if (inheader.cupsBitsPerPixel != 1 && inheader.cupsBitsPerColor != 8 && inheader.cupsBitsPerColor != 16) { _cupsLangPrintFilter(stderr, "ERROR", _("Unsupported raster data.")); fprintf(stderr, "DEBUG: Unsupported cupsBitsPerColor %d on page %d.\n", inheader.cupsBitsPerColor, page); return (1); } memcpy(&outheader, &inheader, sizeof(outheader)); outheader.cupsWidth = page_width; outheader.cupsHeight = page_height; outheader.cupsBytesPerLine = linesize; outheader.cupsInteger[14] = 0; /* VendorIdentifier */ outheader.cupsInteger[15] = 0; /* VendorLength */ if ((val = cupsGetOption("print-content-optimize", num_options, options)) != NULL) { if (!strcmp(val, "automatic")) strlcpy(outheader.OutputType, "Automatic", sizeof(outheader.OutputType)); else if (!strcmp(val, "graphics")) strlcpy(outheader.OutputType, "Graphics", sizeof(outheader.OutputType)); else if (!strcmp(val, "photo")) strlcpy(outheader.OutputType, "Photo", sizeof(outheader.OutputType)); else if (!strcmp(val, "text")) strlcpy(outheader.OutputType, "Text", sizeof(outheader.OutputType)); else if (!strcmp(val, "text-and-graphics")) strlcpy(outheader.OutputType, "TextAndGraphics", sizeof(outheader.OutputType)); else { fprintf(stderr, "DEBUG: Unsupported print-content-type \"%s\".\n", val); outheader.OutputType[0] = '\0'; } } if ((val = cupsGetOption("print-quality", num_options, options)) != NULL) { int quality = atoi(val); /* print-quality value */ if (quality >= IPP_QUALITY_DRAFT && quality <= IPP_QUALITY_HIGH) outheader.cupsInteger[8] = quality; else { fprintf(stderr, "DEBUG: Unsupported print-quality %d.\n", quality); outheader.cupsInteger[8] = 0; } } if ((val = cupsGetOption("print-rendering-intent", num_options, options)) != NULL) { if (!strcmp(val, "absolute")) strlcpy(outheader.cupsRenderingIntent, "Absolute", sizeof(outheader.cupsRenderingIntent)); else if (!strcmp(val, "automatic")) strlcpy(outheader.cupsRenderingIntent, "Automatic", sizeof(outheader.cupsRenderingIntent)); else if (!strcmp(val, "perceptual")) strlcpy(outheader.cupsRenderingIntent, "Perceptual", sizeof(outheader.cupsRenderingIntent)); else if (!strcmp(val, "relative")) strlcpy(outheader.cupsRenderingIntent, "Relative", sizeof(outheader.cupsRenderingIntent)); else if (!strcmp(val, "relative-bpc")) strlcpy(outheader.cupsRenderingIntent, "RelativeBpc", sizeof(outheader.cupsRenderingIntent)); else if (!strcmp(val, "saturation")) strlcpy(outheader.cupsRenderingIntent, "Saturation", sizeof(outheader.cupsRenderingIntent)); else { fprintf(stderr, "DEBUG: Unsupported print-rendering-intent \"%s\".\n", val); outheader.cupsRenderingIntent[0] = '\0'; } } if (inheader.cupsPageSizeName[0] && (pwg_size = _ppdCacheGetSize(cache, inheader.cupsPageSizeName)) != NULL) { strlcpy(outheader.cupsPageSizeName, pwg_size->map.pwg, sizeof(outheader.cupsPageSizeName)); } else { pwg_media = _pwgMediaForSize((int)(2540.0 * inheader.cupsPageSize[0] / 72.0), (int)(2540.0 * inheader.cupsPageSize[1] / 72.0)); if (pwg_media) strlcpy(outheader.cupsPageSizeName, pwg_media->pwg, sizeof(outheader.cupsPageSizeName)); else { fprintf(stderr, "DEBUG: Unsupported PageSize %.2fx%.2f.\n", inheader.cupsPageSize[0], inheader.cupsPageSize[1]); outheader.cupsPageSizeName[0] = '\0'; } } if (inheader.Duplex && !(page & 1) && back && _cups_strcasecmp(back->value, "Normal")) { if (_cups_strcasecmp(back->value, "Flipped")) { if (inheader.Tumble) { outheader.cupsInteger[1] = -1;/* CrossFeedTransform */ outheader.cupsInteger[2] = 1; /* FeedTransform */ outheader.cupsInteger[3] = page_width - page_left - inheader.cupsWidth; /* ImageBoxLeft */ outheader.cupsInteger[4] = page_top; /* ImageBoxTop */ outheader.cupsInteger[5] = page_width - page_left; /* ImageBoxRight */ outheader.cupsInteger[6] = page_height - page_bottom; /* ImageBoxBottom */ } else { outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ outheader.cupsInteger[2] = -1;/* FeedTransform */ outheader.cupsInteger[3] = page_left; /* ImageBoxLeft */ outheader.cupsInteger[4] = page_bottom; /* ImageBoxTop */ outheader.cupsInteger[5] = page_left + inheader.cupsWidth; /* ImageBoxRight */ outheader.cupsInteger[6] = page_height - page_top; /* ImageBoxBottom */ } } else if (_cups_strcasecmp(back->value, "ManualTumble")) { if (inheader.Tumble) { outheader.cupsInteger[1] = -1;/* CrossFeedTransform */ outheader.cupsInteger[2] = -1;/* FeedTransform */ outheader.cupsInteger[3] = page_width - page_left - inheader.cupsWidth; /* ImageBoxLeft */ outheader.cupsInteger[4] = page_bottom; /* ImageBoxTop */ outheader.cupsInteger[5] = page_width - page_left; /* ImageBoxRight */ outheader.cupsInteger[6] = page_height - page_top; /* ImageBoxBottom */ } else { outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ outheader.cupsInteger[2] = 1; /* FeedTransform */ outheader.cupsInteger[3] = page_left; /* ImageBoxLeft */ outheader.cupsInteger[4] = page_top; /* ImageBoxTop */ outheader.cupsInteger[5] = page_left + inheader.cupsWidth; /* ImageBoxRight */ outheader.cupsInteger[6] = page_height - page_bottom; /* ImageBoxBottom */ } } else if (_cups_strcasecmp(back->value, "Rotated")) { if (inheader.Tumble) { outheader.cupsInteger[1] = -1;/* CrossFeedTransform */ outheader.cupsInteger[2] = -1;/* FeedTransform */ outheader.cupsInteger[3] = page_width - page_left - inheader.cupsWidth; /* ImageBoxLeft */ outheader.cupsInteger[4] = page_bottom; /* ImageBoxTop */ outheader.cupsInteger[5] = page_width - page_left; /* ImageBoxRight */ outheader.cupsInteger[6] = page_height - page_top; /* ImageBoxBottom */ } else { outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ outheader.cupsInteger[2] = 1; /* FeedTransform */ outheader.cupsInteger[3] = page_left; /* ImageBoxLeft */ outheader.cupsInteger[4] = page_top; /* ImageBoxTop */ outheader.cupsInteger[5] = page_left + inheader.cupsWidth; /* ImageBoxRight */ outheader.cupsInteger[6] = page_height - page_bottom; /* ImageBoxBottom */ } } else { /* * Unsupported value... */ fprintf(stderr, "DEBUG: Unsupported cupsBackSide \"%s\".\n", back->value); outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ outheader.cupsInteger[2] = 1; /* FeedTransform */ outheader.cupsInteger[3] = page_left; /* ImageBoxLeft */ outheader.cupsInteger[4] = page_top; /* ImageBoxTop */ outheader.cupsInteger[5] = page_left + inheader.cupsWidth; /* ImageBoxRight */ outheader.cupsInteger[6] = page_height - page_bottom; /* ImageBoxBottom */ } } else { outheader.cupsInteger[1] = 1; /* CrossFeedTransform */ outheader.cupsInteger[2] = 1; /* FeedTransform */ outheader.cupsInteger[3] = page_left; /* ImageBoxLeft */ outheader.cupsInteger[4] = page_top; /* ImageBoxTop */ outheader.cupsInteger[5] = page_left + inheader.cupsWidth; /* ImageBoxRight */ outheader.cupsInteger[6] = page_height - page_bottom; /* ImageBoxBottom */ } if (!cupsRasterWriteHeader2(outras, &outheader)) { _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data.")); fprintf(stderr, "DEBUG: Unable to write header for page %d.\n", page); return (1); } /* * Copy raster data... */ line = malloc(linesize); memset(line, white, linesize); for (y = page_top; y > 0; y --) if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine)) { _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data.")); fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n", page_top - y + 1, page); return (1); } for (y = inheader.cupsHeight; y > 0; y --) { cupsRasterReadPixels(inras, line + lineoffset, inheader.cupsBytesPerLine); if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine)) { _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data.")); fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n", inheader.cupsHeight - y + page_top + 1, page); return (1); } } memset(line, white, linesize); for (y = page_bottom; y > 0; y --) if (!cupsRasterWritePixels(outras, line, outheader.cupsBytesPerLine)) { _cupsLangPrintFilter(stderr, "ERROR", _("Error sending raster data.")); fprintf(stderr, "DEBUG: Unable to write line %d for page %d.\n", page_bottom - y + page_top + inheader.cupsHeight + 1, page); return (1); } free(line); } cupsRasterClose(inras); if (fd) close(fd); cupsRasterClose(outras); return (0); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments (6 or 7) */ char *argv[]) /* I - Command-line arguments */ { int print_fd; /* Print file */ int copies; /* Number of copies to print */ int status; /* Exit status */ int port; /* Port number (not used) */ const char *uri; /* Device URI */ char method[255], /* Method in URI */ hostname[1024], /* Hostname */ username[255], /* Username info (not used) */ resource[1024], /* Resource info (device and options) */ *options; /* Pointer to options */ #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ /* * Make sure status messages are not buffered... */ setbuf(stderr, NULL); /* * Ignore SIGPIPE signals... */ #ifdef HAVE_SIGSET sigset(SIGPIPE, SIG_IGN); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); action.sa_handler = SIG_IGN; sigaction(SIGPIPE, &action, NULL); #else signal(SIGPIPE, SIG_IGN); #endif /* HAVE_SIGSET */ /* * Check command-line... */ if (argc == 1) { list_devices(); return (CUPS_BACKEND_OK); } else if (argc < 6 || argc > 7) { _cupsLangPrintf(stderr, _("Usage: %s job-id user title copies options [file]"), argv[0]); return (CUPS_BACKEND_FAILED); } /* * Extract the device name and options from the URI... */ uri = cupsBackendDeviceURI(argv); if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method), username, sizeof(username), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_OK) { _cupsLangPrintFilter(stderr, "ERROR", _("No device URI found in argv[0] or in DEVICE_URI " "environment variable.")); return (1); } /* * See if there are any options... */ if ((options = strchr(resource, '?')) != NULL) { /* * Yup, terminate the device name string and move to the first * character of the options... */ *options++ = '\0'; } /* * If we have 7 arguments, print the file named on the command-line. * Otherwise, send stdin instead... */ if (argc == 6) { print_fd = 0; copies = 1; } else { /* * Try to open the print file... */ if ((print_fd = open(argv[6], O_RDONLY)) < 0) { _cupsLangPrintError("ERROR", _("Unable to open print file")); return (CUPS_BACKEND_FAILED); } copies = atoi(argv[4]); } /* * Finally, send the print file... */ status = print_device(uri, hostname, resource, options, print_fd, copies, argc, argv); /* * Close the input file and return... */ if (print_fd != 0) close(print_fd); return (status); }