00001 #include "mutex.h" 00002 #include "threadexec.h" 00003 00004 #include <stdlib.h> 00005 #include <string.h> 00006 #include <sys/types.h> 00007 #include <sys/wait.h> 00008 #include <unistd.h> 00009 00010 #define MAX_ARGS 32 00011 00012 00013 00014 00015 00016 ThreadExec::ThreadExec() 00017 : Thread() 00018 { 00019 Thread::set_synchronous(1); 00020 arguments = new char*[MAX_ARGS]; 00021 total_arguments = 0; 00022 start_lock = new Mutex; 00023 stdin_fd = 0; 00024 pipe_stdin = 0; 00025 command_line = ""; 00026 } 00027 00028 ThreadExec::~ThreadExec() 00029 { 00030 if(pipe_stdin) 00031 { 00032 fflush(stdin_fd); 00033 fclose(stdin_fd); 00034 close(filedes[0]); 00035 close(filedes[1]); 00036 } 00037 00038 Thread::join(); 00039 00040 for(int i = 0; i < total_arguments; i++) 00041 delete [] arguments[i]; 00042 00043 delete start_lock; 00044 delete [] arguments; 00045 } 00046 00047 00048 00049 void ThreadExec::start_command(char *command_line, int pipe_stdin) 00050 { 00051 this->command_line = command_line; 00052 this->pipe_stdin = pipe_stdin; 00053 00054 Thread::start(); 00055 00056 start_lock->lock(); 00057 start_lock->lock(); 00058 start_lock->unlock(); 00059 } 00060 00061 00062 void ThreadExec::run() 00063 { 00064 char *path_ptr; 00065 char *ptr; 00066 char *argument_ptr; 00067 char argument[BCTEXTLEN]; 00068 char command_line[BCTEXTLEN]; 00069 00070 strcpy(command_line, this->command_line); 00071 00072 00073 00074 // Set up arguments for exec 00075 ptr = command_line; 00076 path_ptr = path; 00077 while(*ptr != ' ' && *ptr != 0) 00078 { 00079 *path_ptr++ = *ptr++; 00080 } 00081 *path_ptr = 0; 00082 00083 arguments[total_arguments] = new char[strlen(path) + 1]; 00084 strcpy(arguments[total_arguments], path); 00085 //printf("%s\n", arguments[total_arguments]); 00086 total_arguments++; 00087 arguments[total_arguments] = 0; 00088 00089 while(*ptr != 0) 00090 { 00091 ptr++; 00092 argument_ptr = argument; 00093 while(*ptr != ' ' && *ptr != 0) 00094 { 00095 *argument_ptr++ = *ptr++; 00096 } 00097 *argument_ptr = 0; 00098 //printf("%s\n", argument); 00099 00100 arguments[total_arguments] = new char[strlen(argument) + 1]; 00101 strcpy(arguments[total_arguments], argument); 00102 total_arguments++; 00103 arguments[total_arguments] = 0; 00104 } 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 if(pipe_stdin) 00116 { 00117 pipe(filedes); 00118 stdin_fd = fdopen(filedes[1], "w"); 00119 } 00120 00121 start_lock->unlock(); 00122 00123 printf("ThreadExec::run 1\n"); 00124 run_program(total_arguments, arguments, filedes[0]); 00125 00126 printf("ThreadExec::run 2\n"); 00127 00128 00129 } 00130 00131 00132 FILE* ThreadExec::get_stdin() 00133 { 00134 return stdin_fd; 00135 } 00136 00137 00138 00139 void ThreadExec::run_program(int argc, char *argv[], int stdin_fd) 00140 { 00141 }
1.4.4