summaryrefslogtreecommitdiff
path: root/readline/examples
diff options
context:
space:
mode:
authorElena Zannoni <ezannoni@kwikemart.cygnus.com>2002-12-08 22:31:39 +0000
committerElena Zannoni <ezannoni@kwikemart.cygnus.com>2002-12-08 22:31:39 +0000
commit711a125cd3c4a70294cd96615120cb61b64ef88e (patch)
treecd5afad1dcbda8ecd4316225edaed77fc3174ee6 /readline/examples
parent726941b1ecf998b928ad0e0f84d637849f6c3e63 (diff)
downloadgdb-711a125cd3c4a70294cd96615120cb61b64ef88e.tar.gz
Import of readline 4.3.
Non-readline modified files: src/gdb/ChangeLog src/gdb/defs.h src/gdb/cli/cli-cmds.c src/gdb/cli/cli-setshow.c src/gdb/tui/ChangeLog src/gdb/tui/tuiWin.c In readline directory: * compat.c, mbutil.c, misc.c, rlmbutil.h, rltypedefs.h, text.c, doc/history.0, doc/history.3, support/wcwidth.c, examples/readlinebuf.h, examples/rlcat.c: New files. * CHANGELOG, CHANGES, INSTALL, MANIFEST, Makefile.in, README, aclocal.m4, ansi_stdlib.h, bind.c, callback.c, chardefs.h, complete.c, config.h.in, configure, configure.in, display.c, emacs_keymap.c, funmap.c, histexpand.c, histfile.c, histlib.h, history.c, history.h, histsearch.c, input.c, isearch.c, keymaps.c, keymaps.h, kill.c, macro.c, nls.c, parens.c, posixdir.h, readline.c, readline.h, rlconf.h, rldefs.h, rlprivate.h, rlshell.h, rlstdc.h, rltty.c, savestring.c, search.c, shell.c, signals.c, terminal.c, tilde.c, tilde.h, undo.c, util.c, vi_keymap.c, vi_mode.c, xmalloc.c, xmalloc.h, doc/Makefile.in, doc/hist.texinfo, doc/hstech.texinfo, doc/hsuser.texinfo, doc/manvers.texinfo, doc/readline.3, doc/rlman.texinfo, doc/rltech.texinfo, doc/rluser.texinfo doc/rluserman.texinfo, doc/texi2dvi, doc/texi2html, shlib/Makefile.in, support/install.sh, support/mkdirs, support/mkdist, support/shlib-install, support/shobj-conf, examples/Inputrc, examples/Makefile.in, examples/fileman.c, examples/histexamp.c, examples/manexamp.c, examples/rl.c, examples/rlfe.c, examples/rltest.c, examples/rlversion.c: Modified files.
Diffstat (limited to 'readline/examples')
-rw-r--r--readline/examples/Inputrc16
-rw-r--r--readline/examples/Makefile.in33
-rw-r--r--readline/examples/fileman.c47
-rw-r--r--readline/examples/histexamp.c32
-rw-r--r--readline/examples/manexamp.c32
-rw-r--r--readline/examples/readlinebuf.h139
-rw-r--r--readline/examples/rl.c24
-rw-r--r--readline/examples/rlcat.c174
-rw-r--r--readline/examples/rlfe.c383
-rw-r--r--readline/examples/rltest.c20
-rw-r--r--readline/examples/rlversion.c20
11 files changed, 880 insertions, 40 deletions
diff --git a/readline/examples/Inputrc b/readline/examples/Inputrc
index 5b71bd701c4..d7fdb42efaa 100644
--- a/readline/examples/Inputrc
+++ b/readline/examples/Inputrc
@@ -4,6 +4,22 @@
# on which program is running, or what terminal is active.
#
+# Copyright (C) 1989-2002 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.
+
# In all programs, all terminals, make sure this is bound.
"\C-x\C-r": re-read-init-file
diff --git a/readline/examples/Makefile.in b/readline/examples/Makefile.in
index 7246e75f8ce..72c9904ff0f 100644
--- a/readline/examples/Makefile.in
+++ b/readline/examples/Makefile.in
@@ -16,6 +16,7 @@
# 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.
+RL_LIBRARY_VERSION = @LIBVERSION@
SHELL = @MAKE_SHELL@
RM = rm -f
@@ -25,16 +26,19 @@ VPATH = .:@srcdir@
top_srcdir = @top_srcdir@
BUILD_DIR = .
+# Support an alternate destination root directory for package building
+DESTDIR =
+
DEFS = @DEFS@
CC = @CC@
CFLAGS = @CFLAGS@
-LOCAL_CFLAGS = @LOCAL_CFLAGS@ -DREADLINE_LIBRARY
+LOCAL_CFLAGS = @LOCAL_CFLAGS@ -DREADLINE_LIBRARY -DRL_LIBRARY_VERSION='"$(RL_LIBRARY_VERSION)"'
CPPFLAGS = @CPPFLAGS@
INCLUDES = -I$(srcdir) -I$(top_srcdir) -I..
CCFLAGS = $(DEFS) $(LOCAL_CFLAGS) $(CPPFLAGS) $(INCLUDES) $(CFLAGS)
-LDFLAGS = -g -L..
+LDFLAGS = -g -L.. @LDFLAGS@
READLINE_LIB = ../libreadline.a
HISTORY_LIB = ../libhistory.a
@@ -45,26 +49,34 @@ TERMCAP_LIB = @TERMCAP_LIB@
${RM} $@
$(CC) $(CCFLAGS) -c $<
-EXECUTABLES = fileman rltest rl rlversion
-OBJECTS = fileman.o rltest.o rl.o rlversion.o
+EXECUTABLES = fileman rltest rl rlcat rlversion histexamp
+OBJECTS = fileman.o rltest.o rl.o rlversion.o histexamp.o
all: $(EXECUTABLES)
+everything: all rlfe
-rl: rl.o
+rl: rl.o $(READLINE_LIB)
$(CC) $(LDFLAGS) -o $@ rl.o -lreadline $(TERMCAP_LIB)
-fileman: fileman.o
+rlcat: rlcat.o $(READLINE_LIB)
+ $(CC) $(LDFLAGS) -o $@ rlcat.o -lreadline $(TERMCAP_LIB)
+
+fileman: fileman.o $(READLINE_LIB)
$(CC) $(LDFLAGS) -o $@ fileman.o -lreadline $(TERMCAP_LIB)
-rltest: rltest.o
+rltest: rltest.o $(READLINE_LIB)
$(CC) $(LDFLAGS) -o $@ rltest.o -lreadline $(TERMCAP_LIB)
rlversion: rlversion.o $(READLINE_LIB)
$(CC) $(LDFLAGS) -o $@ rlversion.o -lreadline $(TERMCAP_LIB)
+histexamp: histexamp.o $(HISTORY_LIB)
+ $(CC) $(LDFLAGS) -o $@ histexamp.o -lhistory $(TERMCAP_LIB)
+
clean mostlyclean:
$(RM) $(OBJECTS)
$(RM) $(EXECUTABLES) *.exe
+ $(RM) rlfe.o rlfe
distclean maintainer-clean: clean
$(RM) Makefile
@@ -73,6 +85,13 @@ fileman.o: fileman.c
rltest.o: rltest.c
rl.o: rl.c
rlversion.o: rlversion.c
+histexamp.o: histexamp.c
+
+fileman.o: $(top_srcdir)/readline.h
+rltest.o: $(top_srcdir)/readline.h
+rl.o: $(top_srcdir)/readline.h
+rlversion.o: $(top_srcdir)/readline.h
+histexamp.o: $(top_srcdir)/history.h
# Stuff for Per Bothner's `rlfe' program
diff --git a/readline/examples/fileman.c b/readline/examples/fileman.c
index dc29a40104f..340eee739f6 100644
--- a/readline/examples/fileman.c
+++ b/readline/examples/fileman.c
@@ -1,3 +1,23 @@
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
+
+ This file is part of the GNU Readline Library, a library for
+ reading lines of text with interactive input and history editing.
+
+ The GNU Readline Library 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.
+
+ The GNU Readline Library 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.
+
+ The GNU General Public License is often shipped with GNU software, and
+ is generally kept in a file called COPYING or LICENSE. If you do not
+ have a copy of the license, write to the Free Software Foundation,
+ 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
/* 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. */
@@ -41,15 +61,22 @@
extern char *xmalloc ();
/* The names of functions that actually do the manipulation. */
-int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
-int com_delete (), com_help (), com_cd (), com_quit ();
+int com_list PARAMS((char *));
+int com_view PARAMS((char *));
+int com_rename PARAMS((char *));
+int com_stat PARAMS((char *));
+int com_pwd PARAMS((char *));
+int com_delete PARAMS((char *));
+int com_help PARAMS((char *));
+int com_cd PARAMS((char *));
+int com_quit PARAMS((char *));
/* A structure which contains information on the commands this program
can understand. */
typedef struct {
char *name; /* User printable name of the function. */
- Function *func; /* Function to call to do the job. */
+ rl_icpfunc_t *func; /* Function to call to do the job. */
char *doc; /* Documentation for this function. */
} COMMAND;
@@ -65,7 +92,7 @@ COMMAND commands[] = {
{ "rename", com_rename, "Rename FILE to NEWNAME" },
{ "stat", com_stat, "Print out statistics on FILE" },
{ "view", com_view, "View the contents of FILE" },
- { (char *)NULL, (Function *)NULL, (char *)NULL }
+ { (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
};
/* Forward declarations. */
@@ -205,8 +232,8 @@ stripwhite (string)
/* */
/* **************************************************************** */
-char *command_generator ();
-char **fileman_completion ();
+char *command_generator PARAMS((const char *, int));
+char **fileman_completion PARAMS((const char *, int, int));
/* Tell the GNU Readline library how to complete. We want to try to complete
on command names if this is the first word in the line, or on filenames
@@ -217,7 +244,7 @@ initialize_readline ()
rl_readline_name = "FileMan";
/* Tell the completer that we want a crack first. */
- rl_attempted_completion_function = (CPPFunction *)fileman_completion;
+ rl_attempted_completion_function = fileman_completion;
}
/* Attempt to complete on the contents of TEXT. START and END bound the
@@ -227,7 +254,7 @@ initialize_readline ()
or NULL if there aren't any. */
char **
fileman_completion (text, start, end)
- char *text;
+ const char *text;
int start, end;
{
char **matches;
@@ -238,7 +265,7 @@ fileman_completion (text, start, end)
to complete. Otherwise it is the name of a file in the current
directory. */
if (start == 0)
- matches = completion_matches (text, command_generator);
+ matches = rl_completion_matches (text, command_generator);
return (matches);
}
@@ -248,7 +275,7 @@ fileman_completion (text, start, end)
start at the top of the list. */
char *
command_generator (text, state)
- char *text;
+ const char *text;
int state;
{
static int list_index, len;
diff --git a/readline/examples/histexamp.c b/readline/examples/histexamp.c
index eceb66df19e..45651dfb137 100644
--- a/readline/examples/histexamp.c
+++ b/readline/examples/histexamp.c
@@ -1,4 +1,34 @@
-main ()
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
+
+ This file is part of the GNU Readline Library, a library for
+ reading lines of text with interactive input and history editing.
+
+ The GNU Readline Library 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.
+
+ The GNU Readline Library 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.
+
+ The GNU General Public License is often shipped with GNU software, and
+ is generally kept in a file called COPYING or LICENSE. If you do not
+ have a copy of the license, write to the Free Software Foundation,
+ 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
+#include <stdio.h>
+
+#ifdef READLINE_LIBRARY
+# include "history.h"
+#else
+# include <readline/history.h>
+#endif
+
+main (argc, argv)
+ int argc;
+ char **argv;
{
char line[1024], *t;
int len, done = 0;
diff --git a/readline/examples/manexamp.c b/readline/examples/manexamp.c
index 3496efa00e7..9c6cf2c76c6 100644
--- a/readline/examples/manexamp.c
+++ b/readline/examples/manexamp.c
@@ -1,12 +1,31 @@
/* manexamp.c -- The examples which appear in the documentation are here. */
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
+
+ This file is part of the GNU Readline Library, a library for
+ reading lines of text with interactive input and history editing.
+
+ The GNU Readline Library 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.
+
+ The GNU Readline Library 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.
+
+ The GNU General Public License is often shipped with GNU software, and
+ is generally kept in a file called COPYING or LICENSE. If you do not
+ have a copy of the license, write to the Free Software Foundation,
+ 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
#include <stdio.h>
#include <readline/readline.h>
-
/* **************************************************************** */
/* */
-* How to Emulate gets () */
+/* How to Emulate gets () */
/* */
/* **************************************************************** */
@@ -82,13 +101,12 @@ invert_case_line (count, key)
for (; start != end; start += direction)
{
- if (uppercase_p (rl_line_buffer[start]))
- rl_line_buffer[start] = to_lower (rl_line_buffer[start]);
- else if (lowercase_p (rl_line_buffer[start]))
- rl_line_buffer[start] = to_upper (rl_line_buffer[start]);
+ if (_rl_uppercase_p (rl_line_buffer[start]))
+ rl_line_buffer[start] = _rl_to_lower (rl_line_buffer[start]);
+ else if (_rl_lowercase_p (rl_line_buffer[start]))
+ rl_line_buffer[start] = _rl_to_upper (rl_line_buffer[start]);
}
/* Move point to on top of the last character changed. */
rl_point = end - direction;
}
-
diff --git a/readline/examples/readlinebuf.h b/readline/examples/readlinebuf.h
new file mode 100644
index 00000000000..a8af52cedf4
--- /dev/null
+++ b/readline/examples/readlinebuf.h
@@ -0,0 +1,139 @@
+/*******************************************************************************
+ * $Revision: 1.2 $
+ * $Date: 2001/09/11 06:19:36 $
+ * $Author: vyzo $
+ *
+ * Contents: A streambuf which uses the GNU readline library for line I/O
+ * (c) 2001 by Dimitris Vyzovitis [vyzo@media.mit.edu]
+ *
+ * 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 of the License, 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-1307 USA
+ *
+ ******************************************************************************/
+
+#ifndef _READLINEBUF_H_
+#define _READLINEBUF_H_
+
+#include <iostream>
+#include <cstring>
+#include <cassert>
+#include <cstdlib>
+#include <cstdio>
+
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#if (defined __GNUC__) && (__GNUC__ < 3)
+#include <streambuf.h>
+#else
+#include <streambuf>
+using std::streamsize;
+using std::streambuf;
+#endif
+
+class readlinebuf : public streambuf {
+public:
+#if (defined __GNUC__) && (__GNUC__ < 3)
+ typedef char char_type;
+ typedef int int_type;
+ typedef streampos pos_type;
+ typedef streamoff off_type;
+#endif
+ static const int_type eof = EOF; // this is -1
+ static const int_type not_eof = 0;
+
+private:
+ const char* prompt_;
+ bool history_;
+ char* line_;
+ int low_;
+ int high_;
+
+protected:
+
+ virtual int_type showmanyc() const { return high_ - low_; }
+
+ virtual streamsize xsgetn( char_type* buf, streamsize n ) {
+ int rd = n > (high_ - low_)? (high_ - low_) : n;
+ memcpy( buf, line_, rd );
+ low_ += rd;
+
+ if ( rd < n ) {
+ low_ = high_ = 0;
+ free( line_ ); // free( NULL ) is a noop
+ line_ = readline( prompt_ );
+ if ( line_ ) {
+ high_ = strlen( line_ );
+ if ( history_ && high_ ) add_history( line_ );
+ rd += xsgetn( buf + rd, n - rd );
+ }
+ }
+
+ return rd;
+ }
+
+ virtual int_type underflow() {
+ if ( high_ == low_ ) {
+ low_ = high_ = 0;
+ free( line_ ); // free( NULL ) is a noop
+ line_ = readline( prompt_ );
+ if ( line_ ) {
+ high_ = strlen( line_ );
+ if ( history_ && high_ ) add_history( line_ );
+ }
+ }
+
+ if ( low_ < high_ ) return line_[low_];
+ else return eof;
+ }
+
+ virtual int_type uflow() {
+ int_type c = underflow();
+ if ( c != eof ) ++low_;
+ return c;
+ }
+
+ virtual int_type pbackfail( int_type c = eof ) {
+ if ( low_ > 0 ) --low_;
+ else if ( c != eof ) {
+ if ( high_ > 0 ) {
+ char* nl = (char*)realloc( line_, high_ + 1 );
+ if ( nl ) {
+ line_ = (char*)memcpy( nl + 1, line_, high_ );
+ high_ += 1;
+ line_[0] = char( c );
+ } else return eof;
+ } else {
+ assert( !line_ );
+ line_ = (char*)malloc( sizeof( char ) );
+ *line_ = char( c );
+ high_ = 1;
+ }
+ } else return eof;
+
+ return not_eof;
+ }
+
+public:
+ readlinebuf( const char* prompt = NULL, bool history = true )
+ : prompt_( prompt ), history_( history ),
+ line_( NULL ), low_( 0 ), high_( 0 ) {
+ setbuf( 0, 0 );
+ }
+
+
+};
+
+#endif
diff --git a/readline/examples/rl.c b/readline/examples/rl.c
index 2d1d17e600d..d2604895e52 100644
--- a/readline/examples/rl.c
+++ b/readline/examples/rl.c
@@ -5,6 +5,26 @@
* usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
*/
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
+
+ This file is part of the GNU Readline Library, a library for
+ reading lines of text with interactive input and history editing.
+
+ The GNU Readline Library 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.
+
+ The GNU Readline Library 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.
+
+ The GNU General Public License is often shipped with GNU software, and
+ is generally kept in a file called COPYING or LICENSE. If you do not
+ have a copy of the license, write to the Free Software Foundation,
+ 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
#if defined (HAVE_CONFIG_H)
# include <config.h>
#endif
@@ -38,7 +58,7 @@ set_deftext ()
{
rl_insert_text (deftext);
deftext = (char *)NULL;
- rl_startup_hook = (Function *)NULL;
+ rl_startup_hook = (rl_hook_func_t *)NULL;
}
return 0;
}
@@ -126,6 +146,6 @@ main (argc, argv)
if (temp == 0)
exit (1);
- puts (temp);
+ printf ("%s\n", temp);
exit (0);
}
diff --git a/readline/examples/rlcat.c b/readline/examples/rlcat.c
new file mode 100644
index 00000000000..176b9f44b68
--- /dev/null
+++ b/readline/examples/rlcat.c
@@ -0,0 +1,174 @@
+/*
+ * rlcat - cat(1) using readline
+ *
+ * usage: rlcat
+ */
+
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
+
+ This file is part of the GNU Readline Library, a library for
+ reading lines of text with interactive input and history editing.
+
+ The GNU Readline Library 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.
+
+ The GNU Readline Library 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.
+
+ The GNU General Public License is often shipped with GNU software, and
+ is generally kept in a file called COPYING or LICENSE. If you do not
+ have a copy of the license, write to the Free Software Foundation,
+ 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
+#if defined (HAVE_CONFIG_H)
+# include <config.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <sys/types.h>
+#include "posixstat.h"
+
+#include <stdio.h>
+#include <ctype.h>
+#include <string.h>
+#include <errno.h>
+
+#ifndef errno
+extern int errno;
+#endif
+
+#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;
+
+static int stdcat();
+
+static char *progname;
+static int vflag;
+
+static void
+usage()
+{
+ fprintf (stderr, "%s: usage: %s [-vEVN] [filename]\n", progname, progname);
+}
+
+int
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ char *temp;
+ int opt, Vflag, Nflag;
+
+ progname = strrchr(argv[0], '/');
+ if (progname == 0)
+ progname = argv[0];
+ else
+ progname++;
+
+ vflag = Vflag = Nflag = 0;
+ while ((opt = getopt(argc, argv, "vEVN")) != EOF)
+ {
+ switch (opt)
+ {
+ case 'v':
+ vflag = 1;
+ break;
+ case 'V':
+ Vflag = 1;
+ break;
+ case 'E':
+ Vflag = 0;
+ break;
+ case 'N':
+ Nflag = 1;
+ break;
+ default:
+ usage ();
+ exit (2);
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (isatty(0) == 0 || argc || Nflag)
+ return stdcat(argc, argv);
+
+ rl_variable_bind ("editing-mode", Vflag ? "vi" : "emacs");
+ while (temp = readline (""))
+ {
+ if (*temp)
+ add_history (temp);
+ printf ("%s\n", temp);
+ }
+
+ return (ferror (stdout));
+}
+
+static int
+fcopy(fp)
+ FILE *fp;
+{
+ int c;
+ char *x;
+
+ while ((c = getc(fp)) != EOF)
+ {
+ if (vflag && isascii ((unsigned char)c) && isprint((unsigned char)c) == 0)
+ {
+ x = rl_untranslate_keyseq (c);
+ if (fputs (x, stdout) != 0)
+ return 1;
+ }
+ else if (putchar (c) == EOF)
+ return 1;
+ }
+ return (ferror (stdout));
+}
+
+int
+stdcat (argc, argv)
+ int argc;
+ char **argv;
+{
+ int i, fd, r;
+ char *s;
+ FILE *fp;
+
+ if (argc == 0)
+ return (fcopy(stdin));
+
+ for (i = 0, r = 1; i < argc; i++)
+ {
+ if (*argv[i] == '-' && argv[i][1] == 0)
+ fp = stdin;
+ else
+ {
+ fp = fopen (argv[i], "r");
+ if (fp == 0)
+ {
+ fprintf (stderr, "%s: %s: cannot open: %s\n", progname, argv[i], strerror(errno));
+ continue;
+ }
+ }
+ r = fcopy (fp);
+ if (fp != stdin)
+ fclose(fp);
+ }
+ return r;
+}
diff --git a/readline/examples/rlfe.c b/readline/examples/rlfe.c
index 5c3c8fde45b..d634d7ce878 100644
--- a/readline/examples/rlfe.c
+++ b/readline/examples/rlfe.c
@@ -64,6 +64,8 @@
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
+#include <limits.h>
+#include <dirent.h>
#ifdef READLINE_LIBRARY
# include "readline.h"
@@ -81,6 +83,7 @@
#endif
#ifndef HAVE_MEMMOVE
+#ifndef memmove
# if __GNUC__ > 1
# define memmove(d, s, n) __builtin_memcpy(d, s, n)
# else
@@ -89,8 +92,19 @@
#else
# define memmove(d, s, n) memcpy(d, s, n)
#endif
+#endif
+
+#define APPLICATION_NAME "Rlfe"
+
+#ifndef errno
+extern int errno;
+#endif
-#define APPLICATION_NAME "Fep"
+extern int optind;
+extern char *optarg;
+
+static char *progname;
+static char *progversion;
static int in_from_inferior_fd;
static int out_to_inferior_fd;
@@ -110,13 +124,15 @@ char echo_suppress_buffer[ECHO_SUPPRESS_MAX];
int echo_suppress_start = 0;
int echo_suppress_limit = 0;
-#define DEBUG
+/* #define DEBUG */
+
+static FILE *logfile = NULL;
#ifdef DEBUG
-FILE *logfile = NULL;
-#define DPRINT0(FMT) (fprintf(logfile, FMT), fflush(logfile))
-#define DPRINT1(FMT, V1) (fprintf(logfile, FMT, V1), fflush(logfile))
-#define DPRINT2(FMT, V1, V2) (fprintf(logfile, FMT, V1, V2), fflush(logfile))
+FILE *debugfile = NULL;
+#define DPRINT0(FMT) (fprintf(debugfile, FMT), fflush(debugfile))
+#define DPRINT1(FMT, V1) (fprintf(debugfile, FMT, V1), fflush(debugfile))
+#define DPRINT2(FMT, V1, V2) (fprintf(debugfile, FMT, V1, V2), fflush(debugfile))
#else
#define DPRINT0(FMT) /* Do nothing */
#define DPRINT1(FMT, V1) /* Do nothing */
@@ -125,6 +141,10 @@ FILE *logfile = NULL;
struct termios orig_term;
+static int rlfe_directory_completion_hook __P((char **));
+static int rlfe_directory_rewrite_hook __P((char **));
+static char *rlfe_filename_completion_function __P((const char *, int));
+
/* Pid of child process. */
static pid_t child = -1;
@@ -370,13 +390,20 @@ my_rl_getc (FILE *dummy)
return ch;
}
+static void
+usage()
+{
+ fprintf (stderr, "%s: usage: %s [-l filename] [-a] [-n appname] [-hv] [command [arguments...]]\n",
+ progname, progname);
+}
+
int
main(int argc, char** argv)
{
char *path;
- int i;
+ int i, append;
int master;
- char *name;
+ char *name, *logfname, *appname;
int in_from_tty_fd;
struct sigaction act;
struct winsize ws;
@@ -387,12 +414,58 @@ main(int argc, char** argv)
char *prompt = empty_string;
int ioctl_err = 0;
+ if ((progname = strrchr (argv[0], '/')) == 0)
+ progname = argv[0];
+ else
+ progname++;
+ progversion = RL_LIBRARY_VERSION;
+
+ append = 0;
+ appname = APPLICATION_NAME;
+ logfname = (char *)NULL;
+
+ while ((i = getopt (argc, argv, "ahl:n:v")) != EOF)
+ {
+ switch (i)
+ {
+ case 'l':
+ logfname = optarg;
+ break;
+ case 'n':
+ appname = optarg;
+ break;
+ case 'a':
+ append = 1;
+ break;
+ case 'h':
+ usage ();
+ exit (0);
+ case 'v':
+ fprintf (stderr, "%s version %s\n", progname, progversion);
+ exit (0);
+ default:
+ usage ();
+ exit (2);
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (logfname)
+ {
+ logfile = fopen (logfname, append ? "a" : "w");
+ if (logfile == 0)
+ fprintf (stderr, "%s: warning: could not open log file %s: %s\n",
+ progname, logfname, strerror (errno));
+ }
+
+ rl_readline_name = appname;
+
#ifdef DEBUG
- logfile = fopen("LOG", "w");
+ debugfile = fopen("LOG", "w");
#endif
- rl_readline_name = APPLICATION_NAME;
-
if ((master = get_master_pty(&name)) < 0)
{
perror("ptypair: could not open master pty");
@@ -486,10 +559,10 @@ main(int argc, char** argv)
/* now start the shell */
{
static char* command_args[] = { COMMAND_ARGS, NULL };
- if (argc <= 1)
+ if (argc < 1)
execvp(COMMAND, command_args);
else
- execvp(argv[1], &argv[1]);
+ execvp(argv[0], &argv[0]);
}
/* should never be reached */
@@ -535,6 +608,13 @@ main(int argc, char** argv)
rl_deprep_term_function = null_deprep_terminal;
rl_callback_handler_install (prompt, line_handler);
+#if 1
+ rl_directory_completion_hook = rlfe_directory_completion_hook;
+ rl_completion_entry_function = rlfe_filename_completion_function;
+#else
+ rl_directory_rewrite_hook = rlfe_directory_rewrite_hook;
+#endif
+
in_from_tty_fd = STDIN_FILENO;
FD_ZERO (&in_set);
maxfd = in_from_inferior_fd > in_from_tty_fd ? in_from_inferior_fd
@@ -644,6 +724,47 @@ main(int argc, char** argv)
}
old_count = buf_count;
+ /* Do some minimal carriage return translation and backspace
+ processing before logging the input line. */
+ if (logfile)
+ {
+#ifndef __GNUC__
+ char *b;
+#else
+ char b[count + 1];
+#endif
+ int i, j;
+
+#ifndef __GNUC__
+ b = malloc (count + 1);
+ if (b) {
+#endif
+ for (i = 0; i < count; i++)
+ b[i] = buf[buf_count + i];
+ b[i] = '\0';
+ for (i = j = 0; i <= count; i++)
+ {
+ if (b[i] == '\r')
+ {
+ if (b[i+1] != '\n')
+ b[j++] = '\n';
+ }
+ else if (b[i] == '\b')
+ {
+ if (i)
+ j--;
+ }
+ else
+ b[j++] = b[i];
+ }
+ fprintf (logfile, "%s", b);
+
+#ifndef __GNUC__
+ free (b);
+ }
+#endif
+ }
+
/* Look for any pending echo that we need to suppress. */
while (echo_suppress_start < echo_suppress_limit
&& count > 0
@@ -683,3 +804,239 @@ main(int argc, char** argv)
}
}
}
+
+/*
+ *
+ * FILENAME COMPLETION FOR RLFE
+ *
+ */
+
+#ifndef PATH_MAX
+# define PATH_MAX 1024
+#endif
+
+#define DIRSEP '/'
+#define ISDIRSEP(x) ((x) == '/')
+#define PATHSEP(x) (ISDIRSEP(x) || (x) == 0)
+
+#define DOT_OR_DOTDOT(x) \
+ ((x)[0] == '.' && (PATHSEP((x)[1]) || \
+ ((x)[1] == '.' && PATHSEP((x)[2]))))
+
+#define FREE(x) if (x) free(x)
+
+#define STRDUP(s, x) do { \
+ s = strdup (x);\
+ if (s == 0) \
+ return ((char *)NULL); \
+ } while (0)
+
+static int
+get_inferior_cwd (path, psize)
+ char *path;
+ size_t psize;
+{
+ int n;
+ static char procfsbuf[PATH_MAX] = { '\0' };
+
+ if (procfsbuf[0] == '\0')
+ sprintf (procfsbuf, "/proc/%d/cwd", (int)child);
+ n = readlink (procfsbuf, path, psize);
+ if (n < 0)
+ return n;
+ if (n > psize)
+ return -1;
+ path[n] = '\0';
+ return n;
+}
+
+static int
+rlfe_directory_rewrite_hook (dirnamep)
+ char **dirnamep;
+{
+ char *ldirname, cwd[PATH_MAX], *retdir, *ld;
+ int n, ldlen;
+
+ ldirname = *dirnamep;
+
+ if (*ldirname == '/')
+ return 0;
+
+ n = get_inferior_cwd (cwd, sizeof(cwd) - 1);
+ if (n < 0)
+ return 0;
+ if (n == 0) /* current directory */
+ {
+ cwd[0] = '.';
+ cwd[1] = '\0';
+ n = 1;
+ }
+
+ /* Minimally canonicalize ldirname by removing leading `./' */
+ for (ld = ldirname; *ld; )
+ {
+ if (ISDIRSEP (ld[0]))
+ ld++;
+ else if (ld[0] == '.' && PATHSEP(ld[1]))
+ ld++;
+ else
+ break;
+ }
+ ldlen = (ld && *ld) ? strlen (ld) : 0;
+
+ retdir = (char *)malloc (n + ldlen + 3);
+ if (retdir == 0)
+ return 0;
+ if (ldlen)
+ sprintf (retdir, "%s/%s", cwd, ld);
+ else
+ strcpy (retdir, cwd);
+ free (ldirname);
+
+ *dirnamep = retdir;
+
+ DPRINT1("rl_directory_rewrite_hook returns %s\n", retdir);
+ return 1;
+}
+
+/* Translate *DIRNAMEP to be relative to the inferior's CWD. Leave a trailing
+ slash on the result. */
+static int
+rlfe_directory_completion_hook (dirnamep)
+ char **dirnamep;
+{
+ char *ldirname, *retdir;
+ int n, ldlen;
+
+ ldirname = *dirnamep;
+
+ if (*ldirname == '/')
+ return 0;
+
+ n = rlfe_directory_rewrite_hook (dirnamep);
+ if (n == 0)
+ return 0;
+
+ ldirname = *dirnamep;
+ ldlen = (ldirname && *ldirname) ? strlen (ldirname) : 0;
+
+ if (ldlen == 0 || ldirname[ldlen - 1] != '/')
+ {
+ retdir = (char *)malloc (ldlen + 3);
+ if (retdir == 0)
+ return 0;
+ if (ldlen)
+ strcpy (retdir, ldirname);
+ else
+ retdir[ldlen++] = '.';
+ retdir[ldlen] = '/';
+ retdir[ldlen+1] = '\0';
+ free (ldirname);
+
+ *dirnamep = retdir;
+ }
+
+ DPRINT1("rl_directory_completion_hook returns %s\n", retdir);
+ return 1;
+}
+
+static char *
+rlfe_filename_completion_function (text, state)
+ const char *text;
+ int state;
+{
+ static DIR *directory;
+ static char *filename = (char *)NULL;
+ static char *dirname = (char *)NULL, *ud = (char *)NULL;
+ static int flen, udlen;
+ char *temp;
+ struct dirent *dentry;
+
+ if (state == 0)
+ {
+ if (directory)
+ {
+ closedir (directory);
+ directory = 0;
+ }
+ FREE (dirname);
+ FREE (filename);
+ FREE (ud);
+
+ if (text && *text)
+ STRDUP (filename, text);
+ else
+ {
+ filename = malloc(1);
+ if (filename == 0)
+ return ((char *)NULL);
+ filename[0] = '\0';
+ }
+ dirname = (text && *text) ? strdup (text) : strdup (".");
+ if (dirname == 0)
+ return ((char *)NULL);
+
+ temp = strrchr (dirname, '/');
+ if (temp)
+ {
+ strcpy (filename, ++temp);
+ *temp = '\0';
+ }
+ else
+ {
+ dirname[0] = '.';
+ dirname[1] = '\0';
+ }
+
+ STRDUP (ud, dirname);
+ udlen = strlen (ud);
+
+ rlfe_directory_completion_hook (&dirname);
+
+ directory = opendir (dirname);
+ flen = strlen (filename);
+
+ rl_filename_completion_desired = 1;
+ }
+
+ dentry = 0;
+ while (directory && (dentry = readdir (directory)))
+ {
+ if (flen == 0)
+ {
+ if (DOT_OR_DOTDOT(dentry->d_name) == 0)
+ break;
+ }
+ else
+ {
+ if ((dentry->d_name[0] == filename[0]) &&
+ (strlen (dentry->d_name) >= flen) &&
+ (strncmp (filename, dentry->d_name, flen) == 0))
+ break;
+ }
+ }
+
+ if (dentry == 0)
+ {
+ if (directory)
+ {
+ closedir (directory);
+ directory = 0;
+ }
+ FREE (dirname);
+ FREE (filename);
+ FREE (ud);
+ dirname = filename = ud = 0;
+ return ((char *)NULL);
+ }
+
+ if (ud == 0 || (ud[0] == '.' && ud[1] == '\0'))
+ temp = strdup (dentry->d_name);
+ else
+ {
+ temp = malloc (1 + udlen + strlen (dentry->d_name));
+ strcpy (temp, ud);
+ strcpy (temp + udlen, dentry->d_name);
+ }
+ return (temp);
+}
diff --git a/readline/examples/rltest.c b/readline/examples/rltest.c
index 6250f900d42..99f083b2b9c 100644
--- a/readline/examples/rltest.c
+++ b/readline/examples/rltest.c
@@ -4,6 +4,26 @@
/* */
/* **************************************************************** */
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
+
+ This file is part of the GNU Readline Library, a library for
+ reading lines of text with interactive input and history editing.
+
+ The GNU Readline Library 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.
+
+ The GNU Readline Library 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.
+
+ The GNU General Public License is often shipped with GNU software, and
+ is generally kept in a file called COPYING or LICENSE. If you do not
+ have a copy of the license, write to the Free Software Foundation,
+ 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
#if defined (HAVE_CONFIG_H)
#include <config.h>
#endif
diff --git a/readline/examples/rlversion.c b/readline/examples/rlversion.c
index 652d37ccb88..53949d1e3b7 100644
--- a/readline/examples/rlversion.c
+++ b/readline/examples/rlversion.c
@@ -2,6 +2,26 @@
* rlversion -- print out readline's version number
*/
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
+
+ This file is part of the GNU Readline Library, a library for
+ reading lines of text with interactive input and history editing.
+
+ The GNU Readline Library 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.
+
+ The GNU Readline Library 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.
+
+ The GNU General Public License is often shipped with GNU software, and
+ is generally kept in a file called COPYING or LICENSE. If you do not
+ have a copy of the license, write to the Free Software Foundation,
+ 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
#if defined (HAVE_CONFIG_H)
# include <config.h>
#endif