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 "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
;
32 for (sect
= ctx
->sections
; sect
; sect
= sect
->next
) {
33 if (!strcasecmp_m(sect
->name
, name
))
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
);
52 section
->name
= talloc_strdup(section
, section_name
);
53 DLIST_ADD_END(ctx
->sections
, section
, struct param_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
);
74 p
= talloc_zero(section
, struct parmlist_entry
);
78 p
->key
= talloc_strdup(p
, name
);
79 DLIST_ADD_END(section
->parameters
->entries
, p
, struct parmlist_entry
*);
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
);
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
);
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
);
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
, ' ');
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
);
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
);
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
);
149 return strtoul(value
, NULL
, 0);
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
);
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
);
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
);
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
);
191 p
= talloc_zero(ctx
->sections
, struct parmlist_entry
);
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
);
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
)
218 ctx
->sections
->name
= talloc_strdup(ctx
->sections
, "global");
219 if (!pm_process( fn
, param_sfunc
, param_pfunc
, ctx
)) {
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
) {
235 lp_do_global_parameter(lp_ctx
, param
->key
,
238 struct loadparm_service
*service
=
239 lp_service(lp_ctx
, section
->name
);
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
);
249 int param_write(struct param_context
*ctx
, const char *fn
)
252 struct param_section
*section
;
254 if (fn
== NULL
|| ctx
== NULL
)
257 file
= open(fn
, O_WRONLY
|O_CREAT
, 0755);
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");