| 1 |
/* |
| 2 |
|
| 3 |
errors.w |
| 4 |
|
| 5 |
Copyright 2009-2010 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 |
#define edit_var "TEXEDIT" |
| 26 |
|
| 27 |
/*tex |
| 28 |
|
| 29 |
When something anomalous is detected, \TeX\ typically does something like this: |
| 30 |
$$\vbox{\halign{#\hfil\cr |print_err("Something anomalous has been |
| 31 |
detected");|\cr |help3("This is the first line of my offer to help.")|\cr |("This |
| 32 |
is the second line. I'm trying to")|\cr |("explain the best way for you to |
| 33 |
proceed.");|\cr |error;|\cr}}$$ A two-line help message would be given using |
| 34 |
|help2|, etc.; these informal helps should use simple vocabulary that complements |
| 35 |
the words used in the official error message that was printed. (Outside the |
| 36 |
U.S.A., the help messages should preferably be translated into the local |
| 37 |
vernacular. Each line of help is at most 60 characters long, in the present |
| 38 |
implementation, so that |max_print_line| will not be exceeded.) |
| 39 |
|
| 40 |
The |print_err| procedure supplies a `\.!' before the official message, and makes |
| 41 |
sure that the terminal is awake if a stop is going to occur. The |error| |
| 42 |
procedure supplies a `\..' after the official message, then it shows the location |
| 43 |
of the error; and if |interaction=error_stop_mode|, it also enters into a dialog |
| 44 |
with the user, during which time the help message may be printed. @^system |
| 45 |
dependencies@> |
| 46 |
|
| 47 |
*/ |
| 48 |
|
| 49 |
/*tex The current level of interaction: */ |
| 50 |
|
| 51 |
int interaction; |
| 52 |
|
| 53 |
/*tex Set from the command line: */ |
| 54 |
|
| 55 |
int interactionoption; |
| 56 |
|
| 57 |
char *last_error = NULL; |
| 58 |
char *last_lua_error = NULL; |
| 59 |
char *last_warning_tag = NULL; |
| 60 |
char *last_warning_str = NULL; |
| 61 |
char *last_error_context = NULL; |
| 62 |
|
| 63 |
int err_old_setting = 0 ; |
| 64 |
int in_error = 0 ; |
| 65 |
|
| 66 |
void set_last_error_context(void) |
| 67 |
{ |
| 68 |
str_number str; |
| 69 |
int sel = selector; |
| 70 |
int saved_new_line_char; |
| 71 |
int saved_new_string_line; |
| 72 |
selector = new_string; |
| 73 |
saved_new_line_char = new_line_char_par; |
| 74 |
saved_new_string_line = new_string_line; |
| 75 |
new_line_char_par = 10; |
| 76 |
new_string_line = 10; |
| 77 |
show_context(); |
| 78 |
xfree(last_error_context); |
| 79 |
str = make_string(); |
| 80 |
last_error_context = makecstring(str); |
| 81 |
flush_str(str); |
| 82 |
selector = sel; |
| 83 |
new_line_char_par = saved_new_line_char; |
| 84 |
new_string_line = saved_new_string_line; |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
void flush_err(void) |
| 89 |
{ |
| 90 |
str_number s_error; |
| 91 |
char *s = NULL; |
| 92 |
int callback_id ; |
| 93 |
if (in_error) { |
| 94 |
selector = err_old_setting; |
| 95 |
str_room(1); |
| 96 |
s_error = make_string(); |
| 97 |
s = makecstring(s_error); |
| 98 |
flush_str(s_error); |
| 99 |
if (interaction == error_stop_mode) { |
| 100 |
wake_up_terminal(); |
| 101 |
} |
| 102 |
xfree(last_error); |
| 103 |
last_error = (string) xmalloc((unsigned) (strlen(s) + 1)); |
| 104 |
strcpy(last_error,s); |
| 105 |
callback_id = callback_defined(show_error_message_callback); |
| 106 |
if (callback_id > 0) { |
| 107 |
run_callback(callback_id, "->"); |
| 108 |
} else { |
| 109 |
callback_id = callback_defined(show_ignored_error_message_callback); |
| 110 |
if (callback_id > 0) { |
| 111 |
run_callback(callback_id, "->"); |
| 112 |
} else { |
| 113 |
tprint(s); |
| 114 |
} |
| 115 |
} |
| 116 |
in_error = 0 ; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void print_err(const char *s) |
| 121 |
{ |
| 122 |
int callback_id = callback_defined(show_error_message_callback); |
| 123 |
if (interaction == error_stop_mode) { |
| 124 |
wake_up_terminal(); |
| 125 |
} |
| 126 |
if (callback_id > 0) { |
| 127 |
err_old_setting = selector; |
| 128 |
selector = new_string; |
| 129 |
in_error = 1 ; |
| 130 |
} |
| 131 |
if (filelineerrorstylep) { |
| 132 |
print_file_line(); |
| 133 |
} else { |
| 134 |
tprint_nl("! "); |
| 135 |
} |
| 136 |
tprint(s); |
| 137 |
if (callback_id <= 0) { |
| 138 |
xfree(last_error); |
| 139 |
last_error = (string) xmalloc((unsigned) (strlen(s) + 1)); |
| 140 |
strcpy(last_error,s); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void print_ignored_err(const char *s) |
| 145 |
{ |
| 146 |
int callback_id = callback_defined(show_ignored_error_message_callback); |
| 147 |
if (interaction == error_stop_mode) { |
| 148 |
wake_up_terminal(); |
| 149 |
} |
| 150 |
err_old_setting = selector; |
| 151 |
if (callback_id > 0) { |
| 152 |
selector = new_string; |
| 153 |
in_error = 1 ; |
| 154 |
} else { |
| 155 |
selector = log_only; |
| 156 |
} |
| 157 |
if (filelineerrorstylep) { |
| 158 |
print_file_line(); |
| 159 |
} else { |
| 160 |
tprint_nl(""); |
| 161 |
} |
| 162 |
tprint("ignored: "); |
| 163 |
tprint(s); |
| 164 |
if (callback_id <= 0) { |
| 165 |
xfree(last_error); |
| 166 |
last_error = (string) xmalloc((unsigned) (strlen(s) + 1)); |
| 167 |
strcpy(last_error,s); |
| 168 |
} |
| 169 |
selector = err_old_setting; |
| 170 |
} |
| 171 |
|
| 172 |
/*tex |
| 173 |
|
| 174 |
\TeX\ is careful not to call |error| when the print |selector| setting might be |
| 175 |
unusual. The only possible values of |selector| at the time of error messages are |
| 176 |
|
| 177 |
|no_print| (when |interaction=batch_mode| and |log_file| not yet open); |
| 178 |
|
| 179 |
|term_only| (when |interaction>batch_mode| and |log_file| not yet open); |
| 180 |
|
| 181 |
|log_only| (when |interaction=batch_mode| and |log_file| is open); |
| 182 |
|
| 183 |
|term_and_log| (when |interaction>batch_mode| and |log_file| is open). |
| 184 |
|
| 185 |
*/ |
| 186 |
|
| 187 |
void fixup_selector(boolean logopened) |
| 188 |
{ |
| 189 |
if (interaction == batch_mode) |
| 190 |
selector = no_print; |
| 191 |
else |
| 192 |
selector = term_only; |
| 193 |
if (logopened) |
| 194 |
selector = selector + 2; |
| 195 |
} |
| 196 |
|
| 197 |
/*tex |
| 198 |
|
| 199 |
A global variable |deletions_allowed| is set |false| if the |get_next| routine is |
| 200 |
active when |error| is called; this ensures that |get_next| and related routines |
| 201 |
like |get_token| will never be called recursively. A similar interlock is |
| 202 |
provided by |set_box_allowed|. @^recursion@> |
| 203 |
|
| 204 |
The global variable |history| records the worst level of error that has been |
| 205 |
detected. It has four possible values: |spotless|, |warning_issued|, |
| 206 |
|error_message_issued|, and |fatal_error_stop|. |
| 207 |
|
| 208 |
Another global variable, |error_count|, is increased by one when an |error| |
| 209 |
occurs without an interactive dialog, and it is reset to zero at the end of every |
| 210 |
paragraph. If |error_count| reaches 100, \TeX\ decides that there is no point in |
| 211 |
continuing further. |
| 212 |
|
| 213 |
*/ |
| 214 |
|
| 215 |
/*tex Is it safe for |error| to call |get_token|? */ |
| 216 |
|
| 217 |
boolean deletions_allowed; |
| 218 |
|
| 219 |
/*tex Is it safe to do a \.{\\setbox} assignment? */ |
| 220 |
|
| 221 |
boolean set_box_allowed; |
| 222 |
/*tex Has the source input been clean so far? */ |
| 223 |
|
| 224 |
int history; |
| 225 |
|
| 226 |
/*tex The number of scrolled errors since the last paragraph ended. */ |
| 227 |
|
| 228 |
int error_count; |
| 229 |
|
| 230 |
/*tex Should \TeX\ pause for instructions? */ |
| 231 |
|
| 232 |
int interrupt; |
| 233 |
|
| 234 |
/*tex Should interrupts be observed? */ |
| 235 |
|
| 236 |
boolean OK_to_interrupt; |
| 237 |
|
| 238 |
/*tex |
| 239 |
|
| 240 |
The value of |history| is initially |fatal_error_stop|, but it will be changed to |
| 241 |
|spotless| if \TeX\ survives the initialization process. |
| 242 |
|
| 243 |
*/ |
| 244 |
|
| 245 |
void initialize_errors(void) |
| 246 |
{ |
| 247 |
if (interactionoption == unspecified_mode) |
| 248 |
interaction = error_stop_mode; |
| 249 |
else |
| 250 |
interaction = interactionoption; |
| 251 |
deletions_allowed = true; |
| 252 |
set_box_allowed = true; |
| 253 |
OK_to_interrupt = true; |
| 254 |
} |
| 255 |
|
| 256 |
/*tex |
| 257 |
|
| 258 |
It is possible for |error| to be called recursively if some error arises when |
| 259 |
|get_token| is being used to delete a token, and/or if some fatal error occurs |
| 260 |
while \TeX\ is trying to fix a non-fatal one. But such recursion @^recursion@> is |
| 261 |
never more than two levels deep. |
| 262 |
|
| 263 |
Individual lines of help are recorded in the array |help_line|. |
| 264 |
|
| 265 |
*/ |
| 266 |
|
| 267 |
const char *help_line[7]; |
| 268 |
|
| 269 |
/*tex |
| 270 |
Should the |err_help| list be shown? |
| 271 |
*/ |
| 272 |
|
| 273 |
boolean use_err_help; |
| 274 |
|
| 275 |
/*tex |
| 276 |
|
| 277 |
The |jump_out| procedure just cuts across all active procedure levels and exits |
| 278 |
the program. It is used when there is no recovery from a particular error. The |
| 279 |
exit code can be overloaded. |
| 280 |
|
| 281 |
*/ |
| 282 |
|
| 283 |
int defaultexitcode = 0; |
| 284 |
|
| 285 |
__attribute__ ((noreturn)) |
| 286 |
void do_final_end(void) |
| 287 |
{ |
| 288 |
update_terminal(); |
| 289 |
ready_already = 0; |
| 290 |
lua_close(Luas); |
| 291 |
if ((history != spotless) && (history != warning_issued)) |
| 292 |
uexit(1); |
| 293 |
else |
| 294 |
uexit(defaultexitcode); |
| 295 |
} |
| 296 |
|
| 297 |
__attribute__ ((noreturn)) |
| 298 |
void jump_out(void) |
| 299 |
{ |
| 300 |
close_files_and_terminate(); |
| 301 |
do_final_end(); |
| 302 |
} |
| 303 |
|
| 304 |
/*tex |
| 305 |
|
| 306 |
Here is the function that calls the editor, if one is defined. This is loosely |
| 307 |
based on a similar function in kpathsea, but the calling convention is quite |
| 308 |
different. |
| 309 |
|
| 310 |
*/ |
| 311 |
|
| 312 |
static const_string edit_value = EDITOR; |
| 313 |
|
| 314 |
#if defined(WIN32) |
| 315 |
|
| 316 |
static int Isspace (char c) |
| 317 |
{ |
| 318 |
return (c == ' ' || c == '\t'); |
| 319 |
} |
| 320 |
|
| 321 |
#endif /* WIN32 */ |
| 322 |
|
| 323 |
__attribute__ ((noreturn)) |
| 324 |
static void luatex_calledit (int baseptr, int linenumber) |
| 325 |
{ |
| 326 |
char *temp, *command; |
| 327 |
char *fullcmd = NULL; /* avoid compiler warning */ |
| 328 |
char c; |
| 329 |
int sdone, ddone, i; |
| 330 |
char *filename = makecstring(input_stack[base_ptr].name_field); |
| 331 |
int fnlength = strlen(filename); |
| 332 |
#ifdef WIN32 |
| 333 |
char *fp, *ffp, *env, editorname[256], buffer[256]; |
| 334 |
int cnt = 0; |
| 335 |
int dontchange = 0; |
| 336 |
#endif |
| 337 |
sdone = ddone = 0; |
| 338 |
/*tex |
| 339 |
Close any open input files, since we're going to kill the job. |
| 340 |
*/ |
| 341 |
close_files_and_terminate(); |
| 342 |
/*tex |
| 343 |
Replace the default with the value of the appropriate environment |
| 344 |
variable or config file value, if it's set. |
| 345 |
*/ |
| 346 |
temp = kpse_var_value (edit_var); |
| 347 |
if (temp != NULL) |
| 348 |
edit_value = temp; |
| 349 |
/*tex |
| 350 |
Construct the command string. The `11' is the maximum length an |
| 351 |
integer might be. |
| 352 |
*/ |
| 353 |
command = xmalloc (strlen (edit_value) + fnlength + 11); |
| 354 |
/*tex |
| 355 |
So we can construct it as we go. |
| 356 |
*/ |
| 357 |
temp = command; |
| 358 |
#ifdef WIN32 |
| 359 |
fp = editorname; |
| 360 |
if ((isalpha(*edit_value) && *(edit_value + 1) == ':' && IS_DIR_SEP (*(edit_value + 2))) |
| 361 |
|| (*edit_value == '"' && isalpha(*(edit_value + 1)) |
| 362 |
&& *(edit_value + 2) == ':' && IS_DIR_SEP (*(edit_value + 3)))) { |
| 363 |
dontchange = 1; |
| 364 |
} |
| 365 |
#endif |
| 366 |
while ((c = *edit_value++) != 0) { |
| 367 |
if (c == '%') { |
| 368 |
switch (c = *edit_value++) { |
| 369 |
case 'd': |
| 370 |
if (ddone) |
| 371 |
FATAL1 ("call_edit: `%%d' appears twice in editor command: `%s'", edit_value); |
| 372 |
sprintf (temp, "%ld", (long int)linenumber); |
| 373 |
while (*temp != '\0') |
| 374 |
temp++; |
| 375 |
ddone = 1; |
| 376 |
break; |
| 377 |
case 's': |
| 378 |
if (sdone) |
| 379 |
FATAL1 ("call_edit: `%%s' appears twice in editor command: `%s'", edit_value); |
| 380 |
for (i =0; i < fnlength; i++) |
| 381 |
*temp++ = filename[i]; |
| 382 |
sdone = 1; |
| 383 |
break; |
| 384 |
case '\0': |
| 385 |
*temp++ = '%'; |
| 386 |
/*tex |
| 387 |
Back up to the null to force termination. |
| 388 |
*/ |
| 389 |
edit_value--; |
| 390 |
break; |
| 391 |
default: |
| 392 |
*temp++ = '%'; |
| 393 |
*temp++ = c; |
| 394 |
break; |
| 395 |
} |
| 396 |
} else { |
| 397 |
#ifdef WIN32 |
| 398 |
if (dontchange) { |
| 399 |
*temp++ = c; |
| 400 |
} else if(Isspace(c) && cnt == 0) { |
| 401 |
cnt++; |
| 402 |
temp = command; |
| 403 |
*temp++ = c; |
| 404 |
*fp = '\0'; |
| 405 |
} else if(!Isspace(c) && cnt == 0) { |
| 406 |
*fp++ = c; |
| 407 |
} else { |
| 408 |
*temp++ = c; |
| 409 |
} |
| 410 |
#else |
| 411 |
*temp++ = c; |
| 412 |
#endif |
| 413 |
} |
| 414 |
} |
| 415 |
*temp = 0; |
| 416 |
#ifdef WIN32 |
| 417 |
if (dontchange == 0) { |
| 418 |
if(editorname[0] == '.' || editorname[0] == '/' || editorname[0] == '\\') { |
| 419 |
fprintf(stderr, "%s is not allowed to execute.\n", editorname); |
| 420 |
do_final_end(); |
| 421 |
} |
| 422 |
env = (char *)getenv("PATH"); |
| 423 |
if(SearchPath(env, editorname, ".exe", 256, buffer, &ffp)==0) { |
| 424 |
if(SearchPath(env, editorname, ".bat", 256, buffer, &ffp)==0) { |
| 425 |
fprintf(stderr, "I cannot find %s in the PATH.\n", editorname); |
| 426 |
do_final_end(); |
| 427 |
} |
| 428 |
} |
| 429 |
fullcmd = (char *)xmalloc(strlen(buffer)+strlen(command)+5); |
| 430 |
strcpy(fullcmd, "\""); |
| 431 |
strcat(fullcmd, buffer); |
| 432 |
strcat(fullcmd, "\""); |
| 433 |
strcat(fullcmd, command); |
| 434 |
} |
| 435 |
#else |
| 436 |
fullcmd = command; |
| 437 |
#endif |
| 438 |
/*tex Execute the command. */ |
| 439 |
if (system (fullcmd) != 0) { |
| 440 |
fprintf (stderr, "! Trouble executing `%s'.\n", command); |
| 441 |
} |
| 442 |
/*tex Quit, since we found an error. */ |
| 443 |
do_final_end (); |
| 444 |
} |
| 445 |
|
| 446 |
/*tex |
| 447 |
|
| 448 |
This completes the job of error reporting. |
| 449 |
|
| 450 |
*/ |
| 451 |
|
| 452 |
void error(void) |
| 453 |
{ |
| 454 |
/*tex What the user types :*/ |
| 455 |
ASCII_code c; |
| 456 |
int callback_id; |
| 457 |
/*tex Used to save global variables when deleting tokens: */ |
| 458 |
int s1, s2, s3, s4; |
| 459 |
int i; |
| 460 |
flush_err(); |
| 461 |
if (history < error_message_issued) |
| 462 |
history = error_message_issued; |
| 463 |
callback_id = callback_defined(show_error_hook_callback); |
| 464 |
if (callback_id > 0) { |
| 465 |
set_last_error_context(); |
| 466 |
run_callback(callback_id, "->"); |
| 467 |
} else { |
| 468 |
print_char('.'); |
| 469 |
show_context(); |
| 470 |
} |
| 471 |
if (haltonerrorp) { |
| 472 |
/*tex If |close_files_and_terminate| generates an error, we'll end up back |
| 473 |
here; just give up in that case. If files are truncated, too bad. */ |
| 474 |
if (haltingonerrorp) { |
| 475 |
/*tex quit immediately */ |
| 476 |
do_final_end (); |
| 477 |
} |
| 478 |
haltingonerrorp=true; |
| 479 |
history = fatal_error_stop; |
| 480 |
jump_out(); |
| 481 |
} |
| 482 |
if (interaction == error_stop_mode) { |
| 483 |
/*tex Get user's advice and |return|. */ |
| 484 |
while (1) { |
| 485 |
CONTINUE: |
| 486 |
/*tex |
| 487 |
Original reports: |
| 488 |
|
| 489 |
https://tex.stackexchange.com/questions/551313/ |
| 490 |
https://tug.org/pipermail/tex-live/2020-June/045876.html |
| 491 |
|
| 492 |
This will probably be fixed by DEK in the 2021 tuneup in a different |
| 493 |
way (so we'll have to remove or alter this change), but the interaction |
| 494 |
sequence in the reports above causes a segmentation fault in web2c - |
| 495 |
writing to the closed \write15 stream because we wrongly decrement |
| 496 |
selector from 16 to 15 in term_input, due to the lack of this check in |
| 497 |
recursive error() call. |
| 498 |
*/ |
| 499 |
if (interaction !=error_stop_mode) |
| 500 |
return; |
| 501 |
clear_for_error_prompt(); |
| 502 |
prompt_input("? "); |
| 503 |
if (last == first) |
| 504 |
return; |
| 505 |
c = buffer[first]; |
| 506 |
if (c >= 'a') |
| 507 |
c = c + 'A' - 'a'; |
| 508 |
/*tex |
| 509 |
Interpret code |c| and |return| if done. It is desirable to |
| 510 |
provide an `\.E' option here that gives the user an easy way |
| 511 |
to return from \TeX\ to the system editor, with the offending |
| 512 |
line ready to be edited. But such an extension requires some |
| 513 |
system wizardry, so the present implementation simply types |
| 514 |
out the name of the file that should be edited and the |
| 515 |
relevant line number. |
| 516 |
*/ |
| 517 |
switch (c) { |
| 518 |
case '0': |
| 519 |
case '1': |
| 520 |
case '2': |
| 521 |
case '3': |
| 522 |
case '4': |
| 523 |
case '5': |
| 524 |
case '6': |
| 525 |
case '7': |
| 526 |
case '8': |
| 527 |
case '9': |
| 528 |
if (deletions_allowed) { |
| 529 |
/*tex |
| 530 |
Delete |c-"0"| tokens and |goto continue|. We allow |
| 531 |
deletion of up to 99 tokens at a time. |
| 532 |
*/ |
| 533 |
s1 = cur_tok; |
| 534 |
s2 = cur_cmd; |
| 535 |
s3 = cur_chr; |
| 536 |
s4 = align_state; |
| 537 |
align_state = 1000000; |
| 538 |
OK_to_interrupt = false; |
| 539 |
if ((last > first + 1) && (buffer[first + 1] >= '0') |
| 540 |
&& (buffer[first + 1] <= '9')) |
| 541 |
c = c * 10 + buffer[first + 1] - '0' * 11; |
| 542 |
else |
| 543 |
c = c - '0'; |
| 544 |
while (c > 0) { |
| 545 |
/*tex One-level recursive call of |error| is possible. */ |
| 546 |
get_token(); |
| 547 |
decr(c); |
| 548 |
} |
| 549 |
cur_tok = s1; |
| 550 |
cur_cmd = s2; |
| 551 |
cur_chr = s3; |
| 552 |
align_state = s4; |
| 553 |
OK_to_interrupt = true; |
| 554 |
help2( |
| 555 |
"I have just deleted some text, as you asked.", |
| 556 |
"You can now delete more, or insert, or whatever." |
| 557 |
); |
| 558 |
show_context(); |
| 559 |
goto CONTINUE; |
| 560 |
} |
| 561 |
break; |
| 562 |
case 'E': |
| 563 |
if (base_ptr > 0) { |
| 564 |
int callback_id = callback_defined(call_edit_callback); |
| 565 |
if (callback_id>0) { |
| 566 |
(void)run_callback(callback_id, "Sd->", makecstring(input_stack[base_ptr].name_field), line); |
| 567 |
/*tex This should not be reached. */ |
| 568 |
jump_out(); |
| 569 |
} else { |
| 570 |
tprint_nl("You want to edit file "); |
| 571 |
print(input_stack[base_ptr].name_field); |
| 572 |
tprint(" at line "); |
| 573 |
print_int(line); |
| 574 |
interaction = scroll_mode; |
| 575 |
if (kpse_init) { |
| 576 |
luatex_calledit(base_ptr, line); |
| 577 |
} else { |
| 578 |
/*tex This should not be reached. */ |
| 579 |
tprint_nl("There is no valid callback defined."); |
| 580 |
jump_out(); |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
break; |
| 585 |
case 'H': |
| 586 |
/*tex Print the help information and |goto continue| */ |
| 587 |
if (use_err_help) { |
| 588 |
give_err_help(); |
| 589 |
} else { |
| 590 |
if (help_line[0] == NULL) { |
| 591 |
help2( |
| 592 |
"Sorry, I don't know how to help in this situation.", |
| 593 |
"Maybe you should try asking a human?" |
| 594 |
); |
| 595 |
} |
| 596 |
i = 0; |
| 597 |
while (help_line[i] != NULL) |
| 598 |
tprint_nl(help_line[i++]); |
| 599 |
help4( |
| 600 |
"Sorry, I already gave what help I could...", |
| 601 |
"Maybe you should try asking a human?", |
| 602 |
"An error might have occurred before I noticed any problems.", |
| 603 |
"``If all else fails, read the instructions.''" |
| 604 |
); |
| 605 |
goto CONTINUE; |
| 606 |
} |
| 607 |
break; |
| 608 |
case 'I': |
| 609 |
/*tex |
| 610 |
|
| 611 |
Introduce new material from the terminal and |return|. When |
| 612 |
the following code is executed, |buffer[(first+1)..(last-1)]| |
| 613 |
may contain the material inserted by the user; otherwise |
| 614 |
another prompt will be given. In order to understand this |
| 615 |
part of the program fully, you need to be familiar with |
| 616 |
\TeX's input stacks. |
| 617 |
|
| 618 |
We enter a new syntactic level for terminal input: |
| 619 |
|
| 620 |
*/ |
| 621 |
begin_file_reading(); |
| 622 |
/*tex |
| 623 |
Now |state=mid_line|, so an initial blank space will count as |
| 624 |
a blank. |
| 625 |
*/ |
| 626 |
if (last > first + 1) { |
| 627 |
iloc = first + 1; |
| 628 |
buffer[first] = ' '; |
| 629 |
} else { |
| 630 |
prompt_input("insert>"); |
| 631 |
iloc = first; |
| 632 |
} |
| 633 |
first = last; |
| 634 |
/*tex No |end_line_char| ends this line. */ |
| 635 |
ilimit = last - 1; |
| 636 |
return; |
| 637 |
break; |
| 638 |
case 'Q': |
| 639 |
case 'R': |
| 640 |
case 'S': |
| 641 |
/*tex |
| 642 |
|
| 643 |
Change the interaction level and |return|. Here the author of |
| 644 |
\TeX\ apologizes for making use of the numerical relation |
| 645 |
between |"Q"|, |"R"|, |"S"|, and the desired interaction |
| 646 |
settings |batch_mode|, |nonstop_mode|, |scroll_mode|. |
| 647 |
|
| 648 |
*/ |
| 649 |
error_count = 0; |
| 650 |
interaction = batch_mode + c - 'Q'; |
| 651 |
tprint("OK, entering "); |
| 652 |
switch (c) { |
| 653 |
case 'Q': |
| 654 |
tprint_esc("batchmode"); |
| 655 |
decr(selector); |
| 656 |
break; |
| 657 |
case 'R': |
| 658 |
tprint_esc("nonstopmode"); |
| 659 |
break; |
| 660 |
case 'S': |
| 661 |
tprint_esc("scrollmode"); |
| 662 |
break; |
| 663 |
} |
| 664 |
tprint("..."); |
| 665 |
print_ln(); |
| 666 |
update_terminal(); |
| 667 |
return; |
| 668 |
break; |
| 669 |
case 'X': |
| 670 |
interaction = scroll_mode; |
| 671 |
jump_out(); |
| 672 |
break; |
| 673 |
default: |
| 674 |
break; |
| 675 |
} |
| 676 |
if (!use_err_help) { |
| 677 |
/* Print the menu of available options */ |
| 678 |
tprint("Type <return> to proceed, S to scroll future error messages,"); |
| 679 |
tprint_nl("R to run without stopping, Q to run quietly,"); |
| 680 |
tprint_nl("I to insert something, "); |
| 681 |
if (base_ptr > 0) |
| 682 |
tprint("E to edit your file,"); |
| 683 |
if (deletions_allowed) |
| 684 |
tprint_nl("1 or ... or 9 to ignore the next 1 to 9 tokens of input,"); |
| 685 |
tprint_nl("H for help, X to quit."); |
| 686 |
} |
| 687 |
use_err_help = false; |
| 688 |
} |
| 689 |
|
| 690 |
} |
| 691 |
incr(error_count); |
| 692 |
if (error_count == 100) { |
| 693 |
tprint_nl("(That makes 100 errors; please try again.)"); |
| 694 |
history = fatal_error_stop; |
| 695 |
jump_out(); |
| 696 |
} |
| 697 |
/*tex Put help message on the transcript file. */ |
| 698 |
if (interaction > batch_mode) { |
| 699 |
/*tex Avoid terminal output: */ |
| 700 |
decr(selector); |
| 701 |
} |
| 702 |
if (use_err_help) { |
| 703 |
print_ln(); |
| 704 |
give_err_help(); |
| 705 |
} else { |
| 706 |
int i1 = 0; |
| 707 |
while (help_line[i1] != NULL) |
| 708 |
tprint_nl(help_line[i1++]); |
| 709 |
} |
| 710 |
print_ln(); |
| 711 |
if (interaction > batch_mode) { |
| 712 |
/*tex Re-enable terminal output: */ |
| 713 |
incr(selector); |
| 714 |
} |
| 715 |
print_ln(); |
| 716 |
} |
| 717 |
|
| 718 |
/*tex |
| 719 |
|
| 720 |
A dozen or so error messages end with a parenthesized integer, so we save a teeny |
| 721 |
bit of program space by declaring the following procedure: |
| 722 |
|
| 723 |
*/ |
| 724 |
|
| 725 |
void int_error(int n) |
| 726 |
{ |
| 727 |
tprint(" ("); |
| 728 |
print_int(n); |
| 729 |
print_char(')'); |
| 730 |
error(); |
| 731 |
} |
| 732 |
|
| 733 |
/*tex |
| 734 |
|
| 735 |
In anomalous cases, the print selector might be in an unknown state; the |
| 736 |
following subroutine is called to fix things just enough to keep running a bit |
| 737 |
longer. |
| 738 |
|
| 739 |
*/ |
| 740 |
|
| 741 |
void normalize_selector(void) |
| 742 |
{ |
| 743 |
if (log_opened_global) |
| 744 |
selector = term_and_log; |
| 745 |
else |
| 746 |
selector = term_only; |
| 747 |
if (job_name == 0) |
| 748 |
open_log_file(); |
| 749 |
if (interaction == batch_mode) |
| 750 |
decr(selector); |
| 751 |
} |
| 752 |
|
| 753 |
/*tex |
| 754 |
|
| 755 |
The following procedure prints \TeX's last words before dying. |
| 756 |
|
| 757 |
*/ |
| 758 |
|
| 759 |
void succumb(void) |
| 760 |
{ |
| 761 |
if (interaction == error_stop_mode) { |
| 762 |
/*tex No more interaction: */ |
| 763 |
interaction = scroll_mode; |
| 764 |
} |
| 765 |
if (log_opened_global) { |
| 766 |
error(); |
| 767 |
} |
| 768 |
history = fatal_error_stop; |
| 769 |
/*tex Irrecoverable error: */ |
| 770 |
jump_out(); |
| 771 |
} |
| 772 |
|
| 773 |
/*tex |
| 774 |
|
| 775 |
This prints |s|, and that's it. |
| 776 |
|
| 777 |
*/ |
| 778 |
|
| 779 |
void fatal_error(const char *s) |
| 780 |
{ |
| 781 |
normalize_selector(); |
| 782 |
print_err("Emergency stop"); |
| 783 |
help1(s); |
| 784 |
succumb(); |
| 785 |
} |
| 786 |
|
| 787 |
/*tex |
| 788 |
|
| 789 |
Here is the most dreaded error message. We stop due to finiteness. |
| 790 |
|
| 791 |
*/ |
| 792 |
|
| 793 |
void overflow(const char *s, unsigned int n) |
| 794 |
{ |
| 795 |
normalize_selector(); |
| 796 |
print_err("TeX capacity exceeded, sorry ["); |
| 797 |
tprint(s); |
| 798 |
print_char('='); |
| 799 |
print_int((int) n); |
| 800 |
print_char(']'); |
| 801 |
if (varmem == NULL) { |
| 802 |
print_err("Sorry, I ran out of memory."); |
| 803 |
print_ln(); |
| 804 |
exit(EXIT_FAILURE); |
| 805 |
} |
| 806 |
help2( |
| 807 |
"If you really absolutely need more capacity,", |
| 808 |
"you can ask a wizard to enlarge me." |
| 809 |
); |
| 810 |
succumb(); |
| 811 |
} |
| 812 |
|
| 813 |
/*tex |
| 814 |
|
| 815 |
The program might sometime run completely amok, at which point there is no choice |
| 816 |
but to stop. If no previous error has been detected, that's bad news; a message |
| 817 |
is printed that is really intended for the \TeX\ maintenance person instead of |
| 818 |
the user (unless the user has been particularly diabolical). The index entries |
| 819 |
for `this can't happen' may help to pinpoint the problem. @^dry rot@> |
| 820 |
|
| 821 |
*/ |
| 822 |
|
| 823 |
void confusion(const char *s) |
| 824 |
{ /* consistency check violated; |s| tells where */ |
| 825 |
normalize_selector(); |
| 826 |
if (history < error_message_issued) { |
| 827 |
print_err("This can't happen ("); |
| 828 |
tprint(s); |
| 829 |
print_char(')'); |
| 830 |
help1( |
| 831 |
"I'm broken. Please show this to someone who can fix" |
| 832 |
); |
| 833 |
} else { |
| 834 |
print_err("I can't go on meeting you like this"); |
| 835 |
help2( |
| 836 |
"One of your faux pas seems to have wounded me deeply...", |
| 837 |
"in fact, I'm barely conscious. Please fix it and try again." |
| 838 |
); |
| 839 |
} |
| 840 |
succumb(); |
| 841 |
} |
| 842 |
|
| 843 |
/*tex |
| 844 |
|
| 845 |
Users occasionally want to interrupt \TeX\ while it's running. If the runtime |
| 846 |
system allows this, one can implement a routine that sets the global variable |
| 847 |
|interrupt| to some nonzero value when such an interrupt is signalled. Otherwise |
| 848 |
there is probably at least a way to make |interrupt| nonzero using the debugger. |
| 849 |
@^system dependencies@> @^debugging@> |
| 850 |
|
| 851 |
*/ |
| 852 |
|
| 853 |
void check_interrupt(void) |
| 854 |
{ |
| 855 |
if (interrupt != 0) |
| 856 |
pause_for_instructions(); |
| 857 |
} |
| 858 |
|
| 859 |
/*tex |
| 860 |
|
| 861 |
When an interrupt has been detected, the program goes into its highest |
| 862 |
interaction level and lets the user have nearly the full flexibility of the |
| 863 |
|error| routine. \TeX\ checks for interrupts only at times when it is safe to do |
| 864 |
this. |
| 865 |
|
| 866 |
*/ |
| 867 |
|
| 868 |
void pause_for_instructions(void) |
| 869 |
{ |
| 870 |
if (OK_to_interrupt) { |
| 871 |
interaction = error_stop_mode; |
| 872 |
if ((selector == log_only) || (selector == no_print)) |
| 873 |
incr(selector); |
| 874 |
print_err("Interruption"); |
| 875 |
help3( |
| 876 |
"You rang?", |
| 877 |
"Try to insert some instructions for me (e.g.,`I\\showlists'),", |
| 878 |
"unless you just want to quit by typing `X'." |
| 879 |
); |
| 880 |
deletions_allowed = false; |
| 881 |
error(); |
| 882 |
deletions_allowed = true; |
| 883 |
interrupt = 0; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
void tex_error(const char *msg, const char **hlp) |
| 888 |
{ |
| 889 |
print_err(msg); |
| 890 |
if (hlp != NULL) { |
| 891 |
int i; |
| 892 |
for (i = 0; (hlp[i] != NULL && i <= 5); i++) { |
| 893 |
help_line[i] = hlp[i]; |
| 894 |
} |
| 895 |
help_line[i] = NULL; |
| 896 |
} else { |
| 897 |
help_line[0] = NULL; |
| 898 |
} |
| 899 |
error(); |
| 900 |
} |
| 901 |
|
| 902 |
/*tex |
| 903 |
|
| 904 |
The |back_error| routine is used when we want to replace an offending token just |
| 905 |
before issuing an error message. This routine, like |back_input|, requires that |
| 906 |
|cur_tok| has been set. We disable interrupts during the call of |back_input| so |
| 907 |
that the help message won't be lost. |
| 908 |
|
| 909 |
*/ |
| 910 |
|
| 911 |
void back_error(void) |
| 912 |
{ |
| 913 |
OK_to_interrupt = false; |
| 914 |
back_input(); |
| 915 |
OK_to_interrupt = true; |
| 916 |
error(); |
| 917 |
} |
| 918 |
|
| 919 |
/*tex |
| 920 |
|
| 921 |
Back up one inserted token and call |error|. |
| 922 |
*/ |
| 923 |
|
| 924 |
void ins_error(void) |
| 925 |
{ |
| 926 |
OK_to_interrupt = false; |
| 927 |
back_input(); |
| 928 |
token_type = inserted; |
| 929 |
OK_to_interrupt = true; |
| 930 |
error(); |
| 931 |
} |
| 932 |
|
| 933 |
/*tex |
| 934 |
|
| 935 |
When \TeX\ wants to typeset a character that doesn't exist, the character node is |
| 936 |
not created; thus the output routine can assume that characters exist when it |
| 937 |
sees them. The following procedure prints a warning message unless the user has |
| 938 |
suppressed it. |
| 939 |
If |tracing_lost_chars_par| (i.e. \.{\\tracinglostchar}) is greater than 2, |
| 940 |
it's considered as an error. |
| 941 |
|
| 942 |
=0 : nothing |
| 943 |
=1 : warning only to log file |
| 944 |
=2 : warning + force terminal |
| 945 |
=3 : error |
| 946 |
=4 : warning (+ force terminal) : per request latex team |
| 947 |
>4 : error : per request latex team |
| 948 |
|
| 949 |
*/ |
| 950 |
|
| 951 |
void char_warning(internal_font_number f, int c) |
| 952 |
{ |
| 953 |
if (tracing_lost_chars_par > 0) { |
| 954 |
/*tex saved value of |tracing_online| */ |
| 955 |
int old_setting = tracing_online_par; |
| 956 |
/* index to current digit; we assume that $0\L n<16^{22}$ */ |
| 957 |
int k; |
| 958 |
if (tracing_lost_chars_par > 1) |
| 959 |
tracing_online_par = 1; |
| 960 |
begin_diagnostic(); |
| 961 |
tprint_nl("Missing character: There is no "); |
| 962 |
print(c); |
| 963 |
tprint(" (U+"); |
| 964 |
k = 0; |
| 965 |
if (c < 16) |
| 966 |
print_char('0'); |
| 967 |
if (c < 256) |
| 968 |
print_char('0'); |
| 969 |
if (c < 4096) |
| 970 |
print_char('0'); |
| 971 |
do { |
| 972 |
dig[k] = c % 16; |
| 973 |
c = c / 16; |
| 974 |
incr(k); |
| 975 |
} while (c != 0); |
| 976 |
print_the_digs((eight_bits) k); |
| 977 |
tprint(") in font "); |
| 978 |
print_font_name(f); |
| 979 |
print_char('!'); |
| 980 |
end_diagnostic(false); |
| 981 |
tracing_online_par = old_setting; |
| 982 |
} |
| 983 |
if (tracing_lost_chars_par == 3) { |
| 984 |
error(); |
| 985 |
} else if (tracing_lost_chars_par > 4) { |
| 986 |
error(); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
void wrapup_backend(void) { |
| 991 |
ensure_output_state(static_pdf, ST_OMODE_FIX); |
| 992 |
if (output_mode_used == OMODE_NONE) { |
| 993 |
print_err(" ==> Fatal error occurred, no FMT file produced!"); |
| 994 |
} else { |
| 995 |
backend_out_control_err[BACKEND_INDEX(backend_control_finish_file)](static_pdf,history == fatal_error_stop); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
void normal_error(const char *t, const char *p) |
| 1000 |
{ |
| 1001 |
normalize_selector(); |
| 1002 |
if (interaction == error_stop_mode) { |
| 1003 |
wake_up_terminal(); |
| 1004 |
} |
| 1005 |
if (filelineerrorstylep) { |
| 1006 |
print_file_line(); |
| 1007 |
} else { |
| 1008 |
tprint_nl("! "); |
| 1009 |
} |
| 1010 |
tprint("error: "); |
| 1011 |
if (cur_file_name) { |
| 1012 |
tprint(" (file "); |
| 1013 |
tprint(cur_file_name); |
| 1014 |
tprint(")"); |
| 1015 |
} |
| 1016 |
if (t != NULL) { |
| 1017 |
tprint(" ("); |
| 1018 |
tprint(t); |
| 1019 |
tprint(")"); |
| 1020 |
} |
| 1021 |
tprint(": "); |
| 1022 |
if (p != NULL) |
| 1023 |
tprint(p); |
| 1024 |
history = fatal_error_stop; |
| 1025 |
wrapup_backend(); |
| 1026 |
exit(EXIT_FAILURE); |
| 1027 |
} |
| 1028 |
|
| 1029 |
void normal_warning(const char *t, const char *p) |
| 1030 |
{ |
| 1031 |
int report_id ; |
| 1032 |
if (strcmp(t,"lua") == 0) { |
| 1033 |
int saved_new_line_char; |
| 1034 |
saved_new_line_char = new_line_char_par; |
| 1035 |
new_line_char_par = 10; |
| 1036 |
report_id = callback_defined(show_lua_error_hook_callback); |
| 1037 |
if (report_id == 0) { |
| 1038 |
if (p != NULL) |
| 1039 |
tprint(p); |
| 1040 |
help2( |
| 1041 |
"The lua interpreter ran into a problem, so the", |
| 1042 |
"remainder of this lua chunk will be ignored." |
| 1043 |
); |
| 1044 |
} else { |
| 1045 |
(void) run_callback(report_id, "->"); |
| 1046 |
} |
| 1047 |
error(); |
| 1048 |
new_line_char_par = saved_new_line_char; |
| 1049 |
} else { |
| 1050 |
report_id = callback_defined(show_warning_message_callback); |
| 1051 |
if (report_id > 0) { |
| 1052 |
/*tex Free the last ones, */ |
| 1053 |
xfree(last_warning_str); |
| 1054 |
xfree(last_warning_tag); |
| 1055 |
last_warning_str = (string) xmalloc(strlen(p) + 1); |
| 1056 |
last_warning_tag = (string) xmalloc(strlen(t) + 1); |
| 1057 |
strcpy(last_warning_str,p); |
| 1058 |
strcpy(last_warning_tag,t); |
| 1059 |
run_callback(report_id, "->"); |
| 1060 |
} else { |
| 1061 |
print_ln(); |
| 1062 |
tprint("warning "); |
| 1063 |
if (cur_file_name) { |
| 1064 |
tprint(" (file "); |
| 1065 |
tprint(cur_file_name); |
| 1066 |
tprint(")"); |
| 1067 |
} |
| 1068 |
if (t != NULL) { |
| 1069 |
tprint(" ("); |
| 1070 |
tprint(t); |
| 1071 |
tprint(")"); |
| 1072 |
} |
| 1073 |
tprint(": "); |
| 1074 |
if (p != NULL) |
| 1075 |
tprint(p); |
| 1076 |
print_ln(); |
| 1077 |
} |
| 1078 |
if (history == spotless) |
| 1079 |
history = warning_issued; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
static char print_buf[PRINTF_BUF_SIZE]; |
| 1084 |
|
| 1085 |
__attribute__ ((format(printf, 2,3))) |
| 1086 |
void formatted_error(const char *t, const char *fmt, ...) |
| 1087 |
{ |
| 1088 |
va_list args; |
| 1089 |
va_start(args, fmt); |
| 1090 |
vsnprintf(print_buf, PRINTF_BUF_SIZE, fmt, args); |
| 1091 |
normal_error(t,print_buf); |
| 1092 |
va_end(args); |
| 1093 |
} |
| 1094 |
|
| 1095 |
__attribute__ ((format(printf, 2,3))) |
| 1096 |
void formatted_warning(const char *t, const char *fmt, ...) |
| 1097 |
{ |
| 1098 |
va_list args; |
| 1099 |
va_start(args, fmt); |
| 1100 |
vsnprintf(print_buf, PRINTF_BUF_SIZE, fmt, args); |
| 1101 |
normal_warning(t,print_buf); |
| 1102 |
va_end(args); |
| 1103 |
} |