bincimap

Log | Files | Refs | LICENSE

framework.cc (1761B)


      1 
      2 /*-*-mode:c++-*-*/
      3 #include <unistd.h>
      4 #include <iostream>
      5 #include <signal.h>
      6 #include <wait.h>
      7 #include <errno.h>
      8 #include "framework.h"
      9 #include "../src/convert.h"
     10 #include "../src/regmatch.h"
     11 #include "../src/tools.h"
     12 
     13 using namespace ::std;
     14 using namespace ::Binc;
     15 
     16 
     17 FrameWork::FrameWork(const std::string &binary)
     18 {
     19   pipe(readpipe);
     20   pipe(writepipe);
     21 
     22   cout << "Starting " << binary << "." << endl;
     23 
     24   if ((childspid = fork()) == 0) {
     25     dup2(writepipe[0], 0);
     26     dup2(readpipe[1], 1);
     27     close(writepipe[1]);
     28     close(readpipe[0]);
     29 
     30     char *cwd = getcwd(0, 0);
     31     string dest = cwd;
     32     dest += "/";
     33     dest += binary;
     34 
     35     execl(dest.c_str(), 0);
     36     exit(2);
     37   }
     38 }
     39 
     40 bool FrameWork::test(const std::string &request, const std::string &result)
     41 {
     42   string tmp = request;
     43   trim(tmp);
     44   string tmp2 = result;
     45   trim(tmp2);
     46 
     47   if (request != "") {
     48     ssize_t res = write(writepipe[1], request.c_str(), request.length());
     49     if (res != (ssize_t) request.length()) {
     50       printf("test(\"%s\", \"%s\") failed: %s\n", tmp.c_str(), tmp2.c_str(), strerror(errno));
     51       return false;
     52     }
     53   }
     54   
     55   char c;
     56   string got;
     57 
     58   while (read(readpipe[0], &c, 1) == 1 && c != '\n')
     59     got += c;
     60   got += '\n';
     61 
     62   if (got != result) {
     63     printf("test(\"%s\", \"%s\") failed: got %s\n", tmp.c_str(), tmp2.c_str(), got.c_str());
     64     return false;
     65   }
     66 
     67   printf("test(\"%s\", \"%s\") ok.\n", tmp.c_str(), tmp2.c_str());
     68   return true;
     69 }
     70 
     71 
     72 FrameWork::~FrameWork(void)
     73 {
     74   kill(childspid, SIGTERM);
     75   int result;
     76   wait(&result);
     77 }
     78 
     79 void FrameWork::setConfig(const std::string &section, const string &key, const string &value)
     80 {
     81   Tools::getInstance().setenv("BINCIMAP_GLOBALCONFIG_" 
     82 			      + toHex(section) + "::" + toHex(key),
     83 			      toHex(value));
     84 }