Make manual page rendering easier.
[jimtcl/wkoszek.git] / jimsh.c
blob518b53ac868cc70370380cfacb9f97fbbdecb4ba
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.
21 #ifdef WIN32
22 #define STRICT
23 #define WIN32_LEAN_AND_MEAN
24 #include <windows.h>
25 #endif /* WIN32 */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #define JIM_EMBEDDED
32 #include "jim.h"
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: */
40 #ifdef WIN32
41 static Jim_Obj *JimGetExePath(Jim_Interp *interp, const char *argv0)
43 char path[MAX_PATH+1], *p;
44 JIM_NOTUSED(argv0);
46 GetModuleFileNameA(NULL, path, MAX_PATH);
47 if ((p = strrchr(path, '\\')) != NULL)
48 *p = 0;
49 return Jim_NewStringObj(interp, path, -1);
51 #else /* WIN32 */
52 #ifndef JIM_ANSIC
53 /* A bit complex on POSIX */
54 #include <unistd.h>
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] == '/') {
61 char *p;
63 strncpy(path, argv0, JIM_PATH_LEN);
64 p = strrchr(path, '/');
65 *(p+1) = '\0';
66 return Jim_NewStringObj(interp, path, -1);
67 } else {
68 char cwd[JIM_PATH_LEN+1];
69 char base[JIM_PATH_LEN+1], *p;
70 int l;
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);
76 l = strlen(cwd);
77 if (l > 0 && cwd[l-1] == '/')
78 cwd[l-1] = '\0';
79 p = strrchr(base, '/');
80 if (p == NULL)
81 base[0] = '\0';
82 else if (p != base)
83 *p = '\0';
84 sprintf(path, "%s/%s", cwd, base);
85 l = strlen(path);
86 if (l > 2 && path[l-2] == '/' && path[l-1] == '.')
87 path[l-1] = '\0';
88 return Jim_NewStringObj(interp, path, -1);
91 #else /* JIM_ANSIC */
92 /* ... and impossible with just ANSI C */
93 static Jim_Obj *JimGetExePath(Jim_Interp *interp, const char *argv0)
95 JIM_NOTUSED(argv0);
96 return Jim_NewStringObj(interp, "/usr/local/lib/jim/", -1);
98 #endif /* JIM_ANSIC */
99 #endif /* WIN32 */
101 static void JimLoadJimRc(Jim_Interp *interp)
103 const char *home;
104 char buf [JIM_PATH_LEN+1];
105 const char *names[] = {".jimrc", "jimrc.tcl", NULL};
106 int i;
107 FILE *fp;
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) {
114 fclose(fp);
115 if (Jim_EvalFile(interp, buf) != JIM_OK) {
116 Jim_PrintErrorMessage(interp);
118 return;
123 int main(int argc, char *const argv[])
125 int retcode, n;
126 Jim_Interp *interp;
127 Jim_Obj *listObj;
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);
152 if (argc == 1) {
153 Jim_SetVariableStrWithStr(interp, "jim_interactive", "1");
154 JimLoadJimRc(interp);
155 retcode = Jim_InteractivePrompt(interp);
156 } else {
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) {
164 retcode = 0;
166 else if (retcode == JIM_EXIT) {
167 retcode = interp->exitCode;
169 else {
170 retcode = 1;
172 Jim_FreeInterp(interp);
173 return retcode;