aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 9d3eb5c50684d0b026e3cbda496acf592276ab1e (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
136
137
138
139
/*
 * blogc: A blog compiler.
 * Copyright (C) 2015 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file COPYING.
 */

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

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "utils/utils.h"
#include "source-parser.h"
#include "template-parser.h"
#include "loader.h"
#include "renderer.h"
#include "error.h"


static void
blogc_print_help(void)
{
    printf(
        "usage:\n"
        "    blogc [-h] -t TEMPLATE [-o OUTPUT] SOURCE [SOURCE ...] - A blog compiler.\n"
        "\n"
        "positional arguments:\n"
        "    SOURCE       source file(s)\n"
        "\n"
        "optional arguments:\n"
        "    -h, --help   show this help message and exit\n"
        "    -t TEMPLATE  template file\n"
        "    -o OUTPUT    output file\n");
}


static void
blogc_print_usage(void)
{
    printf("usage: blogc [-h] -t TEMPLATE [-o OUTPUT] SOURCE [SOURCE ...]\n");
}


int
main(int argc, char **argv)
{
    int rv = 0;

    char *template = NULL;
    char *output = NULL;
    b_slist_t *sources = NULL;

    for (int i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
                case 'h':
                    blogc_print_help();
                    goto cleanup;
                case 't':
                    if (i + 1 < argc)
                        template = b_strdup(argv[++i]);
                    break;
                case 'o':
                    if (i + 1 < argc)
                        output = b_strdup(argv[++i]);
                    break;
            }
        }
        else
            sources = b_slist_append(sources, b_strdup(argv[i]));
    }

    if (template == NULL) {
        blogc_print_usage();
        fprintf(stderr, "blogc: error: argument -t is required\n");
        rv = 2;
        goto cleanup;
    }

    if (b_slist_length(sources) == 0) {
        blogc_print_usage();
        fprintf(stderr, "blogc: error: at least one source file is required\n");
        rv = 2;
        goto cleanup;
    }

    blogc_error_t *err = NULL;

    b_slist_t* l = blogc_template_parse_from_file(template, &err);
    if (err != NULL) {
        blogc_error_print(err);
        goto cleanup2;
    }

    b_slist_t *s = blogc_source_parse_from_files(sources, &err);
    if (err != NULL) {
        blogc_error_print(err);
        goto cleanup3;
    }

    char *out = blogc_render(l, s);

    bool write_to_stdout = (output == NULL || (0 == strcmp(output, "-")));

    FILE *fp = stdout;
    if (!write_to_stdout) {
        fp = fopen(output, "w");
        if (fp == NULL) {
            fprintf(stderr, "blogc: error: failed to open output file (%s): %s\n",
                output, strerror(errno));
            rv = 2;
            goto cleanup4;
        }
    }

    fprintf(fp, "%s", out);

    if (!write_to_stdout)
        fclose(fp);

cleanup4:
    free(out);
cleanup3:
    b_slist_free_full(s, (b_free_func_t) b_trie_free);
cleanup2:
    blogc_template_free_stmts(l);
    blogc_error_free(err);
cleanup:
    free(template);
    free(output);
    b_slist_free_full(sources, free);
    return rv;
}