-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenuconfig.c
More file actions
1494 lines (1183 loc) · 27.3 KB
/
Copy pathmenuconfig.c
File metadata and controls
1494 lines (1183 loc) · 27.3 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ctype.h>
#include <errno.h>
#include <locale.h>
#include <ncurses.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#define MAX_OPTIONS 512
#define MAX_MENUS 128
#define MAX_MENU_CHILDREN 128
#define MAX_CHOICES 32
#define MAX_UNKNOWN 512
#define MAX_LINE 1024
#define KEY_SIZE 128
#define PROMPT_SIZE 256
#define MENU_TITLE_SIZE 128
#define VALUE_SIZE 256
#define CHOICE_SIZE 128
#define ERROR_SIZE 2048
#define SIZE_INVALID (size_t)-1
typedef enum {
OPT_BOOL,
OPT_STRING,
OPT_CHOICE,
} OptionType;
typedef struct {
char key[KEY_SIZE];
char prompt[PROMPT_SIZE];
OptionType type;
char value[VALUE_SIZE];
char default_value[VALUE_SIZE];
char choices[MAX_CHOICES][CHOICE_SIZE];
size_t choice_count;
} ConfigOption;
typedef enum {
MENU_ITEM_OPTION,
MENU_ITEM_SUBMENU,
} MenuItemType;
typedef struct {
MenuItemType type;
size_t index;
} MenuItem;
typedef struct {
char title[MENU_TITLE_SIZE];
int parent;
MenuItem children[MAX_MENU_CHILDREN];
size_t child_count;
} ConfigMenu;
typedef struct {
ConfigOption options[MAX_OPTIONS];
size_t option_count;
ConfigMenu menus[MAX_MENUS];
size_t menu_count;
char unknown_lines[MAX_UNKNOWN][MAX_LINE];
size_t unknown_count;
} ConfigState;
typedef struct {
int current_menu;
int selected[MAX_MENUS];
int offset[MAX_MENUS];
} MenuViewState;
typedef enum {
UI_NORMAL = 1,
UI_HEADER,
UI_PANEL,
UI_PANEL_TITLE,
UI_SELECTED,
UI_CATEGORY,
UI_VALUE,
UI_FOOTER,
UI_STATUS,
UI_DIRTY,
} UiColor;
static bool ui_color_enabled = false;
static void init_ui_theme(void) {
if (!has_colors()) {
return;
}
start_color();
use_default_colors();
init_pair(UI_NORMAL, COLOR_WHITE, -1);
init_pair(UI_HEADER, COLOR_BLACK, COLOR_CYAN);
init_pair(UI_PANEL, COLOR_WHITE, -1);
init_pair(UI_PANEL_TITLE, COLOR_CYAN, -1);
init_pair(UI_SELECTED, COLOR_BLACK, COLOR_WHITE);
init_pair(UI_CATEGORY, COLOR_CYAN, -1);
init_pair(UI_VALUE, COLOR_GREEN, -1);
init_pair(UI_FOOTER, COLOR_BLACK, COLOR_WHITE);
init_pair(UI_STATUS, COLOR_GREEN, -1);
init_pair(UI_DIRTY, COLOR_YELLOW, -1);
ui_color_enabled = true;
}
static void ui_attr_on(const int pair, const int attrs) {
if (ui_color_enabled) {
attron(COLOR_PAIR(pair));
}
if (attrs != A_NORMAL) {
attron(attrs);
}
}
static void ui_attr_off(const int pair, const int attrs) {
if (attrs != A_NORMAL) {
attroff(attrs);
}
if (ui_color_enabled) {
attroff(COLOR_PAIR(pair));
}
}
static void draw_fill(const int y, const int x, const int width) {
if (width <= 0) {
return;
}
mvhline(y, x, ' ', width);
}
static void draw_box(const int y, const int x, const int height, const int width) {
if (height < 2 || width < 2) {
return;
}
mvaddch(y, x, ACS_ULCORNER);
mvaddch(y, x + width - 1, ACS_URCORNER);
mvaddch(y + height - 1, x, ACS_LLCORNER);
mvaddch(y + height - 1, x + width - 1, ACS_LRCORNER);
mvhline(y, x + 1, ACS_HLINE, width - 2);
mvhline(y + height - 1, x + 1, ACS_HLINE, width - 2);
mvvline(y + 1, x, ACS_VLINE, height - 2);
mvvline(y + 1, x + width - 1, ACS_VLINE, height - 2);
}
static void draw_right_text(const int y, const char *text, const int max_x) {
const int len = (int)strlen(text);
if (len >= max_x) {
return;
}
mvaddnstr(y, max_x - len - 2, text, len);
}
static void copy_string(char *dst, const size_t dst_size, const char *src) {
if (src == NULL) {
dst[0] = '\0';
return;
}
size_t len = strlen(src);
if (len >= dst_size) {
len = dst_size - 1;
}
memcpy(dst, src, len);
dst[len] = '\0';
}
static void set_error(char *error, const char *format, ...) {
va_list args;
va_start(args, format);
const int written = vsnprintf(error, ERROR_SIZE, format, args);
va_end(args);
if (written < 0) {
copy_string(error, ERROR_SIZE, "failed to format error message");
} else if ((size_t)written >= ERROR_SIZE) {
error[ERROR_SIZE - 1] = '\0';
}
}
static char *trim(char *text) {
while (isspace((unsigned char)*text)) {
text++;
}
char *end = text + strlen(text);
while (end > text && isspace((unsigned char)end[-1])) {
end--;
}
*end = '\0';
return text;
}
static bool starts_with(const char *text, const char *prefix) {
return strncmp(text, prefix, strlen(prefix)) == 0;
}
static void skip_spaces(char **cursor) {
while (isspace((unsigned char)**cursor)) {
(*cursor)++;
}
}
static bool read_word(char **cursor, char *out, const size_t out_size) {
skip_spaces(cursor);
if (**cursor == '\0') {
return false;
}
size_t len = 0;
while (**cursor != '\0' && !isspace((unsigned char)**cursor)) {
if (len + 1 < out_size) {
out[len++] = **cursor;
}
(*cursor)++;
}
out[len] = '\0';
return len > 0;
}
static bool read_quoted(char **cursor, char *out, const size_t out_size) {
skip_spaces(cursor);
if (**cursor != '"') {
return false;
}
(*cursor)++;
size_t len = 0;
while (**cursor != '\0' && **cursor != '"') {
char c = **cursor;
if (c == '\\') {
(*cursor)++;
if (**cursor == '\0') {
break;
}
switch (**cursor) {
case 'n':
c = '\n';
break;
case 't':
c = '\t';
break;
case '"':
c = '"';
break;
case '\\':
c = '\\';
break;
default:
c = **cursor;
break;
}
}
if (len + 1 < out_size) {
out[len++] = c;
}
(*cursor)++;
}
if (**cursor != '"') {
return false;
}
(*cursor)++;
out[len] = '\0';
return true;
}
static bool read_value(char **cursor, char *out, const size_t out_size) {
skip_spaces(cursor);
if (**cursor == '"') {
return read_quoted(cursor, out, out_size);
}
return read_word(cursor, out, out_size);
}
static ConfigOption *find_option(ConfigState *state, const char *key) {
for (size_t i = 0; i < state->option_count; i++) {
if (strcmp(state->options[i].key, key) == 0) {
return &state->options[i];
}
}
return NULL;
}
static bool choice_contains(const ConfigOption *option, const char *value) {
for (size_t i = 0; i < option->choice_count; i++) {
if (strcmp(option->choices[i], value) == 0) {
return true;
}
}
return false;
}
static int choice_index(const ConfigOption *option) {
for (size_t i = 0; i < option->choice_count; i++) {
if (strcmp(option->choices[i], option->value) == 0) {
return (int)i;
}
}
return 0;
}
static bool parse_option_line(
ConfigState *state,
char *line,
const size_t line_number,
size_t *option_index
) {
if (state->option_count >= MAX_OPTIONS) {
fprintf(stderr, "schema:%zu: too many options\n", line_number);
return false;
}
char *cursor = line;
char type_word[32];
char key[KEY_SIZE];
char prompt[PROMPT_SIZE];
char default_value[VALUE_SIZE];
if (!read_word(&cursor, type_word, sizeof(type_word))) {
return true;
}
if (!read_word(&cursor, key, sizeof(key))) {
fprintf(stderr, "schema:%zu: missing key\n", line_number);
return false;
}
if (!read_quoted(&cursor, prompt, sizeof(prompt))) {
fprintf(stderr, "schema:%zu: missing prompt\n", line_number);
return false;
}
ConfigOption *option = &state->options[state->option_count];
memset(option, 0, sizeof(*option));
copy_string(option->key, sizeof(option->key), key);
copy_string(option->prompt, sizeof(option->prompt), prompt);
if (strcmp(type_word, "bool") == 0) {
option->type = OPT_BOOL;
if (!read_value(&cursor, default_value, sizeof(default_value))) {
copy_string(default_value, sizeof(default_value), "n");
}
if (
strcmp(default_value, "y") != 0 &&
strcmp(default_value, "n") != 0
) {
fprintf(
stderr,
"schema:%zu: bool default must be y or n\n",
line_number
);
return false;
}
copy_string(
option->default_value,
sizeof(option->default_value),
default_value
);
copy_string(
option->value,
sizeof(option->value),
default_value
);
} else if (strcmp(type_word, "string") == 0) {
option->type = OPT_STRING;
if (!read_value(&cursor, default_value, sizeof(default_value))) {
default_value[0] = '\0';
}
copy_string(
option->default_value,
sizeof(option->default_value),
default_value
);
copy_string(
option->value,
sizeof(option->value),
default_value
);
} else if (strcmp(type_word, "choice") == 0) {
option->type = OPT_CHOICE;
if (!read_value(&cursor, default_value, sizeof(default_value))) {
fprintf(
stderr,
"schema:%zu: missing choice default\n",
line_number
);
return false;
}
copy_string(
option->default_value,
sizeof(option->default_value),
default_value
);
copy_string(
option->value,
sizeof(option->value),
default_value
);
char choice[CHOICE_SIZE];
while (read_value(&cursor, choice, sizeof(choice))) {
if (option->choice_count >= MAX_CHOICES) {
fprintf(
stderr,
"schema:%zu: too many choices\n",
line_number
);
return false;
}
copy_string(
option->choices[option->choice_count],
sizeof(option->choices[option->choice_count]),
choice
);
option->choice_count++;
}
if (option->choice_count == 0) {
fprintf(
stderr,
"schema:%zu: choice needs values\n",
line_number
);
return false;
}
if (!choice_contains(option, option->default_value)) {
fprintf(
stderr,
"schema:%zu: choice default is not in values\n",
line_number
);
return false;
}
} else {
fprintf(
stderr,
"schema:%zu: unknown option type: %s\n",
line_number,
type_word
);
return false;
}
*option_index = state->option_count;
state->option_count++;
return true;
}
static void initialize_root_menu(ConfigState *state) {
ConfigMenu *root = &state->menus[0];
memset(root, 0, sizeof(*root));
copy_string(
root->title,
sizeof(root->title),
"Main menu"
);
root->parent = -1;
state->menu_count = 1;
}
static bool add_menu_child(
ConfigMenu *menu,
const MenuItemType type,
const size_t index,
const size_t line_number
) {
if (menu->child_count >= MAX_MENU_CHILDREN) {
fprintf(
stderr,
"schema:%zu: too many menu items\n",
line_number
);
return false;
}
if (index == SIZE_INVALID || index >= MAX_OPTIONS) {
fprintf(
stderr,
"schema:%zu: invalid menu item index\n",
line_number
);
return false;
}
MenuItem *item = &menu->children[menu->child_count];
item->type = type;
item->index = index;
menu->child_count++;
return true;
}
static bool begin_menu(
ConfigState *state,
const char *title,
int *current_menu,
const size_t line_number
) {
if (state->menu_count >= MAX_MENUS) {
fprintf(
stderr,
"schema:%zu: too many menus\n",
line_number
);
return false;
}
const size_t menu_index = state->menu_count;
ConfigMenu *menu = &state->menus[menu_index];
state->menu_count++;
memset(menu, 0, sizeof(*menu));
copy_string(
menu->title,
sizeof(menu->title),
title
);
menu->parent = *current_menu;
if (!add_menu_child(
&state->menus[*current_menu],
MENU_ITEM_SUBMENU,
menu_index,
line_number
)) {
return false;
}
*current_menu = (int)menu_index;
return true;
}
static bool end_menu(
ConfigState *state,
int *current_menu,
const size_t line_number
) {
if (*current_menu == 0) {
fprintf(
stderr,
"schema:%zu: unexpected endmenu\n",
line_number
);
return false;
}
*current_menu = state->menus[*current_menu].parent;
return true;
}
static bool load_schema(ConfigState *state, const char *path) {
FILE *file = fopen(path, "r");
if (file == NULL) {
fprintf(
stderr,
"failed to open schema: %s: %s\n",
path,
strerror(errno)
);
return false;
}
initialize_root_menu(state);
char line[MAX_LINE];
int current_menu = 0;
size_t line_number = 0;
while (fgets(line, sizeof(line), file) != NULL) {
line_number++;
char *text = trim(line);
if (*text == '\0' || *text == '#') {
continue;
}
if (starts_with(text, "menu")) {
char *cursor = text + 4;
char name[MENU_TITLE_SIZE];
if (!read_quoted(&cursor, name, sizeof(name))) {
fprintf(
stderr,
"schema:%zu: menu needs quoted name\n",
line_number
);
fclose(file);
return false;
}
if (!begin_menu(
state,
name,
¤t_menu,
line_number
)) {
fclose(file);
return false;
}
continue;
}
if (strcmp(text, "endmenu") == 0) {
if (!end_menu(
state,
¤t_menu,
line_number
)) {
fclose(file);
return false;
}
continue;
}
size_t option_index = SIZE_INVALID;
if (!parse_option_line(
state,
text,
line_number,
&option_index
)) {
fclose(file);
return false;
}
if (!add_menu_child(
&state->menus[current_menu],
MENU_ITEM_OPTION,
option_index,
line_number
)) {
fclose(file);
return false;
}
}
if (current_menu != 0) {
fprintf(
stderr,
"schema:%zu: missing endmenu\n",
line_number
);
fclose(file);
return false;
}
fclose(file);
return true;
}
static void normalize_config_value(ConfigOption *option) {
if (option->type == OPT_BOOL) {
if (strcmp(option->value, "y") != 0 && strcmp(option->value, "n") != 0) {
copy_string(option->value, sizeof(option->value), option->default_value);
}
} else if (option->type == OPT_CHOICE) {
if (!choice_contains(option, option->value)) {
copy_string(option->value, sizeof(option->value), option->default_value);
}
}
}
static void load_config(ConfigState *state, const char *path) {
FILE *file = fopen(path, "r");
if (file == NULL) {
return;
}
char line[MAX_LINE];
while (fgets(line, sizeof(line), file) != NULL) {
char original[MAX_LINE];
copy_string(original, sizeof(original), line);
original[strcspn(original, "\r\n")] = '\0';
char *text = trim(line);
if (*text == '\0' || *text == '#') {
continue;
}
char *equal = strchr(text, '=');
if (equal == NULL) {
continue;
}
*equal = '\0';
char *key = trim(text);
char *value_text = trim(equal + 1);
ConfigOption *option = find_option(state, key);
if (option == NULL) {
if (starts_with(key, "CONFIG_") && state->unknown_count < MAX_UNKNOWN) {
copy_string(
state->unknown_lines[state->unknown_count],
sizeof(state->unknown_lines[state->unknown_count]),
trim(original)
);
state->unknown_count++;
}
continue;
}
char *cursor = value_text;
char value[VALUE_SIZE];
if (!read_value(&cursor, value, sizeof(value))) {
value[0] = '\0';
}
copy_string(option->value, sizeof(option->value), value);
normalize_config_value(option);
}
fclose(file);
}
static void escape_config_string(FILE *file, const char *value) {
fputc('"', file);
for (const char *p = value; *p != '\0'; p++) {
switch (*p) {
case '\\':
fputs("\\\\", file);
break;
case '"':
fputs("\\\"", file);
break;
case '\n':
fputs("\\n", file);
break;
case '\t':
fputs("\\t", file);
break;
default:
fputc(*p, file);
break;
}
}
fputc('"', file);
}
static bool save_config(
ConfigState *state,
const char *path,
char *error
) {
char tmp_path[1024];
const int tmp_written = snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
if (tmp_written < 0 || (size_t)tmp_written >= sizeof(tmp_path)) {
set_error(error, "config path is too long: %s", path);
return false;
}
FILE *file = fopen(tmp_path, "w");
if (file == NULL) {
set_error(error, "config path is too long: %s", path);
return false;
}
fprintf(file, "# Generated by mochiOS menuconfig\n");
for (size_t i = 0; i < state->option_count; i++) {
ConfigOption *option = &state->options[i];
fprintf(file, "%s=", option->key);
if (option->type == OPT_BOOL) {
fprintf(file, "%s", option->value);
} else {
escape_config_string(file, option->value);
}
fputc('\n', file);
}
if (state->unknown_count > 0) {
fprintf(file, "\n# Preserved unknown entries\n");
for (size_t i = 0; i < state->unknown_count; i++) {
fprintf(file, "%s\n", state->unknown_lines[i]);
}
}
if (fclose(file) != 0) {
set_error(error, "config path is too long: %s", path);
return false;
}
if (rename(tmp_path, path) != 0) {
set_error(error, "config path is too long: %s", path);
return false;
}
return true;
}
static void option_display_value(
const ConfigOption *option,
char *out
) {
const size_t out_size = 264;
if (option->type == OPT_BOOL) {
copy_string(out, out_size, strcmp(option->value, "y") == 0 ? "[*]" : "[ ]");
} else if (option->type == OPT_STRING) {
snprintf(out, out_size, "\"%s\"", option->value);
} else {
snprintf(out, out_size, "<%s>", option->value);
}
}
static void build_menu_path(
const ConfigState *state,
const int menu_index,
char *out,
const size_t out_size
) {
int path[MAX_MENUS];
size_t depth = 0;
int current = menu_index;
while (current >= 0 && depth < MAX_MENUS) {
path[depth] = current;
depth++;
current = state->menus[current].parent;
}
out[0] = '\0';
while (depth > 0) {
const ConfigMenu *menu;
depth--;
menu = &state->menus[path[depth]];
if (out[0] != '\0') {
const size_t remaining =
out_size - strlen(out) - 1;
strncat(out, " > ", remaining);
}
if (strlen(out) + 1 < out_size) {
const size_t remaining =
out_size - strlen(out) - 1;
strncat(out, menu->title, remaining);
}
}
}
static void render(
ConfigState *state,
const MenuViewState *view,
const bool dirty,
const char *config_path,
const char *status
) {
int max_y;
int max_x;
getmaxyx(stdscr, max_y, max_x);
erase();
if (max_y < 8 || max_x < 48) {
mvaddnstr(
0,
0,
"terminal is too small",
max_x - 1
);
refresh();
return;
}
const ConfigMenu *menu =
&state->menus[view->current_menu];
const int selected =
view->selected[view->current_menu];
const int offset =
view->offset[view->current_menu];
ui_attr_on(UI_HEADER, A_BOLD);
draw_fill(0, 0, max_x);
mvaddnstr(
0,
2,
"mochiOS BuildConfig",
max_x - 4
);
draw_right_text(
0,
dirty ? "[modified]" : "[clean]",
max_x
);
ui_attr_off(UI_HEADER, A_BOLD);
char menu_path[MAX_LINE];