Skip to content

Commit 2c05f72

Browse files
committed
use hook functions for shell input
1 parent 288fdd8 commit 2c05f72

File tree

12 files changed

+156
-217
lines changed

12 files changed

+156
-217
lines changed

Makefile.in

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ VPATH = $(srcdir)
5656
HFILES = config.h es.h gc.h input.h prim.h print.h sigmsgs.h \
5757
stdenv.h syntax.h term.h var.h
5858
CFILES = access.c closure.c conv.c dict.c eval.c except.c fd.c gc.c glob.c \
59-
glom.c input.c heredoc.c history.c list.c main.c match.c open.c opt.c \
59+
glom.c input.c heredoc.c list.c main.c match.c open.c opt.c \
6060
prim-ctl.c prim-etc.c prim-io.c readline.c prim-sys.c prim.c \
6161
print.c proc.c sigmsgs.c signal.c split.c status.c str.c syntax.c \
6262
term.c token.c tree.c util.c var.c vec.c version.c y.tab.c dump.c
6363
OFILES = access.o closure.o conv.o dict.o eval.o except.o fd.o gc.o glob.o \
64-
glom.o input.o heredoc.o history.o list.o main.o match.o open.o opt.o \
64+
glom.o input.o heredoc.o list.o main.o match.o open.o opt.o \
6565
prim-ctl.o prim-etc.o prim-io.o readline.o prim-sys.o prim.o \
6666
print.o proc.o sigmsgs.o signal.o split.o status.o str.o syntax.o \
6767
term.o token.o tree.o util.o var.o vec.o version.o y.tab.o
@@ -135,7 +135,6 @@ glob.o : glob.c es.h config.h stdenv.h gc.h
135135
glom.o : glom.c es.h config.h stdenv.h gc.h
136136
input.o : input.c es.h config.h stdenv.h input.h
137137
heredoc.o : heredoc.c es.h config.h stdenv.h gc.h input.h syntax.h
138-
history.o : history.c es.h config.h stdenv.h gc.h input.h
139138
list.o : list.c es.h config.h stdenv.h gc.h
140139
main.o : main.c es.h config.h stdenv.h
141140
match.o : match.c es.h config.h stdenv.h

es.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ extern Boolean streq2(const char *s, const char *t1, const char *t2);
291291
/* input.c */
292292

293293
extern char *prompt, *prompt2;
294-
extern Tree *parse(char *esprompt1, char *esprompt2);
294+
extern Tree *parse(List *);
295295
extern Tree *parsestring(const char *str);
296296
extern Boolean isinteractive(void);
297297
extern Boolean isfromfd(void);
@@ -317,17 +317,10 @@ extern void inithistory(void);
317317
extern void sethistory(char *file);
318318
extern void loghistory(char *cmd);
319319
extern void setmaxhistorylength(int length);
320-
extern void rlsetup(Boolean fromprim);
320+
extern void rlsetup(void);
321321
#endif
322322

323323

324-
/* history.c */
325-
326-
extern void newhistbuffer(void);
327-
extern void addhistbuffer(char c);
328-
extern char *dumphistbuffer(void);
329-
330-
331324
/* opt.c */
332325

333326
extern void esoptbegin(List *list, const char *caller, const char *usage, Boolean throws);

