/[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.69 by sysadm, Wed Jun 25 01:50:14 2025 UTC Revision 1.95 by sysadm, Mon Jan 5 05:41:34 2026 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    menu.c  -  description  /*
3                                                           -------------------   * menu
4          Copyright            : (C) 2004-2025 by Leaflet   *   - configurable user interactive menu feature
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "bbs_cmd.h"  #include "bbs_cmd.h"
# 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>
31    
32  #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"  enum _menu_constant_t
33  #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"  {
34  #define MENU_CONF_DELIM_WITHOUT_SPACE "\r\n"          MENU_SET_RESERVED_LENGTH = sizeof(int16_t) * 4,
35    };
36    
37  #define MENU_SET_RESERVED_LENGTH (sizeof(int16_t) * 4)  static const char MENU_CONF_DELIM_WITH_SPACE[] = " ,\t\r\n";
38    static const char MENU_CONF_DELIM_WITHOUT_SPACE[] = "\r\n";
39    
40  MENU_SET bbs_menu;  MENU_SET bbs_menu;
41    MENU_SET top10_menu;
42    
43  int load_menu(MENU_SET *p_menu_set, const char *conf_file)  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
44  {  {
45            char filepath[FILE_PATH_LEN];
46            int fd;
47            size_t size;
48            void *p_shm;
49          FILE *fin;          FILE *fin;
50          int fin_line = 0;          int fin_line = 0;
51          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
# Line 56  int load_menu(MENU_SET *p_menu_set, cons Line 60  int load_menu(MENU_SET *p_menu_set, cons
60          MENU_ID menu_id;          MENU_ID menu_id;
61          MENU_ITEM_ID menu_item_id;          MENU_ITEM_ID menu_item_id;
62          MENU_SCREEN_ID screen_id;          MENU_SCREEN_ID screen_id;
63          int proj_id;  
64          key_t key;          if (p_menu_set == NULL || conf_file == NULL)
65          size_t size;          {
66                    log_error("NULL pointer error");
67                    return -1;
68            }
69    
70          // Initialize the data structure          // Initialize the data structure
71          memset(p_menu_set, 0, sizeof(*p_menu_set));          memset(p_menu_set, 0, sizeof(*p_menu_set));
# Line 67  int load_menu(MENU_SET *p_menu_set, cons Line 74  int load_menu(MENU_SET *p_menu_set, cons
74          p_menu_set->p_menu_name_dict = trie_dict_create();          p_menu_set->p_menu_name_dict = trie_dict_create();
75          if (p_menu_set->p_menu_name_dict == NULL)          if (p_menu_set->p_menu_name_dict == NULL)
76          {          {
77                  log_error("trie_dict_create() error\n");                  log_error("trie_dict_create() error");
78                  return -3;                  return -1;
79          }          }
80    
81          // Use trie_dict to search screen_id by menu screen name          // Use trie_dict to search screen_id by menu screen name
82          p_menu_set->p_menu_screen_dict = trie_dict_create();          p_menu_set->p_menu_screen_dict = trie_dict_create();
83          if (p_menu_set->p_menu_screen_dict == NULL)          if (p_menu_set->p_menu_screen_dict == NULL)
84          {          {
85                  log_error("trie_dict_create() error\n");                  log_error("trie_dict_create() error");
86                  return -3;                  return -1;
87          }          }
88    
89          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
90          {          {
91                  log_error("Open %s failed\n", conf_file);                  log_error("Open %s error: %d", conf_file, errno);
92                  return -2;                  return -2;
93          }          }
94    
95          // Allocate shared memory          // Allocate shared memory
         proj_id = (int)(time(NULL) % getpid());  
         key = ftok(conf_file, proj_id);  
         if (key == -1)  
         {  
                 log_error("ftok(%s %d) error (%d)\n", conf_file, proj_id, errno);  
                 return -2;  
         }  
   
96          size = MENU_SET_RESERVED_LENGTH +          size = MENU_SET_RESERVED_LENGTH +
97                     sizeof(MENU) * MAX_MENUS +                     sizeof(MENU) * MAX_MENUS +
98                     sizeof(MENU_ITEM) * MAX_MENUITEMS +                     sizeof(MENU_ITEM) * MAX_MENUITEMS +
99                     sizeof(MENU_SCREEN) * MAX_MENUS +                     sizeof(MENU_SCREEN) * MAX_MENUS +
100                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;                     MAX_MENU_SCR_BUF_LENGTH * MAX_MENUS;
101          p_menu_set->shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600);  
102          if (p_menu_set->shmid == -1)          strncpy(filepath, conf_file, sizeof(filepath) - 1);
103            filepath[sizeof(filepath) - 1] = '\0';
104            snprintf(p_menu_set->shm_name, sizeof(p_menu_set->shm_name), "/MENU_SHM_%s", basename(filepath));
105    
106            if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT)
107          {          {
108                  log_error("shmget(size = %d) error (%d)\n", size, errno);                  log_error("shm_unlink(%s) error (%d)", p_menu_set->shm_name, errno);
109                  return -3;                  return -2;
110            }
111    
112            if ((fd = shm_open(p_menu_set->shm_name, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1)
113            {
114                    log_error("shm_open(%s) error (%d)", p_menu_set->shm_name, errno);
115                    return -2;
116          }          }
117          p_menu_set->p_reserved = shmat(p_menu_set->shmid, NULL, 0);          if (ftruncate(fd, (off_t)size) == -1)
         if (p_menu_set->p_reserved == (void *)-1)  
118          {          {
119                  log_error("shmat() error (%d)\n", errno);                  log_error("ftruncate(size=%d) error (%d)", size, errno);
120                  return -3;                  close(fd);
121          }                  return -2;
122          p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH;          }
123          p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS;  
124          p_menu_set->p_menu_screen_pool = p_menu_set->p_menu_item_pool + sizeof(MENU_ITEM) * MAX_MENUITEMS;          p_shm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0L);
125          p_menu_set->p_menu_screen_buf = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS;          if (p_shm == MAP_FAILED)
126            {
127                    log_error("mmap() error (%d)", errno);
128                    close(fd);
129                    return -2;
130            }
131    
132            if (close(fd) < 0)
133            {
134                    log_error("close(fd) error (%d)", errno);
135                    return -1;
136            }
137    
138            p_menu_set->shm_size = size;
139            p_menu_set->p_reserved = p_shm;
140    
141            p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
142            p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
143            p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
144            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
145          p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;          p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;
146    
147          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
# Line 148  int load_menu(MENU_SET *p_menu_set, cons Line 175  int load_menu(MENU_SET *p_menu_set, cons
175                          {                          {
176                                  if (p_menu != NULL)                                  if (p_menu != NULL)
177                                  {                                  {
178                                          log_error("Incomplete menu definition in menu config line %d\n", fin_line);                                          log_error("Incomplete menu definition in menu config line %d", fin_line);
179                                          return -1;                                          return -1;
180                                  }                                  }
181    
182                                  if (p_menu_set->menu_count >= MAX_MENUS)                                  if (p_menu_set->menu_count >= MAX_MENUS)
183                                  {                                  {
184                                          log_error("Menu count (%d) exceed limit (%d)\n", p_menu_set->menu_count, MAX_MENUS);                                          log_error("Menu count (%d) exceed limit (%d)", p_menu_set->menu_count, MAX_MENUS);
185                                          return -3;                                          return -3;
186                                  }                                  }
187                                  menu_id = (MENU_ID)p_menu_set->menu_count;                                  menu_id = (MENU_ID)p_menu_set->menu_count;
# Line 172  int load_menu(MENU_SET *p_menu_set, cons Line 199  int load_menu(MENU_SET *p_menu_set, cons
199                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
200                                  if (q == NULL)                                  if (q == NULL)
201                                  {                                  {
202                                          log_error("Error menu name in menu config line %d\n", fin_line);                                          log_error("Error menu name in menu config line %d", fin_line);
203                                          return -1;                                          return -1;
204                                  }                                  }
205                                  p = q;                                  p = q;
206                                  while (isalnum(*q) || *q == '_' || *q == '-')                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
207                                  {                                  {
208                                          q++;                                          q++;
209                                  }                                  }
210                                  if (*q != '\0')                                  if (*q != '\0')
211                                  {                                  {
212                                          log_error("Error menu name in menu config line %d\n", fin_line);                                          log_error("Error menu name in menu config line %d", fin_line);
213                                          return -1;                                          return -1;
214                                  }                                  }
215    
216                                  if (q - p > sizeof(p_menu->name) - 1)                                  if (q - p > sizeof(p_menu->name) - 1)
217                                  {                                  {
218                                          log_error("Too longer menu name in menu config line %d\n", fin_line);                                          log_error("Too longer menu name in menu config line %d", fin_line);
219                                          return -1;                                          return -1;
220                                  }                                  }
221                                  strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);                                  strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);
# Line 196  int load_menu(MENU_SET *p_menu_set, cons Line 223  int load_menu(MENU_SET *p_menu_set, cons
223    
224                                  if (trie_dict_set(p_menu_set->p_menu_name_dict, p_menu->name, (int64_t)menu_id) != 1)                                  if (trie_dict_set(p_menu_set->p_menu_name_dict, p_menu->name, (int64_t)menu_id) != 1)
225                                  {                                  {
226                                          log_error("Error set menu dict [%s]\n", p_menu->name);                                          log_error("Error set menu dict [%s]", p_menu->name);
227                                  }                                  }
228    
229                                  // Check syntax                                  // Check syntax
230                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
231                                  if (q != NULL)                                  if (q != NULL)
232                                  {                                  {
233                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                          log_error("Unknown extra content in menu config line %d", fin_line);
234                                          return -1;                                          return -1;
235                                  }                                  }
236    
# Line 232  int load_menu(MENU_SET *p_menu_set, cons Line 259  int load_menu(MENU_SET *p_menu_set, cons
259                                                  // BEGIN of menu item                                                  // BEGIN of menu item
260                                                  if (p_menu->item_count >= MAX_ITEMS_PER_MENU)                                                  if (p_menu->item_count >= MAX_ITEMS_PER_MENU)
261                                                  {                                                  {
262                                                          log_error("Menuitem count per menu (%d) exceed limit (%d)\n", p_menu->item_count, MAX_ITEMS_PER_MENU);                                                          log_error("Menuitem count per menu (%d) exceed limit (%d)", p_menu->item_count, MAX_ITEMS_PER_MENU);
263                                                          return -1;                                                          return -1;
264                                                  }                                                  }
265                                                  if (p_menu_set->menu_item_count >= MAX_MENUITEMS)                                                  if (p_menu_set->menu_item_count >= MAX_MENUITEMS)
266                                                  {                                                  {
267                                                          log_error("Menuitem count (%d) exceed limit (%d)\n", p_menu_set->menu_item_count, MAX_MENUITEMS);                                                          log_error("Menuitem count (%d) exceed limit (%d)", p_menu_set->menu_item_count, MAX_MENUITEMS);
268                                                          return -3;                                                          return -3;
269                                                  }                                                  }
270                                                  menu_item_id = (MENU_ITEM_ID)p_menu_set->menu_item_count;                                                  menu_item_id = (MENU_ITEM_ID)p_menu_set->menu_item_count;
# Line 259  int load_menu(MENU_SET *p_menu_set, cons Line 286  int load_menu(MENU_SET *p_menu_set, cons
286                                                  else                                                  else
287                                                  {                                                  {
288                                                          q = p;                                                          q = p;
289                                                          while (isalnum(*q) || *q == '_' || *q == '-')                                                          while (isalnum((int)*q) || *q == '_' || *q == '-')
290                                                          {                                                          {
291                                                                  q++;                                                                  q++;
292                                                          }                                                          }
293                                                          if (*q != '\0')                                                          if (*q != '\0')
294                                                          {                                                          {
295                                                                  log_error("Error menu item action in menu config line %d\n", fin_line);                                                                  log_error("Error menu item action in menu config line %d", fin_line);
296                                                                  return -1;                                                                  return -1;
297                                                          }                                                          }
298                                                  }                                                  }
299    
300                                                  if (q - p > sizeof(p_menu_item->action) - 1)                                                  if (q - p > sizeof(p_menu_item->action) - 1)
301                                                  {                                                  {
302                                                          log_error("Too longer menu action in menu config line %d\n", fin_line);                                                          log_error("Too longer menu action in menu config line %d", fin_line);
303                                                          return -1;                                                          return -1;
304                                                  }                                                  }
305                                                  strncpy(p_menu_item->action, p, sizeof(p_menu_item->action) - 1);                                                  strncpy(p_menu_item->action, p, sizeof(p_menu_item->action) - 1);
# Line 282  int load_menu(MENU_SET *p_menu_set, cons Line 309  int load_menu(MENU_SET *p_menu_set, cons
309                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
310                                                  if (q == NULL)                                                  if (q == NULL)
311                                                  {                                                  {
312                                                          log_error("Error menu item row in menu config line %d\n", fin_line);                                                          log_error("Error menu item row in menu config line %d", fin_line);
313                                                          return -1;                                                          return -1;
314                                                  }                                                  }
315                                                  p = q;                                                  p = q;
316                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
317                                                  {                                                  {
318                                                          q++;                                                          q++;
319                                                  }                                                  }
320                                                  if (*q != '\0')                                                  if (*q != '\0')
321                                                  {                                                  {
322                                                          log_error("Error menu item row in menu config line %d\n", fin_line);                                                          log_error("Error menu item row in menu config line %d", fin_line);
323                                                          return -1;                                                          return -1;
324                                                  }                                                  }
325                                                  p_menu_item->row = (int16_t)atoi(p);                                                  p_menu_item->row = (int16_t)atoi(p);
# Line 301  int load_menu(MENU_SET *p_menu_set, cons Line 328  int load_menu(MENU_SET *p_menu_set, cons
328                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
329                                                  if (q == NULL)                                                  if (q == NULL)
330                                                  {                                                  {
331                                                          log_error("Error menu item col in menu config line %d\n", fin_line);                                                          log_error("Error menu item col in menu config line %d", fin_line);
332                                                          return -1;                                                          return -1;
333                                                  }                                                  }
334                                                  p = q;                                                  p = q;
335                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
336                                                  {                                                  {
337                                                          q++;                                                          q++;
338                                                  }                                                  }
339                                                  if (*q != '\0')                                                  if (*q != '\0')
340                                                  {                                                  {
341                                                          log_error("Error menu item col in menu config line %d\n", fin_line);                                                          log_error("Error menu item col in menu config line %d", fin_line);
342                                                          return -1;                                                          return -1;
343                                                  }                                                  }
344                                                  p_menu_item->col = (int16_t)atoi(p);                                                  p_menu_item->col = (int16_t)atoi(p);
# Line 320  int load_menu(MENU_SET *p_menu_set, cons Line 347  int load_menu(MENU_SET *p_menu_set, cons
347                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
348                                                  if (q == NULL)                                                  if (q == NULL)
349                                                  {                                                  {
350                                                          log_error("Error menu item priv in menu config line %d\n", fin_line);                                                          log_error("Error menu item priv in menu config line %d", fin_line);
351                                                          return -1;                                                          return -1;
352                                                  }                                                  }
353                                                  p = q;                                                  p = q;
354                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
355                                                  {                                                  {
356                                                          q++;                                                          q++;
357                                                  }                                                  }
358                                                  if (*q != '\0')                                                  if (*q != '\0')
359                                                  {                                                  {
360                                                          log_error("Error menu item priv in menu config line %d\n", fin_line);                                                          log_error("Error menu item priv in menu config line %d", fin_line);
361                                                          return -1;                                                          return -1;
362                                                  }                                                  }
363                                                  p_menu_item->priv = atoi(p);                                                  p_menu_item->priv = atoi(p);
# Line 339  int load_menu(MENU_SET *p_menu_set, cons Line 366  int load_menu(MENU_SET *p_menu_set, cons
366                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
367                                                  if (q == NULL)                                                  if (q == NULL)
368                                                  {                                                  {
369                                                          log_error("Error menu item level in menu config line %d\n", fin_line);                                                          log_error("Error menu item level in menu config line %d", fin_line);
370                                                          return -1;                                                          return -1;
371                                                  }                                                  }
372                                                  p = q;                                                  p = q;
373                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
374                                                  {                                                  {
375                                                          q++;                                                          q++;
376                                                  }                                                  }
377                                                  if (*q != '\0')                                                  if (*q != '\0')
378                                                  {                                                  {
379                                                          log_error("Error menu item level in menu config line %d\n", fin_line);                                                          log_error("Error menu item level in menu config line %d", fin_line);
380                                                          return -1;                                                          return -1;
381                                                  }                                                  }
382                                                  p_menu_item->level = atoi(p);                                                  p_menu_item->level = atoi(p);
# Line 358  int load_menu(MENU_SET *p_menu_set, cons Line 385  int load_menu(MENU_SET *p_menu_set, cons
385                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
386                                                  if (q == NULL || *q != '\"')                                                  if (q == NULL || *q != '\"')
387                                                  {                                                  {
388                                                          log_error("Error menu item name in menu config line %d\n", fin_line);                                                          log_error("Error menu item name in menu config line %d", fin_line);
389                                                          return -1;                                                          return -1;
390                                                  }                                                  }
391                                                  q++;                                                  q++;
# Line 378  int load_menu(MENU_SET *p_menu_set, cons Line 405  int load_menu(MENU_SET *p_menu_set, cons
405                                                  }                                                  }
406                                                  if (*q != '\"' || *(q + 1) != '\0')                                                  if (*q != '\"' || *(q + 1) != '\0')
407                                                  {                                                  {
408                                                          log_error("Error menu item name in menu config line %d\n", fin_line);                                                          log_error("Error menu item name in menu config line %d", fin_line);
409                                                          return -1;                                                          return -1;
410                                                  }                                                  }
411                                                  *q = '\0';                                                  *q = '\0';
412    
413                                                  if (q - p > sizeof(p_menu_item->name) - 1)                                                  if (q - p > sizeof(p_menu_item->name) - 1)
414                                                  {                                                  {
415                                                          log_error("Too longer menu name in menu config line %d\n", fin_line);                                                          log_error("Too longer menu name in menu config line %d", fin_line);
416                                                          return -1;                                                          return -1;
417                                                  }                                                  }
418                                                  strncpy(p_menu_item->name, p, sizeof(p_menu_item->name) - 1);                                                  strncpy(p_menu_item->name, p, sizeof(p_menu_item->name) - 1);
# Line 395  int load_menu(MENU_SET *p_menu_set, cons Line 422  int load_menu(MENU_SET *p_menu_set, cons
422                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
423                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)
424                                                  {                                                  {
425                                                          log_error("Error menu item text in menu config line %d\n", fin_line);                                                          log_error("Error menu item text in menu config line %d", fin_line);
426                                                          return -1;                                                          return -1;
427                                                  }                                                  }
428                                                  q++;                                                  q++;
# Line 415  int load_menu(MENU_SET *p_menu_set, cons Line 442  int load_menu(MENU_SET *p_menu_set, cons
442                                                  }                                                  }
443                                                  if (*q != '\"')                                                  if (*q != '\"')
444                                                  {                                                  {
445                                                          log_error("Error menu item text in menu config line %d\n", fin_line);                                                          log_error("Error menu item text in menu config line %d", fin_line);
446                                                          return -1;                                                          return -1;
447                                                  }                                                  }
448                                                  *q = '\0';                                                  *q = '\0';
449    
450                                                  if (q - p > sizeof(p_menu_item->text) - 1)                                                  if (q - p > sizeof(p_menu_item->text) - 1)
451                                                  {                                                  {
452                                                          log_error("Too longer menu item text in menu config line %d\n", fin_line);                                                          log_error("Too longer menu item text in menu config line %d", fin_line);
453                                                          return -1;                                                          return -1;
454                                                  }                                                  }
455                                                  strncpy(p_menu_item->text, p, sizeof(p_menu_item->text) - 1);                                                  strncpy(p_menu_item->text, p, sizeof(p_menu_item->text) - 1);
# Line 432  int load_menu(MENU_SET *p_menu_set, cons Line 459  int load_menu(MENU_SET *p_menu_set, cons
459                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
460                                                  if (q != NULL)                                                  if (q != NULL)
461                                                  {                                                  {
462                                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                                          log_error("Unknown extra content in menu config line %d", fin_line);
463                                                          return -1;                                                          return -1;
464                                                  }                                                  }
465                                          }                                          }
# Line 444  int load_menu(MENU_SET *p_menu_set, cons Line 471  int load_menu(MENU_SET *p_menu_set, cons
471                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
472                                                  if (q == NULL)                                                  if (q == NULL)
473                                                  {                                                  {
474                                                          log_error("Error menu title row in menu config line %d\n", fin_line);                                                          log_error("Error menu title row in menu config line %d", fin_line);
475                                                          return -1;                                                          return -1;
476                                                  }                                                  }
477                                                  p = q;                                                  p = q;
478                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
479                                                  {                                                  {
480                                                          q++;                                                          q++;
481                                                  }                                                  }
482                                                  if (*q != '\0')                                                  if (*q != '\0')
483                                                  {                                                  {
484                                                          log_error("Error menu title row in menu config line %d\n", fin_line);                                                          log_error("Error menu title row in menu config line %d", fin_line);
485                                                          return -1;                                                          return -1;
486                                                  }                                                  }
487                                                  p_menu->title.row = (int16_t)atoi(p);                                                  p_menu->title.row = (int16_t)atoi(p);
# Line 463  int load_menu(MENU_SET *p_menu_set, cons Line 490  int load_menu(MENU_SET *p_menu_set, cons
490                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
491                                                  if (q == NULL)                                                  if (q == NULL)
492                                                  {                                                  {
493                                                          log_error("Error menu title col in menu config line %d\n", fin_line);                                                          log_error("Error menu title col in menu config line %d", fin_line);
494                                                          return -1;                                                          return -1;
495                                                  }                                                  }
496                                                  p = q;                                                  p = q;
497                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
498                                                  {                                                  {
499                                                          q++;                                                          q++;
500                                                  }                                                  }
501                                                  if (*q != '\0')                                                  if (*q != '\0')
502                                                  {                                                  {
503                                                          log_error("Error menu title col in menu config line %d\n", fin_line);                                                          log_error("Error menu title col in menu config line %d", fin_line);
504                                                          return -1;                                                          return -1;
505                                                  }                                                  }
506                                                  p_menu->title.col = (int16_t)atoi(p);                                                  p_menu->title.col = (int16_t)atoi(p);
# Line 482  int load_menu(MENU_SET *p_menu_set, cons Line 509  int load_menu(MENU_SET *p_menu_set, cons
509                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
510                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)                                                  if (q == NULL || (q = strchr(q, '\"')) == NULL)
511                                                  {                                                  {
512                                                          log_error("Error menu title text in menu config line %d\n", fin_line);                                                          log_error("Error menu title text in menu config line %d", fin_line);
513                                                          return -1;                                                          return -1;
514                                                  }                                                  }
515                                                  q++;                                                  q++;
# Line 502  int load_menu(MENU_SET *p_menu_set, cons Line 529  int load_menu(MENU_SET *p_menu_set, cons
529                                                  }                                                  }
530                                                  if (*q != '\"')                                                  if (*q != '\"')
531                                                  {                                                  {
532                                                          log_error("Error menu title text in menu config line %d\n", fin_line);                                                          log_error("Error menu title text in menu config line %d", fin_line);
533                                                          return -1;                                                          return -1;
534                                                  }                                                  }
535                                                  *q = '\0';                                                  *q = '\0';
536    
537                                                  if (q - p > sizeof(p_menu->title.text) - 1)                                                  if (q - p > sizeof(p_menu->title.text) - 1)
538                                                  {                                                  {
539                                                          log_error("Too longer menu title text in menu config line %d\n", fin_line);                                                          log_error("Too longer menu title text in menu config line %d", fin_line);
540                                                          return -1;                                                          return -1;
541                                                  }                                                  }
542                                                  strncpy(p_menu->title.text, p, sizeof(p_menu->title.text) - 1);                                                  strncpy(p_menu->title.text, p, sizeof(p_menu->title.text) - 1);
# Line 519  int load_menu(MENU_SET *p_menu_set, cons Line 546  int load_menu(MENU_SET *p_menu_set, cons
546                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
547                                                  if (q != NULL)                                                  if (q != NULL)
548                                                  {                                                  {
549                                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                                          log_error("Unknown extra content in menu config line %d", fin_line);
550                                                          return -1;                                                          return -1;
551                                                  }                                                  }
552                                          }                                          }
# Line 531  int load_menu(MENU_SET *p_menu_set, cons Line 558  int load_menu(MENU_SET *p_menu_set, cons
558                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
559                                                  if (q == NULL)                                                  if (q == NULL)
560                                                  {                                                  {
561                                                          log_error("Error menu screen row in menu config line %d\n", fin_line);                                                          log_error("Error menu screen row in menu config line %d", fin_line);
562                                                          return -1;                                                          return -1;
563                                                  }                                                  }
564                                                  p = q;                                                  p = q;
565                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
566                                                  {                                                  {
567                                                          q++;                                                          q++;
568                                                  }                                                  }
569                                                  if (*q != '\0')                                                  if (*q != '\0')
570                                                  {                                                  {
571                                                          log_error("Error menu screen row in menu config line %d\n", fin_line);                                                          log_error("Error menu screen row in menu config line %d", fin_line);
572                                                          return -1;                                                          return -1;
573                                                  }                                                  }
574                                                  p_menu->screen_row = (int16_t)atoi(p);                                                  p_menu->screen_row = (int16_t)atoi(p);
# Line 550  int load_menu(MENU_SET *p_menu_set, cons Line 577  int load_menu(MENU_SET *p_menu_set, cons
577                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
578                                                  if (q == NULL)                                                  if (q == NULL)
579                                                  {                                                  {
580                                                          log_error("Error menu screen col in menu config line %d\n", fin_line);                                                          log_error("Error menu screen col in menu config line %d", fin_line);
581                                                          return -1;                                                          return -1;
582                                                  }                                                  }
583                                                  p = q;                                                  p = q;
584                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
585                                                  {                                                  {
586                                                          q++;                                                          q++;
587                                                  }                                                  }
588                                                  if (*q != '\0')                                                  if (*q != '\0')
589                                                  {                                                  {
590                                                          log_error("Error menu screen col in menu config line %d\n", fin_line);                                                          log_error("Error menu screen col in menu config line %d", fin_line);
591                                                          return -1;                                                          return -1;
592                                                  }                                                  }
593                                                  p_menu->screen_col = (int16_t)atoi(p);                                                  p_menu->screen_col = (int16_t)atoi(p);
# Line 569  int load_menu(MENU_SET *p_menu_set, cons Line 596  int load_menu(MENU_SET *p_menu_set, cons
596                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
597                                                  if (q == NULL)                                                  if (q == NULL)
598                                                  {                                                  {
599                                                          log_error("Error menu screen name in menu config line %d\n", fin_line);                                                          log_error("Error menu screen name in menu config line %d", fin_line);
600                                                          return -1;                                                          return -1;
601                                                  }                                                  }
602                                                  p = q;                                                  p = q;
603                                                  while (isalnum(*q) || *q == '_' || *q == '-')                                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
604                                                  {                                                  {
605                                                          q++;                                                          q++;
606                                                  }                                                  }
607                                                  if (*q != '\0')                                                  if (*q != '\0')
608                                                  {                                                  {
609                                                          log_error("Error menu screen name in menu config line %d\n", fin_line);                                                          log_error("Error menu screen name in menu config line %d", fin_line);
610                                                          return -1;                                                          return -1;
611                                                  }                                                  }
612                                                  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 589  int load_menu(MENU_SET *p_menu_set, cons Line 616  int load_menu(MENU_SET *p_menu_set, cons
616                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
617                                                  if (q != NULL)                                                  if (q != NULL)
618                                                  {                                                  {
619                                                          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);
620                                                          return -1;                                                          return -1;
621                                                  }                                                  }
622                                          }                                          }
# Line 599  int load_menu(MENU_SET *p_menu_set, cons Line 626  int load_menu(MENU_SET *p_menu_set, cons
626                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
627                                                  if (q == NULL)                                                  if (q == NULL)
628                                                  {                                                  {
629                                                          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);
630                                                          return -1;                                                          return -1;
631                                                  }                                                  }
632                                                  p = q;                                                  p = q;
633                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
634                                                  {                                                  {
635                                                          q++;                                                          q++;
636                                                  }                                                  }
637                                                  if (*q != '\0')                                                  if (*q != '\0')
638                                                  {                                                  {
639                                                          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);
640                                                          return -1;                                                          return -1;
641                                                  }                                                  }
642                                                  p_menu->page_row = (int16_t)atoi(p);                                                  p_menu->page_row = (int16_t)atoi(p);
# Line 618  int load_menu(MENU_SET *p_menu_set, cons Line 645  int load_menu(MENU_SET *p_menu_set, cons
645                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
646                                                  if (q == NULL)                                                  if (q == NULL)
647                                                  {                                                  {
648                                                          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);
649                                                          return -1;                                                          return -1;
650                                                  }                                                  }
651                                                  p = q;                                                  p = q;
652                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
653                                                  {                                                  {
654                                                          q++;                                                          q++;
655                                                  }                                                  }
656                                                  if (*q != '\0')                                                  if (*q != '\0')
657                                                  {                                                  {
658                                                          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);
659                                                          return -1;                                                          return -1;
660                                                  }                                                  }
661                                                  p_menu->page_col = (int16_t)atoi(p);                                                  p_menu->page_col = (int16_t)atoi(p);
# Line 637  int load_menu(MENU_SET *p_menu_set, cons Line 664  int load_menu(MENU_SET *p_menu_set, cons
664                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
665                                                  if (q == NULL)                                                  if (q == NULL)
666                                                  {                                                  {
667                                                          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);
668                                                          return -1;                                                          return -1;
669                                                  }                                                  }
670                                                  p = q;                                                  p = q;
671                                                  while (isdigit(*q))                                                  while (isdigit((int)*q))
672                                                  {                                                  {
673                                                          q++;                                                          q++;
674                                                  }                                                  }
675                                                  if (*q != '\0')                                                  if (*q != '\0')
676                                                  {                                                  {
677                                                          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);
678                                                          return -1;                                                          return -1;
679                                                  }                                                  }
680                                                  p_menu->page_item_limit = (int16_t)atoi(p);                                                  p_menu->page_item_limit = (int16_t)atoi(p);
# Line 656  int load_menu(MENU_SET *p_menu_set, cons Line 683  int load_menu(MENU_SET *p_menu_set, cons
683                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
684                                                  if (q != NULL)                                                  if (q != NULL)
685                                                  {                                                  {
686                                                          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);
687                                                          return -1;                                                          return -1;
688                                                  }                                                  }
689                                          }                                          }
# Line 668  int load_menu(MENU_SET *p_menu_set, cons Line 695  int load_menu(MENU_SET *p_menu_set, cons
695                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
696                                                  if (q != NULL)                                                  if (q != NULL)
697                                                  {                                                  {
698                                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                                          log_error("Unknown extra content in menu config line %d", fin_line);
699                                                          return -1;                                                          return -1;
700                                                  }                                                  }
701                                          }                                          }
# Line 678  int load_menu(MENU_SET *p_menu_set, cons Line 705  int load_menu(MENU_SET *p_menu_set, cons
705                          {                          {
706                                  if (p_menu_set->menu_item_count >= MAX_MENUS)                                  if (p_menu_set->menu_item_count >= MAX_MENUS)
707                                  {                                  {
708                                          log_error("Menu screen count (%d) exceed limit (%d)\n", p_menu_set->menu_screen_count, MAX_MENUS);                                          log_error("Menu screen count (%d) exceed limit (%d)", p_menu_set->menu_screen_count, MAX_MENUS);
709                                          return -3;                                          return -3;
710                                  }                                  }
711                                  screen_id = (MENU_SCREEN_ID)p_menu_set->menu_screen_count;                                  screen_id = (MENU_SCREEN_ID)p_menu_set->menu_screen_count;
# Line 687  int load_menu(MENU_SET *p_menu_set, cons Line 714  int load_menu(MENU_SET *p_menu_set, cons
714                                  p_screen = get_menu_screen_by_id(p_menu_set, screen_id);                                  p_screen = get_menu_screen_by_id(p_menu_set, screen_id);
715    
716                                  q = p;                                  q = p;
717                                  while (isalnum(*q) || *q == '_' || *q == '-')                                  while (isalnum((int)*q) || *q == '_' || *q == '-')
718                                  {                                  {
719                                          q++;                                          q++;
720                                  }                                  }
721                                  if (*q != '\0')                                  if (*q != '\0')
722                                  {                                  {
723                                          log_error("Error menu screen name in menu config line %d\n", fin_line);                                          log_error("Error menu screen name in menu config line %d", fin_line);
724                                          return -1;                                          return -1;
725                                  }                                  }
726                                  strncpy(p_screen->name, p, sizeof(p_screen->name) - 1);                                  strncpy(p_screen->name, p, sizeof(p_screen->name) - 1);
# Line 701  int load_menu(MENU_SET *p_menu_set, cons Line 728  int load_menu(MENU_SET *p_menu_set, cons
728    
729                                  if (trie_dict_set(p_menu_set->p_menu_screen_dict, p_screen->name, (int64_t)screen_id) != 1)                                  if (trie_dict_set(p_menu_set->p_menu_screen_dict, p_screen->name, (int64_t)screen_id) != 1)
730                                  {                                  {
731                                          log_error("Error set menu screen dict [%s]\n", p_screen->name);                                          log_error("Error set menu screen dict [%s]", p_screen->name);
732                                  }                                  }
733    
734                                  // Check syntax                                  // Check syntax
735                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
736                                  if (q != NULL)                                  if (q != NULL)
737                                  {                                  {
738                                          log_error("Unknown extra content in menu config line %d\n", fin_line);                                          log_error("Unknown extra content in menu config line %d", fin_line);
739                                          return -1;                                          return -1;
740                                  }                                  }
741    
# Line 729  int load_menu(MENU_SET *p_menu_set, cons Line 756  int load_menu(MENU_SET *p_menu_set, cons
756                                          {                                          {
757                                                  if (p_menu_set->p_menu_screen_buf_free + 1 > q)                                                  if (p_menu_set->p_menu_screen_buf_free + 1 > q)
758                                                  {                                                  {
759                                                          log_error("Menu screen buffer depleted (%p + 1 > %p)\n", p_menu_set->p_menu_screen_buf_free, q);                                                          log_error("Menu screen buffer depleted (%p + 1 > %p)", p_menu_set->p_menu_screen_buf_free, q);
760                                                          return -3;                                                          return -3;
761                                                  }                                                  }
762    
# Line 742  int load_menu(MENU_SET *p_menu_set, cons Line 769  int load_menu(MENU_SET *p_menu_set, cons
769                                          // Clear line                                          // Clear line
770                                          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)
771                                          {                                          {
772                                                  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));
773                                                  return -3;                                                  return -3;
774                                          }                                          }
775                                          p_menu_set->p_menu_screen_buf_free = stpcpy(p_menu_set->p_menu_screen_buf_free, CTRL_SEQ_CLR_LINE);                                          p_menu_set->p_menu_screen_buf_free = stpcpy(p_menu_set->p_menu_screen_buf_free, CTRL_SEQ_CLR_LINE);
# Line 752  int load_menu(MENU_SET *p_menu_set, cons Line 779  int load_menu(MENU_SET *p_menu_set, cons
779                                          {                                          {
780                                                  if (p_menu_set->p_menu_screen_buf_free + 2 > q)                                                  if (p_menu_set->p_menu_screen_buf_free + 2 > q)
781                                                  {                                                  {
782                                                          log_error("Menu screen buffer depleted (%p + 2 > %p)\n", p_menu_set->p_menu_screen_buf_free, q);                                                          log_error("Menu screen buffer depleted (%p + 2 > %p)", p_menu_set->p_menu_screen_buf_free, q);
783                                                          return -3;                                                          return -3;
784                                                  }                                                  }
785    
# Line 770  int load_menu(MENU_SET *p_menu_set, cons Line 797  int load_menu(MENU_SET *p_menu_set, cons
797    
798                                  if (p_screen->buf_length == -1)                                  if (p_screen->buf_length == -1)
799                                  {                                  {
800                                          log_error("End of menu screen [%s] not found\n", p_screen->name);                                          log_error("End of menu screen [%s] not found", p_screen->name);
801                                  }                                  }
802                          }                          }
803                  }                  }
804                  else // Invalid prefix                  else // Invalid prefix
805                  {                  {
806                          log_error("Error in menu config line %d\n", fin_line);                          log_error("Error in menu config line %d", fin_line);
807                          return -1;                          return -1;
808                  }                  }
809          }          }
# Line 788  int load_menu(MENU_SET *p_menu_set, cons Line 815  int load_menu(MENU_SET *p_menu_set, cons
815    
816                  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)
817                  {                  {
818                          log_error("Undefined menu screen [%s]\n", p);                          log_error("Undefined menu screen [%s]", p);
819                          return -1;                          return -1;
820                  }                  }
821    
# Line 797  int load_menu(MENU_SET *p_menu_set, cons Line 824  int load_menu(MENU_SET *p_menu_set, cons
824                  {                  {
825                          if ((p_menu->filter_handler = get_cmd_handler(p_menu->name)) == NULL)                          if ((p_menu->filter_handler = get_cmd_handler(p_menu->name)) == NULL)
826                          {                          {
827                                  log_error("Undefined menu filter handler [%s]\n", p_menu->name);                                  log_error("Undefined menu filter handler [%s]", p_menu->name);
828                                  return -1;                                  return -1;
829                          }                          }
830                  }                  }
# Line 812  int load_menu(MENU_SET *p_menu_set, cons Line 839  int load_menu(MENU_SET *p_menu_set, cons
839                  {                  {
840                          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)
841                          {                          {
842                                  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);
843                                  return -1;                                  return -1;
844                          }                          }
845                  }                  }
# Line 821  int load_menu(MENU_SET *p_menu_set, cons Line 848  int load_menu(MENU_SET *p_menu_set, cons
848                  {                  {
849                          if (trie_dict_get(p_menu_set->p_menu_name_dict, p_menu_item->action, (int64_t *)&menu_id) != 1)                          if (trie_dict_get(p_menu_set->p_menu_name_dict, p_menu_item->action, (int64_t *)&menu_id) != 1)
850                          {                          {
851                                  log_error("Undefined sub menu id [%s]\n", p_menu_item->action);                                  log_error("Undefined sub menu id [%s]", p_menu_item->action);
852                                  return -1;                                  return -1;
853                          }                          }
854                          p_menu_item->action_menu_id = menu_id;                          p_menu_item->action_menu_id = menu_id;
# Line 848  int display_menu_cursor(MENU_SET *p_menu Line 875  int display_menu_cursor(MENU_SET *p_menu
875          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
876          if (p_menu == NULL)          if (p_menu == NULL)
877          {          {
878                  log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id);                  log_error("get_menu_by_id(%d) return NULL pointer", menu_id);
879                  return -1;                  return -1;
880          }          }
881    
# Line 857  int display_menu_cursor(MENU_SET *p_menu Line 884  int display_menu_cursor(MENU_SET *p_menu
884          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
885          if (p_menu_item == NULL)          if (p_menu_item == NULL)
886          {          {
887                  log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                  log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
888                  return -1;                  return -1;
889          }          }
890    
# Line 883  static int display_menu_current_page(MEN Line 910  static int display_menu_current_page(MEN
910          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
911          if (p_menu == NULL)          if (p_menu == NULL)
912          {          {
913                  log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id);                  log_error("get_menu_by_id(%d) return NULL pointer", menu_id);
914                  return -1;                  return -1;
915          }          }
916    
# Line 907  static int display_menu_current_page(MEN Line 934  static int display_menu_current_page(MEN
934                  p_menu_screen = get_menu_screen_by_id(p_menu_set, p_menu->screen_id);                  p_menu_screen = get_menu_screen_by_id(p_menu_set, p_menu->screen_id);
935                  if (p_menu_screen == NULL)                  if (p_menu_screen == NULL)
936                  {                  {
937                          log_error("get_menu_screen_by_id(%d) return NULL pointer\n", p_menu->screen_id);                          log_error("get_menu_screen_by_id(%d) return NULL pointer", p_menu->screen_id);
938                          return -1;                          return -1;
939                  }                  }
940    
# Line 979  int display_menu(MENU_SET *p_menu_set) Line 1006  int display_menu(MENU_SET *p_menu_set)
1006          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
1007          if (p_menu == NULL)          if (p_menu == NULL)
1008          {          {
1009                  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);
1010                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1011                  {                  {
1012                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 988  int display_menu(MENU_SET *p_menu_set) Line 1015  int display_menu(MENU_SET *p_menu_set)
1015                  return EXITMENU;                  return EXITMENU;
1016          }          }
1017    
1018            if (p_menu->item_count <= 0) // empty menu
1019            {
1020                    moveto(p_menu->screen_row, p_menu->screen_col);
1021                    clrtoeol();
1022                    prints("没有å¯é€‰é¡¹");
1023                    press_any_key();
1024                    return -1;
1025            }
1026    
1027          menu_item_pos = p_menu_set->menu_item_pos[p_menu_set->choose_step];          menu_item_pos = p_menu_set->menu_item_pos[p_menu_set->choose_step];
1028          menu_item_id = p_menu->items[menu_item_pos];          menu_item_id = p_menu->items[menu_item_pos];
1029          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);
1030          if (p_menu_item == NULL)          if (p_menu_item == NULL)
1031          {          {
1032                  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);
1033                  return EXITMENU;                  return EXITMENU;
1034          }          }
1035    
# Line 1059  int display_menu(MENU_SET *p_menu_set) Line 1095  int display_menu(MENU_SET *p_menu_set)
1095          {          {
1096                  moveto(p_menu->screen_row, p_menu->screen_col);                  moveto(p_menu->screen_row, p_menu->screen_col);
1097                  clrtoeol();                  clrtoeol();
1098                  prints("ûÓпÉÑ¡Ïî");                  prints("没有å¯é€‰é¡¹");
1099                  press_any_key();                  press_any_key();
1100                  return -1;                  return -1;
1101          }          }
# Line 1086  int menu_control(MENU_SET *p_menu_set, i Line 1122  int menu_control(MENU_SET *p_menu_set, i
1122    
1123          if (p_menu_set->menu_count == 0)          if (p_menu_set->menu_count == 0)
1124          {          {
1125                  log_error("Empty menu set\n");                  log_error("Empty menu set");
1126                  return EXITBBS;                  return EXITBBS;
1127          }          }
1128    
# Line 1094  int menu_control(MENU_SET *p_menu_set, i Line 1130  int menu_control(MENU_SET *p_menu_set, i
1130          p_menu = get_menu_by_id(p_menu_set, menu_id);          p_menu = get_menu_by_id(p_menu_set, menu_id);
1131          if (p_menu == NULL)          if (p_menu == NULL)
1132          {          {
1133                  log_error("get_menu_by_id(%d) return NULL pointer\n", menu_id);                  log_error("get_menu_by_id(%d) return NULL pointer", menu_id);
1134                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1135                  {                  {
1136                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 1105  int menu_control(MENU_SET *p_menu_set, i Line 1141  int menu_control(MENU_SET *p_menu_set, i
1141    
1142          if (p_menu->item_count == 0)          if (p_menu->item_count == 0)
1143          {          {
1144  #ifdef _DEBUG                  log_debug("Empty menu (%s)", p_menu->name);
                 log_error("Empty menu (%s)\n", p_menu->name);  
 #endif  
1145                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1146                  {                  {
1147                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
# Line 1123  int menu_control(MENU_SET *p_menu_set, i Line 1157  int menu_control(MENU_SET *p_menu_set, i
1157          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1158          if (p_menu_item == NULL)          if (p_menu_item == NULL)
1159          {          {
1160                  log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                  log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
1161                  p_menu_set->menu_item_pos[p_menu_set->choose_step] = 0;                  p_menu_set->menu_item_pos[p_menu_set->choose_step] = 0;
1162                  return REDRAW;                  return REDRAW;
1163          }          }
# Line 1131  int menu_control(MENU_SET *p_menu_set, i Line 1165  int menu_control(MENU_SET *p_menu_set, i
1165          switch (key)          switch (key)
1166          {          {
1167          case CR:          case CR:
                 igetch_reset();  
1168          case KEY_RIGHT:          case KEY_RIGHT:
1169                  if (p_menu_item->submenu)                  if (p_menu_item->submenu)
1170                  {                  {
# Line 1158  int menu_control(MENU_SET *p_menu_set, i Line 1191  int menu_control(MENU_SET *p_menu_set, i
1191                  if (p_menu_set->choose_step > 0)                  if (p_menu_set->choose_step > 0)
1192                  {                  {
1193                          p_menu_set->choose_step--;                          p_menu_set->choose_step--;
1194                          if (p_menu_set->choose_step == 0)                          return REDRAW;
                         {  
                                 return REDRAW;  
                         }  
                         if (display_menu(p_menu_set) != 0)  
                         {  
                                 return menu_control(p_menu_set, KEY_LEFT);  
                         }  
1195                  }                  }
1196                  else                  else
1197                  {                  {
# Line 1182  int menu_control(MENU_SET *p_menu_set, i Line 1208  int menu_control(MENU_SET *p_menu_set, i
1208                                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1209                                  if (p_menu_item == NULL)                                  if (p_menu_item == NULL)
1210                                  {                                  {
1211                                          log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                                          log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
1212                                          return -1;                                          return -1;
1213                                  }                                  }
1214    
# Line 1215  int menu_control(MENU_SET *p_menu_set, i Line 1241  int menu_control(MENU_SET *p_menu_set, i
1241                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1242                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1243                          {                          {
1244                                  log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                                  log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
1245                                  return -1;                                  return -1;
1246                          }                          }
1247                          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 1246  int menu_control(MENU_SET *p_menu_set, i Line 1272  int menu_control(MENU_SET *p_menu_set, i
1272                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1273                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1274                          {                          {
1275                                  log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                                  log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
1276                                  return -1;                                  return -1;
1277                          }                          }
1278                          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 1270  int menu_control(MENU_SET *p_menu_set, i Line 1296  int menu_control(MENU_SET *p_menu_set, i
1296                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1297                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1298                          {                          {
1299                                  log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                                  log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
1300                                  return -1;                                  return -1;
1301                          }                          }
1302    
# Line 1297  int menu_control(MENU_SET *p_menu_set, i Line 1323  int menu_control(MENU_SET *p_menu_set, i
1323                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                          p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1324                          if (p_menu_item == NULL)                          if (p_menu_item == NULL)
1325                          {                          {
1326                                  log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                                  log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
1327                                  return -1;                                  return -1;
1328                          }                          }
1329    
# Line 1324  int menu_control(MENU_SET *p_menu_set, i Line 1350  int menu_control(MENU_SET *p_menu_set, i
1350                                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);                                  p_menu_item = get_menu_item_by_id(p_menu_set, menu_item_id);
1351                                  if (p_menu_item == NULL)                                  if (p_menu_item == NULL)
1352                                  {                                  {
1353                                          log_error("get_menu_item_by_id(%d) return NULL pointer\n", menu_item_id);                                          log_error("get_menu_item_by_id(%d) return NULL pointer", menu_item_id);
1354                                          return -1;                                          return -1;
1355                                  }                                  }
1356    
# Line 1349  int menu_control(MENU_SET *p_menu_set, i Line 1375  int menu_control(MENU_SET *p_menu_set, i
1375    
1376  int unload_menu(MENU_SET *p_menu_set)  int unload_menu(MENU_SET *p_menu_set)
1377  {  {
         int shmid;  
   
1378          if (p_menu_set == NULL)          if (p_menu_set == NULL)
1379          {          {
1380                    log_error("NULL pointer error");
1381                  return -1;                  return -1;
1382          }          }
1383    
# Line 1368  int unload_menu(MENU_SET *p_menu_set) Line 1393  int unload_menu(MENU_SET *p_menu_set)
1393                  p_menu_set->p_menu_screen_dict = NULL;                  p_menu_set->p_menu_screen_dict = NULL;
1394          }          }
1395    
         shmid = p_menu_set->shmid;  
   
1396          detach_menu_shm(p_menu_set);          detach_menu_shm(p_menu_set);
1397    
1398          if (shmctl(shmid, IPC_RMID, NULL) == -1)          if (shm_unlink(p_menu_set->shm_name) == -1 && errno != ENOENT)
1399          {          {
1400                  log_error("shmctl(shmid=%d, IPC_RMID) error (%d)\n", shmid, errno);                  log_error("shm_unlink(%s) error (%d)", p_menu_set->shm_name, errno);
1401                  return -1;                  return -2;
1402          }          }
1403    
1404          return 0;          return 0;
# Line 1383  int unload_menu(MENU_SET *p_menu_set) Line 1406  int unload_menu(MENU_SET *p_menu_set)
1406    
1407  int get_menu_shm_readonly(MENU_SET *p_menu_set)  int get_menu_shm_readonly(MENU_SET *p_menu_set)
1408  {  {
1409            int fd;
1410          void *p_shm;          void *p_shm;
1411            struct stat sb;
1412            size_t size;
1413    
1414            if (p_menu_set == NULL)
1415            {
1416                    log_error("NULL pointer error");
1417                    return -1;
1418            }
1419    
1420            if ((fd = shm_open(p_menu_set->shm_name, O_RDONLY, 0600)) == -1)
1421            {
1422                    log_error("shm_open(%s) error (%d)", p_menu_set->shm_name, errno);
1423                    return -2;
1424            }
1425    
1426            if (fstat(fd, &sb) < 0)
1427            {
1428                    log_error("fstat(fd) error (%d)", errno);
1429                    close(fd);
1430                    return -2;
1431            }
1432    
1433            size = (size_t)sb.st_size;
1434    
1435          p_shm = shmat(p_menu_set->shmid, NULL, SHM_RDONLY);          p_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0L);
1436          if (p_shm == (void *)-1)          if (p_shm == MAP_FAILED)
1437          {          {
1438                  log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);                  log_error("mmap() error (%d)", errno);
1439                    close(fd);
1440                    return -2;
1441            }
1442    
1443            if (close(fd) < 0)
1444            {
1445                    log_error("close(fd) error (%d)", errno);
1446                  return -1;                  return -1;
1447          }          }
1448    
1449            p_menu_set->shm_size = size;
1450          p_menu_set->p_reserved = p_shm;          p_menu_set->p_reserved = p_shm;
1451          p_menu_set->p_menu_pool = p_menu_set->p_reserved + MENU_SET_RESERVED_LENGTH;  
1452          p_menu_set->p_menu_item_pool = p_menu_set->p_menu_pool + sizeof(MENU) * MAX_MENUS;          p_menu_set->p_menu_pool = (char *)(p_menu_set->p_reserved) + MENU_SET_RESERVED_LENGTH;
1453          p_menu_set->p_menu_screen_pool = p_menu_set->p_menu_item_pool + sizeof(MENU_ITEM) * MAX_MENUITEMS;          p_menu_set->p_menu_item_pool = (char *)(p_menu_set->p_menu_pool) + sizeof(MENU) * MAX_MENUS;
1454          p_menu_set->p_menu_screen_buf = p_menu_set->p_menu_screen_pool + sizeof(MENU_SCREEN) * MAX_MENUS;          p_menu_set->p_menu_screen_pool = (char *)(p_menu_set->p_menu_item_pool) + sizeof(MENU_ITEM) * MAX_MENUITEMS;
1455            p_menu_set->p_menu_screen_buf = (char *)(p_menu_set->p_menu_screen_pool) + sizeof(MENU_SCREEN) * MAX_MENUS;
1456          p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;          p_menu_set->p_menu_screen_buf_free = p_menu_set->p_menu_screen_buf;
1457    
1458          p_menu_set->choose_step = 0;          p_menu_set->choose_step = 0;
# Line 1408  int get_menu_shm_readonly(MENU_SET *p_me Line 1464  int get_menu_shm_readonly(MENU_SET *p_me
1464    
1465  int set_menu_shm_readonly(MENU_SET *p_menu_set)  int set_menu_shm_readonly(MENU_SET *p_menu_set)
1466  {  {
1467          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)  
1468          {          {
1469                  log_error("shmat(menu_shm shmid = %d) error (%d)\n", p_menu_set->shmid, errno);                  log_error("NULL pointer error");
1470                  return -1;                  return -1;
1471          }          }
1472    
1473          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)
1474            {
1475                    log_error("mprotect() error (%d)", errno);
1476                    return -2;
1477            }
1478    
1479          return 0;          return 0;
1480  }  }
1481    
1482  int detach_menu_shm(MENU_SET *p_menu_set)  int detach_menu_shm(MENU_SET *p_menu_set)
1483  {  {
1484            if (p_menu_set == NULL)
1485            {
1486                    log_error("NULL pointer error");
1487                    return -1;
1488            }
1489    
1490          p_menu_set->menu_count = 0;          p_menu_set->menu_count = 0;
1491          p_menu_set->menu_item_count = 0;          p_menu_set->menu_item_count = 0;
1492          p_menu_set->menu_screen_count = 0;          p_menu_set->menu_screen_count = 0;
# Line 1439  int detach_menu_shm(MENU_SET *p_menu_set Line 1501  int detach_menu_shm(MENU_SET *p_menu_set
1501          p_menu_set->p_menu_name_dict = NULL;          p_menu_set->p_menu_name_dict = NULL;
1502          p_menu_set->p_menu_screen_dict = NULL;          p_menu_set->p_menu_screen_dict = NULL;
1503    
1504          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)
1505          {          {
1506                  log_error("shmdt() error (%d)\n", errno);                  log_error("munmap() error (%d)", errno);
1507                  return -1;                  return -2;
1508          }          }
1509    
1510          p_menu_set->p_reserved = NULL;          p_menu_set->p_reserved = NULL;


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

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