aboutsummaryrefslogtreecommitdiff
path: root/tcl_setup.cxx
blob: 15eb3c3996266cf6f44161bfe708d7e3fdf45e2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "tcl_extras.h"
#include <unistd.h>
#include <limits.h>

Tcl_Interp *tcl_for_pd = NULL;

extern "C" void tcl_setup(void) {
    tclpd_setup();
}

void tclpd_setup(void) {
    if(tcl_for_pd) {
        return;
    }

    post("Tcl loader v0.1.1 - 08.2009");

    proxyinlet_setup();

    tcl_for_pd = Tcl_CreateInterp();
    Tcl_Init(tcl_for_pd);
    Tclpd_SafeInit(tcl_for_pd);

    char *dirname   = new char[PATH_MAX];
    char *dirresult = new char[PATH_MAX];
    /* nameresult is only a pointer in dirresult space so don't delete[] it. */
    char *nameresult;
    if(getcwd(dirname, PATH_MAX) < 0) {
        post("Tcl loader: FATAL: cannot get current dir");
        /* exit(69); */ return;
    }

    int fd = open_via_path(dirname, "tcl", PDSUF, dirresult, &nameresult, PATH_MAX, 1);
    if(fd >= 0) {
        close(fd);
    } else {
        post("Tcl loader: %s was not found via the -path!", "tcl" PDSUF);
    }

    Tcl_SetVar(tcl_for_pd, "DIR", dirresult, 0);
    Tcl_Eval(tcl_for_pd, "set auto_path [linsert $auto_path $DIR]");

    if(Tcl_Eval(tcl_for_pd, "source $DIR/tcl.tcl") == TCL_OK) {
        post("Tcl loader: loaded %s/tcl.tcl", dirresult);
    }

    if(Tcl_Eval(tcl_for_pd,"source $env(HOME)/.pd.tcl") == TCL_OK) {
        post("Tcl loader: loaded ~/.pd.tcl");
    }

    delete[] dirresult;
    delete[] dirname;

    sys_register_loader(tclpd_do_load_lib);
}

void tclpd_interp_error(int result) {
    post("Tcl error: %s", Tcl_GetStringResult(tcl_for_pd));
    post("  (see stderr for details)");

    fprintf(stderr, "------------------- Tcl error: -------------------\n");
    Tcl_Obj* dict = Tcl_GetReturnOptions(tcl_for_pd, result);
    Tcl_Obj* errorInfo = NULL;
    Tcl_Obj* errorInfoK = Tcl_NewStringObj("-errorinfo", -1);
    Tcl_IncrRefCount(errorInfoK);
    Tcl_DictObjGet(tcl_for_pd, dict, errorInfoK, &errorInfo);
    Tcl_DecrRefCount(errorInfoK);
    fprintf(stderr, "%s\n", Tcl_GetStringFromObj(errorInfo, 0));
    fprintf(stderr, "--------------------------------------------------\n");
}