| 1 |
/* lstatslib.c |
| 2 |
|
| 3 |
Copyright 2006-2011 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 |
|
| 21 |
#include "ptexlib.h" |
| 22 |
#include "lua/luatex-api.h" |
| 23 |
#include "luatex_svnversion.h" |
| 24 |
|
| 25 |
typedef struct statistic { |
| 26 |
const char *name; |
| 27 |
char type; |
| 28 |
void *value; |
| 29 |
} statistic; |
| 30 |
|
| 31 |
typedef const char *(*charfunc) (void); |
| 32 |
typedef lua_Number(*numfunc) (void); |
| 33 |
typedef int (*intfunc) (void); |
| 34 |
|
| 35 |
static const char *getbanner(void) |
| 36 |
{ |
| 37 |
return (const char *) luatex_banner; |
| 38 |
} |
| 39 |
|
| 40 |
static const char *getlogname(void) |
| 41 |
{ |
| 42 |
return (const char *) texmf_log_name; |
| 43 |
} |
| 44 |
|
| 45 |
static const char *get_output_file_name(void) |
| 46 |
{ |
| 47 |
if (static_pdf != NULL) |
| 48 |
return (const char *) static_pdf->file_name; |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
static const char *output_directory_option(void) |
| 53 |
{ |
| 54 |
if (output_directory != NULL) |
| 55 |
return (const char *) output_directory; |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
/* |
| 61 |
static const char *getfilename(void) |
| 62 |
{ |
| 63 |
int t = 0; |
| 64 |
int level = in_open; |
| 65 |
while ((level > 0)) { |
| 66 |
t = input_stack[level--].name_field; |
| 67 |
if (t >= STRING_OFFSET) { |
| 68 |
return (const char *) str_string(t); |
| 69 |
} |
| 70 |
} |
| 71 |
return ""; |
| 72 |
} |
| 73 |
*/ |
| 74 |
|
| 75 |
static const char *getfilename(void) |
| 76 |
{ |
| 77 |
const char * s; |
| 78 |
int level, t; |
| 79 |
level = in_open; |
| 80 |
while ((level > 0)) { |
| 81 |
s = full_source_filename_stack[level--]; |
| 82 |
if (s != NULL) { |
| 83 |
return s; |
| 84 |
} |
| 85 |
} |
| 86 |
/* old method */ |
| 87 |
level = in_open; |
| 88 |
while ((level > 0)) { |
| 89 |
t = input_stack[level--].name_field; |
| 90 |
if (t >= STRING_OFFSET) { |
| 91 |
return (const char *) str_string(t); |
| 92 |
} |
| 93 |
} |
| 94 |
return ""; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
static const char *getlasterror(void) |
| 99 |
{ |
| 100 |
return last_error; |
| 101 |
} |
| 102 |
|
| 103 |
static const char *getlastluaerror(void) |
| 104 |
{ |
| 105 |
return last_lua_error; |
| 106 |
} |
| 107 |
|
| 108 |
static const char *getlastwarningtag(void) |
| 109 |
{ |
| 110 |
return last_warning_tag; |
| 111 |
} |
| 112 |
|
| 113 |
static const char *getlastwarningstr(void) |
| 114 |
{ |
| 115 |
return last_warning_str; |
| 116 |
} |
| 117 |
|
| 118 |
static const char *getlasterrorcontext(void) |
| 119 |
{ |
| 120 |
return last_error_context; |
| 121 |
} |
| 122 |
|
| 123 |
static const char *luatexrevision(void) |
| 124 |
{ |
| 125 |
return (const char *) (strrchr(luatex_version_string, '.') + 1); |
| 126 |
} |
| 127 |
|
| 128 |
static const char *getenginename(void) |
| 129 |
{ |
| 130 |
return engine_name; |
| 131 |
} |
| 132 |
|
| 133 |
static const char * get_lc_ctype(void) |
| 134 |
{ |
| 135 |
return lc_ctype; |
| 136 |
} |
| 137 |
|
| 138 |
static const char * get_lc_collate(void) |
| 139 |
{ |
| 140 |
return lc_collate; |
| 141 |
} |
| 142 |
|
| 143 |
static const char * get_lc_numeric(void) |
| 144 |
{ |
| 145 |
return lc_numeric; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static lua_Number get_luatexhashchars(void) |
| 151 |
{ |
| 152 |
return (lua_Number) LUAI_HASHLIMIT; |
| 153 |
} |
| 154 |
|
| 155 |
static const char *get_luatexhashtype(void) |
| 156 |
{ |
| 157 |
#ifdef LuajitTeX |
| 158 |
if (jithash_hashname) |
| 159 |
return (const char *)jithash_hashname; |
| 160 |
else |
| 161 |
return "???"; |
| 162 |
#else |
| 163 |
return "lua"; |
| 164 |
#endif |
| 165 |
} |
| 166 |
|
| 167 |
static lua_Number get_pdf_gone(void) |
| 168 |
{ |
| 169 |
if (static_pdf != NULL) |
| 170 |
return (lua_Number) static_pdf->gone; |
| 171 |
return (lua_Number) 0; |
| 172 |
} |
| 173 |
|
| 174 |
static lua_Number get_pdf_ptr(void) |
| 175 |
{ |
| 176 |
if (static_pdf != NULL) |
| 177 |
return (lua_Number) (static_pdf->buf->p - static_pdf->buf->data); |
| 178 |
return (lua_Number) 0; |
| 179 |
} |
| 180 |
|
| 181 |
static lua_Number get_pdf_os_cntr(void) |
| 182 |
{ |
| 183 |
if (static_pdf != NULL) |
| 184 |
return (lua_Number) static_pdf->os->ostm_ctr; |
| 185 |
return (lua_Number) 0; |
| 186 |
} |
| 187 |
|
| 188 |
static lua_Number get_pdf_os_objidx(void) |
| 189 |
{ |
| 190 |
if (static_pdf != NULL) |
| 191 |
return (lua_Number) static_pdf->os->idx; |
| 192 |
return (lua_Number) 0; |
| 193 |
} |
| 194 |
|
| 195 |
static lua_Number get_pdf_mem_size(void) |
| 196 |
{ |
| 197 |
if (static_pdf != NULL) |
| 198 |
return (lua_Number) static_pdf->mem_size; |
| 199 |
return (lua_Number) 0; |
| 200 |
} |
| 201 |
|
| 202 |
static lua_Number get_pdf_mem_ptr(void) |
| 203 |
{ |
| 204 |
if (static_pdf != NULL) |
| 205 |
return (lua_Number) static_pdf->mem_ptr; |
| 206 |
return (lua_Number) 0; |
| 207 |
} |
| 208 |
|
| 209 |
static lua_Number get_obj_ptr(void) |
| 210 |
{ |
| 211 |
if (static_pdf != NULL) |
| 212 |
return (lua_Number) static_pdf->obj_ptr; |
| 213 |
return (lua_Number) 0; |
| 214 |
} |
| 215 |
|
| 216 |
static lua_Number get_obj_tab_size(void) |
| 217 |
{ |
| 218 |
if (static_pdf != NULL) |
| 219 |
return (lua_Number) static_pdf->obj_tab_size; |
| 220 |
return (lua_Number) 0; |
| 221 |
} |
| 222 |
|
| 223 |
static lua_Number get_dest_names_size(void) |
| 224 |
{ |
| 225 |
if (static_pdf != NULL) |
| 226 |
return (lua_Number) static_pdf->dest_names_size; |
| 227 |
return (lua_Number) 0; |
| 228 |
} |
| 229 |
|
| 230 |
static lua_Number get_dest_names_ptr(void) |
| 231 |
{ |
| 232 |
if (static_pdf != NULL) |
| 233 |
return (lua_Number) static_pdf->dest_names_ptr; |
| 234 |
return (lua_Number) 0; |
| 235 |
} |
| 236 |
|
| 237 |
static int get_hash_size(void) |
| 238 |
{ |
| 239 |
return hash_size; /* is a #define */ |
| 240 |
} |
| 241 |
|
| 242 |
static lua_Number shell_escape_state(void) |
| 243 |
{ |
| 244 |
if (shellenabledp <= 0) { |
| 245 |
/* No shell at all. */ |
| 246 |
return (lua_Number) 0; |
| 247 |
} else if (restrictedshell == 0) { |
| 248 |
/* Shell has no restriction. */ |
| 249 |
return (lua_Number) 1; |
| 250 |
} else { |
| 251 |
/* Shell has restrictions, see cnf file. */ |
| 252 |
return (lua_Number) 2; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
static lua_Number safer_option_state(void) |
| 257 |
{ |
| 258 |
if (safer_option == 0) { |
| 259 |
return (lua_Number) 0; |
| 260 |
} else { |
| 261 |
return (lua_Number) 1; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
static lua_Number luadebug_option_state(void) |
| 266 |
{ |
| 267 |
if (luadebug_option == 0) { |
| 268 |
return (lua_Number) 0; |
| 269 |
} else { |
| 270 |
return (lua_Number) 1; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
static lua_Number kpse_used_state(void) |
| 275 |
{ |
| 276 |
if (kpse_init == 1) { |
| 277 |
return (lua_Number) 1; |
| 278 |
} else { |
| 279 |
return (lua_Number) 0; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
static lua_Number get_development_id(void) |
| 284 |
{ |
| 285 |
return (lua_Number) luatex_svn_revision ; |
| 286 |
} |
| 287 |
|
| 288 |
static lua_Number get_dvi_gone(void) |
| 289 |
{ |
| 290 |
if (static_pdf != NULL) |
| 291 |
return (lua_Number) dvi_get_status_gone(static_pdf); |
| 292 |
return (lua_Number) 0; |
| 293 |
} |
| 294 |
|
| 295 |
static lua_Number get_dvi_ptr(void) |
| 296 |
{ |
| 297 |
if (static_pdf != NULL) |
| 298 |
return (lua_Number) dvi_get_status_ptr(static_pdf); |
| 299 |
return (lua_Number) 0; |
| 300 |
} |
| 301 |
|
| 302 |
/* temp, for backward compat */ |
| 303 |
|
| 304 |
static int init_pool_ptr = 0; |
| 305 |
|
| 306 |
static struct statistic stats[] = { |
| 307 |
|
| 308 |
/* most likely accessed */ |
| 309 |
|
| 310 |
{"output_active", 'b', &output_active}, |
| 311 |
{"best_page_break", 'n', &best_page_break}, |
| 312 |
|
| 313 |
{"filename", 'S', (void *) &getfilename}, |
| 314 |
{"inputid", 'g', &(iname)}, |
| 315 |
{"linenumber", 'g', &line}, |
| 316 |
|
| 317 |
{"lasterrorstring", 'S', (void *) &getlasterror}, |
| 318 |
{"lastluaerrorstring", 'S', (void *) &getlastluaerror}, |
| 319 |
{"lastwarningtag", 'S', (void *) &getlastwarningtag}, |
| 320 |
{"lastwarningstring", 'S', (void *) &getlastwarningstr}, |
| 321 |
{"lasterrorcontext", 'S', (void *) &getlasterrorcontext}, |
| 322 |
|
| 323 |
/* seldom or never accessed */ |
| 324 |
|
| 325 |
{"pdf_gone", 'N', &get_pdf_gone}, |
| 326 |
{"pdf_ptr", 'N', &get_pdf_ptr}, |
| 327 |
{"dvi_gone", 'g', &get_dvi_gone}, |
| 328 |
{"dvi_ptr", 'g', &get_dvi_ptr}, |
| 329 |
|
| 330 |
{"total_pages", 'g', &total_pages}, |
| 331 |
{"output_file_name", 'S', (void *) &get_output_file_name}, |
| 332 |
{"log_name", 'S', (void *) &getlogname}, |
| 333 |
{"banner", 'S', (void *) &getbanner}, |
| 334 |
{"luatex_version", 'G', &get_luatexversion}, |
| 335 |
{"luatex_revision", 'S', (void *) &luatexrevision}, |
| 336 |
{"development_id", 'N', &get_development_id}, |
| 337 |
{"luatex_hashtype", 'S', (void *) &get_luatexhashtype}, |
| 338 |
{"luatex_hashchars", 'N', &get_luatexhashchars}, |
| 339 |
{"luatex_engine", 'S', (void *) &getenginename}, |
| 340 |
|
| 341 |
{"ini_version", 'b', &ini_version}, |
| 342 |
|
| 343 |
{"shell_escape", 'N', &shell_escape_state}, |
| 344 |
{"safer_option", 'N', &safer_option_state}, |
| 345 |
{"luadebug_option", 'N', &luadebug_option_state}, |
| 346 |
{"kpse_used", 'N', &kpse_used_state}, |
| 347 |
{"output_directory", 'S', &output_directory_option}, |
| 348 |
/* |
| 349 |
* mem stat |
| 350 |
*/ |
| 351 |
{"var_used", 'g', &var_used}, |
| 352 |
{"dyn_used", 'g', &dyn_used}, |
| 353 |
/* |
| 354 |
* traditional tex stats |
| 355 |
*/ |
| 356 |
{"str_ptr", 'g', &str_ptr}, |
| 357 |
{"init_str_ptr", 'g', &init_str_ptr}, |
| 358 |
{"max_strings", 'g', &max_strings}, |
| 359 |
{"pool_ptr", 'g', &pool_size}, |
| 360 |
{"init_pool_ptr", 'g', &init_pool_ptr}, |
| 361 |
{"pool_size", 'g', &pool_size}, |
| 362 |
{"var_mem_max", 'g', &var_mem_max}, |
| 363 |
{"node_mem_usage", 'S', &sprint_node_mem_usage}, |
| 364 |
{"fix_mem_max", 'g', &fix_mem_max}, |
| 365 |
{"fix_mem_min", 'g', &fix_mem_min}, |
| 366 |
{"fix_mem_end", 'g', &fix_mem_end}, |
| 367 |
{"cs_count", 'g', &cs_count}, |
| 368 |
{"hash_size", 'G', &get_hash_size}, |
| 369 |
{"hash_extra", 'g', &hash_extra}, |
| 370 |
{"font_ptr", 'G', &max_font_id}, |
| 371 |
{"max_in_stack", 'g', &max_in_stack}, |
| 372 |
{"max_nest_stack", 'g', &max_nest_stack}, |
| 373 |
{"max_param_stack", 'g', &max_param_stack}, |
| 374 |
{"max_buf_stack", 'g', &max_buf_stack}, |
| 375 |
{"max_save_stack", 'g', &max_save_stack}, |
| 376 |
{"stack_size", 'g', &stack_size}, |
| 377 |
{"nest_size", 'g', &nest_size}, |
| 378 |
{"param_size", 'g', ¶m_size}, |
| 379 |
{"buf_size", 'g', &buf_size}, |
| 380 |
{"save_size", 'g', &save_size}, |
| 381 |
{"input_ptr", 'g', &input_ptr}, |
| 382 |
{"obj_ptr", 'N', &get_obj_ptr}, |
| 383 |
{"obj_tab_size", 'N', &get_obj_tab_size}, |
| 384 |
{"pdf_os_cntr", 'N', &get_pdf_os_cntr}, |
| 385 |
{"pdf_os_objidx", 'N', &get_pdf_os_objidx}, |
| 386 |
{"pdf_dest_names_ptr", 'N', &get_dest_names_ptr}, |
| 387 |
{"dest_names_size", 'N', &get_dest_names_size}, |
| 388 |
{"pdf_mem_ptr", 'N', &get_pdf_mem_ptr}, |
| 389 |
{"pdf_mem_size", 'N', &get_pdf_mem_size}, |
| 390 |
{"largest_used_mark", 'g', &biggest_used_mark}, |
| 391 |
{"luabytecodes", 'g', &luabytecode_max}, |
| 392 |
{"luabytecode_bytes", 'g', &luabytecode_bytes}, |
| 393 |
{"luastate_bytes", 'g', &luastate_bytes}, |
| 394 |
|
| 395 |
{"callbacks", 'g', &callback_count}, |
| 396 |
{"indirect_callbacks", 'g', &saved_callback_count}, /* these are file io callbacks */ |
| 397 |
|
| 398 |
{"saved_callbacks", 'g', &saved_callback_count}, |
| 399 |
{"late_callbacks", 'g', &late_callback_count}, |
| 400 |
{"direct_callbacks", 'g', &direct_callback_count}, |
| 401 |
{"function_callbacks", 'g', &function_callback_count}, |
| 402 |
|
| 403 |
{"lc_ctype", 'S', (void *) &get_lc_ctype}, |
| 404 |
{"lc_collate", 'S', (void *) &get_lc_collate}, |
| 405 |
{"lc_numeric",'S', (void *) &get_lc_numeric}, |
| 406 |
|
| 407 |
{NULL, 0, 0} |
| 408 |
}; |
| 409 |
|
| 410 |
static int stats_name_to_id(const char *name) |
| 411 |
{ |
| 412 |
int i; |
| 413 |
for (i = 0; stats[i].name != NULL; i++) { |
| 414 |
if (strcmp(stats[i].name, name) == 0) |
| 415 |
return i; |
| 416 |
} |
| 417 |
return -1; |
| 418 |
} |
| 419 |
|
| 420 |
static int do_getstat(lua_State * L, int i) |
| 421 |
{ |
| 422 |
int t; |
| 423 |
const char *st; |
| 424 |
charfunc f; |
| 425 |
intfunc g; |
| 426 |
numfunc n; |
| 427 |
int str; |
| 428 |
t = stats[i].type; |
| 429 |
switch (t) { |
| 430 |
case 'S': |
| 431 |
f = stats[i].value; |
| 432 |
st = f(); |
| 433 |
lua_pushstring(L, st); |
| 434 |
break; |
| 435 |
case 's': |
| 436 |
str = *(int *) (stats[i].value); |
| 437 |
if (str) { |
| 438 |
char *ss = makecstring(str); |
| 439 |
lua_pushstring(L, ss); |
| 440 |
free(ss); |
| 441 |
} else { |
| 442 |
lua_pushnil(L); |
| 443 |
} |
| 444 |
break; |
| 445 |
case 'N': |
| 446 |
n = stats[i].value; |
| 447 |
lua_pushinteger(L, n()); |
| 448 |
break; |
| 449 |
case 'G': |
| 450 |
g = stats[i].value; |
| 451 |
lua_pushinteger(L, g()); |
| 452 |
break; |
| 453 |
case 'g': |
| 454 |
lua_pushinteger(L, *(int *) (stats[i].value)); |
| 455 |
break; |
| 456 |
case 'B': |
| 457 |
g = stats[i].value; |
| 458 |
lua_pushboolean(L, g()); |
| 459 |
break; |
| 460 |
case 'n': |
| 461 |
if (*(halfword *) (stats[i].value) != 0) |
| 462 |
lua_nodelib_push_fast(L, *(halfword *) (stats[i].value)); |
| 463 |
else |
| 464 |
lua_pushnil(L); |
| 465 |
break; |
| 466 |
case 'b': |
| 467 |
lua_pushboolean(L, *(int *) (stats[i].value)); |
| 468 |
break; |
| 469 |
default: |
| 470 |
lua_pushnil(L); |
| 471 |
} |
| 472 |
return 1; |
| 473 |
} |
| 474 |
|
| 475 |
static int getstats(lua_State * L) |
| 476 |
{ |
| 477 |
const char *st; |
| 478 |
int i; |
| 479 |
if (lua_type(L,-1) == LUA_TSTRING) { |
| 480 |
st = lua_tostring(L, -1); |
| 481 |
i = stats_name_to_id(st); |
| 482 |
if (i >= 0) { |
| 483 |
return do_getstat(L, i); |
| 484 |
} |
| 485 |
} |
| 486 |
return 0; |
| 487 |
} |
| 488 |
|
| 489 |
static int setstats(lua_State * L) |
| 490 |
{ |
| 491 |
(void) L; |
| 492 |
return 0; |
| 493 |
} |
| 494 |
|
| 495 |
static int statslist(lua_State * L) |
| 496 |
{ |
| 497 |
int i; |
| 498 |
luaL_checkstack(L, 1, "out of stack space"); |
| 499 |
lua_newtable(L); |
| 500 |
for (i = 0; stats[i].name != NULL; i++) { |
| 501 |
luaL_checkstack(L, 2, "out of stack space"); |
| 502 |
lua_pushstring(L, stats[i].name); |
| 503 |
do_getstat(L, i); |
| 504 |
lua_rawset(L, -3); |
| 505 |
} |
| 506 |
return 1; |
| 507 |
} |
| 508 |
|
| 509 |
static int resetmessages(lua_State * L) |
| 510 |
{ |
| 511 |
xfree(last_warning_str); |
| 512 |
xfree(last_warning_tag); |
| 513 |
xfree(last_error); |
| 514 |
xfree(last_lua_error); |
| 515 |
last_warning_str = NULL; |
| 516 |
last_warning_tag = NULL; |
| 517 |
last_error = NULL; |
| 518 |
last_lua_error = NULL; |
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
static int setexitcode(lua_State * L) { |
| 523 |
defaultexitcode = luaL_checkinteger(L,1); |
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
static const struct luaL_Reg statslib[] = { |
| 528 |
{"list", statslist}, |
| 529 |
{"resetmessages", resetmessages}, |
| 530 |
{"setexitcode", setexitcode}, |
| 531 |
{NULL, NULL} /* sentinel */ |
| 532 |
}; |
| 533 |
|
| 534 |
int luaopen_stats(lua_State * L) |
| 535 |
{ |
| 536 |
luaL_openlib(L, "status", statslib, 0); |
| 537 |
luaL_newmetatable(L, "tex.stats"); |
| 538 |
lua_pushstring(L, "__index"); |
| 539 |
lua_pushcfunction(L, getstats); |
| 540 |
lua_settable(L, -3); |
| 541 |
lua_pushstring(L, "__newindex"); |
| 542 |
lua_pushcfunction(L, setstats); |
| 543 |
lua_settable(L, -3); |
| 544 |
lua_setmetatable(L, -2); /* meta to itself */ |
| 545 |
return 1; |
| 546 |
} |