3
Copyright 2009 Taco Hoekwater <taco@luatex.org>
5
This file is part of LuaTeX.
7
LuaTeX is free software; you can redistribute it and/or modify it under
8
the terms of the GNU General Public License as published by the Free
9
Software Foundation; either version 2 of the License, or (at your
10
option) any later version.
12
LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU General Public License along
18
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
23
static const char _svn_version[] =
24
"$Id: printing.c 3261 2009-12-18 11:38:21Z taco $"
25
"$URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.50.0/source/texk/web2c/luatexdir/tex/printing.c $";
27
#define font_id_text(A) cs_text(font_id_base+(A))
29
#define wlog(A) fputc(A,log_file)
30
#define wterm(A) fputc(A,term_out)
32
extern char *ptexbanner;
35
Messages that are sent to a user's terminal and to the transcript-log file
36
are produced by several `|print|' procedures. These procedures will
37
direct their output to a variety of places, based on the setting of
38
the global variable |selector|, which has the following possible
42
\hang |term_and_log|, the normal setting, prints on the terminal and on the
45
\hang |log_only|, prints only on the transcript file.
47
\hang |term_only|, prints only on the terminal.
49
\hang |no_print|, doesn't print at all. This is used only in rare cases
50
before the transcript file is open.
52
\hang |pseudo|, puts output into a cyclic buffer that is used
53
by the |show_context| routine; when we get to that routine we shall discuss
54
the reasoning behind this curious mode.
56
\hang |new_string|, appends the output to the current string in the
59
\hang 0 to 15, prints on one of the sixteen files for \.{\\write} output.
62
\noindent The symbolic names `|term_and_log|', etc., have been assigned
63
numeric codes that satisfy the convenient relations |no_print+1=term_only|,
64
|no_print+2=log_only|, |term_only+2=log_only+1=term_and_log|.
66
Three additional global variables, |tally| and |term_offset| and
67
|file_offset|, record the number of characters that have been printed
68
since they were most recently cleared to zero. We use |tally| to record
69
the length of (possibly very long) stretches of printing; |term_offset|
70
and |file_offset|, on the other hand, keep track of how many characters
71
have appeared so far on the current line that has been output to the
72
terminal or to the transcript file, respectively.
75
alpha_file log_file; /* transcript of \TeX\ session */
76
int selector = term_only; /* where to print a message */
77
int dig[23]; /* digits in a number being output */
78
int tally = 0; /* the number of characters recently printed */
79
int term_offset = 0; /* the number of characters on the current terminal line */
80
int file_offset = 0; /* the number of characters on the current file line */
81
packed_ASCII_code trick_buf[(ssup_error_line + 1)]; /* circular buffer for pseudoprinting */
82
int trick_count; /* threshold for pseudoprinting, explained later */
83
int first_count; /* another variable for pseudoprinting */
84
boolean inhibit_par_tokens = false; /* for minor adjustments to |show_token_list| */
86
/* To end a line of text output, we call |print_ln| */
89
{ /* prints an end-of-line */
110
fprintf(write_file[selector], "\n");
113
} /* |tally| is not affected */
116
The |print_char| procedure sends one byte to the desired destination.
117
All printing comes through |print_ln| or |print_char|.
120
#define wterm_char(A) do { \
121
if ((A>=0x20)||(A==0x0A)||(A==0x0D)||(A==0x09)) { \
124
if (term_offset+2>=max_print_line) { \
125
wterm_cr(); term_offset=0; \
127
incr(term_offset); wterm('^'); \
128
incr(term_offset); wterm('^'); \
133
#define needs_wrapping(A,B) \
134
(((A>=0xC0)&&(A<=0xDF)&&(B+2>=max_print_line))|| \
135
((A>=0xE0)&&(A<=0xEF)&&(B+3>=max_print_line))|| \
136
((A>=0xF0)&&(B+4>=max_print_line)))
138
#define fix_term_offset(A) do { \
139
if (needs_wrapping(A,term_offset)){ \
140
wterm_cr(); term_offset=0; \
144
#define fix_log_offset(A) do { \
145
if (needs_wrapping(A,file_offset)){ \
146
wlog_cr(); file_offset=0; \
150
void print_char(int s)
151
{ /* prints a single byte */
152
assert(s >= 0 && s < 256);
153
if (s == int_par(new_line_char_code)) {
154
if (selector < pseudo) {
167
if (term_offset == max_print_line) {
171
if (file_offset == max_print_line) {
180
if (file_offset == max_print_line) {
189
if (term_offset == max_print_line) {
197
if (tally < trick_count)
198
trick_buf[tally % error_line] = s;
202
break; /* we drop characters if the string space is full */
204
fprintf(write_file[selector], "%c", s);
210
An entire string is output by calling |print|. Note that if we are outputting
211
the single standard ASCII character \.c, we could call |print("c")|, since
212
|"c"=99| is the number of a single-character string, as explained above. But
213
|print_char("c")| is quicker, so \TeX\ goes directly to the |print_char|
214
routine when it knows that this is safe. (The present implementation
215
assumes that it is always safe to print a visible ASCII character.)
216
@^system dependencies@>
218
The first 256 entries above the 17th unicode plane are used for a
219
special trick: when \TeX\ has to print items in that range, it will
220
instead print the character that results from substracting 0x110000
221
from that value. This allows byte-oriented output to things like
222
\.{\\specials} and \.{\\pdfliterals}. Todo: Perhaps it would be useful
223
to do the same substraction while typesetting.
227
{ /* prints string |s| */
228
unsigned char *j, *l; /* current character code position */
231
/* this can't happen */
236
} else if (s < STRING_OFFSET) {
243
/* TH not sure about this, disabled for now! */
244
if ((false) && (selector > pseudo)) {
246
return; /* internal strings are not expanded */
248
if (s == int_par(new_line_char_code)) {
249
if (selector < pseudo) {
256
} else if (s <= 0x7FF) {
257
print_char(0xC0 + (s / 0x40));
258
print_char(0x80 + (s % 0x40));
259
} else if (s <= 0xFFFF) {
260
print_char(0xE0 + (s / 0x1000));
261
print_char(0x80 + ((s % 0x1000) / 0x40));
262
print_char(0x80 + ((s % 0x1000) % 0x40));
263
} else if (s >= 0x110000) {
264
int c = s - 0x110000;
266
pdf_warning("print", "bad raw byte to print (c=",
269
tprint("), skipped.");
275
print_char(0xF0 + (s / 0x40000));
276
print_char(0x80 + ((s % 0x40000) / 0x1000));
277
print_char(0x80 + (((s % 0x40000) % 0x1000) / 0x40));
278
print_char(0x80 + (((s % 0x40000) % 0x1000) % 0x40));
283
if (selector == new_string) {
284
append_string(str_string(s), str_length(s));
288
l = j + str_length(s);
290
/* 0x110000 in utf=8: 0xF4 0x90 0x80 0x80 */
291
/* I don't bother checking the last two bytes explicitly */
292
if ((j < l - 4) && (*j == 0xF4) && (*(j + 1) == 0x90)) {
293
int c = (*(j + 2) - 128) * 64 + (*(j + 3) - 128);
294
assert(c >= 0 && c < 256);
305
The procedure |print_nl| is like |print|, but it makes sure that the
306
string appears at the beginning of a new line.
310
{ /* move to beginning of a line */
311
if (((term_offset > 0) && (odd(selector))) ||
312
((file_offset > 0) && (selector >= log_only)))
316
void print_nl(str_number s)
317
{ /* prints string |s| at beginning of line */
322
/* char * versions */
326
unsigned char *ss = (unsigned char *) s;
327
if (selector == new_string) {
328
append_string(ss, strlen(s));
335
void tprint_nl(char *s)
341
/* |slow_print| is the same as |print| nowadays, but the name is kept for now. */
343
void slow_print(int s)
344
{ /* prints string |s| */
349
Here is the very first thing that \TeX\ prints: a headline that identifies
350
the version number and format package. The |term_offset| variable is temporarily
351
incorrect, but the discrepancy is not serious since we assume that the banner
352
and format identifier together will occupy at most |max_print_line|
356
void print_banner(char *v, int e)
360
callback_id = callback_defined(start_run_callback);
361
if (callback_id == 0) {
362
fprintf(term_out, "This is LuaTeX, Version %s-%d", v, e);
363
if (format_ident > 0)
364
slow_print(format_ident);
369
fprintf(term_out, "restricted ");
370
fprintf(term_out, "\\write18 enabled.\n");
372
} else if (callback_id > 0) {
373
res = run_callback(callback_id, "->");
377
void log_banner(char *v, int e)
379
char *months[] = { " ",
380
"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
381
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
383
unsigned month = (unsigned) int_par(month_code);
386
fprintf(log_file, "This is LuaTeX, Version %s-%d", v, e);
387
slow_print(format_ident);
390
print_int(int_par(day_code));
392
fprintf(log_file, months[month]);
394
print_int(int_par(year_code));
396
print_two(int_par(time_code) / 60);
398
print_two(int_par(time_code) % 60);
403
fprintf(log_file, "restricted ");
404
fprintf(log_file, "\\write18 enabled.");
406
if (filelineerrorstylep) {
408
fprintf(log_file, " file:line:error style messages enabled.");
410
if (parsefirstlinep) {
412
fprintf(log_file, " %%&-line parsing enabled.");
416
void print_version_banner(void)
418
fprintf(term_out, ptexbanner); /* todo: get the extra info in here */
419
/* write_svnversion(luatex_svnversion); */
423
The procedure |print_esc| prints a string that is preceded by
424
the user's escape character (which is usually a backslash).
427
void print_esc(str_number s)
428
{ /* prints escape character, then |s| */
429
int c; /* the escape character code */
430
/* Set variable |c| to the current escape character */
431
c = int_par(escape_char_code);
432
if (c >= 0 && c < STRING_OFFSET)
437
void tprint_esc(char *s)
438
{ /* prints escape character, then |s| */
439
int c; /* the escape character code */
440
/* Set variable |c| to the current escape character */
441
c = int_par(escape_char_code);
442
if (c >= 0 && c < STRING_OFFSET)
447
/* An array of digits in the range |0..15| is printed by |print_the_digs|.*/
449
void print_the_digs(eight_bits k)
451
/* prints |dig[k-1]|$\,\ldots\,$|dig[0]| */
454
print_char('0' + dig[k]);
456
print_char('A' - 10 + dig[k]);
461
The following procedure, which prints out the decimal representation of a
462
given integer |n|, has been written carefully so that it works properly
463
if |n=0| or if |(-n)| would cause overflow. It does not apply |mod| or |div|
464
to negative arguments, since such operations are not implemented consistently
465
by all \PASCAL\ compilers.
468
void print_int(longinteger n)
469
{ /* prints an integer in decimal form */
470
int k; /* index to current digit; we assume that $|n|<10^{23}$ */
471
longinteger m; /* used to negate |n| in possibly dangerous cases */
475
if (n > -100000000) {
499
Here is a trivial procedure to print two digits; it is usually called with
500
a parameter in the range |0<=n<=99|.
503
void print_two(int n)
504
{ /* prints two least significant digits */
506
print_char('0' + (n / 10));
507
print_char('0' + (n % 10));
511
Hexadecimal printing of nonnegative integers is accomplished by |print_hex|.
514
void print_hex(int n)
515
{ /* prints a positive integer in hexadecimal form */
516
int k; /* index to current digit; we assume that $0\L n<16^{22}$ */
528
Roman numerals are produced by the |print_roman_int| routine. Readers
529
who like puzzles might enjoy trying to figure out how this tricky code
530
works; therefore no explanation will be given. Notice that 1990 yields
531
\.{mcmxc}, not \.{mxm}.
534
void print_roman_int(int n)
536
char *j, *k; /* mysterious indices */
537
nonnegative_integer u, v; /* mysterious numbers */
538
char mystery[] = "m2d5c2l5x2v5i";
539
j = (char *) mystery;
542
while (n >= (int) v) {
547
return; /* nonpositive input produces no output */
549
u = v / (*(k - 1) - '0');
550
if (*(k - 1) == '2') {
552
u = u / (*(k - 1) - '0');
559
v = v / (*(j - 1) - '0');
565
The |print| subroutine will not print a string that is still being
566
created. The following procedure will.
569
void print_current_string(void)
570
{ /* prints a yet-unmade string */
571
unsigned j = 0; /* points to current character code */
572
while (j < cur_length)
573
print_char(cur_string[j++]);
577
The procedure |print_cs| prints the name of a control sequence, given
578
a pointer to its address in |eqtb|. A space is printed after the name
579
unless it is a single nonletter or an active character. This procedure
580
might be invoked with invalid data, so it is ``extra robust.'' The
581
individual characters must be printed one at a time using |print|, since
582
they may be unprintable.
586
{ /* prints a purported control sequence */
587
str_number t = cs_text(p);
588
if (p < hash_base) { /* nullcs */
590
tprint_esc("csname");
591
tprint_esc("endcsname");
593
tprint_esc("IMPOSSIBLE.");
595
} else if ((p >= undefined_control_sequence) &&
596
((p <= eqtb_size) || p > eqtb_size + hash_extra)) {
597
tprint_esc("IMPOSSIBLE.");
598
} else if (t >= str_ptr) {
599
tprint_esc("NONEXISTENT.");
601
if (is_active_cs(t)) {
602
print(active_cs_value(t));
605
if (single_letter(t)) {
606
if (get_cat_code(int_par(cat_code_table_code),
607
pool_to_unichar(str_string(t))) == letter_cmd)
617
Here is a similar procedure; it avoids the error checks, and it never
618
prints a space after the control sequence.
621
void sprint_cs(pointer p)
622
{ /* prints a control sequence */
625
tprint_esc("csname");
626
tprint_esc("endcsname");
630
print(active_cs_value(t));
637
/* This procedure is never called when |interaction<scroll_mode|. */
639
void prompt_input(char *s)
647
Then there is a subroutine that prints glue stretch and shrink, possibly
648
followed by the name of finite units:
651
void print_glue(scaled d, int order, char *s)
652
{ /* prints a glue component */
654
if ((order < normal) || (order > filll)) {
656
} else if (order > normal) {
658
while (order > sfi) {
662
} else if (s != NULL) {
667
/* The next subroutine prints a whole glue specification */
669
void print_spec(int p, char *s)
670
{ /* prints a glue specification */
674
print_scaled(width(p));
677
if (stretch(p) != 0) {
679
print_glue(stretch(p), stretch_order(p), s);
681
if (shrink(p) != 0) {
683
print_glue(shrink(p), shrink_order(p), s);
689
We can reinforce our knowledge of the data structures just introduced
690
by considering two procedures that display a list in symbolic form.
691
The first of these, called |short_display|, is used in ``overfull box''
692
messages to give the top-level description of a list. The other one,
693
called |show_node_list|, prints a detailed description of exactly what
694
is in the data structure.
696
The philosophy of |short_display| is to ignore the fine points about exactly
697
what is inside boxes, except that ligatures and discretionary breaks are
698
expanded. As a result, |short_display| is a recursive procedure, but the
699
recursion is never more than one level deep.
702
A global variable |font_in_short_display| keeps track of the font code that
703
is assumed to be present when |short_display| begins; deviations from this
704
font will be printed.
707
int font_in_short_display; /* an internal font number */
710
Boxes, rules, inserts, whatsits, marks, and things in general that are
711
sort of ``complicated'' are indicated only by printing `\.{[]}'.
714
void print_font_identifier(internal_font_number f)
717
if (pdf_font_blink(f) == null_font)
718
fonttext = font_id_text(f);
720
fonttext = font_id_text(pdf_font_blink(f));
725
if (pdf_font_blink(f) == null_font)
728
print_int(pdf_font_blink(f));
730
if (int_par(pdf_tracing_fonts_code) > 0) {
733
if (font_size(f) != font_dsize(f)) {
735
print_scaled(font_size(f));
739
} else if (pdf_font_expand_ratio(f) != 0) {
741
if (pdf_font_expand_ratio(f) > 0)
743
print_int(pdf_font_expand_ratio(f));
748
void short_display(int p)
749
{ /* prints highlights of list |p| */
751
if (is_char_node(p)) {
752
if (lig_ptr(p) != null) {
753
short_display(lig_ptr(p));
755
if (font(p) != font_in_short_display) {
756
if (!is_valid_font(font(p)))
759
print_font_identifier(font(p));
761
font_in_short_display = font(p);
766
/* Print a short indication of the contents of node |p| */
767
print_short_node_contents(p);
774
The |show_node_list| routine requires some auxiliary subroutines: one to
775
print a font-and-character combination, one to print a token list without
776
its reference count, and one to print a rule dimension.
779
void print_font_and_char(int p)
780
{ /* prints |char_node| data */
781
if (!is_valid_font(font(p)))
784
print_font_identifier(font(p));
789
void print_mark(int p)
790
{ /* prints token list data in braces */
792
if ((p < fix_mem_min) || (p > fix_mem_end))
793
tprint_esc("CLOBBERED.");
795
show_token_list(token_link(p), null, max_print_line - 10);
799
void print_rule_dimen(scaled d)
800
{ /* prints dimension in rule node */
808
Since boxes can be inside of boxes, |show_node_list| is inherently recursive,
810
up to a given maximum number of levels. The history of nesting is indicated
811
by the current string, which will be printed at the beginning of each line;
812
the length of this string, namely |cur_length|, is the depth of nesting.
814
@ A global variable called |depth_threshold| is used to record the maximum
815
depth of nesting for which |show_node_list| will show information. If we
816
have |depth_threshold=0|, for example, only the top level information will
817
be given and no sublists will be traversed. Another global variable, called
818
|breadth_max|, tells the maximum number of items to show at each level;
819
|breadth_max| had better be positive, or you won't see anything.
822
int depth_threshold; /* maximum nesting depth in box displays */
823
int breadth_max; /* maximum number of items shown at the same list level */
826
/* The recursive machinery is started by calling |show_box|. */
828
void show_box(halfword p)
830
/* Assign the values |depth_threshold:=show_box_depth| and
831
|breadth_max:=show_box_breadth| */
832
depth_threshold = int_par(show_box_depth_code);
833
breadth_max = int_par(show_box_breadth_code);
835
if (breadth_max <= 0)
837
show_node_list(p); /* the show starts at |p| */
842
/* Helper for debugging purposes */
844
void short_display_n(int p, int m)
845
{ /* prints highlights of list |p| */
847
font_in_short_display = null_font;
851
if (is_char_node(p)) {
852
if (p <= max_halfword) {
853
if (font(p) != font_in_short_display) {
854
if (!is_valid_font(font(p)))
857
print_font_identifier(font(p));
859
font_in_short_display = font(p);
864
if ((type(p) == glue_node) ||
865
(type(p) == disc_node) ||
866
(type(p) == penalty_node) ||
867
((type(p) == kern_node) && (subtype(p) == explicit)))
871
if (type(p) == disc_node) {
873
short_display(vlink(pre_break(p)));
875
short_display(vlink(post_break(p)));
878
/* Print a short indication of the contents of node |p| */
879
print_short_node_contents(p);
890
When debugging a macro package, it can be useful to see the exact
891
control sequence names in the format file. For example, if ten new
892
csnames appear, it's nice to know what they are, to help pinpoint where
893
they came from. (This isn't a truly ``basic'' printing procedure, but
894
that's a convenient module in which to put it.)
897
void print_csnames(int hstart, int hfinish)
900
unsigned char *c, *l;
901
fprintf(stderr, "fmtdebug:csnames from %d to %d:", (int) hstart,
903
for (h = hstart; h <= hfinish; h++) {
904
if (cs_text(h) > 0) { /* if have anything at this position */
905
c = str_string(cs_text(h));
906
l = c + str_length(cs_text(h));
908
fputc(*c++, stderr); /* print the characters */
910
fprintf(stderr, "|");
916
A helper for printing file:line:error style messages. Look for a
917
filename in |full_source_filename_stack|, and if we fail to find
918
one fall back on the non-file:line:error style.
921
void print_file_line(void)
925
while ((level > 0) && (full_source_filename_stack[level] == 0))
931
tprint(full_source_filename_stack[level]);
933
if (level == in_open)
936
print_int(line_stack[iindex + 1 - (in_open - level)]);
942
\TeX\ is occasionally supposed to print diagnostic information that
943
goes only into the transcript file, unless |tracing_online| is positive.
944
Here are two routines that adjust the destination of print commands:
947
void begin_diagnostic(void)
948
{ /* prepare to do some tracing */
949
global_old_setting = selector;
950
if ((int_par(tracing_online_code) <= 0) && (selector == term_and_log)) {
952
if (history == spotless)
953
history = warning_issued;
958
void end_diagnostic(boolean blank_line)
959
{ /* restore proper conditions after tracing */
963
selector = global_old_setting;
967
Of course we had better declare another global variable, if the previous
968
routines are going to work.
971
int global_old_setting;