1 /* Jimsh - An interactive shell for Jim
2 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
3 * Copyright 2009 Steve Bennett <steveb@workware.net.au>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * A copy of the license is also included in the source distribution
12 * of Jim, as a TXT file name called LICENSE.
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #define WIN32_LEAN_AND_MEAN
35 /* JimGetExePath try to get the absolute path of the directory
36 * of the jim binary, in order to add this path to the library path.
37 * Likely shipped libraries are in the same path too. */
39 /* That's simple on windows: */
41 static Jim_Obj
*JimGetExePath(Jim_Interp
*interp
, const char *argv0
)
43 char path
[MAX_PATH
+1], *p
;
46 GetModuleFileNameA(NULL
, path
, MAX_PATH
);
47 if ((p
= strrchr(path
, '\\')) != NULL
)
49 return Jim_NewStringObj(interp
, path
, -1);
53 /* A bit complex on POSIX */
55 static Jim_Obj
*JimGetExePath(Jim_Interp
*interp
, const char *argv0
)
57 char path
[JIM_PATH_LEN
+1];
59 /* Check if the executable was called with an absolute pathname */
60 if (argv0
[0] == '/') {
63 strncpy(path
, argv0
, JIM_PATH_LEN
);
64 p
= strrchr(path
, '/');
66 return Jim_NewStringObj(interp
, path
, -1);
68 char cwd
[JIM_PATH_LEN
+1];
69 char base
[JIM_PATH_LEN
+1], *p
;
72 strncpy(base
, argv0
, JIM_PATH_LEN
);
73 if (getcwd(cwd
, JIM_PATH_LEN
) == NULL
) {
74 return Jim_NewStringObj(interp
, "/usr/local/lib/jim/", -1);
77 if (l
> 0 && cwd
[l
-1] == '/')
79 p
= strrchr(base
, '/');
84 sprintf(path
, "%s/%s", cwd
, base
);
86 if (l
> 2 && path
[l
-2] == '/' && path
[l
-1] == '.')
88 return Jim_NewStringObj(interp
, path
, -1);
92 /* ... and impossible with just ANSI C */
93 static Jim_Obj
*JimGetExePath(Jim_Interp
*interp
, const char *argv0
)
96 return Jim_NewStringObj(interp
, "/usr/local/lib/jim/", -1);
98 #endif /* JIM_ANSIC */
101 static void JimLoadJimRc(Jim_Interp
*interp
)
104 char buf
[JIM_PATH_LEN
+1];
105 const char *names
[] = {".jimrc", "jimrc.tcl", NULL
};
109 if ((home
= getenv("HOME")) == NULL
) return;
110 for (i
= 0; names
[i
] != NULL
; i
++) {
111 if (strlen(home
)+strlen(names
[i
])+1 > JIM_PATH_LEN
) continue;
112 sprintf(buf
, "%s/%s", home
, names
[i
]);
113 if ((fp
= fopen(buf
, "r")) != NULL
) {
115 if (Jim_EvalFile(interp
, buf
) != JIM_OK
) {
116 Jim_PrintErrorMessage(interp
);
123 int main(int argc
, char *const argv
[])
129 Jim_InitEmbedded(); /* This is the first function embedders should call. */
131 /* Create and initialize the interpreter */
132 interp
= Jim_CreateInterp();
133 Jim_RegisterCoreCommands(interp
);
135 /* Append the path where the executed Jim binary is contained
136 * in the jim_libpath list. */
137 listObj
= Jim_GetVariableStr(interp
, "jim_libpath", JIM_NONE
);
138 if (Jim_IsShared(listObj
))
139 listObj
= Jim_DuplicateObj(interp
, listObj
);
140 Jim_ListAppendElement(interp
, listObj
, JimGetExePath(interp
, argv
[0]));
141 Jim_SetVariableStr(interp
, "jim_libpath", listObj
);
143 /* Populate argv and argv0 global vars */
144 listObj
= Jim_NewListObj(interp
, NULL
, 0);
145 for (n
= 2; n
< argc
; n
++) {
146 Jim_Obj
*obj
= Jim_NewStringObj(interp
, argv
[n
], -1);
147 Jim_ListAppendElement(interp
, listObj
, obj
);
150 Jim_SetVariableStr(interp
, "argv", listObj
);
153 Jim_SetVariableStrWithStr(interp
, "jim_interactive", "1");
154 JimLoadJimRc(interp
);
155 retcode
= Jim_InteractivePrompt(interp
);
157 Jim_SetVariableStr(interp
, "argv0", Jim_NewStringObj(interp
, argv
[1], -1));
158 Jim_SetVariableStrWithStr(interp
, "jim_interactive", "0");
159 if ((retcode
= Jim_EvalFile(interp
, argv
[1])) == JIM_ERR
) {
160 Jim_PrintErrorMessage(interp
);
163 if (retcode
== JIM_OK
) {
166 else if (retcode
== JIM_EXIT
) {
167 retcode
= interp
->exitCode
;
172 Jim_FreeInterp(interp
);