File: config.c

package info (click to toggle)
shishi 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 22,144 kB
  • ctags: 7,865
  • sloc: ansic: 61,153; sh: 12,212; yacc: 1,219; makefile: 1,164; perl: 624; sed: 16
file content (197 lines) | stat: -rw-r--r-- 4,578 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* cfg.c --- Read Shisa Configuration file.
 * Copyright (C) 2002, 2003, 2004, 2007, 2008, 2010  Simon Josefsson
 *
 * This file is part of Shishi.
 *
 * Shishi is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Shishi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Shishi; if not, see http://www.gnu.org/licenses or write
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA
 *
 */

#include "info.h"

/**
 * shisa_cfg_db:
 * @dbh: Shisa library handle created by shisa().
 * @value: string with database definition.
 *
 * Setup and open a new database.  The syntax of the @value parameter
 * is "TYPE[ LOCATION[ PARAMETER]]", where TYPE is one of the
 * supported database types (e.g., "file") and LOCATION and PARAMETER
 * are optional strings passed to the database during initialization.
 * Neither TYPE nor LOCATION can contain " " (SPC), but PARAMETER may.
 *
 * Return Value: Returns %SHISA_OK if database was parsed and open
 *   successfully.
 **/
int
shisa_cfg_db (Shisa * dbh, const char *value)
{
  char *p;
  char *db;
  char *location = NULL;
  char *options = NULL;
  _Shisa_backend *backend;
  void *state;
  int rc;

  db = xstrdup (value);
  if ((p = strchr (db, ' ')))
    {
      *p++ = '\0';
      location = p;
      if ((p = strchr (p, ' ')))
	{
	  *p++ = '\0';
	  options = p;
	}
    }

  backend = _shisa_find_backend (db);
  if (backend == NULL)
    {
      shisa_info (dbh, "Unknown database type: `%s'.", db);
      free (db);
      return SHISA_CFG_SYNTAX_ERROR;
    }

  rc = backend->init (dbh, location, options, &state);
  if (rc != SHISA_OK)
    {
      shisa_info (dbh, "Cannot initialize `%s' database backend.\n"
		  "Location `%s' and options `%s'.", db,
		  location ? location : "N/A", options ? options : "N/A");
      free (db);
      return rc;
    }
  free (db);

  dbh->dbs = xrealloc (dbh->dbs, ++dbh->ndbs * sizeof (*dbh->dbs));
  dbh->dbs->backend = backend;
  dbh->dbs->state = state;

  return SHISA_OK;
}

/**
 * shisa_cfg:
 * @dbh: Shisa library handle created by shisa().
 * @option: string with shisa library option.
 *
 * Configure shisa library with given option.
 *
 * Return Value: Returns SHISA_OK if option was valid.
 **/
int
shisa_cfg (Shisa * dbh, const char *option)
{
  int rc;

  if (!option)
    return SHISA_OK;

  if (strncmp (option, "db=", 3) != 0 && strncmp (option, "db ", 3) != 0)
    {
      shisa_info (dbh, "Unknown option: `%s'.", option);
      return SHISA_CFG_SYNTAX_ERROR;
    }

  rc = shisa_cfg_db (dbh, option + 3);
  if (rc != SHISA_OK)
    return rc;

  return SHISA_OK;
}

/**
 * shisa_cfg_from_file:
 * @dbh: Shisa library handle created by shisa().
 * @cfg: filename to read configuration from.
 *
 * Configure shisa library using configuration file.
 *
 * Return Value: Returns %SHISA_OK iff successful.
 **/
int
shisa_cfg_from_file (Shisa * dbh, const char *cfg)
{
  char *line = NULL;
  size_t len = 0;
  FILE *fh;
  int rc = SHISA_OK;

  if (cfg == NULL)
    return SHISA_OK;

  fh = fopen (cfg, "r");
  if (fh == NULL)
    {
      perror (cfg);
      return SHISA_CFG_NO_FILE;
    }

  while (!feof (fh))
    {
      ssize_t n = getline (&line, &len, fh);
      char *p = line;
      char *q;

      if (n <= 0)
	/* End of file or error.  */
	break;

      while (strlen (p) > 0 && (p[strlen (p) - 1] == '\n' ||
				p[strlen (p) - 1] == '\r'))
	p[strlen (p) - 1] = '\0';

      while (*p && strchr (" \t\r\n", *p))
	p++;

      if (*p == '\0' || *p == '#')
	continue;

      q = strchr (p, ' ');
      if (q && (strchr (p, '=') == NULL || q < strchr (p, '=')))
	*q = '=';

      rc = shisa_cfg (dbh, p);
      if (rc != SHISA_OK)
	break;
    }

  free (line);

  if (ferror (fh))
    if (rc == SHISA_OK)
      return SHISA_CFG_IO_ERROR;

  if (fclose (fh) != 0)
    if (rc == SHISA_OK)
      return SHISA_CFG_IO_ERROR;

  return rc;
}

/**
 * shisa_cfg_default_systemfile:
 * @dbh: Shisa library handle created by shisa().
 *
 * Return value: Return system configuration filename.
 **/
const char *
shisa_cfg_default_systemfile (Shisa * dbh)
{
  return SYSTEMCFGFILE;
}