summaryrefslogtreecommitdiff
path: root/readline/examples
diff options
context:
space:
mode:
authorElena Zannoni <ezannoni@kwikemart.cygnus.com>2002-08-23 22:02:32 +0000
committerElena Zannoni <ezannoni@kwikemart.cygnus.com>2002-08-23 22:02:32 +0000
commit2ee563b53258d390d7446e90a67f465d504ae44c (patch)
tree9360bcf55ff3b872ccf5de2a4d82c7e2fbb873c9 /readline/examples
parent3e6564e1ffbf12b7d15e1124139c02408f054007 (diff)
parent84041b4c47edb0461f3b82afb77ca2d81819ebfa (diff)
This commit was generated by cvs2svn to track changes on a CVS vendor
branch.
Diffstat (limited to 'readline/examples')
-rw-r--r--readline/examples/excallback.c2
-rw-r--r--readline/examples/readlinebuf.h139
-rw-r--r--readline/examples/rlcat.c174
-rw-r--r--readline/examples/rlfe.c383
4 files changed, 685 insertions, 13 deletions
diff --git a/readline/examples/excallback.c b/readline/examples/excallback.c
index ca03fc3daf..3d4bb189c6 100644
--- a/readline/examples/excallback.c
+++ b/readline/examples/excallback.c
@@ -153,6 +153,8 @@ process_line(char *line)
} else {
fprintf(stderr, "|%s|\n", line);
}
+
+ free (line);
}
int
diff --git a/readline/examples/readlinebuf.h b/readline/examples/readlinebuf.h
new file mode 100644
index 0000000000..91ef4d64b8
--- /dev/null
+++ b/readline/examples/readlinebuf.h
@@ -0,0 +1,139 @@
+/*******************************************************************************
+ * $Revision$
+ * $Date$
+ * $Author$
+ *
+ * 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/rlcat.c b/readline/examples/rlcat.c
new file mode 100644
index 0000000000..176b9f44b6
--- /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 5c3c8fde45..d634d7ce87 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);
+}