Merge branch 'master' of ssh://skeezixgit@git.openpandora.org/srv/git/pandora-libraries
[pandora-libraries.git] / lib / pnd_conf.c
1
2 #include <stdio.h> /* for NULL, printf, FILE, etc */
3 #include <stdlib.h> /* for malloc */
4 #include <string.h> /* for strdup */
5 #include <ctype.h> /* for isspace */
6
7 #include "pnd_conf.h"
8 #include "pnd_container.h"
9 #include "pnd_pathiter.h"
10
11 pnd_conf_filename_t pnd_conf_filenames[] = {
12   { pnd_conf_conf,         PND_CONF_FILE },
13   { pnd_conf_apps,         "apps" },
14   { pnd_conf_startup,      "startup" },
15   { pnd_conf_desktop,      "desktop" },
16   { pnd_conf_categories,   "categories" },
17   { pnd_conf_evmap,        "eventmap" },
18   { pnd_conf_nil,          NULL },
19 };
20
21 char *pnd_conf_query_searchpath ( void ) {
22   pnd_conf_handle ch;
23
24   // fetch base config
25   ch = pnd_conf_fetch_by_id ( pnd_conf_conf, PND_CONF_SEARCHPATH );
26
27   if ( ! ch ) {
28     //printf ( "Couldn't locate base conf file '%s'\n", PND_CONF_FILE );
29     return ( strdup ( PND_CONF_SEARCHPATH ) );
30   }
31
32   // can we find a user-specified config path? if so, use it.. if not, fall back!
33   char *searchpath;
34   char *temp;
35
36   temp = pnd_conf_get_as_char ( ch, PND_CONF_KEY );
37
38   if ( temp ) {
39     searchpath = strdup ( temp );
40   } else {
41     searchpath = strdup ( PND_CONF_SEARCHPATH );
42   }
43
44   return ( searchpath );
45 }
46
47 pnd_conf_handle pnd_conf_fetch_by_id ( pnd_conf_filename_e id, char *searchpath ) {
48   pnd_conf_filename_t *p = pnd_conf_filenames;
49
50   while ( p -> filename ) {
51
52     /* found the filename associated to the id? */
53     if ( p -> id == id ) {
54       return ( pnd_conf_fetch_by_name ( p -> filename, searchpath ) );
55     }
56
57     /* next! */
58     p++;
59   } /* while */
60
61   return ( NULL );
62 }
63
64 pnd_conf_handle pnd_conf_fetch_by_name ( char *filename, char *searchpath ) {
65
66   /* the fun part here is that we get to cheat; while we have to search through all the directories
67    * listed in the search path, we can stop at the first matching file. Nothign really fancy going on, and
68    * no need for comprehensive directory crawling. yay!
69    */
70   pnd_conf_handle conf;
71
72   //printf ( "Search path: '%s'\n", searchpath );
73
74   SEARCHPATH_PRE
75   {
76
77     strncat ( buffer, "/", FILENAME_MAX - 1 );
78     strncat ( buffer, filename, FILENAME_MAX - 1 );
79     conf = pnd_conf_fetch_by_path ( buffer );
80
81     if ( conf ) {
82       return ( conf );
83     }
84
85   }
86   SEARCHPATH_POST
87
88   return ( NULL );
89 }
90
91 pnd_conf_handle pnd_conf_fetch_by_path ( char *fullpath ) {
92   FILE *f;
93   char section [ 256 ] = "";
94   char buffer [ FILENAME_MAX ];
95   char inbuffer [ FILENAME_MAX ];
96   char *c, *head, *tail, *mid;
97
98   //printf ( "Attempt to load config from fullpath '%s'\n", fullpath );
99
100   /* WARN:
101    * No check yet to verify the directory is actually mounted and readable; either way
102    * this should not block or take up much time, though SD cards might be slow to open over
103    * and over again .. perhaps need some smarts or caching of results or somesuch, since this
104    * call gets spammed over and over...
105    */
106   f = fopen ( fullpath, "r" );
107
108   if ( ! f ) {
109     return ( NULL );
110   }
111
112   // damn, we actually found a file, so need to try to parse it. Shucks. Give back a box-handle
113   // so the consumer has some lists to look at
114   pnd_box_handle h;
115   h = pnd_box_new ( fullpath );
116
117   // inhale file
118   while ( fgets ( inbuffer, FILENAME_MAX, f ) ) {
119
120     // strip line-endings and DOSisms
121     if ( ( c = strchr ( inbuffer, '\r' ) ) ) {
122       *c = '\0';
123     }
124
125     if ( ( c = strchr ( inbuffer, '\n' ) ) ) {
126       *c = '\0';
127     }
128
129     //printf ( "config line: '%s'\n", inbuffer );
130
131     // strip comments
132     if ( ( c = strchr ( inbuffer, '#' ) ) ) {
133       *c = '\0';
134     }
135
136     // strip leading and trailing spaces
137     head = inbuffer;
138     while ( *head && isspace ( *head ) ) {
139       head++;
140     }
141
142     if ( inbuffer [ 0 ] == '\0' ) {
143       //printf ( "  -> discard\n" );
144       continue; // skip, the line was pure comment or blank
145     }
146
147     tail = strchr ( inbuffer, '\0' ) - 1;
148     while ( *tail && isspace ( *tail ) ) {
149       *tail = '\0';
150       tail--;
151     }
152
153     if ( inbuffer [ 0 ] == '\0' ) {
154       //printf ( "  -> discard\n" );
155       continue; // skip, the line was pure comment or blank
156     }
157
158     // decorated, ie: a section?
159     if ( *head == '[' && *tail == ']' ) {
160       // note: handle the nil-section
161
162       memset ( section, '\0', 256 );
163
164       if ( tail == head + 1 ) {
165         section [ 0 ] = '\0';
166       } else {
167         strncpy ( section, head + 1, tail - head - 1 );
168       }
169
170       //printf ( " -> section '%s'\n", section );
171
172     } else {
173
174       // must be a key (and likely a value) .. find the division
175       mid = head;
176       while ( *mid && ! isspace ( *mid ) ) {
177         mid++;
178       }
179       *mid = '\0';
180       mid++;
181
182       // skip past any heading space for the key
183       while ( *mid && isspace ( *mid ) ) {
184         mid++;
185       }
186
187       //printf ( "key head: '%s'\n", head );
188       //printf ( "key mid: '%s'\n", mid );
189
190       // is thjis a key/value pair, or just a key?
191       if ( mid [ 0 ] ) {
192         // key/value pairing
193         char *v;
194
195         // form the actual new key
196         if ( section [ 0 ] ) {
197           snprintf ( buffer, FILENAME_MAX - 1, "%s.%s", section, head );
198         } else {
199           strncpy ( buffer, head, FILENAME_MAX - 1 );
200         }
201
202         //printf ( "Found key '%s' in config file\n", buffer );
203
204         // alloc node into the box
205         v = pnd_box_allocinsert ( h, buffer, strlen ( mid ) + 1 ); // allow for trailing null
206
207         if ( v ) {
208           strcpy ( v, mid );
209         } else {
210           return ( NULL ); // OOM while reading conf is either sad, or really scary conf (also sad.)
211         }
212
213       } else {
214         // key only
215         char *v;
216
217         // form the actual new key
218         if ( section [ 0 ] ) {
219           snprintf ( buffer, FILENAME_MAX - 1, "%s.%s", section, head );
220         } else {
221           strncpy ( buffer, head, FILENAME_MAX - 1 );
222         }
223
224         //printf ( "Found key with no value '%s' in config file\n", buffer );
225
226         // alloc node into the box
227         v = pnd_box_allocinsert ( h, buffer, 0 ); // zero b/c of no payload
228
229         if ( ! v ) {
230           return ( NULL ); // OOM while reading conf is either sad, or really scary conf (also sad.)
231         }
232
233       } // key or key/value?
234
235     } // section or key/value line?
236     
237   } // while
238
239   // clean up a trifle
240   fclose ( f );
241
242   return ( h );
243 }
244
245 char *pnd_conf_get_as_char ( pnd_conf_handle c, char *key ) {
246   return ( pnd_box_find_by_key ( c, key ) );
247 }
248
249 int pnd_conf_get_as_int ( pnd_conf_handle c, char *key ) {
250   char *t = pnd_box_find_by_key ( c, key );
251
252   if ( ! t ) {
253     return ( PND_CONF_BADNUM ); // non-existant
254   }
255
256   int i = atoi ( t );
257
258   return ( i );
259 }
260
261 int pnd_conf_get_as_int_d ( pnd_conf_handle c, char *key, int def ) {
262   char *t = pnd_box_find_by_key ( c, key );
263
264   if ( ! t ) {
265     return ( def ); // non-existant
266   }
267
268   int i = atoi ( t );
269
270   return ( i );
271 }
272
273 int *pnd_conf_set_int ( pnd_conf_handle c, char *key, int v ) {
274
275   // key is already present? if so, delete it
276   void *kv = pnd_box_find_by_key ( c, key );
277
278   if ( kv ) {
279     pnd_box_delete_node ( c, kv );
280   }
281
282   // add the new node
283   int *nv = pnd_box_allocinsert ( c, key, sizeof(int) );
284
285   if ( nv ) {
286     *nv = v;
287     return ( nv );
288   }
289
290   return ( NULL );
291 }
292
293 char *pnd_conf_set_char ( pnd_conf_handle c, char *key, char *v ) {
294
295   // key is already present? if so, delete it
296   char *kv = pnd_box_find_by_key ( c, key );
297
298   if ( kv ) {
299     pnd_box_delete_node ( c, kv );
300   }
301
302   // add the new node
303   char *nv = pnd_box_allocinsert ( c, key, strlen ( v ) + 1 );
304
305   if ( nv ) {
306     strcpy ( nv, v );
307     return ( nv );
308   }
309
310   return ( NULL );
311 }
312
313 unsigned char pnd_conf_write ( pnd_conf_handle c, char *fullpath ) {
314   char *p = pnd_box_get_head ( c );
315   FILE *f;
316   char lastcategory [ 100 ] = "";
317
318   if ( ! p ) {
319     return ( 1 ); // nothing to save, so.. success?
320   }
321
322   f = fopen ( fullpath, "w" );
323
324   if ( ! f ) {
325     return ( 0 );
326   }
327
328   while ( p ) {
329     char *k = pnd_box_get_key ( p );
330     char *c = strchr ( k, '.' );
331
332     if ( c ) {
333       if ( strncmp ( k, lastcategory, c - k ) == 0 ) {
334         // same category
335       } else {
336         strncpy ( lastcategory, k, c - k );
337         fprintf ( f, "[%s]\n", lastcategory );
338       }
339       fprintf ( f, "%s\t%s\n", c + 1, p );
340     } else {
341       if ( lastcategory [ 0 ] ) {
342         fprintf ( f, "[]\n" );
343       }
344       lastcategory [ 0 ] = '\0';
345       fprintf ( f, "%s\t%s\n", k, p );
346     }
347
348     p = pnd_box_get_next ( p );
349   }
350
351   fclose ( f );
352
353   return ( 1 );
354 }