packaging(RHEL-CTDB):makerpms.sh: replace source/ by source3/
[Samba/bb.git] / source4 / param / generic.c
blob41d01de9d38ca67c63bec995ad7b262a43156a63
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 3 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, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include "../lib/util/dlinklist.h"
21 #include "param/param.h"
22 #include "param/loadparm.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 parmlist_entry *param_section_get(struct param_section *section, const char *name)
42 return parmlist_get(section->parameters, name);
45 struct param_section *param_add_section(struct param_context *ctx, const char *section_name)
47 struct param_section *section;
48 section = talloc_zero(ctx, struct param_section);
49 if (section == NULL)
50 return NULL;
52 section->name = talloc_strdup(section, section_name);
53 DLIST_ADD_END(ctx->sections, section, struct param_section *);
54 return section;
57 /* Look up parameter. If it is not found, add it */
58 struct parmlist_entry *param_get_add(struct param_context *ctx, const char *name, const char *section_name)
60 struct param_section *section;
61 struct parmlist_entry *p;
63 SMB_ASSERT(section_name != NULL);
64 SMB_ASSERT(name != NULL);
66 section = param_get_section(ctx, section_name);
68 if (section == NULL) {
69 section = param_add_section(ctx, section_name);
72 p = param_section_get(section, name);
73 if (p == NULL) {
74 p = talloc_zero(section, struct parmlist_entry);
75 if (p == NULL)
76 return NULL;
78 p->key = talloc_strdup(p, name);
79 DLIST_ADD_END(section->parameters->entries, p, struct parmlist_entry *);
82 return p;
85 const char *param_get_string(struct param_context *ctx, const char *param, const char *section_name)
87 struct param_section *section = param_get_section(ctx, section_name);
89 if (section == NULL)
90 return NULL;
92 return parmlist_get_string(section->parameters, param, NULL);
95 int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section_name)
97 struct param_section *section = param_get_section(ctx, section_name);
99 if (section == NULL)
100 return -1;
102 return parmlist_set_string(section->parameters, param, value);
105 const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section_name)
107 struct param_section *section = param_get_section(ctx, section_name);
109 if (section == NULL)
110 return NULL;
112 return parmlist_get_string_list(section->parameters, param, separator);
115 int param_set_string_list(struct param_context *ctx, const char *param, const char **list, const char *section)
117 struct parmlist_entry *p = param_get_add(ctx, param, section);
119 p->value = str_list_join(p, list, ' ');
121 return 0;
124 int param_get_int(struct param_context *ctx, const char *param, int default_v, const char *section_name)
126 struct param_section *section = param_get_section(ctx, section_name);
128 if (section == NULL)
129 return default_v;
131 return parmlist_get_int(section->parameters, param, default_v);
134 void param_set_int(struct param_context *ctx, const char *param, int value, const char *section)
136 struct parmlist_entry *p = param_get_add(ctx, section, param);
138 if (!p)
139 return;
141 p->value = talloc_asprintf(p, "%d", value);
144 unsigned long param_get_ulong(struct param_context *ctx, const char *param, unsigned long default_v, const char *section)
146 const char *value = param_get_string(ctx, param, section);
148 if (value)
149 return strtoul(value, NULL, 0);
151 return default_v;
154 void param_set_ulong(struct param_context *ctx, const char *name, unsigned long value, const char *section)
156 struct parmlist_entry *p = param_get_add(ctx, name, section);
158 if (!p)
159 return;
161 p->value = talloc_asprintf(p, "%lu", value);
164 static bool param_sfunc (const char *name, void *_ctx)
166 struct param_context *ctx = (struct param_context *)_ctx;
167 struct param_section *section = param_get_section(ctx, name);
169 if (section == NULL) {
170 section = talloc_zero(ctx, struct param_section);
171 if (section == NULL)
172 return false;
174 section->name = talloc_strdup(section, name);
176 DLIST_ADD_END(ctx->sections, section, struct param_section *);
179 /* Make sure this section is on top of the list for param_pfunc */
180 DLIST_PROMOTE(ctx->sections, section);
182 return true;
185 static bool param_pfunc (const char *name, const char *value, void *_ctx)
187 struct param_context *ctx = (struct param_context *)_ctx;
188 struct parmlist_entry *p = param_section_get(ctx->sections, name);
190 if (!p) {
191 p = talloc_zero(ctx->sections, struct parmlist_entry);
192 if (p == NULL)
193 return false;
195 p->key = talloc_strdup(p, name);
196 p->value = talloc_strdup(p, value);
197 DLIST_ADD(ctx->sections->parameters->entries, p);
198 } else { /* Replace current value */
199 talloc_free(p->value);
200 p->value = talloc_strdup(p, value);
203 return true;
206 struct param_context *param_init(TALLOC_CTX *mem_ctx)
208 return talloc_zero(mem_ctx, struct param_context);
212 int param_read(struct param_context *ctx, const char *fn)
214 ctx->sections = talloc_zero(ctx, struct param_section);
215 if (ctx->sections == NULL)
216 return -1;
218 ctx->sections->name = talloc_strdup(ctx->sections, "global");
219 if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
220 return -1;
223 return 0;
226 int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
228 struct param_section *section;
230 for (section = ctx->sections; section; section = section->next) {
231 struct parmlist_entry *param;
232 bool isglobal = strcmp(section->name, "global") == 0;
233 for (param = section->parameters->entries; param; param = param->next) {
234 if (isglobal)
235 lp_do_global_parameter(lp_ctx, param->key,
236 param->value);
237 else {
238 struct loadparm_service *service =
239 lp_service(lp_ctx, section->name);
240 if (service == NULL)
241 service = lp_add_service(lp_ctx, lp_default_service(lp_ctx), section->name);
242 lp_do_service_parameter(lp_ctx, service, param->key, param->value);
246 return 0;
249 int param_write(struct param_context *ctx, const char *fn)
251 int file;
252 struct param_section *section;
254 if (fn == NULL || ctx == NULL)
255 return -1;
257 file = open(fn, O_WRONLY|O_CREAT, 0755);
259 if (file == -1)
260 return -1;
262 for (section = ctx->sections; section; section = section->next) {
263 struct parmlist_entry *param;
265 fdprintf(file, "[%s]\n", section->name);
266 for (param = section->parameters->entries; param; param = param->next) {
267 fdprintf(file, "\t%s = %s\n", param->key, param->value);
269 fdprintf(file, "\n");
272 close(file);
274 return 0;