tabby/bin/main.c
2016-06-06 19:42:15 -05:00

66 lines
1.3 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 device\n", argv[0]);
exit(1);
}
int main(int argc, char **argv) {
int fd, status;
tabby_printer_packet header;
uint8_t body[TABBY_PRINTER_PACKET_MAX_SIZE];
if (argc != 2) {
usage(argc, argv, "No device specified");
}
if ((fd = tabby_link_open(argv[1])) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "tabby_link_open()",
argv[1], strerror(errno));
goto error_link_open;
}
while (1) {
status = tabby_printer_packet_recv(fd, &header, &body);
if (status < 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_link_close(fd);
return 0;
error_link_open:
return 1;
}