7 A Linux/Unix toolset and driver for Nokia mobile phones.
9 Released under the terms of the GNU GPL, see file COPYING for more details.
11 Config file (/etc/gnokiirc and ~/.gnokiirc) reader.
13 Modified from code by Tim Potter.
28 #include "files/cfgreader.h"
30 /* Read configuration information from a ".INI" style file */
31 struct CFG_Header *CFG_ReadFile(char *filename)
36 struct CFG_Header *cfg_info = NULL, *cfg_head = NULL;
39 if (filename == NULL) {
44 if ((buf = (char *)malloc(255)) == NULL) {
49 if ((handle = fopen(filename, "r")) == NULL) {
51 fprintf( stderr, "CFG_ReadFile - open %s: %s\n", filename, strerror(errno));
57 fprintf( stderr, "Opened configuration file %s\n", filename );
60 /* Iterate over lines in the file */
61 while (fgets(buf, 255, handle) != NULL) {
65 /* Strip leading, trailing whitespace */
66 while(isspace((int) *line))
69 while((strlen(line) > 0) && isspace((int) line[strlen(line) - 1]))
70 line[strlen(line) - 1] = '\0';
72 /* Ignore blank lines and comments */
73 if ((*line == '\n') || (*line == '\0') || (*line == '#'))
76 /* Look for "headings" enclosed in square brackets */
77 if ((line[0] == '[') && (line[strlen(line) - 1] == ']')) {
78 struct CFG_Header *heading;
80 /* Allocate new heading entry */
81 if ((heading = (struct CFG_Header *)malloc(sizeof(*heading))) == NULL) {
86 memset(heading, '\0', sizeof(*heading));
89 line[strlen(line) - 1] = '\0';
91 /* FIXME: strdup is not ANSI C compliant. */
92 heading->section = strdup(line);
94 /* Add to tail of list */
95 heading->prev = cfg_info;
97 if (cfg_info != NULL) {
98 cfg_info->next = heading;
100 /* Store copy of head of list for return value */
107 fprintf(stderr, "Added new section %s\n", heading->section);
109 /* Go on to next line */
114 /* Process key/value line */
116 if ((strchr(line, '=') != NULL) && cfg_info != NULL) {
117 struct CFG_Entry *entry;
120 /* Allocate new entry */
121 if ((entry = (struct CFG_Entry *)malloc(sizeof(*entry))) == NULL) {
126 memset(entry, '\0', sizeof(*entry));
128 value = strchr(line, '=');
129 *value = '\0'; /* Split string */
132 while(isspace((int) *value)) { /* Remove leading white */
136 entry->value = strdup(value);
138 while((strlen(line) > 0) && isspace((int) line[strlen(line) - 1])) {
139 line[strlen(line) - 1] = '\0'; /* Remove trailing white */
142 /* FIXME: strdup is not ANSI C compliant. */
143 entry->key = strdup(line);
145 /* Add to head of list */
147 entry->next = cfg_info->entries;
149 if (cfg_info->entries != NULL) {
150 cfg_info->entries->prev = entry;
153 cfg_info->entries = entry;
156 fprintf(stderr, "Adding key/value %s/%s\n", entry->key, entry->value);
158 /* Go on to next line */
162 /* Line not part of any heading */
163 fprintf(stderr, "Orphaned line: %s\n", line);
166 /* Return pointer to configuration information */
170 /* Write configuration information to a config file */
172 int CFG_WriteFile(struct CFG_Header *cfg, char *filename)
174 /* Not implemented - tricky to do and preserve comments */
180 * Find the value of a key in a config file. Return value associated
181 * with key or NULL if no such key exists.
184 char *CFG_Get(struct CFG_Header *cfg, char *section, char *key)
186 struct CFG_Header *h;
189 if ((cfg == NULL) || (section == NULL) || (key == NULL)) {
193 /* Search for section name */
194 for (h = cfg; h != NULL; h = h->next) {
195 if (strcmp(section, h->section) == 0) {
196 /* Search for key within section */
197 for (e = h->entries; e != NULL; e = e->next) {
198 if (strcmp(key, e->key) == 0) {
205 /* Key not found in section */
209 /* Set the value of a key in a config file. Return the new value if
210 the section/key can be found, else return NULL. */
212 char *CFG_Set(struct CFG_Header *cfg, char *section, char *key,
215 struct CFG_Header *h;
218 if ((cfg == NULL) || (section == NULL) || (key == NULL) ||
223 /* Search for section name */
224 for (h = cfg; h != NULL; h = h->next) {
225 if (strcmp(section, h->section) == 0) {
226 /* Search for key within section */
227 for (e = h->entries; e != NULL; e = e->next) {
228 if ((e->key != NULL) && strcmp(key, e->key) == 0) {
229 /* Found - set value */
231 /* FIXME: strdup is not ANSI C compliant. */
232 e->key = strdup(value);
238 /* Key not found in section */
242 struct CFG_Header *CFG_FindGnokiirc()
244 struct CFG_Header *cfg_info;
249 homedir = getenv("HOMEDRIVE");
250 strncpy(rcfile, homedir ? homedir : "", 200);
251 homedir = getenv("HOMEPATH");
252 strncat(rcfile, homedir ? homedir : "", 200);
253 strncat(rcfile, "\gnokiirc", 200);
255 homedir = getenv("HOME");
256 if (homedir) strncpy(rcfile, homedir, 200);
257 strncat(rcfile, "/.gnokiirc", 200);
260 /* Try opening .gnokirc from users home directory first */
261 if ((cfg_info = CFG_ReadFile(rcfile)) == NULL) {
264 /* It failed so try for /etc/gnokiirc */
265 if ((cfg_info = CFG_ReadFile("/etc/gnokiirc")) == NULL) {
266 /* That failed too so exit */
268 fprintf(stderr, _("Couldn't open %s or /etc/gnokiirc. Using defaults...\n"), rcfile);
275 /* It failed so try for gnokiirc */
276 if ((cfg_info = CFG_ReadFile("gnokiirc")) == NULL) {
277 /* That failed too so exit */
279 fprintf(stderr, _("Couldn't open %s or gnokiirc. Using defaults...\n"), rcfile);
290 int CFG_ReadConfig(char **model, char **port, char **initlength,
291 char **connection, char **bindir, char **synchronizetime,
294 struct CFG_Header *cfg_info;
296 char *DefaultPort = "com2:";
298 char *DefaultPort = "/dev/ttyS1";
300 char *DefaultModel = "auto";
301 char *DefaultConnection = "fbus";
302 char *DefaultBindir = "/usr/local/sbin/";
303 char *DefaultSynchronizeTime = "yes";
304 char *DefaultInitLength = "30";
306 char *section = "global";
308 (char *)*model = DefaultModel;
309 (char *)*port = DefaultPort;
310 (char *)*connection = DefaultConnection;
311 (char *)*bindir = DefaultBindir;
312 (char *)*synchronizetime = DefaultSynchronizeTime;
313 (char *)*initlength = DefaultInitLength;
315 cfg_info=CFG_FindGnokiirc();
316 if (cfg_info==NULL) return 0;
318 if (isgnokiid) (char *)section = "gnokiid";
320 (char *)*model = CFG_Get(cfg_info, section, "model");
321 if (!*model) (char *)*model = DefaultModel;
323 (char *)*port = CFG_Get(cfg_info, section, "port");
324 if (!*port) (char *)*port = DefaultPort;
326 (char *)*connection = CFG_Get(cfg_info, section, "connection");
327 if (!*connection) (char *)*connection = DefaultConnection;
329 (char *)*bindir = CFG_Get(cfg_info, section, "bindir");
330 if (!*bindir) (char *)*bindir = DefaultBindir;
332 (char *)*synchronizetime = CFG_Get(cfg_info, section, "synchronizetime");
333 if (!*synchronizetime) (char *)*synchronizetime = DefaultSynchronizeTime;
335 (char *)*initlength = CFG_Get(cfg_info, section, "initlength");
336 if (!*initlength) (char *)*initlength = "default";