/[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.15 by sysadm, Tue Mar 22 12:41:14 2005 UTC Revision 1.35 by sysadm, Sat May 10 02:52:17 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 "reg_ex.h"
21    #include "bbs_cmd.h"
22  #include "menu.h"  #include "menu.h"
23    #include "log.h"
24  #include "io.h"  #include "io.h"
25    #include "screen.h"
26  #include "common.h"  #include "common.h"
27    #include <string.h>
28  #include <stdio.h>  #include <stdio.h>
29  #include <ctype.h>  #include <ctype.h>
 #include <regex.h>  
30  #include <stdlib.h>  #include <stdlib.h>
31    
32    #define MENU_SCREEN_PATH_PREFIX "var/MENU_SCR_"
33    #define MENU_CONF_DELIM_WITH_SPACE " ,\t\r\n"
34    #define MENU_CONF_DELIM_WITHOUT_SPACE "\r\n"
35    
36  MENU_SET bbs_menu;  MENU_SET bbs_menu;
37    
38  int  int load_menu(MENU_SET *p_menu_set, const char *conf_file)
 load_menu (MENU_SET * p_menu_set, const char *conf_file)  
39  {  {
40    FILE *fin, *fout;          FILE *fin, *fout;
41    int i = 0, j;          int fin_line = 0;
42    char buffer[256], screen_filename[256], temp[256];          int i = 0;
43    regmatch_t pmatch[10];          int j = 0;
44            char buffer[LINE_BUFFER_LEN];
45    if ((fin = fopen (conf_file, "r")) == NULL)          char temp[LINE_BUFFER_LEN];
46      {          char screen_filename[FILE_PATH_LEN];
47        log_error ("Open %s failed", conf_file);          char *p = NULL;
48        return -1;          char *q = NULL;
49      }          char *saveptr = NULL;
50            MENU *p_menu = NULL;
51    while (fgets (buffer, 255, fin))          MENU_ITEM *p_item = NULL;
52      {  
53        switch (buffer[0])          p_menu_set->menu_count = 0;
54          {  
55          case '#':          if ((fin = fopen(conf_file, "r")) == NULL)
56            break;          {
57          case '%':                  log_error("Open %s failed", conf_file);
58            if (ireg ("^%S_([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0)                  return -2;
59              {          }
60                strncpy (temp, buffer + pmatch[1].rm_so,  
61                         pmatch[1].rm_eo - pmatch[1].rm_so);          strncpy(p_menu_set->conf_file, conf_file, sizeof(p_menu_set->conf_file) - 1);
62                temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';          p_menu_set->conf_file[sizeof(p_menu_set->conf_file) - 1] = '\0';
63                sprintf (screen_filename, "%sMENU_SCR_%s", app_temp_dir, temp);  
64            while (fgets(buffer, sizeof(buffer), fin))
65                if ((fout = fopen (screen_filename, "w")) == NULL)          {
66                  {                  fin_line++;
67                    log_error ("Open %s failed", screen_filename);  
68                    return -2;                  p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
69                  }                  if (p == NULL) // Blank line
70                    {
71                while (fgets (buffer, 255, fin))                          continue;
72                  {                  }
73                    if (buffer[0] != '%')  
74                      fputs (buffer, fout);                  if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
75                    else                  {
76                      break;                          continue;
77                  }                  }
78    
79                fclose (fout);                  if (*p == '%')
80                break;                  {
81              }                          p++;
82    
83            if (ireg ("^%menu ([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0)                          if (strcmp(p, "menu") == 0) // BEGIN of sub-menu
84              {                          {
85                p_menu_set->p_menu[i] = malloc (sizeof (MENU));                                  if (p_menu != NULL)
86                p_menu_set->p_menu[i]->title.show = 0;                                  {
87                p_menu_set->p_menu[i]->screen.show = 0;                                          log_error("Incomplete menu definition in menu config line %d\n", fin_line);
88                                            return -1;
89                strncpy (p_menu_set->p_menu[i]->name,                                  }
90                         buffer + pmatch[1].rm_so,                                  p_menu = (MENU *)malloc(sizeof(MENU));
91                         pmatch[1].rm_eo - pmatch[1].rm_so);                                  if (p_menu == NULL)
92                p_menu_set->p_menu[i]->name[pmatch[1].rm_eo - pmatch[1].rm_so] =                                  {
93                  '\0';                                          log_error("Unable to allocate memory for menu\n");
94                                            return -3;
95                j = 0;                                  }
96                                    p_menu_set->p_menu[i] = p_menu;
97                while (fgets (buffer, 255, fin))                                  i++;
98                  {                                  p_menu_set->menu_count = i;
99                    if (buffer[0] == '#')  
100                      {                                  j = 0; // Menu item counter
101                        continue;                                  p_menu->item_count = 0;
102                      }                                  p_menu->item_cur_pos = 0;
103                    if (buffer[0] == '%')                                  p_menu->title.show = 0;
104                      {                                  p_menu->screen.show = 0;
105                        p_menu_set->p_menu[i]->item_count = j;  
106                        p_menu_set->p_menu[i]->item_cur_pos = 0;                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
107                        i++;                                  if (q == NULL)
108                        break;                                  {
109                      }                                          log_error("Error menu name in menu config line %d\n", fin_line);
110                    if (ireg ("^!([A-Za-z0-9_.]+)[[:space:]]*([0-9]+),"                                          return -1;
111                              "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+),"                                  }
112                              "[[:space:]]*([0-9]+),[[:space:]]*\"([A-Za-z0-9_]+)\","                                  p = q;
113                              "[[:space:]]*\"([^\"]+)\"",                                  while (isalnum(*q) || *q == '_')
114                              buffer, 8, pmatch) == 0)                                  {
115                      {                                          q++;
116                        p_menu_set->p_menu[i]->items[j] =                                  }
117                          malloc (sizeof (MENU_ITEM));                                  if (*q != '\0')
118                        p_menu_set->p_menu[i]->items[j]->submenu = 1;                                  {
119                        strncpy (p_menu_set->p_menu[i]->items[j]->action,                                          log_error("Error menu name in menu config line %d\n", fin_line);
120                                 buffer + pmatch[1].rm_so,                                          return -1;
121                                 pmatch[1].rm_eo - pmatch[1].rm_so);                                  }
122                        p_menu_set->p_menu[i]->items[j]->action[pmatch[1].  
123                                                                rm_eo -                                  if (q - p > sizeof(p_menu->name) - 1)
124                                                                pmatch[1].                                  {
125                                                                rm_so] = '\0';                                          log_error("Too longer menu name in menu config line %d\n", fin_line);
126                        strncpy (temp, buffer + pmatch[2].rm_so,                                          return -1;
127                                 pmatch[2].rm_eo - pmatch[2].rm_so);                                  }
128                        temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                  strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);
129                        p_menu_set->p_menu[i]->items[j]->row = atoi (temp);                                  p_menu->name[sizeof(p_menu->name) - 1] = '\0';
130                        strncpy (temp,  
131                                 buffer + pmatch[3].rm_so,                                  // Check syntax
132                                 pmatch[3].rm_eo - pmatch[3].rm_so);                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
133                        temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                  if (q != NULL)
134                        p_menu_set->p_menu[i]->items[j]->col = atoi (temp);                                  {
135                        strncpy (temp,                                          log_error("Unknown extra content in menu config line %d\n", fin_line);
136                                 buffer + pmatch[4].rm_so,                                          return -1;
137                                 pmatch[4].rm_eo - pmatch[4].rm_so);                                  }
138                        temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';  
139                        p_menu_set->p_menu[i]->items[j]->priv = atoi (temp);                                  while (fgets(buffer, sizeof(buffer), fin))
140                        strncpy (temp,                                  {
141                                 buffer + pmatch[5].rm_so,                                          fin_line++;
142                                 pmatch[5].rm_eo - pmatch[5].rm_so);  
143                        temp[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0';                                          p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
144                        p_menu_set->p_menu[i]->items[j]->level = atoi (temp);                                          if (p == NULL) // Blank line
145                        strncpy (p_menu_set->p_menu[i]->items[j]->name,                                          {
146                                 buffer + pmatch[6].rm_so,                                                  continue;
147                                 pmatch[6].rm_eo - pmatch[6].rm_so);                                          }
148                        p_menu_set->p_menu[i]->items[j]->name[pmatch[6].rm_eo -  
149                                                              pmatch[6].rm_so] =                                          if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
150                          '\0';                                          {
151                        strncpy (p_menu_set->p_menu[i]->items[j]->text,                                                  continue;
152                                 buffer + pmatch[7].rm_so,                                          }
153                                 pmatch[7].rm_eo - pmatch[7].rm_so);  
154                        p_menu_set->p_menu[i]->items[j]->text[pmatch[7].rm_eo -                                          if (*p == '%') // END of sub-menu
155                                                              pmatch[7].rm_so] =                                          {
156                          '\0';                                                  p_menu = NULL;
157                        j++;                                                  break;
158                        continue;                                          }
159                      }                                          else if (*p == '!' || *p == '@')
160                    if (ireg ("^@([A-Za-z0-9_]+)[[:space:]]*([0-9]+),"                                          {
161                              "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+),"                                                  // BEGIN of menu item
162                              "[[:space:]]*([0-9]+),[[:space:]]*\"([A-Za-z0-9_]+)\","                                                  p_item = (MENU_ITEM *)malloc(sizeof(MENU_ITEM));
163                              "[[:space:]]*\"([^\"]+)\"",                                                  if (p_item == NULL)
164                              buffer, 8, pmatch) == 0)                                                  {
165                      {                                                          log_error("Unable to allocate memory for menu item\n");
166                        p_menu_set->p_menu[i]->items[j] =                                                          return -3;
167                          malloc (sizeof (MENU_ITEM));                                                  }
168                        p_menu_set->p_menu[i]->items[j]->submenu = 0;                                                  p_menu->items[j] = p_item;
169                        strncpy (p_menu_set->p_menu[i]->items[j]->action,                                                  j++;
170                                 buffer + pmatch[1].rm_so,                                                  p_menu->item_count = j;
171                                 pmatch[1].rm_eo - pmatch[1].rm_so);  
172                        p_menu_set->p_menu[i]->items[j]->action[pmatch[1].                                                  p_item->submenu = (*p == '!' ? 1 : 0);
173                                                                rm_eo -  
174                                                                pmatch[1].                                                  // Menu item action
175                                                                rm_so] = '\0';                                                  p++;
176                        strncpy (temp, buffer + pmatch[2].rm_so,                                                  if (strcmp(p, "..") == 0) // Return to parent menu
177                                 pmatch[2].rm_eo - pmatch[2].rm_so);                                                  {
178                        temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                          q = p + 2; // strlen("..")
179                        p_menu_set->p_menu[i]->items[j]->row = atoi (temp);                                                  }
180                        strncpy (temp,                                                  else
181                                 buffer + pmatch[3].rm_so,                                                  {
182                                 pmatch[3].rm_eo - pmatch[3].rm_so);                                                          q = p;
183                        temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                                          while (isalnum(*q) || *q == '_')
184                        p_menu_set->p_menu[i]->items[j]->col = atoi (temp);                                                          {
185                        strncpy (temp,                                                                  q++;
186                                 buffer + pmatch[4].rm_so,                                                          }
187                                 pmatch[4].rm_eo - pmatch[4].rm_so);                                                          if (*q != '\0')
188                        temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';                                                          {
189                        p_menu_set->p_menu[i]->items[j]->priv = atoi (temp);                                                                  log_error("Error menu item action in menu config line %d\n", fin_line);
190                        strncpy (temp,                                                                  return -1;
191                                 buffer + pmatch[5].rm_so,                                                          }
192                                 pmatch[5].rm_eo - pmatch[5].rm_so);                                                  }
193                        temp[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0';  
194                        p_menu_set->p_menu[i]->items[j]->level = atoi (temp);                                                  if (q - p > sizeof(p_item->action) - 1)
195                        strncpy (p_menu_set->p_menu[i]->items[j]->name,                                                  {
196                                 buffer + pmatch[6].rm_so,                                                          log_error("Too longer menu action in menu config line %d\n", fin_line);
197                                 pmatch[6].rm_eo - pmatch[6].rm_so);                                                          return -1;
198                        p_menu_set->p_menu[i]->items[j]->name[pmatch[6].rm_eo -                                                  }
199                                                              pmatch[6].rm_so] =                                                  strncpy(p_item->action, p, sizeof(p_item->action) - 1);
200                          '\0';                                                  p_item->action[sizeof(p_item->action) - 1] = '\0';
201                        strncpy (p_menu_set->p_menu[i]->items[j]->text,  
202                                 buffer + pmatch[7].rm_so,                                                  // Menu item row
203                                 pmatch[7].rm_eo - pmatch[7].rm_so);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
204                        p_menu_set->p_menu[i]->items[j]->text[pmatch[7].rm_eo -                                                  if (q == NULL)
205                                                              pmatch[7].rm_so] =                                                  {
206                          '\0';                                                          log_error("Error menu item row in menu config line %d\n", fin_line);
207                        j++;                                                          return -1;
208                        continue;                                                  }
209                      }                                                  p = q;
210                    if (ireg ("^title[[:space:]]*([0-9]+),"                                                  while (isdigit(*q))
211                              "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\"",                                                  {
212                              buffer, 4, pmatch) == 0)                                                          q++;
213                      {                                                  }
214                        p_menu_set->p_menu[i]->title.show = 1;                                                  if (*q != '\0')
215                        strncpy (temp,                                                  {
216                                 buffer + pmatch[1].rm_so,                                                          log_error("Error menu item row in menu config line %d\n", fin_line);
217                                 pmatch[1].rm_eo - pmatch[1].rm_so);                                                          return -1;
218                        temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                                  }
219                        p_menu_set->p_menu[i]->title.row = atoi (temp);                                                  p_item->row = atoi(p);
220                        strncpy (temp,  
221                                 buffer + pmatch[2].rm_so,                                                  // Menu item col
222                                 pmatch[2].rm_eo - pmatch[2].rm_so);                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
223                        temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                  if (q == NULL)
224                        p_menu_set->p_menu[i]->title.col = atoi (temp);                                                  {
225                        strncpy (p_menu_set->p_menu[i]->title.text,                                                          log_error("Error menu item col in menu config line %d\n", fin_line);
226                                 buffer + pmatch[3].rm_so,                                                          return -1;
227                                 pmatch[3].rm_eo - pmatch[3].rm_so);                                                  }
228                        p_menu_set->p_menu[i]->title.text[pmatch[3].rm_eo -                                                  p = q;
229                                                          pmatch[3].rm_so] =                                                  while (isdigit(*q))
230                          '\0';                                                  {
231                        continue;                                                          q++;
232                      }                                                  }
233                    if (ireg ("^screen[[:space:]]*([0-9]+),"                                                  if (*q != '\0')
234                              "[[:space:]]*([0-9]+),[[:space:]]*S_([A-Za-z0-9_]+)",                                                  {
235                              buffer, 4, pmatch) == 0)                                                          log_error("Error menu item col in menu config line %d\n", fin_line);
236                      {                                                          return -1;
237                        p_menu_set->p_menu[i]->screen.show = 1;                                                  }
238                        strncpy (temp,                                                  p_item->col = atoi(p);
239                                 buffer + pmatch[1].rm_so,  
240                                 pmatch[1].rm_eo - pmatch[1].rm_so);                                                  // Menu item priv
241                        temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
242                        p_menu_set->p_menu[i]->screen.row = atoi (temp);                                                  if (q == NULL)
243                        strncpy (temp,                                                  {
244                                 buffer + pmatch[2].rm_so,                                                          log_error("Error menu item priv in menu config line %d\n", fin_line);
245                                 pmatch[2].rm_eo - pmatch[2].rm_so);                                                          return -1;
246                        temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                  }
247                        p_menu_set->p_menu[i]->screen.col = atoi (temp);                                                  p = q;
248                        strncpy (temp,                                                  while (isdigit(*q))
249                                 buffer + pmatch[3].rm_so,                                                  {
250                                 pmatch[3].rm_eo - pmatch[3].rm_so);                                                          q++;
251                        temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                                  }
252                        sprintf (p_menu_set->p_menu[i]->screen.filename,                                                  if (*q != '\0')
253                                 "%sMENU_SCR_%s", app_temp_dir, temp);                                                  {
254                        continue;                                                          log_error("Error menu item priv in menu config line %d\n", fin_line);
255                      }                                                          return -1;
256                  }                                                  }
257              }                                                  p_item->priv = atoi(p);
258            break;  
259          }                                                  // Menu item level
260      }                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
261    fclose (fin);                                                  if (q == NULL)
262                                                    {
263    p_menu_set->menu_count = i;                                                          log_error("Error menu item level in menu config line %d\n", fin_line);
264    p_menu_set->menu_select_depth = 0;                                                          return -1;
265    p_menu_set->p_menu_select[p_menu_set->menu_select_depth]                                                  }
266      = (i == 0 ? NULL : p_menu_set->p_menu[0]);                                                  p = q;
267                                                    while (isdigit(*q))
268                                                    {
269                                                            q++;
270                                                    }
271                                                    if (*q != '\0')
272                                                    {
273                                                            log_error("Error menu item level in menu config line %d\n", fin_line);
274                                                            return -1;
275                                                    }
276                                                    p_item->level = atoi(p);
277    
278                                                    // Menu item name
279                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
280                                                    if (q == NULL || *q != '\"')
281                                                    {
282                                                            log_error("Error menu item name in menu config line %d\n", fin_line);
283                                                            return -1;
284                                                    }
285                                                    q++;
286                                                    p = q;
287                                                    while (*q != '\0' && *q != '\"')
288                                                    {
289                                                            q++;
290                                                    }
291                                                    if (*q != '\"' || *(q + 1) != '\0')
292                                                    {
293                                                            log_error("Error menu item name in menu config line %d\n", fin_line);
294                                                            return -1;
295                                                    }
296                                                    *q = '\0';
297    
298                                                    if (q - p > sizeof(p_item->name) - 1)
299                                                    {
300                                                            log_error("Too longer menu name in menu config line %d\n", fin_line);
301                                                            return -1;
302                                                    }
303                                                    strncpy(p_item->name, p, sizeof(p_item->name) - 1);
304                                                    p_item->name[sizeof(p_item->name) - 1] = '\0';
305    
306                                                    // Menu item text
307                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
308                                                    if (q == NULL || (q = strchr(q, '\"')) == NULL)
309                                                    {
310                                                            log_error("Error menu item text in menu config line %d\n", fin_line);
311                                                            return -1;
312                                                    }
313                                                    q++;
314                                                    p = q;
315                                                    while (*q != '\0' && *q != '\"')
316                                                    {
317                                                            q++;
318                                                    }
319                                                    if (*q != '\"')
320                                                    {
321                                                            log_error("Error menu item text in menu config line %d\n", fin_line);
322                                                            return -1;
323                                                    }
324                                                    *q = '\0';
325    
326                                                    if (q - p > sizeof(p_item->text) - 1)
327                                                    {
328                                                            log_error("Too longer menu item text in menu config line %d\n", fin_line);
329                                                            return -1;
330                                                    }
331                                                    strncpy(p_item->text, p, sizeof(p_item->text) - 1);
332                                                    p_item->text[sizeof(p_item->text) - 1] = '\0';
333    
334                                                    // Check syntax
335                                                    q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
336                                                    if (q != NULL)
337                                                    {
338                                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
339                                                            return -1;
340                                                    }
341                                            }
342                                            else if (strcmp(p, "title") == 0)
343                                            {
344                                                    p_menu->title.show = 1;
345    
346                                                    // Menu title row
347                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
348                                                    if (q == NULL)
349                                                    {
350                                                            log_error("Error menu title row in menu config line %d\n", fin_line);
351                                                            return -1;
352                                                    }
353                                                    p = q;
354                                                    while (isdigit(*q))
355                                                    {
356                                                            q++;
357                                                    }
358                                                    if (*q != '\0')
359                                                    {
360                                                            log_error("Error menu title row in menu config line %d\n", fin_line);
361                                                            return -1;
362                                                    }
363                                                    p_menu->title.row = atoi(p);
364    
365                                                    // Menu title col
366                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
367                                                    if (q == NULL)
368                                                    {
369                                                            log_error("Error menu title col in menu config line %d\n", fin_line);
370                                                            return -1;
371                                                    }
372                                                    p = q;
373                                                    while (isdigit(*q))
374                                                    {
375                                                            q++;
376                                                    }
377                                                    if (*q != '\0')
378                                                    {
379                                                            log_error("Error menu title col in menu config line %d\n", fin_line);
380                                                            return -1;
381                                                    }
382                                                    p_menu->title.col = atoi(p);
383    
384                                                    // Menu title text
385                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITHOUT_SPACE, &saveptr);
386                                                    if (q == NULL || (q = strchr(q, '\"')) == NULL)
387                                                    {
388                                                            log_error("Error menu title text in menu config line %d\n", fin_line);
389                                                            return -1;
390                                                    }
391                                                    q++;
392                                                    p = q;
393                                                    while (*q != '\0' && *q != '\"')
394                                                    {
395                                                            q++;
396                                                    }
397                                                    if (*q != '\"')
398                                                    {
399                                                            log_error("Error menu title text in menu config line %d\n", fin_line);
400                                                            return -1;
401                                                    }
402                                                    *q = '\0';
403    
404                                                    if (q - p > sizeof(p_item->text) - 1)
405                                                    {
406                                                            log_error("Too longer menu title text in menu config line %d\n", fin_line);
407                                                            return -1;
408                                                    }
409                                                    strncpy(p_menu->title.text, p, sizeof(p_menu->title.text) - 1);
410                                                    p_menu->title.text[sizeof(p_menu->title.text) - 1] = '\0';
411    
412                                                    // Check syntax
413                                                    q = strtok_r(q + 1, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
414                                                    if (q != NULL)
415                                                    {
416                                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
417                                                            return -1;
418                                                    }
419                                            }
420                                            else if (strcmp(p, "screen") == 0)
421                                            {
422                                                    p_menu->screen.show = 1;
423    
424                                                    // Menu screen row
425                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
426                                                    if (q == NULL)
427                                                    {
428                                                            log_error("Error menu screen row in menu config line %d\n", fin_line);
429                                                            return -1;
430                                                    }
431                                                    p = q;
432                                                    while (isdigit(*q))
433                                                    {
434                                                            q++;
435                                                    }
436                                                    if (*q != '\0')
437                                                    {
438                                                            log_error("Error menu screen row in menu config line %d\n", fin_line);
439                                                            return -1;
440                                                    }
441                                                    p_menu->screen.row = atoi(p);
442    
443                                                    // Menu screen col
444                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
445                                                    if (q == NULL)
446                                                    {
447                                                            log_error("Error menu screen col in menu config line %d\n", fin_line);
448                                                            return -1;
449                                                    }
450                                                    p = q;
451                                                    while (isdigit(*q))
452                                                    {
453                                                            q++;
454                                                    }
455                                                    if (*q != '\0')
456                                                    {
457                                                            log_error("Error menu screen col in menu config line %d\n", fin_line);
458                                                            return -1;
459                                                    }
460                                                    p_menu->screen.col = atoi(p);
461    
462                                                    // Menu screen name
463                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
464                                                    if (q == NULL)
465                                                    {
466                                                            log_error("Error menu screen name in menu config line %d\n", fin_line);
467                                                            return -1;
468                                                    }
469                                                    p = q;
470                                                    while (isalnum(*q) || *q == '_')
471                                                    {
472                                                            q++;
473                                                    }
474                                                    if (*q != '\0')
475                                                    {
476                                                            log_error("Error menu screen name in menu config line %d\n", fin_line);
477                                                            return -1;
478                                                    }
479    
480                                                    snprintf(p_menu->screen.filename, sizeof(p_menu->screen.filename), "%s%s", MENU_SCREEN_PATH_PREFIX, p);
481    
482                                                    // Check syntax
483                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
484                                                    if (q != NULL)
485                                                    {
486                                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
487                                                            return -1;
488                                                    }
489                                            }
490                                    }
491                            }
492                            else // BEGIN of menu screen
493                            {
494                                    q = p;
495                                    while (isalnum(*q) || *q == '_')
496                                    {
497                                            q++;
498                                    }
499                                    if (*q != '\0')
500                                    {
501                                            log_error("Error menu screen name in menu config line %d\n", fin_line);
502                                            return -1;
503                                    }
504    
505                                    snprintf(screen_filename, sizeof(screen_filename), "%s%s", MENU_SCREEN_PATH_PREFIX, p);
506    
507                                    // Check syntax
508                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
509                                    if (q != NULL)
510                                    {
511                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
512                                            return -1;
513                                    }
514    
515                                    if ((fout = fopen(screen_filename, "w")) == NULL)
516                                    {
517                                            log_error("Open %s failed", screen_filename);
518                                            return -2;
519                                    }
520    
521                                    while (fgets(buffer, sizeof(buffer), fin))
522                                    {
523                                            fin_line++;
524    
525                                            strncpy(temp, buffer, sizeof(temp)); // Duplicate line for strtok_r
526                                            p = strtok_r(temp, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
527                                            if (p != NULL && *p == '%') // END of menu screen
528                                            {
529                                                    break;
530                                            }
531    
532                                            if (fputs(buffer, fout) < 0)
533                                            {
534                                                    log_error("Write %s failed", screen_filename);
535                                                    return -2;
536                                            }
537                                    }
538    
539                                    fclose(fout);
540                            }
541                    }
542                    else // Invalid prefix
543                    {
544                            log_error("Error in menu config line %d\n", fin_line);
545                            return -1;
546                    }
547            }
548            fclose(fin);
549    
550    return 0;          p_menu_set->menu_count = i;
551            p_menu_set->menu_select_depth = 0;
552            p_menu_set->p_menu_select[p_menu_set->menu_select_depth] = (i == 0 ? NULL : p_menu_set->p_menu[0]);
553    
554            return 0;
555  }  }
556    
557  MENU *  MENU *get_menu(MENU_SET *p_menu_set, const char *menu_name)
 get_menu (MENU_SET * p_menu_set, const char *menu_name)  
558  {  {
559    int i;          int i;
560    
561    for (i = 0; i < p_menu_set->menu_count; i++)          for (i = 0; i < p_menu_set->menu_count; i++)
     {  
       if (strcmp (p_menu_set->p_menu[i]->name, menu_name) == 0)  
562          {          {
563            return p_menu_set->p_menu[i];                  if (strcmp(p_menu_set->p_menu[i]->name, menu_name) == 0)
564                    {
565                            return p_menu_set->p_menu[i];
566                    }
567          }          }
     }  
568    
569    return NULL;          return NULL;
570  }  }
571    
572  void  static void display_menu_cursor(MENU *p_menu, int show)
 display_menu_cursor (MENU * p_menu, int show)  
573  {  {
574    moveto ((p_menu->items[p_menu->item_cur_pos])->r_row,          moveto((p_menu->items[p_menu->item_cur_pos])->r_row,
575            (p_menu->items[p_menu->item_cur_pos])->r_col - 2);                     (p_menu->items[p_menu->item_cur_pos])->r_col - 2);
576    prints (show ? ">" : " ");          outc(show ? '>' : ' ');
577    iflush ();          iflush();
578  }  }
579    
580  int  int display_menu(MENU *p_menu)
 display_menu (MENU * p_menu)  
581  {  {
582    int i, row, col, menu_selectable = 0;          int row = 0;
583            int col = 0;
584    if (p_menu == NULL)          int menu_selectable = 0;
     return -1;  
585    
586    if (p_menu->title.show)          if (p_menu == NULL)
587      show_top (p_menu->title.text);          {
588                    return -1;
589            }
590    
591    if (p_menu->screen.show)          if (p_menu->item_cur_pos > 0 &&
592      {                  checkpriv(&BBS_priv, 0, p_menu->items[p_menu->item_cur_pos]->priv) != 0 &&
593        moveto (p_menu->screen.row, p_menu->screen.col);                  checklevel(&BBS_priv, p_menu->items[p_menu->item_cur_pos]->level) != 0)
594        if (display_file (p_menu->screen.filename) != 0)          {
595          log_error ("Display menu screen <%s> failed!\n",                  menu_selectable = 1;
596                     p_menu->screen.filename);          }
     }  
597    
598    row = p_menu->items[0]->row;          if (p_menu->title.show)
599    col = p_menu->items[0]->col;          {
600                    show_top(p_menu->title.text);
601            }
602    
603    for (i = 0; i < p_menu->item_count; i++)          if (p_menu->screen.show)
     {  
       if (checkpriv (&BBS_priv, 0, p_menu->items[i]->priv) == 0  
           || checklevel (&BBS_priv, p_menu->items[i]->level) == 0)  
604          {          {
605            p_menu->items[i]->display = 0;                  moveto(p_menu->screen.row, p_menu->screen.col);
606                    if (display_file(p_menu->screen.filename) != 0)
607                    {
608                            log_error("Display menu screen <%s> failed!\n",
609                                              p_menu->screen.filename);
610                    }
611          }          }
612        else  
613            for (int i = 0; i < p_menu->item_count; i++)
614          {          {
615            p_menu->items[i]->display = 1;                  if (p_menu->items[i]->row != 0)
616                    {
617                            row = p_menu->items[i]->row;
618                    }
619                    if (p_menu->items[i]->col != 0)
620                    {
621                            col = p_menu->items[i]->col;
622                    }
623    
624                    if (checkpriv(&BBS_priv, 0, p_menu->items[i]->priv) == 0 || checklevel(&BBS_priv, p_menu->items[i]->level) == 0)
625                    {
626                            p_menu->items[i]->display = 0;
627                            p_menu->items[i]->r_row = 0;
628                            p_menu->items[i]->r_col = 0;
629                    }
630                    else
631                    {
632                            p_menu->items[i]->display = 1;
633    
634            menu_selectable = 1;                          if (!menu_selectable)
635                            {
636                                    p_menu->item_cur_pos = i;
637                                    menu_selectable = 1;
638                            }
639    
640            if (p_menu->items[i]->row != 0)                          p_menu->items[i]->r_row = row;
641              row = p_menu->items[i]->row;                          p_menu->items[i]->r_col = col;
642            else  
643              row++;                          moveto(row, col);
644            p_menu->items[i]->r_row = row;                          prints("%s", p_menu->items[i]->text);
645            if (p_menu->items[i]->col != 0)  
646              col = p_menu->items[i]->col;                          row++;
647            p_menu->items[i]->r_col = col;                  }
           moveto (row, col);  
           prints (p_menu->items[i]->text);  
           iflush ();  
648          }          }
     }  
649    
650    if (!menu_selectable)          if (!menu_selectable)
651      return -1;          {
652                    return -1;
653            }
654    
655    display_menu_cursor (p_menu, 1);          display_menu_cursor(p_menu, 1);
656    
657    return 0;          return 0;
658  }  }
659    
660  int  int display_current_menu(MENU_SET *p_menu_set)
 display_current_menu (MENU_SET * p_menu_set)  
661  {  {
662    MENU *p_menu;          MENU *p_menu;
663    
664    p_menu = p_menu_set->p_menu_select[p_menu_set->menu_select_depth];          p_menu = p_menu_set->p_menu_select[p_menu_set->menu_select_depth];
665    
666    return display_menu (p_menu);          return display_menu(p_menu);
667  }  }
668    
669  const char *  int menu_control(MENU_SET *p_menu_set, int key)
 menu_control (MENU_SET * p_menu_set, int key)  
670  {  {
671    int i;          int i;
672    MENU *p_menu;          MENU *p_menu;
673    
674    if (p_menu_set->menu_count == 0)          if (p_menu_set->menu_count == 0)
675      return "";          {
676                    return 0;
677            }
678    
679    p_menu = p_menu_set->p_menu_select[p_menu_set->menu_select_depth];          p_menu = p_menu_set->p_menu_select[p_menu_set->menu_select_depth];
680    
681    switch (key)          switch (key)
682      {          {
683      case CR:          case CR:
684      case KEY_RIGHT:                  igetch(1); // Cleanup remaining '\n' in the buffer
685        if (p_menu->items[p_menu->item_cur_pos]->submenu)          case KEY_RIGHT:
686          {                  if (p_menu->items[p_menu->item_cur_pos]->submenu)
687            if (strcmp (p_menu->items[p_menu->item_cur_pos]->action, "..") == 0)                  {
688              return menu_control (p_menu_set, KEY_LEFT);                          if (strcmp(p_menu->items[p_menu->item_cur_pos]->action, "..") == 0)
689            p_menu_set->menu_select_depth++;                          {
690            p_menu =                                  return menu_control(p_menu_set, KEY_LEFT);
691              p_menu_set->p_menu_select[p_menu_set->menu_select_depth] =                          }
692              get_menu (p_menu_set,                          p_menu_set->menu_select_depth++;
693                        p_menu->items[p_menu->item_cur_pos]->action);                          p_menu = get_menu(p_menu_set, p_menu->items[p_menu->item_cur_pos]->action);
694            if (display_menu (p_menu) != 0)                          p_menu_set->p_menu_select[p_menu_set->menu_select_depth] = p_menu;
695              return menu_control (p_menu_set, KEY_LEFT);  
696            break;                          if (display_menu(p_menu) != 0)
697          }                          {
698        else                                  return menu_control(p_menu_set, KEY_LEFT);
699          {                          }
700            return (p_menu->items[p_menu->item_cur_pos]->action);                          break;
701          }                  }
702      case KEY_LEFT:                  else
703        if (p_menu_set->menu_select_depth > 0)                  {
704          {                          return (exec_cmd(p_menu->items[p_menu->item_cur_pos]->action,
705            p_menu_set->menu_select_depth--;                                                           p_menu->items[p_menu->item_cur_pos]->name));
706            if (display_current_menu (p_menu_set) != 0)                  }
707              return menu_control (p_menu_set, KEY_LEFT);          case KEY_LEFT:
708            break;                  if (p_menu_set->menu_select_depth > 0)
709          }                  {
710        else                          p_menu_set->menu_select_depth--;
711          {                          if (display_current_menu(p_menu_set) != 0)
712            display_menu_cursor (p_menu, 0);                          {
713            p_menu->item_cur_pos = p_menu->item_count - 1;                                  return menu_control(p_menu_set, KEY_LEFT);
714            while (!p_menu->items[p_menu->item_cur_pos]->display                          }
715                   || p_menu->items[p_menu->item_cur_pos]->priv != 0                          break;
716                   || p_menu->items[p_menu->item_cur_pos]->level != 0)                  }
717              p_menu->item_cur_pos--;                  else
718            display_menu_cursor (p_menu, 1);                  {
719            break;                          display_menu_cursor(p_menu, 0);
720          }                          p_menu->item_cur_pos = p_menu->item_count - 1;
721      case KEY_UP:                          while (p_menu->item_cur_pos >= 0 && !p_menu->items[p_menu->item_cur_pos]->display)
722        display_menu_cursor (p_menu, 0);                          {
723        do                                  p_menu->item_cur_pos--;
724          {                          }
725            p_menu->item_cur_pos--;                          display_menu_cursor(p_menu, 1);
726            if (p_menu->item_cur_pos < 0)                          break;
727              p_menu->item_cur_pos = p_menu->item_count - 1;                  }
728          }          case KEY_UP:
729        while (!p_menu->items[p_menu->item_cur_pos]->display);                  display_menu_cursor(p_menu, 0);
730        display_menu_cursor (p_menu, 1);                  do
731        break;                  {
732      case KEY_DOWN:                          p_menu->item_cur_pos--;
733        display_menu_cursor (p_menu, 0);                          if (p_menu->item_cur_pos < 0)
734        do                          {
735          {                                  p_menu->item_cur_pos = p_menu->item_count - 1;
736            p_menu->item_cur_pos++;                          }
737            if (p_menu->item_cur_pos >= p_menu->item_count)                  } while (!p_menu->items[p_menu->item_cur_pos]->display);
738              p_menu->item_cur_pos = 0;                  display_menu_cursor(p_menu, 1);
739          }                  break;
740        while (!p_menu->items[p_menu->item_cur_pos]->display);          case KEY_DOWN:
741        display_menu_cursor (p_menu, 1);                  display_menu_cursor(p_menu, 0);
742        break;                  do
743      default:                  {
744        for (i = 0; i < p_menu->item_count; i++)                          p_menu->item_cur_pos++;
745          {                          if (p_menu->item_cur_pos >= p_menu->item_count)
746            if (key == p_menu->items[i]->name[0])                          {
747              {                                  p_menu->item_cur_pos = 0;
748                display_menu_cursor (p_menu, 0);                          }
749                p_menu->item_cur_pos = i;                  } while (!p_menu->items[p_menu->item_cur_pos]->display);
750                display_menu_cursor (p_menu, 1);                  display_menu_cursor(p_menu, 1);
751                return "";                  break;
752              }          default:
753          }                  if (isalnum(key))
754        if (isalpha (key))                  {
755        {                          for (i = 0; i < p_menu->item_count; i++)
756        for (i = 0; i < p_menu->item_count; i++)                          {
757          {                                  if (toupper(key) == toupper(p_menu->items[i]->name[0]) &&
758            if (toupper (key) == toupper (p_menu->items[i]->name[0]))                                          p_menu->items[i]->display)
759              {                                  {
760                display_menu_cursor (p_menu, 0);                                          display_menu_cursor(p_menu, 0);
761                p_menu->item_cur_pos = i;                                          p_menu->item_cur_pos = i;
762                display_menu_cursor (p_menu, 1);                                          display_menu_cursor(p_menu, 1);
763                return "";                                          return 0;
764              }                                  }
765                            }
766                    }
767            }
768    
769            return 0;
770    }
771    
772    void unload_menu(MENU_SET *p_menu_set)
773    {
774            MENU *p_menu;
775            int i, j;
776    
777            for (i = 0; i < p_menu_set->menu_count; i++)
778            {
779                    p_menu = p_menu_set->p_menu[i];
780                    for (j = 0; j < p_menu->item_count; j++)
781                    {
782                            free(p_menu->items[j]);
783                    }
784                    free(p_menu);
785          }          }
       }  
     }  
786    
787    return "";          p_menu_set->menu_count = 0;
788            p_menu_set->menu_select_depth = 0;
789  }  }
790    
791  void  int reload_menu(MENU_SET *p_menu_set)
 unload_menu (MENU_SET * p_menu_set)  
792  {  {
793    MENU * p_menu;          int result;
794    MENU_ITEM * p_menuitem;          char conf_file[FILE_PATH_LEN];
795    int i, j;  
796              strncpy(conf_file, p_menu_set->conf_file, sizeof(conf_file) - 1);
797    for (i = 0; i < p_menu_set->menu_count; i++)          conf_file[sizeof(conf_file) - 1] = '\0';
798    {  
799      p_menu = p_menu_set->p_menu[i];          unload_menu(p_menu_set);
800      for (j = 0; j < p_menu->item_count; j++)          result = load_menu(p_menu_set, conf_file);
801      {  
802        free (p_menu->items[j]);          return result;
     }  
     free (p_menu);  
   }  
     
   p_menu_set->menu_count = 0;  
   p_menu_set->menu_select_depth = 0;  
803  }  }


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

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