Fix the handling of continuation prompts when using readline

This commit is contained in:
hpa 2001-11-13 02:42:54 +00:00
parent 713e1fe868
commit 13849b4a32

View file

@ -220,15 +220,47 @@ main(int argc, char *argv[])
char *hostname; char *hostname;
/* Called when a command is incomplete; modifies
the global variable "line" */
static void
getmoreargs(const char *partial, const char *mprompt)
{
#ifdef WITH_READLINE
char *eline;
int len, elen;
len = strlen(partial);
eline = readline(mprompt);
if (!eline)
exit(0); /* EOF */
elen = strlen(eline);
if (line)
free(line);
line = xmalloc(len+elen+1);
strcpy(line, partial);
strcpy(line+len, eline);
add_history(line);
#else
int len = strlen(partial);
strcpy(line, partial);
fputs(mprompt, stdout);
if ( fgets(line+len, LBUFLEN-len, stdin) == 0 )
if ( feof(stdin) )
exit(0); /* EOF */
#endif
}
void void
setpeer(int argc, char *argv[]) setpeer(int argc, char *argv[])
{ {
struct hostent *host; struct hostent *host;
if (argc < 2) { if (argc < 2) {
strcpy(line, "Connect "); getmoreargs("connect ", "(to) ");
printf("(to) ");
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
makeargv(); makeargv();
argc = margc; argc = margc;
argv = margv; argv = margv;
@ -344,9 +376,7 @@ put(int argc, char *argv[])
char *cp, *targ; char *cp, *targ;
if (argc < 2) { if (argc < 2) {
strcpy(line, "send "); getmoreargs("send ", "(file) ");
printf("(file) ");
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
makeargv(); makeargv();
argc = margc; argc = margc;
argv = margv; argv = margv;
@ -434,9 +464,7 @@ get(int argc, char *argv[])
char *src; char *src;
if (argc < 2) { if (argc < 2) {
strcpy(line, "get "); getmoreargs("get ", "(files) ");
printf("(files) ");
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
makeargv(); makeargv();
argc = margc; argc = margc;
argv = margv; argv = margv;
@ -515,9 +543,7 @@ setrexmt(int argc, char *argv[])
int t; int t;
if (argc < 2) { if (argc < 2) {
strcpy(line, "Rexmt-timeout "); getmoreargs("rexmt-timeout ", "(value) ");
printf("(value) ");
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
makeargv(); makeargv();
argc = margc; argc = margc;
argv = margv; argv = margv;
@ -541,9 +567,7 @@ settimeout(int argc, char *argv[])
int t; int t;
if (argc < 2) { if (argc < 2) {
strcpy(line, "Maximum-timeout "); getmoreargs("maximum-timeout ", "(value) ");
printf("(value) ");
fgets(&line[strlen(line)], LBUFLEN-strlen(line), stdin);
makeargv(); makeargv();
argc = margc; argc = margc;
argv = margv; argv = margv;
@ -610,10 +634,10 @@ command(void)
for (;;) { for (;;) {
#ifdef WITH_READLINE #ifdef WITH_READLINE
if ( line ) if ( line )
free(line); free(line);
line = readline(prompt); line = readline(prompt);
if ( !line ) if ( !line )
exit(0); /* EOF */ exit(0); /* EOF */
#else #else
fputs(prompt, stdout); fputs(prompt, stdout);
if (fgets(line, LBUFLEN, stdin) == 0) { if (fgets(line, LBUFLEN, stdin) == 0) {