comparison utils/sws-email2db.c @ 17:372ecc4aa2c4

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