This repository was archived by the owner on Apr 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparchment.c
More file actions
138 lines (118 loc) · 3.11 KB
/
parchment.c
File metadata and controls
138 lines (118 loc) · 3.11 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* parchment.c (startup and support code for Parchment)
Copyright © 2024–2025 Victoria Lacroix
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 3 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 <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#ifdef DEVEL
#define APP_ID "ca.vtrlx.Parchment.Devel"
#else
#define APP_ID "ca.vtrlx.Parchment"
#endif
#define QUOTE(name) #name
#define MSTR(macro) QUOTE(macro)
static int
get_is_devel_lua(lua_State *L)
{
#ifdef DEVEL
lua_pushboolean(L, 1);
#else
lua_pushboolean(L, 0);
#endif
return 1;
}
static int
get_app_id_lua(lua_State *L)
{
lua_pushstring(L, APP_ID);
return 1;
}
static int
get_app_ver_lua(lua_State *L)
{
#ifdef VERSION
lua_pushstring(L, MSTR(VERSION));
#else
#error("VERSION macro is not defined!")
#endif
return 1;
}
static int standalone;
static int
get_is_standalone_lua(lua_State *L)
{
lua_pushboolean(L, standalone);
return 1;
}
static int argc;
static char **argv;
static int
get_cli_args_lua(lua_State *L)
/* Returns each argument given to the command line. */
{
int i;
for (i = 0; i < argc; ++i) {
lua_pushstring(L, argv[i]);
}
return argc;
}
static const luaL_Reg parchmentlib[] = {
{ "get_is_devel", get_is_devel_lua },
{ "get_app_id", get_app_id_lua },
{ "get_app_ver", get_app_ver_lua },
{ "get_is_standalone", get_is_standalone_lua },
{ "get_cli_args", get_cli_args_lua },
/* sentinel item marking the end of the array */
{ NULL, NULL },
};
const char parchment_bytecode[] = {
#embed "parchment.bytecode"
};
int
main(int _argc, char **_argv)
{
lua_State *L;
const char *message;
int lua_result;
standalone = 0;
for (int i = 0; i < _argc; ++i)
if (strcmp(_argv[i], "--standalone") == 0)
standalone = 1;
argc = _argc;
argv = _argv;
L = luaL_newstate();
luaL_openlibs(L);
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_remove(L, -2);
lua_pushstring(L, "parchmentlib");
luaL_newlib(L, parchmentlib);
lua_settable(L, -3);
lua_remove(L, -1);
lua_result = luaL_loadbuffer(L, parchment_bytecode, sizeof (parchment_bytecode), APP_ID);
switch (lua_result) {
case LUA_ERRSYNTAX:
fprintf(stderr, "Failed to load Parchment: binary is malformed.\n");
message = luaL_checkstring(L, -1);
if (message)
fprintf(stderr, "%s\n", message);
return 1;
case LUA_ERRMEM:
fprintf(stderr, "Failed to load Parchment: could not allocate memory.\n");
return 2;
case LUA_OK:
break;
default:
fprintf(stderr, "Failed to load Parchment: an unhandled error ocurred.\n");
return -1;
}
lua_call(L, 0, 0);
return 0;
}