#include <sys/select.h>
static int open_device(const char *uri, int *use_bc);
static int side_cb(int print_fd, int device_fd, int snmp_fd,
http_addr_t *addr, int use_bc);
int
print_device(const char *uri,
const char *hostname,
const char *resource,
char *options,
int print_fd,
int copies,
int argc,
char *argv[])
{
int use_bc;
int device_fd;
ssize_t tbytes;
struct termios opts;
(void)argc;
(void)argv;
fputs("STATE: +connecting-to-device\n", stderr);
do
{
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
use_bc = 0;
#elif defined(__sun)
use_bc = 0;
#else
use_bc = _cups_strcasecmp(hostname, "Brother") &&
_cups_strcasecmp(hostname, "Canon") &&
_cups_strncasecmp(hostname, "Konica", 6) &&
_cups_strncasecmp(hostname, "Minolta", 7);
#endif
if ((device_fd = open_device(uri, &use_bc)) == -1)
{
if (getenv("CLASS") != NULL)
{
_cupsLangPrintFilter(stderr, "INFO",
_("Unable to contact printer, queuing on next "
"printer in class."));
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);
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);
}
#ifdef __sun
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
if (print_fd != 0 && tbytes >= 0)
_cupsLangPrintFilter(stderr, "INFO", _("Print file sent."));
}
close(device_fd);
return (CUPS_BACKEND_OK);
}
void
list_devices(void)
{
#ifdef __linux
int i;
int fd;
char device[255],
device_id[1024],
device_uri[1024],
make_model[1024];
for (i = 0; i < 16; i ++)
{
sprintf(device, "/dev/usblp%d", i);
if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
{
if (errno != ENOENT)
continue;
sprintf(device, "/dev/usb/lp%d", i);
if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
{
if (errno != ENOENT)
continue;
sprintf(device, "/dev/usb/usblp%d", i);
if ((fd = open(device, O_RDWR | O_EXCL)) < 0)
continue;
}
}
if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
make_model, sizeof(make_model),
"usb", device_uri, sizeof(device_uri)))
cupsBackendReport("direct", device_uri, make_model, make_model,
device_id, NULL);
close(fd);
}
#elif defined(__sgi)
#elif defined(__sun) && defined(ECPPIOC_GETDEVID)
int i;
int fd;
char device[255],
device_id[1024],
device_uri[1024],
make_model[1024];
for (i = 0; i < 8; i ++)
{
sprintf(device, "/dev/usb/printer%d", i);
if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
{
if (!backendGetDeviceID(fd, device_id, sizeof(device_id),
make_model, sizeof(make_model),
"usb", device_uri, sizeof(device_uri)))
cupsBackendReport("direct", device_uri, make_model, make_model,
device_id, NULL);
close(fd);
}
}
#elif defined(__hpux)
#elif defined(__osf)
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
int i;
char device[255];
for (i = 0; i < 8; i ++)
{
sprintf(device, "/dev/ulpt%d", i);
if (!access(device, 0))
printf("direct usb:%s \"Unknown\" \"USB Printer #%d\"\n", device, i + 1);
sprintf(device, "/dev/unlpt%d", i);
if (!access(device, 0))
printf("direct usb:%s \"Unknown\" \"USB Printer #%d (no reset)\"\n", device, i + 1);
}
#endif
}
static int
open_device(const char *uri,
int *use_bc)
{
int fd;
if (!strncmp(uri, "usb:/dev/", 9))
#ifdef __linux
{
errno = ENODEV;
return (-1);
}
else if (!strncmp(uri, "usb://", 6))
{
int i;
int busy;
char device[255],
device_id[1024],
make_model[1024],
device_uri[1024];
for (;;)
{
for (busy = 0, i = 0; i < 16; i ++)
{
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 (errno == EBUSY)
busy = 1;
device_uri[0] = '\0';
}
if (!strcmp(uri, device_uri))
{
fprintf(stderr, "DEBUG: Printer using device file \"%s\"...\n",
device);
return (fd);
}
if (fd >= 0)
close(fd);
}
if (busy)
_cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
sleep(5);
}
}
#elif defined(__sun) && defined(ECPPIOC_GETDEVID)
{
errno = ENODEV;
return (-1);
}
else if (!strncmp(uri, "usb://", 6))
{
int i;
int busy;
char device[255],
device_id[1024],
make_model[1024],
device_uri[1024];
do
{
for (i = 0, busy = 0; i < 8; i ++)
{
sprintf(device, "/dev/usb/printer%d", i);
if ((fd = open(device, O_WRONLY | O_EXCL)) >= 0)
backendGetDeviceID(fd, device_id, sizeof(device_id),
make_model, sizeof(make_model),
"usb", device_uri, sizeof(device_uri));
else
{
if (errno == EBUSY)
busy = 1;
device_uri[0] = '\0';
}
if (!strcmp(uri, device_uri))
{
fputs("DEBUG: Setting use_bc to 0!\n", stderr);
*use_bc = 0;
return (fd);
}
if (fd >= 0)
close(fd);
}
if (busy)
{
_cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
sleep(5);
}
}
while (busy);
errno = ENODEV;
return (-1);
}
#else
{
if (*use_bc)
fd = open(uri + 4, O_RDWR | O_EXCL);
else
fd = -1;
if (fd < 0)
{
fd = open(uri + 4, O_WRONLY | O_EXCL);
*use_bc = 0;
}
return (fd);
}
#endif
else
{
errno = ENODEV;
return (-1);
}
}
static int
side_cb(int print_fd,
int device_fd,
int snmp_fd,
http_addr_t *addr,
int use_bc)
{
cups_sc_command_t command;
cups_sc_status_t status;
char data[2048];
int datalen;
(void)snmp_fd;
(void)addr;
datalen = sizeof(data);
if (cupsSideChannelRead(&command, &status, data, &datalen, 1.0))
return (-1);
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 :
status = CUPS_SC_STATUS_OK;
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;
}
return (cupsSideChannelWrite(command, status, data, datalen, 1.0));
}