1/*
2 * Copyright (c) 1998-2007 Matthijs Hollemans
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5#ifndef GREPPER_H
6#define GREPPER_H
7
8#include <Messenger.h>
9
10class FileIterator;
11class Model;
12
13// Executes "grep" in a background thread.
14class Grepper {
15public:
16								Grepper(const char* pattern, const Model* model,
17									const BHandler* target,
18									FileIterator* iterator);
19	virtual						~Grepper();
20
21			bool				IsValid() const;
22
23			void				Start();
24			void				Cancel();
25
26private:
27	// Spawns the real grepper threads.
28	static	int32				_SpawnRunnerThread(void* cookie);
29	static	int32				_SpawnWriterThread(void* cookie);
30
31	// The threads functions that does the actual grepping.
32			int32				_RunnerThread();
33			int32				_WriterThread();
34
35	// Remembers, and possibly escapes, the search pattern.
36			void				_SetPattern(const char* source);
37
38	// Prepends all quotes, dollars and backslashes with at backslash
39	// to prevent the shell from misinterpreting them.
40			bool				_EscapeSpecialChars(char* buffer,
41									ssize_t bufferSize);
42
43	private:
44	// The (escaped) search pattern.
45			char*				fPattern;
46
47	// The settings from the model.
48			BMessenger			fTarget;
49			bool				fRegularExpression : 1;
50			bool				fCaseSensitive : 1;
51			uint32				fEncoding;
52
53	// The supplier of files to grep
54			FileIterator*		fIterator;
55
56	// Our thread's ID.
57			thread_id			fRunnerThreadId;
58	// xargs input pipe
59			int					fXargsInput;
60
61	// Whether our thread must quit.
62	volatile bool				fMustQuit;
63};
64
65#endif // GREPPER_H
66