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
30 static const char *_shisa_opts
[] = {
31 /* [DB_OPTION] = */ "db",
32 /* [THE_END] = */ NULL
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
50 shisa_cfg_db (Shisa
* dbh
, const char *value
)
54 char *location
= NULL
;
56 _Shisa_backend
*backend
;
61 if ((p
= strchr (db
, ' ')))
65 if ((p
= strchr (p
, ' ')))
72 backend
= _shisa_find_backend (db
);
75 shisa_info (dbh
, "Unknown database type: `%s'.", db
);
77 return SHISA_CFG_SYNTAX_ERROR
;
80 rc
= backend
->init (dbh
, location
, options
, &state
);
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");
91 dbh
->dbs
= xrealloc (dbh
->dbs
, ++dbh
->ndbs
* sizeof (*dbh
->dbs
));
92 dbh
->dbs
->backend
= backend
;
93 dbh
->dbs
->state
= state
;
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
)
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);
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
)
148 fh
= fopen (cfg
, "r");
152 return SHISA_CFG_NO_FILE
;
157 ssize_t n
= getline (&line
, &len
, fh
);
162 /* End of file or error. */
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
))
172 if (*p
== '\0' || *p
== '#')
176 if (q
&& (strchr (p
, '=') == NULL
|| q
< strchr (p
, '=')))
179 rc
= shisa_cfg (dbh
, p
);
189 return SHISA_CFG_IO_ERROR
;
191 if (fclose (fh
) != 0)
193 return SHISA_CFG_IO_ERROR
;
199 * shisa_cfg_default_systemfile:
200 * @dbh: Shisa library handle created by shisa().
202 * Return value: Return system configuration filename.
205 shisa_cfg_default_systemfile (Shisa
* dbh
)
207 return SYSTEMCFGFILE
;