File: printing.w

package info (click to toggle)
texlive-bin 2018.20181218.49446-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 186,920 kB
  • sloc: ansic: 873,264; cpp: 311,278; perl: 82,918; sh: 23,243; makefile: 8,590; lex: 4,939; python: 4,462; pascal: 3,813; java: 3,569; yacc: 2,901; tcl: 2,379; exp: 2,031; xml: 844; ruby: 678; lisp: 398; sed: 331; asm: 140; csh: 46; awk: 30
file content (1135 lines) | stat: -rw-r--r-- 33,235 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
% printing.w
%
% Copyright 2009-2013 Taco Hoekwater <taco@@luatex.org>
%
% This file is part of LuaTeX.
%
% LuaTeX is free software; you can redistribute it and/or modify it under
% the terms of the GNU General Public License as published by the Free
% Software Foundation; either version 2 of the License, or (at your
% option) any later version.
%
% LuaTeX is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
% License for more details.
%
% You should have received a copy of the GNU General Public License along
% with LuaTeX; if not, see <http://www.gnu.org/licenses/>.

@ @c
#include "ptexlib.h"
#include "lua/luatex-api.h" /* for luatex_banner */

@ @c
#define wlog(A)  fputc(A,log_file)
#define wterm(A) fputc(A,term_out)

int new_string_line = 0;
int escape_controls = 1;

@ Messages that are sent to a user's terminal and to the transcript-log file
are produced by several `|print|' procedures. These procedures will
direct their output to a variety of places, based on the setting of
the global variable |selector|, which has the following possible
values:

\yskip
\hang |term_and_log|, the normal setting, prints on the terminal and on the
  transcript file.

\hang |log_only|, prints only on the transcript file.

\hang |term_only|, prints only on the terminal.

\hang |no_print|, doesn't print at all. This is used only in rare cases
  before the transcript file is open.

\hang |pseudo|, puts output into a cyclic buffer that is used
  by the |show_context| routine; when we get to that routine we shall discuss
  the reasoning behind this curious mode.

\hang |new_string|, appends the output to the current string in the
  string pool.

\hang 0 to 15, prints on one of the sixteen files for \.{\\write} output.

\yskip
\noindent The symbolic names `|term_and_log|', etc., have been assigned
numeric codes that satisfy the convenient relations |no_print+1=term_only|,
|no_print+2=log_only|, |term_only+2=log_only+1=term_and_log|.

Three additional global variables, |tally| and |term_offset| and
|file_offset|, record the number of characters that have been printed
since they were most recently cleared to zero. We use |tally| to record
the length of (possibly very long) stretches of printing; |term_offset|
and |file_offset|, on the other hand, keep track of how many characters
have appeared so far on the current line that has been output to the
terminal or to the transcript file, respectively.

@c
alpha_file log_file;                /* transcript of \TeX\ session */
int selector = term_only;           /* where to print a message */
int dig[23];                        /* digits in a number being output */
int tally = 0;                      /* the number of characters recently printed */
int term_offset = 0;                /* the number of characters on the current terminal line */
int file_offset = 0;                /* the number of characters on the current file line */
packed_ASCII_code trick_buf[(ssup_error_line + 1)]; /* circular buffer for pseudoprinting */
int trick_count;                    /* threshold for pseudoprinting, explained later */
int first_count;                    /* another variable for pseudoprinting */
boolean inhibit_par_tokens = false; /*  for minor adjustments to |show_token_list|  */

@ To end a line of text output, we call |print_ln|

@c
void print_ln(void)
{
    switch (selector) {
        case no_print:
            break;
        case term_only:
            wterm_cr();
            term_offset = 0;
            break;
        case log_only:
            wlog_cr();
            file_offset = 0;
            break;
        case term_and_log:
            wterm_cr();
            wlog_cr();
            term_offset = 0;
            file_offset = 0;
            break;
        case pseudo:
            break;
        case new_string:
            if (new_string_line > 0)
                print_char(new_string_line);
            break;
        default:
            fprintf(write_file[selector], "\n");
            break;
    }
    /* |tally| is not affected */
}

