2008-02-22 22:24:48 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2008-02-24 18:44:20 +00:00
|
|
|
* bare bones version of lpr & lpq: BSD printing utilities
|
2008-02-22 22:24:48 +00:00
|
|
|
*
|
2008-02-24 18:44:20 +00:00
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
|
|
|
* Original idea and code:
|
|
|
|
* Walter Harms <WHarms@bfs.de>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*
|
2008-02-24 22:08:57 +00:00
|
|
|
* See RFC 1179 for protocol description.
|
2008-02-22 22:24:48 +00:00
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
/*
|
|
|
|
* LPD returns binary 0 on success.
|
|
|
|
* Otherwise it returns error message.
|
2008-02-22 22:24:48 +00:00
|
|
|
*/
|
2008-02-27 14:35:21 +00:00
|
|
|
static void get_response_or_say_and_die(int fd, const char *errmsg)
|
2008-02-22 22:24:48 +00:00
|
|
|
{
|
2008-02-25 20:30:24 +00:00
|
|
|
ssize_t sz;
|
|
|
|
char buf[128];
|
2008-02-24 18:44:20 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
buf[0] = ' ';
|
2008-02-27 14:35:21 +00:00
|
|
|
sz = safe_read(fd, buf, 1);
|
2008-02-25 20:30:24 +00:00
|
|
|
if ('\0' != buf[0]) {
|
2008-02-24 18:44:20 +00:00
|
|
|
// request has failed
|
2008-02-25 20:30:24 +00:00
|
|
|
// try to make sure last char is '\n', but do not add
|
|
|
|
// superfluous one
|
2008-02-27 14:35:21 +00:00
|
|
|
sz = full_read(fd, buf + 1, 126);
|
2008-02-25 20:30:24 +00:00
|
|
|
bb_error_msg("error while %s%s", errmsg,
|
|
|
|
(sz > 0 ? ". Server said:" : ""));
|
|
|
|
if (sz > 0) {
|
|
|
|
// sz = (bytes in buf) - 1
|
|
|
|
if (buf[sz] != '\n')
|
|
|
|
buf[++sz] = '\n';
|
|
|
|
safe_write(STDERR_FILENO, buf, sz + 1);
|
|
|
|
}
|
2008-02-22 22:24:48 +00:00
|
|
|
xfunc_die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
|
2008-03-17 09:00:54 +00:00
|
|
|
int lpqr_main(int argc ATTRIBUTE_UNUSED, char *argv[])
|
2008-02-22 22:24:48 +00:00
|
|
|
{
|
|
|
|
enum {
|
2008-02-24 18:44:20 +00:00
|
|
|
OPT_P = 1 << 0, // -P queue[@host[:port]]. If no -P is given use $PRINTER, then "lp@localhost:515"
|
|
|
|
OPT_U = 1 << 1, // -U username
|
|
|
|
|
|
|
|
LPR_V = 1 << 2, // -V: be verbose
|
2008-03-24 02:05:58 +00:00
|
|
|
LPR_h = 1 << 3, // -h: want banner printed
|
2008-02-24 18:44:20 +00:00
|
|
|
LPR_C = 1 << 4, // -C class: job "class" (? supposedly printed on banner)
|
|
|
|
LPR_J = 1 << 5, // -J title: the job title for the banner page
|
|
|
|
LPR_m = 1 << 6, // -m: send mail back to user
|
|
|
|
|
|
|
|
LPQ_SHORT_FMT = 1 << 2, // -s: short listing format
|
|
|
|
LPQ_DELETE = 1 << 3, // -d: delete job(s)
|
|
|
|
LPQ_FORCE = 1 << 4, // -f: force waiting job(s) to be printed
|
2008-02-22 22:24:48 +00:00
|
|
|
};
|
2008-02-24 18:44:20 +00:00
|
|
|
char tempfile[sizeof("/tmp/lprXXXXXX")];
|
|
|
|
const char *job_title;
|
|
|
|
const char *printer_class = ""; // printer class, max 32 char
|
|
|
|
const char *queue; // name of printer queue
|
|
|
|
const char *server = "localhost"; // server[:port] of printer queue
|
|
|
|
char *hostname;
|
|
|
|
// N.B. IMHO getenv("USER") can be way easily spoofed!
|
|
|
|
const char *user = bb_getpwuid(NULL, -1, getuid());
|
|
|
|
unsigned job;
|
|
|
|
unsigned opts;
|
2008-02-27 14:35:21 +00:00
|
|
|
int fd;
|
2008-02-24 18:44:20 +00:00
|
|
|
|
|
|
|
// parse options
|
|
|
|
// TODO: set opt_complementary: s,d,f are mutually exclusive
|
|
|
|
opts = getopt32(argv,
|
|
|
|
(/*lp*/'r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf"
|
|
|
|
, &queue, &user
|
|
|
|
, &printer_class, &job_title
|
|
|
|
);
|
2008-02-22 22:24:48 +00:00
|
|
|
argv += optind;
|
2008-02-24 18:44:20 +00:00
|
|
|
|
|
|
|
// if queue is not specified -> use $PRINTER
|
|
|
|
if (!(opts & OPT_P))
|
|
|
|
queue = getenv("PRINTER");
|
|
|
|
// if queue is still not specified ->
|
|
|
|
if (!queue) {
|
|
|
|
// ... queue defaults to "lp"
|
|
|
|
// server defaults to "localhost"
|
|
|
|
queue = "lp";
|
|
|
|
// if queue is specified ->
|
|
|
|
} else {
|
|
|
|
// queue name is to the left of '@'
|
|
|
|
char *s = strchr(queue, '@');
|
|
|
|
if (s) {
|
|
|
|
// server name is to the right of '@'
|
|
|
|
*s = '\0';
|
|
|
|
server = s + 1;
|
|
|
|
}
|
2008-02-22 22:24:48 +00:00
|
|
|
}
|
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
// do connect
|
|
|
|
fd = create_and_connect_stream_or_die(server, 515);
|
|
|
|
|
|
|
|
//
|
|
|
|
// LPQ ------------------------
|
|
|
|
//
|
|
|
|
if (/*lp*/'q' == applet_name[2]) {
|
|
|
|
char cmd;
|
|
|
|
// force printing of every job still in queue
|
|
|
|
if (opts & LPQ_FORCE) {
|
|
|
|
cmd = 1;
|
|
|
|
goto command;
|
|
|
|
// delete job(s)
|
|
|
|
} else if (opts & LPQ_DELETE) {
|
2008-02-27 14:35:21 +00:00
|
|
|
fdprintf(fd, "\x5" "%s %s", queue, user);
|
2008-02-24 18:44:20 +00:00
|
|
|
while (*argv) {
|
2008-02-27 14:35:21 +00:00
|
|
|
fdprintf(fd, " %s", *argv++);
|
2008-02-24 18:44:20 +00:00
|
|
|
}
|
|
|
|
bb_putchar('\n');
|
|
|
|
// dump current jobs status
|
|
|
|
// N.B. periodical polling should be achieved
|
|
|
|
// via "watch -n delay lpq"
|
|
|
|
// They say it's the UNIX-way :)
|
|
|
|
} else {
|
|
|
|
cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
|
|
|
|
command:
|
2008-02-27 14:35:21 +00:00
|
|
|
fdprintf(fd, "%c" "%s\n", cmd, queue);
|
|
|
|
bb_copyfd_eof(fd, STDOUT_FILENO);
|
2008-02-24 18:44:20 +00:00
|
|
|
}
|
2008-02-22 22:24:48 +00:00
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2008-02-22 22:24:48 +00:00
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
//
|
|
|
|
// LPR ------------------------
|
|
|
|
//
|
|
|
|
if (opts & LPR_V)
|
|
|
|
bb_error_msg("connected to server");
|
2008-02-22 22:24:48 +00:00
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
job = getpid() % 1000;
|
2008-02-27 14:35:21 +00:00
|
|
|
hostname = safe_gethostname();
|
2008-02-22 22:24:48 +00:00
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
// no files given on command line? -> use stdin
|
|
|
|
if (!*argv)
|
|
|
|
*--argv = (char *)"-";
|
|
|
|
|
2008-02-27 14:35:21 +00:00
|
|
|
fdprintf(fd, "\x2" "%s\n", queue);
|
|
|
|
get_response_or_say_and_die(fd, "setting queue");
|
2008-02-24 18:44:20 +00:00
|
|
|
|
|
|
|
// process files
|
|
|
|
do {
|
2008-02-27 14:35:21 +00:00
|
|
|
int dfd;
|
2008-02-24 18:44:20 +00:00
|
|
|
struct stat st;
|
|
|
|
char *c;
|
|
|
|
char *remote_filename;
|
|
|
|
char *controlfile;
|
|
|
|
|
|
|
|
// if data file is stdin, we need to dump it first
|
|
|
|
if (LONE_DASH(*argv)) {
|
|
|
|
strcpy(tempfile, "/tmp/lprXXXXXX");
|
2008-02-27 14:35:21 +00:00
|
|
|
dfd = mkstemp(tempfile);
|
|
|
|
if (dfd < 0)
|
2008-02-24 18:44:20 +00:00
|
|
|
bb_perror_msg_and_die("mkstemp");
|
2008-02-27 14:35:21 +00:00
|
|
|
bb_copyfd_eof(STDIN_FILENO, dfd);
|
|
|
|
xlseek(dfd, 0, SEEK_SET);
|
2008-02-24 18:44:20 +00:00
|
|
|
*argv = (char*)bb_msg_standard_input;
|
|
|
|
} else {
|
2008-02-27 14:35:21 +00:00
|
|
|
dfd = xopen(*argv, O_RDONLY);
|
2008-02-22 22:24:48 +00:00
|
|
|
}
|
2008-02-24 18:44:20 +00:00
|
|
|
|
|
|
|
/* "The name ... should start with ASCII "cfA",
|
|
|
|
* followed by a three digit job number, followed
|
|
|
|
* by the host name which has constructed the file."
|
|
|
|
* We supply 'c' or 'd' as needed for control/data file. */
|
|
|
|
remote_filename = xasprintf("fA%03u%s", job, hostname);
|
|
|
|
|
|
|
|
// create control file
|
|
|
|
// TODO: all lines but 2 last are constants! How we can use this fact?
|
|
|
|
controlfile = xasprintf(
|
|
|
|
"H" "%.32s\n" "P" "%.32s\n" /* H HOST, P USER */
|
|
|
|
"C" "%.32s\n" /* C CLASS - printed on banner page (if L cmd is also given) */
|
|
|
|
"J" "%.99s\n" /* J JOBNAME */
|
|
|
|
/* "class name for banner page and job name
|
|
|
|
* for banner page commands must precede L command" */
|
|
|
|
"L" "%.32s\n" /* L USER - print banner page, with given user's name */
|
|
|
|
"M" "%.32s\n" /* M WHOM_TO_MAIL */
|
|
|
|
"l" "d%.31s\n" /* l DATA_FILE_NAME ("dfAxxx") */
|
|
|
|
, hostname, user
|
|
|
|
, printer_class /* can be "" */
|
|
|
|
, ((opts & LPR_J) ? job_title : *argv)
|
|
|
|
, (opts & LPR_h) ? user : ""
|
|
|
|
, (opts & LPR_m) ? user : ""
|
|
|
|
, remote_filename
|
|
|
|
);
|
|
|
|
// delete possible "\nX\n" patterns
|
2008-02-24 23:32:36 +00:00
|
|
|
c = controlfile;
|
|
|
|
while ((c = strchr(c, '\n')) != NULL) {
|
|
|
|
c++;
|
|
|
|
while (c[0] && c[1] == '\n')
|
|
|
|
memmove(c, c+2, strlen(c+1)); /* strlen(c+1) == strlen(c+2) + 1 */
|
|
|
|
}
|
2008-02-24 18:44:20 +00:00
|
|
|
|
|
|
|
// send control file
|
|
|
|
if (opts & LPR_V)
|
|
|
|
bb_error_msg("sending control file");
|
2008-02-22 22:24:48 +00:00
|
|
|
/* "Once all of the contents have
|
|
|
|
* been delivered, an octet of zero bits is sent as
|
|
|
|
* an indication that the file being sent is complete.
|
|
|
|
* A second level of acknowledgement processing
|
|
|
|
* must occur at this point." */
|
2008-02-27 14:35:21 +00:00
|
|
|
fdprintf(fd, "\x2" "%u c%s\n" "%s" "%c",
|
2008-02-24 18:44:20 +00:00
|
|
|
(unsigned)strlen(controlfile),
|
|
|
|
remote_filename, controlfile, '\0');
|
2008-02-27 14:35:21 +00:00
|
|
|
get_response_or_say_and_die(fd, "sending control file");
|
2008-02-24 18:44:20 +00:00
|
|
|
|
|
|
|
// send data file, with name "dfaXXX"
|
|
|
|
if (opts & LPR_V)
|
|
|
|
bb_error_msg("sending data file");
|
|
|
|
st.st_size = 0; /* paranoia: fstat may theoretically fail */
|
2008-02-27 14:35:21 +00:00
|
|
|
fstat(dfd, &st);
|
|
|
|
fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
|
|
|
|
if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
|
2008-02-24 18:44:20 +00:00
|
|
|
// We're screwed. We sent less bytes than we advertised.
|
|
|
|
bb_error_msg_and_die("local file changed size?!");
|
|
|
|
}
|
2008-02-27 14:35:21 +00:00
|
|
|
write(fd, "", 1); // send ACK
|
|
|
|
get_response_or_say_and_die(fd, "sending data file");
|
2008-02-22 22:24:48 +00:00
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
// delete temporary file if we dumped stdin
|
|
|
|
if (*argv == (char*)bb_msg_standard_input)
|
|
|
|
unlink(tempfile);
|
2008-02-22 22:24:48 +00:00
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
// cleanup
|
|
|
|
close(fd);
|
|
|
|
free(remote_filename);
|
|
|
|
free(controlfile);
|
2008-02-22 22:24:48 +00:00
|
|
|
|
2008-02-27 14:35:21 +00:00
|
|
|
// say job accepted
|
|
|
|
if (opts & LPR_V)
|
|
|
|
bb_error_msg("job accepted");
|
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
// next, please!
|
|
|
|
job = (job + 1) % 1000;
|
|
|
|
} while (*++argv);
|
2008-02-22 22:24:48 +00:00
|
|
|
|
2008-02-24 18:44:20 +00:00
|
|
|
return EXIT_SUCCESS;
|
2008-02-22 22:24:48 +00:00
|
|
|
}
|