#include <cups/cups.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <cups/string.h>
#include <signal.h>
#ifdef __hpux
# include <sys/modem.h>
#endif
#ifdef WIN32
# include <io.h>
#else
# include <unistd.h>
# include <fcntl.h>
# include <termios.h>
# ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
# endif
#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
#ifndef CRTSCTS
# ifdef CNEW_RTSCTS
# define CRTSCTS CNEW_RTSCTS
# else
# define CRTSCTS 0
# endif
#endif
#if defined(__APPLE__)
# include <CoreFoundation/CoreFoundation.h>
# include <IOKit/IOKitLib.h>
# include <IOKit/serial/IOSerialKeys.h>
# include <IOKit/IOBSD.h>
#endif
void list_devices(void);
int
main(int argc,
char *argv[])
{
char method[255],
hostname[1024],
username[255],
resource[1024],
*options,
name[255],
value[255],
*ptr;
int port;
int fp;
int copies;
int fd;
int wbytes;
size_t nbytes,
tbytes;
int dtrdsr;
int bufsize;
char buffer[8192],
*bufptr;
struct termios opts;
struct termios origopts;
#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 (0);
}
else if (argc < 6 || argc > 7)
{
fputs("Usage: serial job-id user title copies options [file]\n", stderr);
return (1);
}
if (argc == 6)
{
fp = 0;
copies = 1;
}
else
{
if ((fp = open(argv[6], O_RDONLY)) < 0)
{
perror("ERROR: unable to open print file");
return (1);
}
copies = atoi(argv[4]);
}
httpSeparate(argv[0], method, username, hostname, &port, resource);
if ((options = strchr(resource, '?')) != NULL)
{
*options++ = '\0';
}
do
{
if ((fd = open(resource, O_WRONLY | O_NOCTTY | O_EXCL | O_NDELAY)) == -1)
{
if (errno == EBUSY)
{
fputs("INFO: Serial port busy; will retry in 30 seconds...\n", stderr);
sleep(30);
}
else
{
fprintf(stderr, "ERROR: Unable to open serial port device file \"%s\": %s\n",
resource, strerror(errno));
return (1);
}
}
}
while (fd < 0);
tcgetattr(fd, &origopts);
tcgetattr(fd, &opts);
opts.c_lflag &= ~(ICANON | ECHO | ISIG);
opts.c_oflag &= ~OPOST;
bufsize = 96;
dtrdsr = 0;
if (options != NULL)
while (*options)
{
for (ptr = name; *options && *options != '=';)
*ptr++ = *options++;
*ptr = '\0';
if (*options == '=')
{
options ++;
for (ptr = value; *options && *options != '+';)
*ptr++ = *options++;
*ptr = '\0';
if (*options == '+')
options ++;
}
else
value[0] = '\0';
if (strcasecmp(name, "baud") == 0)
{
bufsize = atoi(value) / 100;
#if B19200 == 19200
cfsetispeed(&opts, atoi(value));
cfsetospeed(&opts, atoi(value));
#else
switch (atoi(value))
{
case 1200 :
cfsetispeed(&opts, B1200);
cfsetospeed(&opts, B1200);
break;
case 2400 :
cfsetispeed(&opts, B2400);
cfsetospeed(&opts, B2400);
break;
case 4800 :
cfsetispeed(&opts, B4800);
cfsetospeed(&opts, B4800);
break;
case 9600 :
cfsetispeed(&opts, B9600);
cfsetospeed(&opts, B9600);
break;
case 19200 :
cfsetispeed(&opts, B19200);
cfsetospeed(&opts, B19200);
break;
case 38400 :
cfsetispeed(&opts, B38400);
cfsetospeed(&opts, B38400);
break;
# ifdef B57600
case 57600 :
cfsetispeed(&opts, B57600);
cfsetospeed(&opts, B57600);
break;
# endif
# ifdef B115200
case 115200 :
cfsetispeed(&opts, B115200);
cfsetospeed(&opts, B115200);
break;
# endif
# ifdef B230400
case 230400 :
cfsetispeed(&opts, B230400);
cfsetospeed(&opts, B230400);
break;
# endif
default :
fprintf(stderr, "WARNING: Unsupported baud rate %s!\n", value);
break;
}
#endif
}
else if (strcasecmp(name, "bits") == 0)
{
switch (atoi(value))
{
case 7 :
opts.c_cflag &= ~CSIZE;
opts.c_cflag |= CS7;
opts.c_cflag |= PARENB;
opts.c_cflag &= ~PARODD;
break;
case 8 :
opts.c_cflag &= ~CSIZE;
opts.c_cflag |= CS8;
opts.c_cflag &= ~PARENB;
break;
}
}
else if (strcasecmp(name, "parity") == 0)
{
if (strcasecmp(value, "even") == 0)
{
opts.c_cflag |= PARENB;
opts.c_cflag &= ~PARODD;
}
else if (strcasecmp(value, "odd") == 0)
{
opts.c_cflag |= PARENB;
opts.c_cflag |= PARODD;
}
else if (strcasecmp(value, "none") == 0)
opts.c_cflag &= ~PARENB;
}
else if (strcasecmp(name, "flow") == 0)
{
if (strcasecmp(value, "none") == 0)
{
opts.c_iflag &= ~(IXON | IXOFF | IXANY);
opts.c_cflag &= ~CRTSCTS;
}
else if (strcasecmp(value, "soft") == 0)
{
opts.c_iflag |= IXON | IXOFF | IXANY;
opts.c_cflag &= ~CRTSCTS;
}
else if (strcasecmp(value, "hard") == 0 ||
strcasecmp(value, "rtscts") == 0)
{
opts.c_iflag &= ~(IXON | IXOFF | IXANY);
opts.c_cflag |= CRTSCTS;
}
else if (strcasecmp(value, "dtrdsr") == 0)
{
opts.c_iflag &= ~(IXON | IXOFF | IXANY);
opts.c_cflag &= ~CRTSCTS;
dtrdsr = 1;
}
}
}
tcsetattr(fd, TCSANOW, &opts);
fcntl(fd, F_SETFL, 0);
if (argc < 7)
{
#ifdef HAVE_SIGSET
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
}
if (bufsize > sizeof(buffer))
bufsize = sizeof(buffer);
while (copies > 0)
{
copies --;
if (fp != 0)
{
fputs("PAGE: 1 1\n", stderr);
lseek(fp, 0, SEEK_SET);
}
if (dtrdsr)
{
int status;
if (!ioctl(fd, TIOCMGET, &status))
if (!(status & TIOCM_DSR))
{
fputs("DEBUG: DSR is low; waiting for device...\n", stderr);
do
{
sleep(1);
if (ioctl(fd, TIOCMGET, &status))
break;
}
while (!(status & TIOCM_DSR));
fputs("DEBUG: DSR is high; writing to device...\n", stderr);
}
}
tbytes = 0;
while ((nbytes = read(fp, buffer, bufsize)) > 0)
{
tbytes += nbytes;
bufptr = buffer;
while (nbytes > 0)
{
if ((wbytes = write(fd, bufptr, nbytes)) < 0)
if (errno == ENOTTY)
wbytes = write(fd, bufptr, nbytes);
if (wbytes < 0)
{
perror("ERROR: Unable to send print file to printer");
break;
}
nbytes -= wbytes;
bufptr += wbytes;
}
if (argc > 6)
fprintf(stderr, "INFO: Sending print file, %lu bytes...\n",
(unsigned long)tbytes);
}
}
tcsetattr(fd, TCSADRAIN, &origopts);
close(fd);
if (fp != 0)
close(fp);
fputs("INFO: Ready to print.\n", stderr);
return (0);
}
void
list_devices(void)
{
#if defined(__hpux) || defined(__sgi) || defined(__sun) || defined(__FreeBSD__) || defined(__OpenBSD__)
static char *funky_hex = "0123456789abcdefghijklmnopqrstuvwxyz";
#endif
#if defined(__linux) || defined(linux) || defined(__linux__)
int i;
int fd;
char device[255];
for (i = 0; i < 100; i ++)
{
sprintf(device, "/dev/ttyS%d", i);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
# if defined(_ARCH_PPC) || defined(powerpc) || defined(__powerpc)
printf("serial serial:%s?baud=230400 \"Unknown\" \"Serial Port #%d\"\n",
device, i + 1);
# else
printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
device, i + 1);
# endif
}
}
for (i = 0; i < 16; i ++)
{
sprintf(device, "/dev/usb/ttyUSB%d", i);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
printf("serial serial:%s?baud=230400 \"Unknown\" \"USB Serial Port #%d\"\n",
device, i + 1);
}
}
#elif defined(__sgi)
int i, j, n;
char device[255];
inventory_t *inv;
setinvent();
while ((inv = getinvent()) != NULL)
{
if (inv->inv_class == INV_SERIAL)
{
if (inv->inv_type == INV_CDSIO || inv->inv_type == INV_CDSIO_E)
{
for (n = 0; n < 6; n ++)
printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"CDSIO Board %d Serial Port #%d\"\n",
n + 5 + 8 * inv->inv_controller, inv->inv_controller, n + 1);
}
else if (inv->inv_type == INV_EPC_SERIAL)
{
if (inv->inv_unit == 0)
i = 1;
else
i = 41 + 4 * (int)inv->inv_controller;
for (n = 0; n < (int)inv->inv_state; n ++)
printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"EPC Serial Port %d, Ebus slot %d\"\n",
n + i, n + 1, (int)inv->inv_controller);
}
else if (inv->inv_state > 1)
{
for (n = 0; n < (int)inv->inv_state; n ++)
printf("serial serial:/dev/ttyd%d?baud=38400 \"Unknown\" \"Onboard Serial Port %d\"\n",
n + (int)inv->inv_unit + 1, n + (int)inv->inv_unit + 1);
}
else
{
printf("serial serial:/dev/ttyd%d?baud=115200 \"Unknown\" \"Onboard Serial Port %d\"\n",
(int)inv->inv_controller, (int)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/ttydn%d%c", j, funky_hex[n]);
else if (i == 9)
sprintf(device, "/dev/ttydp%d%c", j, funky_hex[n]);
else
sprintf(device, "/dev/ttyd%d%d%c", i, j, funky_hex[n]);
if (access(device, 0) == 0)
{
if (i == 8)
printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
device, j, n);
else if (i == 9)
printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data PCI Serial Port, ID %d, port %d\"\n",
device, j, n);
else
printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial 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 < 26; i ++)
{
sprintf(device, "/dev/cua/%c", 'a' + i);
if (access(device, 0) == 0)
#ifdef B115200
printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
device, i + 1);
#else
printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
device, i + 1);
#endif
}
for (i = 0; i < 40; i ++)
{
sprintf(device, "/dev/term/%02d", i);
if (access(device, 0) == 0)
printf("serial serial:%s?baud=38400 \"Unknown\" \"MAGMA Serial 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/ttyN%d%c", j, funky_hex[n]);
else
sprintf(device, "/dev/sts/tty%c%d%c", i + 'C', j,
funky_hex[n]);
if (access(device, 0) == 0)
{
if (i == 8)
printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
device, j, n);
else
printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
device, i, j, n);
}
}
#elif defined(__hpux)
int i, j, n;
char device[255];
for (i = 0; i < 10; i ++)
{
sprintf(device, "/dev/tty%dp0", i);
if (access(device, 0) == 0)
printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
device, i + 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/ttyN%d%c", j, funky_hex[n]);
else
sprintf(device, "/dev/tty%c%d%c", i + 'C', j,
funky_hex[n]);
if (access(device, 0) == 0)
{
if (i == 8)
printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data EtherLite Serial Port, ID %d, port %d\"\n",
device, j, n);
else
printf("serial serial:%s?baud=38400 \"Unknown\" \"Central Data SCSI Serial Port, logical bus %d, ID %d, port %d\"\n",
device, i, j, n);
}
}
#elif defined(__osf__)
int i;
char device[255];
for (i = 0; i < 100; i ++)
{
sprintf(device, "/dev/tty%02d", i);
if (access(device, 0) == 0)
printf("serial serial:%s?baud=38400 \"Unknown\" \"Serial Port #%d\"\n",
device, i + 1);
}
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
int i, j;
int fd;
char device[255];
for (i = 0; i < 32; i ++)
{
sprintf(device, "/dev/ttyd%c", funky_hex[i]);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
printf("serial serial:%s?baud=115200 \"Unknown\" \"Standard Serial Port #%d\"\n",
device, i + 1);
}
}
for (i = 0; i < 16; i ++)
for (j = 0; j < 32; j ++)
{
sprintf(device, "/dev/ttyc%d%c", i, funky_hex[j]);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Port #%d\"\n",
device, i, j + 1);
}
}
for (i = 0; i < 16; i ++)
for (j = 0; j < 32; j ++)
{
sprintf(device, "/dev/ttyD%d%c", i, funky_hex[j]);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
printf("serial serial:%s?baud=115200 \"Unknown\" \"Digiboard #%d Serial Port #%d\"\n",
device, i, j + 1);
}
}
for (i = 0; i < 32; i ++)
{
sprintf(device, "/dev/ttyE%c", funky_hex[i]);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
printf("serial serial:%s?baud=115200 \"Unknown\" \"Stallion Serial Port #%d\"\n",
device, i + 1);
}
}
for (i = 0; i < 128; i ++)
{
sprintf(device, "/dev/ttyA%d", i + 1);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
printf("serial serial:%s?baud=115200 \"Unknown\" \"SX Serial Port #%d\"\n",
device, i + 1);
}
}
#elif defined(__NetBSD__)
int i, j;
int fd;
char device[255];
for (i = 0; i < 4; i ++)
{
sprintf(device, "/dev/tty%02d", i);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
printf("serial serial:%s?baud=115200 \"Unknown\" \"Serial Port #%d\"\n",
device, i + 1);
}
}
for (i = 0; i < 16; i ++)
for (j = 0; j < 64; j ++)
{
sprintf(device, "/dev/ttyCZ%02d%02d", i, j);
if ((fd = open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0)
{
close(fd);
printf("serial serial:%s?baud=115200 \"Unknown\" \"Cyclades #%d Serial Prt #%d\"\n",
device, i, j + 1);
}
}
#elif defined(__APPLE__)
kern_return_t kernResult;
mach_port_t masterPort;
io_iterator_t serialPortIterator;
CFMutableDictionaryRef classesToMatch;
io_object_t serialService;
printf("serial serial \"Unknown\" \"Serial Printer (serial)\"\n");
kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
if (KERN_SUCCESS != kernResult)
return;
classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
if (classesToMatch != NULL)
{
CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey),
CFSTR(kIOSerialBSDRS232Type));
kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch,
&serialPortIterator);
if (kernResult == KERN_SUCCESS)
{
while ((serialService = IOIteratorNext(serialPortIterator)))
{
CFTypeRef serialNameAsCFString;
CFTypeRef bsdPathAsCFString;
char serialName[128];
char bsdPath[1024];
Boolean result;
serialNameAsCFString =
IORegistryEntryCreateCFProperty(serialService,
CFSTR(kIOTTYDeviceKey),
kCFAllocatorDefault, 0);
if (serialNameAsCFString)
{
result = CFStringGetCString(serialNameAsCFString, serialName,
sizeof(serialName),
kCFStringEncodingASCII);
CFRelease(serialNameAsCFString);
if (result)
{
bsdPathAsCFString =
IORegistryEntryCreateCFProperty(serialService,
CFSTR(kIOCalloutDeviceKey),
kCFAllocatorDefault, 0);
if (bsdPathAsCFString)
{
result = CFStringGetCString(bsdPathAsCFString, bsdPath,
sizeof(bsdPath),
kCFStringEncodingASCII);
CFRelease(bsdPathAsCFString);
if (result)
printf("serial serial:%s?baud=115200 \"Unknown\" \"%s\"\n", bsdPath,
serialName);
}
}
}
IOObjectRelease(serialService);
}
IOObjectRelease(serialPortIterator);
}
}
#endif
}