30 #if defined (HAVE_CONFIG_H) 39 #if defined (__WIN32__) && ! defined (__CYGWIN__) 42 # define WIN32_LEAN_AND_MEAN 1 47 # include <sys/types.h> 53 #if defined (__WIN32__) && ! defined (__CYGWIN__) 56 make_command_string (
const char *cmd,
char *
const *args)
63 cmd_len =
strlen (cmd) + 3;
69 cmd_len +=
strlen (*argp) + 3;
71 command = (
char *) malloc (cmd_len);
73 sprintf (
command,
"\"%s\"", cmd);
83 octave_popen2 (
const char *cmd,
char *
const *args,
bool sync_mode,
84 int *fildes,
const char **errmsg)
91 PROCESS_INFORMATION
pi;
94 HANDLE hProcess = GetCurrentProcess ();
95 HANDLE childRead, childWrite, parentRead, parentWrite;
98 ZeroMemory (&
pi,
sizeof (
pi));
99 ZeroMemory (&si,
sizeof (si));
102 if (! CreatePipe (&childRead, &parentWrite, 0, 0)
103 || ! DuplicateHandle (hProcess, childRead, hProcess, &childRead,
105 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
107 *errmsg =
"popen2: pipe creation failed";
111 if (! CreatePipe (&parentRead, &childWrite, 0, 0)
112 || ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite,
114 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
116 *errmsg =
"popen2: pipe creation failed";
122 pipeMode = PIPE_NOWAIT;
123 SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0);
126 fildes[1] = _open_osfhandle ((intptr_t) parentRead, _O_RDONLY | _O_BINARY);
127 fildes[0] = _open_osfhandle ((intptr_t) parentWrite, _O_WRONLY | _O_BINARY);
129 si.dwFlags |= STARTF_USESTDHANDLES;
131 si.hStdInput = childRead;
132 si.hStdOutput = childWrite;
133 si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
135 command = make_command_string (cmd, args);
137 status = CreateProcess (0,
command, 0, 0, TRUE, 0, 0, 0, &si, &
pi);
143 *errmsg =
"popen2: process creation failed";
147 pid =
pi.dwProcessId;
149 CloseHandle (childRead);
150 CloseHandle (childWrite);
152 CloseHandle (
pi.hProcess);
153 CloseHandle (
pi.hThread);
162 int *fildes,
const char **errmsg)
166 int child_stdin[2], child_stdout[2];
168 if (
pipe (child_stdin) < 0)
170 *errmsg = strerror (errno);
174 if (
pipe (child_stdout) < 0)
176 close (child_stdin[0]);
177 close (child_stdin[1]);
179 *errmsg = strerror (errno);
189 close (child_stdin[1]);
190 close (child_stdout[0]);
194 close (child_stdin[0]);
196 if (
dup2 (child_stdout[1], STDOUT_FILENO) >= 0)
198 close (child_stdout[1]);
200 if (
execvp (cmd, args) < 0)
201 perror (
"popen2 (child)");
204 perror (
"popen2 (child)");
207 perror (
"popen2 (child)");
215 close (child_stdin[0]);
216 close (child_stdout[1]);
218 #if defined (F_SETFL) && defined (O_NONBLOCK) 219 if (! sync_mode &&
fcntl (child_stdout[0], F_SETFL, O_NONBLOCK) < 0)
221 *errmsg = strerror (errno);
227 fildes[0] = child_stdin[1];
228 fildes[1] = child_stdout[0];
235 *errmsg = strerror (errno);
int dup2(int old_fd, int new_fd)
int fcntl(int fd, int cmd, long arg)
to define functions rather than attempting to enter them directly on the command line The block of commands is executed as soon as you exit the editor To avoid executing any simply delete all the lines from the buffer before leaving the editor When invoked with no edit the previously executed command
pid_t octave_popen2(const char *cmd, char *const *args, bool sync_mode, int *fildes, const char **errmsg)
T::size_type strlen(const typename T::value_type *str)
int execvp(const std::string &file, const string_vector &argv)
pid_t fork(std::string &msg)