#include "backend-private.h"
#ifdef __hpux
# include <sys/time.h>
#else
# include <sys/select.h>
#endif
#ifdef WIN32
# include <io.h>
#else
# include <unistd.h>
# include <fcntl.h>
# include <termios.h>
# include <sys/socket.h>
#endif
#ifdef __sgi
# include <invent.h>
# ifndef INV_EPP_ECP_PLP
# define INV_EPP_ECP_PLP 6
# define INV_ASO_SERIAL 14
# define INV_IOC3_DMA 16
# define INV_IOC3_PIO 17
# define INV_ISA_DMA 19
# endif
#endif
static void list_devices(void);
static void side_cb(int print_fd, int device_fd, int use_bc);
int
main(int argc,
char *argv[])
{
char method[255],
hostname[1024],
username[255],
resource[1024],
*options;
int port;
int print_fd,
device_fd,
use_bc;
int copies;
size_t tbytes;
struct termios opts;
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action;
#endif
setbuf(stderr, NULL);
#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
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]\n"),
argv[0]);
return (CUPS_BACKEND_FAILED);
}
if (argc == 6)
{
print_fd = 0;
copies = 1;
}
else
{
if ((print_fd = open(argv[6], O_RDONLY)) < 0)
{
perror("ERROR: unable to open print file");
return (CUPS_BACKEND_FAILED);
}
copies = atoi(argv[4]);
}
httpSeparateURI(HTTP_URI_CODING_ALL, cupsBackendDeviceURI(argv),
method, sizeof(method), username, sizeof(username),
hostname, sizeof(hostname), &port,
resource, sizeof(resource));
if ((options = strchr(resource, '?')) != NULL)
{
*options++ = '\0';
}
fputs("STATE: +connecting-to-device\n", stderr);
do
{
#if defined(__linux) || defined(__FreeBSD__)
device_fd = open(resource, O_WRONLY | O_EXCL);
use_bc = 0;
#else
if ((device_fd = open(resource, O_RDWR | O_EXCL)) < 0)
{
device_fd = open(resource, O_WRONLY | O_EXCL);
use_bc = 0;
}
else
use_bc = 1;
#endif
if (device_fd == -1)
{
if (getenv("CLASS") != NULL)
{
_cupsLangPuts(stderr,
_("INFO: Unable to contact printer, queuing on next "
"printer in class...\n"));
sleep(5);
return (CUPS_BACKEND_FAILED);
}
if (errno == EBUSY)
{
_cupsLangPuts(stderr,
_("INFO: Printer busy; will retry in 30 seconds...\n"));
sleep(30);
}
else if (errno == ENXIO || errno == EIO || errno == ENOENT)
{
_cupsLangPuts(stderr,
_("INFO: Printer not connected; will retry in 30 "
"seconds...\n"));
sleep(30);
}
else
{
_cupsLangPrintf(stderr,
_("ERROR: Unable to open device file \"%s\": %s\n"),
resource, strerror(errno));
return (CUPS_BACKEND_FAILED);
}
}
}
while (device_fd < 0);
fputs("STATE: -connecting-to-device\n", stderr);
tcgetattr(device_fd, &opts);
opts.c_lflag &= ~(ICANON | ECHO | ISIG);
tcsetattr(device_fd, TCSANOW, &opts);
tbytes = 0;
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, use_bc, side_cb);
if (print_fd != 0 && tbytes >= 0)
_cupsLangPrintf(stderr,
#ifdef HAVE_LONG_LONG
_("INFO: Sent print file, %lld bytes...\n"),
#else
_("INFO: Sent print file, %ld bytes...\n"),
#endif
CUPS_LLCAST tbytes);
}
close(device_fd);
if (print_fd != 0)
close(print_fd);
return (tbytes < 0 ? CUPS_BACKEND_FAILED : CUPS_BACKEND_OK);
}
static void
list_devices(void)
{
#if defined(__hpux) || defined(__sgi) || defined(__sun)
static char *funky_hex = "0123456789abcdefghijklmnopqrstuvwxyz";
#endif
#ifdef __linux
int i;
int fd;
char device[255],
basedevice[255],
device_id[1024],
make_model[1024],
uri[1024];
if (!access("/dev/parallel/", 0))
strcpy(basedevice, "/dev/parallel/");
else if (!access("/dev/printers/", 0))
strcpy(basedevice, "/dev/printers/");
else
strcpy(basedevice, "/dev/lp");
for (i = 0; i < 4; i ++)
{
sprintf(device, "%s%d", basedevice, i);
if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
fd = open(device, O_WRONLY);
if (fd >= 0)
{
snprintf(uri, sizeof(uri), "parallel:%s", device);
if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
make_model, sizeof(make_model),
NULL, uri, sizeof(uri)))
printf("direct %s \"%s\" \"%s LPT #%d\" \"%s\"\n", uri,
make_model, make_model, i + 1, device_id);
else
printf("direct %s \"Unknown\" \"LPT #%d\"\n", uri, i + 1);
close(fd);
}
}
#elif defined(__sgi)
int i, j, n;
char device[255];
inventory_t *inv;
setinvent();
while ((inv = getinvent()) != NULL)
{
if (inv->inv_class == INV_PARALLEL &&
(inv->inv_type == INV_ONBOARD_PLP ||
inv->inv_type == INV_EPP_ECP_PLP))
{
puts("direct parallel:/dev/plp \"Unknown\" \"Onboard Parallel Port\"");
}
else if (inv->inv_class == INV_PARALLEL &&
inv->inv_type == INV_EPC_PLP)
{
printf("direct parallel:/dev/plp%d \"Unknown\" \"Integral EPC parallel port, Ebus slot %d\"\n",
inv->inv_controller, inv->inv_controller);
}
}
endinvent();
for (i = 0; i < 10; i ++)
for (j = 0; j < 8; j ++)
for (n = 0; n < 32; n ++)
{
if (i == 8)
sprintf(device, "/dev/lpn%d%c", j, funky_hex[n]);
else if (i == 9)
sprintf(device, "/dev/lpp%d%c", j, funky_hex[n]);
else
sprintf(device, "/dev/lp%d%d%c", i, j, funky_hex[n]);
if (access(device, 0) == 0)
{
if (i == 8)
printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
device, j, n);
else if (i == 9)
printf("direct parallel:%s \"Unknown\" \"Central Data PCI Parallel Port, ID %d, port %d\"\n",
device, j, n);
else
printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
device, i, j, n);
}
}
#elif defined(__sun)
int i, j, n;
char device[255];
for (i = 0; i < 10; i ++)
{
sprintf(device, "/dev/ecpp%d", i);
if (access(device, 0) == 0)
printf("direct parallel:%s \"Unknown\" \"Sun IEEE-1284 Parallel Port #%d\"\n",
device, i + 1);
}
for (i = 0; i < 10; i ++)
{
sprintf(device, "/dev/bpp%d", i);
if (access(device, 0) == 0)
printf("direct parallel:%s \"Unknown\" \"Sun Standard Parallel Port #%d\"\n",
device, i + 1);
}
for (i = 0; i < 3; i ++)
{
sprintf(device, "/dev/lp%d", i);
if (access(device, 0) == 0)
printf("direct parallel:%s \"Unknown\" \"PC Parallel Port #%d\"\n",
device, i + 1);
}
for (i = 0; i < 40; i ++)
{
sprintf(device, "/dev/pm%02d", i);
if (access(device, 0) == 0)
printf("direct parallel:%s \"Unknown\" \"MAGMA Parallel Board #%d Port #%d\"\n",
device, (i / 10) + 1, (i % 10) + 1);
}
for (i = 0; i < 9; i ++)
for (j = 0; j < 8; j ++)
for (n = 0; n < 32; n ++)
{
if (i == 8)
sprintf(device, "/dev/sts/lpN%d%c", j, funky_hex[n]);
else
sprintf(device, "/dev/sts/lp%c%d%c", i + 'C', j,
funky_hex[n]);
if (access(device, 0) == 0)
{
if (i == 8)
printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
device, j, n);
else
printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
device, i, j, n);
}
}
#elif defined(__hpux)
int i, j, n;
char device[255];
if (access("/dev/rlp", 0) == 0)
puts("direct parallel:/dev/rlp \"Unknown\" \"Standard Parallel Port (/dev/rlp)\"");
for (i = 0; i < 7; i ++)
for (j = 0; j < 7; j ++)
{
sprintf(device, "/dev/c%dt%dd0_lp", i, j);
if (access(device, 0) == 0)
printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d,%d\"\n",
device, i, j);
}
for (i = 0; i < 9; i ++)
for (j = 0; j < 8; j ++)
for (n = 0; n < 32; n ++)
{
if (i == 8)
sprintf(device, "/dev/lpN%d%c", j, funky_hex[n]);
else
sprintf(device, "/dev/lp%c%d%c", i + 'C', j,
funky_hex[n]);
if (access(device, 0) == 0)
{
if (i == 8)
printf("direct parallel:%s \"Unknown\" \"Central Data EtherLite Parallel Port, ID %d, port %d\"\n",
device, j, n);
else
printf("direct parallel:%s \"Unknown\" \"Central Data SCSI Parallel Port, logical bus %d, ID %d, port %d\"\n",
device, i, j, n);
}
}
#elif defined(__osf__)
int i;
int fd;
char device[255];
for (i = 0; i < 3; i ++)
{
sprintf(device, "/dev/lp%d", i);
if ((fd = open(device, O_WRONLY)) >= 0)
{
close(fd);
printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device, i + 1);
}
}
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
int i;
int fd;
char device[255];
for (i = 0; i < 3; i ++)
{
sprintf(device, "/dev/lpt%d", i);
if ((fd = open(device, O_WRONLY)) >= 0)
{
close(fd);
printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (interrupt-driven)\"\n", device, i + 1);
}
sprintf(device, "/dev/lpa%d", i);
if ((fd = open(device, O_WRONLY)) >= 0)
{
close(fd);
printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d (polled)\"\n", device, i + 1);
}
}
#elif defined(_AIX)
int i;
int fd;
char device[255];
for (i = 0; i < 8; i ++)
{
sprintf(device, "/dev/lp%d", i);
if ((fd = open(device, O_WRONLY)) >= 0)
{
close(fd);
printf("direct parallel:%s \"Unknown\" \"Parallel Port #%d\"\n", device, i + 1);
}
}
#endif
}
static void
side_cb(int print_fd,
int device_fd,
int use_bc)
{
cups_sc_command_t command;
cups_sc_status_t status;
char data[2048];
int datalen;
datalen = sizeof(data);
if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
{
_cupsLangPuts(stderr, _("WARNING: Failed to read side-channel request!\n"));
return;
}
switch (command)
{
case CUPS_SC_CMD_DRAIN_OUTPUT :
if (backendDrainOutput(print_fd, device_fd))
status = CUPS_SC_STATUS_IO_ERROR;
else if (tcdrain(device_fd))
status = CUPS_SC_STATUS_IO_ERROR;
else
status = CUPS_SC_STATUS_OK;
datalen = 0;
break;
case CUPS_SC_CMD_GET_BIDI :
data[0] = use_bc;
datalen = 1;
break;
case CUPS_SC_CMD_GET_DEVICE_ID :
memset(data, 0, sizeof(data));
if (backendGetDeviceID(device_fd, data, sizeof(data) - 1,
NULL, 0, NULL, NULL, 0))
{
status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
datalen = 0;
}
else
{
status = CUPS_SC_STATUS_OK;
datalen = strlen(data);
}
break;
default :
status = CUPS_SC_STATUS_NOT_IMPLEMENTED;
datalen = 0;
break;
}
cupsSideChannelWrite(command, status, data, datalen, 1.0);
}