forked from kovidgoyal/kitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.c
284 lines (251 loc) · 8.8 KB
/
launcher.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* launcher.c
* Copyright (C) 2017 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#include <sys/syslimits.h>
#include <sys/stat.h>
#else
#include <limits.h>
#endif
#include <Python.h>
#include <wchar.h>
#include <stdbool.h>
#ifndef KITTY_LIB_PATH
#define KITTY_LIB_PATH "../.."
#endif
#ifndef KITTY_LIB_DIR_NAME
#define KITTY_LIB_DIR_NAME "lib"
#endif
static void cleanup_free(void *p) { free(*(void**) p); }
#define FREE_AFTER_FUNCTION __attribute__((cleanup(cleanup_free)))
#ifndef __FreeBSD__
static bool
safe_realpath(const char* src, char *buf, size_t buf_sz) {
FREE_AFTER_FUNCTION char* ans = realpath(src, NULL);
if (ans == NULL) return false;
snprintf(buf, buf_sz, "%s", ans);
return true;
}
#endif
static bool
set_xoptions(const char *exe_dir_c, const char *lc_ctype, bool from_source) {
wchar_t *exe_dir = Py_DecodeLocale(exe_dir_c, NULL);
if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir: %s\n", exe_dir_c); return false; }
wchar_t buf[PATH_MAX+1] = {0};
swprintf(buf, PATH_MAX, L"bundle_exe_dir=%ls", exe_dir);
PySys_AddXOption(buf);
PyMem_RawFree(exe_dir);
if (from_source) PySys_AddXOption(L"kitty_from_source=1");
if (lc_ctype) {
swprintf(buf, PATH_MAX, L"lc_ctype_before_python=%s", lc_ctype);
PySys_AddXOption(buf);
}
return true;
}
typedef struct {
const char *exe, *exe_dir, *lc_ctype, *lib_dir;
char **argv;
int argc;
} RunData;
#ifdef FOR_BUNDLE
#include <bypy-freeze.h>
static bool
canonicalize_path(const char *srcpath, char *dstpath, size_t sz) {
// remove . and .. path segments
bool ok = false;
size_t plen = strlen(srcpath) + 1, chk;
FREE_AFTER_FUNCTION char *wtmp = malloc(plen);
FREE_AFTER_FUNCTION char **tokv = malloc(sizeof(char*) * plen);
if (!wtmp || !tokv) goto end;
char *s, *tok, *sav;
bool relpath = *srcpath != '/';
// use a buffer as strtok modifies its input
memcpy(wtmp, srcpath, plen);
tok = strtok_r(wtmp, "/", &sav);
int ti = 0;
while (tok != NULL) {
if (strcmp(tok, "..") == 0) {
if (ti > 0) ti--;
} else if (strcmp(tok, ".") != 0) {
tokv[ti++] = tok;
}
tok = strtok_r(NULL, "/", &sav);
}
chk = 0;
s = dstpath;
for (int i = 0; i < ti; i++) {
size_t token_sz = strlen(tokv[i]);
if (i > 0 || !relpath) {
if (++chk >= sz) goto end;
*s++ = '/';
}
chk += token_sz;
if (chk >= sz) goto end;
memcpy(s, tokv[i], token_sz);
s += token_sz;
}
if (s == dstpath) {
if (++chk >= sz) goto end;
*s++ = relpath ? '.' : '/';
}
*s = '\0';
ok = true;
end:
return ok;
}
static bool
canonicalize_path_wide(const char *srcpath, wchar_t *dest, size_t sz) {
char buf[sz + 1];
bool ret = canonicalize_path(srcpath, buf, sz);
buf[sz] = 0;
mbstowcs(dest, buf, sz - 1);
dest[sz-1] = 0;
return ret;
}
static int
run_embedded(const RunData run_data) {
bypy_pre_initialize_interpreter(false);
char extensions_dir_full[PATH_MAX+1] = {0}, python_home_full[PATH_MAX+1] = {0};
#ifdef __APPLE__
const char *python_relpath = "../Resources/Python/lib";
#else
const char *python_relpath = "../" KITTY_LIB_DIR_NAME;
#endif
int num = snprintf(extensions_dir_full, PATH_MAX, "%s/%s/kitty-extensions", run_data.exe_dir, python_relpath);
if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to extensions_dir: %s/%s\n", run_data.exe_dir, python_relpath); return 1; }
wchar_t extensions_dir[num+2];
if (!canonicalize_path_wide(extensions_dir_full, extensions_dir, num+1)) {
fprintf(stderr, "Failed to canonicalize the path: %s\n", extensions_dir_full); return 1; }
num = snprintf(python_home_full, PATH_MAX, "%s/%s/python%s", run_data.exe_dir, python_relpath, PYVER);
if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to python home: %s/%s\n", run_data.exe_dir, python_relpath); return 1; }
wchar_t python_home[num+2];
if (!canonicalize_path_wide(python_home_full, python_home, num+1)) {
fprintf(stderr, "Failed to canonicalize the path: %s\n", python_home_full); return 1; }
bypy_initialize_interpreter(L"kitty", python_home, L"kitty_main", extensions_dir, run_data.argc, run_data.argv);
if (!set_xoptions(run_data.exe_dir, run_data.lc_ctype, false)) return 1;
set_sys_bool("frozen", true);
set_sys_string("kitty_extensions_dir", extensions_dir);
return bypy_run_interpreter();
}
#else
static int
free_argv(wchar_t **argv) {
wchar_t **p = argv;
while (*p) { PyMem_RawFree(*p); p++; }
free(argv);
return 1;
}
static int
run_embedded(const RunData run_data) {
bool from_source = false;
#ifdef FROM_SOURCE
from_source = true;
#endif
if (!set_xoptions(run_data.exe_dir, run_data.lc_ctype, from_source)) return 1;
int argc = run_data.argc + 1;
wchar_t **argv = calloc(argc, sizeof(wchar_t*));
if (!argv) { fprintf(stderr, "Out of memory creating argv\n"); return 1; }
memset(argv, 0, sizeof(wchar_t*) * argc);
argv[0] = Py_DecodeLocale(run_data.exe, NULL);
if (!argv[0]) { fprintf(stderr, "Failed to decode path to exe\n"); return free_argv(argv); }
argv[1] = Py_DecodeLocale(run_data.lib_dir, NULL);
if (!argv[1]) { fprintf(stderr, "Failed to decode path to lib_dir\n"); return free_argv(argv); }
for (int i=1; i < run_data.argc; i++) {
argv[i+1] = Py_DecodeLocale(run_data.argv[i], NULL);
if (!argv[i+1]) { fprintf(stderr, "Failed to decode the command line argument: %s\n", run_data.argv[i]); return free_argv(argv); }
}
int ret = Py_Main(argc, argv);
// we cannot free argv properly as Py_Main modifies it
free(argv);
return ret;
}
#endif
// read_exe_path() {{{
#ifdef __APPLE__
static bool
read_exe_path(char *exe, size_t buf_sz) {
(void)buf_sz;
uint32_t size = PATH_MAX;
char apple[PATH_MAX+1] = {0};
if (_NSGetExecutablePath(apple, &size) != 0) { fprintf(stderr, "Failed to get path to executable\n"); return false; }
if (!safe_realpath(apple, exe, buf_sz)) { fprintf(stderr, "realpath() failed on the executable's path\n"); return false; }
return true;
}
#elif defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/sysctl.h>
static bool
read_exe_path(char *exe, size_t buf_sz) {
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
size_t length = buf_sz;
int error = sysctl(name, 4, exe, &length, NULL, 0);
if (error < 0 || length <= 1) {
fprintf(stderr, "failed to get path to executable, sysctl() failed\n");
return false;
}
return true;
}
#elif defined(__NetBSD__)
static bool
read_exe_path(char *exe, size_t buf_sz) {
if (!safe_realpath("/proc/curproc/exe", exe, buf_sz)) { fprintf(stderr, "Failed to read /proc/curproc/exe\n"); return false; }
return true;
}
#elif defined(__OpenBSD__)
static bool
read_exe_path(char *exe, size_t buf_sz) {
const char *path = getenv("PATH");
if (!path) { fprintf(stderr, "No PATH environment variable set, aborting\n"); return false; }
char buf[PATH_MAX + 1] = {0};
strncpy(buf, path, PATH_MAX);
char *token = strtok(buf, ":");
while (token != NULL) {
char q[PATH_MAX + 1] = {0};
snprintf(q, PATH_MAX, "%s/kitty", token);
if (safe_realpath(q, exe, buf_sz)) return true;
token = strtok(NULL, ":");
}
fprintf(stderr, "kitty not found in PATH aborting\n");
return false;
}
#else
static bool
read_exe_path(char *exe, size_t buf_sz) {
if (!safe_realpath("/proc/self/exe", exe, buf_sz)) { fprintf(stderr, "Failed to read /proc/self/exe\n"); return false; }
return true;
}
#endif // }}}
int main(int argc, char *argv[]) {
char exe[PATH_MAX+1] = {0};
char exe_dir_buf[PATH_MAX+1] = {0};
FREE_AFTER_FUNCTION const char *lc_ctype = NULL;
#ifdef __APPLE__
lc_ctype = getenv("LC_CTYPE");
#endif
if (!read_exe_path(exe, sizeof(exe))) return 1;
strncpy(exe_dir_buf, exe, sizeof(exe_dir_buf));
char *exe_dir = dirname(exe_dir_buf);
int num, ret=0;
char lib[PATH_MAX+1] = {0};
num = snprintf(lib, PATH_MAX, "%s/%s", exe_dir, KITTY_LIB_PATH);
if (num < 0 || num >= PATH_MAX) { fprintf(stderr, "Failed to create path to kitty lib\n"); return 1; }
#if PY_VERSION_HEX >= 0x03070000
// Always use UTF-8 mode, see https://github.com/kovidgoyal/kitty/issues/924
Py_UTF8Mode = 1;
#endif
if (lc_ctype) lc_ctype = strdup(lc_ctype);
RunData run_data = {.exe = exe, .exe_dir = exe_dir, .lib_dir = lib, .argc = argc, .argv = argv, .lc_ctype = lc_ctype};
ret = run_embedded(run_data);
return ret;
}