--- lbbs/src/bbs_cmd.c 2005/03/21 17:08:21 1.1 +++ lbbs/src/bbs_cmd.c 2025/11/11 00:28:05 1.28 @@ -1,42 +1,90 @@ -/*************************************************************************** - bbs_cmd.c - description - ------------------- - begin : Wed Mar 16 2004 - copyright : (C) 2005 by Leaflet - email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * bbs_cmd + * - manager of menu command handler + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "bbs_cmd.h" - -BBS_CMD bbs_cmd_list[MAX_CMD_ID] = { - {"EXITBBS", EXITBBS} - , - {"BBSNET", BBSNET} +#include "data_file_mgr.h" +#include "menu_proc.h" +#include "trie_dict.h" +#include + +static const BBS_CMD bbs_cmd_list[] = { + {"LIST_SECTION", list_section}, + {"RunMBEM", exec_mbem}, + {"EXITBBS", exit_bbs}, + {"LICENSE", license}, + {"COPYRIGHT", copyright}, + {"VERSION", version}, + {"RELOADCONF", reload_bbs_conf}, + {"DATAMGR", data_file_mgr}, + {"SHUTDOWN", shutdown_bbs}, + {"M_FAVOR_SECTION", favor_section_filter}, + {"VIEW_EX_ARTICLE", view_ex_article}, + {"LIST_EX_SECTION", list_ex_section}, + {"TOP10", show_top10_menu}, + {"LOCATE_ARTICLE", locate_article}, + {"FAVOR_TOPIC", favor_topic}, + {"LIST_USER", list_user}, + {"LIST_ONLINE_USER", list_online_user}, + {"EDIT_INTRO", edit_intro}, }; -static bbs_cmd_max = 2; +static const int bbs_cmd_count = sizeof(bbs_cmd_list) / sizeof(BBS_CMD); + +static TRIE_NODE *p_bbs_cmd_dict; -int -get_cmd_value (const char *cmd) +int load_cmd() { - int i; + if (p_bbs_cmd_dict != NULL) + { + return -1; + } + + p_bbs_cmd_dict = trie_dict_create(); + + if (p_bbs_cmd_dict == NULL) + { + return -2; + } + + for (int i = 0; i < bbs_cmd_count; i++) + { + if (trie_dict_set(p_bbs_cmd_dict, bbs_cmd_list[i].cmd, (int64_t)i) != 1) + { + return -3; + } + } + + return 0; +} + +void unload_cmd() +{ + trie_dict_destroy(p_bbs_cmd_dict); + p_bbs_cmd_dict = NULL; +} + +bbs_cmd_handler get_cmd_handler(const char *cmd) +{ + int64_t i; + + if (p_bbs_cmd_dict == NULL) + { + return NULL; + } - for (i = 0; i < bbs_cmd_max; i++) - { - if (strcmp (cmd, bbs_cmd_list[i].cmd) == 0) + if (trie_dict_get(p_bbs_cmd_dict, cmd, &i) != 1) { - return bbs_cmd_list[i].value; + return NULL; } - } - return UNKNOWN_CMD; + return (bbs_cmd_list[i].handler); }