aboutsummaryrefslogtreecommitdiff
path: root/scripts/permute/dotconf.c
blob: bf8e067d7de3f0dfd041ef1dd8d4348440324ff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "dotconf.h"

void dotconfig_read(bool * reqsave) {
    FILE *f;
    f = fopen(DOTCONFIG_FILE, "r");
    if (f == NULL) {
        *reqsave = true;
        return;
    }

    char buffer[READBUFFER_SIZE];
    while (fgets(buffer, READBUFFER_SIZE, f) != NULL) {
        if (buffer[0] == '\0' || buffer[1] == '\0')
            continue;
        if (buffer[0] != '#') {
            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;
            if ((sym_get_tristate_value(sym) == yes && *(end + 1) != 'y')
                || (sym_get_tristate_value(sym) == no
                    && *(end + 1) != 'n'))
                *reqsave = true;
            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];
    }
}

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++;
                if (verbose_level > 1)
                    printf("%s=%s\n", sym->name,
                           sym_get_tristate_value(sym) == no ? "n" : "y");
            }
        }
    }

    printf("Variable: %d, Fixed: %d\n", variable, fixed);

    fclose(f);
}