4 #include "jimautoconf.h"
11 #define MAX_LINE_LEN 512
13 static char *linenoise(const char *prompt
)
15 char *line
= malloc(MAX_LINE_LEN
);
17 fputs(prompt
, stdout
);
20 if (fgets(line
, MAX_LINE_LEN
, stdin
) == NULL
) {
28 int Jim_InteractivePrompt(Jim_Interp
*interp
)
31 char *history_file
= NULL
;
35 home
= getenv("HOME");
36 if (home
&& isatty(STDIN_FILENO
)) {
37 int history_len
= strlen(home
) + sizeof("/.jim_history");
38 history_file
= Jim_Alloc(history_len
);
39 snprintf(history_file
, history_len
, "%s/.jim_history", home
);
40 linenoiseHistoryLoad(history_file
);
44 printf("Welcome to Jim version %d.%d" JIM_NL
,
45 JIM_VERSION
/ 100, JIM_VERSION
% 100);
46 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, "1");
49 Jim_Obj
*scriptObjPtr
;
56 const char *retcodestr
= Jim_ReturnCode(retcode
);
58 if (*retcodestr
== '?') {
59 snprintf(prompt
, sizeof(prompt
) - 3, "[%d] ", retcode
);
62 snprintf(prompt
, sizeof(prompt
) - 3, "[%s] ", retcodestr
);
70 scriptObjPtr
= Jim_NewStringObj(interp
, "", 0);
71 Jim_IncrRefCount(scriptObjPtr
);
77 line
= linenoise(prompt
);
82 Jim_DecrRefCount(interp
, scriptObjPtr
);
85 if (Jim_Length(scriptObjPtr
) != 0) {
86 Jim_AppendString(interp
, scriptObjPtr
, "\n", 1);
88 Jim_AppendString(interp
, scriptObjPtr
, line
, -1);
90 str
= Jim_GetString(scriptObjPtr
, &len
);
94 if (Jim_ScriptIsComplete(str
, len
, &state
))
97 snprintf(prompt
, sizeof(prompt
), "%c> ", state
);
100 if (strcmp(str
, "h") == 0) {
101 /* built-in history command */
104 char **history
= linenoiseHistory(&len
);
105 for (i
= 0; i
< len
; i
++) {
106 printf("%4d %s\n", i
+ 1, history
[i
]);
108 Jim_DecrRefCount(interp
, scriptObjPtr
);
112 linenoiseHistoryAdd(Jim_String(scriptObjPtr
));
114 linenoiseHistorySave(history_file
);
117 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
118 Jim_DecrRefCount(interp
, scriptObjPtr
);
122 if (retcode
== JIM_EXIT
) {
123 Jim_Free(history_file
);
126 if (retcode
== JIM_ERR
) {
127 Jim_MakeErrorMessage(interp
);
129 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
131 printf("%s\n", result
);
135 Jim_Free(history_file
);