~ubuntu-branches/ubuntu/maverick/luatex/maverick

« back to all changes in this revision

Viewing changes to source/texk/web2c/luatexdir/lua/luainit.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2009-12-25 09:47:05 UTC
  • mfrom: (1.1.9 upstream) (4.2.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091225094705-y33rpflo8t4u9nag
Tags: 0.50.0-1
* new upstream release
* disable fix-hurd-ftbfs patch, included upstream
* disable upstram-fixes, included upstream
* disable ubuntu_libpoppler-0.11, not needed anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include <sys/stat.h>
21
21
 
22
 
#include "luatex-api.h"
 
22
#include "lua/luatex-api.h"
23
23
#include <ptexlib.h>
24
24
 
25
 
#include <luatexdir/luatexextra.h>
26
 
 
27
25
static const char _svn_version[] =
28
 
    "$Id: luainit.c 2448 2009-06-08 07:43:50Z taco $ $URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.40.6/source/texk/web2c/luatexdir/lua/luainit.c $";
 
26
    "$Id: luainit.c 3188 2009-11-23 14:46:20Z taco $ $URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.50.0/source/texk/web2c/luatexdir/lua/luainit.c $";
29
27
 
30
28
/* TH: TODO
31
29
 *
460
458
        /* adjust array for Pascal and provide extension, if needed */
461
459
        dist = strlen(dump_name) - strlen(DUMP_EXT);
462
460
        if (strstr(dump_name, DUMP_EXT) == dump_name + dist)
463
 
            DUMP_VAR = concat(" ", dump_name);
 
461
            TEX_format_default = dump_name;
464
462
        else
465
 
            DUMP_VAR = concat3(" ", dump_name, DUMP_EXT);
466
 
        DUMP_LENGTH_VAR = strlen(DUMP_VAR + 1);
 
463
            TEX_format_default = concat(dump_name, DUMP_EXT);
467
464
    } else {
468
465
        /* For dump_name to be NULL is a bug.  */
469
466
        if (!ini_version)
471
468
    }
472
469
}
473
470
 
 
471
/* lua require patch */
 
472
 
 
473
/* The lua search function.
 
474
 * When kpathsea is not initialized, then it runs the
 
475
 * normal lua function that is saved in the registry, otherwise
 
476
 * it uses kpathsea.
 
477
 */
 
478
 
 
479
/* two registry ref variables are needed: one for the actual lua 
 
480
 *  function, the other for its environment .
 
481
 */
 
482
 
 
483
static int lua_loader_function = 0;
 
484
static int lua_loader_env = 0;
 
485
 
 
486
static int luatex_kpse_lua_find(lua_State * L)
 
487
{
 
488
    const char *filename;
 
489
    const char *name;
 
490
    name = luaL_checkstring(L, 1);
 
491
    if (program_name_set == 0) {
 
492
        lua_CFunction orig_func;
 
493
        lua_rawgeti(L, LUA_REGISTRYINDEX, lua_loader_function);
 
494
        lua_rawgeti(L, LUA_REGISTRYINDEX, lua_loader_env);
 
495
        lua_replace(L, LUA_ENVIRONINDEX);
 
496
        orig_func = lua_tocfunction(L, -1);
 
497
        lua_pop(L, 1);
 
498
        return (orig_func) (L);
 
499
    }
 
500
    filename = kpse_find_file(name, kpse_lua_format, false);
 
501
    if (filename == NULL)
 
502
        return 1;               /* library not found in this path */
 
503
    if (luaL_loadfile(L, filename) != 0) {
 
504
        luaL_error(L, "error loading module %s from file %s:\n\t%s",
 
505
                   lua_tostring(L, 1), filename, lua_tostring(L, -1));
 
506
    }
 
507
    return 1;                   /* library loaded successfully */
 
508
}
 
509
 
 
510
/* The lua lib search function.
 
511
 * When kpathsea is not initialized, then it runs the
 
512
 * normal lua function that is saved in the registry, otherwise
 
513
 * it uses kpathsea.
 
514
 */
 
515
 
 
516
/* two registry ref variables are needed: one for the actual lua 
 
517
 *  function, the other for its environment .
 
518
 */
 
519
 
 
520
static int clua_loader_function = 0;
 
521
static int clua_loader_env = 0;
 
522
 
 
523
extern int loader_C_luatex(lua_State * L, const char *name,
 
524
                           const char *filename);
 
525
 
 
526
static int luatex_kpse_clua_find(lua_State * L)
 
527
{
 
528
    const char *filename;
 
529
    const char *name;
 
530
    if (safer_option)
 
531
        return 1;               /* library not found in this path */
 
532
    name = luaL_checkstring(L, 1);
 
533
    if (program_name_set == 0) {
 
534
        lua_CFunction orig_func;
 
535
        lua_rawgeti(L, LUA_REGISTRYINDEX, clua_loader_function);
 
536
        lua_rawgeti(L, LUA_REGISTRYINDEX, clua_loader_env);
 
537
        lua_replace(L, LUA_ENVIRONINDEX);
 
538
        orig_func = lua_tocfunction(L, -1);
 
539
        lua_pop(L, 1);
 
540
        return (orig_func) (L);
 
541
    }
 
542
    filename = kpse_find_file(name, kpse_clua_format, false);
 
543
    if (filename == NULL)
 
544
        return 1;               /* library not found in this path */
 
545
    return loader_C_luatex(L, name, filename);
 
546
}
 
