Debian Codegraph @

eg/rl-callbacktest3

raw
1 #!/usr/bin/env perl
2 #
3 # rl-callbacktest3: rl-callbacktest + sigint_handler() + rl_getc_function
4 #
5 # Copyright (C) 2024 Hiroo Hayashi
6 #
7 # Derived from: examples/rl-callbacktest3.c in the GNU Readline Library
8
9 use strict;
10 use warnings;
11 use Term::ReadLine;
12
13 my $t = new Term::ReadLine 'rl-callbacktest';
14 my $a = $t->Attribs;
15
16 my $sigwinch_received = 0;
17 my $sigint_received = 0;
18 my $running = 0;
19 my $prompt = 'rltest$ ';
20
21 # Handle window size changes when readline is not active and reading
22 # characters.
23 sub sigwinch_handler {
24 my $sig = shift;
25 $sigwinch_received = 1;
26 }
27
28 # Handle SIGINT when readline is not active and reading a character.
29 sub sigint_handler {
30 my $sig = shift;
31 $sigint_received = 1;
32 }
33
34 # Callback function called for each line when accept-line executed, EOF
35 # seen, or EOF character read. This sets a flag and returns; it could
36 # also call exit(3).
37 sub cb_linehandler {
38 my $line = shift;
39
40 # Can use ^D (stty eof) or `exit' to exit.
41 if (!$line || $line eq "exit") {
42 print "\n" unless $line;
43 print "exit\n";
44
45 # This function needs to be called to reset the terminal settings,
46 # and calling it from the line handler keeps one extra prompt from
47 # being displayed.
48 $t->callback_handler_remove();
49
50 $running = 0;
51 } else {
52 $t->add_history($line) if $line;
53 print "input line: $line\n";
54 }
55 }
56
57 # replace with something more complex if desired
58 sub my_getc {
59 # my $stream = shift;
60 # my $ch = $t->getc($stream);
61 my $ch = $t->getc($a->{instream});
62 return $ch;
63 }
64
65 # Handle SIGWINCH
66 $SIG{WINCH} = \&sigwinch_handler;
67
68 # Handle SIGINT
69 $SIG{INT} = \&sigint_handler;
70
71 # Install the getc function.
72 $a->{getc_function} = \&my_getc;
73
74 # Install the line handler.
75 $t->callback_handler_install($prompt, \&cb_linehandler);
76
77 # Enter a simple event loop. This waits until something is available
78 # to read on readline's input stream (defaults to standard input) and
79 # calls the builtin character read callback to read it. It does not
80 # have to modify the user's terminal settings.
81 $running = 1;
82 while ($running) {
83 my $fds = '';
84 vec($fds, fileno($a->{instream}), 1) = 1;
85
86 my $r = select($fds, undef, undef, undef);
87 if ($r < 0 && $! && !$!{EINTR}) { # EINTR is not an error
88 warn "$0: select: $!\n";
89 $t->callback_handler_remove();
90 last;
91 }
92 if ($sigwinch_received) {
93 warn "$0: SIGWINCH received\n";
94 $t->resize_terminal();
95 $sigwinch_received = 0;
96 }
97 if ($sigint_received) {
98 print "Quit\n";
99
100 $t->callback_handler_remove();
101 $t->callback_handler_install($prompt, \&cb_linehandler);
102
103 $sigint_received = 0;
104 next;
105 }
106 next if $r < 0; # select returns EINTR (interrupted system call)
107
108 if (vec($fds, fileno($a->{instream}), 1)) {
109 $t->callback_read_char();
110 }
111 }
112
113 print "$0: Event loop has exited\n";
114 exit 0;