@ The |print_char| procedure sends one byte to the desired destination.
All printing comes through |print_ln| or |print_char|, except for the
case of |tprint| (see below).

The checking of the line length is an inheritance from previosu engines
and we might drop it after release 1.0. We're not too picky about the exact
match of that length because we have utf output so length is then a bit
fuzzy anyway.

@c
#define needs_escaping(A) \
    ((! escape_controls) || (A>=0x20) || (A==0x0A) || (A==0x0D) || (A==0x09))

#define escaped_char(A) \
    A+64

#define wterm_char(A) \
    if (needs_escaping(A)) { \
        wterm(A); \
    } else { \
        if (term_offset+2>=max_print_line) { \
            wterm_cr(); \
            term_offset=0; \
        } \
        wterm('^'); \
        wterm('^'); \
        wterm(escaped_char(A)); \
        term_offset += 2; \
    }

/*

#define needs_wrapping(A,B) \
  (((A>=0xF0)&&(B+4>=max_print_line)) || \
   ((A>=0xE0)&&(B+3>=max_print_line)) || \
   ((A>=0xC0)&&(B+2>=max_print_line)))

we have mostly ascii in logs, so ...

*/

#define needs_wrapping(A,B) \
  (   (A>=0xC0) && \
    (((A>=0xF0) && (B+4>=max_print_line)) || \
     ((A>=0xE0) && (B+3>=max_print_line)) || \
     (             (B+2>=max_print_line))) \
  )

#define fix_term_offset(A) \
    if (needs_wrapping(A,term_offset)){ \
        wterm_cr(); \
        term_offset=0; \
    }

#define fix_log_offset(A) \
    if (needs_wrapping(A,file_offset)){ \
        wlog_cr(); \
        file_offset=0; \
    }

void print_char(int s)
{
    if (s < 0 || s > 255) {
        formatted_warning("print","weird character %i",s);
        return;
    }
    if (s == new_line_char_par) {
        if (selector < pseudo) {
            print_ln();
            return;
        }
    }
    switch (selector) {
        case no_print:
            break;
        case term_only:
            fix_term_offset(s);
            wterm_char(s);
            incr(term_offset);
            if (term_offset == max_print_line) {
                wterm_cr();
                term_offset = 0;
            }
            break;
        case log_only:
            fix_log_offset(s);
            wlog(s);
            incr(file_offset);
            if (file_offset == max_print_line) {
                wlog_cr();
                file_offset = 0;
            }
            break;
        case term_and_log:
            fix_term_offset(s);
            fix_log_offset(s);
            wterm_char(s);
            wlog(s);
            incr(term_offset);
            incr(file_offset);
            if (term_offset == max_print_line) {
                wterm_cr();
                term_offset = 0;
            }
            if (file_offset == max_print_line) {
                wlog_cr();
                file_offset = 0;
            }
            break;
        case pseudo:
            if (tally < trick_count)
                trick_buf[tally % error_line] = (packed_ASCII_code) s;
            break;
        case new_string:
            append_char(s);
            break;
        default:
            fprintf(write_file[selector], "%c", s);
    }
    incr(tally);
}

@ An entire string is output by calling |print|. Note that if we are outputting
the single standard ASCII character \.c, we could call |print("c")|, since
|"c"=99| is the number of a single-character string, as explained above. But
|print_char("c")| is quicker, so \TeX\ goes directly to the |print_char|
routine when it knows that this is safe. (The present implementation
assumes that it is always safe to print a visible ASCII character.)

@^system dependencies@>

The first 256 entries above the 17th unicode plane are used for a
special trick: when \TeX\ has to print items in that range, it will
instead print the character that results from substracting 0x110000
from that value. This allows byte-oriented output to things like
\.{\\specials} and \.{\\pdfextension literals}. Todo: Perhaps it would be useful
to do the same substraction while typesetting.

