From 0e28187dca6fe44c5a319045012d4e9927c6158f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sun, 10 May 2015 16:02:25 +0200 Subject: Add saving and loading of dot_config file in permute program --- scripts/permute/Makefile | 6 ++- scripts/permute/dotconf.c | 104 +++++++++++++++++++++++++++++++++++++++++++++ scripts/permute/dotconf.h | 18 ++++++++ scripts/permute/menudata.c | 59 +++++++++++++++++++++++++ scripts/permute/menudata.h | 5 ++- scripts/permute/permute.c | 88 ++++++++++++++++++++++++++++---------- 6 files changed, 255 insertions(+), 25 deletions(-) create mode 100644 scripts/permute/dotconf.c create mode 100644 scripts/permute/dotconf.h create mode 100644 scripts/permute/menudata.c diff --git a/scripts/permute/Makefile b/scripts/permute/Makefile index 96879dd..531b484 100644 --- a/scripts/permute/Makefile +++ b/scripts/permute/Makefile @@ -7,7 +7,9 @@ all: permute KCONFIG_PREFIX = ../shared/kconfig include $(KCONFIG_PREFIX)/files.mk -SRC = permute.c +SRC = permute.c \ + menudata.c \ + dotconf.c OBJ = $(patsubst %.c,%.o,$(SRC)) CFLAGS = -O0 -Wall -ggdb -DDEBUG INCLUDES = -I../shared @@ -20,4 +22,4 @@ permute: $(OBJ) $(KCONFIG_OBJ) clean:: $(RM) $(OBJ) - $(RM) setpermute + $(RM) permute diff --git a/scripts/permute/dotconf.c b/scripts/permute/dotconf.c new file mode 100644 index 0000000..aba607e --- /dev/null +++ b/scripts/permute/dotconf.c @@ -0,0 +1,104 @@ +#include "dotconf.h" + +bool dotconfig_read(void) { + FILE *f; + f = fopen(DOTCONFIG_FILE, "r"); + if (f == NULL) + return false; + + char buffer[READBUFFER_SIZE]; + while (fgets(buffer, READBUFFER_SIZE, f) != NULL) { + if (buffer[0] == '\0' || buffer[1] == '\0') + continue; + if (buffer[0] == '#') { + } else { + char *wstr = buffer + 7; + char *end = strchr(wstr, '='); + *end = '\0'; + struct symbol *sym = sym_find(wstr); + if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE) + continue; + struct property *prop; + for_all_prompts(sym, prop) { + if (prop->menu->data == NULL) + prop->menu->data = menudata_new(); + } + } + } + + fclose(f); + + struct menu *wmenu; + struct menu **stack; + size_t stack_size = 2, stack_pos = 0; + stack = malloc(stack_size * sizeof(struct menu *)); + wmenu = rootmenu.list; + while (wmenu != NULL) { + if (wmenu->list != NULL) { + if (stack_pos >= stack_size) { + stack_size *= 2; + stack = realloc(stack, stack_size * sizeof(struct menu *)); + } + stack[stack_pos++] = wmenu->list; + } + if (wmenu->data == NULL) { + if (wmenu->sym == NULL || wmenu->sym->name == NULL) { + wmenu->data = menudata_new(); + } else { + wmenu->data = menudata_new(); + menudata_set_permute(wmenu, true); + } + } + wmenu = wmenu->next; + if (wmenu == NULL && stack_pos > 0) + wmenu = stack[--stack_pos]; + } + while (wmenu != NULL) { + if (wmenu->list != NULL) + stack[stack_pos++] = wmenu->list; + if (wmenu->data == NULL) { + if (wmenu->sym == NULL || wmenu->sym->name == NULL) { + wmenu->data = menudata_new(); + } else { + wmenu->data = menudata_new(); + menudata_set_permute(wmenu, true); + } + } + wmenu = wmenu->next; + if (wmenu == NULL && stack_pos > 0) + wmenu = stack[--stack_pos]; + } + + return true; +} + +void dotconfig_write(void) { + FILE *f; + f = fopen(DOTCONFIG_FILE, "w"); + + struct symbol *sym; + struct property *prop; + int i; + unsigned variable = 0, fixed = 0; + for_all_symbols(i, sym) + if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) + && sym->name != NULL) { + for_all_prompts(sym, prop) { + if (prop->menu->data == NULL + || !((struct menudata *) prop->menu->data)->permute) { + fprintf(f, "CONFIG_%s=%s\n", sym->name, + sym_get_tristate_value(sym) == no ? "n" : "y"); + fixed++; + break; + } else { + variable++; + printf("%s\n=%s\n", sym->name, + sym_get_tristate_value(sym) == no ? "n" : "y"); + } + } + } + + printf("Variable: %d, Fixed: %d\n", variable, fixed); + + fclose(f); +} diff --git a/scripts/permute/dotconf.h b/scripts/permute/dotconf.h new file mode 100644 index 0000000..8933f54 --- /dev/null +++ b/scripts/permute/dotconf.h @@ -0,0 +1,18 @@ +#include +#include +#include +#include + +#include +#include "menudata.h" + +#ifndef _DOTCONF_H_ +#define _DOTCONF_H_ + +#define DOTCONFIG_FILE "../dot_config" +#define READBUFFER_SIZE 128 + +bool dotconfig_read(void); +void dotconfig_write(void); + +#endif /* _DOTCONF_H_ */ diff --git a/scripts/permute/menudata.c b/scripts/permute/menudata.c new file mode 100644 index 0000000..c941552 --- /dev/null +++ b/scripts/permute/menudata.c @@ -0,0 +1,59 @@ +#include "menudata.h" + +struct menudata *menudata_new(void) { + struct menudata *rtn; + rtn = calloc(1, sizeof(struct menudata)); + return rtn; +} + +void menudata_set_permute(struct menu *m, bool perm) { + ((struct menudata *) m->data)->permute = perm; + ((struct menudata *) m->data)->subpermute = perm; + struct menu *prnt; + for (prnt = m->parent; prnt != NULL; prnt = prnt->parent) { + menudata_cal(prnt); + } + + struct menu **stack; + size_t stack_size = 2, stack_pos = 0; + stack = malloc(stack_size * sizeof(struct menu *)); + + m = m->list; + while (m != NULL) { + if (m->data == NULL) + m->data = menudata_new(); + ((struct menudata *) m->data)->permute = perm; + ((struct menudata *) m->data)->subpermute = perm; + + if (m->list != NULL) { + if (stack_pos >= stack_size) { + stack_size *= 2; + stack = realloc(stack, stack_size * sizeof(struct menu *)); + } + stack[stack_pos++] = m->list; + } + + m = m->next; + if (m == NULL && stack_pos > 0) + m = stack[--stack_pos]; + } +} + +void menudata_cal(struct menu *m) { + bool perm = true; + bool subperm = false; + struct menu *w; + for (w = m->list; w != NULL; w = w->next) { + if (w->data != NULL && (((struct menudata *) w->data)->permute + || ((struct menudata *) w->data)-> + subpermute)) { + subperm = true; + } else { + perm = false; + } + } + if (m->data == NULL) + m->data = menudata_new(); + ((struct menudata *) m->data)->permute = perm && subperm; + ((struct menudata *) m->data)->subpermute = subperm; +} diff --git a/scripts/permute/menudata.h b/scripts/permute/menudata.h index 1369106..5b3c46b 100644 --- a/scripts/permute/menudata.h +++ b/scripts/permute/menudata.h @@ -1,6 +1,7 @@ #include #include #include + #include #ifndef _MENUDATA_H_ @@ -11,6 +12,8 @@ struct menudata { bool subpermute; }; -bool menudata_cal_subpermute(struct menu *m); +struct menudata *menudata_new(void); +void menudata_set_permute(struct menu *m, bool perm); +void menudata_cal(struct menu *m); #endif /* _MENUDATA_H_ */ diff --git a/scripts/permute/permute.c b/scripts/permute/permute.c index fc1221c..e3c6810 100644 --- a/scripts/permute/permute.c +++ b/scripts/permute/permute.c @@ -1,19 +1,22 @@ #include #include +#include #include #include #include - #include #include #include - +#include "menudata.h" +#include "dotconf.h" #define INPUT_SIZE 1024 - int verbose_level; -char *file, *folder; +char *file; -void printf_help(); +bool reqsave; + +void printf_help(void); +void exit_save(void); int main(int argc, char **argv) { verbose_level = 1; @@ -23,8 +26,6 @@ int main(int argc, char **argv) { verbose_level++; else if (file == NULL) file = argv[i]; - else if (folder == NULL) - folder = argv[i]; else { Eprintf("Unknown parameter: %s\n", argv[i]); exit(1); @@ -35,10 +36,6 @@ int main(int argc, char **argv) { Eprintf("No Kconfig input file specified\n"); exit(2); } - if (folder == NULL) { - Eprintf("No output folder specified\n"); - exit(3); - } setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); @@ -47,27 +44,44 @@ int main(int argc, char **argv) { conf_parse(file); conf_read(".config"); + if (!dotconfig_read()) + reqsave = true; + else + reqsave = false; + struct menu *wroot, *wmenu, *wwmenu; wroot = &rootmenu; - unsigned menucount; + int menucount; char *input; int inputi; input = malloc(INPUT_SIZE * sizeof(char)); printf_help(); + rootmenu.data = menudata_new(); while (1) { printf("\n%s\n", wroot->prompt->text); wmenu = wroot->list; menucount = 0; while (wmenu != NULL) { - if (wmenu->prompt != NULL) { - if (wmenu->list == NULL) - printf("%3d: %s\n", ++menucount, - wmenu->prompt->text); + if (wmenu->prompt != NULL + && (wmenu->sym == NULL || wmenu->sym->type == S_BOOLEAN + || wmenu->sym->type == S_TRISTATE + || wmenu->sym->type == S_OTHER)) { + if (wmenu->data == NULL) + wmenu->data = menudata_new(); + printf("%3d", ++menucount); + if (((struct menudata *) wmenu->data)->permute) { + printf(""); + } else if (((struct menudata *) wmenu->data)->subpermute) { + printf("<->"); + } else { + printf(""); + } + if (wmenu->sym == NULL || sym_is_choice(wmenu->sym)) + printf(" %s -->\n", wmenu->prompt->text); else - printf("%3d: %s -->\n", ++menucount, - wmenu->prompt->text); + printf(" %s\n", wmenu->prompt->text); } wmenu = wmenu->next; } @@ -85,7 +99,11 @@ int main(int argc, char **argv) { int y = 0; wwmenu = wroot->list; while (1) { - if (wwmenu->prompt != NULL) + if (wwmenu->prompt != NULL + && (wwmenu->sym == NULL + || wwmenu->sym->type == S_BOOLEAN + || wwmenu->sym->type == S_TRISTATE + || wwmenu->sym->type == S_OTHER)) y++; if (y >= inputi) break; @@ -93,8 +111,14 @@ int main(int argc, char **argv) { } break; case 'u': - wmenu = wmenu->parent; + if (wroot->parent == NULL) + goto input; + wroot = wroot->parent; break; + case 's': + reqsave = false; + dotconfig_write(); + printf("Configuration saved...\n"); case 'r': break; case 'q': @@ -106,27 +130,47 @@ int main(int argc, char **argv) { } switch (input[0]) { case 'e': - wroot = wwmenu; + if (wwmenu->list != NULL) + wroot = wwmenu; + else + goto input; break; case 'v': + menudata_set_permute(wwmenu, true); + reqsave = true; break; case 'f': + menudata_set_permute(wwmenu, false); + reqsave = true; break; } } quit: + exit_save(); return 0; } -void printf_help() { +void printf_help(void) { printf("As input are accepted these commands:\n"); printf(" e Enter menu according to number.\n"); printf(" u Go to previous upper menu.\n"); printf(" v Set menu and all its submenus as variable.\n"); printf(" f Set menu and all its submenus as fixed.\n"); + printf(" s Save settings.\n"); printf(" r Reprint menu.\n"); printf(" h Prints this text.\n"); printf(" q Quit this program\n"); } + +void exit_save(void) { + if (!reqsave) + return; + printf("Unsaved chages. Save (y/N): "); + int ch = fgetc(stdin); + if (ch == 'y' || ch == 'Y') { + dotconfig_write(); + printf("Configuration saved.\n"); + } +} -- cgit v1.2.3