Add support for command line editing and history.

Remove src/contrib/bind/bin/nslookup/commands.c as it is generated by lex
from commands.l.

Submitted by: lpc/cdcontrol patches originally by msmith.
Reviewed by: msmith (in theory)
This commit is contained in:
mdodd 1999-08-19 03:29:15 +00:00
parent e675f51617
commit 203aa2011f
10 changed files with 180 additions and 2089 deletions

File diff suppressed because it is too large Load Diff

View File

@ -95,12 +95,65 @@ static char sccsid[] = "@(#)commands.l 5.13 (Berkeley) 7/24/90";
#include "port_before.h"
#include <sys/types.h>
#include <histedit.h>
#include "port_after.h"
#include "res.h"
extern char rootServerName[];
extern void PrintHelp();
#define YY_INPUT(buf, result, max_size) \
{ \
nslookup_yy_input((char *)buf, &result, \
max_size, yy_current_buffer->yy_is_interactive); \
}
const char *
nslookup_prompt()
{
return ("> ");
}
int
nslookup_yy_input(buf, result, max_size, intr)
char * buf;
int * result;
int max_size;
int intr;
{
static EditLine *el = NULL;
static History *hist = NULL;
int num = 0;
const char *bp = NULL;
if (intr) {
if (!el) {
el = el_init("nslookup", yyin, yyout);
hist = history_init();
history(hist, H_EVENT, 100);
el_set(el, EL_HIST, history, hist);
el_set(el, EL_EDITOR, "emacs");
el_set(el, EL_PROMPT, nslookup_prompt);
el_set(el, EL_SIGNAL, 1);
}
if ((bp = el_gets(el, &num)) == NULL || num == 0) {
*result = 0;
return;
}
*result = (num > max_size) ? max_size : num;
strncpy(buf, bp, *result);
history(hist, H_ENTER, bp);
} else {
if ( ((*result = fread( buf, 1, max_size, yyin )) == 0)
&& ferror( yyin ) )
yy_fatal_error( "input in flex scanner failed" );
}
return;
}
%}
WS [ \t]
FLET [A-Za-z0-9.*\\]

View File

@ -344,12 +344,7 @@ main(argc, argv)
* Yylex returns 0 when ^D or 'exit' is typed.
*/
printf("> ");
fflush(stdout);
while(yylex()) {
printf("> ");
fflush(stdout);
}
while(yylex());
}
exit(0);
}

View File

@ -1,4 +1,7 @@
PROG= cdcontrol
CFLAGS+= -Wall
DPADD += ${LIBEDIT} ${LIBTERMCAP}
LDADD += -ledit -ltermcap
.include <bsd.prog.mk>

View File

