Vaucanson 1.4
|
00001 // out_display.hxx: this file is part of the Vaucanson project. 00002 // 00003 // Vaucanson, a generic library for finite state machines. 00004 // 00005 // Copyright (C) 2005, 2006, 2007, 2011 The Vaucanson Group. 00006 // 00007 // This program is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 00010 // of the License, or (at your option) any later version. 00011 // 00012 // The complete GNU General Public Licence Notice can be found as the 00013 // `COPYING' file in the root directory. 00014 // 00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file. 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 // FIXME: That trick is dirty! 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 // execvp will treat its second argument as if it was 00091 // "const char* const*". See the following page for an 00092 // explanation of why it is only declared as "char* const*". 00093 // http://www.opengroup.org/onlinepubs/000095399/functions/exec.html 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 } // End of namespace tools. 00101 00102 } // End of namespace vcsn. 00103 00104 #endif // ! VCSN_TOOLS_OUT_DISPLAY_HXX