summaryrefslogtreecommitdiff
path: root/src/granger/src/main.c
blob: 3d1b28f3b6f33bb33f83f09c98769b43b2202f29 (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
/*
 * This file is part of Granger.
 * Copyright (c) 2016 Jeff Kent <jeff@jkent.net>
 *
 * Granger 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.
 *
 * Granger 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 Granger.  If not, see <http://www.gnu.org/licenses/>.
 */

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

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "premake/premake.h"

#if defined(PLATFORM_WINDOWS)
#include "getopt.h"
#endif

#include "lnettlelib.h"

extern const luaL_Reg extra_os_functions[];

int main (int argc, char *argv[])
{
	char *script_path;
	lua_State *L;
	int c;

	while ((c = getopt(argc, argv, "C:")) != -1) {
		switch (c) {
		case 'C':
			if (chdir(optarg)) {
				fprintf(stderr, "could not change working directory\n");
				return EXIT_FAILURE;
			}
			break;
		case '?':
		default:
			return EXIT_FAILURE;
		}
	}

	if (optind >= argc || argv[optind][0] == '-') {
		fprintf(stderr, "lua script not provided\n");
		return EXIT_FAILURE;
	}
	script_path = argv[optind];
	optind++;

	L = luaL_newstate();
	if (L == NULL) {
		fprintf(stderr, "cannot create lua state\n");
		return EXIT_FAILURE;
	}

	luaL_openlibs(L);
	luaL_requiref(L, "nettle", luaopen_nettle, 1);
	lua_pop(L, 1);

	premake_init(L);
	premake_locate(L, argv[0]);
	lua_setglobal(L, "_EXE_PATH");

	lua_pushstring(L, script_path);
	lua_setglobal(L, "_GRANGER_SCRIPT");

	lua_newtable(L);
	for (int i = 1; optind < argc; i++, optind++) {
		lua_pushinteger(L, i);
		lua_pushstring(L, argv[optind]);
		lua_settable(L, 1);
	}
	lua_setglobal(L, "argv");

	if (luaL_dofile(L, script_path)) {
		fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
		lua_close(L);
		return EXIT_FAILURE;
	}

	lua_close(L);
	return EXIT_SUCCESS;
}