r26592: Finish fixing the samba3dump script.
[metze/old/samba4-sync/samba4-sync.git/.git] / source / 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(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         bool load(const char *filename) { return lp_load($self, filename); }
53 #ifdef SWIGPYTHON
54         int __len__() { return lp_numservices($self); }
55         struct loadparm_service *__getitem__(const char *name) { return lp_service($self, name); }
56 #endif
57         const char *configfile() { return lp_configfile($self); }
58         bool is_mydomain(const char *domain) { return lp_is_mydomain($self, domain); }
59         bool is_myname(const char *name) { return lp_is_myname($self, name); }
60         int use(struct param_context *param_ctx) { return param_use($self, param_ctx); }
61         bool set(const char *parm_name, const char *parm_value) {
62             return lp_set_cmdline($self, parm_name, parm_value);
63         }
64
65         PyObject *get(const char *param_name, const char *service_name)
66         {
67             struct parm_struct *parm = NULL;
68             void *parm_ptr = NULL;
69             int i;
70
71             if (service_name != NULL) {
72                 struct loadparm_service *service;
73                 /* its a share parameter */
74                 service = lp_service($self, service_name);
75                 if (service == NULL) {
76                     return Py_None;
77                 }
78                 if (strchr(param_name, ':')) {
79                     /* its a parametric option on a share */
80                     const char *type = talloc_strndup($self, 
81                                       param_name, 
82                                       strcspn(param_name, ":"));
83                     const char *option = strchr(param_name, ':') + 1;
84                     const char *value;
85                     if (type == NULL || option == NULL) {
86                         return Py_None;
87                     }
88                     value = lp_get_parametric($self, service, type, option);
89                     if (value == NULL) {
90                         return Py_None;
91                     }
92                     return PyString_FromString(value);
93                 }
94
95                 parm = lp_parm_struct(param_name);
96                 if (parm == NULL || parm->class == P_GLOBAL) {
97                     return Py_None;
98                 }
99                 parm_ptr = lp_parm_ptr($self, service, parm);
100             } else if (strchr(param_name, ':')) {
101                 /* its a global parametric option */
102                 const char *type = talloc_strndup($self, 
103                                   param_name, strcspn(param_name, ":"));
104                 const char *option = strchr(param_name, ':') + 1;
105                 const char *value;
106                 if (type == NULL || option == NULL) {
107                     return Py_None;
108                 }
109                 value = lp_get_parametric($self, NULL, type, option);
110                 if (value == NULL)
111                     return Py_None;
112                 return PyString_FromString(value);
113             } else {
114                 /* its a global parameter */
115                 parm = lp_parm_struct(param_name);
116                 if (parm == NULL) {
117                     return Py_None;
118                 }
119                 parm_ptr = lp_parm_ptr($self, NULL, parm);
120             }
121
122             if (parm == NULL || parm_ptr == NULL) {
123                 return Py_None;
124             }
125
126             /* construct and return the right type of python object */
127             switch (parm->type) {
128             case P_STRING:
129             case P_USTRING:
130                 return PyString_FromString(*(char **)parm_ptr);
131             case P_BOOL:
132                 return PyBool_FromLong(*(bool *)parm_ptr);
133             case P_INTEGER:
134             case P_OCTAL:
135             case P_BYTES:
136                 return PyLong_FromLong(*(int *)parm_ptr);
137             case P_ENUM:
138                 for (i=0; parm->enum_list[i].name; i++) {
139                     if (*(int *)parm_ptr == parm->enum_list[i].value) {
140                         return PyString_FromString(parm->enum_list[i].name);
141                     }
142                 }
143                 return Py_None;
144             case P_LIST: 
145                 {
146                     int j;
147                     const char **strlist = *(const char ***)parm_ptr;
148                     PyObject *pylist = PyList_New(str_list_length(strlist));
149                     for (j = 0; strlist[j]; j++) 
150                         PyList_SetItem(pylist, j, 
151                                        PyString_FromString(strlist[j]));
152                     return pylist;
153                 }
154
155                 break;
156             }
157             return Py_None;
158         }
159     }
160 } loadparm_context;
161
162 %nodefaultctor loadparm_service;
163 %nodefaultdtor loadparm_service;
164
165 typedef struct loadparm_service {
166     %extend { 
167         const char *volume_label() { return volume_label($self); }
168         const char *printername() { return lp_printername($self); }
169         int maxprintjobs() { return lp_maxprintjobs($self); } 
170     }
171 } loadparm_service;
172
173 %rename(ParamFile) param_context;
174
175 %talloctype(param_context);
176 typedef struct param_context {
177     %extend { 
178         param(TALLOC_CTX *mem_ctx) { return param_init(mem_ctx); }
179         struct param_section *get_section(const char *name);
180         struct param_section *add_section(const char *name);
181         struct param_opt *get(const char *name, const char *section_name="global");
182         const char *get_string(const char *name, const char *section_name="global");
183         int set_string(const char *param, const char *value, const char *section="global");
184 #ifdef SWIGPYTHON
185         int set(const char *parameter, PyObject *ob, const char *section_name="global")
186         {
187             struct param_opt *opt = param_get_add($self, parameter, section_name);
188
189             talloc_free(opt->value);
190             opt->value = talloc_strdup(opt, PyString_AsString(PyObject_Str(ob)));
191
192             return 0;
193         }
194         
195 #endif
196
197         struct param_section *first_section() { return $self->sections; }
198         struct param_section *next_section(struct param_section *s) { return s->next; }
199
200         int read(const char *fn);
201         int write(const char *fn);
202     }
203     %pythoncode {
204         def __getitem__(self, name):
205             ret = self.get_section(name)
206             if ret is None:
207                 raise KeyError("No such section %s" % name)
208             return ret
209
210         class SectionIterator:
211             def __init__(self, param):
212                 self.param = param
213                 self.key = None
214
215             def __iter__(self):
216                 return self
217                 
218             def next(self):
219                 if self.key is None:
220                     self.key = self.param.first_section()
221                     if self.key is None:
222                         raise StopIteration
223                     return self.key
224                 else:
225                     self.key = self.param.next_section(self.key)
226                     if self.key is None:
227                         raise StopIteration
228                     return self.key
229
230         def __iter__(self):
231             return self.SectionIterator(self)
232     }
233 } param;
234
235 %talloctype(param_opt);
236
237 typedef struct param_opt {
238     %immutable key;
239     %immutable value;
240     const char *key, *value;
241     %extend {
242 #ifdef SWIGPYTHON
243         const char *__str__() { return $self->value; }
244 #endif
245     }
246 } param_opt;
247
248 %talloctype(param);
249 typedef struct param_section {
250     %immutable name;
251     const char *name;
252     %extend {
253         struct param_opt *get(const char *name);
254         struct param_opt *first_parameter() { return $self->parameters; }
255         struct param_opt *next_parameter(struct param_opt *s) { return s->next; }
256     }
257     %pythoncode {
258         def __getitem__(self, name):
259             ret = self.get(name)
260             if ret is None:
261                 raise KeyError("No such option %s" % name)
262             return ret
263
264         class ParamIterator:
265             def __init__(self, section):
266                 self.section = section
267                 self.key = None
268
269             def __iter__(self):
270                 return self
271                 
272             def next(self):
273                 if self.key is None:
274                     self.key = self.section.first_parameter()
275                     if self.key is None:
276                         raise StopIteration
277                     return self.key
278                 else:
279                     self.key = self.section.next_parameter(self.key)
280                     if self.key is None:
281                         raise StopIteration
282                     return self.key
283
284         def __iter__(self):
285             return self.ParamIterator(self)
286     }
287 } param_section;
288
289 %rename(default_config) global_loadparm;
290 struct loadparm_context *global_loadparm;