annotate utils/sws-email2db.c @ 81:8c23d2a9c013

doc/GrcardSIM2-programming: update for fcsim1-program
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 09 Apr 2021 08:35:54 +0000
parents 372ecc4aa2c4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads extracts from Sysmocom webshop emails containing
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * card provisioning data and converts these bits into our sws-card-db
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * format.
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #define MAX_FIELDS 32
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #define MAX_FIELD_KW 7
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 char *infname;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 FILE *inf;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 char linebuf[128];
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 int lineno;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 char field_names[MAX_FIELDS][MAX_FIELD_KW+1];
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 unsigned nfields;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 get_input_line()
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 char *cp;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (!fgets(linebuf, sizeof linebuf, inf))
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 return(0);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 lineno++;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 cp = index(linebuf, '\n');
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (!cp) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 fprintf(stderr, "%s line %d: too long or missing newline\n",
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 infname, lineno);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 *cp = '\0';
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 read_field_names()
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 for (;;) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (!get_input_line()) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 fprintf(stderr, "error: %s EOFs in field name list\n",
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 infname);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if (!linebuf[0])
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 break;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (nfields >= MAX_FIELDS) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 fprintf(stderr, "%s line %d: too many fields\n",
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 infname, lineno);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (strlen(linebuf) > MAX_FIELD_KW) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 fprintf(stderr, "%s line %d: field name is too long\n",
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 infname, lineno);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 strcpy(field_names[nfields], linebuf);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 nfields++;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (!nfields) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 fprintf(stderr, "error: %s header defines 0 fields\n", infname);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 process_one_card()
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 unsigned nf;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 int rc;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 for (nf = 0; nf < nfields; nf++) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 for (;;) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 rc = get_input_line();
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 if (!rc) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 if (!nf)
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 exit(0);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 fprintf(stderr,
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 "error: %s EOFs in the middle of a card data block\n",
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 infname);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (linebuf[0])
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 break;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (nf) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 fprintf(stderr,
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 "%s line %d: empty line in the middle of a card data block\n",
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 infname, lineno);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 if (nf)
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 putchar(' ');
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 printf("%s=%s", field_names[nf], linebuf);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 putchar('\n');
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 main(argc, argv)
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 char **argv;
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 if (argc != 2) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 fprintf(stderr, "usage: %s email-extract-file\n", argv[0]);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 infname = argv[1];
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 inf = fopen(infname, "r");
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 if (!inf) {
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 perror(infname);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 exit(1);
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 }
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 read_field_names();
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 for (;;)
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 process_one_card();
372ecc4aa2c4 off-line utils ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 }