/
cb-echo.pl6
146 lines (130 loc) · 3.94 KB
/
cb-echo.pl6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env perl6
use v6;
use Readline;
use NativeCall; # For libc declarations
# sub cb_linehandler ( Str ); # forward definition
my Int $running;
my Str $prompt = "cb prompt here ('exit' to exit>)> ";
# Callback function called for each line when accept-line executed, EOF
# seen, or EOF character read. This sets a flag and returns; it could
# also call exit(3).
sub cb_linehandler( Str $line ) {
# Can use ^D (stty eof) or `exit' to exit.
if !$line.defined or $line ~~ /exit/ {
if !$line.defined {
say();
}
say "exit";
# This function needs to be called to reset the terminal settings,
# and calling it from the line handler keeps one extra prompt from
# being displayed.
rl_callback_handler_remove();
$running = 0;
}
else {
if $line ~~ /\S/ {
add_history( $line );
}
say "input line: {$line}";
# free( $line );
}
}
sub perror ( Str ) is native( 'libc' ) { * }
sub MAIN( ) returns Int { # c and v are unused, ignore arguments.
#my fd_set $fds; # struct
my $r = 0;
# Install the line handler.
rl_callback_handler_install( $prompt, &cb_linehandler );
# Enter a simple event loop. This waits until something is available
# to read on readline's input stream (defaults to standard input) and
# calls the builtin character read callback to read it. It does not
# have to modify the user's terminal settings.
$running = 1;
while $running {
#FD_ZERO( \$fds );
#FD_SET( fileno( $rl_instream ), \$fds );
#$r = select( FD_SETSIZE, \$fds, Nil, Nil, Nil );
if $r < 0 {
perror( "rltest: select" );
rl_callback_handler_remove();
last;
}
#if FD_ISSET (fileno (rl_instream), &fds) {
rl_callback_read_char( );
#}
}
say "rltest: Event loop has exited";
return 0;
}
# #my $rl_instream := cglobal( LIB, 'rl_instream', Pointer );
#############################################################################
#
# static void cb_linehandler (char *);
#
# int running;
# const char *prompt = "rltest$ ";
#
# /* Callback function called for each line when accept-line executed, EOF
# seen, or EOF character read. This sets a flag and returns; it could
# also call exit(3). */
# static void
# cb_linehandler (char *line)
# {
# /* Can use ^D (stty eof) or `exit' to exit. */
# if (line == NULL || strcmp (line, "exit") == 0)
# {
# if (line == 0)
# printf ("\n");
# printf ("exit\n");
# /* This function needs to be called to reset the terminal settings,
# and calling it from the line handler keeps one extra prompt from
# being displayed. */
# rl_callback_handler_remove ();
#
# running = 0;
# }
# else
# {
# if (*line)
# add_history (line);
# printf ("input line: %s\n", line);
# free (line);
# }
# }
#
# int
# main (int c, char **v)
# {
# fd_set fds;
# int r;
#
# /* Install the line handler. */
# rl_callback_handler_install (prompt, cb_linehandler);
#
# /* Enter a simple event loop. This waits until something is available
# to read on readline's input stream (defaults to standard input) and
# calls the builtin character read callback to read it. It does not
# have to modify the user's terminal settings. */
# running = 1;
# while (running)
# {
# FD_ZERO (&fds);
# FD_SET (fileno (rl_instream), &fds);
#
# r = select (FD_SETSIZE, &fds, NULL, NULL, NULL);
# if (r < 0)
# {
# perror ("rltest: select");
# rl_callback_handler_remove ();
# break;
# }
#
# if (FD_ISSET (fileno (rl_instream), &fds))
# rl_callback_read_char ();
# }
#
# printf ("rltest: Event loop has exited\n");
# return 0;
# }
#
#############################################################################