1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * os_process.cpp --
|
---|
4 | * Copyright (C) 2010 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
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 "os_process.h"
|
---|
27 |
|
---|
28 | osprocess::osprocess(OSProcessPipeMode mode,
|
---|
29 | char* prog_name, char* argv[], char* envp[])
|
---|
30 | : mode_(mode)
|
---|
31 | {}
|
---|
32 |
|
---|
33 | osprocess::~osprocess()
|
---|
34 | {
|
---|
35 | }
|
---|
36 |
|
---|
37 | bool osprocess::close()
|
---|
38 | {
|
---|
39 | // close any file handles that are still open
|
---|
40 |
|
---|
41 | bool write_close_rv = close_write_pipe(withoutWarning);
|
---|
42 | bool read_close_rv = close_read_pipe(withoutWarning);
|
---|
43 |
|
---|
44 | return (write_close_rv && read_close_rv);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | #if 1
|
---|
51 |
|
---|
52 | #include "os_process_windows.h"
|
---|
53 | #include "os_process_unix.h"
|
---|
54 | #include <iostream>
|
---|
55 | using namespace std;
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | void test_uni_write_pipe()
|
---|
60 | {
|
---|
61 | #ifdef __WIN32__
|
---|
62 | char* prog_name = "type";
|
---|
63 | char* argv[] = { "type", NULL } ;
|
---|
64 |
|
---|
65 | #else
|
---|
66 | char* prog_name = "tr";
|
---|
67 | char* argv[] = { "tr", "[A-Z]", "[a-z]", NULL };
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | cout << "<hr>" << endl;
|
---|
71 | cout << "<p>Unidirectional Write Test" << endl;
|
---|
72 | cout << "<p> Converts uppercase letters to lowercase" << endl;
|
---|
73 | cout << "<p> These are then sent to whereever the stdout of the child process goes" << endl;
|
---|
74 | cout << "<p>" << endl;
|
---|
75 |
|
---|
76 | #ifdef __WIN32__
|
---|
77 | osprocesswindows* process = new osprocesswindows(uniWrite,prog_name,argv);
|
---|
78 | #else
|
---|
79 | osprocessunix* process = new osprocessunix(uniWrite,prog_name,argv);
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | char* buffer = "TESTING uniWirite IN CAP\nTesting uniWrite in Mixed Case\n";
|
---|
83 | int buffer_len = strlen(buffer);
|
---|
84 |
|
---|
85 | if (!process->write(buffer,buffer_len)) {
|
---|
86 | cerr << "Error: failed to write to child process\n";
|
---|
87 | }
|
---|
88 |
|
---|
89 | process->close();
|
---|
90 |
|
---|
91 | process->wait();
|
---|
92 |
|
---|
93 | delete process;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | void test_uni_read_pipe()
|
---|
98 | {
|
---|
99 | char* prog_name = "java";
|
---|
100 | char* argv[] = { "java", NULL };
|
---|
101 |
|
---|
102 | cout << "<hr>" << endl;
|
---|
103 | cout << "<p>Unidirectional Read Test" << endl;
|
---|
104 | cout << "<p> The output of 'java' is read in by this process and then sent to stdout" << endl;
|
---|
105 | cout << "<p>" << endl;
|
---|
106 |
|
---|
107 |
|
---|
108 | #ifdef __WIN32__
|
---|
109 | osprocess* process = new osprocesswindows(uniRead,prog_name,argv);
|
---|
110 | #else
|
---|
111 | osprocess* process = new osprocessunix(uniRead,prog_name,argv);
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | const int BufferSize = 1024;
|
---|
115 | char buffer[BufferSize];
|
---|
116 |
|
---|
117 | int bytes_read = 0;
|
---|
118 | do {
|
---|
119 | bytes_read = process->read(buffer,BufferSize);
|
---|
120 | if (bytes_read>0) {
|
---|
121 | fwrite(buffer,1,bytes_read,stdout);
|
---|
122 | }
|
---|
123 | } while (bytes_read==BufferSize);
|
---|
124 |
|
---|
125 |
|
---|
126 | // deleting object will also have the effect of closing any open handles
|
---|
127 | delete process;
|
---|
128 |
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 | void test_bi_read_write_pipe()
|
---|
134 | {
|
---|
135 | #ifdef __WIN32__
|
---|
136 | char* prog_name = "type";
|
---|
137 | char* argv[] = NULL ;
|
---|
138 |
|
---|
139 | #else
|
---|
140 | char* prog_name = "tr";
|
---|
141 | char* argv[] = { "tr", "[A-Z]", "[a-z]", NULL };
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | // Converts uppercase letters to lowercase
|
---|
145 | // Answer is read back and then printed to STDOUT by this process
|
---|
146 |
|
---|
147 | cout << "<hr>" << endl;
|
---|
148 | cout << "<p>Bidirectional Read/Write Test" << endl;
|
---|
149 | cout << "<p> Converts uppercase letters to lowercase" << endl;
|
---|
150 | cout << "<p> Data is sent to child process down pipe by this process" << endl;
|
---|
151 | cout << "<p> Answer is read back from child process and printed to stdout" << endl;
|
---|
152 | cout << "<p>" << endl;
|
---|
153 |
|
---|
154 | #ifdef __WIN32__
|
---|
155 | osprocesswindows* process = new osprocesswindows(biReadWrite,prog_name,argv);
|
---|
156 | #else
|
---|
157 | osprocessunix* process = new osprocessunix(biReadWrite,prog_name,argv);
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | char* write_buffer = "TESTING biReadWrite IN CAP\nTesting biReadWrite in Mixed Case\n";
|
---|
161 | int write_buffer_len = strlen(write_buffer);
|
---|
162 |
|
---|
163 | if (!process->write(write_buffer,write_buffer_len)) {
|
---|
164 | cerr << "Error: failed to write to child process\n";
|
---|
165 | }
|
---|
166 |
|
---|
167 | const int BufferSize = 1024;
|
---|
168 | char read_buffer[BufferSize];
|
---|
169 | process->close_write_pipe();
|
---|
170 |
|
---|
171 | int bytes_read = 0;
|
---|
172 | do {
|
---|
173 | bytes_read = process->read(read_buffer,BufferSize);
|
---|
174 | if (bytes_read>0) {
|
---|
175 | fwrite(read_buffer,1,bytes_read,stdout);
|
---|
176 | }
|
---|
177 | } while (bytes_read==BufferSize);
|
---|
178 |
|
---|
179 |
|
---|
180 | process->close();
|
---|
181 |
|
---|
182 | delete process;
|
---|
183 |
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | int main()
|
---|
188 | {
|
---|
189 | printf("Content-type: text/html\n\n");
|
---|
190 |
|
---|
191 | printf( "<html>\n");
|
---|
192 | printf( " <head>\n");
|
---|
193 | printf( " <title>Testing</title>\n");
|
---|
194 | printf( " </head>\n");
|
---|
195 | printf( " <body>\n");
|
---|
196 |
|
---|
197 | test_uni_read_pipe();
|
---|
198 | test_uni_write_pipe();
|
---|
199 | test_bi_read_write_pipe();
|
---|
200 |
|
---|
201 | printf( " </body>\n");
|
---|
202 | printf( "</html>\n");
|
---|
203 |
|
---|
204 | return 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 | #endif
|
---|
208 |
|
---|