--- lbbs/src/menu.c 2025/06/21 02:15:18 1.67 +++ lbbs/src/menu.c 2025/11/04 13:49:51 1.80 @@ -1,18 +1,10 @@ -/*************************************************************************** - 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 by Leaflet + */ #include "bbs.h" #include "bbs_cmd.h" @@ -38,7 +30,10 @@ #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4) -MENU_SET *p_bbs_menu; +#define MENU_SHMGET_RETRY_LIMIT 10 + +MENU_SET bbs_menu; +MENU_SET top10_menu; int load_menu(MENU_SET *p_menu_set, const char *conf_file) { @@ -59,6 +54,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 +77,56 @@ 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, errno); + return -2; + } + + 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); + break; + } + 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 +134,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 +997,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 +1015,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 && @@ -1054,7 +1080,7 @@ int display_menu(MENU_SET *p_menu_set) { moveto(p_menu->screen_row, p_menu->screen_col); clrtoeol(); - prints("ûпѡ"); + prints("没有可选项"); press_any_key(); return -1; } @@ -1100,7 +1126,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--; @@ -1124,7 +1152,6 @@ int menu_control(MENU_SET *p_menu_set, i switch (key) { case CR: - igetch_reset(); case KEY_RIGHT: if (p_menu_item->submenu) { @@ -1151,17 +1178,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) @@ -1368,6 +1393,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) {