Show
Ignore:
Timestamp:
27.05.2010 11:32:46 (10 years ago)
Author:
davidb
Message:

Further expansion of functionality (and testing) or os_process classes

Location:
main/trunk/greenstone2/runtime-src/src/recpt
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process.cpp

    r22177 r22183  
    2626#include "os_process.h" 
    2727 
    28 osprocess::osprocess(char* cmdline, OSProcessPipeMode mode) 
     28osprocess::osprocess(OSProcessPipeMode mode, 
     29             char* prog_name, char* argv[], char* envp[]) 
    2930  : mode_(mode) 
    3031{} 
     
    5455using namespace std; 
    5556 
     57 
     58 
    5659void test_uni_write_pipe() 
    5760{ 
    5861#ifdef __WIN32__ 
    59   char* prog_name = "c:\\Windows\\system32\\java.exe"; 
    60   char* cmd_line = "java"; 
    61    
    62   //char* input_line = "echo hi there\n"; 
    63   //char* prog_name  = "c:\\cygwin\\bin\\cat.exe"; 
    64   //char* cmd_line   = "cat"; 
    65 #else 
    66   char* cmd_line = "/usr/bin/java"; 
    67   //char* cmd_line = "java"; 
    68 #endif 
    69  
    70 #ifdef __WIN32__ 
    71   osprocesswindows* process = new osprocesswindows(cmd_line,uniRead); 
    72 #else 
    73   osprocessunix* process = new osprocessunix(cmd_line,uniRead); 
     62  char* prog_name = "type"; 
     63  char* argv[] = { "type", NULL } ; 
     64   
     65#else 
     66  char* prog_name = "tr"; 
     67  char* argv[] = { "tr", "[A-Z]", "[a-z]", NULL }; 
     68#endif 
     69 
     70  cout << "<hr>" << endl; 
     71  cout << "<p>Unidirectional Write Test" << endl; 
     72  cout << "<p>  Converts uppercase letters to lowercase" << endl; 
     73  cout << "<p>  These are then sent to whereever the stdout of the child process goes" << endl; 
     74  cout << "<p>" << endl; 
     75 
     76#ifdef __WIN32__ 
     77  osprocesswindows* process = new osprocesswindows(uniWrite,prog_name,argv); 
     78#else 
     79  osprocessunix* process = new osprocessunix(uniWrite,prog_name,argv); 
     80#endif 
     81   
     82  char* buffer = "TESTING uniWirite IN CAP\nTesting uniWrite in Mixed Case\n"; 
     83  int buffer_len = strlen(buffer); 
     84 
     85  if (!process->write(buffer,buffer_len)) { 
     86    cerr << "Error: failed to write to child process\n"; 
     87  } 
     88 
     89  process->close(); 
     90 
     91  process->wait(); 
     92 
     93  delete process; 
     94} 
     95 
     96 
     97void test_uni_read_pipe() 
     98{ 
     99  char* prog_name = "java"; 
     100  char* argv[] = { "java", NULL }; 
     101 
     102  cout << "<hr>" << endl; 
     103  cout << "<p>Unidirectional Read Test" << endl; 
     104  cout << "<p>  The output of 'java' is read in by this process and then sent to stdout" << endl; 
     105  cout << "<p>" << endl; 
     106 
     107 
     108#ifdef __WIN32__ 
     109  osprocess* process = new osprocesswindows(uniRead,prog_name,argv); 
     110#else 
     111  osprocess* process = new osprocessunix(uniRead,prog_name,argv); 
    74112#endif 
    75113   
     
    86124 
    87125 
     126  // deleting object will also have the effect of closing any open handles 
    88127  delete process; 
    89128 
    90129} 
     130 
     131 
     132 
     133void test_bi_read_write_pipe() 
     134{ 
     135#ifdef __WIN32__ 
     136  char* prog_name = "type"; 
     137  char* argv[] = NULL ; 
     138   
     139#else 
     140  char* prog_name = "tr"; 
     141  char* argv[] = { "tr", "[A-Z]", "[a-z]", NULL }; 
     142#endif 
     143 
     144  // Converts uppercase letters to lowercase 
     145  // Answer is read back and then printed to STDOUT by this process 
     146 
     147  cout << "<hr>" << endl; 
     148  cout << "<p>Bidirectional Read/Write Test" << endl; 
     149  cout << "<p>  Converts uppercase letters to lowercase" << endl; 
     150  cout << "<p>  Data is sent to child process down pipe by this process" << endl; 
     151  cout << "<p>  Answer is read back from child process and printed to stdout" << endl; 
     152  cout << "<p>" << endl; 
     153 
     154#ifdef __WIN32__ 
     155  osprocesswindows* process = new osprocesswindows(biReadWrite,prog_name,argv); 
     156#else 
     157  osprocessunix* process = new osprocessunix(biReadWrite,prog_name,argv); 
     158#endif 
     159   
     160  char* write_buffer = "TESTING biReadWrite IN CAP\nTesting biReadWrite in Mixed Case\n"; 
     161  int write_buffer_len = strlen(write_buffer); 
     162 
     163  if (!process->write(write_buffer,write_buffer_len)) { 
     164    cerr << "Error: failed to write to child process\n"; 
     165  } 
     166 
     167  const int BufferSize = 1024; 
     168  char read_buffer[BufferSize]; 
     169  process->close_write_pipe(); 
     170 
     171  int bytes_read = 0; 
     172  do { 
     173    bytes_read = process->read(read_buffer,BufferSize); 
     174    if (bytes_read>0) { 
     175      fwrite(read_buffer,1,bytes_read,stdout); 
     176    } 
     177  } while (bytes_read==BufferSize); 
     178 
     179 
     180  process->close(); 
     181 
     182  delete process; 
     183 
     184} 
     185 
    91186 
    92187int main() 
     
    100195  printf( "  <body>\n"); 
    101196 
     197  test_uni_read_pipe(); 
    102198  test_uni_write_pipe(); 
     199  test_bi_read_write_pipe(); 
    103200 
    104201  printf( "  </body>\n"); 
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process.h

    r22177 r22183  
    4242   
    4343 public: 
    44   osprocess(char* cmdline, OSProcessPipeMode mode); 
     44  osprocess(OSProcessPipeMode mode, 
     45        char* prog_name, char* argv[]=NULL, char* envp[]=NULL); 
     46 
    4547  virtual ~osprocess(); 
    4648   
     
    5355  virtual int write(char* buffer, const int buffer_len)=0; 
    5456  virtual int read(char* buffer, const int buffer_len)=0; 
    55    
     57 
     58  virtual void wait()=0; 
     59 
    5660  virtual bool close_write_pipe(OSProcessWarnStatus warn_status=withWarning)=0; 
    5761  virtual bool close_read_pipe(OSProcessWarnStatus warn_status=withWarning)=0; 
     
    6165 protected: 
    6266  OSProcessPipeMode mode_; 
    63  
    6467}; 
    6568 
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.cpp

    r22178 r22183  
    3838 
    3939#include <unistd.h> 
     40#include <sys/wait.h> 
    4041 
    4142#include "os_process_unix.h" 
    4243 
    43 osprocessunix::osprocessunix(char* cmdline, OSProcessPipeMode mode) 
     44osprocessunix::osprocessunix(OSProcessPipeMode mode, 
     45                 char* prog_name, char* argv[], char* envp[]) 
    4446  : child_stdout_read_(-1), child_stdin_write_(-1), 
    45     osprocess(cmdline,mode) 
     47    osprocess(mode,prog_name,argv,envp) 
    4648{ 
    4749   
     
    112114 
    113115     
    114     // execvp? 
    115     execve(cmdline, NULL, NULL); 
    116  
     116    // Used to be execve(), but this would appear not to search path for 
     117    // 'prog_name'. The alternative execvp() does search PATH and so is 
     118    // more compatible with how CreateProcess works for Windows 
     119 
     120    // Need to connect envp with externally defined '**environ' ?? 
     121 
     122    execvp(prog_name,argv); 
    117123     
    118124  } 
     
    178184 
    179185 
    180    
     186void osprocessunix::wait() 
     187{ 
     188  int child_status; 
     189  //cerr << "**** waiting ..." << endl; 
     190  ::waitpid(pid_,&child_status,0); 
     191 
     192  //cerr << "*** exit status = " << WEXITSTATUS(child_status) << endl; 
     193 
     194  //cerr << "**** done" << endl; 
     195} 
     196 
     197 
    181198bool osprocessunix::close_write_pipe(OSProcessWarnStatus warn_status) 
    182199{ 
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.h

    r22177 r22183  
    3434#include "os_process.h" 
    3535 
    36 class osprocessunix : osprocess 
     36class osprocessunix : public osprocess 
    3737{ 
    3838    
    3939 public: 
    40   osprocessunix(char* cmd, OSProcessPipeMode mode); 
     40  osprocessunix(OSProcessPipeMode mode, 
     41        char* prog_name, char* argv[]=NULL, char* envp[]=NULL); 
    4142  virtual ~osprocessunix(); 
    4243   
     
    4445  virtual int write(char* buffer, const int buffer_len); 
    4546   
     47  virtual void wait(); 
     48 
    4649  virtual bool close_write_pipe(OSProcessWarnStatus warn_status=withWarning); 
    4750  virtual bool close_read_pipe(OSProcessWarnStatus warn_status=withWarning); 
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process_windows.cpp

    r22177 r22183  
    4545{} 
    4646 
    47 osprocesswindows::osprocesswindows(char* cmdline, OSProcessPipeMode mode) 
     47osprocesswindows::osprocesswindows(OSProcessPipeMode mode, 
     48                   char* prog_name, char* argv[]=NULL,  
     49                   char* envp[]) 
    4850  : child_stdout_read_(NULL), child_stdin_write_(NULL), 
    49     osprocess(cmdline,mode) 
     51    osprocess(mode,prog_name,argv,envp) 
    5052{ 
    5153  HANDLE child_stdin_read = NULL; 
     
    102104  si.hStdInput = child_stdin_read; 
    103105   
     106  text_t cmd_line = ""; 
     107  if (argv != NULL) { 
     108    int i=0; 
     109    while (argv[i] != NULL) { 
     110      if (i>0) { 
     111    cmd_line += " "; 
     112      } 
     113      cmd_line += "\"" + argv[i] + "\""; 
     114      i++; 
     115    } 
     116  } 
     117 
     118  char* cmd_line_cstr = cmd_line.getcstr(); 
     119 
    104120  int rv = CreateProcess(NULL,    // no application name 
    105              cmdline,  
     121             cmd_line_cstr,  
    106122             NULL,  
    107123             NULL,    // no process or thread security attribues  
     
    112128             &si,  
    113129             &pi_);    // process info filled out as a result 
     130 
     131  delete [] cmd_line_cstr; 
     132 
    114133  if (!rv) { 
    115     cerr << "os_process_windows(): Error creating process" << endl; 
     134    cerr << "os_process_windows(): Error creating child process" << endl; 
    116135  } 
    117136 
     
    119138 
    120139  if (!CloseHandle(child_stdout_write)) { 
    121     cerr <<  "os_process_windows(): Error closing StdOutWr Handle" << endl; 
     140    cerr <<  "os_process_windows(): Error closing child's StdOut Write Handle" << endl; 
    122141  } 
    123142 
    124143  if (!CloseHandle(child_stdin_read)) { 
    125     cerr << "os_process_windows(): Error closing StdInRd Handle" << endl; 
     144    cerr << "os_process_windows(): Error closing child's StdIn Read Handle" << endl; 
    126145  } 
    127146 
     
    170189 
    171190 
     191void osprocesswindows::wait() 
     192{ 
     193  WaitForSingleObject(pi_.hProcess, INFINITE); 
     194} 
    172195 
    173196   
  • main/trunk/greenstone2/runtime-src/src/recpt/os_process_windows.h

    r22177 r22183  
    3535#include <windows.h>  
    3636 
    37 class osprocesswindows : osprocess 
     37class osprocesswindows : public osprocess 
    3838{ 
    3939public: 
    40   osprocesswindows(char* cmdline, OSProcessPipeMode mode); 
     40  osprocesswindows(OSProcessPipeMode mode, 
     41           char* prog_name, char* argv[]=NULL, char* envp[]=NULL); 
    4142  virtual ~osprocesswindows(); 
    4243 
    4344  virtual int read(char* buffer, const int buffer_len); 
    4445  virtual int write(const char* buffer, const int buffer_len); 
     46 
     47  virtual void wait(); 
    4548 
    4649  virtual bool close_write_pipe(OSProcessWarnStatus warn_status);