@ -20,7 +20,7 @@
#ifndef lint
static const char rcsid[] =
"$Id: cdcontrol.c,v 1.17 1998/01/26 00:57:54 jmz Exp $";
"$Id: cdcontrol.c,v 1.18 1999/01/31 15:30:21 billf Exp $";
#endif /* not lint */
#include <ctype.h>
@ -33,6 +33,7 @@ static const char rcsid[] =
#include <sys/file.h>
#include <sys/cdio.h>
#include <sys/ioctl.h>
#include <histedit.h>
#define VERSION "2.0"
@ -922,18 +923,48 @@ int status (int *trk, int *min, int *sec, int *frame)
return s.data->header.audio_status;
}
char *input (int *cmd)
const char *
cdcontrol_prompt()
{
static char buf[80];
return ("cdcontrol> ");
}
char *
input (int *cmd)
{
#define MAXLINE 80
static EditLine *el = NULL;
static History *hist = NULL;
static char buf[MAXLINE];
int num = 0;
const char *bp = NULL;
char *p;
do {
if (verbose)
fprintf (stderr, "cdcontrol> ");
if (! fgets (buf, sizeof (buf), stdin)) {
*cmd = CMD_QUIT;
fprintf (stderr, "\r\n");
return (0);
if (verbose) {
if (!el) {
el = el_init("cdcontrol", stdin, stdout);
hist = history_init();
history(hist, H_EVENT, 100);
el_set(el, EL_HIST, history, hist);
el_set(el, EL_EDITOR, "emacs");
el_set(el, EL_PROMPT, cdcontrol_prompt);
el_set(el, EL_SIGNAL, 1);
}
if ((bp = el_gets(el, &num)) == NULL || num == 0)
return (0);
memcpy(buf, bp, (MAXLINE > num ? MAXLINE : num));
buf[num] = 0;
history(hist, H_ENTER, bp);
#undef MAXLINE
} else {
if (! fgets (buf, sizeof (buf), stdin)) {
*cmd = CMD_QUIT;
fprintf (stderr, "\r\n");
return (0);
}
}
p = parse (buf, cmd);
} while (! p);

View File

@ -1,5 +1,5 @@
# From: @(#)Makefile 8.1 (Berkeley) 6/6/93
# $Id: Makefile,v 1.4 1997/12/16 17:53:19 bde Exp $
# $Id: Makefile,v 1.5 1998/03/07 09:47:57 bde Exp $
PROG= lpc
CFLAGS+=-I${.CURDIR}/../common_source ${CWARNFLAGS}
@ -8,7 +8,7 @@ SRCS= lpc.c cmds.c cmdtab.c
BINGRP= daemon
BINMODE=2555
.PATH: ${.CURDIR}/../common_source
DPADD= ${LIBLPR}
LDADD= ${LIBLPR}
DPADD= ${LIBLPR} ${LIBEDIT} ${LIBTERMCAP}
LDADD= ${LIBLPR} -ledit -ltermcap
.include <bsd.prog.mk>

View File

@ -43,7 +43,7 @@ static const char copyright[] =
static char sccsid[] = "@(#)lpc.c 8.3 (Berkeley) 4/28/95";
#endif
static const char rcsid[] =
"$Id: lpc.c,v 1.7 1998/03/22 20:19:27 jb Exp $";
"$Id: lpc.c,v 1.8 1998/09/11 18:49:31 wollman Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -59,6 +59,7 @@ static const char rcsid[] =
#include <syslog.h>
#include <string.h>
#include <unistd.h>
#include <histedit.h>
#include "lp.h"
#include "lpc.h"
@ -79,13 +80,10 @@ static int fromatty;
static char cmdline[MAX_CMDLINE];
static int margc;
static char *margv[MAX_MARGV];
static int top;
uid_t uid, euid;
static jmp_buf toplevel;
int main __P((int, char *[]));
static void cmdscanner __P((int));
static void cmdscanner __P((void));
static struct cmd *getcmd __P((char *));
static void intr __P((int));
static void makeargv __P((void));
@ -125,12 +123,10 @@ main(argc, argv)
exit(0);
}
fromatty = isatty(fileno(stdin));
top = setjmp(toplevel) == 0;
if (top)
if (!fromatty)
signal(SIGINT, intr);
for (;;) {
cmdscanner(top);
top = 1;
cmdscanner();
}
}
@ -138,32 +134,58 @@ static void
intr(signo)
int signo;
{
if (!fromatty)
exit(0);
longjmp(toplevel, 1);
exit(0);
}
static char *
lpc_prompt()
{
return("lpc> ");
}
/*
* Command parser.
*/
static void
cmdscanner(top)
int top;
cmdscanner()
{
register struct cmd *c;
static EditLine *el = NULL;
static History *hist = NULL;
int num = 0;
const char *bp = NULL;
if (!top)
putchar('\n');
for (;;) {
if (fromatty) {
printf("lpc> ");
fflush(stdout);
if (!el) {
el = el_init("lpc", stdin, stdout);
hist = history_init();
history(hist, H_EVENT, 100);
el_set(el, EL_HIST, history, hist);
el_set(el, EL_EDITOR, "emacs");
el_set(el, EL_PROMPT, lpc_prompt);
el_set(el, EL_SIGNAL, 1);
}
if ((bp = el_gets(el, &num)) == NULL || num == 0)
return;
memcpy(cmdline, bp, (MAX_CMDLINE > num ? MAX_CMDLINE : num));
cmdline[num] = 0;
history(hist, H_ENTER, bp);
} else {
if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
quit(0, NULL);
if (cmdline[0] == 0 || cmdline[0] == '\n')
break;
}
if (fgets(cmdline, MAX_CMDLINE, stdin) == 0)
quit(0, NULL);
if (cmdline[0] == 0 || cmdline[0] == '\n')
break;
makeargv();
if (margc == 0)
continue;
if (el_parse(el, margc, margv) != -1)
continue;
c = getcmd(margv[0]);
if (c == (struct cmd *)-1) {
printf("?Ambiguous command\n");
@ -182,7 +204,6 @@ cmdscanner(top)
else
(*c->c_handler)(margc, margv);
}
longjmp(toplevel, 0);
}
static struct cmd *

View File

@ -1,4 +1,4 @@
# $Id: Makefile,v 1.6 1998/05/03 05:14:56 peter Exp $
# $Id: Makefile,v 1.7 1998/05/04 20:00:14 bde Exp $
.include "${.CURDIR}/../named/Makefile.inc"
@ -10,8 +10,8 @@ SRCS= main.c getinfo.c debug.c send.c skip.c list.c subr.c commands.l
MAN8= nslookup.8
CFLAGS+=-D_PATH_HELPFILE=\"${DESTHELP}/nslookup.help\"
LDADD+= -ll
DPADD+= ${LIBL}
LDADD+= -ll -ledit -ltermcap
DPADD+= ${LIBL} ${LIBEDIT} ${LIBTERMCAP}
beforeinstall:
${INSTALL} -c -o ${BINOWN} -g ${BINGRP} -m 444 \

View File

@ -1,11 +1,11 @@
#
# $Id: Makefile,v 1.8 1998/03/07 09:46:17 bde Exp $
# $Id: Makefile,v 1.9 1998/03/19 15:12:05 bde Exp $
#
CFLAGS+= -I${.CURDIR}/../include
DPADD= ${LIBNTP}
LDADD= ${LIBNTP}
DPADD= ${LIBNTP} ${LIBEDIT} ${LIBTERMCAP}
LDADD= ${LIBNTP} -ledit -ltermcap
PROG= xntpdc
MAN8= ${.CURDIR}/../doc/xntpdc.8

View File

@ -8,6 +8,7 @@
#include <sys/types.h>
#include <sys/time.h>
#include <netdb.h>
#include <histedit.h>
#include "ntpdc.h"
#include "ntp_select.h"
@ -210,7 +211,7 @@ int debug;
/*
* main - parse arguments and handle options
*/
void
int
main(argc, argv)
int argc;
char *argv[];
@ -791,6 +792,11 @@ doquery(implcode, reqcode, auth, qitems, qsize, qdata, ritems, rsize, rdata,
return res;
}
const char *
xntpdc_prompt()
{
return (prompt);
}
/*
* getcmds - read commands from the standard input and execute them
@ -798,16 +804,34 @@ doquery(implcode, reqcode, auth, qitems, qsize, qdata, ritems, rsize, rdata,
static void
getcmds()
{
static EditLine *el = NULL;
static History *hist = NULL;
char line[MAXLINE];
int num = 0;
const char *bp = NULL;
for (;;) {
if (interactive) {
(void) fputs(prompt, stderr);
(void) fflush(stderr);
}
if (!el) {
el = el_init("xntpdc", stdin, stdout);
hist = history_init();
history(hist, H_EVENT, 100);
el_set(el, EL_HIST, history, hist);
el_set(el, EL_EDITOR, "emacs");
el_set(el, EL_PROMPT, xntpdc_prompt);
el_set(el, EL_SIGNAL, 1);
}
if ((bp = el_gets(el, &num)) == NULL || num == 0)
return;
if (fgets(line, sizeof line, stdin) == NULL)
return;
memcpy(line, bp, (MAXLINE > num ? MAXLINE : num));
line[num] = 0;
history(hist, H_ENTER, bp);
} else {
if (fgets(line, sizeof line, stdin) == NULL)
return;
}
docmd(line);
}