r25547: Convert to standard bool type.
[kai/samba.git] / source4 / param / generic.c
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.
14  *  
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/>.
17  */
18
19 #include "includes.h"
20 #include "lib/util/dlinklist.h"
21 #include "param/param.h"
22 #include "system/filesys.h"
23
24 struct param_section *param_get_section(struct param_context *ctx, const char *name)
25 {
26         struct param_section *sect;
27
28         if (name == NULL) 
29                 name = GLOBAL_NAME;
30
31         for (sect = ctx->sections; sect; sect = sect->next) {
32                 if (!strcasecmp_m(sect->name, name)) 
33                         return sect;
34         }
35
36         return NULL;
37 }
38
39 struct param_opt *param_section_get(struct param_section *section, 
40                                     const char *name)
41 {
42         struct param_opt *p;
43
44         for (p = section->parameters; p; p = p->next) {
45                 if (strcasecmp_m(p->key, name) == 0) 
46                         return p;
47         }
48
49         return NULL;
50 }
51
52 struct param_opt *param_get (struct param_context *ctx, const char *section_name, const char *name)
53 {
54         struct param_section *section = param_get_section(ctx, section_name);
55         if (section == NULL)
56                 return NULL;
57
58         return param_section_get(section, name);
59 }
60
61 /* Look up parameter. If it is not found, add it */
62 static struct param_opt *param_get_add(struct param_context *ctx, const char *section_name, const char *name)
63 {
64         struct param_section *section;
65         struct param_opt *p;
66
67         section = param_get_section(ctx, section_name);
68
69         if (section == NULL) {
70                 section = talloc_zero(ctx, struct param_section);
71                 if (section == NULL)
72                         return NULL;
73
74                 section->name = talloc_strdup(section, section_name);
75                 DLIST_ADD(ctx->sections, section);
76         }
77
78         p = param_section_get(section, name);
79         if (p == NULL) {
80                 p = talloc_zero(section, struct param_opt);
81                 if (p == NULL)
82                         return NULL;
83
84                 p->key = talloc_strdup(p, name);
85                 DLIST_ADD(section->parameters, p);
86         }
87         
88         return p;
89 }
90
91 const char *param_get_string(struct param_context *ctx, const char *section, const char *param)
92 {
93         struct param_opt *p = param_get(ctx, section, param);
94
95         if (p == NULL)
96                 return NULL;
97
98         return p->value;
99 }
100
101 int param_set_string(struct param_context *ctx, const char *section, const char *param, const char *value)
102 {
103         struct param_opt *p = param_get_add(ctx, section, param);
104
105         if (p == NULL)
106                 return -1;
107
108         p->value = talloc_strdup(p, value);
109
110         return 0;
111 }
112
113 const char **param_get_string_list(struct param_context *ctx, const char *section, const char *param,
114                                  const char *separator)
115 {
116         struct param_opt *p = param_get(ctx, section, param);
117         
118         if (p == NULL)
119                 return NULL;
120
121         if (separator == NULL)
122                 separator = LIST_SEP;
123         
124         return str_list_make(ctx, p->value, separator);
125 }
126
127 int param_set_string_list(struct param_context *ctx, const char *section, const char *param, const char **list)
128 {
129         struct param_opt *p = param_get_add(ctx, section, param);       
130
131         p->value = str_list_join(p, list, ' ');
132
133         return 0;
134 }
135
136 int param_get_int(struct param_context *ctx, const char *section, const char *param, int default_v)
137 {
138         const char *value = param_get_string(ctx, section, param);
139         
140         if (value)
141                 return strtol(value, NULL, 0); 
142
143         return default_v;
144 }
145
146 void param_set_int(struct param_context *ctx, const char *section, const char *param, int value)
147 {
148         struct param_opt *p = param_get_add(ctx, section, param);
149
150         if (!p) 
151                 return;
152
153         p->value = talloc_asprintf(p, "%d", value);
154 }
155
156 unsigned long param_get_ulong(struct param_context *ctx, const char *section, const char *param, unsigned long default_v)
157 {
158         const char *value = param_get_string(ctx, section, param);
159         
160         if (value)
161                 return strtoul(value, NULL, 0);
162
163         return default_v;
164 }
165
166 void param_set_ulong(struct param_context *ctx, const char *section, const char *name, unsigned long value)
167 {
168         struct param_opt *p = param_get_add(ctx, section, name);
169
170         if (!p)
171                 return;
172
173         p->value = talloc_asprintf(p, "%lu", value);
174 }
175
176 static bool param_sfunc (const char *name, void *_ctx)
177 {
178         struct param_context *ctx = (struct param_context *)_ctx;
179         struct param_section *section = param_get_section(ctx, name);
180
181         if (section == NULL) {
182                 section = talloc_zero(ctx, struct param_section);
183                 if (section == NULL)
184                         return false;
185
186                 section->name = talloc_strdup(section, name);
187
188                 DLIST_ADD(ctx->sections, section);
189         }
190
191         /* Make sure this section is on top of the list for param_pfunc */
192         DLIST_PROMOTE(ctx->sections, section);
193
194         return true;
195 }
196
197 static bool param_pfunc (const char *name, const char *value, void *_ctx)
198 {
199         struct param_context *ctx = (struct param_context *)_ctx;
200         struct param_opt *p = param_section_get(ctx->sections, name);
201
202         if (!p) {
203                 p = talloc_zero(ctx->sections, struct param_opt);
204                 if (p == NULL)
205                         return false;
206
207                 p->key = talloc_strdup(p, name);
208                 p->value = talloc_strdup(p, value);
209                 DLIST_ADD(ctx->sections->parameters, p);
210         } else { /* Replace current value */
211                 talloc_free(p->value);
212                 p->value = talloc_strdup(p, value);
213         }
214
215         return true;
216 }
217
218 struct param_context *param_init(TALLOC_CTX *mem_ctx)
219 {
220         return talloc_zero(mem_ctx, struct param_context);
221 }
222
223
224 int param_read(struct param_context *ctx, const char *fn)
225 {
226         ctx->sections = talloc_zero(ctx, struct param_section);
227         if (ctx->sections == NULL)
228                 return -1;
229
230         ctx->sections->name = talloc_strdup(ctx->sections, "global");
231         if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
232                 return -1;
233         }
234
235         return 0;
236 }
237
238 int param_write(struct param_context *ctx, const char *fn)
239 {
240         int file;
241         struct param_section *section;
242
243         if (fn == NULL || ctx == NULL)
244                 return -1;
245
246         file = open(fn, O_WRONLY|O_CREAT, 0755);
247
248         if (file == -1)
249                 return -1;
250         
251         for (section = ctx->sections; section; section = section->next) {
252                 struct param_opt *param;
253                 
254                 fdprintf(file, "[%s]\n", section->name);
255                 for (param = section->parameters; param; param = param->next) {
256                         fdprintf(file, "\t%s = %s\n", param->key, param->value);
257                 }
258                 fdprintf(file, "\n");
259         }
260
261         close(file);
262
263         return 0;
264 }