tabby/bin/capture.c

64 lines
1.4 KiB
C
Raw Normal View History

2016-06-10 03:20:20 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <tabby/link.h>
#include <tabby/printer.h>
static void usage(int argc, char **argv, char *message, ...) {
if (message) {
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
fprintf(stderr, "\n");
va_end(args);
}
2016-06-10 17:34:17 -05:00
fprintf(stderr, "usage: %s capture device\n", argv[0]);
2016-06-10 03:20:20 +00:00
exit(1);
}
2016-06-10 17:34:17 -05:00
int tabby_command_capture(int argc, char **argv) {
2016-06-10 03:20:20 +00:00
tabby_printer *printer;
tabby_printer_packet header;
uint8_t body[TABBY_PRINTER_PACKET_MAX_SIZE];
2016-06-10 03:25:22 +00:00
if (argc != 3) {
2016-06-10 03:20:20 +00:00
usage(argc, argv, "No device specified");
}
2017-01-10 22:19:14 -06:00
if ((printer = tabby_printer_open(argv[2])) == NULL) {
2016-06-10 03:20:20 +00:00
fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "tabby_printer_open()",
2016-06-10 03:25:22 +00:00
argv[2], strerror(errno));
2016-06-10 03:20:20 +00:00
goto error_printer_open;
}
while (1) {
if (tabby_printer_packet_recv(printer, &header, &body) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "tabby_printer_packet_recv()", strerror(errno));
continue;
}
if (header.type == 0x04) {
write(1, &body, header.size);
}
}
tabby_printer_close(printer);
return 0;
error_printer_open:
return 1;
}