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\n",
91 JIM_VERSION
/ 100, JIM_VERSION
% 100);
92 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, "1");
95 Jim_Obj
*scriptObjPtr
;
100 if (retcode
!= JIM_OK
) {
101 const char *retcodestr
= Jim_ReturnCode(retcode
);
103 if (*retcodestr
== '?') {
104 snprintf(prompt
, sizeof(prompt
) - 3, "[%d] . ", retcode
);
107 snprintf(prompt
, sizeof(prompt
) - 3, "[%s] . ", retcodestr
);
111 strcpy(prompt
, ". ");
114 scriptObjPtr
= Jim_NewStringObj(interp
, "", 0);
115 Jim_IncrRefCount(scriptObjPtr
);
120 line
= Jim_HistoryGetline(prompt
);
122 if (errno
== EINTR
) {
125 Jim_DecrRefCount(interp
, scriptObjPtr
);
129 if (Jim_Length(scriptObjPtr
) != 0) {
130 /* Line continuation */
131 Jim_AppendString(interp
, scriptObjPtr
, "\n", 1);
133 Jim_AppendString(interp
, scriptObjPtr
, line
, -1);
135 if (Jim_ScriptIsComplete(interp
, scriptObjPtr
, &state
))
138 snprintf(prompt
, sizeof(prompt
), "%c> ", state
);
141 if (strcmp(Jim_String(scriptObjPtr
), "h") == 0) {
142 /* built-in history command */
144 Jim_DecrRefCount(interp
, scriptObjPtr
);
148 Jim_HistoryAdd(Jim_String(scriptObjPtr
));
150 Jim_HistorySave(history_file
);
153 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
154 Jim_DecrRefCount(interp
, scriptObjPtr
);
156 if (retcode
== JIM_EXIT
) {
159 if (retcode
== JIM_ERR
) {
160 Jim_MakeErrorMessage(interp
);
162 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
164 printf("%s\n", result
);
168 Jim_Free(history_file
);