aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make/main.c
blob: c27c3021644f166f9c3bc6feed0a5d96cdfe1e9f (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../common/error.h"
#include "../common/utils.h"
#include "ctx.h"
#include "rules.h"

// is this beautiful? no. but there's no point in passing something that is
// essentially global to every function in exec.c
const char *argv0 = NULL;


static void
print_help(void)
{
    printf(
        "usage:\n"
        "    blogc-make [-h] [-v] [-V] [-f FILE] [RULE ...]\n"
        "               - A simple build tool for blogc.\n"
        "\n"
        "positional arguments:\n"
        "    RULE          build rule(s) to run. can include comma-separated\n"
        "                  key-value pairs of rule settings (default: all)\n"
        "\n"
        "optional arguments:\n"
        "    -h            show this help message and exit\n"
        "    -v            show version and exit\n"
        "    -V            be verbose when executing commands\n"
        "    -f FILE       read FILE as blogcfile\n");
    bm_rule_print_help();
}


static void
print_usage(void)
{
    printf("usage: blogc-make [-h] [-v] [-V] [-f FILE] [RULE ...]\n");
}


int
#ifdef MAKE_EMBEDDED
bm_main(int argc, char **argv)
#else
main(int argc, char **argv)
#endif
{
    setlocale(LC_ALL, "");

    // i really hope that no operating system omits argv[0], but ...
    if (argc > 0) {
        argv0 = argv[0];
    }

    int rv = 0;
    bc_error_t *err = NULL;

    bc_slist_t *rules = NULL;
    bool verbose = false;
    char *blogcfile = NULL;
    bm_ctx_t *ctx = NULL;

    for (unsigned int i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
                case 'h':
                    print_help();
                    goto cleanup;
                case 'v':
                    printf("%s\n", PACKAGE_STRING);
                    goto cleanup;
                case 'V':
                    verbose = true;
                    break;
                case 'f':
                    if (argv[i][2] != '\0')
                        blogcfile = bc_strdup(argv[i] + 2);
                    else if (i + 1 < argc)
                        blogcfile = bc_strdup(argv[++i]);
                    break;
#ifdef MAKE_EMBEDDED
                case 'm':
                    // no-op, for embedding into blogc binary.
                    break;
#endif
                default:
                    print_usage();
                    fprintf(stderr, "blogc-make: error: invalid argument: "
                        "-%c\n", argv[i][1]);
                    rv = 3;
                    goto cleanup;
            }
        }
        else {
            rules = bc_slist_append(rules, bc_strdup(argv[i]));
        }
    }

    if (rules == NULL) {
        rules = bc_slist_append(rules, bc_strdup("all"));
    }

    ctx = bm_ctx_new(NULL, blogcfile ? blogcfile : "blogcfile", &err);
    if (err != NULL) {
        bc_error_print(err, "blogc-make");
        rv = 3;
        goto cleanup;
    }
    ctx->verbose = verbose;

    rv = bm_rule_executor(ctx, rules, verbose);

cleanup:

    bc_slist_free_full(rules, free);
    free(blogcfile);
    bm_ctx_free(ctx);
    bc_error_free(err);

    return rv;
}