aboutsummaryrefslogtreecommitdiffstats
path: root/tools/abcprintd.c
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2009-01-04 16:42:42 -0800
committerH. Peter Anvin <hpa@zytor.com>2009-01-04 16:42:42 -0800
commitbc0fb6e12bd1999f38d39199164edb47ea56c6b2 (patch)
tree9d3363da80af4cdeb9f9769b21b811c5cfd769ff /tools/abcprintd.c
parent81eed1d4265ae193cf334edfe5aeb0b68418ffcf (diff)
downloadabc80-bc0fb6e12bd1999f38d39199164edb47ea56c6b2.tar.gz
abc80-bc0fb6e12bd1999f38d39199164edb47ea56c6b2.tar.xz
abc80-bc0fb6e12bd1999f38d39199164edb47ea56c6b2.zip
abcprintd: initial implementation of a remote access protocol
Completely untested, but "at least it compiles" implementation of a remote file access protocol in abcprintd.
Diffstat (limited to 'tools/abcprintd.c')
-rw-r--r--tools/abcprintd.c67
1 files changed, 50 insertions, 17 deletions
diff --git a/tools/abcprintd.c b/tools/abcprintd.c
index b813bb4..5925e55 100644
--- a/tools/abcprintd.c
+++ b/tools/abcprintd.c
@@ -1,7 +1,6 @@
-#ident "$Id$"
/* ----------------------------------------------------------------------- *
*
- * Copyright 2004-2006 H. Peter Anvin - All Rights Reserved
+ * Copyright 2004-2009 H. Peter Anvin - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -24,6 +23,7 @@
#include <termios.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
#include <sys/wait.h>
#include <fcntl.h>
@@ -93,8 +93,8 @@ static int open_port(const char *path)
tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN);
- tio.c_cc[VMIN] = 64;
- tio.c_cc[VTIME] = 1;
+ tio.c_cc[VMIN] = 1;
+ tio.c_cc[VTIME] = 0;
cfsetispeed(&tio, B115200);
cfsetospeed(&tio, B115200);
@@ -167,6 +167,9 @@ static void output(int c, FILE *tf, enum print_state *psp)
}
}
+extern bool file_op(unsigned char, int);
+extern const char *file_op_dir;
+
#define BUF_SIZE 4096
int main(int argc, char *argv[])
@@ -176,8 +179,10 @@ int main(int argc, char *argv[])
enum {
st_normal, /* Normal operation */
st_ff, /* 0xFF received */
+ st_file, /* File operation in progress */
} state;
- char ibuf[BUF_SIZE], c;
+ char ibuf[BUF_SIZE];
+ unsigned char c;
int i, b;
enum print_state ps;
static const char *lpr_default_argv[] = {
@@ -185,27 +190,46 @@ int main(int argc, char *argv[])
"--non-printable-format=space",
NULL
};
+ int o;
+ extern char *optarg;
+ extern int optind, opterr, optopt;
+ bool badopt = false;
+
+ file_op_dir = NULL;
+
+ while ((o = getopt(argc, argv, "d:")) != EOF) {
+ switch (o) {
+ case 'd':
+ file_op_dir = optarg;
+ break;
+ default:
+ badopt = true;
+ break;
+ }
+ }
- if ( argc < 2 ) {
- fprintf(stderr, "Usage: %s port [lpr_program [options...]]\n", argv[0]);
+ if ( badopt || optind >= argc ) {
+ fprintf(stderr,
+ "Usage: %s [options] port lpr_program [lpr_args...]]\n",
+ argv[0]);
exit(1);
}
- if ( (fd = open_port(argv[1])) < 0 ) {
- perror(argv[1]);
+ if ( (fd = open_port(argv[optind])) < 0 ) {
+ perror(argv[optind]);
exit(1);
}
- if ( argc == 2 ) {
+ if ( argc == optind ) {
lpr_argc = sizeof(lpr_default_argv)/sizeof(const char *) - 1;
lpr_argv = lpr_default_argv;
} else {
- lpr_argc = argc-2;
- if ( !(lpr_argv = malloc(sizeof(char *) * (lpr_argc+1))) ) {
+ lpr_argc = argc-optind-1;
+ if ( !(lpr_argv = malloc(sizeof(char *)*(lpr_argc+1))) ) {
perror("malloc");
exit(1);
}
- memcpy(lpr_argv, argv+2, sizeof(char *) * (argc-2));
+ memcpy(lpr_argv, argv+optind+1, sizeof(char *)*(lpr_argc+1));
}
daemon(0,0);
@@ -225,18 +249,27 @@ int main(int argc, char *argv[])
switch ( state ) {
case st_normal:
- if ( c == '\xff' )
+ if ( c == 0xff )
state = st_ff;
else
output(c, tf, &ps);
break;
case st_ff:
- if ( c == 0 )
+ if ( c == 0 ) {
print_setup(&tf, &ps); /* BREAK received, end of job */
- else
+ state = st_normal;
+ } else if ( c >= 0xa0 && c <= 0xbf ) {
+ /* Opcode range reserved for file ops */
+ state = file_op(c, fd) ? st_file : st_normal;
+ } else {
output(c, tf, &ps);
- state = st_normal;
+ state = st_normal;
+ }
+ break;
+
+ case st_file:
+ state = file_op(c, fd) ? st_file : st_normal;
break;
}
}