Hi,
I hacked this little thing together late last night. Its proof of concept of
slave printing on any tty. Ideally, I'd like to go "localprint
--printcommand /usr/bin/printme --command telnet server". At the moment you
need to change #defines, and recompile.
I don't have a place to put this (yet), but before I hack some more on it, I'd
like to know if it would help people. It would have helped me if it existed
a year ago.
Apologies for posting an attachment, but its plain text, and quite small.
--
Berend De Schouwer
/*
* localprint.c 0.1.0
*
* Copyright (c) 2003 Berend De Schouwer <[EMAIL PROTECTED]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
extern int errno;
/*
* There are termcap defined time-constraints between ^[ (Esc) and following
* characters. We assume the time constraints are 0.1 seconds. This may be
* incorrect. There must be a time-constraint, otherwise plain Esc won't work.
* It is expected that the network client sends a group of characters
* (like ^[5i) together to guarantee that time-constraint over a network.
* This is true for telnet, but false for rsh. (as implemented on Linux).
*/
const char PRINT_START[] = "\0335i";
const char PRINT_STOP[] = "\0334i";
#define PRINT_AGE 1 /* How many .1 secs do we allow as timeout. */
#define PRINT_COMMAND "/usr/bin/printme"
#define RUN_COMMAND "/bin/bash"
/*
* Too many globals. But hey, atm, its only proof-of-concept code.
*/
char buffer[1024];
int age;
int print_pipe[2];
int printer = 1;
int printing = 0;
int matched = 0;
/*
* Closes the printer pipe, and redirects it to stdout.
*/
int printer_close(int fd) {
close(fd);
return(1);
}
/*
* Starts PRINT_COMMAND, and redirects the printer pipe there.
*/
void printer_open(int p[2]) {
int pid;
if (pipe(p)) {
fprintf(stderr, "Could not create pipe: %s\n", strerror(errno));
exit(1);
}
pid = fork();
if (pid < 0) {
fprintf(stderr, "Could not fork: %s\n", strerror(errno));
exit(1);
} else if (pid > 0) {
printer = p[1];
close(p[0]);
} else if (pid == 0) {
close(0);
close(p[1]);
if (dup(p[0])) { /* stdin is printer pipe */
fprintf(stderr, "Could not dup: %s\n", strerror(errno));
exit(0);
}
execvp(PRINT_COMMAND, NULL);
exit(1); /* We shouldn't reach this. */
}
}
/*
* Transfers some bytes to filedescriptor fd.
*/
int transfer(int fd, int begin, int end) {
int i;
for (i = begin; i < end;
i = i + write(fd, &buffer[i], (end - i)));
return i;
}
void filter(int fd) {
int size;
int total;
int i;
size = read(fd, &buffer, sizeof(buffer));
if (size == 0) {
exit(0);
} else if (size < 0) {
fprintf(stderr, "Failed to read: %s\n", strerror(errno));
}
total = 0;
for (i = 0; i < size; i++) {
if (printing) {
if (buffer[i] == PRINT_STOP[matched]) {
if (matched == 0)
total = transfer(printer, total, i);
matched++;
if (matched == (sizeof(PRINT_STOP) - 1)) {
printer = printer_close(print_pipe[1]);
printing = 0;
total = i + 1;
matched = 0;
}
} else {
if (matched > 0) {
write(printer, PRINT_STOP, matched);
total = i;
matched = 0;
}
}
} else {
if (buffer[i] == PRINT_START[matched]) {
if (matched == 0)
total = transfer(1, total, i);
matched++;
if (matched == (sizeof(PRINT_START) - 1)) {
printer_open(print_pipe);
printing = 1;
total = i + 1;
matched = 0;
}
} else {
if (matched > 0) {
write(1, PRINT_START, matched);
total = i;
matched = 0;
}
}
}
}
if (!matched) /* Transfer the remaining buffer. */
total = transfer(printer, total, size);
/*
* Save matched age, for time problems.
*/
if (matched > 0)
age = PRINT_AGE;
else
age = 0;
}
void parent(int fd) {
fd_set rfds;
struct timeval tv;
int retval;
matched = 0;
printing = 0;
for(;;) {
if (age == 0) matched = 0;
age--;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 100;
retval = select(fd + 1, &rfds, NULL, NULL, &tv);
if (retval < 0) {
fprintf(stderr, "Select failed: %s\n", strerror(errno));
exit(1);
}
if (FD_ISSET(fd, &rfds)) {
filter(fd);
}
}
}
/*
* Main program.
*/
int main(int argc, char *argv[]) {
int pid;
int out[2];
/*
* Setup a pipe, so parent <-> child can communicate.
* We only clone stdout, because we are only expecting a print command
* on stdout.
*/
if (pipe(out)) {
fprintf(stderr, "Could not create pipe: %s\n", strerror(errno));
exit(1);
}
/*
* Fork, so we have a parent and a child.
*/
pid = fork();
if (pid < 0) {
fprintf(stderr, "Could not fork: %s\n", strerror(errno));
exit(1);
/*
* Check if we are the child.
*/
} else if (pid == 0) {
close(1);
if (dup(out[1]) != 1) {
fprintf(stderr, "Cannot clone stdin: %s\n",
strerror(errno));
exit(1);
}
close(out[1]);
execvp(RUN_COMMAND, argv);
exit(1); /* We should never reach this line. */
/*
* We are the parent.
*/
} else {
close(out[1]);
parent(out[0]);
}
exit(0);
}