1
2/* Jimsh - An interactive shell for Jim
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4 * Copyright 2009 Steve Bennett <steveb@workware.net.au>
5 *
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
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * A copy of the license is also included in the source distribution
13 * of Jim, as a TXT file name called LICENSE.
14 *
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.
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "jim.h"
27#include "jimautoconf.h"
28
29/* From initjimsh.tcl */
30extern int Jim_initjimshInit(Jim_Interp *interp);
31
32static void JimSetArgv(Jim_Interp *interp, int argc, char *const argv[])
33{
34    int n;
35    Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
36
37    /* Populate argv global var */
38    for (n = 0; n < argc; n++) {
39        Jim_Obj *obj = Jim_NewStringObj(interp, argv[n], -1);
40
41        Jim_ListAppendElement(interp, listObj, obj);
42    }
43
44    Jim_SetVariableStr(interp, "argv", listObj);
45    Jim_SetVariableStr(interp, "argc", Jim_NewIntObj(interp, argc));
46}
47
48int main(int argc, char *const argv[])
49{
50    int retcode;
51    Jim_Interp *interp;
52
53    if (argc > 1 && strcmp(argv[1], "--version") == 0) {
54        printf("%d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100);
55        return 0;
56    }
57
58    /* Create and initialize the interpreter */
59    interp = Jim_CreateInterp();
60    Jim_RegisterCoreCommands(interp);
61
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)));
66    }
67
68    Jim_SetVariableStrWithStr(interp, "jim_argv0", argv[0]);
69    Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, argc == 1 ? "1" : "0");
70    retcode = Jim_initjimshInit(interp);
71
72    if (argc == 1) {
73        if (retcode == JIM_ERR) {
74            Jim_MakeErrorMessage(interp);
75            fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
76        }
77        if (retcode != JIM_EXIT) {
78            JimSetArgv(interp, 0, NULL);
79            retcode = Jim_InteractivePrompt(interp);
80        }
81    }
82    else {
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)));
88            }
89        }
90        else {
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]);
94        }
95        if (retcode == JIM_ERR) {
96            Jim_MakeErrorMessage(interp);
97            fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
98        }
99    }
100    if (retcode == JIM_EXIT) {
101        retcode = Jim_GetExitCode(interp);
102    }
103    else if (retcode == JIM_ERR) {
104        retcode = 1;
105    }
106    else {
107        retcode = 0;
108    }
109    Jim_FreeInterp(interp);
110    return retcode;
111}
112