[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"
|
---|
| 27 |
|
---|
[1678] | 28 | #if defined(__WIN32__)
|
---|
| 29 | #include <windows.h>
|
---|
| 30 | #include <process.h>
|
---|
| 31 | #endif
|
---|
| 32 |
|
---|
[1076] | 33 | bool littleEndian() {
|
---|
| 34 | char s[2] = {'\xFE', '\xEF'};
|
---|
| 35 |
|
---|
| 36 | if (sizeof(unsigned short) == 2)
|
---|
| 37 | return *(unsigned short*)s == 0xEFFE;
|
---|
| 38 | else if (sizeof(unsigned int) == 2)
|
---|
| 39 | return *(unsigned int*)s == 0xEFFE;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[1146] | 42 | text_t dm_safe (const text_t &instring) {
|
---|
[1076] | 43 |
|
---|
[1146] | 44 | text_t outstring;
|
---|
| 45 | text_t::const_iterator here = instring.begin();
|
---|
| 46 | text_t::const_iterator end = instring.end();
|
---|
| 47 | while (here != end) {
|
---|
| 48 | if (*here == '_' || *here == '\\') outstring.push_back('\\');
|
---|
| 49 | outstring.push_back(*here);
|
---|
| 50 | here ++;
|
---|
| 51 | }
|
---|
| 52 | return outstring;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[1817] | 55 | // gsdl_system creates a new process for the cmd command (which
|
---|
| 56 | // may contain arguments).
|
---|
| 57 | // cmd should contain the full path of the program to run.
|
---|
| 58 | // The child process inherits the environment of the calling
|
---|
| 59 | // process.
|
---|
| 60 | // If sync is true a synchronous call will be made, otherwise
|
---|
| 61 | // an asyncronous call.
|
---|
| 62 | // If sync is true the return value will be the exit code of
|
---|
| 63 | // the child process or -1 if the child process wasn't started.
|
---|
| 64 | // If sync is false the return value will be 0 if the process
|
---|
| 65 | // was started ok or -1 if it failed.
|
---|
| 66 | int gsdl_system (const text_t &cmd, bool sync, ostream &logout) {
|
---|
| 67 | if (cmd.empty()) return -1;
|
---|
| 68 | char *cmd_c = cmd.getcstr();
|
---|
[1176] | 69 |
|
---|
| 70 | #if defined (__WIN32__)
|
---|
[1817] | 71 | // the windows version - this is implemented this way
|
---|
| 72 | // to prevent windows popping up all over the place when
|
---|
| 73 | // we call a console application (like perl)
|
---|
[1176] | 74 | STARTUPINFO ps = {sizeof(STARTUPINFO), NULL, NULL, NULL,
|
---|
| 75 | 0, 0, 0, 0, 0, 0,
|
---|
[1817] | 76 | 0, STARTF_USESHOWWINDOW,
|
---|
| 77 | SW_HIDE, 0, NULL,
|
---|
[1176] | 78 | NULL, NULL, NULL};
|
---|
| 79 | PROCESS_INFORMATION pi;
|
---|
| 80 | BOOL res = CreateProcess(NULL,
|
---|
[1817] | 81 | cmd_c,
|
---|
[1176] | 82 | NULL,
|
---|
| 83 | NULL,
|
---|
| 84 | FALSE,
|
---|
[1817] | 85 | NULL,
|
---|
[1176] | 86 | NULL,
|
---|
| 87 | NULL,
|
---|
| 88 | &ps,
|
---|
| 89 | &pi);
|
---|
| 90 | if (!res) {
|
---|
[1817] | 91 | logout << "gsdl_system failed to start " << cmd_c
|
---|
| 92 | << " process, error code " << GetLastError();
|
---|
| 93 | delete cmd_c;
|
---|
| 94 | return -1;
|
---|
[1176] | 95 | }
|
---|
| 96 |
|
---|
[1817] | 97 | DWORD ret = 0;
|
---|
| 98 | if (sync) { // synchronous system call
|
---|
| 99 | // wait until child process exits.
|
---|
| 100 | WaitForSingleObject(pi.hProcess, INFINITE);
|
---|
| 101 | // set ret to exit code of child process
|
---|
| 102 | GetExitCodeProcess(pi.hProcess, &ret);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[1176] | 105 | CloseHandle(pi.hProcess);
|
---|
| 106 | CloseHandle(pi.hThread);
|
---|
| 107 |
|
---|
[1456] | 108 | #else
|
---|
[1817] | 109 | // the unix version
|
---|
| 110 | int ret = 0;
|
---|
| 111 | if (sync) { // synchronous system call
|
---|
| 112 | // make sure the command interpreter is found
|
---|
| 113 | if (system (NULL) == 0) {
|
---|
| 114 | logout << "gsdl_system failed to start " << cmd_c
|
---|
| 115 | << " process, command interpreter not found\n";
|
---|
| 116 | delete cmd_c;
|
---|
| 117 | return -1;
|
---|
| 118 | }
|
---|
| 119 | ret = system (cmd_c);
|
---|
[1456] | 120 |
|
---|
[1817] | 121 | } else { // asynchronous system call
|
---|
| 122 | int pid = fork();
|
---|
| 123 | if (pid == -1) {
|
---|
| 124 | delete cmd_c;
|
---|
| 125 | return -1;
|
---|
| 126 | }
|
---|
| 127 | if (pid == 0) {
|
---|
| 128 | // child process
|
---|
| 129 | char *argv[4];
|
---|
| 130 | argv[0] = "sh";
|
---|
| 131 | argv[1] = "-c";
|
---|
[1818] | 132 | argv[2] = cmd_c;
|
---|
[1817] | 133 | argv[3] = 0;
|
---|
| 134 | execv("/bin/sh", argv);
|
---|
| 135 | }
|
---|
[1456] | 136 | }
|
---|
[1678] | 137 | #endif
|
---|
[1456] | 138 |
|
---|
[1817] | 139 | delete cmd_c;
|
---|
| 140 | return ret;
|
---|
[1678] | 141 | }
|
---|
[1783] | 142 |
|
---|
| 143 | // attempts to work out if perl is functional
|
---|
[1817] | 144 | bool perl_ok (ostream &logout) {
|
---|
[1793] | 145 | #if defined(__WIN32__)
|
---|
[1818] | 146 | text_t cmd = "perl -e \"exit 0\"";
|
---|
[1817] | 147 | #else
|
---|
[1818] | 148 | text_t cmd = "perl -e 'exit 0'";
|
---|
[1817] | 149 | #endif;
|
---|
| 150 | int i = gsdl_system (cmd, true, logout);
|
---|
[1818] | 151 | return (i == 0);
|
---|
[1783] | 152 | }
|
---|