summaryrefslogtreecommitdiffstats
path: root/src/dnsjit.c
blob: 20b8d142e7879cc50853201ef9d5b340792f6328 (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
/*
 * Copyright (c) 2018-2024 OARC, Inc.
 * All rights reserved.
 *
 * This file is part of dnsjit.
 *
 * dnsjit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dnsjit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dnsjit.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "globals.h"
#include "core/log.h"

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

#include <stdio.h>

int main(int argc, char* argv[])
{
    lua_State* L;
    int        n, err;

    if (argc < 2) {
        fprintf(stderr, "usage: %s <file.lua> ...\n", argv[0]);
        exit(1);
    }

    L = luaL_newstate();
    luaL_openlibs(L);
    dnsjit_globals(L);

    lua_createtable(L, argc, 0);
    for (n = 0; n < argc; n++) {
        lua_pushstring(L, argv[n]);
        lua_rawseti(L, -2, n);
    }
    lua_setglobal(L, "arg");
    if ((err = luaL_loadfile(L, argv[1]))) {
        switch (err) {
        case LUA_ERRSYNTAX:
            glcritical("%s: syntax error during pre-compilation", argv[1]);
            break;
        case LUA_ERRMEM:
            glcritical("%s: memory allocation error", argv[1]);
            break;
        case LUA_ERRFILE:
            glcritical("%s: cannot open/read file", argv[1]);
            break;
        default:
            glcritical("%s: unknown error %d", argv[1], err);
            break;
        }
        return 1;
    }
    if (lua_pcall(L, 0, 0, 0)) {
        glcritical("%s: %s", argv[1], lua_tostring(L, -1));
        lua_pop(L, 1);
        return 1;
    }
    lua_close(L);

    return 0;
}