| 1 | package Psh::Builtins::Jobs; | ||
| 2 | |||
| 3 | require Psh::Util; | ||
| 4 | require Psh::Joblist; | ||
| 5 | require Psh::OS; | ||
| 6 | require Getopt::Std; | ||
| 7 | |||
| 8 | =item * C<jobs [-rs] [-p]> | ||
| 9 | |||
| 10 | List the currently running jobs. | ||
| 11 | |||
| 12 | Option B<-r> restricts the output to display only currently running jobs. | ||
| 13 | Option B<-s> will only show currently stopped jobs | ||
| 14 | |||
| 15 | If you specify option B<-p> only the PIDs of the processes are displayed. | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | sub bi_jobs { | ||
| 20 | my $line= shift; | ||
| 21 | local @ARGV = @{shift()}; | ||
| 22 | |||
| 23 | if( ! Psh::OS::has_job_control()) { | ||
| 24 | Psh::Util::print_error_i18n('no_jobcontrol'); | ||
| 25 | return (0,undef); | ||
| 26 | } | ||
| 27 | my $opt={}; | ||
| 28 | Getopt::Std::getopts('prs',$opt); | ||
| 29 | |||
| 30 | my $result = ''; | ||
| 31 | my $job; | ||
| 32 | my $visindex=0; | ||
| 33 | |||
| 34 | Psh::Joblist::enumerate(); | ||
| 35 | |||
| 36 | while( ($job=Psh::Joblist::each())) { | ||
| 37 | my $pid = $job->{pid}; | ||
| 38 | my $command = $job->{call}; | ||
| 39 | $visindex++; | ||
| 40 | |||
| 41 | next if $opt->{'r'} and !$job->{running}; | ||
| 42 | next if $opt->{'s'} and $job->{running}; | ||
| 43 | |||
| 44 | if ($opt->{'p'}) { # print pid's only | ||
| 45 | $result.= "$pid\n"; | ||
| 46 | } else { | ||
| 47 | $result .= "[$visindex] $pid $command"; | ||
| 48 | |||
| 49 | if ($job->{running}) { $result .= "\n"; } | ||
| 50 | else { $result .= ' ('.Psh::Locale::get_text('stopped').")\n"; } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | if (!$result) { | ||
| 55 | Psh::Util::print_out_i18n('bi_jobs_none'); | ||
| 56 | return (0,undef); | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | Psh::Util::print_out($result); | ||
| 60 | return (1,undef); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; |