Debian Codegraph @

texk/web2c/luatexdir/tex/inputstack.c

raw
1 /*
2
3 inputstack.w
4
5 Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25
26 in_state_record *input_stack = NULL;
27
28 /*tex First unused location of |input_stack|: */
29
30 int input_ptr = 0;
31
32 /*tex Largest value of |input_ptr| when pushing: */
33
34 int max_in_stack = 0;
35
36 /*tex The `top' input state: */
37
38 in_state_record cur_input;
39
40 /*tex The number of lines in the buffer, less one: */
41
42 int in_open = 0;
43
44 /*tex The number of open text files: */
45
46 int open_parens = 0;
47
48 alpha_file *input_file = NULL;
49
50 /*tex The current line number in the current source file: */
51
52 int line = 0;
53
54 int *line_stack = NULL;
55
56 str_number *source_filename_stack = NULL;
57
58 char **full_source_filename_stack = NULL;
59
60 /*tex Can a subfile end now? */
61
62 int scanner_status = 0;
63
64 /*tex Identifier relevant to non-|normal| scanner status: */
65
66 pointer warning_index = null;
67
68 /*tex Reference count of token list being defined: */
69
70 pointer def_ref = null;
71
72 /*tex
73
74 Here is a procedure that uses |scanner_status| to print a warning message when a
75 subfile has ended, and at certain other crucial times:
76
77 */
78
79 void runaway(void)
80 {
81 /*tex The head of the runaway list: */
82 pointer p = null;
83 if (scanner_status > skipping) {
84 switch (scanner_status) {
85 case defining:
86 tprint_nl("Runaway definition");
87 p = def_ref;
88 break;
89 case matching:
90 tprint_nl("Runaway argument");
91 p = temp_token_head;
92 break;
93 case aligning:
94 tprint_nl("Runaway preamble");
95 p = hold_token_head;
96 break;
97 case absorbing:
98 tprint_nl("Runaway text");
99 p = def_ref;
100 break;
101 default:
102 /*tex There are no other cases. */
103 break;
104 }
105 print_char('?');
106 print_ln();
107 show_token_list(token_link(p), null, error_line - 10);
108 }
109 }
110
111 /*tex
112
113 The |param_stack| is an auxiliary array used to hold pointers to the token lists
114 for parameters at the current level and subsidiary levels of input. This stack is
115 maintained with convention (2), and it grows at a different rate from the others.
116
117 */
118
119 /*tex Token list pointers for parameters: */
120
121 pointer *param_stack = NULL;
122
123 /*tex First unused entry in |param_stack|: */
124
125 int param_ptr = 0;
126
127 /*tex Largest value of |param_ptr|, will be |<=param_size+9|: */
128
129 int max_param_stack = 0;
130
131 /*tex
132
133 The input routines must also interact with the processing of \.{\\halign} and
134 \.{\\valign}, since the appearance of tab marks and \.{\\cr} in certain places is
135 supposed to trigger the beginning of special $v_j$ template text in the scanner.
136 This magic is accomplished by an |align_state| variable that is increased by~1
137 when a `\.{\char'173}' is scanned and decreased by~1 when a `\.{\char'175}' is
138 scanned. The |align_state| is nonzero during the $u_j$ template, after which it
139 is set to zero; the $v_j$ template begins when a tab mark or \.{\\cr} occurs at a
140 time that |align_state=0|.
141
142 */
143
144 /*tex The group level with respect to current alignment: */
145
146 int align_state = 0;
147
148 /*tex
149
150 Thus, the ``current input state'' can be very complicated indeed; there can be
151 many levels and each level can arise in a variety of ways. The |show_context|
152 procedure, which is used by \TeX's error-reporting routine to print out the
153 current input state on all levels down to the most recent line of characters from
154 an input file, illustrates most of these conventions. The global variable
155 |base_ptr| contains the lowest level that was displayed by this procedure.
156
157 */
158
159 /*tex The shallowest level shown by |show_context|: */
160
161 int base_ptr = 0;
162
163 /*tex
164
165 The status at each level is indicated by printing two lines, where the first line
166 indicates what was read so far and the second line shows what remains to be read.
167 The context is cropped, if necessary, so that the first line contains at most
168 |half_error_line| characters, and the second contains at most |error_line|.
169 Non-current input levels whose |token_type| is `|backed_up|' are shown only if
170 they have not been fully read.
171
172 */
173
174 static void print_token_list_type(int t)
175 {
176 switch (t) {
177 case parameter:
178 tprint_nl("<argument> ");
179 break;
180 case u_template:
181 case v_template:
182 tprint_nl("<template> ");
183 break;
184 case backed_up:
185 if (iloc == null)
186 tprint_nl("<recently read> ");
187 else
188 tprint_nl("<to be read again> ");
189 break;
190 case inserted:
191 tprint_nl("<inserted text> ");
192 break;
193 case macro:
194 print_ln();
195 print_cs(iname);
196 break;
197 case output_text:
198 tprint_nl("<output> ");
199 break;
200 case every_par_text:
201 tprint_nl("<everypar> ");
202 break;
203 case every_math_text:
204 tprint_nl("<everymath> ");
205 break;
206 case every_display_text:
207 tprint_nl("<everydisplay> ");
208 break;
209 case every_hbox_text:
210 tprint_nl("<everyhbox> ");
211 break;
212 case every_vbox_text:
213 tprint_nl("<everyvbox> ");
214 break;
215 case every_job_text:
216 tprint_nl("<everyjob> ");
217 break;
218 case every_cr_text:
219 tprint_nl("<everycr> ");
220 break;
221 case mark_text:
222 tprint_nl("<mark> ");
223 break;
224 case every_eof_text:
225 tprint_nl("<everyeof> ");
226 break;
227 case write_text:
228 tprint_nl("<write> ");
229 break;
230 case local_text:
231 tprint_nl("<local> ");
232 break;
233 default:
234 tprint_nl("?");
235 /*tex This should never happen. */
236 break;
237 }
238 }
239
240 /*tex
241
242 Here it is necessary to explain a little trick. We don't want to store a long
243 string that corresponds to a token list, because that string might take up lots
244 of memory; and we are printing during a time when an error message is being
245 given, so we dare not do anything that might overflow one of \TeX's tables. So
246 `pseudoprinting' is the answer: We enter a mode of printing that stores
247 characters into a buffer of length |error_line|, where character $k+1$ is placed
248 into \hbox{|trick_buf[k mod error_line]|} if |k<trick_count|, otherwise character
249 |k| is dropped. Initially we set |tally:=0| and |trick_count:=1000000|; then when
250 we reach the point where transition from line 1 to line 2 should occur, we set
251 |first_count:=tally| and |trick_count:=@tmax@>(error_line,
252 tally+1+error_line-half_error_line)|. At the end of the pseudoprinting, the
253 values of |first_count|, |tally|, and |trick_count| give us all the information
254 we need to print the two lines, and all of the necessary text is in |trick_buf|.
255
256 Namely, let |l| be the length of the descriptive information that appears on the
257 first line. The length of the context information gathered for that line is
258 |k=first_count|, and the length of the context information gathered for line~2 is
259 $m=\min(|tally|, |trick_count|)-k$. If |l+k<=h|, where |h=half_error_line|, we
260 print |trick_buf[0..k-1]| after the descriptive information on line~1, and set
261 |n:=l+k|; here |n| is the length of line~1. If $l+k>h$, some cropping is
262 necessary, so we set |n:=h| and print `\.{...}' followed by
263 $$\hbox{|trick_buf[(l+k-h+3)..k-1]|,}$$ where subscripts of |trick_buf| are
264 circular modulo |error_line|. The second line consists of |n|~spaces followed by
265 |trick_buf[k..(k+m-1)]|, unless |n+m>error_line|; in the latter case, further
266 cropping is done. This is easier to program than to explain.
267
268 The following code sets up the print routines so that they will gather the
269 desired information.
270
271 */
272
273 void set_trick_count(void)
274 {
275 first_count = tally;
276 trick_count = tally + 1 + error_line - half_error_line;
277 if (trick_count < error_line)
278 trick_count = error_line;
279 }
280
281 #define begin_pseudoprint() do { \
282 l=tally; \
283 tally=0; \
284 selector=pseudo; \
285 trick_count=1000000; \
286 } while (0)
287
288 #define PSEUDO_PRINT_THE_LINE() do { \
289 begin_pseudoprint(); \
290 if (buffer[ilimit]==end_line_char_par) { \
291 j=ilimit; \
292 } else { \
293 /*tex Determine the effective end of the line. */ \
294 j=ilimit+1; \
295 } \
296 if (j>0) { \
297 for (i=istart;i<=j-1;i++) { \
298 if (i==iloc) \
299 set_trick_count(); \
300 print_char(buffer[i]); \
301 } \
302 } \
303 } while (0)
304
305 /*tex
306
307 We don't care too much if we stay a bit too much below the max error_line even if
308 we have more room on the line. If length is really an issue then any length is.
309 After all one can set the length larger.
310
311 */
312
313 #define print_valid_utf8(q) do { \
314 c = (int)trick_buf[q % error_line]; \
315 if (c < 128) { \
316 print_char(c); \
317 } else if (c < 194) { \
318 /* invalid */ \
319 } else if (c < 224) { \
320 print_char(c); \
321 print_char(trick_buf[(q+1) % error_line]); \
322 } else if (c < 240) { \
323 print_char(c); \
324 print_char(trick_buf[(q+1) % error_line]); \
325 print_char(trick_buf[(q+2) % error_line]); \
326 } else if (c < 245) { \
327 print_char(c); \
328 print_char(trick_buf[(q+1) % error_line]); \
329 print_char(trick_buf[(q+2) % error_line]); \
330 print_char(trick_buf[(q+3) % error_line]); \
331 } else { \
332 /*tex Invalid character! */ \
333 } \
334 } while (0)
335
336 /*tex
337
338 This one prints where the scanner is.
339
340 */
341
342 void show_context(void)
343 {
344 /*tex Saved |selector| setting: */
345 int old_setting;
346 /*tex Number of contexts shown so far, less one: */
347 int nn = -1;
348 /*tex Have we reached the final context to be shown? */
349 boolean bottom_line = false;
350 /*tex Index into |buffer|: */
351 int i;
352 /*tex End of current line in |buffer|: */
353 int j;
354 /*tex Length of descriptive information on line 1: */
355 int l;
356 /*tex Context information gathered for line 2: */
357 int m;
358 /*tex Length of line 1: */
359 int n;
360 /*tex Starting or ending place in |trick_buf|: */
361 int p;
362 /*tex Temporary index: */
363 int q;
364 /*tex Used in sanitizer: */
365 int c;
366 base_ptr = input_ptr;
367 input_stack[base_ptr] = cur_input;
368 /*tex Store the current state. */
369 while (true) {
370 /*tex Enter into the context. */
371 cur_input = input_stack[base_ptr];
372 if (istate != token_list) {
373 if ((iname > 21) || (base_ptr == 0))
374 bottom_line = true;
375 }
376 if ((base_ptr == input_ptr) || bottom_line || (nn < error_context_lines_par)) {
377 /*tex Display the current context. */
378 if ((base_ptr == input_ptr) || (istate != token_list) || (token_type != backed_up) || (iloc != null)) {
379 /*tex
380 We omit backed-up token lists that have already been read.
381 Get ready to count characters.
382 */
383 tally = 0;
384 old_setting = selector;
385 if (istate != token_list) {
386 /*tex
387 Print the location of the current line. This routine
388 should be changed, if necessary, to give the best
389 possible indication of where the current line resides in
390 the input file. For example, on some systems it is best
391 to print both a page and line number.
392 */
393 if (iname <= 17) {
394 if (terminal_input) {
395 if (base_ptr == 0)
396 tprint_nl("<*>");
397 else
398 tprint_nl("<insert> ");
399 } else {
400 tprint_nl("<read ");
401 if (iname == 17)
402 print_char('*');
403 else
404 print_int(iname - 1);
405 print_char('>');
406 };
407 } else {
408 tprint_nl("l.");
409 if (iindex == in_open) {
410 print_int(line);
411 } else {
412 /*tex Input from a pseudo file. */
413 print_int(line_stack[iindex + 1]);
414 }
415 }
416 print_char(' ');
417 PSEUDO_PRINT_THE_LINE();
418 } else {
419 print_token_list_type(token_type);
420
421 begin_pseudoprint();
422 if (token_type < macro) {
423 show_token_list(istart, iloc, 100000);
424 } else {
425 /*tex Avoid reference count. */
426 show_token_list(token_link(istart), iloc, 100000);
427 }
428 }
429 /*tex Stop pseudoprinting. */
430 selector = old_setting;
431 /*tex Print two lines using the tricky pseudoprinted information. */
432 if (trick_count == 1000000) {
433 set_trick_count();
434 }
435 /*tex The |set_trick_count| must be performed. */
436 if (tally < trick_count) {
437 m = tally - first_count;
438 } else {
439 /* The context on line 2: */
440 m = trick_count - first_count;
441 }
442 if (l + first_count <= half_error_line) {
443 p = 0;
444 n = l + first_count;
445 } else {
446 tprint("...");
447 p = l + first_count - half_error_line + 3;
448 n = half_error_line;
449 }
450 for (q = p; q <= first_count - 1; q++) {
451 print_valid_utf8(q);
452 }
453 print_ln();
454 /*tex Print |n| spaces to begin line 2. */
455 for (q = 1; q <= n; q++) {
456 print_char(' ');
457 }
458 if (m + n <= error_line)
459 p = first_count + m;
460 else
461 p = first_count + (error_line - n - 3);
462 for (q = first_count; q <= p - 1; q++)
463 print_valid_utf8(q);
464 if (m + n > error_line)
465 tprint("...");
466 incr(nn);
467 }
468 } else if (nn == error_context_lines_par) {
469 tprint_nl("...");
470 incr(nn);
471 /*tex Omitted if |error_context_lines_par<0|. */
472 }
473 if (bottom_line)
474 break;
475 decr(base_ptr);
476 }
477 /*tex Restore the original state. */
478 cur_input = input_stack[input_ptr];
479 }
480
481 /*tex
482
483 The following subroutines change the input status in commonly needed ways.
484
485 First comes |push_input|, which stores the current state and creates a
486 new level (having, initially, the same properties as the old).
487
488 Enter a new input level, save the old:
489
490 */
491
492 # define pop_input() \
493 cur_input=input_stack[--input_ptr]
494
495 # define push_input() \
496 if (input_ptr > max_in_stack) { \
497 max_in_stack = input_ptr; \
498 if (input_ptr == stack_size) \
499 overflow("input stack size", (unsigned) stack_size); \
500 } \
501 input_stack[input_ptr] = cur_input; \
502 nofilter = false; \
503 incr(input_ptr);
504
505 /*tex
506
507 Here is a procedure that starts a new level of token-list input, given a token
508 list |p| and its type |t|. If |t=macro|, the calling routine should set |name|
509 and |loc|.
510
511 */
512
513 void begin_token_list(halfword p, quarterword t)
514 {
515 push_input();
516 istate = token_list;
517 istart = p;
518 token_type = (unsigned char) t;
519 if (t >= macro) {
520 /*tex The token list starts with a reference count. */
521 add_token_ref(p);
522 if (t == macro) {
523 param_start = param_ptr;
524 } else {
525 iloc = token_link(p);
526 if (tracing_macros_par > 1) {
527 begin_diagnostic();
528 // tprint_nl("");
529 print_input_level();
530 if (t == mark_text)
531 tprint_esc("mark");
532 else if (t == write_text)
533 tprint_esc("write");
534 else
535 print_cmd_chr(assign_toks_cmd, t - output_text + output_routine_loc);
536 tprint("->");
537 token_show(p);
538 end_diagnostic(false);
539 }
540 }
541 } else {
542 iloc = p;
543 }
544 }
545
546 /*tex
547
548 When a token list has been fully scanned, the following computations should be
549 done as we leave that level of input. The |token_type| tends to be equal to
550 either |backed_up| or |inserted| about 2/3 of the time. @^inner loop@>
551
552 */
553
554 void end_token_list(void)
555 {
556 /*tex Leave a token-list input level: */
557 if (token_type >= backed_up) {
558 /*tex The token list to be deleted: */
559 if (token_type <= inserted) {
560 flush_list(istart);
561 } else {
562 /*tex Update the reference count: */
563 delete_token_ref(istart);
564 if (token_type == macro) {
565 /*tex Parameters must be flushed: */
566 while (param_ptr > param_start) {
567 decr(param_ptr);
568 flush_list(param_stack[param_ptr]);
569 }
570 }
571 }
572 } else if (token_type == u_template) {
573 if (align_state > 500000)
574 align_state = 0;
575 else
576 fatal_error("(interwoven alignment preambles are not allowed)");
577 }
578 pop_input();
579 check_interrupt();
580 }
581
582 /*tex
583
584 Sometimes \TeX\ has read too far and wants to ``unscan'' what it has seen. The
585 |back_input| procedure takes care of this by putting the token just scanned back
586 into the input stream, ready to be read again. This procedure can be used only if
587 |cur_tok| represents the token to be replaced. Some applications of \TeX\ use
588 this procedure a lot, so it has been slightly optimized for speed. @^inner loop@>
589
590 */
591
592 /* undoes one token of input */
593
594 void back_input(void)
595 {
596 /*tex A token list of length one: */
597 halfword p;
598 while ((istate == token_list) && (iloc == null) && (token_type != v_template)) {
599 /*tex Conserve stack space. */
600 end_token_list();
601 }
602 p = get_avail();
603 set_token_info(p, cur_tok);
604 if (cur_tok < right_brace_limit) {
605 if (cur_tok < left_brace_limit)
606 decr(align_state);
607 else
608 incr(align_state);
609 }
610 push_input();
611 /*tex This is |back_list(p)|, without procedure overhead: */
612 istate = token_list;
613 istart = p;
614 token_type = backed_up;
615 iloc = p;
616 }
617
618 /*tex
619
620 Insert token |p| into \TeX's input
621
622 */
623
624 void reinsert_token(boolean a, halfword pp)
625 {
626 halfword t;
627 t = cur_tok;
628 cur_tok = pp;
629 if (a) {
630 halfword p;
631 p = get_avail();
632 set_token_info(p, cur_tok);
633 set_token_link(p, iloc);
634 iloc = p;
635 istart = p;
636 if (cur_tok < right_brace_limit) {
637 if (cur_tok < left_brace_limit)
638 decr(align_state);
639 else
640 incr(align_state);
641 }
642 } else {
643 back_input();
644 }
645 cur_tok = t;
646 }
647
648 /*tex
649
650 The |begin_file_reading| procedure starts a new level of input for lines of
651 characters to be read from a file, or as an insertion from the terminal. It does
652 not take care of opening the file, nor does it set |loc| or |limit| or |line|.
653 @^system dependencies@>
654
655 */
656
657 void begin_file_reading(void)
658 {
659 if (in_open == max_in_open)
660 overflow("text input levels", (unsigned) max_in_open);
661 if (first == buf_size)
662 check_buffer_overflow(first);
663 incr(in_open);
664 push_input();
665 iindex = (unsigned short)(in_open);
666 source_filename_stack[iindex] = 0;
667 full_source_filename_stack[iindex] = NULL;
668 eof_seen[iindex] = false;
669 grp_stack[iindex] = cur_boundary;
670 if_stack[iindex] = cond_ptr;
671 line_stack[iindex] = line;
672 istart = first;
673 istate = mid_line;
674 iname = 0;
675 /*tex Variable |terminal_input| is now |true|. */
676 line_catcode_table = DEFAULT_CAT_TABLE;
677 line_partial = false;
678 /*tex Prepare terminal input \SYNCTEX\ information. */
679 synctex_tag = 0;
680 }
681
682 /*tex
683
684 Conversely, the variables must be downdated when such a level of input is
685 finished:
686
687 */
688
689 void end_file_reading(void)
690 {
691 first = istart;
692 line = line_stack[iindex];
693 if ((iname >= 18) && (iname <= 20)) {
694 pseudo_close();
695 } else if (iname == 21) {
696 luacstring_close(iindex);
697 } else if (iname > 17) {
698 /*tex Forget it. */
699 lua_a_close_in(cur_file, 0);
700 source_filename_stack[iindex] = 0;
701 if (full_source_filename_stack[iindex] != NULL) {
702 free(full_source_filename_stack[iindex]);
703 full_source_filename_stack[iindex] = NULL;
704 }
705 }
706 pop_input();
707 decr(in_open);
708 }
709
710 /*tex
711
712 In order to keep the stack from overflowing during a long sequence of
713 inserted `\.{\\show}' commands, the following routine removes completed
714 error-inserted lines from memory.
715
716 */
717 void clear_for_error_prompt(void)
718 {
719 while ((istate != token_list) && terminal_input && (input_ptr > 0) && (iloc > ilimit)) {
720 end_file_reading();
721 }
722 print_ln();
723 clear_terminal();
724 }
725
726 /*tex
727
728 To get \TeX's whole input mechanism going, we perform the following actions.
729
730 */
731
732 void initialize_inputstack(void)
733 {
734 input_ptr = 0;
735 max_in_stack = 0;
736 source_filename_stack[0] = 0;
737
738 full_source_filename_stack[0] = NULL;
739 in_open = 0;
740 open_parens = 0;
741 max_buf_stack = 0;
742
743 grp_stack[0] = 0;
744 if_stack[0] = null;
745 param_ptr = 0;
746 max_param_stack = 0;
747 first = buf_size;
748 do {
749 buffer[first] = 0;
750 decr(first);
751 } while (first != 0);
752 buffer[0] = 0;
753 scanner_status = normal;
754 warning_index = null;
755 first = 1;
756 istate = new_line;
757 istart = 1;
758 iindex = 0;
759 line = 0;
760 iname = 0;
761 nofilter = false;
762 force_eof = false;
763 luacstrings = 0;
764 line_catcode_table = DEFAULT_CAT_TABLE;
765 line_partial = false;
766 align_state = 1000000;
767 if (!init_terminal()) {
768 /*tex |goto final_end|; */
769 exit(EXIT_FAILURE);
770 }
771 /* |init_terminal| has set |loc| and |last| */
772 ilimit = last;
773 first = last + 1;
774 }
775
776 /*tex
777
778 The global variable |pseudo_files| is used to maintain a stack of pseudo files.
779 The |pseudo_lines| field of each pseudo file points to a linked list of variable
780 size nodes representing lines not yet processed: the |subtype| field contains the
781 size of this node, all the following words contain ASCII codes.
782
783 If this is really critical code (which it isn't) then we can consider a c stack
784 and store a pointer to a line in the line node instead which saves splitting here
785 and reconstructing later.
786
787 */
788
789 halfword pseudo_files;
790
791 static halfword string_to_pseudo(str_number str, int nl)
792 {
793 halfword i, r, q = null;
794 unsigned l, len;
795 four_quarters w;
796 int sz;
797 halfword h = new_node(pseudo_file_node, 0);
798 unsigned char *s = str_string(str);
799 len = (unsigned) str_length(str);
800 l = 0;
801 while (l < len) {
802 /*tex start of current line */
803 unsigned m = l;
804 while ((l < len) && (s[l] != nl))
805 l++;
806 sz = (int) (l - m + 7) / 4;
807 if (sz == 1)
808 sz = 2;
809 r = new_node(pseudo_line_node, sz);
810 i = r;
811 while (--sz > 1) {
812 w.b0 = s[m++];
813 w.b1 = s[m++];
814 w.b2 = s[m++];
815 w.b3 = s[m++];
816 varmem[++i].qqqq = w;
817 }
818 w.b0 = (quarterword) (l > m ? s[m++] : ' ');
819 w.b1 = (quarterword) (l > m ? s[m++] : ' ');
820 w.b2 = (quarterword) (l > m ? s[m++] : ' ');
821 w.b3 = (quarterword) (l > m ? s[m] : ' ');
822 varmem[++i].qqqq = w;
823 if (q == null) {
824 pseudo_lines(h) = r;
825 } else {
826 /*tex There is no |prev| node here so no need to couple_nodes! */
827 vlink(q) = r ;
828 }
829 q = r ;
830 if (s[l] == nl)
831 l++;
832 }
833 return h;
834 }
835
836 /*tex
837
838 The |pseudo_start| procedure initiates reading from a pseudo file.
839
840 */
841
842 void pseudo_from_string(void)
843 {
844 /*tex The string to be converted into a pseudo file: */
845 str_number s;
846 /*tex A helper for list construction: */
847 halfword p;
848 s = make_string();
849 /*tex Convert string |s| into a new pseudo file */
850 p = string_to_pseudo(s, new_line_char_par);
851 vlink(p) = pseudo_files;
852 pseudo_files = p;
853 flush_str(s);
854 /*tex
855 Initiate input from new pseudo file. It sets up |cur_file| and a new level
856 of input
857 */
858 begin_file_reading();
859 line = 0;
860 ilimit = istart;
861 /*tex force line read */
862 iloc = ilimit + 1;
863 if (tracing_scan_tokens_par > 0) {
864 if (term_offset > max_print_line - 3)
865 print_ln();
866 else if ((term_offset > 0) || (file_offset > 0))
867 print_char(' ');
868 iname = 20;
869 tprint("( ");
870 incr(open_parens);
871 update_terminal();
872 } else {
873 iname = 18;
874 }
875 /*tex Prepare pseudo file \SYNCTEX\ information. */
876 synctex_tag = 0;
877 }
878
879 void pseudo_start(void)
880 {
881 int old_setting;
882 scan_general_text();
883 old_setting = selector;
884 selector = new_string;
885 token_show(temp_token_head);
886 selector = old_setting;
887 flush_list(token_link(temp_token_head));
888 str_room(1);
889 pseudo_from_string();
890 }
891
892 void lua_string_start(void)
893 {
894 /*tex Set up |cur_file| and a new level of input: */
895 begin_file_reading();
896 line = 0;
897 ilimit = istart;
898 /*tex Force line read: */
899 iloc = ilimit + 1;
900 iname = 21;
901 luacstring_start(iindex);
902 }
903
904 /*tex
905
906 Here we read a line from the current pseudo file into |buffer|.
907 It inputs the next line or returns |false|.
908
909 */
910
911 boolean pseudo_input(void)
912 {
913 /*tex The current line from pseudo file: */
914 halfword p;
915 /*tex The size of node |p|: */
916 int sz;
917 /*tex Four ASCII codes: */
918 four_quarters w;
919 /*tex The loop index: */
920 halfword r;
921 /*tex cf.\ Matthew 19\thinspace:\thinspace30 */
922 last = first;
923 p = pseudo_lines(pseudo_files);
924 if (p == null) {
925 return false;
926 } else {
927 pseudo_lines(pseudo_files) = vlink(p);
928 sz = subtype(p);
929 if (4 * sz - 3 >= buf_size - last)
930 check_buffer_overflow(last + 4 * sz);
931 last = first;
932 for (r = p + 1; r <= p + sz - 1; r++) {
933 w = varmem[r].qqqq;
934 buffer[last] = (packed_ASCII_code) w.b0;
935 buffer[last + 1] = (packed_ASCII_code) w.b1;
936 buffer[last + 2] = (packed_ASCII_code) w.b2;
937 buffer[last + 3] = (packed_ASCII_code) w.b3;
938 last += 4;
939 }
940 if (last >= max_buf_stack)
941 max_buf_stack = last + 1;
942 while ((last > first) && (buffer[last - 1] == ' '))
943 decr(last);
944 flush_node(p);
945 }
946 return true;
947 }
948
949 /*tex
950
951 When we are done with a pseudo file we `close' it.
952
953 */
954
955 void pseudo_close(void)
956 {
957 halfword p;
958 p = vlink(pseudo_files);
959 flush_node(pseudo_files);
960 pseudo_files = p;
961 }