diff options
Diffstat (limited to 'readline/examples')
-rw-r--r-- | readline/examples/Makefile.in | 36 | ||||
-rw-r--r-- | readline/examples/histexamp.c | 118 | ||||
-rw-r--r-- | readline/examples/rl.c | 9 | ||||
-rw-r--r-- | readline/examples/rlcat.c | 180 | ||||
-rw-r--r-- | readline/examples/rltest.c | 6 | ||||
-rw-r--r-- | readline/examples/rlversion.c | 6 |
6 files changed, 282 insertions, 73 deletions
diff --git a/readline/examples/Makefile.in b/readline/examples/Makefile.in index 72c9904ff0f..e27bbc0cf6a 100644 --- a/readline/examples/Makefile.in +++ b/readline/examples/Makefile.in @@ -40,6 +40,8 @@ INCLUDES = -I$(srcdir) -I$(top_srcdir) -I.. CCFLAGS = $(DEFS) $(LOCAL_CFLAGS) $(CPPFLAGS) $(INCLUDES) $(CFLAGS) LDFLAGS = -g -L.. @LDFLAGS@ +PURIFY = @PURIFY@ + READLINE_LIB = ../libreadline.a HISTORY_LIB = ../libhistory.a @@ -50,33 +52,35 @@ TERMCAP_LIB = @TERMCAP_LIB@ $(CC) $(CCFLAGS) -c $< EXECUTABLES = fileman rltest rl rlcat rlversion histexamp -OBJECTS = fileman.o rltest.o rl.o rlversion.o histexamp.o +OBJECTS = fileman.o rltest.o rl.o rlcat.o rlversion.o histexamp.o all: $(EXECUTABLES) -everything: all rlfe +everything: all rl: rl.o $(READLINE_LIB) - $(CC) $(LDFLAGS) -o $@ rl.o -lreadline $(TERMCAP_LIB) + $(PURIFY) $(CC) $(LDFLAGS) -o $@ rl.o $(READLINE_LIB) $(TERMCAP_LIB) rlcat: rlcat.o $(READLINE_LIB) - $(CC) $(LDFLAGS) -o $@ rlcat.o -lreadline $(TERMCAP_LIB) + $(PURIFY) $(CC) $(LDFLAGS) -o $@ rlcat.o $(READLINE_LIB) $(TERMCAP_LIB) fileman: fileman.o $(READLINE_LIB) - $(CC) $(LDFLAGS) -o $@ fileman.o -lreadline $(TERMCAP_LIB) + $(PURIFY) $(CC) $(LDFLAGS) -o $@ fileman.o $(READLINE_LIB) $(TERMCAP_LIB) rltest: rltest.o $(READLINE_LIB) - $(CC) $(LDFLAGS) -o $@ rltest.o -lreadline $(TERMCAP_LIB) + $(PURIFY) $(CC) $(LDFLAGS) -o $@ rltest.o $(READLINE_LIB) $(TERMCAP_LIB) + +rlptytest: rlptytest.o $(READLINE_LIB) + $(PURIFY) $(CC) $(LDFLAGS) -o $@ rlptytest.o $(READLINE_LIB) $(TERMCAP_LIB) rlversion: rlversion.o $(READLINE_LIB) - $(CC) $(LDFLAGS) -o $@ rlversion.o -lreadline $(TERMCAP_LIB) + $(CC) $(LDFLAGS) -o $@ rlversion.o $(READLINE_LIB) $(TERMCAP_LIB) histexamp: histexamp.o $(HISTORY_LIB) - $(CC) $(LDFLAGS) -o $@ histexamp.o -lhistory $(TERMCAP_LIB) + $(PURIFY) $(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 @@ -86,19 +90,13 @@ rltest.o: rltest.c rl.o: rl.c rlversion.o: rlversion.c histexamp.o: histexamp.c +rlcat.o: rlcat.c +rlptytest.o: rlptytest.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 - -rlfe: rlfe.o $(READLINE_LIB) $(HISTORY_LIB) - $(CC) $(LDFLAGS) -o $@ rlfe.o -lreadline -lhistory ${TERMCAP_LIB} - -rlfe.o: rlfe.c - -rlfe.o: $(top_srcdir)/readline.h -rlfe.o: $(top_srcdir)/history.h +rlcat.o: $(top_srcdir)/readline.h $(top_srcdir)/history.h +rlptytest.o: $(top_srcdir)/readline.h $(top_srcdir)/history.h diff --git a/readline/examples/histexamp.c b/readline/examples/histexamp.c index 45651dfb137..4f059c17cc3 100644 --- a/readline/examples/histexamp.c +++ b/readline/examples/histexamp.c @@ -26,14 +26,17 @@ # include <readline/history.h> #endif +#include <string.h> + main (argc, argv) int argc; char **argv; { char line[1024], *t; - int len, done = 0; + int len, done; line[0] = 0; + done = 0; using_history (); while (!done) @@ -42,71 +45,80 @@ main (argc, argv) fflush (stdout); t = fgets (line, sizeof (line) - 1, stdin); if (t && *t) - { - len = strlen (t); - if (t[len - 1] == '\n') - t[len - 1] = '\0'; - } + { + len = strlen (t); + if (t[len - 1] == '\n') + t[len - 1] = '\0'; + } if (!t) - strcpy (line, "quit"); + strcpy (line, "quit"); if (line[0]) - { - char *expansion; - int result; + { + char *expansion; + int result; - using_history (); + using_history (); - result = history_expand (line, &expansion); - if (result) - fprintf (stderr, "%s\n", expansion); + result = history_expand (line, &expansion); + if (result) + fprintf (stderr, "%s\n", expansion); - if (result < 0 || result == 2) - { - free (expansion); - continue; - } + if (result < 0 || result == 2) + { + free (expansion); + continue; + } - add_history (expansion); - strncpy (line, expansion, sizeof (line) - 1); - free (expansion); - } + add_history (expansion); + strncpy (line, expansion, sizeof (line) - 1); + free (expansion); + } if (strcmp (line, "quit") == 0) - done = 1; + done = 1; else if (strcmp (line, "save") == 0) - write_history ("history_file"); + write_history ("history_file"); else if (strcmp (line, "read") == 0) - read_history ("history_file"); + read_history ("history_file"); else if (strcmp (line, "list") == 0) - { - register HIST_ENTRY **the_list; - register int i; - - the_list = history_list (); - if (the_list) - for (i = 0; the_list[i]; i++) - printf ("%d: %s\n", i + history_base, the_list[i]->line); - } + { + register HIST_ENTRY **the_list; + register int i; + time_t tt; + char timestr[128]; + + the_list = history_list (); + if (the_list) + for (i = 0; the_list[i]; i++) + { + tt = history_get_time (the_list[i]); + if (tt) + strftime (timestr, sizeof (timestr), "%a %R", localtime(&tt)); + else + strcpy (timestr, "??"); + printf ("%d: %s: %s\n", i + history_base, timestr, the_list[i]->line); + } + } else if (strncmp (line, "delete", 6) == 0) - { - int which; - if ((sscanf (line + 6, "%d", &which)) == 1) - { - HIST_ENTRY *entry = remove_history (which); - if (!entry) - fprintf (stderr, "No such entry %d\n", which); - else - { - free (entry->line); - free (entry); - } - } - else - { - fprintf (stderr, "non-numeric arg given to `delete'\n"); - } - } + { + int which; + if ((sscanf (line + 6, "%d", &which)) == 1) + { + HIST_ENTRY *entry = remove_history (which); + if (!entry) + fprintf (stderr, "No such entry %d\n", which); + else + { + free (entry->line); + free (entry); + } + } + else + { + fprintf (stderr, "non-numeric arg given to `delete'\n"); + } + } } } diff --git a/readline/examples/rl.c b/readline/examples/rl.c index d2604895e52..c608c15f3d6 100644 --- a/readline/examples/rl.c +++ b/readline/examples/rl.c @@ -31,12 +31,19 @@ #include <stdio.h> #include <sys/types.h> -#include "posixstat.h" + +#ifdef HAVE_STDLIB_H +# include <stdlib.h> +#else +extern void exit(); +#endif #if defined (READLINE_LIBRARY) +# include "posixstat.h" # include "readline.h" # include "history.h" #else +# include <sys/stat.h> # include <readline/readline.h> # include <readline/history.h> #endif diff --git a/readline/examples/rlcat.c b/readline/examples/rlcat.c new file mode 100644 index 00000000000..33aea4a3007 --- /dev/null +++ b/readline/examples/rlcat.c @@ -0,0 +1,180 @@ +/* + * 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> + +#ifdef HAVE_STDLIB_H +# include <stdlib.h> +#else +extern void exit(); +#endif + +#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/rltest.c b/readline/examples/rltest.c index 99f083b2b9c..cb67bab80c5 100644 --- a/readline/examples/rltest.c +++ b/readline/examples/rltest.c @@ -31,6 +31,12 @@ #include <stdio.h> #include <sys/types.h> +#ifdef HAVE_STDLIB_H +# include <stdlib.h> +#else +extern void exit(); +#endif + #ifdef READLINE_LIBRARY # include "readline.h" # include "history.h" diff --git a/readline/examples/rlversion.c b/readline/examples/rlversion.c index 53949d1e3b7..6c8687488e9 100644 --- a/readline/examples/rlversion.c +++ b/readline/examples/rlversion.c @@ -30,6 +30,12 @@ #include <sys/types.h> #include "posixstat.h" +#ifdef HAVE_STDLIB_H +# include <stdlib.h> +#else +extern void exit(); +#endif + #ifdef READLINE_LIBRARY # include "readline.h" #else |