ensure that tests can find tcltest.tcl
[jimtcl.git] / jimsh.c
blobfe89649c02a2f4281777ce3ef027beaf746d08ea
1 /*
2 * jimsh - An interactive shell for Jim
4 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 * Copyright 2009 Steve Bennett <steveb@workware.net.au>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * The views and conclusions contained in the software and documentation
32 * are those of the authors and should not be interpreted as representing
33 * official policies, either expressed or implied, of the Jim Tcl Project.
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include "jim.h"
41 #include "jimautoconf.h"
43 /* From initjimsh.tcl */
44 extern int Jim_initjimshInit(Jim_Interp *interp);
46 static void JimSetArgv(Jim_Interp *interp, int argc, char *const argv[])
48 int n;
49 Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
51 /* Populate argv global var */
52 for (n = 0; n < argc; n++) {
53 Jim_Obj *obj = Jim_NewStringObj(interp, argv[n], -1);
55 Jim_ListAppendElement(interp, listObj, obj);
58 Jim_SetVariableStr(interp, "argv", listObj);
59 Jim_SetVariableStr(interp, "argc", Jim_NewIntObj(interp, argc));
62 static void JimPrintErrorMessage(Jim_Interp *interp)
64 Jim_MakeErrorMessage(interp);
65 fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
68 int main(int argc, char *const argv[])
70 int retcode;
71 Jim_Interp *interp;
73 if (argc > 1 && strcmp(argv[1], "--version") == 0) {
74 printf("%d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100);
75 return 0;
78 /* Create and initialize the interpreter */
79 interp = Jim_CreateInterp();
80 Jim_RegisterCoreCommands(interp);
82 /* Register static extensions */
83 if (Jim_InitStaticExtensions(interp) != JIM_OK) {
84 JimPrintErrorMessage(interp);
87 Jim_SetVariableStrWithStr(interp, "jim::argv0", argv[0]);
88 Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, argc == 1 ? "1" : "0");
89 retcode = Jim_initjimshInit(interp);
91 if (argc == 1) {
92 if (retcode == JIM_ERR) {
93 JimPrintErrorMessage(interp);
95 if (retcode != JIM_EXIT) {
96 JimSetArgv(interp, 0, NULL);
97 retcode = Jim_InteractivePrompt(interp);
100 else {
101 if (argc > 2 && strcmp(argv[1], "-e") == 0) {
102 JimSetArgv(interp, argc - 3, argv + 3);
103 retcode = Jim_Eval(interp, argv[2]);
104 if (retcode != JIM_ERR) {
105 printf("%s\n", Jim_String(Jim_GetResult(interp)));
108 else {
109 Jim_SetVariableStr(interp, "argv0", Jim_NewStringObj(interp, argv[1], -1));
110 JimSetArgv(interp, argc - 2, argv + 2);
111 retcode = Jim_EvalFile(interp, argv[1]);
113 if (retcode == JIM_ERR) {
114 JimPrintErrorMessage(interp);
117 if (retcode == JIM_EXIT) {
118 retcode = Jim_GetExitCode(interp);
120 else if (retcode == JIM_ERR) {
121 retcode = 1;
123 else {
124 retcode = 0;
126 Jim_FreeInterp(interp);
127 return retcode;