Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00026 #ifndef VCSN_TOOLS_OUT_DISPLAY_HXX
00027 # define VCSN_TOOLS_OUT_DISPLAY_HXX
00028 
00029 # include <vaucanson/tools/out_display.hh>
00030 # include <sys/types.h>
00031 # include <sys/wait.h>
00032 # include <unistd.h>
00033 
00034 namespace vcsn {
00035 
00036   namespace tools {
00037 
00038     template <class S, class T>
00039     bool
00040     out_display(const AutomataBase<S>&,
00041                 const T&,
00042                 const Element<S, T>& a,
00043                 const std::string& name,
00044                 const bool bg,
00045                 const char *const argv[],
00046                 void (*function)(std::ostream& o,
00047                                  const Element<S, T>& a,
00048                                  const std::string& name))
00049 
00050     {
00051       int filedes[2];
00052       if (pipe(filedes))
00053         return false;
00054 
00055       pid_t child_pid = fork();
00056       if (child_pid)
00057         {
00058           close(filedes[0]);
00059           if (child_pid == -1)
00060             {
00061               close(filedes[1]);
00062               return false;
00063             }
00064           else
00065             {
00066               
00067               {
00068                 std::cout.flush();
00069                 int old = dup(STDOUT_FILENO);
00070                 if (old != -1)
00071                   dup2(filedes[1], STDOUT_FILENO);
00072                 close(filedes[1]);
00073 
00074                 function(std::cout, a, name);
00075 
00076                 if (old != -1)
00077                   {
00078                     dup2(old, STDOUT_FILENO);
00079                     close(old);
00080                   }
00081               }
00082               return bg or waitpid(child_pid, NULL, 0) == child_pid;
00083             }
00084         }
00085       else
00086         {
00087           close(filedes[1]);
00088           dup2(filedes[0], STDIN_FILENO);
00089           close(filedes[0]);
00090           
00091           
00092           
00093           
00094           execvp(argv[0], const_cast<char *const *>(argv));
00095           std::cerr << "Failed to execute `" << argv[0] << "'." << std::endl;
00096           exit(1);
00097         }
00098     }
00099 
00100   } 
00101 
00102 } 
00103 
00104 #endif // ! VCSN_TOOLS_OUT_DISPLAY_HXX