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 | // gsdl_system spawns a completely separate program (i.e. the calling
|
---|
56 | // program continues and terminates normally). Arguments containing special
|
---|
57 | // characters (e.g. '&') should be quoted with ""
|
---|
58 |
|
---|
59 | #if defined (__WIN32__)
|
---|
60 | void gsdl_system (char *cmd, ostream &logout) {
|
---|
61 |
|
---|
62 | STARTUPINFO ps = {sizeof(STARTUPINFO), NULL, NULL, NULL,
|
---|
63 | 0, 0, 0, 0, 0, 0,
|
---|
64 | 0, 0,
|
---|
65 | 0, 0, NULL,
|
---|
66 | NULL, NULL, NULL};
|
---|
67 | PROCESS_INFORMATION pi;
|
---|
68 | BOOL res = CreateProcess(NULL,
|
---|
69 | cmd,
|
---|
70 | NULL,
|
---|
71 | NULL,
|
---|
72 | FALSE,
|
---|
73 | DETACHED_PROCESS,
|
---|
74 | NULL,
|
---|
75 | NULL,
|
---|
76 | &ps,
|
---|
77 | &pi);
|
---|
78 | if (!res) {
|
---|
79 | logout << "Failed to start " << cmd << " process, error code " << GetLastError();
|
---|
80 | }
|
---|
81 |
|
---|
82 | CloseHandle(pi.hProcess);
|
---|
83 | CloseHandle(pi.hThread);
|
---|
84 | }
|
---|
85 |
|
---|
86 | #else
|
---|
87 | void gsdl_system (char *cmd, ostream &logout) {
|
---|
88 |
|
---|
89 | if (cmd == NULL) return;
|
---|
90 | int pid = fork();
|
---|
91 | if (pid == -1) return;
|
---|
92 | if (pid == 0) {
|
---|
93 | // child process
|
---|
94 | char *argv[4];
|
---|
95 | argv[0] = "sh";
|
---|
96 | argv[1] = "-c";
|
---|
97 | argv[2] = cmd;
|
---|
98 | argv[3] = 0;
|
---|
99 | execv("/bin/sh", argv);
|
---|
100 | // exit(127);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 |
|
---|
105 |
|
---|
106 | // gsdl_call_perl executes the perl script perl_cmd, passing it all the
|
---|
107 | // arguments provided.
|
---|
108 |
|
---|
109 | // Arguments should be char*'s, the last argument should be NULL
|
---|
110 |
|
---|
111 | // perl_cmd shouldn't contain the path to the script as we use "perl -S"
|
---|
112 | // to find it. This means that the PATH environment variable of the calling
|
---|
113 | // process should contain the directory where the perl_cmd script lives.
|
---|
114 |
|
---|
115 | // Uses a standard system() call on unix and a synchronous _spawn() on windows.
|
---|
116 | // We use the _spawn() because windows 9* doesn't seem to pass the
|
---|
117 | // environment of the calling process to the new process when using
|
---|
118 | // system (which we need to do).
|
---|
119 |
|
---|
120 | // all arguments will be quoted with double quotes [""] so they should be unquoted
|
---|
121 | // when passed in.
|
---|
122 |
|
---|
123 | // returns the exit status of the called process (-1 if system() or _spawn() failed)
|
---|
124 |
|
---|
125 | int gsdl_call_perl (char *perl_cmd, ...) {
|
---|
126 |
|
---|
127 | va_list argn;
|
---|
128 | char *thisarg = perl_cmd;
|
---|
129 |
|
---|
130 | #if defined(__WIN32__) && !defined (__GNUC__)
|
---|
131 | int i = 2;
|
---|
132 | #define GSDL_MAX_ARGS 20
|
---|
133 | char *args[GSDL_MAX_ARGS];
|
---|
134 | args[0] = "perl";
|
---|
135 | args[1] = "-S";
|
---|
136 | #else
|
---|
137 | text_t cmd = "perl -S";
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | // get arguments
|
---|
141 | va_start(argn, perl_cmd);
|
---|
142 | while(thisarg != NULL) {
|
---|
143 | #if defined(__WIN32__) && !defined (__GNUC__)
|
---|
144 | if (i >= GSDL_MAX_ARGS) break;
|
---|
145 | text_t tmp = thisarg;
|
---|
146 | tmp = "\"" + tmp + "\"";
|
---|
147 | args[i] = tmp.getcstr();
|
---|
148 | i++;
|
---|
149 | #else
|
---|
150 | cmd += " \"";
|
---|
151 | cmd += thisarg;
|
---|
152 | cmd.push_back ('"');
|
---|
153 | #endif
|
---|
154 | thisarg = va_arg(argn, char *);
|
---|
155 | }
|
---|
156 | va_end(argn);
|
---|
157 |
|
---|
158 | #if defined(__WIN32__) && !defined (__GNUC__)
|
---|
159 | args[i] = NULL;
|
---|
160 | int rv = _spawnvp (_P_WAIT, "perl", args);
|
---|
161 | while (--i > 2) delete (args[i]);
|
---|
162 | #else
|
---|
163 | char *cmdc = cmd.getcstr();
|
---|
164 | int rv = system (cmdc);
|
---|
165 | delete (cmdc);
|
---|
166 | #endif
|
---|
167 | return rv;
|
---|
168 | }
|
---|
169 |
|
---|
170 | // attempts to work out if perl is functional
|
---|
171 | bool perl_ok () {
|
---|
172 | int i = system ("perl -e exit(-10)");
|
---|
173 | return (i == -10);
|
---|
174 | }
|
---|