summaryrefslogtreecommitdiff
path: root/core/defconf.c
blob: 53e2bdfddf80e5d19ccd38115332927d375bb50a (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
/*
 * Copyright (c) 2026, Apollo Telephone Laboratories.
 * Provided under the BSD-3 clause.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "defconf/state.h"
#include "defconf/parser.h"

/* Globals */
static char *config_path = NULL;

static void
help(void)
{
    printf("usage: ./defconf [flags]\n");
    printf("[-h]    Display this help menu\n");
    printf("[-c]    Target config file\n");
}

static int
defconf_run(void)
{
    struct defconf_state state;
    int error;

    error = defconf_state_init(config_path, &state);
    if (error < 0) {
        return error;
    }

    /* Begin parsing the config */
    if ((error = parse_begin(&state)) < 0) {
        defconf_state_destroy(&state);
        return error;
    }

    defconf_state_destroy(&state);
    return 0;
}

int
main(int argc, char **argv)
{
    int opt, retval;

    if (argc < 2) {
        printf("fatal: too few arguments\n");
        help();
        return -1;
    }

    while ((opt = getopt(argc, argv, "hc:")) != -1) {
        switch (opt) {
        case 'h':
            help();
            return -1;
        case 'c':
            if ((config_path = strdup(optarg)) == NULL) {
                printf("fatal: out of memory\n");
                return -1;
            }

            break;
        }
    }

    if (config_path == NULL) {
        printf("fatal: expected config path\n");
        return -1;
    }

    retval = defconf_run();
    free(config_path);
    return retval;
}