4 #include "jimautoconf.h"
10 #define MAX_LINE_LEN 512
12 static char *linenoise(const char *prompt
)
14 char *line
= malloc(MAX_LINE_LEN
);
16 fputs(prompt
, stdout
);
19 if (fgets(line
, MAX_LINE_LEN
, stdin
) == NULL
) {
27 int Jim_InteractivePrompt(Jim_Interp
*interp
)
30 char *history_file
= NULL
;
34 home
= getenv("HOME");
36 int history_len
= strlen(home
) + sizeof("/.jim_history");
37 history_file
= Jim_Alloc(history_len
);
38 snprintf(history_file
, history_len
, "%s/.jim_history", home
);
39 linenoiseHistoryLoad(history_file
);
43 printf("Welcome to Jim version %d.%d" JIM_NL
,
44 JIM_VERSION
/ 100, JIM_VERSION
% 100);
45 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, "1");
48 Jim_Obj
*scriptObjPtr
;
55 const char *retcodestr
= Jim_ReturnCode(retcode
);
57 if (*retcodestr
== '?') {
58 snprintf(prompt
, sizeof(prompt
) - 3, "[%d] ", retcode
);
61 snprintf(prompt
, sizeof(prompt
) - 3, "[%s] ", retcodestr
);
69 scriptObjPtr
= Jim_NewStringObj(interp
, "", 0);
70 Jim_IncrRefCount(scriptObjPtr
);
76 line
= linenoise(prompt
);
81 Jim_DecrRefCount(interp
, scriptObjPtr
);
84 if (Jim_Length(scriptObjPtr
) != 0) {
85 Jim_AppendString(interp
, scriptObjPtr
, "\n", 1);
87 Jim_AppendString(interp
, scriptObjPtr
, line
, -1);
89 str
= Jim_GetString(scriptObjPtr
, &len
);
93 if (Jim_ScriptIsComplete(str
, len
, &state
))
96 snprintf(prompt
, sizeof(prompt
), "%c> ", state
);
99 if (strcmp(str
, "h") == 0) {
100 /* built-in history command */
103 char **history
= linenoiseHistory(&len
);
104 for (i
= 0; i
< len
; i
++) {
105 printf("%4d %s\n", i
+ 1, history
[i
]);
107 Jim_DecrRefCount(interp
, scriptObjPtr
);
111 linenoiseHistoryAdd(Jim_String(scriptObjPtr
));
112 linenoiseHistorySave(history_file
);
114 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
115 Jim_DecrRefCount(interp
, scriptObjPtr
);
119 if (retcode
== JIM_EXIT
) {
120 Jim_Free(history_file
);
123 if (retcode
== JIM_ERR
) {
124 Jim_MakeErrorMessage(interp
);
126 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
128 printf("%s\n", result
);
132 Jim_Free(history_file
);