/[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.63 by sysadm, Wed Jun 4 15:09:59 2025 UTC Revision 1.77 by sysadm, Wed Oct 29 05:30:05 2025 UTC
# Line 16  Line 16 
16    
17  #include "bbs.h"  #include "bbs.h"
18  #include "bbs_cmd.h"  #include "bbs_cmd.h"
 #include "user_priv.h"  
19  #include "bbs_cmd.h"  #include "bbs_cmd.h"
20  #include "menu.h"  #include "common.h"
 #include "log.h"  
21  #include "io.h"  #include "io.h"
22    #include "log.h"
23    #include "menu.h"
24  #include "screen.h"  #include "screen.h"
25  #include "common.h"  #include "user_priv.h"
 #include <string.h>  
 #include <stdio.h>  
26  #include <ctype.h>  #include <ctype.h>
 #include <stdlib.h>  
27  #include <errno.h>  #include <errno.h>
28    #include <stdio.h>
29    #include <stdlib.h>
30    #include <string.h>
31  #include <unistd.h>  #include <unistd.h>
 #include <sys/shm.h>  
32  #include <sys/ipc.h>  #include <sys/ipc.h>
33    #include <sys/shm.h>
34    
35  #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"  #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"
36  #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"  #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"
# Line 38  Line 38 
38    
39  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)
40    
41  MENU_SET *p_bbs_menu;  #define MENU_SHMGET_RETRY_LIMIT 3
42    
43    MENU_SET bbs_menu;
44    MENU_SET top10_menu;
45    
46  int load_menu(MENU_SET *p_menu_set, const char *conf_file)  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
47  {  {
# Line 59  int load_menu(MENU_SET *p_menu_set, cons Line 62  int load_menu(MENU_SET *p_menu_set, cons
62          int proj_id;          int proj_id;
63          key_t key;          key_t key;
64          size_t size;          size_t size;
65            int retry_cnt;
66    
67            // Initialize the data structure
68            memset(p_menu_set, 0, sizeof(*p_menu_set));
69    
70          // Use trie_dict to search menu_id by menu name          // Use trie_dict to search menu_id by menu name
71          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 85  int load_menu(MENU_SET *p_menu_set, cons
85    
86          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
87          {          {
88                  log_error("Open %s failed", conf_file);                  log_error("Open %s failed\n", conf_file);
89                  return -2;                  return -2;
90          }          }
91    
92          // 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;  
         }  
   
93          size = MENU_SET_RESERVED_LENGTH +          size = MENU_SET_RESERVED_LENGTH +
94                     sizeof(MENU) * MAX_MENUS +                     sizeof(MENU) * MAX_MENUS +
95                     sizeof(MENU_ITEM) * MAX_MENUITEMS +                     sizeof(MENU_ITEM) * MAX_MENUITEMS +
96                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
97                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
98          p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
99          if (p_menu_set->shmid == -1)          proj_id = (int)(time(NULL) % getpid());
100            retry_cnt = 0;
101    
102            do
103          {          {
104                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  key = ftok(conf_file, proj_id + retry_cnt);
105                  return -3;                  if (key == -1)
106          }                  {
107                            log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id, errno);
108                            return -2;
109                    }
110    
111                    p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
112    
113                    if (p_menu_set->shmid == -1)
114                    {
115                            if (errno != EEXIST || retry_cnt + 1 >= MENU_SHMGET_RETRY_LIMIT)
116                            {
117                                    log_error("shmget(conf_file=%s, size=%d) error (%d) %d times\n",
118                                                      conf_file, size, errno, retry_cnt + 1);
119                                    break;
120                            }
121    #ifdef _DEBUG
122                            log_error("shmget(conf_file=%s, proj_id=%d, key=0x%x, size=%d) error (%d), retry ...\n",
123                                              conf_file, proj_id + retry_cnt, key, size, errno);
124    #endif                                    
125                            retry_cnt++;
126                    }
127            } while (p_menu_set->shmid == -1);
128    
129          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);
130          if (p_menu_set->p_reserved == (void *)-1)          if (p_menu_set->p_reserved == (void *)-1)
131          {          {
132                  log_error("shmat() error (%d)\n", errno);                  log_error("shmat() error (%d)\n", errno);
133                  return -3;                  return -3;
134          }          }
135          p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH;  
136          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;
137          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;
138          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;
139            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
140          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;
141    
142          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
# Line 119  int load_menu(MENU_SET *p_menu_set, cons Line 144  int load_menu(MENU_SET *p_menu_set, cons
144          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
145          p_menu_set->choose_step = 0;          p_menu_set->choose_step = 0;
146          p_menu_set->menu_id_path[0] = 0;          p_menu_set->menu_id_path[0] = 0;
147            p_menu_set->menu_item_pos[0] = 0;
148            p_menu_set->allow_exit = 0;
149    
150          while (fgets(buffer, sizeof(buffer), fin))          while (fgets(buffer, sizeof(buffer), fin))
151          {          {
# Line 980  int display_menu(MENU_SET *p_menu_set) Line 1007  int display_menu(MENU_SET *p_menu_set)
1007                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
1008                          return REDRAW;                          return REDRAW;
1009                  }                  }
1010                  return EXITBBS;                  return EXITMENU;
1011            }
1012    
1013            if (p_menu->item_count <= 0) // empty menu
1014            {
1015                    moveto(p_menu->screen_row, p_menu->screen_col);
1016                    clrtoeol();
1017                    prints("没有可选项");
1018                    press_any_key();
1019                    return -1;
1020          }          }
1021    
1022          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 1025  int display_menu(MENU_SET *p_menu_set)
1025          if (p_menu_item == NULL)          if (p_menu_item == NULL)
1026          {          {
1027                  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);
1028                  menu_item_pos = 0;                  return EXITMENU;
1029          }          }
1030    
1031          if (menu_item_pos > 0 &&          if (menu_item_pos > 0 &&
# Line 1053  int display_menu(MENU_SET *p_menu_set) Line 1089  int display_menu(MENU_SET *p_menu_set)
1089          if (!menu_selectable)          if (!menu_selectable)
1090          {          {
1091                  moveto(p_menu->screen_row, p_menu->screen_col);                  moveto(p_menu->screen_row, p_menu->screen_col);
1092                  prints("ûпѡ");                  clrtoeol();
1093                    prints("没有可选项");
1094                  press_any_key();                  press_any_key();
1095                  return -1;                  return -1;
1096          }          }
# Line 1099  int menu_control(MENU_SET *p_menu_set, i Line 1136  int menu_control(MENU_SET *p_menu_set, i
1136    
1137          if (p_menu->item_count == 0)          if (p_menu->item_count == 0)
1138          {          {
1139    #ifdef _DEBUG
1140                  log_error("Empty menu (%s)\n", p_menu->name);                  log_error("Empty menu (%s)\n", p_menu->name);
1141    #endif
1142                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1143                  {                  {
1144                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 1123  int menu_control(MENU_SET *p_menu_set, i Line 1162  int menu_control(MENU_SET *p_menu_set, i
1162          switch (key)          switch (key)
1163          {          {
1164          case CR:          case CR:
                 igetch_reset();  
1165          case KEY_RIGHT:          case KEY_RIGHT:
1166                  if (p_menu_item->submenu)                  if (p_menu_item->submenu)
1167                  {                  {
# Line 1150  int menu_control(MENU_SET *p_menu_set, i Line 1188  int menu_control(MENU_SET *p_menu_set, i
1188                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1189                  {                  {
1190                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
1191                          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);  
                         }  
1192                  }                  }
1193                  else                  else
1194                  {                  {
1195                            if (p_menu_set->allow_exit)
1196                            {
1197                                    return EXITMENU;
1198                            }
1199    
1200                          display_menu_cursor(p_menu_set, 0);                          display_menu_cursor(p_menu_set, 0);
1201                          menu_item_pos = p_menu->item_count - 1;                          menu_item_pos = p_menu->item_count - 1;
1202                          while (menu_item_pos >= 0)                          while (menu_item_pos >= 0)
# Line 1367  int unload_menu(MENU_SET *p_menu_set) Line 1403  int unload_menu(MENU_SET *p_menu_set)
1403    
1404          return 0;          return 0;
1405  }  }
1406    
1407    int get_menu_shm_readonly(MENU_SET *p_menu_set)
1408    {
1409            void *p_shm;
1410    
1411            p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY);
1412            if (p_shm == (void *)-1)
1413            {
1414                    log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);
1415                    return -1;
1416            }
1417    
1418            p_menu_set->p_reserved = p_shm;
1419            p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
1420            p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
1421            p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
1422            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
1423            p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;
1424    
1425            p_menu_set->choose_step = 0;
1426            p_menu_set->menu_id_path[0] = 0;
1427            p_menu_set->menu_item_pos[0] = 0;
1428    
1429            return 0;
1430    }
1431    
1432  int set_menu_shm_readonly(MENU_SET *p_menu_set)  int set_menu_shm_readonly(MENU_SET *p_menu_set)
1433  {  {


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

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