Debian Codegraph @

texk/web2c/luatexdir/tex/scanning.c

raw
1 /*
2
3 Copyright 2009-2012 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under the terms
8 of the GNU General Public License as published by the Free Software Foundation;
9 either version 2 of the License, or (at your option) any later version.
10
11 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20 #include "ptexlib.h"
21
22 static int nesting = 0;
23 # define max_nesting 1000
24 static void scan_expr(void);
25
26 /*tex
27
28 Let's turn now to some procedures that \TeX\ calls upon frequently to digest
29 certain kinds of patterns in the input. Most of these are quite simple; some
30 are quite elaborate. Almost all of the routines call |get_x_token|, which can
31 cause them to be invoked recursively.
32
33 The |scan_left_brace| routine is called when a left brace is supposed to be
34 the next non-blank token. (The term ``left brace'' means, more precisely, a
35 character whose catcode is |left_brace|.) \TeX\ allows \.{\\relax} to appear
36 before the |left_brace|.
37
38 */
39
40 /* This reads a mandatory |left_brace|: */
41
42 void scan_left_brace(void)
43 {
44 /*tex Get the next non-blank non-relax non-call token */
45 do {
46 get_x_token();
47 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
48 if (cur_cmd != left_brace_cmd) {
49 print_err("Missing { inserted");
50 help4(
51 "A left brace was mandatory here, so I've put one in.",
52 "You might want to delete and/or insert some corrections",
53 "so that I will find a matching right brace soon.",
54 "If you're confused by all this, try typing `I}' now."
55 );
56 back_error();
57 cur_tok = left_brace_token + '{';
58 cur_cmd = left_brace_cmd;
59 cur_chr = '{';
60 incr(align_state);
61 }
62 }
63
64 /*tex
65
66 The |scan_optional_equals| routine looks for an optional `\.=' sign preceded
67 by optional spaces; `\.{\\relax}' is not ignored here.
68
69 */
70
71 void scan_optional_equals(void)
72 {
73 /*tex Get the next non-blank non-call token */
74 do {
75 get_x_token();
76 } while (cur_cmd == spacer_cmd);
77 if (cur_tok != other_token + '=')
78 back_input();
79 }
80
81 /*tex
82
83 Here is a procedure that sounds an alarm when mu and non-mu units are being
84 switched.
85
86 */
87
88 static void mu_error(void)
89 {
90 print_err("Incompatible glue units");
91 help1("I'm going to assume that 1mu=1pt when they're mixed.");
92 error();
93 }
94
95 /*tex
96
97 The next routine `|scan_something_internal|' is used to fetch internal
98 numeric quantities like `\.{\\hsize}', and also to handle the `\.{\\the}'
99 when expanding constructions like `\.{\\the\\toks0}' and
100 `\.{\\the\\baselineskip}'. Soon we will be considering the |scan_int|
101 procedure, which calls |scan_something_internal|; on the other hand,
102 |scan_something_internal| also calls |scan_int|, for constructions like
103 `\.{\\catcode\`\\\$}' or `\.{\\fontdimen} \.3 \.{\\ff}'. So we have to
104 declare |scan_int| as a |forward| procedure. A few other procedures are also
105 declared at this point.
106
107 \TeX\ doesn't know exactly what to expect when |scan_something_internal|
108 begins. For example, an integer or dimension or glue value could occur
109 immediately after `\.{\\hskip}'; and one can even say \.{\\the} with respect
110 to token lists in constructions like `\.{\\xdef\\o\{\\the\\output\}}'. On the
111 other hand, only integers are allowed after a construction like
112 `\.{\\count}'. To handle the various possibilities, |scan_something_internal|
113 has a |level| parameter, which tells the ``highest'' kind of quantity that
114 |scan_something_internal| is allowed to produce. Eight levels are
115 distinguished, namely |int_val|, |attr_val|, |dimen_val|, |glue_val|,
116 |mu_val|, |dir_val|, |ident_val|, and |tok_val|.
117
118 The output of |scan_something_internal| (and of the other routines
119 |scan_int|, |scan_dimen|, and |scan_glue| below) is put into the global
120 variable |cur_val|, and its level is put into |cur_val_level|. The highest
121 values of |cur_val_level| are special: |mu_val| is used only when |cur_val|
122 points to something in a ``muskip'' register, or to one of the three
123 parameters \.{\\thinmuskip}, \.{\\medmuskip}, \.{\\thickmuskip}; |ident_val|
124 is used only when |cur_val| points to a font identifier; |tok_val| is used
125 only when |cur_val| points to |null| or to the reference count of a token
126 list. The last two cases are allowed only when |scan_something_internal| is
127 called with |level=tok_val|.
128
129 If the output is glue, |cur_val| will point to a glue specification, and the
130 reference count of that glue will have been updated to reflect this
131 reference; if the output is a nonempty token list, |cur_val| will point to
132 its reference count, but in this case the count will not have been updated.
133 Otherwise |cur_val| will contain the integer or scaled value in question.
134
135 */
136
137 /*tex The value returned by numeric scanners: */
138
139 int cur_val;
140
141 /*tex Delcodes are sometimes 51 digits: */
142
143 int cur_val1;
144
145 /*tex The level of this value: */
146
147 int cur_val_level;
148
149 #define scanned_result(A,B) do { \
150 cur_val=A; \
151 cur_val_level=B; \
152 } while (0)
153
154 /*tex
155
156 When a |glue_val| changes to a |dimen_val|, we use the width component of the
157 glue; there is no need to decrease the reference count, since it has not yet
158 been increased. When a |dimen_val| changes to an |int_val|, we use scaled
159 points so that the value doesn't actually change. And when a |mu_val| changes
160 to a |glue_val|, the value doesn't change either.
161
162 */
163
164 static void downgrade_cur_val(boolean delete_glue)
165 {
166 if (cur_val_level == glue_val_level) {
167 halfword m = cur_val;
168 cur_val = width(m);
169 if (delete_glue)
170 flush_node(m);
171 } else if (cur_val_level == mu_val_level) {
172 mu_error();
173 }
174 decr(cur_val_level);
175 }
176
177 void negate_cur_val(boolean modify_glue)
178 {
179 if (cur_val_level >= glue_val_level) {
180 if (modify_glue) {
181 /*tex We modify in-place. */
182 } else {
183 cur_val = new_spec(cur_val);
184 }
185 negate(width(cur_val));
186 negate(stretch(cur_val));
187 negate(shrink(cur_val));
188 } else {
189 negate(cur_val);
190 }
191 }
192
193 /*tex
194
195 Some of the internal items can be fetched both routines, and these have been
196 split off into the next routine, that returns true if the command code was
197 understood.
198
199 */
200
201 static boolean short_scan_something_internal(int cmd, int chr, int level, boolean negative)
202 {
203 /*tex |chr_code| part of the operand token */
204 halfword m;
205 /*tex general purpose index */
206 halfword q;
207 /*tex index into |nest| */
208 int p;
209 int save_cur_chr;
210 boolean succeeded = true;
211 m = chr;
212 switch (cmd) {
213 case assign_toks_cmd:
214 scanned_result(equiv(m), tok_val_level);
215 break;
216 case assign_int_cmd:
217 scanned_result(eqtb[m].cint, int_val_level);
218 break;
219 case assign_attr_cmd:
220 scanned_result(eqtb[m].cint, int_val_level);
221 break;
222 case assign_dimen_cmd:
223 scanned_result(eqtb[m].cint, dimen_val_level);
224 break;
225 case assign_glue_cmd:
226 scanned_result(equiv(m), glue_val_level);
227 break;
228 case assign_mu_glue_cmd:
229 scanned_result(equiv(m), mu_val_level);
230 break;
231 case assign_direction_cmd:
232 if (m == (int_base + line_direction_code)) {
233 m = int_base + text_direction_code;
234 }
235 scanned_result(eqtb[m].cint, int_val_level);
236 break;
237 case assign_dir_cmd:
238 if (m == (int_base + line_direction_code)) {
239 m = int_base + text_direction_code;
240 }
241 scanned_result(eqtb[m].cint, dir_val_level);
242 break;
243 case math_style_cmd:
244 scanned_result(m, int_val_level);
245 break;
246 case set_aux_cmd:
247 /*tex Fetch the |space_factor| or the |prev_depth|. */
248 if (abs(cur_list.mode_field) != m) {
249 print_err("Improper ");
250 print_cmd_chr(set_aux_cmd, m);
251 help4(
252 "You can refer to \\spacefactor only in horizontal mode;",
253 "you can refer to \\prevdepth only in vertical mode; and",
254 "neither of these is meaningful inside \\write. So",
255 "I'm forgetting what you said and using zero instead."
256 );
257 error();
258 if (level != tok_val_level)
259 scanned_result(0, dimen_val_level);
260 else
261 scanned_result(0, int_val_level);
262 } else if (m == vmode) {
263 scanned_result(prev_depth_par, dimen_val_level);
264 } else {
265 scanned_result(space_factor_par, int_val_level);
266 }
267 break;
268 case set_prev_graf_cmd:
269 /*tex Fetch the |prev_graf| */
270 if (cur_list.mode_field == 0) {
271 /*tex |prev_graf=0| within \.{\\write} */
272 scanned_result(0, int_val_level);
273 } else {
274 p = nest_ptr;
275 while (abs(nest[p].mode_field) != vmode)
276 decr(p);
277 scanned_result(nest[p].pg_field, int_val_level);
278 }
279 break;
280 case set_page_int_cmd:
281 /*tex Fetch the |dead_cycles| or the |insert_penalties| */
282 if (m == 0)
283 cur_val = dead_cycles;
284 else if (m == 2)
285 cur_val = interaction;
286 else
287 cur_val = insert_penalties;
288 cur_val_level = int_val_level;
289 break;
290 case set_page_dimen_cmd:
291 /*tex Fetch something on the |page_so_far|. */
292 if ((page_contents == empty) && (!output_active)) {
293 if (m == 0)
294 cur_val = max_dimen;
295 else
296 cur_val = 0;
297 } else {
298 cur_val = page_so_far[m];
299 }
300 cur_val_level = dimen_val_level;
301 break;
302 case set_tex_shape_cmd:
303 /*tex Fetch the |par_shape| size. */
304 if (par_shape_par_ptr == null)
305 cur_val = 0;
306 else
307 cur_val = vinfo(par_shape_par_ptr + 1);
308 cur_val_level = int_val_level;
309 break;
310 case set_etex_shape_cmd:
311 /*tex Fetch a penalties array element. */
312 scan_int();
313 if ((equiv(m) == null) || (cur_val < 0)) {
314 cur_val = 0;
315 } else {
316 if (cur_val > penalty(equiv(m)))
317 cur_val = penalty(equiv(m));
318 cur_val = penalty(equiv(m) + cur_val);
319 }
320 cur_val_level = int_val_level;
321 break;
322 case char_given_cmd:
323 case math_given_cmd:
324 case xmath_given_cmd:
325 scanned_result(cur_chr, int_val_level);
326 break;
327 case last_item_cmd:
328 /*tex
329
330 Because the items in this case directly refer to |cur_chr|, it
331 needs to be saved and restored.
332
333 */
334 save_cur_chr = cur_chr;
335 cur_chr = chr;
336 /*tex
337
338 Fetch an item in the current node, if appropriate. Here is where
339 \.{\\lastpenalty}, \.{\\lastkern}, and \.{\\ } are implemented.
340 The reference count for \.{\\lastskip} will be updated later.
341
342 We also handle \.{\\inputlineno} and \.{\\badness} here, because
343 they are legal in similar contexts.
344
345 */
346 if (m >= input_line_no_code) {
347 if (m >= eTeX_glue) {
348 /*tex Process an expression and |return|. */
349 if (m < eTeX_mu) {
350 if (m == mu_to_glue_code) {
351 scan_mu_glue();
352 };
353 cur_val_level = glue_val_level;
354 } else if (m < eTeX_expr) {
355 if (m == glue_to_mu_code) {
356 scan_normal_glue();
357 }
358 cur_val_level = mu_val_level;
359 } else {
360 cur_val_level = m - eTeX_expr + int_val_level;
361 scan_expr();
362 }
363 /*tex
364
365 This code for reducing |cur_val_level| and\slash or
366 negating the result is similar to the one for all the
367 other cases of |scan_something_internal|; we free a
368 glue_spec when needed.
369
370 */
371 while (cur_val_level > level) {
372 downgrade_cur_val(true);
373 }
374 if (negative) {
375 /*tex
376
377 We get a new glue spec node with negated values and
378 the old intermediate is deleted.
379
380 */
381 negate_cur_val(true);
382 }
383 return succeeded;
384 } else if (m >= eTeX_dim) {
385 switch (m) {
386 case font_char_wd_code:
387 case font_char_ht_code:
388 case font_char_dp_code:
389 case font_char_ic_code:
390 scan_font_ident();
391 q = cur_val;
392 scan_char_num();
393 if (char_exists(q, cur_val)) {
394 switch (m) {
395 case font_char_wd_code:
396 cur_val = char_width(q, cur_val);
397 break;
398 case font_char_ht_code:
399 cur_val = char_height(q, cur_val);
400 break;
401 case font_char_dp_code:
402 cur_val = char_depth(q, cur_val);
403 break;
404 case font_char_ic_code:
405 cur_val = char_italic(q, cur_val);
406 break;
407 }
408 } else {
409 cur_val = 0;
410 }
411 break;
412 case par_shape_length_code:
413 case par_shape_indent_code:
414 case par_shape_dimen_code:
415 q = cur_chr - par_shape_length_code;
416 scan_int();
417 if ((par_shape_par_ptr == null) || (cur_val <= 0)) {
418 cur_val = 0;
419 } else {
420 if (q == 2) {
421 q = cur_val % 2;
422 cur_val = (cur_val + q) / 2;
423 }
424 if (cur_val > vinfo(par_shape_par_ptr + 1))
425 cur_val = vinfo(par_shape_par_ptr + 1);
426 cur_val =
427 varmem[par_shape_par_ptr + 2 * cur_val - q + 1].cint;
428 }
429 cur_val_level = dimen_val_level;
430 break;
431 case glue_stretch_code:
432 case glue_shrink_code:
433 scan_normal_glue();
434 q = cur_val;
435 if (m == glue_stretch_code)
436 cur_val = stretch(q);
437 else
438 cur_val = shrink(q);
439 flush_node(q);
440 break;
441 }
442 cur_val_level = dimen_val_level;
443 } else {
444 switch (m) {
445 case input_line_no_code:
446 cur_val = line;
447 break;
448 case badness_code:
449 cur_val = last_badness;
450 break;
451 case luatex_version_code:
452 cur_val = get_luatexversion();
453 break;
454 case last_saved_box_resource_index_code:
455 cur_val = last_saved_box_index;
456 break;
457 case last_saved_image_resource_index_code:
458 cur_val = last_saved_image_index;
459 break;
460 case last_saved_image_resource_pages_code:
461 cur_val = last_saved_image_pages;
462 break;
463 case last_x_pos_code:
464 cur_val = last_position.h;
465 break;
466 case last_y_pos_code:
467 cur_val = last_position.v;
468 break;
469 case random_seed_code:
470 cur_val = random_seed;
471 break;
472 case eTeX_version_code:
473 cur_val = eTeX_version;
474 break;
475 case eTeX_minor_version_code:
476 cur_val = eTeX_minor_version;
477 break;
478 case current_group_level_code:
479 cur_val = cur_level - level_one;
480 break;
481 case current_group_type_code:
482 cur_val = cur_group;
483 break;
484 case current_if_level_code:
485 q = cond_ptr;
486 cur_val = 0;
487 while (q != null) {
488 incr(cur_val);
489 q = vlink(q);
490 }
491 break;
492 case current_if_type_code:
493 if (cond_ptr == null)
494 cur_val = 0;
495 else if (cur_if < unless_code)
496 cur_val = cur_if + 1;
497 else
498 cur_val = -(cur_if - unless_code + 1);
499 break;
500 case current_if_branch_code:
501 if ((if_limit == or_code) || (if_limit == else_code))
502 cur_val = 1;
503 else if (if_limit == fi_code)
504 cur_val = -1;
505 else
506 cur_val = 0;
507 break;
508 case glue_stretch_order_code:
509 case glue_shrink_order_code:
510 case eTeX_glue_stretch_order_code:
511 case eTeX_glue_shrink_order_code:
512 scan_normal_glue();
513 q = cur_val;
514 if (m == glue_stretch_order_code || m == eTeX_glue_stretch_order_code) {
515 cur_val = stretch_order(q);
516 } else {
517 cur_val = shrink_order(q);
518 }
519 if (cur_val && (m == eTeX_glue_stretch_order_code || m == eTeX_glue_shrink_order_code)) {
520 cur_val = (cur_val == 1) ? - 1 : cur_val - 1;
521 }
522 flush_node(q);
523 break;
524 }
525 cur_val_level = int_val_level;
526 }
527 } else {
528 if (cur_chr == glue_val_level)
529 cur_val = zero_glue;
530 else
531 cur_val = 0;
532 if (cur_chr == last_node_type_code) {
533 cur_val_level = int_val_level;
534 if ((cur_list.tail_field == cur_list.head_field)
535 || (cur_list.mode_field == 0))
536 cur_val = -1;
537 } else {
538 /*tex assumes identical values */
539 cur_val_level = cur_chr;
540 }
541 if ((cur_list.tail_field != contrib_head) && (cur_list.mode_field != 0)) {
542 switch (cur_chr) {
543 case lastpenalty_code:
544 if (type(cur_list.tail_field) == penalty_node)
545 cur_val = penalty(cur_list.tail_field);
546 break;
547 case lastkern_code:
548 if (type(cur_list.tail_field) == kern_node)
549 cur_val = width(cur_list.tail_field);
550 break;
551 case lastskip_code:
552 if (type(cur_list.tail_field) == glue_node)
553 cur_val = cur_list.tail_field;
554 if (subtype(cur_list.tail_field) == mu_glue)
555 cur_val_level = mu_val_level;
556 break;
557 case last_node_type_code:
558 cur_val = visible_last_node_type(cur_list.tail_field);
559 break;
560 }
561 } else if ((cur_list.mode_field == vmode) && (cur_list.tail_field == cur_list.head_field)) {
562 switch (cur_chr) {
563 case lastpenalty_code:
564 cur_val = last_penalty;
565 break;
566 case lastkern_code:
567 cur_val = last_kern;
568 break;
569 case lastskip_code:
570 if (last_glue != max_halfword)
571 cur_val = last_glue;
572 break;
573 case last_node_type_code:
574 cur_val = last_node_type;
575 break;
576 }
577 }
578 }
579 cur_chr = save_cur_chr;
580 break;
581 default:
582 succeeded = false;
583 }
584 if (succeeded) {
585 while (cur_val_level > level) {
586 /*tex Convert |cur_val| to a lower level. */
587 downgrade_cur_val(false);
588 }
589 /*tex
590
591 Fix the reference count, if any, and negate |cur_val| if |negative|.
592 If |cur_val| points to a glue specification at this point, the
593 reference count for the glue does not yet include the reference by
594 |cur_val|. If |negative| is |true|, |cur_val_level| is known to be
595 |<=mu_val|.
596
597 */
598 if (negative) {
599 /*tex We create a new (negated) glue spec and keep the old one. */
600 negate_cur_val(false);
601 } else if ((cur_val_level >= glue_val_level) && (cur_val_level <= mu_val_level)) {
602 cur_val = new_spec(cur_val);
603 }
604 }
605 return succeeded;
606 }
607
608 /*tex
609
610 First, here is a short routine that is called from lua code. All the real
611 work is delegated to |short_scan_something_internal| that is shared between
612 this routine and |scan_something_internal|.
613
614 */
615
616 void scan_something_simple(halfword cmd, halfword subitem)
617 {
618 /*tex Negative is never true. */
619 if (!short_scan_something_internal(cmd, subitem, tok_val_level, false)) {
620 /*tex Complain that |texlib| can not do this; give zero result. */
621 print_err("You can't use `");
622 print_cmd_chr((quarterword) cmd, subitem);
623 tprint("' as tex library index");
624 help1("I'm forgetting what you said and using zero instead.");
625 error();
626 scanned_result(0, int_val_level);
627 }
628 }
629
630 /*tex
631
632 OK, we're ready for |scan_something_internal| itself. A second parameter,
633 |negative|, is set |true| if the value that is found should be negated. It is
634 assumed that |cur_cmd| and |cur_chr| represent the first token of the
635 internal quantity to be scanned; an error will be signalled if
636 |cur_cmd<min_internal| or |cur_cmd>max_internal|.
637
638 */
639
640 /*tex Fetch an internal parameter: */
641
642 void scan_something_internal(int level, boolean negative)
643 {
644 /*tex |chr_code| part of the operand token */
645 halfword m;
646 /*tex accumulators */
647 int n, k;
648 RESTART:
649 m = cur_chr;
650 if (!short_scan_something_internal(cur_cmd, cur_chr, level, negative)) {
651 switch (cur_cmd) {
652 case def_char_code_cmd:
653 /*tex Fetch a character code from some table */
654 scan_char_num();
655 if (m == math_code_base) {
656 cur_val1 = get_math_code_num(cur_val);
657 scanned_result(cur_val1, int_val_level);
658 } else if (m == lc_code_base) {
659 cur_val1 = get_lc_code(cur_val);
660 scanned_result(cur_val1, int_val_level);
661 } else if (m == uc_code_base) {
662 cur_val1 = get_uc_code(cur_val);
663 scanned_result(cur_val1, int_val_level);
664 } else if (m == sf_code_base) {
665 cur_val1 = get_sf_code(cur_val);
666 scanned_result(cur_val1, int_val_level);
667 } else if (m == cat_code_base) {
668 cur_val1 = get_cat_code(cat_code_table_par, cur_val);
669 scanned_result(cur_val1, int_val_level);
670 } else {
671 confusion("def_char");
672 }
673 break;
674 case def_del_code_cmd:
675 case extdef_del_code_cmd:
676 /*tex Fetch a character code from some table. */
677 scan_char_num();
678 cur_val1 = get_del_code_num(cur_val);
679 scanned_result(cur_val1, int_val_level);
680 break;
681 case extdef_math_code_cmd:
682 /*tex Fetch an extended math code table value. */
683 scan_char_num();
684 cur_val1 = get_math_code_num(cur_val);
685 scanned_result(cur_val1, int_val_level);
686 break;
687 case toks_register_cmd:
688 case set_font_cmd:
689 case def_font_cmd:
690 case letterspace_font_cmd:
691 case copy_font_cmd:
692 /*tex Fetch a token list or font identifier, provided that |level=tok_val|. */
693 if (level != tok_val_level) {
694 print_err("Missing number, treated as zero");
695 help3(
696 "A number should have been here; I inserted `0'.",
697 "(If you can't figure out why I needed to see a number,",
698 "look up `weird error' in the index to The TeXbook.)"
699 );
700 back_error();
701 scanned_result(0, dimen_val_level);
702 } else if (cur_cmd == toks_register_cmd) {
703 scan_register_num();
704 m = toks_base + cur_val;
705 scanned_result(equiv(m), tok_val_level);
706 } else {
707 back_input();
708 scan_font_ident();
709 scanned_result(font_id_base + cur_val, ident_val_level);
710 }
711 break;
712 case set_font_id_cmd:
713 scan_int();
714 scanned_result(font_id_base + cur_val, ident_val_level);
715 break;
716 case def_family_cmd:
717 /*tex Fetch a math font identifier. */
718 scan_char_num();
719 cur_val1 = fam_fnt(cur_val, m);
720 scanned_result(font_id_base + cur_val1, ident_val_level);
721 break;
722 case set_math_param_cmd:
723 /*tex Fetch a math parameter. */
724 cur_val1 = cur_chr;
725 get_token();
726 if (cur_cmd != math_style_cmd) {
727 print_err("Missing math style, treated as \\displaystyle");
728 help1("A style should have been here; I inserted `\\displaystyle'.");
729 cur_val = display_style;
730 back_error();
731 } else {
732 cur_val = cur_chr;
733 }
734 if (cur_val1 < math_param_first_mu_glue) {
735 if (cur_val1 == math_param_radical_degree_raise) {
736 cur_val1 = get_math_param(cur_val1, cur_chr);
737 scanned_result(cur_val1, int_val_level);
738 } else {
739 cur_val1 = get_math_param(cur_val1, cur_chr);
740 scanned_result(cur_val1, dimen_val_level);
741 }
742 } else {
743 cur_val1 = get_math_param(cur_val1, cur_chr);
744 if (cur_val1 == thin_mu_skip_code)
745 cur_val1 = thin_mu_skip_par;
746 else if (cur_val1 == med_mu_skip_code)
747 cur_val1 = med_mu_skip_par;
748 else if (cur_val1 == thick_mu_skip_code)
749 cur_val1 = thick_mu_skip_par;
750 scanned_result(cur_val1, mu_val_level);
751 }
752 break;
753 case assign_box_dir_cmd:
754 scan_register_num();
755 m = cur_val;
756 if (box(m) != null)
757 cur_val = box_dir(box(m));
758 else
759 cur_val = 0;
760 cur_val_level = dir_val_level;
761 break;
762 case set_box_dimen_cmd:
763 /*tex Fetch a box dimension. */
764 scan_register_num();
765 if (box(cur_val) == null)
766 cur_val = 0;
767 else
768 cur_val = varmem[box(cur_val) + m].cint;
769 cur_val_level = dimen_val_level;
770 break;
771 case assign_font_dimen_cmd:
772 /*tex Fetch a font dimension. */
773 get_font_dimen();
774 break;
775 case assign_font_int_cmd:
776 /*tex Fetch a font integer. */
777 scan_font_ident();
778 if (m == 0) {
779 scanned_result(hyphen_char(cur_val), int_val_level);
780 } else if (m == 1) {
781 scanned_result(skew_char(cur_val), int_val_level);
782 } else if (m == no_lig_code) {
783 scanned_result(test_no_ligatures(cur_val), int_val_level);
784 } else {
785 n = cur_val;
786 scan_char_num();
787 k = cur_val;
788 switch (m) {
789 case lp_code_base:
790 scanned_result(get_lp_code(n, k), int_val_level);
791 break;
792 case rp_code_base:
793 scanned_result(get_rp_code(n, k), int_val_level);
794 break;
795 case ef_code_base:
796 scanned_result(get_ef_code(n, k), int_val_level);
797 break;
798 case tag_code:
799 scanned_result(get_tag_code(n, k), int_val_level);
800 break;
801 }
802 }
803 break;
804 case register_cmd:
805 /*tex Fetch a register */
806 scan_register_num();
807 switch (m) {
808 case int_val_level:
809 cur_val = count(cur_val);
810 break;
811 case attr_val_level:
812 cur_val = attribute(cur_val);
813 break;
814 case dimen_val_level:
815 cur_val = dimen(cur_val);
816 break;
817 case glue_val_level:
818 cur_val = skip(cur_val);
819 break;
820 case mu_val_level:
821 cur_val = mu_skip(cur_val);
822 break;
823 }
824 cur_val_level = m;
825 break;
826 case ignore_spaces_cmd:
827 /*tex Trap unexpandable primitives. */
828 if (cur_chr == 1) {
829 /*tex
830
831 Reset |cur_tok| for unexpandable primitives, goto
832 restart. This block deals with unexpandable
833 \.{\\primitive} appearing at a spot where an integer or
834 an internal values should have been found. It fetches the
835 next token then resets |cur_cmd|, |cur_cs|, and
836 |cur_tok|, based on the primitive value of that token. No
837 expansion takes place, because the next token may be all
838 sorts of things. This could trigger further expansion
839 creating new errors.
840
841 */
842 get_token();
843 cur_cs = prim_lookup(cs_text(cur_cs));
844 if (cur_cs != undefined_primitive) {
845 cur_cmd = get_prim_eq_type(cur_cs);
846 cur_chr = get_prim_equiv(cur_cs);
847 cur_tok = token_val(cur_cmd, cur_chr);
848 } else {
849 cur_cmd = relax_cmd;
850 cur_chr = 0;
851 cur_tok = cs_token_flag + frozen_relax;
852 cur_cs = frozen_relax;
853 }
854 goto RESTART;
855 }
856 break;
857 case hyph_data_cmd:
858 switch (cur_chr) {
859 case 0:
860 case 1:
861 goto DEFAULT;
862 break;
863 case 2:
864 cur_val = get_pre_hyphen_char(language_par);
865 cur_val_level = int_val_level;
866 break;
867 case 3:
868 cur_val = get_post_hyphen_char(language_par);
869 cur_val_level = int_val_level;
870 break;
871 case 4:
872 cur_val = get_pre_exhyphen_char(language_par);
873 cur_val_level = int_val_level;
874 break;
875 case 5:
876 cur_val = get_post_exhyphen_char(language_par);
877 cur_val_level = int_val_level;
878 break;
879 case 6:
880 cur_val = get_hyphenation_min(language_par);
881 cur_val_level = int_val_level;
882 break;
883 case 7:
884 scan_int();
885 cur_val = get_hj_code(language_par,cur_val);
886 cur_val_level = int_val_level;
887 break;
888 }
889 break;
890 default:
891 DEFAULT:
892 /*tex Complain that \.{\\the} can not do this; give zero result. */
893 print_err("You can't use `");
894 print_cmd_chr((quarterword) cur_cmd, cur_chr);
895 tprint("' after \\the");
896 help1("I'm forgetting what you said and using zero instead.");
897 error();
898 if (level != tok_val_level)
899 scanned_result(0, dimen_val_level);
900 else
901 scanned_result(0, int_val_level);
902 break;
903 }
904 while (cur_val_level > level) {
905 /*tex Convert |cur_val| to a lower level. */
906 downgrade_cur_val(false);
907 }
908 /*tex
909
910 Fix the reference count, if any, and negate |cur_val| if |negative|.
911 If |cur_val| points to a glue specification at this point, the
912 reference count for the glue does not yet include the reference by
913 |cur_val|. If |negative| is |true|, |cur_val_level| is known to be
914 |<=mu_val|.
915
916 */
917 if (negative) {
918 /*tex We create a new (negated) glue spec and keep the old one. */
919 negate_cur_val(false);
920 } else if ((cur_val_level >= glue_val_level) && (cur_val_level <= mu_val_level)) {
921 cur_val = new_spec(cur_val);
922 }
923 }
924 }
925
926 /*tex
927
928 It is nice to have routines that say what they do, so the original
929 |scan_eight_bit_int| is superceded by |scan_register_num| and
930 |scan_mark_num|. It may become split up even further in the future.
931
932 Many of the |restricted classes| routines are the essentially the same except
933 for the upper limit and the error message, so it makes sense to combine these
934 all into one function.
935
936 */
937
938 void scan_limited_int(int max, const char *name)
939 {
940 char hlp[80];
941 scan_int();
942 if ((cur_val < 0) || (cur_val > max)) {
943 if (name == NULL) {
944 snprintf(hlp, 80, "Since I expected to read a number between 0 and %d,", max);
945 print_err("Bad number");
946 } else {
947 char msg[80];
948 snprintf(hlp, 80, "A %s must be between 0 and %d.", name, max);
949 snprintf(msg, 80, "Bad %s", name);
950 print_err(msg);
951 }
952 help2(hlp, "I changed this one to zero.");
953 int_error(cur_val);
954 cur_val = 0;
955 }
956 }
957
958 void scan_fifteen_bit_int(void)
959 {
960 scan_real_fifteen_bit_int();
961 cur_val = ((cur_val / 0x1000) * 0x1000000) + (((cur_val % 0x1000) / 0x100) * 0x10000) + (cur_val % 0x100);
962 }
963
964 void scan_fifty_one_bit_int(void)
965 {
966 int iiii;
967 scan_int();
968 if ((cur_val < 0) || (cur_val > 0777777777)) {
969 print_err("Bad delimiter code");
970 help2(
971 "A numeric delimiter (first part) must be between 0 and 2^{27}-1.",
972 "I changed this one to zero."
973 );
974 int_error(cur_val);
975 cur_val = 0;
976 }
977 iiii = cur_val;
978 scan_int();
979 if ((cur_val < 0) || (cur_val > 0xFFFFFF)) {
980 print_err("Bad delimiter code");
981 help2(
982 "A numeric delimiter (second part) must be between 0 and 2^{24}-1.",
983 "I changed this one to zero."
984 );
985 int_error(cur_val);
986 cur_val = 0;
987 }
988 cur_val1 = cur_val;
989 cur_val = iiii;
990 }
991
992 /*tex
993
994 An integer number can be preceded by any number of spaces and `\.+' or `\.-'
995 signs. Then comes either a decimal constant (i.e., radix 10), an octal
996 constant (i.e., radix 8, preceded by~'), a hexadecimal constant (radix 16,
997 preceded by~"), an alphabetic constant (preceded by~`), or an internal
998 variable. After scanning is complete, |cur_val| will contain the answer,
999 which must be at most $2^{31}-1=2147483647$ in absolute value. The value of
1000 |radix| is set to 10, 8, or 16 in the cases of decimal, octal, or hexadecimal
1001 constants, otherwise |radix| is set to zero. An optional space follows a
1002 constant.
1003
1004 */
1005
1006 /*tex |scan_int| sets this to 8, 10, 16, or zero */
1007
1008 int radix;
1009
1010 /*tex
1011
1012 The |scan_int| routine is used also to scan the integer part of a fraction;
1013 for example, the `\.3' in `\.{3.14159}' will be found by |scan_int|. The
1014 |scan_dimen| routine assumes that |cur_tok=point_token| after the integer
1015 part of such a fraction has been scanned by |scan_int|, and that the decimal
1016 point has been backed up to be scanned again.
1017
1018 */
1019
1020 /*tex Sets |cur_val| to an integer: */
1021
1022 void scan_int(void)
1023 {
1024 /*tex should the answer be negated? */
1025 boolean negative;
1026 /*tex |$2^{31}$ / radix|, the threshold of danger */
1027 int m;
1028 /*tex the digit just scanned */
1029 int d;
1030 /*tex have no digits appeared? */
1031 boolean vacuous;
1032 /*tex has an error message been issued? */
1033 boolean OK_so_far;
1034 radix = 0;
1035 OK_so_far = true;
1036 /*tex Get the next non-blank non-sign token; set |negative| appropriately. */
1037 negative = false;
1038 do {
1039 /*tex Get the next non-blank non-call token. */
1040 do {
1041 get_x_token();
1042 } while (cur_cmd == spacer_cmd);
1043 if (cur_tok == other_token + '-') {
1044 negative = !negative;
1045 cur_tok = other_token + '+';
1046 }
1047 } while (cur_tok == other_token + '+');
1048
1049 RESTART:
1050 if (cur_tok == alpha_token) {
1051 /*tex
1052
1053 Scan an alphabetic character code into |cur_val|. A space is ignored
1054 after an alphabetic character constant, so that such constants behave
1055 like numeric ones.
1056
1057 */
1058 /*tex Suppress macro expansion: */
1059 get_token();
1060 if (cur_tok < cs_token_flag) {
1061 cur_val = cur_chr;
1062 if (cur_cmd <= right_brace_cmd) {
1063 if (cur_cmd == right_brace_cmd)
1064 incr(align_state);
1065 else
1066 decr(align_state);
1067 }
1068 } else {
1069 /*tex The value of a csname in this context is its name. */
1070 str_number txt = cs_text(cur_tok - cs_token_flag);
1071 if (is_active_cs(txt))
1072 cur_val = active_cs_value(txt);
1073 else if (single_letter(txt))
1074 cur_val = pool_to_unichar(str_string(txt));
1075 else
1076 cur_val = (biggest_char + 1);
1077 }
1078 if (cur_val > biggest_char) {
1079 print_err("Improper alphabetic constant");
1080 help2(
1081 "A one-character control sequence belongs after a ` mark.",
1082 "So I'm essentially inserting \\0 here."
1083 );
1084 cur_val = '0';
1085 back_error();
1086 } else {
1087 /*tex Scan an optional space. */
1088 get_x_token();
1089 if (cur_cmd != spacer_cmd)
1090 back_input();
1091 }
1092
1093 } else if (cur_tok == cs_token_flag + frozen_primitive) {
1094 /*tex
1095
1096 Reset |cur_tok| for unexpandable primitives, goto restart This block
1097 deals with unexpandable \.{\\primitive} appearing at a spot where an
1098 integer or an internal values should have been found. It fetches the
1099 next token then resets |cur_cmd|, |cur_cs|, and |cur_tok|, based on
1100 the primitive value of that token. No expansion takes place, because
1101 the next token may be all sorts of things. This could trigger further
1102 expansion creating new errors.
1103
1104 */
1105 get_token();
1106 cur_cs = prim_lookup(cs_text(cur_cs));
1107 if (cur_cs != undefined_primitive) {
1108 cur_cmd = get_prim_eq_type(cur_cs);
1109 cur_chr = get_prim_equiv(cur_cs);
1110 cur_tok = token_val(cur_cmd, cur_chr);
1111 } else {
1112 cur_cmd = relax_cmd;
1113 cur_chr = 0;
1114 cur_tok = cs_token_flag + frozen_relax;
1115 cur_cs = frozen_relax;
1116 }
1117 goto RESTART;
1118 } else if (cur_cmd == math_style_cmd) {
1119 cur_val = cur_chr;
1120 } else if ((cur_cmd >= min_internal_cmd) && (cur_cmd <= max_internal_cmd)) {
1121 scan_something_internal(int_val_level, false);
1122 } else {
1123 /*tex Scan a numeric constant. */
1124 radix = 10;
1125 m = 214748364;
1126 if (cur_tok == octal_token) {
1127 radix = 8;
1128 m = 02000000000;
1129 get_x_token();
1130 } else if (cur_tok == hex_token) {
1131 radix = 16;
1132 m = 01000000000;
1133 get_x_token();
1134 }
1135 vacuous = true;
1136 cur_val = 0;
1137 /*tex Accumulate the constant until |cur_tok| is not a suitable digit. */
1138 while (1) {
1139 if ((cur_tok < zero_token + radix) && (cur_tok >= zero_token) && (cur_tok <= nine_token)) {
1140 d = cur_tok - zero_token;
1141 } else if (radix == 16) {
1142 if ((cur_tok <= A_token + 5) && (cur_tok >= A_token)) {
1143 d = cur_tok - A_token + 10;
1144 } else if ((cur_tok <= other_A_token + 5) && (cur_tok >= other_A_token)) {
1145 d = cur_tok - other_A_token + 10;
1146 } else {
1147 break;
1148 }
1149 } else {
1150 break;
1151 }
1152 vacuous = false;
1153 if ((cur_val >= m) && ((cur_val > m) || (d > 7) || (radix != 10))) {
1154 if (OK_so_far) {
1155 print_err("Number too big");
1156 help2(
1157 "I can only go up to 2147483647='17777777777=\"7FFFFFFF,",
1158 "so I'm using that number instead of yours."
1159 );
1160 error();
1161 cur_val = infinity;
1162 OK_so_far = false;
1163 }
1164 } else {
1165 cur_val = cur_val * radix + d;
1166 }
1167 get_x_token();
1168 }
1169 if (vacuous) {
1170 /*tex Express astonishment that no number was here */
1171 print_err("Missing number, treated as zero");
1172 help3(
1173 "A number should have been here; I inserted `0'.",
1174 "(If you can't figure out why I needed to see a number,",
1175 "look up `weird error' in the index to The TeXbook.)"
1176 );
1177 back_error();
1178 } else if (cur_cmd != spacer_cmd) {
1179 back_input();
1180 }
1181 }
1182 if (negative)
1183 negate(cur_val);
1184 }
1185
1186 /*tex
1187
1188 The following code is executed when |scan_something_internal| was called
1189 asking for |mu_val|, when we really wanted a ``mudimen'' instead of
1190 ``muglue.''
1191
1192 */
1193
1194 static void coerce_glue(void)
1195 {
1196 int v;
1197 if (cur_val_level >= glue_val_level) {
1198 v = width(cur_val);
1199 flush_node(cur_val);
1200 cur_val = v;
1201 }
1202 }
1203
1204 /*tex
1205
1206 The |scan_dimen| routine is similar to |scan_int|, but it sets |cur_val| to a
1207 |scaled| value, i.e., an integral number of sp. One of its main tasks is
1208 therefore to interpret the abbreviations for various kinds of units and to
1209 convert measurements to scaled points.
1210
1211 There are three parameters: |mu| is |true| if the finite units must be
1212 `\.{mu}', while |mu| is |false| if `\.{mu}' units are disallowed; |inf| is
1213 |true| if the infinite units `\.{fil}', `\.{fill}', `\.{filll}' are
1214 permitted; and |shortcut| is |true| if |cur_val| already contains an integer
1215 and only the units need to be considered.
1216
1217 The order of infinity that was found in the case of infinite glue is returned
1218 in the global variable |cur_order|.
1219
1220 */
1221
1222 /*tex The order of infinity found by |scan_dimen|: */
1223
1224 int cur_order;
1225
1226 /*tex
1227
1228 Constructions like `\.{-\'77 pt}' are legal dimensions, so |scan_dimen| may
1229 begin with |scan_int|. This explains why it is convenient to use |scan_int|
1230 also for the integer part of a decimal fraction.
1231
1232 Several branches of |scan_dimen| work with |cur_val| as an integer and with
1233 an auxiliary fraction |f|, so that the actual quantity of interest is
1234 $|cur_val|+|f|/2^{16}$. At the end of the routine, this ``unpacked''
1235 representation is put into the single word |cur_val|, which suddenly switches
1236 significance from |integer| to |scaled|.
1237
1238 The necessary conversion factors can all be specified exactly as fractions
1239 whose numerator and denominator add to 32768 or less. According to the
1240 definitions here, $\rm2660\,dd\approx1000.33297\,mm$; this agrees well with
1241 the value $\rm1000.333\,mm$ cited by Bosshard \^{Bosshard, Hans Rudolf} in
1242 {\sl Technische Grundlagen zur Satzherstellung\/} (Bern, 1980). The Didot
1243 point has been newly standardized in 1978; it's now exactly $\rm
1244 1\,nd=0.375\,mm$. Conversion uses the equation
1245 $0.375=21681/20320/72.27\cdot25.4$. The new Cicero follows the new Didot
1246 point; $\rm 1\,nc=12\,nd$. These would lead to the ratios $21681/20320$ and
1247 $65043/5080$, respectively. The closest approximations supported by the
1248 algorithm would be $11183/10481$ and $1370/107$. In order to maintain the
1249 relation $\rm 1\,nc=12\,nd$, we pick the ratio $685/642$ for $\rm nd$,
1250 however.
1251
1252 */
1253
1254 static void scan_dimen_mu_error(void) {
1255 print_err("Illegal unit of measure (mu inserted)");
1256 help4(
1257 "The unit of measurement in math glue must be mu.",
1258 "To recover gracefully from this error, it's best to",
1259 "delete the erroneous units; e.g., type `2' to delete",
1260 "two letters. (See Chapter 27 of The TeXbook.)"
1261 );
1262 error();
1263 }
1264
1265 static void scan_dimen_unknown_unit_error(void) {
1266 print_err("Illegal unit of measure (pt inserted)");
1267 help6(
1268 "Dimensions can be in units of em, ex, in, pt, pc,",
1269 "cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!",
1270 "I'll assume that you meant to say pt, for printer's points.",
1271 "To recover gracefully from this error, it's best to",
1272 "delete the erroneous units; e.g., type `2' to delete",
1273 "two letters. (See Chapter 27 of The TeXbook.)"
1274 );
1275 error();
1276 }
1277
1278 static void scan_dimen_out_of_range_error(void) {
1279 print_err("Dimension too large");
1280 help2(
1281 "I can't work with sizes bigger than about 19 feet.",
1282 "Continue and I'll use the largest value I can."
1283 );
1284 error();
1285 }
1286
1287 #define set_conversion(A,B) do { num=(A); denom=(B); } while(0)
1288
1289 /*tex
1290
1291 This function sets |cur_val| to a dimension. It could be optimized a bit more
1292 (but not now, something for luatex > 1).
1293
1294 */
1295
1296 void scan_dimen(boolean mu, boolean inf, boolean shortcut)
1297 {
1298 /*tex should the answer be negated? */
1299 boolean negative = false;
1300 boolean is_true = false;
1301 /*tex numerator of a fraction whose denominator is $2^{16}$ */
1302 int f = 0;
1303 /*tex conversion ratio for the scanned units */
1304 int num = 0;
1305 int denom = 0;
1306 /*tex top of decimal digit stack */
1307 halfword q;
1308 /*tex an internal dimension */
1309 scaled v;
1310 /*tex temporary storage of |cur_val| */
1311 int save_cur_val;
1312 arith_error = false;
1313 cur_order = normal;
1314 if (!shortcut) {
1315 /*tex Get the next non-blank non-sign. */
1316 do {
1317 /*tex Get the next non-blank non-call token. */
1318 do {
1319 get_x_token();
1320 } while (cur_cmd == spacer_cmd);
1321 if (cur_tok == minus_token) {
1322 negative = !negative;
1323 cur_tok = plus_token;
1324 }
1325 } while (cur_tok == plus_token);
1326 if ((cur_cmd >= min_internal_cmd) && (cur_cmd <= max_internal_cmd)) {
1327 /*tex Fetch an internal dimension and |goto attach_sign|, or fetch an internal integer. */
1328 if (mu) {
1329 scan_something_internal(mu_val_level, false);
1330 coerce_glue();
1331 if (cur_val_level == mu_val_level) {
1332 goto ATTACH_SIGN;
1333 } else if (cur_val_level != int_val_level) {
1334 mu_error();
1335 }
1336 } else {
1337 scan_something_internal(dimen_val_level, false);
1338 if (cur_val_level == dimen_val_level) {
1339 goto ATTACH_SIGN;
1340 }
1341 }
1342 } else {
1343 back_input();
1344 if (cur_tok == continental_point_token) {
1345 cur_tok = point_token;
1346 }
1347 if (cur_tok != point_token) {
1348 scan_int();
1349 } else {
1350 radix = 10;
1351 cur_val = 0;
1352 }
1353 if (cur_tok == continental_point_token) {
1354 cur_tok = point_token;
1355 }
1356 if ((radix == 10) && (cur_tok == point_token)) {
1357 /*tex
1358
1359 Scan decimal fraction. When the following code is executed,
1360 we have |cur_tok=point_token|, but this token has been backed
1361 up using |back_input|; we must first discard it. It turns out
1362 that a decimal point all by itself is equivalent to
1363 `\.{0.0}'. Let's hope people don't use that fact.
1364
1365 */
1366 int k = 0;
1367 halfword p = null;
1368 int kk;
1369 /*tex The |point_token| is being re-scanned. */
1370 get_token();
1371 while (1) {
1372 get_x_token();
1373 if ((cur_tok > nine_token) || (cur_tok < zero_token))
1374 break;
1375 if (k < 17) {
1376 /*tex Digits for |k>=17| cannot affect the result. */
1377 q = get_avail();
1378 set_token_link(q, p);
1379 set_token_info(q, cur_tok - zero_token);
1380 p = q;
1381 incr(k);
1382 }
1383 }
1384 for (kk = k; kk >= 1; kk--) {
1385 dig[kk - 1] = token_info(p);
1386 q = p;
1387 p = token_link(p);
1388 free_avail(q);
1389 }
1390 f = round_decimals(k);
1391 if (cur_cmd != spacer_cmd) {
1392 back_input();
1393 }
1394 }
1395 }
1396 }
1397 if (cur_val < 0) {
1398 /*tex In this case |f=0|. */
1399 negative = !negative;
1400 negate(cur_val);
1401 }
1402 /*tex
1403
1404 Scan units and set |cur_val| to $x\cdot(|cur_val|+f/2^{16})$, where there
1405 are |x| sp per unit; |goto attach_sign| if the units are internal. Now
1406 comes the harder part: At this point in the program, |cur_val| is a
1407 nonnegative integer and $f/2^{16}$ is a nonnegative fraction less than 1;
1408 we want to multiply the sum of these two quantities by the appropriate
1409 factor, based on the specified units, in order to produce a |scaled|
1410 result, and we want to do the calculation with fixed point arithmetic
1411 that does not overflow.
1412
1413 */
1414 if (inf) {
1415 if (scan_keyword("fi")) {
1416 cur_order = sfi;
1417 if (scan_keyword("l")) {
1418 cur_order = fil;
1419 if (scan_keyword("l")) {
1420 cur_order = fill;
1421 if (scan_keyword("l")) {
1422 cur_order = filll;
1423 }
1424 }
1425 }
1426 goto ATTACH_FRACTION;
1427 }
1428 }
1429 /*tex
1430
1431 Scan for (u)units that are internal dimensions; |goto attach_sign| with
1432 |cur_val| set if found.
1433
1434 */
1435 save_cur_val = cur_val;
1436 /*tex
1437
1438 Get the next non-blank non-call; a pitty if just backed up the input.
1439
1440 */
1441 do {
1442 get_x_token();
1443 } while (cur_cmd == spacer_cmd);
1444
1445 if ((cur_cmd < min_internal_cmd) || (cur_cmd > max_internal_cmd)) {
1446 back_input();
1447 } else {
1448 /*tex |math_given_cmd|, |xmath_given_cmd| and |last_item_cmd} */
1449 if (mu) {
1450 scan_something_internal(mu_val_level, false);
1451 coerce_glue();
1452 if (cur_val_level != mu_val_level) {
1453 mu_error();
1454 }
1455 } else {
1456 scan_something_internal(dimen_val_level, false);
1457 }
1458 v = cur_val;
1459 goto FOUND;
1460 }
1461 /*tex Bah |true| forces to split the unit scanner. */
1462 if (mu) {
1463 /*tex Scan for (m)\.{mu} units and |goto attach_fraction|. */
1464 if (! scan_keyword("mu")) {
1465 scan_dimen_mu_error();
1466 }
1467 goto ATTACH_FRACTION;
1468 } else if (scan_keyword("em")) {
1469 v = quad(get_cur_font());
1470 } else if (scan_keyword("ex")) {
1471 v = x_height(get_cur_font());
1472 } else if (scan_keyword("px")) {
1473 v = px_dimen_par;
1474 } else {
1475 goto PICKUP_UNIT;
1476 }
1477 /*tex Scan an optional space (after em, ex or px) */
1478 get_x_token();
1479 if (cur_cmd != spacer_cmd) {
1480 back_input();
1481 }
1482 FOUND:
1483 cur_val = nx_plus_y(save_cur_val, v, xn_over_d(v, f, 0200000));
1484 goto ATTACH_SIGN;
1485 /*tex
1486
1487 Scan for (a)all other units and adjust |cur_val| and |f| accordingly;
1488 |goto done| in the case of scaled points.
1489
1490 */
1491 PICKUP_UNIT:
1492 if (scan_keyword("pt")) {
1493 /*tex The easy case: */
1494 goto SCALE_VALUE;
1495 } else if (scan_keyword("mm")) {
1496 set_conversion(7227, 2540);
1497 goto SCALE_VALUE;
1498 } else if (scan_keyword("cm")) {
1499 set_conversion(7227, 254);
1500 goto SCALE_VALUE;
1501 } else if (scan_keyword("sp")) {
1502 goto DONE;
1503 } else if (scan_keyword("bp")) {
1504 set_conversion(7227, 7200);
1505 goto SCALE_VALUE;
1506 } else if (scan_keyword("in")) {
1507 set_conversion(7227, 100);
1508 goto SCALE_VALUE;
1509 } else if (scan_keyword("dd")) {
1510 set_conversion(1238, 1157);
1511 goto SCALE_VALUE;
1512 } else if (scan_keyword("cc")) {
1513 set_conversion(14856, 1157);
1514 goto SCALE_VALUE;
1515 } else if (scan_keyword("pc")) {
1516 set_conversion(12, 1);
1517 goto SCALE_VALUE;
1518 } else if (scan_keyword("nd")) {
1519 set_conversion(685, 642);
1520 goto SCALE_VALUE;
1521 } else if (scan_keyword("nc")) {
1522 set_conversion(1370, 107);
1523 goto SCALE_VALUE;
1524 } else if (!is_true && scan_keyword("true")) {
1525 is_true = true;
1526 goto PICKUP_UNIT;
1527 }
1528 /*tex Complain about unknown unit and |goto done2|. */
1529 scan_dimen_unknown_unit_error();
1530 goto BAD_NEWS;
1531 SCALE_VALUE:
1532 /*tex Adjust |f| for the magnification ratio. */
1533 if (is_true) {
1534 /*tex Maybe at some point we will drop mag completely, even in \DVI\ mode. */
1535 if (output_mode_used <= OMODE_DVI) {
1536 prepare_mag();
1537 if (mag_par != 1000) {
1538 cur_val = xn_over_d(cur_val, 1000, mag_par);
1539 f = (1000 * f + 0200000 * tex_remainder) / mag_par;
1540 cur_val = cur_val + (f / 0200000);
1541 f = f % 0200000;
1542 }
1543 } else {
1544 /*tex in \PDF\ mode we're always |true|. */
1545 one_true_inch = one_inch;
1546 }
1547 }
1548 if (num) {
1549 cur_val = xn_over_d(cur_val, num, denom);
1550 f = (num * f + 0200000 * tex_remainder) / denom;
1551 cur_val = cur_val + (f / 0200000);
1552 f = f % 0200000;
1553 }
1554 BAD_NEWS:
1555 ATTACH_FRACTION:
1556 if (cur_val >= 040000) {
1557 arith_error = true;
1558 } else {
1559 cur_val = cur_val * unity + f;
1560 }
1561 DONE:
1562 /*tex Scan an optional space; this happens too often. */
1563 get_x_token();
1564 if (cur_cmd != spacer_cmd) {
1565 back_input();
1566 }
1567 ATTACH_SIGN:
1568 if (arith_error || (abs(cur_val) >= 010000000000)) {
1569 /*tex Report that this dimension is out of range. */
1570 scan_dimen_out_of_range_error();
1571 cur_val = max_dimen;
1572 arith_error = false;
1573 }
1574 if (negative) {
1575 negate(cur_val);
1576 }
1577 }
1578
1579 /*tex
1580
1581 The final member of \TeX's value-scanning trio is |scan_glue|, which makes
1582 |cur_val| point to a glue specification. The reference count of that glue
1583 spec will take account of the fact that |cur_val| is pointing to~it.
1584
1585 The |level| parameter should be either |glue_val| or |mu_val|.
1586
1587 Since |scan_dimen| was so much more complex than |scan_int|, we might expect
1588 |scan_glue| to be even worse. But fortunately, it is very simple, since most
1589 of the work has already been done.
1590
1591 */
1592
1593 void scan_glue(int level)
1594 {
1595 /*tex should the answer be negated? */
1596 boolean negative = false;
1597 /*tex new glue specification */
1598 halfword q = null;
1599 /*tex does |level=mu_val|? */
1600 boolean mu = (level == mu_val_level);
1601 /*tex Get the next non-blank non-sign. */
1602 do {
1603 /*tex Get the next non-blank non-call token. */
1604 do {
1605 get_x_token();
1606 } while (cur_cmd == spacer_cmd);
1607 if (cur_tok == minus_token) {
1608 negative = !negative;
1609 cur_tok = plus_token;
1610 }
1611 } while (cur_tok == plus_token);
1612 if ((cur_cmd >= min_internal_cmd) && (cur_cmd <= max_internal_cmd)) {
1613 scan_something_internal(level, negative);
1614 if (cur_val_level >= glue_val_level) {
1615 if (cur_val_level != level)
1616 mu_error();
1617 return;
1618 }
1619 if (cur_val_level == int_val_level)
1620 scan_dimen(mu, false, true);
1621 else if (level == mu_val_level)
1622 mu_error();
1623 } else {
1624 back_input();
1625 scan_dimen(mu, false, false);
1626 if (negative)
1627 negate(cur_val);
1628 }
1629 /*tex
1630
1631 Create a new glue specification whose width is |cur_val|; scan for its
1632 stretch and shrink components.
1633
1634 */
1635 q = new_spec(zero_glue);
1636 width(q) = cur_val;
1637 if (scan_keyword("plus")) {
1638 scan_dimen(mu, true, false);
1639 stretch(q) = cur_val;
1640 stretch_order(q) = (quarterword) cur_order;
1641 }
1642 if (scan_keyword("minus")) {
1643 scan_dimen(mu, true, false);
1644 shrink(q) = cur_val;
1645 shrink_order(q) = (quarterword) cur_order;
1646 }
1647 cur_val = q;
1648 }
1649
1650 /*tex
1651
1652 This procedure is supposed to scan something like `\.{\\skip\\count12}',
1653 i.e., whatever can follow `\.{\\the}', and it constructs a token list
1654 containing something like `\.{-3.0pt minus 0.5fill}'.
1655
1656 */
1657
1658 halfword the_toks(void)
1659 {
1660 /*tex holds |selector| setting */
1661 int old_setting;
1662 /*tex used for copying a token list */
1663 halfword p, q, r;
1664 /*tex value of |cur_chr| */
1665 int c;
1666 str_number s;
1667 halfword retval;
1668 /*tex Handle \.{\\unexpanded} or \.{\\detokenize} and |return|. */
1669 if (odd(cur_chr)) {
1670 c = cur_chr;
1671 scan_general_text();
1672 if (c == 1) {
1673 return cur_val;
1674 } else {
1675 old_setting = selector;
1676 selector = new_string;
1677 p = get_avail();
1678 set_token_link(p, token_link(temp_token_head));
1679 token_show(p);
1680 flush_list(p);
1681 selector = old_setting;
1682 s = make_string();
1683 retval = str_toks(str_lstring(s));
1684 flush_str(s);
1685 return retval;
1686 }
1687 }
1688 get_x_token();
1689 scan_something_internal(tok_val_level, false);
1690 if (cur_val_level >= ident_val_level) {
1691 /*tex Copy the token list */
1692 p = temp_token_head;
1693 set_token_link(p, null);
1694 if (cur_val_level == ident_val_level) {
1695 store_new_token(cs_token_flag + cur_val);
1696 } else if (cur_val != null) {
1697 /*tex Do not copy the reference count! */
1698 r = token_link(cur_val);
1699 while (r != null) {
1700 fast_store_new_token(token_info(r));
1701 r = token_link(r);
1702 }
1703 }
1704 return p;
1705 } else {
1706 old_setting = selector;
1707 selector = new_string;
1708 switch (cur_val_level) {
1709 case int_val_level:
1710 print_int(cur_val);
1711 break;
1712 case attr_val_level:
1713 print_int(cur_val);
1714 break;
1715 case dir_val_level:
1716 print_dir_par(cur_val);
1717 break;
1718 case dimen_val_level:
1719 print_scaled(cur_val);
1720 tprint("pt");
1721 break;
1722 case glue_val_level:
1723 print_spec(cur_val, "pt");
1724 flush_node(cur_val);
1725 break;
1726 case mu_val_level:
1727 print_spec(cur_val, "mu");
1728 flush_node(cur_val);
1729 break;
1730 }
1731 selector = old_setting;
1732 s = make_string();
1733 retval = str_toks(str_lstring(s));
1734 flush_str(s);
1735 return retval;
1736 }
1737 }
1738
1739 str_number the_scanned_result(void)
1740 {
1741 /*tex holds |selector| setting */
1742 int old_setting;
1743 /*tex return value */
1744 str_number r;
1745 old_setting = selector;
1746 selector = new_string;
1747 if (cur_val_level >= ident_val_level) {
1748 if (cur_val != null) {
1749 show_token_list(token_link(cur_val), null, -1);
1750 r = make_string();
1751 } else {
1752 r = get_nullstr();
1753 }
1754 } else {
1755 switch (cur_val_level) {
1756 case int_val_level:
1757 print_int(cur_val);
1758 break;
1759 case attr_val_level:
1760 print_int(cur_val);
1761 break;
1762 case dir_val_level:
1763 print_dir_par(cur_val);
1764 break;
1765 case dimen_val_level:
1766 print_scaled(cur_val);
1767 tprint("pt");
1768 break;
1769 case glue_val_level:
1770 print_spec(cur_val, "pt");
1771 flush_node(cur_val);
1772 break;
1773 case mu_val_level:
1774 print_spec(cur_val, "mu");
1775 flush_node(cur_val);
1776 break;
1777 }
1778 r = make_string();
1779 }
1780 selector = old_setting;
1781 return r;
1782 }
1783
1784 /*tex
1785
1786 The following routine is used to implement `\.{\\fontdimen} |n| |f|'. The
1787 boolean parameter |writing| is set |true| if the calling program intends to
1788 change the parameter value.
1789
1790 */
1791
1792 static void font_param_error(int f)
1793 {
1794 print_err("Font ");
1795 print_esc(font_id_text(f));
1796 tprint(" has only ");
1797 print_int(font_params(f));
1798 tprint(" fontdimen parameters");
1799 help2(
1800 "To increase the number of font parameters, you must",
1801 "use \\fontdimen immediately after the \\font is loaded."
1802 );
1803 error();
1804 }
1805
1806 void set_font_dimen(void)
1807 {
1808 internal_font_number f;
1809 int n;
1810 scan_int();
1811 n = cur_val;
1812 scan_font_ident();
1813 f = cur_val;
1814 if (n <= 0) {
1815 font_param_error(f);
1816 } else if (n > font_params(f)) {
1817 if (font_used(f)) {
1818 font_param_error(f);
1819 } else {
1820 /*tex Increase the number of parameters in the font. */
1821 // do {
1822 // set_font_param(f, (font_params(f) + 1), 0);
1823 // } while (n != font_params(f));
1824 set_font_params(f, n);
1825 }
1826 }
1827 scan_optional_equals();
1828 scan_normal_dimen();
1829 set_font_param(f, n, cur_val);
1830 }
1831
1832 void get_font_dimen(void)
1833 {
1834 internal_font_number f;
1835 /*tex The parameter number: */
1836 int n;
1837 scan_int();
1838 n = cur_val;
1839 scan_font_ident();
1840 f = cur_val;
1841 /*tex Initialize return value: */
1842 cur_val = 0;
1843 if (n <= 0) {
1844 font_param_error(f);
1845 goto EXIT;
1846 } else if (n > font_params(f)) {
1847 if (font_used(f)) {
1848 font_param_error(f);
1849 goto EXIT;
1850 } else {
1851 /*tex Increase the number of parameters in the font. */
1852 // do {
1853 // set_font_param(f, (font_params(f) + 1), 0);
1854 // } while (n != font_params(f));
1855 set_font_params(f, n);
1856 }
1857 }
1858 cur_val = font_param(f, n);
1859 EXIT:
1860 scanned_result(cur_val, dimen_val_level);
1861 }
1862
1863 /*tex
1864
1865 Here's a similar procedure that returns a pointer to a rule node. This
1866 routine is called just after \TeX\ has seen \.{\\hrule} or \.{\\vrule};
1867 therefore |cur_cmd| will be either |hrule| or |vrule|. The idea is to store
1868 the default rule dimensions in the node, then to override them if
1869 `\.{height}' or `\.{width}' or `\.{depth}' specifications are found (in any
1870 order).
1871
1872 */
1873
1874 halfword scan_rule_spec(void)
1875 {
1876 /*tex |width|, |depth|, and |height| all equal |null_flag| now */
1877 halfword q;
1878 if (cur_cmd == no_vrule_cmd) {
1879 q = new_rule(empty_rule);
1880 cur_cmd = vrule_cmd;
1881 } else if (cur_cmd == no_hrule_cmd) {
1882 q = new_rule(empty_rule);
1883 cur_cmd = hrule_cmd;
1884 } else {
1885 q = new_rule(normal_rule);
1886 }
1887 if (cur_cmd == vrule_cmd) {
1888 width(q) = default_rule;
1889 rule_dir(q) = body_direction_par;
1890 } else {
1891 height(q) = default_rule;
1892 depth(q) = 0;
1893 rule_dir(q) = text_direction_par;
1894 }
1895 RESWITCH:
1896 if (scan_keyword("width")) {
1897 scan_normal_dimen();
1898 width(q) = cur_val;
1899 goto RESWITCH;
1900 }
1901 if (scan_keyword("height")) {
1902 scan_normal_dimen();
1903 height(q) = cur_val;
1904 goto RESWITCH;
1905 }
1906 if (scan_keyword("depth")) {
1907 scan_normal_dimen();
1908 depth(q) = cur_val;
1909 goto RESWITCH;
1910 }
1911 return q;
1912 }
1913
1914 /*tex Declare procedures that scan font-related stuff. */
1915
1916 void scan_font_ident(void)
1917 {
1918 internal_font_number f;
1919 halfword m;
1920 /*tex Get the next non-blank non-call. */
1921 do {
1922 get_x_token();
1923 } while (cur_cmd == spacer_cmd);
1924 if ((cur_cmd == def_font_cmd) || (cur_cmd == letterspace_font_cmd) || (cur_cmd == copy_font_cmd)) {
1925 f = get_cur_font();
1926 } else if (cur_cmd == set_font_cmd) {
1927 f = cur_chr;
1928 set_font_touched(f, 1);
1929 } else if (cur_cmd == def_family_cmd) {
1930 m = cur_chr;
1931 scan_math_family_int();
1932 f = fam_fnt(cur_val, m);
1933 set_font_touched(f, 1);
1934 } else {
1935 print_err("Missing font identifier");
1936 help2(
1937 "I was looking for a control sequence whose",
1938 "current meaning has been defined by \\font."
1939 );
1940 back_error();
1941 f = null_font;
1942 }
1943 cur_val = f;
1944 }
1945
1946 /*tex
1947
1948 The |scan_general_text| procedure is much like |scan_toks(false,false)|, but
1949 will be invoked via |expand|, i.e., recursively.
1950
1951 The token list (balanced text) created by |scan_general_text| begins at
1952 |link(temp_token_head)| and ends at |cur_val|. (If |cur_val=temp_token_head|,
1953 the list is empty.)
1954
1955 */
1956
1957 void scan_general_text(void)
1958 {
1959 /*tex Here we save |scanner_status|: */
1960 int s;
1961 /*tex Here we save |warning_index|: */
1962 halfword w;
1963 /*tex Here we save |def_ref|: */
1964 halfword d;
1965 /*tex The tail of the token list being built: */
1966 halfword p;
1967 /*tex The new node being added to the token list via |store_new_token|: */
1968 halfword q;
1969 /*tex The number of unmatched left braces: */
1970 halfword unbalance;
1971 s = scanner_status;
1972 w = warning_index;
1973 d = def_ref;
1974 scanner_status = absorbing;
1975 warning_index = cur_cs;
1976 p = get_avail();
1977 def_ref = p;
1978 set_token_ref_count(def_ref, 0);
1979 p = def_ref;
1980 /*tex Remove the compulsory left brace. */
1981 scan_left_brace();
1982 unbalance = 1;
1983 while (1) {
1984 get_token();
1985 if (cur_tok < right_brace_limit) {
1986 if (cur_cmd < right_brace_cmd) {
1987 incr(unbalance);
1988 } else {
1989 decr(unbalance);
1990 if (unbalance == 0)
1991 break;
1992 }
1993 }
1994 store_new_token(cur_tok);
1995 }
1996 q = token_link(def_ref);
1997 /*tex Discard reference count. */
1998 free_avail(def_ref);
1999 if (q == null)
2000 cur_val = temp_token_head;
2001 else
2002 cur_val = p;
2003 set_token_link(temp_token_head, q);
2004 scanner_status = s;
2005 warning_index = w;
2006 def_ref = d;
2007 }
2008
2009 /*tex
2010
2011 The |get_x_or_protected| procedure is like |get_x_token| except that
2012 protected macros are not expanded. It sets |cur_cmd|, |cur_chr|,
2013 |cur_tok|,and expands non-protected macros.
2014
2015 */
2016
2017 void get_x_or_protected(void)
2018 {
2019
2020 while (1) {
2021 get_token();
2022 if (cur_cmd <= max_command_cmd)
2023 return;
2024 if ((cur_cmd >= call_cmd) && (cur_cmd < end_template_cmd)) {
2025 if (token_info(token_link(cur_chr)) == protected_token)
2026 return;
2027 }
2028 expand();
2029 }
2030 }
2031
2032 /*tex
2033
2034 |scan_toks|. This function returns a pointer to the tail of a new token list,
2035 and it also makes |def_ref| point to the reference count at the head of that
2036 list.
2037
2038 There are two boolean parameters, |macro_def| and |xpand|. If |macro_def| is
2039 true, the goal is to create the token list for a macro definition; otherwise
2040 the goal is to create the token list for some other \TeX\ primitive:
2041 \.{\\mark}, \.{\\output}, \.{\\everypar}, \.{\\lowercase}, \.{\\uppercase},
2042 \.{\\message}, \.{\\errmessage}, \.{\\write}, or \.{\\special}. In the latter
2043 cases a left brace must be scanned next; this left brace will not be part of
2044 the token list, nor will the matching right brace that comes at the end. If
2045 |xpand| is false, the token list will simply be copied from the input using
2046 |get_token|. Otherwise all expandable tokens will be expanded until
2047 unexpandable tokens are left, except that the results of expanding
2048 `\.{\\the}' are not expanded further. If both |macro_def| and |xpand| are
2049 true, the expansion applies only to the macro body (i.e., to the material
2050 following the first |left_brace| character).
2051
2052 The value of |cur_cs| when |scan_toks| begins should be the |eqtb| address of
2053 the control sequence to display in ``runaway'' error messages.
2054
2055 */
2056
2057 halfword scan_toks(boolean macro_def, boolean xpand)
2058 {
2059 /*tex token representing the highest parameter number */
2060 halfword t;
2061 /*tex saved token */
2062 halfword s;
2063 /*tex tail of the token list being built */
2064 halfword p;
2065 /*tex new node being added to the token list via |store_new_token| */
2066 halfword q;
2067 /*tex number of unmatched left braces */
2068 halfword unbalance;
2069 /*tex possible `\.{\#\{}' token */
2070 halfword hash_brace;
2071 if (macro_def)
2072 scanner_status = defining;
2073 else
2074 scanner_status = absorbing;
2075 warning_index = cur_cs;
2076 p = get_avail();
2077 def_ref = p;
2078 set_token_ref_count(def_ref, 0);
2079 p = def_ref;
2080 hash_brace = 0;
2081 t = zero_token;
2082 if (macro_def) {
2083 /*tex Scan and build the parameter part of the macro definition. */
2084 while (1) {
2085 /*tex Set |cur_cmd|, |cur_chr|, |cur_tok|: */
2086 get_token();
2087 if (cur_tok < right_brace_limit)
2088 break;
2089 if (cur_cmd == mac_param_cmd) {
2090 /*tex
2091
2092 If the next character is a parameter number, make
2093 |cur_tok| a |match| token; but if it is a left brace,
2094 store `|left_brace|, |end_match|', set |hash_brace|, and
2095 |goto done|.
2096
2097 */
2098 s = match_token + cur_chr;
2099 get_token();
2100 /* if (cur_cmd == left_brace_cmd) { */
2101 if (cur_tok < left_brace_limit) {
2102 hash_brace = cur_tok;
2103 store_new_token(cur_tok);
2104 store_new_token(end_match_token);
2105 goto DONE;
2106 }
2107 if (t == nine_token) {
2108 print_err("You already have nine parameters");
2109 help2(
2110 "I'm going to ignore the # sign you just used,",
2111 "as well as the token that followed it."
2112 );
2113 error();
2114 } else {
2115 incr(t);
2116 if (cur_tok != t) {
2117 print_err("Parameters must be numbered consecutively");
2118 help2(
2119 "I've inserted the digit you should have used after the #.",
2120 "Type `1' to delete what you did use."
2121 );
2122 back_error();
2123 }
2124 cur_tok = s;
2125 }
2126 }
2127 store_new_token(cur_tok);
2128 }
2129 store_new_token(end_match_token);
2130 if (cur_cmd == right_brace_cmd) {
2131 /*tex Express shock at the missing left brace; |goto found|. */
2132 print_err("Missing { inserted");
2133 incr(align_state);
2134 help2(
2135 "Where was the left brace? You said something like `\\def\\a}',",
2136 "which I'm going to interpret as `\\def\\a{}'."
2137 );
2138 error();
2139 goto FOUND;
2140 }
2141
2142 } else {
2143 /*tex Remove the compulsory left brace. */
2144 scan_left_brace();
2145 }
2146 DONE:
2147 /*tex Scan and build the body of the token list; |goto found| when finished. */
2148 unbalance = 1;
2149 while (1) {
2150 if (xpand) {
2151 /*tex
2152
2153 Expand the next part of the input. Here we insert an entire token
2154 list created by |the_toks| without expanding it further.
2155
2156 */
2157 while (1) {
2158 get_next();
2159 if (cur_cmd >= call_cmd) {
2160 if (token_info(token_link(cur_chr)) == protected_token) {
2161 cur_cmd = relax_cmd;
2162 cur_chr = no_expand_flag;
2163 }
2164 }
2165 if (cur_cmd <= max_command_cmd)
2166 break;
2167 if (cur_cmd != the_cmd) {
2168 expand();
2169 } else {
2170 q = the_toks();
2171 if (token_link(temp_token_head) != null) {
2172 set_token_link(p, token_link(temp_token_head));
2173 p = q;
2174 }
2175 }
2176 }
2177 x_token();
2178 } else {
2179 get_token();
2180 }
2181 if (cur_tok < right_brace_limit) {
2182 if (cur_cmd < right_brace_cmd) {
2183 incr(unbalance);
2184 } else {
2185 decr(unbalance);
2186 if (unbalance == 0)
2187 goto FOUND;
2188 }
2189 } else if (cur_cmd == mac_param_cmd) {
2190 if (macro_def) {
2191 /*tex Look for parameter number or \.{\#\#}. */
2192 s = cur_tok;
2193 if (xpand)
2194 get_x_token();
2195 else
2196 get_token();
2197 if (cur_cmd != mac_param_cmd) {
2198 if ((cur_tok <= zero_token) || (cur_tok > t)) {
2199 print_err("Illegal parameter number in definition of ");
2200 sprint_cs(warning_index);
2201 help3(
2202 "You meant to type ## instead of #, right?",
2203 "Or maybe a } was forgotten somewhere earlier, and things",
2204 "are all screwed up? I'm going to assume that you meant ##."
2205 );
2206 back_error();
2207 cur_tok = s;
2208 } else {
2209 cur_tok = out_param_token - '0' + cur_chr;
2210 }
2211 }
2212 }
2213 }
2214 store_new_token(cur_tok);
2215 }
2216 FOUND:
2217 scanner_status = normal;
2218 if (hash_brace != 0)
2219 store_new_token(hash_brace);
2220 return p;
2221 }
2222
2223 /*tex
2224
2225 Here we declare two trivial procedures in order to avoid mutually recursive
2226 procedures with parameters.
2227
2228 */
2229
2230 void scan_normal_glue(void)
2231 {
2232 scan_glue(glue_val_level);
2233 }
2234
2235 void scan_mu_glue(void)
2236 {
2237 scan_glue(mu_val_level);
2238 }
2239
2240 /*tex
2241
2242 The |scan_expr| procedure scans and evaluates an expression. Evaluating an
2243 expression is a recursive process: When the left parenthesis of a
2244 subexpression is scanned we descend to the next level of recursion; the
2245 previous level is resumed with the matching right parenthesis.
2246
2247 */
2248
2249 typedef enum {
2250 /*tex \.( seen, or \.( $\langle\it expr\rangle$ \.) seen */
2251 expr_none = 0,
2252 /*tex \.( $\langle\it expr\rangle$ \.+ seen */
2253 expr_add = 1,
2254 /*tex \.( $\langle\it expr\rangle$ \.- seen */
2255 expr_sub = 2,
2256 /*tex $\langle\it term\rangle$ \.* seen */
2257 expr_mult = 3,
2258 /*tex $\langle\it term\rangle$ \./ seen */
2259 expr_div = 4,
2260 /*tex $\langle\it term\rangle$ \.* $\langle\it factor\rangle$ \./ seen */
2261 expr_scale = 5,
2262 } expression_states;
2263
2264 /*tex
2265
2266 We want to make sure that each term and (intermediate) result is in the
2267 proper range. Integer values must not exceed |infinity| ($2^{31}-1$) in
2268 absolute value, dimensions must not exceed |max_dimen| ($2^{30}-1$). We avoid
2269 the absolute value of an integer, because this might fail for the value
2270 $-2^{31}$ using 32-bit arithmetic.
2271
2272 */
2273
2274 /* Clear a number or dimension and set |arith_error|: */
2275
2276 #define num_error(A) do { \
2277 arith_error=true; \
2278 A=0; \
2279 } while (0)
2280
2281 /*tex Clear a glue spec and set |arith_error|: */
2282
2283 #define glue_error(A) do { \
2284 arith_error=true; \
2285 reset_glue_to_zero(A); \
2286 } while (0)
2287
2288 #define normalize_glue(A) do { \
2289 if (stretch(A)==0) stretch_order(A)=normal; \
2290 if (shrink(A)==0) shrink_order(A)=normal; \
2291 } while (0)
2292
2293 /*tex
2294
2295 Parenthesized subexpressions can be inside expressions, and this nesting has
2296 a stack. Seven local variables represent the top of the expression stack: |p|
2297 points to pushed-down entries, if any; |l| specifies the type of expression
2298 currently beeing evaluated; |e| is the expression so far and |r| is the state
2299 of its evaluation; |t| is the term so far and |s| is the state of its
2300 evaluation; finally |n| is the numerator for a combined multiplication and
2301 division, if any.
2302
2303 */
2304
2305 #define expr_add_sub(A,B,C) add_or_sub((A),(B),(C),(r==expr_sub))
2306 #define expr_a(A,B) expr_add_sub((A),(B),max_dimen)
2307
2308 /*tex
2309
2310 The function |add_or_sub(x,y,max_answer,negative)| computes the sum (for
2311 |negative=false|) or difference (for |negative=true|) of |x| and |y|,
2312 provided the absolute value of the result does not exceed |max_answer|.
2313
2314 */
2315
2316 inline static int add_or_sub(int x, int y, int max_answer, boolean negative)
2317 {
2318 int a;
2319 if (negative)
2320 negate(y);
2321 if (x >= 0) {
2322 if (y <= max_answer - x)
2323 a = x + y;
2324 else
2325 num_error(a);
2326 } else if (y >= -max_answer - x) {
2327 a = x + y;
2328 } else {
2329 num_error(a);
2330 }
2331 return a;
2332 }
2333
2334 #define expr_m(A) A = nx_plus_y((A),f,0)
2335 #define expr_d(A) A=quotient((A),f)
2336
2337 /*tex
2338
2339 The function |quotient(n,d)| computes the rounded quotient $q=\lfloor
2340 n/d+{1\over2}\rfloor$, when $n$ and $d$ are positive.
2341
2342 */
2343
2344 inline static int quotient(int n, int d)
2345 {
2346 /*tex Should the answer be negated? */
2347 boolean negative;
2348 /*tex The answer: */
2349 int a;
2350 if (d == 0) {
2351 num_error(a);
2352 } else {
2353 if (d > 0) {
2354 negative = false;
2355 } else {
2356 negate(d);
2357 negative = true;
2358 }
2359 if (n < 0) {
2360 negate(n);
2361 negative = !negative;
2362 }
2363 a = n / d;
2364 n = n - a * d;
2365 /*tex Avoid certain compiler optimizations! */
2366 d = n - d;
2367 if (d + n >= 0)
2368 incr(a);
2369 if (negative)
2370 negate(a);
2371 }
2372 return a;
2373 }
2374
2375 #define expr_s(A) A=fract((A),n,f,max_dimen)
2376
2377 /*tex
2378
2379 Finally, the function |fract(x,n,d,max_answer)| computes the integer
2380 $q=\lfloor xn/d+{1\over2}\rfloor$, when $x$, $n$, and $d$ are positive and
2381 the result does not exceed |max_answer|. We can't use floating point
2382 arithmetic since the routine must produce identical results in all cases; and
2383 it would be too dangerous to multiply by~|n| and then divide by~|d|, in
2384 separate operations, since overflow might well occur. Hence this subroutine
2385 simulates double precision arithmetic, somewhat analogous to Metafont's
2386 |make_fraction| and |take_fraction| routines.
2387
2388 */
2389
2390 int fract(int x, int n, int d, int max_answer)
2391 {
2392 /*tex should the answer be negated? */
2393 boolean negative;
2394 /*tex the answer */
2395 int a;
2396 /*tex a proper fraction */
2397 int f;
2398 /*tex smallest integer such that |2*h>=d| */
2399 int h;
2400 /*tex intermediate remainder */
2401 int r;
2402 /*tex temp variable */
2403 int t;
2404 if (d == 0)
2405 goto TOO_BIG;
2406 a = 0;
2407 if (d > 0) {
2408 negative = false;
2409 } else {
2410 negate(d);
2411 negative = true;
2412 }
2413 if (x < 0) {
2414 negate(x);
2415 negative = !negative;
2416 } else if (x == 0) {
2417 goto DONE;
2418 }
2419 if (n < 0) {
2420 negate(n);
2421 negative = !negative;
2422 }
2423 t = n / d;
2424 if (t > max_answer / x)
2425 goto TOO_BIG;
2426 a = t * x;
2427 n = n - t * d;
2428 if (n == 0)
2429 goto FOUND;
2430 t = x / d;
2431 if (t > (max_answer - a) / n)
2432 goto TOO_BIG;
2433 a = a + t * n;
2434 x = x - t * d;
2435 if (x == 0)
2436 goto FOUND;
2437 if (x < n) {
2438 t = x;
2439 x = n;
2440 n = t;
2441 }
2442 /*tex
2443
2444 Now |0<n<=x<d| and we compute $f=\lfloor xn/d+{1\over2}\rfloor$. The loop
2445 here preserves the following invariant relations between |f|, |x|, |n|,
2446 and~|r|: (i)~$f+\lfloor(xn+(r+d))/d\rfloor=\lfloor
2447 x_0n_0/d+{1\over2}\rfloor$; (ii)~|-d<=r<0<n<=x<d|, where $x_0$, $n_0$ are
2448 the original values of~$x$ and $n$.
2449
2450 Notice that the computation specifies |(x-d)+x| instead of |(x+x)-d|,
2451 because the latter could overflow.
2452
2453 */
2454 f = 0;
2455 r = (d / 2) - d;
2456 h = -r;
2457 while (1) {
2458 if (odd(n)) {
2459 r = r + x;
2460 if (r >= 0) {
2461 r = r - d;
2462 incr(f);
2463 }
2464 }
2465 n = n / 2;
2466 if (n == 0)
2467 break;
2468 if (x < h) {
2469 x = x + x;
2470 } else {
2471 t = x - d;
2472 x = t + x;
2473 f = f + n;
2474 if (x < n) {
2475 if (x == 0)
2476 break;
2477 t = x;
2478 x = n;
2479 n = t;
2480 }
2481 }
2482 }
2483 if (f > (max_answer - a))
2484 goto TOO_BIG;
2485 a = a + f;
2486 FOUND:
2487 if (negative)
2488 negate(a);
2489 goto DONE;
2490 TOO_BIG:
2491 num_error(a);
2492 DONE:
2493 return a;
2494 }
2495
2496 /*tex Scans and evaluates an expression: */
2497
2498 static void scan_expr(void)
2499 {
2500 /*tex saved values of |arith_error| */
2501 boolean a, b;
2502 /*tex type of expression */
2503 int l;
2504 /*tex state of expression so far */
2505 int r;
2506 /*tex state of term so far */
2507 int s;
2508 /*tex next operation or type of next factor */
2509 int o;
2510 /*tex expression so far */
2511 int e;
2512 /*tex term so far */
2513 int t;
2514 /*tex current factor */
2515 int f;
2516 /*tex numerator of combined multiplication and division */
2517 int n;
2518 /*tex top of expression stack */
2519 halfword p;
2520 /*tex for stack manipulations */
2521 halfword q;
2522 l = cur_val_level;
2523 a = arith_error;
2524 b = false;
2525 p = null;
2526 /*tex
2527
2528 Scan and evaluate an expression |e| of type |l|.
2529 To avoid an infinite recursion we set|max_nesting| as upper limit.
2530 This limit is unrelated to the expansion limit |expand_depth| and it cannot be modify at compile time.
2531
2532 */
2533 nesting++;
2534 if (nesting > max_nesting) {
2535 formatted_error("tex", "\\*expr can only be nested %d deep",max_nesting);
2536 }
2537 RESTART:
2538 r = expr_none;
2539 e = 0;
2540 s = expr_none;
2541 t = 0;
2542 n = 0;
2543 CONTINUE:
2544 if (s == expr_none)
2545 o = l;
2546 else
2547 o = int_val_level;
2548 /*tex
2549
2550 Scan a factor |f| of type |o| or start a subexpression. Get the next
2551 non-blank non-call token.
2552
2553 */
2554 do {
2555 get_x_token();
2556 } while (cur_cmd == spacer_cmd);
2557
2558 if (cur_tok == other_token + '(') {
2559 /*tex Push the expression stack and |goto restart|. */
2560 q = new_node(expr_node, 0);
2561 vlink(q) = p;
2562 expr_type(q) = (quarterword) l;
2563 expr_state(q) = (quarterword) (4 * s + r);
2564 expr_e_field(q) = e;
2565 expr_t_field(q) = t;
2566 expr_n_field(q) = n;
2567 p = q;
2568 l = o;
2569 goto RESTART;
2570 }
2571 back_input();
2572 if ((o == int_val_level) || (o == attr_val_level))
2573 scan_int();
2574 else if (o == dimen_val_level)
2575 scan_normal_dimen();
2576 else if (o == glue_val_level)
2577 scan_normal_glue();
2578 else
2579 scan_mu_glue();
2580 f = cur_val;
2581
2582 FOUND:
2583 /*tex
2584
2585 Scan the next operator and set |o| and Get the next non-blank non-call
2586 token.
2587
2588 */
2589 do {
2590 get_x_token();
2591 } while (cur_cmd == spacer_cmd);
2592
2593 if (cur_tok == other_token + '+') {
2594 o = expr_add;
2595 } else if (cur_tok == other_token + '-') {
2596 o = expr_sub;
2597 } else if (cur_tok == other_token + '*') {
2598 o = expr_mult;
2599 } else if (cur_tok == other_token + '/') {
2600 o = expr_div;
2601 } else {
2602 o = expr_none;
2603 if (p == null) {
2604 if (cur_cmd != relax_cmd)
2605 back_input();
2606 } else if (cur_tok != other_token + ')') {
2607 print_err("Missing ) inserted for expression");
2608 help1("I was expecting to see `+', `-', `*', `/', or `)'. Didn't.");
2609 back_error();
2610 }
2611 }
2612 arith_error = b;
2613 /*tex Make sure that |f| is in the proper range. */
2614 if (((l == int_val_level) || (l == attr_val_level)) || (s > expr_sub)) {
2615 if ((f > infinity) || (f < -infinity))
2616 num_error(f);
2617 } else if (l == dimen_val_level) {
2618 if (abs(f) > max_dimen)
2619 num_error(f);
2620 } else {
2621 if ((abs(width(f)) > max_dimen) || (abs(stretch(f)) > max_dimen) || (abs(shrink(f)) > max_dimen))
2622 glue_error(f);
2623 }
2624 /*tex Cases for evaluation of the current term. */
2625 switch (s) {
2626 case expr_none:
2627 /*tex
2628
2629 Applying the factor |f| to the partial term |t| (with the
2630 operator |s|) is delayed until the next operator |o| has been
2631 scanned. Here we handle the first factor of a partial term. A
2632 glue spec has to be copied unless the next operator is a right
2633 parenthesis; this allows us later on to simply modify the glue
2634 components.
2635
2636 */
2637 t = f;
2638 if ((l >= glue_val_level) && (o != expr_none)) {
2639 /*tex Do we really need to copy here? */
2640 normalize_glue(t);
2641 } else {
2642 t = f;
2643 }
2644 break;
2645 case expr_mult:
2646 /*tex
2647
2648 If a multiplication is followed by a division, the two operations
2649 are combined into a `scaling' operation. Otherwise the term |t|
2650 is multiplied by the factor |f|.
2651
2652 */
2653 if (o == expr_div) {
2654 n = f;
2655 o = expr_scale;
2656 } else if ((l == int_val_level) || (l == attr_val_level)) {
2657 t = mult_integers(t, f);
2658 } else if (l == dimen_val_level) {
2659 expr_m(t);
2660 } else {
2661 expr_m(width(t));
2662 expr_m(stretch(t));
2663 expr_m(shrink(t));
2664 }
2665 break;
2666 case expr_div:
2667 /*tex Here we divide the term |t| by the factor |f|. */
2668 if (l < glue_val_level) {
2669 expr_d(t);
2670 } else {
2671 expr_d(width(t));
2672 expr_d(stretch(t));
2673 expr_d(shrink(t));
2674 }
2675 break;
2676 case expr_scale:
2677 /*tex Here the term |t| is multiplied by the quotient $n/f$. */
2678 if ((l == int_val_level) || (l == attr_val_level)) {
2679 t = fract(t, n, f, infinity);
2680 } else if (l == dimen_val_level) {
2681 expr_s(t);
2682 } else {
2683 expr_s(width(t));
2684 expr_s(stretch(t));
2685 expr_s(shrink(t));
2686 }
2687 break;
2688 }
2689 if (o > expr_sub) {
2690 s = o;
2691 } else {
2692 /*tex
2693
2694 Evaluate the current expression. When a term |t| has been completed
2695 it is copied to, added to, or subtracted from the expression |e|.
2696
2697 */
2698 s = expr_none;
2699 if (r == expr_none) {
2700 e = t;
2701 } else if ((l == int_val_level) || (l == attr_val_level)) {
2702 e = expr_add_sub(e, t, infinity);
2703 } else if (l == dimen_val_level) {
2704 e = expr_a(e, t);
2705 } else {
2706 /*tex
2707
2708 Compute the sum or difference of two glue specs. We know that
2709 |stretch_order(e)>normal| implies |stretch(e)<>0| and
2710 |shrink_order(e)>normal| implies |shrink(e)<>0|.
2711
2712 */
2713 width(e) = expr_a(width(e), width(t));
2714 if (stretch_order(e) == stretch_order(t)) {
2715 stretch(e) = expr_a(stretch(e), stretch(t));
2716 } else if ((stretch_order(e) < stretch_order(t)) && (stretch(t) != 0)) {
2717 stretch(e) = stretch(t);
2718 stretch_order(e) = stretch_order(t);
2719 }
2720 if (shrink_order(e) == shrink_order(t)) {
2721 shrink(e) = expr_a(shrink(e), shrink(t));
2722 } else if ((shrink_order(e) < shrink_order(t)) && (shrink(t) != 0)) {
2723 shrink(e) = shrink(t);
2724 shrink_order(e) = shrink_order(t);
2725 }
2726 flush_node(t);
2727 normalize_glue(e);
2728 }
2729 r = o;
2730 }
2731 b = arith_error;
2732 if (o != expr_none)
2733 goto CONTINUE;
2734 if (p != null) {
2735 /*tex Pop the expression stack and |goto found|. */
2736 f = e;
2737 q = p;
2738 e = expr_e_field(q);
2739 t = expr_t_field(q);
2740 n = expr_n_field(q);
2741 s = expr_state(q) / 4;
2742 r = expr_state(q) % 4;
2743 l = expr_type(q);
2744 p = vlink(q);
2745 flush_node(q);
2746 goto FOUND;
2747 }
2748
2749 if (b) {
2750 print_err("Arithmetic overflow");
2751 help2(
2752 "I can't evaluate this expression,",
2753 "since the result is out of range."
2754 );
2755 error();
2756 if (l >= glue_val_level) {
2757 reset_glue_to_zero(e);
2758 } else {
2759 e = 0;
2760 }
2761 }
2762 arith_error = a;
2763 cur_val = e;
2764 cur_val_level = l;
2765 nesting--;
2766 }