2 /* Jimsh - An interactive shell for Jim
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4 * Copyright 2009 Steve Bennett <steveb@workware.net.au>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * A copy of the license is also included in the source distribution
13 * of Jim, as a TXT file name called LICENSE.
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
27 #include "jimautoconf.h"
29 /* From initjimsh.tcl */
30 extern int Jim_initjimshInit(Jim_Interp
*interp
);
32 static void JimSetArgv(Jim_Interp
*interp
, int argc
, char *const argv
[])
35 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
37 /* Populate argv global var */
38 for (n
= 0; n
< argc
; n
++) {
39 Jim_Obj
*obj
= Jim_NewStringObj(interp
, argv
[n
], -1);
41 Jim_ListAppendElement(interp
, listObj
, obj
);
44 Jim_SetVariableStr(interp
, "argv", listObj
);
45 Jim_SetVariableStr(interp
, "argc", Jim_NewIntObj(interp
, argc
));
48 int main(int argc
, char *const argv
[])
53 if (argc
> 1 && strcmp(argv
[1], "--version") == 0) {
54 printf("%d.%d\n", JIM_VERSION
/ 100, JIM_VERSION
% 100);
58 /* Create and initialize the interpreter */
59 interp
= Jim_CreateInterp();
60 Jim_RegisterCoreCommands(interp
);
62 /* Register static extensions */
63 if (Jim_InitStaticExtensions(interp
) != JIM_OK
) {
64 Jim_MakeErrorMessage(interp
);
65 fprintf(stderr
, "%s\n", Jim_String(Jim_GetResult(interp
)));
68 Jim_SetVariableStrWithStr(interp
, "jim_argv0", argv
[0]);
69 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, argc
== 1 ? "1" : "0");
70 retcode
= Jim_initjimshInit(interp
);
73 if (retcode
== JIM_ERR
) {
74 Jim_MakeErrorMessage(interp
);
75 fprintf(stderr
, "%s\n", Jim_String(Jim_GetResult(interp
)));
77 if (retcode
!= JIM_EXIT
) {
78 JimSetArgv(interp
, 0, NULL
);
79 retcode
= Jim_InteractivePrompt(interp
);
83 if (argc
> 2 && strcmp(argv
[1], "-e") == 0) {
84 JimSetArgv(interp
, argc
- 3, argv
+ 3);
85 retcode
= Jim_Eval(interp
, argv
[2]);
86 if (retcode
!= JIM_ERR
) {
87 printf("%s\n", Jim_String(Jim_GetResult(interp
)));
91 Jim_SetVariableStr(interp
, "argv0", Jim_NewStringObj(interp
, argv
[1], -1));
92 JimSetArgv(interp
, argc
- 2, argv
+ 2);
93 retcode
= Jim_EvalFile(interp
, argv
[1]);
95 if (retcode
== JIM_ERR
) {
96 Jim_MakeErrorMessage(interp
);
97 fprintf(stderr
, "%s\n", Jim_String(Jim_GetResult(interp
)));
100 if (retcode
== JIM_EXIT
) {
101 retcode
= Jim_GetExitCode(interp
);
103 else if (retcode
== JIM_ERR
) {
109 Jim_FreeInterp(interp
);