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_nil, NULL },
18 char *pnd_conf_query_searchpath ( void ) {
22 ch = pnd_conf_fetch_by_id ( pnd_conf_conf, PND_CONF_SEARCHPATH );
25 //printf ( "Couldn't locate base conf file '%s'\n", PND_CONF_FILE );
26 return ( strdup ( PND_CONF_SEARCHPATH ) );
29 // can we find a user-specified config path? if so, use it.. if not, fall back!
33 temp = pnd_conf_get_as_char ( ch, PND_CONF_KEY );
36 searchpath = strdup ( temp );
38 searchpath = strdup ( PND_CONF_SEARCHPATH );
41 return ( searchpath );
44 pnd_conf_handle pnd_conf_fetch_by_id ( pnd_conf_filename_e id, char *searchpath ) {
45 pnd_conf_filename_t *p = pnd_conf_filenames;
47 while ( p -> filename ) {
49 /* found the filename associated to the id? */
50 if ( p -> id == id ) {
51 return ( pnd_conf_fetch_by_name ( p -> filename, searchpath ) );
61 pnd_conf_handle pnd_conf_fetch_by_name ( char *filename, char *searchpath ) {
63 /* the fun part here is that we get to cheat; while we have to search through all the directories
64 * listed in the search path, we can stop at the first matching file. Nothign really fancy going on, and
65 * no need for comprehensive directory crawling. yay!
69 //printf ( "Search path: '%s'\n", searchpath );
74 strncat ( buffer, "/", FILENAME_MAX - 1 );
75 strncat ( buffer, filename, FILENAME_MAX - 1 );
76 conf = pnd_conf_fetch_by_path ( buffer );
88 pnd_conf_handle pnd_conf_fetch_by_path ( char *fullpath ) {
90 char section [ 256 ] = "";
91 char buffer [ FILENAME_MAX ];
92 char inbuffer [ FILENAME_MAX ];
93 char *c, *head, *tail, *mid;
95 //printf ( "Attempt to load config from fullpath '%s'\n", fullpath );
98 * No check yet to verify the directory is actually mounted and readable; either way
99 * this should not block or take up much time, though SD cards might be slow to open over
100 * and over again .. perhaps need some smarts or caching of results or somesuch, since this
101 * call gets spammed over and over...
103 f = fopen ( fullpath, "r" );
109 // damn, we actually found a file, so need to try to parse it. Shucks. Give back a box-handle
110 // so the consumer has some lists to look at
112 h = pnd_box_new ( fullpath );
115 while ( fgets ( inbuffer, FILENAME_MAX, f ) ) {
117 // strip line-endings and DOSisms
118 if ( ( c = strchr ( inbuffer, '\r' ) ) ) {
122 if ( ( c = strchr ( inbuffer, '\n' ) ) ) {
126 //printf ( "config line: '%s'\n", inbuffer );
129 if ( ( c = strchr ( inbuffer, '#' ) ) ) {
133 // strip leading and trailing spaces
135 while ( *head && isspace ( *head ) ) {
139 if ( inbuffer [ 0 ] == '\0' ) {
140 //printf ( " -> discard\n" );
141 continue; // skip, the line was pure comment or blank
144 tail = strchr ( inbuffer, '\0' ) - 1;
145 while ( *tail && isspace ( *tail ) ) {
150 if ( inbuffer [ 0 ] == '\0' ) {
151 //printf ( " -> discard\n" );
152 continue; // skip, the line was pure comment or blank
155 // decorated, ie: a section?
156 if ( *head == '[' && *tail == ']' ) {
157 // note: handle the nil-section
159 memset ( section, '\0', 256 );
161 if ( tail == head + 1 ) {
162 section [ 0 ] = '\0';
164 strncpy ( section, head + 1, tail - head - 1 );
167 //printf ( " -> section '%s'\n", section );
171 // must be a key (and likely a value) .. find the division
173 while ( *mid && ! isspace ( *mid ) ) {
179 //printf ( "key head: '%s'\n", head );
180 //printf ( "key mid: '%s'\n", mid );
182 // is thjis a key/value pair, or just a key?
187 // form the actual new key
188 if ( section [ 0 ] ) {
189 snprintf ( buffer, FILENAME_MAX - 1, "%s.%s", section, head );
191 strncpy ( buffer, head, FILENAME_MAX - 1 );
194 //printf ( "Found key '%s' in config file\n", buffer );
196 // alloc node into the box
197 v = pnd_box_allocinsert ( h, buffer, strlen ( mid ) + 1 ); // allow for trailing null
202 return ( NULL ); // OOM while reading conf is either sad, or really scary conf (also sad.)
209 // form the actual new key
210 if ( section [ 0 ] ) {
211 snprintf ( buffer, FILENAME_MAX - 1, "%s.%s", section, head );
213 strncpy ( buffer, head, FILENAME_MAX - 1 );
216 //printf ( "Found key with no value '%s' in config file\n", buffer );
218 // alloc node into the box
219 v = pnd_box_allocinsert ( h, buffer, 0 ); // zero b/c of no payload
222 return ( NULL ); // OOM while reading conf is either sad, or really scary conf (also sad.)
225 } // key or key/value?
227 } // section or key/value line?
237 char *pnd_conf_get_as_char ( pnd_conf_handle c, char *key ) {
238 return ( pnd_box_find_by_key ( c, key ) );