Factor some things out elsewhere
This commit is contained in:
parent
e563a6b5da
commit
043c226341
1 changed files with 67 additions and 43 deletions
110
bin/can2dump.c
110
bin/can2dump.c
|
@ -3,6 +3,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <sysexits.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
@ -19,6 +20,10 @@ enum timestamp_type {
|
||||||
TIMESTAMP_DATE
|
TIMESTAMP_DATE
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct optvalues {
|
||||||
|
enum timestamp_type tstype;
|
||||||
|
};
|
||||||
|
|
||||||
char *hexagram_arglist_can2dump(void) {
|
char *hexagram_arglist_can2dump(void) {
|
||||||
return "[--timestamp=|adzA|] ifname [file.can] [candump.txt]";
|
return "[--timestamp=|adzA|] ifname [file.can] [candump.txt]";
|
||||||
}
|
}
|
||||||
|
@ -35,10 +40,59 @@ static int usage(int argc, char **argv, const char *message, ...) {
|
||||||
|
|
||||||
fprintf(stderr, "usage: hexagram %s %s\n", argv[0], hexagram_arglist_can2dump());
|
fprintf(stderr, "usage: hexagram %s %s\n", argv[0], hexagram_arglist_can2dump());
|
||||||
|
|
||||||
return 1;
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int parseopts(struct optvalues *v,
|
||||||
|
struct option opts[],
|
||||||
|
int *argc,
|
||||||
|
char **argv[]) {
|
||||||
|
int ch, index;
|
||||||
|
|
||||||
|
while ((ch = getopt_long(*argc, *argv, "t:", opts, &index)) >= 0) {
|
||||||
|
switch (ch) {
|
||||||
|
case 't':
|
||||||
|
switch (optarg[0]) {
|
||||||
|
case 'a': v->tstype = TIMESTAMP_ABSOLUTE; break;
|
||||||
|
case 'd': v->tstype = TIMESTAMP_DELTA; break;
|
||||||
|
case 'z': v->tstype = TIMESTAMP_ZERO; break;
|
||||||
|
case 'A': v->tstype = TIMESTAMP_DATE; break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return usage(*argc, *argv, "Invalid timestamp type");
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if (ch == '?') {
|
||||||
|
return usage(*argc, *argv, NULL);
|
||||||
|
} else {
|
||||||
|
return usage(*argc, *argv, "Unknown flag '%c'", ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*argc -= optind;
|
||||||
|
*argv += optind;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hexagram_main_can2dump(int argc, char **argv) {
|
int hexagram_main_can2dump(int argc, char **argv) {
|
||||||
|
static struct option opts[] = {
|
||||||
|
{ "timestamp", required_argument, NULL, 't' },
|
||||||
|
{ NULL, 0, NULL, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct optvalues values = {
|
||||||
|
.tstype = TIMESTAMP_NONE
|
||||||
|
};
|
||||||
|
|
||||||
|
int status;
|
||||||
|
|
||||||
hexagram_capture *capture;
|
hexagram_capture *capture;
|
||||||
const char *ifname;
|
const char *ifname;
|
||||||
FILE *fh;
|
FILE *fh;
|
||||||
|
@ -46,41 +100,13 @@ int hexagram_main_can2dump(int argc, char **argv) {
|
||||||
struct timeval timestamp;
|
struct timeval timestamp;
|
||||||
struct can_frame frame;
|
struct can_frame frame;
|
||||||
|
|
||||||
static struct option opts[] = {
|
int argn = argc;
|
||||||
{ "timestamp", required_argument, NULL, 't' },
|
char **args = argv;
|
||||||
{ NULL, 0, NULL, 0 }
|
|
||||||
};
|
|
||||||
|
|
||||||
enum timestamp_type tstype = TIMESTAMP_NONE;
|
if ((status = parseopts(&values, opts, &argn, &args)) != 0) {
|
||||||
|
return status;
|
||||||
int argn;
|
|
||||||
char **args;
|
|
||||||
|
|
||||||
int ch;
|
|
||||||
|
|
||||||
while ((ch = getopt_long(argc, argv, "t:", opts, NULL)) >= 0) {
|
|
||||||
switch (ch) {
|
|
||||||
case 't':
|
|
||||||
switch (optarg[0]) {
|
|
||||||
case 'a': tstype = TIMESTAMP_ABSOLUTE; break;
|
|
||||||
case 'd': tstype = TIMESTAMP_DELTA; break;
|
|
||||||
case 'z': tstype = TIMESTAMP_ZERO; break;
|
|
||||||
case 'A': tstype = TIMESTAMP_DATE; break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return usage(argc, argv, "Invalid timestamp type");
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return usage(argc, argv, "Unknown flag '%c'", ch);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
argn = argc - optind;
|
|
||||||
args = argv + optind;
|
|
||||||
|
|
||||||
if (argn == 0) {
|
if (argn == 0) {
|
||||||
return usage(argc, argv, "No CAN interface name provided");
|
return usage(argc, argv, "No CAN interface name provided");
|
||||||
}
|
}
|
||||||
|
@ -95,24 +121,20 @@ int hexagram_main_can2dump(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fh = stdout;
|
fh = stdout;
|
||||||
} else if (argn == 2) {
|
} else if (argn > 1) {
|
||||||
if ((capture = hexagram_capture_open_file(args[1], O_RDONLY)) == NULL) {
|
|
||||||
perror("hexagram_capture_open_file()");
|
|
||||||
|
|
||||||
goto error_capture_open;
|
|
||||||
}
|
|
||||||
|
|
||||||
fh = stdout;
|
|
||||||
} else if (argn == 3) {
|
|
||||||
if ((capture = hexagram_capture_open_file(args[1], O_RDONLY)) == NULL) {
|
if ((capture = hexagram_capture_open_file(args[1], O_RDONLY)) == NULL) {
|
||||||
perror("hexagram_capture_open_file()");
|
perror("hexagram_capture_open_file()");
|
||||||
|
|
||||||
goto error_capture_open;
|
goto error_capture_open;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argn == 3) {
|
||||||
if ((fh = fopen(args[2], "w")) == NULL) {
|
if ((fh = fopen(args[2], "w")) == NULL) {
|
||||||
goto error_fopen;
|
goto error_fopen;
|
||||||
}
|
}
|
||||||
|
} else if (argn == 2) {
|
||||||
|
fh = stdout;
|
||||||
} else {
|
} else {
|
||||||
return usage(argc, argv, NULL);
|
return usage(argc, argv, NULL);
|
||||||
}
|
}
|
||||||
|
@ -120,8 +142,10 @@ int hexagram_main_can2dump(int argc, char **argv) {
|
||||||
while (hexagram_capture_read(capture, ×tamp, &frame) >= 0) {
|
while (hexagram_capture_read(capture, ×tamp, &frame) >= 0) {
|
||||||
uint8_t i;
|
uint8_t i;
|
||||||
|
|
||||||
switch (tstype) {
|
switch (values.tstype) {
|
||||||
case TIMESTAMP_NONE:
|
case TIMESTAMP_NONE:
|
||||||
|
fprintf(fh, " ");
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TIMESTAMP_ABSOLUTE:
|
case TIMESTAMP_ABSOLUTE:
|
||||||
|
|
Loading…
Add table
Reference in a new issue