aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-11-23 22:58:19 +0000
committerMike Buland <eichlan@xagasoft.com>2007-11-23 22:58:19 +0000
commitcd215f0da23e16c3f1a7200f2b9f67f23c9b4be7 (patch)
treeef4b144cbc9a68110c1ea6a0b2eb73be2cbd02bb /src
parentb5b68088f28b2593bfbf910a46fd52775007e8b3 (diff)
downloadlibbu++-cd215f0da23e16c3f1a7200f2b9f67f23c9b4be7.tar.gz
libbu++-cd215f0da23e16c3f1a7200f2b9f67f23c9b4be7.tar.bz2
libbu++-cd215f0da23e16c3f1a7200f2b9f67f23c9b4be7.tar.xz
libbu++-cd215f0da23e16c3f1a7200f2b9f67f23c9b4be7.zip
Added the Process stream class, this will allow us to do some really cool stuff
coming up...it's just like popen only cool and managed, and streamey.
Diffstat (limited to 'src')
-rw-r--r--src/fstring.h2
-rw-r--r--src/process.cpp146
-rw-r--r--src/process.h64
-rw-r--r--src/tests/procs.cpp22
4 files changed, 234 insertions, 0 deletions
diff --git a/src/fstring.h b/src/fstring.h
index ee6be28..8cccd5c 100644
--- a/src/fstring.h
+++ b/src/fstring.h
@@ -246,6 +246,8 @@ namespace Bu
246 { 246 {
247 if( nLength == nNewSize ) 247 if( nLength == nNewSize )
248 return; 248 return;
249 if( nNewSize < 0 )
250 nNewSize = 0;
249 251
250 flatten(); 252 flatten();
251 253
diff --git a/src/process.cpp b/src/process.cpp
new file mode 100644
index 0000000..8fe98f3
--- /dev/null
+++ b/src/process.cpp
@@ -0,0 +1,146 @@
1/*
2 * Copyright (C) 2007 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "process.h"
9#include <sys/types.h>
10#include <unistd.h>
11#include <stdarg.h>
12
13Bu::Process::Process( const char *sName, char *const argv[] )
14{
15 gexec( sName, argv );
16}
17
18Bu::Process::Process( const char *sName, const char *argv, ...)
19{
20 int iCnt = 0;
21 va_list ap;
22 va_start( ap, argv );
23 for(; va_arg( ap, const char *); iCnt++ );
24 va_end( ap );
25
26 char const **list = new char const *[iCnt+2];
27 va_start( ap, argv );
28 list[0] = argv;
29 for( int j = 1; j < iCnt; j++ )
30 list[j] = va_arg( ap, const char *);
31 list[iCnt+1] = NULL;
32 va_end( ap );
33
34 gexec( sName, (char *const *)list );
35 delete[] list;
36}
37
38Bu::Process::~Process()
39{
40}
41
42void Bu::Process::gexec( const char *sName, char *const argv[] )
43{
44 int iaStdIn[2];
45 int iaStdOut[2];
46 int iaStdErr[2];
47 pipe( iaStdIn );
48 pipe( iaStdOut );
49 pipe( iaStdErr );
50
51 iStdIn = iaStdIn[1];
52 iStdOut = iaStdOut[0];
53 iStdErr = iaStdErr[0];
54
55 iPid = fork();
56 if( iPid == 0 )
57 {
58 ::close( iaStdIn[1] );
59 ::close( iaStdOut[0] );
60 ::close( iaStdErr[0] );
61 dup2( iaStdIn[0], 0 );
62 dup2( iaStdOut[1], 1 );
63 dup2( iaStdErr[1], 2 );
64 execvp( sName, argv );
65 }
66}
67
68void Bu::Process::close()
69{
70}
71
72size_t Bu::Process::read( void *pBuf, size_t nBytes )
73{
74 return ::read( iStdOut, pBuf, nBytes );
75}
76
77size_t Bu::Process::write( const void *pBuf, size_t nBytes )
78{
79 return ::write( iStdIn, pBuf, nBytes );
80}
81
82long Bu::Process::tell()
83{
84 return 0;
85}
86
87void Bu::Process::seek( long offset )
88{
89}
90
91void Bu::Process::setPos( long pos )
92{
93}
94
95void Bu::Process::setPosEnd( long pos )
96{
97}
98
99bool Bu::Process::isEOS()
100{
101 return false;
102}
103
104bool Bu::Process::isOpen()
105{
106 return true;
107}
108
109void Bu::Process::flush()
110{
111}
112
113bool Bu::Process::canRead()
114{
115 return true;
116}
117
118bool Bu::Process::canWrite()
119{
120 return true;
121}
122
123bool Bu::Process::isReadable()
124{
125 return true;
126}
127
128bool Bu::Process::isWritable()
129{
130 return true;
131}
132
133bool Bu::Process::isSeekable()
134{
135 return false;
136}
137
138bool Bu::Process::isBlocking()
139{
140 return true;
141}
142
143void Bu::Process::setBlocking( bool bBlocking )
144{
145}
146
diff --git a/src/process.h b/src/process.h
new file mode 100644
index 0000000..dbf1553
--- /dev/null
+++ b/src/process.h
@@ -0,0 +1,64 @@
1/*
2 * Copyright (C) 2007 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_PROCESS_H
9#define BU_PROCESS_H
10
11#include <stdint.h>
12#include <sys/types.h>
13
14#include "bu/stream.h"
15#include "bu/fstring.h"
16
17namespace Bu
18{
19 /**
20 * Runs a program and attaches streams to it's stdin, stdout, and stderr.
21 * Reading from a Bu::Process will read from the program's standard output,
22 * writing to a Bu::Process will write to the program's standard input.
23 */
24 class Process : public Bu::Stream
25 {
26 public:
27 Process( const char *sName, char *const argv[] );
28 Process( const char *sName, const char *argv, ...);
29 virtual ~Process();
30
31 virtual void close();
32 virtual size_t read( void *pBuf, size_t nBytes );
33 virtual size_t write( const void *pBuf, size_t nBytes );
34
35 virtual long tell();
36 virtual void seek( long offset );
37 virtual void setPos( long pos );
38 virtual void setPosEnd( long pos );
39 virtual bool isEOS();
40 virtual bool isOpen();
41
42 virtual void flush();
43
44 virtual bool canRead();
45 virtual bool canWrite();
46
47 virtual bool isReadable();
48 virtual bool isWritable();
49 virtual bool isSeekable();
50
51 virtual bool isBlocking();
52 virtual void setBlocking( bool bBlocking=true );
53
54 private:
55 int iStdIn;
56 int iStdOut;
57 int iStdErr;
58 pid_t iPid;
59
60 void gexec( const char *sName, char *const argv[] );
61 };
62}
63
64#endif
diff --git a/src/tests/procs.cpp b/src/tests/procs.cpp
new file mode 100644
index 0000000..53e5142
--- /dev/null
+++ b/src/tests/procs.cpp
@@ -0,0 +1,22 @@
1#include "bu/process.h"
2
3#include <stdio.h>
4
5int main( int agrc, char *argv[] )
6{
7 Bu::Process p( argv[1], argv+1 );
8
9 char buf[1000];
10 for(;;)
11 {
12 int iSize = p.read( buf, 1000 );
13 if( iSize == 0 )
14 break;
15 fwrite( buf, iSize, 1, stdout );
16 if( iSize < 1000 )
17 break;
18 }
19
20 return 0;
21}
22