|
11 | 11 | #include <fcntl.h>
|
12 | 12 | #include <limits.h>
|
13 | 13 | #include <stdarg.h>
|
| 14 | +#include <stdbool.h> |
14 | 15 | #include <stdio.h>
|
15 | 16 | #include <stdlib.h>
|
16 | 17 | #include <string.h>
|
@@ -159,10 +160,6 @@ static int conf_touch_dep(const char *name)
|
159 | 160 | return 0;
|
160 | 161 | }
|
161 | 162 |
|
162 |
| -struct conf_printer { |
163 |
| - void (*print_symbol)(FILE *, struct symbol *, const char *, void *); |
164 |
| -}; |
165 |
| - |
166 | 163 | static void conf_warning(const char *fmt, ...)
|
167 | 164 | __attribute__ ((format (printf, 1, 2)));
|
168 | 165 |
|
@@ -629,104 +626,85 @@ static void conf_write_heading(FILE *fp, const struct comment_style *cs)
|
629 | 626 | * This printer is used when generating the resulting configuration after
|
630 | 627 | * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
|
631 | 628 | * passing a non-NULL argument to the printer.
|
632 |
| - * |
633 | 629 | */
|
634 |
| -static void |
635 |
| -kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) |
636 |
| -{ |
637 |
| - |
638 |
| - switch (sym->type) { |
639 |
| - case S_BOOLEAN: |
640 |
| - case S_TRISTATE: |
641 |
| - if (*value == 'n') { |
642 |
| - bool skip_unset = (arg != NULL); |
643 |
| - |
644 |
| - if (!skip_unset) |
645 |
| - fprintf(fp, "# %s%s is not set\n", |
646 |
| - CONFIG_, sym->name); |
647 |
| - return; |
648 |
| - } |
649 |
| - break; |
650 |
| - default: |
651 |
| - break; |
652 |
| - } |
653 |
| - |
654 |
| - fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value); |
655 |
| -} |
| 630 | +enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE }; |
656 | 631 |
|
657 |
| -static struct conf_printer kconfig_printer_cb = |
| 632 | +static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, |
| 633 | + bool escape_string) |
658 | 634 | {
|
659 |
| - .print_symbol = kconfig_print_symbol, |
660 |
| -}; |
| 635 | + const char *val; |
| 636 | + char *escaped = NULL; |
661 | 637 |
|
662 |
| -/* |
663 |
| - * Header printer |
664 |
| - * |
665 |
| - * This printer is used when generating the `include/generated/autoconf.h' file. |
666 |
| - */ |
667 |
| -static void |
668 |
| -header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) |
669 |
| -{ |
| 638 | + if (sym->type == S_UNKNOWN) |
| 639 | + return; |
670 | 640 |
|
671 |
| - switch (sym->type) { |
672 |
| - case S_BOOLEAN: |
673 |
| - case S_TRISTATE: { |
674 |
| - const char *suffix = ""; |
| 641 | + val = sym_get_string_value(sym); |
675 | 642 |
|
676 |
| - switch (*value) { |
677 |
| - case 'n': |
678 |
| - break; |
679 |
| - case 'm': |
680 |
| - suffix = "_MODULE"; |
681 |
| - /* fall through */ |
682 |
| - default: |
683 |
| - fprintf(fp, "#define %s%s%s 1\n", |
684 |
| - CONFIG_, sym->name, suffix); |
685 |
| - } |
686 |
| - break; |
| 643 | + if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) && |
| 644 | + output_n != OUTPUT_N && *val == 'n') { |
| 645 | + if (output_n == OUTPUT_N_AS_UNSET) |
| 646 | + fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name); |
| 647 | + return; |
687 | 648 | }
|
688 |
| - case S_HEX: { |
689 |
| - const char *prefix = ""; |
690 | 649 |
|
691 |
| - if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X')) |
692 |
| - prefix = "0x"; |
693 |
| - fprintf(fp, "#define %s%s %s%s\n", |
694 |
| - CONFIG_, sym->name, prefix, value); |
695 |
| - break; |
696 |
| - } |
697 |
| - case S_STRING: |
698 |
| - case S_INT: |
699 |
| - fprintf(fp, "#define %s%s %s\n", |
700 |
| - CONFIG_, sym->name, value); |
701 |
| - break; |
702 |
| - default: |
703 |
| - break; |
| 650 | + if (sym->type == S_STRING && escape_string) { |
| 651 | + escaped = sym_escape_string_value(val); |
| 652 | + val = escaped; |
704 | 653 | }
|
705 | 654 |
|
| 655 | + fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val); |
| 656 | + |
| 657 | + free(escaped); |
706 | 658 | }
|
707 | 659 |
|
708 |
| -static struct conf_printer header_printer_cb = |
| 660 | +static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym) |
709 | 661 | {
|
710 |
| - .print_symbol = header_print_symbol, |
711 |
| -}; |
| 662 | + __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true); |
| 663 | +} |
| 664 | + |
| 665 | +static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym) |
| 666 | +{ |
| 667 | + __print_symbol(fp, sym, OUTPUT_N_NONE, true); |
| 668 | +} |
712 | 669 |
|
713 |
| -static void conf_write_symbol(FILE *fp, struct symbol *sym, |
714 |
| - struct conf_printer *printer, void *printer_arg) |
| 670 | +static void print_symbol_for_c(FILE *fp, struct symbol *sym) |
715 | 671 | {
|
716 | 672 | const char *val;
|
| 673 | + const char *sym_suffix = ""; |
| 674 | + const char *val_prefix = ""; |
717 | 675 | char *escaped = NULL;
|
718 | 676 |
|
719 | 677 | if (sym->type == S_UNKNOWN)
|
720 | 678 | return;
|
721 | 679 |
|
722 | 680 | val = sym_get_string_value(sym);
|
723 | 681 |
|
724 |
| - if (sym->type == S_STRING) { |
| 682 | + switch (sym->type) { |
| 683 | + case S_BOOLEAN: |
| 684 | + case S_TRISTATE: |
| 685 | + switch (*val) { |
| 686 | + case 'n': |
| 687 | + return; |
| 688 | + case 'm': |
| 689 | + sym_suffix = "_MODULE"; |
| 690 | + /* fall through */ |
| 691 | + default: |
| 692 | + val = "1"; |
| 693 | + } |
| 694 | + break; |
| 695 | + case S_HEX: |
| 696 | + if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X')) |
| 697 | + val_prefix = "0x"; |
| 698 | + break; |
| 699 | + case S_STRING: |
725 | 700 | escaped = sym_escape_string_value(val);
|
726 | 701 | val = escaped;
|
| 702 | + default: |
| 703 | + break; |
727 | 704 | }
|
728 | 705 |
|
729 |
| - printer->print_symbol(fp, sym, val, printer_arg); |
| 706 | + fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix, |
| 707 | + val_prefix, val); |
730 | 708 |
|
731 | 709 | free(escaped);
|
732 | 710 | }
|
@@ -787,7 +765,7 @@ int conf_write_defconfig(const char *filename)
|
787 | 765 | goto next_menu;
|
788 | 766 | }
|
789 | 767 | }
|
790 |
| - conf_write_symbol(out, sym, &kconfig_printer_cb, NULL); |
| 768 | + print_symbol_for_dotconfig(out, sym); |
791 | 769 | }
|
792 | 770 | next_menu:
|
793 | 771 | if (menu->list != NULL) {
|
@@ -874,7 +852,7 @@ int conf_write(const char *name)
|
874 | 852 | need_newline = false;
|
875 | 853 | }
|
876 | 854 | sym->flags |= SYMBOL_WRITTEN;
|
877 |
| - conf_write_symbol(out, sym, &kconfig_printer_cb, NULL); |
| 855 | + print_symbol_for_dotconfig(out, sym); |
878 | 856 | }
|
879 | 857 |
|
880 | 858 | next:
|
@@ -1060,8 +1038,8 @@ int conf_write_autoconf(int overwrite)
|
1060 | 1038 | continue;
|
1061 | 1039 |
|
1062 | 1040 | /* write symbols to auto.conf and autoconf.h */
|
1063 |
| - conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1); |
1064 |
| - conf_write_symbol(out_h, sym, &header_printer_cb, NULL); |
| 1041 | + print_symbol_for_autoconf(out, sym); |
| 1042 | + print_symbol_for_c(out_h, sym); |
1065 | 1043 | }
|
1066 | 1044 | fclose(out);
|
1067 | 1045 | fclose(out_h);
|
|
0 commit comments