blib/lib/FSM/Basic.pm

filter: Statementstmt 53.8% 120 / 223 Branchbran 39.7% 35 / 88 Conditioncond 31.8% 22 / 69 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 81.8% 9 / 11 Podpod 20.0% 1 / 5 totaltotal 43.2% 187 / 432 CC 84 SCAR 64.3
CC
84
Cov
47%
run
840.3
alter
30.0
set
12.0
CRAP 619.6
↑ Summary
1 package FSM::Basic;
2  
3 6 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶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.23
14  
15 =cut
16  
17 6 our $VERSION = '0.23';
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  
94 =over 1
95  
96 =item In this hash, you have :
97  
98 =back
99  
100 =over 2
101  
102 =item executable code:
103  
104 "do" for perl code
105  
106  
107 "exec" for system code,
108  
109  
110 and 4 dedicated commands:
111  
112 "cat" just reading the file content provided in parameter).
113  
114 "catRAND" chose randomly one of the files provided in parameter space separated
115  
116 "catWRAND" chose randomly (weighted) one of the files provided in parameter space separatedwith a : to separate the weight
117  
118 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
119  
120 "catSEQ" read sequentialy the next files provided in parameter space separated
121 if "catSEQ_idx" is defined, that file is used to keep the state. Otherwise , the state file is named used
122 all the files name from "catSEQ" concatenated with a final '.tate'. All spaces are replaced by an underscore
123  
124 =back
125  
126 =over 3
127  
128 =item * It is possible to use a regex to allow optional commands
129  
130  
131 e.g.
132 "h(elp)?|\\?": {
133 "output": "default\nexit\ntimeoutA\n__PROMPT__"
134 }
135  
136 This json section match for 'h' 'help' or '?'
137  
138  
139  
140 In the regex, the group capture could be used in the command parameter as a substitution
141  
142 1st group is substituted by __1__
143  
144 2nd group is substituted by __2__
145  
146 ...
147  
148 e.g. for a ping
149  
150 "ping (.*)" => {"exec" => "ping -c 3 __1__"},
151  
152 The __1__ is substitued by the first parameter in the expected command
153  
154 If you run the command "ping 127.0.0.1" the exec run the command "ping -c 3 127.0.0.1"
155  
156 Other example:
157  
158  
159 "cat /tmp/test/((\\w)(\\d))": {
160 "cat": "/tmp/test/__3__"
161 }
162
163 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
164  
165  
166  
167 =item * If you need to match all possible partial match for a word ( like "h, "he", "hel", "help" to match the "help" state)
168 Use the extra tag "swapregex": "1"
169  
170 In case of "swapregex": "1" we use this
171  
172 $key =~ /^$in/
173
174 e.g.
175
176 'expect' => {
177 'not_matching' => 'prompt',
178 'help' => {
179 'swapregex' => 1,
180 'output' => 'in enable',
181 'final' => 0
182 }
183 }
184  
185 In this example me match "h, "he", "hel", "help"
186  
187  
188 This reverse the REGEX test
189 In normal case (no "swapregex" )
190 The state is check with
191  
192 $in =~ /^$key$/
193  
194 Where $in is the input from the run() function
195 and $key is the state defined in the HASH
196  
197  
198  
199 =item * If you need to match all possible abbreviation starting from a fixed header you use the tag 'alternation'=>1 and the variable part between sqare bracket.
200  
201  
202 e.g.
203  
204 'ot[her]' => {
205 'alternation' => 1,
206 'output' => 'in enable',
207 'final' => 0
208 }
209  
210 This match 'oth', 'othe' and 'other'
211  
212 In fact the system create the REGEX /^o(t|th|the|ther)$/
213  
214 For a more complex example:
215  
216 'c[onfiguration] t[erminal]'
217  
218 create the REGEX
219  
220 /^c(o|on|onf|onfi|onfig|onfigu|onfigur|onfigura|onfigurat|onfigurati|onfiguratio|onfiguration) t(e|er|erm|ermi|ermin|ermina|erminal)$/
221  
222  
223 It is possible to add the tag 'caseinsensitive' => 1, to allow also the case insentitive matching
224  
225  
226 =over 4
227  
228 =item * It is possible to do a case insensitive matching with the special tag "caseinsensitive": "1"
229  
230 =over 4
231  
232 e.g.
233  
234 'other' => {
235 'output' => 'in other',
236 'caseinsensitive' => 1,
237 'final' => 0
238 }
239
240 in this example "other", "OTHER", "Other", "otheR" (and all other case alternate set) match the state "other"
241  
242 =back
243  
244 =over 4
245  
246 "matching"
247 to define the state when the input match the "expect" value
248  
249 =back
250  
251 =over 4
252  
253 =item "final"
254 to return a flag
255  
256 =back
257  
258 =over 4
259  
260 =item "not_matching"
261 the state if the input is not matching the "expect" value (if missing stay in the same state)
262
263 =back
264  
265 =over 4
266  
267 =item "repeat"
268 a number of trial before the state goes to "not_matching0"
269  
270 =back
271  
272 =over 4
273  
274 =item "not_matching0"
275 the state when the number of failled matching number is reached
276  
277  
278 =back
279  
280 =over 4
281  
282 =item "not_matching_info_last"
283 info returned as second value when the failled matching number is reached
284  
285 =back
286  
287 =over 4
288  
289 =item "output"
290 the info returned as second value
291  
292  
293 =back
294  
295 It is perfectly possible to add extra tag not used by FSM::Basic for generic purpose.
296 Check examples/fake_bash_ssh1.*
297 Take a look at timout and timer usage
298 In this example if destination IP from the SSH connection is available, the file IP.json is used as definition
299 (with fallback to fake_bash1.pl)
300  
301 =cut
302  
303 sub new {
304 5 ▶ my ($class, $l, $s) = @_;
Subroutine new: called CC=7 SCAR=21.1
Pod documented
305 5 my $self;
306 5 $self->{states_list} = $l;
307 5 $self->{state} = $s;
308 5 foreach my $k1 ( keys %{ $self->{states_list} }) {
309 15 ▶ if ( exists $self->{states_list}{ $k1 }{expect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$k1}{"expect"}) 10 5 15
310 10 foreach my $k2 ( keys %{ $self->{states_list}{ $k1 }{expect} }) {
311 28 ▶ if (ref $self->{states_list}{ $k1 }{expect}{$k2} eq 'HASH' && exists $self->{states_list}{ $k1 }{expect}{$k2}{alternation}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH" and exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}) 0 28 28
Condition66%2/3
ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH" and exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}
!ll&&!rl&&r
5230
MC/DC0%0/2
ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH" and exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}
ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH" exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}
Truth table66%2/3
A and B
A= ref $self->{"states_list"}{$k1}{"expect"}{$k2} eq "HASH"B= exists $self->{"states_list"}{$k1}{"expect"}{$k2}{"alternation"}
ABresult
0X0
100
111
312 0 ▶ if (defined $self->{states_list}{ $k1 }{expect}{$k2}{caseinsensitive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'states_list'}{$k1}{'expect'}{$k2}{'caseinsensitive'}) { } 0 0 0
313 0 $self->{states_list}{ $k1 }{expect}{alter($k2,1)} = delete $self->{states_list}{ $k1 }{expect}{$k2};
314 }else{
315 0 $self->{states_list}{ $k1 }{expect}{alter($k2)} = delete $self->{states_list}{ $k1 }{expect}{$k2};
316 }
317 }
318 }
319 }
320 }
321 5 bless($self, $class);
322 5 return $self;
323 }
324  
325 =back
326  
327 B<run>
328  
329 my ( $final, $out ) = $fsm->run( $in );
330  
331 Run the FSM with the input and return the expected output and an extra flag
332  
333  
334 =cut
335  
336 sub run {
337 40009 ▶ my ($self, $in) = @_;
Subroutine run: called CC=69 SCAR=67.3
Pod undocumented
338 40009 my $in_lc = lc($in);
339 40009 my $rev;
340 my $alternation;
341 40009 foreach my $IN (grep { /$in/i } keys %{ $self->{states_list}{ $self->{state} }{expect} }) {
342 40009 ▶ if (ref $self->{states_list}{ $self->{state} }{expect}{$IN} eq 'HASH' && defined $self->{states_list}{ $self->{state} }{expect}{$IN}{swapregex}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN} eq "HASH" and defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN}{"swapregex"}) 0 40009 40009
Condition33%1/3
ref $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN} eq "HASH" and defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN}{"swapregex"}
!ll&&!rl&&r
0400090
MC/DC0%0/2
ref $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN} eq "HASH" and defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN}{"swapregex"}
ref $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN} eq "HASH" defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN}{"swapregex"}
Truth table33%1/3
A and B
A= ref $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN} eq "HASH"B= defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$IN}{"swapregex"}
ABresult
0X0
100
111
343 0 ▶ $rev //= $IN;
Condition0%0/3
$rev //= $IN
l!l&&r!l&&!r
000
MC/DC0%0/2
$rev //= $IN
$rev $IN
Truth table0%0/3
A //= B
A= $revB= $IN
ABresult
1X1
011
000
344 }
345 }
346 40009 my $output = '';
347 40009 ▶ if (exists $self->{states_list}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}) 40009 0 40009
348 40009 ▶ 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) 0 40009 40009
Condition66%4/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
4000450
exists $self->{"states_list"}{$self->{"state"}} and exists $self->{"states_list"}{$self->{"state"}}{"repeat"}
!ll&&!rl&&r
0400045
MC/DC0%0/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 table50%2/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
349 && exists $self->{states_list}{ $self->{state} }{repeat}
350 && $self->{states_list}{ $self->{state} }{repeat} <= 0)
351 {
352 0 $self->{previous_state} = $self->{state};
353 0 ▶ $self->{state} = $self->{states_list}{ $self->{state} }{expect}{not_matching0} // $self->{states_list}{ $self->{state} }{not_matching0};
Condition0%0/3
$self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"} // $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
l!l&&r!l&&!r
000
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 table0%0/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"expect"}{"not_matching0"}B= $self->{"states_list"}{$self->{"state"}}{"not_matching0"}
ABresult
1X1
011
000
354 0 ▶ 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"}) 0 0 0
355 0 $output = $self->{states_list}{ $self->{previous_state} }{not_matching_info_last};
356 }
357 0 ▶ $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
358 0 ▶ return ($self->{states_list}{ $self->{state} }{final} // 0, $output);
Condition0%0/2
$self->{"states_list"}{$self->{"state"}}{"final"} // 0
l!l
00
MC/DC0%0/1
$self->{"states_list"}{$self->{"state"}}{"final"} // 0
$self->{"states_list"}{$self->{"state"}}{"final"}
Truth table0%0/2
A
A= $self->{"states_list"}{$self->{"state"}}{"final"}
Aresult
11
00
359 }
360 40009 ▶ if (exists $self->{states_list}{ $self->{state} }{expect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"expect"}) 40009 0 40009
361 40009 ▶ if (exists $self->{states_list}{ $self->{state} }{info}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"info"}) 0 40009 40009
362 0 $output = $self->{states_list}{ $self->{state} }{info} . $output;
363 }
364 40009 ▶ if (exists $self->{states_list}{ $self->{state} }{info_once}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"states_list"}{$self->{"state"}}{"info_once"}) 0 40009 40009
365 0 $output = delete($self->{states_list}{ $self->{state} }{info_once}) . $output;
366 }
367 40009 my $state;
368 40009 ▶ if ( exists $self->{previous_output}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"previous_output"} and $in eq "" and $self->{"previous_output"} =~ /\[(.+)\]/) 0 40009 40009
Condition50%3/6
exists $self->{"previous_output"} and $in eq "" and $self->{"previous_output"} =~ /\[(.+)\]/
!ll&&!rl&&r
4000900
exists $self->{"previous_output"} and $in eq ""
!ll&&!rl&&r
5400040
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
369 && $in eq ''
370 && $self->{previous_output} =~ /\[(.+)\]/)
371 {
372 0 $in = $1;
373 }
374 40009 ▶ if (defined $rev) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $rev) { } 0 40009 40009
elsif (exists $self->{'states_list'}{$self->{'state'}}{'expect'}{$in_lc} and defined $self->{'states_list'}{$self->{'state'}}{'expect'}{$in_lc}{'caseinsensitive'}) { } 0 40009 40009
elsif (exists $self->{'states_list'}{$self->{'state'}}{'expect'}{$in}) { } 40009 0 40009
375 0 my $key = $rev;
376 0 my $r = $in;
377 0 ▶ $r = '(?i:' . $r . ')' if defined $self->{states_list}{ $self->{state} }{expect}{$rev}{caseinsensitive};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$rev}{"caseinsensitive"} 0 0 0
378 0 ▶ if ($key =~ /^$r/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^$r/) 0 0 0
379 0 ▶ if (@+) {
Branch: 1 branch
BranchTrueFalseTotal
if (@+) 0 0 0
380 0 for my $nbr (1 .. (scalar(@+) - 1)) {
381 0 ▶ if (defined $-[$nbr]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $-[$nbr]) 0 0 0
382 0 my $match = substr($key, $-[$nbr], $+[$nbr] - $-[$nbr]);
383 0 ▶ if (defined($+[$nbr])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+[$nbr]) 0 0 0
384 0 $self->{cmd_regex}{$nbr} = substr($key, $-[$nbr], $+[$nbr] - $-[$nbr]);
385 }
386 }
387 }
388 }
389 0 $state = $self->{states_list}{ $self->{state} }{expect}{$key};
390 }
391 ▶ } elsif (exists $self->{states_list}{ $self->{state} }{expect}{$in_lc} && defined $self->{states_list}{ $self->{state} }{expect}{$in_lc}{caseinsensitive}) {
Condition33%1/3
exists $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc} and defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}{"caseinsensitive"}
!ll&&!rl&&r
0400090
MC/DC0%0/2
exists $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc} and defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}{"caseinsensitive"}
exists $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc} defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}{"caseinsensitive"}
Truth table33%1/3
A and B
A= exists $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}B= defined $self->{"states_list"}{$self->{"state"}}{"expect"}{$in_lc}{"caseinsensitive"}
ABresult
0X0
100
111
392 0 $state = $self->{states_list}{ $self->{state} }{expect}{$in_lc};
393 } elsif (exists $self->{states_list}{ $self->{state} }{expect}{$in}) {
394 40009 $state = $self->{states_list}{ $self->{state} }{expect}{$in};
395 } else {
396 0 foreach my $key (keys %{ $self->{states_list}{ $self->{state} }{expect} }) {
397 0 ▶ if ($in =~ /^$key$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /^$key$/) 0 0 0
398 0 ▶ if (@+) {
Branch: 1 branch
BranchTrueFalseTotal
if (@+) 0 0 0
399 0 for my $nbr (1 .. (scalar(@+) - 1)) {
400 0 ▶ if (defined $-[$nbr]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $-[$nbr]) 0 0 0
401 0 my $match = substr($in, $-[$nbr], $+[$nbr] - $-[$nbr]);
402 0 ▶ if (defined($+[$nbr])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+[$nbr]) 0 0 0
403 0 $self->{cmd_regex}{$nbr} = substr($in, $-[$nbr], $+[$nbr] - $-[$nbr]);
404 }
405 }
406 }
407 }
408 0 $state = $self->{states_list}{ $self->{state} }{expect}{$key};
409 }
410 }
411 }
412 40009 ▶ if (ref $state eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state eq 'HASH') { } 40009 0 40009
413 40009 $self->{previous_state} = $self->{state};
414 40009 ▶ $self->{previous_output} = $state->{output} // $self->{states_list}{ $self->{state} }{output} // '';
Condition40%2/5
($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // ""
l!l
400090
$state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}
l!l&&r!l&&!r
0400090
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
415 40009 ▶ $self->{state} = $state->{matching} // $self->{state};
Condition66%2/3
$state->{"matching"} // $self->{"state"}
l!l&&r!l&&!r
5400040
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
416 40009 ▶ $output .= $state->{output} // $self->{states_list}{ $self->{state} }{output} // '';
Condition40%2/5
($state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}) // ""
l!l
400090
$state->{"output"} // $self->{"states_list"}{$self->{"state"}}{"output"}
l!l&&r!l&&!r
0400090
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
417 40009 ▶ if (exists $state->{cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"cmd"}) 0 40009 40009
418 0 my $cmd_state = delete $state->{cmd};
419 0 $cmd_state =~ s/\$in/$in/g;
420 0 push(@{ $self->{cmd_stack} }, $cmd_state);
421 }
422 40009 ▶ if (exists $state->{cmd_exec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"cmd_exec"}) 0 40009 40009
423 0 my $cmd_exec = join ' ', @{ $self->{cmd_stack} };
424 0 my $string = `$cmd_exec`;
425 0 $output = sprintf("%s", $string) . $output;
426 0 $self->{cmd_exec} = [];
427 }
428 40009 ▶ if (exists $state->{exec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"exec"}) 0 40009 40009
429 0 my $old_exec = $state->{exec};
430 0 $state->{exec} =~ s/__IN__/$in/g;
431 0 foreach my $k (keys %{ $self->{cmd_regex} }) {
432 0 my $v = $self->{cmd_regex}{$k};
433 0 my $K = '__' . $k . '__';
434 0 $state->{exec} =~ s/$K/$v/g;
435 }
436 0 my $string = `$state->{exec}`;
437 0 $output = sprintf("%s", $string) . $output;
438 0 $state->{exec} = $old_exec;
439 }
440 40009 ▶ if (exists $state->{do}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"do"}) 0 40009 40009
441 0 my $old_do = $state->{do};
442 0 $state->{do} =~ s/__IN__/$in/g;
443 0 foreach my $k (keys %{ $self->{cmd_regex} }) {
444 0 my $v = $self->{cmd_regex}{$k};
445 0 my $K = '__' . $k . '__';
446 0 $state->{do} =~ s/$K/$v/g;
447 }
448 0 $output = (eval { $state->{do} }) . $output;
449 0 $state->{do} = $old_do;
450 }
451 40009 ▶ if (exists $state->{cat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"cat"}) 1 40008 40009
452 1 my $old_cat = $state->{cat};
453 1 $state->{cat} =~ s/__IN__/$in/g;
454 1 foreach my $k (keys %{ $self->{cmd_regex} }) {
455 0 my $v = $self->{cmd_regex}{$k};
456 0 my $K = '__' . $k . '__';
457 0 $state->{cat} =~ s/$K/$v/g;
458 }
459 1 my $string = do { local (@ARGV, $/) = $state->{cat}; <> };
460 1 $output = sprintf("%s", $string) . $output;
461 1 $state->{cat} = $old_cat;
462 }
463 40009 ▶ if (exists $state->{catRAND}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"catRAND"}) 20000 20009 40009
464 20000 my $old_cat = $state->{catRAND};
465 20000 $state->{catRAND} =~ s/__IN__/$in/g;
466 20000 foreach my $k (keys %{ $self->{cmd_regex} }) {
467 0 my $v = $self->{cmd_regex}{$k};
468 0 my $K = '__' . $k . '__';
469 0 $state->{catRAND} =~ s/$K/$v/g;
470 }
471 20000 my @files = split /\s+/, $state->{catRAND};
472 20000 my $file = $files[ rand @files ];
473 20000 my $string = do { local (@ARGV, $/) = $file; <> };
474 20000 $output = sprintf("%s", $string) . $output;
475 20000 $state->{catRAND} = $old_cat;
476 }
477 40009 ▶ if (exists $state->{catWRAND}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"catWRAND"}) 20000 20009 40009
478 20000 my $old_cat = $state->{catWRAND};
479 20000 $state->{catWRAND} =~ s/__IN__/$in/g;
480 20000 foreach my $k (keys %{ $self->{cmd_regex} }) {
481 0 my $v = $self->{cmd_regex}{$k};
482 0 my $K = '__' . $k . '__';
483 0 $state->{catWRAND} =~ s/$K/$v/g;
484 }
485 20000 my %files = map { split /:/ } split /\s+/, $state->{catWRAND};
486 20000 my $file;
487 my $weight;
488 20000 ▶ while (my ($p, $w) = each %files) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %files) 40000 20000 60000
489 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
490 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
491 40000 ▶ $file = $p if rand($weight) < $w;
Branch: 1 branch
BranchTrueFalseTotal
if rand $weight < $w 29961 10039 40000
492 }
493 20000 my $string = do { local (@ARGV, $/) = $file; <> };
494 20000 $output = sprintf("%s", $string) . $output;
495 20000 $state->{catWRAND} = $old_cat;
496 }
497 40009 ▶ if (exists $state->{catSEQ}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{"catSEQ"}) 3 40006 40009
498 3 my $old_cat = $state->{catSEQ};
499 3 my $state_file;
500 3 ▶ if (exists $state->{catSEQ_idx}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{'catSEQ_idx'}) { } 3 0 3
501 3 $state_file = $state->{catSEQ_idx};
502 } else {
503 0 $state_file = $old_cat . '.state';
504 0 $state_file =~ s/\s/_/g;
505  
506 }
507 3 $state->{catSEQ} =~ s/__IN__/$in/g;
508 3 foreach my $k (keys %{ $self->{cmd_regex} }) {
509 0 my $v = $self->{cmd_regex}{$k};
510 0 my $K = '__' . $k . '__';
511 0 $state->{catSEQ} =~ s/$K/$v/g;
512 }
513 3 my @files = split /\s+/, $state->{catSEQ};
514 3 tie my $nbr => 'FSM::Basic::Modulo', scalar @files, 0;
515 3 ▶ if (-f $state_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $state_file) 2 1 3
516 2 $nbr = do {
517 2 local (@ARGV, $/) = $state_file;
518 2 <>;
519 };
520 }
521 3 my $file = $files[ $nbr++ ];
522 3 my $string = do { local (@ARGV, $/) = $file; <> };
523 3 $output = sprintf("%s", $string) . $output;
524 3 $state->{catSEQ} = $old_cat;
525 3 write_file($state_file, $nbr);
526 }
527 } else {
528 0 $self->{previous_state} = $self->{state};
529 0 ▶ $self->{state} = $self->{states_list}{ $self->{state} }{not_matching} // $self->{state};
Condition0%0/3
$self->{"states_list"}{$self->{"state"}}{"not_matching"} // $self->{"state"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"states_list"}{$self->{"state"}}{"not_matching"} // $self->{"state"}
$self->{"states_list"}{$self->{"state"}}{"not_matching"} $self->{"state"}
Truth table0%0/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{"not_matching"}B= $self->{"state"}
ABresult
1X1
011
000
530 $self->{states_list}{ $self->{state} }{repeat}--
531 0 ▶ if exists $self->{states_list}{ $self->{state} }{repeat};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"states_list"}{$self->{"state"}}{"repeat"} 0 0 0
532 0 ▶ $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
533 0 ▶ 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 0 0
534 0 $output = $self->{states_list}{ $self->{state} }{not_matching_info} . "\n" . $output;
535 }
536 0 ▶ return ($self->{states_list}{ $self->{state} }{$in}{final} // $self->{states_list}{ $self->{state} }{final} // 0, $output);
Condition0%0/5
($self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}) // 0
l!l
00
$self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}
l!l&&r!l&&!r
000
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 table0%0/3
A // B
A= $self->{"states_list"}{$self->{"state"}}{$in}{"final"}B= $self->{"states_list"}{$self->{"state"}}{"final"}
ABresult
1X1
011
000
537 }
538 40009 ▶ 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
040009
$self->{"states_list"}{$self->{"state"}}{$in}{"final"} // $self->{"states_list"}{$self->{"state"}}{"final"}
l!l&&r!l&&!r
0040009
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
539 }
540 }
541 }
542  
543 sub set {
544 0 ▶ my ($self, $in) = @_;
Subroutine set: not called CC=3 SCAR=24.8
Pod undocumented
545 0 $self->{previous_state} = $self->{state};
546 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
547 0 ▶ $self->{state} = $in if exists $self->{states_list}{$in};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"states_list"}{$in} 0 0 0
548 }
549  
550 sub write_file {
551 3 ▶ my ($file, $content) = @_;
Subroutine write_file: called CC=3 SCAR=11.4
Pod undocumented
552 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
553 3 print $fh $content;
554 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
555 }
556  
557 sub alter {
558 0 ▶ my $r = shift;
Subroutine alter: not called CC=5 SCAR=34.0
Pod undocumented
559 0 ▶ my $c = shift // 0;
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
560 0 $r =~ /([^[]*)\[([^\]]+)\](.*)/;
561 0 my $pre_r = $1;
562 0 my $match_r = $2;
563 0 my $post_r = $3;
564 0 my $out = $pre_r.'(';
565 0 ▶ for (my $i = 1 ; $i <= length $match_r ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= length $match_r) 0 0 0
566 0 $out .= substr($match_r, 0, $i) . '|';
567 }
568 0 chop $out;
569 0 ▶ $post_r = alter($post_r) if $post_r=~ /\[([^\]]+)\]/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $post_r =~ /\[([^\]]+)\]/ 0 0 0
570 0 $out = $out. ')' . $post_r;
571 0 ▶ $out = '(?i:' . $out. ')' if $c ;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
572 0 return $out;
573 }
574  
575 package FSM::Basic::Modulo;
576 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
577 6 ▶sub FETCH { ${ $_[0] }[0] }
Subroutine FETCH: called CC=1 SCAR=0.0
578 5 ▶sub STORE { ${ $_[0] }[0] = $_[1] % ${ $_[0] }[1] }
Subroutine STORE: called CC=1 SCAR=0.0
579 6 1;
580  
581 =back
582  
583 B<EXAMPLE>
584  
585  
586 use strict;
587 use feature qw( say );
588 use FSM::Basic;
589 use JSON;
590 use Term::ReadLine;
591  
592 my %states = (
593 'accept' => {
594 'expect' => {
595 'default' => {
596 'final' => 0,
597 'matching' => 'prompt'
598 }
599 },
600 'not_matching' => 'accept',
601 'not_matching0' => 'close',
602 'not_matching_info_last' => '% Bad passwords
603 ',
604 'output' => 'Password: ',
605 'repeat' => 2
606 },
607  
608 'close' => {'final' => 1},
609 'prompt' => {
610 'expect' => {
611 'not_matching' => 'prompt',
612 'exit' => {
613 'matching' => 'close',
614 'final' => 0
615 },
616 "read" => {'cat' => 'file.txt'},
617 "read_random" => {'catRAND' => 'file1.txt file2.txt file3.txt'},
618 "read_seq" => {'catSEQ' => 'file1.txt file2.txt file3.txt', 'catSEQ_idx' => 'catSEQ_status'},
619 'meminfo' => {'do' => 'do { local( @ARGV, $/ ) = "/proc/meminfo" ; <> }'},
620 'mem' => {
621 '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);"
622 },
623 'h(elp)?|\\?' => {
624 'output' => 'exit
625 read
626 read_random
627 read_seq
628 meminfo
629 mem_usage
630 mem
631 User> '
632 },
633 '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); '},
634 },
635 'not_matching_info' => '% Unknown command or computer name, or unable to find computer address',
636 'output' => 'User> '
637 }
638 );
639 my $history_file = glob( '/tmp/fsm.history' );
640 my $prompt = '> ';
641 my $line;
642 my $final = 0;
643 my $term = new Term::ReadLine 'bash';
644 my $attribs = $term->Attribs->ornaments( 0 );
645 $term->using_history();
646 $term->read_history( $history_file );
647 $term->clear_signals();
648  
649 my $fsm = FSM::Basic->new( \%states, 'accept' );
650 my $out = "Password> ";
651 while ( defined( $line = $term->readline( $out ) ) )
652 {
653 ( $final, $out ) = $fsm->run( $line );
654 $term->write_history( $history_file );
655 last if $final;
656 }
657  
658 print $out if $final;
659  
660  
661 More sample code in the examples folder.
662  
663  
664 TODO
665  
666 add "edit" to allow on the fly modification of the states definition
667  
668 add "verify_states" to check all states are reachable from a original state
669  
670 B<SEE ALSO>
671  
672 FSA::Rules
673  
674 https://metacpan.org/pod/FSA::Rules
675  
676  
677 B<AUTHOR>
678  
679 DULAUNOY Fabrice, C<< <fabrice at dulaunoy.com> >>
680  
681 B<BUGS>
682  
683 Please report any bugs or feature requests to C<bug-FSM-basic at rt.cpan.org>, or through
684 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FSM-Basic>. I will be notified, and then you'll
685 automatically be notified of progress on your bug as I make changes.
686  
687 B<SUPPORT>
688  
689 You can find documentation for this module with the perldoc command.
690  
691 perldoc FSM::Basic
692  
693  
694 You can also look for information at:
695  
696 =over 4
697  
698 =item * RT: CPAN's request tracker (report bugs here)
699  
700 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FSM-Basic>
701  
702 =item * AnnoCPAN: Annotated CPAN documentation
703  
704 L<http://annocpan.org/dist/FSM-Basic>
705  
706 =item * CPAN Ratings
707  
708 L<http://cpanratings.perl.org/d/FSM-Basic>
709  
710 =item * Search CPAN
711  
712 L<http://search.cpan.org/dist/FSM-Basic/>
713  
714 =back
715  
716  
717 B<ACKNOWLEDGEMENTS>
718  
719  
720 B<LICENSE AND COPYRIGHT>
721  
722 Copyright 2008 - 2020 DULAUNOY Fabrice.
723  
724 This program is free software; you can redistribute it and/or modify it
725 under the terms of the the Artistic License (2.0). You may obtain a
726 copy of the full license at:
727  
728 L<http://www.perlfoundation.org/artistic_license_2_0>
729  
730 Any use, modification, and distribution of the Standard or Modified
731 Versions is governed by this Artistic License. By using, modifying or
732 distributing the Package, you accept this license. Do not use, modify,
733 or distribute the Package, if you do not accept this license.
734  
735 If your Modified Version has been derived from a Modified Version made
736 by someone other than you, you are nevertheless required to ensure that
737 your Modified Version complies with the requirements of this license.
738  
739 This license does not grant you the right to use any trademark, service
740 mark, tradename, or logo of the Copyright Holder.
741  
742 This license includes the non-exclusive, worldwide, free-of-charge
743 patent license to make, have made, use, offer to sell, sell, import and
744 otherwise transfer the Package with respect to any patent claims
745 licensable by the Copyright Holder that are necessarily infringed by the
746 Package. If you institute patent litigation (including a cross-claim or
747 counterclaim) against any party alleging that the Package constitutes
748 direct or contributory patent infringement, then this Artistic License
749 to you shall terminate on the date that such litigation is filed.
750  
751 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
752 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
753 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
754 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
755 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
756 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
757 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
758 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
759  
760  
761 =cut
762  
763 6 1; # End of FSM::Basic
↑ Summary