Windows: Exponent modification is only required by MinGW
[jimtcl.git] / jim-interactive.c
blobc80e45148955f706122b5eb48e608d34752cad97
1 #include <errno.h>
2 #include <string.h>
4 #include "jimautoconf.h"
5 #include <jim.h>
7 #ifdef USE_LINENOISE
8 #include <unistd.h>
9 #include "linenoise.h"
10 #else
11 #define MAX_LINE_LEN 512
12 #endif
14 /**
15 * Returns an allocated line, or NULL if EOF.
17 char *Jim_HistoryGetline(const char *prompt)
19 #ifdef USE_LINENOISE
20 return linenoise(prompt);
21 #else
22 int len;
23 char *line = malloc(MAX_LINE_LEN);
25 fputs(prompt, stdout);
26 fflush(stdout);
28 if (fgets(line, MAX_LINE_LEN, stdin) == NULL) {
29 free(line);
30 return NULL;
32 len = strlen(line);
33 if (len && line[len - 1] == '\n') {
34 line[len - 1] = '\0';
36 return line;
37 #endif
40 void Jim_HistoryLoad(const char *filename)
42 #ifdef USE_LINENOISE
43 linenoiseHistoryLoad(filename);
44 #endif
47 void Jim_HistoryAdd(const char *line)
49 #ifdef USE_LINENOISE
50 linenoiseHistoryAdd(line);
51 #endif
54 void Jim_HistorySave(const char *filename)
56 #ifdef USE_LINENOISE
57 linenoiseHistorySave(filename);
58 #endif
61 void Jim_HistoryShow(void)
63 #ifdef USE_LINENOISE
64 /* built-in history command */
65 int i;
66 int len;
67 char **history = linenoiseHistory(&len);
68 for (i = 0; i < len; i++) {
69 printf("%4d %s\n", i + 1, history[i]);
71 #endif
74 int Jim_InteractivePrompt(Jim_Interp *interp)
76 int retcode = JIM_OK;
77 char *history_file = NULL;
78 #ifdef USE_LINENOISE
79 const char *home;
81 home = getenv("HOME");
82 if (home && isatty(STDIN_FILENO)) {
83 int history_len = strlen(home) + sizeof("/.jim_history");
84 history_file = Jim_Alloc(history_len);
85 snprintf(history_file, history_len, "%s/.jim_history", home);
86 Jim_HistoryLoad(history_file);
88 #endif
90 printf("Welcome to Jim version %d.%d\n",
91 JIM_VERSION / 100, JIM_VERSION % 100);
92 Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, "1");
94 while (1) {
95 Jim_Obj *scriptObjPtr;
96 const char *result;
97 int reslen;
98 char prompt[20];
100 if (retcode != JIM_OK) {
101 const char *retcodestr = Jim_ReturnCode(retcode);
103 if (*retcodestr == '?') {
104 snprintf(prompt, sizeof(prompt) - 3, "[%d] . ", retcode);
106 else {
107 snprintf(prompt, sizeof(prompt) - 3, "[%s] . ", retcodestr);
110 else {
111 strcpy(prompt, ". ");
114 scriptObjPtr = Jim_NewStringObj(interp, "", 0);
115 Jim_IncrRefCount(scriptObjPtr);
116 while (1) {
117 char state;
118 char *line;
120 line = Jim_HistoryGetline(prompt);
121 if (line == NULL) {
122 if (errno == EINTR) {
123 continue;
125 Jim_DecrRefCount(interp, scriptObjPtr);
126 retcode = JIM_OK;
127 goto out;
129 if (Jim_Length(scriptObjPtr) != 0) {
130 /* Line continuation */
131 Jim_AppendString(interp, scriptObjPtr, "\n", 1);
133 Jim_AppendString(interp, scriptObjPtr, line, -1);
134 free(line);
135 if (Jim_ScriptIsComplete(interp, scriptObjPtr, &state))
136 break;
138 snprintf(prompt, sizeof(prompt), "%c> ", state);
140 #ifdef USE_LINENOISE
141 if (strcmp(Jim_String(scriptObjPtr), "h") == 0) {
142 /* built-in history command */
143 Jim_HistoryShow();
144 Jim_DecrRefCount(interp, scriptObjPtr);
145 continue;
148 Jim_HistoryAdd(Jim_String(scriptObjPtr));
149 if (history_file) {
150 Jim_HistorySave(history_file);
152 #endif
153 retcode = Jim_EvalObj(interp, scriptObjPtr);
154 Jim_DecrRefCount(interp, scriptObjPtr);
156 if (retcode == JIM_EXIT) {
157 break;
159 if (retcode == JIM_ERR) {
160 Jim_MakeErrorMessage(interp);
162 result = Jim_GetString(Jim_GetResult(interp), &reslen);
163 if (reslen) {
164 printf("%s\n", result);
167 out:
168 Jim_Free(history_file);
169 return retcode;