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