/[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.28 by sysadm, Mon May 5 11:46:04 2025 UTC Revision 1.39 by sysadm, Wed May 14 04:22:45 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                                                    menu.c  -  description                                                    menu.c  -  description
3                                                           -------------------                                                           -------------------
4          begin                : Wed Mar 16 2004          Copyright            : (C) 2004-2025 by Leaflet
5          copyright            : (C) 2005 by Leaflet          Email                : leaflet@leafok.com
         email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
# Line 18  Line 17 
17  #include "bbs.h"  #include "bbs.h"
18  #include "bbs_cmd.h"  #include "bbs_cmd.h"
19  #include "user_priv.h"  #include "user_priv.h"
 #include "reg_ex.h"  
20  #include "bbs_cmd.h"  #include "bbs_cmd.h"
21  #include "menu.h"  #include "menu.h"
22  #include "log.h"  #include "log.h"
# Line 28  Line 26 
26  #include <string.h>  #include <string.h>
27  #include <stdio.h>  #include <stdio.h>
28  #include <ctype.h>  #include <ctype.h>
 #include <regex.h>  
29  #include <stdlib.h>  #include <stdlib.h>
30    
31  #define MENU_TEMP_DIR "var"  #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"
32    #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"
33    #define MENU_CONF_DELIM_WITHOUT_SPACE "\r\n"
34    
35  MENU_SET bbs_menu;  MENU_SET bbs_menu;
36    
37  int load_menu(MENU_SET *p_menu_set, const char *conf_file)  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
38  {  {
39          FILE *fin, *fout;          FILE *fin, *fout;
40          int i = 0, j;          int fin_line = 0;
41            int i = 0;
42            int j = 0;
43          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
         char screen_filename[FILE_PATH_LEN];  
44          char temp[LINE_BUFFER_LEN];          char temp[LINE_BUFFER_LEN];
45          regmatch_t pmatch[10];          char screen_filename[FILE_PATH_LEN];
46            char *p = NULL;
47            char *q = NULL;
48            char *saveptr = NULL;
49            MENU *p_menu = NULL;
50            MENU_ITEM *p_item = NULL;
51    
52            p_menu_set->menu_count = 0;
53            p_menu_set->p_menu_name_dict = trie_dict_create();
54    
55          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
56          {          {
57                  log_error("Open %s failed", conf_file);                  log_error("Open %s failed", conf_file);
58                  return -1;                  return -2;
59          }          }
60    
61          strncpy(p_menu_set->conf_file, conf_file, sizeof(p_menu_set->conf_file) - 1);          strncpy(p_menu_set->conf_file, conf_file, sizeof(p_menu_set->conf_file) - 1);
# Line 55  int load_menu(MENU_SET *p_menu_set, cons Line 63  int load_menu(MENU_SET *p_menu_set, cons
63    
64          while (fgets(buffer, sizeof(buffer), fin))          while (fgets(buffer, sizeof(buffer), fin))
65          {          {
66                  switch (buffer[0])                  fin_line++;
67    
68                    p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
69                    if (p == NULL) // Blank line
70                  {                  {
71                  case '#':                          continue;
72                          break;                  }
                 case '%':  
                         if (ireg("^%S_([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0)  
                         {  
                                 strncpy(temp, buffer + pmatch[1].rm_so,  
                                                 pmatch[1].rm_eo - pmatch[1].rm_so);  
                                 temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';  
                                 snprintf(screen_filename, sizeof(screen_filename), "%s/MENU_SCR_%s", MENU_TEMP_DIR, temp);  
73    
74                                  if ((fout = fopen(screen_filename, "w")) == NULL)                  if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
75                    {
76                            continue;
77                    }
78    
79                    if (*p == '%')
80                    {
81                            p++;
82    
83                            if (strcmp(p, "menu") == 0) // BEGIN of sub-menu
84                            {
85                                    if (p_menu != NULL)
86                                  {                                  {
87                                          log_error("Open %s failed", screen_filename);                                          log_error("Incomplete menu definition in menu config line %d\n", fin_line);
88                                          return -2;                                          return -1;
89                                  }                                  }
90                                    p_menu = (MENU *)malloc(sizeof(MENU));
91                                  while (fgets(buffer, sizeof(buffer), fin))                                  if (p_menu == NULL)
92                                  {                                  {
93                                          if (buffer[0] != '%')                                          log_error("Unable to allocate memory for menu\n");
94                                                  fputs(buffer, fout);                                          return -3;
                                         else  
                                                 break;  
95                                  }                                  }
96                                    p_menu_set->p_menu[i] = p_menu;
97                                    i++;
98                                    p_menu_set->menu_count = i;
99    
100                                  fclose(fout);                                  j = 0; // Menu item counter
101                                  break;                                  p_menu->item_count = 0;
102                          }                                  p_menu->item_cur_pos = 0;
103                                    p_menu->title.show = 0;
104                                    p_menu->screen.show = 0;
105    
106                          if (ireg("^%menu ([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0)                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
107                          {                                  if (q == NULL)
108                                  p_menu_set->p_menu[i] = malloc(sizeof(MENU));                                  {
109                                  p_menu_set->p_menu[i]->title.show = 0;                                          log_error("Error menu name in menu config line %d\n", fin_line);
110                                  p_menu_set->p_menu[i]->screen.show = 0;                                          return -1;
111                                    }
112                                  strncpy(p_menu_set->p_menu[i]->name,                                  p = q;
113                                                  buffer + pmatch[1].rm_so,                                  while (isalnum(*q) || *q == '_')
114                                                  pmatch[1].rm_eo - pmatch[1].rm_so);                                  {
115                                  p_menu_set->p_menu[i]->name[pmatch[1].rm_eo - pmatch[1].rm_so] =                                          q++;
116                                          '\0';                                  }
117                                    if (*q != '\0')
118                                    {
119                                            log_error("Error menu name in menu config line %d\n", fin_line);
120                                            return -1;
121                                    }
122    
123                                  j = 0;                                  if (q - p > sizeof(p_menu->name) - 1)
124                                    {
125                                            log_error("Too longer menu name in menu config line %d\n", fin_line);
126                                            return -1;
127                                    }
128                                    strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);
129                                    p_menu->name[sizeof(p_menu->name) - 1] = '\0';
130                                    if (trie_dict_set(p_menu_set->p_menu_name_dict, p_menu->name, (int64_t)p_menu) != 1)
131                                    {
132                                            log_error("Error set menu dict [%s]\n", p_menu->name);
133                                    }
134    
135                                    // Check syntax
136                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
137                                    if (q != NULL)
138                                    {
139                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
140                                            return -1;
141                                    }
142    
143                                  while (fgets(buffer, sizeof(buffer), fin))                                  while (fgets(buffer, sizeof(buffer), fin))
144                                  {                                  {
145                                          if (buffer[0] == '#')                                          fin_line++;
146    
147                                            p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
148                                            if (p == NULL) // Blank line
149                                          {                                          {
150                                                  continue;                                                  continue;
151                                          }                                          }
152                                          if (buffer[0] == '%')  
153                                            if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
154                                          {                                          {
155                                                  p_menu_set->p_menu[i]->item_count = j;                                                  continue;
156                                                  p_menu_set->p_menu[i]->item_cur_pos = 0;                                          }
157                                                  i++;  
158                                            if (*p == '%') // END of sub-menu
159                                            {
160                                                    p_menu = NULL;
161                                                  break;                                                  break;
162                                          }                                          }
163                                          if (ireg("^!([A-Za-z0-9_.]+)[[:space:]]*([0-9]+),"                                          else if (*p == '!' || *p == '@')
                                                          "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+),"  
                                                          "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\","  
                                                          "[[:space:]]*\"([^\"]+)\"",  
                                                          buffer, 8, pmatch) == 0)  
164                                          {                                          {
165                                                  p_menu_set->p_menu[i]->items[j] =                                                  // BEGIN of menu item
166                                                          malloc(sizeof(MENU_ITEM));                                                  p_item = (MENU_ITEM *)malloc(sizeof(MENU_ITEM));
167                                                  p_menu_set->p_menu[i]->items[j]->submenu = 1;                                                  if (p_item == NULL)
168                                                  strncpy(p_menu_set->p_menu[i]->items[j]->action,                                                  {
169                                                                  buffer + pmatch[1].rm_so,                                                          log_error("Unable to allocate memory for menu item\n");
170                                                                  pmatch[1].rm_eo - pmatch[1].rm_so);                                                          return -3;
171                                                  p_menu_set->p_menu[i]->items[j]->action[pmatch[1].rm_eo -                                                  }
172                                                                                                                                  pmatch[1].rm_so] = '\0';                                                  p_menu->items[j] = p_item;
                                                 strncpy(temp, buffer + pmatch[2].rm_so,  
                                                                 pmatch[2].rm_eo - pmatch[2].rm_so);  
                                                 temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->items[j]->row = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[3].rm_so,  
                                                                 pmatch[3].rm_eo - pmatch[3].rm_so);  
                                                 temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->items[j]->col = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[4].rm_so,  
                                                                 pmatch[4].rm_eo - pmatch[4].rm_so);  
                                                 temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->items[j]->priv = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[5].rm_so,  
                                                                 pmatch[5].rm_eo - pmatch[5].rm_so);  
                                                 temp[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->items[j]->level = atoi(temp);  
                                                 strncpy(p_menu_set->p_menu[i]->items[j]->name,  
                                                                 buffer + pmatch[6].rm_so,  
                                                                 pmatch[6].rm_eo - pmatch[6].rm_so);  
                                                 p_menu_set->p_menu[i]->items[j]->name[pmatch[6].rm_eo -  
                                                                                                                           pmatch[6].rm_so] =  
                                                         '\0';  
                                                 strncpy(p_menu_set->p_menu[i]->items[j]->text,  
                                                                 buffer + pmatch[7].rm_so,  
                                                                 pmatch[7].rm_eo - pmatch[7].rm_so);  
                                                 p_menu_set->p_menu[i]->items[j]->text[pmatch[7].rm_eo -  
                                                                                                                           pmatch[7].rm_so] =  
                                                         '\0';  
173                                                  j++;                                                  j++;
174                                                  continue;                                                  p_menu->item_count = j;
175    
176                                                    p_item->submenu = (*p == '!' ? 1 : 0);
177    
178                                                    // Menu item action
179                                                    p++;
180                                                    if (strcmp(p, "..") == 0) // Return to parent menu
181                                                    {
182                                                            q = p + 2; // strlen("..")
183                                                    }
184                                                    else
185                                                    {
186                                                            q = p;
187                                                            while (isalnum(*q) || *q == '_')
188                                                            {
189                                                                    q++;
190                                                            }
191                                                            if (*q != '\0')
192                                                            {
193                                                                    log_error("Error menu item action in menu config line %d\n", fin_line);
194                                                                    return -1;
195                                                            }
196                                                    }
197    
198                                                    if (q - p > sizeof(p_item->action) - 1)
199                                                    {
200                                                            log_error("Too longer menu action in menu config line %d\n", fin_line);
201                                                            return -1;
202                                                    }
203                                                    strncpy(p_item->action, p, sizeof(p_item->action) - 1);
204                                                    p_item->action[sizeof(p_item->action) - 1] = '\0';
205    
206                                                    // Menu item row
207                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
208                                                    if (q == NULL)
209                                                    {
210                                                            log_error("Error menu item row in menu config line %d\n", fin_line);
211                                                            return -1;
212                                                    }
213                                                    p = q;
214                                                    while (isdigit(*q))
215                                                    {
216                                                            q++;
217                                                    }
218                                                    if (*q != '\0')
219                                                    {
220                                                            log_error("Error menu item row in menu config line %d\n", fin_line);
221                                                            return -1;
222                                                    }
223                                                    p_item->row = atoi(p);
224    
225                                                    // Menu item col
226                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
227                                                    if (q == NULL)
228                                                    {
229                                                            log_error("Error menu item col in menu config line %d\n", fin_line);
230                                                            return -1;
231                                                    }
232                                                    p = q;
233                                                    while (isdigit(*q))
234                                                    {
235                                                            q++;
236                                                    }
237                                                    if (*q != '\0')
238                                                    {
239                                                            log_error("Error menu item col in menu config line %d\n", fin_line);
240                                                            return -1;
241                                                    }
242                                                    p_item->col = atoi(p);
243    
244                                                    // Menu item priv
245                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
246                                                    if (q == NULL)
247                                                    {
248                                                            log_error("Error menu item priv in menu config line %d\n", fin_line);
249                                                            return -1;
250                                                    }
251                                                    p = q;
252                                                    while (isdigit(*q))
253                                                    {
254                                                            q++;
255                                                    }
256                                                    if (*q != '\0')
257                                                    {
258                                                            log_error("Error menu item priv in menu config line %d\n", fin_line);
259                                                            return -1;
260                                                    }
261                                                    p_item->priv = atoi(p);
262    
263                                                    // Menu item level
264                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
265                                                    if (q == NULL)
266                                                    {
267                                                            log_error("Error menu item level in menu config line %d\n", fin_line);
268                                                            return -1;
269                                                    }
270                                                    p = q;
271                                                    while (isdigit(*q))
272                                                    {
273                                                            q++;
274                                                    }
275                                                    if (*q != '\0')
276                                                    {
277                                                            log_error("Error menu item level in menu config line %d\n", fin_line);
278                                                            return -1;
279                                                    }
280                                                    p_item->level = atoi(p);
281    
282                                                    // Menu item name
283                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
284                                                    if (q == NULL || *q != '\"')
285                                                    {
286                                                            log_error("Error menu item name in menu config line %d\n", fin_line);
287                                                            return -1;
288                                                    }
289                                                    q++;
290                                                    p = q;
291                                                    while (*q != '\0' && *q != '\"')
292                                                    {
293                                                            q++;
294                                                    }
295                                                    if (*q != '\"' || *(q + 1) != '\0')
296                                                    {
297                                                            log_error("Error menu item name in menu config line %d\n", fin_line);
298                                                            return -1;
299                                                    }
300                                                    *q = '\0';
301    
302                                                    if (q - p > sizeof(p_item->name) - 1)
303                                                    {
304                                                            log_error("Too longer menu name in menu config line %d\n", fin_line);
305                                                            return -1;
306                                                    }
307                                                    strncpy(p_item->name, p, sizeof(p_item->name) - 1);
308                                                    p_item->name[sizeof(p_item->name) - 1] = '\0';
309    
310                                                    // Menu item text
311                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
312                                                    if (q == NULL || (q = strchr(q, '\"')) == NULL)
313                                                    {
314                                                            log_error("Error menu item text in menu config line %d\n", fin_line);
315                                                            return -1;
316                                                    }
317                                                    q++;
318                                                    p = q;
319                                                    while (*q != '\0' && *q != '\"')
320                                                    {
321                                                            q++;
322                                                    }
323                                                    if (*q != '\"')
324                                                    {
325                                                            log_error("Error menu item text in menu config line %d\n", fin_line);
326                                                            return -1;
327                                                    }
328                                                    *q = '\0';
329    
330                                                    if (q - p > sizeof(p_item->text) - 1)
331                                                    {
332                                                            log_error("Too longer menu item text in menu config line %d\n", fin_line);
333                                                            return -1;
334                                                    }
335                                                    strncpy(p_item->text, p, sizeof(p_item->text) - 1);
336                                                    p_item->text[sizeof(p_item->text) - 1] = '\0';
337    
338                                                    // Check syntax
339                                                    q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
340                                                    if (q != NULL)
341                                                    {
342                                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
343                                                            return -1;
344                                                    }
345                                          }                                          }
346                                          if (ireg("^@([A-Za-z0-9_]+)[[:space:]]*([0-9]+),"                                          else if (strcmp(p, "title") == 0)
                                                          "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+),"  
                                                          "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\","  
                                                          "[[:space:]]*\"([^\"]+)\"",  
                                                          buffer, 8, pmatch) == 0)  
347                                          {                                          {
348                                                  p_menu_set->p_menu[i]->items[j] =                                                  p_menu->title.show = 1;
349                                                          malloc(sizeof(MENU_ITEM));  
350                                                  p_menu_set->p_menu[i]->items[j]->submenu = 0;                                                  // Menu title row
351                                                  strncpy(p_menu_set->p_menu[i]->items[j]->action,                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
352                                                                  buffer + pmatch[1].rm_so,                                                  if (q == NULL)
353                                                                  pmatch[1].rm_eo - pmatch[1].rm_so);                                                  {
354                                                  p_menu_set->p_menu[i]->items[j]->action[pmatch[1].rm_eo -                                                          log_error("Error menu title row in menu config line %d\n", fin_line);
355                                                                                                                                  pmatch[1].rm_so] = '\0';                                                          return -1;
356                                                  strncpy(temp, buffer + pmatch[2].rm_so,                                                  }
357                                                                  pmatch[2].rm_eo - pmatch[2].rm_so);                                                  p = q;
358                                                  temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                  while (isdigit(*q))
359                                                  p_menu_set->p_menu[i]->items[j]->row = atoi(temp);                                                  {
360                                                  strncpy(temp,                                                          q++;
361                                                                  buffer + pmatch[3].rm_so,                                                  }
362                                                                  pmatch[3].rm_eo - pmatch[3].rm_so);                                                  if (*q != '\0')
363                                                  temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                                  {
364                                                  p_menu_set->p_menu[i]->items[j]->col = atoi(temp);                                                          log_error("Error menu title row in menu config line %d\n", fin_line);
365                                                  strncpy(temp,                                                          return -1;
366                                                                  buffer + pmatch[4].rm_so,                                                  }
367                                                                  pmatch[4].rm_eo - pmatch[4].rm_so);                                                  p_menu->title.row = atoi(p);
368                                                  temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';  
369                                                  p_menu_set->p_menu[i]->items[j]->priv = atoi(temp);                                                  // Menu title col
370                                                  strncpy(temp,                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
371                                                                  buffer + pmatch[5].rm_so,                                                  if (q == NULL)
372                                                                  pmatch[5].rm_eo - pmatch[5].rm_so);                                                  {
373                                                  temp[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0';                                                          log_error("Error menu title col in menu config line %d\n", fin_line);
374                                                  p_menu_set->p_menu[i]->items[j]->level = atoi(temp);                                                          return -1;
375                                                  strncpy(p_menu_set->p_menu[i]->items[j]->name,                                                  }
376                                                                  buffer + pmatch[6].rm_so,                                                  p = q;
377                                                                  pmatch[6].rm_eo - pmatch[6].rm_so);                                                  while (isdigit(*q))
378                                                  p_menu_set->p_menu[i]->items[j]->name[pmatch[6].rm_eo -                                                  {
379                                                                                                                            pmatch[6].rm_so] =                                                          q++;
380                                                          '\0';                                                  }
381                                                  strncpy(p_menu_set->p_menu[i]->items[j]->text,                                                  if (*q != '\0')
382                                                                  buffer + pmatch[7].rm_so,                                                  {
383                                                                  pmatch[7].rm_eo - pmatch[7].rm_so);                                                          log_error("Error menu title col in menu config line %d\n", fin_line);
384                                                  p_menu_set->p_menu[i]->items[j]->text[pmatch[7].rm_eo -                                                          return -1;
385                                                                                                                            pmatch[7].rm_so] =                                                  }
386                                                          '\0';                                                  p_menu->title.col = atoi(p);
387                                                  j++;  
388                                                  continue;                                                  // Menu title text
389                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
390                                                    if (q == NULL || (q = strchr(q, '\"')) == NULL)
391                                                    {
392                                                            log_error("Error menu title text in menu config line %d\n", fin_line);
393                                                            return -1;
394                                                    }
395                                                    q++;
396                                                    p = q;
397                                                    while (*q != '\0' && *q != '\"')
398                                                    {
399                                                            q++;
400                                                    }
401                                                    if (*q != '\"')
402                                                    {
403                                                            log_error("Error menu title text in menu config line %d\n", fin_line);
404                                                            return -1;
405                                                    }
406                                                    *q = '\0';
407    
408                                                    if (q - p > sizeof(p_item->text) - 1)
409                                                    {
410                                                            log_error("Too longer menu title text in menu config line %d\n", fin_line);
411                                                            return -1;
412                                                    }
413                                                    strncpy(p_menu->title.text, p, sizeof(p_menu->title.text) - 1);
414                                                    p_menu->title.text[sizeof(p_menu->title.text) - 1] = '\0';
415    
416                                                    // Check syntax
417                                                    q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
418                                                    if (q != NULL)
419                                                    {
420                                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
421                                                            return -1;
422                                                    }
423                                          }                                          }
424                                          if (ireg("^title[[:space:]]*([0-9]+),"                                          else if (strcmp(p, "screen") == 0)
                                                          "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\"",  
                                                          buffer, 4, pmatch) == 0)  
425                                          {                                          {
426                                                  p_menu_set->p_menu[i]->title.show = 1;                                                  p_menu->screen.show = 1;
427                                                  strncpy(temp,  
428                                                                  buffer + pmatch[1].rm_so,                                                  // Menu screen row
429                                                                  pmatch[1].rm_eo - pmatch[1].rm_so);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
430                                                  temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                                  if (q == NULL)
431                                                  p_menu_set->p_menu[i]->title.row = atoi(temp);                                                  {
432                                                  strncpy(temp,                                                          log_error("Error menu screen row in menu config line %d\n", fin_line);
433                                                                  buffer + pmatch[2].rm_so,                                                          return -1;
434                                                                  pmatch[2].rm_eo - pmatch[2].rm_so);                                                  }
435                                                  temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                  p = q;
436                                                  p_menu_set->p_menu[i]->title.col = atoi(temp);                                                  while (isdigit(*q))
437                                                  strncpy(p_menu_set->p_menu[i]->title.text,                                                  {
438                                                                  buffer + pmatch[3].rm_so,                                                          q++;
439                                                                  pmatch[3].rm_eo - pmatch[3].rm_so);                                                  }
440                                                  p_menu_set->p_menu[i]->title.text[pmatch[3].rm_eo -                                                  if (*q != '\0')
441                                                                                                                    pmatch[3].rm_so] =                                                  {
442                                                          '\0';                                                          log_error("Error menu screen row in menu config line %d\n", fin_line);
443                                                  continue;                                                          return -1;
444                                                    }
445                                                    p_menu->screen.row = atoi(p);
446    
447                                                    // Menu screen col
448                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
449                                                    if (q == NULL)
450                                                    {
451                                                            log_error("Error menu screen col in menu config line %d\n", fin_line);
452                                                            return -1;
453                                                    }
454                                                    p = q;
455                                                    while (isdigit(*q))
456                                                    {
457                                                            q++;
458                                                    }
459                                                    if (*q != '\0')
460                                                    {
461                                                            log_error("Error menu screen col in menu config line %d\n", fin_line);
462                                                            return -1;
463                                                    }
464                                                    p_menu->screen.col = atoi(p);
465    
466                                                    // Menu screen name
467                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
468                                                    if (q == NULL)
469                                                    {
470                                                            log_error("Error menu screen name in menu config line %d\n", fin_line);
471                                                            return -1;
472                                                    }
473                                                    p = q;
474                                                    while (isalnum(*q) || *q == '_')
475                                                    {
476                                                            q++;
477                                                    }
478                                                    if (*q != '\0')
479                                                    {
480                                                            log_error("Error menu screen name in menu config line %d\n", fin_line);
481                                                            return -1;
482                                                    }
483    
484                                                    snprintf(p_menu->screen.filename, sizeof(p_menu->screen.filename), "%s%s", MENU_SCREEN_PATH_PREFIX, p);
485    
486                                                    // Check syntax
487                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
488                                                    if (q != NULL)
489                                                    {
490                                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
491                                                            return -1;
492                                                    }
493                                          }                                          }
494                                          if (ireg("^screen[[:space:]]*([0-9]+),"                                  }
495                                                           "[[:space:]]*([0-9]+),[[:space:]]*S_([A-Za-z0-9_]+)",                          }
496                                                           buffer, 4, pmatch) == 0)                          else // BEGIN of menu screen
497                            {
498                                    q = p;
499                                    while (isalnum(*q) || *q == '_')
500                                    {
501                                            q++;
502                                    }
503                                    if (*q != '\0')
504                                    {
505                                            log_error("Error menu screen name in menu config line %d\n", fin_line);
506                                            return -1;
507                                    }
508    
509                                    snprintf(screen_filename, sizeof(screen_filename), "%s%s", MENU_SCREEN_PATH_PREFIX, p);
510    
511                                    // Check syntax
512                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
513                                    if (q != NULL)
514                                    {
515                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
516                                            return -1;
517                                    }
518    
519                                    if ((fout = fopen(screen_filename, "w")) == NULL)
520                                    {
521                                            log_error("Open %s failed", screen_filename);
522                                            return -2;
523                                    }
524    
525                                    while (fgets(buffer, sizeof(buffer), fin))
526                                    {
527                                            fin_line++;
528    
529                                            strncpy(temp, buffer, sizeof(temp)); // Duplicate line for strtok_r
530                                            p = strtok_r(temp, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
531                                            if (p != NULL && *p == '%') // END of menu screen
532                                          {                                          {
533                                                  p_menu_set->p_menu[i]->screen.show = 1;                                                  break;
534                                                  strncpy(temp,                                          }
535                                                                  buffer + pmatch[1].rm_so,  
536                                                                  pmatch[1].rm_eo - pmatch[1].rm_so);                                          if (fputs(buffer, fout) < 0)
537                                                  temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                          {
538                                                  p_menu_set->p_menu[i]->screen.row = atoi(temp);                                                  log_error("Write %s failed", screen_filename);
539                                                  strncpy(temp,                                                  return -2;
                                                                 buffer + pmatch[2].rm_so,  
                                                                 pmatch[2].rm_eo - pmatch[2].rm_so);  
                                                 temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->screen.col = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[3].rm_so,  
                                                                 pmatch[3].rm_eo - pmatch[3].rm_so);  
                                                 temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';  
                                                 snprintf(p_menu_set->p_menu[i]->screen.filename,  
                                                                  sizeof(p_menu_set->p_menu[i]->screen.filename),  
                                                                  "%s/MENU_SCR_%s", MENU_TEMP_DIR, temp);  
                                                 continue;  
540                                          }                                          }
541                                  }                                  }
542    
543                                    fclose(fout);
544                          }                          }
545                          break;                  }
546                    else // Invalid prefix
547                    {
548                            log_error("Error in menu config line %d\n", fin_line);
549                            return -1;
550                  }                  }
551          }          }
552          fclose(fin);          fclose(fin);
# Line 269  int load_menu(MENU_SET *p_menu_set, cons Line 558  int load_menu(MENU_SET *p_menu_set, cons
558          return 0;          return 0;
559  }  }
560    
561  MENU *  MENU *get_menu(MENU_SET *p_menu_set, const char *menu_name)
 get_menu(MENU_SET *p_menu_set, const char *menu_name)  
562  {  {
563          int i;          int ret;
564            int64_t value = 0;
565    
566          for (i = 0; i < p_menu_set->menu_count; i++)          ret = trie_dict_get(p_menu_set->p_menu_name_dict, menu_name, &value);
567    
568            if (ret == 1) // found
569          {          {
570                  if (strcmp(p_menu_set->p_menu[i]->name, menu_name) == 0)                  return ((MENU *)value);
                 {  
                         return p_menu_set->p_menu[i];  
                 }  
571          }          }
572    
573          return NULL;          return NULL;
# Line 304  int display_menu(MENU *p_menu) Line 592  int display_menu(MENU *p_menu)
592                  return -1;                  return -1;
593          }          }
594    
595            if (p_menu->item_cur_pos > 0 &&
596                    checkpriv(&BBS_priv, 0, p_menu->items[p_menu->item_cur_pos]->priv) != 0 &&
597                    checklevel(&BBS_priv, p_menu->items[p_menu->item_cur_pos]->level) != 0)
598            {
599                    menu_selectable = 1;
600            }
601    
602          if (p_menu->title.show)          if (p_menu->title.show)
603          {          {
604                  show_top(p_menu->title.text);                  show_top(p_menu->title.text);
# Line 390  int menu_control(MENU_SET *p_menu_set, i Line 685  int menu_control(MENU_SET *p_menu_set, i
685          switch (key)          switch (key)
686          {          {
687          case CR:          case CR:
                 igetch(1); // Cleanup remaining '\n' in the buffer  
688          case KEY_RIGHT:          case KEY_RIGHT:
689                  if (p_menu->items[p_menu->item_cur_pos]->submenu)                  if (p_menu->items[p_menu->item_cur_pos]->submenu)
690                  {                  {
# Line 406  int menu_control(MENU_SET *p_menu_set, i Line 700  int menu_control(MENU_SET *p_menu_set, i
700                          {                          {
701                                  return menu_control(p_menu_set, KEY_LEFT);                                  return menu_control(p_menu_set, KEY_LEFT);
702                          }                          }
                         break;  
703                  }                  }
704                  else                  else
705                  {                  {
706                          return (exec_cmd(p_menu->items[p_menu->item_cur_pos]->action,                          return (exec_cmd(p_menu->items[p_menu->item_cur_pos]->action,
707                                                           p_menu->items[p_menu->item_cur_pos]->name));                                                           p_menu->items[p_menu->item_cur_pos]->name));
708                  }                  }
709                    break;
710          case KEY_LEFT:          case KEY_LEFT:
711                  if (p_menu_set->menu_select_depth > 0)                  if (p_menu_set->menu_select_depth > 0)
712                  {                  {
# Line 421  int menu_control(MENU_SET *p_menu_set, i Line 715  int menu_control(MENU_SET *p_menu_set, i
715                          {                          {
716                                  return menu_control(p_menu_set, KEY_LEFT);                                  return menu_control(p_menu_set, KEY_LEFT);
717                          }                          }
                         break;  
718                  }                  }
719                  else                  else
720                  {                  {
721                          display_menu_cursor(p_menu, 0);                          display_menu_cursor(p_menu, 0);
722                          p_menu->item_cur_pos = p_menu->item_count - 1;                          p_menu->item_cur_pos = p_menu->item_count - 1;
723                          while (!p_menu->items[p_menu->item_cur_pos]->display ||                          while (p_menu->item_cur_pos >= 0 && (!p_menu->items[p_menu->item_cur_pos]->display ||
724                                     p_menu->items[p_menu->item_cur_pos]->priv != 0 ||                                                                                                   p_menu->items[p_menu->item_cur_pos]->priv != 0 ||
725                                     p_menu->items[p_menu->item_cur_pos]->level != 0)                                                                                                   p_menu->items[p_menu->item_cur_pos]->level != 0))
726                          {                          {
727                                  p_menu->item_cur_pos--;                                  p_menu->item_cur_pos--;
728                          }                          }
729                          display_menu_cursor(p_menu, 1);                          display_menu_cursor(p_menu, 1);
                         break;  
730                  }                  }
731                    break;
732          case KEY_UP:          case KEY_UP:
733                  display_menu_cursor(p_menu, 0);                  display_menu_cursor(p_menu, 0);
734                  do                  do
# Line 461  int menu_control(MENU_SET *p_menu_set, i Line 754  int menu_control(MENU_SET *p_menu_set, i
754                  display_menu_cursor(p_menu, 1);                  display_menu_cursor(p_menu, 1);
755                  break;                  break;
756          default:          default:
757                  for (i = 0; i < p_menu->item_count; i++)                  if (isalnum(key))
                 {  
                         if (key == p_menu->items[i]->name[0] &&  
                                 p_menu->items[i]->display)  
                         {  
                                 display_menu_cursor(p_menu, 0);  
                                 p_menu->item_cur_pos = i;  
                                 display_menu_cursor(p_menu, 1);  
                                 return 0;  
                         }  
                 }  
                 if (isalpha(key))  
758                  {                  {
759                          for (i = 0; i < p_menu->item_count; i++)                          for (i = 0; i < p_menu->item_count; i++)
760                          {                          {
# Line 486  int menu_control(MENU_SET *p_menu_set, i Line 768  int menu_control(MENU_SET *p_menu_set, i
768                                  }                                  }
769                          }                          }
770                  }                  }
771                    break;
772          }          }
773    
774          return 0;          return 0;
# Line 496  void unload_menu(MENU_SET *p_menu_set) Line 779  void unload_menu(MENU_SET *p_menu_set)
779          MENU *p_menu;          MENU *p_menu;
780          int i, j;          int i, j;
781    
782            if (p_menu_set->p_menu_name_dict != NULL)
783            {
784                    trie_dict_destroy(p_menu_set->p_menu_name_dict);
785                    p_menu_set->p_menu_name_dict = NULL;
786            }
787    
788          for (i = 0; i < p_menu_set->menu_count; i++)          for (i = 0; i < p_menu_set->menu_count; i++)
789          {          {
790                  p_menu = p_menu_set->p_menu[i];                  p_menu = p_menu_set->p_menu[i];
# Line 503  void unload_menu(MENU_SET *p_menu_set) Line 792  void unload_menu(MENU_SET *p_menu_set)
792                  {                  {
793                          free(p_menu->items[j]);                          free(p_menu->items[j]);
794                  }                  }
                 remove(p_menu->screen.filename);  
795                  free(p_menu);                  free(p_menu);
796          }          }
797    


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

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