mirror of
https://https.git.savannah.gnu.org/git/bash.git
synced 2026-08-07 02:32:27 +02:00
Imported from ../bash-2.04.tar.gz.
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
# This is the Makefile for the examples subdirectory of readline. -*- text -*-
|
||||
#
|
||||
# Copyright (C) 1994 Free Software Foundation, Inc.
|
||||
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2, or (at your option)
|
||||
# any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
||||
|
||||
EXECUTABLES = fileman rltest rl
|
||||
CFLAGS = -g -I../.. -I..
|
||||
CFLAGS = -g -I../.. -I.. -DREADLINE_LIBRARY
|
||||
LDFLAGS = -g -L..
|
||||
|
||||
.c.o:
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
From: Jeff Solomon <jsolomon@stanford.edu>
|
||||
Date: Fri, 9 Apr 1999 10:13:27 -0700 (PDT)
|
||||
To: chet@po.cwru.edu
|
||||
Subject: new readline example
|
||||
Message-ID: <14094.12094.527305.199695@mrclean.Stanford.EDU>
|
||||
|
||||
Chet,
|
||||
|
||||
I've been using readline 4.0. Specifically, I've been using the perl
|
||||
version Term::ReadLine::Gnu. It works great.
|
||||
|
||||
Anyway, I've been playing around the alternate interface and I wanted
|
||||
to contribute a little C program, callback.c, to you that you could
|
||||
use as an example of the alternate interface in the /examples
|
||||
directory of the readline distribution.
|
||||
|
||||
My example shows how, using the alternate interface, you can
|
||||
interactively change the prompt (which is very nice imo). Also, I
|
||||
point out that you must roll your own terminal setting when using the
|
||||
alternate interface because readline depreps (using your parlance) the
|
||||
terminal while in the user callback. I try to demostrate what I mean
|
||||
with an example. I've included the program below.
|
||||
|
||||
To compile, I just put the program in the examples directory and made
|
||||
the appropriate changes to the EXECUTABLES and OBJECTS line and added
|
||||
an additional target 'callback'.
|
||||
|
||||
I compiled on my Sun Solaris2.6 box using Sun's cc.
|
||||
|
||||
Let me know what you think.
|
||||
|
||||
Jeff
|
||||
*/
|
||||
|
||||
#if defined (HAVE_CONFIG_H)
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <termios.h> /* xxx - should make this more general */
|
||||
|
||||
#ifdef READLINE_LIBRARY
|
||||
# include "readline.h"
|
||||
#else
|
||||
# include <readline/readline.h>
|
||||
#endif
|
||||
|
||||
/* This little examples demonstrates the alternate interface to using readline.
|
||||
* In the alternate interface, the user maintains control over program flow and
|
||||
* only calls readline when STDIN is readable. Using the alternate interface,
|
||||
* you can do anything else while still using readline (like talking to a
|
||||
* network or another program) without blocking.
|
||||
*
|
||||
* Specifically, this program highlights two importants features of the
|
||||
* alternate interface. The first is the ability to interactively change the
|
||||
* prompt, which can't be done using the regular interface since rl_prompt is
|
||||
* read-only.
|
||||
*
|
||||
* The second feature really highlights a subtle point when using the alternate
|
||||
* interface. That is, readline will not alter the terminal when inside your
|
||||
* callback handler. So let's so, your callback executes a user command that
|
||||
* takes a non-trivial amount of time to complete (seconds). While your
|
||||
* executing the command, the user continues to type keystrokes and expects them
|
||||
* to be re-echoed on the new prompt when it returns. Unfortunately, the default
|
||||
* terminal configuration doesn't do this. After the prompt returns, the user
|
||||
* must hit one additional keystroke and then will see all of his previous
|
||||
* keystrokes. To illustrate this, compile and run this program. Type "sleep" at
|
||||
* the prompt and then type "bar" before the prompt returns (you have 3
|
||||
* seconds). Notice how "bar" is re-echoed on the prompt after the prompt
|
||||
* returns? This is what you expect to happen. Now comment out the 4 lines below
|
||||
* the line that says COMMENT LINE BELOW. Recompile and rerun the program and do
|
||||
* the same thing. When the prompt returns, you should not see "bar". Now type
|
||||
* "f", see how "barf" magically appears? This behavior is un-expected and not
|
||||
* desired.
|
||||
*/
|
||||
|
||||
void process_line(char *line);
|
||||
int change_prompt(void);
|
||||
char *get_prompt(void);
|
||||
|
||||
int prompt = 1;
|
||||
char prompt_buf[40], line_buf[256];
|
||||
tcflag_t old_lflag;
|
||||
cc_t old_vtime;
|
||||
struct termios term;
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
fd_set fds;
|
||||
|
||||
/* Adjust the terminal slightly before the handler is installed. Disable
|
||||
* canonical mode processing and set the input character time flag to be
|
||||
* non-blocking.
|
||||
*/
|
||||
if( tcgetattr(STDIN_FILENO, &term) < 0 ) {
|
||||
perror("tcgetattr");
|
||||
exit(1);
|
||||
}
|
||||
old_lflag = term.c_lflag;
|
||||
old_vtime = term.c_cc[VTIME];
|
||||
term.c_lflag &= ~ICANON;
|
||||
term.c_cc[VTIME] = 1;
|
||||
/* COMMENT LINE BELOW - see above */
|
||||
if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 ) {
|
||||
perror("tcsetattr");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
rl_add_defun("change-prompt", change_prompt, CTRL('t'));
|
||||
rl_callback_handler_install(get_prompt(), process_line);
|
||||
|
||||
while(1) {
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(fileno(stdin), &fds);
|
||||
|
||||
if( select(FD_SETSIZE, &fds, NULL, NULL, NULL) < 0) {
|
||||
perror("select");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if( FD_ISSET(fileno(stdin), &fds) ) {
|
||||
rl_callback_read_char();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
process_line(char *line)
|
||||
{
|
||||
if( line == NULL ) {
|
||||
fprintf(stderr, "\n", line);
|
||||
|
||||
/* reset the old terminal setting before exiting */
|
||||
term.c_lflag = old_lflag;
|
||||
term.c_cc[VTIME] = old_vtime;
|
||||
if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 ) {
|
||||
perror("tcsetattr");
|
||||
exit(1);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if( strcmp(line, "sleep") == 0 ) {
|
||||
sleep(3);
|
||||
} else {
|
||||
fprintf(stderr, "|%s|\n", line);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
change_prompt(void)
|
||||
{
|
||||
/* toggle the prompt variable */
|
||||
prompt = !prompt;
|
||||
|
||||
/* save away the current contents of the line */
|
||||
strcpy(line_buf, rl_line_buffer);
|
||||
|
||||
/* install a new handler which will change the prompt and erase the current line */
|
||||
rl_callback_handler_install(get_prompt(), process_line);
|
||||
|
||||
/* insert the old text on the new line */
|
||||
rl_insert_text(line_buf);
|
||||
|
||||
/* redraw the current line - this is an undocumented function. It invokes the
|
||||
* redraw-current-line command.
|
||||
*/
|
||||
rl_refresh_line(0, 0);
|
||||
}
|
||||
|
||||
char *
|
||||
get_prompt(void)
|
||||
{
|
||||
/* The prompts can even be different lengths! */
|
||||
sprintf(prompt_buf, "%s",
|
||||
prompt ? "Hit ctrl-t to toggle prompt> " : "Pretty cool huh?> ");
|
||||
return prompt_buf;
|
||||
}
|
||||
@@ -1,22 +1,22 @@
|
||||
/* fileman.c -- A tiny application which demonstrates how to use the
|
||||
GNU Readline library. This application interactively allows users
|
||||
to manipulate files and their modes. */
|
||||
/*
|
||||
* Remove the next line if you're compiling this against an installed
|
||||
* libreadline.a
|
||||
*/
|
||||
#define READLINE_LIBRARY
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#ifdef HAVE_SYS_FILE_H
|
||||
#include <sys/file.h>
|
||||
# include <sys/file.h>
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
@@ -26,6 +26,10 @@
|
||||
# include <strings.h>
|
||||
#endif /* !HAVE_STRING_H */
|
||||
|
||||
#ifdef HAVE_STDLIB_H
|
||||
# include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#ifdef READLINE_LIBRARY
|
||||
# include "readline.h"
|
||||
# include "history.h"
|
||||
@@ -34,7 +38,6 @@
|
||||
# include <readline/history.h>
|
||||
#endif
|
||||
|
||||
extern char *getwd ();
|
||||
extern char *xmalloc ();
|
||||
|
||||
/* The names of functions that actually do the manipulation. */
|
||||
@@ -300,7 +303,12 @@ com_view (arg)
|
||||
if (!valid_argument ("view", arg))
|
||||
return 1;
|
||||
|
||||
#if defined (__MSDOS__)
|
||||
/* more.com doesn't grok slashes in pathnames */
|
||||
sprintf (syscom, "less %s", arg);
|
||||
#else
|
||||
sprintf (syscom, "more %s", arg);
|
||||
#endif
|
||||
return (system (syscom));
|
||||
}
|
||||
|
||||
@@ -406,7 +414,7 @@ com_pwd (ignore)
|
||||
{
|
||||
char dir[1024], *s;
|
||||
|
||||
s = getwd (dir);
|
||||
s = getcwd (dir, sizeof(dir) - 1);
|
||||
if (s == 0)
|
||||
{
|
||||
printf ("Error getting pwd: %s\n", dir);
|
||||
|
||||
+26
-13
@@ -2,15 +2,9 @@
|
||||
* rl - command-line interface to read a line from the standard input
|
||||
* (or another fd) using readline.
|
||||
*
|
||||
* usage: rl [-p prompt] [-u unit] [-d default]
|
||||
* usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
|
||||
*/
|
||||
|
||||
/*
|
||||
* Remove the next line if you're compiling this against an installed
|
||||
* libreadline.a
|
||||
*/
|
||||
#define READLINE_LIBRARY
|
||||
|
||||
#if defined (HAVE_CONFIG_H)
|
||||
# include <config.h>
|
||||
#endif
|
||||
@@ -18,8 +12,14 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include "posixstat.h"
|
||||
#include "readline.h"
|
||||
#include "history.h"
|
||||
|
||||
#if defined (READLINE_LIBRARY)
|
||||
# include "readline.h"
|
||||
# include "history.h"
|
||||
#else
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
#endif
|
||||
|
||||
extern int optind;
|
||||
extern char *optarg;
|
||||
@@ -40,22 +40,24 @@ set_deftext ()
|
||||
deftext = (char *)NULL;
|
||||
rl_startup_hook = (Function *)NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
usage()
|
||||
{
|
||||
fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default]\n",
|
||||
fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
|
||||
progname, progname);
|
||||
}
|
||||
|
||||
int
|
||||
main (argc, argv)
|
||||
int argc;
|
||||
char **argv;
|
||||
{
|
||||
char *temp, *prompt;
|
||||
struct stat sb;
|
||||
int opt, fd;
|
||||
int opt, fd, nch;
|
||||
FILE *ifp;
|
||||
|
||||
progname = strrchr(argv[0], '/');
|
||||
@@ -66,10 +68,10 @@ main (argc, argv)
|
||||
|
||||
/* defaults */
|
||||
prompt = "readline$ ";
|
||||
fd = 0;
|
||||
fd = nch = 0;
|
||||
deftext = (char *)0;
|
||||
|
||||
while ((opt = getopt(argc, argv, "p:u:d:")) != EOF)
|
||||
while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
@@ -87,6 +89,14 @@ main (argc, argv)
|
||||
case 'd':
|
||||
deftext = optarg;
|
||||
break;
|
||||
case 'n':
|
||||
nch = atoi(optarg);
|
||||
if (nch < 0)
|
||||
{
|
||||
fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
|
||||
exit (2);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
usage ();
|
||||
exit (2);
|
||||
@@ -107,6 +117,9 @@ main (argc, argv)
|
||||
if (deftext && *deftext)
|
||||
rl_startup_hook = set_deftext;
|
||||
|
||||
if (nch > 0)
|
||||
rl_num_chars_to_read = nch;
|
||||
|
||||
temp = readline (prompt);
|
||||
|
||||
/* Test for EOF. */
|
||||
|
||||
@@ -4,20 +4,20 @@
|
||||
/* */
|
||||
/* **************************************************************** */
|
||||
|
||||
/*
|
||||
* Remove the next line if you're compiling this against an installed
|
||||
* libreadline.a
|
||||
*/
|
||||
#define READLINE_LIBRARY
|
||||
|
||||
#if defined (HAVE_CONFIG_H)
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include "readline.h"
|
||||
#include "history.h"
|
||||
|
||||
#ifdef READLINE_LIBRARY
|
||||
# include "readline.h"
|
||||
# include "history.h"
|
||||
#else
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
#endif
|
||||
|
||||
extern HIST_ENTRY **history_list ();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user