@c
void print(int s)
{
    if (s >= str_ptr) {
        normal_warning("print","bad string pointer");
        return;
    } else if (s < STRING_OFFSET) {
        if (s < 0) {
            normal_warning("print","bad string offset");
        } else {
            /* TH not sure about this, disabled for now! */
            if ((false) && (selector > pseudo)) {
                /* internal strings are not expanded */
                print_char(s);
                return;
            }
            if (s == new_line_char_par) {
                if (selector < pseudo) {
                    print_ln();
                    return;
                }
            }
            if (s <= 0x7F) {
                print_char(s);
            } else if (s <= 0x7FF) {
                print_char(0xC0 + (s / 0x40));
                print_char(0x80 + (s % 0x40));
            } else if (s <= 0xFFFF) {
                print_char(0xE0 + (s / 0x1000));
                print_char(0x80 + ((s % 0x1000) / 0x40));
                print_char(0x80 + ((s % 0x1000) % 0x40));
            } else if (s >= 0x110000) {
                int c = s - 0x110000;
                if (c >= 256) {
                    formatted_warning("print", "bad raw byte to print (c=%d), skipped",c);
                } else {
                    print_char(c);
                }
            } else {
                print_char(0xF0 + (s / 0x40000));
                print_char(0x80 + ((s % 0x40000) / 0x1000));
                print_char(0x80 + (((s % 0x40000) % 0x1000) / 0x40));
                print_char(0x80 + (((s % 0x40000) % 0x1000) % 0x40));
            }
        }
        return;
    }
    if (selector == new_string) {
        append_string(str_string(s), (unsigned) str_length(s));
        return;
    }
    lprint(&str_lstring(s));
}

void lprint (lstring *ss) {
    unsigned char *j, *l;       /* current character code position */
    j = ss->s;
    l = j + ss->l;
    while (j < l) {
        /* 0x110000 in utf=8: 0xF4 0x90 0x80 0x80  */
        /* I don't bother checking the last two bytes explicitly */
        if ((j < l - 4) && (*j == 0xF4) && (*(j + 1) == 0x90)) {
            int c = (*(j + 2) - 128) * 64 + (*(j + 3) - 128);
            assert(c >= 0 && c < 256);
            print_char(c);
            j = j + 4;
        } else {
            print_char(*j);
            incr(j);
        }
    }
}

@ The procedure |print_nl| is like |print|, but it makes sure that the
string appears at the beginning of a new line.

@c
void print_nlp(void)
{                               /* move to beginning of a line */
    if (new_string_line > 0) {
        print_char(new_string_line);
    } else if (((term_offset > 0) && (odd(selector))) ||
               ((file_offset > 0) && (selector >= log_only))) {
        print_ln();
    }
}

void print_nl(str_number s)
{                               /* prints string |s| at beginning of line */
    print_nlp();
    print(s);
}

@ |char *| versions of the same procedures. |tprint| is
different because it uses buffering, which works well because
most of the output actually comes through |tprint|.

@c
#define t_flush_buffer(target,offset) \
    buffer[i++] = '\n'; \
    buffer[i++] = '\0';\
    fputs(buffer, target); \
    i = 0; \
    buffer[0] = '\0'; \
    offset=0;

