2 * Reads lexical config files and updates database.
4 * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
6 * Copyright (C) 2001-2003
7 * David Corcoran <corcoran@musclecard.com>
8 * Copyright (C) 2003-2010
9 * Ludovic Rousseau <ludovic.rousseau@free.fr>
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. The name of the author may not be used to endorse or promote products
21 derived from this software without specific prior written permission.
23 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * @brief provides parsing functions for Info.plist files
56 static void eval_key(char *pcToken, list_t *list_key);
57 static void eval_value(char *pcToken, list_t *list_values);
58 void tperrorCheck (char *pcToken_error);
60 static list_t *ListKeys;
61 static list_t *ListValues;
73 \<key\>([A-Z]|[a-z]|[0-9]|[ \t])+\<\/key\> { eval_key(yytext, ListKeys); }
75 \<string\>.+\<\/string\> { eval_value(yytext, ListValues); }
76 . { tperrorCheck(yytext); }
80 static void eval_key(char *pcToken, list_t *list_key)
82 struct bundleElt *elt;
86 /* create a new list element */
87 elt = malloc(sizeof(*elt));
91 * 012345 : 5 is the first key character index */
93 /* calculate the argument length */
94 for (len=0; pcToken[len+5] != '<'; len++)
96 len++; /* final NULL byte */
98 elt->key = malloc(len);
99 memcpy(elt->key, &pcToken[5], len-1);
100 elt->key[len-1] = '\0';
102 r = list_init(&elt->values);
106 /* add the key/values */
107 list_append(list_key, elt);
109 /* set the list to store the values */
110 ListValues = &elt->values;
113 static void eval_value(char *pcToken, list_t *list_values)
120 /* <string>foobar</string>
121 * 012345678 : 8 is the first string character index */
123 /* calculate the argument length */
124 for (len=0; pcToken[len+8] != '<'; len++)
126 len++; /* final NULL byte */
131 memcpy(value, &pcToken[8], len-1);
134 /* for all & in the string */
136 while ((amp = strstr(amp, "&")) != NULL)
140 /* just skip "amp;" substring (4 letters) */
141 for (p = amp+1; *(p+4); p++)
145 /* terminate the now shorter string */
148 /* skip the & and continue */
152 r = list_append(list_values, value);
157 void tperrorCheck (char *token_error)
163 * Find an optional key in a configuration file
164 * No error is logged if the key is not found
166 * @param l list generated by bundleParse()
167 * @param key searched key
168 * @param[out] values list of token value (if key found)
170 * @retval 1 key not found
172 int LTPBundleFindValueWithKey(list_t *l, const char *key, list_t **values)
177 for (i=0; i < list_size(l); i++)
179 struct bundleElt *elt;
181 elt = list_get_at(l, i);
184 if (0 == strcmp(elt->key, key))
186 *values = &elt->values;
196 * Parse a Info.plist file and file a list
198 * @param fileName file name
199 * @param l list containing the results
200 * @retval -1 configuration file not found
203 int bundleParse(const char *fileName, list_t *l)
211 file = fopen(fileName, "r");
214 Log3(PCSC_LOG_CRITICAL, "Could not open bundle file %s: %s",
215 fileName, strerror(errno));
229 } while (!feof(file));
235 printf("size: %d\n", list_size(l));
236 for (i=0; i < list_size(l); i++)
238 struct bundleElt *elt;
241 elt = list_get_at(l, i);
243 printf("Key: %s\n", elt->key);
245 for (j=0; j<list_size(&elt->values); j++)
247 char *v = list_get_at(&elt->values, j);
248 printf(" value: %s\n", v);
257 * Free the list created by bundleParse()
259 * @param l list containing the results
261 void bundleRelease(list_t *l)
265 for (i=0; i < list_size(l); i++)
267 struct bundleElt *elt;
270 elt = list_get_at(l, i);
273 /* free all the values */
274 for (j=0; j<list_size(&elt->values); j++)
275 free(list_get_at(&elt->values, j));
276 list_destroy(&elt->values);