/[LeafOK_CVS]/lbbs/src/menu.c
ViewVC logotype

Diff of /lbbs/src/menu.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.62 by sysadm, Fri May 30 02:57:09 2025 UTC Revision 1.85 by sysadm, Tue Nov 11 00:28:05 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    menu.c  -  description  /*
3                                                           -------------------   * menu
4          Copyright            : (C) 2004-2025 by Leaflet   *   - configurable user interactive menu feature
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "bbs_cmd.h"  #include "bbs_cmd.h"
 #include "user_priv.h"  
15  #include "bbs_cmd.h"  #include "bbs_cmd.h"
16  #include "menu.h"  #include "common.h"
 #include "log.h"  
17  #include "io.h"  #include "io.h"
18    #include "log.h"
19    #include "menu.h"
20  #include "screen.h"  #include "screen.h"
21  #include "common.h"  #include "user_priv.h"
 #include <string.h>  
 #include <stdio.h>  
22  #include <ctype.h>  #include <ctype.h>
 #include <stdlib.h>  
23  #include <errno.h>  #include <errno.h>
24    #include <stdio.h>
25    #include <stdlib.h>
26    #include <string.h>
27  #include <unistd.h>  #include <unistd.h>
 #include <sys/shm.h>  
28  #include <sys/ipc.h>  #include <sys/ipc.h>
29    #include <sys/shm.h>
30    
31  #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"  enum _menu_constant_t
32  #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"  {
33  #define MENU_CONF_DELIM_WITHOUT_SPACE "\r\n"          MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4,
34            MENU_SHMGET_RETRY_LIMIT = 10,
35    };
36    
37  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)  static const char MENU_CONF_DELIM_WITH_SPACE[] = " ,\t\r\n";
38    static const char MENU_CONF_DELIM_WITHOUT_SPACE[] = "\r\n";
39    
40  MENU_SET *p_bbs_menu;  MENU_SET bbs_menu;
41    MENU_SET top10_menu;
42    
43  int load_menu(MENU_SET *p_menu_set, const char *conf_file)  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
44  {  {
# Line 59  int load_menu(MENU_SET *p_menu_set, cons Line 59  int load_menu(MENU_SET *p_menu_set, cons
59          int proj_id;          int proj_id;
60          key_t key;          key_t key;
61          size_t size;          size_t size;
62            int retry_cnt;
63    
64            // Initialize the data structure
65            memset(p_menu_set, 0, sizeof(*p_menu_set));
66    
67          // Use trie_dict to search menu_id by menu name          // Use trie_dict to search menu_id by menu name
68          p_menu_set->p_menu_name_dict = trie_dict_create();          p_menu_set->p_menu_name_dict = trie_dict_create();
# Line 78  int load_menu(MENU_SET *p_menu_set, cons Line 82  int load_menu(MENU_SET *p_menu_set, cons
82    
83          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
84          {          {
85                  log_error("Open %s failed", conf_file);                  log_error("Open %s failed\n", conf_file);
86                  return -2;                  return -2;
87          }          }
88    
89          // Allocate shared memory          // 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;  
         }  
   
90          size = MENU_SET_RESERVED_LENGTH +          size = MENU_SET_RESERVED_LENGTH +
91                     sizeof(MENU) * MAX_MENUS +                     sizeof(MENU) * MAX_MENUS +
92                     sizeof(MENU_ITEM) * MAX_MENUITEMS +                     sizeof(MENU_ITEM) * MAX_MENUITEMS +
93                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
94                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
95          p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
96          if (p_menu_set->shmid == -1)          proj_id = (int)(time(NULL) % getpid());
97            retry_cnt = 0;
98    
99            do
100          {          {
101                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  key = ftok(conf_file, proj_id + retry_cnt);
102                  return -3;                  if (key == -1)
103          }                  {
104                            log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id + retry_cnt, errno);
105                            return -3;
106                    }
107    
108                    p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
109                    if (p_menu_set->shmid == -1)
110                    {
111                            if (errno != EEXIST || retry_cnt + 1 >= MENU_SHMGET_RETRY_LIMIT)
112                            {
113                                    log_error("shmget(conf_file=%s, size=%d) error (%d) %d times\n",
114                                                      conf_file, size, errno, retry_cnt + 1);
115                                    return -3;
116                            }
117                            log_error("shmget(conf_file=%s, proj_id=%d, key=0x%x, size=%d) error (%d), retry ...\n",
118                                              conf_file, proj_id + retry_cnt, key, size, errno);
119                            retry_cnt++;
120                    }
121            } while (p_menu_set->shmid == -1);
122    
123          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);
124          if (p_menu_set->p_reserved == (void *)-1)          if (p_menu_set->p_reserved == (void *)-1)
125          {          {
126                  log_error("shmat() error (%d)\n", errno);                  log_error("shmat() error (%d)\n", errno);
127                  return -3;                  return -3;
128          }          }
129          p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH;  
130          p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS;          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
131          p_menu_set->p_menu_screen_pool = p_menu_set->p_menu_item_pool + sizeof(MENU_ITEM) * MAX_MENUITEMS;          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
132          p_menu_set->p_menu_screen_buf = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS;          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
133            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
134          p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;          p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;
135    
136          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
# Line 119  int load_menu(MENU_SET *p_menu_set, cons Line 138  int load_menu(MENU_SET *p_menu_set, cons
138          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
139          p_menu_set->choose_step = 0;          p_menu_set->choose_step = 0;
140          p_menu_set->menu_id_path[0] = 0;          p_menu_set->menu_id_path[0] = 0;
141            p_menu_set->menu_item_pos[0] = 0;
142            p_menu_set->allow_exit = 0;
143    
144          while (fgets(buffer, sizeof(buffer), fin))          while (fgets(buffer, sizeof(buffer), fin))
145          {          {
# Line 980  int display_menu(MENU_SET *p_menu_set) Line 1001  int display_menu(MENU_SET *p_menu_set)
1001                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
1002                          return REDRAW;                          return REDRAW;
1003                  }                  }
1004                  return EXITBBS;                  return EXITMENU;
1005            }
1006    
1007            if (p_menu->item_count <= 0) // empty menu
1008            {
1009                    moveto(p_menu->screen_row, p_menu->screen_col);
1010                    clrtoeol();
1011                    prints("没有可选项");
1012                    press_any_key();
1013                    return -1;
1014          }          }
1015    
1016          menu_item_pos = p_menu_set->menu_item_pos[p_menu_set->choose_step];          menu_item_pos = p_menu_set->menu_item_pos[p_menu_set->choose_step];
# Line 989  int display_menu(MENU_SET *p_menu_set) Line 1019  int display_menu(MENU_SET *p_menu_set)
1019          if (p_menu_item == NULL)          if (p_menu_item == NULL)
1020          {          {
1021                  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\n", menu_item_id);
1022                  menu_item_pos = 0;                  return EXITMENU;
1023          }          }
1024    
1025          if (menu_item_pos > 0 &&          if (menu_item_pos > 0 &&
# Line 1052  int display_menu(MENU_SET *p_menu_set) Line 1082  int display_menu(MENU_SET *p_menu_set)
1082    
1083          if (!menu_selectable)          if (!menu_selectable)
1084          {          {
1085                  log_error("No selectable menu item in current menu (%s)\n", p_menu->name);                  moveto(p_menu->screen_row, p_menu->screen_col);
1086                    clrtoeol();
1087                    prints("没有可选项");
1088                    press_any_key();
1089                  return -1;                  return -1;
1090          }          }
1091    
# Line 1097  int menu_control(MENU_SET *p_menu_set, i Line 1130  int menu_control(MENU_SET *p_menu_set, i
1130    
1131          if (p_menu->item_count == 0)          if (p_menu->item_count == 0)
1132          {          {
1133    #ifdef _DEBUG
1134                  log_error("Empty menu (%s)\n", p_menu->name);                  log_error("Empty menu (%s)\n", p_menu->name);
1135    #endif
1136                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1137                  {                  {
1138                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 1121  int menu_control(MENU_SET *p_menu_set, i Line 1156  int menu_control(MENU_SET *p_menu_set, i
1156          switch (key)          switch (key)
1157          {          {
1158          case CR:          case CR:
                 igetch_reset();  
1159          case KEY_RIGHT:          case KEY_RIGHT:
1160                  if (p_menu_item->submenu)                  if (p_menu_item->submenu)
1161                  {                  {
# Line 1148  int menu_control(MENU_SET *p_menu_set, i Line 1182  int menu_control(MENU_SET *p_menu_set, i
1182                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1183                  {                  {
1184                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
1185                          if (p_menu_set->choose_step == 0)                          return REDRAW;
                         {  
                                 return REDRAW;  
                         }  
                         if (display_menu(p_menu_set) != 0)  
                         {  
                                 return menu_control(p_menu_set, KEY_LEFT);  
                         }  
1186                  }                  }
1187                  else                  else
1188                  {                  {
1189                            if (p_menu_set->allow_exit)
1190                            {
1191                                    return EXITMENU;
1192                            }
1193    
1194                          display_menu_cursor(p_menu_set, 0);                          display_menu_cursor(p_menu_set, 0);
1195                          menu_item_pos = p_menu->item_count - 1;                          menu_item_pos = p_menu->item_count - 1;
1196                          while (menu_item_pos >= 0)                          while (menu_item_pos >= 0)
# Line 1357  int unload_menu(MENU_SET *p_menu_set) Line 1389  int unload_menu(MENU_SET *p_menu_set)
1389    
1390          detach_menu_shm(p_menu_set);          detach_menu_shm(p_menu_set);
1391    
1392          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
1393          {          {
1394                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
1395                  return -1;                  return -1;
# Line 1365  int unload_menu(MENU_SET *p_menu_set) Line 1397  int unload_menu(MENU_SET *p_menu_set)
1397    
1398          return 0;          return 0;
1399  }  }
1400    
1401    int get_menu_shm_readonly(MENU_SET *p_menu_set)
1402    {
1403            void *p_shm;
1404    
1405            p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY);
1406            if (p_shm == (void *)-1)
1407            {
1408                    log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);
1409                    return -1;
1410            }
1411    
1412            p_menu_set->p_reserved = p_shm;
1413            p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
1414            p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
1415            p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
1416            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
1417            p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;
1418    
1419            p_menu_set->choose_step = 0;
1420            p_menu_set->menu_id_path[0] = 0;
1421            p_menu_set->menu_item_pos[0] = 0;
1422    
1423            return 0;
1424    }
1425    
1426  int set_menu_shm_readonly(MENU_SET *p_menu_set)  int set_menu_shm_readonly(MENU_SET *p_menu_set)
1427  {  {


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1