7 A Linux/Unix toolset and driver for Nokia mobile phones.
9 Copyright (C) 1999, 2000 Hugh Blemings & Pavel JanÃk ml.
11 Released under the terms of the GNU GPL, see file COPYING for more details.
13 Config file (/etc/gnokiirc and ~/.gnokiirc) reader.
15 Modified from code by Tim Potter.
18 Revision 1.1.1.1 2001/11/25 21:58:58 short
19 :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
21 Revision 1.16 2001/11/14 10:46:12 pkot
22 Small cleanup with __unices__
24 Revision 1.15 2001/06/10 11:24:57 machek
25 Kill "slash star" inside comment.
27 Revision 1.14 2001/03/19 23:43:45 pkot
28 Solaris/ *BSD '#if defined' cleanup
30 Revision 1.13 2001/03/13 01:21:38 pkot
31 *BSD updates (Bert Driehuis)
33 Revision 1.12 2001/01/08 15:11:36 pkot
34 Documentation updates.
35 Fixed some bugs and removed FIXMEs.
36 We need to move some stuff from configure.in to aclocal.m4
38 Revision 1.11 2001/01/02 09:09:07 pkot
39 Misc fixes and updates.
41 Revision 1.10 2000/12/19 16:18:15 pkot
42 configure script updates and added shared function for configfile reading
55 #include "cfgreader.h"
57 /* Read configuration information from a ".INI" style file */
58 struct CFG_Header *CFG_ReadFile(char *filename)
63 struct CFG_Header *cfg_info = NULL, *cfg_head = NULL;
66 if (filename == NULL) {
71 if ((buf = (char *)malloc(255)) == NULL) {
76 if ((handle = fopen(filename, "r")) == NULL) {
78 fprintf( stderr, "CFG_ReadFile - open %s: %s\n", filename, strerror(errno));
84 fprintf( stderr, "Opened configuration file %s\n", filename );
87 /* Iterate over lines in the file */
88 while (fgets(buf, 255, handle) != NULL) {
92 /* Strip leading, trailing whitespace */
93 while(isspace((int) *line))
96 while((strlen(line) > 0) && isspace((int) line[strlen(line) - 1]))
97 line[strlen(line) - 1] = '\0';
99 /* Ignore blank lines and comments */
100 if ((*line == '\n') || (*line == '\0') || (*line == '#'))
103 /* Look for "headings" enclosed in square brackets */
104 if ((line[0] == '[') && (line[strlen(line) - 1] == ']')) {
105 struct CFG_Header *heading;
107 /* Allocate new heading entry */
108 if ((heading = (struct CFG_Header *)malloc(sizeof(*heading))) == NULL) {
113 memset(heading, '\0', sizeof(*heading));
116 line[strlen(line) - 1] = '\0';
118 /* FIXME: strdup is not ANSI C compliant. */
119 heading->section = strdup(line);
121 /* Add to tail of list */
122 heading->prev = cfg_info;
124 if (cfg_info != NULL) {
125 cfg_info->next = heading;
127 /* Store copy of head of list for return value */
134 fprintf(stderr, "Added new section %s\n", heading->section);
136 /* Go on to next line */
141 /* Process key/value line */
143 if ((strchr(line, '=') != NULL) && cfg_info != NULL) {
144 struct CFG_Entry *entry;
147 /* Allocate new entry */
148 if ((entry = (struct CFG_Entry *)malloc(sizeof(*entry))) == NULL) {
153 memset(entry, '\0', sizeof(*entry));
155 value = strchr(line, '=');
156 *value = '\0'; /* Split string */
159 while(isspace((int) *value)) { /* Remove leading white */
163 entry->value = strdup(value);
165 while((strlen(line) > 0) && isspace((int) line[strlen(line) - 1])) {
166 line[strlen(line) - 1] = '\0'; /* Remove trailing white */
169 /* FIXME: strdup is not ANSI C compliant. */
170 entry->key = strdup(line);
172 /* Add to head of list */
174 entry->next = cfg_info->entries;
176 if (cfg_info->entries != NULL) {
177 cfg_info->entries->prev = entry;
180 cfg_info->entries = entry;
183 fprintf(stderr, "Adding key/value %s/%s\n", entry->key, entry->value);
185 /* Go on to next line */
189 /* Line not part of any heading */
190 fprintf(stderr, "Orphaned line: %s\n", line);
193 /* Return pointer to configuration information */
197 /* Write configuration information to a config file */
199 int CFG_WriteFile(struct CFG_Header *cfg, char *filename)
201 /* Not implemented - tricky to do and preserve comments */
207 * Find the value of a key in a config file. Return value associated
208 * with key or NULL if no such key exists.
211 char *CFG_Get(struct CFG_Header *cfg, char *section, char *key)
213 struct CFG_Header *h;
216 if ((cfg == NULL) || (section == NULL) || (key == NULL)) {
220 /* Search for section name */
221 for (h = cfg; h != NULL; h = h->next) {
222 if (strcmp(section, h->section) == 0) {
223 /* Search for key within section */
224 for (e = h->entries; e != NULL; e = e->next) {
225 if (strcmp(key, e->key) == 0) {
232 /* Key not found in section */
236 /* Set the value of a key in a config file. Return the new value if
237 the section/key can be found, else return NULL. */
239 char *CFG_Set(struct CFG_Header *cfg, char *section, char *key,
242 struct CFG_Header *h;
245 if ((cfg == NULL) || (section == NULL) || (key == NULL) ||
250 /* Search for section name */
251 for (h = cfg; h != NULL; h = h->next) {
252 if (strcmp(section, h->section) == 0) {
253 /* Search for key within section */
254 for (e = h->entries; e != NULL; e = e->next) {
255 if ((e->key != NULL) && strcmp(key, e->key) == 0) {
256 /* Found - set value */
258 /* FIXME: strdup is not ANSI C compliant. */
259 e->key = strdup(value);
265 /* Key not found in section */
269 int readconfig(char **model, char **port, char **initlength,
270 char **connection, char **bindir)
272 struct CFG_Header *cfg_info;
275 char *DefaultConnection = "serial";
276 char *DefaultBindir = "/usr/local/sbin/";
279 homedir = getenv("HOMEDRIVE");
280 strncpy(rcfile, homedir ? homedir : "", 200);
281 homedir = getenv("HOMEPATH");
282 strncat(rcfile, homedir ? homedir : "", 200);
283 strncat(rcfile, "\\_gnokiirc", 200);
285 homedir = getenv("HOME");
286 if (homedir) strncpy(rcfile, homedir, 200);
287 strncat(rcfile, "/.gnokiirc", 200);
290 /* Try opening .gnokirc from users home directory first */
291 if ((cfg_info = CFG_ReadFile(rcfile)) == NULL) {
292 /* It failed so try for /etc/gnokiirc */
293 if ((cfg_info = CFG_ReadFile("/etc/gnokiirc")) == NULL) {
294 /* That failed too so exit */
295 fprintf(stderr, _("Couldn't open %s or /etc/gnokiirc. Exiting now...\n"), rcfile);
300 (char *)*model = CFG_Get(cfg_info, "global", "model");
302 fprintf(stderr, _("Config error - no model specified. Exiting now...\n"));
306 (char *)*port = CFG_Get(cfg_info, "global", "port");
308 fprintf(stderr, _("Config error - no port specified. Exiting now...\n"));
312 (char *)*initlength = CFG_Get(cfg_info, "global", "initlength");
313 if (!*initlength) (char *)*initlength = "default";
315 (char *)*connection = CFG_Get(cfg_info, "global", "connection");
316 if (!*connection) (char *)*connection = DefaultConnection;
318 (char *)*bindir = CFG_Get(cfg_info, "global", "bindir");
319 if (!*bindir) (char *)*bindir = DefaultBindir;