--- lbbs/src/menu.c 2025/10/18 12:06:10 1.75 +++ lbbs/src/menu.c 2025/12/19 06:16:27 1.93 @@ -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"); + return -1; + } // Initialize the data structure memset(p_menu_set, 0, sizeof(*p_menu_set)); @@ -68,50 +74,70 @@ 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 failed", 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) + + 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(conf_file=%s, proj_id=%d, size = %d) error (%d)\n", conf_file, proj_id, size, errno); - return -3; + log_error("shm_unlink(%s) error (%d)", 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("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; + } + + 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; + } + + 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; @@ -149,13 +175,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; @@ -173,23 +199,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); @@ -197,14 +223,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; } @@ -233,12 +259,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; @@ -260,20 +286,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); @@ -283,17 +309,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); @@ -302,17 +328,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); @@ -321,17 +347,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); @@ -340,17 +366,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); @@ -359,7 +385,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++; @@ -379,14 +405,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); @@ -396,7 +422,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++; @@ -416,14 +442,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); @@ -433,7 +459,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; } } @@ -445,17 +471,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); @@ -464,17 +490,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); @@ -483,7 +509,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++; @@ -503,14 +529,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); @@ -520,7 +546,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; } } @@ -532,17 +558,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); @@ -551,17 +577,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); @@ -570,17 +596,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); @@ -590,7 +616,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; } } @@ -600,17 +626,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); @@ -619,17 +645,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); @@ -638,17 +664,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); @@ -657,7 +683,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; } } @@ -669,7 +695,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; } } @@ -679,7 +705,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; @@ -688,13 +714,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); @@ -702,14 +728,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; } @@ -730,7 +756,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; } @@ -743,7 +769,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); @@ -753,7 +779,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; } @@ -771,13 +797,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; } } @@ -789,7 +815,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; } @@ -798,7 +824,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; } } @@ -813,7 +839,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; } } @@ -822,7 +848,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; @@ -849,7 +875,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; } @@ -858,7 +884,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; } @@ -884,7 +910,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; } @@ -908,7 +934,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; } @@ -980,7 +1006,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--; @@ -1003,7 +1029,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; } @@ -1096,7 +1122,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; } @@ -1104,7 +1130,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--; @@ -1115,9 +1141,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--; @@ -1133,7 +1157,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; } @@ -1184,7 +1208,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; } @@ -1217,7 +1241,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) @@ -1248,7 +1272,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) @@ -1272,7 +1296,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; } @@ -1299,7 +1323,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; } @@ -1326,7 +1350,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; } @@ -1351,10 +1375,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; } @@ -1370,14 +1393,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; @@ -1385,16 +1406,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; @@ -1410,23 +1464,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; @@ -1441,10 +1501,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;