1 /*
2 * tkUnixInit.c --
3 *
4 * This file contains Unix-specific interpreter initialization
5 * functions.
6 *
7 * Copyright (c) 1995-1996 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * SCCS: @(#) tkUnixInit.c 1.13 96/08/22 09:23:05
13 */
14
15 #if _PACKAGE_ast
16 #include <ast.h>
17 #endif
18
19 #include "tkInt.h"
20 #include "tkUnixInt.h"
21
22 /*
23 * Default directory in which to look for libraries:
24 */
25
26 static char defaultLibraryDir[PATH_MAX];
27
28 /*
29 * The following string is the startup script executed in new
30 * interpreters. It looks on disk in several different directories
31 * for a script "tk.tcl" that is compatible with this version
32 * of Tk. The tk.tcl script does all of the real work of
33 * initialization.
34 */
35
36 static char initScript[] =
37 "proc tkInit {} {\n\
38 global tk_library tk_version tk_patchLevel env\n\
39 rename tkInit {}\n\
40 set dirs {}\n\
41 if [info exists env(TK_LIBRARY)] {\n\
42 lappend dirs $env(TK_LIBRARY)\n\
43 }\n\
44 lappend dirs $tk_library\n\
45 lappend dirs [file dirname [info library]]/lib/tk$tk_version\n\
46 set parentDir [file dirname [file dirname [info nameofexecutable]]] \n\
47 lappend dirs $parentDir/lib/tk$tk_version\n\
48 if [string match {*[ab]*} $tk_patchLevel] {\n\
49 set lib tk$tk_patchLevel\n\
50 } else {\n\
51 set lib tk$tk_version\n\
52 }\n\
53 lappend dirs [file dirname $parentDir]/$lib/library\n\
54 lappend dirs [file dirname [file dirname [info library]]]/$lib/library\n\
55 lappend dirs $parentDir/library\n\
56 foreach i $dirs {\n\
57 set tk_library $i\n\
58 if ![catch {uplevel #0 source $i/tk.tcl}] {\n\
59 return\n\
60 }\n\
61 }\n\
62 set msg \"Can't find a usable tk.tcl in the following directories: \n\"\n\
63 append msg \" $dirs\n\"\n\
64 append msg \"This probably means that Tk wasn't installed properly.\n\"\n\
65 error $msg\n\
66 }\n\
67 tkInit";
68
69 /*
70 *----------------------------------------------------------------------
71 *
72 * TkPlatformInit --
73 *
74 * Performs Unix-specific interpreter initialization related to the
75 * tk_library variable.
76 *
77 * Results:
78 * Returns a standard Tcl result. Leaves an error message or result
79 * in interp->result.
80 *
81 * Side effects:
82 * Sets "tk_library" Tcl variable, runs "tk.tcl" script.
83 *
84 *----------------------------------------------------------------------
85 */
86
87 int
TkPlatformInit(interp)88 TkPlatformInit(interp)
89 Tcl_Interp *interp;
90 {
91 char *libDir;
92
93 libDir = Tcl_GetVar(interp, "tk_library", TCL_GLOBAL_ONLY);
94 if (libDir == NULL) {
95 libDir = pathpath(LIB_DIR, "", PATH_EXECUTE|PATH_READ, defaultLibraryDir, sizeof(defaultLibraryDir));
96 if (libDir == NULL)
97 sfsprintf(defaultLibraryDir, sizeof(defaultLibraryDir), "/usr/local/%s", LIB_DIR);
98 Tcl_SetVar(interp, "tk_library", defaultLibraryDir, TCL_GLOBAL_ONLY);
99 }
100 TkCreateXEventSource();
101 return Tcl_Eval(interp, initScript);
102 }
103