annotate sw/sniff-dec/main.c @ 56:966a54303d68

simsniff-dec: factor out high-level decoding
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 04 Oct 2023 01:18:50 +0000
parents 118a12e9483b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * simtrace3-sniff-dec main module: reading the log file.
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <ctype.h>
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 char *log_filename;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 char linebuf[128];
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 int lineno;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 unsigned fpga_word;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 static void
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 process_line()
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char *cp;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 cp = index(linebuf, '\n');
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (!cp) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 fprintf(stderr, "%s line %d: too long or missing newline\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 log_filename, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 exit(1);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (linebuf[0] != '[')
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 return;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 cp = linebuf + 1;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 while (isdigit(*cp) || *cp == ':' || *cp == '.')
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 cp++;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (*cp++ != ']') {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 invalid: fprintf(stderr, "%s line %d: failed to parse\n",
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 log_filename, lineno);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 exit(1);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (!isspace(*cp))
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 goto invalid;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 *cp++ = '\0';
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (strlen(linebuf) > 17)
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 goto invalid;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 while (isspace(*cp))
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 cp++;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (!isxdigit(cp[0]) || !isxdigit(cp[1]) || !isxdigit(cp[2]) ||
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 !isxdigit(cp[3]) || !isspace(cp[4]))
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 goto invalid;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 fpga_word = strtoul(cp, 0, 16);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 process_fpga_word();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 main(argc, argv)
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 char **argv;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 FILE *inf;
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (argc != 2) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 fprintf(stderr, "usage: %s logfile\n", argv[0]);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 exit(1);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 log_filename = argv[1];
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 inf = fopen(log_filename, "r");
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (!inf) {
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 perror(log_filename);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 exit(1);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 process_line();
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 exit(0);
118a12e9483b simtrace3-sniff-dec started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }