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"
32 /* Read configuration information from a ".INI" style file */
33 static struct CFG_Header *CFG_ReadFile(char *filename)
38 struct CFG_Header *cfg_info = NULL, *cfg_head = NULL;
41 if (filename == NULL) {
46 if ((buf = (char *)malloc(255)) == NULL) {
51 if ((handle = fopen(filename, "r")) == NULL) {
53 // fprintf( stderr, "CFG_ReadFile - open %s: %s\n", filename, strerror(errno));
59 fprintf( stderr, "Opened configuration file %s\n", filename );
62 /* Iterate over lines in the file */
63 while (fgets(buf, 255, handle) != NULL) {
67 /* Strip leading, trailing whitespace */
68 while(isspace((int) *line))
71 while((strlen(line) > 0) && isspace((int) line[strlen(line) - 1]))
72 line[strlen(line) - 1] = '\0';
74 /* Ignore blank lines and comments */
75 if ((*line == '\n') || (*line == '\0') || (*line == '#'))
78 /* Look for "headings" enclosed in square brackets */
79 if ((line[0] == '[') && (line[strlen(line) - 1] == ']')) {
80 struct CFG_Header *heading;
82 /* Allocate new heading entry */
83 if ((heading = (struct CFG_Header *)malloc(sizeof(*heading))) == NULL) {
88 memset(heading, '\0', sizeof(*heading));
91 line[strlen(line) - 1] = '\0';
93 /* FIXME: strdup is not ANSI C compliant. */
94 heading->section = strdup(line);
96 /* Add to tail of list */
97 heading->prev = cfg_info;
99 if (cfg_info != NULL) {
100 cfg_info->next = heading;
102 /* Store copy of head of list for return value */
109 // fprintf(stderr, "Added new section %s\n", heading->section);
111 /* Go on to next line */
116 /* Process key/value line */
118 if ((strchr(line, '=') != NULL) && cfg_info != NULL) {
119 struct CFG_Entry *entry;
122 /* Allocate new entry */
123 if ((entry = (struct CFG_Entry *)malloc(sizeof(*entry))) == NULL) {
128 memset(entry, '\0', sizeof(*entry));
130 value = strchr(line, '=');
131 *value = '\0'; /* Split string */
134 while(isspace((int) *value)) { /* Remove leading white */
138 entry->value = strdup(value);
140 while((strlen(line) > 0) && isspace((int) line[strlen(line) - 1])) {
141 line[strlen(line) - 1] = '\0'; /* Remove trailing white */
144 /* FIXME: strdup is not ANSI C compliant. */
145 entry->key = strdup(line);
147 /* Add to head of list */
149 entry->next = cfg_info->entries;
151 if (cfg_info->entries != NULL) {
152 cfg_info->entries->prev = entry;
155 cfg_info->entries = entry;
158 // fprintf(stderr, "Adding key/value %s/%s\n", entry->key, entry->value);
160 /* Go on to next line */
164 /* Line not part of any heading */
165 fprintf(stderr, "Orphaned line: %s\n", line);
168 /* Return pointer to configuration information */
172 /* Write configuration information to a config file */
174 static int CFG_WriteFile(struct CFG_Header *cfg, char *filename)
176 /* Not implemented - tricky to do and preserve comments */
182 * Find the value of a key in a config file. Return value associated
183 * with key or NULL if no such key exists.
186 char *CFG_Get(struct CFG_Header *cfg, char *section, char *key)
188 struct CFG_Header *h;
191 if ((cfg == NULL) || (section == NULL) || (key == NULL)) {
195 /* Search for section name */
196 for (h = cfg; h != NULL; h = h->next) {
197 if (strcmp(section, h->section) == 0) {
198 /* Search for key within section */
199 for (e = h->entries; e != NULL; e = e->next) {
200 if (strcmp(key, e->key) == 0) {
207 /* Key not found in section */
211 /* Set the value of a key in a config file. Return the new value if
212 the section/key can be found, else return NULL. */
214 static char *CFG_Set(struct CFG_Header *cfg, char *section, char *key,
217 struct CFG_Header *h;
220 if ((cfg == NULL) || (section == NULL) || (key == NULL) ||
225 /* Search for section name */
226 for (h = cfg; h != NULL; h = h->next) {
227 if (strcmp(section, h->section) == 0) {
228 /* Search for key within section */
229 for (e = h->entries; e != NULL; e = e->next) {
230 if ((e->key != NULL) && strcmp(key, e->key) == 0) {
231 /* Found - set value */
233 /* FIXME: strdup is not ANSI C compliant. */
234 e->key = strdup(value);
240 /* Key not found in section */
244 struct CFG_Header *CFG_FindGnokiirc()
246 struct CFG_Header *cfg_info;
251 homedir = getenv("HOMEDRIVE");
252 strncpy(rcfile, homedir ? homedir : "", 200);
253 homedir = getenv("HOMEPATH");
254 strncat(rcfile, homedir ? homedir : "", 200);
255 strncat(rcfile, "\gnokiirc", 200);
257 homedir = getenv("HOME");
258 if (homedir) strncpy(rcfile, homedir, 200);
259 strncat(rcfile, "/.gnokiirc", 200);
262 /* Try opening .gnokirc from users home directory first */
263 if ((cfg_info = CFG_ReadFile(rcfile)) == NULL) {
266 /* It failed so try for /etc/gnokiirc */
267 if ((cfg_info = CFG_ReadFile("/etc/gnokiirc")) == NULL) {
268 /* That failed too so exit */
270 // fprintf(stderr, _("Couldn't open %s or /etc/gnokiirc. Using defaults...\n"), rcfile);
277 /* It failed so try for gnokiirc */
278 if ((cfg_info = CFG_ReadFile("gnokiirc")) == NULL) {
279 /* That failed too so exit */
281 // fprintf(stderr, _("Couldn't open %s or gnokiirc. Using defaults...\n"), rcfile);
294 int CFG_ReadConfig(char **model, char **port, char **initlength,
295 char **connection, char **bindir,
300 *model=strdup(CONFIG_CFG_MODEL);
301 *port=strdup(CONFIG_CFG_PORT);
302 *initlength=strdup(CONFIG_CFG_INITLENGTH);
303 *connection=strdup("fbus");
307 struct CFG_Header *cfg_info;
309 char *DefaultPort = "com2:";
311 char *DefaultPort = "/dev/ttyS1";
313 char *DefaultModel = "auto";
314 char *DefaultConnection = "fbus";
315 char *DefaultBindir = "/usr/local/sbin/";
316 char *DefaultInitLength = "30";
318 char *section = "global";
320 (char *)*model = DefaultModel;
321 (char *)*port = DefaultPort;
322 (char *)*connection = DefaultConnection;
323 (char *)*bindir = DefaultBindir;
324 (char *)*initlength = DefaultInitLength;
326 cfg_info=CFG_FindGnokiirc();
327 if (cfg_info==NULL) return 0;
329 if (isgnokiid) (char *)section = "gnokiid";
331 (char *)*model = CFG_Get(cfg_info, section, "model");
332 if (!*model) (char *)*model = DefaultModel;
334 (char *)*port = CFG_Get(cfg_info, section, "port");
335 if (!*port) (char *)*port = DefaultPort;
337 (char *)*connection = CFG_Get(cfg_info, section, "connection");
338 if (!*connection) (char *)*connection = DefaultConnection;
340 (char *)*bindir = CFG_Get(cfg_info, section, "bindir");
341 if (!*bindir) (char *)*bindir = DefaultBindir;
343 (char *)*initlength = CFG_Get(cfg_info, section, "initlength");
344 if (!*initlength) (char *)*initlength = "default";