]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoder.c
Python: Restrict code to stable ABI subset
[libsigrokdecode.git] / decoder.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrokdecode project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <config.h>
22#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23#include "libsigrokdecode.h"
24#include <glib.h>
25
26/**
27 * @file
28 *
29 * Listing, loading, unloading, and handling protocol decoders.
30 */
31
32/**
33 * @defgroup grp_decoder Protocol decoders
34 *
35 * Handling protocol decoders.
36 *
37 * @{
38 */
39
40/** @cond PRIVATE */
41
42/* The list of protocol decoders. */
43static GSList *pd_list = NULL;
44
45/* srd.c */
46extern SRD_PRIV GSList *searchpaths;
47
48/* session.c */
49extern SRD_PRIV GSList *sessions;
50extern SRD_PRIV int max_session_id;
51
52/* module_sigrokdecode.c */
53extern SRD_PRIV PyObject *mod_sigrokdecode;
54
55/** @endcond */
56
57static gboolean srd_check_init(void)
58{
59 if (max_session_id < 0) {
60 srd_err("Library is not initialized.");
61 return FALSE;
62 } else
63 return TRUE;
64}
65
66/**
67 * Returns the list of supported/loaded protocol decoders.
68 *
69 * This is a GSList of pointers to struct srd_decoder items.
70 *
71 * @return List of decoders, NULL if none are supported or loaded.
72 *
73 * @since 0.2.0
74 */
75SRD_API const GSList *srd_decoder_list(void)
76{
77 return pd_list;
78}
79
80/**
81 * Get the decoder with the specified ID.
82 *
83 * @param id The ID string of the decoder to return.
84 *
85 * @return The decoder with the specified ID, or NULL if not found.
86 *
87 * @since 0.1.0
88 */
89SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
90{
91 GSList *l;
92 struct srd_decoder *dec;
93
94 for (l = pd_list; l; l = l->next) {
95 dec = l->data;
96 if (!strcmp(dec->id, id))
97 return dec;
98 }
99
100 return NULL;
101}
102
103static int get_channels(const struct srd_decoder *d, const char *attr,
104 GSList **pdchl)
105{
106 PyObject *py_channellist, *py_entry;
107 struct srd_channel *pdch;
108 int ret, num_channels, i;
109
110 if (!PyObject_HasAttrString(d->py_dec, attr))
111 /* No channels of this type specified. */
112 return SRD_OK;
113
114 py_channellist = PyObject_GetAttrString(d->py_dec, attr);
115 if (!PyTuple_Check(py_channellist)) {
116 srd_err("Protocol decoder %s %s attribute is not a tuple.",
117 d->name, attr);
118 return SRD_ERR_PYTHON;
119 }
120
121 if ((num_channels = PyTuple_Size(py_channellist)) == 0)
122 /* Empty channellist. */
123 return SRD_OK;
124
125 ret = SRD_OK;
126 for (i = 0; i < num_channels; i++) {
127 py_entry = PyTuple_GetItem(py_channellist, i);
128 if (!PyDict_Check(py_entry)) {
129 srd_err("Protocol decoder %s %s attribute is not "
130 "a list with dict elements.", d->name, attr);
131 ret = SRD_ERR_PYTHON;
132 break;
133 }
134
135 pdch = g_malloc(sizeof(struct srd_channel));
136
137 if ((py_dictitem_as_str(py_entry, "id", &pdch->id)) != SRD_OK) {
138 ret = SRD_ERR_PYTHON;
139 break;
140 }
141 if ((py_dictitem_as_str(py_entry, "name", &pdch->name)) != SRD_OK) {
142 ret = SRD_ERR_PYTHON;
143 break;
144 }
145 if ((py_dictitem_as_str(py_entry, "desc", &pdch->desc)) != SRD_OK) {
146 ret = SRD_ERR_PYTHON;
147 break;
148 }
149 pdch->order = i;
150
151 *pdchl = g_slist_append(*pdchl, pdch);
152 }
153
154 Py_DecRef(py_channellist);
155
156 return ret;
157}
158
159static int get_options(struct srd_decoder *d)
160{
161 PyObject *py_opts, *py_opt, *py_val, *py_default, *py_item;
162 Py_ssize_t opt, i;
163 struct srd_decoder_option *o;
164 GVariant *gvar;
165 gint64 lval;
166 double dval;
167 int overflow;
168 char *sval;
169
170 if (!PyObject_HasAttrString(d->py_dec, "options"))
171 /* No options, that's fine. */
172 return SRD_OK;
173
174 /* If present, options must be a tuple. */
175 py_opts = PyObject_GetAttrString(d->py_dec, "options");
176 if (!PyTuple_Check(py_opts)) {
177 srd_err("Protocol decoder %s: options attribute is not "
178 "a tuple.", d->id);
179 return SRD_ERR_PYTHON;
180 }
181
182 for (opt = 0; opt < PyTuple_Size(py_opts); opt++) {
183 py_opt = PyTuple_GetItem(py_opts, opt);
184 if (!PyDict_Check(py_opt)) {
185 srd_err("Protocol decoder %s options: each option "
186 "must consist of a dictionary.", d->name);
187 return SRD_ERR_PYTHON;
188 }
189 if (!(py_val = PyDict_GetItemString(py_opt, "id"))) {
190 srd_err("Protocol decoder %s option %zd has no "
191 "id.", d->name, opt);
192 return SRD_ERR_PYTHON;
193 }
194 o = g_malloc0(sizeof(struct srd_decoder_option));
195 py_str_as_str(py_val, &o->id);
196
197 if ((py_val = PyDict_GetItemString(py_opt, "desc")))
198 py_str_as_str(py_val, &o->desc);
199
200 if ((py_default = PyDict_GetItemString(py_opt, "default"))) {
201 if (PyUnicode_Check(py_default)) {
202 /* UTF-8 string */
203 py_str_as_str(py_default, &sval);
204 o->def = g_variant_new_string(sval);
205 g_free(sval);
206 } else if (PyLong_Check(py_default)) {
207 /* Long */
208 lval = PyLong_AsLongAndOverflow(py_default, &overflow);
209 if (overflow) {
210 /* Value is < LONG_MIN or > LONG_MAX */
211 PyErr_Clear();
212 srd_err("Protocol decoder %s option 'default' has "
213 "invalid default value.", d->name);
214 return SRD_ERR_PYTHON;
215 }
216 o->def = g_variant_new_int64(lval);
217 } else if (PyFloat_Check(py_default)) {
218 /* Float */
219 if ((dval = PyFloat_AsDouble(py_default)) == -1.0) {
220 PyErr_Clear();
221 srd_err("Protocol decoder %s option 'default' has "
222 "invalid default value.", d->name);
223 return SRD_ERR_PYTHON;
224 }
225 o->def = g_variant_new_double(dval);
226 } else {
227 srd_err("Protocol decoder %s option 'default' has "
228 "value of unsupported type.", d->name);
229 return SRD_ERR_PYTHON;
230 }
231 g_variant_ref_sink(o->def);
232 }
233
234 if ((py_val = PyDict_GetItemString(py_opt, "values"))) {
235 /* A default is required if a list of values is
236 * given, since it's used to verify their type. */
237 if (!o->def) {
238 srd_err("No default for option '%s'", o->id);
239 return SRD_ERR_PYTHON;
240 }
241 if (!PyTuple_Check(py_val)) {
242 srd_err("Option '%s' values should be a tuple.", o->id);
243 return SRD_ERR_PYTHON;
244 }
245 for (i = 0; i < PyTuple_Size(py_val); i++) {
246 py_item = PyTuple_GetItem(py_val, i);
247 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
248 srd_err("All values for option '%s' must be "
249 "of the same type as the default.",
250 o->id);
251 return SRD_ERR_PYTHON;
252 }
253 if (PyUnicode_Check(py_item)) {
254 /* UTF-8 string */
255 py_str_as_str(py_item, &sval);
256 gvar = g_variant_new_string(sval);
257 g_variant_ref_sink(gvar);
258 g_free(sval);
259 o->values = g_slist_append(o->values, gvar);
260 } else if (PyLong_Check(py_item)) {
261 /* Long */
262 lval = PyLong_AsLongAndOverflow(py_item, &overflow);
263 if (overflow) {
264 /* Value is < LONG_MIN or > LONG_MAX */
265 PyErr_Clear();
266 srd_err("Protocol decoder %s option 'values' "
267 "has invalid value.", d->name);
268 return SRD_ERR_PYTHON;
269 }
270 gvar = g_variant_new_int64(lval);
271 g_variant_ref_sink(gvar);
272 o->values = g_slist_append(o->values, gvar);
273 } else if (PyFloat_Check(py_item)) {
274 /* Float */
275 if ((dval = PyFloat_AsDouble(py_item)) == -1.0) {
276 PyErr_Clear();
277 srd_err("Protocol decoder %s option 'default' has "
278 "invalid default value.", d->name);
279 return SRD_ERR_PYTHON;
280 }
281 gvar = g_variant_new_double(dval);
282 g_variant_ref_sink(gvar);
283 o->values = g_slist_append(o->values, gvar);
284 }
285 }
286 }
287 d->options = g_slist_append(d->options, o);
288 }
289
290 return SRD_OK;
291}
292
293/**
294 * Load a protocol decoder module into the embedded Python interpreter.
295 *
296 * @param module_name The module name to be loaded.
297 *
298 * @return SRD_OK upon success, a (negative) error code otherwise.
299 *
300 * @since 0.1.0
301 */
302SRD_API int srd_decoder_load(const char *module_name)
303{
304 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
305 PyObject *py_bin_classes, *py_bin_class, *py_ann_rows, *py_ann_row;
306 PyObject *py_ann_classes, *py_long;
307 struct srd_decoder *d;
308 int ret, i, j;
309 char **ann, **bin, *ann_row_id, *ann_row_desc;
310 struct srd_channel *pdch;
311 GSList *l, *ann_classes;
312 struct srd_decoder_annotation_row *ann_row;
313
314 if (!srd_check_init())
315 return SRD_ERR;
316
317 if (!module_name)
318 return SRD_ERR_ARG;
319
320 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
321 /* Module was already imported. */
322 return SRD_OK;
323 }
324
325 srd_dbg("Loading protocol decoder '%s'.", module_name);
326
327 py_basedec = py_method = py_attr = NULL;
328
329 d = g_malloc0(sizeof(struct srd_decoder));
330
331 ret = SRD_ERR_PYTHON;
332
333 /* Import the Python module. */
334 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
335 srd_exception_catch("Import of '%s' failed", module_name);
336 goto err_out;
337 }
338
339 /* Get the 'Decoder' class as Python object. */
340 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
341 /* This generated an AttributeError exception. */
342 PyErr_Clear();
343 srd_err("Decoder class not found in protocol decoder %s.",
344 module_name);
345 goto err_out;
346 }
347
348 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
349 srd_dbg("sigrokdecode module not loaded.");
350 goto err_out;
351 }
352
353 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
354 srd_err("Decoder class in protocol decoder module %s is not "
355 "a subclass of sigrokdecode.Decoder.", module_name);
356 goto err_out;
357 }
358 Py_CLEAR(py_basedec);
359
360 /*
361 * Check that this decoder has the correct PD API version.
362 * PDs of different API versions are incompatible and cannot work.
363 */
364 py_long = PyObject_GetAttrString(d->py_dec, "api_version");
365 if (PyLong_AsLong(py_long) != 2) {
366 srd_err("Only PDs of API version 2 are supported.");
367 goto err_out;
368 }
369 Py_CLEAR(py_long);
370
371 /* Check for a proper start() method. */
372 if (!PyObject_HasAttrString(d->py_dec, "start")) {
373 srd_err("Protocol decoder %s has no start() method Decoder "
374 "class.", module_name);
375 goto err_out;
376 }
377 py_method = PyObject_GetAttrString(d->py_dec, "start");
378 if (!PyCallable_Check(py_method)) {
379 srd_err("Protocol decoder %s Decoder class attribute 'start' "
380 "is not a method.", module_name);
381 goto err_out;
382 }
383 Py_CLEAR(py_method);
384
385 /* Check for a proper decode() method. */
386 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
387 srd_err("Protocol decoder %s has no decode() method Decoder "
388 "class.", module_name);
389 goto err_out;
390 }
391 py_method = PyObject_GetAttrString(d->py_dec, "decode");
392 if (!PyCallable_Check(py_method)) {
393 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
394 "is not a method.", module_name);
395 goto err_out;
396 }
397 Py_CLEAR(py_method);
398
399 /* Store required fields in newly allocated strings. */
400 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
401 goto err_out;
402
403 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
404 goto err_out;
405
406 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
407 goto err_out;
408
409 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
410 goto err_out;
411
412 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
413 goto err_out;
414
415 /* All options and their default values. */
416 if (get_options(d) != SRD_OK)
417 goto err_out;
418
419 /* Check and import required channels. */
420 if (get_channels(d, "channels", &d->channels) != SRD_OK)
421 goto err_out;
422
423 /* Check and import optional channels. */
424 if (get_channels(d, "optional_channels", &d->opt_channels) != SRD_OK)
425 goto err_out;
426
427 /*
428 * Fix order numbers for the optional channels.
429 *
430 * Example:
431 * Required channels: r1, r2, r3. Optional: o1, o2, o3, o4.
432 * 'order' fields in the d->channels list = 0, 1, 2.
433 * 'order' fields in the d->opt_channels list = 3, 4, 5, 6.
434 */
435 for (l = d->opt_channels; l; l = l->next) {
436 pdch = l->data;
437 pdch->order += g_slist_length(d->channels);
438 }
439
440 /* Convert annotation class attribute to GSList of char **. */
441 d->annotations = NULL;
442 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
443 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
444 if (!PyTuple_Check(py_annlist)) {
445 srd_err("Protocol decoder %s annotations should "
446 "be a tuple.", module_name);
447 goto err_out;
448 }
449 for (i = 0; i < PyTuple_Size(py_annlist); i++) {
450 py_ann = PyTuple_GetItem(py_annlist, i);
451 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
452 srd_err("Protocol decoder %s annotation %d should "
453 "be a tuple with two elements.", module_name, i + 1);
454 goto err_out;
455 }
456
457 if (py_strseq_to_char(py_ann, &ann) != SRD_OK) {
458 goto err_out;
459 }
460 d->annotations = g_slist_append(d->annotations, ann);
461 }
462 }
463
464 /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
465 d->annotation_rows = NULL;
466 if (PyObject_HasAttrString(d->py_dec, "annotation_rows")) {
467 py_ann_rows = PyObject_GetAttrString(d->py_dec, "annotation_rows");
468 if (!PyTuple_Check(py_ann_rows)) {
469 srd_err("Protocol decoder %s annotation row list "
470 "must be a tuple.", module_name);
471 goto err_out;
472 }
473 for (i = 0; i < PyTuple_Size(py_ann_rows); i++) {
474 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
475 if (!PyTuple_Check(py_ann_row)) {
476 srd_err("Protocol decoder %s annotation rows "
477 "must be tuples.", module_name);
478 goto err_out;
479 }
480 if (PyTuple_Size(py_ann_row) != 3
481 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 0))
482 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 1))
483 || !PyTuple_Check(PyTuple_GetItem(py_ann_row, 2))) {
484 srd_err("Protocol decoder %s annotation rows "
485 "must contain tuples containing two "
486 "strings and a tuple.", module_name);
487 goto err_out;
488 }
489
490 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 0), &ann_row_id) != SRD_OK)
491 goto err_out;
492
493 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 1), &ann_row_desc) != SRD_OK)
494 goto err_out;
495
496 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
497 ann_classes = NULL;
498 for (j = 0; j < PyTuple_Size(py_ann_classes); j++) {
499 py_long = PyTuple_GetItem(py_ann_classes, j);
500 if (!PyLong_Check(py_long)) {
501 srd_err("Protocol decoder %s annotation row class "
502 "list must only contain numbers.", module_name);
503 goto err_out;
504 }
505 ann_classes = g_slist_append(ann_classes,
506 GINT_TO_POINTER(PyLong_AsLong(py_long)));
507 }
508
509 ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
510 ann_row->id = ann_row_id;
511 ann_row->desc = ann_row_desc;
512 ann_row->ann_classes = ann_classes;
513 d->annotation_rows = g_slist_append(d->annotation_rows, ann_row);
514 }
515 }
516
517 /* Convert binary class to GSList of char *. */
518 d->binary = NULL;
519 if (PyObject_HasAttrString(d->py_dec, "binary")) {
520 py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary");
521 if (!PyTuple_Check(py_bin_classes)) {
522 srd_err("Protocol decoder %s binary classes should "
523 "be a tuple.", module_name);
524 goto err_out;
525 }
526 for (i = 0; i < PyTuple_Size(py_bin_classes); i++) {
527 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
528 if (!PyTuple_Check(py_bin_class)) {
529 srd_err("Protocol decoder %s binary classes "
530 "should consist of tuples.", module_name);
531 goto err_out;
532 }
533 if (PyTuple_Size(py_bin_class) != 2
534 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0))
535 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) {
536 srd_err("Protocol decoder %s binary classes should "
537 "contain tuples with two strings.", module_name);
538 goto err_out;
539 }
540
541 if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) {
542 goto err_out;
543 }
544 d->binary = g_slist_append(d->binary, bin);
545 }
546 }
547
548 /* Append it to the list of supported/loaded decoders. */
549 pd_list = g_slist_append(pd_list, d);
550
551 ret = SRD_OK;
552
553err_out:
554 if (ret != SRD_OK) {
555 Py_XDECREF(py_method);
556 Py_XDECREF(py_basedec);
557 Py_XDECREF(d->py_dec);
558 Py_XDECREF(d->py_mod);
559 g_free(d);
560 }
561
562 return ret;
563}
564
565/**
566 * Return a protocol decoder's docstring.
567 *
568 * @param dec The loaded protocol decoder.
569 *
570 * @return A newly allocated buffer containing the protocol decoder's
571 * documentation. The caller is responsible for free'ing the buffer.
572 *
573 * @since 0.1.0
574 */
575SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
576{
577 PyObject *py_str;
578 char *doc;
579
580 if (!srd_check_init())
581 return NULL;
582
583 if (!dec)
584 return NULL;
585
586 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
587 return NULL;
588
589 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
590 srd_exception_catch("Failed to get docstring");
591 return NULL;
592 }
593
594 doc = NULL;
595 if (py_str != Py_None)
596 py_str_as_str(py_str, &doc);
597 Py_DecRef(py_str);
598
599 return doc;
600}
601
602static void free_channels(GSList *channellist)
603{
604 GSList *l;
605 struct srd_channel *pdch;
606
607 if (channellist == NULL)
608 return;
609
610 for (l = channellist; l; l = l->next) {
611 pdch = l->data;
612 g_free(pdch->id);
613 g_free(pdch->name);
614 g_free(pdch->desc);
615 g_free(pdch);
616 }
617 g_slist_free(channellist);
618}
619
620/**
621 * Unload the specified protocol decoder.
622 *
623 * @param dec The struct srd_decoder to be unloaded.
624 *
625 * @return SRD_OK upon success, a (negative) error code otherwise.
626 *
627 * @since 0.1.0
628 */
629SRD_API int srd_decoder_unload(struct srd_decoder *dec)
630{
631 struct srd_decoder_option *o;
632 struct srd_session *sess;
633 GSList *l;
634
635 if (!srd_check_init())
636 return SRD_ERR;
637
638 if (!dec)
639 return SRD_ERR_ARG;
640
641 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
642
643 /*
644 * Since any instances of this decoder need to be released as well,
645 * but they could be anywhere in the stack, just free the entire
646 * stack. A frontend reloading a decoder thus has to restart all
647 * instances, and rebuild the stack.
648 */
649 for (l = sessions; l; l = l->next) {
650 sess = l->data;
651 srd_inst_free_all(sess, NULL);
652 }
653
654 for (l = dec->options; l; l = l->next) {
655 o = l->data;
656 g_free(o->id);
657 g_free(o->desc);
658 g_variant_unref(o->def);
659 g_free(o);
660 }
661 g_slist_free(dec->options);
662
663 free_channels(dec->channels);
664 free_channels(dec->opt_channels);
665 g_free(dec->id);
666 g_free(dec->name);
667 g_free(dec->longname);
668 g_free(dec->desc);
669 g_free(dec->license);
670
671 /* The module's Decoder class. */
672 Py_XDECREF(dec->py_dec);
673 /* The module itself. */
674 Py_XDECREF(dec->py_mod);
675
676 g_free(dec);
677
678 return SRD_OK;
679}
680
681static void srd_decoder_load_all_zip_path(char *path)
682{
683 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
684 PyObject *prefix_obj, *files, *key, *value, *set, *modname;
685 Py_ssize_t pos = 0;
686 char *prefix;
687 size_t prefix_len;
688
689 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
690
691 zipimport_mod = PyImport_ImportModule("zipimport");
692 if (zipimport_mod == NULL)
693 goto err_out;
694
695 zipimporter_class = PyObject_GetAttrString(zipimport_mod, "zipimporter");
696 if (zipimporter_class == NULL)
697 goto err_out;
698
699 zipimporter = PyObject_CallFunction(zipimporter_class, "s", path);
700 if (zipimporter == NULL)
701 goto err_out;
702
703 prefix_obj = PyObject_GetAttrString(zipimporter, "prefix");
704 if (prefix_obj == NULL)
705 goto err_out;
706
707 files = PyObject_GetAttrString(zipimporter, "_files");
708 if (files == NULL)
709 goto err_out;
710
711 set = PySet_New(NULL);
712 if (set == NULL)
713 goto err_out;
714
715 if (py_str_as_str(prefix_obj, &prefix) != SRD_OK)
716 goto err_out;
717
718 prefix_len = strlen(prefix);
719
720 while (PyDict_Next(files, &pos, &key, &value)) {
721 char *path, *slash;
722 if (py_str_as_str(key, &path) == SRD_OK) {
723 if (strlen(path) > prefix_len &&
724 !memcmp(path, prefix, prefix_len) &&
725 (slash = strchr(path+prefix_len, '/'))) {
726 modname =
727 PyUnicode_FromStringAndSize(path+prefix_len,
728 slash-(path+prefix_len));
729 if (modname == NULL) {
730 PyErr_Clear();
731 } else {
732 PySet_Add(set, modname);
733 Py_XDECREF(modname);
734 }
735 }
736 free(path);
737 }
738 }
739
740 free(prefix);
741
742 while ((modname = PySet_Pop(set))) {
743 char *modname_str;
744 if (py_str_as_str(modname, &modname_str) == SRD_OK) {
745 /* The directory name is the module name (e.g. "i2c"). */
746 srd_decoder_load(modname_str);
747 free(modname_str);
748 }
749 Py_XDECREF(modname);
750 }
751
752err_out:
753 Py_XDECREF(set);
754 Py_XDECREF(files);
755 Py_XDECREF(prefix_obj);
756 Py_XDECREF(zipimporter);
757 Py_XDECREF(zipimporter_class);
758 Py_XDECREF(zipimport_mod);
759 PyErr_Clear();
760}
761
762static void srd_decoder_load_all_path(char *path)
763{
764 GDir *dir;
765 const gchar *direntry;
766
767 if (!(dir = g_dir_open(path, 0, NULL))) {
768 /* Not really fatal */
769 /* Try zipimport method too */
770 srd_decoder_load_all_zip_path(path);
771 return;
772 }
773
774 /* This ignores errors returned by srd_decoder_load(). That
775 * function will have logged the cause, but in any case we
776 * want to continue anyway. */
777 while ((direntry = g_dir_read_name(dir)) != NULL) {
778 /* The directory name is the module name (e.g. "i2c"). */
779 srd_decoder_load(direntry);
780 }
781 g_dir_close(dir);
782
783}
784
785/**
786 * Load all installed protocol decoders.
787 *
788 * @return SRD_OK upon success, a (negative) error code otherwise.
789 *
790 * @since 0.1.0
791 */
792SRD_API int srd_decoder_load_all(void)
793{
794 GSList *l;
795
796 if (!srd_check_init())
797 return SRD_ERR;
798
799 for (l = searchpaths; l; l = l->next)
800 srd_decoder_load_all_path(l->data);
801
802 return SRD_OK;
803}
804
805/**
806 * Unload all loaded protocol decoders.
807 *
808 * @return SRD_OK upon success, a (negative) error code otherwise.
809 *
810 * @since 0.1.0
811 */
812SRD_API int srd_decoder_unload_all(void)
813{
814 GSList *l;
815 struct srd_decoder *dec;
816
817 for (l = pd_list; l; l = l->next) {
818 dec = l->data;
819 srd_decoder_unload(dec);
820 }
821 g_slist_free(pd_list);
822 pd_list = NULL;
823
824 return SRD_OK;
825}
826
827/** @} */