12 #include "mppsout.h" /* for mp_edge_object */
14 #define MPLIB_METATABLE "MPlib"
15 #define MPLIB_FIG_METATABLE "MPlib.fig"
17 #define xfree(A) if (A!=NULL) free(A)
20 #define is_mp(L,b) (MP *)luaL_checkudata(L,b,MPLIB_METATABLE)
21 #define is_fig(L,b) (struct mp_edge_object **)luaL_checkudata(L,b,MPLIB_FIG_METATABLE)
24 P__ZERO, P_ERROR_LINE, P_HALF_LINE, P_MAX_LINE, P_MAIN_MEMORY,
25 P_HASH_SIZE, P_HASH_PRIME, P_PARAM_SIZE, P_IN_OPEN, P_RANDOM_SEED,
26 P_INTERACTION, P_INI_VERSION, P_TROFF_MODE, P_PRINT_NAMES, P_COMMAND_LINE,
27 P_MEM_NAME, P_JOB_NAME, P_FIND_FILE, P_OPEN_FILE, P_CLOSE_FILE,
28 P_EOF_FILE, P_FLUSH_FILE, P_WRITE_ASCII, P_READ_ASCII, P_WRITE_BINARY,
29 P_READ_BINARY, P_RUN_EDITOR, P_RUN_MAKEMPX, P_SHIPOUT, P__SENTINEL,
33 const char *name; /* parameter name */
34 parm_idx idx; /* parameter index */
35 int class; /* parameter class */
38 const char *interaction_options[] =
39 { "unknownmode","batchmode","nonstopmode","scrollmode","errorstopmode", NULL};
42 parm_struct img_parms[] = {
43 {NULL, P__ZERO, 0 }, /* dummy; lua indices run from 1 */
44 {"error_line", P_ERROR_LINE, 'i' },
45 {"half_error_line", P_HALF_LINE, 'i' },
46 {"max_print_line", P_MAX_LINE, 'i' },
47 {"main_memory", P_MAIN_MEMORY, 'i' },
48 {"hash_size", P_HASH_SIZE, 'i' },
49 {"hash_prime", P_HASH_PRIME, 'i' },
50 {"param_size", P_PARAM_SIZE, 'i' },
51 {"max_in_open", P_IN_OPEN, 'i' },
52 {"random_seed", P_RANDOM_SEED, 'i' },
53 {"interaction", P_INTERACTION, 'e' },
54 {"ini_version", P_INI_VERSION, 'b' },
55 {"troff_mode", P_TROFF_MODE, 'b' },
56 {"print_found_names", P_PRINT_NAMES, 'b' },
57 {"command_line", P_COMMAND_LINE,'s' },
58 {"mem_name", P_MEM_NAME, 's' },
59 {"job_name", P_JOB_NAME, 's' },
61 {"find_file", P_FIND_FILE, 'p' },
62 {"open_file", P_OPEN_FILE, 'p' },
63 {"close_file", P_CLOSE_FILE, 'p' },
64 {"eof_file", P_EOF_FILE, 'p' },
65 {"flush_file", P_FLUSH_FILE, 'p' },
66 {"write_ascii_file", P_WRITE_ASCII, 'p' },
67 {"read_ascii_file", P_READ_ASCII, 'p' },
68 {"write_binary_file", P_WRITE_BINARY,'p' },
69 {"read_binary_file", P_READ_BINARY, 'p' },
70 {"run_editor", P_RUN_EDITOR, 'p' },
71 {"run_make_mpx", P_RUN_MAKEMPX, 'p' },
72 {"shipout_backend", P_SHIPOUT, 'p' },
74 {NULL, P__SENTINEL, 0 }
77 typedef struct _FILE_ITEM {
81 typedef struct _FILE_ITEM File;
83 /* Start by defining all the callback routines for the library
84 * except |run_make_mpx| and |run_editor|.
87 char *mplib_filetype_names[] = {"term", "error", "mp", "log", "ps",
88 "mem", "tfm", "map", "pfb", "enc", NULL};
92 char *mplib_find_file (char *fname, char *fmode, int ftype) {
96 lua_getfield(L,LUA_REGISTRYINDEX,"mplib_file_finder");
97 if (lua_isfunction(L,-1)) {
98 char *s = NULL, *x = NULL;
99 lua_pushstring(L, fname);
100 lua_pushstring(L, fmode);
101 if (ftype >= mp_filetype_text) {
102 lua_pushnumber(L, ftype-mp_filetype_text);
104 lua_pushstring(L, mplib_filetype_names[ftype]);
106 if(lua_pcall(L,3,1,0) != 0) {
107 fprintf(stdout,"Error in mp.find_file: %s\n", (char *)lua_tostring(L,-1));
110 x = (char *)lua_tostring(L,-1);
113 lua_pop(L,1); /* pop the string */
119 if (fmode[0] != 'r' || (! access (fname,R_OK)) || ftype) {
120 return strdup(fname);
125 int mplib_find_file_function (lua_State *L) {
126 if (lua_gettop(L)!=1 || (!(lua_isfunction(L,1) || lua_isnil(L,1) ))) {
129 lua_pushstring(L,"Invalid arguments to mp.find_file_function");
132 if (lua_isfunction(L,1)) {
137 lua_pushstring(L, "mplib_file_finder");
139 lua_rawset(L,LUA_REGISTRYINDEX);
144 void *term_file_ptr = NULL;
145 void *err_file_ptr = NULL;
146 void *log_file_ptr = NULL;
147 void *ps_file_ptr = NULL;
149 void *mplib_open_file(char *fname, char *fmode, int ftype) {
150 File *ff = malloc(sizeof (File));
153 if (ftype==mp_filetype_terminal) {
154 if (fmode[0] == 'r') {
157 xfree(term_file_ptr);
159 term_file_ptr = ff->f;
161 } else if (ftype==mp_filetype_error) {
164 err_file_ptr = ff->f;
165 } else if (ftype == mp_filetype_log) {
168 log_file_ptr = ff->f;
169 } else if (ftype == mp_filetype_postscript) {
175 if (fmode[0] == 'r') {
176 f = mplib_find_file(fname,fmode,ftype);
180 ff->f = fopen(f, fmode);
181 if ((fmode[0] == 'r') && (ff->f == NULL)) {
191 static char * input_data = NULL;
192 static char * input_data_ptr = NULL;
193 static size_t input_data_len = 0;
195 #define GET_CHAR() do { \
196 if (f==stdin && input_data != NULL) { \
197 if (input_data_len==0) { \
198 if (input_data_ptr!=NULL) \
199 input_data_ptr = NULL; \
205 c = *input_data_ptr++; \
212 #define UNGET_CHAR() do { \
213 if (f==stdin && input_data != NULL) { \
214 input_data_len++; input_data_ptr--; \
221 char *mplib_read_ascii_file (void *ff, size_t *size) {
223 size_t len = 0, lim = 128;
226 FILE *f = ((File *)ff)->f;
234 if (s==NULL) return NULL;
235 while (c!=EOF && c!='\n' && c!='\r') {
237 s =realloc(s, (lim+(lim>>2)));
238 if (s==NULL) return NULL;
246 if (c!=EOF && c!='\n')
255 static char *term_out = NULL;
256 static char *error_out = NULL;
257 static char *log_out = NULL;
258 static char *ps_out = NULL;
260 #define APPEND_STRING(a,b) do { \
264 a = realloc(a, strlen(a)+strlen(b)+1); \
265 strcpy(a+strlen(a),b); \
269 void mplib_write_ascii_file (void *ff, char *s) {
271 void *f = ((File *)ff)->f;
273 if (f==term_file_ptr) {
274 APPEND_STRING(term_out,s);
275 } else if (f==err_file_ptr) {
276 APPEND_STRING(error_out,s);
277 } else if (f==log_file_ptr) {
278 APPEND_STRING(log_out,s);
279 } else if (f==ps_file_ptr) {
280 APPEND_STRING(ps_out,s);
282 fprintf((FILE *)f,s);
288 void mplib_read_binary_file (void *ff, void **data, size_t *size) {
291 FILE *f = ((File *)ff)->f;
293 len = fread(*data,1,*size,f);
298 void mplib_write_binary_file (void *ff, void *s, size_t size) {
300 FILE *f = ((File *)ff)->f;
307 void mplib_close_file (void *ff) {
309 void *f = ((File *)ff)->f;
310 if (f != NULL && f != term_file_ptr && f != err_file_ptr
311 && f != log_file_ptr && f != ps_file_ptr) {
318 int mplib_eof_file (void *ff) {
320 FILE *f = ((File *)ff)->f;
323 if (f==stdin && input_data != NULL) {
324 return (input_data_len==0);
331 void mplib_flush_file (void *ff) {
335 static struct mp_edge_object *edges = NULL;
337 #define APPEND_TO_EDGES(a) do { \
341 struct mp_edge_object *p = edges; \
342 while (p->_next!=NULL) { p = p->_next; } \
347 void mplib_shipout_backend (MP mp, int h) {
348 struct mp_edge_object *hh;
349 hh = mp_gr_export(mp, h);
357 mplib_setup_file_ops(struct MP_options * options) {
358 options->find_file = mplib_find_file;
359 options->open_file = mplib_open_file;
360 options->close_file = mplib_close_file;
361 options->eof_file = mplib_eof_file;
362 options->flush_file = mplib_flush_file;
363 options->write_ascii_file = mplib_write_ascii_file;
364 options->read_ascii_file = mplib_read_ascii_file;
365 options->write_binary_file = mplib_write_binary_file;
366 options->read_binary_file = mplib_read_binary_file;
367 options->shipout_backend = mplib_shipout_backend;
371 mplib_new (lua_State *L) {
374 struct MP_options * options; /* instance options */
375 mp_ptr = lua_newuserdata(L, sizeof(MP *));
377 options = mp_options();
378 mplib_setup_file_ops(options);
379 options->noninteractive = 1; /* required ! */
380 options->print_found_names = 0;
381 if (lua_type(L,1)==LUA_TTABLE) {
382 for (i=1;img_parms[i].name!=NULL;i++) {
383 lua_getfield(L,1,img_parms[i].name);
384 if (lua_isnil(L,-1)) {
386 continue; /* skip unset */
388 switch(img_parms[i].idx) {
390 options->error_line = lua_tointeger(L,-1);
393 options->half_error_line = lua_tointeger(L,-1);
396 options->max_print_line = lua_tointeger(L,-1);
399 options->main_memory = lua_tointeger(L,-1);
402 options->hash_size = lua_tointeger(L,-1);
405 options->hash_prime = lua_tointeger(L,-1);
408 options->param_size = lua_tointeger(L,-1);
411 options->max_in_open = lua_tointeger(L,-1);
414 options->random_seed = lua_tointeger(L,-1);
417 options->interaction = luaL_checkoption(L,-1,"errorstopmode", interaction_options);
420 options->ini_version = lua_toboolean(L,-1);
423 options->troff_mode = lua_toboolean(L,-1);
426 options->print_found_names = lua_toboolean(L,-1);
429 options->command_line = strdup((char *)lua_tostring(L,-1));
432 options->mem_name = strdup((char *)lua_tostring(L,-1));
435 options->job_name = strdup((char *)lua_tostring(L,-1));
461 *mp_ptr = mp_new(options);
462 xfree(options->command_line);
463 xfree(options->mem_name);
464 xfree(options->job_name);
467 h = mp_initialize(*mp_ptr);
469 luaL_getmetatable(L,MPLIB_METATABLE);
470 lua_setmetatable(L,-2);
480 mplib_collect (lua_State *L) {
481 MP *mp_ptr = is_mp(L,1);
490 mplib_tostring (lua_State *L) {
491 MP *mp_ptr = is_mp(L,1);
493 lua_pushfstring(L,"<MP %p>",*mp_ptr);
500 mplib_run (lua_State *L) {
501 MP *mp_ptr = is_mp(L,1);
503 int h = mp_run(*mp_ptr);
512 mplib_wrapresults(lua_State *L,int h) {
514 if (term_out != NULL) {
515 lua_pushstring(L,term_out);
516 lua_setfield(L,-2,"term");
517 free(term_out); term_out = NULL;
519 if (error_out != NULL) {
520 lua_pushstring(L,error_out);
521 lua_setfield(L,-2,"error");
522 free(error_out); error_out = NULL;
524 if (log_out != NULL ) {
525 lua_pushstring(L,log_out);
526 lua_setfield(L,-2,"log");
527 free(log_out); log_out = NULL;
529 if (edges != NULL ) {
530 /* todo: convert to array */
531 struct mp_edge_object **v = lua_newuserdata (L, sizeof(struct mp_edge_object *));
532 *v = malloc(sizeof(struct mp_edge_object));
533 memcpy(*v, edges, sizeof(struct mp_edge_object));
534 luaL_getmetatable(L,MPLIB_FIG_METATABLE);
535 lua_setmetatable(L,-2);
536 lua_setfield(L,-2,"fig");
537 free(edges); edges = NULL;
540 lua_setfield(L,-2,"status");
545 mplib_execute (lua_State *L) {
546 MP *mp_ptr = is_mp(L,1);
547 if (*mp_ptr!=NULL && lua_isstring(L,2)) {
548 if (input_data_len>0) { /* this should NOT happen */
549 fprintf(stderr,"Can't do concurrency yet!\n");
551 input_data = (char *)lua_tolstring(L,2, &input_data_len);
552 input_data_ptr = input_data;
553 int h = mp_execute(*mp_ptr);
554 return mplib_wrapresults(L, h);
564 mplib_finish (lua_State *L) {
565 MP *mp_ptr = is_mp(L,1);
567 int h = mp_finish(*mp_ptr);
568 return mplib_wrapresults(L, h);
577 mplib_fig_collect (lua_State *L) {
578 struct mp_edge_object **hh = is_fig(L,1);
580 mp_gr_toss_objects((*hh)->_parent,*hh);
588 mplib_fig_tostring (lua_State *L) {
589 struct mp_edge_object **hh = is_fig(L,1);
590 lua_pushfstring(L,"<figure %p>",*hh);
595 mp_wrapped_shipout (struct mp_edge_object *hh, int prologues, int procset) {
597 if (setjmp(mp->jump_buf)) {
600 mp_gr_ship_out(hh,prologues,procset);
605 mplib_fig_postscript (lua_State *L) {
606 struct mp_edge_object **hh = is_fig(L,1);
607 int prologues = luaL_optnumber(L,2,-1);
608 int procset = luaL_optnumber(L,3,-1);
609 if (ps_out == NULL) {
610 if (mp_wrapped_shipout(*hh,prologues, procset)) {
612 lua_pushstring(L, ps_out);
613 free(ps_out); ps_out = NULL;
620 lua_pushstring(L,log_out);
621 free(ps_out); ps_out = NULL;
630 mplib_fig_bb (lua_State *L) {
631 struct mp_edge_object **hh = is_fig(L,1);
633 lua_pushnumber(L, (double)(*hh)->_minx/65536.0);
635 lua_pushnumber(L, (double)(*hh)->_miny/65536.0);
637 lua_pushnumber(L, (double)(*hh)->_maxx/65536.0);
639 lua_pushnumber(L, (double)(*hh)->_maxy/65536.0);
646 static const struct luaL_reg mplib_meta[] = {
647 {"__gc", mplib_collect},
648 {"__tostring", mplib_tostring},
649 {NULL, NULL} /* sentinel */
652 static const struct luaL_reg mplib_fig_meta[] = {
653 {"__gc", mplib_fig_collect},
654 {"__tostring", mplib_fig_tostring},
655 {"postscript", mplib_fig_postscript},
656 {"boundingbox", mplib_fig_bb},
657 {NULL, NULL} /* sentinel */
660 static const struct luaL_reg mplib_d [] = {
662 {"execute", mplib_execute },
663 {"finish", mplib_finish },
664 {NULL, NULL} /* sentinel */
668 static const struct luaL_reg mplib_m[] = {
670 {"find_file_function", mplib_find_file_function },
671 {NULL, NULL} /* sentinel */
676 luaopen_mp (lua_State *L) {
677 luaL_newmetatable(L,MPLIB_FIG_METATABLE);
678 lua_pushvalue(L, -1); /* push metatable */
679 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
680 luaL_register(L, NULL, mplib_fig_meta); /* figure meta methods */
683 luaL_newmetatable(L,MPLIB_METATABLE);
684 lua_pushvalue(L, -1); /* push metatable */
685 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
686 luaL_register(L, NULL, mplib_meta); /* meta methods */
687 luaL_register(L, NULL, mplib_d); /* dict methods */
688 luaL_register(L, "mp", mplib_m); /* module functions */