diff --git a/src/lua_rds.c b/src/lua_rds.c new file mode 100644 index 0000000..01df95e --- /dev/null +++ b/src/lua_rds.c @@ -0,0 +1,50 @@ +#include "lua_rds.h" + +static RDSModulator* mod = NULL; +static lua_State *L = NULL; + +int lua_set_rds_pi(lua_State *localL) { + int pi_value = luaL_checkinteger(localL, 1); + mod->enc->data[mod->enc->program].pi = pi_value; + return 0; +} + +void init_lua(RDSModulator* rds_mod) { + mod = rds_mod; + L = luaL_newstate(); + luaL_openlibs(L); + lua_register(L, "set_rds_pi", lua_set_rds_pi); +} + +void run_lua(char *str, char *cmd_output) { + lua_pushstring(L, str); + lua_setglobal(L, "cmd"); + + int top = lua_gettop(L); + + char path[128]; + snprintf(path, sizeof(path), "%s/.command95.lua", getenv("HOME")); + if (luaL_dofile(L, path) != LUA_OK) { + const char *err = lua_tostring(L, -1); + fprintf(stderr, "Lua error: %s\n", err); + lua_pop(L, 1); + lua_settop(L, top); + return; + } + + lua_getglobal(L, "cmd_output"); + + if (lua_isstring(L, -1)) { + const char * message = lua_tostring(L, -1); + lua_pop(L, 1); + if(cmd_output) strcpy(cmd_output, message); + } +} + +void destroy_lua(void) { + if (L) { + lua_close(L); + L = NULL; + } + mod = NULL; +} diff --git a/src/lua_rds.h b/src/lua_rds.h new file mode 100644 index 0000000..874eb13 --- /dev/null +++ b/src/lua_rds.h @@ -0,0 +1,11 @@ +#pragma once +#include +#include +#include +#include +#include + +int lua_set_rds_pi(lua_State *L); +void init_lua(RDSModulator* rds_mod); +void run_lua(char *str, char *cmd_output); +void destroy_lua(); \ No newline at end of file diff --git a/src/udp_server.c b/src/udp_server.c index da30365..3ca29e4 100644 --- a/src/udp_server.c +++ b/src/udp_server.c @@ -31,6 +31,7 @@ int open_udp_server(int port, RDSModulator* rds_mod) { poller.events = POLLIN; mod = rds_mod; + init_lua(rds_mod); return 0; } @@ -60,6 +61,7 @@ void poll_udp_server() { memset(cmd_output, 0, BUF_SIZE); process_ascii_cmd(mod, cmd_buf, cmd_output); + run_lua(cmd_buf, NULL); size_t out_len = strlen(cmd_output); if (out_len > 0) { @@ -74,4 +76,5 @@ void poll_udp_server() { void close_udp_server() { if (sockfd >= 0) close(sockfd); sockfd = -1; + destroy_lua(); } diff --git a/src/udp_server.h b/src/udp_server.h index 16d207e..009b09a 100644 --- a/src/udp_server.h +++ b/src/udp_server.h @@ -12,6 +12,7 @@ #include #include "modulator.h" #include "ascii_cmd.h" +#include "lua_rds.h" int open_udp_server(int port, RDSModulator *rds_mod); void poll_udp_server();