source: main/tags/2.35a/gsdl/lib/gsdltools.cpp@ 33178

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

Made a small change to the gsdl_system function to overcome weird windows
behaviour preventing collector status pages from updating when using the
web library.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 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#if defined (GSDL_LOCAL_LIBRARY)
86 NULL,
87#else
88 DETACHED_PROCESS,
89#endif
90 NULL,
91 NULL,
92 &ps,
93 &pi);
94 if (!res) {
95 logout << "gsdl_system failed to start " << cmd_c
96 << " process, error code " << GetLastError();
97 delete cmd_c;
98 return -1;
99 }
100
101 DWORD ret = 0;
102 if (sync) { // synchronous system call
103 // wait until child process exits.
104 WaitForSingleObject(pi.hProcess, INFINITE);
105 // set ret to exit code of child process
106 GetExitCodeProcess(pi.hProcess, &ret);
107 }
108
109 CloseHandle(pi.hProcess);
110 CloseHandle(pi.hThread);
111
112#else
113 // the unix version
114 int ret = 0;
115 if (sync) { // synchronous system call
116 // make sure the command interpreter is found
117 if (system (NULL) == 0) {
118 logout << "gsdl_system failed to start " << cmd_c
119 << " process, command interpreter not found\n";
120 delete cmd_c;
121 return -1;
122 }
123 ret = system (cmd_c);
124
125 } else { // asynchronous system call
126 int pid = fork();
127 if (pid == -1) {
128 delete cmd_c;
129 return -1;
130 }
131 if (pid == 0) {
132 // child process
133 char *argv[4];
134 argv[0] = "sh";
135 argv[1] = "-c";
136 argv[2] = cmd_c;
137 argv[3] = 0;
138 execv("/bin/sh", argv);
139 }
140 }
141#endif
142
143 delete cmd_c;
144 return ret;
145}
146
147// attempts to work out if perl is functional
148bool perl_ok (ostream &logout) {
149#if defined(__WIN32__)
150 text_t cmd = "perl -e \"exit 0\"";
151#else
152 text_t cmd = "perl -e 'exit 0'";
153#endif;
154 int i = gsdl_system (cmd, true, logout);
155 return (i == 0);
156}
Note: See TracBrowser for help on using the repository browser.