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

Last change on this file since 17545 was 17545, checked in by mdewsnip, 16 years ago

Moved the xml_safe() function from gtiaction into gsdltools, so it can be used by other classes.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 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 DETACHED_PROCESS,
138#endif
139 NULL,
140 NULL,
141 &ps,
142 &pi);
143 if (!res) {
144 logout << "gsdl_system failed to start " << cmd_c
145 << " process, error code " << GetLastError();
146 delete []cmd_c;
147 return -1;
148 }
149
150 DWORD ret = 0;
151 if (sync) { // synchronous system call
152 // wait until child process exits.
153 WaitForSingleObject(pi.hProcess, INFINITE);
154 // set ret to exit code of child process
155 GetExitCodeProcess(pi.hProcess, &ret);
156 }
157
158 CloseHandle(pi.hProcess);
159 CloseHandle(pi.hThread);
160
161#else
162 // the unix version
163 int ret = 0;
164 if (sync) { // synchronous system call
165 // make sure the command interpreter is found
166 if (system (NULL) == 0) {
167 logout << "gsdl_system failed to start " << cmd_c
168 << " process, command interpreter not found\n";
169 delete []cmd_c;
170 return -1;
171 }
172 ret = system (cmd_c);
173
174 } else { // asynchronous system call
175 int pid = fork();
176 if (pid == -1) {
177 delete []cmd_c;
178 return -1;
179 }
180 if (pid == 0) {
181 // child process
182 char *argv[4];
183 argv[0] = "sh";
184 argv[1] = "-c";
185 argv[2] = cmd_c;
186 argv[3] = 0;
187 execv("/bin/sh", argv);
188 }
189 }
190#endif
191
192 delete []cmd_c;
193 return ret;
194}
195
196
197static bool gsdl_setenv_done = false;
198static char* retain_gsdlosc = NULL;
199static char* retain_gsdlhomec = NULL;
200static char* retain_pathc = NULL;
201
202bool set_gsdl_env_vars (const text_t& gsdlhome)
203{
204 if (gsdl_setenv_done) { return true; }
205
206 // set up GSDLOS, GSDLHOME and PATH environment variables
207
208 text_t gsdlos, path;
209 unsigned int path_separator = ':';
210
211#if defined (__WIN32__)
212 gsdlos = "windows";
213 path_separator = ';';
214
215 path = filename_cat (gsdlhome, "bin", "windows", "perl", "bin;");
216
217#else
218 struct utsname *buf = new struct utsname();
219 int i = uname (buf);
220 if (i == -1) gsdlos = "linux"; // uname failed, default to linux
221 else gsdlos.setcstr (buf->sysname);
222 delete buf;
223 lc (gsdlos);
224#endif
225
226 // according to getenv documentation (after a bit of digging), *getenv*
227 // is responsible for the char* pointer returned, so no need for us
228 // to free it (in fact that would be a mistake!)
229
230 char* orig_pathc = getenv ("PATH");
231 path += filename_cat (gsdlhome, "bin", gsdlos);
232 path.push_back (path_separator);
233 path += filename_cat (gsdlhome, "bin", "script");
234 if (orig_pathc != NULL) {
235 path.push_back (path_separator);
236 path += orig_pathc;
237 }
238
239 text_t putpath = "PATH=" + path;
240 text_t putgsdlos = "GSDLOS=" + gsdlos;
241 text_t putgsdlhome = "GSDLHOME=" + gsdlhome;
242
243 retain_gsdlosc = putgsdlos.getcstr();
244 retain_gsdlhomec = putgsdlhome.getcstr();
245 retain_pathc = putpath.getcstr();
246
247 if ((putenv(retain_gsdlosc)!=0) || (putenv(retain_gsdlhomec)!=0)
248 || (putenv(retain_pathc)!=0))
249 {
250 // Would be better if the ostream& logout was used here.
251 cerr << "Error setting Greenstone environment variables with putenv()" << endl;
252 // perror("putenv(...): "); // This didn't yield any noticable output under Windows running local library server
253 return false;
254 }
255
256 // Need to keep memory allocated setgsdlosc, setgsdlhomec or pathc around
257 // (i.e. can't delete them). This is because putenv() doesn't take
258 // a copy, but places them in the actual environment.
259
260 gsdl_setenv_done = true;
261
262 return true;
263}
264
265
266
267// attempts to work out if perl is functional
268bool perl_ok (ostream &logout) {
269#if defined(__WIN32__)
270 text_t cmd = "perl -e \"exit 0\"";
271#else
272 text_t cmd = "perl -e 'exit 0'";
273#endif
274 int i = gsdl_system (cmd, true, logout);
275 return (i == 0);
276}
Note: See TracBrowser for help on using the repository browser.