Added ability to send string to oct_init via cmd line arg

This commit is contained in:
j4nk 2025-03-16 17:35:06 -04:00
parent 0323c86399
commit 59932d224f
2 changed files with 28 additions and 1 deletions

17
main.c
View File

@ -20,6 +20,10 @@ int oct_type;
lua_State *L = NULL;
#define MAXIMUM_LUA_ARG_LENGTH 512
char lua_args[MAXIMUM_LUA_ARG_LENGTH];
struct {
char port[6]; // max 65535 so 6 bytes needed
// Might come in handy later to keep filename
@ -100,6 +104,9 @@ int process_args(int argc, char* argv[]) {
int log_file_spec = 0;
int valid_last_argument = 0;
// zero out lua_args
memset(lua_args, '\0', MAXIMUM_LUA_ARG_LENGTH);
for (int i = 1; i < argc; i++) {
// argv's are guaranteed to be zero-terminated, so can use
// strcmp instead of strncmp
@ -147,6 +154,12 @@ int process_args(int argc, char* argv[]) {
return 0;
}
}
else if (strcmp(argv[i], "-luaargs") == 0) {
if ((i+1) < argc) {
strncpy(lua_args, argv[i+1], MAXIMUM_LUA_ARG_LENGTH);
i++;
}
}
else {
// This is the lua file to run
if (i == argc-1) {
@ -200,7 +213,8 @@ int initialize_everything(char* lua_file) {
}
OCT_LOG_INFO("Begin running oct_init()");
lua_getglobal(L, "oct_init");
if (lua_pcall(L, 0, 2, 0) != LUA_OK) {
lua_pushstring(L, lua_args);
if (lua_pcall(L, 1, 2, 0) != LUA_OK) {
OCT_LOG_ERROR("%s", luaL_checkstring(L, -1));
deinitialize_everything();
return 0;
@ -255,6 +269,7 @@ void usage() {
fprintf(stderr, " -h\t\tPrint this message and exit\n");
fprintf(stderr, " -ll\t\t Set log level: 0=err, 1=warn, 2=info, 3=debug\n");
fprintf(stderr, " -lf\t\t Specify log output file\n");
fprintf(stderr, " -luaargs\t Specify string to be passed to oct_init function\n");
fprintf(stderr, "\n");
}

12
test_args.lua Normal file
View File

@ -0,0 +1,12 @@
require("oct_utils")
require("termbox_defs")
function oct_init(args)
OCT_LOG_ERROR("Starting oct_init")
OCT_LOG_ERROR("Received args: " .. args)
return OCT_NOT_NEEDS_NETWORKING, OCT_NOT_NEEDS_TERMBOX
end
function oct_loop(key, ch)
end