void tprint(const char *sss)
{
    char *buffer = NULL;
    int i = 0; /* buffer index */
    int newlinechar = new_line_char_par;
    int dolog = 0;
    int doterm = 0;
    switch (selector) {
        case no_print:
            return;
            break;
        case term_only:
            doterm = 1;
            break;
        case log_only:
            dolog = 1;
            break;
        case term_and_log:
            dolog = 1;
            doterm = 1;
            break;
        case pseudo:
            while (*sss) {
               if (tally < trick_count) {
                   trick_buf[tally % error_line] = (packed_ASCII_code) *sss++;
               tally++;
               } else {
                   return;
               }
            }
            return;
            break;
        case new_string:
            append_string((const unsigned char *)sss, (unsigned) strlen(sss));
            return;
            break;
        default:
            {
                char *newstr = xstrdup(sss);
                char *s;
                for (s=newstr;*s;s++) {
                    if (*s == newlinechar) {
                        *s = '\n';
                    }
                }
                fputs(newstr, write_file[selector]);
                free(newstr);
                return;
            }
            break;
    }
    /* what is left is the 3 term/log settings */
    if (dolog || doterm) {
        buffer = xmalloc(strlen(sss)*3);
        if (dolog) {
            const unsigned char *ss = (const unsigned char *) sss;
            while (*ss) {
                int s = *ss++;
                if (needs_wrapping(s,file_offset) || s == newlinechar) {
                    t_flush_buffer(log_file,file_offset);
                }
                if (s != newlinechar) {
                    buffer[i++] = s;
                    if (file_offset++ == max_print_line) {
                        t_flush_buffer(log_file,file_offset);
                    }
                }
            }
            if (*buffer) {
                buffer[i++] = '\0';
                fputs(buffer, log_file);
                buffer[0] = '\0';
            }
            i = 0;
        }
        if (doterm) {
            const unsigned char *ss = (const unsigned char *) sss;
            while (*ss) {
                int s = *ss++;
                if (needs_wrapping(s,term_offset) || s == newlinechar) {
                    t_flush_buffer(term_out,term_offset);
                }
                if (s != newlinechar) {
                    if (needs_escaping(s)) {
                        buffer[i++] = s;
                    } else {
                        buffer[i++] = '^';
                        buffer[i++] = '^';
                        buffer[i++] = escaped_char(s);
                        term_offset += 2;
                    }
                    if (++term_offset == max_print_line) {
                        t_flush_buffer(term_out,term_offset);
                    }
                }
            }
            if (*buffer) {
                buffer[i++] = '\0';
                fputs(buffer, term_out);
            }
        }
        free(buffer);
    }
}

void tprint_nl(const char *s)
{
    print_nlp();
    tprint(s);
}

@ Here is the very first thing that \TeX\ prints: a headline that identifies
the version number and format package. The |term_offset| variable is temporarily
incorrect, but the discrepancy is not serious since we assume that the banner
and format identifier together will occupy at most |max_print_line|
character positions.

@c
void print_banner(const char *v)
{
    int callback_id = callback_defined(start_run_callback);
    if (callback_id == 0) {
        fprintf(term_out, "This is " MyName ", Version %s%s ", v, WEB2CVERSION);
        if (format_ident > 0)
            print(format_ident);
        print_ln();
        if (show_luahashchars){
            wterm(' ');
            fprintf(term_out,"Number of bits used by the hash function (" my_name "): %d",LUAI_HASHLIMIT);
        print_ln();
        }
        if (shellenabledp) {
            wterm(' ');
            if (restrictedshell)
                fprintf(term_out, "restricted ");
            fprintf(term_out, "system commands enabled.\n");
        }
    } else if (callback_id > 0) {
        run_callback(callback_id, "->");
    }
}

@ @c
void log_banner(const char *v)
{
    const char *months[] = { "   ",
        "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
        "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
    };
    unsigned month = (unsigned) month_par;
    if (month > 12)
        month = 0;
    fprintf(log_file, "This is " MyName ", Version %s%s ", v, WEB2CVERSION);
    print(format_ident);
    print_char(' ');
    print_char(' ');
    print_int(day_par);
    print_char(' ');
    fprintf(log_file, "%s", months[month]);
    print_char(' ');
    print_int(year_par);
    print_char(' ');
    print_two(time_par / 60);
    print_char(':');
    print_two(time_par % 60);
    if (shellenabledp) {
        wlog_cr();
        wlog(' ');
        if (restrictedshell)
            fprintf(log_file, "restricted ");
        fprintf(log_file, "system commands enabled.");
    }
    if (filelineerrorstylep) {
        wlog_cr();
        fprintf(log_file, " file:line:error style messages enabled.");
    }
}

