<commit_before>/***
Callback registration interface

A client script should implement this module interface to handle
callbacks from the client. The script will be executed with the scripts
arguments passed in the 'arg' table. All standard Lua libraries are
registered.  Client interaction is available through the `glirc` lirary.

@module extension
@author <NAME>
@license ISC
@copyright <NAME> 2018
*/

#include <assert.h>
#include <string.h>
#include <libgen.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include "glirc-api.h"
#include "glirc-marshal.h"
#include "glirc-lib.h"

/* The Lua extraspace is used to store the glirc client
 * handle for reentry into the client
 */
static_assert(LUA_EXTRASPACE >= sizeof(struct glirc *),
              "Lua extraspace too small");

/* This global variable provides a unique key for storing
 * information in the Lua registry.
 */
static char glirc_callback_module_key;

/* Push path to glirc.lua which should be in the same directory
 * as the given path to the extension shared library.
 *
 * Returns 0 on success, non-zero on failure.
 */
static void push_scriptname
  (lua_State *L,
   const char *path,
   const struct glirc_string *args,
   size_t args_n)
{
        // When no arguments are provided, default to glirc.lua
        if (args_n == 0) {
                char *path_copy = strdup(path);
                const char *dir_part = dirname(path_copy);
                lua_pushfstring(L, "%s/glirc.lua", dir_part);
                free(path_copy);
        } else {
                char * script = glirc_resolve_path
                                  (get_glirc(L), args[0].str, args[0].len);
                lua_pushstring(L, script);
                glirc_free_string(script);
        }
}

static void push_args_table(lua_State *L, const struct glirc_string *args, size_t args_len) {
        lua_createtable(L, args_len > 1 ? args_len - 1 : 0, 1);
        for (size_t i = 1; i < args_len; i++) {
                push_glirc_string(L, args+i);
                lua_rawseti(L, -2, i);
        }
}

/* This function actually initialized the Lua state and
 * runs the user's script. It is allowed to raise errors
 * which will be caught by the use of lua_pcall in start.
 */<commit_msg>Fix build warning on Linux<commit_after>/***
Callback registration interface

A client script should implement this module interface to handle
callbacks from the client. The script will be executed with the scripts
arguments passed in the 'arg' table. All standard Lua libraries are
registered.  Client interaction is available through the `glirc` lirary.

@module extension
@author <NAME>
@license ISC
@copyright <NAME> 2018
*/

#define _POSIX_C_SOURCE 200809L

#include <assert.h>
#include <string.h>
#include <libgen.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include "glirc-api.h"
#include "glirc-marshal.h"
#include "glirc-lib.h"

/* The Lua extraspace is used to store the glirc client
 * handle for reentry into the client
 */
static_assert(LUA_EXTRASPACE >= sizeof(struct glirc *),
              "Lua extraspace too small");

/* This global variable provides a unique key for storing
 * information in the Lua registry.
 */
static char glirc_callback_module_key;

/* Push path to glirc.lua which should be in the same directory
 * as the given path to the extension shared library.
 *
 * Returns 0 on success, non-zero on failure.
 */
static void push_scriptname
  (lua_State *L,
   const char *path,
   const struct glirc_string *args,
   size_t args_n)
{
        // When no arguments are provided, default to glirc.lua
        if (args_n == 0) {
                char *path_copy = strdup(path);
                if (NULL == path_copy) {
                    luaL_error(L, "allocation failure");
                }
                const char *dir_part = dirname(path_copy);
                lua_pushfstring(L, "%s/glirc.lua", dir_part);
                free(path_copy);
        } else {
                char * script = glirc_resolve_path
                                  (get_glirc(L), args[0].str, args[0].len);
                lua_pushstring(L, script);
                glirc_free_string(script);
        }
}

static void push_args_table(lua_State *L, const struct glirc_string *args, size_t args_len) {
        lua_createtable(L, args_len > 1 ? args_len - 1 : 0, 1);
        for (size_t i = 1; i < args_len; i++) {
                push_glirc_string(L, args+i);
                lua_rawseti(L, -2, i);
        }
}

/* This function actually initialized the Lua state and
 * runs the user's script. It is allowed to raise errors
 * which will be caught by the use of lua_pcall in start.
 */
{'fraction_non_alphanumeric': 0.09400428265524625, 'fraction_numerical': 0.007280513918629551, 'mean_word_length': 2.643525741029641, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3711534', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1410, 'n_words': 501}