Debian Codegraph @

texk/web2c/luatexdir/tex/textoken.c

raw
1 /*
2
3 Copyright 2006-2011 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 #define detokenized_line() (line_catcode_table==NO_CAT_TABLE)
23
24 #define do_get_cat_code(a,b) do { \
25 if (line_catcode_table==DEFAULT_CAT_TABLE) \
26 a=get_cat_code(cat_code_table_par,b); \
27 else if (line_catcode_table>-0xFF) \
28 a=get_cat_code(line_catcode_table,b); \
29 else \
30 a= - line_catcode_table - 0xFF ; \
31 } while (0)
32
33
34 /*tex
35
36 The \TeX\ system does nearly all of its own memory allocation, so that it can
37 readily be transported into environments that do not have automatic
38 facilities for strings, garbage collection, etc., and so that it can be in
39 control of what error messages the user receives. The dynamic storage
40 requirements of \TeX\ are handled by providing two large arrays called
41 |fixmem| and |varmem| in which consecutive blocks of words are used as nodes
42 by the \TeX\ routines.
43
44 Pointer variables are indices into this array, or into another array called
45 |eqtb| that will be explained later. A pointer variable might also be a
46 special flag that lies outside the bounds of |mem|, so we allow pointers to
47 assume any |halfword| value. The minimum halfword value represents a null
48 pointer. \TeX\ does not assume that |mem[null]| exists.
49
50 Locations in |fixmem| are used for storing one-word records; a conventional
51 \.{AVAIL} stack is used for allocation in this array.
52
53 */
54
55 /*tex the big dynamic storage area */
56
57 smemory_word *fixmem;
58
59 /*tex the smallest location of one-word memory in use */
60
61 unsigned fix_mem_min;
62
63 /*tex the largest location of one-word memory in use */
64
65 unsigned fix_mem_max;
66
67 /*tex
68
69 In order to study the memory requirements of particular applications, it is
70 possible to prepare a version of \TeX\ that keeps track of current and
71 maximum memory usage. When code between the delimiters |stat| $\ldots$
72 |tats| is not commented out, \TeX\ will run a bit slower but it will report
73 these statistics when |tracing_stats| is sufficiently large.
74
75 */
76
77 /*tex how much memory is in use */
78
79 int dyn_used;
80
81 /*tex head of the list of available one-word nodes */
82
83 halfword avail;
84
85 /*tex the last one-word node used in |mem| */
86
87 unsigned fix_mem_end;
88
89 /*tex head of a junk list, write only */
90
91 halfword garbage;
92
93 /*tex head of a temporary list of some kind */
94
95 halfword temp_token_head;
96
97 /*tex head of a temporary list of another kind */
98
99 halfword hold_token_head;
100
101 /*tex a constant token list */
102
103 halfword omit_template;
104
105 /*tex permanently empty list */
106
107 halfword null_list;
108
109 /*tex head of token list built by |scan_keyword| */
110
111 halfword backup_head;
112
113 void initialize_tokens(void)
114 {
115 halfword p;
116 avail = null;
117 fix_mem_end = 0;
118 p = get_avail();
119 temp_token_head = p;
120 set_token_info(temp_token_head, 0);
121 p = get_avail();
122 hold_token_head = p;
123 set_token_info(hold_token_head, 0);
124 p = get_avail();
125 omit_template = p;
126 set_token_info(omit_template, 0);
127 p = get_avail();
128 null_list = p;
129 set_token_info(null_list, 0);
130 p = get_avail();
131 backup_head = p;
132 set_token_info(backup_head, 0);
133 p = get_avail();
134 garbage = p;
135 set_token_info(garbage, 0);
136 dyn_used = 0;
137 }
138
139 /*tex
140
141 The function |get_avail| returns a pointer to a new one-word node whose
142 |link| field is null. However, \TeX\ will halt if there is no more room left.
143
144 If the available-space list is empty, i.e., if |avail=null|, we try first to
145 increase |fix_mem_end|. If that cannot be done, i.e., if
146 |fix_mem_end=fix_mem_max|, we try to reallocate array |fixmem|. If, that
147 doesn't work, we have to quit.
148
149 Single-word node allocation:
150 */
151
152 halfword get_avail(void)
153 {
154 /*tex The new node being got: */
155 unsigned p;
156 unsigned t;
157 /*tex Get top location in the |avail| stack. */
158 p = (unsigned) avail;
159 if (p != null) {
160 /*tex Pop it off. */
161 avail = token_link(avail);
162 } else if (fix_mem_end < fix_mem_max) {
163 /*tex Go into virgin territory. */
164 incr(fix_mem_end);
165 p = fix_mem_end;
166 } else {
167 /*tex The big dynamic storage area. */
168 smemory_word *new_fixmem;
169 t = (fix_mem_max / 5);
170 new_fixmem = fixmemcast(realloc(fixmem, sizeof(smemory_word) * (fix_mem_max + t + 1)));
171 if (new_fixmem == NULL) {
172 /*tex If memory is exhausted, display possible runaway text. */
173 runaway();
174 overflow("token memory size", fix_mem_max);
175 } else {
176 fixmem = new_fixmem;
177 }
178 memset(voidcast(fixmem + fix_mem_max + 1), 0, t * sizeof(smemory_word));
179 fix_mem_max += t;
180 p = ++fix_mem_end;
181 }
182 /*tex Provide an oft-desired initialization of the new node. */
183 token_link(p) = null;
184 /*tex Maintain statistics. */
185 incr(dyn_used);
186 return (halfword) p;
187 }
188
189 /*tex
190
191 The procedure |flush_list(p)| frees an entire linked list of one-word nodes
192 that starts at position |p|.
193
194 This makes list of single-word nodes available:
195
196 */
197
198 void flush_list(halfword p)
199 {
200 halfword q, r;
201 if (p != null) {
202 r = p;
203 do {
204 q = r;
205 r = token_link(r);
206 decr(dyn_used);
207 } while (r != null);
208 /*tex Now |q| is the last node on the list. */
209 token_link(q) = avail;
210 avail = p;
211 }
212 }
213
214 /*tex
215
216 A \TeX\ token is either a character or a control sequence, and it is
217 represented internally in one of two ways: (1)~A character whose ASCII code
218 number is |c| and whose command code is |m| is represented as the number
219 $2^{21}m+c$; the command code is in the range |1<=m<=14|. (2)~A control
220 sequence whose |eqtb| address is |p| is represented as the number
221 |cs_token_flag+p|. Here |cs_token_flag=t=| $2^{25}-1$ is larger than
222 $2^{21}m+c$, yet it is small enough that |cs_token_flag+p< max_halfword|;
223 thus, a token fits comfortably in a halfword.
224
225 A token |t| represents a |left_brace| command if and only if
226 |t<left_brace_limit|; it represents a |right_brace| command if and only if we
227 have |left_brace_limit<=t<right_brace_limit|; and it represents a |match| or
228 |end_match| command if and only if |match_token<=t<=end_match_token|. The
229 following definitions take care of these token-oriented constants and a few
230 others.
231
232 A token list is a singly linked list of one-word nodes in |mem|, where each
233 word contains a token and a link. Macro definitions, output-routine
234 definitions, marks, \.{\\write} texts, and a few other things are remembered
235 by \TeX\ in the form of token lists, usually preceded by a node with a
236 reference count in its |token_ref_count| field. The token stored in location
237 |p| is called |info(p)|.
238
239 Three special commands appear in the token lists of macro definitions. When
240 |m=match|, it means that \TeX\ should scan a parameter for the current macro;
241 when |m=end_match|, it means that parameter matching should end and \TeX\
242 should start reading the macro text; and when |m=out_param|, it means that
243 \TeX\ should insert parameter number |c| into the text at this point.
244
245 The enclosing \.{\char'173} and \.{\char'175} characters of a macro
246 definition are omitted, but the final right brace of an output routine is
247 included at the end of its token list.
248
249 Here is an example macro definition that illustrates these conventions. After
250 \TeX\ processes the text
251
252 \starttyping
253 \def\mac a#1#2 \b {#1\-a ##1#2 \#2\}
254 \stoptyping
255
256 the definition of \.{\\mac} is represented as a token list containing
257
258 \starttyping
259 (reference count) |letter|a |match|# |match|# |spacer| \b |end_match|
260 |out_param|1 \- |letter|a |spacer|, |mac_param|# |other_char|1
261 |out_param|2 |spacer| |out_param|2
262 \stoptyping
263
264 The procedure |scan_toks| builds such token lists, and |macro_call| does the
265 parameter matching.
266
267 Examples such as \type{\def\m{\def\m{a} b}} explain why reference
268 counts would be needed even if \TeX\ had no \.{\\let} operation: When the
269 token list for \.{\\m} is being read, the redefinition of \.{\\m} changes the
270 |eqtb| entry before the token list has been fully consumed, so we dare not
271 simply destroy a token list when its control sequence is being redefined.
272
273 If the parameter-matching part of a definition ends with `\.{\#\{}', the
274 corresponding token list will have `\.\{' just before the `|end_match|' and
275 also at the very end. The first `\.\{' is used to delimit the parameter; the
276 second one keeps the first from disappearing.
277
278 The |print_meaning| subroutine displays |cur_cmd| and |cur_chr| in symbolic
279 form, including the expansion of a macro or mark.
280
281 */
282
283 void print_meaning(void)
284 {
285 /*tex
286
287 This would make sense but some macro packages don't like it:
288
289 \starttyping
290 if (cur_cmd == math_given_cmd) {
291 cur_cmd = xmath_given_cmd ;
292 }
293 \stoptyping
294
295 */
296 print_cmd_chr((quarterword) cur_cmd, cur_chr);
297 if (cur_cmd >= call_cmd) {
298 print_char(':');
299 print_ln();
300 token_show(cur_chr);
301 } else {
302 /*tex Show the meaning of a mark node. */
303 if ((cur_cmd == top_bot_mark_cmd) && (cur_chr < marks_code)) {
304 print_char(':');
305 print_ln();
306 switch (cur_chr) {
307 case first_mark_code:
308 token_show(first_mark(0));
309 break;
310 case bot_mark_code:
311 token_show(bot_mark(0));
312 break;
313 case split_first_mark_code:
314 token_show(split_first_mark(0));
315 break;
316 case split_bot_mark_code:
317 token_show(split_bot_mark(0));
318 break;
319 default:
320 token_show(top_mark(0));
321 break;
322 }
323 }
324 }
325 }
326
327 /*tex
328
329 The procedure |show_token_list|, which prints a symbolic form of the token
330 list that starts at a given node |p|, illustrates these conventions. The
331 token list being displayed should not begin with a reference count. However,
332 the procedure is intended to be robust, so that if the memory links are awry
333 or if |p| is not really a pointer to a token list, nothing catastrophic will
334 happen.
335
336 An additional parameter |q| is also given; this parameter is either null or
337 it points to a node in the token list where a certain magic computation takes
338 place that will be explained later. (Basically, |q| is non-null when we are
339 printing the two-line context information at the time of an error message;
340 |q| marks the place corresponding to where the second line should begin.)
341
342 For example, if |p| points to the node containing the first \.a in the token
343 list above, then |show_token_list| will print the string $$\hbox{`\.{a\#1\#2\
344 \\b\ ->\#1\\-a\ \#\#1\#2\ \#2}';}$$ and if |q| points to the node containing
345 the second \.a, the magic computation will be performed just before the
346 second \.a is printed.
347
348 The generation will stop, and `\.{\\ETC.}' will be printed, if the length of
349 printing exceeds a given limit~|l|. Anomalous entries are printed in the form
350 of control sequences that are not followed by a blank space, e.g.,
351 `\.{\\BAD.}'; this cannot be confused with actual control sequences because a
352 real control sequence named \.{BAD} would come out `\.{\\BAD\ }'.
353
354 */
355
356 #define not_so_bad(p) \
357 switch (m) { \
358 case assign_int_cmd: \
359 if (c >= (backend_int_base) && c <= (backend_int_last)) \
360 p("[internal backend integer]"); \
361 break; \
362 case assign_dimen_cmd: \
363 if (c >= (backend_dimen_base) && c <= (backend_dimen_last)) \
364 p("[internal backend dimension]"); \
365 break; \
366 case assign_toks_cmd: \
367 if (c >= (backend_toks_base) && c <= (backend_toks_last)) \
368 p("[internal backend tokenlist]"); \
369 break; \
370 case node_cmd: \
371 p("[internal node pointer]"); \
372 break; \
373 case lua_call_cmd: \
374 p("[internal lua function call]"); \
375 break; \
376 case lua_expandable_call_cmd: \
377 p("[internal expandable lua function call]"); \
378 break; \
379 case lua_local_call_cmd: \
380 p("[internal local lua function call]"); \
381 break; \
382 default: \
383 p("BAD"); \
384 break; \
385 }
386
387 void show_token_list(int p, int q, int l)
388 {
389 /*tex pieces of a token */
390 int m, c;
391 /*tex character used in a `|match|' */
392 ASCII_code match_chr = '#';
393 /*tex the highest parameter number, as an ASCII digit */
394 ASCII_code n = '0';
395 tally = 0;
396 if (l < 0)
397 l = 0x3FFFFFFF;
398 while ((p != null) && (tally < l)) {
399 if (p == q) {
400 /*tex Do magic computation. */
401 set_trick_count();
402 }
403 /*tex Display token |p|, and |return| if there are problems. */
404 if ((p < (int) fix_mem_min) || (p > (int) fix_mem_end)) {
405 tprint_esc("CLOBBERED.");
406 return;
407 }
408 if (token_info(p) >= cs_token_flag) {
409 if (!((inhibit_par_tokens) && (token_info(p) == par_token)))
410 print_cs(token_info(p) - cs_token_flag);
411 } else {
412 m = token_cmd(token_info(p));
413 c = token_chr(token_info(p));
414 if (token_info(p) < 0) {
415 tprint_esc("BAD");
416 } else {
417 /*
418 Display the token \type {(|m|,|c|)}. The procedure usually
419 ``learns'' the character code used for macro parameters by
420 seeing one in a |match| command before it runs into any
421 |out_param| commands.
422
423 */
424 switch (m) {
425 case left_brace_cmd:
426 case right_brace_cmd:
427 case math_shift_cmd:
428 case tab_mark_cmd:
429 case sup_mark_cmd:
430 case sub_mark_cmd:
431 case spacer_cmd:
432 case letter_cmd:
433 case other_char_cmd:
434 print(c);
435 break;
436 case mac_param_cmd:
437 if (!in_lua_escape && (is_in_csname==0))
438 print(c);
439 print(c);
440 break;
441 case out_param_cmd:
442 print(match_chr);
443 if (c <= 9) {
444 print_char(c + '0');
445 } else {
446 print_char('!');
447 return;
448 }
449 break;
450 case match_cmd:
451 match_chr = c;
452 print(c);
453 incr(n);
454 print_char(n);
455 if (n > '9')
456 return;
457 break;
458 case end_match_cmd:
459 if (c == 0)
460 tprint("->");
461 break;
462 default:
463 not_so_bad(tprint);
464 break;
465 }
466 }
467 }
468 p = token_link(p);
469 }
470 if (p != null)
471 tprint_esc("ETC.");
472 }
473
474 #define do_buffer_to_unichar(a,b) do { \
475 a = (halfword)str2uni(buffer+b); \
476 b += utf8_size(a); \
477 } while (0)
478
479 /*tex
480
481 Here's the way we sometimes want to display a token list, given a pointer to
482 its reference count; the pointer may be null.
483
484 */
485
486 void token_show(halfword p)
487 {
488 if (p != null)
489 show_token_list(token_link(p), null, 10000000);
490 }
491
492 /*tex
493
494 |delete_token_ref|, is called when a pointer to a token list's reference
495 count is being removed. This means that the token list should disappear if
496 the reference count was |null|, otherwise the count should be decreased by
497 one.
498
499 |p| points to the reference count of a token list that is losing one
500 reference.
501
502 */
503
504 void delete_token_ref(halfword p)
505 {
506 if (token_ref_count(p) == 0)
507 flush_list(p);
508 else
509 decr(token_ref_count(p));
510 }
511
512 int get_char_cat_code(int curchr)
513 {
514 int a;
515 do_get_cat_code(a,curchr);
516 return a;
517 }
518
519 static void invalid_character_error(void)
520 {
521 const char *hlp[] = {
522 "A funny symbol that I can't read has just been input.",
523 "Continue, and I'll forget that it ever happened.",
524 NULL
525 };
526 deletions_allowed = false;
527 tex_error("Text line contains an invalid character", hlp);
528 deletions_allowed = true;
529 }
530
531 static boolean process_sup_mark(void);
532
533 static int scan_control_sequence(void);
534
535 typedef enum {
536 next_line_ok,
537 next_line_return,
538 next_line_restart
539 } next_line_retval;
540
541 static next_line_retval next_line(void);
542
543 /*tex
544
545 In case you are getting bored, here is a slightly less trivial routine: Given
546 a string of lowercase letters, like `\.{pt}' or `\.{plus}' or `\.{width}',
547 the |scan_keyword| routine checks to see whether the next tokens of input
548 match this string. The match must be exact, except that uppercase letters
549 will match their lowercase counterparts; uppercase equivalents are determined
550 by subtracting |"a"-"A"|, rather than using the |uc_code| table, since \TeX\
551 uses this routine only for its own limited set of keywords.
552
553 If a match is found, the characters are effectively removed from the input
554 and |true| is returned. Otherwise |false| is returned, and the input is left
555 essentially unchanged (except for the fact that some macros may have been
556 expanded, etc.).
557
558 */
559
560 boolean scan_keyword(const char *s)
561 {
562 /*tex tail of the backup list */
563 halfword p;
564 /*tex new node being added to the token list via |store_new_token| */
565 halfword q;
566 /*tex index into |str_pool| */
567 const char *k;
568 halfword save_cur_cs = cur_cs;
569 if (strlen(s) == 0) {
570 /*tex but not with newtokenlib zero keyword simply doesn't match */
571 return false ;
572 }
573 p = backup_head;
574 token_link(p) = null;
575 k = s;
576 while (*k) {
577 /*tex Recursion is possible here! */
578 get_x_token();
579 if ((cur_cs == 0) && ((cur_chr == *k) || (cur_chr == *k - 'a' + 'A'))) {
580 store_new_token(cur_tok);
581 k++;
582 } else if ((cur_cmd != spacer_cmd) || (p != backup_head)) {
583 back_input();
584 if (p != backup_head) {
585 begin_token_list(token_link(backup_head), backed_up);
586 }
587 cur_cs = save_cur_cs;
588 return false;
589 }
590 }
591 if (token_link(backup_head) != null)
592 flush_list(token_link(backup_head));
593 cur_cs = save_cur_cs;
594 return true;
595 }
596
597 boolean scan_keyword_case_sensitive(const char *s)
598 {
599 halfword p;
600 halfword q;
601 const char *k;
602 halfword save_cur_cs = cur_cs;
603 if (strlen(s) == 0)
604 return false ;
605 p = backup_head;
606 token_link(p) = null;
607 k = s;
608 while (*k) {
609 get_x_token();
610 if ((cur_cs == 0) && (cur_chr == *k)) {
611 store_new_token(cur_tok);
612 k++;
613 } else if ((cur_cmd != spacer_cmd) || (p != backup_head)) {
614 back_input();
615 if (p != backup_head) {
616 begin_token_list(token_link(backup_head), backed_up);
617 }
618 cur_cs = save_cur_cs;
619 return false;
620 }
621 }
622 if (token_link(backup_head) != null)
623 flush_list(token_link(backup_head));
624 cur_cs = save_cur_cs;
625 return true;
626 }
627
628 /*tex
629
630 We can not return |undefined_control_sequence| under some conditions (inside
631 |shift_case|, for example). This needs thinking.
632
633 */
634
635 /*static char * FFFF = "\xEF\xBF\xBF";*/ /* 0xFFFF */
636
637 halfword active_to_cs(int curchr, int force)
638 {
639 halfword curcs;
640 int nncs = no_new_control_sequence;
641 if (force) {
642 no_new_control_sequence = false;
643 }
644 if (curchr > 0) {
645 char *b = (char *) uni2str((unsigned) curchr);
646 char *utfbytes = xmalloc(8);
647 utfbytes = strcpy(utfbytes, "\xEF\xBF\xBF");
648 utfbytes = strcat(utfbytes, b);
649 free(b);
650 curcs = string_lookup(utfbytes, utf8_size(curchr)+3);
651 free(utfbytes);
652 } else {
653 /*tex 0xFFFF ... why not 3 ? */
654 curcs = string_lookup("\xEF\xBF\xBF", 4);
655 }
656 no_new_control_sequence = nncs;
657 return curcs;
658 }
659
660 /*
661
662 static unsigned char *uni2csstr(unsigned unic)
663 {
664 unsigned char *buf = xmalloc(8);
665 unsigned char *pt = buf;
666 *pt++ = 239; *pt++ = 191; *pt++ = 191; // 0xFFFF
667 if (unic < 0x80)
668 *pt++ = (unsigned char) unic;
669 else if (unic < 0x800) {
670 *pt++ = (unsigned char) (0xc0 | (unic >> 6));
671 *pt++ = (unsigned char) (0x80 | (unic & 0x3f));
672 } else if (unic >= 0x110000) {
673 *pt++ = (unsigned char) (unic - 0x110000);
674 } else if (unic < 0x10000) {
675 *pt++ = (unsigned char) (0xe0 | (unic >> 12));
676 *pt++ = (unsigned char) (0x80 | ((unic >> 6) & 0x3f));
677 *pt++ = (unsigned char) (0x80 | (unic & 0x3f));
678 } else {
679 int u, z, y, x;
680 unsigned val = unic - 0x10000;
681 u = (int) (((val & 0xf0000) >> 16) + 1);
682 z = (int) ((val & 0x0f000) >> 12);
683 y = (int) ((val & 0x00fc0) >> 6);
684 x = (int) (val & 0x0003f);
685 *pt++ = (unsigned char) (0xf0 | (u >> 2));
686 *pt++ = (unsigned char) (0x80 | ((u & 3) << 4) | z);
687 *pt++ = (unsigned char) (0x80 | y);
688 *pt++ = (unsigned char) (0x80 | x);
689 }
690 *pt = '\0';
691 return buf;
692 }
693
694 halfword active_to_cs(int curchr, int force)
695 {
696 halfword curcs;
697 int nncs = no_new_control_sequence;
698 if (force) {
699 no_new_control_sequence = false;
700 }
701 if (curchr > 0) {
702 char * utfbytes = (char *) uni2csstr((unsigned) curchr);
703 curcs = string_lookup(utfbytes, utf8_size(curchr)+3);
704 free(utfbytes);
705 } else {
706 curcs = string_lookup(FFFF, 4); // 0xFFFF ... why not 3 ?
707 }
708 no_new_control_sequence = nncs;
709 return curcs;
710 }
711
712 */
713
714 /*tex
715
716 Maybe this function should listen to \.{\\escapechar} but we can do without.
717
718 */
719
720 static char *cs_to_string(halfword p)
721 {
722 const char *s;
723 char *sh;
724 int k = 0;
725 static char ret[256] = { 0 };
726 if (p == 0 || p == null_cs) {
727 ret[k++] = '\\';
728 s = "csname";
729 while (*s) {
730 ret[k++] = *s++;
731 }
732 ret[k++] = '\\';
733 s = "endcsname";
734 while (*s) {
735 ret[k++] = *s++;
736 }
737 ret[k] = 0;
738
739 } else {
740 str_number txt = cs_text(p);
741 sh = makecstring(txt);
742 s = sh;
743 if (is_active_cs(txt)) {
744 s = s + 3;
745 while (*s) {
746 ret[k++] = *s++;
747 }
748 ret[k] = 0;
749 } else {
750 ret[k++] = '\\';
751 while (*s) {
752 ret[k++] = *s++;
753 }
754 ret[k] = 0;
755 }
756 free(sh);
757 }
758 return (char *) ret;
759 }
760
761 /*tex This is sort of a hack. */
762
763 static char *cmd_chr_to_string(int cmd, int chr)
764 {
765 char *s;
766 str_number str;
767 int sel = selector;
768 selector = new_string;
769 print_cmd_chr((quarterword) cmd, chr);
770 str = make_string();
771 s = makecstring(str);
772 selector = sel;
773 flush_str(str);
774 return s;
775 }
776
777 /*tex
778
779 The heart of \TeX's input mechanism is the |get_next| procedure, which we
780 shall develop in the next few sections of the program. Perhaps we shouldn't
781 actually call it the ``heart,'' however, because it really acts as \TeX's
782 eyes and mouth, reading the source files and gobbling them up. And it also
783 helps \TeX\ to regurgitate stored token lists that are to be processed again.
784
785 The main duty of |get_next| is to input one token and to set |cur_cmd| and
786 |cur_chr| to that token's command code and modifier. Furthermore, if the
787 input token is a control sequence, the |eqtb| location of that control
788 sequence is stored in |cur_cs|; otherwise |cur_cs| is set to zero.
789
790 Underlying this simple description is a certain amount of complexity because
791 of all the cases that need to be handled. However, the inner loop of
792 |get_next| is reasonably short and fast.
793
794 When |get_next| is asked to get the next token of a \.{\\read} line, it sets
795 |cur_cmd=cur_chr=cur_cs=0| in the case that no more tokens appear on that
796 line. (There might not be any tokens at all, if the |end_line_char| has
797 |ignore| as its catcode.)
798
799 The value of |par_loc| is the |eqtb| address of `\.{\\par}'. This quantity is
800 needed because a blank line of input is supposed to be exactly equivalent to
801 the appearance of \.{\\par}; we must set |cur_cs:=par_loc| when detecting a
802 blank line.
803
804 */
805
806 /*tex
807
808 The location of `\.{\\par}' in |eqtb| adn the token representing `\.{\\par}.
809
810 */
811
812 halfword par_loc;
813 halfword par_token;
814
815 /*tex
816
817 Parts |get_next| are executed more often than any other instructions of \TeX.
818
819 The global variable |force_eof| is normally |false|; it is set |true| by an
820 \.{\\endinput} command. |luacstrings| is the number of lua print statements
821 waiting to be input, it is changed by |luatokencall|.
822
823
824 */
825
826 /*tex Should the next \.{\\input} be aborted early? */
827 boolean force_eof;
828
829 /*tex How many lua strings are waiting to be input? */
830
831 int luacstrings;
832
833 /*tex
834
835 If the user has set the |pausing| parameter to some positive value, and if
836 nonstop mode has not been selected, each line of input is displayed on the
837 terminal and the transcript file, followed by `\.{=>}'. \TeX\ waits for a
838 response. If the response is simply |carriage_return|, the line is accepted
839 as it stands, otherwise the line typed is used instead of the line in the
840 file.
841
842 */
843
844 void firm_up_the_line(void)
845 {
846 int k;
847 ilimit = last;
848 if (pausing_par > 0) {
849 if (interaction > nonstop_mode) {
850 wake_up_terminal();
851 print_ln();
852 if (istart < ilimit) {
853 for (k = istart; k <= ilimit - 1; k++)
854 print_char(buffer[k]);
855 }
856 first = ilimit;
857 prompt_input("=>");
858 if (last > first) {
859 /*tex Move line down in buffer. */
860 for (k = first; k < +last - 1; k++)
861 buffer[k + istart - first] = buffer[k];
862 ilimit = istart + last - first;
863 }
864 }
865 }
866 }
867
868 /*tex
869
870 Before getting into |get_next|, let's consider the subroutine that is called
871 when an `\.{\\outer}' control sequence has been scanned or when the end of a
872 file has been reached. These two cases are distinguished by |cur_cs|, which
873 is zero at the end of a file.
874
875 */
876
877 void check_outer_validity(void)
878 {
879 /*tex points to inserted token list */
880 halfword p;
881 /*tex auxiliary pointer */
882 halfword q;
883 if (suppress_outer_error_par)
884 return;
885 if (scanner_status != normal) {
886 deletions_allowed = false;
887 /*tex
888
889 Back up an outer control sequence so that it can be reread. An
890 outer control sequence that occurs in a \.{\\read} will not be
891 reread, since the error recovery for \.{\\read} is not very powerful.
892
893 */
894 if (cur_cs != 0) {
895 if ((istate == token_list) || (iname < 1) || (iname > 17)) {
896 p = get_avail();
897 token_info(p) = cs_token_flag + cur_cs;
898 /*tex prepare to read the control sequence again */
899 begin_token_list(p, backed_up);
900 }
901 /*tex replace it by a space */
902 cur_cmd = spacer_cmd;
903 cur_chr = ' ';
904 }
905 if (scanner_status > skipping) {
906 const char *errhlp[] = {
907 "I suspect you have forgotten a `}', causing me",
908 "to read past where you wanted me to stop.",
909 "I'll try to recover; but if the error is serious,",
910 "you'd better type `E' or `X' now and fix your file.",
911 NULL
912 };
913 char errmsg[318];
914 const char *startmsg;
915 const char *scannermsg;
916 /*tex
917
918 Tell the user what has run away and try to recover Print a
919 definition, argument, or preamble.
920
921 */
922 runaway();
923 if (cur_cs == 0) {
924 startmsg = "File ended";
925 } else {
926 cur_cs = 0;
927 startmsg = "Forbidden control sequence found";
928 }
929 /*tex
930
931 Print either `\.{definition}' or `\.{use}' or `\.{preamble}' or
932 `\.{text}', and insert tokens that should lead to recovery. The
933 recovery procedure can't be fully understood without knowing more
934 about the \TeX\ routines that should be aborted, but we can
935 sketch the ideas here: For a runaway definition we will insert a
936 right brace; for a runaway preamble, we will insert a special
937 \.{\\cr} token and a right brace; and for a runaway argument, we
938 will set |long_state| to |outer_call| and insert \.{\\par}.
939
940 */
941 p = get_avail();
942 switch (scanner_status) {
943 case defining:
944 scannermsg = "definition";
945 token_info(p) = right_brace_token + '}';
946 break;
947 case matching:
948 scannermsg = "use";
949 token_info(p) = par_token;
950 long_state = outer_call_cmd;
951 break;
952 case aligning:
953 scannermsg = "preamble";
954 token_info(p) = right_brace_token + '}';
955 q = p;
956 p = get_avail();
957 token_link(p) = q;
958 token_info(p) = cs_token_flag + frozen_cr;
959 align_state = -1000000;
960 break;
961 case absorbing:
962 scannermsg = "text";
963 token_info(p) = right_brace_token + '}';
964 break;
965 default:
966 scannermsg = "unknown";
967 break;
968 }
969 begin_token_list(p, inserted);
970 snprintf(errmsg, 318, "%s while scanning %s of %s", startmsg, scannermsg, cs_to_string(warning_index));
971 tex_error(errmsg, errhlp);
972 } else {
973 char errmsg[256];
974 const char *errhlp_no[] = {
975 "The file ended while I was skipping conditional text.",
976 "This kind of error happens when you say `\\if...' and forget",
977 "the matching `\\fi'. I've inserted a `\\fi'; this might work.",
978 NULL
979 };
980 const char *errhlp_cs[] = {
981 "A forbidden control sequence occurred in skipped text.",
982 "This kind of error happens when you say `\\if...' and forget",
983 "the matching `\\fi'. I've inserted a `\\fi'; this might work.",
984 NULL
985 };
986 const char **errhlp = (const char **) errhlp_no;
987 char *ss;
988 if (cur_cs != 0) {
989 errhlp = errhlp_cs;
990 cur_cs = 0;
991 }
992 ss = cmd_chr_to_string(if_test_cmd, cur_if);
993 snprintf(errmsg, 255, "Incomplete %s; all text was ignored after line %d",
994 ss, (int) skip_line);
995 free(ss);
996 /*tex Incomplete |\if...| */
997 cur_tok = cs_token_flag + frozen_fi;
998 /*tex back up one inserted token and call |error|. */
999 {
1000 OK_to_interrupt = false;
1001 back_input();
1002 token_type = inserted;
1003 OK_to_interrupt = true;
1004 tex_error(errmsg, errhlp);
1005 }
1006 }
1007 deletions_allowed = true;
1008 }
1009 }
1010
1011 #if 0
1012
1013 /*tex
1014
1015 The other variant gives less clutter in tracing cache usage when profiling
1016 and for some files (like the manual) also a bit of a speedup.
1017
1018 */
1019
1020 static boolean get_next_file(void)
1021 {
1022 SWITCH:
1023 if (iloc <= ilimit) {
1024 /*tex current line not yet finished */
1025 do_buffer_to_unichar(cur_chr, iloc);
1026 RESWITCH:
1027 if (detokenized_line()) {
1028 cur_cmd = (cur_chr == ' ' ? 10 : 12);
1029 } else {
1030 do_get_cat_code(cur_cmd, cur_chr);
1031 }
1032 /*tex
1033
1034 Change state if necessary, and |goto switch| if the current
1035 character should be ignored, or |goto reswitch| if the current
1036 character changes to another;
1037
1038 The following 48-way switch accomplishes the scanning quickly,
1039 assuming that a decent C compiler has translated the code. Note that
1040 the numeric values for |mid_line|, |skip_blanks|, and |new_line| are
1041 spaced apart from each other by |max_char_code+1|, so we can add a
1042 character's command code to the state to get a single number that
1043 characterizes both.
1044
1045 Remark [ls/hh]: checking performance indicated that this switch was
1046 the cause of many branch prediction errors but changing it to:
1047
1048 \starttyping
1049 c = istate + cur_cmd;
1050 if (c == (mid_line + letter_cmd) || c == (mid_line + other_char_cmd)) {
1051 return true;
1052 } else if (c >= new_line) {
1053 switch (c) {
1054 }
1055 } else if (c >= skip_blanks) {
1056 switch (c) {
1057 }
1058 } else if (c >= mid_line) {
1059 switch (c) {
1060 }
1061 } else {
1062 istate = mid_line;
1063 return true;
1064 }
1065 \stoptyping
1066
1067 gives as many prediction errors. So, we can indeed assume that the
1068 compiler does the right job, or that there is simply no other way.
1069
1070 */
1071
1072 switch (istate + cur_cmd) {
1073 case mid_line + ignore_cmd:
1074 case skip_blanks + ignore_cmd:
1075 case new_line + ignore_cmd:
1076 case skip_blanks + spacer_cmd:
1077 case new_line + spacer_cmd:
1078 /*tex Cases where character is ignored. */
1079 goto SWITCH;
1080 break;
1081 case mid_line + escape_cmd:
1082 case new_line + escape_cmd:
1083 case skip_blanks + escape_cmd:
1084 /*tex Scan a control sequence. */
1085 istate = (unsigned char) scan_control_sequence();
1086 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1087 check_outer_validity();
1088 break;
1089 case mid_line + active_char_cmd:
1090 case new_line + active_char_cmd:
1091 case skip_blanks + active_char_cmd:
1092 /*tex Process an active-character. */
1093 cur_cs = active_to_cs(cur_chr, false);
1094 cur_cmd = eq_type(cur_cs);
1095 cur_chr = equiv(cur_cs);
1096 istate = mid_line;
1097 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1098 check_outer_validity();
1099 break;
1100 case mid_line + sup_mark_cmd:
1101 case new_line + sup_mark_cmd:
1102 case skip_blanks + sup_mark_cmd:
1103 /*tex If this |sup_mark| starts. */
1104 if (process_sup_mark())
1105 goto RESWITCH;
1106 else
1107 istate = mid_line;
1108 break;
1109 case mid_line + invalid_char_cmd:
1110 case new_line + invalid_char_cmd:
1111 case skip_blanks + invalid_char_cmd:
1112 /*tex Decry the invalid character and |goto restart|. */
1113 invalid_character_error();
1114 /*tex Because state may be |token_list| now: */
1115 return false;
1116 break;
1117 case mid_line + spacer_cmd:
1118 /*tex Enter |skip_blanks| state, emit a space. */
1119 istate = skip_blanks;
1120 cur_chr = ' ';
1121 break;
1122 case mid_line + car_ret_cmd:
1123 /*tex
1124
1125 Finish line, emit a space. When a character of type |spacer|
1126 gets through, its character code is changed to $\.{"\
1127 "}=040$. This means that the ASCII codes for tab and space,
1128 and for the space inserted at the end of a line, will be
1129 treated alike when macro parameters are being matched. We do
1130 this since such characters are indistinguishable on most
1131 computer terminal displays.
1132
1133 */
1134 iloc = ilimit + 1;
1135 cur_cmd = spacer_cmd;
1136 cur_chr = ' ';
1137 break;
1138 case skip_blanks + car_ret_cmd:
1139 case mid_line + comment_cmd:
1140 case new_line + comment_cmd:
1141 case skip_blanks + comment_cmd:
1142 /*tex Finish line, |goto switch|; */
1143 iloc = ilimit + 1;
1144 goto SWITCH;
1145 break;
1146 case new_line + car_ret_cmd:
1147 /*tex Finish line, emit a \.{\\par}; */
1148 iloc = ilimit + 1;
1149 cur_cs = par_loc;
1150 cur_cmd = eq_type(cur_cs);
1151 cur_chr = equiv(cur_cs);
1152 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1153 check_outer_validity();
1154 break;
1155 case skip_blanks + left_brace_cmd:
1156 case new_line + left_brace_cmd:
1157 istate = mid_line;
1158 /*tex Fall through. */
1159 case mid_line + left_brace_cmd:
1160 align_state++;
1161 break;
1162 case skip_blanks + right_brace_cmd:
1163 case new_line + right_brace_cmd:
1164 istate = mid_line;
1165 /*tex Fall through. */
1166 case mid_line + right_brace_cmd:
1167 align_state--;
1168 break;
1169 case mid_line + math_shift_cmd:
1170 case mid_line + tab_mark_cmd:
1171 case mid_line + mac_param_cmd:
1172 case mid_line + sub_mark_cmd:
1173 case mid_line + letter_cmd:
1174 case mid_line + other_char_cmd:
1175 break;
1176 /*
1177 case skip_blanks + math_shift:
1178 case skip_blanks + tab_mark:
1179 case skip_blanks + mac_param:
1180 case skip_blanks + sub_mark:
1181 case skip_blanks + letter:
1182 case skip_blanks + other_char:
1183 case new_line + math_shift:
1184 case new_line + tab_mark:
1185 case new_line + mac_param:
1186 case new_line + sub_mark:
1187 case new_line + letter:
1188 case new_line + other_char:
1189 */
1190 default:
1191 istate = mid_line;
1192 break;
1193 }
1194 } else {
1195 if (iname != 21)
1196 istate = new_line;
1197 /*tex
1198
1199 Move to next line of file, or |goto restart| if there is no next line,
1200 or |return| if a \.{\\read} line has finished;
1201
1202 */
1203 do {
1204 next_line_retval r = next_line();
1205 if (r == next_line_return) {
1206 return true;
1207 } else if (r == next_line_restart) {
1208 return false;
1209 }
1210 } while (0);
1211 check_interrupt();
1212 goto SWITCH;
1213 }
1214 return true;
1215 }
1216
1217 #else
1218
1219 /*tex
1220
1221 This variant gives 10 times less Bim in vallgrind!
1222
1223 */
1224
1225 static boolean get_next_file(void)
1226 {
1227 int c = 0;
1228 SWITCH:
1229 if (iloc <= ilimit) {
1230 /*tex current line not yet finished */
1231 do_buffer_to_unichar(cur_chr, iloc);
1232 RESWITCH:
1233 if (detokenized_line()) {
1234 cur_cmd = (cur_chr == ' ' ? 10 : 12);
1235 } else {
1236 do_get_cat_code(cur_cmd, cur_chr);
1237 }
1238 /*tex
1239
1240 Change state if necessary, and |goto switch| if the current character
1241 should be ignored, or |goto reswitch| if the current character changes
1242 to another.
1243
1244 */
1245 c = istate + cur_cmd;
1246 if (c == (mid_line + letter_cmd) || c == (mid_line + other_char_cmd)) {
1247 return true;
1248 } else if (c >= new_line) {
1249 switch (c-new_line) {
1250 case escape_cmd:
1251 istate = (unsigned char) scan_control_sequence();
1252 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1253 check_outer_validity();
1254 return true;
1255 case left_brace_cmd:
1256 istate = mid_line;
1257 align_state++;
1258 return true;
1259 case right_brace_cmd:
1260 istate = mid_line;
1261 align_state--;
1262 return true;
1263 case math_shift_cmd:
1264 istate = mid_line;
1265 return true;
1266 case tab_mark_cmd:
1267 istate = mid_line;
1268 return true;
1269 case car_ret_cmd:
1270 /*tex Finish line, emit a \.{\\par}. */
1271 iloc = ilimit + 1;
1272 cur_cs = par_loc;
1273 cur_cmd = eq_type(cur_cs);
1274 cur_chr = equiv(cur_cs);
1275 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1276 check_outer_validity();
1277 return true;
1278 case mac_param_cmd:
1279 istate = mid_line;
1280 return true;
1281 case sup_mark_cmd:
1282 if (process_sup_mark())
1283 goto RESWITCH;
1284 else
1285 istate = mid_line;
1286 return true;
1287 case sub_mark_cmd:
1288 istate = mid_line;
1289 return true;
1290 case ignore_cmd:
1291 goto SWITCH;
1292 return true;
1293 case spacer_cmd:
1294 /*tex Cases where character is ignored. */
1295 goto SWITCH;
1296 case letter_cmd:
1297 istate = mid_line;
1298 return true;
1299 case other_char_cmd:
1300 istate = mid_line;
1301 return true;
1302 case active_char_cmd:
1303 cur_cs = active_to_cs(cur_chr, false);
1304 cur_cmd = eq_type(cur_cs);
1305 cur_chr = equiv(cur_cs);
1306 istate = mid_line;
1307 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1308 check_outer_validity();
1309 return true;
1310 case comment_cmd:
1311 iloc = ilimit + 1;
1312 goto SWITCH;
1313 case invalid_char_cmd:
1314 /*tex Because state may be |token_list| now. */
1315 invalid_character_error();
1316 return false;
1317 default:
1318 istate = mid_line;
1319 return true;
1320 }
1321 } else if (c >= skip_blanks) {
1322 switch (c-skip_blanks) {
1323 case escape_cmd:
1324 /*tex Scan a control sequence. */
1325 istate = (unsigned char) scan_control_sequence();
1326 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1327 check_outer_validity();
1328 return true;
1329 case left_brace_cmd:
1330 istate = mid_line;
1331 align_state++;
1332 return true;
1333 case right_brace_cmd:
1334 istate = mid_line;
1335 align_state--;
1336 return true;
1337 case math_shift_cmd:
1338 istate = mid_line;
1339 return true;
1340 case tab_mark_cmd:
1341 istate = mid_line;
1342 return true;
1343 case car_ret_cmd:
1344 iloc = ilimit + 1;
1345 goto SWITCH;
1346 case mac_param_cmd:
1347 istate = mid_line;
1348 return true;
1349 case sup_mark_cmd:
1350 if (process_sup_mark())
1351 goto RESWITCH;
1352 else
1353 istate = mid_line;
1354 return true;
1355 case sub_mark_cmd:
1356 istate = mid_line;
1357 return true;
1358 case ignore_cmd:
1359 goto SWITCH;
1360 case spacer_cmd:
1361 goto SWITCH;
1362 case letter_cmd:
1363 istate = mid_line;
1364 return true;
1365 case other_char_cmd:
1366 istate = mid_line;
1367 return true;
1368 case active_char_cmd:
1369 cur_cs = active_to_cs(cur_chr, false);
1370 cur_cmd = eq_type(cur_cs);
1371 cur_chr = equiv(cur_cs);
1372 istate = mid_line;
1373 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1374 check_outer_validity();
1375 return true;
1376 case comment_cmd:
1377 /*tex Finish line, |goto switch|. */
1378 iloc = ilimit + 1;
1379 goto SWITCH;
1380 case invalid_char_cmd:
1381 /*tex Decry the invalid character and |goto restart|. */
1382 invalid_character_error();
1383 /*tex Because state may be |token_list| now. */
1384 return false;
1385 default:
1386 istate = mid_line;
1387 return true;
1388 }
1389 } else if (c >= mid_line) {
1390 switch (c-mid_line) {
1391 case escape_cmd:
1392 istate = (unsigned char) scan_control_sequence();
1393 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1394 check_outer_validity();
1395 return true;
1396 case left_brace_cmd:
1397 align_state++;
1398 return true;
1399 case right_brace_cmd:
1400 align_state--;
1401 return true;
1402 case math_shift_cmd:
1403 return true;
1404 case tab_mark_cmd:
1405 return true;
1406 case car_ret_cmd:
1407 /*tex
1408
1409 Finish line, emit a space. When a character of type
1410 |spacer| gets through, its character code is changed to
1411 $\.{"\ "}=040$. This means that the ASCII codes for tab
1412 and space, and for the space inserted at the end of a
1413 line, will be treated alike when macro parameters are
1414 being matched. We do this since such characters are
1415 indistinguishable on most computer terminal displays.
1416
1417 */
1418 iloc = ilimit + 1;
1419 cur_cmd = spacer_cmd;
1420 cur_chr = ' ';
1421 return true;
1422 case mac_param_cmd:
1423 return true;
1424 case sup_mark_cmd:
1425 if (process_sup_mark())
1426 goto RESWITCH;
1427 else
1428 istate = mid_line;
1429 return true;
1430 case sub_mark_cmd:
1431 return true;
1432 case ignore_cmd:
1433 goto SWITCH;
1434 case spacer_cmd:
1435 /*tex Enter |skip_blanks| state, emit a space. */
1436 istate = skip_blanks;
1437 cur_chr = ' ';
1438 return true;
1439 case letter_cmd:
1440 istate = mid_line;
1441 return true;
1442 case other_char_cmd:
1443 istate = mid_line;
1444 return true;
1445 case active_char_cmd:
1446 cur_cs = active_to_cs(cur_chr, false);
1447 cur_cmd = eq_type(cur_cs);
1448 cur_chr = equiv(cur_cs);
1449 istate = mid_line;
1450 if (! suppress_outer_error_par && cur_cmd >= outer_call_cmd)
1451 check_outer_validity();
1452 return true;
1453 case comment_cmd:
1454 iloc = ilimit + 1;
1455 goto SWITCH;
1456 case invalid_char_cmd:
1457 /*tex Because state may be |token_list| now. */
1458 invalid_character_error();
1459 return false;
1460 default:
1461 istate = mid_line;
1462 return true;
1463 }
1464 } else {
1465 istate = mid_line;
1466 return true;
1467 }
1468 } else {
1469 if (iname != 21) {
1470 istate = new_line;
1471 }
1472 /*tex
1473
1474 Move to next line of file, or |goto restart| if there is no next
1475 line, or |return| if a \.{\\read} line has finished;
1476
1477 */
1478 do {
1479 next_line_retval r = next_line();
1480 if (r == next_line_return) {
1481 return true;
1482 } else if (r == next_line_restart) {
1483 return false;
1484 }
1485 } while (0);
1486 check_interrupt();
1487 goto SWITCH;
1488 }
1489 return true;
1490 }
1491
1492 #endif
1493
1494 /*tex
1495
1496 Notice that a code like \.{\^\^8} becomes \.x if not followed by a hex digit.
1497 We only support a limited set:
1498
1499 \starttyping
1500 ^^^^^^XXXXXX
1501 ^^^^XXXXXX
1502 ^^XX ^^<char>
1503 \stoptyping
1504
1505 */
1506
1507 #define is_hex(a) ((a>='0'&&a<='9')||(a>='a'&&a<='f'))
1508
1509 #define add_nybble(c) \
1510 if (c<='9') { \
1511 cur_chr=(cur_chr<<4)+c-'0'; \
1512 } else { \
1513 cur_chr=(cur_chr<<4)+c-'a'+10; \
1514 }
1515
1516 #define set_nybble(c) \
1517 if (c<='9') { \
1518 cur_chr=c-'0'; \
1519 } else { \
1520 cur_chr=c-'a'+10; \
1521 }
1522
1523 #define one_hex_to_cur_chr(c1) \
1524 set_nybble(c1);
1525
1526 #define two_hex_to_cur_chr(c1,c2) \
1527 set_nybble(c1); \
1528 add_nybble(c2);
1529
1530 #define four_hex_to_cur_chr(c1,c2,c3,c4) \
1531 two_hex_to_cur_chr(c1,c2); \
1532 add_nybble(c3); \
1533 add_nybble(c4);
1534
1535 #define six_hex_to_cur_chr(c1,c2,c3,c4,c5,c6) \
1536 four_hex_to_cur_chr(c1,c2,c3,c4); \
1537 add_nybble(c5); \
1538 add_nybble(c6);
1539
1540 static boolean process_sup_mark(void)
1541 {
1542 if (cur_chr == buffer[iloc]) {
1543 if (iloc < ilimit) {
1544 if ((cur_chr == buffer[iloc + 1]) && (cur_chr == buffer[iloc + 2])) {
1545 if ((cur_chr == buffer[iloc + 3]) && (cur_chr == buffer[iloc + 4])) {
1546 /*tex |^^^^^^XXXXXX| */
1547 if ((iloc + 10) <= ilimit) {
1548 int c1 = buffer[iloc + 5];
1549 int c2 = buffer[iloc + 6];
1550 int c3 = buffer[iloc + 7];
1551 int c4 = buffer[iloc + 8];
1552 int c5 = buffer[iloc + 9];
1553 int c6 = buffer[iloc + 10];
1554 if (is_hex(c1) && is_hex(c2) && is_hex(c3) &&
1555 is_hex(c4) && is_hex(c5) && is_hex(c6)) {
1556 iloc = iloc + 11;
1557 six_hex_to_cur_chr(c1,c2,c3,c4,c5,c6);
1558 return true;
1559 } else {
1560 tex_error("^^^^^^ needs six hex digits", NULL);
1561 }
1562 } else {
1563 tex_error("^^^^^^ needs six hex digits, end of input", NULL);
1564 }
1565 } else {
1566 /*tex |^^^^XXXX| */
1567 if ((iloc + 6) <= ilimit) {
1568 int c1 = buffer[iloc + 3];
1569 int c2 = buffer[iloc + 4];
1570 int c3 = buffer[iloc + 5];
1571 int c4 = buffer[iloc + 6];
1572 if (is_hex(c1) && is_hex(c2) && is_hex(c3) && is_hex(c4)) {
1573 iloc = iloc + 7;
1574 four_hex_to_cur_chr(c1,c2,c3,c4);
1575 return true;
1576 } else {
1577 tex_error("^^^^ needs four hex digits", NULL);
1578 }
1579 } else {
1580 tex_error("^^^^ needs four hex digits, end of input", NULL);
1581 }
1582 }
1583 } else {
1584 /*tex |^^XX| */
1585 if ((iloc + 2) <= ilimit) {
1586 int c1 = buffer[iloc + 1];
1587 int c2 = buffer[iloc + 2];
1588 if (is_hex(c1) && is_hex(c2)) {
1589 iloc = iloc + 3;
1590 two_hex_to_cur_chr(c1,c2);
1591 return true;
1592 }
1593 }
1594 /*tex Go on, no error, good old \TEX . */
1595 }
1596 }
1597 /*tex The rest. */
1598 {
1599 int c1 = buffer[iloc + 1];
1600 if (c1 < 0200) {
1601 iloc = iloc + 2;
1602 if (is_hex(c1) && (iloc <= ilimit)) {
1603 int c2 = buffer[iloc];
1604 if (is_hex(c2)) {
1605 incr(iloc);
1606 two_hex_to_cur_chr(c1,c2);
1607 return true;
1608 }
1609 }
1610 cur_chr = (c1 < 0100 ? c1 + 0100 : c1 - 0100);
1611 return true;
1612 }
1613 }
1614 }
1615 return false;
1616 }
1617
1618 /*tex
1619
1620 Control sequence names are scanned only when they appear in some line of a
1621 file; once they have been scanned the first time, their |eqtb| location
1622 serves as a unique identification, so \TeX\ doesn't need to refer to the
1623 original name any more except when it prints the equivalent in symbolic form.
1624
1625 The program that scans a control sequence has been written carefully in order
1626 to avoid the blowups that might otherwise occur if a malicious user tried
1627 something like `\.{\\catcode\'15=0}'. The algorithm might look at
1628 |buffer[ilimit+1]|, but it never looks at |buffer[ilimit+2]|.
1629
1630 If expanded characters like `\.{\^\^A}' or `\.{\^\^df}' appear in or just
1631 following a control sequence name, they are converted to single characters in
1632 the buffer and the process is repeated, slowly but surely.
1633
1634 */
1635
1636 static boolean check_expanded_code(int *kk); /* below */
1637
1638 static int scan_control_sequence(void)
1639 {
1640 int retval = mid_line;
1641 if (iloc > ilimit) {
1642 /*tex |state| is irrelevant in this case. */
1643 cur_cs = null_cs;
1644 } else {
1645 /*tex |cat_code(cur_chr)|, usually: */
1646 register int cat;
1647 while (1) {
1648 int k = iloc;
1649 do_buffer_to_unichar(cur_chr, k);
1650 do_get_cat_code(cat, cur_chr);
1651 if (cat != letter_cmd || k > ilimit) {
1652 retval = (cat == spacer_cmd ? skip_blanks : mid_line);
1653 /*tex If an expanded \unknown */
1654 if (cat == sup_mark_cmd && check_expanded_code(&k))
1655 continue;
1656 } else {
1657 retval = skip_blanks;
1658 do {
1659 do_buffer_to_unichar(cur_chr, k);
1660 do_get_cat_code(cat, cur_chr);
1661 } while (cat == letter_cmd && k <= ilimit);
1662 /*tex If an expanded \unknown */
1663 if (cat == sup_mark_cmd && check_expanded_code(&k))
1664 continue;
1665 if (cat != letter_cmd) {
1666 /*tex Backtrack one character which can be \UTF. */
1667 if (cur_chr <= 0x7F) {
1668 k -= 1; /* in most cases */
1669 } else if (cur_chr > 0xFFFF) {
1670 k -= 4;
1671 } else if (cur_chr > 0x7FF) {
1672 k -= 3;
1673 } else /* if (cur_chr > 0x7F) */ {
1674 k -= 2;
1675 }
1676 /*tex Now |k| points to first nonletter. */
1677 }
1678 }
1679 cur_cs = id_lookup(iloc, k - iloc);
1680 iloc = k;
1681 break;
1682 }
1683 }
1684 cur_cmd = eq_type(cur_cs);
1685 cur_chr = equiv(cur_cs);
1686 return retval;
1687 }
1688
1689 /*tex
1690
1691 Whenever we reach the following piece of code, we will have
1692 |cur_chr=buffer[k-1]| and |k<=ilimit+1| and
1693 |cat=get_cat_code(cat_code_table,cur_chr)|. If an expanded code like
1694 \.{\^\^A} or \.{\^\^df} appears in |buffer[(k-1)..(k+1)]| or
1695 |buffer[(k-1)..(k+2)]|, we will store the corresponding code in |buffer[k-1]|
1696 and shift the rest of the buffer left two or three places.
1697
1698 */
1699
1700 static boolean check_expanded_code(int *kk)
1701 {
1702 int l;
1703 int k = *kk;
1704 int d = 1;
1705 if (buffer[k] == cur_chr && k < ilimit) {
1706 if ((cur_chr == buffer[k + 1]) && (cur_chr == buffer[k + 2])) {
1707 if ((cur_chr == buffer[k + 3]) && (cur_chr == buffer[k + 4])) {
1708 if ((k + 10) <= ilimit) {
1709 int c1 = buffer[k + 6 - 1];
1710 int c2 = buffer[k + 6];
1711 int c3 = buffer[k + 6 + 1];
1712 int c4 = buffer[k + 6 + 2];
1713 int c5 = buffer[k + 6 + 3];
1714 int c6 = buffer[k + 6 + 4];
1715 if (is_hex(c1) && is_hex(c2) && is_hex(c3) && is_hex(c4) && is_hex(c5) && is_hex(c6)) {
1716 d = 6;
1717 six_hex_to_cur_chr(c1,c2,c3,c4,c5,c6);
1718 } else {
1719 tex_error("^^^^^^ needs six hex digits", NULL);
1720 }
1721 } else {
1722 tex_error("^^^^^^ needs six hex digits, end of input", NULL);
1723 }
1724 } else {
1725 if ((k + 6) <= ilimit) {
1726 int c1 = buffer[k + 4 - 1];
1727 int c2 = buffer[k + 4];
1728 int c3 = buffer[k + 4 + 1];
1729 int c4 = buffer[k + 4 + 2];
1730 if (is_hex(c1) && is_hex(c2) && is_hex(c3) && is_hex(c4)) {
1731 d = 4;
1732 four_hex_to_cur_chr(c1,c2,c3,c4);
1733 } else {
1734 tex_error("^^^^ needs four hex digits", NULL);
1735 }
1736 } else {
1737 tex_error("^^^^ needs four hex digits, end of input", NULL);
1738 }
1739 }
1740 } else {
1741 int c1 = buffer[k + 1];
1742 if (c1 < 0200) {
1743 d = 1;
1744 if (is_hex(c1) && (k + 2) <= ilimit) {
1745 int c2 = buffer[k + 2];
1746 if (is_hex(c2)) {
1747 d = 2;
1748 two_hex_to_cur_chr(c1,c2);
1749 } else {
1750 cur_chr = (c1 < 0100 ? c1 + 0100 : c1 - 0100);
1751 }
1752 } else {
1753 cur_chr = (c1 < 0100 ? c1 + 0100 : c1 - 0100);
1754 }
1755 }
1756 }
1757 if (d > 2)
1758 d = 2 * d - 1;
1759 else
1760 d++;
1761 if (cur_chr <= 0x7F) {
1762 buffer[k - 1] = (packed_ASCII_code) cur_chr;
1763 } else if (cur_chr <= 0x7FF) {
1764 buffer[k - 1] = (packed_ASCII_code) (0xC0 + cur_chr / 0x40);
1765 k++;
1766 d--;
1767 buffer[k - 1] = (packed_ASCII_code) (0x80 + cur_chr % 0x40);
1768 } else if (cur_chr <= 0xFFFF) {
1769 buffer[k - 1] = (packed_ASCII_code) (0xE0 + cur_chr / 0x1000);
1770 k++;
1771 d--;
1772 buffer[k - 1] = (packed_ASCII_code) (0x80 + (cur_chr % 0x1000) / 0x40);
1773 k++;
1774 d--;
1775 buffer[k - 1] = (packed_ASCII_code) (0x80 + (cur_chr % 0x1000) % 0x40);
1776 } else {
1777 buffer[k - 1] = (packed_ASCII_code) (0xF0 + cur_chr / 0x40000);
1778 k++;
1779 d--;
1780 buffer[k - 1] = (packed_ASCII_code) (0x80 + (cur_chr % 0x40000) / 0x1000);
1781 k++;
1782 d--;
1783 buffer[k - 1] = (packed_ASCII_code) (0x80 + ((cur_chr % 0x40000) % 0x1000) / 0x40);
1784 k++;
1785 d--;
1786 buffer[k - 1] = (packed_ASCII_code) (0x80 + ((cur_chr % 0x40000) % 0x1000) % 0x40);
1787 }
1788 l = k;
1789 ilimit = ilimit - d;
1790 while (l <= ilimit) {
1791 buffer[l] = buffer[l + d];
1792 l++;
1793 }
1794 *kk = k;
1795 return true;
1796 }
1797 return false;
1798 }
1799
1800 /*tex
1801
1802 All of the easy branches of |get_next| have now been taken care of. There is
1803 one more branch.
1804
1805 */
1806
1807 static next_line_retval next_line(void)
1808 {
1809 /*tex A way to end a pseudo file without trailing space: */
1810 boolean inhibit_eol = false;
1811 if (iname > 17) {
1812 /*tex
1813
1814 Read next line of file into |buffer|, or |goto restart| if the file
1815 has ended.
1816
1817 */
1818 incr(line);
1819 first = istart;
1820 if (!force_eof) {
1821 if (iname <= 20) {
1822 if (pseudo_input()) {
1823 /*tex Not end of file; set |ilimit|. */
1824 firm_up_the_line();
1825 line_catcode_table = DEFAULT_CAT_TABLE;
1826 if ((iname == 19) && (pseudo_lines(pseudo_files) == null))
1827 inhibit_eol = true;
1828 } else if ((every_eof_par != null) && !eof_seen[iindex]) {
1829 ilimit = first - 1;
1830 /*tex Fake one empty line. */
1831 eof_seen[iindex] = true;
1832 if (iname != 19)
1833 begin_token_list(every_eof_par, every_eof_text);
1834 return next_line_restart;
1835 } else {
1836 force_eof = true;
1837 }
1838 } else {
1839 if (iname == 21) {
1840 halfword n = null;
1841 int t = luacstring_input(&n);
1842 switch (t) {
1843 case 0:
1844 force_eof = true;
1845 break;
1846 case 1:
1847 /*tex string */
1848 firm_up_the_line();
1849 line_catcode_table = (short) luacstring_cattable();
1850 line_partial = (signed char) luacstring_partial();
1851 if (luacstring_final_line() || line_partial || line_catcode_table == NO_CAT_TABLE)
1852 inhibit_eol = true;
1853 if (!line_partial)
1854 istate = new_line;
1855 break;
1856 case 2:
1857 /*tex token */
1858 cur_tok = n;
1859 back_input();
1860 /*tex Needs checking. */
1861 return next_line_restart;
1862 break;
1863 case 3:
1864 /*tex node */
1865 if (n < biggest_char) {
1866 /*tex |0x10FFFF == 1114111| */
1867 cur_tok = token_val(node_cmd, n);
1868 back_input();
1869 /*tex Needs checking. */
1870 return next_line_restart;
1871 } else {
1872 normal_warning("nodes","unable to store reference from lua in tex");
1873 force_eof = true;
1874 }
1875 break;
1876 default:
1877 force_eof = true;
1878 break;
1879 }
1880 } else if (lua_input_ln(cur_file, 0, true)) {
1881 /*tex Not end of file, set |ilimit|. */
1882 firm_up_the_line();
1883 line_catcode_table = DEFAULT_CAT_TABLE;
1884 } else if ((every_eof_par != null) && (!eof_seen[iindex])) {
1885 ilimit = first - 1;
1886 /* tex Fake one empty line. */
1887 eof_seen[iindex] = true;
1888 begin_token_list(every_eof_par, every_eof_text);
1889 return next_line_restart;
1890 } else {
1891 force_eof = true;
1892 }
1893 }
1894 }
1895 if (force_eof) {
1896 if (tracing_nesting_par > 0)
1897 if ((grp_stack[in_open] != cur_boundary) || (if_stack[in_open] != cond_ptr))
1898 if (!((iname == 19) || (iname == 21))) {
1899 /*tex Give warning for some unfinished groups and/or conditionals. */
1900 file_warning();
1901 }
1902 if ((iname > 21) || (iname == 20)) {
1903 report_stop_file(filetype_tex);
1904 decr(open_parens);
1905 }
1906 force_eof = false;
1907 /*tex \LUA\ input or \.{\\scantextokens} */
1908 if (tracing_scan_tokens_par > 0 && (iname == 20 || iname == 19)) {
1909 print_char(')');
1910 }
1911 if (iname == 21 || iname == 19) {
1912 end_file_reading();
1913 } else {
1914 end_file_reading();
1915 if (! suppress_outer_error_par)
1916 check_outer_validity();
1917 }
1918 return next_line_restart;
1919 }
1920 if (inhibit_eol || end_line_char_inactive)
1921 ilimit--;
1922 else
1923 buffer[ilimit] = (packed_ASCII_code) end_line_char_par;
1924 first = ilimit + 1;
1925 iloc = istart;
1926 /*tex We're ready to read. */
1927 } else {
1928 if (!terminal_input) {
1929 /*tex \.{\\read} line has ended */
1930 cur_cmd = 0;
1931 cur_chr = 0;
1932 return next_line_return;
1933 }
1934 if (input_ptr > 0) {
1935 /*tex Text was inserted during error recovery. */
1936 end_file_reading();
1937 /*tex Resume previous level. */
1938 return next_line_restart;
1939 }
1940 if (selector < log_only)
1941 open_log_file();
1942 if (interaction > nonstop_mode) {
1943 if (end_line_char_inactive)
1944 ilimit++;
1945 if (ilimit == istart) {
1946 /*tex Previous line was empty. */
1947 tprint_nl("(Please type a command or say `\\end')");
1948 }
1949 print_ln();
1950 first = istart;
1951 /*tex Input on-line into |buffer| */
1952 prompt_input("*");
1953 ilimit = last;
1954 if (end_line_char_inactive)
1955 ilimit--;
1956 else
1957 buffer[ilimit] = (packed_ASCII_code) end_line_char_par;
1958 first = ilimit + 1;
1959 iloc = istart;
1960 } else {
1961 /*tex
1962
1963 Nonstop mode, which is intended for overnight batch processing,
1964 never waits for on-line input.
1965
1966 */
1967 fatal_error("*** (job aborted, no legal \\end found)");
1968 }
1969 }
1970 return next_line_ok;
1971 }
1972
1973 /*tex
1974
1975 Let's consider now what happens when |get_next| is looking at a token list.
1976
1977 */
1978
1979 static boolean get_next_tokenlist(void)
1980 {
1981 register halfword t = token_info(iloc);
1982 /*tex Move to next. */
1983 iloc = token_link(iloc);
1984 if (t >= cs_token_flag) {
1985 /*tex A control sequence token */
1986 cur_cs = t - cs_token_flag;
1987 cur_cmd = eq_type(cur_cs);
1988 if (cur_cmd >= outer_call_cmd) {
1989 if (cur_cmd == dont_expand_cmd) {
1990 /*tex
1991
1992 Get the next token, suppressing expansion. The present point
1993 in the program is reached only when the |expand| routine has
1994 inserted a special marker into the input. In this special
1995 case, |token_info(iloc)| is known to be a control sequence
1996 token, and |token_link(iloc)=null|.
1997
1998 */
1999 cur_cs = token_info(iloc) - cs_token_flag;
2000 iloc = null;
2001 cur_cmd = eq_type(cur_cs);
2002 if (cur_cmd > max_command_cmd) {
2003 cur_cmd = relax_cmd;
2004 cur_chr = no_expand_flag;
2005 return true;
2006 }
2007 } else if (! suppress_outer_error_par) {
2008 check_outer_validity();
2009 }
2010 }
2011 cur_chr = equiv(cur_cs);
2012 } else {
2013 cur_cmd = token_cmd(t);
2014 cur_chr = token_chr(t);
2015 switch (cur_cmd) {
2016 case left_brace_cmd:
2017 align_state++;
2018 break;
2019 case right_brace_cmd:
2020 align_state--;
2021 break;
2022 case out_param_cmd:
2023 /*tex Insert macro parameter and |goto restart|. */
2024 begin_token_list(param_stack[param_start + cur_chr - 1], parameter);
2025 return false;
2026 break;
2027 }
2028 }
2029 return true;
2030 }
2031
2032 /*tex
2033
2034 Now we're ready to take the plunge into |get_next| itself. Parts of this
2035 routine are executed more often than any other instructions of \TeX.
2036
2037 This sets |cur_cmd|, |cur_chr|, |cur_cs| to next token:
2038
2039 */
2040
2041 void get_next(void)
2042 {
2043 RESTART:
2044 cur_cs = 0;
2045 if (istate != token_list) {
2046 /*tex Input from external file, |goto restart| if no input found. */
2047 if (!get_next_file())
2048 goto RESTART;
2049 } else {
2050 if (iloc == null) {
2051 end_token_list();
2052 /*tex List exhausted, resume previous level. */
2053 goto RESTART;
2054 } else if (!get_next_tokenlist()) {
2055 /*tex Parameter needs to be expanded. */
2056 goto RESTART;
2057 }
2058 }
2059 /*tex If an alignment entry has just ended, take appropriate action. */
2060 if ((cur_cmd == tab_mark_cmd || cur_cmd == car_ret_cmd) && align_state == 0) {
2061 insert_vj_template();
2062 goto RESTART;
2063 }
2064 }
2065
2066 /*tex
2067
2068 Since |get_next| is used so frequently in \TeX, it is convenient to define
2069 three related procedures that do a little more:
2070
2071 \startitemize
2072 \startitem
2073 |get_token| not only sets |cur_cmd| and |cur_chr|, it also sets
2074 |cur_tok|, a packed halfword version of the current token.
2075 \stopitem
2076 \startitem
2077 |get_x_token|, meaning ``get an expanded token,'' is like
2078 |get_token|, but if the current token turns out to be a user-defined
2079 control sequence (i.e., a macro call), or a conditional, or something
2080 like \.{\\topmark} or \.{\\expandafter} or \.{\\csname}, it is
2081 eliminated from the input by beginning the expansion of the macro or
2082 the evaluation of the conditional.
2083 \stopitem
2084 \startitem
2085 |x_token| is like |get_x_token| except that it assumes that
2086 |get_next| has already been called.
2087 \stopitem
2088 \stopitemize
2089
2090 In fact, these three procedures account for almost every use of |get_next|.
2091 No new control sequences will be defined except during a call of |get_token|,
2092 or when \.{\\csname} compresses a token list, because
2093 |no_new_control_sequence| is always |true| at other times.
2094
2095 This sets |cur_cmd|, |cur_chr|, |cur_tok|:
2096
2097 */
2098
2099 void get_token(void)
2100 {
2101 no_new_control_sequence = false;
2102 get_next();
2103 no_new_control_sequence = true;
2104 if (cur_cs == 0)
2105 cur_tok = token_val(cur_cmd, cur_chr);
2106 else
2107 cur_tok = cs_token_flag + cur_cs;
2108 }
2109
2110 /*tex This changes the string |s| to a token list. */
2111
2112 halfword string_to_toks(const char *ss)
2113 {
2114 /*tex tail of the token list */
2115 halfword p;
2116 /*tex new node being added to the token list via |store_new_token| */
2117 halfword q;
2118 /*tex token being appended */
2119 halfword t;
2120 const char *s = ss;
2121 const char *se = ss + strlen(s);
2122 p = temp_token_head;
2123 set_token_link(p, null);
2124 while (s < se) {
2125 t = (halfword) str2uni((const unsigned char *) s);
2126 s += utf8_size(t);
2127 if (t == ' ')
2128 t = space_token;
2129 else
2130 t = other_token + t;
2131 fast_store_new_token(t);
2132 }
2133 return token_link(temp_token_head);
2134 }
2135
2136 /*tex
2137
2138 The token lists for macros and for other things like \.{\\mark} and
2139 \.{\\output} and \.{\\write} are produced by a procedure called |scan_toks|.
2140
2141 Before we get into the details of |scan_toks|, let's consider a much simpler
2142 task, that of converting the current string into a token list. The |str_toks|
2143 function does this; it classifies spaces as type |spacer| and everything else
2144 as type |other_char|.
2145
2146 The token list created by |str_toks| begins at |link(temp_token_head)| and
2147 ends at the value |p| that is returned. (If |p=temp_token_head|, the list is
2148 empty.)
2149
2150 |lua_str_toks| is almost identical, but it also escapes the three symbols
2151 that |lua| considers special while scanning a literal string.
2152
2153 This changes the string |str_pool[b..pool_ptr]| to a token list:
2154
2155 */
2156
2157 halfword lua_str_toks(lstring b)
2158 {
2159 /*tex tail of the token list */
2160 halfword p;
2161 /*tex new node being added to the token list via |store_new_token| */
2162 halfword q;
2163 /*tex token being appended */
2164 halfword t;
2165 /*tex index into string */
2166 unsigned char *k;
2167 p = temp_token_head;
2168 set_token_link(p, null);
2169 k = (unsigned char *) b.s;
2170 while (k < (unsigned char *) b.s + b.l) {
2171 t = pool_to_unichar(k);
2172 k += utf8_size(t);
2173 if (t == ' ') {
2174 t = space_token;
2175 } else {
2176 if ((t == '\\') || (t == '"') || (t == '\'') || (t == 10) || (t == 13))
2177 fast_store_new_token(other_token + '\\');
2178 if (t == 10)
2179 t = 'n';
2180 if (t == 13)
2181 t = 'r';
2182 t = other_token + t;
2183 }
2184 fast_store_new_token(t);
2185 }
2186 return p;
2187 }
2188
2189 /*tex
2190
2191 Incidentally, the main reason for wanting |str_toks| is the function
2192 |the_toks|, which has similar input/output characteristics.
2193
2194 This changes the string |str_pool[b..pool_ptr]| to a token list:
2195
2196 */
2197
2198 halfword str_toks(lstring s)
2199 {
2200 /*tex tail of the token list */
2201 halfword p;
2202 /*tex new node being added to the token list via |store_new_token| */
2203 halfword q;
2204 /*tex token being appended */
2205 halfword t;
2206 /*tex index into string */
2207 unsigned char *k, *l;
2208 p = temp_token_head;
2209 set_token_link(p, null);
2210 k = s.s;
2211 l = k + s.l;
2212 while (k < l) {
2213 t = pool_to_unichar(k);
2214 k += utf8_size(t);
2215 if (t == ' ')
2216 t = space_token;
2217 else
2218 t = other_token + t;
2219 fast_store_new_token(t);
2220 }
2221 return p;
2222 }
2223
2224 /*tex
2225
2226 Most of the converter is similar to the one i made for macro so at some point
2227 I can make a helper; also todo: there is no need to go through the pool.
2228
2229 */
2230
2231 /*tex Change the string |str_pool[b..pool_ptr]| to a token list. */
2232
2233 halfword str_scan_toks(int ct, lstring s)
2234 {
2235 /*tex tail of the token list */
2236 halfword p;
2237 /*tex new node being added to the token list via |store_new_token| */
2238 halfword q;
2239 /*tex token being appended */
2240 halfword t;
2241 /*tex index into string */
2242 unsigned char *k, *l;
2243 int cc;
2244 p = temp_token_head;
2245 set_token_link(p, null);
2246 k = s.s;
2247 l = k + s.l;
2248 while (k < l) {
2249 t = pool_to_unichar(k);
2250 k += utf8_size(t);
2251 cc = get_cat_code(ct,t);
2252 if (cc == 0) {
2253 /*tex We have a potential control sequence so we check for it. */
2254 int _lname = 0 ;
2255 int _s = 0 ;
2256 int _c = 0 ;
2257 halfword _cs = null ;
2258 unsigned char *_name = k ;
2259 while (k < l) {
2260 t = (halfword) str2uni((const unsigned char *) k);
2261 _s = utf8_size(t);
2262 _c = get_cat_code(ct,t);
2263 if (_c == 11) {
2264 k += _s ;
2265 _lname = _lname + _s ;
2266 } else if (_c == 10) {
2267 /*tex We ignore a trailing space like normal scanning does. */
2268 k += _s ;
2269 break ;
2270 } else {
2271 break ;
2272 }
2273 }
2274 if (_s > 0) {
2275 /*tex We have a potential |\cs|. */
2276 _cs = string_lookup((const char *) _name, _lname);
2277 if (_cs == undefined_control_sequence) {
2278 /* let's play safe and backtrack */
2279 t = cc * (1<<21) + t ;
2280 k = _name ;
2281 } else {
2282 t = cs_token_flag + _cs;
2283 }
2284 } else {
2285 /*tex
2286
2287 Just a character with some meaning, so |\unknown| becomes
2288 effectively |\unknown| assuming that |\\| has some useful
2289 meaning of course.
2290
2291 */
2292 t = cc * (1<<21) + t ;
2293 k = _name ;
2294 }
2295
2296 } else {
2297 /*tex
2298
2299 Whatever token, so for instance $x^2$ just works given a \TEX\
2300 catcode regime.
2301
2302 */
2303 t = cc * (1<<21) + t ;
2304 }
2305 fast_store_new_token(t);
2306
2307 }
2308 return p;
2309 }
2310
2311 /*tex
2312
2313 Here's part of the |expand| subroutine that we are now ready to complete:
2314
2315 */
2316
2317 void ins_the_toks(void)
2318 {
2319 (void) the_toks();
2320 ins_list(token_link(temp_token_head));
2321 }
2322
2323 #define set_toks_register(n,t,g) do { \
2324 int a = (g>0) ? 4 : 0; \
2325 halfword ref = get_avail(); \
2326 set_token_ref_count(ref, 0); \
2327 set_token_link(ref, token_link(t)); \
2328 define(n + toks_base, call_cmd, ref); \
2329 } while (0)
2330
2331 #define append_copied_toks_list(s,t) do { \
2332 halfword p; \
2333 halfword q; \
2334 p = temp_token_head; \
2335 set_token_link(p, null); \
2336 while (s != null) { \
2337 fast_store_new_token(token_info(s)); \
2338 s = token_link(s); \
2339 } \
2340 while (t != null) { \
2341 fast_store_new_token(token_info(t)); \
2342 t = token_link(t); \
2343 } \
2344 } while (0)
2345
2346
2347 /*tex
2348
2349 \starttabulate[|T||T||]
2350 \NC 0 \NC \type {toksapp} \NC 1 \NC \type {etoksapp} \NC \NR
2351 \NC 2 \NC \type {tokspre} \NC 3 \NC \type {etokspre} \NC \NR
2352 \NC 4 \NC \type {gtoksapp} \NC 5 \NC \type {xtoksapp} \NC \NR
2353 \NC 6 \NC \type {gtokspre} \NC 7 \NC \type {xtokspre} \NC \NR
2354 \stoptabulate
2355
2356 */
2357
2358 # define immediate_permitted(n,target) ((eq_level(toks_base + n) == cur_level) && (token_ref_count(target) == 0))
2359
2360 void combine_the_toks(void)
2361 {
2362 halfword how = cur_chr;
2363 halfword source = null;
2364 halfword target = null;
2365 halfword append = (how == 0) || (how == 1) || (how == 4) || (how == 5);
2366 halfword expand = odd(how);
2367 halfword global = how > 3;
2368 halfword nt, ns, s, t, p, q, h;
2369 get_x_token();
2370 /*tex The target. */
2371 if (cur_cmd == assign_toks_cmd) {
2372 /*tex Check range. */
2373 nt = equiv(cur_cs) - toks_base;
2374 } else {
2375 back_input();
2376 scan_register_num();
2377 nt = cur_val;
2378 }
2379 /*tex The source. */
2380 do {
2381 get_x_token();
2382 } while (cur_cmd == spacer_cmd);
2383 if (cur_cmd == left_brace_cmd) {
2384 back_input();
2385 scan_toks(false,expand);
2386 source = def_ref;
2387 /*tex The action. */
2388 if (source != null) {
2389 target = toks(nt);
2390 if (target == null) {
2391 set_toks_register(nt,source,global);
2392 token_link(source) = null;
2393 } else {
2394 s = token_link(source);
2395 if (s != null) {
2396 t = token_link(target);
2397 if (t == null) {
2398 /*tex Can this happen? */
2399 set_token_link(target, s);
2400 token_link(source) = null;
2401 } else if (append) {
2402 /*tex Append. */
2403 if (immediate_permitted(nt,target)) {
2404 p = t;
2405 while (token_link(p) != null) {
2406 p = token_link(p);
2407 }
2408 while (s != null) {
2409 fast_store_new_token(token_info(s));
2410 s = token_link(s);
2411 }
2412 } else {
2413 append_copied_toks_list(t,s);
2414 set_toks_register(nt,temp_token_head,global);
2415 }
2416 } else {
2417 /* prepend */
2418 if (immediate_permitted(nt,target)) {
2419 h = null;
2420 p = null ;
2421 while (s != null) {
2422 fast_store_new_token(token_info(s));
2423 if (h == null) {
2424 h = p;
2425 }
2426 s = token_link(s);
2427 }
2428 set_token_link(p,t);
2429 set_token_link(target,h);
2430 } else {
2431 append_copied_toks_list(s,t);
2432 set_toks_register(nt,temp_token_head,global);
2433 }
2434 }
2435 }
2436 }
2437 flush_list(source);
2438 }
2439 } else {
2440 if (cur_cmd == assign_toks_cmd) {
2441 ns = equiv(cur_cs) - toks_base;
2442 /*tex Check range. */
2443 } else {
2444 scan_register_num();
2445 ns = cur_val;
2446 }
2447 /*tex The action. */
2448 source = toks(ns);
2449 if (source != null) {
2450 target = toks(nt);
2451 if (target == null) {
2452 /*tex The assign. */
2453 token_ref_count(source)++;
2454 equiv(toks_base+nt) = source;
2455 return;
2456 }
2457 s = token_link(source);
2458 t = token_link(target);
2459 if (append) {
2460 /*tex Append. */
2461 if (immediate_permitted(nt,target)) {
2462 p = t;
2463 while (token_link(p) != null) {
2464 p = token_link(p);
2465 }
2466 while (s != null) {
2467 fast_store_new_token(token_info(s));
2468 s = token_link(s);
2469 }
2470 } else {
2471 append_copied_toks_list(t,s);
2472 set_toks_register(nt,temp_token_head,global);
2473 }
2474 } else {
2475 /*tex Prepend. */
2476 if (immediate_permitted(nt,target)) {
2477 h = null;
2478 p = null;
2479 while (s != null) {
2480 fast_store_new_token(token_info(s));
2481 if (h == null) {
2482 h = p;
2483 }
2484 s = token_link(s);
2485 }
2486 set_token_link(p,t);
2487 set_token_link(target,h);
2488 } else {
2489 append_copied_toks_list(s,t);
2490 set_toks_register(nt,temp_token_head,global);
2491 }
2492 }
2493 }
2494 }
2495 }
2496
2497 /*tex
2498
2499 This routine, used in the next one, prints the job name, possibly modified by
2500 the |process_jobname| callback.
2501
2502 */
2503
2504 static void print_job_name(void)
2505 {
2506 if (job_name) {
2507 /*tex C strings for jobname before and after processing. */
2508 char *s, *ss;
2509 int callback_id, lua_retval;
2510 s = (char*)str_string(job_name);
2511 callback_id = callback_defined(process_jobname_callback);
2512 if (callback_id > 0) {
2513 lua_retval = run_callback(callback_id, "S->S", s, &ss);
2514 if ((lua_retval == true) && (ss != NULL))
2515 s = ss;
2516 }
2517 tprint(s);
2518 } else {
2519 print(job_name);
2520 }
2521 }
2522
2523 /*tex
2524
2525 Here is a routine that print the result of a convert command, using the
2526 argument |i|. It returns |false | if it does not know to print the code |c|.
2527 The function exists because lua code and tex code can both call it to convert
2528 something.
2529
2530 Parse optional \LUA\ state integer, or an instance name to be stored in |sn|
2531 and get the next non-blank non-relax non-call token.
2532
2533 */
2534
2535 int scan_lua_state(void)
2536 {
2537 int sn = 0;
2538 do {
2539 get_x_token();
2540 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
2541 back_input();
2542 if (cur_cmd != left_brace_cmd) {
2543 if (scan_keyword("name")) {
2544 (void) scan_toks(false, true);
2545 sn = def_ref;
2546 } else {
2547 scan_register_num();
2548 if (get_lua_name(cur_val))
2549 sn = (cur_val - 65536);
2550 }
2551 }
2552 return sn;
2553 }
2554
2555 /*tex
2556
2557 The procedure |conv_toks| uses |str_toks| to insert the token list for
2558 |convert| functions into the scanner; `\.{\\outer}' control sequences are
2559 allowed to follow `\.{\\string}' and `\.{\\meaning}'.
2560
2561 The extra temp string |u| is needed because |pdf_scan_ext_toks| incorporates
2562 any pending string in its output. In order to save such a pending string, we
2563 have to create a temporary string that is destroyed immediately after.
2564
2565 */
2566
2567 #define push_selector { \
2568 old_setting = selector; \
2569 selector = new_string; \
2570 }
2571
2572 #define pop_selector { \
2573 selector = old_setting; \
2574 }
2575
2576 static int do_variable_dvi(halfword c)
2577 {
2578 return 0;
2579 }
2580
2581 #define do_variable_backend_int(i) \
2582 cur_cmd = assign_int_cmd; \
2583 cur_val = backend_int_base + i; \
2584 cur_tok = token_val(cur_cmd, cur_val); \
2585 back_input();
2586
2587 #define do_variable_backend_dimen(i) \
2588 cur_cmd = assign_dimen_cmd; \
2589 cur_val = backend_dimen_base + i; \
2590 cur_tok = token_val(cur_cmd, cur_val); \
2591 back_input();
2592
2593 #define do_variable_backend_toks(i) \
2594 cur_cmd = assign_toks_cmd; \
2595 cur_val = backend_toks_base + i ; \
2596 cur_tok = token_val(cur_cmd, cur_val); \
2597 back_input();
2598
2599 static int do_variable_pdf(halfword c)
2600 {
2601 if (scan_keyword("compresslevel")) { do_variable_backend_int(c_pdf_compress_level); }
2602 else if (scan_keyword("decimaldigits")) { do_variable_backend_int(c_pdf_decimal_digits); }
2603 else if (scan_keyword("imageresolution")) { do_variable_backend_int(c_pdf_image_resolution); }
2604 else if (scan_keyword("pkresolution")) { do_variable_backend_int(c_pdf_pk_resolution); }
2605 else if (scan_keyword("uniqueresname")) { do_variable_backend_int(c_pdf_unique_resname); }
2606 else if (scan_keyword("majorversion")) { do_variable_backend_int(c_pdf_major_version); }
2607 else if (scan_keyword("minorversion")) { do_variable_backend_int(c_pdf_minor_version); }
2608 else if (scan_keyword("pagebox")) { do_variable_backend_int(c_pdf_pagebox); }
2609 else if (scan_keyword("inclusionerrorlevel")) { do_variable_backend_int(c_pdf_inclusion_errorlevel); }
2610 else if (scan_keyword("ignoreunknownimages")) { do_variable_backend_int(c_pdf_ignore_unknown_images); }
2611 else if (scan_keyword("gamma")) { do_variable_backend_int(c_pdf_gamma); }
2612 else if (scan_keyword("imageapplygamma")) { do_variable_backend_int(c_pdf_image_apply_gamma); }
2613 else if (scan_keyword("imagegamma")) { do_variable_backend_int(c_pdf_image_gamma); }
2614 else if (scan_keyword("imagehicolor")) { do_variable_backend_int(c_pdf_image_hicolor); }
2615 else if (scan_keyword("imageaddfilename")) { do_variable_backend_int(c_pdf_image_addfilename); }
2616 else if (scan_keyword("objcompresslevel")) { do_variable_backend_int(c_pdf_obj_compress_level); }
2617 else if (scan_keyword("inclusioncopyfonts")) { do_variable_backend_int(c_pdf_inclusion_copy_font); }
2618 else if (scan_keyword("gentounicode")) { do_variable_backend_int(c_pdf_gen_tounicode); }
2619 else if (scan_keyword("pkfixeddpi")) { do_variable_backend_int(c_pdf_pk_fixed_dpi); }
2620 else if (scan_keyword("suppressoptionalinfo")) { do_variable_backend_int(c_pdf_suppress_optional_info); }
2621 else if (scan_keyword("omitcidset")) { do_variable_backend_int(c_pdf_omit_cidset); }
2622 else if (scan_keyword("recompress")) { do_variable_backend_int(c_pdf_recompress); }
2623 else if (scan_keyword("omitcharset")) { do_variable_backend_int(c_pdf_omit_charset); }
2624 else if (scan_keyword("omitinfodict")) { do_variable_backend_int(c_pdf_omit_infodict); }
2625 else if (scan_keyword("omitmediabox")) { do_variable_backend_int(c_pdf_omit_mediabox); }
2626 else if (scan_keyword("linking")) { do_variable_backend_int(c_pdf_linking); }
2627 else if (scan_keyword("omitprocset")) { do_variable_backend_int(c_pdf_omit_procset); }
2628 else if (scan_keyword("ptexprefix")) { do_variable_backend_int(c_pdf_ptex_prefix); }
2629
2630 else if (scan_keyword("horigin")) { do_variable_backend_dimen(d_pdf_h_origin); }
2631 else if (scan_keyword("vorigin")) { do_variable_backend_dimen(d_pdf_v_origin); }
2632 else if (scan_keyword("threadmargin")) { do_variable_backend_dimen(d_pdf_thread_margin); }
2633 else if (scan_keyword("destmargin")) { do_variable_backend_dimen(d_pdf_dest_margin); }
2634 else if (scan_keyword("linkmargin")) { do_variable_backend_dimen(d_pdf_link_margin); }
2635 else if (scan_keyword("xformmargin")) { do_variable_backend_dimen(d_pdf_xform_margin); }
2636
2637 else if (scan_keyword("pageattr")) { do_variable_backend_toks(t_pdf_page_attr); }
2638 else if (scan_keyword("pageresources")) { do_variable_backend_toks(t_pdf_page_resources); }
2639 else if (scan_keyword("pagesattr")) { do_variable_backend_toks(t_pdf_pages_attr); }
2640 else if (scan_keyword("xformattr")) { do_variable_backend_toks(t_pdf_xform_attr); }
2641 else if (scan_keyword("xformresources")) { do_variable_backend_toks(t_pdf_xform_resources); }
2642 else if (scan_keyword("pkmode")) { do_variable_backend_toks(t_pdf_pk_mode); }
2643 else if (scan_keyword("trailerid")) { do_variable_backend_toks(t_pdf_trailer_id); }
2644
2645 else
2646 return 0;
2647 return 1;
2648 }
2649
2650 static int do_feedback_dvi(halfword c)
2651 {
2652 return 0;
2653 }
2654
2655 /*tex Codes not really needed but cleaner when testing */
2656
2657 #define pdftex_version 140 /* these values will not change any more */
2658 #define pdftex_revision "0" /* these values will not change any more */
2659
2660 static int do_feedback_pdf(halfword c)
2661 {
2662 /*tex holds |selector| setting */
2663 int old_setting;
2664 /*tex |scanner_status| upon entry */
2665 int save_scanner_status;
2666 /*tex |def_ref| upon entry, important if inside `\.{\\message}' */
2667 halfword save_def_ref;
2668 halfword save_warning_index;
2669 /*tex temp boolean */
2670 boolean bool_val;
2671 /*tex first temp string */
2672 str_number s;
2673 /*tex for use with |set_ff| */
2674 int ff;
2675 /*tex third temp string, will become non-nil if a string is already being built */
2676 str_number u = 0;
2677 /*tex color stack init str */
2678 char *str;
2679 if (scan_keyword("lastlink")) {
2680 push_selector;
2681 print_int(pdf_last_link);
2682 pop_selector;
2683 } else if (scan_keyword("retval")) {
2684 push_selector;
2685 print_int(pdf_retval);
2686 pop_selector;
2687 } else if (scan_keyword("lastobj")) {
2688 push_selector;
2689 print_int(pdf_last_obj);
2690 pop_selector;
2691 } else if (scan_keyword("lastannot")) {
2692 push_selector;
2693 print_int(pdf_last_annot);
2694 pop_selector;
2695 } else if (scan_keyword("xformname")) {
2696 scan_int();
2697 check_obj_type(static_pdf, obj_type_xform, cur_val);
2698 push_selector;
2699 print_int(obj_info(static_pdf, cur_val));
2700 pop_selector;
2701 } else if (scan_keyword("creationdate")) {
2702 ins_list(string_to_toks(getcreationdate(static_pdf)));
2703 /*tex No further action. */
2704 return 2;
2705 } else if (scan_keyword("fontname")) {
2706 scan_font_ident();
2707 if (cur_val == null_font)
2708 normal_error("pdf backend", "invalid font identifier when asking 'fontname'");
2709 pdf_check_vf(cur_val);
2710 if (!font_used(cur_val))
2711 pdf_init_font(static_pdf, cur_val);
2712 push_selector;
2713 set_ff(cur_val);
2714 print_int(obj_info(static_pdf, pdf_font_num(ff)));
2715 pop_selector;
2716 } else if (scan_keyword("fontobjnum")) {
2717 scan_font_ident();
2718 if (cur_val == null_font)
2719 normal_error("pdf backend", "invalid font identifier when asking 'objnum'");
2720 pdf_check_vf(cur_val);
2721 if (!font_used(cur_val))
2722 pdf_init_font(static_pdf, cur_val);
2723 push_selector;
2724 set_ff(cur_val);
2725 print_int(pdf_font_num(ff));
2726 pop_selector;
2727 } else if (scan_keyword("fontsize")) {
2728 scan_font_ident();
2729 if (cur_val == null_font)
2730 normal_error("pdf backend", "invalid font identifier when asking 'fontsize'");
2731 push_selector;
2732 print_scaled(font_size(cur_val));
2733 tprint("pt");
2734 pop_selector;
2735 } else if (scan_keyword("pageref")) {
2736 scan_int();
2737 if (cur_val <= 0)
2738 normal_error("pdf backend", "invalid page number when asking 'pageref'");
2739 push_selector;
2740 print_int(pdf_get_obj(static_pdf, obj_type_page, cur_val, false));
2741 pop_selector;
2742 } else if (scan_keyword("colorstackinit")) {
2743 bool_val = scan_keyword("page");
2744 if (scan_keyword("direct"))
2745 cur_val = direct_always;
2746 else if (scan_keyword("page"))
2747 cur_val = direct_page;
2748 else if (scan_keyword("text"))
2749 cur_val = direct_text;
2750 else if (scan_keyword("raw"))
2751 cur_val = direct_raw;
2752 else if (scan_keyword("origin"))
2753 cur_val = set_origin;
2754 else
2755 cur_val = set_origin;
2756 save_scanner_status = scanner_status;
2757 save_warning_index = warning_index;
2758 save_def_ref = def_ref;
2759 u = save_cur_string();
2760 scan_toks(false, true);
2761 s = tokens_to_string(def_ref);
2762 delete_token_ref(def_ref);
2763 def_ref = save_def_ref;
2764 warning_index = save_warning_index;
2765 scanner_status = save_scanner_status;
2766 str = makecstring(s);
2767 cur_val = newcolorstack(str, cur_val, bool_val);
2768 free(str);
2769 flush_str(s);
2770 cur_val_level = int_val_level;
2771 if (cur_val < 0) {
2772 print_err("Too many color stacks");
2773 help2("The number of color stacks is limited to 32768.",
2774 "I'll use the default color stack 0 here.");
2775 error();
2776 cur_val = 0;
2777 restore_cur_string(u);
2778 }
2779 push_selector;
2780 print_int(cur_val);
2781 pop_selector;
2782 } else if (scan_keyword("version")) {
2783 push_selector;
2784 print_int(pdftex_version);
2785 pop_selector;
2786 } else if (scan_keyword("revision")) {
2787 ins_list(string_to_toks(pdftex_revision));
2788 return 2;
2789 } else {
2790 return 0;
2791 }
2792 return 1;
2793 }
2794
2795 void conv_toks(void)
2796 {
2797 /*tex holds |selector| setting */
2798 int old_setting;
2799 halfword p, q;
2800 /*tex |scanner_status| upon entry */
2801 int save_scanner_status;
2802 /*tex |def_ref| upon entry, important if inside `\.{\\message}' */
2803 halfword save_def_ref;
2804 halfword save_warning_index;
2805 /*tex temp boolean */
2806 boolean bool_val;
2807 /*tex first temp string */
2808 str_number s;
2809 /*tex lua chunk name */
2810 int sn;
2811 /*tex third temp string, will become non-nil if a string is already being built */
2812 str_number u = 0;
2813 /*tex desired type of conversion */
2814 int c = cur_chr;
2815 str_number str;
2816 int i = 0;
2817 /*tex Scan the argument for command |c|. */
2818 switch (c) {
2819 case number_code:
2820 scan_int();
2821 push_selector;
2822 print_int(cur_val);
2823 pop_selector;
2824 break;
2825 case lua_function_code:
2826 scan_int();
2827 if (cur_val <= 0) {
2828 normal_error("luafunction", "invalid number");
2829 } else {
2830 u = save_cur_string();
2831 luacstrings = 0;
2832 luafunctioncall(cur_val);
2833 restore_cur_string(u);
2834 if (luacstrings > 0)
2835 lua_string_start();
2836 }
2837 return;
2838 break;
2839 case lua_bytecode_code:
2840 scan_int();
2841 if (cur_val < 0 || cur_val > 65535) {
2842 normal_error("luabytecode", "invalid number");
2843 } else {
2844 u = save_cur_string();
2845 luacstrings = 0;
2846 luabytecodecall(cur_val);
2847 restore_cur_string(u);
2848 if (luacstrings > 0)
2849 lua_string_start();
2850 }
2851 return;
2852 break;
2853 case lua_code:
2854 u = save_cur_string();
2855 save_scanner_status = scanner_status;
2856 save_def_ref = def_ref;
2857 save_warning_index = warning_index;
2858 sn = scan_lua_state();
2859 scan_toks(false, true);
2860 s = def_ref;
2861 warning_index = save_warning_index;
2862 def_ref = save_def_ref;
2863 scanner_status = save_scanner_status;
2864 luacstrings = 0;
2865 luatokencall(s, sn);
2866 delete_token_ref(s);
2867 restore_cur_string(u);
2868 if (luacstrings > 0)
2869 lua_string_start();
2870 /*tex No further action. */
2871 return;
2872 break;
2873 case expanded_code:
2874 save_scanner_status = scanner_status;
2875 save_warning_index = warning_index;
2876 save_def_ref = def_ref;
2877 u = save_cur_string();
2878 scan_toks(false, true);
2879 warning_index = save_warning_index;
2880 scanner_status = save_scanner_status;
2881 ins_list(token_link(def_ref));
2882 token_link(def_ref) = null;
2883 free_avail(def_ref);
2884 def_ref = save_def_ref;
2885 restore_cur_string(u);
2886 /*tex No further action. */
2887 return;
2888 break;
2889 case immediate_assignment_code:
2890 case immediate_assigned_code:
2891 /*tex
2892
2893 This is on-the-road-to-bachotex brain-wave but it needs a bit
2894 more testing. A first variant did more in sequence till a relax
2895 of spacer was seen (like do_assignments). It permits for instance
2896 setting counters in full expansion.
2897
2898 */
2899 save_scanner_status = scanner_status;
2900 save_warning_index = warning_index;
2901 save_def_ref = def_ref;
2902 u = save_cur_string();
2903 do {
2904 get_x_token();
2905 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
2906 if (c == immediate_assignment_code) {
2907 /*tex one-step do_assignment */
2908 if (cur_cmd > max_non_prefixed_command) {
2909 set_box_allowed = false;
2910 if (cur_cmd == combine_toks_cmd)
2911 combine_the_toks();
2912 else
2913 prefixed_command();
2914 set_box_allowed = true;
2915 }
2916 /*tex done */
2917 } else {
2918 /*tex pseudo token list do_assignment */
2919 if (cur_cmd == left_brace_cmd) {
2920 while (1) {
2921 do {
2922 get_x_token();
2923 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
2924 if (cur_cmd == right_brace_cmd) {
2925 break;
2926 } else {
2927 set_box_allowed = false;
2928 if (cur_cmd == combine_toks_cmd)
2929 combine_the_toks();
2930 else
2931 prefixed_command();
2932 set_box_allowed = true;
2933 }
2934 }
2935 }
2936 /*tex done */
2937 }
2938 warning_index = save_warning_index;
2939 scanner_status = save_scanner_status;
2940 def_ref = save_def_ref;
2941 restore_cur_string(u);
2942 return;
2943 break;
2944 case math_style_code:
2945 push_selector;
2946 print_math_style();
2947 pop_selector;
2948 break;
2949 case string_code:
2950 save_scanner_status = scanner_status;
2951 scanner_status = normal;
2952 get_token();
2953 scanner_status = save_scanner_status;
2954 push_selector;
2955 if (cur_cs != 0)
2956 sprint_cs(cur_cs);
2957 else
2958 print(cur_chr);
2959 pop_selector;
2960 break;
2961 case cs_string_code:
2962 save_scanner_status = scanner_status;
2963 scanner_status = normal;
2964 get_token();
2965 scanner_status = save_scanner_status;
2966 push_selector;
2967 if (cur_cs != 0)
2968 sprint_cs_name(cur_cs);
2969 else
2970 print(cur_chr);
2971 pop_selector;
2972 break;
2973 case roman_numeral_code:
2974 scan_int();
2975 push_selector;
2976 print_roman_int(cur_val);
2977 pop_selector;
2978 break;
2979 case meaning_code:
2980 save_scanner_status = scanner_status;
2981 scanner_status = normal;
2982 get_token();
2983 scanner_status = save_scanner_status;
2984 push_selector;
2985 print_meaning();
2986 pop_selector;
2987 break;
2988 case uchar_code:
2989 scan_char_num();
2990 push_selector;
2991 print(cur_val);
2992 pop_selector;
2993 break;
2994 case lua_escape_string_code:
2995 {
2996 lstring escstr;
2997 int l = 0;
2998 save_scanner_status = scanner_status;
2999 save_def_ref = def_ref;
3000 save_warning_index = warning_index;
3001 scan_toks(false, true);
3002 bool_val = in_lua_escape;
3003 in_lua_escape = true;
3004 escstr.s = (unsigned char *) tokenlist_to_cstring(def_ref, false, &l);
3005 escstr.l = (unsigned) l;
3006 in_lua_escape = bool_val;
3007 delete_token_ref(def_ref);
3008 def_ref = save_def_ref;
3009 warning_index = save_warning_index;
3010 scanner_status = save_scanner_status;
3011 (void) lua_str_toks(escstr);
3012 ins_list(token_link(temp_token_head));
3013 free(escstr.s);
3014 return;
3015 }
3016 /*tex no further action */
3017 break;
3018 case font_id_code:
3019 scan_font_ident();
3020 push_selector;
3021 print_int(cur_val);
3022 pop_selector;
3023 break;
3024 case font_name_code:
3025 scan_font_ident();
3026 push_selector;
3027 append_string((unsigned char *) font_name(cur_val),(unsigned) strlen(font_name(cur_val)));
3028 if (font_size(cur_val) != font_dsize(cur_val)) {
3029 tprint(" at ");
3030 print_scaled(font_size(cur_val));
3031 tprint("pt");
3032 }
3033 pop_selector;
3034 break;
3035 case left_margin_kern_code:
3036 scan_int();
3037 if ((box(cur_val) == null) || (type(box(cur_val)) != hlist_node))
3038 normal_error("marginkern", "a non-empty hbox expected");
3039 push_selector;
3040 p = list_ptr(box(cur_val));
3041 while ((p != null) && (type(p) == glue_node)) {
3042 p = vlink(p);
3043 }
3044 if ((p != null) && (type(p) == margin_kern_node) && (subtype(p) == left_side))
3045 print_scaled(width(p));
3046 else
3047 print_char('0');
3048 tprint("pt");
3049 pop_selector;
3050 break;
3051 case right_margin_kern_code:
3052 scan_int();
3053 if ((box(cur_val) == null) || (type(box(cur_val)) != hlist_node))
3054 normal_error("marginkern", "a non-empty hbox expected");
3055 push_selector;
3056 p = list_ptr(box(cur_val));
3057 if (p != null) {
3058 p = tail_of_list(p);
3059 /*tex
3060
3061 There can be a leftskip, rightskip, penalty and yes, also a
3062 disc node with a nesting node that points to glue spec ...
3063 and we don't want to analyze that messy lot.
3064
3065 */
3066 while ((p != null) && (type(p) == glue_node)) {
3067 p = alink(p);
3068 }
3069 if ((p != null) && ! ((type(p) == margin_kern_node) && (subtype(p) == right_side))) {
3070 if (type(p) == disc_node) {
3071 q = alink(p);
3072 if ((q != null) && ((type(q) == margin_kern_node) && (subtype(q) == right_side))) {
3073 p = q;
3074 } else {
3075 /*tex
3076
3077 Officially we should look in the replace but
3078 currently protrusion doesn't work anyway with
3079 "foo\discretionary{}{}{bar-} " (no following
3080 char) so we don't need it now.
3081 */
3082 }
3083 }
3084 }
3085 }
3086 if ((p != null) && (type(p) == margin_kern_node) && (subtype(p) == right_side))
3087 print_scaled(width(p));
3088 else
3089 print_char('0');
3090 tprint("pt");
3091 pop_selector;
3092 break;
3093 case uniform_deviate_code:
3094 scan_int();
3095 push_selector;
3096 print_int(unif_rand(cur_val));
3097 pop_selector;
3098 break;
3099 case normal_deviate_code:
3100 push_selector;
3101 print_int(norm_rand());
3102 pop_selector;
3103 break;
3104 case math_char_class_code:
3105 {
3106 mathcodeval mval;
3107 scan_int();
3108 mval = get_math_code(cur_val);
3109 push_selector;
3110 print_int(mval.class_value);
3111 pop_selector;
3112 }
3113 break;
3114 case math_char_fam_code:
3115 {
3116 mathcodeval mval;
3117 scan_int();
3118 mval = get_math_code(cur_val);
3119 push_selector;
3120 print_int(mval.family_value);
3121 pop_selector;
3122 }
3123 break;
3124 case math_char_slot_code:
3125 {
3126 mathcodeval mval;
3127 scan_int();
3128 mval = get_math_code(cur_val);
3129 push_selector;
3130 print_int(mval.character_value);
3131 pop_selector;
3132 }
3133 break;
3134 case insert_ht_code:
3135 scan_register_num();
3136 push_selector;
3137 i = cur_val;
3138 p = page_ins_head;
3139 while (i >= subtype(vlink(p)))
3140 p = vlink(p);
3141 if (subtype(p) == i)
3142 print_scaled(height(p));
3143 else
3144 print_char('0');
3145 tprint("pt");
3146 pop_selector;
3147 break;
3148 case job_name_code:
3149 if (job_name == 0)
3150 open_log_file();
3151 push_selector;
3152 print_job_name();
3153 pop_selector;
3154 break;
3155 case format_name_code:
3156 if (job_name == 0)
3157 open_log_file();
3158 push_selector;
3159 print(format_name);
3160 pop_selector;
3161 break;
3162 case luatex_banner_code:
3163 push_selector;
3164 tprint(luatex_banner);
3165 pop_selector;
3166 break;
3167 case luatex_revision_code:
3168 push_selector;
3169 print(get_luatexrevision());
3170 pop_selector;
3171 break;
3172 case etex_code:
3173 push_selector;
3174 tprint(eTeX_version_string);
3175 pop_selector;
3176 break;
3177 case eTeX_revision_code:
3178 push_selector;
3179 tprint(eTeX_revision);
3180 pop_selector;
3181 break;
3182 case font_identifier_code:
3183 confusion("convert");
3184 break;
3185 default:
3186 confusion("convert");
3187 break;
3188 }
3189 str = make_string();
3190 (void) str_toks(str_lstring(str));
3191 flush_str(str);
3192 ins_list(token_link(temp_token_head));
3193 }
3194
3195 void do_feedback(void)
3196 {
3197 int c = cur_chr;
3198 str_number str;
3199 int done = 1;
3200 switch (c) {
3201 case dvi_feedback_code:
3202 if (get_o_mode() == OMODE_DVI) {
3203 done = do_feedback_dvi(c);
3204 } else {
3205 tex_error("unexpected use of \\dvifeedback",null);
3206 return ;
3207 }
3208 if (done==0) {
3209 /* we recover */
3210 normal_warning("dvi backend","unexpected use of \\dvifeedback");
3211 return;
3212 } else if (done==2) {
3213 return;
3214 }
3215 break;
3216 case pdf_feedback_code:
3217 if (get_o_mode() == OMODE_PDF) {
3218 done = do_feedback_pdf(c);
3219 } else {
3220 tex_error("unexpected use of \\pdffeedback",null);
3221 return ;
3222 }
3223 if (done==0) {
3224 /*tex We recover. */
3225 normal_warning("pdf backend","unexpected use of \\pdffeedback");
3226 return;
3227 } else if (done==2) {
3228 return;
3229 }
3230 break;
3231 default:
3232 confusion("feedback");
3233 break;
3234 }
3235 str = make_string();
3236 (void) str_toks(str_lstring(str));
3237 flush_str(str);
3238 ins_list(token_link(temp_token_head));
3239 }
3240
3241 void do_variable(void)
3242 {
3243 int c = cur_chr;
3244 int done = 1;
3245 switch (c) {
3246 case dvi_variable_code:
3247 done = do_variable_dvi(c);
3248 if (done==0) {
3249 /*tex We recover. */
3250 normal_warning("dvi backend","unexpected use of \\dvivariable");
3251 }
3252 return;
3253 break;
3254 case pdf_variable_code:
3255 done = do_variable_pdf(c);
3256 if (done==0) {
3257 /*tex We recover. */
3258 normal_warning("pdf backend","unexpected use of \\pdfvariable");
3259 }
3260 return;
3261 break;
3262 default:
3263 confusion("variable");
3264 break;
3265 }
3266 }
3267
3268 /*tex This boolean is keeping track of the lua string escape state */
3269
3270 boolean in_lua_escape;
3271
3272 static int the_convert_string_dvi(halfword c, int i)
3273 {
3274 return 0 ;
3275 }
3276
3277 static int the_convert_string_pdf(halfword c, int i)
3278 {
3279 int ff;
3280 if (get_o_mode() != OMODE_PDF) {
3281 return 0;
3282 } else if (scan_keyword("lastlink")) {
3283 print_int(pdf_last_link);
3284 } else if (scan_keyword("retval")) {
3285 print_int(pdf_retval);
3286 } else if (scan_keyword("lastobj")) {
3287 print_int(pdf_last_obj);
3288 } else if (scan_keyword("lastannot")) {
3289 print_int(pdf_last_annot);
3290 } else if (scan_keyword("xformname")) {
3291 print_int(obj_info(static_pdf, i));
3292 } else if (scan_keyword("creationdate")) {
3293 return 0;
3294 } else if (scan_keyword("fontname")) {
3295 set_ff(i);
3296 print_int(obj_info(static_pdf, pdf_font_num(ff)));
3297 } else if (scan_keyword("fontobjnum")) {
3298 set_ff(i);
3299 print_int(pdf_font_num(ff));
3300 } else if (scan_keyword("fontsize")) {
3301 print_scaled(font_size(i));
3302 tprint("pt");
3303 } else if (scan_keyword("pageref")) {
3304 print_int(pdf_get_obj(static_pdf, obj_type_page, i, false));
3305 } else if (scan_keyword("colorstackinit")) {
3306 return 0;
3307 } else {
3308 return 0;
3309 }
3310 return 1;
3311 }
3312
3313 str_number the_convert_string(halfword c, int i)
3314 {
3315 int old_setting;
3316 str_number ret = 0;
3317 boolean done = true ;
3318 old_setting = selector;
3319 selector = new_string;
3320 switch (c) {
3321 case number_code:
3322 print_int(i);
3323 break;
3324 /* case lua_function_code: */
3325 /* case lua_code: */
3326 /* case expanded_code: */
3327 case math_style_code:
3328 print_math_style();
3329 break;
3330 /* case string_code: */
3331 /* case cs_string_code: */
3332 case roman_numeral_code:
3333 print_roman_int(i);
3334 break;
3335 /* case meaning_code: */
3336 case uchar_code:
3337 print(i);
3338 break;
3339 /* lua_escape_string_code: */
3340 case font_id_code:
3341 print_int(i);
3342 break;
3343 case font_name_code:
3344 append_string((unsigned char *) font_name(i),(unsigned) strlen(font_name(i)));
3345 if (font_size(i) != font_dsize(i)) {
3346 tprint(" at ");
3347 print_scaled(font_size(i));
3348 tprint("pt");
3349 }
3350 break;
3351 /* left_margin_kern_code: */
3352 /* right_margin_kern_code: */
3353 case uniform_deviate_code:
3354 print_int(unif_rand(i));
3355 break;
3356 case normal_deviate_code:
3357 print_int(norm_rand());
3358 break;
3359 /* math_char_class_code: */
3360 /* math_char_fam_code: */
3361 /* math_char_slot_code: */
3362 /* insert_ht_code: */
3363 case job_name_code:
3364 print_job_name();
3365 break;
3366 case format_name_code:
3367 print(format_name);
3368 break;
3369 case luatex_banner_code:
3370 tprint(luatex_banner);
3371 break;
3372 case luatex_revision_code:
3373 print(get_luatexrevision());
3374 break;
3375 case etex_code:
3376 tprint(eTeX_version_string);
3377 break;
3378 case eTeX_revision_code:
3379 tprint(eTeX_revision);
3380 break;
3381 case font_identifier_code:
3382 print_font_identifier(i);
3383 break;
3384 /*tex Backend: this might become obsolete! */
3385 case dvi_feedback_code:
3386 done = the_convert_string_dvi(c,i);
3387 break;
3388 case pdf_feedback_code:
3389 done = the_convert_string_pdf(c,i);
3390 break;
3391 /*tex done */
3392 default:
3393 done = false;
3394 break;
3395 }
3396 if (done) {
3397 ret = make_string();
3398 }
3399 selector = old_setting;
3400 return ret;
3401 }
3402
3403 /*tex
3404
3405 Another way to create a token list is via the \.{\\read} command. The sixteen
3406 files potentially usable for reading appear in the following global
3407 variables. The value of |read_open[n]| will be |closed| if stream number |n|
3408 has not been opened or if it has been fully read; |just_open| if an
3409 \.{\\openin} but not a \.{\\read} has been done; and |normal| if it is open
3410 and ready to read the next line.
3411
3412 */
3413
3414 /*tex used for \.{\\read} */
3415
3416 FILE *read_file[16];
3417
3418 /*tex state of |read_file[n]| */
3419
3420 int read_open[17];
3421
3422 void initialize_read(void)
3423 {
3424 int k;
3425 for (k = 0; k <= 16; k++)
3426 read_open[k] = closed;
3427 }
3428
3429 /*tex
3430
3431 The |read_toks| procedure constructs a token list like that for any macro
3432 definition, and makes |cur_val| point to it. Parameter |r| points to the
3433 control sequence that will receive this token list.
3434
3435 */
3436
3437 void read_toks(int n, halfword r, halfword j)
3438 {
3439 /*tex tail of the token list */
3440 halfword p;
3441 /*tex new node being added to the token list via |store_new_token| */
3442 halfword q;
3443 /*tex saved value of |align_state| */
3444 int s;
3445 /*tex stream number */
3446 int m;
3447 scanner_status = defining;
3448 warning_index = r;
3449 p = get_avail();
3450 def_ref = p;
3451 set_token_ref_count(def_ref, 0);
3452 /*tex the reference count */
3453 p = def_ref;
3454 store_new_token(end_match_token);
3455 if ((n < 0) || (n > 15))
3456 m = 16;
3457 else
3458 m = n;
3459 s = align_state;
3460 /*tex disable tab marks, etc. */
3461 align_state = 1000000;
3462 do {
3463 /*tex Input and store tokens from the next line of the file. */
3464 begin_file_reading();
3465 iname = m + 1;
3466 if (read_open[m] == closed) {
3467 /*tex
3468
3469 Input for \.{\\read} from the terminal. We input on-line into the
3470 |buffer| array, prompting the user explicitly if |n>=0|. The
3471 value of |n| is set negative so that additional prompts will not
3472 be given in the case of multi-line input.
3473
3474 */
3475 if (interaction > nonstop_mode) {
3476 if (n < 0) {
3477 prompt_input("");
3478 } else {
3479 wake_up_terminal();
3480 print_ln();
3481 sprint_cs(r);
3482 prompt_input(" =");
3483 n = -1;
3484 }
3485 } else {
3486 /*tex initialize limit to avoid printing some garbage. */
3487 ilimit=0;
3488 fatal_error
3489 ("*** (cannot \\read from terminal in nonstop modes)");
3490 }
3491
3492 } else if (read_open[m] == just_open) {
3493 /*tex
3494
3495 Input the first line of |read_file[m]|. The first line of a file
3496 must be treated specially, since |lua_input_ln| must be told not
3497 to start with |get|.
3498
3499 */
3500 if (lua_input_ln(read_file[m], (m + 1), false)) {
3501 read_open[m] = normal;
3502 } else {
3503 lua_a_close_in(read_file[m], (m + 1));
3504 read_open[m] = closed;
3505 }
3506
3507 } else {
3508 /*tex
3509
3510 Input the next line of |read_file[m]|. An empty line is appended
3511 at the end of a |read_file|.
3512
3513 */
3514 if (!lua_input_ln(read_file[m], (m + 1), true)) {
3515 lua_a_close_in(read_file[m], (m + 1));
3516 read_open[m] = closed;
3517 if (align_state != 1000000) {
3518 runaway();
3519 print_err("File ended within \\read");
3520 help1("This \\read has unbalanced braces.");
3521 align_state = 1000000;
3522 error();
3523 }
3524 }
3525
3526 }
3527 ilimit = last;
3528 if (end_line_char_inactive)
3529 decr(ilimit);
3530 else
3531 buffer[ilimit] = (packed_ASCII_code) end_line_char_par;
3532 first = ilimit + 1;
3533 iloc = istart;
3534 istate = new_line;
3535 /*tex Handle \.{\\readline} and |goto done|. */
3536 if (j == 1) {
3537 while (iloc <= ilimit) {
3538 /*tex Current line not yet finished. */
3539 do_buffer_to_unichar(cur_chr, iloc);
3540 if (cur_chr == ' ')
3541 cur_tok = space_token;
3542 else
3543 cur_tok = cur_chr + other_token;
3544 store_new_token(cur_tok);
3545 }
3546 } else {
3547 while (1) {
3548 get_token();
3549 if (cur_tok == 0) {
3550 /*tex |cur_cmd=cur_chr=0| will occur at the end of the line. */
3551 break;
3552 }
3553 if (align_state < 1000000) {
3554 /*tex Unmatched right brace aborts the line. */
3555 do {
3556 get_token();
3557 } while (cur_tok != 0);
3558 align_state = 1000000;
3559 break;
3560 }
3561 store_new_token(cur_tok);
3562 }
3563 }
3564 end_file_reading();
3565
3566 } while (align_state != 1000000);
3567 cur_val = def_ref;
3568 scanner_status = normal;
3569 align_state = s;
3570 }
3571
3572 /*tex Return a string from tokens list: */
3573
3574 str_number tokens_to_string(halfword p)
3575 {
3576 int old_setting;
3577 if (selector == new_string)
3578 normal_error("tokens","tokens_to_string() called while selector = new_string");
3579 old_setting = selector;
3580 selector = new_string;
3581 show_token_list(token_link(p), null, -1);
3582 selector = old_setting;
3583 return make_string();
3584 }
3585
3586 /*tex
3587
3588 Values like 512 and 128 also work ok. There is not much to gain in
3589 optimization here.
3590
3591 */
3592
3593 #define alloci_default 1024
3594 #define alloci_step 256
3595
3596 #define make_room(a) \
3597 if ((unsigned)i+a+1>alloci) { \
3598 ret = xrealloc(ret,(alloci+alloci_step)); \
3599 alloci = alloci + alloci_step; \
3600 }
3601
3602 #define append_i_byte(a) ret[i++] = (char)(a)
3603
3604 #define Print_char(a) make_room(1); append_i_byte(a)
3605
3606 #define Print_uchar(s) { \
3607 make_room(4); \
3608 if (s<=0x7F) { \
3609 append_i_byte(s); \
3610 } else if (s<=0x7FF) { \
3611 append_i_byte(0xC0 + (s / 0x40)); \
3612 append_i_byte(0x80 + (s % 0x40)); \
3613 } else if (s<=0xFFFF) { \
3614 append_i_byte(0xE0 + (s / 0x1000)); \
3615 append_i_byte(0x80 + ((s % 0x1000) / 0x40)); \
3616 append_i_byte(0x80 + ((s % 0x1000) % 0x40)); \
3617 } else if (s>=0x110000) { \
3618 append_i_byte(s-0x11000); \
3619 } else { \
3620 append_i_byte(0xF0 + (s / 0x40000)); \
3621 append_i_byte(0x80 + ((s % 0x40000) / 0x1000)); \
3622 append_i_byte(0x80 + (((s % 0x40000) % 0x1000) / 0x40)); \
3623 append_i_byte(0x80 + (((s % 0x40000) % 0x1000) % 0x40)); \
3624 } }
3625
3626 #define Print_esc(b) { \
3627 const char *v = b; \
3628 if (e>0 && e<STRING_OFFSET) { \
3629 Print_uchar (e); \
3630 } \
3631 make_room(strlen(v)); \
3632 while (*v) { append_i_byte(*v); v++; } \
3633 }
3634
3635 #define Print_str(b) { \
3636 const char *v = b; \
3637 make_room(strlen(v)); \
3638 while (*v) { append_i_byte(*v); v++; } \
3639 }
3640
3641 #define is_cat_letter(a) \
3642 (get_char_cat_code(pool_to_unichar(str_string((a)))) == 11)
3643
3644 /*tex
3645
3646 The actual token conversion in this function is now functionally equivalent
3647 to |show_token_list|, except that it always prints the whole token list.
3648 TODO: check whether this causes problems in the lua library.
3649
3650 */
3651
3652 char *tokenlist_to_cstring(int pp, int inhibit_par, int *siz)
3653 {
3654 register int p, c, m;
3655 int q;
3656 int infop;
3657 char *s, *sh;
3658 int e = 0;
3659 char *ret;
3660 int match_chr = '#';
3661 int n = '0';
3662 unsigned alloci = alloci_default;
3663 int i = 0;
3664 p = pp;
3665 if (p == null) {
3666 if (siz != NULL)
3667 *siz = 0;
3668 return NULL;
3669 }
3670 ret = xmalloc(alloci);
3671 /*tex Skip refcount. */
3672 p = token_link(p);
3673 if (p != null) {
3674 e = escape_char_par;
3675 }
3676 while (p != null) {
3677 if (p < (int) fix_mem_min || p > (int) fix_mem_end) {
3678 Print_esc("CLOBBERED.");
3679 break;
3680 }
3681 infop = token_info(p);
3682 if (infop >= cs_token_flag) {
3683 if (!(inhibit_par && infop == par_token)) {
3684 q = infop - cs_token_flag;
3685 if (q < hash_base) {
3686 if (q == null_cs) {
3687 Print_esc("csname");
3688 Print_esc("endcsname");
3689 } else {
3690 Print_esc("IMPOSSIBLE.");
3691 }
3692 } else if ((q >= undefined_control_sequence) && ((q <= eqtb_size) || (q > eqtb_size + hash_extra))) {
3693 Print_esc("IMPOSSIBLE.");
3694 } else if ((cs_text(q) < 0) || (cs_text(q) >= str_ptr)) {
3695 Print_esc("NONEXISTENT.");
3696 } else {
3697 str_number txt = cs_text(q);
3698 sh = makecstring(txt);
3699 s = sh;
3700 if (is_active_cs(txt)) {
3701 s = s + 3;
3702 while (*s) {
3703 Print_char(*s);
3704 s++;
3705 }
3706 } else {
3707 if (e>=0 && e<0x110000) Print_uchar(e);
3708 while (*s) {
3709 Print_char(*s);
3710 s++;
3711 }
3712 if ((!single_letter(txt)) || is_cat_letter(txt)) {
3713 Print_char(' ');
3714 }
3715 }
3716 free(sh);
3717 }
3718 }
3719 } else {
3720 if (infop < 0) {
3721 Print_esc("BAD");
3722 } else {
3723 m = token_cmd(infop);
3724 c = token_chr(infop);
3725 switch (m) {
3726 case left_brace_cmd:
3727 case right_brace_cmd:
3728 case math_shift_cmd:
3729 case tab_mark_cmd:
3730 case sup_mark_cmd:
3731 case sub_mark_cmd:
3732 case spacer_cmd:
3733 case letter_cmd:
3734 case other_char_cmd:
3735 Print_uchar(c);
3736 break;
3737 case mac_param_cmd:
3738 if (!in_lua_escape && (is_in_csname==0))
3739 Print_uchar(c);
3740 Print_uchar(c);
3741 break;
3742 case out_param_cmd:
3743 Print_uchar(match_chr);
3744 if (c <= 9) {
3745 Print_char(c + '0');
3746 } else {
3747 Print_char('!');
3748 goto EXIT;
3749 }
3750 break;
3751 case match_cmd:
3752 match_chr = c;
3753 Print_uchar(c);
3754 n++;
3755 Print_char(n);
3756 if (n > '9')
3757 goto EXIT;
3758 break;
3759 case end_match_cmd:
3760 if (c == 0) {
3761 Print_char('-');
3762 Print_char('>');
3763 }
3764 break;
3765 default:
3766 not_so_bad(Print_esc);
3767 break;
3768 }
3769 }
3770 }
3771 p = token_link(p);
3772 }
3773 EXIT:
3774 ret[i] = '\0';
3775 if (siz != NULL)
3776 *siz = i;
3777 return ret;
3778 }
3779
3780 char *tokenlist_to_xstring(int pp, int inhibit_par, int *siz)
3781 {
3782 register int p, c, m;
3783 int q;
3784 int infop;
3785 char *s, *sh;
3786 int e = 0;
3787 char *ret;
3788 int match_chr = '#';
3789 int n = '0';
3790 unsigned alloci = alloci_default;
3791 int i = 0;
3792 int skipping = 1;
3793 p = pp;
3794 if (p == null) {
3795 if (siz != NULL)
3796 *siz = 0;
3797 return NULL;
3798 }
3799 ret = xmalloc(alloci);
3800 /*tex Skip refcount. */
3801 p = token_link(p);
3802 if (p != null) {
3803 e = escape_char_par;
3804 }
3805 while (p != null) {
3806 if (p < (int) fix_mem_min || p > (int) fix_mem_end) {
3807 /*tex Nothing done. */
3808 break;
3809 }
3810 infop = token_info(p);
3811 if (infop >= cs_token_flag) {
3812 if (!(inhibit_par && infop == par_token)) {
3813 q = infop - cs_token_flag;
3814 if (q < hash_base) {
3815 /*tex Nothing done. */
3816 } else if ((q >= undefined_control_sequence) && ((q <= eqtb_size) || (q > eqtb_size + hash_extra))) {
3817 /*tex Nothing done. */
3818 } else if ((cs_text(q) < 0) || (cs_text(q) >= str_ptr)) {
3819 /*tex Nothing done. */
3820 } else if (!skipping) {
3821 str_number txt = cs_text(q);
3822 sh = makecstring(txt);
3823 s = sh;
3824 if (is_active_cs(txt)) {
3825 s = s + 3;
3826 while (*s) {
3827 Print_char(*s);
3828 s++;
3829 }
3830 } else {
3831 if (e>=0 && e<0x110000) Print_uchar(e);
3832 while (*s) {
3833 Print_char(*s);
3834 s++;
3835 }
3836 if ((!single_letter(txt)) || is_cat_letter(txt)) {
3837 Print_char(' ');
3838 }
3839 }
3840 free(sh);
3841 }
3842 }
3843 } else {
3844 if (infop < 0) {
3845 /*tex Nothing done. */
3846 } else {
3847 m = token_cmd(infop);
3848 c = token_chr(infop);
3849 switch (m) {
3850 case left_brace_cmd:
3851 case right_brace_cmd:
3852 case math_shift_cmd:
3853 case tab_mark_cmd:
3854 case sup_mark_cmd:
3855 case sub_mark_cmd:
3856 case spacer_cmd:
3857 case letter_cmd:
3858 case other_char_cmd:
3859 if (!skipping) {
3860 Print_uchar(c);
3861 }
3862 break;
3863 case mac_param_cmd:
3864 if (!skipping) {
3865 if (!in_lua_escape && (is_in_csname==0))
3866 Print_uchar(c);
3867 Print_uchar(c);
3868 }
3869 break;
3870 case out_param_cmd:
3871 if (!skipping) {
3872 Print_uchar(match_chr);
3873 }
3874 if (c <= 9) {
3875 if (!skipping) {
3876 Print_char(c + '0');
3877 }
3878 } else {
3879 /*tex Nothing done. */
3880 goto EXIT;
3881 }
3882 break;
3883 case match_cmd:
3884 match_chr = c;
3885 if (!skipping) {
3886 Print_uchar(c);
3887 }
3888 n++;
3889 if (!skipping) {
3890 Print_char(n);
3891 }
3892 if (n > '9')
3893 goto EXIT;
3894 break;
3895 case end_match_cmd:
3896 if (c == 0) {
3897 if (!skipping) {
3898 Print_char('-');
3899 Print_char('>');
3900 }
3901 i = 0;
3902 skipping = 0 ;
3903 }
3904 break;
3905 default:
3906 /*tex Nothing done. */
3907 break;
3908 }
3909 }
3910 }
3911 p = token_link(p);
3912 }
3913 EXIT:
3914 ret[i] = '\0';
3915 if (siz != NULL)
3916 *siz = i;
3917 return ret;
3918 }
3919
3920 lstring *tokenlist_to_lstring(int pp, int inhibit_par)
3921 {
3922 int siz;
3923 lstring *ret = xmalloc(sizeof(lstring));
3924 ret->s = (unsigned char *) tokenlist_to_cstring(pp, inhibit_par, &siz);
3925 ret->l = (size_t) siz;
3926 return ret;
3927 }
3928
3929 void free_lstring(lstring * ls)
3930 {
3931 if (ls == NULL)
3932 return;
3933 if (ls->s != NULL)
3934 free(ls->s);
3935 free(ls);
3936 }