signal: minor code cleanup
[jimtcl.git] / jimsh.c
blob36d1a2d8a78c23e6450ac516f6f6a29305482f34
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 int main(int argc, char *const argv[])
64 int retcode;
65 Jim_Interp *interp;
67 if (argc > 1 && strcmp(argv[1], "--version") == 0) {
68 printf("%d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100);
69 return 0;
72 /* Create and initialize the interpreter */
73 interp = Jim_CreateInterp();
74 Jim_RegisterCoreCommands(interp);
76 /* Register static extensions */
77 if (Jim_InitStaticExtensions(interp) != JIM_OK) {
78 Jim_MakeErrorMessage(interp);
79 fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
82 Jim_SetVariableStrWithStr(interp, "jim_argv0", argv[0]);
83 Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, argc == 1 ? "1" : "0");
84 retcode = Jim_initjimshInit(interp);
86 if (argc == 1) {
87 if (retcode == JIM_ERR) {
88 Jim_MakeErrorMessage(interp);
89 fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
91 if (retcode != JIM_EXIT) {
92 JimSetArgv(interp, 0, NULL);
93 retcode = Jim_InteractivePrompt(interp);
96 else {
97 if (argc > 2 && strcmp(argv[1], "-e") == 0) {
98 JimSetArgv(interp, argc - 3, argv + 3);
99 retcode = Jim_Eval(interp, argv[2]);
100 if (retcode != JIM_ERR) {
101 printf("%s\n", Jim_String(Jim_GetResult(interp)));
104 else {
105 Jim_SetVariableStr(interp, "argv0", Jim_NewStringObj(interp, argv[1], -1));
106 JimSetArgv(interp, argc - 2, argv + 2);
107 retcode = Jim_EvalFile(interp, argv[1]);
109 if (retcode == JIM_ERR) {
110 Jim_MakeErrorMessage(interp);
111 fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
114 if (retcode == JIM_EXIT) {
115 retcode = Jim_GetExitCode(interp);
117 else if (retcode == JIM_ERR) {
118 retcode = 1;
120 else {
121 retcode = 0;
123 Jim_FreeInterp(interp);
124 return retcode;