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 */
8 #include "pnd_container.h"
9 #include "pnd_pathiter.h"
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_nil, NULL },
19 char *pnd_conf_query_searchpath ( void ) {
23 ch = pnd_conf_fetch_by_id ( pnd_conf_conf, PND_CONF_SEARCHPATH );
26 //printf ( "Couldn't locate base conf file '%s'\n", PND_CONF_FILE );
27 return ( strdup ( PND_CONF_SEARCHPATH ) );
30 // can we find a user-specified config path? if so, use it.. if not, fall back!
34 temp = pnd_conf_get_as_char ( ch, PND_CONF_KEY );
37 searchpath = strdup ( temp );
39 searchpath = strdup ( PND_CONF_SEARCHPATH );
42 return ( searchpath );
45 pnd_conf_handle pnd_conf_fetch_by_id ( pnd_conf_filename_e id, char *searchpath ) {
46 pnd_conf_filename_t *p = pnd_conf_filenames;
48 while ( p -> filename ) {
50 /* found the filename associated to the id? */
51 if ( p -> id == id ) {
52 return ( pnd_conf_fetch_by_name ( p -> filename, searchpath ) );
62 pnd_conf_handle pnd_conf_fetch_by_name ( char *filename, char *searchpath ) {
64 /* the fun part here is that we get to cheat; while we have to search through all the directories
65 * listed in the search path, we can stop at the first matching file. Nothign really fancy going on, and
66 * no need for comprehensive directory crawling. yay!
70 //printf ( "Search path: '%s'\n", searchpath );
75 strncat ( buffer, "/", FILENAME_MAX - 1 );
76 strncat ( buffer, filename, FILENAME_MAX - 1 );
77 conf = pnd_conf_fetch_by_path ( buffer );
89 pnd_conf_handle pnd_conf_fetch_by_path ( char *fullpath ) {
91 char section [ 256 ] = "";
92 char buffer [ FILENAME_MAX ];
93 char inbuffer [ FILENAME_MAX ];
94 char *c, *head, *tail, *mid;
96 //printf ( "Attempt to load config from fullpath '%s'\n", fullpath );
99 * No check yet to verify the directory is actually mounted and readable; either way
100 * this should not block or take up much time, though SD cards might be slow to open over
101 * and over again .. perhaps need some smarts or caching of results or somesuch, since this
102 * call gets spammed over and over...
104 f = fopen ( fullpath, "r" );
110 // damn, we actually found a file, so need to try to parse it. Shucks. Give back a box-handle
111 // so the consumer has some lists to look at
113 h = pnd_box_new ( fullpath );
116 while ( fgets ( inbuffer, FILENAME_MAX, f ) ) {
118 // strip line-endings and DOSisms
119 if ( ( c = strchr ( inbuffer, '\r' ) ) ) {
123 if ( ( c = strchr ( inbuffer, '\n' ) ) ) {
127 //printf ( "config line: '%s'\n", inbuffer );
130 if ( ( c = strchr ( inbuffer, '#' ) ) ) {
134 // strip leading and trailing spaces
136 while ( *head && isspace ( *head ) ) {
140 if ( inbuffer [ 0 ] == '\0' ) {
141 //printf ( " -> discard\n" );
142 continue; // skip, the line was pure comment or blank
145 tail = strchr ( inbuffer, '\0' ) - 1;
146 while ( *tail && isspace ( *tail ) ) {
151 if ( inbuffer [ 0 ] == '\0' ) {
152 //printf ( " -> discard\n" );
153 continue; // skip, the line was pure comment or blank
156 // decorated, ie: a section?
157 if ( *head == '[' && *tail == ']' ) {
158 // note: handle the nil-section
160 memset ( section, '\0', 256 );
162 if ( tail == head + 1 ) {
163 section [ 0 ] = '\0';
165 strncpy ( section, head + 1, tail - head - 1 );
168 //printf ( " -> section '%s'\n", section );
172 // must be a key (and likely a value) .. find the division
174 while ( *mid && ! isspace ( *mid ) ) {
180 //printf ( "key head: '%s'\n", head );
181 //printf ( "key mid: '%s'\n", mid );
183 // is thjis a key/value pair, or just a key?
188 // form the actual new key
189 if ( section [ 0 ] ) {
190 snprintf ( buffer, FILENAME_MAX - 1, "%s.%s", section, head );
192 strncpy ( buffer, head, FILENAME_MAX - 1 );
195 //printf ( "Found key '%s' in config file\n", buffer );
197 // alloc node into the box
198 v = pnd_box_allocinsert ( h, buffer, strlen ( mid ) + 1 ); // allow for trailing null
203 return ( NULL ); // OOM while reading conf is either sad, or really scary conf (also sad.)
210 // form the actual new key
211 if ( section [ 0 ] ) {
212 snprintf ( buffer, FILENAME_MAX - 1, "%s.%s", section, head );
214 strncpy ( buffer, head, FILENAME_MAX - 1 );
217 //printf ( "Found key with no value '%s' in config file\n", buffer );
219 // alloc node into the box
220 v = pnd_box_allocinsert ( h, buffer, 0 ); // zero b/c of no payload
223 return ( NULL ); // OOM while reading conf is either sad, or really scary conf (also sad.)
226 } // key or key/value?
228 } // section or key/value line?
238 char *pnd_conf_get_as_char ( pnd_conf_handle c, char *key ) {
239 return ( pnd_box_find_by_key ( c, key ) );