/[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.8 by sysadm, Sun Mar 20 14:49:24 2005 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   ***************************************************************************/   ***************************************************************************/
16    
17  #include "bbs.h"  #include "bbs.h"
18    #include "bbs_cmd.h"
19    #include "user_priv.h"
20    #include "bbs_cmd.h"
21  #include "menu.h"  #include "menu.h"
22    #include "log.h"
23  #include "io.h"  #include "io.h"
24    #include "screen.h"
25  #include "common.h"  #include "common.h"
26    #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_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  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
 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    char buffer[256], screen_filename[256], temp[256];          int i = 0;
42    regmatch_t pmatch[10];          int j = 0;
43            char buffer[LINE_BUFFER_LEN];
44    if ((fin = fopen (conf_file, "r")) == NULL)          char temp[LINE_BUFFER_LEN];
45      {          char screen_filename[FILE_PATH_LEN];
46        log_error ("Open %s failed", conf_file);          char *p = NULL;
47        return -1;          char *q = NULL;
48      }          char *saveptr = NULL;
49            MENU *p_menu = NULL;
50    while (fgets (buffer, 255, fin))          MENU_ITEM *p_item = NULL;
51      {  
52        switch (buffer[0])          p_menu_set->menu_count = 0;
53          {          p_menu_set->p_menu_name_dict = trie_dict_create();
54          case '#':  
55            break;          if ((fin = fopen(conf_file, "r")) == NULL)
56          case '%':          {
57            if (ireg ("^%S_([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0)                  log_error("Open %s failed", conf_file);
58              {                  return -2;
59                strncpy (temp, buffer + pmatch[1].rm_so,          }
60                         pmatch[1].rm_eo - pmatch[1].rm_so);  
61                temp [pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';          strncpy(p_menu_set->conf_file, conf_file, sizeof(p_menu_set->conf_file) - 1);
62                sprintf (screen_filename, "%sMENU_SCR_%s", app_temp_dir, temp);          p_menu_set->conf_file[sizeof(p_menu_set->conf_file) - 1] = '\0';
63    
64                if ((fout = fopen (screen_filename, "w")) == NULL)          while (fgets(buffer, sizeof(buffer), fin))
65                  {          {
66                    log_error ("Open %s failed", screen_filename);                  fin_line++;
67                    return -2;  
68                  }                  p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
69                    if (p == NULL) // Blank line
70                while (fgets (buffer, 255, fin))                  {
71                  {                          continue;
72                    if (buffer[0] != '%')                  }
73                      fputs (buffer, fout);  
74                    else                  if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
75                      break;                  {
76                  }                          continue;
77                    }
78                fclose (fout);  
79                break;                  if (*p == '%')
80              }                  {
81                            p++;
82            if (ireg ("^%menu ([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0)  
83              {                          if (strcmp(p, "menu") == 0) // BEGIN of sub-menu
84                p_menu_set->p_menu[i] = malloc (sizeof (MENU));                          {
85                                    if (p_menu != NULL)
86                strncpy (p_menu_set->p_menu[i]->name,                                  {
87                  buffer + pmatch[1].rm_so,                                          log_error("Incomplete menu definition in menu config line %d\n", fin_line);
88                  pmatch[1].rm_eo - pmatch[1].rm_so);                                          return -1;
89                p_menu_set->p_menu[i]->name[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                  }
90                                    p_menu = (MENU *)malloc(sizeof(MENU));
91                j = 0;                                  if (p_menu == NULL)
92                                                  {
93                while (fgets (buffer, 255, fin))                                          log_error("Unable to allocate memory for menu\n");
94                  {                                          return -3;
95                    if (buffer[0] == '#')                                  }
96                    {                                  p_menu_set->p_menu[i] = p_menu;
97                      break;                                  i++;
98                    }                                  p_menu_set->menu_count = i;
99                    if (buffer[0] == '%')  
100                    {                                  j = 0; // Menu item counter
101                      p_menu_set->p_menu[i]->item_count = j;                                  p_menu->item_count = 0;
102                      p_menu_set->p_menu[i]->item_cur_pos = 0;                                  p_menu->item_cur_pos = 0;
103                      i++;                                  p_menu->title.show = 0;
104                      break;                                  p_menu->screen.show = 0;
105                    }  
106                    if (ireg (                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
107                      "^!([A-Za-z0-9_.]+)[[:space:]]*([0-9]+),"                                  if (q == NULL)
108                      "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+),"                                  {
109                      "[[:space:]]*\"([A-Za-z0-9_]+)\",[[:space:]]*\"([^\"]+)\"",                                          log_error("Error menu name in menu config line %d\n", fin_line);
110                      buffer, 7, pmatch) == 0)                                          return -1;
111                      {                                  }
112                        p_menu_set->p_menu[i]->items[j] = malloc (sizeof (MENU_ITEM));                                  p = q;
113                        p_menu_set->p_menu[i]->items[j]->submenu = 1;                                  while (isalnum(*q) || *q == '_')
114                        strncpy (p_menu_set->p_menu[i]->items[j]->action,                                  {
115                          buffer + pmatch[1].rm_so,                                          q++;
116                          pmatch[1].rm_eo - pmatch[1].rm_so);                                  }
117                        p_menu_set->p_menu[i]->items[j]->action[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                  if (*q != '\0')
118                        strncpy (temp,                                  {
119                          buffer + pmatch[2].rm_so,                                          log_error("Error menu name in menu config line %d\n", fin_line);
120                          pmatch[2].rm_eo - pmatch[2].rm_so);                                          return -1;
121                        temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                  }
122                        p_menu_set->p_menu[i]->items[j]->row = atoi (temp);  
123                        strncpy (temp,                                  if (q - p > sizeof(p_menu->name) - 1)
124                          buffer + pmatch[3].rm_so,                                  {
125                          pmatch[3].rm_eo - pmatch[3].rm_so);                                          log_error("Too longer menu name in menu config line %d\n", fin_line);
126                        temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                          return -1;
127                        p_menu_set->p_menu[i]->items[j]->col = atoi (temp);                                  }
128                        strncpy (temp,                                  strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);
129                          buffer + pmatch[4].rm_so,                                  p_menu->name[sizeof(p_menu->name) - 1] = '\0';
130                          pmatch[4].rm_eo - pmatch[4].rm_so);                                  if (trie_dict_set(p_menu_set->p_menu_name_dict, p_menu->name, (int64_t)p_menu) != 1)
131                        temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';                                  {
132                        p_menu_set->p_menu[i]->items[j]->priv = atoi (temp);                                          log_error("Error set menu dict [%s]\n", p_menu->name);
133                        strncpy (p_menu_set->p_menu[i]->items[j]->name,                                  }
134                          buffer + pmatch[5].rm_so,  
135                          pmatch[5].rm_eo - pmatch[5].rm_so);                                  // Check syntax
136                        p_menu_set->p_menu[i]->items[j]->name[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0';                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
137                        strncpy (p_menu_set->p_menu[i]->items[j]->text,                                  if (q != NULL)
138                          buffer + pmatch[6].rm_so,                                  {
139                          pmatch[6].rm_eo - pmatch[6].rm_so);                                          log_error("Unknown extra content in menu config line %d\n", fin_line);
140                        p_menu_set->p_menu[i]->items[j]->text[pmatch[6].rm_eo - pmatch[6].rm_so] = '\0';                                          return -1;
141                        j++;                                  }
142                        continue;  
143                      }                                  while (fgets(buffer, sizeof(buffer), fin))
144                    if (ireg (                                  {
145                      "^@([A-Za-z0-9_]+)[[:space:]]*([0-9]+),"                                          fin_line++;
146                      "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+),"  
147                      "[[:space:]]*\"([A-Za-z0-9_]+)\",[[:space:]]*\"([^\"]+)\"",                                          p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
148                      buffer, 7, pmatch) == 0)                                          if (p == NULL) // Blank line
149                      {                                          {
150                        p_menu_set->p_menu[i]->items[j] = malloc (sizeof (MENU_ITEM));                                                  continue;
151                        p_menu_set->p_menu[i]->items[j]->submenu = 0;                                          }
152                        strncpy (p_menu_set->p_menu[i]->items[j]->action,  
153                          buffer + pmatch[1].rm_so,                                          if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
154                          pmatch[1].rm_eo - pmatch[1].rm_so);                                          {
155                        p_menu_set->p_menu[i]->items[j]->action[pmatch[0].rm_eo - pmatch[0].rm_so] = '\0';                                                  continue;
156                        strncpy (temp,                                          }
157                          buffer + pmatch[2].rm_so,  
158                          pmatch[2].rm_eo - pmatch[2].rm_so);                                          if (*p == '%') // END of sub-menu
159                        temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                          {
160                        p_menu_set->p_menu[i]->items[j]->row = atoi (temp);                                                  p_menu = NULL;
161                        strncpy (temp,                                                  break;
162                          buffer + pmatch[3].rm_so,                                          }
163                          pmatch[3].rm_eo - pmatch[3].rm_so);                                          else if (*p == '!' || *p == '@')
164                        temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                          {
165                        p_menu_set->p_menu[i]->items[j]->col = atoi (temp);                                                  // BEGIN of menu item
166                        strncpy (temp,                                                  p_item = (MENU_ITEM *)malloc(sizeof(MENU_ITEM));
167                          buffer + pmatch[4].rm_so,                                                  if (p_item == NULL)
168                          pmatch[4].rm_eo - pmatch[4].rm_so);                                                  {
169                        temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';                                                          log_error("Unable to allocate memory for menu item\n");
170                        p_menu_set->p_menu[i]->items[j]->priv = atoi (temp);                                                          return -3;
171                        strncpy (p_menu_set->p_menu[i]->items[j]->name,                                                  }
172                          buffer + pmatch[5].rm_so,                                                  p_menu->items[j] = p_item;
173                          pmatch[5].rm_eo - pmatch[5].rm_so);                                                  j++;
174                        p_menu_set->p_menu[i]->items[j]->name[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0';                                                  p_menu->item_count = j;
175                        strncpy (p_menu_set->p_menu[i]->items[j]->text,  
176                          buffer + pmatch[6].rm_so,                                                  p_item->submenu = (*p == '!' ? 1 : 0);
177                          pmatch[6].rm_eo - pmatch[6].rm_so);  
178                        p_menu_set->p_menu[i]->items[j]->text[pmatch[6].rm_eo - pmatch[6].rm_so] = '\0';                                                  // Menu item action
179                        j++;                                                  p++;
180                        continue;                                                  if (strcmp(p, "..") == 0) // Return to parent menu
181                      }                                                  {
182                    if (ireg (                                                          q = p + 2; // strlen("..")
183                      "^title[[:space:]]*([0-9]+),"                                                  }
184                      "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\"",                                                  else
185                      buffer, 4, pmatch) == 0)                                                  {
186                      {                                                          q = p;
187                        strncpy (temp,                                                          while (isalnum(*q) || *q == '_')
188                          buffer + pmatch[1].rm_so,                                                          {
189                          pmatch[1].rm_eo - pmatch[1].rm_so);                                                                  q++;
190                        temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                                          }
191                        p_menu_set->p_menu[i]->title.row = atoi (temp);                                                          if (*q != '\0')
192                        strncpy (temp,                                                          {
193                          buffer + pmatch[2].rm_so,                                                                  log_error("Error menu item action in menu config line %d\n", fin_line);
194                          pmatch[2].rm_eo - pmatch[2].rm_so);                                                                  return -1;
195                        temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                          }
196                        p_menu_set->p_menu[i]->title.col = atoi (temp);                                                  }
197                        strncpy (p_menu_set->p_menu[i]->title.text,  
198                          buffer + pmatch[3].rm_so,                                                  if (q - p > sizeof(p_item->action) - 1)
199                          pmatch[3].rm_eo - pmatch[3].rm_so);                                                  {
200                        p_menu_set->p_menu[i]->title.text[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                                          log_error("Too longer menu action in menu config line %d\n", fin_line);
201                        continue;                                                          return -1;
202                      }                                                  }
203                    if (ireg (                                                  strncpy(p_item->action, p, sizeof(p_item->action) - 1);
204                      "^title[[:space:]]*([0-9]+),"                                                  p_item->action[sizeof(p_item->action) - 1] = '\0';
205                      "[[:space:]]*([0-9]+),[[:space:]]*S_([A-Za-z0-9_]+)",  
206                      buffer, 4, pmatch) == 0)                                                  // Menu item row
207                      {                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
208                        strncpy (temp,                                                  if (q == NULL)
209                          buffer + pmatch[1].rm_so,                                                  {
210                          pmatch[1].rm_eo - pmatch[1].rm_so);                                                          log_error("Error menu item row in menu config line %d\n", fin_line);
211                        temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                                          return -1;
212                        p_menu_set->p_menu[i]->screen.row = atoi (temp);                                                  }
213                        strncpy (temp,                                                  p = q;
214                          buffer + pmatch[2].rm_so,                                                  while (isdigit(*q))
215                          pmatch[2].rm_eo - pmatch[2].rm_so);                                                  {
216                        temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                          q++;
217                        p_menu_set->p_menu[i]->screen.col = atoi (temp);                                                  }
218                        strncpy (temp,                                                  if (*q != '\0')
219                          buffer + pmatch[3].rm_so,                                                  {
220                          pmatch[3].rm_eo - pmatch[3].rm_so);                                                          log_error("Error menu item row in menu config line %d\n", fin_line);
221                        temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                                          return -1;
222                        sprintf (p_menu_set->p_menu[i]->screen.filename,                                                  }
223                          "%sMENU_SCR_%s", app_temp_dir, temp);                                                  p_item->row = atoi(p);
224                        continue;  
225                      }                                                  // Menu item col
226                  }                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
227              }                                                  if (q == NULL)
228            break;                                                  {
229          }                                                          log_error("Error menu item col in menu config line %d\n", fin_line);
230      }                                                          return -1;
231    fclose (fin);                                                  }
232                                                    p = q;
233    p_menu_set->menu_count = i;                                                  while (isdigit(*q))
234    p_menu_set->menu_select_depth = 0;                                                  {
235    p_menu_set->p_menu_select[p_menu_set->menu_select_depth]                                                          q++;
236      = (i == 0 ? NULL : p_menu_set->p_menu[0]);                                                  }
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                                            else if (strcmp(p, "title") == 0)
347                                            {
348                                                    p_menu->title.show = 1;
349    
350                                                    // Menu title row
351                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
352                                                    if (q == NULL)
353                                                    {
354                                                            log_error("Error menu title row in menu config line %d\n", fin_line);
355                                                            return -1;
356                                                    }
357                                                    p = q;
358                                                    while (isdigit(*q))
359                                                    {
360                                                            q++;
361                                                    }
362                                                    if (*q != '\0')
363                                                    {
364                                                            log_error("Error menu title row in menu config line %d\n", fin_line);
365                                                            return -1;
366                                                    }
367                                                    p_menu->title.row = atoi(p);
368    
369                                                    // Menu title col
370                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
371                                                    if (q == NULL)
372                                                    {
373                                                            log_error("Error menu title col in menu config line %d\n", fin_line);
374                                                            return -1;
375                                                    }
376                                                    p = q;
377                                                    while (isdigit(*q))
378                                                    {
379                                                            q++;
380                                                    }
381                                                    if (*q != '\0')
382                                                    {
383                                                            log_error("Error menu title col in menu config line %d\n", fin_line);
384                                                            return -1;
385                                                    }
386                                                    p_menu->title.col = atoi(p);
387    
388                                                    // 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                                            else if (strcmp(p, "screen") == 0)
425                                            {
426                                                    p_menu->screen.show = 1;
427    
428                                                    // Menu screen row
429                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
430                                                    if (q == NULL)
431                                                    {
432                                                            log_error("Error menu screen row in menu config line %d\n", fin_line);
433                                                            return -1;
434                                                    }
435                                                    p = q;
436                                                    while (isdigit(*q))
437                                                    {
438                                                            q++;
439                                                    }
440                                                    if (*q != '\0')
441                                                    {
442                                                            log_error("Error menu screen row in menu config line %d\n", fin_line);
443                                                            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                                    }
495                            }
496                            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                                                    break;
534                                            }
535    
536                                            if (fputs(buffer, fout) < 0)
537                                            {
538                                                    log_error("Write %s failed", screen_filename);
539                                                    return -2;
540                                            }
541                                    }
542    
543                                    fclose(fout);
544                            }
545                    }
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);
553    
554            p_menu_set->menu_count = i;
555            p_menu_set->menu_select_depth = 0;
556            p_menu_set->p_menu_select[p_menu_set->menu_select_depth] = (i == 0 ? NULL : p_menu_set->p_menu[0]);
557    
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);
   {  
     if (strcmp(p_menu_set->p_menu[i]->name, menu_name) == 0)  
     {  
       return p_menu_set->p_menu[i];  
     }  
   }  
567    
568    return NULL;          if (ret == 1) // found
569            {
570                    return ((MENU *)value);
571            }
572    
573            return NULL;
574  }  }
575    
576  void  static void display_menu_cursor(MENU *p_menu, int show)
 display_menu_cursor (MENU * p_menu, int show)  
577  {  {
578    moveto ((p_menu->items[p_menu->item_cur_pos])->r_row,          moveto((p_menu->items[p_menu->item_cur_pos])->r_row,
579            (p_menu->items[p_menu->item_cur_pos])->r_col - 2);                     (p_menu->items[p_menu->item_cur_pos])->r_col - 2);
580    prints (show ? ">" : " ");          outc(show ? '>' : ' ');
581    iflush ();          iflush();
582  }  }
583    
584  int  int display_menu(MENU *p_menu)
 display_menu (MENU * p_menu)  
585  {  {
586    int i, row, col, menu_selectable = 0;          int row = 0;
587            int col = 0;
588            int menu_selectable = 0;
589    
590            if (p_menu == NULL)
591            {
592                    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)
603            {
604                    show_top(p_menu->title.text);
605            }
606    
607            if (p_menu->screen.show)
608            {
609                    moveto(p_menu->screen.row, p_menu->screen.col);
610                    if (display_file(p_menu->screen.filename) != 0)
611                    {
612                            log_error("Display menu screen <%s> failed!\n",
613                                              p_menu->screen.filename);
614                    }
615            }
616    
617            for (int i = 0; i < p_menu->item_count; i++)
618            {
619                    if (p_menu->items[i]->row != 0)
620                    {
621                            row = p_menu->items[i]->row;
622                    }
623                    if (p_menu->items[i]->col != 0)
624                    {
625                            col = p_menu->items[i]->col;
626                    }
627    
628                    if (checkpriv(&BBS_priv, 0, p_menu->items[i]->priv) == 0 || checklevel(&BBS_priv, p_menu->items[i]->level) == 0)
629                    {
630                            p_menu->items[i]->display = 0;
631                            p_menu->items[i]->r_row = 0;
632                            p_menu->items[i]->r_col = 0;
633                    }
634                    else
635                    {
636                            p_menu->items[i]->display = 1;
637    
638    if (p_menu == NULL)                          if (!menu_selectable)
639      return -1;                          {
640                                    p_menu->item_cur_pos = i;
641                                    menu_selectable = 1;
642                            }
643    
644    show_top (p_menu->title.text);                          p_menu->items[i]->r_row = row;
645                            p_menu->items[i]->r_col = col;
646    
647                            moveto(row, col);
648                            prints("%s", p_menu->items[i]->text);
649    
650                            row++;
651                    }
652            }
653    
654    moveto (p_menu->screen.row, p_menu->screen.col);          if (!menu_selectable)
655    display_file (p_menu->screen.filename);          {
656                    return -1;
657    row = p_menu->items[0]->row;          }
   col = p_menu->items[0]->col;  
   
   for (i = 0; i < p_menu->item_count; i++)  
     {  
       if (checkpriv (&BBS_priv, 0, p_menu->items[i]->priv) == 0)  
       {  
         p_menu->items[i]->display = 0;  
       }  
       else  
       {  
         p_menu->items[i]->display = 1;  
   
         menu_selectable = 1;  
           
         if (p_menu->items[i]->row != 0)  
           row = p_menu->items[i]->row;  
         else  
           row ++;  
         p_menu->items[i]->r_row = row;  
         if (p_menu->items[i]->col != 0)  
           col = p_menu->items[i]->col;  
         p_menu->items[i]->r_col = col;  
         moveto (row, col);  
         prints (p_menu->items[i]->text);  
         iflush ();  
       }  
     }  
     
   if (!menu_selectable)  
     return -1;  
658    
659    display_menu_cursor (p_menu, 1);          display_menu_cursor(p_menu, 1);
660    
661    return 0;          return 0;
662  }  }
663    
664  const char *  int display_current_menu(MENU_SET *p_menu_set)
 menu_control (MENU_SET * p_menu_set, int key)  
665  {  {
666    int i;          MENU *p_menu;
   MENU *p_menu;  
667    
668    if (p_menu_set->menu_count == 0)          p_menu = p_menu_set->p_menu_select[p_menu_set->menu_select_depth];
     return "";  
669    
670    p_menu = p_menu_set->p_menu_select[p_menu_set->menu_select_depth];          return display_menu(p_menu);
671    }
672    
673    int menu_control(MENU_SET *p_menu_set, int key)
674    {
675            int i;
676            MENU *p_menu;
677    
678    switch (key)          if (p_menu_set->menu_count == 0)
679      {          {
680      case CR:                  return 0;
     case KEY_RIGHT:  
       if (p_menu->items[p_menu->item_cur_pos]->submenu)  
         {  
           p_menu_set->menu_select_depth ++;  
           p_menu =  
             p_menu_set->p_menu_select[p_menu_set->menu_select_depth] =  
             get_menu (p_menu_set, p_menu->items[p_menu->item_cur_pos]->action);  
           if (display_menu (p_menu) != 0)  
             return menu_control (p_menu_set, KEY_LEFT);  
           break;  
         }  
       else  
         {  
           if (strcmp (p_menu->items[p_menu->item_cur_pos]->action, "..") == 0)  
             return menu_control (p_menu_set, KEY_LEFT);  
           else  
             return (p_menu->items[p_menu->item_cur_pos]->action);  
         }  
     case KEY_LEFT:  
       if (p_menu_set->menu_select_depth > 0)  
         {  
           p_menu_set->menu_select_depth --;  
           p_menu =  
             p_menu_set->p_menu_select[p_menu_set->menu_select_depth];  
           if (display_menu (p_menu) != 0)  
             return menu_control (p_menu_set, KEY_LEFT);  
           break;  
         }  
       else  
         {  
           display_menu_cursor (p_menu, 0);  
           p_menu->item_cur_pos = p_menu->item_count - 1;  
           while (!p_menu->items[p_menu->item_cur_pos]->display)  
             p_menu->item_cur_pos --;  
           display_menu_cursor (p_menu, 1);  
           break;  
         }  
     case KEY_UP:  
       display_menu_cursor (p_menu, 0);  
       do  
       {  
          p_menu->item_cur_pos --;  
          if (p_menu->item_cur_pos < 0)  
            p_menu->item_cur_pos = p_menu->item_count - 1;  
       } while (!p_menu->items[p_menu->item_cur_pos]->display);  
       display_menu_cursor (p_menu, 1);  
       break;  
     case KEY_DOWN:  
       display_menu_cursor (p_menu, 0);  
       do  
       {  
         p_menu->item_cur_pos++;  
         if (p_menu->item_cur_pos >= p_menu->item_count)  
           p_menu->item_cur_pos = 0;  
       } while (!p_menu->items[p_menu->item_cur_pos]->display);  
       display_menu_cursor (p_menu, 1);  
       break;  
     default:  
       for (i = 0; i < p_menu->item_count; i++)  
         {  
           if (toupper (key) == toupper (p_menu->items[i]->name[0]))  
             {  
               display_menu_cursor (p_menu, 0);  
               p_menu->item_cur_pos = i;  
               display_menu_cursor (p_menu, 1);  
               break;  
             }  
681          }          }
     }  
682    
683    return "";          p_menu = p_menu_set->p_menu_select[p_menu_set->menu_select_depth];
684    
685            switch (key)
686            {
687            case CR:
688            case KEY_RIGHT:
689                    if (p_menu->items[p_menu->item_cur_pos]->submenu)
690                    {
691                            if (strcmp(p_menu->items[p_menu->item_cur_pos]->action, "..") == 0)
692                            {
693                                    return menu_control(p_menu_set, KEY_LEFT);
694                            }
695                            p_menu_set->menu_select_depth++;
696                            p_menu = get_menu(p_menu_set, p_menu->items[p_menu->item_cur_pos]->action);
697                            p_menu_set->p_menu_select[p_menu_set->menu_select_depth] = p_menu;
698    
699                            if (display_menu(p_menu) != 0)
700                            {
701                                    return menu_control(p_menu_set, KEY_LEFT);
702                            }
703                    }
704                    else
705                    {
706                            return (exec_cmd(p_menu->items[p_menu->item_cur_pos]->action,
707                                                             p_menu->items[p_menu->item_cur_pos]->name));
708                    }
709                    break;
710            case KEY_LEFT:
711                    if (p_menu_set->menu_select_depth > 0)
712                    {
713                            p_menu_set->menu_select_depth--;
714                            if (display_current_menu(p_menu_set) != 0)
715                            {
716                                    return menu_control(p_menu_set, KEY_LEFT);
717                            }
718                    }
719                    else
720                    {
721                            display_menu_cursor(p_menu, 0);
722                            p_menu->item_cur_pos = p_menu->item_count - 1;
723                            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 ||
725                                                                                                     p_menu->items[p_menu->item_cur_pos]->level != 0))
726                            {
727                                    p_menu->item_cur_pos--;
728                            }
729                            display_menu_cursor(p_menu, 1);
730                    }
731                    break;
732            case KEY_UP:
733                    display_menu_cursor(p_menu, 0);
734                    do
735                    {
736                            p_menu->item_cur_pos--;
737                            if (p_menu->item_cur_pos < 0)
738                            {
739                                    p_menu->item_cur_pos = p_menu->item_count - 1;
740                            }
741                    } while (!p_menu->items[p_menu->item_cur_pos]->display);
742                    display_menu_cursor(p_menu, 1);
743                    break;
744            case KEY_DOWN:
745                    display_menu_cursor(p_menu, 0);
746                    do
747                    {
748                            p_menu->item_cur_pos++;
749                            if (p_menu->item_cur_pos >= p_menu->item_count)
750                            {
751                                    p_menu->item_cur_pos = 0;
752                            }
753                    } while (!p_menu->items[p_menu->item_cur_pos]->display);
754                    display_menu_cursor(p_menu, 1);
755                    break;
756            default:
757                    if (isalnum(key))
758                    {
759                            for (i = 0; i < p_menu->item_count; i++)
760                            {
761                                    if (toupper(key) == toupper(p_menu->items[i]->name[0]) &&
762                                            p_menu->items[i]->display)
763                                    {
764                                            display_menu_cursor(p_menu, 0);
765                                            p_menu->item_cur_pos = i;
766                                            display_menu_cursor(p_menu, 1);
767                                            return 0;
768                                    }
769                            }
770                    }
771                    break;
772            }
773    
774            return 0;
775    }
776    
777    void unload_menu(MENU_SET *p_menu_set)
778    {
779            MENU *p_menu;
780            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++)
789            {
790                    p_menu = p_menu_set->p_menu[i];
791                    for (j = 0; j < p_menu->item_count; j++)
792                    {
793                            free(p_menu->items[j]);
794                    }
795                    free(p_menu);
796            }
797    
798            p_menu_set->menu_count = 0;
799            p_menu_set->menu_select_depth = 0;
800    }
801    
802    int reload_menu(MENU_SET *p_menu_set)
803    {
804            int result;
805            char conf_file[FILE_PATH_LEN];
806    
807            strncpy(conf_file, p_menu_set->conf_file, sizeof(conf_file) - 1);
808            conf_file[sizeof(conf_file) - 1] = '\0';
809    
810            unload_menu(p_menu_set);
811            result = load_menu(p_menu_set, conf_file);
812    
813            return result;
814  }  }


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

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