From 7b18487ba9fcc9a04597beef58977044d20a8cf5 Mon Sep 17 00:00:00 2001 From: brian Date: Fri, 7 Nov 1997 20:20:15 +0000 Subject: Style police. bzero -> memset index -> strchr rindex -> strrchr Use libedit (this should make pppctl a lot more attractive than telnet). --- usr.sbin/pppctl/Makefile | 6 ++-- usr.sbin/pppctl/pppctl.8 | 42 ++++++++++++++++------ usr.sbin/pppctl/pppctl.c | 91 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 102 insertions(+), 37 deletions(-) (limited to 'usr.sbin/pppctl') diff --git a/usr.sbin/pppctl/Makefile b/usr.sbin/pppctl/Makefile index fca11dd..7e50241 100644 --- a/usr.sbin/pppctl/Makefile +++ b/usr.sbin/pppctl/Makefile @@ -1,8 +1,10 @@ -# $Id:$ +# $Id: Makefile,v 1.1 1997/06/28 01:04:49 brian Exp $ PROG= pppctl SRCS= pppctl.c -CFLAGS+=-Wall +CFLAGS+=-Wall -Wmissing-prototypes +LDADD+= -ledit -ltermcap +DPADD+= ${LIBEDIT} ${LIBTERMCAP} MAN8= pppctl.8 .include diff --git a/usr.sbin/pppctl/pppctl.8 b/usr.sbin/pppctl/pppctl.8 index 1c896b7..6c778d6 100644 --- a/usr.sbin/pppctl/pppctl.8 +++ b/usr.sbin/pppctl/pppctl.8 @@ -1,4 +1,4 @@ -.\" $Id: pppctl.8,v 1.4 1997/10/05 14:21:30 brian Exp $ +.\" $Id: pppctl.8,v 1.5 1997/11/07 02:54:46 brian Exp $ .Dd 26 June 1997 .Os FreeBSD .Dt PPPCTL 8 @@ -61,13 +61,13 @@ If no arguments are given, .Nm enters interactive mode, where commands are read from standard input. -When in interactive mode, the -.Fl v -option is assumed. Any password negotiation due to the -.Fl p -option is done prior to enabling the -.Fl v -option. +When reading commands, the +.Xr editline 3 +library is used, allowing command-line editing (with +.Xr editrc 5 +defining editing behaviour). The history size +defaults to +.Em 20 lines . The following command line options are available: .Bl -tag -width Ds @@ -76,7 +76,8 @@ Display all data sent to and received from the .Nm ppp daemon. Normally, .Nm pppctl -displays only non-prompt lines received. +displays only non-prompt lines received. This option is ignored in +interactive mode. .It Fl t Ar n Use a timeout of .Ar n @@ -173,9 +174,28 @@ You can even make a generic script: exec pppctl /var/run/internet "$@" .Ed +.Sh ENVIRONMENT VARIABLES +The following environment variables are understood by +.Nm pppctl +when in interactive mode: +.Bl -tag -width XXXXXXXXXX +.It Dv EL_SIZE +The number of history lines. The default is 20. +.It Dv EL_EDITOR +The edit mode. Only values of "emacs" and "vi" are accepted. Other values +are silently ignored. This environment variable will override the +.Ar bind -v +and +.Ar bind -e +commands in +.Pa ~/.editrc . +.El + .Sh SEE ALSO -.Xr services 5 , -.Xr ppp 8 +.Xr editline 3 , +.Xr editrc 5 , +.Xr ppp 8 , +.Xr services 5 .Sh HISTORY The diff --git a/usr.sbin/pppctl/pppctl.c b/usr.sbin/pppctl/pppctl.c index da44a60..809becc 100644 --- a/usr.sbin/pppctl/pppctl.c +++ b/usr.sbin/pppctl/pppctl.c @@ -1,29 +1,37 @@ #include + #include #include #include #include #include + +#include #include -#include #include -#include #include +#include +#include #define LINELEN 2048 static char Buffer[LINELEN], Command[LINELEN]; -static int Usage() +static int +Usage() { - fprintf(stderr, "Usage: pppctl [-v] [ -t n ] [ -p passwd ] Port|LocalSock [command[;command]...]\n"); - fprintf(stderr, " -v tells pppctl to output all conversation\n"); - fprintf(stderr, " -t n specifies a timeout of n seconds (default 2)\n"); + fprintf(stderr, "Usage: pppctl [-v] [ -t n ] [ -p passwd ] " + "Port|LocalSock [command[;command]...]\n"); + fprintf(stderr, " -v tells pppctl to output all" + " conversation\n"); + fprintf(stderr, " -t n specifies a timeout of n" + " seconds (default 2)\n"); fprintf(stderr, " -p passwd specifies your password\n"); return 1; } static int TimedOut = 0; -void Timeout(int Sig) +static void +Timeout(int Sig) { TimedOut = 1; } @@ -33,8 +41,18 @@ void Timeout(int Sig) #define REC_VERBOSE (4) static char *passwd; +static char *prompt; -int Receive(int fd, unsigned TimeoutVal, int display) +static char * +GetPrompt(EditLine *e) +{ + if (prompt == NULL) + prompt = ""; + return prompt; +} + +static int +Receive(int fd, unsigned TimeoutVal, int display) { int Result; struct sigaction act, oact; @@ -50,6 +68,7 @@ int Receive(int fd, unsigned TimeoutVal, int display) alarm(TimeoutVal); } + prompt = Buffer; len = 0; while (Result = read(fd, Buffer+len, sizeof(Buffer)-len-1), Result != -1) { len += Result; @@ -64,10 +83,11 @@ int Receive(int fd, unsigned TimeoutVal, int display) if (display & REC_VERBOSE) last = Buffer+len-1; else - last = rindex(Buffer, '\n'); + last = strrchr(Buffer, '\n'); if (last) { - *++last = '\0'; + last++; write(1, Buffer, last-Buffer); + prompt = last; } } for (last = Buffer+len-2; last > Buffer && *last != ' '; last--) @@ -83,11 +103,11 @@ int Receive(int fd, unsigned TimeoutVal, int display) if (!passwd) passwd = getpass("Password: "); sprintf(Buffer, "passwd %s\n", passwd); - bzero(passwd, strlen(passwd)); + memset(passwd, '\0', strlen(passwd)); if (display & REC_VERBOSE) write(1, Buffer, strlen(Buffer)); write(fd, Buffer, strlen(Buffer)); - bzero(Buffer, strlen(Buffer)); + memset(Buffer, '\0', strlen(Buffer)); return Receive(fd, TimeoutVal, display & ~REC_PASSWD); } Result = 1; @@ -171,7 +191,7 @@ main(int argc, char **argv) } } else { char *port, *host, *colon; - int hlen; + int hlen; colon = strchr(argv[arg], ':'); if (colon) { @@ -262,25 +282,48 @@ main(int argc, char **argv) case 0: if (len == 0) { - if (!verbose) { - /* Give a \n to ppp for a prompt */ - write(fd, "\n", 1); - if (Receive(fd, TimeoutVal, REC_VERBOSE | REC_SHOW) != 0) { - fprintf(stderr, "Connection closed\n"); + EditLine *edit; + History *hist; + const char *l, *env; + int size; + + hist = history_init(); + if ((env = getenv("EL_SIZE"))) { + size = atoi(env); + if (size < 0) + size = 20; + } else + size = 20; + history(hist, H_EVENT, size); + + edit = el_init("pppctl", stdin, stdout); + el_source(edit, NULL); + el_set(edit, EL_PROMPT, GetPrompt); + if ((env = getenv("EL_EDITOR"))) + if (!strcmp(env, "vi")) + el_set(edit, EL_EDITOR, "vi"); + else if (!strcmp(env, "emacs")) + el_set(edit, EL_EDITOR, "emacs"); + el_set(edit, EL_SIGNAL, 1); + el_set(edit, EL_HIST, history, (const char *)hist); + while ((l = el_gets(edit, &len))) { + if (len > 1) + history(hist, H_ENTER, l); + write(fd, l, len); + if (!strcasecmp(l, "quit\n") || + !strcasecmp(l, "bye\n")) /* ok, we're cheating */ break; - } - } - while (fgets(Buffer, sizeof(Buffer)-1, stdin)) { - write(fd, Buffer, strlen(Buffer)); - if (Receive(fd, TimeoutVal, REC_VERBOSE | REC_SHOW) != 0) { + if (Receive(fd, TimeoutVal, REC_SHOW) != 0) { fprintf(stderr, "Connection closed\n"); break; } } + el_end(edit); + history_end(hist); } else { start = Command; do { - next = index(start, ';'); + next = strchr(start, ';'); while (*start == ' ' || *start == '\t') start++; if (next) -- cgit v1.1