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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
|
% luainit.w
%
% Copyright 2006-2014 Taco Hoekwater <taco@@luatex.org>
%
% This file is part of LuaTeX.
%
% LuaTeX 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.
%
% LuaTeX 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 Lesser General Public
% License for more details.
%
% You should have received a copy of the GNU General Public License along
% with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
@ @c
static const char _svn_version[] =
"$Id: luainit.w 5081 2014-11-07 18:38:33Z luigi $"
"$URL: https://foundry.supelec.fr/svn/luatex/trunk/source/texk/web2c/luatexdir/lua/luainit.w $";
#include "ptexlib.h"
#include <kpathsea/c-stat.h>
#include "lua/luatex-api.h"
/* internalized strings: see luatex-api.h */
set_make_keys;
@
TH: TODO
This file is getting a bit messy, but it is not simple to fix unilaterally.
Better to wait until Karl has some time (after texlive 2008) so we can
synchronize with kpathsea. One problem, for instance, is that I would
like to resolve the full executable path. |kpse_set_program_name()| does
that, indirectly (by setting SELFAUTOLOC in the environment), but it
does much more, making it hard to use for our purpose.
In fact, it sets three C variables:
|kpse_invocation_name| |kpse_invocation_short_name| |kpse->program_name|
and five environment variables:
SELFAUTOLOC SELFAUTODIR SELFAUTOPARENT SELFAUTOGRANDPARENT progname
@c
const_string LUATEX_IHELP[] = {
"Usage: " my_name " --lua=FILE [OPTION]... [TEXNAME[.tex]] [COMMANDS]",
" or: " my_name " --lua=FILE [OPTION]... \\FIRST-LINE",
" or: " my_name " --lua=FILE [OPTION]... &FMT ARGS",
" Run " MyName " on TEXNAME, usually creating TEXNAME.pdf.",
" Any remaining COMMANDS are processed as luatex input, after TEXNAME is read.",
"",
" Alternatively, if the first non-option argument begins with a backslash,",
" " my_name " interprets all non-option arguments as an input line.",
"",
" Alternatively, if the first non-option argument begins with a &, the",
" next word is taken as the FMT to read, overriding all else. Any",
" remaining arguments are processed as above.",
"",
" If no arguments or options are specified, prompt for input.",
"",
" The following regular options are understood: ",
"",
" --8bit ignored, input is assumed to be in UTF-8 encoding",
" --credits display credits and exit",
" --debug-format enable format debugging",
" --default-translate-file= ignored, input is assumed to be in UTF-8 encoding",
" --disable-write18 disable \\write18{SHELL COMMAND}",
" --draftmode switch on draft mode (generates no output PDF)",
" --enable-write18 enable \\write18{SHELL COMMAND}",
" --etex ignored, the etex extensions are always active",
" --[no-]file-line-error disable/enable file:line:error style messages",
" --[no-]file-line-error-style aliases of --[no-]file-line-error",
" --fmt=FORMAT load the format file FORMAT",
" --halt-on-error stop processing at the first error",
" --help display help and exit",
" --ini be ini" my_name ", for dumping formats",
" --interaction=STRING set interaction mode (STRING=batchmode/nonstopmode/scrollmode/errorstopmode)",
" --jobname=STRING set the job name to STRING",
" --kpathsea-debug=NUMBER set path searching debugging flags according to the bits of NUMBER",
" --lua=s load and execute a lua initialization script",
" --[no-]mktex=FMT disable/enable mktexFMT generation (FMT=tex/tfm)",
" --nosocket disable the lua socket library",
" --output-comment=STRING use STRING for DVI file comment instead of date (no effect for PDF)",
" --output-directory=DIR use existing DIR as the directory to write files in",
" --output-format=FORMAT use FORMAT for job output; FORMAT is 'dvi' or 'pdf'",
" --[no-]parse-first-line disable/enable parsing of the first line of the input file",
" --progname=STRING set the program name to STRING",
" --recorder enable filename recorder",
" --safer disable easily exploitable lua commands",
" --[no-]shell-escape disable/enable \\write18{SHELL COMMAND}",
" --shell-restricted restrict \\write18 to a list of commands given in texmf.cnf",
" --synctex=NUMBER enable synctex",
" --translate-file= ignored, input is assumed to be in UTF-8 encoding",
" --version display version and exit",
"",
"Alternate behaviour models can be obtained by special switches",
"",
" --luaonly run a lua file, then exit",
" --luaconly byte-compile a lua file, then exit",
" --luahashchars the bits used by current Lua interpreter for strings hashing",
#ifdef LuajitTeX
" --jiton turns the JIT compiler on (default off)",
" --jithash=STRING choose the hash function for the lua strings (lua51|luajit20: default lua51)",
#endif
"",
"See the reference manual for more information about the startup process.",
NULL
};
@ The return value will be the directory of the executable, e.g.: \.{c:/TeX/bin}
@c
static char *ex_selfdir(char *argv0)
{
#if defined(WIN32)
#if defined(__MINGW32__)
char path[PATH_MAX], *fp;
/* SearchPath() always gives back an absolute directory */
if (SearchPath(NULL, argv0, ".exe", PATH_MAX, path, NULL) == 0)
FATAL1("Can't determine where the executable %s is.\n", argv0);
/* slashify the dirname */
for (fp = path; fp && *fp; fp++)
if (IS_DIR_SEP(*fp))
*fp = DIR_SEP;
#else /* __MINGW32__ */
#define PATH_MAX 512
char short_path[PATH_MAX], path[PATH_MAX], *fp;
/* SearchPath() always gives back an absolute directory */
if (SearchPath(NULL, argv0, ".exe", PATH_MAX, short_path, &fp) == 0)
FATAL1("Can't determine where the executable %s is.\n", argv0);
if (getlongpath(path, short_path, sizeof(path)) == 0) {
FATAL1("This path points to an invalid file : %s\n", short_path);
}
#endif /* __MINGW32__ */
return xdirname(path);
#else /* WIN32 */
return kpse_selfdir(argv0);
#endif
}
@ @c
static void
prepare_cmdline(lua_State * L, char **av, int ac, int zero_offset)
{
int i;
char *s;
luaL_checkstack(L, ac + 3, "too many arguments to script");
lua_createtable(L, 0, 0);
for (i = 0; i < ac; i++) {
lua_pushstring(L, av[i]);
lua_rawseti(L, -2, (i - zero_offset));
}
lua_setglobal(L, "arg");
lua_getglobal(L, "os");
s = ex_selfdir(argv[0]);
lua_pushstring(L, s);
xfree(s);
lua_setfield(L, -2, "selfdir");
return;
}
@ @c
string input_name = NULL;
static string user_progname = NULL;
char *startup_filename = NULL;
int lua_only = 0;
int lua_offset = 0;
unsigned char show_luahashchars = 0;
#ifdef LuajitTeX
int luajiton = 0;
char *jithash_hashname = NULL;
#endif
int safer_option = 0;
int nosocket_option = 0;
@ Reading the options.
@ Test whether getopt found an option ``A''.
Assumes the option index is in the variable |option_index|, and the
option table in a variable |long_options|.
@c
#define ARGUMENT_IS(a) STREQ (long_options[option_index].name, a)
/* SunOS cc can't initialize automatic structs, so make this static. */
static struct option long_options[]
= { {"fmt", 1, 0, 0},
{"lua", 1, 0, 0},
{"luaonly", 0, 0, 0},
{"luahashchars", 0, 0, 0},
#ifdef LuajitTeX
{"jiton", 0, 0, 0},
{"jithash", 1, 0, 0},
#endif
{"safer", 0, &safer_option, 1},
{"nosocket", 0, &nosocket_option, 1},
{"help", 0, 0, 0},
{"ini", 0, &ini_version, 1},
{"interaction", 1, 0, 0},
{"halt-on-error", 0, &haltonerrorp, 1},
{"kpathsea-debug", 1, 0, 0},
{"progname", 1, 0, 0},
{"version", 0, 0, 0},
{"credits", 0, 0, 0},
{"recorder", 0, &recorder_enabled, 1},
{"etex", 0, 0, 0},
{"output-comment", 1, 0, 0},
{"output-directory", 1, 0, 0},
{"draftmode", 0, 0, 0},
{"output-format", 1, 0, 0},
{"shell-escape", 0, &shellenabledp, 1},
{"no-shell-escape", 0, &shellenabledp, -1},
{"enable-write18", 0, &shellenabledp, 1},
{"disable-write18", 0, &shellenabledp, -1},
{"shell-restricted", 0, 0, 0},
{"debug-format", 0, &debug_format_file, 1},
{"file-line-error-style", 0, &filelineerrorstylep, 1},
{"no-file-line-error-style", 0, &filelineerrorstylep, -1},
/* Shorter option names for the above. */
{"file-line-error", 0, &filelineerrorstylep, 1},
{"no-file-line-error", 0, &filelineerrorstylep, -1},
{"jobname", 1, 0, 0},
{"parse-first-line", 0, &parsefirstlinep, 1},
{"no-parse-first-line", 0, &parsefirstlinep, -1},
{"translate-file", 1, 0, 0},
{"default-translate-file", 1, 0, 0},
{"8bit", 0, 0, 0},
{"mktex", 1, 0, 0},
{"no-mktex", 1, 0, 0},
/* Synchronization: just like "interaction" above */
{"synctex", 1, 0, 0},
{0, 0, 0, 0}
};
@ @c
int lua_numeric_field_by_index(lua_State * L, int name_index, int dflt)
{
register int i = dflt;
lua_rawgeti(L, LUA_REGISTRYINDEX, name_index); /* fetch the stringptr */
lua_rawget(L, -2);
if (lua_type(L, -1) == LUA_TNUMBER) {
i = lua_roundnumber(L, -1);
}
lua_pop(L, 1);
return i;
}
@ @c
static void parse_options(int ac, char **av)
{
#ifdef WIN32
/* save argc and argv */
int sargc = argc;
char **sargv = argv;
#endif
int g; /* `getopt' return code. */
int option_index;
char *firstfile = NULL;
opterr = 0; /* dont whine */
#ifdef LuajitTeX
if ((strstr(argv[0], "luajittexlua") != NULL) ||
(strstr(argv[0], "texluajit") != NULL)) {
#else
if ((strstr(argv[0], "luatexlua") != NULL) ||
(strstr(argv[0], "texlua") != NULL)) {
#endif
lua_only = 1;
luainit = 1;
}
for (;;) {
g = getopt_long_only(ac, av, "+", long_options, &option_index);
if (g == -1) /* End of arguments, exit the loop. */
break;
if (g == '?') { /* Unknown option. */
if (!luainit)
fprintf(stderr,"%s: unrecognized option '%s'\n", argv[0], argv[optind-1]);
continue;
}
assert(g == 0); /* We have no short option names. */
if (ARGUMENT_IS("luaonly")) {
lua_only = 1;
lua_offset = optind;
luainit = 1;
} else if (ARGUMENT_IS("lua")) {
startup_filename = optarg;
lua_offset = (optind - 1);
luainit = 1;
#ifdef LuajitTeX
} else if (ARGUMENT_IS("jiton")) {
luajiton = 1;
} else if (ARGUMENT_IS("jithash")) {
size_t len = strlen(optarg);
if (len<16)
jithash_hashname = optarg;
else{
WARNING2("hash name truncated to 15 characters from %d. (%s)",
(int) len, optarg);
jithash_hashname = (string) xmalloc(16);
strncpy(jithash_hashname, optarg, 15);
jithash_hashname[15] = 0;
}
#endif
} else if (ARGUMENT_IS("luahashchars")) {
show_luahashchars = 1;
} else if (ARGUMENT_IS("kpathsea-debug")) {
kpathsea_debug |= atoi(optarg);
} else if (ARGUMENT_IS("progname")) {
user_progname = optarg;
} else if (ARGUMENT_IS("jobname")) {
c_job_name = optarg;
} else if (ARGUMENT_IS("fmt")) {
dump_name = optarg;
} else if (ARGUMENT_IS("output-directory")) {
output_directory = optarg;
} else if (ARGUMENT_IS("output-comment")) {
size_t len = strlen(optarg);
if (len < 256) {
output_comment = optarg;
} else {
WARNING2("Comment truncated to 255 characters from %d. (%s)",
(int) len, optarg);
output_comment = (string) xmalloc(256);
strncpy(output_comment, optarg, 255);
output_comment[255] = 0;
}
} else if (ARGUMENT_IS("shell-restricted")) {
shellenabledp = 1;
restrictedshell = 1;
} else if (ARGUMENT_IS("output-format")) {
pdf_output_option = 1;
if (strcmp(optarg, "dvi") == 0) {
pdf_output_value = 0;
} else if (strcmp(optarg, "pdf") == 0) {
pdf_output_value = 2;
} else {
WARNING1("Ignoring unknown value `%s' for --output-format",
optarg);
pdf_output_option = 0;
}
} else if (ARGUMENT_IS("draftmode")) {
pdf_draftmode_option = 1;
pdf_draftmode_value = 1;
} else if (ARGUMENT_IS("mktex")) {
kpse_maketex_option(optarg, true);
} else if (ARGUMENT_IS("no-mktex")) {
kpse_maketex_option(optarg, false);
} else if (ARGUMENT_IS("interaction")) {
/* These numbers match CPP defines */
if (STREQ(optarg, "batchmode")) {
interactionoption = 0;
} else if (STREQ(optarg, "nonstopmode")) {
interactionoption = 1;
} else if (STREQ(optarg, "scrollmode")) {
interactionoption = 2;
} else if (STREQ(optarg, "errorstopmode")) {
interactionoption = 3;
} else {
WARNING1("Ignoring unknown argument `%s' to --interaction",
optarg);
}
} else if (ARGUMENT_IS("synctex")) {
/* Synchronize TeXnology: catching the command line option as a long */
synctexoption = (int) strtol(optarg, NULL, 0);
} else if (ARGUMENT_IS("help")) {
usagehelp(LUATEX_IHELP, BUG_ADDRESS);
} else if (ARGUMENT_IS("version")) {
print_version_banner();
/* *INDENT-OFF* */
puts("\n\nExecute '" my_name " --credits' for credits and version details.\n\n"
"There is NO warranty. Redistribution of this software is covered by\n"
"the terms of the GNU General Public License, version 2 or (at your option)\n"
"any later version. For more information about these matters, see the file\n"
"named COPYING and the LuaTeX source.\n\n"
#ifdef LuajitTeX
"LuaTeX is Copyright 2014 Taco Hoekwater, the LuaTeX Team.\n"
"Libraries and JIT extensions by Luigi Scarso, the LuaTeX SwigLib team.\n");
#else
"Copyright 2014 Taco Hoekwater, the LuaTeX Team.\n");
#endif
/* *INDENT-ON* */
uexit(0);
} else if (ARGUMENT_IS("credits")) {
char *versions;
initversionstring(&versions);
print_version_banner();
/* *INDENT-OFF* */
puts("\n\nThe LuaTeX team is Hans Hagen, Hartmut Henkel, Taco Hoekwater.\n"
MyName " merges and builds upon (parts of) the code from these projects:\n\n"
"tex by Donald Knuth\n"
"etex by Peter Breitenlohner, Phil Taylor and friends\n"
"omega by John Plaice and Yannis Haralambous\n"
"aleph by Giuseppe Bilotta\n"
"pdftex by Han The Thanh and friends\n"
"kpathsea by Karl Berry, Olaf Weber and others\n"
"lua by Roberto Ierusalimschy, Waldemar Celes,\n"
" Luiz Henrique de Figueiredo\n"
"metapost by John Hobby, Taco Hoekwater and friends.\n"
"poppler by Derek Noonburg, Kristian H\\ogsberg (partial)\n"
#ifdef LuajitTeX
"fontforge by George Williams (partial)\n"
"luajit by Mike Pall\n\n"
#else
"fontforge by George Williams (partial)\n\n"
#endif
"Some extensions to lua and additional lua libraries are used, as well as\n"
"libraries for graphic inclusion. More details can be found in the source.\n"
"Code development was sponsored by a grant from Colorado State University\n"
#ifdef LuajitTeX
"via the 'oriental tex' project, the TeX User Groups, and donations.\n"
"The additional libraries and the LuaJIT extensions are provided by the LuaTeX SwigLib project.\n");
#else
"via the 'oriental tex' project, the TeX User Groups, and donations.\n");
#endif
/* *INDENT-ON* */
puts(versions);
uexit(0);
}
}
/* attempt to find |input_name| / |dump_name| */
if (lua_only) {
if (argv[optind]) {
startup_filename = xstrdup(argv[optind]);
lua_offset = optind;
}
} else if (argv[optind] && argv[optind][0] == '&') {
dump_name = xstrdup(argv[optind] + 1);
} else if (argv[optind] && argv[optind][0] != '\\') {
if (argv[optind][0] == '*') {
input_name = xstrdup(argv[optind] + 1);
} else {
firstfile = xstrdup(argv[optind]);
if ((strstr(firstfile, ".lua") ==
firstfile + strlen(firstfile) - 4)
|| (strstr(firstfile, ".luc") ==
firstfile + strlen(firstfile) - 4)
|| (strstr(firstfile, ".LUA") ==
firstfile + strlen(firstfile) - 4)
|| (strstr(firstfile, ".LUC") ==
firstfile + strlen(firstfile) - 4)) {
if (startup_filename == NULL) {
startup_filename = firstfile;
lua_offset = optind;
lua_only = 1;
luainit = 1;
}
} else {
input_name = firstfile;
}
}
#ifdef WIN32
} else if (sargv[sargc-1] && sargv[sargc-1][0] != '-' &&
sargv[sargc-1][0] != '\\') {
if (sargv[sargc-1][0] == '&')
dump_name = xstrdup(sargv[sargc-1] + 1);
else {
char *p;
if (sargv[sargc-1][0] == '*')
input_name = xstrdup(sargv[sargc-1] + 1);
else
input_name = xstrdup(sargv[sargc-1]);
sargv[sargc-1] = normalize_quotes(input_name, "argument");
/* Same as
input_name = (char *)xbasename(input_name);
but without cast const => non-const. */
input_name += xbasename(input_name) - input_name;
p = strrchr(input_name, '.');
if (p != NULL && strcasecmp(p, ".tex") == 0)
*p = '\0';
if (!c_job_name)
c_job_name = normalize_quotes(input_name, "jobname");
}
if (safer_option) /* --safer implies --nosocket */
nosocket_option = 1;
return;
#endif
}
if (safer_option) /* --safer implies --nosocket */
nosocket_option = 1;
/* Finalize the input filename. */
if (input_name != NULL) {
argv[optind] = normalize_quotes(input_name, "argument");
}
}
@ test for readability
@c
#define is_readable(a) (stat(a,&finfo)==0) && S_ISREG(finfo.st_mode) && \
(f=fopen(a,"r")) != NULL && !fclose(f)
@ @c
static char *find_filename(char *name, const char *envkey)
{
struct stat finfo;
char *dirname = NULL;
char *filename = NULL;
FILE *f;
if (is_readable(name)) {
return name;
} else {
dirname = getenv(envkey);
if ((dirname != NULL) && strlen(dirname)) {
dirname = xstrdup(getenv(envkey));
if (*(dirname + strlen(dirname) - 1) == '/') {
*(dirname + strlen(dirname) - 1) = 0;
}
filename = xmalloc((unsigned) (strlen(dirname) + strlen(name) + 2));
filename = concat3(dirname, "/", name);
if (is_readable(filename)) {
xfree(dirname);
return filename;
}
xfree(filename);
}
}
return NULL;
}
@ @c
static void init_kpse(void)
{
if (!user_progname) {
user_progname = dump_name;
} else if (!dump_name) {
dump_name = user_progname;
}
if (!user_progname) {
if (ini_version) {
if (input_name) {
char *p = input_name + strlen(input_name) - 1;
while (p >= input_name) {
if (IS_DIR_SEP (*p)) {
p++;
input_name = p;
break;
}
p--;
}
user_progname = remove_suffix (input_name);
}
if (!user_progname) {
user_progname = kpse_program_basename(argv[0]);
}
} else {
if (!dump_name) {
dump_name = kpse_program_basename(argv[0]);
}
user_progname = dump_name;
}
}
kpse_set_program_enabled(kpse_fmt_format, MAKE_TEX_FMT_BY_DEFAULT,
kpse_src_compile);
kpse_set_program_name(argv[0], user_progname);
init_shell_escape(); /* set up 'restrictedshell' */
program_name_set = 1;
}
@ @c
static void fix_dumpname(void)
{
int dist;
if (dump_name) {
/* adjust array for Pascal and provide extension, if needed */
dist = (int) (strlen(dump_name) - strlen(DUMP_EXT));
if (strstr(dump_name, DUMP_EXT) == dump_name + dist)
TEX_format_default = dump_name;
else
TEX_format_default = concat(dump_name, DUMP_EXT);
} else {
/* For |dump_name| to be NULL is a bug. */
if (!ini_version)
abort();
}
}
@ lua require patch
@ Auxiliary function for kpse search
@c
static const char *luatex_kpse_find_aux(lua_State *L, const char *name,
kpse_file_format_type format, const char *errname)
{
const char *filename;
const char *altname;
altname = luaL_gsub(L, name, ".", "/"); /* Lua convention */
filename = kpse_find_file(altname, format, false);
if (filename == NULL) {
filename = kpse_find_file(name, format, false);
}
if (filename == NULL) {
lua_pushfstring(L, "\n\t[kpse %s searcher] file not found: " LUA_QS,
errname, name);
}
return filename;
}
@ The lua search function.
When kpathsea is not initialized, then it runs the
normal lua function that is saved in the registry, otherwise
it uses kpathsea.
two registry ref variables are needed: one for the actual lua
function, the other for its environment .
@c
static int lua_loader_function = 0;
static int luatex_kpse_lua_find(lua_State * L)
{
const char *filename;
const char *name;
name = luaL_checkstring(L, 1);
if (program_name_set == 0) {
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_loader_function);
lua_pushvalue(L, -2);
lua_call(L, 1, 1);
return 1;
}
filename = luatex_kpse_find_aux(L, name, kpse_lua_format, "lua");
if (filename == NULL)
return 1; /* library not found in this path */
if (luaL_loadfile(L, filename) != 0) {
luaL_error(L, "error loading module %s from file %s:\n\t%s",
lua_tostring(L, 1), filename, lua_tostring(L, -1));
}
return 1; /* library loaded successfully */
}
@ @c
static int clua_loader_function = 0;
extern int searcher_C_luatex (lua_State *L, const char *name, const char *filename);
static int luatex_kpse_clua_find(lua_State * L)
{
const char *filename;
const char *name;
if (safer_option) {
lua_pushliteral(L, "\n\t[C searcher disabled in safer mode]");
return 1; /* library not found in this path */
}
name = luaL_checkstring(L, 1);
if (program_name_set == 0) {
lua_rawgeti(L, LUA_REGISTRYINDEX, clua_loader_function);
lua_pushvalue(L, -2);
lua_call(L, 1, 1);
return 1;
} else {
const char *path_saved;
char *prefix, *postfix, *p, *total;
char *extensionless;
char *temp_name;
int j;
filename = luatex_kpse_find_aux(L, name, kpse_clua_format, "C");
if (filename == NULL)
return 1; /* library not found in this path */
extensionless = strdup(filename);
if (!extensionless) return 1; /* allocation failure */
/* Fix Issue 850: replace '.' with LUA_DIRSEP */
temp_name = strdup(name);
for(j=0; ; j++){
if ((unsigned char)temp_name[j]=='\0') {
break;
}
if ((unsigned char)temp_name[j]=='.'){
temp_name[j]=LUA_DIRSEP[0];
}
}
p = strstr(extensionless, temp_name);
if (!p) return 1; /* this would be exceedingly weird */
*p = '\0';
prefix = strdup(extensionless);
if (!prefix) return 1; /* allocation failure */
postfix = strdup(p+strlen(name));
if (!postfix) return 1; /* allocation failure */
total = malloc(strlen(prefix)+strlen(postfix)+2);
if (!total) return 1; /* allocation failure */
snprintf(total,strlen(prefix)+strlen(postfix)+2, "%s?%s", prefix, postfix);
/* save package.path */
lua_getglobal(L,"package");
lua_getfield(L,-1,"cpath");
path_saved = lua_tostring(L,-1);
lua_pop(L,1);
/* set package.path = "?" */
lua_pushstring(L,total);
lua_setfield(L,-2,"cpath");
lua_pop(L,1); /* pop "package" */
/* run function */
lua_rawgeti(L, LUA_REGISTRYINDEX, clua_loader_function);
lua_pushstring(L, name);
lua_call(L, 1, 1);
/* restore package.path */
lua_getglobal(L,"package");
lua_pushstring(L,path_saved);
lua_setfield(L,-2,"cpath");
lua_pop(L,1); /* pop "package" */
free(extensionless);
free(total);
free(temp_name);
return 1;
}
}
@ Setting up the new search functions.
This replaces package.searchers[2] and package.searchers[3] with the
functions defined above.
@c
static void setup_lua_path(lua_State * L)
{
lua_getglobal(L, "package");
#ifdef LuajitTeX
lua_getfield(L, -1, "loaders");
#else
lua_getfield(L, -1, "searchers");
#endif
lua_rawgeti(L, -1, 2); /* package.searchers[2] */
lua_loader_function = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushcfunction(L, luatex_kpse_lua_find);
lua_rawseti(L, -2, 2); /* replace the normal lua loader */
lua_rawgeti(L, -1, 3); /* package.searchers[3] */
clua_loader_function = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushcfunction(L, luatex_kpse_clua_find);
lua_rawseti(L, -2, 3); /* replace the normal lua lib loader */
lua_pop(L, 2); /* pop the array and table */
}
@ helper variables for the safe keeping of table ids
@c
int tex_table_id;
int pdf_table_id;
int token_table_id;
int node_table_id;
@ @c
int l_pack_type_index [PACK_TYPE_SIZE] ;
int l_group_code_index [GROUP_CODE_SIZE];
int l_math_style_name_index [MATH_STYLE_NAME_SIZE];
int l_dir_par_index [DIR_PAR_SIZE];
int l_dir_text_index [DIR_TEXT_SIZE];
#if defined(WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
char **suffixlist;
# define EXE_SUFFIXES ".com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.ws;.tcl;.py;.pyw"
@ @c
static void mk_suffixlist(void)
{
char **p;
char *q, *r, *v;
int n;
# if defined(__CYGWIN__)
v = xstrdup(EXE_SUFFIXES);
# else
v = (char *) getenv("PATHEXT");
if (v) /* strlwr() exists also in MingW */
v = (char *) strlwr(xstrdup(v));
else
v = xstrdup(EXE_SUFFIXES);
# endif
q = v;
n = 0;
while ((r = strchr(q, ';')) != NULL) {
n++;
r++;
q = r;
}
if (*q)
n++;
suffixlist = (char **) xmalloc((n + 2) * sizeof(char *));
p = suffixlist;
*p = xstrdup(".dll");
p++;
q = v;
while ((r = strchr(q, ';')) != NULL) {
*r = '\0';
*p = xstrdup(q);
p++;
r++;
q = r;
}
if (*q) {
*p = xstrdup(q);
p++;
}
*p = NULL;
free(v);
}
#endif
@ @c
void lua_initialize(int ac, char **av)
{
char *given_file = NULL;
char *banner;
int kpse_init;
static char LC_CTYPE_C[] = "LC_CTYPE=C";
static char LC_COLLATE_C[] = "LC_COLLATE=C";
static char LC_NUMERIC_C[] = "LC_NUMERIC=C";
static char engine_luatex[] = "engine=" my_name;
/* Save to pass along to topenin. */
argc = ac;
argv = av;
if (luatex_svn < 0) {
const char *fmt = "This is " MyName ", Version %s" WEB2CVERSION;
size_t len;
len = strlen(fmt) + strlen(luatex_version_string) ;
banner = xmalloc(len);
sprintf(banner, fmt, luatex_version_string);
} else {
const char *fmt = "This is " MyName ", Version %s" WEB2CVERSION " (rev %d)";
size_t len;
len = strlen(fmt) + strlen(luatex_version_string) + 6;
banner = xmalloc(len);
sprintf(banner, fmt, luatex_version_string, luatex_svn);
}
ptexbanner = banner;
kpse_invocation_name = kpse_program_basename(argv[0]);
/* be 'luac' */
if (argc >1) {
#ifdef LuajitTeX
if (FILESTRCASEEQ(kpse_invocation_name, "texluajitc"))
exit(luac_main(ac, av));
if (STREQ(argv[1], "--luaconly") || STREQ(argv[1], "--luac")) {
char *argv1 = xmalloc (strlen ("luajittex") + 1);
av[1] = argv1;
strcpy (av[1], "luajittex");
exit(luac_main(--ac, ++av));
}
#else
if (FILESTRCASEEQ(kpse_invocation_name, "texluac"))
exit(luac_main(ac, av));
if (STREQ(argv[1], "--luaconly") || STREQ(argv[1], "--luac")) {
strcpy(av[1], "luatex");
exit(luac_main(--ac, ++av));
}
#endif
}
#if defined(WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
mk_suffixlist();
#endif
/* Must be initialized before options are parsed. */
interactionoption = 4;
dump_name = NULL;
/* 0 means "disable Synchronize TeXnology".
synctexoption is a *.web variable.
We initialize it to a weird value to catch the -synctex command line flag
At runtime, if synctexoption is not |INT_MAX|, then it contains the command line option provided,
otherwise no such option was given by the user. */
#define SYNCTEX_NO_OPTION INT_MAX
synctexoption = SYNCTEX_NO_OPTION;
/* parse commandline */
parse_options(ac, av);
if (lua_only)
shellenabledp = true;
/* make sure that the locale is 'sane' (for lua) */
putenv(LC_CTYPE_C);
putenv(LC_COLLATE_C);
putenv(LC_NUMERIC_C);
/* this is sometimes needed */
putenv(engine_luatex);
luainterpreter();
/* init internalized strings */
set_init_keys;
lua_pushstring(Luas,"lua.functions");
lua_newtable(Luas);
lua_settable(Luas,LUA_REGISTRYINDEX);
/* here start the key definitions */
set_pack_type_index;
set_l_group_code_index;
set_l_math_style_name_index;
set_l_dir_par_index;
set_l_dir_text_index;
prepare_cmdline(Luas, argv, argc, lua_offset); /* collect arguments */
setup_lua_path(Luas);
if (startup_filename != NULL) {
given_file = xstrdup(startup_filename);
startup_filename = find_filename(startup_filename, "LUATEXDIR");
}
/* now run the file */
if (startup_filename != NULL) {
char *v1;
/* hide the 'tex' and 'pdf' table */
tex_table_id = hide_lua_table(Luas, "tex");
token_table_id = hide_lua_table(Luas, "token");
node_table_id = hide_lua_table(Luas, "node");
pdf_table_id = hide_lua_table(Luas, "pdf");
if (luaL_loadfile(Luas, startup_filename)) {
fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
exit(1);
}
/* */
init_tex_table(Luas);
if (lua_pcall(Luas, 0, 0, 0)) {
fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
lua_traceback(Luas);
exit(1);
}
/* no filename? quit now! */
if (!input_name) {
get_lua_string("texconfig", "jobname", &input_name);
}
if (!dump_name) {
get_lua_string("texconfig", "formatname", &dump_name);
}
if (lua_only) {
if (given_file)
free(given_file);
/* this is not strictly needed but it pleases valgrind */
lua_close(Luas);
exit(0);
}
/* unhide the 'tex' and 'pdf' table */
unhide_lua_table(Luas, "tex", tex_table_id);
unhide_lua_table(Luas, "pdf", pdf_table_id);
unhide_lua_table(Luas, "token", token_table_id);
unhide_lua_table(Luas, "node", node_table_id);
/* |kpse_init| */
kpse_init = -1;
get_lua_boolean("texconfig", "kpse_init", &kpse_init);
if (kpse_init != 0) {
luainit = 0; /* re-enable loading of texmf.cnf values, see luatex.ch */
init_kpse();
}
/* |prohibit_file_trace| (boolean) */
tracefilenames = 1;
get_lua_boolean("texconfig", "trace_file_names", &tracefilenames);
/* |file_line_error| */
filelineerrorstylep = false;
get_lua_boolean("texconfig", "file_line_error", &filelineerrorstylep);
/* |halt_on_error| */
haltonerrorp = false;
get_lua_boolean("texconfig", "halt_on_error", &haltonerrorp);
/* |restrictedshell| */
v1 = NULL;
get_lua_string("texconfig", "shell_escape", &v1);
if (v1) {
if (*v1 == 't' || *v1 == 'y' || *v1 == '1') {
shellenabledp = 1;
} else if (*v1 == 'p') {
shellenabledp = 1;
restrictedshell = 1;
}
free(v1);
}
/* If shell escapes are restricted, get allowed cmds from cnf. */
if (shellenabledp && restrictedshell == 1) {
v1 = NULL;
get_lua_string("texconfig", "shell_escape_commands", &v1);
if (v1) {
mk_shellcmdlist(v1);
free(v1);
}
}
fix_dumpname();
} else {
if (luainit) {
if (given_file) {
fprintf(stdout, "%s file %s not found\n",
(lua_only ? "Script" : "Configuration"), given_file);
free(given_file);
} else {
fprintf(stdout, "No %s file given\n",
(lua_only ? "script" : "configuration"));
}
exit(1);
} else {
/* init */
init_kpse();
fix_dumpname();
}
}
}
@ @c
void check_texconfig_init(void)
{
if (Luas != NULL) {
lua_getglobal(Luas, "texconfig");
if (lua_istable(Luas, -1)) {
lua_getfield(Luas, -1, "init");
if (lua_isfunction(Luas, -1)) {
int i = lua_pcall(Luas, 0, 0, 0);
if (i != 0) {
/* Can't be more precise here, called before TeX initialization */
fprintf(stderr, "This went wrong: %s\n",
lua_tostring(Luas, -1));
error();
}
}
}
}
}
@ @c
void write_svnversion(char *v)
{
char *a_head, *n;
char *a = xstrdup(v);
size_t l = strlen("$Id: luatex.web ");
if (a != NULL) {
a_head = a;
if (strlen(a) > l)
a += l;
n = a;
while (*n != '\0' && *n != ' ')
n++;
*n = '\0';
fprintf(stdout, " luatex.web >= v%s", a);
free(a_head);
}
}
|