File Coverage

blib/lib/Psh/Joblist.pm
Criterion Covered Total %
statement 3 62 4.8
branch 0 30 0.0
condition 0 3 0.0
subroutine 1 13 7.6
pod 0 10 0.0
total 4 118 3.3


line stmt bran cond sub pod time code
1             package Psh::Joblist;
2              
3 1     1   4 use strict;
  1         2  
  1         835  
4             require Psh::OS;
5              
6             my @jobs_order=();
7             my %jobs_list=();
8              
9             sub create_job {
10 0     0 0   my ($pid, $call, $assoc_obj) = @_;
11              
12 0           my $job = new Psh::Job( $pid, $call, $assoc_obj);
13 0           $jobs_list{$pid}=$job;
14 0           push(@jobs_order,$job);
15 0           return $job;
16             }
17              
18             sub delete_job {
19 0     0 0   my ($pid) = @_;
20              
21 0           my $job= $jobs_list{$pid};
22 0 0         return if !defined($job);
23              
24 0           delete $jobs_list{$pid};
25 0           my $i;
26 0           for($i=0; $i <= $#jobs_order; $i++) {
27 0 0         last if( $jobs_order[$i]==$job);
28             }
29              
30 0           splice( @jobs_order, $i, 1);
31             }
32              
33             sub job_exists {
34 0     0 0   my $pid= shift;
35              
36 0           return exists($jobs_list{$pid});
37             }
38              
39             sub get_job {
40 0     0 0   my $pid= shift;
41 0           return $jobs_list{$pid};
42             }
43              
44             sub list_jobs {
45 0     0 0   return @jobs_order;
46             }
47              
48             sub get_job_number {
49 0     0 0   my $pid= shift;
50              
51 0           for( my $i=0; $i<=$#jobs_order; $i++) {
52 0 0         return $i+1 if( $jobs_order[$i]->{pid}==$pid);
53             }
54 0           return -1;
55             }
56              
57             #
58             # $pid=Psh::Joblist::find_job([$jobnumber])
59             # Finds either the job with the specified job number
60             # or the highest numbered not running job and returns
61             # the job or undef is none is found
62             #
63             sub find_job {
64 0     0 0   my $job_to_start= shift;
65              
66 0 0         return $jobs_order[$job_to_start] if defined( $job_to_start);
67              
68 0           for (my $i = $#jobs_order; $i >= 0; $i--) {
69 0           my $job = $jobs_order[$i];
70              
71 0 0         if(!$job->{running}) {
72 0 0         return wantarray?($i,$job):$job;
73             }
74             }
75 0           return undef;
76             }
77              
78             sub find_last_with_name {
79 0     0 0   my ($name, $runningflag) = @_;
80 0           enumerate();
81 0           my $index=0;
82 0           while( my $job= Psh::Joblist::each()) {
83 0 0 0       next if $runningflag && $job->{running};
84 0           my $call= $job->{call};
85 0 0         if ($call=~ m:([^/\s]+)\s*: ) {
    0          
    0          
86 0           $call= $1;
87             } elsif( $call=~ m:/([^/\s]+)\s+.*$: ) {
88 0           $call= $1;
89             } elsif ( $call=~ m:^([^/\s]+): ) {
90 0           $call= $1;
91             }
92 0 0         if( $call eq $name) {
93 0 0         return wantarray?($index,$job->{pid},$job->{call}):$index;
94             }
95 0           $index++;
96             }
97 0 0         return wantarray?():undef;
98             }
99              
100             {
101             my $pointer;
102              
103             #
104             # Resets the enumeration counter for access using "each"
105             #
106             sub enumerate {
107 0     0 0   $pointer=0;
108             }
109              
110             #
111             # Returns the next job
112             #
113             sub each {
114 0 0   0 0   if ($pointer <= $#jobs_order) {
115 0           return $jobs_order[$pointer++];
116             }
117 0           return undef;
118             }
119             }
120              
121             package Psh::Job;
122              
123             #
124             # $job= new Psh::Job( pid, call);
125             # Creates a new Job object
126             # pid is the pid of the object
127             # call is the name of the executed command
128             #
129             sub new {
130 0     0     my ( $class, $pid, $call, $assoc_obj ) = @_;
131 0           my $self = {};
132 0           bless $self, $class;
133 0           $self->{pid}=$pid;
134 0           $self->{call}=$call;
135 0           $self->{running}=1;
136 0           $self->{assoc_obj}=$assoc_obj;
137 0           return $self;
138             }
139              
140             #
141             # $job->run;
142             # Sends SIGCONT to the job and records it running
143             #
144             sub continue {
145 0     0     my $self= shift;
146              
147             # minus sign to wake up the whole group of the child:
148 0 0         if( Psh::OS::has_job_control()) {
149 0           Psh::OS::resume_job($self);
150             }
151 0           $self->{running}=1;
152             }
153              
154             1;
155             __END__