Debian Codegraph @

eg/rlptytest

raw
1 #!/usr/bin/env perl
2 #
3 # rlptytest: Another test harness for the readline callback interface.
4 #
5 # Copyright (C) 2024 Hiroo Hayashi
6 #
7 # Derived from: examples/rlptytest.c in the GNU Readline Library
8 # Author: Bob Rossi <[email protected]>
9
10 use strict;
11 use warnings;
12 use Term::ReadLine;
13 use IO::Pty;
14 use POSIX qw(termios_h _POSIX_VDISABLE);
15
16 # sys/ioctl.ph may not exist on some systems.
17 # cf. https://perldoc.perl.org/functions/ioctl
18 # require 'sys/ioctl.ph'; # for TIOCGWINSZ
19
20 my $debug = 1;
21 my $t = new Term::ReadLine 'rlptytest';
22 my $a = $t->Attribs;
23
24 # Master/Slave PTY used to keep readline off of stdin/stdout.
25 my $masterfh;
26 my $slavefh;
27
28 sub quit {
29 tty_reset(fileno(STDIN));
30 close($masterfh);
31 close($slavefh);
32 print "\n";
33 exit(0);
34 }
35
36 sub sigint {
37 quit();
38 }
39
40 sub sigwinch {
41 $t->resize_terminal();
42 }
43
44 # STDIN -> user_input() -> masterfh -> pty -> slavefh -> rl_instream -> callback_read_char()
45 # STDERR <- readline_input() <- masterfh <- pty <- slavefh <- rl_outstream
46
47 sub user_input {
48 my $buf;
49 my $MAX = 1024;
50 my $size = sysread(STDIN, $buf, $MAX);
51 die "read: $!\n" unless defined $size;
52
53 my $ret = syswrite($masterfh, $buf, $size);
54 die "print: $!\n" unless $ret;
55 }
56
57 sub readline_input {
58 my $buf;
59 my $MAX = 1024;
60 my $size = sysread($masterfh, $buf, $MAX);
61 die "read: $!\n" unless defined $size;
62
63 # Display output from readline
64 print STDERR $buf if ($size > 0);
65 }
66
67 sub rlctx_send_user_command {
68 my ($line) = @_;
69
70 # This happens when rl_callback_read_char gets EOF
71 return if (!$line);
72 quit() if ($line eq "exit");
73
74 # Don't add the enter command
75 if ($line && $line ne "") {
76 $t->add_history($line);
77 }
78 }
79
80 sub custom_deprep_term_function {
81 }
82
83 sub init_readline {
84 my ($inputfh, $outputfh) = @_;
85 $a->{instream} = $inputfh;
86 $a->{outstream} = $outputfh;
87
88 # Tell readline what the prompt is if it needs to put it back
89 $t->callback_handler_install("(rltest): ", \&rlctx_send_user_command);
90
91 # Set the terminal type to dumb so the output of readline can be
92 # understood by tgdb
93 $t->reset_terminal("dumb"); # always returns 0
94
95 # For some reason, readline can not deprep the terminal.
96 # However, it doesn't matter because no other application is working on
97 # the terminal besides readline
98 $a->{deprep_term_function} = \&custom_deprep_term_function;
99
100 $t->using_history();
101 $t->read_history(".history");
102 }
103
104 sub main_loop {
105 while (1) {
106 # Reset the fd_set, and watch for input from GDB or stdin
107 my $rset = '';
108
109 vec($rset, fileno(STDIN), 1) = 1;
110 vec($rset, fileno($slavefh), 1) = 1;
111 vec($rset, fileno($masterfh), 1) = 1;
112
113 # Wait for input
114 if (select($rset, undef, undef, undef) == -1) {
115 if ($! && $!{EINTR}) {
116 next;
117 } else {
118 die "select: $!\n";
119 }
120 }
121
122 # Input received through the pty: Handle it
123 # Wrote to masterfd, slave fd has that input, alert readline to read it.
124 $t->callback_read_char() if (vec($rset, fileno($slavefh), 1));
125
126 # Input received through the pty.
127 # Readline read from slavefd, and it wrote to the masterfd.
128 readline_input() if (vec($rset, fileno($masterfh), 1));
129
130 # Input received: Handle it, write to masterfd (input to readline)
131 user_input() if (vec($rset, fileno(STDIN), 1));
132 }
133 }
134
135 # The terminal attributes before calling tty_cbreak
136 my $save_termios;
137 my ($RESET, $TCBREAK) = (0, 1);
138 my $ttystate = $RESET;
139
140 # tty_cbreak: Sets terminal to cbreak mode. Also known as noncanonical mode.
141 # 1. Signal handling is still turned on, so the user can still type those.
142 # 2. echo is off
143 # 3. Read in one char at a time.
144 #
145 # $fh - The file handle of the terminal
146 sub tty_cbreak {
147 my ($fh) = @_;
148 my $fd = fileno($fh);
149
150 $save_termios = POSIX::Termios->new;
151 if (!defined($save_termios->getattr($fd))) {
152 die "tcgetattr: $!\n";
153 }
154 my $buf = POSIX::Termios->new;
155 if (!defined($buf->getattr($fd))) {
156 die "tcgetattr: $!\n";
157 }
158
159 $buf->setlflag($buf->getlflag() & ~(ECHO | ICANON));
160 $buf->setiflag($buf->getiflag() & ~(ICRNL | INLCR));
161 $buf->setcc(1, VMIN);
162 $buf->setcc(0, VTIME);
163
164 $buf->setcc(_POSIX_VDISABLE, &POSIX::VLNEXT)
165 if defined(&POSIX::VLNEXT) && defined(_POSIX_VDISABLE);
166
167 $buf->setcc(_POSIX_VDISABLE, &POSIX::VDSUSP)
168 if defined(&POSIX::VDSUSP) && defined(_POSIX_VDISABLE);
169
170 # enable flow control; only stty start char can restart output
171 # $buf->setiflag($buf->getiflag() | IXON | IXOFF);
172 # $buf->setiflag($buf->getiflag() & ~&POSIX::IXANY) if defined(&POSIX::IXANY);
173
174 # disable flow control; let ^S and ^Q through to pty
175 $buf->setiflag($buf->getiflag() & ~(IXON | IXOFF));
176 $buf->setiflag($buf->getiflag() & ~&POSIX::IXANY) if defined(&POSIX::IXANY);
177
178 if (!defined($buf->setattr($fd, TCSAFLUSH))) {
179 die "tcsetattr: $!\n";
180 }
181
182 $ttystate = $TCBREAK;
183
184 # set size
185
186 # my $winsize = '';
187 # if (!defined(ioctl($fh, TIOCGWINSZ(), $winsize))) {
188 # die "ioctl: $!\n";
189 # } else {
190 # my ($rows, $cols, $xpix, $ypix) = unpack 'S4', $winsize;
191 # warn "$rows rows and $cols cols\n" if $debug;
192 # }
193
194 # use IO::Pty->get_winsize() instead of ioctl() for better portability
195 if ($slavefh->clone_winsize_from(\*STDIN)) {
196 my ($rows, $cols, $xpix, $ypix) = $slavefh->get_winsize();
197 warn "$rows rows and $cols cols\n" if $debug;
198 }
199 }
200
201 sub tty_off_xon_xoff {
202 my ($fh) = @_;
203 my $fd = fileno($fh);
204
205 my $buf = POSIX::Termios->new;
206 if (!defined($buf->getattr($fd))) {
207 die "tcgetattr: $!\n";
208 }
209
210 $buf->setiflag($buf->getiflag() & ~(IXON | IXOFF));
211
212 if (!defined($buf->setattr($fd, TCSAFLUSH))) {
213 die "tcsetattr: $!\n";
214 }
215 }
216
217 # tty_reset: Sets the terminal attributes back to their previous state.
218 # PRE: tty_cbreak must have already been called.
219 #
220 # fd - The file descrioptor of the terminal to reset.
221 sub tty_reset {
222 my ($fd) = @_;
223 return if ($ttystate != $TCBREAK);
224
225 if (!defined($save_termios->setattr($fd, TCSAFLUSH))) {
226 die "tcsetattr: $!\n";
227 }
228 $ttystate = $RESET;
229 }
230
231 #
232 # main
233 #
234 $masterfh = new IO::Pty;
235 $slavefh = $masterfh->slave;
236
237 tty_off_xon_xoff($masterfh);
238
239 $SIG{INT} = \&sigint;
240 $SIG{WINCH} = \&sigwinch;
241
242 init_readline($slavefh, $slavefh);
243 tty_cbreak(\*STDIN);
244
245 main_loop();
246
247 tty_reset(fileno(STDIN));
248
249 exit 0;