Debian Codegraph @

eg/excallback

raw
1 #!/usr/bin/env perl
2 #
3 # excallback: Another test harness for the readline callback interface.
4 #
5 # Copyright (C) 2024 Hiroo Hayashi
6 #
7 # Derived from: examples/excallback.c in the GNU Readline Library
8 # Author: Jeff Solomon <[email protected]>
9
10 # This little examples demonstrates the alternate interface to using readline.
11 # In the alternate interface, the user maintains control over program flow and
12 # only calls readline when STDIN is readable. Using the alternate interface,
13 # you can do anything else while still using readline (like talking to a
14 # network or another program) without blocking.
15 #
16 # Specifically, this program highlights two importants features of the
17 # alternate interface. The first is the ability to interactively change the
18 # prompt, which can't be done using the regular interface since rl_prompt is
19 # read-only.
20 #
21 # The second feature really highlights a subtle point when using the alternate
22 # interface. That is, readline will not alter the terminal when inside your
23 # callback handler. So let's so, your callback executes a user command that
24 # takes a non-trivial amount of time to complete (seconds). While your
25 # executing the command, the user continues to type keystrokes and expects them
26 # to be re-echoed on the new prompt when it returns. Unfortunately, the default
27 # terminal configuration doesn't do this. After the prompt returns, the user
28 # must hit one additional keystroke and then will see all of his previous
29 # keystrokes. To illustrate this, compile and run this program. Type "sleep" at
30 # the prompt and then type "bar" before the prompt returns (you have 3
31 # seconds). Notice how "bar" is re-echoed on the prompt after the prompt
32 # returns? This is what you expect to happen. Now comment out the 4 lines below
33 # the line that says COMMENT LINE BELOW. Recompile and rerun the program and do
34 # the same thing. When the prompt returns, you should not see "bar". Now type
35 # "f", see how "barf" magically appears? This behavior is un-expected and not
36 # desired.
37
38 use strict;
39 use warnings;
40 use Term::ReadLine;
41 use IO::Pty;
42 use POSIX qw(termios_h _POSIX_VDISABLE);
43
44 my $t = new Term::ReadLine 'rlptytest';
45 my $a = $t->Attribs;
46
47 my $prompt = 1;
48 my $old_lflag;
49 my $old_vtime;
50 my $term;
51
52 # main program
53
54 sub main {
55 # Adjust the terminal slightly before the handler is installed. Disable
56 # canonical mode processing and set the input character time flag to be
57 # non-blocking.
58 $term = POSIX::Termios->new;
59 if (!defined($term->getattr(fileno(STDIN)))) {
60 die "tcgetattr: $!\n";
61 }
62 $old_lflag = $term->getlflag();
63 $old_vtime = $term->getcc(VTIME);
64 $term->setlflag($old_lflag & ~ICANON);
65 $term->setcc(1, VTIME);
66
67 # COMMENT LINE BELOW - see above
68 if (!defined($term->setattr(fileno(STDIN), TCSANOW))) {
69 die "tcsetattr: $!\n";
70 }
71
72 $t->add_defun("change-prompt", \&change_prompt, ord "\cT");
73 $t->callback_handler_install(get_prompt(), \&process_line);
74 while (1) {
75 my $fds = '';
76 vec($fds, fileno(STDIN), 1) = 1;
77 if (select($fds, undef, undef, undef) < 0) {
78 die "select: $!\n";
79 }
80 $t->callback_read_char() if (vec($fds, fileno(STDIN), 1));
81 }
82 exit 0;
83 }
84
85 sub process_line {
86 my ($line) = @_;
87 if (!$line) {
88 printf STDERR "\n";
89
90 # reset the old terminal setting before exiting
91 $term->setlflag($old_lflag);
92 $term->setcc($old_vtime, VTIME);
93 if (!defined($term->setattr(fileno(STDIN), TCSANOW))) {
94 die "tcsetattr: $!\n";
95 }
96 exit(0);
97 }
98 if ($line eq "sleep") {
99 sleep(3);
100 } else {
101 print STDERR "|$line|\n";
102 }
103 }
104 sub change_prompt {
105 # toggle the prompt variable
106 $prompt = !$prompt;
107 $t->set_prompt(get_prompt());
108 }
109
110 # The original implementation of change_prompt before rl_set_prompt was introduced.
111 sub change_prompt_original {
112 # toggle the prompt variable
113 $prompt = !$prompt;
114
115 # save away the current contents of the line
116 my $line_buf = $a->{line_buffer};
117
118 # install a new handler which will change the prompt and erase the current line
119 $t->callback_handler_install(get_prompt(), \&process_line);
120
121 # insert the old text on the new line
122 $t->insert_text($line_buf);
123
124 # redraw the current line - this is an undocumented function. It invokes the
125 # redraw-current-line command.
126 # $t->refresh_line(0, 0);
127 # $t->forced_update_display();
128 # $t->reset_line_state();
129 $t->redisplay();
130 }
131
132 sub get_prompt {
133 # The prompts can even be different lengths!
134 return $prompt ? "Hit ctrl-t to toggle prompt> " : "Pretty cool huh?> ";
135 }
136
137 main();