diff sw/sniff-dec/main.c @ 41:118a12e9483b

simtrace3-sniff-dec started
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 08:46:23 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sw/sniff-dec/main.c	Thu Aug 31 08:46:23 2023 +0000
@@ -0,0 +1,69 @@
+/*
+ * 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);
+}