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 "../lib/util/parmlist.h"
22 #include "param/param.h"
23 #include "param/loadparm.h"
24 #include "system/filesys.h"
26 struct param_section
*param_get_section(struct param_context
*ctx
, const char *name
)
28 struct param_section
*sect
;
33 for (sect
= ctx
->sections
; sect
; sect
= sect
->next
) {
34 if (!strcasecmp_m(sect
->name
, name
))
41 struct parmlist_entry
*param_section_get(struct param_section
*section
, const char *name
)
43 return parmlist_get(section
->parameters
, name
);
46 struct param_section
*param_add_section(struct param_context
*ctx
, const char *section_name
)
48 struct param_section
*section
;
49 section
= talloc_zero(ctx
, struct param_section
);
53 section
->name
= talloc_strdup(section
, section_name
);
54 DLIST_ADD_END(ctx
->sections
, section
, struct param_section
*);
58 /* Look up parameter. If it is not found, add it */
59 struct parmlist_entry
*param_get_add(struct param_context
*ctx
, const char *name
, const char *section_name
)
61 struct param_section
*section
;
62 struct parmlist_entry
*p
;
64 SMB_ASSERT(section_name
!= NULL
);
65 SMB_ASSERT(name
!= NULL
);
67 section
= param_get_section(ctx
, section_name
);
69 if (section
== NULL
) {
70 section
= param_add_section(ctx
, section_name
);
73 p
= param_section_get(section
, name
);
75 p
= talloc_zero(section
, struct parmlist_entry
);
79 p
->key
= talloc_strdup(p
, name
);
80 DLIST_ADD_END(section
->parameters
->entries
, p
, struct parmlist_entry
*);
86 const char *param_get_string(struct param_context
*ctx
, const char *param
, const char *section_name
)
88 struct param_section
*section
= param_get_section(ctx
, section_name
);
93 return parmlist_get_string(section
->parameters
, param
, NULL
);
96 int param_set_string(struct param_context
*ctx
, const char *param
, const char *value
, const char *section_name
)
98 struct param_section
*section
= param_get_section(ctx
, section_name
);
103 return parmlist_set_string(section
->parameters
, param
, value
);
106 const char **param_get_string_list(struct param_context
*ctx
, const char *param
, const char *separator
, const char *section_name
)
108 struct param_section
*section
= param_get_section(ctx
, section_name
);
113 return parmlist_get_string_list(section
->parameters
, param
, separator
);
116 int param_set_string_list(struct param_context
*ctx
, const char *param
, const char **list
, const char *section
)
118 struct parmlist_entry
*p
= param_get_add(ctx
, param
, section
);
120 p
->value
= str_list_join(p
, list
, ' ');
125 int param_get_int(struct param_context
*ctx
, const char *param
, int default_v
, const char *section_name
)
127 struct param_section
*section
= param_get_section(ctx
, section_name
);
132 return parmlist_get_int(section
->parameters
, param
, default_v
);
135 void param_set_int(struct param_context
*ctx
, const char *param
, int value
, const char *section
)
137 struct parmlist_entry
*p
= param_get_add(ctx
, section
, param
);
142 p
->value
= talloc_asprintf(p
, "%d", value
);
145 unsigned long param_get_ulong(struct param_context
*ctx
, const char *param
, unsigned long default_v
, const char *section
)
147 const char *value
= param_get_string(ctx
, param
, section
);
150 return strtoul(value
, NULL
, 0);
155 void param_set_ulong(struct param_context
*ctx
, const char *name
, unsigned long value
, const char *section
)
157 struct parmlist_entry
*p
= param_get_add(ctx
, name
, section
);
162 p
->value
= talloc_asprintf(p
, "%lu", value
);
165 static bool param_sfunc (const char *name
, void *_ctx
)
167 struct param_context
*ctx
= (struct param_context
*)_ctx
;
168 struct param_section
*section
= param_get_section(ctx
, name
);
170 if (section
== NULL
) {
171 section
= talloc_zero(ctx
, struct param_section
);
175 section
->name
= talloc_strdup(section
, name
);
177 DLIST_ADD_END(ctx
->sections
, section
, struct param_section
*);
180 /* Make sure this section is on top of the list for param_pfunc */
181 DLIST_PROMOTE(ctx
->sections
, section
);
186 static bool param_pfunc (const char *name
, const char *value
, void *_ctx
)
188 struct param_context
*ctx
= (struct param_context
*)_ctx
;
189 struct parmlist_entry
*p
= param_section_get(ctx
->sections
, name
);
192 p
= talloc_zero(ctx
->sections
, struct parmlist_entry
);
196 p
->key
= talloc_strdup(p
, name
);
197 p
->value
= talloc_strdup(p
, value
);
198 DLIST_ADD(ctx
->sections
->parameters
->entries
, p
);
199 } else { /* Replace current value */
200 talloc_free(p
->value
);
201 p
->value
= talloc_strdup(p
, value
);
207 struct param_context
*param_init(TALLOC_CTX
*mem_ctx
)
209 return talloc_zero(mem_ctx
, struct param_context
);
213 int param_read(struct param_context
*ctx
, const char *fn
)
215 ctx
->sections
= talloc_zero(ctx
, struct param_section
);
216 if (ctx
->sections
== NULL
)
219 ctx
->sections
->name
= talloc_strdup(ctx
->sections
, "global");
220 if (!pm_process( fn
, param_sfunc
, param_pfunc
, ctx
)) {
227 int param_use(struct loadparm_context
*lp_ctx
, struct param_context
*ctx
)
229 struct param_section
*section
;
231 for (section
= ctx
->sections
; section
; section
= section
->next
) {
232 struct parmlist_entry
*param
;
233 bool isglobal
= strcmp(section
->name
, "global") == 0;
234 for (param
= section
->parameters
->entries
; param
; param
= param
->next
) {
236 lpcfg_do_global_parameter(lp_ctx
, param
->key
,
239 struct loadparm_service
*service
=
240 lpcfg_service(lp_ctx
, section
->name
);
242 service
= lpcfg_add_service(lp_ctx
, lpcfg_default_service(lp_ctx
), section
->name
);
243 lpcfg_do_service_parameter(lp_ctx
, service
, param
->key
, param
->value
);
250 int param_write(struct param_context
*ctx
, const char *fn
)
253 struct param_section
*section
;
255 if (fn
== NULL
|| ctx
== NULL
)
258 file
= open(fn
, O_WRONLY
|O_CREAT
, 0755);
263 for (section
= ctx
->sections
; section
; section
= section
->next
) {
264 struct parmlist_entry
*param
;
266 fdprintf(file
, "[%s]\n", section
->name
);
267 for (param
= section
->parameters
->entries
; param
; param
= param
->next
) {
268 fdprintf(file
, "\t%s = %s\n", param
->key
, param
->value
);
270 fdprintf(file
, "\n");