blob: 9e3f738743712e6a0319ded47af63518b4518dd0 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- IOChannel.h ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef lldb_IOChannel_h_
11#define lldb_IOChannel_h_
12
13#include <string>
14#include <queue>
15
16#include <editline/readline.h>
17#include <histedit.h>
Caroline Ticebd13b8d2010-09-29 18:35:42 +000018#include <pthread.h>
19#include <sys/time.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21#include "Driver.h"
22
23class IOChannel : public lldb::SBBroadcaster
24{
25public:
26 enum {
27 eBroadcastBitHasUserInput = (1 << 0),
28 eBroadcastBitUserInterrupt = (1 << 1),
29 eBroadcastBitThreadShouldExit = (1 << 2),
30 eBroadcastBitThreadDidExit = (1 << 3),
31 eBroadcastBitThreadDidStart = (1 << 4),
32 eBroadcastBitsSTDOUT = (1 << 5),
33 eBroadcastBitsSTDERR = (1 << 6),
34 eBroadcastBitsSTDIN = (1 << 7),
35 eAllEventBits = 0xffffffff
36 };
37
Caroline Tice969ed3d2011-05-02 20:41:46 +000038 IOChannel (FILE *editline_in,
39 FILE *editline_out,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 FILE *out,
41 FILE *err,
42 Driver *driver = NULL);
43
44 virtual
45 ~IOChannel ();
46
47 bool
48 Start ();
49
50 bool
51 Stop ();
52
53 static void *
54 IOReadThread (void *);
55
56 void
57 Run ();
58
59 void
Caroline Tice969ed3d2011-05-02 20:41:46 +000060 OutWrite (const char *buffer, size_t len, bool asynchronous);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
62 void
Caroline Tice969ed3d2011-05-02 20:41:46 +000063 ErrWrite (const char *buffer, size_t len, bool asynchronous);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
65 bool
66 LibeditGetInput (std::string &);
Caroline Tice969ed3d2011-05-02 20:41:46 +000067
68 static void
69 LibeditOutputBytesReceived (void *baton, const void *src,size_t src_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070
71 void
72 SetPrompt ();
73
74 void
75 RefreshPrompt ();
76
77 void
78 AddCommandToQueue (const char *command);
79
80 bool
81 GetCommandFromQueue (std::string &cmd);
82
83 int
84 CommandQueueSize () const;
85
86 void
87 ClearCommandQueue ();
88
89 bool
90 CommandQueueIsEmpty () const;
91
92 const char *
93 GetPrompt ();
94
Greg Claytonc982c762010-07-09 20:39:50 +000095 static unsigned char
96 ElCompletionFn (EditLine *e, int ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
Caroline Ticebd13b8d2010-09-29 18:35:42 +000098protected:
99
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 bool
101 IsGettingCommand () const;
102
Caroline Ticebd13b8d2010-09-29 18:35:42 +0000103 void
104 SetGettingCommand (bool new_value);
105
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106private:
107
Caroline Ticebd13b8d2010-09-29 18:35:42 +0000108 pthread_mutex_t m_output_mutex;
109 struct timeval m_enter_elgets_time;
110
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 Driver *m_driver;
112 lldb::thread_t m_read_thread;
113 bool m_read_thread_should_exit;
114 FILE *m_out_file;
115 FILE *m_err_file;
116 std::queue<std::string> m_command_queue;
117 const char *m_completion_key;
118
119 EditLine *m_edit_line;
120 History *m_history;
121 HistEvent m_history_event;
122 bool m_getting_command;
Caroline Tice969ed3d2011-05-02 20:41:46 +0000123 bool m_expecting_prompt;
124 std::string m_prompt_str; // for accumlating the prompt as it gets written out by editline
Caroline Tice86a73f92011-05-03 20:53:11 +0000125 bool m_refresh_request_pending;
Greg Claytonc982c762010-07-09 20:39:50 +0000126
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 void
128 HistorySaveLoad (bool save);
Greg Claytonc982c762010-07-09 20:39:50 +0000129
130 unsigned char
131 HandleCompletion (EditLine *e, int ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132};
133
Caroline Ticebd13b8d2010-09-29 18:35:42 +0000134class IOLocker
135{
136public:
137
138 IOLocker (pthread_mutex_t &mutex);
139
140 ~IOLocker ();
141
142protected:
143
144 pthread_mutex_t *m_mutex_ptr;
145
146private:
147
148 IOLocker (const IOLocker&);
149 const IOLocker& operator= (const IOLocker&);
150};
151
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152#endif // lldb_IOChannel_h_