-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathini_parser.c
371 lines (331 loc) · 12.7 KB
/
ini_parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "ini_parser.h"
#include "../msp/vtxmenu.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern MenuSection *current_section;
// Helper function to split the list of values in a comma-separated list
void split_values(
const char *values, char value_list[MAX_VALUE_LIST_ITEMS][MAX_VALUE_LENGTH], int *value_count) {
char temp[MAX_LINE_LENGTH]; // Temporary buffer to hold a copy of the
// original string
strncpy(temp, values,
sizeof(temp) - 1); // Copy the original string to the temp buffer
temp[sizeof(temp) - 1] = '\0'; // Ensure null termination
char *token = strtok(temp, ",");
*value_count = 0; // Initialize the value count
while (token != NULL) {
if (*value_count < MAX_VALUE_LIST_ITEMS) { // Check if there's space in the value list
// Copy each token to the value_list with proper length checking
strncpy(value_list[*value_count], token,
MAX_VALUE_LENGTH - 1); // Copy with limit
value_list[*value_count][MAX_VALUE_LENGTH - 1] = '\0'; // Ensure null termination
(*value_count)++; // Increment the count
} else {
// Optionally handle case when too many values are provided
printf("Warning: Maximum value count exceeded. Some values may be "
"ignored.\n");
break; // Exit if the limit is reached
}
token = strtok(NULL, ","); // Get the next token
}
}
void print_menu_system_state(MenuSystem *menu_system) {
printf("\n=== Menu System Internal State ===\n");
printf("We have %i sections:\n", menu_system->section_count);
// Iterate over each section in the menu system
for (int i = 0; i < menu_system->section_count; i++) {
MenuSection *section = &menu_system->sections[i];
printf("\nSection: %s\n", section->name);
// Iterate over each option in the section
for (int j = 0; j < section->option_count; j++) {
MenuOption *option = §ion->options[j];
printf(" Option: %s (Internal Name: %s)", option->lable, option->name);
switch (option->type) {
case MENU_OPTION_LIST: {
// If it's a list, display the current selected value
char value_list[MAX_VALUE_LIST_ITEMS][MAX_VALUE_LENGTH];
int value_count;
split_values(option->values, value_list, &value_count);
printf(" (List): Current Value: %s Values: %s (Index: %d)\n",
value_list[section->current_value_index[j]], option->values,
section->current_value_index[j]);
break;
}
case MENU_OPTION_RANGE: {
// If it's a range, display the current value
printf(" (Range): Current Value: %d (Min: %d, Max: %d)\n",
section->current_value_index[j], option->min, option->max);
break;
}
case MENU_OPTION_FLOATRANGE: {
// If it's a range, display the current value
printf(" (FloatRange): Current Value: %.1f (Min: %.1f, Max: "
"%.1f)\n",
section->current_value_index[j] / 10.0f, option->min / 10.0f,
option->max / 10.0f);
break;
}
case MENU_OPTION_SUBMENU: {
// If it's a submenu link, display the name of the linked
// submenu
printf(" (Submenu Link): Links to: %s\n", option->name);
break;
}
case MENU_OPTION_COMMAND: {
// If it's a submenu link, display the name of the linked
// submenu
if (option->command_function == runCustomCommand)
printf(" (Command): Command to run: %s\n", option->read_command);
else
printf(" (Command): %s\n", option->lable);
break;
}
default: {
// Generic case for unsupported option types (if any)
printf(" (Unknown Option Type)\n");
break;
}
}
}
}
printf("\n=================================\n");
}
// Function to run a shell command and capture the output
void run_command(const char *command, char *output, int output_size) {
FILE *fp;
printf("Running command: %s\n", command);
if ((fp = popen(command, "r")) == NULL) {
printf("Error running command: %s\n", command);
return;
}
fgets(output, output_size, fp); // Capture output
pclose(fp);
}
// Function to execute the save command with the given value
void save_value_to_system(const char *save_command, const char *value) {
char formatted_command[256]; // Buffer to hold the final formatted command
const char *placeholder = "{}"; // Define the placeholder
const char *placeholder_pos =
strstr(save_command, placeholder); // Find the position of the placeholder
if (placeholder_pos) {
// Copy the part before the placeholder
int prefix_length = placeholder_pos - save_command;
strncpy(formatted_command, save_command, prefix_length);
formatted_command[prefix_length] = '\0'; // Null-terminate the string
// Append the value
strncat(
formatted_command, value, sizeof(formatted_command) - strlen(formatted_command) - 1);
// Append the part after the placeholder
strncat(formatted_command, placeholder_pos + strlen(placeholder),
sizeof(formatted_command) - strlen(formatted_command) - 1);
} else {
// If there's no placeholder, just copy the command as-is
strncpy(formatted_command, save_command, sizeof(formatted_command) - 1);
formatted_command[sizeof(formatted_command) - 1] = '\0'; // Ensure null termination
}
// Debug print to show the full command that will be executed
printf("Executing command: %s\n", formatted_command);
// Execute the formatted command
int result = system(formatted_command);
// Check the result of the system call
if (result != 0) {
printf("Error: Command failed with code %d\n", result);
}
}
// Function to save all current values of options in a section
void save_all_changes() {
MenuSection *section = current_section;
for (int i = 0; i < section->option_count; i++) {
MenuOption *option = §ion->options[i];
// Only attempt to save if there's a save_command
if (strlen(option->save_command) > 0) {
char formatted_command[256]; // Buffer for formatted save command
char value[MAX_VALUE_LENGTH]; // Buffer for current value
// Get the current value depending on the type of option
switch (option->type) {
case MENU_OPTION_LIST: {
// For list options, get the currently selected value
char value_list[MAX_VALUE_LIST_ITEMS][MAX_VALUE_LENGTH];
int value_count;
split_values(option->values, value_list, &value_count);
strncpy(value, value_list[section->current_value_index[i]], sizeof(value));
value[sizeof(value) - 1] = '\0'; // Ensure null termination
break;
}
case MENU_OPTION_RANGE:
// For range options, just store the current value as a string
snprintf(value, sizeof(value), "%d", section->current_value_index[i]);
break;
case MENU_OPTION_FLOATRANGE:
// For range options, just store the current value as a string
snprintf(value, sizeof(value), "%.1f", section->current_value_index[i] / 10.0f);
break;
default:
continue; // Skip other option types
}
// Execute the save command with the formatted value
save_value_to_system(option->save_command, value);
}
}
}
void add_option(
MenuSection *section, const char *name, const char *label, void (*command_function)(void *)) {
MenuOption option;
// Copy name and label dynamically
strncpy(option.name, name, MAX_NAME_LENGTH - 1);
option.name[MAX_NAME_LENGTH - 1] = '\0'; // Ensure null termination
strncpy(option.lable, label, MAX_LABLE_LENGTH - 1);
option.lable[MAX_LABLE_LENGTH - 1] = '\0'; // Ensure null termination
option.type = MENU_OPTION_COMMAND;
option.command_function = command_function; // Function to save all changes
// Add the save option to the section's list of options
section->options[section->option_count++] = option;
}
int parse_ini(const char *filename, MenuSystem *menu_system) {
FILE *file = fopen(filename, "r");
if (!file) {
return -1; // Error opening file
}
char line[MAX_LINE_LENGTH];
MenuSection *current_section = NULL;
menu_system->section_count = 0;
while (fgets(line, sizeof(line), file)) {
// Remove newline character
line[strcspn(line, "\n")] = 0;
// Ignore empty lines
if (strlen(line) == 0) {
continue;
}
// Ignore commented lines
if (line[0] == ';' || line[0] == '#') {
continue;
}
// Check for section header
if (line[0] == '[') {
current_section = &menu_system->sections[menu_system->section_count++];
sscanf(line + 1, "%[^]]", current_section->name);
current_section->option_count = 0;
}
// Check for Options
else if (line[0] == 'O') {
char option_name[MAX_NAME_LENGTH];
char option_lable[MAX_LABLE_LENGTH];
char option_value[MAX_VALUE_LENGTH];
char read_command[MAX_VALUE_LENGTH] = "";
char save_command[MAX_VALUE_LENGTH] = "";
// Updated format: Option1=Label:Values:ReadCommand:SaveCommand
sscanf(line, "%[^=]=%[^:]:%[^:]:%[^:]:%[^\n]", option_name, option_lable, option_value,
read_command, save_command);
// Add option to the current section
MenuOption *option = ¤t_section->options[current_section->option_count++];
strcpy(option->name, option_name);
strcpy(option->lable, option_lable);
strcpy(option->values, option_value);
strcpy(option->read_command, read_command);
strcpy(option->save_command, save_command);
// Check if the value is a list or a range and initialize current
// value index
if (strchr(option_value, ',')) {
option->type = MENU_OPTION_LIST;
current_section->current_value_index[current_section->option_count - 1] =
0; // Default to the first list item
} else if (strstr(option_value, "-")) {
if (strstr(option_value, ".")) {
option->type = MENU_OPTION_FLOATRANGE;
float min_value, max_value;
sscanf(option_value, "%f-%f", &min_value, &max_value);
option->min = (int)(min_value * 10);
option->max = (int)(max_value * 10);
current_section->current_value_index[current_section->option_count - 1] =
option->min; // Default to min value
} else {
option->type = MENU_OPTION_RANGE;
sscanf(option_value, "%d-%d", &option->min, &option->max);
current_section->current_value_index[current_section->option_count - 1] =
option->min; // Default to min value
}
}
// Read Values from system
if (strlen(option->read_command) > 0) {
char output[MAX_VALUE_LENGTH] = "";
run_command(option->read_command, output, sizeof(output));
// Process the output to match the current value index in the
// list or range
output[strcspn(output, "\n")] = '\0'; // Remove the newline from output
// For list options, match the output to the corresponding index
switch (option->type) {
case MENU_OPTION_LIST: {
char value_list[MAX_VALUE_LIST_ITEMS][MAX_VALUE_LENGTH];
int value_count;
split_values(option->values, value_list, &value_count);
for (int i = 0; i < value_count; i++) {
if (strcmp(output, value_list[i]) == 0) {
current_section
->current_value_index[current_section->option_count - 1] = i;
break;
}
}
break;
}
case MENU_OPTION_RANGE: {
int current_value = atoi(output);
if (current_value >= option->min && current_value <= option->max) {
current_section->current_value_index[current_section->option_count - 1] =
current_value;
}
break;
}
case MENU_OPTION_FLOATRANGE: {
int current_value = (int)(atof(output) * 10);
if (current_value >= option->min && current_value <= option->max) {
current_section->current_value_index[current_section->option_count - 1] =
current_value;
}
break;
}
default:
break;
}
}
}
// Otherwise it's a submenu
else if (line[0] == 'S') {
char option_name[MAX_NAME_LENGTH];
char option_lable[MAX_LABLE_LENGTH];
// Split the line into name and value
sscanf(line, "%[^=]=%[^\n]", option_name, option_lable);
// Add option to the current section
MenuOption *option = ¤t_section->options[current_section->option_count++];
option->type = MENU_OPTION_SUBMENU;
strcpy(option->name, option_name);
strcpy(option->lable, option_lable);
}
// parse command only options
else if (line[0] == 'C') {
char option_name[MAX_NAME_LENGTH];
char option_lable[MAX_LABLE_LENGTH];
char option_command[MAX_LABLE_LENGTH];
// Split the line into name and value
sscanf(line, "%[^=]=%[^:]:%[^\n]", option_name, option_lable, option_command);
// Add option to the current section
MenuOption *option = ¤t_section->options[current_section->option_count++];
option->type = MENU_OPTION_COMMAND;
strcpy(option->name, option_name);
strcpy(option->lable, option_lable);
strcpy(option->read_command,
option_command); // store command in read command, we onle
// need one
option->command_function = runCustomCommand;
}
}
// Add programmatic commands
for (int s = 1; s < menu_system->section_count; s++) {
MenuSection *cs = &menu_system->sections[s];
add_option(cs, "Save", "SAVE", save_all_changes);
}
add_option(&menu_system->sections[0], "Reboot", "REBOOT", doreboot);
add_option(&menu_system->sections[0], "Exit", "EXIT", exitVTXMenu);
fclose(file);
return 0;
}