/[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.78 by sysadm, Wed Oct 29 07:36:38 2025 UTC Revision 1.96 by sysadm, Fri Feb 13 12:38:09 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"
# Line 25  Line 21 
21  #include "user_priv.h"  #include "user_priv.h"
22  #include <ctype.h>  #include <ctype.h>
23  #include <errno.h>  #include <errno.h>
24    #include <fcntl.h>
25  #include <stdio.h>  #include <stdio.h>
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <string.h>  #include <string.h>
28  #include <unistd.h>  #include <unistd.h>
29  #include <sys/ipc.h>  #include <sys/mman.h>
30  #include <sys/shm.h>  #include <sys/stat.h>
   
 #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"  
 #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"  
 #define MENU_CONF_DELIM_WITHOUT_SPACE "\r\n"  
31    
32  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)  enum _menu_constant_t
33    {
34            MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4,
35    };
36    
37  #define MENU_SHMGET_RETRY_LIMIT 3  static const char MENU_CONF_DELIM_WITH_SPACE[] = " ,\t\r\n";
38    static const char MENU_CONF_DELIM_WITHOUT_SPACE[] = "\r\n";
39    
40  MENU_SET bbs_menu;  MENU_SET bbs_menu;
41  MENU_SET top10_menu;  MENU_SET top10_menu;
42    
43    // External definitions for inline functions
44    extern inline MENU *get_menu(MENU_SET *p_menu_set, const char *menu_name);
45    extern inline MENU *get_menu_by_id(MENU_SET *p_menu_set, MENU_ID menu_id);
46    extern inline MENU_ITEM *get_menu_item_by_id(MENU_SET *p_menu_set, MENU_ITEM_ID menu_item_id);
47    extern inline MENU_SCREEN *get_menu_screen_by_id(MENU_SET *p_menu_set, MENU_SCREEN_ID menu_screen_id);
48    
49  int load_menu(MENU_SET *p_menu_set, const char *conf_file)  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
50  {  {
51            char filepath[FILE_PATH_LEN];
52            int fd;
53            size_t size;
54            void *p_shm;
55          FILE *fin;          FILE *fin;
56          int fin_line = 0;          int fin_line = 0;
57          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
# Line 59  int load_menu(MENU_SET *p_menu_set, cons Line 66  int load_menu(MENU_SET *p_menu_set, cons
66          MENU_ID menu_id;          MENU_ID menu_id;
67          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
68          MENU_SCREEN_ID screen_id;          MENU_SCREEN_ID screen_id;
69          int proj_id;  
70          key_t key;          if (p_menu_set == NULL || conf_file == NULL)
71          size_t size;          {
72          int retry_cnt;                  log_error("NULL pointer error");
73                    return -1;
74            }
75    
76          // Initialize the data structure          // Initialize the data structure
77          memset(p_menu_set, 0, sizeof(*p_menu_set));          memset(p_menu_set, 0, sizeof(*p_menu_set));
# Line 71  int load_menu(MENU_SET *p_menu_set, cons Line 80  int load_menu(MENU_SET *p_menu_set, cons
80          p_menu_set->p_menu_name_dict = trie_dict_create();          p_menu_set->p_menu_name_dict = trie_dict_create();
81          if (p_menu_set->p_menu_name_dict == NULL)          if (p_menu_set->p_menu_name_dict == NULL)
82          {          {
83                  log_error("trie_dict_create() error\n");                  log_error("trie_dict_create() error");
84                  return -3;                  return -1;
85          }          }
86    
87          // Use trie_dict to search screen_id by menu screen name          // Use trie_dict to search screen_id by menu screen name
88          p_menu_set->p_menu_screen_dict = trie_dict_create();          p_menu_set->p_menu_screen_dict = trie_dict_create();
89          if (p_menu_set->p_menu_screen_dict == NULL)          if (p_menu_set->p_menu_screen_dict == NULL)
90          {          {
91                  log_error("trie_dict_create() error\n");                  log_error("trie_dict_create() error");
92                  return -3;                  return -1;
93          }          }
94    
95          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
96          {          {
97                  log_error("Open %s failed\n", conf_file);                  log_error("Open %s error: %d", conf_file, errno);
98                  return -2;                  return -2;
99          }          }
100    
# Line 96  int load_menu(MENU_SET *p_menu_set, cons Line 105  int load_menu(MENU_SET *p_menu_set, cons
105                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
106                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
107    
108          proj_id = (int)(time(NULL) % getpid());          strncpy(filepath, conf_file, sizeof(filepath) - 1);
109          retry_cnt = 0;          filepath[sizeof(filepath) - 1] = '\0';
110            snprintf(p_menu_set->shm_name, sizeof(p_menu_set->shm_name), "/MENU_SHM_%s", basename(filepath));
111    
112          do          if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT)
113          {          {
114                  key = ftok(conf_file, proj_id + retry_cnt);                  log_error("shm_unlink(%s) error (%d)", p_menu_set->shm_name, errno);
115                  if (key == -1)                  return -2;
116                  {          }
                         log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id, errno);  
                         return -2;  
                 }  
117    
118                  p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);          if ((fd = shm_open(p_menu_set->shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
119            {
120                    log_error("shm_open(%s) error (%d)", p_menu_set->shm_name, errno);
121                    return -2;
122            }
123            if (ftruncate(fd, (off_t)size) == -1)
124            {
125                    log_error("ftruncate(size=%d) error (%d)", size, errno);
126                    close(fd);
127                    return -2;
128            }
129    
130                  if (p_menu_set->shmid == -1)          p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
131                  {          if (p_shm == MAP_FAILED)
132                          if (errno != EEXIST || retry_cnt + 1 >= MENU_SHMGET_RETRY_LIMIT)          {
133                          {                  log_error("mmap() error (%d)", errno);
134                                  log_error("shmget(conf_file=%s, size=%d) error (%d) %d times\n",                  close(fd);
135                                                    conf_file, size, errno, retry_cnt + 1);                  return -2;
136                                  break;          }
                         }  
                         log_error("shmget(conf_file=%s, proj_id=%d, key=0x%x, size=%d) error (%d), retry ...\n",  
                                           conf_file, proj_id + retry_cnt, key, size, errno);  
                         retry_cnt++;  
                 }  
         } while (p_menu_set->shmid == -1);  
137    
138          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);          if (close(fd) < 0)
         if (p_menu_set->p_reserved == (void *)-1)  
139          {          {
140                  log_error("shmat() error (%d)\n", errno);                  log_error("close(fd) error (%d)", errno);
141                  return -3;                  return -1;
142          }          }
143    
144            p_menu_set->shm_size = size;
145            p_menu_set->p_reserved = p_shm;
146    
147          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
148          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
149          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
# Line 168  int load_menu(MENU_SET *p_menu_set, cons Line 181  int load_menu(MENU_SET *p_menu_set, cons
181                          {                          {
182                                  if (p_menu != NULL)                                  if (p_menu != NULL)
183                                  {                                  {
184                                          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);
185                                          return -1;                                          return -1;
186                                  }                                  }
187    
188                                  if (p_menu_set->menu_count >= MAX_MENUS)                                  if (p_menu_set->menu_count >= MAX_MENUS)
189                                  {                                  {
190                                          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);
191                                          return -3;                                          return -3;
192                                  }                                  }
193                                  menu_id = (MENU_ID)p_menu_set->menu_count;                                  menu_id = (MENU_ID)p_menu_set->menu_count;
# Line 192  int load_menu(MENU_SET *p_menu_set, cons Line 205  int load_menu(MENU_SET *p_menu_set, cons
205                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
206                                  if (q == NULL)                                  if (q == NULL)
207                                  {                                  {
208                                          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);
209                                          return -1;                                          return -1;
210                                  }                                  }
211                                  p = q;                                  p = q;
212                                  while (isalnum(*q) || *q == '_' || *q == '-')                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
213                                  {                                  {
214                                          q++;                                          q++;
215                                  }                                  }
216                                  if (*q != '\0')                                  if (*q != '\0')
217                                  {                                  {
218                                          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);
219                                          return -1;                                          return -1;
220                                  }                                  }
221    
222                                  if (q - p > sizeof(p_menu->name) - 1)                                  if (q - p > sizeof(p_menu->name) - 1)
223                                  {                                  {
224                                          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);
225                                          return -1;                                          return -1;
226                                  }                                  }
227                                  strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);                                  strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);
# Line 216  int load_menu(MENU_SET *p_menu_set, cons Line 229  int load_menu(MENU_SET *p_menu_set, cons
229    
230                                  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)
231                                  {                                  {
232                                          log_error("Error set menu dict [%s]\n", p_menu->name);                                          log_error("Error set menu dict [%s]", p_menu->name);
233                                  }                                  }
234    
235                                  // Check syntax                                  // Check syntax
236                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
237                                  if (q != NULL)                                  if (q != NULL)
238                                  {                                  {
239                                          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);
240                                          return -1;                                          return -1;
241                                  }                                  }
242    
# Line 252  int load_menu(MENU_SET *p_menu_set, cons Line 265  int load_menu(MENU_SET *p_menu_set, cons
265                                                  // BEGIN of menu item                                                  // BEGIN of menu item
266                                                  if (p_menu->item_count >= MAX_ITEMS_PER_MENU)                                                  if (p_menu->item_count >= MAX_ITEMS_PER_MENU)
267                                                  {                                                  {
268                                                          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);
269                                                          return -1;                                                          return -1;
270                                                  }                                                  }
271                                                  if (p_menu_set->menu_item_count >= MAX_MENUITEMS)                                                  if (p_menu_set->menu_item_count >= MAX_MENUITEMS)
272                                                  {                                                  {
273                                                          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);
274                                                          return -3;                                                          return -3;
275                                                  }                                                  }
276                                                  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 279  int load_menu(MENU_SET *p_menu_set, cons Line 292  int load_menu(MENU_SET *p_menu_set, cons
292                                                  else                                                  else
293                                                  {                                                  {
294                                                          q = p;                                                          q = p;
295                                                          while (isalnum(*q) || *q == '_' || *q == '-')                                                          while (isalnum((int)*q) || *q == '_' || *q == '-')
296                                                          {                                                          {
297                                                                  q++;                                                                  q++;
298                                                          }                                                          }
299                                                          if (*q != '\0')                                                          if (*q != '\0')
300                                                          {                                                          {
301                                                                  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);
302                                                                  return -1;                                                                  return -1;
303                                                          }                                                          }
304                                                  }                                                  }
305    
306                                                  if (q - p > sizeof(p_menu_item->action) - 1)                                                  if (q - p > sizeof(p_menu_item->action) - 1)
307                                                  {                                                  {
308                                                          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);
309                                                          return -1;                                                          return -1;
310                                                  }                                                  }
311                                                  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 302  int load_menu(MENU_SET *p_menu_set, cons Line 315  int load_menu(MENU_SET *p_menu_set, cons
315                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
316                                                  if (q == NULL)                                                  if (q == NULL)
317                                                  {                                                  {
318                                                          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);
319                                                          return -1;                                                          return -1;
320                                                  }                                                  }
321                                                  p = q;                                                  p = q;
322                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
323                                                  {                                                  {
324                                                          q++;                                                          q++;
325                                                  }                                                  }
326                                                  if (*q != '\0')                                                  if (*q != '\0')
327                                                  {                                                  {
328                                                          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);
329                                                          return -1;                                                          return -1;
330                                                  }                                                  }
331                                                  p_menu_item->row = (int16_t)atoi(p);                                                  p_menu_item->row = (int16_t)atoi(p);
# Line 321  int load_menu(MENU_SET *p_menu_set, cons Line 334  int load_menu(MENU_SET *p_menu_set, cons
334                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
335                                                  if (q == NULL)                                                  if (q == NULL)
336                                                  {                                                  {
337                                                          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);
338                                                          return -1;                                                          return -1;
339                                                  }                                                  }
340                                                  p = q;                                                  p = q;
341                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
342                                                  {                                                  {
343                                                          q++;                                                          q++;
344                                                  }                                                  }
345                                                  if (*q != '\0')                                                  if (*q != '\0')
346                                                  {                                                  {
347                                                          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);
348                                                          return -1;                                                          return -1;
349                                                  }                                                  }
350                                                  p_menu_item->col = (int16_t)atoi(p);                                                  p_menu_item->col = (int16_t)atoi(p);
# Line 340  int load_menu(MENU_SET *p_menu_set, cons Line 353  int load_menu(MENU_SET *p_menu_set, cons
353                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
354                                                  if (q == NULL)                                                  if (q == NULL)
355                                                  {                                                  {
356                                                          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);
357                                                          return -1;                                                          return -1;
358                                                  }                                                  }
359                                                  p = q;                                                  p = q;
360                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
361                                                  {                                                  {
362                                                          q++;                                                          q++;
363                                                  }                                                  }
364                                                  if (*q != '\0')                                                  if (*q != '\0')
365                                                  {                                                  {
366                                                          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);
367                                                          return -1;                                                          return -1;
368                                                  }                                                  }
369                                                  p_menu_item->priv = atoi(p);                                                  p_menu_item->priv = atoi(p);
# Line 359  int load_menu(MENU_SET *p_menu_set, cons Line 372  int load_menu(MENU_SET *p_menu_set, cons
372                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
373                                                  if (q == NULL)                                                  if (q == NULL)
374                                                  {                                                  {
375                                                          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);
376                                                          return -1;                                                          return -1;
377                                                  }                                                  }
378                                                  p = q;                                                  p = q;
379                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
380                                                  {                                                  {
381                                                          q++;                                                          q++;
382                                                  }                                                  }
383                                                  if (*q != '\0')                                                  if (*q != '\0')
384                                                  {                                                  {
385                                                          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);
386                                                          return -1;                                                          return -1;
387                                                  }                                                  }
388                                                  p_menu_item->level = atoi(p);                                                  p_menu_item->level = atoi(p);
# Line 378  int load_menu(MENU_SET *p_menu_set, cons Line 391  int load_menu(MENU_SET *p_menu_set, cons
391                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
392                                                  if (q == NULL || *q != '\"')                                                  if (q == NULL || *q != '\"')
393                                                  {                                                  {
394                                                          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);
395                                                          return -1;                                                          return -1;
396                                                  }                                                  }
397                                                  q++;                                                  q++;
# Line 398  int load_menu(MENU_SET *p_menu_set, cons Line 411  int load_menu(MENU_SET *p_menu_set, cons
411                                                  }                                                  }
412                                                  if (*q != '\"' || *(q + 1) != '\0')                                                  if (*q != '\"' || *(q + 1) != '\0')
413                                                  {                                                  {
414                                                          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);
415                                                          return -1;                                                          return -1;
416                                                  }                                                  }
417                                                  *q = '\0';                                                  *q = '\0';
418    
419                                                  if (q - p > sizeof(p_menu_item->name) - 1)                                                  if (q - p > sizeof(p_menu_item->name) - 1)
420                                                  {                                                  {
421                                                          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);
422                                                          return -1;                                                          return -1;
423                                                  }                                                  }
424                                                  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 415  int load_menu(MENU_SET *p_menu_set, cons Line 428  int load_menu(MENU_SET *p_menu_set, cons
428                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
429                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)
430                                                  {                                                  {
431                                                          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);
432                                                          return -1;                                                          return -1;
433                                                  }                                                  }
434                                                  q++;                                                  q++;
# Line 435  int load_menu(MENU_SET *p_menu_set, cons Line 448  int load_menu(MENU_SET *p_menu_set, cons
448                                                  }                                                  }
449                                                  if (*q != '\"')                                                  if (*q != '\"')
450                                                  {                                                  {
451                                                          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);
452                                                          return -1;                                                          return -1;
453                                                  }                                                  }
454                                                  *q = '\0';                                                  *q = '\0';
455    
456                                                  if (q - p > sizeof(p_menu_item->text) - 1)                                                  if (q - p > sizeof(p_menu_item->text) - 1)
457                                                  {                                                  {
458                                                          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);
459                                                          return -1;                                                          return -1;
460                                                  }                                                  }
461                                                  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 452  int load_menu(MENU_SET *p_menu_set, cons Line 465  int load_menu(MENU_SET *p_menu_set, cons
465                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
466                                                  if (q != NULL)                                                  if (q != NULL)
467                                                  {                                                  {
468                                                          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);
469                                                          return -1;                                                          return -1;
470                                                  }                                                  }
471                                          }                                          }
# Line 464  int load_menu(MENU_SET *p_menu_set, cons Line 477  int load_menu(MENU_SET *p_menu_set, cons
477                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
478                                                  if (q == NULL)                                                  if (q == NULL)
479                                                  {                                                  {
480                                                          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);
481                                                          return -1;                                                          return -1;
482                                                  }                                                  }
483                                                  p = q;                                                  p = q;
484                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
485                                                  {                                                  {
486                                                          q++;                                                          q++;
487                                                  }                                                  }
488                                                  if (*q != '\0')                                                  if (*q != '\0')
489                                                  {                                                  {
490                                                          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);
491                                                          return -1;                                                          return -1;
492                                                  }                                                  }
493                                                  p_menu->title.row = (int16_t)atoi(p);                                                  p_menu->title.row = (int16_t)atoi(p);
# Line 483  int load_menu(MENU_SET *p_menu_set, cons Line 496  int load_menu(MENU_SET *p_menu_set, cons
496                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
497                                                  if (q == NULL)                                                  if (q == NULL)
498                                                  {                                                  {
499                                                          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);
500                                                          return -1;                                                          return -1;
501                                                  }                                                  }
502                                                  p = q;                                                  p = q;
503                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
504                                                  {                                                  {
505                                                          q++;                                                          q++;
506                                                  }                                                  }
507                                                  if (*q != '\0')                                                  if (*q != '\0')
508                                                  {                                                  {
509                                                          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);
510                                                          return -1;                                                          return -1;
511                                                  }                                                  }
512                                                  p_menu->title.col = (int16_t)atoi(p);                                                  p_menu->title.col = (int16_t)atoi(p);
# Line 502  int load_menu(MENU_SET *p_menu_set, cons Line 515  int load_menu(MENU_SET *p_menu_set, cons
515                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
516                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)
517                                                  {                                                  {
518                                                          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);
519                                                          return -1;                                                          return -1;
520                                                  }                                                  }
521                                                  q++;                                                  q++;
# Line 522  int load_menu(MENU_SET *p_menu_set, cons Line 535  int load_menu(MENU_SET *p_menu_set, cons
535                                                  }                                                  }
536                                                  if (*q != '\"')                                                  if (*q != '\"')
537                                                  {                                                  {
538                                                          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);
539                                                          return -1;                                                          return -1;
540                                                  }                                                  }
541                                                  *q = '\0';                                                  *q = '\0';
542    
543                                                  if (q - p > sizeof(p_menu->title.text) - 1)                                                  if (q - p > sizeof(p_menu->title.text) - 1)
544                                                  {                                                  {
545                                                          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);
546                                                          return -1;                                                          return -1;
547                                                  }                                                  }
548                                                  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 539  int load_menu(MENU_SET *p_menu_set, cons Line 552  int load_menu(MENU_SET *p_menu_set, cons
552                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
553                                                  if (q != NULL)                                                  if (q != NULL)
554                                                  {                                                  {
555                                                          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);
556                                                          return -1;                                                          return -1;
557                                                  }                                                  }
558                                          }                                          }
# Line 551  int load_menu(MENU_SET *p_menu_set, cons Line 564  int load_menu(MENU_SET *p_menu_set, cons
564                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
565                                                  if (q == NULL)                                                  if (q == NULL)
566                                                  {                                                  {
567                                                          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);
568                                                          return -1;                                                          return -1;
569                                                  }                                                  }
570                                                  p = q;                                                  p = q;
571                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
572                                                  {                                                  {
573                                                          q++;                                                          q++;
574                                                  }                                                  }
575                                                  if (*q != '\0')                                                  if (*q != '\0')
576                                                  {                                                  {
577                                                          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);
578                                                          return -1;                                                          return -1;
579                                                  }                                                  }
580                                                  p_menu->screen_row = (int16_t)atoi(p);                                                  p_menu->screen_row = (int16_t)atoi(p);
# Line 570  int load_menu(MENU_SET *p_menu_set, cons Line 583  int load_menu(MENU_SET *p_menu_set, cons
583                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
584                                                  if (q == NULL)                                                  if (q == NULL)
585                                                  {                                                  {
586                                                          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);
587                                                          return -1;                                                          return -1;
588                                                  }                                                  }
589                                                  p = q;                                                  p = q;
590                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
591                                                  {                                                  {
592                                                          q++;                                                          q++;
593                                                  }                                                  }
594                                                  if (*q != '\0')                                                  if (*q != '\0')
595                                                  {                                                  {
596                                                          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);
597                                                          return -1;                                                          return -1;
598                                                  }                                                  }
599                                                  p_menu->screen_col = (int16_t)atoi(p);                                                  p_menu->screen_col = (int16_t)atoi(p);
# Line 589  int load_menu(MENU_SET *p_menu_set, cons Line 602  int load_menu(MENU_SET *p_menu_set, cons
602                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
603                                                  if (q == NULL)                                                  if (q == NULL)
604                                                  {                                                  {
605                                                          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);
606                                                          return -1;                                                          return -1;
607                                                  }                                                  }
608                                                  p = q;                                                  p = q;
609                                                  while (isalnum(*q) || *q == '_' || *q == '-')                                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
610                                                  {                                                  {
611                                                          q++;                                                          q++;
612                                                  }                                                  }
613                                                  if (*q != '\0')                                                  if (*q != '\0')
614                                                  {                                                  {
615                                                          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);
616                                                          return -1;                                                          return -1;
617                                                  }                                                  }
618                                                  strncpy(p_menu->screen_name, p, sizeof(p_menu->screen_name) - 1);                                                  strncpy(p_menu->screen_name, p, sizeof(p_menu->screen_name) - 1);
# Line 609  int load_menu(MENU_SET *p_menu_set, cons Line 622  int load_menu(MENU_SET *p_menu_set, cons
622                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
623                                                  if (q != NULL)                                                  if (q != NULL)
624                                                  {                                                  {
625                                                          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);
626                                                          return -1;                                                          return -1;
627                                                  }                                                  }
628                                          }                                          }
# Line 619  int load_menu(MENU_SET *p_menu_set, cons Line 632  int load_menu(MENU_SET *p_menu_set, cons
632                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
633                                                  if (q == NULL)                                                  if (q == NULL)
634                                                  {                                                  {
635                                                          log_error("Error menu page row in menu config line %d\n", fin_line);                                                          log_error("Error menu page row in menu config line %d", fin_line);
636                                                          return -1;                                                          return -1;
637                                                  }                                                  }
638                                                  p = q;                                                  p = q;
639                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
640                                                  {                                                  {
641                                                          q++;                                                          q++;
642                                                  }                                                  }
643                                                  if (*q != '\0')                                                  if (*q != '\0')
644                                                  {                                                  {
645                                                          log_error("Error menu page row in menu config line %d\n", fin_line);                                                          log_error("Error menu page row in menu config line %d", fin_line);
646                                                          return -1;                                                          return -1;
647                                                  }                                                  }
648                                                  p_menu->page_row = (int16_t)atoi(p);                                                  p_menu->page_row = (int16_t)atoi(p);
# Line 638  int load_menu(MENU_SET *p_menu_set, cons Line 651  int load_menu(MENU_SET *p_menu_set, cons
651                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
652                                                  if (q == NULL)                                                  if (q == NULL)
653                                                  {                                                  {
654                                                          log_error("Error menu page col in menu config line %d\n", fin_line);                                                          log_error("Error menu page col in menu config line %d", fin_line);
655                                                          return -1;                                                          return -1;
656                                                  }                                                  }
657                                                  p = q;                                                  p = q;
658                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
659                                                  {                                                  {
660                                                          q++;                                                          q++;
661                                                  }                                                  }
662                                                  if (*q != '\0')                                                  if (*q != '\0')
663                                                  {                                                  {
664                                                          log_error("Error menu page col in menu config line %d\n", fin_line);                                                          log_error("Error menu page col in menu config line %d", fin_line);
665                                                          return -1;                                                          return -1;
666                                                  }                                                  }
667                                                  p_menu->page_col = (int16_t)atoi(p);                                                  p_menu->page_col = (int16_t)atoi(p);
# Line 657  int load_menu(MENU_SET *p_menu_set, cons Line 670  int load_menu(MENU_SET *p_menu_set, cons
670                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
671                                                  if (q == NULL)                                                  if (q == NULL)
672                                                  {                                                  {
673                                                          log_error("Error menu page item limit in menu config line %d\n", fin_line);                                                          log_error("Error menu page item limit in menu config line %d", fin_line);
674                                                          return -1;                                                          return -1;
675                                                  }                                                  }
676                                                  p = q;                                                  p = q;
677                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
678                                                  {                                                  {
679                                                          q++;                                                          q++;
680                                                  }                                                  }
681                                                  if (*q != '\0')                                                  if (*q != '\0')
682                                                  {                                                  {
683                                                          log_error("Error menu page item limit in menu config line %d\n", fin_line);                                                          log_error("Error menu page item limit in menu config line %d", fin_line);
684                                                          return -1;                                                          return -1;
685                                                  }                                                  }
686                                                  p_menu->page_item_limit = (int16_t)atoi(p);                                                  p_menu->page_item_limit = (int16_t)atoi(p);
# Line 676  int load_menu(MENU_SET *p_menu_set, cons Line 689  int load_menu(MENU_SET *p_menu_set, cons
689                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
690                                                  if (q != NULL)                                                  if (q != NULL)
691                                                  {                                                  {
692                                                          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);
693                                                          return -1;                                                          return -1;
694                                                  }                                                  }
695                                          }                                          }
# Line 688  int load_menu(MENU_SET *p_menu_set, cons Line 701  int load_menu(MENU_SET *p_menu_set, cons
701                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
702                                                  if (q != NULL)                                                  if (q != NULL)
703                                                  {                                                  {
704                                                          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);
705                                                          return -1;                                                          return -1;
706                                                  }                                                  }
707                                          }                                          }
# Line 698  int load_menu(MENU_SET *p_menu_set, cons Line 711  int load_menu(MENU_SET *p_menu_set, cons
711                          {                          {
712                                  if (p_menu_set->menu_item_count >= MAX_MENUS)                                  if (p_menu_set->menu_item_count >= MAX_MENUS)
713                                  {                                  {
714                                          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);
715                                          return -3;                                          return -3;
716                                  }                                  }
717                                  screen_id = (MENU_SCREEN_ID)p_menu_set->menu_screen_count;                                  screen_id = (MENU_SCREEN_ID)p_menu_set->menu_screen_count;
# Line 707  int load_menu(MENU_SET *p_menu_set, cons Line 720  int load_menu(MENU_SET *p_menu_set, cons
720                                  p_screen = get_menu_screen_by_id(p_menu_set, screen_id);                                  p_screen = get_menu_screen_by_id(p_menu_set, screen_id);
721    
722                                  q = p;                                  q = p;
723                                  while (isalnum(*q) || *q == '_' || *q == '-')                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
724                                  {                                  {
725                                          q++;                                          q++;
726                                  }                                  }
727                                  if (*q != '\0')                                  if (*q != '\0')
728                                  {                                  {
729                                          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);
730                                          return -1;                                          return -1;
731                                  }                                  }
732                                  strncpy(p_screen->name, p, sizeof(p_screen->name) - 1);                                  strncpy(p_screen->name, p, sizeof(p_screen->name) - 1);
# Line 721  int load_menu(MENU_SET *p_menu_set, cons Line 734  int load_menu(MENU_SET *p_menu_set, cons
734    
735                                  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)
736                                  {                                  {
737                                          log_error("Error set menu screen dict [%s]\n", p_screen->name);                                          log_error("Error set menu screen dict [%s]", p_screen->name);
738                                  }                                  }
739    
740                                  // Check syntax                                  // Check syntax
741                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
742                                  if (q != NULL)                                  if (q != NULL)
743                                  {                                  {
744                                          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);
745                                          return -1;                                          return -1;
746                                  }                                  }
747    
# Line 749  int load_menu(MENU_SET *p_menu_set, cons Line 762  int load_menu(MENU_SET *p_menu_set, cons
762                                          {                                          {
763                                                  if (p_menu_set->p_menu_screen_buf_free + 1 > q)                                                  if (p_menu_set->p_menu_screen_buf_free + 1 > q)
764                                                  {                                                  {
765                                                          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);
766                                                          return -3;                                                          return -3;
767                                                  }                                                  }
768    
# Line 762  int load_menu(MENU_SET *p_menu_set, cons Line 775  int load_menu(MENU_SET *p_menu_set, cons
775                                          // Clear line                                          // Clear line
776                                          if (p_menu_set->p_menu_screen_buf_free + strlen(CTRL_SEQ_CLR_LINE) > q)                                          if (p_menu_set->p_menu_screen_buf_free + strlen(CTRL_SEQ_CLR_LINE) > q)
777                                          {                                          {
778                                                  log_error("Menu screen buffer depleted (%p + %d > %p)\n", p_menu_set->p_menu_screen_buf_free, q, strlen(CTRL_SEQ_CLR_LINE));                                                  log_error("Menu screen buffer depleted (%p + %d > %p)", p_menu_set->p_menu_screen_buf_free, q, strlen(CTRL_SEQ_CLR_LINE));
779                                                  return -3;                                                  return -3;
780                                          }                                          }
781                                          p_menu_set->p_menu_screen_buf_free = stpcpy(p_menu_set->p_menu_screen_buf_free, CTRL_SEQ_CLR_LINE);                                          p_menu_set->p_menu_screen_buf_free = stpcpy(p_menu_set->p_menu_screen_buf_free, CTRL_SEQ_CLR_LINE);
# Line 772  int load_menu(MENU_SET *p_menu_set, cons Line 785  int load_menu(MENU_SET *p_menu_set, cons
785                                          {                                          {
786                                                  if (p_menu_set->p_menu_screen_buf_free + 2 > q)                                                  if (p_menu_set->p_menu_screen_buf_free + 2 > q)
787                                                  {                                                  {
788                                                          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);
789                                                          return -3;                                                          return -3;
790                                                  }                                                  }
791    
# Line 790  int load_menu(MENU_SET *p_menu_set, cons Line 803  int load_menu(MENU_SET *p_menu_set, cons
803    
804                                  if (p_screen->buf_length == -1)                                  if (p_screen->buf_length == -1)
805                                  {                                  {
806                                          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);
807                                  }                                  }
808                          }                          }
809                  }                  }
810                  else // Invalid prefix                  else // Invalid prefix
811                  {                  {
812                          log_error("Error in menu config line %d\n", fin_line);                          log_error("Error in menu config line %d", fin_line);
813                          return -1;                          return -1;
814                  }                  }
815          }          }
# Line 808  int load_menu(MENU_SET *p_menu_set, cons Line 821  int load_menu(MENU_SET *p_menu_set, cons
821    
822                  if (trie_dict_get(p_menu_set->p_menu_screen_dict, p_menu->screen_name, (int64_t *)(&(p_menu->screen_id))) != 1)                  if (trie_dict_get(p_menu_set->p_menu_screen_dict, p_menu->screen_name, (int64_t *)(&(p_menu->screen_id))) != 1)
823                  {                  {
824                          log_error("Undefined menu screen [%s]\n", p);                          log_error("Undefined menu screen [%s]", p);
825                          return -1;                          return -1;
826                  }                  }
827    
# Line 817  int load_menu(MENU_SET *p_menu_set, cons Line 830  int load_menu(MENU_SET *p_menu_set, cons
830                  {                  {
831                          if ((p_menu->filter_handler = get_cmd_handler(p_menu->name)) == NULL)                          if ((p_menu->filter_handler = get_cmd_handler(p_menu->name)) == NULL)
832                          {                          {
833                                  log_error("Undefined menu filter handler [%s]\n", p_menu->name);                                  log_error("Undefined menu filter handler [%s]", p_menu->name);
834                                  return -1;                                  return -1;
835                          }                          }
836                  }                  }
# Line 832  int load_menu(MENU_SET *p_menu_set, cons Line 845  int load_menu(MENU_SET *p_menu_set, cons
845                  {                  {
846                          if ((p_menu_item->action_cmd_handler = get_cmd_handler(p_menu_item->action)) == NULL)                          if ((p_menu_item->action_cmd_handler = get_cmd_handler(p_menu_item->action)) == NULL)
847                          {                          {
848                                  log_error("Undefined menu action cmd handler [%s]\n", p_menu_item->action);                                  log_error("Undefined menu action cmd handler [%s]", p_menu_item->action);
849                                  return -1;                                  return -1;
850                          }                          }
851                  }                  }
# Line 841  int load_menu(MENU_SET *p_menu_set, cons Line 854  int load_menu(MENU_SET *p_menu_set, cons
854                  {                  {
855                          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)
856                          {                          {
857                                  log_error("Undefined sub menu id [%s]\n", p_menu_item->action);                                  log_error("Undefined sub menu id [%s]", p_menu_item->action);
858                                  return -1;                                  return -1;
859                          }                          }
860                          p_menu_item->action_menu_id = menu_id;                          p_menu_item->action_menu_id = menu_id;
# Line 868  int display_menu_cursor(MENU_SET *p_menu Line 881  int display_menu_cursor(MENU_SET *p_menu
881          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
882          if (p_menu == NULL)          if (p_menu == NULL)
883          {          {
884                  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);
885                  return -1;                  return -1;
886          }          }
887    
# Line 877  int display_menu_cursor(MENU_SET *p_menu Line 890  int display_menu_cursor(MENU_SET *p_menu
890          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);
891          if (p_menu_item == NULL)          if (p_menu_item == NULL)
892          {          {
893                  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);
894                  return -1;                  return -1;
895          }          }
896    
# Line 903  static int display_menu_current_page(MEN Line 916  static int display_menu_current_page(MEN
916          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
917          if (p_menu == NULL)          if (p_menu == NULL)
918          {          {
919                  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);
920                  return -1;                  return -1;
921          }          }
922    
# Line 927  static int display_menu_current_page(MEN Line 940  static int display_menu_current_page(MEN
940                  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);
941                  if (p_menu_screen == NULL)                  if (p_menu_screen == NULL)
942                  {                  {
943                          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);
944                          return -1;                          return -1;
945                  }                  }
946    
# Line 999  int display_menu(MENU_SET *p_menu_set) Line 1012  int display_menu(MENU_SET *p_menu_set)
1012          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
1013          if (p_menu == NULL)          if (p_menu == NULL)
1014          {          {
1015                  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);
1016                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1017                  {                  {
1018                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 1022  int display_menu(MENU_SET *p_menu_set) Line 1035  int display_menu(MENU_SET *p_menu_set)
1035          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);
1036          if (p_menu_item == NULL)          if (p_menu_item == NULL)
1037          {          {
1038                  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);
1039                  return EXITMENU;                  return EXITMENU;
1040          }          }
1041    
# Line 1115  int menu_control(MENU_SET *p_menu_set, i Line 1128  int menu_control(MENU_SET *p_menu_set, i
1128    
1129          if (p_menu_set->menu_count == 0)          if (p_menu_set->menu_count == 0)
1130          {          {
1131                  log_error("Empty menu set\n");                  log_error("Empty menu set");
1132                  return EXITBBS;                  return EXITBBS;
1133          }          }
1134    
# Line 1123  int menu_control(MENU_SET *p_menu_set, i Line 1136  int menu_control(MENU_SET *p_menu_set, i
1136          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
1137          if (p_menu == NULL)          if (p_menu == NULL)
1138          {          {
1139                  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);
1140                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1141                  {                  {
1142                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 1134  int menu_control(MENU_SET *p_menu_set, i Line 1147  int menu_control(MENU_SET *p_menu_set, i
1147    
1148          if (p_menu->item_count == 0)          if (p_menu->item_count == 0)
1149          {          {
1150  #ifdef _DEBUG                  log_debug("Empty menu (%s)", p_menu->name);
                 log_error("Empty menu (%s)\n", p_menu->name);  
 #endif  
1151                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1152                  {                  {
1153                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 1152  int menu_control(MENU_SET *p_menu_set, i Line 1163  int menu_control(MENU_SET *p_menu_set, i
1163          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);
1164          if (p_menu_item == NULL)          if (p_menu_item == NULL)
1165          {          {
1166                  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);
1167                  p_menu_set->menu_item_pos[p_menu_set->choose_step] = 0;                  p_menu_set->menu_item_pos[p_menu_set->choose_step] = 0;
1168                  return REDRAW;                  return REDRAW;
1169          }          }
# Line 1203  int menu_control(MENU_SET *p_menu_set, i Line 1214  int menu_control(MENU_SET *p_menu_set, i
1214                                  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);
1215                                  if (p_menu_item == NULL)                                  if (p_menu_item == NULL)
1216                                  {                                  {
1217                                          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);
1218                                          return -1;                                          return -1;
1219                                  }                                  }
1220    
# Line 1236  int menu_control(MENU_SET *p_menu_set, i Line 1247  int menu_control(MENU_SET *p_menu_set, i
1247                          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);
1248                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1249                          {                          {
1250                                  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);
1251                                  return -1;                                  return -1;
1252                          }                          }
1253                          if (require_page_change && p_menu_set->menu_item_page_id[menu_item_pos] != page_id)                          if (require_page_change && p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
# Line 1267  int menu_control(MENU_SET *p_menu_set, i Line 1278  int menu_control(MENU_SET *p_menu_set, i
1278                          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);
1279                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1280                          {                          {
1281                                  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);
1282                                  return -1;                                  return -1;
1283                          }                          }
1284                          if (require_page_change && p_menu_set->menu_item_page_id[menu_item_pos] != page_id)                          if (require_page_change && p_menu_set->menu_item_page_id[menu_item_pos] != page_id)
# Line 1291  int menu_control(MENU_SET *p_menu_set, i Line 1302  int menu_control(MENU_SET *p_menu_set, i
1302                          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);
1303                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1304                          {                          {
1305                                  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);
1306                                  return -1;                                  return -1;
1307                          }                          }
1308    
# Line 1318  int menu_control(MENU_SET *p_menu_set, i Line 1329  int menu_control(MENU_SET *p_menu_set, i
1329                          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);
1330                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1331                          {                          {
1332                                  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);
1333                                  return -1;                                  return -1;
1334                          }                          }
1335    
# Line 1345  int menu_control(MENU_SET *p_menu_set, i Line 1356  int menu_control(MENU_SET *p_menu_set, i
1356                                  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);
1357                                  if (p_menu_item == NULL)                                  if (p_menu_item == NULL)
1358                                  {                                  {
1359                                          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);
1360                                          return -1;                                          return -1;
1361                                  }                                  }
1362    
# Line 1370  int menu_control(MENU_SET *p_menu_set, i Line 1381  int menu_control(MENU_SET *p_menu_set, i
1381    
1382  int unload_menu(MENU_SET *p_menu_set)  int unload_menu(MENU_SET *p_menu_set)
1383  {  {
         int shmid;  
   
1384          if (p_menu_set == NULL)          if (p_menu_set == NULL)
1385          {          {
1386                    log_error("NULL pointer error");
1387                  return -1;                  return -1;
1388          }          }
1389    
# Line 1389  int unload_menu(MENU_SET *p_menu_set) Line 1399  int unload_menu(MENU_SET *p_menu_set)
1399                  p_menu_set->p_menu_screen_dict = NULL;                  p_menu_set->p_menu_screen_dict = NULL;
1400          }          }
1401    
         shmid = p_menu_set->shmid;  
   
