Debian Codegraph @

texk/web2c/mfluadir/mfluac.c

raw
1 #include <string.h>
2
3 #define EXTERN extern
4
5
6 #if defined(JIT)
7 #include "mfluajitd.h"
8 #else
9 #include "mfluad.h"
10 #endif
11
12 #include <lua.h>
13 #include <lualib.h>
14 #include <lauxlib.h>
15 #include <kpathsea/kpathsea.h>
16 #if defined(JIT)
17 #include <luajit.h>
18 #endif
19
20 #include "potrace/luafunc.h"
21
22 #include "luapeg/lpeg.h"
23
24 /* otfcc */
25 #if defined(NO_OTFCC)
26 /* */
27 #else
28 extern int otfcc_build(int argc, char *argv[]);
29 extern int otfcc_dump(int argc, char *argv[]);
30 #endif
31
32 extern int luaopen_lpeg(lua_State *L);
33 extern int mfluaopen_lpeg(lua_State * L);
34
35 /* See shell_cmd_is_allowed below */
36 int shellenabledp = 1;
37 int restrictedshell = 0;
38
39 /**************************************************************/
40 /* */
41 /* MFbuiltin private functions */
42 /* */
43 /**************************************************************/
44 static lua_State *Luas[1];
45
46 /* static */
47 /* void stackdump_g(lua_State* L) */
48 /* { */
49 /* int i; */
50 /* int top = lua_gettop(L); */
51
52 /* printf("total in stack %d\n",top); */
53
54 /* for (i = 1; i <= top; i++) */
55 /* { /\* repeat for each level *\/ */
56 /* int t = lua_type(L, i); */
57 /* printf("[%d][%d] ",i,i-top-1); */
58 /* switch (t) { */
59 /* case LUA_TSTRING: /\* strings *\/ */
60 /* printf("string: '%s'\n", lua_tostring(L, i)); */
61 /* break; */
62 /* case LUA_TBOOLEAN: /\* booleans *\/ */
63 /* printf("boolean %s\n",lua_toboolean(L, i) ? "true" : "false"); */
64 /* break; */
65 /* case LUA_TNUMBER: /\* numbers *\/ */
66 /* printf("number: %g\n", lua_tonumber(L, i)); */
67 /* break; */
68 /* default: /\* other values *\/ */
69 /* printf("%s\n", lua_typename(L, t)); */
70 /* break; */
71 /* } */
72 /* printf(" "); /\* put a separator *\/ */
73 /* } */
74 /* printf("\n"); /\* end the listing *\/ */
75 /* } */
76
77
78 static
79 void priv_lua_reporterrors(lua_State *L, int status)
80 {
81 if ( status!=0 ) {
82 fprintf(stderr,"\n! %s\n",lua_tostring(L, -1));
83 lua_pop(L, 1); /* remove error message */
84 }
85 }
86
87
88 /* static */
89 /* void priv_lua_writemessage(lua_State *L, char *startmsg, char *bodymsg, char *endmsg, int status) */
90 /* { */
91 /* (void) L; */
92 /* if ( status!=0 ) { */
93 /* fprintf(stdout,"%s%s%s\n",startmsg,bodymsg,endmsg); */
94 /* } */
95 /* } */
96
97
98 #define link_field(p) mem[p].hhfield.rh /* {the |link| field of a memory word} */
99 static int priv_mfweb_link(lua_State *L)
100 {
101 halfword p,q;
102 p = (halfword) lua_tonumber(L,1);
103 q = link_field(p);
104 lua_pushnumber(L,q);
105 return 1;
106 }
107
108 #define info_field(p) mem[p].hhfield.lhfield /* {the |info| field of a memory word} */
109 static int priv_mfweb_info(lua_State *L)
110 {
111 halfword p,q;
112 p = (halfword) lua_tonumber(L,1);
113 q = info_field(p);
114 lua_pushnumber(L,q);
115 return 1;
116 }
117
118
119 #define x_coord(p) mem[p+1].cint /* {the |x| coordinate of this knot} */
120 static int priv_mfweb_x_coord(lua_State *L)
121 {
122 halfword p;
123 int q;
124 p = (halfword) lua_tonumber(L,1);
125 q = x_coord(p);
126 lua_pushnumber(L,q);
127 return 1;
128 }
129
130 #define y_coord(p) mem[p+2].cint /* {the |y| coordinate of this knot} */
131 static int priv_mfweb_y_coord(lua_State *L)
132 {
133 halfword p,q;
134 p = (halfword) lua_tonumber(L,1);
135 q = y_coord(p);
136 lua_pushnumber(L,q);
137 return 1;
138 }
139
140
141 #define left_type(p) mem[p].hhfield.b0 /* {characterizes the path entering this knot} */
142 static int priv_mfweb_left_type(lua_State *L)
143 {
144 halfword p,q;
145 p = (halfword) lua_tonumber(L,1);
146 q = left_type(p);
147 lua_pushnumber(L,q);
148 return 1;
149 }
150
151 #define right_type(p) mem[p].hhfield.b1 /* {characterizes the path leaving this knot} */
152 static int priv_mfweb_right_type(lua_State *L)
153 {
154 halfword p,q;
155 p = (halfword) lua_tonumber(L,1);
156 q = right_type(p);
157 lua_pushnumber(L,q);
158 return 1;
159 }
160
161
162 #define left_x(p) mem[p+3].cint /* {the |x| coordinate of previous control point} */
163 static int priv_mfweb_left_x(lua_State *L)
164 {
165 halfword p;
166 integer q ;
167 p = (halfword) lua_tonumber(L,1);
168 q = left_x(p);
169 lua_pushnumber(L,q);
170 return 1;
171 }
172
173
174 #define left_y(p) mem[p+4].cint /* {the |y| coordinate of previous control point} */
175 static int priv_mfweb_left_y(lua_State *L)
176 {
177 halfword p;
178 integer q ;
179 p = (halfword) lua_tonumber(L,1);
180 q = left_y(p);
181 lua_pushnumber(L,q);
182 return 1;
183 }
184
185
186 #define right_x(p) mem[p+5].cint /* {the |x| coordinate of next control point} */
187 static int priv_mfweb_right_x(lua_State *L)
188 {
189 halfword p;
190 integer q ;
191 p = (halfword) lua_tonumber(L,1);
192 q = right_x(p);
193 lua_pushnumber(L,q);
194 return 1;
195 }
196
197 #define right_y(p) mem[p+6].cint /* {the |y| coordinate of next control point} */
198 static int priv_mfweb_right_y(lua_State *L)
199 {
200 halfword p;
201 integer q;
202 p = (halfword) lua_tonumber(L,1);
203 q = right_y(p);
204 lua_pushnumber(L,q);
205 return 1;
206 }
207
208 /* @ Conversely, the |n_sin_cos| routine takes an |angle| and produces the sine */
209 /* and cosine of that angle. The results of this routine are */
210 /* stored in global integer variables |n_sin| and |n_cos|. */
211
212 /* @<Glob...@>= */
213 /* @!n_sin,@!n_cos:fraction; {results computed by |n_sin_cos|} */
214
215 /* @ Given an integer |z| that is $2^{20}$ times an angle $\theta$ in degrees, */
216 /* the purpose of |n_sin_cos(z)| is to set */
217 /* |x=@t$r\cos\theta$@>| and |y=@t$r\sin\theta$@>| (approximately), */
218 /* for some rather large number~|r|. The maximum of |x| and |y| */
219 /* will be between $2^{28}$ and $2^{30}$, so that there will be hardly */
220 /* any loss of accuracy. Then |x| and~|y| are divided by~|r|. */
221 static int priv_mfweb_n_sin_cos(lua_State *L)
222 {
223 nsincos(lua_tonumber(L,1));
224 return 0;
225 }
226
227
228 /* @ When a lot of work is being done on a particular edge structure, we plant */
229 /* a pointer to its main header in the global variable |cur_edges|. */
230 /* This saves us from having to pass this pointer as a parameter over and */
231 /* over again between subroutines. */
232
233 /* Similarly, |cur_wt| is a global weight that is being used by several */
234 /* procedures at once. */
235
236 /* @<Glob...@>= */
237 /* @!cur_edges:pointer; {the edge structure of current interest} */
238 /* @!cur_wt:integer; {the edge weight of current interest} */
239 static int priv_mfweb_LUAGLOBALGET_cur_edges(lua_State *L)
240 {
241 halfword p = curedges;
242 lua_pushnumber(L,p);
243 return 1;
244 }
245
246
247 /* @<Glob...@>= */
248 /* @!cur_type:small_number; {the type of the expression just found} */
249 /* @!cur_exp:integer; {the value of the expression just found} */
250 static int priv_mfweb_LUAGLOBALGET_cur_exp(lua_State *L)
251 {
252 halfword p = curexp;
253 lua_pushnumber(L,p);
254 return 1;
255 }
256
257
258
259
260
261
262 /* @d mem_top==30000 {largest index in the |mem| array dumped by \.{INIMF}; */
263 /* must be substantially larger than |mem_min| */
264 /* and not greater than |mem_max|} */
265 static int priv_mfweb_LUAGLOBALGET_mem_top(lua_State *L)
266 {
267 integer p = memtop;
268 lua_pushnumber(L,p);
269 return 1;
270 }
271
272
273
274 /* And there are two global variables that affect the rounding */
275 /* decisions, as we'll see later; they are called |cur_pen| and |cur_path_type|. */
276 /* The latter will be |double_path_code| if |make_spec| is being */
277 /* applied to a double path.*/
278 /* @!cur_pen:pointer; {an implicit input of |make_spec|, used in autorounding} */
279 static int priv_mfweb_LUAGLOBALGET_cur_pen(lua_State *L)
280 {
281 halfword p = curpen;
282 lua_pushnumber(L,p);
283 return 1;
284 }
285
286
287 /* @ The current octant code appears in a global variable. If, for example, */
288 /* we have |octant=third_octant|, it means that a curve traveling in a north to */
289 /* north-westerly direction has been rotated for the purposes of internal */
290 /* calculations so that the |move| data travels in an east to north-easterly */
291 /* direction. We want to unrotate as we update the edge structure. */
292 /* */
293 /* @<Glob...@>= */
294 /* @!octant:first_octant..sixth_octant; {the current octant of interest} */
295 static int priv_mfweb_LUAGLOBALGET_octant(lua_State *L)
296 {
297 char p = octant;
298 lua_pushnumber(L,p);
299 return 1;
300 }
301
302
303 /* @ The |make_spec| routine has an interesting side effect, namely to set */
304 /* the global variable |turning_number| to the number of times the tangent */
305 /* vector of the given cyclic path winds around the origin. */
306 /* */
307 /* @<Glob...@>= */
308 /* @!turning_number:integer; {another output of |make_spec|} */
309 static int priv_mfweb_LUAGLOBALGET_turning_number(lua_State *L)
310 {
311 integer p = turningnumber;
312 lua_pushnumber(L,p);
313 return 1;
314 }
315
316
317
318 /* @ \MF\ also has a bunch of internal parameters that a user might want to */
319 /* fuss with. Every such parameter has an identifying code number, defined here. */
320 /* Warning: these parameters must be the same of the original MF !! */
321 #define tracing_titles 1 /*show titles online when they appear} */
322 #define tracing_equations 2 /*show each variable when it becomes known} */
323 #define tracing_capsules 3 /*show capsules too} */
324 #define tracing_choices 4 /*show the control points chosen for paths} */
325 #define tracing_specs 5 /*show subdivision of paths into octants before digitizing} */
326 #define tracing_pens 6 /*show details of pens that are made} */
327 #define tracing_commands 7 /*show commands and operations before they are performed} */
328 #define tracing_restores 8 /*show when a variable or internal is restored} */
329 #define tracing_macros 9 /*show macros before they are expanded} */
330 #define tracing_edges 10 /*show digitized edges as they are computed} */
331 #define tracing_output 11 /*show digitized edges as they are output} */
332 #define tracing_stats 12 /*show memory usage at end of job} */
333 #define tracing_online 13 /*show long diagnostics on terminal and in the log file} */
334 #define year 14 /*the current year (e.g., 1984)} */
335 #define month 15 /*the current month (e.g., 3 $\equiv$ March)} */
336 #define day 16 /*the current day of the month} */
337 #define time 17 /*the number of minutes past midnight when this job started} */
338 #define char_code 18 /*the number of the next character to be output} */
339 #define char_ext 19 /*the extension code of the next character to be output} */
340 #define char_wd 20 /*the width of the next character to be output} */
341 #define char_ht 21 /*the height of the next character to be output} */
342 #define char_dp 22 /*the depth of the next character to be output} */
343 #define char_ic 23 /*the italic correction of the next character to be output} */
344 #define char_dx 24 /*the device's $x$ movement for the next character, in pixels} */
345 #define char_dy 25 /*the device's $y$ movement for the next character, in pixels} */
346 #define design_size 26 /*the unit of measure used for |char_wd..char_ic|, in points} */
347 #define hppp 27 /*the number of horizontal pixels per point} */
348 #define vppp 28 /*the number of vertical pixels per point} */
349 #define x_offset 29 /*horizontal displacement of shipped-out characters} */
350 #define y_offset 30 /*vertical displacement of shipped-out characters} */
351 #define pausing 31 /*positive to display lines on the terminal before they are read} */
352 #define showstopping 32 /*positive to stop after each \&{show} command} */
353 #define fontmaking 33 /*positive if font metric output is to be produced} */
354 #define proofing 34 /*positive for proof mode, negative to suppress output} */
355 #define smoothing 35 /*positive if moves are to be ``smoothed''} */
356 #define autorounding 36 /*controls path modification to ``good'' points} */
357 #define granularity 37 /*autorounding uses this pixel size} */
358 #define fillin 38 /*extra darkness of diagonal lines} */
359 #define turning_check 39 /*controls reorientation of clockwise paths} */
360 #define warning_check 40 /*controls error message when variable value is large} */
361 #define boundary_char 41 /*the right boundary character for ligatures} */
362 /* @d max_given_internal=41 */
363
364 static int priv_mfweb_LUAGLOBALGET_char_code(lua_State *L)
365 {
366 integer p = roundunscaled ( internal [char_code]) % 256 ;
367 lua_pushnumber(L,p);
368 return 1;
369 }
370 static int priv_mfweb_LUAGLOBALGET_char_ext(lua_State *L)
371 {
372 integer p = roundunscaled ( internal [char_ext]) ;
373 lua_pushnumber(L,p);
374 return 1;
375 }
376
377
378 static int priv_mfweb_LUAGLOBALGET_char_wd(lua_State *L)
379 {
380 integer p = internal [char_wd] ;
381 lua_pushnumber(L,p);
382 return 1;
383 }
384 static int priv_mfweb_LUAGLOBALGET_char_ht(lua_State *L)
385 {
386 integer p = internal [char_ht] ;
387 lua_pushnumber(L,p);
388 return 1;
389 }
390 static int priv_mfweb_LUAGLOBALGET_char_dp(lua_State *L)
391 {
392 integer p = internal [char_dp] ;
393 lua_pushnumber(L,p);
394 return 1;
395 }
396 static int priv_mfweb_LUAGLOBALGET_char_ic(lua_State *L)
397 {
398 integer p = internal [char_ic] ;
399 lua_pushnumber(L,p);
400 return 1;
401 }
402
403 static int priv_mfweb_LUAGLOBALGET_char_dx(lua_State *L)
404 {
405 integer p = internal [char_dx] ;
406 lua_pushnumber(L,p);
407 return 1;
408 }
409
410 static int priv_mfweb_LUAGLOBALGET_char_dy(lua_State *L)
411 {
412 integer p = internal [char_dy] ;
413 lua_pushnumber(L,p);
414 return 1;
415 }
416
417 static int priv_mfweb_LUAGLOBALGET_designsize(lua_State *L)
418 {
419 integer p = internal [design_size] ;
420 lua_pushnumber(L,p);
421 return 1;
422 }
423
424 static int priv_mfweb_LUAGLOBALGET_hppp(lua_State *L)
425 {
426 integer p = internal [hppp] ;
427 lua_pushnumber(L,p);
428 return 1;
429 }
430
431 static int priv_mfweb_LUAGLOBALGET_vppp(lua_State *L)
432 {
433 integer p = internal [vppp] ;
434 lua_pushnumber(L,p);
435 return 1;
436 }
437
438 static int priv_mfweb_LUAGLOBALGET_x_offset(lua_State *L)
439 {
440 integer p = internal [x_offset] ;
441 lua_pushnumber(L,p);
442 return 1;
443 }
444
445 static int priv_mfweb_LUAGLOBALGET_y_offset(lua_State *L)
446 {
447 integer p = internal [y_offset] ;
448 lua_pushnumber(L,p);
449 return 1;
450 }
451
452 static int priv_mfweb_LUAGLOBALGET_granularity(lua_State *L)
453 {
454 integer p = internal [granularity] ;
455 lua_pushnumber(L,p);
456 return 1;
457 }
458
459 static int priv_mfweb_LUAGLOBALGET_fillin(lua_State *L)
460 {
461 integer p = internal [fillin] ;
462 lua_pushnumber(L,p);
463 return 1;
464 }
465
466 static int priv_mfweb_LUAGLOBALGET_turning_check(lua_State *L)
467 {
468 integer p = internal [turning_check] ;
469 lua_pushnumber(L,p);
470 return 1;
471 }
472
473 static int priv_mfweb_LUAGLOBALGET_boundary_char(lua_State *L)
474 {
475 integer p = internal [boundary_char] ;
476 lua_pushnumber(L,p);
477 return 1;
478 }
479
480 static int priv_mflua_version(lua_State *L)
481 {
482 lua_pushstring(L,MFLUA_VERSION);
483 return 1;
484 }
485
486 static int priv_mflua_banner(lua_State *L)
487 {
488 lua_pushstring(L,BANNER);
489 return 1;
490 }
491
492 static int priv_mflua_developid(lua_State *L)
493 {
494 lua_pushstring(L,mflua_svn_revision);
495 return 1;
496 }
497
498
499
500 /**************************************************************/
501 /* */
502 /* Adapters for LuaJIT */
503 /* */
504 /**************************************************************/
505
506 #ifdef MFLuaJIT
507 static void lua_len (lua_State *L, int i) {
508 switch (lua_type(L, i)) {
509 case LUA_TSTRING:
510 lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
511 break;
512 case LUA_TTABLE:
513 if (!luaL_callmeta(L, i, "__len"))
514 lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
515 break;
516 case LUA_TUSERDATA:
517 if (luaL_callmeta(L, i, "__len"))
518 break;
519 /* FALLTHROUGH */
520 default:
521 luaL_error(L, "attempt to get length of a %s value",
522 lua_typename(L, lua_type(L, i)));
523 }
524 }
525
526 static int lua_absindex (lua_State *L, int i) {
527 if (i < 0 && i > LUA_REGISTRYINDEX)
528 i += lua_gettop(L) + 1;
529 return i;
530 }
531 static int lua_geti (lua_State *L, int index, lua_Integer i) {
532 index = lua_absindex(L, index);
533 lua_pushinteger(L, i);
534 lua_gettable(L, index);
535 return lua_type(L, -1);
536 }
537 static void lua_seti (lua_State *L, int index, lua_Integer i) {
538 index = lua_absindex(L, index);
539 lua_pushinteger(L, i);
540 lua_insert(L, -2);
541 lua_settable(L, index);
542 }
543 #endif
544
545
546 /**************************************************************/
547 /* */
548 /* otfcc private functions */
549 /* */
550 /**************************************************************/
551
552
553 static int priv_mflua_otf_dump(lua_State *L) {
554 if (!lua_istable(L, -1)) {
555 fprintf(stderr, "! dump: expected a table\n");
556 lua_pop(L,1);
557 return 0;
558 } else {
559 /* table is in the stack at index '-1' */
560 size_t l=0;
561 int j;
562 char **av;
563
564 lua_len (L, -1);
565 l = (size_t)(lua_tonumber(L,-1));
566 lua_pop(L,1);
567 if (l==0 )
568 return 0;
569 av = malloc((l+1) * sizeof(char*));
570 if (av==NULL) {
571 fprintf(stderr, "! dump: unable to allocate av\n");
572 return 0;
573 }
574 av[0] = malloc((strlen("/dump")+1)*sizeof(char));
575 if (av[0]==NULL) {
576 fprintf(stderr, "! dump: unable to allocate av[0]\n");
577 return 0;
578 }
579 strcpy(av[0], "/dump");
580 /* fill the av array */
581 for(j=1;j<=l;j++){
582 const char *k;
583 lua_geti(L,-1,j);
584 k = lua_tostring(L, -1);
585 av[j] = malloc((strlen(k)+1)*sizeof(char));
586 if (av[j]==NULL) {
587 fprintf(stderr, "! dump: unable to allocate av[%d]\n",j);
588 return 0;
589 }
590 strcpy(av[j], k);
591 lua_pop(L,1);
592 }
593 #if defined(NO_OTFCC)
594 /* */
595 #else
596 otfcc_dump(l+1,av);
597 #endif
598 /* release the av */
599 free(av[0]);
600 for(j=1;j<=l;j++)
601 free(av[j]);
602 free(av);
603 return 0;
604 }
605 }
606
607
608 static int priv_mflua_otf_build(lua_State *L) {
609 if (!lua_istable(L, -1)) {
610 fprintf(stderr, "! build: expected a table\n");
611 lua_pop(L,1);
612 return 0;
613 } else {
614 /* table is in the stack at index '-1' */
615 size_t l=0;
616 int j;
617 char **av;
618
619 lua_len (L, -1);
620 l = (size_t)(lua_tonumber(L,-1)) ;
621 lua_pop(L,1);
622 if (l==0 )
623 return 0;
624 av = malloc((l+1) * sizeof(char*));
625 if (av==NULL) {
626 fprintf(stderr, "! build: unable to allocate av\n");
627 return 0;
628 }
629 av[0] = malloc((strlen("/build")+1)*sizeof(char));
630 if (av[0]==NULL) {
631 fprintf(stderr, "! build: unable to allocate av[0]\n");
632 return 0;
633 }
634 strcpy(av[0], "/build");
635 /* fill the av array */
636 for(j=1;j<=l;j++){
637 const char *k;
638 lua_geti(L,-1,j);
639 k = lua_tostring(L, -1);
640 av[j] = malloc((strlen(k)+1)*sizeof(char));
641 if (av[j]==NULL) {
642 fprintf(stderr, "! build: unable to allocate av[%d]\n",j);
643 return 0;
644 }
645 strcpy(av[j], k);
646 lua_pop(L,1);
647 }
648 #if defined(NO_OTFCC)
649 /* */
650 #else
651 otfcc_build(l+1,av);
652 #endif
653 /* release the av */
654 free(av[0]);
655 for(j=1;j<=l;j++)
656 free(av[j]);
657 free(av);
658 return 0;
659 }
660 }
661
662
663 /**************************************************************/
664 /* */
665 /* private potrace layer */
666 /* */
667 /**************************************************************/
668
669
670 static int priv_mflua_potrace_build(lua_State *L) {
671 int w,h;
672 size_t l;
673 const uint8_t *raster;
674 MFoutlines_param_t opt_param;
675 potrace_state_t *trace;
676 potrace_path_t *p;
677 potrace_dpoint_t (*c)[3];
678 double xoffs,yoffs;
679 int n,i,*tag,jj;
680
681 w=h=0;
682 raster=NULL;
683 l=0;
684 opt_param.xoffs = opt_param.yoffs = 0.0;
685 opt_param.potrace_param = NULL;
686 trace = NULL;
687
688 raster = (const uint8_t*) luaL_checklstring(L,1,&l);
689 if (raster==NULL) {
690 fprintf(stderr,"! raster is empty\n");
691 lua_settop(L,0);
692 return 0;
693 }
694 w = (int)luaL_checkinteger(L,2);
695 h = (int)luaL_checkinteger(L,3);
696
697 if ( (w%8==0? w/8:(w/8+1))*h != l) {
698 fprintf(stderr,"! raster dimension is wrong\n");
699 lua_settop(L,0);
700 return 0;
701 }
702
703 /* optional param table */
704 if (lua_istable(L,4)){
705 lua_getfield(L,4, "xoffs");
706 if (lua_isnumber(L,-1)){
707 opt_param.xoffs = (double)lua_tonumber(L,-1);
708 }
709 lua_pop(L,1);
710 lua_getfield(L,4, "yoffs");
711 if (lua_isnumber(L,-1)){
712 opt_param.yoffs = (double)lua_tonumber(L,-1);
713 }
714 lua_pop(L,1);
715 lua_getfield(L,4, "potrace_params");
716 if (lua_istable(L,-1)){
717 opt_param.potrace_param = potrace_param_default();
718 if (!opt_param.potrace_param) {
719 fprintf(stderr, "! Error allocating potrace_param: %s\n", strerror(errno));
720 lua_settop(L,0);
721 return 0;
722 }
723 lua_getfield(L,-1,"turdsize"); if(lua_isnumber(L,-1)){opt_param.potrace_param->turdsize =(int)(lua_tonumber(L,-1)) ;};lua_pop(L,1);
724 lua_getfield(L,-1,"alphamax"); if(lua_isnumber(L,-1)){opt_param.potrace_param->alphamax =(double)(lua_tonumber(L,-1));};lua_pop(L,1);
725 lua_getfield(L,-1,"opticurve"); if(lua_isnumber(L,-1)){opt_param.potrace_param->opticurve =(int)(lua_tonumber(L,-1)) ;};lua_pop(L,1);
726 lua_getfield(L,-1,"opttolerance"); if(lua_isnumber(L,-1)){opt_param.potrace_param->opttolerance=(double)(lua_tonumber(L,-1));};lua_pop(L,1);
727 lua_getfield(L,-1,"turnpolicy");
728 if(lua_isnumber(L,-1)){
729 switch ((int)(lua_tonumber(L,-1))) {
730 case 1:
731 opt_param.potrace_param->turnpolicy = POTRACE_TURNPOLICY_BLACK;
732 break;
733 case 2:
734 opt_param.potrace_param->turnpolicy = POTRACE_TURNPOLICY_WHITE;
735 break;
736 case 3:
737 opt_param.potrace_param->turnpolicy = POTRACE_TURNPOLICY_LEFT;
738 break;
739 case 4:
740 opt_param.potrace_param->turnpolicy = POTRACE_TURNPOLICY_RIGHT;
741 break;
742 case 5:
743 opt_param.potrace_param->turnpolicy = POTRACE_TURNPOLICY_MINORITY;
744 break;
745 case 6:
746 opt_param.potrace_param->turnpolicy = POTRACE_TURNPOLICY_MAJORITY;
747 break;;
748 case 7:
749 opt_param.potrace_param->turnpolicy = POTRACE_TURNPOLICY_RANDOM;
750 break;
751 default:
752 break;
753 }
754 }
755 lua_pop(L,1);
756 }
757 }
758 lua_pop(L,1);
759
760 if (potrace_getMFoutlines(raster,w,h,opt_param,&trace)) {
761 lua_settop(L,0);
762 return 0;
763 }
764
765 jj=1;
766 lua_newtable(L); // main table
767
768 xoffs = opt_param.xoffs;yoffs = opt_param.yoffs;
769 //printf("%%%%!PS-Adobe-3.0 EPSF-3.0\n");
770 //printf("%%%%Creator: MFLua / potrace 1.16, written by Peter Selinger 2001-2019\n");
771 //printf("%%%%BoundingBox: %d %d %d %d\n", (int)(xoffs),(int)(yoffs), w, h);
772 //printf("%%%%HiResBoundingBox: %f %f %f %f\n",xoffs,yoffs, (double)(w), (double)(h));
773 //printf("%%%%Pages: 1\n");
774 //printf("%%%%EndComments\n");
775 //printf("gsave\n");
776 /* draw each curve */
777 p = trace->plist;
778 while (p != NULL) {
779 double px,py,c1x,c1y,c2x,c2y,qx,qy;
780 int j=1;
781 lua_newtable(L); // table for the cycle
782 n = p->curve.n;
783 tag = p->curve.tag;
784 c = p->curve.c;
785 //printf("%f %f moveto\n", c[n-1][2].x+xoffs, c[n-1][2].y+yoffs);
786 px=c[n-1][2].x+xoffs;py=c[n-1][2].y+yoffs; //lua_pushnumber(L,py);lua_pushnumber(L,px);lua_seti(L,-3,j++);lua_seti(L,-2,j++);
787 for (i=0; i<n; i++) {
788 lua_pushnumber(L,py);lua_pushnumber(L,px);lua_seti(L,-3,j++);lua_seti(L,-2,j++);
789 switch (tag[i]) {
790 case POTRACE_CORNER:
791 //printf("%f %f lineto\n", c[i][1].x+xoffs, c[i][1].y+yoffs);
792 //printf("%f %f lineto\n", c[i][2].x+xoffs, c[i][2].y+yoffs);
793 c1x=px;c1y=py; lua_pushnumber(L,c1y);lua_pushnumber(L,c1x);lua_seti(L,-3,j++);lua_seti(L,-2,j++);
794 c2x=c[i][1].x+xoffs;c2y=c[i][1].y+yoffs;lua_pushnumber(L,c2y);lua_pushnumber(L,c2x);lua_seti(L,-3,j++);lua_seti(L,-2,j++);
795 qx=c2x;qy=c2y; lua_pushnumber(L,qy); lua_pushnumber(L,qx); lua_seti(L,-3,j++);lua_seti(L,-2,j++);
796 px=qx;py=qy; lua_pushnumber(L,py); lua_pushnumber(L,px); lua_seti(L,-3,j++);lua_seti(L,-2,j++);
797 c1x=px;c1y=py; lua_pushnumber(L,c1y);lua_pushnumber(L,c1x);lua_seti(L,-3,j++);lua_seti(L,-2,j++);
798 c2x=c[i][2].x+xoffs;c2y=c[i][2].y+yoffs;lua_pushnumber(L,c2y);lua_pushnumber(L,c2x);lua_seti(L,-3,j++);lua_seti(L,-2,j++);
799 qx=c2x;qy=c2y; lua_pushnumber(L,qy); lua_pushnumber(L,qx); lua_seti(L,-3,j++);lua_seti(L,-2,j++);
800 break;
801 case POTRACE_CURVETO:
802 //printf("%f %f %f %f %f %f curveto\n",
803 // c[i][0].x+xoffs, c[i][0].y+yoffs,
804 // c[i][1].x+xoffs, c[i][1].y+yoffs,
805 // c[i][2].x+xoffs, c[i][2].y+yoffs);
806 c1x=c[i][0].x+xoffs;c1y=c[i][0].y+yoffs;lua_pushnumber(L,c1y);lua_pushnumber(L,c1x);lua_seti(L,-3,j++);lua_seti(L,-2,j++);
807 c2x=c[i][1].x+xoffs;c2y=c[i][1].y+yoffs;lua_pushnumber(L,c2y);lua_pushnumber(L,c2x);lua_seti(L,-3,j++);lua_seti(L,-2,j++);
808 qx=c[i][2].x+xoffs;qy=c[i][2].y+yoffs; lua_pushnumber(L,qy); lua_pushnumber(L,qx); lua_seti(L,-3,j++);lua_seti(L,-2,j++);
809 break;
810 }
811 px=qx;py=qy;
812 }
813 /* at the end of a group of a positive path and its negative
814 children, fill. */
815 if (p->next == NULL || p->next->sign == '+') {
816 //printf("0 setgray fill\n");
817 lua_pushstring (L,"+");lua_seti(L,-2,j++);
818 } else if(p->next->sign == '-') {
819 lua_pushstring (L,"-");lua_seti(L,-2,j++);
820 }
821 /* save the cycle into the main table */
822 lua_seti(L,-2,jj++);
823 p = p->next;
824 }
825 //printf("grestore\n");
826 //printf("%%%%EOF\n");
827
828 if (trace) {
829 potrace_state_free(trace);
830 }
831 if(opt_param.potrace_param){
832 potrace_param_free(opt_param.potrace_param);
833 }
834 lua_settop(L,-1);
835 return 1;
836 //return 0;
837
838 }
839
840 /**************************************************************/
841 /* */
842 /* mflua layer */
843 /* */
844 /**************************************************************/
845 static const struct luaL_Reg MFbuiltin_l[] = {
846 {"link", priv_mfweb_link},
847 {"info", priv_mfweb_info},
848 {"x_coord", priv_mfweb_x_coord},
849 {"y_coord", priv_mfweb_y_coord},
850 {"left_type", priv_mfweb_left_type},
851 {"right_type", priv_mfweb_right_type},
852 {"left_x", priv_mfweb_left_x},
853 {"left_y", priv_mfweb_left_y},
854 {"right_x", priv_mfweb_right_x},
855 {"right_y", priv_mfweb_right_y},
856 {"n_sin_cos", priv_mfweb_n_sin_cos},
857 {"cur_edges", priv_mfweb_LUAGLOBALGET_cur_edges},
858 {"cur_exp", priv_mfweb_LUAGLOBALGET_cur_exp},
859 {"mem_top", priv_mfweb_LUAGLOBALGET_mem_top},
860 {"cur_pen", priv_mfweb_LUAGLOBALGET_cur_pen},
861 {"octant", priv_mfweb_LUAGLOBALGET_octant},
862 {"char_code", priv_mfweb_LUAGLOBALGET_char_code},
863 {"char_ext", priv_mfweb_LUAGLOBALGET_char_ext},
864 {"char_wd", priv_mfweb_LUAGLOBALGET_char_wd},
865 {"char_ht", priv_mfweb_LUAGLOBALGET_char_ht},
866 {"char_dp", priv_mfweb_LUAGLOBALGET_char_dp},
867 {"char_ic", priv_mfweb_LUAGLOBALGET_char_ic},
868 {"char_dx", priv_mfweb_LUAGLOBALGET_char_dx},
869 {"char_dy", priv_mfweb_LUAGLOBALGET_char_dy},
870 {"designsize", priv_mfweb_LUAGLOBALGET_designsize},
871 {"hppp", priv_mfweb_LUAGLOBALGET_hppp},
872 {"vppp", priv_mfweb_LUAGLOBALGET_vppp},
873 {"x_offset", priv_mfweb_LUAGLOBALGET_x_offset},
874 {"y_offset", priv_mfweb_LUAGLOBALGET_y_offset},
875 {"granularity", priv_mfweb_LUAGLOBALGET_granularity},
876 {"fillin", priv_mfweb_LUAGLOBALGET_fillin},
877 {"turning_check", priv_mfweb_LUAGLOBALGET_turning_check},
878 {"boundary_char", priv_mfweb_LUAGLOBALGET_boundary_char},
879 {"turning_number", priv_mfweb_LUAGLOBALGET_turning_number},
880 {"mflua_version",priv_mflua_version},
881 {"mflua_banner",priv_mflua_banner},
882 {"mflua_developid",priv_mflua_developid},
883 {NULL, NULL} /* sentinel */
884 };
885
886 static struct luaL_Reg mflua_otfcc[] = {
887 { "build", priv_mflua_otf_build },
888 { "dump", priv_mflua_otf_dump },
889 { NULL, NULL}
890 };
891
892 static struct luaL_Reg mflua_trace[] = {
893 { "trace", priv_mflua_potrace_build },
894 { NULL, NULL}
895 };
896
897
898
899 #define lua_swap(L) lua_insert(L, -2)
900
901 #define GETGLOBALTABLEMFLUA(a) lua_getglobal(L, "mflua");\
902 if (!lua_istable(L, -1)) { \
903 lua_pushstring(L,#a); \
904 lua_pushstring(L,":global table mflua not found"); \
905 lua_concat (L, 2); \
906 priv_lua_reporterrors(L, 1); \
907 } \
908
909
910
911 int mfluabeginprogram(void)
912 {
913 lua_State *L ;
914 char* luafile ;
915 int res ;
916
917 L = luaL_newstate();
918 luaL_openlibs(L);
919 Luas[0] = L;
920 /* register lua functions */
921 luaopen_kpse(L);
922 /* to be sure of having a clear stack */
923 lua_settop(L,0);
924 /* lpeg */
925 #ifdef MFLuaJIT
926 lua_pushcfunction(L, luaopen_lpeg);
927 lua_pushstring(L, "lpeg");
928 lua_call(L, 1, 0);
929 #else
930 luaL_requiref(L, "lpeg",luaopen_lpeg,1);
931 lua_pop(L, 1);
932 #endif
933 /* to be sure of having a clear stack */
934 lua_settop(L,0);
935
936
937 lua_getglobal(L, "mflua");
938 if (!lua_istable(L, -1)) {
939 lua_pop(L,1);
940 lua_newtable(L);
941 lua_setglobal(L,"mflua");
942 /* check it */
943 lua_getglobal(L,"mflua");
944 if (!lua_istable(L, -1)) {
945 printf("mflua table NOT registered!\n");
946 } else {
947
948 /* set the MFbuiltin table */
949 lua_pushstring(L,"MFbuiltin");
950 #ifdef MFLuaJIT
951 /* 5.1 */
952 luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_OFF);
953 lua_newtable(L);
954 luaL_register (L,NULL,MFbuiltin_l);
955 #else
956 luaL_newlib(L,MFbuiltin_l);
957 #endif
958 lua_settable(L, -3);
959
960 /* set the otfcc table */
961 lua_pushstring(L,"otf");
962 #ifdef MFLuaJIT
963 /* 5.1 */
964 lua_newtable(L);
965 luaL_register (L,NULL,mflua_otfcc);
966 #else
967 luaL_newlib(L,mflua_otfcc);
968 #endif
969 lua_settable(L, -3);
970
971 /* set the trace table */
972 lua_pushstring(L,"trace");
973 #ifdef MFLuaJIT
974 /* 5.1 */
975 lua_newtable(L);
976 luaL_register (L,NULL,mflua_trace);
977 #else
978 luaL_newlib(L,mflua_trace);
979 #endif
980 lua_settable(L, -3);
981 }
982 lua_pop(L,1);
983 }
984
985
986 luafile = kpse_find_file("mflua.lua", kpse_lua_format, 0);
987 if (luafile==NULL) {
988 res = 1;
989 lua_pushstring(L,"mflua.lua not found.");
990 priv_lua_reporterrors(L, res);
991 goto EXIT;
992 }
993 res = luaL_loadfile(L, luafile);
994 free(luafile);
995 if ( res==LUA_OK ) {
996 if((res=lua_pcall(L,0,0,0))){
997 priv_lua_reporterrors(L, res);
998 goto EXIT;
999 }
1000 } else {
1001 priv_lua_reporterrors(L, res);
1002 goto EXIT;
1003 }
1004 /* Still a chance that mflua is around */
1005 GETGLOBALTABLEMFLUA(mfluabeginprogram);
1006 if (lua_istable(L, -1)) {
1007 lua_getfield(L,-1,"begin_program");
1008 if((res=lua_pcall(L,0,0,0))) {
1009 lua_pushstring(L,"error in begin_program:");
1010 lua_swap(L);lua_concat (L, 2);
1011 priv_lua_reporterrors(L, res);
1012 }
1013 }
1014 EXIT:
1015 lua_settop(L,0);
1016 return 0;
1017 }
1018
1019 /* /\* TODO*\/ */
1020 /* int mfluaendprogram(void) */
1021 /* { */
1022 /* lua_State *L; */
1023 /* char* luafile; */
1024 /* int res; */
1025
1026 /* L = Luas[0]; */
1027 /* GETGLOBALTABLEMFLUA(mfluaendprogram); */
1028 /* luafile = kpse_find_file("end_program.lua", kpse_lua_format, 0); */
1029 /* if (luafile==NULL) { */
1030 /* res = 1; */
1031 /* lua_pushstring(L,"end_program.lua not found"); */
1032 /* priv_lua_reporterrors(L, res); */
1033 /* lua_settop(L,0); */
1034 /* return 0; */
1035 /* } */
1036 /* priv_lua_writemessage(L,"(",luafile,")",1); */
1037 /* res = luaL_loadfile(L, luafile); */
1038 /* free(luafile); */
1039 /* if ( res==0 ) { */
1040 /* res = lua_pcall(L, 0, 0, 0); */
1041 /* } */
1042 /* /\* stackdump_g(L); *\/ */
1043 /* priv_lua_reporterrors(L, res); */
1044 /* lua_settop(L,0); */
1045 /* return 0; */
1046 /* } */
1047
1048 int mfluaendprogram(void)
1049 {
1050 lua_State *L;
1051 int res;
1052
1053 L = Luas[0];
1054 GETGLOBALTABLEMFLUA(mfluaendprogram);
1055 if (lua_istable(L, -1)) {
1056 lua_getfield(L,-1,"end_program");
1057 /* do the call (0 arguments, 0 result) */
1058 if((res=lua_pcall(L, 0, 0, 0))){
1059 lua_pushstring(L,"error in end_program:");
1060 lua_swap(L);lua_concat (L, 2);
1061 priv_lua_reporterrors(L, res);
1062 }
1063 }
1064 lua_settop(L,0);
1065 return 0;
1066 }
1067
1068
1069
1070 int mfluaPREstartofMF(void)
1071 {
1072 lua_State *L;
1073 int res;
1074
1075 L = Luas[0];
1076 GETGLOBALTABLEMFLUA(mfluaPREstartofMF);
1077 if (lua_istable(L, -1)) {
1078 lua_getfield(L,-1,"PRE_start_of_MF");
1079 /* do the call (0 arguments, 0 result) */
1080 if((res=lua_pcall(L, 0, 0, 0))){
1081 lua_pushstring(L,"error in PRE_start_of_MF:");
1082 lua_swap(L);lua_concat (L, 2);
1083 priv_lua_reporterrors(L, res);
1084 }
1085 }
1086 lua_settop(L,0);
1087 return 0;
1088 }
1089
1090
1091 int mfluaPREmaincontrol(void)
1092 {
1093 lua_State *L;
1094 int res;
1095
1096 L = Luas[0];
1097 GETGLOBALTABLEMFLUA(mfluaPREmaincontrol);
1098 if (lua_istable(L, -1)) {
1099 lua_getfield(L,-1, "PRE_main_control");
1100 /* do the call (0 arguments, 0 result) */
1101 if((res=lua_pcall(L, 0, 0, 0))){
1102 lua_pushstring(L,"error in PRE_main_control:");
1103 lua_swap(L);lua_concat (L, 2);
1104 priv_lua_reporterrors(L, res);
1105 }
1106 }
1107 lua_settop(L,0);
1108 return 0;
1109 }
1110
1111 int mfluaPOSTmaincontrol(void)
1112 {
1113 lua_State *L;
1114 int res;
1115
1116 L = Luas[0];
1117 GETGLOBALTABLEMFLUA(mfluaPOSTmaincontrol);
1118 if (lua_istable(L, -1)) {
1119 lua_getfield(L,-1, "POST_main_control");
1120 /* do the call (0 arguments, 0 result) */
1121 if((res=lua_pcall(L, 0, 0, 0))){
1122 lua_pushstring(L,"error in POST_main_control:");
1123 lua_swap(L);lua_concat (L, 2);
1124 priv_lua_reporterrors(L, res);
1125 }
1126 }
1127 lua_settop(L,0);
1128 return 0;
1129
1130 }
1131
1132
1133 int mfluainitialize(void)
1134 {
1135 lua_State *L;
1136 int res;
1137
1138 L = Luas[0];
1139 GETGLOBALTABLEMFLUA(mfluainitialize);
1140 if (lua_istable(L, -1)) {
1141 lua_getfield(L,-1, "mflua_initialize");
1142 /* do the call (0 arguments, 0 result) */
1143 if((res=lua_pcall(L, 0, 0, 0))){
1144 lua_pushstring(L,"error in mflua_initialize:");
1145 lua_swap(L);lua_concat (L, 2);
1146 priv_lua_reporterrors(L, res);
1147 }
1148 }
1149 lua_settop(L,0);
1150 return 0;
1151 }
1152
1153
1154 int mfluaPOSTfinalcleanup(void)
1155 {
1156 lua_State *L;
1157 int res;
1158
1159 L = Luas[0];
1160 GETGLOBALTABLEMFLUA(mfluaPOSTfinalcleanup);
1161 if (lua_istable(L, -1)) {
1162 lua_getfield(L,-1, "POST_final_cleanup");
1163 /* do the call (0 arguments, 0 result) */
1164 if((res=lua_pcall(L, 0, 0, 0))){
1165 lua_pushstring(L,"error in POST_final_cleanup:");
1166 lua_swap(L);lua_concat (L, 2);
1167 priv_lua_reporterrors(L, res);
1168 }
1169 }
1170 lua_settop(L,0);
1171 return 0;
1172 }
1173
1174
1175 /* Not a good way: */
1176 /* 1) these definitions are taken from mfcoerc.h which is generated at run-time */
1177 /* 2) too much coupling with webc2c translation of mf.web*/
1178 /* */
1179 /* #define printdiagnostic(s, t, nuline) zprintdiagnostic((strnumber) (s), (strnumber) (t), (boolean) (nuline)) */
1180 /* #define print(s) zprint((integer) (s)) */
1181 /* #define printarg(q, n, b) zprintarg((halfword) (q), (integer) (n), (halfword) (b) */
1182 /* int mfluaprintpath P3C(halfword, h , strnumber, s , boolean, nuline) */
1183 /* { */
1184 /* fprintf(stderr,"\n! %d \n",s); */
1185 /* printdiagnostic ( 517 , s , nuline ) ; */
1186 /* println () ; */
1187 /* print ( 518 ) ; */
1188 /* println () ; */
1189 /* fprintf(stderr,"\n! %s\n","*end***************"); */
1190
1191 /* lua_State *L = Luas[0]; */
1192 /* char* file = kpse_find_file("print_path.lua", kpse_lua_format, 0); */
1193 /* int res = luaL_loadfile(L, file); */
1194 /* if (file) free (file); */
1195 /* if ( res==0 ) { */
1196 /* res = lua_pcall(L, 0, 0, 0); */
1197 /* } */
1198 /* // */
1199 /* //stackdump_g(L); */
1200
1201 /* // */
1202 /* priv_lua_reporterrors(L, res); */
1203 /* return 0; */
1204
1205 /* } */
1206
1207 int mfluaprintpath(halfword h, strnumber s, boolean nuline)
1208 {
1209 lua_State *L;
1210 int res;
1211
1212 L = Luas[0];
1213 GETGLOBALTABLEMFLUA(mfluaprintpath);
1214 if (lua_istable(L, -1)) {
1215 lua_getfield(L,-1, "printpath");
1216 lua_pushnumber(L, h); /* push 1st argument */
1217 lua_pushnumber(L, s); /* push 2nd argument */
1218 lua_pushnumber(L, nuline); /* push 3nd argument */
1219 /* do the call (3 arguments, 0 result) */
1220 if((res=lua_pcall(L, 3, 0, 0))){
1221 lua_pushstring(L,"error in printpath:");
1222 lua_swap(L);lua_concat (L, 2);
1223 priv_lua_reporterrors(L, res);
1224 }
1225 }
1226 lua_settop(L,0);
1227 return 0;
1228 }
1229
1230
1231 int mfluaprintedges(strnumber s, boolean nuline, integer xoff, integer yoff)
1232 {
1233 lua_State *L;
1234 int res;
1235
1236 L = Luas[0];
1237 GETGLOBALTABLEMFLUA(mfluaprintedges);
1238 if (lua_istable(L, -1)) {
1239 lua_getfield(L,-1, "printedges");
1240 lua_pushnumber(L, s); /* push 1st argument */
1241 lua_pushnumber(L, nuline); /* push 2nd argument */
1242 lua_pushnumber(L, xoff); /* push 3nd argument */
1243 lua_pushnumber(L, yoff); /* push 4nd argument */
1244 /* do the call (4 arguments, 0 result) */
1245 if((res=lua_pcall(L, 4, 0, 0))){
1246 lua_pushstring(L,"error in printedges:");
1247 lua_swap(L);lua_concat (L, 2);
1248 priv_lua_reporterrors(L, res);
1249 }
1250 }
1251 lua_settop(L,0);
1252 return 0;
1253 }
1254
1255
1256 /* */
1257 /* Sensor before and after offset_prep */
1258 /* */
1259 int mfluaPREoffsetprep(halfword c, halfword h)
1260 {
1261
1262 lua_State *L;
1263 int res;
1264
1265 L = Luas[0];
1266 GETGLOBALTABLEMFLUA(mfluaPREoffsetprep);
1267 if (lua_istable(L, -1)) {
1268 lua_getfield(L,-1, "PRE_offset_prep");
1269 lua_pushnumber(L, c); /* push 1st argument */
1270 lua_pushnumber(L, h); /* push 2nd argument */
1271 /* do the call (2 arguments, 0 result) */
1272 if((res=lua_pcall(L, 2, 0, 0))){
1273 lua_pushstring(L,"error in PRE_offset_prep:");
1274 lua_swap(L);lua_concat (L, 2);
1275 priv_lua_reporterrors(L, res);
1276 }
1277 }
1278 lua_settop(L,0);
1279 return 0;
1280 }
1281
1282
1283 int mfluaPOSToffsetprep(halfword c, halfword h)
1284 {
1285 lua_State *L;
1286 int res;
1287
1288 L = Luas[0];
1289 GETGLOBALTABLEMFLUA(mfluaPOSToffsetprep);
1290 if (lua_istable(L, -1)) {
1291 lua_getfield(L,-1, "POST_offset_prep");
1292 lua_pushnumber(L, c); /* push 1st argument */
1293 lua_pushnumber(L, h); /* push 2nd argument */
1294 /* do the call (2 arguments, 0 result) */
1295 if((res=lua_pcall(L, 2, 0, 0))){
1296 lua_pushstring(L,"error in POST_offset_prep:");
1297 lua_swap(L);lua_concat (L, 2);
1298 priv_lua_reporterrors(L, res);
1299 }
1300 }
1301 lua_settop(L,0);
1302 return 0;
1303 }
1304
1305
1306 int mfluaPREmakespecrhs(halfword rhs)
1307 {
1308 lua_State *L;
1309 int res;
1310
1311 L = Luas[0];
1312 GETGLOBALTABLEMFLUA(mfluaPREmakespecrhs);
1313 if (lua_istable(L, -1)) {
1314 lua_getfield(L,-1, "PRE_make_spec_rhs");
1315 lua_pushnumber(L, rhs); /* push 1st argument */
1316 /* do the call (1 arguments, 0result) */
1317 if((res=lua_pcall(L, 1, 0, 0))){
1318 lua_pushstring(L,"error in PRE_make_spec_rhs:");
1319 lua_swap(L);lua_concat (L, 2);
1320 priv_lua_reporterrors(L, res);
1321 }
1322 }
1323 lua_settop(L,0);
1324 return 0;
1325 }
1326
1327
1328 int mfluaPOSTmakespecrhs(halfword rhs)
1329 {
1330 lua_State *L;
1331 int res;
1332
1333 L = Luas[0];
1334 GETGLOBALTABLEMFLUA(mfluaPOSTmakespecrhs);
1335 if (lua_istable(L, -1)) {
1336 lua_getfield(L,-1, "POST_make_spec_rhs");
1337 lua_pushnumber(L, rhs); /* push 1st argument */
1338 /* do the call (1 arguments, 0result) */
1339 if((res=lua_pcall(L, 1, 0, 0))){
1340 lua_pushstring(L,"error in POST_make_spec_rhs:");
1341 lua_swap(L);lua_concat (L, 2);
1342 priv_lua_reporterrors(L, res);
1343 }
1344 }
1345 lua_settop(L,0);
1346 return 0;
1347 }
1348
1349
1350 int mfluaPREmakespeclhs(halfword lhs)
1351 {
1352 lua_State *L;
1353 int res;
1354
1355 L = Luas[0];
1356 GETGLOBALTABLEMFLUA(mfluaPREmakespeclhs);
1357 if (lua_istable(L, -1)) {
1358 lua_getfield(L,-1, "PRE_make_spec_lhs");
1359 lua_pushnumber(L, lhs); /* push 1st argument */
1360 /* do the call (1 arguments, 0 result) */
1361 if((res=lua_pcall(L, 1, 0, 0))){
1362 lua_pushstring(L,"error in PRE_make_spec_lhs:");
1363 lua_swap(L);lua_concat (L, 2);
1364 priv_lua_reporterrors(L, res);
1365 }
1366 }
1367 lua_settop(L,0);
1368 return 0;
1369 }
1370
1371 int mfluaPOSTmakespeclhs(halfword lhs)
1372 {
1373 lua_State *L;
1374 int res;
1375
1376 L = Luas[0];
1377 GETGLOBALTABLEMFLUA(mfluaPOSTmakespeclhs);
1378 if (lua_istable(L, -1)) {
1379 lua_getfield(L,-1, "POST_make_spec_lhs");
1380 lua_pushnumber(L, lhs); /* push 1st argument */
1381 /* do the call (1 arguments, 0 result) */
1382 if((res=lua_pcall(L, 1, 0, 0))){
1383 lua_pushstring(L,"error in POST_make_spec_lhs:");
1384 lua_swap(L);lua_concat (L, 2);
1385 priv_lua_reporterrors(L, res);
1386 }
1387 }
1388 lua_settop(L,0);
1389 return 0;
1390 }
1391
1392
1393
1394 int mfluaPREfillenveloperhs(halfword rhs)
1395 {
1396 lua_State *L;
1397 int res;
1398
1399 L = Luas[0];
1400 GETGLOBALTABLEMFLUA(mfluaPREfillenveloperhs);
1401 if (lua_istable(L, -1)) {
1402 lua_getfield(L,-1, "PRE_fill_envelope_rhs");
1403 lua_pushnumber(L, rhs); /* push 1st argument */
1404 /* do the call (1 arguments, 0 result) */
1405 if((res=lua_pcall(L, 1, 0, 0))){
1406 lua_pushstring(L,"error in PRE_fill_envelope_rhs:");
1407 lua_swap(L);lua_concat (L, 2);
1408 priv_lua_reporterrors(L, res);
1409 }
1410 }
1411 lua_settop(L,0);
1412 return 0;
1413 }
1414
1415
1416
1417 int mfluaPOSTfillenveloperhs(halfword rhs)
1418 {
1419 lua_State *L;
1420 int res;
1421
1422 L = Luas[0];
1423 GETGLOBALTABLEMFLUA(mfluaPOSTfillenveloperhs);
1424 if (lua_istable(L, -1)) {
1425 lua_getfield(L,-1, "POST_fill_envelope_rhs");
1426 lua_pushnumber(L, rhs); /* push 1st argument */
1427 /* do the call (1 arguments, 0 result) */
1428 if((res=lua_pcall(L, 1, 0, 0))){
1429 lua_pushstring(L,"error in POST_fill_envelope_rhs:");
1430 lua_swap(L);lua_concat (L, 2);
1431 priv_lua_reporterrors(L, res);
1432 }
1433 }
1434 lua_settop(L,0);
1435 return 0;
1436 }
1437
1438
1439
1440 int mfluaPREfillenvelopelhs(halfword lhs)
1441 {
1442 lua_State *L;
1443 int res;
1444
1445 L = Luas[0];
1446 GETGLOBALTABLEMFLUA(mfluaPREfillenvelopelhs);
1447 if (lua_istable(L, -1)) {
1448 lua_getfield(L,-1, "PRE_fill_envelope_lhs");
1449 lua_pushnumber(L, lhs); /* push 1st argument */
1450 /* do the call (1 arguments, 0 result) */
1451 if((res=lua_pcall(L, 1, 0, 0))){
1452 lua_pushstring(L,"error in PRE_fill_envelope_lhs:");
1453 lua_swap(L);lua_concat (L, 2);
1454 priv_lua_reporterrors(L, res);
1455 }
1456 }
1457 lua_settop(L,0);
1458 return 0;
1459 }
1460
1461
1462
1463 int mfluaPOSTfillenvelopelhs(halfword lhs)
1464 {
1465 lua_State *L;
1466 int res;
1467
1468 L = Luas[0];
1469 GETGLOBALTABLEMFLUA(mfluaPOSTfillenvelopelhs);
1470 if (lua_istable(L, -1)) {
1471 lua_getfield(L,-1, "POST_fill_envelope_lhs");
1472 lua_pushnumber(L, lhs); /* push 1st argument */
1473 /* do the call (1 arguments, 0 result) */
1474 if((res=lua_pcall(L, 1, 0, 0))){
1475 lua_pushstring(L,"error in POST_fill_envelope_lhs:");
1476 lua_swap(L);lua_concat (L, 2);
1477 priv_lua_reporterrors(L, res);
1478 }
1479 }
1480 lua_settop(L,0);
1481 return 0;
1482 }
1483
1484
1485
1486 int mfluaPREfillspecrhs(halfword rhs)
1487 {
1488 lua_State *L;
1489 int res;
1490
1491 L = Luas[0];
1492 GETGLOBALTABLEMFLUA(mfluaPREfillspecrhs);
1493 if (lua_istable(L, -1)) {
1494 lua_getfield(L,-1, "PRE_fill_spec_rhs");
1495 lua_pushnumber(L, rhs); /* push 1st argument */
1496 /* do the call (1 arguments, 0 result) */
1497 if((res=lua_pcall(L, 1, 0, 0))){
1498 lua_pushstring(L,"error in PRE_fill_spec_rhs:");
1499 lua_swap(L);lua_concat (L, 2);
1500 priv_lua_reporterrors(L, res);
1501 }
1502 }
1503 lua_settop(L,0);
1504 return 0;
1505 }
1506
1507 int mfluaPOSTfillspecrhs(halfword rhs)
1508 {
1509 lua_State *L;
1510 int res;
1511
1512 L = Luas[0];
1513 GETGLOBALTABLEMFLUA(mfluaPOSTfillspecrhs);
1514 if (lua_istable(L, -1)) {
1515 lua_getfield(L,-1, "POST_fill_spec_rhs");
1516 lua_pushnumber(L, rhs); /* push 1st argument */
1517 /* do the call (1 arguments, 0 result) */
1518 if((res=lua_pcall(L, 1, 0, 0))){
1519 lua_pushstring(L,"error in POST_fill_spec_rhs:");
1520 lua_swap(L);lua_concat (L, 2);
1521 priv_lua_reporterrors(L, res);
1522 }
1523 }
1524 lua_settop(L,0);
1525 return 0;
1526 }
1527
1528 int mfluaPREfillspeclhs(halfword lhs)
1529 {
1530 lua_State *L;
1531 int res;
1532
1533 L = Luas[0];
1534 GETGLOBALTABLEMFLUA(mfluaPREfillspeclhs);
1535 if (lua_istable(L, -1)) {
1536 lua_getfield(L,-1, "PRE_fill_spec_lhs");
1537 L = Luas[0];
1538 lua_pushnumber(L, lhs); /* push 1st argument */
1539 /* do the call (1 arguments, 0 result) */
1540 if((res=lua_pcall(L, 1, 0, 0))){
1541 lua_pushstring(L,"error in PRE_fill_spec_lhs:");
1542 lua_swap(L);lua_concat (L, 2);
1543 priv_lua_reporterrors(L, res);
1544 }
1545 }
1546 lua_settop(L,0);
1547 return 0;
1548 }
1549
1550
1551 int mfluaPOSTfillspeclhs(halfword lhs)
1552 {
1553 lua_State *L;
1554 int res;
1555
1556 L = Luas[0];
1557 GETGLOBALTABLEMFLUA(mfluaPOSTfillspeclhs);
1558 if (lua_istable(L, -1)) {
1559 lua_getfield(L,-1, "POST_fill_spec_lhs");
1560 L = Luas[0];
1561 lua_pushnumber(L, lhs); /* push 1st argument */
1562 /* do the call (1 arguments, 0 result) */
1563 if((res=lua_pcall(L, 1, 0, 0))){
1564 lua_pushstring(L,"error in POST_fill_spec_lhs:");
1565 lua_swap(L);lua_concat (L, 2);
1566 priv_lua_reporterrors(L, res);
1567 }
1568 }
1569 lua_settop(L,0);
1570 return 0;
1571 }
1572
1573
1574
1575 int mfluaPREmovetoedges(halfword lhs)
1576 {
1577 lua_State *L;
1578 int res;
1579
1580 L = Luas[0];
1581 GETGLOBALTABLEMFLUA(mfluaPREmovetoedges);
1582 if (lua_istable(L, -1)) {
1583 lua_getfield(L,-1, "PRE_move_to_edges");
1584 lua_pushnumber(L, lhs); /* push 1st argument */
1585 /* do the call (1 arguments, 0 result) */
1586 if((res=lua_pcall(L, 1, 0, 0))){
1587 lua_pushstring(L,"error in PRE_move_to_edges:");
1588 lua_swap(L);lua_concat (L, 2);
1589 priv_lua_reporterrors(L, res);
1590 }
1591 }
1592 lua_settop(L,0);
1593 return 0;
1594 }
1595
1596
1597 int mfluaPOSTmovetoedges(halfword lhs)
1598 {
1599 lua_State *L;
1600 int res;
1601
1602 L = Luas[0];
1603 GETGLOBALTABLEMFLUA(mfluaPOSTmovetoedges);
1604 if (lua_istable(L, -1)) {
1605 lua_getfield(L,-1, "POST_move_to_edges");
1606 lua_pushnumber(L, lhs); /* push 1st argument */
1607 /* do the call (1 arguments, 0 result) */
1608 if((res=lua_pcall(L, 1, 0, 0))){
1609 lua_pushstring(L,"error in POST_move_to_edges:");
1610 lua_swap(L);lua_concat (L, 2);
1611 priv_lua_reporterrors(L, res);
1612 }
1613 }
1614 lua_settop(L,0);
1615 return 0;
1616 }
1617
1618
1619 int mfluaPREmakechoices(halfword p)
1620 {
1621 lua_State *L;
1622 int res;
1623
1624 L = Luas[0];
1625 GETGLOBALTABLEMFLUA(mfluaPREmakechoices);
1626 if (lua_istable(L, -1)) {
1627 lua_getfield(L,-1,"PRE_make_choices"); /* function to be called */
1628 lua_pushnumber(L, p); /* push 1st argument */
1629 /* do the call (1 arguments, 0 result) */
1630 if((res=lua_pcall(L, 1, 0, 0))){
1631 lua_pushstring(L,"error in PRE_make_choices:");
1632 lua_swap(L);lua_concat (L, 2);
1633 priv_lua_reporterrors(L, res);
1634 }
1635 }
1636 lua_settop(L,0);
1637 return 0;
1638 }
1639
1640
1641
1642 int mfluaPOSTmakechoices(halfword p)
1643 {
1644 lua_State *L;
1645 int res;
1646
1647 L = Luas[0];
1648 GETGLOBALTABLEMFLUA(mfluaPOSTmakechoices);
1649 if (lua_istable(L, -1)) {
1650 lua_getfield(L,-1,"POST_make_choices"); /* function to be called */
1651 lua_pushnumber(L, p); /* push 1st argument */
1652 /* do the call (1 arguments, 0 result) */
1653 if((res=lua_pcall(L, 1, 0, 0))){
1654 lua_pushstring(L,"error in POST_make_choices:");
1655 lua_swap(L);lua_concat (L, 2);
1656 priv_lua_reporterrors(L, res);
1657 }
1658 }
1659 lua_settop(L,0);
1660 return 0;
1661 }
1662
1663
1664
1665 int mfluaprintretrogradeline(integer x0, integer y0, integer cur_x, integer cur_y)
1666 {
1667 lua_State *L;
1668 int res;
1669
1670 L = Luas[0];
1671 GETGLOBALTABLEMFLUA(mfluaprintretrogradeline);
1672 if (lua_istable(L, -1)) {
1673 lua_getfield(L,-1,"print_retrograde_line");
1674 lua_pushnumber(L, x0); /* push 1st argument */
1675 lua_pushnumber(L, y0); /* push 2nd argument */
1676 lua_pushnumber(L, cur_x); /* push 3th argument */
1677 lua_pushnumber(L, cur_y); /* push 4th argument */
1678 /* do the call (4 arguments, 0 result) */
1679 if((res=lua_pcall(L, 4, 0, 0))){
1680 lua_pushstring(L,"error in print_retrograde_line:");
1681 lua_swap(L);lua_concat (L, 2);
1682 priv_lua_reporterrors(L, res);
1683 }
1684 }
1685 lua_settop(L,0);
1686 return 0;
1687 }
1688
1689
1690 int mfluaPREmakeellipse(integer major_axis, integer minor_axis, integer theta_angle , integer tx_val, integer ty_val,integer q)
1691 {
1692 lua_State *L;
1693 int res;
1694
1695 L = Luas[0];
1696 GETGLOBALTABLEMFLUA(mfluaPREmakeellipse);
1697 if (lua_istable(L, -1)) {
1698 lua_getfield(L,-1,"PRE_make_ellipse");
1699 lua_pushnumber(L, major_axis); /* push 1st argument */
1700 lua_pushnumber(L, minor_axis); /* push 2nd argument */
1701 lua_pushnumber(L, theta_angle); /* push 3th argument */
1702 lua_pushnumber(L, tx_val); /* push 4th argument */
1703 lua_pushnumber(L, ty_val); /* push 5th argument */
1704 lua_pushnumber(L, q); /* push 6th argument */
1705 /* do the call (6 arguments, 0 result) */
1706 if((res=lua_pcall(L, 6, 1, 0))){
1707 lua_pushstring(L,"error in PRE_make_ellipse:");
1708 lua_swap(L);lua_concat (L, 2);
1709 priv_lua_reporterrors(L, res);
1710 }
1711 }
1712 lua_settop(L,0);
1713 return 0;
1714 }
1715
1716
1717
1718 int mfluaPOSTmakeellipse(integer major_axis, integer minor_axis, integer mfl_theta , integer mfl_tx, integer mfl_ty,integer q)
1719 {
1720 lua_State *L;
1721 int res;
1722
1723 L = Luas[0];
1724 GETGLOBALTABLEMFLUA(mfluaPOSTmakeellipse);
1725 if (lua_istable(L, -1)) {
1726 lua_getfield(L,-1,"POST_make_ellipse");
1727 lua_pushnumber(L, major_axis); /* push 1st argument */
1728 lua_pushnumber(L, minor_axis); /* push 2nd argument */
1729 lua_pushnumber(L, mfl_theta); /* push 3th argument */
1730 lua_pushnumber(L, mfl_tx); /* push 4th argument */
1731 lua_pushnumber(L, mfl_ty); /* push 5th argument */
1732 lua_pushnumber(L, q); /* push 6th argument */
1733 /* do the call (6 arguments, 0 result) */
1734 if((res=lua_pcall(L, 6, 1, 0))){
1735 lua_pushstring(L,"error in POST_make_ellipse:");
1736 lua_swap(L);lua_concat (L, 2);
1737 priv_lua_reporterrors(L, res);
1738 }
1739 }
1740 lua_settop(L,0);
1741 return 0;
1742 }
1743
1744
1745 int mfluaprinttransitionlinefrom(integer x, integer y)
1746 {
1747 lua_State *L;
1748 int res;
1749
1750 L = Luas[0];
1751 GETGLOBALTABLEMFLUA(mfluaprinttransitionlinefrom);
1752 if (lua_istable(L, -1)) {
1753 lua_getfield(L,-1,"print_transition_line_from");
1754 lua_pushnumber(L, x); /* push 1st argument */
1755 lua_pushnumber(L, y); /* push 2nd argument */
1756 /* do the call (2 arguments, 0 result) */
1757 if((res=lua_pcall(L, 2, 0, 0))){
1758 lua_pushstring(L,"error in print_transition_line_from:");
1759 lua_swap(L);lua_concat (L, 2);
1760 priv_lua_reporterrors(L, res);
1761 }
1762 }
1763 lua_settop(L,0);
1764 return 0;
1765 }
1766
1767
1768 int mfluaprinttransitionlineto(integer x, integer y)
1769 {
1770 lua_State *L;
1771 int res;
1772
1773 L = Luas[0];
1774 GETGLOBALTABLEMFLUA(mfluaprinttransitionlineto);
1775 if (lua_istable(L, -1)) {
1776 lua_getfield(L,-1,"print_transition_line_to");
1777 lua_pushnumber(L, x); /* push 1st argument */
1778 lua_pushnumber(L, y); /* push 2nd argument */
1779 /* do the call (2 arguments, 0 result) */
1780 if((res=lua_pcall(L, 2, 0, 0))){
1781 lua_pushstring(L,"error in print_transition_line_to:");
1782 lua_swap(L);lua_concat (L, 2);
1783 priv_lua_reporterrors(L, res);
1784 }
1785 }
1786 lua_settop(L,0);
1787 return 0;
1788 }
1789
1790
1791
1792 #define priv_append_char(c) do { \
1793 strpool[poolptr]=c; \
1794 poolptr++; \
1795 } while(0)
1796
1797 #define priv_str_room(l) do { \
1798 if ((poolptr + l) > maxpoolptr) { \
1799 if ((poolptr + l) > poolsize) { \
1800 fprintf(stderr,"\n! (Lua) MFLua capacity exceeded, sorry [pool size=%ld]\nIf you really absolutely need more capacity,\nyou can ask a wizard to enlarge me.\n",poolsize-initpoolptr); \
1801 exit(1); \
1802 } \
1803 maxpoolptr = poolptr + l; \
1804 } \
1805 } while (0)
1806
1807 int mfluarunscript(halfword j, halfword first_val, halfword limit)
1808 {
1809 int i ;
1810 lua_State *L ;
1811 unsigned char last_char;
1812 int error;
1813 const char *str;
1814 size_t len;
1815 L = Luas[0];
1816 last_char = strpool[j+limit-first_val] ;
1817 /* end of a C string */
1818 strpool[j+limit-first_val] = '\0';
1819 str = (const char *)(strpool+j) ;
1820 /* do the call (0 arguments, 1 result) */
1821 error = (luaL_loadstring(L, str) || lua_pcall(L, 0, 1, 0)) ;
1822 strpool[j+limit-first_val] = last_char;
1823 if(error) {
1824 priv_lua_reporterrors(L,error);
1825 } else {
1826 /* retrieve result */
1827 str = lua_tolstring(L, -1,&len);
1828 priv_str_room(len);
1829 for(i=0;i<len;i++){
1830 priv_append_char( (unsigned char)(*(str+i))) ;
1831 }
1832 lua_pop(L, 1); /* pop returned value */
1833 }
1834 return 0;
1835 }
1836
1837 /* TODO: check if shell_cmd_is_allowed makes sense */
1838 int shell_cmd_is_allowed(const char *cmd, char **safecmd, char **cmdname){return 1;}