/[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.70 by sysadm, Thu Jun 26 03:25:25 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"
# Line 32  Line 28 
28  #include <sys/ipc.h>  #include <sys/ipc.h>
29  #include <sys/shm.h>  #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 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          // Initialize the data structure
65          memset(p_menu_set, 0, sizeof(*p_menu_set));          memset(p_menu_set, 0, sizeof(*p_menu_set));
# Line 86  int load_menu(MENU_SET *p_menu_set, cons Line 87  int load_menu(MENU_SET *p_menu_set, cons
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 988  int display_menu(MENU_SET *p_menu_set) Line 1004  int display_menu(MENU_SET *p_menu_set)
1004                  return EXITMENU;                  return EXITMENU;
1005          }          }
1006    
1007          if(p_menu->item_count <= 0) // empty menu          if (p_menu->item_count <= 0) // empty menu
1008          {          {
1009                  moveto(p_menu->screen_row, p_menu->screen_col);                  moveto(p_menu->screen_row, p_menu->screen_col);
1010                  clrtoeol();                  clrtoeol();
1011                  prints("没有可选项");                  prints("娌℃湁鍙夐」");
1012                  press_any_key();                  press_any_key();
1013                  return -1;                  return -1;
1014          }          }
# Line 1068  int display_menu(MENU_SET *p_menu_set) Line 1084  int display_menu(MENU_SET *p_menu_set)
1084          {          {
1085                  moveto(p_menu->screen_row, p_menu->screen_col);                  moveto(p_menu->screen_row, p_menu->screen_col);
1086                  clrtoeol();                  clrtoeol();
1087                  prints("没有可选项");                  prints("娌℃湁鍙夐」");
1088                  press_any_key();                  press_any_key();
1089                  return -1;                  return -1;
1090          }          }
# Line 1140  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 1167  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                  {                  {
# Line 1381  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 1402  int get_menu_shm_readonly(MENU_SET *p_me Line 1410  int get_menu_shm_readonly(MENU_SET *p_me
1410          }          }
1411    
1412          p_menu_set->p_reserved = p_shm;          p_menu_set->p_reserved = p_shm;
1413          p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH;          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
1414          p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS;          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 = p_menu_set->p_menu_item_pool + sizeof(MENU_ITEM) * MAX_MENUITEMS;          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 = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS;          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;          p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;
1418    
1419          p_menu_set->choose_step = 0;          p_menu_set->choose_step = 0;


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

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