forked from mkhoeini/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.d
1416 lines (1156 loc) · 40.3 KB
/
process.d
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copy of the new std.process that is currently stuck waiting on changes
// to druntime, and for review. Since I don't need the parts affected by
// the needed druntime changes, using it here should be ok.
// Written in the D programming language.
/** This is a proposal for a replacement for the $(D std._process) module.
This is a summary of the functions in this module:
$(UL $(LI
$(LREF spawnProcess) spawns a new _process, optionally assigning it an
arbitrary set of standard input, output, and error streams.
The function returns immediately, leaving the child _process to execute
in parallel with its parent. All other functions in this module that
spawn processes are built around $(LREF spawnProcess).)
$(LI
$(LREF wait) makes the parent _process wait for a child _process to
terminate. In general one should always do this, to avoid
child _processes becoming "zombies" when the parent _process exits.
Scope guards are perfect for this – see the $(LREF spawnProcess)
documentation for examples.)
$(LI
$(LREF pipeProcess) and $(LREF pipeShell) also spawn a child _process
which runs in parallel with its parent. However, instead of taking
arbitrary streams, they automatically create a set of
pipes that allow the parent to communicate with the child
through the child's standard input, output, and/or error streams.
These functions correspond roughly to C's $(D popen) function.)
$(LI
$(LREF execute) and $(LREF shell) start a new _process and wait for it
to complete before returning. Additionally, they capture
the _process' standard output and error streams and return
the output of these as a string.
These correspond roughly to C's $(D system) function.)
)
$(LREF shell) and $(LREF pipeShell) both run the given command
through the user's default command interpreter. On Windows, this is
the $(I cmd.exe) program, on POSIX it is determined by the SHELL environment
variable (defaulting to $(I /bin/sh) if it cannot be determined). The
command is specified as a single string which is sent directly to the
shell.
The other commands all have two forms, one where the program name
and its arguments are specified in a single string parameter, separated
by spaces, and one where the arguments are specified as an array of
strings. Use the latter whenever the program name or any of the arguments
contain spaces.
Unless a directory is specified in the program name, all functions will
search for the executable in the directories specified in the PATH
environment variable.
Macros:
WIKI=Phobos/StdProcess
*/
module process;
version(Posix)
{
import core.stdc.errno;
import core.stdc.string;
import core.sys.posix.stdio;
import core.sys.posix.unistd;
import core.sys.posix.sys.wait;
}
version(Windows)
{
import core.sys.windows.windows;
import std.utf;
import std.windows.syserror;
import std.c.stdio;
version(DigitalMars)
{
// this helps on Wine
version = PIPE_USE_ALT_FDOPEN;
}
}
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.path;
import std.stdio;
import std.string;
import std.typecons;
version(Posix)
{
version(OSX)
{
// https://www.gnu.org/software/gnulib/manual/html_node/environ.html
private extern(C) extern __gshared char*** _NSGetEnviron();
// need to declare environ = *_NSGetEnviron() in static this()
}
else
{
// Made available by the C runtime:
private extern(C) extern __gshared const char** environ;
}
}
else version(Windows)
{
// Use the same spawnProcess() implementations on both Windows
// and POSIX, only the spawnProcessImpl() function has to be
// different.
LPVOID environ = null;
// TODO: This should be in druntime!
extern(Windows)
{
alias WCHAR* LPWCH;
LPWCH GetEnvironmentStringsW();
BOOL FreeEnvironmentStringsW(LPWCH lpszEnvironmentBlock);
DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer,
DWORD nSize);
BOOL SetEnvironmentVariableW(LPCWSTR lpName, LPCWSTR lpValue);
}
}
/** A handle corresponding to a spawned process. */
final class Pid
{
/** The ID number assigned to the process by the operating
system.
*/
@property int processID() const
{
enforce(_processID >= 0,
"Pid doesn't correspond to a running process.");
return _processID;
}
// See module-level wait() for documentation.
version(Posix) int wait()
{
if (_processID == terminated) return _exitCode;
int exitCode;
while(true)
{
int status;
auto check = waitpid(processID, &status, 0);
enforce (check != -1 || errno != ECHILD,
"Process does not exist or is not a child process.");
if (WIFEXITED(status))
{
exitCode = WEXITSTATUS(status);
break;
}
else if (WIFSIGNALED(status))
{
exitCode = -WTERMSIG(status);
break;
}
// Process has stopped, but not terminated, so we continue waiting.
}
// Mark Pid as terminated, and cache and return exit code.
_processID = terminated;
_exitCode = exitCode;
return exitCode;
}
else version(Windows)
{
int wait()
{
if (_processID == terminated) return _exitCode;
if(_handle != INVALID_HANDLE_VALUE)
{
auto result = WaitForSingleObject(_handle, INFINITE);
enforce(result == WAIT_OBJECT_0, "Wait failed");
// the process has exited, get the return code
enforce(GetExitCodeProcess(_handle, cast(LPDWORD)&_exitCode));
CloseHandle(_handle);
_handle = INVALID_HANDLE_VALUE;
_processID = terminated;
}
return _exitCode;
}
~this()
{
if(_handle != INVALID_HANDLE_VALUE)
{
CloseHandle(_handle);
_handle = INVALID_HANDLE_VALUE;
}
}
}
private:
// Special values for _processID.
enum invalid = -1, terminated = -2;
// OS process ID number. Only nonnegative IDs correspond to
// running processes.
int _processID = invalid;
// Exit code cached by wait(). This is only expected to hold a
// sensible value if _processID == terminated.
int _exitCode;
// Pids are only meant to be constructed inside this module, so
// we make the constructor private.
version(Windows)
{
HANDLE _handle;
this(int pid, HANDLE handle)
{
_processID = pid;
_handle = handle;
}
}
else
{
this(int id)
{
_processID = id;
}
}
}
/** Spawns a new process.
This function returns immediately, and the child process
executes in parallel with its parent.
Unless a directory is specified in the $(D _command) (or $(D name))
parameter, this function will search the directories in the
PATH environment variable for the program. To run an executable in
the current directory, use $(D "./$(I executable_name)").
Params:
command = A string containing the program name and
its arguments, separated by spaces. If the program
name or any of the arguments contain spaces, use
the third or fourth form of this function, where
they are specified separately.
environmentVars = The environment variables for the
child process can be specified using this parameter.
If it is omitted, the child process executes in the
same environment as the parent process.
stdin_ = The standard input stream of the child process.
This can be any $(XREF stdio,File) that is opened for reading.
By default the child process inherits the parent's input
stream.
stdout_ = The standard output stream of the child process.
This can be any $(XREF stdio,File) that is opened for writing.
By default the child process inherits the parent's output
stream.
stderr_ = The standard error stream of the child process.
This can be any $(XREF stdio,File) that is opened for writing.
By default the child process inherits the parent's error
stream.
config = Options controlling the behaviour of $(D spawnProcess).
See the $(LREF Config) documentation for details.
name = The name of the executable file.
args = The _command line arguments to give to the program.
(There is no need to specify the program name as the
zeroth argument; this is done automatically.)
Note:
If you pass an $(XREF stdio,File) object that is $(I not) one of the standard
input/output/error streams of the parent process, that stream
will by default be closed in the parent process when this
function returns. See the $(LREF Config) documentation below for information
about how to disable this behaviour.
Examples:
Open Firefox on the D homepage and wait for it to complete:
---
auto pid = spawnProcess("firefox http://www.d-programming-language.org");
wait(pid);
---
Use the $(I ls) _command to retrieve a list of files:
---
string[] files;
auto p = pipe();
auto pid = spawnProcess("ls", stdin, p.writeEnd);
scope(exit) wait(pid);
foreach (f; p.readEnd.byLine()) files ~= f.idup;
---
Use the $(I ls -l) _command to get a list of files, pipe the output
to $(I grep) and let it filter out all files except D source files,
and write the output to the file $(I dfiles.txt):
---
// Let's emulate the command "ls -l | grep \.d > dfiles.txt"
auto p = pipe();
auto file = File("dfiles.txt", "w");
auto lsPid = spawnProcess("ls -l", stdin, p.writeEnd);
scope(exit) wait(lsPid);
auto grPid = spawnProcess("grep \\.d", p.readEnd, file);
scope(exit) wait(grPid);
---
Open a set of files in OpenOffice Writer, and make it print
any error messages to the standard output stream. Note that since
the filenames contain spaces, we have to pass them in an array:
---
spawnProcess("oowriter", ["my document.odt", "your document.odt"],
stdin, stdout, stdout);
---
*/
Pid spawnProcess(string command,
File stdin_ = std.stdio.stdin,
File stdout_ = std.stdio.stdout,
File stderr_ = std.stdio.stderr,
Config config = Config.none)
{
auto splitCmd = split(command);
return spawnProcessImpl(splitCmd[0], splitCmd[1 .. $],
environ,
stdin_, stdout_, stderr_, config);
}
/// ditto
Pid spawnProcess(string command, string[string] environmentVars,
File stdin_ = std.stdio.stdin,
File stdout_ = std.stdio.stdout,
File stderr_ = std.stdio.stderr,
Config config = Config.none)
{
auto splitCmd = split(command);
return spawnProcessImpl(splitCmd[0], splitCmd[1 .. $],
toEnvz(environmentVars),
stdin_, stdout_, stderr_, config);
}
/// ditto
Pid spawnProcess(string name, const string[] args,
File stdin_ = std.stdio.stdin,
File stdout_ = std.stdio.stdout,
File stderr_ = std.stdio.stderr,
Config config = Config.none)
{
return spawnProcessImpl(name, args,
environ,
stdin_, stdout_, stderr_, config);
}
/// ditto
Pid spawnProcess(string name, const string[] args,
string[string] environmentVars,
File stdin_ = std.stdio.stdin,
File stdout_ = std.stdio.stdout,
File stderr_ = std.stdio.stderr,
Config config = Config.none)
{
return spawnProcessImpl(name, args,
toEnvz(environmentVars),
stdin_, stdout_, stderr_, config);
}
// The actual implementation of the above.
version(Posix) private Pid spawnProcessImpl
(string name, const string[] args, const char** envz,
File stdin_, File stdout_, File stderr_, Config config)
{
string fullName = name;
// Make sure the file exists and is executable.
if (any!isDirSeparator(name))
{
enforce(isExecutable(fullName), "Not an executable file: "~name);
}
else
{
fullName = searchPathFor(name);
enforce(fullName != null, "Executable file not found: "~name);
}
// Get the file descriptors of the streams.
auto stdinFD = core.stdc.stdio.fileno(stdin_.getFP());
errnoEnforce(stdinFD != -1, "Invalid stdin stream");
auto stdoutFD = core.stdc.stdio.fileno(stdout_.getFP());
errnoEnforce(stdoutFD != -1, "Invalid stdout stream");
auto stderrFD = core.stdc.stdio.fileno(stderr_.getFP());
errnoEnforce(stderrFD != -1, "Invalid stderr stream");
auto argz = toArgz(fullName, args);
auto namez = toStringz(fullName);
auto id = fork();
errnoEnforce (id >= 0, "Cannot spawn new process");
if (id == 0)
{
// Child process
// Redirect streams and close the old file descriptors.
// In the case that stderr is redirected to stdout, we need
// to backup the file descriptor since stdout may be redirected
// as well.
if (stderrFD == STDOUT_FILENO) stderrFD = dup(stderrFD);
dup2(stdinFD, STDIN_FILENO);
dup2(stdoutFD, STDOUT_FILENO);
dup2(stderrFD, STDERR_FILENO);
// Close the old file descriptors, unless they are
// either of the standard streams.
if (stdinFD > STDERR_FILENO) close(stdinFD);
if (stdoutFD > STDERR_FILENO) close(stdoutFD);
if (stderrFD > STDERR_FILENO) close(stderrFD);
// Execute program
execve(namez, argz, envz);
// If execution fails, exit as quick as possible.
perror("spawnProcess(): Failed to execute program");
_exit(1);
assert (0);
}
else
{
// Parent process: Close streams and return.
with (Config)
{
if (stdinFD > STDERR_FILENO && !(config & noCloseStdin))
stdin_.close();
if (stdoutFD > STDERR_FILENO && !(config & noCloseStdout))
stdout_.close();
if (stderrFD > STDERR_FILENO && !(config & noCloseStderr))
stderr_.close();
}
return new Pid(id);
}
}
else version(Windows) private Pid spawnProcessImpl
(string name, const string[] args, LPVOID envz,
File stdin_, File stdout_, File stderr_, Config config)
{
// Create a process info structure. Note that we don't care about wide
// characters yet.
STARTUPINFO startinfo;
startinfo.cb = startinfo.sizeof;
// Create a process information structure.
PROCESS_INFORMATION pi;
//
// Windows is a little strange when passing command line. It requires the
// command-line to be one single command line, and the quoting processing
// is rather bizzare. Through trial and error, here are the rules I've
// discovered that Windows uses to parse the command line WRT quotes:
//
// inside or outside quote mode:
// 1. if 2 or more backslashes are followed by a quote, the first
// 2 backslashes are reduced to 1 backslash which does not
// affect anything after it.
// 2. one backslash followed by a quote is interpreted as a
// literal quote, which cannot be used to close quote mode, and
// does not affect anything after it.
//
// outside quote mode:
// 3. a quote enters quote mode
// 4. whitespace delineates an argument
//
// inside quote mode:
// 5. 2 quotes sequentially are interpreted as a literal quote and
// an exit from quote mode.
// 6. a quote at the end of the string, or one that is followed by
// anything other than a quote exits quote mode, but does not
// affect the character after the quote.
// 7. end of line exits quote mode
//
// In our 'reverse' routine, we will only utilize the first 2 rules
// for escapes.
//
char[] cmdline;
uint minsize = 0;
foreach(s; args)
minsize += args.length;
// reserve enough space to hold the program and all the arguments, plus 3
// extra characters per arg for the quotes and the space, plus 5 extra
// chars for good measure (in case we have to add escaped quotes).
cmdline.reserve(minsize + name.length + 3 * args.length + 5);
// this could be written more optimized...
void addArg(string a)
{
if(cmdline.length)
cmdline ~= " ";
// first, determine if we need a quote
bool needquote = false;
foreach(dchar d; a)
if(d == ' ')
{
needquote = true;
break;
}
if(needquote)
cmdline ~= '"';
foreach(dchar d; a)
{
if(d == '"')
cmdline ~= '\\';
cmdline ~= d;
}
if(needquote)
cmdline ~= '"';
}
addArg(name);
foreach(a; args)
addArg(a);
cmdline ~= '\0';
// ok, the command line is ready. Figure out the startup info
startinfo.dwFlags = STARTF_USESTDHANDLES;
// Get the file descriptors of the streams.
auto stdinFD = _fileno(stdin_.getFP());
errnoEnforce(stdinFD != -1, "Invalid stdin stream");
auto stdoutFD = _fileno(stdout_.getFP());
errnoEnforce(stdoutFD != -1, "Invalid stdout stream");
auto stderrFD = _fileno(stderr_.getFP());
errnoEnforce(stderrFD != -1, "Invalid stderr stream");
// need to convert file descriptors to HANDLEs
startinfo.hStdInput = _fdToHandle(stdinFD);
startinfo.hStdOutput = _fdToHandle(stdoutFD);
startinfo.hStdError = _fdToHandle(stderrFD);
// TODO: need to fix this for unicode
if(!CreateProcessA(null, cmdline.ptr, null, null, true, (config & Config.gui) ? CREATE_NO_WINDOW : 0, envz, null, &startinfo, &pi))
{
throw new Exception("Error starting process: " ~ sysErrorString(GetLastError()), __FILE__, __LINE__);
}
// figure out if we should close any of the streams
with (Config)
{
if (stdinFD > STDERR_FILENO && !(config & noCloseStdin))
stdin_.close();
if (stdoutFD > STDERR_FILENO && !(config & noCloseStdout))
stdout_.close();
if (stderrFD > STDERR_FILENO && !(config & noCloseStderr))
stderr_.close();
}
// close the thread handle in the process info structure
CloseHandle(pi.hThread);
return new Pid(pi.dwProcessId, pi.hProcess);
}
// Searches the PATH variable for the given executable file,
// (checking that it is in fact executable).
version(Posix) private string searchPathFor(string executable)
{
auto pathz = environment["PATH"];
if (pathz == null) return null;
foreach (dir; splitter(to!string(pathz), ':'))
{
auto execPath = buildPath(dir, executable);
if (isExecutable(execPath)) return execPath;
}
return null;
}
// Converts a C array of C strings to a string[] array,
// setting the program name as the zeroth element.
version(Posix) private const(char)** toArgz(string prog, const string[] args)
{
alias const(char)* stringz_t;
auto argz = new stringz_t[](args.length+2);
argz[0] = toStringz(prog);
foreach (i; 0 .. args.length)
{
argz[i+1] = toStringz(args[i]);
}
argz[$-1] = null;
return argz.ptr;
}
// Converts a string[string] array to a C array of C strings
// on the form "key=value".
version(Posix) private const(char)** toEnvz(const string[string] env)
{
alias const(char)* stringz_t;
auto envz = new stringz_t[](env.length+1);
int i = 0;
foreach (k, v; env)
{
envz[i] = (k~'='~v~'\0').ptr;
i++;
}
envz[$-1] = null;
return envz.ptr;
}
else version(Windows) private LPVOID toEnvz(const string[string] env)
{
uint len = 1; // reserve 1 byte for termination of environment block
foreach(k, v; env)
{
len += k.length + v.length + 2; // one for '=', one for null char
}
char [] envz;
envz.reserve(len);
foreach(k, v; env)
{
envz ~= k ~ '=' ~ v ~ '\0';
}
envz ~= '\0';
return envz.ptr;
}
// Checks whether the file exists and can be executed by the
// current user.
version(Posix) private bool isExecutable(string path)
{
return (access(toStringz(path), X_OK) == 0);
}
/** Flags that control the behaviour of $(LREF spawnProcess).
Use bitwise OR to combine flags.
Example:
---
auto logFile = File("myapp_error.log", "w");
// Start program in a console window (Windows only), redirect
// its error stream to logFile, and leave logFile open in the
// parent process as well.
auto pid = spawnProcess("myapp", stdin, stdout, logFile,
Config.noCloseStderr | Config.gui);
scope(exit)
{
auto exitCode = wait(pid);
logFile.writeln("myapp exited with code ", exitCode);
logFile.close();
}
---
*/
enum Config
{
none = 0,
/** Unless the child process inherits the standard
input/output/error streams of its parent, one almost
always wants the streams closed in the parent when
$(LREF spawnProcess) returns. Therefore, by default, this
is done. If this is not desirable, pass any of these
options to spawnProcess.
*/
noCloseStdin = 1,
noCloseStdout = 2, /// ditto
noCloseStderr = 4, /// ditto
/** On Windows, this option causes the process to run in
a console window. On POSIX it has no effect.
*/
gui = 8,
}
/** Waits for a specific spawned process to terminate and returns
its exit status.
In general one should always _wait for child processes to terminate
before exiting the parent process. Otherwise, they may become
"$(WEB en.wikipedia.org/wiki/Zombie_process,zombies)" – processes
that are defunct, yet still occupy a slot in the OS process table.
Note:
On POSIX systems, if the process is terminated by a signal,
this function returns a negative number whose absolute value
is the signal number. (POSIX restricts normal exit codes
to the range 0-255.)
Examples:
See the $(LREF spawnProcess) documentation.
*/
int wait(Pid pid)
{
enforce(pid !is null, "Called wait on a null Pid.");
return pid.wait();
}
/+
/** Creates a unidirectional _pipe.
Data is written to one end of the _pipe and read from the other.
---
auto p = pipe();
p.writeEnd.writeln("Hello World");
assert (p.readEnd.readln().chomp() == "Hello World");
---
Pipes can, for example, be used for interprocess communication
by spawning a new process and passing one end of the _pipe to
the child, while the parent uses the other end. See the
$(LREF spawnProcess) documentation for examples of this.
*/
version(Posix) Pipe pipe()
{
int[2] fds;
errnoEnforce(core.sys.posix.unistd.pipe(fds) == 0,
"Unable to create pipe");
Pipe p;
// TODO: Using the internals of File like this feels like a hack,
// but the File.wrapFile() function disables automatic closing of
// the file. Perhaps there should be a protected version of
// wrapFile() that fills this purpose?
p._read.p = new File.Impl(
errnoEnforce(fdopen(fds[0], "r"), "Cannot open read end of pipe"),
1, null);
p._write.p = new File.Impl(
errnoEnforce(fdopen(fds[1], "w"), "Cannot open write end of pipe"),
1, null);
return p;
}
else version(Windows) Pipe pipe()
{
// use CreatePipe to create an anonymous pipe
HANDLE readHandle;
HANDLE writeHandle;
SECURITY_ATTRIBUTES sa;
sa.nLength = sa.sizeof;
sa.lpSecurityDescriptor = null;
sa.bInheritHandle = true;
if(!CreatePipe(&readHandle, &writeHandle, &sa, 0))
{
throw new Exception("Error creating pipe: " ~ sysErrorString(GetLastError()), __FILE__, __LINE__);
}
// Create file descriptors from the handles
auto readfd = _handleToFD(readHandle, FHND_DEVICE);
auto writefd = _handleToFD(writeHandle, FHND_DEVICE);
Pipe p;
version(PIPE_USE_ALT_FDOPEN)
{
// This is a re-implementation of DMC's fdopen, but without the
// mucking with the file descriptor. POSIX standard requires the
// new fdopen'd file to retain the given file descriptor's
// position.
FILE * local_fdopen(int fd, const(char)* mode)
{
auto fp = core.stdc.stdio.fopen("NUL", mode);
if(!fp)
return null;
FLOCK(fp);
auto iob = cast(_iobuf*)fp;
.close(iob._file);
iob._file = fd;
iob._flag &= ~_IOTRAN;
FUNLOCK(fp);
return fp;
}
p._read.p = new File.Impl(
errnoEnforce(local_fdopen(readfd, "r"), "Cannot open read end of pipe"),
1, null);
p._write.p = new File.Impl(
errnoEnforce(local_fdopen(writefd, "a"), "Cannot open write end of pipe"),
1, null);
}
else
{
p._read.p = new File.Impl(
errnoEnforce(fdopen(readfd, "r"), "Cannot open read end of pipe"),
1, null);
p._write.p = new File.Impl(
errnoEnforce(fdopen(writefd, "a"), "Cannot open write end of pipe"),
1, null);
}
return p;
}
/// ditto
struct Pipe
{
/** The read end of the pipe. */
@property File readEnd() { return _read; }
/** The write end of the pipe. */
@property File writeEnd() { return _write; }
/** Closes both ends of the pipe.
Normally it is not necessary to do this manually, as $(XREF stdio,File)
objects are automatically closed when there are no more references
to them.
Note that if either end of the pipe has been passed to a child process,
it will only be closed in the parent process.
*/
void close()
{
_read.close();
_write.close();
}
private:
File _read, _write;
}
unittest
{
auto p = pipe();
p.writeEnd.writeln("Hello World");
assert (p.readEnd.readln().chomp() == "Hello World");
}
// ============================== pipeProcess() ==============================
/** Starts a new process, creating pipes to redirect its standard
input, output and/or error streams.
These functions return immediately, leaving the child process to
execute in parallel with the parent.
$(LREF pipeShell) invokes the user's _command interpreter
to execute the given program or _command.
Example:
---
auto pipes = pipeProcess("my_application");
// Store lines of output.
string[] output;
foreach (line; pipes.stdout.byLine) output ~= line.idup;
// Store lines of errors.
string[] errors;
foreach (line; pipes.stderr.byLine) errors ~= line.idup;
---
*/
ProcessPipes pipeProcess(string command,
Redirect redirectFlags = Redirect.all)
{
auto splitCmd = split(command);
return pipeProcess(splitCmd[0], splitCmd[1 .. $], redirectFlags);
}
/// ditto
ProcessPipes pipeProcess(string name, string[] args,
Redirect redirectFlags = Redirect.all)
{
File stdinFile, stdoutFile, stderrFile;
ProcessPipes pipes;
pipes._redirectFlags = redirectFlags;
if (redirectFlags & Redirect.stdin)
{
auto p = pipe();
stdinFile = p.readEnd;
pipes._stdin = p.writeEnd;
}
else
{
stdinFile = std.stdio.stdin;
}
if (redirectFlags & Redirect.stdout)
{
enforce((redirectFlags & Redirect.stdoutToStderr) == 0,
"Invalid combination of options: Redirect.stdout | "
~"Redirect.stdoutToStderr");
auto p = pipe();
stdoutFile = p.writeEnd;
pipes._stdout = p.readEnd;
}
else
{
stdoutFile = std.stdio.stdout;
}
if (redirectFlags & Redirect.stderr)
{
enforce((redirectFlags & Redirect.stderrToStdout) == 0,
"Invalid combination of options: Redirect.stderr | "
~"Redirect.stderrToStdout");
auto p = pipe();
stderrFile = p.writeEnd;
pipes._stderr = p.readEnd;
}
else
{
stderrFile = std.stdio.stderr;
}
if (redirectFlags & Redirect.stdoutToStderr)
{
if (redirectFlags & Redirect.stderrToStdout)
{
// We know that neither of the other options have been
// set, so we assign the std.stdio.std* streams directly.
stdoutFile = std.stdio.stderr;
stderrFile = std.stdio.stdout;
}
else
{
stdoutFile = stderrFile;
}
}
else if (redirectFlags & Redirect.stderrToStdout)
{
stderrFile = stdoutFile;
}
pipes._pid = spawnProcess(name, args, stdinFile, stdoutFile, stderrFile);
return pipes;
}
/// ditto
ProcessPipes pipeShell(string command, Redirect redirectFlags = Redirect.all)
{
return pipeProcess(getShell(), [shellSwitch, command], redirectFlags);
}
/** Flags that can be passed to $(LREF pipeProcess) and $(LREF pipeShell)
to specify which of the child process' standard streams are redirected.
Use bitwise OR to combine flags.
*/
enum Redirect
{
none = 0,
/** Redirect the standard input, output or error streams, respectively. */
stdin = 1,
stdout = 2, /// ditto
stderr = 4, /// ditto
all = stdin | stdout | stderr, /// ditto
/** Redirect the standard error stream into the standard output
stream, and vice versa.
*/
stderrToStdout = 8,
stdoutToStderr = 16, /// ditto
}
/** Object containing $(XREF stdio,File) handles that allow communication with
a child process through its standard streams.