X-Git-Url: http://rtime.felk.cvut.cz/gitweb/sojka/can-utils.git/blobdiff_plain/3d26e6f710c3427840b4cc4e2852f0376a0a44a2..HEAD:/log2asc.c diff --git a/log2asc.c b/log2asc.c index 1230534..446c0b6 100644 --- a/log2asc.c +++ b/log2asc.c @@ -1,7 +1,3 @@ -/* - * $Id$ - */ - /* * log2asc.c - convert compact CAN frame logfile to ASC logfile * @@ -12,8 +8,7 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions, the following disclaimer and - * the referenced file 'COPYING'. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. @@ -23,8 +18,8 @@ * * Alternatively, provided that this notice is retained in full, this * software may be distributed under the terms of the GNU General - * Public License ("GPL") version 2 as distributed in the 'COPYING' - * file from the main directory of the linux kernel source. + * Public License ("GPL") version 2, in which case the provisions of the + * GPL apply INSTEAD OF those given above. * * The provided data structures and external interfaces from this code * are not restricted to be used by modules with a GPL compatible license. @@ -42,7 +37,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * - * Send feedback to + * Send feedback to * */ @@ -57,133 +52,152 @@ #include "lib.h" +#define BUFSZ 400 /* for one line in the logfile */ + extern int optind, opterr, optopt; void print_usage(char *prg) { - fprintf(stderr, "Usage: %s [can-interfaces]\n", prg); - fprintf(stderr, "Options: -I (default stdin)\n"); - fprintf(stderr, " -O (default stdout)\n"); - fprintf(stderr, " -4 (reduce decimal place to 4 digits)\n"); - fprintf(stderr, " -n (set newline to cr/lf - default lf)\n"); + fprintf(stderr, "Usage: %s [can-interfaces]\n", prg); + fprintf(stderr, "Options: -I (default stdin)\n"); + fprintf(stderr, " -O (default stdout)\n"); + fprintf(stderr, " -4 (reduce decimal place to 4 digits)\n"); + fprintf(stderr, " -n (set newline to cr/lf - default lf)\n"); } int main(int argc, char **argv) { - char buf[100], device[100], ascframe[100], id[10]; - - struct can_frame cf; - static struct timeval tv, start_tv; - FILE *infile = stdin; - FILE *outfile = stdout; - static int maxdev, devno, i, crlf, d4, opt; - - while ((opt = getopt(argc, argv, "I:O:4n")) != -1) { - switch (opt) { - case 'I': - infile = fopen(optarg, "r"); - if (!infile) { - perror("infile"); - return 1; - } - break; + static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ], id[10]; + + struct canfd_frame cf; + static struct timeval tv, start_tv; + FILE *infile = stdin; + FILE *outfile = stdout; + static int maxdev, devno, i, crlf, d4, opt; + + while ((opt = getopt(argc, argv, "I:O:4n?")) != -1) { + switch (opt) { + case 'I': + infile = fopen(optarg, "r"); + if (!infile) { + perror("infile"); + return 1; + } + break; + + case 'O': + outfile = fopen(optarg, "w"); + if (!outfile) { + perror("outfile"); + return 1; + } + break; + + case 'n': + crlf = 1; + break; + + case '4': + d4 = 1; + break; + + case '?': + print_usage(basename(argv[0])); + return 0; + break; + + default: + fprintf(stderr, "Unknown option %c\n", opt); + print_usage(basename(argv[0])); + return 1; + break; + } + } + + maxdev = argc - optind; /* find real number of CAN devices */ - case 'O': - outfile = fopen(optarg, "w"); - if (!outfile) { - perror("outfile"); + if (!maxdev) { + fprintf(stderr, "no CAN interfaces defined!\n"); + print_usage(basename(argv[0])); return 1; - } - break; - - case 'n': - crlf = 1; - break; - - case '4': - d4 = 1; - break; - - default: - fprintf(stderr, "Unknown option %c\n", opt); - print_usage(basename(argv[0])); - return 1; - break; } - } + + //printf("Found %d CAN devices!\n", maxdev); - maxdev = argc - optind; /* find real number of CAN devices */ + while (fgets(buf, BUFSZ-1, infile)) { - if (!maxdev) { - fprintf(stderr, "no CAN interfaces defined!\n"); - print_usage(basename(argv[0])); - return 1; - } - - //printf("Found %d CAN devices!\n", maxdev); - - while (fgets(buf, 99, infile)) { - if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec, - device, ascframe) != 4) - return 1; - - if (!start_tv.tv_sec) { /* print banner */ - start_tv = tv; - fprintf(outfile, "date %s", ctime(&start_tv.tv_sec)); - fprintf(outfile, "base hex timestamps absolute%s", - (crlf)?"\r\n":"\n"); - fprintf(outfile, "no internal events logged%s", - (crlf)?"\r\n":"\n"); - } + if (strlen(buf) >= BUFSZ-2) { + fprintf(stderr, "line too long for input buffer\n"); + return 1; + } - for (i=0, devno=0; i