2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jelmer Vernooij 2005
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 3 of the License, or
8 * (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
20 #include "lib/util/dlinklist.h"
21 #include "param/param.h"
22 #include "system/filesys.h"
24 struct param_section *param_get_section(struct param_context *ctx, const char *name)
26 struct param_section *sect;
31 for (sect = ctx->sections; sect; sect = sect->next) {
32 if (!strcasecmp_m(sect->name, name))
39 struct param_opt *param_section_get(struct param_section *section,
44 for (p = section->parameters; p; p = p->next) {
45 if (strcasecmp_m(p->key, name) == 0)
52 struct param_opt *param_get (struct param_context *ctx, const char *name, const char *section_name)
54 struct param_section *section = param_get_section(ctx, section_name);
58 return param_section_get(section, name);
61 struct param_section *param_add_section(struct param_context *ctx, const char *section_name)
63 struct param_section *section;
64 section = talloc_zero(ctx, struct param_section);
68 section->name = talloc_strdup(section, section_name);
69 DLIST_ADD_END(ctx->sections, section, struct param_section *);
73 /* Look up parameter. If it is not found, add it */
74 struct param_opt *param_get_add(struct param_context *ctx, const char *name, const char *section_name)
76 struct param_section *section;
79 SMB_ASSERT(section_name != NULL);
80 SMB_ASSERT(name != NULL);
82 section = param_get_section(ctx, section_name);
84 if (section == NULL) {
85 section = param_add_section(ctx, section_name);
88 p = param_section_get(section, name);
90 p = talloc_zero(section, struct param_opt);
94 p->key = talloc_strdup(p, name);
95 DLIST_ADD_END(section->parameters, p, struct param_opt *);
101 const char *param_get_string(struct param_context *ctx, const char *param, const char *section)
103 struct param_opt *p = param_get(ctx, param, section);
111 int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section)
113 struct param_opt *p = param_get_add(ctx, param, section);
118 p->value = talloc_strdup(p, value);
123 const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section)
125 struct param_opt *p = param_get(ctx, param, section);
130 if (separator == NULL)
131 separator = LIST_SEP;
133 return str_list_make(ctx, p->value, separator);
136 int param_set_string_list(struct param_context *ctx, const char *param, const char **list, const char *section)
138 struct param_opt *p = param_get_add(ctx, param, section);
140 p->value = str_list_join(p, list, ' ');
145 int param_get_int(struct param_context *ctx, const char *param, int default_v, const char *section)
147 const char *value = param_get_string(ctx, param, section);
150 return strtol(value, NULL, 0);
155 void param_set_int(struct param_context *ctx, const char *param, int value, const char *section)
157 struct param_opt *p = param_get_add(ctx, section, param);
162 p->value = talloc_asprintf(p, "%d", value);
165 unsigned long param_get_ulong(struct param_context *ctx, const char *param, unsigned long default_v, const char *section)
167 const char *value = param_get_string(ctx, param, section);
170 return strtoul(value, NULL, 0);
175 void param_set_ulong(struct param_context *ctx, const char *name, unsigned long value, const char *section)
177 struct param_opt *p = param_get_add(ctx, name, section);
182 p->value = talloc_asprintf(p, "%lu", value);
185 static bool param_sfunc (const char *name, void *_ctx)
187 struct param_context *ctx = (struct param_context *)_ctx;
188 struct param_section *section = param_get_section(ctx, name);
190 if (section == NULL) {
191 section = talloc_zero(ctx, struct param_section);
195 section->name = talloc_strdup(section, name);
197 DLIST_ADD_END(ctx->sections, section, struct param_section *);
200 /* Make sure this section is on top of the list for param_pfunc */
201 DLIST_PROMOTE(ctx->sections, section);
206 static bool param_pfunc (const char *name, const char *value, void *_ctx)
208 struct param_context *ctx = (struct param_context *)_ctx;
209 struct param_opt *p = param_section_get(ctx->sections, name);
212 p = talloc_zero(ctx->sections, struct param_opt);
216 p->key = talloc_strdup(p, name);
217 p->value = talloc_strdup(p, value);
218 DLIST_ADD(ctx->sections->parameters, p);
219 } else { /* Replace current value */
220 talloc_free(p->value);
221 p->value = talloc_strdup(p, value);
227 struct param_context *param_init(TALLOC_CTX *mem_ctx)
229 return talloc_zero(mem_ctx, struct param_context);
233 int param_read(struct param_context *ctx, const char *fn)
235 ctx->sections = talloc_zero(ctx, struct param_section);
236 if (ctx->sections == NULL)
239 ctx->sections->name = talloc_strdup(ctx->sections, "global");
240 if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
247 int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
249 struct param_section *section;
251 for (section = ctx->sections; section; section = section->next) {
252 struct param_opt *param;
253 bool isglobal = strcmp(section->name, "global") == 0;
254 for (param = section->parameters; param; param = param->next) {
256 lp_do_global_parameter(lp_ctx, param->key,
259 struct loadparm_service *service =
260 lp_service(lp_ctx, section->name);
262 service = lp_add_service(lp_ctx, lp_default_service(lp_ctx), section->name);
263 lp_do_service_parameter(lp_ctx, service, param->key, param->value);
270 int param_write(struct param_context *ctx, const char *fn)
273 struct param_section *section;
275 if (fn == NULL || ctx == NULL)
278 file = open(fn, O_WRONLY|O_CREAT, 0755);
283 for (section = ctx->sections; section; section = section->next) {
284 struct param_opt *param;
286 fdprintf(file, "[%s]\n", section->name);
287 for (param = section->parameters; param; param = param->next) {
288 fdprintf(file, "\t%s = %s\n", param->key, param->value);
290 fdprintf(file, "\n");