Debian Codegraph @

texk/web2c/luatexdir/lua/ltexlib.c

raw
1 /* ltexlib.c
2
3 Copyright 2006-2012 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
19
20 /* hh-ls: Because the lists start with a temp node, we have to set the prev link
21 to nil because otherwise at the lua end we expose temp which can create havoc. In the
22 setter no prev link is created so we can presume that it's not used later on. */
23
24 #include "ptexlib.h"
25 #include "lua/luatex-api.h"
26
27 /* tex random generators */
28 extern int unif_rand(int );
29 extern int norm_rand(void );
30 extern void init_randoms(int );
31
32 typedef struct {
33 char *text;
34 unsigned int tsize;
35 void *next;
36 boolean partial;
37 int cattable;
38 halfword tok;
39 halfword nod;
40 } rope;
41
42 typedef struct {
43 rope *head;
44 rope *tail;
45 char complete; /* currently still writing ? */
46 } spindle;
47
48 #define PARTIAL_LINE 1
49 #define FULL_LINE 0
50
51 #define write_spindle spindles[spindle_index]
52 #define read_spindle spindles[(spindle_index-1)]
53
54 static int spindle_size = 0;
55 static spindle *spindles = NULL;
56 static int spindle_index = 0;
57
58 static int luac_store(lua_State * L, int i, int partial, int cattable)
59 {
60 char *st = NULL;
61 size_t tsize = 0;
62 rope *rn = NULL;
63 halfword tok = null;
64 halfword nod = null;
65 int t = lua_type(L, i);
66 if (t == LUA_TNUMBER || t == LUA_TSTRING) {
67 const char *sttemp;
68 sttemp = lua_tolstring(L, i, &tsize);
69 st = xmalloc((unsigned) (tsize + 1));
70 memcpy(st, sttemp, (tsize + 1));
71 } else if (t == LUA_TUSERDATA) {
72 void *p ;
73 p = lua_touserdata(L, i);
74 if (p == NULL) {
75 return 0;
76 } else if (lua_getmetatable(L, i)) {
77 lua_get_metatablelua(luatex_token);
78 if (lua_rawequal(L, -1, -2)) {
79 tok = (halfword) token_info((*((lua_token *)p)).token);
80 lua_pop(L, 2);
81 } else {
82 lua_get_metatablelua(luatex_node);
83 if (lua_rawequal(L, -1, -3)) {
84 nod = *((halfword *)p);
85 lua_pop(L, 3);
86 } else {
87 lua_pop(L, 3);
88 return 0;
89 }
90 }
91 } else {
92 return 0;
93 }
94 } else {
95 return 0;
96 }
97 /* common */
98 luacstrings++;
99 rn = (rope *) xmalloc(sizeof(rope));
100 rn->text = st;
101 rn->tsize = (unsigned) tsize;
102 rn->tok = tok;
103 rn->nod = nod;
104 rn->next = NULL;
105 rn->partial = partial;
106 rn->cattable = cattable;
107 /* add */
108 if (write_spindle.head == NULL) {
109 write_spindle.head = rn;
110 } else {
111 write_spindle.tail->next = rn;
112 }
113 write_spindle.tail = rn;
114 write_spindle.complete = 0;
115 return 1;
116 }
117
118 static int do_luacprint(lua_State * L, int partial, int deftable)
119 {
120 int cattable = deftable;
121 int startstrings = 1;
122 int n = lua_gettop(L);
123 if (cattable != NO_CAT_TABLE) {
124 if (lua_type(L, 1) == LUA_TNUMBER && n > 1) {
125 cattable = lua_tointeger(L, 1);
126 startstrings = 2;
127 if (cattable != -1 && cattable != -2 && !valid_catcode_table(cattable)) {
128 cattable = DEFAULT_CAT_TABLE;
129 }
130 }
131 }
132 if (lua_type(L, startstrings) == LUA_TTABLE) {
133 int i;
134 for (i = 1;; i++) {
135 lua_rawgeti(L, startstrings, i);
136 if (luac_store(L, -1, partial, cattable)) {
137 lua_pop(L, 1);
138 } else {
139 lua_pop(L, 1);
140 break;
141 }
142 }
143 } else {
144 int i;
145 for (i = startstrings; i <= n; i++) {
146 luac_store(L, i, partial, cattable);
147 }
148 }
149 return 0;
150 }
151
152 /* lua.write */
153
154 static int luacwrite(lua_State * L)
155 {
156 return do_luacprint(L, FULL_LINE, NO_CAT_TABLE);
157 }
158
159 /* lua.print */
160
161 static int luacprint(lua_State * L)
162 {
163 return do_luacprint(L, FULL_LINE, DEFAULT_CAT_TABLE);
164 }
165
166 /* lua.sprint */
167
168 static int luacsprint(lua_State * L)
169 {
170 return do_luacprint(L, PARTIAL_LINE, DEFAULT_CAT_TABLE);
171 }
172
173 /* lua.cprint */
174
175 static int luaccprint(lua_State * L)
176 {
177 /* so a negative value is a specific catcode with offset 1 */
178 int cattable = lua_tointeger(L,1);
179 if (cattable < 0 || cattable > 15) {
180 cattable = - 12 - 0xFF ;
181 } else {
182 cattable = - cattable - 0xFF;
183 }
184 if (lua_type(L, 2) == LUA_TTABLE) {
185 int i;
186 for (i = 1;; i++) {
187 lua_rawgeti(L, 2, i);
188 if (luac_store(L, -1, PARTIAL_LINE, cattable)) {
189 lua_pop(L, 1);
190 } else {
191 lua_pop(L, 1);
192 break;
193 }
194 }
195 } else {
196 int i;
197 int n = lua_gettop(L);
198 for (i = 2; i <= n; i++) {
199 luac_store(L, i, PARTIAL_LINE, cattable);
200 }
201 }
202 return 0;
203 }
204
205 /* lua.tprint */
206
207 static int luactprint(lua_State * L)
208 {
209 int i, j;
210 int cattable, startstrings;
211 int n = lua_gettop(L);
212 for (i = 1; i <= n; i++) {
213 cattable = DEFAULT_CAT_TABLE;
214 startstrings = 1;
215 if (lua_type(L, i) != LUA_TTABLE) {
216 luaL_error(L, "no string to print");
217 }
218 lua_pushvalue(L, i); /* push the table */
219 lua_pushinteger(L, 1);
220 lua_gettable(L, -2);
221 if (lua_type(L, -1) == LUA_TNUMBER) {
222 cattable = lua_tointeger(L, -1);
223 startstrings = 2;
224 if (cattable != -1 && cattable != -2 && !valid_catcode_table(cattable)) {
225 cattable = DEFAULT_CAT_TABLE;
226 }
227 }
228 lua_pop(L, 1);
229 for (j = startstrings;; j++) {
230 lua_pushinteger(L, j);
231 lua_gettable(L, -2);
232 if (luac_store(L, -1, PARTIAL_LINE, cattable)) {
233 lua_pop(L, 1);
234 } else {
235 lua_pop(L, 1);
236 break;
237 }
238 }
239 lua_pop(L, 1); /* pop the table */
240 }
241 return 0;
242 }
243
244 int luacstring_cattable(void)
245 {
246 return (int) read_spindle.tail->cattable;
247 }
248
249 int luacstring_partial(void)
250 {
251 return read_spindle.tail->partial;
252 }
253
254 int luacstring_final_line(void)
255 {
256 return (read_spindle.tail->next == NULL);
257 }
258
259 int luacstring_input(halfword *n)
260 {
261 rope *t = read_spindle.head;
262 int ret = 1 ;
263 if (!read_spindle.complete) {
264 read_spindle.complete = 1;
265 read_spindle.tail = NULL;
266 }
267 if (t == NULL) {
268 if (read_spindle.tail != NULL)
269 free(read_spindle.tail);
270 read_spindle.tail = NULL;
271 return 0;
272 }
273 if (t->text != NULL) {
274 /* put that thing in the buffer */
275 char *st = t->text;
276 int ret = first;
277 last = first;
278 check_buffer_overflow(last + (int) t->tsize);
279 while (t->tsize-- > 0)
280 buffer[last++] = (packed_ASCII_code) * st++;
281 if (!t->partial) {
282 while (last - 1 > ret && buffer[last - 1] == ' ')
283 last--;
284 }
285 free(t->text);
286 t->text = NULL;
287 } else if (t->tok > 0) {
288 *n = t->tok;
289 ret = 2;
290 } else if (t->nod > 0) {
291 *n = t->nod;
292 ret = 3;
293 }
294 if (read_spindle.tail != NULL) {
295 /* not a one-liner */
296 free(read_spindle.tail);
297 }
298 read_spindle.tail = t;
299 read_spindle.head = t->next;
300 return ret;
301 }
302
303 /* open for reading, and make a new one for writing */
304
305 void luacstring_start(int n)
306 {
307 (void) n; /* for -W */
308 spindle_index++;
309 if (spindle_size == spindle_index) {
310 /* add a new one */
311 spindles = xrealloc(spindles, (unsigned) (sizeof(spindle) * (unsigned) (spindle_size + 1)));
312 spindles[spindle_index].head = NULL;
313 spindles[spindle_index].tail = NULL;
314 spindles[spindle_index].complete = 0;
315 spindle_size++;
316 }
317 }
318
319 /* close for reading */
320
321 void luacstring_close(int n)
322 {
323 rope *next, *t;
324 (void) n; /* for -W */
325 next = read_spindle.head;
326 while (next != NULL) {
327 if (next->text != NULL)
328 free(next->text);
329 t = next;
330 next = next->next;
331 if (t==read_spindle.tail) {
332 read_spindle.tail = NULL;
333 }
334 free(t);
335 }
336 read_spindle.head = NULL;
337 if (read_spindle.tail != NULL)
338 free(read_spindle.tail);
339 read_spindle.tail = NULL;
340 read_spindle.complete = 0;
341 spindle_index--;
342 }
343
344 /* local (static) versions */
345
346 #define check_index_range(j,s) \
347 if (j<0 || j > 65535) { \
348 luaL_error(L, "incorrect index specification for tex.%s()", s); }
349
350 #define check_register(base) do { \
351 int k = get_item_index(L, lua_gettop(L), base); \
352 if ((k>=0) && (k <= 65535)) { \
353 lua_pushinteger(L,k); \
354 } else { \
355 lua_pushboolean(L,false); \
356 } \
357 return 1; \
358 } while (1)
359
360 static const char *scan_integer_part(lua_State * L, const char *ss, int *ret, int *radix_ret)
361 {
362 boolean negative = false; /* should the answer be negated? */
363 int m = 214748364; /* |$2^{31}$ / radix|, the threshold of danger */
364 int d; /* the digit just scanned */
365 boolean vacuous = true; /* have no digits appeared? */
366 boolean OK_so_far = true; /* has an error message been issued? */
367 int radix1 = 10; /* the radix of the integer */
368 int c = 0; /* the current character */
369 const char *s; /* where we stopped in the string |ss| */
370 integer val = 0; /* return value */
371 s = ss;
372 do {
373 do {
374 c = *s++;
375 } while (c && c == ' ');
376 if (c == '-') {
377 negative = !negative;
378 c = '+';
379 }
380 } while (c == '+');
381 if (c == '\'') {
382 radix1 = 8;
383 m = 02000000000;
384 c = *s++;
385 } else if (c == '"') {
386 radix1 = 16;
387 m = 01000000000;
388 c = *s++;
389 }
390 /* Accumulate the constant until |cur_tok| is not a suitable digit */
391 while (1) {
392 if ((c < '0' + radix1) && (c >= '0') && (c <= '0' + 9)) {
393 d = c - '0';
394 } else if (radix1 == 16) {
395 if ((c <= 'A' + 5) && (c >= 'A')) {
396 d = c - 'A' + 10;
397 } else if ((c <= 'a' + 5) && (c >= 'a')) {
398 d = c - 'a' + 10;
399 } else {
400 break;
401 }
402 } else {
403 break;
404 }
405 vacuous = false;
406 if ((val >= m) && ((val > m) || (d > 7) || (radix1 != 10))) {
407 if (OK_so_far) {
408 luaL_error(L, "Number too big");
409 val = infinity;
410 OK_so_far = false;
411 }
412 } else {
413 val = val * radix1 + d;
414 }
415 c = *s++;
416 }
417 if (vacuous) {
418 /* Express astonishment that no number was here */
419 luaL_error(L, "Missing number, treated as zero");
420 }
421 if (negative)
422 val = -val;
423 *ret = val;
424 *radix_ret = radix1;
425 if (c != ' ' && s > ss)
426 s--;
427 return s;
428 }
429
430 #define set_conversion(A,B) do { num=(A); denom=(B); } while(0)
431
432 /* sets |cur_val| to a dimension */
433
434 static const char *scan_dimen_part(lua_State * L, const char *ss, int *ret)
435 {
436 boolean negative = false; /* should the answer be negated? */
437 int f = 0; /* numerator of a fraction whose denominator is $2^{16}$ */
438 int num, denom; /* conversion ratio for the scanned units */
439 int k; /* number of digits in a decimal fraction */
440 scaled v; /* an internal dimension */
441 int save_cur_val; /* temporary storage of |cur_val| */
442 int c; /* the current character */
443 const char *s = ss; /* where we are in the string */
444 int radix1 = 0; /* the current radix */
445 int rdig[18]; /* to save the |dig[]| array */
446 int saved_tex_remainder; /* to save |tex_remainder| */
447 int saved_arith_error; /* to save |arith_error| */
448 int saved_cur_val; /* to save the global |cur_val| */
449 saved_tex_remainder = tex_remainder;
450 saved_arith_error = arith_error;
451 saved_cur_val = cur_val;
452 /* get the next non-blank non-sign */
453 do {
454 /* get the next non-blank non-call token */
455 do {
456 c = *s++;
457 } while (c && c == ' ');
458 if (c == '-') {
459 negative = !negative;
460 c = '+';
461 }
462 } while (c == '+');
463 if (c == ',') {
464 c = '.';
465 }
466 if (c != '.') {
467 s = scan_integer_part(L, (s > ss ? (s - 1) : ss), &cur_val, &radix1);
468 c = *s;
469 } else {
470 radix1 = 10;
471 cur_val = 0;
472 c = *(--s);
473 }
474 if (c == ',')
475 c = '.';
476 if ((radix1 == 10) && (c == '.')) {
477 /* scan decimal fraction */
478 for (k = 0; k < 18; k++)
479 rdig[k] = dig[k];
480 k = 0;
481 s++;
482 /* get rid of the '.' */
483 while (1) {
484 c = *s++;
485 if ((c > '0' + 9) || (c < '0'))
486 break;
487 if (k < 17) {
488 /* digits for |k>=17| cannot affect the result */
489 dig[k++] = c - '0';
490 }
491 }
492 f = round_decimals(k);
493 if (c != ' ')
494 c = *(--s);
495 for (k = 0; k < 18; k++)
496 dig[k] = rdig[k];
497 }
498 if (cur_val < 0) { /* in this case |f=0| */
499 negative = !negative;
500 cur_val = -cur_val;
501 }
502 /*
503 Scan for (u)units that are internal dimensions; |goto attach_sign| with
504 |cur_val| set if found.
505 */
506 save_cur_val = cur_val;
507 /* Get the next non-blank non-call... */
508 do {
509 c = *s++;
510 } while (c && c == ' ');
511 if (c != ' ')
512 c = *(--s);
513 if (strncmp(s, "em", 2) == 0) {
514 s += 2;
515 v = (quad(get_cur_font()));
516 } else if (strncmp(s, "ex", 2) == 0) {
517 s += 2;
518 v = (x_height(get_cur_font()));
519 } else if (strncmp(s, "px", 2) == 0) {
520 s += 2;
521 v = px_dimen_par;
522 } else {
523 goto NOT_FOUND;
524 }
525 c = *s++;
526 if (c != ' ') {
527 c = *(--s);
528 }
529 cur_val = nx_plus_y(save_cur_val, v, xn_over_d(v, f, 0200000));
530 goto ATTACH_SIGN;
531 NOT_FOUND:
532 /* Scan for (m)\.{mu} units and |goto attach_fraction| */
533 if (strncmp(s, "mu", 2) == 0) {
534 s += 2;
535 goto ATTACH_FRACTION;
536 }
537 if (strncmp(s, "true", 4) == 0) {
538 /* Adjust (f)for the magnification ratio */
539 s += 4;
540 if (output_mode_used <= OMODE_DVI) {
541 prepare_mag();
542 if (mag_par != 1000) {
543 cur_val = xn_over_d(cur_val, 1000, mag_par);
544 f = (1000 * f + 0200000 * tex_remainder) / mag_par;
545 cur_val = cur_val + (f / 0200000);
546 f = f % 0200000;
547 }
548 }
549 do {
550 c = *s++;
551 } while (c && c == ' ');
552 c = *(--s);
553 }
554 if (strncmp(s, "pt", 2) == 0) {
555 s += 2;
556 goto ATTACH_FRACTION; /* the easy case */
557 }
558 /*
559 Scan for (a)all other units and adjust |cur_val| and |f| accordingly; |goto done|
560 in the case of scaled points
561 */
562 if (strncmp(s, "mm", 2) == 0) {
563 s += 2;
564 set_conversion(7227, 2540);
565 } else if (strncmp(s, "cm", 2) == 0) {
566 s += 2;
567 set_conversion(7227, 254);
568 } else if (strncmp(s, "sp", 2) == 0) {
569 s += 2;
570 goto DONE;
571 } else if (strncmp(s, "bp", 2) == 0) {
572 s += 2;
573 set_conversion(7227, 7200);
574 } else if (strncmp(s, "in", 2) == 0) {
575 s += 2;
576 set_conversion(7227, 100);
577 } else if (strncmp(s, "dd", 2) == 0) {
578 s += 2;
579 set_conversion(1238, 1157);
580 } else if (strncmp(s, "cc", 2) == 0) {
581 s += 2;
582 set_conversion(14856, 1157);
583 } else if (strncmp(s, "pc", 2) == 0) {
584 s += 2;
585 set_conversion(12, 1);
586 } else if (strncmp(s, "nd", 2) == 0) {
587 s += 2;
588 set_conversion(685, 642);
589 } else if (strncmp(s, "nc", 2) == 0) {
590 s += 2;
591 set_conversion(1370, 107);
592 } else {
593 /* Complain about unknown unit and |goto done2| */
594 luaL_error(L, "Illegal unit of measure (pt inserted)");
595 goto DONE2;
596 }
597 cur_val = xn_over_d(cur_val, num, denom);
598 f = (num * f + 0200000 * tex_remainder) / denom;
599 cur_val = cur_val + (f / 0200000);
600 f = f % 0200000;
601 DONE2:
602 ATTACH_FRACTION:
603 if (cur_val >= 040000)
604 arith_error = true;
605 else
606 cur_val = cur_val * 65536 + f;
607 DONE:
608 /* Scan an optional space */
609 c = *s++;
610 if (c != ' ')
611 s--;
612 ATTACH_SIGN:
613 if (arith_error || (abs(cur_val) >= 010000000000)) {
614 /* Report that this dimension is out of range */
615 luaL_error(L, "Dimension too large");
616 cur_val = max_dimen;
617 }
618 if (negative)
619 cur_val = -cur_val;
620 *ret = cur_val;
621 tex_remainder = saved_tex_remainder;
622 arith_error = saved_arith_error;
623 cur_val = saved_cur_val;
624 return s;
625 }
626
627 int dimen_to_number(lua_State * L, const char *s)
628 {
629 int j = 0;
630 const char *d = scan_dimen_part(L, s, &j);
631 if (*d) {
632 luaL_error(L, "conversion failed (trailing junk?)");
633 j = 0;
634 }
635 return j;
636 }
637
638 static int tex_scaledimen(lua_State * L)
639 {
640 int sp;
641 int t = lua_type(L, 1);
642 if (t == LUA_TNUMBER) {
643 sp = lua_roundnumber(L, 1);
644 } else if (t == LUA_TSTRING) {
645 sp = dimen_to_number(L, lua_tostring(L, 1));
646 } else {
647 luaL_error(L, "argument must be a string or a number");
648 return 0;
649 }
650 lua_pushinteger(L, sp);
651 return 1;
652 }
653
654 static int texerror (lua_State * L)
655 {
656 const char **errhlp = NULL;
657 const char *error = luaL_checkstring(L,1);
658 int n = lua_gettop(L);
659 if (n==2 && lua_type(L, n) == LUA_TTABLE) {
660 int i;
661 int l = 1; /* |errhlp| is terminated by a NULL entry */
662 for (i = 1;; i++) {
663 lua_rawgeti(L, n, i);
664 if (lua_type(L,-1) == LUA_TSTRING) {
665 l++;
666 lua_pop(L, 1);
667 } else {
668 lua_pop(L, 1);
669 break;
670 }
671 }
672 if (l>1) {
673 errhlp = xmalloc(l * sizeof(char *));
674 memset(errhlp,0,l * sizeof(char *));
675 for (i = 1;; i++) {
676 lua_rawgeti(L, n, i);
677 if (lua_type(L,-1) == LUA_TSTRING) {
678 errhlp[(i-1)] = lua_tostring(L,-1);
679 lua_pop(L, 1);
680 } else {
681 break;
682 }
683 }
684 }
685 }
686 deletions_allowed = false;
687 tex_error(error, errhlp);
688 if (errhlp)
689 xfree(errhlp);
690 deletions_allowed = true;
691 return 0;
692 }
693
694 static int get_item_index(lua_State * L, int i, int base)
695 {
696 size_t kk;
697 int k;
698 int cur_cs1;
699 const char *s;
700 switch (lua_type(L, i)) {
701 case LUA_TSTRING:
702 s = lua_tolstring(L, i, &kk);
703 cur_cs1 = string_lookup(s, kk);
704 if (cur_cs1 == undefined_control_sequence || cur_cs1 == undefined_cs_cmd)
705 k = -1; /* guarandeed invalid */
706 else
707 k = (equiv(cur_cs1) - base);
708 break;
709 case LUA_TNUMBER:
710 k = luaL_checkinteger(L, i);
711 break;
712 default:
713 luaL_error(L, "argument must be a string or a number");
714 k = -1; /* not a valid index */
715 }
716 return k;
717 }
718
719 #define check_item_global(L,top,isglobal) \
720 if (top == 3 && (lua_type(L,1) == LUA_TSTRING)) { \
721 const char *s = lua_tostring(L, 1); \
722 if (lua_key_eq(s,global)) { \
723 isglobal = 1; \
724 } \
725 }
726
727 #define set_item_index_plus(L, where, base, what, value, is_global, is_assign, set_register, glue) { \
728 size_t len; \
729 const char *str; \
730 int key, err, cs; \
731 int save_global_defs = global_defs_par; \
732 if (is_global) { \
733 global_defs_par = 1; \
734 } \
735 switch (lua_type(L, where)) { \
736 case LUA_TSTRING: \
737 str = lua_tolstring(L, where, &len); \
738 cs = string_lookup(str, len); \
739 if (cs == undefined_control_sequence || cs == undefined_cs_cmd) { \
740 luaL_error(L, "incorrect %s name", what); \
741 } else { \
742 key = equiv(cs) - base; \
743 if (key >= 0 && key <= 65535) { \
744 err = set_register(key, value); \
745 if (err) { \
746 luaL_error(L, "incorrect %s value", what); \
747 } \
748 } else if (is_assign(eq_type(cs))) { \
749 if (glue) { \
750 int a = is_global; \
751 define(equiv(cs), assign_glue_cmd, value); \
752 } else { \
753 assign_internal_value((is_global ? 4 : 0), equiv(cs), value); \
754 } \
755 } else { \
756 luaL_error(L, "incorrect %s name", what); \
757 } \
758 } \
759 break; \
760 case LUA_TNUMBER: \
761 key = luaL_checkinteger(L, where); \
762 if (key>=0 && key <= 65535) { \
763 err = set_register(key, value); \
764 if (err) { \
765 luaL_error(L, "incorrect %s value", what); \
766 } \
767 } else { \
768 luaL_error(L, "incorrect %s index", what); \
769 } \
770 break; \
771 default: \
772 luaL_error(L, "argument of 'set%s' must be a string or a number", what); \
773 } \
774 global_defs_par = save_global_defs; \
775 }
776
777 static int gettex(lua_State * L);
778
779 #define get_item_index_plus(L, where, base, what, value, is_assign, get_register, texget) { \
780 size_t len; \
781 const char *str; \
782 int key, cs; \
783 switch (lua_type(L, where)) { \
784 case LUA_TSTRING: \
785 str = lua_tolstring(L, where, &len); \
786 cs = string_lookup(str, len); \
787 if (cs == undefined_control_sequence || cs == undefined_cs_cmd) { \
788 luaL_error(L, "incorrect %s name", what); \
789 } else { \
790 key = equiv(cs) - base; \
791 if (key >= 0 && key <= 65535) { \
792 value = get_register(key); \
793 } else if (is_assign(eq_type(cs))) { \
794 texget = gettex(L); /* lazy */ \
795 } else { \
796 luaL_error(L, "incorrect %s name", what); \
797 } \
798 } \
799 break; \
800 case LUA_TNUMBER: \
801 key = luaL_checkinteger(L, where); \
802 if (key>=0 && key <= 65535) { \
803 value = get_register(key); \
804 } else { \
805 luaL_error(L, "incorrect %s index", what); \
806 } \
807 break; \
808 default: \
809 luaL_error(L, "argument of 'get%s' must be a string or a number", what); \
810 } \
811 }
812
813 static int isdimen(lua_State * L)
814 {
815 check_register(scaled_base);
816 }
817
818 static int setdimen(lua_State * L)
819 {
820 int isglobal = 0;
821 int value = 0;
822 int top = lua_gettop(L);
823 int t = lua_type(L, top);
824 check_item_global(L,top,isglobal);
825 if (t == LUA_TNUMBER) {
826 value = lua_roundnumber(L, top);
827 } else if (t == LUA_TSTRING) {
828 value = dimen_to_number(L, lua_tostring(L, top));
829 } else {
830 luaL_error(L, "unsupported %s value type","dimen");
831 }
832 set_item_index_plus(L, top-1, scaled_base, "dimen", value, isglobal, is_dim_assign, set_tex_dimen_register, false);
833 return 0;
834 }
835
836 static int getdimen(lua_State * L)
837 {
838 int value = 0;
839 int texget = 0;
840 get_item_index_plus(L, lua_gettop(L), scaled_base, "dimen", value, is_dim_assign, get_tex_dimen_register, texget);
841 if (texget > 0) {
842 return texget;
843 }
844 lua_pushinteger(L, value);
845 return 1;
846 }
847
848 static int isskip(lua_State * L)
849 {
850 check_register(skip_base);
851 }
852
853 static int setskip(lua_State * L)
854 {
855 int isglobal = 0;
856 halfword *value = NULL;
857 int top = lua_gettop(L);
858 check_item_global(L,top,isglobal);
859 value = check_isnode(L, top);
860 if (type(*value) == glue_spec_node) {
861 set_item_index_plus(L, top-1, skip_base, "skip", *value, isglobal, is_glue_assign, set_tex_skip_register, true);
862 } else {
863 luaL_error(L, "glue_spec expected");
864 }
865 return 0;
866 }
867
868 static int getskip(lua_State * L)
869 {
870 int value = 0;
871 int texget = 0;
872 get_item_index_plus(L, lua_gettop(L), skip_base, "skip", value, is_glue_assign, get_tex_skip_register, texget);
873 if (texget > 0) {
874 return texget;
875 }
876 if (value == null) {
877 lua_nodelib_push_fast(L, copy_node(zero_glue));
878 } else {
879 lua_nodelib_push_fast(L, copy_node(value));
880 }
881 return 1;
882 }
883
884 static int setglue(lua_State * L)
885 {
886 int isglobal = 0;
887 int index = 1;
888 halfword value = copy_node(zero_glue);
889 int top = lua_gettop(L);
890 check_item_global(L,top,isglobal);
891 if (isglobal) {
892 index = 2;
893 top -= 1;
894 }
895 /* [global] slot [width] [stretch] [shrink] [stretch_order] [shrink_order] */
896 if (top > 1) { width(value) = lua_roundnumber(L,index+1); }
897 if (top > 2) { stretch(value) = lua_roundnumber(L,index+2); }
898 if (top > 3) { shrink(value) = lua_roundnumber(L,index+3); }
899 if (top > 4) { stretch_order(value) = lua_tointeger(L,index+4); }
900 if (top > 5) { shrink_order(value) = lua_tointeger(L,index+5); }
901 set_item_index_plus(L, index, skip_base, "skip", value, isglobal, is_glue_assign, set_tex_skip_register, true);
902 return 0;
903 }
904
905 static int getglue(lua_State * L)
906 {
907 int value = 0;
908 int texget = 0;
909 int top = lua_gettop(L);
910 int dim = -1;
911 if (top > 1 && lua_type(L,top) == LUA_TBOOLEAN) {
912 dim = lua_toboolean(L,top);
913 top = top - 1;
914 } else {
915 lua_pushboolean(L,1);
916 dim = 1 ;
917 /* no top adaption. somewhat messy, but the gettex fallback checks itself */
918 }
919 /* checks itself for the boolean */
920 get_item_index_plus(L, top, skip_base, "glue", value, is_glue_assign, get_tex_skip_register, texget);
921 if (texget > 0) {
922 return texget;
923 }
924 if (dim == 0) {
925 /* false */
926 if (value == null) {
927 lua_pushinteger(L,0);
928 } else {
929 lua_pushinteger(L,width(value));
930 }
931 return 1;
932 } else {
933 /* true and nil */
934 if (value == null) {
935 lua_pushinteger(L,0);
936 lua_pushinteger(L,0);
937 lua_pushinteger(L,0);
938 lua_pushinteger(L,0);
939 lua_pushinteger(L,0);
940 } else {
941 lua_pushinteger(L,width(value));
942 lua_pushinteger(L,stretch(value));
943 lua_pushinteger(L,shrink(value));
944 lua_pushinteger(L,stretch_order(value));
945 lua_pushinteger(L,shrink_order(value));
946 }
947 return 5;
948 }
949 }
950
951 static int ismuskip(lua_State * L)
952 {
953 check_register(mu_skip_base);
954 }
955
956 static int setmuskip(lua_State * L)
957 {
958 int isglobal = 0;
959 halfword *value = NULL;
960 int top = lua_gettop(L);
961 check_item_global(L,top,isglobal);
962 value = check_isnode(L, top);
963 set_item_index_plus(L, top-1, mu_skip_base, "muskip", *value, isglobal, is_mu_glue_assign, set_tex_mu_skip_register, true);
964 return 0;
965 }
966
967 static int getmuskip(lua_State * L)
968 {
969 int value = 0;
970 int texget = 0;
971 get_item_index_plus(L, lua_gettop(L), mu_skip_base, "muskip", value, is_mu_glue_assign, get_tex_mu_skip_register, texget);
972 if (texget > 0) {
973 return texget;
974 }
975 lua_nodelib_push_fast(L, copy_node(value));
976 return 1;
977 }
978
979 static int setmuglue(lua_State * L)
980 {
981 int isglobal = 0;
982 int index = 1;
983 halfword value = copy_node(zero_glue);
984 int top = lua_gettop(L);
985 check_item_global(L,top,isglobal);
986 if (isglobal) {
987 index = 2;
988 top -= 1;
989 }
990 /* [global] slot [width] [stretch] [shrink] [stretch_order] [shrink_order] */
991 if (top > 1) { width(value) = lua_roundnumber(L,index+1); }
992 if (top > 2) { stretch(value) = lua_roundnumber(L,index+2); }
993 if (top > 3) { shrink(value) = lua_roundnumber(L,index+3); }
994 if (top > 4) { stretch_order(value) = lua_tointeger(L,index+4); }
995 if (top > 5) { shrink_order(value) = lua_tointeger(L,index+5); }
996 set_item_index_plus(L, index, mu_skip_base, "muskip", value, isglobal, is_mu_glue_assign, set_tex_mu_skip_register, true);
997 return 0;
998 }
999
1000 static int getmuglue(lua_State * L)
1001 {
1002 int value = 0;
1003 int texget = 0;
1004 int top = lua_gettop(L);
1005 int dim = -1;
1006 if (top > 1 && lua_type(L,top) == LUA_TBOOLEAN) {
1007 dim = lua_toboolean(L,top);
1008 top = top - 1;
1009 } else {
1010 lua_pushboolean(L,1);
1011 dim = 1 ;
1012 /* no top adaption. somewhat messy, but the gettex fallback checks itself */
1013 }
1014 /* checks itself for the boolean */
1015 get_item_index_plus(L, top, mu_skip_base, "muskip", value, is_mu_glue_assign, get_tex_mu_skip_register, texget);
1016 if (texget > 0) {
1017 return texget;
1018 }
1019 if (dim == 0) {
1020 /* false */
1021 if (value == null) {
1022 lua_pushinteger(L,0);
1023 } else {
1024 lua_pushinteger(L,width(value));
1025 }
1026 return 1;
1027 } else {
1028 /* true and nil */
1029 if (value == null) {
1030 lua_pushinteger(L,0);
1031 lua_pushinteger(L,0);
1032 lua_pushinteger(L,0);
1033 lua_pushinteger(L,0);
1034 lua_pushinteger(L,0);
1035 } else {
1036 lua_pushinteger(L,width(value));
1037 lua_pushinteger(L,stretch(value));
1038 lua_pushinteger(L,shrink(value));
1039 lua_pushinteger(L,stretch_order(value));
1040 lua_pushinteger(L,shrink_order(value));
1041 }
1042 return 5;
1043 }
1044 }
1045
1046 static int iscount(lua_State * L)
1047 {
1048 check_register(count_base);
1049 }
1050
1051 static int setcount(lua_State * L)
1052 {
1053 int t;
1054 int isglobal = 0;
1055 int value = 0;
1056 int top = lua_gettop(L);
1057 check_item_global(L,top,isglobal);
1058 t = lua_type(L,top);
1059 if (t == LUA_TNUMBER) {
1060 value = lua_tointeger(L, top);
1061 } else {
1062 luaL_error(L, "unsupported %s value type","count");
1063 }
1064 set_item_index_plus(L, top-1, count_base, "count", value, isglobal, is_int_assign, set_tex_count_register, false);
1065 return 0;
1066 }
1067
1068 static int getcount(lua_State * L)
1069 {
1070 int value = 0;
1071 int texget = 0;
1072 get_item_index_plus(L, lua_gettop(L), count_base, "count", value, is_int_assign, get_tex_count_register, texget);
1073 if (texget > 0) {
1074 return texget;
1075 }
1076 lua_pushinteger(L, value);
1077 return 1;
1078 }
1079
1080 static int isattribute(lua_State * L)
1081 {
1082 check_register(attribute_base);
1083 }
1084
1085 /* there are no system set attributes so this is a bit overkill */
1086
1087 static int setattribute(lua_State * L)
1088 {
1089 int t;
1090 int isglobal = 0;
1091 int value = 0;
1092 int top = lua_gettop(L);
1093 check_item_global(L,top,isglobal);
1094 t = lua_type(L,top);
1095 if (t == LUA_TNUMBER) {
1096 value = lua_tointeger(L, top);
1097 } else {
1098 luaL_error(L, "unsupported %s value type","attribute");
1099 }
1100 set_item_index_plus(L, top-1, attribute_base, "attribute", value, isglobal, is_attr_assign, set_tex_attribute_register, false);
1101 return 0;
1102 }
1103
1104 static int getattribute(lua_State * L)
1105 {
1106 int value = 0;
1107 int texget = 0;
1108 get_item_index_plus(L, lua_gettop(L), attribute_base, "attribute", value, is_attr_assign, get_tex_attribute_register, texget);
1109 if (texget > 0) {
1110 return texget;
1111 }
1112 lua_pushinteger(L, value);
1113 return 1;
1114 }
1115
1116 /* todo: we can avoid memcpy as there is no need to go through the pool */
1117
1118 /* use string_to_toks */
1119
1120 static int istoks(lua_State * L)
1121 {
1122 check_register(toks_base);
1123 }
1124
1125 static int settoks(lua_State * L)
1126 {
1127 int i, err, k;
1128 lstring str;
1129 char *s;
1130 const char *ss;
1131 int is_global = 0;
1132 int save_global_defs = global_defs_par;
1133 int n = lua_gettop(L);
1134 if (n == 3 && (lua_type(L,1) == LUA_TSTRING)) {
1135 const char *s = lua_tostring(L, 1);
1136 if (lua_key_eq(s,global))
1137 is_global = 1;
1138 }
1139 if (is_global)
1140 global_defs_par = 1;
1141 i = lua_gettop(L);
1142 if (lua_type(L,i) != LUA_TSTRING) {
1143 luaL_error(L, "unsupported value type");
1144 }
1145 ss = lua_tolstring(L, i, &str.l);
1146 s = xmalloc (str.l+1);
1147 memcpy (s, ss, str.l+1);
1148 str.s = (unsigned char *)s;
1149 k = get_item_index(L, (i - 1), toks_base);
1150 check_index_range(k, "settoks");
1151 err = set_tex_toks_register(k, str);
1152 xfree(str.s);
1153 global_defs_par = save_global_defs;
1154 if (err) {
1155 luaL_error(L, "incorrect value");
1156 }
1157 return 0;
1158 }
1159
1160 static int scantoks(lua_State * L)
1161 {
1162 int i, err, k, c;
1163 lstring str;
1164 char *s;
1165 const char *ss;
1166 int is_global = 0;
1167 int save_global_defs = global_defs_par;
1168 int n = lua_gettop(L);
1169 if (n == 4 && (lua_type(L,1) == LUA_TSTRING)) {
1170 const char *s = lua_tostring(L, 1);
1171 if (lua_key_eq(s,global))
1172 is_global = 1;
1173 }
1174 /* action : vsettokscct(L, is_global); */
1175 if (is_global)
1176 global_defs_par = 1;
1177 i = lua_gettop(L);
1178 if (lua_type(L,i) != LUA_TSTRING) {
1179 luaL_error(L, "unsupported value type");
1180 }
1181 ss = lua_tolstring(L, i, &str.l);
1182 s = xmalloc (str.l+1);
1183 memcpy (s, ss, str.l+1);
1184 str.s = (unsigned char *)s;
1185 k = get_item_index(L, (i - 2), toks_base);
1186 c = luaL_checkinteger(L, i - 1);
1187 check_index_range(k, "settoks");
1188 err = scan_tex_toks_register(k, c, str);
1189 xfree(str.s);
1190 global_defs_par = save_global_defs;
1191 if (err) {
1192 luaL_error(L, "incorrect value");
1193 }
1194 return 0;
1195 }
1196
1197 static int gettoks(lua_State * L)
1198 {
1199 char *ss;
1200 str_number t;
1201 int k = get_item_index(L, lua_gettop(L), toks_base);
1202 check_index_range(k, "gettoks");
1203 t = get_tex_toks_register(k);
1204 ss = makecstring(t);
1205 lua_pushstring(L, ss);
1206 free(ss);
1207 flush_str(t);
1208 return 1;
1209 }
1210
1211 static int getmark(lua_State *L)
1212 {
1213 if (lua_gettop(L) == 0) {
1214 lua_pushinteger(L, biggest_used_mark);
1215 return 1;
1216 } else if (lua_type(L, 1) == LUA_TSTRING) {
1217 int num = luaL_optinteger(L, 2, 0);
1218 if (num >= 0 && num <= biggest_used_mark) {
1219 int ptr = null;
1220 const char *s = lua_tostring(L, 1);
1221 if (lua_key_eq(s, top)) {
1222 ptr = top_marks_array[num];
1223 } else if (lua_key_eq(s, first)) {
1224 ptr = first_marks_array[num];
1225 } else if (lua_key_eq(s, bottom)) {
1226 ptr = bot_marks_array[num];
1227 } else if (lua_key_eq(s, splitfirst)) {
1228 ptr = split_first_marks_array[num];
1229 } else if (lua_key_eq(s, splitbottom)) {
1230 ptr = split_bot_marks_array[num];
1231 }
1232 if (ptr) {
1233 char *str = tokenlist_to_cstring(ptr, 1, NULL);
1234 lua_pushstring(L, str);
1235 free(str);
1236 return 1;
1237 }
1238 }
1239 }
1240 lua_pushnil(L);
1241 return 1;
1242 }
1243
1244 static int get_box_id(lua_State * L, int i, boolean report)
1245 {
1246 const char *s;
1247 int cur_cs1, cur_cmd1;
1248 size_t k = 0;
1249 int j = -1;
1250 switch (lua_type(L, i)) {
1251 case LUA_TSTRING:
1252 s = lua_tolstring(L, i, &k);
1253 cur_cs1 = string_lookup(s, k);
1254 cur_cmd1 = eq_type(cur_cs1);
1255 if (cur_cmd1 == char_given_cmd ||
1256 cur_cmd1 == math_given_cmd) {
1257 j = equiv(cur_cs1);
1258 }
1259 break;
1260 case LUA_TNUMBER:
1261 j = lua_tointeger(L, (i));
1262 break;
1263 default:
1264 if (report) {
1265 luaL_error(L, "argument must be a string or a number");
1266 }
1267 j = -1; /* not a valid box id */
1268 }
1269 return j;
1270 }
1271
1272 static int getbox(lua_State * L)
1273 {
1274 int t;
1275 int k = get_box_id(L, -1, true);
1276 check_index_range(k, "getbox");
1277 t = get_tex_box_register(k);
1278 nodelist_to_lua(L, t);
1279 return 1;
1280 }
1281
1282 static int splitbox(lua_State * L)
1283 {
1284 const char *s;
1285 int k = get_box_id(L, 1, true);
1286 check_index_range(k, "splitbox");
1287 if (lua_isnumber(L, 2)) {
1288 int m = 1;
1289 if (lua_type(L, 3) == LUA_TSTRING) {
1290 s = lua_tostring(L, 3);
1291 if (lua_key_eq(s, exactly)) {
1292 m = 0;
1293 } else if (lua_key_eq(s, additional)) {
1294 m = 1;
1295 }
1296 } else if (lua_type(L, 3) == LUA_TNUMBER) {
1297 m = (int) lua_tointeger(L, 3);
1298 }
1299 if ((m<0) || (m>1)) {
1300 luaL_error(L, "wrong mode in splitbox");
1301 }
1302 nodelist_to_lua(L, vsplit(k,lua_roundnumber(L,2),m));
1303 } else {
1304 /* maybe a warning */
1305 lua_pushnil(L);
1306 }
1307 return 1;
1308 }
1309
1310 static int isbox(lua_State * L)
1311 {
1312 int k = get_box_id(L, -1, false);
1313 lua_pushboolean(L,(k>=0 && k<=65535));
1314 return 1;
1315 }
1316
1317 static int vsetbox(lua_State * L, int is_global)
1318 {
1319 int j, k, err, t;
1320 int save_global_defs;
1321 k = get_box_id(L, -2, true);
1322 check_index_range(k, "setbox");
1323 t = lua_type(L, -1);
1324 if (t == LUA_TBOOLEAN) {
1325 j = lua_toboolean(L, -1);
1326 if (j == 0) {
1327 j = null;
1328 } else {
1329 return 0;
1330 }
1331 } else if (t == LUA_TNIL) {
1332 j = null;
1333 } else {
1334 j = nodelist_from_lua(L,-1);
1335 if (j != null && type(j) != hlist_node && type(j) != vlist_node) {
1336 luaL_error(L, "setbox: incompatible node type (%s)\n", get_node_name(type(j), subtype(j)));
1337 return 0;
1338 }
1339 }
1340 save_global_defs = global_defs_par;
1341 if (is_global) {
1342 global_defs_par = 1;
1343 }
1344 err = set_tex_box_register(k, j);
1345 global_defs_par = save_global_defs;
1346 if (err) {
1347 luaL_error(L, "incorrect value");
1348 }
1349 return 0;
1350 }
1351
1352 static int setbox(lua_State * L)
1353 {
1354 int isglobal = 0;
1355 int n = lua_gettop(L);
1356 if (n == 3 && (lua_type(L,1) == LUA_TSTRING)) {
1357 const char *s = lua_tostring(L, 1);
1358 if (lua_key_eq(s,global))
1359 isglobal = 1;
1360 }
1361 return vsetbox(L, isglobal);
1362 }
1363
1364 #define check_char_range(j,s,lim) \
1365 if (j<0 || j >= lim) { \
1366 luaL_error(L, "incorrect character value %d for tex.%s()", (int) j, s); \
1367 }
1368
1369 static int setcode (lua_State *L, void (*setone)(int,halfword,quarterword),
1370 void (*settwo)(int,halfword,quarterword), const char *name, int lim)
1371 {
1372 int ch;
1373 halfword val, ucval;
1374 int level = cur_level;
1375 int n = lua_gettop(L);
1376 int f = 1;
1377 if (n>1 && lua_type(L,1) == LUA_TTABLE)
1378 f++;
1379 if (n>2 && (lua_type(L,f) == LUA_TSTRING)) {
1380 const char *s = lua_tostring(L, f);
1381 if (lua_key_eq(s,global)) {
1382 level = level_one;
1383 f++;
1384 }
1385 }
1386 ch = luaL_checkinteger(L, f);
1387 check_char_range(ch, name, 65536*17);
1388 val = (halfword) luaL_checkinteger(L, f+1);
1389 check_char_range(val, name, lim);
1390 (setone)(ch, val, level);
1391 if (settwo != NULL && n-f == 2) {
1392 ucval = (halfword) luaL_checkinteger(L, f+2);
1393 check_char_range(ucval, name, lim);
1394 (settwo)(ch, ucval, level);
1395 }
1396 return 0;
1397 }
1398
1399 static int setlccode(lua_State * L)
1400 {
1401 return setcode(L, &set_lc_code, &set_uc_code, "setlccode", 65536*17);
1402 }
1403
1404 static int getlccode(lua_State * L)
1405 {
1406 int ch = luaL_checkinteger(L, -1);
1407 check_char_range(ch, "getlccode", 65536*17);
1408 lua_pushinteger(L, get_lc_code(ch));
1409 return 1;
1410 }
1411
1412 static int setuccode(lua_State * L)
1413 {
1414 return setcode(L, &set_uc_code, &set_lc_code, "setuccode", 65536*17);
1415 }
1416
1417 static int getuccode(lua_State * L)
1418 {
1419 int ch = luaL_checkinteger(L, -1);
1420 check_char_range(ch, "getuccode", 65536*17);
1421 lua_pushinteger(L, get_uc_code(ch));
1422 return 1;
1423 }
1424
1425 static int setsfcode(lua_State * L)
1426 {
1427 return setcode(L, &set_sf_code, NULL, "setsfcode", 32768);
1428 }
1429
1430 static int getsfcode(lua_State * L)
1431 {
1432 int ch = luaL_checkinteger(L, -1);
1433 check_char_range(ch, "getsfcode", 65536*17);
1434 lua_pushinteger(L, get_sf_code(ch));
1435 return 1;
1436 }
1437
1438 static int setcatcode(lua_State * L)
1439 {
1440 int ch;
1441 halfword val;
1442 int level = cur_level;
1443 int cattable = cat_code_table_par;
1444 int n = lua_gettop(L);
1445 int f = 1;
1446 if (n>1 && lua_type(L,1) == LUA_TTABLE)
1447 f++;
1448 if (n>2 && (lua_type(L,f) == LUA_TSTRING)) {
1449 const char *s = lua_tostring(L, f);
1450 if (lua_key_eq(s,global)) {
1451 level = level_one;
1452 f++;
1453 }
1454 }
1455 if (n-f == 2) {
1456 cattable = luaL_checkinteger(L, -3);
1457 }
1458 ch = luaL_checkinteger(L, -2);
1459 check_char_range(ch, "setcatcode", 65536*17);
1460 val = (halfword) luaL_checkinteger(L, -1);
1461 check_char_range(val, "setcatcode", 16);
1462 set_cat_code(cattable, ch, val, level);
1463 return 0;
1464 }
1465
1466 static int getcatcode(lua_State * L)
1467 {
1468 int cattable = cat_code_table_par;
1469 int ch = luaL_checkinteger(L, -1);
1470 if (lua_gettop(L)>=2 && lua_type(L,-2)==LUA_TNUMBER) {
1471 cattable = luaL_checkinteger(L, -2);
1472 }
1473 check_char_range(ch, "getcatcode", 65536*17);
1474 lua_pushinteger(L, get_cat_code(cattable, ch));
1475 return 1;
1476 }
1477
1478 /*
1479
1480 static int setmathcode(lua_State * L)
1481 {
1482 int ch;
1483 halfword cval, fval, chval;
1484 int level = cur_level;
1485 int n = lua_gettop(L);
1486 int f = 1;
1487 if (n>1 && lua_type(L,1) == LUA_TTABLE)
1488 f++;
1489 if (n>2 && (lua_type(L,f) == LUA_TSTRING)) {
1490 const char *s = lua_tostring(L, f);
1491 if (lua_key_eq(s,global)) {
1492 level = level_one;
1493 f++;
1494 }
1495 }
1496 if (n-f!=1 || lua_type(L,f+1) != LUA_TTABLE) {
1497 luaL_error(L, "Bad arguments for tex.setmathcode()");
1498 }
1499 ch = luaL_checkinteger(L, -2);
1500 check_char_range(ch, "setmathcode", 65536*17);
1501
1502 lua_rawgeti(L, -1, 1);
1503 cval = (halfword) luaL_checkinteger(L, -1);
1504 lua_rawgeti(L, -2, 2);
1505 fval = (halfword) luaL_checkinteger(L, -1);
1506 lua_rawgeti(L, -3, 3);
1507 chval = (halfword) luaL_checkinteger(L, -1);
1508 lua_pop(L,3);
1509
1510 check_char_range(cval, "setmathcode", 8);
1511 check_char_range(fval, "setmathcode", 256);
1512 check_char_range(chval, "setmathcode", 65536*17);
1513 set_math_code(ch, cval,fval, chval, (quarterword) (level));
1514 return 0;
1515 }
1516
1517 */
1518
1519 /*
1520 [global] code { c f ch }
1521 [global] code c f ch (a bit easier on memory, counterpart of getter)
1522 */
1523
1524 static int setmathcode(lua_State * L)
1525 {
1526 int ch;
1527 halfword cval, fval, chval;
1528 int level = cur_level;
1529 int f = 1;
1530 if (lua_type(L,1) == LUA_TSTRING) {
1531 const char *s = lua_tostring(L,1);
1532 if (lua_key_eq(s,global)) {
1533 level = level_one;
1534 f = 2;
1535 }
1536 }
1537 ch = luaL_checkinteger(L, f);
1538 check_char_range(ch, "setmathcode", 65536*17);
1539 f += 1 ;
1540 if (lua_type(L,f) == LUA_TNUMBER) {
1541 cval = luaL_checkinteger(L, f);
1542 fval = luaL_checkinteger(L, f+1);
1543 chval = luaL_checkinteger(L, f+2);
1544 } else if (lua_type(L,f) == LUA_TTABLE) {
1545 lua_rawgeti(L, f, 1);
1546 cval = (halfword) luaL_checkinteger(L, -1);
1547 lua_rawgeti(L, f, 2);
1548 fval = (halfword) luaL_checkinteger(L, -1);
1549 lua_rawgeti(L, f, 3);
1550 chval = (halfword) luaL_checkinteger(L, -1);
1551 lua_pop(L,3);
1552 } else {
1553 luaL_error(L, "Bad arguments for tex.setmathcode()");
1554 return 0;
1555 }
1556 check_char_range(cval, "setmathcode", 8);
1557 check_char_range(fval, "setmathcode", 256);
1558 check_char_range(chval, "setmathcode", 65536*17);
1559 set_math_code(ch, cval,fval, chval, (quarterword) (level));
1560 return 0;
1561 }
1562
1563 static int getmathcode(lua_State * L)
1564 {
1565 mathcodeval mval = { 0, 0, 0 };
1566 int ch = luaL_checkinteger(L, -1);
1567 check_char_range(ch, "getmathcode", 65536*17);
1568 mval = get_math_code(ch);
1569 lua_newtable(L);
1570 lua_pushinteger(L,mval.class_value);
1571 lua_rawseti(L, -2, 1);
1572 lua_pushinteger(L,mval.family_value);
1573 lua_rawseti(L, -2, 2);
1574 lua_pushinteger(L,mval.character_value);
1575 lua_rawseti(L, -2, 3);
1576 return 1;
1577 }
1578
1579 static int getmathcodes(lua_State * L)
1580 {
1581 mathcodeval mval = { 0, 0, 0 };
1582 int ch = luaL_checkinteger(L, -1);
1583 check_char_range(ch, "getmathcodes", 65536*17);
1584 mval = get_math_code(ch);
1585 lua_pushinteger(L,mval.class_value);
1586 lua_pushinteger(L,mval.family_value);
1587 lua_pushinteger(L,mval.character_value);
1588 return 3;
1589 }
1590
1591 /*
1592
1593 static int setdelcode(lua_State * L)
1594 {
1595 int ch;
1596 halfword sfval, scval, lfval, lcval;
1597 int level = cur_level;
1598 int n = lua_gettop(L);
1599 int f = 1;
1600 if (n>1 && lua_type(L,1) == LUA_TTABLE)
1601 f++;
1602 if (n>2 && (lua_type(L,f) == LUA_TSTRING)) {
1603 const char *s = lua_tostring(L, f);
1604 if (lua_key_eq(s,global)) {
1605 level = level_one;
1606 f++;
1607 }
1608 }
1609 if (n-f!=1 || lua_type(L,f+1) != LUA_TTABLE) {
1610 luaL_error(L, "Bad arguments for tex.setdelcode()");
1611 }
1612 ch = luaL_checkinteger(L, -2);
1613 check_char_range(ch, "setdelcode", 65536*17);
1614 lua_rawgeti(L, -1, 1);
1615 sfval = (halfword) luaL_checkinteger(L, -1);
1616 lua_rawgeti(L, -2, 2);
1617 scval = (halfword) luaL_checkinteger(L, -1);
1618 lua_rawgeti(L, -3, 3);
1619 lfval = (halfword) luaL_checkinteger(L, -1);
1620 lua_rawgeti(L, -4, 4);
1621 lcval = (halfword) luaL_checkinteger(L, -1);
1622 lua_pop(L,4);
1623
1624 check_char_range(sfval, "setdelcode", 256);
1625 check_char_range(scval, "setdelcode", 65536*17);
1626 check_char_range(lfval, "setdelcode", 256);
1627 check_char_range(lcval, "setdelcode", 65536*17);
1628 set_del_code(ch, sfval, scval, lfval, lcval, (quarterword) (level));
1629
1630 return 0;
1631 }
1632
1633 */
1634
1635 /*
1636 [global] code { c f ch }
1637 [global] code c f ch (a bit easier on memory, counterpart of getter)
1638 */
1639
1640 static int setdelcode(lua_State * L)
1641 {
1642 int ch;
1643 halfword sfval, scval, lfval, lcval;
1644 int level = cur_level;
1645 int f = 1;
1646 if (lua_type(L,1) == LUA_TSTRING) {
1647 const char *s = lua_tostring(L,1);
1648 if (lua_key_eq(s,global)) {
1649 level = level_one;
1650 f = 2;
1651 }
1652 }
1653 ch = luaL_checkinteger(L, f);
1654 check_char_range(ch, "setdelcode", 65536*17);
1655 f += 1;
1656 if (lua_type(L,f) == LUA_TNUMBER) {
1657 sfval = luaL_checkinteger(L, f);
1658 scval = luaL_checkinteger(L, f+1);
1659 lfval = luaL_checkinteger(L, f+2);
1660 lcval = luaL_checkinteger(L, f+3);
1661 } else if (lua_type(L,f) == LUA_TTABLE) {
1662 lua_rawgeti(L, f, 1);
1663 sfval = (halfword) luaL_checkinteger(L, -1);
1664 lua_rawgeti(L, f, 2);
1665 scval = (halfword) luaL_checkinteger(L, -1);
1666 lua_rawgeti(L, f, 3);
1667 lfval = (halfword) luaL_checkinteger(L, -1);
1668 lua_rawgeti(L, f, 4);
1669 lcval = (halfword) luaL_checkinteger(L, -1);
1670 lua_pop(L,4);
1671 } else {
1672 luaL_error(L, "Bad arguments for tex.setdelcode()");
1673 return 0;
1674 }
1675 check_char_range(sfval, "setdelcode", 256);
1676 check_char_range(scval, "setdelcode", 65536*17);
1677 check_char_range(lfval, "setdelcode", 256);
1678 check_char_range(lcval, "setdelcode", 65536*17);
1679 set_del_code(ch, sfval, scval, lfval, lcval, (quarterword) (level));
1680 return 0;
1681 }
1682
1683 static int getdelcode(lua_State * L)
1684 {
1685 delcodeval mval = { 0, 0, 0, 0, 0 };
1686 int ch = luaL_checkinteger(L, -1);
1687 check_char_range(ch, "getdelcode", 65536*17);
1688 mval = get_del_code(ch);
1689 lua_newtable(L);
1690 lua_pushinteger(L,mval.small_family_value);
1691 lua_rawseti(L, -2, 1);
1692 lua_pushinteger(L,mval.small_character_value);
1693 lua_rawseti(L, -2, 2);
1694 lua_pushinteger(L,mval.large_family_value);
1695 lua_rawseti(L, -2, 3);
1696 lua_pushinteger(L,mval.large_character_value);
1697 lua_rawseti(L, -2, 4);
1698 return 1;
1699 }
1700
1701 static int getdelcodes(lua_State * L)
1702 {
1703 delcodeval mval = { 0, 0, 0, 0, 0 };
1704 int ch = luaL_checkinteger(L, -1);
1705 check_char_range(ch, "getdelcodes", 65536*17);
1706 mval = get_del_code(ch);
1707 lua_pushinteger(L,mval.small_family_value);
1708 lua_pushinteger(L,mval.small_character_value);
1709 lua_pushinteger(L,mval.large_family_value);
1710 lua_pushinteger(L,mval.large_character_value);
1711 return 4;
1712 }
1713
1714 static int settex(lua_State * L)
1715 {
1716 const char *st;
1717 int texstr, isprim;
1718 size_t k;
1719 int cur_cs1, cur_cmd1;
1720 int isglobal = 0;
1721 int j = 0;
1722 int i = lua_gettop(L);
1723 if (lua_type(L,i-1) == LUA_TSTRING) {
1724 st = lua_tolstring(L, (i - 1), &k);
1725 if (lua_key_eq(st,prevdepth)) {
1726 if (lua_type(L, i) == LUA_TNUMBER) {
1727 cur_list.prev_depth_field = lua_roundnumber(L, i);
1728 } else if (lua_type(L, i) == LUA_TSTRING) {
1729 cur_list.prev_depth_field = dimen_to_number(L, lua_tostring(L, i));
1730 } else {
1731 luaL_error(L, "unsupported value type");
1732 }
1733 return 0;
1734 } else if (lua_key_eq(st,prevgraf)) {
1735 if (lua_type(L, i) == LUA_TNUMBER) {
1736 cur_list.pg_field = lua_tointeger(L, i);
1737 } else {
1738 luaL_error(L, "unsupported value type");
1739 }
1740 return 0;
1741 } else if (lua_key_eq(st,spacefactor)) {
1742 if (lua_type(L, i) == LUA_TNUMBER) {
1743 cur_list.space_factor_field = lua_roundnumber(L, i);
1744 } else {
1745 luaL_error(L, "unsupported value type");
1746 }
1747 return 0;
1748 }
1749 texstr = maketexlstring(st, k);
1750 isprim = is_primitive(texstr);
1751 flush_str(texstr);
1752 if (isprim) {
1753 if (i == 3 && (lua_type(L,1) == LUA_TSTRING)) {
1754 const char *s = lua_tostring(L, 1);
1755 if (lua_key_eq(s,global))
1756 isglobal = 1;
1757 }
1758 cur_cs1 = string_lookup(st, k);
1759 flush_str(texstr);
1760 cur_cmd1 = eq_type(cur_cs1);
1761 if (is_int_assign(cur_cmd1)) {
1762 if (lua_type(L, i) == LUA_TNUMBER) {
1763 int luai = lua_tointeger(L, i);
1764 assign_internal_value((isglobal ? 4 : 0), equiv(cur_cs1), luai);
1765 } else {
1766 luaL_error(L, "unsupported value type");
1767 }
1768 } else if (is_dim_assign(cur_cmd1)) {
1769 if (lua_type(L, i) == LUA_TNUMBER) {
1770 j = lua_roundnumber(L, i);
1771 } else if (lua_type(L, i) == LUA_TSTRING) {
1772 j = dimen_to_number(L, lua_tostring(L, i));
1773 } else {
1774 luaL_error(L, "unsupported value type");
1775 }
1776 assign_internal_value((isglobal ? 4 : 0), equiv(cur_cs1), j);
1777 } else if (is_glue_assign(cur_cmd1)) {
1778 int a = isglobal;
1779 if (lua_type(L, i) == LUA_TNUMBER) {
1780 halfword value = copy_node(zero_glue);
1781 width(value) = lua_roundnumber(L,i);
1782 if (i > 1) { stretch(value) = lua_roundnumber(L,i+1); }
1783 if (i > 3) { shrink(value) = lua_roundnumber(L,i+2); }
1784 if (i > 4) { stretch_order(value) = lua_tointeger(L,i+3); }
1785 if (i > 5) { shrink_order(value) = lua_tointeger(L,i+4); }
1786 define(equiv(cur_cs1), assign_glue_cmd, value);
1787 } else {
1788 halfword *j1 = check_isnode(L, i); /* the value */
1789 define(equiv(cur_cs1), assign_glue_cmd, *j1);
1790 }
1791 } else if (is_toks_assign(cur_cmd1)) {
1792 if (lua_type(L,i) == LUA_TSTRING) {
1793 j = tokenlist_from_lua(L, -1); /* uses stack -1 */
1794 assign_internal_value((isglobal ? 4 : 0), equiv(cur_cs1), j);
1795
1796 } else {
1797 luaL_error(L, "unsupported value type");
1798 }
1799 } else if (lua_istable(L, (i - 2))) {
1800 /*
1801 people may want to add keys that are also primitives |tex.wd| for example)
1802 so creating an error is not right here
1803 */
1804 lua_rawset(L, (i - 2));
1805 }
1806 } else if (lua_istable(L, (i - 2))) {
1807 lua_rawset(L, (i - 2));
1808 }
1809 } else if (lua_istable(L, (i - 2))) {
1810 lua_rawset(L, (i - 2));
1811 }
1812 return 0;
1813 }
1814
1815 /* todo: some will to the pdf namespace .. ignore > 31 */
1816
1817 static int do_convert(lua_State * L, int cur_code)
1818 {
1819 int texstr;
1820 int i = -1;
1821 char *str = NULL;
1822 switch (cur_code) {
1823 /* ignored (yet) */
1824
1825 case insert_ht_code: /* arg <register int> */
1826 case lua_code: /* arg complex */
1827 case lua_escape_string_code: /* arg token list */
1828 case left_margin_kern_code: /* arg box */
1829 case right_margin_kern_code: /* arg box */
1830 case string_code: /* arg token */
1831 case cs_string_code: /* arg token */
1832 case meaning_code: /* arg token */
1833 break;
1834
1835 /* the next fall through, and come from 'official' indices! */
1836
1837 case font_name_code: /* arg fontid */
1838 case font_identifier_code: /* arg fontid */
1839 case uniform_deviate_code: /* arg int */
1840 case number_code: /* arg int */
1841 case roman_numeral_code: /* arg int */
1842
1843 if (lua_gettop(L) < 1) {
1844 /* error */
1845 }
1846 i = lua_tointeger(L, 1);
1847
1848 /* these fall through! */
1849
1850 default:
1851 /* no backend here */
1852 if (cur_code < 32) {
1853 texstr = the_convert_string(cur_code, i);
1854 if (texstr) {
1855 str = makecstring(texstr);
1856 flush_str(texstr);
1857 }
1858 }
1859 }
1860 /* end */
1861 if (str) {
1862 lua_pushstring(L, str);
1863 free(str);
1864 } else {
1865 lua_pushnil(L);
1866 }
1867 return 1;
1868 }
1869
1870 static int do_scan_internal(lua_State * L, int cur_cmd1, int cur_code, int values)
1871 {
1872 int texstr;
1873 int retval = 1 ;
1874 char *str = NULL;
1875 int save_cur_val, save_cur_val_level;
1876 save_cur_val = cur_val;
1877 save_cur_val_level = cur_val_level;
1878 scan_something_simple(cur_cmd1, cur_code);
1879 switch (cur_val_level) {
1880 case int_val_level:
1881 case dimen_val_level:
1882 case attr_val_level:
1883 lua_pushinteger(L, cur_val);
1884 break;
1885 case glue_val_level:
1886 case mu_val_level:
1887 if (values == 0) {
1888 lua_pushinteger(L,width(cur_val));
1889 flush_node(cur_val);
1890 } else if (values == 1) {
1891 lua_pushinteger(L,width(cur_val));
1892 lua_pushinteger(L,stretch(cur_val));
1893 lua_pushinteger(L,shrink(cur_val));
1894 lua_pushinteger(L,stretch_order(cur_val));
1895 lua_pushinteger(L,shrink_order(cur_val));
1896 flush_node(cur_val);
1897 retval = 5;
1898 } else {
1899 lua_nodelib_push_fast(L, cur_val);
1900 }
1901 break;
1902 default:
1903 texstr = the_scanned_result();
1904 str = makecstring(texstr);
1905 if (str) {
1906 lua_pushstring(L, str);
1907 free(str);
1908 } else {
1909 lua_pushnil(L);
1910 }
1911 flush_str(texstr);
1912 break;
1913 }
1914 cur_val = save_cur_val;
1915 cur_val_level = save_cur_val_level;
1916 return retval;
1917 }
1918
1919 static int do_lastitem(lua_State * L, int cur_code)
1920 {
1921 int retval = 1;
1922 switch (cur_code) {
1923 /* the next two do not actually exist */
1924 case lastattr_code:
1925 case attrexpr_code:
1926 lua_pushnil(L);
1927 break;
1928 /* the expressions do something complicated with arguments, yuck */
1929 case numexpr_code:
1930 case dimexpr_code:
1931 case glueexpr_code:
1932 case muexpr_code:
1933 lua_pushnil(L);
1934 break;
1935 /* these read a glue or muglue, todo */
1936 case mu_to_glue_code:
1937 case glue_to_mu_code:
1938 case glue_stretch_order_code:
1939 case glue_shrink_order_code:
1940 case glue_stretch_code:
1941 case glue_shrink_code:
1942 lua_pushnil(L);
1943 break;
1944 /* these read a fontid and a char, todo */
1945 case font_char_wd_code:
1946 case font_char_ht_code:
1947 case font_char_dp_code:
1948 case font_char_ic_code:
1949 lua_pushnil(L);
1950 break;
1951 /* these read an integer, todo */
1952 case par_shape_length_code:
1953 case par_shape_indent_code:
1954 case par_shape_dimen_code:
1955 lua_pushnil(L);
1956 break;
1957 case lastpenalty_code:
1958 case lastkern_code:
1959 case lastskip_code:
1960 case last_node_type_code:
1961 case input_line_no_code:
1962 case badness_code:
1963 case last_saved_box_resource_index_code:
1964 case last_saved_image_resource_index_code:
1965 case last_saved_image_resource_pages_code:
1966 case last_x_pos_code:
1967 case last_y_pos_code:
1968 case random_seed_code:
1969 case luatex_version_code:
1970 case eTeX_minor_version_code:
1971 case eTeX_version_code:
1972 case current_group_level_code:
1973 case current_group_type_code:
1974 case current_if_level_code:
1975 case current_if_type_code:
1976 case current_if_branch_code:
1977 retval = do_scan_internal(L, last_item_cmd, cur_code, -1);
1978 break;
1979 default:
1980 lua_pushnil(L);
1981 break;
1982 }
1983 return retval;
1984 }
1985
1986 static int tex_setmathparm(lua_State * L)
1987 {
1988 int i, j;
1989 int k;
1990 int l = cur_level;
1991 void *p;
1992 int n = lua_gettop(L);
1993 if ((n == 3) || (n == 4)) {
1994 if (n == 4 && (lua_type(L,1) == LUA_TSTRING)) {
1995 const char *s = lua_tostring(L, 1);
1996 if (lua_key_eq(s,global))
1997 l = 1;
1998 }
1999 i = luaL_checkoption(L, (n - 2), NULL, math_param_names);
2000 j = luaL_checkoption(L, (n - 1), NULL, math_style_names);
2001 if (i < 0 || j < 0) {
2002 /* invalid spec, just ignore it */
2003 } else if (i>=math_param_first_mu_glue) {
2004 p = lua_touserdata(L, n);
2005 k = *((halfword *)p);
2006 def_math_param(i, j, (scaled) k, l);
2007 } else if (lua_type(L, n) == LUA_TNUMBER) {
2008 k = lua_roundnumber(L, n);
2009 def_math_param(i, j, (scaled) k, l);
2010 } else {
2011 luaL_error(L, "argument must be a number");
2012 }
2013 }
2014 return 0;
2015 }
2016
2017 static int tex_getmathparm(lua_State * L)
2018 {
2019 if (lua_gettop(L) == 2) {
2020 int i = luaL_checkoption(L, 1, NULL, math_param_names);
2021 int j = luaL_checkoption(L, 2, NULL, math_style_names);
2022 if (i < 0 || j < 0) {
2023 lua_pushnil(L);
2024 } else {
2025 scaled k = get_math_param(i, j);
2026 if (i >= math_param_first_mu_glue) {
2027 if (k <= thick_mu_skip_code) {
2028 k = glue_par(k);
2029 }
2030 lua_nodelib_push_fast(L, k);
2031 } else {
2032 lua_pushinteger(L, k);
2033 }
2034 }
2035 } else {
2036 lua_pushnil(L);
2037 }
2038 return 1;
2039 }
2040
2041 static int getfontname(lua_State * L)
2042 {
2043 return do_convert(L, font_name_code);
2044 }
2045
2046 static int getfontidentifier(lua_State * L)
2047 {
2048 return do_convert(L, font_identifier_code);
2049 }
2050
2051 static int getuniformdeviate(lua_State * L)
2052 {
2053 return do_convert(L, uniform_deviate_code);
2054 }
2055
2056 static int getnumber(lua_State * L)
2057 {
2058 return do_convert(L, number_code);
2059 }
2060
2061 static int getromannumeral(lua_State * L)
2062 {
2063 return do_convert(L, roman_numeral_code);
2064 }
2065
2066 static int get_parshape(lua_State * L)
2067 {
2068 halfword ptr = par_shape_par_ptr;
2069 if (ptr != null) {
2070 int m = 1;
2071 int n = vinfo(ptr + 1);
2072 lua_createtable(L, n, 0);
2073 while (m <= n) {
2074 lua_createtable(L, 2, 0);
2075 lua_pushinteger(L, vlink((ptr) + (2 * (m - 1)) + 2));
2076 lua_rawseti(L, -2, 1);
2077 lua_pushinteger(L, vlink((ptr) + (2 * (m - 1)) + 3));
2078 lua_rawseti(L, -2, 2);
2079 lua_rawseti(L, -2, m);
2080 m++;
2081 }
2082 } else {
2083 lua_pushnil(L);
2084 }
2085 return 1;
2086 }
2087
2088 static int get_etex_parshape(lua_State * L, int code)
2089 {
2090 halfword ptr = null;
2091 switch (code) {
2092 case inter_line_penalties_loc:
2093 ptr = inter_line_penalties_par_ptr;
2094 break;
2095 case club_penalties_loc:
2096 ptr = club_penalties_par_ptr;
2097 break;
2098 case widow_penalties_loc:
2099 ptr = widow_penalties_par_ptr;
2100 break;
2101 case display_widow_penalties_loc:
2102 ptr = display_widow_penalties_par_ptr;
2103 break;
2104 }
2105 if (ptr != null) {
2106 int m = 1;
2107 int n = vinfo(ptr + 1) + 1;
2108 lua_createtable(L, n, 0);
2109 while (m <= n) {
2110 lua_pushinteger(L, penalty(ptr + m));
2111 lua_rawseti(L, -2, m);
2112 m++;
2113 }
2114 } else {
2115 lua_pushnil(L);
2116 }
2117 return 1;
2118 }
2119
2120 static int gettex(lua_State * L)
2121 {
2122 int cur_cs1 = -1;
2123 int retval = 1; /* default is to return nil */
2124 int t = lua_gettop(L);
2125 int b = -1 ;
2126 if (t > 1 && lua_type(L,t) == LUA_TBOOLEAN) {
2127 /*
2128 0 == flush width only
2129 1 == flush all glue parameters
2130 */
2131 b = lua_toboolean(L,t);
2132 t = t - 1;
2133 }
2134 if (lua_type(L,t) == LUA_TSTRING) {
2135 /*
2136 1 == tex
2137 2 == boxmaxdepth
2138 or
2139 1 == boxmaxdepth
2140 */
2141 int texstr;
2142 size_t k;
2143 const char *st = lua_tolstring(L, t, &k);
2144 if (lua_key_eq(st,prevdepth)) {
2145 lua_pushinteger(L, cur_list.prev_depth_field);
2146 return 1;
2147 } else if (lua_key_eq(st,prevgraf)) {
2148 lua_pushinteger(L, cur_list.pg_field);
2149 return 1;
2150 } else if (lua_key_eq(st,spacefactor)) {
2151 lua_pushinteger(L, cur_list.space_factor_field);
2152 return 1;
2153 }
2154 texstr = maketexlstring(st, k);
2155 cur_cs1 = prim_lookup(texstr); /* not found == relax == 0 */
2156 flush_str(texstr);
2157 }
2158 if (cur_cs1 > 0) {
2159 int cur_cmd1 = get_prim_eq_type(cur_cs1);
2160 int cur_code = get_prim_equiv(cur_cs1);
2161 switch (cur_cmd1) {
2162 case last_item_cmd:
2163 retval = do_lastitem(L, cur_code);
2164 break;
2165 case convert_cmd:
2166 retval = do_convert(L, cur_code);
2167 break;
2168 case assign_toks_cmd:
2169 case assign_int_cmd:
2170 case assign_attr_cmd:
2171 case assign_dir_cmd:
2172 case assign_direction_cmd:
2173 case assign_dimen_cmd:
2174 case set_aux_cmd:
2175 case set_prev_graf_cmd:
2176 case set_page_int_cmd:
2177 case set_page_dimen_cmd:
2178 case char_given_cmd:
2179 case math_given_cmd:
2180 retval = do_scan_internal(L, cur_cmd1, cur_code, -1);
2181 break;
2182 case assign_glue_cmd:
2183 case assign_mu_glue_cmd:
2184 retval = do_scan_internal(L, cur_cmd1, cur_code, b);
2185 break;
2186 case set_tex_shape_cmd:
2187 retval = get_parshape(L);
2188 break;
2189 case set_etex_shape_cmd:
2190 retval = get_etex_parshape(L, cur_code);
2191 break;
2192 default:
2193 lua_pushnil(L);
2194 break;
2195 }
2196 } else if ((t == 2) && (lua_type(L,2) == LUA_TSTRING)) {
2197 lua_rawget(L, 1);
2198 }
2199 return retval;
2200 }
2201
2202 static int getlist(lua_State * L)
2203 {
2204 const char *str;
2205 int top = lua_gettop(L);
2206 if (lua_type(L,top) == LUA_TSTRING) {
2207 str = lua_tostring(L, top);
2208 if (lua_key_eq(str,page_ins_head)) {
2209 if (vlink(page_ins_head) == page_ins_head)
2210 lua_pushinteger(L, null);
2211 else
2212 lua_pushinteger(L, vlink(page_ins_head));
2213 lua_nodelib_push(L);
2214 } else if (lua_key_eq(str,contrib_head)) {
2215 alink(vlink(contrib_head)) = null ;
2216 lua_pushinteger(L, vlink(contrib_head));
2217 lua_nodelib_push(L);
2218 } else if (lua_key_eq(str,page_discards_head)) {
2219 alink(vlink(page_disc)) = null ;
2220 lua_pushinteger(L, page_disc);
2221 lua_nodelib_push(L);
2222 } else if (lua_key_eq(str,split_discards_head)) {
2223 alink(vlink(split_disc)) = null ;
2224 lua_pushinteger(L, split_disc);
2225 lua_nodelib_push(L);
2226 } else if (lua_key_eq(str,page_head)) {
2227 alink(vlink(page_head)) = null ;/*hh-ls */
2228 lua_pushinteger(L, vlink(page_head));
2229 lua_nodelib_push(L);
2230 } else if (lua_key_eq(str,temp_head)) {
2231 alink(vlink(temp_head)) = null ;/*hh-ls */
2232 lua_pushinteger(L, vlink(temp_head));
2233 lua_nodelib_push(L);
2234 } else if (lua_key_eq(str,hold_head)) {
2235 alink(vlink(hold_head)) = null ;/*hh-ls */
2236 lua_pushinteger(L, vlink(hold_head));
2237 lua_nodelib_push(L);
2238 } else if (lua_key_eq(str,adjust_head)) {
2239 alink(vlink(adjust_head)) = null ;/*hh-ls */
2240 lua_pushinteger(L, vlink(adjust_head));
2241 lua_nodelib_push(L);
2242 } else if (lua_key_eq(str,best_page_break)) {
2243 lua_pushinteger(L, best_page_break);
2244 lua_nodelib_push(L);
2245 } else if (lua_key_eq(str,least_page_cost)) {
2246 lua_pushinteger(L, least_page_cost);
2247 } else if (lua_key_eq(str,best_size)) {
2248 lua_pushinteger(L, best_size);
2249 } else if (lua_key_eq(str,pre_adjust_head)) {
2250 alink(vlink(pre_adjust_head)) = null ;/*hh-ls */
2251 lua_pushinteger(L, vlink(pre_adjust_head));
2252 lua_nodelib_push(L);
2253 } else if (lua_key_eq(str,align_head)) {
2254 alink(vlink(align_head)) = null ;/*hh-ls */
2255 lua_pushinteger(L, vlink(align_head));
2256 lua_nodelib_push(L);
2257 } else {
2258 lua_pushnil(L);
2259 }
2260 } else {
2261 lua_pushnil(L);
2262 }
2263 return 1;
2264 }
2265
2266 static int setlist(lua_State * L)
2267 {
2268 int top = (lua_type(L,1) == LUA_TTABLE) ? 2 : 1 ;
2269 if (lua_type(L,top) == LUA_TSTRING) {
2270 const char *str = lua_tostring(L, top);
2271 if (lua_key_eq(str,best_size)) {
2272 best_size = (int) lua_tointeger(L, top+1);
2273 } else if (lua_key_eq(str,least_page_cost)) {
2274 least_page_cost = (int) lua_tointeger(L, top+1);
2275 } else {
2276 halfword *n_ptr;
2277 halfword n = 0;
2278 if (!lua_isnil(L, top+1)) {
2279 n_ptr = check_isnode(L, top+1);
2280 n = *n_ptr;
2281 }
2282 if (lua_key_eq(str,page_ins_head)) {
2283 if (n == 0) {
2284 vlink(page_ins_head) = page_ins_head;
2285 } else {
2286 halfword m;
2287 vlink(page_ins_head) = n;
2288 m = tail_of_list(n);
2289 vlink(m) = page_ins_head;
2290 }
2291 } else if (lua_key_eq(str,contrib_head)) {
2292 vlink(contrib_head) = n;
2293 if (n == 0) {
2294 contrib_tail = contrib_head;
2295 }
2296 } else if (lua_key_eq(str,best_page_break)) {
2297 best_page_break = n;
2298 } else if (lua_key_eq(str,page_head)) {
2299 vlink(page_head) = n;
2300 page_tail = (n == 0 ? page_head : tail_of_list(n));
2301 } else if (lua_key_eq(str,temp_head)) {
2302 vlink(temp_head) = n;
2303 } else if (lua_key_eq(str,page_discards_head)) {
2304 page_disc = n;
2305 } else if (lua_key_eq(str,split_discards_head)) {
2306 split_disc = n;
2307 } else if (lua_key_eq(str,hold_head)) {
2308 vlink(hold_head) = n;
2309 } else if (lua_key_eq(str,adjust_head)) {
2310 vlink(adjust_head) = n;
2311 adjust_tail = (n == 0 ? adjust_head : tail_of_list(n));
2312 } else if (lua_key_eq(str,pre_adjust_head)) {
2313 vlink(pre_adjust_head) = n;
2314 pre_adjust_tail = (n == 0 ? pre_adjust_head : tail_of_list(n));
2315 } else if (lua_key_eq(str,align_head)) {
2316 vlink(align_head) = n;
2317 }
2318 }
2319 }
2320 return 0;
2321 }
2322
2323 #define NEST_METATABLE "luatex.nest"
2324
2325 static int lua_nest_getfield(lua_State * L)
2326 {
2327 list_state_record *r, **rv = lua_touserdata(L, -2);
2328 const char *field = lua_tostring(L, -1);
2329 r = *rv;
2330 if (lua_key_eq(field,mode)) {
2331 lua_pushinteger(L, r->mode_field);
2332 } else if (lua_key_eq(field,head)) {
2333 lua_nodelib_push_fast(L, r->head_field);
2334 } else if (lua_key_eq(field,tail)) {
2335 lua_nodelib_push_fast(L, r->tail_field);
2336 } else if (lua_key_eq(field,delimptr)) {
2337 lua_pushinteger(L, r->eTeX_aux_field);
2338 lua_nodelib_push(L);
2339 } else if (lua_key_eq(field,prevgraf)) {
2340 lua_pushinteger(L, r->pg_field);
2341 } else if (lua_key_eq(field,modeline)) {
2342 lua_pushinteger(L, r->ml_field);
2343 } else if (lua_key_eq(field,prevdepth)) {
2344 lua_pushinteger(L, r->prev_depth_field);
2345 } else if (lua_key_eq(field,spacefactor)) {
2346 lua_pushinteger(L, r->space_factor_field);
2347 } else if (lua_key_eq(field,noad)) {
2348 lua_pushinteger(L, r->incompleat_noad_field);
2349 lua_nodelib_push(L);
2350 } else if (lua_key_eq(field,dirs)) {
2351 lua_pushinteger(L, r->dirs_field);
2352 lua_nodelib_push(L);
2353 } else if (lua_key_eq(field,mathdir)) {
2354 lua_pushboolean(L, r->math_field);
2355 } else if (lua_key_eq(field,mathstyle)) {
2356 lua_pushinteger(L, r->math_style_field);
2357 } else {
2358 lua_pushnil(L);
2359 }
2360 return 1;
2361 }
2362
2363 static int lua_nest_setfield(lua_State * L)
2364 {
2365 halfword *n;
2366 int i;
2367 list_state_record *r, **rv = lua_touserdata(L, -3);
2368 const char *field = lua_tostring(L, -2);
2369 r = *rv;
2370 if (lua_key_eq(field,mode)) {
2371 i = lua_tointeger(L, -1);
2372 r->mode_field = i;
2373 } else if (lua_key_eq(field,head)) {
2374 n = check_isnode(L, -1);
2375 r->head_field = *n;
2376 } else if (lua_key_eq(field,tail)) {
2377 n = check_isnode(L, -1);
2378 r->tail_field = *n;
2379 } else if (lua_key_eq(field,delimptr)) {
2380 n = check_isnode(L, -1);
2381 r->eTeX_aux_field = *n;
2382 } else if (lua_key_eq(field,prevgraf)) {
2383 i = lua_tointeger(L, -1);
2384 r->pg_field = i;
2385 } else if (lua_key_eq(field,modeline)) {
2386 i = lua_tointeger(L, -1);
2387 r->ml_field = i;
2388 } else if (lua_key_eq(field,prevdepth)) {
2389 i = lua_roundnumber(L, -1);
2390 r->prev_depth_field = i;
2391 } else if (lua_key_eq(field,spacefactor)) {
2392 i = lua_roundnumber(L, -1);
2393 r->space_factor_field = i;
2394 } else if (lua_key_eq(field,noad)) {
2395 n = check_isnode(L, -1);
2396 r->incompleat_noad_field = *n;
2397 } else if (lua_key_eq(field,dirs)) {
2398 n = check_isnode(L, -1);
2399 r->dirs_field = *n;
2400 } else if (lua_key_eq(field,mathdir)) {
2401 r->math_field = lua_toboolean(L, -1);
2402 } else if (lua_key_eq(field,mathstyle)) {
2403 i = lua_tointeger(L, -1);
2404 r->math_style_field = i;
2405 }
2406 return 0;
2407 }
2408
2409 static const struct luaL_Reg nest_m[] = {
2410 {"__index", lua_nest_getfield},
2411 {"__newindex", lua_nest_setfield},
2412 {NULL, NULL} /* sentinel */
2413 };
2414
2415 static void init_nest_lib(lua_State * L)
2416 {
2417 luaL_newmetatable(L, NEST_METATABLE);
2418 luaL_openlib(L, NULL, nest_m, 0);
2419 lua_pop(L, 1);
2420 }
2421
2422 static int getnest(lua_State * L)
2423 {
2424 list_state_record **nestitem;
2425 int n = lua_gettop(L);
2426 int p = -1 ;
2427 if (n == 0) {
2428 p = nest_ptr;
2429 } else {
2430 int t = lua_type(L, n);
2431 if (t == LUA_TNUMBER) {
2432 int ptr = lua_tointeger(L, n);
2433 if (ptr >= 0 && ptr <= nest_ptr) {
2434 p = ptr;
2435 }
2436 } else if (t == LUA_TSTRING) {
2437 const char *s = lua_tostring(L, n);
2438 if (lua_key_eq(s,top)) {
2439 p = nest_ptr;
2440 } else if (lua_key_eq(s,ptr)) {
2441 lua_pushinteger(L, nest_ptr);
2442 return 1;
2443 }
2444 }
2445 }
2446 if (p > -1) {
2447 nestitem = lua_newuserdata(L, sizeof(list_state_record *));
2448 *nestitem = &nest[p];
2449 luaL_getmetatable(L, NEST_METATABLE);
2450 lua_setmetatable(L, -2);
2451 } else {
2452 lua_pushnil(L);
2453 }
2454 return 1;
2455 }
2456
2457 static int setnest(lua_State * L)
2458 {
2459 luaL_error(L, "You can't modify the semantic nest array directly");
2460 return 0;
2461 }
2462
2463 static int do_integer_error(double m)
2464 {
2465 const char *help[] = {
2466 "I can only go up to 2147483647='17777777777=" "7FFFFFFF,",
2467 "so I'm using that number instead of yours.",
2468 NULL
2469 };
2470 tex_error("Number too big", help);
2471 return (m > 0.0 ? infinity : -infinity);
2472 }
2473
2474 static int tex_roundnumber(lua_State * L)
2475 {
2476 double m = (double) lua_tonumber(L, 1) + 0.5; /* integer or float */
2477 if (abs(m) > (double) infinity)
2478 lua_pushinteger(L, do_integer_error(m));
2479 else
2480 lua_pushinteger(L, floor(m));
2481 return 1;
2482 }
2483
2484 static int tex_scaletable(lua_State * L)
2485 {
2486 double delta = luaL_checknumber(L, 2);
2487 if (lua_istable(L, 1)) {
2488 lua_newtable(L); /* the new table is at index 3 */
2489 lua_pushnil(L);
2490 while (lua_next(L, 1) != 0) { /* numeric value */
2491 lua_pushvalue(L, -2);
2492 lua_insert(L, -2);
2493 if (lua_type(L,-2) == LUA_TNUMBER) {
2494 double m = (double) lua_tonumber(L, -1) * delta + 0.5; /* integer or float */
2495 lua_pop(L, 1);
2496 if (abs(m) > (double) infinity)
2497 lua_pushinteger(L, do_integer_error(m));
2498 else
2499 lua_pushinteger(L, floor(m));
2500 }
2501 lua_rawset(L, 3);
2502 }
2503 } else if (lua_type(L,1) == LUA_TNUMBER) {
2504 double m = (double) lua_tonumber(L, 1) * delta + 0.5; /* integer or float */
2505 if (abs(m) > (double) infinity)
2506 lua_pushinteger(L, do_integer_error(m));
2507 else
2508 lua_pushinteger(L, floor(m));
2509 } else {
2510 lua_pushnil(L);
2511 }
2512 return 1;
2513 }
2514
2515 #define hash_text(A) hash[(A)].rh
2516
2517 static int tex_definefont(lua_State * L)
2518 {
2519 const char *csname;
2520 int f, u;
2521 str_number t, d;
2522 size_t l;
2523 int i = 1;
2524 int a = 0;
2525 if (!no_new_control_sequence) {
2526 const char *help[] = { "You can't create a new font inside a \\csname\\endcsname pair", NULL };
2527 tex_error("Definition active", help);
2528 }
2529 if ((lua_gettop(L) == 3) && lua_isboolean(L, 1)) {
2530 a = lua_toboolean(L, 1);
2531 i = 2;
2532 }
2533 csname = luaL_checklstring(L, i, &l);
2534 f = luaL_checkinteger(L, (i + 1));
2535 no_new_control_sequence = 0;
2536 u = string_lookup(csname, l);
2537 no_new_control_sequence = 1;
2538 if (a)
2539 geq_define(u, set_font_cmd, f);
2540 else
2541 eq_define(u, set_font_cmd, f);
2542 eqtb[font_id_base + f] = eqtb[u];
2543 /*tex
2544
2545 This is tricky: when we redefine a string we loose the old one. So this
2546 will change as it's only used to display the |\fontname| so we can store
2547 that with the font.
2548
2549 */
2550 d = cs_text(font_id_base + f);
2551 t = maketexlstring(csname, l); /* the csname */
2552 if (!d) {
2553 /*tex We have a new string. */
2554 cs_text(font_id_base + f) = t;
2555 } else if ((d!=t) && str_eq_str(d,t)){
2556 /*tex We have a duplicate string. */
2557 flush_str(t);
2558 } else if(d!=t){
2559 d = search_string(t);
2560 if (d) {
2561 /*tex We have already such a string. */
2562 cs_text(font_id_base + f) = d;
2563 flush_str(t);
2564 } else {
2565 /*tex The old value is lost but still in the pool. */
2566 cs_text(font_id_base + f) = t;
2567 }
2568 }
2569 return 0;
2570 }
2571
2572 /*
2573 static int tex_hashpairs(lua_State * L)
2574 {
2575 int cmd, chr;
2576 str_number s = 0;
2577 int cs = 1;
2578 lua_newtable(L);
2579 while (cs < hash_size) {
2580 s = hash_text(cs);
2581 if (s > 0) {
2582 char *ss = makecstring(s);
2583 lua_pushstring(L, ss);
2584 free(ss);
2585 cmd = eq_type(cs);
2586 chr = equiv(cs);
2587 make_token_table(L, cmd, chr, cs);
2588 lua_rawset(L, -3);
2589 cs++;
2590 }
2591 }
2592 return 1;
2593 }
2594
2595 static int tex_primitives(lua_State * L)
2596 {
2597 int cmd, chr;
2598 str_number s = 0;
2599 int cs = 0;
2600 lua_newtable(L);
2601 while (cs < prim_size) {
2602 s = get_prim_text(cs);
2603 if (s > 0) {
2604 char *ss = makecstring(s);
2605 lua_pushstring(L, ss);
2606 free(ss);
2607 cmd = get_prim_eq_type(cs);
2608 chr = get_prim_equiv(cs);
2609 make_token_table(L, cmd, chr, 0);
2610 lua_rawset(L, -3);
2611 }
2612 cs++;
2613 }
2614 return 1;
2615 }
2616
2617 static int tex_extraprimitives(lua_State * L)
2618 {
2619 int n, i;
2620 int mask = 0;
2621 int cs = 0;
2622 n = lua_gettop(L);
2623 if (n == 0) {
2624 mask = etex_command + luatex_command;
2625 } else {
2626 for (i = 1; i <= n; i++) {
2627 if (lua_type(L,i) == LUA_TSTRING) {
2628 const char *s = lua_tostring(L, i);
2629 if (lua_key_eq(s,etex)) {
2630 mask |= etex_command;
2631 } else if (lua_key_eq(s,tex)) {
2632 mask |= tex_command;
2633 } else if (lua_key_eq(s,core)) {
2634 mask |= core_command;
2635 } else if (lua_key_eq(s,luatex)) {
2636 mask |= luatex_command;
2637 }
2638 }
2639 }
2640 }
2641 lua_newtable(L);
2642 i = 1;
2643 while (cs < prim_size) {
2644 str_number s = 0;
2645 s = get_prim_text(cs);
2646 if (s > 0) {
2647 if (get_prim_origin(cs) & mask) {
2648 char *ss = makecstring(s);
2649 lua_pushstring(L, ss);
2650 free(ss);
2651 lua_rawseti(L, -2, i++);
2652 }
2653 }
2654 cs++;
2655 }
2656 return 1;
2657 }
2658
2659 */
2660
2661 static int tex_hashpairs(lua_State * L)
2662 {
2663 str_number s = 0;
2664 int cs = 1;
2665 int nt = 0;
2666 lua_newtable(L);
2667 while (cs < hash_size) {
2668 s = hash_text(cs);
2669 if (s > 0) {
2670 halfword n = cs_next(cs);
2671 char *ss = makecstring(s);
2672 lua_pushstring(L, ss);
2673 free(ss);
2674 lua_rawseti(L, -2, ++nt);
2675 while (n) {
2676 s = cs_text(n);
2677 if (s) {
2678 ss = makecstring(s);
2679 lua_pushstring(L, ss);
2680 free(ss);
2681 lua_rawseti(L, -2, ++nt);
2682 }
2683 n = cs_next(n);
2684 }
2685 }
2686 cs++;
2687 }
2688 return 1;
2689 }
2690
2691 static int tex_primitives(lua_State * L)
2692 {
2693 str_number s = 0;
2694 int cs = 0;
2695 int nt = 0;
2696 lua_newtable(L);
2697 while (cs < prim_size) {
2698 s = get_prim_text(cs);
2699 if (s > 0) {
2700 char *ss = makecstring(s);
2701 lua_pushstring(L, ss);
2702 free(ss);
2703 lua_rawseti(L, -2, ++nt);
2704 }
2705 cs++;
2706 }
2707 return 1;
2708 }
2709
2710 static int tex_extraprimitives(lua_State * L)
2711 {
2712 int n, i;
2713 int mask = 0;
2714 int cs = 0;
2715 int nt = 0;
2716 n = lua_gettop(L);
2717 if (n == 0) {
2718 mask = etex_command + luatex_command;
2719 } else {
2720 for (i = 1; i <= n; i++) {
2721 if (lua_type(L,i) == LUA_TSTRING) {
2722 const char *s = lua_tostring(L, i);
2723 if (lua_key_eq(s,etex)) {
2724 mask |= etex_command;
2725 } else if (lua_key_eq(s,tex)) {
2726 mask |= tex_command;
2727 } else if (lua_key_eq(s,core)) {
2728 mask |= core_command;
2729 } else if (lua_key_eq(s,luatex)) {
2730 mask |= luatex_command;
2731 }
2732 }
2733 }
2734 }
2735 lua_newtable(L);
2736 while (cs < prim_size) {
2737 str_number s = 0;
2738 s = get_prim_text(cs);
2739 if (s > 0) {
2740 if (get_prim_origin(cs) & mask) {
2741 char *ss = makecstring(s);
2742 lua_pushstring(L, ss);
2743 free(ss);
2744 lua_rawseti(L, -2, ++nt);
2745 }
2746 }
2747 cs++;
2748 }
2749 return 1;
2750 }
2751
2752 static int tex_enableprimitives(lua_State * L)
2753 {
2754 int n = lua_gettop(L);
2755 if (n != 2) {
2756 luaL_error(L, "wrong number of arguments");
2757 } else {
2758 size_t l;
2759 int i;
2760 const char *pre = luaL_checklstring(L, 1, &l);
2761 if (lua_istable(L, 2)) {
2762 int nncs = no_new_control_sequence;
2763 no_new_control_sequence = true;
2764 i = 1;
2765 while (1) {
2766 lua_rawgeti(L, 2, i);
2767 if (lua_type(L,3) == LUA_TSTRING) {
2768 const char *prim = lua_tostring(L, 3);
2769 str_number s = maketexstring(prim);
2770 halfword prim_val = prim_lookup(s);
2771 if (prim_val != undefined_primitive) {
2772 char *newprim;
2773 int val;
2774 size_t newl;
2775 halfword cur_cmd1 = get_prim_eq_type(prim_val);
2776 halfword cur_chr1 = get_prim_equiv(prim_val);
2777 if (strncmp(pre, prim, l) != 0) { /* not a prefix */
2778 newl = strlen(prim) + l;
2779 newprim = (char *) xmalloc((unsigned) (newl + 1));
2780 strcpy(newprim, pre);
2781 strcat(newprim + l, prim);
2782 } else {
2783 newl = strlen(prim);
2784 newprim = (char *) xmalloc((unsigned) (newl + 1));
2785 strcpy(newprim, prim);
2786 }
2787 val = string_lookup(newprim, newl);
2788 if (val == undefined_control_sequence ||
2789 eq_type(val) == undefined_cs_cmd) {
2790 primitive_def(newprim, newl, (quarterword) cur_cmd1, cur_chr1);
2791 }
2792 free(newprim);
2793 }
2794 flush_str(s);
2795 } else {
2796 lua_pop(L, 1);
2797 break;
2798 }
2799 lua_pop(L, 1);
2800 i++;
2801 }
2802 lua_pop(L, 1); /* the table */
2803 no_new_control_sequence = nncs;
2804 } else {
2805 luaL_error(L, "Expected an array of names as second argument");
2806 }
2807 }
2808 return 0;
2809 }
2810
2811 #define get_int_par(A,B) do { \
2812 lua_key_rawgeti(A); \
2813 if (lua_type(L, -1) == LUA_TNUMBER) { \
2814 A = (int) lua_tointeger(L, -1); \
2815 } else { \
2816 A = (B); \
2817 } \
2818 lua_pop(L,1); \
2819 } while (0)
2820
2821 #define get_intx_par(A,B,C,D) do { \
2822 lua_key_rawgeti(A); \
2823 if (lua_type(L, -1) == LUA_TNUMBER) { \
2824 A = (int) lua_tointeger(L, -1); \
2825 C = null; \
2826 } else if (lua_type(L, -1) == LUA_TTABLE){ \
2827 A = 0; \
2828 C = nodelib_topenalties(L, lua_gettop(L)); \
2829 } else { \
2830 A = (B); \
2831 C = (D); \
2832 } \
2833 lua_pop(L,1); \
2834 } while (0)
2835
2836 #define get_dimen_par(A,B) do { \
2837 lua_key_rawgeti(A); \
2838 if (lua_type(L, -1) == LUA_TNUMBER) { \
2839 A = (int) lua_roundnumber(L, -1); \
2840 } else { \
2841 A = (B); \
2842 } \
2843 lua_pop(L,1); \
2844 } while (0)
2845
2846 #define get_glue_par(A,B) do { \
2847 lua_key_rawgeti(A); \
2848 if (lua_type(L, -1) != LUA_TNIL) { \
2849 A = *check_isnode(L, -1); \
2850 } else { \
2851 A = (B); \
2852 } \
2853 lua_pop(L,1); \
2854 } while (0)
2855
2856 static halfword nodelib_toparshape(lua_State * L, int i)
2857 {
2858 halfword p;
2859 int n = 0;
2860 int width, indent, j;
2861 /* find |n| */
2862 lua_pushnil(L);
2863 while (lua_next(L, i) != 0) {
2864 n++;
2865 lua_pop(L, 1);
2866 }
2867 if (n == 0)
2868 return null;
2869 p = new_node(shape_node, 2 * (n + 1) + 1);
2870 vinfo(p + 1) = n;
2871 /* fill |p| */
2872 lua_pushnil(L);
2873 j = 0;
2874 while (lua_next(L, i) != 0) {
2875 /* don't give an error for non-tables, we may add special syntaxes at some point */
2876 j++;
2877 if (lua_type(L, i) == LUA_TTABLE) {
2878 lua_rawgeti(L, -1, 1); /* indent */
2879 if (lua_type(L, -1) == LUA_TNUMBER) {
2880 indent = lua_roundnumber(L, -1);
2881 lua_pop(L, 1);
2882 lua_rawgeti(L, -1, 2); /* width */
2883 if (lua_type(L, -1) == LUA_TNUMBER) {
2884 width = lua_roundnumber(L, -1);
2885 lua_pop(L, 1);
2886 varmem[p + 2 * j].cint = indent;
2887 varmem[p + 2 * j + 1].cint = width;
2888 }
2889 }
2890 }
2891 lua_pop(L, 1);
2892 }
2893 return p;
2894 }
2895
2896 /* penalties */
2897
2898 static halfword nodelib_topenalties(lua_State * L, int i)
2899 {
2900 halfword p;
2901 int n = 0;
2902 int j;
2903 /* find |n| */
2904 lua_pushnil(L);
2905 while (lua_next(L, i) != 0) {
2906 n++;
2907 lua_pop(L, 1);
2908 }
2909 if (n == 0)
2910 return null;
2911 p = new_node(shape_node, 2 * ((n / 2) + 1) + 1 + 1);
2912 vinfo(p + 1) = (n / 2) + 1;
2913 varmem[p + 2].cint = n;
2914 lua_pushnil(L);
2915 j = 2;
2916 while (lua_next(L, i) != 0) {
2917 j++;
2918 if (lua_type(L, -1) == LUA_TNUMBER) {
2919 int pen = lua_tointeger(L, -1);
2920 varmem[p+j].cint = pen;
2921 }
2922 lua_pop(L, 1);
2923 }
2924 if (!odd(n))
2925 varmem[p+j+1].cint = 0;
2926 return p;
2927 }
2928
2929 static int tex_run_linebreak(lua_State * L)
2930 {
2931
2932 halfword *j;
2933 halfword p;
2934 halfword final_par_glue;
2935 int paragraph_dir = 0;
2936 /* locally initialized parameters for line breaking */
2937 int pretolerance, tracingparagraphs, tolerance, looseness,
2938 adjustspacing, adjdemerits, protrudechars,
2939 linepenalty, lastlinefit, doublehyphendemerits, finalhyphendemerits,
2940 hangafter, interlinepenalty, widowpenalty, clubpenalty, brokenpenalty;
2941 halfword emergencystretch, hangindent, hsize, leftskip, rightskip,parshape;
2942 int fewest_demerits = 0, actual_looseness = 0;
2943 halfword clubpenalties, interlinepenalties, widowpenalties;
2944 int save_vlink_tmp_head;
2945 /* push a new nest level */
2946 push_nest();
2947 save_vlink_tmp_head = vlink(temp_head);
2948
2949 j = check_isnode(L, 1); /* the value */
2950 vlink(temp_head) = *j;
2951 p = *j;
2952 if ((!is_char_node(vlink(*j))) && ((type(vlink(*j)) == local_par_node))) {
2953 paragraph_dir = local_par_dir(vlink(*j));
2954 }
2955
2956 while (vlink(p) != null)
2957 p = vlink(p);
2958 final_par_glue = p;
2959
2960 /* initialize local parameters */
2961
2962 if (lua_gettop(L) != 2 || lua_type(L, 2) != LUA_TTABLE) {
2963 lua_checkstack(L, 3);
2964 lua_newtable(L);
2965 }
2966 lua_key_rawgeti(pardir);
2967 if (lua_type(L, -1) == LUA_TSTRING) {
2968 paragraph_dir = nodelib_getdir(L, -1);
2969 }
2970 lua_pop(L, 1);
2971
2972 lua_key_rawgeti(parshape);
2973 if (lua_type(L, -1) == LUA_TTABLE) {
2974 parshape = nodelib_toparshape(L, lua_gettop(L));
2975 } else {
2976 parshape = equiv(par_shape_loc);
2977 }
2978 lua_pop(L, 1);
2979
2980 get_int_par (pretolerance, pretolerance_par);
2981 get_int_par (tracingparagraphs, tracing_paragraphs_par);
2982 get_int_par (tolerance, tolerance_par);
2983 get_int_par (looseness, looseness_par);
2984 get_int_par (adjustspacing, adjust_spacing_par);
2985 get_int_par (adjdemerits, adj_demerits_par);
2986 get_int_par (protrudechars, protrude_chars_par);
2987 get_int_par (linepenalty, line_penalty_par);
2988 get_int_par (lastlinefit, last_line_fit_par);
2989 get_int_par (doublehyphendemerits, double_hyphen_demerits_par);
2990 get_int_par (finalhyphendemerits, final_hyphen_demerits_par);
2991 get_int_par (hangafter, hang_after_par);
2992 get_intx_par (interlinepenalty,inter_line_penalty_par, interlinepenalties, equiv(inter_line_penalties_loc));
2993 get_intx_par (clubpenalty, club_penalty_par, clubpenalties, equiv(club_penalties_loc));
2994 get_intx_par (widowpenalty, widow_penalty_par, widowpenalties, equiv(widow_penalties_loc));
2995 get_int_par (brokenpenalty, broken_penalty_par);
2996 get_dimen_par(emergencystretch, emergency_stretch_par);
2997 get_dimen_par(hangindent, hang_indent_par);
2998 get_dimen_par(hsize, hsize_par);
2999 get_glue_par (leftskip, left_skip_par);
3000 get_glue_par (rightskip, right_skip_par);
3001 ext_do_line_break(paragraph_dir,
3002 pretolerance,
3003 tracingparagraphs,
3004 tolerance,
3005 emergencystretch,
3006 looseness,
3007 adjustspacing,
3008 parshape,
3009 adjdemerits,
3010 protrudechars,
3011 linepenalty,
3012 lastlinefit,
3013 doublehyphendemerits,
3014 finalhyphendemerits,
3015 hangindent,
3016 hsize,
3017 hangafter,
3018 leftskip,
3019 rightskip,
3020 interlinepenalties,
3021 interlinepenalty,
3022 clubpenalty,
3023 clubpenalties,
3024 widowpenalties,
3025 widowpenalty,
3026 brokenpenalty,
3027 final_par_glue);
3028
3029 /* return the generated list, and its prevdepth */
3030 get_linebreak_info (&fewest_demerits, &actual_looseness) ;
3031 lua_nodelib_push_fast(L, vlink(cur_list.head_field));
3032 lua_newtable(L);
3033 lua_push_key(demerits);
3034 lua_pushinteger(L, fewest_demerits);
3035 lua_settable(L, -3);
3036 lua_push_key(looseness);
3037 lua_pushinteger(L, actual_looseness);
3038 lua_settable(L, -3);
3039 lua_push_key(prevdepth);
3040 lua_pushinteger(L, cur_list.prev_depth_field);
3041 lua_settable(L, -3);
3042 lua_push_key(prevgraf);
3043 lua_pushinteger(L, cur_list.pg_field);
3044 lua_settable(L, -3);
3045
3046 /* restore nest stack */
3047 vlink(temp_head) = save_vlink_tmp_head;
3048 pop_nest();
3049 if (parshape != equiv(par_shape_loc))
3050 flush_node(parshape);
3051 return 2;
3052 }
3053
3054 static int tex_reset_paragraph(lua_State * L)
3055 {
3056 (void) L;
3057 normal_paragraph();
3058 return 0;
3059 }
3060
3061 static int tex_shipout(lua_State * L)
3062 {
3063 int boxnum = get_box_id(L, 1, true);
3064 ship_out(static_pdf, box(boxnum), SHIPPING_PAGE);
3065 box(boxnum) = null;
3066 return 0;
3067 }
3068
3069 static int tex_badness(lua_State * L)
3070 {
3071 scaled t = lua_tointeger(L,1);
3072 scaled s = lua_tointeger(L,2);
3073 lua_pushinteger(L, badness(t,s));
3074 return 1;
3075 }
3076
3077 static int tex_run_boot(lua_State * L)
3078 {
3079 int n = lua_gettop(L);
3080 const char *format = NULL;
3081 if (n >= 1) {
3082 ini_version = 0;
3083 format = luaL_checkstring(L, 1);
3084 } else {
3085 ini_version = 1;
3086 }
3087 if (main_initialize()) { /* > 0 = failure */
3088 lua_pushboolean(L, 0); /* false */
3089 return 1;
3090 }
3091 if (format) {
3092 if (!zopen_w_input(&fmt_file, format, DUMP_FORMAT, FOPEN_RBIN_MODE)) {
3093 lua_pushboolean(L, 0); /* false */
3094 return 1;
3095 }
3096 if (!load_fmt_file(format)) {
3097 zwclose(fmt_file);
3098 lua_pushboolean(L, 0); /* false */
3099 return 1;
3100 }
3101 zwclose(fmt_file);
3102 }
3103 pdf_init_map_file("pdftex.map");
3104 /* */
3105 if (end_line_char_inactive)
3106 decr(ilimit);
3107 else
3108 buffer[ilimit] = (packed_ASCII_code) end_line_char_par;
3109 fix_date_and_time();
3110 random_seed = (microseconds * 1000) + (epochseconds % 1000000);
3111 init_randoms(random_seed);
3112 initialize_math();
3113 fixup_selector(log_opened_global);
3114 check_texconfig_init();
3115 text_dir_ptr = new_dir(0);
3116 history = spotless; /* ready to go! */
3117 /* Initialize synctex primitive */
3118 synctexinitcommand();
3119 /* tex is ready to go, now */
3120 /*
3121 unhide_lua_table(Luas, "tex", tex_table_id);
3122 unhide_lua_table(Luas, "pdf", pdf_table_id);
3123 unhide_lua_table(Luas, "token", token_table_id);
3124 unhide_lua_table(Luas, "node", node_table_id);
3125 */
3126 lua_pushboolean(L, 1); /* true */
3127 return 1;
3128 }
3129
3130 /* tex random generators */
3131
3132 static int tex_init_rand(lua_State * L)
3133 {
3134 int sp;
3135 if (lua_type(L, 1) != LUA_TNUMBER) {
3136 luaL_error(L, "argument must be a number");
3137 return 0;
3138 }
3139 sp = lua_roundnumber(L, 1);
3140 init_randoms(sp);
3141 return 0;
3142 }
3143
3144 static int tex_unif_rand(lua_State * L)
3145 {
3146 int sp;
3147 if (lua_type(L, 1) != LUA_TNUMBER) {
3148 luaL_error(L, "argument must be a number");
3149 return 0;
3150 }
3151 sp = lua_roundnumber(L, 1);
3152 lua_pushinteger(L, unif_rand(sp));
3153 return 1;
3154 }
3155
3156 static int tex_norm_rand(lua_State * L)
3157 {
3158 lua_pushinteger(L, norm_rand());
3159 return 1;
3160 }
3161
3162 /* Same as lua but with tex rng */
3163
3164 static int lua_math_random (lua_State *L)
3165 {
3166 lua_Number rand_max = 0x7fffffff ;
3167 lua_Number r = unif_rand(rand_max) ;
3168 r = (r>=0 ? 0+r : 0-r) ;
3169 r = r / rand_max;
3170 switch (lua_gettop(L)) { /* check number of arguments */
3171 case 0: { /* no arguments */
3172 lua_pushnumber(L, r); /* float: [0, 1] */
3173 break;
3174 }
3175 case 1: { /* only upper limit */
3176 lua_Number u = luaL_checknumber(L, 1);
3177 luaL_argcheck(L, (lua_Number)1.0 <= u, 1, "interval is empty");
3178 lua_pushnumber(L, floor(r*u) + (lua_Number)(1.0)); /* float: [1, u] */
3179 break;
3180 }
3181 case 2: { /* lower and upper limits */
3182 lua_Number l = luaL_checknumber(L, 1);
3183 lua_Number u = luaL_checknumber(L, 2);
3184 luaL_argcheck(L, l <= u, 2, "interval is empty");
3185 lua_pushnumber(L, floor(r*(u-l+1)) + l); /* float: [l, u] */
3186 break;
3187 }
3188 default:
3189 return luaL_error(L, "wrong number of arguments");
3190 }
3191 return 1;
3192 }
3193
3194 static int tex_run_main(lua_State * L)
3195 {
3196 (void) L;
3197 main_control();
3198 return 0;
3199 }
3200
3201 static int tex_run_end(lua_State * L)
3202 {
3203 (void) L;
3204 final_cleanup(); /* prepare for death */
3205 close_files_and_terminate();
3206 do_final_end();
3207 return 0;
3208 }
3209
3210 static int tex_show_context(lua_State * L)
3211 {
3212 (void) L;
3213 show_context();
3214 return 0;
3215 }
3216
3217 static int tex_save_box_resource(lua_State * L)
3218 {
3219 halfword boxdata;
3220 int index = null;
3221 int attributes = null;
3222 int resources = null;
3223 int type = 0;
3224 int margin = pdf_xform_margin;
3225 boolean immediate = false;
3226 /* more or less same as scanner variant */
3227 if (lua_type(L,1) == LUA_TNUMBER) {
3228 halfword boxnumber = lua_tointeger(L,1);
3229 boxdata = box(boxnumber);
3230 box(boxnumber) = null;
3231 } else {
3232 boxdata = nodelist_from_lua(L,1);
3233 if (type(boxdata) != hlist_node && type(boxdata) != vlist_node) {
3234 normal_error("pdf backend", "xforms can only be used with a box or [h|v]list");
3235 }
3236 }
3237 if (boxdata == null) {
3238 normal_error("pdf backend", "xforms cannot be used with a void box or empty [h|v]list");
3239 }
3240 /* box attributes resources */
3241 if (lua_type(L,2) == LUA_TSTRING) {
3242 lua_pushvalue(L, 2);
3243 attributes = luaL_ref(L, LUA_REGISTRYINDEX);
3244 }
3245 if (lua_type(L,3) == LUA_TSTRING) {
3246 lua_pushvalue(L, 3);
3247 resources = luaL_ref(L, LUA_REGISTRYINDEX);
3248 }
3249 if (lua_type(L,4) == LUA_TBOOLEAN) {
3250 immediate = lua_toboolean(L, 4);
3251 }
3252 if (lua_type(L,5) == LUA_TNUMBER) {
3253 type = lua_tointeger(L, 5);
3254 }
3255 if (lua_type(L,6) == LUA_TNUMBER) {
3256 margin = lua_tointeger(L, 6);
3257 }
3258 static_pdf->xform_count++;
3259 index = pdf_create_obj(static_pdf, obj_type_xform, static_pdf->xform_count);
3260 set_obj_data_ptr(static_pdf, index, pdf_get_mem(static_pdf, pdfmem_xform_size));
3261 set_obj_xform_attr(static_pdf, index, null);
3262 set_obj_xform_attr_str(static_pdf, index, attributes);
3263 set_obj_xform_resources(static_pdf, index, null);
3264 set_obj_xform_resources_str(static_pdf, index, resources);
3265 set_obj_xform_box(static_pdf, index, (int) boxdata);
3266 set_obj_xform_width(static_pdf, index, width(boxdata));
3267 set_obj_xform_height(static_pdf, index, height(boxdata));
3268 set_obj_xform_depth(static_pdf, index, depth(boxdata));
3269 set_obj_xform_type(static_pdf, index, type);
3270 set_obj_xform_margin(static_pdf, index, margin);
3271 last_saved_box_index = index;
3272 lua_pushinteger(L, index);
3273 if (immediate) {
3274 pdf_cur_form = last_saved_box_index;
3275 ship_out(static_pdf, obj_xform_box(static_pdf, last_saved_box_index), SHIPPING_FORM);
3276 }
3277 return 1;
3278 }
3279
3280 static int tex_use_box_resource(lua_State * L)
3281 {
3282 halfword rule;
3283 int index = 0;
3284 scaled_whd alt, nat, dim;
3285 if (lua_type(L,1) != LUA_TNUMBER) {
3286 lua_pushnil(L);
3287 lua_pushnil(L);
3288 lua_pushnil(L);
3289 lua_pushnil(L);
3290 } else {
3291 index = lua_tointeger(L,1);
3292 alt.wd = null_flag;
3293 alt.ht = null_flag;
3294 alt.dp = null_flag;
3295 if (lua_type(L,2) == LUA_TNUMBER) {
3296 alt.wd = (scaled) lua_roundnumber(L,2);
3297 }
3298 if (lua_type(L,3) == LUA_TNUMBER) {
3299 alt.ht = (scaled) lua_roundnumber(L,3);
3300 }
3301 if (lua_type(L,4) == LUA_TNUMBER) {
3302 alt.dp = (scaled) lua_roundnumber(L,4);
3303 }
3304 /* sort of the same as backend */
3305 check_obj_type(static_pdf, obj_type_xform, index);
3306 nat.wd = obj_xform_width(static_pdf, index);
3307 nat.ht = obj_xform_height(static_pdf, index);
3308 nat.dp = obj_xform_depth(static_pdf, index);
3309 if (alt.wd != null_flag || alt.ht != null_flag || alt.dp != null_flag) {
3310 dim = tex_scale(nat, alt);
3311 } else {
3312 dim = nat;
3313 }
3314 rule = new_rule(box_rule);
3315 rule_index(rule) = index;
3316 width(rule) = dim.wd;
3317 height(rule) = dim.ht;
3318 depth(rule) = dim.dp;
3319 nodelist_to_lua(L, rule);
3320 lua_pushinteger(L, (int) dim.wd);
3321 lua_pushinteger(L, (int) dim.ht);
3322 lua_pushinteger(L, (int) dim.dp);
3323 }
3324 return 4;
3325 }
3326
3327 static int tex_get_box_resource_dimensions(lua_State * L)
3328 {
3329 int index = 0;
3330 if (lua_type(L,1) != LUA_TNUMBER) {
3331 lua_pushnil(L);
3332 lua_pushnil(L);
3333 lua_pushnil(L);
3334 lua_pushnil(L);
3335 } else {
3336 index = lua_tointeger(L,1);
3337 check_obj_type(static_pdf, obj_type_xform, index);
3338 lua_pushinteger(L, (int) obj_xform_width(static_pdf, index));
3339 lua_pushinteger(L, (int) obj_xform_height(static_pdf, index));
3340 lua_pushinteger(L, (int) obj_xform_depth(static_pdf, index));
3341 lua_pushinteger(L, (int) obj_xform_margin(static_pdf, index));
3342 }
3343 return 4;
3344 }
3345
3346 static int tex_get_box_resource_box(lua_State * L)
3347 {
3348 /* no checking yet as this might go */
3349 halfword b;
3350 int index = lua_tointeger(L,1);
3351 check_obj_type(static_pdf, obj_type_xform, index);
3352 b = obj_xform_box(static_pdf, index);
3353 nodelist_to_lua(L, b);
3354 return 1;
3355 }
3356
3357 static int tex_build_page(lua_State * L)
3358 {
3359 build_page();
3360 return 0;
3361 }
3362
3363 static int lua_get_page_state(lua_State * L)
3364 {
3365 lua_pushinteger(L,page_contents);
3366 return 1;
3367 }
3368
3369 static int lua_get_local_level(lua_State * L)
3370 {
3371 lua_pushinteger(L,current_local_level());
3372 return 1;
3373 }
3374
3375 /* synctex */
3376
3377 static int lua_set_synctex_mode(lua_State * L)
3378 {
3379 halfword mode = lua_tointeger(L, 1);
3380 synctex_set_mode(mode);
3381 return 0;
3382 }
3383 static int lua_get_synctex_mode(lua_State * L)
3384 {
3385 lua_pushinteger(L,synctex_get_mode());
3386 return 1;
3387 }
3388
3389 static int lua_set_synctex_tag(lua_State * L)
3390 {
3391 halfword tag = lua_tointeger(L, 1);
3392 synctex_set_tag(tag);
3393 return 0;
3394 }
3395
3396 static int lua_get_synctex_tag(lua_State * L)
3397 {
3398 lua_pushinteger(L,synctex_get_tag());
3399 return 1;
3400 }
3401
3402 static int lua_force_synctex_tag(lua_State * L)
3403 {
3404 halfword tag = lua_tointeger(L, 1);
3405 synctex_force_tag(tag);
3406 return 0;
3407 }
3408
3409 static int lua_force_synctex_line(lua_State * L)
3410 {
3411 halfword line = lua_tointeger(L, 1);
3412 synctex_force_line(line);
3413 return 0;
3414 }
3415
3416 static int lua_set_synctex_line(lua_State * L)
3417 {
3418 halfword line = lua_tointeger(L, 1);
3419 synctex_set_line(line);
3420 return 0;
3421 }
3422
3423 static int lua_get_synctex_line(lua_State * L)
3424 {
3425 lua_pushinteger(L,synctex_get_line());
3426 return 1;
3427 }
3428
3429 static int lua_set_synctex_no_files(lua_State * L)
3430 {
3431 halfword flag = lua_tointeger(L, 1);
3432 synctex_set_no_files(flag);
3433 return 0;
3434 }
3435
3436 /*
3437 This is experimental and might change. In version 10 we hope to have the
3438 final version available. It actually took quite a bit of time to understand
3439 the implications of mixing lua prints in here. The current variant is (so far)
3440 the most robust (wrt crashes and side effects).
3441 */
3442
3443 #define mode mode_par
3444
3445 /*
3446 When we add save levels then we can get crashes when one flushed bad
3447 groups due to out of order flushing. So we play safe! But still we can
3448 have issues so best make sure you're in hmode.
3449 */
3450
3451 static int forcehmode(lua_State * L)
3452 {
3453 if (abs(mode) == vmode) {
3454 if (lua_type(L,1) == LUA_TBOOLEAN) {
3455 new_graf(lua_toboolean(L,1));
3456 } else {
3457 new_graf(1);
3458 }
3459 }
3460 return 0;
3461 }
3462
3463 static int runtoks(lua_State * L)
3464 {
3465 if (lua_type(L,1) == LUA_TFUNCTION) {
3466 int old_mode = mode;
3467 int ref;
3468 halfword r = get_avail();
3469 halfword t = get_avail();
3470 token_info(r) = token_val(extension_cmd,end_local_code);
3471 lua_pushvalue(L, 1);
3472 ref = luaL_ref(L,LUA_REGISTRYINDEX);
3473 token_info(t) = token_val(lua_local_call_cmd, ref);
3474 begin_token_list(r,inserted);
3475 begin_token_list(t,inserted);
3476 if (luacstrings > 0) {
3477 lua_string_start();
3478 }
3479 if (tracing_nesting_par > 2) {
3480 local_control_message("entering token scanner via function");
3481 }
3482 mode = -hmode;
3483 local_control();
3484 mode = old_mode;
3485 luaL_unref(L,LUA_REGISTRYINDEX,ref);
3486 } else {
3487 halfword t;
3488 int k = get_item_index(L, 1, toks_base);
3489 check_index_range(k, "gettoks");
3490 t = toks(k);
3491 if (! t) {
3492 /* nothing to do */
3493 } else if ((scanner_status != defining) || (lua_toboolean(L,2))) {
3494 int grouped = lua_toboolean(L,3);
3495 int old_mode = mode;
3496 if (grouped) {
3497 halfword r = get_avail();
3498 token_info(r) = token_val(right_brace_cmd,0);
3499 begin_token_list(r,inserted);
3500 }
3501 {
3502 halfword r = get_avail();
3503 token_info(r) = token_val(extension_cmd,end_local_code);
3504 begin_token_list(r,inserted);
3505 }
3506 begin_token_list(t,local_text);
3507 if (grouped) {
3508 halfword r = get_avail();
3509 token_info(r) = token_val(left_brace_cmd,0);
3510 begin_token_list(r,inserted);
3511 }
3512 if (luacstrings > 0) {
3513 lua_string_start();
3514 }
3515 if (tracing_nesting_par > 2) {
3516 local_control_message("entering token scanner via register");
3517 }
3518 mode = -hmode;
3519 local_control();
3520 mode = old_mode;
3521 /* unsave(); */
3522 } else {
3523 halfword q;
3524 halfword p = temp_token_head;
3525 halfword r = token_link(t);
3526 set_token_link(p, null);
3527 while (r) {
3528 fast_store_new_token(token_info(r));
3529 r = token_link(r);
3530 }
3531 ins_list(token_link(temp_token_head));
3532 }
3533 }
3534 return 0;
3535 }
3536
3537 static int quittoks(lua_State * L)
3538 {
3539 (void) L;
3540 if (tracing_nesting_par > 2) {
3541 local_control_message("quitting token scanner");
3542 }
3543 end_local_control();
3544 return 0;
3545 }
3546
3547 /*tex Negative values are internal and inline. */
3548
3549 static int tex_getmodevalues(lua_State * L)
3550 {
3551 lua_newtable(L);
3552 lua_pushinteger(L,0);
3553 lua_push_string_by_name(L,unset); /* 0 */
3554 lua_rawset(L, -3);
3555 lua_pushinteger(L,vmode);
3556 lua_push_string_by_name(L,vertical); /* 1 */
3557 lua_rawset(L, -3);
3558 lua_pushinteger(L,hmode);
3559 lua_push_string_by_name(L,horizontal); /* 127 */
3560 lua_rawset(L, -3);
3561 lua_pushinteger(L,mmode);
3562 lua_push_string_by_name(L,math); /* 253 */
3563 lua_rawset(L, -3);
3564 return 1;
3565 }
3566
3567 /*tex
3568 This setter is only there so that we can still generate older \CONTEXT\ documents (like
3569 articles and progress reports) that refer to some now obsolete commands. Some of these
3570 math control are still present in \LUAMETATEX\ but in a different way. In \LUATEX\ they
3571 became obsolete in 2025 (prelude to TL 2026) because we (MS/HH) notices that they'd
3572 never been used anywhere else.
3573 */
3574
3575 static int tex_permitmathobsolete(lua_State * L)
3576 {
3577 permit_math_obsolete = lua_toboolean(L, 1);
3578 formatted_warning("math","obsolete commands are %s", permit_math_obsolete ? "permitted" : "blocked" );
3579 return 0;
3580 }
3581
3582 /* till here */
3583
3584 void init_tex_table(lua_State * L)
3585 {
3586 lua_createtable(L, 0, 3);
3587 lua_pushcfunction(L, tex_run_boot);
3588 lua_setfield(L, -2, "initialize");
3589 lua_pushcfunction(L, tex_run_main);
3590 lua_setfield(L, -2, "run");
3591 lua_pushcfunction(L, tex_run_end);
3592 lua_setfield(L, -2, "finish");
3593 lua_setglobal(L, "tex");
3594 }
3595
3596 static const struct luaL_Reg texlib[] = {
3597 { "run", tex_run_main }, /* may be needed */
3598 { "finish", tex_run_end }, /* may be needed */
3599 { "write", luacwrite },
3600 { "print", luacprint },
3601 { "sprint", luacsprint },
3602 { "tprint", luactprint },
3603 { "cprint", luaccprint },
3604 { "error", texerror },
3605 { "set", settex },
3606 { "get", gettex },
3607 { "isdimen", isdimen },
3608 { "setdimen", setdimen },
3609 { "getdimen", getdimen },
3610 { "isskip", isskip },
3611 { "setskip", setskip },
3612 { "getskip", getskip },
3613 { "isglue", isskip },
3614 { "setglue", setglue },
3615 { "getglue", getglue },
3616 { "ismuskip", ismuskip },
3617 { "setmuskip", setmuskip },
3618 { "getmuskip", getmuskip },
3619 { "ismuglue", ismuskip },
3620 { "setmuglue", setmuglue },
3621 { "getmuglue", getmuglue },
3622 { "isattribute", isattribute },
3623 { "setattribute", setattribute },
3624 { "getattribute", getattribute },
3625 { "iscount", iscount },
3626 { "setcount", setcount },
3627 { "getcount", getcount },
3628 { "istoks", istoks },
3629 { "settoks", settoks },
3630 { "scantoks", scantoks },
3631 { "gettoks", gettoks },
3632 { "getmark", getmark },
3633 { "isbox", isbox },
3634 { "setbox", setbox },
3635 { "getbox", getbox },
3636 { "splitbox", splitbox },
3637 { "setlist", setlist },
3638 { "getlist", getlist },
3639 { "setnest", setnest }, /* only a message */
3640 { "getnest", getnest },
3641 { "setcatcode", setcatcode },
3642 { "getcatcode", getcatcode },
3643 { "setdelcode", setdelcode },
3644 { "getdelcode", getdelcode },
3645 { "getdelcodes", getdelcodes },
3646 { "setlccode", setlccode },
3647 { "getlccode", getlccode },
3648 { "setmathcode", setmathcode },
3649 { "getmathcode", getmathcode },
3650 { "getmathcodes", getmathcodes },
3651 { "setsfcode", setsfcode },
3652 { "getsfcode", getsfcode },
3653 { "setuccode", setuccode },
3654 { "getuccode", getuccode },
3655 { "round", tex_roundnumber },
3656 { "scale", tex_scaletable },
3657 { "sp", tex_scaledimen },
3658 { "fontname", getfontname },
3659 { "fontidentifier", getfontidentifier },
3660 { "uniformdeviate", getuniformdeviate },
3661 { "number", getnumber },
3662 { "romannumeral", getromannumeral },
3663 { "definefont", tex_definefont },
3664 { "hashtokens", tex_hashpairs },
3665 { "primitives", tex_primitives },
3666 { "extraprimitives", tex_extraprimitives },
3667 { "enableprimitives", tex_enableprimitives },
3668 { "shipout", tex_shipout },
3669 { "badness", tex_badness },
3670 { "setmath", tex_setmathparm },
3671 { "getmath", tex_getmathparm },
3672 { "linebreak", tex_run_linebreak },
3673 { "resetparagraph", tex_reset_paragraph },
3674 /* tex random generators */
3675 { "init_rand", tex_init_rand },
3676 { "uniform_rand",tex_unif_rand },
3677 { "normal_rand", tex_norm_rand },
3678 { "lua_math_randomseed", tex_init_rand }, /* syntactic sugar */
3679 { "lua_math_random", lua_math_random },
3680 { "show_context", tex_show_context },
3681 { "saveboxresource", tex_save_box_resource },
3682 { "useboxresource", tex_use_box_resource },
3683 { "getboxresourcedimensions", tex_get_box_resource_dimensions },
3684 /* might go, used when sanitizing backend */
3685 { "getboxresourcebox", tex_get_box_resource_box },
3686 /* just for testing: it will probably stay but maybe with options */
3687 { "triggerbuildpage", tex_build_page },
3688 { "getpagestate", lua_get_page_state },
3689 { "getlocallevel", lua_get_local_level },
3690 /* not the best place but better than in node */
3691 { "set_synctex_mode", lua_set_synctex_mode },
3692 { "get_synctex_mode", lua_get_synctex_mode },
3693 { "set_synctex_tag", lua_set_synctex_tag },
3694 { "get_synctex_tag", lua_get_synctex_tag },
3695 { "set_synctex_no_files", lua_set_synctex_no_files },
3696 { "force_synctex_tag", lua_force_synctex_tag },
3697 { "force_synctex_line", lua_force_synctex_line },
3698 { "set_synctex_line", lua_set_synctex_line },
3699 { "get_synctex_line", lua_get_synctex_line },
3700 /* test */
3701 { "runtoks", runtoks },
3702 { "quittoks", quittoks },
3703 { "forcehmode", forcehmode },
3704 { "getmodevalues", tex_getmodevalues },
3705 /* private */
3706 { "permitmathobsolete", tex_permitmathobsolete },
3707 /* sentinel */
3708 { NULL, NULL }
3709 };
3710
3711 int luaopen_tex(lua_State * L)
3712 {
3713 luaL_openlib(L, "tex", texlib, 0);
3714 /* *INDENT-OFF* */
3715 make_table(L, "attribute", "tex.attribute", "getattribute", "setattribute");
3716 make_table(L, "skip", "tex.skip", "getskip", "setskip");
3717 make_table(L, "glue", "tex.glue", "getglue", "setglue");
3718 make_table(L, "muskip", "tex.muskip", "getmuskip", "setmuskip");
3719 make_table(L, "muglue", "tex.muglue", "getmuglue", "setmuglue");
3720 make_table(L, "dimen", "tex.dimen", "getdimen", "setdimen");
3721 make_table(L, "count", "tex.count", "getcount", "setcount");
3722 make_table(L, "toks", "tex.toks", "gettoks", "settoks");
3723 make_table(L, "box", "tex.box", "getbox", "setbox");
3724 make_table(L, "sfcode", "tex.sfcode", "getsfcode", "setsfcode");
3725 make_table(L, "lccode", "tex.lccode", "getlccode", "setlccode");
3726 make_table(L, "uccode", "tex.uccode", "getuccode", "setuccode");
3727 make_table(L, "catcode", "tex.catcode", "getcatcode", "setcatcode");
3728 make_table(L, "mathcode", "tex.mathcode", "getmathcode", "setmathcode");
3729 make_table(L, "delcode", "tex.delcode", "getdelcode", "setdelcode");
3730 make_table(L, "lists", "tex.lists", "getlist", "setlist");
3731 make_table(L, "nest", "tex.nest", "getnest", "setnest");
3732 /* *INDENT-ON* */
3733 init_nest_lib(L);
3734 /* make the meta entries */
3735 /* fetch it back */
3736 luaL_newmetatable(L, "tex.meta");
3737 lua_pushstring(L, "__index");
3738 lua_pushcfunction(L, gettex);
3739 lua_settable(L, -3);
3740 lua_pushstring(L, "__newindex");
3741 lua_pushcfunction(L, settex);
3742 lua_settable(L, -3);
3743 lua_setmetatable(L, -2); /* meta to itself */
3744 /* initialize the I/O stack: */
3745 spindles = xmalloc(sizeof(spindle));
3746 spindle_index = 0;
3747 spindles[0].head = NULL;
3748 spindles[0].tail = NULL;
3749 spindle_size = 1;
3750 /* a somewhat odd place for this assert, maybe */
3751 if (command_names[data_cmd].id != data_cmd) {
3752 fatal_error("mismatch between tex and lua command name tables");
3753 };
3754 return 1;
3755 }