--- lbbs/src/main.c 2025/05/09 12:58:27 1.28 +++ lbbs/src/main.c 2025/05/25 08:10:48 1.40 @@ -21,6 +21,7 @@ #include "log.h" #include "io.h" #include "menu.h" +#include "file_loader.h" #include #include #include @@ -70,13 +71,13 @@ int main(int argc, char *argv[]) break; case 'h': app_help(); - exit(0); + return 0; case 'v': puts(app_version); - exit(0); + return 0; default: arg_error(); - exit(1); + return 1; } } } @@ -90,12 +91,12 @@ int main(int argc, char *argv[]) if (strcmp(argv[i] + 2, "help") == 0) { app_help(); - exit(0); + return 0; } if (strcmp(argv[i] + 2, "version") == 0) { puts(app_version); - exit(0); + return 0; } if (strcmp(argv[i] + 2, "display-log") == 0) { @@ -126,7 +127,7 @@ int main(int argc, char *argv[]) // Initialize log if (log_begin(LOG_FILE_INFO, LOG_FILE_ERROR) < 0) { - exit(-1); + return -1; } if ((!daemon) && std_log_redir) @@ -141,15 +142,51 @@ int main(int argc, char *argv[]) // Load configuration if (load_conf(CONF_BBSD) < 0) { - exit(-2); + return -2; + } + + // Initialize trie dict pool + if (trie_dict_init(CONF_BBSD, TRIE_NODE_PER_POOL) < 0) + { + printf("trie_dict_init failed\n"); + return -3; + } + + // Load BBS cmd + if (load_cmd() < 0) + { + return -3; } // Load menus - if (load_menu(&bbs_menu, CONF_MENU) < 0) + p_bbs_menu = calloc(1, sizeof(MENU_SET)); + if (p_bbs_menu == NULL) + { + log_error("OOM: calloc(MENU_SET)\n"); + return -3; + } + if (load_menu(p_bbs_menu, CONF_MENU) < 0) + { + unload_menu(p_bbs_menu); + free(p_bbs_menu); + return -3; + } + + // Load data files + if (file_loader_init() < 0) { - unload_menu(&bbs_menu); - exit(-3); + log_error("file_loader_init() error\n"); + return -4; } + for (int i = 0; i < data_files_load_startup_count; i++) + { + if (load_file(data_files_load_startup[i]) < 0) + { + log_error("load_file_mmap(%s) error\n", data_files_load_startup[i]); + } + } + + log_std("Debug: trie dict nodes used = %d, limit = %d\n", trie_dict_used_nodes(), TRIE_NODE_PER_POOL); // Set signal handler signal(SIGHUP, sig_hup_handler); @@ -159,8 +196,16 @@ int main(int argc, char *argv[]) // Initialize socket server net_server(BBS_address, BBS_port); - // Cleanup - unload_menu(&bbs_menu); + // Cleanup loaded data files + file_loader_cleanup(); + + // Cleanup menu + unload_menu(p_bbs_menu); + free(p_bbs_menu); + p_bbs_menu = NULL; + + // Cleanup trie dict pool + trie_dict_cleanup(); log_std("Main process exit normally\n");