diff options
Diffstat (limited to 'usr.sbin/pppctl/pppctl.c')
-rw-r--r-- | usr.sbin/pppctl/pppctl.c | 91 |
1 files changed, 67 insertions, 24 deletions
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 <sys/types.h> + #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/un.h> #include <netdb.h> + +#include <histedit.h> #include <signal.h> -#include <string.h> #include <stdio.h> -#include <unistd.h> #include <stdlib.h> +#include <string.h> +#include <unistd.h> #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) |