@ @c
void print_version_banner(void)
{
    fprintf(term_out, "%s", luatex_banner);
}

@ The procedure |print_esc| prints a string that is preceded by
the user's escape character (which is usually a backslash).

@c
void print_esc(str_number s)
{
    int c = escape_char_par; /* Set variable |c| to the current escape character */
    if (c >= 0 && c < STRING_OFFSET)
        print(c);
    print(s);
}

@ This prints escape character, then |s|.

@c
void tprint_esc(const char *s)
{
    int c = escape_char_par; /* Set variable |c| to the current escape character */
    if (c >= 0 && c < STRING_OFFSET)
        print(c);
    tprint(s);
}

@ An array of digits in the range |0..15| is printed by |print_the_digs|.

@c
void print_the_digs(eight_bits k)
{
    /* prints |dig[k-1]|$\,\ldots\,$|dig[0]| */
    while (k-- > 0) {
        if (dig[k] < 10)
            print_char('0' + dig[k]);
        else
            print_char('A' - 10 + dig[k]);
    }
}

@ The following procedure, which prints out the decimal representation of a
given integer |n|, has been written carefully so that it works properly
if |n=0| or if |(-n)| would cause overflow. It does not apply |mod| or |div|
to negative arguments, since such operations are not implemented consistently
by all PASCAL compilers.

@c
void print_int(longinteger n)
{
    int k = 0;     /* index to current digit; we assume that $|n|<10^{23}$ */
    longinteger m; /* used to negate |n| in possibly dangerous cases */
    if (n < 0) {
        print_char('-');
        if (n > -100000000) {
            n = -n;
        } else {
            m = -1 - n;
            n = m / 10;
            m = (m % 10) + 1;
            k = 1;
            if (m < 10)
                dig[0] = (int) m;
            else {
                dig[0] = 0;
                incr(n);
            }
        }
    }
    do {
        dig[k] = (int) (n % 10);
        n = n / 10;
        incr(k);
    } while (n != 0);
    print_the_digs((eight_bits) k);
}


@ Here is a trivial procedure to print two digits; it is usually called with
a parameter in the range |0<=n<=99|.

@c
void print_two(int n)
{
    n = abs(n) % 100;
    print_char('0' + (n / 10));
    print_char('0' + (n % 10));
}

@ Hexadecimal printing of nonnegative integers is accomplished by |print_hex|.

@c
void print_hex(int n)
{
    int k = 0 ; /* index to current digit; we assume that $0\L n<16^{22}$ */
    print_char('"');
    do {
        dig[k] = n % 16;
        n = n / 16;
        incr(k);
    } while (n != 0);
    print_the_digs((eight_bits) k);
}

@ Roman numerals are produced by the |print_roman_int| routine.  Readers
who like puzzles might enjoy trying to figure out how this tricky code
works; therefore no explanation will be given. Notice that 1990 yields
\.{mcmxc}, not \.{mxm}.

@c
void print_roman_int(int n)
{
    char *j, *k; /* mysterious indices */
    int u, v;    /* mysterious numbers */
    char mystery[] = "m2d5c2l5x2v5i";
    j = (char *) mystery;
    v = 1000;
    while (1) {
        while (n >= v) {
            print_char(*j);
            n = n - v;
        }
        if (n <= 0) {
            /* nonpositive input produces no output */
            return;
        }
        k = j + 2;
        u = v / (*(k - 1) - '0');
        if (*(k - 1) == '2') {
            k = k + 2;
            u = u / (*(k - 1) - '0');
        }
        if (n + u >= v) {
            print_char(*k);
            n = n + u;
        } else {
            j = j + 2;
            v = v / (*(j - 1) - '0');
        }
    }
}

@ The |print| subroutine will not print a string that is still being
created. The following procedure will.