547
 
 
548
/* Setting up the new search functions. 
 
549
 * This replaces package.loaders[2] with the function defined above.
 
550
 */
 
551
 
 
552
static void setup_lua_path(lua_State * L)
 
553
{
 
554
    lua_getglobal(L, "package");
 
555
    lua_getfield(L, -1, "loaders");
 
556
    lua_rawgeti(L, -1, 2);      /* package.loaders[2] */
 
557
    lua_getfenv(L, -1);
 
558
    lua_loader_env = luaL_ref(L, LUA_REGISTRYINDEX);
 
559
    lua_loader_function = luaL_ref(L, LUA_REGISTRYINDEX);
 
560
    lua_pushcfunction(L, luatex_kpse_lua_find);
 
561
    lua_rawseti(L, -2, 2);      /* replace the normal lua loader */
 
562
 
 
563
    lua_rawgeti(L, -1, 3);      /* package.loaders[3] */
 
564
    lua_getfenv(L, -1);
 
565
    clua_loader_env = luaL_ref(L, LUA_REGISTRYINDEX);
 
566
    clua_loader_function = luaL_ref(L, LUA_REGISTRYINDEX);
 
567
    lua_pushcfunction(L, luatex_kpse_clua_find);
 
568
    lua_rawseti(L, -2, 3);      /* replace the normal lua lib loader */
 
569
    lua_pop(L, 2);              /* pop the array and table */
 
570
}
 
571
 
 
572
/* helper variables for the safe keeping of table ids */
 
573
int tex_table_id;
 
574
int pdf_table_id;
 
575
int token_table_id;
 
576
int node_table_id;
 
577
 
 
578
extern void init_tex_table(lua_State * L);
 
579
 
 
580
#if defined(WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
 
581
char **suffixlist;
 
582
 
 
583
#  define EXE_SUFFIXES ".com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.ws;.tcl;.py;.pyw"
 
584
 
 
585
static void mk_suffixlist(void)
 
586
{
 
587
    char **p;
 
588
    char *q, *r, *v;
 
589
    int n;
 
590
 
 
591
#  if defined(__CYGWIN__)
 
592
    v = xstrdup(EXE_SUFFIXES);
 
593
#  else
 
594
    v = (char *) getenv("PATHEXT");
 
595
    if (v)                      /* strlwr() exists also in MingW */
 
596
        v = (char *) strlwr(xstrdup(v));
 
597
    else
 
598
        v = xstrdup(EXE_SUFFIXES);
 
599
#  endif
 
600
 
 
601
    q = v;
 
602
    n = 0;
 
603
 
 
604
    while ((r = strchr(q, ';')) != NULL) {
 
605
        n++;
 
606
        r++;
 
607
        q = r;
 
608
    }
 
609
    if (*q)
 
610
        n++;
 
611
    suffixlist = (char **) xmalloc((n + 2) * sizeof(char *));
 
612
    p = suffixlist;
 
613
    *p = xstrdup(".dll");
 
614
    p++;
 
615
    q = v;
 
616
    while ((r = strchr(q, ';')) != NULL) {
 
617
        *r = '\0';
 
618
        *p = xstrdup(q);
 
619
        p++;
 
620
        r++;
 
621
        q = r;
 
622
    }
 
623
    if (*q) {
 
624
        *p = xstrdup(q);
 
625
        p++;
 
626
    }
 
627
    *p = NULL;
 
628
    free(v);
 
629
}
 
630
#endif                          /* WIN32 || __MIBGW32__ || __CYGWIN__ */
 
631
 
474
632
void lua_initialize(int ac, char **av)
475
633
{
476
634
 
477
635
    char *given_file = NULL;
478
636
    int kpse_init;
479
 
    int tex_table_id;
480
 
    int pdf_table_id;
481
 
    int token_table_id;
482
 
    int node_table_id;
483
637
    /* Save to pass along to topenin.  */
484
638
    argc = ac;
485
639
    argv = av;
486
640
 
487
 
    ptexbanner = BANNER;
 
641
    ptexbanner = malloc(256);
 
642
    snprintf(ptexbanner, 256, "This is LuaTeX, Version %s-%d",
 
643
             luatex_version_string, luatex_date_info);
488
644
 
489
645
    program_invocation_name = cleaned_invocation_name(argv[0]);
490
646
 
494
650
         STREQ(argv[1], "--luaconly") || STREQ(argv[1], "--luac"))) {
495
651
        exit(luac_main(ac, av));
496
652
    }
 
653
#if defined(WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
 
654
    mk_suffixlist();
 
655
#endif                          /* WIN32 || __MIBGW32__ || __CYGWIN__ */
497
656
 
498
657
    /* Must be initialized before options are parsed.  */
499
658
    interactionoption = 4;
523
682
    luainterpreter();
524
683
 
525
684
    prepare_cmdline(Luas, argv, argc, lua_offset);      /* collect arguments */
 
685
    setup_lua_path(Luas);
526
686
 
527
687
    if (startup_filename != NULL) {
528
688
        given_file = xstrdup(startup_filename);
541
701
            fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
542
702
            exit(1);
543
703
        }
 
704
        /* */
 
705
        init_tex_table(Luas);
544
706
        if (lua_pcall(Luas, 0, 0, 0)) {
545
707
            fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
546
708
            exit(1);