--- lbbs/src/menu.c 2025/05/03 13:41:21 1.24 +++ lbbs/src/menu.c 2025/05/14 04:22:45 1.39 @@ -1,16 +1,15 @@ /*************************************************************************** menu.c - description ------------------- - begin : Wed Mar 16 2004 - copyright : (C) 2005 by Leaflet - email : leaflet@leafok.com + 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 2 of the License, or * + * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ @@ -18,7 +17,6 @@ #include "bbs.h" #include "bbs_cmd.h" #include "user_priv.h" -#include "reg_ex.h" #include "bbs_cmd.h" #include "menu.h" #include "log.h" @@ -28,232 +26,527 @@ #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" + MENU_SET bbs_menu; int load_menu(MENU_SET *p_menu_set, const char *conf_file) { FILE *fin, *fout; - int i = 0, j; + int fin_line = 0; + int i = 0; + int j = 0; char buffer[LINE_BUFFER_LEN]; - char screen_filename[LINE_BUFFER_LEN]; char temp[LINE_BUFFER_LEN]; - regmatch_t pmatch[10]; + char screen_filename[FILE_PATH_LEN]; + char *p = NULL; + char *q = NULL; + char *saveptr = NULL; + MENU *p_menu = NULL; + MENU_ITEM *p_item = NULL; + + p_menu_set->menu_count = 0; + p_menu_set->p_menu_name_dict = trie_dict_create(); if ((fin = fopen(conf_file, "r")) == NULL) { log_error("Open %s failed", conf_file); - return -1; + return -2; } - strcpy(p_menu_set->conf_file, conf_file); + strncpy(p_menu_set->conf_file, conf_file, sizeof(p_menu_set->conf_file) - 1); + p_menu_set->conf_file[sizeof(p_menu_set->conf_file) - 1] = '\0'; while (fgets(buffer, sizeof(buffer), fin)) { - switch (buffer[0]) + fin_line++; + + p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr); + if (p == NULL) // Blank line { - case '#': - break; - case '%': - if (ireg("^%S_([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0) - { - strncpy(temp, buffer + pmatch[1].rm_so, - pmatch[1].rm_eo - pmatch[1].rm_so); - temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0'; - sprintf(screen_filename, "%sMENU_SCR_%s", app_temp_dir, temp); + continue; + } - if ((fout = fopen(screen_filename, "w")) == NULL) + if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line + { + continue; + } + + if (*p == '%') + { + p++; + + if (strcmp(p, "menu") == 0) // BEGIN of sub-menu + { + if (p_menu != NULL) { - log_error("Open %s failed", screen_filename); - return -2; + log_error("Incomplete menu definition in menu config line %d\n", fin_line); + return -1; } - - while (fgets(buffer, sizeof(buffer), fin)) + p_menu = (MENU *)malloc(sizeof(MENU)); + if (p_menu == NULL) { - if (buffer[0] != '%') - fputs(buffer, fout); - else - break; + log_error("Unable to allocate memory for menu\n"); + return -3; } + p_menu_set->p_menu[i] = p_menu; + i++; + p_menu_set->menu_count = i; - fclose(fout); - break; - } + j = 0; // Menu item counter + p_menu->item_count = 0; + p_menu->item_cur_pos = 0; + p_menu->title.show = 0; + p_menu->screen.show = 0; - if (ireg("^%menu ([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0) - { - p_menu_set->p_menu[i] = malloc(sizeof(MENU)); - p_menu_set->p_menu[i]->title.show = 0; - p_menu_set->p_menu[i]->screen.show = 0; - - strncpy(p_menu_set->p_menu[i]->name, - buffer + pmatch[1].rm_so, - pmatch[1].rm_eo - pmatch[1].rm_so); - p_menu_set->p_menu[i]->name[pmatch[1].rm_eo - pmatch[1].rm_so] = - '\0'; + 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); + return -1; + } + p = q; + while (isalnum(*q) || *q == '_') + { + q++; + } + if (*q != '\0') + { + log_error("Error menu name in menu config line %d\n", fin_line); + return -1; + } - j = 0; + if (q - p > sizeof(p_menu->name) - 1) + { + log_error("Too longer menu name in menu config line %d\n", fin_line); + return -1; + } + strncpy(p_menu->name, p, sizeof(p_menu->name) - 1); + p_menu->name[sizeof(p_menu->name) - 1] = '\0'; + if (trie_dict_set(p_menu_set->p_menu_name_dict, p_menu->name, (int64_t)p_menu) != 1) + { + log_error("Error set menu dict [%s]\n", 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); + return -1; + } while (fgets(buffer, sizeof(buffer), fin)) { - if (buffer[0] == '#') + fin_line++; + + p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr); + if (p == NULL) // Blank line { continue; } - if (buffer[0] == '%') + + if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line { - p_menu_set->p_menu[i]->item_count = j; - p_menu_set->p_menu[i]->item_cur_pos = 0; - i++; + continue; + } + + if (*p == '%') // END of sub-menu + { + p_menu = NULL; break; } - if (ireg("^!([A-Za-z0-9_.]+)[[:space:]]*([0-9]+)," - "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+)," - "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\"," - "[[:space:]]*\"([^\"]+)\"", - buffer, 8, pmatch) == 0) + else if (*p == '!' || *p == '@') { - p_menu_set->p_menu[i]->items[j] = - malloc(sizeof(MENU_ITEM)); - p_menu_set->p_menu[i]->items[j]->submenu = 1; - strncpy(p_menu_set->p_menu[i]->items[j]->action, - buffer + pmatch[1].rm_so, - pmatch[1].rm_eo - pmatch[1].rm_so); - p_menu_set->p_menu[i]->items[j]->action[pmatch[1].rm_eo - - pmatch[1].rm_so] = '\0'; - strncpy(temp, buffer + pmatch[2].rm_so, - pmatch[2].rm_eo - pmatch[2].rm_so); - temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0'; - p_menu_set->p_menu[i]->items[j]->row = atoi(temp); - strncpy(temp, - buffer + pmatch[3].rm_so, - pmatch[3].rm_eo - pmatch[3].rm_so); - temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0'; - p_menu_set->p_menu[i]->items[j]->col = atoi(temp); - strncpy(temp, - buffer + pmatch[4].rm_so, - pmatch[4].rm_eo - pmatch[4].rm_so); - temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0'; - p_menu_set->p_menu[i]->items[j]->priv = atoi(temp); - strncpy(temp, - buffer + pmatch[5].rm_so, - pmatch[5].rm_eo - pmatch[5].rm_so); - temp[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0'; - p_menu_set->p_menu[i]->items[j]->level = atoi(temp); - strncpy(p_menu_set->p_menu[i]->items[j]->name, - buffer + pmatch[6].rm_so, - pmatch[6].rm_eo - pmatch[6].rm_so); - p_menu_set->p_menu[i]->items[j]->name[pmatch[6].rm_eo - - pmatch[6].rm_so] = - '\0'; - strncpy(p_menu_set->p_menu[i]->items[j]->text, - buffer + pmatch[7].rm_so, - pmatch[7].rm_eo - pmatch[7].rm_so); - p_menu_set->p_menu[i]->items[j]->text[pmatch[7].rm_eo - - pmatch[7].rm_so] = - '\0'; + // BEGIN of menu item + p_item = (MENU_ITEM *)malloc(sizeof(MENU_ITEM)); + if (p_item == NULL) + { + log_error("Unable to allocate memory for menu item\n"); + return -3; + } + p_menu->items[j] = p_item; j++; - continue; + p_menu->item_count = j; + + p_item->submenu = (*p == '!' ? 1 : 0); + + // Menu item action + p++; + if (strcmp(p, "..") == 0) // Return to parent menu + { + q = p + 2; // strlen("..") + } + else + { + q = p; + while (isalnum(*q) || *q == '_') + { + q++; + } + if (*q != '\0') + { + log_error("Error menu item action in menu config line %d\n", fin_line); + return -1; + } + } + + if (q - p > sizeof(p_item->action) - 1) + { + log_error("Too longer menu action in menu config line %d\n", fin_line); + return -1; + } + strncpy(p_item->action, p, sizeof(p_item->action) - 1); + p_item->action[sizeof(p_item->action) - 1] = '\0'; + + // Menu item row + 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); + return -1; + } + p = q; + while (isdigit(*q)) + { + q++; + } + if (*q != '\0') + { + log_error("Error menu item row in menu config line %d\n", fin_line); + return -1; + } + p_item->row = atoi(p); + + // Menu item col + 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); + return -1; + } + p = q; + while (isdigit(*q)) + { + q++; + } + if (*q != '\0') + { + log_error("Error menu item col in menu config line %d\n", fin_line); + return -1; + } + p_item->col = atoi(p); + + // Menu item priv + 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); + return -1; + } + p = q; + while (isdigit(*q)) + { + q++; + } + if (*q != '\0') + { + log_error("Error menu item priv in menu config line %d\n", fin_line); + return -1; + } + p_item->priv = atoi(p); + + // Menu item level + 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); + return -1; + } + p = q; + while (isdigit(*q)) + { + q++; + } + if (*q != '\0') + { + log_error("Error menu item level in menu config line %d\n", fin_line); + return -1; + } + p_item->level = atoi(p); + + // Menu item name + 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); + return -1; + } + q++; + p = q; + while (*q != '\0' && *q != '\"') + { + q++; + } + if (*q != '\"' || *(q + 1) != '\0') + { + log_error("Error menu item name in menu config line %d\n", fin_line); + return -1; + } + *q = '\0'; + + if (q - p > sizeof(p_item->name) - 1) + { + log_error("Too longer menu name in menu config line %d\n", fin_line); + return -1; + } + strncpy(p_item->name, p, sizeof(p_item->name) - 1); + p_item->name[sizeof(p_item->name) - 1] = '\0'; + + // Menu item text + 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); + return -1; + } + q++; + p = q; + while (*q != '\0' && *q != '\"') + { + q++; + } + if (*q != '\"') + { + log_error("Error menu item text in menu config line %d\n", fin_line); + return -1; + } + *q = '\0'; + + if (q - p > sizeof(p_item->text) - 1) + { + log_error("Too longer menu item text in menu config line %d\n", fin_line); + return -1; + } + strncpy(p_item->text, p, sizeof(p_item->text) - 1); + p_item->text[sizeof(p_item->text) - 1] = '\0'; + + // Check syntax + 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); + return -1; + } } - if (ireg("^@([A-Za-z0-9_]+)[[:space:]]*([0-9]+)," - "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+)," - "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\"," - "[[:space:]]*\"([^\"]+)\"", - buffer, 8, pmatch) == 0) + else if (strcmp(p, "title") == 0) { - p_menu_set->p_menu[i]->items[j] = - malloc(sizeof(MENU_ITEM)); - p_menu_set->p_menu[i]->items[j]->submenu = 0; - strncpy(p_menu_set->p_menu[i]->items[j]->action, - buffer + pmatch[1].rm_so, - pmatch[1].rm_eo - pmatch[1].rm_so); - p_menu_set->p_menu[i]->items[j]->action[pmatch[1].rm_eo - - pmatch[1].rm_so] = '\0'; - strncpy(temp, buffer + pmatch[2].rm_so, - pmatch[2].rm_eo - pmatch[2].rm_so); - temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0'; - p_menu_set->p_menu[i]->items[j]->row = atoi(temp); - strncpy(temp, - buffer + pmatch[3].rm_so, - pmatch[3].rm_eo - pmatch[3].rm_so); - temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0'; - p_menu_set->p_menu[i]->items[j]->col = atoi(temp); - strncpy(temp, - buffer + pmatch[4].rm_so, - pmatch[4].rm_eo - pmatch[4].rm_so); - temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0'; - p_menu_set->p_menu[i]->items[j]->priv = atoi(temp); - strncpy(temp, - buffer + pmatch[5].rm_so, - pmatch[5].rm_eo - pmatch[5].rm_so); - temp[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0'; - p_menu_set->p_menu[i]->items[j]->level = atoi(temp); - strncpy(p_menu_set->p_menu[i]->items[j]->name, - buffer + pmatch[6].rm_so, - pmatch[6].rm_eo - pmatch[6].rm_so); - p_menu_set->p_menu[i]->items[j]->name[pmatch[6].rm_eo - - pmatch[6].rm_so] = - '\0'; - strncpy(p_menu_set->p_menu[i]->items[j]->text, - buffer + pmatch[7].rm_so, - pmatch[7].rm_eo - pmatch[7].rm_so); - p_menu_set->p_menu[i]->items[j]->text[pmatch[7].rm_eo - - pmatch[7].rm_so] = - '\0'; - j++; - continue; + p_menu->title.show = 1; + + // Menu title row + 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); + return -1; + } + p = q; + while (isdigit(*q)) + { + q++; + } + if (*q != '\0') + { + log_error("Error menu title row in menu config line %d\n", fin_line); + return -1; + } + p_menu->title.row = atoi(p); + + // Menu title col + 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); + return -1; + } + p = q; + while (isdigit(*q)) + { + q++; + } + if (*q != '\0') + { + log_error("Error menu title col in menu config line %d\n", fin_line); + return -1; + } + p_menu->title.col = atoi(p); + + // Menu title text + 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); + return -1; + } + q++; + p = q; + while (*q != '\0' && *q != '\"') + { + q++; + } + if (*q != '\"') + { + log_error("Error menu title text in menu config line %d\n", fin_line); + return -1; + } + *q = '\0'; + + if (q - p > sizeof(p_item->text) - 1) + { + log_error("Too longer menu title text in menu config line %d\n", fin_line); + return -1; + } + strncpy(p_menu->title.text, p, sizeof(p_menu->title.text) - 1); + p_menu->title.text[sizeof(p_menu->title.text) - 1] = '\0'; + + // Check syntax + 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); + return -1; + } } - if (ireg("^title[[:space:]]*([0-9]+)," - "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\"", - buffer, 4, pmatch) == 0) + else if (strcmp(p, "screen") == 0) { - p_menu_set->p_menu[i]->title.show = 1; - strncpy(temp, - buffer + pmatch[1].rm_so, - pmatch[1].rm_eo - pmatch[1].rm_so); - temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0'; - p_menu_set->p_menu[i]->title.row = atoi(temp); - strncpy(temp, - buffer + pmatch[2].rm_so, - pmatch[2].rm_eo - pmatch[2].rm_so); - temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0'; - p_menu_set->p_menu[i]->title.col = atoi(temp); - strncpy(p_menu_set->p_menu[i]->title.text, - buffer + pmatch[3].rm_so, - pmatch[3].rm_eo - pmatch[3].rm_so); - p_menu_set->p_menu[i]->title.text[pmatch[3].rm_eo - - pmatch[3].rm_so] = - '\0'; - continue; + p_menu->screen.show = 1; + + // Menu screen row + 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); + return -1; + } + p = q; + while (isdigit(*q)) + { + q++; + } + if (*q != '\0') + { + log_error("Error menu screen row in menu config line %d\n", fin_line); + return -1; + } + p_menu->screen.row = atoi(p); + + // Menu screen col + 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); + return -1; + } + p = q; + while (isdigit(*q)) + { + q++; + } + if (*q != '\0') + { + log_error("Error menu screen col in menu config line %d\n", fin_line); + return -1; + } + p_menu->screen.col = atoi(p); + + // Menu screen name + 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); + return -1; + } + p = q; + while (isalnum(*q) || *q == '_') + { + q++; + } + if (*q != '\0') + { + log_error("Error menu screen name in menu config line %d\n", fin_line); + return -1; + } + + snprintf(p_menu->screen.filename, sizeof(p_menu->screen.filename), "%s%s", MENU_SCREEN_PATH_PREFIX, p); + + // 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); + return -1; + } + } + } + } + else // BEGIN of menu screen + { + q = p; + while (isalnum(*q) || *q == '_') + { + q++; + } + if (*q != '\0') + { + log_error("Error menu screen name in menu config line %d\n", fin_line); + return -1; + } + + snprintf(screen_filename, sizeof(screen_filename), "%s%s", MENU_SCREEN_PATH_PREFIX, p); + + // 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); + return -1; + } + + if ((fout = fopen(screen_filename, "w")) == NULL) + { + log_error("Open %s failed", screen_filename); + return -2; + } + + while (fgets(buffer, sizeof(buffer), fin)) + { + fin_line++; + + strncpy(temp, buffer, sizeof(temp)); // Duplicate line for strtok_r + p = strtok_r(temp, MENU_CONF_DELIM_WITH_SPACE, &saveptr); + if (p != NULL && *p == '%') // END of menu screen + { + break; } - if (ireg("^screen[[:space:]]*([0-9]+)," - "[[:space:]]*([0-9]+),[[:space:]]*S_([A-Za-z0-9_]+)", - buffer, 4, pmatch) == 0) + + if (fputs(buffer, fout) < 0) { - p_menu_set->p_menu[i]->screen.show = 1; - strncpy(temp, - buffer + pmatch[1].rm_so, - pmatch[1].rm_eo - pmatch[1].rm_so); - temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0'; - p_menu_set->p_menu[i]->screen.row = atoi(temp); - strncpy(temp, - buffer + pmatch[2].rm_so, - pmatch[2].rm_eo - pmatch[2].rm_so); - temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0'; - p_menu_set->p_menu[i]->screen.col = atoi(temp); - strncpy(temp, - buffer + pmatch[3].rm_so, - pmatch[3].rm_eo - pmatch[3].rm_so); - temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0'; - sprintf(p_menu_set->p_menu[i]->screen.filename, - "%sMENU_SCR_%s", app_temp_dir, temp); - continue; + log_error("Write %s failed", screen_filename); + return -2; } } + + fclose(fout); } - break; + } + else // Invalid prefix + { + log_error("Error in menu config line %d\n", fin_line); + return -1; } } fclose(fin); @@ -265,17 +558,16 @@ int load_menu(MENU_SET *p_menu_set, cons return 0; } -MENU * -get_menu(MENU_SET *p_menu_set, const char *menu_name) +MENU *get_menu(MENU_SET *p_menu_set, const char *menu_name) { - int i; + int ret; + int64_t value = 0; - for (i = 0; i < p_menu_set->menu_count; i++) + ret = trie_dict_get(p_menu_set->p_menu_name_dict, menu_name, &value); + + if (ret == 1) // found { - if (strcmp(p_menu_set->p_menu[i]->name, menu_name) == 0) - { - return p_menu_set->p_menu[i]; - } + return ((MENU *)value); } return NULL; @@ -285,59 +577,84 @@ static void display_menu_cursor(MENU *p_ { moveto((p_menu->items[p_menu->item_cur_pos])->r_row, (p_menu->items[p_menu->item_cur_pos])->r_col - 2); - prints(show ? ">" : " "); + outc(show ? '>' : ' '); iflush(); } int display_menu(MENU *p_menu) { - int i, row, col, menu_selectable = 0; + int row = 0; + int col = 0; + int menu_selectable = 0; if (p_menu == NULL) + { return -1; + } + + if (p_menu->item_cur_pos > 0 && + checkpriv(&BBS_priv, 0, p_menu->items[p_menu->item_cur_pos]->priv) != 0 && + checklevel(&BBS_priv, p_menu->items[p_menu->item_cur_pos]->level) != 0) + { + menu_selectable = 1; + } if (p_menu->title.show) + { show_top(p_menu->title.text); + } if (p_menu->screen.show) { moveto(p_menu->screen.row, p_menu->screen.col); if (display_file(p_menu->screen.filename) != 0) + { log_error("Display menu screen <%s> failed!\n", p_menu->screen.filename); + } } - row = p_menu->items[0]->row; - col = p_menu->items[0]->col; - - for (i = 0; i < p_menu->item_count; i++) + for (int i = 0; i < p_menu->item_count; i++) { + if (p_menu->items[i]->row != 0) + { + row = p_menu->items[i]->row; + } + if (p_menu->items[i]->col != 0) + { + col = p_menu->items[i]->col; + } + if (checkpriv(&BBS_priv, 0, p_menu->items[i]->priv) == 0 || checklevel(&BBS_priv, p_menu->items[i]->level) == 0) { p_menu->items[i]->display = 0; + p_menu->items[i]->r_row = 0; + p_menu->items[i]->r_col = 0; } else { p_menu->items[i]->display = 1; - menu_selectable = 1; + if (!menu_selectable) + { + p_menu->item_cur_pos = i; + menu_selectable = 1; + } - if (p_menu->items[i]->row != 0) - row = p_menu->items[i]->row; - else - row++; p_menu->items[i]->r_row = row; - if (p_menu->items[i]->col != 0) - col = p_menu->items[i]->col; p_menu->items[i]->r_col = col; + moveto(row, col); - prints(p_menu->items[i]->text); - iflush(); + prints("%s", p_menu->items[i]->text); + + row++; } } if (!menu_selectable) + { return -1; + } display_menu_cursor(p_menu, 1); @@ -359,57 +676,68 @@ int menu_control(MENU_SET *p_menu_set, i MENU *p_menu; if (p_menu_set->menu_count == 0) + { return 0; + } p_menu = p_menu_set->p_menu_select[p_menu_set->menu_select_depth]; switch (key) { case CR: - igetch(1); // Cleanup remaining '\n' in the buffer case KEY_RIGHT: if (p_menu->items[p_menu->item_cur_pos]->submenu) { if (strcmp(p_menu->items[p_menu->item_cur_pos]->action, "..") == 0) + { return menu_control(p_menu_set, KEY_LEFT); + } p_menu_set->menu_select_depth++; - p_menu = - p_menu_set->p_menu_select[p_menu_set->menu_select_depth] = - get_menu(p_menu_set, - p_menu->items[p_menu->item_cur_pos]->action); + p_menu = get_menu(p_menu_set, p_menu->items[p_menu->item_cur_pos]->action); + p_menu_set->p_menu_select[p_menu_set->menu_select_depth] = p_menu; + if (display_menu(p_menu) != 0) + { return menu_control(p_menu_set, KEY_LEFT); - break; + } } else { return (exec_cmd(p_menu->items[p_menu->item_cur_pos]->action, p_menu->items[p_menu->item_cur_pos]->name)); } + break; case KEY_LEFT: if (p_menu_set->menu_select_depth > 0) { p_menu_set->menu_select_depth--; if (display_current_menu(p_menu_set) != 0) + { return menu_control(p_menu_set, KEY_LEFT); - break; + } } else { display_menu_cursor(p_menu, 0); p_menu->item_cur_pos = p_menu->item_count - 1; - while (!p_menu->items[p_menu->item_cur_pos]->display || p_menu->items[p_menu->item_cur_pos]->priv != 0 || p_menu->items[p_menu->item_cur_pos]->level != 0) + while (p_menu->item_cur_pos >= 0 && (!p_menu->items[p_menu->item_cur_pos]->display || + p_menu->items[p_menu->item_cur_pos]->priv != 0 || + p_menu->items[p_menu->item_cur_pos]->level != 0)) + { p_menu->item_cur_pos--; + } display_menu_cursor(p_menu, 1); - break; } + break; case KEY_UP: display_menu_cursor(p_menu, 0); do { p_menu->item_cur_pos--; if (p_menu->item_cur_pos < 0) + { p_menu->item_cur_pos = p_menu->item_count - 1; + } } while (!p_menu->items[p_menu->item_cur_pos]->display); display_menu_cursor(p_menu, 1); break; @@ -419,23 +747,14 @@ int menu_control(MENU_SET *p_menu_set, i { p_menu->item_cur_pos++; if (p_menu->item_cur_pos >= p_menu->item_count) + { p_menu->item_cur_pos = 0; + } } while (!p_menu->items[p_menu->item_cur_pos]->display); display_menu_cursor(p_menu, 1); break; default: - for (i = 0; i < p_menu->item_count; i++) - { - if (key == p_menu->items[i]->name[0] && - p_menu->items[i]->display) - { - display_menu_cursor(p_menu, 0); - p_menu->item_cur_pos = i; - display_menu_cursor(p_menu, 1); - return 0; - } - } - if (isalpha(key)) + if (isalnum(key)) { for (i = 0; i < p_menu->item_count; i++) { @@ -449,6 +768,7 @@ int menu_control(MENU_SET *p_menu_set, i } } } + break; } return 0; @@ -457,9 +777,14 @@ int menu_control(MENU_SET *p_menu_set, i void unload_menu(MENU_SET *p_menu_set) { MENU *p_menu; - MENU_ITEM *p_menuitem; int i, j; + if (p_menu_set->p_menu_name_dict != NULL) + { + trie_dict_destroy(p_menu_set->p_menu_name_dict); + p_menu_set->p_menu_name_dict = NULL; + } + for (i = 0; i < p_menu_set->menu_count; i++) { p_menu = p_menu_set->p_menu[i]; @@ -467,7 +792,6 @@ void unload_menu(MENU_SET *p_menu_set) { free(p_menu->items[j]); } - remove(p_menu->screen.filename); free(p_menu); } @@ -480,7 +804,9 @@ int reload_menu(MENU_SET *p_menu_set) int result; char conf_file[FILE_PATH_LEN]; - strncpy(conf_file, p_menu_set->conf_file, sizeof(conf_file)); + strncpy(conf_file, p_menu_set->conf_file, sizeof(conf_file) - 1); + conf_file[sizeof(conf_file) - 1] = '\0'; + unload_menu(p_menu_set); result = load_menu(p_menu_set, conf_file);