view sw/sniff-dec/main.c @ 45:b0524d1dc6ef

simtrace3-sniff-dec: implement command decoding
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 09:32:48 +0000
parents 118a12e9483b
children
line wrap: on
line source

/*
 * simtrace3-sniff-dec main module: reading the log file.
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

char *log_filename;
char linebuf[128];
int lineno;
unsigned fpga_word;

static void
process_line()
{
	char *cp;

	cp = index(linebuf, '\n');
	if (!cp) {
		fprintf(stderr, "%s line %d: too long or missing newline\n",
			log_filename, lineno);
		exit(1);
	}
	if (linebuf[0] != '[')
		return;
	cp = linebuf + 1;
	while (isdigit(*cp) || *cp == ':' || *cp == '.')
		cp++;
	if (*cp++ != ']') {
invalid:	fprintf(stderr, "%s line %d: failed to parse\n",
			log_filename, lineno);
		exit(1);
	}
	if (!isspace(*cp))
		goto invalid;
	*cp++ = '\0';
	if (strlen(linebuf) > 17)
		goto invalid;
	while (isspace(*cp))
		cp++;
	if (!isxdigit(cp[0]) || !isxdigit(cp[1]) || !isxdigit(cp[2]) ||
	    !isxdigit(cp[3]) || !isspace(cp[4]))
		goto invalid;
	fpga_word = strtoul(cp, 0, 16);
	process_fpga_word();
}

main(argc, argv)
	char **argv;
{
	FILE *inf;

	if (argc != 2) {
		fprintf(stderr, "usage: %s logfile\n", argv[0]);
		exit(1);
	}
	log_filename = argv[1];
	inf = fopen(log_filename, "r");
	if (!inf) {
		perror(log_filename);
		exit(1);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
		process_line();
	exit(0);
}