/[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.64 by sysadm, Mon Jun 16 14:30:44 2025 UTC Revision 1.84 by sysadm, Wed Nov 5 04:19:21 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     */
 /***************************************************************************  
  *                                                                         *  
  *   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.                                   *  
  *                                                                         *  
  ***************************************************************************/  
   
 #define _POSIX_C_SOURCE 200809L  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "bbs_cmd.h"  #include "bbs_cmd.h"
 #include "user_priv.h"  
11  #include "bbs_cmd.h"  #include "bbs_cmd.h"
12  #include "menu.h"  #include "common.h"
 #include "log.h"  
13  #include "io.h"  #include "io.h"
14    #include "log.h"
15    #include "menu.h"
16  #include "screen.h"  #include "screen.h"
17  #include "common.h"  #include "user_priv.h"
 #include <string.h>  
 #include <stdio.h>  
18  #include <ctype.h>  #include <ctype.h>
 #include <stdlib.h>  
19  #include <errno.h>  #include <errno.h>
20    #include <stdio.h>
21    #include <stdlib.h>
22    #include <string.h>
23  #include <unistd.h>  #include <unistd.h>
 #include <sys/shm.h>  
24  #include <sys/ipc.h>  #include <sys/ipc.h>
25    #include <sys/shm.h>
26    
27  #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"  enum _menu_constant_t
28  #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"  {
29  #define MENU_CONF_DELIM_WITHOUT_SPACE "\r\n"          MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4,
30            MENU_SHMGET_RETRY_LIMIT = 10,
31    };
32    
33  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)  static const char MENU_CONF_DELIM_WITH_SPACE[] = " ,\t\r\n";
34    static const char MENU_CONF_DELIM_WITHOUT_SPACE[] = "\r\n";
35    
36  MENU_SET *p_bbs_menu;  MENU_SET bbs_menu;
37    MENU_SET top10_menu;
38    
39  int load_menu(MENU_SET *p_menu_set, const char *conf_file)  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
40  {  {
# Line 61  int load_menu(MENU_SET *p_menu_set, cons Line 55  int load_menu(MENU_SET *p_menu_set, cons
55          int proj_id;          int proj_id;
56          key_t key;          key_t key;
57          size_t size;          size_t size;
58            int retry_cnt;
59    
60            // Initialize the data structure
61            memset(p_menu_set, 0, sizeof(*p_menu_set));
62    
63          // Use trie_dict to search menu_id by menu name          // Use trie_dict to search menu_id by menu name
64          p_menu_set->p_menu_name_dict = trie_dict_create();          p_menu_set->p_menu_name_dict = trie_dict_create();
# Line 80  int load_menu(MENU_SET *p_menu_set, cons Line 78  int load_menu(MENU_SET *p_menu_set, cons
78    
79          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
80          {          {
81                  log_error("Open %s failed", conf_file);                  log_error("Open %s failed\n", conf_file);
82                  return -2;                  return -2;
83          }          }
84    
85          // 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;  
         }  
   
86          size = MENU_SET_RESERVED_LENGTH +          size = MENU_SET_RESERVED_LENGTH +
87                     sizeof(MENU) * MAX_MENUS +                     sizeof(MENU) * MAX_MENUS +
88                     sizeof(MENU_ITEM) * MAX_MENUITEMS +                     sizeof(MENU_ITEM) * MAX_MENUITEMS +
89                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
90                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
91          p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
92          if (p_menu_set->shmid == -1)          proj_id = (int)(time(NULL) % getpid());
93            retry_cnt = 0;
94    
95            do
96          {          {
97                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  key = ftok(conf_file, proj_id + retry_cnt);
98                  return -3;                  if (key == -1)
99          }                  {
100                            log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id + retry_cnt, errno);
101                            return -3;
102                    }
103    
104                    p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
105                    if (p_menu_set->shmid == -1)
106                    {
107                            if (errno != EEXIST || retry_cnt + 1 >= MENU_SHMGET_RETRY_LIMIT)
108                            {
109                                    log_error("shmget(conf_file=%s, size=%d) error (%d) %d times\n",
110                                                      conf_file, size, errno, retry_cnt + 1);
111                                    return -3;
112                            }
113                            log_error("shmget(conf_file=%s, proj_id=%d, key=0x%x, size=%d) error (%d), retry ...\n",
114                                              conf_file, proj_id + retry_cnt, key, size, errno);
115                            retry_cnt++;
116                    }
117            } while (p_menu_set->shmid == -1);
118    
119          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);
120          if (p_menu_set->p_reserved == (void *)-1)          if (p_menu_set->p_reserved == (void *)-1)
121          {          {
122                  log_error("shmat() error (%d)\n", errno);                  log_error("shmat() error (%d)\n", errno);
123                  return -3;                  return -3;
124          }          }
125          p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH;  
126          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;
127          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;
128          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;
129            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
130          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;
131    
132          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
# Line 121  int load_menu(MENU_SET *p_menu_set, cons Line 134  int load_menu(MENU_SET *p_menu_set, cons
134          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
135          p_menu_set->choose_step = 0;          p_menu_set->choose_step = 0;
136          p_menu_set->menu_id_path[0] = 0;          p_menu_set->menu_id_path[0] = 0;
137            p_menu_set->menu_item_pos[0] = 0;
138            p_menu_set->allow_exit = 0;
139    
140          while (fgets(buffer, sizeof(buffer), fin))          while (fgets(buffer, sizeof(buffer), fin))
141          {          {
# Line 982  int display_menu(MENU_SET *p_menu_set) Line 997  int display_menu(MENU_SET *p_menu_set)
997                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
998                          return REDRAW;                          return REDRAW;
999                  }                  }
1000                  return EXITBBS;                  return EXITMENU;
1001            }
1002    
1003            if (p_menu->item_count <= 0) // empty menu
1004            {
1005                    moveto(p_menu->screen_row, p_menu->screen_col);
1006                    clrtoeol();
1007                    prints("没有可选项");
1008                    press_any_key();
1009                    return -1;
1010          }          }
1011    
1012          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 991  int display_menu(MENU_SET *p_menu_set) Line 1015  int display_menu(MENU_SET *p_menu_set)
1015          if (p_menu_item == NULL)          if (p_menu_item == NULL)
1016          {          {
1017                  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);
1018                  menu_item_pos = 0;                  return EXITMENU;
1019          }          }
1020    
1021          if (menu_item_pos > 0 &&          if (menu_item_pos > 0 &&
# Line 1055  int display_menu(MENU_SET *p_menu_set) Line 1079  int display_menu(MENU_SET *p_menu_set)
1079          if (!menu_selectable)          if (!menu_selectable)
1080          {          {
1081                  moveto(p_menu->screen_row, p_menu->screen_col);                  moveto(p_menu->screen_row, p_menu->screen_col);
1082                  prints("ûпѡ");                  clrtoeol();
1083                    prints("没有可选项");
1084                  press_any_key();                  press_any_key();
1085                  return -1;                  return -1;
1086          }          }
# Line 1101  int menu_control(MENU_SET *p_menu_set, i Line 1126  int menu_control(MENU_SET *p_menu_set, i
1126    
1127          if (p_menu->item_count == 0)          if (p_menu->item_count == 0)
1128          {          {
1129    #ifdef _DEBUG
1130                  log_error("Empty menu (%s)\n", p_menu->name);                  log_error("Empty menu (%s)\n", p_menu->name);
1131    #endif
1132                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1133                  {                  {
1134                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 1125  int menu_control(MENU_SET *p_menu_set, i Line 1152  int menu_control(MENU_SET *p_menu_set, i
1152          switch (key)          switch (key)
1153          {          {
1154          case CR:          case CR:
                 igetch_reset();  
1155          case KEY_RIGHT:          case KEY_RIGHT:
1156                  if (p_menu_item->submenu)                  if (p_menu_item->submenu)
1157                  {                  {
# Line 1152  int menu_control(MENU_SET *p_menu_set, i Line 1178  int menu_control(MENU_SET *p_menu_set, i
1178                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1179                  {                  {
1180                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
1181                          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);  
                         }  
1182                  }                  }
1183                  else                  else
1184                  {                  {
1185                            if (p_menu_set->allow_exit)
1186                            {
1187                                    return EXITMENU;
1188                            }
1189    
1190                          display_menu_cursor(p_menu_set, 0);                          display_menu_cursor(p_menu_set, 0);
1191                          menu_item_pos = p_menu->item_count - 1;                          menu_item_pos = p_menu->item_count - 1;
1192                          while (menu_item_pos >= 0)                          while (menu_item_pos >= 0)
# Line 1361  int unload_menu(MENU_SET *p_menu_set) Line 1385  int unload_menu(MENU_SET *p_menu_set)
1385    
1386          detach_menu_shm(p_menu_set);          detach_menu_shm(p_menu_set);
1387    
1388          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shmid != 0 && shmctl(shmid, IPC_RMID, NULL) == -1)
1389          {          {
1390                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);
1391                  return -1;                  return -1;
# Line 1369  int unload_menu(MENU_SET *p_menu_set) Line 1393  int unload_menu(MENU_SET *p_menu_set)
1393    
1394          return 0;          return 0;
1395  }  }
1396    
1397    int get_menu_shm_readonly(MENU_SET *p_menu_set)
1398    {
1399            void *p_shm;
1400    
1401            p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY);
1402            if (p_shm == (void *)-1)
1403            {
1404                    log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);
1405                    return -1;
1406            }
1407    
1408            p_menu_set->p_reserved = p_shm;
1409            p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
1410            p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
1411            p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
1412            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
1413            p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;
1414    
1415            p_menu_set->choose_step = 0;
1416            p_menu_set->menu_id_path[0] = 0;
1417            p_menu_set->menu_item_pos[0] = 0;
1418    
1419            return 0;
1420    }
1421    
1422  int set_menu_shm_readonly(MENU_SET *p_menu_set)  int set_menu_shm_readonly(MENU_SET *p_menu_set)
1423  {  {


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

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