forked from vtrlx/telepipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelepipe.c
More file actions
160 lines (136 loc) · 3.74 KB
/
telepipe.c
File metadata and controls
160 lines (136 loc) · 3.74 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* telepipe.c (startup and support code for Telepipe)
Copyright © 2026 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 <libintl.h>
#include <locale.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
/* The first macro quotes the argument name as a string. The second allows the passing of a macro value to be quoted instead. */
#define QUOTE(name) #name
#define MSTR(macro) QUOTE(macro)
/* Environment variables passed from the Makefile, whose values are made into C strings. */
#define APP_ID MSTR(PACKAGE)
#define APP_VER MSTR(VERSION)
#define INSTALL_PREFIX MSTR(PREFIX)
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_is_flatpak_lua(lua_State *L)
{
#ifdef FLATPAK
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)
{
lua_pushstring(L, APP_VER);
return 1;
}
static int
get_install_prefix_lua(lua_State *L)
{
lua_pushstring(L, INSTALL_PREFIX);
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. Used for the --new-window flag. */
{
int i;
for (i = 0; i < argc; ++i)
lua_pushstring(L, argv[i]);
return argc;
}
static int
gettext_lua(lua_State *L)
/* Returns a localized string using gettext(). */
{
const char *msgid;
char *msg;
msgid = luaL_checkstring(L, 1);
if (!msgid) {
luaL_pushfail(L);
return 1;
}
msg = gettext(msgid);
lua_pushstring(L, msg);
return 1;
}
static const luaL_Reg telepipelib[] = {
{ "get_is_devel", get_is_devel_lua },
{ "get_is_flatpak", get_is_flatpak_lua },
{ "get_app_id", get_app_id_lua },
{ "get_app_ver", get_app_ver_lua },
{ "get_install_prefix", get_install_prefix_lua },
{ "get_cli_args", get_cli_args_lua },
{ "gettext", gettext_lua },
/* sentinel item, marks the end of the array */
{ NULL, NULL },
};
const char telepipe_bytecode[] = {
#embed "telepipe.bytecode"
};
int
main(int _argc, char **_argv)
{
lua_State *L;
const char *message;
int lua_result;
setlocale(LC_ALL, "");
/* Tells gettext where to look for messages files. Dest should be /app/share/locale/<lang>/LC_MESSAGES/<domain>.mo */
bindtextdomain("messages", "/app/share/locale");
textdomain("messages");
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, "telepipelib");
luaL_newlib(L, telepipelib);
lua_settable(L, -3);
lua_remove(L, -1);
lua_result = luaL_loadbuffer(L, telepipe_bytecode, sizeof (telepipe_bytecode), APP_ID);
switch (lua_result) {
case LUA_ERRSYNTAX:
fprintf(stderr, "Failed to load Telepipe: 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 Telepipe: could not allocate memory.\n");
return 2;
case LUA_OK:
break;
default:
fprintf(stderr, "Failed to load Telepipe: an unhandled error ocurred.\n");
return -1;
}
lua_call(L, 0, 0);
return 0;
}