2 Unix SMB/CIFS implementation.
3 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
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.
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.
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/>.
19 %module(package="samba.param") param
26 #include "param/param.h"
27 #include "param/loadparm.h"
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;
39 %import "../lib/talloc/talloc.i"
41 %typemap(default) struct loadparm_context * {
42 $1 = loadparm_init(NULL);
45 %rename(LoadParm) loadparm_context;
47 %talloctype(loadparm_context);
49 typedef struct loadparm_context {
51 loadparm_context(TALLOC_CTX *mem_ctx) { return loadparm_init(mem_ctx); }
52 bool load(const char *filename) { return lp_load($self, filename); }
54 int __len__() { return lp_numservices($self); }
55 struct loadparm_service *__getitem__(const char *name) { return lp_service($self, name); }
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) { return param_use($self, param); }
61 bool set(const char *parm_name, const char *parm_value) {
62 return lp_set_cmdline($self, parm_name, parm_value);
65 PyObject *get(const char *param_name, const char *service_name)
67 struct parm_struct *parm = NULL;
68 void *parm_ptr = NULL;
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) {
78 if (strchr(param_name, ':')) {
79 /* its a parametric option on a share */
80 const char *type = talloc_strndup($self,
82 strcspn(param_name, ":"));
83 const char *option = strchr(param_name, ':') + 1;
85 if (type == NULL || option == NULL) {
88 value = lp_get_parametric($self, service, type, option);
92 return PyString_FromString(value);
95 parm = lp_parm_struct(param_name);
96 if (parm == NULL || parm->class == P_GLOBAL) {
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;
106 if (type == NULL || option == NULL) {
109 value = lp_get_parametric($self, NULL, type, option);
112 return PyString_FromString(value);
114 /* its a global parameter */
115 parm = lp_parm_struct(param_name);
119 parm_ptr = lp_parm_ptr($self, NULL, parm);
122 if (parm == NULL || parm_ptr == NULL) {
126 /* construct and return the right type of python object */
127 switch (parm->type) {
130 return PyString_FromString(*(char **)parm_ptr);
132 return PyBool_FromLong(*(bool *)parm_ptr);
136 return PyLong_FromLong(*(int *)parm_ptr);
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);
147 const char **strlist = *(const char ***)parm_ptr;
148 PyObject *pylist = PyList_New(str_list_length(strlist));
149 for (i = 0; strlist[i]; i++)
150 PyList_SetItem(pylist, i,
151 PyString_FromString(strlist[i]));
162 %nodefaultctor loadparm_service;
163 %nodefaultdtor loadparm_service;
165 typedef struct loadparm_service {
167 const char *volume_label() { return volume_label($self); }
168 const char *printername() { return lp_printername($self); }
169 int maxprintjobs() { return lp_maxprintjobs($self); }
173 %rename(ParamFile) param_context;
175 %talloctype(param_context);
176 typedef struct param_context {
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 int set(const char *param, PyObject *ob, const char *section_name="global")
186 struct param_opt *opt = param_get_add($self, param, section_name);
188 talloc_free(opt->value);
189 opt->value = talloc_strdup(opt, PyObject_Str(ob));
194 int read(const char *fn);
195 int write(const char *fn);
198 def __getitem__(self, name):
199 ret = self.get_section(name)
201 raise KeyError("No such section %s" % name)
206 %talloctype(param_opt);
208 typedef struct param_opt {
211 const char *__str__() { return $self->value; }
217 typedef struct param_section {
219 struct param_opt *get(const char *name);
222 def __getitem__(self, name):
225 raise KeyError("No such option %s" % name)
230 %rename(default_config) global_loadparm;
231 struct loadparm_context *global_loadparm;