Debian Codegraph @

texk/web2c/luatexdir/tex/maincontrol.c

raw
1 /*
2
3 maincontrol.w
4
5 Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
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 mode mode_par
28 #define tail tail_par
29 #define head head_par
30 #define dir_save dirs_par
31
32 /*tex
33
34 We come now to the |main_control| routine, which contains the master switch that
35 causes all the various pieces of \TeX\ to do their things, in the right order.
36
37 In a sense, this is the grand climax of the program: It applies all the tools
38 that we have worked so hard to construct. In another sense, this is the messiest
39 part of the program: It necessarily refers to other pieces of code all over the
40 place, so that a person can't fully understand what is going on without paging
41 back and forth to be reminded of conventions that are defined elsewhere. We are
42 now at the hub of the web, the central nervous system that touches most of the
43 other parts and ties them together. @^brain@>
44
45 The structure of |main_control| itself is quite simple. There's a label called
46 |big_switch|, at which point the next token of input is fetched using
47 |get_x_token|. Then the program branches at high speed into one of about 100
48 possible directions, based on the value of the current mode and the newly fetched
49 command code; the sum |abs(mode)+cur_cmd| indicates what to do next. For example,
50 the case `|vmode+letter|' arises when a letter occurs in vertical mode (or
51 internal vertical mode); this case leads to instructions that initialize a new
52 paragraph and enter horizontal mode.
53
54 The big |case| statement that contains this multiway switch has been labeled
55 |reswitch|, so that the program can |goto reswitch| when the next token has
56 already been fetched. Most of the cases are quite short; they call an ``action
57 procedure'' that does the work for that case, and then they either |goto
58 reswitch| or they ``fall through'' to the end of the |case| statement, which
59 returns control back to |big_switch|. Thus, |main_control| is not an extremely
60 large procedure, in spite of the multiplicity of things it must do; it is small
61 enough to be handled by PASCAL compilers that put severe restrictions on
62 procedure size. @!@^action procedure@>
63
64 One case is singled out for special treatment, because it accounts for most of
65 \TeX's activities in typical applications. The process of reading simple text and
66 converting it into |char_node| records, while looking for ligatures and kerns, is
67 part of \TeX's ``inner loop''; the whole program runs efficiently when its inner
68 loop is fast, so this part has been written with particular care.
69
70 We leave the |space_factor| unchanged if |sf_code(cur_chr)=0|; otherwise we set
71 it equal to |sf_code(cur_chr)|, except that it should never change from a value
72 less than 1000 to a value exceeding 1000. The most common case is
73 |sf_code(cur_chr)=1000|, so we want that case to be fast.
74
75 */
76
77 void adjust_space_factor(void)
78 {
79 halfword s = get_sf_code(cur_chr);
80 if (s == 1000) {
81 space_factor_par = 1000;
82 } else if (s < 1000) {
83 if (s > 0)
84 space_factor_par = s;
85 } else if (space_factor_par < 1000) {
86 space_factor_par = 1000;
87 } else {
88 space_factor_par = s;
89 }
90 }
91
92 /*tex
93
94 To handle the execution state of |main_control|'s eternal loop, an extra global
95 variable is used, along with a macro to define its values.
96
97 */
98
99 #define goto_next 0
100 #define goto_skip_token 1
101 #define goto_return 2
102
103 static int main_control_state;
104 static int local_level = 0;
105
106 /*tex
107
108 @* Main control helpers.
109
110 Here are all the functions that are called from |main_control| that
111 are not already defined elsewhere. For the moment, this list simply
112 in the order that the appear in |init_main_control|, below.
113
114 */
115
116 static void run_char_num (void) {
117 scan_char_num();
118 cur_chr = cur_val;
119 adjust_space_factor();
120 tail_append(new_char(cur_font_par, cur_chr));
121 }
122
123 static void run_char (void) {
124 adjust_space_factor();
125 tail_append(new_char(cur_font_par, cur_chr));
126 }
127
128 static void run_node (void) {
129 halfword n = cur_chr;
130 if (copy_lua_input_nodes_par) {
131 n = copy_node_list(n);
132 }
133 tail_append(n);
134 if (nodetype_has_attributes(type(n)) && node_attr(n) == null) {
135 build_attribute_list(n);
136 }
137 while (vlink(n) != null) {
138 n = vlink(n);
139 tail_append(n);
140 if (nodetype_has_attributes(type(n)) && node_attr(n) == null) {
141 build_attribute_list(n);
142 }
143 }
144 }
145
146 static void run_lua_call(void) {
147 if (cur_chr <= 0) {
148 normal_error("luacall", "invalid number");
149 } else {
150 str_number u = save_cur_string();
151 luacstrings = 0;
152 luafunctioncall(cur_chr);
153 restore_cur_string(u);
154 if (luacstrings > 0)
155 lua_string_start();
156 }
157 }
158
159 /*tex
160
161 The occurrence of blank spaces is almost part of \TeX's inner loop, since we
162 usually encounter about one space for every five non-blank characters. Therefore
163 |main_control| gives second-highest priority to ordinary spaces.
164
165 When a glue parameter like \.{\\spaceskip} is set to `\.{0pt}', we will see to it
166 later that the corresponding glue specification is precisely |zero_glue|, not
167 merely a pointer to some specification that happens to be full of zeroes.
168 Therefore it is simple to test whether a glue parameter is zero or~not.
169
170 */
171
172 static void run_app_space (void) {
173 halfword p; /* was a global temp_ptr */
174 int method = disable_space_par ;
175 if (method == 1) {
176 /*tex Don't inject anything, not even zero skip. */
177 } else if (method == 2) {
178 p = new_glue(zero_glue);
179 couple_nodes(tail,p);
180 tail = p;
181 } else if ((abs(mode) + cur_cmd == hmode + spacer_cmd) && (!(space_factor_par == 1000))) {
182 app_space();
183 } else {
184 /*tex Append a normal inter-word space to the current list. */
185 if (glue_is_zero(space_skip_par)) {
186 /*tex Find the glue specification for text spaces in the current font. */
187 p = new_glue(zero_glue);
188 width(p) = space(cur_font_par);
189 stretch(p) = space_stretch(cur_font_par);
190 shrink(p) = space_shrink(cur_font_par);
191
192 } else {
193 p = new_param_glue(space_skip_code);
194 }
195 /*tex So from now we have a subtype with spaces: */
196 subtype(p) = space_skip_code + 1 ;
197 couple_nodes(tail,p);
198 tail = p;
199 }
200 }
201
202 /*tex
203
204 Append a |boundary_node|
205
206 */
207
208 static void run_boundary (void) {
209 halfword n ;
210 n = new_node(boundary_node,cur_chr);
211 if ((cur_chr == 1) || (cur_chr == 2) ) {
212 /*tex We expect a user boundary or protrusion boundary. */
213 scan_int();
214 boundary_value(n) = cur_val;
215 }
216 couple_nodes(tail, n);
217 tail = n;
218 }
219
220 static void run_char_ghost (void) {
221 int t;
222 t = cur_chr;
223 get_x_token();
224 if ((cur_cmd == letter_cmd) || (cur_cmd == other_char_cmd)
225 || (cur_cmd == char_given_cmd) || (cur_cmd == char_num_cmd)) {
226 halfword p = new_glyph(get_cur_font(), cur_chr);
227 if (t == 0) {
228 set_is_leftghost(p);
229 } else {
230 set_is_rightghost(p);
231 }
232 tail_append(p);
233 }
234 }
235
236 static void run_relax (void) {
237 return;
238 }
239
240 /*tex
241
242 |ignore_spaces| is a special case: after it has acted, |get_x_token| has already
243 fetched the next token from the input, so that operation in |main_control| should
244 be skipped.
245
246 */
247
248 static void run_ignore_spaces (void) {
249 if (cur_chr == 0) {
250 /*tex Get the next non-blank non-call... */
251 do {
252 get_x_token();
253 } while (cur_cmd == spacer_cmd);
254 main_control_state = goto_skip_token;
255 } else {
256 int t = scanner_status;
257 scanner_status = normal;
258 get_next();
259 scanner_status = t;
260 cur_cs = prim_lookup(cs_text(cur_cs));
261 if (cur_cs != undefined_primitive) {
262 cur_cmd = get_prim_eq_type(cur_cs);
263 cur_chr = get_prim_equiv(cur_cs);
264 cur_tok = (cur_cmd * STRING_OFFSET) + cur_chr;
265 main_control_state = goto_skip_token;
266 }
267 }
268 }
269
270 /*tex
271
272 |stop| is the second special case. We want |main_control| to return to its caller
273 if there is nothing left to do.
274
275 */
276
277 static void run_stop (void) {
278 if (its_all_over()) {
279 /*tex this is the only way out */
280 main_control_state= goto_return;
281 }
282 }
283
284 static void run_non_math_math (void) {
285 back_input();
286 new_graf(true);
287 }
288
289 /*tex
290
291 We build up an argument to |set_math_char|:
292
293 */
294
295 static void run_math_char_num (void) {
296 mathcodeval mval;
297 if (cur_chr == 0)
298 mval = scan_mathchar(tex_mathcode);
299 else if (cur_chr == 1)
300 mval = scan_mathchar(umath_mathcode);
301 else
302 mval = scan_mathchar(umathnum_mathcode);
303 math_char_in_text(mval);
304 }
305
306 /*tex
307
308 We build up an argument to |set_math_char|:
309 */
310
311 static void run_math_given (void) {
312 mathcodeval mval;
313 mval = mathchar_from_integer(cur_chr, tex_mathcode);
314 math_char_in_text(mval);
315 }
316
317 /*tex
318
319 We build up an argument to |set_math_char| the \LUATEX\ way:
320 */
321
322 static void run_xmath_given (void) {
323 mathcodeval mval;
324 mval = mathchar_from_integer(cur_chr, umath_mathcode);
325 math_char_in_text(mval);
326 }
327
328 /*tex
329
330 The most important parts of |main_control| are concerned with \TeX's chief
331 mission of box-making. We need to control the activities that put entries on
332 vlists and hlists, as well as the activities that convert those lists into boxes.
333 All of the necessary machinery has already been developed; it remains for us to
334 ``push the buttons'' at the right times.
335
336 As an introduction to these routines, let's consider one of the simplest cases:
337 What happens when `\.{\\hrule}' occurs in vertical mode, or `\.{\\vrule}' in
338 horizontal mode or math mode? The code in |main_control| is short, since the
339 |scan_rule_spec| routine already does most of what is required; thus, there is no
340 need for a special action procedure.
341
342 Note that baselineskip calculations are disabled after a rule in vertical mode,
343 by setting |prev_depth:=ignore_depth|.
344
345 */
346
347 static void run_rule (void) {
348 tail_append(scan_rule_spec());
349 if (abs(mode) == vmode)
350 prev_depth_par = ignore_depth;
351 else if (abs(mode) == hmode)
352 space_factor_par = 1000;
353 }
354
355 /*tex
356
357 Many of the actions related to box-making are triggered by the appearance of
358 braces in the input. For example, when the user says `\.{\\hbox} \.{to}
359 \.{100pt\{$\langle\,\hbox{hlist}\,\rangle$\}}' in vertical mode, the information
360 about the box size (100pt, |exactly|) is put onto |save_stack| with a level
361 boundary word just above it, and |cur_group:=adjusted_hbox_group|; \TeX\ enters
362 restricted horizontal mode to process the hlist. The right brace eventually
363 causes |save_stack| to be restored to its former state, at which time the
364 information about the box size (100pt, |exactly|) is available once again; a box
365 is packaged and we leave restricted horizontal mode, appending the new box to the
366 current list of the enclosing mode (in this case to the current list of vertical
367 mode), followed by any vertical adjustments that were removed from the box by
368 |hpack|.
369
370 The next few sections of the program are therefore concerned with the treatment
371 of left and right curly braces.
372
373 If a left brace occurs in the middle of a page or paragraph, it simply introduces
374 a new level of grouping, and the matching right brace will not have such a
375 drastic effect. Such grouping affects neither the mode nor the current list.
376
377 */
378
379 static void run_left_brace (void) {
380 new_save_level(simple_group);
381 eq_word_define(int_base + no_local_whatsits_code, 0);
382 eq_word_define(int_base + no_local_dirs_code, 0);
383 }
384
385 static void run_begin_group (void) {
386 new_save_level(semi_simple_group);
387 eq_word_define(int_base + no_local_whatsits_code, 0);
388 eq_word_define(int_base + no_local_dirs_code, 0);
389 }
390
391 static void run_end_group (void) {
392 if (cur_group == semi_simple_group) {
393 fixup_directions();
394 } else {
395 off_save();
396 }
397 }
398
399 /*tex
400
401 Constructions that require a box are started by calling |scan_box| with a
402 specified context code. The |scan_box| routine verifies that a |make_box| command
403 comes next and then it calls |begin_box|.
404
405 */
406
407 static void run_move (void) {
408 int t = cur_chr;
409 scan_normal_dimen();
410 if (t == 0)
411 scan_box(cur_val);
412 else
413 scan_box(-cur_val);
414 }
415
416 static void run_leader_ship (void) {
417 scan_box(leader_flag - a_leaders + cur_chr);
418 }
419
420 static void run_make_box (void) {
421 begin_box(0);
422 }
423
424 static void run_box_dir (void) {
425 scan_register_num();
426 cur_box = box(cur_val);
427 scan_optional_equals();
428 scan_direction();
429 if (cur_box != null)
430 box_dir(cur_box) = cur_val;
431 }
432
433 static void run_box_direction (void) {
434 scan_register_num();
435 cur_box = box(cur_val);
436 scan_optional_equals();
437 scan_int();
438 check_dir_value(cur_val);
439 if (cur_box != null)
440 box_dir(cur_box) = cur_val;
441 }
442
443 /*tex
444
445 There is a really small patch to add a new primitive called \.{\\quitvmode}. In
446 vertical modes, it is identical to \.{\\indent}, but in horizontal and math modes
447 it is really a no-op (as opposed to \.{\\indent}, which executes the
448 |indent_in_hmode| procedure).
449
450 A paragraph begins when horizontal-mode material occurs in vertical mode, or when
451 the paragraph is explicitly started by `\.{\\quitvmode}', `\.{\\indent}' or
452 `\.{\\noindent}'.
453
454 */
455
456 static void run_start_par_vmode (void) {
457 new_graf((cur_chr > 0));
458 }
459
460 static void run_start_par (void) {
461 if (cur_chr != 2)
462 indent_in_hmode();
463 }
464
465 static void run_new_graf (void) {
466 /* the |partoken_context_code_par| branch is taken from pdftex as-is: */
467 if (cur_cmd == valign_cmd && partoken_context_code_par > 0 && mode == hmode) {
468 back_input();
469 cur_tok = par_token;
470 back_input();
471 token_type = inserted;
472 } else {
473 back_input();
474 new_graf(true);
475 }
476 }
477
478 /*tex
479
480 A paragraph ends when a |par_end| command is sensed, or when we are in horizontal
481 mode when reaching the right brace of vertical-mode routines like \.{\\vbox},
482 \.{\\insert}, or \.{\\output}.
483
484 */
485
486 static void run_par_end_vmode (void) {
487 normal_paragraph();
488 if (mode > 0) {
489 checked_page_filter(vmode_par);
490 build_page();
491 }
492 }
493
494 static void run_par_end_hmode (void) {
495 if (align_state < 0) {
496 /*tex This tries to recover from an alignment that didn't end properly. */
497 off_save();
498 }
499 /* This takes us to the enclosing mode, if |mode>0|. */
500 end_graf(bottom_level);
501 if (mode == vmode) {
502 checked_page_filter(hmode_par);
503 build_page();
504 }
505 }
506
507 static void append_italic_correction_mmode (void) {
508 tail_append(new_kern(0));
509 }
510
511 static void run_local_box (void) {
512 append_local_box(cur_chr);
513 }
514
515 static void run_halign_mmode (void) {
516 if (privileged()) {
517 if (cur_group == math_shift_group)
518 init_align();
519 else
520 off_save();
521 }
522 }
523
524 static void run_eq_no (void) {
525 if (privileged()) {
526 if (cur_group == math_shift_group)
527 start_eq_no();
528 else
529 off_save();
530 }
531 }
532
533 static void run_letter_mmode (void) {
534 set_math_char(get_math_code(cur_chr));
535 }
536
537 static void run_char_num_mmode (void) {
538 scan_char_num();
539 cur_chr = cur_val;
540 set_math_char(get_math_code(cur_chr));
541 }
542
543 static void run_math_char_num_mmode (void) {
544 mathcodeval mval;
545 if (cur_chr == 0)
546 mval = scan_mathchar(tex_mathcode);
547 else if (cur_chr == 1)
548 mval = scan_mathchar(umath_mathcode);
549 else
550 mval = scan_mathchar(umathnum_mathcode);
551 set_math_char(mval);
552 }
553
554 static void run_math_given_mmode (void) {
555 mathcodeval mval;
556 mval = mathchar_from_integer(cur_chr, tex_mathcode);
557 set_math_char(mval);
558 }
559
560 static void run_xmath_given_mmode (void) {
561 mathcodeval mval;
562 mval = mathchar_from_integer(cur_chr, umath_mathcode);
563 set_math_char(mval);
564 }
565
566 static void run_delim_num (void) {
567 mathcodeval mval;
568 if (cur_chr == 0)
569 mval = scan_delimiter_as_mathchar(tex_mathcode);
570 else
571 mval = scan_delimiter_as_mathchar(umath_mathcode);
572 set_math_char(mval);
573 }
574
575 static void run_vcenter (void) {
576 scan_spec(vcenter_group);
577 normal_paragraph();
578 push_nest();
579 mode = -vmode;
580 prev_depth_par = ignore_depth;
581 if (every_vbox_par != null)
582 begin_token_list(every_vbox_par, every_vbox_text);
583 }
584
585 static void run_math_style (void) {
586 tail_append(new_style((small_number) cur_chr));
587 }
588
589 static void run_non_script (void) {
590 tail_append(new_glue(zero_glue));
591 subtype(tail) = cond_math_glue;
592 }
593
594 static void run_math_choice (void) {
595 if (cur_chr == 0)
596 append_choices();
597 else
598 setup_math_style();
599 }
600
601 static void run_math_shift (void) {
602 if (cur_group == math_shift_group)
603 after_math();
604 else
605 off_save();
606 }
607
608 static void run_after_assignment (void) {
609 get_token();
610 after_token = cur_tok;
611 }
612
613 static void run_after_group (void) {
614 get_token();
615 save_for_after(cur_tok);
616 }
617
618 static void run_par_token (void) {
619 get_token();
620 if (cur_cs > 0) {
621 par_loc = cur_cs;
622 par_token = cur_tok;
623 }
624 }
625
626 static void run_extension (void) {
627 do_extension(0, 0);
628 }
629
630 static void run_normal (void) {
631 {
632 switch (cur_chr) {
633 case save_pos_code:
634 new_whatsit(save_pos_node);
635 break;
636 case save_cat_code_table_code:
637 scan_int();
638 if ((cur_val < 0) || (cur_val > 0x7FFF)) {
639 print_err("Invalid \\catcode table");
640 help1(
641 "All \\catcode table ids must be between 0 and 0x7FFF"
642 );
643 error();
644 } else {
645 if (cur_val == cat_code_table_par) {
646 print_err("Invalid \\catcode table");
647 help1(
648 "You cannot overwrite the current \\catcode table"
649 );
650 error();
651 } else {
652 copy_cat_codes(cat_code_table_par, cur_val);
653 }
654 }
655 break;
656 case init_cat_code_table_code:
657 scan_int();
658 if ((cur_val < 0) || (cur_val > 0x7FFF)) {
659 print_err("Invalid \\catcode table");
660 help1(
661 "All \\catcode table ids must be between 0 and 0x7FFF"
662 );
663 error();
664 } else {
665 if (cur_val == cat_code_table_par) {
666 print_err("Invalid \\catcode table");
667 help1(
668 "You cannot overwrite the current \\catcode table"
669 );
670 error();
671 } else {
672 initex_cat_codes(cur_val);
673 }
674 }
675 break;
676 case set_random_seed_code:
677 /* Negative random seed values are silently converted to positive ones */
678 scan_int();
679 if (cur_val < 0)
680 negate(cur_val);
681 random_seed = cur_val;
682 init_randoms(random_seed);
683 break;
684 case late_lua_code:
685 new_whatsit(late_lua_node); /* type == normal */
686 late_lua_name(tail) = scan_lua_state();
687 (void) scan_toks(false, false);
688 late_lua_data(tail) = def_ref;
689 break;
690 case late_lua_call_code:
691 new_whatsit(late_lua_node);
692 late_lua_type(tail) = lua_refid_call;
693 scan_int();
694 late_lua_data(tail) = cur_val;
695 break;
696 case expand_font_code:
697 read_expand_font();
698 break;
699 default:
700 confusion("int1");
701 break;
702 }
703 }
704 }
705
706 /*tex
707
708 This is experimental and not used for production, only for testing and writing
709 macros (some options stay). It's now obsolete. We keep the cmd_code becuase it
710 looks like hard coded numbers are used in macro packages.
711
712 */
713
714 static void run_option(void) {
715 normal_error("mathoption", "obsolete command");
716 }
717
718 static void lua_function_call(void) {
719 scan_int();
720 if (cur_val <= 0) {
721 normal_error("luafunctioncall", "invalid number");
722 } else {
723 str_number u = save_cur_string();
724 luacstrings = 0;
725 luafunctioncall(cur_val);
726 restore_cur_string(u);
727 if (luacstrings > 0)
728 lua_string_start();
729 }
730 }
731
732 static void lua_bytecode_call(void) {
733 scan_int();
734 if (cur_val < 0 || cur_val > 65535) {
735 normal_error("luabytecodecall", "invalid number");
736 } else {
737 str_number u = save_cur_string();
738 luacstrings = 0;
739 luabytecodecall(cur_val);
740 restore_cur_string(u);
741 if (luacstrings > 0)
742 lua_string_start();
743 }
744 }
745
746 /*tex
747
748 For mode-independent commands, the following macro is useful.
749
750 Also, there is a list of cases where the user has probably gotten into or out of
751 math mode by mistake. \TeX\ will insert a dollar sign and rescan the current
752 token, and it makes sense ot have a macro for that as well.
753
754 */
755
756 #define any_mode(A,B) jump_table[vmode+(A)]=B; jump_table[hmode+(A)]=B; jump_table[mmode+(A)]=B
757 #define non_math(A,B) jump_table[vmode+(A)]=B; jump_table[hmode+(A)]=B;
758
759 /*tex
760
761 The |main_control| uses a jump table, and |init_main_control| sets that table up.
762
763 */
764
765 typedef void (*main_control_function) (void);
766
767 main_control_function *jump_table;
768
769 static void init_main_control (void) {
770 jump_table = xmalloc((mmode+max_command_cmd+1) * sizeof(main_control_function)) ;
771
772 jump_table[hmode + char_num_cmd] = run_char_num;
773 jump_table[hmode + letter_cmd] = run_char;
774 jump_table[hmode + other_char_cmd] = run_char;
775 jump_table[hmode + char_given_cmd] = run_char;
776 jump_table[hmode + spacer_cmd] = run_app_space;
777 jump_table[hmode + ex_space_cmd] = run_app_space;
778 jump_table[mmode + ex_space_cmd] = run_app_space;
779 jump_table[hmode + boundary_cmd] = run_boundary;
780 jump_table[hmode + char_ghost_cmd] = run_char_ghost;
781 jump_table[mmode + char_ghost_cmd] = run_char_ghost;
782 any_mode(relax_cmd, run_relax);
783 jump_table[vmode + spacer_cmd] = run_relax;
784 jump_table[mmode + spacer_cmd] = run_relax;
785 jump_table[mmode + boundary_cmd] = run_relax;
786 any_mode(ignore_spaces_cmd,run_ignore_spaces);
787 jump_table[vmode + stop_cmd] = run_stop;
788 jump_table[vmode + math_char_num_cmd] = run_non_math_math;
789 jump_table[vmode + math_given_cmd] = run_non_math_math;
790 jump_table[vmode + xmath_given_cmd] = run_non_math_math;
791 jump_table[hmode + math_char_num_cmd] = run_math_char_num;
792 jump_table[hmode + math_given_cmd] = run_math_given;
793 jump_table[hmode + xmath_given_cmd] = run_xmath_given;
794
795 jump_table[vmode + vmove_cmd] = report_illegal_case;
796 jump_table[hmode + hmove_cmd] = report_illegal_case;
797 jump_table[mmode + hmove_cmd] = report_illegal_case;
798 any_mode(last_item_cmd, report_illegal_case);
799 jump_table[vmode + vadjust_cmd] = report_illegal_case;
800 jump_table[vmode + ital_corr_cmd] = report_illegal_case;
801 non_math(eq_no_cmd,report_illegal_case);
802 any_mode(mac_param_cmd,report_illegal_case);
803
804 non_math(sup_mark_cmd, insert_dollar_sign);
805 non_math(sub_mark_cmd, insert_dollar_sign);
806 non_math(super_sub_script_cmd, insert_dollar_sign);
807 non_math(no_super_sub_script_cmd, insert_dollar_sign);
808 non_math(math_comp_cmd, insert_dollar_sign);
809 non_math(delim_num_cmd, insert_dollar_sign);
810 non_math(left_right_cmd, insert_dollar_sign);
811 non_math(above_cmd, insert_dollar_sign);
812 non_math(radical_cmd, insert_dollar_sign);
813 non_math(math_style_cmd, insert_dollar_sign);
814 non_math(math_choice_cmd, insert_dollar_sign);
815 non_math(vcenter_cmd, insert_dollar_sign);
816 non_math(non_script_cmd, insert_dollar_sign);
817 non_math(mkern_cmd, insert_dollar_sign);
818 non_math(limit_switch_cmd, insert_dollar_sign);
819 non_math(mskip_cmd, insert_dollar_sign);
820 non_math(math_accent_cmd, insert_dollar_sign);
821 jump_table[mmode + endv_cmd] = insert_dollar_sign;
822 jump_table[mmode + par_end_cmd] = insert_dollar_sign_par_end;
823 jump_table[mmode + stop_cmd] = insert_dollar_sign;
824 jump_table[mmode + vskip_cmd] = insert_dollar_sign;
825 jump_table[mmode + un_vbox_cmd] = insert_dollar_sign;
826 jump_table[mmode + valign_cmd] = insert_dollar_sign;
827 jump_table[mmode + hrule_cmd] = insert_dollar_sign;
828 jump_table[mmode + no_hrule_cmd] = insert_dollar_sign;
829 jump_table[vmode + hrule_cmd] = run_rule;
830 jump_table[vmode + no_hrule_cmd] = run_rule;
831 jump_table[hmode + vrule_cmd] = run_rule;
832 jump_table[hmode + no_vrule_cmd] = run_rule;
833 jump_table[mmode + vrule_cmd] = run_rule;
834 jump_table[mmode + no_vrule_cmd] = run_rule;
835 jump_table[vmode + vskip_cmd] = append_glue;
836 jump_table[hmode + hskip_cmd] = append_glue;
837 jump_table[mmode + hskip_cmd] = append_glue;
838 jump_table[mmode + mskip_cmd] = append_glue;
839 any_mode(kern_cmd, append_kern);
840 jump_table[mmode + mkern_cmd] = append_kern;
841 non_math(left_brace_cmd, run_left_brace);
842 any_mode(begin_group_cmd,run_begin_group);
843 any_mode(end_group_cmd, run_end_group);
844 any_mode(right_brace_cmd, handle_right_brace);
845 jump_table[vmode + hmove_cmd] = run_move;
846 jump_table[hmode + vmove_cmd] = run_move;
847 jump_table[mmode + vmove_cmd] = run_move;
848 any_mode(leader_ship_cmd, run_leader_ship);
849 any_mode(make_box_cmd, run_make_box);
850 any_mode(assign_box_dir_cmd, run_box_dir);
851 any_mode(assign_box_direction_cmd, run_box_direction);
852 jump_table[vmode + start_par_cmd] = run_start_par_vmode;
853 jump_table[hmode + start_par_cmd] = run_start_par;
854 jump_table[mmode + start_par_cmd] = run_start_par;
855 jump_table[vmode + letter_cmd] = run_new_graf;
856 jump_table[vmode + other_char_cmd] = run_new_graf;
857 jump_table[vmode + char_num_cmd] = run_new_graf;
858 jump_table[vmode + char_given_cmd] = run_new_graf;
859 jump_table[vmode + char_ghost_cmd] = run_new_graf;
860 jump_table[vmode + math_shift_cmd] = run_new_graf;
861 jump_table[vmode + math_shift_cs_cmd] = run_new_graf;
862 jump_table[vmode + un_hbox_cmd] = run_new_graf;
863 jump_table[vmode + vrule_cmd] = run_new_graf;
864 jump_table[vmode + no_vrule_cmd] = run_new_graf;
865 jump_table[vmode + accent_cmd] = run_new_graf;
866 jump_table[vmode + discretionary_cmd] = run_new_graf;
867 jump_table[vmode + hskip_cmd] = run_new_graf;
868 jump_table[vmode + valign_cmd] = run_new_graf;
869 jump_table[vmode + ex_space_cmd] = run_new_graf;
870 jump_table[vmode + boundary_cmd] = run_new_graf;
871 jump_table[vmode + par_end_cmd] = run_par_end_vmode;
872 jump_table[hmode + par_end_cmd] = run_par_end_hmode;
873 jump_table[hmode + stop_cmd] = head_for_vmode;
874 jump_table[hmode + vskip_cmd] = head_for_vmode;
875 jump_table[hmode + hrule_cmd] = head_for_vmode;
876 jump_table[hmode + no_hrule_cmd] = head_for_vmode;
877 jump_table[hmode + un_vbox_cmd] = head_for_vmode;
878 jump_table[hmode + halign_cmd] = head_for_vmode;
879 any_mode(insert_cmd,begin_insert_or_adjust);
880 jump_table[hmode + vadjust_cmd] = begin_insert_or_adjust;
881 jump_table[mmode + vadjust_cmd] = begin_insert_or_adjust;
882 any_mode(mark_cmd, handle_mark);
883 any_mode(break_penalty_cmd, append_penalty);
884 any_mode(remove_item_cmd, delete_last);
885 jump_table[vmode + un_vbox_cmd] = unpackage;
886 jump_table[hmode + un_hbox_cmd] = unpackage;
887 jump_table[mmode + un_hbox_cmd] = unpackage;
888 jump_table[hmode + ital_corr_cmd] = append_italic_correction;
889 jump_table[mmode + ital_corr_cmd] = append_italic_correction_mmode;
890 jump_table[hmode + discretionary_cmd] = append_discretionary;
891 jump_table[mmode + discretionary_cmd] = append_discretionary;
892 any_mode(assign_local_box_cmd, run_local_box);
893 jump_table[hmode + accent_cmd] = make_accent;
894 any_mode(car_ret_cmd,align_error);
895 any_mode(tab_mark_cmd,align_error);
896 any_mode(no_align_cmd,no_align_error);
897 any_mode(omit_cmd, omit_error);
898 jump_table[vmode + halign_cmd] = init_align;
899 jump_table[hmode + valign_cmd] = init_align;
900 jump_table[mmode + halign_cmd] = run_halign_mmode;
901 jump_table[vmode + endv_cmd] = do_endv;
902 jump_table[hmode + endv_cmd] = do_endv;
903 any_mode(end_cs_name_cmd, cs_error);
904 jump_table[hmode + math_shift_cmd] = init_math;
905 jump_table[hmode + math_shift_cs_cmd] = init_math;
906 jump_table[mmode + eq_no_cmd] = run_eq_no;
907 jump_table[mmode + left_brace_cmd] = math_left_brace;
908 jump_table[mmode + letter_cmd] = run_letter_mmode;
909 jump_table[mmode + other_char_cmd] = run_letter_mmode;
910 jump_table[mmode + char_given_cmd] = run_letter_mmode;
911 jump_table[mmode + char_num_cmd] = run_char_num_mmode;
912 jump_table[mmode + math_char_num_cmd] = run_math_char_num_mmode;
913 jump_table[mmode + math_given_cmd] = run_math_given_mmode;
914 jump_table[mmode + xmath_given_cmd] = run_xmath_given_mmode;
915 jump_table[mmode + delim_num_cmd] = run_delim_num;
916 jump_table[mmode + math_comp_cmd] = math_math_comp;
917 jump_table[mmode + limit_switch_cmd] = math_limit_switch;
918 jump_table[mmode + radical_cmd] = math_radical;
919 jump_table[mmode + accent_cmd] = math_ac;
920 jump_table[mmode + math_accent_cmd] = math_ac;
921 jump_table[mmode + vcenter_cmd] = run_vcenter;
922 jump_table[mmode + math_style_cmd] = run_math_style;
923 jump_table[mmode + non_script_cmd] = run_non_script;
924 jump_table[mmode + math_choice_cmd] = run_math_choice;
925 jump_table[mmode + above_cmd] = math_fraction;
926 jump_table[mmode + sub_mark_cmd] = sub_sup;
927 jump_table[mmode + sup_mark_cmd] = sub_sup;
928 jump_table[mmode + super_sub_script_cmd] = sub_sup;
929 jump_table[mmode + no_super_sub_script_cmd] = no_sub_sup;
930 jump_table[mmode + left_right_cmd] = math_left_right;
931 jump_table[mmode + math_shift_cmd] = run_math_shift;
932 jump_table[mmode + math_shift_cs_cmd] = run_math_shift;
933 any_mode(toks_register_cmd, prefixed_command);
934 any_mode(assign_toks_cmd, prefixed_command);
935 any_mode(assign_int_cmd, prefixed_command);
936 any_mode(assign_attr_cmd, prefixed_command);
937 any_mode(assign_dir_cmd, prefixed_command);
938 any_mode(assign_direction_cmd, prefixed_command);
939 any_mode(assign_dimen_cmd, prefixed_command);
940 any_mode(assign_glue_cmd, prefixed_command);
941 any_mode(assign_mu_glue_cmd, prefixed_command);
942 any_mode(assign_font_dimen_cmd, prefixed_command);
943 any_mode(assign_font_int_cmd, prefixed_command);
944 any_mode(set_aux_cmd, prefixed_command);
945 any_mode(set_prev_graf_cmd, prefixed_command);
946 any_mode(set_page_dimen_cmd, prefixed_command);
947 any_mode(set_page_int_cmd, prefixed_command);
948 any_mode(set_box_dimen_cmd, prefixed_command);
949 any_mode(set_tex_shape_cmd, prefixed_command);
950 any_mode(set_etex_shape_cmd, prefixed_command);
951 any_mode(def_char_code_cmd, prefixed_command);
952 any_mode(def_del_code_cmd, prefixed_command);
953 any_mode(extdef_math_code_cmd, prefixed_command);
954 any_mode(extdef_del_code_cmd, prefixed_command);
955 any_mode(def_family_cmd, prefixed_command);
956 any_mode(set_math_param_cmd, prefixed_command);
957 any_mode(set_font_cmd, prefixed_command);
958 any_mode(def_font_cmd, prefixed_command);
959 any_mode(letterspace_font_cmd, prefixed_command);
960 any_mode(copy_font_cmd, prefixed_command);
961 any_mode(set_font_id_cmd, prefixed_command);
962 any_mode(register_cmd, prefixed_command);
963 any_mode(advance_cmd, prefixed_command);
964 any_mode(multiply_cmd, prefixed_command);
965 any_mode(divide_cmd, prefixed_command);
966 any_mode(prefix_cmd, prefixed_command);
967 any_mode(let_cmd, prefixed_command);
968 any_mode(shorthand_def_cmd, prefixed_command);
969 any_mode(read_to_cs_cmd, prefixed_command);
970 any_mode(def_cmd, prefixed_command);
971 any_mode(set_box_cmd, prefixed_command);
972 any_mode(hyph_data_cmd, prefixed_command);
973 any_mode(set_interaction_cmd, prefixed_command);
974 any_mode(after_assignment_cmd,run_after_assignment);
975 any_mode(after_group_cmd,run_after_group);
976 any_mode(partoken_name_cmd,run_par_token);
977 any_mode(in_stream_cmd,open_or_close_in);
978 any_mode(message_cmd,issue_message);
979 any_mode(case_shift_cmd, shift_case);
980 any_mode(xray_cmd, show_whatever);
981 any_mode(normal_cmd, run_normal);
982 any_mode(extension_cmd, run_extension);
983 any_mode(option_cmd, run_option); /* obsolete */
984
985 any_mode(lua_function_call_cmd, lua_function_call);
986 any_mode(lua_bytecode_call_cmd, lua_bytecode_call);
987 any_mode(def_lua_call_cmd, prefixed_command);
988 any_mode(lua_call_cmd, run_lua_call);
989 /* any_mode(lua_expandable_call_cmd, run_lua_call); */ /* no! outside jump table anyway, handled in expand() */
990 any_mode(node_cmd, run_node);
991
992 any_mode(combine_toks_cmd, combine_the_toks);
993 }
994
995 /*tex
996
997 And here is |main_control| itself. It is quite short nowadays.
998
999 */
1000
1001 void main_control(void)
1002 {
1003 main_control_state = goto_next;
1004 init_main_control() ;
1005 if (equiv(every_job_loc) != null) {
1006 begin_token_list(equiv(every_job_loc), every_job_text);
1007 }
1008 while (1) {
1009 if (main_control_state == goto_skip_token) {
1010 main_control_state = goto_next;
1011 } else {
1012 get_x_token();
1013 }
1014 /*tex
1015 Give diagnostic information, if requested When a new token has just
1016 been fetched at |big_switch|, we have an ideal place to monitor
1017 \TeX's activity.
1018 */
1019 if (interrupt != 0 && OK_to_interrupt) {
1020 back_input();
1021 check_interrupt();
1022 continue;
1023 }
1024 if (tracing_commands_par > 0) {
1025 show_cur_cmd_chr();
1026 }
1027 /*tex run the command */
1028 (jump_table[(abs(mode) + cur_cmd)])();
1029 if (main_control_state == goto_return) {
1030 return;
1031 }
1032 }
1033 /*tex not reached */
1034 return;
1035 }
1036
1037 /*tex
1038
1039 We assume a trailing relax: |{...}\relax|, so we don't need a |back_input()| here.
1040
1041 */
1042
1043 extern void local_control_message(const char *s)
1044 {
1045 tprint("local control level ");
1046 print_int(local_level);
1047 tprint(": ");
1048 tprint(s);
1049 tprint_nl("");
1050 }
1051
1052 /*tex Note for me (HH): in luametatex I have the option to use the save stack which for
1053 some experiments seems to work better. But I'll probably not go forward with
1054 some intended extension anyway (too messy), so we're currently in sync.
1055 Instead there are now two optional parameters in |runtoks|, plus a |quittoks|
1056 companion. I also added some more tracing as well as a warning when we jump
1057 out of control too often.
1058 */
1059
1060 void local_control(void)
1061 {
1062 /*tex Wrr to saving the state local_control is like some of the conv_toks which
1063 is not entirely correct but good enough for now. Future extensions might
1064 demand another solution.
1065 */
1066 int save_scanner_status = scanner_status;
1067 halfword save_def_ref = def_ref;
1068 halfword save_warning_index = warning_index;
1069 int ll = local_level;
1070 main_control_state = goto_next;
1071 local_level += 1;
1072 if (tracing_nesting_par > 2) {
1073 local_control_message("entering local control");
1074 }
1075 while (1) {
1076 if (main_control_state == goto_skip_token) {
1077 main_control_state = goto_next;
1078 } else {
1079 get_x_token();
1080 }
1081 if (interrupt != 0 && OK_to_interrupt) {
1082 back_input();
1083 check_interrupt();
1084 continue;
1085 }
1086 if (tracing_commands_par > 0) {
1087 show_cur_cmd_chr();
1088 }
1089 (jump_table[(abs(mode) + cur_cmd)])();
1090 if (local_level <= ll) {
1091 main_control_state = goto_next;
1092 if (tracing_nesting_par > 2) {
1093 local_control_message("leaving local control due to level change");
1094 }
1095 break;
1096 } else if (main_control_state == goto_return) {
1097 if (tracing_nesting_par > 2) {
1098 local_control_message("leaving local control due to triggering");
1099 }
1100 break;
1101 }
1102 }
1103 /*tex From the perspective of ending and changing the level this is the
1104 wrong spot, as it should be done in |end_local_control| in which case
1105 we should use the save stack. Maybe some day.
1106 */
1107 scanner_status = save_scanner_status;
1108 def_ref = save_def_ref;
1109 warning_index = save_warning_index;
1110 }
1111
1112 void end_local_control(void )
1113 {
1114 if (local_level > 0) {
1115 local_level -= 1;
1116 } else {
1117 local_control_message("redundant end local control");
1118 }
1119 }
1120
1121 /*tex
1122 We need to go back to the main loop. This is rather nasty and dirty
1123 and counterintuive code and there might be a cleaner way. Basically
1124 we trigger the main control state from here.
1125
1126 \starttyping
1127 0 0 \directlua{token.scan_list()}\hbox{!}
1128 -1 0 \setbox0\hbox{x}\directlua{token.scan_list()}\box0
1129 1 1 \toks0={\directlua{token.scan_list()}\hbox{x}}\directlua{tex.runtoks(0)}
1130 0 0 1 1 \directlua{tex.box[0]=token.scan_list()}\hbox{x\directlua{node.write(token.scan_list())}\hbox{x}}
1131 0 0 0 1 \setbox0\hbox{x}\directlua{tex.box[0]=token.scan_list()}\hbox{x\directlua{node.write(token.scan_list())}\box0}
1132 \stoptyping
1133
1134 It's rather fragile code so we added some tracing options.
1135
1136 */
1137
1138 halfword local_scan_box(void)
1139 {
1140 int old_mode = mode;
1141 int ll = local_level;
1142 mode = -hmode;
1143 scan_box(lua_scan_flag);
1144 if (local_level == ll) {
1145 /*tex |\directlua{print(token.scan_list())}\hbox{!}| (n n) */
1146 if (tracing_nesting_par > 2) {
1147 local_control_message("entering at end of box scanning");
1148 }
1149 local_control();
1150 } else {
1151 /*tex |\directlua{print(token.scan_list())}\box0| (n-1 n) */
1152 /*
1153 if (tracing_nesting_par > 2) {
1154 local_control_message("setting level after box scanning");
1155 }
1156 */
1157 local_level = ll;
1158 }
1159 mode = old_mode;
1160 return cur_box;
1161 }
1162
1163 /*tex
1164
1165 We have an issue with modes when we quit here because we're coming
1166 from and still staying at the \LUA\ end. So, unless we're already
1167 nested, we trigger an end_local_level token (an extension code).
1168
1169 */
1170
1171 static void wrapup_local_scan_box(void)
1172 {
1173 /*
1174 if (tracing_nesting_par > 2) {
1175 local_control_message("leaving box scanner");
1176 }
1177 */
1178 local_level -= 1;
1179 }
1180
1181 int current_local_level(void)
1182 {
1183 return local_level;
1184 }
1185
1186 void app_space(void)
1187 { /* handle spaces when |space_factor<>1000| */
1188 halfword q; /* glue node */
1189 if ((space_factor_par >= 2000) && (! glue_is_zero(xspace_skip_par))) {
1190 q = new_param_glue(xspace_skip_code);
1191 /* so from now we have a subtype with spaces: */
1192 subtype(q) = xspace_skip_code + 1;
1193 } else {
1194 if (!glue_is_zero(space_skip_par)) {
1195 q = new_glue(space_skip_par);
1196 } else {
1197 q = new_glue(zero_glue);
1198 width(q) = space(cur_font_par);
1199 stretch(q) = space_stretch(cur_font_par);
1200 shrink(q) = space_shrink(cur_font_par);
1201 }
1202 /* Modify the glue specification in |q| according to the space factor */
1203 if (space_factor_par >= 2000)
1204 width(q) = width(q) + extra_space(cur_font_par);
1205 stretch(q) = xn_over_d(stretch(q), space_factor_par, 1000);
1206 shrink(q) = xn_over_d(shrink(q), 1000, space_factor_par);
1207
1208 /* so from now we have a subtype with spaces: */
1209 subtype(q) = space_skip_code + 1;
1210 }
1211 couple_nodes(tail, q);
1212 tail = q;
1213 }
1214
1215 void insert_dollar_sign(void)
1216 {
1217 back_input();
1218 cur_tok = math_shift_token + '$';
1219 print_err("Missing $ inserted");
1220 help2(
1221 "I've inserted a begin-math/end-math symbol since I think",
1222 "you left one out. Proceed, with fingers crossed."
1223 );
1224 ins_error();
1225 }
1226
1227 /*tex
1228
1229 We can silently ignore \.{\\par}s in a math formula.
1230
1231 */
1232
1233 void insert_dollar_sign_par_end(void)
1234 {
1235 if (!suppress_mathpar_error_par) {
1236 insert_dollar_sign() ;
1237 }
1238 }
1239
1240 /*tex
1241
1242 The `|you_cant|' procedure prints a line saying that the current command is
1243 illegal in the current mode; it identifies these things symbolically.
1244
1245 */
1246
1247 void you_cant(void)
1248 {
1249 print_err("You can't use `");
1250 print_cmd_chr((quarterword) cur_cmd, cur_chr);
1251 print_in_mode(mode);
1252 }
1253
1254 /*tex
1255
1256 When erroneous situations arise, \TeX\ usually issues an error message specific
1257 to the particular error. For example, `\.{\\noalign}' should not appear in any
1258 mode, since it is recognized by the |align_peek| routine in all of its legitimate
1259 appearances; a special error message is given when `\.{\\noalign}' occurs
1260 elsewhere. But sometimes the most appropriate error message is simply that the
1261 user is not allowed to do what he or she has attempted. For example,
1262 `\.{\\moveleft}' is allowed only in vertical mode, and `\.{\\lower}' only in
1263 non-vertical modes. Such cases are enumerated here and in the other sections
1264 referred to under `See also \dots.'
1265
1266 */
1267
1268 void report_illegal_case(void)
1269 {
1270 you_cant();
1271 help4(
1272 "Sorry, but I'm not programmed to handle this case;",
1273 "I'll just pretend that you didn't ask for it.",
1274 "If you're in the wrong mode, you might be able to",
1275 "return to the right one by typing `I}' or `I$' or `I\\par'."
1276 );
1277 error();
1278 }
1279
1280 /*tex
1281
1282 Some operations are allowed only in privileged modes, i.e., in cases that
1283 |mode>0|. The |privileged| function is used to detect violations of this rule; it
1284 issues an error message and returns |false| if the current |mode| is negative.
1285
1286 */
1287
1288 boolean privileged(void)
1289 {
1290 if (mode > 0) {
1291 return true;
1292 } else {
1293 report_illegal_case();
1294 return false;
1295 }
1296 }
1297
1298 /*tex
1299
1300 We don't want to leave |main_control| immediately when a |stop| command is
1301 sensed, because it may be necessary to invoke an \.{\\output} routine several
1302 times before things really grind to a halt. (The output routine might even say
1303 `\.{\\gdef\\end\{...\}}', to prolong the life of the job.) Therefore
1304 |its_all_over| is |true| only when the current page and contribution list are
1305 empty, and when the last output was not a ``dead cycle.''
1306
1307 */
1308
1309
1310 boolean its_all_over(void)
1311 { /* do this when \.{\\end} or \.{\\dump} occurs */
1312 if (privileged()) {
1313 if ((page_head == page_tail) && (head == tail) && (dead_cycles == 0)) {
1314 return true;
1315 }
1316 back_input(); /* we will try to end again after ejecting residual material */
1317 tail_append(new_null_box());
1318 width(tail) = hsize_par;
1319 tail_append(new_glue(fill_glue));
1320 tail_append(new_penalty(-010000000000,final_penalty));
1321 normal_page_filter(end);
1322 build_page(); /* append \.{\\hbox to \\hsize\{\}\\vfill\\penalty-'10000000000} */
1323 }
1324 return false;
1325 }
1326
1327 /*tex
1328
1329 The |hskip| and |vskip| command codes are used for control sequences like
1330 \.{\\hss} and \.{\\vfil} as well as for \.{\\hskip} and \.{\\vskip}. The
1331 difference is in the value of |cur_chr|.
1332
1333 All the work relating to glue creation has been relegated to the following
1334 subroutine. It does not call |build_page|, because it is used in at least one
1335 place where that would be a mistake.
1336
1337 */
1338
1339 void append_glue(void)
1340 {
1341 int s = cur_chr;
1342 switch (s) {
1343 case fil_code:
1344 cur_val = new_glue(fil_glue);
1345 break;
1346 case fill_code:
1347 cur_val = new_glue(fill_glue);
1348 break;
1349 case ss_code:
1350 cur_val = new_glue(ss_glue);
1351 break;
1352 case fil_neg_code:
1353 cur_val = new_glue(fil_neg_glue);
1354 break;
1355 case skip_code:
1356 scan_glue(glue_val_level);
1357 break;
1358 case mskip_code:
1359 scan_glue(mu_val_level);
1360 break;
1361 }
1362 /* now |cur_val| points to the glue specification */
1363 tail_append(new_glue(cur_val));
1364 flush_node(cur_val);
1365 if (s > skip_code) {
1366 subtype(tail) = mu_glue;
1367 }
1368 }
1369
1370 void append_kern(void)
1371 {
1372 int s = cur_chr; /* |subtype| of the kern node */
1373 scan_dimen((s == mu_glue), false, false);
1374 tail_append(new_kern(cur_val));
1375 subtype(tail) = (quarterword) s;
1376 }
1377
1378 /*tex
1379
1380 We have to deal with errors in which braces and such things are not properly
1381 nested. Sometimes the user makes an error of commission by inserting an extra
1382 symbol, but sometimes the user makes an error of omission. \TeX\ can't always
1383 tell one from the other, so it makes a guess and tries to avoid getting into a
1384 loop.
1385
1386 The |off_save| routine is called when the current group code is wrong. It tries
1387 to insert something into the user's input that will help clean off the top level.
1388
1389 */
1390
1391 void off_save(void)
1392 {
1393 halfword p, q;
1394 if (cur_group == bottom_level) {
1395 /*tex Drop current token and complain that it was unmatched */
1396 print_err("Extra ");
1397 print_cmd_chr((quarterword) cur_cmd, cur_chr);
1398 help1(
1399 "Things are pretty mixed up, but I think the worst is over."
1400 );
1401 error();
1402
1403 } else {
1404 back_input();
1405 p = get_avail();
1406 set_token_link(temp_token_head, p);
1407 print_err("Missing ");
1408 /*tex
1409 Prepare to insert a token that matches |cur_group|, and print what it
1410 is. At this point, |link(temp_token_head)=p|, a pointer to an empty
1411 one-word node.
1412 */
1413 switch (cur_group) {
1414 case semi_simple_group:
1415 set_token_info(p, cs_token_flag + frozen_end_group);
1416 tprint_esc("endgroup");
1417 break;
1418 case math_shift_group:
1419 set_token_info(p, math_shift_token + '$');
1420 print_char('$');
1421 break;
1422 case math_left_group:
1423 set_token_info(p, cs_token_flag + frozen_right);
1424 q = get_avail();
1425 set_token_link(p, q);
1426 p = token_link(p);
1427 set_token_info(p, other_token + '.');
1428 tprint_esc("right.");
1429 break;
1430 default:
1431 set_token_info(p, right_brace_token + '}');
1432 print_char('}');
1433 break;
1434 }
1435 tprint(" inserted");
1436 ins_list(token_link(temp_token_head));
1437 help5(
1438 "I've inserted something that you may have forgotten.",
1439 "(See the <inserted text> above.)",
1440 "With luck, this will get me unwedged. But if you",
1441 "really didn't forget anything, try typing `2' now; then",
1442 "my insertion and my current dilemma will both disappear."
1443 );
1444 error();
1445 }
1446 }
1447 /*tex
1448
1449 The routine for a |right_brace| character branches into many subcases, since a
1450 variety of things may happen, depending on |cur_group|. Some types of groups are
1451 not supposed to be ended by a right brace; error messages are given in hopes of
1452 pinpointing the problem. Most branches of this routine will be filled in later,
1453 when we are ready to understand them; meanwhile, we must prepare ourselves to
1454 deal with such errors.
1455
1456 */
1457
1458 void handle_right_brace(void)
1459 {
1460 halfword p, q; /* for short-term use */
1461 scaled d; /* holds |split_max_depth| in |insert_group| */
1462 int f; /* holds |floating_penalty| in |insert_group| */
1463 p = null;
1464 /*
1465 The |partoken_context_code_par| branch is taken from pdftex as-is. We could save some lines
1466 by first testing for hmode and the parameter but in the end it's not cleaner.
1467 */
1468 switch (cur_group) {
1469 case simple_group:
1470 fixup_directions();
1471 break;
1472 case bottom_level:
1473 print_err("Too many }'s");
1474 help2(
1475 "You've closed more groups than you opened.",
1476 "Such booboos are generally harmless, so keep going."
1477 );
1478 error();
1479 break;
1480 case semi_simple_group:
1481 case math_shift_group:
1482 case math_left_group:
1483 extra_right_brace();
1484 break;
1485 /*tex
1486 When the right brace occurs at the end of an \.{\\hbox} or
1487 \.{\\vbox} or \.{\\vtop} construction, the |package| routine
1488 comes into action. We might also have to finish a paragraph that
1489 hasn't ended.
1490 */
1491 case hbox_group:
1492 if (fixup_boxes_par) {
1493 /*tex
1494 This is unofficial! Fixing up (also elsewhere) might become default
1495 some day but for a while I will test this in ConTeXt.
1496 */
1497 fixup_directions_only();
1498 }
1499 package(0);
1500 break;
1501 case adjusted_hbox_group:
1502 adjust_tail = adjust_head;
1503 pre_adjust_tail = pre_adjust_head;
1504 package(0);
1505 break;
1506 case vbox_group:
1507 /* the |partoken_context_code_par| branch is taken from pdftex as-is: */
1508 if (partoken_context_code_par > 0 && mode == hmode) {
1509 back_input();
1510 cur_tok = par_token;
1511 back_input();
1512 token_type = inserted;
1513 } else {
1514 end_graf(vbox_group);
1515 package(0);
1516 }
1517 break;
1518 case vtop_group:
1519 /* the |partoken_context_code_par| branch is taken from pdftex as-is: */
1520 if (partoken_context_code_par > 0 && mode == hmode) {
1521 back_input();
1522 cur_tok = par_token;
1523 back_input();
1524 token_type = inserted;
1525 } else {
1526 end_graf(vtop_group);
1527 package(vtop_code);
1528 }
1529 break;
1530 case insert_group:
1531 /* the |partoken_context_code_par| branch is taken from pdftex as-is: */
1532 if (partoken_context_code_par > 1 && mode == hmode) {
1533 back_input();
1534 cur_tok = par_token;
1535 back_input();
1536 token_type = inserted;
1537 } else {
1538 end_graf(insert_group);
1539 q = new_glue(split_top_skip_par);
1540 d = split_max_depth_par;
1541 f = floating_penalty_par;
1542 unsave();
1543 save_ptr--;
1544 /*tex
1545 Now |saved_value(0)| is the insertion number, or the |vadjust| subtype.
1546 */
1547 p = vpack(vlink(head), 0, additional, -1);
1548 pop_nest();
1549 if (saved_type(0) == saved_insert) {
1550 tail_append(new_node(ins_node, saved_value(0)));
1551 height(tail) = height(p) + depth(p);
1552 ins_ptr(tail) = list_ptr(p);
1553 split_top_ptr(tail) = q;
1554 depth(tail) = d;
1555 float_cost(tail) = f;
1556 } else if (saved_type(0) == saved_adjust) {
1557 tail_append(new_node(adjust_node, saved_value(0)));
1558 adjust_ptr(tail) = list_ptr(p);
1559 flush_node(q);
1560 } else {
1561 confusion("insert_group");
1562 }
1563 list_ptr(p) = null;
1564 flush_node(p);
1565 if (nest_ptr == 0) {
1566 checked_page_filter(insert);
1567 build_page();
1568 }
1569 }
1570 break;
1571 case output_group:
1572 /* the |partoken_context_code_par| branch is taken from pdftex as-is: */
1573 if (partoken_context_code_par > 1 && mode == hmode) {
1574 back_input();
1575 cur_tok = par_token;
1576 back_input();
1577 token_type = inserted;
1578 } else {
1579 /*tex
1580 this is needed in case the \.{\\output} executes a \.{\\textdir} command.
1581 */
1582 if (dir_level(text_dir_ptr) == cur_level) {
1583 /*tex Remove from |text_dir_ptr| */
1584 halfword text_dir_tmp = vlink(text_dir_ptr);
1585 flush_node(text_dir_ptr);
1586 text_dir_ptr = text_dir_tmp;
1587 }
1588 resume_after_output();
1589 }
1590 break;
1591 case disc_group:
1592 build_discretionary();
1593 break;
1594 case local_box_group:
1595 build_local_box();
1596 break;
1597 case align_group:
1598 back_input();
1599 cur_tok = cs_token_flag + frozen_cr;
1600 print_err("Missing \\cr inserted");
1601 help1(
1602 "I'm guessing that you meant to end an alignment here."
1603 );
1604 ins_error();
1605 break;
1606 case no_align_group:
1607 /* the |partoken_context_code_par| branch is taken from pdftex as-is: */
1608 if (partoken_context_code_par > 1 && mode == hmode) {
1609 back_input();
1610 cur_tok = par_token;
1611 back_input();
1612 token_type = inserted;
1613 } else {
1614 end_graf(no_align_group);
1615 unsave();
1616 align_peek();
1617 }
1618 break;
1619 case vcenter_group:
1620 /* the |partoken_context_code_par| branch is taken from pdftex as-is: */
1621 if (partoken_context_code_par > 0 && mode == hmode) {
1622 back_input();
1623 cur_tok = par_token;
1624 back_input();
1625 token_type = inserted;
1626 } else {
1627 end_graf(vcenter_group);
1628 finish_vcenter();
1629 }
1630 break;
1631 case math_choice_group:
1632 build_choices();
1633 break;
1634 case math_group:
1635 close_math_group(p);
1636 break;
1637 default:
1638 confusion("rightbrace");
1639 break;
1640 }
1641 }
1642
1643 void extra_right_brace(void)
1644 {
1645 print_err("Extra }, or forgotten ");
1646 switch (cur_group) {
1647 case semi_simple_group:
1648 tprint_esc("endgroup");
1649 break;
1650 case math_shift_group:
1651 print_char('$');
1652 break;
1653 case math_left_group:
1654 tprint_esc("right");
1655 break;
1656 }
1657 help5(
1658 "I've deleted a group-closing symbol because it seems to be",
1659 "spurious, as in `$x}$'. But perhaps the } is legitimate and",
1660 "you forgot something else, as in `\\hbox{$x}'. In such cases",
1661 "the way to recover is to insert both the forgotten and the",
1662 "deleted material, e.g., by typing `I$}'."
1663 );
1664 error();
1665 incr(align_state);
1666 }
1667
1668 /*tex
1669
1670 Here is where we clear the parameters that are supposed to revert to their
1671 default values after every paragraph and when internal vertical mode is entered.
1672
1673 */
1674
1675 void normal_paragraph(void)
1676 {
1677 if (looseness_par != 0)
1678 eq_word_define(int_base + looseness_code, 0);
1679 if (hang_indent_par != 0)
1680 eq_word_define(dimen_base + hang_indent_code, 0);
1681 if (hang_after_par != 1)
1682 eq_word_define(int_base + hang_after_code, 1);
1683 if (par_shape_par_ptr != null)
1684 eq_define(par_shape_loc, shape_ref_cmd, null);
1685 if (inter_line_penalties_par_ptr != null)
1686 eq_define(inter_line_penalties_loc, shape_ref_cmd, null);
1687 if (shape_mode_par > 0)
1688 eq_word_define(int_base + shape_mode_code, 0);
1689 }
1690
1691 /*tex
1692
1693 The global variable |cur_box| will point to a newly-made box. If the box is void,
1694 we will have |cur_box=null|. Otherwise we will have |type(cur_box)=hlist_node| or
1695 |vlist_node| or |rule_node|; the |rule_node| case can occur only with leaders.
1696
1697 */
1698
1699 halfword cur_box; /* box to be placed into its context */
1700
1701 /*tex
1702
1703 The |box_end| procedure does the right thing with |cur_box|, if |box_context|
1704 represents the context as explained above.
1705
1706 */
1707
1708 void box_end(int box_context)
1709 {
1710 if (box_context < box_flag) {
1711 /*tex
1712
1713 Append box |cur_box| to the current list, shifted by |box_context|.
1714 The global variable |adjust_tail| will be non-null if and only if the
1715 current box might include adjustments that should be appended to the
1716 current vertical list.
1717
1718 */
1719
1720 if (cur_box != null) {
1721 shift_amount(cur_box) = box_context;
1722 if (abs(mode) == vmode) {
1723 if (pre_adjust_tail != null) {
1724 if (pre_adjust_head != pre_adjust_tail)
1725 append_list(pre_adjust_head, pre_adjust_tail);
1726 pre_adjust_tail = null;
1727 }
1728 append_to_vlist(cur_box,lua_key_index(box));
1729 if (adjust_tail != null) {
1730 if (adjust_head != adjust_tail)
1731 append_list(adjust_head, adjust_tail);
1732 adjust_tail = null;
1733 }
1734 if (mode > 0) {
1735 checked_page_filter(box);
1736 build_page();
1737 }
1738 } else {
1739 if (abs(mode) == hmode)
1740 space_factor_par = 1000;
1741 else
1742 cur_box = new_sub_box(cur_box);
1743 couple_nodes(tail, cur_box);
1744 tail = cur_box;
1745 }
1746 }
1747 } else if (box_context < ship_out_flag) {
1748 /*tex
1749 Store |cur_box| in a box register
1750 */
1751 if (box_context < global_box_flag)
1752 eq_define(box_base + box_context - box_flag, box_ref_cmd, cur_box);
1753 else
1754 geq_define(box_base + box_context - global_box_flag, box_ref_cmd, cur_box);
1755 } else if (box_context == lua_scan_flag) {
1756 /*tex
1757 We are done with scanning so let's return to the caller.
1758 */
1759 wrapup_local_scan_box();
1760 } else if (cur_box != null) {
1761 /*tex
1762 The leaders contexts come after shipout and luascan contexts.
1763 */
1764 /* if (box_context > lua_scan_flag) { */
1765 if (box_context >= leader_flag) {
1766 /*tex
1767 Append a new leader node that uses |cur_box| and get the next
1768 non-blank non-relax...
1769 */
1770 do {
1771 get_x_token();
1772 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
1773 if (((cur_cmd == hskip_cmd) && (abs(mode) != vmode)) ||
1774 ((cur_cmd == vskip_cmd) && (abs(mode) == vmode))) {
1775 append_glue();
1776 subtype(tail) = (quarterword) (box_context - (leader_flag - a_leaders));
1777 leader_ptr(tail) = cur_box;
1778 } else {
1779 print_err("Leaders not followed by proper glue");
1780 help3(
1781 "You should say `\\leaders <box or rule><hskip or vskip>'.",
1782 "I found the <box or rule>, but there's no suitable",
1783 "<hskip or vskip>, so I'm ignoring these leaders."
1784 );
1785 back_error();
1786 flush_node_list(cur_box);
1787 }
1788 } else {
1789 if (box_context != ship_out_flag) {
1790 normal_error("scanner","shipout expected");
1791 }
1792 ship_out(static_pdf, cur_box, SHIPPING_PAGE);
1793 }
1794 }
1795 }
1796
1797 /*tex
1798
1799 the next input should specify a box or perhaps a rule
1800
1801 */
1802
1803 void scan_box(int box_context)
1804 {
1805 /*tex Get the next non-blank non-relax... */
1806 do {
1807 get_x_token();
1808 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
1809 if (cur_cmd == make_box_cmd) {
1810 begin_box(box_context);
1811 } else if ((box_context >= leader_flag) &&
1812 ((cur_cmd == hrule_cmd) || (cur_cmd == vrule_cmd) ||
1813 (cur_cmd == no_hrule_cmd) || (cur_cmd == no_vrule_cmd))) {
1814 cur_box = scan_rule_spec();
1815 box_end(box_context);
1816 } else {
1817 print_err("A <box> was supposed to be here");
1818 help3(
1819 "I was expecting to see \\hbox or \\vbox or \\copy or \\box or",
1820 "something like that. So you might find something missing in",
1821 "your output. But keep trying; you can fix this later."
1822 );
1823 back_error();
1824 if (box_context == lua_scan_flag) {
1825 cur_box = null;
1826 box_end(box_context);
1827 }
1828 }
1829 }
1830
1831 void new_graf(boolean indented)
1832 {
1833 halfword p, q, dir_graf_tmp;
1834 halfword dir_rover;
1835 int callback_id;
1836 if ((mode == vmode) || (head != tail)) {
1837 tail_append(new_param_glue(par_skip_code));
1838 }
1839 callback_id = callback_defined(new_graf_callback);
1840 if (callback_id > 0) {
1841 run_callback(callback_id, "db->b", cur_list.mode_field,indented,&indented);
1842 }
1843 prev_graf_par = 0;
1844 push_nest();
1845 mode = hmode;
1846 space_factor_par = 1000;
1847 /*tex Add local paragraph node */
1848 tail_append(make_local_par_node(new_graf_par_code));
1849 if (indented) {
1850 p = new_null_box();
1851 box_dir(p) = par_direction_par;
1852 width(p) = par_indent_par;
1853 subtype(p) = indent_list;
1854 q = tail;
1855 tail_append(p);
1856 } else {
1857 q = tail;
1858 }
1859 dir_rover = text_dir_ptr;
1860 while (dir_rover != null) {
1861 if ((vlink(dir_rover) != null) || (dir_dir(dir_rover) != par_direction_par)) {
1862 dir_graf_tmp = new_dir(dir_dir(dir_rover));
1863 try_couple_nodes(dir_graf_tmp,vlink(q));
1864 couple_nodes(q,dir_graf_tmp);
1865 }
1866 dir_rover = vlink(dir_rover);
1867 }
1868 q = head;
1869 while (vlink(q) != null)
1870 q = vlink(q);
1871 tail = q;
1872 if (every_par_par != null)
1873 begin_token_list(every_par_par, every_par_text);
1874 if (nest_ptr == 1) {
1875 checked_page_filter(new_graf);
1876 /*tex put |par_skip| glue on current page */
1877 build_page();
1878 }
1879 }
1880
1881 void indent_in_hmode(void)
1882 {
1883 halfword p;
1884 if (cur_chr > 0) {
1885 /*tex \.{\\indent} */
1886 p = new_null_box();
1887 width(p) = par_indent_par;
1888 if (abs(mode) == hmode)
1889 space_factor_par = 1000;
1890 else
1891 p = new_sub_box(p);
1892 tail_append(p);
1893 }
1894 }
1895
1896 void head_for_vmode(void)
1897 {
1898 if (mode < 0) {
1899 if ((cur_cmd != hrule_cmd) && (cur_cmd != no_hrule_cmd)) {
1900 off_save();
1901 } else {
1902 print_err("You can't use `\\hrule' here except with leaders");
1903 help2(
1904 "To put a horizontal rule in an hbox or an alignment,",
1905 "you should use \\leaders or \\hrulefill (see The TeXbook)."
1906 );
1907 error();
1908 }
1909 } else {
1910 back_input();
1911 cur_tok = par_token;
1912 back_input();
1913 token_type = inserted;
1914 }
1915 }
1916
1917 /*tex
1918
1919 |dir_save| would have been set by |line_break| by means of |post_line_break|, but
1920 this is not done right now, as it introduces pretty heavy memory leaks. This
1921 means the current code might be wrong in some way that relates to in-paragraph
1922 displays.
1923
1924 */
1925
1926 static int only_dirs(halfword n)
1927 {
1928 while (n) {
1929 if (type(n) == local_par_node || type(n) == dir_node) {
1930 n = vlink(n);
1931 } else {
1932 return 0;
1933 }
1934 }
1935 return 1;
1936 }
1937
1938 void end_graf(int line_break_context)
1939 {
1940 if (mode == hmode) {
1941 /*tex
1942 We ignore |null| paragraphs, that is those that only have a local par node
1943 and possibly a few dir nodes injected automatically.
1944 */
1945 if (head == tail) {
1946 pop_nest();
1947 } else if (only_dirs(vlink(head))) {
1948 flush_node_list(vlink(head));
1949 pop_nest();
1950 } else {
1951 line_break(false, line_break_context);
1952 }
1953 if (dir_save != null) {
1954 flush_node_list(dir_save);
1955 dir_save = null;
1956 }
1957 normal_paragraph();
1958 error_count = 0;
1959 }
1960 }
1961
1962 void begin_insert_or_adjust(void)
1963 {
1964 if (cur_cmd != vadjust_cmd) {
1965 scan_register_num();
1966 if (cur_val == output_box_par) {
1967 print_err("You can't \\insert");
1968 print_int(output_box_par);
1969 help1(
1970 "I'm changing to \\insert0; box \\outputbox is special."
1971 );
1972 error();
1973 cur_val = 0;
1974 }
1975 set_saved_record(0, saved_insert, 0, cur_val);
1976 } else if (scan_keyword("pre")) {
1977 set_saved_record(0, saved_adjust, 0, 1);
1978 } else {
1979 set_saved_record(0, saved_adjust, 0, 0);
1980 }
1981 save_ptr++;
1982 new_save_level(insert_group);
1983 scan_left_brace();
1984 normal_paragraph();
1985 push_nest();
1986 mode = -vmode;
1987 prev_depth_par = ignore_depth;
1988 }
1989
1990 /*tex
1991
1992 I (TH)'ve renamed the |make_mark| procedure to this, because if the current chr
1993 code is 1, then the actual command was \.{\\clearmarks}, which does not generate
1994 a mark node but instead destroys the current mark tokenlists.
1995
1996 */
1997
1998 void handle_mark(void)
1999 {
2000 halfword p; /* new node */
2001 halfword c; /* the mark class */
2002 if (cur_chr == clear_marks_code) {
2003 scan_mark_num();
2004 c = cur_val;
2005 delete_top_mark(c);
2006 delete_bot_mark(c);
2007 delete_first_mark(c);
2008 delete_split_first_mark(c);
2009 delete_split_bot_mark(c);
2010 } else {
2011 if (cur_chr == 0) {
2012 c = 0;
2013 } else {
2014 scan_mark_num();
2015 c = cur_val;
2016 if (c > biggest_used_mark)
2017 biggest_used_mark = c;
2018 }
2019 p = scan_toks(false, true);
2020 p = new_node(mark_node, 0); /* the |subtype| is not used */
2021 mark_class(p) = c;
2022 mark_ptr(p) = def_ref;
2023 couple_nodes(tail, p);
2024 tail = p;
2025 }
2026 }
2027
2028 void append_penalty(void)
2029 {
2030 scan_int();
2031 tail_append(new_penalty(cur_val,user_penalty));
2032 if (mode == vmode) {
2033 checked_page_filter(penalty);
2034 build_page();
2035 }
2036 }
2037
2038 /*tex
2039
2040 When |delete_last| is called, |cur_chr| is the |type| of node that will be
2041 deleted, if present.
2042
2043 The |remove_item| command removes a penalty, kern, or glue node if it appears at
2044 the tail of the current list, using a brute-force linear scan. Like
2045 \.{\\lastbox}, this command is not allowed in vertical mode (except internal
2046 vertical mode), since the current list in vertical mode is sent to the page
2047 builder. But if we happen to be able to implement it in vertical mode, we do.
2048
2049 */
2050
2051 void delete_last(void)
2052 {
2053 halfword p, q; /* run through the current list */
2054 if ((mode == vmode) && (tail == head)) {
2055 /*tex
2056 Apologize for inability to do the operation now, unless \.{\\unskip}
2057 follows non-glue
2058 */
2059 if ((cur_chr != glue_node) || (last_glue != max_halfword)) {
2060 you_cant();
2061 if (cur_chr == kern_node) {
2062 help2(
2063 "Sorry...I usually can't take things from the current page.",
2064 "Try `I\\kern-\\lastkern' instead."
2065 );
2066 } else if (cur_chr != glue_node) {
2067 help2
2068 ("Sorry...I usually can't take things from the current page.",
2069 "Perhaps you can make the output routine do it.");
2070 } else {
2071 help2
2072 ("Sorry...I usually can't take things from the current page.",
2073 "Try `I\\vskip-\\lastskip' instead.");
2074 }
2075 error();
2076 }
2077 } else {
2078 /*tex Todo: clean this up! */
2079 if (!is_char_node(tail)) {
2080 if (type(tail) == cur_chr) {
2081 q = head;
2082 do {
2083 p = q;
2084 if (!is_char_node(q)) {
2085 if (type(q) == disc_node) {
2086 if (p == tail)
2087 return;
2088 }
2089 }
2090 q = vlink(p);
2091 } while (q != tail);
2092 vlink(p) = null;
2093 flush_node_list(tail);
2094 tail = p;
2095 }
2096 }
2097 }
2098 }
2099
2100 void unpackage(void)
2101 {
2102 halfword p; /* the box */
2103 halfword r; /* to remove marginal kern nodes */
2104 int c; /* should we copy? */
2105 halfword s; /* for varmem assignment */
2106 if (cur_chr > copy_code) {
2107 /*tex Handle saved items and |goto done| */
2108 try_couple_nodes(tail, disc_ptr[cur_chr]);
2109 disc_ptr[cur_chr] = null;
2110 goto DONE;
2111 }
2112 c = cur_chr;
2113 scan_register_num();
2114 p = box(cur_val);
2115 if (p == null)
2116 return;
2117 if ((abs(mode) == mmode)
2118 || ((abs(mode) == vmode) && (type(p) != vlist_node))
2119 || ((abs(mode) == hmode) && (type(p) != hlist_node))) {
2120 print_err("Incompatible list can't be unboxed");
2121 help3(
2122 "Sorry, Pandora. (You sneaky devil.)",
2123 "I refuse to unbox an \\hbox in vertical mode or vice versa.",
2124 "And I can't open any boxes in math mode."
2125 );
2126 error();
2127 return;
2128 }
2129 if (c == copy_code) {
2130 s = copy_node_list(list_ptr(p));
2131 try_couple_nodes(tail,s);
2132 } else {
2133 try_couple_nodes(tail,list_ptr(p));
2134 box(cur_val) = null;
2135 list_ptr(p) = null;
2136 flush_node(p);
2137 }
2138 DONE:
2139 while (vlink(tail) != null) {
2140 r = vlink(tail);
2141 if (!is_char_node(r) && (type(r) == margin_kern_node)) {
2142 try_couple_nodes(tail,vlink(r));
2143 flush_node(r);
2144 }
2145 tail = vlink(tail);
2146 }
2147 }
2148
2149 /*tex
2150
2151 Italic corrections are converted to kern nodes when the |ital_corr| command
2152 follows a character. In math mode the same effect is achieved by appending a kern
2153 of zero here, since italic corrections are supplied later.
2154
2155 */
2156
2157 void append_italic_correction(void)
2158 {
2159 halfword p; /* |char_node| at the tail of the current list */
2160 internal_font_number f; /* the font in the |char_node| */
2161 if (tail != head) {
2162 if (is_char_node(tail))
2163 p = tail;
2164 else
2165 return;
2166 f = font(p);
2167 tail_append(new_kern(char_italic(f, character(p))));
2168 subtype(tail) = italic_kern;
2169 }
2170 }
2171
2172 void append_local_box(int kind)
2173 {
2174 incr(save_ptr);
2175 set_saved_record(-1, saved_boxtype, 0, kind);
2176 new_save_level(local_box_group);
2177 scan_left_brace();
2178 push_nest();
2179 mode = -hmode;
2180 space_factor_par = 1000;
2181 }
2182
2183 /*tex
2184
2185 Discretionary nodes are easy in the common case `\.{\\-}', but in the general
2186 case we must process three braces full of items.
2187
2188 The space factor does not change when we append a discretionary node, but it
2189 starts out as 1000 in the subsidiary lists.
2190
2191 */
2192
2193 void append_discretionary(void)
2194 {
2195 int c;
2196 tail_append(new_disc());
2197 subtype(tail) = (quarterword) cur_chr;
2198 if (cur_chr == explicit_disc) {
2199 /* |\-| */
2200 c = get_pre_hyphen_char(cur_lang_par);
2201 if (c > 0) {
2202 vlink(pre_break(tail)) = new_char(equiv(cur_font_loc), c);
2203 alink(vlink(pre_break(tail))) = pre_break(tail);
2204 tlink(pre_break(tail)) = vlink(pre_break(tail));
2205 }
2206 c = get_post_hyphen_char(cur_lang_par);
2207 if (c > 0) {
2208 vlink(post_break(tail)) = new_char(equiv(cur_font_loc), c);
2209 alink(vlink(post_break(tail))) = post_break(tail);
2210 tlink(post_break(tail)) = vlink(post_break(tail));
2211 }
2212 set_explicit_disc_penalty(tail);
2213 } else if (cur_chr == automatic_disc) {
2214 /*tex As done in hyphenator: */
2215 c = get_pre_exhyphen_char(cur_lang_par);
2216 if (c <= 0) {
2217 c = ex_hyphen_char_par;
2218 }
2219 if (c > 0) {
2220 vlink(pre_break(tail)) = new_char(equiv(cur_font_loc), c);
2221 alink(vlink(pre_break(tail))) = pre_break(tail);
2222 tlink(pre_break(tail)) = vlink(pre_break(tail));
2223 }
2224 c = get_post_exhyphen_char(cur_lang_par);
2225 if (c > 0) {
2226 vlink(post_break(tail)) = new_char(equiv(cur_font_loc), c);
2227 alink(vlink(post_break(tail))) = post_break(tail);
2228 tlink(post_break(tail)) = vlink(post_break(tail));
2229 }
2230 c = ex_hyphen_char_par;
2231 if (c > 0) {
2232 vlink(no_break(tail)) = new_char(equiv(cur_font_loc), c);
2233 alink(vlink(no_break(tail))) = no_break(tail);
2234 tlink(no_break(tail)) = vlink(no_break(tail));
2235 }
2236 set_automatic_disc_penalty(tail);
2237 } else {
2238 /*tex |\discretionary| */
2239 if (scan_keyword("penalty")) {
2240 scan_int();
2241 disc_penalty(tail) = cur_val;
2242 }
2243 incr(save_ptr);
2244 set_saved_record(-1, saved_disc, 0, 0);
2245 new_save_level(disc_group);
2246 scan_left_brace();
2247 push_nest();
2248 mode = -hmode;
2249 space_factor_par = 1000;
2250 /*tex Already preset: |disc_penalty(tail) = hyphen_penalty_par;| */
2251 }
2252 }
2253
2254 /*tex
2255
2256 The test for |p != null| ensures that empty \.{\\localleftbox} and
2257 \.{\\localrightbox} commands are not applied.
2258
2259 */
2260
2261 void build_local_box(void)
2262 {
2263 halfword p;
2264 int kind;
2265 unsave();
2266 assert(saved_type(-1) == saved_boxtype);
2267 kind = saved_value(-1);
2268 decr(save_ptr);
2269 p = vlink(head);
2270 pop_nest();
2271 if (p != null) {
2272 /*tex
2273 Somehow |filtered_hpack| goes beyond the first node so we loose it.
2274 */
2275 /*tex
2276 There is no need for |new_hyphenation(p, null);| here as we're in
2277 an |\hbox|.
2278 */
2279 (void) new_ligkern(p, null);
2280 p = lua_hpack_filter(p, 0, additional, local_box_group, -1, null);
2281 /*tex
2282 We really need something packed so we play safe! This feature is inherited
2283 but could have been delegated to a callback anyway.
2284 */
2285 p = hpack(p, 0, additional, -1);
2286 }
2287 if (kind == 0)
2288 eq_define(local_left_box_base, box_ref_cmd, p);
2289 else
2290 eq_define(local_right_box_base, box_ref_cmd, p);
2291 if (abs(mode) == hmode) {
2292 /*tex Add local paragraph node */
2293 tail_append(make_local_par_node(local_box_par_code));
2294 }
2295 eq_word_define(int_base + no_local_whatsits_code, no_local_whatsits_par + 1);
2296 }
2297
2298 /*tex
2299
2300 The three discretionary lists are constructed somewhat as if they were hboxes.
2301 A~subroutine called |build_discretionary| handles the transitions. (This is sort
2302 of fun.)
2303
2304 */
2305
2306 void build_discretionary(void)
2307 {
2308 halfword p, q; /* for link manipulation */
2309 int n; /* length of discretionary list */
2310 unsave();
2311 /*tex
2312 Prune the current list, if necessary, until it contains only |char_node|,
2313 |kern_node|, |hlist_node|, |vlist_node| and |rule_node| items; set |n| to
2314 the length of the list, and set |q| to the lists tail. During this loop,
2315 |p=vlink(q)| and there are |n| items preceding |p|.
2316 */
2317 q = head;
2318 p = vlink(q);
2319 n = 0;
2320 while (p != null) {
2321 if (!is_char_node(p) && type(p) > rule_node && type(p) != kern_node) {
2322 print_err("Improper discretionary list");
2323 help1(
2324 "Discretionary lists must contain only boxes and kerns."
2325 );
2326 error();
2327 begin_diagnostic();
2328 tprint_nl("The following discretionary sublist has been deleted:");
2329 show_box(p);
2330 end_diagnostic(true);
2331 flush_node_list(p);
2332 vlink(q) = null;
2333 break;
2334 }
2335 alink(p) = q;
2336 q = p;
2337 p = vlink(q);
2338 incr(n);
2339 }
2340
2341 p = vlink(head);
2342 pop_nest();
2343 assert(saved_type(-1) == saved_disc);
2344 switch (saved_value(-1)) {
2345 case 0:
2346 if (n > 0) {
2347 vlink(pre_break(tail)) = p;
2348 alink(p) = pre_break(tail);
2349 tlink(pre_break(tail)) = q;
2350 }
2351 break;
2352 case 1:
2353 if (n > 0) {
2354 vlink(post_break(tail)) = p;
2355 alink(p) = post_break(tail);
2356 tlink(post_break(tail)) = q;
2357 }
2358 break;
2359 case 2:
2360 /*tex
2361 Attach list |p| to the current list, and record its length; then
2362 finish up and |return|
2363 */
2364 if ((n > 0) && (abs(mode) == mmode)) {
2365 print_err("Illegal math \\discretionary");
2366 help2(
2367 "Sorry: The third part of a discretionary break must be",
2368 "empty, in math formulas. I had to delete your third part."
2369 );
2370 flush_node_list(p);
2371 error();
2372 } else {
2373 if (n > 0) {
2374 vlink(no_break(tail)) = p;
2375 alink(p) = no_break(tail);
2376 tlink(no_break(tail)) = q;
2377 }
2378 }
2379 decr(save_ptr);
2380 /*tex There are no other cases. */
2381 return;
2382 break;
2383 }
2384 set_saved_record(-1, saved_disc, 0, (saved_value(-1) + 1));
2385 new_save_level(disc_group);
2386 scan_left_brace();
2387 push_nest();
2388 mode = -hmode;
2389 space_factor_par = 1000;
2390 }
2391
2392 /*tex
2393
2394 The positioning of accents is straightforward but tedious. Given an accent of
2395 width |a|, designed for characters of height |x| and slant |s|; and given a
2396 character of width |w|, height |h|, and slant |t|: We will shift the accent down
2397 by |x-h|, and we will insert kern nodes that have the effect of centering the
2398 accent over the character and shifting the accent to the right by
2399 $\delta={1\over2}(w-a)+h\cdot t-x\cdot s$. If either character is absent from the
2400 font, we will simply use the other, without shifting.
2401
2402 */
2403
2404 void make_accent(void)
2405 {
2406 double s, t; /* amount of slant */
2407 halfword p, q, r; /* character, box, and kern nodes */
2408 internal_font_number f; /* relevant font */
2409 scaled a, h, x, w, delta; /* heights and widths, as explained above */
2410 scan_char_num();
2411 f = equiv(cur_font_loc);
2412 p = new_glyph(f, cur_val);
2413 if (p != null) {
2414 x = x_height(f);
2415 /*tex real division */
2416 s = float_cast(slant(f)) / float_constant(65536);
2417 a = glyph_width(p);
2418 do_assignments();
2419 /*tex
2420 Create a character node |q| for the next character, but set |q:=null|
2421 if problems arise
2422 */
2423 q = null;
2424 f = equiv(cur_font_loc);
2425 if ((cur_cmd == letter_cmd) ||
2426 (cur_cmd == other_char_cmd) || (cur_cmd == char_given_cmd)) {
2427 q = new_glyph(f, cur_chr);
2428 } else if (cur_cmd == char_num_cmd) {
2429 scan_char_num();
2430 q = new_glyph(f, cur_val);
2431 } else {
2432 back_input();
2433 }
2434
2435 if (q != null) {
2436 /*tex
2437 Append the accent with appropriate kerns, then set |p:=q|. The
2438 kern nodes appended here must be distinguished from other kerns,
2439 lest they be wiped away by the hyphenation algorithm or by a
2440 previous line break. The two kerns are computed with
2441 (machine-dependent) |real| arithmetic, but their sum is
2442 machine-independent; the net effect is machine-independent,
2443 because the user cannot remove these nodes nor access them via
2444 \.{\\lastkern}.
2445 */
2446 t = float_cast(slant(f)) / float_constant(65536); /* real division */
2447 w = glyph_width(q);
2448 h = glyph_height(q);
2449 if (h != x) {
2450 /*tex the accent must be shifted up or down */
2451 p = hpack(p, 0, additional, -1);
2452 shift_amount(p) = x - h;
2453 }
2454 /*tex real multiplication */
2455 delta = round(float_cast(w - a) / float_constant(2) + h * t - x * s);
2456 r = new_kern(delta);
2457 subtype(r) = accent_kern;
2458 couple_nodes(tail, r);
2459 couple_nodes(r, p);
2460 tail = new_kern(-a - delta);
2461 subtype(tail) = accent_kern;
2462 couple_nodes(p, tail);
2463 p = q;
2464
2465 }
2466 couple_nodes(tail, p);
2467 tail = p;
2468 space_factor_par = 1000;
2469 }
2470 }
2471
2472 /*tex
2473
2474 When `\.{\\cr}' or `\.{\\span}' or a tab mark comes through the scanner into
2475 |main_control|, it might be that the user has foolishly inserted one of them into
2476 something that has nothing to do with alignment. But it is far more likely that a
2477 left brace or right brace has been omitted, since |get_next| takes actions
2478 appropriate to alignment only when `\.{\\cr}' or `\.{\\span}' or tab marks occur
2479 with |align_state=0|. The following program attempts to make an appropriate
2480 recovery.
2481
2482 */
2483
2484 void align_error(void)
2485 {
2486 if (abs(align_state) > 2) {
2487 /*tex
2488 Express consternation over the fact that no alignment is in progress.
2489 */
2490 print_err("Misplaced ");
2491 print_cmd_chr((quarterword) cur_cmd, cur_chr);
2492 if (cur_tok == tab_token + '&') {
2493 help6(
2494 "I can't figure out why you would want to use a tab mark",
2495 "here. If you just want an ampersand, the remedy is",
2496 "simple: Just type `I\\&' now. But if some right brace",
2497 "up above has ended a previous alignment prematurely,",
2498 "you're probably due for more error messages, and you",
2499 "might try typing `S' now just to see what is salvageable."
2500 );
2501 } else {
2502 help5(
2503 "I can't figure out why you would want to use a tab mark",
2504 "or \\cr or \\span just now. If something like a right brace",
2505 "up above has ended a previous alignment prematurely,",
2506 "you're probably due for more error messages, and you",
2507 "might try typing `S' now just to see what is salvageable."
2508 );
2509 }
2510 error();
2511
2512 } else {
2513 back_input();
2514 if (align_state < 0) {
2515 print_err("Missing { inserted");
2516 incr(align_state);
2517 cur_tok = left_brace_token + '{';
2518 } else {
2519 print_err("Missing } inserted");
2520 decr(align_state);
2521 cur_tok = right_brace_token + '}';
2522 }
2523 help3(
2524 "I've put in what seems to be necessary to fix",
2525 "the current column of the current alignment.",
2526 "Try to go on, since this might almost work."
2527 );
2528 ins_error();
2529 }
2530 }
2531
2532 /*tex
2533
2534 The help messages here contain a little white lie, since \.{\\noalign} and
2535 \.{\\omit} are allowed also after `\.{\\noalign\{...\}}'.
2536
2537 */
2538
2539 void no_align_error(void)
2540 {
2541 print_err("Misplaced \\noalign");
2542 help2(
2543 "I expect to see \\noalign only after the \\cr of",
2544 "an alignment. Proceed, and I'll ignore this case."
2545 );
2546 error();
2547 }
2548
2549 void omit_error(void)
2550 {
2551 print_err("Misplaced \\omit");
2552 help2(
2553 "I expect to see \\omit only after tab marks or the \\cr of",
2554 "an alignment. Proceed, and I'll ignore this case."
2555 );
2556 error();
2557 }
2558
2559 /*tex
2560
2561 We've now covered most of the abuses of \.{\\halign} and \.{\\valign}. Let's take
2562 a look at what happens when they are used correctly.
2563
2564 An |align_group| code is supposed to remain on the |save_stack| during an entire
2565 alignment, until |fin_align| removes it.
2566
2567 A devious user might force an |endv| command to occur just about anywhere; we
2568 must defeat such hacks.
2569
2570 */
2571
2572 void do_endv(void)
2573 {
2574 base_ptr = input_ptr;
2575 input_stack[base_ptr] = cur_input;
2576 while ((input_stack[base_ptr].index_field != v_template) &&
2577 (input_stack[base_ptr].loc_field == null) &&
2578 (input_stack[base_ptr].state_field == token_list))
2579 decr(base_ptr);
2580 if ((input_stack[base_ptr].index_field != v_template) ||
2581 (input_stack[base_ptr].loc_field != null) ||
2582 (input_stack[base_ptr].state_field != token_list))
2583 fatal_error("(interwoven alignment preambles are not allowed)");
2584 /*tex interwoven alignment preambles... */
2585 if (cur_group == align_group) {
2586 end_graf(align_group);
2587 if (fin_col())
2588 fin_row();
2589 } else {
2590 off_save();
2591 }
2592 }
2593
2594 /*tex
2595
2596 Finally, \.{\\endcsname} is not supposed to get through to |main_control|.
2597
2598 */
2599
2600 void cs_error(void)
2601 {
2602 print_err("Extra \\endcsname");
2603 help1(
2604 "I'm ignoring this, since I wasn't doing a \\csname."
2605 );
2606 error();
2607 }
2608
2609 /*tex
2610
2611 Assignments to values in |eqtb| can be global or local. Furthermore, a control
2612 sequence can be defined to be `\.{\\long}', `\.{\\protected}', or `\.{\\outer}',
2613 and it might or might not be expanded. The prefixes `\.{\\global}', `\.{\\long}',
2614 `\.{\\protected}', and `\.{\\outer}' can occur in any order. Therefore we assign
2615 binary numeric codes, making it possible to accumulate the union of all specified
2616 prefixes by adding the corresponding codes. (PASCAL's |set| operations could also
2617 have been used.)
2618
2619 Every prefix, and every command code that might or might not be prefixed, calls
2620 the action procedure |prefixed_command|. This routine accumulates a sequence of
2621 prefixes until coming to a non-prefix, then it carries out the command.
2622
2623 */
2624
2625 /*tex
2626
2627 If the user says, e.g., `\.{\\global\\global}', the redundancy is silently
2628 accepted. The different types of code values have different legal ranges; the
2629 following program is careful to check each case properly.
2630
2631 */
2632
2633 #define check_def_code(A) do { \
2634 if (((cur_val<0)&&(p<(A)))||(cur_val>n)) { \
2635 print_err("Invalid code ("); \
2636 print_int(cur_val); \
2637 if (p<(A)) \
2638 tprint("), should be in the range 0.."); \
2639 else \
2640 tprint("), should be at most "); \
2641 print_int(n); \
2642 help1( \
2643 "I'm going to use 0 instead of that illegal code value." \
2644 ); \
2645 error(); \
2646 cur_val=0; \
2647 } \
2648 } while (0)
2649
2650 /*
2651 halfword swap_hang_indent(halfword indentation, halfword shape_mode) {
2652 if (shape_mode == 1 || shape_mode == 3 || shape_mode == -1 || shape_mode == -3) {
2653 return negate(indentation);
2654 } else {
2655 return indentation;
2656 }
2657 }
2658
2659 halfword swap_parshape_indent(halfword indentation, halfword width, halfword shape_mode) {
2660 if (shape_mode == 2 || shape_mode == 3 || shape_mode == -2 || shape_mode == -3) {
2661 return hsize_par - width - indentation;
2662 } else {
2663 return indentation;
2664 }
2665 }
2666
2667 */
2668
2669 /*
2670
2671 \def\foo{0} {\tracingcommands2 \tracingonline2 \globaldefs 1 \global\def \foo{1}} \foo 1 \par
2672 \def\foo{0} {\tracingcommands2 \tracingonline2 \globaldefs 1 \def \foo{2}} \foo 2 \par % {\global enforced}
2673 \def\foo{0} {\tracingcommands2 \tracingonline2 \globaldefs 0 \def \foo{4}} \foo 0 \par
2674 \def\foo{0} {\tracingcommands2 \tracingonline2 \globaldefs 0 \gdef\foo{4}} \foo 4 \par
2675 \def\foo{0} {\tracingcommands2 \tracingonline2 \globaldefs-1 \global\def \foo{3}} \foo 0 \par % {\global canceled}
2676 \def\foo{0} {\tracingcommands2 \tracingonline2 \globaldefs-1 \gdef\foo{4}} \foo 0 \par % {\global canceled}
2677
2678 */
2679
2680 void prefixed_command(void)
2681 {
2682 int a; /* accumulated prefix codes so far */
2683 internal_font_number f; /* identifies a font */
2684 halfword j; /* index into a \.{\\parshape} specification */
2685 halfword p, q; /* for temporary short-term use */
2686 int n; /* ditto */
2687 boolean e, check_glue; /* should a definition be expanded? or was \.{\\let} not done? */
2688 mathcodeval mval; /* for handling of \.{\\mathchardef}s */
2689 a = 0;
2690 while (cur_cmd == prefix_cmd) {
2691 if (!odd(a / cur_chr))
2692 a = a + cur_chr;
2693 /*tex
2694 Get the next non-blank non-relax...
2695 */
2696 do {
2697 get_x_token();
2698 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
2699
2700 if (cur_cmd <= max_non_prefixed_command || cur_cmd == combine_toks_cmd) {
2701 /*tex
2702 Discard erroneous prefixes and |return|
2703 */
2704 print_err("You can't use a prefix with `");
2705 print_cmd_chr((quarterword) cur_cmd, cur_chr);
2706 print_char('\'');
2707 help2(
2708 "I'll pretend you didn't say \\long or \\outer or \\global or",
2709 "\\protected."
2710 );
2711 back_error();
2712 return;
2713 }
2714 if (tracing_commands_par > 2)
2715 show_cur_cmd_chr();
2716 }
2717 /*tex
2718 Discard the prefixes \.{\\long} and \.{\\outer} if they are irrelevant
2719 */
2720 if (a >= 8) {
2721 j = protected_token;
2722 a = a - 8;
2723 } else {
2724 j = 0;
2725 }
2726 if ((cur_cmd != def_cmd) && (cur_cmd != def_lua_call_cmd) && ((a % 4 != 0) || (j != 0))) {
2727 print_err("You can't use `\\long' or `\\outer' or `\\protected' with `");
2728 print_cmd_chr((quarterword) cur_cmd, cur_chr);
2729 print_char('\'');
2730 help1(
2731 "I'll pretend you didn't say \\long or \\outer or \\protected here."
2732 );
2733 error();
2734 }
2735 /*tex
2736 Adjust for the setting of \.{\\globaldefs}
2737 */
2738 /* if (global_defs_par != 0) { */
2739 /* if (global_defs_par < 0) { */
2740 /* if (is_global(a)) */
2741 /* a = a - 4; */
2742 /* } else { */
2743 /* if (!is_global(a)) */
2744 /* a = a + 4; */
2745 /* } */
2746 /* } */
2747 if (cur_cmd == def_cmd && odd(cur_chr) && ! is_global(a)) {
2748 a += 4;
2749 }
2750 if (global_defs_par < 0) {
2751 if (is_global(a)) {
2752 a -= 4;
2753 if (tracing_commands_par > 1) {
2754 begin_diagnostic();
2755 tprint_nl("{\\global canceled}");
2756 end_diagnostic(false);
2757 }
2758 }
2759 } else if (global_defs_par > 0) {
2760 if (! is_global(a)) {
2761 a += 4;
2762 if (tracing_commands_par > 1) {
2763 begin_diagnostic();
2764 tprint_nl("{\\global enforced}");
2765 end_diagnostic(false);
2766 }
2767 }
2768 }
2769 switch (cur_cmd) {
2770 case set_font_cmd:
2771 /*tex
2772 Here's an example of the way many of the following routines operate.
2773 (Unfortunately, they aren't all as simple as this.)
2774 */
2775 define(cur_font_loc, data_cmd, cur_chr);
2776 break;
2777 case def_cmd:
2778 /*tex
2779 When a |def| command has been scanned, |cur_chr| is odd if the
2780 definition is supposed to be global, and |cur_chr>=2| if the
2781 definition is supposed to be expanded.
2782 */
2783 /* if (odd(cur_chr) && !is_global(a) && (global_defs_par >= 0)) { */
2784 /* a = a + 4; */
2785 /* } */
2786 e = (cur_chr >= 2);
2787 get_r_token();
2788 p = cur_cs;
2789 q = scan_toks(true, e);
2790 if (j != 0) {
2791 q = get_avail();
2792 set_token_info(q, j);
2793 set_token_link(q, token_link(def_ref));
2794 set_token_link(def_ref, q);
2795 }
2796 define(p, call_cmd + (a % 4), def_ref);
2797 break;
2798 case let_cmd:
2799 n = cur_chr;
2800 switch (n) {
2801 case 0:
2802 /*tex |glet| */
2803 if (!is_global(a) && (global_defs_par >= 0)) {
2804 a = a + 4;
2805 }
2806 case 1:
2807 /*tex |let| */
2808 get_r_token();
2809 p = cur_cs;
2810 do {
2811 get_token();
2812 } while (cur_cmd == spacer_cmd);
2813 if (cur_tok == other_token + '=') {
2814 get_token();
2815 if (cur_cmd == spacer_cmd)
2816 get_token();
2817 }
2818 break;
2819 case 2:
2820 /*tex |futurelet| */
2821 get_r_token();
2822 p = cur_cs;
2823 get_token();
2824 q = cur_tok;
2825 get_token();
2826 back_input();
2827 cur_tok = q;
2828 /*tex
2829 We look ahead and then back up. Note that |back_input| doesn't
2830 affect |cur_cmd|, |cur_chr|
2831 */
2832 back_input();
2833 break;
2834 case 3:
2835 /*tex |letcharcode| */
2836 scan_int();
2837 if (cur_val > 0) {
2838 /*tex HH: I need to do a more extensive test later. */
2839 p = active_to_cs(cur_val, true);
2840 do {
2841 get_token();
2842 } while (cur_cmd == spacer_cmd);
2843 if (cur_tok == other_token + '=') {
2844 get_token();
2845 if (cur_cmd == spacer_cmd)
2846 get_token();
2847 }
2848 } else {
2849 p = null;
2850 tex_error("invalid number for \\letcharcode",NULL);
2851 }
2852 break;
2853 default:
2854 /*tex We please the compiler. */
2855 p = null;
2856 confusion("let");
2857 break;
2858 }
2859 if (cur_cmd >= call_cmd)
2860 add_token_ref(cur_chr);
2861 define(p, cur_cmd, cur_chr);
2862 break;
2863 case shorthand_def_cmd:
2864 /*tex
2865 We temporarily define |p| to be |relax|, so that an occurrence of
2866 |p| while scanning the definition will simply stop the scanning
2867 instead of producing an ``undefined control sequence'' error or
2868 expanding the previous meaning. This allows, for instance,
2869 `\.{\\chardef\\foo=123\\foo}'.
2870 */
2871 n = cur_chr;
2872 get_r_token();
2873 p = cur_cs;
2874 define(p, relax_cmd, too_big_char);
2875 scan_optional_equals();
2876 switch (n) {
2877 case char_def_code:
2878 scan_char_num();
2879 define(p, char_given_cmd, cur_val);
2880 break;
2881 case math_char_def_code:
2882 mval = scan_mathchar(tex_mathcode);
2883 /* if (math_umathcode_meaning_par == 1) { */
2884 /* cur_val = (mval.class_value + (8 * mval.family_value)) * (65536 * 32) + mval.character_value; */
2885 /* define(p, xmath_given_cmd, cur_val); */
2886 /* } else { */
2887 cur_val = (mval.class_value * 16 + mval.family_value) * 256 + mval.character_value;
2888 define(p, math_given_cmd, cur_val);
2889 /* } */
2890 break;
2891 case xmath_char_def_code:
2892 mval = scan_mathchar(umath_mathcode);
2893 cur_val = (mval.class_value + (8 * mval.family_value)) * (65536 * 32) + mval.character_value;
2894 define(p, xmath_given_cmd, cur_val);
2895 break;
2896 case umath_char_def_code:
2897 mval = scan_mathchar(umathnum_mathcode);
2898 cur_val = (mval.class_value + (8 * mval.family_value)) * (65536 * 32) + mval.character_value;
2899 define(p, xmath_given_cmd, cur_val);
2900 break;
2901 default:
2902 scan_register_num();
2903 switch (n) {
2904 case count_def_code:
2905 define(p, assign_int_cmd, count_base + cur_val);
2906 break;
2907 case attribute_def_code:
2908 define(p, assign_attr_cmd, attribute_base + cur_val);
2909 break;
2910 case dimen_def_code:
2911 define(p, assign_dimen_cmd, scaled_base + cur_val);
2912 break;
2913 case skip_def_code:
2914 define(p, assign_glue_cmd, skip_base + cur_val);
2915 break;
2916 case mu_skip_def_code:
2917 define(p, assign_mu_glue_cmd, mu_skip_base + cur_val);
2918 break;
2919 case toks_def_code:
2920 define(p, assign_toks_cmd, toks_base + cur_val);
2921 break;
2922 default:
2923 confusion("shorthand_def");
2924 break;
2925 }
2926 break;
2927 }
2928 break;
2929 case read_to_cs_cmd:
2930 j = cur_chr;
2931 scan_int();
2932 n = cur_val;
2933 if (!scan_keyword("to")) {
2934 print_err("Missing `to' inserted");
2935 help2(
2936 "You should have said `\\read<number> to \\cs'.",
2937 "I'm going to look for the \\cs now."
2938 );
2939 error();
2940 }
2941 get_r_token();
2942 p = cur_cs;
2943 read_toks(n, p, j);
2944 define(p, call_cmd, cur_val);
2945 break;
2946 case toks_register_cmd:
2947 case assign_toks_cmd:
2948 /*tex
2949 The token-list parameters, \.{\\output} and \.{\\everypar}, etc.,
2950 receive their values in the following way. (For safety's sake, we
2951 place an enclosing pair of braces around an \.{\\output} list.)
2952 */
2953 q = cur_cs;
2954 if (cur_cmd == toks_register_cmd) {
2955 scan_register_num();
2956 p = toks_base + cur_val;
2957 } else {
2958 /*tex |p=every_par_loc| or |output_routine_loc| or \dots */
2959 p = cur_chr;
2960 }
2961 scan_optional_equals();
2962 /*tex Get the next non-blank non-relax non-call token */
2963 do {
2964 get_x_token();
2965 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
2966
2967 if (cur_cmd != left_brace_cmd) {
2968 /*tex
2969 If the right-hand side is a token parameter or token
2970 register, finish the assignment and |goto done|
2971 */
2972 if (cur_cmd == toks_register_cmd) {
2973 scan_register_num();
2974 cur_cmd = assign_toks_cmd;
2975 cur_chr = toks_base + cur_val;
2976 }
2977 if (cur_cmd == assign_toks_cmd) {
2978 q = equiv(cur_chr);
2979 if (q == null) {
2980 define(p, undefined_cs_cmd, null);
2981 } else {
2982 add_token_ref(q);
2983 define(p, call_cmd, q);
2984 }
2985 goto DONE;
2986 }
2987 }
2988 back_input();
2989 cur_cs = q;
2990 q = scan_toks(false, false);
2991 if (token_link(def_ref) == null) { /* empty list: revert to the default */
2992 define(p, undefined_cs_cmd, null);
2993 free_avail(def_ref);
2994 } else {
2995 if (p == output_routine_loc) { /* enclose in curlies */
2996 p = get_avail();
2997 set_token_link(q, p);
2998 p = output_routine_loc;
2999 q = token_link(q);
3000 set_token_info(q, right_brace_token + '}');
3001 q = get_avail();
3002 set_token_info(q, left_brace_token + '{');
3003 set_token_link(q, token_link(def_ref));
3004 set_token_link(def_ref, q);
3005 }
3006 define(p, call_cmd, def_ref);
3007 }
3008 break;
3009 case assign_int_cmd:
3010 /*tex Similar routines are used to assign values to the numeric parameters. */
3011 p = cur_chr;
3012 scan_optional_equals();
3013 scan_int();
3014 assign_internal_value(a, p, cur_val);
3015 break;
3016 case assign_attr_cmd:
3017 p = cur_chr;
3018 scan_optional_equals();
3019 scan_int();
3020 if ((p - attribute_base) > max_used_attr)
3021 max_used_attr = (p - attribute_base);
3022 attr_list_cache = cache_disabled;
3023 word_define(p, cur_val);
3024 break;
3025 case assign_direction_cmd:
3026 case assign_dir_cmd:
3027 /*tex Assign direction codes. */
3028 if (cur_cmd == assign_direction_cmd) {
3029 p = cur_chr;
3030 scan_optional_equals();
3031 scan_int();
3032 check_dir_value(cur_val);
3033 cur_chr = p;
3034 } else {
3035 scan_direction();
3036 }
3037 switch (cur_chr) {
3038 case int_base + page_direction_code:
3039 eq_word_define(int_base + page_direction_code, cur_val);
3040 break;
3041 case int_base + body_direction_code:
3042 eq_word_define(int_base + body_direction_code, cur_val);
3043 break;
3044 case int_base + par_direction_code:
3045 eq_word_define(int_base + par_direction_code, cur_val);
3046 break;
3047 case int_base + math_direction_code:
3048 eq_word_define(int_base + math_direction_code, cur_val);
3049 break;
3050 case int_base + text_direction_code:
3051 case int_base + line_direction_code:
3052 /*
3053 pre version 0.97 this was a commented section because various tests hint that this
3054 is unnecessary and sometimes even produces weird results, like:
3055
3056 (\hbox{\textdir TRT ABC\textdir TLT DEF}))
3057
3058 becomes
3059
3060 (DEFCBA)
3061
3062 in the output when we use
3063
3064 tail_append(new_dir(text_direction_par)
3065
3066 but when we append the reverse of the current it goes better
3067
3068 */
3069 check_glue = (cur_chr == (int_base + line_direction_code));
3070 if (check_glue) {
3071 cur_chr = int_base + text_direction_code ;
3072 }
3073 if (abs(mode) == hmode) {
3074 if (no_local_dirs_par > 0) {
3075 /*tex |tail| is non zero but we test anyway. */
3076 if (check_glue && (tail != null && type(tail) == glue_node)) {
3077 halfword prev = alink(tail);
3078 halfword dirn = new_dir(text_direction_par);
3079 subtype(dirn) = cancel_dir;
3080 couple_nodes(prev,dirn);
3081 couple_nodes(dirn,tail);
3082 } else {
3083 tail_append(new_dir(text_direction_par));
3084 subtype(tail) = cancel_dir;
3085 }
3086 } else {
3087 /*tex What is the use of nolocaldirs? Maybe we should get rid of it. */
3088 }
3089 update_text_dir_ptr(cur_val);
3090 tail_append(new_dir(cur_val));
3091 dir_level(tail) = cur_level;
3092 } else {
3093 update_text_dir_ptr(cur_val);
3094 }
3095 eq_word_define(int_base + text_direction_code, cur_val);
3096 eq_word_define(int_base + no_local_dirs_code, no_local_dirs_par + 1);
3097 break;
3098 }
3099 break;
3100 case assign_dimen_cmd:
3101 p = cur_chr;
3102 scan_optional_equals();
3103 scan_normal_dimen();
3104 assign_internal_value(a, p, cur_val);
3105 break;
3106 case assign_glue_cmd:
3107 case assign_mu_glue_cmd:
3108 p = cur_chr;
3109 n = cur_cmd;
3110 scan_optional_equals();
3111 if (n == assign_mu_glue_cmd)
3112 scan_glue(mu_val_level);
3113 else
3114 scan_glue(glue_val_level);
3115 define(p, glue_ref_cmd, cur_val);
3116 break;
3117 case def_char_code_cmd:
3118 case def_del_code_cmd:
3119 /*tex Let |n| be the largest legal code value, based on |cur_chr| */
3120 if (cur_chr == cat_code_base)
3121 n = max_char_code;
3122 else if (cur_chr == sf_code_base)
3123 n = 077777;
3124 else
3125 n = biggest_char;
3126 p = cur_chr;
3127 if (cur_chr == math_code_base) {
3128 if (is_global(a))
3129 cur_val1 = level_one;
3130 else
3131 cur_val1 = cur_level;
3132 scan_extdef_math_code(cur_val1, tex_mathcode);
3133 } else if (cur_chr == lc_code_base) {
3134 scan_char_num();
3135 p = cur_val;
3136 scan_optional_equals();
3137 scan_int();
3138 check_def_code(lc_code_base);
3139 define_lc_code(p, cur_val);
3140 } else if (cur_chr == uc_code_base) {
3141 scan_char_num();
3142 p = cur_val;
3143 scan_optional_equals();
3144 scan_int();
3145 check_def_code(uc_code_base);
3146 define_uc_code(p, cur_val);
3147 } else if (cur_chr == sf_code_base) {
3148 scan_char_num();
3149 p = cur_val;
3150 scan_optional_equals();
3151 scan_int();
3152 check_def_code(sf_code_base);
3153 define_sf_code(p, cur_val);
3154 } else if (cur_chr == cat_code_base) {
3155 scan_char_num();
3156 p = cur_val;
3157 scan_optional_equals();
3158 scan_int();
3159 check_def_code(cat_code_base);
3160 define_cat_code(p, cur_val);
3161 } else if (cur_chr == del_code_base) {
3162 if (is_global(a))
3163 cur_val1 = level_one;
3164 else
3165 cur_val1 = cur_level;
3166 scan_extdef_del_code(cur_val1, tex_mathcode);
3167 }
3168 break;
3169 case extdef_math_code_cmd:
3170 case extdef_del_code_cmd:
3171 if (is_global(a))
3172 cur_val1 = level_one;
3173 else
3174 cur_val1 = cur_level;
3175 if (cur_chr == math_code_base)
3176 scan_extdef_math_code(cur_val1, umath_mathcode);
3177 else if (cur_chr == math_code_base + 1)
3178 scan_extdef_math_code(cur_val1, umathnum_mathcode);
3179 else if (cur_chr == del_code_base)
3180 scan_extdef_del_code(cur_val1, umath_mathcode);
3181 else if (cur_chr == del_code_base + 1)
3182 scan_extdef_del_code(cur_val1, umathnum_mathcode);
3183 break;
3184 case def_family_cmd:
3185 p = cur_chr;
3186 scan_math_family_int();
3187 cur_val1 = cur_val;
3188 scan_optional_equals();
3189 scan_font_ident();
3190 define_fam_fnt(cur_val1, p, cur_val);
3191 break;
3192 case set_math_param_cmd:
3193 p = cur_chr;
3194 get_token();
3195 if (cur_cmd != math_style_cmd) {
3196 print_err("Missing math style, treated as \\displaystyle");
3197 help1(
3198 "A style should have been here; I inserted `\\displaystyle'."
3199 );
3200 cur_val1 = display_style;
3201 back_error();
3202 } else {
3203 cur_val1 = cur_chr;
3204 }
3205 scan_optional_equals();
3206 if (p < math_param_first_mu_glue) {
3207 if (p == math_param_radical_degree_raise)
3208 scan_int();
3209 else
3210 scan_dimen(false, false, false);
3211 } else {
3212 scan_glue(mu_val_level);
3213 if (cur_val == thin_mu_skip_par)
3214 cur_val = thin_mu_skip_code;
3215 else if (cur_val == med_mu_skip_par)
3216 cur_val = med_mu_skip_code;
3217 else if (cur_val == thick_mu_skip_par)
3218 cur_val = thick_mu_skip_code;
3219 }
3220 define_math_param(p, cur_val1, cur_val);
3221 break;
3222 case register_cmd:
3223 case advance_cmd:
3224 case multiply_cmd:
3225 case divide_cmd:
3226 do_register_command(a);
3227 break;
3228 case set_box_cmd:
3229 /*tex
3230 The processing of boxes is somewhat different, because we may
3231 need to scan and create an entire box before we actually change
3232 the value of the old one.
3233 */
3234 scan_register_num();
3235 if (is_global(a))
3236 n = global_box_flag + cur_val;
3237 else
3238 n = box_flag + cur_val;
3239 scan_optional_equals();
3240 if (set_box_allowed) {
3241 scan_box(n);
3242 } else {
3243 print_err("Improper \\setbox");
3244 help3(
3245 "Sorry, \\setbox is not allowed after \\halign in a display,",
3246 "between \\accent and an accented character, or in immediate",
3247 "assignments."
3248 );
3249 error();
3250 }
3251 break;
3252 case set_aux_cmd:
3253 /*tex
3254 The |space_factor| or |prev_depth| settings are changed when a
3255 |set_aux| command is sensed. Similarly, |prev_graf| is changed in
3256 the presence of |set_prev_graf|, and |dead_cycles| or
3257 |insert_penalties| in the presence of |set_page_int|. These
3258 definitions are always global.
3259 */
3260 alter_aux();
3261 break;
3262 case set_prev_graf_cmd:
3263 alter_prev_graf();
3264 break;
3265 case set_page_dimen_cmd:
3266 alter_page_so_far();
3267 break;
3268 case set_page_int_cmd:
3269 alter_integer();
3270 break;
3271 case set_box_dimen_cmd:
3272 /*tex
3273 When some dimension of a box register is changed, the change
3274 isn't exactly global; but \TeX\ does not look at the \.{\\global}
3275 switch.
3276 */
3277 alter_box_dimen();
3278 break;
3279 case set_tex_shape_cmd:
3280 q = cur_chr;
3281 scan_optional_equals();
3282 scan_int();
3283 n = cur_val;
3284 if (n <= 0) {
3285 p = null;
3286 } else {
3287 p = new_node(shape_node, 2 * (n + 1) + 1);
3288 vinfo(p + 1) = n;
3289 for (j = 1; j <= n; j++) {
3290 scan_normal_dimen();
3291 varmem[p + 2 * j].cint = cur_val; /* indentation */
3292 scan_normal_dimen();
3293 varmem[p + 2 * j + 1].cint = cur_val; /* width */
3294 }
3295 }
3296 define(q, shape_ref_cmd, p);
3297 break;
3298 case set_etex_shape_cmd:
3299 q = cur_chr;
3300 scan_optional_equals();
3301 scan_int();
3302 n = cur_val;
3303 if (n <= 0) {
3304 p = null;
3305 } else {
3306 n = (cur_val / 2) + 1;
3307 p = new_node(shape_node, 2 * n + 1 + 1);
3308 vinfo(p + 1) = n;
3309 n = cur_val;
3310 varmem[p + 2].cint = n; /* number of penalties */
3311 for (j = p + 3; j <= p + n + 2; j++) {
3312 scan_int();
3313 varmem[j].cint = cur_val; /* penalty values */
3314 }
3315 if (!odd(n))
3316 varmem[p + n + 3].cint = 0; /* unused */
3317 }
3318 define(q, shape_ref_cmd, p);
3319 break;
3320 case hyph_data_cmd:
3321 /*tex
3322 All of \TeX's parameters are kept in |eqtb| except the font
3323 information, the interaction mode, and the hyphenation tables;
3324 these are strictly global.
3325 */
3326 switch (cur_chr) {
3327 case 0:
3328 new_hyph_exceptions();
3329 break;
3330 case 1:
3331 new_patterns();
3332 break;
3333 case 2:
3334 new_pre_hyphen_char();
3335 break;
3336 case 3:
3337 new_post_hyphen_char();
3338 break;
3339 case 4:
3340 new_pre_exhyphen_char();
3341 break;
3342 case 5:
3343 new_post_exhyphen_char();
3344 break;
3345 case 6:
3346 new_hyphenation_min();
3347 break;
3348 case 7:
3349 new_hj_code();
3350 break;
3351 }
3352 break;
3353 case assign_font_dimen_cmd:
3354 set_font_dimen();
3355 break;
3356 case assign_font_int_cmd:
3357 n = cur_chr;
3358 scan_font_ident();
3359 f = cur_val;
3360 if (n == no_lig_code) {
3361 set_no_ligatures(f);
3362 } else if (n < lp_code_base) {
3363 scan_optional_equals();
3364 scan_int();
3365 if (n == 0)
3366 set_hyphen_char(f, cur_val);
3367 else
3368 set_skew_char(f, cur_val);
3369 } else {
3370 scan_char_num();
3371 p = cur_val;
3372 scan_optional_equals();
3373 scan_int();
3374 switch (n) {
3375 case lp_code_base:
3376 set_lp_code(f, p, cur_val);
3377 break;
3378 case rp_code_base:
3379 set_rp_code(f, p, cur_val);
3380 break;
3381 case ef_code_base:
3382 set_ef_code(f, p, cur_val);
3383 break;
3384 case tag_code:
3385 set_tag_code(f, p, cur_val);
3386 break;
3387 }
3388 }
3389 break;
3390 case def_font_cmd:
3391 /*tex Here is where the information for a new font gets loaded. */
3392 tex_def_font((small_number) a);
3393 break;
3394 case def_lua_call_cmd:
3395 get_r_token();
3396 p = cur_cs;
3397 scan_optional_equals();
3398 scan_int();
3399 if (j != 0) {
3400 define(p, lua_call_cmd, cur_val);
3401 } else {
3402 define(p, lua_expandable_call_cmd, cur_val);
3403 }
3404 break;
3405 case letterspace_font_cmd:
3406 new_letterspaced_font((small_number) a);
3407 break;
3408 case copy_font_cmd:
3409 make_font_copy((small_number) a);
3410 break;
3411 case set_font_id_cmd:
3412 scan_int();
3413 if (is_valid_font(cur_val))
3414 zset_cur_font(cur_val);
3415 break ;
3416 case set_interaction_cmd:
3417 new_interaction();
3418 break;
3419 default:
3420 confusion("prefix");
3421 break;
3422 }
3423 /*tex End of assignments cases. */
3424 DONE:
3425 /*tex Insert a token saved by \.{\\afterassignment}, if any. */
3426 if (after_token != 0) {
3427 cur_tok = after_token;
3428 back_input();
3429 after_token = 0;
3430 }
3431 }
3432
3433 void fixup_directions(void)
3434 {
3435 int temp_no_whatsits = no_local_whatsits_par;
3436 int temp_no_dirs = no_local_dirs_par;
3437 int temporary_dir = text_direction_par;
3438 if (dir_level(text_dir_ptr) == cur_level) {
3439 /* Remove from |text_dir_ptr|. */
3440 halfword text_dir_tmp = vlink(text_dir_ptr);
3441 flush_node(text_dir_ptr);
3442 text_dir_ptr = text_dir_tmp;
3443 }
3444 unsave();
3445 if (abs(mode) == hmode) {
3446 if (temp_no_dirs != 0) {
3447 /* Add local dir node. */
3448 tail_append(new_dir(text_direction_par));
3449 dir_dir(tail) = temporary_dir;
3450 subtype(tail) = cancel_dir;
3451 }
3452 if (temp_no_whatsits != 0) {
3453 /*tex Add local paragraph node. */
3454 tail_append(make_local_par_node(hmode_par_par_code));
3455 }
3456 }
3457 }
3458
3459 /*tex
3460
3461 This is experimental and needs more checking!
3462
3463 */
3464
3465 void fixup_directions_only(void)
3466 {
3467 int temp_no_dirs = no_local_dirs_par;
3468 int temporary_dir = text_direction_par;
3469 if (dir_level(text_dir_ptr) == cur_level) {
3470 /* Remove from |text_dir_ptr|. */
3471 halfword text_dir_tmp = vlink(text_dir_ptr);
3472 flush_node(text_dir_ptr);
3473 text_dir_ptr = text_dir_tmp;
3474 }
3475 if (temp_no_dirs != 0) {
3476 /* Add local dir node. */
3477 tail_append(new_dir(text_direction_par));
3478 dir_dir(tail) = temporary_dir;
3479 subtype(tail) = cancel_dir;
3480 }
3481 }
3482
3483 /*tex
3484
3485 When a control sequence is to be defined, by \.{\\def} or \.{\\let} or something
3486 similar, the |get_r_token| routine will substitute a special control sequence for
3487 a token that is not redefinable.
3488
3489 */
3490
3491 void get_r_token(void)
3492 {
3493 RESTART:
3494 do {
3495 get_token();
3496 } while (cur_tok == space_token);
3497 if ((cur_cs == 0) || (cur_cs > eqtb_top) ||
3498 ((cur_cs > frozen_control_sequence) && (cur_cs <= eqtb_size))) {
3499 print_err("Missing control sequence inserted");
3500 help5(
3501 "Please don't say `\\def cs{...}', say `\\def\\cs{...}'.",
3502 "I've inserted an inaccessible control sequence so that your",
3503 "definition will be completed without mixing me up too badly.",
3504 "You can recover graciously from this error, if you're",
3505 "careful; see exercise 27.2 in The TeXbook."
3506 );
3507 if (cur_cs == 0)
3508 back_input();
3509 cur_tok = cs_token_flag + frozen_protection;
3510 ins_error();
3511 goto RESTART;
3512 }
3513 }
3514
3515 void assign_internal_value(int a, halfword p, int val)
3516 {
3517 halfword n;
3518 if ((p >= int_base) && (p < attribute_base)) {
3519 switch ((p - int_base)) {
3520 case cat_code_table_code:
3521 if (valid_catcode_table(val)) {
3522 if (val != cat_code_table_par)
3523 word_define(p, val);
3524 } else {
3525 print_err("Invalid \\catcode table");
3526 help2(
3527 "You can only switch to a \\catcode table that is initialized",
3528 "using \\savecatcodetable or \\initcatcodetable, or to table 0"
3529 );
3530 error();
3531 }
3532 break;
3533 case output_box_code:
3534 if ((val > 65535) | (val < 0)) {
3535 print_err("Invalid \\outputbox");
3536 help1(
3537 "The value for \\outputbox has to be between 0 and 65535."
3538 );
3539 error();
3540 } else {
3541 word_define(p, val);
3542 }
3543 break;
3544 case new_line_char_code:
3545 if (val > 127) {
3546 print_err("Invalid \\newlinechar");
3547 help2(
3548 "The value for \\newlinechar has to be no higher than 127.",
3549 "Your invalid assignment will be ignored."
3550 );
3551 error();
3552 } else {
3553 word_define(p, val);
3554 }
3555 break;
3556 case end_line_char_code:
3557 if (val > 127) {
3558 print_err("Invalid \\endlinechar");
3559 help2(
3560 "The value for \\endlinechar has to be no higher than 127.",
3561 "Your invalid assignment will be ignored."
3562 );
3563 error();
3564 } else {
3565 word_define(p, val);
3566 }
3567 break;
3568 case language_code:
3569 if (val < 0) {
3570 word_define(int_base + cur_lang_code, -1);
3571 word_define(p, -1);
3572 } else if (val > 16383) {
3573 print_err("Invalid \\language");
3574 help2(
3575 "The absolute value for \\language has to be no higher than 16383.",
3576 "Your invalid assignment will be ignored."
3577 );
3578 error();
3579 } else {
3580 word_define(int_base + cur_lang_code, val);
3581 word_define(p, val);
3582 }
3583 break;
3584 /* */
3585 case math_italics_mode_code:
3586 if (permit_math_obsolete) {
3587 if (math_italics_mode_par != val) {
3588 normal_warning("math", "\\mathitalicsmode is obsolete");
3589 }
3590 word_define(p, val);
3591 }
3592 break;
3593 case math_nolimits_mode_code:
3594 if (permit_math_obsolete) {
3595 if (math_nolimits_mode_par != val) {
3596 normal_warning("math", "\\mathnolimitssmode is obsolete");
3597 }
3598 word_define(p, val);
3599 }
3600 break;
3601 case math_script_char_mode_code:
3602 if (permit_math_obsolete) {
3603 if (math_script_char_mode_par != val) {
3604 normal_warning("math", "\\mathscriptcharmode is obsolete");
3605 }
3606 word_define(p, val);
3607 }
3608 break;
3609 case math_script_box_mode_code:
3610 if (permit_math_obsolete) {
3611 if (math_script_box_mode_par != val) {
3612 normal_warning("math", "\\mathscriptboxmode is obsolete");
3613 }
3614 word_define(p, val);
3615 }
3616 break;
3617 case math_flatten_mode_code:
3618 word_define(p, val);
3619 break;
3620 case math_defaults_mode_code:
3621 if (permit_math_obsolete) {
3622 if (math_defaults_mode_par != val) {
3623 normal_warning("math", "\\mathdefaultsmode is obsolete");
3624 }
3625 word_define(p, val);
3626 }
3627 break;
3628 case math_delimiters_mode_code:
3629 if (permit_math_obsolete) {
3630 if (math_delimiters_mode_par != val) {
3631 normal_warning("math", "\\mathdelimitersmode is obsolete");
3632 }
3633 word_define(p, val);
3634 }
3635 break;
3636 /* */
3637 default:
3638 word_define(p, val);
3639 break;
3640 }
3641 /*tex
3642 If we are defining subparagraph penalty levels while we are in hmode,
3643 then we put out a whatsit immediately, otherwise we leave it alone.
3644 This mechanism might not be sufficiently powerful, and some other
3645 algorithm, searching down the stack, might be necessary. Good first
3646 step.
3647 */
3648 if ((abs(mode) == hmode) &&
3649 ((p == (int_base + local_inter_line_penalty_code)) ||
3650 (p == (int_base + local_broken_penalty_code)))) {
3651 /*tex Add local paragraph node */
3652 tail_append(make_local_par_node(penalty_par_code));
3653 eq_word_define(int_base + no_local_whatsits_code, no_local_whatsits_par + 1);
3654 }
3655 } else if ((p >= dimen_base) && (p <= eqtb_size)) {
3656 if (p == (dimen_base + page_left_offset_code)) {
3657 n = val - one_true_inch;
3658 word_define(dimen_base + h_offset_code, n);
3659 } else if (p == (dimen_base + h_offset_code)) {
3660 n = val + one_true_inch;
3661 word_define(dimen_base + page_left_offset_code, n);
3662 } else if (p == (dimen_base + page_top_offset_code)) {
3663 n = val - one_true_inch;
3664 word_define(dimen_base + v_offset_code, n);
3665 } else if (p == (dimen_base + v_offset_code)) {
3666 n = val + one_true_inch;
3667 word_define(dimen_base + page_top_offset_code, n);
3668 }
3669 word_define(p, val);
3670 } else if ((p >= local_base) && (p < toks_base)) {
3671 /*tex internal locals */
3672 define(p, call_cmd, val);
3673 } else {
3674 confusion("assign internal value");
3675 }
3676 }
3677
3678 /*tex
3679
3680 We use the fact that |register| $<$ |advance| $<$ |multiply| $<$ |divide|/ We
3681 compute the register location |l| and its type |p| but |return| if invalid. Here
3682 we use the fact that the consecutive codes |int_val..mu_val| and
3683 |assign_int..assign_mu_glue| correspond to each other nicely.
3684
3685 */
3686
3687 void do_register_command(int a)
3688 {
3689 int p;
3690 halfword q = cur_cmd;
3691 halfword l = 0;
3692 if (q != register_cmd) {
3693 get_x_token();
3694 if ((cur_cmd >= assign_int_cmd) && (cur_cmd <= assign_mu_glue_cmd)) {
3695 l = cur_chr;
3696 p = cur_cmd - assign_int_cmd;
3697 goto FOUND;
3698 }
3699 if (cur_cmd != register_cmd) {
3700 print_err("You can't use `");
3701 print_cmd_chr((quarterword) cur_cmd, cur_chr);
3702 tprint("' after ");
3703 print_cmd_chr((quarterword) q, 0);
3704 help1(
3705 "I'm forgetting what you said and not changing anything."
3706 );
3707 error();
3708 return;
3709 }
3710 }
3711 p = cur_chr;
3712 scan_register_num();
3713 if (p == int_val_level)
3714 l = cur_val + count_base;
3715 else if (p == attr_val_level)
3716 l = cur_val + attribute_base;
3717 else if (p == dimen_val_level)
3718 l = cur_val + scaled_base;
3719 else if (p == glue_val_level)
3720 l = cur_val + skip_base;
3721 else if (p == mu_val_level)
3722 l = cur_val + mu_skip_base;
3723 FOUND:
3724 if (q == register_cmd) {
3725 scan_optional_equals();
3726 } else if (scan_keyword("by")) {
3727 /*tex optional `\.{by}' */
3728 }
3729 arith_error = false;
3730 if (q < multiply_cmd) {
3731 /*tex Compute result of |register| or |advance|, put it in |cur_val|. */
3732 if (p < glue_val_level) {
3733 if ((p == int_val_level) || (p == attr_val_level))
3734 scan_int();
3735 else
3736 scan_normal_dimen();
3737 if (q == advance_cmd)
3738 cur_val = cur_val + eqtb[l].cint;
3739 } else {
3740 scan_glue(p);
3741 if (q == advance_cmd) {
3742 /* Compute the sum of two glue specs */
3743 halfword r = equiv(l);
3744 q = new_spec(cur_val);
3745 flush_node(cur_val);
3746 width(q) = width(q) + width(r);
3747 if (stretch(q) == 0) {
3748 stretch_order(q) = normal;
3749 }
3750 if (stretch_order(q) == stretch_order(r)) {
3751 stretch(q) = stretch(q) + stretch(r);
3752 } else if ((stretch_order(q) < stretch_order(r)) && (stretch(r) != 0)) {
3753 stretch(q) = stretch(r);
3754 stretch_order(q) = stretch_order(r);
3755 }
3756 if (shrink(q) == 0) {
3757 shrink_order(q) = normal;
3758 }
3759 if (shrink_order(q) == shrink_order(r)) {
3760 shrink(q) = shrink(q) + shrink(r);
3761 } else if ((shrink_order(q) < shrink_order(r)) && (shrink(r) != 0)) {
3762 shrink(q) = shrink(r);
3763 shrink_order(q) = shrink_order(r);
3764 }
3765 cur_val = q;
3766 }
3767 }
3768 } else {
3769 /*tex Compute result of |multiply| or |divide|, put it in |cur_val| */
3770 scan_int();
3771 if (p < glue_val_level) {
3772 if (q == multiply_cmd) {
3773 if ((p == int_val_level) || (p == attr_val_level)) {
3774 cur_val = mult_integers(eqtb[l].cint, cur_val);
3775 } else {
3776 cur_val = nx_plus_y(eqtb[l].cint, cur_val, 0);
3777 }
3778 } else {
3779 cur_val = x_over_n(eqtb[l].cint, cur_val);
3780 }
3781 } else {
3782 halfword s = equiv(l);
3783 halfword r = new_spec(s);
3784 if (q == multiply_cmd) {
3785 width(r) = nx_plus_y(width(s), cur_val, 0);
3786 stretch(r) = nx_plus_y(stretch(s), cur_val, 0);
3787 shrink(r) = nx_plus_y(shrink(s), cur_val, 0);
3788 } else {
3789 width(r) = x_over_n(width(s), cur_val);
3790 stretch(r) = x_over_n(stretch(s), cur_val);
3791 shrink(r) = x_over_n(shrink(s), cur_val);
3792 }
3793 cur_val = r;
3794 }
3795 }
3796 if (arith_error) {
3797 print_err("Arithmetic overflow");
3798 help2(
3799 "I can't carry out that multiplication or division,",
3800 "since the result is out of range."
3801 );
3802 if (p >= glue_val_level)
3803 flush_node(cur_val);
3804 error();
3805 return;
3806 }
3807 if (p < glue_val_level) {
3808 if (p == attr_val_level) {
3809 if ((l - attribute_base) > max_used_attr)
3810 max_used_attr = (l - attribute_base);
3811 attr_list_cache = cache_disabled;
3812 }
3813 if ((p == int_val_level) || (p == dimen_val_level))
3814 assign_internal_value(a, l, cur_val);
3815 else
3816 word_define(l, cur_val);
3817 } else {
3818 define(l, glue_ref_cmd, cur_val);
3819 }
3820 }
3821
3822 void alter_aux(void)
3823 {
3824 halfword c; /* |hmode| or |vmode| */
3825 if (cur_chr != abs(mode)) {
3826 report_illegal_case();
3827 } else {
3828 c = cur_chr;
3829 scan_optional_equals();
3830 if (c == vmode) {
3831 scan_normal_dimen();
3832 prev_depth_par = cur_val;
3833 } else {
3834 scan_int();
3835 if ((cur_val <= 0) || (cur_val > 32767)) {
3836 print_err("Bad space factor");
3837 help1(
3838 "I allow only values in the range 1..32767 here."
3839 );
3840 int_error(cur_val);
3841 } else {
3842 space_factor_par = cur_val;
3843 }
3844 }
3845 }
3846 }
3847
3848 void alter_prev_graf(void)
3849 {
3850 int p = nest_ptr; /* index into |nest| */
3851 while (abs(nest[p].mode_field) != vmode)
3852 decr(p);
3853 scan_optional_equals();
3854 scan_int();
3855 if (cur_val < 0) {
3856 print_err("Bad \\prevgraf");
3857 help1(
3858 "I allow only nonnegative values here."
3859 );
3860 int_error(cur_val);
3861 } else {
3862 nest[p].pg_field = cur_val;
3863 }
3864 }
3865
3866 void alter_page_so_far(void)
3867 {
3868 int c = cur_chr; /* index into |page_so_far| */
3869 scan_optional_equals();
3870 scan_normal_dimen();
3871 page_so_far[c] = cur_val;
3872 }
3873
3874 /*tex
3875 The value of |c| is 0 for \.{\\deadcycles}, 1 for \.{\\insertpenalties}, etc.
3876 */
3877
3878 void alter_integer(void)
3879 {
3880 int c = cur_chr;
3881 scan_optional_equals();
3882 scan_int();
3883 if (c == 0) {
3884 dead_cycles = cur_val;
3885 } else if (c == 2) {
3886 if ((cur_val < batch_mode) || (cur_val > error_stop_mode)) {
3887 print_err("Bad interaction mode");
3888 help2(
3889 "Modes are 0=batch, 1=nonstop, 2=scroll, and",
3890 "3=errorstop. Proceed, and I'll ignore this case."
3891 );
3892 int_error(cur_val);
3893 } else {
3894 cur_chr = cur_val;
3895 new_interaction();
3896 }
3897 } else {
3898 insert_penalties = cur_val;
3899 }
3900 }
3901
3902 void alter_box_dimen(void)
3903 {
3904 int c; /* |width_offset| or |height_offset| or |depth_offset| */
3905 int b; /* box number */
3906 c = cur_chr;
3907 scan_register_num();
3908 b = cur_val;
3909 scan_optional_equals();
3910 scan_normal_dimen();
3911 if (box(b) != null)
3912 varmem[box(b) + c].cint = cur_val;
3913 }
3914
3915 void new_interaction(void)
3916 {
3917 print_ln();
3918 interaction = cur_chr;
3919 if (interaction == batch_mode)
3920 kpse_make_tex_discard_errors = 1;
3921 else
3922 kpse_make_tex_discard_errors = 0;
3923 fixup_selector(log_opened_global);
3924 }
3925
3926 /*tex
3927
3928 The \.{\\afterassignment} command puts a token into the global variable
3929 |after_token|. This global variable is examined just after every assignment has
3930 been performed. It's value is zero, or a saved token.
3931
3932 */
3933
3934 halfword after_token;
3935
3936 /*tex
3937
3938 Here is a procedure that might be called `Get the next non-blank non-relax
3939 non-call non-assignment token'.
3940
3941 */
3942
3943 void do_assignments(void)
3944 {
3945 while (true) {
3946 /*tex Get the next non-blank non-relax... */
3947 do {
3948 get_x_token();
3949 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
3950 if (cur_cmd <= max_non_prefixed_command)
3951 return;
3952 set_box_allowed = false;
3953 prefixed_command();
3954 set_box_allowed = true;
3955 }
3956 }
3957
3958 /*tex |cur_chr| is 1 for \.{\\openin}, 0 for \.{\\closein}: */
3959
3960 void open_or_close_in(void)
3961 {
3962 int c = cur_chr;
3963 int n;
3964 char *fn;
3965 scan_four_bit_int();
3966 n = cur_val;
3967 if (read_open[n] != closed) {
3968 lua_a_close_in(read_file[n], (n + 1));
3969 read_open[n] = closed;
3970 }
3971 if (c != 0) {
3972 scan_optional_equals();
3973 do {
3974 get_x_token();
3975 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
3976 back_input();
3977 if (cur_cmd != left_brace_cmd) {
3978 /*tex Set |cur_name| to desired file name. */
3979 scan_file_name();
3980 } else {
3981 scan_file_name_toks();
3982 }
3983 fn = pack_file_name(cur_name, cur_area, cur_ext);
3984 if (lua_a_open_in(&(read_file[n]), fn, (n + 1))) {
3985 read_open[n] = just_open;
3986 }
3987 }
3988 }
3989
3990 /*tex
3991 Has the long \.{\\errmessage} help been used?
3992 */
3993
3994 boolean long_help_seen;
3995
3996 void issue_message(void)
3997 {
3998 int old_setting; /* holds |selector| setting */
3999 int c; /* identifies \.{\\message} and \.{\\errmessage} */
4000 str_number s; /* the message */
4001 c = cur_chr;
4002 (void) scan_toks(false, true);
4003 old_setting = selector;
4004 selector = new_string;
4005 token_show(def_ref);
4006 selector = old_setting;
4007 flush_list(def_ref);
4008 str_room(1);
4009 s = make_string();
4010 if (c == 0) {
4011 /*tex Print string |s| on the terminal */
4012 if (term_offset + (int) str_length(s) > max_print_line - 2)
4013 print_ln();
4014 else if ((term_offset > 0) || (file_offset > 0))
4015 print_char(' ');
4016 print(s);
4017 update_terminal();
4018
4019 } else {
4020 /*tex
4021 Print string |s| as an error message. If \.{\\errmessage} occurs
4022 often in |scroll_mode|, without user-defined \.{\\errhelp}, we don't
4023 want to give a long help message each time. So we give a verbose
4024 explanation only once.
4025 */
4026 print_err("");
4027 print(s);
4028 if (err_help_par != null) {
4029 use_err_help = true;
4030 } else if (long_help_seen) {
4031 help1(
4032 "(That was another \\errmessage.)"
4033 );
4034 } else {
4035 if (interaction < error_stop_mode)
4036 long_help_seen = true;
4037 help4(
4038 "This error message was generated by an \\errmessage",
4039 "command, so I can't give any explicit help.",
4040 "Pretend that you're Hercule Poirot: Examine all clues,",
4041 "and deduce the truth by order and method."
4042 );
4043 }
4044 error();
4045 use_err_help = false;
4046
4047 }
4048 flush_str(s);
4049 }
4050
4051 /*tex
4052
4053 The |error| routine calls on |give_err_help| if help is requested from the
4054 |err_help| parameter.
4055
4056 */
4057
4058 void give_err_help(void)
4059 {
4060 token_show(err_help_par);
4061 }
4062
4063 /*tex
4064
4065 The \.{\\uppercase} and \.{\\lowercase} commands are implemented by building a
4066 token list and then changing the cases of the letters in it.
4067
4068 */
4069
4070 void shift_case(void)
4071 {
4072 halfword b; /* |lc_code_base| or |uc_code_base| */
4073 halfword p; /* runs through the token list */
4074 halfword t; /* token */
4075 halfword c; /* character code */
4076 halfword i; /* inbetween */
4077 b = cur_chr;
4078 p = scan_toks(false, false);
4079 p = token_link(def_ref);
4080 while (p != null) {
4081 /*tex
4082 Change the case of the token in |p|, if a change is appropriate. When
4083 the case of a |chr_code| changes, we don't change the |cmd|. We also
4084 change active characters.
4085 */
4086 t = token_info(p);
4087 if (t < cs_token_flag) {
4088 c = t % STRING_OFFSET;
4089 if (b == uc_code_base)
4090 i = get_uc_code(c);
4091 else
4092 i = get_lc_code(c);
4093 if (i != 0)
4094 set_token_info(p, t - c + i);
4095 } else if (is_active_cs(cs_text(t - cs_token_flag))) {
4096 c = active_cs_value(cs_text(t - cs_token_flag));
4097 if (b == uc_code_base)
4098 i = get_uc_code(c);
4099 else
4100 i = get_lc_code(c);
4101 if (i != 0)
4102 set_token_info(p, active_to_cs(i, true) + cs_token_flag);
4103 }
4104 p = token_link(p);
4105 }
4106 back_list(token_link(def_ref));
4107 free_avail(def_ref);
4108 }
4109
4110 /*tex
4111
4112 We come finally to the last pieces missing from |main_control|, namely the
4113 `\.{\\show}' commands that are useful when debugging.
4114
4115 */
4116
4117 void show_whatever(void)
4118 {
4119 halfword p; /* tail of a token list to show */
4120 int t; /* type of conditional being shown */
4121 int m; /* upper bound on |fi_or_else| codes */
4122 int l; /* line where that conditional began */
4123 int n; /* level of \.{\\if...\\fi} nesting */
4124 switch (cur_chr) {
4125 case show_lists:
4126 if (file_can_be_written(show_stream_par))
4127 selector = show_stream_par;
4128 begin_diagnostic();
4129 show_activities();
4130 break;
4131 case show_box_code:
4132 /*tex Show the current contents of a box. */
4133 scan_register_num();
4134 if (file_can_be_written(show_stream_par))
4135 selector = show_stream_par;
4136 begin_diagnostic();
4137 tprint_nl("> \\box");
4138 print_int(cur_val);
4139 print_char('=');
4140 if (box(cur_val) == null)
4141 tprint("void");
4142 else
4143 show_box(box(cur_val));
4144 break;
4145 case show_code:
4146 /*tex Show the current meaning of a token, then |goto common_ending|. */
4147 get_token();
4148 if (file_can_be_written(show_stream_par))
4149 selector = show_stream_par;
4150 else if (interaction == error_stop_mode)
4151 wake_up_terminal();
4152 tprint_nl("> ");
4153 if (cur_cs != 0) {
4154 sprint_cs(cur_cs);
4155 print_char('=');
4156 }
4157 print_meaning();
4158 goto COMMON_ENDING;
4159 break;
4160 /*tex Cases for |show_whatever| */
4161 case show_groups:
4162 if (file_can_be_written(show_stream_par))
4163 selector = show_stream_par;
4164 begin_diagnostic();
4165 show_save_groups();
4166 break;
4167 case show_ifs:
4168 if (file_can_be_written(show_stream_par))
4169 selector = show_stream_par;
4170 begin_diagnostic();
4171 tprint_nl("");
4172 print_ln();
4173 if (cond_ptr == null) {
4174 tprint_nl("### ");
4175 tprint("no active conditionals");
4176 } else {
4177 p = cond_ptr;
4178 n = 0;
4179 do {
4180 incr(n);
4181 p = vlink(p);
4182 } while (p != null);
4183 p = cond_ptr;
4184 t = cur_if;
4185 l = if_line;
4186 m = if_limit;
4187 do {
4188 tprint_nl("### level ");
4189 print_int(n);
4190 tprint(": ");
4191 print_cmd_chr(if_test_cmd, t);
4192 if (m == fi_code)
4193 tprint_esc("else");
4194 print_if_line(l);
4195 decr(n);
4196 t = if_limit_subtype(p);
4197 l = if_line_field(p);
4198 m = if_limit_type(p);
4199 p = vlink(p);
4200 } while (p != null);
4201 }
4202 break;
4203 default:
4204 /*tex
4205 Show the current value of some parameter or register, then |goto
4206 common_ending|.
4207 */
4208 p = the_toks();
4209 if (file_can_be_written(show_stream_par))
4210 selector = show_stream_par;
4211 else if (interaction == error_stop_mode)
4212 wake_up_terminal();
4213 tprint_nl("> ");
4214 token_show(temp_token_head);
4215 flush_list(token_link(temp_token_head));
4216 goto COMMON_ENDING;
4217 break;
4218 }
4219 /*tex Complete a potentially long \.{\\show} command: */
4220 end_diagnostic(true);
4221 print_err("OK");
4222 if (selector == term_and_log) {
4223 if (tracing_online_par <= 0) {
4224 selector = term_only;
4225 tprint(" (see the transcript file)");
4226 selector = term_and_log;
4227 }
4228 }
4229 COMMON_ENDING:
4230 if (valid_write_file(selector)) {
4231 fixup_selector(log_opened_global);
4232 return;
4233 }
4234 if (interaction < error_stop_mode) {
4235 help0();
4236 decr(error_count);
4237 } else if (tracing_online_par > 0) {
4238 help3(
4239 "This isn't an error message; I'm just \\showing something.",
4240 "Type `I\\show...' to show more (e.g., \\show\\cs,",
4241 "\\showthe\\count10, \\showbox255, \\showlists)."
4242 );
4243 } else {
4244 help5(
4245 "This isn't an error message; I'm just \\showing something.",
4246 "Type `I\\show...' to show more (e.g., \\show\\cs,",
4247 "\\showthe\\count10, \\showbox255, \\showlists).",
4248 "And type `I\\tracingonline=1\\show...' to show boxes and",
4249 "lists on your terminal as well as in the transcript file."
4250 );
4251 }
4252 error();
4253 }
4254
4255 /*tex
4256 This procedure gets things started properly:
4257 */
4258
4259 void initialize(void)
4260 {
4261 int k; /* index into |mem|, |eqtb|, etc. */
4262 /*tex
4263 Initialize whatever \TeX\ might access and set initial values of key
4264 variables
4265 */
4266 initialize_errors();
4267 initialize_arithmetic();
4268 max_used_attr = -1;
4269 attr_list_cache = cache_disabled;
4270 initialize_nesting();
4271 /*tex Start a new current page: */
4272 page_contents = empty;
4273 page_tail = page_head;
4274 #if 0
4275 vlink(page_head) = null;
4276 #endif
4277 last_glue = max_halfword;
4278 last_penalty = 0;
4279 last_kern = 0;
4280 last_node_type = -1;
4281 page_depth = 0;
4282 page_max_depth = 0;
4283
4284 initialize_equivalents();
4285 no_new_control_sequence = true; /* new identifiers are usually forbidden */
4286 init_primitives();
4287
4288 mag_set = 0;
4289 initialize_marks();
4290 initialize_read();
4291
4292 static_pdf = init_pdf_struct(static_pdf); /* should be init_backend() */
4293
4294 format_ident = 0;
4295 format_name = get_nullstr();
4296 initialize_directions();
4297 initialize_write_files();
4298 seconds_and_micros(epochseconds, microseconds);
4299 initialize_start_time(static_pdf);
4300
4301 edit_name_start = 0;
4302 stop_at_space = true;
4303
4304 if (ini_version) {
4305 /*tex Initialize table entries (done by \.{INITEX} only). */
4306 init_node_mem(500);
4307 initialize_tokens();
4308 /*tex Initialize the special list heads and constant nodes. */
4309 initialize_alignments();
4310 initialize_buildpage();
4311
4312 initialize_active();
4313
4314 set_eq_type(undefined_control_sequence, undefined_cs_cmd);
4315 set_equiv(undefined_control_sequence, null);
4316 set_eq_level(undefined_control_sequence, level_zero);
4317 for (k = null_cs; k <= (eqtb_top - 1); k++)
4318 eqtb[k] = eqtb[undefined_control_sequence];
4319 set_equiv(glue_base, zero_glue);
4320 set_eq_level(glue_base, level_one);
4321 set_eq_type(glue_base, glue_ref_cmd);
4322 for (k = glue_base + 1; k <= local_base - 1; k++) {
4323 eqtb[k] = eqtb[glue_base];
4324 }
4325 par_shape_par_ptr = null;
4326 set_eq_type(par_shape_loc, shape_ref_cmd);
4327 set_eq_level(par_shape_loc, level_one);
4328 for (k = etex_pen_base; k <= (etex_pens - 1); k++)
4329 eqtb[k] = eqtb[par_shape_loc];
4330 for (k = output_routine_loc; k <= toks_base + biggest_reg; k++)
4331 eqtb[k] = eqtb[undefined_control_sequence];
4332 box(0) = null;
4333 set_eq_type(box_base, box_ref_cmd);
4334 set_eq_level(box_base, level_one);
4335 for (k = box_base + 1; k <= (box_base + biggest_reg); k++)
4336 eqtb[k] = eqtb[box_base];
4337 cur_font_par = null_font;
4338 set_eq_type(cur_font_loc, data_cmd);
4339 set_eq_level(cur_font_loc, level_one);
4340 set_equiv(cat_code_base, 0);
4341 set_eq_type(cat_code_base, data_cmd);
4342 set_eq_level(cat_code_base, level_one);
4343 eqtb[internal_math_param_base] = eqtb[cat_code_base];
4344 eqtb[lc_code_base] = eqtb[cat_code_base];
4345 eqtb[uc_code_base] = eqtb[cat_code_base];
4346 eqtb[sf_code_base] = eqtb[cat_code_base];
4347 eqtb[math_code_base] = eqtb[cat_code_base];
4348 cat_code_table_par = 0;
4349 initialize_math_codes();
4350 initialize_text_codes();
4351 initex_cat_codes(0);
4352 for (k = '0'; k <= '9'; k++)
4353 set_math_code(k, math_use_current_family_code, 0, k, level_one);
4354 for (k = 'A'; k <= 'Z'; k++) {
4355 set_math_code(k, math_use_current_family_code, 1, k, level_one);
4356 set_math_code((k + 32), math_use_current_family_code, 1, (k + 32), level_one);
4357 set_lc_code(k, k + 32, level_one);
4358 set_lc_code(k + 32, k + 32, level_one);
4359 set_uc_code(k, k, level_one);
4360 set_uc_code(k + 32, k, level_one);
4361 set_sf_code(k, 999, level_one);
4362 }
4363 for (k = int_base; k <= attribute_base - 1; k++)
4364 eqtb[k].cint = 0;
4365 for (k = attribute_base; k <= del_code_base - 1; k++)
4366 eqtb[k].cint = UNUSED_ATTRIBUTE;
4367 mag_par = 1000;
4368 tolerance_par = 10000;
4369 hang_after_par = 1;
4370 max_dead_cycles_par = 25;
4371 math_pre_display_gap_factor_par = 2000;
4372 pre_bin_op_penalty_par = inf_penalty;
4373 /* obsolete but kept for old documents */
4374 math_script_box_mode_par = 1;
4375 math_script_char_mode_par = 1;
4376 math_flatten_mode_par = 1;
4377 math_defaults_mode_par = 1; /* was 0 in TL 2025 but set by latex to 1 */
4378 /* */
4379 pre_rel_penalty_par = inf_penalty;
4380 compound_hyphen_mode_par = 1;
4381 escape_char_par = '\\';
4382 end_line_char_par = carriage_return;
4383 set_del_code('.', 0, 0, 0, 0, level_one); /* this null delimiter is used in error recovery */
4384 ex_hyphen_char_par = '-';
4385 output_box_par = 255;
4386 for (k = dimen_base; k <= eqtb_size; k++)
4387 eqtb[k].cint = 0;
4388 page_left_offset_par = one_inch;
4389 page_top_offset_par = one_inch;
4390 page_right_offset_par = one_inch;
4391 page_bottom_offset_par = one_inch;
4392 show_stream_par = -1;
4393 ini_init_primitives();
4394 hash_used = frozen_control_sequence;
4395 hash_high = 0;
4396 cs_count = 0;
4397 set_eq_type(frozen_dont_expand, dont_expand_cmd);
4398 cs_text(frozen_dont_expand) = maketexstring("notexpanded:");
4399 set_eq_type(frozen_primitive, ignore_spaces_cmd);
4400 set_equiv(frozen_primitive, 1);
4401 set_eq_level(frozen_primitive, level_one);
4402 cs_text(frozen_primitive) = maketexstring("primitive");
4403 create_null_font();
4404 font_bytes = 0;
4405 px_dimen_par = one_bp;
4406 math_eqno_gap_step_par = 1000 ;
4407 var_fam_par = -1;
4408 cs_text(frozen_protection) = maketexstring("inaccessible");
4409 format_ident = maketexstring(" (INITEX)");
4410 cs_text(end_write) = maketexstring("endwrite");
4411 set_eq_level(end_write, level_one);
4412 set_eq_type(end_write, outer_call_cmd);
4413 set_equiv(end_write, null);
4414 /*tex Bah, this is a bad place do do this. */
4415 set_pdf_major_version(1);
4416 set_pdf_minor_version(0);
4417 }
4418 synctexoffset = int_base + synctex_code;
4419 }