Skip to content

Commit 1a4923b

Browse files
committed
initial sketch of a $&readline primitive
1 parent dd2fef1 commit 1a4923b

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

readline.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ extern void rlsetup(UNUSED Boolean fromprim) {
192192
initialized = TRUE;
193193
}
194194

195+
/* TODO: from-primitive completion function */
196+
rl_attempted_completion_function = (fromprim ? NULL : builtin_completion);
197+
195198
if (reloadhistory)
196199
reload_history();
197200
if (resetterminal) {
@@ -245,16 +248,41 @@ PRIM(resetterminal) {
245248
return ltrue;
246249
}
247250

248-
/*
251+
static char *callreadline(char *prompt) {
252+
char *r, *volatile line;
253+
/* should this be called after each interruption, or? */
254+
rlsetup(TRUE);
255+
interrupted = FALSE;
256+
if (!setjmp(slowlabel)) {
257+
slow = TRUE;
258+
r = interrupted ? NULL : readline(prompt);
259+
if (interrupted)
260+
errno = EINTR;
261+
} else {
262+
r = NULL;
263+
errno = EINTR;
264+
}
265+
slow = FALSE;
266+
if (r != NULL) {
267+
line = str("%s", r);
268+
efree(r);
269+
}
270+
SIGCHK();
271+
return line;
272+
}
273+
249274
PRIM(readline) {
250275
char *line;
251-
rlsetup(TRUE);
252276
Ref(char *, prompt, (list == NULL ? "" : getstr(list->term)));
253-
line = readline(prompt);
277+
do {
278+
line = callreadline(prompt);
279+
} while (line == NULL && errno == EINTR);
254280
RefEnd(prompt);
255-
return mklist(mkstr(line), NULL);
281+
if (line == NULL)
282+
return NULL;
283+
list = mklist(mkstr(line), NULL);
284+
return list;
256285
}
257-
*/
258286

259287
/*
260288
* initialization
@@ -265,7 +293,7 @@ extern Dict *initprims_readline(Dict *primdict) {
265293
X(writehistory);
266294
X(resetterminal);
267295
X(setmaxhistorylength);
268-
/* X(readline); */
296+
X(readline);
269297
return primdict;
270298
}
271299

0 commit comments

Comments
 (0)