fixed a memory error in change notify handling in gentest
[kai/samba.git] / source4 / param / param.i
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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 %module(docstring="Parsing and writing Samba configuration files.",package="samba.param") param
20
21 %{
22 #include <stdint.h>
23 #include <stdbool.h>
24
25 #include "includes.h"
26 #include "param/param.h"
27 #include "param/loadparm.h"
28
29 typedef struct param_context param;
30 typedef struct loadparm_context loadparm_context;
31 typedef struct loadparm_service loadparm_service;
32 typedef struct param_section param_section;
33 typedef struct param_opt param_opt;
34 %}
35
36 %import "stdint.i"
37 %import "carrays.i"
38 %import "typemaps.i"
39 %import "../lib/talloc/talloc.i"
40
41 %typemap(default,noblock=1) struct loadparm_context * {
42     $1 = loadparm_init(NULL);
43 }
44
45 %rename(LoadParm) loadparm_context;
46
47 %talloctype(loadparm_context);
48
49 typedef struct loadparm_context {
50     %extend {
51         loadparm_context(TALLOC_CTX *mem_ctx) { return loadparm_init(mem_ctx); }
52         struct loadparm_service *default_service() { return lp_default_service($self); }
53         %feature("docstring") load "S.load(filename) -> None\n" \
54                                    "Load specified file.";
55         bool load(const char *filename) { return lp_load($self, filename); }
56         %feature("docstring") load_default "S.load_default() -> None\n" \
57                                    "Load default smb.conf file.";
58         bool load_default() { return lp_load_default($self); }
59 #ifdef SWIGPYTHON
60         int __len__() { return lp_numservices($self); }
61         struct loadparm_service *__getitem__(const char *name) { return lp_service($self, name); }
62 #endif
63         %feature("docstring") configfile "S.configfile() -> string\n" \
64                                    "Return name of last config file that was loaded.";
65         const char *configfile() { return lp_configfile($self); }
66         %feature("docstring") is_mydomain "S.is_mydomain(domain_name) -> bool\n" \
67                                    "Check whether the specified name matches our domain name.";
68         bool is_mydomain(const char *domain) { return lp_is_mydomain($self, domain); }
69         %feature("docstring") is_myname "S.is_myname(netbios_name) -> bool\n" \
70                                    "Check whether the specified name matches one of our netbios names.";
71         bool is_myname(const char *name) { return lp_is_myname($self, name); }
72         int use(struct param_context *param_ctx) { return param_use($self, param_ctx); }
73         %feature("docstring") set "S.set(name, value) -> bool\n" \
74                                    "Change a parameter.";
75         bool set(const char *parm_name, const char *parm_value) {
76             if (parm_value == NULL)
77                 return false;
78             return lp_set_cmdline($self, parm_name, parm_value);
79         }
80
81         %feature("docstring") set "S.get(name, service_name) -> value\n" \
82                                    "Find specified parameter.";
83         PyObject *get(const char *param_name, const char *service_name)
84         {
85             struct parm_struct *parm = NULL;
86             void *parm_ptr = NULL;
87             int i;
88
89             if (service_name != NULL) {
90                 struct loadparm_service *service;
91                 /* its a share parameter */
92                 service = lp_service($self, service_name);
93                 if (service == NULL) {
94                     return Py_None;
95                 }
96                 if (strchr(param_name, ':')) {
97                     /* its a parametric option on a share */
98                     const char *type = talloc_strndup($self, 
99                                       param_name, 
100                                       strcspn(param_name, ":"));
101                     const char *option = strchr(param_name, ':') + 1;
102                     const char *value;
103                     if (type == NULL || option == NULL) {
104                         return Py_None;
105                     }
106                     value = lp_get_parametric($self, service, type, option);
107                     if (value == NULL) {
108                         return Py_None;
109                     }
110                     return PyString_FromString(value);
111                 }
112
113                 parm = lp_parm_struct(param_name);
114                 if (parm == NULL || parm->class == P_GLOBAL) {
115                     return Py_None;
116                 }
117                 parm_ptr = lp_parm_ptr($self, service, parm);
118             } else if (strchr(param_name, ':')) {
119                 /* its a global parametric option */
120                 const char *type = talloc_strndup($self, 
121                                   param_name, strcspn(param_name, ":"));
122                 const char *option = strchr(param_name, ':') + 1;
123                 const char *value;
124                 if (type == NULL || option == NULL) {
125                     return Py_None;
126                 }
127                 value = lp_get_parametric($self, NULL, type, option);
128                 if (value == NULL)
129                     return Py_None;
130                 return PyString_FromString(value);
131             } else {
132                 /* its a global parameter */
133                 parm = lp_parm_struct(param_name);
134                 if (parm == NULL) {
135                     return Py_None;
136                 }
137                 parm_ptr = lp_parm_ptr($self, NULL, parm);
138             }
139
140             if (parm == NULL || parm_ptr == NULL) {
141                 return Py_None;
142             }
143
144             /* construct and return the right type of python object */
145             switch (parm->type) {
146             case P_STRING:
147             case P_USTRING:
148                 return PyString_FromString(*(char **)parm_ptr);
149             case P_BOOL:
150                 return PyBool_FromLong(*(bool *)parm_ptr);
151             case P_INTEGER:
152             case P_OCTAL:
153             case P_BYTES:
154                 return PyLong_FromLong(*(int *)parm_ptr);
155             case P_ENUM:
156                 for (i=0; parm->enum_list[i].name; i++) {
157                     if (*(int *)parm_ptr == parm->enum_list[i].value) {
158                         return PyString_FromString(parm->enum_list[i].name);
159                     }
160                 }
161                 return Py_None;
162             case P_LIST: 
163                 {
164                     int j;
165                     const char **strlist = *(const char ***)parm_ptr;
166                     PyObject *pylist = PyList_New(str_list_length(strlist));
167                     for (j = 0; strlist[j]; j++) 
168                         PyList_SetItem(pylist, j, 
169                                        PyString_FromString(strlist[j]));
170                     return pylist;
171                 }
172
173                 break;
174             }
175             return Py_None;
176         }
177     }
178 } loadparm_context;
179
180 %nodefaultctor loadparm_service;
181 %nodefaultdtor loadparm_service;
182
183 typedef struct loadparm_service {
184     %extend { 
185         const char *volume_label(struct loadparm_service *sDefault) { return volume_label($self, sDefault); }
186         const char *printername(struct loadparm_service *sDefault) { return lp_printername($self, sDefault); }
187         int maxprintjobs(struct loadparm_service *sDefault) { return lp_maxprintjobs($self, sDefault); } 
188     }
189 } loadparm_service;
190
191 %rename(ParamFile) param_context;
192
193 %talloctype(param_context);
194 typedef struct param_context {
195     %extend { 
196         param(TALLOC_CTX *mem_ctx) { return param_init(mem_ctx); }
197         %feature("docstring") add_section "S.get_section(name) -> section\n"
198                                           "Get an existing section.";
199         struct param_section *get_section(const char *name);
200         %feature("docstring") add_section "S.add_section(name) -> section\n"
201                                           "Add a new section.";
202         struct param_section *add_section(const char *name);
203         struct param_opt *get(const char *name, const char *section_name="global");
204         const char *get_string(const char *name, const char *section_name="global");
205         int set_string(const char *param, const char *value, const char *section="global");
206 #ifdef SWIGPYTHON
207         int set(const char *parameter, PyObject *ob, const char *section_name="global")
208         {
209             struct param_opt *opt = param_get_add($self, parameter, section_name);
210
211             talloc_free(opt->value);
212             opt->value = talloc_strdup(opt, PyString_AsString(PyObject_Str(ob)));
213
214             return 0;
215         }
216         
217 #endif
218
219         %feature("docstring") first_section "S.first_section() -> section\n"
220                                           "Find first section";
221         struct param_section *first_section() { return $self->sections; }
222         %feature("docstring") next_section "S.next_section(prev) -> section\n"
223                                           "Find next section";
224         struct param_section *next_section(struct param_section *s) { return s->next; }
225
226         %feature("docstring") read "S.read(filename) -> bool\n"
227                                           "Read a filename.";
228         int read(const char *fn);
229         %feature("docstring") read "S.write(filename) -> bool\n"
230                                           "Write this object to a file.";
231         int write(const char *fn);
232     }
233     %pythoncode {
234         def __getitem__(self, name):
235             ret = self.get_section(name)
236             if ret is None:
237                 raise KeyError("No such section %s" % name)
238             return ret
239
240         class SectionIterator:
241             def __init__(self, param):
242                 self.param = param
243                 self.key = None
244
245             def __iter__(self):
246                 return self
247                 
248             def next(self):
249                 if self.key is None:
250                     self.key = self.param.first_section()
251                     if self.key is None:
252                         raise StopIteration
253                     return self.key
254                 else:
255                     self.key = self.param.next_section(self.key)
256                     if self.key is None:
257                         raise StopIteration
258                     return self.key
259
260         def __iter__(self):
261             return self.SectionIterator(self)
262     }
263 } param;
264
265 %talloctype(param_opt);
266
267 typedef struct param_opt {
268     %immutable key;
269     %immutable value;
270     const char *key, *value;
271     %extend {
272 #ifdef SWIGPYTHON
273         const char *__str__() { return $self->value; }
274 #endif
275     }
276 } param_opt;
277
278 %talloctype(param);
279 typedef struct param_section {
280     %immutable name;
281     const char *name;
282     %extend {
283         struct param_opt *get(const char *name);
284         struct param_opt *first_parameter() { return $self->parameters; }
285         struct param_opt *next_parameter(struct param_opt *s) { return s->next; }
286     }
287     %pythoncode {
288         def __getitem__(self, name):
289             ret = self.get(name)
290             if ret is None:
291                 raise KeyError("No such option %s" % name)
292             return ret
293
294         class ParamIterator:
295             def __init__(self, section):
296                 self.section = section
297                 self.key = None
298
299             def __iter__(self):
300                 return self
301                 
302             def next(self):
303                 if self.key is None:
304                     self.key = self.section.first_parameter()
305                     if self.key is None:
306                         raise StopIteration
307                     return self.key
308                 else:
309                     self.key = self.section.next_parameter(self.key)
310                     if self.key is None:
311                         raise StopIteration
312                     return self.key
313
314         def __iter__(self):
315             return self.ParamIterator(self)
316     }
317 } param_section;
318
319 %rename(default_config) global_loadparm;
320 struct loadparm_context *global_loadparm;
321
322 %{
323
324 struct loadparm_context *lp_from_py_object(PyObject *py_obj)
325 {
326     struct loadparm_context *lp_ctx;
327     if (PyString_Check(py_obj)) {
328         lp_ctx = loadparm_init(NULL);
329         if (!lp_load(lp_ctx, PyString_AsString(py_obj))) {
330             talloc_free(lp_ctx);
331             return NULL;
332         }
333         return lp_ctx;
334     }
335
336     if (py_obj == Py_None) {
337         lp_ctx = loadparm_init(NULL);
338         if (!lp_load_default(lp_ctx)) {
339             talloc_free(lp_ctx);
340             return NULL;
341         }
342         return lp_ctx;
343     }
344
345     if (SWIG_ConvertPtr(py_obj, (void *)&lp_ctx, SWIGTYPE_p_loadparm_context, 0 |  0 ) < 0)
346         return NULL;
347     return lp_ctx;
348 }
349
350 %}