<commit_before>/*
 * nix operating system project tre interpreter
 * Copyright (c) 2005-2008 <NAME> <<EMAIL>
 *
 * Dynamic linker support
 */

#include "config.h"
#include "atom.h"
#include "error.h"
#include "argument.h"
#include "number.h"
#include "alien_dl.h"
#include "list.h"
#include "string.h"

#include <dlfcn.h>

/* Open shared object. */
treptr
trealien_builtin_dlopen (treptr args)
{
    treptr  path = trearg_get (args);
    void    * hdl;

    while (TREPTR_IS_STRING(path) == FALSE)
        path = treerror (path, "path to shared object expected");

    hdl = dlopen (TREATOM_STRINGP(path), RTLD_NOW);
    if (hdl == NULL)
        return treerror (path, dlerror ());
    return treatom_number_get ((double) (int) hdl, TRENUMTYPE_INTEGER);
}

/* Close shared object. */
treptr
trealien_builtin_dlclose (treptr args)
{
    treptr  hdl = trearg_get (args);
    int     ret;

    while (TREPTR_IS_NUMBER(hdl) == FALSE)
        hdl = treerror (hdl, "number handle expected");

    ret = dlclose ((void *) (int) TRENUMBER_VAL(hdl));
    if (ret == -1)
        return treerror (hdl, dlerror ());
    return treatom_number_get ((double) ret, TRENUMTYPE_INTEGER);
}

/* Get pointer to symbol. */
treptr
trealien_builtin_dlsym (treptr args)
{
    treptr  hdl;
    treptr  sym;
    void    * ret;

    trearg_get2 (&hdl, &sym, args);

    while (TREPTR_IS_NUMBER(hdl) == FALSE)
        hdl = treerror (hdl, "number handle expected");

    while (TREPTR_IS_STRING(sym) == FALSE)
        sym = treerror (sym, "symbol string expected");

    ret = dlsym ((void *) (int) TRENUMBER_VAL(hdl), TREATOM_STRINGP(sym));
    if (ret == NULL)
        return treerror (hdl, dlerror ());
    return treatom_number_get ((double) (int) ret, TRENUMTYPE_INTEGER);
}

size_t
trealien_argconv (treptr arg)
{
    if (TREPTR_IS_NUMBER(arg))
        return (size_t) TRENUMBER_VAL(arg);
    if (TREPTR_IS_STRING(arg))
        return (size_t) TREATOM_STRINGP(arg);

    treerror_norecover (arg, "integer number or string expected");

    /*NOTREACHED*/
    return 0;
}

/* Call C function without arguments. */
treptr
trealien_builtin_call (treptr args)
{
    int     ret;
    treptr  ptr;
    int     (* fun) (void);<commit_msg>ALIEN-DL*: On error, return error description string.<commit_after>/*
 * nix operating system project tre interpreter
 * Copyright (c) 2005-2008 <NAME> <<EMAIL>
 *
 * Dynamic linker support
 */

#include "config.h"
#include "atom.h"
#include "error.h"
#include "argument.h"
#include "number.h"
#include "alien_dl.h"
#include "list.h"
#include "string.h"

#include <dlfcn.h>

/* Open shared object. */
treptr
trealien_builtin_dlopen (treptr args)
{
    treptr  path = trearg_get (args);
    void    * hdl;

retry:
    while (TREPTR_IS_STRING(path) == FALSE)
        path = treerror (path, "path to shared object expected");

    hdl = dlopen (TREATOM_STRINGP(path), RTLD_NOW);
    if (hdl == NULL) {
        path = treerror (path, dlerror ());
		goto retry;
	}

    return treatom_number_get ((double) (int) hdl, TRENUMTYPE_INTEGER);
}

/* Close shared object. */
treptr
trealien_builtin_dlclose (treptr args)
{
    treptr  hdl = trearg_get (args);
    int     ret;

    while (TREPTR_IS_NUMBER(hdl) == FALSE)
        hdl = treerror (hdl, "number handle expected");

    ret = dlclose ((void *) (int) TRENUMBER_VAL(hdl));
    if (ret == -1)
        return trestring_get (dlerror ());

    return treatom_number_get ((double) ret, TRENUMTYPE_INTEGER);
}

/* Get pointer to symbol. */
treptr
trealien_builtin_dlsym (treptr args)
{
    treptr  hdl;
    treptr  sym;
    void    * ret;

    trearg_get2 (&hdl, &sym, args);

    while (TREPTR_IS_NUMBER(hdl) == FALSE)
        hdl = treerror (hdl, "number handle expected");

    while (TREPTR_IS_STRING(sym) == FALSE)
        sym = treerror (sym, "symbol string expected");

    ret = dlsym ((void *) (int) TRENUMBER_VAL(hdl), TREATOM_STRINGP(sym));
    if (ret == NULL)
        return trestring_get (dlerror ());

    return treatom_number_get ((double) (int) ret, TRENUMTYPE_INTEGER);
}

/* Call C function without arguments. */
treptr
trealien_builtin_call (treptr args)
{
    int     ret;
    treptr  ptr;
    int     (* fun) (void);
{'fraction_non_alphanumeric': 0.1324898543805204, 'fraction_numerical': 0.00501312962520888, 'mean_word_length': 3.2842535787321063, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3587980', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1529, 'n_words': 398}