Debian Codegraph @

texk/web2c/luatexdir/tex/printing.c

raw
1 /*
2
3 printing.w
4
5 Copyright 2009-2013 Taco Hoekwater <[email protected]>
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 #include "lua/luatex-api.h"
26
27 #define wlog(A) fputc(A,log_file)
28 #define wterm(A) fputc(A,term_out)
29
30 int new_string_line = 0;
31 int escape_controls = 1;
32
33 /*tex
34
35 Messages that are sent to a user's terminal and to the transcript-log file are
36 produced by several `|print|' procedures. These procedures will direct their
37 output to a variety of places, based on the setting of the global variable
38 |selector|, which has the following possible values:
39
40 \startitemize
41
42 \startitem
43 |term_and_log|, the normal setting, prints on the terminal and on the
44 transcript file.
45 \stopitem
46
47 \startitem
48 |log_only|, prints only on the transcript file.
49 \stopitem
50
51 \startitem
52 |term_only|, prints only on the terminal.
53 \stopitem
54
55 \startitem
56 |no_print|, doesn't print at all. This is used only in rare cases before the
57 transcript file is open.
58 \stopitem
59
60 \startitem
61 |pseudo|, puts output into a cyclic buffer that is used by the |show_context|
62 routine; when we get to that routine we shall discuss the reasoning behind
63 this curious mode.
64 \stopitem
65
66 \startitem
67 |new_string|, appends the output to the current string in the string pool.
68 \stopitem
69
70 \startitem
71 0 to 15, prints on one of the sixteen files for \.{\\write} output.
72 \stopitem
73
74 \stopitemize
75
76 The symbolic names `|term_and_log|', etc., have been assigned numeric codes that
77 satisfy the convenient relations |no_print+1=term_only|, |no_print+2=log_only|,
78 |term_only+2=log_only+1=term_and_log|.
79
80 Three additional global variables, |tally| and |term_offset| and |file_offset|,
81 record the number of characters that have been printed since they were most
82 recently cleared to zero. We use |tally| to record the length of (possibly very
83 long) stretches of printing; |term_offset| and |file_offset|, on the other hand,
84 keep track of how many characters have appeared so far on the current line that
85 has been output to the terminal or to the transcript file, respectively.
86
87 */
88
89 /*tex transcript of \TeX\ session */
90
91 alpha_file log_file;
92
93 /*tex where to print a message */
94
95 int selector = term_only;
96
97 /*tex digits in a number being output */
98
99 int dig[23];
100
101 /*tex the number of characters recently printed */
102
103 int tally = 0;
104
105 /*tex the number of characters on the current terminal line */
106
107 int term_offset = 0;
108
109 /*tex the number of characters on the current file line */
110
111 int file_offset = 0;
112
113 /*tex circular buffer for pseudoprinting */
114
115 packed_ASCII_code trick_buf[(ssup_error_line + 1)];
116
117 /*tex threshold for pseudoprinting, explained later */
118
119 int trick_count;
120
121 /*tex another variable for pseudoprinting */
122
123 int first_count;
124
125 /*tex for minor adjustments to |show_token_list| */
126
127 boolean inhibit_par_tokens = false;
128
129 /*tex
130
131 To end a line of text output, we call |print_ln|
132
133 */
134
135 void print_ln(void)
136 {
137 switch (selector) {
138 case no_print:
139 break;
140 case term_only:
141 wterm_cr();
142 term_offset = 0;
143 break;
144 case log_only:
145 wlog_cr();
146 file_offset = 0;
147 break;
148 case term_and_log:
149 wterm_cr();
150 wlog_cr();
151 term_offset = 0;
152 file_offset = 0;
153 break;
154 case pseudo:
155 break;
156 case new_string:
157 if (new_string_line > 0)
158 print_char(new_string_line);
159 break;
160 default:
161 fprintf(write_file[selector], "\n");
162 break;
163 }
164 /*tex |tally| is not affected */
165 }
166
167 /*tex
168
169 The |print_char| procedure sends one byte to the desired destination. All
170 printing comes through |print_ln| or |print_char|, except for the case of
171 |tprint| (see below).
172
173 The checking of the line length is an inheritance from previosu engines and we
174 might drop it after release 1.0. We're not too picky about the exact match of
175 that length because we have utf output so length is then a bit fuzzy anyway.
176
177 */
178
179 #define needs_escaping(A) \
180 ((! escape_controls) || (A>=0x20) || (A==0x0A) || (A==0x0D) || (A==0x09))
181
182 #define escaped_char(A) \
183 A+64
184
185
186 #define wlog_char(A) \
187 if (needs_escaping(A)) { \
188 wlog(A); \
189 } else { \
190 if (file_offset+2>=max_print_line) { \
191 wlog_cr(); \
192 file_offset=0; \
193 } \
194 wlog('^'); \
195 wlog('^'); \
196 wlog(escaped_char(A)); \
197 file_offset += 2; \
198 }
199
200
201 #define wterm_char(A) \
202 if (needs_escaping(A)) { \
203 wterm(A); \
204 } else { \
205 if (term_offset+2>=max_print_line) { \
206 wterm_cr(); \
207 term_offset=0; \
208 } \
209 wterm('^'); \
210 wterm('^'); \
211 wterm(escaped_char(A)); \
212 term_offset += 2; \
213 }
214
215 #define needs_wrapping(A,B) \
216 ( (A>=0xC0) && \
217 (((A>=0xF0) && (B+4>=max_print_line)) || \
218 ((A>=0xE0) && (B+3>=max_print_line)) || \
219 ( (B+2>=max_print_line))) \
220 )
221
222 #define fix_term_offset(A) \
223 if (needs_wrapping(A,term_offset)){ \
224 wterm_cr(); \
225 term_offset=0; \
226 }
227
228 #define fix_log_offset(A) \
229 if (needs_wrapping(A,file_offset)){ \
230 wlog_cr(); \
231 file_offset=0; \
232 }
233
234 void print_char(int s)
235 {
236 if (s < 0 || s > 255) {
237 formatted_warning("print","weird character %i",s);
238 return;
239 }
240 if (s == new_line_char_par) {
241 if (selector < pseudo) {
242 print_ln();
243 return;
244 }
245 }
246 switch (selector) {
247 case no_print:
248 break;
249 case term_only:
250 fix_term_offset(s);
251 wterm_char(s);
252 incr(term_offset);
253 if (term_offset == max_print_line) {
254 wterm_cr();
255 term_offset = 0;
256 }
257 break;
258 case log_only:
259 fix_log_offset(s);
260 wlog_char(s);
261 incr(file_offset);
262 if (file_offset == max_print_line) {
263 wlog_cr();
264 file_offset = 0;
265 }
266 break;
267 case term_and_log:
268 fix_term_offset(s);
269 fix_log_offset(s);
270 wterm_char(s);
271 wlog_char(s);
272 incr(term_offset);
273 incr(file_offset);
274 if (term_offset == max_print_line) {
275 wterm_cr();
276 term_offset = 0;
277 }
278 if (file_offset == max_print_line) {
279 wlog_cr();
280 file_offset = 0;
281 }
282 break;
283 case pseudo:
284 if (tally < trick_count)
285 trick_buf[tally % error_line] = (packed_ASCII_code) s;
286 break;
287 case new_string:
288 append_char(s);
289 break;
290 default:
291 fprintf(write_file[selector], "%c", s);
292 }
293 incr(tally);
294 }
295
296 /*tex
297
298 An entire string is output by calling |print|. Note that if we are outputting the
299 single standard ASCII character \.c, we could call |print("c")|, since |"c"=99|
300 is the number of a single-character string, as explained above. But
301 |print_char("c")| is quicker, so \TeX\ goes directly to the |print_char| routine
302 when it knows that this is safe. (The present implementation assumes that it is
303 always safe to print a visible ASCII character.)
304
305 The first 256 entries above the 17th unicode plane are used for a special trick:
306 when \TeX\ has to print items in that range, it will instead print the character
307 that results from substracting 0x110000 from that value. This allows
308 byte-oriented output to things like \.{\\specials} and \.{\\pdfextension
309 literals}. Todo: Perhaps it would be useful to do the same substraction while
310 typesetting.
311
312
313 */
314
315 void print(int s)
316 {
317 if (s >= str_ptr) {
318 normal_warning("print","bad string pointer");
319 return;
320 } else if (s < STRING_OFFSET) {
321 if (s < 0) {
322 normal_warning("print","bad string offset");
323 } else {
324 /*tex We're not sure about this so it's disabled for now! */
325 if ((false) && (selector > pseudo)) {
326 /*tex internal strings are not expanded */
327 print_char(s);
328 return;
329 }
330 if (s == new_line_char_par) {
331 if (selector < pseudo) {
332 print_ln();
333 return;
334 }
335 }
336 if (s <= 0x7F) {
337 print_char(s);
338 } else if (s <= 0x7FF) {
339 print_char(0xC0 + (s / 0x40));
340 print_char(0x80 + (s % 0x40));
341 } else if (s <= 0xFFFF) {
342 print_char(0xE0 + (s / 0x1000));
343 print_char(0x80 + ((s % 0x1000) / 0x40));
344 print_char(0x80 + ((s % 0x1000) % 0x40));
345 } else if (s >= 0x110000) {
346 int c = s - 0x110000;
347 if (c >= 256) {
348 formatted_warning("print", "bad raw byte to print (c=%d), skipped",c);
349 } else {
350 print_char(c);
351 }
352 } else {
353 print_char(0xF0 + (s / 0x40000));
354 print_char(0x80 + ((s % 0x40000) / 0x1000));
355 print_char(0x80 + (((s % 0x40000) % 0x1000) / 0x40));
356 print_char(0x80 + (((s % 0x40000) % 0x1000) % 0x40));
357 }
358 }
359 return;
360 }
361 if (selector == new_string) {
362 append_string(str_string(s), (unsigned) str_length(s));
363 return;
364 }
365 lprint(&str_lstring(s));
366 }
367
368 void lprint (lstring *ss) {
369 /*tex current character code position */
370 unsigned char *j, *l;
371 j = ss->s;
372 l = j + ss->l;
373 while (j < l) {
374 /*tex I don't bother checking the last two bytes explicitly */
375 /* 0x110000 in utf=8: 0xF4 0x90 0x80 0x80 */
376 if ((j < l - 4) && (*j == 0xF4) && (*(j + 1) == 0x90)) {
377 int c = (*(j + 2) - 128) * 64 + (*(j + 3) - 128);
378 assert(c >= 0 && c < 256);
379 print_char(c);
380 j = j + 4;
381 } else {
382 print_char(*j);
383 incr(j);
384 }
385 }
386 }
387
388 /*tex
389
390 The procedure |print_nl| is like |print|, but it makes sure that the string
391 appears at the beginning of a new line.
392
393 */
394
395 /*tex Moev to the beginning of the next line. */
396
397 void print_nlp(void)
398 {
399 if (new_string_line > 0) {
400 print_char(new_string_line);
401 } else if ((selector < no_print) ||
402 ((term_offset > 0) && (odd(selector))) ||
403 ((file_offset > 0) && (selector >= log_only))) {
404 print_ln();
405 }
406 }
407
408 /*tex Prints string |s| at beginning of the next line. */
409
410 void print_nl(str_number s)
411 {
412 print_nlp();
413 print(s);
414 }
415
416 /*tex
417
418 |char *| versions of the same procedures. |tprint| is different because it uses
419 buffering, which works well because most of the output actually comes through
420 |tprint|.
421
422 */
423
424 #define t_flush_buffer(target,offset) \
425 buffer[i++] = '\n'; \
426 buffer[i++] = '\0';\
427 fputs(buffer, target); \
428 i = 0; \
429 buffer[0] = '\0'; \
430 offset=0;
431
432 void tprint(const char *sss)
433 {
434 char *buffer = NULL;
435 int i = 0;
436 int newlinechar = (lua_only == 1) ? 10 : new_line_char_par;
437 int dolog = 0;
438 int doterm = 0;
439 switch (selector) {
440 case no_print:
441 return;
442 break;
443 case term_only:
444 doterm = 1;
445 break;
446 case log_only:
447 dolog = 1;
448 break;
449 case term_and_log:
450 dolog = 1;
451 doterm = 1;
452 break;
453 case pseudo:
454 while (*sss) {
455 if (tally < trick_count) {
456 trick_buf[tally % error_line] = (packed_ASCII_code) *sss++;
457 tally++;
458 } else {
459 return;
460 }
461 }
462 return;
463 break;
464 case new_string:
465 append_string((const unsigned char *)sss, (unsigned) strlen(sss));
466 return;
467 break;
468 default:
469 {
470 char *newstr = xstrdup(sss);
471 char *s;
472 for (s=newstr;*s;s++) {
473 if (*s == newlinechar) {
474 *s = '\n';
475 }
476 }
477 fputs(newstr, write_file[selector]);
478 free(newstr);
479 return;
480 }
481 break;
482 }
483 /*tex What is left is the 3 term/log settings. */
484 if (dolog || doterm) {
485 buffer = xmalloc(strlen(sss)*3);
486 buffer[0] = '\0';
487 /*tex The |wrapup_run| callback acts when the log file is already closed.*/
488 if (dolog && log_opened_global) {
489 const unsigned char *ss = (const unsigned char *) sss;
490 while (*ss) {
491 int s = *ss++;
492 if (needs_wrapping(s,file_offset) || s == newlinechar) {
493 t_flush_buffer(log_file,file_offset);
494 }
495 if (s != newlinechar) {
496 buffer[i++] = s;
497 if (file_offset++ == max_print_line) {
498 t_flush_buffer(log_file,file_offset);
499 }
500 }
501 }
502 if (*buffer) {
503 buffer[i++] = '\0';
504 fputs(buffer, log_file);
505 buffer[0] = '\0';
506 }
507 i = 0;
508 }
509 if (doterm) {
510 const unsigned char *ss = (const unsigned char *) sss;
511 while (*ss) {
512 int s = *ss++;
513 if (needs_wrapping(s,term_offset) || s == newlinechar) {
514 t_flush_buffer(term_out,term_offset);
515 }
516 if (s != newlinechar) {
517 if (needs_escaping(s)) {
518 buffer[i++] = s;
519 } else {
520 buffer[i++] = '^';
521 buffer[i++] = '^';
522 buffer[i++] = escaped_char(s);
523 term_offset += 2;
524 }
525 if (++term_offset == max_print_line) {
526 t_flush_buffer(term_out,term_offset);
527 }
528 }
529 }
530 tally += i;
531 if (*buffer) {
532 buffer[i++] = '\0';
533 fputs(buffer, term_out);
534 }
535 }
536 free(buffer);
537 }
538 }
539
540 void tprint_nl(const char *s)
541 {
542 print_nlp();
543 tprint(s);
544 }
545
546 /*tex
547
548 Here is the very first thing that \TeX\ prints: a headline that identifies the
549 version number and format package. The |term_offset| variable is temporarily
550 incorrect, but the discrepancy is not serious since we assume that the banner and
551 format identifier together will occupy at most |max_print_line| character
552 positions.
553
554 */
555
556 void print_banner(const char *v)
557 {
558 int callback_id = callback_defined(start_run_callback);
559 if (callback_id == 0) {
560 fprintf(term_out, "This is " MyName ", Version %s%s ", v, WEB2CVERSION);
561 if (format_ident > 0)
562 print(format_ident);
563 print_ln();
564 if (show_luahashchars) {
565 wterm(' ');
566 fprintf(term_out,"Number of bits used by the hash function (" my_name "): %d",LUAI_HASHLIMIT);
567 print_ln();
568 }
569 if (shellenabledp) {
570 wterm(' ');
571 if (restrictedshell) {
572 fprintf(term_out, "restricted ");
573 }
574 fprintf(term_out, "system commands enabled.\n");
575 }
576 } else if (callback_id > 0) {
577 run_callback(callback_id, "->");
578 }
579 }
580
581 void log_banner(const char *v)
582 {
583 const char *months[] = { " ",
584 "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
585 "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
586 };
587 unsigned month = (unsigned) month_par;
588 if (month > 12)
589 month = 0;
590 fprintf(log_file, "This is " MyName ", Version %s%s ", v, WEB2CVERSION);
591 print(format_ident);
592 print_char(' ');
593 print_char(' ');
594 print_int(day_par);
595 print_char(' ');
596 fprintf(log_file, "%s", months[month]);
597 print_char(' ');
598 print_int(year_par);
599 print_char(' ');
600 print_two(time_par / 60);
601 print_char(':');
602 print_two(time_par % 60);
603 if (shellenabledp) {
604 wlog_cr();
605 wlog(' ');
606 if (restrictedshell)
607 fprintf(log_file, "restricted ");
608 fprintf(log_file, "system commands enabled.");
609 }
610 if (filelineerrorstylep) {
611 wlog_cr();
612 fprintf(log_file, " file:line:error style messages enabled.");
613 }
614 }
615
616 void print_version_banner(void)
617 {
618 fprintf(term_out, "%s", luatex_banner);
619 }
620
621 /*tex
622
623 The procedure |print_esc| prints a string that is preceded by the user's escape
624 character (which is usually a backslash).
625
626 */
627
628 void print_esc(str_number s)
629 {
630 /*tex Set variable |c| to the current escape character: */
631 int c = escape_char_par;
632 if (c >= 0 && c < 0x110000)
633 print(c);
634 print(s);
635 }
636
637 /*tex
638
639 This prints escape character, then |s|.
640
641 */
642
643 void tprint_esc(const char *s)
644 {
645 /*tex Set variable |c| to the current escape character: */
646 int c = escape_char_par;
647 if (c >= 0 && c < 0x110000)
648 print(c);
649 tprint(s);
650 }
651
652 /*tex
653
654 An array of digits in the range |0..15| is printed by |print_the_digs|.
655
656 */
657
658 void print_the_digs(eight_bits k)
659 {
660 /*tex prints |dig[k-1]|$\,\ldots\,$|dig[0]| */
661 while (k-- > 0) {
662 if (dig[k] < 10)
663 print_char('0' + dig[k]);
664 else
665 print_char('A' - 10 + dig[k]);
666 }
667 }
668
669 /*tex
670
671 The following procedure, which prints out the decimal representation of a given
672 integer |n|, has been written carefully so that it works properly if |n=0| or if
673 |(-n)| would cause overflow. It does not apply |mod| or |div| to negative
674 arguments, since such operations are not implemented consistently by all PASCAL
675 compilers.
676
677 */
678
679 void print_int(longinteger n)
680 {
681 /*tex index to current digit; we assume that $|n|<10^{23}$ */
682 int k = 0;
683 /*tex used to negate |n| in possibly dangerous cases */
684 longinteger m;
685 if (n < 0) {
686 print_char('-');
687 if (n > -100000000) {
688 n = -n;
689 } else {
690 m = -1 - n;
691 n = m / 10;
692 m = (m % 10) + 1;
693 k = 1;
694 if (m < 10)
695 dig[0] = (int) m;
696 else {
697 dig[0] = 0;
698 incr(n);
699 }
700 }
701 }
702 do {
703 dig[k] = (int) (n % 10);
704 n = n / 10;
705 incr(k);
706 } while (n != 0);
707 print_the_digs((eight_bits) k);
708 }
709
710 /*tex
711
712 Here is a trivial procedure to print two digits; it is usually called with a
713 parameter in the range |0<=n<=99|.
714
715 */
716
717 void print_two(int n)
718 {
719 n = abs(n) % 100;
720 print_char('0' + (n / 10));
721 print_char('0' + (n % 10));
722 }
723
724 /*tex
725
726 Hexadecimal printing of nonnegative integers is accomplished by |print_hex|.
727
728 */
729
730 void print_qhex(int n)
731 {
732 /*tex index to current digit; we assume that $0\L n<16^{22}$ */
733 int k = 0 ;
734 print_char('"');
735 do {
736 dig[k] = n % 16;
737 n = n / 16;
738 incr(k);
739 } while (n != 0);
740 print_the_digs((eight_bits) k);
741 }
742
743 /*tex
744
745 Roman numerals are produced by the |print_roman_int| routine. Readers who like
746 puzzles might enjoy trying to figure out how this tricky code works; therefore no
747 explanation will be given. Notice that 1990 yields \.{mcmxc}, not \.{mxm}.
748
749 */
750
751 void print_roman_int(int n)
752 {
753 char *j, *k;
754 int u, v;
755 char mystery[] = "m2d5c2l5x2v5i";
756 j = (char *) mystery;
757 v = 1000;
758 while (1) {
759 while (n >= v) {
760 print_char(*j);
761 n = n - v;
762 }
763 if (n <= 0) {
764 /*tex nonpositive input produces no output */
765 return;
766 }
767 k = j + 2;
768 u = v / (*(k - 1) - '0');
769 if (*(k - 1) == '2') {
770 k = k + 2;
771 u = u / (*(k - 1) - '0');
772 }
773 if (n + u >= v) {
774 print_char(*k);
775 n = n + u;
776 } else {
777 j = j + 2;
778 v = v / (*(j - 1) - '0');
779 }
780 }
781 }
782
783 /*tex
784
785 The |print| subroutine will not print a string that is still being created. The
786 following procedure will.
787
788 */
789
790 void print_current_string(void)
791 {
792 /*tex points to current character code */
793 unsigned j = 0;
794 while (j < cur_length)
795 print_char(cur_string[j++]);
796 }
797
798 /*tex
799
800 The procedure |print_cs| prints the name of a control sequence, given a pointer
801 to its address in |eqtb|. A space is printed after the name unless it is a single
802 nonletter or an active character. This procedure might be invoked with invalid
803 data, so it is ``extra robust.'' The individual characters must be printed one at
804 a time using |print|, since they may be unprintable.
805
806 */
807
808 void print_cs(int p)
809 {
810 str_number t = cs_text(p);
811 if (p < hash_base) {
812 /* nullcs */
813 if (p == null_cs) {
814 tprint_esc("csname");
815 tprint_esc("endcsname");
816 print_char(' ');
817 } else {
818 tprint_esc("IMPOSSIBLE.");
819 }
820 } else if ((p >= undefined_control_sequence) &&
821 ((p <= eqtb_size) || p > eqtb_size + hash_extra)) {
822 tprint_esc("IMPOSSIBLE.");
823 } else if (t >= str_ptr) {
824 tprint_esc("NONEXISTENT.");
825 } else {
826 if (is_active_cs(t)) {
827 print(active_cs_value(t));
828 } else {
829 print_esc(t);
830 if (single_letter(t)) {
831 if (get_cat_code(cat_code_table_par, pool_to_unichar(str_string(t))) == letter_cmd)
832 print_char(' ');
833 } else {
834 print_char(' ');
835 }
836 }
837 }
838 }
839
840 /*tex
841
842 Here is a similar procedure; it avoids the error checks, and it never prints a
843 space after the control sequence.
844
845 */
846 void sprint_cs(pointer p)
847 {
848 str_number t;
849 if (p == null_cs) {
850 tprint_esc("csname");
851 tprint_esc("endcsname");
852 } else {
853 t = cs_text(p);
854 if (is_active_cs(t))
855 print(active_cs_value(t));
856 else
857 print_esc(t);
858 }
859 }
860
861 void sprint_cs_name(pointer p)
862 {
863 str_number t;
864 if (p != null_cs) {
865 t = cs_text(p);
866 if (is_active_cs(t))
867 print(active_cs_value(t));
868 else
869 print(t);
870 }
871 }
872
873 /*tex
874
875 This procedure is never called when |interaction<scroll_mode|.
876
877 */
878
879 void prompt_input(const char *s)
880 {
881 wake_up_terminal();
882 tprint(s);
883 term_input();
884 }
885
886 /*tex
887
888 Then there is a subroutine that prints glue stretch and shrink, possibly followed
889 by the name of finite units:
890
891 */
892
893 void print_glue(scaled d, int order, const char *s)
894 {
895 print_scaled(d);
896 if ((order < normal) || (order > filll)) {
897 tprint("foul");
898 } else if (order > normal) {
899 tprint("fi");
900 while (order > sfi) {
901 print_char('l');
902 decr(order);
903 }
904 } else if (s != NULL) {
905 tprint(s);
906 }
907 }
908
909 /*tex
910
911 The next subroutine prints a whole glue specification
912
913 */
914
915 void print_spec(int p, const char *s)
916 {
917 if (p < 0) {
918 print_char('*');
919 } else {
920 print_scaled(width(p));
921 if (s != NULL)
922 tprint(s);
923 if (stretch(p) != 0) {
924 tprint(" plus ");
925 print_glue(stretch(p), stretch_order(p), s);
926 }
927 if (shrink(p) != 0) {
928 tprint(" minus ");
929 print_glue(shrink(p), shrink_order(p), s);
930 }
931 }
932 }
933
934 /*tex
935
936 We can reinforce our knowledge of the data structures just introduced by
937 considering two procedures that display a list in symbolic form. The first of
938 these, called |short_display|, is used in ``overfull box'' messages to give the
939 top-level description of a list. The other one, called |show_node_list|, prints a
940 detailed description of exactly what is in the data structure.
941
942 The philosophy of |short_display| is to ignore the fine points about exactly what
943 is inside boxes, except that ligatures and discretionary breaks are expanded. As
944 a result, |short_display| is a recursive procedure, but the recursion is never
945 more than one level deep. @^recursion@>
946
947 A global variable |font_in_short_display| keeps track of the font code that is
948 assumed to be present when |short_display| begins; deviations from this font will
949 be printed.
950
951 */
952
953 /*tex An internal font number: */
954
955 int font_in_short_display;
956
957 /*tex
958
959 Boxes, rules, inserts, whatsits, marks, and things in general that are sort of
960 ``complicated'' are indicated only by printing `\.{[]}'.
961
962 We print a bit more than original \TEX. A value of 0 or 1 or any large value will
963 behave the same as before. The reason for this extension is that a |name| not
964 always makes sense.
965
966 \starttyping
967 0 \foo xyz
968 1 \foo (bar)
969 2 <bar> xyz
970 3 <bar @ ..> xyz
971 4 <id>
972 5 <id: bar>
973 6 <id: bar @ ..> xyz
974 \stoptyping
975
976 */
977
978 void print_font_identifier(internal_font_number f)
979 {
980 str_number fonttext;
981 fonttext = font_id_text(f);
982 if (tracing_fonts_par >= 2 && tracing_fonts_par <= 6) {
983 /*tex < > is less likely to clash with text parenthesis */
984 tprint("<");
985 if (tracing_fonts_par >= 2 && tracing_fonts_par <= 3) {
986 print_font_name(f);
987 if (tracing_fonts_par >= 3 || font_size(f) != font_dsize(f)) {
988 tprint(" @ ");
989 print_scaled(font_size(f));
990 tprint("pt");
991 }
992 } else if (tracing_fonts_par >= 4 && tracing_fonts_par <= 6) {
993 print_int(f);
994 if (tracing_fonts_par >= 5) {
995 tprint(": ");
996 print_font_name(f);
997 if (tracing_fonts_par >= 6 || font_size(f) != font_dsize(f)) {
998 tprint(" @ ");
999 print_scaled(font_size(f));
1000 tprint("pt");
1001 }
1002 }
1003 }
1004 print_char('>');
1005 } else {
1006 /*tex old method, inherited from pdftex */
1007 if (fonttext > 0) {
1008 print_esc(fonttext);
1009 } else {
1010 tprint_esc("FONT");
1011 print_int(f);
1012 }
1013 if (tracing_fonts_par > 0) {
1014 tprint(" (");
1015 print_font_name(f);
1016 if (font_size(f) != font_dsize(f)) {
1017 tprint("@");
1018 print_scaled(font_size(f));
1019 tprint("pt");
1020 }
1021 print_char(')');
1022 }
1023 }
1024 }
1025
1026 /*tex
1027 We could do this much nicer but then we need to also adapt short_display a
1028 bit and we have to be as compatible as possible in the log for some macro
1029 packages.
1030
1031 The callback is also responsible for either or not reporting the character
1032 number itself.
1033 */
1034
1035 void print_character_info(halfword p)
1036 {
1037 int callback_id = callback_defined(glyph_info_callback);
1038 if (callback_id) {
1039 char* str = NULL;
1040 run_callback(callback_id, "N->R", p, &str);
1041 if (str == NULL) {
1042 print_qhex(character(p));
1043 } else {
1044 tprint(str);
1045 free(str);
1046 }
1047 } else {
1048 print(character(p));
1049 }
1050 }
1051
1052 /*tex
1053 This prints highlights of list |p|.
1054 */
1055
1056 void short_display(int p)
1057 {
1058 while (p != null) {
1059 if (is_char_node(p)) {
1060 if (lig_ptr(p) != null) {
1061 short_display(lig_ptr(p));
1062 } else {
1063 if (font(p) != font_in_short_display) {
1064 if (!is_valid_font(font(p))) {
1065 print_char('*');
1066 } else {
1067 print_font_identifier(font(p));
1068 }
1069 print_char(' ');
1070 font_in_short_display = font(p);
1071 }
1072 print_character_info(p);
1073 }
1074 } else {
1075 /*tex Print a short indication of the contents of node |p| */
1076 print_short_node_contents(p);
1077 }
1078 p = vlink(p);
1079 }
1080 }
1081
1082 /*tex
1083
1084 The |show_node_list| routine requires some auxiliary subroutines: one to print a
1085 font-and-character combination, one to print a token list without its reference
1086 count, and one to print a rule dimension.
1087
1088 */
1089
1090 /*tex
1091
1092 This prints |char_node| data.
1093
1094 */
1095
1096 void print_font_and_char(int p)
1097 {
1098 if (!is_valid_font(font(p)))
1099 print_char('*');
1100 else
1101 print_font_identifier(font(p));
1102 print_char(' ');
1103 print_character_info(p);
1104 }
1105
1106 /*tex
1107
1108 This prints token list data in braces
1109
1110 */
1111
1112 void print_mark(int p)
1113 {
1114 print_char('{');
1115 if ((p < (int) fix_mem_min) || (p > (int) fix_mem_end))
1116 tprint_esc("CLOBBERED.");
1117 else
1118 show_token_list(token_link(p), null, max_print_line - 10);
1119 print_char('}');
1120 }
1121
1122 /*tex
1123
1124 This prints dimensions of a rule node.
1125
1126 */
1127
1128 void print_rule_dimen(scaled d)
1129 {
1130 if (is_running(d))
1131 print_char('*');
1132 else
1133 print_scaled(d);
1134 }
1135
1136 /*tex
1137
1138 Since boxes can be inside of boxes, |show_node_list| is inherently recursive,
1139 @^recursion@> up to a given maximum number of levels. The history of nesting is
1140 indicated by the current string, which will be printed at the beginning of each
1141 line; the length of this string, namely |cur_length|, is the depth of nesting.
1142
1143 A global variable called |depth_threshold| is used to record the maximum depth of
1144 nesting for which |show_node_list| will show information. If we have
1145 |depth_threshold=0|, for example, only the top level information will be given
1146 and no sublists will be traversed. Another global variable, called |breadth_max|,
1147 tells the maximum number of items to show at each level; |breadth_max| had better
1148 be positive, or you won't see anything.
1149
1150 */
1151
1152 /*tex The maximum nesting depth in box displays: */
1153
1154 int depth_threshold;
1155
1156 /*tex The maximum number of items shown at the same list level: */
1157
1158 int breadth_max;
1159
1160 /*tex
1161
1162 The recursive machinery is started by calling |show_box|. Assign the values
1163 |depth_threshold:=show_box_depth| and |breadth_max:=show_box_breadth|
1164
1165 */
1166
1167 void show_box(halfword p)
1168 {
1169 depth_threshold = show_box_depth_par;
1170 breadth_max = show_box_breadth_par;
1171 if (breadth_max <= 0)
1172 breadth_max = 5;
1173 /*tex the show starts at |p| */
1174 show_node_list(p);
1175 print_ln();
1176 }
1177
1178 /*tex
1179
1180 Helper for debugging purposes. It prints highlights of list |p|
1181
1182 */
1183
1184 void short_display_n(int p, int m)
1185 {
1186 int i = 0;
1187 font_in_short_display = null_font;
1188 if (p == null)
1189 return;
1190 while (p != null) {
1191 if (is_char_node(p)) {
1192 if (p <= max_halfword) {
1193 if (font(p) != font_in_short_display) {
1194 if (!is_valid_font(font(p)))
1195 print_char('*');
1196 else
1197 print_font_identifier(font(p));
1198 print_char(' ');
1199 font_in_short_display = font(p);
1200 }
1201 print_character_info(p);
1202 }
1203 } else {
1204 if ( (type(p) == glue_node) ||
1205 (type(p) == disc_node) ||
1206 (type(p) == penalty_node) ||
1207 ((type(p) == kern_node) && (subtype(p) == explicit_kern ||
1208 subtype(p) == italic_kern ))) {
1209 incr(i);
1210 }
1211 if (i >= m)
1212 return;
1213 if (type(p) == disc_node) {
1214 print_char('|');
1215 short_display(vlink(pre_break(p)));
1216 print_char('|');
1217 short_display(vlink(post_break(p)));
1218 print_char('|');
1219 } else {
1220 /*tex Print a short indication of the contents of node |p| */
1221 print_short_node_contents(p);
1222 }
1223 }
1224 p = vlink(p);
1225 if (p == null)
1226 return;
1227 }
1228 update_terminal();
1229 }
1230
1231 /*tex
1232
1233 When debugging a macro package, it can be useful to see the exact control
1234 sequence names in the format file. For example, if ten new csnames appear, it's
1235 nice to know what they are, to help pinpoint where they came from. (This isn't a
1236 truly ``basic'' printing procedure, but that's a convenient module in which to
1237 put it.)
1238
1239 */
1240
1241 void print_csnames(int hstart, int hfinish)
1242 {
1243 int h;
1244 unsigned char *c, *l;
1245 fprintf(stderr, "fmtdebug:csnames from %d to %d:", (int) hstart, (int) hfinish);
1246 for (h = hstart; h <= hfinish; h++) {
1247 if (cs_text(h) > 0) {
1248 /*tex We have anything at this position. */
1249 c = str_string(cs_text(h));
1250 l = c + str_length(cs_text(h));
1251 while (c < l) {
1252 /*tex Print the characters. */
1253 fputc(*c++, stderr);
1254 }
1255 fprintf(stderr, "|");
1256 }
1257 }
1258 }
1259
1260 /*tex
1261
1262 A helper for printing file:line:error style messages. Look for a filename in
1263 |full_source_filename_stack|, and if we fail to find one fall back on the
1264 non-file:line:error style.
1265
1266 */
1267
1268 void print_file_line(void)
1269 {
1270 int level = in_open;
1271 while ((level > 0) && (full_source_filename_stack[level] == 0))
1272 decr(level);
1273 if (level == 0) {
1274 tprint_nl("! ");
1275 } else {
1276 tprint_nl("");
1277 tprint(full_source_filename_stack[level]);
1278 print_char(':');
1279 if (level == in_open)
1280 print_int(line);
1281 else
1282 print_int(line_stack[level + 1]);
1283 tprint(": ");
1284 }
1285 }
1286
1287 /*tex
1288
1289 \TeX\ is occasionally supposed to print diagnostic information that goes only
1290 into the transcript file, unless |tracing_online| is positive. Here are two
1291 routines that adjust the destination of print commands:
1292
1293 */
1294
1295 void begin_diagnostic(void)
1296 {
1297 global_old_setting = selector;
1298 if ((tracing_online_par <= 0) && (selector == term_and_log)) {
1299 decr(selector);
1300 if (history == spotless)
1301 history = warning_issued;
1302 }
1303 }
1304
1305 /*tex
1306
1307 Restore proper conditions after tracing.
1308
1309 */
1310
1311 void end_diagnostic(boolean blank_line)
1312 {
1313 tprint_nl("");
1314 if (blank_line)
1315 print_ln();
1316 selector = global_old_setting;
1317 }
1318
1319 /*
1320 Indentation (based on a suggestion by PO on tex-implementors but adapted to
1321 out situation and with built-in limits and so.
1322 It's possible to add an extra newline with texconfig.trace_extra_newline = 1
1323 */
1324
1325 void print_input_level(void)
1326 {
1327 int callback_id = callback_defined(input_level_string_callback);
1328 if (callback_id>0) {
1329 char *s = NULL;
1330 if (run_callback(callback_id, "d->S", input_ptr, &s)) {
1331 if (s && strlen(s) > 0) {
1332 tprint_nl(s);
1333 free(s);
1334 } else {
1335 if (traceextranewline)
1336 print_ln();
1337 }
1338 } else {
1339 if (traceextranewline)
1340 print_ln();
1341 }
1342 } else {
1343 int m = level_max;
1344 if (m) {
1345 int l = input_ptr;
1346 int c = level_chr > 0 ? level_chr : '.';
1347 if (l > m) {
1348 tprint_nl("[");
1349 print_int((l/m)*m);
1350 print(']');
1351 l = l % m;
1352 } else {
1353 if (traceextranewline)
1354 print_ln();
1355 }
1356 while (l > 0) {
1357 print(c);
1358 l--;
1359 }
1360 } else {
1361 if (traceextranewline)
1362 print_ln();
1363 }
1364 }
1365 }
1366
1367 /*tex
1368
1369 Of course we had better declare another global variable, if the previous routines
1370 are going to work.
1371
1372 */
1373
1374 int global_old_setting;