4 #include "jimautoconf.h"
11 #include "linenoise.h"
13 #define MAX_LINE_LEN 512
17 * Returns an allocated line, or NULL if EOF.
19 char *Jim_HistoryGetline(const char *prompt
)
22 return linenoise(prompt
);
25 char *line
= malloc(MAX_LINE_LEN
);
27 fputs(prompt
, stdout
);
30 if (fgets(line
, MAX_LINE_LEN
, stdin
) == NULL
) {
35 if (len
&& line
[len
- 1] == '\n') {
42 void Jim_HistoryLoad(const char *filename
)
45 linenoiseHistoryLoad(filename
);
49 void Jim_HistoryAdd(const char *line
)
52 linenoiseHistoryAdd(line
);
56 void Jim_HistorySave(const char *filename
)
59 linenoiseHistorySave(filename
);
63 void Jim_HistoryShow(void)
66 /* built-in history command */
69 char **history
= linenoiseHistory(&len
);
70 for (i
= 0; i
< len
; i
++) {
71 printf("%4d %s\n", i
+ 1, history
[i
]);
76 int Jim_InteractivePrompt(Jim_Interp
*interp
)
79 char *history_file
= NULL
;
83 home
= getenv("HOME");
84 if (home
&& isatty(STDIN_FILENO
)) {
85 int history_len
= strlen(home
) + sizeof("/.jim_history");
86 history_file
= Jim_Alloc(history_len
);
87 snprintf(history_file
, history_len
, "%s/.jim_history", home
);
88 Jim_HistoryLoad(history_file
);
92 printf("Welcome to Jim version %d.%d\n",
93 JIM_VERSION
/ 100, JIM_VERSION
% 100);
94 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, "1");
97 Jim_Obj
*scriptObjPtr
;
102 if (retcode
!= JIM_OK
) {
103 const char *retcodestr
= Jim_ReturnCode(retcode
);
105 if (*retcodestr
== '?') {
106 snprintf(prompt
, sizeof(prompt
) - 3, "[%d] . ", retcode
);
109 snprintf(prompt
, sizeof(prompt
) - 3, "[%s] . ", retcodestr
);
113 strcpy(prompt
, ". ");
116 scriptObjPtr
= Jim_NewStringObj(interp
, "", 0);
117 Jim_IncrRefCount(scriptObjPtr
);
122 line
= Jim_HistoryGetline(prompt
);
124 if (errno
== EINTR
) {
127 Jim_DecrRefCount(interp
, scriptObjPtr
);
131 if (Jim_Length(scriptObjPtr
) != 0) {
132 /* Line continuation */
133 Jim_AppendString(interp
, scriptObjPtr
, "\n", 1);
135 Jim_AppendString(interp
, scriptObjPtr
, line
, -1);
137 if (Jim_ScriptIsComplete(interp
, scriptObjPtr
, &state
))
140 snprintf(prompt
, sizeof(prompt
), "%c> ", state
);
143 if (strcmp(Jim_String(scriptObjPtr
), "h") == 0) {
144 /* built-in history command */
146 Jim_DecrRefCount(interp
, scriptObjPtr
);
150 Jim_HistoryAdd(Jim_String(scriptObjPtr
));
152 Jim_HistorySave(history_file
);
155 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
156 Jim_DecrRefCount(interp
, scriptObjPtr
);
158 if (retcode
== JIM_EXIT
) {
161 if (retcode
== JIM_ERR
) {
162 Jim_MakeErrorMessage(interp
);
164 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
166 printf("%s\n", result
);
170 Jim_Free(history_file
);