about summary refs log tree commit diff
path: root/src/main.c
blob: 3ab2608de2ccd41e647eb3cb4bfcb5ad221aca4b (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
#include "main.h"

#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>

#include "platform/getch.h"
#include "3cl.h"
#include "readfile.h"


const char *program_name;

static const char *const usage_message =
    "usage: %s [-h] file\n";
static const char *const usage_description =
    "ccl language interpreter\n"
    "Arguments:\n"
    "  file         file to execute\n"
    "Options\n"
    "  -h, --help   show this help message and quit\n";

static struct option long_options[] =
{
    {"help",     no_argument, NULL, 'b'},
    {0}
};

noreturn void usage(bool full)
{
    printf(usage_message, program_name);
    if (full) printf(usage_description);
    exit(full ? 0 : 1);
}

int main(int argc, char **argv)
{
    program_name = argv[0];
    getch_init();

    int ch;
    while ((ch = getopt_long(argc, argv, "h", long_options, NULL)) != EOF)
    {
        switch (ch)
        {
        case 'h':
            usage(true);
            break;
        default:
            usage(false);
            break;
        }
    }
    if (argv[optind] == NULL)
        usage(false);

    char *code = readfile(argv[optind]);
    struct CCL ccl;
    ccl_init(&ccl, code, getch, (void(*)(int))putchar);
    ccl_exec(&ccl);
    free(code);
    ccl_free(&ccl);
    return 0;
}