--- lbbs/src/menu.c 2025/10/13 07:13:39 1.73 +++ lbbs/src/menu.c 2025/11/17 12:16:48 1.86 @@ -1,18 +1,14 @@ -/*************************************************************************** - 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" @@ -32,11 +28,14 @@ #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 bbs_menu; MENU_SET top10_menu; @@ -60,6 +59,7 @@ 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)); @@ -87,35 +87,50 @@ int load_menu(MENU_SET *p_menu_set, cons } // 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; @@ -177,7 +192,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isalnum(*q) || *q == '_' || *q == '-') + while (isalnum((int)*q) || *q == '_' || *q == '-') { q++; } @@ -260,7 +275,7 @@ int load_menu(MENU_SET *p_menu_set, cons else { q = p; - while (isalnum(*q) || *q == '_' || *q == '-') + while (isalnum((int)*q) || *q == '_' || *q == '-') { q++; } @@ -287,7 +302,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -306,7 +321,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -325,7 +340,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -344,7 +359,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -449,7 +464,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -468,7 +483,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -536,7 +551,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -555,7 +570,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -574,7 +589,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isalnum(*q) || *q == '_' || *q == '-') + while (isalnum((int)*q) || *q == '_' || *q == '-') { q++; } @@ -604,7 +619,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -623,7 +638,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -642,7 +657,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -688,7 +703,7 @@ int load_menu(MENU_SET *p_menu_set, cons p_screen = get_menu_screen_by_id(p_menu_set, screen_id); q = p; - while (isalnum(*q) || *q == '_' || *q == '-') + while (isalnum((int)*q) || *q == '_' || *q == '-') { q++; } @@ -989,7 +1004,7 @@ int display_menu(MENU_SET *p_menu_set) return EXITMENU; } - if(p_menu->item_count <= 0) // empty menu + if (p_menu->item_count <= 0) // empty menu { moveto(p_menu->screen_row, p_menu->screen_col); clrtoeol(); @@ -1141,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) { @@ -1375,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; @@ -1396,10 +1410,10 @@ int get_menu_shm_readonly(MENU_SET *p_me } p_menu_set->p_reserved = p_shm; - 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->choose_step = 0; @@ -1414,7 +1428,16 @@ int set_menu_shm_readonly(MENU_SET *p_me void *p_shm; // Remap shared memory in read-only mode +#if defined(__MSYS__) || defined(__MINGW32__) + if (shmdt(p_menu_set->p_reserved) == -1) + { + log_error("shmdt() error (%d)\n", errno); + return -1; + } + p_shm = shmat(p_menu_set->shmid, p_menu_set->p_reserved, SHM_RDONLY); +#else p_shm = shmat(p_menu_set->shmid, p_menu_set->p_reserved, SHM_RDONLY | SHM_REMAP); +#endif if (p_shm == (void *)-1) { log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);