C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
parse_menu_desc.c
Go to the documentation of this file.
1/** @file parse_menu_desc.c
2 @brief Parse menu description file and create Menu
3 @author Bill Waller
4 Copyright (c) 2025
5 MIT License
6 billxwaller@gmail.com
7 @date 2026-02-09
8 */
9
10/** @defgroup parse_menu Menu Parser
11 @brief Functions for parsing menu description files and creating Menu
12 structures
13 */
14
15#include <common.h>
16#include <ctype.h>
17#include <stddef.h>
18#include <stdlib.h>
19#include <string.h>
20
21unsigned int parse_menu_description(Init *);
22unsigned int get_command_type(char *);
23
24/** @brief Parse menu description file and create Menu
25 @ingroup parse_menu
26 @param init Pointer to Init structure containing menu information
27 @return 0 on success, non-zero on failure
28 */
29unsigned int parse_menu_description(Init *init) {
30 FILE *fp;
31 char tmp_str[MAXLEN];
32 char tmp_buf[MAXLEN];
33 char in_buf[MAXLEN];
34 char *in_buf_p;
35 int Pos;
36 unsigned char ltr;
37 unsigned char fltr[127];
38 int directive;
39 int l;
40 char *s, *d, *e;
41 int actions = 0;
42 int choices = 0;
43 int in_fp_line = 0;
44 menu = init->menu;
45 fp = fopen(menu->mapp_spec, "r");
46 if (fp == nullptr) {
47 strnz__cpy(tmp_buf, "file not found", MAXLEN);
48 abend(-1, tmp_buf);
49 exit(-1);
50 }
51 while ((fgets(in_buf, MAXLEN, fp)) != nullptr) {
52 if (in_buf[0] == '\0')
53 continue;
54 in_fp_line++;
55 chrep(in_buf, '\r', '\0');
56 chrep(in_buf, '\n', '\0');
57 chrep(in_buf, '\t', ' ');
58 in_buf_p = in_buf;
59 directive = *in_buf_p;
60 in_buf_p++;
61 strnz__cpy(tmp_buf, in_buf_p, MAXLEN);
62 trim(tmp_buf);
63 l = strlen(tmp_buf);
64 if (!l)
65 continue;
66 if (directive == '#')
67 continue;
68 switch (directive) {
69 /** '!' Command */
70 case '!':
71 if (!menu->line_idx)
72 break;
74 break;
76 l = strlen(tmp_buf);
77 if (l + 1 > MAXLEN)
78 break;
80 calloc(MAXLEN + 1, sizeof(char));
82 sprintf(tmp_str,
83 "0-malloc(%d bytes) failed M-L[%d]->command_str",
85 abend(-1, tmp_str);
86 }
88 MAXLEN - 1);
90 get_command_type(tmp_buf);
92 if (*s == '-' || *s == '_') {
93 s++;
94 ltr = *s++;
95 } else
96 ltr = *s;
97 l = 0;
98 while (*s++ != '\0')
99 l++;
100 if (l + 1 > MAXLEN)
101 l = MAXLEN - 1;
103 calloc(MAXLEN + 1, sizeof(char));
105 sprintf(tmp_str,
106 "1-malloc(%d bytes) failed M-L[%d]->choice_text",
108 abend(-1, tmp_str);
109 }
111 e = d + l;
113 if (*s == '-' || *s == '_')
114 s += 2;
115 while (*s != '\0' && d < e)
116 *d++ = *s++;
117 *d = (char)'\0';
118 if (l > menu->choice_max_len)
122 else
126 actions++;
127 break;
128 /** ':' Text line, parse and add to menu */
129 case ':':
130 if (choices > actions) {
132 "More choices than actions at line %d of",
133 in_fp_line);
135 strnz__cpy(em2, in_buf, MAXLEN - 1);
137 abend(-1, "unrecoverable error");
138 }
139 chrep(tmp_buf, '\t', ' ');
140 l = strlen(tmp_buf);
141 if (l > menu->text_max_len)
143 if (!menu->title[0]) { /**< in_buf -> Title */
144 if (l + 5 > MAXLEN)
145 l = MAXLEN - 5;
147 l += 4;
148 if (l > menu->text_max_len)
150 } else {
151 menu->line[menu->line_idx] = calloc(1, sizeof(Line));
152 if (menu->line[menu->line_idx] == (Line *)0) {
153 sprintf(tmp_str,
154 "2-malloc(%ld bytes) failed menu->line[%d]",
155 sizeof(Line), menu->line_idx);
156 abend(-1, tmp_str);
157 }
159 menu->line[menu->line_idx]->raw_text = strdup(tmp_buf);
166 choices++;
167 }
168 break;
169 case '?':
170 break; /** ' ' Empty line, ignore */
171 case ' ':
172 case '\0':
173 case '\n':
174 break;
175 default:
176 ssnprintf(em0, MAXLEN - 1, "Invalid directive '%c' at line %d of",
177 directive, in_fp_line);
179 strnz__cpy(em2, in_buf, MAXLEN - 1);
181 }
182 }
183 fclose(fp);
185 for (ltr = '0'; ltr < 'z'; ltr++)
186 fltr[ltr] = FALSE;
187 fltr['Q'] = TRUE;
189 menu->line_idx++) {
191 if (ltr < '0' || ltr > 'z')
192 ltr = '0';
193 if (!fltr[ltr] ||
195 ltr == 'Q')) {
196 fltr[ltr] = TRUE;
198 Pos = 0;
199 while (*s != '\0') {
200 if (*s == ltr)
201 break;
202 s++;
203 Pos++;
204 }
205 } else {
206 Pos = 0;
207 while (ltr != '\0') {
208 Pos++;
209 ltr = toupper(menu->line[menu->line_idx]->choice_text[Pos]);
210 if (ltr >= '0' && ltr <= 'Z')
211 if (!fltr[ltr])
212 break;
213 }
214 if (ltr == '\0') {
215 for (ltr = '0'; ltr < 'Z'; ltr++)
216 if (!fltr[ltr])
217 break;
218 if (ltr > 'z') {
219 Perror("Ran out of letters");
220 return 0;
221 }
222 }
223 fltr[ltr] = TRUE;
225 }
227 }
231 else
233 if (menu->cols >= MAXLEN)
234 Perror("line too long");
236 menu->line_idx++) {
237 strnz__cpy(tmp_buf, " x - ", MAXLEN - 1);
240 MAXLEN - 1);
242 MAXLEN - 1);
243 }
244 return 0;
245}
246/** @brief Get command type from command string
247 @ingroup parse_menu
248 @param t Command string
249 @return Command type as an unsigned int
250 */
251unsigned int get_command_type(char *t) {
252 char *s, *p;
253
254 s = p = t;
255 while (*s != ' ' && *s != '\0') {
256 if (*s == '/')
257 p = s + 1;
258 s++;
259 }
260 *s = '\0';
261 if (!strcmp(p, "ckeys"))
262 return (CT_CKEYS);
263 else if (!strcmp(p, "exec"))
264 return (CT_EXEC);
265 else if (!strcmp(p, "help"))
266 return (CT_HELP);
267 else if (!strcmp(p, "about"))
268 return (CT_ABOUT);
269 else if (!strcmp(p, "menu"))
270 return (CT_MENU);
271 else if (!strcmp(p, "form"))
272 return (CT_FORM);
273 else if (!strcmp(p, "pick"))
274 return (CT_PICK);
275 else if (!strcmp(p, "return"))
276 return (CT_RETURN);
277 else if (!strcmp(p, "view"))
278 return (CT_VIEW);
279 else if (!strcmp(p, "?"))
280 return (CT_HELP);
281 else if (!strcmp(p, "write_config"))
282 return (CT_WRITE_CONFIG);
283 return (CT_UNDEFINED);
284}
#define nullptr
Definition cm.h:23
@ MT_TEXT
Definition menu.h:22
@ MT_CHOICE
Definition menu.h:22
Menu * menu
Definition mem.c:45
@ CT_PICK
Definition menu.h:37
@ CT_FORM
Definition menu.h:33
@ CT_UNDEFINED
Definition menu.h:43
@ CT_MENU
Definition menu.h:36
@ CT_RETURN
Definition menu.h:40
@ CT_HELP
Definition menu.h:31
@ CT_WRITE_CONFIG
Definition menu.h:42
@ CT_EXEC
Definition menu.h:30
@ CT_VIEW
Definition menu.h:38
@ CT_ABOUT
Definition menu.h:32
@ CT_CKEYS
Definition menu.h:39
#define TRUE
Definition iloan.c:19
#define FALSE
Definition iloan.c:18
#define MAXLEN
Definition curskeys.c:15
char em1[MAXLEN]
Definition dwin.c:133
char em0[MAXLEN]
Definition dwin.c:132
char em2[MAXLEN]
Definition dwin.c:134
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1110
int display_error(char *em0, char *em1, char *em2, char *em3)
Display an error message window or print to stderr.
Definition dwin.c:1054
void abend(int, char *)
Abnormal program termination.
Definition dwin.c:1331
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition futil.c:269
size_t trim(char *)
Trims leading and trailing spaces from string s in place.
Definition futil.c:118
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:147
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition futil.c:298
bool chrep(char *, char, char)
Replaces all occurrences of old_chr in s with new_chr in place.
Definition futil.c:486
unsigned int get_command_type(char *)
Get command type from command string.
unsigned int parse_menu_description(Init *)
Parse menu description file and create Menu.
Menu * menu
Definition common.h:172
char * choice_text
Definition menu.h:54
char * raw_text
Definition menu.h:51
char * command_str
Definition menu.h:70
unsigned int type
Definition menu.h:49
int letter_pos
Definition menu.h:60
unsigned int command_type
Definition menu.h:64
char choice_letter
Definition menu.h:57
int cols
Definition menu.h:92
char mapp_spec[MAXLEN]
Definition menu.h:115
Line * line[MAX_MENU_LINES]
Definition menu.h:194
int text_max_len
Definition menu.h:181
int item_count
Definition menu.h:187
int choice_max_len
Definition menu.h:175
int line_idx
Definition menu.h:190
int lines
Definition menu.h:90
char title[MAXLEN]
Definition menu.h:106