4 #include "jimautoconf.h"
11 #define MAX_LINE_LEN 512
15 * Returns an allocated line, or NULL if EOF.
17 char *Jim_HistoryGetline(const char *prompt
)
20 return linenoise(prompt
);
23 char *line
= malloc(MAX_LINE_LEN
);
25 fputs(prompt
, stdout
);
28 if (fgets(line
, MAX_LINE_LEN
, stdin
) == NULL
) {
33 if (len
&& line
[len
- 1] == '\n') {
40 void Jim_HistoryLoad(const char *filename
)
43 linenoiseHistoryLoad(filename
);
47 void Jim_HistoryAdd(const char *line
)
50 linenoiseHistoryAdd(line
);
54 void Jim_HistorySave(const char *filename
)
57 linenoiseHistorySave(filename
);
61 void Jim_HistoryShow(void)
64 /* built-in history command */
67 char **history
= linenoiseHistory(&len
);
68 for (i
= 0; i
< len
; i
++) {
69 printf("%4d %s\n", i
+ 1, history
[i
]);
74 int Jim_InteractivePrompt(Jim_Interp
*interp
)
77 char *history_file
= NULL
;
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
);
90 printf("Welcome to Jim version %d.%d" JIM_NL
,
91 JIM_VERSION
/ 100, JIM_VERSION
% 100);
92 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, "1");
95 Jim_Obj
*scriptObjPtr
;
102 const char *retcodestr
= Jim_ReturnCode(retcode
);
104 if (*retcodestr
== '?') {
105 snprintf(prompt
, sizeof(prompt
) - 3, "[%d] ", retcode
);
108 snprintf(prompt
, sizeof(prompt
) - 3, "[%s] ", retcodestr
);
114 strcat(prompt
, ". ");
116 scriptObjPtr
= Jim_NewStringObj(interp
, "", 0);
117 Jim_IncrRefCount(scriptObjPtr
);
123 line
= Jim_HistoryGetline(prompt
);
125 if (errno
== EINTR
) {
128 Jim_DecrRefCount(interp
, scriptObjPtr
);
132 if (Jim_Length(scriptObjPtr
) != 0) {
133 Jim_AppendString(interp
, scriptObjPtr
, "\n", 1);
135 Jim_AppendString(interp
, scriptObjPtr
, line
, -1);
137 str
= Jim_GetString(scriptObjPtr
, &len
);
141 if (Jim_ScriptIsComplete(str
, len
, &state
))
144 snprintf(prompt
, sizeof(prompt
), "%c> ", state
);
147 if (strcmp(str
, "h") == 0) {
148 /* built-in history command */
150 Jim_DecrRefCount(interp
, scriptObjPtr
);
154 Jim_HistoryAdd(Jim_String(scriptObjPtr
));
156 Jim_HistorySave(history_file
);
159 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
160 Jim_DecrRefCount(interp
, scriptObjPtr
);
162 if (retcode
== JIM_EXIT
) {
166 if (retcode
== JIM_ERR
) {
167 Jim_MakeErrorMessage(interp
);
169 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
171 printf("%s\n", result
);
175 Jim_Free(history_file
);