aboutsummaryrefslogtreecommitdiff
path: root/lib/user_input.c
blob: 589a084519d34706d4ef24c9021fb21871488d38 (plain) (blame)
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
#include "spm.h"

int spm_prompt_user(const char *msg, int empty_input_is_yes) {
    int status_choice = 0;
    char ch_yes = 'y';
    char ch_no = 'n';

    if (empty_input_is_yes) {
        ch_yes = 'Y';
    } else {
        ch_no = 'N';
    }

    printf("\n%s [%c/%c] ", msg, ch_yes, ch_no);

    char ch[2] = {0,};
    int input_count = 0;
    while (scanf("%c", ch) == 1) {
        ch[1] = '\0';

        if (input_count != 0) {
            input_count = 0;
            ch[0] = '\0';
            continue;
        }

        if ((isempty(ch) && empty_input_is_yes != 0)) {
            return 1;
        } else if ((isempty(ch) && empty_input_is_yes == 0)) {
            return 0;
        } else if (tolower(ch[0]) == tolower(ch_yes)) { // Yes
            return 1;
        } else if (tolower(ch[0]) == tolower(ch_no)) { // No
            return 0;
        } else {
            printf("Please answer '%c' or '%c'...\n", tolower(ch_yes), tolower(ch_no));
        }
        input_count++;
    }
    return -1;
}