Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / threadexec.C
blob03cf0e97f48fb7cabecdd4dd1229e85653dbc0ec
1 #include "mutex.h"
2 #include "threadexec.h"
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
10 #define MAX_ARGS 32
16 ThreadExec::ThreadExec()
17  : Thread()
19         Thread::set_synchronous(1);
20         arguments = new char*[MAX_ARGS];
21         total_arguments = 0;
22         start_lock = new Mutex;
23         stdin_fd = 0;
24         pipe_stdin = 0;
25         command_line = "";
28 ThreadExec::~ThreadExec()
29 {               
30         if(pipe_stdin)
31         {
32                 fflush(stdin_fd);
33                 fclose(stdin_fd);
34                 close(filedes[0]);
35                 close(filedes[1]);
36         }
38         Thread::join();
40         for(int i = 0; i < total_arguments; i++)
41                 delete [] arguments[i];
42         
43         delete start_lock;
44         delete [] arguments;
49 void ThreadExec::start_command(char *command_line, int pipe_stdin)
51         this->command_line = command_line;
52         this->pipe_stdin = pipe_stdin;
54         Thread::start();
56         start_lock->lock();
57         start_lock->lock();
58         start_lock->unlock();
62 void ThreadExec::run()
64         char *path_ptr;
65         char *ptr;
66         char *argument_ptr;
67         char argument[BCTEXTLEN];
68         char command_line[BCTEXTLEN];
70         strcpy(command_line, this->command_line);
74 // Set up arguments for exec
75         ptr = command_line;
76         path_ptr = path;
77         while(*ptr != ' ' && *ptr != 0)
78         {
79                 *path_ptr++ = *ptr++;
80         }
81         *path_ptr = 0;
83         arguments[total_arguments] = new char[strlen(path) + 1];
84         strcpy(arguments[total_arguments], path);
85 //printf("%s\n", arguments[total_arguments]);
86         total_arguments++;
87         arguments[total_arguments] = 0;
89         while(*ptr != 0)
90         {
91                 ptr++;
92                 argument_ptr = argument;
93                 while(*ptr != ' ' && *ptr != 0)
94                 {
95                         *argument_ptr++ = *ptr++;
96                 }
97                 *argument_ptr = 0;
98 //printf("%s\n", argument);
100                 arguments[total_arguments] = new char[strlen(argument) + 1];
101                 strcpy(arguments[total_arguments], argument);
102                 total_arguments++;
103                 arguments[total_arguments] = 0;
104         }
115         if(pipe_stdin)
116         {
117                 pipe(filedes);
118                 stdin_fd = fdopen(filedes[1], "w");
119         }
121         start_lock->unlock();
123 printf("ThreadExec::run 1\n");
124         run_program(total_arguments, arguments, filedes[0]);
126 printf("ThreadExec::run 2\n");
132 FILE* ThreadExec::get_stdin()
134         return stdin_fd;
139 void ThreadExec::run_program(int argc, char *argv[], int stdin_fd)
143 //      Local Variables:
144 //      mode: C++
145 //      c-file-style: "linux"
146 //      End: