[1076] | 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"
|
---|
[16895] | 27 | #include "fileutil.h"
|
---|
[1076] | 28 |
|
---|
[16949] | 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>
|
---|
| 36 | using namespace std;
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
| 39 |
|
---|
[1678] | 40 | #if defined(__WIN32__)
|
---|
| 41 | #include <windows.h>
|
---|
| 42 | #include <process.h>
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
[16895] | 45 | #if !defined (__WIN32__)
|
---|
| 46 | #include <sys/utsname.h>
|
---|
| 47 | #include <unistd.h>
|
---|
| 48 | #endif
|
---|
| 49 |
|
---|
| 50 |
|
---|
[1076] | 51 | bool 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 |
|
---|
[1146] | 60 | text_t dm_safe (const text_t &instring) {
|
---|
[1076] | 61 |
|
---|
[1146] | 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);
|
---|
[9593] | 68 | ++here;
|
---|
[1146] | 69 | }
|
---|
| 70 | return outstring;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[8727] | 73 | void 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 |
|
---|
[1817] | 88 | // gsdl_system creates a new process for the cmd command (which
|
---|
| 89 | // may contain arguments).
|
---|
| 90 | // cmd should contain the full path of the program to run.
|
---|
| 91 | // The child process inherits the environment of the calling
|
---|
| 92 | // process.
|
---|
| 93 | // If sync is true a synchronous call will be made, otherwise
|
---|
| 94 | // an asyncronous call.
|
---|
| 95 | // If sync is true the return value will be the exit code of
|
---|
| 96 | // the child process or -1 if the child process wasn't started.
|
---|
| 97 | // If sync is false the return value will be 0 if the process
|
---|
| 98 | // was started ok or -1 if it failed.
|
---|
| 99 | int gsdl_system (const text_t &cmd, bool sync, ostream &logout) {
|
---|
| 100 | if (cmd.empty()) return -1;
|
---|
| 101 | char *cmd_c = cmd.getcstr();
|
---|
[1176] | 102 |
|
---|
| 103 | #if defined (__WIN32__)
|
---|
[1817] | 104 | // the windows version - this is implemented this way
|
---|
| 105 | // to prevent windows popping up all over the place when
|
---|
| 106 | // we call a console application (like perl)
|
---|
[1176] | 107 | STARTUPINFO ps = {sizeof(STARTUPINFO), NULL, NULL, NULL,
|
---|
| 108 | 0, 0, 0, 0, 0, 0,
|
---|
[1817] | 109 | 0, STARTF_USESHOWWINDOW,
|
---|
| 110 | SW_HIDE, 0, NULL,
|
---|
[2298] | 111 | NULL, NULL, NULL};
|
---|
[1176] | 112 | PROCESS_INFORMATION pi;
|
---|
| 113 | BOOL res = CreateProcess(NULL,
|
---|
[1817] | 114 | cmd_c,
|
---|
[1176] | 115 | NULL,
|
---|
| 116 | NULL,
|
---|
| 117 | FALSE,
|
---|
[2298] | 118 | #if defined (GSDL_LOCAL_LIBRARY)
|
---|
[1817] | 119 | NULL,
|
---|
[2298] | 120 | #else
|
---|
| 121 | DETACHED_PROCESS,
|
---|
| 122 | #endif
|
---|
[1176] | 123 | NULL,
|
---|
| 124 | NULL,
|
---|
| 125 | &ps,
|
---|
| 126 | &pi);
|
---|
| 127 | if (!res) {
|
---|
[1817] | 128 | logout << "gsdl_system failed to start " << cmd_c
|
---|
| 129 | << " process, error code " << GetLastError();
|
---|
[8727] | 130 | delete []cmd_c;
|
---|
[1817] | 131 | return -1;
|
---|
[1176] | 132 | }
|
---|
| 133 |
|
---|
[1817] | 134 | DWORD ret = 0;
|
---|
| 135 | if (sync) { // synchronous system call
|
---|
| 136 | // wait until child process exits.
|
---|
| 137 | WaitForSingleObject(pi.hProcess, INFINITE);
|
---|
| 138 | // set ret to exit code of child process
|
---|
| 139 | GetExitCodeProcess(pi.hProcess, &ret);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[1176] | 142 | CloseHandle(pi.hProcess);
|
---|
| 143 | CloseHandle(pi.hThread);
|
---|
| 144 |
|
---|
[1456] | 145 | #else
|
---|
[1817] | 146 | // the unix version
|
---|
| 147 | int ret = 0;
|
---|
| 148 | if (sync) { // synchronous system call
|
---|
| 149 | // make sure the command interpreter is found
|
---|
| 150 | if (system (NULL) == 0) {
|
---|
| 151 | logout << "gsdl_system failed to start " << cmd_c
|
---|
| 152 | << " process, command interpreter not found\n";
|
---|
[8727] | 153 | delete []cmd_c;
|
---|
[1817] | 154 | return -1;
|
---|
| 155 | }
|
---|
| 156 | ret = system (cmd_c);
|
---|
[1456] | 157 |
|
---|
[1817] | 158 | } else { // asynchronous system call
|
---|
| 159 | int pid = fork();
|
---|
| 160 | if (pid == -1) {
|
---|
[8727] | 161 | delete []cmd_c;
|
---|
[1817] | 162 | return -1;
|
---|
| 163 | }
|
---|
| 164 | if (pid == 0) {
|
---|
| 165 | // child process
|
---|
| 166 | char *argv[4];
|
---|
| 167 | argv[0] = "sh";
|
---|
| 168 | argv[1] = "-c";
|
---|
[1818] | 169 | argv[2] = cmd_c;
|
---|
[1817] | 170 | argv[3] = 0;
|
---|
| 171 | execv("/bin/sh", argv);
|
---|
| 172 | }
|
---|
[1456] | 173 | }
|
---|
[1678] | 174 | #endif
|
---|
[1456] | 175 |
|
---|
[8727] | 176 | delete []cmd_c;
|
---|
[1817] | 177 | return ret;
|
---|
[1678] | 178 | }
|
---|
[1783] | 179 |
|
---|
[16895] | 180 |
|
---|
| 181 | static bool gsdl_setenv_done = false;
|
---|
[16909] | 182 | static char* retain_gsdlosc = NULL;
|
---|
| 183 | static char* retain_gsdlhomec = NULL;
|
---|
| 184 | static char* retain_pathc = NULL;
|
---|
[16895] | 185 |
|
---|
| 186 | bool set_gsdl_env_vars (const text_t& gsdlhome)
|
---|
| 187 | {
|
---|
| 188 | if (gsdl_setenv_done) { return true; }
|
---|
| 189 |
|
---|
| 190 | // set up GSDLOS, GSDLHOME and PATH environment variables
|
---|
| 191 |
|
---|
| 192 | text_t gsdlos, path;
|
---|
| 193 | unsigned int path_separator = ':';
|
---|
| 194 |
|
---|
| 195 | #if defined (__WIN32__)
|
---|
| 196 | gsdlos = "windows";
|
---|
| 197 | path_separator = ';';
|
---|
| 198 |
|
---|
| 199 | path = filename_cat (gsdlhome, "bin", "windows", "perl", "bin;");
|
---|
| 200 |
|
---|
| 201 | #else
|
---|
| 202 | struct utsname *buf = new struct utsname();
|
---|
| 203 | int i = uname (buf);
|
---|
| 204 | if (i == -1) gsdlos = "linux"; // uname failed, default to linux
|
---|
| 205 | else gsdlos.setcstr (buf->sysname);
|
---|
| 206 | delete buf;
|
---|
| 207 | lc (gsdlos);
|
---|
| 208 | #endif
|
---|
| 209 |
|
---|
| 210 | // according to getenv documentation (after a bit of digging), *getenv*
|
---|
| 211 | // is responsible for the char* pointer returned, so no need for us
|
---|
| 212 | // to free it (in fact that would be a mistake!)
|
---|
| 213 |
|
---|
| 214 | char* orig_pathc = getenv ("PATH");
|
---|
| 215 | path += filename_cat (gsdlhome, "bin", gsdlos);
|
---|
| 216 | path.push_back (path_separator);
|
---|
| 217 | path += filename_cat (gsdlhome, "bin", "script");
|
---|
| 218 | if (orig_pathc != NULL) {
|
---|
| 219 | path.push_back (path_separator);
|
---|
| 220 | path += orig_pathc;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[16909] | 223 | text_t putpath = "PATH=" + path;
|
---|
| 224 | text_t putgsdlos = "GSDLOS=" + gsdlos;
|
---|
| 225 | text_t putgsdlhome = "GSDLHOME=" + gsdlhome;
|
---|
| 226 |
|
---|
| 227 | retain_gsdlosc = putgsdlos.getcstr();
|
---|
| 228 | retain_gsdlhomec = putgsdlhome.getcstr();
|
---|
[16949] | 229 | retain_pathc = putpath.getcstr();
|
---|
[16895] | 230 |
|
---|
[16909] | 231 | if ((putenv(retain_gsdlosc)!=0) || (putenv(retain_gsdlhomec)!=0)
|
---|
| 232 | || (putenv(retain_pathc)!=0))
|
---|
[16895] | 233 | {
|
---|
[16949] | 234 | // Would be better if the ostream& logout was used here.
|
---|
| 235 | cerr << "Error setting Greenstone environment variables with putenv()" << endl;
|
---|
| 236 | // perror("putenv(...): "); // This didn't yield any noticable output under Windows running local library server
|
---|
[16895] | 237 | return false;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[16909] | 240 | // Need to keep memory allocated setgsdlosc, setgsdlhomec or pathc around
|
---|
| 241 | // (i.e. can't delete them). This is because putenv() doesn't take
|
---|
| 242 | // a copy, but places them in the actual environment.
|
---|
[16895] | 243 |
|
---|
| 244 | gsdl_setenv_done = true;
|
---|
| 245 |
|
---|
| 246 | return true;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 |
|
---|
| 250 |
|
---|
[1783] | 251 | // attempts to work out if perl is functional
|
---|
[1817] | 252 | bool perl_ok (ostream &logout) {
|
---|
[1793] | 253 | #if defined(__WIN32__)
|
---|
[1818] | 254 | text_t cmd = "perl -e \"exit 0\"";
|
---|
[1817] | 255 | #else
|
---|
[1818] | 256 | text_t cmd = "perl -e 'exit 0'";
|
---|
[2937] | 257 | #endif
|
---|
[1817] | 258 | int i = gsdl_system (cmd, true, logout);
|
---|
[1818] | 259 | return (i == 0);
|
---|
[1783] | 260 | }
|
---|