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
18class BMessage;
19class BMessenger;
20class BString;
21
22namespace BPrivate {
23
24class BCommandPipe {
25public:
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	// You MUST NOT free/delete the strings in the array, but you MUST free
39	// just the array itself.
40			const char**		Argv(int32& _argc) const;
41
42	// If you use these, you must explicitly call "close" for the parameters
43	// (stdOut/stdErr) when you are done with them!
44			thread_id			Pipe(int* stdOut, int* stdErr) const;
45			thread_id			Pipe(int* stdOut) const;
46			thread_id			PipeAll(int* stdOutAndErr) const;
47
48	// If you use these, you need NOT call "fclose" for the parameters
49	// (out/err) when you are done with them, also you need not do any
50	// allocation for these FILE* pointers, just use FILE* out = NULL
51	// and pass &out and so on...
52			thread_id			PipeInto(FILE** _out, FILE** _err);
53			thread_id			PipeInto(FILE** _outAndErr);
54
55	// Run() is a synchronous call, and waits till the command has finished
56	// executing RunAsync() is an asynchronous call that returns immediately
57	// after launching the command Neither of these bother about redirecting
58	// pipes for you to use
59			void				Run();
60			void				RunAsync();
61
62	// Reading the Pipe output
63
64			class LineReader {
65			public:
66				virtual				~LineReader() {}
67				virtual	bool		IsCanceled() = 0;
68				virtual	status_t	ReadLine(const BString& line) = 0;
69				// TODO: Add a Timeout() method.
70			};
71
72	// This function reads line-by-line from "file". It calls IsCanceled()
73	// on the passed LineReader instance for each byte read from file
74	// (currently). And it calls ReadLine() for each complete line.
75			status_t			ReadLines(FILE* file, LineReader* lineReader);
76	// This method can be used to read the entire file into a BString.
77			BString				ReadLines(FILE* file);
78
79	// Stardard append operators, if you use pointers to a BCommandPipe,
80	// you must use *pipe << "command"; and not pipe << "command" (as it
81	// will not compile that way)
82			BCommandPipe&		operator<<(const char* arg);
83			BCommandPipe&		operator<<(const BString& arg);
84			BCommandPipe&		operator<<(const BCommandPipe& arg);
85
86protected:
87			BList				fArgList;
88			int					fStdOut[2];
89			int					fStdErr[2];
90			bool				fStdOutOpen;
91			bool				fStdErrOpen;
92};
93
94}	// namespace BPrivate
95
96using BPrivate::BCommandPipe;
97
98#endif // _COMMAND_PIPE_H
99