Debian Codegraph @

menuselect/menuselect.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2005 - 2010, Digium, Inc.
5 *
6 * Russell Bryant <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 *
22 * \author Russell Bryant <[email protected]>
23 *
24 * \brief A menu-driven system for Asterisk module selection
25 */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdarg.h>
32 #include <getopt.h>
33 #include <ctype.h>
34
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37
38 #include "autoconfig.h"
39 #include "linkedlists.h"
40 #include "menuselect.h"
41
42 #define ARRAY_LEN(a) (size_t) (sizeof(a) / sizeof(0[a]))
43
44 #ifdef MENUSELECT_DEBUG
45 static FILE *debug;
46 #endif
47
48 /*! The list of categories */
49 struct categories categories = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
50
51 /*!
52 We have to maintain a pointer to the root of the trees generated from reading
53 the build options XML files so that we can free it when we're done. We don't
54 copy any of the information over from these trees. Our list is just a
55 convenient mapping to the information contained in these lists with one
56 additional piece of information - whether the build option is enabled or not.
57 */
58 struct tree {
59 /*! the root of the tree */
60 xmlDoc *root;
61 /*! for linking */
62 AST_LIST_ENTRY(tree) list;
63 };
64
65 /*! The list of trees from menuselect-tree files */
66 static AST_LIST_HEAD_NOLOCK_STATIC(trees, tree);
67
68 static const char * const tree_files[] = {
69 "menuselect-tree"
70 };
71
72 static char *output_makeopts = OUTPUT_MAKEOPTS_DEFAULT;
73 static char *output_makedeps = OUTPUT_MAKEDEPS_DEFAULT;
74
75 /*! This is set to 1 if menuselect.makeopts pre-existed the execution of this app */
76 static int existing_config = 0;
77
78 /*! This is set when the --check-deps argument is provided. */
79 static int check_deps = 0;
80
81 /*! These are set when the --list-options or --list-groups arguments are provided. */
82 static int list_options = 0, list_groups = 0;
83
84 /*! This variable is non-zero when any changes are made */
85 int changes_made = 0;
86
87 /*! Menu name */
88 const char *menu_name = "Menuselect";
89
90 enum dep_file_state {
91 DEP_FILE_UNKNOWN = -2,
92 DEP_FILE_DISABLED = -1,
93 DEP_FILE_UNMET = 0,
94 DEP_FILE_MET = 1,
95 };
96
97 /*! Global list of dependencies that are external to the tree */
98 struct dep_file {
99 char name[32];
100 enum dep_file_state met;
101 enum dep_file_state previously_met;
102 AST_LIST_ENTRY(dep_file) list;
103 };
104 AST_LIST_HEAD_NOLOCK_STATIC(deps_file, dep_file);
105
106 /*! \brief return a pointer to the first non-whitespace character */
107 static inline char *skip_blanks(char *str)
108 {
109 if (!str)
110 return NULL;
111
112 while (*str && *str < 33)
113 str++;
114
115 return str;
116 }
117
118 static int open_debug(void)
119 {
120 #ifdef MENUSELECT_DEBUG
121 if (!(debug = fopen("menuselect_debug.txt", "w"))) {
122 fprintf(stderr, "Failed to open menuselect_debug.txt for debug output.\n");
123 return -1;
124 }
125 #endif
126 return 0;
127 }
128
129 #define print_debug(f, ...) __print_debug(__LINE__, f, ## __VA_ARGS__)
130 static void __attribute__((format(printf, 2, 3))) __print_debug(int line, const char *format, ...)
131 {
132 #ifdef MENUSELECT_DEBUG
133 va_list ap;
134
135 fprintf(debug, "%d -", line);
136
137 va_start(ap, format);
138 vfprintf(debug, format, ap);
139 va_end(ap);
140
141 fflush(debug);
142 #endif
143 }
144
145 static void close_debug(void)
146 {
147 #ifdef MENUSELECT_DEBUG
148 if (debug)
149 fclose(debug);
150 #endif
151 }
152
153 /*! \brief Finds a category with the given name or creates it if not found */
154 static struct category *category_find_or_create(const char *name)
155 {
156 struct category *c;
157
158 AST_LIST_TRAVERSE(&categories, c, list) {
159 if (!strcmp(c->name, name)) {
160 xmlFree((void *) name);
161 return c;
162 }
163 }
164
165 if (!(c = calloc(1, sizeof(*c)))) {
166 return NULL;
167 }
168
169 c->name = name;
170
171 AST_LIST_INSERT_TAIL(&categories, c, list);
172
173 return c;
174 }
175
176 static void free_reference(struct reference *ref)
177 {
178 /* If name and displayname point to the same place, only free one of them */
179 if (ref->name == ref->displayname) {
180 xmlFree((void *) ref->name);
181 } else {
182 xmlFree((void *) ref->name);
183 xmlFree((void *) ref->displayname);
184 }
185
186 free(ref);
187 }
188
189 /*! \brief Free a member structure and all of its members */
190 static void free_member(struct member *mem)
191 {
192 struct reference *ref;
193
194 if (!mem) {
195 return;
196 }
197
198 while ((ref = AST_LIST_REMOVE_HEAD(&mem->deps, list)))
199 free_reference(ref);
200 while ((ref = AST_LIST_REMOVE_HEAD(&mem->conflicts, list)))
201 free_reference(ref);
202 while ((ref = AST_LIST_REMOVE_HEAD(&mem->uses, list)))
203 free_reference(ref);
204
205 if (!mem->is_separator) {
206 xmlFree((void *) mem->name);
207 xmlFree((void *) mem->displayname);
208 xmlFree((void *) mem->touch_on_change);
209 xmlFree((void *) mem->remove_on_change);
210 xmlFree((void *) mem->defaultenabled);
211 xmlFree((void *) mem->support_level);
212 xmlFree((void *) mem->replacement);
213 xmlFree((void *) mem->deprecated_in);
214 xmlFree((void *) mem->removed_in);
215 }
216
217 free(mem);
218 }
219
220 /*! \assigns values to support level strings */
221 static enum support_level_values string_to_support_level(const char *support_level)
222 {
223 if (!support_level) {
224 return SUPPORT_UNSPECIFIED;
225 }
226
227 if (!strcasecmp(support_level, "core")) {
228 return SUPPORT_CORE;
229 }
230
231 if (!strcasecmp(support_level, "extended")) {
232 return SUPPORT_EXTENDED;
233 }
234
235 if (!strcasecmp(support_level, "deprecated")) {
236 return SUPPORT_DEPRECATED;
237 }
238
239 if (!strcasecmp(support_level, "external")) {
240 return SUPPORT_EXTERNAL;
241 }
242
243 if (!strcasecmp(support_level, "option")) {
244 return SUPPORT_OPTION;
245 }
246
247 return SUPPORT_UNSPECIFIED;
248 }
249
250 /*! \gets const separator strings from support level values */
251 static const char *support_level_to_string(enum support_level_values support_level)
252 {
253 switch (support_level) {
254 case SUPPORT_CORE:
255 return "Core";
256 case SUPPORT_EXTENDED:
257 return "Extended";
258 case SUPPORT_DEPRECATED:
259 return "Deprecated";
260 case SUPPORT_EXTERNAL:
261 return "External";
262 case SUPPORT_OPTION:
263 return "Module Options";
264 default:
265 return "Unspecified";
266 }
267 }
268
269 static void categories_flatten()
270 {
271 int idx;
272 struct category *c;
273 struct member *m;
274
275 AST_LIST_TRAVERSE(&categories, c, list) {
276 for (idx = 0; idx < SUPPORT_COUNT; idx++) {
277 struct support_level_bucket bucket = c->buckets[idx];
278 while ((m = AST_LIST_REMOVE_HEAD(&bucket, list))) {
279 AST_LIST_INSERT_TAIL(&c->members, m, list);
280 }
281 }
282 }
283 }
284
285 /*! \sets default values for a given separator */
286 static struct member *create_separator(enum support_level_values level)
287 {
288 struct member *separator = calloc(1, sizeof(*separator));
289 separator->name = support_level_to_string(level);
290 separator->displayname = "";
291 separator->is_separator = 1;
292 return separator;
293 }
294
295 /*! \adds a member to a category and attaches it to the last element of a particular support level used */
296 static int add_member_list_order(struct member *mem, struct category *cat)
297 {
298 enum support_level_values support_level = string_to_support_level(mem->support_level);
299 struct support_level_bucket *bucket = &cat->buckets[support_level];
300
301 if (AST_LIST_EMPTY(bucket)) {
302 struct member *sep = create_separator(support_level);
303 AST_LIST_INSERT_TAIL(bucket, sep, list);
304 }
305
306 AST_LIST_INSERT_TAIL(bucket, mem, list);
307
308 return 0;
309 }
310
311 static int process_xml_defaultenabled_node(xmlNode *node, struct member *mem)
312 {
313 const char *tmp = (const char *) xmlNodeGetContent(node);
314
315 if (tmp && !strlen_zero(tmp)) {
316 mem->defaultenabled = tmp;
317 }
318
319 return 0;
320 }
321
322 static int process_xml_supportlevel_node(xmlNode *node, struct member *mem)
323 {
324 const char *tmp = (const char *) xmlNodeGetContent(node);
325
326 if (tmp && !strlen_zero(tmp)) {
327 xmlFree((void *) mem->support_level);
328 mem->support_level = tmp;
329 print_debug("Set support_level for %s to %s\n", mem->name, mem->support_level);
330 }
331
332 return 0;
333 }
334
335 static int process_xml_replacement_node(xmlNode *node, struct member *mem)
336 {
337 const char *tmp = (const char *) xmlNodeGetContent(node);
338
339 if (tmp && !strlen_zero(tmp)) {
340 mem->replacement = tmp;
341 }
342
343 return 0;
344 }
345
346 static int process_xml_deprecatedin_node(xmlNode *node, struct member *mem)
347 {
348 const char *tmp = (const char *) xmlNodeGetContent(node);
349
350 if (tmp && !strlen_zero(tmp)) {
351 xmlFree((void *) mem->deprecated_in);
352 mem->deprecated_in = tmp;
353 print_debug("Set deprecated_in for %s to %s\n", mem->name, mem->deprecated_in);
354 }
355
356 return 0;
357 }
358
359 static int process_xml_removedin_node(xmlNode *node, struct member *mem)
360 {
361 const char *tmp = (const char *) xmlNodeGetContent(node);
362
363 if (tmp && !strlen_zero(tmp)) {
364 xmlFree((void *) mem->removed_in);
365 mem->removed_in = tmp;
366 print_debug("Set removed_in for %s to %s\n", mem->name, mem->removed_in);
367 }
368
369 return 0;
370 }
371
372 static int process_xml_ref_node(xmlNode *node, struct member *mem, struct reference_list *refs)
373 {
374 struct reference *ref;
375 const char *tmp;
376
377 if (!(ref = calloc(1, sizeof(*ref)))) {
378 free_member(mem);
379 return -1;
380 }
381
382 if ((tmp = (const char *) xmlGetProp(node, BAD_CAST "name"))) {
383 if (!strlen_zero(tmp)) {
384 ref->name = tmp;
385 }
386 }
387
388 if ((tmp = (const char *) xmlGetProp(node, BAD_CAST "autoselect"))) {
389 ref->autoselect = !strcasecmp(tmp, "yes");
390 }
391
392 tmp = (const char *) xmlNodeGetContent(node);
393
394 if (tmp && !strlen_zero(tmp)) {
395 ref->displayname = tmp;
396 if (!ref->name) {
397 ref->name = ref->displayname;
398 }
399
400 AST_LIST_INSERT_TAIL(refs, ref, list);
401 } else {
402 free_reference(ref);
403 }
404
405 return 0;
406 }
407
408 static int process_xml_depend_node(xmlNode *node, struct member *mem)
409 {
410 return process_xml_ref_node(node, mem, &mem->deps);
411 }
412
413 static int process_xml_conflict_node(xmlNode *node, struct member *mem)
414 {
415 return process_xml_ref_node(node, mem, &mem->conflicts);
416 }
417
418 static int process_xml_use_node(xmlNode *node, struct member *mem)
419 {
420 return process_xml_ref_node(node, mem, &mem->uses);
421 }
422
423 static int process_xml_member_data_node(xmlNode *node, struct member *mem)
424 {
425 return 0;
426 }
427
428 static int process_xml_unknown_node(xmlNode *node, struct member *mem)
429 {
430 fprintf(stderr, "Encountered unknown node: %s\n", node->name);
431 return 0;
432 }
433
434 typedef int (*node_handler)(xmlNode *, struct member *);
435
436 static const struct {
437 const char *name;
438 node_handler func;
439 } node_handlers[] = {
440 { "defaultenabled", process_xml_defaultenabled_node },
441 { "support_level", process_xml_supportlevel_node },
442 { "replacement", process_xml_replacement_node },
443 { "depend", process_xml_depend_node },
444 { "conflict", process_xml_conflict_node },
445 { "use", process_xml_use_node },
446 { "member_data", process_xml_member_data_node },
447 { "deprecated_in", process_xml_deprecatedin_node },
448 { "removed_in", process_xml_removedin_node },
449 };
450
451 static node_handler lookup_node_handler(xmlNode *node)
452 {
453 int i;
454
455 for (i = 0; i < ARRAY_LEN(node_handlers); i++) {
456 if (!strcmp(node_handlers[i].name, (const char *) node->name)) {
457 return node_handlers[i].func;
458 }
459 }
460
461 return process_xml_unknown_node;
462 }
463
464 static int process_process_xml_category_child_node(xmlNode *node, struct member *mem)
465 {
466 node_handler handler = lookup_node_handler(node);
467
468 return handler(node, mem);
469 }
470
471 static int process_xml_member_node(xmlNode *node, struct category *cat)
472 {
473 xmlNode *cur;
474 struct member *mem;
475 xmlChar *tmp;
476
477 if (!(mem = calloc(1, sizeof(*mem)))) {
478 return -1;
479 }
480
481 mem->name = (const char *) xmlGetProp(node, BAD_CAST "name");
482 mem->displayname = (const char *) xmlGetProp(node, BAD_CAST "displayname");
483 mem->touch_on_change = (const char *) xmlGetProp(node, BAD_CAST "touch_on_change");
484 mem->remove_on_change = (const char *) xmlGetProp(node, BAD_CAST "remove_on_change");
485 mem->support_level = (const char *) xmlCharStrdup("unspecified");
486
487 if ((tmp = xmlGetProp(node, BAD_CAST "explicitly_enabled_only"))) {
488 mem->explicitly_enabled_only = !xmlStrcasecmp(tmp, BAD_CAST "yes");
489 xmlFree(tmp);
490 }
491
492 for (cur = node->children; cur; cur = cur->next) {
493 if (cur->type != XML_ELEMENT_NODE) {
494 continue;
495 }
496
497 process_process_xml_category_child_node(cur, mem);
498 }
499
500 if (!cat->positive_output) {
501 mem->enabled = 1;
502 if (!mem->defaultenabled || strcasecmp(mem->defaultenabled, "no")) {
503 mem->was_enabled = 1;
504 print_debug("Enabled %s because the category does not have positive output\n", mem->name);
505 }
506 }
507
508 if (add_member_list_order(mem, cat)) {
509 free_member(mem);
510 }
511
512 return 0;
513 }
514
515 static void free_category(struct category *cat)
516 {
517 struct member *mem;
518
519 xmlFree((void *) cat->name);
520 xmlFree((void *) cat->displayname);
521 xmlFree((void *) cat->remove_on_change);
522 xmlFree((void *) cat->touch_on_change);
523
524 while ((mem = AST_LIST_REMOVE_HEAD(&cat->members, list))) {
525 free_member(mem);
526 }
527
528 free(cat);
529 }
530
531 static int process_xml_category_node(xmlNode *node)
532 {
533 struct category *cat;
534 const char *tmp;
535 xmlNode *cur;
536
537 if (!(tmp = (const char *) xmlGetProp(node, BAD_CAST "name"))) {
538 fprintf(stderr, "Missing 'name' attribute for 'category' element. Skipping...\n");
539 /* Return success here so we don't bail on the whole document */
540 return 0;
541 }
542
543 cat = category_find_or_create(tmp);
544
545 if ((tmp = (const char *) xmlGetProp(node, BAD_CAST "displayname"))) {
546 xmlFree((void *) cat->displayname);
547 cat->displayname = tmp;
548 }
549
550 if ((tmp = (const char *) xmlGetProp(node, BAD_CAST "remove_on_change"))) {
551 xmlFree((void *) cat->remove_on_change);
552 cat->remove_on_change = tmp;
553 }
554
555 if ((tmp = (const char *) xmlGetProp(node, BAD_CAST "touch_on_change"))) {
556 xmlFree((void *) cat->touch_on_change);
557 cat->touch_on_change = tmp;
558 }
559
560 if ((tmp = (const char *) xmlGetProp(node, BAD_CAST "positive_output"))) {
561 cat->positive_output = !strcasecmp(tmp, "yes");
562 xmlFree((void *) tmp);
563 }
564
565 if ((tmp = (const char *) xmlGetProp(node, BAD_CAST "exclusive"))) {
566 cat->exclusive = !strcasecmp(tmp, "yes");
567 xmlFree((void *) tmp);
568 }
569
570 for (cur = node->children; cur; cur = cur->next) {
571 if (cur->type != XML_ELEMENT_NODE) {
572 continue;
573 }
574
575 if (xmlStrcmp(cur->name, BAD_CAST "member")) {
576 fprintf(stderr, "Ignoring unknown element: %s\n", cur->name);
577 continue;
578 }
579
580 process_xml_member_node(cur, cat);
581 }
582
583 return 0;
584 }
585
586 static int process_xml_menu_node(struct tree *tree, xmlNode *node)
587 {
588 xmlNode *cur;
589 xmlChar *tmp;
590
591 if (strcmp((const char *) node->name, "menu")) {
592 fprintf(stderr, "Invalid document: Expected \"menu\" element.\n");
593 return -1;
594 }
595
596 AST_LIST_INSERT_HEAD(&trees, tree, list);
597
598 if ((tmp = xmlGetProp(node, BAD_CAST "name"))) {
599 menu_name = (const char *) tmp;
600 }
601
602 for (cur = node->children; cur; cur = cur->next) {
603 if (cur->type != XML_ELEMENT_NODE) {
604 continue;
605 }
606
607 if (xmlStrcmp(cur->name, BAD_CAST "category")) {
608 fprintf(stderr, "Ignoring unknown element: %s\n", cur->name);
609 continue;
610 }
611
612 if (process_xml_category_node(cur)) {
613 return -1;
614 }
615 }
616
617 categories_flatten();
618
619 return 0;
620 }
621
622 /*! \brief Parse an input makeopts file */
623 static int parse_tree(const char *tree_file)
624 {
625 struct tree *tree;
626 xmlNode *menu;
627
628 if (!(tree = calloc(1, sizeof(*tree)))) {
629 return -1;
630 }
631
632 if (!(tree->root = xmlParseFile(tree_file))) {
633 free(tree);
634 return -1;
635 }
636
637 if (!(menu = xmlDocGetRootElement(tree->root))) {
638 fprintf(stderr, "Invalid document: No root element\n");
639 xmlFreeDoc(tree->root);
640 free(tree);
641 return -1;
642 }
643
644 if (process_xml_menu_node(tree, menu)) {
645 xmlFreeDoc(tree->root);
646 free(tree);
647 return -1;
648 }
649
650 return 0;
651 }
652
653 /*!
654 * \arg interactive Set to non-zero if being called while user is making changes
655 */
656 static unsigned int calc_dep_failures(int interactive, int pre_confload)
657 {
658 unsigned int result = 0;
659 struct category *cat;
660 struct member *mem;
661 struct reference *dep;
662 struct dep_file *dep_file;
663 unsigned int changed;
664
665 AST_LIST_TRAVERSE(&categories, cat, list) {
666 AST_LIST_TRAVERSE(&cat->members, mem, list) {
667 if (mem->is_separator) {
668 continue;
669 }
670 mem->depsfailedold = mem->depsfailed;
671 AST_LIST_TRAVERSE(&mem->deps, dep, list) {
672 if (dep->member)
673 continue;
674
675 mem->depsfailed = HARD_FAILURE;
676 AST_LIST_TRAVERSE(&deps_file, dep_file, list) {
677 if (!strcasecmp(dep_file->name, dep->name)) {
678 if (dep_file->met == DEP_FILE_MET) {
679 mem->depsfailed = NO_FAILURE;
680 }
681 break;
682 }
683 }
684 if (mem->depsfailed != NO_FAILURE) {
685 break; /* This dependency is not met, so we can stop now */
686 }
687 }
688 if (mem->depsfailedold == SOFT_FAILURE && mem->depsfailed != HARD_FAILURE)
689 mem->depsfailed = SOFT_FAILURE;
690 }
691 }
692
693 if (pre_confload) {
694 return 0;
695 }
696
697 do {
698 changed = 0;
699
700 AST_LIST_TRAVERSE(&categories, cat, list) {
701 AST_LIST_TRAVERSE(&cat->members, mem, list) {
702 if (mem->is_separator) {
703 continue;
704 }
705
706 if (mem->depsfailed == HARD_FAILURE)
707 continue;
708
709 mem->depsfailed = NO_FAILURE;
710
711 AST_LIST_TRAVERSE(&mem->deps, dep, list) {
712 if (!dep->member)
713 continue;
714 if (dep->member->depsfailed == HARD_FAILURE) {
715 mem->depsfailed = HARD_FAILURE;
716 break;
717 } else if (dep->member->depsfailed == SOFT_FAILURE) {
718 mem->depsfailed = SOFT_FAILURE;
719 } else if (!dep->member->enabled) {
720 mem->depsfailed = SOFT_FAILURE;
721 }
722 }
723
724 if (mem->depsfailed != mem->depsfailedold) {
725 if ((mem->depsfailed == NO_FAILURE) && mem->was_defaulted) {
726 mem->enabled = !strcasecmp(mem->defaultenabled, "yes");
727 print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
728 } else {
729 mem->enabled = interactive ? 0 : mem->was_enabled;
730 print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
731 }
732 changed = 1;
733 /* We need to update the old failed deps for the next loop of this */
734 mem->depsfailedold = mem->depsfailed;
735 break; /* This dependency is not met, so we can stop now */
736 }
737 }
738 if (changed)
739 break;
740 }
741
742 if (changed)
743 result = 1;
744
745 } while (changed);
746
747 return result;
748 }
749
750 static unsigned int calc_conflict_failures(int interactive, int pre_confload)
751 {
752 unsigned int result = 0;
753 struct category *cat;
754 struct member *mem;
755 struct reference *cnf;
756 struct dep_file *dep_file;
757 unsigned int changed, old_failure;
758
759 AST_LIST_TRAVERSE(&categories, cat, list) {
760 AST_LIST_TRAVERSE(&cat->members, mem, list) {
761 if (mem->is_separator) {
762 continue;
763 }
764
765 old_failure = mem->conflictsfailed;
766 AST_LIST_TRAVERSE(&mem->conflicts, cnf, list) {
767 if (cnf->member)
768 continue;
769
770 mem->conflictsfailed = NO_FAILURE;
771 AST_LIST_TRAVERSE(&deps_file, dep_file, list) {
772 if (!strcasecmp(dep_file->name, cnf->name)) {
773 if (dep_file->met == DEP_FILE_MET) {
774 mem->conflictsfailed = HARD_FAILURE;
775 print_debug("Setting %s conflictsfailed to HARD_FAILURE\n", mem->name);
776 }
777 break;
778 }
779 }
780
781 if (mem->conflictsfailed != NO_FAILURE)
782 break; /* This conflict was found, so we can stop now */
783 }
784 if (old_failure == SOFT_FAILURE && mem->conflictsfailed != HARD_FAILURE) {
785 print_debug("%d - Setting %s conflictsfailed to SOFT_FAILURE\n", __LINE__, mem->name);
786 mem->conflictsfailed = SOFT_FAILURE;
787 }
788 }
789 }
790
791 if (pre_confload) {
792 return 0;
793 }
794
795 do {
796 changed = 0;
797
798 AST_LIST_TRAVERSE(&categories, cat, list) {
799 AST_LIST_TRAVERSE(&cat->members, mem, list) {
800 if (mem->is_separator) {
801 continue;
802 }
803
804 old_failure = mem->conflictsfailed;
805
806 if (mem->conflictsfailed == HARD_FAILURE)
807 continue;
808
809 mem->conflictsfailed = NO_FAILURE;
810
811 AST_LIST_TRAVERSE(&mem->conflicts, cnf, list) {
812 if (!cnf->member)
813 continue;
814
815 if (cnf->member->enabled) {
816 mem->conflictsfailed = SOFT_FAILURE;
817 print_debug("%d - Setting %s conflictsfailed to SOFT_FAILURE because %s is enabled\n", __LINE__, mem->name, cnf->member->name);
818 break;
819 }
820 }
821
822 if (mem->conflictsfailed != old_failure && mem->conflictsfailed != NO_FAILURE) {
823 mem->enabled = 0;
824 print_debug("Just set %s enabled to %d because of conflicts\n", mem->name, mem->enabled);
825 changed = 1;
826 break; /* This conflict has been found, so we can stop now */
827 }
828 }
829 if (changed)
830 break;
831 }
832
833 if (changed)
834 result = 1;
835
836 } while (changed);
837
838 return result;
839 }
840
841 /*! \brief Process dependencies against the input dependencies file */
842 static int process_deps(void)
843 {
844 FILE *f;
845 char buf[80];
846 int res = 0;
847 struct dep_file *dep_file;
848
849 if (!(f = fopen(MENUSELECT_DEPS, "r"))) {
850 fprintf(stderr, "Unable to open '%s' for reading! Did you run ./configure ?\n", MENUSELECT_DEPS);
851 return -1;
852 }
853
854 /* Build a dependency list from the file generated by configure */
855 while (memset(buf, 0, sizeof(buf)), fgets(buf, sizeof(buf), f)) {
856 char *name, *cur, *prev, *p;
857 int val;
858
859 /* Strip trailing CR/NL */
860 while ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) {
861 *p = '\0';
862 }
863
864 p = buf;
865 name = strsep(&p, "=");
866
867 if (!p)
868 continue;
869
870 cur = strsep(&p, ":");
871 prev = strsep(&p, ":");
872
873 if (!(dep_file = calloc(1, sizeof(*dep_file))))
874 break;
875
876 strncpy(dep_file->name, name, sizeof(dep_file->name) - 1);
877 dep_file->met = DEP_FILE_UNKNOWN;
878 dep_file->previously_met = DEP_FILE_UNKNOWN;
879
880 if (sscanf(cur, "%d", &val) != 1) {
881 fprintf(stderr, "Unknown value '%s' found in %s for %s\n", cur, MENUSELECT_DEPS, name);
882 } else {
883 switch (val) {
884 case DEP_FILE_MET:
885 case DEP_FILE_UNMET:
886 case DEP_FILE_DISABLED:
887 dep_file->met = val;
888 break;
889 default:
890 fprintf(stderr, "Unknown value '%s' found in %s for %s\n", cur, MENUSELECT_DEPS, name);
891 break;
892 }
893 }
894
895 if (prev) {
896 if (sscanf(prev, "%d", &val) != 1) {
897 fprintf(stderr, "Unknown value '%s' found in %s for %s\n", prev, MENUSELECT_DEPS, name);
898 } else {
899 switch (val) {
900 case DEP_FILE_MET:
901 case DEP_FILE_UNMET:
902 case DEP_FILE_DISABLED:
903 dep_file->previously_met = val;
904 break;
905 default:
906 fprintf(stderr, "Unknown value '%s' found in %s for %s\n", prev, MENUSELECT_DEPS, name);
907 break;
908 }
909 }
910 }
911
912 AST_LIST_INSERT_TAIL(&deps_file, dep_file, list);
913 }
914
915 fclose(f);
916
917 return res;
918 }
919
920 static void free_deps_file(void)
921 {
922 struct dep_file *dep_file;
923
924 /* Free the dependency list we built from the file */
925 while ((dep_file = AST_LIST_REMOVE_HEAD(&deps_file, list)))
926 free(dep_file);
927 }
928
929 static int match_member_relations(void)
930 {
931 struct category *cat, *cat2;
932 struct member *mem, *mem2;
933 struct reference *dep;
934 struct reference *cnf;
935 struct reference *use;
936
937 /* Traverse through each module's dependency list and determine whether each is another module */
938 AST_LIST_TRAVERSE(&categories, cat, list) {
939 AST_LIST_TRAVERSE(&cat->members, mem, list) {
940 if (mem->is_separator) {
941 continue;
942 }
943
944 AST_LIST_TRAVERSE(&mem->deps, dep, list) {
945 AST_LIST_TRAVERSE(&cat->members, mem2, list) {
946 if (mem->is_separator) {
947 continue;
948 }
949
950 if (strcasecmp(mem2->name, dep->name))
951 continue;
952
953 dep->member = mem2;
954 break;
955 }
956 if (dep->member)
957 continue;
958
959 AST_LIST_TRAVERSE(&categories, cat2, list) {
960 AST_LIST_TRAVERSE(&cat2->members, mem2, list) {
961 if (mem->is_separator) {
962 continue;
963 }
964
965 if (strcasecmp(mem2->name, dep->name))
966 continue;
967
968 dep->member = mem2;
969 break;
970 }
971 if (dep->member)
972 break;
973 }
974 }
975 }
976 }
977
978 /* Traverse through each module's use list and determine whether each is another module */
979 AST_LIST_TRAVERSE(&categories, cat, list) {
980 AST_LIST_TRAVERSE(&cat->members, mem, list) {
981 if (mem->is_separator) {
982 continue;
983 }
984
985 AST_LIST_TRAVERSE(&mem->uses, use, list) {
986 AST_LIST_TRAVERSE(&cat->members, mem2, list) {
987 if (mem->is_separator) {
988 continue;
989 }
990
991 if (strcasecmp(mem2->name, use->name))
992 continue;
993
994 use->member = mem2;
995 break;
996 }
997 if (use->member)
998 continue;
999
1000 AST_LIST_TRAVERSE(&categories, cat2, list) {
1001 AST_LIST_TRAVERSE(&cat2->members, mem2, list) {
1002 if (mem->is_separator) {
1003 continue;
1004 }
1005
1006 if (strcasecmp(mem2->name, use->name))
1007 continue;
1008
1009 use->member = mem2;
1010 break;
1011 }
1012 if (use->member)
1013 break;
1014 }
1015 }
1016 }
1017 }
1018
1019 /*
1020 * BUGBUG:
1021 * This doesn't work, the only way we can fix this is to remove OPTIONAL_API
1022 * toggle from menuselect and add a command-line argument to ./configure.
1023 */
1024 #if 0
1025 AST_LIST_TRAVERSE(&categories, cat, list) {
1026 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1027 if (mem->is_separator) {
1028 continue;
1029 }
1030
1031 AST_LIST_TRAVERSE_SAFE_BEGIN(&mem->uses, use, list) {
1032 if (use->member) {
1033 AST_LIST_REMOVE_CURRENT(&mem->uses, list);
1034 AST_LIST_INSERT_TAIL(&mem->deps, use, list);
1035 }
1036 }
1037 AST_LIST_TRAVERSE_SAFE_END;
1038 }
1039 }
1040 #endif
1041
1042 /* Traverse through each category marked as exclusive and mark every member as conflicting with every other member */
1043 AST_LIST_TRAVERSE(&categories, cat, list) {
1044 if (!cat->exclusive)
1045 continue;
1046
1047 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1048 if (mem->is_separator) {
1049 continue;
1050 }
1051
1052 AST_LIST_TRAVERSE(&cat->members, mem2, list) {
1053 if (mem->is_separator) {
1054 continue;
1055 }
1056
1057 if (mem2 == mem)
1058 continue;
1059
1060 if (!(cnf = calloc(1, sizeof(*cnf))))
1061 return -1;
1062
1063 cnf->name = mem2->name;
1064 cnf->member = mem2;
1065 AST_LIST_INSERT_TAIL(&mem->conflicts, cnf, list);
1066 }
1067 }
1068 }
1069
1070 /* Traverse through each category and determine whether named conflicts for each module are other modules */
1071 AST_LIST_TRAVERSE(&categories, cat, list) {
1072 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1073 if (mem->is_separator) {
1074 continue;
1075 }
1076
1077 AST_LIST_TRAVERSE(&mem->conflicts, cnf, list) {
1078 AST_LIST_TRAVERSE(&cat->members, mem2, list) {
1079 if (mem->is_separator) {
1080 continue;
1081 }
1082
1083 if (strcasecmp(mem2->name, cnf->name))
1084 continue;
1085
1086 cnf->member = mem2;
1087 break;
1088 }
1089 if (cnf->member)
1090 continue;
1091
1092 AST_LIST_TRAVERSE(&categories, cat2, list) {
1093 AST_LIST_TRAVERSE(&cat2->members, mem2, list) {
1094 if (mem->is_separator) {
1095 continue;
1096 }
1097
1098 if (strcasecmp(mem2->name, cnf->name))
1099 continue;
1100
1101 cnf->member = mem2;
1102 break;
1103 }
1104 if (cnf->member)
1105 break;
1106 }
1107 }
1108 }
1109 }
1110
1111 return 0;
1112 }
1113
1114 /*! \brief Iterate through all of the input tree files and call the parse function on them */
1115 static int build_member_list(void)
1116 {
1117 int i;
1118 int res = -1;
1119
1120 for (i = 0; i < (sizeof(tree_files) / sizeof(tree_files[0])); i++) {
1121 if ((res = parse_tree(tree_files[i]))) {
1122 fprintf(stderr, "Error parsing '%s'!\n", tree_files[i]);
1123 break;
1124 }
1125 }
1126
1127 if (!res)
1128 res = match_member_relations();
1129
1130 return res;
1131 }
1132
1133 static void mark_as_present_helper(const char *member, const char *category, int present)
1134 {
1135 struct category *cat;
1136 struct member *mem;
1137 char negate = 0;
1138
1139 if (*member == '-') {
1140 member++;
1141 negate = 1;
1142 }
1143
1144 print_debug("Marking %s of %s as %s\n", member, category, present ? "present" : "not present");
1145
1146 AST_LIST_TRAVERSE(&categories, cat, list) {
1147 if (strcmp(category, cat->name)) {
1148 continue;
1149 }
1150 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1151 if (mem->is_separator) {
1152 continue;
1153 }
1154
1155 if (!strcmp(member, mem->name)) {
1156 if (present) {
1157 mem->was_enabled = mem->enabled = (negate ? !cat->positive_output : cat->positive_output);
1158 } else {
1159 mem->was_enabled = mem->enabled = 0;
1160 }
1161 print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
1162 break;
1163 }
1164 }
1165 if (!mem) {
1166 fprintf(stderr, "member '%s' in category '%s' not found, ignoring.\n", member, category);
1167 }
1168 break;
1169 }
1170
1171 if (!cat) {
1172 fprintf(stderr, "category '%s' not found! Can't mark '%s' as disabled.\n", category, member);
1173 }
1174 }
1175
1176 /*! \brief Given the string representation of a member and category, mark it as present in a given input file */
1177 #define mark_as_present(member, category) mark_as_present_helper(member, category, 1)
1178
1179 /*! \brief Given the string representation of a member and category, mark it as not present in a given input file */
1180 #define mark_as_not_present(member, category) mark_as_present_helper(member, category, 0)
1181
1182 unsigned int enable_member(struct member *mem)
1183 {
1184 struct reference *dep;
1185 unsigned int can_enable = 1;
1186
1187 AST_LIST_TRAVERSE(&mem->deps, dep, list) {
1188 if (!dep->member)
1189 continue;
1190
1191 if (!dep->member->enabled) {
1192 if (dep->member->conflictsfailed != NO_FAILURE) {
1193 can_enable = 0;
1194 break;
1195 }
1196
1197 if (dep->member->depsfailed == HARD_FAILURE) {
1198 can_enable = 0;
1199 break;
1200 }
1201
1202 if (dep->member->explicitly_enabled_only) {
1203 can_enable = 0;
1204 break;
1205 }
1206
1207 if (!(can_enable = enable_member(dep->member)))
1208 break;
1209 }
1210 }
1211
1212 if ((mem->enabled = can_enable)) {
1213 struct reference *use;
1214
1215 print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
1216 while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
1217
1218 AST_LIST_TRAVERSE(&mem->uses, use, list) {
1219 if (use->member && use->autoselect && !use->member->enabled) {
1220 enable_member(use->member);
1221 }
1222 }
1223 }
1224
1225 return can_enable;
1226 }
1227
1228 void toggle_enabled(struct member *mem)
1229 {
1230 if ((mem->depsfailed == HARD_FAILURE) || (mem->conflictsfailed == HARD_FAILURE) || (mem->is_separator))
1231 return;
1232
1233 if (!mem->enabled)
1234 enable_member(mem);
1235 else
1236 mem->enabled = 0;
1237
1238 print_debug("3- changed %s to %d\n", mem->name, mem->enabled);
1239 mem->was_defaulted = 0;
1240 changes_made++;
1241
1242 while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
1243 }
1244
1245 /*! \brief Toggle a member of a category at the specified index to enabled/disabled */
1246 void toggle_enabled_index(struct category *cat, int index)
1247 {
1248 struct member *mem;
1249 int i = 0;
1250
1251 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1252 if (i++ == index)
1253 break;
1254 }
1255
1256 if (!mem)
1257 return;
1258
1259 toggle_enabled(mem);
1260 }
1261
1262 static void set_member_enabled(struct member *mem)
1263 {
1264 if ((mem->depsfailed == HARD_FAILURE) || (mem->conflictsfailed == HARD_FAILURE))
1265 return;
1266
1267 if ((mem->enabled) || (mem->is_separator))
1268 return;
1269
1270 enable_member(mem);
1271 mem->was_defaulted = 0;
1272 changes_made++;
1273
1274 while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
1275 }
1276
1277 void set_enabled(struct category *cat, int index)
1278 {
1279 struct member *mem;
1280 int i = 0;
1281
1282 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1283 if (mem->is_separator) {
1284 continue;
1285 }
1286
1287 if (i++ == index)
1288 break;
1289 }
1290
1291 if (!mem)
1292 return;
1293
1294 set_member_enabled(mem);
1295 }
1296
1297 static void clear_member_enabled(struct member *mem)
1298 {
1299 if (!mem->enabled)
1300 return;
1301
1302 mem->enabled = 0;
1303 mem->was_defaulted = 0;
1304 changes_made++;
1305
1306 while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
1307 }
1308
1309 void clear_enabled(struct category *cat, int index)
1310 {
1311 struct member *mem;
1312 int i = 0;
1313
1314 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1315 if (mem->is_separator) {
1316 continue;
1317 }
1318
1319 if (i++ == index)
1320 break;
1321 }
1322
1323 if (!mem)
1324 return;
1325
1326 clear_member_enabled(mem);
1327 }
1328
1329 /*! \brief Process a previously failed dependency
1330 *
1331 * If a module was previously disabled because of a failed dependency
1332 * or a conflict, and not because the user selected it to be that way,
1333 * then it needs to be re-enabled by default if the problem is no longer present.
1334 */
1335 static void process_prev_failed_deps(char *buf)
1336 {
1337 const char *cat_name, *mem_name;
1338 struct category *cat;
1339 struct member *mem;
1340
1341 cat_name = strsep(&buf, "=");
1342 mem_name = strsep(&buf, "\n");
1343
1344 if (!cat_name || !mem_name)
1345 return;
1346
1347 AST_LIST_TRAVERSE(&categories, cat, list) {
1348 if (strcasecmp(cat->name, cat_name))
1349 continue;
1350 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1351 if (mem->is_separator) {
1352 continue;
1353 }
1354
1355 if (strcasecmp(mem->name, mem_name))
1356 continue;
1357
1358 if (!mem->depsfailed && !mem->conflictsfailed) {
1359 mem->enabled = 1;
1360 print_debug("Just set %s enabled to %d in processing of previously failed deps\n", mem->name, mem->enabled);
1361 mem->was_defaulted = 0;
1362 }
1363
1364 break;
1365 }
1366 break;
1367 }
1368
1369 if (!cat || !mem)
1370 fprintf(stderr, "Unable to find '%s' in category '%s'\n", mem_name, cat_name);
1371 }
1372
1373 /*! \brief Parse an existing output makeopts file and enable members previously selected */
1374 static int parse_existing_config(const char *infile)
1375 {
1376 FILE *f;
1377 #define PARSE_BUF_SIZE 8192
1378 char *buf = NULL;
1379 char *category, *parse, *member;
1380 int lineno = 0;
1381
1382 if (!(f = fopen(infile, "r"))) {
1383 /* This isn't really an error, so only print the message in debug mode */
1384 print_debug("Unable to open '%s' for reading existing config.\n", infile);
1385 return -1;
1386 }
1387
1388 buf = malloc(PARSE_BUF_SIZE);
1389 if (!buf) {
1390 fprintf(stderr, "Unable to allocate buffer for reading existing config.\n");
1391 exit(1);
1392 }
1393
1394 while (fgets(buf, PARSE_BUF_SIZE, f)) {
1395 struct category *cat;
1396 struct member *mem;
1397
1398 lineno++;
1399
1400 if (strlen_zero(buf))
1401 continue;
1402
1403 /* skip lines that are not for this tool */
1404 if (strncasecmp(buf, "MENUSELECT_", strlen("MENUSELECT_")))
1405 continue;
1406
1407 if (!strncasecmp(buf, "MENUSELECT_DEPENDS_", strlen("MENUSELECT_DEPENDS_")))
1408 continue;
1409
1410 if (!strncasecmp(buf, "MENUSELECT_BUILD_DEPS", strlen("MENUSELECT_BUILD_DEPS")))
1411 continue;
1412
1413 parse = buf;
1414 parse = skip_blanks(parse);
1415 if (strlen_zero(parse))
1416 continue;
1417
1418 /* Grab the category name */
1419 category = strsep(&parse, "=");
1420 if (!parse) {
1421 fprintf(stderr, "Invalid string in '%s' at line '%d'!\n", output_makeopts, lineno);
1422 continue;
1423 }
1424
1425 parse = skip_blanks(parse);
1426
1427 if (!strcasecmp(category, "MENUSELECT_DEPSFAILED")) {
1428 process_prev_failed_deps(parse);
1429 continue;
1430 }
1431
1432 AST_LIST_TRAVERSE(&categories, cat, list) {
1433 if (strcmp(category, cat->name)) {
1434 continue;
1435 }
1436 if (!cat->positive_output) {
1437 print_debug("Category %s is NOT positive output\n", cat->name);
1438 /* if NOT positive_output, then if listed in makeopts, it's disabled! */
1439 /* this means that what's listed in menuselect.makeopts is a list of modules
1440 * that are NOT selected, so we can't use that to mark things as present.
1441 * In fact, we need to mark everything as present, UNLESS it's listed
1442 * in menuselect.makeopts */
1443 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1444 if (mem->is_separator) {
1445 continue;
1446 }
1447 mem->was_enabled = 1;
1448 print_debug("Just set %s enabled to %d\n", mem->name, mem->enabled);
1449 }
1450 /* okay, now go ahead, and mark anything listed in makeopts as NOT present */
1451 while ((member = strsep(&parse, " \n"))) {
1452 member = skip_blanks(member);
1453 if (strlen_zero(member)) {
1454 continue;
1455 }
1456 mark_as_not_present(member, category);
1457 }
1458 } else {
1459 print_debug("Category %s is positive output\n", cat->name);
1460 /* if present, it was enabled (e.g. MENUSELECT_CFLAGS, MENUSELECT_UTILS, MENUSELECT_MOH, etc. */
1461 while ((member = strsep(&parse, " \n"))) {
1462 member = skip_blanks(member);
1463 if (strlen_zero(member)) {
1464 continue;
1465 }
1466 mark_as_present(member, category);
1467 }
1468 }
1469 break;
1470 }
1471 }
1472
1473 free(buf);
1474 fclose(f);
1475
1476 return 0;
1477 }
1478
1479 /*! \brief Create the output dependencies file */
1480 static int generate_makedeps_file(void)
1481 {
1482 FILE *f;
1483 struct category *cat;
1484 struct member *mem;
1485 struct reference *dep;
1486 struct reference *use;
1487 struct dep_file *dep_file;
1488
1489 if (!(f = fopen(output_makedeps, "w"))) {
1490 fprintf(stderr, "Unable to open dependencies file (%s) for writing!\n", output_makedeps);
1491 return -1;
1492 }
1493
1494 /* Traverse all categories and members and mark which used packages were found,
1495 * skipping other members
1496 */
1497 AST_LIST_TRAVERSE(&categories, cat, list) {
1498 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1499 if (mem->is_separator) {
1500 continue;
1501 }
1502
1503 AST_LIST_TRAVERSE(&mem->uses, use, list) {
1504 if (use->member) {
1505 use->met = 0;
1506 continue;
1507 }
1508 AST_LIST_TRAVERSE(&deps_file, dep_file, list) {
1509 if ((use->met = !strcasecmp(use->name, dep_file->name))) {
1510 break;
1511 }
1512 }
1513 }
1514 }
1515 }
1516
1517 /* Traverse all categories and members and output dependencies for each member */
1518 AST_LIST_TRAVERSE(&categories, cat, list) {
1519 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1520 if (mem->is_separator) {
1521 continue;
1522 }
1523
1524 unsigned char header_printed = 0;
1525
1526 if (AST_LIST_EMPTY(&mem->deps) && AST_LIST_EMPTY(&mem->uses))
1527 continue;
1528
1529 AST_LIST_TRAVERSE(&mem->deps, dep, list) {
1530 const char *c;
1531
1532 if (dep->member) {
1533 continue;
1534 }
1535
1536 if (!header_printed) {
1537 fprintf(f, "MENUSELECT_DEPENDS_%s=", mem->name);
1538 header_printed = 1;
1539 }
1540
1541 for (c = dep->name; *c; c++)
1542 fputc(toupper(*c), f);
1543 fputc(' ', f);
1544 }
1545 AST_LIST_TRAVERSE(&mem->uses, use, list) {
1546 const char *c;
1547
1548 if (!use->met) {
1549 continue;
1550 }
1551
1552 if (!header_printed) {
1553 fprintf(f, "MENUSELECT_DEPENDS_%s=", mem->name);
1554 header_printed = 1;
1555 }
1556
1557 for (c = use->name; *c; c++)
1558 fputc(toupper(*c), f);
1559 fputc(' ', f);
1560 }
1561
1562 if (header_printed) {
1563 fprintf(f, "\n");
1564 }
1565 }
1566 }
1567
1568 fclose(f);
1569
1570 return 0;
1571 }
1572
1573 /*! \brief Create the output makeopts file that results from the user's selections */
1574 static int generate_makeopts_file(void)
1575 {
1576 FILE *f;
1577 struct category *cat;
1578 struct member *mem;
1579 struct reference *dep;
1580 struct reference *use;
1581
1582 if (!(f = fopen(output_makeopts, "w"))) {
1583 fprintf(stderr, "Unable to open build configuration file (%s) for writing!\n", output_makeopts);
1584 return -1;
1585 }
1586
1587 /* Traverse all categories and members and output them as var/val pairs */
1588 AST_LIST_TRAVERSE(&categories, cat, list) {
1589 fprintf(f, "%s=", cat->name);
1590 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1591 if (mem->is_separator) {
1592 continue;
1593 }
1594
1595 if ((!cat->positive_output && (!mem->enabled || mem->depsfailed || mem->conflictsfailed)) ||
1596 (cat->positive_output && mem->enabled && !mem->depsfailed && !mem->conflictsfailed))
1597 fprintf(f, "%s ", mem->name);
1598 }
1599 fprintf(f, "\n");
1600 }
1601
1602 /* Traverse all categories and members, and for every member that is not disabled,
1603 if it has internal dependencies (other members), list those members one time only
1604 in a special variable */
1605 fprintf(f, "MENUSELECT_BUILD_DEPS=");
1606 AST_LIST_TRAVERSE(&categories, cat, list) {
1607 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1608 if (mem->is_separator) {
1609 continue;
1610 }
1611
1612 if ((!cat->positive_output && (!mem->enabled || mem->depsfailed || mem->conflictsfailed)) ||
1613 (cat->positive_output && mem->enabled && !mem->depsfailed && !mem->conflictsfailed))
1614 continue;
1615
1616 AST_LIST_TRAVERSE(&mem->deps, dep, list) {
1617 /* we only care about dependencies between members (internal, not external) */
1618 if (!dep->member)
1619 continue;
1620 /* if this has already been output, continue */
1621 if (dep->member->build_deps_output)
1622 continue;
1623 fprintf(f, "%s ", dep->member->name);
1624 dep->member->build_deps_output = 1;
1625 }
1626 AST_LIST_TRAVERSE(&mem->uses, use, list) {
1627 /* we only care about dependencies between members (internal, not external) */
1628 if (!use->member)
1629 continue;
1630 /* if the dependency module is not going to be built, don't list it */
1631 if (!use->member->enabled)
1632 continue;
1633 /* if this has already been output, continue */
1634 if (use->member->build_deps_output)
1635 continue;
1636 fprintf(f, "%s ", use->member->name);
1637 use->member->build_deps_output = 1;
1638 }
1639 }
1640 }
1641 fprintf(f, "\n");
1642
1643 /* Output which members were disabled because of failed dependencies or conflicts */
1644 AST_LIST_TRAVERSE(&categories, cat, list) {
1645 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1646 if (mem->is_separator) {
1647 continue;
1648 }
1649
1650 if (mem->depsfailed != HARD_FAILURE && mem->conflictsfailed != HARD_FAILURE)
1651 continue;
1652
1653 if (!mem->defaultenabled || !strcasecmp(mem->defaultenabled, "yes"))
1654 fprintf(f, "MENUSELECT_DEPSFAILED=%s=%s\n", cat->name, mem->name);
1655 }
1656 }
1657
1658 fclose(f);
1659
1660 /* there is no need to process remove_on_change rules if we did not have
1661 configuration information to start from
1662 */
1663 if (!existing_config)
1664 return 0;
1665
1666 /* Traverse all categories and members and remove any files that are supposed
1667 to be removed when an item has been changed */
1668 AST_LIST_TRAVERSE(&categories, cat, list) {
1669 unsigned int had_changes = 0;
1670 char rmcommand[256] = "rm -rf ";
1671 char touchcommand[256] = "touch -c ";
1672 char *file, *buf;
1673
1674 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1675 if (mem->is_separator) {
1676 continue;
1677 }
1678
1679 if ((mem->enabled == mem->was_enabled) && !mem->was_defaulted)
1680 continue;
1681
1682 had_changes = 1;
1683
1684 if (mem->touch_on_change) {
1685 for (buf = ast_strdupa(mem->touch_on_change), file = strsep(&buf, " ");
1686 file;
1687 file = strsep(&buf, " ")) {
1688 strcpy(&touchcommand[9], file);
1689 system(touchcommand);
1690 }
1691 }
1692
1693 if (mem->remove_on_change) {
1694 for (buf = ast_strdupa(mem->remove_on_change), file = strsep(&buf, " ");
1695 file;
1696 file = strsep(&buf, " ")) {
1697 strcpy(&rmcommand[7], file);
1698 system(rmcommand);
1699 }
1700 }
1701 }
1702
1703 if (cat->touch_on_change && had_changes) {
1704 for (buf = ast_strdupa(cat->touch_on_change), file = strsep(&buf, " ");
1705 file;
1706 file = strsep(&buf, " ")) {
1707 strcpy(&touchcommand[9], file);
1708 system(touchcommand);
1709 }
1710 }
1711
1712 if (cat->remove_on_change && had_changes) {
1713 for (buf = ast_strdupa(cat->remove_on_change), file = strsep(&buf, " ");
1714 file;
1715 file = strsep(&buf, " ")) {
1716 strcpy(&rmcommand[7], file);
1717 system(rmcommand);
1718 }
1719 }
1720 }
1721
1722 return 0;
1723 }
1724
1725 /*! \brief Print out all of the information contained in our tree */
1726 static void dump_member_list(void)
1727 {
1728 #ifdef MENUSELECT_DEBUG
1729 struct category *cat;
1730 struct member *mem;
1731 struct reference *dep;
1732 struct reference *cnf;
1733
1734 AST_LIST_TRAVERSE(&categories, cat, list) {
1735 fprintf(stderr, "Category: '%s'\n", cat->name);
1736 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1737 if (mem->is_separator) {
1738 continue;
1739 }
1740
1741 fprintf(stderr, " ==>> Member: '%s' (%s)", mem->name, mem->enabled ? "Enabled" : "Disabled");
1742 fprintf(stderr, " Was %s\n", mem->was_enabled ? "Enabled" : "Disabled");
1743 if (mem->defaultenabled)
1744 fprintf(stderr, " Defaults to %s\n", !strcasecmp(mem->defaultenabled, "yes") ? "Enabled" : "Disabled");
1745 AST_LIST_TRAVERSE(&mem->deps, dep, list)
1746 fprintf(stderr, " --> Depends on: '%s'\n", dep->name);
1747 if (!AST_LIST_EMPTY(&mem->deps))
1748 fprintf(stderr, " --> Dependencies Met: %s\n", mem->depsfailed ? "No" : "Yes");
1749 AST_LIST_TRAVERSE(&mem->conflicts, cnf, list)
1750 fprintf(stderr, " --> Conflicts with: '%s'\n", cnf->name);
1751 if (!AST_LIST_EMPTY(&mem->conflicts))
1752 fprintf(stderr, " --> Conflicts Found: %s\n", mem->conflictsfailed ? "Yes" : "No");
1753 }
1754 }
1755 #endif
1756 }
1757
1758 /*! \brief Free all categories and their members */
1759 static void free_member_list(void)
1760 {
1761 struct category *cat;
1762
1763 while ((cat = AST_LIST_REMOVE_HEAD(&categories, list))) {
1764 free_category(cat);
1765 }
1766 }
1767
1768 /*! \brief Free all of the XML trees */
1769 static void free_trees(void)
1770 {
1771 struct tree *tree;
1772
1773 while ((tree = AST_LIST_REMOVE_HEAD(&trees, list))) {
1774 xmlFreeDoc(tree->root);
1775 free(tree);
1776 }
1777 }
1778
1779 /*! \brief Enable/Disable all members of a category as long as dependencies have been met and no conflicts are found */
1780 void set_all(struct category *cat, int val)
1781 {
1782 struct member *mem;
1783
1784 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1785 if (mem->enabled == val)
1786 continue;
1787
1788 if (mem->is_separator)
1789 continue;
1790
1791 if ((mem->depsfailed == HARD_FAILURE) || (mem->conflictsfailed == HARD_FAILURE))
1792 continue;
1793
1794 if (val) {
1795 enable_member(mem);
1796 } else {
1797 mem->enabled = 0;
1798 }
1799
1800 mem->was_defaulted = 0;
1801 changes_made++;
1802 }
1803
1804 while (calc_dep_failures(1, 0) || calc_conflict_failures(1, 0));
1805 }
1806
1807 int count_categories(void)
1808 {
1809 struct category *cat;
1810 int count = 0;
1811
1812 AST_LIST_TRAVERSE(&categories, cat, list)
1813 count++;
1814
1815 return count;
1816 }
1817
1818 int count_members(struct category *cat)
1819 {
1820 struct member *mem;
1821 int count = 0;
1822
1823 AST_LIST_TRAVERSE(&cat->members, mem, list)
1824 count++;
1825
1826 return count;
1827 }
1828
1829 static void print_sanity_dep_header(struct dep_file *dep_file, unsigned int *flag)
1830 {
1831 fprintf(stderr, "\n"
1832 "***********************************************************\n"
1833 " The '%s' dependency was previously satisfied but \n"
1834 " is now unsatisfied. \n",
1835 dep_file->name);
1836 *flag = 1;
1837 }
1838
1839 /*! \brief Make sure an existing menuselect.makeopts disabled everything it should have */
1840 static int sanity_check(void)
1841 {
1842 unsigned int insane = 0;
1843 struct category *cat;
1844 struct member *mem;
1845 struct reference *dep;
1846 struct reference *use;
1847 struct dep_file *dep_file;
1848 unsigned int dep_header_printed;
1849 unsigned int group_header_printed;
1850
1851 AST_LIST_TRAVERSE(&deps_file, dep_file, list) {
1852 if (!((dep_file->previously_met == DEP_FILE_MET) &&
1853 (dep_file->met == DEP_FILE_UNMET))) {
1854 continue;
1855 }
1856
1857 /* this dependency was previously met, but now is not, so
1858 warn the user about members that could be affected by it
1859 */
1860
1861 dep_header_printed = 0;
1862
1863 group_header_printed = 0;
1864 AST_LIST_TRAVERSE(&categories, cat, list) {
1865 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1866 if (mem->is_separator) {
1867 continue;
1868 }
1869
1870 if (!mem->enabled) {
1871 continue;
1872 }
1873 AST_LIST_TRAVERSE(&mem->deps, dep, list) {
1874 if (strcasecmp(dep->name, dep_file->name)) {
1875 continue;
1876 }
1877 if (!group_header_printed) {
1878 if (!dep_header_printed) {
1879 print_sanity_dep_header(dep_file, &dep_header_printed);
1880 }
1881 fprintf(stderr, "\n"
1882 " The following modules will no longer be available:\n");
1883 group_header_printed = 1;
1884 }
1885 fprintf(stderr, " %s\n", mem->name);
1886 insane = 1;
1887 }
1888 }
1889 }
1890
1891 group_header_printed = 0;
1892 AST_LIST_TRAVERSE(&categories, cat, list) {
1893 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1894 if (mem->is_separator) {
1895 continue;
1896 }
1897
1898 if (!mem->enabled) {
1899 continue;
1900 }
1901 AST_LIST_TRAVERSE(&mem->uses, use, list) {
1902 if (strcasecmp(use->name, dep_file->name)) {
1903 continue;
1904 }
1905 if (!group_header_printed) {
1906 if (!dep_header_printed) {
1907 print_sanity_dep_header(dep_file, &dep_header_printed);
1908 }
1909 fprintf(stderr, "\n"
1910 " The functionality of the following modules will\n"
1911 " be affected:\n");
1912 group_header_printed = 1;
1913 }
1914 fprintf(stderr, " %s\n", mem->name);
1915 insane = 1;
1916 }
1917 }
1918 }
1919
1920 if (dep_header_printed) {
1921 fprintf(stderr,
1922 "***********************************************************\n");
1923 }
1924 }
1925
1926 AST_LIST_TRAVERSE(&categories, cat, list) {
1927 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1928 if (mem->is_separator) {
1929 continue;
1930 }
1931
1932 if ((mem->depsfailed || mem->conflictsfailed) && mem->enabled) {
1933 fprintf(stderr, "\n"
1934 "***********************************************************\n"
1935 " The existing menuselect.makeopts file did not specify \n"
1936 " that '%s' should not be included. However, %s%s%s.\n"
1937 " \n"
1938 " Either run 'make menuselect' or remove the existing \n"
1939 " menuselect.makeopts file to resolve this issue. \n"
1940 "***********************************************************\n"
1941 "\n", mem->name,
1942 mem->depsfailed ? "\ndependencies for this module were not found" : "",
1943 mem->depsfailed && mem->conflictsfailed ? " and" : "",
1944 mem->conflictsfailed ? "\na conflict exists" : "");
1945 insane = 1;
1946 }
1947 }
1948 }
1949
1950 return insane ? -1 : 0;
1951 }
1952
1953 /*! \brief Set the forced default values if they exist */
1954 static void process_defaults(void)
1955 {
1956 struct category *cat;
1957 struct member *mem;
1958
1959 print_debug("Processing default values since config was not present\n");
1960
1961 AST_LIST_TRAVERSE(&categories, cat, list) {
1962 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1963 if (mem->is_separator) {
1964 continue;
1965 }
1966
1967 if (!mem->defaultenabled)
1968 continue;
1969
1970 if (mem->depsfailed == HARD_FAILURE)
1971 continue;
1972
1973 if (mem->conflictsfailed == HARD_FAILURE)
1974 continue;
1975
1976 if (!strcasecmp(mem->defaultenabled, "yes")) {
1977 mem->enabled = 1;
1978 mem->was_defaulted = 1;
1979 } else if (!strcasecmp(mem->defaultenabled, "no")) {
1980 mem->enabled = 0;
1981 mem->was_defaulted = 1;
1982 } else
1983 fprintf(stderr, "Invalid defaultenabled value for '%s' in category '%s'\n", mem->name, cat->name);
1984 }
1985 }
1986
1987 }
1988
1989 struct member *find_member(const char *name)
1990 {
1991 struct category *cat;
1992 struct member *mem;
1993
1994 AST_LIST_TRAVERSE(&categories, cat, list) {
1995 AST_LIST_TRAVERSE(&cat->members, mem, list) {
1996 if (mem->is_separator) {
1997 continue;
1998 }
1999
2000 if (!strcasecmp(name, mem->name)) {
2001 return mem;
2002 }
2003 }
2004 }
2005
2006 return NULL;
2007 }
2008
2009 struct category *find_category(const char *name)
2010 {
2011 struct category *cat;
2012
2013 AST_LIST_TRAVERSE(&categories, cat, list) {
2014 if (!strcasecmp(name, cat->name)) {
2015 return cat;
2016 }
2017 }
2018
2019 return NULL;
2020 }
2021
2022 static int usage(const char *argv0)
2023 {
2024 fprintf(stderr, "Usage: %s [--enable <option>] [--disable <option>]\n", argv0);
2025 fprintf(stderr, " [--enable-category <category>] [--enable-all]\n");
2026 fprintf(stderr, " [--disable-category <category>] [--disable-all] [...]\n");
2027 fprintf(stderr, " [<config-file> [...]]\n");
2028 fprintf(stderr, "Usage: %s { --check-deps | --list-options\n", argv0);
2029 fprintf(stderr, " | --list-category <category> | --category-list | --help }\n");
2030 fprintf(stderr, " [<config-file> [...]]\n");
2031 return 0;
2032 }
2033
2034 int main(int argc, char *argv[])
2035 {
2036 int res = 0;
2037 const char *list_group = NULL;
2038 unsigned int x;
2039 static struct option long_options[] = {
2040 /*
2041 * The --check-deps option is used to ask this application to check to
2042 * see if that an existing menuselect.makeopts file contains all of the
2043 * modules that have dependencies that have not been met. If this
2044 * is not the case, an informative message will be printed to the
2045 * user and the build will fail.
2046 */
2047 { "check-deps", no_argument, &check_deps, 1 },
2048 { "enable", required_argument, 0, 'e' },
2049 { "enable-category", required_argument, 0, 'E' },
2050 { "enable-all", no_argument, 0, 'a' },
2051 { "disable", required_argument, 0, 'd' },
2052 { "disable-category", required_argument, 0, 'D' },
2053 { "disable-all", no_argument, 0, 'A' },
2054 { "list-options", no_argument, &list_options, 1 },
2055 { "list-category", required_argument, 0, 'L' },
2056 { "category-list", no_argument, &list_groups, 1 },
2057 { "help", no_argument, 0, 'h' },
2058
2059 { 0, 0, 0, 0 },
2060 };
2061 int do_menu = 1, do_settings = 1;
2062 int c, option_index = 0;
2063
2064 if (open_debug()) {
2065 exit(1);
2066 }
2067
2068 LIBXML_TEST_VERSION;
2069
2070 /* Parse the input XML files to build the list of available options */
2071 if ((res = build_member_list()))
2072 exit(res);
2073
2074 /* Load module dependencies */
2075 if ((res = process_deps()))
2076 exit(res);
2077
2078 while (calc_dep_failures(0, 1) || calc_conflict_failures(0, 1));
2079
2080 while ((c = getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
2081 switch (c) {
2082 case 'L':
2083 list_group = optarg;
2084 do_settings = 0;
2085 /* Fall-through */
2086 case 'a':
2087 case 'A':
2088 case 'e':
2089 case 'E':
2090 case 'd':
2091 case 'D':
2092 do_menu = 0;
2093 break;
2094 case 'h':
2095 return usage(argv[0]);
2096 break;
2097 default:
2098 break;
2099 }
2100 }
2101
2102 if (check_deps || list_options || list_groups) {
2103 do_menu = 0;
2104 do_settings = 0;
2105 }
2106
2107 if (optind < argc) {
2108 for (x = optind; x < argc; x++) {
2109 res = parse_existing_config(argv[x]);
2110 if (!res && !strcasecmp(argv[x], OUTPUT_MAKEOPTS_DEFAULT))
2111 existing_config = 1;
2112 res = 0;
2113 }
2114 }
2115
2116 /* Dump the list produced by parsing the various input files */
2117 dump_member_list();
2118
2119 while (calc_dep_failures(0, 0) || calc_conflict_failures(0, 0));
2120
2121 if (!existing_config)
2122 process_defaults();
2123 else if (check_deps)
2124 res = sanity_check();
2125
2126 while (calc_dep_failures(0, 0) || calc_conflict_failures(0, 0));
2127
2128 print_debug("do_menu=%d, do_settings=%d\n", do_menu, do_settings);
2129
2130 if (do_menu && !res) {
2131 res = run_menu();
2132 } else if (!do_settings) {
2133 if (list_groups) {
2134 struct category *cat;
2135 AST_LIST_TRAVERSE(&categories, cat, list) {
2136 fprintf(stdout, "%s\n", cat->name);
2137 }
2138 } else if (list_options) {
2139 struct category *cat;
2140 struct member *mem;
2141 AST_LIST_TRAVERSE(&categories, cat, list) {
2142 AST_LIST_TRAVERSE(&cat->members, mem, list) {
2143 if (mem->is_separator) {
2144 continue;
2145 }
2146
2147 fprintf(stdout, "%c %-30.30s %s\n", mem->enabled ? '+' : '-', mem->name, cat->name);
2148 }
2149 }
2150 } else if (!strlen_zero(list_group)) {
2151 struct category *cat;
2152 struct member *mem;
2153 if ((cat = find_category(list_group))) {
2154 AST_LIST_TRAVERSE(&cat->members, mem, list) {
2155 if (mem->is_separator) {
2156 continue;
2157 }
2158
2159 fprintf(stdout, "%c %s\n", mem->enabled ? '+' : '-', mem->name);
2160 }
2161 }
2162 }
2163 } else if (!do_menu && do_settings) {
2164 struct member *mem;
2165 struct category *cat;
2166
2167 print_debug("Doing settings with argc=%d\n", argc);
2168
2169 /* Reset options processing */
2170 option_index = 0;
2171 optind = 1;
2172 res = 0;
2173
2174 while ((c = getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
2175 print_debug("Got option %c\n", c);
2176 switch (c) {
2177 case 'e':
2178 if (!strlen_zero(optarg)) {
2179 if ((mem = find_member(optarg))) {
2180 set_member_enabled(mem);
2181 } else {
2182 fprintf(stderr, "'%s' not found\n", optarg);
2183 res = 1;
2184 }
2185 }
2186 break;
2187 case 'E':
2188 if (!strlen_zero(optarg)) {
2189 if ((cat = find_category(optarg))) {
2190 set_all(cat, 1);
2191 } else {
2192 fprintf(stderr, "'%s' not found\n", optarg);
2193 res = 1;
2194 }
2195 }
2196 break;
2197 case 'a': /* enable-all */
2198 AST_LIST_TRAVERSE(&categories, cat, list) {
2199 set_all(cat, 1);
2200 }
2201 break;
2202 case 'd':
2203 if (!strlen_zero(optarg)) {
2204 if ((mem = find_member(optarg))) {
2205 clear_member_enabled(mem);
2206 } else {
2207 fprintf(stderr, "'%s' not found\n", optarg);
2208 res = 1;
2209 }
2210 }
2211 break;
2212 case 'D':
2213 if (!strlen_zero(optarg)) {
2214 if ((cat = find_category(optarg))) {
2215 set_all(cat, 0);
2216 } else {
2217 fprintf(stderr, "'%s' not found\n", optarg);
2218 res = 1;
2219 }
2220 }
2221 break;
2222 case 'A': /* disable-all */
2223 AST_LIST_TRAVERSE(&categories, cat, list) {
2224 set_all(cat, 0);
2225 }
2226 break;
2227 case '?':
2228 break;
2229 default:
2230 break;
2231 }
2232 }
2233 }
2234
2235 if (!res) {
2236 res = generate_makeopts_file();
2237 }
2238
2239 /* Always generate the dependencies file */
2240 if (!res) {
2241 generate_makedeps_file();
2242 }
2243
2244 /* free everything we allocated */
2245 free_deps_file();
2246 free_trees();
2247 free_member_list();
2248
2249 close_debug();
2250
2251 xmlCleanupParser();
2252
2253 exit(res);
2254 }