r17860: Let's commit the work down up to now on the new schema module.
[Samba.git] / source4 / param / generic.c
blobbed675be693ec279231f41af5ea63a2f653aac03
1 /*
2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jelmer Vernooij 2005
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "includes.h"
21 #include "dlinklist.h"
22 #include "param/param.h"
23 #include "system/filesys.h"
25 struct param_section *param_get_section(struct param_context *ctx, const char *name)
27 struct param_section *sect;
29 if (name == NULL)
30 name = GLOBAL_NAME;
32 for (sect = ctx->sections; sect; sect = sect->next) {
33 if (!strcasecmp_m(sect->name, name))
34 return sect;
37 return NULL;
40 struct param *param_section_get (struct param_section *section, const char *name)
42 struct param *p;
44 for (p = section->parameters; p; p = p->next) {
45 if (strcasecmp_m(p->name, name) == 0)
46 return p;
49 return NULL;
52 struct param *param_get (struct param_context *ctx, const char *section_name, const char *name)
54 struct param_section *section = param_get_section(ctx, section_name);
55 if (section == NULL)
56 return NULL;
58 return param_section_get(section, name);
61 /* Look up parameter. If it is not found, add it */
62 static struct param *param_get_add(struct param_context *ctx, const char *section_name, const char *name)
64 struct param_section *section;
65 struct param *p;
67 section = param_get_section(ctx, section_name);
69 if (section == NULL) {
70 section = talloc_zero(ctx, struct param_section);
71 if (section == NULL)
72 return NULL;
74 section->name = talloc_strdup(section, section_name);
75 DLIST_ADD(ctx->sections, section);
78 p = param_section_get(section, name);
79 if (p == NULL) {
80 p = talloc_zero(section, struct param);
81 if (p == NULL)
82 return NULL;
84 p->name = talloc_strdup(p, name);
85 DLIST_ADD(section->parameters, p);
88 return p;
91 const char *param_get_string(struct param_context *ctx, const char *section, const char *param)
93 struct param *p = param_get(ctx, section, param);
95 if (p == NULL)
96 return NULL;
98 return p->value;
101 int param_set_string(struct param_context *ctx, const char *section, const char *param, const char *value)
103 struct param *p = param_get_add(ctx, section, param);
105 if (p == NULL)
106 return -1;
108 p->value = talloc_strdup(p, value);
110 return 0;
113 const char **param_get_string_list(struct param_context *ctx, const char *section, const char *param,
114 const char *separator)
116 struct param *p = param_get(ctx, section, param);
118 if (p == NULL)
119 return NULL;
121 if (separator == NULL)
122 separator = LIST_SEP;
124 if (p->list_value == NULL) {
125 p->list_value = str_list_make(ctx, p->value, separator);
128 return p->list_value;
131 int param_set_string_list(struct param_context *ctx, const char *section, const char *param, const char **list)
133 struct param *p = param_get_add(ctx, section, param);
135 p->value = str_list_join(p, list, ' ');
136 p->list_value = str_list_copy(p, list);
138 return 0;
141 int param_get_int(struct param_context *ctx, const char *section, const char *param, int default_v)
143 const char *value = param_get_string(ctx, section, param);
145 if (value)
146 return strtol(value, NULL, 0);
148 return default_v;
151 void param_set_int(struct param_context *ctx, const char *section, const char *param, int value)
153 struct param *p = param_get_add(ctx, section, param);
155 if (!p)
156 return;
158 p->value = talloc_asprintf(p, "%d", value);
161 unsigned long param_get_ulong(struct param_context *ctx, const char *section, const char *param, unsigned long default_v)
163 const char *value = param_get_string(ctx, section, param);
165 if (value)
166 return strtoul(value, NULL, 0);
168 return default_v;
171 void param_set_ulong(struct param_context *ctx, const char *section, const char *name, unsigned long value)
173 struct param *p = param_get_add(ctx, section, name);
175 if (!p)
176 return;
178 p->value = talloc_asprintf(p, "%lu", value);
181 static BOOL param_sfunc (const char *name, void *_ctx)
183 struct param_context *ctx = _ctx;
184 struct param_section *section = param_get_section(ctx, name);
186 if (section == NULL) {
187 section = talloc_zero(ctx, struct param_section);
188 if (section == NULL)
189 return False;
191 section->name = talloc_strdup(section, name);
193 DLIST_ADD(ctx->sections, section);
196 /* Make sure this section is on top of the list for param_pfunc */
197 DLIST_PROMOTE(ctx->sections, section);
199 return True;
202 static BOOL param_pfunc (const char *name, const char *value, void *_ctx)
204 struct param_context *ctx = _ctx;
205 struct param *p = param_section_get(ctx->sections, name);
207 if (!p) {
208 p = talloc_zero(ctx->sections, struct param);
209 if (p == NULL)
210 return False;
212 p->name = talloc_strdup(p, name);
213 p->value = talloc_strdup(p, value);
214 DLIST_ADD(ctx->sections->parameters, p);
215 } else { /* Replace current value */
216 talloc_free(p->value);
217 p->value = talloc_strdup(p, value);
220 return True;
223 struct param_context *param_init(TALLOC_CTX *mem_ctx)
225 return talloc_zero(mem_ctx, struct param_context);
229 int param_read(struct param_context *ctx, const char *fn)
231 ctx->sections = talloc_zero(ctx, struct param_section);
232 if (ctx->sections == NULL)
233 return -1;
235 ctx->sections->name = talloc_strdup(ctx->sections, "global");
236 if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
237 return -1;
240 return 0;
243 int param_write(struct param_context *ctx, const char *fn)
245 int file;
246 struct param_section *section;
248 if (fn == NULL || ctx == NULL)
249 return -1;
251 file = open(fn, O_WRONLY|O_CREAT, 0755);
253 if (file == -1)
254 return -1;
256 for (section = ctx->sections; section; section = section->next) {
257 struct param *param;
259 fdprintf(file, "[%s]\n", section->name);
260 for (param = section->parameters; param; param = param->next) {
261 fdprintf(file, "\t%s = %s\n", param->name, param->value);
263 fdprintf(file, "\n");
266 close(file);
268 return 0;