/[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.72 by sysadm, Mon Oct 13 05:30:33 2025 UTC Revision 1.80 by sysadm, Tue Nov 4 13:49:51 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 by 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.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "bbs_cmd.h"  #include "bbs_cmd.h"
# Line 38  Line 30 
30    
31  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)
32    
33    #define MENU_SHMGET_RETRY_LIMIT 10
34    
35  MENU_SET bbs_menu;  MENU_SET bbs_menu;
36    MENU_SET top10_menu;
37    
38  int load_menu(MENU_SET *p_menu_set, const char *conf_file)  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
39  {  {
# Line 59  int load_menu(MENU_SET *p_menu_set, cons Line 54  int load_menu(MENU_SET *p_menu_set, cons
54          int proj_id;          int proj_id;
55          key_t key;          key_t key;
56          size_t size;          size_t size;
57            int retry_cnt;
58    
59          // Initialize the data structure          // Initialize the data structure
60          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 82  int load_menu(MENU_SET *p_menu_set, cons
82          }          }
83    
84          // 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;  
         }  
   
85          size = MENU_SET_RESERVED_LENGTH +          size = MENU_SET_RESERVED_LENGTH +
86                     sizeof(MENU) * MAX_MENUS +                     sizeof(MENU) * MAX_MENUS +
87                     sizeof(MENU_ITEM) * MAX_MENUITEMS +                     sizeof(MENU_ITEM) * MAX_MENUITEMS +
88                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
89                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
90          p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
91          if (p_menu_set->shmid == -1)          proj_id = (int)(time(NULL) % getpid());
92            retry_cnt = 0;
93    
94            do
95          {          {
96                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  key = ftok(conf_file, proj_id + retry_cnt);
97                  return -3;                  if (key == -1)
98          }                  {
99                            log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id, errno);
100                            return -2;
101                    }
102    
103                    p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);
104    
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                                    break;
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 988  int display_menu(MENU_SET *p_menu_set) Line 1000  int display_menu(MENU_SET *p_menu_set)
1000                  return EXITMENU;                  return EXITMENU;
1001          }          }
1002    
1003          if(p_menu->item_count <= 0) // empty menu          if (p_menu->item_count <= 0) // empty menu
1004          {          {
1005                  moveto(p_menu->screen_row, p_menu->screen_col);                  moveto(p_menu->screen_row, p_menu->screen_col);
1006                  clrtoeol();                  clrtoeol();
# Line 1140  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 1395  int get_menu_shm_readonly(MENU_SET *p_me Line 1406  int get_menu_shm_readonly(MENU_SET *p_me
1406          }          }
1407    
1408          p_menu_set->p_reserved = p_shm;          p_menu_set->p_reserved = p_shm;
1409          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;
1410          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;
1411          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;
1412          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;
1413          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;
1414    
1415          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