Debian Codegraph @

eg/rl-callbacktest2

raw
1 #!/usr/bin/env perl
2 #
3 # rl-callbacktest2: Provides readline()-like interface using the alternate interface
4 #
5 # Copyright (C) 2024 Hiroo Hayashi
6 #
7 # Derived from: examples/rl-callbacktest2.c in the GNU Readline Library
8
9 use strict;
10 use warnings;
11 use Term::ReadLine;
12
13 BEGIN {
14 import Term::ReadLine::Gnu qw(RL_STATE_ISEARCH RL_STATE_NSEARCH);
15 }
16
17 my $t = new Term::ReadLine 'rl-callbacktest2';
18 my $a = $t->Attribs;
19
20 my $saw_signal = 0;
21
22 my $running = 0;
23 my $prompt = 'rltest$ ';
24 my $input_string;
25
26 # Callback function called for each line when accept-line executed, EOF
27 # seen, or EOF character read. This sets a flag and returns; it could
28 # also call exit(3).
29 sub cb_linehandler {
30 my $line = shift;
31 $t->add_history($line) if $line;
32 print "input line: ", ($line or ""), "\n";
33 $input_string = $line;
34 $t->callback_handler_remove();
35 }
36
37 sub cb_readline {
38 my $not_done = "";
39
40 # Install the line handler.
41 $t->callback_handler_install($prompt, \&cb_linehandler);
42
43 # printf STDERR ("readline_state = 0x%lx\n", $a->{readline_state});
44 if ($t->ISSTATE(RL_STATE_ISEARCH)) {
45 printf STDERR (
46 "cb_readline: after handler install, state (ISEARCH) = %lx\n",
47 $a->{readline_state}
48 );
49 } elsif ($t->ISSTATE(RL_STATE_NSEARCH)) {
50 printf STDERR (
51 "cb_readline: after handler install, state (NSEARCH) = %lx\n",
52 $a->{readline_state}
53 );
54 }
55
56 # MULTIKEY VIMOTION NUMERICARG _rl_callback_func
57
58 my $fds = '';
59 vec($fds, fileno($a->{instream}), 1) = 1;
60
61 $input_string = $not_done;
62
63 while (defined($input_string) and $input_string eq $not_done) {
64 my $r = 0;
65 my $err = 0;
66
67 # Enter a simple event loop. This waits until something is available
68 # to read on readline's input stream (defaults to standard input) and
69 # calls the builtin character read callback to read it. It does not
70 # have to modify the user's terminal settings.
71 while ($r == 0) {
72 vec($fds, fileno($a->{instream}), 1) = 1;
73 $r = select($fds, undef, undef, 0.1);
74 $err = $!;
75 }
76
77 if ($saw_signal) {
78 sigint_handler($saw_signal);
79 }
80
81 if ($r < 0) {
82 warn "rltest: select: $!\n";
83 $t->callback_handler_remove();
84 last;
85 }
86
87 if ($r > 0) {
88 $t->callback_read_char();
89 }
90 }
91 return $input_string;
92 }
93
94 sub sigint_sighandler {
95 $saw_signal = shift;
96 }
97
98 sub sigint_handler {
99 my $s = shift;
100 $t->free_line_state();
101 $t->callback_sigcleanup();
102 $t->cleanup_after_signal();
103 $t->callback_handler_remove();
104 $saw_signal = 0;
105 return $s;
106 }
107
108 $running = 1;
109 $a->{catch_signals} = 1;
110
111 $t->bind_key(ord 'r', 'history-search-backward', 'emacs-meta');
112 $t->bind_key(ord 's', 'history-search-forward', 'emacs-meta');
113
114 $SIG{INT} = \&sigint_sighandler;
115 while ($running) {
116 my $p = cb_readline();
117 last if (!$p or $p eq 'exit');
118 }
119
120 print "$0: Event loop has exited\n";
121 exit 0;