Skip to content

Commit 6ce45a9

Browse files
committed
kconfig: refactor conf_write_symbol()
I do not think 'struct conf_printer' is so useful. Add simple functions, print_symbol_for_*() to write out one symbol. Signed-off-by: Masahiro Yamada <[email protected]>
1 parent ca51b26 commit 6ce45a9

File tree

1 file changed

+57
-79
lines changed

1 file changed

+57
-79
lines changed

scripts/kconfig/confdata.c

Lines changed: 57 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <fcntl.h>
1212
#include <limits.h>
1313
#include <stdarg.h>
14+
#include <stdbool.h>
1415
#include <stdio.h>
1516
#include <stdlib.h>
1617
#include <string.h>
@@ -159,10 +160,6 @@ static int conf_touch_dep(const char *name)
159160
return 0;
160161
}
161162

162-
struct conf_printer {
163-
void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
164-
};
165-
166163
static void conf_warning(const char *fmt, ...)
167164
__attribute__ ((format (printf, 1, 2)));
168165

@@ -629,104 +626,85 @@ static void conf_write_heading(FILE *fp, const struct comment_style *cs)
629626
* This printer is used when generating the resulting configuration after
630627
* kconfig invocation and `defconfig' files. Unset symbol might be omitted by
631628
* passing a non-NULL argument to the printer.
632-
*
633629
*/
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 };
656631

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)
658634
{
659-
.print_symbol = kconfig_print_symbol,
660-
};
635+
const char *val;
636+
char *escaped = NULL;
661637

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;
670640

671-
switch (sym->type) {
672-
case S_BOOLEAN:
673-
case S_TRISTATE: {
674-
const char *suffix = "";
641+
val = sym_get_string_value(sym);
675642

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;
687648
}
688-
case S_HEX: {
689-
const char *prefix = "";
690649

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;
704653
}
705654

655+
fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
656+
657+
free(escaped);
706658
}
707659

708-
static struct conf_printer header_printer_cb =
660+
static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
709661
{
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+
}
712669

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)
715671
{
716672
const char *val;
673+
const char *sym_suffix = "";
674+
const char *val_prefix = "";
717675
char *escaped = NULL;
718676

719677
if (sym->type == S_UNKNOWN)
720678
return;
721679

722680
val = sym_get_string_value(sym);
723681

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:
725700
escaped = sym_escape_string_value(val);
726701
val = escaped;
702+
default:
703+
break;
727704
}
728705

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);
730708

731709
free(escaped);
732710
}
@@ -787,7 +765,7 @@ int conf_write_defconfig(const char *filename)
787765
goto next_menu;
788766
}
789767
}
790-
conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
768+
print_symbol_for_dotconfig(out, sym);
791769
}
792770
next_menu:
793771
if (menu->list != NULL) {
@@ -874,7 +852,7 @@ int conf_write(const char *name)
874852
need_newline = false;
875853
}
876854
sym->flags |= SYMBOL_WRITTEN;
877-
conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
855+
print_symbol_for_dotconfig(out, sym);
878856
}
879857

880858
next:
@@ -1060,8 +1038,8 @@ int conf_write_autoconf(int overwrite)
10601038
continue;
10611039

10621040
/* 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);
10651043
}
10661044
fclose(out);
10671045
fclose(out_h);

0 commit comments

Comments
 (0)