@c
void print_current_string(void)
{
    unsigned j = 0; /* points to current character code */
    while (j < cur_length)
        print_char(cur_string[j++]);
}

@ The procedure |print_cs| prints the name of a control sequence, given
a pointer to its address in |eqtb|. A space is printed after the name
unless it is a single nonletter or an active character. This procedure
might be invoked with invalid data, so it is ``extra robust.'' The
individual characters must be printed one at a time using |print|, since
they may be unprintable.

@c
void print_cs(int p)
{
    str_number t = cs_text(p);
    if (p < hash_base) {
        /* nullcs */
        if (p == null_cs) {
            tprint_esc("csname");
            tprint_esc("endcsname");
            print_char(' ');
        } else {
            tprint_esc("IMPOSSIBLE.");
        }
    } else if ((p >= undefined_control_sequence) &&
               ((p <= eqtb_size) || p > eqtb_size + hash_extra)) {
        tprint_esc("IMPOSSIBLE.");
    } else if (t >= str_ptr) {
        tprint_esc("NONEXISTENT.");
    } else {
        if (is_active_cs(t)) {
            print(active_cs_value(t));
        } else {
            print_esc(t);
            if (single_letter(t)) {
                if (get_cat_code(cat_code_table_par, pool_to_unichar(str_string(t))) == letter_cmd)
                    print_char(' ');
            } else {
                print_char(' ');
            }
        }
    }
}

@ Here is a similar procedure; it avoids the error checks, and it never
prints a space after the control sequence.

@c
void sprint_cs(pointer p)
{
    str_number t;
    if (p == null_cs) {
        tprint_esc("csname");
        tprint_esc("endcsname");
    } else {
        t = cs_text(p);
        if (is_active_cs(t))
            print(active_cs_value(t));
        else
            print_esc(t);
    }
}

void sprint_cs_name(pointer p)
{
    str_number t;
    if (p != null_cs) {
        t = cs_text(p);
        if (is_active_cs(t))
            print(active_cs_value(t));
        else
            print(t);
    }
}

@ This procedure is never called when |interaction<scroll_mode|.

@c
void prompt_input(const char *s)
{
    wake_up_terminal();
    tprint(s);
    term_input();
}

@ Then there is a subroutine that prints glue stretch and shrink, possibly
followed by the name of finite units:

@c
void print_glue(scaled d, int order, const char *s)
{
    print_scaled(d);
    if ((order < normal) || (order > filll)) {
        tprint("foul");
    } else if (order > normal) {
        tprint("fi");
        while (order > sfi) {
            print_char('l');
            decr(order);
        }
    } else if (s != NULL) {
        tprint(s);
    }
}

@ The next subroutine prints a whole glue specification

@c
void print_spec(int p, const char *s)
{
    if (p < 0) {
        print_char('*');
    } else {
        print_scaled(width(p));
        if (s != NULL)
            tprint(s);
        if (stretch(p) != 0) {
            tprint(" plus ");
            print_glue(stretch(p), stretch_order(p), s);
        }
        if (shrink(p) != 0) {
            tprint(" minus ");
            print_glue(shrink(p), shrink_order(p), s);
        }
    }
}

@ We can reinforce our knowledge of the data structures just introduced
by considering two procedures that display a list in symbolic form.
The first of these, called |short_display|, is used in ``overfull box''
messages to give the top-level description of a list. The other one,
called |show_node_list|, prints a detailed description of exactly what
is in the data structure.

The philosophy of |short_display| is to ignore the fine points about exactly
what is inside boxes, except that ligatures and discretionary breaks are
expanded. As a result, |short_display| is a recursive procedure, but the
recursion is never more than one level deep.
@^recursion@>

A global variable |font_in_short_display| keeps track of the font code that
is assumed to be present when |short_display| begins; deviations from this
font will be printed.

@c
int font_in_short_display; /* an internal font number */

