hexagram/bin/capture.c

85 lines
1.8 KiB
C
Raw Normal View History

2019-02-12 20:55:40 -06:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>
#include <fcntl.h>
#include <hexagram/can.h>
#include <hexagram/capture.h>
2019-05-07 20:39:39 -05:00
#include "capture.h"
char *hexagram_arglist_capture(void) {
return "canif [file.can]";
}
static int usage(int argc, char **argv, const char *message, ...) {
2019-02-12 20:55:40 -06:00
if (message) {
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
va_end(args);
}
2019-05-07 20:39:39 -05:00
fprintf(stderr, "usage: hexagram %s %s\n", argv[0], hexagram_arglist_capture());
2019-02-12 20:55:40 -06:00
exit(1);
}
2019-05-07 20:39:39 -05:00
int hexagram_main_capture(int argc, char **argv) {
2019-02-12 20:55:40 -06:00
hexagram_can_if *can_if;
hexagram_capture *capture;
if (argc == 2) {
if ((capture = hexagram_capture_open_fd(fileno(stdin), O_WRONLY)) == NULL) {
perror("hexagram_capture_open_fd()");
goto error_capture_open;
}
} else if (argc == 3) {
2019-02-16 01:39:43 -06:00
if ((capture = hexagram_capture_open_file(argv[2], O_CREAT | O_TRUNC | O_WRONLY)) == NULL) {
2019-02-12 20:55:40 -06:00
perror("hexagram_capture_open_file()");
goto error_capture_open;
}
} else {
usage(argc, argv, NULL);
exit(1);
}
if ((can_if = hexagram_can_if_open(argv[1])) == NULL) {
perror("hexagram_can_if_open()");
goto error_can_if_open;
}
if (hexagram_capture_save(capture, can_if) < 0) {
goto error_capture_save;
2019-02-12 20:55:40 -06:00
}
hexagram_can_if_close(can_if);
if (argc == 3) {
hexagram_capture_close(capture);
} else {
hexagram_capture_destroy(capture);
}
return 0;
error_capture_save:
2019-02-12 20:55:40 -06:00
hexagram_can_if_close(can_if);
error_can_if_open:
if (argc == 3) {
hexagram_capture_close(capture);
} else {
hexagram_capture_destroy(capture);
}
error_capture_open:
return 1;
}