heredoc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern Tree *snarfheredoc(const char *eof, Boolean quoted) {
4747

4848
for (tree = NULL, tailp = &tree, buf = openbuffer(0);;) {
4949
int c;
50-
print_prompt2();
50+
increment_line();
5151
for (s = (unsigned char *) eof; (c = GETC()) == *s; s++)
5252
;
5353
if (*s == '\0' && (c == '\n' || c == EOF)) {

history.c

Lines changed: 0 additions & 49 deletions
This file was deleted.

initial.es

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,29 @@ if {~ <=$&primitives writehistory} {
648648
# The parsed code is executed only if it is non-empty, because otherwise
649649
# result gets set to zero when it should not be.
650650

651-
fn-%parse = $&parse
652651
fn-%batch-loop = $&batchloop
653652
fn-%is-interactive = $&isinteractive
654653

654+
fn %parse {
655+
if %is-interactive {
656+
let (in = (); p = $*(1))
657+
let (code = <={$&parse {
658+
let (r = <={$&readline $p}) {
659+
in = $in $r
660+
p = $*(2)
661+
result $r
662+
}
663+
}}) {
664+
if {!~ $#fn-%write-history 0} {
665+
%write-history <={%flatten \n $in}
666+
}
667+
result $code
668+
}
669+
} {
670+
$&parse $&read
671+
}
672+
}
673+
655674
fn %interactive-loop {
656675
let (result = <=true) {
657676
catch @ e type msg {

input.c

Lines changed: 81 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
*/
2323

2424
Input *input;
25-
char *prompt, *prompt2;
26-
2725
Boolean ignoreeof = FALSE;
2826

2927

@@ -103,11 +101,8 @@ extern void unget(Input *in, int c) {
103101
/* get -- get a character, filter out nulls */
104102
static int get(Input *in) {
105103
int c;
106-
Boolean uf = (in->fill == ungetfill);
107104
while ((c = (in->buf < in->bufend ? *in->buf++ : (*in->fill)(in))) == '\0')
108105
warn("null character ignored");
109-
if (!uf && c != EOF)
110-
addhistbuffer((char)c);
111106
return c;
112107
}
113108

@@ -131,58 +126,9 @@ static int eoffill(Input UNUSED *in) {
131126
return EOF;
132127
}
133128

134-
#if HAVE_READLINE
135-
/* callreadline -- readline wrapper */
136-
static char *callreadline(char *prompt0) {
137-
char *volatile prompt = prompt0;
138-
char *r;
139-
if (prompt == NULL)
140-
prompt = ""; /* bug fix for readline 2.0 */
141-
rlsetup(FALSE);
142-
interrupted = FALSE;
143-
if (!setjmp(slowlabel)) {
144-
slow = TRUE;
145-
r = interrupted ? NULL : readline(prompt);
146-
if (interrupted)
147-
errno = EINTR;
148-
} else {
149-
r = NULL;
150-
errno = EINTR;
151-
}
152-
slow = FALSE;
153-
SIGCHK();
154-
return r;
155-
}
156-
#endif
157-
158129
/* fdfill -- fill input buffer by reading from a file descriptor */
159130
static int fdfill(Input *in) {
160131
long nread;
161-
assert(in->buf == in->bufend);
162-
assert(in->fd >= 0);
163-
164-
#if HAVE_READLINE
165-
if (in->runflags & run_interactive && in->fd == 0) {
166-
char *rlinebuf = NULL;
167-
do {
168-
rlinebuf = callreadline(prompt);
169-
} while (rlinebuf == NULL && errno == EINTR);
170-
171-
if (rlinebuf == NULL)
172-
nread = 0;
173-
else {
174-
nread = strlen(rlinebuf) + 1;
175-
if (in->buflen < (unsigned int)nread) {
176-
while (in->buflen < (unsigned int)nread)
177-
in->buflen *= 2;
178-
in->bufbegin = erealloc(in->bufbegin, in->buflen);
179-
}
180-
memcpy(in->bufbegin, rlinebuf, nread - 1);
181-
in->bufbegin[nread - 1] = '\n';
182-
efree(rlinebuf);
183-
}
184-
} else
185-
#endif
186132
do {
187133
nread = eread(in->fd, (char *) in->bufbegin, in->buflen);
188134
SIGCHK();
@@ -205,13 +151,69 @@ static int fdfill(Input *in) {
205151
return *in->buf++;
206152
}
207153

154+
static List *fillcmd = NULL;
155+
156+
static int cmdfill(Input *in) {
157+
char *read;
158+
List *result;
159+
size_t nread;
160+
int oldf;
161+
162+
assert(in->buf == in->bufend);
163+
assert(in->fd >= 0);
164+
165+
if (fillcmd == NULL)
166+
return fdfill(in);
167+
168+
oldf = dup(0);
169+
if (dup2(in->fd, 0) == -1)
170+
fail("$&parse", "dup2: %s", esstrerror(errno));
171+
172+
ExceptionHandler
173+
174+
result = eval(fillcmd, NULL, 0);
175+
176+
CatchException (e)
177+
178+
mvfd(oldf, 0);
179+
throw(e);
180+
181+
EndExceptionHandler
182+
183+
mvfd(oldf, 0);
184+
185+
if (result == NULL) { /* eof */
186+
if (!ignoreeof) {
187+
close(in->fd);
188+
in->fd = -1;
189+
in->fill = eoffill;
190+
in->runflags &= ~run_interactive;
191+
}
192+
return EOF;
193+
}
194+
read = str("%L\n", result, " ");
195+
196+
if ((nread = strlen(read)) > in->buflen) {
197+
in->bufbegin = erealloc(in->bufbegin, nread);
198+
in->buflen = nread;
199+
}
200+
memcpy(in->bufbegin, read, nread);
201+
202+
in->buf = in->bufbegin;
203+
in->bufend = &in->buf[nread];
204+
205+
return *in->buf++;
206+
}
208207

209208
/*
210209
* the input loop
211210
*/
212211

213-
/* parse -- call yyparse(), but disable garbage collection and catch errors */
214-
extern Tree *parse(char *pr1, char *pr2) {
212+
213+
static Boolean parsing = FALSE;
214+
215+
/* parse -- call yyparse() and catch errors */
216+
extern Tree *parse(List *fc) {
215217
int result;
216218
assert(error == NULL);
217219

@@ -221,17 +223,26 @@ extern Tree *parse(char *pr1, char *pr2) {
221223
if (ISEOF(input))
222224
throw(mklist(mkstr("eof"), NULL));
223225

224-
#if HAVE_READLINE
225-
prompt = (pr1 == NULL) ? "" : pr1;
226-
#else
227-
if (pr1 != NULL)
228-
eprint("%s", pr1);
229-
#endif
230-
prompt2 = pr2;
226+
if (parsing)
227+
fail("$&parse", "cannot perform nested parsing");
228+
229+
fillcmd = fc;
230+
parsing = TRUE;
231+
232+
ExceptionHandler
233+
234+
result = yyparse();
235+
236+
CatchException (e)
237+
238+
parsing = FALSE;
239+
fillcmd = NULL;
240+
throw(e);
241+
242+
EndExceptionHandler
231243

232-
gcdisable();
233-
result = yyparse();
234-
gcenable();
244+
parsing = FALSE;
245+
fillcmd = NULL;
235246

236247
if (result || error != NULL) {
237248
char *e;
@@ -328,7 +339,7 @@ extern List *runfd(int fd, const char *name, int flags) {
328339

329340
memzero(&in, sizeof (Input));
330341
in.lineno = 1;
331-
in.fill = fdfill;
342+
in.fill = cmdfill;
332343
in.cleanup = fdcleanup;
333344
in.fd = fd;
334345
registerfd(&in.fd, TRUE);
@@ -391,7 +402,7 @@ extern Tree *parseinput(Input *in) {
391402
input = in;
392403

393404
ExceptionHandler
394-
result = parse(NULL, NULL);
405+
result = parse(NULL);
395406
if (get(in) != EOF)
396407
fail("$&parse", "more than one value in term");
397408
CatchException (e)
@@ -439,7 +450,7 @@ extern Boolean isinteractive(void) {
439450
}
440451

441452
extern Boolean isfromfd(void) {
442-
return input == NULL ? FALSE : (input->fill == fdfill);
453+
return input == NULL ? FALSE : (input->fill == fdfill || input->fill == cmdfill);
443454
}
444455

445456

@@ -452,7 +463,6 @@ extern void initinput(void) {
452463
input = NULL;
453464

454465
/* declare the global roots */
466+
globalroot(&fillcmd);
455467
globalroot(&error); /* parse errors */
456-
globalroot(&prompt); /* main prompt */
457-
globalroot(&prompt2); /* secondary prompt */
458468
}

input.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extern void yyerror(char *s);
3636
extern const char dnw[];
3737
extern int yylex(void);
3838
extern void inityy(void);
39-
extern void print_prompt2(void);
39+
extern void increment_line(void);
4040

4141

4242
/* parse.y */

0 commit comments

Comments
 (0)