/[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.42 by sysadm, Thu May 15 08:53:23 2025 UTC Revision 1.95 by sysadm, Mon Jan 5 05:41:34 2026 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-2026  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 <fcntl.h>
25    #include <stdio.h>
26    #include <stdlib.h>
27    #include <string.h>
28  #include <unistd.h>  #include <unistd.h>
29  #include <sys/shm.h>  #include <sys/mman.h>
30  #include <sys/ipc.h>  #include <sys/stat.h>
31    
32  #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"  enum _menu_constant_t
33  #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"  {
34  #define MENU_CONF_DELIM_WITHOUT_SPACE "\r\n"          MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4,
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  {  {
45            char filepath[FILE_PATH_LEN];
46            int fd;
47            size_t size;
48            void *p_shm;
49          FILE *fin;          FILE *fin;
50          int fin_line = 0;          int fin_line = 0;
51          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
52          char temp[LINE_BUFFER_LEN];          char temp[LINE_BUFFER_LEN];
53          char *p = NULL;          char *p = NULL;
54          char *q = NULL;          char *q = NULL;
55            char *r = NULL;
56          char *saveptr = NULL;          char *saveptr = NULL;
57          MENU *p_menu = NULL;          MENU *p_menu = NULL;
58          MENU_ITEM *p_menu_item = NULL;          MENU_ITEM *p_menu_item = NULL;
# Line 55  int load_menu(MENU_SET *p_menu_set, cons Line 60  int load_menu(MENU_SET *p_menu_set, cons
60          MENU_ID menu_id;          MENU_ID menu_id;
61          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
62          MENU_SCREEN_ID screen_id;          MENU_SCREEN_ID screen_id;
63          int proj_id;  
64          key_t key;          if (p_menu_set == NULL || conf_file == NULL)
65          size_t size;          {
66                    log_error("NULL pointer error");
67                    return -1;
68            }
69    
70            // Initialize the data structure
71            memset(p_menu_set, 0, sizeof(*p_menu_set));
72    
73          // Use trie_dict to search menu_id by menu name          // Use trie_dict to search menu_id by menu name
74          p_menu_set->p_menu_name_dict = trie_dict_create();          p_menu_set->p_menu_name_dict = trie_dict_create();
75          if (p_menu_set->p_menu_name_dict == NULL)          if (p_menu_set->p_menu_name_dict == NULL)
76          {          {
77                  log_error("trie_dict_create() error\n");                  log_error("trie_dict_create() error");
78                  return -3;                  return -1;
79          }          }
80    
81          // Use trie_dict to search screen_id by menu screen name          // Use trie_dict to search screen_id by menu screen name
82          p_menu_set->p_menu_screen_dict = trie_dict_create();          p_menu_set->p_menu_screen_dict = trie_dict_create();
83          if (p_menu_set->p_menu_screen_dict == NULL)          if (p_menu_set->p_menu_screen_dict == NULL)
84          {          {
85                  log_error("trie_dict_create() error\n");                  log_error("trie_dict_create() error");
86                  return -3;                  return -1;
87          }          }
88    
89          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
90          {          {
91                  log_error("Open %s failed", conf_file);                  log_error("Open %s error: %d", conf_file, errno);
92                  return -2;                  return -2;
93          }          }
94    
95          // 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;  
         }  
   
96          size = MENU_SET_RESERVED_LENGTH +          size = MENU_SET_RESERVED_LENGTH +
97                     sizeof(MENU) * MAX_MENUS +                     sizeof(MENU) * MAX_MENUS +
98                     sizeof(MENU_ITEM) * MAX_MENUITEMS +                     sizeof(MENU_ITEM) * MAX_MENUITEMS +
99                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
100                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
101          p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
102          if (p_menu_set->shmid == -1)          strncpy(filepath, conf_file, sizeof(filepath) - 1);
103            filepath[sizeof(filepath) - 1] = '\0';
104            snprintf(p_menu_set->shm_name, sizeof(p_menu_set->shm_name), "/MENU_SHM_%s", basename(filepath));
105    
106            if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT)
107            {
108                    log_error("shm_unlink(%s) error (%d)", p_menu_set->shm_name, errno);
109                    return -2;
110            }
111    
112            if ((fd = shm_open(p_menu_set->shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
113            {
114                    log_error("shm_open(%s) error (%d)", p_menu_set->shm_name, errno);
115                    return -2;
116            }
117            if (ftruncate(fd, (off_t)size) == -1)
118            {
119                    log_error("ftruncate(size=%d) error (%d)", size, errno);
120                    close(fd);
121                    return -2;
122            }
123    
124            p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
125            if (p_shm == MAP_FAILED)
126          {          {
127                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  log_error("mmap() error (%d)", errno);
128                  return -3;                  close(fd);
129                    return -2;
130          }          }
131          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);  
132          if (p_menu_set->p_reserved == (void *)-1)          if (close(fd) < 0)
133          {          {
134                  log_error("shmat() error (%d)\n", errno);                  log_error("close(fd) error (%d)", errno);
135                  return -3;                  return -1;
136          }          }
137          p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH;  
138          p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS;          p_menu_set->shm_size = size;
139          p_menu_set->p_menu_screen_pool = p_menu_set->p_menu_item_pool + sizeof(MENU_ITEM) * MAX_MENUITEMS;          p_menu_set->p_reserved = p_shm;
140          p_menu_set->p_menu_screen_buf = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS;  
141            p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
142            p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
143            p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
144            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
145          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;
146    
147          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
# Line 118  int load_menu(MENU_SET *p_menu_set, cons Line 149  int load_menu(MENU_SET *p_menu_set, cons
149          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
150          p_menu_set->choose_step = 0;          p_menu_set->choose_step = 0;
151          p_menu_set->menu_id_path[0] = 0;          p_menu_set->menu_id_path[0] = 0;
152            p_menu_set->menu_item_pos[0] = 0;
153            p_menu_set->allow_exit = 0;
154    
155          while (fgets(buffer, sizeof(buffer), fin))          while (fgets(buffer, sizeof(buffer), fin))
156          {          {
# Line 142  int load_menu(MENU_SET *p_menu_set, cons Line 175  int load_menu(MENU_SET *p_menu_set, cons
175                          {                          {
176                                  if (p_menu != NULL)                                  if (p_menu != NULL)
177                                  {                                  {
178                                          log_error("Incomplete menu definition in menu config line %d\n", fin_line);                                          log_error("Incomplete menu definition in menu config line %d", fin_line);
179                                          return -1;                                          return -1;
180                                  }                                  }
181    
182                                  if (p_menu_set->menu_count >= MAX_MENUS)                                  if (p_menu_set->menu_count >= MAX_MENUS)
183                                  {                                  {
184                                          log_error("Menu count (%d) exceed limit (%d)\n", p_menu_set->menu_count, MAX_MENUS);                                          log_error("Menu count (%d) exceed limit (%d)", p_menu_set->menu_count, MAX_MENUS);
185                                          return -3;                                          return -3;
186                                  }                                  }
187                                  menu_id = (MENU_ID)p_menu_set->menu_count;                                  menu_id = (MENU_ID)p_menu_set->menu_count;
# Line 159  int load_menu(MENU_SET *p_menu_set, cons Line 192  int load_menu(MENU_SET *p_menu_set, cons
192                                  p_menu->item_count = 0;                                  p_menu->item_count = 0;
193                                  p_menu->title.show = 0;                                  p_menu->title.show = 0;
194                                  p_menu->screen_show = 0;                                  p_menu->screen_show = 0;
195                                    p_menu->page_item_limit = 0;
196                                    p_menu->use_filter = 0;
197                                    p_menu->filter_handler = NULL;
198    
199                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
200                                  if (q == NULL)                                  if (q == NULL)
201                                  {                                  {
202                                          log_error("Error menu name in menu config line %d\n", fin_line);                                          log_error("Error menu name in menu config line %d", fin_line);
203                                          return -1;                                          return -1;
204                                  }                                  }
205                                  p = q;                                  p = q;
206                                  while (isalnum(*q) || *q == '_')                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
207                                  {                                  {
208                                          q++;                                          q++;
209                                  }                                  }
210                                  if (*q != '\0')                                  if (*q != '\0')
211                                  {                                  {
212                                          log_error("Error menu name in menu config line %d\n", fin_line);                                          log_error("Error menu name in menu config line %d", fin_line);
213                                          return -1;                                          return -1;
214                                  }                                  }
215    
216                                  if (q - p > sizeof(p_menu->name) - 1)                                  if (q - p > sizeof(p_menu->name) - 1)
217                                  {                                  {
218                                          log_error("Too longer menu name in menu config line %d\n", fin_line);                                          log_error("Too longer menu name in menu config line %d", fin_line);
219                                          return -1;                                          return -1;
220                                  }                                  }
221                                  strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);                                  strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);
# Line 187  int load_menu(MENU_SET *p_menu_set, cons Line 223  int load_menu(MENU_SET *p_menu_set, cons
223    
224                                  if (trie_dict_set(p_menu_set->p_menu_name_dict, p_menu->name, (int64_t)menu_id) != 1)                                  if (trie_dict_set(p_menu_set->p_menu_name_dict, p_menu->name, (int64_t)menu_id) != 1)
225                                  {                                  {
226                                          log_error("Error set menu dict [%s]\n", p_menu->name);                                          log_error("Error set menu dict [%s]", p_menu->name);
227                                  }                                  }
228    
229                                  // Check syntax                                  // Check syntax
230                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
231                                  if (q != NULL)                                  if (q != NULL)
232                                  {                                  {
233                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                          log_error("Unknown extra content in menu config line %d", fin_line);
234                                          return -1;                                          return -1;
235                                  }                                  }
236    
# Line 223  int load_menu(MENU_SET *p_menu_set, cons Line 259  int load_menu(MENU_SET *p_menu_set, cons
259                                                  // BEGIN of menu item                                                  // BEGIN of menu item
260                                                  if (p_menu->item_count >= MAX_ITEMS_PER_MENU)                                                  if (p_menu->item_count >= MAX_ITEMS_PER_MENU)
261                                                  {                                                  {
262                                                          log_error("Menuitem count per menu (%d) exceed limit (%d)\n", p_menu->item_count, MAX_ITEMS_PER_MENU);                                                          log_error("Menuitem count per menu (%d) exceed limit (%d)", p_menu->item_count, MAX_ITEMS_PER_MENU);
263                                                          return -1;                                                          return -1;
264                                                  }                                                  }
265                                                  if (p_menu_set->menu_item_count >= MAX_MENUITEMS)                                                  if (p_menu_set->menu_item_count >= MAX_MENUITEMS)
266                                                  {                                                  {
267                                                          log_error("Menuitem count (%d) exceed limit (%d)\n", p_menu_set->menu_item_count, MAX_MENUITEMS);                                                          log_error("Menuitem count (%d) exceed limit (%d)", p_menu_set->menu_item_count, MAX_MENUITEMS);
268                                                          return -3;                                                          return -3;
269                                                  }                                                  }
270                                                  menu_item_id = (MENU_ITEM_ID)p_menu_set->menu_item_count;                                                  menu_item_id = (MENU_ITEM_ID)p_menu_set->menu_item_count;
# Line 250  int load_menu(MENU_SET *p_menu_set, cons Line 286  int load_menu(MENU_SET *p_menu_set, cons
286                                                  else                                                  else
287                                                  {                                                  {
288                                                          q = p;                                                          q = p;
289                                                          while (isalnum(*q) || *q == '_')                                                          while (isalnum((int)*q) || *q == '_' || *q == '-')
290                                                          {                                                          {
291                                                                  q++;                                                                  q++;
292                                                          }                                                          }
293                                                          if (*q != '\0')                                                          if (*q != '\0')
294                                                          {                                                          {
295                                                                  log_error("Error menu item action in menu config line %d\n", fin_line);                                                                  log_error("Error menu item action in menu config line %d", fin_line);
296                                                                  return -1;                                                                  return -1;
297                                                          }                                                          }
298                                                  }                                                  }
299    
300                                                  if (q - p > sizeof(p_menu_item->action) - 1)                                                  if (q - p > sizeof(p_menu_item->action) - 1)
301                                                  {                                                  {
302                                                          log_error("Too longer menu action in menu config line %d\n", fin_line);                                                          log_error("Too longer menu action in menu config line %d", fin_line);
303                                                          return -1;                                                          return -1;
304                                                  }                                                  }
305                                                  strncpy(p_menu_item->action, p, sizeof(p_menu_item->action) - 1);                                                  strncpy(p_menu_item->action, p, sizeof(p_menu_item->action) - 1);
# Line 273  int load_menu(MENU_SET *p_menu_set, cons Line 309  int load_menu(MENU_SET *p_menu_set, cons
309                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
310                                                  if (q == NULL)                                                  if (q == NULL)
311                                                  {                                                  {
312                                                          log_error("Error menu item row in menu config line %d\n", fin_line);                                                          log_error("Error menu item row in menu config line %d", fin_line);
313                                                          return -1;                                                          return -1;
314                                                  }                                                  }
315                                                  p = q;                                                  p = q;
316                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
317                                                  {                                                  {
318                                                          q++;                                                          q++;
319                                                  }                                                  }
320                                                  if (*q != '\0')                                                  if (*q != '\0')
321                                                  {                                                  {
322                                                          log_error("Error menu item row in menu config line %d\n", fin_line);                                                          log_error("Error menu item row in menu config line %d", fin_line);
323                                                          return -1;                                                          return -1;
324                                                  }                                                  }
325                                                  p_menu_item->row = (int16_t)atoi(p);                                                  p_menu_item->row = (int16_t)atoi(p);
# Line 292  int load_menu(MENU_SET *p_menu_set, cons Line 328  int load_menu(MENU_SET *p_menu_set, cons
328                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
329                                                  if (q == NULL)                                                  if (q == NULL)
330                                                  {                                                  {
331                                                          log_error("Error menu item col in menu config line %d\n", fin_line);                                                          log_error("Error menu item col in menu config line %d", fin_line);
332                                                          return -1;                                                          return -1;
333                                                  }                                                  }
334                                                  p = q;                                                  p = q;
335                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
336                                                  {                                                  {
337                                                          q++;                                                          q++;
338                                                  }                                                  }
339                                                  if (*q != '\0')                                                  if (*q != '\0')
340                                                  {                                                  {
341                                                          log_error("Error menu item col in menu config line %d\n", fin_line);                                                          log_error("Error menu item col in menu config line %d", fin_line);
342                                                          return -1;                                                          return -1;
343                                                  }                                                  }
344                                                  p_menu_item->col = (int16_t)atoi(p);                                                  p_menu_item->col = (int16_t)atoi(p);
# Line 311  int load_menu(MENU_SET *p_menu_set, cons Line 347  int load_menu(MENU_SET *p_menu_set, cons
347                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
348                                                  if (q == NULL)                                                  if (q == NULL)
349                                                  {                                                  {
350                                                          log_error("Error menu item priv in menu config line %d\n", fin_line);                                                          log_error("Error menu item priv in menu config line %d", fin_line);
351                                                          return -1;                                                          return -1;
352                                                  }                                                  }
353                                                  p = q;                                                  p = q;
354                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
355                                                  {                                                  {
356                                                          q++;                                                          q++;
357                                                  }                                                  }
358                                                  if (*q != '\0')                                                  if (*q != '\0')
359                                                  {                                                  {
360                                                          log_error("Error menu item priv in menu config line %d\n", fin_line);                                                          log_error("Error menu item priv in menu config line %d", fin_line);
361                                                          return -1;                                                          return -1;
362                                                  }                                                  }
363                                                  p_menu_item->priv = atoi(p);                                                  p_menu_item->priv = atoi(p);
# Line 330  int load_menu(MENU_SET *p_menu_set, cons Line 366  int load_menu(MENU_SET *p_menu_set, cons
366                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
367                                                  if (q == NULL)                                                  if (q == NULL)
368                                                  {                                                  {
369                                                          log_error("Error menu item level in menu config line %d\n", fin_line);                                                          log_error("Error menu item level in menu config line %d", fin_line);
370                                                          return -1;                                                          return -1;
371                                                  }                                                  }
372                                                  p = q;                                                  p = q;
373                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
374                                                  {                                                  {
375                                                          q++;                                                          q++;
376                                                  }                                                  }
377                                                  if (*q != '\0')                                                  if (*q != '\0')
378                                                  {                                                  {
379                                                          log_error("Error menu item level in menu config line %d\n", fin_line);                                                          log_error("Error menu item level in menu config line %d", fin_line);
380                                                          return -1;                                                          return -1;
381                                                  }                                                  }
382                                                  p_menu_item->level = atoi(p);                                                  p_menu_item->level = atoi(p);
# Line 349  int load_menu(MENU_SET *p_menu_set, cons Line 385  int load_menu(MENU_SET *p_menu_set, cons
385                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
386                                                  if (q == NULL || *q != '\"')                                                  if (q == NULL || *q != '\"')
387                                                  {                                                  {
388                                                          log_error("Error menu item name in menu config line %d\n", fin_line);                                                          log_error("Error menu item name in menu config line %d", fin_line);
389                                                          return -1;                                                          return -1;
390                                                  }                                                  }
391                                                  q++;                                                  q++;
392                                                  p = q;                                                  p = q;
393                                                  while (*q != '\0' && *q != '\"')                                                  while (*q != '\0' && *q != '\"')
394                                                  {                                                  {
395                                                            if (*q == '\\')
396                                                            {
397                                                                    r = q;
398                                                                    while (*r != '\0')
399                                                                    {
400                                                                            *r = *(r + 1);
401                                                                            r++;
402                                                                    }
403                                                            }
404                                                          q++;                                                          q++;
405                                                  }                                                  }
406                                                  if (*q != '\"' || *(q + 1) != '\0')                                                  if (*q != '\"' || *(q + 1) != '\0')
407                                                  {                                                  {
408                                                          log_error("Error menu item name in menu config line %d\n", fin_line);                                                          log_error("Error menu item name in menu config line %d", fin_line);
409                                                          return -1;                                                          return -1;
410                                                  }                                                  }
411                                                  *q = '\0';                                                  *q = '\0';
412    
413                                                  if (q - p > sizeof(p_menu_item->name) - 1)                                                  if (q - p > sizeof(p_menu_item->name) - 1)
414                                                  {                                                  {
415                                                          log_error("Too longer menu name in menu config line %d\n", fin_line);                                                          log_error("Too longer menu name in menu config line %d", fin_line);
416                                                          return -1;                                                          return -1;
417                                                  }                                                  }
418                                                  strncpy(p_menu_item->name, p, sizeof(p_menu_item->name) - 1);                                                  strncpy(p_menu_item->name, p, sizeof(p_menu_item->name) - 1);
# Line 377  int load_menu(MENU_SET *p_menu_set, cons Line 422  int load_menu(MENU_SET *p_menu_set, cons
422                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
423                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)
424                                                  {                                                  {
425                                                          log_error("Error menu item text in menu config line %d\n", fin_line);                                                          log_error("Error menu item text in menu config line %d", fin_line);
426                                                          return -1;                                                          return -1;
427                                                  }                                                  }
428                                                  q++;                                                  q++;
429                                                  p = q;                                                  p = q;
430                                                  while (*q != '\0' && *q != '\"')                                                  while (*q != '\0' && *q != '\"')
431                                                  {                                                  {
432                                                            if (*q == '\\')
433                                                            {
434                                                                    r = q;
435                                                                    while (*r != '\0')
436                                                                    {
437                                                                            *r = *(r + 1);
438                                                                            r++;
439                                                                    }
440                                                            }
441                                                          q++;                                                          q++;
442                                                  }                                                  }
443                                                  if (*q != '\"')                                                  if (*q != '\"')
444                                                  {                                                  {
445                                                          log_error("Error menu item text in menu config line %d\n", fin_line);                                                          log_error("Error menu item text in menu config line %d", fin_line);
446                                                          return -1;                                                          return -1;
447                                                  }                                                  }
448                                                  *q = '\0';                                                  *q = '\0';
449    
450                                                  if (q - p > sizeof(p_menu_item->text) - 1)                                                  if (q - p > sizeof(p_menu_item->text) - 1)
451                                                  {                                                  {
452                                                          log_error("Too longer menu item text in menu config line %d\n", fin_line);                                                          log_error("Too longer menu item text in menu config line %d", fin_line);
453                                                          return -1;                                                          return -1;
454                                                  }                                                  }
455                                                  strncpy(p_menu_item->text, p, sizeof(p_menu_item->text) - 1);                                                  strncpy(p_menu_item->text, p, sizeof(p_menu_item->text) - 1);
# Line 405  int load_menu(MENU_SET *p_menu_set, cons Line 459  int load_menu(MENU_SET *p_menu_set, cons
459                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
460                                                  if (q != NULL)                                                  if (q != NULL)
461                                                  {                                                  {
462                                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                                          log_error("Unknown extra content in menu config line %d", fin_line);
463                                                          return -1;                                                          return -1;
464                                                  }                                                  }
465                                          }                                          }
# Line 417  int load_menu(MENU_SET *p_menu_set, cons Line 471  int load_menu(MENU_SET *p_menu_set, cons
471                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
472                                                  if (q == NULL)                                                  if (q == NULL)
473                                                  {                                                  {
474                                                          log_error("Error menu title row in menu config line %d\n", fin_line);                                                          log_error("Error menu title row in menu config line %d", fin_line);
475                                                          return -1;                                                          return -1;
476                                                  }                                                  }
477                                                  p = q;                                                  p = q;
478                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
479                                                  {                                                  {
480                                                          q++;                                                          q++;
481                                                  }                                                  }
482                                                  if (*q != '\0')                                                  if (*q != '\0')
483                                                  {                                                  {
484                                                          log_error("Error menu title row in menu config line %d\n", fin_line);                                                          log_error("Error menu title row in menu config line %d", fin_line);
485                                                          return -1;                                                          return -1;
486                                                  }                                                  }
487                                                  p_menu->title.row = (int16_t)atoi(p);                                                  p_menu->title.row = (int16_t)atoi(p);
# Line 436  int load_menu(MENU_SET *p_menu_set, cons Line 490  int load_menu(MENU_SET *p_menu_set, cons
490                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
491                                                  if (q == NULL)                                                  if (q == NULL)
492                                                  {                                                  {
493                                                          log_error("Error menu title col in menu config line %d\n", fin_line);                                                          log_error("Error menu title col in menu config line %d", fin_line);
494                                                          return -1;                                                          return -1;
495                                                  }                                                  }
496                                                  p = q;                                                  p = q;
497                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
498                                                  {                                                  {
499                                                          q++;                                                          q++;
500                                                  }                                                  }
501                                                  if (*q != '\0')                                                  if (*q != '\0')
502                                                  {                                                  {
503                                                          log_error("Error menu title col in menu config line %d\n", fin_line);                                                          log_error("Error menu title col in menu config line %d", fin_line);
504                                                          return -1;                                                          return -1;
505                                                  }                                                  }
506                                                  p_menu->title.col = (int16_t)atoi(p);                                                  p_menu->title.col = (int16_t)atoi(p);
# Line 455  int load_menu(MENU_SET *p_menu_set, cons Line 509  int load_menu(MENU_SET *p_menu_set, cons
509                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
510                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)
511                                                  {                                                  {
512                                                          log_error("Error menu title text in menu config line %d\n", fin_line);                                                          log_error("Error menu title text in menu config line %d", fin_line);
513                                                          return -1;                                                          return -1;
514                                                  }                                                  }
515                                                  q++;                                                  q++;
516                                                  p = q;                                                  p = q;
517                                                  while (*q != '\0' && *q != '\"')                                                  while (*q != '\0' && *q != '\"')
518                                                  {                                                  {
519                                                            if (*q == '\\')
520                                                            {
521                                                                    r = q;
522                                                                    while (*r != '\0')
523                                                                    {
524                                                                            *r = *(r + 1);
525                                                                            r++;
526                                                                    }
527                                                            }
528                                                          q++;                                                          q++;
529                                                  }                                                  }
530                                                  if (*q != '\"')                                                  if (*q != '\"')
531                                                  {                                                  {
532                                                          log_error("Error menu title text in menu config line %d\n", fin_line);                                                          log_error("Error menu title text in menu config line %d", fin_line);
533                                                          return -1;                                                          return -1;
534                                                  }                                                  }
535                                                  *q = '\0';                                                  *q = '\0';
536    
537                                                  if (q - p > sizeof(p_menu_item->text) - 1)                                                  if (q - p > sizeof(p_menu->title.text) - 1)
538                                                  {                                                  {
539                                                          log_error("Too longer menu title text in menu config line %d\n", fin_line);                                                          log_error("Too longer menu title text in menu config line %d", fin_line);
540                                                          return -1;                                                          return -1;
541                                                  }                                                  }
542                                                  strncpy(p_menu->title.text, p, sizeof(p_menu->title.text) - 1);                                                  strncpy(p_menu->title.text, p, sizeof(p_menu->title.text) - 1);
# Line 483  int load_menu(MENU_SET *p_menu_set, cons Line 546  int load_menu(MENU_SET *p_menu_set, cons
546                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
547                                                  if (q != NULL)                                                  if (q != NULL)
548                                                  {                                                  {
549                                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                                          log_error("Unknown extra content in menu config line %d", fin_line);
550                                                          return -1;                                                          return -1;
551                                                  }                                                  }
552                                          }                                          }
# Line 495  int load_menu(MENU_SET *p_menu_set, cons Line 558  int load_menu(MENU_SET *p_menu_set, cons
558                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
559                                                  if (q == NULL)                                                  if (q == NULL)
560                                                  {                                                  {
561                                                          log_error("Error menu screen row in menu config line %d\n", fin_line);                                                          log_error("Error menu screen row in menu config line %d", fin_line);
562                                                          return -1;                                                          return -1;
563                                                  }                                                  }
564                                                  p = q;                                                  p = q;
565                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
566                                                  {                                                  {
567                                                          q++;                                                          q++;
568                                                  }                                                  }
569                                                  if (*q != '\0')                                                  if (*q != '\0')
570                                                  {                                                  {
571                                                          log_error("Error menu screen row in menu config line %d\n", fin_line);                                                          log_error("Error menu screen row in menu config line %d", fin_line);
572                                                          return -1;                                                          return -1;
573                                                  }                                                  }
574                                                  p_menu->screen_row = (int16_t)atoi(p);                                                  p_menu->screen_row = (int16_t)atoi(p);
# Line 514  int load_menu(MENU_SET *p_menu_set, cons Line 577  int load_menu(MENU_SET *p_menu_set, cons
577                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
578                                                  if (q == NULL)                                                  if (q == NULL)
579                                                  {                                                  {
580                                                          log_error("Error menu screen col in menu config line %d\n", fin_line);                                                          log_error("Error menu screen col in menu config line %d", fin_line);
581                                                          return -1;                                                          return -1;
582                                                  }                                                  }
583                                                  p = q;                                                  p = q;
584                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
585                                                  {                                                  {
586                                                          q++;                                                          q++;
587                                                  }                                                  }
588                                                  if (*q != '\0')                                                  if (*q != '\0')
589                                                  {                                                  {
590                                                          log_error("Error menu screen col in menu config line %d\n", fin_line);                                                          log_error("Error menu screen col in menu config line %d", fin_line);
591                                                          return -1;                                                          return -1;
592                                                  }                                                  }
593                                                  p_menu->screen_col = (int16_t)atoi(p);                                                  p_menu->screen_col = (int16_t)atoi(p);
# Line 533  int load_menu(MENU_SET *p_menu_set, cons Line 596  int load_menu(MENU_SET *p_menu_set, cons
596                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
597                                                  if (q == NULL)                                                  if (q == NULL)
598                                                  {                                                  {
599                                                          log_error("Error menu screen name in menu config line %d\n", fin_line);                                                          log_error("Error menu screen name in menu config line %d", fin_line);
600                                                          return -1;                                                          return -1;
601                                                  }                                                  }
602                                                  p = q;                                                  p = q;
603                                                  while (isalnum(*q) || *q == '_')                                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
604                                                  {                                                  {
605                                                          q++;                                                          q++;
606                                                  }                                                  }
607                                                  if (*q != '\0')                                                  if (*q != '\0')
608                                                  {                                                  {
609                                                          log_error("Error menu screen name in menu config line %d\n", fin_line);                                                          log_error("Error menu screen name in menu config line %d", fin_line);
610                                                            return -1;
611                                                    }
612                                                    strncpy(p_menu->screen_name, p, sizeof(p_menu->screen_name) - 1);
613                                                    p_menu->screen_name[sizeof(p_menu->screen_name) - 1] = '\0';
614    
615                                                    // Check syntax
616                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
617                                                    if (q != NULL)
618                                                    {
619                                                            log_error("Unknown extra content in menu config line %d", fin_line);
620                                                            return -1;
621                                                    }
622                                            }
623                                            else if (strcmp(p, "page") == 0)
624                                            {
625                                                    // Menu page row
626                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
627                                                    if (q == NULL)
628                                                    {
629                                                            log_error("Error menu page row in menu config line %d", fin_line);
630                                                          return -1;                                                          return -1;
631                                                  }                                                  }
632                                                  if (trie_dict_get(p_menu_set->p_menu_screen_dict, p, (int64_t *)&screen_id) != 1)                                                  p = q;
633                                                    while (isdigit((int)*q))
634                                                    {
635                                                            q++;
636                                                    }
637                                                    if (*q != '\0')
638                                                  {                                                  {
639                                                          log_error("Undefined menu screen [%s]\n", p);                                                          log_error("Error menu page row in menu config line %d", fin_line);
640                                                          return -1;                                                          return -1;
641                                                  }                                                  }
642                                                  p_menu->screen_id = screen_id;                                                  p_menu->page_row = (int16_t)atoi(p);
643    
644                                                    // Menu page col
645                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
646                                                    if (q == NULL)
647                                                    {
648                                                            log_error("Error menu page col in menu config line %d", fin_line);
649                                                            return -1;
650                                                    }
651                                                    p = q;
652                                                    while (isdigit((int)*q))
653                                                    {
654                                                            q++;
655                                                    }
656                                                    if (*q != '\0')
657                                                    {
658                                                            log_error("Error menu page col in menu config line %d", fin_line);
659                                                            return -1;
660                                                    }
661                                                    p_menu->page_col = (int16_t)atoi(p);
662    
663                                                    // Menu page item limit
664                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
665                                                    if (q == NULL)
666                                                    {
667                                                            log_error("Error menu page item limit in menu config line %d", fin_line);
668                                                            return -1;
669                                                    }
670                                                    p = q;
671                                                    while (isdigit((int)*q))
672                                                    {
673                                                            q++;
674                                                    }
675                                                    if (*q != '\0')
676                                                    {
677                                                            log_error("Error menu page item limit in menu config line %d", fin_line);
678                                                            return -1;
679                                                    }
680                                                    p_menu->page_item_limit = (int16_t)atoi(p);
681    
682                                                    // Check syntax
683                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
684                                                    if (q != NULL)
685                                                    {
686                                                            log_error("Unknown extra content in menu config line %d", fin_line);
687                                                            return -1;
688                                                    }
689                                            }
690                                            else if (strcmp(p, "use_filter") == 0)
691                                            {
692                                                    p_menu->use_filter = 1;
693    
694                                                  // Check syntax                                                  // Check syntax
695                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
696                                                  if (q != NULL)                                                  if (q != NULL)
697                                                  {                                                  {
698                                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                                          log_error("Unknown extra content in menu config line %d", fin_line);
699                                                          return -1;                                                          return -1;
700                                                  }                                                  }
701                                          }                                          }
# Line 567  int load_menu(MENU_SET *p_menu_set, cons Line 705  int load_menu(MENU_SET *p_menu_set, cons
705                          {                          {
706                                  if (p_menu_set->menu_item_count >= MAX_MENUS)                                  if (p_menu_set->menu_item_count >= MAX_MENUS)
707                                  {                                  {
708                                          log_error("Menu screen count (%d) exceed limit (%d)\n", p_menu_set->menu_screen_count, MAX_MENUS);                                          log_error("Menu screen count (%d) exceed limit (%d)", p_menu_set->menu_screen_count, MAX_MENUS);
709                                          return -3;                                          return -3;
710                                  }                                  }
711                                  screen_id = (MENU_SCREEN_ID)p_menu_set->menu_screen_count;                                  screen_id = (MENU_SCREEN_ID)p_menu_set->menu_screen_count;
# Line 576  int load_menu(MENU_SET *p_menu_set, cons Line 714  int load_menu(MENU_SET *p_menu_set, cons
714                                  p_screen = get_menu_screen_by_id(p_menu_set, screen_id);                                  p_screen = get_menu_screen_by_id(p_menu_set, screen_id);
715    
716                                  q = p;                                  q = p;
717                                  while (isalnum(*q) || *q == '_')                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
718                                  {                                  {
719                                          q++;                                          q++;
720                                  }                                  }
721                                  if (*q != '\0')                                  if (*q != '\0')
722                                  {                                  {
723                                          log_error("Error menu screen name in menu config line %d\n", fin_line);                                          log_error("Error menu screen name in menu config line %d", fin_line);
724                                          return -1;                                          return -1;
725                                  }                                  }
726                                  strncpy(p_screen->name, p, sizeof(p_screen->name) - 1);                                  strncpy(p_screen->name, p, sizeof(p_screen->name) - 1);
# Line 590  int load_menu(MENU_SET *p_menu_set, cons Line 728  int load_menu(MENU_SET *p_menu_set, cons
728    
729                                  if (trie_dict_set(p_menu_set->p_menu_screen_dict, p_screen->name, (int64_t)screen_id) != 1)                                  if (trie_dict_set(p_menu_set->p_menu_screen_dict, p_screen->name, (int64_t)screen_id) != 1)
730                                  {                                  {
731                                          log_error("Error set menu screen dict [%s]\n", p_screen->name);                                          log_error("Error set menu screen dict [%s]", p_screen->name);
732                                  }                                  }
733    
734                                  // Check syntax                                  // Check syntax
735                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
736                                  if (q != NULL)                                  if (q != NULL)
737                                  {                                  {
738                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                          log_error("Unknown extra content in menu config line %d", fin_line);
739                                          return -1;                                          return -1;
740                                  }                                  }
741    
# Line 618  int load_menu(MENU_SET *p_menu_set, cons Line 756  int load_menu(MENU_SET *p_menu_set, cons
756                                          {                                          {
757                                                  if (p_menu_set->p_menu_screen_buf_free + 1 > q)                                                  if (p_menu_set->p_menu_screen_buf_free + 1 > q)
758                                                  {                                                  {
759                                                          log_error("Menu screen buffer depleted (%p + 1 > %p)\n", p_menu_set->p_menu_screen_buf_free, q);                                                          log_error("Menu screen buffer depleted (%p + 1 > %p)", p_menu_set->p_menu_screen_buf_free, q);
760                                                          return -3;                                                          return -3;
761                                                  }                                                  }
762    
# Line 628  int load_menu(MENU_SET *p_menu_set, cons Line 766  int load_menu(MENU_SET *p_menu_set, cons
766                                                  break;                                                  break;
767                                          }                                          }
768    
769                                            // Clear line
770                                            if (p_menu_set->p_menu_screen_buf_free + strlen(CTRL_SEQ_CLR_LINE) > q)
771                                            {
772                                                    log_error("Menu screen buffer depleted (%p + %d > %p)", p_menu_set->p_menu_screen_buf_free, q, strlen(CTRL_SEQ_CLR_LINE));
773                                                    return -3;
774                                            }
775                                            p_menu_set->p_menu_screen_buf_free = stpcpy(p_menu_set->p_menu_screen_buf_free, CTRL_SEQ_CLR_LINE);
776    
777                                          p = buffer;                                          p = buffer;
778                                          while (*p != '\0')                                          while (*p != '\0')
779                                          {                                          {
780                                                  if (p_menu_set->p_menu_screen_buf_free + 2 > q)                                                  if (p_menu_set->p_menu_screen_buf_free + 2 > q)
781                                                  {                                                  {
782                                                          log_error("Menu screen buffer depleted (%p + 2 > %p)\n", p_menu_set->p_menu_screen_buf_free, q);                                                          log_error("Menu screen buffer depleted (%p + 2 > %p)", p_menu_set->p_menu_screen_buf_free, q);
783                                                          return -3;                                                          return -3;
784                                                  }                                                  }
785    
# Line 651  int load_menu(MENU_SET *p_menu_set, cons Line 797  int load_menu(MENU_SET *p_menu_set, cons
797    
798                                  if (p_screen->buf_length == -1)                                  if (p_screen->buf_length == -1)
799                                  {                                  {
800                                          log_error("End of menu screen [%s] not found\n", p_screen->name);                                          log_error("End of menu screen [%s] not found", p_screen->name);
801                                  }                                  }
802                          }                          }
803                  }                  }
804                  else // Invalid prefix                  else // Invalid prefix
805                  {                  {
806                          log_error("Error in menu config line %d\n", fin_line);                          log_error("Error in menu config line %d", fin_line);
807                          return -1;                          return -1;
808                  }                  }
809          }          }
810          fclose(fin);          fclose(fin);
811    
812          // Set menu_item->action_menu_id of each menu item pointing to a submenu to the menu_id of the corresponding submenu          for (menu_id = 0; menu_id < p_menu_set->menu_count; menu_id++)
813            {
814                    p_menu = get_menu_by_id(p_menu_set, menu_id);
815    
816                    if (trie_dict_get(p_menu_set->p_menu_screen_dict, p_menu->screen_name, (int64_t *)(&(p_menu->screen_id))) != 1)
817                    {
818                            log_error("Undefined menu screen [%s]", p);
819                            return -1;
820                    }
821    
822                    // Set menu->filter_handler of each menu pointing to filter
823                    if (p_menu->use_filter == 1)
824                    {
825                            if ((p_menu->filter_handler = get_cmd_handler(p_menu->name)) == NULL)
826                            {
827                                    log_error("Undefined menu filter handler [%s]", p_menu->name);
828                                    return -1;
829                            }
830                    }
831            }
832    
833          for (menu_item_id = 0; menu_item_id < p_menu_set->menu_item_count; menu_item_id++)          for (menu_item_id = 0; menu_item_id < p_menu_set->menu_item_count; menu_item_id++)
834          {          {
835                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
836                  if (p_menu_item->submenu == 1 && strcmp(p_menu_item->action, "..") != 0)  
837                    // Set menu_item->action_cmd_handler of each menu item pointing to bbs_cmd
838                    if (p_menu_item->submenu == 0)
839                    {
840                            if ((p_menu_item->action_cmd_handler = get_cmd_handler(p_menu_item->action)) == NULL)
841                            {
842                                    log_error("Undefined menu action cmd handler [%s]", p_menu_item->action);
843                                    return -1;
844                            }
845                    }
846                    // Set menu_item->action_menu_id of each menu item pointing to a submenu to the menu_id of the corresponding submenu
847                    else if (strcmp(p_menu_item->action, "..") != 0)
848                  {                  {
849                          if (trie_dict_get(p_menu_set->p_menu_name_dict, p_menu_item->action, (int64_t *)&menu_id) != 1)                          if (trie_dict_get(p_menu_set->p_menu_name_dict, p_menu_item->action, (int64_t *)&menu_id) != 1)
850                          {                          {
851                                  log_error("Undefined menu action [%s]\n", p_menu_item->action);                                  log_error("Undefined sub menu id [%s]", p_menu_item->action);
852                                  return -1;                                  return -1;
853                          }                          }
854                          p_menu_item->action_menu_id = menu_id;                          p_menu_item->action_menu_id = menu_id;
# Line 686  int load_menu(MENU_SET *p_menu_set, cons Line 863  int load_menu(MENU_SET *p_menu_set, cons
863          return 0;          return 0;
864  }  }
865    
866  static int display_menu_cursor(MENU_SET *p_menu_set, int show)  int display_menu_cursor(MENU_SET *p_menu_set, int show)
867  {  {
868          MENU_ID menu_id;          MENU_ID menu_id;
869          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
# Line 698  static int display_menu_cursor(MENU_SET Line 875  static int display_menu_cursor(MENU_SET
875          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
876          if (p_menu == NULL)          if (p_menu == NULL)
877          {          {
878                  log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id);                  log_error("get_menu_by_id(%d) return NULL pointer", menu_id);
879                  return -1;                  return -1;
880          }          }
881    
# Line 707  static int display_menu_cursor(MENU_SET Line 884  static int display_menu_cursor(MENU_SET
884          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
885          if (p_menu_item == NULL)          if (p_menu_item == NULL)
886          {          {
887                  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", menu_item_id);
888                  return -1;                  return -1;
889          }          }
890    
891          moveto(p_menu_set->menu_item_r_row[menu_item_pos], p_menu_set->menu_item_r_col[menu_item_pos] - 2);          moveto(p_menu_set->menu_item_r_row[menu_item_pos], p_menu_set->menu_item_r_col[menu_item_pos] - 2);
892          outc(show ? '>' : ' ');          outc(show ? '>' : ' ');
         iflush();  
893    
894          return 0;          return 0;
895  }  }
896    
897  int display_menu(MENU_SET *p_menu_set)  static int display_menu_current_page(MENU_SET *p_menu_set)
898  {  {
899          int16_t row = 0;          int16_t row = 0;
900          int16_t col = 0;          int16_t col = 0;
         int menu_selectable = 0;  
901          MENU_ID menu_id;          MENU_ID menu_id;
902          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
903          MENU *p_menu;          MENU *p_menu;
904          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
         MENU_SCREEN *p_menu_screen;  
905          int16_t menu_item_pos;          int16_t menu_item_pos;
906            int16_t page_id = 0;
907            MENU_SCREEN *p_menu_screen;
908    
909          menu_id = p_menu_set->menu_id_path[p_menu_set->choose_step];          menu_id = p_menu_set->menu_id_path[p_menu_set->choose_step];
910          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
911          if (p_menu == NULL)          if (p_menu == NULL)
912          {          {
913                  log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id);                  log_error("get_menu_by_id(%d) return NULL pointer", menu_id);
914                  return -1;                  return -1;
915          }          }
916    
917          menu_item_pos = p_menu_set->menu_item_pos[p_menu_set->choose_step];          clrline(p_menu->page_row, p_menu->page_row + p_menu->page_item_limit - 1);
         menu_item_id = p_menu->items[menu_item_pos];  
         p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);  
         if (p_menu_item == NULL)  
         {  
                 log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);  
                 return -1;  
         }  
   
         if (menu_item_pos > 0 &&  
                 checkpriv(&BBS_priv, 0, p_menu_item->priv) != 0 &&  
                 checklevel(&BBS_priv, p_menu_item->level) != 0)  
         {  
                 menu_selectable = 1;  
         }  
