Debian Codegraph @

texk/web2c/luatexdir/tex/equivalents.c

raw
1 /*
2
3 equivalents.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
26 halfword last_cs_name = null_cs;
27
28 /*tex |eqtb[p]| has just been restored or retained. */
29
30 static void diagnostic_trace(halfword p, const char *s)
31 {
32 begin_diagnostic();
33 print_char('{');
34 tprint(s);
35 print_char(' ');
36 show_eqtb(p);
37 print_char('}');
38 end_diagnostic(false);
39 }
40
41 void show_eqtb_meaning(halfword n);
42
43 /*tex
44
45 Now that we have studied the data structures for \TeX's semantic routines, we
46 ought to consider the data structures used by its syntactic routines. In other
47 words, our next concern will be the tables that \TeX\ looks at when it is
48 scanning what the user has written.
49
50 The biggest and most important such table is called |eqtb|. It holds the current
51 ``equivalents'' of things; i.e., it explains what things mean or what their
52 current values are, for all quantities that are subject to the nesting structure
53 provided by \TeX's grouping mechanism. There are six parts to |eqtb|:
54
55 1) |eqtb[null_cs]| holds the current equivalent of the zero-length control
56 sequence.
57
58 2) |eqtb[hash_base..(glue_base-1)]| holds the current equivalents of single- and
59 multiletter control sequences.
60
61 3) |eqtb[glue_base..(local_base-1)]| holds the current equivalents of glue
62 parameters like the current baselineskip.
63
64 4) |eqtb[local_base..(int_base-1)]| holds the current equivalents of local
65 halfword quantities like the current box registers, the current ``catcodes,'' the
66 current font, and a pointer to the current paragraph shape.
67
68 5) |eqtb[int_base..(dimen_base-1)]| holds the current equivalents of fullword
69 integer parameters like the current hyphenation penalty.
70
71 6) |eqtb[dimen_base..eqtb_size]| holds the current equivalents of fullword
72 dimension parameters like the current hsize or amount of hanging indentation.
73
74 Note that, for example, the current amount of baselineskip glue is determined by
75 the setting of a particular location in region~3 of |eqtb|, while the current
76 meaning of the control sequence `\.{\\baselineskip}' (which might have been
77 changed by \.{\\def} or \.{\\let}) appears in region~2.
78
79 The last two regions of |eqtb| have fullword values instead of the three fields
80 |eq_level|, |eq_type|, and |equiv|. An |eq_type| is unnecessary, but \TeX\ needs
81 to store the |eq_level| information in another array called |xeq_level|.
82
83 */
84
85 memory_word *eqtb;
86 halfword eqtb_top;
87 quarterword xeq_level[(eqtb_size + 1)];
88
89 void initialize_equivalents(void)
90 {
91 int k;
92 for (k = int_base; k <= eqtb_size; k++)
93 xeq_level[k] = level_one;
94 }
95
96 /*tex
97
98 The nested structure provided by `$\.{\char'173}\ldots\.{\char'175}$' groups in
99 \TeX\ means that |eqtb| entries valid in outer groups should be saved and
100 restored later if they are overridden inside the braces. When a new |eqtb| value
101 is being assigned, the program therefore checks to see if the previous entry
102 belongs to an outer level. In such a case, the old value is placed on the
103 |save_stack| just before the new value enters |eqtb|. At the end of a grouping
104 level, i.e., when the right brace is sensed, the |save_stack| is used to restore
105 the outer values, and the inner ones are destroyed.
106
107 Entries on the |save_stack| are of type |save_record|. The top item on this stack
108 is |save_stack[p]|, where |p=save_ptr-1|; it contains three fields called
109 |save_type|, |save_level|, and |save_value|, and it is interpreted in one of four
110 ways:
111
112 1) If |save_type(p)=restore_old_value|, then |save_value(p)| is a location in
113 |eqtb| whose current value should be destroyed at the end of the current group
114 and replaced by |save_word(p-1)| (|save_type(p-1)==saved_eqtb|). Furthermore if
115 |save_value(p)>=int_base|, then |save_level(p)| should replace the corresponding
116 entry in |xeq_level| (if |save_value(p)<int_base|, then the level is part of
117 |save_word(p-1)|).
118
119 2) If |save_type(p)=restore_zero|, then |save_value(p)| is a location in |eqtb|
120 whose current value should be destroyed at the end of the current group, when it
121 should be replaced by the current value of |eqtb[undefined_control_sequence]|.
122
123 3) If |save_type(p)=insert_token|, then |save_value(p)| is a token that should be
124 inserted into \TeX's input when the current group ends.
125
126 4) If |save_type(p)=level_boundary|, then |save_level(p)| is a code explaining
127 what kind of group we were previously in, and |save_value(p)| points to the level
128 boundary word at the bottom of the entries for that group. Furthermore,
129 |save_value(p-1)| contains the source line number at which the current level of
130 grouping was entered, this field has itself a type: |save_type(p-1)==saved_line|.
131
132 Besides this `official' use, various subroutines push temporary variables on the
133 save stack when it is handy to do so. These all have an explicit |save_type|, and
134 they are:
135
136 |saved_adjust| signifies an adjustment is beging scanned,
137 |saved_insert| an insertion is being scanned,
138 |saved_disc| the \.{\\discretionary} sublist we are working on right now,
139 |saved_boxtype| whether a \.{\\localbox} is \.{\\left} or \.{\\right},
140 |saved_textdir| a text direction to be restored,
141 |saved_eqno| diffentiates between \.{\\eqno} and \.{\\leqno},
142 |saved_choices| the \.{\\mathchoices} sublist we are working on right now,
143 |saved_math| and interrupted math list,
144 |saved_boxcontext| the box context value,
145 |saved_boxspec| the box \.{to} or \.{spread} specification,
146 |saved_boxdir| the box \.{dir} specification,
147 |saved_boxattr| the box \.{attr} specification,
148 |saved_boxpack| the box \.{pack} specification.
149
150 The global variable |cur_group| keeps track of what sort of group we are
151 currently in. Another global variable, |cur_boundary|, points to the topmost
152 |level_boundary| word. And |cur_level| is the current depth of nesting. The
153 routines are designed to preserve the condition that no entry in the |save_stack|
154 or in |eqtb| ever has a level greater than |cur_level|.
155
156 */
157
158 save_record *save_stack;
159 int save_ptr; /* first unused entry on the save stack */
160 int max_save_stack; /* maximum usage of save stack */
161 quarterword cur_level = level_one; /* current nesting level for groups */
162 group_code cur_group = bottom_level; /* current group type */
163 int cur_boundary; /* where the current level begins */
164
165 /*tex
166
167 At this time it might be a good idea for the reader to review the introduction to
168 |eqtb| that was given above just before the long lists of parameter names. Recall
169 that the ``outer level'' of the program is |level_one|, since undefined control
170 sequences are assumed to be ``defined'' at |level_zero|.
171
172 The following macro is used to test if there is room for up to eight more entries
173 on |save_stack|. By making a conservative test like this, we can get by with
174 testing for overflow in only a few places.
175
176 */
177
178 #define check_full_save_stack() do { \
179 if (save_ptr>max_save_stack) { \
180 max_save_stack=save_ptr; \
181 if (max_save_stack>save_size-8) \
182 overflow("save size",(unsigned)save_size); \
183 } \
184 } while (0)
185
186 /*tex
187
188 Procedure |new_save_level| is called when a group begins. The argument is a group
189 identification code like `|hbox_group|'. After calling this routine, it is safe
190 to put six more entries on |save_stack|.
191
192 In some cases integer-valued items are placed onto the |save_stack| just below a
193 |level_boundary| word, because this is a convenient place to keep information
194 that is supposed to ``pop up'' just when the group has finished. For example,
195 when `\.{\\hbox to 100pt}' is being treated, the 100pt dimension is stored on
196 |save_stack| just before |new_save_level| is called.
197
198 */
199
200 void new_save_level(group_code c)
201 { /*tex We begin a new level of grouping. */
202 check_full_save_stack();
203 set_saved_record(0, saved_line, 0, line);
204 incr(save_ptr);
205 save_type(save_ptr) = level_boundary;
206 save_level(save_ptr) = cur_group;
207 save_value(save_ptr) = cur_boundary;
208 if (cur_level == max_quarterword) {
209 overflow("grouping levels", max_quarterword - min_quarterword);
210 }
211 /*tex We quit if |(cur_level+1)| is too big to be stored in |eqtb|. */
212 cur_boundary = save_ptr;
213 cur_group = c;
214 if (tracing_groups_par > 0)
215 group_trace(false);
216 incr(cur_level);
217 incr(save_ptr);
218 }
219
220 static const char *save_stack_type(int v)
221 {
222 const char *s = "";
223 switch (save_type(v)) {
224 case restore_old_value: s = "restore_old_value"; break;
225 case restore_zero: s = "restore_zero"; break;
226 case insert_token: s = "insert_token"; break;
227 case level_boundary: s = "level_boundary"; break;
228 case saved_line: s = "saved_line"; break;
229 case saved_adjust: s = "saved_adjust"; break;
230 case saved_insert: s = "saved_insert"; break;
231 case saved_disc: s = "saved_disc"; break;
232 case saved_boxtype: s = "saved_boxtype"; break;
233 case saved_textdir: s = "saved_textdir"; break;
234 case saved_eqno: s = "saved_eqno"; break;
235 case saved_choices: s = "saved_choices"; break;
236 case saved_math: s = "saved_math"; break;
237 case saved_boxcontext: s = "saved_boxcontext"; break;
238 case saved_boxspec: s = "saved_boxspec"; break;
239 case saved_boxdir: s = "saved_boxdir"; break;
240 case saved_boxattr: s = "saved_boxattr"; break;
241 case saved_boxpack: s = "saved_boxpack"; break;
242 case saved_attrlist: s = "saved_attrlist"; break;
243 case saved_eqtb: s = "saved_eqtb"; break;
244 default: break;
245 }
246 return s;
247 }
248
249 void print_save_stack(void)
250 {
251 int i;
252 begin_diagnostic();
253 selector = term_and_log;
254 print_ln();
255 for (i = (save_ptr - 1); i >= 0; i--) {
256 tprint("save_stack[");
257 if (i < 100)
258 print_char(' ');
259 if (i < 10)
260 print_char(' ');
261 print_int(i);
262 tprint("]: ");
263 tprint(save_stack_type(i));
264 switch (save_type(i)) {
265 case restore_old_value:
266 tprint(", ");
267 show_eqtb_meaning(save_value(i));
268 tprint("=");
269 if (save_value(i) >= int_base) {
270 print_int(save_word(i - 1).cint);
271 } else {
272 print_int(eq_type_field(save_word(i - 1)));
273 print_char(',');
274 print_int(equiv_field(save_word(i - 1)));
275 }
276 i--;
277 break;
278 case restore_zero:
279 tprint(", ");
280 show_eqtb_meaning(save_value(i));
281 break;
282 case insert_token:
283 tprint(", ");
284 {
285 halfword p = get_avail();
286 set_token_info(p, save_value(i));
287 show_token_list(p, null, 1);
288 free_avail(p);
289 }
290 break;
291 case level_boundary:
292 tprint(", old group=");
293 print_int(save_level(i));
294 tprint(", boundary = ");
295 print_int(save_value(i));
296 tprint(", line = ");
297 print_int(save_value(i - 1));
298 i--;
299 break;
300 case saved_adjust:
301 tprint(", ");
302 /*tex vadjust vs vadjust pre */
303 print_int(save_level(i));
304 break;
305 case saved_insert:
306 tprint(", ");
307 /*tex insert number */
308 print_int(save_value(i));
309 break;
310 case saved_boxtype:
311 /*tex \.{\\localleftbox} vs \.{\\localrightbox} */
312 tprint(", ");
313 print_int(save_value(i));
314 break;
315 case saved_eqno:
316 /*tex \.{\\eqno} vs \.{\\leqno} */
317 tprint(", ");
318 print_int(save_value(i));
319 break;
320 case saved_disc:
321 case saved_choices:
322 tprint(", ");
323 print_int(save_value(i));
324 break;
325 case saved_math:
326 tprint(", listptr=");
327 print_int(save_value(i));
328 break;
329 case saved_boxcontext:
330 tprint(", ");
331 print_int(save_value(i));
332 break;
333 case saved_boxspec:
334 tprint(", spec=");
335 print_int(save_level(i));
336 tprint(", dimen=");
337 print_int(save_value(i));
338 break;
339 case saved_textdir:
340 case saved_boxdir:
341 tprint(", ");
342 print_dir_text(save_value(i));
343 break;
344 case saved_boxattr:
345 case saved_boxpack:
346 case saved_attrlist:
347 tprint(", ");
348 print_int(save_value(i));
349 break;
350 case saved_line:
351 case saved_eqtb:
352 break;
353 default:
354 break;
355 }
356 print_ln();
357 }
358 end_diagnostic(true);
359 }
360
361 /*tex
362
363 The \.{\\showgroups} command displays all currently active grouping levels.
364
365 The modifications of \TeX\ required for the display produced by the
366 |show_save_groups| procedure were first discussed by Donald~E. Knuth in {\sl
367 TUGboat\/} {\bf 11}, 165--170 and 499--511, 1990. @^Knuth, Donald Ervin@>
368
369 In order to understand a group type we also have to know its mode. Since
370 unrestricted horizontal modes are not associated with grouping, they are skipped
371 when traversing the semantic nest.
372
373 */
374
375 void show_save_groups(void)
376 {
377 int p = nest_ptr;
378 int m;
379 save_pointer v = save_ptr;
380 quarterword l = cur_level;
381 group_code c = cur_group;
382 int a = 1; /* to keep track of alignments */
383 int i;
384 quarterword j;
385 const char *s = NULL;
386 save_ptr = cur_boundary;
387 decr(cur_level);
388 tprint_nl("");
389 print_ln();
390 while (1) {
391 tprint_nl("### ");
392 print_group(true);
393 if (cur_group == bottom_level)
394 goto DONE;
395 do {
396 m = nest[p].mode_field;
397 if (p > 0)
398 decr(p);
399 else
400 m = vmode;
401 } while (m == hmode);
402 tprint(" (");
403 switch (cur_group) {
404 case simple_group:
405 incr(p);
406 goto FOUND2;
407 break;
408 case hbox_group:
409 case adjusted_hbox_group:
410 s = "hbox";
411 break;
412 case vbox_group:
413 s = "vbox";
414 break;
415 case vtop_group:
416 s = "vtop";
417 break;
418 case align_group:
419 if (a == 0) {
420 if (m == -vmode)
421 s = "halign";
422 else
423 s = "valign";
424 a = 1;
425 goto FOUND1;
426 } else {
427 if (a == 1)
428 tprint("align entry");
429 else
430 tprint_esc("cr");
431 if (p >= a)
432 p = p - a;
433 a = 0;
434 goto FOUND;
435 }
436 break;
437 case no_align_group:
438 incr(p);
439 a = -1;
440 tprint_esc("noalign");
441 goto FOUND2;
442 break;
443 case output_group:
444 tprint_esc("output");
445 goto FOUND;
446 break;
447 case math_group:
448 goto FOUND2;
449 break;
450 case disc_group:
451 tprint_esc("discretionary");
452 for (i = 1; i < 3; i++) {
453 if (i <= saved_value(-2)) {
454 tprint("{}");
455 }
456 }
457 goto FOUND2;
458 break;
459 case math_choice_group:
460 tprint_esc("mathchoice");
461 for (i = 1; i < 4; i++) {
462 /*tex A different offset because |-2==saved_textdir|: */
463 if (i <= saved_value(-3)) {
464 tprint("{}");
465 }
466 }
467 goto FOUND2;
468 break;
469 case insert_group:
470 if (saved_type(-1) == saved_adjust) {
471 tprint_esc("vadjust");
472 if (saved_level(-1) != 0)
473 tprint(" pre");
474 } else {
475 tprint_esc("insert");
476 print_int(saved_value(-1));
477 }
478 goto FOUND2;
479 break;
480 case vcenter_group:
481 s = "vcenter";
482 goto FOUND1;
483 break;
484 case semi_simple_group:
485 incr(p);
486 tprint_esc("begingroup");
487 goto FOUND;
488 break;
489 case math_shift_group:
490 if (m == mmode) {
491 print_char('$');
492 } else if (nest[p].mode_field == mmode) {
493 print_cmd_chr(eq_no_cmd, saved_value(-2));
494 goto FOUND;
495 }
496 print_char('$');
497 goto FOUND;
498 break;
499 case math_left_group:
500 if (subtype(nest[p + 1].eTeX_aux_field) == left_noad_side)
501 tprint_esc("left");
502 else
503 tprint_esc("middle");
504 goto FOUND;
505 break;
506 default:
507 confusion("showgroups");
508 break;
509 }
510 /*tex Show the box context */
511 i = saved_value(-5);
512 if (i != 0) {
513 if (i < box_flag) {
514 if (abs(nest[p].mode_field) == vmode)
515 j = hmove_cmd;
516 else
517 j = vmove_cmd;
518 if (i > 0)
519 print_cmd_chr(j, 0);
520 else
521 print_cmd_chr(j, 1);
522 print_scaled(abs(i));
523 tprint("pt");
524 } else if (i < ship_out_flag) {
525 if (i >= global_box_flag) {
526 tprint_esc("global");
527 i = i - (global_box_flag - box_flag);
528 }
529 tprint_esc("setbox");
530 print_int(i - box_flag);
531 print_char('=');
532 } else {
533 print_cmd_chr(leader_ship_cmd, i - (leader_flag - a_leaders));
534 }
535 }
536 FOUND1:
537 tprint_esc(s);
538 /*tex Show the box packaging info. The offsets may vary. */
539 {
540 int ii = -1;
541 while (saved_type(ii) != saved_boxspec)
542 ii--;
543 if (saved_value(ii) != 0) {
544 print_char(' ');
545 if (saved_level(ii) == exactly)
546 tprint("to");
547 else
548 tprint("spread");
549 print_scaled(saved_value(ii));
550 tprint("pt");
551 }
552 }
553 FOUND2:
554 print_char('{');
555 FOUND:
556 print_char(')');
557 decr(cur_level);
558 cur_group = save_level(save_ptr);
559 save_ptr = save_value(save_ptr);
560 }
561 DONE:
562 save_ptr = v;
563 cur_level = l;
564 cur_group = c;
565 }
566
567 /*tex
568
569 Just before an entry of |eqtb| is changed, the following procedure should be
570 called to update the other data structures properly. It is important to keep in
571 mind that reference counts in |mem| include references from within |save_stack|,
572 so these counts must be handled carefully. @^reference counts@>
573
574 We don't need to destroy when an assignment has the same node:
575
576 */
577
578 void eq_destroy(memory_word w)
579 {
580 halfword q;
581 switch (eq_type_field(w)) {
582 case call_cmd:
583 case long_call_cmd:
584 case outer_call_cmd:
585 case long_outer_call_cmd:
586 delete_token_ref(equiv_field(w));
587 break;
588 case glue_ref_cmd:
589 flush_node(equiv_field(w));
590 break;
591 case shape_ref_cmd:
592 q = equiv_field(w);
593 if (q != null) {
594 /*tex
595 We need to free a \.{\\parshape} block. Such a block is
596 |2n+1| words long, where |n=vinfo(q)|. It happens in the
597 flush function.
598 */
599 flush_node(q);
600 }
601 break;
602 case box_ref_cmd:
603 flush_node_list(equiv_field(w));
604 break;
605 default:
606 break;
607 }
608 }
609
610 /*tex
611
612 To save a value of |eqtb[p]| that was established at level |l|, we can use the
613 following subroutine.
614
615 */
616 void eq_save(halfword p, quarterword l)
617 {
618 check_full_save_stack();
619 if (l == level_zero) {
620 save_type(save_ptr) = restore_zero;
621 } else {
622 save_word(save_ptr) = eqtb[p];
623 save_type(save_ptr) = saved_eqtb;
624 incr(save_ptr);
625 save_type(save_ptr) = restore_old_value;
626 }
627 save_level(save_ptr) = l;
628 save_value(save_ptr) = p;
629 incr(save_ptr);
630 }
631
632 /*tex
633
634 The procedure |eq_define| defines an |eqtb| entry having specified |eq_type| and
635 |equiv| fields, and saves the former value if appropriate. This procedure is used
636 only for entries in the first four regions of |eqtb|, i.e., only for entries that
637 have |eq_type| and |equiv| fields. After calling this routine, it is safe to put
638 four more entries on |save_stack|, provided that there was room for four more
639 entries before the call, since |eq_save| makes the necessary test.
640
641 */
642
643 void eq_define(halfword p, quarterword t, halfword e)
644 {
645 boolean trace = tracing_assigns_par > 0;
646 if ((eq_type(p) == t) && (equiv(p) == e)) {
647 if (trace)
648 diagnostic_trace(p, "reassigning");
649 eq_destroy(eqtb[p]);
650 return;
651 }
652 if (trace)
653 diagnostic_trace(p, "changing");
654 if (eq_level(p) == cur_level)
655 eq_destroy(eqtb[p]);
656 else if (cur_level > level_one)
657 eq_save(p, eq_level(p));
658 set_eq_level(p, cur_level);
659 set_eq_type(p, t);
660 set_equiv(p, e);
661 if (trace)
662 diagnostic_trace(p, "into");
663 }
664
665 /*tex
666
667 The counterpart of |eq_define| for the remaining (fullword) positions in |eqtb|
668 is called |eq_word_define|. Since |xeq_level[p]>=level_one| for all |p|, a
669 `|restore_zero|' will never be used in this case.
670
671 */
672
673 void eq_word_define(halfword p, int w)
674 {
675 boolean trace = tracing_assigns_par > 0;
676 if (eqtb[p].cint == w) {
677 if (trace)
678 diagnostic_trace(p, "reassigning");
679 return;
680 }
681 if (trace)
682 diagnostic_trace(p, "changing");
683 if (xeq_level[p] != cur_level) {
684 eq_save(p, xeq_level[p]);
685 xeq_level[p] = cur_level;
686 }
687 eqtb[p].cint = w;
688 if (trace)
689 diagnostic_trace(p, "into");
690 }
691
692 /*tex
693
694 The |eq_define| and |eq_word_define| routines take care of local definitions.
695 @^global definitions@> Global definitions are done in almost the same way, but
696 there is no need to save old values, and the new value is associated with
697 |level_one|.
698
699 */
700
701 void geq_define(halfword p, quarterword t, halfword e)
702 {
703 boolean trace = tracing_assigns_par > 0;
704 if (trace)
705 diagnostic_trace(p, "globally changing");
706 eq_destroy(eqtb[p]);
707 set_eq_level(p, level_one);
708 set_eq_type(p, t);
709 set_equiv(p, e);
710 if (trace)
711 diagnostic_trace(p, "into");
712 }
713
714 void geq_word_define(halfword p, int w)
715 {
716 boolean trace = tracing_assigns_par > 0;
717 if (trace)
718 diagnostic_trace(p, "globally changing");
719 eqtb[p].cint = w;
720 xeq_level[p] = level_one;
721 if (trace)
722 diagnostic_trace(p, "into");
723 }
724
725 /*tex
726
727 Subroutine |save_for_after| puts a token on the stack for save-keeping.
728
729 */
730
731 void save_for_after(halfword t)
732 {
733 if (cur_level > level_one) {
734 check_full_save_stack();
735 save_type(save_ptr) = insert_token;
736 save_level(save_ptr) = level_zero;
737 save_value(save_ptr) = t;
738 incr(save_ptr);
739 }
740 }
741
742 /*tex
743
744 The |unsave| routine goes the other way, taking items off of |save_stack|. This
745 routine takes care of restoration when a level ends; everything belonging to the
746 topmost group is cleared off of the save stack.
747
748 */
749
750 void unsave(void)
751 {
752 halfword p;
753 quarterword l = level_one;
754 /*tex Variable |a| registers if we already have processed an \.{\\aftergroup}. */
755 boolean a = false;
756 unsave_math_codes(cur_level);
757 unsave_cat_codes(cat_code_table_par, cur_level);
758 unsave_text_codes(cur_level);
759 unsave_math_data(cur_level);
760 if (cur_level > level_one) {
761 boolean trace = tracing_restores_par > 0;
762 decr(cur_level);
763 /*tex Clear off top level from |save_stack|. */
764 while (true) {
765 decr(save_ptr);
766 if (save_type(save_ptr) == level_boundary)
767 break;
768 p = save_value(save_ptr);
769 if (save_type(save_ptr) == insert_token) {
770 reinsert_token(a, p);
771 /*tex always |true| as we are always in \ETEX\ now. */
772 a = true;
773 } else {
774 if (save_type(save_ptr) == restore_old_value) {
775 l = save_level(save_ptr);
776 decr(save_ptr);
777 } else {
778 save_word(save_ptr) = eqtb[undefined_control_sequence];
779 }
780 /*tex
781 Store |save_stack[save_ptr]| in |eqtb[p]|, unless |eqtb[p]|
782 holds a global value A global definition, which sets the
783 level to |level_one|, will not be undone by |unsave|. If at
784 least one global definition of |eqtb[p]| has been carried out
785 within the group that just ended, the last such definition
786 will therefore survive.
787 */
788 if (p < int_base || p > eqtb_size) {
789 if (eq_level(p) == level_one) {
790 /*tex Destroy the saved value: */
791 eq_destroy(save_word(save_ptr));
792 if (trace)
793 diagnostic_trace(p, "retaining");
794 } else {
795 /*tex Destroy the current value: */
796 eq_destroy(eqtb[p]);
797 /*tex Restore the saved value: */
798 eqtb[p] = save_word(save_ptr);
799 if (trace)
800 diagnostic_trace(p, "restoring");
801 }
802 } else if (xeq_level[p] != level_one) {
803 eqtb[p] = save_word(save_ptr);
804 xeq_level[p] = l;
805 if (trace)
806 diagnostic_trace(p, "restoring");
807 } else {
808 if (trace)
809 diagnostic_trace(p, "retaining");
810 }
811 }
812 }
813
814 if (tracing_groups_par > 0) {
815 group_trace(true);
816 }
817 if (grp_stack[in_open] == cur_boundary) {
818 /*tex Groups are possibly not properly nested with files. */
819 group_warning();
820 }
821 cur_group = save_level(save_ptr);
822 cur_boundary = save_value(save_ptr);
823 decr(save_ptr);
824 } else {
825 /*tex |unsave| is not used when |cur_group=bottom_level| */
826 confusion("curlevel");
827 }
828 attr_list_cache = cache_disabled;
829 }
830
831 /*tex
832
833 Most of the parameters kept in |eqtb| can be changed freely, but there's an
834 exception: The magnification should not be used with two different values during
835 any \TeX\ job, since a single magnification is applied to an entire run. The
836 global variable |mag_set| is set to the current magnification whenever it becomes
837 necessary to ``freeze'' it at a particular value.
838
839 The |prepare_mag| subroutine is called whenever \TeX\ wants to use |mag| for
840 magnification. If nonzero, this magnification should be used henceforth. We might
841 drop magnifaction at some point.
842
843 */
844
845 int mag_set = 0;
846
847 void prepare_mag(void)
848 {
849 if ((mag_set > 0) && (mag_par != mag_set)) {
850 print_err("Incompatible magnification (");
851 print_int(mag_par);
852 tprint(");");
853 tprint_nl(" the previous value will be retained");
854 help2(
855 "I can handle only one magnification ratio per job. So I've",
856 "reverted to the magnification you used earlier on this run."
857 );
858 int_error(mag_set);
859 geq_word_define(int_base + mag_code, mag_set); /* |mag:=mag_set| */
860 }
861 if ((mag_par <= 0) || (mag_par > 32768)) {
862 print_err("Illegal magnification has been changed to 1000");
863 help1(
864 "The magnification ratio must be between 1 and 32768."
865 );
866 int_error(mag_par);
867 geq_word_define(int_base + mag_code, 1000);
868 }
869 if ((mag_set == 0) && (mag_par != mag_set)) {
870 if (mag_par != 1000)
871 one_true_inch = xn_over_d(one_hundred_inch, 10, mag_par);
872 else
873 one_true_inch = one_inch;
874 }
875 mag_set = mag_par;
876 }
877
878 /*tex
879
880 Let's pause a moment now and try to look at the Big Picture. The \TeX\ program
881 consists of three main parts: syntactic routines, semantic routines, and output
882 routines. The chief purpose of the syntactic routines is to deliver the user's
883 input to the semantic routines, one token at a time. The semantic routines act as
884 an interpreter responding to these tokens, which may be regarded as commands. And
885 the output routines are periodically called on to convert box-and-glue lists into
886 a compact set of instructions that will be sent to a typesetter. We have
887 discussed the basic data structures and utility routines of \TeX, so we are good
888 and ready to plunge into the real activity by considering the syntactic routines.
889
890 Our current goal is to come to grips with the |get_next| procedure, which is the
891 keystone of \TeX's input mechanism. Each call of |get_next| sets the value of
892 three variables |cur_cmd|, |cur_chr|, and |cur_cs|, representing the next input
893 token.
894
895 $$
896 \vbox{\halign{#\hfil\cr
897 \hbox{|cur_cmd| denotes a command code from the long list of codes given above;}\cr
898 \hbox{|cur_chr| denotes a character code or other modifier of the command code;}\cr
899 \hbox{|cur_cs| is the |eqtb| location of the current control sequence,}\cr
900 \hbox{\qquad if the current token was a control sequence, otherwise it's zero.}\cr}}
901 $$
902
903 Underlying this external behavior of |get_next| is all the machinery necessary to
904 convert from character files to tokens. At a given time we may be only partially
905 finished with the reading of several files (for which \.{\\input} was specified),
906 and partially finished with the expansion of some user-defined macros and/or some
907 macro parameters, and partially finished with the generation of some text in a
908 template for \.{\\halign}, and so on. When reading a character file, special
909 characters must be classified as math delimiters, etc.; comments and extra blank
910 spaces must be removed, paragraphs must be recognized, and control sequences must
911 be found in the hash table. Furthermore there are occasions in which the scanning
912 routines have looked ahead for a word like `\.{plus}' but only part of that word
913 was found, hence a few characters must be put back into the input and scanned
914 again.
915
916 To handle these situations, which might all be present simultaneously, \TeX\ uses
917 various stacks that hold information about the incomplete activities, and there
918 is a finite state control for each level of the input mechanism. These stacks
919 record the current state of an implicitly recursive process, but the |get_next|
920 procedure is not recursive. Therefore it will not be difficult to translate these
921 algorithms into low-level languages that do not support recursion.
922
923 In general, |cur_cmd| is the current command as set by |get_next|, while
924 |cur_chr| is the operand of the current command. The control sequence found here
925 is registered in |cur_cs| and is zero if none found. The |cur_tok| variable
926 contains the packed representative of |cur_cmd| and |cur_chr| and like the other
927 ones is global.
928
929 */
930
931 int cur_cmd;
932 halfword cur_chr;
933 halfword cur_cs;
934 halfword cur_tok;
935
936 /*tex
937
938 Here is a procedure that displays the current command. The variable |n| holds the
939 level of \.{\\if...\\fi} nesting and |l| the line where \.{\\if} started.
940
941 */
942
943 void show_cur_cmd_chr(void)
944 {
945 int n, l;
946 halfword p;
947 begin_diagnostic();
948 tprint_nl("{");
949 if (mode_par != shown_mode) {
950 print_mode(mode_par);
951 tprint(": ");
952 shown_mode = mode_par;
953 }
954 print_cmd_chr((quarterword) cur_cmd, cur_chr);
955 if (tracing_ifs_par > 0) {
956 if (cur_cmd >= if_test_cmd) {
957 if (cur_cmd <= fi_or_else_cmd) {
958 tprint(": ");
959 if (cur_cmd == fi_or_else_cmd) {
960 print_cmd_chr(if_test_cmd, cur_if);
961 print_char(' ');
962 n = 0;
963 l = if_line;
964 } else {
965 n = 1;
966 l = line;
967 }
968 p = cond_ptr;
969 while (p != null) {
970 incr(n);
971 p = vlink(p);
972 }
973 tprint("(level ");
974 print_int(n);
975 print_char(')');
976 print_if_line(l);
977 }
978 }
979 }
980 print_char('}');
981 end_diagnostic(false);
982 }
983
984 /*tex
985
986 Here is a procedure that displays the contents of |eqtb[n]| symbolically.
987
988 */
989
990 void show_eqtb(halfword n)
991 {
992 if (n < null_cs) {
993 /*tex
994 This can't happen in a pure \TEX\ run, but careless usage of tokens
995 at the \LUA\ end can make you end up here.
996 */
997 tprint("? bad token, case 1: ");
998 print_int(n);
999 } else if ((n < glue_base) || ((n > eqtb_size) && (n <= eqtb_top))) {
1000 /*tex
1001 This routine show the current meaning of |eqtb| entry |n| in region 1 or 2.
1002 Similar routines for the other regions will appear below.
1003 */
1004 sprint_cs(n);
1005 print_char('=');
1006 print_cmd_chr(eq_type(n), equiv(n));
1007 if (eq_type(n) >= call_cmd) {
1008 print_char(':');
1009 show_token_list(token_link(equiv(n)), null, 32);
1010 }
1011 } else if (n < local_base) {
1012 /*tex
1013 Here we show equivalent |n| in region 3. All glue parameters and registers
1014 are initially `\.{0pt plus0pt minus0pt}'.
1015 */
1016 if (n < skip_base) {
1017 if (n < glue_base + thin_mu_skip_code)
1018 print_cmd_chr(assign_glue_cmd, n);
1019 else
1020 print_cmd_chr(assign_mu_glue_cmd, n);
1021 print_char('=');
1022 if (n < glue_base + thin_mu_skip_code)
1023 print_spec(equiv(n), "pt");
1024 else
1025 print_spec(equiv(n), "mu");
1026 } else if (n < mu_skip_base) {
1027 tprint_esc("skip");
1028 print_int(n - skip_base);
1029 print_char('=');
1030 print_spec(equiv(n), "pt");
1031 } else {
1032 tprint_esc("muskip");
1033 print_int(n - mu_skip_base);
1034 print_char('=');
1035 print_spec(equiv(n), "mu");
1036 }
1037
1038 } else if (n < int_base) {
1039 /*tex
1040 We're now at equivalent |n| in region 4. First we initialize most
1041 things to null or undefined values. An undefined font is represented
1042 by the internal code |font_base|.
1043
1044 However, the character code tables are given initial values based on
1045 the conventional interpretation of ASCII code. These initial values
1046 should not be changed when \TeX\ is adapted for use with non-English
1047 languages; all changes to the initialization conventions should be
1048 made in format packages, not in \TeX\ itself, so that global
1049 interchange of formats is possible.
1050 */
1051 if ((n == par_shape_loc) || ((n >= etex_pen_base) && (n < etex_pens))) {
1052 if (n == par_shape_loc)
1053 print_cmd_chr(set_tex_shape_cmd, n);
1054 else
1055 print_cmd_chr(set_etex_shape_cmd, n);
1056 print_char('=');
1057 if (equiv(n) == null) {
1058 print_char('0');
1059 } else if (n > par_shape_loc) {
1060 print_int(penalty(equiv(n)));
1061 print_char(' ');
1062 print_int(penalty(equiv(n) + 1));
1063 if (penalty(equiv(n)) > 1)
1064 tprint_esc("ETC.");
1065 } else {
1066 print_int(vinfo(par_shape_par_ptr + 1));
1067 }
1068 } else if (n < toks_base) {
1069 print_cmd_chr(assign_toks_cmd, n);
1070 print_char('=');
1071 if (equiv(n) != null)
1072 show_token_list(token_link(equiv(n)), null, 32);
1073 } else if (n < box_base) {
1074 tprint_esc("toks");
1075 print_int(n - toks_base);
1076 print_char('=');
1077 if (equiv(n) != null)
1078 show_token_list(token_link(equiv(n)), null, 32);
1079 } else if (n < cur_font_loc) {
1080 tprint_esc("box");
1081 print_int(n - box_base);
1082 print_char('=');
1083 if (equiv(n) == null) {
1084 tprint("void");
1085 } else {
1086 depth_threshold = 0;
1087 breadth_max = 1;
1088 show_node_list(equiv(n));
1089 }
1090 } else if (n == cur_font_loc) {
1091 /*tex
1092 Let's show the font identifier in |eqtb[n]|, that's
1093 |font_id_text(equiv(n))|
1094 */
1095 tprint("current font");
1096 print_char('=');
1097 print_esc(hash[font_id_base + equiv(n)].rh);
1098 }
1099 } else if (n < dimen_base) {
1100 /*tex Show equivalent |n| in region 5: */
1101 if (n < dir_base) {
1102 print_cmd_chr(assign_int_cmd, n);
1103 print_char('=');
1104 print_int(eqtb[n].cint);
1105 } else if (n < count_base) {
1106 print_cmd_chr(assign_dir_cmd, n);
1107 print_char(' ');
1108 print_dir_par(eqtb[n].cint);
1109 } else if (n < attribute_base) {
1110 tprint_esc("count");
1111 print_int(n - count_base);
1112 print_char('=');
1113 print_int(eqtb[n].cint);
1114 } else if (n < del_code_base) {
1115 tprint_esc("attribute");
1116 print_int(n - attribute_base);
1117 print_char('=');
1118 print_int(eqtb[n].cint);
1119 }
1120 } else if (n <= eqtb_size) {
1121 /*tex Show equivalent |n| in region 6: */
1122 if (n < scaled_base) {
1123 print_cmd_chr(assign_dimen_cmd, n);
1124 } else {
1125 tprint_esc("dimen");
1126 print_int(n - scaled_base);
1127 }
1128 print_char('=');
1129 print_scaled(eqtb[n].cint);
1130 tprint("pt");
1131 } else {
1132 /*tex This can't happen unless you messed up at the \LUA\ end. */
1133 tprint("? bad token, case 2: ");
1134 print_int(n);
1135 }
1136 }
1137
1138 void show_eqtb_meaning(halfword n)
1139 {
1140 if (n < null_cs) {
1141 /*tex This can't happen. */
1142 print_char('?');
1143 } else if ((n < glue_base) || ((n > eqtb_size) && (n <= eqtb_top))) {
1144 /*tex
1145 Here is a routine that displays the current meaning of an |eqtb|
1146 entry in region 1 or~2. Similar routines for the other regions will
1147 appear below.
1148 */
1149 sprint_cs(n);
1150 } else if (n < local_base) {
1151 /*
1152 Show equivalent |n| in region 3. All glue parameters and registers
1153 are initially `\.{0pt plus0pt minus0pt}'.
1154 */
1155 if (n < skip_base) {
1156 if (n < glue_base + thin_mu_skip_code)
1157 print_cmd_chr(assign_glue_cmd, n);
1158 else
1159 print_cmd_chr(assign_mu_glue_cmd, n);
1160 } else if (n < mu_skip_base) {
1161 tprint_esc("skip");
1162 print_int(n - skip_base);
1163 } else {
1164 tprint_esc("muskip");
1165 print_int(n - mu_skip_base);
1166 }
1167 } else if (n < int_base) {
1168 /*tex
1169 Show equivalent |n| in region 4. We initialize most things to null or
1170 undefined values. An undefined font is represented by the internal
1171 code |font_base|. However, the character code tables are given
1172 initial values based on the conventional interpretation of ASCII
1173 code. These initial values should not be changed when \TeX\ is
1174 adapted for use with non-English languages; all changes to the
1175 initialization conventions should be made in format packages, not in
1176 \TeX\ itself, so that global interchange of formats is possible.
1177 */
1178 if ((n == par_shape_loc) || ((n >= etex_pen_base) && (n < etex_pens))) {
1179 if (n == par_shape_loc)
1180 print_cmd_chr(set_tex_shape_cmd, n);
1181 else
1182 print_cmd_chr(set_etex_shape_cmd, n);
1183 } else if (n < toks_base) {
1184 print_cmd_chr(assign_toks_cmd, n);
1185 } else if (n < box_base) {
1186 tprint_esc("toks");
1187 print_int(n - toks_base);
1188 } else if (n < cur_font_loc) {
1189 tprint_esc("box");
1190 print_int(n - box_base);
1191 } else if (n == cur_font_loc) {
1192 /*tex Show the font identifier in |eqtb[n]|. */
1193 tprint("current font");
1194 }
1195 } else if (n < dimen_base) {
1196 /*tex Show equivalent |n| in region 5. */
1197 if (n < dir_base) {
1198 print_cmd_chr(assign_int_cmd, n);
1199 } else if (n < count_base) {
1200 print_cmd_chr(assign_dir_cmd, n);
1201 } else if (n < attribute_base) {
1202 tprint_esc("count");
1203 print_int(n - count_base);
1204 } else if (n < del_code_base) {
1205 tprint_esc("attribute");
1206 print_int(n - attribute_base);
1207 }
1208 } else if (n <= eqtb_size) {
1209 /*tex Show equivalent |n| in region 6. */
1210 if (n < scaled_base) {
1211 print_cmd_chr(assign_dimen_cmd, n);
1212 } else {
1213 tprint_esc("dimen");
1214 print_int(n - scaled_base);
1215 }
1216 } else {
1217 /*tex This can't happen either. */
1218 print_char('?');
1219 }
1220 }