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
/>.
22 #include
"lua/luatex-api.h" /* for luatex_banner
*/
25 #define wlog
(A
) fputc
(A
,log_file
)
26 #define wterm
(A
) fputc
(A
,term_out
)
28 int new_string_line
= 0;
29 int escape_controls
= 1;
31 @ Messages that are sent to a user's terminal and to the transcript-log file
32 are produced by several `|print|' procedures. These procedures will
33 direct their output to a variety of places
, based on the setting of
34 the global variable |selector|
, which has the following possible
38 \hang |term_and_log|
, the normal setting
, prints on the terminal and on the
41 \hang |log_only|
, prints only on the transcript file.
43 \hang |term_only|
, prints only on the terminal.
45 \hang |no_print|
, doesn't print at all. This is used only in rare cases
46 before the transcript file is open.
48 \hang |pseudo|
, puts output into a cyclic buffer that is used
49 by the |show_context| routine
; when we get to that routine we shall discuss
50 the reasoning behind this curious mode.
52 \hang |new_string|
, appends the output to the current string in the
55 \hang
0 to
15, prints on one of the sixteen files for \.
{\\write
} output.
58 \noindent The symbolic names `|term_and_log|'
, etc.
, have been assigned
59 numeric codes that satisfy the convenient relations |no_print
+1=term_only|
,
60 |no_print
+2=log_only|
, |term_only
+2=log_only
+1=term_and_log|.
62 Three additional global variables
, |tally| and |term_offset| and
63 |file_offset|
, record the number of characters that have been printed
64 since they were most recently cleared to zero. We use |tally| to record
65 the length of
(possibly very long
) stretches of printing
; |term_offset|
66 and |file_offset|
, on the other hand
, keep track of how many characters
67 have appeared so far on the current line that has been output to the
68 terminal or to the transcript file
, respectively.
71 alpha_file log_file
; /* transcript of \TeX\ session
*/
72 int selector
= term_only
; /* where to print a message
*/
73 int dig
[23]; /* digits in a number being output
*/
74 int tally
= 0; /* the number of characters recently printed
*/
75 int term_offset
= 0; /* the number of characters on the current terminal line
*/
76 int file_offset
= 0; /* the number of characters on the current file line
*/
77 packed_ASCII_code trick_buf
[(ssup_error_line
+ 1)]; /* circular buffer for pseudoprinting
*/
78 int trick_count
; /* threshold for pseudoprinting
, explained later
*/
79 int first_count
; /* another variable for pseudoprinting
*/
80 boolean inhibit_par_tokens
= false
; /* for minor adjustments to |show_token_list|
*/
82 @ To end a line of text output
, we call |print_ln|
107 if
(new_string_line
> 0)
108 print_char
(new_string_line
);
111 fprintf
(write_file
[selector
], "\n");
114 /* |tally| is not affected
*/
117 @ The |print_char| procedure sends one byte to the desired destination.
118 All printing comes through |print_ln| or |print_char|
, except for the
119 case of |tprint|
(see below
).
121 The checking of the line length is an inheritance from previosu engines
122 and we might drop it after release
1.0. We're not too picky about the exact
123 match of that length because we have utf output so length is then a bit
127 #define needs_escaping
(A
) \
128 ((! escape_controls
) ||
(A
>=0x20) ||
(A
==0x0A) ||
(A
==0x0D) ||
(A
==0x09))
130 #define escaped_char
(A
) \
133 #define wterm_char
(A
) \
134 if
(needs_escaping
(A
)) { \
137 if
(term_offset
+2>=max_print_line
) { \
143 wterm
(escaped_char
(A
)); \
149 #define needs_wrapping
(A
,B
) \
150 (((A
>=0xF0)&&(B+4>=max_print_line)) || \
151 ((A
>=0xE0)&&(B+3>=max_print_line)) || \
152 ((A
>=0xC0)&&(B+2>=max_print_line)))
154 we have mostly ascii in logs
, so ...
158 #define needs_wrapping
(A
,B
) \
160 (((A
>=0xF0) && (B+4>=max_print_line)) || \
161 ((A
>=0xE0) && (B+3>=max_print_line)) || \
162 ( (B
+2>=max_print_line
))) \
165 #define fix_term_offset
(A
) \
166 if
(needs_wrapping
(A
,term_offset
)){ \
171 #define fix_log_offset
(A
) \
172 if
(needs_wrapping
(A
,file_offset
)){ \
177 void print_char
(int s
)
179 if
(s
< 0 || s
> 255) {
180 formatted_warning
("print","weird character %i",s
);
183 if
(s
== new_line_char_par
) {
184 if
(selector
< pseudo
) {
196 if
(term_offset
== max_print_line
) {
205 if
(file_offset
== max_print_line
) {
217 if
(term_offset
== max_print_line
) {
221 if
(file_offset
== max_print_line
) {
227 if
(tally
< trick_count
)
228 trick_buf
[tally
% error_line
] = (packed_ASCII_code
) s
;
234 fprintf
(write_file
[selector
], "%c", s
);
239 @ An entire string is output by calling |print|. Note that if we are outputting
240 the single standard ASCII character \.c
, we could call |print
("c")|
, since
241 |
"c"=99| is the number of a single-character string
, as explained above. But
242 |print_char
("c")| is quicker
, so \TeX\ goes directly to the |print_char|
243 routine when it knows that this is safe.
(The present implementation
244 assumes that it is always safe to print a visible ASCII character.
)
246 @^system dependencies@
>
248 The first
256 entries above the
17th unicode plane are used for a
249 special trick
: when \TeX\ has to print items in that range
, it will
250 instead print the character that results from substracting
0x110000
251 from that value. This allows byte-oriented output to things like
252 \.
{\\specials
} and \.
{\\pdfextension literals
}. Todo
: Perhaps it would be useful
253 to do the same substraction while typesetting.
259 normal_warning
("print","bad string pointer");
261 } else if
(s
< STRING_OFFSET
) {
263 normal_warning
("print","bad string offset");
265 /* TH not sure about this
, disabled for now
! */
266 if
((false
) && (selector > pseudo)) {
267 /* internal strings are not expanded
*/
271 if
(s
== new_line_char_par
) {
272 if
(selector
< pseudo
) {
279 } else if
(s
<= 0x7FF) {
280 print_char
(0xC0 + (s
/ 0x40));
281 print_char
(0x80 + (s
% 0x40));
282 } else if
(s
<= 0xFFFF) {
283 print_char
(0xE0 + (s
/ 0x1000));
284 print_char
(0x80 + ((s
% 0x1000) / 0x40));
285 print_char
(0x80 + ((s
% 0x1000) % 0x40));
286 } else if
(s
>= 0x110000) {
287 int c
= s
- 0x110000;
289 formatted_warning
("print", "bad raw byte to print (c=%d), skipped",c
);
294 print_char
(0xF0 + (s
/ 0x40000));
295 print_char
(0x80 + ((s
% 0x40000) / 0x1000));
296 print_char
(0x80 + (((s
% 0x40000) % 0x1000) / 0x40));
297 print_char
(0x80 + (((s
% 0x40000) % 0x1000) % 0x40));
302 if
(selector
== new_string
) {
303 append_string
(str_string
(s
), (unsigned
) str_length
(s
));
306 lprint
(&str_lstring(s));
309 void lprint
(lstring
*ss
) {
310 unsigned char
*j
, *l
; /* current character code position
*/
314 /* 0x110000 in utf
=8: 0xF4 0x90 0x80 0x80 */
315 /* I don't bother checking the last two bytes explicitly
*/
316 if
((j
< l
- 4) && (*j == 0xF4) && (*(j + 1) == 0x90)) {
317 int c
= (*(j
+ 2) - 128) * 64 + (*(j
+ 3) - 128);
318 assert
(c
>= 0 && c < 256);
328 @ The procedure |print_nl| is like |print|
, but it makes sure that the
329 string appears at the beginning of a new line.
333 { /* move to beginning of a line
*/
334 if
(new_string_line
> 0) {
335 print_char
(new_string_line
);
336 } else if
(((term_offset
> 0) && (odd(selector))) ||
337 ((file_offset
> 0) && (selector >= log_only))) {
342 void print_nl
(str_number s
)
343 { /* prints string |s| at beginning of line
*/
348 @ |char
*| versions of the same procedures. |tprint| is
349 different because it uses buffering
, which works well because
350 most of the output actually comes through |tprint|.
353 #define t_flush_buffer
(target
,offset
) \
354 buffer
[i
++] = '\n'
; \
356 fputs
(buffer
, target
); \
361 void tprint
(const char
*sss
)
364 int i
= 0; /* buffer index
*/
365 int newlinechar
= new_line_char_par
;
384 if
(tally
< trick_count
) {
385 trick_buf
[tally
% error_line
] = (packed_ASCII_code
) *sss
++;
394 append_string
((const unsigned char
*)sss
, (unsigned
) strlen
(sss
));
399 char
*newstr
= xstrdup
(sss
);
401 for
(s
=newstr
;*s
;s
++) {
402 if
(*s
== newlinechar
) {
406 fputs
(newstr
, write_file
[selector
]);
412 /* what is left is the
3 term
/log settings
*/
413 if
(dolog || doterm
) {
414 buffer
= xmalloc
(strlen
(sss
)*3);
416 const unsigned char
*ss
= (const unsigned char
*) sss
;
419 if
(needs_wrapping
(s
,file_offset
) || s
== newlinechar
) {
420 t_flush_buffer
(log_file
,file_offset
);
422 if
(s
!= newlinechar
) {
424 if
(file_offset
++ == max_print_line
) {
425 t_flush_buffer
(log_file
,file_offset
);
431 fputs
(buffer
, log_file
);
437 const unsigned char
*ss
= (const unsigned char
*) sss
;
440 if
(needs_wrapping
(s
,term_offset
) || s
== newlinechar
) {
441 t_flush_buffer
(term_out
,term_offset
);
443 if
(s
!= newlinechar
) {
444 if
(needs_escaping
(s
)) {
449 buffer
[i
++] = escaped_char
(s
);
452 if
(++term_offset
== max_print_line
) {
453 t_flush_buffer
(term_out
,term_offset
);
459 fputs
(buffer
, term_out
);
466 void tprint_nl
(const char
*s
)
472 @ Here is the very first thing that \TeX\ prints
: a headline that identifies
473 the version number and format package. The |term_offset| variable is temporarily
474 incorrect
, but the discrepancy is not serious since we assume that the banner
475 and format identifier together will occupy at most |max_print_line|
479 void print_banner
(const char
*v
)
481 int callback_id
= callback_defined
(start_run_callback
);
482 if
(callback_id
== 0) {
483 fprintf
(term_out
, "This is " MyName
", Version %s%s ", v
, WEB2CVERSION
);
484 if
(format_ident
> 0)
487 if
(show_luahashchars
){
489 fprintf
(term_out
,"Number of bits used by the hash function (" my_name
"): %d",LUAI_HASHLIMIT
);
495 fprintf
(term_out
, "restricted ");
496 fprintf
(term_out
, "system commands enabled.\n");
498 } else if
(callback_id
> 0) {
499 run_callback
(callback_id
, "->");
504 void log_banner
(const char
*v
)
506 const char
*months
[] = { " ",
507 "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
508 "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
510 unsigned month
= (unsigned
) month_par
;
513 fprintf
(log_file
, "This is " MyName
", Version %s%s ", v
, WEB2CVERSION
);
519 fprintf
(log_file
, "%s", months
[month
]);
523 print_two
(time_par
/ 60);
525 print_two
(time_par
% 60);
530 fprintf
(log_file
, "restricted ");
531 fprintf
(log_file
, "system commands enabled.");
533 if
(filelineerrorstylep
) {
535 fprintf
(log_file
, " file:line:error style messages enabled.");
540 void print_version_banner
(void
)
542 fprintf
(term_out
, "%s", luatex_banner
);
545 @ The procedure |print_esc| prints a string that is preceded by
546 the user's escape character
(which is usually a backslash
).
549 void print_esc
(str_number s
)
551 int c
= escape_char_par
; /* Set variable |c| to the current escape character
*/
552 if
(c
>= 0 && c < STRING_OFFSET)
557 @ This prints escape character
, then |s|.
560 void tprint_esc
(const char
*s
)
562 int c
= escape_char_par
; /* Set variable |c| to the current escape character
*/
563 if
(c
>= 0 && c < STRING_OFFSET)
568 @ An array of digits in the range |
0.
.15| is printed by |print_the_digs|.
571 void print_the_digs
(eight_bits k
)
573 /* prints |dig
[k-1
]|$\
,\ldots\
,$|dig
[0]|
*/
576 print_char
('
0'
+ dig
[k
]);
578 print_char
('A'
- 10 + dig
[k
]);
582 @ The following procedure
, which prints out the decimal representation of a
583 given integer |n|
, has been written carefully so that it works properly
584 if |n
=0| or if |
(-n
)| would cause overflow. It does not apply |mod| or |div|
585 to negative arguments
, since such operations are not implemented consistently
586 by all
PASCAL compilers.
589 void print_int
(longinteger n
)
591 int k
= 0; /* index to current digit
; we assume that $|n|
<10^
{23}$
*/
592 longinteger m
; /* used to negate |n| in possibly dangerous cases
*/
595 if
(n
> -100000000) {
611 dig
[k
] = (int
) (n
% 10);
615 print_the_digs
((eight_bits
) k
);
619 @ Here is a trivial procedure to print two digits
; it is usually called with
620 a parameter in the range |
0<=n
<=99|.
623 void print_two
(int n
)
626 print_char
('
0'
+ (n
/ 10));
627 print_char
('
0'
+ (n
% 10));
630 @ Hexadecimal printing of nonnegative integers is accomplished by |print_hex|.
633 void print_hex
(int n
)
635 int k
= 0 ; /* index to current digit
; we assume that $
0\L n
<16^
{22}$
*/
642 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 /* nonpositive input produces no output */
668 u = v / (*(k - 1) - '0');
669 if (*(k - 1) == '2') {
671 u = u / (*(k - 1) - '0');
678 v = v / (*(j - 1) - '0');
683 @ The |print| subroutine will not print a string that is still being
684 created. The following procedure will.
687 void print_current_string(void)
689 unsigned j = 0; /* points to current character code */
690 while (j < cur_length)
691 print_char(cur_string[j++]);
694 @ The procedure |print_cs| prints the name of a control sequence, given
695 a pointer to its address in |eqtb|. A space is printed after the name
696 unless it is a single nonletter or an active character. This procedure
697 might be invoked with invalid data, so it is ``extra robust.'' The
698 individual characters must be printed one at a time using |print|, since
699 they may be unprintable.
704 str_number t = cs_text(p);
708 tprint_esc("csname
");
709 tprint_esc("endcsname
");
712 tprint_esc("IMPOSSIBLE.
");
714 } else if ((p >= undefined_control_sequence) &&
715 ((p <= eqtb_size) || p > eqtb_size + hash_extra)) {
716 tprint_esc("IMPOSSIBLE.
");
717 } else if (t >= str_ptr) {
718 tprint_esc("NONEXISTENT.
");
720 if (is_active_cs(t)) {
721 print(active_cs_value(t));
724 if (single_letter(t)) {
725 if (get_cat_code(cat_code_table_par, 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)
742 tprint_esc("csname
");
743 tprint_esc("endcsname
");
747 print(active_cs_value(t));
753 void sprint_cs_name(pointer p)
759 print(active_cs_value(t));
765 @ This procedure is never called when |interaction<scroll_mode|.
768 void prompt_input(const char *s)
775 @ Then there is a subroutine that prints glue stretch and shrink, possibly
776 followed by the name of finite units:
779 void print_glue(scaled d, int order, const char *s)
782 if ((order < normal) || (order > filll)) {
784 } else if (order > normal) {
786 while (order > sfi) {
790 } else if (s != NULL) {
795 @ The next subroutine prints a whole glue specification
798 void print_spec(int p, const char *s)
803 print_scaled(width(p));
806 if (stretch(p) != 0) {
808 print_glue(stretch(p), stretch_order(p), s);
810 if (shrink(p) != 0) {
812 print_glue(shrink(p), shrink_order(p), s);
817 @ We can reinforce our knowledge of the data structures just introduced
818 by considering two procedures that display a list in symbolic form.
819 The first of these, called |short_display|, is used in ``overfull box''
820 messages to give the top-level description of a list. The other one,
821 called |show_node_list|, prints a detailed description of exactly what
822 is in the data structure.
824 The philosophy of |short_display| is to ignore the fine points about exactly
825 what is inside boxes, except that ligatures and discretionary breaks are
826 expanded. As a result, |short_display| is a recursive procedure, but the
827 recursion is never more than one level deep.
830 A global variable |font_in_short_display| keeps track of the font code that
831 is assumed to be present when |short_display| begins; deviations from this
832 font will be printed.
835 int font_in_short_display; /* an internal font number */
837 @ Boxes, rules, inserts, whatsits, marks, and things in general that are
838 sort of ``complicated'' are indicated only by printing `\.{[]}'.
841 void print_font_identifier(internal_font_number f)
844 fonttext = font_id_text(f);
851 if (tracing_fonts_par > 0) {
854 if (font_size(f) != font_dsize(f)) {
856 print_scaled(font_size(f));
863 @ This prints highlights of list |p|.
866 void short_display(int p)
869 if (is_char_node(p)) {
870 if (lig_ptr(p) != null) {
871 short_display(lig_ptr(p));
873 if (font(p) != font_in_short_display) {
874 if (!is_valid_font(font(p)))
877 print_font_identifier(font(p));
879 font_in_short_display = font(p);
884 /* Print a short indication of the contents of node |p| */
885 print_short_node_contents(p);
891 @ The |show_node_list| routine requires some auxiliary subroutines: one to
892 print a font-and-character combination, one to print a token list without
893 its reference count, and one to print a rule dimension.
895 @ This prints |char_node| data.
898 void print_font_and_char(int p)
900 if (!is_valid_font(font(p)))
903 print_font_identifier(font(p));
908 @ This prints token list data in braces
911 void print_mark(int p)
914 if ((p < (int) fix_mem_min) || (p > (int) fix_mem_end))
915 tprint_esc("CLOBBERED.
");
917 show_token_list(token_link(p), null, max_print_line - 10);
921 @ This prints dimensions of a rule node.
924 void print_rule_dimen(scaled d)
932 @ Since boxes can be inside of boxes, |show_node_list| is inherently recursive,
934 up to a given maximum number of levels. The history of nesting is indicated
935 by the current string, which will be printed at the beginning of each line;
936 the length of this string, namely |cur_length|, is the depth of nesting.
938 A global variable called |depth_threshold| is used to record the maximum
939 depth of nesting for which |show_node_list| will show information. If we
940 have |depth_threshold=0|, for example, only the top level information will
941 be given and no sublists will be traversed. Another global variable, called
942 |breadth_max|, tells the maximum number of items to show at each level;
943 |breadth_max| had better be positive, or you won't see anything.
946 int depth_threshold; /* maximum nesting depth in box displays */
947 int breadth_max; /* maximum number of items shown at the same list level */
949 @ The recursive machinery is started by calling |show_box|. Assign the values
950 |depth_threshold:=show_box_depth| and |breadth_max:=show_box_breadth|
953 void show_box(halfword p)
955 depth_threshold = show_box_depth_par;
956 breadth_max = show_box_breadth_par;
957 if (breadth_max <= 0)
959 /* the show starts at |p| */
964 @ Helper for debugging purposes. It prints highlights of list |p|
967 void short_display_n(int p, int m)
970 font_in_short_display = null_font;
974 if (is_char_node(p)) {
975 if (p <= max_halfword) {
976 if (font(p) != font_in_short_display) {
977 if (!is_valid_font(font(p)))
980 print_font_identifier(font(p));
982 font_in_short_display = font(p);
987 if ( (type(p) == glue_node) ||
988 (type(p) == disc_node) ||
989 (type(p) == penalty_node) ||
990 ((type(p) == kern_node) && (subtype(p) == explicit_kern ||
991 subtype(p) == italic_kern ))) {
996 if (type(p) == disc_node) {
998 short_display(vlink(pre_break(p)));
1000 short_display(vlink(post_break(p)));
1003 /* Print a short indication of the contents of node |p| */
1004 print_short_node_contents(p);
1014 @ When debugging a macro package, it can be useful to see the exact
1015 control sequence names in the format file. For example, if ten new
1016 csnames appear, it's nice to know what they are, to help pinpoint where
1017 they came from. (This isn't a truly ``basic'' printing procedure, but
1018 that's a convenient module in which to put it.)
1021 void print_csnames(int hstart, int hfinish)
1024 unsigned char *c, *l;
1025 fprintf(stderr, "fmtdebug
:csnames from
%d to
%d
:", (int) hstart, (int) hfinish);
1026 for (h = hstart; h <= hfinish; h++) {
1027 if (cs_text(h) > 0) {
1028 /* we have anything at this position */
1029 c = str_string(cs_text(h));
1030 l = c + str_length(cs_text(h));
1032 /* print the characters */
1033 fputc(*c++, stderr);
1035 fprintf(stderr, "|
");
1040 @ A helper for printing file:line:error style messages. Look for a
1041 filename in |full_source_filename_stack|, and if we fail to find
1042 one fall back on the non-file:line:error style.
1045 void print_file_line(void)
1047 int level = in_open;
1048 while ((level > 0) && (full_source_filename_stack[level] == 0))
1054 tprint(full_source_filename_stack[level]);
1056 if (level == in_open)
1059 print_int(line_stack[level + 1]);
1064 @ \TeX\ is occasionally supposed to print diagnostic information that
1065 goes only into the transcript file, unless |tracing_online| is positive.
1066 Here are two routines that adjust the destination of print commands:
1069 void begin_diagnostic(void)
1071 global_old_setting = selector;
1072 if ((tracing_online_par <= 0) && (selector == term_and_log)) {
1074 if (history == spotless)
1075 history = warning_issued;
1079 @ Restore proper conditions after tracing.
1082 void end_diagnostic(boolean blank_line)
1087 selector = global_old_setting;
1090 @ Of course we had better declare another global variable, if the previous
1091 routines are going to work.
1094 int global_old_setting;