aio: remove leftover debugging
[jimtcl.git] / jimsh.c
blobb4561939e773fb3a712786a5a5d15199b3837e1a
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 void usage(const char* executable_name)
70 printf("jimsh version %d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100);
71 printf("Usage: %s\n", executable_name);
72 printf("or : %s [options] [filename]\n", executable_name);
73 printf("\n");
74 printf("Without options: Interactive mode\n");
75 printf("\n");
76 printf("Options:\n");
77 printf(" --version : prints the version string\n");
78 printf(" --help : prints this text\n");
79 printf(" -e CMD : executes command CMD\n");
80 printf(" NOTE: all subsequent options will be passed as arguments to the command\n");
81 printf(" [filename|-] : executes the script contained in the named file, or from stdin if \"-\"\n");
82 printf(" NOTE: all subsequent options will be passed to the script\n\n");
85 int main(int argc, char *const argv[])
87 int retcode;
88 Jim_Interp *interp;
89 char *const orig_argv0 = argv[0];
91 /* Parse initial arguments before interpreter is started */
92 if (argc > 1 && strcmp(argv[1], "--version") == 0) {
93 printf("%d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100);
94 return 0;
96 else if (argc > 1 && strcmp(argv[1], "--help") == 0) {
97 usage(argv[0]);
98 return 0;
101 /* Create and initialize the interpreter */
102 interp = Jim_CreateInterp();
103 Jim_RegisterCoreCommands(interp);
105 /* Register static extensions */
106 if (Jim_InitStaticExtensions(interp) != JIM_OK) {
107 JimPrintErrorMessage(interp);
110 Jim_SetVariableStrWithStr(interp, "jim::argv0", orig_argv0);
111 Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, argc == 1 ? "1" : "0");
112 retcode = Jim_initjimshInit(interp);
114 if (argc == 1) {
115 /* Executable name is the only argument - start interactive prompt */
116 if (retcode == JIM_ERR) {
117 JimPrintErrorMessage(interp);
119 if (retcode != JIM_EXIT) {
120 JimSetArgv(interp, 0, NULL);
121 retcode = Jim_InteractivePrompt(interp);
124 else {
125 /* Additional arguments - interpret them */
126 if (argc > 2 && strcmp(argv[1], "-e") == 0) {
127 /* Evaluate code in subsequent argument */
128 JimSetArgv(interp, argc - 3, argv + 3);
129 retcode = Jim_Eval(interp, argv[2]);
130 if (retcode != JIM_ERR) {
131 int len;
132 const char *msg = Jim_GetString(Jim_GetResult(interp), &len);
133 fwrite(msg, len, 1, stdout);
134 putchar('\n');
137 else {
138 Jim_SetVariableStr(interp, "argv0", Jim_NewStringObj(interp, argv[1], -1));
139 JimSetArgv(interp, argc - 2, argv + 2);
140 if (strcmp(argv[1], "-") == 0) {
141 retcode = Jim_Eval(interp, "eval [info source [stdin read] stdin 1]");
142 } else {
143 retcode = Jim_EvalFile(interp, argv[1]);
146 if (retcode == JIM_ERR) {
147 JimPrintErrorMessage(interp);
150 if (retcode == JIM_EXIT) {
151 retcode = Jim_GetExitCode(interp);
153 else if (retcode == JIM_ERR) {
154 retcode = 1;
156 else {
157 retcode = 0;
159 Jim_FreeInterp(interp);
160 return retcode;