Lolly 1.4.27
Loading...
Searching...
No Matches
spawn.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : spawn.cpp
4 * DESCRIPTION: external command handling
5 * COPYRIGHT : (C) 2015 Denis RAUX
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "spawn.hpp"
13#include "array.hpp"
14#include <errno.h>
15#include <windows.h>
16
17Channel::Channel (int s) : sz (s) {
18 origin = -1;
19 saved = -1;
20 fd = -1;
21 toBeClosed= -1;
22 str = NULL;
23 tid = 0;
24}
25
26void
28 int pp[2];
29 if (_pipe (pp, sz, O_NOINHERIT | _O_BINARY) == 0) {
30 int pr[2];
31 if (_pipe (pr, sz, _O_BINARY) == 0) {
32 _close (pr[d]);
35 fd = pp[d];
37 }
38 else _close (pp[d]);
40 }
41}
42
43void
45 int pp[2];
46 origin= _fd;
47 if (_pipe (pp, sz, O_NOINHERIT | _O_BINARY) == 0) {
48 fd = pp[d];
50 }
51 else origin= -1;
52}
53
54void
56 if (origin < 0) return;
57 saved= _dup (origin);
59}
60
61void
62Channel::read (std::string* _str) {
63 str= _str;
64 tid= _beginthreadex (NULL, 0, bkgread, this, 0, NULL);
65}
66
67void
69 if (fd >= 0) {
70 _close (fd);
71 fd= -1;
72 }
73}
74
75void
77 if (toBeClosed >= 0) {
79 toBeClosed= -1;
80 }
81 restore ();
82}
83
84void
86 if (tid && WaitForSingleObject ((HANDLE) tid, 5000) == 0 &&
88 tid= 0;
89}
90
91void
93 if (saved >= 0) {
95 _close (saved);
96 saved= -1;
97 }
98}
99
101 closeUnused ();
102 close ();
103 if (tid) {
104 TerminateThread ((HANDLE) tid, -99);
106 }
107}
108
109unsigned
110bkgread (void* thatv) {
111 char buf[1024];
112 int cnt;
114 do {
115 cnt= _read (that->fd, buf, sizeof (buf));
116 if (cnt > 0) *(that->str)+= std::basic_string<char> (buf, cnt);
117 else if (cnt == 0) that->close ();
118 } while (cnt > 0);
119 return (cnt);
120}
121
123 const char* const* args)
124 : channel (ch) {
125 for (int i= 0; i < N (channel); ++i)
126 channel[i].redirect ();
127 pid= _spawnvp (_P_NOWAIT, name, args);
128 for (int i= 0; i < N (channel); ++i)
129 channel[i].closeUnused ();
130}
131
132int
134 int ret;
135 if (pid > 0) ret= _cwait (&ret, pid, 0) == -1 ? errno : ret;
136 else ret= EINVAL;
137 for (int i= 0; i < N (channel); ++i)
138 channel[i].wait ();
139 return (ret);
140}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
const int sz
Definition spawn.hpp:38
void Init(int fd, Direction d)
Definition spawn.cpp:44
int fd
Definition spawn.hpp:47
std::string * str
Definition spawn.hpp:48
~Channel()
Definition spawn.cpp:100
Direction
Definition spawn.hpp:24
void read(std::string *str)
Definition spawn.cpp:62
Channel(int s=2048)
Definition spawn.cpp:17
void closeUnused()
Definition spawn.cpp:76
friend unsigned int bkgread(void *)
Definition spawn.cpp:110
int saved
Definition spawn.hpp:46
void close()
Definition spawn.cpp:68
int toBeClosed
Definition spawn.hpp:44
void restore()
Definition spawn.cpp:92
void redirect()
Definition spawn.cpp:55
int origin
Definition spawn.hpp:45
enum Direction otherDirection(Direction t)
Definition spawn.hpp:41
void wait()
Definition spawn.cpp:85
uintptr_t tid
Definition spawn.hpp:49
The list class represents a linked list.
Definition list.hpp:48
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137
int wait()
Definition spawn.cpp:133
intptr_t pid
Definition spawn.hpp:61
array< Channel > & channel
Definition spawn.hpp:62
spawn_system(array< Channel > &ch, char *name, const char *const *args)
Definition spawn.cpp:122
unsigned bkgread(void *thatv)
Definition spawn.cpp:110