--- lbbs/src/menu.c 2025/10/29 05:30:05 1.77 +++ lbbs/src/menu.c 2026/02/13 12:38:09 1.96 @@ -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-2026 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "bbs.h" #include "bbs_cmd.h" @@ -25,26 +21,37 @@ #include "user_priv.h" #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" +#include +#include -#define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4) +enum _menu_constant_t +{ + MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4, +}; -#define MENU_SHMGET_RETRY_LIMIT 3 +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; +// External definitions for inline functions +extern inline MENU *get_menu(MENU_SET *p_menu_set, const char *menu_name); +extern inline MENU *get_menu_by_id(MENU_SET *p_menu_set, MENU_ID menu_id); +extern inline MENU_ITEM *get_menu_item_by_id(MENU_SET *p_menu_set, MENU_ITEM_ID menu_item_id); +extern inline MENU_SCREEN *get_menu_screen_by_id(MENU_SET *p_menu_set, MENU_SCREEN_ID menu_screen_id); + 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]; @@ -59,10 +66,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; - int retry_cnt; + + if (p_menu_set == NULL || conf_file == NULL) + { + log_error("NULL pointer error"); + return -1; + } // Initialize the data structure memset(p_menu_set, 0, sizeof(*p_menu_set)); @@ -71,21 +80,21 @@ int load_menu(MENU_SET *p_menu_set, cons p_menu_set->p_menu_name_dict = trie_dict_create(); if (p_menu_set->p_menu_name_dict == NULL) { - log_error("trie_dict_create() error\n"); - return -3; + log_error("trie_dict_create() error"); + return -1; } // Use trie_dict to search screen_id by menu screen name p_menu_set->p_menu_screen_dict = trie_dict_create(); if (p_menu_set->p_menu_screen_dict == NULL) { - log_error("trie_dict_create() error\n"); - return -3; + log_error("trie_dict_create() error"); + return -1; } if ((fin = fopen(conf_file, "r")) == NULL) { - log_error("Open %s failed\n", conf_file); + log_error("Open %s error: %d", conf_file, errno); return -2; } @@ -96,43 +105,45 @@ int load_menu(MENU_SET *p_menu_set, cons sizeof(MENU_SCREEN) * MAX_MENUS + MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS; - proj_id = (int)(time(NULL) % getpid()); - retry_cnt = 0; + 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)); - do + if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT) { - 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; - } + log_error("shm_unlink(%s) error (%d)", p_menu_set->shm_name, errno); + return -2; + } - p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); + if ((fd = shm_open(p_menu_set->shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) + { + log_error("shm_open(%s) error (%d)", p_menu_set->shm_name, errno); + return -2; + } + if (ftruncate(fd, (off_t)size) == -1) + { + log_error("ftruncate(size=%d) error (%d)", size, errno); + close(fd); + return -2; + } - 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; - } -#ifdef _DEBUG - 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); -#endif - retry_cnt++; - } - } while (p_menu_set->shmid == -1); + p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L); + if (p_shm == MAP_FAILED) + { + log_error("mmap() error (%d)", errno); + close(fd); + return -2; + } - p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0); - if (p_menu_set->p_reserved == (void *)-1) + if (close(fd) < 0) { - log_error("shmat() error (%d)\n", errno); - return -3; + log_error("close(fd) error (%d)", errno); + return -1; } + 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; @@ -170,13 +181,13 @@ int load_menu(MENU_SET *p_menu_set, cons { if (p_menu != NULL) { - log_error("Incomplete menu definition in menu config line %d\n", fin_line); + log_error("Incomplete menu definition in menu config line %d", fin_line); return -1; } if (p_menu_set->menu_count >= MAX_MENUS) { - log_error("Menu count (%d) exceed limit (%d)\n", p_menu_set->menu_count, MAX_MENUS); + log_error("Menu count (%d) exceed limit (%d)", p_menu_set->menu_count, MAX_MENUS); return -3; } menu_id = (MENU_ID)p_menu_set->menu_count; @@ -194,23 +205,23 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu name in menu config line %d\n", fin_line); + log_error("Error menu name in menu config line %d", fin_line); return -1; } p = q; - while (isalnum(*q) || *q == '_' || *q == '-') + while (isalnum((int)*q) || *q == '_' || *q == '-') { q++; } if (*q != '\0') { - log_error("Error menu name in menu config line %d\n", fin_line); + log_error("Error menu name in menu config line %d", fin_line); return -1; } if (q - p > sizeof(p_menu->name) - 1) { - log_error("Too longer menu name in menu config line %d\n", fin_line); + log_error("Too longer menu name in menu config line %d", fin_line); return -1; } strncpy(p_menu->name, p, sizeof(p_menu->name) - 1); @@ -218,14 +229,14 @@ int load_menu(MENU_SET *p_menu_set, cons if (trie_dict_set(p_menu_set->p_menu_name_dict, p_menu->name, (int64_t)menu_id) != 1) { - log_error("Error set menu dict [%s]\n", p_menu->name); + log_error("Error set menu dict [%s]", p_menu->name); } // Check syntax q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q != NULL) { - log_error("Unknown extra content in menu config line %d\n", fin_line); + log_error("Unknown extra content in menu config line %d", fin_line); return -1; } @@ -254,12 +265,12 @@ int load_menu(MENU_SET *p_menu_set, cons // BEGIN of menu item if (p_menu->item_count >= MAX_ITEMS_PER_MENU) { - log_error("Menuitem count per menu (%d) exceed limit (%d)\n", p_menu->item_count, MAX_ITEMS_PER_MENU); + log_error("Menuitem count per menu (%d) exceed limit (%d)", p_menu->item_count, MAX_ITEMS_PER_MENU); return -1; } if (p_menu_set->menu_item_count >= MAX_MENUITEMS) { - log_error("Menuitem count (%d) exceed limit (%d)\n", p_menu_set->menu_item_count, MAX_MENUITEMS); + log_error("Menuitem count (%d) exceed limit (%d)", p_menu_set->menu_item_count, MAX_MENUITEMS); return -3; } menu_item_id = (MENU_ITEM_ID)p_menu_set->menu_item_count; @@ -281,20 +292,20 @@ int load_menu(MENU_SET *p_menu_set, cons else { q = p; - while (isalnum(*q) || *q == '_' || *q == '-') + while (isalnum((int)*q) || *q == '_' || *q == '-') { q++; } if (*q != '\0') { - log_error("Error menu item action in menu config line %d\n", fin_line); + log_error("Error menu item action in menu config line %d", fin_line); return -1; } } if (q - p > sizeof(p_menu_item->action) - 1) { - log_error("Too longer menu action in menu config line %d\n", fin_line); + log_error("Too longer menu action in menu config line %d", fin_line); return -1; } strncpy(p_menu_item->action, p, sizeof(p_menu_item->action) - 1); @@ -304,17 +315,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu item row in menu config line %d\n", fin_line); + log_error("Error menu item row in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu item row in menu config line %d\n", fin_line); + log_error("Error menu item row in menu config line %d", fin_line); return -1; } p_menu_item->row = (int16_t)atoi(p); @@ -323,17 +334,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu item col in menu config line %d\n", fin_line); + log_error("Error menu item col in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu item col in menu config line %d\n", fin_line); + log_error("Error menu item col in menu config line %d", fin_line); return -1; } p_menu_item->col = (int16_t)atoi(p); @@ -342,17 +353,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu item priv in menu config line %d\n", fin_line); + log_error("Error menu item priv in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu item priv in menu config line %d\n", fin_line); + log_error("Error menu item priv in menu config line %d", fin_line); return -1; } p_menu_item->priv = atoi(p); @@ -361,17 +372,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu item level in menu config line %d\n", fin_line); + log_error("Error menu item level in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu item level in menu config line %d\n", fin_line); + log_error("Error menu item level in menu config line %d", fin_line); return -1; } p_menu_item->level = atoi(p); @@ -380,7 +391,7 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL || *q != '\"') { - log_error("Error menu item name in menu config line %d\n", fin_line); + log_error("Error menu item name in menu config line %d", fin_line); return -1; } q++; @@ -400,14 +411,14 @@ int load_menu(MENU_SET *p_menu_set, cons } if (*q != '\"' || *(q + 1) != '\0') { - log_error("Error menu item name in menu config line %d\n", fin_line); + log_error("Error menu item name in menu config line %d", fin_line); return -1; } *q = '\0'; if (q - p > sizeof(p_menu_item->name) - 1) { - log_error("Too longer menu name in menu config line %d\n", fin_line); + log_error("Too longer menu name in menu config line %d", fin_line); return -1; } strncpy(p_menu_item->name, p, sizeof(p_menu_item->name) - 1); @@ -417,7 +428,7 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr); if (q == NULL || (q = strchr(q, '\"')) == NULL) { - log_error("Error menu item text in menu config line %d\n", fin_line); + log_error("Error menu item text in menu config line %d", fin_line); return -1; } q++; @@ -437,14 +448,14 @@ int load_menu(MENU_SET *p_menu_set, cons } if (*q != '\"') { - log_error("Error menu item text in menu config line %d\n", fin_line); + log_error("Error menu item text in menu config line %d", fin_line); return -1; } *q = '\0'; if (q - p > sizeof(p_menu_item->text) - 1) { - log_error("Too longer menu item text in menu config line %d\n", fin_line); + log_error("Too longer menu item text in menu config line %d", fin_line); return -1; } strncpy(p_menu_item->text, p, sizeof(p_menu_item->text) - 1); @@ -454,7 +465,7 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q != NULL) { - log_error("Unknown extra content in menu config line %d\n", fin_line); + log_error("Unknown extra content in menu config line %d", fin_line); return -1; } } @@ -466,17 +477,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu title row in menu config line %d\n", fin_line); + log_error("Error menu title row in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu title row in menu config line %d\n", fin_line); + log_error("Error menu title row in menu config line %d", fin_line); return -1; } p_menu->title.row = (int16_t)atoi(p); @@ -485,17 +496,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu title col in menu config line %d\n", fin_line); + log_error("Error menu title col in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu title col in menu config line %d\n", fin_line); + log_error("Error menu title col in menu config line %d", fin_line); return -1; } p_menu->title.col = (int16_t)atoi(p); @@ -504,7 +515,7 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr); if (q == NULL || (q = strchr(q, '\"')) == NULL) { - log_error("Error menu title text in menu config line %d\n", fin_line); + log_error("Error menu title text in menu config line %d", fin_line); return -1; } q++; @@ -524,14 +535,14 @@ int load_menu(MENU_SET *p_menu_set, cons } if (*q != '\"') { - log_error("Error menu title text in menu config line %d\n", fin_line); + log_error("Error menu title text in menu config line %d", fin_line); return -1; } *q = '\0'; if (q - p > sizeof(p_menu->title.text) - 1) { - log_error("Too longer menu title text in menu config line %d\n", fin_line); + log_error("Too longer menu title text in menu config line %d", fin_line); return -1; } strncpy(p_menu->title.text, p, sizeof(p_menu->title.text) - 1); @@ -541,7 +552,7 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q != NULL) { - log_error("Unknown extra content in menu config line %d\n", fin_line); + log_error("Unknown extra content in menu config line %d", fin_line); return -1; } } @@ -553,17 +564,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu screen row in menu config line %d\n", fin_line); + log_error("Error menu screen row in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu screen row in menu config line %d\n", fin_line); + log_error("Error menu screen row in menu config line %d", fin_line); return -1; } p_menu->screen_row = (int16_t)atoi(p); @@ -572,17 +583,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu screen col in menu config line %d\n", fin_line); + log_error("Error menu screen col in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu screen col in menu config line %d\n", fin_line); + log_error("Error menu screen col in menu config line %d", fin_line); return -1; } p_menu->screen_col = (int16_t)atoi(p); @@ -591,17 +602,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu screen name in menu config line %d\n", fin_line); + log_error("Error menu screen name in menu config line %d", fin_line); return -1; } p = q; - while (isalnum(*q) || *q == '_' || *q == '-') + while (isalnum((int)*q) || *q == '_' || *q == '-') { q++; } if (*q != '\0') { - log_error("Error menu screen name in menu config line %d\n", fin_line); + log_error("Error menu screen name in menu config line %d", fin_line); return -1; } strncpy(p_menu->screen_name, p, sizeof(p_menu->screen_name) - 1); @@ -611,7 +622,7 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q != NULL) { - log_error("Unknown extra content in menu config line %d\n", fin_line); + log_error("Unknown extra content in menu config line %d", fin_line); return -1; } } @@ -621,17 +632,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu page row in menu config line %d\n", fin_line); + log_error("Error menu page row in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu page row in menu config line %d\n", fin_line); + log_error("Error menu page row in menu config line %d", fin_line); return -1; } p_menu->page_row = (int16_t)atoi(p); @@ -640,17 +651,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu page col in menu config line %d\n", fin_line); + log_error("Error menu page col in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu page col in menu config line %d\n", fin_line); + log_error("Error menu page col in menu config line %d", fin_line); return -1; } p_menu->page_col = (int16_t)atoi(p); @@ -659,17 +670,17 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q == NULL) { - log_error("Error menu page item limit in menu config line %d\n", fin_line); + log_error("Error menu page item limit in menu config line %d", fin_line); return -1; } p = q; - while (isdigit(*q)) + while (isdigit((int)*q)) { q++; } if (*q != '\0') { - log_error("Error menu page item limit in menu config line %d\n", fin_line); + log_error("Error menu page item limit in menu config line %d", fin_line); return -1; } p_menu->page_item_limit = (int16_t)atoi(p); @@ -678,7 +689,7 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q != NULL) { - log_error("Unknown extra content in menu config line %d\n", fin_line); + log_error("Unknown extra content in menu config line %d", fin_line); return -1; } } @@ -690,7 +701,7 @@ int load_menu(MENU_SET *p_menu_set, cons q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q != NULL) { - log_error("Unknown extra content in menu config line %d\n", fin_line); + log_error("Unknown extra content in menu config line %d", fin_line); return -1; } } @@ -700,7 +711,7 @@ int load_menu(MENU_SET *p_menu_set, cons { if (p_menu_set->menu_item_count >= MAX_MENUS) { - log_error("Menu screen count (%d) exceed limit (%d)\n", p_menu_set->menu_screen_count, MAX_MENUS); + log_error("Menu screen count (%d) exceed limit (%d)", p_menu_set->menu_screen_count, MAX_MENUS); return -3; } screen_id = (MENU_SCREEN_ID)p_menu_set->menu_screen_count; @@ -709,13 +720,13 @@ 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++; } if (*q != '\0') { - log_error("Error menu screen name in menu config line %d\n", fin_line); + log_error("Error menu screen name in menu config line %d", fin_line); return -1; } strncpy(p_screen->name, p, sizeof(p_screen->name) - 1); @@ -723,14 +734,14 @@ int load_menu(MENU_SET *p_menu_set, cons if (trie_dict_set(p_menu_set->p_menu_screen_dict, p_screen->name, (int64_t)screen_id) != 1) { - log_error("Error set menu screen dict [%s]\n", p_screen->name); + log_error("Error set menu screen dict [%s]", p_screen->name); } // Check syntax q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr); if (q != NULL) { - log_error("Unknown extra content in menu config line %d\n", fin_line); + log_error("Unknown extra content in menu config line %d", fin_line); return -1; } @@ -751,7 +762,7 @@ int load_menu(MENU_SET *p_menu_set, cons { if (p_menu_set->p_menu_screen_buf_free + 1 > q) { - log_error("Menu screen buffer depleted (%p + 1 > %p)\n", p_menu_set->p_menu_screen_buf_free, q); + log_error("Menu screen buffer depleted (%p + 1 > %p)", p_menu_set->p_menu_screen_buf_free, q); return -3; } @@ -764,7 +775,7 @@ int load_menu(MENU_SET *p_menu_set, cons // Clear line if (p_menu_set->p_menu_screen_buf_free + strlen(CTRL_SEQ_CLR_LINE) > q) { - log_error("Menu screen buffer depleted (%p + %d > %p)\n", p_menu_set->p_menu_screen_buf_free, q, strlen(CTRL_SEQ_CLR_LINE)); + log_error("Menu screen buffer depleted (%p + %d > %p)", p_menu_set->p_menu_screen_buf_free, q, strlen(CTRL_SEQ_CLR_LINE)); return -3; } p_menu_set->p_menu_screen_buf_free = stpcpy(p_menu_set->p_menu_screen_buf_free, CTRL_SEQ_CLR_LINE); @@ -774,7 +785,7 @@ int load_menu(MENU_SET *p_menu_set, cons { if (p_menu_set->p_menu_screen_buf_free + 2 > q) { - log_error("Menu screen buffer depleted (%p + 2 > %p)\n", p_menu_set->p_menu_screen_buf_free, q); + log_error("Menu screen buffer depleted (%p + 2 > %p)", p_menu_set->p_menu_screen_buf_free, q); return -3; } @@ -792,13 +803,13 @@ int load_menu(MENU_SET *p_menu_set, cons if (p_screen->buf_length == -1) { - log_error("End of menu screen [%s] not found\n", p_screen->name); + log_error("End of menu screen [%s] not found", p_screen->name); } } } else // Invalid prefix { - log_error("Error in menu config line %d\n", fin_line); + log_error("Error in menu config line %d", fin_line); return -1; } } @@ -810,7 +821,7 @@ int load_menu(MENU_SET *p_menu_set, cons if (trie_dict_get(p_menu_set->p_menu_screen_dict, p_menu->screen_name, (int64_t *)(&(p_menu->screen_id))) != 1) { - log_error("Undefined menu screen [%s]\n", p); + log_error("Undefined menu screen [%s]", p); return -1; } @@ -819,7 +830,7 @@ int load_menu(MENU_SET *p_menu_set, cons { if ((p_menu->filter_handler = get_cmd_handler(p_menu->name)) == NULL) { - log_error("Undefined menu filter handler [%s]\n", p_menu->name); + log_error("Undefined menu filter handler [%s]", p_menu->name); return -1; } } @@ -834,7 +845,7 @@ int load_menu(MENU_SET *p_menu_set, cons { if ((p_menu_item->action_cmd_handler = get_cmd_handler(p_menu_item->action)) == NULL) { - log_error("Undefined menu action cmd handler [%s]\n", p_menu_item->action); + log_error("Undefined menu action cmd handler [%s]", p_menu_item->action); return -1; } } @@ -843,7 +854,7 @@ int load_menu(MENU_SET *p_menu_set, cons { if (trie_dict_get(p_menu_set->p_menu_name_dict, p_menu_item->action, (int64_t *)&menu_id) != 1) { - log_error("Undefined sub menu id [%s]\n", p_menu_item->action); + log_error("Undefined sub menu id [%s]", p_menu_item->action); return -1; } p_menu_item->action_menu_id = menu_id; @@ -870,7 +881,7 @@ int display_menu_cursor(MENU_SET *p_menu p_menu = get_menu_by_id(p_menu_set, menu_id); if (p_menu == NULL) { - log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id); + log_error("get_menu_by_id(%d) return NULL pointer", menu_id); return -1; } @@ -879,7 +890,7 @@ int display_menu_cursor(MENU_SET *p_menu p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); return -1; } @@ -905,7 +916,7 @@ static int display_menu_current_page(MEN p_menu = get_menu_by_id(p_menu_set, menu_id); if (p_menu == NULL) { - log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id); + log_error("get_menu_by_id(%d) return NULL pointer", menu_id); return -1; } @@ -929,7 +940,7 @@ static int display_menu_current_page(MEN p_menu_screen = get_menu_screen_by_id(p_menu_set, p_menu->screen_id); if (p_menu_screen == NULL) { - log_error("get_menu_screen_by_id(%d) return NULL pointer\n", p_menu->screen_id); + log_error("get_menu_screen_by_id(%d) return NULL pointer", p_menu->screen_id); return -1; } @@ -1001,7 +1012,7 @@ int display_menu(MENU_SET *p_menu_set) p_menu = get_menu_by_id(p_menu_set, menu_id); if (p_menu == NULL) { - log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id); + log_error("get_menu_by_id(%d) return NULL pointer", menu_id); if (p_menu_set->choose_step > 0) { p_menu_set->choose_step--; @@ -1024,7 +1035,7 @@ int display_menu(MENU_SET *p_menu_set) p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); return EXITMENU; } @@ -1117,7 +1128,7 @@ int menu_control(MENU_SET *p_menu_set, i if (p_menu_set->menu_count == 0) { - log_error("Empty menu set\n"); + log_error("Empty menu set"); return EXITBBS; } @@ -1125,7 +1136,7 @@ int menu_control(MENU_SET *p_menu_set, i p_menu = get_menu_by_id(p_menu_set, menu_id); if (p_menu == NULL) { - log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id); + log_error("get_menu_by_id(%d) return NULL pointer", menu_id); if (p_menu_set->choose_step > 0) { p_menu_set->choose_step--; @@ -1136,9 +1147,7 @@ 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 + log_debug("Empty menu (%s)", p_menu->name); if (p_menu_set->choose_step > 0) { p_menu_set->choose_step--; @@ -1154,7 +1163,7 @@ int menu_control(MENU_SET *p_menu_set, i p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); p_menu_set->menu_item_pos[p_menu_set->choose_step] = 0; return REDRAW; } @@ -1205,7 +1214,7 @@ int menu_control(MENU_SET *p_menu_set, i p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); return -1; } @@ -1238,7 +1247,7 @@ int menu_control(MENU_SET *p_menu_set, i p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); return -1; } if (require_page_change && p_menu_set->menu_item_page_id[menu_item_pos] != page_id) @@ -1269,7 +1278,7 @@ int menu_control(MENU_SET *p_menu_set, i p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); return -1; } if (require_page_change && p_menu_set->menu_item_page_id[menu_item_pos] != page_id) @@ -1293,7 +1302,7 @@ int menu_control(MENU_SET *p_menu_set, i p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); return -1; } @@ -1320,7 +1329,7 @@ int menu_control(MENU_SET *p_menu_set, i p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); return -1; } @@ -1347,7 +1356,7 @@ int menu_control(MENU_SET *p_menu_set, i p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id); if (p_menu_item == NULL) { - log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id); + log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id); return -1; } @@ -1372,10 +1381,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"); return -1; } @@ -1391,14 +1399,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)", p_menu_set->shm_name, errno); + return -2; } return 0; @@ -1406,16 +1412,49 @@ 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; - p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY); - 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"); return -1; } + if ((fd = shm_open(p_menu_set->shm_name, O_RDONLY, 0600)) == -1) + { + log_error("shm_open(%s) error (%d)", p_menu_set->shm_name, errno); + return -2; + } + + if (fstat(fd, &sb) < 0) + { + log_error("fstat(fd) error (%d)", 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)", errno); + close(fd); + return -2; + } + + if (close(fd) < 0) + { + log_error("close(fd) error (%d)", errno); + return -1; + } + + 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; @@ -1431,23 +1470,29 @@ 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"); return -1; } - p_menu_set->p_reserved = p_shm; + if (p_menu_set->p_reserved != NULL && mprotect(p_menu_set->p_reserved, p_menu_set->shm_size, PROT_READ) < 0) + { + log_error("mprotect() error (%d)", errno); + return -2; + } return 0; } int detach_menu_shm(MENU_SET *p_menu_set) { + if (p_menu_set == NULL) + { + log_error("NULL pointer error"); + return -1; + } + p_menu_set->menu_count = 0; p_menu_set->menu_item_count = 0; p_menu_set->menu_screen_count = 0; @@ -1462,10 +1507,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)", errno); + return -2; } p_menu_set->p_reserved = NULL;