<commit_before>/**
 * This file is part of Slideshow.
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 *
 * Slideshow is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Slideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Slideshow.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "module_loader.h"
#include "assembler.h"
#include "log.h"
#include "path.h"
#include "browsers/browser.h"
#include "transitions/transition.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>

#ifdef WIN32
#	define SO_SUFFIX ".dll"
#else
#	define SO_SUFFIX ".la"
#endif

struct module_context_t {
	lt_dlhandle handle;
};

static enum {
	MODULE_NO_ERROR,
	MODULE_NOT_FOUND,
	MODULE_INVALID,
	MODULE_WRONG_TYPE,
} errnum = MODULE_NO_ERROR;

void moduleloader_init(const char* searchpath){
	lt_dlinit();

	char* path_list = strdup(searchpath);
	char* path = strtok(path_list, ":");
	while ( path ){
		lt_dladdsearchdir(path);
		path = strtok(NULL, ":");
	}
	free(path_list);
}

void moduleloader_cleanup(){
	lt_dlexit();
}

int module_error(){
	return errnum;
}

const char* module_error_string(){
	switch ( errnum ){
		case MODULE_NO_ERROR: return NULL;
		case MODULE_NOT_FOUND: return "Module not found";
		case MODULE_INVALID: return "Not a valid module";
		case MODULE_WRONG_TYPE: return "Requested module is not of the requested type";
	}

	/* make gcc happy (warning: control reaches end of non-void function) */
	return NULL;
}

static struct module_t* default_alloc(){
	return (struct module_t*)malloc(sizeof(struct module_t));
}

static void default_free(struct module_t* m){
	free(m);
}

/**
 * returns non-zero if MODULE_CALLEE_INIT is set
 * or if neither MODULE_CALLEE_INIT or
 * MODULE_CALLER_INIT is.
 */
static int callee_init(int flags){
	int A = flags & MODULE_CALLEE_INIT;
	int B = flags & MODULE_CALLER_INIT;

	/* converse implication */
	return A | !B;
}

module_handle module_open(const char* name, enum module_type_t type, int flags){
	log_message(Log_Debug, "Loading plugin '%s'\n", name);

	/* dlopenext tries all searchpaths and adds appropriate suffix */
	lt_dlhandle handle = lt_dlopenext(name);

	if ( !handle ){
		log_message(Log_Debug, "Plugin '%s' not found in search path.\n", name);
		errnum = MODULE_NOT_FOUND;
		return NULL;
	}

	/* test if the module is valid by quering a symbol which must exists in
	 * every module. */
	void* sym = lt_dlsym(handle, "__module_type");<commit_msg>loader: show lt error<commit_after>/**
 * This file is part of Slideshow.
 * Copyright (C) 2008 <NAME> <<EMAIL>>
 *
 * Slideshow is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Slideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Slideshow.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "module_loader.h"
#include "assembler.h"
#include "log.h"
#include "path.h"
#include "browsers/browser.h"
#include "transitions/transition.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>

#ifdef WIN32
#	define SO_SUFFIX ".dll"
#else
#	define SO_SUFFIX ".la"
#endif

struct module_context_t {
	lt_dlhandle handle;
};

static enum {
	MODULE_NO_ERROR,
	MODULE_NOT_FOUND,
	MODULE_INVALID,
	MODULE_WRONG_TYPE,
} errnum = MODULE_NO_ERROR;

void moduleloader_init(const char* searchpath){
	lt_dlinit();

	char* path_list = strdup(searchpath);
	char* path = strtok(path_list, ":");
	while ( path ){
		lt_dladdsearchdir(path);
		path = strtok(NULL, ":");
	}
	free(path_list);
}

void moduleloader_cleanup(){
	lt_dlexit();
}

int module_error(){
	return errnum;
}

const char* module_error_string(){
	switch ( errnum ){
		case MODULE_NO_ERROR: return NULL;
		case MODULE_NOT_FOUND: return "Module not found";
		case MODULE_INVALID: return "Not a valid module";
		case MODULE_WRONG_TYPE: return "Requested module is not of the requested type";
	}

	/* make gcc happy (warning: control reaches end of non-void function) */
	return NULL;
}

static struct module_t* default_alloc(){
	return (struct module_t*)malloc(sizeof(struct module_t));
}

static void default_free(struct module_t* m){
	free(m);
}

/**
 * returns non-zero if MODULE_CALLEE_INIT is set
 * or if neither MODULE_CALLEE_INIT or
 * MODULE_CALLER_INIT is.
 */
static int callee_init(int flags){
	int A = flags & MODULE_CALLEE_INIT;
	int B = flags & MODULE_CALLER_INIT;

	/* converse implication */
	return A | !B;
}

module_handle module_open(const char* name, enum module_type_t type, int flags){
	log_message(Log_Debug, "Loading plugin '%s'\n", name);

	/* dlopenext tries all searchpaths and adds appropriate suffix */
	lt_dlhandle handle = lt_dlopenext(name);

	if ( !handle ){
		log_message(Log_Debug, "Failed to load plugin '%s': %s\n", name, lt_dlerror());
		errnum = MODULE_NOT_FOUND;
		return NULL;
	}

	/* test if the module is valid by quering a symbol which must exists in
	 * every module. */
	void* sym = lt_dlsym(handle, "__module_type");
{'fraction_non_alphanumeric': 0.10946012877662209, 'fraction_numerical': 0.0023113752682846294, 'mean_word_length': 4.562901744719927, 'pattern_counts': {'":': 4, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1274952', 'n_tokens_mistral': 2068, 'n_tokens_neox': 2035, 'n_words': 676}