1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/* pipe routines for OS/2
* lee johnson, spring 1995
*/
#define INCL_DOSFILEMGR
#define INCL_DOSQUEUES
#define INCL_DOSERRORS
#define INCL_NOPMAPI
#include <os2.h>
#include "estruct.h"
#include <process.h>
#ifndef EOS2ERR
#define EOS2ERR EINVAL
#endif
#ifndef EOUTOFMEM
#define EOUTOFMEM ENOMEM
#endif
#if CC_WATCOM
#define _fdopen fdopen
#define _cwait cwait
#endif
/* We have one pipe running from the parent to the child... */
static HFILE pipe_p_to_c = NULLHANDLE, pipe_c_from_p = NULLHANDLE;
/* ...and another running from the child to the parent. */
static HFILE pipe_c_to_p = NULLHANDLE, pipe_p_from_c = NULLHANDLE;
static FILE *vile_in = NULL; /* stream attached to pipe_p_from_c */
static FILE *vile_out = NULL; /* stream attached to pipe_p_to_c */
static int pid = -1;
static void
DupError(APIRET rc)
{
switch(rc)
{
case ERROR_TOO_MANY_OPEN_FILES:
errno = EMFILE;
break;
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_TARGET_HANDLE:
errno = EBADF;
break;
default:
errno = EOS2ERR;
break;
}
}
int
_os2_command(char *command)
{
char buffer[1024];
char *shell;
if ((shell = getenv("COMSPEC")) != NULL)
{
sprintf(buffer, "/C %s", command);
return spawnlp(P_NOWAIT, shell, shell, buffer, NULL);
}
else
{
return -1;
}
}
static void
close_pipes(void)
{
if (vile_in != NULL)
{
TRACE(("close_pipes, vile_in #%d\n", fileno(vile_in)));
fclose(vile_in);
vile_in = NULL;
pipe_p_from_c = NULLHANDLE;
}
else if (pipe_p_from_c != NULLHANDLE)
{
TRACE(("close_pipes, pipe_p_from_c #%d\n", pipe_p_from_c));
DosClose(pipe_p_from_c);
pipe_p_from_c = NULLHANDLE;
}
if (vile_out != NULL)
{
TRACE(("close_pipes, vile_out #%d\n", fileno(vile_out)));
fclose(vile_out);
vile_out = NULL;
pipe_p_to_c = NULLHANDLE;
}
else if (pipe_p_to_c != NULLHANDLE)
{
TRACE(("close_pipes, pipe_p_to_c #%d\n", pipe_p_to_c));
DosClose(pipe_p_to_c);
pipe_p_to_c = NULLHANDLE;
}
if (pipe_c_to_p != NULLHANDLE)
{
TRACE(("close_pipes, pipe_c_to_p #%d\n", pipe_c_to_p));
DosClose(pipe_c_to_p);
pipe_c_to_p = NULLHANDLE;
}
if (pipe_c_from_p != NULLHANDLE)
{
TRACE(("close_pipes, pipe_c_from_p #%d\n", pipe_c_from_p));
DosClose(pipe_c_from_p);
pipe_c_from_p = NULLHANDLE;
}
}
int
inout_popen(FILE **infile, FILE **outfile, char *command)
{
HFILE save_in, save_out, save_err;
HFILE fd;
APIRET rc;
save_in = save_out = save_err = NULLHANDLE;
/* We have one pipe running from the parent to the child... */
pipe_p_to_c = pipe_c_from_p = NULLHANDLE;
/* ...and another running from the child to the parent. */
pipe_c_to_p = pipe_p_from_c = NULLHANDLE;
vile_in = vile_out = NULL;
/* Create the pipes we'll use for IPC. */
rc = DosCreatePipe(&pipe_p_from_c, &pipe_c_to_p, 4096);
if (rc != NO_ERROR)
{
errno = EOUTOFMEM;
goto Error;
}
rc = DosCreatePipe(&pipe_c_from_p, &pipe_p_to_c, 4096);
if (rc != NO_ERROR)
{
errno = EOUTOFMEM;
goto Error;
}
/*
* Save standard I/O handles.
*/
save_in = ~0;
if (DosDupHandle(0, &save_in) != NO_ERROR)
{
DupError(rc);
goto Error;
}
save_out = ~0;
if (DosDupHandle(1, &save_out) != NO_ERROR)
{
DupError(rc);
goto Error;
}
save_err = ~0;
if (DosDupHandle(2, &save_err) != NO_ERROR)
{
DupError(rc);
goto Error;
}
/*
* Redirect standard file handles for the CHILD PROCESS.
*/
/* Get standard input from the read end of the p_to_c pipe. */
fd = 0;
if (DosDupHandle(pipe_c_from_p, &fd) != NO_ERROR)
{
DupError(rc);
goto Error;
}
/* Send standard output to the write end of the c_to_p pipe. */
fd = 1;
if (DosDupHandle(pipe_c_to_p, &fd) != NO_ERROR)
{
DupError(rc);
goto Error;
}
/* Send error output to the write end of the c_to_p pipe. */
fd = 2;
if (DosDupHandle(pipe_c_to_p, &fd) != NO_ERROR)
{
DupError(rc);
goto Error;
}
/*
* Ensure that the p_to_c pipe will close cleanly when the parent
* process (vile) is done with it.
*/
if (DosSetFHState(pipe_p_to_c, OPEN_FLAGS_NOINHERIT) != NO_ERROR)
goto Error;
/* Launch the command. */
if ((pid = _os2_command((char *)command)) < 0)
goto Error;
/*
* Ensure that the c_to_p pipe will close cleanly when the child
* process is done with it.
*/
DosClose(pipe_c_to_p);
pipe_c_to_p = NULLHANDLE;
/* Connect the read end of the c_to_p pipe to a stream. */
if (infile != NULL
&& (vile_in = *infile = _fdopen(pipe_p_from_c, "r")) == NULL)
goto Error;
TRACE(("inout_popen, vile_in #%d\n", fileno(vile_in)));
/* Connect the write end of the p_to_c pipe to a stream. */
if (outfile != NULL
&& (vile_out = *outfile = _fdopen(pipe_p_to_c, "w")) == NULL)
goto Error;
TRACE(("inout_popen, vile_out #%d\n", fileno(vile_out)));
/* Restore redirected file handles. */
if (save_in != NULLHANDLE)
{
fd = 0;
DosDupHandle(save_in, &fd);
DosClose(save_in);
TRACE(("inout_popen, dup 0: #%d, #%d\n", save_in, fd));
}
if (save_out != NULLHANDLE)
{
fd = 1;
DosDupHandle(save_out, &fd);
DosClose(save_out);
TRACE(("inout_popen, dup 1: #%d, #%d\n", save_out, fd));
}
if (save_err != NULLHANDLE)
{
fd = 2;
DosDupHandle(save_err, &fd);
DosClose(save_err);
TRACE(("inout_popen, dup 2: #%d, #%d\n", save_err, fd));
}
return TRUE;
Error:
TRACE(("inout_popen, Error\n"));
/* Restore redirected file handles. */
if (save_in != NULLHANDLE)
{
fd = 0;
DosDupHandle(save_in, &fd);
}
if (save_out != NULLHANDLE)
{
fd = 1;
DosDupHandle(save_out, &fd);
}
if (save_err != NULLHANDLE)
{
fd = 2;
DosDupHandle(save_err, &fd);
}
close_pipes();
return FALSE;
}
int
softfork(void)
{
return FALSE;
}
void
npflush(void)
{
if (vile_out != NULL)
{
TRACE(("npflush, vile_out #%d\n", fileno(vile_out)));
fflush(vile_out);
fclose(vile_out);
vile_out = NULL;
pipe_p_to_c = NULLHANDLE;
}
}
void
npclose(FILE *pipe_file)
{
int wait_status;
npflush();
_cwait(&wait_status, pid, WAIT_CHILD);
close_pipes();
}
|