Add.
[shishi.git] / db / config.c
blob3648e8b233772545e1b43f24e5bd8507516fba0b
1 /* cfg.c --- Read Shisa Configuration file.
2 * Copyright (C) 2002, 2003, 2004 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "info.h"
24 enum
26 DB_OPTION = 0,
27 THE_END
30 static const char *_shisa_opts[] = {
31 /* [DB_OPTION] = */ "db",
32 /* [THE_END] = */ NULL
35 /**
36 * shisa_cfg_db:
37 * @dbh: Shisa library handle created by shisa().
38 * @value: string with database definition.
40 * Setup and open a new database. The syntax of the @value parameter
41 * is "TYPE[ LOCATION[ PARAMETER]]", where TYPE is one of the
42 * supported database types (e.g., "file") and LOCATION and PARAMETER
43 * are optional strings passed to the database during initialization.
44 * Neither TYPE nor LOCATION can contain " " (SPC), but PARAMETER may.
46 * Return Value: Returns %SHISA_OK if database was parsed and open
47 * successfully.
48 **/
49 int
50 shisa_cfg_db (Shisa * dbh, const char *value)
52 char *p;
53 char *db;
54 char *location = NULL;
55 char *options = NULL;
56 _Shisa_backend *backend;
57 void *state;
58 int rc;
60 db = xstrdup (value);
61 if ((p = strchr (db, ' ')))
63 *p++ = '\0';
64 location = p;
65 if ((p = strchr (p, ' ')))
67 *p++ = '\0';
68 options = p;
72 backend = _shisa_find_backend (db);
73 if (backend == NULL)
75 shisa_info (dbh, "Unknown database type: `%s'.", db);
76 free (db);
77 return SHISA_CFG_SYNTAX_ERROR;
80 rc = backend->init (dbh, location, options, &state);
81 if (rc != SHISA_OK)
83 shisa_info (dbh, "Cannot initialize `%s' database backend.\n"
84 "Location `%s' and options `%s'.", db,
85 location ? location : "N/A", options ? options : "N/A");
86 free (db);
87 return rc;
89 free (db);
91 dbh->dbs = xrealloc (dbh->dbs, ++dbh->ndbs * sizeof (*dbh->dbs));
92 dbh->dbs->backend = backend;
93 dbh->dbs->state = state;
95 return SHISA_OK;
98 /**
99 * shisa_cfg:
100 * @dbh: Shisa library handle created by shisa().
101 * @option: string with shisa library option.
103 * Configure shisa library with given option.
105 * Return Value: Returns SHISA_OK if option was valid.
108 shisa_cfg (Shisa * dbh, char *option)
110 int rc;
112 if (!option)
113 return SHISA_OK;
115 if (strncmp (option, "db=", 3) != 0 && strncmp (option, "db ", 3) != 0)
117 shisa_info (dbh, "Unknown option: `%s'.", option);
118 return SHISA_CFG_SYNTAX_ERROR;
121 rc = shisa_cfg_db (dbh, option + 3);
122 if (rc != SHISA_OK)
123 return rc;
125 return SHISA_OK;
129 * shisa_cfg_from_file:
130 * @dbh: Shisa library handle created by shisa().
131 * @cfg: filename to read configuration from.
133 * Configure shisa library using configuration file.
135 * Return Value: Returns %SHISA_OK iff succesful.
138 shisa_cfg_from_file (Shisa * dbh, const char *cfg)
140 char *line = NULL;
141 size_t len = 0;
142 FILE *fh;
143 int rc = SHISA_OK;
145 if (cfg == NULL)
146 return SHISA_OK;
148 fh = fopen (cfg, "r");
149 if (fh == NULL)
151 perror (cfg);
152 return SHISA_CFG_NO_FILE;
155 while (!feof (fh))
157 ssize_t n = getline (&line, &len, fh);
158 char *p = line;
159 char *q;
161 if (n <= 0)
162 /* End of file or error. */
163 break;
165 while (strlen (p) > 0 && (p[strlen (p) - 1] == '\n' ||
166 p[strlen (p) - 1] == '\r'))
167 p[strlen (p) - 1] = '\0';
169 while (*p && strchr (" \t\r\n", *p))
170 p++;
172 if (*p == '\0' || *p == '#')
173 continue;
175 q = strchr (p, ' ');
176 if (q && (strchr (p, '=') == NULL || q < strchr (p, '=')))
177 *q = '=';
179 rc = shisa_cfg (dbh, p);
180 if (rc != SHISA_OK)
181 break;
184 if (line)
185 free (line);
187 if (ferror (fh))
188 if (rc == SHISA_OK)
189 return SHISA_CFG_IO_ERROR;
191 if (fclose (fh) != 0)
192 if (rc == SHISA_OK)
193 return SHISA_CFG_IO_ERROR;
195 return rc;
199 * shisa_cfg_default_systemfile:
200 * @dbh: Shisa library handle created by shisa().
202 * Return value: Return system configuration filename.
204 const char *
205 shisa_cfg_default_systemfile (Shisa * dbh)
207 return SYSTEMCFGFILE;