aboutsummaryrefslogtreecommitdiff
path: root/client/acronc/config.c
blob: 433fbf4dcc0383c28f4d32b2b3e6a4a64d8b06ed (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
/*
 * Created by yuuta on 7/24/22.
 */

#include "config.h"

#include <libac.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

int config_parse(const int argc, const char **argv, ac_connection_parameters_t *params) {
    if (argc <= 1) {
        fprintf(stderr, "Usage: %s [-t token] [-p port] <ID>@<server>\n"
                        "Please refer to acronc(1) for more details.\n",
                argv[0] ? "acronc" : argv[0]);
        return 64;
    }
    bool svr_set = false;
    for (int i = 1; i < argc; i++) {
        const char *arg = argv[i];
        if (arg[0] != '-') {
            if (svr_set) {
                fprintf(stderr, "Unexpected value: %s\n", arg);
                return 64;
            }
            const char *id = strtok((char *) arg, "@");
            if (!id) {
                fprintf(stderr, "Malformed argument: should be ID@server.\n");
                return 64;
            }
            const char *server = strtok(NULL, "");
            if (!server) {
                fprintf(stderr, "Malformed argument: should be ID@server.\n");
                return 64;
            }
            params->host = (char *) server;
            params->id = (char *) id;
            svr_set = true;
            continue;
        }
        char a = arg[1];
        char **value_ptr;
        char *port_str = NULL;
        switch (a) {
            case 't': {
                value_ptr = &params->token;
                goto read_value;
            }
            case 'p': {
                value_ptr = &port_str;
                goto read_value;
            }
            default: {
                fprintf(stderr, "Unexpected switch -%c.\n", a);
                return 64;
            }
        }
        read_value:
        {
            if (i == (argc - 1)) {
                fprintf(stderr, "-%c requires an argument.\n", a);
                return 64;
            }
            *value_ptr = (char *) argv[++i];
        }
        if (port_str) {
            errno = 0;
            char *endptr;
            unsigned long val = strtol(port_str, &endptr, 0);
            if (errno) {
                const int e = errno;
                fprintf(stderr, "Cannot parse port: %s", strerror(e));
                return e;
            }
            if (endptr == port_str) {
                fprintf(stderr, "Illegal port.\n");
                return 64;
            }
            if (val > UINT16_MAX) {
                fprintf(stderr, "Illegal port.\n");
                return 64;
            }
            params->port = (uint16_t) val;
            port_str = NULL;
        }
    }
    if (!params->host || !params->id) {
        fprintf(stderr, "Usage: acronc ID@server[:port]\n");
        return 64;
    }
    static char tok[1024];
    if (!params->token) {
        if (!(params->token = getenv("ACRONC_TOKEN"))) {
            printf("Token: ");
#ifdef _WIN32
            scanf("%1023[^\r\n]", tok);
#else
            scanf("%1023[^\n]", tok);
#endif
            params->token = tok;
        }
    }
    return 0;
}