source: trunk/gsdl/lib/gsdltools.cpp@ 1817

Last change on this file since 1817 was 1817, checked in by sjboddie, 23 years ago

Completely overhauled the way system calls are made on windows (i.e.
the gsdl_system() function), in the process removing the old gsdl_call_perl
function. Greenstone can now spawn console applications without the
annoying popup window so the collector is a little smoother and prettier.
Unfortunately, the old problem with the web library/apache server not
updating the browser page before a spawned process completes has again
reared its head. This seems the lesser of two evils though as the local
library is (hopefully) the version that the large majority of windows
users will use. I'll have another go at fixing the web library when I have
a chance.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 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
28#if defined(__WIN32__)
29#include <windows.h>
30#include <process.h>
31#endif
32
33bool 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
42text_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 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.
66int gsdl_system (const text_t &cmd, bool sync, ostream &logout) {
67 if (cmd.empty()) return -1;
68 char *cmd_c = cmd.getcstr();
69
70#if defined (__WIN32__)
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)
74 STARTUPINFO ps = {sizeof(STARTUPINFO), NULL, NULL, NULL,
75 0, 0, 0, 0, 0, 0,
76 0, STARTF_USESHOWWINDOW,
77 SW_HIDE, 0, NULL,
78 NULL, NULL, NULL};
79 PROCESS_INFORMATION pi;
80 BOOL res = CreateProcess(NULL,
81 cmd_c,
82 NULL,
83 NULL,
84 FALSE,
85 NULL,
86 NULL,
87 NULL,
88 &ps,
89 &pi);
90 if (!res) {
91 logout << "gsdl_system failed to start " << cmd_c
92 << " process, error code " << GetLastError();
93 delete cmd_c;
94 return -1;
95 }
96
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
105 CloseHandle(pi.hProcess);
106 CloseHandle(pi.hThread);
107
108#else
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);
120
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";
132 argv[2] = cmd;
133 argv[3] = 0;
134 execv("/bin/sh", argv);
135 }
136 }
137#endif
138
139 delete cmd_c;
140 return ret;
141}
142
143// attempts to work out if perl is functional
144bool perl_ok (ostream &logout) {
145#if defined(__WIN32__)
146 text_t cmd = "perl -e \"exit 1\"";
147#else
148 text_t cmd = "perl -e \"'exit 1'\"";
149#endif;
150 int i = gsdl_system (cmd, true, logout);
151 return (i == 1);
152}
Note: See TracBrowser for help on using the repository browser.