1 /* 2 * Copyright 2007 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ramshankar, v.ramshankar@gmail.com 7 */ 8 #ifndef _COMMAND_PIPE_H 9 #define _COMMAND_PIPE_H 10 11 12 #include <stdio.h> 13 14 #include <List.h> 15 #include <OS.h> 16 17 18 class BMessage; 19 class BMessenger; 20 class BString; 21 22 namespace BPrivate { 23 24 class BCommandPipe { 25 public: 26 BCommandPipe(); 27 virtual ~BCommandPipe(); 28 29 status_t AddArg(const char* argv); 30 void PrintToStream() const; 31 32 // FlushArgs() deletes the commands while Close() explicity closes all 33 // pending pipe-ends 34 // Note: Internally FlushArgs() calls Close() 35 void FlushArgs(); 36 void Close(); 37 38 // If you use these, you must explicitly call "close" for the parameters 39 // (outdes/errdes) when you are done with them! 40 thread_id Pipe(int* outdes, int* errdes) const; 41 thread_id Pipe(int* outdes) const; 42 thread_id PipeAll(int* outAndErrDes) const; 43 44 // If you use these, you need NOT call "fclose" for the parameters 45 // (out/err) when you are done with them, also you need not do any 46 // allocation for these FILE* pointers, just use FILE* out = NULL 47 // and pass &out and so on... 48 thread_id PipeInto(FILE** _out, FILE** _err); 49 thread_id PipeInto(FILE** _outAndErr); 50 51 // Run() is a synchronous call, and waits till the command has finished 52 // executing RunAsync() is an asynchronous call that returns immediately 53 // after launching the command Neither of these bother about redirecting 54 // pipes for you to use 55 void Run(); 56 void RunAsync(); 57 58 // This function reads line-by-line from "file", cancels its reading 59 // when "*cancel" is true. It reports each line it has read to "target" 60 // using the supplied "_message" and string field name. "cancel" can be 61 // NULL 62 BString ReadLines(FILE* file, bool* cancel, 63 BMessenger& target, const BMessage& message, 64 const BString& stringFieldName); 65 66 // You need NOT free/delete the return array of strings 67 const char** Argv(int32& _argc) const; 68 69 // Stardard append operators, if you use pointers to a BCommandPipe, 70 // you must use *pipe << "command"; and not pipe << "command" (as it 71 // will not compile that way) 72 BCommandPipe& operator<<(const char *arg); 73 BCommandPipe& operator<<(const BString& arg); 74 BCommandPipe& operator<<(const BCommandPipe& arg); 75 76 protected: 77 BList fArgList; 78 int fOutDes[2]; 79 int fErrDes[2]; 80 bool fOutDesOpen; 81 bool fErrDesOpen; 82 }; 83 84 } // namespace BPrivate 85 86 using BPrivate::BCommandPipe; 87 88 #endif // _COMMAND_PIPE_H 89