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
]);
82 struct JimCompletionInfo
{
87 void JimCompletionCallback(const char *prefix
, linenoiseCompletions
*comp
, void *userdata
)
89 struct JimCompletionInfo
*info
= (struct JimCompletionInfo
*)userdata
;
92 objv
[0] = info
->command
;
93 objv
[1] = Jim_NewStringObj(info
->interp
, prefix
, -1);
95 int ret
= Jim_EvalObjVector(info
->interp
, 2, objv
);
97 /* XXX: Consider how best to handle errors here. bgerror? */
100 Jim_Obj
*listObj
= Jim_GetResult(info
->interp
);
101 int len
= Jim_ListLength(info
->interp
, listObj
);
102 for (i
= 0; i
< len
; i
++) {
103 linenoiseAddCompletion(comp
, Jim_String(Jim_ListGetIndex(info
->interp
, listObj
, i
)));
109 int Jim_InteractivePrompt(Jim_Interp
*interp
)
111 int retcode
= JIM_OK
;
112 char *history_file
= NULL
;
115 struct JimCompletionInfo compinfo
;
117 home
= getenv("HOME");
118 if (home
&& isatty(STDIN_FILENO
)) {
119 int history_len
= strlen(home
) + sizeof("/.jim_history");
120 history_file
= Jim_Alloc(history_len
);
121 snprintf(history_file
, history_len
, "%s/.jim_history", home
);
122 Jim_HistoryLoad(history_file
);
125 compinfo
.interp
= interp
;
126 compinfo
.command
= Jim_NewStringObj(interp
, "tcl::autocomplete", -1);
127 Jim_IncrRefCount(compinfo
.command
);
129 /* Register a callback function for tab-completion. */
130 linenoiseSetCompletionCallback(JimCompletionCallback
, &compinfo
);
133 printf("Welcome to Jim version %d.%d\n",
134 JIM_VERSION
/ 100, JIM_VERSION
% 100);
135 Jim_SetVariableStrWithStr(interp
, JIM_INTERACTIVE
, "1");
138 Jim_Obj
*scriptObjPtr
;
143 if (retcode
!= JIM_OK
) {
144 const char *retcodestr
= Jim_ReturnCode(retcode
);
146 if (*retcodestr
== '?') {
147 snprintf(prompt
, sizeof(prompt
) - 3, "[%d] . ", retcode
);
150 snprintf(prompt
, sizeof(prompt
) - 3, "[%s] . ", retcodestr
);
154 strcpy(prompt
, ". ");
157 scriptObjPtr
= Jim_NewStringObj(interp
, "", 0);
158 Jim_IncrRefCount(scriptObjPtr
);
163 line
= Jim_HistoryGetline(prompt
);
165 if (errno
== EINTR
) {
168 Jim_DecrRefCount(interp
, scriptObjPtr
);
172 if (Jim_Length(scriptObjPtr
) != 0) {
173 /* Line continuation */
174 Jim_AppendString(interp
, scriptObjPtr
, "\n", 1);
176 Jim_AppendString(interp
, scriptObjPtr
, line
, -1);
178 if (Jim_ScriptIsComplete(interp
, scriptObjPtr
, &state
))
181 snprintf(prompt
, sizeof(prompt
), "%c> ", state
);
184 if (strcmp(Jim_String(scriptObjPtr
), "h") == 0) {
185 /* built-in history command */
187 Jim_DecrRefCount(interp
, scriptObjPtr
);
191 Jim_HistoryAdd(Jim_String(scriptObjPtr
));
193 Jim_HistorySave(history_file
);
196 retcode
= Jim_EvalObj(interp
, scriptObjPtr
);
197 Jim_DecrRefCount(interp
, scriptObjPtr
);
199 if (retcode
== JIM_EXIT
) {
202 if (retcode
== JIM_ERR
) {
203 Jim_MakeErrorMessage(interp
);
205 result
= Jim_GetString(Jim_GetResult(interp
), &reslen
);
207 printf("%s\n", result
);
211 Jim_Free(history_file
);
214 Jim_DecrRefCount(interp
, compinfo
.command
);
215 linenoiseSetCompletionCallback(NULL
, NULL
);