No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / file / dist / python / py_magic.c
blob1435aa4dfa5fbffe7f2f686112c252b2fd13a204
1 /* $NetBSD$ */
3 /*
4 Python wrappers for magic functions.
6 Copyright (C) Brett Funderburg, Deepfile Corp. Austin, TX, US 2003
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
10 are met:
11 1. Redistributions of source code must retain the above copyright
12 notice immediately at the beginning of the file, without modification,
13 this list of conditions, and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17 3. The name of the author may not be used to endorse or promote products
18 derived from this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 SUCH DAMAGE.
33 #include <Python.h>
34 #include <magic.h>
35 #include "py_magic.h"
37 /* Exceptions raised by this module */
39 PyObject* magic_error_obj;
41 /* Create a new magic_cookie_hnd object */
42 PyObject* new_magic_cookie_handle(magic_t cookie)
44 magic_cookie_hnd* mch;
46 mch = PyObject_New(magic_cookie_hnd, &magic_cookie_type);
48 mch->cookie = cookie;
50 return (PyObject*)mch;
53 static char _magic_open__doc__[] =
54 "Returns a magic cookie on success and None on failure.\n";
55 static PyObject* py_magic_open(PyObject* self, PyObject* args)
57 int flags = 0;
58 magic_t cookie;
60 if(!PyArg_ParseTuple(args, "i", &flags))
61 return NULL;
63 if(!(cookie = magic_open(flags))) {
64 PyErr_SetString(magic_error_obj, "failure initializing magic cookie");
65 return NULL;
68 return new_magic_cookie_handle(cookie);
71 static char _magic_close__doc__[] =
72 "Closes the magic database and deallocates any resources used.\n";
73 static PyObject* py_magic_close(PyObject* self, PyObject* args)
75 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
77 magic_close(hnd->cookie);
79 Py_INCREF(Py_None);
80 return Py_None;
83 static char _magic_error__doc__[] =
84 "Returns a textual explanation of the last error or None \
85 if there was no error.\n";
86 static PyObject* py_magic_error(PyObject* self, PyObject* args)
88 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
89 const char* message = NULL;
90 PyObject* result = Py_None;
92 message = magic_error(hnd->cookie);
94 if(message != NULL)
95 result = PyString_FromString(message);
96 else
97 Py_INCREF(Py_None);
99 return result;
102 static char _magic_errno__doc__[] =
103 "Returns a numeric error code. If return value is 0, an internal \
104 magic error occurred. If return value is non-zero, the value is \
105 an OS error code. Use the errno module or os.strerror() can be used \
106 to provide detailed error information.\n";
107 static PyObject* py_magic_errno(PyObject* self, PyObject* args)
109 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
110 return PyInt_FromLong(magic_errno(hnd->cookie));
113 static char _magic_file__doc__[] =
114 "Returns a textual description of the contents of the argument passed \
115 as a filename or None if an error occurred and the MAGIC_ERROR flag \
116 is set. A call to errno() will return the numeric error code.\n";
117 static PyObject* py_magic_file(PyObject* self, PyObject* args)
119 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
120 char* filename = NULL;
121 const char* message = NULL;
122 PyObject* result = Py_None;
124 if(!(PyArg_ParseTuple(args, "s", &filename)))
125 return NULL;
127 message = magic_file(hnd->cookie, filename);
129 if(message != NULL)
130 result = PyString_FromString(message);
131 else
132 Py_INCREF(Py_None);
134 return result;
137 static char _magic_buffer__doc__[] =
138 "Returns a textual description of the contents of the argument passed \
139 as a buffer or None if an error occurred and the MAGIC_ERROR flag \
140 is set. A call to errno() will return the numeric error code.\n";
141 static PyObject* py_magic_buffer(PyObject* self, PyObject* args)
143 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
144 void* buffer = NULL;
145 int buffer_length = 0;
146 const char* message = NULL;
147 PyObject* result = Py_None;
149 if(!(PyArg_ParseTuple(args, "s#", (char**)&buffer, &buffer_length)))
150 return NULL;
152 message = magic_buffer(hnd->cookie, buffer, buffer_length);
154 if(message != NULL)
155 result = PyString_FromString(message);
156 else
157 Py_INCREF(Py_None);
159 return result;
162 static char _magic_setflags__doc__[] =
163 "Set flags on the cookie object.\n \
164 Returns -1 on systems that don't support utime(2) or utimes(2) \
165 when MAGIC_PRESERVE_ATIME is set.\n";
166 static PyObject* py_magic_setflags(PyObject* self, PyObject* args)
168 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
169 int flags;
170 int result;
172 if(!(PyArg_ParseTuple(args, "i", &flags)))
173 return NULL;
175 result = magic_setflags(hnd->cookie, flags);
177 return PyInt_FromLong(result);
180 static char _magic_check__doc__[] =
181 "Check the validity of entries in the colon separated list of \
182 database files passed as argument or the default database file \
183 if no argument.\n Returns 0 on success and -1 on failure.\n";
184 static PyObject* py_magic_check(PyObject* self, PyObject* args)
186 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
187 char* filename = NULL;
188 int result;
190 if(!(PyArg_ParseTuple(args, "|s", &filename)))
191 return NULL;
193 result = magic_check(hnd->cookie, filename);
195 return PyInt_FromLong(result);
198 static char _magic_compile__doc__[] =
199 "Compile entries in the colon separated list of database files \
200 passed as argument or the default database file if no argument.\n \
201 Returns 0 on success and -1 on failure.\n \
202 The compiled files created are named from the basename(1) of each file \
203 argument with \".mgc\" appended to it.\n";
204 static PyObject* py_magic_compile(PyObject* self, PyObject* args)
206 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
207 char* filename = NULL;
208 int result;
210 if(!(PyArg_ParseTuple(args, "|s", &filename)))
211 return NULL;
213 result = magic_compile(hnd->cookie, filename);
215 return PyInt_FromLong(result);
218 static char _magic_load__doc__[] =
219 "Must be called to load entries in the colon separated list of database files \
220 passed as argument or the default database file if no argument before \
221 any magic queries can be performed.\n \
222 Returns 0 on success and -1 on failure.\n";
223 static PyObject* py_magic_load(PyObject* self, PyObject* args)
225 magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
226 char* filename = NULL;
227 int result;
229 if(!(PyArg_ParseTuple(args, "|s", &filename)))
230 return NULL;
232 result = magic_load(hnd->cookie, filename);
234 return PyInt_FromLong(result);
237 /* object methods */
239 static PyMethodDef magic_cookie_hnd_methods[] = {
240 { "close", (PyCFunction)py_magic_close,
241 METH_NOARGS, _magic_close__doc__ },
242 { "error", (PyCFunction)py_magic_error,
243 METH_NOARGS, _magic_error__doc__ },
244 { "file", (PyCFunction)py_magic_file,
245 METH_VARARGS, _magic_file__doc__ },
246 { "buffer", (PyCFunction)py_magic_buffer,
247 METH_VARARGS, _magic_buffer__doc__ },
248 { "setflags", (PyCFunction)py_magic_setflags,
249 METH_VARARGS, _magic_setflags__doc__ },
250 { "check", (PyCFunction)py_magic_check,
251 METH_VARARGS, _magic_check__doc__ },
252 { "compile", (PyCFunction)py_magic_compile,
253 METH_VARARGS, _magic_compile__doc__ },
254 { "load", (PyCFunction)py_magic_load,
255 METH_VARARGS, _magic_load__doc__ },
256 { "errno", (PyCFunction)py_magic_errno,
257 METH_NOARGS, _magic_errno__doc__ },
258 { NULL, NULL }
261 /* module level methods */
263 static PyMethodDef magic_methods[] = {
264 { "open", (PyCFunction)py_magic_open,
265 METH_VARARGS, _magic_open__doc__ },
266 { NULL, NULL }
269 static void py_magic_dealloc(PyObject* self)
271 PyObject_Del(self);
274 static PyObject* py_magic_getattr(PyObject* self, char* attrname)
276 return Py_FindMethod(magic_cookie_hnd_methods, self, attrname);
279 PyTypeObject magic_cookie_type = {
280 PyObject_HEAD_INIT(NULL)
282 "Magic cookie",
283 sizeof(magic_cookie_hnd),
285 py_magic_dealloc, /* tp_dealloc */
286 0, /* tp_print */
287 py_magic_getattr, /* tp_getattr */
288 0, /* tp_setattr */
289 0, /* tp_compare */
290 0, /* tp_repr */
291 0, /* tp_as_number */
292 0, /* tp_as_sequence */
293 0, /* tp_as_mapping */
294 0, /* tp_hash */
297 /* Initialize constants */
299 static struct const_vals {
300 const char* const name;
301 unsigned int value;
302 } module_const_vals[] = {
303 { "MAGIC_NONE", MAGIC_NONE },
304 { "MAGIC_DEBUG", MAGIC_DEBUG },
305 { "MAGIC_SYMLINK", MAGIC_SYMLINK },
306 { "MAGIC_COMPRESS", MAGIC_COMPRESS },
307 { "MAGIC_DEVICES", MAGIC_DEVICES },
308 { "MAGIC_MIME", MAGIC_MIME },
309 { "MAGIC_CONTINUE", MAGIC_CONTINUE },
310 { "MAGIC_CHECK", MAGIC_CHECK },
311 { "MAGIC_PRESERVE_ATIME", MAGIC_PRESERVE_ATIME },
312 { "MAGIC_ERROR", MAGIC_ERROR},
313 { NULL }
316 static void const_init(PyObject* dict)
318 struct const_vals* tmp;
319 PyObject *obj;
321 for(tmp = module_const_vals; tmp->name; ++tmp) {
322 obj = PyInt_FromLong(tmp->value);
323 PyDict_SetItemString(dict, tmp->name, obj);
324 Py_DECREF(obj);
329 * Module initialization
332 void initmagic(void)
334 PyObject* module;
335 PyObject* dict;
337 /* Initialize module */
339 module = Py_InitModule("magic", magic_methods);
340 dict = PyModule_GetDict(module);
342 magic_error_obj = PyErr_NewException("magic.error", NULL, NULL);
343 PyDict_SetItemString(dict, "error", magic_error_obj);
345 magic_cookie_type.ob_type = &PyType_Type;
347 /* Initialize constants */
349 const_init(dict);
351 if(PyErr_Occurred())
352 Py_FatalError("can't initialize module magic");