FreeCalypso > hg > fc-sim-tools
annotate libutil/dbread.c @ 93:6041c601304d
fcsim1-mkprov: revert OTA key addition
It appears that GrcardSIM2 cards (which is what we got for FCSIM1)
do not support OTA after all, contrary to what we were previously
led to believe by some tech support emails from Grcard - apparently
those support emails and OTA descriptions referred to some other
card model(s).
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 21 Apr 2021 05:38:39 +0000 |
parents | fa81221ac9b6 |
children |
rev | line source |
---|---|
27
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements functions for reading key=value database files. |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <ctype.h> |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdio.h> |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdlib.h> |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <string.h> |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <strings.h> |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include "dbread.h" |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 static |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 parse_read_line(dbs, filename_for_errs, lineno_for_errs) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 struct dbread_state *dbs; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 char *filename_for_errs; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 int lineno_for_errs; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 unsigned n; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 char *cp, *np; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 n = 0; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 for (cp = dbs->linebuf; ; ) { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 while (isspace(*cp)) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 cp++; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (!*cp) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 break; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (*cp == '=') { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 inv_syntax: fprintf(stderr, "%s line %d: invalid syntax\n", |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 filename_for_errs, lineno_for_errs); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 return(-1); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 for (np = cp; ; cp++) { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (*cp == '=') |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 break; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 if (!*cp || isspace(*cp)) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 goto inv_syntax; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 *cp++ = '\0'; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 if (n >= DBREAD_MAX_KV_PAIRS) { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 fprintf(stderr, |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 "%s line %d: too many key=value pairs\n", |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 filename_for_errs, lineno_for_errs); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 return(-1); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 dbs->kv_pairs[n].key = np; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 dbs->kv_pairs[n].value = cp; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 n++; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 while (*cp && !isspace(*cp)) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 cp++; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (*cp) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 *cp++ = '\0'; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 dbs->num_kv_pairs = n; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 return(0); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 char * |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 dbread_find_key(dbs, sought_key) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 struct dbread_state *dbs; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 char *sought_key; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 unsigned n; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 for (n = 0; n < dbs->num_kv_pairs; n++) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 if (!strcasecmp(dbs->kv_pairs[n].key, sought_key)) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 return dbs->kv_pairs[n].value; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 return 0; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 |
28
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
70 char * |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
71 dbread_find_key_req(dbs, sought_key) |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
72 struct dbread_state *dbs; |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
73 char *sought_key; |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
74 { |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
75 char *val; |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
76 |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
77 val = dbread_find_key(dbs, sought_key); |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
78 if (val) |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
79 return val; |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
80 fprintf(stderr, "error: card record has no %s field\n", sought_key); |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
81 return 0; |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
82 } |
fa81221ac9b6
retrieval of required db fields moved into libutil
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
83 |
27
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 dbread_find_record(filename, dbs, sought_key, sought_value) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 char *filename, *sought_key, *sought_value; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 struct dbread_state *dbs; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 FILE *inf; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 int lineno; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 char *cp; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 inf = fopen(filename, "r"); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 if (!inf) { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 perror(filename); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 return(-1); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 for (lineno = 1; fgets(dbs->linebuf, DBREAD_LINEBUF_SIZE, inf); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 lineno++) { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 if (!index(dbs->linebuf, '\n')) { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 fprintf(stderr, |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 "%s line %d: too long or missing newline\n", |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 filename, lineno); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 fclose(inf); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 return(-1); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 if (parse_read_line(dbs, filename, lineno) < 0) { |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 /* error msg already printed */ |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 fclose(inf); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 return(-1); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 cp = dbread_find_key(dbs, sought_key); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 if (!cp) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 continue; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 if (strcmp(cp, sought_value)) |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 continue; |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 /* found our record: struct dbread_state is our return */ |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 fclose(inf); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 return(0); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
119 } |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
120 fclose(inf); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
121 fprintf(stderr, "error: %s=%s not found in %s\n", sought_key, |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
122 sought_value, filename); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
123 return(-1); |
ca8a6f95826a
implemented sws-card-lookup and underlying libutil functions
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
124 } |