forked from revspace/revbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevbank
executable file
·307 lines (239 loc) · 9.71 KB
/
revbank
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env perl
use v5.32;
use warnings;
use experimental 'isa'; # stable since v5.36
use experimental 'signatures'; # stable since v5.36
use List::Util qw(uniq);
use Sub::Util qw(subname);
use POSIX qw(ttyname);
use FindBin qw($RealBin);
use lib "$RealBin/lib";
use RevBank::Plugins;
use RevBank::Global;
use RevBank::Messages;
use RevBank::Cart;
use RevBank::Prompt;
our $VERSION = "6.2.0";
our %HELP1 = (
"abort" => "Abort the current transaction",
);
my @words; # input
my $one_off = 0;
if (@ARGV) {
# Non-interactive like in sh: -c command_string
@ARGV >= 1 and $ARGV[0] eq '-c' or die "$0 has no switches, except -c.";
$one_off = 1;
@words = RevBank::Prompt::split_input($ARGV[1]);
@words and not ref $words[0] or die "Syntax error.\n";
push @words, @ARGV[3 .. $#ARGV] if @ARGV > 3;
push @words, "help" if not @words;
} elsif (not ttyname fileno STDIN) {
warn "\e[31;1mNo controlling terminal, things will be borken!\n";
warn "Use ssh -t (or RequestTTY in .ssh/config) for interactive sessions.\e[m\n";
}
$| = 1;
my $cart = RevBank::Cart->new;
RevBank::Plugins->load;
call_hooks("startup");
my $retry; # reason (text)
my @retry; # (@accepted, $rejected, [@trailing])
OUTER: for (;;) {
if (not @words or $words[0] eq "\0SEPARATOR") {
call_hooks("cart_changed", $cart) if $cart->changed;
print "\n";
}
my $prompt = "";
my @plugins = RevBank::Plugins->new;
my $method = "command";
sub abort {
print @_, " " if @_;
@words = ();
@retry = ();
call_hooks "abort", $cart, \@_;
$cart->empty;
RevBank::FileIO::release_all_locks;
{ no warnings; redo OUTER; }
}
PROMPT: {
if (not @words) {
if ($one_off) {
exit if $one_off++ > 1;
abort "Incomplete command." if $cart->size;
exit;
}
call_hooks "prompt", $cart, $prompt;
my $split_input = !ref($method) && $method eq 'command';
my @completions = uniq 'abort', map $_->Tab($method), @plugins;
my $default = "";
my $pos = 0;
if ($retry) {
print "$retry\n";
my $word_based = ref($retry[-1]);
my @trailing = $word_based ? @{ pop @retry } : ();
my @rejected = pop @retry;
my @accepted = @retry;
if ($word_based) {
for (@accepted, @rejected, @trailing) {
$_ = RevBank::Prompt::reconstruct($_);
}
}
my $sep = $word_based ? " " : "";
$default = join($sep, @accepted, @rejected, @trailing);
$pos = @accepted ? length "@accepted$sep" : 0;
@retry = ();
$retry = 0;
}
my $input = RevBank::Prompt::prompt(
$prompt, \@completions, $default, $pos, $cart, \@plugins
);
if (not defined $input) {
exit if not ttyname fileno STDIN; # Controlling terminal gone
}
call_hooks "input", $cart, $input, $split_input;
length $input or redo PROMPT;
if ($split_input) {
@words = RevBank::Prompt::split_input($input);
if (ref $words[0]) {
my $pos = ${ $words[0] };
@retry = @words = ();
$retry = "Syntax error.";
if ($input =~ /['"]/) {
$retry .= " (Quotes must match and (only) be at both ends of a term.)";
if (($input =~ tr/'//) == 1 and $input !~ /"/) {
$retry .= "\nDid you mean: " . $input =~ s/'/\\'/r;
}
}
push @retry, substr($input, 0, $pos) if $pos > 0;
push @retry, substr($input, $pos);
redo PROMPT;
}
} else {
$input = "\0ABORT" if $input =~ /^\s*abort\s*$/;
@words = $input;
}
}
WORD: for (;;) {
redo PROMPT if not @words;
abort if grep $_ eq "\0ABORT", @words;
my $origword = my $word = shift @words;
my @allwords = ($origword);
next WORD if $word eq "\0SEPARATOR";
abort if $method eq "command" and $word eq "abort"; # here, even when quoted
push @retry, $word;
ALL_PLUGINS: { PLUGIN: for my $plugin (@plugins) {
$cart->prohibit_checkout(
@words && $words[0] ne "\0SEPARATOR",
"unexpected trailing input (use ';' to separate transactions)."
);
my $coderef = ref($method) ? $method : $plugin->can($method);
my ($mname) = $coderef
? (subname($coderef) eq "__ANON__" ? "" : subname($coderef) . ": ")
: (ref($method) ? "" : "$method: ");
my ($rv, @rvargs) =
($word =~ /[^\x20-\x7f]/ and $method eq 'command' || !$plugin->AllChars($method))
? (REJECT, "Unexpected control character in input.")
: eval { $plugin->$method($cart, $word) };
if ($@ isa 'RevBank::Cart::CheckoutProhibited') {
@words or die "Internal inconsistency"; # other cause than trailing input
push @retry, shift @words; # reject next word (first of trailing)
push @retry, [@words];
@words = ();
$retry = $@->reason;
redo OUTER;
} elsif ($@ isa 'RevBank::Exception::RejectInput') {
$rv = REJECT;
@rvargs = $@->reason;
} elsif ($@) {
call_hooks "plugin_fail", $plugin->id, "$mname$@";
abort;
}
if (not defined $rv) {
call_hooks "plugin_fail", $plugin->id, $mname . "No return code";
abort;
}
if (not ref $rv) {
abort "Incomplete command." if $one_off and not @words;
if (@words and $words[0] eq "\0SEPARATOR") {
push @retry, shift @words; # reject the ';'
push @retry, [@words];
@words = ();
$retry = "Incomplete command (expected: $rv)";
redo OUTER;
}
$prompt = $rv;
@plugins = $plugin;
($method) = @rvargs;
if (not ref $method) {
call_hooks "plugin_fail", $plugin->id, $mname . "No method supplied";
abort;
}
next WORD;
}
if ($rv == ABORT) {
abort(@rvargs);
}
if ($rv == REDO) {
$word = $rvargs[0];
call_hooks "redo", $plugin->id, $origword, $word;
push @allwords, $word;
redo ALL_PLUGINS;
}
if ($rv == REJECT) {
my ($reason) = @rvargs;
if (@words) {
call_hooks "retry", $plugin->id, $reason, @words ? 1 : 0;
push @retry, [@words];
@words = ();
$retry = $reason;
redo OUTER;
} else {
call_hooks "reject", $plugin->id, $reason, @words ? 1 : 0;
@retry = ();
redo PROMPT;
}
}
if ($rv == ACCEPT) {
if ($method ne 'command' and @words and $words[0] ne "\0SEPARATOR") {
@retry = (); # remove what's already done
push @retry, shift @words; # reject first
push @retry, [@words];
@words = ();
$retry = "Confirm trailing input to execute. (Hint: use ';' after command arguments.)";
redo OUTER;
}
@retry = ();
next OUTER;
}
if ($rv == NEXT) {
next PLUGIN if $method eq 'command';
call_hooks "plugin_fail", $plugin->id, $mname
. "Only 'command' should ever return NEXT.";
abort;
}
call_hooks "plugin_fail", $plugin->id, $mname . "Invalid return value";
abort;
}
call_hooks "invalid_input", $cart, $origword, $word, \@allwords;
@retry = ();
abort if @words;
redo OUTER;
} }
}
}
=head1 NAME
revbank - Banking for hackerspace visitors
=head1 DESCRIPTION
Maybe I'll write some documentation, but not now.
Shell-like invocation with C<-c> is supported, sort of, but it has to be a
complete command. Currently, multiple commands are supported on the command
line (space separated), but that's an unintended feature...
=head1 PLUGINS
Refer to L<RevBank::Plugins> for documentation about writing plugins.
Plugins themselves may have some documentation in the respective plugin files.
Note that plugins that begin with C<revspace_> are revspace specific hacks, and
were not written with reusability in mind. They will probably not work for your
setup.
=head1 AUTHOR
Juerd Waalboer <#####@juerd.nl>
=head1 LICENSE
Pick your favorite OSI license.