summaryrefslogtreecommitdiff
path: root/src/main.c
blob: bd303e67faff09389715c0a1b9c04b9579b7fd04 (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
#include <stdio.h>
#include <stdlib.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static
#include "optparse.h"

int luaopen_hpdf(lua_State *L);

extern const char mkpdf_lua[];
extern const unsigned long mkpdf_lua_size;

void usage(const char *program)
{
    fprintf(stderr, "%s [--version] [--help] <script-path>\n", program);
}

int main(int argc, char **argv)
{
    lua_State *L;
    int rc = 0;
    int option = 0;
    struct optparse optparse;
    const char *script;

    const struct optparse_long options[] =
    {
        { "version", 'v', OPTPARSE_NONE },
        { "help", 'h', OPTPARSE_NONE },
        { NULL }
    };

    optparse_init(&optparse, argv);

    while ((option = optparse_long(&optparse, options, NULL)) != -1)
    {
        switch (option)
        {
            case 'v':
                printf("%s\n", MKPDF_VERSION);
                return 0;

            case 'h':
                usage(argv[0]);
                return 1;

            case '?':
                fprintf(stderr, "%s\n", optparse.errmsg);
                usage(argv[0]);
                return 1;
        }
    }

    if ((script = optparse_arg(&optparse)) == NULL)
    {
        fprintf(stderr, "error: no script given\n\n");
        usage(argv[0]);
        return 1;
    }

    L = luaL_newstate();

    luaL_openlibs(L);

    lua_getglobal(L, "package");
    lua_getfield(L, -1, "preload");
    lua_pushcfunction(L, luaopen_hpdf);
    lua_setfield(L, -2, "hpdf");
    lua_pop(L, 2);

    if (luaL_dostring(L, mkpdf_lua))
    {
        fprintf(stderr, "%s\n", lua_tostring(L, -1));
        rc = 1;
    }

    if (luaL_dofile(L, argv[1]))
    {
        fprintf(stderr, "%s\n", lua_tostring(L, -1));
        rc = 1;
    }

    lua_close(L);

    return rc;
}