/[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.30 by sysadm, Tue May 6 05:31:26 2025 UTC Revision 1.33 by sysadm, Thu May 8 15:24:59 2025 UTC
# Line 27  Line 27 
27  #include <string.h>  #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_TEMP_DIR "var"  #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 load_menu(MENU_SET *p_menu_set, const char *conf_file)  int 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            int i = 0;
43            int j = 0;
44          char buffer[LINE_BUFFER_LEN];          char buffer[LINE_BUFFER_LEN];
         char screen_filename[FILE_PATH_LEN];  
45          char temp[LINE_BUFFER_LEN];          char temp[LINE_BUFFER_LEN];
46          regmatch_t pmatch[10];          char screen_filename[FILE_PATH_LEN];
47            char *p = NULL;
48            char *q = NULL;
49            char *saveptr = NULL;
50            MENU *p_menu = NULL;
51            MENU_ITEM *p_item = NULL;
52    
53            p_menu_set->menu_count = 0;
54    
55          if ((fin = fopen(conf_file, "r")) == NULL)          if ((fin = fopen(conf_file, "r")) == NULL)
56          {          {
57                  log_error("Open %s failed", conf_file);                  log_error("Open %s failed", conf_file);
58                  return -1;                  return -2;
59          }          }
60    
61          strncpy(p_menu_set->conf_file, conf_file, sizeof(p_menu_set->conf_file) - 1);          strncpy(p_menu_set->conf_file, conf_file, sizeof(p_menu_set->conf_file) - 1);
# Line 54  int load_menu(MENU_SET *p_menu_set, cons Line 63  int load_menu(MENU_SET *p_menu_set, cons
63    
64          while (fgets(buffer, sizeof(buffer), fin))          while (fgets(buffer, sizeof(buffer), fin))
65          {          {
66                  switch (buffer[0])                  fin_line++;
67    
68                    p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
69                    if (p == NULL) // Blank line
70                  {                  {
71                  case '#':                          continue;
72                          break;                  }
                 case '%':  
                         if (ireg("^%S_([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0)  
                         {  
                                 strncpy(temp, buffer + pmatch[1].rm_so,  
                                                 (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so));  
                                 temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';  
                                 snprintf(screen_filename, sizeof(screen_filename), "%s/MENU_SCR_%s", MENU_TEMP_DIR, temp);  
73    
74                                  if ((fout = fopen(screen_filename, "w")) == NULL)                  if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
75                    {
76                            continue;
77                    }
78    
79                    if (*p == '%')
80                    {
81                            p++;
82    
83                            if (strcmp(p, "menu") == 0) // BEGIN of sub-menu
84                            {
85                                    if (p_menu != NULL)
86                                  {                                  {
87                                          log_error("Open %s failed", screen_filename);                                          log_error("Incomplete menu definition in menu config line %d\n", fin_line);
88                                          return -2;                                          return -1;
89                                  }                                  }
90                                    p_menu = (MENU *)malloc(sizeof(MENU));
91                                  while (fgets(buffer, sizeof(buffer), fin))                                  if (p_menu == NULL)
92                                  {                                  {
93                                          if (buffer[0] != '%')                                          log_error("Unable to allocate memory for menu\n");
94                                                  fputs(buffer, fout);                                          return -3;
                                         else  
                                                 break;  
95                                  }                                  }
96                                    p_menu_set->p_menu[i] = p_menu;
97                                    i++;
98                                    p_menu_set->menu_count = i;
99    
100                                  fclose(fout);                                  j = 0; // Menu item counter
101                                  break;                                  p_menu->item_count = 0;
102                          }                                  p_menu->item_cur_pos = 0;
103                                    p_menu->title.show = 0;
104                                    p_menu->screen.show = 0;
105    
106                          if (ireg("^%menu ([A-Za-z0-9_]+)", buffer, 2, pmatch) == 0)                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
107                          {                                  if (q == NULL)
108                                  p_menu_set->p_menu[i] = malloc(sizeof(MENU));                                  {
109                                  p_menu_set->p_menu[i]->title.show = 0;                                          log_error("Error menu name in menu config line %d\n", fin_line);
110                                  p_menu_set->p_menu[i]->screen.show = 0;                                          return -1;
111                                    }
112                                  strncpy(p_menu_set->p_menu[i]->name,                                  p = q;
113                                                  buffer + pmatch[1].rm_so,                                  while (isalnum(*q) || *q == '_')
114                                                  (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so));                                  {
115                                  p_menu_set->p_menu[i]->name[pmatch[1].rm_eo - pmatch[1].rm_so] =                                          q++;
116                                          '\0';                                  }
117                                    if (*q != '\0')
118                                    {
119                                            log_error("Error menu name in menu config line %d\n", fin_line);
120                                            return -1;
121                                    }
122    
123                                  j = 0;                                  if (q - p > sizeof(p_menu->name) - 1)
124                                    {
125                                            log_error("Too longer menu name in menu config line %d\n", fin_line);
126                                            return -1;
127                                    }
128                                    strncpy(p_menu->name, p, sizeof(p_menu->name) - 1);
129                                    p_menu->name[sizeof(p_menu->name) - 1] = '\0';
130    
131                                    // Check syntax
132                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
133                                    if (q != NULL)
134                                    {
135                                            log_error("Unknown extra content in menu config line %d\n", fin_line);
136                                            return -1;
137                                    }
138    
139                                  while (fgets(buffer, sizeof(buffer), fin))                                  while (fgets(buffer, sizeof(buffer), fin))
140                                  {                                  {
141                                          if (buffer[0] == '#')                                          fin_line++;
142    
143                                            p = strtok_r(buffer, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
144                                            if (p == NULL) // Blank line
145                                            {
146                                                    continue;
147                                            }
148    
149                                            if (*p == '#' || *p == '\r' || *p == '\n') // Comment or blank line
150                                          {                                          {
151                                                  continue;                                                  continue;
152                                          }                                          }
153                                          if (buffer[0] == '%')  
154                                            if (*p == '%') // END of sub-menu
155                                          {                                          {
156                                                  p_menu_set->p_menu[i]->item_count = j;                                                  p_menu = NULL;
                                                 p_menu_set->p_menu[i]->item_cur_pos = 0;  
                                                 i++;  
157                                                  break;                                                  break;
158                                          }                                          }
159                                          if (ireg("^!([A-Za-z0-9_.]+)[[:space:]]*([0-9]+),"                                          else if (*p == '!' || *p == '@')
                                                          "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+),"  
                                                          "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\","  
                                                          "[[:space:]]*\"([^\"]+)\"",  
                                                          buffer, 8, pmatch) == 0)  
160                                          {                                          {
161                                                  p_menu_set->p_menu[i]->items[j] =                                                  // BEGIN of menu item
162                                                          malloc(sizeof(MENU_ITEM));                                                  p_item = (MENU_ITEM *)malloc(sizeof(MENU_ITEM));
163                                                  p_menu_set->p_menu[i]->items[j]->submenu = 1;                                                  if (p_item == NULL)
164                                                  strncpy(p_menu_set->p_menu[i]->items[j]->action,                                                  {
165                                                                  buffer + pmatch[1].rm_so,                                                          log_error("Unable to allocate memory for menu item\n");
166                                                                  (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so));                                                          return -3;
167                                                  p_menu_set->p_menu[i]->items[j]->action[pmatch[1].rm_eo -                                                  }
168                                                                                                                                  pmatch[1].rm_so] = '\0';                                                  p_menu->items[j] = p_item;
                                                 strncpy(temp, buffer + pmatch[2].rm_so,  
                                                                 (size_t)(pmatch[2].rm_eo - pmatch[2].rm_so));  
                                                 temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->items[j]->row = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[3].rm_so,  
                                                                 (size_t)(pmatch[3].rm_eo - pmatch[3].rm_so));  
                                                 temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->items[j]->col = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[4].rm_so,  
                                                                 (size_t)(pmatch[4].rm_eo - pmatch[4].rm_so));  
                                                 temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->items[j]->priv = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[5].rm_so,  
                                                                 (size_t)(pmatch[5].rm_eo - pmatch[5].rm_so));  
                                                 temp[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->items[j]->level = atoi(temp);  
                                                 strncpy(p_menu_set->p_menu[i]->items[j]->name,  
                                                                 buffer + pmatch[6].rm_so,  
                                                                 (size_t)(pmatch[6].rm_eo - pmatch[6].rm_so));  
                                                 p_menu_set->p_menu[i]->items[j]->name[pmatch[6].rm_eo -  
                                                                                                                           pmatch[6].rm_so] =  
                                                         '\0';  
                                                 strncpy(p_menu_set->p_menu[i]->items[j]->text,  
                                                                 buffer + pmatch[7].rm_so,  
                                                                 (size_t)(pmatch[7].rm_eo - pmatch[7].rm_so));  
                                                 p_menu_set->p_menu[i]->items[j]->text[pmatch[7].rm_eo -  
                                                                                                                           pmatch[7].rm_so] =  
                                                         '\0';  
169                                                  j++;                                                  j++;
170                                                  continue;                                                  p_menu->item_count = j;
171    
172                                                    p_item->submenu = (*p == '!' ? 1 : 0);
173    
174                                                    // Menu item action
175                                                    p++;
176                                                    if (strcmp(p, "..") == 0) // Return to parent menu
177                                                    {
178                                                            q = p + 2; // strlen("..")
179                                                    }
180                                                    else
181                                                    {
182                                                            q = p;
183                                                            while (isalnum(*q) || *q == '_')
184                                                            {
185                                                                    q++;
186                                                            }
187                                                            if (*q != '\0')
188                                                            {
189                                                                    log_error("Error menu item action in menu config line %d\n", fin_line);
190                                                                    return -1;
191                                                            }
192                                                    }
193    
194                                                    if (q - p > sizeof(p_item->action) - 1)
195                                                    {
196                                                            log_error("Too longer menu action in menu config line %d\n", fin_line);
197                                                            return -1;
198                                                    }
199                                                    strncpy(p_item->action, p, sizeof(p_item->action) - 1);
200                                                    p_item->action[sizeof(p_item->action) - 1] = '\0';
201    
202                                                    // Menu item row
203                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
204                                                    if (q == NULL)
205                                                    {
206                                                            log_error("Error menu item row in menu config line %d\n", fin_line);
207                                                            return -1;
208                                                    }
209                                                    p = q;
210                                                    while (isdigit(*q))
211                                                    {
212                                                            q++;
213                                                    }
214                                                    if (*q != '\0')
215                                                    {
216                                                            log_error("Error menu item row in menu config line %d\n", fin_line);
217                                                            return -1;
218                                                    }
219                                                    p_item->row = atoi(p);
220    
221                                                    // Menu item col
222                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
223                                                    if (q == NULL)
224                                                    {
225                                                            log_error("Error menu item col in menu config line %d\n", fin_line);
226                                                            return -1;
227                                                    }
228                                                    p = q;
229                                                    while (isdigit(*q))
230                                                    {
231                                                            q++;
232                                                    }
233                                                    if (*q != '\0')
234                                                    {
235                                                            log_error("Error menu item col in menu config line %d\n", fin_line);
236                                                            return -1;
237                                                    }
238                                                    p_item->col = atoi(p);
239    
240                                                    // Menu item priv
241                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
242                                                    if (q == NULL)
243                                                    {
244                                                            log_error("Error menu item priv in menu config line %d\n", fin_line);
245                                                            return -1;
246                                                    }
247                                                    p = q;
248                                                    while (isdigit(*q))
249                                                    {
250                                                            q++;
251                                                    }
252                                                    if (*q != '\0')
253                                                    {
254                                                            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    
259                                                    // Menu item level
260                                                    q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
261                                                    if (q == NULL)
262                                                    {
263                                                            log_error("Error menu item level in menu config line %d\n", fin_line);
264                                                            return -1;
265                                                    }
266                                                    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                                          if (ireg("^@([A-Za-z0-9_]+)[[:space:]]*([0-9]+),"                                          else if (strcmp(p, "title") == 0)
                                                          "[[:space:]]*([0-9]+),[[:space:]]*([0-9]+),"  
                                                          "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\","  
                                                          "[[:space:]]*\"([^\"]+)\"",  
                                                          buffer, 8, pmatch) == 0)  
343                                          {                                          {
344                                                  p_menu_set->p_menu[i]->items[j] =                                                  p_menu->title.show = 1;
345                                                          malloc(sizeof(MENU_ITEM));  
346                                                  p_menu_set->p_menu[i]->items[j]->submenu = 0;                                                  // Menu title row
347                                                  strncpy(p_menu_set->p_menu[i]->items[j]->action,                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
348                                                                  buffer + pmatch[1].rm_so,                                                  if (q == NULL)
349                                                                  (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so));                                                  {
350                                                  p_menu_set->p_menu[i]->items[j]->action[pmatch[1].rm_eo -                                                          log_error("Error menu title row in menu config line %d\n", fin_line);
351                                                                                                                                  pmatch[1].rm_so] = '\0';                                                          return -1;
352                                                  strncpy(temp, buffer + pmatch[2].rm_so,                                                  }
353                                                                  (size_t)(pmatch[2].rm_eo - pmatch[2].rm_so));                                                  p = q;
354                                                  temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                  while (isdigit(*q))
355                                                  p_menu_set->p_menu[i]->items[j]->row = atoi(temp);                                                  {
356                                                  strncpy(temp,                                                          q++;
357                                                                  buffer + pmatch[3].rm_so,                                                  }
358                                                                  (size_t)(pmatch[3].rm_eo - pmatch[3].rm_so));                                                  if (*q != '\0')
359                                                  temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';                                                  {
360                                                  p_menu_set->p_menu[i]->items[j]->col = atoi(temp);                                                          log_error("Error menu title row in menu config line %d\n", fin_line);
361                                                  strncpy(temp,                                                          return -1;
362                                                                  buffer + pmatch[4].rm_so,                                                  }
363                                                                  (size_t)(pmatch[4].rm_eo - pmatch[4].rm_so));                                                  p_menu->title.row = atoi(p);
364                                                  temp[pmatch[4].rm_eo - pmatch[4].rm_so] = '\0';  
365                                                  p_menu_set->p_menu[i]->items[j]->priv = atoi(temp);                                                  // Menu title col
366                                                  strncpy(temp,                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
367                                                                  buffer + pmatch[5].rm_so,                                                  if (q == NULL)
368                                                                  (size_t)(pmatch[5].rm_eo - pmatch[5].rm_so));                                                  {
369                                                  temp[pmatch[5].rm_eo - pmatch[5].rm_so] = '\0';                                                          log_error("Error menu title col in menu config line %d\n", fin_line);
370                                                  p_menu_set->p_menu[i]->items[j]->level = atoi(temp);                                                          return -1;
371                                                  strncpy(p_menu_set->p_menu[i]->items[j]->name,                                                  }
372                                                                  buffer + pmatch[6].rm_so,                                                  p = q;
373                                                                  (size_t)(pmatch[6].rm_eo - pmatch[6].rm_so));                                                  while (isdigit(*q))
374                                                  p_menu_set->p_menu[i]->items[j]->name[pmatch[6].rm_eo -                                                  {
375                                                                                                                            pmatch[6].rm_so] =                                                          q++;
376                                                          '\0';                                                  }
377                                                  strncpy(p_menu_set->p_menu[i]->items[j]->text,                                                  if (*q != '\0')
378                                                                  buffer + pmatch[7].rm_so,                                                  {
379                                                                  (size_t)(pmatch[7].rm_eo - pmatch[7].rm_so));                                                          log_error("Error menu title col in menu config line %d\n", fin_line);
380                                                  p_menu_set->p_menu[i]->items[j]->text[pmatch[7].rm_eo -                                                          return -1;
381                                                                                                                            pmatch[7].rm_so] =                                                  }
382                                                          '\0';                                                  p_menu->title.col = atoi(p);
383                                                  j++;  
384                                                  continue;                                                  // 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                                          if (ireg("^title[[:space:]]*([0-9]+),"                                          else if (strcmp(p, "screen") == 0)
                                                          "[[:space:]]*([0-9]+),[[:space:]]*\"([^\"]+)\"",  
                                                          buffer, 4, pmatch) == 0)  
421                                          {                                          {
422                                                  p_menu_set->p_menu[i]->title.show = 1;                                                  p_menu->screen.show = 1;
423                                                  strncpy(temp,  
424                                                                  buffer + pmatch[1].rm_so,                                                  // Menu screen row
425                                                                  (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so));                                                  q = strtok_r(NULL, MENU_CONF_DELIM_WITH_SPACE, &saveptr);
426                                                  temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';                                                  if (q == NULL)
427                                                  p_menu_set->p_menu[i]->title.row = atoi(temp);                                                  {
428                                                  strncpy(temp,                                                          log_error("Error menu screen row in menu config line %d\n", fin_line);
429                                                                  buffer + pmatch[2].rm_so,                                                          return -1;
430                                                                  (size_t)(pmatch[2].rm_eo - pmatch[2].rm_so));                                                  }
431                                                  temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';                                                  p = q;
432                                                  p_menu_set->p_menu[i]->title.col = atoi(temp);                                                  while (isdigit(*q))
433                                                  strncpy(p_menu_set->p_menu[i]->title.text,                                                  {
434                                                                  buffer + pmatch[3].rm_so,                                                          q++;
435                                                                  (size_t)(pmatch[3].rm_eo - pmatch[3].rm_so));                                                  }
436                                                  p_menu_set->p_menu[i]->title.text[pmatch[3].rm_eo -                                                  if (*q != '\0')
437                                                                                                                    pmatch[3].rm_so] =                                                  {
438                                                          '\0';                                                          log_error("Error menu screen row in menu config line %d\n", fin_line);
439                                                  continue;                                                          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                                            memcpy(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                                          if (ireg("^screen[[:space:]]*([0-9]+),"  
532                                                           "[[:space:]]*([0-9]+),[[:space:]]*S_([A-Za-z0-9_]+)",                                          if (fputs(buffer, fout) < 0)
                                                          buffer, 4, pmatch) == 0)  
533                                          {                                          {
534                                                  p_menu_set->p_menu[i]->screen.show = 1;                                                  log_error("Write %s failed", screen_filename);
535                                                  strncpy(temp,                                                  return -2;
                                                                 buffer + pmatch[1].rm_so,  
                                                                 (size_t)(pmatch[1].rm_eo - pmatch[1].rm_so));  
                                                 temp[pmatch[1].rm_eo - pmatch[1].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->screen.row = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[2].rm_so,  
                                                                 (size_t)(pmatch[2].rm_eo - pmatch[2].rm_so));  
                                                 temp[pmatch[2].rm_eo - pmatch[2].rm_so] = '\0';  
                                                 p_menu_set->p_menu[i]->screen.col = atoi(temp);  
                                                 strncpy(temp,  
                                                                 buffer + pmatch[3].rm_so,  
                                                                 (size_t)(pmatch[3].rm_eo - pmatch[3].rm_so));  
                                                 temp[pmatch[3].rm_eo - pmatch[3].rm_so] = '\0';  
                                                 snprintf(p_menu_set->p_menu[i]->screen.filename,  
                                                                  sizeof(p_menu_set->p_menu[i]->screen.filename),  
                                                                  "%s/MENU_SCR_%s", MENU_TEMP_DIR, temp);  
                                                 continue;  
536                                          }                                          }
537                                  }                                  }
538    
539                                    fclose(fout);
540                          }                          }
541                          break;                  }
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);          fclose(fin);
# Line 268  int load_menu(MENU_SET *p_menu_set, cons Line 554  int load_menu(MENU_SET *p_menu_set, cons
554          return 0;          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    
# Line 460  int menu_control(MENU_SET *p_menu_set, i Line 745  int menu_control(MENU_SET *p_menu_set, i
745                  display_menu_cursor(p_menu, 1);                  display_menu_cursor(p_menu, 1);
746                  break;                  break;
747          default:          default:
748                  for (i = 0; i < p_menu->item_count; i++)                  if (isalnum(key))
                 {  
                         if (key == p_menu->items[i]->name[0] &&  
                                 p_menu->items[i]->display)  
                         {  
                                 display_menu_cursor(p_menu, 0);  
                                 p_menu->item_cur_pos = i;  
                                 display_menu_cursor(p_menu, 1);  
                                 return 0;  
                         }  
                 }  
                 if (isalpha(key))  
749                  {                  {
750                          for (i = 0; i < p_menu->item_count; i++)                          for (i = 0; i < p_menu->item_count; i++)
751                          {                          {
# Line 502  void unload_menu(MENU_SET *p_menu_set) Line 776  void unload_menu(MENU_SET *p_menu_set)
776                  {                  {
777                          free(p_menu->items[j]);                          free(p_menu->items[j]);
778                  }                  }
                 remove(p_menu->screen.filename);  
779                  free(p_menu);                  free(p_menu);
780          }          }
781    


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

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