29 {
30 FILE *fp;
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;
45 fp = fopen(
menu->mapp_spec,
"r");
46 if (fp == nullptr) {
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++;
63 l = strlen(tmp_buf);
64 if (!l)
65 continue;
66 if (directive == '#')
67 continue;
68 switch (directive) {
70 case '!':
72 break;
74 break;
76 l = strlen(tmp_buf);
78 break;
79 menu->line[
menu->line_idx]->command_str =
80 calloc(
MAXLEN + 1,
sizeof(
char));
81 if (!
menu->line[
menu->line_idx]->command_str) {
82 sprintf(tmp_str,
83 "0-malloc(%d bytes) failed M-L[%d]->command_str",
86 }
89 menu->line[
menu->line_idx]->command_type =
91 s =
menu->line[
menu->line_idx]->raw_text;
92 if (*s == '-' || *s == '_') {
93 s++;
94 ltr = *s++;
95 } else
96 ltr = *s;
97 l = 0;
98 while (*s++ != '\0')
99 l++;
102 menu->line[
menu->line_idx]->choice_text =
103 calloc(
MAXLEN + 1,
sizeof(
char));
104 if (!
menu->line[
menu->line_idx]->choice_text) {
105 sprintf(tmp_str,
106 "1-malloc(%d bytes) failed M-L[%d]->choice_text",
109 }
110 d =
menu->line[
menu->line_idx]->choice_text;
111 e = d + l;
112 s =
menu->line[
menu->line_idx]->raw_text;
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)
119 menu->choice_max_len = l;
121 menu->line[
menu->line_idx]->choice_letter =
'Q';
122 else
123 menu->line[
menu->line_idx]->choice_letter = ltr;
126 actions++;
127 break;
129 case ':':
130 if (choices > actions) {
132 "More choices than actions at line %d of",
133 in_fp_line);
137 abend(-1,
"unrecoverable error");
138 }
139 chrep(tmp_buf,
'\t',
' ');
140 l = strlen(tmp_buf);
141 if (l >
menu->text_max_len)
142 menu->text_max_len = l;
143 if (!
menu->title[0]) {
147 l += 4;
148 if (l >
menu->text_max_len)
149 menu->text_max_len = l;
150 } else {
153 sprintf(tmp_str,
154 "2-malloc(%ld bytes) failed menu->line[%d]",
157 }
159 menu->line[
menu->line_idx]->raw_text = strdup(tmp_buf);
160 menu->line[
menu->line_idx]->choice_text =
nullptr;
161 menu->line[
menu->line_idx]->choice_letter =
'\0';
162 menu->line[
menu->line_idx]->letter_pos = 0;
163 menu->line[
menu->line_idx]->command_type =
'\0';
164 menu->line[
menu->line_idx]->command_str =
nullptr;
166 choices++;
167 }
168 break;
169 case '?':
170 break;
171 case ' ':
172 case '\0':
173 case '\n':
174 break;
175 default:
177 directive, in_fp_line);
181 }
182 }
183 fclose(fp);
185 for (ltr = '0'; ltr < 'z'; ltr++)
188 for (
menu->line_idx = 0;
menu->line_idx <
menu->item_count;
190 ltr =
menu->line[
menu->line_idx]->choice_letter;
191 if (ltr < '0' || ltr > 'z')
192 ltr = '0';
193 if (!fltr[ltr] ||
195 ltr == 'Q')) {
197 s =
menu->line[
menu->line_idx]->choice_text;
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 }
224 menu->line[
menu->line_idx]->choice_letter = ltr;
225 }
226 menu->line[
menu->line_idx]->letter_pos = Pos;
227 }
229 if (
menu->text_max_len > (
menu->choice_max_len + 6))
231 else
232 menu->cols =
menu->choice_max_len + 6;
235 for (
menu->line_idx = 0;
menu->line_idx <
menu->item_count;
238 tmp_buf[1] =
menu->line[
menu->line_idx]->choice_letter;
243 }
244 return 0;
245}
int Perror(char *)
Display a simple error message window or print to stderr.
int display_error(char *em0, char *em1, char *em2, char *em3)
Display an error message window or print to stderr.
void abend(int, char *)
Abnormal program termination.
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
size_t trim(char *)
Trims leading and trailing spaces from string s in place.
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
bool chrep(char *, char, char)
Replaces all occurrences of old_chr in s with new_chr in place.
unsigned int get_command_type(char *)
Get command type from command string.
The Line strutures are attached to the Menu main structure.