diff --git a/scan.c b/scan.c index 4e3515801..bee16f591 100644 --- a/scan.c +++ b/scan.c @@ -14,9 +14,7 @@ #include struct iio_scan_context { - bool scan_usb; - bool scan_network; - bool scan_local; + char *backendopts; }; const char * iio_context_info_get_description( @@ -35,37 +33,34 @@ ssize_t iio_scan_context_get_info_list(struct iio_scan_context *ctx, struct iio_context_info ***info) { struct iio_scan_result scan_result = { 0, NULL }; - - if (WITH_LOCAL_BACKEND && ctx->scan_local) { - int ret = local_context_scan(&scan_result); - if (ret < 0) { - if (scan_result.info) - iio_context_info_list_free(scan_result.info); - return ret; - } - } - - if (WITH_USB_BACKEND && ctx->scan_usb) { - int ret = usb_context_scan(&scan_result); - if (ret < 0) { - if (scan_result.info) - iio_context_info_list_free(scan_result.info); - return ret; - } - } - - if (HAVE_DNS_SD && ctx->scan_network) { - int ret = dnssd_context_scan(&scan_result); - if (ret < 0) { - if (scan_result.info) - iio_context_info_list_free(scan_result.info); - return ret; + char *token, *rest=NULL; + ssize_t ret; + + for (token = iio_strtok_r(ctx->backendopts, ":", &rest); + token; token = iio_strtok_r(NULL, ":", &rest)) { + + /* Since tokens are all null terminated, it's safe to use strcmp on them */ + if (WITH_LOCAL_BACKEND && !strcmp(token, "local")) { + ret = local_context_scan(&scan_result); + } else if (WITH_USB_BACKEND && !strcmp(token, "usb")) { + ret = usb_context_scan(&scan_result); + } else if (HAVE_DNS_SD && !strcmp(token, "ip")) { + ret = dnssd_context_scan(&scan_result); + } else { + ret = -ENODEV; } + if (ret < 0) + goto err_free_scan_result_info; } *info = scan_result.info; return (ssize_t) scan_result.size; + +err_free_scan_result_info: + if (scan_result.info) + iio_context_info_list_free(scan_result.info); + return ret; } void iio_context_info_list_free(struct iio_context_info **list) @@ -126,20 +121,19 @@ struct iio_scan_context * iio_create_scan_context( return NULL; } - if (!backend || strstr(backend, "local")) - ctx->scan_local = true; - - if (!backend || strstr(backend, "usb")) - ctx->scan_usb = true; - - if (!backend || strstr(backend, "ip")) - ctx->scan_network = true; + ctx->backendopts = iio_strndup(backend ? backend : "local:usb:ip", PATH_MAX); + if (!ctx->backendopts) { + free(ctx); + errno = ENOMEM; + return NULL; + } return ctx; } void iio_scan_context_destroy(struct iio_scan_context *ctx) { + free(ctx->backendopts); free(ctx); }