--- lbbs/src/menu.c 2025/05/05 14:27:57 1.29 +++ lbbs/src/menu.c 2025/05/09 02:10:01 1.34 @@ -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. * * * ***************************************************************************/ @@ -28,26 +27,35 @@ #include #include #include -#include #include -#define MENU_TEMP_DIR "var" +#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[FILE_PATH_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; if ((fin = fopen(conf_file, "r")) == NULL) { log_error("Open %s failed", conf_file); - return -1; + return -2; } strncpy(p_menu_set->conf_file, conf_file, sizeof(p_menu_set->conf_file) - 1); @@ -55,209 +63,486 @@ int load_menu(MENU_SET *p_menu_set, cons 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, - (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so)); - temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0'; - snprintf(screen_filename, sizeof(screen_filename), "%s/MENU_SCR_%s", MENU_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, - (size_t)(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'; + + // 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 (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line { continue; } - if (buffer[0] == '%') + + if (*p == '%') // END of sub-menu { - p_menu_set->p_menu[i]->item_count = j; - p_menu_set->p_menu[i]->item_cur_pos = 0; - i++; + 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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(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, - (size_t)(pmatch[3].rm_eo - pmatch[3].rm_so)); - temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0'; - snprintf(p_menu_set->p_menu[i]->screen.filename, - sizeof(p_menu_set->p_menu[i]->screen.filename), - "%s/MENU_SCR_%s", MENU_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); @@ -269,8 +554,7 @@ 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; @@ -461,18 +745,7 @@ int menu_control(MENU_SET *p_menu_set, i 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++) { @@ -503,7 +776,6 @@ void unload_menu(MENU_SET *p_menu_set) { free(p_menu->items[j]); } - remove(p_menu->screen.filename); free(p_menu); }