Debian Codegraph @

texk/web2c/luatexdir/tex/extensions.c

raw
1 /*
2
3 Copyright 2009-2010 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 mode mode_par
23 #define tail tail_par
24 #define head head_par
25 #define dir_save dirs_par
26
27 /*tex
28
29 The program above includes a bunch of ``hooks'' that allow further
30 capabilities to be added without upsetting \TeX's basic structure. Most of
31 these hooks are concerned with ``whatsit'' nodes, which are intended to be
32 used for special purposes; whenever a new extension to \TeX\ involves a new
33 kind of whatsit node, a corresponding change needs to be made to the routines
34 below that deal with such nodes, but it will usually be unnecessary to make
35 many changes to the other parts of this program.
36
37 In order to demonstrate how extensions can be made, we shall treat
38 `\.{\\write}', `\.{\\openout}', `\.{\\closeout}', `\.{\\immediate}', and
39 `\.{\\special}' as if they were extensions. These commands are actually
40 primitives of \TeX, and they should appear in all implementations of the
41 system; but let's try to imagine that they aren't. Then the program below
42 illustrates how a person could add them.
43
44 Sometimes, of course, an extension will require changes to \TeX\ itself; no
45 system of hooks could be complete enough for all conceivable extensions. The
46 features associated with `\.{\\write}' are almost all confined to the
47 following paragraphs, but there are small parts of the |print_ln| and
48 |print_char| procedures that were introduced specifically to \.{\\write}
49 characters. Furthermore one of the token lists recognized by the scanner is a
50 |write_text|; and there are a few other miscellaneous places where we have
51 already provided for some aspect of \.{\\write}. The goal of a \TeX\ extender
52 should be to minimize alterations to the standard parts of the program, and
53 to avoid them completely if possible. He or she should also be quite sure
54 that there's no easy way to accomplish the desired goals with the standard
55 features that \TeX\ already has. ``Think thrice before extending,'' because
56 that may save a lot of work, and it will also keep incompatible extensions of
57 \TeX\ from proliferating.
58
59 First let's consider the format of whatsit nodes that are used to represent
60 the data associated with \.{\\write} and its relatives. Recall that a whatsit
61 has |type=whatsit_node|, and the |subtype| is supposed to distinguish
62 different kinds of whatsits. Each node occupies two or more words; the exact
63 number is immaterial, as long as it is readily determined from the |subtype|
64 or other data.
65
66 We shall introduce five |subtype| values here, corresponding to the control
67 sequences \.{\\openout}, \.{\\write}, \.{\\closeout}, and \.{\\special}. The
68 second word of I/O whatsits has a |write_stream| field that identifies the
69 write-stream number (0 to 15, or 16 for out-of-range and positive, or 17 for
70 out-of-range and negative). In the case of \.{\\write} and \.{\\special},
71 there is also a field that points to the reference count of a token list that
72 should be sent. In the case of \.{\\openout}, we need three words and three
73 auxiliary subfields to hold the string numbers for name, area, and extension.
74
75 Extensions might introduce new command codes; but it's best to use
76 |extension| with a modifier, whenever possible, so that |main_control| stays
77 the same.
78
79 The sixteen possible \.{\\write} streams are represented by the |write_file|
80 array. The |j|th file is open if and only if |write_open[j]=true|. The last
81 two streams are special; |write_open[16]| represents a stream number greater
82 than 15, while |write_open[17]| represents a negative stream number, and both
83 of these variables are always |false|.
84
85 */
86
87 alpha_file write_file[last_file_selector+1];
88 halfword write_file_mode[last_file_selector+1];
89 halfword write_file_translation[last_file_selector+1];
90 boolean write_open[last_file_selector+1];
91
92 scaled neg_wd;
93 scaled pos_wd;
94 scaled neg_ht;
95
96 /*tex
97
98 The variable |write_loc| just introduced is used to provide an appropriate
99 error message in case of ``runaway'' write texts.
100
101 */
102
103 /*tex The |eqtb| address of \.{\\write}: */
104
105 halfword write_loc;
106
107 /*tex
108
109 When an |extension| command occurs in |main_control|, in any mode, the
110 |do_extension| routine is called.
111
112 */
113
114 int last_saved_image_index ;
115 int last_saved_image_pages ;
116 int last_saved_box_index ;
117 scaledpos last_position = { 0, 0 };
118
119 static void do_extension_dvi_literal(int deferred)
120 {
121 new_whatsit(deferred ? late_special_node : special_node);
122 write_stream(tail) = null;
123 scan_toks(false, ! deferred);
124 write_tokens(tail) = def_ref;
125 }
126
127 static void do_extension_dvi(int immediate, int deferred)
128 {
129 if (scan_keyword("literal")) {
130 /* immediate is the default here */
131 if (scan_keyword("shipout")) {
132 deferred = 1;
133 }
134 do_extension_dvi_literal(deferred);
135 } else if (scan_keyword("lateliteral")) {
136 do_extension_dvi_literal(1);
137 } else {
138 tex_error("unexpected use of \\dviextension",null);
139 }
140 }
141
142 static void do_extension_pdf_literal(int deferred)
143 {
144 new_whatsit(deferred ? pdf_late_literal_node : pdf_literal_node);
145 if (scan_keyword("direct"))
146 set_pdf_literal_mode(tail, direct_always);
147 else if (scan_keyword("page"))
148 set_pdf_literal_mode(tail, direct_page);
149 else if (scan_keyword("text"))
150 set_pdf_literal_mode(tail, direct_text);
151 else if (scan_keyword("raw"))
152 set_pdf_literal_mode(tail, direct_raw);
153 else if (scan_keyword("origin"))
154 set_pdf_literal_mode(tail, set_origin);
155 else
156 set_pdf_literal_mode(tail, set_origin);
157 scan_toks(false, ! deferred);
158 set_pdf_literal_type(tail, normal);
159 set_pdf_literal_data(tail, def_ref);
160 }
161
162 static void do_extension_pdf(int immediate, int deferred)
163 {
164 int i;
165 if (scan_keyword("literal")) {
166 /* immediate is the default here */
167 if (scan_keyword("shipout")) {
168 deferred = 1;
169 }
170 do_extension_pdf_literal(deferred);
171 } else if (scan_keyword("lateliteral")) {
172 do_extension_pdf_literal(1);
173 } else if (scan_keyword("dest")) {
174 scan_pdfdest(static_pdf);
175 } else if (scan_keyword("annot")) {
176 scan_annot(static_pdf);
177 } else if (scan_keyword("save")) {
178 new_whatsit(pdf_save_node);
179 } else if (scan_keyword("restore")) {
180 new_whatsit(pdf_restore_node);
181 } else if (scan_keyword("linkstate")) {
182 new_whatsit(pdf_link_state_node);
183 scan_int();
184 pdf_link_state(tail) = cur_val;
185 } else if (scan_keyword("setmatrix")) {
186 new_whatsit(pdf_setmatrix_node);
187 scan_toks(false, true);
188 set_pdf_setmatrix_data(tail, def_ref);
189 } else if (scan_keyword("obj")) {
190 scan_obj(static_pdf);
191 if (immediate) {
192 if (obj_data_ptr(static_pdf, pdf_last_obj) == 0) {
193 /*tex This object has not been initialized yet. */
194 normal_error("pdf backend","\\pdfextension obj 'reserveobjnum' cannot be used with \\immediate");
195 }
196 pdf_write_obj(static_pdf, pdf_last_obj);
197 }
198 } else if (scan_keyword("refobj")) {
199 scan_refobj(static_pdf);
200 } else if (scan_keyword("colorstack")) {
201 scan_int();
202 if (cur_val >= colorstackused()) {
203 print_err("Unknown color stack number ");
204 print_int(cur_val);
205 help3(
206 "Allocate and initialize a color stack with \\pdfextension colorstackinit.",
207 "I'll use default color stack 0 here.",
208 "Proceed, with fingers crossed."
209 );
210 error();
211 cur_val = 0;
212 }
213 if (cur_val < 0) {
214 print_err("Invalid negative color stack number");
215 help2(
216 "I'll use default color stack 0 here.",
217 "Proceed, with fingers crossed."
218 );
219 error();
220 cur_val = 0;
221 }
222 if (scan_keyword("set"))
223 i = colorstack_set;
224 else if (scan_keyword("push"))
225 i = colorstack_push;
226 else if (scan_keyword("pop"))
227 i = colorstack_pop;
228 else if (scan_keyword("current"))
229 i = colorstack_current;
230 else
231 i = -1;
232 if (i >= 0) {
233 new_whatsit(pdf_colorstack_node);
234 set_pdf_colorstack_stack(tail, cur_val);
235 set_pdf_colorstack_cmd(tail, i);
236 set_pdf_colorstack_data(tail, null);
237 if (i <= colorstack_data) {
238 scan_toks(false, true);
239 set_pdf_colorstack_data(tail, def_ref);
240 }
241 } else {
242 print_err("Color stack action is missing");
243 help3(
244 "The expected actions for \\pdfextension colorstack:",
245 " set, push, pop, current",
246 "I'll ignore the color stack command."
247 );
248 error();
249 }
250 } else if (scan_keyword("startlink")) {
251 scan_startlink(static_pdf);
252 } else if (scan_keyword("endlink")) {
253 if (abs(mode) == vmode)
254 normal_error("pdf backend", "\\pdfextension endlink cannot be used in vertical mode");
255 new_whatsit(pdf_end_link_node);
256 } else if (scan_keyword("startthread")) {
257 new_annot_whatsit(pdf_start_thread_node);
258 scan_thread_id();
259 } else if (scan_keyword("endthread")) {
260 new_whatsit(pdf_end_thread_node);
261 } else if (scan_keyword("thread")) {
262 new_annot_whatsit(pdf_thread_node);
263 scan_thread_id();
264 } else if (scan_keyword("outline")) {
265 scan_pdfoutline(static_pdf);
266 } else if (scan_keyword("glyphtounicode")) {
267 glyph_to_unicode();
268 } else if (scan_keyword("catalog")) {
269 scan_pdfcatalog(static_pdf);
270 } else if (scan_keyword("fontattr")) {
271 /*tex
272
273 The font attributes are simply initialized to zero now, this is
274 easier to deal with from C than an empty \TeX{} string, and surely
275 nobody will want to set font attr to a string containing a single
276 zero, as that would be nonsensical in the PDF output.
277
278 */
279 scan_font_ident();
280 i = cur_val;
281 if (i == null_font)
282 normal_error("pdf backend", "invalid font identifier");
283 scan_toks(false, true);
284 set_pdf_font_attr(i, tokens_to_string(def_ref));
285 if (str_length(pdf_font_attr(i)) == 0) {
286 /*tex From |tokens_to_string|. */
287 flush_str((str_ptr - 1));
288 set_pdf_font_attr(i, 0);
289 }
290 } else if (scan_keyword("mapfile")) {
291 scan_toks(false, true);
292 pdfmapfile(def_ref);
293 delete_token_ref(def_ref);
294 } else if (scan_keyword("mapline")) {
295 scan_toks(false, true);
296 pdfmapline(def_ref);
297 delete_token_ref(def_ref);
298 } else if (scan_keyword("includechars")) {
299 pdf_include_chars(static_pdf);
300 } else if (scan_keyword("info")) {
301 scan_toks(false, true);
302 pdf_info_toks = concat_tokens(pdf_info_toks, def_ref);
303 } else if (scan_keyword("names")) {
304 scan_toks(false, true);
305 pdf_names_toks = concat_tokens(pdf_names_toks, def_ref);
306 } else if (scan_keyword("trailer")) {
307 scan_toks(false, true);
308 pdf_trailer_toks = concat_tokens(pdf_trailer_toks, def_ref);
309 } else {
310 tex_error("unexpected use of \\pdfextension",null);
311 }
312 }
313
314 static void do_resource_dvi(int immediate, int code)
315 {
316 /*tex Nothing is done here. */
317 }
318
319 static void do_resource_pdf(int immediate, int code)
320 {
321 switch (code) {
322 case use_box_resource_code:
323 scan_pdfrefxform(static_pdf);
324 break;
325 case use_image_resource_code:
326 scan_pdfrefximage(static_pdf);
327 break;
328 case save_box_resource_code:
329 scan_pdfxform(static_pdf);
330 if (immediate) {
331 pdf_cur_form = last_saved_box_index;
332 ship_out(static_pdf, obj_xform_box(static_pdf, last_saved_box_index), SHIPPING_FORM);
333 }
334 break;
335 case save_image_resource_code:
336 scan_pdfximage(static_pdf);
337 if (immediate) {
338 pdf_write_image(static_pdf, last_saved_image_index);
339 }
340 break;
341 }
342 }
343
344 /*tex
345
346 Ad immediate:
347
348 To write a token list, we must run it through \TeX's scanner, expanding
349 macros and \.{\\the} and \.{\\number}, etc. This might cause runaways, if a
350 delimited macro parameter isn't matched, and runaways would be extremely
351 confusing since we are calling on \TeX's scanner in the middle of a
352 \.{\\shipout} command. Therefore we will put a dummy control sequence as a
353 ``stopper,'' right after the token list. This control sequence is
354 artificially defined to be \.{\\outer}.
355
356 The presence of `\.{\\immediate}' causes the |do_extension| procedure to
357 descend to one level of recursion. Nothing happens unless \.{\\immediate} is
358 followed by `\.{\\openout}', `\.{\\write}', or `\.{\\closeout}'.
359
360 */
361
362 /*tex
363
364 The extensions are backend related. The next subroutine uses |cur_chr| to
365 decide what sort of whatsit is involved, and also inserts a |write_stream|
366 number.
367
368 */
369
370 static void new_write_whatsit(int w, int check)
371 {
372 new_whatsit(cur_chr);
373 if (check) {
374 /*tex So we check with open and close. */
375 scan_limited_int(last_file_selector,NULL);
376 } else {
377 /*tex But we're tolerant with the rest. */
378 scan_int();
379 if (cur_val < 0)
380 cur_val = term_only;
381 else if (cur_val > last_file_selector) {
382 cur_val = term_and_log;
383 }
384 }
385 write_stream(tail) = cur_val;
386 }
387
388 /*
389 Specials and literals are immediate by default but can be deferred as well. We have two prefixes that
390 don't accumulate but trigger a next token pickup. Writes and resources are deferred by default. The
391 (wish for a) shipout key scanner comes from similar features in the other engines. So, we're
392 compatible but also conceptually a bit more consistent by introducing a |\deferred| prefix.
393 */
394
395 void do_extension(int immediate, int deferred)
396 {
397 /*tex All-purpose pointers. */
398 halfword k, p;
399 if (cur_cmd == extension_cmd) {
400 /*tex These have their own range starting at 0. */
401 switch (cur_chr) {
402 case open_code:
403 p = tail;
404 new_write_whatsit(open_node_size,1);
405 scan_optional_equals();
406 /* scan_file_name(); */
407 do {
408 get_x_token();
409 } while ((cur_cmd == spacer_cmd) || (cur_cmd == relax_cmd));
410 back_input();
411 if (cur_cmd != left_brace_cmd) {
412 scan_file_name();
413 } else {
414 scan_file_name_toks();
415 }
416 /* */
417 open_name(tail) = cur_name;
418 open_area(tail) = cur_area;
419 open_ext(tail) = cur_ext;
420 if (immediate) {
421 wrapup_leader(tail);
422 flush_node_list(tail);
423 tail = p;
424 vlink(p) = null;
425 }
426 break;
427 case write_code:
428 /*tex
429
430 When `\.{\\write 12\{...\}}' appears, we scan the token list
431 `\.{\{...\}}' without expanding its macros; the macros will
432 be expanded later when this token list is rescanned.
433
434 */
435 k = cur_cs;
436 p = tail;
437 new_write_whatsit(write_node_size,0); // this can modify cur_cs
438 cur_cs = k; // so we restore the prev. cur_cs, as in pdftex
439 scan_toks(false, false);
440 write_tokens(tail) = def_ref;
441 if (immediate) {
442 wrapup_leader(tail);
443 flush_node_list(tail);
444 tail = p;
445 vlink(p) = null;
446 }
447 break;
448 case close_code:
449 p = tail;
450 new_write_whatsit(close_node_size,1);
451 write_tokens(tail) = null;
452 if (immediate) {
453 wrapup_leader(tail);
454 flush_node_list(tail);
455 tail = p;
456 vlink(p) = null;
457 }
458 break;
459 case special_code:
460 /*tex
461
462 When `\.{\\special\{...\}}' appears, we expand the macros in
463 the token list as in \.{\\xdef} and \.{\\mark}. When marked with \.{shipout}, we keep
464 tokens unexpanded for now.
465
466 */
467 if (scan_keyword("shipout")) {
468 deferred = 1;
469 }
470 new_whatsit(deferred ? late_special_node : special_node);
471 write_stream(tail) = null;
472 p = scan_toks(false, ! deferred);
473 write_tokens(tail) = def_ref;
474 break;
475 case reserved_immediate_code:
476 get_x_token();
477 do_extension(1, 0);
478 break;
479 case reserved_deferred_code:
480 get_x_token();
481 do_extension(0, 1);
482 break;
483 case end_local_code:
484 if (tracing_nesting_par > 2) {
485 local_control_message("leaving token scanner");
486 }
487 end_local_control();
488 break;
489 case use_box_resource_code:
490 case use_image_resource_code:
491 case save_box_resource_code:
492 case save_image_resource_code:
493 switch (get_o_mode()) {
494 case OMODE_DVI:
495 do_resource_dvi(immediate,cur_chr);
496 break;
497 case OMODE_PDF:
498 do_resource_pdf(immediate,cur_chr);
499 break;
500 default:
501 break;
502 }
503 break;
504 /*tex Backend extensions have their own range starting at 32. */
505 case dvi_extension_code:
506 if (get_o_mode() == OMODE_DVI)
507 do_extension_dvi(immediate, deferred);
508 break;
509 case pdf_extension_code:
510 if (get_o_mode() == OMODE_PDF)
511 do_extension_pdf(immediate, deferred);
512 break;
513 /*tex Done. */
514 default:
515 if (immediate) {
516 back_input();
517 } else {
518 confusion("invalid extension");
519 }
520 break;
521 }
522 } else {
523 /*tex No extension command, quite certainly following |\immediate|. */
524 back_input();
525 }
526 }
527
528 /*tex
529
530 Here is a subroutine that creates a whatsit node having a given |subtype| and
531 a given number of words. It initializes only the first word of the whatsit,
532 and appends it to the current list.
533
534 */
535
536 void new_whatsit(int s)
537 {
538 halfword p = new_node(whatsit_node, s);
539 couple_nodes(tail, p);
540 tail = p;
541 }
542
543 /*tex
544
545 The final line of this routine is slightly subtle; at least, the author
546 didn't think about it until getting burnt! There is a used-up token list on
547 the stack, namely the one that contained |end_write_token|. (We insert this
548 artificial `\.{\\endwrite}' to prevent runaways, as explained above.) If it
549 were not removed, and if there were numerous writes on a single page, the
550 stack would overflow.
551
552 */
553
554 void expand_macros_in_tokenlist(halfword p)
555 {
556 int old_mode;
557 pointer q = get_avail();
558 pointer r = get_avail();
559 token_info(q) = right_brace_token + '}';
560 token_link(q) = r;
561 token_info(r) = end_write_token;
562 begin_token_list(q, inserted);
563 begin_token_list(p, write_text);
564 q = get_avail();
565 token_info(q) = left_brace_token + '{';
566 begin_token_list(q, inserted);
567 /*tex
568
569 Now we're ready to scan `\.\{$\langle\,$token list$\,\rangle$\.{\}
570 \\endwrite}'.
571
572 */
573 old_mode = mode;
574 mode = 0;
575 /*tex
576
577 Disable \.{\\prevdepth}, \.{\\spacefactor}, \.{\\lastskip},
578 \.{\\prevgraf}.
579
580 */
581 cur_cs = write_loc;
582 /*tex Expand macros, etc. */
583 q = scan_toks(false, true);
584 get_token();
585 if (cur_tok != end_write_token) {
586 /*tex Recover from an unbalanced write command */
587 const char *hlp[] = {
588 "On this page there's a \\write with fewer real {'s than }'s.",
589 "I can't handle that very well; good luck.",
590 NULL
591 };
592 tex_error("Unbalanced write command", hlp);
593 do {
594 get_token();
595 } while (cur_tok != end_write_token);
596 }
597 mode = old_mode;
598 /*tex Conserve stack space. */
599 end_token_list();
600 }
601
602 void write_out(halfword p)
603 {
604 /*tex holds print |selector| */
605 int old_setting;
606 /*tex write stream number */
607 int j;
608 /*tex line to be written, as a Lua string */
609 lstring *ss = NULL;
610 lstring *s = NULL;
611 int callback_id;
612 int lua_retval;
613 expand_macros_in_tokenlist(write_tokens(p));
614 old_setting = selector;
615 j = write_stream(p);
616 if (file_can_be_written(j)) {
617 selector = j;
618 } else if ((j == term_only) && (selector == term_and_log)) {
619 /*tex write to the terminal if file isn't open */
620 selector = log_only;
621 tprint_nl("");
622 } else {
623 tprint_nl("");
624 }
625 s = tokenlist_to_lstring(def_ref, false);
626 if (selector < no_print) {
627 /*tex selector is a file */
628 callback_id = callback_defined(process_output_buffer_callback);
629 if (callback_id > 0) {
630 /*tex fix up the output buffer using callbacks */
631 lua_retval = run_callback(callback_id, "L->L", s, &ss);
632 if ((lua_retval == true) && (ss != NULL)) {
633 free_lstring(s);
634 s = ss;
635 }
636 }
637 }
638 lprint(s);
639 free_lstring(s);
640 print_ln();
641 flush_list(def_ref);
642 selector = old_setting;
643 }
644
645 void finalize_write_files(void) {
646 int k;
647 for (k = 0; k <= last_file_selector; k++) {
648 if (write_open[k]) {
649 lua_a_close_out(write_file[k]);
650 }
651 }
652 }
653
654 void initialize_write_files(void) {
655 int k;
656 for (k = 0; k <= last_file_selector; k++) {
657 write_open[k] = false;
658 }
659 }
660
661 void close_write_file(int id) {
662 if (write_open[id]) {
663 lua_a_close_out(write_file[id]);
664 write_open[id] = false;
665 }
666 }
667
668 boolean open_write_file(int id, char *fn) {
669 if (lua_a_open_out(&(write_file[id]), fn, (id + 1))) {
670 write_open[id] = true;
671 return true;
672 } else {
673 return false;
674 }
675 }
676
677 /*tex
678
679 To implement primitives as \.{\\pdfextension info}, \.{\\pdfextension
680 catalog} or \.{\\pdfextension names} we need to concatenate tokens lists.
681
682 */
683
684 halfword concat_tokens(halfword q, halfword r)
685 {
686 /*tex concat |q| and |r| and returns the result tokens list */
687 halfword p;
688 if (q == null)
689 return r;
690 p = q;
691 while (token_link(p) != null)
692 p = token_link(p);
693 set_token_link(p, token_link(r));
694 free_avail(r);
695 return q;
696 }
697
698 /*tex
699
700 The \eTeX\ features available in extended mode are grouped into two
701 categories: (1)~Some of them are permanently enabled and have no semantic
702 effect as long as none of the additional primitives are executed. (2)~The
703 remaining \eTeX\ features are optional and can be individually enabled and
704 disabled. For each optional feature there is an \eTeX\ state variable named
705 \.{\\...state}; the feature is enabled, resp.\ disabled by assigning a
706 positive, resp.\ non-positive value to that integer.
707
708 In order to handle \.{\\everyeof} we need an array |eof_seen| of boolean
709 variables.
710
711 */
712
713 boolean *eof_seen;
714
715 /*tex
716
717 The |print_group| procedure prints the current level of grouping and the name
718 corresponding to |cur_group|.
719
720 */
721
722 void print_group(boolean e)
723 {
724 switch (cur_group) {
725 case bottom_level:
726 tprint("bottom level");
727 return;
728 break;
729 case simple_group:
730 case semi_simple_group:
731 if (cur_group == semi_simple_group)
732 tprint("semi ");
733 tprint("simple");
734 break;;
735 case hbox_group:
736 case adjusted_hbox_group:
737 if (cur_group == adjusted_hbox_group)
738 tprint("adjusted ");
739 tprint("hbox");
740 break;
741 case vbox_group:
742 tprint("vbox");
743 break;
744 case vtop_group:
745 tprint("vtop");
746 break;
747 case align_group:
748 case no_align_group:
749 if (cur_group == no_align_group)
750 tprint("no ");
751 tprint("align");
752 break;
753 case output_group:
754 tprint("output");
755 break;
756 case disc_group:
757 tprint("disc");
758 break;
759 case insert_group:
760 tprint("insert");
761 break;
762 case vcenter_group:
763 tprint("vcenter");
764 break;
765 case math_group:
766 case math_choice_group:
767 case math_shift_group:
768 case math_left_group:
769 tprint("math");
770 if (cur_group == math_choice_group)
771 tprint(" choice");
772 else if (cur_group == math_shift_group)
773 tprint(" shift");
774 else if (cur_group == math_left_group)
775 tprint(" left");
776 break;
777 }
778 tprint(" group (level ");
779 print_int(cur_level);
780 print_char(')');
781 if (saved_value(-1) != 0) {
782 /*tex |saved_line| */
783 if (e)
784 tprint(" entered at line ");
785 else
786 tprint(" at line ");
787 print_int(saved_value(-1));
788 }
789 }
790
791 /*tex
792
793 The |group_trace| procedure is called when a new level of grouping begins
794 (|e=false|) or ends (|e=true|) with |saved_value(-1)| containing the line
795 number.
796
797 */
798
799 void group_trace(boolean e)
800 {
801 begin_diagnostic();
802 print_char('{');
803 if (e)
804 tprint("leaving ");
805 else
806 tprint("entering ");
807 print_group(e);
808 print_char('}');
809 end_diagnostic(false);
810 }
811
812 /*tex
813
814 A group entered (or a conditional started) in one file may end in a different
815 file. Such slight anomalies, although perfectly legitimate, may cause errors
816 that are difficult to locate. In order to be able to give a warning message
817 when such anomalies occur, \eTeX\ uses the |grp_stack| and |if_stack| arrays
818 to record the initial |cur_boundary| and |cond_ptr| values for each input
819 file.
820
821 */
822
823 /*tex initial |cur_boundary| */
824
825 save_pointer *grp_stack;
826
827 /*tex initial |cond_ptr| */
828
829 halfword *if_stack;
830
831 /*tex
832
833 When a group ends that was apparently entered in a different input file, the
834 |group_warning| procedure is invoked in order to update the |grp_stack|. If
835 moreover \.{\\tracingnesting} is positive we want to give a warning message.
836 The situation is, however, somewhat complicated by two facts: (1)~There may
837 be |grp_stack| elements without a corresponding \.{\\input} file or
838 \.{\\scantokens} pseudo file (e.g., error insertions from the terminal); and
839 (2)~the relevant information is recorded in the |name_field| of the
840 |input_stack| only loosely synchronized with the |in_open| variable indexing
841 |grp_stack|.
842
843 */
844
845 void group_warning(void)
846 {
847 /*tex do we need a warning? */
848 boolean w = false;
849 /*tex index into |grp_stack| */
850 int i = in_open;
851 base_ptr = input_ptr;
852 /*tex store current state */
853 input_stack[base_ptr] = cur_input;
854 while ((grp_stack[i] == cur_boundary) && (i > 0)) {
855 /*tex
856
857 Set variable |w| to indicate if this case should be reported. This
858 code scans the input stack in order to determine the type of the
859 current input file.
860
861 */
862 if (tracing_nesting_par > 0) {
863 while ((input_stack[base_ptr].state_field == token_list) || (input_stack[base_ptr].index_field > i))
864 decr(base_ptr);
865 if (input_stack[base_ptr].name_field > 17)
866 w = true;
867 }
868 grp_stack[i] = save_value(save_ptr);
869 decr(i);
870 }
871 if (w) {
872 tprint_nl("Warning: end of ");
873 print_group(true);
874 tprint(" of a different file");
875 print_ln();
876 if (tracing_nesting_par > 1)
877 show_context();
878 if (history == spotless)
879 history = warning_issued;
880 }
881 }
882
883 /*tex
884
885 When a conditional ends that was apparently started in a different input
886 file, the |if_warning| procedure is invoked in order to update the
887 |if_stack|. If moreover \.{\\tracingnesting} is positive we want to give a
888 warning message (with the same complications as above).
889
890 */
891
892 void if_warning(void)
893 {
894 /*tex Do we need a warning? */
895 boolean w = false;
896 int i = in_open;
897 base_ptr = input_ptr;
898 /*tex Store current state. */
899 input_stack[base_ptr] = cur_input;
900 while (if_stack[i] == cond_ptr) {
901 /*tex Set variable |w| to. */
902 if (tracing_nesting_par > 0) {
903 while ((input_stack[base_ptr].state_field == token_list) || (input_stack[base_ptr].index_field > i))
904 decr(base_ptr);
905 if (input_stack[base_ptr].name_field > 17)
906 w = true;
907 }
908 if_stack[i] = vlink(cond_ptr);
909 decr(i);
910 }
911 if (w) {
912 tprint_nl("Warning: end of ");
913 print_cmd_chr(if_test_cmd, cur_if);
914 print_if_line(if_line);
915 tprint(" of a different file");
916 print_ln();
917 if (tracing_nesting_par > 1)
918 show_context();
919 if (history == spotless)
920 history = warning_issued;
921 }
922 }
923
924 /*tex
925
926 Conversely, the |file_warning| procedure is invoked when a file ends and some
927 groups entered or conditionals started while reading from that file are still
928 incomplete.
929
930 */
931
932 void file_warning(void)
933 {
934 /*tex saved value of |save_ptr| or |cond_ptr| */
935 halfword p = save_ptr;
936 /*tex saved value of |cur_level| or |if_limit| */
937 int l = cur_level;
938 /*tex saved value of |cur_group| or |cur_if| */
939 int c = cur_group;
940 /*tex saved value of |if_line| */
941 int i;
942 save_ptr = cur_boundary;
943 while (grp_stack[in_open] != save_ptr) {
944 decr(cur_level);
945 tprint_nl("Warning: end of file when ");
946 print_group(true);
947 tprint(" is incomplete");
948 cur_group = save_level(save_ptr);
949 save_ptr = save_value(save_ptr);
950 }
951 save_ptr = p;
952 cur_level = (quarterword) l;
953 /*tex Restore old values. */
954 cur_group = (group_code) c;
955 p = cond_ptr;
956 l = if_limit;
957 c = cur_if;
958 i = if_line;
959 while (if_stack[in_open] != cond_ptr) {
960 tprint_nl("Warning: end of file when ");
961 print_cmd_chr(if_test_cmd, cur_if);
962 if (if_limit == fi_code)
963 tprint_esc("else");
964 print_if_line(if_line);
965 tprint(" is incomplete");
966 if_line = if_line_field(cond_ptr);
967 cur_if = if_limit_subtype(cond_ptr);
968 if_limit = if_limit_type(cond_ptr);
969 cond_ptr = vlink(cond_ptr);
970 }
971 /*tex restore old values */
972 cond_ptr = p;
973 if_limit = l;
974 cur_if = c;
975 if_line = i;
976 print_ln();
977 if (tracing_nesting_par > 1)
978 show_context();
979 if (history == spotless)
980 history = warning_issued;
981 }
982
983 /*tex The |par_fill_skip| glue node of the new paragraph. */
984
985 halfword last_line_fill;
986
987 /*tex
988
989 The lua interface needs some extra functions. The functions themselves are
990 quite boring, but they are handy because otherwise this internal stuff has to
991 be accessed from C directly, where lots of the defines are not available.
992
993 */
994
995 #define get_tex_dimen_register(j) dimen(j)
996 #define get_tex_skip_register(j) skip(j)
997 #define get_tex_mu_skip_register(j) mu_skip(j)
998 #define get_tex_count_register(j) count(j)
999 #define get_tex_attribute_register(j) attribute(j)
1000 #define get_tex_box_register(j) box(j)
1001
1002 /*tex These can now be macros (todo). */
1003
1004 int get_tex_extension_count_register(int i)
1005 {
1006 return (int) int_par(backend_int_base-int_base+i);
1007 }
1008
1009 void set_tex_extension_count_register(int i, int d)
1010 {
1011 int_par(backend_int_base-int_base+i) = d;
1012 }
1013
1014 int get_tex_extension_dimen_register(int i)
1015 {
1016 return (int) dimen_par(backend_dimen_base-dimen_base+i);
1017 }
1018
1019 void set_tex_extension_dimen_register(int i, int d)
1020 {
1021 dimen_par(backend_dimen_base-dimen_base+i) = d;
1022 }
1023
1024 int get_tex_extension_toks_register(int i)
1025 {
1026 return equiv(backend_toks_base+i);
1027 }
1028
1029 int set_tex_dimen_register(int j, scaled v)
1030 {
1031 int a;
1032 if (global_defs_par > 0)
1033 a = 4;
1034 else
1035 a = 0;
1036 word_define(j + scaled_base, v);
1037 return 0;
1038 }
1039
1040 int set_tex_skip_register(int j, halfword v)
1041 {
1042 int a;
1043 if (global_defs_par > 0)
1044 a = 4;
1045 else
1046 a = 0;
1047 if (type(v) != glue_spec_node)
1048 return 1;
1049 word_define(j + skip_base, v);
1050 return 0;
1051 }
1052
1053 int set_tex_mu_skip_register(int j, halfword v)
1054 {
1055 int a;
1056 if (global_defs_par > 0)
1057 a = 4;
1058 else
1059 a = 0;
1060 if (type(v) != glue_spec_node)
1061 return 1;
1062 word_define(j + mu_skip_base, v);
1063 return 0;
1064 }
1065
1066 int set_tex_count_register(int j, scaled v)
1067 {
1068 int a;
1069 if (global_defs_par > 0)
1070 a = 4;
1071 else
1072 a = 0;
1073 word_define(j + count_base, v);
1074 return 0;
1075 }
1076
1077 int set_tex_box_register(int j, scaled v)
1078 {
1079 int a;
1080 if (global_defs_par > 0)
1081 a = 4;
1082 else
1083 a = 0;
1084 define(j + box_base, box_ref_cmd, v);
1085 return 0;
1086 }
1087
1088 int set_tex_attribute_register(int j, scaled v)
1089 {
1090 int a;
1091 if (global_defs_par > 0)
1092 a = 4;
1093 else
1094 a = 0;
1095 if (j > max_used_attr)
1096 max_used_attr = j;
1097 attr_list_cache = cache_disabled;
1098 word_define(j + attribute_base, v);
1099 return 0;
1100 }
1101
1102 int get_tex_toks_register(int j)
1103 {
1104 str_number s = get_nullstr();
1105 if (toks(j) != null) {
1106 s = tokens_to_string(toks(j));
1107 }
1108 return s;
1109 }
1110
1111 int set_tex_toks_register(int j, lstring s)
1112 {
1113 int a;
1114 halfword ref = get_avail();
1115 (void) str_toks(s);
1116 set_token_ref_count(ref, 0);
1117 set_token_link(ref, token_link(temp_token_head));
1118 if (global_defs_par > 0)
1119 a = 4;
1120 else
1121 a = 0;
1122 define(j + toks_base, call_cmd, ref);
1123 return 0;
1124 }
1125
1126 int scan_tex_toks_register(int j, int c, lstring s)
1127 {
1128 int a;
1129 halfword ref = get_avail();
1130 (void) str_scan_toks(c,s);
1131 set_token_ref_count(ref, 0);
1132 set_token_link(ref, token_link(temp_token_head));
1133 if (global_defs_par > 0)
1134 a = 4;
1135 else
1136 a = 0;
1137 define(j + toks_base, call_cmd, ref);
1138 return 0;
1139 }
1140
1141 scaled get_tex_box_width(int j)
1142 {
1143 halfword q = box(j);
1144 if (q != null)
1145 return width(q);
1146 return 0;
1147 }
1148
1149 int set_tex_box_width(int j, scaled v)
1150 {
1151 halfword q = box(j);
1152 if (q == null)
1153 return 1;
1154 width(q) = v;
1155 return 0;
1156 }
1157
1158 scaled get_tex_box_height(int j)
1159 {
1160 halfword q = box(j);
1161 if (q != null)
1162 return height(q);
1163 return 0;
1164 }
1165
1166 int set_tex_box_height(int j, scaled v)
1167 {
1168 halfword q = box(j);
1169 if (q == null)
1170 return 1;
1171 height(q) = v;
1172 return 0;
1173 }
1174
1175 scaled get_tex_box_depth(int j)
1176 {
1177 halfword q = box(j);
1178 if (q != null)
1179 return depth(q);
1180 return 0;
1181 }
1182
1183 int set_tex_box_depth(int j, scaled v)
1184 {
1185 halfword q = box(j);
1186 if (q == null)
1187 return 1;
1188 depth(q) = v;
1189 return 0;
1190 }
1191
1192 /*tex
1193
1194 This section is devoted to the {\sl Synchronize \TeX nology} - or simply {\sl
1195 Sync\TeX} - used to synchronize between input and output. This section
1196 explains how synchronization basics are implemented. Before we enter into
1197 more technical details, let us recall in a few words what is synchronization.
1198
1199 \TeX\ typesetting system clearly separates the input and the output material,
1200 and synchronization will provide a new link between both that can help text
1201 editors and viewers to work together. More precisely, forwards
1202 synchronization is the ability, given a location in the input source file, to
1203 find what is the corresponding place in the output. Backwards synchronization
1204 just performs the opposite: given a location in the output, retrieve the
1205 corresponding material in the input source file.
1206
1207 For better code management and maintainance, we adopt a naming convention.
1208 Throughout this program, code related to the {\sl Synchronize \TeX nology} is
1209 tagged with the ``{\sl synctex}'' key word. Any code extract where {\sl
1210 Sync\TeX} plays its part, either explicitly or implicitly, (should) contain
1211 the string ``{\sl synctex}''. This naming convention also holds for external
1212 files. Moreover, all the code related to {\sl Sync\TeX} is gathered in this
1213 section, except the definitions.
1214
1215 Enabling synchronization should be performed from the command line,
1216 |synctexoption| is used for that purpose. This global integer variable is
1217 declared here but it is not used here. This is just a placeholder where the
1218 command line controller will put the {\sl Sync\TeX} related options, and the
1219 {\sl Sync\TeX} controller will read them.
1220
1221 */
1222
1223 int synctexoption;
1224
1225 /*tex
1226
1227 A convenient primitive is provided: \.{\\synctex=1} in the input source file
1228 enables synchronization whereas \.{\\synctex=0} disables it. Its memory
1229 address is |synctex_code|. It is initialized by the {\sl Sync\TeX} controller
1230 to the command-line option if given. The controller may filter some reserved
1231 bits.
1232
1233 In order to give the {\sl Sync\TeX} controller read and write access to the
1234 contents of the \.{\\synctex} primitive, we declare |synctexoffset|, such
1235 that |mem[synctexoffset]| and \.{\\synctex} correspond to the same memory
1236 storage. |synctexoffset| is initialized to the correct value when quite
1237 everything is initialized.
1238
1239 */
1240
1241 /*tex Holds the true value of |synctex_code|: */
1242
1243 int synctexoffset;
1244
1245 /*tex
1246
1247 Synchronization is achieved with the help of an auxiliary file named `\.{{\sl
1248 jobname}.synctex}' ({\sl jobname} is the contents of the \.{\\jobname}
1249 macro), where a {\sl Sync\TeX} controller implemented in the external
1250 |synctex.c| file will store geometrical information. This {\sl Sync\TeX}
1251 controller will take care of every technical details concerning the {\sl
1252 Sync\TeX} file, we will only focus on the messages the controller will
1253 receive from the \TeX\ program.
1254
1255 The most accurate synchronization information should allow to map any
1256 character of the input source file to the corresponding location in the
1257 output, if relevant. Ideally, the synchronization information of the input
1258 material consists of the file name, the line and column numbers of every
1259 character. The synchronization information in the output is simply the page
1260 number and either point coordinates, or box dimensions and position. The
1261 problem is that the mapping between these informations is only known at ship
1262 out time, which means that we must keep track of the input synchronization
1263 information until the pages ship out.
1264
1265 As \TeX\ only knows about file names and line numbers, but forgets the column
1266 numbers, we only consider a restricted input synchronization information
1267 called {\sl Sync\TeX\ information}. It consists of a unique file name
1268 identifier, the {\sl Sync\TeX\ file tag}, and the line number.
1269
1270 Keeping track of such information, should be different whether characters or
1271 nodes are involved. Actually, only certain nodes are involved in {\sl
1272 Sync\TeX}, we call them {\sl synchronized nodes}. Synchronized nodes store
1273 the {\sl Sync\TeX} information in their last two words: the first one
1274 contains a {\sl Sync\TeX\ file tag} uniquely identifying the input file, and
1275 the second one contains the current line number, as returned by the
1276 \.{\\inputlineno} primitive. The |synctex_field_size| macro contains the
1277 necessary size to store the {\sl Sync\TeX} information in a node.
1278
1279 When declaring the size of a new node, it is recommanded to use the following
1280 convention: if the node is synchronized, use a definition similar to
1281 |my_synchronized_node_size|={\sl xxx}+|synctex_field_size|. Moreover, one
1282 should expect that the {\sl Sync\TeX} information is always stored in the
1283 last two words of a synchronized node.
1284
1285 By default, every node with a sufficiently big size is initialized at
1286 creation time in the |get_node| routine with the current {\sl Sync\TeX}
1287 information, whether or not the node is synchronized. One purpose is to set
1288 this information very early in order to minimize code dependencies, including
1289 forthcoming extensions. Another purpose is to avoid the assumption that every
1290 node type has a dedicated getter, where initialization should take place.
1291 Actually, it appears that some nodes are created using directly the
1292 |get_node| routine and not the dedicated constructor. And finally,
1293 initializing the node at only one place is less error prone.
1294
1295 Instead of storing the input file name, it is better to store just an
1296 identifier. Each time \TeX\ opens a new file, it notifies the {\sl Sync\TeX}
1297 controller with a |synctex_start_input| message. This controller will create
1298 a new {\sl Sync\TeX} file tag and will update the current input state record
1299 accordingly. If the input comes from the terminal or a pseudo file, the
1300 |synctex_tag| is set to 0. It results in automatically disabling
1301 synchronization for material input from the terminal or pseudo files.
1302
1303 Synchronized nodes are boxes, math, kern and glue nodes. Other nodes should
1304 be synchronized too, in particular math noads. \TeX\ assumes that math, kern
1305 and glue nodes have the same size, this is why both are synchronized. {\sl In
1306 fine}, only horizontal lists are really used in {\sl Sync\TeX}, but all box
1307 nodes are considered the same with respect to synchronization, because a box
1308 node type is allowed to change at execution time.
1309
1310 {\em Nota Bene:} The {\sl Sync\TeX} code is very close to the memory model.
1311 It is not connected to any other part of the code, except for memory
1312 management. It is possible to neutralize the {\sl Sync\TeX} code rather
1313 simply. The first step is to define a null |synctex_field_size|. The second
1314 step is to comment out the code in ``Initialize bigger nodes...'' and every
1315 ``Copy ... {\sl Sync\TeX} information''. The last step will be to comment out
1316 the |synctex_tag_field| related code in the definition of |synctex_tag| and
1317 the various ``Prepare ... {\sl Sync\TeX} information''. Then all the
1318 remaining code should be just harmless. The resulting program would behave
1319 exactly the same as if absolutely no {\sl Sync\TeX} related code was there,
1320 including memory management. Of course, all this assumes that {\sl Sync\TeX}
1321 is turned off from the command line.
1322
1323 Here are extra variables for Web2c. (This numbering of the system-dependent
1324 section allows easy integration of Web2c and e-\TeX, etc.)
1325
1326 */
1327
1328 /*tex where the filename to switch to starts */
1329
1330 pool_pointer edit_name_start;
1331
1332 /*tex what line to start editing at */
1333
1334 int edit_name_length, edit_line;
1335
1336 /*tex whether |more_name| returns false for space */
1337
1338 boolean stop_at_space;
1339
1340 /*tex
1341
1342 The |edit_name_start| will be set to point into |str_pool| somewhere after
1343 its beginning if \TeX\ is supposed to switch to an editor on exit.
1344
1345 */
1346
1347 int shellenabledp;
1348 int restrictedshell;
1349 char *output_comment;
1350
1351 /*tex
1352
1353 Are we printing extra info as we read the format file?
1354
1355 */
1356
1357 boolean debug_format_file;
1358
1359 void wrapup_leader(halfword p)
1360 {
1361 /*tex Do some work that has been queued up for \.{\\write}. */
1362 if (!doing_leaders) {
1363 int j = write_stream(p);
1364 if (subtype(p) == write_node) {
1365 write_out(p);
1366 } else if (subtype(p) == close_node) {
1367 close_write_file(j);
1368 } else if (valid_write_file(j)) {
1369 char *fn;
1370 close_write_file(j);
1371 cur_name = open_name(p);
1372 cur_area = open_area(p);
1373 cur_ext = open_ext(p);
1374 if (cur_ext == get_nullstr())
1375 cur_ext = maketexstring(".tex");
1376 fn = pack_file_name(cur_name, cur_area, cur_ext);
1377 while (! open_write_file(j,fn)) {
1378 fn = prompt_file_name("output file name", ".tex");
1379 }
1380 }
1381 }
1382 }