3 % Copyright
2009-2013 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
/>.
24 #include
"lua/luatex-api.h" /* for luatex_banner
*/
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
)
33 @ Messages that are sent to a user's terminal and to the transcript-log file
34 are produced by several `|print|' procedures. These procedures will
35 direct their output to a variety of places
, based on the setting of
36 the global variable |selector|
, which has the following possible
40 \hang |term_and_log|
, the normal setting
, prints on the terminal and on the
43 \hang |log_only|
, prints only on the transcript file.
45 \hang |term_only|
, prints only on the terminal.
47 \hang |no_print|
, doesn't print at all. This is used only in rare cases
48 before the transcript file is open.
50 \hang |pseudo|
, puts output into a cyclic buffer that is used
51 by the |show_context| routine
; when we get to that routine we shall discuss
52 the reasoning behind this curious mode.
54 \hang |new_string|
, appends the output to the current string in the
57 \hang
0 to
15, prints on one of the sixteen files for \.
{\\write
} output.
60 \noindent The symbolic names `|term_and_log|'
, etc.
, have been assigned
61 numeric codes that satisfy the convenient relations |no_print
+1=term_only|
,
62 |no_print
+2=log_only|
, |term_only
+2=log_only
+1=term_and_log|.
64 Three additional global variables
, |tally| and |term_offset| and
65 |file_offset|
, record the number of characters that have been printed
66 since they were most recently cleared to zero. We use |tally| to record
67 the length of
(possibly very long
) stretches of printing
; |term_offset|
68 and |file_offset|
, on the other hand
, keep track of how many characters
69 have appeared so far on the current line that has been output to the
70 terminal or to the transcript file
, respectively.
73 alpha_file log_file
; /* transcript of \TeX\ session
*/
74 int selector
= term_only
; /* where to print a message
*/
75 int dig
[23]; /* digits in a number being output
*/
76 int tally
= 0; /* the number of characters recently printed
*/
77 int term_offset
= 0; /* the number of characters on the current terminal line
*/
78 int file_offset
= 0; /* the number of characters on the current file line
*/
79 packed_ASCII_code trick_buf
[(ssup_error_line
+ 1)]; /* circular buffer for pseudoprinting
*/
80 int trick_count
; /* threshold for pseudoprinting
, explained later
*/
81 int first_count
; /* another variable for pseudoprinting
*/
82 boolean inhibit_par_tokens
= false
; /* for minor adjustments to |show_token_list|
*/
84 @ To end a line of text output
, we call |print_ln|
87 { /* prints an end-of-line
*/
108 fprintf
(write_file
[selector
], "\n");
111 } /* |tally| is not affected
*/
114 @ The |print_char| procedure sends one byte to the desired destination.
115 All printing comes through |print_ln| or |print_char|
, except for the
116 case of |tprint|
(see below
).
119 #define wterm_char
(A
) do
{ \
120 if
((A
>=0x20)||
(A
==0x0A)||
(A
==0x0D)||
(A
==0x09)) { \
123 if
(term_offset
+2>=max_print_line
) { \
124 wterm_cr
(); term_offset
=0; \
126 incr
(term_offset
); wterm
('^'
); \
127 incr
(term_offset
); wterm
('^'
); \
132 #define needs_wrapping
(A
,B
) \
133 (((A
>=0xF0)&&(B+4>=max_print_line))|| \
134 ((A
>=0xE0)&&(B+3>=max_print_line))|| \
135 ((A
>=0xC0)&&(B+2>=max_print_line)))
137 #define fix_term_offset
(A
) do
{ \
138 if
(needs_wrapping
(A
,term_offset
)){ \
139 wterm_cr
(); term_offset
=0; \
143 #define fix_log_offset
(A
) do
{ \
144 if
(needs_wrapping
(A
,file_offset
)){ \
145 wlog_cr
(); file_offset
=0; \
149 void print_char
(int s
)
150 { /* prints a single byte
*/
151 assert
(s
>= 0 && s < 256);
152 if
(s
== int_par
(new_line_char_code
)) {
153 if
(selector
< pseudo
) {
166 if
(term_offset
== max_print_line
) {
170 if
(file_offset
== max_print_line
) {
179 if
(file_offset
== max_print_line
) {
188 if
(term_offset
== max_print_line
) {
196 if
(tally
< trick_count
)
197 trick_buf
[tally
% error_line
] = (packed_ASCII_code
) s
;
201 break
; /* we drop characters if the string space is full
*/
203 fprintf
(write_file
[selector
], "%c", s
);
209 @ An entire string is output by calling |print|. Note that if we are outputting
210 the single standard ASCII character \.c
, we could call |print
("c")|
, since
211 |
"c"=99| is the number of a single-character string
, as explained above. But
212 |print_char
("c")| is quicker
, so \TeX\ goes directly to the |print_char|
213 routine when it knows that this is safe.
(The present implementation
214 assumes that it is always safe to print a visible ASCII character.
)
215 @^system dependencies@
>
217 The first
256 entries above the
17th unicode plane are used for a
218 special trick
: when \TeX\ has to print items in that range
, it will
219 instead print the character that results from substracting
0x110000
220 from that value. This allows byte-oriented output to things like
221 \.
{\\specials
} and \.
{\\pdfliterals
}. Todo
: Perhaps it would be useful
222 to do the same substraction while typesetting.
226 { /* prints string |s|
*/
228 /* this can't happen
*/
233 } else if
(s
< STRING_OFFSET
) {
240 /* TH not sure about this
, disabled for now
! */
241 if
((false
) && (selector > pseudo)) {
243 return
; /* internal strings are not expanded
*/
245 if
(s
== int_par
(new_line_char_code
)) {
246 if
(selector
< pseudo
) {
253 } else if
(s
<= 0x7FF) {
254 print_char
(0xC0 + (s
/ 0x40));
255 print_char
(0x80 + (s
% 0x40));
256 } else if
(s
<= 0xFFFF) {
257 print_char
(0xE0 + (s
/ 0x1000));
258 print_char
(0x80 + ((s
% 0x1000) / 0x40));
259 print_char
(0x80 + ((s
% 0x1000) % 0x40));
260 } else if
(s
>= 0x110000) {
261 int c
= s
- 0x110000;
263 pdf_warning
("print", "bad raw byte to print (c=",
266 tprint
("), skipped.");
272 print_char
(0xF0 + (s
/ 0x40000));
273 print_char
(0x80 + ((s
% 0x40000) / 0x1000));
274 print_char
(0x80 + (((s
% 0x40000) % 0x1000) / 0x40));
275 print_char
(0x80 + (((s
% 0x40000) % 0x1000) % 0x40));
280 if
(selector
== new_string
) {
281 append_string
(str_string
(s
), (unsigned
) str_length
(s
));
284 lprint
(&str_lstring(s));
287 void lprint
(lstring
*ss
) {
288 unsigned char
*j
, *l
; /* current character code position
*/
292 /* 0x110000 in utf
=8: 0xF4 0x90 0x80 0x80 */
293 /* I don't bother checking the last two bytes explicitly
*/
294 if
((j
< l
- 4) && (*j == 0xF4) && (*(j + 1) == 0x90)) {
295 int c
= (*(j
+ 2) - 128) * 64 + (*(j
+ 3) - 128);
296 assert
(c
>= 0 && c < 256);
306 @ The procedure |print_nl| is like |print|
, but it makes sure that the
307 string appears at the beginning of a new line.
311 { /* move to beginning of a line
*/
312 if
(((term_offset
> 0) && (odd(selector))) ||
313 ((file_offset
> 0) && (selector >= log_only)))
317 void print_nl
(str_number s
)
318 { /* prints string |s| at beginning of line
*/
323 @ |char
*| versions of the same procedures. |tprint| is
324 different because it uses buffering
, which works well because
325 most of the output actually comes through |tprint|.
328 void tprint
(const char
*sss
)
331 int i
= 0; /* buffer index
*/
332 int newlinechar
= int_par
(new_line_char_code
);
337 append_string
((const unsigned char
*)sss
, (unsigned
) strlen
(sss
));
342 if
(tally
< trick_count
) {
343 trick_buf
[tally
% error_line
] = (packed_ASCII_code
) *sss
++;
366 char
*newstr
= xstrdup
(sss
);
368 for
(s
=newstr
;*s
;s
++) {
369 if
(*s
== newlinechar
) {
373 fputs
(newstr
, write_file
[selector
]);
379 /* what is left is the
3 term
/log settings
*/
380 buffer
= xmalloc
(strlen
(sss
)*3);
382 const unsigned char
*ss
= (const unsigned char
*) sss
;
385 if
(needs_wrapping
(s
,file_offset
) || s
== newlinechar
) {
388 fputs
(buffer
, log_file
);
389 i
= 0; buffer
[0] = '\
0'
;
392 if
(s
!= newlinechar
) {
394 if
(file_offset
++ == max_print_line
) {
397 fputs
(buffer
, log_file
);
398 i
= 0; buffer
[0] = '\
0'
;
405 fputs
(buffer
, log_file
);
411 const unsigned char
*ss
= (const unsigned char
*) sss
;
414 if
(needs_wrapping
(s
,term_offset
) || s
== newlinechar
) {
417 fputs
(buffer
, term_out
);
418 i
= 0; buffer
[0] = '\
0'
;
421 if
(s
!= newlinechar
) {
422 if
((s
>=0x20)||
(s
==0x0A)||
(s
==0x0D)||
(s
==0x09)) {
430 if
(++term_offset
== max_print_line
) {
433 fputs
(buffer
, term_out
);
434 i
= 0; buffer
[0] = '\
0'
;
441 fputs
(buffer
, term_out
);
447 void tprint_nl
(const char
*s
)
453 @ Here is the very first thing that \TeX\ prints
: a headline that identifies
454 the version number and format package. The |term_offset| variable is temporarily
455 incorrect
, but the discrepancy is not serious since we assume that the banner
456 and format identifier together will occupy at most |max_print_line|
460 void print_banner
(const char
*v
, int ver
)
463 callback_id
= callback_defined
(start_run_callback
);
464 if
(callback_id
== 0) {
466 fprintf
(term_out
, "This is " MyName
", Version %s ", v
);
468 fprintf
(term_out
, "This is " MyName
", Version %s%s (rev %d) ", v
,
470 if
(format_ident
> 0)
473 if
(show_luahashchars
){
475 fprintf
(term_out
,"Number of bits used by the hash function (" my_name
"): %d",LUAI_HASHLIMIT
);
481 fprintf
(term_out
, "restricted ");
482 fprintf
(term_out
, "\\write18 enabled.\n");
484 } else if
(callback_id
> 0) {
485 run_callback
(callback_id
, "->");
490 void log_banner
(const char
*v
, int ver
)
492 const char
*months
[] = { " ",
493 "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
494 "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
496 unsigned month
= (unsigned
) int_par
(month_code
);
500 fprintf
(log_file
, "This is " MyName
", Version %s ", v
);
502 fprintf
(log_file
, "This is " MyName
", Version %s%s (rev %d) ", v
,
507 print_int
(int_par
(day_code
));
509 fprintf
(log_file
, "%s", months
[month
]);
511 print_int
(int_par
(year_code
));
513 print_two
(int_par
(time_code
) / 60);
515 print_two
(int_par
(time_code
) % 60);
520 fprintf
(log_file
, "restricted ");
521 fprintf
(log_file
, "\\write18 enabled.");
523 if
(filelineerrorstylep
) {
525 fprintf
(log_file
, " file:line:error style messages enabled.");
527 if
(parsefirstlinep
) {
529 fprintf
(log_file
, " %%&-line parsing enabled.");
534 void print_version_banner
(void
)
536 fprintf
(term_out
, "%s", luatex_banner
);
539 @ The procedure |print_esc| prints a string that is preceded by
540 the user's escape character
(which is usually a backslash
).
543 void print_esc
(str_number s
)
544 { /* prints escape character
, then |s|
*/
545 int c
; /* the escape character code
*/
546 /* Set variable |c| to the current escape character
*/
547 c
= int_par
(escape_char_code
);
548 if
(c
>= 0 && c < STRING_OFFSET)
554 void tprint_esc
(const char
*s
)
555 { /* prints escape character
, then |s|
*/
556 int c
; /* the escape character code
*/
557 /* Set variable |c| to the current escape character
*/
558 c
= int_par
(escape_char_code
);
559 if
(c
>= 0 && c < STRING_OFFSET)
564 @ An array of digits in the range |
0.
.15| is printed by |print_the_digs|.
567 void print_the_digs
(eight_bits k
)
569 /* prints |dig
[k-1
]|$\
,\ldots\
,$|dig
[0]|
*/
572 print_char
('
0'
+ dig
[k
]);
574 print_char
('A'
- 10 + dig
[k
]);
578 @ The following procedure
, which prints out the decimal representation of a
579 given integer |n|
, has been written carefully so that it works properly
580 if |n
=0| or if |
(-n
)| would cause overflow. It does not apply |mod| or |div|
581 to negative arguments
, since such operations are not implemented consistently
582 by all
PASCAL compilers.
585 void print_int
(longinteger n
)
586 { /* prints an integer in decimal form
*/
587 int k
; /* index to current digit
; we assume that $|n|
<10^
{23}$
*/
588 longinteger m
; /* used to negate |n| in possibly dangerous cases
*/
592 if
(n
> -100000000) {
608 dig
[k
] = (int
) (n
% 10);
612 print_the_digs
((eight_bits
) k
);
616 @ Here is a trivial procedure to print two digits
; it is usually called with
617 a parameter in the range |
0<=n
<=99|.
620 void print_two
(int n
)
621 { /* prints two least significant digits
*/
623 print_char
('
0'
+ (n
/ 10));
624 print_char
('
0'
+ (n
% 10));
628 @ Hexadecimal printing of nonnegative integers is accomplished by |print_hex|.
631 void print_hex
(int n
)
632 { /* prints a positive integer in hexadecimal form
*/
633 int k
; /* index to current digit
; we assume that $
0\L n
<16^
{22}$
*/
641 print_the_digs((eight_bits) k);
645 @ Roman numerals are produced by the |print_roman_int| routine. Readers
646 who like puzzles might enjoy trying to figure out how this tricky code
647 works; therefore no explanation will be given. Notice that 1990 yields
648 \.{mcmxc}, not \.{mxm}.
651 void print_roman_int(int n)
653 char *j, *k; /* mysterious indices */
654 int u, v; /* mysterious numbers */
655 char mystery[] = "m2d5c2l5x2v5i
";
656 j = (char *) mystery;
664 return; /* nonpositive input produces no output */
666 u = v / (*(k - 1) - '0');
667 if (*(k - 1) == '2') {
669 u = u / (*(k - 1) - '0');
676 v = v / (*(j - 1) - '0');
682 @ The |print| subroutine will not print a string that is still being
683 created. The following procedure will.
686 void print_current_string(void)
687 { /* prints a yet-unmade string */
688 unsigned j = 0; /* points to current character code */
689 while (j < cur_length)
690 print_char(cur_string[j++]);
693 @ The procedure |print_cs| prints the name of a control sequence, given
694 a pointer to its address in |eqtb|. A space is printed after the name
695 unless it is a single nonletter or an active character. This procedure
696 might be invoked with invalid data, so it is ``extra robust.'' The
697 individual characters must be printed one at a time using |print|, since
698 they may be unprintable.
702 { /* prints a purported control sequence */
703 str_number t = cs_text(p);
704 if (p < hash_base) { /* nullcs */
706 tprint_esc("csname
");
707 tprint_esc("endcsname
");
710 tprint_esc("IMPOSSIBLE.
");
712 } else if ((p >= undefined_control_sequence) &&
713 ((p <= eqtb_size) || p > eqtb_size + hash_extra)) {
714 tprint_esc("IMPOSSIBLE.
");
715 } else if (t >= str_ptr) {
716 tprint_esc("NONEXISTENT.
");
718 if (is_active_cs(t)) {
719 print(active_cs_value(t));
722 if (single_letter(t)) {
723 if (get_cat_code(int_par(cat_code_table_code),
724 pool_to_unichar(str_string(t))) == letter_cmd)
734 @ Here is a similar procedure; it avoids the error checks, and it never
735 prints a space after the control sequence.
738 void sprint_cs(pointer p)
739 { /* prints a control sequence */
742 tprint_esc("csname
");
743 tprint_esc("endcsname
");
747 print(active_cs_value(t));
754 @ This procedure is never called when |interaction<scroll_mode|.
756 void prompt_input(const char *s)
764 @ Then there is a subroutine that prints glue stretch and shrink, possibly
765 followed by the name of finite units:
768 void print_glue(scaled d, int order, const char *s)
769 { /* prints a glue component */
771 if ((order < normal) || (order > filll)) {
773 } else if (order > normal) {
775 while (order > sfi) {
779 } else if (s != NULL) {
784 @ The next subroutine prints a whole glue specification
786 void print_spec(int p, const char *s)
787 { /* prints a glue specification */
791 print_scaled(width(p));
794 if (stretch(p) != 0) {
796 print_glue(stretch(p), stretch_order(p), s);
798 if (shrink(p) != 0) {
800 print_glue(shrink(p), shrink_order(p), s);
806 @ We can reinforce our knowledge of the data structures just introduced
807 by considering two procedures that display a list in symbolic form.
808 The first of these, called |short_display|, is used in ``overfull box''
809 messages to give the top-level description of a list. The other one,
810 called |show_node_list|, prints a detailed description of exactly what
811 is in the data structure.
813 The philosophy of |short_display| is to ignore the fine points about exactly
814 what is inside boxes, except that ligatures and discretionary breaks are
815 expanded. As a result, |short_display| is a recursive procedure, but the
816 recursion is never more than one level deep.
819 A global variable |font_in_short_display| keeps track of the font code that
820 is assumed to be present when |short_display| begins; deviations from this
821 font will be printed.
824 int font_in_short_display; /* an internal font number */
827 @ Boxes, rules, inserts, whatsits, marks, and things in general that are
828 sort of ``complicated'' are indicated only by printing `\.{[]}'.
831 void print_font_identifier(internal_font_number f)
834 fonttext = font_id_text(f);
841 if (int_par(pdf_tracing_fonts_code) > 0) {
844 if (font_size(f) != font_dsize(f)) {
846 print_scaled(font_size(f));
854 void short_display(int p)
855 { /* prints highlights of list |p| */
857 if (is_char_node(p)) {
858 if (lig_ptr(p) != null) {
859 short_display(lig_ptr(p));
861 if (font(p) != font_in_short_display) {
862 if (!is_valid_font(font(p)))
865 print_font_identifier(font(p));
867 font_in_short_display = font(p);
872 /* Print a short indication of the contents of node |p| */
873 print_short_node_contents(p);
880 @ The |show_node_list| routine requires some auxiliary subroutines: one to
881 print a font-and-character combination, one to print a token list without
882 its reference count, and one to print a rule dimension.
885 void print_font_and_char(int p)
886 { /* prints |char_node| data */
887 if (!is_valid_font(font(p)))
890 print_font_identifier(font(p));
896 void print_mark(int p)
897 { /* prints token list data in braces */
899 if ((p < (int) fix_mem_min) || (p > (int) fix_mem_end))
900 tprint_esc("CLOBBERED.
");
902 show_token_list(token_link(p), null, max_print_line - 10);
907 void print_rule_dimen(scaled d)
908 { /* prints dimension in rule node */
916 @ Since boxes can be inside of boxes, |show_node_list| is inherently recursive,
918 up to a given maximum number of levels. The history of nesting is indicated
919 by the current string, which will be printed at the beginning of each line;
920 the length of this string, namely |cur_length|, is the depth of nesting.
922 A global variable called |depth_threshold| is used to record the maximum
923 depth of nesting for which |show_node_list| will show information. If we
924 have |depth_threshold=0|, for example, only the top level information will
925 be given and no sublists will be traversed. Another global variable, called
926 |breadth_max|, tells the maximum number of items to show at each level;
927 |breadth_max| had better be positive, or you won't see anything.
930 int depth_threshold; /* maximum nesting depth in box displays */
931 int breadth_max; /* maximum number of items shown at the same list level */
934 @ The recursive machinery is started by calling |show_box|.
937 void show_box(halfword p)
939 /* Assign the values |depth_threshold:=show_box_depth| and
940 |breadth_max:=show_box_breadth| */
941 depth_threshold = int_par(show_box_depth_code);
942 breadth_max = int_par(show_box_breadth_code);
944 if (breadth_max <= 0)
946 show_node_list(p); /* the show starts at |p| */
951 @ Helper for debugging purposes
954 void short_display_n(int p, int m)
955 { /* prints highlights of list |p| */
957 font_in_short_display = null_font;
961 if (is_char_node(p)) {
962 if (p <= max_halfword) {
963 if (font(p) != font_in_short_display) {
964 if (!is_valid_font(font(p)))
967 print_font_identifier(font(p));
969 font_in_short_display = font(p);
974 if ((type(p) == glue_node) ||
975 (type(p) == disc_node) ||
976 (type(p) == penalty_node) ||
977 ((type(p) == kern_node) && (subtype(p) == explicit)))
981 if (type(p) == disc_node) {
983 short_display(vlink(pre_break(p)));
985 short_display(vlink(post_break(p)));
988 /* Print a short indication of the contents of node |p| */
989 print_short_node_contents(p);
1000 @ When debugging a macro package, it can be useful to see the exact
1001 control sequence names in the format file. For example, if ten new
1002 csnames appear, it's nice to know what they are, to help pinpoint where
1003 they came from. (This isn't a truly ``basic'' printing procedure, but
1004 that's a convenient module in which to put it.)
1007 void print_csnames(int hstart, int hfinish)
1010 unsigned char *c, *l;
1011 fprintf(stderr, "fmtdebug
:csnames from
%d to
%d
:", (int) hstart,
1013 for (h = hstart; h <= hfinish; h++) {
1014 if (cs_text(h) > 0) { /* if have anything at this position */
1015 c = str_string(cs_text(h));
1016 l = c + str_length(cs_text(h));
1018 fputc(*c++, stderr); /* print the characters */
1020 fprintf(stderr, "|
");
1026 @ A helper for printing file:line:error style messages. Look for a
1027 filename in |full_source_filename_stack|, and if we fail to find
1028 one fall back on the non-file:line:error style.
1031 void print_file_line(void)
1035 while ((level > 0) && (full_source_filename_stack[level] == 0))
1041 tprint(full_source_filename_stack[level]);
1043 if (level == in_open)
1046 print_int(line_stack[level + 1]);
1052 @ \TeX\ is occasionally supposed to print diagnostic information that
1053 goes only into the transcript file, unless |tracing_online| is positive.
1054 Here are two routines that adjust the destination of print commands:
1057 void begin_diagnostic(void)
1058 { /* prepare to do some tracing */
1059 global_old_setting = selector;
1060 if ((int_par(tracing_online_code) <= 0) && (selector == term_and_log)) {
1062 if (history == spotless)
1063 history = warning_issued;
1068 void end_diagnostic(boolean blank_line)
1069 { /* restore proper conditions after tracing */
1073 selector = global_old_setting;
1077 @ Of course we had better declare another global variable, if the previous
1078 routines are going to work.
1081 int global_old_setting;