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