--- lbbs/src/menu.c 2025/10/13 07:13:39 1.73 +++ lbbs/src/menu.c 2025/11/19 15:44:49 1.90 @@ -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" @@ -25,24 +21,31 @@ #include "user_priv.h" #include #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, +}; -#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; int load_menu(MENU_SET *p_menu_set, const char *conf_file) { + char filepath[FILE_PATH_LEN]; + int fd; + size_t size; + void *p_shm; FILE *fin; int fin_line = 0; char buffer[LINE_BUFFER_LEN]; @@ -57,9 +60,12 @@ int load_menu(MENU_SET *p_menu_set, cons MENU_ID menu_id; MENU_ITEM_ID menu_item_id; MENU_SCREEN_ID screen_id; - int proj_id; - key_t key; - size_t size; + + if (p_menu_set == NULL || conf_file == NULL) + { + log_error("NULL pointer error\n"); + return -1; + } // Initialize the data structure memset(p_menu_set, 0, sizeof(*p_menu_set)); @@ -69,7 +75,7 @@ int load_menu(MENU_SET *p_menu_set, cons if (p_menu_set->p_menu_name_dict == NULL) { log_error("trie_dict_create() error\n"); - return -3; + return -1; } // Use trie_dict to search screen_id by menu screen name @@ -77,7 +83,7 @@ int load_menu(MENU_SET *p_menu_set, cons if (p_menu_set->p_menu_screen_dict == NULL) { log_error("trie_dict_create() error\n"); - return -3; + return -1; } if ((fin = fopen(conf_file, "r")) == NULL) @@ -87,35 +93,55 @@ 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) + + strncpy(filepath, conf_file, sizeof(filepath) - 1); + filepath[sizeof(filepath) - 1] = '\0'; + snprintf(p_menu_set->shm_name, sizeof(p_menu_set->shm_name), "/MENU_SHM_%s", basename(filepath)); + + if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT) { - log_error("shmget(size = %d) error (%d)\n", size, errno); - return -3; + log_error("shm_unlink(%s) error (%d)\n", p_menu_set->shm_name, errno); + return -2; } - p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0); - if (p_menu_set->p_reserved == (void *)-1) + + if ((fd = shm_open(p_menu_set->shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) { - log_error("shmat() error (%d)\n", errno); - return -3; + log_error("shm_open(%s) error (%d)\n", p_menu_set->shm_name, errno); + return -2; + } + if (ftruncate(fd, (off_t)size) == -1) + { + log_error("ftruncate(size=%d) error (%d)\n", size, errno); + close(fd); + return -2; + } + + p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); + if (p_shm == MAP_FAILED) + { + log_error("mmap() error (%d)\n", errno); + close(fd); + return -2; + } + + if (close(fd) < 0) + { + log_error("close(fd) error (%d)\n", errno); + return -1; } - 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->shm_size = size; + 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->menu_count = 0; @@ -177,7 +203,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 +286,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 +313,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -306,7 +332,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -325,7 +351,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -344,7 +370,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -449,7 +475,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -468,7 +494,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -536,7 +562,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -555,7 +581,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -574,7 +600,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 +630,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -623,7 +649,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -642,7 +668,7 @@ int load_menu(MENU_SET *p_menu_set, cons return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } @@ -688,7 +714,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 +1015,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 +1167,6 @@ int menu_control(MENU_SET *p_menu_set, i switch (key) { case CR: - igetch_reset(); case KEY_RIGHT: if (p_menu_item->submenu) { @@ -1352,10 +1377,9 @@ int menu_control(MENU_SET *p_menu_set, i int unload_menu(MENU_SET *p_menu_set) { - int shmid; - if (p_menu_set == NULL) { + log_error("NULL pointer error\n"); return -1; } @@ -1371,14 +1395,12 @@ int unload_menu(MENU_SET *p_menu_set) p_menu_set->p_menu_screen_dict = NULL; } - shmid = p_menu_set->shmid; - detach_menu_shm(p_menu_set); - if (shmctl(shmid, IPC_RMID, NULL) == -1) + if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT) { - log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno); - return -1; + log_error("shm_unlink(%s) error (%d)\n", p_menu_set->shm_name, errno); + return -2; } return 0; @@ -1386,20 +1408,53 @@ int unload_menu(MENU_SET *p_menu_set) int get_menu_shm_readonly(MENU_SET *p_menu_set) { + int fd; void *p_shm; + struct stat sb; + size_t size; + + if (p_menu_set == NULL) + { + log_error("NULL pointer error\n"); + return -1; + } + + if ((fd = shm_open(p_menu_set->shm_name, O_RDONLY, 0600)) == -1) + { + log_error("shm_open(%s) error (%d)\n", p_menu_set->shm_name, errno); + return -2; + } + + if (fstat(fd, &sb) < 0) + { + log_error("fstat(fd) error (%d)\n", errno); + close(fd); + return -2; + } + + size = (size_t)sb.st_size; + + p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L); + if (p_shm == MAP_FAILED) + { + log_error("mmap() error (%d)\n", errno); + close(fd); + return -2; + } - p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY); - if (p_shm == (void *)-1) + if (close(fd) < 0) { - log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno); + log_error("close(fd) error (%d)\n", errno); return -1; } + p_menu_set->shm_size = size; 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; @@ -1411,23 +1466,35 @@ int get_menu_shm_readonly(MENU_SET *p_me int set_menu_shm_readonly(MENU_SET *p_menu_set) { - void *p_shm; - - // Remap shared memory in read-only mode - p_shm = shmat(p_menu_set->shmid, p_menu_set->p_reserved, SHM_RDONLY | SHM_REMAP); - if (p_shm == (void *)-1) + if (p_menu_set == NULL) { - log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno); + log_error("NULL pointer error\n"); return -1; } - p_menu_set->p_reserved = p_shm; + if (p_menu_set->p_reserved != NULL && munmap(p_menu_set->p_reserved, p_menu_set->shm_size) < 0) + { + log_error("munmap() error (%d)\n", errno); + return -2; + } + + if (get_menu_shm_readonly(p_menu_set) < 0) + { + log_error("get_menu_shm_readonly() error\n"); + return -3; + } return 0; } int detach_menu_shm(MENU_SET *p_menu_set) { + if (p_menu_set == NULL) + { + log_error("NULL pointer error\n"); + return -1; + } + p_menu_set->menu_count = 0; p_menu_set->menu_item_count = 0; p_menu_set->menu_screen_count = 0; @@ -1442,10 +1509,10 @@ int detach_menu_shm(MENU_SET *p_menu_set p_menu_set->p_menu_name_dict = NULL; p_menu_set->p_menu_screen_dict = NULL; - if (p_menu_set->p_reserved != NULL && shmdt(p_menu_set->p_reserved) == -1) + if (p_menu_set->p_reserved != NULL && munmap(p_menu_set->p_reserved, p_menu_set->shm_size) < 0) { - log_error("shmdt() error (%d)\n", errno); - return -1; + log_error("munmap() error (%d)\n", errno); + return -2; } p_menu_set->p_reserved = NULL;