From 7fc1eccefc3093a5858dbca88842ecd5b79d1beb Mon Sep 17 00:00:00 2001 From: mdodd Date: Fri, 19 Oct 2001 00:23:07 +0000 Subject: Add libedit support to tftp. --- usr.bin/tftp/main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) (limited to 'usr.bin/tftp/main.c') diff --git a/usr.bin/tftp/main.c b/usr.bin/tftp/main.c index 4ae5dc4..4d7f5f2 100644 --- a/usr.bin/tftp/main.c +++ b/usr.bin/tftp/main.c @@ -70,10 +70,14 @@ static const char rcsid[] = #include #include +#include + #include "extern.h" #define TIMEOUT 5 /* secs between rexmt's */ +#define MAXLINE 200 + struct sockaddr_in peeraddr; int f; short port; @@ -81,10 +85,9 @@ int trace; int verbose; int connected; char mode[32]; -char line[200]; +char line[MAXLINE]; int margc; char *margv[20]; -char *prompt = "tftp"; jmp_buf toplevel; void intr(); struct servent *sp; @@ -589,6 +592,11 @@ tail(filename) return (filename); } +static const char * +command_prompt() { + return ("tftp> "); +} + /* * Command parser. */ @@ -597,14 +605,42 @@ command() { register struct cmd *c; char *cp; + static EditLine *el = NULL; + static History *hist = NULL; + HistEvent he; + const char * bp; + int len, num; + int verbose; + + verbose = isatty(0); + + if (verbose) { + el = el_init("tftp", stdin, stdout, stderr); + hist = history_init(); + history(hist, &he, H_EVENT, 100); + el_set(el, EL_HIST, history, hist); + el_set(el, EL_EDITOR, "emacs"); + el_set(el, EL_PROMPT, command_prompt); + el_set(el, EL_SIGNAL, 1); + el_source(el, NULL); + } for (;;) { - printf("%s> ", prompt); - if (fgets(line, sizeof line , stdin) == 0) { - if (feof(stdin)) { - exit(0); - } else { - continue; + if (verbose) { + if ((bp = el_gets(el, &num)) == NULL || num == 0) + exit(0); + + len = (num > MAXLINE) ? MAXLINE : num; + memcpy(line, bp, len); + line[len] = '\0'; + history(hist, &he, H_ENTER, bp); + } else { + if (fgets(line, sizeof line , stdin) == 0) { + if (feof(stdin)) { + exit(0); + } else { + continue; + } } } if ((cp = strchr(line, '\n'))) -- cgit v1.1