Debian Codegraph @

texk/web2c/luatexdir/lua/luainit.c

raw
1 /*
2
3 luainit.w
4
5 Copyright 2006-2021 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25
26 #include <kpathsea/c-stat.h>
27 #include <kpathsea/cnf.h>
28
29 #include "lua/luatex-api.h"
30
31 #include <locale.h>
32
33 #include "luatex_svnversion.h"
34
35 extern int load_luatex_core_lua (lua_State * L);
36
37 /*tex internalized strings: see luatex-api.h */
38
39 set_make_keys;
40
41 /*tex
42
43 This file is getting a bit messy, but it is not simple to fix unilaterally. In
44 fact, it sets three C variables:
45
46 |kpse_invocation_name| |kpse_invocation_short_name| |kpse->program_name|
47
48 and five environment variables:
49
50 |SELFAUTOLOC| |SELFAUTODIR| |SELFAUTOPARENT| |SELFAUTOGRANDPARENT| |progname|
51
52 */
53
54 const_string LUATEX_IHELP[] = {
55 "Usage: " my_name " --lua=FILE [OPTION]... [TEXNAME[.tex]] [COMMANDS]",
56 " or: " my_name " --lua=FILE [OPTION]... \\FIRST-LINE",
57 " or: " my_name " --lua=FILE [OPTION]... &FMT ARGS",
58 " Run " MyName " on TEXNAME, usually creating TEXNAME.pdf.",
59 " Any remaining COMMANDS are processed as luatex input, after TEXNAME is read.",
60 "",
61 " Alternatively, if the first non-option argument begins with a backslash,",
62 " " my_name " interprets all non-option arguments as an input line.",
63 "",
64 " Alternatively, if the first non-option argument begins with a &, the",
65 " next word is taken as the FMT to read, overriding all else. Any",
66 " remaining arguments are processed as above.",
67 "",
68 " If no arguments or options are specified, prompt for input.",
69 "",
70 " The following regular options are understood: ",
71 "",
72 " --cnf-line=STRING parse STRING as a configuration file line",
73 " --credits display credits and exit",
74 " --debug-format enable format debugging",
75 " --draftmode switch on draft mode (generates no output PDF)",
76 " --[no-]check-dvi-total-pages exit when DVI exceeds 65535 pages (default: check)",
77 " --[no-]file-line-error disable/enable file:line:error style messages",
78 " --[no-]file-line-error-style aliases of --[no-]file-line-error",
79 " --fmt=FORMAT load the format file FORMAT",
80 " --halt-on-error stop processing at the first error",
81 " --help display help and exit",
82 " --ini be ini" my_name ", for dumping formats",
83 " --interaction=STRING set interaction mode (STRING=batchmode/nonstopmode/scrollmode/errorstopmode)",
84 " --jobname=STRING set the job name to STRING",
85 " --kpathsea-debug=NUMBER set path searching debugging flags according to the bits of NUMBER",
86 " --lua=FILE load and execute a lua initialization script",
87 " --luadebug enable lua debug library",
88 " --[no-]mktex=FMT disable/enable mktexFMT generation (FMT=tex/tfm/fmt)",
89 " --nosocket disable the lua socket library",
90 " --no-socket disable the lua socket library",
91 " --socket enable the lua socket library",
92 " --output-comment=STRING use STRING for DVI file comment instead of date (no effect for PDF)",
93 " --output-directory=DIR use existing DIR as the directory to write files in",
94 " --output-format=FORMAT use FORMAT for job output; FORMAT is 'dvi' or 'pdf'",
95 " --progname=STRING set the program name to STRING",
96 " --recorder enable filename recorder",
97 " --safer disable easily exploitable lua commands",
98 " --[no-]shell-escape disable/enable system commands",
99 " --shell-restricted restrict system commands to a list of commands given in texmf.cnf",
100 " --synctex=NUMBER enable synctex (see man synctex)",
101 " --utc init time to UTC",
102 " --version display version and exit",
103 "",
104 "Alternate behaviour models can be obtained by special switches",
105 "",
106 " --luaonly run a lua file, then exit",
107 " --luaconly byte-compile a lua file, then exit",
108 " --luahashchars the bits used by current Lua interpreter for strings hashing",
109 #ifdef LuajitTeX
110 " --jiton turns the JIT compiler on (default off)",
111 " --jithash=STRING choose the hash function for the lua strings (lua51|luajit20: default lua51)",
112 #endif
113 "",
114 "See the reference manual for more information about the startup process.",
115 "LuaTeX package page: https://ctan.org/pkg/luatex",
116 NULL
117 };
118
119 /*tex
120
121 Later we will put on environment |LC_CTYPE|, |LC_COLLATE| and |LC_NUMERIC| set to
122 |C|, so we need a place where to store the old values.
123
124 */
125
126 const char *lc_ctype;
127 const char *lc_collate;
128 const char *lc_numeric;
129
130 /*
131 " --8bit ignored, input is assumed to be in UTF-8 encoding",
132 " --default-translate-file=FILE ignored, input is assumed to be in UTF-8 encoding",
133 " --etex ignored, the etex extensions are always active",
134 " --disable-write18 disable \\write18{SHELL COMMAND}",
135 " --enable-write18 enable \\write18{SHELL COMMAND}",
136 " --[no-]parse-first-line ignored",
137 " --translate-file=FILE ignored, input is assumed to be in UTF-8 encoding",
138 */
139
140 /*tex
141
142 The return value will be the directory of the executable, e.g.: \.{c:/TeX/bin}
143
144 */
145
146 static char *ex_selfdir(char *argv0)
147 {
148 #if defined(WIN32)
149 #if defined(__MINGW32__)
150 char path[PATH_MAX], *fp;
151 /*tex SearchPath() always gives back an absolute directory */
152 if (SearchPath(NULL, argv0, ".exe", PATH_MAX, path, NULL) == 0)
153 FATAL1("Can't determine where the executable %s is.\n", argv0);
154 /*tex slashify the dirname */
155 for (fp = path; fp && *fp; fp++)
156 if (IS_DIR_SEP(*fp))
157 *fp = DIR_SEP;
158 #else /* __MINGW32__ */
159 #define PATH_MAX 512
160 char short_path[PATH_MAX], path[PATH_MAX], *fp;
161 /*tex SearchPath() always gives back an absolute directory */
162 if (SearchPath(NULL, argv0, ".exe", PATH_MAX, short_path, &fp) == 0)
163 FATAL1("Can't determine where the executable %s is.\n", argv0);
164 if (getlongpath(path, short_path, sizeof(path)) == 0) {
165 FATAL1("This path points to an invalid file : %s\n", short_path);
166 }
167 #endif /* __MINGW32__ */
168 return xdirname(path);
169 #else /* WIN32 */
170 return kpse_selfdir(argv0);
171 #endif
172 }
173
174 static void prepare_cmdline(lua_State * L, char **av, int ac, int zero_offset)
175 {
176 int i;
177 char *s;
178 luaL_checkstack(L, ac + 3, "too many arguments to script");
179 lua_createtable(L, 0, 0);
180 for (i = 0; i < ac; i++) {
181 lua_pushstring(L, av[i]);
182 lua_rawseti(L, -2, (i - zero_offset));
183 }
184 lua_setglobal(L, "arg");
185 lua_getglobal(L, "os");
186 s = ex_selfdir(argv[0]);
187 lua_pushstring(L, s);
188 xfree(s);
189 lua_setfield(L, -2, "selfdir");
190 return;
191 }
192
193 int kpse_init = -1;
194
195 /*tex
196
197 Array and count of values given with --cnf-line.
198
199 */
200 static string *user_cnf_lines = NULL;
201 static unsigned user_cnf_nlines = 0;
202
203
204
205 string input_name = NULL;
206
207 static string user_progname = NULL;
208
209 char *startup_filename = NULL;
210 int lua_only = 0;
211 int lua_offset = 0;
212 unsigned char show_luahashchars = 0;
213
214 #ifdef LuajitTeX
215 int luajiton = 0;
216 char *jithash_hashname = NULL;
217 #endif
218
219 int safer_option = 0;
220 int nosocket_option = 1;
221 int nosocket_cli_option = 0;
222 int yessocket_cli_option = 0;
223 int socket_bitmask = 0;
224 int utc_option = 0;
225 int luadebug_option = 0;
226
227 /*tex We use a bitmask for the socket library: |0000| and |1xxx| implies |--nosocket|,
228 otherwise the socket library is enabled. Default value is |0000|, i.e. |--nosocket|.
229 */
230 #define UPDATE_SOCKET_STATUS() do { \
231 socket_bitmask = 0; \
232 socket_bitmask = safer_option==1? (8+socket_bitmask):socket_bitmask;\
233 socket_bitmask = nosocket_cli_option==1? (4+socket_bitmask):socket_bitmask;\
234 socket_bitmask = (shellenabledp == 1 && restrictedshell == 0)?(2+socket_bitmask):socket_bitmask;\
235 socket_bitmask = yessocket_cli_option==1? (1+socket_bitmask):socket_bitmask;\
236 if( socket_bitmask==0) { \
237 nosocket_option = 1; \
238 } else if ( socket_bitmask<4) { \
239 nosocket_option = 0; \
240 } else { \
241 nosocket_option = 1; \
242 } \
243 } while (0)
244
245 /*tex
246
247 Test whether getopt found an option ``A''. Assumes the option index is in the
248 variable |option_index|, and the option table in a variable |long_options|.
249
250 */
251
252 #define ARGUMENT_IS(a) STREQ (long_options[option_index].name, a)
253
254 /*tex
255 Nota Bene: we still intercept some options that other engines handle
256 so that existing scripted usage will not fail.
257
258 SunOS cc can't initialize automatic structs, so make this static.
259 */
260
261 static struct option long_options[] = {
262 {"fmt", 1, 0, 0},
263 {"lua", 1, 0, 0},
264 {"luaonly", 0, 0, 0},
265 {"luahashchars", 0, 0, 0},
266 #ifdef LuajitTeX
267 {"jiton", 0, 0, 0},
268 {"jithash", 1, 0, 0},
269 #endif
270 {"safer", 0, &safer_option, 1},
271 {"luadebug", 0, &luadebug_option, 1},
272 {"utc", 0, &utc_option, 1},
273 {"nosocket", 0, &nosocket_cli_option, 1},
274 {"no-socket", 0, &nosocket_cli_option, 1},
275 {"socket", 0, &yessocket_cli_option, 1},
276 {"help", 0, 0, 0},
277 {"ini", 0, &ini_version, 1},
278 {"interaction", 1, 0, 0},
279 {"halt-on-error", 0, &haltonerrorp, 1},
280 {"kpathsea-debug", 1, 0, 0},
281 {"cnf-line", 1,0 ,0},
282 {"progname", 1, 0, 0},
283 {"version", 0, 0, 0},
284 {"credits", 0, 0, 0},
285 {"recorder", 0, 0, 0},
286 {"etex", 0, 0, 0},
287 {"output-comment", 1, 0, 0},
288 {"output-directory", 1, 0, 0},
289 {"draftmode", 0, 0, 0},
290 {"output-format", 1, 0, 0},
291 {"shell-escape", 0, &shellenabledp, 1},
292 {"no-shell-escape", 0, &shellenabledp, -1},
293 {"enable-write18", 0, &shellenabledp, 1},
294 {"disable-write18", 0, &shellenabledp, -1},
295 {"shell-restricted", 0, 0, 0},
296 {"debug-format", 0, &debug_format_file, 1},
297 {"check-dvi-total-pages", 0, &check_dvi_total_pages, 1},
298 {"no-check-dvi-total-pages", 0, &check_dvi_total_pages, 0},
299 {"file-line-error-style", 0, &filelineerrorstylep, 1},
300 {"no-file-line-error-style", 0, &filelineerrorstylep, -1},
301 /*tex Shorter option names for the above. */
302 {"file-line-error", 0, &filelineerrorstylep, 1},
303 {"no-file-line-error", 0, &filelineerrorstylep, -1},
304 {"jobname", 1, 0, 0},
305 /*{"parse-first-line", 0, &parsefirstlinep, 1},*/ /* ignored, left as reference*/
306 /*{"no-parse-first-line", 0, &parsefirstlinep, -1},*/
307 {"translate-file", 1, 0, 0},
308 {"default-translate-file", 1, 0, 0},
309 {"8bit", 0, 0, 0},
310 {"mktex", 1, 0, 0},
311 {"no-mktex", 1, 0, 0},
312 /*tex Synchronization: just like ``interaction'' above */
313 {"synctex", 1, 0, 0},
314 {0, 0, 0, 0}
315 };
316
317 int lua_numeric_field_by_index(lua_State * L, int name_index, int dflt)
318 {
319 register int i = dflt;
320 /*tex fetch the stringptr */
321 lua_rawgeti(L, LUA_REGISTRYINDEX, name_index);
322 lua_rawget(L, -2);
323 if (lua_type(L, -1) == LUA_TNUMBER) {
324 i = lua_roundnumber(L, -1);
325 }
326 lua_pop(L, 1);
327 return i;
328 }
329
330 unsigned int lua_unsigned_numeric_field_by_index(lua_State * L, int name_index, int dflt)
331 {
332 register unsigned int i = dflt;
333 /*tex fetch the stringptr */
334 lua_rawgeti(L, LUA_REGISTRYINDEX, name_index);
335 lua_rawget(L, -2);
336 if (lua_type(L, -1) == LUA_TNUMBER) {
337 i = lua_uroundnumber(L, -1);
338 }
339 lua_pop(L, 1);
340 return i;
341 }
342
343 static int recorderoption = 0;
344
345 static void parse_options(int ac, char **av)
346 {
347 #ifdef WIN32
348 /*tex We save |argc| and |argv|. */
349 int sargc = argc;
350 char **sargv = argv;
351 #endif
352 /*tex The `getopt' return code. */
353 int g;
354 int option_index;
355 char *firstfile = NULL;
356 /*tex Dont whine. */
357 opterr = 0;
358 #ifdef LuajitTeX
359 if ((strstr(argv[0], "luajittexlua") != NULL) ||
360 (strstr(argv[0], "texluajit") != NULL) ||
361 (strstr(argv[0], "texluahbjit") != NULL) ) {
362 #else
363 if ((strstr(argv[0], "luatexlua") != NULL) ||
364 (strstr(argv[0], "texlua") != NULL) ||
365 (strstr(argv[0], "texluahb") != NULL)) {
366 #endif
367 lua_only = 1;
368 luainit = 1;
369 }
370
371 for (;;) {
372 g = getopt_long_only(ac, av, "+", long_options, &option_index);
373 if (g == -1) {
374 /*tex End of arguments, exit the loop. */
375 break;
376 }
377 if (g == '?') {
378 /*tex Unknown option. */
379 if (!luainit)
380 fprintf(stderr,"%s: unrecognized option '%s'\n", argv[0], argv[optind-1]);
381 continue;
382 }
383 /* We have no short option names. */
384 assert(g == 0);
385 if (ARGUMENT_IS("luaonly")) {
386 lua_only = 1;
387 lua_offset = optind;
388 luainit = 1;
389 } else if (ARGUMENT_IS("lua")) {
390 startup_filename = xstrdup(optarg);
391 lua_offset = (optind - 1);
392 luainit = 1;
393 #ifdef LuajitTeX
394 } else if (ARGUMENT_IS("jiton")) {
395 luajiton = 1;
396 } else if (ARGUMENT_IS("jithash")) {
397 size_t len = strlen(optarg);
398 if (len<16) {
399 jithash_hashname = optarg;
400 } else {
401 WARNING2("hash name truncated to 15 characters from %d. (%s)", (int) len, optarg);
402 jithash_hashname = (string) xmalloc(16);
403 strncpy(jithash_hashname, optarg, 15);
404 jithash_hashname[15] = 0;
405 }
406 #endif
407 } else if (ARGUMENT_IS("luahashchars")) {
408 show_luahashchars = 1;
409 } else if (ARGUMENT_IS("kpathsea-debug")) {
410 kpathsea_debug |= atoi(optarg);
411 } else if (ARGUMENT_IS ("cnf-line")) {
412 if (user_cnf_lines == NULL) {
413 user_cnf_nlines = 1;
414 user_cnf_lines = xmalloc (sizeof (const_string));
415 } else {
416 user_cnf_nlines++;
417 user_cnf_lines = xrealloc (user_cnf_lines,
418 user_cnf_nlines * sizeof (const_string));
419 }
420 user_cnf_lines[user_cnf_nlines-1] = xstrdup (optarg);
421 } else if (ARGUMENT_IS("progname")) {
422 user_progname = optarg;
423 } else if (ARGUMENT_IS("jobname")) {
424 c_job_name = optarg;
425 } else if (ARGUMENT_IS("fmt")) {
426 dump_name = optarg;
427 } else if (ARGUMENT_IS("output-directory")) {
428 output_directory = optarg;
429 } else if (ARGUMENT_IS("output-comment")) {
430 size_t len = strlen(optarg);
431 if (len < 256) {
432 output_comment = optarg;
433 } else {
434 WARNING2("Comment truncated to 255 characters from %d. (%s)", (int) len, optarg);
435 output_comment = (string) xmalloc(256);
436 strncpy(output_comment, optarg, 255);
437 output_comment[255] = 0;
438 }
439 } else if (ARGUMENT_IS("shell-restricted")) {
440 shellenabledp = 1;
441 restrictedshell = 1;
442 } else if (ARGUMENT_IS("output-format")) {
443 output_mode_option = 1;
444 if (strcmp(optarg, "dvi") == 0) {
445 output_mode_value = 0;
446 } else if (strcmp(optarg, "pdf") == 0) {
447 output_mode_value = 1;
448 } else {
449 WARNING1("Ignoring unknown value `%s' for --output-format",optarg);
450 output_mode_option = 0;
451 }
452 } else if (ARGUMENT_IS("check-dvi-total-pages")) {
453 check_dvi_total_pages = 1;
454 } else if (ARGUMENT_IS("no-check-dvi-total-pages")) {
455 check_dvi_total_pages = 0;
456 } else if (ARGUMENT_IS("draftmode")) {
457 draft_mode_option = 1;
458 draft_mode_value = 1;
459 } else if (ARGUMENT_IS("mktex")) {
460 kpse_maketex_option(optarg, true);
461 } else if (ARGUMENT_IS("no-mktex")) {
462 kpse_maketex_option(optarg, false);
463 } else if (ARGUMENT_IS("interaction")) {
464 /* These numbers match CPP defines */
465 if (STREQ(optarg, "batchmode")) {
466 interactionoption = 0;
467 } else if (STREQ(optarg, "nonstopmode")) {
468 interactionoption = 1;
469 } else if (STREQ(optarg, "scrollmode")) {
470 interactionoption = 2;
471 } else if (STREQ(optarg, "errorstopmode")) {
472 interactionoption = 3;
473 } else {
474 WARNING1("Ignoring unknown argument `%s' to --interaction", optarg);
475 }
476 } else if (ARGUMENT_IS("synctex")) {
477 /*tex Synchronize TeXnology: catching the command line option as a long */
478 synctexoption = (int) strtol(optarg, NULL, 0);
479 } else if (ARGUMENT_IS("recorder")) {
480 recorderoption = 1 ;
481 } else if (ARGUMENT_IS("help")) {
482 usagehelp(LUATEX_IHELP, BUG_ADDRESS);
483 } else if (ARGUMENT_IS("version")) {
484 #define STR(tok) STR2(tok)
485 #define STR2(tok) #tok
486 print_version_banner();
487 puts("\nDevelopment id: " STR(luatex_svn_revision));
488 /* *INDENT-OFF* */
489 puts("\nExecute '" my_name " --credits' for credits and version details.\n\n"
490 "There is NO warranty. Redistribution of this software is covered by\n"
491 "the terms of the GNU General Public License, version 2 or (at your option)\n"
492 "any later version. For more information about these matters, see the file\n"
493 "named COPYING and the LuaTeX source.\n\n"
494 "LuaTeX is Copyright 2025 Taco Hoekwater and the LuaTeX Team.\n");
495 /* *INDENT-ON* */
496 uexit(0);
497 } else if (ARGUMENT_IS("credits")) {
498 char *versions;
499 initversionstring(&versions);
500 print_version_banner();
501 /* *INDENT-OFF* */
502 puts("\n\nThe luatex project is a ConTeXt community project by\nHans Hagen, Hartmut Henkel, Taco Hoekwater and Luigi Scarso.\nThe current maintainers are Hans Hagen and Luigi Scarso.\n\n"
503 MyName " merges and builds upon (parts of) the code from these projects:\n\n"
504 "tex : Donald Knuth\n"
505 "etex : Peter Breitenlohner, Phil Taylor and friends\n"
506 "omega : John Plaice and Yannis Haralambous\n"
507 "aleph : Giuseppe Bilotta\n"
508 "pdftex : Han The Thanh and friends\n"
509 "kpathsea : Karl Berry, Olaf Weber and others\n"
510 "lua : Roberto Ierusalimschy, Waldemar Celes and Luiz Henrique de Figueiredo\n"
511 "metapost : John Hobby, Taco Hoekwater, Luigi Scarso, Hans Hagen and friends\n"
512 "pplib : Paweł Jackowski\n"
513 "fontforge : George Williams (partial)\n"
514 "luajit : Mike Pall (used in LuajitTeX)\n");
515 /* *INDENT-ON* */
516 puts(versions);
517 uexit(0);
518 }
519 }
520 /*tex attempt to find |input_name| and |dump_name| */
521 if (lua_only) {
522 if (argv[optind]) {
523 startup_filename = xstrdup(argv[optind]);
524 lua_offset = optind;
525 }
526 } else if (argv[optind] && argv[optind][0] == '&') {
527 dump_name = xstrdup(argv[optind] + 1);
528 } else if (argv[optind] && argv[optind][0] != '\\') {
529 if (argv[optind][0] == '*') {
530 input_name = xstrdup(argv[optind] + 1);
531 } else {
532 firstfile = xstrdup(argv[optind]);
533 if ((strstr(firstfile, ".lua") ==
534 firstfile + strlen(firstfile) - 4)
535 || (strstr(firstfile, ".luc") ==
536 firstfile + strlen(firstfile) - 4)
537 || (strstr(firstfile, ".LUA") ==
538 firstfile + strlen(firstfile) - 4)
539 || (strstr(firstfile, ".LUC") ==
540 firstfile + strlen(firstfile) - 4)) {
541 if (startup_filename == NULL) {
542 startup_filename = firstfile;
543 lua_offset = optind;
544 lua_only = 1;
545 luainit = 1;
546 }
547 } else {
548 input_name = firstfile;
549 }
550 }
551 #ifdef WIN32
552 } else if (sargc > 1 && sargv[sargc-1] && sargv[sargc-1][0] != '-' &&
553 sargv[sargc-1][0] != '\\') {
554 if (sargv[sargc-1][0] == '&')
555 dump_name = xstrdup(sargv[sargc-1] + 1);
556 else {
557 if (sargv[sargc-1][0] == '*')
558 input_name = xstrdup(sargv[sargc-1] + 1);
559 else
560 input_name = xstrdup(sargv[sargc-1]);
561 sargv[sargc-1] = normalize_quotes(input_name, "argument");
562 }
563 UPDATE_SOCKET_STATUS();
564 return;
565 #endif
566 }
567 UPDATE_SOCKET_STATUS();
568 /*tex Finalize the input filename. */
569 if (input_name != NULL) {
570 argv[optind] = normalize_quotes(input_name, "argument");
571 }
572 }
573
574 /*tex
575 Test for readability.
576 */
577
578 #define is_readable(a) (stat(a,&finfo)==0) \
579 && S_ISREG(finfo.st_mode) \
580 && (f=fopen(a,"r")) != NULL && !fclose(f)
581
582 static char *find_filename(char *name, const char *envkey)
583 {
584 struct stat finfo;
585 char *dirname = NULL;
586 char *filename = NULL;
587 FILE *f;
588 if (is_readable(name)) {
589 return name;
590 } else {
591 dirname = getenv(envkey);
592 if ((dirname != NULL) && strlen(dirname)) {
593 dirname = xstrdup(getenv(envkey));
594 if (*(dirname + strlen(dirname) - 1) == '/') {
595 *(dirname + strlen(dirname) - 1) = 0;
596 }
597 filename = xmalloc((unsigned) (strlen(dirname) + strlen(name) + 2));
598 filename = concat3(dirname, "/", name);
599 xfree(dirname);
600 if (is_readable(filename)) {
601 return filename;
602 }
603 xfree(filename);
604 }
605 }
606 return NULL;
607 }
608
609 static void init_kpse(void)
610 {
611 if (!user_progname) {
612 user_progname = dump_name;
613 } else if (!dump_name) {
614 dump_name = user_progname;
615 }
616 if (!user_progname) {
617 if (ini_version) {
618 if (input_name) {
619 char *p = input_name + strlen(input_name) - 1;
620 while (p >= input_name) {
621 if (IS_DIR_SEP (*p)) {
622 p++;
623 input_name = p;
624 break;
625 }
626 p--;
627 }
628 user_progname = remove_suffix (input_name);
629 }
630 if (!user_progname) {
631 user_progname = kpse_program_basename(argv[0]);
632 }
633 } else {
634 if (!dump_name) {
635 dump_name = kpse_program_basename(argv[0]);
636 }
637 user_progname = dump_name;
638 }
639 }
640 kpse_set_program_enabled(kpse_fmt_format, MAKE_TEX_FMT_BY_DEFAULT, kpse_src_compile);
641 kpse_set_program_name(argv[0], user_progname);
642 /*tex set up 'restrictedshell' */
643 init_shell_escape();
644 init_start_time();
645 program_name_set = 1 ;
646 if (recorderoption) {
647 recorder_enabled = 1;
648 }
649 }
650
651 static void fix_dumpname(void)
652 {
653 int dist;
654 if (dump_name) {
655 /*tex Adjust array for Pascal and provide extension, if needed. */
656 dist = (int) (strlen(dump_name) - strlen(DUMP_EXT));
657 if (strstr(dump_name, DUMP_EXT) == dump_name + dist)
658 TEX_format_default = dump_name;
659 else
660 TEX_format_default = concat(dump_name, DUMP_EXT);
661 } else {
662 /*tex For |dump_name| to be NULL is a bug. */
663 if (!ini_version) {
664 fprintf(stdout, "no format given, quitting\n");
665 exit(1);
666 }
667 }
668 }
669
670 /*tex
671 Auxiliary function for kpse search.
672 */
673
674 static const char *luatex_kpse_find_aux(lua_State *L, const char *name,
675 kpse_file_format_type format, const char *errname)
676 {
677 const char *filename;
678 const char *altname;
679 /*tex Lua convention */
680 altname = luaL_gsub(L, name, ".", "/");
681 filename = kpse_find_file(altname, format, false);
682 if (filename == NULL) {
683 filename = kpse_find_file(name, format, false);
684 }
685 if (filename == NULL) {
686 lua_pushfstring(L, "\n\t[kpse %s searcher] file not found: " LUA_QS, errname, name);
687 }
688 return filename;
689 }
690
691 /*tex
692
693 Here comes the \LUA\ search function. When kpathsea is not initialized, then it
694 runs the normal \LUA\ function that is saved in the registry, otherwise it uses
695 kpathsea.
696
697 Two registry ref variables are needed: one for the actual \LUA\ function, the
698 other for its environment .
699
700 */
701
702 static int lua_loader_function = 0;
703
704 static int luatex_kpse_lua_find(lua_State * L)
705 {
706 const char *filename;
707 const char *name;
708 name = luaL_checkstring(L, 1);
709 if (program_name_set == 0) {
710 lua_rawgeti(L, LUA_REGISTRYINDEX, lua_loader_function);
711 lua_pushvalue(L, -2);
712 lua_call(L, 1, 1);
713 return 1;
714 }
715 filename = luatex_kpse_find_aux(L, name, kpse_lua_format, "lua");
716 if (filename == NULL) {
717 /*tex library not found in this path */
718 return 1;
719 }
720 recorder_record_input(filename);
721 if (luaL_loadfile(L, filename) != 0) {
722 luaL_error(L, "error loading module %s from file %s:\n\t%s",
723 lua_tostring(L, 1), filename, lua_tostring(L, -1));
724 }
725 /*tex library loaded successfully */
726 return 1;
727 }
728
729 static int clua_loader_function = 0;
730 extern int searcher_C_luatex (lua_State *L, const char *name, const char *filename);
731
732 static int luatex_kpse_clua_find(lua_State * L)
733 {
734 const char *filename;
735 const char *name;
736 if (!clua_loader_function) {
737 /*tex library not found in this path */
738 lua_pushliteral(L, "\n\t[C searcher requires unrestricted shell escape]");
739 return 1;
740 }
741 if (safer_option) {
742 /*tex library not found in this path */
743 lua_pushliteral(L, "\n\t[C searcher disabled in safer mode]");
744 return 1;
745 }
746 name = luaL_checkstring(L, 1);
747 if (program_name_set == 0) {
748 lua_rawgeti(L, LUA_REGISTRYINDEX, clua_loader_function);
749 lua_pushvalue(L, -2);
750 lua_call(L, 1, 1);
751 return 1;
752 } else {
753 const char *path_saved;
754 char *prefix, *postfix, *p, *total;
755 char *extensionless;
756 char *temp_name;
757 int j;
758 filename = luatex_kpse_find_aux(L, name, kpse_clua_format, "C");
759 if (filename == NULL) {
760 /*tex library not found in this path */
761 return 1;
762 }
763 recorder_record_input(filename);
764 extensionless = strdup(filename);
765 if (!extensionless) {
766 /*tex allocation failure */
767 return 1;
768 }
769 /*tex Replace '.' with |LUA_DIRSEP| */
770 temp_name = strdup(name);
771 for(j=0; ; j++){
772 if ((unsigned char)temp_name[j]=='\0') {
773 break;
774 }
775 if ((unsigned char)temp_name[j]=='.'){
776 temp_name[j]=LUA_DIRSEP[0];
777 }
778 }
779 p = strstr(extensionless, temp_name);
780 if (!p) {
781 /*tex this would be exceedingly weird */
782 return 1;
783 }
784 *p = '\0';
785 prefix = strdup(extensionless);
786 if (!prefix) {
787 /*tex allocation failure */
788 return 1;
789 }
790 postfix = strdup(p+strlen(name));
791 if (!postfix) {
792 /*tex allocation failure */
793 return 1;
794 }
795 total = malloc(strlen(prefix)+strlen(postfix)+2);
796 if (!total) return 1; /* allocation failure */
797 snprintf(total,strlen(prefix)+strlen(postfix)+2, "%s?%s", prefix, postfix);
798 /*tex save package.path */
799 lua_getglobal(L,"package");
800 lua_getfield(L,-1,"cpath");
801 path_saved = lua_tostring(L,-1);
802 lua_pop(L,1);
803 /*tex set package.path = "?" */
804 lua_pushstring(L,total);
805 lua_setfield(L,-2,"cpath");
806 /*tex pop ``package'' */
807 lua_pop(L,1);
808 /*tex run function */
809 lua_rawgeti(L, LUA_REGISTRYINDEX, clua_loader_function);
810 lua_pushstring(L, name);
811 lua_call(L, 1, 1);
812 /*tex restore package.path */
813 lua_getglobal(L,"package");
814 lua_pushstring(L,path_saved);
815 lua_setfield(L,-2,"cpath");
816 /*tex pop ``package'' */
817 lua_pop(L,1);
818 free(extensionless);
819 free(total);
820 free(temp_name);
821 return 1;
822 }
823 }
824
825 /*tex
826
827 Setting up the new search functions. This replaces package.searchers[2] and
828 package.searchers[3] with the functions defined above.
829
830 */
831
832 static void setup_lua_path(lua_State * L)
833 {
834 lua_getglobal(L, "package");
835 #ifdef LuajitTeX
836 lua_getfield(L, -1, "loaders");
837 #else
838 lua_getfield(L, -1, "searchers");
839 #endif
840 /*tex package.searchers[2] */
841 lua_rawgeti(L, -1, 2);
842 lua_loader_function = luaL_ref(L, LUA_REGISTRYINDEX);
843 lua_pushcfunction(L, luatex_kpse_lua_find);
844 /*tex replace the normal lua loader */
845 lua_rawseti(L, -2, 2);
846 /*tex package.searchers[3] */
847 lua_rawgeti(L, -1, 3);
848 clua_loader_function = luaL_ref(L, LUA_REGISTRYINDEX);
849 lua_pushcfunction(L, luatex_kpse_clua_find);
850 /*tex replace the normal lua lib loader */
851 lua_rawseti(L, -2, 3);
852 /*tex pop the array and table */
853 lua_pop(L, 2);
854 }
855
856 /*tex
857
858 Helper variables for the safe keeping of table ids.
859
860 */
861
862 int l_pack_type_index [PACK_TYPE_SIZE] ;
863 int l_group_code_index [GROUP_CODE_SIZE];
864 int l_local_par_index [LOCAL_PAR_SIZE];
865 int l_math_style_name_index [MATH_STYLE_NAME_SIZE];
866 int l_dir_par_index [DIR_PAR_SIZE];
867 int l_dir_text_index_normal [DIR_TEXT_SIZE];
868 int l_dir_text_index_cancel [DIR_TEXT_SIZE];
869
870 int img_parms [img_parms_max];
871 int img_pageboxes [img_pageboxes_max];
872
873 int lua_show_valid_list(lua_State *L, const char **list, int offset, int max)
874 {
875 int i;
876 lua_newtable(L);
877 for (i = 0; i < max; i++) {
878 lua_pushinteger(L,i+offset);
879 lua_pushstring(L, list[i]);
880 lua_settable(L, -3);
881 }
882 return 1;
883 }
884
885 int lua_show_valid_keys(lua_State *L, int *list, int max)
886 {
887 int i;
888 lua_newtable(L);
889 for (i = 0; i < max; i++) {
890 lua_pushinteger(L,i+1);
891 lua_rawgeti(L, LUA_REGISTRYINDEX, list[i]);
892 lua_settable(L, -3);
893 }
894 return 1;
895 }
896
897 #if defined(WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
898 char **suffixlist;
899
900 /* Why do we add script stuff to this weird incomplete. Let's go more minimal. */
901
902 /*
903 #define EXE_SUFFIXES ".com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.ws;.tcl;.py;.pyw"
904 */
905
906 #define EXE_SUFFIXES ".com;.exe;.bat;.cmd"
907
908 static void mk_suffixlist(void)
909 {
910 char **p;
911 char *q, *r, *v;
912 int n;
913 # if defined(__CYGWIN__)
914 v = xstrdup(EXE_SUFFIXES);
915 # else
916 v = (char *) getenv("PATHEXT");
917 /*tex strlwr() exists also in MingW */
918 if (v)
919 v = (char *) strlwr(xstrdup(v));
920 else
921 v = xstrdup(EXE_SUFFIXES);
922 # endif
923 q = v;
924 n = 0;
925 while ((r = strchr(q, ';')) != NULL) {
926 n++;
927 r++;
928 q = r;
929 }
930 if (*q)
931 n++;
932 suffixlist = (char **) xmalloc((n + 2) * sizeof(char *));
933 p = suffixlist;
934 *p = xstrdup(".dll");
935 p++;
936 q = v;
937 while ((r = strchr(q, ';')) != NULL) {
938 *r = '\0';
939 *p = xstrdup(q);
940 p++;
941 r++;
942 q = r;
943 }
944 if (*q) {
945 *p = xstrdup(q);
946 p++;
947 }
948 *p = NULL;
949 free(v);
950 }
951 #endif
952
953 void lua_initialize(int ac, char **av)
954 {
955 char *given_file = NULL;
956 char *banner;
957 size_t len;
958 int starttime;
959 int utc;
960 static char LC_CTYPE_C[] = "LC_CTYPE=C";
961 static char LC_COLLATE_C[] = "LC_COLLATE=C";
962 static char LC_NUMERIC_C[] = "LC_NUMERIC=C";
963 static char engine_luatex[] = "engine=" my_name;
964 char *old_locale = NULL;
965 char *env_locale = NULL;
966 char *tmp = NULL;
967 /*tex Save to pass along to topenin. */
968 const char *fmt = "This is " MyName ", Version %s" WEB2CVERSION;
969 argc = ac;
970 argv = av;
971 len = strlen(fmt) + strlen(luatex_version_string) ;
972 banner = xmalloc(len);
973 sprintf(banner, fmt, luatex_version_string);
974 luatex_banner = banner;
975 kpse_invocation_name = kpse_program_basename(argv[0]);
976 /*tex be `luac' */
977 if (argc >1) {
978 #ifdef LuajitTeX
979 if (FILESTRCASEEQ(kpse_invocation_name, "texluajitc"))
980 exit(luac_main(ac, av));
981 if (STREQ(argv[1], "--luaconly") || STREQ(argv[1], "--luac")) {
982 char *argv1 = xmalloc (strlen ("luajittex") + 1);
983 av[1] = argv1;
984 strcpy (av[1], "luajittex");
985 exit(luac_main(--ac, ++av));
986 }
987 #else
988 if (FILESTRCASEEQ(kpse_invocation_name, "texluac"))
989 exit(luac_main(ac, av));
990 if (STREQ(argv[1], "--luaconly") || STREQ(argv[1], "--luac")) {
991 strcpy(av[1], "luatex");
992 exit(luac_main(--ac, ++av));
993 }
994 #endif
995 }
996 #if defined(WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
997 mk_suffixlist();
998 #endif
999 /*tex Must be initialized before options are parsed and might get adapted by config table. */
1000 check_dvi_total_pages = true;
1001 interactionoption = 4;
1002 filelineerrorstylep = false;
1003 haltonerrorp = false;
1004 haltingonerrorp = false;
1005 tracefilenames = 1;
1006 traceextranewline = 0;
1007 dump_name = NULL;
1008 /*tex
1009 In the next option 0 means ``disable Synchronize TeXnology''. The
1010 |synctexoption| is a *.web variable. We initialize it to a weird value to
1011 catch the -synctex command line flag At runtime, if synctexoption is not
1012 |INT_MAX|, then it contains the command line option provided, otherwise
1013 no such option was given by the user.
1014 */
1015 #define SYNCTEX_NO_OPTION INT_MAX
1016 synctexoption = SYNCTEX_NO_OPTION;
1017 /*tex parse commandline */
1018 parse_options(ac, av);
1019 if (lua_only) {
1020 /*tex Shell has no restrictions. */
1021 shellenabledp = true;
1022 restrictedshell = false;
1023 safer_option = 0;
1024 nosocket_option = 0;
1025 luadebug_option = true;
1026 }
1027 /*tex
1028 Get the current locale (it should be |C|) and save |LC_CTYPE|, |LC_COLLATE|
1029 and |LC_NUMERIC|. Later |luainterpreter()| will consciously use them.
1030 */
1031 old_locale = xstrdup(setlocale (LC_ALL, NULL));
1032 lc_ctype = NULL;
1033 lc_collate = NULL;
1034 lc_numeric = NULL;
1035 if (old_locale) {
1036 /*tex
1037 If |setlocale| fails here, then the state could be compromised, and
1038 we exit.
1039 */
1040 env_locale = setlocale (LC_ALL, "");
1041 if (!env_locale && !lua_only) {
1042 fprintf(stderr,"Unable to read locale data: please check the 'locale' settings of your environment for consistency. Exiting now.\n");
1043 exit(1);
1044 }
1045 tmp = setlocale (LC_CTYPE, NULL);
1046 if (tmp) {
1047 lc_ctype = xstrdup(tmp);
1048 }
1049 tmp = setlocale (LC_COLLATE, NULL);
1050 if (tmp) {
1051 lc_collate = xstrdup(tmp);
1052 }
1053 tmp = setlocale (LC_NUMERIC, NULL);
1054 if (tmp) {
1055 lc_numeric = xstrdup(tmp);
1056 }
1057 /*tex
1058 Return to the previous locale if possible, otherwise it's a serious
1059 error and we exit: we can't ensure a 'sane' locale for lua.
1060 */
1061 env_locale = setlocale (LC_ALL, old_locale);
1062 if (!env_locale) {
1063 fprintf(stderr,"Unable to restore original locale %s: exiting now.\n",old_locale);
1064 exit(1);
1065 }
1066 xfree(old_locale);
1067 } else {
1068 fprintf(stderr,"Unable to store environment locale.\n");
1069 }
1070 /*tex make sure that the locale is 'sane' (for lua) */
1071 putenv(LC_CTYPE_C);
1072 putenv(LC_COLLATE_C);
1073 putenv(LC_NUMERIC_C);
1074 /*tex this is sometimes needed */
1075 putenv(engine_luatex);
1076 /*tex add user's cnf values*/
1077 if (user_cnf_lines) {
1078 unsigned i;
1079 for (i = 0; i < user_cnf_nlines; i++) {
1080 /* debug printf ("ucnf%d: %s\n", i, user_cnf_lines[i]); */
1081 kpathsea_cnf_line_env_progname (kpse_def, user_cnf_lines[i]);
1082 free (user_cnf_lines[i]);
1083 }
1084 }
1085 luainterpreter();
1086 /*tex init internalized strings */
1087 set_init_keys;
1088 lua_pushstring(Luas,"lua.functions");
1089 lua_newtable(Luas);
1090 lua_settable(Luas,LUA_REGISTRYINDEX);
1091 /*tex here start the key definitions */
1092 set_l_pack_type_index;
1093 set_l_group_code_index;
1094 set_l_local_par_index;
1095 set_l_math_style_name_index;
1096 set_l_dir_par_index;
1097 set_l_dir_text_index;
1098 l_set_node_data();
1099 l_set_whatsit_data();
1100 l_set_token_data();
1101 set_l_img_keys_index;
1102 set_l_img_pageboxes_index;
1103 /*tex collect arguments */
1104 prepare_cmdline(Luas, argv, argc, lua_offset);
1105 setup_lua_path(Luas);
1106 if (startup_filename != NULL) {
1107 given_file = xstrdup(startup_filename);
1108 if (lua_only) {
1109 xfree(startup_filename);
1110 }
1111 startup_filename = find_filename(given_file, "LUATEXDIR");
1112 }
1113 /*tex now run the file */
1114 if (startup_filename != NULL) {
1115 char *v1;
1116 int tex_table_id = hide_lua_table(Luas, "tex");
1117 int token_table_id = hide_lua_table(Luas, "token");
1118 int node_table_id = hide_lua_table(Luas, "node");
1119 int pdf_table_id = hide_lua_table(Luas, "pdf");
1120 if (lua_only) {
1121 /*tex hide the 'tex' and 'pdf' table */
1122 if (load_luatex_core_lua(Luas)) {
1123 fprintf(stderr, "Error in execution of luatex-core.lua .\n");
1124 }
1125 if (luaL_loadfile(Luas, startup_filename)) {
1126 fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
1127 exit(1);
1128 }
1129 init_tex_table(Luas);
1130 lua_pushcfunction(Luas, lua_traceback);
1131 lua_insert(Luas, -2);
1132 if (lua_pcall(Luas, 0, 0, -2)) {
1133 fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
1134 /*tex lua_close(Luas); */
1135 exit(1);
1136 } else {
1137 if (given_file)
1138 free(given_file);
1139 /*tex lua_close(Luas); */
1140 exit(0);
1141 }
1142 lua_remove(Luas, -1);
1143 }
1144 /*tex a normal tex run */
1145 init_tex_table(Luas);
1146 unhide_lua_table(Luas, "tex", tex_table_id);
1147 unhide_lua_table(Luas, "pdf", pdf_table_id);
1148 unhide_lua_table(Luas, "token", token_table_id);
1149 unhide_lua_table(Luas, "node", node_table_id);
1150 if (luaL_loadfile(Luas, startup_filename)) {
1151 fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
1152 exit(1);
1153 }
1154 lua_pushcfunction(Luas, lua_traceback);
1155 lua_insert(Luas, -2);
1156 if (lua_pcall(Luas, 0, 0, -2)) {
1157 fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
1158 exit(1);
1159 }
1160 lua_remove(Luas, -1);
1161 if (!input_name) {
1162 get_lua_string("texconfig", "jobname", &input_name);
1163 }
1164 if (!dump_name) {
1165 get_lua_string("texconfig", "formatname", &dump_name);
1166 }
1167 kpse_init = -1;
1168 get_lua_boolean("texconfig", "kpse_init", &kpse_init);
1169 if (kpse_init != 0) {
1170 /*tex re-enable loading of texmf.cnf values, see luatex.ch */
1171 luainit = 0;
1172 init_kpse();
1173 kpse_init = 1;
1174 }
1175 /*tex |check_dvi_total_pages| (boolean) */
1176 get_lua_boolean("texconfig", "check_dvi_total_pages", &check_dvi_total_pages);
1177 /*tex |prohibit_file_trace| (boolean) */
1178 get_lua_boolean("texconfig", "trace_file_names", &tracefilenames);
1179 /*tex |trace_extra_newline| (boolean) */
1180 get_lua_boolean("texconfig", "trace_extra_newline", &traceextranewline);
1181 /*tex |file_line_error| */
1182 get_lua_boolean("texconfig", "file_line_error", &filelineerrorstylep);
1183 /*tex |halt_on_error| */
1184 get_lua_boolean("texconfig", "halt_on_error", &haltonerrorp);
1185 /*tex |interactionoption| */
1186 get_lua_number("texconfig", "interaction", &interactionoption);
1187 if ((interactionoption < 0) || (interactionoption > 4)) {
1188 interactionoption = 4;
1189 }
1190 /*tex |restrictedshell| */
1191 v1 = NULL;
1192 get_lua_string("texconfig", "shell_escape", &v1);
1193 if (v1) {
1194 if (*v1 == 't' || *v1 == 'y' || *v1 == '1') {
1195 shellenabledp = 1;
1196 } else if (*v1 == 'p') {
1197 shellenabledp = 1;
1198 restrictedshell = 1;
1199 }
1200 free(v1);
1201 }
1202 UPDATE_SOCKET_STATUS();
1203 /*tex If shell escapes are restricted, get allowed cmds from cnf. */
1204 if (shellenabledp && restrictedshell == 1) {
1205 v1 = NULL;
1206 get_lua_string("texconfig", "shell_escape_commands", &v1);
1207 if (v1) {
1208 mk_shellcmdlist(v1);
1209 free(v1);
1210 }
1211 }
1212 starttime = -1 ;
1213 get_lua_number("texconfig", "start_time", &starttime);
1214 if (starttime < 0) {
1215 /*tex
1216 We provide this one for compatibility reasons and therefore also in
1217 uppercase.
1218 */
1219 get_lua_number("texconfig", "SOURCE_DATE_EPOCH", &starttime);
1220 }
1221 if (starttime >= 0) {
1222 set_start_time(starttime);
1223 }
1224 utc = -1 ;
1225 get_lua_boolean("texconfig", "use_utc_time", &utc);
1226 if (utc >= 0 && utc <= 1) {
1227 utc_option = utc;
1228 }
1229 fix_dumpname();
1230 } else if (luainit) {
1231 if (given_file) {
1232 fprintf(stdout, "%s file %s not found\n", (lua_only ? "Script" : "Configuration"), given_file);
1233 free(given_file);
1234 } else {
1235 fprintf(stdout, "No %s file given\n", (lua_only ? "script" : "configuration"));
1236 }
1237 exit(1);
1238 } else {
1239 /* init */
1240 init_kpse();
1241 kpse_init = 1;
1242 fix_dumpname();
1243 }
1244 if (output_directory) {
1245 xputenv ("TEXMF_OUTPUT_DIRECTORY", output_directory);
1246 } else if (getenv ("TEXMF_OUTPUT_DIRECTORY")) {
1247 output_directory = getenv ("TEXMF_OUTPUT_DIRECTORY");
1248 }
1249 /* the lua debug library is enabled if shell escape permits everything */
1250 if (shellenabledp && restrictedshell != 1) {
1251 luadebug_option = 1 ;
1252 } else {
1253 luaL_unref(Luas, LUA_REGISTRYINDEX, clua_loader_function);
1254 clua_loader_function = 0;
1255 }
1256 /*tex Here we load luatex-core.lua which takes care of some protection on demand. */
1257 if (load_luatex_core_lua(Luas)) {
1258 fprintf(stderr, "Error in execution of luatex-core.lua .\n");
1259 }
1260 }
1261
1262 void check_texconfig_init(void)
1263 {
1264 if (Luas != NULL) {
1265 lua_getglobal(Luas, "texconfig");
1266 if (lua_istable(Luas, -1)) {
1267 lua_getfield(Luas, -1, "init");
1268 if (lua_isfunction(Luas, -1)) {
1269 int i = lua_pcall(Luas, 0, 0, 0);
1270 if (i != 0) {
1271 /*tex
1272 We can't be more precise hereas it's called before \TEX\
1273 initialization happens.
1274 */
1275 fprintf(stderr, "This went wrong: %s\n", lua_tostring(Luas, -1));
1276 error();
1277 }
1278 }
1279 }
1280 }
1281 }