-
Notifications
You must be signed in to change notification settings - Fork 12
/
pstream.h
2451 lines (2190 loc) · 73.9 KB
/
pstream.h
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
// PStreams - POSIX Process I/O for C++
// Copyright (C) 2001 - 2024 Jonathan Wakely
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// SPDX-License-Identifier: BSL-1.0
/**
* @file pstream.h
* @brief Declares all PStreams classes.
* @author Jonathan Wakely
*
* Defines classes redi::ipstream, redi::opstream, redi::pstream
* and redi::rpstream.
*/
#ifndef REDI_PSTREAM_H_SEEN
#define REDI_PSTREAM_H_SEEN
#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>
#include <string>
#include <vector>
#include <algorithm> // for min()
#include <cerrno> // for errno
#include <cstddef> // for size_t, NULL
#include <cstdlib> // for exit()
#include <sys/types.h> // for pid_t
#include <sys/wait.h> // for waitpid()
#include <sys/ioctl.h> // for ioctl() and FIONREAD
#if defined(__sun)
# include <sys/filio.h> // for FIONREAD on Solaris 2.5
#endif
#include <unistd.h> // for pipe() fork() exec() and filedes functions
#include <signal.h> // for kill()
#include <fcntl.h> // for fcntl()
#if REDI_EVISCERATE_PSTREAMS
# include <stdio.h> // for FILE, fdopen()
#endif
/// The library version.
#define PSTREAMS_VERSION 0x0104 // 1.0.4
/**
* @namespace redi
* @brief All PStreams classes are declared in namespace redi.
*
* Like the standard iostreams, PStreams is a set of class templates,
* taking a character type and traits type. As with the standard streams
* they are most likely to be used with @c char and the default
* traits type, so typedefs for this most common case are provided.
*
* The @c pstream_common class template is not intended to be used directly,
* it is used internally to provide the common functionality for the
* other stream classes.
*/
namespace redi
{
/// Common base class providing constants and typenames.
struct pstreams
{
/// Type used to specify how to connect to the process.
typedef std::ios_base::openmode pmode;
/// Type used to hold the arguments for a command.
typedef std::vector<std::string> argv_type;
/// Type used for file descriptors.
typedef int fd_type;
static const pmode pstdin = std::ios_base::out; ///< Write to stdin
static const pmode pstdout = std::ios_base::in; ///< Read from stdout
static const pmode pstderr = std::ios_base::app; ///< Read from stderr
/// Create a new process group for the child process.
static const pmode newpg = std::ios_base::trunc;
protected:
enum {
bufsz = 32, ///< Size of pstreambuf buffers.
pbsz = 2 ///< Number of putback characters kept.
};
#if __cplusplus >= 201103L
template<typename T>
using stringable = decltype((void)std::string(std::declval<const T&>()));
#endif
};
/// Class template for stream buffer.
template <typename CharT, typename Traits = std::char_traits<CharT> >
class basic_pstreambuf
: public std::basic_streambuf<CharT, Traits>
, public pstreams
{
public:
// Type definitions for dependent types
typedef CharT char_type;
typedef Traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::off_type off_type;
typedef typename traits_type::pos_type pos_type;
/** @deprecated use pstreams::fd_type instead. */
typedef fd_type fd_t;
/// Default constructor.
basic_pstreambuf();
/// Constructor that initialises the buffer with @a cmd.
basic_pstreambuf(const std::string& cmd, pmode mode);
/// Constructor that initialises the buffer with @a file and @a argv.
basic_pstreambuf( const std::string& file,
const argv_type& argv,
pmode mode );
#if __cplusplus >= 201103L
basic_pstreambuf(basic_pstreambuf&&) noexcept;
basic_pstreambuf& operator=(basic_pstreambuf&&) noexcept;
void swap(basic_pstreambuf&) noexcept;
#endif
/// Destructor.
~basic_pstreambuf();
/// Initialise the stream buffer with @a cmd.
basic_pstreambuf*
open(const std::string& cmd, pmode mode);
/// Initialise the stream buffer with @a file and @a argv.
basic_pstreambuf*
open(const std::string& file, const argv_type& argv, pmode mode);
/// Close the stream buffer and wait for the process to exit.
basic_pstreambuf*
close();
/// Send a signal to the process.
basic_pstreambuf*
kill(int signal = SIGTERM);
/// Send a signal to the process' process group.
basic_pstreambuf*
killpg(int signal = SIGTERM);
/// Close the pipe connected to the process' stdin.
void
peof();
/// Change active input source.
bool
read_err(bool readerr = true);
/// Report whether the stream buffer has been initialised.
bool
is_open() const;
/// Report whether the process has exited.
bool
exited();
#if REDI_EVISCERATE_PSTREAMS
/// Obtain FILE pointers for each of the process' standard streams.
std::size_t
fopen(FILE*& in, FILE*& out, FILE*& err);
#endif
/// Return the exit status of the process.
int
status() const;
/// Return the error number (errno) for the most recent failed operation.
int
error() const;
protected:
/// Transfer characters to the pipe when character buffer overflows.
int_type
overflow(int_type c);
/// Transfer characters from the pipe when the character buffer is empty.
int_type
underflow();
/// Make a character available to be returned by the next extraction.
int_type
pbackfail(int_type c = traits_type::eof());
/// Write any buffered characters to the stream.
int
sync();
/// Insert multiple characters into the pipe.
std::streamsize
xsputn(const char_type* s, std::streamsize n);
/// Insert a sequence of characters into the pipe.
std::streamsize
write(const char_type* s, std::streamsize n);
/// Extract a sequence of characters from the pipe.
std::streamsize
read(char_type* s, std::streamsize n);
/// Report how many characters can be read from active input without blocking.
std::streamsize
showmanyc();
protected:
/// Enumerated type to indicate whether stdout or stderr is to be read.
enum buf_read_src { rsrc_out = 0, rsrc_err = 1 };
/// Initialise pipes and fork process.
pid_t
fork(pmode mode);
/// Wait for the child process to exit.
int
wait(bool nohang = false);
/// Return the file descriptor for the output pipe.
fd_type&
wpipe();
/// Return the file descriptor for the active input pipe.
fd_type&
rpipe();
/// Return the file descriptor for the specified input pipe.
fd_type&
rpipe(buf_read_src which);
void
create_buffers(pmode mode);
void
destroy_buffers(pmode mode);
/// Writes buffered characters to the process' stdin pipe.
bool
empty_buffer();
bool
fill_buffer(bool non_blocking = false);
/// Return the active input buffer.
char_type*
rbuffer();
buf_read_src
switch_read_buffer(buf_read_src);
private:
#if __cplusplus >= 201103L
using basic_streambuf = std::basic_streambuf<char_type, traits_type>;
#else
basic_pstreambuf(const basic_pstreambuf&);
basic_pstreambuf& operator=(const basic_pstreambuf&);
#endif
void
init_rbuffers();
pid_t ppid_; // pid of process
fd_type wpipe_; // pipe used to write to process' stdin
fd_type rpipe_[2]; // two pipes to read from, stdout and stderr
char_type* wbuffer_;
char_type* rbuffer_[2];
char_type* rbufstate_[3];
/// Index into rpipe_[] to indicate active source for read operations.
buf_read_src rsrc_;
int status_; // hold exit status of child process
int error_; // hold errno if fork() or exec() fails
};
/// Class template for common base class.
template <typename CharT, typename Traits = std::char_traits<CharT> >
class pstream_common
: virtual public std::basic_ios<CharT, Traits>
, virtual public pstreams
{
protected:
typedef basic_pstreambuf<CharT, Traits> streambuf_type;
typedef std::basic_ios<CharT, Traits> ios_type;
typedef pstreams::pmode pmode;
typedef pstreams::argv_type argv_type;
/// Default constructor.
pstream_common();
/// Constructor that initialises the stream by starting a process.
pstream_common(const std::string& cmd, pmode mode);
/// Constructor that initialises the stream by starting a process.
pstream_common(const std::string& file, const argv_type& argv, pmode mode);
/// Pure virtual destructor.
virtual
~pstream_common() = 0;
#if __cplusplus >= 201103L
pstream_common(pstream_common&& rhs) noexcept
: command_(std::move(rhs.command_))
, buf_(std::move(rhs.buf_))
{
/* derived class is responsible for ios_type::move(rhs) happening */
}
pstream_common&
operator=(pstream_common&& rhs) noexcept
{
command_ = std::move(rhs.command_);
buf_ = std::move(rhs.buf_);
return *this;
}
void
swap(pstream_common& rhs) noexcept
{
/* derived class is responsible for ios_type::swap(rhs) happening */
command_.swap(rhs.command_);
buf_.swap(rhs.buf_);
}
#endif // C++11
/// Start a process.
void
do_open(const std::string& cmd, pmode mode);
/// Start a process.
void
do_open(const std::string& file, const argv_type& argv, pmode mode);
public:
/// Close the pipe, returning the program's exit status, as
/// pclose(3) does.
int
close();
/// Report whether the stream's buffer has been initialised.
bool
is_open() const;
/// Return the command used to initialise the stream.
const std::string&
command() const;
/// Return a pointer to the stream buffer.
streambuf_type*
rdbuf() const;
#if REDI_EVISCERATE_PSTREAMS
/// Obtain FILE pointers for each of the process' standard streams.
std::size_t
fopen(FILE*& in, FILE*& out, FILE*& err);
#endif
protected:
std::string command_; ///< The command used to start the process.
streambuf_type buf_; ///< The stream buffer.
};
/**
* @class basic_ipstream
* @brief Class template for Input PStreams.
*
* Reading from an ipstream reads the command's standard output and/or
* standard error (depending on how the ipstream is opened)
* and the command's standard input is the same as that of the process
* that created the object, unless altered by the command itself.
*/
template <typename CharT, typename Traits = std::char_traits<CharT> >
class basic_ipstream
: public std::basic_istream<CharT, Traits>
, public pstream_common<CharT, Traits>
, virtual public pstreams
{
typedef std::basic_istream<CharT, Traits> istream_type;
typedef pstream_common<CharT, Traits> pbase_type;
using pbase_type::buf_; // declare name in this scope
// Ensure a basic_ipstream will read from at least one pipe
pmode readable(pmode mode)
{
if (!(mode & (pstdout|pstderr)))
mode |= pstdout;
return mode;
}
public:
/// Type used to specify how to connect to the process.
typedef typename pbase_type::pmode pmode;
/// Type used to hold the arguments for a command.
typedef typename pbase_type::argv_type argv_type;
/// Default constructor, creates an uninitialised stream.
basic_ipstream()
: istream_type(NULL), pbase_type()
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling do_open() with the supplied
* arguments.
*
* @param cmd a string containing a shell command.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, pmode)
*/
explicit
basic_ipstream(const std::string& cmd, pmode mode = pstdout)
: istream_type(NULL), pbase_type(cmd, readable(mode))
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling do_open() with the supplied
* arguments.
*
* @param file a string containing the pathname of a program to execute.
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
basic_ipstream( const std::string& file,
const argv_type& argv,
pmode mode = pstdout )
: istream_type(NULL), pbase_type(file, argv, readable(mode))
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling
* @c do_open(argv[0],argv,mode|pstdout)
*
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
explicit
basic_ipstream(const argv_type& argv, pmode mode = pstdout)
: istream_type(NULL), pbase_type(argv.at(0), argv, readable(mode))
{ }
#if __cplusplus >= 201103L
template<typename T, typename = stringable<T>>
explicit
basic_ipstream(std::initializer_list<T> args, pmode mode = pstdout)
: basic_ipstream(argv_type(args.begin(), args.end()), mode)
{ }
basic_ipstream(basic_ipstream&& rhs)
: istream_type(std::move(rhs))
, pbase_type(std::move(rhs))
{ istream_type::set_rdbuf(std::addressof(pbase_type::buf_)); }
basic_ipstream&
operator=(basic_ipstream&& rhs)
{
istream_type::operator=(std::move(rhs));
pbase_type::operator=(std::move(rhs));
return *this;
}
void
swap(basic_ipstream& rhs)
{
istream_type::swap(rhs);
pbase_type::swap(rhs);
}
#endif // C++11
/**
* @brief Destructor.
*
* Closes the stream and waits for the child to exit.
*/
~basic_ipstream()
{ }
/**
* @brief Start a process.
*
* Calls do_open( @a cmd , @a mode|pstdout ).
*
* @param cmd a string containing a shell command.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, pmode)
*/
void
open(const std::string& cmd, pmode mode = pstdout)
{
this->do_open(cmd, readable(mode));
}
/**
* @brief Start a process.
*
* Calls do_open( @a file , @a argv , @a mode|pstdout ).
*
* @param file a string containing the pathname of a program to execute.
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
void
open( const std::string& file,
const argv_type& argv,
pmode mode = pstdout )
{
this->do_open(file, argv, readable(mode));
}
/**
* @brief Set streambuf to read from process' @c stdout.
* @return @c *this
*/
basic_ipstream&
out()
{
this->buf_.read_err(false);
return *this;
}
/**
* @brief Set streambuf to read from process' @c stderr.
* @return @c *this
*/
basic_ipstream&
err()
{
this->buf_.read_err(true);
return *this;
}
};
/**
* @class basic_opstream
* @brief Class template for Output PStreams.
*
* Writing to an open opstream writes to the standard input of the command;
* the command's standard output is the same as that of the process that
* created the pstream object, unless altered by the command itself.
*/
template <typename CharT, typename Traits = std::char_traits<CharT> >
class basic_opstream
: public std::basic_ostream<CharT, Traits>
, public pstream_common<CharT, Traits>
, virtual public pstreams
{
typedef std::basic_ostream<CharT, Traits> ostream_type;
typedef pstream_common<CharT, Traits> pbase_type;
using pbase_type::buf_; // declare name in this scope
public:
/// Type used to specify how to connect to the process.
typedef typename pbase_type::pmode pmode;
/// Type used to hold the arguments for a command.
typedef typename pbase_type::argv_type argv_type;
/// Default constructor, creates an uninitialised stream.
basic_opstream()
: ostream_type(NULL), pbase_type()
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling do_open() with the supplied
* arguments.
*
* @param cmd a string containing a shell command.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, pmode)
*/
explicit
basic_opstream(const std::string& cmd, pmode mode = pstdin)
: ostream_type(NULL), pbase_type(cmd, mode|pstdin)
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling do_open() with the supplied
* arguments.
*
* @param file a string containing the pathname of a program to execute.
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
basic_opstream( const std::string& file,
const argv_type& argv,
pmode mode = pstdin )
: ostream_type(NULL), pbase_type(file, argv, mode|pstdin)
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling
* @c do_open(argv[0],argv,mode|pstdin)
*
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
explicit
basic_opstream(const argv_type& argv, pmode mode = pstdin)
: ostream_type(NULL), pbase_type(argv.at(0), argv, mode|pstdin)
{ }
#if __cplusplus >= 201103L
/**
* @brief Constructor that initialises the stream by starting a process.
*
* @param args a list of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
template<typename T, typename = stringable<T>>
explicit
basic_opstream(std::initializer_list<T> args, pmode mode = pstdin)
: basic_opstream(argv_type(args.begin(), args.end()), mode)
{ }
basic_opstream(basic_opstream&& rhs)
: ostream_type(std::move(rhs))
, pbase_type(std::move(rhs))
{ ostream_type::set_rdbuf(std::addressof(pbase_type::buf_)); }
basic_opstream&
operator=(basic_opstream&& rhs)
{
ostream_type::operator=(std::move(rhs));
pbase_type::operator=(std::move(rhs));
return *this;
}
void
swap(basic_opstream& rhs)
{
ostream_type::swap(rhs);
pbase_type::swap(rhs);
}
#endif // C++11
/**
* @brief Destructor
*
* Closes the stream and waits for the child to exit.
*/
~basic_opstream() { }
/**
* @brief Start a process.
*
* Calls do_open( @a cmd , @a mode|pstdin ).
*
* @param cmd a string containing a shell command.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, pmode)
*/
void
open(const std::string& cmd, pmode mode = pstdin)
{
this->do_open(cmd, mode|pstdin);
}
/**
* @brief Start a process.
*
* Calls do_open( @a file , @a argv , @a mode|pstdin ).
*
* @param file a string containing the pathname of a program to execute.
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
void
open( const std::string& file,
const argv_type& argv,
pmode mode = pstdin)
{
this->do_open(file, argv, mode|pstdin);
}
};
/**
* @class basic_pstream
* @brief Class template for Bidirectional PStreams.
*
* Writing to a pstream opened with @c pmode @c pstdin writes to the
* standard input of the command.
* Reading from a pstream opened with @c pmode @c pstdout and/or @c pstderr
* reads the command's standard output and/or standard error.
* Any of the process' @c stdin, @c stdout or @c stderr that is not
* connected to the pstream (as specified by the @c pmode)
* will be the same as the process that created the pstream object,
* unless altered by the command itself.
*/
template <typename CharT, typename Traits = std::char_traits<CharT> >
class basic_pstream
: public std::basic_iostream<CharT, Traits>
, public pstream_common<CharT, Traits>
, virtual public pstreams
{
typedef std::basic_iostream<CharT, Traits> iostream_type;
typedef pstream_common<CharT, Traits> pbase_type;
using pbase_type::buf_; // declare name in this scope
public:
/// Type used to specify how to connect to the process.
typedef typename pbase_type::pmode pmode;
/// Type used to hold the arguments for a command.
typedef typename pbase_type::argv_type argv_type;
/// Default constructor, creates an uninitialised stream.
basic_pstream()
: iostream_type(NULL), pbase_type()
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling do_open() with the supplied
* arguments.
*
* @param cmd a string containing a shell command.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, pmode)
*/
explicit
basic_pstream(const std::string& cmd, pmode mode = pstdout|pstdin)
: iostream_type(NULL), pbase_type(cmd, mode)
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling do_open() with the supplied
* arguments.
*
* @param file a string containing the pathname of a program to execute.
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
basic_pstream( const std::string& file,
const argv_type& argv,
pmode mode = pstdout|pstdin )
: iostream_type(NULL), pbase_type(file, argv, mode)
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling
* @c do_open(argv[0],argv,mode)
*
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
explicit
basic_pstream(const argv_type& argv, pmode mode = pstdout|pstdin)
: iostream_type(NULL), pbase_type(argv.at(0), argv, mode)
{ }
#if __cplusplus >= 201103L
/**
* @brief Constructor that initialises the stream by starting a process.
*
* @param l a list of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
template<typename T, typename = stringable<T>>
explicit
basic_pstream(std::initializer_list<T> l, pmode mode = pstdout|pstdin)
: basic_pstream(argv_type(l.begin(), l.end()), mode)
{ }
basic_pstream(basic_pstream&& rhs)
: iostream_type(std::move(rhs))
, pbase_type(std::move(rhs))
{ iostream_type::set_rdbuf(std::addressof(pbase_type::buf_)); }
basic_pstream&
operator=(basic_pstream&& rhs)
{
iostream_type::operator=(std::move(rhs));
pbase_type::operator=(std::move(rhs));
return *this;
}
void
swap(basic_pstream& rhs)
{
iostream_type::swap(rhs);
pbase_type::swap(rhs);
}
#endif // C++11
/**
* @brief Destructor
*
* Closes the stream and waits for the child to exit.
*/
~basic_pstream() { }
/**
* @brief Start a process.
*
* Calls do_open( @a cnd , @a mode ).
*
* @param cmd a string containing a shell command.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, pmode)
*/
void
open(const std::string& cmd, pmode mode = pstdout|pstdin)
{
this->do_open(cmd, mode);
}
/**
* @brief Start a process.
*
* Calls do_open( @a file , @a argv , @a mode ).
*
* @param file a string containing the pathname of a program to execute.
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
void
open( const std::string& file,
const argv_type& argv,
pmode mode = pstdout|pstdin )
{
this->do_open(file, argv, mode);
}
/**
* @brief Set streambuf to read from process' @c stdout.
* @return @c *this
*/
basic_pstream&
out()
{
this->buf_.read_err(false);
return *this;
}
/**
* @brief Set streambuf to read from process' @c stderr.
* @return @c *this
*/
basic_pstream&
err()
{
this->buf_.read_err(true);
return *this;
}
};
/**
* @class basic_rpstream
* @brief Class template for Restricted PStreams.
*
* Writing to an rpstream opened with @c pmode @c pstdin writes to the
* standard input of the command.
* It is not possible to read directly from an rpstream object, to use
* an rpstream as in istream you must call either basic_rpstream::out()
* or basic_rpstream::err(). This is to prevent accidental reads from
* the wrong input source. If the rpstream was not opened with @c pmode
* @c pstderr then the class cannot read the process' @c stderr, and
* basic_rpstream::err() will return an istream that reads from the
* process' @c stdout, and vice versa.
* Reading from an rpstream opened with @c pmode @c pstdout and/or
* @c pstderr reads the command's standard output and/or standard error.
* Any of the process' @c stdin, @c stdout or @c stderr that is not
* connected to the pstream (as specified by the @c pmode)
* will be the same as the process that created the pstream object,
* unless altered by the command itself.
*/
template <typename CharT, typename Traits = std::char_traits<CharT> >
class basic_rpstream
: public std::basic_ostream<CharT, Traits>
, private std::basic_istream<CharT, Traits>
, private pstream_common<CharT, Traits>
, virtual public pstreams
{
typedef std::basic_ostream<CharT, Traits> ostream_type;
typedef std::basic_istream<CharT, Traits> istream_type;
typedef pstream_common<CharT, Traits> pbase_type;
using pbase_type::buf_; // declare name in this scope
public:
/// Type used to specify how to connect to the process.
typedef typename pbase_type::pmode pmode;
/// Type used to hold the arguments for a command.
typedef typename pbase_type::argv_type argv_type;
/// Default constructor, creates an uninitialised stream.
basic_rpstream()
: ostream_type(NULL), istream_type(NULL), pbase_type()
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling do_open() with the supplied
* arguments.
*
* @param cmd a string containing a shell command.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, pmode)
*/
explicit
basic_rpstream(const std::string& cmd, pmode mode = pstdout|pstdin)
: ostream_type(NULL) , istream_type(NULL) , pbase_type(cmd, mode)
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling do_open() with the supplied
* arguments.
*
* @param file a string containing the pathname of a program to execute.
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
basic_rpstream( const std::string& file,
const argv_type& argv,
pmode mode = pstdout|pstdin )
: ostream_type(NULL), istream_type(NULL), pbase_type(file, argv, mode)
{ }
/**
* @brief Constructor that initialises the stream by starting a process.
*
* Initialises the stream buffer by calling
* @c do_open(argv[0],argv,mode)
*
* @param argv a vector of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
explicit
basic_rpstream(const argv_type& argv, pmode mode = pstdout|pstdin)
: ostream_type(NULL), istream_type(NULL)
, pbase_type(argv.at(0), argv, mode)
{ }
#if __cplusplus >= 201103L
/**
* @brief Constructor that initialises the stream by starting a process.
*
* @param l a list of argument strings passed to the new program.
* @param mode the I/O mode to use when opening the pipe.
* @see do_open(const std::string&, const argv_type&, pmode)
*/
template<typename T, typename = stringable<T>>
explicit
basic_rpstream(std::initializer_list<T> l, pmode mode = pstdout|pstdin)
: basic_rpstream(argv_type(l.begin(), l.end()), mode)
{ }
basic_rpstream(basic_rpstream&& rhs)
: ostream_type(NULL), istream_type(std::move(rhs))
, pbase_type(std::move(rhs))
{ istream_type::set_rdbuf(std::addressof(pbase_type::buf_)); }