918    
919          if (p_menu->title.show)          if (p_menu->title.show)
920          {          {
921                  show_top(p_menu->title.text);                  if (p_menu->title.row == 0 && p_menu->title.col == 0)
922                    {
923                            show_top(p_menu->title.text, BBS_name, "");
924                    }
925                    else
926                    {
927                            moveto(p_menu->title.row, p_menu->title.col);
928                            prints("%s", p_menu->title.text);
929                    }
930          }          }
931    
932          if (p_menu->screen_show)          if (p_menu->screen_show)
# Line 764  int display_menu(MENU_SET *p_menu_set) Line 934  int display_menu(MENU_SET *p_menu_set)
934                  p_menu_screen = get_menu_screen_by_id(p_menu_set, p_menu->screen_id);                  p_menu_screen = get_menu_screen_by_id(p_menu_set, p_menu->screen_id);
935                  if (p_menu_screen == NULL)                  if (p_menu_screen == NULL)
936                  {                  {
937                          log_error("get_menu_screen_by_id(%d) return NULL pointer\n", p_menu->screen_id);                          log_error("get_menu_screen_by_id(%d) return NULL pointer", p_menu->screen_id);
938                          return -1;                          return -1;
939                  }                  }
940    
941                  moveto(p_menu->screen_row, p_menu->screen_col);                  row = p_menu->screen_row;
942                    col = p_menu->screen_col;
943    
944                    moveto(row, col);
945                  prints("%s", p_menu_set->p_menu_screen_buf + p_menu_screen->buf_offset);                  prints("%s", p_menu_set->p_menu_screen_buf + p_menu_screen->buf_offset);
946                  iflush();          }
947    
948            menu_item_pos = p_menu_set->menu_item_pos[p_menu_set->choose_step];
949            page_id = p_menu_set->menu_item_page_id[menu_item_pos];
950    
951            while (menu_item_pos >= 0)
952            {
953                    if (p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
954                    {
955                            menu_item_pos++;
956                            break;
957                    }
958    
959                    if (menu_item_pos == 0)
960                    {
961                            break;
962                    }
963    
964                    menu_item_pos--;
965            }
966    
967            for (; menu_item_pos < p_menu->item_count; menu_item_pos++)
968            {
969                    if (p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
970                    {
971                            break;
972                    }
973    
974                    menu_item_id = p_menu->items[menu_item_pos];
975                    p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
976    
977                    if (p_menu_set->menu_item_display[menu_item_pos] == 0)
978                    {
979                            continue;
980                    }
981    
982                    row = p_menu_set->menu_item_r_row[menu_item_pos];
983                    col = p_menu_set->menu_item_r_col[menu_item_pos];
984    
985                    moveto(row, col);
986                    prints("%s", p_menu_item->text);
987            }
988    
989            return 0;
990    }
991    
992    int display_menu(MENU_SET *p_menu_set)
993    {
994            int16_t row = 0;
995            int16_t col = 0;
996            int menu_selectable = 0;
997            MENU_ID menu_id;
998            MENU_ITEM_ID menu_item_id;
999            MENU *p_menu;
1000            MENU_ITEM *p_menu_item;
1001            int16_t menu_item_pos;
1002            int16_t page_id = 0;
1003            int page_item_count = 0;
1004    
1005            menu_id = p_menu_set->menu_id_path[p_menu_set->choose_step];
1006            p_menu = get_menu_by_id(p_menu_set, menu_id);
1007            if (p_menu == NULL)
1008            {
1009                    log_error("get_menu_by_id(%d) return NULL pointer", menu_id);
1010                    if (p_menu_set->choose_step > 0)
1011                    {
1012                            p_menu_set->choose_step--;
1013                            return REDRAW;
1014                    }
1015                    return EXITMENU;
1016            }
1017    
1018            if (p_menu->item_count <= 0) // empty menu
1019            {
1020                    moveto(p_menu->screen_row, p_menu->screen_col);
1021                    clrtoeol();
1022                    prints("没有可选项");
1023                    press_any_key();
1024                    return -1;
1025            }
1026    
1027            menu_item_pos = p_menu_set->menu_item_pos[p_menu_set->choose_step];
1028            menu_item_id = p_menu->items[menu_item_pos];
1029            p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1030            if (p_menu_item == NULL)
1031            {
1032                    log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
1033                    return EXITMENU;
1034            }
1035    
1036            if (menu_item_pos > 0 &&
1037                    !(p_menu->use_filter ? (p_menu->filter_handler((void *)p_menu_item) == 0)
1038                                                             : (checkpriv(&BBS_priv, 0, p_menu_item->priv) == 0 ||
1039                                                                    checklevel2(&BBS_priv, p_menu_item->level) == 0)))
1040            {
1041                    menu_selectable = 1;
1042          }          }
1043    
1044          for (menu_item_pos = 0; menu_item_pos < p_menu->item_count; menu_item_pos++)          for (menu_item_pos = 0; menu_item_pos < p_menu->item_count; menu_item_pos++)
# Line 787  int display_menu(MENU_SET *p_menu_set) Line 1055  int display_menu(MENU_SET *p_menu_set)
1055                          col = p_menu_item->col;                          col = p_menu_item->col;
1056                  }                  }
1057    
1058                  if (checkpriv(&BBS_priv, 0, p_menu_item->priv) == 0 || checklevel(&BBS_priv, p_menu_item->level) == 0)                  p_menu_set->menu_item_page_id[menu_item_pos] = page_id;
1059    
1060                    if (p_menu->use_filter ? (p_menu->filter_handler((void *)p_menu_item) == 0)
1061                                                               : (checkpriv(&BBS_priv, 0, p_menu_item->priv) == 0 ||
1062                                                                      checklevel2(&BBS_priv, p_menu_item->level) == 0))
1063                  {                  {
1064                          p_menu_set->menu_item_display[menu_item_pos] = 0;                          p_menu_set->menu_item_display[menu_item_pos] = 0;
1065                          p_menu_set->menu_item_r_row[menu_item_pos] = 0;                          p_menu_set->menu_item_r_row[menu_item_pos] = 0;
# Line 806  int display_menu(MENU_SET *p_menu_set) Line 1078  int display_menu(MENU_SET *p_menu_set)
1078                          p_menu_set->menu_item_r_row[menu_item_pos] = row;                          p_menu_set->menu_item_r_row[menu_item_pos] = row;
1079                          p_menu_set->menu_item_r_col[menu_item_pos] = col;                          p_menu_set->menu_item_r_col[menu_item_pos] = col;
1080    
                         moveto(row, col);  
                         prints("%s", p_menu_item->text);  
   
1081                          row++;                          row++;
1082    
1083                            page_item_count++;
1084                            if (p_menu->page_item_limit > 0 && page_item_count >= p_menu->page_item_limit)
1085                            {
1086                                    page_id++;
1087                                    page_item_count = 0;
1088                                    row = p_menu->page_row;
1089                                    col = p_menu->page_col;
1090                            }
1091                  }                  }
1092          }          }
1093    
1094          if (!menu_selectable)          if (!menu_selectable)
1095          {          {
1096                    moveto(p_menu->screen_row, p_menu->screen_col);
1097                    clrtoeol();
1098                    prints("没有可选项");
1099                    press_any_key();
1100                    return -1;
1101            }
1102    
1103            if (display_menu_current_page(p_menu_set) != 0)
1104            {
1105                  return -1;                  return -1;
1106          }          }
1107    
# Line 830  int menu_control(MENU_SET *p_menu_set, i Line 1117  int menu_control(MENU_SET *p_menu_set, i
1117          MENU *p_menu;          MENU *p_menu;
1118          MENU_ITEM *p_menu_item;          MENU_ITEM *p_menu_item;
1119          int16_t menu_item_pos;          int16_t menu_item_pos;
1120            int16_t page_id;
1121            int require_page_change = 0;
1122    
1123          if (p_menu_set->menu_count == 0)          if (p_menu_set->menu_count == 0)
1124          {          {
1125                  return 0;                  log_error("Empty menu set");
1126                    return EXITBBS;
1127          }          }
1128    
1129          menu_id = p_menu_set->menu_id_path[p_menu_set->choose_step];          menu_id = p_menu_set->menu_id_path[p_menu_set->choose_step];
1130          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
1131          if (p_menu == NULL)          if (p_menu == NULL)
1132          {          {
1133                  log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id);                  log_error("get_menu_by_id(%d) return NULL pointer", menu_id);
1134                  return -1;                  if (p_menu_set->choose_step > 0)
1135                    {
1136                            p_menu_set->choose_step--;
1137                            return REDRAW;
1138                    }
1139                    return EXITBBS;
1140          }          }
1141    
1142          if (p_menu->item_count == 0)          if (p_menu->item_count == 0)
1143          {          {
1144                  return 0;                  log_debug("Empty menu (%s)", p_menu->name);
1145                    if (p_menu_set->choose_step > 0)
1146                    {
1147                            p_menu_set->choose_step--;
1148                            return REDRAW;
1149                    }
1150                    return EXITBBS;
1151          }          }
1152    
1153          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];
1154            page_id = p_menu_set->menu_item_page_id[menu_item_pos];
1155    
1156          menu_item_id = p_menu->items[menu_item_pos];          menu_item_id = p_menu->items[menu_item_pos];
1157          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1158          if (p_menu_item == NULL)          if (p_menu_item == NULL)
1159          {          {
1160                  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", menu_item_id);
1161                  return -1;                  p_menu_set->menu_item_pos[p_menu_set->choose_step] = 0;
1162                    return REDRAW;
1163          }          }
1164    
1165          switch (key)          switch (key)
1166          {          {
1167          case CR:          case CR:
                 igetch_reset();  
1168          case KEY_RIGHT:          case KEY_RIGHT:
1169                  if (p_menu_item->submenu)                  if (p_menu_item->submenu)
1170                  {                  {
# Line 880  int menu_control(MENU_SET *p_menu_set, i Line 1183  int menu_control(MENU_SET *p_menu_set, i
1183                  }                  }
1184                  else                  else
1185                  {                  {
1186                          return (exec_cmd(p_menu_item->action, p_menu_item->name));                          return ((*(p_menu_item->action_cmd_handler))((void *)(p_menu_item->name)));
1187                  }                  }
1188                  break;                  break;
1189            case KEY_ESC:
1190          case KEY_LEFT:          case KEY_LEFT:
1191                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1192                  {                  {
1193                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
1194                          if (display_menu(p_menu_set) != 0)                          return REDRAW;
                         {  
                                 return menu_control(p_menu_set, KEY_LEFT);  
                         }  
1195                  }                  }
1196                  else                  else
1197                  {                  {
1198                            if (p_menu_set->allow_exit)
1199                            {
1200                                    return EXITMENU;
1201                            }
1202    
1203                          display_menu_cursor(p_menu_set, 0);                          display_menu_cursor(p_menu_set, 0);
1204                          menu_item_pos = p_menu->item_count - 1;                          menu_item_pos = p_menu->item_count - 1;
1205                          while (menu_item_pos >= 0)                          while (menu_item_pos >= 0)
# Line 902  int menu_control(MENU_SET *p_menu_set, i Line 1208  int menu_control(MENU_SET *p_menu_set, i
1208                                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1209                                  if (p_menu_item == NULL)                                  if (p_menu_item == NULL)
1210                                  {                                  {
1211                                          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", menu_item_id);
1212                                          return -1;                                          return -1;
1213                                  }                                  }
1214    
# Line 919  int menu_control(MENU_SET *p_menu_set, i Line 1225  int menu_control(MENU_SET *p_menu_set, i
1225                          display_menu_cursor(p_menu_set, 1);                          display_menu_cursor(p_menu_set, 1);
1226                  }                  }
1227                  break;                  break;
1228            case KEY_PGUP:
1229                    require_page_change = 1;
1230          case KEY_UP:          case KEY_UP:
1231                  display_menu_cursor(p_menu_set, 0);                  display_menu_cursor(p_menu_set, 0);
1232                  do                  do
# Line 927  int menu_control(MENU_SET *p_menu_set, i Line 1235  int menu_control(MENU_SET *p_menu_set, i
1235                          if (menu_item_pos < 0)                          if (menu_item_pos < 0)
1236                          {                          {
1237                                  menu_item_pos = p_menu->item_count - 1;                                  menu_item_pos = p_menu->item_count - 1;
1238                                    require_page_change = 0;
1239                          }                          }
1240                          menu_item_id = p_menu->items[menu_item_pos];                          menu_item_id = p_menu->items[menu_item_pos];
1241                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1242                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1243                          {                          {
1244                                  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", menu_item_id);
1245                                  return -1;                                  return -1;
1246                          }                          }
1247                  } while (!p_menu_set->menu_item_display[menu_item_pos]);                          if (require_page_change && p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
1248                            {
1249                                    require_page_change = 0;
1250                            }
1251                    } while (require_page_change || !p_menu_set->menu_item_display[menu_item_pos]);
1252                  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] = menu_item_pos;
1253                    if (p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
1254                    {
1255                            display_menu_current_page(p_menu_set);
1256                    }
1257                  display_menu_cursor(p_menu_set, 1);                  display_menu_cursor(p_menu_set, 1);
1258                  break;                  break;
1259            case KEY_PGDN:
1260                    require_page_change = 1;
1261          case KEY_DOWN:          case KEY_DOWN:
1262                  display_menu_cursor(p_menu_set, 0);                  display_menu_cursor(p_menu_set, 0);
1263                  do                  do
# Line 947  int menu_control(MENU_SET *p_menu_set, i Line 1266  int menu_control(MENU_SET *p_menu_set, i
1266                          if (menu_item_pos >= p_menu->item_count)                          if (menu_item_pos >= p_menu->item_count)
1267                          {                          {
1268                                  menu_item_pos = 0;                                  menu_item_pos = 0;
1269                                    require_page_change = 0;
1270                          }                          }
1271                          menu_item_id = p_menu->items[menu_item_pos];                          menu_item_id = p_menu->items[menu_item_pos];
1272                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1273                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1274                          {                          {
1275                                  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", menu_item_id);
1276                                  return -1;                                  return -1;
1277                          }                          }
1278                  } while (!p_menu_set->menu_item_display[menu_item_pos]);                          if (require_page_change && p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
1279                            {
1280                                    require_page_change = 0;
1281                            }
1282                    } while (require_page_change || !p_menu_set->menu_item_display[menu_item_pos]);
1283                  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] = menu_item_pos;
1284                    if (p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
1285                    {
1286                            display_menu_current_page(p_menu_set);
1287                    }
1288                  display_menu_cursor(p_menu_set, 1);                  display_menu_cursor(p_menu_set, 1);
1289                  break;                  break;
1290          case KEY_PGUP:          case KEY_HOME:
1291                  display_menu_cursor(p_menu_set, 0);                  display_menu_cursor(p_menu_set, 0);
1292                  menu_item_pos = 0;                  menu_item_pos = 0;
1293                  while (menu_item_pos < p_menu->item_count - 1)                  while (menu_item_pos < p_menu->item_count - 1)
# Line 968  int menu_control(MENU_SET *p_menu_set, i Line 1296  int menu_control(MENU_SET *p_menu_set, i
1296                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1297                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1298                          {                          {
1299                                  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", menu_item_id);
1300                                  return -1;                                  return -1;
1301                          }                          }
1302    
# Line 980  int menu_control(MENU_SET *p_menu_set, i Line 1308  int menu_control(MENU_SET *p_menu_set, i
1308                          menu_item_pos++;                          menu_item_pos++;
1309                  }                  }
1310                  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] = menu_item_pos;
1311                    if (p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
1312                    {
1313                            display_menu_current_page(p_menu_set);
1314                    }
1315                  display_menu_cursor(p_menu_set, 1);                  display_menu_cursor(p_menu_set, 1);
1316                  break;                  break;
1317          case KEY_PGDN:          case KEY_END:
1318                  display_menu_cursor(p_menu_set, 0);                  display_menu_cursor(p_menu_set, 0);
1319                  menu_item_pos = p_menu->item_count - 1;                  menu_item_pos = p_menu->item_count - 1;
1320                  while (menu_item_pos > 0)                  while (menu_item_pos > 0)
# Line 991  int menu_control(MENU_SET *p_menu_set, i Line 1323  int menu_control(MENU_SET *p_menu_set, i
1323                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1324                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1325                          {                          {
1326                                  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", menu_item_id);
1327                                  return -1;                                  return -1;
1328                          }                          }
1329    
# Line 1003  int menu_control(MENU_SET *p_menu_set, i Line 1335  int menu_control(MENU_SET *p_menu_set, i
1335                          menu_item_pos--;                          menu_item_pos--;
1336                  }                  }
1337                  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] = menu_item_pos;
1338                    if (p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
1339                    {
1340                            display_menu_current_page(p_menu_set);
1341                    }
1342                  display_menu_cursor(p_menu_set, 1);                  display_menu_cursor(p_menu_set, 1);
1343                  break;                  break;
1344          default:          default:
# Line 1014  int menu_control(MENU_SET *p_menu_set, i Line 1350  int menu_control(MENU_SET *p_menu_set, i
1350                                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1351                                  if (p_menu_item == NULL)                                  if (p_menu_item == NULL)
1352                                  {                                  {
1353                                          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", menu_item_id);
1354                                          return -1;                                          return -1;
1355                                  }                                  }
1356    
# Line 1022  int menu_control(MENU_SET *p_menu_set, i Line 1358  int menu_control(MENU_SET *p_menu_set, i
1358                                  {                                  {
1359                                          display_menu_cursor(p_menu_set, 0);                                          display_menu_cursor(p_menu_set, 0);
1360                                          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] = menu_item_pos;
1361                                            if (p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
1362                                            {
1363                                                    display_menu_current_page(p_menu_set);
1364                                            }
1365                                          display_menu_cursor(p_menu_set, 1);                                          display_menu_cursor(p_menu_set, 1);
1366                                          return 0;                                          break;
1367                                  }                                  }
1368                          }                          }
1369                  }                  }
1370                  break;                  break;
1371          }          }
1372    
1373          return 0;          return NOREDRAW;
1374  }  }
1375    
1376  int unload_menu(MENU_SET *p_menu_set)  int unload_menu(MENU_SET *p_menu_set)
1377  {  {
1378            if (p_menu_set == NULL)
1379            {
1380                    log_error("NULL pointer error");
1381                    return -1;
1382            }
1383    
1384          if (p_menu_set->p_menu_name_dict != NULL)          if (p_menu_set->p_menu_name_dict != NULL)
1385          {          {
1386                  trie_dict_destroy(p_menu_set->p_menu_name_dict);                  trie_dict_destroy(p_menu_set->p_menu_name_dict);
# Line 1047  int unload_menu(MENU_SET *p_menu_set) Line 1393  int unload_menu(MENU_SET *p_menu_set)
1393                  p_menu_set->p_menu_screen_dict = NULL;                  p_menu_set->p_menu_screen_dict = NULL;
1394          }          }
1395    
1396          unload_menu_shm(p_menu_set);          detach_menu_shm(p_menu_set);
1397    
1398          if (shmctl(p_menu_set->shmid, IPC_RMID, NULL) == -1)          if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT)
1399          {          {
1400                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", p_menu_set->shmid, errno);                  log_error("shm_unlink(%s) error (%d)", p_menu_set->shm_name, errno);
1401                  return -1;                  return -2;
1402          }          }
1403    
1404          return 0;          return 0;
1405  }  }
1406    
1407  int load_menu_shm(MENU_SET *p_menu_set)  int get_menu_shm_readonly(MENU_SET *p_menu_set)
1408  {  {
1409          // Mount shared memory          int fd;
1410          if (p_menu_set->p_reserved != NULL)          void *p_shm;
1411            struct stat sb;
1412            size_t size;
1413    
1414            if (p_menu_set == NULL)
1415          {          {
1416                  log_error("Menu is already loaded\n");                  log_error("NULL pointer error");
1417                  return -1;                  return -1;
1418          }          }
1419    
1420          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, SHM_RDONLY);          if ((fd = shm_open(p_menu_set->shm_name, O_RDONLY, 0600)) == -1)
         if (p_menu_set->p_reserved == (void *)-1)  
1421          {          {
1422                  log_error("shmat() error (%d)\n", errno);                  log_error("shm_open(%s) error (%d)", p_menu_set->shm_name, errno);
1423                    return -2;
1424            }
1425    
1426            if (fstat(fd, &sb) < 0)
1427            {
1428                    log_error("fstat(fd) error (%d)", errno);
1429                    close(fd);
1430                    return -2;
1431            }
1432    
1433            size = (size_t)sb.st_size;
1434    
1435            p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
1436            if (p_shm == MAP_FAILED)
1437            {
1438                    log_error("mmap() error (%d)", errno);
1439                    close(fd);
1440                  return -2;                  return -2;
1441          }          }
         p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH;  
         p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS;  
         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_buf = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS;  
         p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;  
1442    
1443          // Restore status varaibles into reserved memory area          if (close(fd) < 0)
1444          p_menu_set->menu_count = *((int16_t *)p_menu_set->p_reserved);          {
1445          p_menu_set->menu_item_count = *(((int16_t *)p_menu_set->p_reserved) + 1);                  log_error("close(fd) error (%d)", errno);
1446          p_menu_set->menu_screen_count = *(((int16_t *)p_menu_set->p_reserved) + 2);                  return -1;
1447            }
1448    
1449            p_menu_set->shm_size = size;
1450            p_menu_set->p_reserved = p_shm;
1451    
1452            p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
1453            p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
1454            p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
1455            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
1456            p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;
1457    
1458          p_menu_set->choose_step = 0;          p_menu_set->choose_step = 0;
1459          p_menu_set->menu_id_path[0] = 0;          p_menu_set->menu_id_path[0] = 0;
1460            p_menu_set->menu_item_pos[0] = 0;
1461    
1462          p_menu_set->p_menu_name_dict = NULL;          return 0;
1463          p_menu_set->p_menu_screen_dict = NULL;  }
1464    
1465    int set_menu_shm_readonly(MENU_SET *p_menu_set)
1466    {
1467            if (p_menu_set == NULL)
1468            {
1469                    log_error("NULL pointer error");
1470                    return -1;
1471            }
1472    
1473            if (p_menu_set->p_reserved != NULL && mprotect(p_menu_set->p_reserved, p_menu_set->shm_size, PROT_READ) < 0)
1474            {
1475                    log_error("mprotect() error (%d)", errno);
1476                    return -2;
1477            }
1478    
1479          return 0;          return 0;
1480  }  }
1481    
1482  int unload_menu_shm(MENU_SET *p_menu_set)  int detach_menu_shm(MENU_SET *p_menu_set)
1483  {  {
1484            if (p_menu_set == NULL)
1485            {
1486                    log_error("NULL pointer error");
1487                    return -1;
1488            }
1489    
1490          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
1491          p_menu_set->menu_item_count = 0;          p_menu_set->menu_item_count = 0;
1492          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
# Line 1106  int unload_menu_shm(MENU_SET *p_menu_set Line 1498  int unload_menu_shm(MENU_SET *p_menu_set
1498          p_menu_set->p_menu_screen_buf = NULL;          p_menu_set->p_menu_screen_buf = NULL;
1499          p_menu_set->p_menu_screen_buf_free = NULL;          p_menu_set->p_menu_screen_buf_free = NULL;
1500    
1501          if (p_menu_set->p_reserved != NULL && shmdt(p_menu_set->p_reserved) == -1)          p_menu_set->p_menu_name_dict = NULL;
1502            p_menu_set->p_menu_screen_dict = NULL;
1503    
1504            if (p_menu_set->p_reserved != NULL && munmap(p_menu_set->p_reserved, p_menu_set->shm_size) < 0)
1505          {          {
1506                  log_error("shmdt() error (%d)\n", errno);                  log_error("munmap() error (%d)", errno);
1507                  return -1;                  return -2;
1508          }          }
1509    
1510          p_menu_set->p_reserved = NULL;          p_menu_set->p_reserved = NULL;
1511    
1512          return 0;          return 0;


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

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