aboutsummaryrefslogtreecommitdiff
path: root/src/lua_globs.c
blob: 5a8e51c2f14076661e0efc451116202cad64af63 (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
/*

  pkgit - package it!

  Copyright (C) 2026 dacctal
  This program 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 2 of the License, or
  (at your option) any later version.

  This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

*/

#include "lua_globs.h"
#include "files.h"
#include "globs.h"
#include "log.h"
#include "str.h"

lua_State *L = NULL;
lua_State *B = NULL;
bool config_loaded = false;
bool bldit_loaded = false;

void push_lua_path(lua_State *L, const char *new_path) {
	lua_getglobal(L, "package");
	lua_getfield(L, -1, "path");
	const char *current_path = lua_tostring(L, -1);
	if (!current_path)
		current_path = "";
	lua_pop(L, 1);
	lua_pushfstring(L, "%s;%s", current_path, new_path);
	lua_setfield(L, -2, "path");
	lua_pop(L, 1);
}

void init_lua_state(void) {
	if (L != NULL)
		return;
	L = luaL_newstate();
	luaL_openlibs(L);
	str lua_path = str_format("%.*s/?.lua", str_fmt(&cfg.dir));
	push_lua_path(L, lua_path.data);
	if (luaL_loadfile(L, cfg.name.data) || lua_pcall(L, 0, 0, 0)) {
		log_error("cannot run configuration script: %s", lua_tostring(L, -1));
		log_pkgit("to generate a configuration file, head into the");
		log_pkgit(
			"root directory of the pkgit source and run `make defconfig`");
		exit(EXIT_FAILURE);
	}
	if (file_exists(cfg.repos.data)) {
		if (luaL_loadfile(L, cfg.repos.data) || lua_pcall(L, 0, 0, 0)) {
			log_warn("cannot load repository file: %s", lua_tostring(L, -1));
			lua_pop(L, 1);
		}
	}
	str_free(&lua_path);
	config_loaded = true;
}

void init_bldit(void) {
	if (B != NULL)
		return;
	B = luaL_newstate();
	luaL_openlibs(B);
	if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) {
		if (flags.verbose)
			log_warn("cannot run bldit: %s", lua_tostring(B, -1));
		return;
	}
	bldit_loaded = true;
}

void free_lua_state(void) {
	if (L != NULL) {
		lua_close(L);
		L = NULL;
	}
	config_loaded = false;
}

lua_State *get_lua_state(void) {
	return L;
}

void lua_isnt_type(char *variable, char *type) {
	if (flags.verbose)
		log_error("init.lua: '%s' is not a %s.", variable, type);
}