Salome HOME
Added support for Microsoft Visual C++ (also experimental)
[tools/libbatch.git] / src / Local / Batch_BatchManager_Local_SH.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  * BatchManager_Local_SH.cxx :
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
27  * Date   : Thu Nov  6 10:17:22 2003
28  * Projet : Salome 2
29  *
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #  include <SALOMEconfig.h>
34 #endif
35
36 #include <iostream>
37 #include <fstream>
38 #include <sstream>
39 #include <cstdlib>
40 #include <sys/types.h>
41 #ifndef WIN32
42 #include <sys/wait.h>
43 #include <unistd.h>
44 #endif
45 #include <ctime>
46 #include <pthread.h>
47 #include <signal.h>
48 #include <errno.h>
49 #include <string.h>
50 #include "Batch_IOMutex.hxx"
51 #include "Batch_BatchManager_Local_SH.hxx"
52
53 #include "Batch_config.h"
54
55 #ifndef RM
56 #error "RM undefined. You must set RM to a valid path to a rm-like command."
57 #endif
58
59 #ifndef CP
60 #error "CP undefined. You must set CP to a valid path to a cp-like command."
61 #endif
62
63 #ifndef SH
64 #error "SH undefined. You must set SH to a valid path to a sh-like command."
65 #endif
66
67 using namespace std;
68
69 namespace Batch {
70
71   // Simple method to fix path strings depending on the platform. On Windows, it will replace
72   // forward slashes '/' by backslashes '\'. On Unix, the path is just copied without change.
73   string BatchManager_Local_SH::fixPath(const string & path) const
74   {
75     string fixedPath = path;
76 #ifdef WIN32
77     for (int i=0 ; i<fixedPath.size() ; i++) {
78       if (fixedPath[i] == '/') fixedPath[i] = '\\';
79     }
80 #endif
81     return fixedPath;
82   }
83
84   // Constructeur
85   BatchManager_Local_SH::BatchManager_Local_SH(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager_Local(parent, host)
86   {
87   }
88
89   // Destructeur
90   BatchManager_Local_SH::~BatchManager_Local_SH()
91   {
92   }
93
94
95   // Methode qui renvoie la commande de copie du fichier source en destination
96   string BatchManager_Local_SH::copy_command(const std::string & user_source,
97                                              const std::string & host_source,
98                                              const std::string & source,
99                                              const std::string & user_destination,
100                                              const std::string & host_destination,
101                                              const std::string & destination) const
102   {
103     ostringstream copy_cmd;
104     if (strchr(CP, ' ') == NULL)
105       copy_cmd << CP;
106     else
107       copy_cmd << "\"" << CP << "\"";
108     copy_cmd << " \"" << fixPath(source) << "\" \"" << fixPath(destination) << "\"";
109     return copy_cmd.str();
110   }
111
112   // Methode qui renvoie la commande a executer
113   string BatchManager_Local_SH::exec_command(Parametre & param) const
114   {
115     ostringstream exec_sub_cmd;
116 #ifdef WIN32
117     exec_sub_cmd << "\"";
118 #endif
119 #ifdef SH_COMMAND_IS_CMD
120     char drive[_MAX_DRIVE];
121     _splitpath_s(fixPath(param[WORKDIR]).c_str(), drive, _MAX_DRIVE, NULL, 0, NULL, 0, NULL, 0);
122     if (strlen(drive) > 0) exec_sub_cmd << drive << " && ";
123     exec_sub_cmd << "cd " << fixPath(param[WORKDIR]) << " && " << fixPath(param[EXECUTABLE]);
124 #else
125     exec_sub_cmd << "cd " << param[WORKDIR] << " && " << param[EXECUTABLE];
126 #endif
127
128     if (param.find(ARGUMENTS) != param.end()) {
129       Versatile V = param[ARGUMENTS];
130       for(Versatile::const_iterator it=V.begin(); it!=V.end(); it++) {
131         StringType argt = * static_cast<StringType *>(*it);
132         string     arg  = argt;
133         exec_sub_cmd << " " << arg;
134       }
135     }
136 #ifdef WIN32
137     exec_sub_cmd << "\"";
138 #endif
139
140 #ifdef SH_COMMAND_IS_CMD
141     param[ARGUMENTS]  = "/c";
142 #else
143     param[ARGUMENTS]  = "-c";
144 #endif
145     param[ARGUMENTS] += exec_sub_cmd.str();
146
147     return fixPath(SH);
148   }
149
150   // Methode qui renvoie la commande d'effacement du fichier
151   string BatchManager_Local_SH::remove_command(const std::string & user_destination,
152                                                const std::string & host_destination,
153                                                const std::string & destination) const
154   {
155     ostringstream remove_cmd;
156     if (strchr(RM, ' ') == NULL)
157       remove_cmd << RM;
158     else
159       remove_cmd << "\"" << RM << "\"";
160
161     remove_cmd << " \"" << fixPath(destination) << "\"";
162     return remove_cmd.str();
163   }
164
165 }