diff options
author | Poul-Henning Kamp <phk@FreeBSD.org> | 2001-03-16 19:35:07 +0000 |
---|---|---|
committer | Poul-Henning Kamp <phk@FreeBSD.org> | 2001-03-16 19:35:07 +0000 |
commit | 420e745a9071afc365282a3ed1c3e70db7833aac (patch) | |
tree | caf9a32b48bfab1c706f1fe3f2d87741e2b711c3 /usr.sbin/ngctl | |
parent | daa4742e72c80cf48cf3b9f59c3ad92ddcb500c0 (diff) | |
download | src-420e745a9071afc365282a3ed1c3e70db7833aac.tar.gz src-420e745a9071afc365282a3ed1c3e70db7833aac.zip |
Notes
Diffstat (limited to 'usr.sbin/ngctl')
-rw-r--r-- | usr.sbin/ngctl/Makefile | 2 | ||||
-rw-r--r-- | usr.sbin/ngctl/main.c | 60 |
2 files changed, 54 insertions, 8 deletions
diff --git a/usr.sbin/ngctl/Makefile b/usr.sbin/ngctl/Makefile index d8dee7c6a9c1..9565b15cd8c8 100644 --- a/usr.sbin/ngctl/Makefile +++ b/usr.sbin/ngctl/Makefile @@ -6,7 +6,7 @@ SRCS= main.c mkpeer.c config.c connect.c name.c show.c list.c \ msg.c debug.c shutdown.c rmhook.c status.c types.c MAN8= ngctl.8 DPADD+= ${LIBNETGRAPH} -LDADD+= -lnetgraph +LDADD+= -lnetgraph -ledit COPTS+= -Wall .include <bsd.prog.mk> diff --git a/usr.sbin/ngctl/main.c b/usr.sbin/ngctl/main.c index 5e9411c9f283..93dfed3b8db8 100644 --- a/usr.sbin/ngctl/main.c +++ b/usr.sbin/ngctl/main.c @@ -39,23 +39,27 @@ */ #include "ngctl.h" +#include <histedit.h> #define PROMPT "+ " #define MAX_ARGS 512 #define WHITESPACE " \t\r\n\v\f" #define DUMP_BYTES_PER_LINE 16 +#define HISTSIZE 100 /* Internal functions */ static int ReadFile(FILE *fp); static int DoParseCommand(char *line); static int DoCommand(int ac, char **av); static int DoInteractive(void); +static int DoEditLine(void); static const struct ngcmd *FindCommand(const char *string); static int MatchCommand(const struct ngcmd *cmd, const char *s); static void Usage(const char *msg); static int ReadCmd(int ac, char **av); static int HelpCmd(int ac, char **av); static int QuitCmd(int ac, char **av); +static char * prompt(EditLine *el); /* List of commands */ static const struct ngcmd *const cmds[] = { @@ -103,6 +107,18 @@ const struct ngcmd quit_cmd = { /* Our control and data sockets */ int csock, dsock; +/* Provide editline(3) functionality */ +static EditLine *el = NULL; + +/* + * EL_PROMPT expects a function for the prompt + */ +static char * +prompt(EditLine *el) +{ + return PROMPT; +} + /* * main() */ @@ -252,13 +268,7 @@ DoInteractive(void) /* Get any user input */ if (FD_ISSET(0, &rfds)) { - char buf[LINE_MAX]; - - if (fgets(buf, sizeof(buf), stdin) == NULL) { - printf("\n"); - break; - } - if (DoParseCommand(buf) == CMDRTN_QUIT) + if (DoEditLine() == CMDRTN_QUIT) break; } } @@ -302,6 +312,42 @@ DoCommand(int ac, char **av) } /* + * Handle user input with editline(3) + */ +static int +DoEditLine(void) +{ + int num; + char *av = "ngctl"; + const char *buf; + History *hist; + + hist = history_init(); /* Initialize history */ + history(hist, H_EVENT, HISTSIZE); /* Size of history */ + + el = el_init(av, stdin, stdout); /* Initialize editline */ + + el_set(el, EL_PROMPT, prompt); /* Set the prompt */ + el_set(el, EL_EDITOR, "vi"); /* Default editor is vi */ + el_set(el, EL_HIST, history, hist); /* Use history */ + el_set(el, EL_SIGNAL, 1); /* Handle signals properly */ + el_source(el, NULL); /* Source user defaults */ + + while ((buf = el_gets(el, &num)) != NULL && num != 0) { + + history(hist, H_ENTER, buf); /* Add command to history */ + + if (DoParseCommand((char *)buf) == CMDRTN_QUIT) + return (CMDRTN_QUIT); /* Handle "quit" command */ + } + + el_end(el); /* Clean up editline */ + history_end(hist); /* and history */ + + return 0; +} + +/* * Find a command */ static const struct ngcmd * |