blib/lib/FSM/Basic.pm

filter: Statementstmt 77.7% 140 / 180 Branchbran 68.3% 41 / 60 Conditioncond 54.5% 30 / 55 MC/DCmcdc 14.8% 4 / 27 Subroutinesub 90.0% 9 / 10 Podpod 50.0% 2 / 4 totaltotal 67.2% 226 / 336 CC 61 SCAR 46.6
CC
61
Cov
72%
run
130.4
set
12.0
write_file
3.1
CRAP 105.2
↑ Summary
1 package FSM::Basic;
2  
3 10 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 FSM::Basic - Finite state machine using HASH as state definitions
10  
11 =head1 VERSION
12  
13 Version 0.20
14  
15 =cut
16  
17 10 our $VERSION = '0.20';
18  
19 =head1 SYNOPSIS
20  
21 A small finite state machine using a HASH data as description of the states
22 Mainly used to create fake bash or fake telnet server in the purpose to mimic some CLI device interface (like SWITCH or ROUTER interface)
23 Perhaps a little code snippet.
24 The HASH is easily using a JSON file
25  
26 use FSM::Basic;
27  
28 my $fsm = FSM::Basic->new( \%states, 'accept' );
29 my $final = 0;
30 my $out;
31 foreach my $in ( @ins )
32 {
33 ( $final, $out ) = $fsm->run( $in );
34 say $out;
35 last if $final;
36 }
37  
38  
39 =head1 SUBROUTINES/METHODS
40  
41 =head2 new
42  
43 my $fsm = FSM::Basic->new( \%states, 'accept' );
44  
45 Create the FSM with the HASH ref as first paramter
46 and the initial state as second parameter
47  
48 The HASH is like this:
49  
50 my %states = (
51 'accept' => {
52 'expect' => {
53 'default' => {
54 'final' => 0,
55 'matching' => 'prompt'
56 }
57 },
58 'not_matching' => 'accept',
59 'not_matching0' => 'close',
60 'not_matching_info_last' => '% Bad passwords
61 ',
62 'output' => 'Password: ',
63 'repeat' => 2
64 },
65  
66 'close' => {'final' => 1},
67 'prompt' => {
68 'expect' => {
69 'not_matching' => 'prompt',
70 'exit' => {
71 'matching' => 'close',
72 'final' => 0
73 },
74 'meminfo' => {'do' => 'do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> }'},
75 'h(elp)?|\\?' => {
76 'output' => 'exit
77 meminfo
78 mem_usage
79 User> '
80 },
81 'mem_usage' => {'do' => 'my ( $tot,$avail) = (split /\\n/ ,do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> })[0,2];$tot =~ s/\\D*//g; $avail =~ s/\\D*//g; sprintf "%0.2f%%\\n",(100*($tot-$avail)/$tot); '},
82 },
83 'not_matching_info' => '% Unknown command or computer name, or unable to find computer address',
84 'output' => 'User> '
85 }
86 );
87  
88  
89  
90 The keys are the states name.
91 "expect" contain a sub HASH where the keys are word or REGEX expected as input
92  
93 =over 1
94  
95 =item In this hash, you have :
96  
97 =over 2
98  
99 =item executable code:
100  
101 "do" for perl code
102  
103  
104 "exec" for system code,
105  
106  
107 and 4 dedicated commands:
108  
109 "cat" just reading the file content provided in parameter).
110  
111 "catRAND" chose randomly one of the files provided in parameter space separated
112  
113 "catWRAND" chose randomly (weighted) one of the files provided in parameter space separatedwith a : to separate the weight
114  
115 e.g. 'catWRAND' => './t/test_cat.txt:1 ./t/test_cat1.txt:50', in this case the file ./t/test_cat1.txt get 50 more chance to be selected than file ./t/test_cat.txt
116  
117 "catSEQ" read sequentialy the next files provided in parameter space separated
118 if "catSEQ_idx" is defined, that file is used to keep the state. Otherwise , the state file is named used
119 all the files name from "catSEQ" concatenated with a final '.tate'. All spaces are replaced by an underscore
120  
121 =back
122  
123 =over 2
124  
125 It is possible to use a regex to allow generics commands
126  
127 e.g.
128 "h(elp)?|\\?": {
129 "output": "default\nexit\ntimeoutA\n__PROMPT__"
130 }
131  
132 This json section match for 'h' 'help' or '?'
133  
134  
135  
136 In the regex, the group capture could be used in the command parameter as a substitution
137  
138 1st group is substituted by __1__
139  
140 2nd group is substituted by __2__
141  
142 ..
143 e.g. for a ping
144  
145 "ping (.*)" => {"exec" => "ping -c 3 __1__"},
146  
147 The __1__ is substitued by the first parameter in the expected command
148  
149 If you run the command "ping 127.0.0.1" the exec run the command "ping -c 3 127.0.0.1"
150  
151 Other example:
152  
153  
154 "cat /tmp/test/((\\w)(\\d))": {
155 "cat": "/tmp/test/__3__"
156 }
157
158 If you call with "cat /tmp/test/a1 the cat read the file /tmp/test/1" (without the 'a' because the group 3 is matching a single digit after a single character
159  
160 =back
161  
162 =over 4
163  
164 =item "matching"
165 to define the state when the input match the "expect" value
166  
167 =back
168  
169 =over 4
170  
171 =item "final"
172 to return a flag
173  
174 =back
175  
176 =over 4
177  
178 =item "not_matching"
179 the state if the input is not matching the "expect" value (if missing stay in the same state)
180
181 =back
182  
183 =over 4
184  
185 =item "repeat"
186 a number of trial before the state goes to "not_matching0"
187  
188 =back
189  
190 =over 4
191  
192 =item "not_matching0"
193 the state when the number of failled matching number is reached
194  
195  
196 =back
197  
198 =over 4
199  
200 =item "not_matching_info_last"
201 info returned as second value when the failled matching number is reached
202  
203 =back
204  
205 =over 4
206  
207 =item "output"
208 the info returned as second value
209  
210  
211 =back
212  
213 It is perfectly possible to add extra tag not used by FSM::Basic for generic purpose.
214 Check examples/fake_bash_ssh1.*
215 Take a look at timout and timer usage
216 In this example if destination IP from the SSH connection is available, the file IP.json is used as definition
217 (with fallback to fake_bash1.pl)
218  
219 =cut
220  
221 sub new {
222 9 ▶ my ($class, $l, $s) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
223 9 my $self;
224 9 $self->{states_list} = $l;
225 9 $self->{state} = $s;
226 9 bless($self, $class);
227 9 return $self;
228 }
229  
230 =back
231  
232 =head2 run
233  
234 my ( $final, $out ) = $fsm->run( $in );
235  
236 Run the FSM with the input and return the expected output and an extra flag
237  
238  
239 =cut
240  
241 sub run {
242 40018 ▶ my ($self, $in) = @_;
Subroutine run: called CC=56 SCAR=48.7
Pod documented
243  
244 40018 my $output = '';
245 40018 ▶ if (exists $self->{states_list}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}) 40018 0 40018
246 40018 ▶ if ( exists $self->{states_list}{ $self->{state} }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"} and $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0) 1 40017 40018
Condition83%5/6
exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"} and $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0
!ll&&!rl&&r
40007101
exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"}
!ll&&!rl&&r
04000711
MC/DC66%2/3
exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"} and $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0
exists $self->{"states_list"}{$self->{"state"}} exists $self->{"states_list"}{$self->{"state"}}{"repeat"} $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0
Truth table75%3/4
A and B and C
A= exists $self->{"states_list"}{$self->{"state"}}B= exists $self->{"states_list"}{$self->{"state"}}{"repeat"}C= $self->{"states_list"}{$self->{"state"}}{"repeat"} <= 0
ABCresult
0XX0
10X0
1100
1111
247 && exists $self->{states_list}{ $self->{state} }{repeat}
248 && $self->{states_list}{ $self->{state} }{repeat} <= 0)
249 {
250 1 $self->{previous_state} = $self->{state};
251 1 ▶ $self->{state} = $self->{states_list}{ $self->{state} }{expect}{not_matching0} // $self->{states_list}{ $self->{state} }{not_matching0};
Condition33%1/3
$self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"} // $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"} // $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
$self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"} $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
Truth table33%1/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"}B= $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
ABresult
1X1
011
000
252 1 ▶ if (exists $self->{states_list}{ $self->{previous_state} }{not_matching_info_last}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"previous_state"}}{"not_matching_info_last"}) 1 0 1
253 1 $output = $self->{states_list}{ $self->{previous_state} }{not_matching_info_last};
254 }
255 1 ▶ $output .= $self->{states_list}{ $self->{state} }{output} // '';
Condition50%1/2
$self->{"states_list"}{$self->{"state"}}{"output"} // ""
l!l
01
MC/DC0%0/1
$self->{"states_list"}{$self->{"state"}}{"output"} // ""
$self->{"states_list"}{$self->{"state"}}{"output"}
Truth table50%1/2
A
A= $self->{"states_list"}{$self->{"state"}}{"output"}
Aresult
11
00
256 1 ▶ return ($self->{states_list}{ $self->{state} }{final} // 0, $output);
Condition50%1/2
$self->{"states_list"}{$self->{"state"}}{"final"} // 0
l!l
10
MC/DC0%0/1
$self->{"states_list"}{$self->{"state"}}{"final"} // 0
$self->{"states_list"}{$self->{"state"}}{"final"}
Truth table50%1/2
A
A= $self->{"states_list"}{$self->{"state"}}{"final"}
Aresult
11
00
257 }
258 40017 ▶ if (exists $self->{states_list}{ $self->{state} }{expect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"expect"}) 40017 0 40017
259 40017 ▶ if (exists $self->{states_list}{ $self->{state} }{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"info"}) 0 40017 40017
260 $output =
261 0 $self->{states_list}{ $self->{state} }{info} . $output;
262 }
263 40017 ▶ if (exists $self->{states_list}{ $self->{state} }{info_once}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"info_once"}) 0 40017 40017
264 0 $output = delete($self->{states_list}{ $self->{state} }{info_once}) . $output;
265 }
266 40017 my $state;
267 40017 ▶ if ( exists $self->{previous_output}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"previous_output"} and $in eq "" and $self->{"previous_output"} =~ /\[(.+)\]/) 0 40017 40017
Condition50%3/6
exists $self->{"previous_output"} and $in eq "" and $self->{"previous_output"} =~ /\[(.+)\]/
!ll&&!rl&&r
4001700
exists $self->{"previous_output"} and $in eq ""
!ll&&!rl&&r
10400070
MC/DC0%0/3
exists $self->{"previous_output"} and $in eq "" and $self->{"previous_output"} =~ /\[(.+)\]/
exists $self->{"previous_output"} $in eq "" $self->{"previous_output"} =~ /\[(.+)\]/
Truth table50%2/4
A and B and C
A= exists $self->{"previous_output"}B= $in eq ""C= $self->{"previous_output"} =~ /\[(.+)\]/
ABCresult
0XX0
10X0
1100
1111
268 && $in eq ''
269 && $self->{previous_output} =~ /\[(.+)\]/)
270 {
271 0 $in = $1;
272 }
273 40017 ▶ if (exists $self->{states_list}{ $self->{state} }{expect}{$in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'states_list'}{$self->{'state'}}{'expect'}{$in}) { } 9 40008 40017
274 9 $state = $self->{states_list}{ $self->{state} }{expect}{$in};
275 } else {
276 40008 foreach my $key (keys %{ $self->{states_list}{ $self->{state} }{expect} }) {
277 160023 ▶ if ($in =~ /$key/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /$key/) 40005 120018 160023
278 40005 ▶ if (@+) {
Branch: 1 branch
BranchTrueFalseTotal
if (@+) 40005 0 40005
279 40005 for my $nbr (1 .. (scalar(@+)-1)) {
280 2 ▶ if ( defined $-[$nbr] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $-[$nbr]) 2 0 2
281 2 my $match = substr($in, $-[$nbr], $+[$nbr] - $-[$nbr]);
282 2 ▶ if (defined($+[$nbr])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+[$nbr]) 2 0 2
283 2 $self->{cmd_regex}{$nbr} = substr($in, $-[$nbr], $+[$nbr] - $-[$nbr]);
284 }
285 }
286 }
287 }
288 40005 $state = $self->{states_list}{ $self->{state} }{expect}{$key};
289 }
290 }
291 }
292 40017 ▶ if (ref $state eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state eq 'HASH') { } 40014 3 40017
293 40014 $self->{previous_state} = $self->{state};
294 40014 ▶ $self->{previous_output} = $state->{output} // $self->{states_list}{ $self->{state} }{output} // '';
Condition40%2/5
($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // ""
l!l
400140
$state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}
l!l&&r!l&&!r
0400140
MC/DC0%0/2
($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // ""
$state->{"output"} $self->{"states_list"}{$self->{"state"}}{"output"}
Truth table33%1/3
A // B
A= $state->{"output"}B= $self->{"states_list"}{$self->{"state"}}{"output"}
ABresult
1X1
011
000
295 40014 ▶ $self->{state} = $state->{matching} // $self->{state};
Condition66%2/3
$state->{"matching"} // $self->{"state"}
l!l&&r!l&&!r
8400060
MC/DC0%0/2
$state->{"matching"} // $self->{"state"}
$state->{"matching"} $self->{"state"}
Truth table66%2/3
A // B
A= $state->{"matching"}B= $self->{"state"}
ABresult
1X1
011
000
296 40014 ▶ $output .= $state->{output} // $self->{states_list}{ $self->{state} }{output} // '';
Condition80%4/5
($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // ""
l!l
400131
$state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}
l!l&&r!l&&!r
0400131
MC/DC50%1/2
($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // ""
$state->{"output"} $self->{"states_list"}{$self->{"state"}}{"output"}
Truth table66%2/3
A // B
A= $state->{"output"}B= $self->{"states_list"}{$self->{"state"}}{"output"}
ABresult
1X1
011
000
297 40014 ▶ if (exists $state->{cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"cmd"}) 0 40014 40014
298 0 my $cmd_state = delete $state->{cmd};
299 0 $cmd_state =~ s/\$in/$in/g;
300 0 push(@{ $self->{cmd_stack} }, $cmd_state);
301 }
302 40014 ▶ if (exists $state->{cmd_exec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"cmd_exec"}) 0 40014 40014
303 0 my $cmd_exec = join ' ', @{ $self->{cmd_stack} };
304 0 my $string = `$cmd_exec`;
305 0 $output = sprintf("%s", $string) . $output;
306 0 $self->{cmd_exec} = [];
307 }
308 40014 ▶ if (exists $state->{exec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"exec"}) 2 40012 40014
309 2 my $old_exec = $state->{exec};
310 2 $state->{exec} =~ s/__IN__/$in/g;
311 2 foreach my $k ( keys %{$self->{cmd_regex}} ) {
312 2 my $v = $self->{cmd_regex}{$k};
313 2 my $K = '__'.$k.'__';
314 2 $state->{exec} =~ s/$K/$v/g;
315 }
316 2 my $string = `$state->{exec}`;
317 2 $output = sprintf("%s", $string) . $output;
318 2 $state->{exec} = $old_exec;
319 }
320 40014 ▶ if (exists $state->{do}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"do"}) 0 40014 40014
321 0 my $old_do = $state->{do};
322 0 $state->{do} =~ s/__IN__/$in/g;
323 0 foreach my $k ( keys %{$self->{cmd_regex}} ) {
324 0 my $v = $self->{cmd_regex}{$k};
325 0 my $K = '__'.$k.'__';
326 0 $state->{do} =~ s/$K/$v/g;
327 }
328 0 $output = (eval $state->{do}) . $output;
329 0 $state->{do} = $old_do;
330 }
331 40014 ▶ if (exists $state->{cat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"cat"}) 1 40013 40014
332 1 my $old_cat = $state->{cat};
333 1 $state->{cat} =~ s/__IN__/$in/g;
334 1 foreach my $k ( keys %{$self->{cmd_regex}} ) {
335 0 my $v = $self->{cmd_regex}{$k};
336 0 my $K = '__'.$k.'__';
337 0 $state->{cat} =~ s/$K/$v/g;
338 }
339 1 my $string = do { local (@ARGV, $/) = $state->{cat}; <> };
340 1 $output = sprintf("%s", $string) . $output;
341 1 $state->{cat} = $old_cat;
342 }
343 40014 ▶ if (exists $state->{catRAND}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"catRAND"}) 20000 20014 40014
344 20000 my $old_cat = $state->{catRAND};
345 20000 $state->{catRAND} =~ s/__IN__/$in/g;
346 20000 foreach my $k ( keys %{$self->{cmd_regex}} ) {
347 0 my $v = $self->{cmd_regex}{$k};
348 0 my $K = '__'.$k.'__';
349 0 $state->{catRAND} =~ s/$K/$v/g;
350 }
351 20000 my @files = split /\s+/, $state->{catRAND};
352 20000 my $file = $files[ rand @files ];
353 20000 my $string = do { local (@ARGV, $/) = $file; <> };
354 20000 $output = sprintf("%s", $string) . $output;
355 20000 $state->{catRAND} = $old_cat;
356 }
357 40014 ▶ if (exists $state->{catWRAND}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"catWRAND"}) 20000 20014 40014
358 20000 my $old_cat = $state->{catWRAND};
359 20000 $state->{catWRAND} =~ s/__IN__/$in/g;
360 20000 foreach my $k ( keys %{$self->{cmd_regex}} ) {
361 0 my $v = $self->{cmd_regex}{$k};
362 0 my $K = '__'.$k.'__';
363 0 $state->{catWRAND} =~ s/$K/$v/g;
364 }
365 20000 my %files = map { split /:/ } split /\s+/, $state->{catWRAND};
366 20000 my $file;
367 my $weight;
368 20000 ▶ while (my ($p, $w) = each %files) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %files) 40000 20000 60000
369 40000 ▶ $w //= 1;
Condition50%1/2
$w //= 1
l!l
400000
MC/DC0%0/1
$w //= 1
$w
Truth table50%1/2
A
A= $w
Aresult
11
00
370 40000 ▶ $weight += $w // 1;
Condition50%1/2
$w // 1
l!l
400000
MC/DC0%0/1
$w // 1
$w
Truth table50%1/2
A
A= $w
Aresult
11
00
371 40000 ▶ $file = $p if rand($weight) < $w;
Branch: 1 branch
BranchTrueFalseTotal
if rand $weight < $w 30004 9996 40000
372 }
373 20000 my $string = do { local (@ARGV, $/) = $file; <> };
374 20000 $output = sprintf("%s", $string) . $output;
375 20000 $state->{catWRAND} = $old_cat;
376 }
377 40014 ▶ if (exists $state->{catSEQ}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"catSEQ"}) 3 40011 40014
378 3 my $old_cat = $state->{catSEQ};
379 3 my $state_file;
380 3 ▶ if (exists $state->{catSEQ_idx}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{'catSEQ_idx'}) { } 3 0 3
381 3 $state_file = $state->{catSEQ_idx};
382 } else {
383 0 $state_file = $old_cat . '.state';
384 0 $state_file =~ s/\s/_/g;
385  
386 }
387 3 $state->{catSEQ} =~ s/__IN__/$in/g;
388 3 foreach my $k ( keys %{$self->{cmd_regex}} ) {
389 0 my $v = $self->{cmd_regex}{$k};
390 0 my $K = '__'.$k.'__';
391 0 $state->{catSEQ} =~ s/$K/$v/g;
392 }
393 3 my @files = split /\s+/, $state->{catSEQ};
394 3 tie my $nbr => 'FSM::Basic::Modulo', scalar @files, 0;
395 3 ▶ if (-f $state_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $state_file) 2 1 3
396 2 $nbr = do {
397 2 local (@ARGV, $/) = $state_file;
398 2 <>;
399 };
400 }
401 3 my $file = $files[ $nbr++ ];
402 3 my $string = do { local (@ARGV, $/) = $file; <> };
403 3 $output = sprintf("%s", $string) . $output;
404 3 $state->{catSEQ} = $old_cat;
405 3 write_file($state_file, $nbr);
406 }
407 } else {
408 3 $self->{previous_state} = $self->{state};
409 3 ▶ $self->{state} = $self->{states_list}{ $self->{state} }{not_matching} // $self->{state};
Condition33%1/3
$self->{"states_list"}{$self->{"state"}}{"not_matching"} // $self->{"state"}
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{"states_list"}{$self->{"state"}}{"not_matching"} // $self->{"state"}
$self->{"states_list"}{$self->{"state"}}{"not_matching"} $self->{"state"}
Truth table33%1/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"not_matching"}B= $self->{"state"}
ABresult
1X1
011
000
410 $self->{states_list}{ $self->{state} }{repeat}--
411 3 ▶ if exists $self->{states_list}{ $self->{state} }{repeat};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"states_list"}{$self->{"state"}}{"repeat"} 3 0 3
412 3 ▶ $output .= $self->{states_list}{ $self->{state} }{output} // '';
Condition50%1/2
$self->{"states_list"}{$self->{"state"}}{"output"} // ""
l!l
30
MC/DC0%0/1
$self->{"states_list"}{$self->{"state"}}{"output"} // ""
$self->{"states_list"}{$self->{"state"}}{"output"}
Truth table50%1/2
A
A= $self->{"states_list"}{$self->{"state"}}{"output"}
Aresult
11
00
413 3 ▶ if (exists $self->{states_list}{ $self->{state} }{not_matching_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"not_matching_info"}) 0 3 3
414 0 $output = $self->{states_list}{ $self->{state} }{not_matching_info} . "\n" . $output;
415 }
416 3 ▶ return ($self->{states_list}{ $self->{state} }{$in}{final} // $self->{states_list}{ $self->{state} }{final} // 0, $output);
Condition40%2/5
($self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}) // 0
l!l
03
$self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}
l!l&&r!l&&!r
003
MC/DC0%0/2
($self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}) // 0
$self->{"states_list"}{$self->{"state"}}{$in}{"final"} $self->{"states_list"}{$self->{"state"}}{"final"}
Truth table33%1/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{$in}{"final"}B= $self->{"states_list"}{$self->{"state"}}{"final"}
ABresult
1X1
011
000
417 }
418 40014 ▶ return ($self->{states_list}{ $self->{state} }{$in}{final} // $self->{states_list}{ $self->{state} }{final} // 0, $output);
Condition80%4/5
($self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}) // 0
l!l
140013
$self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}
l!l&&r!l&&!r
0140013
MC/DC50%1/2
($self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}) // 0
$self->{"states_list"}{$self->{"state"}}{$in}{"final"} $self->{"states_list"}{$self->{"state"}}{"final"}
Truth table66%2/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{$in}{"final"}B= $self->{"states_list"}{$self->{"state"}}{"final"}
ABresult
1X1
011
000
419 }
420 }
421 }
422  
423 sub set {
424 0 ▶ my ($self, $in) = @_;
Subroutine set: not called CC=3 SCAR=24.8
Pod undocumented
425 0 $self->{previous_state} = $self->{state};
426 0 ▶ $self->{previous_output} = $self->{states_list}{ $self->{state} }{output} // '';
Condition0%0/2
$self->{"states_list"}{$self->{"state"}}{"output"} // ""
l!l
00
MC/DC0%0/1
$self->{"states_list"}{$self->{"state"}}{"output"} // ""
$self->{"states_list"}{$self->{"state"}}{"output"}
Truth table0%0/2
A
A= $self->{"states_list"}{$self->{"state"}}{"output"}
Aresult
11
00
427 0 ▶ $self->{state} = $in if exists $self->{states_list}{$in};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"states_list"}{$in} 0 0 0
428 }
429  
430 sub write_file {
431 3 ▶ my ($file, $content) = @_;
Subroutine write_file: called CC=3 SCAR=11.4
Pod undocumented
432 3 ▶ open my $fh, '>', $file or die "Error opening file for write $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Error opening file for write ${file}: $!\n" 0 3 3
433 3 print $fh $content;
434 3 ▶ close $fh or die "Error closing file $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Error closing file ${file}: $!\n" 0 3 3
435 }
436  
437 package FSM::Basic::Modulo;
438 3 ▶sub TIESCALAR { bless [ $_[2] || 0, $_[1] ] => $_[0] }
Subroutine TIESCALAR: called CC=2 SCAR=7.6
Condition50%1/2
$_[2] || 0
l!l
03
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table50%1/2
A
A= $_[2]
Aresult
11
00
439 6 ▶sub FETCH { ${ $_[0] }[0] }
Subroutine FETCH: called CC=1 SCAR=0.0
440 5 ▶sub STORE { ${ $_[0] }[0] = $_[1] % ${ $_[0] }[1] }
Subroutine STORE: called CC=1 SCAR=0.0
441 10 1;
442  
443 =head1 EXAMPLE
444  
445  
446 use strict;
447 use feature qw( say );
448 use FSM::Basic;
449 use JSON;
450 use Term::ReadLine;
451  
452 my %states = (
453 'accept' => {
454 'expect' => {
455 'default' => {
456 'final' => 0,
457 'matching' => 'prompt'
458 }
459 },
460 'not_matching' => 'accept',
461 'not_matching0' => 'close',
462 'not_matching_info_last' => '% Bad passwords
463 ',
464 'output' => 'Password: ',
465 'repeat' => 2
466 },
467  
468 'close' => {'final' => 1},
469 'prompt' => {
470 'expect' => {
471 'not_matching' => 'prompt',
472 'exit' => {
473 'matching' => 'close',
474 'final' => 0
475 },
476 "read" => {'cat' => 'file.txt'},
477 "read_random" => {'catRAND' => 'file1.txt file2.txt file3.txt'},
478 "read_seq" => {'catSEQ' => 'file1.txt file2.txt file3.txt', 'catSEQ_idx' => 'catSEQ_status'},
479 'meminfo' => {'do' => 'do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> }'},
480 'mem' => {
481 'do' => "my ( $tot,$avail) = (split /\n/ ,do { local( @ARGV, $/ ) = \"/proc/meminfo\" ; <> })[0,2];$tot =~ s/\\D*//g; $avail =~ s/\\D*//g; sprintf \"%0.2f%%\\n\",(100*($tot-$avail)/$tot);"
482 },
483 'h(elp)?|\\?' => {
484 'output' => 'exit
485 read
486 read_random
487 read_seq
488 meminfo
489 mem_usage
490 mem
491 User> '
492 },
493 'mem_usage' => {'do' => 'my ( $tot,$avail) = (split /\\n/ ,do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> })[0,2];$tot =~ s/\\D*//g; $avail =~ s/\\D*//g; sprintf "%0.2f%%\\n",(100*($tot-$avail)/$tot); '},
494 },
495 'not_matching_info' => '% Unknown command or computer name, or unable to find computer address',
496 'output' => 'User> '
497 }
498 );
499 my $history_file = glob( '/tmp/fsm.history' );
500 my $prompt = '> ';
501 my $line;
502 my $final = 0;
503 my $term = new Term::ReadLine 'bash';
504 my $attribs = $term->Attribs->ornaments( 0 );
505 $term->using_history();
506 $term->read_history( $history_file );
507 $term->clear_signals();
508  
509 my $fsm = FSM::Basic->new( \%states, 'accept' );
510 my $out = "Password> ";
511 while ( defined( $line = $term->readline( $out ) ) )
512 {
513 ( $final, $out ) = $fsm->run( $line );
514 $term->write_history( $history_file );
515 last if $final;
516 }
517  
518 print $out if $final;
519  
520  
521 More sample code in the examples folder.
522  
523  
524 =head1 TODO
525  
526 add "edit" to allow on the fly modification of the states definition
527  
528 add "verify_states" to check all states are reachable from a original state
529  
530 =head1 SEE ALSO
531  
532 FSA::Rules
533  
534 https://metacpan.org/pod/FSA::Rules
535  
536  
537 =head1 AUTHOR
538  
539 DULAUNOY Fabrice, C<< <fabrice at dulaunoy.com> >>
540  
541 =head1 BUGS
542  
543 Please report any bugs or feature requests to C<bug-FSM-basic at rt.cpan.org>, or through
544 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FSM-Basic>. I will be notified, and then you'll
545 automatically be notified of progress on your bug as I make changes.
546  
547 =head1 SUPPORT
548  
549 You can find documentation for this module with the perldoc command.
550  
551 perldoc FSM::Basic
552  
553  
554 You can also look for information at:
555  
556 =over 4
557  
558 =item * RT: CPAN's request tracker (report bugs here)
559  
560 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FSM-Basic>
561  
562 =item * AnnoCPAN: Annotated CPAN documentation
563  
564 L<http://annocpan.org/dist/FSM-Basic>
565  
566 =item * CPAN Ratings
567  
568 L<http://cpanratings.perl.org/d/FSM-Basic>
569  
570 =item * Search CPAN
571  
572 L<http://search.cpan.org/dist/FSM-Basic/>
573  
574 =back
575  
576  
577 =head1 ACKNOWLEDGEMENTS
578  
579  
580 =head1 LICENSE AND COPYRIGHT
581  
582 Copyright 2008 - 2020 DULAUNOY Fabrice.
583  
584 This program is free software; you can redistribute it and/or modify it
585 under the terms of the the Artistic License (2.0). You may obtain a
586 copy of the full license at:
587  
588 L<http://www.perlfoundation.org/artistic_license_2_0>
589  
590 Any use, modification, and distribution of the Standard or Modified
591 Versions is governed by this Artistic License. By using, modifying or
592 distributing the Package, you accept this license. Do not use, modify,
593 or distribute the Package, if you do not accept this license.
594  
595 If your Modified Version has been derived from a Modified Version made
596 by someone other than you, you are nevertheless required to ensure that
597 your Modified Version complies with the requirements of this license.
598  
599 This license does not grant you the right to use any trademark, service
600 mark, tradename, or logo of the Copyright Holder.
601  
602 This license includes the non-exclusive, worldwide, free-of-charge
603 patent license to make, have made, use, offer to sell, sell, import and
604 otherwise transfer the Package with respect to any patent claims
605 licensable by the Copyright Holder that are necessarily infringed by the
606 Package. If you institute patent litigation (including a cross-claim or
607 counterclaim) against any party alleging that the Package constitutes
608 direct or contributory patent infringement, then this Artistic License
609 to you shall terminate on the date that such litigation is filed.
610  
611 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
612 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
613 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
614 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
615 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
616 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
617 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
618 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
619  
620  
621 =cut
622  
623 10 1; # End of FSM::Basic
↑ Summary