Annotation of embedaddon/readline/examples/rlevent.c, revision 1.1
1.1 ! misho 1: /*
! 2: * rl - command-line interface to read a line from the standard input
! 3: * (or another fd) using readline.
! 4: *
! 5: * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
! 6: */
! 7:
! 8: /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
! 9:
! 10: This file is part of the GNU Readline Library (Readline), a library for
! 11: reading lines of text with interactive input and history editing.
! 12:
! 13: Readline is free software: you can redistribute it and/or modify
! 14: it under the terms of the GNU General Public License as published by
! 15: the Free Software Foundation, either version 3 of the License, or
! 16: (at your option) any later version.
! 17:
! 18: Readline is distributed in the hope that it will be useful,
! 19: but WITHOUT ANY WARRANTY; without even the implied warranty of
! 20: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 21: GNU General Public License for more details.
! 22:
! 23: You should have received a copy of the GNU General Public License
! 24: along with Readline. If not, see <http://www.gnu.org/licenses/>.
! 25: */
! 26:
! 27: #if defined (HAVE_CONFIG_H)
! 28: # include <config.h>
! 29: #endif
! 30:
! 31: #include <stdio.h>
! 32: #include <sys/types.h>
! 33:
! 34: #ifdef HAVE_STDLIB_H
! 35: # include <stdlib.h>
! 36: #else
! 37: extern void exit();
! 38: #endif
! 39:
! 40: #if defined (READLINE_LIBRARY)
! 41: # include "posixstat.h"
! 42: # include "readline.h"
! 43: # include "history.h"
! 44: #else
! 45: # include <sys/stat.h>
! 46: # include <readline/readline.h>
! 47: # include <readline/history.h>
! 48: #endif
! 49:
! 50: extern int optind;
! 51: extern char *optarg;
! 52:
! 53: #if !defined (strchr) && !defined (__STDC__)
! 54: extern char *strrchr();
! 55: #endif
! 56:
! 57: static char *progname;
! 58: static char *deftext;
! 59:
! 60: static int
! 61: event_hook ()
! 62: {
! 63: fprintf (stderr, "ding!\n");
! 64: sleep (1);
! 65: return 0;
! 66: }
! 67:
! 68: static int
! 69: set_deftext ()
! 70: {
! 71: if (deftext)
! 72: {
! 73: rl_insert_text (deftext);
! 74: deftext = (char *)NULL;
! 75: rl_startup_hook = (rl_hook_func_t *)NULL;
! 76: }
! 77: return 0;
! 78: }
! 79:
! 80: static void
! 81: usage()
! 82: {
! 83: fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
! 84: progname, progname);
! 85: }
! 86:
! 87: int
! 88: main (argc, argv)
! 89: int argc;
! 90: char **argv;
! 91: {
! 92: char *temp, *prompt;
! 93: struct stat sb;
! 94: int opt, fd, nch;
! 95: FILE *ifp;
! 96:
! 97: progname = strrchr(argv[0], '/');
! 98: if (progname == 0)
! 99: progname = argv[0];
! 100: else
! 101: progname++;
! 102:
! 103: /* defaults */
! 104: prompt = "readline$ ";
! 105: fd = nch = 0;
! 106: deftext = (char *)0;
! 107:
! 108: while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
! 109: {
! 110: switch (opt)
! 111: {
! 112: case 'p':
! 113: prompt = optarg;
! 114: break;
! 115: case 'u':
! 116: fd = atoi(optarg);
! 117: if (fd < 0)
! 118: {
! 119: fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
! 120: exit (2);
! 121: }
! 122: break;
! 123: case 'd':
! 124: deftext = optarg;
! 125: break;
! 126: case 'n':
! 127: nch = atoi(optarg);
! 128: if (nch < 0)
! 129: {
! 130: fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
! 131: exit (2);
! 132: }
! 133: break;
! 134: default:
! 135: usage ();
! 136: exit (2);
! 137: }
! 138: }
! 139:
! 140: if (fd != 0)
! 141: {
! 142: if (fstat (fd, &sb) < 0)
! 143: {
! 144: fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
! 145: exit (1);
! 146: }
! 147: ifp = fdopen (fd, "r");
! 148: rl_instream = ifp;
! 149: }
! 150:
! 151: if (deftext && *deftext)
! 152: rl_startup_hook = set_deftext;
! 153:
! 154: if (nch > 0)
! 155: rl_num_chars_to_read = nch;
! 156:
! 157: rl_event_hook = event_hook;
! 158: temp = readline (prompt);
! 159:
! 160: /* Test for EOF. */
! 161: if (temp == 0)
! 162: exit (1);
! 163:
! 164: printf ("%s\n", temp);
! 165: exit (0);
! 166: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>