]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/permute/permute.c
Add license
[linux-conf-perf.git] / scripts / permute / permute.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdbool.h>
4 #include <string.h>
5 #include <locale.h>
6 #include <libintl.h>
7 #include <kconfig/lkc.h>
8 #include <macros.h>
9 #include <build_files.h>
10 #include "menudata.h"
11 #include "dotconf.h"
12
13 #define INPUT_SIZE 1024
14
15 int verbose_level;
16 char *file;
17
18 bool reqsave;
19
20 void printf_help(void);
21 void exit_save(void);
22
23 int main(int argc, char **argv) {
24     verbose_level = 1;
25     int i;
26     for (i = 1; i < argc; i++) {
27         if (!strcmp(argv[i], "-v"))
28             verbose_level++;
29         else if (file == NULL)
30             file = argv[i];
31         else {
32             Eprintf("Unknown parameter: %s\n", argv[i]);
33             exit(1);
34         }
35     }
36
37     if (file == NULL) {
38         Eprintf("No Kconfig input file specified\n");
39         exit(2);
40     }
41
42     setlocale(LC_ALL, "");
43     bindtextdomain(PACKAGE, LOCALEDIR);
44     textdomain(PACKAGE);
45
46     conf_parse(file);
47     conf_read(".config");
48
49     dotconfig_read(&reqsave);
50
51     struct menu *wroot, *wmenu, *wwmenu;
52     wroot = &rootmenu;
53     int menucount;
54     char *input;
55     int inputi;
56     input = malloc(INPUT_SIZE * sizeof(char));
57
58     printf_help();
59
60     rootmenu.data = menudata_new();
61     while (1) {
62         printf("\n%s\n", wroot->prompt->text);
63         wmenu = wroot->list;
64         menucount = 0;
65         while (wmenu != NULL) {
66             if (wmenu->prompt != NULL
67                 && (wmenu->sym == NULL || wmenu->sym->type == S_BOOLEAN
68                     || wmenu->sym->type == S_TRISTATE
69                     || wmenu->sym->type == S_OTHER)) {
70                 if (wmenu->data == NULL)
71                     wmenu->data = menudata_new();
72                 printf("%3d", ++menucount);
73                 if (((struct menudata *) wmenu->data)->permute) {
74                     printf("<O>");
75                 } else if (((struct menudata *) wmenu->data)->subpermute) {
76                     printf("<->");
77                 } else {
78                     printf("<X>");
79                 }
80                 if (wmenu->sym == NULL || sym_is_choice(wmenu->sym))
81                     printf(" %s -->\n", wmenu->prompt->text);
82                 else
83                     printf(" %s\n", wmenu->prompt->text);
84             }
85             wmenu = wmenu->next;
86         }
87
88       input:
89         printf("Input: ");
90         fgets(input, INPUT_SIZE, stdin);
91         switch (input[0]) {
92         case 'e':
93         case 'v':
94         case 'f':
95             inputi = atoi(input + 1);
96             if (inputi <= 0 && inputi > menucount)
97                 goto input;
98             int y = 0;
99             wwmenu = wroot->list;
100             while (1) {
101                 if (wwmenu->prompt != NULL
102                     && (wwmenu->sym == NULL
103                         || wwmenu->sym->type == S_BOOLEAN
104                         || wwmenu->sym->type == S_TRISTATE
105                         || wwmenu->sym->type == S_OTHER))
106                     y++;
107                 if (y >= inputi)
108                     break;
109                 wwmenu = wwmenu->next;
110             }
111             break;
112         case 'u':
113             if (wroot->parent == NULL)
114                 goto input;
115             wroot = wroot->parent;
116             break;
117         case 's':
118             reqsave = false;
119             dotconfig_write();
120             printf("Configuration saved...\n");
121         case 'r':
122             break;
123         case 'q':
124             goto quit;
125         case 'h':
126             printf_help();
127         default:
128             goto input;
129         }
130         switch (input[0]) {
131         case 'e':
132             if (wwmenu->list != NULL)
133                 wroot = wwmenu;
134             else
135                 goto input;
136             break;
137         case 'v':
138             if (input[1] == 'a') {
139                 menudata_set_all_permute(wwmenu, true);
140             } else {
141                 menudata_set_permute(wwmenu, true);
142             }
143             reqsave = true;
144             break;
145         case 'f':
146             menudata_set_permute(wwmenu, false);
147             reqsave = true;
148             break;
149         }
150     }
151
152   quit:
153     exit_save();
154
155     return 0;
156 }
157
158 void printf_help(void) {
159     printf("As input are accepted these commands:\n");
160     printf("  e <NUM>  Enter menu according to number.\n");
161     printf("  u        Go to previous upper menu.\n");
162     printf("  v <NUM>  Set config as variable.\n");
163     printf("  va <NUM> Set menu and all its submenus as variable.\n");
164     printf("  f <NUM>  Set menu and all its submenus as fixed.\n");
165     printf("  s        Save settings.\n");
166     printf("  r        Reprint menu.\n");
167     printf("  h        Prints this text.\n");
168     printf("  q        Quit this program\n");
169 }
170
171 void exit_save(void) {
172     if (!reqsave)
173         return;
174     printf("Unsaved chages. Save (y/N): ");
175     int ch = fgetc(stdin);
176     if (ch == 'y' || ch == 'Y') {
177         dotconfig_write();
178         printf("Configuration saved.\n");
179     }
180 }