--- lbbs/src/menu.c 2025/05/30 02:57:09 1.62 +++ lbbs/src/menu.c 2025/11/11 00:28:05 1.85 @@ -1,44 +1,44 @@ -/*************************************************************************** - menu.c - description - ------------------- - Copyright : (C) 2004-2025 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 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * menu + * - configurable user interactive menu feature + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "bbs.h" #include "bbs_cmd.h" -#include "user_priv.h" #include "bbs_cmd.h" -#include "menu.h" -#include "log.h" +#include "common.h" #include "io.h" +#include "log.h" +#include "menu.h" #include "screen.h" -#include "common.h" -#include -#include +#include "user_priv.h" #include -#include #include +#include +#include +#include #include -#include #include +#include -#define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_" -#define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n" -#define MENU_CONF_DELIM_WITHOUT_SPACE "\r\n" +enum _menu_constant_t +{ + MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4, + MENU_SHMGET_RETRY_LIMIT = 10, +}; -#define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4) +static const char MENU_CONF_DELIM_WITH_SPACE[] = " ,\t\r\n"; +static const char MENU_CONF_DELIM_WITHOUT_SPACE[] = "\r\n"; -MENU_SET *p_bbs_menu; +MENU_SET bbs_menu; +MENU_SET top10_menu; int load_menu(MENU_SET *p_menu_set, const char *conf_file) { @@ -59,6 +59,10 @@ int load_menu(MENU_SET *p_menu_set, cons int proj_id; key_t key; size_t size; + int retry_cnt; + + // Initialize the data structure + memset(p_menu_set, 0, sizeof(*p_menu_set)); // Use trie_dict to search menu_id by menu name p_menu_set->p_menu_name_dict = trie_dict_create(); @@ -78,40 +82,55 @@ int load_menu(MENU_SET *p_menu_set, cons if ((fin = fopen(conf_file, "r")) == NULL) { - log_error("Open %s failed", conf_file); + log_error("Open %s failed\n", conf_file); return -2; } // Allocate shared memory - proj_id = (int)(time(NULL) % getpid()); - key = ftok(conf_file, proj_id); - if (key == -1) - { - log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id, errno); - return -2; - } - size = MENU_SET_RESERVED_LENGTH + sizeof(MENU) * MAX_MENUS + sizeof(MENU_ITEM) * MAX_MENUITEMS + sizeof(MENU_SCREEN) * MAX_MENUS + MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS; - p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); - if (p_menu_set->shmid == -1) + + proj_id = (int)(time(NULL) % getpid()); + retry_cnt = 0; + + do { - log_error("shmget(size = %d) error (%d)\n", size, errno); - return -3; - } + key = ftok(conf_file, proj_id + retry_cnt); + if (key == -1) + { + log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id + retry_cnt, errno); + return -3; + } + + p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); + if (p_menu_set->shmid == -1) + { + if (errno != EEXIST || retry_cnt + 1 >= MENU_SHMGET_RETRY_LIMIT) + { + log_error("shmget(conf_file=%s, size=%d) error (%d) %d times\n", + conf_file, size, errno, retry_cnt + 1); + return -3; + } + log_error("shmget(conf_file=%s, proj_id=%d, key=0x%x, size=%d) error (%d), retry ...\n", + conf_file, proj_id + retry_cnt, key, size, errno); + retry_cnt++; + } + } while (p_menu_set->shmid == -1); + p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0); if (p_menu_set->p_reserved == (void *)-1) { log_error("shmat() error (%d)\n", errno); return -3; } - p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH; - p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS; - p_menu_set->p_menu_screen_pool = p_menu_set->p_menu_item_pool + sizeof(MENU_ITEM) * MAX_MENUITEMS; - p_menu_set->p_menu_screen_buf = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS; + + p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH; + p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS; + p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS; + p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS; p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf; p_menu_set->menu_count = 0; @@ -119,6 +138,8 @@ int load_menu(MENU_SET *p_menu_set, cons p_menu_set->menu_screen_count = 0; p_menu_set->choose_step = 0; p_menu_set->menu_id_path[0] = 0; + p_menu_set->menu_item_pos[0] = 0; + p_menu_set->allow_exit = 0; while (fgets(buffer, sizeof(buffer), fin)) { @@ -980,7 +1001,16 @@ int display_menu(MENU_SET *p_menu_set) p_menu_set->choose_step--; return REDRAW; } - return EXITBBS; + return EXITMENU; + } + + if (p_menu->item_count <= 0) // empty menu + { + moveto(p_menu->screen_row, p_menu->screen_col); + clrtoeol(); + prints("没有可选项"); + press_any_key(); + return -1; } menu_item_pos = p_menu_set->menu_item_pos[p_menu_set->choose_step]; @@ -989,7 +1019,7 @@ int display_menu(MENU_SET *p_menu_set) if (p_menu_item == NULL) { log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); - menu_item_pos = 0; + return EXITMENU; } if (menu_item_pos > 0 && @@ -1052,7 +1082,10 @@ int display_menu(MENU_SET *p_menu_set) if (!menu_selectable) { - log_error("No selectable menu item in current menu (%s)\n", p_menu->name); + moveto(p_menu->screen_row, p_menu->screen_col); + clrtoeol(); + prints("没有可选项"); + press_any_key(); return -1; } @@ -1097,7 +1130,9 @@ int menu_control(MENU_SET *p_menu_set, i if (p_menu->item_count == 0) { +#ifdef _DEBUG log_error("Empty menu (%s)\n", p_menu->name); +#endif if (p_menu_set->choose_step > 0) { p_menu_set->choose_step--; @@ -1121,7 +1156,6 @@ int menu_control(MENU_SET *p_menu_set, i switch (key) { case CR: - igetch_reset(); case KEY_RIGHT: if (p_menu_item->submenu) { @@ -1148,17 +1182,15 @@ int menu_control(MENU_SET *p_menu_set, i if (p_menu_set->choose_step > 0) { p_menu_set->choose_step--; - if (p_menu_set->choose_step == 0) - { - return REDRAW; - } - if (display_menu(p_menu_set) != 0) - { - return menu_control(p_menu_set, KEY_LEFT); - } + return REDRAW; } else { + if (p_menu_set->allow_exit) + { + return EXITMENU; + } + display_menu_cursor(p_menu_set, 0); menu_item_pos = p_menu->item_count - 1; while (menu_item_pos >= 0) @@ -1357,7 +1389,7 @@ int unload_menu(MENU_SET *p_menu_set) detach_menu_shm(p_menu_set); - if (shmctl(shmid, IPC_RMID, NULL) == -1) + if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1) { log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); return -1; @@ -1365,6 +1397,31 @@ int unload_menu(MENU_SET *p_menu_set) return 0; } + +int get_menu_shm_readonly(MENU_SET *p_menu_set) +{ + void *p_shm; + + p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY); + if (p_shm == (void *)-1) + { + log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno); + return -1; + } + + p_menu_set->p_reserved = p_shm; + p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH; + p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS; + p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS; + p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS; + p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf; + + p_menu_set->choose_step = 0; + p_menu_set->menu_id_path[0] = 0; + p_menu_set->menu_item_pos[0] = 0; + + return 0; +} int set_menu_shm_readonly(MENU_SET *p_menu_set) {