@ Boxes, rules, inserts, whatsits, marks, and things in general that are
sort of ``complicated'' are indicated only by printing `\.{[]}'.

@c

/*
So, 0, 1 as well as any large value will behave the same as before. The reason
for this extension is that a \name not always makes sense.

0   \foo xyz
1   \foo (bar)
2   <bar> xyz
3   <bar @@ ..> xyz
4   <id>
5   <id: bar>
6   <id: bar @@ ..> xyz

*/

void print_font_identifier(internal_font_number f)
{
    str_number fonttext;
    fonttext = font_id_text(f);
    if (tracing_fonts_par >= 2 && tracing_fonts_par <= 6) {
        /* < > is less likely to clash with text parenthesis */
        tprint("<");
        if (tracing_fonts_par >= 2 && tracing_fonts_par <= 3) {
            print_font_name(f);
            if (tracing_fonts_par >= 3 || font_size(f) != font_dsize(f)) {
                tprint(" @@ ");
                print_scaled(font_size(f));
                tprint("pt");
            }
        } else if (tracing_fonts_par >= 4 && tracing_fonts_par <= 6) {
            print_int(f);
            if (tracing_fonts_par >= 5) {
                tprint(": ");
                print_font_name(f);
                if (tracing_fonts_par >= 6 || font_size(f) != font_dsize(f)) {
                    tprint(" @@ ");
                    print_scaled(font_size(f));
                    tprint("pt");
                }
            }
        }
        print_char('>');
    } else {
        /* old method, inherited from pdftex  */
        if (fonttext > 0) {
            print_esc(fonttext);
        } else {
            tprint_esc("FONT");
            print_int(f);
        }
        if (tracing_fonts_par > 0) {
            tprint(" (");
            print_font_name(f);
            if (font_size(f) != font_dsize(f)) {
                tprint("@@");
                print_scaled(font_size(f));
                tprint("pt");
            }
            print_char(')');
        }
    }
}

@ This prints highlights of list |p|.

@c
void short_display(int p)
{
    while (p != null) {
        if (is_char_node(p)) {
            if (lig_ptr(p) != null) {
                short_display(lig_ptr(p));
            } else {
                if (font(p) != font_in_short_display) {
                    if (!is_valid_font(font(p)))
                        print_char('*');
                    else
                        print_font_identifier(font(p));
                    print_char(' ');
                    font_in_short_display = font(p);
                }
                print(character(p));
            }
        } else {
            /* Print a short indication of the contents of node |p| */
            print_short_node_contents(p);
        }
        p = vlink(p);
    }
}

@ The |show_node_list| routine requires some auxiliary subroutines: one to
print a font-and-character combination, one to print a token list without
its reference count, and one to print a rule dimension.

@ This prints |char_node| data.

@c
void print_font_and_char(int p)
{
    if (!is_valid_font(font(p)))
        print_char('*');
    else
        print_font_identifier(font(p));
    print_char(' ');
    print(character(p));
}

@ This prints token list data in braces

@c
void print_mark(int p)
{
    print_char('{');
    if ((p < (int) fix_mem_min) || (p > (int) fix_mem_end))
        tprint_esc("CLOBBERED.");
    else
        show_token_list(token_link(p), null, max_print_line - 10);
    print_char('}');
}

@ This prints dimensions of a rule node.

@c
void print_rule_dimen(scaled d)
{
    if (is_running(d))
        print_char('*');
    else
        print_scaled(d);
}

@ Since boxes can be inside of boxes, |show_node_list| is inherently recursive,
@^recursion@>
up to a given maximum number of levels.  The history of nesting is indicated
by the current string, which will be printed at the beginning of each line;
the length of this string, namely |cur_length|, is the depth of nesting.

A global variable called |depth_threshold| is used to record the maximum
depth of nesting for which |show_node_list| will show information.  If we
have |depth_threshold=0|, for example, only the top level information will
be given and no sublists will be traversed. Another global variable, called
|breadth_max|, tells the maximum number of items to show at each level;
|breadth_max| had better be positive, or you won't see anything.

@c
int depth_threshold; /* maximum nesting depth in box displays */
int breadth_max;     /* maximum number of items shown at the same list level */

@ The recursive machinery is started by calling |show_box|. Assign the values
|depth_threshold:=show_box_depth| and |breadth_max:=show_box_breadth|

@c
void show_box(halfword p)
{
    depth_threshold = show_box_depth_par;
    breadth_max = show_box_breadth_par;
    if (breadth_max <= 0)
        breadth_max = 5;
    /* the show starts at |p| */
    show_node_list(p);
    print_ln();
}

@ Helper for debugging purposes. It prints highlights of list |p|

@c
void short_display_n(int p, int m)
{
    int i = 0;
    font_in_short_display = null_font;
    if (p == null)
        return;
    while (p != null) {
        if (is_char_node(p)) {
            if (p <= max_halfword) {
                if (font(p) != font_in_short_display) {
                    if (!is_valid_font(font(p)))
                        print_char('*');
                    else
                        print_font_identifier(font(p));
                    print_char(' ');
                    font_in_short_display = font(p);
                }
                print(character(p));
            }
        } else {
            if ( (type(p) == glue_node) ||
                 (type(p) == disc_node) ||
                 (type(p) == penalty_node) ||
                ((type(p) == kern_node) && (subtype(p) == explicit_kern ||
                                            subtype(p) == italic_kern   ))) {
                incr(i);
            }
            if (i >= m)
                return;
            if (type(p) == disc_node) {
                print_char('|');
                short_display(vlink(pre_break(p)));
                print_char('|');
                short_display(vlink(post_break(p)));
                print_char('|');
            } else {
                /* Print a short indication of the contents of node |p| */
                print_short_node_contents(p);
            }
        }
        p = vlink(p);
        if (p == null)
            return;
    }
    update_terminal();
}

@ When debugging a macro package, it can be useful to see the exact
control sequence names in the format file.  For example, if ten new
csnames appear, it's nice to know what they are, to help pinpoint where
they came from.  (This isn't a truly ``basic'' printing procedure, but
that's a convenient module in which to put it.)

@c
void print_csnames(int hstart, int hfinish)
{
    int h;
    unsigned char *c, *l;
    fprintf(stderr, "fmtdebug:csnames from %d to %d:", (int) hstart, (int) hfinish);
    for (h = hstart; h <= hfinish; h++) {
        if (cs_text(h) > 0) {
            /* we have anything at this position */
            c = str_string(cs_text(h));
            l = c + str_length(cs_text(h));
            while (c < l) {
                /* print the characters */
                fputc(*c++, stderr);
            }
            fprintf(stderr, "|");
        }
    }
}

@ A helper for printing file:line:error style messages.  Look for a
filename in |full_source_filename_stack|, and if we fail to find
one fall back on the non-file:line:error style.

@c
void print_file_line(void)
{
    int level = in_open;
    while ((level > 0) && (full_source_filename_stack[level] == 0))
        decr(level);
    if (level == 0) {
        tprint_nl("! ");
    } else {
        tprint_nl("");
        tprint(full_source_filename_stack[level]);
        print_char(':');
        if (level == in_open)
            print_int(line);
        else
            print_int(line_stack[level + 1]);
        tprint(": ");
    }
}

@ \TeX\ is occasionally supposed to print diagnostic information that
goes only into the transcript file, unless |tracing_online| is positive.
Here are two routines that adjust the destination of print commands:

@c
void begin_diagnostic(void)
{
    global_old_setting = selector;
    if ((tracing_online_par <= 0) && (selector == term_and_log)) {
        decr(selector);
        if (history == spotless)
            history = warning_issued;
    }
}

@ Restore proper conditions after tracing.

@c
void end_diagnostic(boolean blank_line)
{
    tprint_nl("");
    if (blank_line)
        print_ln();
    selector = global_old_setting;
}

@ Of course we had better declare another global variable, if the previous
routines are going to work.

@c
int global_old_setting;