| 1 |
/* luatex.c: Hand-coded routines for TeX or Metafont in C. Originally |
| 2 |
written by Tim Morgan, drawing from other Unix ports of TeX. This is |
| 3 |
a collection of miscellany, everything that's easier (or only |
| 4 |
possible) to do in C. |
| 5 |
|
| 6 |
This file is public domain. */ |
| 7 |
|
| 8 |
/* This file is used to create texextra.c etc., with this line |
| 9 |
changed to include texd.h or mfd.h. The ?d.h file is what |
| 10 |
#defines TeX or MF, which avoids the need for a special |
| 11 |
Makefile rule. */ |
| 12 |
|
| 13 |
/* We |#define DLLPROC| in order to build LuaTeX and LuajitTeX as DLL |
| 14 |
for W32TeX. */ |
| 15 |
|
| 16 |
#if defined LuajitTeX |
| 17 |
#define DLLPROC dllluajittexmain |
| 18 |
#else |
| 19 |
#define DLLPROC dllluatexmain |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "ptexlib.h" |
| 23 |
#include "luatex.h" |
| 24 |
#include "lua/luatex-api.h" |
| 25 |
|
| 26 |
#define TeX |
| 27 |
|
| 28 |
/* |
| 29 |
The version number can be queried with \.{\\luatexversion} and the revision with |
| 30 |
with \.{\\luatexrevision}. Traditionally the revision can be any character and |
| 31 |
pdf\TeX\ occasionally used no digits. Here we still use a character but we will |
| 32 |
stick to "0" upto "9" so users can expect a number represented as string. |
| 33 |
*/ |
| 34 |
|
| 35 |
int luatex_version = 124; |
| 36 |
int luatex_revision = '0'; |
| 37 |
const char *luatex_version_string = "1.24.0"; |
| 38 |
const char *engine_name = my_name; |
| 39 |
|
| 40 |
#include <kpathsea/c-ctype.h> |
| 41 |
#include <kpathsea/line.h> |
| 42 |
#include <kpathsea/readable.h> |
| 43 |
#include <kpathsea/variable.h> |
| 44 |
#include <kpathsea/absolute.h> |
| 45 |
#ifdef WIN32 |
| 46 |
#include <kpathsea/concatn.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
#ifdef _MSC_VER |
| 50 |
#undef timezone |
| 51 |
#endif |
| 52 |
|
| 53 |
#include <time.h> /* For `struct tm'. */ |
| 54 |
#if defined (HAVE_SYS_TIME_H) |
| 55 |
# include <sys/time.h> |
| 56 |
#elif defined (HAVE_SYS_TIMEB_H) |
| 57 |
# include <sys/timeb.h> |
| 58 |
#endif |
| 59 |
|
| 60 |
#if defined(__STDC__) |
| 61 |
# include <locale.h> |
| 62 |
#endif |
| 63 |
|
| 64 |
#include <signal.h> /* Catch interrupts. */ |
| 65 |
|
| 66 |
/* |
| 67 |
Shell escape. |
| 68 |
|
| 69 |
If shellenabledp == 0, all shell escapes are forbidden. |
| 70 |
|
| 71 |
If (shellenabledp == 1 && restrictedshell == 0), any command is allowed for a |
| 72 |
shell escape. |
| 73 |
|
| 74 |
If (shellenabledp == 1 && restrictedshell == 1), only commands given in the |
| 75 |
configuration file as shell_escape_commands = |
| 76 |
kpsewhich,ebb,extractbb,mpost,metafun,... (no spaces between commands) in |
| 77 |
texmf.cnf are allowed for a shell escape in a restricted form: command name |
| 78 |
and arguments should be separated by a white space. The first word should be |
| 79 |
a command name. The quotation character for an argument with spaces, |
| 80 |
including a pathname, should be ". ' should not be used. Internally, all |
| 81 |
arguments are quoted by ' (Unix) or " (Windows) before calling the system() |
| 82 |
function in order to forbid execution of any embedded command. |
| 83 |
|
| 84 |
If the --shell-escape option is given, we set shellenabledp = 1 and |
| 85 |
restrictedshell = 0, i.e., any command is allowed. |
| 86 |
|
| 87 |
If the --shell-restricted option is given, we set shellenabledp = 1 and |
| 88 |
restrictedshell = 1, i.e., only given cmds allowed. |
| 89 |
|
| 90 |
If the --no-shell-escape option is given, we set shellenabledp = -1 (and |
| 91 |
restrictedshell is irrelevant). |
| 92 |
|
| 93 |
If none of these option are given, there are three cases: |
| 94 |
|
| 95 |
(1) In the case where shell_escape = y or shell_escape = t or shell_escape = |
| 96 |
1 it becomes shellenabledp = 1 and restrictedshell = 0, that is, any |
| 97 |
command is allowed. |
| 98 |
(2) In the case where shell_escape = p it becomes shellenabledp = 1 and |
| 99 |
restrictedshell = 1, that is, restricted shell escape is allowed. |
| 100 |
(3) In all other cases, shellenabledp = 0, that is, shell escape is |
| 101 |
forbidden. The value of restrictedshell is irrelevant if shellenabledp == |
| 102 |
0. |
| 103 |
*/ |
| 104 |
|
| 105 |
#ifdef TeX |
| 106 |
|
| 107 |
/* |
| 108 |
The cmdlist is a list of allowed commands which are given like this: |
| 109 |
shell_escape_commands = kpsewhich,ebb,extractbb,mpost,metafun in texmf.cnf. |
| 110 |
*/ |
| 111 |
|
| 112 |
static char **cmdlist = NULL; |
| 113 |
|
| 114 |
void mk_shellcmdlist(char *v) |
| 115 |
{ |
| 116 |
char **p; |
| 117 |
char *q, *r; |
| 118 |
size_t n; |
| 119 |
|
| 120 |
q = v; |
| 121 |
n = 1; |
| 122 |
|
| 123 |
/* |
| 124 |
Analyze the variable shell_escape_commands = foo,bar,... spaces before |
| 125 |
and after (,) are not allowed. |
| 126 |
*/ |
| 127 |
|
| 128 |
while ((r = strchr(q, ',')) != 0) { |
| 129 |
n++; |
| 130 |
q = r + 1; |
| 131 |
} |
| 132 |
if (*q) |
| 133 |
n++; |
| 134 |
cmdlist = (char **) xmalloc(n * sizeof (char *)); |
| 135 |
p = cmdlist; |
| 136 |
q = v; |
| 137 |
while ((r = strchr(q, ',')) != 0) { |
| 138 |
*r = '\0'; |
| 139 |
*p++ = xstrdup (q); |
| 140 |
q = r + 1; |
| 141 |
} |
| 142 |
if (*q) |
| 143 |
*p++ = xstrdup (q); |
| 144 |
*p = NULL; |
| 145 |
} |
| 146 |
|
| 147 |
/* |
| 148 |
Called from maininit. Not static because also called from |
| 149 |
luatexdir/lua/luainit.c. |
| 150 |
|
| 151 |
In order to avoid all kind of time code in the backend code we use a |
| 152 |
function. The start time can be overloaded in several ways: |
| 153 |
|
| 154 |
(1) By setting the environmment variable SOURCE_DATE_EPOCH. This will |
| 155 |
influence the tex parameters, random seed, pdf timestamp and pdf id that is |
| 156 |
derived from the time. This variable is consulted when the kpse library is |
| 157 |
enabled which is analogue to other properties. |
| 158 |
|
| 159 |
(2) By setting the texconfig.start_time variable (as with other variables we |
| 160 |
use the internal name there). This has the same effect as (1) and is provided |
| 161 |
for when kpse is not used to set these variables or when an overloaded is |
| 162 |
wanted. This is analogue to other properties. |
| 163 |
|
| 164 |
When an utc time is needed one can provide the flag --utc. This property is |
| 165 |
independent of this time hackery. This flag has a corresponding texconfig |
| 166 |
option use_utc_time. |
| 167 |
|
| 168 |
To some extend a cleaner solution would be to have a flag that disables all |
| 169 |
variable data in one go (like filenames and so) but we just follow the method |
| 170 |
implemented in pdftex where primitives are used tod disable it. |
| 171 |
|
| 172 |
*/ |
| 173 |
|
| 174 |
static int start_time = -1; |
| 175 |
|
| 176 |
int get_start_time(void) { |
| 177 |
if (start_time < 0) { |
| 178 |
start_time = time((time_t *) NULL); |
| 179 |
} |
| 180 |
return start_time; |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
This one is called as part of the kpse initialization which only happens when |
| 185 |
this library is enabled. |
| 186 |
*/ |
| 187 |
|
| 188 |
#if defined(_MSC_VER) |
| 189 |
#define strtoull _strtoui64 |
| 190 |
#endif |
| 191 |
|
| 192 |
void init_start_time(void) { |
| 193 |
if (start_time < 0) { |
| 194 |
unsigned long long epoch; |
| 195 |
char *endptr; |
| 196 |
/* |
| 197 |
We don't really care how kpse sets up this variable but we prefer to |
| 198 |
just use its abstract interface. |
| 199 |
*/ |
| 200 |
char *source_date_epoch = kpse_var_value("SOURCE_DATE_EPOCH"); |
| 201 |
if (source_date_epoch) { |
| 202 |
errno = 0; |
| 203 |
epoch = strtoull(source_date_epoch, &endptr, 10); |
| 204 |
if (*endptr != '\0' || errno != 0) { |
| 205 |
epoch = 0; |
| 206 |
} |
| 207 |
#if defined(_MSC_VER) |
| 208 |
/* |
| 209 |
We avoid to crash if users test a large value which is not |
| 210 |
supported by Visual Studio 2010: a later time than 3001/01/01 |
| 211 |
20:59:59. |
| 212 |
*/ |
| 213 |
if (epoch > 32535291599ULL) |
| 214 |
epoch = 32535291599ULL; |
| 215 |
#endif |
| 216 |
start_time = epoch; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/* |
| 222 |
This one is used to fetch a value from texconfig which can also be used to |
| 223 |
set properties. This might come in handy when one has other ways to get date |
| 224 |
info in the pdf file. |
| 225 |
*/ |
| 226 |
|
| 227 |
void set_start_time(int s) { |
| 228 |
if (s >= 0) { |
| 229 |
start_time = s ; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
void init_shell_escape(void) |
| 234 |
{ |
| 235 |
if (shellenabledp < 0) { |
| 236 |
/* --no-shell-escape on cmd line */ |
| 237 |
shellenabledp = 0; |
| 238 |
|
| 239 |
} else { |
| 240 |
if (shellenabledp == 0) { |
| 241 |
/* no shell options on cmd line, check cnf */ |
| 242 |
char *v1 = kpse_var_value("shell_escape"); |
| 243 |
if (v1) { |
| 244 |
if (*v1 == 't' || *v1 == 'y' || *v1 == '1') { |
| 245 |
shellenabledp = 1; |
| 246 |
} else if (*v1 == 'p') { |
| 247 |
shellenabledp = 1; |
| 248 |
restrictedshell = 1; |
| 249 |
} |
| 250 |
free(v1); |
| 251 |
} |
| 252 |
} |
| 253 |
/* If shell escapes are restricted, get allowed cmds from cnf. */ |
| 254 |
if (shellenabledp && restrictedshell == 1) { |
| 255 |
char *v2 = kpse_var_value("shell_escape_commands"); |
| 256 |
if (v2) { |
| 257 |
mk_shellcmdlist(v2); |
| 258 |
free(v2); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
# ifdef WIN32 |
| 265 |
# define QUOTE '"' |
| 266 |
# else |
| 267 |
# define QUOTE '\'' |
| 268 |
# endif |
| 269 |
|
| 270 |
# if 0 |
| 271 |
# ifdef WIN32 |
| 272 |
static int char_needs_quote(int c) |
| 273 |
{ |
| 274 |
/* special characters of cmd.exe */ |
| 275 |
return (c == '&' || c == '|' || c == '%' || c == '<' || |
| 276 |
c == '>' || c == ';' || c == ',' || c == '(' || c == ')'); |
| 277 |
} |
| 278 |
# endif |
| 279 |
# endif |
| 280 |
|
| 281 |
static int Isspace(char c) |
| 282 |
{ |
| 283 |
return (c == ' ' || c == '\t'); |
| 284 |
} |
| 285 |
|
| 286 |
/* |
| 287 |
Return values: |
| 288 |
|
| 289 |
-1 : invalid quotation of an argument |
| 290 |
0 : command is not allowed |
| 291 |
2 : restricted shell escape, CMD is allowed. |
| 292 |
|
| 293 |
We set *SAFECMD to a safely-quoted version of *CMD; this is what should get |
| 294 |
executed. And we set CMDNAME to its first word; this is what is checked |
| 295 |
against the shell_escape_commands list. |
| 296 |
*/ |
| 297 |
|
| 298 |
int shell_cmd_is_allowed(const char *cmd, char **safecmd, char **cmdname) |
| 299 |
{ |
| 300 |
char **p; |
| 301 |
char *buf; |
| 302 |
char *c, *d; |
| 303 |
const char *s; |
| 304 |
int pre; |
| 305 |
unsigned spaces; |
| 306 |
int allow = 0; |
| 307 |
|
| 308 |
/* |
| 309 |
pre == 1 means that the previous character is a white space |
| 310 |
pre == 0 means that the previous character is not a white space |
| 311 |
*/ |
| 312 |
buf = xmalloc(strlen(cmd) + 1); |
| 313 |
strcpy(buf, cmd); |
| 314 |
c = buf; |
| 315 |
while (Isspace(*c)) |
| 316 |
c++; |
| 317 |
d = c; |
| 318 |
while (!Isspace(*d) && *d) |
| 319 |
d++; |
| 320 |
*d = '\0'; |
| 321 |
|
| 322 |
/* |
| 323 |
*cmdname is the first word of the command line. For example, *cmdname == |
| 324 |
"kpsewhich" for: |
| 325 |
|
| 326 |
\write18{kpsewhich --progname=dvipdfm --format="other text files" config} |
| 327 |
*/ |
| 328 |
*cmdname = xstrdup(c); |
| 329 |
free(buf); |
| 330 |
|
| 331 |
/* |
| 332 |
Is *cmdname listed in a texmf.cnf variable as shell_escape_commands = |
| 333 |
foo,bar,... ? |
| 334 |
*/ |
| 335 |
p = cmdlist; |
| 336 |
if (p) { |
| 337 |
while (*p) { |
| 338 |
if (strcmp(*p, *cmdname) == 0) { |
| 339 |
/* |
| 340 |
*cmdname is found in the list, so restricted shell escape is |
| 341 |
allowed |
| 342 |
*/ |
| 343 |
allow = 2; |
| 344 |
break; |
| 345 |
} |
| 346 |
p++; |
| 347 |
} |
| 348 |
} |
| 349 |
if (allow == 2) { |
| 350 |
spaces = 0; |
| 351 |
for (s = cmd; *s; s++) { |
| 352 |
if (Isspace(*s)) |
| 353 |
spaces++; |
| 354 |
} |
| 355 |
|
| 356 |
/* allocate enough memory (too much?) */ |
| 357 |
# ifdef WIN32 |
| 358 |
*safecmd = xmalloc(2 * strlen(cmd) + 3 + 2 * spaces); |
| 359 |
# else |
| 360 |
*safecmd = xmalloc(strlen(cmd) + 3 + 2 * spaces); |
| 361 |
# endif |
| 362 |
|
| 363 |
/* make a safe command line *safecmd */ |
| 364 |
s = cmd; |
| 365 |
while (Isspace(*s)) |
| 366 |
s++; |
| 367 |
d = *safecmd; |
| 368 |
while (!Isspace(*s) && *s) |
| 369 |
*d++ = *s++; |
| 370 |
|
| 371 |
pre = 1; |
| 372 |
while (*s) { |
| 373 |
/* |
| 374 |
Quotation given by a user. " should always be used; we transform |
| 375 |
it below. On Unix, if ' is used, simply immediately return a |
| 376 |
quotation error. |
| 377 |
*/ |
| 378 |
if (*s == '\'') { |
| 379 |
return -1; |
| 380 |
} |
| 381 |
|
| 382 |
if (*s == '"') { |
| 383 |
/* |
| 384 |
All arguments are quoted as 'foo' (Unix) or "foo" (Windows) |
| 385 |
before calling system(). Therefore closing QUOTE is necessary |
| 386 |
if the previous character is not a white space. For example: |
| 387 |
|
| 388 |
--format="other text files" becomes |
| 389 |
'--format=''other text files' (Unix) |
| 390 |
"--format"="other text files" (Windows) |
| 391 |
*/ |
| 392 |
if (pre == 0) { |
| 393 |
# ifdef WIN32 |
| 394 |
if (*(s-1) == '=') { |
| 395 |
*(d-1) = QUOTE; |
| 396 |
*d++ = '='; |
| 397 |
} else { |
| 398 |
*d++ = QUOTE; |
| 399 |
} |
| 400 |
# else |
| 401 |
*d++ = QUOTE; |
| 402 |
# endif |
| 403 |
} |
| 404 |
pre = 0; |
| 405 |
/* |
| 406 |
Output the quotation mark for the quoted argument. |
| 407 |
*/ |
| 408 |
*d++ = QUOTE; |
| 409 |
s++; |
| 410 |
|
| 411 |
while (*s != '"') { |
| 412 |
/* |
| 413 |
Illegal use of ', or closing quotation mark is missing |
| 414 |
*/ |
| 415 |
if (*s == '\'' || *s == '\0') |
| 416 |
return -1; |
| 417 |
# if 0 |
| 418 |
# ifdef WIN32 |
| 419 |
if (char_needs_quote(*s)) |
| 420 |
*d++ = '^'; |
| 421 |
# endif |
| 422 |
# endif |
| 423 |
*d++ = *s++; |
| 424 |
} |
| 425 |
/* |
| 426 |
Closing quotation mark will be output afterwards, so we do |
| 427 |
nothing here. |
| 428 |
*/ |
| 429 |
s++; |
| 430 |
/* |
| 431 |
The character after the closing quotation mark should be a |
| 432 |
white space or NULL. |
| 433 |
*/ |
| 434 |
if (!Isspace(*s) && *s) |
| 435 |
return -1; |
| 436 |
/* |
| 437 |
Beginning of a usual argument. |
| 438 |
*/ |
| 439 |
} else if (pre == 1 && !Isspace(*s)) { |
| 440 |
pre = 0; |
| 441 |
*d++ = QUOTE; |
| 442 |
# if 0 |
| 443 |
# ifdef WIN32 |
| 444 |
if (char_needs_quote(*s)) |
| 445 |
*d++ = '^'; |
| 446 |
# endif |
| 447 |
# endif |
| 448 |
*d++ = *s++; |
| 449 |
/* |
| 450 |
Ending of a usual argument. |
| 451 |
*/ |
| 452 |
} else if (pre == 0 && Isspace(*s)) { |
| 453 |
pre = 1; |
| 454 |
/* Closing quotation mark */ |
| 455 |
*d++ = QUOTE; |
| 456 |
*d++ = *s++; |
| 457 |
} else { |
| 458 |
/* |
| 459 |
Copy a character from cmd to *safecmd. |
| 460 |
*/ |
| 461 |
# if 0 |
| 462 |
# ifdef WIN32 |
| 463 |
if (char_needs_quote(*s)) |
| 464 |
*d++ = '^'; |
| 465 |
# endif |
| 466 |
# endif |
| 467 |
*d++ = *s++; |
| 468 |
} |
| 469 |
} |
| 470 |
/* |
| 471 |
End of the command line. |
| 472 |
*/ |
| 473 |
if (pre == 0) { |
| 474 |
*d++ = QUOTE; |
| 475 |
} |
| 476 |
*d = '\0'; |
| 477 |
#if 0 |
| 478 |
#ifdef WIN32 |
| 479 |
{ |
| 480 |
char *p, *q, *r; |
| 481 |
p = *safecmd; |
| 482 |
if (strlen (p) > 2 && p[1] == ':' && !IS_DIR_SEP (p[2])) { |
| 483 |
q = xmalloc (strlen (p) + 2); |
| 484 |
q[0] = p[0]; |
| 485 |
q[1] = p[1]; |
| 486 |
q[2] = '/'; |
| 487 |
q[3] = '\0'; |
| 488 |
strcat (q, (p + 2)); |
| 489 |
free (*safecmd); |
| 490 |
*safecmd = q; |
| 491 |
} else if (!IS_DIR_SEP (p[0]) && !(p[1] == ':' && IS_DIR_SEP (p[2]))) { |
| 492 |
p = kpse_var_value ("SELFAUTOLOC"); |
| 493 |
if (p) { |
| 494 |
r = *safecmd; |
| 495 |
while (*r && !Isspace(*r)) |
| 496 |
r++; |
| 497 |
if (*r == '\0') |
| 498 |
q = concatn ("\"", p, "/", *safecmd, "\"", NULL); |
| 499 |
else { |
| 500 |
*r = '\0'; |
| 501 |
r++; |
| 502 |
while (*r && Isspace(*r)) |
| 503 |
r++; |
| 504 |
if (*r) |
| 505 |
q = concatn ("\"", p, "/", *safecmd, "\" ", r, NULL); |
| 506 |
else |
| 507 |
q = concatn ("\"", p, "/", *safecmd, "\"", NULL); |
| 508 |
} |
| 509 |
free (p); |
| 510 |
free (*safecmd); |
| 511 |
*safecmd = q; |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
#endif |
| 516 |
#endif |
| 517 |
} |
| 518 |
return allow; |
| 519 |
} |
| 520 |
|
| 521 |
#endif |
| 522 |
|
| 523 |
/* What we were invoked as and with. */ |
| 524 |
|
| 525 |
char **argv; |
| 526 |
int argc; |
| 527 |
|
| 528 |
/* The C version of what might wind up in |TEX_format_default|. */ |
| 529 |
|
| 530 |
string dump_name; |
| 531 |
|
| 532 |
/* The C version of the jobname, if given. */ |
| 533 |
|
| 534 |
const_string c_job_name; |
| 535 |
|
| 536 |
const char *luatex_banner; |
| 537 |
|
| 538 |
#ifdef _MSC_VER |
| 539 |
/* Invalid parameter handler */ |
| 540 |
static void myInvalidParameterHandler(const wchar_t * expression, |
| 541 |
const wchar_t * function, |
| 542 |
const wchar_t * file, |
| 543 |
unsigned int line, |
| 544 |
uintptr_t pReserved) |
| 545 |
{ |
| 546 |
/* |
| 547 |
printf(L"Invalid parameter detected in function %s." |
| 548 |
L" File: %s Line: %d\n", function, file, line); |
| 549 |
printf(L"Expression: %s\n", expression); |
| 550 |
|
| 551 |
I return silently to avoid an exit with the error 0xc0000417 (invalid |
| 552 |
parameter) when we use non-embedded fonts in luatex-ja, which works without |
| 553 |
any problem on Unix systems. I hope it is not dangerous. |
| 554 |
*/ |
| 555 |
return; |
| 556 |
} |
| 557 |
#endif |
| 558 |
|
| 559 |
/* |
| 560 |
The entry point: set up for reading the command line, which will happen in |
| 561 |
`topenin', then call the main body. |
| 562 |
*/ |
| 563 |
|
| 564 |
int |
| 565 |
#if defined(DLLPROC) |
| 566 |
DLLPROC (int ac, string *av) |
| 567 |
#else |
| 568 |
main (int ac, string *av) |
| 569 |
#endif |
| 570 |
{ |
| 571 |
# ifdef __EMX__ |
| 572 |
_wildcard(&ac, &av); |
| 573 |
_response(&ac, &av); |
| 574 |
# endif |
| 575 |
|
| 576 |
# ifdef WIN32 |
| 577 |
# ifdef _MSC_VER |
| 578 |
_set_invalid_parameter_handler(myInvalidParameterHandler); |
| 579 |
# endif |
| 580 |
av[0] = kpse_program_basename (av[0]); |
| 581 |
_setmaxstdio(2048); |
| 582 |
/* |
| 583 |
We choose to crash for fatal errors: |
| 584 |
|
| 585 |
SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX); |
| 586 |
*/ |
| 587 |
setmode(fileno(stdin), _O_BINARY); |
| 588 |
# endif |
| 589 |
|
| 590 |
lua_initialize(ac, av); |
| 591 |
|
| 592 |
# ifdef WIN32 |
| 593 |
if (ac > 1) { |
| 594 |
char *pp; |
| 595 |
if ((strlen(av[ac-1]) > 2) && isalpha(av[ac-1][0]) && (av[ac-1][1] == ':') && (av[ac-1][2] == '\\')) { |
| 596 |
for (pp=av[ac-1]+2; *pp; pp++) { |
| 597 |
if (IS_KANJI(pp)) { |
| 598 |
pp++; |
| 599 |
continue; |
| 600 |
} |
| 601 |
if (*pp == '\\') |
| 602 |
*pp = '/'; |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
# endif |
| 607 |
|
| 608 |
/* |
| 609 |
Call the real main program. |
| 610 |
*/ |
| 611 |
main_body(); |
| 612 |
|
| 613 |
return EXIT_SUCCESS; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
/* |
| 618 |
This is supposed to ``open the terminal for input'', but what we really do is |
| 619 |
copy command line arguments into TeX's or Metafont's buffer, so they can |
| 620 |
handle them. If nothing is available, or we've been called already (and |
| 621 |
hence, argc==0), we return with `last=first'. |
| 622 |
*/ |
| 623 |
|
| 624 |
void topenin(void) |
| 625 |
{ |
| 626 |
int i; |
| 627 |
|
| 628 |
buffer[first] = 0; /* In case there are no arguments. */ |
| 629 |
|
| 630 |
if (optind < argc) { |
| 631 |
/* We have command line arguments. */ |
| 632 |
int k = first; |
| 633 |
for (i = optind; i < argc; i++) { |
| 634 |
char *ptr = &(argv[i][0]); |
| 635 |
/* |
| 636 |
We cannot use strcat, because we have multibyte UTF-8 input. |
| 637 |
*/ |
| 638 |
while (*ptr) { |
| 639 |
buffer[k++] = (packed_ASCII_code) * (ptr++); |
| 640 |
} |
| 641 |
buffer[k++] = ' '; |
| 642 |
} |
| 643 |
/* Don't do this again. */ |
| 644 |
argc = 0; |
| 645 |
buffer[k] = 0; |
| 646 |
} |
| 647 |
|
| 648 |
/* |
| 649 |
Find the end of the buffer looking at spaces and newlines. |
| 650 |
*/ |
| 651 |
|
| 652 |
for (last = first; buffer[last]; ++last); |
| 653 |
|
| 654 |
/* |
| 655 |
We conform to the way Web2c does handle trailing tabs and spaces. This |
| 656 |
decade old behaviour was changed in September 2017 and can introduce |
| 657 |
compatibility issues in existing workflows. Because we don't want too |
| 658 |
many differences with upstream TeXlive we just follow up on that patch |
| 659 |
and it's up to macro packages to deal with possible issues (which can be |
| 660 |
done via the usual callbacks. One can wonder why we then still prune |
| 661 |
spaces but we leave that to the reader. |
| 662 |
*/ |
| 663 |
|
| 664 |
/* Patched original comment: |
| 665 |
|
| 666 |
Make `last' be one past the last non-space character in `buffer', |
| 667 |
ignoring line terminators (but not, e.g., tabs). This is because |
| 668 |
we are supposed to treat this like a line of TeX input. Although |
| 669 |
there are pathological cases (SPC CR SPC CR) where this differs |
| 670 |
from input_line below, and from previous behavior of removing all |
| 671 |
whitespace, the simplicity of removing all trailing line terminators |
| 672 |
seems more in keeping with actual command line processing. |
| 673 |
*/ |
| 674 |
|
| 675 |
/* |
| 676 |
The IS_SPC_OR_EOL macro deals with space characters (SPACE 32) and |
| 677 |
newlines (CR and LF) and no longer looks at tabs (TAB 9). |
| 678 |
|
| 679 |
*/ |
| 680 |
|
| 681 |
#define IS_SPC_OR_EOL(c) ((c) == ' ' || (c) == '\r' || (c) == '\n') |
| 682 |
for (--last; last >= first && IS_SPC_OR_EOL (buffer[last]); --last); |
| 683 |
last++; |
| 684 |
/* |
| 685 |
One more time, this time converting to TeX's internal character |
| 686 |
representation. |
| 687 |
*/ |
| 688 |
} |
| 689 |
|
| 690 |
/* |
| 691 |
Normalize quoting of filename -- that is, only quote if there is a space, and |
| 692 |
always use the quote-name-quote style. |
| 693 |
*/ |
| 694 |
|
| 695 |
string normalize_quotes(const_string name, const_string mesg) |
| 696 |
{ |
| 697 |
boolean quoted = false; |
| 698 |
boolean must_quote = (strchr(name, ' ') != NULL); |
| 699 |
/* Leave room for quotes and NUL. */ |
| 700 |
string ret = (string) xmalloc((unsigned) strlen(name) + 3); |
| 701 |
string p; |
| 702 |
const_string q; |
| 703 |
p = ret; |
| 704 |
if (must_quote) |
| 705 |
*p++ = '"'; |
| 706 |
for (q = name; *q; q++) { |
| 707 |
if (*q == '"') |
| 708 |
quoted = !quoted; |
| 709 |
else |
| 710 |
*p++ = *q; |
| 711 |
} |
| 712 |
if (must_quote) |
| 713 |
*p++ = '"'; |
| 714 |
*p = '\0'; |
| 715 |
if (quoted) { |
| 716 |
fprintf(stderr, "! Unbalanced quotes in %s %s\n", mesg, name); |
| 717 |
uexit(1); |
| 718 |
} |
| 719 |
return ret; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
/* |
| 724 |
All our interrupt handler has to do is set TeX's or Metafont's global |
| 725 |
variable `interrupt'; then they will do everything needed. |
| 726 |
*/ |
| 727 |
|
| 728 |
#ifdef WIN32 |
| 729 |
/* Win32 doesn't set SIGINT ... */ |
| 730 |
static BOOL WINAPI catch_interrupt(DWORD arg) |
| 731 |
{ |
| 732 |
switch (arg) { |
| 733 |
case CTRL_C_EVENT: |
| 734 |
case CTRL_BREAK_EVENT: |
| 735 |
interrupt = 1; |
| 736 |
return TRUE; |
| 737 |
default: |
| 738 |
/* No need to set interrupt as we are exiting anyway */ |
| 739 |
return FALSE; |
| 740 |
} |
| 741 |
} |
| 742 |
#else /* not WIN32 */ |
| 743 |
static RETSIGTYPE catch_interrupt(int arg) |
| 744 |
{ |
| 745 |
(void) arg; |
| 746 |
interrupt = 1; |
| 747 |
# ifdef OS2 |
| 748 |
(void) signal(SIGINT, SIG_ACK); |
| 749 |
# else |
| 750 |
(void) signal(SIGINT, catch_interrupt); |
| 751 |
# endif /* not OS2 */ |
| 752 |
} |
| 753 |
#endif /* not WIN32 */ |
| 754 |
|
| 755 |
/* |
| 756 |
Besides getting the date and time here, we also set up the interrupt handler, |
| 757 |
for no particularly good reason. It's just that since the `fix_date_and_time' |
| 758 |
routine is called early on (section 1337 in TeX, ``Get the first line of |
| 759 |
input and prepare to start''), this is as good a place as any. |
| 760 |
*/ |
| 761 |
|
| 762 |
void get_date_and_time(int *minutes, int *day, int *month, int *year) |
| 763 |
{ |
| 764 |
time_t myclock = get_start_time(); |
| 765 |
struct tm *tmptr ; |
| 766 |
if (utc_option) { |
| 767 |
tmptr = gmtime(&myclock); |
| 768 |
} else { |
| 769 |
tmptr = localtime(&myclock); |
| 770 |
} |
| 771 |
|
| 772 |
*minutes = tmptr->tm_hour * 60 + tmptr->tm_min; |
| 773 |
*day = tmptr->tm_mday; |
| 774 |
*month = tmptr->tm_mon + 1; |
| 775 |
*year = tmptr->tm_year + 1900; |
| 776 |
|
| 777 |
{ |
| 778 |
#ifdef SA_INTERRUPT |
| 779 |
|
| 780 |
/* |
| 781 |
Under SunOS 4.1.x, the default action after return from the signal |
| 782 |
handler is to restart the I/O if nothing has been transferred. The |
| 783 |
effect on TeX is that interrupts are ignored if we are waiting for |
| 784 |
input. The following tells the system to return EINTR from read() in |
| 785 |
this case. From [email protected]. |
| 786 |
*/ |
| 787 |
|
| 788 |
struct sigaction a, oa; |
| 789 |
|
| 790 |
a.sa_handler = catch_interrupt; |
| 791 |
sigemptyset(&a.sa_mask); |
| 792 |
sigaddset(&a.sa_mask, SIGINT); |
| 793 |
a.sa_flags = SA_INTERRUPT; |
| 794 |
sigaction(SIGINT, &a, &oa); |
| 795 |
if (oa.sa_handler != SIG_DFL) |
| 796 |
sigaction(SIGINT, &oa, (struct sigaction *) 0); |
| 797 |
#else /* no SA_INTERRUPT */ |
| 798 |
# ifdef WIN32 |
| 799 |
SetConsoleCtrlHandler(catch_interrupt, TRUE); |
| 800 |
# else /* not WIN32 */ |
| 801 |
RETSIGTYPE(*old_handler) (int); |
| 802 |
|
| 803 |
old_handler = signal(SIGINT, catch_interrupt); |
| 804 |
if (old_handler != SIG_DFL) |
| 805 |
signal(SIGINT, old_handler); |
| 806 |
# endif /* not WIN32 */ |
| 807 |
#endif /* no SA_INTERRUPT */ |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
/* |
| 812 |
Getting a high resolution time. |
| 813 |
*/ |
| 814 |
|
| 815 |
void get_seconds_and_micros(int *seconds, int *micros) |
| 816 |
{ |
| 817 |
#if defined (HAVE_GETTIMEOFDAY) |
| 818 |
struct timeval tv; |
| 819 |
gettimeofday(&tv, NULL); |
| 820 |
*seconds = (int)tv.tv_sec; |
| 821 |
*micros = (int)tv.tv_usec; |
| 822 |
#elif defined (HAVE_FTIME) |
| 823 |
struct timeb tb; |
| 824 |
ftime(&tb); |
| 825 |
*seconds = tb.time; |
| 826 |
*micros = tb.millitm * 1000; |
| 827 |
#else |
| 828 |
time_t myclock = time((time_t *) NULL); |
| 829 |
*seconds = (int) myclock; |
| 830 |
*micros = 0; |
| 831 |
#endif |
| 832 |
} |
| 833 |
|
| 834 |
/* |
| 835 |
Generating a better seed numbers |
| 836 |
*/ |
| 837 |
|
| 838 |
int getrandomseed(void) |
| 839 |
{ |
| 840 |
#if defined (HAVE_GETTIMEOFDAY) |
| 841 |
struct timeval tv; |
| 842 |
gettimeofday(&tv, NULL); |
| 843 |
return (int)(tv.tv_usec + 1000000 * tv.tv_usec); |
| 844 |
#elif defined (HAVE_FTIME) |
| 845 |
struct timeb tb; |
| 846 |
ftime(&tb); |
| 847 |
return (tb.millitm + 1000 * tb.time); |
| 848 |
#else |
| 849 |
time_t myclock = get_start_time((time_t *) NULL); |
| 850 |
struct tm *tmptr ; |
| 851 |
if (utc_option) { |
| 852 |
tmptr = gmtime(&myclock); |
| 853 |
} else { |
| 854 |
tmptr = localtime(&myclock); |
| 855 |
} |
| 856 |
return (tmptr->tm_sec + 60 * (tmptr->tm_min + 60 * tmptr->tm_hour)); |
| 857 |
#endif |
| 858 |
} |
| 859 |
|
| 860 |
/* |
| 861 |
Read a line of input as efficiently as possible while still looking like |
| 862 |
Pascal. We set `last' to `first' and return `false' if we get to eof. |
| 863 |
Otherwise, we return `true' and set last = first + length(line except |
| 864 |
trailing whitespace). |
| 865 |
*/ |
| 866 |
|
| 867 |
boolean input_line(FILE * f) |
| 868 |
{ |
| 869 |
int i = EOF; |
| 870 |
|
| 871 |
#ifdef WIN32 |
| 872 |
if (f != Poptr && fileno (f) != fileno (stdin)) { |
| 873 |
long position = ftell (f); |
| 874 |
if (position == 0L) { |
| 875 |
/* Detect and skip Byte order marks. */ |
| 876 |
int k1 = getc (f); |
| 877 |
|
| 878 |
if (k1 != 0xff && k1 != 0xfe && k1 != 0xef) |
| 879 |
rewind (f); |
| 880 |
else { |
| 881 |
int k2 = getc (f); |
| 882 |
if (k2 != 0xff && k2 != 0xfe && k2 != 0xbb) |
| 883 |
rewind (f); |
| 884 |
else if ((k1 == 0xff && k2 == 0xfe) || /* UTF-16(LE) */ |
| 885 |
(k1 == 0xfe && k2 == 0xff)) /* UTF-16(BE) */ |
| 886 |
; |
| 887 |
else { |
| 888 |
int k3 = getc (f); |
| 889 |
int k4 = getc (f); |
| 890 |
if (k1 == 0xef && k2 == 0xbb && k3 == 0xbf && |
| 891 |
k4 >= 0 && k4 <= 0x7e) /* UTF-8 */ |
| 892 |
ungetc (k4, f); |
| 893 |
else |
| 894 |
rewind (f); |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
#endif |
| 900 |
/* |
| 901 |
Recognize either LF or CR as a line terminator. |
| 902 |
*/ |
| 903 |
last = first; |
| 904 |
while (last < buf_size && (i = getc(f)) != EOF && i != '\n' && i != '\r') |
| 905 |
buffer[last++] = (packed_ASCII_code) i; |
| 906 |
|
| 907 |
if (i == EOF && errno != EINTR && last == first) |
| 908 |
return false; |
| 909 |
|
| 910 |
/* |
| 911 |
We didn't get the whole line because our buffer was too small. |
| 912 |
*/ |
| 913 |
if (i != EOF && i != '\n' && i != '\r') { |
| 914 |
fprintf(stderr, "! Unable to read an entire line---bufsize=%u.\n", |
| 915 |
(unsigned) buf_size); |
| 916 |
fputs("Please increase buf_size in texmf.cnf.\n", stderr); |
| 917 |
uexit(1); |
| 918 |
} |
| 919 |
|
| 920 |
buffer[last] = ' '; |
| 921 |
if (last >= max_buf_stack) |
| 922 |
max_buf_stack = last; |
| 923 |
|
| 924 |
/* |
| 925 |
If next char is LF of a CRLF, read it. |
| 926 |
*/ |
| 927 |
if (i == '\r') { |
| 928 |
while ((i = getc(f)) == EOF && errno == EINTR); |
| 929 |
if (i != '\n') |
| 930 |
ungetc(i, f); |
| 931 |
} |
| 932 |
|
| 933 |
/* |
| 934 |
Trim trailing space character (but not, e.g., tabs). We can't have line |
| 935 |
terminators because we stopped reading at the first \r or \n. TeX's rule |
| 936 |
is to strip only trailing spaces (and eols). David Fuchs mentions that |
| 937 |
this stripping was done to ensure portability of TeX documents given the |
| 938 |
padding with spaces on fixed-record "lines" on some systems of the time, |
| 939 |
e.g., IBM VM/CMS and OS/360. |
| 940 |
*/ |
| 941 |
while (last > first && buffer[last - 1] == ' ') |
| 942 |
--last; |
| 943 |
|
| 944 |
/* |
| 945 |
Don't bother using xord if we don't need to. |
| 946 |
*/ |
| 947 |
|
| 948 |
return true; |
| 949 |
} |
| 950 |
|
| 951 |
/* |
| 952 |
Get the job name to be used, which may have been set from the command line. |
| 953 |
*/ |
| 954 |
|
| 955 |
str_number getjobname(str_number name) |
| 956 |
{ |
| 957 |
str_number ret = name; |
| 958 |
if (c_job_name != NULL) |
| 959 |
ret = maketexstring(c_job_name); |
| 960 |
return ret; |
| 961 |
} |