4 #include "jimautoconf.h"
12 #include "linenoise.h"
14 #define MAX_LINE_LEN 512
18 * Returns an allocated line, or NULL if EOF.
20 char *Jim_HistoryGetline(const char *prompt
)
23 return linenoise(prompt
);
26 char *line
= malloc(MAX_LINE_LEN
);
28 fputs(prompt
, stdout
);
31 if (fgets(line
, MAX_LINE_LEN
, stdin
) == NULL
) {
36 if (len
&& line
[len
- 1] == '\n') {
43 void Jim_HistoryLoad(const char *filename
)
46 linenoiseHistoryLoad(filename
);
50 void Jim_HistoryAdd(const char *line
)
53 linenoiseHistoryAdd(line
);
57 void Jim_HistorySave(const char *filename
)
61 /* Just u=rw, but note that this is only effective for newly created files */
62 mask
= umask(S_IXUSR
| S_IRWXG
| S_IRWXO
);
63 linenoiseHistorySave(filename
);
68 void Jim_HistoryShow(void)
71 /* built-in history command */
74 char **history
= linenoiseHistory(&len
);
75 for (i
= 0; i
< len
; i
++) {
76 printf("%4d %s\n", i
+ 1, history
[i
]);
81 int Jim_InteractivePrompt(Jim_Interp
*interp
)
84 char *history_file
= NULL
;
88 home
= getenv("HOME");
89 if (home
&& isatty(STDIN_FILENO
)) {
90 int history_len
= strlen(home
) + sizeof("/.jim_history");
91 history_file
= Jim_Alloc(history_len
);
92 snprintf(history_file
, history_len
, "%s/.jim_history", home
);
93 Jim_HistoryLoad(history_file
);
97 printf("Welcome to Jim version %d.%d\n",
98 JIM_VERSION
/ 100, JIM_VERSION
% 100);
99 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, "1");
102 Jim_Obj
*scriptObjPtr
;
107 if (retcode
!= JIM_OK
) {
108 const char *retcodestr
= Jim_ReturnCode(retcode
);
110 if (*retcodestr
== '?') {
111 snprintf(prompt
, sizeof(prompt
) - 3, "[%d] . ", retcode
);
114 snprintf(prompt
, sizeof(prompt
) - 3, "[%s] . ", retcodestr
);
118 strcpy(prompt
, ". ");
121 scriptObjPtr
= Jim_NewStringObj(interp
, "", 0);
122 Jim_IncrRefCount(scriptObjPtr
);
127 line
= Jim_HistoryGetline(prompt
);
129 if (errno
== EINTR
) {
132 Jim_DecrRefCount(interp
, scriptObjPtr
);
136 if (Jim_Length(scriptObjPtr
) != 0) {
137 /* Line continuation */
138 Jim_AppendString(interp
, scriptObjPtr
, "\n", 1);
140 Jim_AppendString(interp
, scriptObjPtr
, line
, -1);
142 if (Jim_ScriptIsComplete(interp
, scriptObjPtr
, &state
))
145 snprintf(prompt
, sizeof(prompt
), "%c> ", state
);
148 if (strcmp(Jim_String(scriptObjPtr
), "h") == 0) {
149 /* built-in history command */
151 Jim_DecrRefCount(interp
, scriptObjPtr
);
155 Jim_HistoryAdd(Jim_String(scriptObjPtr
));
157 Jim_HistorySave(history_file
);
160 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
161 Jim_DecrRefCount(interp
, scriptObjPtr
);
163 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
);