static int list_plugins(args_t *args) { plugin_t *plugins = NULL; int nplugins = 0, mplugins = 0; init_plugin_paths(args); kstring_t str = {0,0,0}; int i; for (i=0; i<args->nplugin_paths; i++) { DIR *dp = opendir(args->plugin_paths[i]); if ( dp==NULL ) continue; struct dirent *ep; while ( (ep=readdir(dp)) ) { int len = strlen(ep->d_name); if ( strcasecmp(".so",ep->d_name+len-3) ) continue; str.l = 0; ksprintf(&str,"%s/%s", args->plugin_paths[i],ep->d_name); hts_expand(plugin_t, nplugins+1, mplugins, plugins); if ( load_plugin(args, str.s, 0, &plugins[nplugins]) < 0 ) continue; nplugins++; str.l = 0; kputs(ep->d_name, &str); int l = str.l - 1; while ( l>=0 && str.s[l]!='.' ) l--; if ( l>=0 ) str.s[l] = 0; free(plugins[nplugins-1].name); plugins[nplugins-1].name = strdup(str.s); // use a short name } closedir(dp); } if ( nplugins ) { qsort(plugins, nplugins, sizeof(plugins[0]), cmp_plugin_name); for (i=0; i<nplugins; i++) printf("\n-- %s --\n%s", plugins[i].name, plugins[i].about()); printf("\n"); } else print_plugin_usage_hint(); free(str.s); return nplugins ? 0 : 1; }
static int load_plugin(args_t *args, const char *fname, int exit_on_error, plugin_t *plugin) { plugin->name = strdup(fname); plugin->handle = dlopen_plugin(args, fname); if ( !plugin->handle ) { if ( exit_on_error ) { print_plugin_usage_hint(); error("Could not load \"%s\".\n\n", fname); } return -1; } dlerror(); plugin->init = (dl_init_f) dlsym(plugin->handle, "init"); char *ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s: %s\n", plugin->name, ret); return -1; } plugin->version = (dl_version_f) dlsym(plugin->handle, "version"); ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s: %s\n", plugin->name, ret); return -1; } plugin->about = (dl_about_f) dlsym(plugin->handle, "about"); ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s: %s\n", plugin->name, ret); return -1; } plugin->usage = (dl_about_f) dlsym(plugin->handle, "usage"); ret = dlerror(); if ( ret ) plugin->usage = plugin->about; plugin->process = (dl_process_f) dlsym(plugin->handle, "process"); ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s: %s\n", plugin->name, ret); return -1; } plugin->destroy = (dl_destroy_f) dlsym(plugin->handle, "destroy"); ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s: %s\n", plugin->name, ret); return -1; } return 0; }
static int load_plugin(args_t *args, const char *fname, int exit_on_error, plugin_t *plugin) { plugin->name = strdup(fname); plugin->handle = dlopen_plugin(args, fname); if ( !plugin->handle ) { if ( exit_on_error ) { print_plugin_usage_hint(); error("Could not load \"%s\".\n\n", fname); } return -1; } dlerror(); plugin->init = (dl_init_f) dlsym(plugin->handle, "init"); char *ret = dlerror(); if ( ret ) plugin->init = NULL; else if ( args->verbose ) fprintf(stderr,"\tinit .. ok\n"); plugin->run = (dl_run_f) dlsym(plugin->handle, "run"); ret = dlerror(); if ( ret ) plugin->run = NULL; else if ( args->verbose ) fprintf(stderr,"\trun .. ok\n"); if ( !plugin->init && !plugin->run ) { if ( exit_on_error ) error("Could not initialize %s, neither run or init found \n", plugin->name); else if ( args->verbose ) fprintf(stderr,"\tinit/run .. not found\n"); return -1; } plugin->version = (dl_version_f) dlsym(plugin->handle, "version"); ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s, version string not found\n", plugin->name); else if ( args->verbose ) fprintf(stderr,"\tversion .. not found\n"); return -1; } plugin->about = (dl_about_f) dlsym(plugin->handle, "about"); ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s: %s\n", plugin->name, ret); return -1; } plugin->usage = (dl_about_f) dlsym(plugin->handle, "usage"); ret = dlerror(); if ( ret ) plugin->usage = plugin->about; if ( plugin->run ) return 0; plugin->process = (dl_process_f) dlsym(plugin->handle, "process"); ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s: %s\n", plugin->name, ret); return -1; } plugin->destroy = (dl_destroy_f) dlsym(plugin->handle, "destroy"); ret = dlerror(); if ( ret ) { if ( exit_on_error ) error("Could not initialize %s: %s\n", plugin->name, ret); return -1; } return 0; }