tabby/bin/capture.c
2016-06-10 17:34:17 -05:00

63 lines
1.4 KiB
C

#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);
}
fprintf(stderr, "usage: %s capture device\n", argv[0]);
exit(1);
}
int tabby_command_capture(int argc, char **argv) {
tabby_printer *printer;
tabby_printer_packet header;
uint8_t body[TABBY_PRINTER_PACKET_MAX_SIZE];
if (argc != 3) {
usage(argc, argv, "No device specified");
}
if ((printer = tabby_printer_open(argv[1])) == NULL) {
fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "tabby_printer_open()",
argv[2], strerror(errno));
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;
}