-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuselect.c
1548 lines (1293 loc) · 38.4 KB
/
menuselect.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
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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2005 - 2006, Russell Bryant
*
* Russell Bryant <russell@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
*
* \author Russell Bryant <russell@digium.com>
*
* \brief A menu-driven system for Asterisk module selection
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include "mxml/mxml.h"
#include "linkedlists.h"
#include "menuselect.h"
#ifdef MENUSELECT_DEBUG
static FILE *debug;
#endif
/*! The list of categories */
struct categories categories = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
/*!
We have to maintain a pointer to the root of the trees generated from reading
the build options XML files so that we can free it when we're done. We don't
copy any of the information over from these trees. Our list is just a
convenient mapping to the information contained in these lists with one
additional piece of information - whether the build option is enabled or not.
*/
struct tree {
/*! the root of the tree */
mxml_node_t *root;
/*! for linking */
AST_LIST_ENTRY(tree) list;
};
/*! The list of trees from menuselect-tree files */
static AST_LIST_HEAD_NOLOCK_STATIC(trees, tree);
static const char * const tree_files[] = {
"menuselect-tree"
};
static char *output_makeopts = OUTPUT_MAKEOPTS_DEFAULT;
static char *output_makedeps = OUTPUT_MAKEDEPS_DEFAULT;
/*! This is set to 1 if menuselect.makeopts pre-existed the execution of this app */
static int existing_config = 0;
/*! This is set when the --check-deps argument is provided. */
static int check_deps = 0;
/*! This variable is non-zero when any changes are made */
int changes_made = 0;
/*! Menu name */
const char *menu_name = "Menuselect";
enum dep_file_state {
DEP_FILE_UNKNOWN = -2,
DEP_FILE_DISABLED = -1,
DEP_FILE_UNMET = 0,
DEP_FILE_MET = 1,
};
/*! Global list of dependencies that are external to the tree */
struct dep_file {
char name[32];
enum dep_file_state met;
enum dep_file_state previously_met;
AST_LIST_ENTRY(dep_file) list;
};
AST_LIST_HEAD_NOLOCK_STATIC(deps_file, dep_file);
#if !defined(ast_strdupa) && defined(__GNUC__)
#define ast_strdupa(s) \
(__extension__ \
({ \
const char *__old = (s); \
size_t __len = strlen(__old) + 1; \
char *__new = __builtin_alloca(__len); \
memcpy (__new, __old, __len); \
__new; \
}))
#endif
/*! \brief return a pointer to the first non-whitespace character */
static inline char *skip_blanks(char *str)
{
if (!str)
return NULL;
while (*str && *str < 33)
str++;
return str;
}
static int open_debug(void)
{
#ifdef MENUSELECT_DEBUG
if (!(debug = fopen("menuselect_debug.txt", "w"))) {
fprintf(stderr, "Failed to open menuselect_debug.txt for debug output.\n");
return -1;
}
#endif
return 0;
}
#define print_debug(f, ...) __print_debug(__LINE__, f, ## __VA_ARGS__)
static void __attribute__((format(printf, 2, 3))) __print_debug(int line, const char *format, ...)
{
#ifdef MENUSELECT_DEBUG
va_list ap;
fprintf(debug, "%d -", line);
va_start(ap, format);
vfprintf(debug, format, ap);
va_end(ap);
fflush(debug);
#endif
}
static void close_debug(void)
{
#ifdef MENUSELECT_DEBUG
if (debug)
fclose(debug);
#endif
}
/*! \brief Add a category to the category list, ensuring that there are no duplicates */
static struct category *add_category(struct category *cat)
{
struct category *tmp;
AST_LIST_TRAVERSE(&categories, tmp, list) {
if (!strcmp(tmp->name, cat->name)) {
return tmp;
}
}
AST_LIST_INSERT_TAIL(&categories, cat, list);
return cat;
}
/*! \brief Add a member to the member list of a category, ensuring that there are no duplicates */
static int add_member(struct member *mem, struct category *cat)
{
struct member *tmp;
AST_LIST_TRAVERSE(&cat->members, tmp, list) {
if (!strcmp(tmp->name, mem->name)) {
fprintf(stderr, "Member '%s' already exists in category '%s', ignoring.\n", mem->name, cat->name);
return -1;
}
}
AST_LIST_INSERT_TAIL(&cat->members, mem, list);
return 0;
}
/*! \brief Free a member structure and all of its members */
static void free_member(struct member *mem)
{
struct depend *dep;
struct conflict *cnf;
struct use *use;
while ((dep = AST_LIST_REMOVE_HEAD(&mem->deps, list)))
free(dep);
while ((cnf = AST_LIST_REMOVE_HEAD(&mem->conflicts, list)))
free(cnf);
while ((use = AST_LIST_REMOVE_HEAD(&mem->uses, list)))
free(use);
free(mem);
}
/*! \brief Parse an input makeopts file */
static int parse_tree(const char *tree_file)
{
FILE *f;
struct tree *tree;
struct member *mem;
struct depend *dep;
struct conflict *cnf;
struct use *use;
mxml_node_t *cur;
mxml_node_t *cur2;
mxml_node_t *cur3;
mxml_node_t *menu;
const char *tmp;
if (!(f = fopen(tree_file, "r"))) {
fprintf(stderr, "Unable to open '%s' for reading!\n", tree_file);
return -1;
}
if (!(tree = calloc(1, sizeof(*tree)))) {
fclose(f);
return -1;
}
if (!(tree->root = mxmlLoadFile(NULL, f, MXML_OPAQUE_CALLBACK))) {
fclose(f);
free(tree);
return -1;
}
AST_LIST_INSERT_HEAD(&trees, tree, list);
menu = mxmlFindElement(tree->root, tree->root, "menu", NULL, NULL, MXML_DESCEND);
if ((tmp = mxmlElementGetAttr(menu, "name")))
menu_name = tmp;
for (cur = mxmlFindElement(menu, menu, "category", NULL, NULL, MXML_DESCEND_FIRST);
cur;
cur = mxmlFindElement(cur, menu, "category", NULL, NULL, MXML_NO_DESCEND))
{
struct category *cat;
struct category *newcat;
if (!(cat = calloc(1, sizeof(*cat))))
return -1;
cat->name = mxmlElementGetAttr(cur, "name");
newcat = add_category(cat);
if (newcat != cat) {
/* want to append members, and potentially update the category. */
free(cat);
cat = newcat;
}
if ((tmp = mxmlElementGetAttr(cur, "displayname")))
cat->displayname = tmp;
if ((tmp = mxmlElementGetAttr(cur, "positive_output")))
cat->positive_output = !strcasecmp(tmp, "yes");
if ((tmp = mxmlElementGetAttr(cur, "exclusive")))
cat->exclusive = !strcasecmp(tmp, "yes");
if ((tmp = mxmlElementGetAttr(cur, "remove_on_change")))
cat->remove_on_change = tmp;
if ((tmp = mxmlElementGetAttr(cur, "touch_on_change")))
cat->touch_on_change = tmp;
for (cur2 = mxmlFindElement(cur, cur, "member", NULL, NULL, MXML_DESCEND_FIRST);
cur2;
cur2 = mxmlFindElement(cur2, cur, "member", NULL, NULL, MXML_NO_DESCEND))
{
if (!(mem = calloc(1, sizeof(*mem))))
return -1;
mem->name = mxmlElementGetAttr(cur2, "name");
mem->displayname = mxmlElementGetAttr(cur2, "displayname");
mem->touch_on_change = mxmlElementGetAttr(cur2, "touch_on_change");
mem->remove_on_change = mxmlElementGetAttr(cur2, "remove_on_change");
if (!cat->positive_output) {
mem->was_enabled = mem->enabled = 1;
print_debug("Enabling %s because the category does not have positive output\n", mem->name);
}
cur3 = mxmlFindElement(cur2, cur2, "defaultenabled", NULL, NULL, MXML_DESCEND);
if (cur3 && cur3->child)
mem->defaultenabled = cur3->child->value.opaque;
for (cur3 = mxmlFindElement(cur2, cur2, "depend", NULL, NULL, MXML_DESCEND_FIRST);
cur3 && cur3->child;
cur3 = mxmlFindElement(cur3, cur2, "depend", NULL, NULL, MXML_NO_DESCEND))
{
if (!(dep = calloc(1, sizeof(*dep)))) {
free_member(mem);
return -1;
}
if ((tmp = mxmlElementGetAttr(cur3, "name"))) {
if (!strlen_zero(tmp)) {
dep->name = tmp;
}
}
if (!strlen_zero(cur3->child->value.opaque)) {
dep->displayname = cur3->child->value.opaque;
if (!dep->name) {
dep->name = dep->displayname;
}
AST_LIST_INSERT_TAIL(&mem->deps, dep, list);
} else
free(dep);
}
for (cur3 = mxmlFindElement(cur2, cur2, "conflict", NULL, NULL, MXML_DESCEND_FIRST);
cur3 && cur3->child;
cur3 = mxmlFindElement(cur3, cur2, "conflict", NULL, NULL, MXML_NO_DESCEND))
{
if (!(cnf = calloc(1, sizeof(*cnf)))) {
free_member(mem);
return -1;
}
if ((tmp = mxmlElementGetAttr(cur3, "name"))) {
if (!strlen_zero(tmp)) {
cnf->name = tmp;
}
}
if (!strlen_zero(cur3->child->value.opaque)) {
cnf->displayname = cur3->child->value.opaque;
if (!cnf->name) {
cnf->name = cnf->displayname;
}
AST_LIST_INSERT_TAIL(&mem->conflicts, cnf, list);
} else
free(cnf);
}
for (cur3 = mxmlFindElement(cur2, cur2, "use", NULL, NULL, MXML_DESCEND_FIRST);
cur3 && cur3->child;
cur3 = mxmlFindElement(cur3, cur2, "use", NULL, NULL, MXML_NO_DESCEND))
{
if (!(use = calloc(1, sizeof(*use)))) {
free_member(mem);
return -1;
}
if ((tmp = mxmlElementGetAttr(cur3, "name"))) {
if (!strlen_zero(tmp)) {
use->name = tmp;
}
}
if (!strlen_zero(cur3->child->value.opaque)) {
use->displayname = cur3->child->value.opaque;
if (!use->name) {
use->name = use->displayname;
}
AST_LIST_INSERT_TAIL(&mem->uses, use, list);
} else
free(use);
}
if (add_member(mem, cat))
free_member(mem);
}
}
fclose(f);
return 0;
}
/*!
* \arg interactive Set to non-zero if being called while user is making changes
*/
static unsigned int calc_dep_failures(int interactive, int pre_confload)
{
unsigned int result = 0;
struct category *cat;
struct member *mem;
struct depend *dep;
struct dep_file *dep_file;
unsigned int changed, old_failure;
AST_LIST_TRAVERSE(&categories, cat, list) {
AST_LIST_TRAVERSE(&cat->members, mem, list) {
old_failure = mem->depsfailed;
AST_LIST_TRAVERSE(&mem->deps, dep, list) {
if (dep->member)
continue;
mem->depsfailed = HARD_FAILURE;
AST_LIST_TRAVERSE(&deps_file, dep_file, list) {
if (!strcasecmp(dep_file->name, dep->name)) {
if (dep_file->met == DEP_FILE_MET) {
mem->depsfailed = NO_FAILURE;
}
break;
}
}
if (mem->depsfailed != NO_FAILURE) {
break; /* This dependency is not met, so we can stop now */
}
}
if (old_failure == SOFT_FAILURE && mem->depsfailed != HARD_FAILURE)
mem->depsfailed = SOFT_FAILURE;
}
}
if (pre_confload) {
return 0;
}
do {
changed = 0;
AST_LIST_TRAVERSE(&categories, cat, list) {
AST_LIST_TRAVERSE(&cat->members, mem, list) {
old_failure = mem->depsfailed;
if (mem->depsfailed == HARD_FAILURE)
continue;
mem->depsfailed = NO_FAILURE;
AST_LIST_TRAVERSE(&mem->deps, dep, list) {
if (!dep->member)
continue;
if (dep->member->depsfailed == HARD_FAILURE) {
mem->depsfailed = HARD_FAILURE;
break;
} else if (dep->member->depsfailed == SOFT_FAILURE) {
mem->depsfailed = SOFT_FAILURE;
} else if (!dep->member->enabled) {
mem->depsfailed = SOFT_FAILURE;
}
}
if (mem->depsfailed != old_failure) {
if ((mem->depsfailed == NO_FAILURE) && mem->was_defaulted) {
mem->enabled = !strcasecmp(mem->defaultenabled, "yes");
print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
} else {
mem->enabled = interactive ? 0 : mem->was_enabled;
print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
}
changed = 1;
break; /* This dependency is not met, so we can stop now */
}
}
if (changed)
break;
}
if (changed)
result = 1;
} while (changed);
return result;
}
static unsigned int calc_conflict_failures(int interactive, int pre_confload)
{
unsigned int result = 0;
struct category *cat;
struct member *mem;
struct conflict *cnf;
struct dep_file *dep_file;
unsigned int changed, old_failure;
AST_LIST_TRAVERSE(&categories, cat, list) {
AST_LIST_TRAVERSE(&cat->members, mem, list) {
old_failure = mem->conflictsfailed;
AST_LIST_TRAVERSE(&mem->conflicts, cnf, list) {
if (cnf->member)
continue;
mem->conflictsfailed = NO_FAILURE;
AST_LIST_TRAVERSE(&deps_file, dep_file, list) {
if (!strcasecmp(dep_file->name, cnf->name)) {
if (dep_file->met == DEP_FILE_MET) {
mem->conflictsfailed = HARD_FAILURE;
print_debug("Setting %s conflictsfailed to HARD_FAILURE\n", mem->name);
}
break;
}
}
if (mem->conflictsfailed != NO_FAILURE)
break; /* This conflict was found, so we can stop now */
}
if (old_failure == SOFT_FAILURE && mem->conflictsfailed != HARD_FAILURE) {
print_debug("%d - Setting %s conflictsfailed to SOFT_FAILURE\n", __LINE__, mem->name);
mem->conflictsfailed = SOFT_FAILURE;
}
}
}
if (pre_confload) {
return 0;
}
do {
changed = 0;
AST_LIST_TRAVERSE(&categories, cat, list) {
AST_LIST_TRAVERSE(&cat->members, mem, list) {
old_failure = mem->conflictsfailed;
if (mem->conflictsfailed == HARD_FAILURE)
continue;
mem->conflictsfailed = NO_FAILURE;
AST_LIST_TRAVERSE(&mem->conflicts, cnf, list) {
if (!cnf->member)
continue;
if (cnf->member->enabled) {
mem->conflictsfailed = SOFT_FAILURE;
print_debug("%d - Setting %s conflictsfailed to SOFT_FAILURE because %s is enabled\n", __LINE__, mem->name, cnf->member->name);
break;
}
}
if (mem->conflictsfailed != old_failure && mem->conflictsfailed != NO_FAILURE) {
mem->enabled = 0;
print_debug("Just set %s enabled to %d because of conflicts\n", mem->name, mem->enabled);
changed = 1;
break; /* This conflict has been found, so we can stop now */
}
}
if (changed)
break;
}
if (changed)
result = 1;
} while (changed);
return result;
}
/*! \brief Process dependencies against the input dependencies file */
static int process_deps(void)
{
FILE *f;
char buf[80];
int res = 0;
struct dep_file *dep_file;
if (!(f = fopen(MENUSELECT_DEPS, "r"))) {
fprintf(stderr, "Unable to open '%s' for reading! Did you run ./configure ?\n", MENUSELECT_DEPS);
return -1;
}
/* Build a dependency list from the file generated by configure */
while (memset(buf, 0, sizeof(buf)), fgets(buf, sizeof(buf), f)) {
char *name, *cur, *prev, *p;
int val;
/* Strip trailing CR/NL */
while ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) {
*p = '\0';
}
p = buf;
name = strsep(&p, "=");
if (!p)
continue;
cur = strsep(&p, ":");
prev = strsep(&p, ":");
if (!(dep_file = calloc(1, sizeof(*dep_file))))
break;
strncpy(dep_file->name, name, sizeof(dep_file->name) - 1);
dep_file->met = DEP_FILE_UNKNOWN;
dep_file->previously_met = DEP_FILE_UNKNOWN;
if (sscanf(cur, "%d", &val) != 1) {
fprintf(stderr, "Unknown value '%s' found in %s for %s\n", cur, MENUSELECT_DEPS, name);
} else {
switch (val) {
case DEP_FILE_MET:
case DEP_FILE_UNMET:
case DEP_FILE_DISABLED:
dep_file->met = val;
break;
default:
fprintf(stderr, "Unknown value '%s' found in %s for %s\n", cur, MENUSELECT_DEPS, name);
break;
}
}
if (prev) {
if (sscanf(prev, "%d", &val) != 1) {
fprintf(stderr, "Unknown value '%s' found in %s for %s\n", prev, MENUSELECT_DEPS, name);
} else {
switch (val) {
case DEP_FILE_MET:
case DEP_FILE_UNMET:
case DEP_FILE_DISABLED:
dep_file->previously_met = val;
break;
default:
fprintf(stderr, "Unknown value '%s' found in %s for %s\n", prev, MENUSELECT_DEPS, name);
break;
}
}
}
AST_LIST_INSERT_TAIL(&deps_file, dep_file, list);
}
fclose(f);
return res;
}
static void free_deps_file(void)
{
struct dep_file *dep_file;
/* Free the dependency list we built from the file */
while ((dep_file = AST_LIST_REMOVE_HEAD(&deps_file, list)))
free(dep_file);
}
static int match_member_relations(void)
{
struct category *cat, *cat2;
struct member *mem, *mem2;
struct depend *dep;
struct conflict *cnf;
struct use *use;
AST_LIST_TRAVERSE(&categories, cat, list) {
AST_LIST_TRAVERSE(&cat->members, mem, list) {
AST_LIST_TRAVERSE(&mem->deps, dep, list) {
AST_LIST_TRAVERSE(&cat->members, mem2, list) {
if (strcasecmp(mem2->name, dep->name))
continue;
dep->member = mem2;
break;
}
if (dep->member)
continue;
AST_LIST_TRAVERSE(&categories, cat2, list) {
AST_LIST_TRAVERSE(&cat2->members, mem2, list) {
if (strcasecmp(mem2->name, dep->name))
continue;
dep->member = mem2;
break;
}
if (dep->member)
break;
}
}
}
}
AST_LIST_TRAVERSE(&categories, cat, list) {
AST_LIST_TRAVERSE(&cat->members, mem, list) {
AST_LIST_TRAVERSE(&mem->uses, use, list) {
AST_LIST_TRAVERSE(&cat->members, mem2, list) {
if (strcasecmp(mem2->name, use->name))
continue;
use->member = mem2;
break;
}
if (use->member)
continue;
AST_LIST_TRAVERSE(&categories, cat2, list) {
AST_LIST_TRAVERSE(&cat2->members, mem2, list) {
if (strcasecmp(mem2->name, use->name))
continue;
use->member = mem2;
break;
}
if (use->member)
break;
}
}
}
}
AST_LIST_TRAVERSE(&categories, cat, list) {
if (!cat->exclusive)
continue;
AST_LIST_TRAVERSE(&cat->members, mem, list) {
AST_LIST_TRAVERSE(&cat->members, mem2, list) {
if (mem2 == mem)
continue;
if (!(cnf = calloc(1, sizeof(*cnf))))
return -1;
cnf->name = mem2->name;
cnf->member = mem2;
AST_LIST_INSERT_TAIL(&mem->conflicts, cnf, list);
}
}
}
AST_LIST_TRAVERSE(&categories, cat, list) {
AST_LIST_TRAVERSE(&cat->members, mem, list) {
AST_LIST_TRAVERSE(&mem->conflicts, cnf, list) {
AST_LIST_TRAVERSE(&cat->members, mem2, list) {
if (strcasecmp(mem2->name, cnf->name))
continue;
cnf->member = mem2;
break;
}
if (cnf->member)
continue;
AST_LIST_TRAVERSE(&categories, cat2, list) {
AST_LIST_TRAVERSE(&cat2->members, mem2, list) {
if (strcasecmp(mem2->name, cnf->name))
continue;
cnf->member = mem2;
break;
}
if (cnf->member)
break;
}
}
}
}
return 0;
}
/*! \brief Iterate through all of the input tree files and call the parse function on them */
static int build_member_list(void)
{
int i;
int res = -1;
for (i = 0; i < (sizeof(tree_files) / sizeof(tree_files[0])); i++) {
if ((res = parse_tree(tree_files[i]))) {
fprintf(stderr, "Error parsing '%s'!\n", tree_files[i]);
break;
}
}
if (!res)
res = match_member_relations();
return res;
}
/*! \brief Given the string representation of a member and category, mark it as present in a given input file */
static void mark_as_present(const char *member, const char *category)
{
struct category *cat;
struct member *mem;
print_debug("Marking %s of %s as present\n", member, category);
AST_LIST_TRAVERSE(&categories, cat, list) {
if (strcmp(category, cat->name))
continue;
AST_LIST_TRAVERSE(&cat->members, mem, list) {
if (!strcmp(member, mem->name)) {
mem->was_enabled = mem->enabled = cat->positive_output;
print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
break;
}
}
if (!mem)
fprintf(stderr, "member '%s' in category '%s' not found, ignoring.\n", member, category);
break;
}
if (!cat)
fprintf(stderr, "category '%s' not found! Can't mark '%s' as disabled.\n", category, member);
}
unsigned int enable_member(struct member *mem)
{
struct depend *dep;
unsigned int can_enable = 1;
AST_LIST_TRAVERSE(&mem->deps, dep, list) {
if (!dep->member)
continue;
if (!dep->member->enabled) {
if (dep->member->conflictsfailed != NO_FAILURE) {
can_enable = 0;
break;
}
if (dep->member->depsfailed == HARD_FAILURE) {
can_enable = 0;
break;
}
if (!(can_enable = enable_member(dep->member)))
break;
}
}
if ((mem->enabled = can_enable)) {
print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
}
return can_enable;
}
void toggle_enabled(struct member *mem)
{
if ((mem->depsfailed == HARD_FAILURE) || (mem->conflictsfailed == HARD_FAILURE))
return;
if (!mem->enabled)
enable_member(mem);
else
mem->enabled = 0;
print_debug("3- changed %s to %d\n", mem->name, mem->enabled);
mem->was_defaulted = 0;
changes_made++;
while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
}
/*! \brief Toggle a member of a category at the specified index to enabled/disabled */
void toggle_enabled_index(struct category *cat, int index)
{
struct member *mem;
int i = 0;
AST_LIST_TRAVERSE(&cat->members, mem, list) {
if (i++ == index)
break;
}
if (!mem)
return;
toggle_enabled(mem);
}
void set_enabled(struct category *cat, int index)
{
struct member *mem;
int i = 0;
AST_LIST_TRAVERSE(&cat->members, mem, list) {
if (i++ == index)
break;
}
if (!mem)
return;
if ((mem->depsfailed == HARD_FAILURE) || (mem->conflictsfailed == HARD_FAILURE))
return;
if (mem->enabled)
return;
enable_member(mem);
mem->was_defaulted = 0;
changes_made++;
while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
}
void clear_enabled(struct category *cat, int index)
{
struct member *mem;
int i = 0;
AST_LIST_TRAVERSE(&cat->members, mem, list) {
if (i++ == index)
break;
}
if (!mem)
return;
if (!mem->enabled)
return;
mem->enabled = 0;
mem->was_defaulted = 0;
changes_made++;
while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
}
/*! \brief Process a previously failed dependency
*
* If a module was previously disabled because of a failed dependency
* or a conflict, and not because the user selected it to be that way,
* then it needs to be re-enabled by default if the problem is no longer present.
*/
static void process_prev_failed_deps(char *buf)
{
const char *cat_name, *mem_name;
struct category *cat;
struct member *mem;
cat_name = strsep(&buf, "=");
mem_name = strsep(&buf, "\n");
if (!cat_name || !mem_name)
return;
AST_LIST_TRAVERSE(&categories, cat, list) {
if (strcasecmp(cat->name, cat_name))
continue;
AST_LIST_TRAVERSE(&cat->members, mem, list) {
if (strcasecmp(mem->name, mem_name))
continue;
if (!mem->depsfailed && !mem->conflictsfailed) {
mem->enabled = 1;
print_debug("Just set %s enabled to %d in processing of previously failed deps\n", mem->name, mem->enabled);
mem->was_defaulted = 0;
}
break;
}
break;
}
if (!cat || !mem)
fprintf(stderr, "Unable to find '%s' in category '%s'\n", mem_name, cat_name);
}
/*! \brief Parse an existing output makeopts file and enable members previously selected */
static int parse_existing_config(const char *infile)
{
FILE *f;
char buf[2048];
char *category, *parse, *member;
int lineno = 0;
if (!(f = fopen(infile, "r"))) {
/* This isn't really an error, so only print the message in debug mode */
print_debug("Unable to open '%s' for reading existing config.\n", infile);
return -1;
}
while (fgets(buf, sizeof(buf), f)) {
lineno++;
if (strlen_zero(buf))
continue;
/* skip lines that are not for this tool */
if (strncasecmp(buf, "MENUSELECT_", strlen("MENUSELECT_")))
continue;
if (!strncasecmp(buf, "MENUSELECT_DEPENDS_", strlen("MENUSELECT_DEPENDS_")))
continue;
if (!strncasecmp(buf, "MENUSELECT_BUILD_DEPS", strlen("MENUSELECT_BUILD_DEPS")))
continue;
parse = buf;
parse = skip_blanks(parse);
if (strlen_zero(parse))
continue;
/* Grab the category name */
category = strsep(&parse, "=");
if (!parse) {
fprintf(stderr, "Invalid string in '%s' at line '%d'!\n", output_makeopts, lineno);
continue;
}
parse = skip_blanks(parse);
if (!strcasecmp(category, "MENUSELECT_DEPSFAILED")) {
process_prev_failed_deps(parse);
continue;
}
while ((member = strsep(&parse, " \n"))) {
member = skip_blanks(member);
if (strlen_zero(member))
continue;
mark_as_present(member, category);
}
}