tcltest: do a better job of cleanup up after tests
[jimtcl.git] / jim-interactive.c
blob1d16ce0d15b6ffd9d8d5aa029c2c18eba5b82953
1 #include <errno.h>
2 #include <string.h>
4 #include "jimautoconf.h"
5 #include <jim.h>
7 #ifdef USE_LINENOISE
8 #ifdef HAVE_UNISTD_H
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #endif
12 #include "linenoise.h"
13 #else
14 #define MAX_LINE_LEN 512
15 #endif
17 /**
18 * Returns an allocated line, or NULL if EOF.
20 char *Jim_HistoryGetline(const char *prompt)
22 #ifdef USE_LINENOISE
23 return linenoise(prompt);
24 #else
25 int len;
26 char *line = malloc(MAX_LINE_LEN);
28 fputs(prompt, stdout);
29 fflush(stdout);
31 if (fgets(line, MAX_LINE_LEN, stdin) == NULL) {
32 free(line);
33 return NULL;
35 len = strlen(line);
36 if (len && line[len - 1] == '\n') {
37 line[len - 1] = '\0';
39 return line;
40 #endif
43 void Jim_HistoryLoad(const char *filename)
45 #ifdef USE_LINENOISE
46 linenoiseHistoryLoad(filename);
47 #endif
50 void Jim_HistoryAdd(const char *line)
52 #ifdef USE_LINENOISE
53 linenoiseHistoryAdd(line);
54 #endif
57 void Jim_HistorySave(const char *filename)
59 #ifdef USE_LINENOISE
60 mode_t mask;
61 /* Just u=rw, but note that this is only effective for newly created files */
62 mask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
63 linenoiseHistorySave(filename);
64 mask = umask(mask);
65 #endif
68 void Jim_HistoryShow(void)
70 #ifdef USE_LINENOISE
71 /* built-in history command */
72 int i;
73 int len;
74 char **history = linenoiseHistory(&len);
75 for (i = 0; i < len; i++) {
76 printf("%4d %s\n", i + 1, history[i]);
78 #endif
81 int Jim_InteractivePrompt(Jim_Interp *interp)
83 int retcode = JIM_OK;
84 char *history_file = NULL;
85 #ifdef USE_LINENOISE
86 const char *home;
88 home = getenv("HOME");
89 if (home && isatty(STDIN_FILENO)) {
90 int history_len = strlen(home) + sizeof("/.jim_history");
91 history_file = Jim_Alloc(history_len);
92 snprintf(history_file, history_len, "%s/.jim_history", home);
93 Jim_HistoryLoad(history_file);
95 #endif
97 printf("Welcome to Jim version %d.%d\n",
98 JIM_VERSION / 100, JIM_VERSION % 100);
99 Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, "1");
101 while (1) {
102 Jim_Obj *scriptObjPtr;
103 const char *result;
104 int reslen;
105 char prompt[20];
107 if (retcode != JIM_OK) {
108 const char *retcodestr = Jim_ReturnCode(retcode);
110 if (*retcodestr == '?') {
111 snprintf(prompt, sizeof(prompt) - 3, "[%d] . ", retcode);
113 else {
114 snprintf(prompt, sizeof(prompt) - 3, "[%s] . ", retcodestr);
117 else {
118 strcpy(prompt, ". ");
121 scriptObjPtr = Jim_NewStringObj(interp, "", 0);
122 Jim_IncrRefCount(scriptObjPtr);
123 while (1) {
124 char state;
125 char *line;
127 line = Jim_HistoryGetline(prompt);
128 if (line == NULL) {
129 if (errno == EINTR) {
130 continue;
132 Jim_DecrRefCount(interp, scriptObjPtr);
133 retcode = JIM_OK;
134 goto out;
136 if (Jim_Length(scriptObjPtr) != 0) {
137 /* Line continuation */
138 Jim_AppendString(interp, scriptObjPtr, "\n", 1);
140 Jim_AppendString(interp, scriptObjPtr, line, -1);
141 free(line);
142 if (Jim_ScriptIsComplete(interp, scriptObjPtr, &state))
143 break;
145 snprintf(prompt, sizeof(prompt), "%c> ", state);
147 #ifdef USE_LINENOISE
148 if (strcmp(Jim_String(scriptObjPtr), "h") == 0) {
149 /* built-in history command */
150 Jim_HistoryShow();
151 Jim_DecrRefCount(interp, scriptObjPtr);
152 continue;
155 Jim_HistoryAdd(Jim_String(scriptObjPtr));
156 if (history_file) {
157 Jim_HistorySave(history_file);
159 #endif
160 retcode = Jim_EvalObj(interp, scriptObjPtr);
161 Jim_DecrRefCount(interp, scriptObjPtr);
163 if (retcode == JIM_EXIT) {
164 break;
166 if (retcode == JIM_ERR) {
167 Jim_MakeErrorMessage(interp);
169 result = Jim_GetString(Jim_GetResult(interp), &reslen);
170 if (reslen) {
171 printf("%s\n", result);
174 out:
175 Jim_Free(history_file);
176 return retcode;