4 #include "jimautoconf.h"
11 #ifdef HAVE_SYS_STAT_H
14 #include "linenoise.h"
16 #define MAX_LINE_LEN 512
20 static void JimCompletionCallback(const char *prefix
, linenoiseCompletions
*comp
, void *userdata
);
21 static const char completion_callback_assoc_key
[] = "interactive-completion";
25 * Returns an allocated line, or NULL if EOF.
27 char *Jim_HistoryGetline(Jim_Interp
*interp
, const char *prompt
)
30 struct JimCompletionInfo
*compinfo
= Jim_GetAssocData(interp
, completion_callback_assoc_key
);
32 /* Set any completion callback just during the call to linenoise()
33 * to allow for per-interp settings
36 linenoiseSetCompletionCallback(JimCompletionCallback
, compinfo
);
38 result
= linenoise(prompt
);
39 /* unset the callback */
40 linenoiseSetCompletionCallback(NULL
, NULL
);
44 char *line
= malloc(MAX_LINE_LEN
);
46 fputs(prompt
, stdout
);
49 if (fgets(line
, MAX_LINE_LEN
, stdin
) == NULL
) {
54 if (len
&& line
[len
- 1] == '\n') {
61 void Jim_HistoryLoad(const char *filename
)
64 linenoiseHistoryLoad(filename
);
68 void Jim_HistoryAdd(const char *line
)
71 linenoiseHistoryAdd(line
);
75 void Jim_HistorySave(const char *filename
)
80 /* Just u=rw, but note that this is only effective for newly created files */
81 mask
= umask(S_IXUSR
| S_IRWXG
| S_IRWXO
);
83 linenoiseHistorySave(filename
);
90 void Jim_HistoryShow(void)
93 /* built-in history command */
96 char **history
= linenoiseHistory(&len
);
97 for (i
= 0; i
< len
; i
++) {
98 printf("%4d %s\n", i
+ 1, history
[i
]);
104 struct JimCompletionInfo
{
109 static void JimCompletionCallback(const char *prefix
, linenoiseCompletions
*comp
, void *userdata
)
111 struct JimCompletionInfo
*info
= (struct JimCompletionInfo
*)userdata
;
115 objv
[0] = info
->command
;
116 objv
[1] = Jim_NewStringObj(info
->interp
, prefix
, -1);
118 ret
= Jim_EvalObjVector(info
->interp
, 2, objv
);
120 /* XXX: Consider how best to handle errors here. bgerror? */
123 Jim_Obj
*listObj
= Jim_GetResult(info
->interp
);
124 int len
= Jim_ListLength(info
->interp
, listObj
);
125 for (i
= 0; i
< len
; i
++) {
126 linenoiseAddCompletion(comp
, Jim_String(Jim_ListGetIndex(info
->interp
, listObj
, i
)));
131 static void JimHistoryFreeCompletion(Jim_Interp
*interp
, void *data
)
133 struct JimCompletionInfo
*compinfo
= data
;
135 Jim_DecrRefCount(interp
, compinfo
->command
);
142 * Sets a completion command to be used with Jim_HistoryGetline()
143 * If commandObj is NULL, deletes any existing completion command.
145 void Jim_HistorySetCompletion(Jim_Interp
*interp
, Jim_Obj
*commandObj
)
149 /* Increment now in case the existing object is the same */
150 Jim_IncrRefCount(commandObj
);
153 Jim_DeleteAssocData(interp
, completion_callback_assoc_key
);
156 struct JimCompletionInfo
*compinfo
= Jim_Alloc(sizeof(*compinfo
));
157 compinfo
->interp
= interp
;
158 compinfo
->command
= commandObj
;
160 Jim_SetAssocData(interp
, completion_callback_assoc_key
, JimHistoryFreeCompletion
, compinfo
);
165 int Jim_InteractivePrompt(Jim_Interp
*interp
)
167 int retcode
= JIM_OK
;
168 char *history_file
= NULL
;
172 home
= getenv("HOME");
173 if (home
&& isatty(STDIN_FILENO
)) {
174 int history_len
= strlen(home
) + sizeof("/.jim_history");
175 history_file
= Jim_Alloc(history_len
);
176 snprintf(history_file
, history_len
, "%s/.jim_history", home
);
177 Jim_HistoryLoad(history_file
);
180 Jim_HistorySetCompletion(interp
, Jim_NewStringObj(interp
, "tcl::autocomplete", -1));
183 printf("Welcome to Jim version %d.%d\n",
184 JIM_VERSION
/ 100, JIM_VERSION
% 100);
185 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, "1");
188 Jim_Obj
*scriptObjPtr
;
193 if (retcode
!= JIM_OK
) {
194 const char *retcodestr
= Jim_ReturnCode(retcode
);
196 if (*retcodestr
== '?') {
197 snprintf(prompt
, sizeof(prompt
) - 3, "[%d] . ", retcode
);
200 snprintf(prompt
, sizeof(prompt
) - 3, "[%s] . ", retcodestr
);
204 strcpy(prompt
, ". ");
207 scriptObjPtr
= Jim_NewStringObj(interp
, "", 0);
208 Jim_IncrRefCount(scriptObjPtr
);
213 line
= Jim_HistoryGetline(interp
, prompt
);
215 if (errno
== EINTR
) {
218 Jim_DecrRefCount(interp
, scriptObjPtr
);
222 if (Jim_Length(scriptObjPtr
) != 0) {
223 /* Line continuation */
224 Jim_AppendString(interp
, scriptObjPtr
, "\n", 1);
226 Jim_AppendString(interp
, scriptObjPtr
, line
, -1);
228 if (Jim_ScriptIsComplete(interp
, scriptObjPtr
, &state
))
231 snprintf(prompt
, sizeof(prompt
), "%c> ", state
);
234 if (strcmp(Jim_String(scriptObjPtr
), "h") == 0) {
235 /* built-in history command */
237 Jim_DecrRefCount(interp
, scriptObjPtr
);
241 Jim_HistoryAdd(Jim_String(scriptObjPtr
));
243 Jim_HistorySave(history_file
);
246 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
247 Jim_DecrRefCount(interp
, scriptObjPtr
);
249 if (retcode
== JIM_EXIT
) {
252 if (retcode
== JIM_ERR
) {
253 Jim_MakeErrorMessage(interp
);
255 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
257 printf("%s\n", result
);
261 Jim_Free(history_file
);