updates for several readline examples; update examples in readline documentation; update documentation for --rcfile option

This commit is contained in:
Chet Ramey
2024-07-02 14:34:40 -04:00
parent 5e28a1813c
commit dbff8b786e
10 changed files with 100 additions and 102 deletions
+15 -13
View File
@@ -1526,16 +1526,8 @@ invert_case_line (count, key)
if (rl_point >= rl_end)
return (0);
if (count < 0)
@{
direction = -1;
count = -count;
@}
else
direction = 1;
/* Find the end of the range to modify. */
end = start + (count * direction);
end = start + count;
/* Force it to be within range. */
if (end > rl_end)
@@ -1546,6 +1538,11 @@ invert_case_line (count, key)
if (start == end)
return (0);
/* For positive arguments, put point after the last changed character. For
negative arguments, put point before the last changed character. */
rl_point = end;
/* Swap start and end if we are moving backwards */
if (start > end)
@{
int temp = start;
@@ -1564,8 +1561,7 @@ invert_case_line (count, key)
else if (_rl_lowercase_p (rl_line_buffer[i]))
rl_line_buffer[i] = _rl_to_upper (rl_line_buffer[i]);
@}
/* Move point to on top of the last character changed. */
rl_point = (direction == 1) ? end - 1 : start;
return (0);
@}
@end example
@@ -1583,7 +1579,6 @@ It understands the EOF character or "exit" to exit the program.
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <locale.h>
/* Used for select(2) */
#include <sys/types.h>
@@ -1591,12 +1586,19 @@ It understands the EOF character or "exit" to exit the program.
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <locale.h>
/* Standard readline include files. */
#include <readline/readline.h>
#include <readline/history.h>
#if !defined (errno)
extern int errno;
#endif
static void cb_linehandler (char *);
static void sighandler (int);
@@ -1664,7 +1666,7 @@ main (int c, char **v)
while (running)
@{
FD_ZERO (&fds);
FD_SET (fileno (rl_instream), &fds);
FD_SET (fileno (rl_instream), &fds);
r = select (FD_SETSIZE, &fds, NULL, NULL, NULL);
if (r < 0 && errno != EINTR)
+8 -17
View File
@@ -54,8 +54,10 @@ Copyright (C) 1999 Jeff Solomon
#ifdef READLINE_LIBRARY
# include "readline.h"
# include "history.h"
#else
# include <readline/readline.h>
# include <readline/history.h>
#endif
#ifndef STDIN_FILENO
@@ -92,7 +94,7 @@ Copyright (C) 1999 Jeff Solomon
*/
void process_line(char *line);
int change_prompt(void);
int change_prompt(int, int);
char *get_prompt(void);
int prompt = 1;
@@ -101,7 +103,7 @@ tcflag_t old_lflag;
cc_t old_vtime;
struct termios term;
int
int
main(int c, char **v)
{
fd_set fds;
@@ -170,31 +172,20 @@ process_line(char *line)
}
int
change_prompt(void)
change_prompt(int count, int key)
{
/* 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);
rl_set_prompt (get_prompt ());
return 0;
}
char *
get_prompt(void)
{
/* The prompts can even be different lengths! */
sprintf(prompt_buf, "%s",
sprintf(prompt_buf, "%s",
prompt ? "Hit ctrl-t to toggle prompt> " : "Pretty cool huh?> ");
return prompt_buf;
}
+7 -2
View File
@@ -23,12 +23,15 @@
# include <readline/history.h>
#endif
#if !defined (errno)
extern int errno;
#endif
static void cb_linehandler (char *);
static void signandler (int);
int running, sigwinch_received;
int running;
int sigwinch_received;
const char *prompt = "rltest$ ";
/* Handle SIGWINCH and window size changes when readline is not active and
@@ -73,9 +76,11 @@ main (int c, char **v)
fd_set fds;
int r;
/* Set the default locale values according to environment variables. */
setlocale (LC_ALL, "");
/* Handle SIGWINCH */
/* Handle window size changes when readline is not active and reading
characters. */
signal (SIGWINCH, sighandler);
/* Install the line handler. */
+16 -12
View File
@@ -6,13 +6,14 @@
/* Used for select(2) */
#include <sys/types.h>
#include <sys/select.h>
#include <errno.h>
#include <signal.h>
#include <locale.h>
#include <errno.h>
#include <stdio.h>
#include <locale.h>
/* Standard readline include files. */
#if defined (READLINE_LIBRARY)
# include "readline.h"
@@ -22,6 +23,10 @@
# include <readline/history.h>
#endif
#if !defined (errno)
extern int errno;
#endif
static void cb_linehandler (char *);
static void sigint_sighandler (int);
static int sigint_handler (int);
@@ -51,18 +56,18 @@ cb_readline (void)
fd_set fds;
int r, err;
char *not_done = "";
/* Install the line handler. */
rl_callback_handler_install (prompt, cb_linehandler);
if (RL_ISSTATE (RL_STATE_ISEARCH))
fprintf(stderr, "cb_readline: after handler install, state (ISEARCH) = %d", rl_readline_state);
else if (RL_ISSTATE (RL_STATE_NSEARCH))
fprintf(stderr, "cb_readline: after handler install, state (NSEARCH) = %d", rl_readline_state);
/* MULTIKEY VIMOTION NUMERICARG _rl_callback_func */
if (RL_ISSTATE (RL_STATE_ISEARCH))
fprintf(stderr, "cb_readline: after handler install, state (ISEARCH) = %lu", rl_readline_state);
else if (RL_ISSTATE (RL_STATE_NSEARCH))
fprintf(stderr, "cb_readline: after handler install, state (NSEARCH) = %lu", rl_readline_state);
/* MULTIKEY VIMOTION NUMERICARG _rl_callback_func */
FD_ZERO (&fds);
FD_SET (fileno (rl_instream), &fds);
FD_SET (fileno (rl_instream), &fds);
input_string = not_done;
@@ -76,10 +81,10 @@ else if (RL_ISSTATE (RL_STATE_NSEARCH))
while (r == 0)
{
struct timeval timeout = {0, 100000};
struct timeval *timeoutp = NULL;
struct timeval *timeoutp = NULL;
timeoutp = &timeout;
FD_SET (fileno (rl_instream), &fds);
FD_SET (fileno (rl_instream), &fds);
r = select (FD_SETSIZE, &fds, NULL, NULL, timeoutp);
err = errno;
}
@@ -115,7 +120,6 @@ sigint_handler (int s)
rl_cleanup_after_signal ();
rl_callback_handler_remove ();
saw_signal = 0;
fprintf(stderr, "sigint_handler: readline state = %d\r\n", rl_readline_state);
return s;
}
+7 -1
View File
@@ -19,6 +19,7 @@
#include <signal.h>
/* Need a configure check here to turn this into a real application. */
#if 1 /* LINUX */
#include <pty.h>
#else
@@ -31,10 +32,14 @@
#ifdef READLINE_LIBRARY
# include "readline.h"
# include "history.h"
#else
# include <readline/readline.h>
# include <readline/history.h>
#endif
int tty_reset(int fd);
/**
* Master/Slave PTY used to keep readline off of stdin/stdout.
*/
@@ -301,7 +306,8 @@ tty_off_xon_xoff (int fd)
*
* Returns: 0 on success, -1 on error
*/
int tty_reset(int fd)
int
tty_reset(int fd)
{
if(ttystate != TCBREAK)
return (0);