Add man1/clive.1.pod, simplify bin/clive POD
[clive.git] / lib / clive / Exec.pm
blob37009d692b95ac6f2678b44517419cf802fe6396
1 # -*- coding: ascii -*-
2 ###########################################################################
3 # clive, command line video extraction utility.
5 # Copyright 2009 Toni Gundogdu.
7 # This file is part of clive.
9 # clive is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU General Public License as published by the Free
11 # Software Foundation, either version 3 of the License, or (at your option)
12 # any later version.
14 # clive is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program. If not, see <http://www.gnu.org/licenses/>.
21 ###########################################################################
22 package clive::Exec;
24 use warnings;
25 use strict;
27 use base 'Class::Singleton';
29 use clive::Error qw(CLIVE_OK CLIVE_OPTARG CLIVE_SYSTEM);
31 sub init {
32 my $self = shift;
33 $self->{exec_queue} = [];
34 $self->{stream_flag} = 0;
35 $self->{stream_pid} = -1;
37 my $config = clive::Config->instance->config;
38 if ( $config->{exec} ) {
39 if ( $config->{exec} !~ /[;+]$/ ) {
40 clive::Log->instance->err( CLIVE_OPTARG,
41 "--exec expression must be "
42 . "terminated by either ';' or '+'" );
43 exit(CLIVE_OPTARG);
47 if ( !$config->{exec} && $config->{exec_run} ) {
48 clive::Log->instance->err( CLIVE_OPTARG,
49 "--exec-run depends on --exec" );
50 exit(CLIVE_OPTARG);
54 sub queue {
55 my $self = shift;
56 if (@_) {
57 my $config = clive::Config->instance->config;
58 if ( $config->{exec} && $config->{exec_run} ) {
59 my $props = shift;
60 push( @{ $self->{exec_queue} }, $$props->filename );
63 return $self->{exec_queue};
66 sub runExec {
67 my $config = clive::Config->instance->config;
68 return if !$config->{exec_run};
70 my $self = shift;
71 if ( $config->{exec} =~ /;$/ ) { # Semi
72 foreach ( @{ $self->{exec_queue} } ) {
73 my $cmd = $config->{exec};
74 $cmd =~ s/%i/"$_"/g;
75 $cmd =~ tr{;}//d;
76 system("$cmd");
79 else { # Plus
80 my $cmd = sprintf( "%s ", $config->{exec} );
81 $cmd =~ s/%i//g;
82 $cmd =~ tr{+}//d;
83 $cmd .= sprintf( '"%s" ', $_ ) foreach ( @{ $self->{exec_queue} } );
84 system("$cmd");
88 sub resetStream {
89 my $self = shift;
91 waitpid( $self->{stream_pid}, 0 )
92 if $self->{stream_flag};
94 $self->{stream_flag} = 0;
95 $self->{stream_pid} = -1;
98 sub runStream {
99 my ( $self, $percent, $props ) = @_;
100 my $config = clive::Config->instance->config;
101 if ( $config->{stream}
102 && $config->{stream_exec}
103 && !$self->{stream_flag} )
105 _forkStreamer( $self, \$config, $props )
106 if ( $percent >= $config->{stream} );
110 sub passStream {
111 my ( $self, $props ) = @_;
113 my $config = clive::Config->instance->config;
114 my $cmd = $config->{stream_exec};
115 my $lnk = $$props->video_link;
117 $cmd =~ s/%i/"$lnk"/g;
119 my $log = clive::Log->instance;
120 $log->out("stream ...");
122 my $n = system($cmd);
124 if ( $n == 0 ) {
125 $log->out("done.\n");
127 elsif ( $n == -1 ) {
128 $log->errn( CLIVE_SYSTEM, "failed to execute: `$!'" );
130 else {
131 $log->errn( CLIVE_SYSTEM, "child exited with: " . ( $n >> 8 ) );
135 sub _forkStreamer {
136 my ( $self, $config, $props ) = @_;
138 $self->{stream_flag} = 1;
139 my $child = fork;
140 if ( $child < 0 ) {
141 clive::Log->instance->errn( CLIVE_SYSTEM, "fork: $!" );
143 elsif ( $child == 0 ) {
144 my $cmd = $$config->{stream_exec};
145 my $fname = $$props->filename;
146 $cmd =~ s/%i/"$fname"/g;
147 system("$cmd");
148 exit(CLIVE_OK);
154 # Businessmen they, they drink my wine.