2008-02-23 03:54:48 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2008-02-25 00:14:20 +05:30
|
|
|
* bare bones version of lpr & lpq: BSD printing utilities
|
2008-02-23 03:54:48 +05:30
|
|
|
*
|
2008-02-25 00:14:20 +05:30
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
|
|
|
* Original idea and code:
|
|
|
|
* Walter Harms <WHarms@bfs.de>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-02-25 00:14:20 +05:30
|
|
|
*
|
2008-02-25 03:38:57 +05:30
|
|
|
* See RFC 1179 for protocol description.
|
2008-02-23 03:54:48 +05:30
|
|
|
*/
|
2015-10-19 04:15:46 +05:30
|
|
|
//config:config LPR
|
|
|
|
//config: bool "lpr"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: lpr sends files (or standard input) to a print spooling daemon.
|
|
|
|
//config:
|
|
|
|
//config:config LPQ
|
|
|
|
//config: bool "lpq"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: lpq is a print spool queue examination and manipulation program.
|
|
|
|
|
2017-01-29 19:27:33 +05:30
|
|
|
// APPLET_ODDNAME:name main location suid_type help
|
2015-10-19 04:15:46 +05:30
|
|
|
//applet:IF_LPQ(APPLET_ODDNAME(lpq, lpqr, BB_DIR_USR_BIN, BB_SUID_DROP, lpq))
|
|
|
|
//applet:IF_LPR(APPLET_ODDNAME(lpr, lpqr, BB_DIR_USR_BIN, BB_SUID_DROP, lpr))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_LPR) += lpr.o
|
|
|
|
//kbuild:lib-$(CONFIG_LPQ) += lpr.o
|
2011-04-11 06:59:49 +05:30
|
|
|
|
|
|
|
//usage:#define lpr_trivial_usage
|
|
|
|
//usage: "-P queue[@host[:port]] -U USERNAME -J TITLE -Vmh [FILE]..."
|
|
|
|
/* -C CLASS exists too, not shown.
|
|
|
|
* CLASS is supposed to be printed on banner page, if one is requested */
|
|
|
|
//usage:#define lpr_full_usage "\n\n"
|
2011-06-05 07:28:28 +05:30
|
|
|
//usage: " -P lp service to connect to (else uses $PRINTER)"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -m Send mail on completion"
|
|
|
|
//usage: "\n -h Print banner page too"
|
|
|
|
//usage: "\n -V Verbose"
|
|
|
|
//usage:
|
|
|
|
//usage:#define lpq_trivial_usage
|
|
|
|
//usage: "[-P queue[@host[:port]]] [-U USERNAME] [-d JOBID]... [-fs]"
|
|
|
|
//usage:#define lpq_full_usage "\n\n"
|
2011-06-05 07:28:28 +05:30
|
|
|
//usage: " -P lp service to connect to (else uses $PRINTER)"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: "\n -d Delete jobs"
|
|
|
|
//usage: "\n -f Force any waiting job to be printed"
|
|
|
|
//usage: "\n -s Short display"
|
|
|
|
|
2008-02-23 03:54:48 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
/*
|
|
|
|
* LPD returns binary 0 on success.
|
|
|
|
* Otherwise it returns error message.
|
2008-02-23 03:54:48 +05:30
|
|
|
*/
|
2008-02-27 20:05:21 +05:30
|
|
|
static void get_response_or_say_and_die(int fd, const char *errmsg)
|
2008-02-23 03:54:48 +05:30
|
|
|
{
|
2008-02-26 02:00:24 +05:30
|
|
|
ssize_t sz;
|
|
|
|
char buf[128];
|
2008-02-25 00:14:20 +05:30
|
|
|
|
2008-02-26 02:00:24 +05:30
|
|
|
buf[0] = ' ';
|
2008-02-27 20:05:21 +05:30
|
|
|
sz = safe_read(fd, buf, 1);
|
2008-02-26 02:00:24 +05:30
|
|
|
if ('\0' != buf[0]) {
|
2008-02-25 00:14:20 +05:30
|
|
|
// request has failed
|
2008-02-26 02:00:24 +05:30
|
|
|
// try to make sure last char is '\n', but do not add
|
|
|
|
// superfluous one
|
2008-02-27 20:05:21 +05:30
|
|
|
sz = full_read(fd, buf + 1, 126);
|
2008-02-26 02:00:24 +05:30
|
|
|
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-23 03:54:48 +05:30
|
|
|
xfunc_die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int lpqr_main(int argc UNUSED_PARAM, char *argv[])
|
2008-02-23 03:54:48 +05:30
|
|
|
{
|
|
|
|
enum {
|
2008-02-25 00:14:20 +05:30
|
|
|
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 07:35:58 +05:30
|
|
|
LPR_h = 1 << 3, // -h: want banner printed
|
2008-02-25 00:14:20 +05:30
|
|
|
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-23 03:54:48 +05:30
|
|
|
};
|
2008-02-25 00:14:20 +05:30
|
|
|
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!
|
2008-12-03 04:26:59 +05:30
|
|
|
const char *user = xuid2uname(getuid());
|
2008-02-25 00:14:20 +05:30
|
|
|
unsigned job;
|
|
|
|
unsigned opts;
|
2008-02-27 20:05:21 +05:30
|
|
|
int fd;
|
2008-02-25 00:14:20 +05:30
|
|
|
|
2012-06-10 17:17:17 +05:30
|
|
|
queue = getenv("PRINTER");
|
|
|
|
if (!queue)
|
|
|
|
queue = "lp";
|
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
// 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-23 03:54:48 +05:30
|
|
|
argv += optind;
|
2008-02-25 00:14:20 +05:30
|
|
|
|
2012-06-10 17:17:17 +05:30
|
|
|
{
|
2008-02-25 00:14:20 +05:30
|
|
|
// 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-23 03:54:48 +05:30
|
|
|
}
|
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
// 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 20:05:21 +05:30
|
|
|
fdprintf(fd, "\x5" "%s %s", queue, user);
|
2008-02-25 00:14:20 +05:30
|
|
|
while (*argv) {
|
2008-02-27 20:05:21 +05:30
|
|
|
fdprintf(fd, " %s", *argv++);
|
2008-02-25 00:14:20 +05:30
|
|
|
}
|
|
|
|
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 20:05:21 +05:30
|
|
|
fdprintf(fd, "%c" "%s\n", cmd, queue);
|
|
|
|
bb_copyfd_eof(fd, STDOUT_FILENO);
|
2008-02-25 00:14:20 +05:30
|
|
|
}
|
2008-02-23 03:54:48 +05:30
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2008-02-23 03:54:48 +05:30
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
//
|
|
|
|
// LPR ------------------------
|
|
|
|
//
|
|
|
|
if (opts & LPR_V)
|
|
|
|
bb_error_msg("connected to server");
|
2008-02-23 03:54:48 +05:30
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
job = getpid() % 1000;
|
2008-02-27 20:05:21 +05:30
|
|
|
hostname = safe_gethostname();
|
2008-02-23 03:54:48 +05:30
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
// no files given on command line? -> use stdin
|
|
|
|
if (!*argv)
|
|
|
|
*--argv = (char *)"-";
|
|
|
|
|
2008-02-27 20:05:21 +05:30
|
|
|
fdprintf(fd, "\x2" "%s\n", queue);
|
|
|
|
get_response_or_say_and_die(fd, "setting queue");
|
2008-02-25 00:14:20 +05:30
|
|
|
|
|
|
|
// process files
|
|
|
|
do {
|
2008-07-23 00:45:30 +05:30
|
|
|
unsigned cflen;
|
2008-02-27 20:05:21 +05:30
|
|
|
int dfd;
|
2008-02-25 00:14:20 +05:30
|
|
|
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");
|
2010-10-22 16:57:16 +05:30
|
|
|
dfd = xmkstemp(tempfile);
|
2008-02-27 20:05:21 +05:30
|
|
|
bb_copyfd_eof(STDIN_FILENO, dfd);
|
|
|
|
xlseek(dfd, 0, SEEK_SET);
|
2008-02-25 00:14:20 +05:30
|
|
|
*argv = (char*)bb_msg_standard_input;
|
|
|
|
} else {
|
2008-02-27 20:05:21 +05:30
|
|
|
dfd = xopen(*argv, O_RDONLY);
|
2008-02-23 03:54:48 +05:30
|
|
|
}
|
2008-02-25 00:14:20 +05:30
|
|
|
|
2012-06-10 17:17:17 +05:30
|
|
|
st.st_size = 0; /* paranoia: fstat may theoretically fail */
|
|
|
|
fstat(dfd, &st);
|
|
|
|
|
|
|
|
/* Apparently, some servers are buggy and won't accept 0-sized jobs.
|
|
|
|
* Standard lpr works around it by refusing to send such jobs:
|
|
|
|
*/
|
|
|
|
if (st.st_size == 0) {
|
|
|
|
bb_error_msg("nothing to print");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
/* "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
|
|
|
|
);
|
2012-06-10 17:17:17 +05:30
|
|
|
// delete possible "\nX\n" (that is, one-char) patterns
|
2008-02-25 05:02:36 +05:30
|
|
|
c = controlfile;
|
|
|
|
while ((c = strchr(c, '\n')) != NULL) {
|
2008-07-23 00:45:30 +05:30
|
|
|
if (c[1] && c[2] == '\n') {
|
2012-04-27 18:33:34 +05:30
|
|
|
overlapping_strcpy(c, c+2);
|
2008-07-23 00:45:30 +05:30
|
|
|
} else {
|
|
|
|
c++;
|
|
|
|
}
|
2008-02-25 05:02:36 +05:30
|
|
|
}
|
2008-02-25 00:14:20 +05:30
|
|
|
|
|
|
|
// send control file
|
|
|
|
if (opts & LPR_V)
|
|
|
|
bb_error_msg("sending control file");
|
2008-07-23 00:45:30 +05:30
|
|
|
/* "Acknowledgement processing must occur as usual
|
|
|
|
* after the command is sent." */
|
2012-04-27 18:33:34 +05:30
|
|
|
cflen = (unsigned)strlen(controlfile);
|
2008-07-23 00:45:30 +05:30
|
|
|
fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
|
|
|
|
get_response_or_say_and_die(fd, "sending control file");
|
2008-02-23 03:54:48 +05:30
|
|
|
/* "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-07-23 00:45:30 +05:30
|
|
|
full_write(fd, controlfile, cflen + 1); /* writes NUL byte too */
|
2008-02-27 20:05:21 +05:30
|
|
|
get_response_or_say_and_die(fd, "sending control file");
|
2008-02-25 00:14:20 +05:30
|
|
|
|
|
|
|
// send data file, with name "dfaXXX"
|
|
|
|
if (opts & LPR_V)
|
|
|
|
bb_error_msg("sending data file");
|
2008-02-27 20:05:21 +05:30
|
|
|
fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
|
2008-07-23 00:45:30 +05:30
|
|
|
get_response_or_say_and_die(fd, "sending data file");
|
2008-02-27 20:05:21 +05:30
|
|
|
if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
|
2008-02-25 00:14:20 +05:30
|
|
|
// We're screwed. We sent less bytes than we advertised.
|
|
|
|
bb_error_msg_and_die("local file changed size?!");
|
|
|
|
}
|
2008-02-27 20:05:21 +05:30
|
|
|
write(fd, "", 1); // send ACK
|
|
|
|
get_response_or_say_and_die(fd, "sending data file");
|
2008-02-23 03:54:48 +05:30
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
// delete temporary file if we dumped stdin
|
|
|
|
if (*argv == (char*)bb_msg_standard_input)
|
|
|
|
unlink(tempfile);
|
2008-02-23 03:54:48 +05:30
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
// cleanup
|
|
|
|
close(fd);
|
|
|
|
free(remote_filename);
|
|
|
|
free(controlfile);
|
2008-02-23 03:54:48 +05:30
|
|
|
|
2008-02-27 20:05:21 +05:30
|
|
|
// say job accepted
|
|
|
|
if (opts & LPR_V)
|
|
|
|
bb_error_msg("job accepted");
|
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
// next, please!
|
|
|
|
job = (job + 1) % 1000;
|
|
|
|
} while (*++argv);
|
2008-02-23 03:54:48 +05:30
|
|
|
|
2008-02-25 00:14:20 +05:30
|
|
|
return EXIT_SUCCESS;
|
2008-02-23 03:54:48 +05:30
|
|
|
}
|