source: gsdl/trunk/common-src/src/lib/gsdltools.cpp@ 17698

Last change on this file since 17698 was 17698, checked in by ak19, 15 years ago

Now the console window no longer appears when calling gsdl_system() method to launch a process.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/**********************************************************************
2 *
3 * gsdltools.cpp --
4 * A component of the Greenstone digital library software
5 * from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 1999 The New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "gsdltools.h"
27#include "fileutil.h"
28
29
30#if defined(GSDL_USE_OBJECTSPACE)
31# include <ospace/std/iostream>
32#elif defined(GSDL_USE_IOS_H)
33# include <iostream.h>
34#else
35# include <iostream>
36using namespace std;
37#endif
38
39
40#if defined(__WIN32__)
41#include <windows.h>
42#include <process.h>
43#endif
44
45#if !defined (__WIN32__)
46#include <sys/utsname.h>
47#include <unistd.h>
48#endif
49
50
51bool littleEndian() {
52 char s[2] = {'\xFE', '\xEF'};
53
54 if (sizeof(unsigned short) == 2)
55 return *(unsigned short*)s == 0xEFFE;
56 else if (sizeof(unsigned int) == 2)
57 return *(unsigned int*)s == 0xEFFE;
58}
59
60text_t dm_safe (const text_t &instring) {
61
62 text_t outstring;
63 text_t::const_iterator here = instring.begin();
64 text_t::const_iterator end = instring.end();
65 while (here != end) {
66 if (*here == '_' || *here == '\\') outstring.push_back('\\');
67 outstring.push_back(*here);
68 ++here;
69 }
70 return outstring;
71}
72
73void dm_js_safe(const text_t &instring, text_t &outstring)
74{
75 text_t::const_iterator here = instring.begin();
76 text_t::const_iterator end = instring.end();
77 while (here != end) {
78 if (*here == '_') outstring.push_back('\\');
79 else if (*here == '\\' || *here == '\'') {
80 outstring.push_back('\\');
81 outstring.push_back('\\');
82 }
83 outstring.push_back(*here);
84 ++here;
85 }
86}
87
88text_t xml_safe(const text_t &text_string)
89{
90 text_t text_string_safe;
91 text_t::const_iterator here = text_string.begin();
92 text_t::const_iterator end = text_string.end();
93 while (here != end) {
94 if (*here == '&') text_string_safe += "&amp;";
95 else if (*here == '<') text_string_safe += "&lt;";
96 else if (*here == '>') text_string_safe += "&gt;";
97 else text_string_safe.push_back(*here);
98 ++here;
99 }
100 return text_string_safe;
101}
102
103
104// gsdl_system creates a new process for the cmd command (which
105// may contain arguments).
106// cmd should contain the full path of the program to run.
107// The child process inherits the environment of the calling
108// process.
109// If sync is true a synchronous call will be made, otherwise
110// an asyncronous call.
111// If sync is true the return value will be the exit code of
112// the child process or -1 if the child process wasn't started.
113// If sync is false the return value will be 0 if the process
114// was started ok or -1 if it failed.
115int gsdl_system (const text_t &cmd, bool sync, ostream &logout) {
116 if (cmd.empty()) return -1;
117 char *cmd_c = cmd.getcstr();
118
119#if defined (__WIN32__)
120 // the windows version - this is implemented this way
121 // to prevent windows popping up all over the place when
122 // we call a console application (like perl)
123 STARTUPINFO ps = {sizeof(STARTUPINFO), NULL, NULL, NULL,
124 0, 0, 0, 0, 0, 0,
125 0, STARTF_USESHOWWINDOW,
126 SW_HIDE, 0, NULL,
127 NULL, NULL, NULL};
128 PROCESS_INFORMATION pi;
129 BOOL res = CreateProcess(NULL,
130 cmd_c,
131 NULL,
132 NULL,
133 FALSE,
134#if defined (GSDL_LOCAL_LIBRARY)
135 NULL,
136#else
137 CREATE_NO_WINDOW, // previously this was: DETACHED_PROCESS,
138 // see http://msdn.microsoft.com/en-us/library/ms682425.aspx
139 // and http://msdn.microsoft.com/en-us/library/ms684863(VS.85).aspx
140#endif
141 NULL,
142 NULL,
143 &ps,
144 &pi);
145
146 if (!res) {
147 logout << "gsdl_system failed to start " << cmd_c
148 << " process, error code " << GetLastError() << endl;
149 delete []cmd_c;
150 return -1;
151 }
152
153 DWORD ret = 0;
154 if (sync) { // synchronous system call
155 // wait until child process exits.
156 WaitForSingleObject(pi.hProcess, INFINITE);
157 // set ret to exit code of child process
158 GetExitCodeProcess(pi.hProcess, &ret);
159 }
160
161 CloseHandle(pi.hProcess);
162 CloseHandle(pi.hThread);
163
164#else
165 // the unix version
166 int ret = 0;
167 if (sync) { // synchronous system call
168 // make sure the command interpreter is found
169 if (system (NULL) == 0) {
170 logout << "gsdl_system failed to start " << cmd_c
171 << " process, command interpreter not found" << endl;
172 delete []cmd_c;
173 return -1;
174 }
175 ret = system (cmd_c);
176
177 } else { // asynchronous system call
178 int pid = fork();
179 if (pid == -1) {
180 delete []cmd_c;
181 return -1;
182 }
183 if (pid == 0) {
184 // child process
185 char *argv[4];
186 argv[0] = "sh";
187 argv[1] = "-c";
188 argv[2] = cmd_c;
189 argv[3] = 0;
190 execv("/bin/sh", argv);
191 }
192 }
193#endif
194
195 delete []cmd_c;
196 return ret;
197}
198
199
200static bool gsdl_setenv_done = false;
201static char* retain_gsdlosc = NULL;
202static char* retain_gsdlhomec = NULL;
203static char* retain_pathc = NULL;
204
205bool set_gsdl_env_vars (const text_t& gsdlhome)
206{
207 if (gsdl_setenv_done) { return true; }
208
209 // set up GSDLOS, GSDLHOME and PATH environment variables
210
211 text_t gsdlos, path;
212 unsigned int path_separator = ':';
213
214#if defined (__WIN32__)
215 gsdlos = "windows";
216 path_separator = ';';
217
218 path = filename_cat (gsdlhome, "bin", "windows", "perl", "bin;");
219
220#else
221 struct utsname *buf = new struct utsname();
222 int i = uname (buf);
223 if (i == -1) gsdlos = "linux"; // uname failed, default to linux
224 else gsdlos.setcstr (buf->sysname);
225 delete buf;
226 lc (gsdlos);
227#endif
228
229 // according to getenv documentation (after a bit of digging), *getenv*
230 // is responsible for the char* pointer returned, so no need for us
231 // to free it (in fact that would be a mistake!)
232
233 char* orig_pathc = getenv ("PATH");
234 path += filename_cat (gsdlhome, "bin", gsdlos);
235 path.push_back (path_separator);
236 path += filename_cat (gsdlhome, "bin", "script");
237 if (orig_pathc != NULL) {
238 path.push_back (path_separator);
239 path += orig_pathc;
240 }
241
242 text_t putpath = "PATH=" + path;
243 text_t putgsdlos = "GSDLOS=" + gsdlos;
244 text_t putgsdlhome = "GSDLHOME=" + gsdlhome;
245
246 retain_gsdlosc = putgsdlos.getcstr();
247 retain_gsdlhomec = putgsdlhome.getcstr();
248 retain_pathc = putpath.getcstr();
249
250 if ((putenv(retain_gsdlosc)!=0) || (putenv(retain_gsdlhomec)!=0)
251 || (putenv(retain_pathc)!=0))
252 {
253 // Would be better if the ostream& logout was used here.
254 cerr << "Error setting Greenstone environment variables with putenv()" << endl;
255 // perror("putenv(...): "); // This didn't yield any noticable output under Windows running local library server
256 return false;
257 }
258
259 // Need to keep memory allocated setgsdlosc, setgsdlhomec or pathc around
260 // (i.e. can't delete them). This is because putenv() doesn't take
261 // a copy, but places them in the actual environment.
262
263 gsdl_setenv_done = true;
264
265 return true;
266}
267
268
269
270// attempts to work out if perl is functional
271bool perl_ok (ostream &logout) {
272#if defined(__WIN32__)
273 text_t cmd = "perl -e \"exit 0\"";
274#else
275 text_t cmd = "perl -e 'exit 0'";
276#endif
277 int i = gsdl_system (cmd, true, logout);
278 return (i == 0);
279}
Note: See TracBrowser for help on using the repository browser.