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