2021-11-05 15:42:52 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2021-11-06 13:20:36 +01:00
|
|
|
##### CONFIG
|
|
|
|
|
|
|
|
# symbols to ignore
|
2022-11-02 03:38:58 +01:00
|
|
|
IGNORE_SYM='luaL_pushmodule'
|
2021-11-06 13:20:36 +01:00
|
|
|
|
|
|
|
##### CONFIG
|
|
|
|
|
|
|
|
|
2021-11-05 15:42:52 +01:00
|
|
|
# https://stackoverflow.com/a/13062682
|
|
|
|
uncomment() {
|
|
|
|
[ $# -eq 2 ] && arg="$1" || arg=""
|
|
|
|
eval file="\$$#"
|
|
|
|
sed 's/a/aA/g; s/__/aB/g; s/#/aC/g' "$file" | \
|
|
|
|
gcc -P -E $arg - | \
|
|
|
|
sed 's/aC/#/g; s/aB/__/g; s/aA/a/g'
|
|
|
|
}
|
|
|
|
|
|
|
|
# this is the magic that turns multiline statements into
|
|
|
|
# single line statements
|
|
|
|
# LITERALLY DOES NOT WORK WITH PREPROCESSOR
|
|
|
|
onelineize() {
|
|
|
|
grep -v '^#' | sed -e ':r;$!{N;br};s/\([^{;]\)\n\s*/\1 /g'
|
|
|
|
}
|
|
|
|
|
|
|
|
discard_preprocessors() {
|
|
|
|
grep -v '#\(include\|if\|endif\)'
|
|
|
|
}
|
|
|
|
|
|
|
|
# sed regex for extracting data from function signature
|
|
|
|
# if this isn't regex, idk what is
|
|
|
|
# LUA_API (return type as \2) (function name as \3) (args as \4)
|
2021-11-06 12:22:55 +01:00
|
|
|
sym_regex='^LUA\(LIB\)\?_API\s\+\([^(]\+\)\s*(\([^)]\+\))\s\+(\([^)]\+\));'
|
2021-11-05 15:42:52 +01:00
|
|
|
|
|
|
|
# get funcptr declarations
|
|
|
|
ptrize() {
|
2021-11-06 13:36:33 +01:00
|
|
|
grep '^LUA' | grep -v "$IGNORE_SYM" | sed -e "s/$sym_regex/static\t\2(*\3)\t(\4);/"
|
2021-11-06 12:22:55 +01:00
|
|
|
}
|
|
|
|
|
2021-12-01 13:41:27 +01:00
|
|
|
# create a stub function that warns user when calling it
|
|
|
|
makestub() {
|
|
|
|
grep '^LUA' | grep -v "$IGNORE_SYM" | sed -e "s/$sym_regex/static\t\2\t__lite_xl_fallback_\3\t(\4) { fputs(\"warning: \3 is a stub\", stderr); }/"
|
|
|
|
}
|
|
|
|
|
2021-11-06 12:22:55 +01:00
|
|
|
import_sym() {
|
2021-11-06 13:20:36 +01:00
|
|
|
grep '^LUA' | grep -v "$IGNORE_SYM" | sed -e "s/$sym_regex/\tIMPORT_SYMBOL(\3, \2, \4);/"
|
2021-11-05 15:42:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
decl() {
|
2021-11-08 05:17:26 +01:00
|
|
|
echo "/** $(basename "$1") **/"
|
|
|
|
echo
|
|
|
|
|
2021-11-05 15:42:52 +01:00
|
|
|
header="$(uncomment $1 | discard_preprocessors)"
|
|
|
|
header1="$(onelineize <<< "$header")"
|
|
|
|
|
|
|
|
# typedef
|
|
|
|
grep -v '^\(LUA\|#\|extern\)' <<< "$header1"
|
|
|
|
# funcptrs
|
|
|
|
ptrize <<< "$header1"
|
|
|
|
# defines
|
2021-11-06 13:20:36 +01:00
|
|
|
(grep '^#' | grep -v "$IGNORE_SYM") <<< "$header"
|
2021-12-01 13:41:27 +01:00
|
|
|
# stubs
|
|
|
|
makestub <<< "$header1"
|
2021-11-05 15:42:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-06 12:22:55 +01:00
|
|
|
decl_import() {
|
|
|
|
uncomment $1 | onelineize | import_sym
|
2021-11-05 15:42:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-06 12:22:55 +01:00
|
|
|
generate_header() {
|
|
|
|
local LUA_PATH="$1"
|
|
|
|
echo "#ifndef LITE_XL_PLUGIN_API"
|
|
|
|
echo "#define LITE_XL_PLUGIN_API"
|
|
|
|
echo "/**"
|
|
|
|
echo "The lite_xl plugin API is quite simple. Any shared library can be a plugin file, so long"
|
|
|
|
echo "as it has an entrypoint that looks like the following, where xxxxx is the plugin name:"
|
|
|
|
echo '#include "lite_xl_plugin_api.h"'
|
2022-11-02 02:01:25 +01:00
|
|
|
echo "int luaopen_lite_xl_xxxxx(lua_State* L, void* XL) {"
|
2021-11-06 12:22:55 +01:00
|
|
|
echo " lite_xl_plugin_init(XL);"
|
|
|
|
echo " ..."
|
|
|
|
echo " return 1;"
|
|
|
|
echo "}"
|
|
|
|
echo "In linux, to compile this file, you'd do: 'gcc -o xxxxx.so -shared xxxxx.c'. Simple!"
|
|
|
|
echo "Due to the way the API is structured, you *should not* link or include lua libraries."
|
2021-11-06 13:36:33 +01:00
|
|
|
echo "This file was automatically generated. DO NOT MODIFY DIRECTLY."
|
2021-11-06 13:24:17 +01:00
|
|
|
echo "**/"
|
2021-11-06 12:22:55 +01:00
|
|
|
echo
|
|
|
|
echo
|
2021-11-06 13:24:40 +01:00
|
|
|
echo "#include <stdarg.h>"
|
|
|
|
echo "#include <stdio.h> // for BUFSIZ? this is kinda weird"
|
2021-11-06 13:36:33 +01:00
|
|
|
echo
|
2021-11-08 05:17:26 +01:00
|
|
|
echo "/** luaconf.h **/"
|
|
|
|
echo
|
|
|
|
uncomment "$LUA_PATH/luaconf.h"
|
2021-11-06 13:36:33 +01:00
|
|
|
echo
|
2021-11-06 12:22:55 +01:00
|
|
|
|
|
|
|
decl "$LUA_PATH/lua.h"
|
2021-11-08 05:17:26 +01:00
|
|
|
echo
|
2021-11-06 12:22:55 +01:00
|
|
|
decl "$LUA_PATH/lauxlib.h"
|
2022-11-02 03:38:58 +01:00
|
|
|
echo "static void (*luaL_openlibs) (lua_State *L);"
|
|
|
|
echo 'static void __lite_xl_fallback_luaL_openlibs (lua_State *L) { fputs("warning: luaL_openlibs is a stub", stderr); }'
|
2021-11-08 05:17:26 +01:00
|
|
|
echo
|
2021-11-06 12:22:55 +01:00
|
|
|
|
2021-12-01 13:41:27 +01:00
|
|
|
echo "#define IMPORT_SYMBOL(name, ret, ...) name = (name = (ret (*) (__VA_ARGS__)) symbol(#name), name == NULL ? &__lite_xl_fallback_##name : name)"
|
2021-11-06 12:22:55 +01:00
|
|
|
echo "static void lite_xl_plugin_init(void *XL) {"
|
|
|
|
echo -e "\tvoid* (*symbol)(const char *) = (void* (*) (const char *)) XL;"
|
|
|
|
|
|
|
|
decl_import "$LUA_PATH/lua.h"
|
|
|
|
decl_import "$LUA_PATH/lauxlib.h"
|
2022-11-02 03:38:58 +01:00
|
|
|
echo -e "\tIMPORT_SYMBOL(luaL_openlibs, void, lua_State* L);"
|
2021-11-06 12:22:55 +01:00
|
|
|
|
|
|
|
echo "}"
|
|
|
|
echo "#endif"
|
|
|
|
}
|
2021-11-05 15:42:52 +01:00
|
|
|
|
2021-11-06 12:22:55 +01:00
|
|
|
show_help() {
|
|
|
|
echo -e "Usage: $0 <OPTIONS> prefix"
|
|
|
|
echo
|
|
|
|
echo -e "Available options:"
|
|
|
|
echo -e "-p\t--prefix\tSet prefix (where to find lua.h and lauxlib.h)"
|
|
|
|
}
|
2021-11-05 15:42:52 +01:00
|
|
|
|
2021-11-06 12:22:55 +01:00
|
|
|
main() {
|
|
|
|
local prefix=""
|
|
|
|
|
|
|
|
for i in "$@"; do
|
|
|
|
case $i in
|
|
|
|
-h|--help)
|
|
|
|
show_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-p|--prefix)
|
|
|
|
prefix="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-11-17 01:58:44 +01:00
|
|
|
generate_header "$prefix"
|
2021-11-06 12:22:55 +01:00
|
|
|
}
|
2021-11-05 15:42:52 +01:00
|
|
|
|
2021-11-06 12:22:55 +01:00
|
|
|
main "$@"
|
2021-12-01 13:41:27 +01:00
|
|
|
# create a stub function that warns user when calling it
|