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