1402          detach_menu_shm(p_menu_set);          detach_menu_shm(p_menu_set);
1403    
1404          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT)
1405          {          {
1406                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shm_unlink(%s) error (%d)", p_menu_set->shm_name, errno);
1407                  return -1;                  return -2;
1408          }          }
1409    
1410          return 0;          return 0;
# Line 1404  int unload_menu(MENU_SET *p_menu_set) Line 1412  int unload_menu(MENU_SET *p_menu_set)
1412    
1413  int get_menu_shm_readonly(MENU_SET *p_menu_set)  int get_menu_shm_readonly(MENU_SET *p_menu_set)
1414  {  {
1415            int fd;
1416          void *p_shm;          void *p_shm;
1417            struct stat sb;
1418            size_t size;
1419    
1420          p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY);          if (p_menu_set == NULL)
         if (p_shm == (void *)-1)  
1421          {          {
1422                  log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);                  log_error("NULL pointer error");
1423                  return -1;                  return -1;
1424          }          }
1425    
1426            if ((fd = shm_open(p_menu_set->shm_name, O_RDONLY, 0600)) == -1)
1427            {
1428                    log_error("shm_open(%s) error (%d)", p_menu_set->shm_name, errno);
1429                    return -2;
1430            }
1431    
1432            if (fstat(fd, &sb) < 0)
1433            {
1434                    log_error("fstat(fd) error (%d)", errno);
1435                    close(fd);
1436                    return -2;
1437            }
1438    
1439            size = (size_t)sb.st_size;
1440    
1441            p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
1442            if (p_shm == MAP_FAILED)
1443            {
1444                    log_error("mmap() error (%d)", errno);
1445                    close(fd);
1446                    return -2;
1447            }
1448    
1449            if (close(fd) < 0)
1450            {
1451                    log_error("close(fd) error (%d)", errno);
1452                    return -1;
1453            }
1454    
1455            p_menu_set->shm_size = size;
1456          p_menu_set->p_reserved = p_shm;          p_menu_set->p_reserved = p_shm;
1457    
1458          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
1459          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
1460          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
# Line 1429  int get_menu_shm_readonly(MENU_SET *p_me Line 1470  int get_menu_shm_readonly(MENU_SET *p_me
1470    
1471  int set_menu_shm_readonly(MENU_SET *p_menu_set)  int set_menu_shm_readonly(MENU_SET *p_menu_set)
1472  {  {
1473          void *p_shm;          if (p_menu_set == NULL)
   
         // Remap shared memory in read-only mode  
         p_shm = shmat(p_menu_set->shmid, p_menu_set->p_reserved, SHM_RDONLY | SHM_REMAP);  
         if (p_shm == (void *)-1)  
1474          {          {
1475                  log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);                  log_error("NULL pointer error");
1476                  return -1;                  return -1;
1477          }          }
1478    
1479          p_menu_set->p_reserved = p_shm;          if (p_menu_set->p_reserved != NULL && mprotect(p_menu_set->p_reserved, p_menu_set->shm_size, PROT_READ) < 0)
1480            {
1481                    log_error("mprotect() error (%d)", errno);
1482                    return -2;
1483            }
1484    
1485          return 0;          return 0;
1486  }  }
1487    
1488  int detach_menu_shm(MENU_SET *p_menu_set)  int detach_menu_shm(MENU_SET *p_menu_set)
1489  {  {
1490            if (p_menu_set == NULL)
1491            {
1492                    log_error("NULL pointer error");
1493                    return -1;
1494            }
1495    
1496          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
1497          p_menu_set->menu_item_count = 0;          p_menu_set->menu_item_count = 0;
1498          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
# Line 1460  int detach_menu_shm(MENU_SET *p_menu_set Line 1507  int detach_menu_shm(MENU_SET *p_menu_set
1507          p_menu_set->p_menu_name_dict = NULL;          p_menu_set->p_menu_name_dict = NULL;
1508          p_menu_set->p_menu_screen_dict = NULL;          p_menu_set->p_menu_screen_dict = NULL;
1509    
1510          if (p_menu_set->p_reserved != NULL && shmdt(p_menu_set->p_reserved) == -1)          if (p_menu_set->p_reserved != NULL && munmap(p_menu_set->p_reserved, p_menu_set->shm_size) < 0)
1511          {          {
1512                  log_error("shmdt() error (%d)\n", errno);                  log_error("munmap() error (%d)", errno);
1513                  return -1;                  return -2;
1514          }          }
1515    
1516          p_menu_set->p_reserved = NULL;          p_menu_set->p_reserved = NULL;


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

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